Index: lib/Transforms/Scalar/IndVarSimplify.cpp =================================================================== --- lib/Transforms/Scalar/IndVarSimplify.cpp +++ lib/Transforms/Scalar/IndVarSimplify.cpp @@ -1327,9 +1327,9 @@ } // Does this user itself evaluate to a recurrence after widening? - const SCEVAddRecExpr *WideAddRec = getWideRecurrence(DU.NarrowUse); + const SCEVAddRecExpr *WideAddRec = getExtendedOperandRecurrence(DU); if (!WideAddRec) - WideAddRec = getExtendedOperandRecurrence(DU); + WideAddRec = getWideRecurrence(DU.NarrowUse); if (!WideAddRec) { // If use is a loop condition, try to promote the condition instead of Index: test/Transforms/IndVarSimplify/iv-widen.ll =================================================================== --- test/Transforms/IndVarSimplify/iv-widen.ll +++ test/Transforms/IndVarSimplify/iv-widen.ll @@ -69,3 +69,42 @@ declare void @dummy(i32) declare void @dummy.i64(i64) + + +define void @loop_2(i32 %size, i32 %nsteps, i32 %hsize, i32* %lined, i8 %tmp1) { +; CHECK-LABEL: @loop_2( +entry: + %cmp215 = icmp sgt i32 %size, 1 + %tmp0 = bitcast i32* %lined to i8* + br label %for.body + +for.body: + %j = phi i32 [ 0, %entry ], [ %inc6, %for.inc ] + %mul = mul nsw i32 %j, %size + %add = add nsw i32 %mul, %hsize + br i1 %cmp215, label %for.body2, label %for.inc + +; check that the induction variable of the inner loop has been widened after indvars. +; CHECK: [[INNERLOOPINV:%[^ ]+]] = add nsw i64 +; CHECK: for.body2: +; CHECK: %indvars.iv = phi i64 [ 1, %for.body2.preheader ], [ %indvars.iv.next, %for.body2 ] +; CHECK: [[WIDENED:%[^ ]+]] = add nsw i64 [[INNERLOOPINV]], %indvars.iv +; CHECK: %add.ptr = getelementptr inbounds i8, i8* %tmp0, i64 [[WIDENED]] +for.body2: + %k = phi i32 [ %inc, %for.body2 ], [ 1, %for.body ] + %add4 = add nsw i32 %add, %k + %idx.ext = sext i32 %add4 to i64 + %add.ptr = getelementptr inbounds i8, i8* %tmp0, i64 %idx.ext + store i8 %tmp1, i8* %add.ptr, align 1 + %inc = add nsw i32 %k, 1 + %cmp2 = icmp slt i32 %inc, %size + br i1 %cmp2, label %for.body2, label %for.inc + +for.inc: + %inc6 = add nsw i32 %j, 1 + %cmp = icmp slt i32 %inc6, %nsteps + br i1 %cmp, label %for.body, label %for.end.loopexit + +for.end.loopexit: + ret void +}