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