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/DSGraph.h"
20 #include "llvm/Function.h"
22 struct DSCallSiteIterator {
23 // FCs are the edges out of the current node are the call site targets...
24 const std::vector<DSCallSite> *FCs;
26 unsigned CallSiteEntry;
28 DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) {
29 CallSite = 0; CallSiteEntry = 0;
30 advanceToValidCallee();
33 // End iterator ctor...
34 DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
35 CallSite = FCs->size(); CallSiteEntry = 0;
38 static bool isVAHackFn(const Function *F) {
39 return F->getName() == "printf" || F->getName() == "sscanf" ||
40 F->getName() == "fprintf" || F->getName() == "open" ||
41 F->getName() == "sprintf" || F->getName() == "fputs" ||
42 F->getName() == "fscanf" || F->getName() == "bzero" ||
43 F->getName() == "memset";
46 // isUnresolvableFunction - Return true if this is an unresolvable
47 // external function. A direct or indirect call to this cannot be resolved.
49 static bool isUnresolvableFunc(const Function* callee) {
50 return callee->isExternal() && !isVAHackFn(callee);
53 void advanceToValidCallee() {
54 while (CallSite < FCs->size()) {
55 if ((*FCs)[CallSite].isDirectCall()) {
56 if (CallSiteEntry == 0 && // direct call only has one target...
57 ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc()))
58 return; // and not an unresolvable external func
60 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
61 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
62 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
63 while (CallSiteEntry < Callees.size()) {
64 if (isa<Function>(Callees[CallSiteEntry]))
75 // isCompleteNode - Return true if we know all of the targets of this node,
76 // and if the call sites are not external.
78 static inline bool isCompleteNode(DSNode *N) {
79 if (N->isIncomplete()) return false;
80 const std::vector<GlobalValue*> &Callees = N->getGlobals();
81 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
82 if (isUnresolvableFunc(cast<Function>(Callees[i])))
83 return false; // Unresolvable external function found...
84 return true; // otherwise ok
88 static DSCallSiteIterator begin_aux(DSGraph &G) {
89 return G.getAuxFunctionCalls();
91 static DSCallSiteIterator end_aux(DSGraph &G) {
92 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
94 static DSCallSiteIterator begin_std(DSGraph &G) {
95 return G.getFunctionCalls();
97 static DSCallSiteIterator end_std(DSGraph &G) {
98 return DSCallSiteIterator(G.getFunctionCalls(), true);
100 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
101 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
102 return DSCallSiteIterator(CSs, true);
104 bool operator==(const DSCallSiteIterator &CSI) const {
105 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
107 bool operator!=(const DSCallSiteIterator &CSI) const {
108 return !operator==(CSI);
111 unsigned getCallSiteIdx() const { return CallSite; }
112 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
114 Function *operator*() const {
115 if ((*FCs)[CallSite].isDirectCall()) {
116 return (*FCs)[CallSite].getCalleeFunc();
118 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
119 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
123 DSCallSiteIterator& operator++() { // Preincrement
125 advanceToValidCallee();
128 DSCallSiteIterator operator++(int) { // Postincrement
129 DSCallSiteIterator tmp = *this; ++*this; return tmp;