Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 1 | //===- DSCallSiteIterator.h - Iterator for DSGraph call sites ---*- C++ -*-===// |
| 2 | // |
| 3 | // This file implements an iterator for complete call sites in DSGraphs. This |
| 4 | // code can either iterator over the normal call list or the aux calls list, and |
| 5 | // is used by the TD and BU passes. |
| 6 | // |
| 7 | //===----------------------------------------------------------------------===// |
| 8 | |
| 9 | #ifndef DSCALLSITEITERATOR_H |
| 10 | #define DSCALLSITEITERATOR_H |
| 11 | |
| 12 | #include "llvm/Analysis/DSGraph.h" |
| 13 | #include "llvm/Function.h" |
| 14 | |
| 15 | struct DSCallSiteIterator { |
| 16 | // FCs are the edges out of the current node are the call site targets... |
| 17 | const std::vector<DSCallSite> *FCs; |
| 18 | unsigned CallSite; |
| 19 | unsigned CallSiteEntry; |
| 20 | |
| 21 | DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) { |
| 22 | CallSite = 0; CallSiteEntry = 0; |
| 23 | advanceToValidCallee(); |
| 24 | } |
| 25 | |
| 26 | // End iterator ctor... |
| 27 | DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) { |
| 28 | CallSite = FCs->size(); CallSiteEntry = 0; |
| 29 | } |
| 30 | |
Vikram S. Adve | 2e1de5e | 2003-07-16 21:25:17 +0000 | [diff] [blame^] | 31 | static bool isVAHackFn(const Function *F) { |
| 32 | return F->getName() == "printf" || F->getName() == "sscanf" || |
| 33 | F->getName() == "fprintf" || F->getName() == "open" || |
| 34 | F->getName() == "sprintf" || F->getName() == "fputs" || |
| 35 | F->getName() == "fscanf" || F->getName() == "bzero" || |
| 36 | F->getName() == "memset"; |
| 37 | } |
| 38 | |
| 39 | // isUnresolvableFunction - Return true if this is an unresolvable |
| 40 | // external function. A direct or indirect call to this cannot be resolved. |
| 41 | // |
| 42 | static bool isUnresolvableFunc(const Function* callee) { |
| 43 | return callee->isExternal() && !isVAHackFn(callee); |
| 44 | } |
| 45 | |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 46 | void advanceToValidCallee() { |
| 47 | while (CallSite < FCs->size()) { |
| 48 | if ((*FCs)[CallSite].isDirectCall()) { |
| 49 | if (CallSiteEntry == 0 && // direct call only has one target... |
Vikram S. Adve | 2e1de5e | 2003-07-16 21:25:17 +0000 | [diff] [blame^] | 50 | ! DSCallSite::isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc())) |
| 51 | return; // and not an unresolvable external func |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 52 | } else { |
| 53 | DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode(); |
| 54 | if (CallSiteEntry || isCompleteNode(CalleeNode)) { |
| 55 | const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals(); |
Chris Lattner | b6734b2 | 2003-07-01 16:27:53 +0000 | [diff] [blame] | 56 | while (CallSiteEntry < Callees.size()) { |
| 57 | if (isa<Function>(Callees[CallSiteEntry])) |
| 58 | return; |
| 59 | ++CallSiteEntry; |
| 60 | } |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 61 | } |
| 62 | } |
| 63 | CallSiteEntry = 0; |
| 64 | ++CallSite; |
| 65 | } |
| 66 | } |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 67 | |
| 68 | // isCompleteNode - Return true if we know all of the targets of this node, |
| 69 | // and if the call sites are not external. |
| 70 | // |
| 71 | static inline bool isCompleteNode(DSNode *N) { |
| 72 | if (N->isIncomplete()) return false; |
| 73 | const std::vector<GlobalValue*> &Callees = N->getGlobals(); |
| 74 | for (unsigned i = 0, e = Callees.size(); i != e; ++i) |
Vikram S. Adve | 2e1de5e | 2003-07-16 21:25:17 +0000 | [diff] [blame^] | 75 | if (isUnresolvableFunc(cast<Function>(Callees[i]))) |
| 76 | return false; // Unresolvable external function found... |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 77 | return true; // otherwise ok |
| 78 | } |
| 79 | |
| 80 | public: |
| 81 | static DSCallSiteIterator begin_aux(DSGraph &G) { |
| 82 | return G.getAuxFunctionCalls(); |
| 83 | } |
| 84 | static DSCallSiteIterator end_aux(DSGraph &G) { |
| 85 | return DSCallSiteIterator(G.getAuxFunctionCalls(), true); |
| 86 | } |
| 87 | static DSCallSiteIterator begin_std(DSGraph &G) { |
| 88 | return G.getFunctionCalls(); |
| 89 | } |
| 90 | static DSCallSiteIterator end_std(DSGraph &G) { |
| 91 | return DSCallSiteIterator(G.getFunctionCalls(), true); |
| 92 | } |
| 93 | static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; } |
| 94 | static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) { |
| 95 | return DSCallSiteIterator(CSs, true); |
| 96 | } |
| 97 | bool operator==(const DSCallSiteIterator &CSI) const { |
| 98 | return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry; |
| 99 | } |
| 100 | bool operator!=(const DSCallSiteIterator &CSI) const { |
| 101 | return !operator==(CSI); |
| 102 | } |
| 103 | |
| 104 | unsigned getCallSiteIdx() const { return CallSite; } |
| 105 | const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; } |
| 106 | |
| 107 | Function *operator*() const { |
| 108 | if ((*FCs)[CallSite].isDirectCall()) { |
| 109 | return (*FCs)[CallSite].getCalleeFunc(); |
| 110 | } else { |
| 111 | DSNode *Node = (*FCs)[CallSite].getCalleeNode(); |
| 112 | return cast<Function>(Node->getGlobals()[CallSiteEntry]); |
| 113 | } |
| 114 | } |
| 115 | |
| 116 | DSCallSiteIterator& operator++() { // Preincrement |
| 117 | ++CallSiteEntry; |
| 118 | advanceToValidCallee(); |
| 119 | return *this; |
| 120 | } |
| 121 | DSCallSiteIterator operator++(int) { // Postincrement |
| 122 | DSCallSiteIterator tmp = *this; ++*this; return tmp; |
| 123 | } |
| 124 | }; |
| 125 | |
| 126 | #endif |