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