Index: llvm/lib/Transforms/Scalar/LoopFlatten.cpp =================================================================== --- llvm/lib/Transforms/Scalar/LoopFlatten.cpp +++ llvm/lib/Transforms/Scalar/LoopFlatten.cpp @@ -429,11 +429,12 @@ // Both of the loop limit values must be invariant in the outer loop // (non-instructions are all inherently invariant). - if (!OuterLoop->isLoopInvariant(InnerLimit)) { + bool Changed; + if (!OuterLoop->makeLoopInvariant(InnerLimit, Changed)) { LLVM_DEBUG(dbgs() << "inner loop limit not invariant\n"); return false; } - if (!OuterLoop->isLoopInvariant(OuterLimit)) { + if (!OuterLoop->makeLoopInvariant(OuterLimit, Changed)) { LLVM_DEBUG(dbgs() << "outer loop limit not invariant\n"); return false; } Index: llvm/test/Transforms/LoopFlatten/limit-not-invariant.ll =================================================================== --- llvm/test/Transforms/LoopFlatten/limit-not-invariant.ll +++ llvm/test/Transforms/LoopFlatten/limit-not-invariant.ll @@ -11,11 +11,11 @@ ; CHECK-NEXT: br i1 [[CMP26_NOT]], label [[FOR_END12:%.*]], label [[FOR_COND1_PREHEADER_LR_PH:%.*]] ; CHECK: for.cond1.preheader.lr.ph: ; CHECK-NEXT: [[CONV4:%.*]] = sext i16 [[VAL:%.*]] to i32 +; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[N]] to i64 ; CHECK-NEXT: br label [[FOR_COND1_PREHEADER_US:%.*]] ; CHECK: for.cond1.preheader.us: ; CHECK-NEXT: [[I_027_US:%.*]] = phi i32 [ 0, [[FOR_COND1_PREHEADER_LR_PH]] ], [ [[INC11_US:%.*]], [[FOR_COND1_FOR_INC10_CRIT_EDGE_US:%.*]] ] ; CHECK-NEXT: [[MUL_US:%.*]] = mul i32 [[I_027_US]], [[N]] -; CHECK-NEXT: [[WIDE_TRIP_COUNT:%.*]] = zext i32 [[N]] to i64 ; CHECK-NEXT: br label [[FOR_BODY3_US:%.*]] ; CHECK: for.body3.us: ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_COND1_PREHEADER_US]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY3_US]] ] @@ -88,11 +88,11 @@ ; CHECK-NEXT: br i1 [[CMP26_NOT]], label [[FOR_END12:%.*]], label [[FOR_COND1_PREHEADER_LR_PH:%.*]] ; CHECK: for.cond1.preheader.lr.ph: ; CHECK-NEXT: [[CONV4:%.*]] = sext i16 [[VAL:%.*]] to i32 +; CHECK-NEXT: [[TRUNC_TRIP_COUNT:%.*]] = trunc i64 [[M:%.*]] to i32 ; CHECK-NEXT: br label [[FOR_COND1_PREHEADER_US:%.*]] ; CHECK: for.cond1.preheader.us: ; CHECK-NEXT: [[I_027_US:%.*]] = phi i32 [ 0, [[FOR_COND1_PREHEADER_LR_PH]] ], [ [[INC11_US:%.*]], [[FOR_COND1_FOR_INC10_CRIT_EDGE_US:%.*]] ] ; CHECK-NEXT: [[MUL_US:%.*]] = mul i32 [[I_027_US]], [[N]] -; CHECK-NEXT: [[TRUNC_TRIP_COUNT:%.*]] = trunc i64 [[M:%.*]] to i32 ; CHECK-NEXT: br label [[FOR_BODY3_US:%.*]] ; CHECK: for.body3.us: ; CHECK-NEXT: [[INDVARS_IV:%.*]] = phi i64 [ 0, [[FOR_COND1_PREHEADER_US]] ], [ [[INDVARS_IV_NEXT:%.*]], [[FOR_BODY3_US]] ]