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