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