blob: 5bcfa4c98bc44e5102591f79c8e21880b4da46dd [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:
31 LICMTest() : pool_(), allocator_(&pool_) {
32 graph_ = CreateGraph(&allocator_);
33 }
34
35 ~LICMTest() { }
36
37 // Builds a singly-nested loop structure in CFG. Tests can further populate
38 // the basic blocks with instructions to set up interesting scenarios.
39 void BuildLoop() {
40 entry_ = new (&allocator_) HBasicBlock(graph_);
41 loop_preheader_ = new (&allocator_) HBasicBlock(graph_);
42 loop_header_ = new (&allocator_) HBasicBlock(graph_);
43 loop_body_ = new (&allocator_) HBasicBlock(graph_);
David Brazdildb51efb2015-11-06 01:36:20 +000044 return_ = new (&allocator_) HBasicBlock(graph_);
Aart Bik3f307f32015-07-21 18:30:18 -070045 exit_ = new (&allocator_) HBasicBlock(graph_);
46
47 graph_->AddBlock(entry_);
48 graph_->AddBlock(loop_preheader_);
49 graph_->AddBlock(loop_header_);
50 graph_->AddBlock(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000051 graph_->AddBlock(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070052 graph_->AddBlock(exit_);
53
54 graph_->SetEntryBlock(entry_);
55 graph_->SetExitBlock(exit_);
56
57 // Set up loop flow in CFG.
58 entry_->AddSuccessor(loop_preheader_);
59 loop_preheader_->AddSuccessor(loop_header_);
60 loop_header_->AddSuccessor(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000061 loop_header_->AddSuccessor(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070062 loop_body_->AddSuccessor(loop_header_);
David Brazdildb51efb2015-11-06 01:36:20 +000063 return_->AddSuccessor(exit_);
Aart Bik3f307f32015-07-21 18:30:18 -070064
65 // Provide boiler-plate instructions.
Andreas Gampea5b09a62016-11-17 15:21:22 -080066 parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(),
67 dex::TypeIndex(0),
68 0,
69 Primitive::kPrimNot);
Aart Bik3f307f32015-07-21 18:30:18 -070070 entry_->AddInstruction(parameter_);
David Brazdil15693bf2015-12-16 10:30:45 +000071 int_constant_ = graph_->GetIntConstant(42);
72 float_constant_ = graph_->GetFloatConstant(42.0f);
David Brazdil77a48ae2015-09-15 12:34:04 +000073 loop_preheader_->AddInstruction(new (&allocator_) HGoto());
Aart Bik3f307f32015-07-21 18:30:18 -070074 loop_header_->AddInstruction(new (&allocator_) HIf(parameter_));
75 loop_body_->AddInstruction(new (&allocator_) HGoto());
David Brazdil4833f5a2015-12-16 10:37:39 +000076 return_->AddInstruction(new (&allocator_) HReturnVoid());
Aart Bik3f307f32015-07-21 18:30:18 -070077 exit_->AddInstruction(new (&allocator_) HExit());
78 }
79
80 // Performs LICM optimizations (after proper set up).
81 void PerformLICM() {
David Brazdilbadd8262016-02-02 16:28:56 +000082 graph_->BuildDominatorTree();
Aart Bik3f307f32015-07-21 18:30:18 -070083 SideEffectsAnalysis side_effects(graph_);
84 side_effects.Run();
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +010085 LICM(graph_, side_effects, nullptr).Run();
Aart Bik3f307f32015-07-21 18:30:18 -070086 }
87
88 // General building fields.
89 ArenaPool pool_;
90 ArenaAllocator allocator_;
91 HGraph* graph_;
92
93 // Specific basic blocks.
94 HBasicBlock* entry_;
95 HBasicBlock* loop_preheader_;
96 HBasicBlock* loop_header_;
97 HBasicBlock* loop_body_;
David Brazdildb51efb2015-11-06 01:36:20 +000098 HBasicBlock* return_;
Aart Bik3f307f32015-07-21 18:30:18 -070099 HBasicBlock* exit_;
100
101 HInstruction* parameter_; // "this"
David Brazdil15693bf2015-12-16 10:30:45 +0000102 HInstruction* int_constant_;
103 HInstruction* float_constant_;
Aart Bik3f307f32015-07-21 18:30:18 -0700104};
105
106//
107// The actual LICM tests.
108//
109
Aart Bik3f307f32015-07-21 18:30:18 -0700110TEST_F(LICMTest, FieldHoisting) {
111 BuildLoop();
112
113 // Populate the loop with instructions: set/get field with different types.
Mingyao Yang8df69d42015-10-22 15:40:58 -0700114 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000115 nullptr,
Mingyao Yang8df69d42015-10-22 15:40:58 -0700116 Primitive::kPrimLong,
117 MemberOffset(10),
118 false,
119 kUnknownFieldIndex,
120 kUnknownClassDefIndex,
121 graph_->GetDexFile(),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700122 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700123 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
124 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000125 parameter_, int_constant_, nullptr, Primitive::kPrimInt, MemberOffset(20),
126 false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700127 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
128
Aart Bik8f08f9e2015-07-22 11:27:39 -0700129 EXPECT_EQ(get_field->GetBlock(), loop_body_);
130 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700131 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700132 EXPECT_EQ(get_field->GetBlock(), loop_preheader_);
133 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700134}
135
136TEST_F(LICMTest, NoFieldHoisting) {
137 BuildLoop();
138
139 // Populate the loop with instructions: set/get field with same types.
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800140 ScopedNullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700141 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000142 nullptr,
Mingyao Yang8df69d42015-10-22 15:40:58 -0700143 Primitive::kPrimLong,
144 MemberOffset(10),
145 false,
146 kUnknownFieldIndex,
147 kUnknownClassDefIndex,
148 graph_->GetDexFile(),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700149 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700150 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Mingyao Yang8df69d42015-10-22 15:40:58 -0700151 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(parameter_,
152 get_field,
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(set_field, loop_body_->GetLastInstruction());
162
Aart Bik8f08f9e2015-07-22 11:27:39 -0700163 EXPECT_EQ(get_field->GetBlock(), loop_body_);
164 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700165 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700166 EXPECT_EQ(get_field->GetBlock(), loop_body_);
167 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700168}
169
170TEST_F(LICMTest, ArrayHoisting) {
171 BuildLoop();
172
173 // Populate the loop with instructions: set/get array with different types.
174 HInstruction* get_array = new (&allocator_) HArrayGet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700175 parameter_, int_constant_, Primitive::kPrimInt, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700176 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
177 HInstruction* set_array = new (&allocator_) HArraySet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700178 parameter_, int_constant_, float_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700179 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
180
Aart Bik8f08f9e2015-07-22 11:27:39 -0700181 EXPECT_EQ(get_array->GetBlock(), loop_body_);
182 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700183 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700184 EXPECT_EQ(get_array->GetBlock(), loop_preheader_);
185 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700186}
187
188TEST_F(LICMTest, NoArrayHoisting) {
189 BuildLoop();
190
191 // Populate the loop with instructions: set/get array with same types.
192 HInstruction* get_array = new (&allocator_) HArrayGet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700193 parameter_, int_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700194 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
195 HInstruction* set_array = new (&allocator_) HArraySet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700196 parameter_, get_array, float_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700197 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
198
Aart Bik8f08f9e2015-07-22 11:27:39 -0700199 EXPECT_EQ(get_array->GetBlock(), loop_body_);
200 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700201 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700202 EXPECT_EQ(get_array->GetBlock(), loop_body_);
203 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700204}
205
206} // namespace art