blob: f8481099f427a607736806e53499b524e8c4d3a4 [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"
Vladimír Marko434d9682022-11-04 14:04:17 +000020#include "base/macros.h"
Aart Bik3f307f32015-07-21 18:30:18 -070021#include "builder.h"
Aart Bik3f307f32015-07-21 18:30:18 -070022#include "nodes.h"
23#include "optimizing_unit_test.h"
24#include "side_effects_analysis.h"
25
Vladimír Marko434d9682022-11-04 14:04:17 +000026namespace art HIDDEN {
Aart Bik3f307f32015-07-21 18:30:18 -070027
28/**
29 * Fixture class for the LICM tests.
30 */
Vladimir Markoca6fff82017-10-03 14:49:14 +010031class LICMTest : public OptimizingUnitTest {
Aart Bik3f307f32015-07-21 18:30:18 -070032 public:
Andreas Gamped9911ee2017-03-27 13:27:24 -070033 LICMTest()
Vladimir Markoca6fff82017-10-03 14:49:14 +010034 : entry_(nullptr),
Andreas Gamped9911ee2017-03-27 13:27:24 -070035 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) {
Vladimir Markoca6fff82017-10-03 14:49:14 +010043 graph_ = CreateGraph();
Aart Bik3f307f32015-07-21 18:30:18 -070044 }
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() {
Vladimir Markoca6fff82017-10-03 14:49:14 +010051 entry_ = new (GetAllocator()) HBasicBlock(graph_);
52 loop_preheader_ = new (GetAllocator()) HBasicBlock(graph_);
53 loop_header_ = new (GetAllocator()) HBasicBlock(graph_);
54 loop_body_ = new (GetAllocator()) HBasicBlock(graph_);
55 return_ = new (GetAllocator()) HBasicBlock(graph_);
56 exit_ = new (GetAllocator()) HBasicBlock(graph_);
Aart Bik3f307f32015-07-21 18:30:18 -070057
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.
Vladimir Markoca6fff82017-10-03 14:49:14 +010077 parameter_ = new (GetAllocator()) HParameterValue(graph_->GetDexFile(),
78 dex::TypeIndex(0),
79 0,
80 DataType::Type::kReference);
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);
Vladimir Markoca6fff82017-10-03 14:49:14 +010084 loop_preheader_->AddInstruction(new (GetAllocator()) HGoto());
85 loop_header_->AddInstruction(new (GetAllocator()) HIf(parameter_));
86 loop_body_->AddInstruction(new (GetAllocator()) HGoto());
87 return_->AddInstruction(new (GetAllocator()) HReturnVoid());
88 exit_->AddInstruction(new (GetAllocator()) HExit());
Aart Bik3f307f32015-07-21 18:30:18 -070089 }
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.
Aart Bik3f307f32015-07-21 18:30:18 -0700100 HGraph* graph_;
101
102 // Specific basic blocks.
103 HBasicBlock* entry_;
104 HBasicBlock* loop_preheader_;
105 HBasicBlock* loop_header_;
106 HBasicBlock* loop_body_;
David Brazdildb51efb2015-11-06 01:36:20 +0000107 HBasicBlock* return_;
Aart Bik3f307f32015-07-21 18:30:18 -0700108 HBasicBlock* exit_;
109
110 HInstruction* parameter_; // "this"
David Brazdil15693bf2015-12-16 10:30:45 +0000111 HInstruction* int_constant_;
112 HInstruction* float_constant_;
Aart Bik3f307f32015-07-21 18:30:18 -0700113};
114
115//
116// The actual LICM tests.
117//
118
Aart Bik3f307f32015-07-21 18:30:18 -0700119TEST_F(LICMTest, FieldHoisting) {
120 BuildLoop();
121
122 // Populate the loop with instructions: set/get field with different types.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100123 HInstruction* get_field = new (GetAllocator()) HInstanceFieldGet(parameter_,
124 nullptr,
125 DataType::Type::kInt64,
126 MemberOffset(10),
127 false,
128 kUnknownFieldIndex,
129 kUnknownClassDefIndex,
130 graph_->GetDexFile(),
131 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700132 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100133 HInstruction* set_field = new (GetAllocator()) HInstanceFieldSet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100134 parameter_, int_constant_, nullptr, DataType::Type::kInt32, MemberOffset(20),
Nicolas Geoffrayc52b26d2016-12-19 09:18:07 +0000135 false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700136 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
137
Aart Bik8f08f9e2015-07-22 11:27:39 -0700138 EXPECT_EQ(get_field->GetBlock(), loop_body_);
139 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700140 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700141 EXPECT_EQ(get_field->GetBlock(), loop_preheader_);
142 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700143}
144
145TEST_F(LICMTest, NoFieldHoisting) {
146 BuildLoop();
147
148 // Populate the loop with instructions: set/get field with same types.
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800149 ScopedNullHandle<mirror::DexCache> dex_cache;
Vladimir Markoca6fff82017-10-03 14:49:14 +0100150 HInstruction* get_field = new (GetAllocator()) HInstanceFieldGet(parameter_,
151 nullptr,
152 DataType::Type::kInt64,
153 MemberOffset(10),
154 false,
155 kUnknownFieldIndex,
156 kUnknownClassDefIndex,
157 graph_->GetDexFile(),
158 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700159 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100160 HInstruction* set_field = new (GetAllocator()) HInstanceFieldSet(parameter_,
161 get_field,
162 nullptr,
163 DataType::Type::kInt64,
164 MemberOffset(10),
165 false,
166 kUnknownFieldIndex,
167 kUnknownClassDefIndex,
168 graph_->GetDexFile(),
169 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700170 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
171
Aart Bik8f08f9e2015-07-22 11:27:39 -0700172 EXPECT_EQ(get_field->GetBlock(), loop_body_);
173 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700174 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700175 EXPECT_EQ(get_field->GetBlock(), loop_body_);
176 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700177}
178
179TEST_F(LICMTest, ArrayHoisting) {
180 BuildLoop();
181
182 // Populate the loop with instructions: set/get array with different types.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100183 HInstruction* get_array = new (GetAllocator()) HArrayGet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100184 parameter_, int_constant_, DataType::Type::kInt32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700185 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100186 HInstruction* set_array = new (GetAllocator()) HArraySet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100187 parameter_, int_constant_, float_constant_, DataType::Type::kFloat32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700188 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
189
Aart Bik8f08f9e2015-07-22 11:27:39 -0700190 EXPECT_EQ(get_array->GetBlock(), loop_body_);
191 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700192 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700193 EXPECT_EQ(get_array->GetBlock(), loop_preheader_);
194 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700195}
196
197TEST_F(LICMTest, NoArrayHoisting) {
198 BuildLoop();
199
200 // Populate the loop with instructions: set/get array with same types.
Vladimir Markoca6fff82017-10-03 14:49:14 +0100201 HInstruction* get_array = new (GetAllocator()) HArrayGet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100202 parameter_, int_constant_, DataType::Type::kFloat32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700203 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
Vladimir Markoca6fff82017-10-03 14:49:14 +0100204 HInstruction* set_array = new (GetAllocator()) HArraySet(
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100205 parameter_, get_array, float_constant_, DataType::Type::kFloat32, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700206 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
207
Aart Bik8f08f9e2015-07-22 11:27:39 -0700208 EXPECT_EQ(get_array->GetBlock(), loop_body_);
209 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700210 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700211 EXPECT_EQ(get_array->GetBlock(), loop_body_);
212 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700213}
214
215} // namespace art