1*7c3d14c8STreehugger Robot // RUN: %clang_profgen -O2 -o %t %s
2*7c3d14c8STreehugger Robot // RUN: %run %t %t.profraw 1 1
3*7c3d14c8STreehugger Robot // RUN: llvm-profdata show --all-functions --counts %t.profraw | FileCheck %s
4*7c3d14c8STreehugger Robot
5*7c3d14c8STreehugger Robot #include <stdint.h>
6*7c3d14c8STreehugger Robot #include <stdio.h>
7*7c3d14c8STreehugger Robot #include <stdlib.h>
8*7c3d14c8STreehugger Robot
9*7c3d14c8STreehugger Robot int __llvm_profile_runtime = 0;
10*7c3d14c8STreehugger Robot uint64_t __llvm_profile_get_size_for_buffer(void);
11*7c3d14c8STreehugger Robot int __llvm_profile_write_buffer(char *);
12*7c3d14c8STreehugger Robot void __llvm_profile_reset_counters(void);
13*7c3d14c8STreehugger Robot void __llvm_profile_merge_from_buffer(const char *, uint64_t);
14*7c3d14c8STreehugger Robot
dumpBuffer(const char * FileN,const char * Buffer,uint64_t Size)15*7c3d14c8STreehugger Robot int dumpBuffer(const char *FileN, const char *Buffer, uint64_t Size) {
16*7c3d14c8STreehugger Robot FILE *File = fopen(FileN, "w");
17*7c3d14c8STreehugger Robot if (!File)
18*7c3d14c8STreehugger Robot return 1;
19*7c3d14c8STreehugger Robot if (fwrite(Buffer, 1, Size, File) != Size)
20*7c3d14c8STreehugger Robot return 1;
21*7c3d14c8STreehugger Robot return fclose(File);
22*7c3d14c8STreehugger Robot }
23*7c3d14c8STreehugger Robot
24*7c3d14c8STreehugger Robot int g = 0;
foo(char c)25*7c3d14c8STreehugger Robot void foo(char c) {
26*7c3d14c8STreehugger Robot if (c == '1')
27*7c3d14c8STreehugger Robot g++;
28*7c3d14c8STreehugger Robot else
29*7c3d14c8STreehugger Robot g--;
30*7c3d14c8STreehugger Robot }
31*7c3d14c8STreehugger Robot
32*7c3d14c8STreehugger Robot /* This function is not profiled */
bar(int M)33*7c3d14c8STreehugger Robot void bar(int M) { g += M; }
34*7c3d14c8STreehugger Robot
main(int argc,const char * argv[])35*7c3d14c8STreehugger Robot int main(int argc, const char *argv[]) {
36*7c3d14c8STreehugger Robot int i;
37*7c3d14c8STreehugger Robot if (argc < 4)
38*7c3d14c8STreehugger Robot return 1;
39*7c3d14c8STreehugger Robot
40*7c3d14c8STreehugger Robot const uint64_t MaxSize = 10000;
41*7c3d14c8STreehugger Robot static char Buffer[MaxSize];
42*7c3d14c8STreehugger Robot
43*7c3d14c8STreehugger Robot uint64_t Size = __llvm_profile_get_size_for_buffer();
44*7c3d14c8STreehugger Robot if (Size > MaxSize)
45*7c3d14c8STreehugger Robot return 1;
46*7c3d14c8STreehugger Robot
47*7c3d14c8STreehugger Robot /* Start profiling. */
48*7c3d14c8STreehugger Robot __llvm_profile_reset_counters();
49*7c3d14c8STreehugger Robot foo(argv[2][0]);
50*7c3d14c8STreehugger Robot /* End profiling by freezing counters. */
51*7c3d14c8STreehugger Robot if (__llvm_profile_write_buffer(Buffer))
52*7c3d14c8STreehugger Robot return 1;
53*7c3d14c8STreehugger Robot
54*7c3d14c8STreehugger Robot /* Its profile will be discarded. */
55*7c3d14c8STreehugger Robot for (i = 0; i < 10; i++)
56*7c3d14c8STreehugger Robot bar(1);
57*7c3d14c8STreehugger Robot
58*7c3d14c8STreehugger Robot /* Start profiling again and merge in previously
59*7c3d14c8STreehugger Robot saved counters in buffer. */
60*7c3d14c8STreehugger Robot __llvm_profile_reset_counters();
61*7c3d14c8STreehugger Robot __llvm_profile_merge_from_buffer(Buffer, Size);
62*7c3d14c8STreehugger Robot foo(argv[3][0]);
63*7c3d14c8STreehugger Robot /* End profiling */
64*7c3d14c8STreehugger Robot if (__llvm_profile_write_buffer(Buffer))
65*7c3d14c8STreehugger Robot return 1;
66*7c3d14c8STreehugger Robot
67*7c3d14c8STreehugger Robot /* Its profile will be discarded. */
68*7c3d14c8STreehugger Robot bar(2);
69*7c3d14c8STreehugger Robot
70*7c3d14c8STreehugger Robot /* Now it is time to dump the profile to file. */
71*7c3d14c8STreehugger Robot return dumpBuffer(argv[1], Buffer, Size);
72*7c3d14c8STreehugger Robot }
73*7c3d14c8STreehugger Robot
74*7c3d14c8STreehugger Robot // Not profiled
75*7c3d14c8STreehugger Robot // CHECK-LABEL: dumpBuffer:
76*7c3d14c8STreehugger Robot // CHECK: Counters: 3
77*7c3d14c8STreehugger Robot // CHECK-NEXT: Function count: 0
78*7c3d14c8STreehugger Robot // CHECK-NEXT: Block counts: [0, 0]
79*7c3d14c8STreehugger Robot
80*7c3d14c8STreehugger Robot // Profiled with entry count == 2
81*7c3d14c8STreehugger Robot // CHECK-LABEL: foo:
82*7c3d14c8STreehugger Robot // CHECK: Counters: 2
83*7c3d14c8STreehugger Robot // CHECK-NEXT: Function count: 2
84*7c3d14c8STreehugger Robot // CHECK-NEXT: Block counts: [2]
85*7c3d14c8STreehugger Robot
86*7c3d14c8STreehugger Robot // Not profiled
87*7c3d14c8STreehugger Robot // CHECK-LABEL: bar:
88*7c3d14c8STreehugger Robot // CHECK: Counters: 1
89*7c3d14c8STreehugger Robot // CHECK-NEXT Function count: 0
90*7c3d14c8STreehugger Robot // CHECK-NEXT Block counts: []
91*7c3d14c8STreehugger Robot
92*7c3d14c8STreehugger Robot // Not profiled
93*7c3d14c8STreehugger Robot // CHECK-LABEL: main:
94*7c3d14c8STreehugger Robot // CHECK: Counters: 6
95*7c3d14c8STreehugger Robot // CHECK-NEXT: Function count: 0
96*7c3d14c8STreehugger Robot // CHECK-NEXT: Block counts: [0, 0, 0, 0, 0]
97