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 | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 69 | } |
| 70 | |
| 71 | /** Constructs loop with given upper bound. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 72 | void BuildLoop(int32_t lower, HInstruction* upper, int32_t stride) { |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 73 | // Control flow. |
| 74 | loop_preheader_ = new (&allocator_) HBasicBlock(graph_); |
| 75 | graph_->AddBlock(loop_preheader_); |
| 76 | HBasicBlock* loop_header = new (&allocator_) HBasicBlock(graph_); |
| 77 | graph_->AddBlock(loop_header); |
| 78 | HBasicBlock* loop_body = new (&allocator_) HBasicBlock(graph_); |
| 79 | graph_->AddBlock(loop_body); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 80 | HBasicBlock* return_block = new (&allocator_) HBasicBlock(graph_); |
| 81 | graph_->AddBlock(return_block); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 82 | entry_block_->AddSuccessor(loop_preheader_); |
| 83 | loop_preheader_->AddSuccessor(loop_header); |
| 84 | loop_header->AddSuccessor(loop_body); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 85 | loop_header->AddSuccessor(return_block); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 86 | loop_body->AddSuccessor(loop_header); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 87 | return_block->AddSuccessor(exit_block_); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 88 | // Instructions. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 89 | loop_preheader_->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 90 | HPhi* phi = new (&allocator_) HPhi(&allocator_, 0, 0, Primitive::kPrimInt); |
| 91 | loop_header->AddPhi(phi); |
| 92 | phi->AddInput(graph_->GetIntConstant(lower)); // i = l |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 93 | if (stride > 0) { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 94 | condition_ = new (&allocator_) HLessThan(phi, upper); // i < u |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 95 | } else { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 96 | condition_ = new (&allocator_) HGreaterThan(phi, upper); // i > u |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 97 | } |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 98 | loop_header->AddInstruction(condition_); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 99 | loop_header->AddInstruction(new (&allocator_) HIf(condition_)); |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 100 | increment_ = new (&allocator_) HAdd(Primitive::kPrimInt, phi, graph_->GetIntConstant(stride)); |
| 101 | loop_body->AddInstruction(increment_); // i += s |
| 102 | phi->AddInput(increment_); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 103 | loop_body->AddInstruction(new (&allocator_) HGoto()); |
David Brazdil | db51efb | 2015-11-06 01:36:20 +0000 | [diff] [blame] | 104 | return_block->AddInstruction(new (&allocator_) HReturnVoid()); |
| 105 | exit_block_->AddInstruction(new (&allocator_) HExit()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 106 | } |
| 107 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 108 | /** Constructs SSA and performs induction variable analysis. */ |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 109 | void PerformInductionVarAnalysis() { |
David Brazdil | badd826 | 2016-02-02 16:28:56 +0000 | [diff] [blame] | 110 | graph_->BuildDominatorTree(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 111 | iva_->Run(); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 112 | } |
| 113 | |
| 114 | /** Constructs an invariant. */ |
| 115 | HInductionVarAnalysis::InductionInfo* CreateInvariant(char opc, |
| 116 | HInductionVarAnalysis::InductionInfo* a, |
| 117 | HInductionVarAnalysis::InductionInfo* b) { |
| 118 | HInductionVarAnalysis::InductionOp op; |
| 119 | switch (opc) { |
| 120 | case '+': op = HInductionVarAnalysis::kAdd; break; |
| 121 | case '-': op = HInductionVarAnalysis::kSub; break; |
| 122 | case 'n': op = HInductionVarAnalysis::kNeg; break; |
| 123 | case '*': op = HInductionVarAnalysis::kMul; break; |
| 124 | case '/': op = HInductionVarAnalysis::kDiv; break; |
| 125 | default: op = HInductionVarAnalysis::kNop; break; |
| 126 | } |
| 127 | return iva_->CreateInvariantOp(op, a, b); |
| 128 | } |
| 129 | |
| 130 | /** Constructs a fetch. */ |
| 131 | HInductionVarAnalysis::InductionInfo* CreateFetch(HInstruction* fetch) { |
| 132 | return iva_->CreateInvariantFetch(fetch); |
| 133 | } |
| 134 | |
| 135 | /** Constructs a constant. */ |
| 136 | HInductionVarAnalysis::InductionInfo* CreateConst(int32_t c) { |
| 137 | return CreateFetch(graph_->GetIntConstant(c)); |
| 138 | } |
| 139 | |
| 140 | /** Constructs a trip-count. */ |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 141 | HInductionVarAnalysis::InductionInfo* CreateTripCount(int32_t tc, bool in_loop, bool safe) { |
| 142 | if (in_loop && safe) { |
| 143 | return iva_->CreateTripCount( |
| 144 | HInductionVarAnalysis::kTripCountInLoop, CreateConst(tc), nullptr); |
| 145 | } else if (in_loop) { |
| 146 | return iva_->CreateTripCount( |
| 147 | HInductionVarAnalysis::kTripCountInLoopUnsafe, CreateConst(tc), nullptr); |
| 148 | } else if (safe) { |
| 149 | return iva_->CreateTripCount( |
| 150 | HInductionVarAnalysis::kTripCountInBody, CreateConst(tc), nullptr); |
| 151 | } else { |
| 152 | return iva_->CreateTripCount( |
| 153 | HInductionVarAnalysis::kTripCountInBodyUnsafe, CreateConst(tc), nullptr); |
| 154 | } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 155 | } |
| 156 | |
| 157 | /** Constructs a linear a * i + b induction. */ |
| 158 | HInductionVarAnalysis::InductionInfo* CreateLinear(int32_t a, int32_t b) { |
| 159 | return iva_->CreateInduction(HInductionVarAnalysis::kLinear, CreateConst(a), CreateConst(b)); |
| 160 | } |
| 161 | |
| 162 | /** Constructs a range [lo, hi] using a periodic induction. */ |
| 163 | HInductionVarAnalysis::InductionInfo* CreateRange(int32_t lo, int32_t hi) { |
| 164 | return iva_->CreateInduction( |
| 165 | HInductionVarAnalysis::kPeriodic, CreateConst(lo), CreateConst(hi)); |
| 166 | } |
| 167 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 168 | /** Constructs a wrap-around induction consisting of a constant, followed info */ |
| 169 | HInductionVarAnalysis::InductionInfo* CreateWrapAround( |
| 170 | int32_t initial, |
| 171 | HInductionVarAnalysis::InductionInfo* info) { |
| 172 | return iva_->CreateInduction(HInductionVarAnalysis::kWrapAround, CreateConst(initial), info); |
| 173 | } |
| 174 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 175 | /** Constructs a wrap-around induction consisting of a constant, followed by a range. */ |
| 176 | HInductionVarAnalysis::InductionInfo* CreateWrapAround(int32_t initial, int32_t lo, int32_t hi) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 177 | return CreateWrapAround(initial, CreateRange(lo, hi)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | // |
| 181 | // Relay methods. |
| 182 | // |
| 183 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 184 | bool NeedsTripCount(HInductionVarAnalysis::InductionInfo* info) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 185 | return range_.NeedsTripCount(info); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 186 | } |
| 187 | |
| 188 | bool IsBodyTripCount(HInductionVarAnalysis::InductionInfo* trip) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 189 | return range_.IsBodyTripCount(trip); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | bool IsUnsafeTripCount(HInductionVarAnalysis::InductionInfo* trip) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 193 | return range_.IsUnsafeTripCount(trip); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 194 | } |
| 195 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 196 | Value GetMin(HInductionVarAnalysis::InductionInfo* info, |
| 197 | HInductionVarAnalysis::InductionInfo* induc) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 198 | return range_.GetVal(info, induc, /* in_body */ true, /* is_min */ true); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | Value GetMax(HInductionVarAnalysis::InductionInfo* info, |
| 202 | HInductionVarAnalysis::InductionInfo* induc) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 203 | return range_.GetVal(info, induc, /* in_body */ true, /* is_min */ false); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | Value GetMul(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 207 | HInductionVarAnalysis::InductionInfo* info2, |
| 208 | bool is_min) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 209 | return range_.GetMul(info1, info2, nullptr, /* in_body */ true, is_min); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 210 | } |
| 211 | |
| 212 | Value GetDiv(HInductionVarAnalysis::InductionInfo* info1, |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 213 | HInductionVarAnalysis::InductionInfo* info2, |
| 214 | bool is_min) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 215 | return range_.GetDiv(info1, info2, nullptr, /* in_body */ true, is_min); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 216 | } |
| 217 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 218 | bool IsConstantRange(HInductionVarAnalysis::InductionInfo* info, |
| 219 | int32_t* min_value, |
| 220 | int32_t* max_value) { |
| 221 | return range_.IsConstantRange(info, min_value, max_value); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 222 | } |
| 223 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 224 | Value AddValue(Value v1, Value v2) { return range_.AddValue(v1, v2); } |
| 225 | Value SubValue(Value v1, Value v2) { return range_.SubValue(v1, v2); } |
| 226 | Value MulValue(Value v1, Value v2) { return range_.MulValue(v1, v2); } |
| 227 | Value DivValue(Value v1, Value v2) { return range_.DivValue(v1, v2); } |
| 228 | Value MinValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, true); } |
| 229 | Value MaxValue(Value v1, Value v2) { return range_.MergeVal(v1, v2, false); } |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 230 | |
| 231 | // General building fields. |
| 232 | ArenaPool pool_; |
| 233 | ArenaAllocator allocator_; |
| 234 | HGraph* graph_; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 235 | HBasicBlock* entry_block_; |
| 236 | HBasicBlock* exit_block_; |
| 237 | HBasicBlock* loop_preheader_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 238 | HInductionVarAnalysis* iva_; |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 239 | InductionVarRange range_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 240 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 241 | // Instructions. |
| 242 | HInstruction* condition_; |
| 243 | HInstruction* increment_; |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 244 | HInstruction* x_; |
| 245 | HInstruction* y_; |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 246 | }; |
| 247 | |
| 248 | // |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 249 | // Tests on private methods. |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 250 | // |
| 251 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 252 | TEST_F(InductionVarRangeTest, TripCountProperties) { |
| 253 | EXPECT_FALSE(NeedsTripCount(nullptr)); |
| 254 | EXPECT_FALSE(NeedsTripCount(CreateConst(1))); |
| 255 | EXPECT_TRUE(NeedsTripCount(CreateLinear(1, 1))); |
| 256 | EXPECT_FALSE(NeedsTripCount(CreateWrapAround(1, 2, 3))); |
| 257 | EXPECT_TRUE(NeedsTripCount(CreateWrapAround(1, CreateLinear(1, 1)))); |
| 258 | |
| 259 | EXPECT_FALSE(IsBodyTripCount(nullptr)); |
| 260 | EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, true))); |
| 261 | EXPECT_FALSE(IsBodyTripCount(CreateTripCount(100, true, false))); |
| 262 | EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, true))); |
| 263 | EXPECT_TRUE(IsBodyTripCount(CreateTripCount(100, false, false))); |
| 264 | |
| 265 | EXPECT_FALSE(IsUnsafeTripCount(nullptr)); |
| 266 | EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, true, true))); |
| 267 | EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, true, false))); |
| 268 | EXPECT_FALSE(IsUnsafeTripCount(CreateTripCount(100, false, true))); |
| 269 | EXPECT_TRUE(IsUnsafeTripCount(CreateTripCount(100, false, false))); |
| 270 | } |
| 271 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 272 | TEST_F(InductionVarRangeTest, GetMinMaxNull) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 273 | ExpectEqual(Value(), GetMin(nullptr, nullptr)); |
| 274 | ExpectEqual(Value(), GetMax(nullptr, nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 275 | } |
| 276 | |
| 277 | TEST_F(InductionVarRangeTest, GetMinMaxAdd) { |
| 278 | ExpectEqual(Value(12), |
| 279 | GetMin(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 280 | ExpectEqual(Value(22), |
| 281 | GetMax(CreateInvariant('+', CreateConst(2), CreateRange(10, 20)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 282 | ExpectEqual(Value(x_, 1, -20), |
| 283 | GetMin(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 284 | ExpectEqual(Value(x_, 1, -10), |
| 285 | GetMax(CreateInvariant('+', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 286 | ExpectEqual(Value(x_, 1, 10), |
| 287 | GetMin(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
| 288 | ExpectEqual(Value(x_, 1, 20), |
| 289 | GetMax(CreateInvariant('+', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 290 | ExpectEqual(Value(5), |
| 291 | GetMin(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 292 | ExpectEqual(Value(19), |
| 293 | GetMax(CreateInvariant('+', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 294 | } |
| 295 | |
| 296 | TEST_F(InductionVarRangeTest, GetMinMaxSub) { |
| 297 | ExpectEqual(Value(-18), |
| 298 | GetMin(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 299 | ExpectEqual(Value(-8), |
| 300 | GetMax(CreateInvariant('-', CreateConst(2), CreateRange(10, 20)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 301 | ExpectEqual(Value(x_, 1, 10), |
| 302 | GetMin(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 303 | ExpectEqual(Value(x_, 1, 20), |
| 304 | GetMax(CreateInvariant('-', CreateFetch(x_), CreateRange(-20, -10)), nullptr)); |
| 305 | ExpectEqual(Value(x_, -1, 10), |
| 306 | GetMin(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
| 307 | ExpectEqual(Value(x_, -1, 20), |
| 308 | GetMax(CreateInvariant('-', CreateRange(10, 20), CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 309 | ExpectEqual(Value(-25), |
| 310 | GetMin(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 311 | ExpectEqual(Value(-11), |
| 312 | GetMax(CreateInvariant('-', CreateRange(-5, -1), CreateRange(10, 20)), nullptr)); |
| 313 | } |
| 314 | |
| 315 | TEST_F(InductionVarRangeTest, GetMinMaxNeg) { |
| 316 | ExpectEqual(Value(-20), GetMin(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 317 | ExpectEqual(Value(-10), GetMax(CreateInvariant('n', nullptr, CreateRange(10, 20)), nullptr)); |
| 318 | ExpectEqual(Value(10), GetMin(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
| 319 | ExpectEqual(Value(20), GetMax(CreateInvariant('n', nullptr, CreateRange(-20, -10)), nullptr)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 320 | ExpectEqual(Value(x_, -1, 0), GetMin(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr)); |
| 321 | ExpectEqual(Value(x_, -1, 0), GetMax(CreateInvariant('n', nullptr, CreateFetch(x_)), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 322 | } |
| 323 | |
| 324 | TEST_F(InductionVarRangeTest, GetMinMaxMul) { |
| 325 | ExpectEqual(Value(20), |
| 326 | GetMin(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 327 | ExpectEqual(Value(40), |
| 328 | GetMax(CreateInvariant('*', CreateConst(2), CreateRange(10, 20)), nullptr)); |
| 329 | } |
| 330 | |
| 331 | TEST_F(InductionVarRangeTest, GetMinMaxDiv) { |
| 332 | ExpectEqual(Value(3), |
| 333 | GetMin(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 334 | ExpectEqual(Value(5), |
| 335 | GetMax(CreateInvariant('/', CreateRange(12, 20), CreateConst(4)), nullptr)); |
| 336 | } |
| 337 | |
| 338 | TEST_F(InductionVarRangeTest, GetMinMaxConstant) { |
| 339 | ExpectEqual(Value(12345), GetMin(CreateConst(12345), nullptr)); |
| 340 | ExpectEqual(Value(12345), GetMax(CreateConst(12345), nullptr)); |
| 341 | } |
| 342 | |
| 343 | TEST_F(InductionVarRangeTest, GetMinMaxFetch) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 344 | ExpectEqual(Value(x_, 1, 0), GetMin(CreateFetch(x_), nullptr)); |
| 345 | ExpectEqual(Value(x_, 1, 0), GetMax(CreateFetch(x_), nullptr)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | TEST_F(InductionVarRangeTest, GetMinMaxLinear) { |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 349 | ExpectEqual(Value(20), GetMin(CreateLinear(10, 20), CreateTripCount(100, true, true))); |
| 350 | ExpectEqual(Value(1010), GetMax(CreateLinear(10, 20), CreateTripCount(100, true, true))); |
| 351 | ExpectEqual(Value(-970), GetMin(CreateLinear(-10, 20), CreateTripCount(100, true, true))); |
| 352 | ExpectEqual(Value(20), GetMax(CreateLinear(-10, 20), CreateTripCount(100, true, true))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | TEST_F(InductionVarRangeTest, GetMinMaxWrapAround) { |
| 356 | ExpectEqual(Value(-5), GetMin(CreateWrapAround(-5, -1, 10), nullptr)); |
| 357 | ExpectEqual(Value(10), GetMax(CreateWrapAround(-5, -1, 10), nullptr)); |
| 358 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(2, -1, 10), nullptr)); |
| 359 | ExpectEqual(Value(10), GetMax(CreateWrapAround(2, -1, 10), nullptr)); |
| 360 | ExpectEqual(Value(-1), GetMin(CreateWrapAround(20, -1, 10), nullptr)); |
| 361 | ExpectEqual(Value(20), GetMax(CreateWrapAround(20, -1, 10), nullptr)); |
| 362 | } |
| 363 | |
| 364 | TEST_F(InductionVarRangeTest, GetMinMaxPeriodic) { |
| 365 | ExpectEqual(Value(-2), GetMin(CreateRange(-2, 99), nullptr)); |
| 366 | ExpectEqual(Value(99), GetMax(CreateRange(-2, 99), nullptr)); |
| 367 | } |
| 368 | |
| 369 | TEST_F(InductionVarRangeTest, GetMulMin) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 370 | ExpectEqual(Value(6), GetMul(CreateRange(2, 10), CreateRange(3, 5), true)); |
| 371 | ExpectEqual(Value(-50), GetMul(CreateRange(2, 10), CreateRange(-5, -3), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 372 | ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 373 | ExpectEqual(Value(-50), GetMul(CreateRange(-10, -2), CreateRange(3, 5), true)); |
| 374 | ExpectEqual(Value(6), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 375 | ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), true)); |
| 376 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), true)); |
| 377 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), true)); |
| 378 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 379 | } |
| 380 | |
| 381 | TEST_F(InductionVarRangeTest, GetMulMax) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 382 | ExpectEqual(Value(50), GetMul(CreateRange(2, 10), CreateRange(3, 5), false)); |
| 383 | ExpectEqual(Value(-6), GetMul(CreateRange(2, 10), CreateRange(-5, -3), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 384 | ExpectEqual(Value(), GetMul(CreateRange(2, 10), CreateRange(-1, 1), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 385 | ExpectEqual(Value(-6), GetMul(CreateRange(-10, -2), CreateRange(3, 5), false)); |
| 386 | ExpectEqual(Value(50), GetMul(CreateRange(-10, -2), CreateRange(-5, -3), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 387 | ExpectEqual(Value(), GetMul(CreateRange(-10, -2), CreateRange(-1, 1), false)); |
| 388 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(2, 10), false)); |
| 389 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-10, -2), false)); |
| 390 | ExpectEqual(Value(), GetMul(CreateRange(-1, 1), CreateRange(-1, 1), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 391 | } |
| 392 | |
| 393 | TEST_F(InductionVarRangeTest, GetDivMin) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 394 | ExpectEqual(Value(10), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), true)); |
| 395 | ExpectEqual(Value(-500), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 396 | ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), true)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 397 | ExpectEqual(Value(-500), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), true)); |
| 398 | ExpectEqual(Value(10), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), true)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 399 | ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), true)); |
| 400 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), true)); |
| 401 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, -40), true)); |
| 402 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), true)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 403 | } |
| 404 | |
| 405 | TEST_F(InductionVarRangeTest, GetDivMax) { |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 406 | ExpectEqual(Value(500), GetDiv(CreateRange(40, 1000), CreateRange(2, 4), false)); |
| 407 | ExpectEqual(Value(-10), GetDiv(CreateRange(40, 1000), CreateRange(-4, -2), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 408 | ExpectEqual(Value(), GetDiv(CreateRange(40, 1000), CreateRange(-1, 1), false)); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 409 | ExpectEqual(Value(-10), GetDiv(CreateRange(-1000, -40), CreateRange(2, 4), false)); |
| 410 | ExpectEqual(Value(500), GetDiv(CreateRange(-1000, -40), CreateRange(-4, -2), false)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 411 | ExpectEqual(Value(), GetDiv(CreateRange(-1000, -40), CreateRange(-1, 1), false)); |
| 412 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(40, 1000), false)); |
| 413 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1000, 40), false)); |
| 414 | ExpectEqual(Value(), GetDiv(CreateRange(-1, 1), CreateRange(-1, 1), false)); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 415 | } |
| 416 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 417 | TEST_F(InductionVarRangeTest, IsConstantRange) { |
| 418 | int32_t min_value; |
| 419 | int32_t max_value; |
| 420 | ASSERT_TRUE(IsConstantRange(CreateConst(12345), &min_value, &max_value)); |
| 421 | EXPECT_EQ(12345, min_value); |
| 422 | EXPECT_EQ(12345, max_value); |
| 423 | ASSERT_TRUE(IsConstantRange(CreateRange(1, 2), &min_value, &max_value)); |
| 424 | EXPECT_EQ(1, min_value); |
| 425 | EXPECT_EQ(2, max_value); |
| 426 | EXPECT_FALSE(IsConstantRange(CreateFetch(x_), &min_value, &max_value)); |
Aart Bik | 9401f53 | 2015-09-28 16:25:56 -0700 | [diff] [blame] | 427 | } |
| 428 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 429 | TEST_F(InductionVarRangeTest, AddValue) { |
| 430 | ExpectEqual(Value(110), AddValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 431 | ExpectEqual(Value(-5), AddValue(Value(x_, 1, -4), Value(x_, -1, -1))); |
| 432 | ExpectEqual(Value(x_, 3, -5), AddValue(Value(x_, 2, -4), Value(x_, 1, -1))); |
| 433 | ExpectEqual(Value(), AddValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 434 | ExpectEqual(Value(x_, 1, 23), AddValue(Value(x_, 1, 20), Value(3))); |
| 435 | ExpectEqual(Value(y_, 1, 5), AddValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 436 | const int32_t max_value = std::numeric_limits<int32_t>::max(); |
| 437 | ExpectEqual(Value(max_value), AddValue(Value(max_value - 5), Value(5))); |
| 438 | ExpectEqual(Value(), AddValue(Value(max_value - 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 439 | } |
| 440 | |
| 441 | TEST_F(InductionVarRangeTest, SubValue) { |
| 442 | ExpectEqual(Value(-90), SubValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 443 | ExpectEqual(Value(-3), SubValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 444 | ExpectEqual(Value(x_, 2, -3), SubValue(Value(x_, 3, -4), Value(x_, 1, -1))); |
| 445 | ExpectEqual(Value(), SubValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 446 | ExpectEqual(Value(x_, 1, 17), SubValue(Value(x_, 1, 20), Value(3))); |
| 447 | ExpectEqual(Value(y_, -4, 105), SubValue(Value(55), Value(y_, 4, -50))); |
Aart Bik | cd26feb | 2015-09-23 17:50:50 -0700 | [diff] [blame] | 448 | const int32_t min_value = std::numeric_limits<int32_t>::min(); |
| 449 | ExpectEqual(Value(min_value), SubValue(Value(min_value + 5), Value(5))); |
| 450 | ExpectEqual(Value(), SubValue(Value(min_value + 5), Value(6))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 451 | } |
| 452 | |
| 453 | TEST_F(InductionVarRangeTest, MulValue) { |
| 454 | ExpectEqual(Value(1000), MulValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 455 | ExpectEqual(Value(), MulValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 456 | ExpectEqual(Value(), MulValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 457 | ExpectEqual(Value(x_, 9, 60), MulValue(Value(x_, 3, 20), Value(3))); |
| 458 | ExpectEqual(Value(y_, 55, -110), MulValue(Value(55), Value(y_, 1, -2))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 459 | ExpectEqual(Value(), MulValue(Value(90000), Value(-90000))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 460 | } |
| 461 | |
| 462 | TEST_F(InductionVarRangeTest, DivValue) { |
| 463 | ExpectEqual(Value(25), DivValue(Value(100), Value(4))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 464 | ExpectEqual(Value(), DivValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 465 | ExpectEqual(Value(), DivValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 466 | ExpectEqual(Value(), DivValue(Value(x_, 12, 24), Value(3))); |
| 467 | ExpectEqual(Value(), DivValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | b3365e0 | 2015-09-21 14:45:05 -0700 | [diff] [blame] | 468 | ExpectEqual(Value(), DivValue(Value(1), Value(0))); // unsafe |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 469 | } |
| 470 | |
| 471 | TEST_F(InductionVarRangeTest, MinValue) { |
| 472 | ExpectEqual(Value(10), MinValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 473 | ExpectEqual(Value(x_, 1, -4), MinValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 474 | ExpectEqual(Value(x_, 4, -4), MinValue(Value(x_, 4, -4), Value(x_, 4, -1))); |
| 475 | ExpectEqual(Value(), MinValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 476 | ExpectEqual(Value(), MinValue(Value(x_, 1, 20), Value(3))); |
| 477 | ExpectEqual(Value(), MinValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | TEST_F(InductionVarRangeTest, MaxValue) { |
| 481 | ExpectEqual(Value(100), MaxValue(Value(10), Value(100))); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 482 | ExpectEqual(Value(x_, 1, -1), MaxValue(Value(x_, 1, -4), Value(x_, 1, -1))); |
| 483 | ExpectEqual(Value(x_, 4, -1), MaxValue(Value(x_, 4, -4), Value(x_, 4, -1))); |
| 484 | ExpectEqual(Value(), MaxValue(Value(x_, 1, 5), Value(y_, 1, -7))); |
| 485 | ExpectEqual(Value(), MaxValue(Value(x_, 1, 20), Value(3))); |
| 486 | ExpectEqual(Value(), MaxValue(Value(55), Value(y_, 1, -50))); |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 487 | } |
| 488 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 489 | // |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 490 | // Tests on public methods. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 491 | // |
| 492 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 493 | TEST_F(InductionVarRangeTest, ConstantTripCountUp) { |
| 494 | BuildLoop(0, graph_->GetIntConstant(1000), 1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 495 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 496 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 497 | Value v1, v2; |
| 498 | bool needs_finite_test = true; |
| 499 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 500 | // In context of header: known. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 501 | range_.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 502 | EXPECT_FALSE(needs_finite_test); |
| 503 | ExpectEqual(Value(0), v1); |
| 504 | ExpectEqual(Value(1000), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 505 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 506 | |
| 507 | // In context of loop-body: known. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 508 | range_.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 509 | EXPECT_FALSE(needs_finite_test); |
| 510 | ExpectEqual(Value(0), v1); |
| 511 | ExpectEqual(Value(999), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 512 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
| 513 | range_.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 514 | EXPECT_FALSE(needs_finite_test); |
| 515 | ExpectEqual(Value(1), v1); |
| 516 | ExpectEqual(Value(1000), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 517 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 518 | } |
| 519 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 520 | TEST_F(InductionVarRangeTest, ConstantTripCountDown) { |
| 521 | BuildLoop(1000, graph_->GetIntConstant(0), -1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 522 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 523 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 524 | Value v1, v2; |
| 525 | bool needs_finite_test = true; |
| 526 | |
| 527 | // In context of header: known. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 528 | range_.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 529 | EXPECT_FALSE(needs_finite_test); |
| 530 | ExpectEqual(Value(0), v1); |
| 531 | ExpectEqual(Value(1000), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 532 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 533 | |
| 534 | // In context of loop-body: known. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 535 | range_.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 536 | EXPECT_FALSE(needs_finite_test); |
| 537 | ExpectEqual(Value(1), v1); |
| 538 | ExpectEqual(Value(1000), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 539 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
| 540 | range_.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 541 | EXPECT_FALSE(needs_finite_test); |
| 542 | ExpectEqual(Value(0), v1); |
| 543 | ExpectEqual(Value(999), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 544 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 545 | } |
| 546 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 547 | TEST_F(InductionVarRangeTest, SymbolicTripCountUp) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 548 | BuildLoop(0, x_, 1); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 549 | PerformInductionVarAnalysis(); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 550 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 551 | Value v1, v2; |
| 552 | bool needs_finite_test = true; |
| 553 | bool needs_taken_test = true; |
| 554 | |
| 555 | // In context of header: upper unknown. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 556 | range_.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 557 | EXPECT_FALSE(needs_finite_test); |
| 558 | ExpectEqual(Value(0), v1); |
| 559 | ExpectEqual(Value(), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 560 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 561 | |
| 562 | // In context of loop-body: known. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 563 | range_.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 564 | EXPECT_FALSE(needs_finite_test); |
| 565 | ExpectEqual(Value(0), v1); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 566 | ExpectEqual(Value(x_, 1, -1), v2); |
| 567 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
| 568 | range_.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 569 | EXPECT_FALSE(needs_finite_test); |
| 570 | ExpectEqual(Value(1), v1); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 571 | ExpectEqual(Value(x_, 1, 0), v2); |
| 572 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 573 | |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 574 | HInstruction* lower = nullptr; |
| 575 | HInstruction* upper = nullptr; |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 576 | HInstruction* taken = nullptr; |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 577 | |
| 578 | // Can generate code in context of loop-body only. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 579 | EXPECT_FALSE(range_.CanGenerateCode( |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 580 | condition_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 581 | ASSERT_TRUE(range_.CanGenerateCode( |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 582 | increment_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test)); |
| 583 | EXPECT_FALSE(needs_finite_test); |
| 584 | EXPECT_TRUE(needs_taken_test); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 585 | |
| 586 | // Generates code. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 587 | range_.GenerateRangeCode( |
| 588 | increment_, condition_->InputAt(0), graph_, loop_preheader_, &lower, &upper); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 589 | |
| 590 | // Verify lower is 0+0. |
| 591 | ASSERT_TRUE(lower != nullptr); |
| 592 | ASSERT_TRUE(lower->IsAdd()); |
| 593 | ASSERT_TRUE(lower->InputAt(0)->IsIntConstant()); |
| 594 | EXPECT_EQ(0, lower->InputAt(0)->AsIntConstant()->GetValue()); |
| 595 | ASSERT_TRUE(lower->InputAt(1)->IsIntConstant()); |
| 596 | EXPECT_EQ(0, lower->InputAt(1)->AsIntConstant()->GetValue()); |
| 597 | |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 598 | // Verify upper is (V-1)+0. |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 599 | ASSERT_TRUE(upper != nullptr); |
| 600 | ASSERT_TRUE(upper->IsAdd()); |
| 601 | ASSERT_TRUE(upper->InputAt(0)->IsSub()); |
| 602 | EXPECT_TRUE(upper->InputAt(0)->InputAt(0)->IsParameterValue()); |
| 603 | ASSERT_TRUE(upper->InputAt(0)->InputAt(1)->IsIntConstant()); |
| 604 | EXPECT_EQ(1, upper->InputAt(0)->InputAt(1)->AsIntConstant()->GetValue()); |
| 605 | ASSERT_TRUE(upper->InputAt(1)->IsIntConstant()); |
| 606 | EXPECT_EQ(0, upper->InputAt(1)->AsIntConstant()->GetValue()); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 607 | |
| 608 | // Verify taken-test is 0<V. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 609 | range_.GenerateTakenTest(increment_, graph_, loop_preheader_, &taken); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 610 | ASSERT_TRUE(taken != nullptr); |
| 611 | ASSERT_TRUE(taken->IsLessThan()); |
| 612 | ASSERT_TRUE(taken->InputAt(0)->IsIntConstant()); |
| 613 | EXPECT_EQ(0, taken->InputAt(0)->AsIntConstant()->GetValue()); |
| 614 | EXPECT_TRUE(taken->InputAt(1)->IsParameterValue()); |
| 615 | } |
| 616 | |
| 617 | TEST_F(InductionVarRangeTest, SymbolicTripCountDown) { |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 618 | BuildLoop(1000, x_, -1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 619 | PerformInductionVarAnalysis(); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 620 | |
| 621 | Value v1, v2; |
| 622 | bool needs_finite_test = true; |
| 623 | bool needs_taken_test = true; |
| 624 | |
| 625 | // In context of header: lower unknown. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 626 | range_.GetInductionRange(condition_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 627 | EXPECT_FALSE(needs_finite_test); |
| 628 | ExpectEqual(Value(), v1); |
| 629 | ExpectEqual(Value(1000), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 630 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 631 | |
| 632 | // In context of loop-body: known. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 633 | range_.GetInductionRange(increment_, condition_->InputAt(0), &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 634 | EXPECT_FALSE(needs_finite_test); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 635 | ExpectEqual(Value(x_, 1, 1), v1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 636 | ExpectEqual(Value(1000), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 637 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
| 638 | range_.GetInductionRange(increment_, increment_, &v1, &v2, &needs_finite_test); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 639 | EXPECT_FALSE(needs_finite_test); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 640 | ExpectEqual(Value(x_, 1, 0), v1); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 641 | ExpectEqual(Value(999), v2); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 642 | EXPECT_FALSE(range_.RefineOuter(&v1, &v2)); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 643 | |
| 644 | HInstruction* lower = nullptr; |
| 645 | HInstruction* upper = nullptr; |
| 646 | HInstruction* taken = nullptr; |
| 647 | |
| 648 | // Can generate code in context of loop-body only. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 649 | EXPECT_FALSE(range_.CanGenerateCode( |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 650 | condition_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test)); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 651 | ASSERT_TRUE(range_.CanGenerateCode( |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 652 | increment_, condition_->InputAt(0), &needs_finite_test, &needs_taken_test)); |
| 653 | EXPECT_FALSE(needs_finite_test); |
| 654 | EXPECT_TRUE(needs_taken_test); |
| 655 | |
| 656 | // Generates code. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 657 | range_.GenerateRangeCode( |
| 658 | increment_, condition_->InputAt(0), graph_, loop_preheader_, &lower, &upper); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 659 | |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 660 | // Verify lower is 1000-((1000-V)-1). |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 661 | ASSERT_TRUE(lower != nullptr); |
| 662 | ASSERT_TRUE(lower->IsSub()); |
| 663 | ASSERT_TRUE(lower->InputAt(0)->IsIntConstant()); |
| 664 | EXPECT_EQ(1000, lower->InputAt(0)->AsIntConstant()->GetValue()); |
| 665 | lower = lower->InputAt(1); |
| 666 | ASSERT_TRUE(lower->IsSub()); |
| 667 | ASSERT_TRUE(lower->InputAt(1)->IsIntConstant()); |
| 668 | EXPECT_EQ(1, lower->InputAt(1)->AsIntConstant()->GetValue()); |
| 669 | lower = lower->InputAt(0); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 670 | ASSERT_TRUE(lower->IsSub()); |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 671 | ASSERT_TRUE(lower->InputAt(0)->IsIntConstant()); |
| 672 | EXPECT_EQ(1000, lower->InputAt(0)->AsIntConstant()->GetValue()); |
| 673 | EXPECT_TRUE(lower->InputAt(1)->IsParameterValue()); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 674 | |
| 675 | // Verify upper is 1000-0. |
| 676 | ASSERT_TRUE(upper != nullptr); |
| 677 | ASSERT_TRUE(upper->IsSub()); |
| 678 | ASSERT_TRUE(upper->InputAt(0)->IsIntConstant()); |
| 679 | EXPECT_EQ(1000, upper->InputAt(0)->AsIntConstant()->GetValue()); |
| 680 | ASSERT_TRUE(upper->InputAt(1)->IsIntConstant()); |
| 681 | EXPECT_EQ(0, upper->InputAt(1)->AsIntConstant()->GetValue()); |
| 682 | |
| 683 | // Verify taken-test is 1000>V. |
Aart Bik | 7d57d7f | 2015-12-09 14:39:48 -0800 | [diff] [blame] | 684 | range_.GenerateTakenTest(increment_, graph_, loop_preheader_, &taken); |
Aart Bik | 389b3db | 2015-10-28 14:23:40 -0700 | [diff] [blame] | 685 | ASSERT_TRUE(taken != nullptr); |
| 686 | ASSERT_TRUE(taken->IsGreaterThan()); |
| 687 | ASSERT_TRUE(taken->InputAt(0)->IsIntConstant()); |
| 688 | EXPECT_EQ(1000, taken->InputAt(0)->AsIntConstant()->GetValue()); |
| 689 | EXPECT_TRUE(taken->InputAt(1)->IsParameterValue()); |
Aart Bik | aec3cce | 2015-10-14 17:44:55 -0700 | [diff] [blame] | 690 | } |
| 691 | |
Aart Bik | d14c595 | 2015-09-08 15:25:15 -0700 | [diff] [blame] | 692 | } // namespace art |