Skip to content

Commit 1bb3aea

Browse files
committed
Use RecursivelyDeleteDeadPHINode instead of cleaning IndVar manually
1 parent c7fe9ca commit 1bb3aea

File tree

2 files changed

+9
-35
lines changed

2 files changed

+9
-35
lines changed

llvm/lib/CodeGen/EVLIndVarSimplify.cpp

Lines changed: 9 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@
2727
#include "llvm/Support/MathExtras.h"
2828
#include "llvm/Support/raw_ostream.h"
2929
#include "llvm/Transforms/Scalar/LoopPassManager.h"
30+
#include "llvm/Transforms/Utils/Local.h"
3031

3132
#define DEBUG_TYPE "evl-iv-simplify"
3233

@@ -105,28 +106,6 @@ static uint32_t getVFFromIndVar(const SCEV *Step, const Function &F) {
105106
return 0U;
106107
}
107108

108-
// Remove the original induction variable if it's not used anywhere.
109-
static void tryCleanupOriginalIndVar(PHINode *OrigIndVar,
110-
const InductionDescriptor &IVD) {
111-
if (OrigIndVar->getNumIncomingValues() != 2)
112-
return;
113-
Value *InitValue = OrigIndVar->getIncomingValue(0);
114-
Value *RecValue = OrigIndVar->getIncomingValue(1);
115-
if (InitValue != IVD.getStartValue())
116-
std::swap(InitValue, RecValue);
117-
118-
// If the only user of OrigIndVar is the one that produces RecValue, then we
119-
// can safely remove it.
120-
if (!OrigIndVar->hasOneUse() || OrigIndVar->user_back() != RecValue)
121-
return;
122-
123-
LLVM_DEBUG(dbgs() << "Removed the original IndVar " << *OrigIndVar << "\n");
124-
// Turn OrigIndVar into dead code by replacing all its uses by the initial
125-
// value of this loop.
126-
OrigIndVar->replaceAllUsesWith(InitValue);
127-
OrigIndVar->eraseFromParent();
128-
}
129-
130109
bool EVLIndVarSimplifyImpl::run(Loop &L) {
131110
if (!EnableEVLIndVarSimplify)
132111
return false;
@@ -262,7 +241,14 @@ bool EVLIndVarSimplifyImpl::run(Loop &L) {
262241
auto *NewPred = Builder.CreateICmp(Pred, EVLIndVar, TC);
263242
OrigLatchCmp->replaceAllUsesWith(NewPred);
264243

265-
tryCleanupOriginalIndVar(IndVar, IVD);
244+
// llvm::RecursivelyDeleteDeadPHINode only deletes cycles whose values are
245+
// not used outside the cycles. However, in this case the now-RAUW-ed
246+
// OrigLatchCmp will be consied a use outside the cycle while in reality it's
247+
// practically dead. Thus we need to remove it before calling
248+
// RecursivelyDeleteDeadPHINode.
249+
(void)RecursivelyDeleteTriviallyDeadInstructions(OrigLatchCmp);
250+
if (llvm::RecursivelyDeleteDeadPHINode(IndVar))
251+
LLVM_DEBUG(dbgs() << "Removed original IndVar\n");
266252

267253
++NumEliminatedCanonicalIV;
268254

llvm/test/CodeGen/RISCV/evl-iv-simplify.ll

Lines changed: 0 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ define void @simple(ptr noalias %a, ptr noalias %b, <vscale x 4 x i32> %c, i64 %
2020
; CHECK-NEXT: [[N_RND_UP:%.*]] = add i64 [[N]], [[TMP8]]
2121
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 [[N_RND_UP]], [[TMP5]]
2222
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 [[N_RND_UP]], [[N_MOD_VF]]
23-
; CHECK-NEXT: [[TMP9:%.*]] = call i64 @llvm.vscale.i64()
24-
; CHECK-NEXT: [[TMP10:%.*]] = mul i64 [[TMP9]], 4
2523
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
2624
; CHECK: vector.body:
2725
; CHECK-NEXT: [[EVL_BASED_IV:%.*]] = phi i64 [ 0, [[VECTOR_PH]] ], [ [[INDEX_EVL_NEXT:%.*]], [[VECTOR_BODY]] ]
@@ -37,9 +35,7 @@ define void @simple(ptr noalias %a, ptr noalias %b, <vscale x 4 x i32> %c, i64 %
3735
; CHECK-NEXT: call void @llvm.vp.store.nxv4i32.p0(<vscale x 4 x i32> [[TMP18]], ptr align 4 [[TMP20]], <vscale x 4 x i1> shufflevector (<vscale x 4 x i1> insertelement (<vscale x 4 x i1> poison, i1 true, i64 0), <vscale x 4 x i1> poison, <vscale x 4 x i32> zeroinitializer), i32 [[TMP12]])
3836
; CHECK-NEXT: [[TMP21:%.*]] = zext i32 [[TMP12]] to i64
3937
; CHECK-NEXT: [[INDEX_EVL_NEXT]] = add i64 [[TMP21]], [[EVL_BASED_IV]]
40-
; CHECK-NEXT: [[INDEX_NEXT:%.*]] = add i64 0, [[TMP10]]
4138
; CHECK-NEXT: [[TMP23:%.*]] = icmp eq i64 [[INDEX_EVL_NEXT]], [[N]]
42-
; CHECK-NEXT: [[TMP26:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
4339
; CHECK-NEXT: br i1 [[TMP23]], label [[MIDDLE_BLOCK:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP0:![0-9]+]]
4440
; CHECK: middle.block:
4541
; CHECK-NEXT: br i1 true, label [[FOR_COND_CLEANUP:%.*]], label [[SCALAR_PH]]
@@ -166,8 +162,6 @@ define void @fixed_iv_step(ptr %arg0, ptr %arg1, i64 %N) #0 {
166162
; CHECK-NEXT: entry:
167163
; CHECK-NEXT: br label [[VECTOR_PH:%.*]]
168164
; CHECK: vector.ph:
169-
; CHECK-NEXT: [[N_RND_UP:%.*]] = add nsw i64 [[N]], 15
170-
; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N_RND_UP]], -16
171165
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 2 x ptr> poison, ptr [[ARG0]], i64 0
172166
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 2 x ptr> [[BROADCAST_SPLATINSERT]], <vscale x 2 x ptr> poison, <vscale x 2 x i32> zeroinitializer
173167
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
@@ -179,9 +173,7 @@ define void @fixed_iv_step(ptr %arg0, ptr %arg1, i64 %N) #0 {
179173
; CHECK-NEXT: tail call void @llvm.vp.store.nxv2p0.p0(<vscale x 2 x ptr> [[BROADCAST_SPLAT]], ptr align 8 [[GEP]], <vscale x 2 x i1> shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> poison, i1 true, i64 0), <vscale x 2 x i1> poison, <vscale x 2 x i32> zeroinitializer), i32 [[TMP1]])
180174
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
181175
; CHECK-NEXT: [[INDEX_EVL_NEXT]] = add i64 [[EVL_BASED_IV]], [[TMP2]]
182-
; CHECK-NEXT: [[LSR_IV_NEXT33:%.*]] = add i64 [[N_VEC]], -16
183176
; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i64 [[INDEX_EVL_NEXT]], [[N]]
184-
; CHECK-NEXT: [[TMP5:%.*]] = icmp eq i64 [[LSR_IV_NEXT33]], 0
185177
; CHECK-NEXT: br i1 [[TMP4]], label [[FOR_END_LOOPEXIT5:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP3]]
186178
; CHECK: for.end.loopexit5:
187179
; CHECK-NEXT: br label [[FOR_END:%.*]]
@@ -244,8 +236,6 @@ define void @fixed_iv_step_tc(ptr %arg0, ptr %arg1) #0 {
244236
; CHECK-NEXT: entry:
245237
; CHECK-NEXT: br label [[VECTOR_PH:%.*]]
246238
; CHECK: vector.ph:
247-
; CHECK-NEXT: [[N_RND_UP:%.*]] = add nsw i64 87, 15
248-
; CHECK-NEXT: [[N_VEC:%.*]] = and i64 [[N_RND_UP]], -16
249239
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 2 x ptr> poison, ptr [[ARG0]], i64 0
250240
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 2 x ptr> [[BROADCAST_SPLATINSERT]], <vscale x 2 x ptr> poison, <vscale x 2 x i32> zeroinitializer
251241
; CHECK-NEXT: br label [[VECTOR_BODY:%.*]]
@@ -257,9 +247,7 @@ define void @fixed_iv_step_tc(ptr %arg0, ptr %arg1) #0 {
257247
; CHECK-NEXT: tail call void @llvm.vp.store.nxv2p0.p0(<vscale x 2 x ptr> [[BROADCAST_SPLAT]], ptr align 8 [[GEP]], <vscale x 2 x i1> shufflevector (<vscale x 2 x i1> insertelement (<vscale x 2 x i1> poison, i1 true, i64 0), <vscale x 2 x i1> poison, <vscale x 2 x i32> zeroinitializer), i32 [[TMP1]])
258248
; CHECK-NEXT: [[TMP2:%.*]] = zext i32 [[TMP1]] to i64
259249
; CHECK-NEXT: [[INDEX_EVL_NEXT]] = add i64 [[EVL_BASED_IV]], [[TMP2]]
260-
; CHECK-NEXT: [[LSR_IV_NEXT33:%.*]] = add i64 [[N_VEC]], -16
261250
; CHECK-NEXT: [[TMP3:%.*]] = icmp eq i64 [[INDEX_EVL_NEXT]], 87
262-
; CHECK-NEXT: [[TMP4:%.*]] = icmp eq i64 [[LSR_IV_NEXT33]], 0
263251
; CHECK-NEXT: br i1 [[TMP3]], label [[FOR_END_LOOPEXIT5:%.*]], label [[VECTOR_BODY]], !llvm.loop [[LOOP3]]
264252
; CHECK: for.end.loopexit5:
265253
; CHECK-NEXT: br label [[FOR_END:%.*]]

0 commit comments

Comments
 (0)