blob: adc3cabe870a83d531934b40a0820350585a4910 [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
Andreas Gampe8cf9cb32017-07-19 09:28:38 -070017#include "licm.h"
18
Aart Bik3f307f32015-07-21 18:30:18 -070019#include "base/arena_allocator.h"
20#include "builder.h"
Aart Bik3f307f32015-07-21 18:30:18 -070021#include "nodes.h"
22#include "optimizing_unit_test.h"
23#include "side_effects_analysis.h"
24
25namespace art {
26
27/**
28 * Fixture class for the LICM tests.
29 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010030class LICMTest : public OptimizingUnitTest {
Aart Bik3f307f32015-07-21 18:30:18 -070031 public:
Andreas Gamped9911ee2017-03-27 13:27:24 -070032 LICMTest()
Vladimir Markoca6fff82017-10-03 14:49:14 +010033 : entry_(nullptr),
Andreas Gamped9911ee2017-03-27 13:27:24 -070034 loop_preheader_(nullptr),
35 loop_header_(nullptr),
36 loop_body_(nullptr),
37 return_(nullptr),
38 exit_(nullptr),
39 parameter_(nullptr),
40 int_constant_(nullptr),
41 float_constant_(nullptr) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010042 graph_ = CreateGraph();
Aart Bik3f307f32015-07-21 18:30:18 -070043 }
44
45 ~LICMTest() { }
46
47 // Builds a singly-nested loop structure in CFG. Tests can further populate
48 // the basic blocks with instructions to set up interesting scenarios.
49 void BuildLoop() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010050 entry_ = new (GetAllocator()) HBasicBlock(graph_);
51 loop_preheader_ = new (GetAllocator()) HBasicBlock(graph_);
52 loop_header_ = new (GetAllocator()) HBasicBlock(graph_);
53 loop_body_ = new (GetAllocator()) HBasicBlock(graph_);
54 return_ = new (GetAllocator()) HBasicBlock(graph_);
55 exit_ = new (GetAllocator()) HBasicBlock(graph_);
Aart Bik3f307f32015-07-21 18:30:18 -070056
57 graph_->AddBlock(entry_);
58 graph_->AddBlock(loop_preheader_);
59 graph_->AddBlock(loop_header_);
60 graph_->AddBlock(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000061 graph_->AddBlock(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070062 graph_->AddBlock(exit_);
63
64 graph_->SetEntryBlock(entry_);
65 graph_->SetExitBlock(exit_);
66
67 // Set up loop flow in CFG.
68 entry_->AddSuccessor(loop_preheader_);
69 loop_preheader_->AddSuccessor(loop_header_);
70 loop_header_->AddSuccessor(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000071 loop_header_->AddSuccessor(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070072 loop_body_->AddSuccessor(loop_header_);
David Brazdildb51efb2015-11-06 01:36:20 +000073 return_->AddSuccessor(exit_);
Aart Bik3f307f32015-07-21 18:30:18 -070074
75 // Provide boiler-plate instructions.
Vladimir Markoca6fff82017-10-03 14:49:14 +010076 parameter_ = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
77 dex::TypeIndex(0),
78 0,
79 DataType::Type::kReference);
Aart Bik3f307f32015-07-21 18:30:18 -070080 entry_->AddInstruction(parameter_);
David Brazdil15693bf2015-12-16 10:30:45 +000081 int_constant_ = graph_->GetIntConstant(42);
82 float_constant_ = graph_->GetFloatConstant(42.0f);
Vladimir Markoca6fff82017-10-03 14:49:14 +010083 loop_preheader_->AddInstruction(new (GetAllocator()) HGoto());
84 loop_header_->AddInstruction(new (GetAllocator()) HIf(parameter_));
85 loop_body_->AddInstruction(new (GetAllocator()) HGoto());
86 return_->AddInstruction(new (GetAllocator()) HReturnVoid());
87 exit_->AddInstruction(new (GetAllocator()) HExit());
Aart Bik3f307f32015-07-21 18:30:18 -070088 }
89
90 // Performs LICM optimizations (after proper set up).
91 void PerformLICM() {
David Brazdilbadd8262016-02-02 16:28:56 +000092 graph_->BuildDominatorTree();
Aart Bik3f307f32015-07-21 18:30:18 -070093 SideEffectsAnalysis side_effects(graph_);
94 side_effects.Run();
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +010095 LICM(graph_, side_effects, nullptr).Run();
Aart Bik3f307f32015-07-21 18:30:18 -070096 }
97
98 // General building fields.
Aart Bik3f307f32015-07-21 18:30:18 -070099 HGraph* graph_;
100
101 // Specific basic blocks.
102 HBasicBlock* entry_;
103 HBasicBlock* loop_preheader_;
104 HBasicBlock* loop_header_;
105 HBasicBlock* loop_body_;
David Brazdildb51efb2015-11-06 01:36:20 +0000106 HBasicBlock* return_;
Aart Bik3f307f32015-07-21 18:30:18 -0700107 HBasicBlock* exit_;
108
109 HInstruction* parameter_; // "this"
David Brazdil15693bf2015-12-16 10:30:45 +0000110 HInstruction* int_constant_;
111 HInstruction* float_constant_;
Aart Bik3f307f32015-07-21 18:30:18 -0700112};
113
114//
115// The actual LICM tests.
116//
117
Aart Bik3f307f32015-07-21 18:30:18 -0700118TEST_F(LICMTest, FieldHoisting) {
119 BuildLoop();
120
121 // Populate the loop with instructions: set/get field with different types.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100122 HInstruction* get_field = new (GetAllocator()) HInstanceFieldGet(parameter_,
123 nullptr,
124 DataType::Type::kInt64,
125 MemberOffset(10),
126 false,
127 kUnknownFieldIndex,
128 kUnknownClassDefIndex,
129 graph_->GetDexFile(),
130 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700131 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100132 HInstruction* set_field = new (GetAllocator()) HInstanceFieldSet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100133 parameter_, int_constant_, nullptr, DataType::Type::kInt32, MemberOffset(20),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000134 false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700135 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
136
Aart Bik8f08f9e2015-07-22 11:27:39 -0700137 EXPECT_EQ(get_field->GetBlock(), loop_body_);
138 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700139 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700140 EXPECT_EQ(get_field->GetBlock(), loop_preheader_);
141 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700142}
143
144TEST_F(LICMTest, NoFieldHoisting) {
145 BuildLoop();
146
147 // Populate the loop with instructions: set/get field with same types.
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800148 ScopedNullHandle<mirror::DexCache> dex_cache;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100149 HInstruction* get_field = new (GetAllocator()) HInstanceFieldGet(parameter_,
150 nullptr,
151 DataType::Type::kInt64,
152 MemberOffset(10),
153 false,
154 kUnknownFieldIndex,
155 kUnknownClassDefIndex,
156 graph_->GetDexFile(),
157 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700158 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100159 HInstruction* set_field = new (GetAllocator()) HInstanceFieldSet(parameter_,
160 get_field,
161 nullptr,
162 DataType::Type::kInt64,
163 MemberOffset(10),
164 false,
165 kUnknownFieldIndex,
166 kUnknownClassDefIndex,
167 graph_->GetDexFile(),
168 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700169 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
170
Aart Bik8f08f9e2015-07-22 11:27:39 -0700171 EXPECT_EQ(get_field->GetBlock(), loop_body_);
172 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700173 PerformLICM();
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}
177
178TEST_F(LICMTest, ArrayHoisting) {
179 BuildLoop();
180
181 // Populate the loop with instructions: set/get array with different types.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100182 HInstruction* get_array = new (GetAllocator()) HArrayGet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100183 parameter_, int_constant_, DataType::Type::kInt32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700184 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100185 HInstruction* set_array = new (GetAllocator()) HArraySet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100186 parameter_, int_constant_, float_constant_, DataType::Type::kFloat32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700187 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
188
Aart Bik8f08f9e2015-07-22 11:27:39 -0700189 EXPECT_EQ(get_array->GetBlock(), loop_body_);
190 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700191 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700192 EXPECT_EQ(get_array->GetBlock(), loop_preheader_);
193 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700194}
195
196TEST_F(LICMTest, NoArrayHoisting) {
197 BuildLoop();
198
199 // Populate the loop with instructions: set/get array with same types.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100200 HInstruction* get_array = new (GetAllocator()) HArrayGet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100201 parameter_, int_constant_, DataType::Type::kFloat32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700202 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100203 HInstruction* set_array = new (GetAllocator()) HArraySet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100204 parameter_, get_array, float_constant_, DataType::Type::kFloat32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700205 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
206
Aart Bik8f08f9e2015-07-22 11:27:39 -0700207 EXPECT_EQ(get_array->GetBlock(), loop_body_);
208 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700209 PerformLICM();
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}
213
214} // namespace art