Aart Bik | 30efb4e | 2015-07-30 12:14:31 -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 <regex> |
| 18 | |
| 19 | #include "base/arena_allocator.h" |
| 20 | #include "builder.h" |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 21 | #include "induction_var_analysis.h" |
| 22 | #include "nodes.h" |
| 23 | #include "optimizing_unit_test.h" |
| 24 | |
| 25 | namespace art { |
| 26 | |
| 27 | /** |
| 28 | * Fixture class for the InductionVarAnalysis tests. |
| 29 | */ |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 30 | class InductionVarAnalysisTest : public CommonCompilerTest { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 31 | public: |
| 32 | InductionVarAnalysisTest() : pool_(), allocator_(&pool_) { |
| 33 | graph_ = CreateGraph(&allocator_); |
| 34 | } |
| 35 | |
| 36 | ~InductionVarAnalysisTest() { } |
| 37 | |
| 38 | // Builds single for-loop at depth d. |
| 39 | void BuildForLoop(int d, int n) { |
| 40 | ASSERT_LT(d, n); |
| 41 | loop_preheader_[d] = new (&allocator_) HBasicBlock(graph_); |
| 42 | graph_->AddBlock(loop_preheader_[d]); |
| 43 | loop_header_[d] = new (&allocator_) HBasicBlock(graph_); |
| 44 | graph_->AddBlock(loop_header_[d]); |
| 45 | loop_preheader_[d]->AddSuccessor(loop_header_[d]); |
| 46 | if (d < (n - 1)) { |
| 47 | BuildForLoop(d + 1, n); |
| 48 | } |
| 49 | loop_body_[d] = new (&allocator_) HBasicBlock(graph_); |
| 50 | graph_->AddBlock(loop_body_[d]); |
| 51 | loop_body_[d]->AddSuccessor(loop_header_[d]); |
| 52 | if (d < (n - 1)) { |
| 53 | loop_header_[d]->AddSuccessor(loop_preheader_[d + 1]); |
| 54 | loop_header_[d + 1]->AddSuccessor(loop_body_[d]); |
| 55 | } else { |
| 56 | loop_header_[d]->AddSuccessor(loop_body_[d]); |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | // Builds a n-nested loop in CFG where each loop at depth 0 <= d < n |
| 61 | // is defined as "for (int i_d = 0; i_d < 100; i_d++)". Tests can further |
| 62 | // populate the loop with instructions to set up interesting scenarios. |
| 63 | void BuildLoopNest(int n) { |
| 64 | ASSERT_LE(n, 10); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 65 | graph_->SetNumberOfVRegs(n + 3); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 66 | |
| 67 | // Build basic blocks with entry, nested loop, exit. |
| 68 | entry_ = new (&allocator_) HBasicBlock(graph_); |
| 69 | graph_->AddBlock(entry_); |
| 70 | BuildForLoop(0, n); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 71 | return_ = new (&allocator_) HBasicBlock(graph_); |
| 72 | graph_->AddBlock(return_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 73 | exit_ = new (&allocator_) HBasicBlock(graph_); |
| 74 | graph_->AddBlock(exit_); |
| 75 | entry_->AddSuccessor(loop_preheader_[0]); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 76 | loop_header_[0]->AddSuccessor(return_); |
| 77 | return_->AddSuccessor(exit_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 78 | graph_->SetEntryBlock(entry_); |
| 79 | graph_->SetExitBlock(exit_); |
| 80 | |
| 81 | // Provide entry and exit instructions. |
Calin Juravle | e6e3bea | 2015-10-14 13:53:10 +0000 | [diff] [blame] | 82 | parameter_ = new (&allocator_) HParameterValue( |
| 83 | graph_->GetDexFile(), 0, 0, Primitive::kPrimNot, true); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 84 | entry_->AddInstruction(parameter_); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 85 | constant0_ = graph_->GetIntConstant(0); |
| 86 | constant1_ = graph_->GetIntConstant(1); |
| 87 | constant100_ = graph_->GetIntConstant(100); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 88 | float_constant0_ = graph_->GetFloatConstant(0.0f); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 89 | return_->AddInstruction(new (&allocator_) HReturnVoid()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 90 | exit_->AddInstruction(new (&allocator_) HExit()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 91 | |
| 92 | // Provide loop instructions. |
| 93 | for (int d = 0; d < n; d++) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 94 | basic_[d] = new (&allocator_) HPhi(&allocator_, d, 0, Primitive::kPrimInt); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 95 | loop_preheader_[d]->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 96 | loop_header_[d]->AddPhi(basic_[d]); |
| 97 | HInstruction* compare = new (&allocator_) HLessThan(basic_[d], constant100_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 98 | loop_header_[d]->AddInstruction(compare); |
| 99 | loop_header_[d]->AddInstruction(new (&allocator_) HIf(compare)); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 100 | increment_[d] = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[d], constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 101 | loop_body_[d]->AddInstruction(increment_[d]); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 102 | loop_body_[d]->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 103 | |
| 104 | basic_[d]->AddInput(constant0_); |
| 105 | basic_[d]->AddInput(increment_[d]); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 106 | } |
| 107 | } |
| 108 | |
| 109 | // Builds if-statement at depth d. |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 110 | HPhi* BuildIf(int d, HBasicBlock** ifT, HBasicBlock** ifF) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 111 | HBasicBlock* cond = new (&allocator_) HBasicBlock(graph_); |
| 112 | HBasicBlock* ifTrue = new (&allocator_) HBasicBlock(graph_); |
| 113 | HBasicBlock* ifFalse = new (&allocator_) HBasicBlock(graph_); |
| 114 | graph_->AddBlock(cond); |
| 115 | graph_->AddBlock(ifTrue); |
| 116 | graph_->AddBlock(ifFalse); |
| 117 | // Conditional split. |
| 118 | loop_header_[d]->ReplaceSuccessor(loop_body_[d], cond); |
| 119 | cond->AddSuccessor(ifTrue); |
| 120 | cond->AddSuccessor(ifFalse); |
| 121 | ifTrue->AddSuccessor(loop_body_[d]); |
| 122 | ifFalse->AddSuccessor(loop_body_[d]); |
| 123 | cond->AddInstruction(new (&allocator_) HIf(parameter_)); |
| 124 | *ifT = ifTrue; |
| 125 | *ifF = ifFalse; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 126 | |
| 127 | HPhi* select_phi = new (&allocator_) HPhi(&allocator_, -1, 0, Primitive::kPrimInt); |
| 128 | loop_body_[d]->AddPhi(select_phi); |
| 129 | return select_phi; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 130 | } |
| 131 | |
| 132 | // Inserts instruction right before increment at depth d. |
| 133 | HInstruction* InsertInstruction(HInstruction* instruction, int d) { |
| 134 | loop_body_[d]->InsertInstructionBefore(instruction, increment_[d]); |
| 135 | return instruction; |
| 136 | } |
| 137 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 138 | // Inserts a phi to loop header at depth d and returns it. |
| 139 | HPhi* InsertLoopPhi(int vreg, int d) { |
| 140 | HPhi* phi = new (&allocator_) HPhi(&allocator_, vreg, 0, Primitive::kPrimInt); |
| 141 | loop_header_[d]->AddPhi(phi); |
| 142 | return phi; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 143 | } |
| 144 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 145 | // Inserts an array store with given `subscript` at depth d to |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 146 | // enable tests to inspect the computed induction at that point easily. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 147 | HInstruction* InsertArrayStore(HInstruction* subscript, int d) { |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 148 | // ArraySet is given a float value in order to avoid SsaBuilder typing |
| 149 | // it from the array's non-existent reference type info. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 150 | return InsertInstruction(new (&allocator_) HArraySet( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 151 | parameter_, subscript, float_constant0_, Primitive::kPrimFloat, 0), d); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 152 | } |
| 153 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 154 | // Returns induction information of instruction in loop at depth d. |
| 155 | std::string GetInductionInfo(HInstruction* instruction, int d) { |
| 156 | return HInductionVarAnalysis::InductionToString( |
| 157 | iva_->LookupInfo(loop_body_[d]->GetLoopInformation(), instruction)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 158 | } |
| 159 | |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 160 | // Returns induction information of the trip-count of loop at depth d. |
| 161 | std::string GetTripCount(int d) { |
| 162 | HInstruction* control = loop_header_[d]->GetLastInstruction(); |
| 163 | DCHECK(control->IsIf()); |
| 164 | return GetInductionInfo(control, d); |
| 165 | } |
| 166 | |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 167 | // Returns true if instructions have identical induction. |
| 168 | bool HaveSameInduction(HInstruction* instruction1, HInstruction* instruction2) { |
| 169 | return HInductionVarAnalysis::InductionEqual( |
| 170 | iva_->LookupInfo(loop_body_[0]->GetLoopInformation(), instruction1), |
| 171 | iva_->LookupInfo(loop_body_[0]->GetLoopInformation(), instruction2)); |
| 172 | } |
| 173 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 174 | // Performs InductionVarAnalysis (after proper set up). |
| 175 | void PerformInductionVarAnalysis() { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 176 | graph_->BuildDominatorTree(); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 177 | iva_ = new (&allocator_) HInductionVarAnalysis(graph_); |
| 178 | iva_->Run(); |
| 179 | } |
| 180 | |
| 181 | // General building fields. |
| 182 | ArenaPool pool_; |
| 183 | ArenaAllocator allocator_; |
| 184 | HGraph* graph_; |
| 185 | HInductionVarAnalysis* iva_; |
| 186 | |
| 187 | // Fixed basic blocks and instructions. |
| 188 | HBasicBlock* entry_; |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 189 | HBasicBlock* return_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 190 | HBasicBlock* exit_; |
| 191 | HInstruction* parameter_; // "this" |
| 192 | HInstruction* constant0_; |
| 193 | HInstruction* constant1_; |
| 194 | HInstruction* constant100_; |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 195 | HInstruction* float_constant0_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 196 | |
| 197 | // Loop specifics. |
| 198 | HBasicBlock* loop_preheader_[10]; |
| 199 | HBasicBlock* loop_header_[10]; |
| 200 | HBasicBlock* loop_body_[10]; |
| 201 | HInstruction* increment_[10]; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 202 | HPhi* basic_[10]; // "vreg_d", the "i_d" |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 203 | }; |
| 204 | |
| 205 | // |
| 206 | // The actual InductionVarAnalysis tests. |
| 207 | // |
| 208 | |
| 209 | TEST_F(InductionVarAnalysisTest, ProperLoopSetup) { |
| 210 | // Setup: |
| 211 | // for (int i_0 = 0; i_0 < 100; i_0++) { |
| 212 | // .. |
| 213 | // for (int i_9 = 0; i_9 < 100; i_9++) { |
| 214 | // } |
| 215 | // .. |
| 216 | // } |
| 217 | BuildLoopNest(10); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 218 | graph_->BuildDominatorTree(); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 219 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 220 | ASSERT_EQ(entry_->GetLoopInformation(), nullptr); |
| 221 | for (int d = 0; d < 1; d++) { |
| 222 | ASSERT_EQ(loop_preheader_[d]->GetLoopInformation(), |
| 223 | (d == 0) ? nullptr |
| 224 | : loop_header_[d - 1]->GetLoopInformation()); |
| 225 | ASSERT_NE(loop_header_[d]->GetLoopInformation(), nullptr); |
| 226 | ASSERT_NE(loop_body_[d]->GetLoopInformation(), nullptr); |
| 227 | ASSERT_EQ(loop_header_[d]->GetLoopInformation(), |
| 228 | loop_body_[d]->GetLoopInformation()); |
| 229 | } |
| 230 | ASSERT_EQ(exit_->GetLoopInformation(), nullptr); |
| 231 | } |
| 232 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 233 | TEST_F(InductionVarAnalysisTest, FindBasicInduction) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 234 | // Setup: |
| 235 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 236 | // a[i] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 237 | // } |
| 238 | BuildLoopNest(1); |
| 239 | HInstruction* store = InsertArrayStore(basic_[0], 0); |
| 240 | PerformInductionVarAnalysis(); |
| 241 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 242 | EXPECT_STREQ("((1) * i + (0)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 243 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(increment_[0], 0).c_str()); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 244 | |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 245 | // Offset matters! |
| 246 | EXPECT_FALSE(HaveSameInduction(store->InputAt(1), increment_[0])); |
| 247 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 248 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 249 | EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))", GetTripCount(0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 250 | } |
| 251 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 252 | TEST_F(InductionVarAnalysisTest, FindDerivedInduction) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 253 | // Setup: |
| 254 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 255 | // k = 100 + i; |
| 256 | // k = 100 - i; |
| 257 | // k = 100 * i; |
| 258 | // k = i << 1; |
| 259 | // k = - i; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 260 | // } |
| 261 | BuildLoopNest(1); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 262 | HInstruction* add = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 263 | new (&allocator_) HAdd(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 264 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 265 | new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 266 | HInstruction* mul = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 267 | new (&allocator_) HMul(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 268 | HInstruction* shl = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 269 | new (&allocator_) HShl(Primitive::kPrimInt, basic_[0], constant1_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 270 | HInstruction* neg = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 271 | new (&allocator_) HNeg(Primitive::kPrimInt, basic_[0]), 0); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 272 | PerformInductionVarAnalysis(); |
| 273 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 274 | EXPECT_STREQ("((1) * i + (100)):PrimInt", GetInductionInfo(add, 0).c_str()); |
| 275 | EXPECT_STREQ("(( - (1)) * i + (100)):PrimInt", GetInductionInfo(sub, 0).c_str()); |
| 276 | EXPECT_STREQ("((100) * i + (0)):PrimInt", GetInductionInfo(mul, 0).c_str()); |
| 277 | EXPECT_STREQ("((2) * i + (0)):PrimInt", GetInductionInfo(shl, 0).c_str()); |
| 278 | EXPECT_STREQ("(( - (1)) * i + (0)):PrimInt", GetInductionInfo(neg, 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 279 | } |
| 280 | |
| 281 | TEST_F(InductionVarAnalysisTest, FindChainInduction) { |
| 282 | // Setup: |
| 283 | // k = 0; |
| 284 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 285 | // k = k + 100; |
| 286 | // a[k] = 0; |
| 287 | // k = k - 1; |
| 288 | // a[k] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 289 | // } |
| 290 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 291 | HPhi* k = InsertLoopPhi(0, 0); |
| 292 | k->AddInput(constant0_); |
| 293 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 294 | HInstruction* add = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 295 | new (&allocator_) HAdd(Primitive::kPrimInt, k, constant100_), 0); |
| 296 | HInstruction* store1 = InsertArrayStore(add, 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 297 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 298 | new (&allocator_) HSub(Primitive::kPrimInt, add, constant1_), 0); |
| 299 | HInstruction* store2 = InsertArrayStore(sub, 0); |
| 300 | k->AddInput(sub); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 301 | PerformInductionVarAnalysis(); |
| 302 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 303 | EXPECT_STREQ("(((100) - (1)) * i + (100)):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 304 | GetInductionInfo(store1->InputAt(1), 0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 305 | EXPECT_STREQ("(((100) - (1)) * i + ((100) - (1))):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 306 | GetInductionInfo(store2->InputAt(1), 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | TEST_F(InductionVarAnalysisTest, FindTwoWayBasicInduction) { |
| 310 | // Setup: |
| 311 | // k = 0; |
| 312 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 313 | // if () k = k + 1; |
| 314 | // else k = k + 1; |
| 315 | // a[k] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 316 | // } |
| 317 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 318 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 319 | k_header->AddInput(constant0_); |
| 320 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 321 | HBasicBlock* ifTrue; |
| 322 | HBasicBlock* ifFalse; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 323 | HPhi* k_body = BuildIf(0, &ifTrue, &ifFalse); |
| 324 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 325 | // True-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 326 | HInstruction* inc1 = new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 327 | ifTrue->AddInstruction(inc1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 328 | k_body->AddInput(inc1); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 329 | // False-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 330 | HInstruction* inc2 = new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 331 | ifFalse->AddInstruction(inc2); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 332 | k_body->AddInput(inc2); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 333 | // Merge over a phi. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 334 | HInstruction* store = InsertArrayStore(k_body, 0); |
| 335 | k_header->AddInput(k_body); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 336 | PerformInductionVarAnalysis(); |
| 337 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 338 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 339 | |
| 340 | // Both increments get same induction. |
| 341 | EXPECT_TRUE(HaveSameInduction(store->InputAt(1), inc1)); |
| 342 | EXPECT_TRUE(HaveSameInduction(store->InputAt(1), inc2)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | TEST_F(InductionVarAnalysisTest, FindTwoWayDerivedInduction) { |
| 346 | // Setup: |
| 347 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 348 | // if () k = i + 1; |
| 349 | // else k = i + 1; |
| 350 | // a[k] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 351 | // } |
| 352 | BuildLoopNest(1); |
| 353 | HBasicBlock* ifTrue; |
| 354 | HBasicBlock* ifFalse; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 355 | HPhi* k = BuildIf(0, &ifTrue, &ifFalse); |
| 356 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 357 | // True-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 358 | HInstruction* inc1 = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[0], constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 359 | ifTrue->AddInstruction(inc1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 360 | k->AddInput(inc1); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 361 | // False-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 362 | HInstruction* inc2 = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[0], constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 363 | ifFalse->AddInstruction(inc2); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 364 | k->AddInput(inc2); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 365 | // Merge over a phi. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 366 | HInstruction* store = InsertArrayStore(k, 0); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 367 | PerformInductionVarAnalysis(); |
| 368 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 369 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 370 | } |
| 371 | |
| 372 | TEST_F(InductionVarAnalysisTest, FindFirstOrderWrapAroundInduction) { |
| 373 | // Setup: |
| 374 | // k = 0; |
| 375 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 376 | // a[k] = 0; |
| 377 | // k = 100 - i; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 378 | // } |
| 379 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 380 | HPhi* k = InsertLoopPhi(0, 0); |
| 381 | k->AddInput(constant0_); |
| 382 | |
| 383 | HInstruction* store = InsertArrayStore(k, 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 384 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 385 | new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
| 386 | k->AddInput(sub); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 387 | PerformInductionVarAnalysis(); |
| 388 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 389 | EXPECT_STREQ("wrap((0), (( - (1)) * i + (100)):PrimInt):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 390 | GetInductionInfo(store->InputAt(1), 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | TEST_F(InductionVarAnalysisTest, FindSecondOrderWrapAroundInduction) { |
| 394 | // Setup: |
| 395 | // k = 0; |
| 396 | // t = 100; |
| 397 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 398 | // a[k] = 0; |
| 399 | // k = t; |
| 400 | // t = 100 - i; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 401 | // } |
| 402 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 403 | HPhi* k = InsertLoopPhi(0, 0); |
| 404 | k->AddInput(constant0_); |
| 405 | HPhi* t = InsertLoopPhi(1, 0); |
| 406 | t->AddInput(constant100_); |
| 407 | |
| 408 | HInstruction* store = InsertArrayStore(k, 0); |
| 409 | k->AddInput(t); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 410 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 411 | new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0], 0), 0); |
| 412 | t->AddInput(sub); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 413 | PerformInductionVarAnalysis(); |
| 414 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 415 | EXPECT_STREQ("wrap((0), wrap((100), (( - (1)) * i + (100)):PrimInt):PrimInt):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 416 | GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 417 | } |
| 418 | |
| 419 | TEST_F(InductionVarAnalysisTest, FindWrapAroundDerivedInduction) { |
| 420 | // Setup: |
| 421 | // k = 0; |
| 422 | // for (int i = 0; i < 100; i++) { |
| 423 | // t = k + 100; |
| 424 | // t = k - 100; |
| 425 | // t = k * 100; |
| 426 | // t = k << 1; |
| 427 | // t = - k; |
| 428 | // k = i << 1; |
| 429 | // } |
| 430 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 431 | HPhi* k = InsertLoopPhi(0, 0); |
| 432 | k->AddInput(constant0_); |
| 433 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 434 | HInstruction* add = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 435 | new (&allocator_) HAdd(Primitive::kPrimInt, k, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 436 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 437 | new (&allocator_) HSub(Primitive::kPrimInt, k, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 438 | HInstruction* mul = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 439 | new (&allocator_) HMul(Primitive::kPrimInt, k, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 440 | HInstruction* shl = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 441 | new (&allocator_) HShl(Primitive::kPrimInt, k, constant1_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 442 | HInstruction* neg = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 443 | new (&allocator_) HNeg(Primitive::kPrimInt, k), 0); |
| 444 | k->AddInput( |
| 445 | InsertInstruction(new (&allocator_) HShl(Primitive::kPrimInt, basic_[0], constant1_), 0)); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 446 | PerformInductionVarAnalysis(); |
| 447 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 448 | EXPECT_STREQ("wrap((100), ((2) * i + (100)):PrimInt):PrimInt", |
| 449 | GetInductionInfo(add, 0).c_str()); |
| 450 | EXPECT_STREQ("wrap(((0) - (100)), ((2) * i + ((0) - (100))):PrimInt):PrimInt", |
| 451 | GetInductionInfo(sub, 0).c_str()); |
| 452 | EXPECT_STREQ("wrap((0), (((2) * (100)) * i + (0)):PrimInt):PrimInt", |
| 453 | GetInductionInfo(mul, 0).c_str()); |
| 454 | EXPECT_STREQ("wrap((0), (((2) * (2)) * i + (0)):PrimInt):PrimInt", |
| 455 | GetInductionInfo(shl, 0).c_str()); |
| 456 | EXPECT_STREQ("wrap((0), (( - (2)) * i + (0)):PrimInt):PrimInt", |
| 457 | GetInductionInfo(neg, 0).c_str()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 458 | } |
| 459 | |
| 460 | TEST_F(InductionVarAnalysisTest, FindPeriodicInduction) { |
| 461 | // Setup: |
| 462 | // k = 0; |
| 463 | // t = 100; |
| 464 | // for (int i = 0; i < 100; i++) { |
| 465 | // a[k] = 0; |
| 466 | // a[t] = 0; |
| 467 | // // Swap t <-> k. |
| 468 | // d = t; |
| 469 | // t = k; |
| 470 | // k = d; |
| 471 | // } |
| 472 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 473 | HPhi* k = InsertLoopPhi(0, 0); |
| 474 | k->AddInput(constant0_); |
| 475 | HPhi* t = InsertLoopPhi(1, 0); |
| 476 | t->AddInput(constant100_); |
| 477 | |
| 478 | HInstruction* store1 = InsertArrayStore(k, 0); |
| 479 | HInstruction* store2 = InsertArrayStore(t, 0); |
| 480 | k->AddInput(t); |
| 481 | t->AddInput(k); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 482 | PerformInductionVarAnalysis(); |
| 483 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 484 | EXPECT_STREQ("periodic((0), (100)):PrimInt", GetInductionInfo(store1->InputAt(1), 0).c_str()); |
| 485 | EXPECT_STREQ("periodic((100), (0)):PrimInt", GetInductionInfo(store2->InputAt(1), 0).c_str()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | TEST_F(InductionVarAnalysisTest, FindIdiomaticPeriodicInduction) { |
| 489 | // Setup: |
| 490 | // k = 0; |
| 491 | // for (int i = 0; i < 100; i++) { |
| 492 | // a[k] = 0; |
| 493 | // k = 1 - k; |
| 494 | // } |
| 495 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 496 | HPhi* k = InsertLoopPhi(0, 0); |
| 497 | k->AddInput(constant0_); |
| 498 | |
| 499 | HInstruction* store = InsertArrayStore(k, 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 500 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 501 | new (&allocator_) HSub(Primitive::kPrimInt, constant1_, k), 0); |
| 502 | k->AddInput(sub); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 503 | PerformInductionVarAnalysis(); |
| 504 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 505 | EXPECT_STREQ("periodic((0), (1)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 506 | EXPECT_STREQ("periodic((1), (0)):PrimInt", GetInductionInfo(sub, 0).c_str()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 509 | TEST_F(InductionVarAnalysisTest, FindXorPeriodicInduction) { |
| 510 | // Setup: |
| 511 | // k = 0; |
| 512 | // for (int i = 0; i < 100; i++) { |
| 513 | // a[k] = 0; |
| 514 | // k = k ^ 1; |
| 515 | // } |
| 516 | BuildLoopNest(1); |
| 517 | HPhi* k = InsertLoopPhi(0, 0); |
| 518 | k->AddInput(constant0_); |
| 519 | |
| 520 | HInstruction* store = InsertArrayStore(k, 0); |
| 521 | HInstruction* x = InsertInstruction( |
| 522 | new (&allocator_) HXor(Primitive::kPrimInt, k, constant1_), 0); |
| 523 | k->AddInput(x); |
| 524 | PerformInductionVarAnalysis(); |
| 525 | |
| 526 | EXPECT_STREQ("periodic((0), (1)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 527 | EXPECT_STREQ("periodic((1), (0)):PrimInt", GetInductionInfo(x, 0).c_str()); |
| 528 | } |
| 529 | |
| 530 | TEST_F(InductionVarAnalysisTest, FindXor100PeriodicInduction) { |
| 531 | // Setup: |
| 532 | // k = 100; |
| 533 | // for (int i = 0; i < 100; i++) { |
| 534 | // k = k ^ 100; |
| 535 | // } |
| 536 | BuildLoopNest(1); |
| 537 | HPhi* k = InsertLoopPhi(0, 0); |
| 538 | k->AddInput(constant100_); |
| 539 | |
| 540 | HInstruction* x = InsertInstruction( |
| 541 | new (&allocator_) HXor(Primitive::kPrimInt, k, constant100_), 0); |
| 542 | k->AddInput(x); |
| 543 | PerformInductionVarAnalysis(); |
| 544 | |
| 545 | EXPECT_STREQ("periodic(((100) ^ (100)), (100)):PrimInt", GetInductionInfo(x, 0).c_str()); |
| 546 | } |
| 547 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 548 | TEST_F(InductionVarAnalysisTest, FindDerivedPeriodicInduction) { |
| 549 | // Setup: |
| 550 | // k = 0; |
| 551 | // for (int i = 0; i < 100; i++) { |
| 552 | // k = 1 - k; |
| 553 | // t = k + 100; |
| 554 | // t = k - 100; |
| 555 | // t = k * 100; |
| 556 | // t = k << 1; |
| 557 | // t = - k; |
| 558 | // } |
| 559 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 560 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 561 | k_header->AddInput(constant0_); |
| 562 | |
| 563 | HInstruction* k_body = InsertInstruction( |
| 564 | new (&allocator_) HSub(Primitive::kPrimInt, constant1_, k_header), 0); |
| 565 | k_header->AddInput(k_body); |
| 566 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 567 | // Derived expressions. |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 568 | HInstruction* add = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 569 | new (&allocator_) HAdd(Primitive::kPrimInt, k_body, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 570 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 571 | new (&allocator_) HSub(Primitive::kPrimInt, k_body, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 572 | HInstruction* mul = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 573 | new (&allocator_) HMul(Primitive::kPrimInt, k_body, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 574 | HInstruction* shl = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 575 | new (&allocator_) HShl(Primitive::kPrimInt, k_body, constant1_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 576 | HInstruction* neg = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 577 | new (&allocator_) HNeg(Primitive::kPrimInt, k_body), 0); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 578 | PerformInductionVarAnalysis(); |
| 579 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 580 | EXPECT_STREQ("periodic(((1) + (100)), (100)):PrimInt", GetInductionInfo(add, 0).c_str()); |
| 581 | EXPECT_STREQ("periodic(((1) - (100)), ((0) - (100))):PrimInt", GetInductionInfo(sub, 0).c_str()); |
| 582 | EXPECT_STREQ("periodic((100), (0)):PrimInt", GetInductionInfo(mul, 0).c_str()); |
| 583 | EXPECT_STREQ("periodic((2), (0)):PrimInt", GetInductionInfo(shl, 0).c_str()); |
| 584 | EXPECT_STREQ("periodic(( - (1)), (0)):PrimInt", GetInductionInfo(neg, 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 585 | } |
| 586 | |
| 587 | TEST_F(InductionVarAnalysisTest, FindDeepLoopInduction) { |
| 588 | // Setup: |
| 589 | // k = 0; |
| 590 | // for (int i_0 = 0; i_0 < 100; i_0++) { |
| 591 | // .. |
| 592 | // for (int i_9 = 0; i_9 < 100; i_9++) { |
| 593 | // k = 1 + k; |
| 594 | // a[k] = 0; |
| 595 | // } |
| 596 | // .. |
| 597 | // } |
| 598 | BuildLoopNest(10); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 599 | |
| 600 | HPhi* k[10]; |
| 601 | for (int d = 0; d < 10; d++) { |
| 602 | k[d] = InsertLoopPhi(0, d); |
| 603 | } |
| 604 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 605 | HInstruction* inc = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 606 | new (&allocator_) HAdd(Primitive::kPrimInt, constant1_, k[9]), 9); |
| 607 | HInstruction* store = InsertArrayStore(inc, 9); |
| 608 | |
| 609 | for (int d = 0; d < 10; d++) { |
David Brazdil | 6dd10fa | 2016-02-15 17:14:31 +0000 | [diff] [blame] | 610 | k[d]->AddInput((d != 0) ? k[d - 1] : constant0_); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 611 | k[d]->AddInput((d != 9) ? k[d + 1] : inc); |
| 612 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 613 | PerformInductionVarAnalysis(); |
| 614 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 615 | // Avoid exact phi number, since that depends on the SSA building phase. |
| 616 | std::regex r("\\(\\(1\\) \\* i \\+ " |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 617 | "\\(\\(1\\) \\+ \\(\\d+:Phi\\)\\)\\):PrimInt"); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 618 | |
| 619 | for (int d = 0; d < 10; d++) { |
| 620 | if (d == 9) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 621 | EXPECT_TRUE(std::regex_match(GetInductionInfo(store->InputAt(1), d), r)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 622 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 623 | EXPECT_STREQ("", GetInductionInfo(store->InputAt(1), d).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 624 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 625 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(increment_[d], d).c_str()); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 626 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 627 | EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))", GetTripCount(d).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 628 | } |
| 629 | } |
| 630 | |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 631 | TEST_F(InductionVarAnalysisTest, ByteInductionIntLoopControl) { |
| 632 | // Setup: |
| 633 | // for (int i = 0; i < 100; i++) { |
| 634 | // k = (byte) i; |
| 635 | // a[k] = 0; |
| 636 | // a[i] = 0; |
| 637 | // } |
| 638 | BuildLoopNest(1); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 639 | HInstruction* conv = InsertInstruction( |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 640 | new (&allocator_) HTypeConversion(Primitive::kPrimByte, basic_[0], -1), 0); |
| 641 | HInstruction* store1 = InsertArrayStore(conv, 0); |
| 642 | HInstruction* store2 = InsertArrayStore(basic_[0], 0); |
| 643 | PerformInductionVarAnalysis(); |
| 644 | |
| 645 | // Regular int induction (i) is "transferred" over conversion into byte induction (k). |
| 646 | EXPECT_STREQ("((1) * i + (0)):PrimByte", GetInductionInfo(store1->InputAt(1), 0).c_str()); |
| 647 | EXPECT_STREQ("((1) * i + (0)):PrimInt", GetInductionInfo(store2->InputAt(1), 0).c_str()); |
| 648 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(increment_[0], 0).c_str()); |
| 649 | |
| 650 | // Type matters! |
| 651 | EXPECT_FALSE(HaveSameInduction(store1->InputAt(1), store2->InputAt(1))); |
| 652 | |
| 653 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 654 | EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))", GetTripCount(0).c_str()); |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 655 | } |
| 656 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 657 | TEST_F(InductionVarAnalysisTest, ByteLoopControl1) { |
| 658 | // Setup: |
| 659 | // for (byte i = -128; i < 127; i++) { // just fits! |
| 660 | // } |
| 661 | BuildLoopNest(1); |
| 662 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-128), 0); |
| 663 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 664 | ifs->ReplaceInput(graph_->GetIntConstant(127), 1); |
| 665 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimByte, increment_[0], -1); |
| 666 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 667 | basic_[0]->ReplaceInput(conv, 1); |
| 668 | PerformInductionVarAnalysis(); |
| 669 | |
| 670 | EXPECT_STREQ("((1) * i + ((-128) + (1))):PrimByte", GetInductionInfo(increment_[0], 0).c_str()); |
| 671 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 672 | EXPECT_STREQ("(((127) - (-128)) (TC-loop) ((-128) < (127)))", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 673 | } |
| 674 | |
| 675 | TEST_F(InductionVarAnalysisTest, ByteLoopControl2) { |
| 676 | // Setup: |
| 677 | // for (byte i = -128; i < 128; i++) { // infinite loop! |
| 678 | // } |
| 679 | BuildLoopNest(1); |
| 680 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-128), 0); |
| 681 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 682 | ifs->ReplaceInput(graph_->GetIntConstant(128), 1); |
| 683 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimByte, increment_[0], -1); |
| 684 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 685 | basic_[0]->ReplaceInput(conv, 1); |
| 686 | PerformInductionVarAnalysis(); |
| 687 | |
| 688 | EXPECT_STREQ("((1) * i + ((-128) + (1))):PrimByte", GetInductionInfo(increment_[0], 0).c_str()); |
| 689 | // Trip-count undefined. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 690 | EXPECT_STREQ("", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 691 | } |
| 692 | |
| 693 | TEST_F(InductionVarAnalysisTest, ShortLoopControl1) { |
| 694 | // Setup: |
| 695 | // for (short i = -32768; i < 32767; i++) { // just fits! |
| 696 | // } |
| 697 | BuildLoopNest(1); |
| 698 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-32768), 0); |
| 699 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 700 | ifs->ReplaceInput(graph_->GetIntConstant(32767), 1); |
| 701 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimShort, increment_[0], -1); |
| 702 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 703 | basic_[0]->ReplaceInput(conv, 1); |
| 704 | PerformInductionVarAnalysis(); |
| 705 | |
| 706 | EXPECT_STREQ("((1) * i + ((-32768) + (1))):PrimShort", |
| 707 | GetInductionInfo(increment_[0], 0).c_str()); |
| 708 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 709 | EXPECT_STREQ("(((32767) - (-32768)) (TC-loop) ((-32768) < (32767)))", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 710 | } |
| 711 | |
| 712 | TEST_F(InductionVarAnalysisTest, ShortLoopControl2) { |
| 713 | // Setup: |
| 714 | // for (short i = -32768; i < 32768; i++) { // infinite loop! |
| 715 | // } |
| 716 | BuildLoopNest(1); |
| 717 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-32768), 0); |
| 718 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 719 | ifs->ReplaceInput(graph_->GetIntConstant(32768), 1); |
| 720 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimShort, increment_[0], -1); |
| 721 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 722 | basic_[0]->ReplaceInput(conv, 1); |
| 723 | PerformInductionVarAnalysis(); |
| 724 | |
| 725 | EXPECT_STREQ("((1) * i + ((-32768) + (1))):PrimShort", |
| 726 | GetInductionInfo(increment_[0], 0).c_str()); |
| 727 | // Trip-count undefined. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 728 | EXPECT_STREQ("", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 729 | } |
| 730 | |
| 731 | TEST_F(InductionVarAnalysisTest, CharLoopControl1) { |
| 732 | // Setup: |
| 733 | // for (char i = 0; i < 65535; i++) { // just fits! |
| 734 | // } |
| 735 | BuildLoopNest(1); |
| 736 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 737 | ifs->ReplaceInput(graph_->GetIntConstant(65535), 1); |
| 738 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimChar, increment_[0], -1); |
| 739 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 740 | basic_[0]->ReplaceInput(conv, 1); |
| 741 | PerformInductionVarAnalysis(); |
| 742 | |
| 743 | EXPECT_STREQ("((1) * i + (1)):PrimChar", GetInductionInfo(increment_[0], 0).c_str()); |
| 744 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 745 | EXPECT_STREQ("((65535) (TC-loop) ((0) < (65535)))", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 746 | } |
| 747 | |
| 748 | TEST_F(InductionVarAnalysisTest, CharLoopControl2) { |
| 749 | // Setup: |
| 750 | // for (char i = 0; i < 65536; i++) { // infinite loop! |
| 751 | // } |
| 752 | BuildLoopNest(1); |
| 753 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 754 | ifs->ReplaceInput(graph_->GetIntConstant(65536), 1); |
| 755 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimChar, increment_[0], -1); |
| 756 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 757 | basic_[0]->ReplaceInput(conv, 1); |
| 758 | PerformInductionVarAnalysis(); |
| 759 | |
| 760 | EXPECT_STREQ("((1) * i + (1)):PrimChar", GetInductionInfo(increment_[0], 0).c_str()); |
| 761 | // Trip-count undefined. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 762 | EXPECT_STREQ("", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 763 | } |
| 764 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 765 | } // namespace art |