1// RUN: %clang_profgen -DCHECK_SYMBOLS -O3 -o %t.symbols %s
2// RUN: llvm-nm %t.symbols | FileCheck %s --check-prefix=CHECK-SYMBOLS
3// RUN: %clang_profgen -O3 -o %t %s
4// RUN: %run %t %t.profraw
5// RUN: llvm-profdata merge -o %t.profdata %t.profraw
6// RUN: %clang_profuse=%t.profdata -o - -S -emit-llvm %s | FileCheck %s
7
8#include <stdint.h>
9#include <stdlib.h>
10
11#ifndef CHECK_SYMBOLS
12#include <stdio.h>
13#endif
14
15int __llvm_profile_runtime = 0;
16uint64_t __llvm_profile_get_size_for_buffer(void);
17int __llvm_profile_write_buffer(char *);
18void __llvm_profile_merge_from_buffer(const char *, uint64_t Size);
19
20int write_buffer(uint64_t, const char *);
21int main(int argc, const char *argv[]) {
22  // CHECK-LABEL: define {{.*}} @main(
23  // CHECK: br i1 %{{.*}}, label %{{.*}}, label %{{.*}}, !prof ![[PD1:[0-9]+]]
24  if (argc < 2)
25    return 1;
26
27  const uint64_t MaxSize = 10000;
28  static char Buffer[MaxSize];
29
30  uint64_t Size = __llvm_profile_get_size_for_buffer();
31  if (Size > MaxSize)
32    return 1;
33  int Write = __llvm_profile_write_buffer(Buffer);
34  if (Write)
35    return Write;
36
37#ifdef CHECK_SYMBOLS
38  // Don't write it out.  Since we're checking the symbols, we don't have libc
39  // available.
40  // Call merge function to make sure it does not bring in libc deps:
41  __llvm_profile_merge_from_buffer(Buffer, Size);
42  return 0;
43#else
44  // Actually write it out so we can FileCheck the output.
45  FILE *File = fopen(argv[1], "w");
46  if (!File)
47    return 1;
48  if (fwrite(Buffer, 1, Size, File) != Size)
49    return 1;
50  return fclose(File);
51#endif
52}
53// CHECK: ![[PD1]] = !{!"branch_weights", i32 1, i32 2}
54
55// CHECK-SYMBOLS-NOT: {{ }}___cxx_global_var_init
56// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_register_write_file_atexit
57// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_set_filename
58// CHECK-SYMBOLS-NOT: {{ }}___llvm_profile_write_file
59// CHECK-SYMBOLS-NOT: {{ }}_fdopen
60// CHECK-SYMBOLS-NOT: {{ }}_fopen
61// CHECK-SYMBOLS-NOT: {{ }}_fwrite
62// CHECK-SYMBOLS-NOT: {{ }}_getenv
63// CHECK-SYMBOLS-NOT: {{ }}getenv
64// CHECK-SYMBOLS-NOT: {{ }}_malloc
65// CHECK-SYMBOLS-NOT: {{ }}malloc
66// CHECK-SYMBOLS-NOT: {{ }}_calloc
67// CHECK-SYMBOLS-NOT: {{ }}calloc
68// CHECK-SYMBOLS-NOT: {{ }}_free
69// CHECK-SYMBOLS-NOT: {{ }}free
70// CHECK-SYMBOLS-NOT: {{ }}_open
71