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