1 // RUN: %clang_profgen -mllvm -vp-static-alloc=false -O2 -o %t %s 2 // RUN: env LLVM_PROFILE_FILE=%t.profraw %run %t 3 // RUN: env LLVM_PROFILE_FILE=%t-2.profraw %run %t DO_NOT_INSTRUMENT 4 // RUN: llvm-profdata merge -o %t.profdata %t.profraw 5 // RUN: llvm-profdata merge -o %t-2.profdata %t-2.profraw 6 // RUN: llvm-profdata merge -o %t-merged.profdata %t.profraw %t-2.profdata 7 // RUN: llvm-profdata show --all-functions -ic-targets %t-2.profdata | FileCheck %s -check-prefix=NO-VALUE 8 // RUN: llvm-profdata show --all-functions -ic-targets %t.profdata | FileCheck %s 9 // RUN: llvm-profdata show --all-functions -ic-targets %t-merged.profdata | FileCheck %s 10 // 11 // RUN: env LLVM_PROFILE_FILE=%t-3.profraw LLVM_VP_BUFFER_SIZE=1 %run %t 12 // RUN: env LLVM_PROFILE_FILE=%t-4.profraw LLVM_VP_BUFFER_SIZE=8 %run %t 13 // RUN: env LLVM_PROFILE_FILE=%t-5.profraw LLVM_VP_BUFFER_SIZE=128 %run %t 14 // RUN: env LLVM_PROFILE_FILE=%t-6.profraw LLVM_VP_BUFFER_SIZE=1024 %run %t 15 // RUN: env LLVM_PROFILE_FILE=%t-7.profraw LLVM_VP_BUFFER_SIZE=102400 %run %t 16 // RUN: llvm-profdata merge -o %t-3.profdata %t-3.profraw 17 // RUN: llvm-profdata merge -o %t-4.profdata %t-4.profraw 18 // RUN: llvm-profdata merge -o %t-5.profdata %t-5.profraw 19 // RUN: llvm-profdata merge -o %t-6.profdata %t-6.profraw 20 // RUN: llvm-profdata merge -o %t-7.profdata %t-7.profraw 21 // RUN: llvm-profdata show --all-functions -ic-targets %t-3.profdata | FileCheck %s 22 // RUN: llvm-profdata show --all-functions -ic-targets %t-4.profdata | FileCheck %s 23 // RUN: llvm-profdata show --all-functions -ic-targets %t-5.profdata | FileCheck %s 24 // RUN: llvm-profdata show --all-functions -ic-targets %t-6.profdata | FileCheck %s 25 // RUN: llvm-profdata show --all-functions -ic-targets %t-7.profdata | FileCheck %s 26 27 #include <stdint.h> 28 #include <stdio.h> 29 #include <stdlib.h> 30 typedef struct __llvm_profile_data __llvm_profile_data; 31 const __llvm_profile_data *__llvm_profile_begin_data(void); 32 const __llvm_profile_data *__llvm_profile_end_data(void); 33 void __llvm_profile_set_num_value_sites(__llvm_profile_data *Data, 34 uint32_t ValueKind, 35 uint16_t NumValueSites); 36 __llvm_profile_data * 37 __llvm_profile_iterate_data(const __llvm_profile_data *Data); 38 void *__llvm_get_function_addr(const __llvm_profile_data *Data); 39 void __llvm_profile_instrument_target(uint64_t TargetValue, void *Data, 40 uint32_t CounterIndex); 41 42 #define DEF_FUNC(x) \ 43 void x() {} 44 #define DEF_2_FUNCS(x) DEF_FUNC(x##_1) DEF_FUNC(x##_2) 45 #define DEF_4_FUNCS(x) DEF_2_FUNCS(x##_1) DEF_2_FUNCS(x##_2) 46 #define DEF_8_FUNCS(x) DEF_4_FUNCS(x##_1) DEF_4_FUNCS(x##_2) 47 #define DEF_16_FUNCS(x) DEF_8_FUNCS(x##_1) DEF_8_FUNCS(x##_2) 48 #define DEF_32_FUNCS(x) DEF_16_FUNCS(x##_1) DEF_16_FUNCS(x##_2) 49 #define DEF_64_FUNCS(x) DEF_32_FUNCS(x##_1) DEF_32_FUNCS(x##_2) 50 #define DEF_128_FUNCS(x) DEF_64_FUNCS(x##_1) DEF_64_FUNCS(x##_2) 51 52 #define FUNC_ADDR(x) &x, 53 #define FUNC_2_ADDRS(x) FUNC_ADDR(x##_1) FUNC_ADDR(x##_2) 54 #define FUNC_4_ADDRS(x) FUNC_2_ADDRS(x##_1) FUNC_2_ADDRS(x##_2) 55 #define FUNC_8_ADDRS(x) FUNC_4_ADDRS(x##_1) FUNC_4_ADDRS(x##_2) 56 #define FUNC_16_ADDRS(x) FUNC_8_ADDRS(x##_1) FUNC_8_ADDRS(x##_2) 57 #define FUNC_32_ADDRS(x) FUNC_16_ADDRS(x##_1) FUNC_16_ADDRS(x##_2) 58 #define FUNC_64_ADDRS(x) FUNC_32_ADDRS(x##_1) FUNC_32_ADDRS(x##_2) 59 #define FUNC_128_ADDRS(x) FUNC_64_ADDRS(x##_1) FUNC_64_ADDRS(x##_2) 60 61 DEF_8_FUNCS(callee) 62 DEF_128_FUNCS(caller) 63 64 void *CallerAddrs[] = {FUNC_128_ADDRS(caller)}; 65 void *CalleeAddrs[] = {FUNC_8_ADDRS(callee)}; 66 typedef struct CallerInfo { 67 void *CallerAddr; 68 uint32_t NS; /* Number value sites. */ 69 } CallerInfo; 70 71 CallerInfo CallerInfos[128]; 72 73 int cmpaddr(const void *p1, const void *p2) { 74 CallerInfo *addr1 = (CallerInfo *)p1; 75 CallerInfo *addr2 = (CallerInfo *)p2; 76 return (intptr_t)addr2->CallerAddr - (intptr_t)addr1->CallerAddr; 77 } 78 79 int main(int argc, const char *argv[]) { 80 unsigned S, NS = 0, I, V, doInstrument = 1; 81 const __llvm_profile_data *Data, *DataEnd; 82 83 if (argc >= 2 && !strcmp(argv[1], "DO_NOT_INSTRUMENT")) 84 doInstrument = 0; 85 86 for (I = 0; I < 128; I++) { 87 CallerInfos[I].CallerAddr = CallerAddrs[I]; 88 CallerInfos[I].NS = I; 89 } 90 qsort(CallerInfos, sizeof(CallerInfos) / sizeof(CallerInfo), sizeof(CallerInfo), 91 cmpaddr); 92 93 /* We will synthesis value profile data for 128 callers functions declared. 94 * The number of value sites for each caller function is recorded in 95 * the NS field of the CallerInfo object. For each value site, the number of 96 * callee values is determined by the site index (modulo 8). The frequency 97 * of each callee target synthesized is equal to V + 1, in which V is the 98 * index of the target value for the callsite. */ 99 100 Data = __llvm_profile_begin_data(); 101 DataEnd = __llvm_profile_end_data(); 102 103 for (; Data < DataEnd; Data = __llvm_profile_iterate_data(Data)) { 104 void *func = __llvm_get_function_addr(Data); 105 CallerInfo Key, *Res; 106 Key.CallerAddr = func; 107 Res = (CallerInfo *) bsearch(&Key, CallerInfos, sizeof(CallerInfos) / sizeof(CallerInfo), 108 sizeof(CallerInfo), cmpaddr); 109 if (Res) { 110 NS = Res->NS; 111 __llvm_profile_set_num_value_sites((__llvm_profile_data *)Data, 112 0 /*IPVK_IndirectCallTarget */, NS); 113 if (!doInstrument) { 114 continue; 115 } 116 for (S = 0; S < NS; S++) { 117 for (V = 0; V < S % 8; V++) { 118 unsigned C; 119 for (C = 0; C < V + 1; C++) 120 __llvm_profile_instrument_target((uint64_t)CalleeAddrs[V], 121 (void *)Data, S); 122 } 123 } 124 } 125 } 126 } 127 128 // NO-VALUE: Indirect Call Site Count: 127 129 // NO-VALUE-NEXT: Indirect Target Results: 130 // CHECK-LABEL: caller_1_1_1_1_2_2_1: 131 // CHECK: Indirect Call Site Count: 6 132 // CHECK-NEXT: Indirect Target Results: 133 // CHECK-NEXT: [ 1, callee_1_1_1, 1 ] 134 // CHECK-NEXT: [ 2, callee_1_1_2, 2 ] 135 // CHECK-NEXT: [ 2, callee_1_1_1, 1 ] 136 // CHECK-NEXT: [ 3, callee_1_2_1, 3 ] 137 // CHECK-NEXT: [ 3, callee_1_1_2, 2 ] 138 // CHECK-NEXT: [ 3, callee_1_1_1, 1 ] 139 // CHECK-NEXT: [ 4, callee_1_2_2, 4 ] 140 // CHECK-NEXT: [ 4, callee_1_2_1, 3 ] 141 // CHECK-NEXT: [ 4, callee_1_1_2, 2 ] 142 // CHECK-NEXT: [ 4, callee_1_1_1, 1 ] 143 // CHECK-NEXT: [ 5, callee_2_1_1, 5 ] 144 // CHECK-NEXT: [ 5, callee_1_2_2, 4 ] 145 // CHECK-NEXT: [ 5, callee_1_2_1, 3 ] 146 // CHECK-NEXT: [ 5, callee_1_1_2, 2 ] 147 // CHECK-NEXT: [ 5, callee_1_1_1, 1 ] 148 // CHECK-LABEL: caller_2_2_2_2_2_2_2: 149 // CHECK: Indirect Call Site Count: 127 150 // CHECK-NEXT: Indirect Target Results: 151 // CHECK-NEXT: [ 1, callee_1_1_1, 1 ] 152 // CHECK-NEXT: [ 2, callee_1_1_2, 2 ] 153 // CHECK-NEXT: [ 2, callee_1_1_1, 1 ] 154 // CHECK-NEXT: [ 3, callee_1_2_1, 3 ] 155 // CHECK-NEXT: [ 3, callee_1_1_2, 2 ] 156 // CHECK-NEXT: [ 3, callee_1_1_1, 1 ] 157 // CHECK-NEXT: [ 4, callee_1_2_2, 4 ] 158 // CHECK-NEXT: [ 4, callee_1_2_1, 3 ] 159 // CHECK-NEXT: [ 4, callee_1_1_2, 2 ] 160 // CHECK-NEXT: [ 4, callee_1_1_1, 1 ] 161 // CHECK-NEXT: [ 5, callee_2_1_1, 5 ] 162 // CHECK-NEXT: [ 5, callee_1_2_2, 4 ] 163 // CHECK-NEXT: [ 5, callee_1_2_1, 3 ] 164 // CHECK-NEXT: [ 5, callee_1_1_2, 2 ] 165 // CHECK-NEXT: [ 5, callee_1_1_1, 1 ] 166 // CHECK-NEXT: [ 6, callee_2_1_2, 6 ] 167 // CHECK-NEXT: [ 6, callee_2_1_1, 5 ] 168 // CHECK-NEXT: [ 6, callee_1_2_2, 4 ] 169 // CHECK-NEXT: [ 6, callee_1_2_1, 3 ] 170 // CHECK-NEXT: [ 6, callee_1_1_2, 2 ] 171 // CHECK-NEXT: [ 6, callee_1_1_1, 1 ] 172 // CHECK-NEXT: [ 7, callee_2_2_1, 7 ] 173 // CHECK-NEXT: [ 7, callee_2_1_2, 6 ] 174 // CHECK-NEXT: [ 7, callee_2_1_1, 5 ] 175 // CHECK-NEXT: [ 7, callee_1_2_2, 4 ] 176 // CHECK-NEXT: [ 7, callee_1_2_1, 3 ] 177 // CHECK-NEXT: [ 7, callee_1_1_2, 2 ] 178 // CHECK-NEXT: [ 7, callee_1_1_1, 1 ] 179 // CHECK-NEXT: [ 9, callee_1_1_1, 1 ] 180 // CHECK-NEXT: [ 10, callee_1_1_2, 2 ] 181 // CHECK-NEXT: [ 10, callee_1_1_1, 1 ] 182 // CHECK-NEXT: [ 11, callee_1_2_1, 3 ] 183 // CHECK-NEXT: [ 11, callee_1_1_2, 2 ] 184 // CHECK-NEXT: [ 11, callee_1_1_1, 1 ] 185 // CHECK-NEXT: [ 12, callee_1_2_2, 4 ] 186 // CHECK-NEXT: [ 12, callee_1_2_1, 3 ] 187 // CHECK-NEXT: [ 12, callee_1_1_2, 2 ] 188 // CHECK-NEXT: [ 12, callee_1_1_1, 1 ] 189 // CHECK-NEXT: [ 13, callee_2_1_1, 5 ] 190 // CHECK-NEXT: [ 13, callee_1_2_2, 4 ] 191 // CHECK-NEXT: [ 13, callee_1_2_1, 3 ] 192 // CHECK-NEXT: [ 13, callee_1_1_2, 2 ] 193 // CHECK-NEXT: [ 13, callee_1_1_1, 1 ] 194 // CHECK-NEXT: [ 14, callee_2_1_2, 6 ] 195 // CHECK-NEXT: [ 14, callee_2_1_1, 5 ] 196 // CHECK-NEXT: [ 14, callee_1_2_2, 4 ] 197 // CHECK-NEXT: [ 14, callee_1_2_1, 3 ] 198 // CHECK-NEXT: [ 14, callee_1_1_2, 2 ] 199 // CHECK-NEXT: [ 14, callee_1_1_1, 1 ] 200 // CHECK-NEXT: [ 15, callee_2_2_1, 7 ] 201 // CHECK-NEXT: [ 15, callee_2_1_2, 6 ] 202 // CHECK-NEXT: [ 15, callee_2_1_1, 5 ] 203 // CHECK-NEXT: [ 15, callee_1_2_2, 4 ] 204 // CHECK-NEXT: [ 15, callee_1_2_1, 3 ] 205 // CHECK-NEXT: [ 15, callee_1_1_2, 2 ] 206 // CHECK-NEXT: [ 15, callee_1_1_1, 1 ] 207 // CHECK-NEXT: [ 17, callee_1_1_1, 1 ] 208 // CHECK-NEXT: [ 18, callee_1_1_2, 2 ] 209 // CHECK-NEXT: [ 18, callee_1_1_1, 1 ] 210 // CHECK-NEXT: [ 19, callee_1_2_1, 3 ] 211 // CHECK-NEXT: [ 19, callee_1_1_2, 2 ] 212 // CHECK-NEXT: [ 19, callee_1_1_1, 1 ] 213 // CHECK-NEXT: [ 20, callee_1_2_2, 4 ] 214 // CHECK-NEXT: [ 20, callee_1_2_1, 3 ] 215 // CHECK-NEXT: [ 20, callee_1_1_2, 2 ] 216 // CHECK-NEXT: [ 20, callee_1_1_1, 1 ] 217 // CHECK-NEXT: [ 21, callee_2_1_1, 5 ] 218 // CHECK-NEXT: [ 21, callee_1_2_2, 4 ] 219 // CHECK-NEXT: [ 21, callee_1_2_1, 3 ] 220 // CHECK-NEXT: [ 21, callee_1_1_2, 2 ] 221 // CHECK-NEXT: [ 21, callee_1_1_1, 1 ] 222 // CHECK-NEXT: [ 22, callee_2_1_2, 6 ] 223 // CHECK-NEXT: [ 22, callee_2_1_1, 5 ] 224 // CHECK-NEXT: [ 22, callee_1_2_2, 4 ] 225 // CHECK-NEXT: [ 22, callee_1_2_1, 3 ] 226 // CHECK-NEXT: [ 22, callee_1_1_2, 2 ] 227 // CHECK-NEXT: [ 22, callee_1_1_1, 1 ] 228 229