2009-06-25 05:29:13 +08:00
|
|
|
//===- LoopDependenceAnalysis.cpp - LDA Implementation ----------*- C++ -*-===//
|
|
|
|
//
|
|
|
|
// The LLVM Compiler Infrastructure
|
|
|
|
//
|
|
|
|
// This file is distributed under the University of Illinois Open Source
|
|
|
|
// License. See LICENSE.TXT for details.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
//
|
|
|
|
// This is the (beginning) of an implementation of a loop dependence analysis
|
|
|
|
// framework, which is used to detect dependences in memory accesses in loops.
|
|
|
|
//
|
|
|
|
// Please note that this is work in progress and the interface is subject to
|
|
|
|
// change.
|
|
|
|
//
|
|
|
|
// TODO: adapt as implementation progresses.
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#define DEBUG_TYPE "lda"
|
2009-07-29 03:49:49 +08:00
|
|
|
#include "llvm/ADT/Statistic.h"
|
2009-07-02 05:45:23 +08:00
|
|
|
#include "llvm/Analysis/AliasAnalysis.h"
|
2009-06-25 05:29:13 +08:00
|
|
|
#include "llvm/Analysis/LoopDependenceAnalysis.h"
|
|
|
|
#include "llvm/Analysis/LoopPass.h"
|
|
|
|
#include "llvm/Analysis/ScalarEvolution.h"
|
2009-06-28 08:21:21 +08:00
|
|
|
#include "llvm/Instructions.h"
|
2009-07-25 07:19:28 +08:00
|
|
|
#include "llvm/Support/Allocator.h"
|
2009-06-30 10:12:10 +08:00
|
|
|
#include "llvm/Support/Debug.h"
|
2009-07-12 04:10:48 +08:00
|
|
|
#include "llvm/Support/ErrorHandling.h"
|
2009-07-25 07:19:28 +08:00
|
|
|
#include "llvm/Support/raw_ostream.h"
|
2009-07-02 05:45:23 +08:00
|
|
|
#include "llvm/Target/TargetData.h"
|
2009-06-25 05:29:13 +08:00
|
|
|
using namespace llvm;
|
|
|
|
|
2009-07-29 03:49:49 +08:00
|
|
|
STATISTIC(NumAnswered, "Number of dependence queries answered");
|
|
|
|
STATISTIC(NumAnalysed, "Number of distinct dependence pairs analysed");
|
|
|
|
STATISTIC(NumDependent, "Number of pairs with dependent accesses");
|
|
|
|
STATISTIC(NumIndependent, "Number of pairs with independent accesses");
|
|
|
|
STATISTIC(NumUnknown, "Number of pairs with unknown accesses");
|
|
|
|
|
2009-06-25 05:29:13 +08:00
|
|
|
LoopPass *llvm::createLoopDependenceAnalysisPass() {
|
|
|
|
return new LoopDependenceAnalysis();
|
|
|
|
}
|
|
|
|
|
|
|
|
static RegisterPass<LoopDependenceAnalysis>
|
|
|
|
R("lda", "Loop Dependence Analysis", false, true);
|
|
|
|
char LoopDependenceAnalysis::ID = 0;
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-28 08:21:21 +08:00
|
|
|
// Utility Functions
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-06-30 02:51:11 +08:00
|
|
|
static inline bool IsMemRefInstr(const Value *V) {
|
|
|
|
const Instruction *I = dyn_cast<const Instruction>(V);
|
|
|
|
return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
|
2009-06-28 08:21:21 +08:00
|
|
|
}
|
|
|
|
|
2009-07-23 09:57:06 +08:00
|
|
|
static void GetMemRefInstrs(const Loop *L,
|
|
|
|
SmallVectorImpl<Instruction*> &Memrefs) {
|
2009-06-28 08:35:22 +08:00
|
|
|
for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
|
2009-07-29 03:49:25 +08:00
|
|
|
b != be; ++b)
|
2009-06-28 08:35:22 +08:00
|
|
|
for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
|
2009-07-29 03:49:25 +08:00
|
|
|
i != ie; ++i)
|
2009-06-29 08:50:26 +08:00
|
|
|
if (IsMemRefInstr(i))
|
2009-07-23 09:57:06 +08:00
|
|
|
Memrefs.push_back(i);
|
2009-06-28 08:35:22 +08:00
|
|
|
}
|
|
|
|
|
2009-06-30 10:12:10 +08:00
|
|
|
static bool IsLoadOrStoreInst(Value *I) {
|
|
|
|
return isa<LoadInst>(I) || isa<StoreInst>(I);
|
|
|
|
}
|
|
|
|
|
|
|
|
static Value *GetPointerOperand(Value *I) {
|
|
|
|
if (LoadInst *i = dyn_cast<LoadInst>(I))
|
|
|
|
return i->getPointerOperand();
|
|
|
|
if (StoreInst *i = dyn_cast<StoreInst>(I))
|
|
|
|
return i->getPointerOperand();
|
2009-07-15 00:55:14 +08:00
|
|
|
llvm_unreachable("Value is no load or store instruction!");
|
2009-06-30 10:12:10 +08:00
|
|
|
// Never reached.
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2009-07-29 03:49:25 +08:00
|
|
|
static AliasAnalysis::AliasResult UnderlyingObjectsAlias(AliasAnalysis *AA,
|
|
|
|
const Value *A,
|
|
|
|
const Value *B) {
|
|
|
|
const Value *aObj = A->getUnderlyingObject();
|
|
|
|
const Value *bObj = B->getUnderlyingObject();
|
|
|
|
return AA->alias(aObj, AA->getTypeStoreSize(aObj->getType()),
|
|
|
|
bObj, AA->getTypeStoreSize(bObj->getType()));
|
|
|
|
}
|
|
|
|
|
2009-06-28 08:21:21 +08:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// Dependence Testing
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
2009-07-23 09:57:06 +08:00
|
|
|
bool LoopDependenceAnalysis::isDependencePair(const Value *A,
|
|
|
|
const Value *B) const {
|
|
|
|
return IsMemRefInstr(A) &&
|
|
|
|
IsMemRefInstr(B) &&
|
|
|
|
(cast<const Instruction>(A)->mayWriteToMemory() ||
|
|
|
|
cast<const Instruction>(B)->mayWriteToMemory());
|
2009-06-28 08:21:21 +08:00
|
|
|
}
|
|
|
|
|
2009-07-29 03:49:25 +08:00
|
|
|
bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *A,
|
|
|
|
Value *B,
|
2009-07-23 22:32:46 +08:00
|
|
|
DependencePair *&P) {
|
|
|
|
void *insertPos = 0;
|
|
|
|
FoldingSetNodeID id;
|
2009-07-29 03:49:25 +08:00
|
|
|
id.AddPointer(A);
|
|
|
|
id.AddPointer(B);
|
2009-07-23 22:32:46 +08:00
|
|
|
|
|
|
|
P = Pairs.FindNodeOrInsertPos(id, insertPos);
|
|
|
|
if (P) return true;
|
|
|
|
|
|
|
|
P = PairAllocator.Allocate<DependencePair>();
|
2009-07-29 03:49:25 +08:00
|
|
|
new (P) DependencePair(id, A, B);
|
2009-07-23 22:32:46 +08:00
|
|
|
Pairs.InsertNode(P, insertPos);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
void LoopDependenceAnalysis::analysePair(DependencePair *P) const {
|
2009-07-25 20:19:58 +08:00
|
|
|
DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
|
2009-07-23 22:32:46 +08:00
|
|
|
|
|
|
|
// Our default answer: we don't know anything, i.e. we failed to analyse this
|
|
|
|
// pair to get a more specific answer (dependent, independent).
|
|
|
|
P->Result = Unknown;
|
|
|
|
|
|
|
|
// We only analyse loads and stores but no possible memory accesses by e.g.
|
|
|
|
// free, call, or invoke instructions.
|
|
|
|
if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
|
2009-07-25 20:19:58 +08:00
|
|
|
DEBUG(errs() << "--> [?] no load/store\n");
|
2009-07-23 22:32:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2009-07-29 03:49:25 +08:00
|
|
|
Value *aPtr = GetPointerOperand(P->A);
|
|
|
|
Value *bPtr = GetPointerOperand(P->B);
|
2009-07-02 05:45:23 +08:00
|
|
|
|
2009-07-29 03:49:25 +08:00
|
|
|
switch (UnderlyingObjectsAlias(AA, aPtr, bPtr)) {
|
|
|
|
case AliasAnalysis::MayAlias:
|
|
|
|
// We can not analyse objects if we do not know about their aliasing.
|
2009-07-25 20:19:58 +08:00
|
|
|
DEBUG(errs() << "---> [?] may alias\n");
|
2009-07-23 22:32:46 +08:00
|
|
|
return;
|
2009-06-30 10:12:10 +08:00
|
|
|
|
2009-07-29 03:49:25 +08:00
|
|
|
case AliasAnalysis::NoAlias:
|
|
|
|
// If the objects noalias, they are distinct, accesses are independent.
|
2009-07-25 20:19:58 +08:00
|
|
|
DEBUG(errs() << "---> [I] no alias\n");
|
2009-07-23 22:32:46 +08:00
|
|
|
P->Result = Independent;
|
|
|
|
return;
|
2009-06-30 10:12:10 +08:00
|
|
|
|
2009-07-29 03:49:25 +08:00
|
|
|
case AliasAnalysis::MustAlias:
|
|
|
|
break; // The underlying objects alias, test accesses for dependence.
|
|
|
|
}
|
2009-07-02 05:45:23 +08:00
|
|
|
|
2009-07-25 20:19:58 +08:00
|
|
|
DEBUG(errs() << "---> [?] cannot analyse\n");
|
2009-07-23 22:32:46 +08:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
|
|
|
|
assert(isDependencePair(A, B) && "Values form no dependence pair!");
|
2009-07-29 03:49:49 +08:00
|
|
|
++NumAnswered;
|
2009-07-23 22:32:46 +08:00
|
|
|
|
|
|
|
DependencePair *p;
|
|
|
|
if (!findOrInsertDependencePair(A, B, p)) {
|
|
|
|
// The pair is not cached, so analyse it.
|
2009-07-29 03:49:49 +08:00
|
|
|
++NumAnalysed;
|
2009-07-23 22:32:46 +08:00
|
|
|
analysePair(p);
|
2009-07-29 03:49:49 +08:00
|
|
|
switch (p->Result) {
|
|
|
|
case Dependent: ++NumDependent; break;
|
|
|
|
case Independent: ++NumIndependent; break;
|
|
|
|
case Unknown: ++NumUnknown; break;
|
|
|
|
}
|
2009-07-23 22:32:46 +08:00
|
|
|
}
|
|
|
|
return p->Result != Independent;
|
2009-06-28 08:21:21 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
2009-06-25 05:29:13 +08:00
|
|
|
// LoopDependenceAnalysis Implementation
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
|
|
|
|
this->L = L;
|
2009-07-02 05:45:23 +08:00
|
|
|
AA = &getAnalysis<AliasAnalysis>();
|
2009-06-25 05:29:13 +08:00
|
|
|
SE = &getAnalysis<ScalarEvolution>();
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2009-07-23 22:32:46 +08:00
|
|
|
void LoopDependenceAnalysis::releaseMemory() {
|
|
|
|
Pairs.clear();
|
|
|
|
PairAllocator.Reset();
|
|
|
|
}
|
|
|
|
|
2009-06-25 05:29:13 +08:00
|
|
|
void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
|
|
|
|
AU.setPreservesAll();
|
2009-07-02 05:45:23 +08:00
|
|
|
AU.addRequiredTransitive<AliasAnalysis>();
|
2009-06-28 08:16:08 +08:00
|
|
|
AU.addRequiredTransitive<ScalarEvolution>();
|
|
|
|
}
|
|
|
|
|
2009-07-23 09:57:06 +08:00
|
|
|
static void PrintLoopInfo(raw_ostream &OS,
|
|
|
|
LoopDependenceAnalysis *LDA, const Loop *L) {
|
2009-06-28 08:16:08 +08:00
|
|
|
if (!L->empty()) return; // ignore non-innermost loops
|
|
|
|
|
2009-07-03 09:42:52 +08:00
|
|
|
SmallVector<Instruction*, 8> memrefs;
|
|
|
|
GetMemRefInstrs(L, memrefs);
|
|
|
|
|
2009-06-28 08:16:08 +08:00
|
|
|
OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
|
|
|
|
WriteAsOperand(OS, L->getHeader(), false);
|
|
|
|
OS << "\n";
|
2009-06-28 08:35:22 +08:00
|
|
|
|
|
|
|
OS << " Load/store instructions: " << memrefs.size() << "\n";
|
2009-07-03 09:42:52 +08:00
|
|
|
for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
|
2009-07-29 03:49:25 +08:00
|
|
|
end = memrefs.end(); x != end; ++x)
|
2009-07-23 09:57:06 +08:00
|
|
|
OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
|
2009-07-03 09:42:52 +08:00
|
|
|
|
2009-06-28 08:35:22 +08:00
|
|
|
OS << " Pairwise dependence results:\n";
|
|
|
|
for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
|
2009-07-29 03:49:25 +08:00
|
|
|
end = memrefs.end(); x != end; ++x)
|
2009-06-28 08:35:22 +08:00
|
|
|
for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
|
2009-07-29 03:49:25 +08:00
|
|
|
y != end; ++y)
|
2009-06-28 08:35:22 +08:00
|
|
|
if (LDA->isDependencePair(*x, *y))
|
|
|
|
OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
|
|
|
|
<< ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
|
|
|
|
<< "\n";
|
2009-06-28 08:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
|
2009-06-28 08:35:22 +08:00
|
|
|
// TODO: doc why const_cast is safe
|
|
|
|
PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
|
2009-06-28 08:16:08 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
|
|
|
|
raw_os_ostream os(OS);
|
|
|
|
print(os, M);
|
2009-06-25 05:29:13 +08:00
|
|
|
}
|