You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

201 lines
5.2 KiB

2 months ago
  1. #include <benchmark/benchmark.h>
  2. #ifdef __clang__
  3. #pragma clang diagnostic ignored "-Wreturn-type"
  4. #endif
  5. BENCHMARK_DISABLE_DEPRECATED_WARNING
  6. extern "C" {
  7. extern int ExternInt;
  8. extern int ExternInt2;
  9. extern int ExternInt3;
  10. extern int BigArray[2049];
  11. const int ConstBigArray[2049]{};
  12. inline int Add42(int x) { return x + 42; }
  13. struct NotTriviallyCopyable {
  14. NotTriviallyCopyable();
  15. explicit NotTriviallyCopyable(int x) : value(x) {}
  16. NotTriviallyCopyable(NotTriviallyCopyable const &);
  17. int value;
  18. };
  19. struct Large {
  20. int value;
  21. int data[2];
  22. };
  23. struct ExtraLarge {
  24. int arr[2049];
  25. };
  26. }
  27. extern ExtraLarge ExtraLargeObj;
  28. const ExtraLarge ConstExtraLargeObj{};
  29. // CHECK-LABEL: test_with_rvalue:
  30. extern "C" void test_with_rvalue() {
  31. benchmark::DoNotOptimize(Add42(0));
  32. // CHECK: movl $42, %eax
  33. // CHECK: ret
  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: ExternInt(%rip)
  39. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG:[a-z]+]]
  40. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG]])
  41. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG]])
  42. // CHECK: ret
  43. }
  44. // CHECK-LABEL: test_with_non_trivial_rvalue:
  45. extern "C" void test_with_non_trivial_rvalue() {
  46. benchmark::DoNotOptimize(NotTriviallyCopyable(ExternInt));
  47. // CHECK: mov{{l|q}} ExternInt(%rip)
  48. // CHECK: ret
  49. }
  50. // CHECK-LABEL: test_with_lvalue:
  51. extern "C" void test_with_lvalue() {
  52. int x = 101;
  53. benchmark::DoNotOptimize(x);
  54. // CHECK-GNU: movl $101, %eax
  55. // CHECK-CLANG: movl $101, -{{[0-9]+}}(%[[REG:[a-z]+]])
  56. // CHECK: ret
  57. }
  58. // CHECK-LABEL: test_with_large_lvalue:
  59. extern "C" void test_with_large_lvalue() {
  60. Large L{ExternInt, {ExternInt, ExternInt}};
  61. benchmark::DoNotOptimize(L);
  62. // CHECK: ExternInt(%rip)
  63. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG:[a-z]+]])
  64. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG]])
  65. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG]])
  66. // CHECK: ret
  67. }
  68. // CHECK-LABEL: test_with_extra_large_lvalue_with_op:
  69. extern "C" void test_with_extra_large_lvalue_with_op() {
  70. ExtraLargeObj.arr[16] = 42;
  71. benchmark::DoNotOptimize(ExtraLargeObj);
  72. // CHECK: movl $42, ExtraLargeObj+64(%rip)
  73. // CHECK: ret
  74. }
  75. // CHECK-LABEL: test_with_big_array_with_op
  76. extern "C" void test_with_big_array_with_op() {
  77. BigArray[16] = 42;
  78. benchmark::DoNotOptimize(BigArray);
  79. // CHECK: movl $42, BigArray+64(%rip)
  80. // CHECK: ret
  81. }
  82. // CHECK-LABEL: test_with_non_trivial_lvalue:
  83. extern "C" void test_with_non_trivial_lvalue() {
  84. NotTriviallyCopyable NTC(ExternInt);
  85. benchmark::DoNotOptimize(NTC);
  86. // CHECK: ExternInt(%rip)
  87. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG:[a-z]+]])
  88. // CHECK: ret
  89. }
  90. // CHECK-LABEL: test_with_const_lvalue:
  91. extern "C" void test_with_const_lvalue() {
  92. const int x = 123;
  93. benchmark::DoNotOptimize(x);
  94. // CHECK: movl $123, %eax
  95. // CHECK: ret
  96. }
  97. // CHECK-LABEL: test_with_large_const_lvalue:
  98. extern "C" void test_with_large_const_lvalue() {
  99. const Large L{ExternInt, {ExternInt, ExternInt}};
  100. benchmark::DoNotOptimize(L);
  101. // CHECK: ExternInt(%rip)
  102. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG:[a-z]+]])
  103. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG]])
  104. // CHECK: movl %eax, -{{[0-9]+}}(%[[REG]])
  105. // CHECK: ret
  106. }
  107. // CHECK-LABEL: test_with_const_extra_large_obj:
  108. extern "C" void test_with_const_extra_large_obj() {
  109. benchmark::DoNotOptimize(ConstExtraLargeObj);
  110. // CHECK: ret
  111. }
  112. // CHECK-LABEL: test_with_const_big_array
  113. extern "C" void test_with_const_big_array() {
  114. benchmark::DoNotOptimize(ConstBigArray);
  115. // CHECK: ret
  116. }
  117. // CHECK-LABEL: test_with_non_trivial_const_lvalue:
  118. extern "C" void test_with_non_trivial_const_lvalue() {
  119. const NotTriviallyCopyable Obj(ExternInt);
  120. benchmark::DoNotOptimize(Obj);
  121. // CHECK: mov{{q|l}} ExternInt(%rip)
  122. // CHECK: ret
  123. }
  124. // CHECK-LABEL: test_div_by_two:
  125. extern "C" int test_div_by_two(int input) {
  126. int divisor = 2;
  127. benchmark::DoNotOptimize(divisor);
  128. return input / divisor;
  129. // CHECK: movl $2, [[DEST:.*]]
  130. // CHECK: idivl [[DEST]]
  131. // CHECK: ret
  132. }
  133. // CHECK-LABEL: test_inc_integer:
  134. extern "C" int test_inc_integer() {
  135. int x = 0;
  136. for (int i = 0; i < 5; ++i) benchmark::DoNotOptimize(++x);
  137. // CHECK: movl $1, [[DEST:.*]]
  138. // CHECK: {{(addl \$1,|incl)}} [[DEST]]
  139. // CHECK: {{(addl \$1,|incl)}} [[DEST]]
  140. // CHECK: {{(addl \$1,|incl)}} [[DEST]]
  141. // CHECK: {{(addl \$1,|incl)}} [[DEST]]
  142. // CHECK-CLANG: movl [[DEST]], %eax
  143. // CHECK: ret
  144. return x;
  145. }
  146. // CHECK-LABEL: test_pointer_rvalue
  147. extern "C" void test_pointer_rvalue() {
  148. // CHECK: movl $42, [[DEST:.*]]
  149. // CHECK: leaq [[DEST]], %rax
  150. // CHECK-CLANG: movq %rax, -{{[0-9]+}}(%[[REG:[a-z]+]])
  151. // CHECK: ret
  152. int x = 42;
  153. benchmark::DoNotOptimize(&x);
  154. }
  155. // CHECK-LABEL: test_pointer_const_lvalue:
  156. extern "C" void test_pointer_const_lvalue() {
  157. // CHECK: movl $42, [[DEST:.*]]
  158. // CHECK: leaq [[DEST]], %rax
  159. // CHECK-CLANG: movq %rax, -{{[0-9]+}}(%[[REG:[a-z]+]])
  160. // CHECK: ret
  161. int x = 42;
  162. int *const xp = &x;
  163. benchmark::DoNotOptimize(xp);
  164. }
  165. // CHECK-LABEL: test_pointer_lvalue:
  166. extern "C" void test_pointer_lvalue() {
  167. // CHECK: movl $42, [[DEST:.*]]
  168. // CHECK: leaq [[DEST]], %rax
  169. // CHECK-CLANG: movq %rax, -{{[0-9]+}}(%[[REG:[a-z+]+]])
  170. // CHECK: ret
  171. int x = 42;
  172. int *xp = &x;
  173. benchmark::DoNotOptimize(xp);
  174. }