1 //===- DSCallSiteIterator.h - Iterator for DSGraph call sites ---*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef DSCALLSITEITERATOR_H
17 #define DSCALLSITEITERATOR_H
19 #include "llvm/Analysis/DataStructure/DSGraph.h"
20 #include "llvm/Function.h"
24 struct DSCallSiteIterator {
25 // FCs are the edges out of the current node are the call site targets...
26 const std::vector<DSCallSite> *FCs;
28 unsigned CallSiteEntry;
30 DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) {
31 CallSite = 0; CallSiteEntry = 0;
32 advanceToValidCallee();
35 // End iterator ctor...
36 DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
37 CallSite = FCs->size(); CallSiteEntry = 0;
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";
47 // isUnresolvableFunction - Return true if this is an unresolvable
48 // external function. A direct or indirect call to this cannot be resolved.
50 static bool isUnresolvableFunc(const Function* callee) {
51 return callee->isExternal() && !isVAHackFn(callee);
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
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]))
76 // isCompleteNode - Return true if we know all of the targets of this node,
77 // and if the call sites are not external.
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
89 static DSCallSiteIterator begin_aux(DSGraph &G) {
90 return G.getAuxFunctionCalls();
92 static DSCallSiteIterator end_aux(DSGraph &G) {
93 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
95 static DSCallSiteIterator begin_std(DSGraph &G) {
96 return G.getFunctionCalls();
98 static DSCallSiteIterator end_std(DSGraph &G) {
99 return DSCallSiteIterator(G.getFunctionCalls(), true);
101 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
102 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
103 return DSCallSiteIterator(CSs, true);
105 bool operator==(const DSCallSiteIterator &CSI) const {
106 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
108 bool operator!=(const DSCallSiteIterator &CSI) const {
109 return !operator==(CSI);
112 unsigned getCallSiteIdx() const { return CallSite; }
113 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
115 Function *operator*() const {
116 if ((*FCs)[CallSite].isDirectCall()) {
117 return (*FCs)[CallSite].getCalleeFunc();
119 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
120 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
124 DSCallSiteIterator& operator++() { // Preincrement
126 advanceToValidCallee();
129 DSCallSiteIterator operator++(int) { // Postincrement
130 DSCallSiteIterator tmp = *this; ++*this; return tmp;
134 } // End llvm namespace