LLVM API Documentation
00001 //===- FunctionAttrs.cpp - Pass which marks functions readnone or readonly ===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file implements a simple interprocedural pass which walks the 00011 // call-graph, looking for functions which do not access or only read 00012 // non-local memory, and marking them readnone/readonly. In addition, 00013 // it marks function arguments (of pointer type) 'nocapture' if a call 00014 // to the function does not create any copies of the pointer value that 00015 // outlive the call. This more or less means that the pointer is only 00016 // dereferenced, and not returned from the function or stored in a global. 00017 // This pass is implemented as a bottom-up traversal of the call-graph. 00018 // 00019 //===----------------------------------------------------------------------===// 00020 00021 #define DEBUG_TYPE "functionattrs" 00022 #include "llvm/Transforms/IPO.h" 00023 #include "llvm/CallGraphSCCPass.h" 00024 #include "llvm/GlobalVariable.h" 00025 #include "llvm/Instructions.h" 00026 #include "llvm/Analysis/CallGraph.h" 00027 #include "llvm/Analysis/CaptureTracking.h" 00028 #include "llvm/ADT/SmallSet.h" 00029 #include "llvm/ADT/Statistic.h" 00030 #include "llvm/Support/Compiler.h" 00031 #include "llvm/Support/InstIterator.h" 00032 using namespace llvm; 00033 00034 STATISTIC(NumReadNone, "Number of functions marked readnone"); 00035 STATISTIC(NumReadOnly, "Number of functions marked readonly"); 00036 STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); 00037 00038 namespace { 00039 struct VISIBILITY_HIDDEN FunctionAttrs : public CallGraphSCCPass { 00040 static char ID; // Pass identification, replacement for typeid 00041 FunctionAttrs() : CallGraphSCCPass(&ID) {} 00042 00043 // runOnSCC - Analyze the SCC, performing the transformation if possible. 00044 bool runOnSCC(const std::vector<CallGraphNode *> &SCC); 00045 00046 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. 00047 bool AddReadAttrs(const std::vector<CallGraphNode *> &SCC); 00048 00049 // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. 00050 bool AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC); 00051 00052 virtual void getAnalysisUsage(AnalysisUsage &AU) const { 00053 AU.setPreservesCFG(); 00054 CallGraphSCCPass::getAnalysisUsage(AU); 00055 } 00056 00057 bool PointsToLocalMemory(Value *V); 00058 }; 00059 } 00060 00061 char FunctionAttrs::ID = 0; 00062 static RegisterPass<FunctionAttrs> 00063 X("functionattrs", "Deduce function attributes"); 00064 00065 Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } 00066 00067 00068 /// PointsToLocalMemory - Returns whether the given pointer value points to 00069 /// memory that is local to the function. Global constants are considered 00070 /// local to all functions. 00071 bool FunctionAttrs::PointsToLocalMemory(Value *V) { 00072 V = V->getUnderlyingObject(); 00073 // An alloca instruction defines local memory. 00074 if (isa<AllocaInst>(V)) 00075 return true; 00076 // A global constant counts as local memory for our purposes. 00077 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) 00078 return GV->isConstant(); 00079 // Could look through phi nodes and selects here, but it doesn't seem 00080 // to be useful in practice. 00081 return false; 00082 } 00083 00084 /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. 00085 bool FunctionAttrs::AddReadAttrs(const std::vector<CallGraphNode *> &SCC) { 00086 SmallPtrSet<CallGraphNode*, 8> SCCNodes; 00087 CallGraph &CG = getAnalysis<CallGraph>(); 00088 00089 // Fill SCCNodes with the elements of the SCC. Used for quickly 00090 // looking up whether a given CallGraphNode is in this SCC. 00091 for (unsigned i = 0, e = SCC.size(); i != e; ++i) 00092 SCCNodes.insert(SCC[i]); 00093 00094 // Check if any of the functions in the SCC read or write memory. If they 00095 // write memory then they can't be marked readnone or readonly. 00096 bool ReadsMemory = false; 00097 for (unsigned i = 0, e = SCC.size(); i != e; ++i) { 00098 Function *F = SCC[i]->getFunction(); 00099 00100 if (F == 0) 00101 // External node - may write memory. Just give up. 00102 return false; 00103 00104 if (F->doesNotAccessMemory()) 00105 // Already perfect! 00106 continue; 00107 00108 // Definitions with weak linkage may be overridden at linktime with 00109 // something that writes memory, so treat them like declarations. 00110 if (F->isDeclaration() || F->mayBeOverridden()) { 00111 if (!F->onlyReadsMemory()) 00112 // May write memory. Just give up. 00113 return false; 00114 00115 ReadsMemory = true; 00116 continue; 00117 } 00118 00119 // Scan the function body for instructions that may read or write memory. 00120 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { 00121 Instruction *I = &*II; 00122 00123 // Some instructions can be ignored even if they read or write memory. 00124 // Detect these now, skipping to the next instruction if one is found. 00125 CallSite CS = CallSite::get(I); 00126 if (CS.getInstruction()) { 00127 // Ignore calls to functions in the same SCC. 00128 if (SCCNodes.count(CG[CS.getCalledFunction()])) 00129 continue; 00130 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { 00131 // Ignore loads from local memory. 00132 if (PointsToLocalMemory(LI->getPointerOperand())) 00133 continue; 00134 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { 00135 // Ignore stores to local memory. 00136 if (PointsToLocalMemory(SI->getPointerOperand())) 00137 continue; 00138 } 00139 00140 // Any remaining instructions need to be taken seriously! Check if they 00141 // read or write memory. 00142 if (I->mayWriteToMemory()) 00143 // Writes memory. Just give up. 00144 return false; 00145 // If this instruction may read memory, remember that. 00146 ReadsMemory |= I->mayReadFromMemory(); 00147 } 00148 } 00149 00150 // Success! Functions in this SCC do not access memory, or only read memory. 00151 // Give them the appropriate attribute. 00152 bool MadeChange = false; 00153 for (unsigned i = 0, e = SCC.size(); i != e; ++i) { 00154 Function *F = SCC[i]->getFunction(); 00155 00156 if (F->doesNotAccessMemory()) 00157 // Already perfect! 00158 continue; 00159 00160 if (F->onlyReadsMemory() && ReadsMemory) 00161 // No change. 00162 continue; 00163 00164 MadeChange = true; 00165 00166 // Clear out any existing attributes. 00167 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); 00168 00169 // Add in the new attribute. 00170 F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone); 00171 00172 if (ReadsMemory) 00173 ++NumReadOnly; 00174 else 00175 ++NumReadNone; 00176 } 00177 00178 return MadeChange; 00179 } 00180 00181 /// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. 00182 bool FunctionAttrs::AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC) { 00183 bool Changed = false; 00184 00185 // Check each function in turn, determining which pointer arguments are not 00186 // captured. 00187 for (unsigned i = 0, e = SCC.size(); i != e; ++i) { 00188 Function *F = SCC[i]->getFunction(); 00189 00190 if (F == 0) 00191 // External node - skip it; 00192 continue; 00193 00194 // Definitions with weak linkage may be overridden at linktime with 00195 // something that writes memory, so treat them like declarations. 00196 if (F->isDeclaration() || F->mayBeOverridden()) 00197 continue; 00198 00199 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A) 00200 if (isa<PointerType>(A->getType()) && !A->hasNoCaptureAttr() && 00201 !PointerMayBeCaptured(A, true)) { 00202 A->addAttr(Attribute::NoCapture); 00203 ++NumNoCapture; 00204 Changed = true; 00205 } 00206 } 00207 00208 return Changed; 00209 } 00210 00211 bool FunctionAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) { 00212 bool Changed = AddReadAttrs(SCC); 00213 Changed |= AddNoCaptureAttrs(SCC); 00214 return Changed; 00215 }