Skip to content

Commit 4275da2

Browse files
committed
[ValueTracking] Add isGuaranteedNotToBeUndef() variant (NFC)
We have a bunch of places where we have to guard against undef to avoid multi-use issues, but would be fine with poison. Use a different function for these to make it clear, and to indicate that this check can be removed once we no longer support undef. I've replaced some of the obvious cases, but there's probably more. For now, the implementation is the same as UndefOrPoison, it just has a more precise name.
1 parent 5c672d8 commit 4275da2

File tree

4 files changed

+21
-7
lines changed

4 files changed

+21
-7
lines changed

llvm/include/llvm/Analysis/ValueTracking.h

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1002,11 +1002,19 @@ bool isGuaranteedNotToBeUndefOrPoison(const Value *V,
10021002
const Instruction *CtxI = nullptr,
10031003
const DominatorTree *DT = nullptr,
10041004
unsigned Depth = 0);
1005+
1006+
/// Returns true if V cannot be poison, but may be undef.
10051007
bool isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC = nullptr,
10061008
const Instruction *CtxI = nullptr,
10071009
const DominatorTree *DT = nullptr,
10081010
unsigned Depth = 0);
10091011

1012+
/// Returns true if V cannot be undef, but may be poison.
1013+
bool isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC = nullptr,
1014+
const Instruction *CtxI = nullptr,
1015+
const DominatorTree *DT = nullptr,
1016+
unsigned Depth = 0);
1017+
10101018
/// Return true if undefined behavior would provable be executed on the path to
10111019
/// OnPathTo if Root produced a posion result. Note that this doesn't say
10121020
/// anything about whether OnPathTo is actually executed or whether Root is

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -880,7 +880,7 @@ LazyValueInfoImpl::solveBlockValueSelect(SelectInst *SI, BasicBlock *BB) {
880880
Value *Cond = SI->getCondition();
881881
// If the value is undef, a different value may be chosen in
882882
// the select condition.
883-
if (isGuaranteedNotToBeUndefOrPoison(Cond, AC)) {
883+
if (isGuaranteedNotToBeUndef(Cond, AC)) {
884884
TrueVal = intersect(TrueVal,
885885
getValueFromCondition(SI->getTrueValue(), Cond, true));
886886
FalseVal = intersect(
@@ -1719,7 +1719,7 @@ ConstantRange LazyValueInfo::getConstantRangeAtUse(const Use &U,
17191719
if (auto *SI = dyn_cast<SelectInst>(CurrI)) {
17201720
// If the value is undef, a different value may be chosen in
17211721
// the select condition and at use.
1722-
if (!isGuaranteedNotToBeUndefOrPoison(SI->getCondition(), AC))
1722+
if (!isGuaranteedNotToBeUndef(SI->getCondition(), AC))
17231723
break;
17241724
if (CurrU->getOperandNo() == 1)
17251725
CondVal = getValueFromCondition(V, SI->getCondition(), true);

llvm/lib/Analysis/ValueTracking.cpp

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -388,10 +388,9 @@ static void computeKnownBitsMul(const Value *Op0, const Value *Op1, bool NSW,
388388
}
389389

390390
bool SelfMultiply = Op0 == Op1;
391-
// TODO: SelfMultiply can be poison, but not undef.
392391
if (SelfMultiply)
393392
SelfMultiply &=
394-
isGuaranteedNotToBeUndefOrPoison(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
393+
isGuaranteedNotToBeUndef(Op0, Q.AC, Q.CxtI, Q.DT, Depth + 1);
395394
Known = KnownBits::mul(Known, Known2, SelfMultiply);
396395

397396
// Only make use of no-wrap flags if we failed to compute the sign bit
@@ -6488,7 +6487,7 @@ OverflowResult llvm::computeOverflowForUnsignedSub(const Value *LHS,
64886487
// See simplifyICmpWithBinOpOnLHS() for candidates.
64896488
if (match(RHS, m_URem(m_Specific(LHS), m_Value())) ||
64906489
match(RHS, m_NUWSub(m_Specific(LHS), m_Value())))
6491-
if (isGuaranteedNotToBeUndefOrPoison(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6490+
if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
64926491
return OverflowResult::NeverOverflows;
64936492

64946493
// Checking for conditions implied by dominating conditions may be expensive.
@@ -6521,7 +6520,7 @@ OverflowResult llvm::computeOverflowForSignedSub(const Value *LHS,
65216520
// then determining no-overflow may allow other transforms.
65226521
if (match(RHS, m_SRem(m_Specific(LHS), m_Value())) ||
65236522
match(RHS, m_NSWSub(m_Specific(LHS), m_Value())))
6524-
if (isGuaranteedNotToBeUndefOrPoison(LHS, SQ.AC, SQ.CxtI, SQ.DT))
6523+
if (isGuaranteedNotToBeUndef(LHS, SQ.AC, SQ.CxtI, SQ.DT))
65256524
return OverflowResult::NeverOverflows;
65266525

65276526
// If LHS and RHS each have at least two sign bits, the subtraction
@@ -6982,6 +6981,13 @@ bool llvm::isGuaranteedNotToBePoison(const Value *V, AssumptionCache *AC,
69826981
return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, true);
69836982
}
69846983

6984+
bool llvm::isGuaranteedNotToBeUndef(const Value *V, AssumptionCache *AC,
6985+
const Instruction *CtxI,
6986+
const DominatorTree *DT, unsigned Depth) {
6987+
// TODO: This is currently equivalent to isGuaranteedNotToBeUndefOrPoison().
6988+
return ::isGuaranteedNotToBeUndefOrPoison(V, AC, CtxI, DT, Depth, false);
6989+
}
6990+
69856991
/// Return true if undefined behavior would provably be executed on the path to
69866992
/// OnPathTo if Root produced a posion result. Note that this doesn't say
69876993
/// anything about whether OnPathTo is actually executed or whether Root is

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -757,7 +757,7 @@ static bool expandUDivOrURem(BinaryOperator *Instr, const ConstantRange &XCR,
757757
// NOTE: this transformation introduces two uses of X,
758758
// but it may be undef so we must freeze it first.
759759
Value *FrozenX = X;
760-
if (!isGuaranteedNotToBeUndefOrPoison(X))
760+
if (!isGuaranteedNotToBeUndef(X))
761761
FrozenX = B.CreateFreeze(X, X->getName() + ".frozen");
762762
auto *AdjX = B.CreateNUWSub(FrozenX, Y, Instr->getName() + ".urem");
763763
auto *Cmp =

0 commit comments

Comments
 (0)