1 //===- DSCallSiteIterator.h - Iterator for DSGraph call sites ---*- C++ -*-===//
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.
7 //===----------------------------------------------------------------------===//
9 #ifndef DSCALLSITEITERATOR_H
10 #define DSCALLSITEITERATOR_H
12 #include "llvm/Analysis/DSGraph.h"
13 #include "llvm/Function.h"
15 struct DSCallSiteIterator {
16 // FCs are the edges out of the current node are the call site targets...
17 const std::vector<DSCallSite> *FCs;
19 unsigned CallSiteEntry;
21 DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) {
22 CallSite = 0; CallSiteEntry = 0;
23 advanceToValidCallee();
26 // End iterator ctor...
27 DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
28 CallSite = FCs->size(); CallSiteEntry = 0;
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
39 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
40 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
41 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
42 while (CallSiteEntry < Callees.size()) {
43 if (isa<Function>(Callees[CallSiteEntry]))
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" ||
58 F->getName() == "fscanf";
61 // isCompleteNode - Return true if we know all of the targets of this node,
62 // and if the call sites are not external.
64 static inline bool isCompleteNode(DSNode *N) {
65 if (N->isIncomplete()) return false;
66 const std::vector<GlobalValue*> &Callees = N->getGlobals();
67 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
68 if (Callees[i]->isExternal())
69 if (!isVAHackFn(cast<Function>(Callees[i])))
70 return false; // External function found...
71 return true; // otherwise ok
75 static DSCallSiteIterator begin_aux(DSGraph &G) {
76 return G.getAuxFunctionCalls();
78 static DSCallSiteIterator end_aux(DSGraph &G) {
79 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
81 static DSCallSiteIterator begin_std(DSGraph &G) {
82 return G.getFunctionCalls();
84 static DSCallSiteIterator end_std(DSGraph &G) {
85 return DSCallSiteIterator(G.getFunctionCalls(), true);
87 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
88 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
89 return DSCallSiteIterator(CSs, true);
91 bool operator==(const DSCallSiteIterator &CSI) const {
92 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
94 bool operator!=(const DSCallSiteIterator &CSI) const {
95 return !operator==(CSI);
98 unsigned getCallSiteIdx() const { return CallSite; }
99 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
101 Function *operator*() const {
102 if ((*FCs)[CallSite].isDirectCall()) {
103 return (*FCs)[CallSite].getCalleeFunc();
105 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
106 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
110 DSCallSiteIterator& operator++() { // Preincrement
112 advanceToValidCallee();
115 DSCallSiteIterator operator++(int) { // Postincrement
116 DSCallSiteIterator tmp = *this; ++*this; return tmp;