Skip to content

Commit e3c537f

Browse files
committed
[VPlan] Consider non-header phis in planContainsAdditionalSimp.
Update planContainsAdditionalSimplifications to also check phis not in the loop header. This ensures we don't miss cases where VPBlendRecipes (which correspond to such phis) have been simplified. Fixes #107473.
1 parent 99fb150 commit e3c537f

File tree

2 files changed

+86
-3
lines changed

2 files changed

+86
-3
lines changed

llvm/lib/Transforms/Vectorize/LoopVectorize.cpp

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -7314,9 +7314,10 @@ static bool planContainsAdditionalSimplifications(VPlan &Plan,
73147314
// Return true if the loop contains any instructions that are not also part of
73157315
// the VPlan or are skipped for VPlan-based cost computations. This indicates
73167316
// that the VPlan contains extra simplifications.
7317-
return any_of(TheLoop->blocks(), [&SeenInstrs, &CostCtx](BasicBlock *BB) {
7318-
return any_of(*BB, [&SeenInstrs, &CostCtx](Instruction &I) {
7319-
if (isa<PHINode>(&I))
7317+
return any_of(TheLoop->blocks(), [&SeenInstrs, &CostCtx,
7318+
TheLoop](BasicBlock *BB) {
7319+
return any_of(*BB, [&SeenInstrs, &CostCtx, TheLoop, BB](Instruction &I) {
7320+
if (isa<PHINode>(&I) && BB == TheLoop->getHeader())
73207321
return false;
73217322
return !SeenInstrs.contains(&I) && !CostCtx.skipCostComputation(&I, true);
73227323
});

llvm/test/Transforms/LoopVectorize/RISCV/dead-ops-cost.ll

Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -322,7 +322,87 @@ return:
322322
ret i32 0
323323
}
324324

325+
; Test case for https://github.com/llvm/llvm-project/issues/107473.
326+
define void @test_phi_in_latch_redundant(ptr %dst, i32 %a) {
327+
; CHECK-LABEL: define void @test_phi_in_latch_redundant(
328+
; CHECK-SAME: ptr [[DST:%.*]], i32 [[A:%.*]]) #[[ATTR0]] {
329+
; CHECK-NEXT: [[ENTRY:.*]]:
330+
; CHECK-NEXT: [[TMP0:%.*]] = call i64 @llvm.vscale.i64()
331+
; CHECK-NEXT: [[TMP1:%.*]] = mul i64 [[TMP0]], 2
332+
; CHECK-NEXT: [[MIN_ITERS_CHECK:%.*]] = icmp ult i64 37, [[TMP1]]
333+
; CHECK-NEXT: br i1 [[MIN_ITERS_CHECK]], label %[[SCALAR_PH:.*]], label %[[VECTOR_PH:.*]]
334+
; CHECK: [[VECTOR_PH]]:
335+
; CHECK-NEXT: [[TMP2:%.*]] = call i64 @llvm.vscale.i64()
336+
; CHECK-NEXT: [[TMP3:%.*]] = mul i64 [[TMP2]], 2
337+
; CHECK-NEXT: [[N_MOD_VF:%.*]] = urem i64 37, [[TMP3]]
338+
; CHECK-NEXT: [[N_VEC:%.*]] = sub i64 37, [[N_MOD_VF]]
339+
; CHECK-NEXT: [[IND_END:%.*]] = mul i64 [[N_VEC]], 9
340+
; CHECK-NEXT: [[TMP4:%.*]] = call i64 @llvm.vscale.i64()
341+
; CHECK-NEXT: [[TMP5:%.*]] = mul i64 [[TMP4]], 2
342+
; CHECK-NEXT: [[TMP6:%.*]] = call <vscale x 2 x i64> @llvm.stepvector.nxv2i64()
343+
; CHECK-NEXT: [[TMP7:%.*]] = add <vscale x 2 x i64> [[TMP6]], zeroinitializer
344+
; CHECK-NEXT: [[TMP8:%.*]] = mul <vscale x 2 x i64> [[TMP7]], shufflevector (<vscale x 2 x i64> insertelement (<vscale x 2 x i64> poison, i64 9, i64 0), <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer)
345+
; CHECK-NEXT: [[INDUCTION:%.*]] = add <vscale x 2 x i64> zeroinitializer, [[TMP8]]
346+
; CHECK-NEXT: [[TMP9:%.*]] = mul i64 9, [[TMP5]]
347+
; CHECK-NEXT: [[DOTSPLATINSERT:%.*]] = insertelement <vscale x 2 x i64> poison, i64 [[TMP9]], i64 0
348+
; CHECK-NEXT: [[DOTSPLAT:%.*]] = shufflevector <vscale x 2 x i64> [[DOTSPLATINSERT]], <vscale x 2 x i64> poison, <vscale x 2 x i32> zeroinitializer
349+
; CHECK-NEXT: [[BROADCAST_SPLATINSERT:%.*]] = insertelement <vscale x 2 x i32> poison, i32 [[A]], i64 0
350+
; CHECK-NEXT: [[BROADCAST_SPLAT:%.*]] = shufflevector <vscale x 2 x i32> [[BROADCAST_SPLATINSERT]], <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer
351+
; CHECK-NEXT: br label %[[VECTOR_BODY:.*]]
352+
; CHECK: [[VECTOR_BODY]]:
353+
; CHECK-NEXT: [[INDEX:%.*]] = phi i64 [ 0, %[[VECTOR_PH]] ], [ [[INDEX_NEXT:%.*]], %[[VECTOR_BODY]] ]
354+
; CHECK-NEXT: [[VEC_IND:%.*]] = phi <vscale x 2 x i64> [ [[INDUCTION]], %[[VECTOR_PH]] ], [ [[VEC_IND_NEXT:%.*]], %[[VECTOR_BODY]] ]
355+
; CHECK-NEXT: [[TMP10:%.*]] = xor <vscale x 2 x i32> [[BROADCAST_SPLAT]], shufflevector (<vscale x 2 x i32> insertelement (<vscale x 2 x i32> poison, i32 -1, i64 0), <vscale x 2 x i32> poison, <vscale x 2 x i32> zeroinitializer)
356+
; CHECK-NEXT: [[TMP11:%.*]] = getelementptr i32, ptr [[DST]], <vscale x 2 x i64> [[VEC_IND]]
357+
; CHECK-NEXT: call void @llvm.masked.scatter.nxv2i32.nxv2p0(<vscale x 2 x i32> [[TMP10]], <vscale x 2 x ptr> [[TMP11]], i32 4, <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))
358+
; CHECK-NEXT: [[INDEX_NEXT]] = add nuw i64 [[INDEX]], [[TMP5]]
359+
; CHECK-NEXT: [[VEC_IND_NEXT]] = add <vscale x 2 x i64> [[VEC_IND]], [[DOTSPLAT]]
360+
; CHECK-NEXT: [[TMP12:%.*]] = icmp eq i64 [[INDEX_NEXT]], [[N_VEC]]
361+
; CHECK-NEXT: br i1 [[TMP12]], label %[[MIDDLE_BLOCK:.*]], label %[[VECTOR_BODY]], !llvm.loop [[LOOP18:![0-9]+]]
362+
; CHECK: [[MIDDLE_BLOCK]]:
363+
; CHECK-NEXT: [[CMP_N:%.*]] = icmp eq i64 37, [[N_VEC]]
364+
; CHECK-NEXT: br i1 [[CMP_N]], label %[[EXIT:.*]], label %[[SCALAR_PH]]
365+
; CHECK: [[SCALAR_PH]]:
366+
; CHECK-NEXT: [[BC_RESUME_VAL:%.*]] = phi i64 [ [[IND_END]], %[[MIDDLE_BLOCK]] ], [ 0, %[[ENTRY]] ]
367+
; CHECK-NEXT: br label %[[LOOP_HEADER:.*]]
368+
; CHECK: [[LOOP_HEADER]]:
369+
; CHECK-NEXT: [[IV:%.*]] = phi i64 [ [[BC_RESUME_VAL]], %[[SCALAR_PH]] ], [ [[IV_NEXT:%.*]], %[[LOOP_LATCH:.*]] ]
370+
; CHECK-NEXT: br i1 false, label %[[LOOP_LATCH]], label %[[THEN:.*]]
371+
; CHECK: [[THEN]]:
372+
; CHECK-NEXT: [[NOT_A:%.*]] = xor i32 [[A]], -1
373+
; CHECK-NEXT: br label %[[LOOP_LATCH]]
374+
; CHECK: [[LOOP_LATCH]]:
375+
; CHECK-NEXT: [[P:%.*]] = phi i32 [ [[NOT_A]], %[[THEN]] ], [ 0, %[[LOOP_HEADER]] ]
376+
; CHECK-NEXT: [[GEP:%.*]] = getelementptr i32, ptr [[DST]], i64 [[IV]]
377+
; CHECK-NEXT: store i32 [[P]], ptr [[GEP]], align 4
378+
; CHECK-NEXT: [[IV_NEXT]] = add i64 [[IV]], 9
379+
; CHECK-NEXT: [[EC:%.*]] = icmp slt i64 [[IV]], 322
380+
; CHECK-NEXT: br i1 [[EC]], label %[[LOOP_HEADER]], label %[[EXIT]], !llvm.loop [[LOOP19:![0-9]+]]
381+
; CHECK: [[EXIT]]:
382+
; CHECK-NEXT: ret void
383+
;
384+
entry:
385+
br label %loop.header
325386

387+
loop.header:
388+
%iv = phi i64 [ 0, %entry ], [ %iv.next, %loop.latch ]
389+
br i1 false, label %loop.latch, label %then
390+
391+
then:
392+
%not.a = xor i32 %a, -1
393+
br label %loop.latch
394+
395+
loop.latch:
396+
%p = phi i32 [ %not.a, %then ], [ 0, %loop.header ]
397+
%gep = getelementptr i32, ptr %dst, i64 %iv
398+
store i32 %p, ptr %gep, align 4
399+
%iv.next = add i64 %iv, 9
400+
%ec = icmp slt i64 %iv, 322
401+
br i1 %ec, label %loop.header, label %exit
402+
403+
exit:
404+
ret void
405+
}
326406

327407
;.
328408
; CHECK: [[LOOP0]] = distinct !{[[LOOP0]], [[META1:![0-9]+]], [[META2:![0-9]+]]}
@@ -343,4 +423,6 @@ return:
343423
; CHECK: [[META15]] = distinct !{[[META15]], [[META13]]}
344424
; CHECK: [[LOOP16]] = distinct !{[[LOOP16]], [[META1]], [[META2]]}
345425
; CHECK: [[LOOP17]] = distinct !{[[LOOP17]], [[META1]]}
426+
; CHECK: [[LOOP18]] = distinct !{[[LOOP18]], [[META1]], [[META2]]}
427+
; CHECK: [[LOOP19]] = distinct !{[[LOOP19]], [[META2]], [[META1]]}
346428
;.

0 commit comments

Comments
 (0)