blob: a81d49aa0cd7db6745efdc96cc5b62038f05e34d [file] [log] [blame]
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +01001/*
2 * Copyright (C) 2014 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
Mathieu Chartierb666f482015-02-18 14:33:14 -080017#include "base/arena_allocator.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010018#include "builder.h"
19#include "gvn.h"
20#include "nodes.h"
21#include "optimizing_unit_test.h"
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000022#include "side_effects_analysis.h"
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010023
24#include "gtest/gtest.h"
25
26namespace art {
27
28TEST(GVNTest, LocalFieldElimination) {
29 ArenaPool pool;
30 ArenaAllocator allocator(&pool);
31
32 HGraph* graph = new (&allocator) HGraph(&allocator);
33 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
34 graph->AddBlock(entry);
35 graph->SetEntryBlock(entry);
36 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
37 entry->AddInstruction(parameter);
38
39 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
40 graph->AddBlock(block);
41 entry->AddSuccessor(block);
42
43 block->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +000044 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimNot,
45 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010046 block->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +000047 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimNot,
48 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010049 HInstruction* to_remove = block->GetLastInstruction();
50 block->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +000051 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimNot,
52 MemberOffset(43), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010053 HInstruction* different_offset = block->GetLastInstruction();
54 // Kill the value.
55 block->AddInstruction(new (&allocator) HInstanceFieldSet(
Calin Juravle52c48962014-12-16 17:02:57 +000056 parameter, parameter, Primitive::kPrimNot, MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010057 block->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +000058 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimNot,
59 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010060 HInstruction* use_after_kill = block->GetLastInstruction();
61 block->AddInstruction(new (&allocator) HExit());
62
63 ASSERT_EQ(to_remove->GetBlock(), block);
64 ASSERT_EQ(different_offset->GetBlock(), block);
65 ASSERT_EQ(use_after_kill->GetBlock(), block);
66
Nicolas Geoffraye53798a2014-12-01 10:31:54 +000067 graph->TryBuildingSsa();
Nicolas Geoffraye6f17152015-01-26 15:13:47 +000068 SideEffectsAnalysis side_effects(graph);
69 side_effects.Run();
Nicolas Geoffray827eedb2015-01-26 15:18:36 +000070 GVNOptimization(graph, side_effects).Run();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010071
72 ASSERT_TRUE(to_remove->GetBlock() == nullptr);
73 ASSERT_EQ(different_offset->GetBlock(), block);
74 ASSERT_EQ(use_after_kill->GetBlock(), block);
75}
76
77TEST(GVNTest, GlobalFieldElimination) {
78 ArenaPool pool;
79 ArenaAllocator allocator(&pool);
80
81 HGraph* graph = new (&allocator) HGraph(&allocator);
82 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
83 graph->AddBlock(entry);
84 graph->SetEntryBlock(entry);
85 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
86 entry->AddInstruction(parameter);
87
88 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
89 graph->AddBlock(block);
90 entry->AddSuccessor(block);
91 block->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +000092 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
93 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +010094
95 block->AddInstruction(new (&allocator) HIf(block->GetLastInstruction()));
96 HBasicBlock* then = new (&allocator) HBasicBlock(graph);
97 HBasicBlock* else_ = new (&allocator) HBasicBlock(graph);
98 HBasicBlock* join = new (&allocator) HBasicBlock(graph);
99 graph->AddBlock(then);
100 graph->AddBlock(else_);
101 graph->AddBlock(join);
102
103 block->AddSuccessor(then);
104 block->AddSuccessor(else_);
105 then->AddSuccessor(join);
106 else_->AddSuccessor(join);
107
108 then->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000109 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
110 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100111 then->AddInstruction(new (&allocator) HGoto());
112 else_->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000113 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
114 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100115 else_->AddInstruction(new (&allocator) HGoto());
116 join->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000117 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
118 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100119 join->AddInstruction(new (&allocator) HExit());
120
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000121 graph->TryBuildingSsa();
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000122 SideEffectsAnalysis side_effects(graph);
123 side_effects.Run();
Nicolas Geoffray827eedb2015-01-26 15:18:36 +0000124 GVNOptimization(graph, side_effects).Run();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100125
126 // Check that all field get instructions have been GVN'ed.
127 ASSERT_TRUE(then->GetFirstInstruction()->IsGoto());
128 ASSERT_TRUE(else_->GetFirstInstruction()->IsGoto());
129 ASSERT_TRUE(join->GetFirstInstruction()->IsExit());
130}
131
132TEST(GVNTest, LoopFieldElimination) {
133 ArenaPool pool;
134 ArenaAllocator allocator(&pool);
135
136 HGraph* graph = new (&allocator) HGraph(&allocator);
137 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
138 graph->AddBlock(entry);
139 graph->SetEntryBlock(entry);
140
141 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimNot);
142 entry->AddInstruction(parameter);
143
144 HBasicBlock* block = new (&allocator) HBasicBlock(graph);
145 graph->AddBlock(block);
146 entry->AddSuccessor(block);
147 block->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000148 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
149 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100150 block->AddInstruction(new (&allocator) HGoto());
151
152 HBasicBlock* loop_header = new (&allocator) HBasicBlock(graph);
153 HBasicBlock* loop_body = new (&allocator) HBasicBlock(graph);
154 HBasicBlock* exit = new (&allocator) HBasicBlock(graph);
155
156 graph->AddBlock(loop_header);
157 graph->AddBlock(loop_body);
158 graph->AddBlock(exit);
159 block->AddSuccessor(loop_header);
160 loop_header->AddSuccessor(loop_body);
161 loop_header->AddSuccessor(exit);
162 loop_body->AddSuccessor(loop_header);
163
164 loop_header->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000165 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
166 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100167 HInstruction* field_get_in_loop_header = loop_header->GetLastInstruction();
168 loop_header->AddInstruction(new (&allocator) HIf(block->GetLastInstruction()));
169
170 // Kill inside the loop body to prevent field gets inside the loop header
171 // and the body to be GVN'ed.
172 loop_body->AddInstruction(new (&allocator) HInstanceFieldSet(
Calin Juravle52c48962014-12-16 17:02:57 +0000173 parameter, parameter, Primitive::kPrimNot, MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100174 HInstruction* field_set = loop_body->GetLastInstruction();
175 loop_body->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000176 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
177 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100178 HInstruction* field_get_in_loop_body = loop_body->GetLastInstruction();
179 loop_body->AddInstruction(new (&allocator) HGoto());
180
181 exit->AddInstruction(
Calin Juravle52c48962014-12-16 17:02:57 +0000182 new (&allocator) HInstanceFieldGet(parameter, Primitive::kPrimBoolean,
183 MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100184 HInstruction* field_get_in_exit = exit->GetLastInstruction();
185 exit->AddInstruction(new (&allocator) HExit());
186
187 ASSERT_EQ(field_get_in_loop_header->GetBlock(), loop_header);
188 ASSERT_EQ(field_get_in_loop_body->GetBlock(), loop_body);
189 ASSERT_EQ(field_get_in_exit->GetBlock(), exit);
190
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000191 graph->TryBuildingSsa();
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000192 {
193 SideEffectsAnalysis side_effects(graph);
194 side_effects.Run();
Nicolas Geoffray827eedb2015-01-26 15:18:36 +0000195 GVNOptimization(graph, side_effects).Run();
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000196 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100197
198 // Check that all field get instructions are still there.
199 ASSERT_EQ(field_get_in_loop_header->GetBlock(), loop_header);
200 ASSERT_EQ(field_get_in_loop_body->GetBlock(), loop_body);
201 // The exit block is dominated by the loop header, whose field get
202 // does not get killed by the loop flags.
203 ASSERT_TRUE(field_get_in_exit->GetBlock() == nullptr);
204
205 // Now remove the field set, and check that all field get instructions have been GVN'ed.
206 loop_body->RemoveInstruction(field_set);
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000207 {
208 SideEffectsAnalysis side_effects(graph);
209 side_effects.Run();
Nicolas Geoffray827eedb2015-01-26 15:18:36 +0000210 GVNOptimization(graph, side_effects).Run();
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000211 }
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100212
213 ASSERT_TRUE(field_get_in_loop_header->GetBlock() == nullptr);
214 ASSERT_TRUE(field_get_in_loop_body->GetBlock() == nullptr);
215 ASSERT_TRUE(field_get_in_exit->GetBlock() == nullptr);
216}
217
218// Test that inner loops affect the side effects of the outer loop.
219TEST(GVNTest, LoopSideEffects) {
220 ArenaPool pool;
221 ArenaAllocator allocator(&pool);
222
223 HGraph* graph = new (&allocator) HGraph(&allocator);
224 HBasicBlock* entry = new (&allocator) HBasicBlock(graph);
225 graph->AddBlock(entry);
226 graph->SetEntryBlock(entry);
227
228 HBasicBlock* outer_loop_header = new (&allocator) HBasicBlock(graph);
229 HBasicBlock* outer_loop_body = new (&allocator) HBasicBlock(graph);
230 HBasicBlock* outer_loop_exit = new (&allocator) HBasicBlock(graph);
231 HBasicBlock* inner_loop_header = new (&allocator) HBasicBlock(graph);
232 HBasicBlock* inner_loop_body = new (&allocator) HBasicBlock(graph);
233 HBasicBlock* inner_loop_exit = new (&allocator) HBasicBlock(graph);
234
235 graph->AddBlock(outer_loop_header);
236 graph->AddBlock(outer_loop_body);
237 graph->AddBlock(outer_loop_exit);
238 graph->AddBlock(inner_loop_header);
239 graph->AddBlock(inner_loop_body);
240 graph->AddBlock(inner_loop_exit);
241
242 entry->AddSuccessor(outer_loop_header);
243 outer_loop_header->AddSuccessor(outer_loop_body);
244 outer_loop_header->AddSuccessor(outer_loop_exit);
245 outer_loop_body->AddSuccessor(inner_loop_header);
246 inner_loop_header->AddSuccessor(inner_loop_body);
247 inner_loop_header->AddSuccessor(inner_loop_exit);
248 inner_loop_body->AddSuccessor(inner_loop_header);
249 inner_loop_exit->AddSuccessor(outer_loop_header);
250
251 HInstruction* parameter = new (&allocator) HParameterValue(0, Primitive::kPrimBoolean);
252 entry->AddInstruction(parameter);
253 entry->AddInstruction(new (&allocator) HGoto());
254 outer_loop_header->AddInstruction(new (&allocator) HIf(parameter));
255 outer_loop_body->AddInstruction(new (&allocator) HGoto());
256 inner_loop_header->AddInstruction(new (&allocator) HIf(parameter));
257 inner_loop_body->AddInstruction(new (&allocator) HGoto());
258 inner_loop_exit->AddInstruction(new (&allocator) HGoto());
259 outer_loop_exit->AddInstruction(new (&allocator) HExit());
260
Nicolas Geoffraye53798a2014-12-01 10:31:54 +0000261 graph->TryBuildingSsa();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100262
263 ASSERT_TRUE(inner_loop_header->GetLoopInformation()->IsIn(
264 *outer_loop_header->GetLoopInformation()));
265
266 // Check that the loops don't have side effects.
267 {
268 // Make one block with a side effect.
269 entry->AddInstruction(new (&allocator) HInstanceFieldSet(
Calin Juravle52c48962014-12-16 17:02:57 +0000270 parameter, parameter, Primitive::kPrimNot, MemberOffset(42), false));
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100271
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000272 SideEffectsAnalysis side_effects(graph);
273 side_effects.Run();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100274
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000275 ASSERT_TRUE(side_effects.GetBlockEffects(entry).HasSideEffects());
276 ASSERT_FALSE(side_effects.GetLoopEffects(outer_loop_header).HasSideEffects());
277 ASSERT_FALSE(side_effects.GetLoopEffects(inner_loop_header).HasSideEffects());
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100278 }
279
280 // Check that the side effects of the outer loop does not affect the inner loop.
281 {
282 outer_loop_body->InsertInstructionBefore(
283 new (&allocator) HInstanceFieldSet(
Calin Juravle52c48962014-12-16 17:02:57 +0000284 parameter, parameter, Primitive::kPrimNot, MemberOffset(42), false),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100285 outer_loop_body->GetLastInstruction());
286
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000287 SideEffectsAnalysis side_effects(graph);
288 side_effects.Run();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100289
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000290 ASSERT_TRUE(side_effects.GetBlockEffects(entry).HasSideEffects());
291 ASSERT_TRUE(side_effects.GetBlockEffects(outer_loop_body).HasSideEffects());
292 ASSERT_TRUE(side_effects.GetLoopEffects(outer_loop_header).HasSideEffects());
293 ASSERT_FALSE(side_effects.GetLoopEffects(inner_loop_header).HasSideEffects());
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100294 }
295
296 // Check that the side effects of the inner loop affects the outer loop.
297 {
298 outer_loop_body->RemoveInstruction(outer_loop_body->GetFirstInstruction());
299 inner_loop_body->InsertInstructionBefore(
300 new (&allocator) HInstanceFieldSet(
Calin Juravle52c48962014-12-16 17:02:57 +0000301 parameter, parameter, Primitive::kPrimNot, MemberOffset(42), false),
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100302 inner_loop_body->GetLastInstruction());
303
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000304 SideEffectsAnalysis side_effects(graph);
305 side_effects.Run();
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100306
Nicolas Geoffraye6f17152015-01-26 15:13:47 +0000307 ASSERT_TRUE(side_effects.GetBlockEffects(entry).HasSideEffects());
308 ASSERT_FALSE(side_effects.GetBlockEffects(outer_loop_body).HasSideEffects());
309 ASSERT_TRUE(side_effects.GetLoopEffects(outer_loop_header).HasSideEffects());
310 ASSERT_TRUE(side_effects.GetLoopEffects(inner_loop_header).HasSideEffects());
Nicolas Geoffrayd31cf3d2014-09-08 17:30:24 +0100311 }
312}
313} // namespace art