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