1// A contrived example to test the double jump removal peephole.
2
3// RUN: %clang %cflags -O0 %s -o %t.exe
4// RUN: llvm-bolt %t.exe -o %t.bolt --peepholes=double-jumps | \
5// RUN: FileCheck %s -check-prefix=CHECKBOLT
6// RUN: llvm-objdump --no-print-imm-hex -d %t.bolt | FileCheck %s
7
8// CHECKBOLT: BOLT-INFO: Peephole: 1 double jumps patched.
9
10// CHECK: <_Z3foom>:
11// CHECK-NEXT: sub sp, sp, #16
12// CHECK-NEXT: str x0, [sp, #8]
13// CHECK-NEXT: ldr [[REG:x[0-28]+]], [sp, #8]
14// CHECK-NEXT: cmp [[REG]], #0
15// CHECK-NEXT: b.eq {{.*}} <_Z3foom+0x34>
16// CHECK-NEXT: add [[REG]], [[REG]], #1
17// CHECK-NEXT: add [[REG]], [[REG]], #1
18// CHECK-NEXT: cmp [[REG]], #2
19// CHECK-NEXT: b.eq {{.*}} <_Z3foom+0x28>
20// CHECK-NEXT: add [[REG]], [[REG]], #1
21// CHECK-NEXT: mov [[REG]], x1
22// CHECK-NEXT: ldr x1, [sp]
23// CHECK-NEXT: b {{.*}} <bar>
24// CHECK-NEXT: ldr x1, [sp]
25// CHECK-NEXT: add [[REG]], [[REG]], #1
26// CHECK-NEXT: b {{.*}} <bar>
27
28extern "C" unsigned long bar(unsigned long count) { return count + 1; }
29
30unsigned long foo(unsigned long count) {
31 asm volatile(" cmp %0,#0\n"
32 " b.eq .L7\n"
33 " add %0, %0, #1\n"
34 " b .L1\n"
35 ".L1: b .L2\n"
36 ".L2: add %0, %0, #1\n"
37 " cmp %0, #2\n"
38 " b.ne .L3\n"
39 " b .L4\n"
40 ".L3: b .L5\n"
41 ".L5: add %0, %0, #1\n"
42 ".L4: mov %0,x1\n"
43 " ldr x1, [sp]\n"
44 " b .L6\n"
45 ".L7: ldr x1, [sp]\n"
46 " add %0, %0, #1\n"
47 " b .L6\n"
48 ".L6: b bar\n"
49 :
50 : "r"(count)
51 :);
52 return count;
53}
54
55extern "C" int _start() { return foo(count: 38); }
56

source code of bolt/test/AArch64/double_jump.cpp