blob: 956de2cb8a303d72854aacfa11a2794d4f431a4f [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 Brazdild9510df2015-11-04 23:30:22 +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 Brazdil77a48ae2015-09-15 12:34:04 +000068 constant_ = graph_->GetIntConstant(42);
69 loop_preheader_->AddInstruction(new (&allocator_) HGoto());
Aart Bik3f307f32015-07-21 18:30:18 -070070 loop_header_->AddInstruction(new (&allocator_) HIf(parameter_));
71 loop_body_->AddInstruction(new (&allocator_) HGoto());
David Brazdild9510df2015-11-04 23:30:22 +000072 return_->AddInstruction(new (&allocator_) HReturnVoid());
Aart Bik3f307f32015-07-21 18:30:18 -070073 exit_->AddInstruction(new (&allocator_) HExit());
74 }
75
76 // Performs LICM optimizations (after proper set up).
77 void PerformLICM() {
David Brazdild9510df2015-11-04 23:30:22 +000078 TransformToSsa(graph_);
Aart Bik3f307f32015-07-21 18:30:18 -070079 SideEffectsAnalysis side_effects(graph_);
80 side_effects.Run();
David Brazdild9510df2015-11-04 23:30:22 +000081 LICM(graph_, side_effects).Run();
Aart Bik3f307f32015-07-21 18:30:18 -070082 }
83
84 // General building fields.
85 ArenaPool pool_;
86 ArenaAllocator allocator_;
87 HGraph* graph_;
88
89 // Specific basic blocks.
90 HBasicBlock* entry_;
91 HBasicBlock* loop_preheader_;
92 HBasicBlock* loop_header_;
93 HBasicBlock* loop_body_;
David Brazdildb51efb2015-11-06 01:36:20 +000094 HBasicBlock* return_;
Aart Bik3f307f32015-07-21 18:30:18 -070095 HBasicBlock* exit_;
96
97 HInstruction* parameter_; // "this"
98 HInstruction* constant_;
99};
100
101//
102// The actual LICM tests.
103//
104
Aart Bik3f307f32015-07-21 18:30:18 -0700105TEST_F(LICMTest, FieldHoisting) {
106 BuildLoop();
107
108 // Populate the loop with instructions: set/get field with different types.
Mathieu Chartier736b5602015-09-02 14:54:11 -0700109 NullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700110 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
111 Primitive::kPrimLong,
112 MemberOffset(10),
113 false,
114 kUnknownFieldIndex,
115 kUnknownClassDefIndex,
116 graph_->GetDexFile(),
117 dex_cache,
118 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700119 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
120 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(
121 parameter_, constant_, Primitive::kPrimInt, MemberOffset(20),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700122 false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), dex_cache, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700123 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
124
Aart Bik8f08f9e2015-07-22 11:27:39 -0700125 EXPECT_EQ(get_field->GetBlock(), loop_body_);
126 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700127 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700128 EXPECT_EQ(get_field->GetBlock(), loop_preheader_);
129 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700130}
131
132TEST_F(LICMTest, NoFieldHoisting) {
133 BuildLoop();
134
135 // Populate the loop with instructions: set/get field with same types.
Mathieu Chartier736b5602015-09-02 14:54:11 -0700136 NullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700137 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
138 Primitive::kPrimLong,
139 MemberOffset(10),
140 false,
141 kUnknownFieldIndex,
142 kUnknownClassDefIndex,
143 graph_->GetDexFile(),
144 dex_cache,
145 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700146 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Mingyao Yang8df69d42015-10-22 15:40:58 -0700147 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(parameter_,
148 get_field,
149 Primitive::kPrimLong,
150 MemberOffset(10),
151 false,
152 kUnknownFieldIndex,
153 kUnknownClassDefIndex,
154 graph_->GetDexFile(),
155 dex_cache,
156 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700157 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
158
Aart Bik8f08f9e2015-07-22 11:27:39 -0700159 EXPECT_EQ(get_field->GetBlock(), loop_body_);
160 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700161 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700162 EXPECT_EQ(get_field->GetBlock(), loop_body_);
163 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700164}
165
166TEST_F(LICMTest, ArrayHoisting) {
167 BuildLoop();
168
169 // Populate the loop with instructions: set/get array with different types.
170 HInstruction* get_array = new (&allocator_) HArrayGet(
David Brazdild9510df2015-11-04 23:30:22 +0000171 parameter_, constant_, Primitive::kPrimByte, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700172 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
173 HInstruction* set_array = new (&allocator_) HArraySet(
David Brazdild9510df2015-11-04 23:30:22 +0000174 parameter_, constant_, constant_, Primitive::kPrimShort, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700175 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
176
Aart Bik8f08f9e2015-07-22 11:27:39 -0700177 EXPECT_EQ(get_array->GetBlock(), loop_body_);
178 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700179 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700180 EXPECT_EQ(get_array->GetBlock(), loop_preheader_);
181 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700182}
183
184TEST_F(LICMTest, NoArrayHoisting) {
185 BuildLoop();
186
187 // Populate the loop with instructions: set/get array with same types.
188 HInstruction* get_array = new (&allocator_) HArrayGet(
David Brazdild9510df2015-11-04 23:30:22 +0000189 parameter_, constant_, Primitive::kPrimByte, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700190 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
191 HInstruction* set_array = new (&allocator_) HArraySet(
David Brazdild9510df2015-11-04 23:30:22 +0000192 parameter_, get_array, constant_, Primitive::kPrimByte, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700193 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
194
Aart Bik8f08f9e2015-07-22 11:27:39 -0700195 EXPECT_EQ(get_array->GetBlock(), loop_body_);
196 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700197 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700198 EXPECT_EQ(get_array->GetBlock(), loop_body_);
199 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700200}
201
202} // namespace art