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. |
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 | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 68 | int_constant_ = graph_->GetIntConstant(42); |
| 69 | float_constant_ = graph_->GetFloatConstant(42.0f); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 70 | loop_preheader_->AddInstruction(new (&allocator_) HGoto()); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 71 | loop_header_->AddInstruction(new (&allocator_) HIf(parameter_)); |
| 72 | loop_body_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 73 | return_->AddInstruction(new (&allocator_) HReturnVoid()); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 74 | exit_->AddInstruction(new (&allocator_) HExit()); |
| 75 | } |
| 76 | |
| 77 | // Performs LICM optimizations (after proper set up). |
| 78 | void PerformLICM() { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 79 | graph_->BuildDominatorTree(); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 80 | SideEffectsAnalysis side_effects(graph_); |
| 81 | side_effects.Run(); |
Jean-Philippe Halimi | 38e9e80 | 2016-02-18 16:42:03 +0100 | [diff] [blame] | 82 | LICM(graph_, side_effects, nullptr).Run(); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 95 | HBasicBlock* return_; |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 96 | HBasicBlock* exit_; |
| 97 | |
| 98 | HInstruction* parameter_; // "this" |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 99 | HInstruction* int_constant_; |
| 100 | HInstruction* float_constant_; |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 101 | }; |
| 102 | |
| 103 | // |
| 104 | // The actual LICM tests. |
| 105 | // |
| 106 | |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 107 | TEST_F(LICMTest, FieldHoisting) { |
| 108 | BuildLoop(); |
| 109 | |
| 110 | // Populate the loop with instructions: set/get field with different types. |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 111 | ScopedNullHandle<mirror::DexCache> dex_cache; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 112 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 121 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
| 122 | HInstruction* set_field = new (&allocator_) HInstanceFieldSet( |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 123 | parameter_, int_constant_, Primitive::kPrimInt, MemberOffset(20), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 124 | false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), dex_cache, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 125 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 126 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 127 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 128 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 129 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 130 | EXPECT_EQ(get_field->GetBlock(), loop_preheader_); |
| 131 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | TEST_F(LICMTest, NoFieldHoisting) { |
| 135 | BuildLoop(); |
| 136 | |
| 137 | // Populate the loop with instructions: set/get field with same types. |
Mathieu Chartier | 9865bde | 2015-12-21 09:58:16 -0800 | [diff] [blame] | 138 | ScopedNullHandle<mirror::DexCache> dex_cache; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 139 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 148 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 149 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 159 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 160 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 161 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 162 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 163 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 164 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 165 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 166 | } |
| 167 | |
| 168 | TEST_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 Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 173 | parameter_, int_constant_, Primitive::kPrimInt, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 174 | loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction()); |
| 175 | HInstruction* set_array = new (&allocator_) HArraySet( |
Aart Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 176 | parameter_, int_constant_, float_constant_, Primitive::kPrimFloat, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 177 | loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction()); |
| 178 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 179 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 180 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 181 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 182 | EXPECT_EQ(get_array->GetBlock(), loop_preheader_); |
| 183 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | TEST_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 Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 191 | parameter_, int_constant_, Primitive::kPrimFloat, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 192 | loop_body_->InsertInstructionBefore(get_array, loop_body_->GetLastInstruction()); |
| 193 | HInstruction* set_array = new (&allocator_) HArraySet( |
Aart Bik | 18b36ab | 2016-04-13 16:41:35 -0700 | [diff] [blame] | 194 | parameter_, get_array, float_constant_, Primitive::kPrimFloat, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 195 | loop_body_->InsertInstructionBefore(set_array, loop_body_->GetLastInstruction()); |
| 196 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 197 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 198 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 199 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 200 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 201 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 202 | } |
| 203 | |
| 204 | } // namespace art |