1 //===- llvm/Analysis/MemoryDependenceAnalysis.h - Memory Deps --*- 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 file defines the MemoryDependenceAnalysis analysis pass.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
15 #define LLVM_ANALYSIS_MEMORY_DEPENDENCE_H
17 #include "llvm/Pass.h"
18 #include "llvm/ADT/DenseMap.h"
19 #include "llvm/ADT/SmallPtrSet.h"
20 #include "llvm/ADT/PointerIntPair.h"
28 /// MemDepResult - A memory dependence query can return one of three different
30 /// Normal : The query is dependent on a specific instruction.
31 /// NonLocal: The query does not depend on anything inside this block, but
32 /// we haven't scanned beyond the block to find out what.
33 /// None : The query does not depend on anything: we found the entry
34 /// block or the allocation site of the memory.
37 Invalid = 0, Normal, NonLocal, None
39 typedef PointerIntPair<Instruction*, 2, DepType> PairTy;
41 explicit MemDepResult(PairTy V) : Value(V) {}
43 MemDepResult() : Value(0, Invalid) {}
45 /// get methods: These are static ctor methods for creating various
46 /// MemDepResult kinds.
47 static MemDepResult get(Instruction *Inst) {
48 return MemDepResult(PairTy(Inst, Normal));
50 static MemDepResult getNonLocal() {
51 return MemDepResult(PairTy(0, NonLocal));
53 static MemDepResult getNone() {
54 return MemDepResult(PairTy(0, None));
57 /// isNormal - Return true if this MemDepResult represents a query that is
58 /// a normal instruction dependency.
59 bool isNormal() const { return Value.getInt() == Normal; }
61 /// isNonLocal - Return true if this MemDepResult represents an query that
62 /// is transparent to the start of the block, but where a non-local hasn't
64 bool isNonLocal() const { return Value.getInt() == NonLocal; }
66 /// isNone - Return true if this MemDepResult represents a query that
67 /// doesn't depend on any instruction.
68 bool isNone() const { return Value.getInt() == None; }
70 /// getInst() - If this is a normal dependency, return the instruction that
71 /// is depended on. Otherwise, return null.
72 Instruction *getInst() const { return isNormal() ? Value.getPointer() : 0; }
74 bool operator==(const MemDepResult &M) { return M.Value == Value; }
75 bool operator!=(const MemDepResult &M) { return M.Value != Value; }
78 /// MemoryDependenceAnalysis - This is an analysis that determines, for a
79 /// given memory operation, what preceding memory operations it depends on.
80 /// It builds on alias analysis information, and tries to provide a lazy,
81 /// caching interface to a common kind of alias information query.
82 class MemoryDependenceAnalysis : public FunctionPass {
83 /// DepType - This enum is used to indicate what flavor of dependence this
84 /// is. If the type is Normal, there is an associated instruction pointer.
86 /// Dirty - Entries with this marker may come in two forms, depending on
87 /// whether they are in a LocalDeps map or NonLocalDeps map. In either
88 /// case, this marker indicates that the cached value has been invalidated
89 /// by a removeInstruction call.
91 /// If in the LocalDeps map, the Instruction field will indicate the place
92 /// in the current block to start scanning. If in the non-localdeps map,
93 /// the instruction will be null.
95 /// In a default-constructed DepResultTy object, the type will be Dirty
96 /// and the instruction pointer will be null.
98 /// FIXME: Why not add a scanning point for the non-local deps map???
101 /// Normal - This is a normal instruction dependence. The pointer member
102 /// of the DepResultTy pair holds the instruction.
105 /// None - This dependence type indicates that the query does not depend
106 /// on any instructions, either because it scanned to the start of the
107 /// function or it scanned to the definition of the memory
111 /// NonLocal - This marker indicates that the query has no dependency in
112 /// the specified block. To find out more, the client should query other
113 /// predecessor blocks.
116 typedef PointerIntPair<Instruction*, 2, DepType> DepResultTy;
118 // A map from instructions to their dependency.
119 typedef DenseMap<Instruction*, DepResultTy> LocalDepMapType;
120 LocalDepMapType LocalDeps;
122 // A map from instructions to their non-local dependencies.
123 // FIXME: DENSEMAP of DENSEMAP not a great idea.
124 typedef DenseMap<Instruction*,
125 DenseMap<BasicBlock*, DepResultTy> > nonLocalDepMapType;
126 nonLocalDepMapType depGraphNonLocal;
128 // A reverse mapping from dependencies to the dependees. This is
129 // used when removing instructions to keep the cache coherent.
130 typedef DenseMap<Instruction*,
131 SmallPtrSet<Instruction*, 4> > reverseDepMapType;
132 reverseDepMapType reverseDep;
134 // A reverse mapping form dependencies to the non-local dependees.
135 reverseDepMapType reverseDepNonLocal;
138 MemoryDependenceAnalysis() : FunctionPass(&ID) {}
141 /// Pass Implementation stuff. This doesn't do any analysis.
143 bool runOnFunction(Function &) {return false; }
145 /// Clean up memory in between runs
146 void releaseMemory() {
148 depGraphNonLocal.clear();
150 reverseDepNonLocal.clear();
153 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
154 /// and Alias Analysis.
156 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
158 /// getDependency - Return the instruction on which a memory operation
159 /// depends, starting with start.
160 MemDepResult getDependency(Instruction *query, Instruction *start = 0,
161 BasicBlock *block = 0);
163 /// getNonLocalDependency - Fills the passed-in map with the non-local
164 /// dependencies of the queries. The map will contain NonLocal for
165 /// blocks between the query and its dependencies.
166 void getNonLocalDependency(Instruction* query,
167 DenseMap<BasicBlock*, MemDepResult> &resp);
169 /// removeInstruction - Remove an instruction from the dependence analysis,
170 /// updating the dependence of instructions that previously depended on it.
171 void removeInstruction(Instruction *InstToRemove);
173 /// dropInstruction - Remove an instruction from the analysis, making
174 /// absolutely conservative assumptions when updating the cache. This is
175 /// useful, for example when an instruction is changed rather than removed.
176 void dropInstruction(Instruction *InstToDrop);
179 DepResultTy ConvFromResult(MemDepResult R) {
180 if (Instruction *I = R.getInst())
181 return DepResultTy(I, Normal);
183 return DepResultTy(0, NonLocal);
184 assert(R.isNone() && "Unknown MemDepResult!");
185 return DepResultTy(0, None);
188 MemDepResult ConvToResult(DepResultTy R) {
189 if (R.getInt() == Normal)
190 return MemDepResult::get(R.getPointer());
191 if (R.getInt() == NonLocal)
192 return MemDepResult::getNonLocal();
193 assert(R.getInt() == None && "Unknown MemDepResult!");
194 return MemDepResult::getNone();
198 /// verifyRemoved - Verify that the specified instruction does not occur
199 /// in our internal data structures.
200 void verifyRemoved(Instruction *Inst) const;
202 MemDepResult getCallSiteDependency(CallSite C, Instruction* start,
204 void nonLocalHelper(Instruction* query, BasicBlock* block,
205 DenseMap<BasicBlock*, DepResultTy> &resp);
208 } // End llvm namespace