[IndVars] Preserve LCSSA in `eliminateIdentitySCEV`
authorSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 7 Oct 2015 17:38:31 +0000 (17:38 +0000)
committerSanjoy Das <sanjoy@playingwithpointers.com>
Wed, 7 Oct 2015 17:38:31 +0000 (17:38 +0000)
Summary:
After r249211, SCEV can see through some LCSSA phis.  Add a
`replacementPreservesLCSSAForm` check before replacing uses of these phi
nodes with a simplified use of the induction variable to avoid breaking
LCSSA.

Fixes 25047.

Depends on D13460.

Reviewers: atrick, hfinkel

Subscribers: llvm-commits

Differential Revision: http://reviews.llvm.org/D13461

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@249575 91177308-0d34-0410-b5e6-96231b3b80d8

lib/Transforms/Utils/SimplifyIndVar.cpp
test/Transforms/IndVarSimplify/pr25047.ll [new file with mode: 0644]

index c7ec1bc263d3214a8bba457fcc381db37673a7a2..58715fa4bef1aafb4abdaafec8787b0e9be235a0 100644 (file)
@@ -333,6 +333,9 @@ bool SimplifyIndvar::eliminateIdentitySCEV(Instruction *UseInst,
     if (!DT || !DT->dominates(IVOperand, UseInst))
       return false;
 
+  if (!LI->replacementPreservesLCSSAForm(UseInst, IVOperand))
+    return false;
+
   DEBUG(dbgs() << "INDVARS: Eliminated identity: " << *UseInst << '\n');
 
   UseInst->replaceAllUsesWith(IVOperand);
diff --git a/test/Transforms/IndVarSimplify/pr25047.ll b/test/Transforms/IndVarSimplify/pr25047.ll
new file mode 100644 (file)
index 0000000..dc39a78
--- /dev/null
@@ -0,0 +1,49 @@
+; RUN: opt -indvars -S < %s | FileCheck %s
+
+target datalayout = "e-m:o-i64:64-f80:128-n8:16:32:64-S128"
+target triple = "x86_64-apple-macosx10.11.0"
+
+define void @fn1(i1 %c0, i1 %c1) {
+; CHECK-LABEL: @fn1(
+entry:
+  br i1 %c0, label %for.end.34, label %for.cond.1thread-pre-split
+
+for.cond.loopexit:                                ; preds = %for.end.29, %for.end.7
+  %f.lcssa = phi i32 [ %f.1, %for.end.29 ], [ %f.1, %for.end.7 ]
+  br i1 %c1, label %for.end.34, label %for.cond.1thread-pre-split
+
+for.cond.1thread-pre-split:                       ; preds = %for.cond.loopexit, %entry
+  %f.047 = phi i32 [ %f.lcssa, %for.cond.loopexit ], [ 0, %entry ]
+  br label %for.cond.1
+
+for.cond.1:                                       ; preds = %for.cond.1, %for.cond.1thread-pre-split
+  br i1 %c1, label %for.cond.4, label %for.cond.1
+
+for.cond.4:                                       ; preds = %for.end.29, %for.cond.1
+  %f.1 = phi i32 [ 0, %for.end.29 ], [ %f.047, %for.cond.1 ]
+  br label %for.cond.5
+
+for.cond.5:                                       ; preds = %for.cond.5, %for.cond.4
+  %h.0 = phi i32 [ 0, %for.cond.4 ], [ %inc, %for.cond.5 ]
+  %cmp = icmp slt i32 %h.0, 1
+  %inc = add nsw i32 %h.0, 1
+  br i1 %cmp, label %for.cond.5, label %for.end.7
+
+for.end.7:                                        ; preds = %for.cond.5
+  %g.lcssa = phi i32 [ %h.0, %for.cond.5 ]
+  %tobool10 = icmp eq i32 %g.lcssa, 0
+  br i1 %tobool10, label %for.end.8, label %for.cond.loopexit
+
+for.end.8:                       ; preds = %for.end.7
+  br i1 %c1, label %for.cond.19, label %for.end.29
+
+for.cond.19:                                      ; preds = %for.cond.19, %for.end.8
+  br label %for.cond.19
+
+for.end.29:                                       ; preds = %for.end.8
+  %tobool30 = icmp eq i32 %f.1, 0
+  br i1 %tobool30, label %for.cond.4, label %for.cond.loopexit
+
+for.end.34:                                       ; preds = %for.cond.loopexit, %entry
+  ret void
+}