[llvm] r353816 - [IndVars] Fix corner case with unreachable Phi inputs. PR40454
Max Kazantsev via llvm-commits
llvm-commits at lists.llvm.org
Tue Feb 12 01:59:44 PST 2019
Author: mkazantsev
Date: Tue Feb 12 01:59:44 2019
New Revision: 353816
URL: http://llvm.org/viewvc/llvm-project?rev=353816&view=rev
Log:
[IndVars] Fix corner case with unreachable Phi inputs. PR40454
Logic in `getInsertPointForUses` doesn't account for a corner case when `Def`
only comes to a Phi user from unreachable blocks. In this case, the incoming
value may be arbitrary (and not even available in the input block) and break
the loop-related invariants that are asserted below.
In fact, if we encounter this situation, no IR modification is needed. This
Phi will be simplified away with nearest cleanup.
Differential Revision: https://reviews.llvm.org/D58045
Reviewed By: spatel
Modified:
llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
llvm/trunk/test/Transforms/IndVarSimplify/pr40454.ll
Modified: llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp?rev=353816&r1=353815&r2=353816&view=diff
==============================================================================
--- llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp (original)
+++ llvm/trunk/lib/Transforms/Scalar/IndVarSimplify.cpp Tue Feb 12 01:59:44 2019
@@ -217,7 +217,9 @@ bool IndVarSimplify::isValidRewrite(Valu
/// Determine the insertion point for this user. By default, insert immediately
/// before the user. SCEVExpander or LICM will hoist loop invariants out of the
/// loop. For PHI nodes, there may be multiple uses, so compute the nearest
-/// common dominator for the incoming blocks.
+/// common dominator for the incoming blocks. A nullptr can be returned if no
+/// viable location is found: it may happen if User is a PHI and Def only comes
+/// to this PHI from unreachable blocks.
static Instruction *getInsertPointForUses(Instruction *User, Value *Def,
DominatorTree *DT, LoopInfo *LI) {
PHINode *PHI = dyn_cast<PHINode>(User);
@@ -230,6 +232,10 @@ static Instruction *getInsertPointForUse
continue;
BasicBlock *InsertBB = PHI->getIncomingBlock(i);
+
+ if (!DT->isReachableFromEntry(InsertBB))
+ continue;
+
if (!InsertPt) {
InsertPt = InsertBB->getTerminator();
continue;
@@ -237,7 +243,11 @@ static Instruction *getInsertPointForUse
InsertBB = DT->findNearestCommonDominator(InsertPt->getParent(), InsertBB);
InsertPt = InsertBB->getTerminator();
}
- assert(InsertPt && "Missing phi operand");
+
+ // If we have skipped all inputs, it means that Def only comes to Phi from
+ // unreachable blocks.
+ if (!InsertPt)
+ return nullptr;
auto *DefI = dyn_cast<Instruction>(Def);
if (!DefI)
@@ -1307,10 +1317,12 @@ WidenIV::WidenedRecTy WidenIV::getWideRe
/// This IV user cannot be widen. Replace this use of the original narrow IV
/// with a truncation of the new wide IV to isolate and eliminate the narrow IV.
static void truncateIVUse(NarrowIVDefUse DU, DominatorTree *DT, LoopInfo *LI) {
+ auto *InsertPt = getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT, LI);
+ if (!InsertPt)
+ return;
LLVM_DEBUG(dbgs() << "INDVARS: Truncate IV " << *DU.WideDef << " for user "
<< *DU.NarrowUse << "\n");
- IRBuilder<> Builder(
- getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT, LI));
+ IRBuilder<> Builder(InsertPt);
Value *Trunc = Builder.CreateTrunc(DU.WideDef, DU.NarrowDef->getType());
DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, Trunc);
}
@@ -1347,8 +1359,10 @@ bool WidenIV::widenLoopCompare(NarrowIVD
assert(CastWidth <= IVWidth && "Unexpected width while widening compare.");
// Widen the compare instruction.
- IRBuilder<> Builder(
- getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT, LI));
+ auto *InsertPt = getInsertPointForUses(DU.NarrowUse, DU.NarrowDef, DT, LI);
+ if (!InsertPt)
+ return false;
+ IRBuilder<> Builder(InsertPt);
DU.NarrowUse->replaceUsesOfWith(DU.NarrowDef, DU.WideDef);
// Widen the other operand of the compare, if necessary.
Modified: llvm/trunk/test/Transforms/IndVarSimplify/pr40454.ll
URL: http://llvm.org/viewvc/llvm-project/llvm/trunk/test/Transforms/IndVarSimplify/pr40454.ll?rev=353816&r1=353815&r2=353816&view=diff
==============================================================================
--- llvm/trunk/test/Transforms/IndVarSimplify/pr40454.ll (original)
+++ llvm/trunk/test/Transforms/IndVarSimplify/pr40454.ll Tue Feb 12 01:59:44 2019
@@ -1,11 +1,32 @@
+; NOTE: Assertions have been autogenerated by utils/update_test_checks.py
; RUN: opt -S -indvars < %s | FileCheck %s
-; REQUIRES: asserts
-; XFAIL: *
target datalayout = "e-m:e-i64:64-f80:128-n8:16:32:64-S128"
define void @test() {
-; CHECK-LABEL: @test
+; CHECK-LABEL: @test(
+; CHECK-NEXT: bb:
+; CHECK-NEXT: br label [[BB2:%.*]]
+; CHECK: bb1:
+; CHECK-NEXT: br label [[BB2]]
+; CHECK: bb2:
+; CHECK-NEXT: [[TMP:%.*]] = phi i32 [ -9, [[BB:%.*]] ], [ [[TMP6:%.*]], [[BB1:%.*]] ]
+; CHECK-NEXT: br label [[BB3:%.*]]
+; CHECK: bb3:
+; CHECK-NEXT: [[TMP4:%.*]] = phi i32 [ -9, [[BB2]] ], [ [[TMP6]], [[BB10:%.*]] ]
+; CHECK-NEXT: br i1 false, label [[BB5:%.*]], label [[BB12:%.*]]
+; CHECK: bb5:
+; CHECK-NEXT: [[TMP6]] = add nsw i32 [[TMP4]], -1
+; CHECK-NEXT: br i1 undef, label [[BB8:%.*]], label [[BB9:%.*]]
+; CHECK: bb8:
+; CHECK-NEXT: br label [[BB10]]
+; CHECK: bb9:
+; CHECK-NEXT: br label [[BB10]]
+; CHECK: bb10:
+; CHECK-NEXT: br label [[BB3]]
+; CHECK: bb12:
+; CHECK-NEXT: ret void
+;
bb:
br label %bb2
More information about the llvm-commits
mailing list