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