Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 1 | //===- ComputeLocal.cpp - Compute a local data structure graph for a fn ---===// |
| 2 | // |
| 3 | // Compute the local version of the data structure graph for a function. The |
| 4 | // external interface to this file is the DSGraph constructor. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 8 | #include "llvm/Function.h" |
| 9 | #include "llvm/iMemory.h" |
| 10 | #include "llvm/iTerminators.h" |
| 11 | #include "llvm/iPHINode.h" |
| 12 | #include "llvm/iOther.h" |
| 13 | #include "llvm/Constants.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 14 | #include "llvm/GlobalVariable.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 15 | #include "llvm/DerivedTypes.h" |
| 16 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 17 | #include "llvm/Analysis/DataStructure.h" // FIXME: |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 18 | using std::map; |
| 19 | using std::vector; |
| 20 | |
| 21 | //===----------------------------------------------------------------------===// |
| 22 | // GraphBuilder Class |
| 23 | //===----------------------------------------------------------------------===// |
| 24 | // |
| 25 | // This class is the builder class that constructs the local data structure |
| 26 | // graph by performing a single pass over the function in question. |
| 27 | // |
| 28 | |
| 29 | namespace { |
| 30 | class GraphBuilder : InstVisitor<GraphBuilder> { |
| 31 | DSGraph &G; |
| 32 | vector<DSNode*> &Nodes; |
| 33 | DSNodeHandle &RetNode; // Node that gets returned... |
| 34 | map<Value*, DSNodeHandle> &ValueMap; |
| 35 | vector<vector<DSNodeHandle> > &FunctionCalls; |
| 36 | |
| 37 | public: |
| 38 | GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode, |
| 39 | map<Value*, DSNodeHandle> &vm, |
| 40 | vector<vector<DSNodeHandle> > &fc) |
| 41 | : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 42 | |
| 43 | // Create scalar nodes for all pointer arguments... |
| 44 | for (Function::aiterator I = G.getFunction().abegin(), |
| 45 | E = G.getFunction().aend(); I != E; ++I) |
| 46 | if (isa<PointerType>(I->getType())) |
| 47 | getValueNode(*I); |
| 48 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 49 | visit(G.getFunction()); // Single pass over the function |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 50 | G.removeDeadNodes(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 51 | } |
| 52 | |
| 53 | private: |
| 54 | // Visitor functions, used to handle each instruction type we encounter... |
| 55 | friend class InstVisitor<GraphBuilder>; |
| 56 | void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); } |
| 57 | void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);} |
| 58 | void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT); |
| 59 | |
| 60 | void visitPHINode(PHINode &PN); |
| 61 | |
| 62 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 63 | void visitReturnInst(ReturnInst &RI); |
| 64 | void visitLoadInst(LoadInst &LI); |
| 65 | void visitStoreInst(StoreInst &SI); |
| 66 | void visitCallInst(CallInst &CI); |
| 67 | void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored |
| 68 | void visitFreeInst(FreeInst &FI) {} // Ignore free instructions |
| 69 | void visitInstruction(Instruction &I) { |
| 70 | #ifndef NDEBUG |
| 71 | bool bad = isa<PointerType>(I.getType()); |
| 72 | for (Instruction::op_iterator i = I.op_begin(), E = I.op_end(); i!=E; ++i) |
| 73 | bad |= isa<PointerType>(i->get()->getType()); |
| 74 | if (bad) { |
| 75 | std::cerr << "\n\n\nUNKNOWN PTR INSTRUCTION type: " << I << "\n\n\n"; |
| 76 | assert(0 && "Cannot proceed"); |
| 77 | } |
| 78 | #endif |
| 79 | } |
| 80 | |
| 81 | private: |
| 82 | // Helper functions used to implement the visitation functions... |
| 83 | |
| 84 | // createNode - Create a new DSNode, ensuring that it is properly added to |
| 85 | // the graph. |
| 86 | // |
| 87 | DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty); |
| 88 | |
| 89 | // getValueNode - Return a DSNode that corresponds the the specified LLVM |
| 90 | // value. This either returns the already existing node, or creates a new |
| 91 | // one and adds it to the graph, if none exists. |
| 92 | // |
| 93 | DSNode *getValueNode(Value &V); |
| 94 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 95 | // getGlobalNode - Just like getValueNode, except the global node itself is |
| 96 | // returned, not a scalar node pointing to a global. |
| 97 | // |
| 98 | DSNode *getGlobalNode(GlobalValue &V); |
| 99 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 100 | // getLink - This method is used to either return the specified link in the |
| 101 | // specified node if one exists. If a link does not already exist (it's |
| 102 | // null), then we create a new node, link it, then return it. |
| 103 | // |
| 104 | DSNode *getLink(DSNode *Node, unsigned Link); |
| 105 | |
| 106 | // getSubscriptedNode - Perform the basic getelementptr functionality that |
| 107 | // must be factored out of gep, load and store while they are all MAI's. |
| 108 | // |
| 109 | DSNode *getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 110 | }; |
| 111 | } |
| 112 | |
| 113 | //===----------------------------------------------------------------------===// |
| 114 | // DSGraph constructor - Simply use the GraphBuilder to construct the local |
| 115 | // graph. |
| 116 | DSGraph::DSGraph(Function &F) : Func(F), RetNode(0) { |
| 117 | // Use the graph builder to construct the local version of the graph |
| 118 | GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 119 | markIncompleteNodes(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 120 | } |
| 121 | |
| 122 | |
| 123 | //===----------------------------------------------------------------------===// |
| 124 | // Helper method implementations... |
| 125 | // |
| 126 | |
| 127 | |
| 128 | // createNode - Create a new DSNode, ensuring that it is properly added to the |
| 129 | // graph. |
| 130 | // |
| 131 | DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) { |
| 132 | DSNode *N = new DSNode(NodeType, Ty); |
| 133 | Nodes.push_back(N); |
| 134 | return N; |
| 135 | } |
| 136 | |
| 137 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 138 | // getGlobalNode - Just like getValueNode, except the global node itself is |
| 139 | // returned, not a scalar node pointing to a global. |
| 140 | // |
| 141 | DSNode *GraphBuilder::getGlobalNode(GlobalValue &V) { |
| 142 | DSNodeHandle &NH = ValueMap[&V]; |
| 143 | if (NH) return NH; // Already have a node? Just return it... |
| 144 | |
| 145 | // Create a new global node for this global variable... |
| 146 | DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType()); |
| 147 | G->addGlobal(&V); |
| 148 | |
| 149 | // If this node has outgoing edges, make sure to recycle the same node for |
| 150 | // each use. For functions and other global variables, this is unneccesary, |
| 151 | // so avoid excessive merging by cloning these nodes on demand. |
| 152 | // |
| 153 | NH = G; |
| 154 | return G; |
| 155 | } |
| 156 | |
| 157 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 158 | // getValueNode - Return a DSNode that corresponds the the specified LLVM value. |
| 159 | // This either returns the already existing node, or creates a new one and adds |
| 160 | // it to the graph, if none exists. |
| 161 | // |
| 162 | DSNode *GraphBuilder::getValueNode(Value &V) { |
| 163 | assert(isa<PointerType>(V.getType()) && "Should only use pointer scalars!"); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 164 | if (!isa<GlobalValue>(V)) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 165 | DSNodeHandle &NH = ValueMap[&V]; |
| 166 | if (NH) return NH; // Already have a node? Just return it... |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 167 | } |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 168 | |
| 169 | // Otherwise we need to create a new scalar node... |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 170 | DSNode *N = createNode(DSNode::ScalarNode, V.getType()); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 171 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 172 | // If this is a global value, create the global pointed to. |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 173 | if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 174 | DSNode *G = getGlobalNode(*GV); |
| 175 | N->addEdgeTo(G); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 176 | } else { |
| 177 | ValueMap[&V] = N; |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 178 | } |
| 179 | |
| 180 | return N; |
| 181 | } |
| 182 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 183 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 184 | // getLink - This method is used to either return the specified link in the |
| 185 | // specified node if one exists. If a link does not already exist (it's |
| 186 | // null), then we create a new node, link it, then return it. |
| 187 | // |
| 188 | DSNode *GraphBuilder::getLink(DSNode *Node, unsigned Link) { |
| 189 | assert(Link < Node->getNumLinks() && "Link accessed out of range!"); |
| 190 | if (Node->getLink(Link) == 0) { |
| 191 | DSNode::NodeTy NT; |
| 192 | const Type *Ty; |
| 193 | |
| 194 | switch (Node->getType()->getPrimitiveID()) { |
| 195 | case Type::PointerTyID: |
| 196 | Ty = cast<PointerType>(Node->getType())->getElementType(); |
| 197 | NT = DSNode::ShadowNode; |
| 198 | break; |
| 199 | case Type::ArrayTyID: |
| 200 | Ty = cast<ArrayType>(Node->getType())->getElementType(); |
| 201 | NT = DSNode::SubElement; |
| 202 | break; |
| 203 | case Type::StructTyID: |
| 204 | Ty = cast<StructType>(Node->getType())->getContainedType(Link); |
| 205 | NT = DSNode::SubElement; |
| 206 | break; |
| 207 | default: |
| 208 | assert(0 && "Unexpected type to dereference!"); |
| 209 | abort(); |
| 210 | } |
| 211 | |
| 212 | DSNode *New = createNode(NT, Ty); |
| 213 | Node->addEdgeTo(Link, New); |
| 214 | } |
| 215 | |
| 216 | return Node->getLink(Link); |
| 217 | } |
| 218 | |
| 219 | // getSubscriptedNode - Perform the basic getelementptr functionality that must |
| 220 | // be factored out of gep, load and store while they are all MAI's. |
| 221 | // |
| 222 | DSNode *GraphBuilder::getSubscriptedNode(MemAccessInst &MAI, DSNode *Ptr) { |
| 223 | for (unsigned i = MAI.getFirstIndexOperandNumber(), e = MAI.getNumOperands(); |
| 224 | i != e; ++i) |
| 225 | if (MAI.getOperand(i)->getType() == Type::UIntTy) |
| 226 | Ptr = getLink(Ptr, 0); |
| 227 | else if (MAI.getOperand(i)->getType() == Type::UByteTy) |
| 228 | Ptr = getLink(Ptr, cast<ConstantUInt>(MAI.getOperand(i))->getValue()); |
| 229 | |
| 230 | if (MAI.getFirstIndexOperandNumber() == MAI.getNumOperands()) |
| 231 | Ptr = getLink(Ptr, 0); // All MAI's have an implicit 0 if nothing else. |
| 232 | |
| 233 | return Ptr; |
| 234 | } |
| 235 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 236 | //===----------------------------------------------------------------------===// |
| 237 | // Specific instruction type handler implementations... |
| 238 | // |
| 239 | |
| 240 | // Alloca & Malloc instruction implementation - Simply create a new memory |
| 241 | // object, pointing the scalar to it. |
| 242 | // |
| 243 | void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) { |
| 244 | DSNode *Scalar = getValueNode(AI); |
| 245 | DSNode *New = createNode(NodeType, AI.getAllocatedType()); |
| 246 | Scalar->addEdgeTo(New); // Make the scalar point to the new node... |
| 247 | } |
| 248 | |
| 249 | // PHINode - Make the scalar for the PHI node point to all of the things the |
| 250 | // incoming values point to... which effectively causes them to be merged. |
| 251 | // |
| 252 | void GraphBuilder::visitPHINode(PHINode &PN) { |
| 253 | if (!isa<PointerType>(PN.getType())) return; // Only pointer PHIs |
| 254 | |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 255 | DSNode *Scalar = getValueNode(PN); |
| 256 | DSNode *ScalarDest = getLink(Scalar, 0); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 257 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 258 | ScalarDest->mergeWith(getLink(getValueNode(*PN.getIncomingValue(i)), 0)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 259 | } |
| 260 | |
| 261 | void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 262 | DSNode *Ptr = getSubscriptedNode(GEP, getValueNode(*GEP.getOperand(0))); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 263 | getValueNode(GEP)->addEdgeTo(Ptr); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | void GraphBuilder::visitLoadInst(LoadInst &LI) { |
| 267 | if (!isa<PointerType>(LI.getType())) return; // Only pointer PHIs |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 268 | DSNode *Ptr = getSubscriptedNode(LI, getValueNode(*LI.getOperand(0))); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 269 | getValueNode(LI)->addEdgeTo(getLink(Ptr, 0)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 270 | } |
| 271 | |
| 272 | void GraphBuilder::visitStoreInst(StoreInst &SI) { |
| 273 | if (!isa<PointerType>(SI.getOperand(0)->getType())) return; |
| 274 | DSNode *Value = getValueNode(*SI.getOperand(0)); |
| 275 | DSNode *DestPtr = getValueNode(*SI.getOperand(1)); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 276 | getSubscriptedNode(SI, DestPtr)->addEdgeTo(getLink(Value, 0)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void GraphBuilder::visitReturnInst(ReturnInst &RI) { |
| 280 | if (RI.getNumOperands() && isa<PointerType>(RI.getOperand(0)->getType())) { |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 281 | DSNode *Value = getLink(getValueNode(*RI.getOperand(0)), 0); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 282 | Value->mergeWith(RetNode); |
| 283 | RetNode = Value; |
| 284 | } |
| 285 | } |
| 286 | |
| 287 | void GraphBuilder::visitCallInst(CallInst &CI) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 288 | // Add a new function call entry... |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 289 | FunctionCalls.push_back(vector<DSNodeHandle>()); |
| 290 | vector<DSNodeHandle> &Args = FunctionCalls.back(); |
| 291 | |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 292 | // Set up the return value... |
| 293 | if (isa<PointerType>(CI.getType())) |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 294 | Args.push_back(getLink(getValueNode(CI), 0)); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 295 | else |
| 296 | Args.push_back(0); |
| 297 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 298 | unsigned Start = 0; |
| 299 | // Special case for direct call, avoid creating spurious scalar node... |
| 300 | if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0))) { |
| 301 | Args.push_back(getGlobalNode(*GV)); |
| 302 | Start = 1; |
| 303 | } |
| 304 | |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 305 | // Pass the arguments in... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 306 | for (unsigned i = Start, e = CI.getNumOperands(); i != e; ++i) |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 307 | if (isa<PointerType>(CI.getOperand(i)->getType())) |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame^] | 308 | Args.push_back(getLink(getValueNode(*CI.getOperand(i)), 0)); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 309 | } |