93 lines · c
1#include <stdio.h>2 3typedef int (*func_ptr)(int, int);4 5int add(int a, int b) { return a + b; }6 7int main() {8 func_ptr fun;9 fun = add;10 int sum = fun(10, 20); // indirect call to 'add'11 printf("The sum is: %d\n", sum);12 return 0;13}14/*15REQUIRES: system-linux,bolt-runtime16 17RUN: %clang %cflags %s -o %t.exe -Wl,-q -no-pie -fpie18RUN: llvm-objdump --disassemble-symbols=main %t.exe \19RUN: | FileCheck %s --check-prefix=CHECKINDIRECTREG20 21CHECKINDIRECTREG: mov w0, #0xa22CHECKINDIRECTREG-NEXT: mov w1, #0x1423CHECKINDIRECTREG-NEXT: blr x824 25RUN: llvm-bolt %t.exe --instrument --instrumentation-file=%t.fdata \26RUN: -o %t.instrumented \27RUN: | FileCheck %s --check-prefix=CHECK-INSTR-LOG28 29CHECK-INSTR-LOG: BOLT-INSTRUMENTER: Number of indirect call site descriptors: 130 31RUN: llvm-objdump --disassemble-symbols=main %t.instrumented \32RUN: | FileCheck %s --check-prefix=CHECK-INSTR-INDIRECTREG33 34RUN: llvm-objdump --disassemble-symbols=__bolt_instr_ind_call_handler \35RUN: %t.instrumented | FileCheck %s --check-prefix=CHECK-INSTR-INDIR-CALL36RUN: llvm-objdump --disassemble-symbols=__bolt_instr_ind_call_handler_func \37RUN: %t.instrumented | FileCheck %s --check-prefix=CHECK-INSTR-INDIR-CALL-FUNC38 39CHECK-INSTR-INDIRECTREG: mov w0, #0xa40CHECK-INSTR-INDIRECTREG-NEXT: mov w1, #0x1441// store current values42CHECK-INSTR-INDIRECTREG-NEXT: stp x0, x1, {{.*}}43// store the indirect target address in x044CHECK-INSTR-INDIRECTREG-NEXT: mov x0, x845// load callsite id into x146CHECK-INSTR-INDIRECTREG-NEXT: movk x1, {{.*}}47CHECK-INSTR-INDIRECTREG-NEXT: movk x1, {{.*}}48CHECK-INSTR-INDIRECTREG-NEXT: movk x1, {{.*}}49CHECK-INSTR-INDIRECTREG-NEXT: movk x1, {{.*}}50CHECK-INSTR-INDIRECTREG-NEXT: stp x0, x30, {{.*}}51CHECK-INSTR-INDIRECTREG-NEXT: adrp x8, {{.*}}52CHECK-INSTR-INDIRECTREG-NEXT: add x8, {{.*}}53// call instrumentation library handler function54CHECK-INSTR-INDIRECTREG-NEXT: blr x855// restore registers saved before56CHECK-INSTR-INDIRECTREG-NEXT: ldp x0, x30, {{.*}}57CHECK-INSTR-INDIRECTREG-NEXT: mov x8, x058CHECK-INSTR-INDIRECTREG-NEXT: ldp x0, x1, {{.*}}59// original indirect call instruction60CHECK-INSTR-INDIRECTREG-NEXT: blr x861 62 63CHECK-INSTR-INDIR-CALL: __bolt_instr_ind_call_handler>:64CHECK-INSTR-INDIR-CALL-NEXT: ret65 66CHECK-INSTR-INDIR-CALL-FUNC: __bolt_instr_ind_call_handler_func>:67CHECK-INSTR-INDIR-CALL-FUNC-NEXT: adrp x068CHECK-INSTR-INDIR-CALL-FUNC-NEXT: ldr x069CHECK-INSTR-INDIR-CALL-FUNC-NEXT: cmp x0, #0x070CHECK-INSTR-INDIR-CALL-FUNC-NEXT: b.eq{{.*}}__bolt_instr_ind_call_handler71CHECK-INSTR-INDIR-CALL-FUNC-NEXT: str x3072CHECK-INSTR-INDIR-CALL-FUNC-NEXT: blr x073CHECK-INSTR-INDIR-CALL-FUNC-NEXT: ldr x3074CHECK-INSTR-INDIR-CALL-FUNC-NEXT: b{{.*}}__bolt_instr_ind_call_handler75 76# Instrumented program needs to finish returning zero77RUN: %t.instrumented | FileCheck %s -check-prefix=CHECK-OUTPUT78 79# Test that the instrumented data makes sense80RUN: llvm-bolt %t.exe -o %t.bolted --data %t.fdata \81RUN: --reorder-blocks=ext-tsp --reorder-functions=hfsort+ \82RUN: --print-only=main --print-finalized | FileCheck %s83 84RUN: %t.bolted | FileCheck %s -check-prefix=CHECK-OUTPUT85 86CHECK-OUTPUT: The sum is: 3087 88# Check that our indirect call has 1 hit recorded in the fdata file and that89# this was processed correctly by BOLT90CHECK: blr x8 # CallProfile: 1 (0 misses) :91CHECK-NEXT: { add: 1 (0 misses) }92*/93