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( |
Andreas Gampe | a5b09a6 | 2016-11-17 15:21:22 -0800 | [diff] [blame] | 83 | graph_->GetDexFile(), dex::TypeIndex(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); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 87 | constant2_ = graph_->GetIntConstant(2); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 88 | constant7_ = graph_->GetIntConstant(7); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 89 | constant100_ = graph_->GetIntConstant(100); |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 90 | float_constant0_ = graph_->GetFloatConstant(0.0f); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 91 | return_->AddInstruction(new (&allocator_) HReturnVoid()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 92 | exit_->AddInstruction(new (&allocator_) HExit()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 93 | |
| 94 | // Provide loop instructions. |
| 95 | for (int d = 0; d < n; d++) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 96 | basic_[d] = new (&allocator_) HPhi(&allocator_, d, 0, Primitive::kPrimInt); |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 97 | loop_preheader_[d]->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 98 | loop_header_[d]->AddPhi(basic_[d]); |
| 99 | HInstruction* compare = new (&allocator_) HLessThan(basic_[d], constant100_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 100 | loop_header_[d]->AddInstruction(compare); |
| 101 | loop_header_[d]->AddInstruction(new (&allocator_) HIf(compare)); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 102 | increment_[d] = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[d], constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 103 | loop_body_[d]->AddInstruction(increment_[d]); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 104 | loop_body_[d]->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 105 | |
| 106 | basic_[d]->AddInput(constant0_); |
| 107 | basic_[d]->AddInput(increment_[d]); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 108 | } |
| 109 | } |
| 110 | |
| 111 | // Builds if-statement at depth d. |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 112 | HPhi* BuildIf(int d, HBasicBlock** ifT, HBasicBlock** ifF) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 113 | HBasicBlock* cond = new (&allocator_) HBasicBlock(graph_); |
| 114 | HBasicBlock* ifTrue = new (&allocator_) HBasicBlock(graph_); |
| 115 | HBasicBlock* ifFalse = new (&allocator_) HBasicBlock(graph_); |
| 116 | graph_->AddBlock(cond); |
| 117 | graph_->AddBlock(ifTrue); |
| 118 | graph_->AddBlock(ifFalse); |
| 119 | // Conditional split. |
| 120 | loop_header_[d]->ReplaceSuccessor(loop_body_[d], cond); |
| 121 | cond->AddSuccessor(ifTrue); |
| 122 | cond->AddSuccessor(ifFalse); |
| 123 | ifTrue->AddSuccessor(loop_body_[d]); |
| 124 | ifFalse->AddSuccessor(loop_body_[d]); |
| 125 | cond->AddInstruction(new (&allocator_) HIf(parameter_)); |
| 126 | *ifT = ifTrue; |
| 127 | *ifF = ifFalse; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 128 | |
| 129 | HPhi* select_phi = new (&allocator_) HPhi(&allocator_, -1, 0, Primitive::kPrimInt); |
| 130 | loop_body_[d]->AddPhi(select_phi); |
| 131 | return select_phi; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 132 | } |
| 133 | |
| 134 | // Inserts instruction right before increment at depth d. |
| 135 | HInstruction* InsertInstruction(HInstruction* instruction, int d) { |
| 136 | loop_body_[d]->InsertInstructionBefore(instruction, increment_[d]); |
| 137 | return instruction; |
| 138 | } |
| 139 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 140 | // Inserts a phi to loop header at depth d and returns it. |
| 141 | HPhi* InsertLoopPhi(int vreg, int d) { |
| 142 | HPhi* phi = new (&allocator_) HPhi(&allocator_, vreg, 0, Primitive::kPrimInt); |
| 143 | loop_header_[d]->AddPhi(phi); |
| 144 | return phi; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 145 | } |
| 146 | |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 147 | // Inserts an array store with given `subscript` at depth d to |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 148 | // enable tests to inspect the computed induction at that point easily. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 149 | HInstruction* InsertArrayStore(HInstruction* subscript, int d) { |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 150 | // ArraySet is given a float value in order to avoid SsaBuilder typing |
| 151 | // it from the array's non-existent reference type info. |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 152 | return InsertInstruction(new (&allocator_) HArraySet( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 153 | parameter_, subscript, float_constant0_, Primitive::kPrimFloat, 0), d); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 154 | } |
| 155 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 156 | // Returns induction information of instruction in loop at depth d. |
| 157 | std::string GetInductionInfo(HInstruction* instruction, int d) { |
| 158 | return HInductionVarAnalysis::InductionToString( |
| 159 | iva_->LookupInfo(loop_body_[d]->GetLoopInformation(), instruction)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 160 | } |
| 161 | |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 162 | // Returns induction information of the trip-count of loop at depth d. |
| 163 | std::string GetTripCount(int d) { |
| 164 | HInstruction* control = loop_header_[d]->GetLastInstruction(); |
| 165 | DCHECK(control->IsIf()); |
| 166 | return GetInductionInfo(control, d); |
| 167 | } |
| 168 | |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 169 | // Returns true if instructions have identical induction. |
| 170 | bool HaveSameInduction(HInstruction* instruction1, HInstruction* instruction2) { |
| 171 | return HInductionVarAnalysis::InductionEqual( |
| 172 | iva_->LookupInfo(loop_body_[0]->GetLoopInformation(), instruction1), |
| 173 | iva_->LookupInfo(loop_body_[0]->GetLoopInformation(), instruction2)); |
| 174 | } |
| 175 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 176 | // Performs InductionVarAnalysis (after proper set up). |
| 177 | void PerformInductionVarAnalysis() { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 178 | graph_->BuildDominatorTree(); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 179 | iva_ = new (&allocator_) HInductionVarAnalysis(graph_); |
| 180 | iva_->Run(); |
| 181 | } |
| 182 | |
| 183 | // General building fields. |
| 184 | ArenaPool pool_; |
| 185 | ArenaAllocator allocator_; |
| 186 | HGraph* graph_; |
| 187 | HInductionVarAnalysis* iva_; |
| 188 | |
| 189 | // Fixed basic blocks and instructions. |
| 190 | HBasicBlock* entry_; |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 191 | HBasicBlock* return_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 192 | HBasicBlock* exit_; |
| 193 | HInstruction* parameter_; // "this" |
| 194 | HInstruction* constant0_; |
| 195 | HInstruction* constant1_; |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 196 | HInstruction* constant2_; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 197 | HInstruction* constant7_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 198 | HInstruction* constant100_; |
David Brazdil | 15693bf | 2015-12-16 10:30:45 +0000 | [diff] [blame] | 199 | HInstruction* float_constant0_; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 200 | |
| 201 | // Loop specifics. |
| 202 | HBasicBlock* loop_preheader_[10]; |
| 203 | HBasicBlock* loop_header_[10]; |
| 204 | HBasicBlock* loop_body_[10]; |
| 205 | HInstruction* increment_[10]; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 206 | HPhi* basic_[10]; // "vreg_d", the "i_d" |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 207 | }; |
| 208 | |
| 209 | // |
| 210 | // The actual InductionVarAnalysis tests. |
| 211 | // |
| 212 | |
| 213 | TEST_F(InductionVarAnalysisTest, ProperLoopSetup) { |
| 214 | // Setup: |
| 215 | // for (int i_0 = 0; i_0 < 100; i_0++) { |
| 216 | // .. |
| 217 | // for (int i_9 = 0; i_9 < 100; i_9++) { |
| 218 | // } |
| 219 | // .. |
| 220 | // } |
| 221 | BuildLoopNest(10); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 222 | graph_->BuildDominatorTree(); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 223 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 224 | ASSERT_EQ(entry_->GetLoopInformation(), nullptr); |
| 225 | for (int d = 0; d < 1; d++) { |
| 226 | ASSERT_EQ(loop_preheader_[d]->GetLoopInformation(), |
| 227 | (d == 0) ? nullptr |
| 228 | : loop_header_[d - 1]->GetLoopInformation()); |
| 229 | ASSERT_NE(loop_header_[d]->GetLoopInformation(), nullptr); |
| 230 | ASSERT_NE(loop_body_[d]->GetLoopInformation(), nullptr); |
| 231 | ASSERT_EQ(loop_header_[d]->GetLoopInformation(), |
| 232 | loop_body_[d]->GetLoopInformation()); |
| 233 | } |
| 234 | ASSERT_EQ(exit_->GetLoopInformation(), nullptr); |
| 235 | } |
| 236 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 237 | TEST_F(InductionVarAnalysisTest, FindBasicInduction) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 238 | // Setup: |
| 239 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 240 | // a[i] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 241 | // } |
| 242 | BuildLoopNest(1); |
| 243 | HInstruction* store = InsertArrayStore(basic_[0], 0); |
| 244 | PerformInductionVarAnalysis(); |
| 245 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 246 | EXPECT_STREQ("((1) * i + (0)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 247 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(increment_[0], 0).c_str()); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 248 | |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 249 | // Offset matters! |
| 250 | EXPECT_FALSE(HaveSameInduction(store->InputAt(1), increment_[0])); |
| 251 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 252 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 253 | EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))", GetTripCount(0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 254 | } |
| 255 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 256 | TEST_F(InductionVarAnalysisTest, FindDerivedInduction) { |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 257 | // Setup: |
| 258 | // for (int i = 0; i < 100; i++) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 259 | // t = 100 + i; |
| 260 | // t = 100 - i; |
| 261 | // t = 100 * i; |
| 262 | // t = i << 1; |
| 263 | // t = - i; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 264 | // } |
| 265 | BuildLoopNest(1); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 266 | HInstruction* add = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 267 | new (&allocator_) HAdd(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 268 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 269 | new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 270 | HInstruction* mul = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 271 | new (&allocator_) HMul(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 272 | HInstruction* shl = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 273 | new (&allocator_) HShl(Primitive::kPrimInt, basic_[0], constant1_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 274 | HInstruction* neg = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 275 | new (&allocator_) HNeg(Primitive::kPrimInt, basic_[0]), 0); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 276 | PerformInductionVarAnalysis(); |
| 277 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 278 | EXPECT_STREQ("((1) * i + (100)):PrimInt", GetInductionInfo(add, 0).c_str()); |
| 279 | EXPECT_STREQ("(( - (1)) * i + (100)):PrimInt", GetInductionInfo(sub, 0).c_str()); |
| 280 | EXPECT_STREQ("((100) * i + (0)):PrimInt", GetInductionInfo(mul, 0).c_str()); |
| 281 | EXPECT_STREQ("((2) * i + (0)):PrimInt", GetInductionInfo(shl, 0).c_str()); |
| 282 | EXPECT_STREQ("(( - (1)) * i + (0)):PrimInt", GetInductionInfo(neg, 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 283 | } |
| 284 | |
| 285 | TEST_F(InductionVarAnalysisTest, FindChainInduction) { |
| 286 | // Setup: |
| 287 | // k = 0; |
| 288 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 289 | // k = k + 100; |
| 290 | // a[k] = 0; |
| 291 | // k = k - 1; |
| 292 | // a[k] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 293 | // } |
| 294 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 295 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 296 | k_header->AddInput(constant0_); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 297 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 298 | HInstruction* add = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 299 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant100_), 0); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 300 | HInstruction* store1 = InsertArrayStore(add, 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 301 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 302 | new (&allocator_) HSub(Primitive::kPrimInt, add, constant1_), 0); |
| 303 | HInstruction* store2 = InsertArrayStore(sub, 0); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 304 | k_header->AddInput(sub); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 305 | PerformInductionVarAnalysis(); |
| 306 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 307 | EXPECT_STREQ("(((100) - (1)) * i + (0)):PrimInt", |
| 308 | GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 309 | EXPECT_STREQ("(((100) - (1)) * i + (100)):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 310 | GetInductionInfo(store1->InputAt(1), 0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 311 | EXPECT_STREQ("(((100) - (1)) * i + ((100) - (1))):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 312 | GetInductionInfo(store2->InputAt(1), 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 313 | } |
| 314 | |
| 315 | TEST_F(InductionVarAnalysisTest, FindTwoWayBasicInduction) { |
| 316 | // Setup: |
| 317 | // k = 0; |
| 318 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 319 | // if () k = k + 1; |
| 320 | // else k = k + 1; |
| 321 | // a[k] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 322 | // } |
| 323 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 324 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 325 | k_header->AddInput(constant0_); |
| 326 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 327 | HBasicBlock* ifTrue; |
| 328 | HBasicBlock* ifFalse; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 329 | HPhi* k_body = BuildIf(0, &ifTrue, &ifFalse); |
| 330 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 331 | // True-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 332 | HInstruction* inc1 = new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 333 | ifTrue->AddInstruction(inc1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 334 | k_body->AddInput(inc1); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 335 | // False-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 336 | HInstruction* inc2 = new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 337 | ifFalse->AddInstruction(inc2); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 338 | k_body->AddInput(inc2); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 339 | // Merge over a phi. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 340 | HInstruction* store = InsertArrayStore(k_body, 0); |
| 341 | k_header->AddInput(k_body); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 342 | PerformInductionVarAnalysis(); |
| 343 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 344 | EXPECT_STREQ("((1) * i + (0)):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 345 | 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] | 346 | |
| 347 | // Both increments get same induction. |
| 348 | EXPECT_TRUE(HaveSameInduction(store->InputAt(1), inc1)); |
| 349 | EXPECT_TRUE(HaveSameInduction(store->InputAt(1), inc2)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 350 | } |
| 351 | |
| 352 | TEST_F(InductionVarAnalysisTest, FindTwoWayDerivedInduction) { |
| 353 | // Setup: |
| 354 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 355 | // if () k = i + 1; |
| 356 | // else k = i + 1; |
| 357 | // a[k] = 0; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 358 | // } |
| 359 | BuildLoopNest(1); |
| 360 | HBasicBlock* ifTrue; |
| 361 | HBasicBlock* ifFalse; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 362 | HPhi* k = BuildIf(0, &ifTrue, &ifFalse); |
| 363 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 364 | // True-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 365 | HInstruction* inc1 = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[0], constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 366 | ifTrue->AddInstruction(inc1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 367 | k->AddInput(inc1); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 368 | // False-branch. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 369 | HInstruction* inc2 = new (&allocator_) HAdd(Primitive::kPrimInt, basic_[0], constant1_); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 370 | ifFalse->AddInstruction(inc2); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 371 | k->AddInput(inc2); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 372 | // Merge over a phi. |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 373 | HInstruction* store = InsertArrayStore(k, 0); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 374 | PerformInductionVarAnalysis(); |
| 375 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 376 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 377 | |
| 378 | // Both increments get same induction. |
| 379 | EXPECT_TRUE(HaveSameInduction(store->InputAt(1), inc1)); |
| 380 | EXPECT_TRUE(HaveSameInduction(store->InputAt(1), inc2)); |
| 381 | } |
| 382 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 383 | TEST_F(InductionVarAnalysisTest, AddLinear) { |
| 384 | // Setup: |
| 385 | // for (int i = 0; i < 100; i++) { |
| 386 | // t1 = i + i; |
| 387 | // t2 = 7 + i; |
| 388 | // t3 = t1 + t2; |
| 389 | // } |
| 390 | BuildLoopNest(1); |
| 391 | |
| 392 | HInstruction* add1 = InsertInstruction( |
| 393 | new (&allocator_) HAdd(Primitive::kPrimInt, basic_[0], basic_[0]), 0); |
| 394 | HInstruction* add2 = InsertInstruction( |
| 395 | new (&allocator_) HAdd(Primitive::kPrimInt, constant7_, basic_[0]), 0); |
| 396 | HInstruction* add3 = InsertInstruction( |
| 397 | new (&allocator_) HAdd(Primitive::kPrimInt, add1, add2), 0); |
| 398 | PerformInductionVarAnalysis(); |
| 399 | |
| 400 | EXPECT_STREQ("((1) * i + (0)):PrimInt", GetInductionInfo(basic_[0], 0).c_str()); |
| 401 | EXPECT_STREQ("(((1) + (1)) * i + (0)):PrimInt", GetInductionInfo(add1, 0).c_str()); |
| 402 | EXPECT_STREQ("((1) * i + (7)):PrimInt", GetInductionInfo(add2, 0).c_str()); |
| 403 | EXPECT_STREQ("((((1) + (1)) + (1)) * i + (7)):PrimInt", GetInductionInfo(add3, 0).c_str()); |
| 404 | } |
| 405 | |
| 406 | TEST_F(InductionVarAnalysisTest, FindPolynomialInduction) { |
| 407 | // Setup: |
| 408 | // k = 1; |
| 409 | // for (int i = 0; i < 100; i++) { |
| 410 | // t = i * 2; |
| 411 | // t = 100 + t |
| 412 | // k = t + k; // polynomial |
| 413 | // } |
| 414 | BuildLoopNest(1); |
| 415 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 416 | k_header->AddInput(constant1_); |
| 417 | |
| 418 | HInstruction* mul = InsertInstruction( |
| 419 | new (&allocator_) HMul(Primitive::kPrimInt, basic_[0], constant2_), 0); |
| 420 | HInstruction* add = InsertInstruction( |
| 421 | new (&allocator_) HAdd(Primitive::kPrimInt, constant100_, mul), 0); |
| 422 | HInstruction* pol = InsertInstruction( |
| 423 | new (&allocator_) HAdd(Primitive::kPrimInt, add, k_header), 0); |
| 424 | k_header->AddInput(pol); |
| 425 | PerformInductionVarAnalysis(); |
| 426 | |
| 427 | // Note, only the phi in the cycle and the base linear induction are classified. |
| 428 | EXPECT_STREQ("poly(sum_lt(((2) * i + (100)):PrimInt) + (1)):PrimInt", |
| 429 | GetInductionInfo(k_header, 0).c_str()); |
| 430 | EXPECT_STREQ("((2) * i + (100)):PrimInt", GetInductionInfo(add, 0).c_str()); |
| 431 | EXPECT_STREQ("", GetInductionInfo(pol, 0).c_str()); |
| 432 | } |
| 433 | |
| 434 | TEST_F(InductionVarAnalysisTest, FindPolynomialInductionAndDerived) { |
| 435 | // Setup: |
| 436 | // k = 1; |
| 437 | // for (int i = 0; i < 100; i++) { |
| 438 | // t = k + 100; |
| 439 | // t = k - 1; |
| 440 | // t = - t |
| 441 | // t = k * 2; |
| 442 | // t = k << 2; |
| 443 | // k = k + i; // polynomial |
| 444 | // } |
| 445 | BuildLoopNest(1); |
| 446 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 447 | k_header->AddInput(constant1_); |
| 448 | |
| 449 | HInstruction* add = InsertInstruction( |
| 450 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant100_), 0); |
| 451 | HInstruction* sub = InsertInstruction( |
| 452 | new (&allocator_) HSub(Primitive::kPrimInt, k_header, constant1_), 0); |
| 453 | HInstruction* neg = InsertInstruction( |
| 454 | new (&allocator_) HNeg(Primitive::kPrimInt, sub), 0); |
| 455 | HInstruction* mul = InsertInstruction( |
| 456 | new (&allocator_) HMul(Primitive::kPrimInt, k_header, constant2_), 0); |
| 457 | HInstruction* shl = InsertInstruction( |
| 458 | new (&allocator_) HShl(Primitive::kPrimInt, k_header, constant2_), 0); |
| 459 | HInstruction* pol = InsertInstruction( |
| 460 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, basic_[0]), 0); |
| 461 | k_header->AddInput(pol); |
| 462 | PerformInductionVarAnalysis(); |
| 463 | |
| 464 | // Note, only the phi in the cycle and derived are classified. |
| 465 | EXPECT_STREQ("poly(sum_lt(((1) * i + (0)):PrimInt) + (1)):PrimInt", |
| 466 | GetInductionInfo(k_header, 0).c_str()); |
| 467 | EXPECT_STREQ("poly(sum_lt(((1) * i + (0)):PrimInt) + ((1) + (100))):PrimInt", |
| 468 | GetInductionInfo(add, 0).c_str()); |
| 469 | EXPECT_STREQ("poly(sum_lt(((1) * i + (0)):PrimInt) + ((1) - (1))):PrimInt", |
| 470 | GetInductionInfo(sub, 0).c_str()); |
| 471 | EXPECT_STREQ("poly(sum_lt((( - (1)) * i + (0)):PrimInt) + ((1) - (1))):PrimInt", |
| 472 | GetInductionInfo(neg, 0).c_str()); |
| 473 | EXPECT_STREQ("poly(sum_lt(((2) * i + (0)):PrimInt) + (2)):PrimInt", |
| 474 | GetInductionInfo(mul, 0).c_str()); |
| 475 | EXPECT_STREQ("poly(sum_lt(((4) * i + (0)):PrimInt) + (4)):PrimInt", |
| 476 | GetInductionInfo(shl, 0).c_str()); |
| 477 | EXPECT_STREQ("", GetInductionInfo(pol, 0).c_str()); |
| 478 | } |
| 479 | |
| 480 | TEST_F(InductionVarAnalysisTest, AddPolynomial) { |
| 481 | // Setup: |
| 482 | // k = 7; |
| 483 | // for (int i = 0; i < 100; i++) { |
| 484 | // t = k + k; |
| 485 | // t = t + k; |
| 486 | // k = k + i |
| 487 | // } |
| 488 | BuildLoopNest(1); |
| 489 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 490 | k_header->AddInput(constant7_); |
| 491 | |
| 492 | HInstruction* add1 = InsertInstruction( |
| 493 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, k_header), 0); |
| 494 | HInstruction* add2 = InsertInstruction( |
| 495 | new (&allocator_) HAdd(Primitive::kPrimInt, add1, k_header), 0); |
| 496 | HInstruction* add3 = InsertInstruction( |
| 497 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, basic_[0]), 0); |
| 498 | k_header->AddInput(add3); |
| 499 | PerformInductionVarAnalysis(); |
| 500 | |
| 501 | // Note, only the phi in the cycle and added-derived are classified. |
| 502 | EXPECT_STREQ("poly(sum_lt(((1) * i + (0)):PrimInt) + (7)):PrimInt", |
| 503 | GetInductionInfo(k_header, 0).c_str()); |
| 504 | EXPECT_STREQ("poly(sum_lt((((1) + (1)) * i + (0)):PrimInt) + ((7) + (7))):PrimInt", |
| 505 | GetInductionInfo(add1, 0).c_str()); |
| 506 | EXPECT_STREQ( |
| 507 | "poly(sum_lt(((((1) + (1)) + (1)) * i + (0)):PrimInt) + (((7) + (7)) + (7))):PrimInt", |
| 508 | GetInductionInfo(add2, 0).c_str()); |
| 509 | EXPECT_STREQ("", GetInductionInfo(add3, 0).c_str()); |
| 510 | } |
| 511 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 512 | TEST_F(InductionVarAnalysisTest, FindGeometricMulInduction) { |
| 513 | // Setup: |
| 514 | // k = 1; |
| 515 | // for (int i = 0; i < 100; i++) { |
| 516 | // k = k * 100; // geometric (x 100) |
| 517 | // } |
| 518 | BuildLoopNest(1); |
| 519 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 520 | k_header->AddInput(constant1_); |
| 521 | |
| 522 | HInstruction* mul = InsertInstruction( |
| 523 | new (&allocator_) HMul(Primitive::kPrimInt, k_header, constant100_), 0); |
| 524 | k_header->AddInput(mul); |
| 525 | PerformInductionVarAnalysis(); |
| 526 | |
| 527 | EXPECT_STREQ("geo((1) * 100 ^ i + (0)):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
| 528 | EXPECT_STREQ("geo((100) * 100 ^ i + (0)):PrimInt", GetInductionInfo(mul, 0).c_str()); |
| 529 | } |
| 530 | |
| 531 | TEST_F(InductionVarAnalysisTest, FindGeometricShlInductionAndDerived) { |
| 532 | // Setup: |
| 533 | // k = 1; |
| 534 | // for (int i = 0; i < 100; i++) { |
| 535 | // t = k + 1; |
| 536 | // k = k << 1; // geometric (x 2) |
| 537 | // t = k + 100; |
| 538 | // t = k - 1; |
| 539 | // t = - t; |
| 540 | // t = k * 2; |
| 541 | // t = k << 2; |
| 542 | // } |
| 543 | BuildLoopNest(1); |
| 544 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 545 | k_header->AddInput(constant1_); |
| 546 | |
| 547 | HInstruction* add1 = InsertInstruction( |
| 548 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant1_), 0); |
| 549 | HInstruction* shl1 = InsertInstruction( |
| 550 | new (&allocator_) HShl(Primitive::kPrimInt, k_header, constant1_), 0); |
| 551 | HInstruction* add2 = InsertInstruction( |
| 552 | new (&allocator_) HAdd(Primitive::kPrimInt, shl1, constant100_), 0); |
| 553 | HInstruction* sub = InsertInstruction( |
| 554 | new (&allocator_) HSub(Primitive::kPrimInt, shl1, constant1_), 0); |
| 555 | HInstruction* neg = InsertInstruction( |
| 556 | new (&allocator_) HNeg(Primitive::kPrimInt, sub), 0); |
| 557 | HInstruction* mul = InsertInstruction( |
| 558 | new (&allocator_) HMul(Primitive::kPrimInt, shl1, constant2_), 0); |
| 559 | HInstruction* shl2 = InsertInstruction( |
| 560 | new (&allocator_) HShl(Primitive::kPrimInt, shl1, constant2_), 0); |
| 561 | k_header->AddInput(shl1); |
| 562 | PerformInductionVarAnalysis(); |
| 563 | |
| 564 | EXPECT_STREQ("geo((1) * 2 ^ i + (0)):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
| 565 | EXPECT_STREQ("geo((1) * 2 ^ i + (1)):PrimInt", GetInductionInfo(add1, 0).c_str()); |
| 566 | EXPECT_STREQ("geo((2) * 2 ^ i + (0)):PrimInt", GetInductionInfo(shl1, 0).c_str()); |
| 567 | EXPECT_STREQ("geo((2) * 2 ^ i + (100)):PrimInt", GetInductionInfo(add2, 0).c_str()); |
| 568 | EXPECT_STREQ("geo((2) * 2 ^ i + ((0) - (1))):PrimInt", GetInductionInfo(sub, 0).c_str()); |
| 569 | EXPECT_STREQ("geo(( - (2)) * 2 ^ i + ( - ((0) - (1)))):PrimInt", |
| 570 | GetInductionInfo(neg, 0).c_str()); |
| 571 | EXPECT_STREQ("geo(((2) * (2)) * 2 ^ i + (0)):PrimInt", GetInductionInfo(mul, 0).c_str()); |
| 572 | EXPECT_STREQ("geo(((2) * (4)) * 2 ^ i + (0)):PrimInt", GetInductionInfo(shl2, 0).c_str()); |
| 573 | } |
| 574 | |
| 575 | TEST_F(InductionVarAnalysisTest, FindGeometricDivInductionAndDerived) { |
| 576 | // Setup: |
| 577 | // k = 1; |
| 578 | // for (int i = 0; i < 100; i++) { |
| 579 | // t = k + 100; |
| 580 | // t = k - 1; |
| 581 | // t = - t |
| 582 | // t = k * 2; |
| 583 | // t = k << 2; |
| 584 | // k = k / 100; // geometric (/ 100) |
| 585 | // } |
| 586 | BuildLoopNest(1); |
| 587 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 588 | k_header->AddInput(constant1_); |
| 589 | |
| 590 | HInstruction* add = InsertInstruction( |
| 591 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant100_), 0); |
| 592 | HInstruction* sub = InsertInstruction( |
| 593 | new (&allocator_) HSub(Primitive::kPrimInt, k_header, constant1_), 0); |
| 594 | HInstruction* neg = InsertInstruction( |
| 595 | new (&allocator_) HNeg(Primitive::kPrimInt, sub), 0); |
| 596 | HInstruction* mul = InsertInstruction( |
| 597 | new (&allocator_) HMul(Primitive::kPrimInt, k_header, constant2_), 0); |
| 598 | HInstruction* shl = InsertInstruction( |
| 599 | new (&allocator_) HShl(Primitive::kPrimInt, k_header, constant2_), 0); |
| 600 | HInstruction* div = InsertInstruction( |
| 601 | new (&allocator_) HDiv(Primitive::kPrimInt, k_header, constant100_, kNoDexPc), 0); |
| 602 | k_header->AddInput(div); |
| 603 | PerformInductionVarAnalysis(); |
| 604 | |
| 605 | // Note, only the phi in the cycle and direct additive derived are classified. |
| 606 | EXPECT_STREQ("geo((1) * 100 ^ -i + (0)):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
| 607 | EXPECT_STREQ("geo((1) * 100 ^ -i + (100)):PrimInt", GetInductionInfo(add, 0).c_str()); |
| 608 | EXPECT_STREQ("geo((1) * 100 ^ -i + ((0) - (1))):PrimInt", GetInductionInfo(sub, 0).c_str()); |
| 609 | EXPECT_STREQ("", GetInductionInfo(neg, 0).c_str()); |
| 610 | EXPECT_STREQ("", GetInductionInfo(mul, 0).c_str()); |
| 611 | EXPECT_STREQ("", GetInductionInfo(shl, 0).c_str()); |
| 612 | EXPECT_STREQ("", GetInductionInfo(div, 0).c_str()); |
| 613 | } |
| 614 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 615 | TEST_F(InductionVarAnalysisTest, FindRemWrapAroundInductionAndDerived) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 616 | // Setup: |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 617 | // k = 100; |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 618 | // for (int i = 0; i < 100; i++) { |
| 619 | // t = k + 100; |
| 620 | // t = k - 1; |
| 621 | // t = -t |
| 622 | // t = k * 2; |
| 623 | // t = k << 2; |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 624 | // k = k % 7; |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 625 | // } |
| 626 | BuildLoopNest(1); |
| 627 | HPhi* k_header = InsertLoopPhi(0, 0); |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 628 | k_header->AddInput(constant100_); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 629 | |
| 630 | HInstruction* add = InsertInstruction( |
| 631 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant100_), 0); |
| 632 | HInstruction* sub = InsertInstruction( |
| 633 | new (&allocator_) HSub(Primitive::kPrimInt, k_header, constant1_), 0); |
| 634 | HInstruction* neg = InsertInstruction( |
| 635 | new (&allocator_) HNeg(Primitive::kPrimInt, sub), 0); |
| 636 | HInstruction* mul = InsertInstruction( |
| 637 | new (&allocator_) HMul(Primitive::kPrimInt, k_header, constant2_), 0); |
| 638 | HInstruction* shl = InsertInstruction( |
| 639 | new (&allocator_) HShl(Primitive::kPrimInt, k_header, constant2_), 0); |
| 640 | HInstruction* rem = InsertInstruction( |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 641 | new (&allocator_) HRem(Primitive::kPrimInt, k_header, constant7_, kNoDexPc), 0); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 642 | k_header->AddInput(rem); |
| 643 | PerformInductionVarAnalysis(); |
| 644 | |
Aart Bik | df7822e | 2016-12-06 10:05:30 -0800 | [diff] [blame^] | 645 | // Note, only the phi in the cycle and derived are classified. |
| 646 | EXPECT_STREQ("wrap((100), ((100) % (7))):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
| 647 | EXPECT_STREQ("wrap(((100) + (100)), (((100) % (7)) + (100))):PrimInt", GetInductionInfo(add, 0).c_str()); |
| 648 | EXPECT_STREQ("wrap(((100) - (1)), (((100) % (7)) - (1))):PrimInt", GetInductionInfo(sub, 0).c_str()); |
| 649 | EXPECT_STREQ("wrap(( - ((100) - (1))), ( - (((100) % (7)) - (1)))):PrimInt", GetInductionInfo(neg, 0).c_str()); |
| 650 | EXPECT_STREQ("wrap(((100) * (2)), (((100) % (7)) * (2))):PrimInt", GetInductionInfo(mul, 0).c_str()); |
| 651 | EXPECT_STREQ("wrap(((100) * (4)), (((100) % (7)) * (4))):PrimInt", GetInductionInfo(shl, 0).c_str()); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 652 | EXPECT_STREQ("", GetInductionInfo(rem, 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 653 | } |
| 654 | |
| 655 | TEST_F(InductionVarAnalysisTest, FindFirstOrderWrapAroundInduction) { |
| 656 | // Setup: |
| 657 | // k = 0; |
| 658 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 659 | // a[k] = 0; |
| 660 | // k = 100 - i; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 661 | // } |
| 662 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 663 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 664 | k_header->AddInput(constant0_); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 665 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 666 | HInstruction* store = InsertArrayStore(k_header, 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 667 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 668 | new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0]), 0); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 669 | k_header->AddInput(sub); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 670 | PerformInductionVarAnalysis(); |
| 671 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 672 | EXPECT_STREQ("wrap((0), (( - (1)) * i + (100)):PrimInt):PrimInt", |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 673 | GetInductionInfo(k_header, 0).c_str()); |
| 674 | EXPECT_STREQ("wrap((0), (( - (1)) * i + (100)):PrimInt):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 675 | GetInductionInfo(store->InputAt(1), 0).c_str()); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 676 | EXPECT_STREQ("(( - (1)) * i + (100)):PrimInt", GetInductionInfo(sub, 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 677 | } |
| 678 | |
| 679 | TEST_F(InductionVarAnalysisTest, FindSecondOrderWrapAroundInduction) { |
| 680 | // Setup: |
| 681 | // k = 0; |
| 682 | // t = 100; |
| 683 | // for (int i = 0; i < 100; i++) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 684 | // a[k] = 0; |
| 685 | // k = t; |
| 686 | // t = 100 - i; |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 687 | // } |
| 688 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 689 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 690 | k_header->AddInput(constant0_); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 691 | HPhi* t = InsertLoopPhi(1, 0); |
| 692 | t->AddInput(constant100_); |
| 693 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 694 | HInstruction* store = InsertArrayStore(k_header, 0); |
| 695 | k_header->AddInput(t); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 696 | HInstruction* sub = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 697 | new (&allocator_) HSub(Primitive::kPrimInt, constant100_, basic_[0], 0), 0); |
| 698 | t->AddInput(sub); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 699 | PerformInductionVarAnalysis(); |
| 700 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 701 | EXPECT_STREQ("wrap((0), wrap((100), (( - (1)) * i + (100)):PrimInt):PrimInt):PrimInt", |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 702 | GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 703 | } |
| 704 | |
| 705 | TEST_F(InductionVarAnalysisTest, FindWrapAroundDerivedInduction) { |
| 706 | // Setup: |
| 707 | // k = 0; |
| 708 | // for (int i = 0; i < 100; i++) { |
| 709 | // t = k + 100; |
| 710 | // t = k - 100; |
| 711 | // t = k * 100; |
| 712 | // t = k << 1; |
| 713 | // t = - k; |
| 714 | // k = i << 1; |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 715 | // t = - k; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 716 | // } |
| 717 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 718 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 719 | k_header->AddInput(constant0_); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 720 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 721 | HInstruction* add = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 722 | new (&allocator_) HAdd(Primitive::kPrimInt, k_header, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 723 | HInstruction* sub = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 724 | new (&allocator_) HSub(Primitive::kPrimInt, k_header, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 725 | HInstruction* mul = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 726 | new (&allocator_) HMul(Primitive::kPrimInt, k_header, constant100_), 0); |
| 727 | HInstruction* shl1 = InsertInstruction( |
| 728 | new (&allocator_) HShl(Primitive::kPrimInt, k_header, constant1_), 0); |
| 729 | HInstruction* neg1 = InsertInstruction( |
| 730 | new (&allocator_) HNeg(Primitive::kPrimInt, k_header), 0); |
| 731 | HInstruction* shl2 = InsertInstruction( |
| 732 | new (&allocator_) HShl(Primitive::kPrimInt, basic_[0], constant1_), 0); |
| 733 | HInstruction* neg2 = InsertInstruction( |
| 734 | new (&allocator_) HNeg(Primitive::kPrimInt, shl2), 0); |
| 735 | k_header->AddInput(shl2); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 736 | PerformInductionVarAnalysis(); |
| 737 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 738 | EXPECT_STREQ("wrap((100), ((2) * i + (100)):PrimInt):PrimInt", |
| 739 | GetInductionInfo(add, 0).c_str()); |
| 740 | EXPECT_STREQ("wrap(((0) - (100)), ((2) * i + ((0) - (100))):PrimInt):PrimInt", |
| 741 | GetInductionInfo(sub, 0).c_str()); |
| 742 | EXPECT_STREQ("wrap((0), (((2) * (100)) * i + (0)):PrimInt):PrimInt", |
| 743 | GetInductionInfo(mul, 0).c_str()); |
| 744 | EXPECT_STREQ("wrap((0), (((2) * (2)) * i + (0)):PrimInt):PrimInt", |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 745 | GetInductionInfo(shl1, 0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 746 | EXPECT_STREQ("wrap((0), (( - (2)) * i + (0)):PrimInt):PrimInt", |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 747 | GetInductionInfo(neg1, 0).c_str()); |
| 748 | EXPECT_STREQ("((2) * i + (0)):PrimInt", GetInductionInfo(shl2, 0).c_str()); |
| 749 | EXPECT_STREQ("(( - (2)) * i + (0)):PrimInt", GetInductionInfo(neg2, 0).c_str()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 750 | } |
| 751 | |
| 752 | TEST_F(InductionVarAnalysisTest, FindPeriodicInduction) { |
| 753 | // Setup: |
| 754 | // k = 0; |
| 755 | // t = 100; |
| 756 | // for (int i = 0; i < 100; i++) { |
| 757 | // a[k] = 0; |
| 758 | // a[t] = 0; |
| 759 | // // Swap t <-> k. |
| 760 | // d = t; |
| 761 | // t = k; |
| 762 | // k = d; |
| 763 | // } |
| 764 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 765 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 766 | k_header->AddInput(constant0_); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 767 | HPhi* t = InsertLoopPhi(1, 0); |
| 768 | t->AddInput(constant100_); |
| 769 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 770 | HInstruction* store1 = InsertArrayStore(k_header, 0); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 771 | HInstruction* store2 = InsertArrayStore(t, 0); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 772 | k_header->AddInput(t); |
| 773 | t->AddInput(k_header); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 774 | PerformInductionVarAnalysis(); |
| 775 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 776 | EXPECT_STREQ("periodic((0), (100)):PrimInt", GetInductionInfo(store1->InputAt(1), 0).c_str()); |
| 777 | 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] | 778 | } |
| 779 | |
| 780 | TEST_F(InductionVarAnalysisTest, FindIdiomaticPeriodicInduction) { |
| 781 | // Setup: |
| 782 | // k = 0; |
| 783 | // for (int i = 0; i < 100; i++) { |
| 784 | // a[k] = 0; |
| 785 | // k = 1 - k; |
| 786 | // } |
| 787 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 788 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 789 | k_header->AddInput(constant0_); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 790 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 791 | HInstruction* store = InsertArrayStore(k_header, 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 792 | HInstruction* sub = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 793 | new (&allocator_) HSub(Primitive::kPrimInt, constant1_, k_header), 0); |
| 794 | k_header->AddInput(sub); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 795 | PerformInductionVarAnalysis(); |
| 796 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 797 | EXPECT_STREQ("periodic((0), (1)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 798 | EXPECT_STREQ("periodic((1), (0)):PrimInt", GetInductionInfo(sub, 0).c_str()); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 799 | } |
| 800 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 801 | TEST_F(InductionVarAnalysisTest, FindXorPeriodicInduction) { |
| 802 | // Setup: |
| 803 | // k = 0; |
| 804 | // for (int i = 0; i < 100; i++) { |
| 805 | // a[k] = 0; |
| 806 | // k = k ^ 1; |
| 807 | // } |
| 808 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 809 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 810 | k_header->AddInput(constant0_); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 811 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 812 | HInstruction* store = InsertArrayStore(k_header, 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 813 | HInstruction* x = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 814 | new (&allocator_) HXor(Primitive::kPrimInt, k_header, constant1_), 0); |
| 815 | k_header->AddInput(x); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 816 | PerformInductionVarAnalysis(); |
| 817 | |
| 818 | EXPECT_STREQ("periodic((0), (1)):PrimInt", GetInductionInfo(store->InputAt(1), 0).c_str()); |
| 819 | EXPECT_STREQ("periodic((1), (0)):PrimInt", GetInductionInfo(x, 0).c_str()); |
| 820 | } |
| 821 | |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 822 | TEST_F(InductionVarAnalysisTest, FindXorConstantLeftPeriodicInduction) { |
| 823 | // Setup: |
| 824 | // k = 1; |
| 825 | // for (int i = 0; i < 100; i++) { |
| 826 | // k = 1 ^ k; |
| 827 | // } |
| 828 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 829 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 830 | k_header->AddInput(constant1_); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 831 | |
| 832 | HInstruction* x = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 833 | new (&allocator_) HXor(Primitive::kPrimInt, constant1_, k_header), 0); |
| 834 | k_header->AddInput(x); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 835 | PerformInductionVarAnalysis(); |
| 836 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 837 | EXPECT_STREQ("periodic((1), ((1) ^ (1))):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 838 | EXPECT_STREQ("periodic(((1) ^ (1)), (1)):PrimInt", GetInductionInfo(x, 0).c_str()); |
| 839 | } |
| 840 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 841 | TEST_F(InductionVarAnalysisTest, FindXor100PeriodicInduction) { |
| 842 | // Setup: |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 843 | // k = 1; |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 844 | // for (int i = 0; i < 100; i++) { |
| 845 | // k = k ^ 100; |
| 846 | // } |
| 847 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 848 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 849 | k_header->AddInput(constant1_); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 850 | |
| 851 | HInstruction* x = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 852 | new (&allocator_) HXor(Primitive::kPrimInt, k_header, constant100_), 0); |
| 853 | k_header->AddInput(x); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 854 | PerformInductionVarAnalysis(); |
| 855 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 856 | EXPECT_STREQ("periodic((1), ((1) ^ (100))):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 857 | EXPECT_STREQ("periodic(((1) ^ (100)), (1)):PrimInt", GetInductionInfo(x, 0).c_str()); |
| 858 | } |
| 859 | |
| 860 | TEST_F(InductionVarAnalysisTest, FindBooleanEqPeriodicInduction) { |
| 861 | // Setup: |
| 862 | // k = 0; |
| 863 | // for (int i = 0; i < 100; i++) { |
| 864 | // k = (k == 0); |
| 865 | // } |
| 866 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 867 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 868 | k_header->AddInput(constant0_); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 869 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 870 | HInstruction* x = InsertInstruction(new (&allocator_) HEqual(k_header, constant0_), 0); |
| 871 | k_header->AddInput(x); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 872 | PerformInductionVarAnalysis(); |
| 873 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 874 | EXPECT_STREQ("periodic((0), (1)):PrimBoolean", GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 875 | EXPECT_STREQ("periodic((1), (0)):PrimBoolean", GetInductionInfo(x, 0).c_str()); |
| 876 | } |
| 877 | |
| 878 | TEST_F(InductionVarAnalysisTest, FindBooleanEqConstantLeftPeriodicInduction) { |
| 879 | // Setup: |
| 880 | // k = 0; |
| 881 | // for (int i = 0; i < 100; i++) { |
| 882 | // k = (0 == k); |
| 883 | // } |
| 884 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 885 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 886 | k_header->AddInput(constant0_); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 887 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 888 | HInstruction* x = InsertInstruction(new (&allocator_) HEqual(constant0_, k_header), 0); |
| 889 | k_header->AddInput(x); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 890 | PerformInductionVarAnalysis(); |
| 891 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 892 | EXPECT_STREQ("periodic((0), (1)):PrimBoolean", GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 893 | EXPECT_STREQ("periodic((1), (0)):PrimBoolean", GetInductionInfo(x, 0).c_str()); |
| 894 | } |
| 895 | |
| 896 | TEST_F(InductionVarAnalysisTest, FindBooleanNePeriodicInduction) { |
| 897 | // Setup: |
| 898 | // k = 0; |
| 899 | // for (int i = 0; i < 100; i++) { |
| 900 | // k = (k != 1); |
| 901 | // } |
| 902 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 903 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 904 | k_header->AddInput(constant0_); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 905 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 906 | HInstruction* x = InsertInstruction(new (&allocator_) HNotEqual(k_header, constant1_), 0); |
| 907 | k_header->AddInput(x); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 908 | PerformInductionVarAnalysis(); |
| 909 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 910 | EXPECT_STREQ("periodic((0), (1)):PrimBoolean", GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 911 | EXPECT_STREQ("periodic((1), (0)):PrimBoolean", GetInductionInfo(x, 0).c_str()); |
| 912 | } |
| 913 | |
| 914 | TEST_F(InductionVarAnalysisTest, FindBooleanNeConstantLeftPeriodicInduction) { |
| 915 | // Setup: |
| 916 | // k = 0; |
| 917 | // for (int i = 0; i < 100; i++) { |
| 918 | // k = (1 != k); |
| 919 | // } |
| 920 | BuildLoopNest(1); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 921 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 922 | k_header->AddInput(constant0_); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 923 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 924 | HInstruction* x = InsertInstruction(new (&allocator_) HNotEqual(constant1_, k_header), 0); |
| 925 | k_header->AddInput(x); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 926 | PerformInductionVarAnalysis(); |
| 927 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 928 | EXPECT_STREQ("periodic((0), (1)):PrimBoolean", GetInductionInfo(k_header, 0).c_str()); |
Aart Bik | 639cc8c | 2016-10-18 13:03:31 -0700 | [diff] [blame] | 929 | EXPECT_STREQ("periodic((1), (0)):PrimBoolean", GetInductionInfo(x, 0).c_str()); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 930 | } |
| 931 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 932 | TEST_F(InductionVarAnalysisTest, FindDerivedPeriodicInduction) { |
| 933 | // Setup: |
| 934 | // k = 0; |
| 935 | // for (int i = 0; i < 100; i++) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 936 | // t = - k; |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 937 | // k = 1 - k; |
| 938 | // t = k + 100; |
| 939 | // t = k - 100; |
| 940 | // t = k * 100; |
| 941 | // t = k << 1; |
| 942 | // t = - k; |
| 943 | // } |
| 944 | BuildLoopNest(1); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 945 | HPhi* k_header = InsertLoopPhi(0, 0); |
| 946 | k_header->AddInput(constant0_); |
| 947 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 948 | HInstruction* neg1 = InsertInstruction( |
| 949 | new (&allocator_) HNeg(Primitive::kPrimInt, k_header), 0); |
| 950 | HInstruction* idiom = InsertInstruction( |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 951 | new (&allocator_) HSub(Primitive::kPrimInt, constant1_, k_header), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 952 | HInstruction* add = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 953 | new (&allocator_) HAdd(Primitive::kPrimInt, idiom, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 954 | HInstruction* sub = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 955 | new (&allocator_) HSub(Primitive::kPrimInt, idiom, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 956 | HInstruction* mul = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 957 | new (&allocator_) HMul(Primitive::kPrimInt, idiom, constant100_), 0); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 958 | HInstruction* shl = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 959 | new (&allocator_) HShl(Primitive::kPrimInt, idiom, constant1_), 0); |
| 960 | HInstruction* neg2 = InsertInstruction( |
| 961 | new (&allocator_) HNeg(Primitive::kPrimInt, idiom), 0); |
| 962 | k_header->AddInput(idiom); |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 963 | PerformInductionVarAnalysis(); |
| 964 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 965 | EXPECT_STREQ("periodic((0), (1)):PrimInt", GetInductionInfo(k_header, 0).c_str()); |
| 966 | EXPECT_STREQ("periodic((0), ( - (1))):PrimInt", GetInductionInfo(neg1, 0).c_str()); |
| 967 | EXPECT_STREQ("periodic((1), (0)):PrimInt", GetInductionInfo(idiom, 0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 968 | EXPECT_STREQ("periodic(((1) + (100)), (100)):PrimInt", GetInductionInfo(add, 0).c_str()); |
| 969 | EXPECT_STREQ("periodic(((1) - (100)), ((0) - (100))):PrimInt", GetInductionInfo(sub, 0).c_str()); |
| 970 | EXPECT_STREQ("periodic((100), (0)):PrimInt", GetInductionInfo(mul, 0).c_str()); |
| 971 | EXPECT_STREQ("periodic((2), (0)):PrimInt", GetInductionInfo(shl, 0).c_str()); |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 972 | EXPECT_STREQ("periodic(( - (1)), (0)):PrimInt", GetInductionInfo(neg2, 0).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 973 | } |
| 974 | |
| 975 | TEST_F(InductionVarAnalysisTest, FindDeepLoopInduction) { |
| 976 | // Setup: |
| 977 | // k = 0; |
| 978 | // for (int i_0 = 0; i_0 < 100; i_0++) { |
| 979 | // .. |
| 980 | // for (int i_9 = 0; i_9 < 100; i_9++) { |
| 981 | // k = 1 + k; |
| 982 | // a[k] = 0; |
| 983 | // } |
| 984 | // .. |
| 985 | // } |
| 986 | BuildLoopNest(10); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 987 | |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 988 | HPhi* k_header[10]; |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 989 | for (int d = 0; d < 10; d++) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 990 | k_header[d] = InsertLoopPhi(0, d); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 991 | } |
| 992 | |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 993 | HInstruction* inc = InsertInstruction( |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 994 | new (&allocator_) HAdd(Primitive::kPrimInt, constant1_, k_header[9]), 9); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 995 | HInstruction* store = InsertArrayStore(inc, 9); |
| 996 | |
| 997 | for (int d = 0; d < 10; d++) { |
Aart Bik | c071a01 | 2016-12-01 10:22:31 -0800 | [diff] [blame] | 998 | k_header[d]->AddInput((d != 0) ? k_header[d - 1] : constant0_); |
| 999 | k_header[d]->AddInput((d != 9) ? k_header[d + 1] : inc); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 1000 | } |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1001 | PerformInductionVarAnalysis(); |
| 1002 | |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1003 | // Avoid exact phi number, since that depends on the SSA building phase. |
| 1004 | std::regex r("\\(\\(1\\) \\* i \\+ " |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1005 | "\\(\\(1\\) \\+ \\(\\d+:Phi\\)\\)\\):PrimInt"); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1006 | |
| 1007 | for (int d = 0; d < 10; d++) { |
| 1008 | if (d == 9) { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1009 | EXPECT_TRUE(std::regex_match(GetInductionInfo(store->InputAt(1), d), r)); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1010 | } else { |
Aart Bik | e609b7c | 2015-08-27 13:46:58 -0700 | [diff] [blame] | 1011 | EXPECT_STREQ("", GetInductionInfo(store->InputAt(1), d).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1012 | } |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1013 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(increment_[d], d).c_str()); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 1014 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1015 | EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))", GetTripCount(d).c_str()); |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1016 | } |
| 1017 | } |
| 1018 | |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 1019 | TEST_F(InductionVarAnalysisTest, ByteInductionIntLoopControl) { |
| 1020 | // Setup: |
| 1021 | // for (int i = 0; i < 100; i++) { |
| 1022 | // k = (byte) i; |
| 1023 | // a[k] = 0; |
| 1024 | // a[i] = 0; |
| 1025 | // } |
| 1026 | BuildLoopNest(1); |
Aart Bik | 7dc9693 | 2016-10-12 10:01:05 -0700 | [diff] [blame] | 1027 | HInstruction* conv = InsertInstruction( |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 1028 | new (&allocator_) HTypeConversion(Primitive::kPrimByte, basic_[0], -1), 0); |
| 1029 | HInstruction* store1 = InsertArrayStore(conv, 0); |
| 1030 | HInstruction* store2 = InsertArrayStore(basic_[0], 0); |
| 1031 | PerformInductionVarAnalysis(); |
| 1032 | |
| 1033 | // Regular int induction (i) is "transferred" over conversion into byte induction (k). |
| 1034 | EXPECT_STREQ("((1) * i + (0)):PrimByte", GetInductionInfo(store1->InputAt(1), 0).c_str()); |
| 1035 | EXPECT_STREQ("((1) * i + (0)):PrimInt", GetInductionInfo(store2->InputAt(1), 0).c_str()); |
| 1036 | EXPECT_STREQ("((1) * i + (1)):PrimInt", GetInductionInfo(increment_[0], 0).c_str()); |
| 1037 | |
| 1038 | // Type matters! |
| 1039 | EXPECT_FALSE(HaveSameInduction(store1->InputAt(1), store2->InputAt(1))); |
| 1040 | |
| 1041 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1042 | EXPECT_STREQ("((100) (TC-loop) ((0) < (100)))", GetTripCount(0).c_str()); |
Aart Bik | 7829691 | 2016-03-25 13:14:53 -0700 | [diff] [blame] | 1043 | } |
| 1044 | |
Aart Bik | cc42be0 | 2016-10-20 16:14:16 -0700 | [diff] [blame] | 1045 | TEST_F(InductionVarAnalysisTest, ByteInductionDerivedIntLoopControl) { |
| 1046 | // Setup: |
| 1047 | // for (int i = 0; i < 100; i++) { |
| 1048 | // k = (byte) i; |
| 1049 | // a[k] = 0; |
| 1050 | // k = k + 1 |
| 1051 | // a[k] = 0; |
| 1052 | // } |
| 1053 | BuildLoopNest(1); |
| 1054 | HInstruction* conv = InsertInstruction( |
| 1055 | new (&allocator_) HTypeConversion(Primitive::kPrimByte, basic_[0], -1), 0); |
| 1056 | HInstruction* store1 = InsertArrayStore(conv, 0); |
| 1057 | HInstruction* add = InsertInstruction( |
| 1058 | new (&allocator_) HAdd(Primitive::kPrimInt, conv, constant1_), 0); |
| 1059 | HInstruction* store2 = InsertArrayStore(add, 0); |
| 1060 | |
| 1061 | PerformInductionVarAnalysis(); |
| 1062 | |
| 1063 | // Byte induction (k) is "transferred" over conversion into addition (k + 1). |
| 1064 | // This means only values within byte range can be trusted (even though |
| 1065 | // addition can jump out of the range of course). |
| 1066 | EXPECT_STREQ("((1) * i + (0)):PrimByte", GetInductionInfo(store1->InputAt(1), 0).c_str()); |
| 1067 | EXPECT_STREQ("((1) * i + (1)):PrimByte", GetInductionInfo(store2->InputAt(1), 0).c_str()); |
| 1068 | } |
| 1069 | |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1070 | TEST_F(InductionVarAnalysisTest, ByteLoopControl1) { |
| 1071 | // Setup: |
| 1072 | // for (byte i = -128; i < 127; i++) { // just fits! |
| 1073 | // } |
| 1074 | BuildLoopNest(1); |
| 1075 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-128), 0); |
| 1076 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 1077 | ifs->ReplaceInput(graph_->GetIntConstant(127), 1); |
| 1078 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimByte, increment_[0], -1); |
| 1079 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 1080 | basic_[0]->ReplaceInput(conv, 1); |
| 1081 | PerformInductionVarAnalysis(); |
| 1082 | |
| 1083 | EXPECT_STREQ("((1) * i + ((-128) + (1))):PrimByte", GetInductionInfo(increment_[0], 0).c_str()); |
| 1084 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1085 | EXPECT_STREQ("(((127) - (-128)) (TC-loop) ((-128) < (127)))", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1086 | } |
| 1087 | |
| 1088 | TEST_F(InductionVarAnalysisTest, ByteLoopControl2) { |
| 1089 | // Setup: |
| 1090 | // for (byte i = -128; i < 128; i++) { // infinite loop! |
| 1091 | // } |
| 1092 | BuildLoopNest(1); |
| 1093 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-128), 0); |
| 1094 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 1095 | ifs->ReplaceInput(graph_->GetIntConstant(128), 1); |
| 1096 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimByte, increment_[0], -1); |
| 1097 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 1098 | basic_[0]->ReplaceInput(conv, 1); |
| 1099 | PerformInductionVarAnalysis(); |
| 1100 | |
| 1101 | EXPECT_STREQ("((1) * i + ((-128) + (1))):PrimByte", GetInductionInfo(increment_[0], 0).c_str()); |
| 1102 | // Trip-count undefined. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1103 | EXPECT_STREQ("", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1104 | } |
| 1105 | |
| 1106 | TEST_F(InductionVarAnalysisTest, ShortLoopControl1) { |
| 1107 | // Setup: |
| 1108 | // for (short i = -32768; i < 32767; i++) { // just fits! |
| 1109 | // } |
| 1110 | BuildLoopNest(1); |
| 1111 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-32768), 0); |
| 1112 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 1113 | ifs->ReplaceInput(graph_->GetIntConstant(32767), 1); |
| 1114 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimShort, increment_[0], -1); |
| 1115 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 1116 | basic_[0]->ReplaceInput(conv, 1); |
| 1117 | PerformInductionVarAnalysis(); |
| 1118 | |
| 1119 | EXPECT_STREQ("((1) * i + ((-32768) + (1))):PrimShort", |
| 1120 | GetInductionInfo(increment_[0], 0).c_str()); |
| 1121 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1122 | EXPECT_STREQ("(((32767) - (-32768)) (TC-loop) ((-32768) < (32767)))", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1123 | } |
| 1124 | |
| 1125 | TEST_F(InductionVarAnalysisTest, ShortLoopControl2) { |
| 1126 | // Setup: |
| 1127 | // for (short i = -32768; i < 32768; i++) { // infinite loop! |
| 1128 | // } |
| 1129 | BuildLoopNest(1); |
| 1130 | basic_[0]->ReplaceInput(graph_->GetIntConstant(-32768), 0); |
| 1131 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 1132 | ifs->ReplaceInput(graph_->GetIntConstant(32768), 1); |
| 1133 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimShort, increment_[0], -1); |
| 1134 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 1135 | basic_[0]->ReplaceInput(conv, 1); |
| 1136 | PerformInductionVarAnalysis(); |
| 1137 | |
| 1138 | EXPECT_STREQ("((1) * i + ((-32768) + (1))):PrimShort", |
| 1139 | GetInductionInfo(increment_[0], 0).c_str()); |
| 1140 | // Trip-count undefined. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1141 | EXPECT_STREQ("", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1142 | } |
| 1143 | |
| 1144 | TEST_F(InductionVarAnalysisTest, CharLoopControl1) { |
| 1145 | // Setup: |
| 1146 | // for (char i = 0; i < 65535; i++) { // just fits! |
| 1147 | // } |
| 1148 | BuildLoopNest(1); |
| 1149 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 1150 | ifs->ReplaceInput(graph_->GetIntConstant(65535), 1); |
| 1151 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimChar, increment_[0], -1); |
| 1152 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 1153 | basic_[0]->ReplaceInput(conv, 1); |
| 1154 | PerformInductionVarAnalysis(); |
| 1155 | |
| 1156 | EXPECT_STREQ("((1) * i + (1)):PrimChar", GetInductionInfo(increment_[0], 0).c_str()); |
| 1157 | // Trip-count. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1158 | EXPECT_STREQ("((65535) (TC-loop) ((0) < (65535)))", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1159 | } |
| 1160 | |
| 1161 | TEST_F(InductionVarAnalysisTest, CharLoopControl2) { |
| 1162 | // Setup: |
| 1163 | // for (char i = 0; i < 65536; i++) { // infinite loop! |
| 1164 | // } |
| 1165 | BuildLoopNest(1); |
| 1166 | HInstruction* ifs = loop_header_[0]->GetLastInstruction()->GetPrevious(); |
| 1167 | ifs->ReplaceInput(graph_->GetIntConstant(65536), 1); |
| 1168 | HInstruction* conv = new(&allocator_) HTypeConversion(Primitive::kPrimChar, increment_[0], -1); |
| 1169 | loop_body_[0]->InsertInstructionBefore(conv, increment_[0]->GetNext()); |
| 1170 | basic_[0]->ReplaceInput(conv, 1); |
| 1171 | PerformInductionVarAnalysis(); |
| 1172 | |
| 1173 | EXPECT_STREQ("((1) * i + (1)):PrimChar", GetInductionInfo(increment_[0], 0).c_str()); |
| 1174 | // Trip-count undefined. |
Aart Bik | 009cace | 2016-09-16 10:15:19 -0700 | [diff] [blame] | 1175 | EXPECT_STREQ("", GetTripCount(0).c_str()); |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 1176 | } |
| 1177 | |
Aart Bik | 30efb4e | 2015-07-30 12:14:31 -0700 | [diff] [blame] | 1178 | } // namespace art |