[llvm] r363180 - [SCEV] Teach computeSCEVAtScope benefit from one-input Phi. PR39673

Philip Reames via llvm-commits llvm-commits at lists.llvm.org
Wed Jun 12 10:21:48 PDT 2019


Author: reames
Date: Wed Jun 12 10:21:47 2019
New Revision: 363180

URL: http://llvm.org/viewvc/llvm-project?rev=363180&view=rev
Log:
[SCEV] Teach computeSCEVAtScope benefit from one-input Phi. PR39673

SCEV does not propagate arguments through one-input Phis so as to make it easy for the SCEV expander (and related code) to preserve LCSSA.  It's not entirely clear this restriction is neccessary, but for the moment it exists.   For this reason, we don't analyze single-entry phi inputs.  However it is possible that when an this input leaves the loop through LCSSA Phi, it is a provable constant.  Missing that results in an order of optimization issue in loop exit value rewriting where we miss some oppurtunities based on order in which we visit sibling loops.

This patch teaches computeSCEVAtScope about this case. We can generalize it later, but so far we can only replace LCSSA Phis with their constant loop-exiting values.  We should probably also add similiar logic directly in the SCEV construction path itself.

Patch by: mkazantsev (with revised commit message by me)
Differential Revision: https://reviews.llvm.org/D58113


Modified:
    llvm/trunk/lib/Analysis/ScalarEvolution.cpp
    llvm/trunk/test/Transforms/IndVarSimplify/pr39673.ll

Modified: llvm/trunk/lib/Analysis/ScalarEvolution.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Analysis/ScalarEvolution.cpp?rev=363180&r1=363179&r2=363180&view=diff
==============================================================================
--- llvm/trunk/lib/Analysis/ScalarEvolution.cpp (original)
+++ llvm/trunk/lib/Analysis/ScalarEvolution.cpp Wed Jun 12 10:21:47 2019
@@ -8138,6 +8138,16 @@ const SCEV *ScalarEvolution::computeSCEV
             if (RV) return getSCEV(RV);
           }
         }
+
+        // If there is a single-input Phi, evaluate it at our scope. If we can
+        // prove that this replacement does not break LCSSA form, use new value.
+        if (PN->getNumOperands() == 1) {
+          const SCEV *Input = getSCEV(PN->getOperand(0));
+          const SCEV *InputAtScope = getSCEVAtScope(Input, L);
+          // TODO: We can generalize it using LI.replacementPreservesLCSSAForm,
+          // for the simplest case just support constants.
+          if (isa<SCEVConstant>(InputAtScope)) return InputAtScope;
+        }
       }
 
       // Okay, this is an expression that we cannot symbolically evaluate

Modified: llvm/trunk/test/Transforms/IndVarSimplify/pr39673.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/pr39673.ll?rev=363180&r1=363179&r2=363180&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/IndVarSimplify/pr39673.ll (original)
+++ llvm/trunk/test/Transforms/IndVarSimplify/pr39673.ll Wed Jun 12 10:21:47 2019
@@ -21,8 +21,7 @@ define i16 @test() {
 ; CHECK-NEXT:    [[CMP2:%.*]] = icmp ult i16 [[L2_ADD]], 2
 ; CHECK-NEXT:    br i1 [[CMP2]], label [[LOOP2]], label [[LOOP2_END:%.*]]
 ; CHECK:       loop2.end:
-; CHECK-NEXT:    [[K2_ADD_LCSSA:%.*]] = phi i16 [ [[K2_ADD]], [[LOOP2]] ]
-; CHECK-NEXT:    ret i16 [[K2_ADD_LCSSA]]
+; CHECK-NEXT:    ret i16 184
 ;
 entry:
   br label %loop1




More information about the llvm-commits mailing list