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" |
Victor Hernandez | f006b18 | 2009-10-27 20:05:49 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/MemoryBuiltins.h" |
Duncan Sands | 338cd6b | 2009-01-02 11:54:37 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SmallSet.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/UniqueVector.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 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 { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 42 | struct FunctionAttrs : public CallGraphSCCPass { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 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 | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 47 | bool runOnSCC(CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 48 | |
| 49 | // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 50 | bool AddReadAttrs(const CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 51 | |
| 52 | // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 53 | bool AddNoCaptureAttrs(const CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 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. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 60 | bool AddNoAliasAttrs(const CallGraphSCC &SCC); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 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; |
Owen Anderson | d13db2c | 2010-07-21 22:09:45 +0000 | [diff] [blame] | 72 | INITIALIZE_PASS(FunctionAttrs, "functionattrs", |
| 73 | "Deduce function attributes", false, false); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 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) { |
Duncan Sands | 5d8ea11 | 2010-01-07 05:48:42 +0000 | [diff] [blame] | 82 | SmallVector<Value*, 16> Worklist; |
| 83 | unsigned MaxLookup = 8; |
Duncan Sands | e10920d | 2010-01-06 15:37:47 +0000 | [diff] [blame] | 84 | |
| 85 | Worklist.push_back(V); |
| 86 | |
| 87 | do { |
| 88 | V = Worklist.pop_back_val()->getUnderlyingObject(); |
| 89 | |
| 90 | // An alloca instruction defines local memory. |
| 91 | if (isa<AllocaInst>(V)) |
| 92 | continue; |
| 93 | |
| 94 | // A global constant counts as local memory for our purposes. |
| 95 | if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) { |
| 96 | if (!GV->isConstant()) |
| 97 | return false; |
| 98 | continue; |
| 99 | } |
| 100 | |
| 101 | // If both select values point to local memory, then so does the select. |
| 102 | if (SelectInst *SI = dyn_cast<SelectInst>(V)) { |
| 103 | Worklist.push_back(SI->getTrueValue()); |
| 104 | Worklist.push_back(SI->getFalseValue()); |
| 105 | continue; |
| 106 | } |
| 107 | |
| 108 | // If all values incoming to a phi node point to local memory, then so does |
| 109 | // the phi. |
| 110 | if (PHINode *PN = dyn_cast<PHINode>(V)) { |
| 111 | // Don't bother inspecting phi nodes with many operands. |
| 112 | if (PN->getNumIncomingValues() > MaxLookup) |
| 113 | return false; |
| 114 | for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 115 | Worklist.push_back(PN->getIncomingValue(i)); |
| 116 | continue; |
| 117 | } |
| 118 | |
| 119 | return false; |
| 120 | } while (!Worklist.empty() && --MaxLookup); |
| 121 | |
| 122 | return Worklist.empty(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 126 | bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 127 | SmallPtrSet<Function*, 8> SCCNodes; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 128 | |
| 129 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 130 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 131 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 132 | SCCNodes.insert((*I)->getFunction()); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 133 | |
| 134 | // Check if any of the functions in the SCC read or write memory. If they |
| 135 | // write memory then they can't be marked readnone or readonly. |
| 136 | bool ReadsMemory = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 137 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 138 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 139 | |
| 140 | if (F == 0) |
| 141 | // External node - may write memory. Just give up. |
| 142 | return false; |
| 143 | |
| 144 | if (F->doesNotAccessMemory()) |
| 145 | // Already perfect! |
| 146 | continue; |
| 147 | |
| 148 | // Definitions with weak linkage may be overridden at linktime with |
| 149 | // something that writes memory, so treat them like declarations. |
| 150 | if (F->isDeclaration() || F->mayBeOverridden()) { |
| 151 | if (!F->onlyReadsMemory()) |
| 152 | // May write memory. Just give up. |
| 153 | return false; |
| 154 | |
| 155 | ReadsMemory = true; |
| 156 | continue; |
| 157 | } |
| 158 | |
| 159 | // Scan the function body for instructions that may read or write memory. |
| 160 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 161 | Instruction *I = &*II; |
| 162 | |
| 163 | // Some instructions can be ignored even if they read or write memory. |
| 164 | // Detect these now, skipping to the next instruction if one is found. |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame^] | 165 | CallSite CS(cast<Value>(I)); |
| 166 | if (CS && CS.getCalledFunction()) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 167 | // Ignore calls to functions in the same SCC. |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 168 | if (SCCNodes.count(CS.getCalledFunction())) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 169 | continue; |
Duncan Sands | 7c422ac | 2010-01-06 08:45:52 +0000 | [diff] [blame] | 170 | // Ignore intrinsics that only access local memory. |
| 171 | if (unsigned id = CS.getCalledFunction()->getIntrinsicID()) |
| 172 | if (AliasAnalysis::getModRefBehavior(id) == |
| 173 | AliasAnalysis::AccessesArguments) { |
| 174 | // Check that all pointer arguments point to local memory. |
| 175 | for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 176 | CI != CE; ++CI) { |
| 177 | Value *Arg = *CI; |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 178 | if (Arg->getType()->isPointerTy() && !PointsToLocalMemory(Arg)) |
Duncan Sands | 7c422ac | 2010-01-06 08:45:52 +0000 | [diff] [blame] | 179 | // Writes memory. Just give up. |
| 180 | return false; |
| 181 | } |
| 182 | // Only reads and writes local memory. |
| 183 | continue; |
| 184 | } |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 185 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
| 186 | // Ignore loads from local memory. |
| 187 | if (PointsToLocalMemory(LI->getPointerOperand())) |
| 188 | continue; |
| 189 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
| 190 | // Ignore stores to local memory. |
| 191 | if (PointsToLocalMemory(SI->getPointerOperand())) |
| 192 | continue; |
| 193 | } |
| 194 | |
| 195 | // Any remaining instructions need to be taken seriously! Check if they |
| 196 | // read or write memory. |
| 197 | if (I->mayWriteToMemory()) |
| 198 | // Writes memory. Just give up. |
| 199 | return false; |
Duncan Sands | cfd0ebe | 2009-05-06 08:42:00 +0000 | [diff] [blame] | 200 | |
Victor Hernandez | a276c60 | 2009-10-17 01:18:07 +0000 | [diff] [blame] | 201 | if (isMalloc(I)) |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 202 | // malloc claims not to write memory! PR3754. |
Duncan Sands | cfd0ebe | 2009-05-06 08:42:00 +0000 | [diff] [blame] | 203 | return false; |
| 204 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 205 | // If this instruction may read memory, remember that. |
| 206 | ReadsMemory |= I->mayReadFromMemory(); |
| 207 | } |
| 208 | } |
| 209 | |
| 210 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 211 | // Give them the appropriate attribute. |
| 212 | bool MadeChange = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 213 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 214 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 215 | |
| 216 | if (F->doesNotAccessMemory()) |
| 217 | // Already perfect! |
| 218 | continue; |
| 219 | |
| 220 | if (F->onlyReadsMemory() && ReadsMemory) |
| 221 | // No change. |
| 222 | continue; |
| 223 | |
| 224 | MadeChange = true; |
| 225 | |
| 226 | // Clear out any existing attributes. |
| 227 | F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); |
| 228 | |
| 229 | // Add in the new attribute. |
| 230 | F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone); |
| 231 | |
| 232 | if (ReadsMemory) |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 233 | ++NumReadOnly; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 234 | else |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 235 | ++NumReadNone; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 236 | } |
| 237 | |
| 238 | return MadeChange; |
| 239 | } |
| 240 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 241 | /// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 242 | bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 243 | bool Changed = false; |
| 244 | |
| 245 | // Check each function in turn, determining which pointer arguments are not |
| 246 | // captured. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 247 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 248 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 249 | |
| 250 | if (F == 0) |
| 251 | // External node - skip it; |
| 252 | continue; |
| 253 | |
| 254 | // Definitions with weak linkage may be overridden at linktime with |
| 255 | // something that writes memory, so treat them like declarations. |
| 256 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 257 | continue; |
| 258 | |
| 259 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A) |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 260 | if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr() && |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 261 | !PointerMayBeCaptured(A, true, /*StoreCaptures=*/false)) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 262 | A->addAttr(Attribute::NoCapture); |
Nick Lewycky | 6b05686 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 263 | ++NumNoCapture; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 264 | Changed = true; |
| 265 | } |
| 266 | } |
| 267 | |
| 268 | return Changed; |
| 269 | } |
| 270 | |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 271 | /// IsFunctionMallocLike - A function is malloc-like if it returns either null |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 272 | /// 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] | 273 | bool FunctionAttrs::IsFunctionMallocLike(Function *F, |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 274 | SmallPtrSet<Function*, 8> &SCCNodes) const { |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 275 | UniqueVector<Value *> FlowsToReturn; |
| 276 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 277 | if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) |
| 278 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 279 | |
| 280 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
| 281 | Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved. |
| 282 | |
| 283 | if (Constant *C = dyn_cast<Constant>(RetVal)) { |
| 284 | if (!C->isNullValue() && !isa<UndefValue>(C)) |
| 285 | return false; |
| 286 | |
| 287 | continue; |
| 288 | } |
| 289 | |
| 290 | if (isa<Argument>(RetVal)) |
| 291 | return false; |
| 292 | |
| 293 | if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
| 294 | switch (RVI->getOpcode()) { |
| 295 | // Extend the analysis by looking upwards. |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 296 | case Instruction::BitCast: |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 297 | case Instruction::GetElementPtr: |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 298 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 299 | continue; |
| 300 | case Instruction::Select: { |
| 301 | SelectInst *SI = cast<SelectInst>(RVI); |
| 302 | FlowsToReturn.insert(SI->getTrueValue()); |
| 303 | FlowsToReturn.insert(SI->getFalseValue()); |
Chris Lattner | 439044f | 2009-09-27 21:29:28 +0000 | [diff] [blame] | 304 | continue; |
| 305 | } |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 306 | case Instruction::PHI: { |
| 307 | PHINode *PN = cast<PHINode>(RVI); |
| 308 | for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 309 | FlowsToReturn.insert(PN->getIncomingValue(i)); |
Chris Lattner | 439044f | 2009-09-27 21:29:28 +0000 | [diff] [blame] | 310 | continue; |
| 311 | } |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 312 | |
| 313 | // Check whether the pointer came from an allocation. |
| 314 | case Instruction::Alloca: |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 315 | break; |
| 316 | case Instruction::Call: |
| 317 | case Instruction::Invoke: { |
| 318 | CallSite CS(RVI); |
| 319 | if (CS.paramHasAttr(0, Attribute::NoAlias)) |
| 320 | break; |
| 321 | if (CS.getCalledFunction() && |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 322 | SCCNodes.count(CS.getCalledFunction())) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 323 | break; |
| 324 | } // fall-through |
| 325 | default: |
| 326 | return false; // Did not come from an allocation. |
| 327 | } |
| 328 | |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 329 | if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 330 | return false; |
| 331 | } |
| 332 | |
| 333 | return true; |
| 334 | } |
| 335 | |
| 336 | /// AddNoAliasAttrs - Deduce noalias attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 337 | bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 338 | SmallPtrSet<Function*, 8> SCCNodes; |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 339 | |
| 340 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 341 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 342 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 343 | SCCNodes.insert((*I)->getFunction()); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 344 | |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 345 | // Check each function in turn, determining which functions return noalias |
| 346 | // pointers. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 347 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 348 | Function *F = (*I)->getFunction(); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 349 | |
| 350 | if (F == 0) |
| 351 | // External node - skip it; |
| 352 | return false; |
| 353 | |
| 354 | // Already noalias. |
| 355 | if (F->doesNotAlias(0)) |
| 356 | continue; |
| 357 | |
| 358 | // Definitions with weak linkage may be overridden at linktime, so |
| 359 | // treat them like declarations. |
| 360 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 361 | return false; |
| 362 | |
| 363 | // We annotate noalias return values, which are only applicable to |
| 364 | // pointer types. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 365 | if (!F->getReturnType()->isPointerTy()) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 366 | continue; |
| 367 | |
| 368 | if (!IsFunctionMallocLike(F, SCCNodes)) |
| 369 | return false; |
| 370 | } |
| 371 | |
| 372 | bool MadeChange = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 373 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 374 | Function *F = (*I)->getFunction(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 375 | if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 376 | continue; |
| 377 | |
| 378 | F->setDoesNotAlias(0); |
| 379 | ++NumNoAlias; |
| 380 | MadeChange = true; |
| 381 | } |
| 382 | |
| 383 | return MadeChange; |
| 384 | } |
| 385 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 386 | bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 387 | bool Changed = AddReadAttrs(SCC); |
| 388 | Changed |= AddNoCaptureAttrs(SCC); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 389 | Changed |= AddNoAliasAttrs(SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 390 | return Changed; |
| 391 | } |