blob: 2bb769a43046a7236b7ab3dd0e2cca6fd75ec31f [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"
19#include "gtest/gtest.h"
20#include "licm.h"
21#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 */
30class LICMTest : public testing::Test {
31 public:
32 LICMTest() : pool_(), allocator_(&pool_) {
33 graph_ = CreateGraph(&allocator_);
34 }
35
36 ~LICMTest() { }
37
38 // Builds a singly-nested loop structure in CFG. Tests can further populate
39 // the basic blocks with instructions to set up interesting scenarios.
40 void BuildLoop() {
41 entry_ = new (&allocator_) HBasicBlock(graph_);
42 loop_preheader_ = new (&allocator_) HBasicBlock(graph_);
43 loop_header_ = new (&allocator_) HBasicBlock(graph_);
44 loop_body_ = new (&allocator_) HBasicBlock(graph_);
David Brazdildb51efb2015-11-06 01:36:20 +000045 return_ = new (&allocator_) HBasicBlock(graph_);
Aart Bik3f307f32015-07-21 18:30:18 -070046 exit_ = new (&allocator_) HBasicBlock(graph_);
47
48 graph_->AddBlock(entry_);
49 graph_->AddBlock(loop_preheader_);
50 graph_->AddBlock(loop_header_);
51 graph_->AddBlock(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000052 graph_->AddBlock(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070053 graph_->AddBlock(exit_);
54
55 graph_->SetEntryBlock(entry_);
56 graph_->SetExitBlock(exit_);
57
58 // Set up loop flow in CFG.
59 entry_->AddSuccessor(loop_preheader_);
60 loop_preheader_->AddSuccessor(loop_header_);
61 loop_header_->AddSuccessor(loop_body_);
David Brazdildb51efb2015-11-06 01:36:20 +000062 loop_header_->AddSuccessor(return_);
Aart Bik3f307f32015-07-21 18:30:18 -070063 loop_body_->AddSuccessor(loop_header_);
David Brazdildb51efb2015-11-06 01:36:20 +000064 return_->AddSuccessor(exit_);
Aart Bik3f307f32015-07-21 18:30:18 -070065
66 // Provide boiler-plate instructions.
Calin Juravlee6e3bea2015-10-14 13:53:10 +000067 parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), 0, 0, Primitive::kPrimNot);
Aart Bik3f307f32015-07-21 18:30:18 -070068 entry_->AddInstruction(parameter_);
David Brazdil77a48ae2015-09-15 12:34:04 +000069 constant_ = graph_->GetIntConstant(42);
70 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());
73 exit_->AddInstruction(new (&allocator_) HExit());
74 }
75
76 // Performs LICM optimizations (after proper set up).
77 void PerformLICM() {
78 ASSERT_TRUE(graph_->TryBuildingSsa());
79 SideEffectsAnalysis side_effects(graph_);
80 side_effects.Run();
81 LICM licm(graph_, side_effects);
82 licm.Run();
83 }
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"
99 HInstruction* constant_;
100};
101
102//
103// The actual LICM tests.
104//
105
Aart Bik3f307f32015-07-21 18:30:18 -0700106TEST_F(LICMTest, FieldHoisting) {
107 BuildLoop();
108
109 // Populate the loop with instructions: set/get field with different types.
Mathieu Chartier736b5602015-09-02 14:54:11 -0700110 NullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700111 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
112 Primitive::kPrimLong,
113 MemberOffset(10),
114 false,
115 kUnknownFieldIndex,
116 kUnknownClassDefIndex,
117 graph_->GetDexFile(),
118 dex_cache,
119 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700120 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
121 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(
122 parameter_, constant_, Primitive::kPrimInt, MemberOffset(20),
Mingyao Yang8df69d42015-10-22 15:40:58 -0700123 false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), dex_cache, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700124 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
125
Aart Bik8f08f9e2015-07-22 11:27:39 -0700126 EXPECT_EQ(get_field->GetBlock(), loop_body_);
127 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700128 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700129 EXPECT_EQ(get_field->GetBlock(), loop_preheader_);
130 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700131}
132
133TEST_F(LICMTest, NoFieldHoisting) {
134 BuildLoop();
135
136 // Populate the loop with instructions: set/get field with same types.
Mathieu Chartier736b5602015-09-02 14:54:11 -0700137 NullHandle<mirror::DexCache> dex_cache;
Mingyao Yang8df69d42015-10-22 15:40:58 -0700138 HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_,
139 Primitive::kPrimLong,
140 MemberOffset(10),
141 false,
142 kUnknownFieldIndex,
143 kUnknownClassDefIndex,
144 graph_->GetDexFile(),
145 dex_cache,
146 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700147 loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction());
Mingyao Yang8df69d42015-10-22 15:40:58 -0700148 HInstruction* set_field = new (&allocator_) HInstanceFieldSet(parameter_,
149 get_field,
150 Primitive::kPrimLong,
151 MemberOffset(10),
152 false,
153 kUnknownFieldIndex,
154 kUnknownClassDefIndex,
155 graph_->GetDexFile(),
156 dex_cache,
157 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700158 loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction());
159
Aart Bik8f08f9e2015-07-22 11:27:39 -0700160 EXPECT_EQ(get_field->GetBlock(), loop_body_);
161 EXPECT_EQ(set_field->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700162 PerformLICM();
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}
166
167TEST_F(LICMTest, ArrayHoisting) {
168 BuildLoop();
169
170 // Populate the loop with instructions: set/get array with different types.
171 HInstruction* get_array = new (&allocator_) HArrayGet(
Calin Juravle154746b2015-10-06 15:46:54 +0100172 parameter_, constant_, Primitive::kPrimLong, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700173 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
174 HInstruction* set_array = new (&allocator_) HArraySet(
175 parameter_, constant_, constant_, Primitive::kPrimInt, 0);
176 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
177
Aart Bik8f08f9e2015-07-22 11:27:39 -0700178 EXPECT_EQ(get_array->GetBlock(), loop_body_);
179 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700180 PerformLICM();
Aart Bik8f08f9e2015-07-22 11:27:39 -0700181 EXPECT_EQ(get_array->GetBlock(), loop_preheader_);
182 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700183}
184
185TEST_F(LICMTest, NoArrayHoisting) {
186 BuildLoop();
187
188 // Populate the loop with instructions: set/get array with same types.
189 HInstruction* get_array = new (&allocator_) HArrayGet(
Calin Juravle154746b2015-10-06 15:46:54 +0100190 parameter_, constant_, Primitive::kPrimLong, 0);
Aart Bik3f307f32015-07-21 18:30:18 -0700191 loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction());
192 HInstruction* set_array = new (&allocator_) HArraySet(
193 parameter_, get_array, constant_, Primitive::kPrimLong, 0);
194 loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction());
195
Aart Bik8f08f9e2015-07-22 11:27:39 -0700196 EXPECT_EQ(get_array->GetBlock(), loop_body_);
197 EXPECT_EQ(set_array->GetBlock(), loop_body_);
Aart Bik3f307f32015-07-21 18:30:18 -0700198 PerformLICM();
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}
202
203} // namespace art