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