blob: 421e4e1ac18b6f2716dfa54dca59361354350e9d [file] [log] [blame]
Andreas Bolka306b5b22009-06-24 21:29:13 +00001//===- LoopDependenceAnalysis.cpp - LDA Implementation ----------*- C++ -*-===//
2//
3// The LLVM Compiler Infrastructure
4//
5// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
7//
8//===----------------------------------------------------------------------===//
9//
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.
12//
13// Please note that this is work in progress and the interface is subject to
14// change.
15//
16// TODO: adapt as implementation progresses.
17//
18//===----------------------------------------------------------------------===//
19
20#define DEBUG_TYPE "lda"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000021#include "llvm/Analysis/AliasAnalysis.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000022#include "llvm/Analysis/LoopDependenceAnalysis.h"
23#include "llvm/Analysis/LoopPass.h"
24#include "llvm/Analysis/ScalarEvolution.h"
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000025#include "llvm/Instructions.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000026#include "llvm/Support/Allocator.h"
Andreas Bolka5771ed92009-06-30 02:12:10 +000027#include "llvm/Support/Debug.h"
Edwin Török675d5622009-07-11 20:10:48 +000028#include "llvm/Support/ErrorHandling.h"
Andreas Bolka127c2e72009-07-24 23:19:28 +000029#include "llvm/Support/raw_ostream.h"
Andreas Bolka8f9cd692009-07-01 21:45:23 +000030#include "llvm/Target/TargetData.h"
Andreas Bolka306b5b22009-06-24 21:29:13 +000031using namespace llvm;
32
33LoopPass *llvm::createLoopDependenceAnalysisPass() {
34 return new LoopDependenceAnalysis();
35}
36
37static RegisterPass<LoopDependenceAnalysis>
38R("lda", "Loop Dependence Analysis", false, true);
39char LoopDependenceAnalysis::ID = 0;
40
41//===----------------------------------------------------------------------===//
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000042// Utility Functions
43//===----------------------------------------------------------------------===//
44
Andreas Bolkafc8a04a2009-06-29 18:51:11 +000045static inline bool IsMemRefInstr(const Value *V) {
46 const Instruction *I = dyn_cast<const Instruction>(V);
47 return I && (I->mayReadFromMemory() || I->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000048}
49
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000050static void GetMemRefInstrs(const Loop *L,
51 SmallVectorImpl<Instruction*> &Memrefs) {
Andreas Bolka158c5902009-06-28 00:35:22 +000052 for (Loop::block_iterator b = L->block_begin(), be = L->block_end();
53 b != be; ++b)
54 for (BasicBlock::iterator i = (*b)->begin(), ie = (*b)->end();
55 i != ie; ++i)
Andreas Bolka88a17f02009-06-29 00:50:26 +000056 if (IsMemRefInstr(i))
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000057 Memrefs.push_back(i);
Andreas Bolka158c5902009-06-28 00:35:22 +000058}
59
Andreas Bolka5771ed92009-06-30 02:12:10 +000060static bool IsLoadOrStoreInst(Value *I) {
61 return isa<LoadInst>(I) || isa<StoreInst>(I);
62}
63
64static Value *GetPointerOperand(Value *I) {
65 if (LoadInst *i = dyn_cast<LoadInst>(I))
66 return i->getPointerOperand();
67 if (StoreInst *i = dyn_cast<StoreInst>(I))
68 return i->getPointerOperand();
Edwin Törökbd448e32009-07-14 16:55:14 +000069 llvm_unreachable("Value is no load or store instruction!");
Andreas Bolka5771ed92009-06-30 02:12:10 +000070 // Never reached.
71 return 0;
72}
73
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000074//===----------------------------------------------------------------------===//
75// Dependence Testing
76//===----------------------------------------------------------------------===//
77
Andreas Bolkad14a5eb2009-07-23 01:57:06 +000078bool LoopDependenceAnalysis::isDependencePair(const Value *A,
79 const Value *B) const {
80 return IsMemRefInstr(A) &&
81 IsMemRefInstr(B) &&
82 (cast<const Instruction>(A)->mayWriteToMemory() ||
83 cast<const Instruction>(B)->mayWriteToMemory());
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +000084}
85
Andreas Bolka0a528a02009-07-23 14:32:46 +000086bool LoopDependenceAnalysis::findOrInsertDependencePair(Value *X,
87 Value *Y,
88 DependencePair *&P) {
89 void *insertPos = 0;
90 FoldingSetNodeID id;
91 id.AddPointer(X);
92 id.AddPointer(Y);
Andreas Bolka5771ed92009-06-30 02:12:10 +000093
Andreas Bolka0a528a02009-07-23 14:32:46 +000094 P = Pairs.FindNodeOrInsertPos(id, insertPos);
95 if (P) return true;
Andreas Bolka5771ed92009-06-30 02:12:10 +000096
Andreas Bolka0a528a02009-07-23 14:32:46 +000097 P = PairAllocator.Allocate<DependencePair>();
98 new (P) DependencePair(id, X, Y);
99 Pairs.InsertNode(P, insertPos);
100 return false;
101}
102
103void LoopDependenceAnalysis::analysePair(DependencePair *P) const {
Andreas Bolka89569882009-07-25 12:19:58 +0000104 DEBUG(errs() << "Analysing:\n" << *P->A << "\n" << *P->B << "\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000105
106 // Our default answer: we don't know anything, i.e. we failed to analyse this
107 // pair to get a more specific answer (dependent, independent).
108 P->Result = Unknown;
109
110 // We only analyse loads and stores but no possible memory accesses by e.g.
111 // free, call, or invoke instructions.
112 if (!IsLoadOrStoreInst(P->A) || !IsLoadOrStoreInst(P->B)) {
Andreas Bolka89569882009-07-25 12:19:58 +0000113 DEBUG(errs() << "--> [?] no load/store\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000114 return;
115 }
116
117 Value *aptr = GetPointerOperand(P->A);
118 Value *bptr = GetPointerOperand(P->B);
119 const Value *aobj = aptr->getUnderlyingObject();
120 const Value *bobj = bptr->getUnderlyingObject();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000121 AliasAnalysis::AliasResult alias = AA->alias(
Dan Gohman624f9612009-07-25 00:48:42 +0000122 aobj, AA->getTypeStoreSize(aobj->getType()),
123 bobj, AA->getTypeStoreSize(bobj->getType()));
Andreas Bolka5771ed92009-06-30 02:12:10 +0000124
Andreas Bolka0a528a02009-07-23 14:32:46 +0000125 // We can not analyse objects if we do not know about their aliasing.
126 if (alias == AliasAnalysis::MayAlias) {
Andreas Bolka89569882009-07-25 12:19:58 +0000127 DEBUG(errs() << "---> [?] may alias\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000128 return;
129 }
Andreas Bolka5771ed92009-06-30 02:12:10 +0000130
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000131 // If the objects noalias, they are distinct, accesses are independent.
Andreas Bolka0a528a02009-07-23 14:32:46 +0000132 if (alias == AliasAnalysis::NoAlias) {
Andreas Bolka89569882009-07-25 12:19:58 +0000133 DEBUG(errs() << "---> [I] no alias\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000134 P->Result = Independent;
135 return;
136 }
Andreas Bolka5771ed92009-06-30 02:12:10 +0000137
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000138 // TODO: the underlying objects MustAlias, test for dependence
139
Andreas Bolka89569882009-07-25 12:19:58 +0000140 DEBUG(errs() << "---> [?] cannot analyse\n");
Andreas Bolka0a528a02009-07-23 14:32:46 +0000141 return;
142}
143
144bool LoopDependenceAnalysis::depends(Value *A, Value *B) {
145 assert(isDependencePair(A, B) && "Values form no dependence pair!");
146
147 DependencePair *p;
148 if (!findOrInsertDependencePair(A, B, p)) {
149 // The pair is not cached, so analyse it.
150 analysePair(p);
151 }
152 return p->Result != Independent;
Andreas Bolkaf17ab7f2009-06-28 00:21:21 +0000153}
154
155//===----------------------------------------------------------------------===//
Andreas Bolka306b5b22009-06-24 21:29:13 +0000156// LoopDependenceAnalysis Implementation
157//===----------------------------------------------------------------------===//
158
159bool LoopDependenceAnalysis::runOnLoop(Loop *L, LPPassManager &) {
160 this->L = L;
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000161 AA = &getAnalysis<AliasAnalysis>();
Andreas Bolka306b5b22009-06-24 21:29:13 +0000162 SE = &getAnalysis<ScalarEvolution>();
163 return false;
164}
165
Andreas Bolka0a528a02009-07-23 14:32:46 +0000166void LoopDependenceAnalysis::releaseMemory() {
167 Pairs.clear();
168 PairAllocator.Reset();
169}
170
Andreas Bolka306b5b22009-06-24 21:29:13 +0000171void LoopDependenceAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
172 AU.setPreservesAll();
Andreas Bolka8f9cd692009-07-01 21:45:23 +0000173 AU.addRequiredTransitive<AliasAnalysis>();
Andreas Bolkab88ee912009-06-28 00:16:08 +0000174 AU.addRequiredTransitive<ScalarEvolution>();
175}
176
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000177static void PrintLoopInfo(raw_ostream &OS,
178 LoopDependenceAnalysis *LDA, const Loop *L) {
Andreas Bolkab88ee912009-06-28 00:16:08 +0000179 if (!L->empty()) return; // ignore non-innermost loops
180
Andreas Bolkab512fb02009-07-03 01:42:52 +0000181 SmallVector<Instruction*, 8> memrefs;
182 GetMemRefInstrs(L, memrefs);
183
Andreas Bolkab88ee912009-06-28 00:16:08 +0000184 OS << "Loop at depth " << L->getLoopDepth() << ", header block: ";
185 WriteAsOperand(OS, L->getHeader(), false);
186 OS << "\n";
Andreas Bolka158c5902009-06-28 00:35:22 +0000187
Andreas Bolka158c5902009-06-28 00:35:22 +0000188 OS << " Load/store instructions: " << memrefs.size() << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000189 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
190 end = memrefs.end(); x != end; ++x)
Andreas Bolkad14a5eb2009-07-23 01:57:06 +0000191 OS << "\t" << (x - memrefs.begin()) << ": " << **x << "\n";
Andreas Bolkab512fb02009-07-03 01:42:52 +0000192
Andreas Bolka158c5902009-06-28 00:35:22 +0000193 OS << " Pairwise dependence results:\n";
194 for (SmallVector<Instruction*, 8>::const_iterator x = memrefs.begin(),
195 end = memrefs.end(); x != end; ++x)
196 for (SmallVector<Instruction*, 8>::const_iterator y = x + 1;
197 y != end; ++y)
198 if (LDA->isDependencePair(*x, *y))
199 OS << "\t" << (x - memrefs.begin()) << "," << (y - memrefs.begin())
200 << ": " << (LDA->depends(*x, *y) ? "dependent" : "independent")
201 << "\n";
Andreas Bolkab88ee912009-06-28 00:16:08 +0000202}
203
204void LoopDependenceAnalysis::print(raw_ostream &OS, const Module*) const {
Andreas Bolka158c5902009-06-28 00:35:22 +0000205 // TODO: doc why const_cast is safe
206 PrintLoopInfo(OS, const_cast<LoopDependenceAnalysis*>(this), this->L);
Andreas Bolkab88ee912009-06-28 00:16:08 +0000207}
208
209void LoopDependenceAnalysis::print(std::ostream &OS, const Module *M) const {
210 raw_os_ostream os(OS);
211 print(os, M);
Andreas Bolka306b5b22009-06-24 21:29:13 +0000212}