Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 1 | //===- IPModRef.cpp - Compute IP Mod/Ref information ------------*- C++ -*-===// |
| 2 | // |
| 3 | // See high-level comments in include/llvm/Analysis/IPModRef.h |
| 4 | // |
| 5 | //===----------------------------------------------------------------------===// |
| 6 | |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame^] | 7 | #include "llvm/Analysis/IPModRef.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 8 | #include "llvm/Analysis/DataStructure.h" |
| 9 | #include "llvm/Analysis/DSGraph.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 10 | #include "llvm/Module.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 11 | #include "llvm/iOther.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 12 | #include "Support/Statistic.h" |
| 13 | #include "Support/STLExtras.h" |
| 14 | #include "Support/StringExtras.h" |
| 15 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 16 | //---------------------------------------------------------------------------- |
| 17 | // Private constants and data |
| 18 | //---------------------------------------------------------------------------- |
| 19 | |
| 20 | static RegisterAnalysis<IPModRef> |
| 21 | Z("ipmodref", "Interprocedural mod/ref analysis"); |
| 22 | |
| 23 | |
| 24 | //---------------------------------------------------------------------------- |
| 25 | // class ModRefInfo |
| 26 | //---------------------------------------------------------------------------- |
| 27 | |
| 28 | void ModRefInfo::print(std::ostream &O) const |
| 29 | { |
| 30 | O << std::endl << "Modified nodes = " << modNodeSet; |
| 31 | O << "Referenced nodes = " << refNodeSet << std::endl; |
| 32 | } |
| 33 | |
| 34 | void ModRefInfo::dump() const |
| 35 | { |
| 36 | print(std::cerr); |
| 37 | } |
| 38 | |
| 39 | //---------------------------------------------------------------------------- |
| 40 | // class FunctionModRefInfo |
| 41 | //---------------------------------------------------------------------------- |
| 42 | |
| 43 | |
| 44 | // This constructor computes a node numbering for the TD graph. |
| 45 | // |
| 46 | FunctionModRefInfo::FunctionModRefInfo(const Function& func, |
| 47 | const DSGraph& tdg, |
| 48 | const DSGraph& ldg) |
| 49 | : F(func), |
| 50 | funcTDGraph(tdg), |
| 51 | funcLocalGraph(ldg), |
| 52 | funcModRefInfo(tdg.getGraphSize()) |
| 53 | { |
| 54 | for (unsigned i=0, N = funcTDGraph.getGraphSize(); i < N; ++i) |
| 55 | NodeIds[funcTDGraph.getNodes()[i]] = i; |
| 56 | } |
| 57 | |
| 58 | |
| 59 | FunctionModRefInfo::~FunctionModRefInfo() |
| 60 | { |
| 61 | for(std::map<const CallInst*, ModRefInfo*>::iterator |
| 62 | I=callSiteModRefInfo.begin(), E=callSiteModRefInfo.end(); I != E; ++I) |
| 63 | delete(I->second); |
| 64 | |
| 65 | // Empty map just to make problems easier to track down |
| 66 | callSiteModRefInfo.clear(); |
| 67 | } |
| 68 | |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame^] | 69 | unsigned FunctionModRefInfo::getNodeId(const Value* value) const { |
| 70 | return getNodeId(funcTDGraph.getNodeForValue(const_cast<Value*>(value)) |
| 71 | .getNode()); |
| 72 | } |
| 73 | |
| 74 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 75 | |
| 76 | // Dummy function that will be replaced with one that inlines |
| 77 | // the callee's BU graph into the caller's TD graph. |
| 78 | // |
| 79 | const DSGraph* ResolveGraphForCallSite(const DSGraph& funcTDGraph, |
| 80 | const CallInst& callInst) |
| 81 | { |
| 82 | return &funcTDGraph; // TEMPORARY |
| 83 | } |
| 84 | |
| 85 | |
| 86 | // Compute Mod/Ref bit vectors for the entire function. |
| 87 | // These are simply copies of the Read/Write flags from the nodes of |
| 88 | // the top-down DS graph. |
| 89 | // |
| 90 | void FunctionModRefInfo::computeModRef(const Function &func) |
| 91 | { |
| 92 | // Mark all nodes in the graph that are marked MOD as being mod |
| 93 | // and all those marked REF as being ref. |
| 94 | for (unsigned i = 0, N = funcTDGraph.getGraphSize(); i < N; ++i) |
| 95 | { |
| 96 | if (funcTDGraph.getNodes()[i]->isModified()) |
| 97 | funcModRefInfo.setNodeIsMod(i); |
| 98 | if (funcTDGraph.getNodes()[i]->isRead()) |
| 99 | funcModRefInfo.setNodeIsRef(i); |
| 100 | } |
| 101 | |
| 102 | // Compute the Mod/Ref info for all call sites within the function |
| 103 | // Use the Local DSgraph, which includes all the call sites in the |
| 104 | // original program. |
| 105 | const std::vector<DSCallSite>& callSites = funcLocalGraph.getFunctionCalls(); |
| 106 | for (unsigned i = 0, N = callSites.size(); i < N; ++i) |
| 107 | computeModRef(callSites[i].getCallInst()); |
| 108 | } |
| 109 | |
| 110 | |
| 111 | // Compute Mod/Ref bit vectors for a single call site. |
| 112 | // These are copies of the Read/Write flags from the nodes of |
| 113 | // the graph produced by clearing all flags in teh caller's TD graph |
| 114 | // and then inlining the callee's BU graph into the caller's TD graph. |
| 115 | // |
| 116 | void |
| 117 | FunctionModRefInfo::computeModRef(const CallInst& callInst) |
| 118 | { |
| 119 | // Allocate the mod/ref info for the call site. Bits automatically cleared. |
| 120 | ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph.getGraphSize()); |
| 121 | callSiteModRefInfo[&callInst] = callModRefInfo; |
| 122 | |
| 123 | // Get a copy of the graph for the callee with the callee inlined |
| 124 | const DSGraph* csgp = ResolveGraphForCallSite(funcTDGraph, callInst); |
| 125 | assert(csgp && "Unable to compute callee mod/ref information"); |
| 126 | |
| 127 | // For all nodes in the graph, extract the mod/ref information |
| 128 | const std::vector<DSNode*>& csgNodes = csgp->getNodes(); |
| 129 | const std::vector<DSNode*>& origNodes = funcTDGraph.getNodes(); |
| 130 | assert(csgNodes.size() == origNodes.size()); |
| 131 | for (unsigned i=0, N = csgNodes.size(); i < N; ++i) |
| 132 | { |
| 133 | if (csgNodes[i]->isModified()) |
| 134 | callModRefInfo->setNodeIsMod(getNodeId(origNodes[i])); |
| 135 | if (csgNodes[i]->isRead()) |
| 136 | callModRefInfo->setNodeIsRef(getNodeId(origNodes[i])); |
| 137 | } |
| 138 | } |
| 139 | |
| 140 | |
| 141 | // Print the results of the pass. |
| 142 | // Currently this just prints bit-vectors and is not very readable. |
| 143 | // |
| 144 | void FunctionModRefInfo::print(std::ostream &O) const |
| 145 | { |
| 146 | O << "---------- Mod/ref information for function " |
| 147 | << F.getName() << "---------- \n\n"; |
| 148 | |
| 149 | O << "Mod/ref info for function body:\n"; |
| 150 | funcModRefInfo.print(O); |
| 151 | |
| 152 | for (std::map<const CallInst*, ModRefInfo*>::const_iterator |
| 153 | CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end(); |
| 154 | CI != CE; ++CI) |
| 155 | { |
| 156 | O << "Mod/ref info for call site " << CI->first << ":\n"; |
| 157 | CI->second->print(O); |
| 158 | } |
| 159 | |
| 160 | O << "\n"; |
| 161 | } |
| 162 | |
| 163 | void FunctionModRefInfo::dump() const |
| 164 | { |
| 165 | print(std::cerr); |
| 166 | } |
| 167 | |
| 168 | |
| 169 | //---------------------------------------------------------------------------- |
| 170 | // class IPModRef: An interprocedural pass that computes IP Mod/Ref info. |
| 171 | //---------------------------------------------------------------------------- |
| 172 | |
| 173 | |
| 174 | // Free the FunctionModRefInfo objects cached in funcToModRefInfoMap. |
| 175 | // |
| 176 | void IPModRef::releaseMemory() |
| 177 | { |
| 178 | for(std::map<const Function*, FunctionModRefInfo*>::iterator |
| 179 | I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I) |
| 180 | delete(I->second); |
| 181 | |
| 182 | // Clear map so memory is not re-released if we are called again |
| 183 | funcToModRefInfoMap.clear(); |
| 184 | } |
| 185 | |
| 186 | |
| 187 | // Run the "interprocedural" pass on each function. This needs to do |
| 188 | // NO real interprocedural work because all that has been done the |
| 189 | // data structure analysis. |
| 190 | // |
| 191 | bool IPModRef::run(Module &theModule) |
| 192 | { |
| 193 | M = &theModule; |
| 194 | for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) |
| 195 | if (! FI->isExternal()) |
| 196 | getFuncInfo(*FI, /*computeIfMissing*/ true); |
| 197 | return true; |
| 198 | } |
| 199 | |
| 200 | |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame^] | 201 | FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func, |
| 202 | bool computeIfMissing) |
| 203 | { |
| 204 | FunctionModRefInfo*& funcInfo = funcToModRefInfoMap[&func]; |
| 205 | assert (funcInfo != NULL || computeIfMissing); |
| 206 | if (funcInfo == NULL && computeIfMissing) |
| 207 | { // Create a new FunctionModRefInfo object |
| 208 | funcInfo = new FunctionModRefInfo(func, // inserts into map |
| 209 | getAnalysis<TDDataStructures>().getDSGraph(func), |
| 210 | getAnalysis<LocalDataStructures>().getDSGraph(func)); |
| 211 | funcInfo->computeModRef(func); // computes the mod/ref info |
| 212 | } |
| 213 | return *funcInfo; |
| 214 | } |
| 215 | |
| 216 | // getAnalysisUsage - This pass requires top-down data structure graphs. |
| 217 | // It modifies nothing. |
| 218 | // |
| 219 | void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const { |
| 220 | AU.setPreservesAll(); |
| 221 | AU.addRequired<LocalDataStructures>(); |
| 222 | AU.addRequired<TDDataStructures>(); |
| 223 | } |
| 224 | |
| 225 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 226 | void IPModRef::print(std::ostream &O) const |
| 227 | { |
| 228 | O << "\n========== Results of Interprocedural Mod/Ref Analysis ==========\n"; |
| 229 | |
| 230 | for (std::map<const Function*, FunctionModRefInfo*>::const_iterator |
| 231 | mapI = funcToModRefInfoMap.begin(), mapE = funcToModRefInfoMap.end(); |
| 232 | mapI != mapE; ++mapI) |
| 233 | mapI->second->print(O); |
| 234 | |
| 235 | O << "\n"; |
| 236 | } |
| 237 | |
| 238 | |
| 239 | void IPModRef::dump() const |
| 240 | { |
| 241 | print(std::cerr); |
| 242 | } |