Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 1 | //===- FunctionRepBuilder.h - Structures for graph building ------*- C++ -*--=// |
| 2 | // |
| 3 | // This file defines the FunctionRepBuilder and InitVisitor classes that are |
| 4 | // used to build the local data structure graph for a method. |
| 5 | // |
| 6 | //===----------------------------------------------------------------------===// |
| 7 | |
| 8 | #ifndef DATA_STRUCTURE_METHOD_REP_BUILDER_H |
| 9 | #define DATA_STRUCTURE_METHOD_REP_BUILDER_H |
| 10 | |
| 11 | #include "llvm/Analysis/DataStructure.h" |
| 12 | #include "llvm/Support/InstVisitor.h" |
Anand Shukla | a928403 | 2002-06-25 20:35:19 +0000 | [diff] [blame^] | 13 | #include <iostream> |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 14 | |
| 15 | // DEBUG_DATA_STRUCTURE_CONSTRUCTION - Define this to 1 if you want debug output |
Chris Lattner | fe14568 | 2002-04-17 03:24:59 +0000 | [diff] [blame] | 16 | //#define DEBUG_DATA_STRUCTURE_CONSTRUCTION 1 |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 17 | |
| 18 | class FunctionRepBuilder; |
| 19 | |
| 20 | // InitVisitor - Used to initialize the worklists for data structure analysis. |
| 21 | // Iterate over the instructions in the method, creating nodes for malloc and |
| 22 | // call instructions. Add all uses of these to the worklist of instructions |
| 23 | // to process. |
| 24 | // |
| 25 | class InitVisitor : public InstVisitor<InitVisitor> { |
| 26 | FunctionRepBuilder *Rep; |
| 27 | Function *Func; |
| 28 | public: |
| 29 | InitVisitor(FunctionRepBuilder *R, Function *F) : Rep(R), Func(F) {} |
| 30 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 31 | void visitCallInst(CallInst &CI); |
| 32 | void visitAllocationInst(AllocationInst &AI); |
| 33 | void visitInstruction(Instruction &I); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 34 | |
| 35 | // visitOperand - If the specified instruction operand is a global value, add |
| 36 | // a node for it... |
| 37 | // |
| 38 | void visitOperand(Value *V); |
| 39 | }; |
| 40 | |
| 41 | |
| 42 | // FunctionRepBuilder - This builder object creates the datastructure graph for |
| 43 | // a method. |
| 44 | // |
| 45 | class FunctionRepBuilder : InstVisitor<FunctionRepBuilder> { |
| 46 | friend class InitVisitor; |
| 47 | FunctionDSGraph *F; |
| 48 | PointerValSet RetNode; |
| 49 | |
| 50 | // ValueMap - Mapping between values we are processing and the possible |
| 51 | // datastructures that they may point to... |
Anand Shukla | a928403 | 2002-06-25 20:35:19 +0000 | [diff] [blame^] | 52 | std::map<Value*, PointerValSet> ValueMap; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 53 | |
| 54 | // CallMap - Keep track of which call nodes correspond to which call insns. |
| 55 | // The reverse mapping is stored in the CallDSNodes themselves. |
| 56 | // |
Anand Shukla | a928403 | 2002-06-25 20:35:19 +0000 | [diff] [blame^] | 57 | std::map<CallInst*, CallDSNode*> CallMap; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 58 | |
| 59 | // Worklist - Vector of (pointer typed) instructions to process still... |
| 60 | std::vector<Instruction *> WorkList; |
| 61 | |
| 62 | // Nodes - Keep track of all of the resultant nodes, because there may not |
| 63 | // be edges connecting these to anything. |
| 64 | // |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 65 | std::vector<AllocDSNode*> AllocNodes; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 66 | std::vector<ShadowDSNode*> ShadowNodes; |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 67 | std::vector<GlobalDSNode*> GlobalNodes; |
| 68 | std::vector<CallDSNode*> CallNodes; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 69 | |
| 70 | // addAllUsesToWorkList - Add all of the instructions users of the specified |
| 71 | // value to the work list for further processing... |
| 72 | // |
| 73 | void addAllUsesToWorkList(Value *V); |
| 74 | |
| 75 | public: |
| 76 | FunctionRepBuilder(FunctionDSGraph *f) : F(f) { |
| 77 | initializeWorkList(F->getFunction()); |
| 78 | processWorkList(); |
| 79 | } |
| 80 | |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 81 | const std::vector<AllocDSNode*> &getAllocNodes() const { return AllocNodes; } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 82 | const std::vector<ShadowDSNode*> &getShadowNodes() const {return ShadowNodes;} |
Chris Lattner | 1120c8b | 2002-03-28 17:56:03 +0000 | [diff] [blame] | 83 | const std::vector<GlobalDSNode*> &getGlobalNodes() const {return GlobalNodes;} |
| 84 | const std::vector<CallDSNode*> &getCallNodes() const { return CallNodes; } |
| 85 | |
Chris Lattner | fe14568 | 2002-04-17 03:24:59 +0000 | [diff] [blame] | 86 | |
| 87 | ShadowDSNode *makeSynthesizedShadow(const Type *Ty, DSNode *Parent); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 88 | |
| 89 | const PointerValSet &getRetNode() const { return RetNode; } |
| 90 | |
Anand Shukla | a928403 | 2002-06-25 20:35:19 +0000 | [diff] [blame^] | 91 | const std::map<Value*, PointerValSet> &getValueMap() const { return ValueMap; } |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 92 | private: |
| 93 | static PointerVal getIndexedPointerDest(const PointerVal &InP, |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 94 | const MemAccessInst &MAI); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 95 | |
| 96 | void initializeWorkList(Function *Func); |
| 97 | void processWorkList() { |
| 98 | // While the worklist still has instructions to process, process them! |
| 99 | while (!WorkList.empty()) { |
| 100 | Instruction *I = WorkList.back(); WorkList.pop_back(); |
Anand Shukla | a928403 | 2002-06-25 20:35:19 +0000 | [diff] [blame^] | 101 | |
Chris Lattner | fe14568 | 2002-04-17 03:24:59 +0000 | [diff] [blame] | 102 | #ifdef DEBUG_DATA_STRUCTURE_CONSTRUCTION |
Anand Shukla | a928403 | 2002-06-25 20:35:19 +0000 | [diff] [blame^] | 103 | std::cerr << "Processing worklist inst: " << I; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 104 | #endif |
| 105 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 106 | visit(*I); // Dispatch to a visitXXX function based on instruction type... |
Chris Lattner | fe14568 | 2002-04-17 03:24:59 +0000 | [diff] [blame] | 107 | #ifdef DEBUG_DATA_STRUCTURE_CONSTRUCTION |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 108 | if (I->hasName() && ValueMap.count(I)) { |
Anand Shukla | a928403 | 2002-06-25 20:35:19 +0000 | [diff] [blame^] | 109 | std::cerr << "Inst %" << I->getName() << " value is:\n"; |
| 110 | ValueMap[I].print(std::cerr); |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 111 | } |
| 112 | #endif |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | //===--------------------------------------------------------------------===// |
| 117 | // Functions used to process the worklist of instructions... |
| 118 | // |
| 119 | // Allow the visitor base class to invoke these methods... |
| 120 | friend class InstVisitor<FunctionRepBuilder>; |
| 121 | |
Chris Lattner | 1896150 | 2002-06-25 16:12:52 +0000 | [diff] [blame] | 122 | void visitGetElementPtrInst(GetElementPtrInst &GEP); |
| 123 | void visitReturnInst(ReturnInst &RI); |
| 124 | void visitLoadInst(LoadInst &LI); |
| 125 | void visitStoreInst(StoreInst &SI); |
| 126 | void visitCallInst(CallInst &CI); |
| 127 | void visitPHINode(PHINode &PN); |
| 128 | void visitSetCondInst(SetCondInst &SCI) {} // SetEQ & friends are ignored |
| 129 | void visitFreeInst(FreeInst &FI) {} // Ignore free instructions |
| 130 | void visitInstruction(Instruction &I) { |
| 131 | std::cerr << "\n\n\nUNKNOWN INSTRUCTION type: " << I << "\n\n\n"; |
Chris Lattner | bb2a28f | 2002-03-26 22:39:06 +0000 | [diff] [blame] | 132 | assert(0 && "Cannot proceed"); |
| 133 | } |
| 134 | }; |
| 135 | |
| 136 | #endif |