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 | |
| 31 | void advanceToValidCallee() { |
| 32 | while (CallSite < FCs->size()) { |
| 33 | if ((*FCs)[CallSite].isDirectCall()) { |
| 34 | if (CallSiteEntry == 0 && // direct call only has one target... |
| 35 | (!(*FCs)[CallSite].getCalleeFunc()->isExternal() || |
| 36 | isVAHackFn((*FCs)[CallSite].getCalleeFunc()))) // If not external |
| 37 | return; |
| 38 | } else { |
| 39 | DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode(); |
| 40 | if (CallSiteEntry || isCompleteNode(CalleeNode)) { |
| 41 | const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals(); |
Chris Lattner | b6734b2 | 2003-07-01 16:27:53 +0000 | [diff] [blame^] | 42 | while (CallSiteEntry < Callees.size()) { |
| 43 | if (isa<Function>(Callees[CallSiteEntry])) |
| 44 | return; |
| 45 | ++CallSiteEntry; |
| 46 | } |
Chris Lattner | 2b4c8df | 2003-06-30 05:27:53 +0000 | [diff] [blame] | 47 | } |
| 48 | } |
| 49 | CallSiteEntry = 0; |
| 50 | ++CallSite; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | static bool isVAHackFn(const Function *F) { |
| 55 | return F->getName() == "printf" || F->getName() == "sscanf" || |
| 56 | F->getName() == "fprintf" || F->getName() == "open" || |
| 57 | F->getName() == "sprintf" || F->getName() == "fputs" || |
| 58 | F->getName() == "fscanf"; |
| 59 | } |
| 60 | |
| 61 | // isCompleteNode - Return true if we know all of the targets of this node, |
| 62 | // and if the call sites are not external. |
| 63 | // |
| 64 | static inline bool isCompleteNode(DSNode *N) { |
| 65 | if (N->isIncomplete()) return false; |
| 66 | const std::vector<GlobalValue*> &Callees = N->getGlobals(); |
| 67 | for (unsigned i = 0, e = Callees.size(); i != e; ++i) |
| 68 | if (Callees[i]->isExternal()) |
| 69 | if (!isVAHackFn(cast<Function>(Callees[i]))) |
| 70 | return false; // External function found... |
| 71 | return true; // otherwise ok |
| 72 | } |
| 73 | |
| 74 | public: |
| 75 | static DSCallSiteIterator begin_aux(DSGraph &G) { |
| 76 | return G.getAuxFunctionCalls(); |
| 77 | } |
| 78 | static DSCallSiteIterator end_aux(DSGraph &G) { |
| 79 | return DSCallSiteIterator(G.getAuxFunctionCalls(), true); |
| 80 | } |
| 81 | static DSCallSiteIterator begin_std(DSGraph &G) { |
| 82 | return G.getFunctionCalls(); |
| 83 | } |
| 84 | static DSCallSiteIterator end_std(DSGraph &G) { |
| 85 | return DSCallSiteIterator(G.getFunctionCalls(), true); |
| 86 | } |
| 87 | static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; } |
| 88 | static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) { |
| 89 | return DSCallSiteIterator(CSs, true); |
| 90 | } |
| 91 | bool operator==(const DSCallSiteIterator &CSI) const { |
| 92 | return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry; |
| 93 | } |
| 94 | bool operator!=(const DSCallSiteIterator &CSI) const { |
| 95 | return !operator==(CSI); |
| 96 | } |
| 97 | |
| 98 | unsigned getCallSiteIdx() const { return CallSite; } |
| 99 | const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; } |
| 100 | |
| 101 | Function *operator*() const { |
| 102 | if ((*FCs)[CallSite].isDirectCall()) { |
| 103 | return (*FCs)[CallSite].getCalleeFunc(); |
| 104 | } else { |
| 105 | DSNode *Node = (*FCs)[CallSite].getCalleeNode(); |
| 106 | return cast<Function>(Node->getGlobals()[CallSiteEntry]); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | DSCallSiteIterator& operator++() { // Preincrement |
| 111 | ++CallSiteEntry; |
| 112 | advanceToValidCallee(); |
| 113 | return *this; |
| 114 | } |
| 115 | DSCallSiteIterator operator++(int) { // Postincrement |
| 116 | DSCallSiteIterator tmp = *this; ++*this; return tmp; |
| 117 | } |
| 118 | }; |
| 119 | |
| 120 | #endif |