blob: 62a1515f92183947c666a795382a0cdea417917c [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();
Chris Lattnerb6734b22003-07-01 16:27:53 +000042 while (CallSiteEntry < Callees.size()) {
43 if (isa<Function>(Callees[CallSiteEntry]))
44 return;
45 ++CallSiteEntry;
46 }
Chris Lattner2b4c8df2003-06-30 05:27:53 +000047 }
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" ||
Chris Lattnerf07804b2003-07-02 04:38:00 +000058 F->getName() == "fscanf" || F->getName() == "bzero" ||
59 F->getName() == "memset";
Chris Lattner2b4c8df2003-06-30 05:27:53 +000060 }
61
62 // isCompleteNode - Return true if we know all of the targets of this node,
63 // and if the call sites are not external.
64 //
65 static inline bool isCompleteNode(DSNode *N) {
66 if (N->isIncomplete()) return false;
67 const std::vector<GlobalValue*> &Callees = N->getGlobals();
68 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
69 if (Callees[i]->isExternal())
70 if (!isVAHackFn(cast<Function>(Callees[i])))
71 return false; // External function found...
72 return true; // otherwise ok
73 }
74
75public:
76 static DSCallSiteIterator begin_aux(DSGraph &G) {
77 return G.getAuxFunctionCalls();
78 }
79 static DSCallSiteIterator end_aux(DSGraph &G) {
80 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
81 }
82 static DSCallSiteIterator begin_std(DSGraph &G) {
83 return G.getFunctionCalls();
84 }
85 static DSCallSiteIterator end_std(DSGraph &G) {
86 return DSCallSiteIterator(G.getFunctionCalls(), true);
87 }
88 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
89 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
90 return DSCallSiteIterator(CSs, true);
91 }
92 bool operator==(const DSCallSiteIterator &CSI) const {
93 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
94 }
95 bool operator!=(const DSCallSiteIterator &CSI) const {
96 return !operator==(CSI);
97 }
98
99 unsigned getCallSiteIdx() const { return CallSite; }
100 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
101
102 Function *operator*() const {
103 if ((*FCs)[CallSite].isDirectCall()) {
104 return (*FCs)[CallSite].getCalleeFunc();
105 } else {
106 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
107 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
108 }
109 }
110
111 DSCallSiteIterator& operator++() { // Preincrement
112 ++CallSiteEntry;
113 advanceToValidCallee();
114 return *this;
115 }
116 DSCallSiteIterator operator++(int) { // Postincrement
117 DSCallSiteIterator tmp = *this; ++*this; return tmp;
118 }
119};
120
121#endif