Index: lib/Transforms/Utils/SimplifyIndVar.cpp =================================================================== --- lib/Transforms/Utils/SimplifyIndVar.cpp +++ lib/Transforms/Utils/SimplifyIndVar.cpp @@ -38,6 +38,9 @@ STATISTIC( NumSimplifiedSDiv, "Number of IV signed division operations converted to unsigned division"); +STATISTIC( + NumSimplifiedSRem, + "Number of IV signed remainder operations converted to unsigned remainder"); STATISTIC(NumElimCmp , "Number of IV comparisons eliminated"); namespace { @@ -76,9 +79,10 @@ bool eliminateOverflowIntrinsic(CallInst *CI); bool eliminateIVUser(Instruction *UseInst, Instruction *IVOperand); void eliminateIVComparison(ICmpInst *ICmp, Value *IVOperand); - void eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand, + bool eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand, bool IsSigned); bool eliminateSDiv(BinaryOperator *SDiv); + bool eliminateSRem(BinaryOperator *SRem); bool strengthenOverflowingOperation(BinaryOperator *OBO, Value *IVOperand); }; } @@ -296,15 +300,40 @@ return false; } +bool SimplifyIndvar::eliminateSRem(BinaryOperator *SRem) { + // Get the SCEVs for the ICmp operands. + auto *N = SE->getSCEV(SRem->getOperand(0)); + auto *D = SE->getSCEV(SRem->getOperand(1)); + + // Simplify unnecessary loops away. + const Loop *L = LI->getLoopFor(SRem->getParent()); + N = SE->getSCEVAtScope(N, L); + D = SE->getSCEVAtScope(D, L); + + // Replace sdiv by udiv if both of the operands are non-negative + if (SE->isKnownNonNegative(N) && SE->isKnownNonNegative(D)) { + auto *URem = BinaryOperator::Create( + BinaryOperator::URem, SRem->getOperand(0), SRem->getOperand(1), + SRem->getName() + ".urem", SRem); + SRem->replaceAllUsesWith(URem); + DEBUG(dbgs() << "INDVARS: Simplified srem: " << *SRem << '\n'); + ++NumSimplifiedSRem; + Changed = true; + DeadInsts.push_back(SRem); + return true; + } + + return false; +} + /// SimplifyIVUsers helper for eliminating useless /// remainder operations operating on an induction variable. -void SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem, - Value *IVOperand, - bool IsSigned) { +bool SimplifyIndvar::eliminateIVRemainder(BinaryOperator *Rem, Value *IVOperand, + bool IsSigned) { // We're only interested in the case where we know something about // the numerator. if (IVOperand != Rem->getOperand(0)) - return; + return false; // Get the SCEVs for the ICmp operands. const SCEV *S = SE->getSCEV(Rem->getOperand(0)); @@ -324,12 +353,12 @@ // (i+1) % n --> (i+1)==n?0:(i+1) if i is in [0,n). const SCEV *LessOne = SE->getMinusSCEV(S, SE->getOne(S->getType())); if (IsSigned && !SE->isKnownNonNegative(LessOne)) - return; + return false; if (!SE->isKnownPredicate(IsSigned ? ICmpInst::ICMP_SLT : ICmpInst::ICMP_ULT, LessOne, X)) - return; + return false; ICmpInst *ICmp = new ICmpInst(Rem, ICmpInst::ICMP_EQ, Rem->getOperand(0), Rem->getOperand(1)); @@ -344,6 +373,7 @@ ++NumElimRem; Changed = true; DeadInsts.emplace_back(Rem); + return true; } bool SimplifyIndvar::eliminateOverflowIntrinsic(CallInst *CI) { @@ -460,7 +490,8 @@ if (BinaryOperator *Bin = dyn_cast(UseInst)) { bool IsSRem = Bin->getOpcode() == Instruction::SRem; if (IsSRem || Bin->getOpcode() == Instruction::URem) { - eliminateIVRemainder(Bin, IVOperand, IsSRem); + if (!eliminateIVRemainder(Bin, IVOperand, IsSRem) && IsSRem) + eliminateSRem(Bin); return true; } Index: test/Transforms/IndVarSimplify/replace-srem-by-urem.ll =================================================================== --- /dev/null +++ test/Transforms/IndVarSimplify/replace-srem-by-urem.ll @@ -0,0 +1,109 @@ +; RUN: opt < %s -indvars -S | FileCheck %s + +define void @test0(i32* %a) { +; CHECK-LABEL: @test0( +entry: + br label %for.body + +for.body: ; preds = %entry, %for.body + %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %rem = srem i32 %i.01, 2 +; CHECK-NOT: srem +; CHECK: urem + %idxprom = sext i32 %rem to i64 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom + store i32 %i.01, i32* %arrayidx, align 4 + %inc = add nsw i32 %i.01, 1 + %cmp = icmp slt i32 %inc, 64 + br i1 %cmp, label %for.body, label %for.end + +for.end: ; preds = %for.body + ret void +} + +define void @test2(i32* %a, i32 %d) { +; CHECK-LABEL: @test2( +entry: + br label %for.body + +for.body: ; preds = %entry, %for.body + %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %mul = mul nsw i32 %i.01, 64 + %rem = srem i32 %mul, %d +; CHECK-NOT: urem + %idxprom = sext i32 %rem to i64 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom + store i32 %i.01, i32* %arrayidx, align 4 + %inc = add nsw i32 %i.01, 1 + %cmp = icmp slt i32 %inc, 64 + br i1 %cmp, label %for.body, label %for.end + +for.end: ; preds = %for.body + ret void +} + +define void @test3(i32* %a) { +; CHECK-LABEL: @test3( +entry: + br label %for.body + +for.body: ; preds = %entry, %for.body + %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %rem = srem i32 2048, %i.01 +; CHECK: urem +; CHECK-NOT: srem + %idxprom = sext i32 %rem to i64 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom + store i32 %i.01, i32* %arrayidx, align 4 + %inc = add nsw i32 %i.01, 1 + %cmp = icmp slt i32 %inc, 64 + br i1 %cmp, label %for.body, label %for.end + +for.end: ; preds = %for.body + ret void +} + +define void @test4(i32* %a) { +; CHECK-LABEL: @test4( +entry: + br label %for.body + +for.body: ; preds = %entry, %for.body + %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %mul = mul nsw i32 %i.01, 64 + %rem = srem i32 %mul, 8 +; CHECK: urem +; CHECK-NOT: srem + %idxprom = sext i32 %rem to i64 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom + store i32 %i.01, i32* %arrayidx, align 4 + %inc = add nsw i32 %i.01, 1 + %cmp = icmp slt i32 %inc, 64 + br i1 %cmp, label %for.body, label %for.end + +for.end: ; preds = %for.body + ret void +} + +define void @test5(i32* %a) { +; CHECK-LABEL: @test5( +entry: + br label %for.body + +for.body: ; preds = %entry, %for.body + %i.01 = phi i32 [ 0, %entry ], [ %inc, %for.body ] + %mul = mul nsw i32 %i.01, 64 + %rem = srem i32 %mul, 6 +; CHECK: urem +; CHECK-NOT: srem + %idxprom = sext i32 %rem to i64 + %arrayidx = getelementptr inbounds i32, i32* %a, i64 %idxprom + store i32 %i.01, i32* %arrayidx, align 4 + %inc = add nsw i32 %i.01, 1 + %cmp = icmp slt i32 %inc, 64 + br i1 %cmp, label %for.body, label %for.end + +for.end: ; preds = %for.body + ret void +} +