[PATCH] D58113: [SCEV] Teach computeSCEVAtScope benefit from one-input Phi. PR39673

Max Kazantsev via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Feb 12 03:57:28 PST 2019


mkazantsev created this revision.
mkazantsev added reviewers: sanjoy, uabelho, reames, skatkov.
Herald added subscribers: jdoerfert, javed.absar.

SCEV does not propagate arguments through one-input Phis for sake of LCSSA
form preservation. So whenever computeSCEVAtScope is asked about such Phi,
it will not go ahead and try to analyze its input. However it is possible that when
an this input leaves the loop through LCSSA Phi, it is a provable constant.

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.


https://reviews.llvm.org/D58113

Files:
  lib/Analysis/ScalarEvolution.cpp
  test/Transforms/IndVarSimplify/pr39673.ll


Index: test/Transforms/IndVarSimplify/pr39673.ll
===================================================================
--- test/Transforms/IndVarSimplify/pr39673.ll
+++ test/Transforms/IndVarSimplify/pr39673.ll
@@ -21,8 +21,7 @@
 ; 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
Index: lib/Analysis/ScalarEvolution.cpp
===================================================================
--- lib/Analysis/ScalarEvolution.cpp
+++ lib/Analysis/ScalarEvolution.cpp
@@ -8142,6 +8142,16 @@
             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


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D58113.186435.patch
Type: text/x-patch
Size: 1473 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20190212/304c96db/attachment.bin>


More information about the llvm-commits mailing list