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" |
Dan Gohman | ea8900f | 2010-11-08 17:12:04 +0000 | [diff] [blame] | 26 | #include "llvm/LLVMContext.h" |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 27 | #include "llvm/Analysis/AliasAnalysis.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 28 | #include "llvm/Analysis/CallGraph.h" |
Duncan Sands | 8556d2a | 2009-01-18 12:19:30 +0000 | [diff] [blame] | 29 | #include "llvm/Analysis/CaptureTracking.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 |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 44 | FunctionAttrs() : CallGraphSCCPass(ID), AA(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 45 | initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); |
| 46 | } |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 47 | |
| 48 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 49 | bool runOnSCC(CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 50 | |
| 51 | // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 52 | bool AddReadAttrs(const CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 53 | |
| 54 | // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 55 | bool AddNoCaptureAttrs(const CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 56 | |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 57 | // IsFunctionMallocLike - Does this function allocate new memory? |
| 58 | bool IsFunctionMallocLike(Function *F, |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 59 | SmallPtrSet<Function*, 8> &) const; |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 60 | |
| 61 | // AddNoAliasAttrs - Deduce noalias attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 62 | bool AddNoAliasAttrs(const CallGraphSCC &SCC); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 63 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 64 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 65 | AU.setPreservesCFG(); |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 66 | AU.addRequired<AliasAnalysis>(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 67 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 68 | } |
| 69 | |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 70 | private: |
| 71 | AliasAnalysis *AA; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 72 | }; |
| 73 | } |
| 74 | |
| 75 | char FunctionAttrs::ID = 0; |
Owen Anderson | ae0a7bc | 2010-10-13 22:00:45 +0000 | [diff] [blame] | 76 | INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", |
| 77 | "Deduce function attributes", false, false) |
| 78 | INITIALIZE_AG_DEPENDENCY(CallGraph) |
| 79 | INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 80 | "Deduce function attributes", false, false) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 81 | |
| 82 | Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } |
| 83 | |
| 84 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 85 | /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 86 | bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 87 | SmallPtrSet<Function*, 8> SCCNodes; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 88 | |
| 89 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 90 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 91 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 92 | SCCNodes.insert((*I)->getFunction()); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 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; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 97 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 98 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 99 | |
| 100 | if (F == 0) |
| 101 | // External node - may write memory. Just give up. |
| 102 | return false; |
| 103 | |
Dan Gohman | 6d44d64 | 2010-11-09 20:13:27 +0000 | [diff] [blame] | 104 | AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F); |
| 105 | if (MRB == AliasAnalysis::DoesNotAccessMemory) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 106 | // Already perfect! |
| 107 | continue; |
| 108 | |
| 109 | // Definitions with weak linkage may be overridden at linktime with |
| 110 | // something that writes memory, so treat them like declarations. |
| 111 | if (F->isDeclaration() || F->mayBeOverridden()) { |
Dan Gohman | 6d44d64 | 2010-11-09 20:13:27 +0000 | [diff] [blame] | 112 | if (!AliasAnalysis::onlyReadsMemory(MRB)) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 113 | // May write memory. Just give up. |
| 114 | return false; |
| 115 | |
| 116 | ReadsMemory = true; |
| 117 | continue; |
| 118 | } |
| 119 | |
| 120 | // Scan the function body for instructions that may read or write memory. |
| 121 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 122 | Instruction *I = &*II; |
| 123 | |
| 124 | // Some instructions can be ignored even if they read or write memory. |
| 125 | // Detect these now, skipping to the next instruction if one is found. |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 126 | CallSite CS(cast<Value>(I)); |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 127 | if (CS) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 128 | // Ignore calls to functions in the same SCC. |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 129 | if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 130 | continue; |
Dan Gohman | 42c31a7 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 131 | AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS); |
| 132 | // If the call doesn't access arbitrary memory, we may be able to |
| 133 | // figure out something. |
Dan Gohman | 432d08c | 2010-11-10 17:34:04 +0000 | [diff] [blame] | 134 | if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { |
| 135 | // If the call does access argument pointees, check each argument. |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 136 | if (AliasAnalysis::doesAccessArgPointees(MRB)) |
Dan Gohman | 42c31a7 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 137 | // Check whether all pointer arguments point to local memory, and |
| 138 | // ignore calls that only access local memory. |
| 139 | for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 140 | CI != CE; ++CI) { |
| 141 | Value *Arg = *CI; |
| 142 | if (Arg->getType()->isPointerTy()) { |
| 143 | AliasAnalysis::Location Loc(Arg, |
| 144 | AliasAnalysis::UnknownSize, |
| 145 | I->getMetadata(LLVMContext::MD_tbaa)); |
| 146 | if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) { |
| 147 | if (MRB & AliasAnalysis::Mod) |
| 148 | // Writes non-local memory. Give up. |
| 149 | return false; |
| 150 | if (MRB & AliasAnalysis::Ref) |
| 151 | // Ok, it reads non-local memory. |
| 152 | ReadsMemory = true; |
| 153 | } |
Dan Gohman | 40b6a19 | 2010-11-09 19:56:27 +0000 | [diff] [blame] | 154 | } |
| 155 | } |
Dan Gohman | 40b6a19 | 2010-11-09 19:56:27 +0000 | [diff] [blame] | 156 | continue; |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 157 | } |
Dan Gohman | 42c31a7 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 158 | // The call could access any memory. If that includes writes, give up. |
| 159 | if (MRB & AliasAnalysis::Mod) |
| 160 | return false; |
| 161 | // If it reads, note it. |
| 162 | if (MRB & AliasAnalysis::Ref) |
| 163 | ReadsMemory = true; |
| 164 | continue; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 165 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Eli Friedman | 2199dfb | 2011-08-16 01:28:22 +0000 | [diff] [blame^] | 166 | // Ignore non-volatile loads from local memory. (Atomic is okay here.) |
| 167 | if (!LI->isVolatile()) { |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 168 | AliasAnalysis::Location Loc = AA->getLocation(LI); |
Dan Gohman | ea8900f | 2010-11-08 17:12:04 +0000 | [diff] [blame] | 169 | if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 170 | continue; |
| 171 | } |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 172 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Eli Friedman | 2199dfb | 2011-08-16 01:28:22 +0000 | [diff] [blame^] | 173 | // Ignore non-volatile stores to local memory. (Atomic is okay here.) |
| 174 | if (!SI->isVolatile()) { |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 175 | AliasAnalysis::Location Loc = AA->getLocation(SI); |
Dan Gohman | ea8900f | 2010-11-08 17:12:04 +0000 | [diff] [blame] | 176 | if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 177 | continue; |
| 178 | } |
Dan Gohman | 4cf0dcf | 2010-11-09 20:17:38 +0000 | [diff] [blame] | 179 | } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { |
| 180 | // Ignore vaargs on local memory. |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 181 | AliasAnalysis::Location Loc = AA->getLocation(VI); |
Dan Gohman | 4cf0dcf | 2010-11-09 20:17:38 +0000 | [diff] [blame] | 182 | if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 183 | continue; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 184 | } |
| 185 | |
| 186 | // Any remaining instructions need to be taken seriously! Check if they |
| 187 | // read or write memory. |
| 188 | if (I->mayWriteToMemory()) |
| 189 | // Writes memory. Just give up. |
| 190 | return false; |
Duncan Sands | cfd0ebe | 2009-05-06 08:42:00 +0000 | [diff] [blame] | 191 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 192 | // If this instruction may read memory, remember that. |
| 193 | ReadsMemory |= I->mayReadFromMemory(); |
| 194 | } |
| 195 | } |
| 196 | |
| 197 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 198 | // Give them the appropriate attribute. |
| 199 | bool MadeChange = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 200 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 201 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 202 | |
| 203 | if (F->doesNotAccessMemory()) |
| 204 | // Already perfect! |
| 205 | continue; |
| 206 | |
| 207 | if (F->onlyReadsMemory() && ReadsMemory) |
| 208 | // No change. |
| 209 | continue; |
| 210 | |
| 211 | MadeChange = true; |
| 212 | |
| 213 | // Clear out any existing attributes. |
| 214 | F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone); |
| 215 | |
| 216 | // Add in the new attribute. |
| 217 | F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone); |
| 218 | |
| 219 | if (ReadsMemory) |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 220 | ++NumReadOnly; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 221 | else |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 222 | ++NumReadNone; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 223 | } |
| 224 | |
| 225 | return MadeChange; |
| 226 | } |
| 227 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 228 | /// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 229 | bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 230 | bool Changed = false; |
| 231 | |
| 232 | // Check each function in turn, determining which pointer arguments are not |
| 233 | // captured. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 234 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 235 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 236 | |
| 237 | if (F == 0) |
| 238 | // External node - skip it; |
| 239 | continue; |
| 240 | |
| 241 | // Definitions with weak linkage may be overridden at linktime with |
| 242 | // something that writes memory, so treat them like declarations. |
| 243 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 244 | continue; |
| 245 | |
| 246 | 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] | 247 | if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr() && |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 248 | !PointerMayBeCaptured(A, true, /*StoreCaptures=*/false)) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 249 | A->addAttr(Attribute::NoCapture); |
Nick Lewycky | 6b05686 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 250 | ++NumNoCapture; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 251 | Changed = true; |
| 252 | } |
| 253 | } |
| 254 | |
| 255 | return Changed; |
| 256 | } |
| 257 | |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 258 | /// IsFunctionMallocLike - A function is malloc-like if it returns either null |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 259 | /// 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] | 260 | bool FunctionAttrs::IsFunctionMallocLike(Function *F, |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 261 | SmallPtrSet<Function*, 8> &SCCNodes) const { |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 262 | UniqueVector<Value *> FlowsToReturn; |
| 263 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 264 | if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) |
| 265 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 266 | |
| 267 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
| 268 | Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved. |
| 269 | |
| 270 | if (Constant *C = dyn_cast<Constant>(RetVal)) { |
| 271 | if (!C->isNullValue() && !isa<UndefValue>(C)) |
| 272 | return false; |
| 273 | |
| 274 | continue; |
| 275 | } |
| 276 | |
| 277 | if (isa<Argument>(RetVal)) |
| 278 | return false; |
| 279 | |
| 280 | if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
| 281 | switch (RVI->getOpcode()) { |
| 282 | // Extend the analysis by looking upwards. |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 283 | case Instruction::BitCast: |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 284 | case Instruction::GetElementPtr: |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 285 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 286 | continue; |
| 287 | case Instruction::Select: { |
| 288 | SelectInst *SI = cast<SelectInst>(RVI); |
| 289 | FlowsToReturn.insert(SI->getTrueValue()); |
| 290 | FlowsToReturn.insert(SI->getFalseValue()); |
Chris Lattner | 439044f | 2009-09-27 21:29:28 +0000 | [diff] [blame] | 291 | continue; |
| 292 | } |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 293 | case Instruction::PHI: { |
| 294 | PHINode *PN = cast<PHINode>(RVI); |
| 295 | for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 296 | FlowsToReturn.insert(PN->getIncomingValue(i)); |
Chris Lattner | 439044f | 2009-09-27 21:29:28 +0000 | [diff] [blame] | 297 | continue; |
| 298 | } |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 299 | |
| 300 | // Check whether the pointer came from an allocation. |
| 301 | case Instruction::Alloca: |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 302 | break; |
| 303 | case Instruction::Call: |
| 304 | case Instruction::Invoke: { |
| 305 | CallSite CS(RVI); |
| 306 | if (CS.paramHasAttr(0, Attribute::NoAlias)) |
| 307 | break; |
| 308 | if (CS.getCalledFunction() && |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 309 | SCCNodes.count(CS.getCalledFunction())) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 310 | break; |
| 311 | } // fall-through |
| 312 | default: |
| 313 | return false; // Did not come from an allocation. |
| 314 | } |
| 315 | |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 316 | if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 317 | return false; |
| 318 | } |
| 319 | |
| 320 | return true; |
| 321 | } |
| 322 | |
| 323 | /// AddNoAliasAttrs - Deduce noalias attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 324 | bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 325 | SmallPtrSet<Function*, 8> SCCNodes; |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 326 | |
| 327 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 328 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 329 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 330 | SCCNodes.insert((*I)->getFunction()); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 331 | |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 332 | // Check each function in turn, determining which functions return noalias |
| 333 | // pointers. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 334 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 335 | Function *F = (*I)->getFunction(); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 336 | |
| 337 | if (F == 0) |
| 338 | // External node - skip it; |
| 339 | return false; |
| 340 | |
| 341 | // Already noalias. |
| 342 | if (F->doesNotAlias(0)) |
| 343 | continue; |
| 344 | |
| 345 | // Definitions with weak linkage may be overridden at linktime, so |
| 346 | // treat them like declarations. |
| 347 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 348 | return false; |
| 349 | |
| 350 | // We annotate noalias return values, which are only applicable to |
| 351 | // pointer types. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 352 | if (!F->getReturnType()->isPointerTy()) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 353 | continue; |
| 354 | |
| 355 | if (!IsFunctionMallocLike(F, SCCNodes)) |
| 356 | return false; |
| 357 | } |
| 358 | |
| 359 | bool MadeChange = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 360 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 361 | Function *F = (*I)->getFunction(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 362 | if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 363 | continue; |
| 364 | |
| 365 | F->setDoesNotAlias(0); |
| 366 | ++NumNoAlias; |
| 367 | MadeChange = true; |
| 368 | } |
| 369 | |
| 370 | return MadeChange; |
| 371 | } |
| 372 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 373 | bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 374 | AA = &getAnalysis<AliasAnalysis>(); |
| 375 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 376 | bool Changed = AddReadAttrs(SCC); |
| 377 | Changed |= AddNoCaptureAttrs(SCC); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 378 | Changed |= AddNoAliasAttrs(SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 379 | return Changed; |
| 380 | } |