Skip to content

Commit 79d6f52

Browse files
committed
[LVI] Use Constant instead of Tristate for predicate results
A lot of the users just end up converting it into a Constant themselves. Doing this in the API leaves less room for error with vector types, and brings getPredicateResult() closer to LatticeValueElement::getCompare(), hopefully allowing us to consolidate them.
1 parent 8299bfa commit 79d6f52

File tree

4 files changed

+90
-125
lines changed

4 files changed

+90
-125
lines changed

llvm/include/llvm/Analysis/LazyValueInfo.h

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -60,32 +60,29 @@ namespace llvm {
6060
return *this;
6161
}
6262

63-
/// This is used to return true/false/dunno results.
64-
enum Tristate { Unknown = -1, False = 0, True = 1 };
65-
6663
// Public query interface.
6764

6865
/// Determine whether the specified value comparison with a constant is
6966
/// known to be true or false on the specified CFG edge. Pred is a CmpInst
7067
/// predicate.
71-
Tristate getPredicateOnEdge(CmpInst::Predicate Pred, Value *V, Constant *C,
72-
BasicBlock *FromBB, BasicBlock *ToBB,
73-
Instruction *CxtI = nullptr);
68+
Constant *getPredicateOnEdge(CmpInst::Predicate Pred, Value *V, Constant *C,
69+
BasicBlock *FromBB, BasicBlock *ToBB,
70+
Instruction *CxtI = nullptr);
7471

7572
/// Determine whether the specified value comparison with a constant is
7673
/// known to be true or false at the specified instruction. \p Pred is a
7774
/// CmpInst predicate. If \p UseBlockValue is true, the block value is also
7875
/// taken into account.
79-
Tristate getPredicateAt(CmpInst::Predicate Pred, Value *V, Constant *C,
80-
Instruction *CxtI, bool UseBlockValue);
76+
Constant *getPredicateAt(CmpInst::Predicate Pred, Value *V, Constant *C,
77+
Instruction *CxtI, bool UseBlockValue);
8178

8279
/// Determine whether the specified value comparison is known to be true
8380
/// or false at the specified instruction. While this takes two Value's,
8481
/// it still requires that one of them is a constant.
8582
/// \p Pred is a CmpInst predicate.
8683
/// If \p UseBlockValue is true, the block value is also taken into account.
87-
Tristate getPredicateAt(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
88-
Instruction *CxtI, bool UseBlockValue);
84+
Constant *getPredicateAt(CmpInst::Predicate Pred, Value *LHS, Value *RHS,
85+
Instruction *CxtI, bool UseBlockValue);
8986

9087
/// Determine whether the specified value is known to be a constant at the
9188
/// specified instruction. Return null if not.

llvm/lib/Analysis/LazyValueInfo.cpp

Lines changed: 47 additions & 56 deletions
Original file line numberDiff line numberDiff line change
@@ -1775,70 +1775,66 @@ ConstantRange LazyValueInfo::getConstantRangeOnEdge(Value *V,
17751775
return toConstantRange(Result, V->getType(), /*UndefAllowed*/ true);
17761776
}
17771777

1778-
static LazyValueInfo::Tristate
1779-
getPredicateResult(CmpInst::Predicate Pred, Constant *C,
1780-
const ValueLatticeElement &Val, const DataLayout &DL) {
1778+
static Constant *getPredicateResult(CmpInst::Predicate Pred, Constant *C,
1779+
const ValueLatticeElement &Val,
1780+
const DataLayout &DL) {
17811781
// If we know the value is a constant, evaluate the conditional.
1782-
Constant *Res = nullptr;
1783-
if (Val.isConstant()) {
1784-
Res = ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL);
1785-
if (ConstantInt *ResCI = dyn_cast_or_null<ConstantInt>(Res))
1786-
return ResCI->isZero() ? LazyValueInfo::False : LazyValueInfo::True;
1787-
return LazyValueInfo::Unknown;
1788-
}
1782+
if (Val.isConstant())
1783+
return ConstantFoldCompareInstOperands(Pred, Val.getConstant(), C, DL);
17891784

1785+
Type *ResTy = CmpInst::makeCmpResultType(C->getType());
17901786
if (Val.isConstantRange()) {
17911787
ConstantInt *CI = dyn_cast<ConstantInt>(C);
1792-
if (!CI) return LazyValueInfo::Unknown;
1788+
if (!CI)
1789+
return nullptr;
17931790

17941791
const ConstantRange &CR = Val.getConstantRange();
17951792
ConstantRange RHS(CI->getValue());
17961793
if (CR.icmp(Pred, RHS))
1797-
return LazyValueInfo::True;
1794+
return ConstantInt::getTrue(ResTy);
17981795
if (CR.icmp(CmpInst::getInversePredicate(Pred), RHS))
1799-
return LazyValueInfo::False;
1800-
return LazyValueInfo::Unknown;
1796+
return ConstantInt::getFalse(ResTy);
1797+
return nullptr;
18011798
}
18021799

18031800
if (Val.isNotConstant()) {
18041801
// If this is an equality comparison, we can try to fold it knowing that
18051802
// "V != C1".
18061803
if (Pred == ICmpInst::ICMP_EQ) {
18071804
// !C1 == C -> false iff C1 == C.
1808-
Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1809-
Val.getNotConstant(), C, DL);
1805+
Constant *Res = ConstantFoldCompareInstOperands(
1806+
ICmpInst::ICMP_NE, Val.getNotConstant(), C, DL);
18101807
if (Res && Res->isNullValue())
1811-
return LazyValueInfo::False;
1808+
return ConstantInt::getFalse(ResTy);
18121809
} else if (Pred == ICmpInst::ICMP_NE) {
18131810
// !C1 != C -> true iff C1 == C.
1814-
Res = ConstantFoldCompareInstOperands(ICmpInst::ICMP_NE,
1815-
Val.getNotConstant(), C, DL);
1811+
Constant *Res = ConstantFoldCompareInstOperands(
1812+
ICmpInst::ICMP_NE, Val.getNotConstant(), C, DL);
18161813
if (Res && Res->isNullValue())
1817-
return LazyValueInfo::True;
1814+
return ConstantInt::getTrue(ResTy);
18181815
}
1819-
return LazyValueInfo::Unknown;
1816+
return nullptr;
18201817
}
18211818

1822-
return LazyValueInfo::Unknown;
1819+
return nullptr;
18231820
}
18241821

18251822
/// Determine whether the specified value comparison with a constant is known to
18261823
/// be true or false on the specified CFG edge. Pred is a CmpInst predicate.
1827-
LazyValueInfo::Tristate
1828-
LazyValueInfo::getPredicateOnEdge(CmpInst::Predicate Pred, Value *V,
1829-
Constant *C, BasicBlock *FromBB,
1830-
BasicBlock *ToBB, Instruction *CxtI) {
1824+
Constant *LazyValueInfo::getPredicateOnEdge(CmpInst::Predicate Pred, Value *V,
1825+
Constant *C, BasicBlock *FromBB,
1826+
BasicBlock *ToBB,
1827+
Instruction *CxtI) {
18311828
Module *M = FromBB->getModule();
18321829
ValueLatticeElement Result =
18331830
getOrCreateImpl(M).getValueOnEdge(V, FromBB, ToBB, CxtI);
18341831

18351832
return getPredicateResult(Pred, C, Result, M->getDataLayout());
18361833
}
18371834

1838-
LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred,
1839-
Value *V, Constant *C,
1840-
Instruction *CxtI,
1841-
bool UseBlockValue) {
1835+
Constant *LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred, Value *V,
1836+
Constant *C, Instruction *CxtI,
1837+
bool UseBlockValue) {
18421838
// Is or is not NonNull are common predicates being queried. If
18431839
// isKnownNonZero can tell us the result of the predicate, we can
18441840
// return it quickly. But this is only a fastpath, and falling
@@ -1847,18 +1843,19 @@ LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred,
18471843
const DataLayout &DL = M->getDataLayout();
18481844
if (V->getType()->isPointerTy() && C->isNullValue() &&
18491845
isKnownNonZero(V->stripPointerCastsSameRepresentation(), DL)) {
1846+
Type *ResTy = CmpInst::makeCmpResultType(C->getType());
18501847
if (Pred == ICmpInst::ICMP_EQ)
1851-
return LazyValueInfo::False;
1848+
return ConstantInt::getFalse(ResTy);
18521849
else if (Pred == ICmpInst::ICMP_NE)
1853-
return LazyValueInfo::True;
1850+
return ConstantInt::getTrue(ResTy);
18541851
}
18551852

18561853
auto &Impl = getOrCreateImpl(M);
18571854
ValueLatticeElement Result =
18581855
UseBlockValue ? Impl.getValueInBlock(V, CxtI->getParent(), CxtI)
18591856
: Impl.getValueAt(V, CxtI);
1860-
Tristate Ret = getPredicateResult(Pred, C, Result, DL);
1861-
if (Ret != Unknown)
1857+
Constant *Ret = getPredicateResult(Pred, C, Result, DL);
1858+
if (Ret)
18621859
return Ret;
18631860

18641861
// Note: The following bit of code is somewhat distinct from the rest of LVI;
@@ -1889,31 +1886,31 @@ LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred,
18891886
// analysis below.
18901887
pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
18911888
if (PI == PE)
1892-
return Unknown;
1889+
return nullptr;
18931890

18941891
// If V is a PHI node in the same block as the context, we need to ask
18951892
// questions about the predicate as applied to the incoming value along
18961893
// each edge. This is useful for eliminating cases where the predicate is
18971894
// known along all incoming edges.
18981895
if (auto *PHI = dyn_cast<PHINode>(V))
18991896
if (PHI->getParent() == BB) {
1900-
Tristate Baseline = Unknown;
1897+
Constant *Baseline = nullptr;
19011898
for (unsigned i = 0, e = PHI->getNumIncomingValues(); i < e; i++) {
19021899
Value *Incoming = PHI->getIncomingValue(i);
19031900
BasicBlock *PredBB = PHI->getIncomingBlock(i);
19041901
// Note that PredBB may be BB itself.
1905-
Tristate Result =
1902+
Constant *Result =
19061903
getPredicateOnEdge(Pred, Incoming, C, PredBB, BB, CxtI);
19071904

19081905
// Keep going as long as we've seen a consistent known result for
19091906
// all inputs.
19101907
Baseline = (i == 0) ? Result /* First iteration */
19111908
: (Baseline == Result ? Baseline
1912-
: Unknown); /* All others */
1913-
if (Baseline == Unknown)
1909+
: nullptr); /* All others */
1910+
if (!Baseline)
19141911
break;
19151912
}
1916-
if (Baseline != Unknown)
1913+
if (Baseline)
19171914
return Baseline;
19181915
}
19191916

@@ -1924,11 +1921,11 @@ LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred,
19241921
// For predecessor edge, determine if the comparison is true or false
19251922
// on that edge. If they're all true or all false, we can conclude
19261923
// the value of the comparison in this block.
1927-
Tristate Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1928-
if (Baseline != Unknown) {
1924+
Constant *Baseline = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1925+
if (Baseline) {
19291926
// Check that all remaining incoming values match the first one.
19301927
while (++PI != PE) {
1931-
Tristate Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
1928+
Constant *Ret = getPredicateOnEdge(Pred, V, C, *PI, BB, CxtI);
19321929
if (Ret != Baseline)
19331930
break;
19341931
}
@@ -1939,13 +1936,12 @@ LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred,
19391936
}
19401937
}
19411938

1942-
return Unknown;
1939+
return nullptr;
19431940
}
19441941

1945-
LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred,
1946-
Value *LHS, Value *RHS,
1947-
Instruction *CxtI,
1948-
bool UseBlockValue) {
1942+
Constant *LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred, Value *LHS,
1943+
Value *RHS, Instruction *CxtI,
1944+
bool UseBlockValue) {
19491945
if (auto *C = dyn_cast<Constant>(RHS))
19501946
return getPredicateAt(Pred, LHS, C, CxtI, UseBlockValue);
19511947
if (auto *C = dyn_cast<Constant>(LHS))
@@ -1960,19 +1956,14 @@ LazyValueInfo::Tristate LazyValueInfo::getPredicateAt(CmpInst::Predicate Pred,
19601956
ValueLatticeElement L =
19611957
getOrCreateImpl(M).getValueInBlock(LHS, CxtI->getParent(), CxtI);
19621958
if (L.isOverdefined())
1963-
return LazyValueInfo::Unknown;
1959+
return nullptr;
19641960

19651961
ValueLatticeElement R =
19661962
getOrCreateImpl(M).getValueInBlock(RHS, CxtI->getParent(), CxtI);
19671963
Type *Ty = CmpInst::makeCmpResultType(LHS->getType());
1968-
if (Constant *Res = L.getCompare(Pred, Ty, R, M->getDataLayout())) {
1969-
if (Res->isNullValue())
1970-
return LazyValueInfo::False;
1971-
if (Res->isOneValue())
1972-
return LazyValueInfo::True;
1973-
}
1964+
return L.getCompare(Pred, Ty, R, M->getDataLayout());
19741965
}
1975-
return LazyValueInfo::Unknown;
1966+
return nullptr;
19761967
}
19771968

19781969
void LazyValueInfo::threadEdge(BasicBlock *PredBB, BasicBlock *OldSucc,

llvm/lib/Transforms/Scalar/CorrelatedValuePropagation.cpp

Lines changed: 22 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -109,14 +109,8 @@ static Constant *getConstantAt(Value *V, Instruction *At, LazyValueInfo *LVI) {
109109
if (!Op1)
110110
return nullptr;
111111

112-
LazyValueInfo::Tristate Result = LVI->getPredicateAt(
113-
C->getPredicate(), Op0, Op1, At, /*UseBlockValue=*/false);
114-
if (Result == LazyValueInfo::Unknown)
115-
return nullptr;
116-
117-
return (Result == LazyValueInfo::True)
118-
? ConstantInt::getTrue(C->getContext())
119-
: ConstantInt::getFalse(C->getContext());
112+
return LVI->getPredicateAt(C->getPredicate(), Op0, Op1, At,
113+
/*UseBlockValue=*/false);
120114
}
121115

122116
static bool processSelect(SelectInst *S, LazyValueInfo *LVI) {
@@ -243,15 +237,17 @@ static Value *getValueOnEdge(LazyValueInfo *LVI, Value *Incoming,
243237

244238
// The "false" case
245239
if (auto *C = dyn_cast<Constant>(SI->getFalseValue()))
246-
if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
247-
LazyValueInfo::False)
240+
if (auto *Res = dyn_cast_or_null<ConstantInt>(
241+
LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI));
242+
Res && Res->isZero())
248243
return SI->getTrueValue();
249244

250245
// The "true" case,
251246
// similar to the select "false" case, but try the select "true" value
252247
if (auto *C = dyn_cast<Constant>(SI->getTrueValue()))
253-
if (LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI) ==
254-
LazyValueInfo::False)
248+
if (auto *Res = dyn_cast_or_null<ConstantInt>(
249+
LVI->getPredicateOnEdge(ICmpInst::ICMP_EQ, SI, C, From, To, CxtI));
250+
Res && Res->isZero())
255251
return SI->getFalseValue();
256252

257253
return nullptr;
@@ -320,16 +316,13 @@ static bool processICmp(ICmpInst *Cmp, LazyValueInfo *LVI) {
320316
static bool constantFoldCmp(CmpInst *Cmp, LazyValueInfo *LVI) {
321317
Value *Op0 = Cmp->getOperand(0);
322318
Value *Op1 = Cmp->getOperand(1);
323-
LazyValueInfo::Tristate Result =
324-
LVI->getPredicateAt(Cmp->getPredicate(), Op0, Op1, Cmp,
325-
/*UseBlockValue=*/true);
326-
if (Result == LazyValueInfo::Unknown)
319+
Constant *Res = LVI->getPredicateAt(Cmp->getPredicate(), Op0, Op1, Cmp,
320+
/*UseBlockValue=*/true);
321+
if (!Res)
327322
return false;
328323

329324
++NumCmps;
330-
Constant *TorF =
331-
ConstantInt::get(CmpInst::makeCmpResultType(Op0->getType()), Result);
332-
Cmp->replaceAllUsesWith(TorF);
325+
Cmp->replaceAllUsesWith(Res);
333326
Cmp->eraseFromParent();
334327
return true;
335328
}
@@ -371,11 +364,11 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
371364

372365
for (auto CI = SI->case_begin(), CE = SI->case_end(); CI != CE;) {
373366
ConstantInt *Case = CI->getCaseValue();
374-
LazyValueInfo::Tristate State =
367+
auto *Res = dyn_cast_or_null<ConstantInt>(
375368
LVI->getPredicateAt(CmpInst::ICMP_EQ, Cond, Case, I,
376-
/* UseBlockValue */ true);
369+
/* UseBlockValue */ true));
377370

378-
if (State == LazyValueInfo::False) {
371+
if (Res && Res->isZero()) {
379372
// This case never fires - remove it.
380373
BasicBlock *Succ = CI->getCaseSuccessor();
381374
Succ->removePredecessor(BB);
@@ -392,7 +385,7 @@ static bool processSwitch(SwitchInst *I, LazyValueInfo *LVI,
392385
DTU.applyUpdatesPermissive({{DominatorTree::Delete, BB, Succ}});
393386
continue;
394387
}
395-
if (State == LazyValueInfo::True) {
388+
if (Res && Res->isOne()) {
396389
// This case always fires. Arrange for the switch to be turned into an
397390
// unconditional branch by replacing the switch condition with the case
398391
// value.
@@ -716,11 +709,12 @@ static bool processCallSite(CallBase &CB, LazyValueInfo *LVI) {
716709
// relatively expensive analysis for constants which are obviously either
717710
// null or non-null to start with.
718711
if (Type && !CB.paramHasAttr(ArgNo, Attribute::NonNull) &&
719-
!isa<Constant>(V) &&
720-
LVI->getPredicateAt(ICmpInst::ICMP_EQ, V,
721-
ConstantPointerNull::get(Type), &CB,
722-
/*UseBlockValue=*/false) == LazyValueInfo::False)
723-
ArgNos.push_back(ArgNo);
712+
!isa<Constant>(V))
713+
if (auto *Res = dyn_cast_or_null<ConstantInt>(LVI->getPredicateAt(
714+
ICmpInst::ICMP_EQ, V, ConstantPointerNull::get(Type), &CB,
715+
/*UseBlockValue=*/false));
716+
Res && Res->isZero())
717+
ArgNos.push_back(ArgNo);
724718
ArgNo++;
725719
}
726720

0 commit comments

Comments
 (0)