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 | d9510df | 2015-11-04 23:30:22 +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. |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 66 | parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), 0, 0, Primitive::kPrimNot); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 67 | entry_->AddInstruction(parameter_); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 68 | constant_ = graph_->GetIntConstant(42); |
| 69 | loop_preheader_->AddInstruction(new (&allocator_) HGoto()); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 70 | loop_header_->AddInstruction(new (&allocator_) HIf(parameter_)); |
| 71 | loop_body_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 72 | return_->AddInstruction(new (&allocator_) HReturnVoid()); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 73 | exit_->AddInstruction(new (&allocator_) HExit()); |
| 74 | } |
| 75 | |
| 76 | // Performs LICM optimizations (after proper set up). |
| 77 | void PerformLICM() { |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 78 | TransformToSsa(graph_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 79 | SideEffectsAnalysis side_effects(graph_); |
| 80 | side_effects.Run(); |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 81 | LICM(graph_, side_effects).Run(); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 82 | } |
| 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 94 | HBasicBlock* return_; |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 95 | HBasicBlock* exit_; |
| 96 | |
| 97 | HInstruction* parameter_; // "this" |
| 98 | HInstruction* constant_; |
| 99 | }; |
| 100 | |
| 101 | // |
| 102 | // The actual LICM tests. |
| 103 | // |
| 104 | |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 105 | TEST_F(LICMTest, FieldHoisting) { |
| 106 | BuildLoop(); |
| 107 | |
| 108 | // Populate the loop with instructions: set/get field with different types. |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 109 | NullHandle<mirror::DexCache> dex_cache; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 110 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 119 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
| 120 | HInstruction* set_field = new (&allocator_) HInstanceFieldSet( |
| 121 | parameter_, constant_, Primitive::kPrimInt, MemberOffset(20), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 122 | false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), dex_cache, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 123 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 124 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 125 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 126 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 127 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 128 | EXPECT_EQ(get_field->GetBlock(), loop_preheader_); |
| 129 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | TEST_F(LICMTest, NoFieldHoisting) { |
| 133 | BuildLoop(); |
| 134 | |
| 135 | // Populate the loop with instructions: set/get field with same types. |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 136 | NullHandle<mirror::DexCache> dex_cache; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 137 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 146 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 147 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 157 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 158 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 159 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 160 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 161 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 162 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 163 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 164 | } |
| 165 | |
| 166 | TEST_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 Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 171 | parameter_, constant_, Primitive::kPrimByte, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 172 | loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction()); |
| 173 | HInstruction* set_array = new (&allocator_) HArraySet( |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 174 | parameter_, constant_, constant_, Primitive::kPrimShort, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 175 | loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction()); |
| 176 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 177 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 178 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 179 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 180 | EXPECT_EQ(get_array->GetBlock(), loop_preheader_); |
| 181 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 182 | } |
| 183 | |
| 184 | TEST_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 Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 189 | parameter_, constant_, Primitive::kPrimByte, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 190 | loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction()); |
| 191 | HInstruction* set_array = new (&allocator_) HArraySet( |
David Brazdil | d9510df | 2015-11-04 23:30:22 +0000 | [diff] [blame] | 192 | parameter_, get_array, constant_, Primitive::kPrimByte, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 193 | loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction()); |
| 194 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 195 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 196 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 197 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 198 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 199 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 200 | } |
| 201 | |
| 202 | } // namespace art |