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