Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 1 | //===- IPModRef.cpp - Compute IP Mod/Ref information ------------*- C++ -*-===// |
John Criswell | b576c94 | 2003-10-20 19:43:21 +0000 | [diff] [blame^] | 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file was developed by the LLVM research group and is distributed under |
| 6 | // the University of Illinois Open Source License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 9 | // |
| 10 | // See high-level comments in include/llvm/Analysis/IPModRef.h |
| 11 | // |
| 12 | //===----------------------------------------------------------------------===// |
| 13 | |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame] | 14 | #include "llvm/Analysis/IPModRef.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 15 | #include "llvm/Analysis/DataStructure.h" |
| 16 | #include "llvm/Analysis/DSGraph.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 17 | #include "llvm/Module.h" |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 18 | #include "llvm/Function.h" |
| 19 | #include "llvm/iMemory.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 20 | #include "llvm/iOther.h" |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 21 | #include "Support/Statistic.h" |
| 22 | #include "Support/STLExtras.h" |
| 23 | #include "Support/StringExtras.h" |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 24 | #include <vector> |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 25 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 26 | //---------------------------------------------------------------------------- |
| 27 | // Private constants and data |
| 28 | //---------------------------------------------------------------------------- |
| 29 | |
| 30 | static RegisterAnalysis<IPModRef> |
| 31 | Z("ipmodref", "Interprocedural mod/ref analysis"); |
| 32 | |
| 33 | |
| 34 | //---------------------------------------------------------------------------- |
| 35 | // class ModRefInfo |
| 36 | //---------------------------------------------------------------------------- |
| 37 | |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 38 | void ModRefInfo::print(std::ostream &O, |
| 39 | const std::string& sprefix) const |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 40 | { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 41 | O << sprefix << "Modified nodes = " << modNodeSet; |
| 42 | O << sprefix << "Referenced nodes = " << refNodeSet; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 43 | } |
| 44 | |
| 45 | void ModRefInfo::dump() const |
| 46 | { |
| 47 | print(std::cerr); |
| 48 | } |
| 49 | |
| 50 | //---------------------------------------------------------------------------- |
| 51 | // class FunctionModRefInfo |
| 52 | //---------------------------------------------------------------------------- |
| 53 | |
| 54 | |
| 55 | // This constructor computes a node numbering for the TD graph. |
| 56 | // |
| 57 | FunctionModRefInfo::FunctionModRefInfo(const Function& func, |
Chris Lattner | 2110808 | 2002-11-06 19:07:13 +0000 | [diff] [blame] | 58 | IPModRef& ipmro, |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 59 | DSGraph* tdgClone) |
Chris Lattner | 2110808 | 2002-11-06 19:07:13 +0000 | [diff] [blame] | 60 | : F(func), IPModRefObj(ipmro), |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 61 | funcTDGraph(tdgClone), |
| 62 | funcModRefInfo(tdgClone->getGraphSize()) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 63 | { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 64 | for (unsigned i=0, N = funcTDGraph->getGraphSize(); i < N; ++i) |
| 65 | NodeIds[funcTDGraph->getNodes()[i]] = i; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 66 | } |
| 67 | |
| 68 | |
| 69 | FunctionModRefInfo::~FunctionModRefInfo() |
| 70 | { |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 71 | for(std::map<const Instruction*, ModRefInfo*>::iterator |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 72 | I=callSiteModRefInfo.begin(), E=callSiteModRefInfo.end(); I != E; ++I) |
| 73 | delete(I->second); |
| 74 | |
| 75 | // Empty map just to make problems easier to track down |
| 76 | callSiteModRefInfo.clear(); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 77 | |
| 78 | delete funcTDGraph; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 79 | } |
| 80 | |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame] | 81 | unsigned FunctionModRefInfo::getNodeId(const Value* value) const { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 82 | return getNodeId(funcTDGraph->getNodeForValue(const_cast<Value*>(value)) |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame] | 83 | .getNode()); |
| 84 | } |
| 85 | |
| 86 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 87 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 88 | // Compute Mod/Ref bit vectors for the entire function. |
| 89 | // These are simply copies of the Read/Write flags from the nodes of |
| 90 | // the top-down DS graph. |
| 91 | // |
| 92 | void FunctionModRefInfo::computeModRef(const Function &func) |
| 93 | { |
| 94 | // Mark all nodes in the graph that are marked MOD as being mod |
| 95 | // and all those marked REF as being ref. |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 96 | for (unsigned i = 0, N = funcTDGraph->getGraphSize(); i < N; ++i) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 97 | { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 98 | if (funcTDGraph->getNodes()[i]->isModified()) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 99 | funcModRefInfo.setNodeIsMod(i); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 100 | if (funcTDGraph->getNodes()[i]->isRead()) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 101 | funcModRefInfo.setNodeIsRef(i); |
| 102 | } |
| 103 | |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 104 | // Compute the Mod/Ref info for all call sites within the function. |
| 105 | // The call sites are recorded in the TD graph. |
| 106 | const std::vector<DSCallSite>& callSites = funcTDGraph->getFunctionCalls(); |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 107 | for (unsigned i = 0, N = callSites.size(); i < N; ++i) |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 108 | computeModRef(callSites[i].getCallSite()); |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 109 | } |
| 110 | |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 111 | |
Chris Lattner | 268748a | 2002-11-06 19:38:43 +0000 | [diff] [blame] | 112 | // ResolveCallSiteModRefInfo - This method performs the following actions: |
| 113 | // |
| 114 | // 1. It clones the top-down graph for the current function |
| 115 | // 2. It clears all of the mod/ref bits in the cloned graph |
| 116 | // 3. It then merges the bottom-up graph(s) for the specified call-site into |
| 117 | // the clone (bringing new mod/ref bits). |
Chris Lattner | 4476ceb | 2002-11-06 19:59:33 +0000 | [diff] [blame] | 118 | // 4. It returns the clone, and a mapping of nodes from the original TDGraph to |
| 119 | // the cloned graph with Mod/Ref info for the callsite. |
Chris Lattner | 268748a | 2002-11-06 19:38:43 +0000 | [diff] [blame] | 120 | // |
| 121 | // NOTE: Because this clones a dsgraph and returns it, the caller is responsible |
| 122 | // for deleting the returned graph! |
Chris Lattner | ed8e649 | 2002-11-07 07:12:23 +0000 | [diff] [blame] | 123 | // NOTE: This method may return a null pointer if it is unable to determine the |
| 124 | // requested information (because the call site calls an external |
| 125 | // function or we cannot determine the complete set of functions invoked). |
Chris Lattner | 268748a | 2002-11-06 19:38:43 +0000 | [diff] [blame] | 126 | // |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 127 | DSGraph* FunctionModRefInfo::ResolveCallSiteModRefInfo(CallSite CS, |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 128 | hash_map<const DSNode*, DSNodeHandle> &NodeMap) |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 129 | { |
| 130 | // Step #0: Quick check if we are going to fail anyway: avoid |
| 131 | // all the graph cloning and map copying in steps #1 and #2. |
| 132 | // |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 133 | if (const Function *F = CS.getCalledFunction()) { |
| 134 | if (F->isExternal()) |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 135 | return 0; // We cannot compute Mod/Ref info for this callsite... |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 136 | } else { |
| 137 | // Eventually, should check here if any callee is external. |
| 138 | // For now we are not handling this case anyway. |
| 139 | std::cerr << "IP Mod/Ref indirect call not implemented yet: " |
| 140 | << "Being conservative\n"; |
| 141 | return 0; // We cannot compute Mod/Ref info for this callsite... |
| 142 | } |
Chris Lattner | 268748a | 2002-11-06 19:38:43 +0000 | [diff] [blame] | 143 | |
Chris Lattner | 4476ceb | 2002-11-06 19:59:33 +0000 | [diff] [blame] | 144 | // Step #1: Clone the top-down graph... |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 145 | DSGraph *Result = new DSGraph(*funcTDGraph, NodeMap); |
Chris Lattner | 4476ceb | 2002-11-06 19:59:33 +0000 | [diff] [blame] | 146 | |
| 147 | // Step #2: Clear Mod/Ref information... |
| 148 | Result->maskNodeTypes(~(DSNode::Modified | DSNode::Read)); |
| 149 | |
Chris Lattner | ed8e649 | 2002-11-07 07:12:23 +0000 | [diff] [blame] | 150 | // Step #3: clone the bottom up graphs for the callees into the caller graph |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 151 | if (Function *F = CS.getCalledFunction()) |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 152 | { |
| 153 | assert(!F->isExternal()); |
| 154 | |
| 155 | // Build up a DSCallSite for our invocation point here... |
| 156 | |
| 157 | // If the call returns a value, make sure to merge the nodes... |
| 158 | DSNodeHandle RetVal; |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 159 | if (DS::isPointerType(CS.getInstruction()->getType())) |
| 160 | RetVal = Result->getNodeForValue(CS.getInstruction()); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 161 | |
| 162 | // Populate the arguments list... |
| 163 | std::vector<DSNodeHandle> Args; |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 164 | for (CallSite::arg_iterator I = CS.arg_begin(), E = CS.arg_end(); |
| 165 | I != E; ++I) |
| 166 | if (DS::isPointerType((*I)->getType())) |
| 167 | Args.push_back(Result->getNodeForValue(*I)); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 168 | |
| 169 | // Build the call site... |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 170 | DSCallSite CS(CS, RetVal, F, Args); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 171 | |
| 172 | // Perform the merging now of the graph for the callee, which will |
| 173 | // come with mod/ref bits set... |
Chris Lattner | a321b04 | 2003-06-30 03:14:54 +0000 | [diff] [blame] | 174 | Result->mergeInGraph(CS, *F, IPModRefObj.getBUDSGraph(*F), |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 175 | DSGraph::StripAllocaBit |
| 176 | | DSGraph::DontCloneCallNodes |
| 177 | | DSGraph::DontCloneAuxCallNodes); |
Chris Lattner | ed8e649 | 2002-11-07 07:12:23 +0000 | [diff] [blame] | 178 | } |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 179 | else |
| 180 | assert(0 && "See error message"); |
Chris Lattner | 268748a | 2002-11-06 19:38:43 +0000 | [diff] [blame] | 181 | |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 182 | // Remove dead nodes aggressively to match the caller's original graph. |
Chris Lattner | 381977d | 2003-01-23 22:06:33 +0000 | [diff] [blame] | 183 | Result->removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Chris Lattner | ed8e649 | 2002-11-07 07:12:23 +0000 | [diff] [blame] | 184 | |
| 185 | // Step #4: Return the clone + the mapping (by ref) |
Chris Lattner | 268748a | 2002-11-06 19:38:43 +0000 | [diff] [blame] | 186 | return Result; |
| 187 | } |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 188 | |
| 189 | // Compute Mod/Ref bit vectors for a single call site. |
| 190 | // These are copies of the Read/Write flags from the nodes of |
Misha Brukman | 2f2d065 | 2003-09-11 18:14:24 +0000 | [diff] [blame] | 191 | // the graph produced by clearing all flags in the caller's TD graph |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 192 | // and then inlining the callee's BU graph into the caller's TD graph. |
| 193 | // |
| 194 | void |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 195 | FunctionModRefInfo::computeModRef(CallSite CS) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 196 | { |
| 197 | // Allocate the mod/ref info for the call site. Bits automatically cleared. |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 198 | ModRefInfo* callModRefInfo = new ModRefInfo(funcTDGraph->getGraphSize()); |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 199 | callSiteModRefInfo[CS.getInstruction()] = callModRefInfo; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 200 | |
| 201 | // Get a copy of the graph for the callee with the callee inlined |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 202 | hash_map<const DSNode*, DSNodeHandle> NodeMap; |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 203 | DSGraph* csgp = ResolveCallSiteModRefInfo(CS, NodeMap); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 204 | if (!csgp) |
| 205 | { // Callee's side effects are unknown: mark all nodes Mod and Ref. |
| 206 | // Eventually this should only mark nodes visible to the callee, i.e., |
| 207 | // exclude stack variables not reachable from any outgoing argument |
| 208 | // or any global. |
| 209 | callModRefInfo->getModSet().set(); |
| 210 | callModRefInfo->getRefSet().set(); |
| 211 | return; |
| 212 | } |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 213 | |
| 214 | // For all nodes in the graph, extract the mod/ref information |
| 215 | const std::vector<DSNode*>& csgNodes = csgp->getNodes(); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 216 | const std::vector<DSNode*>& origNodes = funcTDGraph->getNodes(); |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 217 | assert(csgNodes.size() == origNodes.size()); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 218 | for (unsigned i=0, N = origNodes.size(); i < N; ++i) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 219 | { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 220 | DSNode* csgNode = NodeMap[origNodes[i]].getNode(); |
| 221 | assert(csgNode && "Inlined and original graphs do not correspond!"); |
| 222 | if (csgNode->isModified()) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 223 | callModRefInfo->setNodeIsMod(getNodeId(origNodes[i])); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 224 | if (csgNode->isRead()) |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 225 | callModRefInfo->setNodeIsRef(getNodeId(origNodes[i])); |
| 226 | } |
Chris Lattner | e83cb53 | 2002-11-07 05:00:35 +0000 | [diff] [blame] | 227 | |
| 228 | // Drop nodemap before we delete the graph... |
| 229 | NodeMap.clear(); |
Chris Lattner | 268748a | 2002-11-06 19:38:43 +0000 | [diff] [blame] | 230 | delete csgp; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 231 | } |
| 232 | |
| 233 | |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 234 | class DSGraphPrintHelper { |
| 235 | const DSGraph& tdGraph; |
| 236 | std::vector<std::vector<const Value*> > knownValues; // identifiable objects |
| 237 | |
| 238 | public: |
| 239 | /*ctor*/ DSGraphPrintHelper(const FunctionModRefInfo& fmrInfo) |
| 240 | : tdGraph(fmrInfo.getFuncGraph()) |
| 241 | { |
| 242 | knownValues.resize(tdGraph.getGraphSize()); |
| 243 | |
| 244 | // For every identifiable value, save Value pointer in knownValues[i] |
Chris Lattner | 41c04f7 | 2003-02-01 04:52:08 +0000 | [diff] [blame] | 245 | for (hash_map<Value*, DSNodeHandle>::const_iterator |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 246 | I = tdGraph.getScalarMap().begin(), |
| 247 | E = tdGraph.getScalarMap().end(); I != E; ++I) |
| 248 | if (isa<GlobalValue>(I->first) || |
| 249 | isa<Argument>(I->first) || |
| 250 | isa<LoadInst>(I->first) || |
| 251 | isa<AllocaInst>(I->first) || |
| 252 | isa<MallocInst>(I->first)) |
| 253 | { |
| 254 | unsigned nodeId = fmrInfo.getNodeId(I->second.getNode()); |
| 255 | knownValues[nodeId].push_back(I->first); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | void printValuesInBitVec(std::ostream &O, const BitSetVector& bv) const |
| 260 | { |
| 261 | assert(bv.size() == knownValues.size()); |
| 262 | |
| 263 | if (bv.none()) |
| 264 | { // No bits are set: just say so and return |
| 265 | O << "\tNONE.\n"; |
| 266 | return; |
| 267 | } |
| 268 | |
| 269 | if (bv.all()) |
| 270 | { // All bits are set: just say so and return |
| 271 | O << "\tALL GRAPH NODES.\n"; |
| 272 | return; |
| 273 | } |
| 274 | |
| 275 | for (unsigned i=0, N=bv.size(); i < N; ++i) |
| 276 | if (bv.test(i)) |
| 277 | { |
| 278 | O << "\tNode# " << i << " : "; |
| 279 | if (! knownValues[i].empty()) |
| 280 | for (unsigned j=0, NV=knownValues[i].size(); j < NV; j++) |
| 281 | { |
| 282 | const Value* V = knownValues[i][j]; |
| 283 | |
| 284 | if (isa<GlobalValue>(V)) O << "(Global) "; |
| 285 | else if (isa<Argument>(V)) O << "(Target of FormalParm) "; |
| 286 | else if (isa<LoadInst>(V)) O << "(Target of LoadInst ) "; |
| 287 | else if (isa<AllocaInst>(V)) O << "(Target of AllocaInst) "; |
| 288 | else if (isa<MallocInst>(V)) O << "(Target of MallocInst) "; |
| 289 | |
| 290 | if (V->hasName()) O << V->getName(); |
| 291 | else if (isa<Instruction>(V)) O << *V; |
| 292 | else O << "(Value*) 0x" << (void*) V; |
| 293 | |
| 294 | O << std::string((j < NV-1)? "; " : "\n"); |
| 295 | } |
| 296 | else |
| 297 | tdGraph.getNodes()[i]->print(O, /*graph*/ NULL); |
| 298 | } |
| 299 | } |
| 300 | }; |
| 301 | |
| 302 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 303 | // Print the results of the pass. |
| 304 | // Currently this just prints bit-vectors and is not very readable. |
| 305 | // |
| 306 | void FunctionModRefInfo::print(std::ostream &O) const |
| 307 | { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 308 | DSGraphPrintHelper DPH(*this); |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 309 | |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 310 | O << "========== Mod/ref information for function " |
| 311 | << F.getName() << "========== \n\n"; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 312 | |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 313 | // First: Print Globals and Locals modified anywhere in the function. |
| 314 | // |
| 315 | O << " -----Mod/Ref in the body of function " << F.getName()<< ":\n"; |
| 316 | |
| 317 | O << " --Objects modified in the function body:\n"; |
| 318 | DPH.printValuesInBitVec(O, funcModRefInfo.getModSet()); |
| 319 | |
| 320 | O << " --Objects referenced in the function body:\n"; |
| 321 | DPH.printValuesInBitVec(O, funcModRefInfo.getRefSet()); |
| 322 | |
| 323 | O << " --Mod and Ref vectors for the nodes listed above:\n"; |
| 324 | funcModRefInfo.print(O, "\t"); |
| 325 | |
| 326 | O << "\n"; |
| 327 | |
| 328 | // Second: Print Globals and Locals modified at each call site in function |
| 329 | // |
Chris Lattner | 808a7ae | 2003-09-20 16:34:13 +0000 | [diff] [blame] | 330 | for (std::map<const Instruction *, ModRefInfo*>::const_iterator |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 331 | CI = callSiteModRefInfo.begin(), CE = callSiteModRefInfo.end(); |
| 332 | CI != CE; ++CI) |
| 333 | { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 334 | O << " ----Mod/Ref information for call site\n" << CI->first; |
| 335 | |
| 336 | O << " --Objects modified at call site:\n"; |
| 337 | DPH.printValuesInBitVec(O, CI->second->getModSet()); |
| 338 | |
| 339 | O << " --Objects referenced at call site:\n"; |
| 340 | DPH.printValuesInBitVec(O, CI->second->getRefSet()); |
| 341 | |
| 342 | O << " --Mod and Ref vectors for the nodes listed above:\n"; |
| 343 | CI->second->print(O, "\t"); |
| 344 | |
| 345 | O << "\n"; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 346 | } |
| 347 | |
| 348 | O << "\n"; |
| 349 | } |
| 350 | |
| 351 | void FunctionModRefInfo::dump() const |
| 352 | { |
| 353 | print(std::cerr); |
| 354 | } |
| 355 | |
| 356 | |
| 357 | //---------------------------------------------------------------------------- |
| 358 | // class IPModRef: An interprocedural pass that computes IP Mod/Ref info. |
| 359 | //---------------------------------------------------------------------------- |
| 360 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 361 | // Free the FunctionModRefInfo objects cached in funcToModRefInfoMap. |
| 362 | // |
| 363 | void IPModRef::releaseMemory() |
| 364 | { |
| 365 | for(std::map<const Function*, FunctionModRefInfo*>::iterator |
| 366 | I=funcToModRefInfoMap.begin(), E=funcToModRefInfoMap.end(); I != E; ++I) |
| 367 | delete(I->second); |
| 368 | |
| 369 | // Clear map so memory is not re-released if we are called again |
| 370 | funcToModRefInfoMap.clear(); |
| 371 | } |
| 372 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 373 | // Run the "interprocedural" pass on each function. This needs to do |
| 374 | // NO real interprocedural work because all that has been done the |
| 375 | // data structure analysis. |
| 376 | // |
| 377 | bool IPModRef::run(Module &theModule) |
| 378 | { |
| 379 | M = &theModule; |
Chris Lattner | e83cb53 | 2002-11-07 05:00:35 +0000 | [diff] [blame] | 380 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 381 | for (Module::const_iterator FI = M->begin(), FE = M->end(); FI != FE; ++FI) |
| 382 | if (! FI->isExternal()) |
| 383 | getFuncInfo(*FI, /*computeIfMissing*/ true); |
| 384 | return true; |
| 385 | } |
| 386 | |
| 387 | |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame] | 388 | FunctionModRefInfo& IPModRef::getFuncInfo(const Function& func, |
| 389 | bool computeIfMissing) |
| 390 | { |
| 391 | FunctionModRefInfo*& funcInfo = funcToModRefInfoMap[&func]; |
| 392 | assert (funcInfo != NULL || computeIfMissing); |
Chris Lattner | 2110808 | 2002-11-06 19:07:13 +0000 | [diff] [blame] | 393 | if (funcInfo == NULL) |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 394 | { // Create a new FunctionModRefInfo object. |
| 395 | // Clone the top-down graph and remove any dead nodes first, because |
| 396 | // otherwise original and merged graphs will not match. |
| 397 | // The memory for this graph clone will be freed by FunctionModRefInfo. |
| 398 | DSGraph* funcTDGraph = |
| 399 | new DSGraph(getAnalysis<TDDataStructures>().getDSGraph(func)); |
Chris Lattner | 381977d | 2003-01-23 22:06:33 +0000 | [diff] [blame] | 400 | funcTDGraph->removeDeadNodes(DSGraph::KeepUnreachableGlobals); |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 401 | |
| 402 | funcInfo = new FunctionModRefInfo(func, *this, funcTDGraph); //auto-insert |
| 403 | funcInfo->computeModRef(func); // computes the mod/ref info |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame] | 404 | } |
| 405 | return *funcInfo; |
| 406 | } |
| 407 | |
Chris Lattner | ed8e649 | 2002-11-07 07:12:23 +0000 | [diff] [blame] | 408 | /// getBUDSGraph - This method returns the BU data structure graph for F through |
| 409 | /// the use of the BUDataStructures object. |
| 410 | /// |
| 411 | const DSGraph &IPModRef::getBUDSGraph(const Function &F) { |
| 412 | return getAnalysis<BUDataStructures>().getDSGraph(F); |
| 413 | } |
| 414 | |
| 415 | |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame] | 416 | // getAnalysisUsage - This pass requires top-down data structure graphs. |
| 417 | // It modifies nothing. |
| 418 | // |
| 419 | void IPModRef::getAnalysisUsage(AnalysisUsage &AU) const { |
| 420 | AU.setPreservesAll(); |
| 421 | AU.addRequired<LocalDataStructures>(); |
Chris Lattner | 2110808 | 2002-11-06 19:07:13 +0000 | [diff] [blame] | 422 | AU.addRequired<BUDataStructures>(); |
Chris Lattner | fc92824 | 2002-11-06 18:38:18 +0000 | [diff] [blame] | 423 | AU.addRequired<TDDataStructures>(); |
| 424 | } |
| 425 | |
| 426 | |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 427 | void IPModRef::print(std::ostream &O) const |
| 428 | { |
Vikram S. Adve | 9a96428 | 2002-11-27 17:37:46 +0000 | [diff] [blame] | 429 | O << "\nRESULTS OF INTERPROCEDURAL MOD/REF ANALYSIS:\n\n"; |
Vikram S. Adve | 895c0bd | 2002-11-06 17:02:03 +0000 | [diff] [blame] | 430 | |
| 431 | for (std::map<const Function*, FunctionModRefInfo*>::const_iterator |
| 432 | mapI = funcToModRefInfoMap.begin(), mapE = funcToModRefInfoMap.end(); |
| 433 | mapI != mapE; ++mapI) |
| 434 | mapI->second->print(O); |
| 435 | |
| 436 | O << "\n"; |
| 437 | } |
| 438 | |
| 439 | |
| 440 | void IPModRef::dump() const |
| 441 | { |
| 442 | print(std::cerr); |
| 443 | } |