Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 1 | //===- Local.cpp - Compute a local data structure graph for a function ----===// |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 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 | c5f21de | 2002-10-02 22:14:38 +0000 | [diff] [blame] | 8 | #include "llvm/Analysis/DSGraph.h" |
Chris Lattner | 055dc2c | 2002-07-18 15:54:42 +0000 | [diff] [blame] | 9 | #include "llvm/Analysis/DataStructure.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 10 | #include "llvm/iMemory.h" |
| 11 | #include "llvm/iTerminators.h" |
| 12 | #include "llvm/iPHINode.h" |
| 13 | #include "llvm/iOther.h" |
| 14 | #include "llvm/Constants.h" |
| 15 | #include "llvm/DerivedTypes.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 16 | #include "llvm/Function.h" |
| 17 | #include "llvm/GlobalVariable.h" |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 18 | #include "llvm/Support/InstVisitor.h" |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 19 | #include "llvm/Target/TargetData.h" |
| 20 | #include "Support/Statistic.h" |
| 21 | |
| 22 | // FIXME: This should eventually be a FunctionPass that is automatically |
| 23 | // aggregated into a Pass. |
| 24 | // |
| 25 | #include "llvm/Module.h" |
| 26 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 27 | using std::map; |
| 28 | using std::vector; |
| 29 | |
Chris Lattner | 97f51a3 | 2002-07-27 01:12:15 +0000 | [diff] [blame] | 30 | static RegisterAnalysis<LocalDataStructures> |
| 31 | X("datastructure", "Local Data Structure Analysis"); |
Chris Lattner | 97f51a3 | 2002-07-27 01:12:15 +0000 | [diff] [blame] | 32 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 33 | using namespace DataStructureAnalysis; |
| 34 | |
| 35 | namespace DataStructureAnalysis { |
| 36 | // FIXME: Do something smarter with target data! |
| 37 | TargetData TD("temp-td"); |
| 38 | unsigned PointerSize(TD.getPointerSize()); |
| 39 | |
| 40 | // isPointerType - Return true if this type is big enough to hold a pointer. |
| 41 | bool isPointerType(const Type *Ty) { |
| 42 | if (isa<PointerType>(Ty)) |
| 43 | return true; |
| 44 | else if (Ty->isPrimitiveType() && Ty->isInteger()) |
| 45 | return Ty->getPrimitiveSize() >= PointerSize; |
| 46 | return false; |
| 47 | } |
| 48 | } |
| 49 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 50 | |
| 51 | namespace { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 52 | //===--------------------------------------------------------------------===// |
| 53 | // GraphBuilder Class |
| 54 | //===--------------------------------------------------------------------===// |
| 55 | // |
| 56 | /// This class is the builder class that constructs the local data structure |
| 57 | /// graph by performing a single pass over the function in question. |
| 58 | /// |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 59 | class GraphBuilder : InstVisitor<GraphBuilder> { |
| 60 | DSGraph &G; |
| 61 | vector<DSNode*> &Nodes; |
| 62 | DSNodeHandle &RetNode; // Node that gets returned... |
| 63 | map<Value*, DSNodeHandle> &ValueMap; |
| 64 | vector<vector<DSNodeHandle> > &FunctionCalls; |
| 65 | |
| 66 | public: |
| 67 | GraphBuilder(DSGraph &g, vector<DSNode*> &nodes, DSNodeHandle &retNode, |
| 68 | map<Value*, DSNodeHandle> &vm, |
| 69 | vector<vector<DSNodeHandle> > &fc) |
| 70 | : G(g), Nodes(nodes), RetNode(retNode), ValueMap(vm), FunctionCalls(fc) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 71 | |
| 72 | // Create scalar nodes for all pointer arguments... |
| 73 | for (Function::aiterator I = G.getFunction().abegin(), |
| 74 | E = G.getFunction().aend(); I != E; ++I) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 75 | if (isPointerType(I->getType())) |
| 76 | getValueDest(*I); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 77 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 78 | visit(G.getFunction()); // Single pass over the function |
Chris Lattner | 2a2c490 | 2002-07-18 18:19:09 +0000 | [diff] [blame] | 79 | |
| 80 | // Not inlining, only eliminate trivially dead nodes. |
| 81 | G.removeTriviallyDeadNodes(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 82 | } |
| 83 | |
| 84 | private: |
| 85 | // Visitor functions, used to handle each instruction type we encounter... |
| 86 | friend class InstVisitor<GraphBuilder>; |
| 87 | void visitMallocInst(MallocInst &MI) { handleAlloc(MI, DSNode::NewNode); } |
| 88 | void visitAllocaInst(AllocaInst &AI) { handleAlloc(AI, DSNode::AllocaNode);} |
| 89 | void handleAlloc(AllocationInst &AI, DSNode::NodeTy NT); |
| 90 | |
| 91 | void visitPHINode(PHINode &PN); |
| 92 | |
| 93 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 94 | void visitReturnInst(ReturnInst &RI); |
| 95 | void visitLoadInst(LoadInst &LI); |
| 96 | void visitStoreInst(StoreInst &SI); |
| 97 | void visitCallInst(CallInst &CI); |
| 98 | void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored |
| 99 | void visitFreeInst(FreeInst &FI) {} // Ignore free instructions |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 100 | void visitCastInst(CastInst &CI); |
| 101 | void visitInstruction(Instruction &I) {} |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 102 | |
| 103 | private: |
| 104 | // Helper functions used to implement the visitation functions... |
| 105 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 106 | /// createNode - Create a new DSNode, ensuring that it is properly added to |
| 107 | /// the graph. |
| 108 | /// |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 109 | DSNode *createNode(DSNode::NodeTy NodeType, const Type *Ty); |
| 110 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 111 | /// getValueNode - Return a DSNode that corresponds the the specified LLVM |
| 112 | /// value. This either returns the already existing node, or creates a new |
| 113 | /// one and adds it to the graph, if none exists. |
| 114 | /// |
| 115 | DSNodeHandle getValueNode(Value &V); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 116 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 117 | /// getValueDest - Return the DSNode that the actual value points to. This |
| 118 | /// is basically the same thing as: getLink(getValueNode(V), 0) |
| 119 | /// |
| 120 | DSNodeHandle &getValueDest(Value &V); |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 121 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 122 | /// getGlobalNode - Just like getValueNode, except the global node itself is |
| 123 | /// returned, not a scalar node pointing to a global. |
| 124 | /// |
| 125 | DSNodeHandle &getGlobalNode(GlobalValue &V); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 126 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 127 | /// getLink - This method is used to return the specified link in the |
| 128 | /// specified node if one exists. If a link does not already exist (it's |
| 129 | /// null), then we create a new node, link it, then return it. We must |
| 130 | /// specify the type of the Node field we are accessing so that we know what |
| 131 | /// type should be linked to if we need to create a new node. |
| 132 | /// |
| 133 | DSNodeHandle &getLink(const DSNodeHandle &Node, unsigned Link, |
| 134 | const Type *FieldTy); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 135 | }; |
| 136 | } |
| 137 | |
| 138 | //===----------------------------------------------------------------------===// |
| 139 | // DSGraph constructor - Simply use the GraphBuilder to construct the local |
| 140 | // graph. |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 141 | DSGraph::DSGraph(Function &F) : Func(&F) { |
| 142 | // Use the graph builder to construct the local version of the graph |
| 143 | GraphBuilder B(*this, Nodes, RetNode, ValueMap, FunctionCalls); |
| 144 | markIncompleteNodes(); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | |
| 148 | //===----------------------------------------------------------------------===// |
| 149 | // Helper method implementations... |
| 150 | // |
| 151 | |
| 152 | |
| 153 | // createNode - Create a new DSNode, ensuring that it is properly added to the |
| 154 | // graph. |
| 155 | // |
| 156 | DSNode *GraphBuilder::createNode(DSNode::NodeTy NodeType, const Type *Ty) { |
| 157 | DSNode *N = new DSNode(NodeType, Ty); |
| 158 | Nodes.push_back(N); |
| 159 | return N; |
| 160 | } |
| 161 | |
| 162 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 163 | // getGlobalNode - Just like getValueNode, except the global node itself is |
| 164 | // returned, not a scalar node pointing to a global. |
| 165 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 166 | DSNodeHandle &GraphBuilder::getGlobalNode(GlobalValue &V) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 167 | DSNodeHandle &NH = ValueMap[&V]; |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 168 | if (NH.getNode()) return NH; // Already have a node? Just return it... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 169 | |
| 170 | // Create a new global node for this global variable... |
| 171 | DSNode *G = createNode(DSNode::GlobalNode, V.getType()->getElementType()); |
| 172 | G->addGlobal(&V); |
| 173 | |
| 174 | // If this node has outgoing edges, make sure to recycle the same node for |
| 175 | // each use. For functions and other global variables, this is unneccesary, |
| 176 | // so avoid excessive merging by cloning these nodes on demand. |
| 177 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 178 | NH.setNode(G); |
| 179 | return NH; |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 180 | } |
| 181 | |
| 182 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 183 | // getValueNode - Return a DSNode that corresponds the the specified LLVM value. |
| 184 | // This either returns the already existing node, or creates a new one and adds |
| 185 | // it to the graph, if none exists. |
| 186 | // |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 187 | DSNodeHandle GraphBuilder::getValueNode(Value &V) { |
| 188 | assert(isPointerType(V.getType()) && "Should only use pointer scalars!"); |
| 189 | // Do not share the pointer value to globals... this would cause way too much |
| 190 | // false merging. |
| 191 | // |
| 192 | DSNodeHandle &NH = ValueMap[&V]; |
| 193 | if (!isa<GlobalValue>(V) && NH.getNode()) |
| 194 | return NH; // Already have a node? Just return it... |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 195 | |
| 196 | // Otherwise we need to create a new scalar node... |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 197 | DSNode *N = createNode(DSNode::ScalarNode, V.getType()); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 198 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 199 | // If this is a global value, create the global pointed to. |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 200 | if (GlobalValue *GV = dyn_cast<GlobalValue>(&V)) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 201 | N->addEdgeTo(0, getGlobalNode(*GV)); |
| 202 | return DSNodeHandle(N, 0); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 203 | } else { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 204 | NH.setOffset(0); |
| 205 | NH.setNode(N); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 206 | } |
| 207 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 208 | return NH; |
| 209 | } |
| 210 | |
| 211 | /// getValueDest - Return the DSNode that the actual value points to. This |
| 212 | /// is basically the same thing as: getLink(getValueNode(V), 0) |
| 213 | /// |
| 214 | DSNodeHandle &GraphBuilder::getValueDest(Value &V) { |
| 215 | return getLink(getValueNode(V), 0, V.getType()); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 216 | } |
| 217 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 218 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 219 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 220 | /// getLink - This method is used to return the specified link in the |
| 221 | /// specified node if one exists. If a link does not already exist (it's |
| 222 | /// null), then we create a new node, link it, then return it. We must |
| 223 | /// specify the type of the Node field we are accessing so that we know what |
| 224 | /// type should be linked to if we need to create a new node. |
| 225 | /// |
| 226 | DSNodeHandle &GraphBuilder::getLink(const DSNodeHandle &node, |
| 227 | unsigned LinkNo, const Type *FieldTy) { |
| 228 | DSNodeHandle &Node = const_cast<DSNodeHandle&>(node); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 229 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 230 | DSNodeHandle *Link = Node.getLink(LinkNo); |
| 231 | if (Link) return *Link; |
| 232 | |
| 233 | // If the link hasn't been created yet, make and return a new shadow node of |
| 234 | // the appropriate type for FieldTy... |
| 235 | // |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 236 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 237 | // If we are indexing with a typed pointer, then the thing we are pointing |
| 238 | // to is of the pointed type. If we are pointing to it with an integer |
| 239 | // (because of cast to an integer), we represent it with a void type. |
| 240 | // |
| 241 | const Type *ReqTy; |
| 242 | if (const PointerType *Ptr = dyn_cast<PointerType>(FieldTy)) |
| 243 | ReqTy = Ptr->getElementType(); |
| 244 | else |
| 245 | ReqTy = Type::VoidTy; |
| 246 | |
| 247 | DSNode *N = createNode(DSNode::ShadowNode, ReqTy); |
| 248 | Node.setLink(LinkNo, N); |
| 249 | return *Node.getLink(LinkNo); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 250 | } |
| 251 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 252 | |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 253 | //===----------------------------------------------------------------------===// |
| 254 | // Specific instruction type handler implementations... |
| 255 | // |
| 256 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 257 | /// Alloca & Malloc instruction implementation - Simply create a new memory |
| 258 | /// object, pointing the scalar to it. |
| 259 | /// |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 260 | void GraphBuilder::handleAlloc(AllocationInst &AI, DSNode::NodeTy NodeType) { |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 261 | DSNode *New = createNode(NodeType, AI.getAllocatedType()); |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 262 | |
| 263 | // Make the scalar point to the new node... |
| 264 | getValueNode(AI).addEdgeTo(New); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 265 | } |
| 266 | |
| 267 | // PHINode - Make the scalar for the PHI node point to all of the things the |
| 268 | // incoming values point to... which effectively causes them to be merged. |
| 269 | // |
| 270 | void GraphBuilder::visitPHINode(PHINode &PN) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 271 | if (!isPointerType(PN.getType())) return; // Only pointer PHIs |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 272 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 273 | DSNodeHandle &ScalarDest = getValueDest(PN); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 274 | for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 275 | if (!isa<ConstantPointerNull>(PN.getIncomingValue(i))) |
| 276 | ScalarDest.mergeWith(getValueDest(*PN.getIncomingValue(i))); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 277 | } |
| 278 | |
| 279 | void GraphBuilder::visitGetElementPtrInst(GetElementPtrInst &GEP) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 280 | DSNodeHandle Value = getValueDest(*GEP.getOperand(0)); |
| 281 | |
| 282 | unsigned Offset = 0; |
| 283 | const Type *CurTy = GEP.getOperand(0)->getType(); |
| 284 | |
| 285 | for (unsigned i = 1, e = GEP.getNumOperands(); i != e; ++i) |
| 286 | if (GEP.getOperand(i)->getType() == Type::LongTy) { |
Chris Lattner | e03f32b | 2002-10-02 06:24:36 +0000 | [diff] [blame] | 287 | // Get the type indexing into... |
| 288 | const SequentialType *STy = cast<SequentialType>(CurTy); |
| 289 | CurTy = STy->getElementType(); |
| 290 | if (ConstantSInt *CS = dyn_cast<ConstantSInt>(GEP.getOperand(i))) { |
| 291 | if (isa<PointerType>(STy)) |
| 292 | std::cerr << "Pointer indexing not handled yet!\n"; |
| 293 | else |
| 294 | Offset += CS->getValue()*TD.getTypeSize(CurTy); |
| 295 | } else { |
| 296 | // Variable index into a node. We must merge all of the elements of the |
| 297 | // sequential type here. |
| 298 | if (isa<PointerType>(STy)) |
| 299 | std::cerr << "Pointer indexing not handled yet!\n"; |
| 300 | else { |
| 301 | const ArrayType *ATy = cast<ArrayType>(STy); |
| 302 | unsigned ElSize = TD.getTypeSize(CurTy); |
| 303 | DSNode *N = Value.getNode(); |
| 304 | assert(N && "Value must have a node!"); |
| 305 | unsigned RawOffset = Offset+Value.getOffset(); |
| 306 | |
| 307 | // Loop over all of the elements of the array, merging them into the |
| 308 | // zero'th element. |
| 309 | for (unsigned i = 1, e = ATy->getNumElements(); i != e; ++i) |
| 310 | // Merge all of the byte components of this array element |
| 311 | for (unsigned j = 0; j != ElSize; ++j) |
| 312 | N->mergeIndexes(RawOffset+j, RawOffset+i*ElSize+j); |
| 313 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 314 | } |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 315 | } else if (GEP.getOperand(i)->getType() == Type::UByteTy) { |
| 316 | unsigned FieldNo = cast<ConstantUInt>(GEP.getOperand(i))->getValue(); |
| 317 | const StructType *STy = cast<StructType>(CurTy); |
| 318 | Offset += TD.getStructLayout(STy)->MemberOffsets[FieldNo]; |
| 319 | CurTy = STy->getContainedType(FieldNo); |
| 320 | } |
| 321 | |
| 322 | // Add in the offset calculated... |
| 323 | Value.setOffset(Value.getOffset()+Offset); |
| 324 | |
| 325 | // Value is now the pointer we want to GEP to be... |
| 326 | getValueNode(GEP).addEdgeTo(Value); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 327 | } |
| 328 | |
| 329 | void GraphBuilder::visitLoadInst(LoadInst &LI) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 330 | DSNodeHandle &Ptr = getValueDest(*LI.getOperand(0)); |
| 331 | if (isPointerType(LI.getType())) |
| 332 | getValueNode(LI).addEdgeTo(getLink(Ptr, 0, LI.getType())); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 333 | } |
| 334 | |
| 335 | void GraphBuilder::visitStoreInst(StoreInst &SI) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 336 | DSNodeHandle &Dest = getValueDest(*SI.getOperand(1)); |
| 337 | |
| 338 | // Avoid adding edges from null, or processing non-"pointer" stores |
| 339 | if (isPointerType(SI.getOperand(0)->getType()) && |
| 340 | !isa<ConstantPointerNull>(SI.getOperand(0))) { |
| 341 | Dest.addEdgeTo(getValueDest(*SI.getOperand(0))); |
| 342 | } |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 343 | } |
| 344 | |
| 345 | void GraphBuilder::visitReturnInst(ReturnInst &RI) { |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 346 | if (RI.getNumOperands() && isPointerType(RI.getOperand(0)->getType()) && |
| 347 | !isa<ConstantPointerNull>(RI.getOperand(0))) { |
| 348 | DSNodeHandle &Value = getValueDest(*RI.getOperand(0)); |
| 349 | Value.mergeWith(RetNode); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 350 | RetNode = Value; |
| 351 | } |
| 352 | } |
| 353 | |
| 354 | void GraphBuilder::visitCallInst(CallInst &CI) { |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 355 | // Add a new function call entry... |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 356 | FunctionCalls.push_back(vector<DSNodeHandle>()); |
| 357 | vector<DSNodeHandle> &Args = FunctionCalls.back(); |
| 358 | |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 359 | // Set up the return value... |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 360 | if (isPointerType(CI.getType())) |
| 361 | Args.push_back(getLink(getValueNode(CI), 0, CI.getType())); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 362 | else |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 363 | Args.push_back(DSNodeHandle()); |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 364 | |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 365 | unsigned Start = 0; |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 366 | // Special case for a direct call, avoid creating spurious scalar node... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 367 | if (GlobalValue *GV = dyn_cast<GlobalValue>(CI.getOperand(0))) { |
| 368 | Args.push_back(getGlobalNode(*GV)); |
| 369 | Start = 1; |
| 370 | } |
| 371 | |
Chris Lattner | c314ac4 | 2002-07-11 20:32:02 +0000 | [diff] [blame] | 372 | // Pass the arguments in... |
Chris Lattner | 0d9bab8 | 2002-07-18 00:12:30 +0000 | [diff] [blame] | 373 | for (unsigned i = Start, e = CI.getNumOperands(); i != e; ++i) |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 374 | if (isPointerType(CI.getOperand(i)->getType())) |
| 375 | Args.push_back(getLink(getValueNode(*CI.getOperand(i)), 0, |
| 376 | CI.getOperand(i)->getType())); |
Chris Lattner | c68c31b | 2002-07-10 22:38:08 +0000 | [diff] [blame] | 377 | } |
Chris Lattner | 055dc2c | 2002-07-18 15:54:42 +0000 | [diff] [blame] | 378 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 379 | /// Handle casts... |
| 380 | void GraphBuilder::visitCastInst(CastInst &CI) { |
| 381 | if (isPointerType(CI.getType()) && isPointerType(CI.getOperand(0)->getType())) |
| 382 | getValueNode(CI).addEdgeTo(getLink(getValueNode(*CI.getOperand(0)), 0, |
| 383 | CI.getOperand(0)->getType())); |
| 384 | } |
Chris Lattner | 055dc2c | 2002-07-18 15:54:42 +0000 | [diff] [blame] | 385 | |
Chris Lattner | fccd06f | 2002-10-01 22:33:50 +0000 | [diff] [blame] | 386 | |
| 387 | |
| 388 | |
| 389 | //===----------------------------------------------------------------------===// |
| 390 | // LocalDataStructures Implementation |
| 391 | //===----------------------------------------------------------------------===// |
| 392 | |
| 393 | // releaseMemory - If the pass pipeline is done with this pass, we can release |
| 394 | // our memory... here... |
| 395 | // |
| 396 | void LocalDataStructures::releaseMemory() { |
| 397 | for (std::map<const Function*, DSGraph*>::iterator I = DSInfo.begin(), |
| 398 | E = DSInfo.end(); I != E; ++I) |
| 399 | delete I->second; |
| 400 | |
| 401 | // Empty map so next time memory is released, data structures are not |
| 402 | // re-deleted. |
| 403 | DSInfo.clear(); |
| 404 | } |
| 405 | |
| 406 | bool LocalDataStructures::run(Module &M) { |
| 407 | // Calculate all of the graphs... |
| 408 | for (Module::iterator I = M.begin(), E = M.end(); I != E; ++I) |
| 409 | if (!I->isExternal()) |
| 410 | DSInfo.insert(std::make_pair(I, new DSGraph(*I))); |
| 411 | return false; |
Chris Lattner | 055dc2c | 2002-07-18 15:54:42 +0000 | [diff] [blame] | 412 | } |
| 413 | |