[llvm] d82f0b7 - [IndVars] Don't assume backedge value is instruction (PR64891)

Nikita Popov via llvm-commits llvm-commits at lists.llvm.org
Tue Aug 22 01:35:47 PDT 2023


Author: Nikita Popov
Date: 2023-08-22T10:33:33+02:00
New Revision: d82f0b74dec133cb90767edb032f987930d53772

URL: https://github.com/llvm/llvm-project/commit/d82f0b74dec133cb90767edb032f987930d53772
DIFF: https://github.com/llvm/llvm-project/commit/d82f0b74dec133cb90767edb032f987930d53772.diff

LOG: [IndVars] Don't assume backedge value is instruction (PR64891)

In degenerate cases, the backedge value can be folded to poison.

Fixes https://github.com/llvm/llvm-project/issues/64891.

Added: 
    llvm/test/Transforms/IndVarSimplify/pr64891.ll

Modified: 
    llvm/lib/Transforms/Utils/SimplifyIndVar.cpp

Removed: 
    


################################################################################
diff  --git a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
index a28916bc9baf3b..14582c7e95c238 100644
--- a/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
+++ b/llvm/lib/Transforms/Utils/SimplifyIndVar.cpp
@@ -1944,13 +1944,15 @@ PHINode *WidenIV::createWideIV(SCEVExpander &Rewriter) {
   // SCEVExpander. Henceforth, we produce 1-to-1 narrow to wide uses.
   if (BasicBlock *LatchBlock = L->getLoopLatch()) {
     WideInc =
-      cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));
-    WideIncExpr = SE->getSCEV(WideInc);
-    // Propagate the debug location associated with the original loop increment
-    // to the new (widened) increment.
-    auto *OrigInc =
-      cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
-    WideInc->setDebugLoc(OrigInc->getDebugLoc());
+        dyn_cast<Instruction>(WidePhi->getIncomingValueForBlock(LatchBlock));
+    if (WideInc) {
+      WideIncExpr = SE->getSCEV(WideInc);
+      // Propagate the debug location associated with the original loop
+      // increment to the new (widened) increment.
+      auto *OrigInc =
+          cast<Instruction>(OrigPhi->getIncomingValueForBlock(LatchBlock));
+      WideInc->setDebugLoc(OrigInc->getDebugLoc());
+    }
   }
 
   LLVM_DEBUG(dbgs() << "Wide IV: " << *WidePhi << "\n");

diff  --git a/llvm/test/Transforms/IndVarSimplify/pr64891.ll b/llvm/test/Transforms/IndVarSimplify/pr64891.ll
new file mode 100644
index 00000000000000..69b22bc373a602
--- /dev/null
+++ b/llvm/test/Transforms/IndVarSimplify/pr64891.ll
@@ -0,0 +1,27 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py UTC_ARGS: --version 2
+; RUN: opt -S -passes=indvars < %s | FileCheck %s
+
+target datalayout = "n8:16:32:64"
+
+; Just make sure this doesn't crash.
+; SCEVExpander produces a degenerate phi node for the widened IV here,
+; where the "increment" instruction folds to a poison value.
+define i32 @main() {
+; CHECK-LABEL: define i32 @main() {
+; CHECK-NEXT:  entry:
+; CHECK-NEXT:    br label [[LOOP:%.*]]
+; CHECK:       loop:
+; CHECK-NEXT:    br label [[LOOP]]
+;
+entry:
+  %div = sdiv i32 1, 0
+  %trunc = trunc i32 %div to i16
+  br label %loop
+
+loop:
+  %phi = phi i16 [ 0, %entry ], [ %or, %loop ]
+  %or = or i16 %phi, %trunc
+  %phi.ext = sext i16 %phi to i64
+  %add.ptr = getelementptr i8, ptr null, i64 %phi.ext
+  br label %loop
+}


        


More information about the llvm-commits mailing list