|
| 1 | +#include <benchmark/benchmark.h> |
| 2 | + |
| 3 | +#ifdef __clang__ |
| 4 | +#pragma clang diagnostic ignored "-Wreturn-type" |
| 5 | +#endif |
| 6 | + |
| 7 | +extern "C" { |
| 8 | + |
| 9 | +extern int ExternInt; |
| 10 | +extern int ExternInt2; |
| 11 | +extern int ExternInt3; |
| 12 | + |
| 13 | +inline int Add42(int x) { return x + 42; } |
| 14 | + |
| 15 | +struct NotTriviallyCopyable { |
| 16 | + NotTriviallyCopyable(); |
| 17 | + explicit NotTriviallyCopyable(int x) : value(x) {} |
| 18 | + NotTriviallyCopyable(NotTriviallyCopyable const&); |
| 19 | + int value; |
| 20 | +}; |
| 21 | + |
| 22 | +struct Large { |
| 23 | + int value; |
| 24 | + int data[2]; |
| 25 | +}; |
| 26 | + |
| 27 | +} |
| 28 | +// CHECK-LABEL: test_with_rvalue: |
| 29 | +extern "C" void test_with_rvalue() { |
| 30 | + benchmark::DoNotOptimize(Add42(0)); |
| 31 | + // CHECK: movl $42, %eax |
| 32 | + // CHECK: ret |
| 33 | +} |
| 34 | + |
| 35 | +// CHECK-LABEL: test_with_large_rvalue: |
| 36 | +extern "C" void test_with_large_rvalue() { |
| 37 | + benchmark::DoNotOptimize(Large{ExternInt, {ExternInt, ExternInt}}); |
| 38 | + // CHECK: movl ExternInt(%rip), %eax |
| 39 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 40 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 41 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 42 | + // CHECK: ret |
| 43 | +} |
| 44 | + |
| 45 | +// CHECK-LABEL: test_with_non_trivial_rvalue: |
| 46 | +extern "C" void test_with_non_trivial_rvalue() { |
| 47 | + benchmark::DoNotOptimize(NotTriviallyCopyable(ExternInt)); |
| 48 | + // CHECK: movl ExternInt(%rip), %eax |
| 49 | + // CHECK: ret |
| 50 | +} |
| 51 | + |
| 52 | +// CHECK-LABEL: test_with_lvalue: |
| 53 | +extern "C" void test_with_lvalue() { |
| 54 | + int x = 101; |
| 55 | + benchmark::DoNotOptimize(x); |
| 56 | + // CHECK-GNU: movl $101, %eax |
| 57 | + // CHECK-CLANG: movl $101, -{{[0-9]+}}(%rsp) |
| 58 | + // CHECK: ret |
| 59 | +} |
| 60 | + |
| 61 | +// CHECK-LABEL: test_with_large_lvalue: |
| 62 | +extern "C" void test_with_large_lvalue() { |
| 63 | + Large L{ExternInt, {ExternInt, ExternInt}}; |
| 64 | + benchmark::DoNotOptimize(L); |
| 65 | + // CHECK: movl ExternInt(%rip), %eax |
| 66 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 67 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 68 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 69 | + // CHECK: ret |
| 70 | +} |
| 71 | + |
| 72 | +// CHECK-LABEL: test_with_non_trivial_lvalue: |
| 73 | +extern "C" void test_with_non_trivial_lvalue() { |
| 74 | + NotTriviallyCopyable NTC(ExternInt); |
| 75 | + benchmark::DoNotOptimize(NTC); |
| 76 | + // CHECK: movl ExternInt(%rip), %eax |
| 77 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 78 | + // CHECK: ret |
| 79 | +} |
| 80 | + |
| 81 | + |
| 82 | +// CHECK-LABEL: test_with_const_lvalue: |
| 83 | +extern "C" void test_with_const_lvalue() { |
| 84 | + const int x = 123; |
| 85 | + benchmark::DoNotOptimize(x); |
| 86 | + // CHECK: movl $123, %eax |
| 87 | + // CHECK: ret |
| 88 | +} |
| 89 | + |
| 90 | +// CHECK-LABEL: test_with_large_const_lvalue: |
| 91 | +extern "C" void test_with_large_const_lvalue() { |
| 92 | + const Large L{ExternInt, {ExternInt, ExternInt}}; |
| 93 | + benchmark::DoNotOptimize(L); |
| 94 | + // CHECK: movl ExternInt(%rip), %eax |
| 95 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 96 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 97 | + // CHECK: movl %eax, -{{[0-9]+}}(%rsp) |
| 98 | + // CHECK: ret |
| 99 | +} |
| 100 | + |
| 101 | +// CHECK-LABEL: test_with_non_trivial_const_lvalue: |
| 102 | +extern "C" void test_with_non_trivial_const_lvalue() { |
| 103 | + const NotTriviallyCopyable Obj(ExternInt); |
| 104 | + benchmark::DoNotOptimize(Obj); |
| 105 | + // CHECK: movl ExternInt(%rip), %eax |
| 106 | + // CHECK: ret |
| 107 | +} |
| 108 | + |
| 109 | +// CHECK-LABEL: test_div_by_two: |
| 110 | +extern "C" int test_div_by_two(int input) { |
| 111 | + int divisor = 2; |
| 112 | + benchmark::DoNotOptimize(divisor); |
| 113 | + return input / divisor; |
| 114 | + // CHECK: movl $2, [[DEST:.*]] |
| 115 | + // CHECK: idivl [[DEST]] |
| 116 | + // CHECK: ret |
| 117 | +} |
| 118 | + |
0 commit comments