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 /// Normal - This is a normal instruction dependence. The pointer member
87 /// of the DepResultTy pair holds the instruction.
90 /// None - This dependence type indicates that the query does not depend
91 /// on any instructions, either because it scanned to the start of the
92 /// function or it scanned to the definition of the memory
96 /// NonLocal - This marker indicates that the query has no dependency in
97 /// the specified block. To find out more, the client should query other
98 /// predecessor blocks.
101 /// Dirty - This is an internal marker indicating that that a cache entry
105 typedef PointerIntPair<Instruction*, 2, DepType> DepResultTy;
107 // A map from instructions to their dependency, with a boolean
108 // flags for whether this mapping is confirmed or not.
109 typedef DenseMap<Instruction*,
110 std::pair<DepResultTy, bool> > LocalDepMapType;
111 LocalDepMapType LocalDeps;
113 // A map from instructions to their non-local dependencies.
114 // FIXME: DENSEMAP of DENSEMAP not a great idea.
115 typedef DenseMap<Instruction*,
116 DenseMap<BasicBlock*, DepResultTy> > nonLocalDepMapType;
117 nonLocalDepMapType depGraphNonLocal;
119 // A reverse mapping from dependencies to the dependees. This is
120 // used when removing instructions to keep the cache coherent.
121 typedef DenseMap<DepResultTy,
122 SmallPtrSet<Instruction*, 4> > reverseDepMapType;
123 reverseDepMapType reverseDep;
125 // A reverse mapping form dependencies to the non-local dependees.
126 reverseDepMapType reverseDepNonLocal;
129 MemoryDependenceAnalysis() : FunctionPass(&ID) {}
132 /// Pass Implementation stuff. This doesn't do any analysis.
134 bool runOnFunction(Function &) {return false; }
136 /// Clean up memory in between runs
137 void releaseMemory() {
139 depGraphNonLocal.clear();
141 reverseDepNonLocal.clear();
144 /// getAnalysisUsage - Does not modify anything. It uses Value Numbering
145 /// and Alias Analysis.
147 virtual void getAnalysisUsage(AnalysisUsage &AU) const;
149 /// getDependency - Return the instruction on which a memory operation
150 /// depends, starting with start.
151 MemDepResult getDependency(Instruction *query, Instruction *start = 0,
152 BasicBlock *block = 0);
154 /// getNonLocalDependency - Fills the passed-in map with the non-local
155 /// dependencies of the queries. The map will contain NonLocal for
156 /// blocks between the query and its dependencies.
157 void getNonLocalDependency(Instruction* query,
158 DenseMap<BasicBlock*, MemDepResult> &resp);
160 /// removeInstruction - Remove an instruction from the dependence analysis,
161 /// updating the dependence of instructions that previously depended on it.
162 void removeInstruction(Instruction *InstToRemove);
164 /// dropInstruction - Remove an instruction from the analysis, making
165 /// absolutely conservative assumptions when updating the cache. This is
166 /// useful, for example when an instruction is changed rather than removed.
167 void dropInstruction(Instruction *InstToDrop);
170 DepResultTy ConvFromResult(MemDepResult R) {
171 if (Instruction *I = R.getInst())
172 return DepResultTy(I, Normal);
174 return DepResultTy(0, NonLocal);
175 assert(R.isNone() && "Unknown MemDepResult!");
176 return DepResultTy(0, None);
179 MemDepResult ConvToResult(DepResultTy R) {
180 if (R.getInt() == Normal)
181 return MemDepResult::get(R.getPointer());
182 if (R.getInt() == NonLocal)
183 return MemDepResult::getNonLocal();
184 assert(R.getInt() == None && "Unknown MemDepResult!");
185 return MemDepResult::getNone();
189 /// verifyRemoved - Verify that the specified instruction does not occur
190 /// in our internal data structures.
191 void verifyRemoved(Instruction *Inst) const;
193 MemDepResult getCallSiteDependency(CallSite C, Instruction* start,
195 void nonLocalHelper(Instruction* query, BasicBlock* block,
196 DenseMap<BasicBlock*, DepResultTy> &resp);
199 } // End llvm namespace