Silence a spurious warning
[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 namespace llvm {
23
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
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" ||
44       F->getName() == "fscanf";
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
54   void advanceToValidCallee() {
55     while (CallSite < FCs->size()) {
56       if ((*FCs)[CallSite].isDirectCall()) {
57         if (CallSiteEntry == 0 &&        // direct call only has one target...
58             ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc()))
59           return;                       // and not an unresolvable external func
60       } else {
61         DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
62         if (CallSiteEntry || isCompleteNode(CalleeNode)) {
63           const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
64           while (CallSiteEntry < Callees.size()) {
65             if (isa<Function>(Callees[CallSiteEntry]))
66               return;
67             ++CallSiteEntry;
68           }
69         }
70       }
71       CallSiteEntry = 0;
72       ++CallSite;
73     }
74   }
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)
83       if (isUnresolvableFunc(cast<Function>(Callees[i])))
84         return false;               // Unresolvable external function found...
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
134 } // End llvm namespace
135
136 #endif