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