LLVM API Documentation
00001 //===- DSCallSiteIterator.h - Iterator for DSGraph call sites ---*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file was developed by the LLVM research group and is distributed under 00006 // the University of Illinois Open Source License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements an iterator for complete call sites in DSGraphs. This 00011 // code can either iterator over the normal call list or the aux calls list, and 00012 // is used by the TD and BU passes. 00013 // 00014 //===----------------------------------------------------------------------===// 00015 00016 #ifndef DSCALLSITEITERATOR_H 00017 #define DSCALLSITEITERATOR_H 00018 00019 #include "llvm/Analysis/DataStructure/DSGraph.h" 00020 #include "llvm/Function.h" 00021 00022 namespace llvm { 00023 00024 struct DSCallSiteIterator { 00025 // FCs are the edges out of the current node are the call site targets... 00026 const std::vector<DSCallSite> *FCs; 00027 unsigned CallSite; 00028 unsigned CallSiteEntry; 00029 00030 DSCallSiteIterator(const std::vector<DSCallSite> &CS) : FCs(&CS) { 00031 CallSite = 0; CallSiteEntry = 0; 00032 advanceToValidCallee(); 00033 } 00034 00035 // End iterator ctor... 00036 DSCallSiteIterator(const std::vector<DSCallSite> &CS, bool) : FCs(&CS) { 00037 CallSite = FCs->size(); CallSiteEntry = 0; 00038 } 00039 00040 static bool isVAHackFn(const Function *F) { 00041 return F->getName() == "printf" || F->getName() == "sscanf" || 00042 F->getName() == "fprintf" || F->getName() == "open" || 00043 F->getName() == "sprintf" || F->getName() == "fputs" || 00044 F->getName() == "fscanf"; 00045 } 00046 00047 // isUnresolvableFunction - Return true if this is an unresolvable 00048 // external function. A direct or indirect call to this cannot be resolved. 00049 // 00050 static bool isUnresolvableFunc(const Function* callee) { 00051 return callee->isExternal() && !isVAHackFn(callee); 00052 } 00053 00054 void advanceToValidCallee() { 00055 while (CallSite < FCs->size()) { 00056 if ((*FCs)[CallSite].isDirectCall()) { 00057 if (CallSiteEntry == 0 && // direct call only has one target... 00058 ! isUnresolvableFunc((*FCs)[CallSite].getCalleeFunc())) 00059 return; // and not an unresolvable external func 00060 } else { 00061 DSNode *CalleeNode = (*FCs)[CallSite].getCalleeNode(); 00062 if (CallSiteEntry || isCompleteNode(CalleeNode)) { 00063 const std::vector<GlobalValue*> &Callees = CalleeNode->getGlobals(); 00064 while (CallSiteEntry < Callees.size()) { 00065 if (isa<Function>(Callees[CallSiteEntry])) 00066 return; 00067 ++CallSiteEntry; 00068 } 00069 } 00070 } 00071 CallSiteEntry = 0; 00072 ++CallSite; 00073 } 00074 } 00075 00076 // isCompleteNode - Return true if we know all of the targets of this node, 00077 // and if the call sites are not external. 00078 // 00079 static inline bool isCompleteNode(DSNode *N) { 00080 if (N->isIncomplete()) return false; 00081 const std::vector<GlobalValue*> &Callees = N->getGlobals(); 00082 for (unsigned i = 0, e = Callees.size(); i != e; ++i) 00083 if (isUnresolvableFunc(cast<Function>(Callees[i]))) 00084 return false; // Unresolvable external function found... 00085 return true; // otherwise ok 00086 } 00087 00088 public: 00089 static DSCallSiteIterator begin_aux(DSGraph &G) { 00090 return G.getAuxFunctionCalls(); 00091 } 00092 static DSCallSiteIterator end_aux(DSGraph &G) { 00093 return DSCallSiteIterator(G.getAuxFunctionCalls(), true); 00094 } 00095 static DSCallSiteIterator begin_std(DSGraph &G) { 00096 return G.getFunctionCalls(); 00097 } 00098 static DSCallSiteIterator end_std(DSGraph &G) { 00099 return DSCallSiteIterator(G.getFunctionCalls(), true); 00100 } 00101 static DSCallSiteIterator begin(std::vector<DSCallSite> &CSs) { return CSs; } 00102 static DSCallSiteIterator end(std::vector<DSCallSite> &CSs) { 00103 return DSCallSiteIterator(CSs, true); 00104 } 00105 bool operator==(const DSCallSiteIterator &CSI) const { 00106 return CallSite == CSI.CallSite && CallSiteEntry == CSI.CallSiteEntry; 00107 } 00108 bool operator!=(const DSCallSiteIterator &CSI) const { 00109 return !operator==(CSI); 00110 } 00111 00112 unsigned getCallSiteIdx() const { return CallSite; } 00113 const DSCallSite &getCallSite() const { return (*FCs)[CallSite]; } 00114 00115 Function *operator*() const { 00116 if ((*FCs)[CallSite].isDirectCall()) { 00117 return (*FCs)[CallSite].getCalleeFunc(); 00118 } else { 00119 DSNode *Node = (*FCs)[CallSite].getCalleeNode(); 00120 return cast<Function>(Node->getGlobals()[CallSiteEntry]); 00121 } 00122 } 00123 00124 DSCallSiteIterator& operator++() { // Preincrement 00125 ++CallSiteEntry; 00126 advanceToValidCallee(); 00127 return *this; 00128 } 00129 DSCallSiteIterator operator++(int) { // Postincrement 00130 DSCallSiteIterator tmp = *this; ++*this; return tmp; 00131 } 00132 }; 00133 00134 } // End llvm namespace 00135 00136 #endif