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