Index: llvm/lib/Transforms/Scalar/IndVarSimplify.cpp =================================================================== --- llvm/lib/Transforms/Scalar/IndVarSimplify.cpp +++ llvm/lib/Transforms/Scalar/IndVarSimplify.cpp @@ -875,7 +875,8 @@ int LatchIdx = Phi->getBasicBlockIndex(L->getLoopLatch()); Value *IncV = Phi->getIncomingValue(LatchIdx); - return (getLoopPhiForCounter(IncV, L) == Phi); + return (getLoopPhiForCounter(IncV, L) == Phi && + isa(SE->getSCEV(IncV))); } /// Search the loop header for a loop counter (anadd rec w/step of one) @@ -1103,7 +1104,9 @@ // not covered by the post-inc addrec. (If the new IV was not dynamically // dead, it could not be poison on the first iteration in the first place.) if (auto *BO = dyn_cast(IncVar)) { - const SCEVAddRecExpr *AR = cast(SE->getSCEV(IncVar)); + const SCEV *IncSCEV = SE->getSCEV(IncVar); + assert(isa(IncSCEV) && "Expected an add recurrence"); + const SCEVAddRecExpr *AR = cast(IncSCEV); if (BO->hasNoUnsignedWrap()) BO->setHasNoUnsignedWrap(AR->hasNoUnsignedWrap()); if (BO->hasNoSignedWrap()) Index: llvm/test/Transforms/IndVarSimplify/lftr.ll =================================================================== --- llvm/test/Transforms/IndVarSimplify/lftr.ll +++ llvm/test/Transforms/IndVarSimplify/lftr.ll @@ -657,6 +657,52 @@ ret void } +define void @PR49993() { +; CHECK-LABEL: @PR49993( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[IF_END:%.*]] +; CHECK: d: +; CHECK-NEXT: [[PHI:%.*]] = phi i32 [ [[ADD:%.*]], [[D:%.*]] ], [ [[REM10:%.*]], [[IF_END]] ] +; CHECK-NEXT: [[ADD]] = add nsw i32 [[PHI]], 1 +; CHECK-NEXT: [[CMP:%.*]] = icmp slt i32 [[PHI]], 3 +; CHECK-NEXT: br i1 [[CMP]], label [[D]], label [[IF_END_LOOPEXIT:%.*]] +; CHECK: if.end.loopexit: +; CHECK-NEXT: br label [[IF_END]] +; CHECK: if.end: +; CHECK-NEXT: [[REM1:%.*]] = urem i32 undef, undef +; CHECK-NEXT: [[REM2:%.*]] = urem i32 [[REM1]], undef +; CHECK-NEXT: [[REM3:%.*]] = urem i32 [[REM2]], undef +; CHECK-NEXT: [[REM4:%.*]] = urem i32 [[REM3]], undef +; CHECK-NEXT: [[REM5:%.*]] = urem i32 [[REM4]], undef +; CHECK-NEXT: [[REM6:%.*]] = urem i32 [[REM5]], undef +; CHECK-NEXT: [[REM7:%.*]] = urem i32 [[REM6]], undef +; CHECK-NEXT: [[REM8:%.*]] = urem i32 [[REM7]], undef +; CHECK-NEXT: [[REM9:%.*]] = urem i32 [[REM8]], undef +; CHECK-NEXT: [[REM10]] = urem i32 [[REM9]], undef +; CHECK-NEXT: br label [[D]] +; +entry: + br label %if.end + +d: + %phi = phi i32 [ %add, %d ], [ %rem10, %if.end ] + %add = add nsw i32 %phi, 1 + %cmp = icmp slt i32 %phi, 3 + br i1 %cmp, label %d, label %if.end + +if.end: + %rem1 = urem i32 undef, undef + %rem2 = urem i32 %rem1, undef + %rem3 = urem i32 %rem2, undef + %rem4 = urem i32 %rem3, undef + %rem5 = urem i32 %rem4, undef + %rem6 = urem i32 %rem5, undef + %rem7 = urem i32 %rem6, undef + %rem8 = urem i32 %rem7, undef + %rem9 = urem i32 %rem8, undef + %rem10 = urem i32 %rem9, undef + br label %d +} declare i32 @llvm.loop.decrement.reg.i32(i32, i32)