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" |
Devang Patel | cd11991 | 2009-03-03 00:28:44 +0000 | [diff] [blame] | 25 | #include "llvm/IntrinsicInst.h" |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 26 | #include "llvm/Analysis/AliasAnalysis.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/CallGraph.h" |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CaptureTracking.h" |
Duncan Sands | 338cd6b | 2009-01-02 11:54:37 +0000 | [diff] [blame] | 29 | #include "llvm/ADT/SmallSet.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/UniqueVector.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 32 | #include "llvm/Support/Compiler.h" |
| 33 | #include "llvm/Support/InstIterator.h" |
| 34 | using namespace llvm; |
| 35 | |
| 36 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 37 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 38 | STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 39 | STATISTIC(NumNoAlias, "Number of function returns marked noalias"); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 40 | |
| 41 | namespace { |
| 42 | struct VISIBILITY_HIDDEN FunctionAttrs : public CallGraphSCCPass { |
| 43 | static char ID; // Pass identification, replacement for typeid |
| 44 | FunctionAttrs() : CallGraphSCCPass(&ID) {} |
| 45 | |
| 46 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
Chris Lattner | 5095e3d | 2009-08-31 00:19:58 +0000 | [diff] [blame] | 47 | bool runOnSCC(std::vector<CallGraphNode *> &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 48 | |
| 49 | // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
| 50 | bool AddReadAttrs(const std::vector<CallGraphNode *> &SCC); |
| 51 | |
| 52 | // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
| 53 | bool AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC); |
| 54 | |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 55 | // IsFunctionMallocLike - Does this function allocate new memory? |
| 56 | bool IsFunctionMallocLike(Function *F, |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 57 | SmallPtrSet<Function*, 8> &) const; |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 58 | |
| 59 | // AddNoAliasAttrs - Deduce noalias attributes for the SCC. |
| 60 | bool AddNoAliasAttrs(const std::vector<CallGraphNode *> &SCC); |
| 61 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 62 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 63 | AU.setPreservesCFG(); |
| 64 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 65 | } |
| 66 | |
| 67 | bool PointsToLocalMemory(Value *V); |
| 68 | }; |
| 69 | } |
| 70 | |
| 71 | char FunctionAttrs::ID = 0; |
| 72 | static RegisterPass<FunctionAttrs> |
| 73 | X("functionattrs", "Deduce function attributes"); |
| 74 | |
| 75 | Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } |
| 76 | |
| 77 | |
| 78 | /// PointsToLocalMemory - Returns whether the given pointer value points to |
| 79 | /// memory that is local to the function. Global constants are considered |
| 80 | /// local to all functions. |
| 81 | bool FunctionAttrs::PointsToLocalMemory(Value *V) { |
| 82 | V = V->getUnderlyingObject(); |
| 83 | // An alloca instruction defines local memory. |
| 84 | if (isa<AllocaInst>(V)) |
| 85 | return true; |
| 86 | // A global constant counts as local memory for our purposes. |
| 87 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) |
| 88 | return GV->isConstant(); |
| 89 | // Could look through phi nodes and selects here, but it doesn't seem |
| 90 | // to be useful in practice. |
| 91 | return false; |
| 92 | } |
| 93 | |
| 94 | /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
| 95 | bool FunctionAttrs::AddReadAttrs(const std::vector<CallGraphNode *> &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 96 | SmallPtrSet<Function*, 8> SCCNodes; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 97 | |
| 98 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 99 | // looking up whether a given CallGraphNode is in this SCC. |
| 100 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 101 | SCCNodes.insert(SCC[i]->getFunction()); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 102 | |
| 103 | // Check if any of the functions in the SCC read or write memory. If they |
| 104 | // write memory then they can't be marked readnone or readonly. |
| 105 | bool ReadsMemory = false; |
| 106 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 107 | Function *F = SCC[i]->getFunction(); |
| 108 | |
| 109 | if (F == 0) |
| 110 | // External node - may write memory. Just give up. |
| 111 | return false; |
| 112 | |
| 113 | if (F->doesNotAccessMemory()) |
| 114 | // Already perfect! |
| 115 | continue; |
| 116 | |
| 117 | // Definitions with weak linkage may be overridden at linktime with |
| 118 | // something that writes memory, so treat them like declarations. |
| 119 | if (F->isDeclaration() || F->mayBeOverridden()) { |
| 120 | if (!F->onlyReadsMemory()) |
| 121 | // May write memory. Just give up. |
| 122 | return false; |
| 123 | |
| 124 | ReadsMemory = true; |
| 125 | continue; |
| 126 | } |
| 127 | |
| 128 | // Scan the function body for instructions that may read or write memory. |
| 129 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 130 | Instruction *I = &*II; |
| 131 | |
| 132 | // Some instructions can be ignored even if they read or write memory. |
| 133 | // Detect these now, skipping to the next instruction if one is found. |
| 134 | CallSite CS = CallSite::get(I); |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 135 | if (CS.getInstruction() && CS.getCalledFunction()) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 136 | // Ignore calls to functions in the same SCC. |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 137 | if (SCCNodes.count(CS.getCalledFunction())) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 138 | continue; |
| 139 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 140 | // Ignore loads from local memory. |
| 141 | if (PointsToLocalMemory(LI->getPointerOperand())) |
| 142 | continue; |
| 143 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 144 | // Ignore stores to local memory. |
| 145 | if (PointsToLocalMemory(SI->getPointerOperand())) |
| 146 | continue; |
| 147 | } |
| 148 | |
| 149 | // Any remaining instructions need to be taken seriously! Check if they |
| 150 | // read or write memory. |
| 151 | if (I->mayWriteToMemory()) |
| 152 | // Writes memory. Just give up. |
| 153 | return false; |
Duncan Sands | cfd0ebe | 2009-05-06 08:42:00 +0000 | [diff] [blame] | 154 | |
| 155 | if (isa<MallocInst>(I)) |
| 156 | // MallocInst claims not to write memory! PR3754. |
| 157 | return false; |
| 158 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 159 | // If this instruction may read memory, remember that. |
| 160 | ReadsMemory |= I->mayReadFromMemory(); |
| 161 | } |
| 162 | } |
| 163 | |
| 164 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 165 | // Give them the appropriate attribute. |
| 166 | bool MadeChange = false; |
| 167 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 168 | Function *F = SCC[i]->getFunction(); |
| 169 | |
| 170 | if (F->doesNotAccessMemory()) |
| 171 | // Already perfect! |
| 172 | continue; |
| 173 | |
| 174 | if (F->onlyReadsMemory() && ReadsMemory) |
| 175 | // No change. |
| 176 | continue; |
| 177 | |
| 178 | MadeChange = true; |
| 179 | |
| 180 | // Clear out any existing attributes. |
| 181 | F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); |
| 182 | |
| 183 | // Add in the new attribute. |
| 184 | F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone); |
| 185 | |
| 186 | if (ReadsMemory) |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 187 | ++NumReadOnly; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 188 | else |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 189 | ++NumReadNone; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 190 | } |
| 191 | |
| 192 | return MadeChange; |
| 193 | } |
| 194 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 195 | /// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
| 196 | bool FunctionAttrs::AddNoCaptureAttrs(const std::vector<CallGraphNode *> &SCC) { |
| 197 | bool Changed = false; |
| 198 | |
| 199 | // Check each function in turn, determining which pointer arguments are not |
| 200 | // captured. |
| 201 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 202 | Function *F = SCC[i]->getFunction(); |
| 203 | |
| 204 | if (F == 0) |
| 205 | // External node - skip it; |
| 206 | continue; |
| 207 | |
| 208 | // Definitions with weak linkage may be overridden at linktime with |
| 209 | // something that writes memory, so treat them like declarations. |
| 210 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 211 | continue; |
| 212 | |
| 213 | 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] | 214 | if (isa<PointerType>(A->getType()) && !A->hasNoCaptureAttr() && |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 215 | !PointerMayBeCaptured(A, true)) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 216 | A->addAttr(Attribute::NoCapture); |
Nick Lewycky | 6b05686 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 217 | ++NumNoCapture; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 218 | Changed = true; |
| 219 | } |
| 220 | } |
| 221 | |
| 222 | return Changed; |
| 223 | } |
| 224 | |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 225 | /// IsFunctionMallocLike - A function is malloc-like if it returns either null |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 226 | /// or a pointer that doesn't alias any other pointer visible to the caller. |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 227 | bool FunctionAttrs::IsFunctionMallocLike(Function *F, |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 228 | SmallPtrSet<Function*, 8> &SCCNodes) const { |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 229 | UniqueVector<Value *> FlowsToReturn; |
| 230 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 231 | if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) |
| 232 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 233 | |
| 234 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
| 235 | Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved. |
| 236 | |
| 237 | if (Constant *C = dyn_cast<Constant>(RetVal)) { |
| 238 | if (!C->isNullValue() && !isa<UndefValue>(C)) |
| 239 | return false; |
| 240 | |
| 241 | continue; |
| 242 | } |
| 243 | |
| 244 | if (isa<Argument>(RetVal)) |
| 245 | return false; |
| 246 | |
| 247 | if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
| 248 | switch (RVI->getOpcode()) { |
| 249 | // Extend the analysis by looking upwards. |
| 250 | case Instruction::GetElementPtr: |
| 251 | case Instruction::BitCast: |
| 252 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 253 | continue; |
| 254 | case Instruction::Select: { |
| 255 | SelectInst *SI = cast<SelectInst>(RVI); |
| 256 | FlowsToReturn.insert(SI->getTrueValue()); |
| 257 | FlowsToReturn.insert(SI->getFalseValue()); |
| 258 | } continue; |
| 259 | case Instruction::PHI: { |
| 260 | PHINode *PN = cast<PHINode>(RVI); |
| 261 | for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 262 | FlowsToReturn.insert(PN->getIncomingValue(i)); |
| 263 | } continue; |
| 264 | |
| 265 | // Check whether the pointer came from an allocation. |
| 266 | case Instruction::Alloca: |
| 267 | case Instruction::Malloc: |
| 268 | break; |
| 269 | case Instruction::Call: |
| 270 | case Instruction::Invoke: { |
| 271 | CallSite CS(RVI); |
| 272 | if (CS.paramHasAttr(0, Attribute::NoAlias)) |
| 273 | break; |
| 274 | if (CS.getCalledFunction() && |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 275 | SCCNodes.count(CS.getCalledFunction())) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 276 | break; |
| 277 | } // fall-through |
| 278 | default: |
| 279 | return false; // Did not come from an allocation. |
| 280 | } |
| 281 | |
| 282 | if (PointerMayBeCaptured(RetVal, false)) |
| 283 | return false; |
| 284 | } |
| 285 | |
| 286 | return true; |
| 287 | } |
| 288 | |
| 289 | /// AddNoAliasAttrs - Deduce noalias attributes for the SCC. |
| 290 | bool FunctionAttrs::AddNoAliasAttrs(const std::vector<CallGraphNode *> &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 291 | SmallPtrSet<Function*, 8> SCCNodes; |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 292 | |
| 293 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 294 | // looking up whether a given CallGraphNode is in this SCC. |
| 295 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame^] | 296 | SCCNodes.insert(SCC[i]->getFunction()); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 297 | |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 298 | // Check each function in turn, determining which functions return noalias |
| 299 | // pointers. |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 300 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 301 | Function *F = SCC[i]->getFunction(); |
| 302 | |
| 303 | if (F == 0) |
| 304 | // External node - skip it; |
| 305 | return false; |
| 306 | |
| 307 | // Already noalias. |
| 308 | if (F->doesNotAlias(0)) |
| 309 | continue; |
| 310 | |
| 311 | // Definitions with weak linkage may be overridden at linktime, so |
| 312 | // treat them like declarations. |
| 313 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 314 | return false; |
| 315 | |
| 316 | // We annotate noalias return values, which are only applicable to |
| 317 | // pointer types. |
| 318 | if (!isa<PointerType>(F->getReturnType())) |
| 319 | continue; |
| 320 | |
| 321 | if (!IsFunctionMallocLike(F, SCCNodes)) |
| 322 | return false; |
| 323 | } |
| 324 | |
| 325 | bool MadeChange = false; |
| 326 | for (unsigned i = 0, e = SCC.size(); i != e; ++i) { |
| 327 | Function *F = SCC[i]->getFunction(); |
| 328 | if (F->doesNotAlias(0) || !isa<PointerType>(F->getReturnType())) |
| 329 | continue; |
| 330 | |
| 331 | F->setDoesNotAlias(0); |
| 332 | ++NumNoAlias; |
| 333 | MadeChange = true; |
| 334 | } |
| 335 | |
| 336 | return MadeChange; |
| 337 | } |
| 338 | |
Chris Lattner | 5095e3d | 2009-08-31 00:19:58 +0000 | [diff] [blame] | 339 | bool FunctionAttrs::runOnSCC(std::vector<CallGraphNode *> &SCC) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 340 | bool Changed = AddReadAttrs(SCC); |
| 341 | Changed |= AddNoCaptureAttrs(SCC); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 342 | Changed |= AddNoAliasAttrs(SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 343 | return Changed; |
| 344 | } |