Skip to content

Commit 531e066

Browse files
committed
[ValueTracking] Return ConstantRange for intrinsic ranges (NFC)
Instead of setting Lower and Upper, return a ConstantRange. Should do this for the others as well.
1 parent a7a8e23 commit 531e066

File tree

1 file changed

+65
-57
lines changed

1 file changed

+65
-57
lines changed

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 65 additions & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -7354,67 +7354,66 @@ static void setLimitsForBinOp(const BinaryOperator &BO, APInt &Lower,
73547354
}
73557355
}
73567356

7357-
static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
7358-
APInt &Upper) {
7359-
unsigned Width = Lower.getBitWidth();
7357+
static ConstantRange getRangeForIntrinsic(const IntrinsicInst &II) {
7358+
unsigned Width = II.getType()->getScalarSizeInBits();
73607359
const APInt *C;
73617360
switch (II.getIntrinsicID()) {
73627361
case Intrinsic::ctpop:
73637362
case Intrinsic::ctlz:
73647363
case Intrinsic::cttz:
73657364
// Maximum of set/clear bits is the bit width.
7366-
assert(Lower == 0 && "Expected lower bound to be zero");
7367-
Upper = Width + 1;
7368-
break;
7365+
return ConstantRange(APInt::getZero(Width), APInt(Width, Width + 1));
73697366
case Intrinsic::uadd_sat:
73707367
// uadd.sat(x, C) produces [C, UINT_MAX].
73717368
if (match(II.getOperand(0), m_APInt(C)) ||
73727369
match(II.getOperand(1), m_APInt(C)))
7373-
Lower = *C;
7370+
return ConstantRange::getNonEmpty(*C, APInt::getZero(Width));
73747371
break;
73757372
case Intrinsic::sadd_sat:
73767373
if (match(II.getOperand(0), m_APInt(C)) ||
73777374
match(II.getOperand(1), m_APInt(C))) {
7378-
if (C->isNegative()) {
7375+
if (C->isNegative())
73797376
// sadd.sat(x, -C) produces [SINT_MIN, SINT_MAX + (-C)].
7380-
Lower = APInt::getSignedMinValue(Width);
7381-
Upper = APInt::getSignedMaxValue(Width) + *C + 1;
7382-
} else {
7383-
// sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
7384-
Lower = APInt::getSignedMinValue(Width) + *C;
7385-
Upper = APInt::getSignedMaxValue(Width) + 1;
7386-
}
7377+
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
7378+
APInt::getSignedMaxValue(Width) + *C +
7379+
1);
7380+
7381+
// sadd.sat(x, +C) produces [SINT_MIN + C, SINT_MAX].
7382+
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) + *C,
7383+
APInt::getSignedMaxValue(Width) + 1);
73877384
}
73887385
break;
73897386
case Intrinsic::usub_sat:
73907387
// usub.sat(C, x) produces [0, C].
73917388
if (match(II.getOperand(0), m_APInt(C)))
7392-
Upper = *C + 1;
7389+
return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
7390+
73937391
// usub.sat(x, C) produces [0, UINT_MAX - C].
7394-
else if (match(II.getOperand(1), m_APInt(C)))
7395-
Upper = APInt::getMaxValue(Width) - *C + 1;
7392+
if (match(II.getOperand(1), m_APInt(C)))
7393+
return ConstantRange::getNonEmpty(APInt::getZero(Width),
7394+
APInt::getMaxValue(Width) - *C + 1);
73967395
break;
73977396
case Intrinsic::ssub_sat:
73987397
if (match(II.getOperand(0), m_APInt(C))) {
7399-
if (C->isNegative()) {
7398+
if (C->isNegative())
74007399
// ssub.sat(-C, x) produces [SINT_MIN, -SINT_MIN + (-C)].
7401-
Lower = APInt::getSignedMinValue(Width);
7402-
Upper = *C - APInt::getSignedMinValue(Width) + 1;
7403-
} else {
7404-
// ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
7405-
Lower = *C - APInt::getSignedMaxValue(Width);
7406-
Upper = APInt::getSignedMaxValue(Width) + 1;
7407-
}
7400+
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
7401+
*C - APInt::getSignedMinValue(Width) +
7402+
1);
7403+
7404+
// ssub.sat(+C, x) produces [-SINT_MAX + C, SINT_MAX].
7405+
return ConstantRange::getNonEmpty(*C - APInt::getSignedMaxValue(Width),
7406+
APInt::getSignedMaxValue(Width) + 1);
74087407
} else if (match(II.getOperand(1), m_APInt(C))) {
7409-
if (C->isNegative()) {
7408+
if (C->isNegative())
74107409
// ssub.sat(x, -C) produces [SINT_MIN - (-C), SINT_MAX]:
7411-
Lower = APInt::getSignedMinValue(Width) - *C;
7412-
Upper = APInt::getSignedMaxValue(Width) + 1;
7413-
} else {
7414-
// ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
7415-
Lower = APInt::getSignedMinValue(Width);
7416-
Upper = APInt::getSignedMaxValue(Width) - *C + 1;
7417-
}
7410+
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width) - *C,
7411+
APInt::getSignedMaxValue(Width) + 1);
7412+
7413+
// ssub.sat(x, +C) produces [SINT_MIN, SINT_MAX - C].
7414+
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
7415+
APInt::getSignedMaxValue(Width) - *C +
7416+
1);
74187417
}
74197418
break;
74207419
case Intrinsic::umin:
@@ -7427,19 +7426,15 @@ static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
74277426

74287427
switch (II.getIntrinsicID()) {
74297428
case Intrinsic::umin:
7430-
Upper = *C + 1;
7431-
break;
7429+
return ConstantRange::getNonEmpty(APInt::getZero(Width), *C + 1);
74327430
case Intrinsic::umax:
7433-
Lower = *C;
7434-
break;
7431+
return ConstantRange::getNonEmpty(*C, APInt::getZero(Width));
74357432
case Intrinsic::smin:
7436-
Lower = APInt::getSignedMinValue(Width);
7437-
Upper = *C + 1;
7438-
break;
7433+
return ConstantRange::getNonEmpty(APInt::getSignedMinValue(Width),
7434+
*C + 1);
74397435
case Intrinsic::smax:
7440-
Lower = *C;
7441-
Upper = APInt::getSignedMaxValue(Width) + 1;
7442-
break;
7436+
return ConstantRange::getNonEmpty(*C,
7437+
APInt::getSignedMaxValue(Width) + 1);
74437438
default:
74447439
llvm_unreachable("Must be min/max intrinsic");
74457440
}
@@ -7448,13 +7443,16 @@ static void setLimitsForIntrinsic(const IntrinsicInst &II, APInt &Lower,
74487443
// If abs of SIGNED_MIN is poison, then the result is [0..SIGNED_MAX],
74497444
// otherwise it is [0..SIGNED_MIN], as -SIGNED_MIN == SIGNED_MIN.
74507445
if (match(II.getOperand(1), m_One()))
7451-
Upper = APInt::getSignedMaxValue(Width) + 1;
7452-
else
7453-
Upper = APInt::getSignedMinValue(Width) + 1;
7454-
break;
7446+
return ConstantRange(APInt::getZero(Width),
7447+
APInt::getSignedMaxValue(Width) + 1);
7448+
7449+
return ConstantRange(APInt::getZero(Width),
7450+
APInt::getSignedMinValue(Width) + 1);
74557451
default:
74567452
break;
74577453
}
7454+
7455+
return ConstantRange::getFull(Width);
74587456
}
74597457

74607458
static void setLimitsForSelectPattern(const SelectInst &SI, APInt &Lower,
@@ -7543,18 +7541,28 @@ ConstantRange llvm::computeConstantRange(const Value *V, bool ForSigned,
75437541

75447542
InstrInfoQuery IIQ(UseInstrInfo);
75457543
unsigned BitWidth = V->getType()->getScalarSizeInBits();
7546-
APInt Lower = APInt(BitWidth, 0);
7547-
APInt Upper = APInt(BitWidth, 0);
7548-
if (auto *BO = dyn_cast<BinaryOperator>(V))
7544+
ConstantRange CR = ConstantRange::getFull(BitWidth);
7545+
if (auto *BO = dyn_cast<BinaryOperator>(V)) {
7546+
APInt Lower = APInt(BitWidth, 0);
7547+
APInt Upper = APInt(BitWidth, 0);
7548+
// TODO: Return ConstantRange.
75497549
setLimitsForBinOp(*BO, Lower, Upper, IIQ, ForSigned);
7550-
else if (auto *II = dyn_cast<IntrinsicInst>(V))
7551-
setLimitsForIntrinsic(*II, Lower, Upper);
7552-
else if (auto *SI = dyn_cast<SelectInst>(V))
7550+
CR = ConstantRange::getNonEmpty(Lower, Upper);
7551+
} else if (auto *II = dyn_cast<IntrinsicInst>(V))
7552+
CR = getRangeForIntrinsic(*II);
7553+
else if (auto *SI = dyn_cast<SelectInst>(V)) {
7554+
APInt Lower = APInt(BitWidth, 0);
7555+
APInt Upper = APInt(BitWidth, 0);
7556+
// TODO: Return ConstantRange.
75537557
setLimitsForSelectPattern(*SI, Lower, Upper, IIQ);
7554-
else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V))
7558+
CR = ConstantRange::getNonEmpty(Lower, Upper);
7559+
} else if (isa<FPToUIInst>(V) || isa<FPToSIInst>(V)) {
7560+
APInt Lower = APInt(BitWidth, 0);
7561+
APInt Upper = APInt(BitWidth, 0);
7562+
// TODO: Return ConstantRange.
75557563
setLimitForFPToI(cast<Instruction>(V), Lower, Upper);
7556-
7557-
ConstantRange CR = ConstantRange::getNonEmpty(Lower, Upper);
7564+
CR = ConstantRange::getNonEmpty(Lower, Upper);
7565+
}
75587566

75597567
if (auto *I = dyn_cast<Instruction>(V))
75607568
if (auto *Range = IIQ.getMetadata(I, LLVMContext::MD_range))

0 commit comments

Comments
 (0)