1 //===- LoopDependenceAnalysis.cpp - LDA Implementation ----------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This is the (beginning) of an implementation of a loop dependence analysis
11 // framework, which is used to detect dependences in memory accesses in loops.
13 // Please note that this is work in progress and the interface is subject to
16 // TODO: adapt as implementation progresses.
18 // TODO: document lingo (pair, subscript, index)
20 //===----------------------------------------------------------------------===//
22 #define DEBUG_TYPE "lda"
23 #include "llvm/ADT/DenseSet.h"
24 #include "llvm/ADT/Statistic.h"
25 #include "llvm/Analysis/AliasAnalysis.h"
26 #include "llvm/Analysis/LoopDependenceAnalysis.h"
27 #include "llvm/Analysis/LoopPass.h"
28 #include "llvm/Analysis/ScalarEvolution.h"
29 #include "llvm/Analysis/ScalarEvolutionExpressions.h"
30 #include "llvm/Instructions.h"
31 #include "llvm/Operator.h"
32 #include "llvm/Support/Allocator.h"
33 #include "llvm/Support/Debug.h"
34 #include "llvm/Support/ErrorHandling.h"
35 #include "llvm/Support/raw_ostream.h"
36 #include "llvm/Target/TargetData.h"
39 STATISTIC(NumAnswered
, "Number of dependence queries answered");
40 STATISTIC(NumAnalysed
, "Number of distinct dependence pairs analysed");
41 STATISTIC(NumDependent
, "Number of pairs with dependent accesses");
42 STATISTIC(NumIndependent
, "Number of pairs with independent accesses");
43 STATISTIC(NumUnknown
, "Number of pairs with unknown accesses");
45 LoopPass
*llvm::createLoopDependenceAnalysisPass() {
46 return new LoopDependenceAnalysis();
49 static RegisterPass
<LoopDependenceAnalysis
>
50 R("lda", "Loop Dependence Analysis", false, true);
51 char LoopDependenceAnalysis::ID
= 0;
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 static inline bool IsMemRefInstr(const Value
*V
) {
58 const Instruction
*I
= dyn_cast
<const Instruction
>(V
);
59 return I
&& (I
->mayReadFromMemory() || I
->mayWriteToMemory());
62 static void GetMemRefInstrs(const Loop
*L
,
63 SmallVectorImpl
<Instruction
*> &Memrefs
) {
64 for (Loop::block_iterator b
= L
->block_begin(), be
= L
->block_end();
66 for (BasicBlock::iterator i
= (*b
)->begin(), ie
= (*b
)->end();
72 static bool IsLoadOrStoreInst(Value
*I
) {
73 return isa
<LoadInst
>(I
) || isa
<StoreInst
>(I
);
76 static Value
*GetPointerOperand(Value
*I
) {
77 if (LoadInst
*i
= dyn_cast
<LoadInst
>(I
))
78 return i
->getPointerOperand();
79 if (StoreInst
*i
= dyn_cast
<StoreInst
>(I
))
80 return i
->getPointerOperand();
81 llvm_unreachable("Value is no load or store instruction!");
86 static AliasAnalysis::AliasResult
UnderlyingObjectsAlias(AliasAnalysis
*AA
,
89 const Value
*aObj
= A
->getUnderlyingObject();
90 const Value
*bObj
= B
->getUnderlyingObject();
91 return AA
->alias(aObj
, AA
->getTypeStoreSize(aObj
->getType()),
92 bObj
, AA
->getTypeStoreSize(bObj
->getType()));
95 static inline const SCEV
*GetZeroSCEV(ScalarEvolution
*SE
) {
96 return SE
->getConstant(Type::getInt32Ty(SE
->getContext()), 0L);
99 //===----------------------------------------------------------------------===//
100 // Dependence Testing
101 //===----------------------------------------------------------------------===//
103 bool LoopDependenceAnalysis::isDependencePair(const Value
*A
,
104 const Value
*B
) const {
105 return IsMemRefInstr(A
) &&
107 (cast
<const Instruction
>(A
)->mayWriteToMemory() ||
108 cast
<const Instruction
>(B
)->mayWriteToMemory());
111 bool LoopDependenceAnalysis::findOrInsertDependencePair(Value
*A
,
113 DependencePair
*&P
) {
119 P
= Pairs
.FindNodeOrInsertPos(id
, insertPos
);
122 P
= PairAllocator
.Allocate
<DependencePair
>();
123 new (P
) DependencePair(id
, A
, B
);
124 Pairs
.InsertNode(P
, insertPos
);
128 void LoopDependenceAnalysis::getLoops(const SCEV
*S
,
129 DenseSet
<const Loop
*>* Loops
) const {
130 // Refactor this into an SCEVVisitor, if efficiency becomes a concern.
131 for (const Loop
*L
= this->L
; L
!= 0; L
= L
->getParentLoop())
132 if (!S
->isLoopInvariant(L
))
136 bool LoopDependenceAnalysis::isLoopInvariant(const SCEV
*S
) const {
137 DenseSet
<const Loop
*> loops
;
139 return loops
.empty();
142 bool LoopDependenceAnalysis::isAffine(const SCEV
*S
) const {
143 const SCEVAddRecExpr
*rec
= dyn_cast
<SCEVAddRecExpr
>(S
);
144 return isLoopInvariant(S
) || (rec
&& rec
->isAffine());
147 bool LoopDependenceAnalysis::isZIVPair(const SCEV
*A
, const SCEV
*B
) const {
148 return isLoopInvariant(A
) && isLoopInvariant(B
);
151 bool LoopDependenceAnalysis::isSIVPair(const SCEV
*A
, const SCEV
*B
) const {
152 DenseSet
<const Loop
*> loops
;
155 return loops
.size() == 1;
158 LoopDependenceAnalysis::DependenceResult
159 LoopDependenceAnalysis::analyseZIV(const SCEV
*A
,
161 Subscript
*S
) const {
162 assert(isZIVPair(A
, B
) && "Attempted to ZIV-test non-ZIV SCEVs!");
163 return A
== B
? Dependent
: Independent
;
166 LoopDependenceAnalysis::DependenceResult
167 LoopDependenceAnalysis::analyseSIV(const SCEV
*A
,
169 Subscript
*S
) const {
170 return Unknown
; // TODO: Implement.
173 LoopDependenceAnalysis::DependenceResult
174 LoopDependenceAnalysis::analyseMIV(const SCEV
*A
,
176 Subscript
*S
) const {
177 return Unknown
; // TODO: Implement.
180 LoopDependenceAnalysis::DependenceResult
181 LoopDependenceAnalysis::analyseSubscript(const SCEV
*A
,
183 Subscript
*S
) const {
184 DEBUG(errs() << " Testing subscript: " << *A
<< ", " << *B
<< "\n");
187 DEBUG(errs() << " -> [D] same SCEV\n");
191 if (!isAffine(A
) || !isAffine(B
)) {
192 DEBUG(errs() << " -> [?] not affine\n");
197 return analyseZIV(A
, B
, S
);
200 return analyseSIV(A
, B
, S
);
202 return analyseMIV(A
, B
, S
);
205 LoopDependenceAnalysis::DependenceResult
206 LoopDependenceAnalysis::analysePair(DependencePair
*P
) const {
207 DEBUG(errs() << "Analysing:\n" << *P
->A
<< "\n" << *P
->B
<< "\n");
209 // We only analyse loads and stores but no possible memory accesses by e.g.
210 // free, call, or invoke instructions.
211 if (!IsLoadOrStoreInst(P
->A
) || !IsLoadOrStoreInst(P
->B
)) {
212 DEBUG(errs() << "--> [?] no load/store\n");
216 Value
*aPtr
= GetPointerOperand(P
->A
);
217 Value
*bPtr
= GetPointerOperand(P
->B
);
219 switch (UnderlyingObjectsAlias(AA
, aPtr
, bPtr
)) {
220 case AliasAnalysis::MayAlias
:
221 // We can not analyse objects if we do not know about their aliasing.
222 DEBUG(errs() << "---> [?] may alias\n");
225 case AliasAnalysis::NoAlias
:
226 // If the objects noalias, they are distinct, accesses are independent.
227 DEBUG(errs() << "---> [I] no alias\n");
230 case AliasAnalysis::MustAlias
:
231 break; // The underlying objects alias, test accesses for dependence.
234 const GEPOperator
*aGEP
= dyn_cast
<GEPOperator
>(aPtr
);
235 const GEPOperator
*bGEP
= dyn_cast
<GEPOperator
>(bPtr
);
240 // FIXME: Is filtering coupled subscripts necessary?
242 // Collect GEP operand pairs (FIXME: use GetGEPOperands from BasicAA), adding
243 // trailing zeroes to the smaller GEP, if needed.
244 typedef SmallVector
<std::pair
<const SCEV
*, const SCEV
*>, 4> GEPOpdPairsTy
;
246 for(GEPOperator::const_op_iterator aIdx
= aGEP
->idx_begin(),
247 aEnd
= aGEP
->idx_end(),
248 bIdx
= bGEP
->idx_begin(),
249 bEnd
= bGEP
->idx_end();
250 aIdx
!= aEnd
&& bIdx
!= bEnd
;
251 aIdx
+= (aIdx
!= aEnd
), bIdx
+= (bIdx
!= bEnd
)) {
252 const SCEV
* aSCEV
= (aIdx
!= aEnd
) ? SE
->getSCEV(*aIdx
) : GetZeroSCEV(SE
);
253 const SCEV
* bSCEV
= (bIdx
!= bEnd
) ? SE
->getSCEV(*bIdx
) : GetZeroSCEV(SE
);
254 opds
.push_back(std::make_pair(aSCEV
, bSCEV
));
257 if (!opds
.empty() && opds
[0].first
!= opds
[0].second
) {
258 // We cannot (yet) handle arbitrary GEP pointer offsets. By limiting
260 // TODO: this could be relaxed by adding the size of the underlying object
261 // to the first subscript. If we have e.g. (GEP x,0,i; GEP x,2,-i) and we
262 // know that x is a [100 x i8]*, we could modify the first subscript to be
263 // (i, 200-i) instead of (i, -i).
267 // Now analyse the collected operand pairs (skipping the GEP ptr offsets).
268 for (GEPOpdPairsTy::const_iterator i
= opds
.begin() + 1, end
= opds
.end();
271 DependenceResult result
= analyseSubscript(i
->first
, i
->second
, &subscript
);
272 if (result
!= Dependent
) {
273 // We either proved independence or failed to analyse this subscript.
274 // Further subscripts will not improve the situation, so abort early.
277 P
->Subscripts
.push_back(subscript
);
279 // We successfully analysed all subscripts but failed to prove independence.
283 bool LoopDependenceAnalysis::depends(Value
*A
, Value
*B
) {
284 assert(isDependencePair(A
, B
) && "Values form no dependence pair!");
288 if (!findOrInsertDependencePair(A
, B
, p
)) {
289 // The pair is not cached, so analyse it.
291 switch (p
->Result
= analysePair(p
)) {
292 case Dependent
: ++NumDependent
; break;
293 case Independent
: ++NumIndependent
; break;
294 case Unknown
: ++NumUnknown
; break;
297 return p
->Result
!= Independent
;
300 //===----------------------------------------------------------------------===//
301 // LoopDependenceAnalysis Implementation
302 //===----------------------------------------------------------------------===//
304 bool LoopDependenceAnalysis::runOnLoop(Loop
*L
, LPPassManager
&) {
306 AA
= &getAnalysis
<AliasAnalysis
>();
307 SE
= &getAnalysis
<ScalarEvolution
>();
311 void LoopDependenceAnalysis::releaseMemory() {
313 PairAllocator
.Reset();
316 void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage
&AU
) const {
317 AU
.setPreservesAll();
318 AU
.addRequiredTransitive
<AliasAnalysis
>();
319 AU
.addRequiredTransitive
<ScalarEvolution
>();
322 static void PrintLoopInfo(raw_ostream
&OS
,
323 LoopDependenceAnalysis
*LDA
, const Loop
*L
) {
324 if (!L
->empty()) return; // ignore non-innermost loops
326 SmallVector
<Instruction
*, 8> memrefs
;
327 GetMemRefInstrs(L
, memrefs
);
329 OS
<< "Loop at depth " << L
->getLoopDepth() << ", header block: ";
330 WriteAsOperand(OS
, L
->getHeader(), false);
333 OS
<< " Load/store instructions: " << memrefs
.size() << "\n";
334 for (SmallVector
<Instruction
*, 8>::const_iterator x
= memrefs
.begin(),
335 end
= memrefs
.end(); x
!= end
; ++x
)
336 OS
<< "\t" << (x
- memrefs
.begin()) << ": " << **x
<< "\n";
338 OS
<< " Pairwise dependence results:\n";
339 for (SmallVector
<Instruction
*, 8>::const_iterator x
= memrefs
.begin(),
340 end
= memrefs
.end(); x
!= end
; ++x
)
341 for (SmallVector
<Instruction
*, 8>::const_iterator y
= x
+ 1;
343 if (LDA
->isDependencePair(*x
, *y
))
344 OS
<< "\t" << (x
- memrefs
.begin()) << "," << (y
- memrefs
.begin())
345 << ": " << (LDA
->depends(*x
, *y
) ? "dependent" : "independent")
349 void LoopDependenceAnalysis::print(raw_ostream
&OS
, const Module
*) const {
350 // TODO: doc why const_cast is safe
351 PrintLoopInfo(OS
, const_cast<LoopDependenceAnalysis
*>(this), this->L
);