Skip to content

Commit 103fc0f

Browse files
committed
[SCEV] Replace IsAvailableOnEntry with block disposition
As far as I understand, the IsAvailableOnEntry() function basically implements the same functionality as the properlyDominates() block disposition. The primary difference (apart from a weaker implementation) seems to be in this comment at the top: // Checks if the SCEV S is available at BB. S is considered available at BB // if S can be materialized at BB without introducing a fault. However, I don't really understand why there would be such a requirement. It's my understanding that SCEV explicitly does not care about trapping udiv instructions itself, and it's the job of SCEVExpander's isSafeToExpand() to make sure these don't get expanded if they may trap. Differential Revision: https://reviews.llvm.org/D149344
1 parent d961f66 commit 103fc0f

File tree

2 files changed

+3
-89
lines changed

2 files changed

+3
-89
lines changed

llvm/lib/Analysis/ScalarEvolution.cpp

Lines changed: 2 additions & 88 deletions
Original file line numberDiff line numberDiff line change
@@ -5890,90 +5890,6 @@ const SCEV *ScalarEvolution::createAddRecFromPHI(PHINode *PN) {
58905890
return nullptr;
58915891
}
58925892

5893-
// Checks if the SCEV S is available at BB. S is considered available at BB
5894-
// if S can be materialized at BB without introducing a fault.
5895-
static bool IsAvailableOnEntry(const Loop *L, DominatorTree &DT, const SCEV *S,
5896-
BasicBlock *BB) {
5897-
struct CheckAvailable {
5898-
bool TraversalDone = false;
5899-
bool Available = true;
5900-
5901-
const Loop *L = nullptr; // The loop BB is in (can be nullptr)
5902-
BasicBlock *BB = nullptr;
5903-
DominatorTree &DT;
5904-
5905-
CheckAvailable(const Loop *L, BasicBlock *BB, DominatorTree &DT)
5906-
: L(L), BB(BB), DT(DT) {}
5907-
5908-
bool setUnavailable() {
5909-
TraversalDone = true;
5910-
Available = false;
5911-
return false;
5912-
}
5913-
5914-
bool follow(const SCEV *S) {
5915-
switch (S->getSCEVType()) {
5916-
case scConstant:
5917-
case scVScale:
5918-
case scPtrToInt:
5919-
case scTruncate:
5920-
case scZeroExtend:
5921-
case scSignExtend:
5922-
case scAddExpr:
5923-
case scMulExpr:
5924-
case scUMaxExpr:
5925-
case scSMaxExpr:
5926-
case scUMinExpr:
5927-
case scSMinExpr:
5928-
case scSequentialUMinExpr:
5929-
// These expressions are available if their operand(s) is/are.
5930-
return true;
5931-
5932-
case scAddRecExpr: {
5933-
// We allow add recurrences that are on the loop BB is in, or some
5934-
// outer loop. This guarantees availability because the value of the
5935-
// add recurrence at BB is simply the "current" value of the induction
5936-
// variable. We can relax this in the future; for instance an add
5937-
// recurrence on a sibling dominating loop is also available at BB.
5938-
const auto *ARLoop = cast<SCEVAddRecExpr>(S)->getLoop();
5939-
if (L && (ARLoop == L || ARLoop->contains(L)))
5940-
return true;
5941-
5942-
return setUnavailable();
5943-
}
5944-
5945-
case scUnknown: {
5946-
// For SCEVUnknown, we check for simple dominance.
5947-
const auto *SU = cast<SCEVUnknown>(S);
5948-
Value *V = SU->getValue();
5949-
5950-
if (isa<Argument>(V))
5951-
return false;
5952-
5953-
if (isa<Instruction>(V) && DT.dominates(cast<Instruction>(V), BB))
5954-
return false;
5955-
5956-
return setUnavailable();
5957-
}
5958-
5959-
case scUDivExpr:
5960-
case scCouldNotCompute:
5961-
// We do not try to smart about these at all.
5962-
return setUnavailable();
5963-
}
5964-
llvm_unreachable("Unknown SCEV kind!");
5965-
}
5966-
5967-
bool isDone() { return TraversalDone; }
5968-
};
5969-
5970-
CheckAvailable CA(L, BB, DT);
5971-
SCEVTraversal<CheckAvailable> ST(CA);
5972-
5973-
ST.visitAll(S);
5974-
return CA.Available;
5975-
}
5976-
59775893
// Try to match a control flow sequence that branches out at BI and merges back
59785894
// at Merge into a "C ? LHS : RHS" select pattern. Return true on a successful
59795895
// match.
@@ -6011,8 +5927,6 @@ const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
60115927
auto IsReachable =
60125928
[&](BasicBlock *BB) { return DT.isReachableFromEntry(BB); };
60135929
if (PN->getNumIncomingValues() == 2 && all_of(PN->blocks(), IsReachable)) {
6014-
const Loop *L = LI.getLoopFor(PN->getParent());
6015-
60165930
// Try to match
60175931
//
60185932
// br %cond, label %left, label %right
@@ -6033,8 +5947,8 @@ const SCEV *ScalarEvolution::createNodeFromSelectLikePHI(PHINode *PN) {
60335947

60345948
if (BI && BI->isConditional() &&
60355949
BrPHIToSelect(DT, BI, PN, Cond, LHS, RHS) &&
6036-
IsAvailableOnEntry(L, DT, getSCEV(LHS), PN->getParent()) &&
6037-
IsAvailableOnEntry(L, DT, getSCEV(RHS), PN->getParent()))
5950+
properlyDominates(getSCEV(LHS), PN->getParent()) &&
5951+
properlyDominates(getSCEV(RHS), PN->getParent()))
60385952
return createNodeForSelectOrPHI(PN, Cond, LHS, RHS);
60395953
}
60405954

llvm/test/Analysis/ScalarEvolution/logical-operations.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -427,7 +427,7 @@ define ptr @tautological_select_like_phi(i32 %tc) {
427427
; CHECK-NEXT: %iv = phi i32 [ 0, %entry ], [ %iv.next, %latch ]
428428
; CHECK-NEXT: --> {0,+,1}<nuw><nsw><%loop> U: [0,101) S: [0,101) Exits: 100 LoopDispositions: { %loop: Computable }
429429
; CHECK-NEXT: %r = phi ptr [ @constant, %truebb ], [ @another_constant, %falsebb ]
430-
; CHECK-NEXT: --> %r U: [0,-3) S: [-9223372036854775808,9223372036854775805) Exits: <<Unknown>> LoopDispositions: { %loop: Variant }
430+
; CHECK-NEXT: --> @constant U: [0,-3) S: [-9223372036854775808,9223372036854775805) Exits: @constant LoopDispositions: { %loop: Invariant }
431431
; CHECK-NEXT: %iv.next = add i32 %iv, 1
432432
; CHECK-NEXT: --> {1,+,1}<nuw><nsw><%loop> U: [1,102) S: [1,102) Exits: 101 LoopDispositions: { %loop: Computable }
433433
; CHECK-NEXT: Determining loop execution counts for: @tautological_select_like_phi

0 commit comments

Comments
 (0)