Index: llvm/lib/Transforms/Utils/LoopUtils.cpp =================================================================== --- llvm/lib/Transforms/Utils/LoopUtils.cpp +++ llvm/lib/Transforms/Utils/LoopUtils.cpp @@ -1395,7 +1395,10 @@ // and next SCEV may errneously get smaller cost. // Collect all the candidate PHINodes to be rewritten. - RewritePhiSet.emplace_back(PN, i, ExitValue, Inst, HighCost); + Instruction *InsertPt = + (isa(Inst) || isa(Inst)) ? + &*Inst->getParent()->getFirstInsertionPt() : Inst; + RewritePhiSet.emplace_back(PN, i, ExitValue, InsertPt, HighCost); } } } Index: llvm/test/Transforms/IndVarSimplify/rewrite-loop-exit-value.ll =================================================================== --- llvm/test/Transforms/IndVarSimplify/rewrite-loop-exit-value.ll +++ llvm/test/Transforms/IndVarSimplify/rewrite-loop-exit-value.ll @@ -158,4 +158,34 @@ ret i32 %phi_indvar } +define void @pr57336(i16 %end, i16 %m) mustprogress { +; CHECK-LABEL: @pr57336( +; CHECK-NEXT: entry: +; CHECK-NEXT: br label [[FOR_BODY:%.*]] +; CHECK: for.body: +; CHECK-NEXT: [[INC8:%.*]] = phi i16 [ [[INC:%.*]], [[FOR_BODY]] ], [ 0, [[ENTRY:%.*]] ] +; CHECK-NEXT: [[INC]] = add nuw nsw i16 [[INC8]], 1 +; CHECK-NEXT: [[MUL:%.*]] = mul nsw i16 [[INC8]], [[M:%.*]] +; CHECK-NEXT: [[CMP_NOT:%.*]] = icmp sgt i16 [[MUL]], [[END:%.*]] +; CHECK-NEXT: br i1 [[CMP_NOT]], label [[CRIT_EDGE:%.*]], label [[FOR_BODY]] +; CHECK: crit_edge: +; CHECK-NEXT: ret void +; +entry: + br label %for.body + +for.body: + %inc8 = phi i16 [ %inc, %for.body ], [ 0, %entry ] + %inc137 = phi i32 [ %inc1, %for.body ], [ 0, %entry ] + %inc1 = add nsw i32 %inc137, 1 + %inc = add nsw i16 %inc8, 1 + %mul = mul nsw i16 %m, %inc8 + %cmp.not = icmp slt i16 %end, %mul + br i1 %cmp.not, label %crit_edge, label %for.body + +crit_edge: + %inc137.lcssa = phi i32 [ %inc137, %for.body ] + %conv = trunc i32 %inc137.lcssa to i16 + ret void +}