blob: df9f36908aeb8163b79742e6643751866317a496 [file] [log] [blame]
Chris Lattner2b4c8df2003-06-30 05:27:53 +00001//===- DSCallSiteIterator.h - Iterator for DSGraph call sites ---*- C++ -*-===//
John Criswell856ba762003-10-21 15:17:13 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattner2b4c8df2003-06-30 05:27:53 +00009//
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.
13//
14//===----------------------------------------------------------------------===//
15
16#ifndef DSCALLSITEITERATOR_H
17#define DSCALLSITEITERATOR_H
18
19#include "llvm/Analysis/DSGraph.h"
20#include "llvm/Function.h"
21
Brian Gaeked0fde302003-11-11 22:41:34 +000022namespace llvm {
23
Chris Lattner2b4c8df2003-06-30 05:27:53 +000024struct DSCallSiteIterator {
25 // FCs are the edges out of the current node are the call site targets...
26 const std::vector<DSCallSite> *FCs;
27 unsigned CallSite;
28 unsigned CallSiteEntry;
29
30 DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) {
31 CallSite = 0; CallSiteEntry = 0;
32 advanceToValidCallee();
33 }
34
35 // End iterator ctor...
36 DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
37 CallSite = FCs->size(); CallSiteEntry = 0;
38 }
39
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000040 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" ||
Chris Lattnerfb8c6102003-11-08 21:55:50 +000044 F->getName() == "fscanf";
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000045 }
46
47 // isUnresolvableFunction - Return true if this is an unresolvable
48 // external function. A direct or indirect call to this cannot be resolved.
49 //
50 static bool isUnresolvableFunc(const Function* callee) {
51 return callee->isExternal() && !isVAHackFn(callee);
52 }
53
Chris Lattner2b4c8df2003-06-30 05:27:53 +000054 void advanceToValidCallee() {
55 while (CallSite < FCs->size()) {
56 if ((*FCs)[CallSite].isDirectCall()) {
57 if (CallSiteEntry == 0 && // direct call only has one target...
Brian Gaeke1d2ba442003-07-17 19:07:46 +000058 ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc()))
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000059 return; // and not an unresolvable external func
Chris Lattner2b4c8df2003-06-30 05:27:53 +000060 } else {
61 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
62 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
63 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
Chris Lattnerb6734b22003-07-01 16:27:53 +000064 while (CallSiteEntry < Callees.size()) {
65 if (isa<Function>(Callees[CallSiteEntry]))
66 return;
67 ++CallSiteEntry;
68 }
Chris Lattner2b4c8df2003-06-30 05:27:53 +000069 }
70 }
71 CallSiteEntry = 0;
72 ++CallSite;
73 }
74 }
Chris Lattner2b4c8df2003-06-30 05:27:53 +000075
76 // isCompleteNode - Return true if we know all of the targets of this node,
77 // and if the call sites are not external.
78 //
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)
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000083 if (isUnresolvableFunc(cast<Function>(Callees[i])))
84 return false; // Unresolvable external function found...
Chris Lattner2b4c8df2003-06-30 05:27:53 +000085 return true; // otherwise ok
86 }
87
88public:
89 static DSCallSiteIterator begin_aux(DSGraph &G) {
90 return G.getAuxFunctionCalls();
91 }
92 static DSCallSiteIterator end_aux(DSGraph &G) {
93 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
94 }
95 static DSCallSiteIterator begin_std(DSGraph &G) {
96 return G.getFunctionCalls();
97 }
98 static DSCallSiteIterator end_std(DSGraph &G) {
99 return DSCallSiteIterator(G.getFunctionCalls(), true);
100 }
101 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
102 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
103 return DSCallSiteIterator(CSs, true);
104 }
105 bool operator==(const DSCallSiteIterator &CSI) const {
106 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
107 }
108 bool operator!=(const DSCallSiteIterator &CSI) const {
109 return !operator==(CSI);
110 }
111
112 unsigned getCallSiteIdx() const { return CallSite; }
113 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
114
115 Function *operator*() const {
116 if ((*FCs)[CallSite].isDirectCall()) {
117 return (*FCs)[CallSite].getCalleeFunc();
118 } else {
119 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
120 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
121 }
122 }
123
124 DSCallSiteIterator& operator++() { // Preincrement
125 ++CallSiteEntry;
126 advanceToValidCallee();
127 return *this;
128 }
129 DSCallSiteIterator operator++(int) { // Postincrement
130 DSCallSiteIterator tmp = *this; ++*this; return tmp;
131 }
132};
133
Brian Gaeked0fde302003-11-11 22:41:34 +0000134} // End llvm namespace
135
Chris Lattner2b4c8df2003-06-30 05:27:53 +0000136#endif