Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 1 | //===- FunctionAttrs.cpp - Pass which marks functions readnone or readonly ===// |
| 2 | // |
| 3 | // The LLVM Compiler Infrastructure |
| 4 | // |
| 5 | // This file is distributed under the University of Illinois Open Source |
| 6 | // License. See LICENSE.TXT for details. |
| 7 | // |
| 8 | //===----------------------------------------------------------------------===// |
| 9 | // |
| 10 | // This file implements a simple interprocedural pass which walks the |
| 11 | // call-graph, looking for functions which do not access or only read |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 12 | // non-local memory, and marking them readnone/readonly. In addition, |
| 13 | // it marks function arguments (of pointer type) 'nocapture' if a call |
| 14 | // to the function does not create any copies of the pointer value that |
| 15 | // outlive the call. This more or less means that the pointer is only |
| 16 | // dereferenced, and not returned from the function or stored in a global. |
| 17 | // This pass is implemented as a bottom-up traversal of the call-graph. |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 18 | // |
| 19 | //===----------------------------------------------------------------------===// |
| 20 | |
| 21 | #define DEBUG_TYPE "functionattrs" |
| 22 | #include "llvm/Transforms/IPO.h" |
| 23 | #include "llvm/CallGraphSCCPass.h" |
| 24 | #include "llvm/GlobalVariable.h" |
| 25 | #include "llvm/Instructions.h" |
| 26 | #include "llvm/Analysis/CallGraph.h" |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame^] | 27 | #include "llvm/Analysis/CaptureTracking.h" |
Duncan Sands | 338cd6b | 2009-01-02 11:54:37 +0000 | [diff] [blame] | 28 | #include "llvm/ADT/SmallSet.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/Statistic.h" |
| 30 | #include "llvm/Support/Compiler.h" |
| 31 | #include "llvm/Support/InstIterator.h" |
| 32 | using namespace llvm; |
| 33 | |
| 34 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 35 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 36 | STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); |
| 37 | |
| 38 | namespace { |
| 39 | struct VISIBILITY_HIDDEN FunctionAttrs : public CallGraphSCCPass { |
| 40 | static char ID; // Pass identification, replacement for typeid |
| 41 | FunctionAttrs() : CallGraphSCCPass(&ID) {} |
| 42 | |
| 43 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
| 44 | bool runOnSCC(const std::vector<CallGraphNode *> &SCC); |
| 45 | |
| 46 | // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
| 47 | bool AddReadAttrs(const std::vector<CallGraphNode *> &SCC); |
| 48 | |
| 49 | // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
| 50 | bool AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC); |
| 51 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 52 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 53 | AU.setPreservesCFG(); |
| 54 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 55 | } |
| 56 | |
| 57 | bool PointsToLocalMemory(Value *V); |
| 58 | }; |
| 59 | } |
| 60 | |
| 61 | char FunctionAttrs::ID = 0; |
| 62 | static RegisterPass<FunctionAttrs> |
| 63 | X("functionattrs", "Deduce function attributes"); |
| 64 | |
| 65 | Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } |
| 66 | |
| 67 | |
| 68 | /// PointsToLocalMemory - Returns whether the given pointer value points to |
| 69 | /// memory that is local to the function. Global constants are considered |
| 70 | /// local to all functions. |
| 71 | bool FunctionAttrs::PointsToLocalMemory(Value *V) { |
| 72 | V = V->getUnderlyingObject(); |
| 73 | // An alloca instruction defines local memory. |
| 74 | if (isa<AllocaInst>(V)) |
| 75 | return true; |
| 76 | // A global constant counts as local memory for our purposes. |
| 77 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 78 | return GV->isConstant(); |
| 79 | // Could look through phi nodes and selects here, but it doesn't seem |
| 80 | // to be useful in practice. |
| 81 | return false; |
| 82 | } |
| 83 | |
| 84 | /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
| 85 | bool FunctionAttrs::AddReadAttrs(const std::vector<CallGraphNode *> &SCC) { |
| 86 | SmallPtrSet<CallGraphNode*, 8> SCCNodes; |
| 87 | CallGraph &CG = getAnalysis<CallGraph>(); |
| 88 | |
| 89 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 90 | // looking up whether a given CallGraphNode is in this SCC. |
| 91 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
| 92 | SCCNodes.insert(SCC[i]); |
| 93 | |
| 94 | // Check if any of the functions in the SCC read or write memory. If they |
| 95 | // write memory then they can't be marked readnone or readonly. |
| 96 | bool ReadsMemory = false; |
| 97 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 98 | Function *F = SCC[i]->getFunction(); |
| 99 | |
| 100 | if (F == 0) |
| 101 | // External node - may write memory. Just give up. |
| 102 | return false; |
| 103 | |
| 104 | if (F->doesNotAccessMemory()) |
| 105 | // Already perfect! |
| 106 | continue; |
| 107 | |
| 108 | // Definitions with weak linkage may be overridden at linktime with |
| 109 | // something that writes memory, so treat them like declarations. |
| 110 | if (F->isDeclaration() || F->mayBeOverridden()) { |
| 111 | if (!F->onlyReadsMemory()) |
| 112 | // May write memory. Just give up. |
| 113 | return false; |
| 114 | |
| 115 | ReadsMemory = true; |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | // Scan the function body for instructions that may read or write memory. |
| 120 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 121 | Instruction *I = &*II; |
| 122 | |
| 123 | // Some instructions can be ignored even if they read or write memory. |
| 124 | // Detect these now, skipping to the next instruction if one is found. |
| 125 | CallSite CS = CallSite::get(I); |
| 126 | if (CS.getInstruction()) { |
| 127 | // Ignore calls to functions in the same SCC. |
| 128 | if (SCCNodes.count(CG[CS.getCalledFunction()])) |
| 129 | continue; |
| 130 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 131 | // Ignore loads from local memory. |
| 132 | if (PointsToLocalMemory(LI->getPointerOperand())) |
| 133 | continue; |
| 134 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 135 | // Ignore stores to local memory. |
| 136 | if (PointsToLocalMemory(SI->getPointerOperand())) |
| 137 | continue; |
| 138 | } |
| 139 | |
| 140 | // Any remaining instructions need to be taken seriously! Check if they |
| 141 | // read or write memory. |
| 142 | if (I->mayWriteToMemory()) |
| 143 | // Writes memory. Just give up. |
| 144 | return false; |
| 145 | // If this instruction may read memory, remember that. |
| 146 | ReadsMemory |= I->mayReadFromMemory(); |
| 147 | } |
| 148 | } |
| 149 | |
| 150 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 151 | // Give them the appropriate attribute. |
| 152 | bool MadeChange = false; |
| 153 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 154 | Function *F = SCC[i]->getFunction(); |
| 155 | |
| 156 | if (F->doesNotAccessMemory()) |
| 157 | // Already perfect! |
| 158 | continue; |
| 159 | |
| 160 | if (F->onlyReadsMemory() && ReadsMemory) |
| 161 | // No change. |
| 162 | continue; |
| 163 | |
| 164 | MadeChange = true; |
| 165 | |
| 166 | // Clear out any existing attributes. |
| 167 | F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); |
| 168 | |
| 169 | // Add in the new attribute. |
| 170 | F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone); |
| 171 | |
| 172 | if (ReadsMemory) |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 173 | ++NumReadOnly; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 174 | else |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 175 | ++NumReadNone; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 176 | } |
| 177 | |
| 178 | return MadeChange; |
| 179 | } |
| 180 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 181 | /// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
| 182 | bool FunctionAttrs::AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC) { |
| 183 | bool Changed = false; |
| 184 | |
| 185 | // Check each function in turn, determining which pointer arguments are not |
| 186 | // captured. |
| 187 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 188 | Function *F = SCC[i]->getFunction(); |
| 189 | |
| 190 | if (F == 0) |
| 191 | // External node - skip it; |
| 192 | continue; |
| 193 | |
| 194 | // Definitions with weak linkage may be overridden at linktime with |
| 195 | // something that writes memory, so treat them like declarations. |
| 196 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 197 | continue; |
| 198 | |
| 199 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A) |
Duncan Sands | 17da06f | 2008-12-31 18:08:59 +0000 | [diff] [blame] | 200 | if (isa<PointerType>(A->getType()) && !A->hasNoCaptureAttr() && |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame^] | 201 | !PointerMayBeCaptured(A, true)) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 202 | A->addAttr(Attribute::NoCapture); |
Nick Lewycky | 6b05686 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 203 | ++NumNoCapture; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 204 | Changed = true; |
| 205 | } |
| 206 | } |
| 207 | |
| 208 | return Changed; |
| 209 | } |
| 210 | |
| 211 | bool FunctionAttrs::runOnSCC(const std::vector<CallGraphNode *> &SCC) { |
| 212 | bool Changed = AddReadAttrs(SCC); |
| 213 | Changed |= AddNoCaptureAttrs(SCC); |
| 214 | return Changed; |
| 215 | } |