mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-08-17 21:29:20 +00:00
Handle decrementing loops properly. Fixes PR1533.
Always pass the constant as the second parameter to HowManyLessThans. Remove obsolete "isSigned" parameter. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@39893 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
f7b71c64cf
commit
eefdebe002
@ -475,10 +475,8 @@ namespace llvm {
|
|||||||
/// looking at this is that it returns the first iteration number where the
|
/// looking at this is that it returns the first iteration number where the
|
||||||
/// value is not in the condition, thus computing the exit count. If the
|
/// value is not in the condition, thus computing the exit count. If the
|
||||||
/// iteration count can't be computed, an instance of SCEVCouldNotCompute is
|
/// iteration count can't be computed, an instance of SCEVCouldNotCompute is
|
||||||
/// returned. The isSigned parameter indicates whether the ConstantRange
|
/// returned.
|
||||||
/// should be treated as signed or unsigned.
|
SCEVHandle getNumIterationsInRange(ConstantRange Range) const;
|
||||||
SCEVHandle getNumIterationsInRange(ConstantRange Range,
|
|
||||||
bool isSigned) const;
|
|
||||||
|
|
||||||
SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
|
SCEVHandle replaceSymbolicValuesWithConcrete(const SCEVHandle &Sym,
|
||||||
const SCEVHandle &Conc) const;
|
const SCEVHandle &Conc) const;
|
||||||
|
@ -1671,8 +1671,7 @@ SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
|
|||||||
ConstantRange CompRange(
|
ConstantRange CompRange(
|
||||||
ICmpInst::makeConstantRange(Cond, CompVal->getValue()));
|
ICmpInst::makeConstantRange(Cond, CompVal->getValue()));
|
||||||
|
|
||||||
SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange,
|
SCEVHandle Ret = AddRec->getNumIterationsInRange(CompRange);
|
||||||
false /*Always treat as unsigned range*/);
|
|
||||||
if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
|
if (!isa<SCEVCouldNotCompute>(Ret)) return Ret;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -1696,7 +1695,8 @@ SCEVHandle ScalarEvolutionsImpl::ComputeIterationCount(const Loop *L) {
|
|||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case ICmpInst::ICMP_SGT: {
|
case ICmpInst::ICMP_SGT: {
|
||||||
SCEVHandle TC = HowManyLessThans(RHS, LHS, L);
|
SCEVHandle TC = HowManyLessThans(SCEV::getNegativeSCEV(LHS),
|
||||||
|
SCEV::getNegativeSCEV(RHS), L);
|
||||||
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
|
if (!isa<SCEVCouldNotCompute>(TC)) return TC;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
@ -2406,8 +2406,7 @@ HowManyLessThans(SCEV *LHS, SCEV *RHS, const Loop *L) {
|
|||||||
/// this is that it returns the first iteration number where the value is not in
|
/// this is that it returns the first iteration number where the value is not in
|
||||||
/// the condition, thus computing the exit count. If the iteration count can't
|
/// the condition, thus computing the exit count. If the iteration count can't
|
||||||
/// be computed, an instance of SCEVCouldNotCompute is returned.
|
/// be computed, an instance of SCEVCouldNotCompute is returned.
|
||||||
SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
|
SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range) const {
|
||||||
bool isSigned) const {
|
|
||||||
if (Range.isFullSet()) // Infinite loop.
|
if (Range.isFullSet()) // Infinite loop.
|
||||||
return new SCEVCouldNotCompute();
|
return new SCEVCouldNotCompute();
|
||||||
|
|
||||||
@ -2419,7 +2418,7 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
|
|||||||
SCEVHandle Shifted = SCEVAddRecExpr::get(Operands, getLoop());
|
SCEVHandle Shifted = SCEVAddRecExpr::get(Operands, getLoop());
|
||||||
if (SCEVAddRecExpr *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
|
if (SCEVAddRecExpr *ShiftedAddRec = dyn_cast<SCEVAddRecExpr>(Shifted))
|
||||||
return ShiftedAddRec->getNumIterationsInRange(
|
return ShiftedAddRec->getNumIterationsInRange(
|
||||||
Range.subtract(SC->getValue()->getValue()),isSigned);
|
Range.subtract(SC->getValue()->getValue()));
|
||||||
// This is strange and shouldn't happen.
|
// This is strange and shouldn't happen.
|
||||||
return new SCEVCouldNotCompute();
|
return new SCEVCouldNotCompute();
|
||||||
}
|
}
|
||||||
@ -2443,17 +2442,16 @@ SCEVHandle SCEVAddRecExpr::getNumIterationsInRange(ConstantRange Range,
|
|||||||
// If this is an affine expression then we have this situation:
|
// If this is an affine expression then we have this situation:
|
||||||
// Solve {0,+,A} in Range === Ax in Range
|
// Solve {0,+,A} in Range === Ax in Range
|
||||||
|
|
||||||
// Since we know that zero is in the range, we know that the upper value of
|
// We know that zero is in the range. If A is positive then we know that
|
||||||
// the range must be the first possible exit value. Also note that we
|
// the upper value of the range must be the first possible exit value.
|
||||||
// already checked for a full range.
|
// If A is negative then the lower of the range is the last possible loop
|
||||||
const APInt &Upper = Range.getUpper();
|
// value. Also note that we already checked for a full range.
|
||||||
APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
|
|
||||||
APInt One(getBitWidth(),1);
|
APInt One(getBitWidth(),1);
|
||||||
|
APInt A = cast<SCEVConstant>(getOperand(1))->getValue()->getValue();
|
||||||
|
APInt End = A.sge(One) ? (Range.getUpper() - One) : Range.getLower();
|
||||||
|
|
||||||
// The exit value should be (Upper+A-1)/A.
|
// The exit value should be (End+A)/A.
|
||||||
APInt ExitVal(Upper);
|
APInt ExitVal = (End + A).sdiv(A);
|
||||||
if (A != One)
|
|
||||||
ExitVal = (Upper + A - One).sdiv(A);
|
|
||||||
ConstantInt *ExitValue = ConstantInt::get(ExitVal);
|
ConstantInt *ExitValue = ConstantInt::get(ExitVal);
|
||||||
|
|
||||||
// Evaluate at the exit value. If we really did fall out of the valid
|
// Evaluate at the exit value. If we really did fall out of the valid
|
||||||
|
20
test/Analysis/ScalarEvolution/2007-07-15-NegativeStride.ll
Normal file
20
test/Analysis/ScalarEvolution/2007-07-15-NegativeStride.ll
Normal file
@ -0,0 +1,20 @@
|
|||||||
|
; RUN: llvm-as < %s | opt -analyze -scalar-evolution 2>&1 | grep "Loop bb: 100 iterations"
|
||||||
|
; PR1533
|
||||||
|
|
||||||
|
@array = weak global [101 x i32] zeroinitializer, align 32 ; <[100 x i32]*> [#uses=1]
|
||||||
|
|
||||||
|
define void @loop(i32 %x) {
|
||||||
|
entry:
|
||||||
|
br label %bb
|
||||||
|
|
||||||
|
bb: ; preds = %bb, %entry
|
||||||
|
%i.01.0 = phi i32 [ 100, %entry ], [ %tmp4, %bb ] ; <i32> [#uses=2]
|
||||||
|
%tmp1 = getelementptr [101 x i32]* @array, i32 0, i32 %i.01.0 ; <i32*> [#uses=1]
|
||||||
|
store i32 %x, i32* %tmp1
|
||||||
|
%tmp4 = add i32 %i.01.0, -1 ; <i32> [#uses=2]
|
||||||
|
%tmp7 = icmp sgt i32 %tmp4, -1 ; <i1> [#uses=1]
|
||||||
|
br i1 %tmp7, label %bb, label %return
|
||||||
|
|
||||||
|
return: ; preds = %bb
|
||||||
|
ret void
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user