[llvm] 3abaa37 - [LSR] Preserve LCSSA in expander when rewriting loop exit values.

Florian Hahn via llvm-commits llvm-commits at lists.llvm.org
Tue Sep 27 01:59:52 PDT 2022


Author: Florian Hahn
Date: 2022-09-27T09:58:48+01:00
New Revision: 3abaa3760d232b9ad530c7be982583573cf9ec3d

URL: https://github.com/llvm/llvm-project/commit/3abaa3760d232b9ad530c7be982583573cf9ec3d
DIFF: https://github.com/llvm/llvm-project/commit/3abaa3760d232b9ad530c7be982583573cf9ec3d.diff

LOG: [LSR] Preserve LCSSA in expander when rewriting loop exit values.

The expanded values when rewriting exit values need to preserve LCSSA.
Ask SCEVExpander to preserve LCSSA to ensure that.

Fixes #58007.

Added: 
    

Modified: 
    llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
    llvm/test/Transforms/LoopStrengthReduce/scev-expander-lcssa.ll

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
index 959ce9b2b14fb..8c94380821b67 100644
--- a/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
+++ b/llvm/lib/Transforms/Scalar/LoopStrengthReduce.cpp
@@ -6760,7 +6760,7 @@ static bool ReduceLoopStrength(Loop *L, IVUsers &IU, ScalarEvolution &SE,
   if (L->isRecursivelyLCSSAForm(DT, LI) && L->getExitBlock()) {
     SmallVector<WeakTrackingVH, 16> DeadInsts;
     const DataLayout &DL = L->getHeader()->getModule()->getDataLayout();
-    SCEVExpander Rewriter(SE, DL, "lsr", false);
+    SCEVExpander Rewriter(SE, DL, "lsr", true);
     int Rewrites = rewriteLoopExitValues(L, &LI, &TLI, &SE, &TTI, Rewriter, &DT,
                                          UnusedIndVarInLoop, DeadInsts);
     if (Rewrites) {

diff  --git a/llvm/test/Transforms/LoopStrengthReduce/scev-expander-lcssa.ll b/llvm/test/Transforms/LoopStrengthReduce/scev-expander-lcssa.ll
index e05dcda85138a..ec2ddfffcf05b 100644
--- a/llvm/test/Transforms/LoopStrengthReduce/scev-expander-lcssa.ll
+++ b/llvm/test/Transforms/LoopStrengthReduce/scev-expander-lcssa.ll
@@ -142,3 +142,47 @@ for.body45.preheader.i:                           ; preds = %if.else.i2488
 if.end107.i:                                      ; preds = %if.else.i2488, %for.cond.i2472
   unreachable
 }
+
+define void @test_pr58007(ptr %A) {
+; CHECK-LABEL: @test_pr58007(
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[LOOP_1:%.*]]
+; CHECK:       loop.1:
+; CHECK-NEXT:    [[L:%.*]] = load i16, ptr [[A:%.*]], align 2
+; CHECK-NEXT:    br i1 false, label [[LOOP_1]], label [[LOOP_2_HEADER_PREHEADER:%.*]]
+; CHECK:       loop.2.header.preheader:
+; CHECK-NEXT:    [[L_LCSSA:%.*]] = phi i16 [ [[L]], [[LOOP_1]] ]
+; CHECK-NEXT:    br label [[LOOP_2_HEADER:%.*]]
+; CHECK:       loop.2.header:
+; CHECK-NEXT:    [[P:%.*]] = phi i16 [ 1, [[LOOP_2_LATCH:%.*]] ], [ 0, [[LOOP_2_HEADER_PREHEADER]] ]
+; CHECK-NEXT:    [[CMP3_I:%.*]] = icmp eq i16 [[P]], 0
+; CHECK-NEXT:    br i1 [[CMP3_I]], label [[LOOP_2_LATCH]], label [[EXIT:%.*]]
+; CHECK:       loop.2.latch:
+; CHECK-NEXT:    br label [[LOOP_2_HEADER]]
+; CHECK:       exit:
+; CHECK-NEXT:    store i16 [[L_LCSSA]], ptr [[A]], align 2
+; CHECK-NEXT:    ret void
+;
+entry:
+  br label %loop.1
+
+loop.1:                                           ; preds = %loop.1, %entry
+  %l = load i16, ptr %A, align 2
+  %l.dec = add nsw i16 %l, -1
+  br i1 false, label %loop.1, label %loop.2.header
+
+loop.2.header:                                    ; preds = %loop.2.latch, %loop.1
+  %iv = phi i16 [ %l.dec, %loop.1 ], [ %iv.next, %loop.2.latch ]
+  %p = phi i16 [ 0, %loop.1 ], [ 1, %loop.2.latch ]
+  %cmp3.i = icmp eq i16 %p, 0
+  br i1 %cmp3.i, label %loop.2.latch, label %exit
+
+loop.2.latch:                                     ; preds = %loop.2.header
+  %iv.next = add nsw i16 %iv, 1
+  br label %loop.2.header
+
+exit:                                             ; preds = %loop.2.header
+  %iv.lcssa = phi i16 [ %iv, %loop.2.header ]
+  store i16 %iv.lcssa, ptr %A, align 2
+  ret void
+}


        


More information about the llvm-commits mailing list