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" |
| 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 | |
| 25 | namespace art { |
| 26 | |
| 27 | /** |
| 28 | * Fixture class for the LICM tests. |
| 29 | */ |
| 30 | class 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 45 | return_ = new (&allocator_) HBasicBlock(graph_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 46 | 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 52 | graph_->AddBlock(return_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 53 | 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 Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 62 | loop_header_->AddSuccessor(return_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 63 | loop_body_->AddSuccessor(loop_header_); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 64 | return_->AddSuccessor(exit_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 65 | |
| 66 | // Provide boiler-plate instructions. |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 67 | parameter_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), 0, 0, Primitive::kPrimNot); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 68 | entry_->AddInstruction(parameter_); |
David Brazdil | 77a48ae | 2015-09-15 12:34:04 +0000 | [diff] [blame] | 69 | constant_ = graph_->GetIntConstant(42); |
| 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()); |
| 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 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" |
| 99 | HInstruction* constant_; |
| 100 | }; |
| 101 | |
| 102 | // |
| 103 | // The actual LICM tests. |
| 104 | // |
| 105 | |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 106 | TEST_F(LICMTest, FieldHoisting) { |
| 107 | BuildLoop(); |
| 108 | |
| 109 | // Populate the loop with instructions: set/get field with different types. |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 110 | NullHandle<mirror::DexCache> dex_cache; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 111 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 120 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
| 121 | HInstruction* set_field = new (&allocator_) HInstanceFieldSet( |
| 122 | parameter_, constant_, Primitive::kPrimInt, MemberOffset(20), |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 123 | false, kUnknownFieldIndex, kUnknownClassDefIndex, graph_->GetDexFile(), dex_cache, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 124 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 125 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 126 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 127 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 128 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 129 | EXPECT_EQ(get_field->GetBlock(), loop_preheader_); |
| 130 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 131 | } |
| 132 | |
| 133 | TEST_F(LICMTest, NoFieldHoisting) { |
| 134 | BuildLoop(); |
| 135 | |
| 136 | // Populate the loop with instructions: set/get field with same types. |
Mathieu Chartier | 736b560 | 2015-09-02 14:54:11 -0700 | [diff] [blame] | 137 | NullHandle<mirror::DexCache> dex_cache; |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 138 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 147 | loop_body_->InsertInstructionBefore(get_field, loop_body_->GetLastInstruction()); |
Mingyao Yang | 8df69d4 | 2015-10-22 15:40:58 -0700 | [diff] [blame] | 148 | 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 Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 158 | loop_body_->InsertInstructionBefore(set_field, loop_body_->GetLastInstruction()); |
| 159 | |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 160 | EXPECT_EQ(get_field->GetBlock(), loop_body_); |
| 161 | EXPECT_EQ(set_field->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 162 | PerformLICM(); |
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 | } |
| 166 | |
| 167 | TEST_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 Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 172 | parameter_, constant_, Primitive::kPrimLong, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 173 | 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 Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 178 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 179 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 180 | PerformLICM(); |
Aart Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 181 | EXPECT_EQ(get_array->GetBlock(), loop_preheader_); |
| 182 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 183 | } |
| 184 | |
| 185 | TEST_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 Juravle | 154746b | 2015-10-06 15:46:54 +0100 | [diff] [blame] | 190 | parameter_, constant_, Primitive::kPrimLong, 0); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 191 | 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 Bik | 8f08f9e | 2015-07-22 11:27:39 -0700 | [diff] [blame] | 196 | EXPECT_EQ(get_array->GetBlock(), loop_body_); |
| 197 | EXPECT_EQ(set_array->GetBlock(), loop_body_); |
Aart Bik | 3f307f3 | 2015-07-21 18:30:18 -0700 | [diff] [blame] | 198 | PerformLICM(); |
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 | } |
| 202 | |
| 203 | } // namespace art |