Skip to content

Commit ffb2af3

Browse files
authored
[SCEVExpander] Attempt to reinfer flags dropped due to CSE (#72431)
LSR uses SCEVExpander to generate induction formulas. The expander internally tries to reuse existing IR expressions. To do that, it needs to strip any poison generating flags (nsw, nuw, exact, nneg, etc..) which may not be valid for the newly added users. This is conservatively correct, but has the effect that LSR will strip nneg flags on zext instructions involved in trip counts in loop preheaders. To avoid this, this patch adjusts the expanded to reinfer the flags on the CSE candidate if legal for all possible users. This should fix the regression reported in #71200. This should arguably be done inside canReuseInstruction instead, but doing it outside is more conservative compile time wise. Both canReuseInstruction and isGuaranteedNotToBePoison walk operand lists, so right now we are performing work which is roughly O(N^2) in the size of the operand graph. We should fix that before making the per operand step more expensive. My tenative plan is to land this, and then rework the code to sink the logic into more core interfaces.
1 parent e87f33d commit ffb2af3

File tree

7 files changed

+29
-14
lines changed

7 files changed

+29
-14
lines changed

llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp

Lines changed: 19 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1532,8 +1532,26 @@ Value *SCEVExpander::expand(const SCEV *S) {
15321532
V = visit(S);
15331533
V = fixupLCSSAFormFor(V);
15341534
} else {
1535-
for (Instruction *I : DropPoisonGeneratingInsts)
1535+
for (Instruction *I : DropPoisonGeneratingInsts) {
15361536
I->dropPoisonGeneratingFlagsAndMetadata();
1537+
// See if we can re-infer from first principles any of the flags we just
1538+
// dropped.
1539+
if (auto *OBO = dyn_cast<OverflowingBinaryOperator>(I))
1540+
if (auto Flags = SE.getStrengthenedNoWrapFlagsFromBinOp(OBO)) {
1541+
auto *BO = cast<BinaryOperator>(I);
1542+
BO->setHasNoUnsignedWrap(
1543+
ScalarEvolution::maskFlags(*Flags, SCEV::FlagNUW) == SCEV::FlagNUW);
1544+
BO->setHasNoSignedWrap(
1545+
ScalarEvolution::maskFlags(*Flags, SCEV::FlagNSW) == SCEV::FlagNSW);
1546+
}
1547+
if (auto *NNI = dyn_cast<PossiblyNonNegInst>(I)) {
1548+
auto *Src = NNI->getOperand(0);
1549+
if (isImpliedByDomCondition(ICmpInst::ICMP_SGE, Src,
1550+
Constant::getNullValue(Src->getType()), I,
1551+
DL).value_or(false))
1552+
NNI->setNonNeg(true);
1553+
}
1554+
}
15371555
}
15381556
// Remember the expanded value for this SCEV at this location.
15391557
//

llvm/test/CodeGen/RISCV/riscv-codegenprepare-asm.ll

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,7 @@ define void @test1(ptr nocapture noundef %a, i32 noundef signext %n) {
1010
; CHECK: # %bb.0: # %entry
1111
; CHECK-NEXT: blez a1, .LBB0_3
1212
; CHECK-NEXT: # %bb.1: # %for.body.preheader
13-
; CHECK-NEXT: slli a1, a1, 32
14-
; CHECK-NEXT: srli a1, a1, 30
13+
; CHECK-NEXT: slli a1, a1, 2
1514
; CHECK-NEXT: add a1, a0, a1
1615
; CHECK-NEXT: .LBB0_2: # %for.body
1716
; CHECK-NEXT: # =>This Inner Loop Header: Depth=1

llvm/test/CodeGen/Thumb2/mve-blockplacement.ll

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -385,19 +385,17 @@ define i32 @d(i64 %e, i32 %f, i64 %g, i32 %h) {
385385
; CHECK-NEXT: @ implicit-def: $r11
386386
; CHECK-NEXT: mov.w r9, #12
387387
; CHECK-NEXT: str r4, [sp, #12] @ 4-byte Spill
388-
; CHECK-NEXT: add.w r0, r0, r2, lsr #1
389388
; CHECK-NEXT: add.w r1, r1, r2, lsr #1
390-
; CHECK-NEXT: movw r2, #65532
391-
; CHECK-NEXT: vdup.32 q6, r0
392-
; CHECK-NEXT: movt r2, #32767
393-
; CHECK-NEXT: and.w r3, r1, r2
389+
; CHECK-NEXT: add.w r0, r0, r2, lsr #1
390+
; CHECK-NEXT: bic r3, r1, #3
394391
; CHECK-NEXT: adr r1, .LCPI1_0
395-
; CHECK-NEXT: vdup.32 q7, r0
396392
; CHECK-NEXT: vldrw.u32 q0, [r1]
397393
; CHECK-NEXT: adr r1, .LCPI1_1
398394
; CHECK-NEXT: vldrw.u32 q5, [r1]
399-
; CHECK-NEXT: strd r3, r7, [sp, #4] @ 8-byte Folded Spill
395+
; CHECK-NEXT: vdup.32 q6, r0
400396
; CHECK-NEXT: vadd.i32 q4, q0, r7
397+
; CHECK-NEXT: vdup.32 q7, r0
398+
; CHECK-NEXT: strd r3, r7, [sp, #4] @ 8-byte Folded Spill
401399
; CHECK-NEXT: b .LBB1_6
402400
; CHECK-NEXT: .LBB1_2: @ %for.body6.preheader
403401
; CHECK-NEXT: @ in Loop: Header=BB1_6 Depth=1

llvm/test/Transforms/IRCE/non-loop-invariant-rhs-instr.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ define i32 @test_01(i32 %A, i64 %Len, ptr %array) {
99
; CHECK-NEXT: br i1 [[TRIPCHECK]], label [[LOOP_PREHEADER:%.*]], label [[ZERO:%.*]]
1010
; CHECK: loop.preheader:
1111
; CHECK-NEXT: [[TMP0:%.*]] = zext i32 [[A:%.*]] to i64
12-
; CHECK-NEXT: [[TMP1:%.*]] = add i64 [[TMP0]], 1
12+
; CHECK-NEXT: [[TMP1:%.*]] = add nuw nsw i64 [[TMP0]], 1
1313
; CHECK-NEXT: [[SMIN:%.*]] = call i64 @llvm.smin.i64(i64 [[LEN]], i64 0)
1414
; CHECK-NEXT: [[TMP2:%.*]] = sub i64 [[LEN]], [[SMIN]]
1515
; CHECK-NEXT: [[UMIN:%.*]] = call i64 @llvm.umin.i64(i64 [[TMP2]], i64 [[TMP1]])

llvm/test/Transforms/IndVarSimplify/promote-iv-to-eliminate-casts.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -182,7 +182,7 @@ define void @promote_latch_condition_decrementing_loop_01(ptr %p, ptr %a) {
182182
; CHECK-LABEL: @promote_latch_condition_decrementing_loop_01(
183183
; CHECK-NEXT: entry:
184184
; CHECK-NEXT: [[LEN:%.*]] = load i32, ptr [[P:%.*]], align 4, !range [[RNG0:![0-9]+]]
185-
; CHECK-NEXT: [[LEN_MINUS_1:%.*]] = add i32 [[LEN]], -1
185+
; CHECK-NEXT: [[LEN_MINUS_1:%.*]] = add nsw i32 [[LEN]], -1
186186
; CHECK-NEXT: [[ZERO_CHECK:%.*]] = icmp eq i32 [[LEN]], 0
187187
; CHECK-NEXT: br i1 [[ZERO_CHECK]], label [[LOOPEXIT:%.*]], label [[PREHEADER:%.*]]
188188
; CHECK: preheader:

llvm/test/Transforms/LoopPredication/basic.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1681,7 +1681,7 @@ define i32 @ne_latch_zext(ptr %array, i32 %length, i16 %n16) {
16811681
; CHECK-LABEL: @ne_latch_zext(
16821682
; CHECK-NEXT: loop.preheader:
16831683
; CHECK-NEXT: [[N:%.*]] = zext i16 [[N16:%.*]] to i32
1684-
; CHECK-NEXT: [[NPLUS1:%.*]] = add i32 [[N]], 1
1684+
; CHECK-NEXT: [[NPLUS1:%.*]] = add nuw nsw i32 [[N]], 1
16851685
; CHECK-NEXT: [[TMP0:%.*]] = icmp ule i32 [[NPLUS1]], [[LENGTH:%.*]]
16861686
; CHECK-NEXT: [[TMP1:%.*]] = icmp ult i32 0, [[LENGTH]]
16871687
; CHECK-NEXT: [[TMP2:%.*]] = and i1 [[TMP1]], [[TMP0]]

llvm/test/Transforms/LoopUnroll/runtime-exit-phi-scev-invalidation.ll

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -96,7 +96,7 @@ define void @pr56282() {
9696
; CHECK: inner.2.preheader:
9797
; CHECK-NEXT: br label [[INNER_2]]
9898
; CHECK: inner.2:
99-
; CHECK-NEXT: [[OUTER_IV_NEXT]] = add i64 [[OUTER_IV]], 1
99+
; CHECK-NEXT: [[OUTER_IV_NEXT]] = add nuw i64 [[OUTER_IV]], 1
100100
; CHECK-NEXT: br label [[OUTER_HEADER]]
101101
; CHECK: exit:
102102
; CHECK-NEXT: ret void

0 commit comments

Comments
 (0)