[clang] [llvm] [ValueTracking] Extend computeConstantRange for add/sub, sext/zext/trunc (PR #181110)
Guy David via cfe-commits
cfe-commits at lists.llvm.org
Sun Feb 15 11:34:04 PST 2026
================
@@ -10242,6 +10242,34 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
// TODO: Return ConstantRange.
setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
CR = ConstantRange::getNonEmpty(Lower, Upper);
+ if (BO->getOpcode() == Instruction::Add ||
+ BO->getOpcode() == Instruction::Sub) {
+ ConstantRange LHS = computeConstantRange(
+ BO->getOperand(0), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
+ ConstantRange RHS = computeConstantRange(
+ BO->getOperand(1), ForSigned, UseInstrInfo, AC, CtxI, DT, Depth + 1);
+ unsigned NoWrapKind = 0;
+ if (IIQ.hasNoUnsignedWrap(BO))
+ NoWrapKind |= OverflowingBinaryOperator::NoUnsignedWrap;
+ if (IIQ.hasNoSignedWrap(BO))
+ NoWrapKind |= OverflowingBinaryOperator::NoSignedWrap;
+ ConstantRange OpCR = BO->getOpcode() == Instruction::Add
+ ? LHS.addWithNoWrap(RHS, NoWrapKind)
+ : LHS.subWithNoWrap(RHS, NoWrapKind);
+ CR = CR.intersectWith(OpCR);
+ }
+ } else if (auto *SExt = dyn_cast<SExtInst>(V)) {
+ CR = computeConstantRange(SExt->getOperand(0), ForSigned, UseInstrInfo, AC,
+ CtxI, DT, Depth + 1)
+ .signExtend(BitWidth);
+ } else if (auto *ZExt = dyn_cast<ZExtInst>(V)) {
+ CR = computeConstantRange(ZExt->getOperand(0), ForSigned, UseInstrInfo, AC,
+ CtxI, DT, Depth + 1)
+ .zeroExtend(BitWidth);
+ } else if (auto *Trunc = dyn_cast<TruncInst>(V)) {
+ CR = computeConstantRange(Trunc->getOperand(0), ForSigned, UseInstrInfo, AC,
+ CtxI, DT, Depth + 1)
+ .truncate(BitWidth);
----------------
guy-david wrote:
FP conversions are also casts so I kept the handling of those as is, but I did try to restructure it for sext/zext/trunc.
https://github.com/llvm/llvm-project/pull/181110
More information about the cfe-commits
mailing list