[PATCH] D80975: [SCEV][IndVarSimplify] insert point should not be block front if the front instruction is a PHI

ChenZheng via Phabricator via llvm-commits llvm-commits at lists.llvm.org
Tue Jun 2 23:25:41 PDT 2020


shchenz updated this revision to Diff 268063.
shchenz added a comment.

fix the insert point at the caller site


Repository:
  rG LLVM Github Monorepo

CHANGES SINCE LAST ACTION
  https://reviews.llvm.org/D80975/new/

https://reviews.llvm.org/D80975

Files:
  llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
  llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
  llvm/test/Transforms/IndVarSimplify/widen-i32-i8ptr.ll


Index: llvm/test/Transforms/IndVarSimplify/widen-i32-i8ptr.ll
===================================================================
--- /dev/null
+++ llvm/test/Transforms/IndVarSimplify/widen-i32-i8ptr.ll
@@ -0,0 +1,24 @@
+; RUN: opt < %s -indvars -S | FileCheck %s
+
+target datalayout = "e-m:e-i64:64-n32:64"
+
+define dso_local void @Widen_i32_i8ptr() local_unnamed_addr {
+; CHECK-LABEL: @Widen_i32_i8ptr(
+; CHECK: phi i8*
+; CHECK: phi i32
+entry:
+  %ptrids = alloca [15 x i8*], align 8
+  %arraydecay2032 = getelementptr inbounds [15 x i8*], [15 x i8*]* %ptrids, i64 0, i64 0
+  store i8** %arraydecay2032, i8*** inttoptr (i64 8 to i8***), align 8
+  br label %for.cond2106
+
+for.cond2106:                                     ; preds = %for.cond2106, %entry
+  %gid.0 = phi i8* [ null, %entry ], [ %incdec.ptr, %for.cond2106 ]
+  %i.0 = phi i32 [ 0, %entry ], [ %inc2117, %for.cond2106 ]
+  %incdec.ptr = getelementptr inbounds i8, i8* %gid.0, i64 1
+  %idxprom2114 = zext i32 %i.0 to i64
+  %arrayidx2115 = getelementptr inbounds [15 x i8*], [15 x i8*]* %ptrids, i64 0, i64 %idxprom2114
+  store i8* %gid.0, i8** %arrayidx2115, align 8
+  %inc2117 = add nuw nsw i32 %i.0, 1
+  br label %for.cond2106
+}
Index: llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
===================================================================
--- llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
+++ llvm/lib/Transforms/Utils/ScalarEvolutionExpander.cpp
@@ -1288,7 +1288,8 @@
   if (useSubtract)
     Step = SE.getNegativeSCEV(Step);
   // Expand the step somewhere that dominates the loop header.
-  Value *StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front());
+  Value *StepV = expandCodeFor(Step, IntTy, 
+                               &*L->getHeader()->getFirstInsertionPt());
 
   // The no-wrap behavior proved by IsIncrement(NUW|NSW) is only applicable if
   // we actually do emit an addition.  It does not apply if we emit a
@@ -1434,7 +1435,8 @@
       {
         // Expand the step somewhere that dominates the loop header.
         SCEVInsertPointGuard Guard(Builder, this);
-        StepV = expandCodeFor(Step, IntTy, &L->getHeader()->front());
+        StepV = expandCodeFor(Step, IntTy,
+                              &*L->getHeader()->getFirstInsertionPt());
       }
       Result = expandIVInc(PN, StepV, L, ExpandTy, IntTy, useSubtract);
     }
@@ -1941,7 +1943,8 @@
   // Emit code for it.
   SCEVInsertPointGuard Guard(Builder, this);
   PHINode *V =
-      cast<PHINode>(expandCodeFor(H, nullptr, &L->getHeader()->front()));
+      cast<PHINode>(expandCodeFor(H, nullptr,
+                                  &*L->getHeader()->getFirstInsertionPt()));
 
   return V;
 }
Index: llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
===================================================================
--- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
+++ llvm/lib/Transforms/Scalar/IndVarSimplify.cpp
@@ -1435,8 +1435,12 @@
   // either find an existing phi or materialize a new one. Either way, we
   // expect a well-formed cyclic phi-with-increments. i.e. any operand not part
   // of the phi-SCC dominates the loop entry.
-  Instruction *InsertPt = &L->getHeader()->front();
-  WidePhi = cast<PHINode>(Rewriter.expandCodeFor(AddRec, WideType, InsertPt));
+  Instruction *InsertPt = &*L->getHeader()->getFirstInsertionPt();
+  WidePhi = dyn_cast<PHINode>(Rewriter.expandCodeFor(AddRec, WideType, InsertPt));
+  // If the wide phi is not a phi node, for example a cast node, like bitcast,
+  // inttoptr, ptrtoint, just skip for now.
+  if (!WidePhi)
+    return nullptr;
 
   // Remembering the WideIV increment generated by SCEVExpander allows
   // widenIVUse to reuse it when widening the narrow IV's increment. We don't


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D80975.268063.patch
Type: text/x-patch
Size: 3749 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/llvm-commits/attachments/20200603/2545d290/attachment.bin>


More information about the llvm-commits mailing list