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 static bool isVAHackFn(const Function *F) {
32 return F->getName() == "printf" || F->getName() == "sscanf" ||
33 F->getName() == "fprintf" || F->getName() == "open" ||
34 F->getName() == "sprintf" || F->getName() == "fputs" ||
35 F->getName() == "fscanf" || F->getName() == "bzero" ||
36 F->getName() == "memset";
39 // isUnresolvableFunction - Return true if this is an unresolvable
40 // external function. A direct or indirect call to this cannot be resolved.
42 static bool isUnresolvableFunc(const Function* callee) {
43 return callee->isExternal() && !isVAHackFn(callee);
46 void advanceToValidCallee() {
47 while (CallSite < FCs->size()) {
48 if ((*FCs)[CallSite].isDirectCall()) {
49 if (CallSiteEntry == 0 && // direct call only has one target...
50 ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc()))
51 return; // and not an unresolvable external func
53 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
54 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
55 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
56 while (CallSiteEntry < Callees.size()) {
57 if (isa<Function>(Callees[CallSiteEntry]))
68 // isCompleteNode - Return true if we know all of the targets of this node,
69 // and if the call sites are not external.
71 static inline bool isCompleteNode(DSNode *N) {
72 if (N->isIncomplete()) return false;
73 const std::vector<GlobalValue*> &Callees = N->getGlobals();
74 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
75 if (isUnresolvableFunc(cast<Function>(Callees[i])))
76 return false; // Unresolvable external function found...
77 return true; // otherwise ok
81 static DSCallSiteIterator begin_aux(DSGraph &G) {
82 return G.getAuxFunctionCalls();
84 static DSCallSiteIterator end_aux(DSGraph &G) {
85 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
87 static DSCallSiteIterator begin_std(DSGraph &G) {
88 return G.getFunctionCalls();
90 static DSCallSiteIterator end_std(DSGraph &G) {
91 return DSCallSiteIterator(G.getFunctionCalls(), true);
93 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
94 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
95 return DSCallSiteIterator(CSs, true);
97 bool operator==(const DSCallSiteIterator &CSI) const {
98 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
100 bool operator!=(const DSCallSiteIterator &CSI) const {
101 return !operator==(CSI);
104 unsigned getCallSiteIdx() const { return CallSite; }
105 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
107 Function *operator*() const {
108 if ((*FCs)[CallSite].isDirectCall()) {
109 return (*FCs)[CallSite].getCalleeFunc();
111 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
112 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
116 DSCallSiteIterator& operator++() { // Preincrement
118 advanceToValidCallee();
121 DSCallSiteIterator operator++(int) { // Postincrement
122 DSCallSiteIterator tmp = *this; ++*this; return tmp;