Aart Bik | d14c595 | 2015-09-08 15:25:15 -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 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 17 | #include "base/arena_allocator.h" |
| 18 | #include "builder.h" |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 19 | #include "induction_var_analysis.h" |
| 20 | #include "induction_var_range.h" |
| 21 | #include "nodes.h" |
| 22 | #include "optimizing_unit_test.h" |
| 23 | |
| 24 | namespace art { |
| 25 | |
| 26 | using Value = InductionVarRange::Value; |
| 27 | |
| 28 | /** |
| 29 | * Fixture class for the InductionVarRange tests. |
| 30 | */ |
David Brazdil | 4833f5a | 2015-12-16 10:37:39 +0000 | [diff] [blame] | 31 | class InductionVarRangeTest : public CommonCompilerTest { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 32 | public: |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 33 | InductionVarRangeTest() |
| 34 | : pool_(), |
| 35 | allocator_(&pool_), |
| 36 | graph_(CreateGraph(&allocator_)), |
| 37 | iva_(new (&allocator_) HInductionVarAnalysis(graph_)), |
| 38 | range_(iva_) { |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 39 | BuildGraph(); |
| 40 | } |
| 41 | |
| 42 | ~InductionVarRangeTest() { } |
| 43 | |
| 44 | void ExpectEqual(Value v1, Value v2) { |
| 45 | EXPECT_EQ(v1.instruction, v2.instruction); |
| 46 | EXPECT_EQ(v1.a_constant, v2.a_constant); |
| 47 | EXPECT_EQ(v1.b_constant, v2.b_constant); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 48 | EXPECT_EQ(v1.is_known, v2.is_known); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 49 | } |
| 50 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 51 | // |
| 52 | // Construction methods. |
| 53 | // |
| 54 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 55 | /** Constructs bare minimum graph. */ |
| 56 | void BuildGraph() { |
| 57 | graph_->SetNumberOfVRegs(1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 58 | entry_block_ = new (&allocator_) HBasicBlock(graph_); |
| 59 | exit_block_ = new (&allocator_) HBasicBlock(graph_); |
| 60 | graph_->AddBlock(entry_block_); |
| 61 | graph_->AddBlock(exit_block_); |
| 62 | graph_->SetEntryBlock(entry_block_); |
| 63 | graph_->SetExitBlock(exit_block_); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 64 | // Two parameters. |
| 65 | x_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), 0, 0, Primitive::kPrimInt); |
| 66 | entry_block_->AddInstruction(x_); |
| 67 | y_ = new (&allocator_) HParameterValue(graph_->GetDexFile(), 0, 0, Primitive::kPrimInt); |
| 68 | entry_block_->AddInstruction(y_); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 69 | // Set arbitrary range analysis hint while testing private methods. |
| 70 | SetHint(x_); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 71 | } |
| 72 | |
| 73 | /** Constructs loop with given upper bound. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 74 | void BuildLoop(int32_t lower, HInstruction* upper, int32_t stride) { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 75 | // Control flow. |
| 76 | loop_preheader_ = new (&allocator_) HBasicBlock(graph_); |
| 77 | graph_->AddBlock(loop_preheader_); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 78 | loop_header_ = new (&allocator_) HBasicBlock(graph_); |
| 79 | graph_->AddBlock(loop_header_); |
| 80 | loop_body_ = new (&allocator_) HBasicBlock(graph_); |
| 81 | graph_->AddBlock(loop_body_); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 82 | HBasicBlock* return_block = new (&allocator_) HBasicBlock(graph_); |
| 83 | graph_->AddBlock(return_block); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 84 | entry_block_->AddSuccessor(loop_preheader_); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 85 | loop_preheader_->AddSuccessor(loop_header_); |
| 86 | loop_header_->AddSuccessor(loop_body_); |
| 87 | loop_header_->AddSuccessor(return_block); |
| 88 | loop_body_->AddSuccessor(loop_header_); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 89 | return_block->AddSuccessor(exit_block_); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 90 | // Instructions. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 91 | loop_preheader_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 92 | HPhi* phi = new (&allocator_) HPhi(&allocator_, 0, 0, Primitive::kPrimInt); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 93 | loop_header_->AddPhi(phi); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 94 | phi->AddInput(graph_->GetIntConstant(lower)); // i = l |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 95 | if (stride > 0) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 96 | condition_ = new (&allocator_) HLessThan(phi, upper); // i < u |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 97 | } else { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 98 | condition_ = new (&allocator_) HGreaterThan(phi, upper); // i > u |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 99 | } |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 100 | loop_header_->AddInstruction(condition_); |
| 101 | loop_header_->AddInstruction(new (&allocator_) HIf(condition_)); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 102 | increment_ = new (&allocator_) HAdd(Primitive::kPrimInt, phi, graph_->GetIntConstant(stride)); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 103 | loop_body_->AddInstruction(increment_); // i += s |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 104 | phi->AddInput(increment_); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 105 | loop_body_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 106 | return_block->AddInstruction(new (&allocator_) HReturnVoid()); |
| 107 | exit_block_->AddInstruction(new (&allocator_) HExit()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 110 | /** Constructs SSA and performs induction variable analysis. */ |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 111 | void PerformInductionVarAnalysis() { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 112 | graph_->BuildDominatorTree(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 113 | iva_->Run(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 114 | } |
| 115 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 116 | /** Sets hint. */ |
| 117 | void SetHint(HInstruction* hint) { |
| 118 | range_.chase_hint_ = hint; |
| 119 | } |
| 120 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 121 | /** Constructs an invariant. */ |
| 122 | HInductionVarAnalysis::InductionInfo* CreateInvariant(char opc, |
| 123 | HInductionVarAnalysis::InductionInfo* a, |
| 124 | HInductionVarAnalysis::InductionInfo* b) { |
| 125 | HInductionVarAnalysis::InductionOp op; |
| 126 | switch (opc) { |
| 127 | case '+': op = HInductionVarAnalysis::kAdd; break; |
| 128 | case '-': op = HInductionVarAnalysis::kSub; break; |
| 129 | case 'n': op = HInductionVarAnalysis::kNeg; break; |
| 130 | case '*': op = HInductionVarAnalysis::kMul; break; |
| 131 | case '/': op = HInductionVarAnalysis::kDiv; break; |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 132 | case '<': op = HInductionVarAnalysis::kLT; break; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 133 | default: op = HInductionVarAnalysis::kNop; break; |
| 134 | } |
| 135 | return iva_->CreateInvariantOp(op, a, b); |
| 136 | } |
| 137 | |
| 138 | /** Constructs a fetch. */ |
| 139 | HInductionVarAnalysis::InductionInfo* CreateFetch(HInstruction* fetch) { |
| 140 | return iva_->CreateInvariantFetch(fetch); |
| 141 | } |
| 142 | |
| 143 | /** Constructs a constant. */ |
| 144 | HInductionVarAnalysis::InductionInfo* CreateConst(int32_t c) { |
| 145 | return CreateFetch(graph_->GetIntConstant(c)); |
| 146 | } |
| 147 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 148 | /** Constructs a constant trip-count. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 149 | HInductionVarAnalysis::InductionInfo* CreateTripCount(int32_t tc, bool in_loop, bool safe) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 150 | HInductionVarAnalysis::InductionOp op = HInductionVarAnalysis::kTripCountInBodyUnsafe; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 151 | if (in_loop && safe) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 152 | op = HInductionVarAnalysis::kTripCountInLoop; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 153 | } else if (in_loop) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 154 | op = HInductionVarAnalysis::kTripCountInLoopUnsafe; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 155 | } else if (safe) { |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 156 | op = HInductionVarAnalysis::kTripCountInBody; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 157 | } |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 158 | // Return TC with taken-test 0 < TC. |
| 159 | return iva_->CreateTripCount(op, |
| 160 | CreateConst(tc), |
| 161 | CreateInvariant('<', CreateConst(0), CreateConst(tc)), |
| 162 | Primitive::kPrimInt); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 163 | } |
| 164 | |
| 165 | /** Constructs a linear a * i + b induction. */ |
| 166 | HInductionVarAnalysis::InductionInfo* CreateLinear(int32_t a, int32_t b) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 167 | return iva_->CreateInduction( |
| 168 | HInductionVarAnalysis::kLinear, CreateConst(a), CreateConst(b), Primitive::kPrimInt); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 169 | } |
| 170 | |
| 171 | /** Constructs a range [lo, hi] using a periodic induction. */ |
| 172 | HInductionVarAnalysis::InductionInfo* CreateRange(int32_t lo, int32_t hi) { |
| 173 | return iva_->CreateInduction( |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 174 | HInductionVarAnalysis::kPeriodic, CreateConst(lo), CreateConst(hi), Primitive::kPrimInt); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 177 | /** Constructs a wrap-around induction consisting of a constant, followed info */ |
| 178 | HInductionVarAnalysis::InductionInfo* CreateWrapAround( |
| 179 | int32_t initial, |
| 180 | HInductionVarAnalysis::InductionInfo* info) { |
Aart Bik | 0d345cf | 2016-03-16 10:49:38 -0700 | [diff] [blame] | 181 | return iva_->CreateInduction( |
| 182 | HInductionVarAnalysis::kWrapAround, CreateConst(initial), info, Primitive::kPrimInt); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 183 | } |
| 184 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 185 | /** Constructs a wrap-around induction consisting of a constant, followed by a range. */ |
| 186 | HInductionVarAnalysis::InductionInfo* CreateWrapAround(int32_t initial, int32_t lo, int32_t hi) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 187 | return CreateWrapAround(initial, CreateRange(lo, hi)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 188 | } |
| 189 | |
| 190 | // |
| 191 | // Relay methods. |
| 192 | // |
| 193 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 194 | bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) { |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 195 | int64_t s = 0; |
| 196 | return range_.NeedsTripCount(info, &s); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 200 | return range_.IsBodyTripCount(trip); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 201 | } |
| 202 | |
| 203 | bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 204 | return range_.IsUnsafeTripCount(trip); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 205 | } |
| 206 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 207 | Value GetMin(HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 208 | HInductionVarAnalysis::InductionInfo* trip) { |
| 209 | return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ true); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | Value GetMax(HInductionVarAnalysis::InductionInfo* info, |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 213 | HInductionVarAnalysis::InductionInfo* trip) { |
| 214 | return range_.GetVal(info, trip, /* in_body */ true, /* is_min */ false); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 215 | } |
| 216 | |
| 217 | Value GetMul(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 218 | HInductionVarAnalysis::InductionInfo* info2, |
| 219 | bool is_min) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 220 | return range_.GetMul(info1, info2, nullptr, /* in_body */ true, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 221 | } |
| 222 | |
| 223 | Value GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 224 | HInductionVarAnalysis::InductionInfo* info2, |
| 225 | bool is_min) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 226 | return range_.GetDiv(info1, info2, nullptr, /* in_body */ true, is_min); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 227 | } |
| 228 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 229 | bool IsExact(HInductionVarAnalysis::InductionInfo* info, int64_t* value) { |
| 230 | return range_.IsConstant(info, InductionVarRange::kExact, value); |
| 231 | } |
| 232 | |
| 233 | bool IsAtMost(HInductionVarAnalysis::InductionInfo* info, int64_t* value) { |
| 234 | return range_.IsConstant(info, InductionVarRange::kAtMost, value); |
| 235 | } |
| 236 | |
| 237 | bool IsAtLeast(HInductionVarAnalysis::InductionInfo* info, int64_t* value) { |
| 238 | return range_.IsConstant(info, InductionVarRange::kAtLeast, value); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 239 | } |
| 240 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 241 | Value AddValue(Value v1, Value v2) { return range_.AddValue(v1, v2); } |
| 242 | Value SubValue(Value v1, Value v2) { return range_.SubValue(v1, v2); } |
| 243 | Value MulValue(Value v1, Value v2) { return range_.MulValue(v1, v2); } |
| 244 | Value DivValue(Value v1, Value v2) { return range_.DivValue(v1, v2); } |
| 245 | Value MinValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, true); } |
| 246 | Value MaxValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, false); } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 247 | |
| 248 | // General building fields. |
| 249 | ArenaPool pool_; |
| 250 | ArenaAllocator allocator_; |
| 251 | HGraph* graph_; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 252 | HBasicBlock* entry_block_; |
| 253 | HBasicBlock* exit_block_; |
| 254 | HBasicBlock* loop_preheader_; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 255 | HBasicBlock* loop_header_; |
| 256 | HBasicBlock* loop_body_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 257 | HInductionVarAnalysis* iva_; |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 258 | InductionVarRange range_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 259 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 260 | // Instructions. |
| 261 | HInstruction* condition_; |
| 262 | HInstruction* increment_; |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 263 | HInstruction* x_; |
| 264 | HInstruction* y_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 265 | }; |
| 266 | |
| 267 | // |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 268 | // Tests on private methods. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 269 | // |
| 270 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 271 | TEST_F(InductionVarRangeTest, IsConstant) { |
| 272 | int64_t value; |
| 273 | // Constant. |
| 274 | EXPECT_TRUE(IsExact(CreateConst(12345), &value)); |
| 275 | EXPECT_EQ(12345, value); |
| 276 | EXPECT_TRUE(IsAtMost(CreateConst(12345), &value)); |
| 277 | EXPECT_EQ(12345, value); |
| 278 | EXPECT_TRUE(IsAtLeast(CreateConst(12345), &value)); |
| 279 | EXPECT_EQ(12345, value); |
| 280 | // Constant trivial range. |
| 281 | EXPECT_TRUE(IsExact(CreateRange(111, 111), &value)); |
| 282 | EXPECT_EQ(111, value); |
| 283 | EXPECT_TRUE(IsAtMost(CreateRange(111, 111), &value)); |
| 284 | EXPECT_EQ(111, value); |
| 285 | EXPECT_TRUE(IsAtLeast(CreateRange(111, 111), &value)); |
| 286 | EXPECT_EQ(111, value); |
| 287 | // Constant non-trivial range. |
| 288 | EXPECT_FALSE(IsExact(CreateRange(11, 22), &value)); |
| 289 | EXPECT_TRUE(IsAtMost(CreateRange(11, 22), &value)); |
| 290 | EXPECT_EQ(22, value); |
| 291 | EXPECT_TRUE(IsAtLeast(CreateRange(11, 22), &value)); |
| 292 | EXPECT_EQ(11, value); |
| 293 | // Symbolic. |
| 294 | EXPECT_FALSE(IsExact(CreateFetch(x_), &value)); |
| 295 | EXPECT_FALSE(IsAtMost(CreateFetch(x_), &value)); |
| 296 | EXPECT_FALSE(IsAtLeast(CreateFetch(x_), &value)); |
| 297 | } |
| 298 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 299 | TEST_F(InductionVarRangeTest, TripCountProperties) { |
| 300 | EXPECT_FALSE(NeedsTripCount(nullptr)); |
| 301 | EXPECT_FALSE(NeedsTripCount(CreateConst(1))); |
| 302 | EXPECT_TRUE(NeedsTripCount(CreateLinear(1, 1))); |
| 303 | EXPECT_FALSE(NeedsTripCount(CreateWrapAround(1, 2, 3))); |
| 304 | EXPECT_TRUE(NeedsTripCount(CreateWrapAround(1, CreateLinear(1, 1)))); |
| 305 | |
| 306 | EXPECT_FALSE(IsBodyTripCount(nullptr)); |
| 307 | EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, true))); |
| 308 | EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, false))); |
| 309 | EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, true))); |
| 310 | EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, false))); |
| 311 | |
| 312 | EXPECT_FALSE(IsUnsafeTripCount(nullptr)); |
| 313 | EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, true, true))); |
| 314 | EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, true, false))); |
| 315 | EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, false, true))); |
| 316 | EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, false, false))); |
| 317 | } |
| 318 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 319 | TEST_F(InductionVarRangeTest, GetMinMaxNull) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 320 | ExpectEqual(Value(), GetMin(nullptr, nullptr)); |
| 321 | ExpectEqual(Value(), GetMax(nullptr, nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | TEST_F(InductionVarRangeTest, GetMinMaxAdd) { |
| 325 | ExpectEqual(Value(12), |
| 326 | GetMin(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 327 | ExpectEqual(Value(22), |
| 328 | GetMax(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 329 | ExpectEqual(Value(x_, 1, -20), |
| 330 | GetMin(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 331 | ExpectEqual(Value(x_, 1, -10), |
| 332 | GetMax(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 333 | ExpectEqual(Value(x_, 1, 10), |
| 334 | GetMin(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
| 335 | ExpectEqual(Value(x_, 1, 20), |
| 336 | GetMax(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 337 | ExpectEqual(Value(5), |
| 338 | GetMin(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 339 | ExpectEqual(Value(19), |
| 340 | GetMax(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 341 | } |
| 342 | |
| 343 | TEST_F(InductionVarRangeTest, GetMinMaxSub) { |
| 344 | ExpectEqual(Value(-18), |
| 345 | GetMin(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 346 | ExpectEqual(Value(-8), |
| 347 | GetMax(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 348 | ExpectEqual(Value(x_, 1, 10), |
| 349 | GetMin(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 350 | ExpectEqual(Value(x_, 1, 20), |
| 351 | GetMax(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 352 | ExpectEqual(Value(x_, -1, 10), |
| 353 | GetMin(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
| 354 | ExpectEqual(Value(x_, -1, 20), |
| 355 | GetMax(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 356 | ExpectEqual(Value(-25), |
| 357 | GetMin(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 358 | ExpectEqual(Value(-11), |
| 359 | GetMax(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 360 | } |
| 361 | |
| 362 | TEST_F(InductionVarRangeTest, GetMinMaxNeg) { |
| 363 | ExpectEqual(Value(-20), GetMin(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 364 | ExpectEqual(Value(-10), GetMax(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 365 | ExpectEqual(Value(10), GetMin(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
| 366 | ExpectEqual(Value(20), GetMax(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 367 | ExpectEqual(Value(x_, -1, 0), GetMin(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr)); |
| 368 | ExpectEqual(Value(x_, -1, 0), GetMax(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 369 | } |
| 370 | |
| 371 | TEST_F(InductionVarRangeTest, GetMinMaxMul) { |
| 372 | ExpectEqual(Value(20), |
| 373 | GetMin(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 374 | ExpectEqual(Value(40), |
| 375 | GetMax(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 376 | } |
| 377 | |
| 378 | TEST_F(InductionVarRangeTest, GetMinMaxDiv) { |
| 379 | ExpectEqual(Value(3), |
| 380 | GetMin(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 381 | ExpectEqual(Value(5), |
| 382 | GetMax(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 383 | } |
| 384 | |
| 385 | TEST_F(InductionVarRangeTest, GetMinMaxConstant) { |
| 386 | ExpectEqual(Value(12345), GetMin(CreateConst(12345), nullptr)); |
| 387 | ExpectEqual(Value(12345), GetMax(CreateConst(12345), nullptr)); |
| 388 | } |
| 389 | |
| 390 | TEST_F(InductionVarRangeTest, GetMinMaxFetch) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 391 | ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(x_), nullptr)); |
| 392 | ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(x_), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 393 | } |
| 394 | |
| 395 | TEST_F(InductionVarRangeTest, GetMinMaxLinear) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 396 | ExpectEqual(Value(20), GetMin(CreateLinear(10, 20), CreateTripCount(100, true, true))); |
| 397 | ExpectEqual(Value(1010), GetMax(CreateLinear(10, 20), CreateTripCount(100, true, true))); |
| 398 | ExpectEqual(Value(-970), GetMin(CreateLinear(-10, 20), CreateTripCount(100, true, true))); |
| 399 | ExpectEqual(Value(20), GetMax(CreateLinear(-10, 20), CreateTripCount(100, true, true))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 400 | } |
| 401 | |
| 402 | TEST_F(InductionVarRangeTest, GetMinMaxWrapAround) { |
| 403 | ExpectEqual(Value(-5), GetMin(CreateWrapAround(-5, -1, 10), nullptr)); |
| 404 | ExpectEqual(Value(10), GetMax(CreateWrapAround(-5, -1, 10), nullptr)); |
| 405 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(2, -1, 10), nullptr)); |
| 406 | ExpectEqual(Value(10), GetMax(CreateWrapAround(2, -1, 10), nullptr)); |
| 407 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(20, -1, 10), nullptr)); |
| 408 | ExpectEqual(Value(20), GetMax(CreateWrapAround(20, -1, 10), nullptr)); |
| 409 | } |
| 410 | |
| 411 | TEST_F(InductionVarRangeTest, GetMinMaxPeriodic) { |
| 412 | ExpectEqual(Value(-2), GetMin(CreateRange(-2, 99), nullptr)); |
| 413 | ExpectEqual(Value(99), GetMax(CreateRange(-2, 99), nullptr)); |
| 414 | } |
| 415 | |
| 416 | TEST_F(InductionVarRangeTest, GetMulMin) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 417 | ExpectEqual(Value(-14), GetMul(CreateConst(2), CreateRange(-7, 8), true)); |
| 418 | ExpectEqual(Value(-16), GetMul(CreateConst(-2), CreateRange(-7, 8), true)); |
| 419 | ExpectEqual(Value(-14), GetMul(CreateRange(-7, 8), CreateConst(2), true)); |
| 420 | ExpectEqual(Value(-16), GetMul(CreateRange(-7, 8), CreateConst(-2), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 421 | ExpectEqual(Value(6), GetMul(CreateRange(2, 10), CreateRange(3, 5), true)); |
| 422 | ExpectEqual(Value(-50), GetMul(CreateRange(2, 10), CreateRange(-5, -3), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 423 | ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 424 | ExpectEqual(Value(-50), GetMul(CreateRange(-10, -2), CreateRange(3, 5), true)); |
| 425 | ExpectEqual(Value(6), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 426 | ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), true)); |
| 427 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), true)); |
| 428 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), true)); |
| 429 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 430 | } |
| 431 | |
| 432 | TEST_F(InductionVarRangeTest, GetMulMax) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 433 | ExpectEqual(Value(16), GetMul(CreateConst(2), CreateRange(-7, 8), false)); |
| 434 | ExpectEqual(Value(14), GetMul(CreateConst(-2), CreateRange(-7, 8), false)); |
| 435 | ExpectEqual(Value(16), GetMul(CreateRange(-7, 8), CreateConst(2), false)); |
| 436 | ExpectEqual(Value(14), GetMul(CreateRange(-7, 8), CreateConst(-2), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 437 | ExpectEqual(Value(50), GetMul(CreateRange(2, 10), CreateRange(3, 5), false)); |
| 438 | ExpectEqual(Value(-6), GetMul(CreateRange(2, 10), CreateRange(-5, -3), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 439 | ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 440 | ExpectEqual(Value(-6), GetMul(CreateRange(-10, -2), CreateRange(3, 5), false)); |
| 441 | ExpectEqual(Value(50), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 442 | ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), false)); |
| 443 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), false)); |
| 444 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), false)); |
| 445 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 446 | } |
| 447 | |
| 448 | TEST_F(InductionVarRangeTest, GetDivMin) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 449 | ExpectEqual(Value(-5), GetDiv(CreateRange(-10, 20), CreateConst(2), true)); |
| 450 | ExpectEqual(Value(-10), GetDiv(CreateRange(-10, 20), CreateConst(-2), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 451 | ExpectEqual(Value(10), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), true)); |
| 452 | ExpectEqual(Value(-500), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 453 | ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 454 | ExpectEqual(Value(-500), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), true)); |
| 455 | ExpectEqual(Value(10), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 456 | ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), true)); |
| 457 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), true)); |
| 458 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, -40), true)); |
| 459 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | TEST_F(InductionVarRangeTest, GetDivMax) { |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 463 | ExpectEqual(Value(10), GetDiv(CreateRange(-10, 20), CreateConst(2), false)); |
| 464 | ExpectEqual(Value(5), GetDiv(CreateRange(-10, 20), CreateConst(-2), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 465 | ExpectEqual(Value(500), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), false)); |
| 466 | ExpectEqual(Value(-10), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 467 | ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 468 | ExpectEqual(Value(-10), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), false)); |
| 469 | ExpectEqual(Value(500), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 470 | ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), false)); |
| 471 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), false)); |
| 472 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, 40), false)); |
| 473 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 474 | } |
| 475 | |
| 476 | TEST_F(InductionVarRangeTest, AddValue) { |
| 477 | ExpectEqual(Value(110), AddValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 478 | ExpectEqual(Value(-5), AddValue(Value(x_, 1, -4), Value(x_, -1, -1))); |
| 479 | ExpectEqual(Value(x_, 3, -5), AddValue(Value(x_, 2, -4), Value(x_, 1, -1))); |
| 480 | ExpectEqual(Value(), AddValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 481 | ExpectEqual(Value(x_, 1, 23), AddValue(Value(x_, 1, 20), Value(3))); |
| 482 | ExpectEqual(Value(y_, 1, 5), AddValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 483 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 484 | ExpectEqual(Value(max_value), AddValue(Value(max_value - 5), Value(5))); |
| 485 | ExpectEqual(Value(), AddValue(Value(max_value - 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 486 | } |
| 487 | |
| 488 | TEST_F(InductionVarRangeTest, SubValue) { |
| 489 | ExpectEqual(Value(-90), SubValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 490 | ExpectEqual(Value(-3), SubValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 491 | ExpectEqual(Value(x_, 2, -3), SubValue(Value(x_, 3, -4), Value(x_, 1, -1))); |
| 492 | ExpectEqual(Value(), SubValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 493 | ExpectEqual(Value(x_, 1, 17), SubValue(Value(x_, 1, 20), Value(3))); |
| 494 | ExpectEqual(Value(y_, -4, 105), SubValue(Value(55), Value(y_, 4, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 495 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 496 | ExpectEqual(Value(min_value), SubValue(Value(min_value + 5), Value(5))); |
| 497 | ExpectEqual(Value(), SubValue(Value(min_value + 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 498 | } |
| 499 | |
| 500 | TEST_F(InductionVarRangeTest, MulValue) { |
| 501 | ExpectEqual(Value(1000), MulValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 502 | ExpectEqual(Value(), MulValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 503 | ExpectEqual(Value(), MulValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 504 | ExpectEqual(Value(x_, 9, 60), MulValue(Value(x_, 3, 20), Value(3))); |
| 505 | ExpectEqual(Value(y_, 55, -110), MulValue(Value(55), Value(y_, 1, -2))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 506 | ExpectEqual(Value(), MulValue(Value(90000), Value(-90000))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 507 | } |
| 508 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 509 | TEST_F(InductionVarRangeTest, MulValueSpecial) { |
| 510 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 511 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 512 | |
| 513 | // Unsafe. |
| 514 | ExpectEqual(Value(), MulValue(Value(min_value), Value(min_value))); |
| 515 | ExpectEqual(Value(), MulValue(Value(min_value), Value(-1))); |
| 516 | ExpectEqual(Value(), MulValue(Value(min_value), Value(max_value))); |
| 517 | ExpectEqual(Value(), MulValue(Value(max_value), Value(max_value))); |
| 518 | |
| 519 | // Safe. |
| 520 | ExpectEqual(Value(min_value), MulValue(Value(min_value), Value(1))); |
| 521 | ExpectEqual(Value(max_value), MulValue(Value(max_value), Value(1))); |
| 522 | ExpectEqual(Value(-max_value), MulValue(Value(max_value), Value(-1))); |
| 523 | ExpectEqual(Value(-1), MulValue(Value(1), Value(-1))); |
| 524 | ExpectEqual(Value(1), MulValue(Value(-1), Value(-1))); |
| 525 | } |
| 526 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 527 | TEST_F(InductionVarRangeTest, DivValue) { |
| 528 | ExpectEqual(Value(25), DivValue(Value(100), Value(4))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 529 | ExpectEqual(Value(), DivValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 530 | ExpectEqual(Value(), DivValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 531 | ExpectEqual(Value(), DivValue(Value(x_, 12, 24), Value(3))); |
| 532 | ExpectEqual(Value(), DivValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 533 | ExpectEqual(Value(), DivValue(Value(1), Value(0))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 534 | } |
| 535 | |
Aart Bik | 97412c92 | 2016-02-19 20:14:38 -0800 | [diff] [blame] | 536 | TEST_F(InductionVarRangeTest, DivValueSpecial) { |
| 537 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 538 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 539 | |
| 540 | // Unsafe. |
| 541 | ExpectEqual(Value(), DivValue(Value(min_value), Value(-1))); |
| 542 | |
| 543 | // Safe. |
| 544 | ExpectEqual(Value(1), DivValue(Value(min_value), Value(min_value))); |
| 545 | ExpectEqual(Value(1), DivValue(Value(max_value), Value(max_value))); |
| 546 | ExpectEqual(Value(min_value), DivValue(Value(min_value), Value(1))); |
| 547 | ExpectEqual(Value(max_value), DivValue(Value(max_value), Value(1))); |
| 548 | ExpectEqual(Value(-max_value), DivValue(Value(max_value), Value(-1))); |
| 549 | ExpectEqual(Value(-1), DivValue(Value(1), Value(-1))); |
| 550 | ExpectEqual(Value(1), DivValue(Value(-1), Value(-1))); |
| 551 | } |
| 552 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 553 | TEST_F(InductionVarRangeTest, MinValue) { |
| 554 | ExpectEqual(Value(10), MinValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 555 | ExpectEqual(Value(x_, 1, -4), MinValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 556 | ExpectEqual(Value(x_, 4, -4), MinValue(Value(x_, 4, -4), Value(x_, 4, -1))); |
| 557 | ExpectEqual(Value(), MinValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 558 | ExpectEqual(Value(), MinValue(Value(x_, 1, 20), Value(3))); |
| 559 | ExpectEqual(Value(), MinValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 560 | } |
| 561 | |
| 562 | TEST_F(InductionVarRangeTest, MaxValue) { |
| 563 | ExpectEqual(Value(100), MaxValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 564 | ExpectEqual(Value(x_, 1, -1), MaxValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 565 | ExpectEqual(Value(x_, 4, -1), MaxValue(Value(x_, 4, -4), Value(x_, 4, -1))); |
| 566 | ExpectEqual(Value(), MaxValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 567 | ExpectEqual(Value(), MaxValue(Value(x_, 1, 20), Value(3))); |
| 568 | ExpectEqual(Value(), MaxValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 569 | } |
| 570 | |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 571 | TEST_F(InductionVarRangeTest, ArrayLengthAndHints) { |
| 572 | HInstruction* new_array = new (&allocator_) |
| 573 | HNewArray(x_, |
| 574 | graph_->GetCurrentMethod(), |
| 575 | 0, Primitive::kPrimInt, |
| 576 | graph_->GetDexFile(), |
| 577 | kQuickAllocArray); |
| 578 | entry_block_->AddInstruction(new_array); |
| 579 | HInstruction* array_length = new (&allocator_) HArrayLength(new_array, 0); |
| 580 | entry_block_->AddInstruction(array_length); |
| 581 | // With null hint: yields extreme constants. |
| 582 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 583 | SetHint(nullptr); |
| 584 | ExpectEqual(Value(0), GetMin(CreateFetch(array_length), nullptr)); |
| 585 | ExpectEqual(Value(max_value), GetMax(CreateFetch(array_length), nullptr)); |
| 586 | // With explicit hint: yields the length instruction. |
| 587 | SetHint(array_length); |
| 588 | ExpectEqual(Value(array_length, 1, 0), GetMin(CreateFetch(array_length), nullptr)); |
| 589 | ExpectEqual(Value(array_length, 1, 0), GetMax(CreateFetch(array_length), nullptr)); |
| 590 | // With any non-null hint: chases beyond the length instruction. |
| 591 | SetHint(x_); |
| 592 | ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(array_length), nullptr)); |
| 593 | ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(array_length), nullptr)); |
| 594 | } |
| 595 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 596 | // |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 597 | // Tests on public methods. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 598 | // |
| 599 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 600 | TEST_F(InductionVarRangeTest, ConstantTripCountUp) { |
| 601 | BuildLoop(0, graph_->GetIntConstant(1000), 1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 602 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 603 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 604 | Value v1, v2; |
| 605 | bool needs_finite_test = true; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 606 | bool needs_taken_test = true; |
| 607 | |
| 608 | HInstruction* phi = condition_->InputAt(0); |
| 609 | HInstruction* exit = exit_block_->GetLastInstruction(); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 610 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 611 | // In context of header: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 612 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 613 | EXPECT_FALSE(needs_finite_test); |
| 614 | ExpectEqual(Value(0), v1); |
| 615 | ExpectEqual(Value(1000), v2); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 616 | |
| 617 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 618 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 619 | EXPECT_FALSE(needs_finite_test); |
| 620 | ExpectEqual(Value(0), v1); |
| 621 | ExpectEqual(Value(999), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 622 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 623 | EXPECT_FALSE(needs_finite_test); |
| 624 | ExpectEqual(Value(1), v1); |
| 625 | ExpectEqual(Value(1000), v2); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 626 | |
| 627 | // Induction vs. no-induction. |
| 628 | EXPECT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
| 629 | EXPECT_TRUE(range_.CanGenerateLastValue(phi)); |
| 630 | EXPECT_FALSE(range_.CanGenerateRange(exit, exit, &needs_finite_test, &needs_taken_test)); |
| 631 | EXPECT_FALSE(range_.CanGenerateLastValue(exit)); |
| 632 | |
| 633 | // Last value (unsimplified). |
| 634 | HInstruction* last = range_.GenerateLastValue(phi, graph_, loop_preheader_); |
| 635 | ASSERT_TRUE(last->IsAdd()); |
| 636 | ASSERT_TRUE(last->InputAt(0)->IsIntConstant()); |
| 637 | EXPECT_EQ(1000, last->InputAt(0)->AsIntConstant()->GetValue()); |
| 638 | ASSERT_TRUE(last->InputAt(1)->IsIntConstant()); |
| 639 | EXPECT_EQ(0, last->InputAt(1)->AsIntConstant()->GetValue()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 640 | } |
| 641 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 642 | TEST_F(InductionVarRangeTest, ConstantTripCountDown) { |
| 643 | BuildLoop(1000, graph_->GetIntConstant(0), -1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 644 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 645 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 646 | Value v1, v2; |
| 647 | bool needs_finite_test = true; |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 648 | bool needs_taken_test = true; |
| 649 | |
| 650 | HInstruction* phi = condition_->InputAt(0); |
| 651 | HInstruction* exit = exit_block_->GetLastInstruction(); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 652 | |
| 653 | // In context of header: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 654 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 655 | EXPECT_FALSE(needs_finite_test); |
| 656 | ExpectEqual(Value(0), v1); |
| 657 | ExpectEqual(Value(1000), v2); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 658 | |
| 659 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 660 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 661 | EXPECT_FALSE(needs_finite_test); |
| 662 | ExpectEqual(Value(1), v1); |
| 663 | ExpectEqual(Value(1000), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 664 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 665 | EXPECT_FALSE(needs_finite_test); |
| 666 | ExpectEqual(Value(0), v1); |
| 667 | ExpectEqual(Value(999), v2); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 668 | |
| 669 | // Induction vs. no-induction. |
| 670 | EXPECT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
| 671 | EXPECT_TRUE(range_.CanGenerateLastValue(phi)); |
| 672 | EXPECT_FALSE(range_.CanGenerateRange(exit, exit, &needs_finite_test, &needs_taken_test)); |
| 673 | EXPECT_FALSE(range_.CanGenerateLastValue(exit)); |
| 674 | |
| 675 | // Last value (unsimplified). |
| 676 | HInstruction* last = range_.GenerateLastValue(phi, graph_, loop_preheader_); |
| 677 | ASSERT_TRUE(last->IsSub()); |
| 678 | ASSERT_TRUE(last->InputAt(0)->IsIntConstant()); |
| 679 | EXPECT_EQ(1000, last->InputAt(0)->AsIntConstant()->GetValue()); |
| 680 | ASSERT_TRUE(last->InputAt(1)->IsNeg()); |
| 681 | last = last->InputAt(1)->InputAt(0); |
| 682 | ASSERT_TRUE(last->IsSub()); |
| 683 | ASSERT_TRUE(last->InputAt(0)->IsIntConstant()); |
| 684 | EXPECT_EQ(0, last->InputAt(0)->AsIntConstant()->GetValue()); |
| 685 | ASSERT_TRUE(last->InputAt(1)->IsIntConstant()); |
| 686 | EXPECT_EQ(1000, last->InputAt(1)->AsIntConstant()->GetValue()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 687 | } |
| 688 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 689 | TEST_F(InductionVarRangeTest, SymbolicTripCountUp) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 690 | BuildLoop(0, x_, 1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 691 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 692 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 693 | Value v1, v2; |
| 694 | bool needs_finite_test = true; |
| 695 | bool needs_taken_test = true; |
| 696 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 697 | HInstruction* phi = condition_->InputAt(0); |
| 698 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 699 | // In context of header: upper unknown. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 700 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 701 | EXPECT_FALSE(needs_finite_test); |
| 702 | ExpectEqual(Value(0), v1); |
| 703 | ExpectEqual(Value(), v2); |
| 704 | |
| 705 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 706 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 707 | EXPECT_FALSE(needs_finite_test); |
| 708 | ExpectEqual(Value(0), v1); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 709 | ExpectEqual(Value(x_, 1, -1), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 710 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 711 | EXPECT_FALSE(needs_finite_test); |
| 712 | ExpectEqual(Value(1), v1); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 713 | ExpectEqual(Value(x_, 1, 0), v2); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 714 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 715 | HInstruction* lower = nullptr; |
| 716 | HInstruction* upper = nullptr; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 717 | |
| 718 | // Can generate code in context of loop-body only. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 719 | EXPECT_FALSE(range_.CanGenerateRange(condition_, phi, &needs_finite_test, &needs_taken_test)); |
| 720 | ASSERT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 721 | EXPECT_FALSE(needs_finite_test); |
| 722 | EXPECT_TRUE(needs_taken_test); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 723 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 724 | // Generates code (unsimplified). |
| 725 | range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 726 | |
| 727 | // Verify lower is 0+0. |
| 728 | ASSERT_TRUE(lower != nullptr); |
| 729 | ASSERT_TRUE(lower->IsAdd()); |
| 730 | ASSERT_TRUE(lower->InputAt(0)->IsIntConstant()); |
| 731 | EXPECT_EQ(0, lower->InputAt(0)->AsIntConstant()->GetValue()); |
| 732 | ASSERT_TRUE(lower->InputAt(1)->IsIntConstant()); |
| 733 | EXPECT_EQ(0, lower->InputAt(1)->AsIntConstant()->GetValue()); |
| 734 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 735 | // Verify upper is (V-1)+0. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 736 | ASSERT_TRUE(upper != nullptr); |
| 737 | ASSERT_TRUE(upper->IsAdd()); |
| 738 | ASSERT_TRUE(upper->InputAt(0)->IsSub()); |
| 739 | EXPECT_TRUE(upper->InputAt(0)->InputAt(0)->IsParameterValue()); |
| 740 | ASSERT_TRUE(upper->InputAt(0)->InputAt(1)->IsIntConstant()); |
| 741 | EXPECT_EQ(1, upper->InputAt(0)->InputAt(1)->AsIntConstant()->GetValue()); |
| 742 | ASSERT_TRUE(upper->InputAt(1)->IsIntConstant()); |
| 743 | EXPECT_EQ(0, upper->InputAt(1)->AsIntConstant()->GetValue()); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 744 | |
| 745 | // Verify taken-test is 0<V. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 746 | HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 747 | ASSERT_TRUE(taken != nullptr); |
| 748 | ASSERT_TRUE(taken->IsLessThan()); |
| 749 | ASSERT_TRUE(taken->InputAt(0)->IsIntConstant()); |
| 750 | EXPECT_EQ(0, taken->InputAt(0)->AsIntConstant()->GetValue()); |
| 751 | EXPECT_TRUE(taken->InputAt(1)->IsParameterValue()); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 752 | |
| 753 | // Replacement. |
| 754 | range_.Replace(loop_header_->GetLastInstruction(), x_, y_); |
| 755 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
| 756 | EXPECT_FALSE(needs_finite_test); |
| 757 | ExpectEqual(Value(1), v1); |
| 758 | ExpectEqual(Value(y_, 1, 0), v2); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 759 | } |
| 760 | |
| 761 | TEST_F(InductionVarRangeTest, SymbolicTripCountDown) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 762 | BuildLoop(1000, x_, -1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 763 | PerformInductionVarAnalysis(); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 764 | |
| 765 | Value v1, v2; |
| 766 | bool needs_finite_test = true; |
| 767 | bool needs_taken_test = true; |
| 768 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 769 | HInstruction* phi = condition_->InputAt(0); |
| 770 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 771 | // In context of header: lower unknown. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 772 | range_.GetInductionRange(condition_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 773 | EXPECT_FALSE(needs_finite_test); |
| 774 | ExpectEqual(Value(), v1); |
| 775 | ExpectEqual(Value(1000), v2); |
| 776 | |
| 777 | // In context of loop-body: known. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 778 | range_.GetInductionRange(increment_, phi, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 779 | EXPECT_FALSE(needs_finite_test); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 780 | ExpectEqual(Value(x_, 1, 1), v1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 781 | ExpectEqual(Value(1000), v2); |
Aart Bik | 52be7e7 | 2016-06-23 11:20:41 -0700 | [diff] [blame] | 782 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 783 | EXPECT_FALSE(needs_finite_test); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 784 | ExpectEqual(Value(x_, 1, 0), v1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 785 | ExpectEqual(Value(999), v2); |
| 786 | |
| 787 | HInstruction* lower = nullptr; |
| 788 | HInstruction* upper = nullptr; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 789 | |
| 790 | // Can generate code in context of loop-body only. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 791 | EXPECT_FALSE(range_.CanGenerateRange(condition_, phi, &needs_finite_test, &needs_taken_test)); |
| 792 | ASSERT_TRUE(range_.CanGenerateRange(increment_, phi, &needs_finite_test, &needs_taken_test)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 793 | EXPECT_FALSE(needs_finite_test); |
| 794 | EXPECT_TRUE(needs_taken_test); |
| 795 | |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 796 | // Generates code (unsimplified). |
| 797 | range_.GenerateRange(increment_, phi, graph_, loop_preheader_, &lower, &upper); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 798 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 799 | // Verify lower is 1000-((1000-V)-1). |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 800 | ASSERT_TRUE(lower != nullptr); |
| 801 | ASSERT_TRUE(lower->IsSub()); |
| 802 | ASSERT_TRUE(lower->InputAt(0)->IsIntConstant()); |
| 803 | EXPECT_EQ(1000, lower->InputAt(0)->AsIntConstant()->GetValue()); |
| 804 | lower = lower->InputAt(1); |
| 805 | ASSERT_TRUE(lower->IsSub()); |
| 806 | ASSERT_TRUE(lower->InputAt(1)->IsIntConstant()); |
| 807 | EXPECT_EQ(1, lower->InputAt(1)->AsIntConstant()->GetValue()); |
| 808 | lower = lower->InputAt(0); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 809 | ASSERT_TRUE(lower->IsSub()); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 810 | ASSERT_TRUE(lower->InputAt(0)->IsIntConstant()); |
| 811 | EXPECT_EQ(1000, lower->InputAt(0)->AsIntConstant()->GetValue()); |
| 812 | EXPECT_TRUE(lower->InputAt(1)->IsParameterValue()); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 813 | |
| 814 | // Verify upper is 1000-0. |
| 815 | ASSERT_TRUE(upper != nullptr); |
| 816 | ASSERT_TRUE(upper->IsSub()); |
| 817 | ASSERT_TRUE(upper->InputAt(0)->IsIntConstant()); |
| 818 | EXPECT_EQ(1000, upper->InputAt(0)->AsIntConstant()->GetValue()); |
| 819 | ASSERT_TRUE(upper->InputAt(1)->IsIntConstant()); |
| 820 | EXPECT_EQ(0, upper->InputAt(1)->AsIntConstant()->GetValue()); |
| 821 | |
| 822 | // Verify taken-test is 1000>V. |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 823 | HInstruction* taken = range_.GenerateTakenTest(increment_, graph_, loop_preheader_); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 824 | ASSERT_TRUE(taken != nullptr); |
| 825 | ASSERT_TRUE(taken->IsGreaterThan()); |
| 826 | ASSERT_TRUE(taken->InputAt(0)->IsIntConstant()); |
| 827 | EXPECT_EQ(1000, taken->InputAt(0)->AsIntConstant()->GetValue()); |
| 828 | EXPECT_TRUE(taken->InputAt(1)->IsParameterValue()); |
Aart Bik | 16d3a65 | 2016-09-09 10:33:50 -0700 | [diff] [blame^] | 829 | |
| 830 | // Replacement. |
| 831 | range_.Replace(loop_header_->GetLastInstruction(), x_, y_); |
| 832 | range_.GetInductionRange(increment_, increment_, x_, &v1, &v2, &needs_finite_test); |
| 833 | EXPECT_FALSE(needs_finite_test); |
| 834 | ExpectEqual(Value(y_, 1, 0), v1); |
| 835 | ExpectEqual(Value(999), v2); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 836 | } |
| 837 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 838 | } // namespace art |