fix a corner case in delinearization

handle special cases Step==1, Step==-1, GCD==1, and GCD==-1

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@201868 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Sebastian Pop 2014-02-21 18:15:15 +00:00
parent f9cb030376
commit 24d33a55a1

View File

@ -7255,46 +7255,37 @@ SCEVAddRecExpr::delinearize(ScalarEvolution &SE,
DEBUG(dbgs() << "(delinearize: " << *this << "\n"); DEBUG(dbgs() << "(delinearize: " << *this << "\n");
// Currently we fail to delinearize when the stride of this SCEV is 1. We // When the stride of this SCEV is 1, do not compute the GCD: the size of this
// could decide to not fail in this case: we could just return 1 for the size // subscript is 1, and this same SCEV for the access function.
// of the subscript, and this same SCEV for the access function. const SCEV *Remainder = Zero;
if (Step == One) { const SCEV *GCD = One;
DEBUG(dbgs() << "failed to delinearize " << *this << "\n)\n");
return this;
}
// Find the GCD and Remainder of the Start and Step coefficients of this SCEV. // Find the GCD and Remainder of the Start and Step coefficients of this SCEV.
const SCEV *Remainder = NULL; if (Step != One && !Step->isAllOnesValue())
const SCEV *GCD = SCEVGCD::findGCD(SE, Start, Step, &Remainder); GCD = SCEVGCD::findGCD(SE, Start, Step, &Remainder);
DEBUG(dbgs() << "GCD: " << *GCD << "\n"); DEBUG(dbgs() << "GCD: " << *GCD << "\n");
DEBUG(dbgs() << "Remainder: " << *Remainder << "\n"); DEBUG(dbgs() << "Remainder: " << *Remainder << "\n");
// Same remark as above: we currently fail the delinearization, although we const SCEV *Quotient = Start;
// can very well handle this special case. if (GCD != One && !GCD->isAllOnesValue())
if (GCD == One) { // As findGCD computed Remainder, GCD divides "Start - Remainder." The
DEBUG(dbgs() << "failed to delinearize " << *this << "\n)\n"); // Quotient is then this SCEV without Remainder, scaled down by the GCD. The
return this; // Quotient is what will be used in the next subscript delinearization.
} Quotient = SCEVDivision::divide(SE, SE.getMinusSCEV(Start, Remainder), GCD);
// As findGCD computed Remainder, GCD divides "Start - Remainder." The
// Quotient is then this SCEV without Remainder, scaled down by the GCD. The
// Quotient is what will be used in the next subscript delinearization.
const SCEV *Quotient =
SCEVDivision::divide(SE, SE.getMinusSCEV(Start, Remainder), GCD);
DEBUG(dbgs() << "Quotient: " << *Quotient << "\n"); DEBUG(dbgs() << "Quotient: " << *Quotient << "\n");
const SCEV *Rem; const SCEV *Rem = Quotient;
if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Quotient)) if (const SCEVAddRecExpr *AR = dyn_cast<SCEVAddRecExpr>(Quotient))
// Recursively call delinearize on the Quotient until there are no more // Recursively call delinearize on the Quotient until there are no more
// multiples that can be recognized. // multiples that can be recognized.
Rem = AR->delinearize(SE, Subscripts, Sizes); Rem = AR->delinearize(SE, Subscripts, Sizes);
else
Rem = Quotient;
// Scale up the canonical induction variable IV by whatever remains from the // Scale up the canonical induction variable IV by whatever remains from the
// Step after division by the GCD: the GCD is the size of all the sub-array. // Step after division by the GCD: the GCD is the size of all the sub-array.
if (Step != GCD) { if (Step != One && !Step->isAllOnesValue() && GCD != One &&
!GCD->isAllOnesValue() && Step != GCD) {
Step = SCEVDivision::divide(SE, Step, GCD); Step = SCEVDivision::divide(SE, Step, GCD);
IV = SE.getMulExpr(IV, Step); IV = SE.getMulExpr(IV, Step);
} }