blob: 4815c6e8a0e019e67de8f06a58681f8fd2307240 [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" ||
42 F->getName() == "fscanf" || F->getName() == "bzero" ||
43 F->getName() == "memset";
44 }
45
46 // isUnresolvableFunction - Return true if this is an unresolvable
47 // external function. A direct or indirect call to this cannot be resolved.
48 //
49 static bool isUnresolvableFunc(const Function* callee) {
50 return callee->isExternal() && !isVAHackFn(callee);
51 }
52
Chris Lattner2b4c8df2003-06-30 05:27:53 +000053 void advanceToValidCallee() {
54 while (CallSite < FCs->size()) {
55 if ((*FCs)[CallSite].isDirectCall()) {
56 if (CallSiteEntry == 0 && // direct call only has one target...
Brian Gaeke1d2ba442003-07-17 19:07:46 +000057 ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc()))
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000058 return; // and not an unresolvable external func
Chris Lattner2b4c8df2003-06-30 05:27:53 +000059 } else {
60 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode();
61 if (CallSiteEntry || isCompleteNode(CalleeNode)) {
62 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals();
Chris Lattnerb6734b22003-07-01 16:27:53 +000063 while (CallSiteEntry < Callees.size()) {
64 if (isa<Function>(Callees[CallSiteEntry]))
65 return;
66 ++CallSiteEntry;
67 }
Chris Lattner2b4c8df2003-06-30 05:27:53 +000068 }
69 }
70 CallSiteEntry = 0;
71 ++CallSite;
72 }
73 }
Chris Lattner2b4c8df2003-06-30 05:27:53 +000074
75 // isCompleteNode - Return true if we know all of the targets of this node,
76 // and if the call sites are not external.
77 //
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)
Vikram S. Adve2e1de5e2003-07-16 21:25:17 +000082 if (isUnresolvableFunc(cast<Function>(Callees[i])))
83 return false; // Unresolvable external function found...
Chris Lattner2b4c8df2003-06-30 05:27:53 +000084 return true; // otherwise ok
85 }
86
87public:
88 static DSCallSiteIterator begin_aux(DSGraph &G) {
89 return G.getAuxFunctionCalls();
90 }
91 static DSCallSiteIterator end_aux(DSGraph &G) {
92 return DSCallSiteIterator(G.getAuxFunctionCalls(), true);
93 }
94 static DSCallSiteIterator begin_std(DSGraph &G) {
95 return G.getFunctionCalls();
96 }
97 static DSCallSiteIterator end_std(DSGraph &G) {
98 return DSCallSiteIterator(G.getFunctionCalls(), true);
99 }
100 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; }
101 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) {
102 return DSCallSiteIterator(CSs, true);
103 }
104 bool operator==(const DSCallSiteIterator &CSI) const {
105 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry;
106 }
107 bool operator!=(const DSCallSiteIterator &CSI) const {
108 return !operator==(CSI);
109 }
110
111 unsigned getCallSiteIdx() const { return CallSite; }
112 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; }
113
114 Function *operator*() const {
115 if ((*FCs)[CallSite].isDirectCall()) {
116 return (*FCs)[CallSite].getCalleeFunc();
117 } else {
118 DSNode *Node = (*FCs)[CallSite].getCalleeNode();
119 return cast<Function>(Node->getGlobals()[CallSiteEntry]);
120 }
121 }
122
123 DSCallSiteIterator& operator++() { // Preincrement
124 ++CallSiteEntry;
125 advanceToValidCallee();
126 return *this;
127 }
128 DSCallSiteIterator operator++(int) { // Postincrement
129 DSCallSiteIterator tmp = *this; ++*this; return tmp;
130 }
131};
132
133#endif