mirror of
https://github.com/RPCS3/llvm.git
synced 2024-11-30 07:00:57 +00:00
Teach ScalarEvolution how to reason about no-wrap flags on loops
where the induction variable has a non-unit stride, such as {0,+,2}, and there are expressions such as {1,+,2} inside the loop formed with or or add nsw operators. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@82151 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
f9ca50e3dc
commit
1f96e67f78
@ -253,7 +253,8 @@ namespace llvm {
|
||||
/// CouldNotCompute if an intermediate computation overflows.
|
||||
const SCEV *getBECount(const SCEV *Start,
|
||||
const SCEV *End,
|
||||
const SCEV *Step);
|
||||
const SCEV *Step,
|
||||
bool NoWrap);
|
||||
|
||||
/// getBackedgeTakenInfo - Return the BackedgeTakenInfo for the given
|
||||
/// loop, lazily computing new values if the loop hasn't been analyzed
|
||||
|
@ -2972,8 +2972,20 @@ const SCEV *ScalarEvolution::createSCEV(Value *V) {
|
||||
const SCEV *LHS = getSCEV(U->getOperand(0));
|
||||
const APInt &CIVal = CI->getValue();
|
||||
if (GetMinTrailingZeros(LHS) >=
|
||||
(CIVal.getBitWidth() - CIVal.countLeadingZeros()))
|
||||
return getAddExpr(LHS, getSCEV(U->getOperand(1)));
|
||||
(CIVal.getBitWidth() - CIVal.countLeadingZeros())) {
|
||||
// Build a plain add SCEV.
|
||||
const SCEV *S = getAddExpr(LHS, getSCEV(CI));
|
||||
// If the LHS of the add was an addrec and it has no-wrap flags,
|
||||
// transfer the no-wrap flags, since an or won't introduce a wrap.
|
||||
if (const SCEVAddRecExpr *NewAR = dyn_cast<SCEVAddRecExpr>(S)) {
|
||||
const SCEVAddRecExpr *OldAR = cast<SCEVAddRecExpr>(LHS);
|
||||
if (OldAR->hasNoUnsignedWrap())
|
||||
const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoUnsignedWrap(true);
|
||||
if (OldAR->hasNoSignedWrap())
|
||||
const_cast<SCEVAddRecExpr *>(NewAR)->setHasNoSignedWrap(true);
|
||||
}
|
||||
return S;
|
||||
}
|
||||
}
|
||||
break;
|
||||
case Instruction::Xor:
|
||||
@ -4795,7 +4807,8 @@ ScalarEvolution::isImpliedCondOperandsHelper(ICmpInst::Predicate Pred,
|
||||
/// CouldNotCompute if an intermediate computation overflows.
|
||||
const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
|
||||
const SCEV *End,
|
||||
const SCEV *Step) {
|
||||
const SCEV *Step,
|
||||
bool NoWrap) {
|
||||
const Type *Ty = Start->getType();
|
||||
const SCEV *NegOne = getIntegerSCEV(-1, Ty);
|
||||
const SCEV *Diff = getMinusSCEV(End, Start);
|
||||
@ -4805,15 +4818,17 @@ const SCEV *ScalarEvolution::getBECount(const SCEV *Start,
|
||||
// the division will effectively round up.
|
||||
const SCEV *Add = getAddExpr(Diff, RoundUp);
|
||||
|
||||
// Check Add for unsigned overflow.
|
||||
// TODO: More sophisticated things could be done here.
|
||||
const Type *WideTy = IntegerType::get(getContext(),
|
||||
getTypeSizeInBits(Ty) + 1);
|
||||
const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
|
||||
const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
|
||||
const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
|
||||
if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
|
||||
return getCouldNotCompute();
|
||||
if (!NoWrap) {
|
||||
// Check Add for unsigned overflow.
|
||||
// TODO: More sophisticated things could be done here.
|
||||
const Type *WideTy = IntegerType::get(getContext(),
|
||||
getTypeSizeInBits(Ty) + 1);
|
||||
const SCEV *EDiff = getZeroExtendExpr(Diff, WideTy);
|
||||
const SCEV *ERoundUp = getZeroExtendExpr(RoundUp, WideTy);
|
||||
const SCEV *OperandExtendedAdd = getAddExpr(EDiff, ERoundUp);
|
||||
if (getZeroExtendExpr(Add, WideTy) != OperandExtendedAdd)
|
||||
return getCouldNotCompute();
|
||||
}
|
||||
|
||||
return getUDivExpr(Add, Step);
|
||||
}
|
||||
@ -4831,6 +4846,10 @@ ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
|
||||
if (!AddRec || AddRec->getLoop() != L)
|
||||
return getCouldNotCompute();
|
||||
|
||||
// Check to see if we have a flag which makes analysis easy.
|
||||
bool NoWrap = isSigned ? AddRec->hasNoSignedWrap() :
|
||||
AddRec->hasNoUnsignedWrap();
|
||||
|
||||
if (AddRec->isAffine()) {
|
||||
// FORNOW: We only support unit strides.
|
||||
unsigned BitWidth = getTypeSizeInBits(AddRec->getType());
|
||||
@ -4843,7 +4862,10 @@ ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
|
||||
if (CStep->isOne()) {
|
||||
// With unit stride, the iteration never steps past the limit value.
|
||||
} else if (CStep->getValue()->getValue().isStrictlyPositive()) {
|
||||
if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) {
|
||||
if (NoWrap) {
|
||||
// We know the iteration won't step past the maximum value for its type.
|
||||
;
|
||||
} else if (const SCEVConstant *CLimit = dyn_cast<SCEVConstant>(RHS)) {
|
||||
// Test whether a positive iteration iteration can step past the limit
|
||||
// value and past the maximum value for its type in a single step.
|
||||
if (isSigned) {
|
||||
@ -4896,11 +4918,11 @@ ScalarEvolution::HowManyLessThans(const SCEV *LHS, const SCEV *RHS,
|
||||
|
||||
// Finally, we subtract these two values and divide, rounding up, to get
|
||||
// the number of times the backedge is executed.
|
||||
const SCEV *BECount = getBECount(Start, End, Step);
|
||||
const SCEV *BECount = getBECount(Start, End, Step, NoWrap);
|
||||
|
||||
// The maximum backedge count is similar, except using the minimum start
|
||||
// value and the maximum end value.
|
||||
const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step);
|
||||
const SCEV *MaxBECount = getBECount(MinStart, MaxEnd, Step, NoWrap);
|
||||
|
||||
return BackedgeTakenInfo(BECount, MaxBECount);
|
||||
}
|
||||
|
76
test/Analysis/ScalarEvolution/nsw-offset.ll
Normal file
76
test/Analysis/ScalarEvolution/nsw-offset.ll
Normal file
@ -0,0 +1,76 @@
|
||||
; RUN: opt < %s -S -analyze -scalar-evolution -disable-output | FileCheck %s
|
||||
|
||||
; ScalarEvolution should be able to fold away the sign-extensions
|
||||
; on this loop with a primary induction variable incremented with
|
||||
; a nsw add of 2.
|
||||
|
||||
target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-f80:128:128"
|
||||
|
||||
define void @foo(i32 %n, double* nocapture %d, double* nocapture %q) nounwind {
|
||||
entry:
|
||||
%0 = icmp sgt i32 %n, 0 ; <i1> [#uses=1]
|
||||
br i1 %0, label %bb.nph, label %return
|
||||
|
||||
bb.nph: ; preds = %entry
|
||||
br label %bb
|
||||
|
||||
bb: ; preds = %bb.nph, %bb1
|
||||
%i.01 = phi i32 [ %16, %bb1 ], [ 0, %bb.nph ] ; <i32> [#uses=5]
|
||||
|
||||
; CHECK: %1 = sext i32 %i.01 to i64
|
||||
; CHECK: --> {0,+,2}<bb>
|
||||
%1 = sext i32 %i.01 to i64 ; <i64> [#uses=1]
|
||||
|
||||
; CHECK: %2 = getelementptr inbounds double* %d, i64 %1
|
||||
; CHECK: --> {%d,+,16}<bb>
|
||||
%2 = getelementptr inbounds double* %d, i64 %1 ; <double*> [#uses=1]
|
||||
|
||||
%3 = load double* %2, align 8 ; <double> [#uses=1]
|
||||
%4 = sext i32 %i.01 to i64 ; <i64> [#uses=1]
|
||||
%5 = getelementptr inbounds double* %q, i64 %4 ; <double*> [#uses=1]
|
||||
%6 = load double* %5, align 8 ; <double> [#uses=1]
|
||||
%7 = or i32 %i.01, 1 ; <i32> [#uses=1]
|
||||
|
||||
; CHECK: %8 = sext i32 %7 to i64
|
||||
; CHECK: --> {1,+,2}<bb>
|
||||
%8 = sext i32 %7 to i64 ; <i64> [#uses=1]
|
||||
|
||||
; CHECK: %9 = getelementptr inbounds double* %q, i64 %8
|
||||
; CHECK: {(8 + %q),+,16}<bb>
|
||||
%9 = getelementptr inbounds double* %q, i64 %8 ; <double*> [#uses=1]
|
||||
|
||||
; Artificially repeat the above three instructions, this time using
|
||||
; add nsw instead of or.
|
||||
%t7 = add nsw i32 %i.01, 1 ; <i32> [#uses=1]
|
||||
|
||||
; CHECK: %t8 = sext i32 %t7 to i64
|
||||
; CHECK: --> {1,+,2}<bb>
|
||||
%t8 = sext i32 %t7 to i64 ; <i64> [#uses=1]
|
||||
|
||||
; CHECK: %t9 = getelementptr inbounds double* %q, i64 %t8
|
||||
; CHECK: {(8 + %q),+,16}<bb>
|
||||
%t9 = getelementptr inbounds double* %q, i64 %t8 ; <double*> [#uses=1]
|
||||
|
||||
%10 = load double* %9, align 8 ; <double> [#uses=1]
|
||||
%11 = fadd double %6, %10 ; <double> [#uses=1]
|
||||
%12 = fadd double %11, 3.200000e+00 ; <double> [#uses=1]
|
||||
%13 = fmul double %3, %12 ; <double> [#uses=1]
|
||||
%14 = sext i32 %i.01 to i64 ; <i64> [#uses=1]
|
||||
%15 = getelementptr inbounds double* %d, i64 %14 ; <double*> [#uses=1]
|
||||
store double %13, double* %15, align 8
|
||||
%16 = add nsw i32 %i.01, 2 ; <i32> [#uses=2]
|
||||
br label %bb1
|
||||
|
||||
bb1: ; preds = %bb
|
||||
%17 = icmp slt i32 %16, %n ; <i1> [#uses=1]
|
||||
br i1 %17, label %bb, label %bb1.return_crit_edge
|
||||
|
||||
bb1.return_crit_edge: ; preds = %bb1
|
||||
br label %return
|
||||
|
||||
return: ; preds = %bb1.return_crit_edge, %entry
|
||||
ret void
|
||||
}
|
||||
|
||||
; CHECK: Loop bb: backedge-taken count is ((-1 + %n) /u 2)
|
||||
; CHECK: Loop bb: max backedge-taken count is 1073741823
|
Loading…
Reference in New Issue
Block a user