mirror of
https://github.com/c64scene-ar/llvm-6502.git
synced 2024-12-12 13:30:51 +00:00
remove constant terms
The delinearization is needed only to remove the non linearity induced by expressions involving multiplications of parameters and induction variables. There is no problem in dealing with constant times parameters, or constant times an induction variable. For this reason, the current patch discards all constant terms and multipliers before running the delinearization algorithm on the terms. The only thing remaining in the term expressions are parameters and multiply expressions of parameters: these simplified term expressions are passed to the array shape recognizer that will not recognize constant dimensions anymore: these will be recognized as different strides in parametric subscripts. The only important special case of a constant dimension is the size of elements. Instead of relying on the delinearization to infer the size of an element, compute the element size from the base address type. This is a much more precise way of computing the element size than before, as we would have mixed together the size of an element with the strides of the innermost dimension. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@209691 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
parent
50adf38080
commit
421b2c571c
@ -910,7 +910,8 @@ namespace llvm {
|
||||
const Constraint &CurConstraint) const;
|
||||
|
||||
bool tryDelinearize(const SCEV *SrcSCEV, const SCEV *DstSCEV,
|
||||
SmallVectorImpl<Subscript> &Pair) const;
|
||||
SmallVectorImpl<Subscript> &Pair,
|
||||
const SCEV *ElementSize) const;
|
||||
|
||||
public:
|
||||
static char ID; // Class identification, replacement for typeinfo
|
||||
|
@ -894,10 +894,14 @@ namespace llvm {
|
||||
/// indirect operand.
|
||||
bool hasOperand(const SCEV *S, const SCEV *Op) const;
|
||||
|
||||
/// Return the size of an element read or written by Inst.
|
||||
const SCEV *getElementSize(Instruction *Inst);
|
||||
|
||||
/// Compute the array dimensions Sizes from the set of Terms extracted from
|
||||
/// the memory access function of this SCEVAddRecExpr.
|
||||
void findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
|
||||
SmallVectorImpl<const SCEV *> &Sizes) const;
|
||||
SmallVectorImpl<const SCEV *> &Sizes,
|
||||
const SCEV *ElementSize) const;
|
||||
|
||||
bool runOnFunction(Function &F) override;
|
||||
void releaseMemory() override;
|
||||
|
@ -434,7 +434,8 @@ namespace llvm {
|
||||
/// Overall, we have: A[][n][m], and the access function: A[j+k][2i][5i].
|
||||
const SCEV *delinearize(ScalarEvolution &SE,
|
||||
SmallVectorImpl<const SCEV *> &Subscripts,
|
||||
SmallVectorImpl<const SCEV *> &Sizes) const;
|
||||
SmallVectorImpl<const SCEV *> &Sizes,
|
||||
const SCEV *ElementSize) const;
|
||||
};
|
||||
|
||||
//===--------------------------------------------------------------------===//
|
||||
|
@ -108,8 +108,8 @@ void Delinearization::print(raw_ostream &O, const Module *) const {
|
||||
O << "AddRec: " << *AR << "\n";
|
||||
|
||||
SmallVector<const SCEV *, 3> Subscripts, Sizes;
|
||||
const SCEV *Res = AR->delinearize(*SE, Subscripts, Sizes);
|
||||
if (Res == AR || Subscripts.size() == 0 || Sizes.size() == 0 ||
|
||||
const SCEV *Res = AR->delinearize(*SE, Subscripts, Sizes, SE->getElementSize(Inst));
|
||||
if (Subscripts.size() == 0 || Sizes.size() == 0 ||
|
||||
Subscripts.size() != Sizes.size()) {
|
||||
O << "failed to delinearize\n";
|
||||
continue;
|
||||
|
@ -3180,9 +3180,10 @@ void DependenceAnalysis::updateDirection(Dependence::DVEntry &Level,
|
||||
/// source and destination array references are recurrences on a nested loop,
|
||||
/// this function flattens the nested recurrences into separate recurrences
|
||||
/// for each loop level.
|
||||
bool
|
||||
DependenceAnalysis::tryDelinearize(const SCEV *SrcSCEV, const SCEV *DstSCEV,
|
||||
SmallVectorImpl<Subscript> &Pair) const {
|
||||
bool DependenceAnalysis::tryDelinearize(const SCEV *SrcSCEV,
|
||||
const SCEV *DstSCEV,
|
||||
SmallVectorImpl<Subscript> &Pair,
|
||||
const SCEV *ElementSize) const {
|
||||
const SCEVAddRecExpr *SrcAR = dyn_cast<SCEVAddRecExpr>(SrcSCEV);
|
||||
const SCEVAddRecExpr *DstAR = dyn_cast<SCEVAddRecExpr>(DstSCEV);
|
||||
if (!SrcAR || !DstAR || !SrcAR->isAffine() || !DstAR->isAffine())
|
||||
@ -3195,7 +3196,7 @@ DependenceAnalysis::tryDelinearize(const SCEV *SrcSCEV, const SCEV *DstSCEV,
|
||||
|
||||
// Second step: find subscript sizes.
|
||||
SmallVector<const SCEV *, 4> Sizes;
|
||||
SE->findArrayDimensions(Terms, Sizes);
|
||||
SE->findArrayDimensions(Terms, Sizes, ElementSize);
|
||||
|
||||
// Third step: compute the access functions for each subscript.
|
||||
SmallVector<const SCEV *, 4> SrcSubscripts, DstSubscripts;
|
||||
@ -3353,7 +3354,7 @@ Dependence *DependenceAnalysis::depends(Instruction *Src,
|
||||
}
|
||||
|
||||
if (Delinearize && Pairs == 1 && CommonLevels > 1 &&
|
||||
tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair)) {
|
||||
tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair, SE->getElementSize(Src))) {
|
||||
DEBUG(dbgs() << " delinerized GEP\n");
|
||||
Pairs = Pair.size();
|
||||
}
|
||||
@ -3777,7 +3778,7 @@ const SCEV *DependenceAnalysis::getSplitIteration(const Dependence *Dep,
|
||||
}
|
||||
|
||||
if (Delinearize && Pairs == 1 && CommonLevels > 1 &&
|
||||
tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair)) {
|
||||
tryDelinearize(Pair[0].Src, Pair[0].Dst, Pair, SE->getElementSize(Src))) {
|
||||
DEBUG(dbgs() << " delinerized GEP\n");
|
||||
Pairs = Pair.size();
|
||||
}
|
||||
|
@ -6944,7 +6944,7 @@ struct SCEVCollectTerms {
|
||||
: Terms(T) {}
|
||||
|
||||
bool follow(const SCEV *S) {
|
||||
if (isa<SCEVUnknown>(S) || isa<SCEVConstant>(S) || isa<SCEVMulExpr>(S)) {
|
||||
if (isa<SCEVUnknown>(S) || isa<SCEVMulExpr>(S)) {
|
||||
if (!containsUndefs(S))
|
||||
Terms.push_back(S);
|
||||
|
||||
@ -7356,13 +7356,46 @@ static inline int numberOfTerms(const SCEV *S) {
|
||||
return 1;
|
||||
}
|
||||
|
||||
static const SCEV *removeConstantFactors(ScalarEvolution &SE, const SCEV *T) {
|
||||
if (isa<SCEVConstant>(T))
|
||||
return nullptr;
|
||||
|
||||
if (isa<SCEVUnknown>(T))
|
||||
return T;
|
||||
|
||||
if (const SCEVMulExpr *M = dyn_cast<SCEVMulExpr>(T)) {
|
||||
SmallVector<const SCEV *, 2> Factors;
|
||||
for (const SCEV *Op : M->operands())
|
||||
if (!isa<SCEVConstant>(Op))
|
||||
Factors.push_back(Op);
|
||||
|
||||
return SE.getMulExpr(Factors);
|
||||
}
|
||||
|
||||
return T;
|
||||
}
|
||||
|
||||
/// Return the size of an element read or written by Inst.
|
||||
const SCEV *ScalarEvolution::getElementSize(Instruction *Inst) {
|
||||
Type *Ty;
|
||||
if (StoreInst *Store = dyn_cast<StoreInst>(Inst))
|
||||
Ty = Store->getValueOperand()->getType();
|
||||
else if (LoadInst *Load = dyn_cast<LoadInst>(Inst))
|
||||
Ty = Load->getPointerOperand()->getType();
|
||||
else
|
||||
return nullptr;
|
||||
|
||||
Type *ETy = getEffectiveSCEVType(PointerType::getUnqual(Ty));
|
||||
return getSizeOfExpr(ETy, Ty);
|
||||
}
|
||||
|
||||
/// Second step of delinearization: compute the array dimensions Sizes from the
|
||||
/// set of Terms extracted from the memory access function of this SCEVAddRec.
|
||||
void ScalarEvolution::findArrayDimensions(
|
||||
SmallVectorImpl<const SCEV *> &Terms,
|
||||
SmallVectorImpl<const SCEV *> &Sizes) const {
|
||||
void ScalarEvolution::findArrayDimensions(SmallVectorImpl<const SCEV *> &Terms,
|
||||
SmallVectorImpl<const SCEV *> &Sizes,
|
||||
const SCEV *ElementSize) const {
|
||||
|
||||
if (Terms.size() < 2)
|
||||
if (Terms.size() < 1)
|
||||
return;
|
||||
|
||||
// Early return when Terms do not contain parameters: we do not delinearize
|
||||
@ -7385,20 +7418,37 @@ void ScalarEvolution::findArrayDimensions(
|
||||
return numberOfTerms(LHS) > numberOfTerms(RHS);
|
||||
});
|
||||
|
||||
ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
|
||||
|
||||
// Divide all terms by the element size.
|
||||
for (const SCEV *&Term : Terms) {
|
||||
const SCEV *Q, *R;
|
||||
SCEVDivision::divide(SE, Term, ElementSize, &Q, &R);
|
||||
Term = Q;
|
||||
}
|
||||
|
||||
SmallVector<const SCEV *, 4> NewTerms;
|
||||
|
||||
// Remove constant factors.
|
||||
for (const SCEV *T : Terms)
|
||||
if (const SCEV *NewT = removeConstantFactors(SE, T))
|
||||
NewTerms.push_back(NewT);
|
||||
|
||||
DEBUG({
|
||||
dbgs() << "Terms after sorting:\n";
|
||||
for (const SCEV *T : Terms)
|
||||
for (const SCEV *T : NewTerms)
|
||||
dbgs() << *T << "\n";
|
||||
});
|
||||
|
||||
ScalarEvolution &SE = *const_cast<ScalarEvolution *>(this);
|
||||
bool Res = findArrayDimensionsRec(SE, Terms, Sizes);
|
||||
|
||||
if (!Res) {
|
||||
if (NewTerms.empty() ||
|
||||
!findArrayDimensionsRec(SE, NewTerms, Sizes)) {
|
||||
Sizes.clear();
|
||||
return;
|
||||
}
|
||||
|
||||
// The last element to be pushed into Sizes is the size of an element.
|
||||
Sizes.push_back(ElementSize);
|
||||
|
||||
DEBUG({
|
||||
dbgs() << "Sizes:\n";
|
||||
for (const SCEV *S : Sizes)
|
||||
@ -7433,9 +7483,14 @@ const SCEV *SCEVAddRecExpr::computeAccessFunctions(
|
||||
|
||||
Res = Q;
|
||||
|
||||
// Do not record the last subscript corresponding to the size of elements in
|
||||
// the array.
|
||||
if (i == Last) {
|
||||
// Do not record the last subscript corresponding to the size of elements
|
||||
// in the array.
|
||||
|
||||
// Bail out if the remainder is too complex.
|
||||
if (isa<SCEVAddRecExpr>(R))
|
||||
return nullptr;
|
||||
|
||||
Remainder = R;
|
||||
continue;
|
||||
}
|
||||
@ -7507,10 +7562,9 @@ const SCEV *SCEVAddRecExpr::computeAccessFunctions(
|
||||
/// asking for the SCEV of the memory access with respect to all enclosing
|
||||
/// loops, calling SCEV->delinearize on that and printing the results.
|
||||
|
||||
const SCEV *
|
||||
SCEVAddRecExpr::delinearize(ScalarEvolution &SE,
|
||||
SmallVectorImpl<const SCEV *> &Subscripts,
|
||||
SmallVectorImpl<const SCEV *> &Sizes) const {
|
||||
const SCEV *SCEVAddRecExpr::delinearize(
|
||||
ScalarEvolution &SE, SmallVectorImpl<const SCEV *> &Subscripts,
|
||||
SmallVectorImpl<const SCEV *> &Sizes, const SCEV *ElementSize) const {
|
||||
// First step: collect parametric terms.
|
||||
SmallVector<const SCEV *, 4> Terms;
|
||||
collectParametricTerms(SE, Terms);
|
||||
@ -7519,7 +7573,7 @@ SCEVAddRecExpr::delinearize(ScalarEvolution &SE,
|
||||
return nullptr;
|
||||
|
||||
// Second step: find subscript sizes.
|
||||
SE.findArrayDimensions(Terms, Sizes);
|
||||
SE.findArrayDimensions(Terms, Sizes, ElementSize);
|
||||
|
||||
if (Sizes.empty())
|
||||
return nullptr;
|
||||
|
@ -0,0 +1,45 @@
|
||||
; RUN: opt < %s -analyze -delinearize | FileCheck %s
|
||||
|
||||
; Derived from the following code:
|
||||
;
|
||||
; void foo(long n, long m, long b, double A[n][m]) {
|
||||
; for (long i = 0; i < n; i++)
|
||||
; for (long j = 0; j < m; j++)
|
||||
; A[2i+b][2j] = 1.0;
|
||||
; }
|
||||
|
||||
; AddRec: {{((%m * %b * sizeof(double)) + %A),+,(2 * %m * sizeof(double))}<%for.i>,+,(2 * sizeof(double))}<%for.j>
|
||||
; CHECK: Base offset: %A
|
||||
; CHECK: ArrayDecl[UnknownSize][%m] with elements of sizeof(double) bytes.
|
||||
; CHECK: ArrayRef[{%b,+,2}<%for.i>][{0,+,2}<%for.j>]
|
||||
|
||||
|
||||
define void @foo(i64 %n, i64 %m, i64 %b, double* %A) {
|
||||
entry:
|
||||
br label %for.i
|
||||
|
||||
for.i:
|
||||
%i = phi i64 [ 0, %entry ], [ %i.inc, %for.i.inc ]
|
||||
%outerdim = mul nsw i64 %i, 2
|
||||
%outerdim2 = add nsw i64 %outerdim, %b
|
||||
%tmp = mul nsw i64 %outerdim2, %m
|
||||
br label %for.j
|
||||
|
||||
for.j:
|
||||
%j = phi i64 [ 0, %for.i ], [ %j.inc, %for.j ]
|
||||
%prodj = mul i64 %j, 2
|
||||
%vlaarrayidx.sum = add i64 %prodj, %tmp
|
||||
%arrayidx = getelementptr inbounds double* %A, i64 %vlaarrayidx.sum
|
||||
store double 1.0, double* %arrayidx
|
||||
%j.inc = add nsw i64 %j, 1
|
||||
%j.exitcond = icmp eq i64 %j.inc, %m
|
||||
br i1 %j.exitcond, label %for.i.inc, label %for.j
|
||||
|
||||
for.i.inc:
|
||||
%i.inc = add nsw i64 %i, 1
|
||||
%i.exitcond = icmp eq i64 %i.inc, %n
|
||||
br i1 %i.exitcond, label %end, label %for.i
|
||||
|
||||
end:
|
||||
ret void
|
||||
}
|
@ -269,10 +269,10 @@ entry:
|
||||
; CHECK: da analyze - none!
|
||||
|
||||
; DELIN: 'Dependence Analysis' for function 'gcd4'
|
||||
; DELIN: da analyze - none!
|
||||
; DELIN: da analyze - output [* *]!
|
||||
; DELIN: da analyze - none!
|
||||
; DELIN: da analyze - confused!
|
||||
; DELIN: da analyze - none!
|
||||
; DELIN: da analyze - input [* *]!
|
||||
; DELIN: da analyze - confused!
|
||||
; DELIN: da analyze - none!
|
||||
|
||||
@ -339,10 +339,10 @@ entry:
|
||||
; CHECK: da analyze - none!
|
||||
|
||||
; DELIN: 'Dependence Analysis' for function 'gcd5'
|
||||
; DELIN: da analyze - none!
|
||||
; DELIN: da analyze - output [* *]!
|
||||
; DELIN: da analyze - flow [<> *]!
|
||||
; DELIN: da analyze - confused!
|
||||
; DELIN: da analyze - none!
|
||||
; DELIN: da analyze - input [* *]!
|
||||
; DELIN: da analyze - confused!
|
||||
; DELIN: da analyze - none!
|
||||
|
||||
@ -410,10 +410,10 @@ entry:
|
||||
; CHECK: da analyze - output [* *]!
|
||||
|
||||
; DELIN: 'Dependence Analysis' for function 'gcd6'
|
||||
; DELIN: da analyze - output [* *]!
|
||||
; DELIN: da analyze - none!
|
||||
; DELIN: da analyze - flow [=> =>|<]!
|
||||
; DELIN: da analyze - confused!
|
||||
; DELIN: da analyze - none!
|
||||
; DELIN: da analyze - input [* *]!
|
||||
; DELIN: da analyze - confused!
|
||||
; DELIN: da analyze - output [* *]!
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user