Add stride normalization to SCEV Normalize/Denormalize transformation.
authorMichael Zolotukhin <mzolotukhin@apple.com>
Tue, 18 Mar 2014 17:34:03 +0000 (17:34 +0000)
committerMichael Zolotukhin <mzolotukhin@apple.com>
Tue, 18 Mar 2014 17:34:03 +0000 (17:34 +0000)
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@204161 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Analysis/ScalarEvolutionNormalization.cpp
test/CodeGen/X86/lsr-normalization.ll

index 33036761a0b08275673c1bab683d5b21198a0794..1e4c0bdc7be004ebe5151f04acb1f240eb873c6b 100644 (file)
@@ -131,7 +131,10 @@ TransformImpl(const SCEV *S, Instruction *User, Value *OperandValToReplace) {
       // expression: {-2,+,1,+,2} + {1,+,2} => {-1,+,3,+,2}
       if (AR->isAffine() &&
           IVUseShouldUsePostIncValue(User, OperandValToReplace, L, &DT)) {
-        Result = SE.getMinusSCEV(Result, AR->getStepRecurrence(SE));
+        const SCEV *TransformedStep =
+          TransformSubExpr(AR->getStepRecurrence(SE),
+                           User, OperandValToReplace);
+        Result = SE.getMinusSCEV(Result, TransformedStep);
         Loops.insert(L);
       }
 #if 0
@@ -144,6 +147,20 @@ TransformImpl(const SCEV *S, Instruction *User, Value *OperandValToReplace) {
 #endif
       break;
     case Normalize:
+      // We want to normalize step expression, because otherwise we might not be
+      // able to denormalize to the original expression.
+      //
+      // Here is an example what will happen if we don't normalize step:
+      //  ORIGINAL ISE:
+      //    {(100 /u {1,+,1}<%bb16>),+,(100 /u {1,+,1}<%bb16>)}<%bb25>
+      //  NORMALIZED ISE:
+      //    {((-1 * (100 /u {1,+,1}<%bb16>)) + (100 /u {0,+,1}<%bb16>)),+,
+      //     (100 /u {0,+,1}<%bb16>)}<%bb25>
+      //  DENORMALIZED BACK ISE:
+      //    {((2 * (100 /u {1,+,1}<%bb16>)) + (-1 * (100 /u {2,+,1}<%bb16>))),+,
+      //     (100 /u {1,+,1}<%bb16>)}<%bb25>
+      //  Note that the initial value changes after normalization +
+      //  denormalization, which isn't correct.
       if (Loops.count(L)) {
         const SCEV *TransformedStep =
           TransformSubExpr(AR->getStepRecurrence(SE),
@@ -157,8 +174,14 @@ TransformImpl(const SCEV *S, Instruction *User, Value *OperandValToReplace) {
 #endif
       break;
     case Denormalize:
-      if (Loops.count(L))
-        Result = cast<SCEVAddRecExpr>(Result)->getPostIncExpr(SE);
+      // Here we want to normalize step expressions for the same reasons, as
+      // stated above.
+      if (Loops.count(L)) {
+        const SCEV *TransformedStep =
+          TransformSubExpr(AR->getStepRecurrence(SE),
+                           User, OperandValToReplace);
+        Result = SE.getAddExpr(Result, TransformedStep);
+      }
       break;
     }
     return Result;
index bbf8f010efde995c5b46782187f0c4728075da59..2c66734995307d11e9181d924eefb726aab9246f 100644 (file)
@@ -1,4 +1,5 @@
-; RUN: llc < %s -march=x86-64 | FileCheck %s
+; REQUIRES: asserts
+; RUN: llc -debug < %s -march=x86-64 2>&1 | FileCheck %s
 ; rdar://8168938
 
 ; This testcase involves SCEV normalization with the exit value from
@@ -6,6 +7,8 @@
 ; loop. The expression should be properly normalized and simplified,
 ; and require only a single division.
 
+; CHECK-NOT: DISCARDING (NORMALIZATION ISN'T INVERTIBLE)
+; CHECK: _main:
 ; CHECK: div
 ; CHECK-NOT: div