Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 1 | /* |
| 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 19 | #include "licm.h" |
| 20 | #include "nodes.h" |
| 21 | #include "optimizing_unit_test.h" |
| 22 | #include "side_effects_analysis.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | /** |
| 27 | * Fixture class for the LICM tests. |
| 28 | */ |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 29 | class LICMTest : public CommonCompilerTest { |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 30 | 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 44 | return_ = new (&allocator_) HBasicBlock(graph_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 45 | 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 51 | graph_->AddBlock(return_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 52 | 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 61 | loop_header_->AddSuccessor(return_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 62 | loop_body_->AddSuccessor(loop_header_); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 63 | return_->AddSuccessor(exit_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 64 | |
| 65 | // Provide boiler-plate instructions. |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 66 | parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), |
| 67 | dex::TypeIndex(0), |
| 68 | 0, |
| 69 | Primitive::kPrimNot); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 70 | entry_->AddInstruction(parameter_); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 71 | int_constant_ = graph_->GetIntConstant(42); |
| 72 | float_constant_ = graph_->GetFloatConstant(42.0f); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 73 | loop_preheader_->AddInstruction(new (&allocator_) HGoto()); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 74 | loop_header_->AddInstruction(new (&allocator_) HIf(parameter_)); |
| 75 | loop_body_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 76 | return_->AddInstruction(new (&allocator_) HReturnVoid()); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 77 | exit_->AddInstruction(new (&allocator_) HExit()); |
| 78 | } |
| 79 | |
| 80 | // Performs LICM optimizations (after proper set up). |
| 81 | void PerformLICM() { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 82 | graph_->BuildDominatorTree(); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 83 | SideEffectsAnalysis side_effects(graph_); |
| 84 | side_effects.Run(); |
Jean-Philippe Halimi | 38e9e80 | 2016-02-18 16:42:03 +0100 | [diff] [blame] | 85 | LICM(graph_, side_effects, nullptr).Run(); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 86 | } |
| 87 | |
| 88 | // General building fields. |
| 89 | ArenaPool pool_; |
| 90 | ArenaAllocator allocator_; |
| 91 | HGraph* graph_; |
| 92 | |
| 93 | // Specific basic blocks. |
| 94 | HBasicBlock* entry_; |
| 95 | HBasicBlock* loop_preheader_; |
| 96 | HBasicBlock* loop_header_; |
| 97 | HBasicBlock* loop_body_; |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 98 | HBasicBlock* return_; |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 99 | HBasicBlock* exit_; |
| 100 | |
| 101 | HInstruction* parameter_; // "this" |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 102 | HInstruction* int_constant_; |
| 103 | HInstruction* float_constant_; |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 104 | }; |
| 105 | |
| 106 | // |
| 107 | // The actual LICM tests. |
| 108 | // |
| 109 | |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 110 | TEST_F(LICMTest, FieldHoisting) { |
| 111 | BuildLoop(); |
| 112 | |
| 113 | // Populate the loop with instructions: set/get field with different types. |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 114 | HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 115 | nullptr, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 116 | Primitive::kPrimLong, |
| 117 | MemberOffset(10), |
| 118 | false, |
| 119 | kUnknownFieldIndex, |
| 120 | kUnknownClassDefIndex, |
| 121 | graph_->GetDexFile(), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 122 | 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 123 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
| 124 | HInstruction* set_field = new (&allocator_) HInstanceFieldSet( |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 125 | parameter_, int_constant_, nullptr, Primitive::kPrimInt, MemberOffset(20), |
| 126 | false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 127 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 128 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 129 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 130 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 131 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 132 | EXPECT_EQ(get_field->GetBlock(), loop_preheader_); |
| 133 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 134 | } |
| 135 | |
| 136 | TEST_F(LICMTest, NoFieldHoisting) { |
| 137 | BuildLoop(); |
| 138 | |
| 139 | // Populate the loop with instructions: set/get field with same types. |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 140 | ScopedNullHandle<mirror::DexCache> dex_cache; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 141 | HInstruction* get_field = new (&allocator_) HInstanceFieldGet(parameter_, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 142 | nullptr, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 143 | Primitive::kPrimLong, |
| 144 | MemberOffset(10), |
| 145 | false, |
| 146 | kUnknownFieldIndex, |
| 147 | kUnknownClassDefIndex, |
| 148 | graph_->GetDexFile(), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 149 | 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 150 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 151 | HInstruction* set_field = new (&allocator_) HInstanceFieldSet(parameter_, |
| 152 | get_field, |
Nicolas Geoffray | c52b26d | 2016-12-19 09:18:07 +0000 | [diff] [blame] | 153 | nullptr, |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 154 | Primitive::kPrimLong, |
| 155 | MemberOffset(10), |
| 156 | false, |
| 157 | kUnknownFieldIndex, |
| 158 | kUnknownClassDefIndex, |
| 159 | graph_->GetDexFile(), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 160 | 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 161 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 162 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 163 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 164 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 165 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 166 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 167 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 168 | } |
| 169 | |
| 170 | TEST_F(LICMTest, ArrayHoisting) { |
| 171 | BuildLoop(); |
| 172 | |
| 173 | // Populate the loop with instructions: set/get array with different types. |
| 174 | HInstruction* get_array = new (&allocator_) HArrayGet( |
Aart Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 175 | parameter_, int_constant_, Primitive::kPrimInt, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 176 | loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction()); |
| 177 | HInstruction* set_array = new (&allocator_) HArraySet( |
Aart Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 178 | parameter_, int_constant_, float_constant_, Primitive::kPrimFloat, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 179 | loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction()); |
| 180 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 181 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 182 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 183 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 184 | EXPECT_EQ(get_array->GetBlock(), loop_preheader_); |
| 185 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | TEST_F(LICMTest, NoArrayHoisting) { |
| 189 | BuildLoop(); |
| 190 | |
| 191 | // Populate the loop with instructions: set/get array with same types. |
| 192 | HInstruction* get_array = new (&allocator_) HArrayGet( |
Aart Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 193 | parameter_, int_constant_, Primitive::kPrimFloat, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 194 | loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction()); |
| 195 | HInstruction* set_array = new (&allocator_) HArraySet( |
Aart Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 196 | parameter_, get_array, float_constant_, Primitive::kPrimFloat, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 197 | loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction()); |
| 198 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 199 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 200 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 201 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 202 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 203 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | } // namespace art |