blob: 324111cffad3067dbf70047664ceaa7a9f0db696 [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
22struct DSCallSiteIterator {
23 // FCs are the edges out of the current node are the call site targets...
24 const std::vector<DSCallSite> *FCs;
25 unsigned CallSite;
26 unsigned CallSiteEntry;
27
28 DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) {
29 CallSite = 0; CallSiteEntry = 0;
30 advanceToValidCallee();
31 }
32
33 // End iterator ctor...
34 DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) {
35 CallSite = FCs->size(); CallSiteEntry = 0;
36 }
37
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000038 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" ||
Chris Lattnerfb8c6102003-11-08 21:55:50 +000042 F->getName() == "fscanf";
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000043 }
44
45 // isUnresolvableFunction - Return true if this is an unresolvable
46 // external function. A direct or indirect call to this cannot be resolved.
47 //
48 static bool isUnresolvableFunc(const Function* callee) {
49 return callee->isExternal() && !isVAHackFn(callee);
50 }
51
Chris Lattner2b4c8df2003-06-30 05:27:53 +000052 void advanceToValidCallee() {
53 while (CallSite < FCs->size()) {
54 if ((*FCs)[CallSite].isDirectCall()) {
55 if (CallSiteEntry == 0 && // direct call only has one target...
Brian Gaeke1d2ba442003-07-17 19:07:46 +000056 ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc()))
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000057 return; // and not an unresolvable external func
Chris Lattner2b4c8df2003-06-30 05:27:53 +000058 } else {
59 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
60 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
61 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
Chris Lattnerb6734b22003-07-01 16:27:53 +000062 while (CallSiteEntry < Callees.size()) {
63 if (isa<Function>(Callees[CallSiteEntry]))
64 return;
65 ++CallSiteEntry;
66 }
Chris Lattner2b4c8df2003-06-30 05:27:53 +000067 }
68 }
69 CallSiteEntry = 0;
70 ++CallSite;
71 }
72 }
Chris Lattner2b4c8df2003-06-30 05:27:53 +000073
74 // isCompleteNode - Return true if we know all of the targets of this node,
75 // and if the call sites are not external.
76 //
77 static inline bool isCompleteNode(DSNode *N) {
78 if (N->isIncomplete()) return false;
79 const std::vector<GlobalValue*> &Callees = N->getGlobals();
80 for (unsigned i = 0, e = Callees.size(); i != e; ++i)
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000081 if (isUnresolvableFunc(cast<Function>(Callees[i])))
82 return false; // Unresolvable external function found...
Chris Lattner2b4c8df2003-06-30 05:27:53 +000083 return true; // otherwise ok
84 }
85
86public:
87 static DSCallSiteIterator begin_aux(DSGraph &G) {
88 return G.getAuxFunctionCalls();
89 }
90 static DSCallSiteIterator end_aux(DSGraph &G) {
91 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
92 }
93 static DSCallSiteIterator begin_std(DSGraph &G) {
94 return G.getFunctionCalls();
95 }
96 static DSCallSiteIterator end_std(DSGraph &G) {
97 return DSCallSiteIterator(G.getFunctionCalls(), true);
98 }
99 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
100 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
101 return DSCallSiteIterator(CSs, true);
102 }
103 bool operator==(const DSCallSiteIterator &CSI) const {
104 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
105 }
106 bool operator!=(const DSCallSiteIterator &CSI) const {
107 return !operator==(CSI);
108 }
109
110 unsigned getCallSiteIdx() const { return CallSite; }
111 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
112
113 Function *operator*() const {
114 if ((*FCs)[CallSite].isDirectCall()) {
115 return (*FCs)[CallSite].getCalleeFunc();
116 } else {
117 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
118 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
119 }
120 }
121
122 DSCallSiteIterator& operator++() { // Preincrement
123 ++CallSiteEntry;
124 advanceToValidCallee();
125 return *this;
126 }
127 DSCallSiteIterator operator++(int) { // Postincrement
128 DSCallSiteIterator tmp = *this; ++*this; return tmp;
129 }
130};
131
132#endif