[SCEV] Try to re-use existing LCSSA phis when expanding SCEVAddRecExpr. (#147214)
If an AddRec is expanded outside a loop with a single exit block, check if any of the (lcssa) phi nodes in the exit block match the AddRec. If that's the case, simply use the existing lcssa phi. This can reduce the number of instruction created for SCEV expansions, mainly for runtime checks generated by the loop vectorizer. Compile-time impact should be mostly neutral https://llvm-compile-time-tracker.com/compare.php?from=48c7a3187f9831304a38df9bdb3b4d5bf6b6b1a2&to=cf9d039a7b0db5d0d912e0e2c01b19c2a653273a&stat=instructions:u PR: https://github.com/llvm/llvm-project/pull/147214
This commit is contained in:
parent
8f18dde6c0
commit
46357438ba
@ -530,6 +530,7 @@ private:
|
||||
|
||||
bool isExpandedAddRecExprPHI(PHINode *PN, Instruction *IncV, const Loop *L);
|
||||
|
||||
Value *tryToReuseLCSSAPhi(const SCEVAddRecExpr *S);
|
||||
Value *expandAddRecExprLiterally(const SCEVAddRecExpr *);
|
||||
PHINode *getAddRecExprPHILiterally(const SCEVAddRecExpr *Normalized,
|
||||
const Loop *L, Type *&TruncTy,
|
||||
|
||||
@ -1223,6 +1223,24 @@ Value *SCEVExpander::expandAddRecExprLiterally(const SCEVAddRecExpr *S) {
|
||||
return Result;
|
||||
}
|
||||
|
||||
Value *SCEVExpander::tryToReuseLCSSAPhi(const SCEVAddRecExpr *S) {
|
||||
const Loop *L = S->getLoop();
|
||||
BasicBlock *EB = L->getExitBlock();
|
||||
if (!EB || !EB->getSinglePredecessor() ||
|
||||
!SE.DT.dominates(EB, Builder.GetInsertBlock()))
|
||||
return nullptr;
|
||||
|
||||
for (auto &PN : EB->phis()) {
|
||||
if (!SE.isSCEVable(PN.getType()) || PN.getType() != S->getType())
|
||||
continue;
|
||||
auto *ExitV = SE.getSCEV(&PN);
|
||||
if (S == ExitV)
|
||||
return &PN;
|
||||
}
|
||||
|
||||
return nullptr;
|
||||
}
|
||||
|
||||
Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
|
||||
// In canonical mode we compute the addrec as an expression of a canonical IV
|
||||
// using evaluateAtIteration and expand the resulting SCEV expression. This
|
||||
@ -1262,6 +1280,11 @@ Value *SCEVExpander::visitAddRecExpr(const SCEVAddRecExpr *S) {
|
||||
return V;
|
||||
}
|
||||
|
||||
// If S is expanded outside the defining loop, check if there is a
|
||||
// matching LCSSA phi node for it.
|
||||
if (Value *V = tryToReuseLCSSAPhi(S))
|
||||
return V;
|
||||
|
||||
// {X,+,F} --> X + {0,+,F}
|
||||
if (!S->getStart()->isZero()) {
|
||||
if (isa<PointerType>(S->getType())) {
|
||||
|
||||
@ -18,11 +18,9 @@ define void @reuse_lcssa_phi_for_add_rec1(ptr %head) {
|
||||
; CHECK-NEXT: [[IV_NEXT]] = add nuw i64 [[IV]], 1
|
||||
; CHECK-NEXT: br i1 [[EC_1]], label %[[PH:.*]], label %[[LOOP_1]]
|
||||
; CHECK: [[PH]]:
|
||||
; CHECK-NEXT: [[IV_2_LCSSA:%.*]] = phi i32 [ [[IV_2]], %[[LOOP_1]] ]
|
||||
; CHECK-NEXT: [[IV_LCSSA:%.*]] = phi i64 [ [[IV]], %[[LOOP_1]] ]
|
||||
; CHECK-NEXT: [[IV_2_NEXT_LCSSA:%.*]] = phi i32 [ [[IV_2_NEXT]], %[[LOOP_1]] ]
|
||||
; CHECK-NEXT: [[TMP0:%.*]] = phi i32 [ [[IV_2_NEXT]], %[[LOOP_1]] ]
|
||||
; CHECK-NEXT: [[SRC_2:%.*]] = tail call noalias noundef dereferenceable_or_null(8) ptr @calloc(i64 1, i64 8)
|
||||
; CHECK-NEXT: [[TMP0:%.*]] = add i32 [[IV_2_LCSSA]], 1
|
||||
; CHECK-NEXT: [[SMIN:%.*]] = call i32 @llvm.smin.i32(i32 [[TMP0]], i32 1)
|
||||
; CHECK-NEXT: [[TMP1:%.*]] = sub i32 [[TMP0]], [[SMIN]]
|
||||
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user