Added LLVM copyright header.
[oota-llvm.git] / lib / Analysis / DataStructure / DSCallSiteIterator.h
1 //===- DSCallSiteIterator.h - Iterator for DSGraph call sites ---*- C++ -*-===//
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 //===----------------------------------------------------------------------===//
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
22 struct DSCallSiteIterator {
23   // FCs are the edges out of the current node are the call site targets...
24   const std::vector<DSCallSite> *FCs;
25   unsigned CallSite;
26   unsigned CallSiteEntry;
27
28   DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) {
29     CallSite = 0; CallSiteEntry = 0;
30     advanceToValidCallee();
31   }
32
33   // End iterator ctor...
34   DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
35     CallSite = FCs->size(); CallSiteEntry = 0;
36   }
37
38   static bool isVAHackFn(const Function *F) {
39     return F->getName() == "printf"  || F->getName() == "sscanf" ||
40       F->getName() == "fprintf" || F->getName() == "open" ||
41       F->getName() == "sprintf" || F->getName() == "fputs" ||
42       F->getName() == "fscanf" || F->getName() == "bzero" ||
43       F->getName() == "memset";
44   }
45
46   // isUnresolvableFunction - Return true if this is an unresolvable
47   // external function.  A direct or indirect call to this cannot be resolved.
48   // 
49   static bool isUnresolvableFunc(const Function* callee) {
50     return callee->isExternal() && !isVAHackFn(callee);
51   } 
52
53   void advanceToValidCallee() {
54     while (CallSite < FCs->size()) {
55       if ((*FCs)[CallSite].isDirectCall()) {
56         if (CallSiteEntry == 0 &&        // direct call only has one target...
57             ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc()))
58           return;                       // and not an unresolvable external func
59       } else {
60         DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
61         if (CallSiteEntry || isCompleteNode(CalleeNode)) {
62           const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
63           while (CallSiteEntry < Callees.size()) {
64             if (isa<Function>(Callees[CallSiteEntry]))
65               return;
66             ++CallSiteEntry;
67           }
68         }
69       }
70       CallSiteEntry = 0;
71       ++CallSite;
72     }
73   }
74   
75   // isCompleteNode - Return true if we know all of the targets of this node,
76   // and if the call sites are not external.
77   //
78   static inline bool isCompleteNode(DSNode *N) {
79     if (N->isIncomplete()) return false;
80     const std::vector<GlobalValue*> &Callees = N->getGlobals();
81     for (unsigned i = 0, e = Callees.size(); i != e; ++i)
82       if (isUnresolvableFunc(cast<Function>(Callees[i])))
83         return false;               // Unresolvable external function found...
84     return true;  // otherwise ok
85   }
86
87 public:
88   static DSCallSiteIterator begin_aux(DSGraph &G) {
89     return G.getAuxFunctionCalls();
90   }
91   static DSCallSiteIterator end_aux(DSGraph &G) {
92     return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
93   }
94   static DSCallSiteIterator begin_std(DSGraph &G) {
95     return G.getFunctionCalls();
96   }
97   static DSCallSiteIterator end_std(DSGraph &G) {
98     return DSCallSiteIterator(G.getFunctionCalls(), true);
99   }
100   static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
101   static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
102     return DSCallSiteIterator(CSs, true);
103   }
104   bool operator==(const DSCallSiteIterator &CSI) const {
105     return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
106   }
107   bool operator!=(const DSCallSiteIterator &CSI) const {
108     return !operator==(CSI);
109   }
110
111   unsigned getCallSiteIdx() const { return CallSite; }
112   const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
113
114   Function *operator*() const {
115     if ((*FCs)[CallSite].isDirectCall()) {
116       return (*FCs)[CallSite].getCalleeFunc();
117     } else {
118       DSNode *Node = (*FCs)[CallSite].getCalleeNode();
119       return cast<Function>(Node->getGlobals()[CallSiteEntry]);
120     }
121   }
122
123   DSCallSiteIterator& operator++() {                // Preincrement
124     ++CallSiteEntry;
125     advanceToValidCallee();
126     return *this;
127   }
128   DSCallSiteIterator operator++(int) { // Postincrement
129     DSCallSiteIterator tmp = *this; ++*this; return tmp; 
130   }
131 };
132
133 #endif