1 //===- InferIntRangeCommon.cpp - Inference for common ops ------------===// 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 // This file contains implementations of range inference for operations that are 10 // common to both the `arith` and `index` dialects to facilitate reuse. 11 // 12 //===----------------------------------------------------------------------===// 13 14 #include "mlir/Interfaces/Utils/InferIntRangeCommon.h" 15 16 #include "mlir/Interfaces/InferIntRangeInterface.h" 17 18 #include "llvm/ADT/ArrayRef.h" 19 #include "llvm/ADT/STLExtras.h" 20 21 #include "llvm/Support/Debug.h" 22 23 #include <iterator> 24 #include <optional> 25 26 using namespace mlir; 27 28 #define DEBUG_TYPE "int-range-analysis" 29 30 //===----------------------------------------------------------------------===// 31 // General utilities 32 //===----------------------------------------------------------------------===// 33 34 /// Function that evaluates the result of doing something on arithmetic 35 /// constants and returns std::nullopt on overflow. 36 using ConstArithFn = 37 function_ref<std::optional<APInt>(const APInt &, const APInt &)>; 38 using ConstArithStdFn = 39 std::function<std::optional<APInt>(const APInt &, const APInt &)>; 40 41 /// Compute op(minLeft, minRight) and op(maxLeft, maxRight) if possible, 42 /// If either computation overflows, make the result unbounded. 43 static ConstantIntRanges computeBoundsBy(ConstArithFn op, const APInt &minLeft, 44 const APInt &minRight, 45 const APInt &maxLeft, 46 const APInt &maxRight, bool isSigned) { 47 std::optional<APInt> maybeMin = op(minLeft, minRight); 48 std::optional<APInt> maybeMax = op(maxLeft, maxRight); 49 if (maybeMin && maybeMax) 50 return ConstantIntRanges::range(*maybeMin, *maybeMax, isSigned); 51 return ConstantIntRanges::maxRange(minLeft.getBitWidth()); 52 } 53 54 /// Compute the minimum and maximum of `(op(l, r) for l in lhs for r in rhs)`, 55 /// ignoring unbounded values. Returns the maximal range if `op` overflows. 56 static ConstantIntRanges minMaxBy(ConstArithFn op, ArrayRef<APInt> lhs, 57 ArrayRef<APInt> rhs, bool isSigned) { 58 unsigned width = lhs[0].getBitWidth(); 59 APInt min = 60 isSigned ? APInt::getSignedMaxValue(width) : APInt::getMaxValue(width); 61 APInt max = 62 isSigned ? APInt::getSignedMinValue(width) : APInt::getZero(width); 63 for (const APInt &left : lhs) { 64 for (const APInt &right : rhs) { 65 std::optional<APInt> maybeThisResult = op(left, right); 66 if (!maybeThisResult) 67 return ConstantIntRanges::maxRange(width); 68 APInt result = std::move(*maybeThisResult); 69 min = (isSigned ? result.slt(min) : result.ult(min)) ? result : min; 70 max = (isSigned ? result.sgt(max) : result.ugt(max)) ? result : max; 71 } 72 } 73 return ConstantIntRanges::range(min, max, isSigned); 74 } 75 76 //===----------------------------------------------------------------------===// 77 // Ext, trunc, index op handling 78 //===----------------------------------------------------------------------===// 79 80 ConstantIntRanges 81 mlir::intrange::inferIndexOp(const InferRangeFn &inferFn, 82 ArrayRef<ConstantIntRanges> argRanges, 83 intrange::CmpMode mode) { 84 ConstantIntRanges sixtyFour = inferFn(argRanges); 85 SmallVector<ConstantIntRanges, 2> truncated; 86 llvm::transform(argRanges, std::back_inserter(truncated), 87 [](const ConstantIntRanges &range) { 88 return truncRange(range, /*destWidth=*/indexMinWidth); 89 }); 90 ConstantIntRanges thirtyTwo = inferFn(truncated); 91 ConstantIntRanges thirtyTwoAsSixtyFour = 92 extRange(thirtyTwo, /*destWidth=*/indexMaxWidth); 93 ConstantIntRanges sixtyFourAsThirtyTwo = 94 truncRange(sixtyFour, /*destWidth=*/indexMinWidth); 95 96 LLVM_DEBUG(llvm::dbgs() << "Index handling: 64-bit result = " << sixtyFour 97 << " 32-bit = " << thirtyTwo << "\n"); 98 bool truncEqual = false; 99 switch (mode) { 100 case intrange::CmpMode::Both: 101 truncEqual = (thirtyTwo == sixtyFourAsThirtyTwo); 102 break; 103 case intrange::CmpMode::Signed: 104 truncEqual = (thirtyTwo.smin() == sixtyFourAsThirtyTwo.smin() && 105 thirtyTwo.smax() == sixtyFourAsThirtyTwo.smax()); 106 break; 107 case intrange::CmpMode::Unsigned: 108 truncEqual = (thirtyTwo.umin() == sixtyFourAsThirtyTwo.umin() && 109 thirtyTwo.umax() == sixtyFourAsThirtyTwo.umax()); 110 break; 111 } 112 if (truncEqual) 113 // Returing the 64-bit result preserves more information. 114 return sixtyFour; 115 ConstantIntRanges merged = sixtyFour.rangeUnion(thirtyTwoAsSixtyFour); 116 return merged; 117 } 118 119 ConstantIntRanges mlir::intrange::extRange(const ConstantIntRanges &range, 120 unsigned int destWidth) { 121 APInt umin = range.umin().zext(destWidth); 122 APInt umax = range.umax().zext(destWidth); 123 APInt smin = range.smin().sext(destWidth); 124 APInt smax = range.smax().sext(destWidth); 125 return {umin, umax, smin, smax}; 126 } 127 128 ConstantIntRanges mlir::intrange::extUIRange(const ConstantIntRanges &range, 129 unsigned destWidth) { 130 APInt umin = range.umin().zext(destWidth); 131 APInt umax = range.umax().zext(destWidth); 132 return ConstantIntRanges::fromUnsigned(umin, umax); 133 } 134 135 ConstantIntRanges mlir::intrange::extSIRange(const ConstantIntRanges &range, 136 unsigned destWidth) { 137 APInt smin = range.smin().sext(destWidth); 138 APInt smax = range.smax().sext(destWidth); 139 return ConstantIntRanges::fromSigned(smin, smax); 140 } 141 142 ConstantIntRanges mlir::intrange::truncRange(const ConstantIntRanges &range, 143 unsigned int destWidth) { 144 // If you truncate the first four bytes in [0xaaaabbbb, 0xccccbbbb], 145 // the range of the resulting value is not contiguous ind includes 0. 146 // Ex. If you truncate [256, 258] from i16 to i8, you validly get [0, 2], 147 // but you can't truncate [255, 257] similarly. 148 bool hasUnsignedRollover = 149 range.umin().lshr(destWidth) != range.umax().lshr(destWidth); 150 APInt umin = hasUnsignedRollover ? APInt::getZero(destWidth) 151 : range.umin().trunc(destWidth); 152 APInt umax = hasUnsignedRollover ? APInt::getMaxValue(destWidth) 153 : range.umax().trunc(destWidth); 154 155 // Signed post-truncation rollover will not occur when either: 156 // - The high parts of the min and max, plus the sign bit, are the same 157 // - The high halves + sign bit of the min and max are either all 1s or all 0s 158 // and you won't create a [positive, negative] range by truncating. 159 // For example, you can truncate the ranges [256, 258]_i16 to [0, 2]_i8 160 // but not [255, 257]_i16 to a range of i8s. You can also truncate 161 // [-256, -256]_i16 to [-2, 0]_i8, but not [-257, -255]_i16. 162 // You can also truncate [-130, 0]_i16 to i8 because -130_i16 (0xff7e) 163 // will truncate to 0x7e, which is greater than 0 164 APInt sminHighPart = range.smin().ashr(destWidth - 1); 165 APInt smaxHighPart = range.smax().ashr(destWidth - 1); 166 bool hasSignedOverflow = 167 (sminHighPart != smaxHighPart) && 168 !(sminHighPart.isAllOnes() && 169 (smaxHighPart.isAllOnes() || smaxHighPart.isZero())) && 170 !(sminHighPart.isZero() && smaxHighPart.isZero()); 171 APInt smin = hasSignedOverflow ? APInt::getSignedMinValue(destWidth) 172 : range.smin().trunc(destWidth); 173 APInt smax = hasSignedOverflow ? APInt::getSignedMaxValue(destWidth) 174 : range.smax().trunc(destWidth); 175 return {umin, umax, smin, smax}; 176 } 177 178 //===----------------------------------------------------------------------===// 179 // Addition 180 //===----------------------------------------------------------------------===// 181 182 ConstantIntRanges 183 mlir::intrange::inferAdd(ArrayRef<ConstantIntRanges> argRanges, 184 OverflowFlags ovfFlags) { 185 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 186 187 ConstArithStdFn uadd = [=](const APInt &a, 188 const APInt &b) -> std::optional<APInt> { 189 bool overflowed = false; 190 APInt result = any(ovfFlags & OverflowFlags::Nuw) 191 ? a.uadd_sat(b) 192 : a.uadd_ov(b, overflowed); 193 return overflowed ? std::optional<APInt>() : result; 194 }; 195 ConstArithStdFn sadd = [=](const APInt &a, 196 const APInt &b) -> std::optional<APInt> { 197 bool overflowed = false; 198 APInt result = any(ovfFlags & OverflowFlags::Nsw) 199 ? a.sadd_sat(b) 200 : a.sadd_ov(b, overflowed); 201 return overflowed ? std::optional<APInt>() : result; 202 }; 203 204 ConstantIntRanges urange = computeBoundsBy( 205 uadd, lhs.umin(), rhs.umin(), lhs.umax(), rhs.umax(), /*isSigned=*/false); 206 ConstantIntRanges srange = computeBoundsBy( 207 sadd, lhs.smin(), rhs.smin(), lhs.smax(), rhs.smax(), /*isSigned=*/true); 208 return urange.intersection(srange); 209 } 210 211 //===----------------------------------------------------------------------===// 212 // Subtraction 213 //===----------------------------------------------------------------------===// 214 215 ConstantIntRanges 216 mlir::intrange::inferSub(ArrayRef<ConstantIntRanges> argRanges, 217 OverflowFlags ovfFlags) { 218 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 219 220 ConstArithStdFn usub = [=](const APInt &a, 221 const APInt &b) -> std::optional<APInt> { 222 bool overflowed = false; 223 APInt result = any(ovfFlags & OverflowFlags::Nuw) 224 ? a.usub_sat(b) 225 : a.usub_ov(b, overflowed); 226 return overflowed ? std::optional<APInt>() : result; 227 }; 228 ConstArithStdFn ssub = [=](const APInt &a, 229 const APInt &b) -> std::optional<APInt> { 230 bool overflowed = false; 231 APInt result = any(ovfFlags & OverflowFlags::Nsw) 232 ? a.ssub_sat(b) 233 : a.ssub_ov(b, overflowed); 234 return overflowed ? std::optional<APInt>() : result; 235 }; 236 ConstantIntRanges urange = computeBoundsBy( 237 usub, lhs.umin(), rhs.umax(), lhs.umax(), rhs.umin(), /*isSigned=*/false); 238 ConstantIntRanges srange = computeBoundsBy( 239 ssub, lhs.smin(), rhs.smax(), lhs.smax(), rhs.smin(), /*isSigned=*/true); 240 return urange.intersection(srange); 241 } 242 243 //===----------------------------------------------------------------------===// 244 // Multiplication 245 //===----------------------------------------------------------------------===// 246 247 ConstantIntRanges 248 mlir::intrange::inferMul(ArrayRef<ConstantIntRanges> argRanges, 249 OverflowFlags ovfFlags) { 250 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 251 252 ConstArithStdFn umul = [=](const APInt &a, 253 const APInt &b) -> std::optional<APInt> { 254 bool overflowed = false; 255 APInt result = any(ovfFlags & OverflowFlags::Nuw) 256 ? a.umul_sat(b) 257 : a.umul_ov(b, overflowed); 258 return overflowed ? std::optional<APInt>() : result; 259 }; 260 ConstArithStdFn smul = [=](const APInt &a, 261 const APInt &b) -> std::optional<APInt> { 262 bool overflowed = false; 263 APInt result = any(ovfFlags & OverflowFlags::Nsw) 264 ? a.smul_sat(b) 265 : a.smul_ov(b, overflowed); 266 return overflowed ? std::optional<APInt>() : result; 267 }; 268 269 ConstantIntRanges urange = 270 minMaxBy(umul, {lhs.umin(), lhs.umax()}, {rhs.umin(), rhs.umax()}, 271 /*isSigned=*/false); 272 ConstantIntRanges srange = 273 minMaxBy(smul, {lhs.smin(), lhs.smax()}, {rhs.smin(), rhs.smax()}, 274 /*isSigned=*/true); 275 return urange.intersection(srange); 276 } 277 278 //===----------------------------------------------------------------------===// 279 // DivU, CeilDivU (Unsigned division) 280 //===----------------------------------------------------------------------===// 281 282 /// Fix up division results (ex. for ceiling and floor), returning an APInt 283 /// if there has been no overflow 284 using DivisionFixupFn = function_ref<std::optional<APInt>( 285 const APInt &lhs, const APInt &rhs, const APInt &result)>; 286 287 static ConstantIntRanges inferDivURange(const ConstantIntRanges &lhs, 288 const ConstantIntRanges &rhs, 289 DivisionFixupFn fixup) { 290 const APInt &lhsMin = lhs.umin(), &lhsMax = lhs.umax(), &rhsMin = rhs.umin(), 291 &rhsMax = rhs.umax(); 292 293 if (!rhsMin.isZero()) { 294 auto udiv = [&fixup](const APInt &a, 295 const APInt &b) -> std::optional<APInt> { 296 return fixup(a, b, a.udiv(b)); 297 }; 298 return minMaxBy(udiv, {lhsMin, lhsMax}, {rhsMin, rhsMax}, 299 /*isSigned=*/false); 300 } 301 302 APInt umin = APInt::getZero(rhsMin.getBitWidth()); 303 if (lhsMin.uge(rhsMax) && !rhsMax.isZero()) 304 umin = lhsMin.udiv(rhsMax); 305 306 // X u/ Y u<= X. 307 APInt umax = lhsMax; 308 return ConstantIntRanges::fromUnsigned(umin, umax); 309 } 310 311 ConstantIntRanges 312 mlir::intrange::inferDivU(ArrayRef<ConstantIntRanges> argRanges) { 313 return inferDivURange(argRanges[0], argRanges[1], 314 [](const APInt &lhs, const APInt &rhs, 315 const APInt &result) { return result; }); 316 } 317 318 ConstantIntRanges 319 mlir::intrange::inferCeilDivU(ArrayRef<ConstantIntRanges> argRanges) { 320 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 321 322 auto ceilDivUIFix = [](const APInt &lhs, const APInt &rhs, 323 const APInt &result) -> std::optional<APInt> { 324 if (!lhs.urem(rhs).isZero()) { 325 bool overflowed = false; 326 APInt corrected = 327 result.uadd_ov(APInt(result.getBitWidth(), 1), overflowed); 328 return overflowed ? std::optional<APInt>() : corrected; 329 } 330 return result; 331 }; 332 return inferDivURange(lhs, rhs, ceilDivUIFix); 333 } 334 335 //===----------------------------------------------------------------------===// 336 // DivS, CeilDivS, FloorDivS (Signed division) 337 //===----------------------------------------------------------------------===// 338 339 static ConstantIntRanges inferDivSRange(const ConstantIntRanges &lhs, 340 const ConstantIntRanges &rhs, 341 DivisionFixupFn fixup) { 342 const APInt &lhsMin = lhs.smin(), &lhsMax = lhs.smax(), &rhsMin = rhs.smin(), 343 &rhsMax = rhs.smax(); 344 bool canDivide = rhsMin.isStrictlyPositive() || rhsMax.isNegative(); 345 346 if (canDivide) { 347 auto sdiv = [&fixup](const APInt &a, 348 const APInt &b) -> std::optional<APInt> { 349 bool overflowed = false; 350 APInt result = a.sdiv_ov(b, overflowed); 351 return overflowed ? std::optional<APInt>() : fixup(a, b, result); 352 }; 353 return minMaxBy(sdiv, {lhsMin, lhsMax}, {rhsMin, rhsMax}, 354 /*isSigned=*/true); 355 } 356 return ConstantIntRanges::maxRange(rhsMin.getBitWidth()); 357 } 358 359 ConstantIntRanges 360 mlir::intrange::inferDivS(ArrayRef<ConstantIntRanges> argRanges) { 361 return inferDivSRange(argRanges[0], argRanges[1], 362 [](const APInt &lhs, const APInt &rhs, 363 const APInt &result) { return result; }); 364 } 365 366 ConstantIntRanges 367 mlir::intrange::inferCeilDivS(ArrayRef<ConstantIntRanges> argRanges) { 368 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 369 370 auto ceilDivSIFix = [](const APInt &lhs, const APInt &rhs, 371 const APInt &result) -> std::optional<APInt> { 372 if (!lhs.srem(rhs).isZero() && lhs.isNonNegative() == rhs.isNonNegative()) { 373 bool overflowed = false; 374 APInt corrected = 375 result.sadd_ov(APInt(result.getBitWidth(), 1), overflowed); 376 return overflowed ? std::optional<APInt>() : corrected; 377 } 378 return result; 379 }; 380 return inferDivSRange(lhs, rhs, ceilDivSIFix); 381 } 382 383 ConstantIntRanges 384 mlir::intrange::inferFloorDivS(ArrayRef<ConstantIntRanges> argRanges) { 385 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 386 387 auto floorDivSIFix = [](const APInt &lhs, const APInt &rhs, 388 const APInt &result) -> std::optional<APInt> { 389 if (!lhs.srem(rhs).isZero() && lhs.isNonNegative() != rhs.isNonNegative()) { 390 bool overflowed = false; 391 APInt corrected = 392 result.ssub_ov(APInt(result.getBitWidth(), 1), overflowed); 393 return overflowed ? std::optional<APInt>() : corrected; 394 } 395 return result; 396 }; 397 return inferDivSRange(lhs, rhs, floorDivSIFix); 398 } 399 400 //===----------------------------------------------------------------------===// 401 // Signed remainder (RemS) 402 //===----------------------------------------------------------------------===// 403 404 ConstantIntRanges 405 mlir::intrange::inferRemS(ArrayRef<ConstantIntRanges> argRanges) { 406 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 407 const APInt &lhsMin = lhs.smin(), &lhsMax = lhs.smax(), &rhsMin = rhs.smin(), 408 &rhsMax = rhs.smax(); 409 410 unsigned width = rhsMax.getBitWidth(); 411 APInt smin = APInt::getSignedMinValue(width); 412 APInt smax = APInt::getSignedMaxValue(width); 413 // No bounds if zero could be a divisor. 414 bool canBound = (rhsMin.isStrictlyPositive() || rhsMax.isNegative()); 415 if (canBound) { 416 APInt maxDivisor = rhsMin.isStrictlyPositive() ? rhsMax : rhsMin.abs(); 417 bool canNegativeDividend = lhsMin.isNegative(); 418 bool canPositiveDividend = lhsMax.isStrictlyPositive(); 419 APInt zero = APInt::getZero(maxDivisor.getBitWidth()); 420 APInt maxPositiveResult = maxDivisor - 1; 421 APInt minNegativeResult = -maxPositiveResult; 422 smin = canNegativeDividend ? minNegativeResult : zero; 423 smax = canPositiveDividend ? maxPositiveResult : zero; 424 // Special case: sweeping out a contiguous range in N/[modulus]. 425 if (rhsMin == rhsMax) { 426 if ((lhsMax - lhsMin).ult(maxDivisor)) { 427 APInt minRem = lhsMin.srem(maxDivisor); 428 APInt maxRem = lhsMax.srem(maxDivisor); 429 if (minRem.sle(maxRem)) { 430 smin = minRem; 431 smax = maxRem; 432 } 433 } 434 } 435 } 436 return ConstantIntRanges::fromSigned(smin, smax); 437 } 438 439 //===----------------------------------------------------------------------===// 440 // Unsigned remainder (RemU) 441 //===----------------------------------------------------------------------===// 442 443 ConstantIntRanges 444 mlir::intrange::inferRemU(ArrayRef<ConstantIntRanges> argRanges) { 445 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 446 const APInt &rhsMin = rhs.umin(), &rhsMax = rhs.umax(); 447 448 unsigned width = rhsMin.getBitWidth(); 449 APInt umin = APInt::getZero(width); 450 // Remainder can't be larger than either of its arguments. 451 APInt umax = llvm::APIntOps::umin((rhsMax - 1), lhs.umax()); 452 453 if (!rhsMin.isZero()) { 454 // Special case: sweeping out a contiguous range in N/[modulus] 455 if (rhsMin == rhsMax) { 456 const APInt &lhsMin = lhs.umin(), &lhsMax = lhs.umax(); 457 if ((lhsMax - lhsMin).ult(rhsMax)) { 458 APInt minRem = lhsMin.urem(rhsMax); 459 APInt maxRem = lhsMax.urem(rhsMax); 460 if (minRem.ule(maxRem)) { 461 umin = minRem; 462 umax = maxRem; 463 } 464 } 465 } 466 } 467 return ConstantIntRanges::fromUnsigned(umin, umax); 468 } 469 470 //===----------------------------------------------------------------------===// 471 // Max and min (MaxS, MaxU, MinS, MinU) 472 //===----------------------------------------------------------------------===// 473 474 ConstantIntRanges 475 mlir::intrange::inferMaxS(ArrayRef<ConstantIntRanges> argRanges) { 476 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 477 478 const APInt &smin = lhs.smin().sgt(rhs.smin()) ? lhs.smin() : rhs.smin(); 479 const APInt &smax = lhs.smax().sgt(rhs.smax()) ? lhs.smax() : rhs.smax(); 480 return ConstantIntRanges::fromSigned(smin, smax); 481 } 482 483 ConstantIntRanges 484 mlir::intrange::inferMaxU(ArrayRef<ConstantIntRanges> argRanges) { 485 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 486 487 const APInt &umin = lhs.umin().ugt(rhs.umin()) ? lhs.umin() : rhs.umin(); 488 const APInt &umax = lhs.umax().ugt(rhs.umax()) ? lhs.umax() : rhs.umax(); 489 return ConstantIntRanges::fromUnsigned(umin, umax); 490 } 491 492 ConstantIntRanges 493 mlir::intrange::inferMinS(ArrayRef<ConstantIntRanges> argRanges) { 494 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 495 496 const APInt &smin = lhs.smin().slt(rhs.smin()) ? lhs.smin() : rhs.smin(); 497 const APInt &smax = lhs.smax().slt(rhs.smax()) ? lhs.smax() : rhs.smax(); 498 return ConstantIntRanges::fromSigned(smin, smax); 499 } 500 501 ConstantIntRanges 502 mlir::intrange::inferMinU(ArrayRef<ConstantIntRanges> argRanges) { 503 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 504 505 const APInt &umin = lhs.umin().ult(rhs.umin()) ? lhs.umin() : rhs.umin(); 506 const APInt &umax = lhs.umax().ult(rhs.umax()) ? lhs.umax() : rhs.umax(); 507 return ConstantIntRanges::fromUnsigned(umin, umax); 508 } 509 510 //===----------------------------------------------------------------------===// 511 // Bitwise operators (And, Or, Xor) 512 //===----------------------------------------------------------------------===// 513 514 /// "Widen" bounds - if 0bvvvvv??? <= a <= 0bvvvvv???, 515 /// relax the bounds to 0bvvvvv000 <= a <= 0bvvvvv111, where vvvvv are the bits 516 /// that both bonuds have in common. This gives us a consertive approximation 517 /// for what values can be passed to bitwise operations. 518 static std::tuple<APInt, APInt> 519 widenBitwiseBounds(const ConstantIntRanges &bound) { 520 APInt leftVal = bound.umin(), rightVal = bound.umax(); 521 unsigned bitwidth = leftVal.getBitWidth(); 522 unsigned differingBits = bitwidth - (leftVal ^ rightVal).countl_zero(); 523 leftVal.clearLowBits(differingBits); 524 rightVal.setLowBits(differingBits); 525 return std::make_tuple(std::move(leftVal), std::move(rightVal)); 526 } 527 528 ConstantIntRanges 529 mlir::intrange::inferAnd(ArrayRef<ConstantIntRanges> argRanges) { 530 auto [lhsZeros, lhsOnes] = widenBitwiseBounds(argRanges[0]); 531 auto [rhsZeros, rhsOnes] = widenBitwiseBounds(argRanges[1]); 532 auto andi = [](const APInt &a, const APInt &b) -> std::optional<APInt> { 533 return a & b; 534 }; 535 return minMaxBy(andi, {lhsZeros, lhsOnes}, {rhsZeros, rhsOnes}, 536 /*isSigned=*/false); 537 } 538 539 ConstantIntRanges 540 mlir::intrange::inferOr(ArrayRef<ConstantIntRanges> argRanges) { 541 auto [lhsZeros, lhsOnes] = widenBitwiseBounds(argRanges[0]); 542 auto [rhsZeros, rhsOnes] = widenBitwiseBounds(argRanges[1]); 543 auto ori = [](const APInt &a, const APInt &b) -> std::optional<APInt> { 544 return a | b; 545 }; 546 return minMaxBy(ori, {lhsZeros, lhsOnes}, {rhsZeros, rhsOnes}, 547 /*isSigned=*/false); 548 } 549 550 ConstantIntRanges 551 mlir::intrange::inferXor(ArrayRef<ConstantIntRanges> argRanges) { 552 auto [lhsZeros, lhsOnes] = widenBitwiseBounds(argRanges[0]); 553 auto [rhsZeros, rhsOnes] = widenBitwiseBounds(argRanges[1]); 554 auto xori = [](const APInt &a, const APInt &b) -> std::optional<APInt> { 555 return a ^ b; 556 }; 557 return minMaxBy(xori, {lhsZeros, lhsOnes}, {rhsZeros, rhsOnes}, 558 /*isSigned=*/false); 559 } 560 561 //===----------------------------------------------------------------------===// 562 // Shifts (Shl, ShrS, ShrU) 563 //===----------------------------------------------------------------------===// 564 565 ConstantIntRanges 566 mlir::intrange::inferShl(ArrayRef<ConstantIntRanges> argRanges, 567 OverflowFlags ovfFlags) { 568 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 569 const APInt &rhsUMin = rhs.umin(), &rhsUMax = rhs.umax(); 570 571 // The signed/unsigned overflow behavior of shl by `rhs` matches a mul with 572 // 2^rhs. 573 ConstArithStdFn ushl = [=](const APInt &l, 574 const APInt &r) -> std::optional<APInt> { 575 bool overflowed = false; 576 APInt result = any(ovfFlags & OverflowFlags::Nuw) 577 ? l.ushl_sat(r) 578 : l.ushl_ov(r, overflowed); 579 return overflowed ? std::optional<APInt>() : result; 580 }; 581 ConstArithStdFn sshl = [=](const APInt &l, 582 const APInt &r) -> std::optional<APInt> { 583 bool overflowed = false; 584 APInt result = any(ovfFlags & OverflowFlags::Nsw) 585 ? l.sshl_sat(r) 586 : l.sshl_ov(r, overflowed); 587 return overflowed ? std::optional<APInt>() : result; 588 }; 589 590 ConstantIntRanges urange = 591 minMaxBy(ushl, {lhs.umin(), lhs.umax()}, {rhsUMin, rhsUMax}, 592 /*isSigned=*/false); 593 ConstantIntRanges srange = 594 minMaxBy(sshl, {lhs.smin(), lhs.smax()}, {rhsUMin, rhsUMax}, 595 /*isSigned=*/true); 596 return urange.intersection(srange); 597 } 598 599 ConstantIntRanges 600 mlir::intrange::inferShrS(ArrayRef<ConstantIntRanges> argRanges) { 601 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 602 603 auto ashr = [](const APInt &l, const APInt &r) -> std::optional<APInt> { 604 return r.uge(r.getBitWidth()) ? std::optional<APInt>() : l.ashr(r); 605 }; 606 607 return minMaxBy(ashr, {lhs.smin(), lhs.smax()}, {rhs.umin(), rhs.umax()}, 608 /*isSigned=*/true); 609 } 610 611 ConstantIntRanges 612 mlir::intrange::inferShrU(ArrayRef<ConstantIntRanges> argRanges) { 613 const ConstantIntRanges &lhs = argRanges[0], &rhs = argRanges[1]; 614 615 auto lshr = [](const APInt &l, const APInt &r) -> std::optional<APInt> { 616 return r.uge(r.getBitWidth()) ? std::optional<APInt>() : l.lshr(r); 617 }; 618 return minMaxBy(lshr, {lhs.umin(), lhs.umax()}, {rhs.umin(), rhs.umax()}, 619 /*isSigned=*/false); 620 } 621 622 //===----------------------------------------------------------------------===// 623 // Comparisons (Cmp) 624 //===----------------------------------------------------------------------===// 625 626 static intrange::CmpPredicate invertPredicate(intrange::CmpPredicate pred) { 627 switch (pred) { 628 case intrange::CmpPredicate::eq: 629 return intrange::CmpPredicate::ne; 630 case intrange::CmpPredicate::ne: 631 return intrange::CmpPredicate::eq; 632 case intrange::CmpPredicate::slt: 633 return intrange::CmpPredicate::sge; 634 case intrange::CmpPredicate::sle: 635 return intrange::CmpPredicate::sgt; 636 case intrange::CmpPredicate::sgt: 637 return intrange::CmpPredicate::sle; 638 case intrange::CmpPredicate::sge: 639 return intrange::CmpPredicate::slt; 640 case intrange::CmpPredicate::ult: 641 return intrange::CmpPredicate::uge; 642 case intrange::CmpPredicate::ule: 643 return intrange::CmpPredicate::ugt; 644 case intrange::CmpPredicate::ugt: 645 return intrange::CmpPredicate::ule; 646 case intrange::CmpPredicate::uge: 647 return intrange::CmpPredicate::ult; 648 } 649 llvm_unreachable("unknown cmp predicate value"); 650 } 651 652 static bool isStaticallyTrue(intrange::CmpPredicate pred, 653 const ConstantIntRanges &lhs, 654 const ConstantIntRanges &rhs) { 655 switch (pred) { 656 case intrange::CmpPredicate::sle: 657 return lhs.smax().sle(rhs.smin()); 658 case intrange::CmpPredicate::slt: 659 return lhs.smax().slt(rhs.smin()); 660 case intrange::CmpPredicate::ule: 661 return lhs.umax().ule(rhs.umin()); 662 case intrange::CmpPredicate::ult: 663 return lhs.umax().ult(rhs.umin()); 664 case intrange::CmpPredicate::sge: 665 return lhs.smin().sge(rhs.smax()); 666 case intrange::CmpPredicate::sgt: 667 return lhs.smin().sgt(rhs.smax()); 668 case intrange::CmpPredicate::uge: 669 return lhs.umin().uge(rhs.umax()); 670 case intrange::CmpPredicate::ugt: 671 return lhs.umin().ugt(rhs.umax()); 672 case intrange::CmpPredicate::eq: { 673 std::optional<APInt> lhsConst = lhs.getConstantValue(); 674 std::optional<APInt> rhsConst = rhs.getConstantValue(); 675 return lhsConst && rhsConst && lhsConst == rhsConst; 676 } 677 case intrange::CmpPredicate::ne: { 678 // While equality requires that there is an interpration of the preceeding 679 // computations that produces equal constants, whether that be signed or 680 // unsigned, statically determining inequality requires that neither 681 // interpretation produce potentially overlapping ranges. 682 bool sne = isStaticallyTrue(intrange::CmpPredicate::slt, lhs, rhs) || 683 isStaticallyTrue(intrange::CmpPredicate::sgt, lhs, rhs); 684 bool une = isStaticallyTrue(intrange::CmpPredicate::ult, lhs, rhs) || 685 isStaticallyTrue(intrange::CmpPredicate::ugt, lhs, rhs); 686 return sne && une; 687 } 688 } 689 return false; 690 } 691 692 std::optional<bool> mlir::intrange::evaluatePred(CmpPredicate pred, 693 const ConstantIntRanges &lhs, 694 const ConstantIntRanges &rhs) { 695 if (isStaticallyTrue(pred, lhs, rhs)) 696 return true; 697 if (isStaticallyTrue(invertPredicate(pred), lhs, rhs)) 698 return false; 699 return std::nullopt; 700 } 701