blob: 8d15f78cceb999b478be7ec0033a19f51844a673 [file] [log] [blame]
Aart Bik3f307f32015-07-21 18:30:18 -07001/*
2 * Copyright (C) 2015 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
17#include "base/arena_allocator.h"
18#include "builder.h"
Aart Bik3f307f32015-07-21 18:30:18 -070019#include "licm.h"
20#include "nodes.h"
21#include "optimizing_unit_test.h"
22#include "side_effects_analysis.h"
23
24namespace art {
25
26/**
27 * Fixture class for the LICM tests.
28 */
David Brazdil4833f5a2015-12-16 10:37:39 +000029class LICMTest : public CommonCompilerTest {
Aart Bik3f307f32015-07-21 18:30:18 -070030 public:
Andreas Gamped9911ee2017-03-27 13:27:24 -070031 LICMTest()
32 : pool_(),
33 allocator_(&pool_),
34 entry_(nullptr),
35 loop_preheader_(nullptr),
36 loop_header_(nullptr),
37 loop_body_(nullptr),
38 return_(nullptr),
39 exit_(nullptr),
40 parameter_(nullptr),
41 int_constant_(nullptr),
42 float_constant_(nullptr) {
Aart Bik3f307f32015-07-21 18:30:18 -070043 graph_ = CreateGraph(&allocator_);
44 }
45
46 ~LICMTest() { }
47
48 // Builds a singly-nested loop structure in CFG. Tests can further populate
49 // the basic blocks with instructions to set up interesting scenarios.
50 void BuildLoop() {
51 entry_ = new (&allocator_) HBasicBlock(graph_);
52 loop_preheader_ = new (&allocator_) HBasicBlock(graph_);
53 loop_header_ = new (&allocator_) HBasicBlock(graph_);
54 loop_body_ = new (&allocator_) HBasicBlock(graph_);
David Brazdildb51efb2015-11-06 01:36:20 +000055 return_ = new (&allocator_) HBasicBlock(graph_);
Aart Bik3f307f32015-07-21 18:30:18 -070056 exit_ = new (&allocator_) HBasicBlock(graph_);
57
58 graph_->AddBlock(entry_);
59 graph_->AddBlock(loop_preheader_);
60 graph_->AddBlock(loop_header_);
61 graph_->AddBlock(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000062 graph_->AddBlock(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070063 graph_->AddBlock(exit_);
64
65 graph_->SetEntryBlock(entry_);
66 graph_->SetExitBlock(exit_);
67
68 // Set up loop flow in CFG.
69 entry_->AddSuccessor(loop_preheader_);
70 loop_preheader_->AddSuccessor(loop_header_);
71 loop_header_->AddSuccessor(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000072 loop_header_->AddSuccessor(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070073 loop_body_->AddSuccessor(loop_header_);
David Brazdildb51efb2015-11-06 01:36:20 +000074 return_->AddSuccessor(exit_);
Aart Bik3f307f32015-07-21 18:30:18 -070075
76 // Provide boiler-plate instructions.
Andreas Gampea5b09a62016-11-17 15:21:22 -080077 parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(),
78 dex::TypeIndex(0),
79 0,
80 Primitive::kPrimNot);
Aart Bik3f307f32015-07-21 18:30:18 -070081 entry_->AddInstruction(parameter_);
David Brazdil15693bf2015-12-16 10:30:45 +000082 int_constant_ = graph_->GetIntConstant(42);
83 float_constant_ = graph_->GetFloatConstant(42.0f);
David Brazdil77a48ae2015-09-15 12:34:04 +000084 loop_preheader_->AddInstruction(new (&allocator_) HGoto());
Aart Bik3f307f32015-07-21 18:30:18 -070085 loop_header_->AddInstruction(new (&allocator_) HIf(parameter_));
86 loop_body_->AddInstruction(new (&allocator_) HGoto());
David Brazdil4833f5a2015-12-16 10:37:39 +000087 return_->AddInstruction(new (&allocator_) HReturnVoid());
Aart Bik3f307f32015-07-21 18:30:18 -070088 exit_->AddInstruction(new (&allocator_) HExit());
89 }
90
91 // Performs LICM optimizations (after proper set up).
92 void PerformLICM() {
David Brazdilbadd8262016-02-02 16:28:56 +000093 graph_->BuildDominatorTree();
Aart Bik3f307f32015-07-21 18:30:18 -070094 SideEffectsAnalysis side_effects(graph_);
95 side_effects.Run();
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +010096 LICM(graph_, side_effects, nullptr).Run();
Aart Bik3f307f32015-07-21 18:30:18 -070097 }
98
99 // General building fields.
100 ArenaPool pool_;
101 ArenaAllocator allocator_;
102 HGraph* graph_;
103
104 // Specific basic blocks.
105 HBasicBlock* entry_;
106 HBasicBlock* loop_preheader_;
107 HBasicBlock* loop_header_;
108 HBasicBlock* loop_body_;
David Brazdildb51efb2015-11-06 01:36:20 +0000109 HBasicBlock* return_;
Aart Bik3f307f32015-07-21 18:30:18 -0700110 HBasicBlock* exit_;
111
112 HInstruction* parameter_; // "this"
David Brazdil15693bf2015-12-16 10:30:45 +0000113 HInstruction* int_constant_;
114 HInstruction* float_constant_;
Aart Bik3f307f32015-07-21 18:30:18 -0700115};
116
117//
118// The actual LICM tests.
119//
120
Aart Bik3f307f32015-07-21 18:30:18 -0700121TEST_F(LICMTest, FieldHoisting) {
122 BuildLoop();
123
124 // Populate the loop with instructions: set/get field with different types.
Mingyao Yang8df69d42015-10-22 15:40:58 -0700125 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000126 nullptr,
Mingyao Yang8df69d42015-10-22 15:40:58 -0700127 Primitive::kPrimLong,
128 MemberOffset(10),
129 false,
130 kUnknownFieldIndex,
131 kUnknownClassDefIndex,
132 graph_->GetDexFile(),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700133 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700134 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
135 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000136 parameter_, int_constant_, nullptr, Primitive::kPrimInt, MemberOffset(20),
137 false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700138 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
139
Aart Bik8f08f9e2015-07-22 11:27:39 -0700140 EXPECT_EQ(get_field->GetBlock(), loop_body_);
141 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700142 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700143 EXPECT_EQ(get_field->GetBlock(), loop_preheader_);
144 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700145}
146
147TEST_F(LICMTest, NoFieldHoisting) {
148 BuildLoop();
149
150 // Populate the loop with instructions: set/get field with same types.
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800151 ScopedNullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700152 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000153 nullptr,
Mingyao Yang8df69d42015-10-22 15:40:58 -0700154 Primitive::kPrimLong,
155 MemberOffset(10),
156 false,
157 kUnknownFieldIndex,
158 kUnknownClassDefIndex,
159 graph_->GetDexFile(),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700160 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700161 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Mingyao Yang8df69d42015-10-22 15:40:58 -0700162 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(parameter_,
163 get_field,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000164 nullptr,
Mingyao Yang8df69d42015-10-22 15:40:58 -0700165 Primitive::kPrimLong,
166 MemberOffset(10),
167 false,
168 kUnknownFieldIndex,
169 kUnknownClassDefIndex,
170 graph_->GetDexFile(),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700171 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700172 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
173
Aart Bik8f08f9e2015-07-22 11:27:39 -0700174 EXPECT_EQ(get_field->GetBlock(), loop_body_);
175 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700176 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700177 EXPECT_EQ(get_field->GetBlock(), loop_body_);
178 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700179}
180
181TEST_F(LICMTest, ArrayHoisting) {
182 BuildLoop();
183
184 // Populate the loop with instructions: set/get array with different types.
185 HInstruction* get_array = new (&allocator_) HArrayGet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700186 parameter_, int_constant_, Primitive::kPrimInt, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700187 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
188 HInstruction* set_array = new (&allocator_) HArraySet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700189 parameter_, int_constant_, float_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700190 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
191
Aart Bik8f08f9e2015-07-22 11:27:39 -0700192 EXPECT_EQ(get_array->GetBlock(), loop_body_);
193 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700194 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700195 EXPECT_EQ(get_array->GetBlock(), loop_preheader_);
196 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700197}
198
199TEST_F(LICMTest, NoArrayHoisting) {
200 BuildLoop();
201
202 // Populate the loop with instructions: set/get array with same types.
203 HInstruction* get_array = new (&allocator_) HArrayGet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700204 parameter_, int_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700205 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
206 HInstruction* set_array = new (&allocator_) HArraySet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700207 parameter_, get_array, float_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700208 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
209
Aart Bik8f08f9e2015-07-22 11:27:39 -0700210 EXPECT_EQ(get_array->GetBlock(), loop_body_);
211 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700212 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700213 EXPECT_EQ(get_array->GetBlock(), loop_body_);
214 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700215}
216
217} // namespace art