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