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