blob: 2a6264346586d9bf121656135ff1ac9f9848ba20 [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.
Calin Juravlee6e3bea2015-10-14 13:53:10 +000066 parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), 0, 0, Primitive::kPrimNot);
Aart Bik3f307f32015-07-21 18:30:18 -070067 entry_->AddInstruction(parameter_);
David Brazdil15693bf2015-12-16 10:30:45 +000068 int_constant_ = graph_->GetIntConstant(42);
69 float_constant_ = graph_->GetFloatConstant(42.0f);
David Brazdil77a48ae2015-09-15 12:34:04 +000070 loop_preheader_->AddInstruction(new (&allocator_) HGoto());
Aart Bik3f307f32015-07-21 18:30:18 -070071 loop_header_->AddInstruction(new (&allocator_) HIf(parameter_));
72 loop_body_->AddInstruction(new (&allocator_) HGoto());
David Brazdil4833f5a2015-12-16 10:37:39 +000073 return_->AddInstruction(new (&allocator_) HReturnVoid());
Aart Bik3f307f32015-07-21 18:30:18 -070074 exit_->AddInstruction(new (&allocator_) HExit());
75 }
76
77 // Performs LICM optimizations (after proper set up).
78 void PerformLICM() {
David Brazdilbadd8262016-02-02 16:28:56 +000079 graph_->BuildDominatorTree();
Aart Bik3f307f32015-07-21 18:30:18 -070080 SideEffectsAnalysis side_effects(graph_);
81 side_effects.Run();
Jean-Philippe Halimi38e9e802016-02-18 16:42:03 +010082 LICM(graph_, side_effects, nullptr).Run();
Aart Bik3f307f32015-07-21 18:30:18 -070083 }
84
85 // General building fields.
86 ArenaPool pool_;
87 ArenaAllocator allocator_;
88 HGraph* graph_;
89
90 // Specific basic blocks.
91 HBasicBlock* entry_;
92 HBasicBlock* loop_preheader_;
93 HBasicBlock* loop_header_;
94 HBasicBlock* loop_body_;
David Brazdildb51efb2015-11-06 01:36:20 +000095 HBasicBlock* return_;
Aart Bik3f307f32015-07-21 18:30:18 -070096 HBasicBlock* exit_;
97
98 HInstruction* parameter_; // "this"
David Brazdil15693bf2015-12-16 10:30:45 +000099 HInstruction* int_constant_;
100 HInstruction* float_constant_;
Aart Bik3f307f32015-07-21 18:30:18 -0700101};
102
103//
104// The actual LICM tests.
105//
106
Aart Bik3f307f32015-07-21 18:30:18 -0700107TEST_F(LICMTest, FieldHoisting) {
108 BuildLoop();
109
110 // Populate the loop with instructions: set/get field with different types.
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800111 ScopedNullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700112 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
113 Primitive::kPrimLong,
114 MemberOffset(10),
115 false,
116 kUnknownFieldIndex,
117 kUnknownClassDefIndex,
118 graph_->GetDexFile(),
119 dex_cache,
120 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700121 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
122 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(
David Brazdil15693bf2015-12-16 10:30:45 +0000123 parameter_, int_constant_, Primitive::kPrimInt, MemberOffset(20),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700124 false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), dex_cache, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700125 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
126
Aart Bik8f08f9e2015-07-22 11:27:39 -0700127 EXPECT_EQ(get_field->GetBlock(), loop_body_);
128 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700129 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700130 EXPECT_EQ(get_field->GetBlock(), loop_preheader_);
131 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700132}
133
134TEST_F(LICMTest, NoFieldHoisting) {
135 BuildLoop();
136
137 // Populate the loop with instructions: set/get field with same types.
Mathieu Chartier9865bde2015-12-21 09:58:16 -0800138 ScopedNullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700139 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
140 Primitive::kPrimLong,
141 MemberOffset(10),
142 false,
143 kUnknownFieldIndex,
144 kUnknownClassDefIndex,
145 graph_->GetDexFile(),
146 dex_cache,
147 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700148 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Mingyao Yang8df69d42015-10-22 15:40:58 -0700149 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(parameter_,
150 get_field,
151 Primitive::kPrimLong,
152 MemberOffset(10),
153 false,
154 kUnknownFieldIndex,
155 kUnknownClassDefIndex,
156 graph_->GetDexFile(),
157 dex_cache,
158 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700159 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
160
Aart Bik8f08f9e2015-07-22 11:27:39 -0700161 EXPECT_EQ(get_field->GetBlock(), loop_body_);
162 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700163 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700164 EXPECT_EQ(get_field->GetBlock(), loop_body_);
165 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700166}
167
168TEST_F(LICMTest, ArrayHoisting) {
169 BuildLoop();
170
171 // Populate the loop with instructions: set/get array with different types.
172 HInstruction* get_array = new (&allocator_) HArrayGet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700173 parameter_, int_constant_, Primitive::kPrimInt, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700174 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
175 HInstruction* set_array = new (&allocator_) HArraySet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700176 parameter_, int_constant_, float_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700177 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
178
Aart Bik8f08f9e2015-07-22 11:27:39 -0700179 EXPECT_EQ(get_array->GetBlock(), loop_body_);
180 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700181 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700182 EXPECT_EQ(get_array->GetBlock(), loop_preheader_);
183 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700184}
185
186TEST_F(LICMTest, NoArrayHoisting) {
187 BuildLoop();
188
189 // Populate the loop with instructions: set/get array with same types.
190 HInstruction* get_array = new (&allocator_) HArrayGet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700191 parameter_, int_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700192 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
193 HInstruction* set_array = new (&allocator_) HArraySet(
Aart Bik18b36ab2016-04-13 16:41:35 -0700194 parameter_, get_array, float_constant_, Primitive::kPrimFloat, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700195 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
196
Aart Bik8f08f9e2015-07-22 11:27:39 -0700197 EXPECT_EQ(get_array->GetBlock(), loop_body_);
198 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700199 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700200 EXPECT_EQ(get_array->GetBlock(), loop_body_);
201 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700202}
203
204} // namespace art