1 //===- ConstantRangeTest.cpp - ConstantRange tests ------------------------===// 2 // 3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions. 4 // See https://llvm.org/LICENSE.txt for license information. 5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception 6 // 7 //===----------------------------------------------------------------------===// 8 9 #include "llvm/ADT/BitVector.h" 10 #include "llvm/ADT/SmallBitVector.h" 11 #include "llvm/IR/ConstantRange.h" 12 #include "llvm/IR/Instructions.h" 13 #include "llvm/IR/Operator.h" 14 #include "llvm/Support/KnownBits.h" 15 #include "gtest/gtest.h" 16 17 using namespace llvm; 18 19 namespace { 20 21 class ConstantRangeTest : public ::testing::Test { 22 protected: 23 static ConstantRange Full; 24 static ConstantRange Empty; 25 static ConstantRange One; 26 static ConstantRange Some; 27 static ConstantRange Wrap; 28 }; 29 30 template<typename Fn> 31 static void EnumerateConstantRanges(unsigned Bits, Fn TestFn) { 32 unsigned Max = 1 << Bits; 33 for (unsigned Lo = 0; Lo < Max; Lo++) { 34 for (unsigned Hi = 0; Hi < Max; Hi++) { 35 // Enforce ConstantRange invariant. 36 if (Lo == Hi && Lo != 0 && Lo != Max - 1) 37 continue; 38 39 ConstantRange CR(APInt(Bits, Lo), APInt(Bits, Hi)); 40 TestFn(CR); 41 } 42 } 43 } 44 45 template<typename Fn> 46 static void EnumerateTwoConstantRanges(unsigned Bits, Fn TestFn) { 47 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR1) { 48 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR2) { 49 TestFn(CR1, CR2); 50 }); 51 }); 52 } 53 54 template<typename Fn> 55 static void ForeachNumInConstantRange(const ConstantRange &CR, Fn TestFn) { 56 if (!CR.isEmptySet()) { 57 APInt N = CR.getLower(); 58 do TestFn(N); 59 while (++N != CR.getUpper()); 60 } 61 } 62 63 using PreferFn = llvm::function_ref<bool(const ConstantRange &, 64 const ConstantRange &)>; 65 66 bool PreferSmallest(const ConstantRange &CR1, const ConstantRange &CR2) { 67 return CR1.isSizeStrictlySmallerThan(CR2); 68 } 69 70 bool PreferSmallestUnsigned(const ConstantRange &CR1, 71 const ConstantRange &CR2) { 72 if (CR1.isWrappedSet() != CR2.isWrappedSet()) 73 return CR1.isWrappedSet() < CR2.isWrappedSet(); 74 return PreferSmallest(CR1, CR2); 75 } 76 77 bool PreferSmallestSigned(const ConstantRange &CR1, const ConstantRange &CR2) { 78 if (CR1.isSignWrappedSet() != CR2.isSignWrappedSet()) 79 return CR1.isSignWrappedSet() < CR2.isSignWrappedSet(); 80 return PreferSmallest(CR1, CR2); 81 } 82 83 bool PreferSmallestNonFullUnsigned(const ConstantRange &CR1, 84 const ConstantRange &CR2) { 85 if (CR1.isFullSet() != CR2.isFullSet()) 86 return CR1.isFullSet() < CR2.isFullSet(); 87 return PreferSmallestUnsigned(CR1, CR2); 88 } 89 90 bool PreferSmallestNonFullSigned(const ConstantRange &CR1, 91 const ConstantRange &CR2) { 92 if (CR1.isFullSet() != CR2.isFullSet()) 93 return CR1.isFullSet() < CR2.isFullSet(); 94 return PreferSmallestSigned(CR1, CR2); 95 } 96 97 98 99 // Check whether constant range CR is an optimal approximation of the set 100 // Elems under the given PreferenceFn. The preference function should return 101 // true if the first range argument is strictly preferred to the second one. 102 static void TestRange(const ConstantRange &CR, const SmallBitVector &Elems, 103 PreferFn PreferenceFn, ArrayRef<ConstantRange> Inputs) { 104 unsigned BitWidth = CR.getBitWidth(); 105 106 // Check conservative correctness. 107 for (unsigned Elem : Elems.set_bits()) { 108 EXPECT_TRUE(CR.contains(APInt(BitWidth, Elem))); 109 } 110 111 // Make sure we have at least one element for the code below. 112 if (Elems.none()) { 113 EXPECT_TRUE(CR.isEmptySet()); 114 return; 115 } 116 117 auto NotPreferred = [&](const ConstantRange &PossibleCR) { 118 if (!PreferenceFn(PossibleCR, CR)) 119 return testing::AssertionSuccess(); 120 121 testing::AssertionResult Result = testing::AssertionFailure(); 122 Result << "Inputs = "; 123 for (const ConstantRange &Input : Inputs) 124 Result << Input << ", "; 125 Result << "CR = " << CR << ", BetterCR = " << PossibleCR; 126 return Result; 127 }; 128 129 // Look at all pairs of adjacent elements and the slack-free ranges 130 // [Elem, PrevElem] they imply. Check that none of the ranges are strictly 131 // preferred over the computed range (they may have equal preference). 132 int FirstElem = Elems.find_first(); 133 int PrevElem = FirstElem, Elem; 134 do { 135 Elem = Elems.find_next(PrevElem); 136 if (Elem < 0) 137 Elem = FirstElem; // Wrap around to first element. 138 139 ConstantRange PossibleCR = 140 ConstantRange::getNonEmpty(APInt(BitWidth, Elem), 141 APInt(BitWidth, PrevElem) + 1); 142 // We get a full range any time PrevElem and Elem are adjacent. Avoid 143 // repeated checks by skipping here, and explicitly checking below instead. 144 if (!PossibleCR.isFullSet()) { 145 EXPECT_TRUE(NotPreferred(PossibleCR)); 146 } 147 148 PrevElem = Elem; 149 } while (Elem != FirstElem); 150 151 EXPECT_TRUE(NotPreferred(ConstantRange::getFull(BitWidth))); 152 } 153 154 using UnaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &)>; 155 using UnaryIntFn = llvm::function_ref<Optional<APInt>(const APInt &)>; 156 157 static void TestUnaryOpExhaustive(UnaryRangeFn RangeFn, UnaryIntFn IntFn, 158 PreferFn PreferenceFn = PreferSmallest) { 159 unsigned Bits = 4; 160 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { 161 SmallBitVector Elems(1 << Bits); 162 ForeachNumInConstantRange(CR, [&](const APInt &N) { 163 if (Optional<APInt> ResultN = IntFn(N)) 164 Elems.set(ResultN->getZExtValue()); 165 }); 166 TestRange(RangeFn(CR), Elems, PreferenceFn, {CR}); 167 }); 168 } 169 170 using BinaryRangeFn = llvm::function_ref<ConstantRange(const ConstantRange &, 171 const ConstantRange &)>; 172 using BinaryIntFn = llvm::function_ref<Optional<APInt>(const APInt &, 173 const APInt &)>; 174 175 static void TestBinaryOpExhaustive(BinaryRangeFn RangeFn, BinaryIntFn IntFn, 176 PreferFn PreferenceFn = PreferSmallest) { 177 unsigned Bits = 4; 178 EnumerateTwoConstantRanges( 179 Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) { 180 SmallBitVector Elems(1 << Bits); 181 ForeachNumInConstantRange(CR1, [&](const APInt &N1) { 182 ForeachNumInConstantRange(CR2, [&](const APInt &N2) { 183 if (Optional<APInt> ResultN = IntFn(N1, N2)) 184 Elems.set(ResultN->getZExtValue()); 185 }); 186 }); 187 TestRange(RangeFn(CR1, CR2), Elems, PreferenceFn, {CR1, CR2}); 188 }); 189 } 190 191 static void TestBinaryOpExhaustiveCorrectnessOnly(BinaryRangeFn RangeFn, 192 BinaryIntFn IntFn) { 193 unsigned Bits = 4; 194 EnumerateTwoConstantRanges( 195 Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) { 196 ConstantRange ResultCR = RangeFn(CR1, CR2); 197 ForeachNumInConstantRange(CR1, [&](const APInt &N1) { 198 ForeachNumInConstantRange(CR2, [&](const APInt &N2) { 199 if (Optional<APInt> ResultN = IntFn(N1, N2)) { 200 EXPECT_TRUE(ResultCR.contains(*ResultN)); 201 } 202 }); 203 }); 204 }); 205 } 206 207 struct OpRangeGathererBase { 208 void account(const APInt &N); 209 ConstantRange getRange(); 210 }; 211 212 struct UnsignedOpRangeGatherer : public OpRangeGathererBase { 213 APInt Min; 214 APInt Max; 215 216 UnsignedOpRangeGatherer(unsigned Bits) 217 : Min(APInt::getMaxValue(Bits)), Max(APInt::getMinValue(Bits)) {} 218 219 void account(const APInt &N) { 220 if (N.ult(Min)) 221 Min = N; 222 if (N.ugt(Max)) 223 Max = N; 224 } 225 226 ConstantRange getRange() { 227 if (Min.ugt(Max)) 228 return ConstantRange::getEmpty(Min.getBitWidth()); 229 return ConstantRange::getNonEmpty(Min, Max + 1); 230 } 231 }; 232 233 struct SignedOpRangeGatherer : public OpRangeGathererBase { 234 APInt Min; 235 APInt Max; 236 237 SignedOpRangeGatherer(unsigned Bits) 238 : Min(APInt::getSignedMaxValue(Bits)), 239 Max(APInt::getSignedMinValue(Bits)) {} 240 241 void account(const APInt &N) { 242 if (N.slt(Min)) 243 Min = N; 244 if (N.sgt(Max)) 245 Max = N; 246 } 247 248 ConstantRange getRange() { 249 if (Min.sgt(Max)) 250 return ConstantRange::getEmpty(Min.getBitWidth()); 251 return ConstantRange::getNonEmpty(Min, Max + 1); 252 } 253 }; 254 255 ConstantRange ConstantRangeTest::Full(16, true); 256 ConstantRange ConstantRangeTest::Empty(16, false); 257 ConstantRange ConstantRangeTest::One(APInt(16, 0xa)); 258 ConstantRange ConstantRangeTest::Some(APInt(16, 0xa), APInt(16, 0xaaa)); 259 ConstantRange ConstantRangeTest::Wrap(APInt(16, 0xaaa), APInt(16, 0xa)); 260 261 TEST_F(ConstantRangeTest, Basics) { 262 EXPECT_TRUE(Full.isFullSet()); 263 EXPECT_FALSE(Full.isEmptySet()); 264 EXPECT_TRUE(Full.inverse().isEmptySet()); 265 EXPECT_FALSE(Full.isWrappedSet()); 266 EXPECT_TRUE(Full.contains(APInt(16, 0x0))); 267 EXPECT_TRUE(Full.contains(APInt(16, 0x9))); 268 EXPECT_TRUE(Full.contains(APInt(16, 0xa))); 269 EXPECT_TRUE(Full.contains(APInt(16, 0xaa9))); 270 EXPECT_TRUE(Full.contains(APInt(16, 0xaaa))); 271 272 EXPECT_FALSE(Empty.isFullSet()); 273 EXPECT_TRUE(Empty.isEmptySet()); 274 EXPECT_TRUE(Empty.inverse().isFullSet()); 275 EXPECT_FALSE(Empty.isWrappedSet()); 276 EXPECT_FALSE(Empty.contains(APInt(16, 0x0))); 277 EXPECT_FALSE(Empty.contains(APInt(16, 0x9))); 278 EXPECT_FALSE(Empty.contains(APInt(16, 0xa))); 279 EXPECT_FALSE(Empty.contains(APInt(16, 0xaa9))); 280 EXPECT_FALSE(Empty.contains(APInt(16, 0xaaa))); 281 282 EXPECT_FALSE(One.isFullSet()); 283 EXPECT_FALSE(One.isEmptySet()); 284 EXPECT_FALSE(One.isWrappedSet()); 285 EXPECT_FALSE(One.contains(APInt(16, 0x0))); 286 EXPECT_FALSE(One.contains(APInt(16, 0x9))); 287 EXPECT_TRUE(One.contains(APInt(16, 0xa))); 288 EXPECT_FALSE(One.contains(APInt(16, 0xaa9))); 289 EXPECT_FALSE(One.contains(APInt(16, 0xaaa))); 290 EXPECT_FALSE(One.inverse().contains(APInt(16, 0xa))); 291 292 EXPECT_FALSE(Some.isFullSet()); 293 EXPECT_FALSE(Some.isEmptySet()); 294 EXPECT_FALSE(Some.isWrappedSet()); 295 EXPECT_FALSE(Some.contains(APInt(16, 0x0))); 296 EXPECT_FALSE(Some.contains(APInt(16, 0x9))); 297 EXPECT_TRUE(Some.contains(APInt(16, 0xa))); 298 EXPECT_TRUE(Some.contains(APInt(16, 0xaa9))); 299 EXPECT_FALSE(Some.contains(APInt(16, 0xaaa))); 300 301 EXPECT_FALSE(Wrap.isFullSet()); 302 EXPECT_FALSE(Wrap.isEmptySet()); 303 EXPECT_TRUE(Wrap.isWrappedSet()); 304 EXPECT_TRUE(Wrap.contains(APInt(16, 0x0))); 305 EXPECT_TRUE(Wrap.contains(APInt(16, 0x9))); 306 EXPECT_FALSE(Wrap.contains(APInt(16, 0xa))); 307 EXPECT_FALSE(Wrap.contains(APInt(16, 0xaa9))); 308 EXPECT_TRUE(Wrap.contains(APInt(16, 0xaaa))); 309 } 310 311 TEST_F(ConstantRangeTest, Equality) { 312 EXPECT_EQ(Full, Full); 313 EXPECT_EQ(Empty, Empty); 314 EXPECT_EQ(One, One); 315 EXPECT_EQ(Some, Some); 316 EXPECT_EQ(Wrap, Wrap); 317 EXPECT_NE(Full, Empty); 318 EXPECT_NE(Full, One); 319 EXPECT_NE(Full, Some); 320 EXPECT_NE(Full, Wrap); 321 EXPECT_NE(Empty, One); 322 EXPECT_NE(Empty, Some); 323 EXPECT_NE(Empty, Wrap); 324 EXPECT_NE(One, Some); 325 EXPECT_NE(One, Wrap); 326 EXPECT_NE(Some, Wrap); 327 } 328 329 TEST_F(ConstantRangeTest, SingleElement) { 330 EXPECT_EQ(Full.getSingleElement(), static_cast<APInt *>(nullptr)); 331 EXPECT_EQ(Empty.getSingleElement(), static_cast<APInt *>(nullptr)); 332 EXPECT_EQ(Full.getSingleMissingElement(), static_cast<APInt *>(nullptr)); 333 EXPECT_EQ(Empty.getSingleMissingElement(), static_cast<APInt *>(nullptr)); 334 335 EXPECT_EQ(*One.getSingleElement(), APInt(16, 0xa)); 336 EXPECT_EQ(Some.getSingleElement(), static_cast<APInt *>(nullptr)); 337 EXPECT_EQ(Wrap.getSingleElement(), static_cast<APInt *>(nullptr)); 338 339 EXPECT_EQ(One.getSingleMissingElement(), static_cast<APInt *>(nullptr)); 340 EXPECT_EQ(Some.getSingleMissingElement(), static_cast<APInt *>(nullptr)); 341 342 ConstantRange OneInverse = One.inverse(); 343 EXPECT_EQ(*OneInverse.getSingleMissingElement(), *One.getSingleElement()); 344 345 EXPECT_FALSE(Full.isSingleElement()); 346 EXPECT_FALSE(Empty.isSingleElement()); 347 EXPECT_TRUE(One.isSingleElement()); 348 EXPECT_FALSE(Some.isSingleElement()); 349 EXPECT_FALSE(Wrap.isSingleElement()); 350 } 351 352 TEST_F(ConstantRangeTest, GetMinsAndMaxes) { 353 EXPECT_EQ(Full.getUnsignedMax(), APInt(16, UINT16_MAX)); 354 EXPECT_EQ(One.getUnsignedMax(), APInt(16, 0xa)); 355 EXPECT_EQ(Some.getUnsignedMax(), APInt(16, 0xaa9)); 356 EXPECT_EQ(Wrap.getUnsignedMax(), APInt(16, UINT16_MAX)); 357 358 EXPECT_EQ(Full.getUnsignedMin(), APInt(16, 0)); 359 EXPECT_EQ(One.getUnsignedMin(), APInt(16, 0xa)); 360 EXPECT_EQ(Some.getUnsignedMin(), APInt(16, 0xa)); 361 EXPECT_EQ(Wrap.getUnsignedMin(), APInt(16, 0)); 362 363 EXPECT_EQ(Full.getSignedMax(), APInt(16, INT16_MAX)); 364 EXPECT_EQ(One.getSignedMax(), APInt(16, 0xa)); 365 EXPECT_EQ(Some.getSignedMax(), APInt(16, 0xaa9)); 366 EXPECT_EQ(Wrap.getSignedMax(), APInt(16, INT16_MAX)); 367 368 EXPECT_EQ(Full.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); 369 EXPECT_EQ(One.getSignedMin(), APInt(16, 0xa)); 370 EXPECT_EQ(Some.getSignedMin(), APInt(16, 0xa)); 371 EXPECT_EQ(Wrap.getSignedMin(), APInt(16, (uint64_t)INT16_MIN)); 372 373 // Found by Klee 374 EXPECT_EQ(ConstantRange(APInt(4, 7), APInt(4, 0)).getSignedMax(), 375 APInt(4, 7)); 376 } 377 378 TEST_F(ConstantRangeTest, SignWrapped) { 379 EXPECT_FALSE(Full.isSignWrappedSet()); 380 EXPECT_FALSE(Empty.isSignWrappedSet()); 381 EXPECT_FALSE(One.isSignWrappedSet()); 382 EXPECT_FALSE(Some.isSignWrappedSet()); 383 EXPECT_TRUE(Wrap.isSignWrappedSet()); 384 385 EXPECT_FALSE(ConstantRange(APInt(8, 127), APInt(8, 128)).isSignWrappedSet()); 386 EXPECT_TRUE(ConstantRange(APInt(8, 127), APInt(8, 129)).isSignWrappedSet()); 387 EXPECT_FALSE(ConstantRange(APInt(8, 128), APInt(8, 129)).isSignWrappedSet()); 388 EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 9)).isSignWrappedSet()); 389 EXPECT_TRUE(ConstantRange(APInt(8, 10), APInt(8, 250)).isSignWrappedSet()); 390 EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 10)).isSignWrappedSet()); 391 EXPECT_FALSE(ConstantRange(APInt(8, 250), APInt(8, 251)).isSignWrappedSet()); 392 } 393 394 TEST_F(ConstantRangeTest, UpperWrapped) { 395 // The behavior here is the same as for isWrappedSet() / isSignWrappedSet(). 396 EXPECT_FALSE(Full.isUpperWrapped()); 397 EXPECT_FALSE(Empty.isUpperWrapped()); 398 EXPECT_FALSE(One.isUpperWrapped()); 399 EXPECT_FALSE(Some.isUpperWrapped()); 400 EXPECT_TRUE(Wrap.isUpperWrapped()); 401 EXPECT_FALSE(Full.isUpperSignWrapped()); 402 EXPECT_FALSE(Empty.isUpperSignWrapped()); 403 EXPECT_FALSE(One.isUpperSignWrapped()); 404 EXPECT_FALSE(Some.isUpperSignWrapped()); 405 EXPECT_TRUE(Wrap.isUpperSignWrapped()); 406 407 // The behavior differs if Upper is the Min/SignedMin value. 408 ConstantRange CR1(APInt(8, 42), APInt::getMinValue(8)); 409 EXPECT_FALSE(CR1.isWrappedSet()); 410 EXPECT_TRUE(CR1.isUpperWrapped()); 411 412 ConstantRange CR2(APInt(8, 42), APInt::getSignedMinValue(8)); 413 EXPECT_FALSE(CR2.isSignWrappedSet()); 414 EXPECT_TRUE(CR2.isUpperSignWrapped()); 415 } 416 417 TEST_F(ConstantRangeTest, Trunc) { 418 ConstantRange TFull = Full.truncate(10); 419 ConstantRange TEmpty = Empty.truncate(10); 420 ConstantRange TOne = One.truncate(10); 421 ConstantRange TSome = Some.truncate(10); 422 ConstantRange TWrap = Wrap.truncate(10); 423 EXPECT_TRUE(TFull.isFullSet()); 424 EXPECT_TRUE(TEmpty.isEmptySet()); 425 EXPECT_EQ(TOne, ConstantRange(One.getLower().trunc(10), 426 One.getUpper().trunc(10))); 427 EXPECT_TRUE(TSome.isFullSet()); 428 EXPECT_TRUE(TWrap.isFullSet()); 429 430 // trunc([2, 5), 3->2) = [2, 1) 431 ConstantRange TwoFive(APInt(3, 2), APInt(3, 5)); 432 EXPECT_EQ(TwoFive.truncate(2), ConstantRange(APInt(2, 2), APInt(2, 1))); 433 434 // trunc([2, 6), 3->2) = full 435 ConstantRange TwoSix(APInt(3, 2), APInt(3, 6)); 436 EXPECT_TRUE(TwoSix.truncate(2).isFullSet()); 437 438 // trunc([5, 7), 3->2) = [1, 3) 439 ConstantRange FiveSeven(APInt(3, 5), APInt(3, 7)); 440 EXPECT_EQ(FiveSeven.truncate(2), ConstantRange(APInt(2, 1), APInt(2, 3))); 441 442 // trunc([7, 1), 3->2) = [3, 1) 443 ConstantRange SevenOne(APInt(3, 7), APInt(3, 1)); 444 EXPECT_EQ(SevenOne.truncate(2), ConstantRange(APInt(2, 3), APInt(2, 1))); 445 } 446 447 TEST_F(ConstantRangeTest, ZExt) { 448 ConstantRange ZFull = Full.zeroExtend(20); 449 ConstantRange ZEmpty = Empty.zeroExtend(20); 450 ConstantRange ZOne = One.zeroExtend(20); 451 ConstantRange ZSome = Some.zeroExtend(20); 452 ConstantRange ZWrap = Wrap.zeroExtend(20); 453 EXPECT_EQ(ZFull, ConstantRange(APInt(20, 0), APInt(20, 0x10000))); 454 EXPECT_TRUE(ZEmpty.isEmptySet()); 455 EXPECT_EQ(ZOne, ConstantRange(One.getLower().zext(20), 456 One.getUpper().zext(20))); 457 EXPECT_EQ(ZSome, ConstantRange(Some.getLower().zext(20), 458 Some.getUpper().zext(20))); 459 EXPECT_EQ(ZWrap, ConstantRange(APInt(20, 0), APInt(20, 0x10000))); 460 461 // zext([5, 0), 3->7) = [5, 8) 462 ConstantRange FiveZero(APInt(3, 5), APInt(3, 0)); 463 EXPECT_EQ(FiveZero.zeroExtend(7), ConstantRange(APInt(7, 5), APInt(7, 8))); 464 } 465 466 TEST_F(ConstantRangeTest, SExt) { 467 ConstantRange SFull = Full.signExtend(20); 468 ConstantRange SEmpty = Empty.signExtend(20); 469 ConstantRange SOne = One.signExtend(20); 470 ConstantRange SSome = Some.signExtend(20); 471 ConstantRange SWrap = Wrap.signExtend(20); 472 EXPECT_EQ(SFull, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true), 473 APInt(20, INT16_MAX + 1, true))); 474 EXPECT_TRUE(SEmpty.isEmptySet()); 475 EXPECT_EQ(SOne, ConstantRange(One.getLower().sext(20), 476 One.getUpper().sext(20))); 477 EXPECT_EQ(SSome, ConstantRange(Some.getLower().sext(20), 478 Some.getUpper().sext(20))); 479 EXPECT_EQ(SWrap, ConstantRange(APInt(20, (uint64_t)INT16_MIN, true), 480 APInt(20, INT16_MAX + 1, true))); 481 482 EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, 140)).signExtend(16), 483 ConstantRange(APInt(16, -128), APInt(16, 128))); 484 485 EXPECT_EQ(ConstantRange(APInt(16, 0x0200), APInt(16, 0x8000)).signExtend(19), 486 ConstantRange(APInt(19, 0x0200), APInt(19, 0x8000))); 487 } 488 489 TEST_F(ConstantRangeTest, IntersectWith) { 490 EXPECT_EQ(Empty.intersectWith(Full), Empty); 491 EXPECT_EQ(Empty.intersectWith(Empty), Empty); 492 EXPECT_EQ(Empty.intersectWith(One), Empty); 493 EXPECT_EQ(Empty.intersectWith(Some), Empty); 494 EXPECT_EQ(Empty.intersectWith(Wrap), Empty); 495 EXPECT_EQ(Full.intersectWith(Full), Full); 496 EXPECT_EQ(Some.intersectWith(Some), Some); 497 EXPECT_EQ(Some.intersectWith(One), One); 498 EXPECT_EQ(Full.intersectWith(One), One); 499 EXPECT_EQ(Full.intersectWith(Some), Some); 500 EXPECT_EQ(Some.intersectWith(Wrap), Empty); 501 EXPECT_EQ(One.intersectWith(Wrap), Empty); 502 EXPECT_EQ(One.intersectWith(Wrap), Wrap.intersectWith(One)); 503 504 // Klee generated testcase from PR4545. 505 // The intersection of i16 [4, 2) and [6, 5) is disjoint, looking like 506 // 01..4.6789ABCDEF where the dots represent values not in the intersection. 507 ConstantRange LHS(APInt(16, 4), APInt(16, 2)); 508 ConstantRange RHS(APInt(16, 6), APInt(16, 5)); 509 EXPECT_TRUE(LHS.intersectWith(RHS) == LHS); 510 511 // previous bug: intersection of [min, 3) and [2, max) should be 2 512 LHS = ConstantRange(APInt(32, -2147483646), APInt(32, 3)); 513 RHS = ConstantRange(APInt(32, 2), APInt(32, 2147483646)); 514 EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2))); 515 516 // [2, 0) /\ [4, 3) = [2, 0) 517 LHS = ConstantRange(APInt(32, 2), APInt(32, 0)); 518 RHS = ConstantRange(APInt(32, 4), APInt(32, 3)); 519 EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 2), APInt(32, 0))); 520 521 // [2, 0) /\ [4, 2) = [4, 0) 522 LHS = ConstantRange(APInt(32, 2), APInt(32, 0)); 523 RHS = ConstantRange(APInt(32, 4), APInt(32, 2)); 524 EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 0))); 525 526 // [4, 2) /\ [5, 1) = [5, 1) 527 LHS = ConstantRange(APInt(32, 4), APInt(32, 2)); 528 RHS = ConstantRange(APInt(32, 5), APInt(32, 1)); 529 EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 5), APInt(32, 1))); 530 531 // [2, 0) /\ [7, 4) = [7, 4) 532 LHS = ConstantRange(APInt(32, 2), APInt(32, 0)); 533 RHS = ConstantRange(APInt(32, 7), APInt(32, 4)); 534 EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 7), APInt(32, 4))); 535 536 // [4, 2) /\ [1, 0) = [1, 0) 537 LHS = ConstantRange(APInt(32, 4), APInt(32, 2)); 538 RHS = ConstantRange(APInt(32, 1), APInt(32, 0)); 539 EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 4), APInt(32, 2))); 540 541 // [15, 0) /\ [7, 6) = [15, 0) 542 LHS = ConstantRange(APInt(32, 15), APInt(32, 0)); 543 RHS = ConstantRange(APInt(32, 7), APInt(32, 6)); 544 EXPECT_EQ(LHS.intersectWith(RHS), ConstantRange(APInt(32, 15), APInt(32, 0))); 545 } 546 547 template<typename Fn1, typename Fn2> 548 void testBinarySetOperationExhaustive(Fn1 OpFn, Fn2 InResultFn) { 549 unsigned Bits = 4; 550 EnumerateTwoConstantRanges(Bits, 551 [=](const ConstantRange &CR1, const ConstantRange &CR2) { 552 SmallBitVector Elems(1 << Bits); 553 APInt Num(Bits, 0); 554 for (unsigned I = 0, Limit = 1 << Bits; I < Limit; ++I, ++Num) 555 if (InResultFn(CR1, CR2, Num)) 556 Elems.set(Num.getZExtValue()); 557 558 ConstantRange SmallestCR = OpFn(CR1, CR2, ConstantRange::Smallest); 559 TestRange(SmallestCR, Elems, PreferSmallest, {CR1, CR2}); 560 561 ConstantRange UnsignedCR = OpFn(CR1, CR2, ConstantRange::Unsigned); 562 TestRange(UnsignedCR, Elems, PreferSmallestNonFullUnsigned, {CR1, CR2}); 563 564 ConstantRange SignedCR = OpFn(CR1, CR2, ConstantRange::Signed); 565 TestRange(SignedCR, Elems, PreferSmallestNonFullSigned, {CR1, CR2}); 566 }); 567 } 568 569 TEST_F(ConstantRangeTest, IntersectWithExhaustive) { 570 testBinarySetOperationExhaustive( 571 [](const ConstantRange &CR1, const ConstantRange &CR2, 572 ConstantRange::PreferredRangeType Type) { 573 return CR1.intersectWith(CR2, Type); 574 }, 575 [](const ConstantRange &CR1, const ConstantRange &CR2, const APInt &N) { 576 return CR1.contains(N) && CR2.contains(N); 577 }); 578 } 579 580 TEST_F(ConstantRangeTest, UnionWithExhaustive) { 581 testBinarySetOperationExhaustive( 582 [](const ConstantRange &CR1, const ConstantRange &CR2, 583 ConstantRange::PreferredRangeType Type) { 584 return CR1.unionWith(CR2, Type); 585 }, 586 [](const ConstantRange &CR1, const ConstantRange &CR2, const APInt &N) { 587 return CR1.contains(N) || CR2.contains(N); 588 }); 589 } 590 591 TEST_F(ConstantRangeTest, UnionWith) { 592 EXPECT_EQ(Wrap.unionWith(One), 593 ConstantRange(APInt(16, 0xaaa), APInt(16, 0xb))); 594 EXPECT_EQ(One.unionWith(Wrap), Wrap.unionWith(One)); 595 EXPECT_EQ(Empty.unionWith(Empty), Empty); 596 EXPECT_EQ(Full.unionWith(Full), Full); 597 EXPECT_EQ(Some.unionWith(Wrap), Full); 598 599 // PR4545 600 EXPECT_EQ(ConstantRange(APInt(16, 14), APInt(16, 1)).unionWith( 601 ConstantRange(APInt(16, 0), APInt(16, 8))), 602 ConstantRange(APInt(16, 14), APInt(16, 8))); 603 EXPECT_EQ(ConstantRange(APInt(16, 6), APInt(16, 4)).unionWith( 604 ConstantRange(APInt(16, 4), APInt(16, 0))), 605 ConstantRange::getFull(16)); 606 EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 0)).unionWith( 607 ConstantRange(APInt(16, 2), APInt(16, 1))), 608 ConstantRange::getFull(16)); 609 } 610 611 TEST_F(ConstantRangeTest, SetDifference) { 612 EXPECT_EQ(Full.difference(Empty), Full); 613 EXPECT_EQ(Full.difference(Full), Empty); 614 EXPECT_EQ(Empty.difference(Empty), Empty); 615 EXPECT_EQ(Empty.difference(Full), Empty); 616 617 ConstantRange A(APInt(16, 3), APInt(16, 7)); 618 ConstantRange B(APInt(16, 5), APInt(16, 9)); 619 ConstantRange C(APInt(16, 3), APInt(16, 5)); 620 ConstantRange D(APInt(16, 7), APInt(16, 9)); 621 ConstantRange E(APInt(16, 5), APInt(16, 4)); 622 ConstantRange F(APInt(16, 7), APInt(16, 3)); 623 EXPECT_EQ(A.difference(B), C); 624 EXPECT_EQ(B.difference(A), D); 625 EXPECT_EQ(E.difference(A), F); 626 } 627 628 TEST_F(ConstantRangeTest, getActiveBits) { 629 unsigned Bits = 4; 630 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { 631 unsigned Exact = 0; 632 ForeachNumInConstantRange(CR, [&](const APInt &N) { 633 Exact = std::max(Exact, N.getActiveBits()); 634 }); 635 636 unsigned ResultCR = CR.getActiveBits(); 637 EXPECT_EQ(Exact, ResultCR); 638 }); 639 } 640 TEST_F(ConstantRangeTest, losslessUnsignedTruncationZeroext) { 641 unsigned Bits = 4; 642 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { 643 unsigned MinBitWidth = CR.getActiveBits(); 644 if (MinBitWidth == 0) { 645 EXPECT_TRUE(CR.isEmptySet() || (CR.isSingleElement() && 646 CR.getSingleElement()->isNullValue())); 647 return; 648 } 649 if (MinBitWidth == Bits) 650 return; 651 EXPECT_EQ(CR, CR.truncate(MinBitWidth).zeroExtend(Bits)); 652 }); 653 } 654 655 TEST_F(ConstantRangeTest, getMinSignedBits) { 656 unsigned Bits = 4; 657 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { 658 unsigned Exact = 0; 659 ForeachNumInConstantRange(CR, [&](const APInt &N) { 660 Exact = std::max(Exact, N.getMinSignedBits()); 661 }); 662 663 unsigned ResultCR = CR.getMinSignedBits(); 664 EXPECT_EQ(Exact, ResultCR); 665 }); 666 } 667 TEST_F(ConstantRangeTest, losslessSignedTruncationSignext) { 668 unsigned Bits = 4; 669 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { 670 unsigned MinBitWidth = CR.getMinSignedBits(); 671 if (MinBitWidth == 0) { 672 EXPECT_TRUE(CR.isEmptySet()); 673 return; 674 } 675 if (MinBitWidth == Bits) 676 return; 677 EXPECT_EQ(CR, CR.truncate(MinBitWidth).signExtend(Bits)); 678 }); 679 } 680 681 TEST_F(ConstantRangeTest, SubtractAPInt) { 682 EXPECT_EQ(Full.subtract(APInt(16, 4)), Full); 683 EXPECT_EQ(Empty.subtract(APInt(16, 4)), Empty); 684 EXPECT_EQ(Some.subtract(APInt(16, 4)), 685 ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6))); 686 EXPECT_EQ(Wrap.subtract(APInt(16, 4)), 687 ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6))); 688 EXPECT_EQ(One.subtract(APInt(16, 4)), 689 ConstantRange(APInt(16, 0x6))); 690 } 691 692 TEST_F(ConstantRangeTest, Add) { 693 EXPECT_EQ(Full.add(APInt(16, 4)), Full); 694 EXPECT_EQ(Full.add(Full), Full); 695 EXPECT_EQ(Full.add(Empty), Empty); 696 EXPECT_EQ(Full.add(One), Full); 697 EXPECT_EQ(Full.add(Some), Full); 698 EXPECT_EQ(Full.add(Wrap), Full); 699 EXPECT_EQ(Empty.add(Empty), Empty); 700 EXPECT_EQ(Empty.add(One), Empty); 701 EXPECT_EQ(Empty.add(Some), Empty); 702 EXPECT_EQ(Empty.add(Wrap), Empty); 703 EXPECT_EQ(Empty.add(APInt(16, 4)), Empty); 704 EXPECT_EQ(Some.add(APInt(16, 4)), 705 ConstantRange(APInt(16, 0xe), APInt(16, 0xaae))); 706 EXPECT_EQ(Wrap.add(APInt(16, 4)), 707 ConstantRange(APInt(16, 0xaae), APInt(16, 0xe))); 708 EXPECT_EQ(One.add(APInt(16, 4)), 709 ConstantRange(APInt(16, 0xe))); 710 711 TestBinaryOpExhaustive( 712 [](const ConstantRange &CR1, const ConstantRange &CR2) { 713 return CR1.add(CR2); 714 }, 715 [](const APInt &N1, const APInt &N2) { 716 return N1 + N2; 717 }); 718 } 719 720 template <typename Fn1, typename Fn2> 721 static void TestAddWithNoSignedWrapExhaustive(Fn1 RangeFn, Fn2 IntFn) { 722 unsigned Bits = 4; 723 EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1, 724 const ConstantRange &CR2) { 725 ConstantRange CR = RangeFn(CR1, CR2); 726 SignedOpRangeGatherer R(CR.getBitWidth()); 727 bool AllOverflow = true; 728 ForeachNumInConstantRange(CR1, [&](const APInt &N1) { 729 ForeachNumInConstantRange(CR2, [&](const APInt &N2) { 730 bool IsOverflow = false; 731 APInt N = IntFn(IsOverflow, N1, N2); 732 if (!IsOverflow) { 733 AllOverflow = false; 734 R.account(N); 735 EXPECT_TRUE(CR.contains(N)); 736 } 737 }); 738 }); 739 740 EXPECT_EQ(CR.isEmptySet(), AllOverflow); 741 742 if (CR1.isSignWrappedSet() || CR2.isSignWrappedSet()) 743 return; 744 745 ConstantRange Exact = R.getRange(); 746 EXPECT_EQ(Exact, CR); 747 }); 748 } 749 750 template <typename Fn1, typename Fn2> 751 static void TestAddWithNoUnsignedWrapExhaustive(Fn1 RangeFn, Fn2 IntFn) { 752 unsigned Bits = 4; 753 EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1, 754 const ConstantRange &CR2) { 755 ConstantRange CR = RangeFn(CR1, CR2); 756 UnsignedOpRangeGatherer R(CR.getBitWidth()); 757 bool AllOverflow = true; 758 ForeachNumInConstantRange(CR1, [&](const APInt &N1) { 759 ForeachNumInConstantRange(CR2, [&](const APInt &N2) { 760 bool IsOverflow = false; 761 APInt N = IntFn(IsOverflow, N1, N2); 762 if (!IsOverflow) { 763 AllOverflow = false; 764 R.account(N); 765 EXPECT_TRUE(CR.contains(N)); 766 } 767 }); 768 }); 769 770 EXPECT_EQ(CR.isEmptySet(), AllOverflow); 771 772 if (CR1.isWrappedSet() || CR2.isWrappedSet()) 773 return; 774 775 ConstantRange Exact = R.getRange(); 776 EXPECT_EQ(Exact, CR); 777 }); 778 } 779 780 template <typename Fn1, typename Fn2, typename Fn3> 781 static void TestAddWithNoSignedUnsignedWrapExhaustive(Fn1 RangeFn, 782 Fn2 IntFnSigned, 783 Fn3 IntFnUnsigned) { 784 unsigned Bits = 4; 785 EnumerateTwoConstantRanges( 786 Bits, [&](const ConstantRange &CR1, const ConstantRange &CR2) { 787 ConstantRange CR = RangeFn(CR1, CR2); 788 UnsignedOpRangeGatherer UR(CR.getBitWidth()); 789 SignedOpRangeGatherer SR(CR.getBitWidth()); 790 bool AllOverflow = true; 791 ForeachNumInConstantRange(CR1, [&](const APInt &N1) { 792 ForeachNumInConstantRange(CR2, [&](const APInt &N2) { 793 bool IsOverflow = false, IsSignedOverflow = false; 794 APInt N = IntFnSigned(IsSignedOverflow, N1, N2); 795 (void) IntFnUnsigned(IsOverflow, N1, N2); 796 if (!IsSignedOverflow && !IsOverflow) { 797 AllOverflow = false; 798 UR.account(N); 799 SR.account(N); 800 EXPECT_TRUE(CR.contains(N)); 801 } 802 }); 803 }); 804 805 EXPECT_EQ(CR.isEmptySet(), AllOverflow); 806 807 if (CR1.isWrappedSet() || CR2.isWrappedSet() || 808 CR1.isSignWrappedSet() || CR2.isSignWrappedSet()) 809 return; 810 811 ConstantRange ExactUnsignedCR = UR.getRange(); 812 ConstantRange ExactSignedCR = SR.getRange(); 813 814 if (ExactUnsignedCR.isEmptySet() || ExactSignedCR.isEmptySet()) { 815 EXPECT_TRUE(CR.isEmptySet()); 816 return; 817 } 818 819 ConstantRange Exact = ExactSignedCR.intersectWith(ExactUnsignedCR); 820 EXPECT_EQ(Exact, CR); 821 }); 822 } 823 824 TEST_F(ConstantRangeTest, AddWithNoWrap) { 825 typedef OverflowingBinaryOperator OBO; 826 EXPECT_EQ(Empty.addWithNoWrap(Some, OBO::NoSignedWrap), Empty); 827 EXPECT_EQ(Some.addWithNoWrap(Empty, OBO::NoSignedWrap), Empty); 828 EXPECT_EQ(Full.addWithNoWrap(Full, OBO::NoSignedWrap), Full); 829 EXPECT_NE(Full.addWithNoWrap(Some, OBO::NoSignedWrap), Full); 830 EXPECT_NE(Some.addWithNoWrap(Full, OBO::NoSignedWrap), Full); 831 EXPECT_EQ(Full.addWithNoWrap(ConstantRange(APInt(16, 1), APInt(16, 2)), 832 OBO::NoSignedWrap), 833 ConstantRange(APInt(16, INT16_MIN + 1), APInt(16, INT16_MIN))); 834 EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 2)) 835 .addWithNoWrap(Full, OBO::NoSignedWrap), 836 ConstantRange(APInt(16, INT16_MIN + 1), APInt(16, INT16_MIN))); 837 EXPECT_EQ(Full.addWithNoWrap(ConstantRange(APInt(16, -1), APInt(16, 0)), 838 OBO::NoSignedWrap), 839 ConstantRange(APInt(16, INT16_MIN), APInt(16, INT16_MAX))); 840 EXPECT_EQ(ConstantRange(APInt(8, 100), APInt(8, 120)) 841 .addWithNoWrap(ConstantRange(APInt(8, 120), APInt(8, 123)), 842 OBO::NoSignedWrap), 843 ConstantRange(8, false)); 844 EXPECT_EQ(ConstantRange(APInt(8, -120), APInt(8, -100)) 845 .addWithNoWrap(ConstantRange(APInt(8, -110), APInt(8, -100)), 846 OBO::NoSignedWrap), 847 ConstantRange(8, false)); 848 EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101)) 849 .addWithNoWrap(ConstantRange(APInt(8, -128), APInt(8, 28)), 850 OBO::NoSignedWrap), 851 ConstantRange(8, true)); 852 EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101)) 853 .addWithNoWrap(ConstantRange(APInt(8, -120), APInt(8, 29)), 854 OBO::NoSignedWrap), 855 ConstantRange(APInt(8, -120), APInt(8, -128))); 856 EXPECT_EQ(ConstantRange(APInt(8, -50), APInt(8, 50)) 857 .addWithNoWrap(ConstantRange(APInt(8, 10), APInt(8, 20)), 858 OBO::NoSignedWrap), 859 ConstantRange(APInt(8, -40), APInt(8, 69))); 860 EXPECT_EQ(ConstantRange(APInt(8, 10), APInt(8, 20)) 861 .addWithNoWrap(ConstantRange(APInt(8, -50), APInt(8, 50)), 862 OBO::NoSignedWrap), 863 ConstantRange(APInt(8, -40), APInt(8, 69))); 864 EXPECT_EQ(ConstantRange(APInt(8, 120), APInt(8, -10)) 865 .addWithNoWrap(ConstantRange(APInt(8, 5), APInt(8, 20)), 866 OBO::NoSignedWrap), 867 ConstantRange(APInt(8, 125), APInt(8, 9))); 868 EXPECT_EQ(ConstantRange(APInt(8, 5), APInt(8, 20)) 869 .addWithNoWrap(ConstantRange(APInt(8, 120), APInt(8, -10)), 870 OBO::NoSignedWrap), 871 ConstantRange(APInt(8, 125), APInt(8, 9))); 872 873 TestAddWithNoSignedWrapExhaustive( 874 [](const ConstantRange &CR1, const ConstantRange &CR2) { 875 return CR1.addWithNoWrap(CR2, OBO::NoSignedWrap); 876 }, 877 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 878 return N1.sadd_ov(N2, IsOverflow); 879 }); 880 881 EXPECT_EQ(Empty.addWithNoWrap(Some, OBO::NoUnsignedWrap), Empty); 882 EXPECT_EQ(Some.addWithNoWrap(Empty, OBO::NoUnsignedWrap), Empty); 883 EXPECT_EQ(Full.addWithNoWrap(Full, OBO::NoUnsignedWrap), Full); 884 EXPECT_NE(Full.addWithNoWrap(Some, OBO::NoUnsignedWrap), Full); 885 EXPECT_NE(Some.addWithNoWrap(Full, OBO::NoUnsignedWrap), Full); 886 EXPECT_EQ(Full.addWithNoWrap(ConstantRange(APInt(16, 1), APInt(16, 2)), 887 OBO::NoUnsignedWrap), 888 ConstantRange(APInt(16, 1), APInt(16, 0))); 889 EXPECT_EQ(ConstantRange(APInt(16, 1), APInt(16, 2)) 890 .addWithNoWrap(Full, OBO::NoUnsignedWrap), 891 ConstantRange(APInt(16, 1), APInt(16, 0))); 892 EXPECT_EQ(ConstantRange(APInt(8, 200), APInt(8, 220)) 893 .addWithNoWrap(ConstantRange(APInt(8, 100), APInt(8, 123)), 894 OBO::NoUnsignedWrap), 895 ConstantRange(8, false)); 896 EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101)) 897 .addWithNoWrap(ConstantRange(APInt(8, 0), APInt(8, 156)), 898 OBO::NoUnsignedWrap), 899 ConstantRange(8, true)); 900 EXPECT_EQ(ConstantRange(APInt(8, 0), APInt(8, 101)) 901 .addWithNoWrap(ConstantRange(APInt(8, 10), APInt(8, 29)), 902 OBO::NoUnsignedWrap), 903 ConstantRange(APInt(8, 10), APInt(8, 129))); 904 EXPECT_EQ(ConstantRange(APInt(8, 20), APInt(8, 10)) 905 .addWithNoWrap(ConstantRange(APInt(8, 50), APInt(8, 200)), 906 OBO::NoUnsignedWrap), 907 ConstantRange(APInt(8, 50), APInt(8, 0))); 908 EXPECT_EQ(ConstantRange(APInt(8, 10), APInt(8, 20)) 909 .addWithNoWrap(ConstantRange(APInt(8, 50), APInt(8, 200)), 910 OBO::NoUnsignedWrap), 911 ConstantRange(APInt(8, 60), APInt(8, -37))); 912 EXPECT_EQ(ConstantRange(APInt(8, 20), APInt(8, -30)) 913 .addWithNoWrap(ConstantRange(APInt(8, 5), APInt(8, 20)), 914 OBO::NoUnsignedWrap), 915 ConstantRange(APInt(8, 25), APInt(8, -11))); 916 EXPECT_EQ(ConstantRange(APInt(8, 5), APInt(8, 20)) 917 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, -30)), 918 OBO::NoUnsignedWrap), 919 ConstantRange(APInt(8, 25), APInt(8, -11))); 920 921 TestAddWithNoUnsignedWrapExhaustive( 922 [](const ConstantRange &CR1, const ConstantRange &CR2) { 923 return CR1.addWithNoWrap(CR2, OBO::NoUnsignedWrap); 924 }, 925 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 926 return N1.uadd_ov(N2, IsOverflow); 927 }); 928 929 EXPECT_EQ(ConstantRange(APInt(8, 50), APInt(8, 100)) 930 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 70)), 931 OBO::NoSignedWrap), 932 ConstantRange(APInt(8, 70), APInt(8, -128))); 933 EXPECT_EQ(ConstantRange(APInt(8, 50), APInt(8, 100)) 934 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 70)), 935 OBO::NoUnsignedWrap), 936 ConstantRange(APInt(8, 70), APInt(8, 169))); 937 EXPECT_EQ(ConstantRange(APInt(8, 50), APInt(8, 100)) 938 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 70)), 939 OBO::NoUnsignedWrap | OBO::NoSignedWrap), 940 ConstantRange(APInt(8, 70), APInt(8, -128))); 941 942 EXPECT_EQ(ConstantRange(APInt(8, -100), APInt(8, -50)) 943 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 30)), 944 OBO::NoSignedWrap), 945 ConstantRange(APInt(8, -80), APInt(8, -21))); 946 EXPECT_EQ(ConstantRange(APInt(8, -100), APInt(8, -50)) 947 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 30)), 948 OBO::NoUnsignedWrap), 949 ConstantRange(APInt(8, 176), APInt(8, 235))); 950 EXPECT_EQ(ConstantRange(APInt(8, -100), APInt(8, -50)) 951 .addWithNoWrap(ConstantRange(APInt(8, 20), APInt(8, 30)), 952 OBO::NoUnsignedWrap | OBO::NoSignedWrap), 953 ConstantRange(APInt(8, 176), APInt(8, 235))); 954 955 TestAddWithNoSignedUnsignedWrapExhaustive( 956 [](const ConstantRange &CR1, const ConstantRange &CR2) { 957 return CR1.addWithNoWrap(CR2, OBO::NoUnsignedWrap | OBO::NoSignedWrap); 958 }, 959 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 960 return N1.sadd_ov(N2, IsOverflow); 961 }, 962 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 963 return N1.uadd_ov(N2, IsOverflow); 964 }); 965 } 966 967 TEST_F(ConstantRangeTest, Sub) { 968 EXPECT_EQ(Full.sub(APInt(16, 4)), Full); 969 EXPECT_EQ(Full.sub(Full), Full); 970 EXPECT_EQ(Full.sub(Empty), Empty); 971 EXPECT_EQ(Full.sub(One), Full); 972 EXPECT_EQ(Full.sub(Some), Full); 973 EXPECT_EQ(Full.sub(Wrap), Full); 974 EXPECT_EQ(Empty.sub(Empty), Empty); 975 EXPECT_EQ(Empty.sub(One), Empty); 976 EXPECT_EQ(Empty.sub(Some), Empty); 977 EXPECT_EQ(Empty.sub(Wrap), Empty); 978 EXPECT_EQ(Empty.sub(APInt(16, 4)), Empty); 979 EXPECT_EQ(Some.sub(APInt(16, 4)), 980 ConstantRange(APInt(16, 0x6), APInt(16, 0xaa6))); 981 EXPECT_EQ(Some.sub(Some), 982 ConstantRange(APInt(16, 0xf561), APInt(16, 0xaa0))); 983 EXPECT_EQ(Wrap.sub(APInt(16, 4)), 984 ConstantRange(APInt(16, 0xaa6), APInt(16, 0x6))); 985 EXPECT_EQ(One.sub(APInt(16, 4)), 986 ConstantRange(APInt(16, 0x6))); 987 988 TestBinaryOpExhaustive( 989 [](const ConstantRange &CR1, const ConstantRange &CR2) { 990 return CR1.sub(CR2); 991 }, 992 [](const APInt &N1, const APInt &N2) { 993 return N1 - N2; 994 }); 995 } 996 997 TEST_F(ConstantRangeTest, SubWithNoWrap) { 998 typedef OverflowingBinaryOperator OBO; 999 TestAddWithNoSignedWrapExhaustive( 1000 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1001 return CR1.subWithNoWrap(CR2, OBO::NoSignedWrap); 1002 }, 1003 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 1004 return N1.ssub_ov(N2, IsOverflow); 1005 }); 1006 TestAddWithNoUnsignedWrapExhaustive( 1007 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1008 return CR1.subWithNoWrap(CR2, OBO::NoUnsignedWrap); 1009 }, 1010 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 1011 return N1.usub_ov(N2, IsOverflow); 1012 }); 1013 TestAddWithNoSignedUnsignedWrapExhaustive( 1014 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1015 return CR1.subWithNoWrap(CR2, OBO::NoUnsignedWrap | OBO::NoSignedWrap); 1016 }, 1017 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 1018 return N1.ssub_ov(N2, IsOverflow); 1019 }, 1020 [](bool &IsOverflow, const APInt &N1, const APInt &N2) { 1021 return N1.usub_ov(N2, IsOverflow); 1022 }); 1023 } 1024 1025 TEST_F(ConstantRangeTest, Multiply) { 1026 EXPECT_EQ(Full.multiply(Full), Full); 1027 EXPECT_EQ(Full.multiply(Empty), Empty); 1028 EXPECT_EQ(Full.multiply(One), Full); 1029 EXPECT_EQ(Full.multiply(Some), Full); 1030 EXPECT_EQ(Full.multiply(Wrap), Full); 1031 EXPECT_EQ(Empty.multiply(Empty), Empty); 1032 EXPECT_EQ(Empty.multiply(One), Empty); 1033 EXPECT_EQ(Empty.multiply(Some), Empty); 1034 EXPECT_EQ(Empty.multiply(Wrap), Empty); 1035 EXPECT_EQ(One.multiply(One), ConstantRange(APInt(16, 0xa*0xa), 1036 APInt(16, 0xa*0xa + 1))); 1037 EXPECT_EQ(One.multiply(Some), ConstantRange(APInt(16, 0xa*0xa), 1038 APInt(16, 0xa*0xaa9 + 1))); 1039 EXPECT_EQ(One.multiply(Wrap), Full); 1040 EXPECT_EQ(Some.multiply(Some), Full); 1041 EXPECT_EQ(Some.multiply(Wrap), Full); 1042 EXPECT_EQ(Wrap.multiply(Wrap), Full); 1043 1044 ConstantRange Zero(APInt(16, 0)); 1045 EXPECT_EQ(Zero.multiply(Full), Zero); 1046 EXPECT_EQ(Zero.multiply(Some), Zero); 1047 EXPECT_EQ(Zero.multiply(Wrap), Zero); 1048 EXPECT_EQ(Full.multiply(Zero), Zero); 1049 EXPECT_EQ(Some.multiply(Zero), Zero); 1050 EXPECT_EQ(Wrap.multiply(Zero), Zero); 1051 1052 // http://llvm.org/PR4545 1053 EXPECT_EQ(ConstantRange(APInt(4, 1), APInt(4, 6)).multiply( 1054 ConstantRange(APInt(4, 6), APInt(4, 2))), 1055 ConstantRange(4, /*isFullSet=*/true)); 1056 1057 EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 0)).multiply( 1058 ConstantRange(APInt(8, 252), APInt(8, 4))), 1059 ConstantRange(APInt(8, 250), APInt(8, 9))); 1060 EXPECT_EQ(ConstantRange(APInt(8, 254), APInt(8, 255)).multiply( 1061 ConstantRange(APInt(8, 2), APInt(8, 4))), 1062 ConstantRange(APInt(8, 250), APInt(8, 253))); 1063 1064 // TODO: This should be return [-2, 0] 1065 EXPECT_EQ(ConstantRange(APInt(8, -2)).multiply( 1066 ConstantRange(APInt(8, 0), APInt(8, 2))), 1067 ConstantRange(APInt(8, -2), APInt(8, 1))); 1068 } 1069 1070 TEST_F(ConstantRangeTest, UMax) { 1071 EXPECT_EQ(Full.umax(Full), Full); 1072 EXPECT_EQ(Full.umax(Empty), Empty); 1073 EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 1074 EXPECT_EQ(Full.umax(Wrap), Full); 1075 EXPECT_EQ(Full.umax(Some), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 1076 EXPECT_EQ(Empty.umax(Empty), Empty); 1077 EXPECT_EQ(Empty.umax(Some), Empty); 1078 EXPECT_EQ(Empty.umax(Wrap), Empty); 1079 EXPECT_EQ(Empty.umax(One), Empty); 1080 EXPECT_EQ(Some.umax(Some), Some); 1081 EXPECT_EQ(Some.umax(Wrap), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 1082 EXPECT_EQ(Some.umax(One), Some); 1083 EXPECT_EQ(Wrap.umax(Wrap), Wrap); 1084 EXPECT_EQ(Wrap.umax(One), ConstantRange(APInt(16, 0xa), APInt(16, 0))); 1085 EXPECT_EQ(One.umax(One), One); 1086 1087 TestBinaryOpExhaustive( 1088 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1089 return CR1.umax(CR2); 1090 }, 1091 [](const APInt &N1, const APInt &N2) { 1092 return APIntOps::umax(N1, N2); 1093 }, 1094 PreferSmallestNonFullUnsigned); 1095 } 1096 1097 TEST_F(ConstantRangeTest, SMax) { 1098 EXPECT_EQ(Full.smax(Full), Full); 1099 EXPECT_EQ(Full.smax(Empty), Empty); 1100 EXPECT_EQ(Full.smax(Some), ConstantRange(APInt(16, 0xa), 1101 APInt::getSignedMinValue(16))); 1102 EXPECT_EQ(Full.smax(Wrap), Full); 1103 EXPECT_EQ(Full.smax(One), ConstantRange(APInt(16, 0xa), 1104 APInt::getSignedMinValue(16))); 1105 EXPECT_EQ(Empty.smax(Empty), Empty); 1106 EXPECT_EQ(Empty.smax(Some), Empty); 1107 EXPECT_EQ(Empty.smax(Wrap), Empty); 1108 EXPECT_EQ(Empty.smax(One), Empty); 1109 EXPECT_EQ(Some.smax(Some), Some); 1110 EXPECT_EQ(Some.smax(Wrap), ConstantRange(APInt(16, 0xa), 1111 APInt(16, (uint64_t)INT16_MIN))); 1112 EXPECT_EQ(Some.smax(One), Some); 1113 EXPECT_EQ(Wrap.smax(One), ConstantRange(APInt(16, 0xa), 1114 APInt(16, (uint64_t)INT16_MIN))); 1115 EXPECT_EQ(One.smax(One), One); 1116 1117 TestBinaryOpExhaustive( 1118 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1119 return CR1.smax(CR2); 1120 }, 1121 [](const APInt &N1, const APInt &N2) { 1122 return APIntOps::smax(N1, N2); 1123 }, 1124 PreferSmallestNonFullSigned); 1125 } 1126 1127 TEST_F(ConstantRangeTest, UMin) { 1128 EXPECT_EQ(Full.umin(Full), Full); 1129 EXPECT_EQ(Full.umin(Empty), Empty); 1130 EXPECT_EQ(Full.umin(Some), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); 1131 EXPECT_EQ(Full.umin(Wrap), Full); 1132 EXPECT_EQ(Empty.umin(Empty), Empty); 1133 EXPECT_EQ(Empty.umin(Some), Empty); 1134 EXPECT_EQ(Empty.umin(Wrap), Empty); 1135 EXPECT_EQ(Empty.umin(One), Empty); 1136 EXPECT_EQ(Some.umin(Some), Some); 1137 EXPECT_EQ(Some.umin(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); 1138 EXPECT_EQ(Some.umin(One), One); 1139 EXPECT_EQ(Wrap.umin(Wrap), Wrap); 1140 EXPECT_EQ(Wrap.umin(One), ConstantRange(APInt(16, 0), APInt(16, 0xb))); 1141 EXPECT_EQ(One.umin(One), One); 1142 1143 TestBinaryOpExhaustive( 1144 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1145 return CR1.umin(CR2); 1146 }, 1147 [](const APInt &N1, const APInt &N2) { 1148 return APIntOps::umin(N1, N2); 1149 }, 1150 PreferSmallestNonFullUnsigned); 1151 } 1152 1153 TEST_F(ConstantRangeTest, SMin) { 1154 EXPECT_EQ(Full.smin(Full), Full); 1155 EXPECT_EQ(Full.smin(Empty), Empty); 1156 EXPECT_EQ(Full.smin(Some), ConstantRange(APInt(16, (uint64_t)INT16_MIN), 1157 APInt(16, 0xaaa))); 1158 EXPECT_EQ(Full.smin(Wrap), Full); 1159 EXPECT_EQ(Empty.smin(Empty), Empty); 1160 EXPECT_EQ(Empty.smin(Some), Empty); 1161 EXPECT_EQ(Empty.smin(Wrap), Empty); 1162 EXPECT_EQ(Empty.smin(One), Empty); 1163 EXPECT_EQ(Some.smin(Some), Some); 1164 EXPECT_EQ(Some.smin(Wrap), ConstantRange(APInt(16, (uint64_t)INT16_MIN), 1165 APInt(16, 0xaaa))); 1166 EXPECT_EQ(Some.smin(One), One); 1167 EXPECT_EQ(Wrap.smin(Wrap), Wrap); 1168 EXPECT_EQ(Wrap.smin(One), ConstantRange(APInt(16, (uint64_t)INT16_MIN), 1169 APInt(16, 0xb))); 1170 EXPECT_EQ(One.smin(One), One); 1171 1172 TestBinaryOpExhaustive( 1173 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1174 return CR1.smin(CR2); 1175 }, 1176 [](const APInt &N1, const APInt &N2) { 1177 return APIntOps::smin(N1, N2); 1178 }, 1179 PreferSmallestNonFullSigned); 1180 } 1181 1182 TEST_F(ConstantRangeTest, UDiv) { 1183 EXPECT_EQ(Full.udiv(Full), Full); 1184 EXPECT_EQ(Full.udiv(Empty), Empty); 1185 EXPECT_EQ(Full.udiv(One), ConstantRange(APInt(16, 0), 1186 APInt(16, 0xffff / 0xa + 1))); 1187 EXPECT_EQ(Full.udiv(Some), ConstantRange(APInt(16, 0), 1188 APInt(16, 0xffff / 0xa + 1))); 1189 EXPECT_EQ(Full.udiv(Wrap), Full); 1190 EXPECT_EQ(Empty.udiv(Empty), Empty); 1191 EXPECT_EQ(Empty.udiv(One), Empty); 1192 EXPECT_EQ(Empty.udiv(Some), Empty); 1193 EXPECT_EQ(Empty.udiv(Wrap), Empty); 1194 EXPECT_EQ(One.udiv(One), ConstantRange(APInt(16, 1))); 1195 EXPECT_EQ(One.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 2))); 1196 EXPECT_EQ(One.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); 1197 EXPECT_EQ(Some.udiv(Some), ConstantRange(APInt(16, 0), APInt(16, 0x111))); 1198 EXPECT_EQ(Some.udiv(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); 1199 EXPECT_EQ(Wrap.udiv(Wrap), Full); 1200 1201 1202 ConstantRange Zero(APInt(16, 0)); 1203 EXPECT_EQ(Zero.udiv(One), Zero); 1204 EXPECT_EQ(Zero.udiv(Full), Zero); 1205 1206 EXPECT_EQ(ConstantRange(APInt(16, 0), APInt(16, 99)).udiv(Full), 1207 ConstantRange(APInt(16, 0), APInt(16, 99))); 1208 EXPECT_EQ(ConstantRange(APInt(16, 10), APInt(16, 99)).udiv(Full), 1209 ConstantRange(APInt(16, 0), APInt(16, 99))); 1210 } 1211 1212 TEST_F(ConstantRangeTest, SDiv) { 1213 unsigned Bits = 4; 1214 EnumerateTwoConstantRanges(Bits, [&](const ConstantRange &CR1, 1215 const ConstantRange &CR2) { 1216 // Collect possible results in a bit vector. We store the signed value plus 1217 // a bias to make it unsigned. 1218 int Bias = 1 << (Bits - 1); 1219 BitVector Results(1 << Bits); 1220 ForeachNumInConstantRange(CR1, [&](const APInt &N1) { 1221 ForeachNumInConstantRange(CR2, [&](const APInt &N2) { 1222 // Division by zero is UB. 1223 if (N2 == 0) 1224 return; 1225 1226 // SignedMin / -1 is UB. 1227 if (N1.isMinSignedValue() && N2.isAllOnesValue()) 1228 return; 1229 1230 APInt N = N1.sdiv(N2); 1231 Results.set(N.getSExtValue() + Bias); 1232 }); 1233 }); 1234 1235 ConstantRange CR = CR1.sdiv(CR2); 1236 if (Results.none()) { 1237 EXPECT_TRUE(CR.isEmptySet()); 1238 return; 1239 } 1240 1241 // If there is a non-full signed envelope, that should be the result. 1242 APInt SMin(Bits, Results.find_first() - Bias); 1243 APInt SMax(Bits, Results.find_last() - Bias); 1244 ConstantRange Envelope = ConstantRange::getNonEmpty(SMin, SMax + 1); 1245 if (!Envelope.isFullSet()) { 1246 EXPECT_EQ(Envelope, CR); 1247 return; 1248 } 1249 1250 // If the signed envelope is a full set, try to find a smaller sign wrapped 1251 // set that is separated in negative and positive components (or one which 1252 // can also additionally contain zero). 1253 int LastNeg = Results.find_last_in(0, Bias) - Bias; 1254 int LastPos = Results.find_next(Bias) - Bias; 1255 if (Results[Bias]) { 1256 if (LastNeg == -1) 1257 ++LastNeg; 1258 else if (LastPos == 1) 1259 --LastPos; 1260 } 1261 1262 APInt WMax(Bits, LastNeg); 1263 APInt WMin(Bits, LastPos); 1264 ConstantRange Wrapped = ConstantRange::getNonEmpty(WMin, WMax + 1); 1265 EXPECT_EQ(Wrapped, CR); 1266 }); 1267 } 1268 1269 TEST_F(ConstantRangeTest, URem) { 1270 EXPECT_EQ(Full.urem(Empty), Empty); 1271 EXPECT_EQ(Empty.urem(Full), Empty); 1272 // urem by zero is poison. 1273 EXPECT_EQ(Full.urem(ConstantRange(APInt(16, 0))), Empty); 1274 // urem by full range doesn't contain MaxValue. 1275 EXPECT_EQ(Full.urem(Full), ConstantRange(APInt(16, 0), APInt(16, 0xffff))); 1276 // urem is upper bounded by maximum RHS minus one. 1277 EXPECT_EQ(Full.urem(ConstantRange(APInt(16, 0), APInt(16, 123))), 1278 ConstantRange(APInt(16, 0), APInt(16, 122))); 1279 // urem is upper bounded by maximum LHS. 1280 EXPECT_EQ(ConstantRange(APInt(16, 0), APInt(16, 123)).urem(Full), 1281 ConstantRange(APInt(16, 0), APInt(16, 123))); 1282 // If the LHS is always lower than the RHS, the result is the LHS. 1283 EXPECT_EQ(ConstantRange(APInt(16, 10), APInt(16, 20)) 1284 .urem(ConstantRange(APInt(16, 20), APInt(16, 30))), 1285 ConstantRange(APInt(16, 10), APInt(16, 20))); 1286 // It has to be strictly lower, otherwise the top value may wrap to zero. 1287 EXPECT_EQ(ConstantRange(APInt(16, 10), APInt(16, 20)) 1288 .urem(ConstantRange(APInt(16, 19), APInt(16, 30))), 1289 ConstantRange(APInt(16, 0), APInt(16, 20))); 1290 // [12, 14] % 10 is [2, 4], but we conservatively compute [0, 9]. 1291 EXPECT_EQ(ConstantRange(APInt(16, 12), APInt(16, 15)) 1292 .urem(ConstantRange(APInt(16, 10))), 1293 ConstantRange(APInt(16, 0), APInt(16, 10))); 1294 1295 TestBinaryOpExhaustiveCorrectnessOnly( 1296 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1297 return CR1.urem(CR2); 1298 }, 1299 [](const APInt &N1, const APInt &N2) -> Optional<APInt> { 1300 if (N2.isNullValue()) 1301 return None; 1302 return N1.urem(N2); 1303 }); 1304 } 1305 1306 TEST_F(ConstantRangeTest, SRem) { 1307 EXPECT_EQ(Full.srem(Empty), Empty); 1308 EXPECT_EQ(Empty.srem(Full), Empty); 1309 // srem by zero is UB. 1310 EXPECT_EQ(Full.srem(ConstantRange(APInt(16, 0))), Empty); 1311 // srem by full range doesn't contain SignedMinValue. 1312 EXPECT_EQ(Full.srem(Full), ConstantRange(APInt::getSignedMinValue(16) + 1, 1313 APInt::getSignedMinValue(16))); 1314 1315 ConstantRange PosMod(APInt(16, 10), APInt(16, 21)); // [10, 20] 1316 ConstantRange NegMod(APInt(16, -20), APInt(16, -9)); // [-20, -10] 1317 ConstantRange IntMinMod(APInt::getSignedMinValue(16)); 1318 1319 ConstantRange Expected(16, true); 1320 1321 // srem is bounded by abs(RHS) minus one. 1322 ConstantRange PosLargeLHS(APInt(16, 0), APInt(16, 41)); 1323 Expected = ConstantRange(APInt(16, 0), APInt(16, 20)); 1324 EXPECT_EQ(PosLargeLHS.srem(PosMod), Expected); 1325 EXPECT_EQ(PosLargeLHS.srem(NegMod), Expected); 1326 ConstantRange NegLargeLHS(APInt(16, -40), APInt(16, 1)); 1327 Expected = ConstantRange(APInt(16, -19), APInt(16, 1)); 1328 EXPECT_EQ(NegLargeLHS.srem(PosMod), Expected); 1329 EXPECT_EQ(NegLargeLHS.srem(NegMod), Expected); 1330 ConstantRange PosNegLargeLHS(APInt(16, -32), APInt(16, 38)); 1331 Expected = ConstantRange(APInt(16, -19), APInt(16, 20)); 1332 EXPECT_EQ(PosNegLargeLHS.srem(PosMod), Expected); 1333 EXPECT_EQ(PosNegLargeLHS.srem(NegMod), Expected); 1334 1335 // srem is bounded by LHS. 1336 ConstantRange PosLHS(APInt(16, 0), APInt(16, 16)); 1337 EXPECT_EQ(PosLHS.srem(PosMod), PosLHS); 1338 EXPECT_EQ(PosLHS.srem(NegMod), PosLHS); 1339 EXPECT_EQ(PosLHS.srem(IntMinMod), PosLHS); 1340 ConstantRange NegLHS(APInt(16, -15), APInt(16, 1)); 1341 EXPECT_EQ(NegLHS.srem(PosMod), NegLHS); 1342 EXPECT_EQ(NegLHS.srem(NegMod), NegLHS); 1343 EXPECT_EQ(NegLHS.srem(IntMinMod), NegLHS); 1344 ConstantRange PosNegLHS(APInt(16, -12), APInt(16, 18)); 1345 EXPECT_EQ(PosNegLHS.srem(PosMod), PosNegLHS); 1346 EXPECT_EQ(PosNegLHS.srem(NegMod), PosNegLHS); 1347 EXPECT_EQ(PosNegLHS.srem(IntMinMod), PosNegLHS); 1348 1349 // srem is LHS if it is smaller than RHS. 1350 ConstantRange PosSmallLHS(APInt(16, 3), APInt(16, 8)); 1351 EXPECT_EQ(PosSmallLHS.srem(PosMod), PosSmallLHS); 1352 EXPECT_EQ(PosSmallLHS.srem(NegMod), PosSmallLHS); 1353 EXPECT_EQ(PosSmallLHS.srem(IntMinMod), PosSmallLHS); 1354 ConstantRange NegSmallLHS(APInt(16, -7), APInt(16, -2)); 1355 EXPECT_EQ(NegSmallLHS.srem(PosMod), NegSmallLHS); 1356 EXPECT_EQ(NegSmallLHS.srem(NegMod), NegSmallLHS); 1357 EXPECT_EQ(NegSmallLHS.srem(IntMinMod), NegSmallLHS); 1358 ConstantRange PosNegSmallLHS(APInt(16, -3), APInt(16, 8)); 1359 EXPECT_EQ(PosNegSmallLHS.srem(PosMod), PosNegSmallLHS); 1360 EXPECT_EQ(PosNegSmallLHS.srem(NegMod), PosNegSmallLHS); 1361 EXPECT_EQ(PosNegSmallLHS.srem(IntMinMod), PosNegSmallLHS); 1362 1363 // Example of a suboptimal result: 1364 // [12, 14] srem 10 is [2, 4], but we conservatively compute [0, 9]. 1365 EXPECT_EQ(ConstantRange(APInt(16, 12), APInt(16, 15)) 1366 .srem(ConstantRange(APInt(16, 10))), 1367 ConstantRange(APInt(16, 0), APInt(16, 10))); 1368 1369 TestBinaryOpExhaustiveCorrectnessOnly( 1370 [](const ConstantRange &CR1, const ConstantRange &CR2) { 1371 return CR1.srem(CR2); 1372 }, 1373 [](const APInt &N1, const APInt &N2) -> Optional<APInt> { 1374 if (N2.isNullValue()) 1375 return None; 1376 return N1.srem(N2); 1377 }); 1378 } 1379 1380 TEST_F(ConstantRangeTest, Shl) { 1381 ConstantRange Some2(APInt(16, 0xfff), APInt(16, 0x8000)); 1382 ConstantRange WrapNullMax(APInt(16, 0x1), APInt(16, 0x0)); 1383 EXPECT_EQ(Full.shl(Full), Full); 1384 EXPECT_EQ(Full.shl(Empty), Empty); 1385 EXPECT_EQ(Full.shl(One), Full); // TODO: [0, (-1 << 0xa) + 1) 1386 EXPECT_EQ(Full.shl(Some), Full); // TODO: [0, (-1 << 0xa) + 1) 1387 EXPECT_EQ(Full.shl(Wrap), Full); 1388 EXPECT_EQ(Empty.shl(Empty), Empty); 1389 EXPECT_EQ(Empty.shl(One), Empty); 1390 EXPECT_EQ(Empty.shl(Some), Empty); 1391 EXPECT_EQ(Empty.shl(Wrap), Empty); 1392 EXPECT_EQ(One.shl(One), ConstantRange(APInt(16, 0xa << 0xa), 1393 APInt(16, (0xa << 0xa) + 1))); 1394 EXPECT_EQ(One.shl(Some), Full); // TODO: [0xa << 0xa, 0) 1395 EXPECT_EQ(One.shl(Wrap), Full); // TODO: [0xa, 0xa << 14 + 1) 1396 EXPECT_EQ(Some.shl(Some), Full); // TODO: [0xa << 0xa, 0xfc01) 1397 EXPECT_EQ(Some.shl(Wrap), Full); // TODO: [0xa, 0x7ff << 0x5 + 1) 1398 EXPECT_EQ(Wrap.shl(Wrap), Full); 1399 EXPECT_EQ( 1400 Some2.shl(ConstantRange(APInt(16, 0x1))), 1401 ConstantRange(APInt(16, 0xfff << 0x1), APInt(16, 0x7fff << 0x1) + 1)); 1402 EXPECT_EQ(One.shl(WrapNullMax), Full); 1403 } 1404 1405 TEST_F(ConstantRangeTest, Lshr) { 1406 EXPECT_EQ(Full.lshr(Full), Full); 1407 EXPECT_EQ(Full.lshr(Empty), Empty); 1408 EXPECT_EQ(Full.lshr(One), ConstantRange(APInt(16, 0), 1409 APInt(16, (0xffff >> 0xa) + 1))); 1410 EXPECT_EQ(Full.lshr(Some), ConstantRange(APInt(16, 0), 1411 APInt(16, (0xffff >> 0xa) + 1))); 1412 EXPECT_EQ(Full.lshr(Wrap), Full); 1413 EXPECT_EQ(Empty.lshr(Empty), Empty); 1414 EXPECT_EQ(Empty.lshr(One), Empty); 1415 EXPECT_EQ(Empty.lshr(Some), Empty); 1416 EXPECT_EQ(Empty.lshr(Wrap), Empty); 1417 EXPECT_EQ(One.lshr(One), ConstantRange(APInt(16, 0))); 1418 EXPECT_EQ(One.lshr(Some), ConstantRange(APInt(16, 0))); 1419 EXPECT_EQ(One.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); 1420 EXPECT_EQ(Some.lshr(Some), ConstantRange(APInt(16, 0), 1421 APInt(16, (0xaaa >> 0xa) + 1))); 1422 EXPECT_EQ(Some.lshr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); 1423 EXPECT_EQ(Wrap.lshr(Wrap), Full); 1424 } 1425 1426 TEST_F(ConstantRangeTest, Ashr) { 1427 EXPECT_EQ(Full.ashr(Full), Full); 1428 EXPECT_EQ(Full.ashr(Empty), Empty); 1429 EXPECT_EQ(Full.ashr(One), ConstantRange(APInt(16, 0xffe0), 1430 APInt(16, (0x7fff >> 0xa) + 1 ))); 1431 ConstantRange Small(APInt(16, 0xa), APInt(16, 0xb)); 1432 EXPECT_EQ(Full.ashr(Small), ConstantRange(APInt(16, 0xffe0), 1433 APInt(16, (0x7fff >> 0xa) + 1 ))); 1434 EXPECT_EQ(Full.ashr(Some), ConstantRange(APInt(16, 0xffe0), 1435 APInt(16, (0x7fff >> 0xa) + 1 ))); 1436 EXPECT_EQ(Full.ashr(Wrap), Full); 1437 EXPECT_EQ(Empty.ashr(Empty), Empty); 1438 EXPECT_EQ(Empty.ashr(One), Empty); 1439 EXPECT_EQ(Empty.ashr(Some), Empty); 1440 EXPECT_EQ(Empty.ashr(Wrap), Empty); 1441 EXPECT_EQ(One.ashr(One), ConstantRange(APInt(16, 0))); 1442 EXPECT_EQ(One.ashr(Some), ConstantRange(APInt(16, 0))); 1443 EXPECT_EQ(One.ashr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xb))); 1444 EXPECT_EQ(Some.ashr(Some), ConstantRange(APInt(16, 0), 1445 APInt(16, (0xaaa >> 0xa) + 1))); 1446 EXPECT_EQ(Some.ashr(Wrap), ConstantRange(APInt(16, 0), APInt(16, 0xaaa))); 1447 EXPECT_EQ(Wrap.ashr(Wrap), Full); 1448 ConstantRange Neg(APInt(16, 0xf3f0, true), APInt(16, 0xf7f8, true)); 1449 EXPECT_EQ(Neg.ashr(Small), ConstantRange(APInt(16, 0xfffc, true), 1450 APInt(16, 0xfffe, true))); 1451 } 1452 1453 TEST(ConstantRange, MakeAllowedICmpRegion) { 1454 // PR8250 1455 ConstantRange SMax = ConstantRange(APInt::getSignedMaxValue(32)); 1456 EXPECT_TRUE(ConstantRange::makeAllowedICmpRegion(ICmpInst::ICMP_SGT, SMax) 1457 .isEmptySet()); 1458 } 1459 1460 TEST(ConstantRange, MakeSatisfyingICmpRegion) { 1461 ConstantRange LowHalf(APInt(8, 0), APInt(8, 128)); 1462 ConstantRange HighHalf(APInt(8, 128), APInt(8, 0)); 1463 ConstantRange EmptySet(8, /* isFullSet = */ false); 1464 1465 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, LowHalf), 1466 HighHalf); 1467 1468 EXPECT_EQ( 1469 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_NE, HighHalf), 1470 LowHalf); 1471 1472 EXPECT_TRUE(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_EQ, 1473 HighHalf).isEmptySet()); 1474 1475 ConstantRange UnsignedSample(APInt(8, 5), APInt(8, 200)); 1476 1477 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULT, 1478 UnsignedSample), 1479 ConstantRange(APInt(8, 0), APInt(8, 5))); 1480 1481 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_ULE, 1482 UnsignedSample), 1483 ConstantRange(APInt(8, 0), APInt(8, 6))); 1484 1485 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGT, 1486 UnsignedSample), 1487 ConstantRange(APInt(8, 200), APInt(8, 0))); 1488 1489 EXPECT_EQ(ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_UGE, 1490 UnsignedSample), 1491 ConstantRange(APInt(8, 199), APInt(8, 0))); 1492 1493 ConstantRange SignedSample(APInt(8, -5), APInt(8, 5)); 1494 1495 EXPECT_EQ( 1496 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLT, SignedSample), 1497 ConstantRange(APInt(8, -128), APInt(8, -5))); 1498 1499 EXPECT_EQ( 1500 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SLE, SignedSample), 1501 ConstantRange(APInt(8, -128), APInt(8, -4))); 1502 1503 EXPECT_EQ( 1504 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGT, SignedSample), 1505 ConstantRange(APInt(8, 5), APInt(8, -128))); 1506 1507 EXPECT_EQ( 1508 ConstantRange::makeSatisfyingICmpRegion(ICmpInst::ICMP_SGE, SignedSample), 1509 ConstantRange(APInt(8, 4), APInt(8, -128))); 1510 } 1511 1512 TEST(ConstantRange, MakeGuaranteedNoWrapRegion) { 1513 const int IntMin4Bits = 8; 1514 const int IntMax4Bits = 7; 1515 typedef OverflowingBinaryOperator OBO; 1516 1517 for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) { 1518 APInt C(4, Const, true /* = isSigned */); 1519 1520 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 1521 Instruction::Add, C, OBO::NoUnsignedWrap); 1522 1523 EXPECT_FALSE(NUWRegion.isEmptySet()); 1524 1525 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 1526 Instruction::Add, C, OBO::NoSignedWrap); 1527 1528 EXPECT_FALSE(NSWRegion.isEmptySet()); 1529 1530 for (APInt I = NUWRegion.getLower(), E = NUWRegion.getUpper(); I != E; 1531 ++I) { 1532 bool Overflow = false; 1533 (void)I.uadd_ov(C, Overflow); 1534 EXPECT_FALSE(Overflow); 1535 } 1536 1537 for (APInt I = NSWRegion.getLower(), E = NSWRegion.getUpper(); I != E; 1538 ++I) { 1539 bool Overflow = false; 1540 (void)I.sadd_ov(C, Overflow); 1541 EXPECT_FALSE(Overflow); 1542 } 1543 } 1544 1545 for (int Const : {0, -1, -2, 1, 2, IntMin4Bits, IntMax4Bits}) { 1546 APInt C(4, Const, true /* = isSigned */); 1547 1548 auto NUWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 1549 Instruction::Sub, C, OBO::NoUnsignedWrap); 1550 1551 EXPECT_FALSE(NUWRegion.isEmptySet()); 1552 1553 auto NSWRegion = ConstantRange::makeGuaranteedNoWrapRegion( 1554 Instruction::Sub, C, OBO::NoSignedWrap); 1555 1556 EXPECT_FALSE(NSWRegion.isEmptySet()); 1557 1558 for (APInt I = NUWRegion.getLower(), E = NUWRegion.getUpper(); I != E; 1559 ++I) { 1560 bool Overflow = false; 1561 (void)I.usub_ov(C, Overflow); 1562 EXPECT_FALSE(Overflow); 1563 } 1564 1565 for (APInt I = NSWRegion.getLower(), E = NSWRegion.getUpper(); I != E; 1566 ++I) { 1567 bool Overflow = false; 1568 (void)I.ssub_ov(C, Overflow); 1569 EXPECT_FALSE(Overflow); 1570 } 1571 } 1572 1573 auto NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion( 1574 Instruction::Add, ConstantRange(32, /* isFullSet = */ true), 1575 OBO::NoSignedWrap); 1576 EXPECT_TRUE(NSWForAllValues.isSingleElement() && 1577 NSWForAllValues.getSingleElement()->isMinValue()); 1578 1579 NSWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion( 1580 Instruction::Sub, ConstantRange(32, /* isFullSet = */ true), 1581 OBO::NoSignedWrap); 1582 EXPECT_TRUE(NSWForAllValues.isSingleElement() && 1583 NSWForAllValues.getSingleElement()->isMaxValue()); 1584 1585 auto NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion( 1586 Instruction::Add, ConstantRange(32, /* isFullSet = */ true), 1587 OBO::NoUnsignedWrap); 1588 EXPECT_TRUE(NUWForAllValues.isSingleElement() && 1589 NUWForAllValues.getSingleElement()->isMinValue()); 1590 1591 NUWForAllValues = ConstantRange::makeGuaranteedNoWrapRegion( 1592 Instruction::Sub, ConstantRange(32, /* isFullSet = */ true), 1593 OBO::NoUnsignedWrap); 1594 EXPECT_TRUE(NUWForAllValues.isSingleElement() && 1595 NUWForAllValues.getSingleElement()->isMaxValue()); 1596 1597 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion( 1598 Instruction::Add, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet()); 1599 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion( 1600 Instruction::Add, APInt(32, 0), OBO::NoSignedWrap).isFullSet()); 1601 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion( 1602 Instruction::Sub, APInt(32, 0), OBO::NoUnsignedWrap).isFullSet()); 1603 EXPECT_TRUE(ConstantRange::makeGuaranteedNoWrapRegion( 1604 Instruction::Sub, APInt(32, 0), OBO::NoSignedWrap).isFullSet()); 1605 1606 ConstantRange OneToFive(APInt(32, 1), APInt(32, 6)); 1607 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1608 Instruction::Add, OneToFive, OBO::NoSignedWrap), 1609 ConstantRange(APInt::getSignedMinValue(32), 1610 APInt::getSignedMaxValue(32) - 4)); 1611 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1612 Instruction::Add, OneToFive, OBO::NoUnsignedWrap), 1613 ConstantRange(APInt::getMinValue(32), APInt::getMinValue(32) - 5)); 1614 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1615 Instruction::Sub, OneToFive, OBO::NoSignedWrap), 1616 ConstantRange(APInt::getSignedMinValue(32) + 5, 1617 APInt::getSignedMinValue(32))); 1618 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1619 Instruction::Sub, OneToFive, OBO::NoUnsignedWrap), 1620 ConstantRange(APInt::getMinValue(32) + 5, APInt::getMinValue(32))); 1621 1622 ConstantRange MinusFiveToMinusTwo(APInt(32, -5), APInt(32, -1)); 1623 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1624 Instruction::Add, MinusFiveToMinusTwo, OBO::NoSignedWrap), 1625 ConstantRange(APInt::getSignedMinValue(32) + 5, 1626 APInt::getSignedMinValue(32))); 1627 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1628 Instruction::Add, MinusFiveToMinusTwo, OBO::NoUnsignedWrap), 1629 ConstantRange(APInt(32, 0), APInt(32, 2))); 1630 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1631 Instruction::Sub, MinusFiveToMinusTwo, OBO::NoSignedWrap), 1632 ConstantRange(APInt::getSignedMinValue(32), 1633 APInt::getSignedMaxValue(32) - 4)); 1634 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1635 Instruction::Sub, MinusFiveToMinusTwo, OBO::NoUnsignedWrap), 1636 ConstantRange(APInt::getMaxValue(32) - 1, 1637 APInt::getMinValue(32))); 1638 1639 ConstantRange MinusOneToOne(APInt(32, -1), APInt(32, 2)); 1640 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1641 Instruction::Add, MinusOneToOne, OBO::NoSignedWrap), 1642 ConstantRange(APInt::getSignedMinValue(32) + 1, 1643 APInt::getSignedMinValue(32) - 1)); 1644 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1645 Instruction::Add, MinusOneToOne, OBO::NoUnsignedWrap), 1646 ConstantRange(APInt(32, 0), APInt(32, 1))); 1647 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1648 Instruction::Sub, MinusOneToOne, OBO::NoSignedWrap), 1649 ConstantRange(APInt::getSignedMinValue(32) + 1, 1650 APInt::getSignedMinValue(32) - 1)); 1651 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1652 Instruction::Sub, MinusOneToOne, OBO::NoUnsignedWrap), 1653 ConstantRange(APInt::getMaxValue(32), 1654 APInt::getMinValue(32))); 1655 1656 ConstantRange One(APInt(32, 1), APInt(32, 2)); 1657 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1658 Instruction::Add, One, OBO::NoSignedWrap), 1659 ConstantRange(APInt::getSignedMinValue(32), 1660 APInt::getSignedMaxValue(32))); 1661 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1662 Instruction::Add, One, OBO::NoUnsignedWrap), 1663 ConstantRange(APInt::getMinValue(32), APInt::getMaxValue(32))); 1664 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1665 Instruction::Sub, One, OBO::NoSignedWrap), 1666 ConstantRange(APInt::getSignedMinValue(32) + 1, 1667 APInt::getSignedMinValue(32))); 1668 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1669 Instruction::Sub, One, OBO::NoUnsignedWrap), 1670 ConstantRange(APInt::getMinValue(32) + 1, APInt::getMinValue(32))); 1671 1672 ConstantRange OneLessThanBitWidth(APInt(32, 0), APInt(32, 31) + 1); 1673 ConstantRange UpToBitWidth(APInt(32, 0), APInt(32, 32) + 1); 1674 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1675 Instruction::Shl, UpToBitWidth, OBO::NoUnsignedWrap), 1676 ConstantRange::makeGuaranteedNoWrapRegion( 1677 Instruction::Shl, OneLessThanBitWidth, OBO::NoUnsignedWrap)); 1678 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1679 Instruction::Shl, UpToBitWidth, OBO::NoSignedWrap), 1680 ConstantRange::makeGuaranteedNoWrapRegion( 1681 Instruction::Shl, OneLessThanBitWidth, OBO::NoSignedWrap)); 1682 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1683 Instruction::Shl, UpToBitWidth, OBO::NoUnsignedWrap), 1684 ConstantRange(APInt(32, 0), APInt(32, 1) + 1)); 1685 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1686 Instruction::Shl, UpToBitWidth, OBO::NoSignedWrap), 1687 ConstantRange(APInt(32, -1), APInt(32, 0) + 1)); 1688 1689 EXPECT_EQ( 1690 ConstantRange::makeGuaranteedNoWrapRegion( 1691 Instruction::Shl, ConstantRange::getFull(32), OBO::NoUnsignedWrap), 1692 ConstantRange::makeGuaranteedNoWrapRegion( 1693 Instruction::Shl, OneLessThanBitWidth, OBO::NoUnsignedWrap)); 1694 EXPECT_EQ( 1695 ConstantRange::makeGuaranteedNoWrapRegion( 1696 Instruction::Shl, ConstantRange::getFull(32), OBO::NoSignedWrap), 1697 ConstantRange::makeGuaranteedNoWrapRegion( 1698 Instruction::Shl, OneLessThanBitWidth, OBO::NoSignedWrap)); 1699 1700 ConstantRange IllegalShAmt(APInt(32, 32), APInt(32, 0) + 1); 1701 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1702 Instruction::Shl, IllegalShAmt, OBO::NoUnsignedWrap), 1703 ConstantRange::getFull(32)); 1704 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1705 Instruction::Shl, IllegalShAmt, OBO::NoSignedWrap), 1706 ConstantRange::getFull(32)); 1707 1708 EXPECT_EQ( 1709 ConstantRange::makeGuaranteedNoWrapRegion( 1710 Instruction::Shl, ConstantRange(APInt(32, -32), APInt(32, 16) + 1), 1711 OBO::NoUnsignedWrap), 1712 ConstantRange::makeGuaranteedNoWrapRegion( 1713 Instruction::Shl, ConstantRange(APInt(32, 0), APInt(32, 16) + 1), 1714 OBO::NoUnsignedWrap)); 1715 EXPECT_EQ( 1716 ConstantRange::makeGuaranteedNoWrapRegion( 1717 Instruction::Shl, ConstantRange(APInt(32, -32), APInt(32, 16) + 1), 1718 OBO::NoSignedWrap), 1719 ConstantRange::makeGuaranteedNoWrapRegion( 1720 Instruction::Shl, ConstantRange(APInt(32, 0), APInt(32, 16) + 1), 1721 OBO::NoSignedWrap)); 1722 1723 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1724 Instruction::Shl, 1725 ConstantRange(APInt(32, -32), APInt(32, 16) + 1), 1726 OBO::NoUnsignedWrap), 1727 ConstantRange(APInt(32, 0), APInt(32, 65535) + 1)); 1728 EXPECT_EQ(ConstantRange::makeGuaranteedNoWrapRegion( 1729 Instruction::Shl, 1730 ConstantRange(APInt(32, -32), APInt(32, 16) + 1), 1731 OBO::NoSignedWrap), 1732 ConstantRange(APInt(32, -32768), APInt(32, 32767) + 1)); 1733 } 1734 1735 template<typename Fn> 1736 void TestNoWrapRegionExhaustive(Instruction::BinaryOps BinOp, 1737 unsigned NoWrapKind, Fn OverflowFn) { 1738 unsigned Bits = 5; 1739 EnumerateConstantRanges(Bits, [&](const ConstantRange &CR) { 1740 if (CR.isEmptySet()) 1741 return; 1742 if (Instruction::isShift(BinOp) && CR.getUnsignedMax().uge(Bits)) 1743 return; 1744 1745 ConstantRange NoWrap = 1746 ConstantRange::makeGuaranteedNoWrapRegion(BinOp, CR, NoWrapKind); 1747 ConstantRange Full = ConstantRange::getFull(Bits); 1748 ForeachNumInConstantRange(Full, [&](const APInt &N1) { 1749 bool NoOverflow = true; 1750 bool Overflow = true; 1751 ForeachNumInConstantRange(CR, [&](const APInt &N2) { 1752 if (OverflowFn(N1, N2)) 1753 NoOverflow = false; 1754 else 1755 Overflow = false; 1756 }); 1757 EXPECT_EQ(NoOverflow, NoWrap.contains(N1)); 1758 1759 // The no-wrap range is exact for single-element ranges. 1760 if (CR.isSingleElement()) { 1761 EXPECT_EQ(Overflow, !NoWrap.contains(N1)); 1762 } 1763 }); 1764 }); 1765 } 1766 1767 // Show that makeGuaranteedNoWrapRegion() is maximal, and for single-element 1768 // ranges also exact. 1769 TEST(ConstantRange, NoWrapRegionExhaustive) { 1770 TestNoWrapRegionExhaustive( 1771 Instruction::Add, OverflowingBinaryOperator::NoUnsignedWrap, 1772 [](const APInt &N1, const APInt &N2) { 1773 bool Overflow; 1774 (void) N1.uadd_ov(N2, Overflow); 1775 return Overflow; 1776 }); 1777 TestNoWrapRegionExhaustive( 1778 Instruction::Add, OverflowingBinaryOperator::NoSignedWrap, 1779 [](const APInt &N1, const APInt &N2) { 1780 bool Overflow; 1781 (void) N1.sadd_ov(N2, Overflow); 1782 return Overflow; 1783 }); 1784 TestNoWrapRegionExhaustive( 1785 Instruction::Sub, OverflowingBinaryOperator::NoUnsignedWrap, 1786 [](const APInt &N1, const APInt &N2) { 1787 bool Overflow; 1788 (void) N1.usub_ov(N2, Overflow); 1789 return Overflow; 1790 }); 1791 TestNoWrapRegionExhaustive( 1792 Instruction::Sub, OverflowingBinaryOperator::NoSignedWrap, 1793 [](const APInt &N1, const APInt &N2) { 1794 bool Overflow; 1795 (void) N1.ssub_ov(N2, Overflow); 1796 return Overflow; 1797 }); 1798 TestNoWrapRegionExhaustive( 1799 Instruction::Mul, OverflowingBinaryOperator::NoUnsignedWrap, 1800 [](const APInt &N1, const APInt &N2) { 1801 bool Overflow; 1802 (void) N1.umul_ov(N2, Overflow); 1803 return Overflow; 1804 }); 1805 TestNoWrapRegionExhaustive( 1806 Instruction::Mul, OverflowingBinaryOperator::NoSignedWrap, 1807 [](const APInt &N1, const APInt &N2) { 1808 bool Overflow; 1809 (void) N1.smul_ov(N2, Overflow); 1810 return Overflow; 1811 }); 1812 TestNoWrapRegionExhaustive(Instruction::Shl, 1813 OverflowingBinaryOperator::NoUnsignedWrap, 1814 [](const APInt &N1, const APInt &N2) { 1815 bool Overflow; 1816 (void)N1.ushl_ov(N2, Overflow); 1817 return Overflow; 1818 }); 1819 TestNoWrapRegionExhaustive(Instruction::Shl, 1820 OverflowingBinaryOperator::NoSignedWrap, 1821 [](const APInt &N1, const APInt &N2) { 1822 bool Overflow; 1823 (void)N1.sshl_ov(N2, Overflow); 1824 return Overflow; 1825 }); 1826 } 1827 1828 TEST(ConstantRange, GetEquivalentICmp) { 1829 APInt RHS; 1830 CmpInst::Predicate Pred; 1831 1832 EXPECT_TRUE(ConstantRange(APInt::getMinValue(32), APInt(32, 100)) 1833 .getEquivalentICmp(Pred, RHS)); 1834 EXPECT_EQ(Pred, CmpInst::ICMP_ULT); 1835 EXPECT_EQ(RHS, APInt(32, 100)); 1836 1837 EXPECT_TRUE(ConstantRange(APInt::getSignedMinValue(32), APInt(32, 100)) 1838 .getEquivalentICmp(Pred, RHS)); 1839 EXPECT_EQ(Pred, CmpInst::ICMP_SLT); 1840 EXPECT_EQ(RHS, APInt(32, 100)); 1841 1842 EXPECT_TRUE(ConstantRange(APInt(32, 100), APInt::getMinValue(32)) 1843 .getEquivalentICmp(Pred, RHS)); 1844 EXPECT_EQ(Pred, CmpInst::ICMP_UGE); 1845 EXPECT_EQ(RHS, APInt(32, 100)); 1846 1847 EXPECT_TRUE(ConstantRange(APInt(32, 100), APInt::getSignedMinValue(32)) 1848 .getEquivalentICmp(Pred, RHS)); 1849 EXPECT_EQ(Pred, CmpInst::ICMP_SGE); 1850 EXPECT_EQ(RHS, APInt(32, 100)); 1851 1852 EXPECT_TRUE( 1853 ConstantRange(32, /*isFullSet=*/true).getEquivalentICmp(Pred, RHS)); 1854 EXPECT_EQ(Pred, CmpInst::ICMP_UGE); 1855 EXPECT_EQ(RHS, APInt(32, 0)); 1856 1857 EXPECT_TRUE( 1858 ConstantRange(32, /*isFullSet=*/false).getEquivalentICmp(Pred, RHS)); 1859 EXPECT_EQ(Pred, CmpInst::ICMP_ULT); 1860 EXPECT_EQ(RHS, APInt(32, 0)); 1861 1862 EXPECT_FALSE(ConstantRange(APInt(32, 100), APInt(32, 200)) 1863 .getEquivalentICmp(Pred, RHS)); 1864 1865 EXPECT_FALSE(ConstantRange(APInt::getSignedMinValue(32) - APInt(32, 100), 1866 APInt::getSignedMinValue(32) + APInt(32, 100)) 1867 .getEquivalentICmp(Pred, RHS)); 1868 1869 EXPECT_FALSE(ConstantRange(APInt::getMinValue(32) - APInt(32, 100), 1870 APInt::getMinValue(32) + APInt(32, 100)) 1871 .getEquivalentICmp(Pred, RHS)); 1872 1873 EXPECT_TRUE(ConstantRange(APInt(32, 100)).getEquivalentICmp(Pred, RHS)); 1874 EXPECT_EQ(Pred, CmpInst::ICMP_EQ); 1875 EXPECT_EQ(RHS, APInt(32, 100)); 1876 1877 EXPECT_TRUE( 1878 ConstantRange(APInt(32, 100)).inverse().getEquivalentICmp(Pred, RHS)); 1879 EXPECT_EQ(Pred, CmpInst::ICMP_NE); 1880 EXPECT_EQ(RHS, APInt(32, 100)); 1881 1882 EXPECT_TRUE( 1883 ConstantRange(APInt(512, 100)).inverse().getEquivalentICmp(Pred, RHS)); 1884 EXPECT_EQ(Pred, CmpInst::ICMP_NE); 1885 EXPECT_EQ(RHS, APInt(512, 100)); 1886 1887 // NB! It would be correct for the following four calls to getEquivalentICmp 1888 // to return ordered predicates like CmpInst::ICMP_ULT or CmpInst::ICMP_UGT. 1889 // However, that's not the case today. 1890 1891 EXPECT_TRUE(ConstantRange(APInt(32, 0)).getEquivalentICmp(Pred, RHS)); 1892 EXPECT_EQ(Pred, CmpInst::ICMP_EQ); 1893 EXPECT_EQ(RHS, APInt(32, 0)); 1894 1895 EXPECT_TRUE( 1896 ConstantRange(APInt(32, 0)).inverse().getEquivalentICmp(Pred, RHS)); 1897 EXPECT_EQ(Pred, CmpInst::ICMP_NE); 1898 EXPECT_EQ(RHS, APInt(32, 0)); 1899 1900 EXPECT_TRUE(ConstantRange(APInt(32, -1)).getEquivalentICmp(Pred, RHS)); 1901 EXPECT_EQ(Pred, CmpInst::ICMP_EQ); 1902 EXPECT_EQ(RHS, APInt(32, -1)); 1903 1904 EXPECT_TRUE( 1905 ConstantRange(APInt(32, -1)).inverse().getEquivalentICmp(Pred, RHS)); 1906 EXPECT_EQ(Pred, CmpInst::ICMP_NE); 1907 EXPECT_EQ(RHS, APInt(32, -1)); 1908 } 1909 1910 #define EXPECT_MAY_OVERFLOW(op) \ 1911 EXPECT_EQ(ConstantRange::OverflowResult::MayOverflow, (op)) 1912 #define EXPECT_ALWAYS_OVERFLOWS_LOW(op) \ 1913 EXPECT_EQ(ConstantRange::OverflowResult::AlwaysOverflowsLow, (op)) 1914 #define EXPECT_ALWAYS_OVERFLOWS_HIGH(op) \ 1915 EXPECT_EQ(ConstantRange::OverflowResult::AlwaysOverflowsHigh, (op)) 1916 #define EXPECT_NEVER_OVERFLOWS(op) \ 1917 EXPECT_EQ(ConstantRange::OverflowResult::NeverOverflows, (op)) 1918 1919 TEST_F(ConstantRangeTest, UnsignedAddOverflow) { 1920 // Ill-defined - may overflow is a conservative result. 1921 EXPECT_MAY_OVERFLOW(Some.unsignedAddMayOverflow(Empty)); 1922 EXPECT_MAY_OVERFLOW(Empty.unsignedAddMayOverflow(Some)); 1923 1924 // Never overflow despite one full/wrap set. 1925 ConstantRange Zero(APInt::getNullValue(16)); 1926 EXPECT_NEVER_OVERFLOWS(Full.unsignedAddMayOverflow(Zero)); 1927 EXPECT_NEVER_OVERFLOWS(Wrap.unsignedAddMayOverflow(Zero)); 1928 EXPECT_NEVER_OVERFLOWS(Zero.unsignedAddMayOverflow(Full)); 1929 EXPECT_NEVER_OVERFLOWS(Zero.unsignedAddMayOverflow(Wrap)); 1930 1931 // But usually full/wrap always may overflow. 1932 EXPECT_MAY_OVERFLOW(Full.unsignedAddMayOverflow(One)); 1933 EXPECT_MAY_OVERFLOW(Wrap.unsignedAddMayOverflow(One)); 1934 EXPECT_MAY_OVERFLOW(One.unsignedAddMayOverflow(Full)); 1935 EXPECT_MAY_OVERFLOW(One.unsignedAddMayOverflow(Wrap)); 1936 1937 ConstantRange A(APInt(16, 0xfd00), APInt(16, 0xfe00)); 1938 ConstantRange B1(APInt(16, 0x0100), APInt(16, 0x0201)); 1939 ConstantRange B2(APInt(16, 0x0100), APInt(16, 0x0202)); 1940 EXPECT_NEVER_OVERFLOWS(A.unsignedAddMayOverflow(B1)); 1941 EXPECT_MAY_OVERFLOW(A.unsignedAddMayOverflow(B2)); 1942 EXPECT_NEVER_OVERFLOWS(B1.unsignedAddMayOverflow(A)); 1943 EXPECT_MAY_OVERFLOW(B2.unsignedAddMayOverflow(A)); 1944 1945 ConstantRange C1(APInt(16, 0x0299), APInt(16, 0x0400)); 1946 ConstantRange C2(APInt(16, 0x0300), APInt(16, 0x0400)); 1947 EXPECT_MAY_OVERFLOW(A.unsignedAddMayOverflow(C1)); 1948 EXPECT_ALWAYS_OVERFLOWS_HIGH(A.unsignedAddMayOverflow(C2)); 1949 EXPECT_MAY_OVERFLOW(C1.unsignedAddMayOverflow(A)); 1950 EXPECT_ALWAYS_OVERFLOWS_HIGH(C2.unsignedAddMayOverflow(A)); 1951 } 1952 1953 TEST_F(ConstantRangeTest, UnsignedSubOverflow) { 1954 // Ill-defined - may overflow is a conservative result. 1955 EXPECT_MAY_OVERFLOW(Some.unsignedSubMayOverflow(Empty)); 1956 EXPECT_MAY_OVERFLOW(Empty.unsignedSubMayOverflow(Some)); 1957 1958 // Never overflow despite one full/wrap set. 1959 ConstantRange Zero(APInt::getNullValue(16)); 1960 ConstantRange Max(APInt::getAllOnesValue(16)); 1961 EXPECT_NEVER_OVERFLOWS(Full.unsignedSubMayOverflow(Zero)); 1962 EXPECT_NEVER_OVERFLOWS(Wrap.unsignedSubMayOverflow(Zero)); 1963 EXPECT_NEVER_OVERFLOWS(Max.unsignedSubMayOverflow(Full)); 1964 EXPECT_NEVER_OVERFLOWS(Max.unsignedSubMayOverflow(Wrap)); 1965 1966 // But usually full/wrap always may overflow. 1967 EXPECT_MAY_OVERFLOW(Full.unsignedSubMayOverflow(One)); 1968 EXPECT_MAY_OVERFLOW(Wrap.unsignedSubMayOverflow(One)); 1969 EXPECT_MAY_OVERFLOW(One.unsignedSubMayOverflow(Full)); 1970 EXPECT_MAY_OVERFLOW(One.unsignedSubMayOverflow(Wrap)); 1971 1972 ConstantRange A(APInt(16, 0x0000), APInt(16, 0x0100)); 1973 ConstantRange B(APInt(16, 0x0100), APInt(16, 0x0200)); 1974 EXPECT_NEVER_OVERFLOWS(B.unsignedSubMayOverflow(A)); 1975 EXPECT_ALWAYS_OVERFLOWS_LOW(A.unsignedSubMayOverflow(B)); 1976 1977 ConstantRange A1(APInt(16, 0x0000), APInt(16, 0x0101)); 1978 ConstantRange B1(APInt(16, 0x0100), APInt(16, 0x0201)); 1979 EXPECT_NEVER_OVERFLOWS(B1.unsignedSubMayOverflow(A1)); 1980 EXPECT_MAY_OVERFLOW(A1.unsignedSubMayOverflow(B1)); 1981 1982 ConstantRange A2(APInt(16, 0x0000), APInt(16, 0x0102)); 1983 ConstantRange B2(APInt(16, 0x0100), APInt(16, 0x0202)); 1984 EXPECT_MAY_OVERFLOW(B2.unsignedSubMayOverflow(A2)); 1985 EXPECT_MAY_OVERFLOW(A2.unsignedSubMayOverflow(B2)); 1986 } 1987 1988 TEST_F(ConstantRangeTest, SignedAddOverflow) { 1989 // Ill-defined - may overflow is a conservative result. 1990 EXPECT_MAY_OVERFLOW(Some.signedAddMayOverflow(Empty)); 1991 EXPECT_MAY_OVERFLOW(Empty.signedAddMayOverflow(Some)); 1992 1993 // Never overflow despite one full/wrap set. 1994 ConstantRange Zero(APInt::getNullValue(16)); 1995 EXPECT_NEVER_OVERFLOWS(Full.signedAddMayOverflow(Zero)); 1996 EXPECT_NEVER_OVERFLOWS(Wrap.signedAddMayOverflow(Zero)); 1997 EXPECT_NEVER_OVERFLOWS(Zero.signedAddMayOverflow(Full)); 1998 EXPECT_NEVER_OVERFLOWS(Zero.signedAddMayOverflow(Wrap)); 1999 2000 // But usually full/wrap always may overflow. 2001 EXPECT_MAY_OVERFLOW(Full.signedAddMayOverflow(One)); 2002 EXPECT_MAY_OVERFLOW(Wrap.signedAddMayOverflow(One)); 2003 EXPECT_MAY_OVERFLOW(One.signedAddMayOverflow(Full)); 2004 EXPECT_MAY_OVERFLOW(One.signedAddMayOverflow(Wrap)); 2005 2006 ConstantRange A(APInt(16, 0x7d00), APInt(16, 0x7e00)); 2007 ConstantRange B1(APInt(16, 0x0100), APInt(16, 0x0201)); 2008 ConstantRange B2(APInt(16, 0x0100), APInt(16, 0x0202)); 2009 EXPECT_NEVER_OVERFLOWS(A.signedAddMayOverflow(B1)); 2010 EXPECT_MAY_OVERFLOW(A.signedAddMayOverflow(B2)); 2011 ConstantRange B3(APInt(16, 0x8000), APInt(16, 0x0201)); 2012 ConstantRange B4(APInt(16, 0x8000), APInt(16, 0x0202)); 2013 EXPECT_NEVER_OVERFLOWS(A.signedAddMayOverflow(B3)); 2014 EXPECT_MAY_OVERFLOW(A.signedAddMayOverflow(B4)); 2015 ConstantRange B5(APInt(16, 0x0299), APInt(16, 0x0400)); 2016 ConstantRange B6(APInt(16, 0x0300), APInt(16, 0x0400)); 2017 EXPECT_MAY_OVERFLOW(A.signedAddMayOverflow(B5)); 2018 EXPECT_ALWAYS_OVERFLOWS_HIGH(A.signedAddMayOverflow(B6)); 2019 2020 ConstantRange C(APInt(16, 0x8200), APInt(16, 0x8300)); 2021 ConstantRange D1(APInt(16, 0xfe00), APInt(16, 0xff00)); 2022 ConstantRange D2(APInt(16, 0xfd99), APInt(16, 0xff00)); 2023 EXPECT_NEVER_OVERFLOWS(C.signedAddMayOverflow(D1)); 2024 EXPECT_MAY_OVERFLOW(C.signedAddMayOverflow(D2)); 2025 ConstantRange D3(APInt(16, 0xfe00), APInt(16, 0x8000)); 2026 ConstantRange D4(APInt(16, 0xfd99), APInt(16, 0x8000)); 2027 EXPECT_NEVER_OVERFLOWS(C.signedAddMayOverflow(D3)); 2028 EXPECT_MAY_OVERFLOW(C.signedAddMayOverflow(D4)); 2029 ConstantRange D5(APInt(16, 0xfc00), APInt(16, 0xfd02)); 2030 ConstantRange D6(APInt(16, 0xfc00), APInt(16, 0xfd01)); 2031 EXPECT_MAY_OVERFLOW(C.signedAddMayOverflow(D5)); 2032 EXPECT_ALWAYS_OVERFLOWS_LOW(C.signedAddMayOverflow(D6)); 2033 2034 ConstantRange E(APInt(16, 0xff00), APInt(16, 0x0100)); 2035 EXPECT_NEVER_OVERFLOWS(E.signedAddMayOverflow(E)); 2036 ConstantRange F(APInt(16, 0xf000), APInt(16, 0x7000)); 2037 EXPECT_MAY_OVERFLOW(F.signedAddMayOverflow(F)); 2038 } 2039 2040 TEST_F(ConstantRangeTest, SignedSubOverflow) { 2041 // Ill-defined - may overflow is a conservative result. 2042 EXPECT_MAY_OVERFLOW(Some.signedSubMayOverflow(Empty)); 2043 EXPECT_MAY_OVERFLOW(Empty.signedSubMayOverflow(Some)); 2044 2045 // Never overflow despite one full/wrap set. 2046 ConstantRange Zero(APInt::getNullValue(16)); 2047 EXPECT_NEVER_OVERFLOWS(Full.signedSubMayOverflow(Zero)); 2048 EXPECT_NEVER_OVERFLOWS(Wrap.signedSubMayOverflow(Zero)); 2049 2050 // But usually full/wrap always may overflow. 2051 EXPECT_MAY_OVERFLOW(Full.signedSubMayOverflow(One)); 2052 EXPECT_MAY_OVERFLOW(Wrap.signedSubMayOverflow(One)); 2053 EXPECT_MAY_OVERFLOW(One.signedSubMayOverflow(Full)); 2054 EXPECT_MAY_OVERFLOW(One.signedSubMayOverflow(Wrap)); 2055 2056 ConstantRange A(APInt(16, 0x7d00), APInt(16, 0x7e00)); 2057 ConstantRange B1(APInt(16, 0xfe00), APInt(16, 0xff00)); 2058 ConstantRange B2(APInt(16, 0xfd99), APInt(16, 0xff00)); 2059 EXPECT_NEVER_OVERFLOWS(A.signedSubMayOverflow(B1)); 2060 EXPECT_MAY_OVERFLOW(A.signedSubMayOverflow(B2)); 2061 ConstantRange B3(APInt(16, 0xfc00), APInt(16, 0xfd02)); 2062 ConstantRange B4(APInt(16, 0xfc00), APInt(16, 0xfd01)); 2063 EXPECT_MAY_OVERFLOW(A.signedSubMayOverflow(B3)); 2064 EXPECT_ALWAYS_OVERFLOWS_HIGH(A.signedSubMayOverflow(B4)); 2065 2066 ConstantRange C(APInt(16, 0x8200), APInt(16, 0x8300)); 2067 ConstantRange D1(APInt(16, 0x0100), APInt(16, 0x0201)); 2068 ConstantRange D2(APInt(16, 0x0100), APInt(16, 0x0202)); 2069 EXPECT_NEVER_OVERFLOWS(C.signedSubMayOverflow(D1)); 2070 EXPECT_MAY_OVERFLOW(C.signedSubMayOverflow(D2)); 2071 ConstantRange D3(APInt(16, 0x0299), APInt(16, 0x0400)); 2072 ConstantRange D4(APInt(16, 0x0300), APInt(16, 0x0400)); 2073 EXPECT_MAY_OVERFLOW(C.signedSubMayOverflow(D3)); 2074 EXPECT_ALWAYS_OVERFLOWS_LOW(C.signedSubMayOverflow(D4)); 2075 2076 ConstantRange E(APInt(16, 0xff00), APInt(16, 0x0100)); 2077 EXPECT_NEVER_OVERFLOWS(E.signedSubMayOverflow(E)); 2078 ConstantRange F(APInt(16, 0xf000), APInt(16, 0x7001)); 2079 EXPECT_MAY_OVERFLOW(F.signedSubMayOverflow(F)); 2080 } 2081 2082 template<typename Fn1, typename Fn2> 2083 static void TestOverflowExhaustive(Fn1 OverflowFn, Fn2 MayOverflowFn) { 2084 // Constant range overflow checks are tested exhaustively on 4-bit numbers. 2085 unsigned Bits = 4; 2086 EnumerateTwoConstantRanges(Bits, [=](const ConstantRange &CR1, 2087 const ConstantRange &CR2) { 2088 // Loop over all N1 in CR1 and N2 in CR2 and check whether any of the 2089 // operations have overflow / have no overflow. 2090 bool RangeHasOverflowLow = false; 2091 bool RangeHasOverflowHigh = false; 2092 bool RangeHasNoOverflow = false; 2093 ForeachNumInConstantRange(CR1, [&](const APInt &N1) { 2094 ForeachNumInConstantRange(CR2, [&](const APInt &N2) { 2095 bool IsOverflowHigh; 2096 if (!OverflowFn(IsOverflowHigh, N1, N2)) { 2097 RangeHasNoOverflow = true; 2098 return; 2099 } 2100 2101 if (IsOverflowHigh) 2102 RangeHasOverflowHigh = true; 2103 else 2104 RangeHasOverflowLow = true; 2105 }); 2106 }); 2107 2108 ConstantRange::OverflowResult OR = MayOverflowFn(CR1, CR2); 2109 switch (OR) { 2110 case ConstantRange::OverflowResult::AlwaysOverflowsLow: 2111 EXPECT_TRUE(RangeHasOverflowLow); 2112 EXPECT_FALSE(RangeHasOverflowHigh); 2113 EXPECT_FALSE(RangeHasNoOverflow); 2114 break; 2115 case ConstantRange::OverflowResult::AlwaysOverflowsHigh: 2116 EXPECT_TRUE(RangeHasOverflowHigh); 2117 EXPECT_FALSE(RangeHasOverflowLow); 2118 EXPECT_FALSE(RangeHasNoOverflow); 2119 break; 2120 case ConstantRange::OverflowResult::NeverOverflows: 2121 EXPECT_FALSE(RangeHasOverflowLow); 2122 EXPECT_FALSE(RangeHasOverflowHigh); 2123 EXPECT_TRUE(RangeHasNoOverflow); 2124 break; 2125 case ConstantRange::OverflowResult::MayOverflow: 2126 // We return MayOverflow for empty sets as a conservative result, 2127 // but of course neither the RangeHasOverflow nor the 2128 // RangeHasNoOverflow flags will be set. 2129 if (CR1.isEmptySet() || CR2.isEmptySet()) 2130 break; 2131 2132 EXPECT_TRUE(RangeHasOverflowLow || RangeHasOverflowHigh); 2133 EXPECT_TRUE(RangeHasNoOverflow); 2134 break; 2135 } 2136 }); 2137 } 2138 2139 TEST_F(ConstantRangeTest, UnsignedAddOverflowExhaustive) { 2140 TestOverflowExhaustive( 2141 [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) { 2142 bool Overflow; 2143 (void) N1.uadd_ov(N2, Overflow); 2144 IsOverflowHigh = true; 2145 return Overflow; 2146 }, 2147 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2148 return CR1.unsignedAddMayOverflow(CR2); 2149 }); 2150 } 2151 2152 TEST_F(ConstantRangeTest, UnsignedSubOverflowExhaustive) { 2153 TestOverflowExhaustive( 2154 [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) { 2155 bool Overflow; 2156 (void) N1.usub_ov(N2, Overflow); 2157 IsOverflowHigh = false; 2158 return Overflow; 2159 }, 2160 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2161 return CR1.unsignedSubMayOverflow(CR2); 2162 }); 2163 } 2164 2165 TEST_F(ConstantRangeTest, UnsignedMulOverflowExhaustive) { 2166 TestOverflowExhaustive( 2167 [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) { 2168 bool Overflow; 2169 (void) N1.umul_ov(N2, Overflow); 2170 IsOverflowHigh = true; 2171 return Overflow; 2172 }, 2173 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2174 return CR1.unsignedMulMayOverflow(CR2); 2175 }); 2176 } 2177 2178 TEST_F(ConstantRangeTest, SignedAddOverflowExhaustive) { 2179 TestOverflowExhaustive( 2180 [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) { 2181 bool Overflow; 2182 (void) N1.sadd_ov(N2, Overflow); 2183 IsOverflowHigh = N1.isNonNegative(); 2184 return Overflow; 2185 }, 2186 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2187 return CR1.signedAddMayOverflow(CR2); 2188 }); 2189 } 2190 2191 TEST_F(ConstantRangeTest, SignedSubOverflowExhaustive) { 2192 TestOverflowExhaustive( 2193 [](bool &IsOverflowHigh, const APInt &N1, const APInt &N2) { 2194 bool Overflow; 2195 (void) N1.ssub_ov(N2, Overflow); 2196 IsOverflowHigh = N1.isNonNegative(); 2197 return Overflow; 2198 }, 2199 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2200 return CR1.signedSubMayOverflow(CR2); 2201 }); 2202 } 2203 2204 TEST_F(ConstantRangeTest, FromKnownBits) { 2205 KnownBits Unknown(16); 2206 EXPECT_EQ(Full, ConstantRange::fromKnownBits(Unknown, /*signed*/false)); 2207 EXPECT_EQ(Full, ConstantRange::fromKnownBits(Unknown, /*signed*/true)); 2208 2209 // .10..01. -> unsigned 01000010 (66) to 11011011 (219) 2210 // -> signed 11000010 (194) to 01011011 (91) 2211 KnownBits Known(8); 2212 Known.Zero = 36; 2213 Known.One = 66; 2214 ConstantRange Unsigned(APInt(8, 66), APInt(8, 219 + 1)); 2215 ConstantRange Signed(APInt(8, 194), APInt(8, 91 + 1)); 2216 EXPECT_EQ(Unsigned, ConstantRange::fromKnownBits(Known, /*signed*/false)); 2217 EXPECT_EQ(Signed, ConstantRange::fromKnownBits(Known, /*signed*/true)); 2218 2219 // 1.10.10. -> 10100100 (164) to 11101101 (237) 2220 Known.Zero = 18; 2221 Known.One = 164; 2222 ConstantRange CR1(APInt(8, 164), APInt(8, 237 + 1)); 2223 EXPECT_EQ(CR1, ConstantRange::fromKnownBits(Known, /*signed*/false)); 2224 EXPECT_EQ(CR1, ConstantRange::fromKnownBits(Known, /*signed*/true)); 2225 2226 // 01.0.1.0 -> 01000100 (68) to 01101110 (110) 2227 Known.Zero = 145; 2228 Known.One = 68; 2229 ConstantRange CR2(APInt(8, 68), APInt(8, 110 + 1)); 2230 EXPECT_EQ(CR2, ConstantRange::fromKnownBits(Known, /*signed*/false)); 2231 EXPECT_EQ(CR2, ConstantRange::fromKnownBits(Known, /*signed*/true)); 2232 } 2233 2234 TEST_F(ConstantRangeTest, FromKnownBitsExhaustive) { 2235 unsigned Bits = 4; 2236 unsigned Max = 1 << Bits; 2237 KnownBits Known(Bits); 2238 for (unsigned Zero = 0; Zero < Max; ++Zero) { 2239 for (unsigned One = 0; One < Max; ++One) { 2240 Known.Zero = Zero; 2241 Known.One = One; 2242 if (Known.hasConflict() || Known.isUnknown()) 2243 continue; 2244 2245 UnsignedOpRangeGatherer UR(Bits); 2246 SignedOpRangeGatherer SR(Bits); 2247 for (unsigned N = 0; N < Max; ++N) { 2248 APInt Num(Bits, N); 2249 if ((Num & Known.Zero) != 0 || (~Num & Known.One) != 0) 2250 continue; 2251 2252 UR.account(Num); 2253 SR.account(Num); 2254 } 2255 2256 ConstantRange UnsignedCR = UR.getRange(); 2257 ConstantRange SignedCR = SR.getRange(); 2258 EXPECT_EQ(UnsignedCR, ConstantRange::fromKnownBits(Known, false)); 2259 EXPECT_EQ(SignedCR, ConstantRange::fromKnownBits(Known, true)); 2260 } 2261 } 2262 } 2263 2264 TEST_F(ConstantRangeTest, Negative) { 2265 // All elements in an empty set (of which there are none) are both negative 2266 // and non-negative. Empty & full sets checked explicitly for clarity, but 2267 // they are also covered by the exhaustive test below. 2268 EXPECT_TRUE(Empty.isAllNegative()); 2269 EXPECT_TRUE(Empty.isAllNonNegative()); 2270 EXPECT_FALSE(Full.isAllNegative()); 2271 EXPECT_FALSE(Full.isAllNonNegative()); 2272 2273 unsigned Bits = 4; 2274 EnumerateConstantRanges(Bits, [](const ConstantRange &CR) { 2275 bool AllNegative = true; 2276 bool AllNonNegative = true; 2277 ForeachNumInConstantRange(CR, [&](const APInt &N) { 2278 if (!N.isNegative()) 2279 AllNegative = false; 2280 if (!N.isNonNegative()) 2281 AllNonNegative = false; 2282 }); 2283 assert((CR.isEmptySet() || !AllNegative || !AllNonNegative) && 2284 "Only empty set can be both all negative and all non-negative"); 2285 2286 EXPECT_EQ(AllNegative, CR.isAllNegative()); 2287 EXPECT_EQ(AllNonNegative, CR.isAllNonNegative()); 2288 }); 2289 } 2290 2291 TEST_F(ConstantRangeTest, UAddSat) { 2292 TestBinaryOpExhaustive( 2293 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2294 return CR1.uadd_sat(CR2); 2295 }, 2296 [](const APInt &N1, const APInt &N2) { 2297 return N1.uadd_sat(N2); 2298 }, 2299 PreferSmallestUnsigned); 2300 } 2301 2302 TEST_F(ConstantRangeTest, USubSat) { 2303 TestBinaryOpExhaustive( 2304 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2305 return CR1.usub_sat(CR2); 2306 }, 2307 [](const APInt &N1, const APInt &N2) { 2308 return N1.usub_sat(N2); 2309 }, 2310 PreferSmallestUnsigned); 2311 } 2312 2313 TEST_F(ConstantRangeTest, UMulSat) { 2314 TestBinaryOpExhaustive( 2315 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2316 return CR1.umul_sat(CR2); 2317 }, 2318 [](const APInt &N1, const APInt &N2) { return N1.umul_sat(N2); }, 2319 PreferSmallestUnsigned); 2320 } 2321 2322 TEST_F(ConstantRangeTest, UShlSat) { 2323 TestBinaryOpExhaustive( 2324 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2325 return CR1.ushl_sat(CR2); 2326 }, 2327 [](const APInt &N1, const APInt &N2) { return N1.ushl_sat(N2); }, 2328 PreferSmallestUnsigned); 2329 } 2330 2331 TEST_F(ConstantRangeTest, SAddSat) { 2332 TestBinaryOpExhaustive( 2333 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2334 return CR1.sadd_sat(CR2); 2335 }, 2336 [](const APInt &N1, const APInt &N2) { 2337 return N1.sadd_sat(N2); 2338 }, 2339 PreferSmallestSigned); 2340 } 2341 2342 TEST_F(ConstantRangeTest, SSubSat) { 2343 TestBinaryOpExhaustive( 2344 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2345 return CR1.ssub_sat(CR2); 2346 }, 2347 [](const APInt &N1, const APInt &N2) { 2348 return N1.ssub_sat(N2); 2349 }, 2350 PreferSmallestSigned); 2351 } 2352 2353 TEST_F(ConstantRangeTest, SMulSat) { 2354 TestBinaryOpExhaustive( 2355 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2356 return CR1.smul_sat(CR2); 2357 }, 2358 [](const APInt &N1, const APInt &N2) { return N1.smul_sat(N2); }, 2359 PreferSmallestSigned); 2360 } 2361 2362 TEST_F(ConstantRangeTest, SShlSat) { 2363 TestBinaryOpExhaustive( 2364 [](const ConstantRange &CR1, const ConstantRange &CR2) { 2365 return CR1.sshl_sat(CR2); 2366 }, 2367 [](const APInt &N1, const APInt &N2) { return N1.sshl_sat(N2); }, 2368 PreferSmallestSigned); 2369 } 2370 2371 TEST_F(ConstantRangeTest, Abs) { 2372 TestUnaryOpExhaustive( 2373 [](const ConstantRange &CR) { return CR.abs(); }, 2374 [](const APInt &N) { return N.abs(); }); 2375 2376 TestUnaryOpExhaustive( 2377 [](const ConstantRange &CR) { return CR.abs(/*IntMinIsPoison=*/true); }, 2378 [](const APInt &N) -> Optional<APInt> { 2379 if (N.isMinSignedValue()) 2380 return None; 2381 return N.abs(); 2382 }); 2383 } 2384 2385 TEST_F(ConstantRangeTest, castOps) { 2386 ConstantRange A(APInt(16, 66), APInt(16, 128)); 2387 ConstantRange FpToI8 = A.castOp(Instruction::FPToSI, 8); 2388 EXPECT_EQ(8u, FpToI8.getBitWidth()); 2389 EXPECT_TRUE(FpToI8.isFullSet()); 2390 2391 ConstantRange FpToI16 = A.castOp(Instruction::FPToSI, 16); 2392 EXPECT_EQ(16u, FpToI16.getBitWidth()); 2393 EXPECT_EQ(A, FpToI16); 2394 2395 ConstantRange FPExtToDouble = A.castOp(Instruction::FPExt, 64); 2396 EXPECT_EQ(64u, FPExtToDouble.getBitWidth()); 2397 EXPECT_TRUE(FPExtToDouble.isFullSet()); 2398 2399 ConstantRange PtrToInt = A.castOp(Instruction::PtrToInt, 64); 2400 EXPECT_EQ(64u, PtrToInt.getBitWidth()); 2401 EXPECT_TRUE(PtrToInt.isFullSet()); 2402 2403 ConstantRange IntToPtr = A.castOp(Instruction::IntToPtr, 64); 2404 EXPECT_EQ(64u, IntToPtr.getBitWidth()); 2405 EXPECT_TRUE(IntToPtr.isFullSet()); 2406 } 2407 2408 TEST_F(ConstantRangeTest, binaryXor) { 2409 // Single element ranges. 2410 ConstantRange R16(APInt(8, 16)); 2411 ConstantRange R20(APInt(8, 20)); 2412 EXPECT_EQ(*R16.binaryXor(R16).getSingleElement(), APInt(8, 0)); 2413 EXPECT_EQ(*R16.binaryXor(R20).getSingleElement(), APInt(8, 16 ^ 20)); 2414 2415 // Ranges with more than a single element. Handled conservatively for now. 2416 ConstantRange R16_35(APInt(8, 16), APInt(8, 35)); 2417 ConstantRange R0_99(APInt(8, 0), APInt(8, 99)); 2418 EXPECT_TRUE(R16_35.binaryXor(R16_35).isFullSet()); 2419 EXPECT_TRUE(R16_35.binaryXor(R0_99).isFullSet()); 2420 EXPECT_TRUE(R0_99.binaryXor(R16_35).isFullSet()); 2421 } 2422 2423 TEST_F(ConstantRangeTest, binaryNot) { 2424 TestUnaryOpExhaustive( 2425 [](const ConstantRange &CR) { return CR.binaryNot(); }, 2426 [](const APInt &N) { return ~N; }, 2427 PreferSmallest); 2428 TestUnaryOpExhaustive( 2429 [](const ConstantRange &CR) { 2430 return CR.binaryXor( 2431 ConstantRange(APInt::getAllOnesValue(CR.getBitWidth()))); 2432 }, 2433 [](const APInt &N) { return ~N; }, 2434 PreferSmallest); 2435 TestUnaryOpExhaustive( 2436 [](const ConstantRange &CR) { 2437 return ConstantRange(APInt::getAllOnesValue(CR.getBitWidth())) 2438 .binaryXor(CR); 2439 }, 2440 [](const APInt &N) { return ~N; }, 2441 PreferSmallest); 2442 } 2443 2444 } // anonymous namespace 2445