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