[llvm] 0e34b6a - [LCSSA] Compute SCEV of LCSSA phi if original instruction had SCEV

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Mon Jun 26 05:43:39 PDT 2023


Author: Nikita Popov
Date: 2023-06-26T14:43:31+02:00
New Revision: 0e34b6a504a61d845209e58be70eaf829e59a922

URL: https://github.com/llvm/llvm-project/commit/0e34b6a504a61d845209e58be70eaf829e59a922
DIFF: https://github.com/llvm/llvm-project/commit/0e34b6a504a61d845209e58be70eaf829e59a922.diff

LOG: [LCSSA] Compute SCEV of LCSSA phi if original instruction had SCEV

The backstory is that the LCSSA invalidation we perform here is not
really necessary from a SCEV perspective. However, other code may
rely on the fact that invalidating only LCSSA phi nodes is sufficient
for transforms like loop peeling
(see https://reviews.llvm.org/D149331#4398582 for more details).

However, performing invalidation during LCSSA construction also
means that SCEV expansion (which may need to construct LCSSA) can
invalidate SCEV, which is somewhat unexpected and code may not be
prepared to deal with it (see the added test case, reported at
https://reviews.llvm.org/D149435#4428219).

Instead of invalidating SCEV, ensure that the LCSSA phi node also
has cached SCEV if the original instruction did. This means that
later invalidation of LCSSA phi nodes will work as expected. This
should avoid both the above issues and be more efficient.

Differential Revision: https://reviews.llvm.org/D153145

Added: 
    

Modified: 
    llvm/include/llvm/Analysis/ScalarEvolution.h
    llvm/lib/Transforms/Utils/LCSSA.cpp
    llvm/test/Transforms/IndVarSimplify/lcssa-preservation.ll

Removed: 
    


################################################################################
diff  --git a/llvm/include/llvm/Analysis/ScalarEvolution.h b/llvm/include/llvm/Analysis/ScalarEvolution.h
index 2db7126d8078d..ad6db524be5bf 100644
--- a/llvm/include/llvm/Analysis/ScalarEvolution.h
+++ b/llvm/include/llvm/Analysis/ScalarEvolution.h
@@ -560,6 +560,9 @@ class ScalarEvolution {
   /// expression.
   const SCEV *getSCEV(Value *V);
 
+  /// Return an existing SCEV for V if there is one, otherwise return nullptr.
+  const SCEV *getExistingSCEV(Value *V);
+
   const SCEV *getConstant(ConstantInt *V);
   const SCEV *getConstant(const APInt &Val);
   const SCEV *getConstant(Type *Ty, uint64_t V, bool isSigned = false);
@@ -2057,9 +2060,6 @@ class ScalarEvolution {
                           SmallPtrSetImpl<Instruction *> &Visited,
                           SmallVectorImpl<const SCEV *> &ToForget);
 
-  /// Return an existing SCEV for V if there is one, otherwise return nullptr.
-  const SCEV *getExistingSCEV(Value *V);
-
   /// Erase Value from ValueExprMap and ExprValueMap.
   void eraseValueFromMap(Value *V);
 

diff  --git a/llvm/lib/Transforms/Utils/LCSSA.cpp b/llvm/lib/Transforms/Utils/LCSSA.cpp
index 183e0a4422d15..c36b0533580b9 100644
--- a/llvm/lib/Transforms/Utils/LCSSA.cpp
+++ b/llvm/lib/Transforms/Utils/LCSSA.cpp
@@ -148,13 +148,10 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
     SSAUpdater SSAUpdate(&LocalInsertedPHIs);
     SSAUpdate.Initialize(I->getType(), I->getName());
 
-    // Force re-computation of I, as some users now need to use the new PHI
-    // node.
-    if (SE)
-      SE->forgetValue(I);
-
     // Insert the LCSSA phi's into all of the exit blocks dominated by the
     // value, and add them to the Phi's map.
+    bool HasSCEV = SE && SE->isSCEVable(I->getType()) &&
+                   SE->getExistingSCEV(I) != nullptr;
     for (BasicBlock *ExitBB : ExitBlocks) {
       if (!DT.dominates(DomNode, DT.getNode(ExitBB)))
         continue;
@@ -202,6 +199,13 @@ bool llvm::formLCSSAForInstructions(SmallVectorImpl<Instruction *> &Worklist,
       if (auto *OtherLoop = LI.getLoopFor(ExitBB))
         if (!L->contains(OtherLoop))
           PostProcessPHIs.push_back(PN);
+
+      // If we have a cached SCEV for the original instruction, make sure the
+      // new LCSSA phi node is also cached. This makes sures that BECounts
+      // based on it will be invalidated when the LCSSA phi node is invalidated,
+      // which some passes rely on.
+      if (HasSCEV)
+        SE->getSCEV(PN);
     }
 
     // Rewrite all uses outside the loop in terms of the new PHIs we just

diff  --git a/llvm/test/Transforms/IndVarSimplify/lcssa-preservation.ll b/llvm/test/Transforms/IndVarSimplify/lcssa-preservation.ll
index 1242e0080422d..a372fa900ddbf 100644
--- a/llvm/test/Transforms/IndVarSimplify/lcssa-preservation.ll
+++ b/llvm/test/Transforms/IndVarSimplify/lcssa-preservation.ll
@@ -102,7 +102,7 @@ define void @pr57000(i64 %a) {
 ; CHECK-NEXT:    [[CMP_EXT:%.*]] = zext i1 [[CMP]] to i8
 ; CHECK-NEXT:    br i1 [[CMP]], label [[LOOP_1]], label [[LOOP_2_HEADER_PREHEADER:%.*]]
 ; CHECK:       loop.2.header.preheader:
-; CHECK-NEXT:    [[CMP_LCSSA:%.*]] = phi i1 [ [[CMP]], [[LOOP_1]] ]
+; CHECK-NEXT:    [[CMP_LCSSA2:%.*]] = phi i1 [ [[CMP]], [[LOOP_1]] ]
 ; CHECK-NEXT:    [[CMP_EXT_LCSSA:%.*]] = phi i8 [ [[CMP_EXT]], [[LOOP_1]] ]
 ; CHECK-NEXT:    br label [[LOOP_2_HEADER_OUTER:%.*]]
 ; CHECK:       loop.2.header.outer:
@@ -114,7 +114,7 @@ define void @pr57000(i64 %a) {
 ; CHECK-NEXT:    i8 4, label [[LOOP_2_HEADER]]
 ; CHECK-NEXT:    ]
 ; CHECK:       loop.2.latch:
-; CHECK-NEXT:    [[CMP_TRUNC_LCSSA1:%.*]] = phi i1 [ [[CMP_LCSSA]], [[LOOP_2_HEADER]] ], [ [[CMP_LCSSA]], [[LOOP_2_HEADER]] ]
+; CHECK-NEXT:    [[CMP_TRUNC_LCSSA1:%.*]] = phi i1 [ [[CMP_LCSSA2]], [[LOOP_2_HEADER]] ], [ [[CMP_LCSSA2]], [[LOOP_2_HEADER]] ]
 ; CHECK-NEXT:    call void @use(i1 [[CMP_TRUNC_LCSSA1]])
 ; CHECK-NEXT:    br label [[LOOP_2_HEADER_OUTER]]
 ;
@@ -141,4 +141,63 @@ loop.2.latch:
   br label %loop.2.header
 }
 
+define void @D149435(i16 %arg) {
+; CHECK-LABEL: @D149435(
+; CHECK-NEXT:    br label [[LOOP1:%.*]]
+; CHECK:       loop1:
+; CHECK-NEXT:    [[FR:%.*]] = freeze i16 [[ARG:%.*]]
+; CHECK-NEXT:    [[ARRAYIDX_IDX:%.*]] = shl i16 [[FR]], 1
+; CHECK-NEXT:    [[OR:%.*]] = or i16 [[ARRAYIDX_IDX]], 1
+; CHECK-NEXT:    br i1 false, label [[LOOP1]], label [[LOOP2_PREHEADER:%.*]]
+; CHECK:       loop2.preheader:
+; CHECK-NEXT:    [[FR_LCSSA:%.*]] = phi i16 [ [[FR]], [[LOOP1]] ]
+; CHECK-NEXT:    [[OR_LCSSA:%.*]] = phi i16 [ [[OR]], [[LOOP1]] ]
+; CHECK-NEXT:    [[UMAX:%.*]] = call i16 @llvm.umax.i16(i16 [[OR_LCSSA]], i16 2)
+; CHECK-NEXT:    [[TMP1:%.*]] = add i16 [[UMAX]], -1
+; CHECK-NEXT:    [[TMP2:%.*]] = shl i16 [[FR_LCSSA]], 1
+; CHECK-NEXT:    [[TMP3:%.*]] = sub i16 [[TMP1]], [[TMP2]]
+; CHECK-NEXT:    [[TMP4:%.*]] = icmp eq i16 [[TMP3]], 0
+; CHECK-NEXT:    [[UMAX1:%.*]] = call i16 @llvm.umax.i16(i16 [[ARG]], i16 2)
+; CHECK-NEXT:    [[TMP5:%.*]] = sub i16 [[UMAX1]], [[ARG]]
+; CHECK-NEXT:    [[TMP6:%.*]] = icmp eq i16 [[TMP5]], 0
+; CHECK-NEXT:    br label [[LOOP2:%.*]]
+; CHECK:       loop2:
+; CHECK-NEXT:    br i1 [[TMP4]], label [[TRAP:%.*]], label [[FOR_BODY8:%.*]]
+; CHECK:       for.body8:
+; CHECK-NEXT:    br i1 [[TMP6]], label [[TRAP]], label [[LOOP2_LATCH:%.*]]
+; CHECK:       loop2.latch:
+; CHECK-NEXT:    br i1 false, label [[LOOP2]], label [[TRAP]]
+; CHECK:       trap:
+; CHECK-NEXT:    unreachable
+;
+  br label %loop1
+
+loop1:
+  %fr = freeze i16 %arg
+  %arrayidx.idx = shl i16 %fr, 1
+  %or = or i16 %arrayidx.idx, 1
+  br i1 false, label %loop1, label %loop2.preheader
+
+loop2.preheader:
+  br label %loop2
+
+loop2:
+  %iv = phi i16 [ %iv.next, %loop2.latch ], [ 0, %loop2.preheader ]
+  %add = add i16 %or, %iv
+  %cmp = icmp ugt i16 %add, 1
+  br i1 %cmp, label %trap, label %for.body8
+
+for.body8:
+  %add2 = add i16 %arg, %iv
+  %cmp2 = icmp ugt i16 %add2, 1
+  br i1 %cmp2, label %trap, label %loop2.latch
+
+loop2.latch:
+  %iv.next = add i16 %iv, 1
+  br i1 false, label %loop2, label %trap
+
+trap:
+  unreachable
+}
+
 declare void @use(i1)


        


More information about the llvm-commits mailing list