Index: lib/Transforms/Scalar/IndVarSimplify.cpp =================================================================== --- lib/Transforms/Scalar/IndVarSimplify.cpp +++ lib/Transforms/Scalar/IndVarSimplify.cpp @@ -846,6 +846,9 @@ Type *WideType; bool IsSigned; + // True if the narrow induction variable can be proved to be always positive. + bool AlwaysPositive; + // Context LoopInfo *LI; Loop *L; @@ -868,6 +871,7 @@ OrigPhi(WI.NarrowIV), WideType(WI.WidestNativeType), IsSigned(WI.IsSigned), + AlwaysPositive(false), LI(LInfo), L(LI->getLoopFor(OrigPhi->getParent())), SE(SEv), @@ -1079,8 +1083,22 @@ if (!Cmp) return false; - // Sign of IV user and compare must match. - if (IsSigned != CmpInst::isSigned(Cmp->getPredicate())) + // We can legally widen the comparison in the following two cases: + // + // - The signedness of the IV extension and comparison match + // + // - The narrow IV is always positive (and thus its sign extension is equal + // to its zero extension). For instance, let's say we're zero extending + // %narrow for the following use + // + // icmp slt i32 %narrow, %val ... (A) + // + // and %narrow is always positive. Then + // + // (A) == icmp slt i32 sext(%narrow), sext(%val) + // == icmp slt i32 zext(%narrow), sext(%val) + + if (!(AlwaysPositive || IsSigned == Cmp->isSigned())) return false; Value *Op = Cmp->getOperand(Cmp->getOperand(0) == DU.NarrowDef ? 1 : 0); @@ -1094,7 +1112,7 @@ // Widen the other operand of the compare, if necessary. if (CastWidth < IVWidth) { - Value *ExtOp = getExtend(Op, WideType, IsSigned, Cmp); + Value *ExtOp = getExtend(Op, WideType, Cmp->isSigned(), Cmp); DU.NarrowUse->replaceUsesOfWith(Op, ExtOp); } return true; @@ -1244,6 +1262,9 @@ if (!AddRec) return nullptr; + AlwaysPositive = SE->isKnownPredicate(ICmpInst::ICMP_SGE, AddRec, + SE->getConstant(AddRec->getType(), 0)); + // Widen the induction variable expression. const SCEV *WideIVExpr = IsSigned ? SE->getSignExtendExpr(AddRec, WideType) : Index: test/Transforms/IndVarSimplify/widen-loop-comp.ll =================================================================== --- test/Transforms/IndVarSimplify/widen-loop-comp.ll +++ test/Transforms/IndVarSimplify/widen-loop-comp.ll @@ -193,3 +193,61 @@ for.end: ret i32 %sum.0 } + +define i32 @test6(i32* %a, i32 %b) { +; CHECK-LABEL: @test6( +; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64 +; CHECK: for.cond: +; CHECK: icmp sle i64 %indvars.iv, [[B_SEXT]] + +entry: + br label %for.cond + +for.cond: + %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ] + %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %cmp = icmp sle i32 %i.0, %b + br i1 %cmp, label %for.body, label %for.end + +for.body: + %idxprom = zext i32 %i.0 to i64 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom + %0 = load i32, i32* %arrayidx, align 4 + %add = add nsw i32 %sum.0, %0 + %inc = add nsw i32 %i.0, 1 + br label %for.cond + +for.end: + ret i32 %sum.0 +} + +define i32 @test7(i32* %a, i32 %b) { +; CHECK-LABEL: @test7( +; CHECK: [[B_ZEXT:%[a-z0-9]+]] = zext i32 %b to i64 +; CHECK: [[B_SEXT:%[a-z0-9]+]] = sext i32 %b to i64 +; CHECK: for.cond: +; CHECK: icmp ule i64 %indvars.iv, [[B_ZEXT]] +; CHECK: for.body: +; CHECK: icmp sle i64 %indvars.iv, [[B_SEXT]] + +entry: + br label %for.cond + +for.cond: + %sum.0 = phi i32 [ 0, %entry ], [ %add, %for.body ] + %i.0 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %cmp = icmp ule i32 %i.0, %b + br i1 %cmp, label %for.body, label %for.end + +for.body: + %idxprom = sext i32 %i.0 to i64 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom + %0 = load i32, i32* %arrayidx, align 4 + %add = add nsw i32 %sum.0, %0 + %inc = add nsw i32 %i.0, 1 + %cmp2 = icmp sle i32 %i.0, %b + br i1 %cmp2, label %for.cond, label %for.end + +for.end: + ret i32 %sum.0 +}