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" |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 30 | #include "llvm/ADT/SCCIterator.h" |
Duncan Sands | 338cd6b | 2009-01-02 11:54:37 +0000 | [diff] [blame] | 31 | #include "llvm/ADT/SmallSet.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 32 | #include "llvm/ADT/Statistic.h" |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 33 | #include "llvm/ADT/UniqueVector.h" |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 34 | #include "llvm/Support/InstIterator.h" |
| 35 | using namespace llvm; |
| 36 | |
| 37 | STATISTIC(NumReadNone, "Number of functions marked readnone"); |
| 38 | STATISTIC(NumReadOnly, "Number of functions marked readonly"); |
| 39 | STATISTIC(NumNoCapture, "Number of arguments marked nocapture"); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 40 | STATISTIC(NumNoAlias, "Number of function returns marked noalias"); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 41 | |
| 42 | namespace { |
Nick Lewycky | 6726b6d | 2009-10-25 06:33:48 +0000 | [diff] [blame] | 43 | struct FunctionAttrs : public CallGraphSCCPass { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 44 | static char ID; // Pass identification, replacement for typeid |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 45 | FunctionAttrs() : CallGraphSCCPass(ID), AA(0) { |
Owen Anderson | 081c34b | 2010-10-19 17:21:58 +0000 | [diff] [blame] | 46 | initializeFunctionAttrsPass(*PassRegistry::getPassRegistry()); |
| 47 | } |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 48 | |
| 49 | // runOnSCC - Analyze the SCC, performing the transformation if possible. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 50 | bool runOnSCC(CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 51 | |
| 52 | // AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 53 | bool AddReadAttrs(const CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 54 | |
| 55 | // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 56 | bool AddNoCaptureAttrs(const CallGraphSCC &SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 57 | |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 58 | // IsFunctionMallocLike - Does this function allocate new memory? |
| 59 | bool IsFunctionMallocLike(Function *F, |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 60 | SmallPtrSet<Function*, 8> &) const; |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 61 | |
| 62 | // AddNoAliasAttrs - Deduce noalias attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 63 | bool AddNoAliasAttrs(const CallGraphSCC &SCC); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 64 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 65 | virtual void getAnalysisUsage(AnalysisUsage &AU) const { |
| 66 | AU.setPreservesCFG(); |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 67 | AU.addRequired<AliasAnalysis>(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 68 | CallGraphSCCPass::getAnalysisUsage(AU); |
| 69 | } |
| 70 | |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 71 | private: |
| 72 | AliasAnalysis *AA; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 73 | }; |
| 74 | } |
| 75 | |
| 76 | char FunctionAttrs::ID = 0; |
Owen Anderson | ae0a7bc | 2010-10-13 22:00:45 +0000 | [diff] [blame] | 77 | INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs", |
| 78 | "Deduce function attributes", false, false) |
| 79 | INITIALIZE_AG_DEPENDENCY(CallGraph) |
| 80 | INITIALIZE_PASS_END(FunctionAttrs, "functionattrs", |
Owen Anderson | ce665bd | 2010-10-07 22:25:06 +0000 | [diff] [blame] | 81 | "Deduce function attributes", false, false) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 82 | |
| 83 | Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); } |
| 84 | |
| 85 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 86 | /// AddReadAttrs - Deduce readonly/readnone attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 87 | bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 88 | SmallPtrSet<Function*, 8> SCCNodes; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 89 | |
| 90 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 91 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 92 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 93 | SCCNodes.insert((*I)->getFunction()); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 94 | |
| 95 | // Check if any of the functions in the SCC read or write memory. If they |
| 96 | // write memory then they can't be marked readnone or readonly. |
| 97 | bool ReadsMemory = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 98 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 99 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 100 | |
| 101 | if (F == 0) |
| 102 | // External node - may write memory. Just give up. |
| 103 | return false; |
| 104 | |
Dan Gohman | 6d44d64 | 2010-11-09 20:13:27 +0000 | [diff] [blame] | 105 | AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F); |
| 106 | if (MRB == AliasAnalysis::DoesNotAccessMemory) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 107 | // Already perfect! |
| 108 | continue; |
| 109 | |
| 110 | // Definitions with weak linkage may be overridden at linktime with |
| 111 | // something that writes memory, so treat them like declarations. |
| 112 | if (F->isDeclaration() || F->mayBeOverridden()) { |
Dan Gohman | 6d44d64 | 2010-11-09 20:13:27 +0000 | [diff] [blame] | 113 | if (!AliasAnalysis::onlyReadsMemory(MRB)) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 114 | // May write memory. Just give up. |
| 115 | return false; |
| 116 | |
| 117 | ReadsMemory = true; |
| 118 | continue; |
| 119 | } |
| 120 | |
| 121 | // Scan the function body for instructions that may read or write memory. |
| 122 | for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) { |
| 123 | Instruction *I = &*II; |
| 124 | |
| 125 | // Some instructions can be ignored even if they read or write memory. |
| 126 | // Detect these now, skipping to the next instruction if one is found. |
Gabor Greif | 7d3056b | 2010-07-28 22:50:26 +0000 | [diff] [blame] | 127 | CallSite CS(cast<Value>(I)); |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 128 | if (CS) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 129 | // Ignore calls to functions in the same SCC. |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 130 | if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction())) |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 131 | continue; |
Dan Gohman | 42c31a7 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 132 | AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS); |
| 133 | // If the call doesn't access arbitrary memory, we may be able to |
| 134 | // figure out something. |
Dan Gohman | 432d08c | 2010-11-10 17:34:04 +0000 | [diff] [blame] | 135 | if (AliasAnalysis::onlyAccessesArgPointees(MRB)) { |
| 136 | // If the call does access argument pointees, check each argument. |
Dan Gohman | 68a6056 | 2010-11-10 18:17:28 +0000 | [diff] [blame] | 137 | if (AliasAnalysis::doesAccessArgPointees(MRB)) |
Dan Gohman | 42c31a7 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 138 | // Check whether all pointer arguments point to local memory, and |
| 139 | // ignore calls that only access local memory. |
| 140 | for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end(); |
| 141 | CI != CE; ++CI) { |
| 142 | Value *Arg = *CI; |
| 143 | if (Arg->getType()->isPointerTy()) { |
| 144 | AliasAnalysis::Location Loc(Arg, |
| 145 | AliasAnalysis::UnknownSize, |
| 146 | I->getMetadata(LLVMContext::MD_tbaa)); |
| 147 | if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) { |
| 148 | if (MRB & AliasAnalysis::Mod) |
| 149 | // Writes non-local memory. Give up. |
| 150 | return false; |
| 151 | if (MRB & AliasAnalysis::Ref) |
| 152 | // Ok, it reads non-local memory. |
| 153 | ReadsMemory = true; |
| 154 | } |
Dan Gohman | 40b6a19 | 2010-11-09 19:56:27 +0000 | [diff] [blame] | 155 | } |
| 156 | } |
Dan Gohman | 40b6a19 | 2010-11-09 19:56:27 +0000 | [diff] [blame] | 157 | continue; |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 158 | } |
Dan Gohman | 42c31a7 | 2010-11-10 01:02:18 +0000 | [diff] [blame] | 159 | // The call could access any memory. If that includes writes, give up. |
| 160 | if (MRB & AliasAnalysis::Mod) |
| 161 | return false; |
| 162 | // If it reads, note it. |
| 163 | if (MRB & AliasAnalysis::Ref) |
| 164 | ReadsMemory = true; |
| 165 | continue; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 166 | } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) { |
Eli Friedman | 2199dfb | 2011-08-16 01:28:22 +0000 | [diff] [blame] | 167 | // Ignore non-volatile loads from local memory. (Atomic is okay here.) |
| 168 | if (!LI->isVolatile()) { |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 169 | AliasAnalysis::Location Loc = AA->getLocation(LI); |
Dan Gohman | ea8900f | 2010-11-08 17:12:04 +0000 | [diff] [blame] | 170 | if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 171 | continue; |
| 172 | } |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 173 | } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) { |
Eli Friedman | 2199dfb | 2011-08-16 01:28:22 +0000 | [diff] [blame] | 174 | // Ignore non-volatile stores to local memory. (Atomic is okay here.) |
| 175 | if (!SI->isVolatile()) { |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 176 | AliasAnalysis::Location Loc = AA->getLocation(SI); |
Dan Gohman | ea8900f | 2010-11-08 17:12:04 +0000 | [diff] [blame] | 177 | if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 178 | continue; |
| 179 | } |
Dan Gohman | 4cf0dcf | 2010-11-09 20:17:38 +0000 | [diff] [blame] | 180 | } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) { |
| 181 | // Ignore vaargs on local memory. |
Dan Gohman | 6d8eb15 | 2010-11-11 21:50:19 +0000 | [diff] [blame] | 182 | AliasAnalysis::Location Loc = AA->getLocation(VI); |
Dan Gohman | 4cf0dcf | 2010-11-09 20:17:38 +0000 | [diff] [blame] | 183 | if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) |
| 184 | continue; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | // Any remaining instructions need to be taken seriously! Check if they |
| 188 | // read or write memory. |
| 189 | if (I->mayWriteToMemory()) |
| 190 | // Writes memory. Just give up. |
| 191 | return false; |
Duncan Sands | cfd0ebe | 2009-05-06 08:42:00 +0000 | [diff] [blame] | 192 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 193 | // If this instruction may read memory, remember that. |
| 194 | ReadsMemory |= I->mayReadFromMemory(); |
| 195 | } |
| 196 | } |
| 197 | |
| 198 | // Success! Functions in this SCC do not access memory, or only read memory. |
| 199 | // Give them the appropriate attribute. |
| 200 | bool MadeChange = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 201 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 202 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 203 | |
| 204 | if (F->doesNotAccessMemory()) |
| 205 | // Already perfect! |
| 206 | continue; |
| 207 | |
| 208 | if (F->onlyReadsMemory() && ReadsMemory) |
| 209 | // No change. |
| 210 | continue; |
| 211 | |
| 212 | MadeChange = true; |
| 213 | |
| 214 | // Clear out any existing attributes. |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 215 | Attributes::Builder B; |
| 216 | B.addAttribute(Attributes::ReadOnly) |
| 217 | .addAttribute(Attributes::ReadNone); |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame^] | 218 | F->removeAttribute(~0, Attributes::get(F->getContext(), B)); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 219 | |
| 220 | // Add in the new attribute. |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 221 | B.clear(); |
| 222 | B.addAttribute(ReadsMemory ? Attributes::ReadOnly : Attributes::ReadNone); |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame^] | 223 | F->addAttribute(~0, Attributes::get(F->getContext(), B)); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 224 | |
| 225 | if (ReadsMemory) |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 226 | ++NumReadOnly; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 227 | else |
Duncan Sands | b2f2279 | 2009-01-02 11:46:24 +0000 | [diff] [blame] | 228 | ++NumReadNone; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 229 | } |
| 230 | |
| 231 | return MadeChange; |
| 232 | } |
| 233 | |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 234 | namespace { |
| 235 | // For a given pointer Argument, this retains a list of Arguments of functions |
| 236 | // in the same SCC that the pointer data flows into. We use this to build an |
| 237 | // SCC of the arguments. |
| 238 | struct ArgumentGraphNode { |
| 239 | Argument *Definition; |
| 240 | SmallVector<ArgumentGraphNode*, 4> Uses; |
| 241 | }; |
| 242 | |
| 243 | class ArgumentGraph { |
| 244 | // We store pointers to ArgumentGraphNode objects, so it's important that |
| 245 | // that they not move around upon insert. |
| 246 | typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy; |
| 247 | |
| 248 | ArgumentMapTy ArgumentMap; |
| 249 | |
| 250 | // There is no root node for the argument graph, in fact: |
| 251 | // void f(int *x, int *y) { if (...) f(x, y); } |
| 252 | // is an example where the graph is disconnected. The SCCIterator requires a |
| 253 | // single entry point, so we maintain a fake ("synthetic") root node that |
| 254 | // uses every node. Because the graph is directed and nothing points into |
| 255 | // the root, it will not participate in any SCCs (except for its own). |
| 256 | ArgumentGraphNode SyntheticRoot; |
| 257 | |
| 258 | public: |
| 259 | ArgumentGraph() { SyntheticRoot.Definition = 0; } |
| 260 | |
| 261 | typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator; |
| 262 | |
| 263 | iterator begin() { return SyntheticRoot.Uses.begin(); } |
| 264 | iterator end() { return SyntheticRoot.Uses.end(); } |
| 265 | ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; } |
| 266 | |
| 267 | ArgumentGraphNode *operator[](Argument *A) { |
| 268 | ArgumentGraphNode &Node = ArgumentMap[A]; |
| 269 | Node.Definition = A; |
| 270 | SyntheticRoot.Uses.push_back(&Node); |
| 271 | return &Node; |
| 272 | } |
| 273 | }; |
| 274 | |
| 275 | // This tracker checks whether callees are in the SCC, and if so it does not |
| 276 | // consider that a capture, instead adding it to the "Uses" list and |
| 277 | // continuing with the analysis. |
| 278 | struct ArgumentUsesTracker : public CaptureTracker { |
| 279 | ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes) |
| 280 | : Captured(false), SCCNodes(SCCNodes) {} |
| 281 | |
| 282 | void tooManyUses() { Captured = true; } |
| 283 | |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 284 | bool captured(Use *U) { |
| 285 | CallSite CS(U->getUser()); |
| 286 | if (!CS.getInstruction()) { Captured = true; return true; } |
| 287 | |
| 288 | Function *F = CS.getCalledFunction(); |
| 289 | if (!F || !SCCNodes.count(F)) { Captured = true; return true; } |
| 290 | |
| 291 | Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end(); |
| 292 | for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end(); |
| 293 | PI != PE; ++PI, ++AI) { |
| 294 | if (AI == AE) { |
| 295 | assert(F->isVarArg() && "More params than args in non-varargs call"); |
| 296 | Captured = true; |
| 297 | return true; |
| 298 | } |
| 299 | if (PI == U) { |
| 300 | Uses.push_back(AI); |
| 301 | break; |
| 302 | } |
| 303 | } |
| 304 | assert(!Uses.empty() && "Capturing call-site captured nothing?"); |
| 305 | return false; |
| 306 | } |
| 307 | |
| 308 | bool Captured; // True only if certainly captured (used outside our SCC). |
| 309 | SmallVector<Argument*, 4> Uses; // Uses within our SCC. |
| 310 | |
| 311 | const SmallPtrSet<Function*, 8> &SCCNodes; |
| 312 | }; |
| 313 | } |
| 314 | |
| 315 | namespace llvm { |
| 316 | template<> struct GraphTraits<ArgumentGraphNode*> { |
| 317 | typedef ArgumentGraphNode NodeType; |
| 318 | typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType; |
| 319 | |
| 320 | static inline NodeType *getEntryNode(NodeType *A) { return A; } |
| 321 | static inline ChildIteratorType child_begin(NodeType *N) { |
| 322 | return N->Uses.begin(); |
| 323 | } |
| 324 | static inline ChildIteratorType child_end(NodeType *N) { |
| 325 | return N->Uses.end(); |
| 326 | } |
| 327 | }; |
| 328 | template<> struct GraphTraits<ArgumentGraph*> |
| 329 | : public GraphTraits<ArgumentGraphNode*> { |
| 330 | static NodeType *getEntryNode(ArgumentGraph *AG) { |
| 331 | return AG->getEntryNode(); |
| 332 | } |
| 333 | static ChildIteratorType nodes_begin(ArgumentGraph *AG) { |
| 334 | return AG->begin(); |
| 335 | } |
| 336 | static ChildIteratorType nodes_end(ArgumentGraph *AG) { |
| 337 | return AG->end(); |
| 338 | } |
| 339 | }; |
| 340 | } |
| 341 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 342 | /// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 343 | bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) { |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 344 | bool Changed = false; |
| 345 | |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 346 | SmallPtrSet<Function*, 8> SCCNodes; |
| 347 | |
| 348 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 349 | // looking up whether a given CallGraphNode is in this SCC. |
| 350 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 351 | Function *F = (*I)->getFunction(); |
| 352 | if (F && !F->isDeclaration() && !F->mayBeOverridden()) |
| 353 | SCCNodes.insert(F); |
| 354 | } |
| 355 | |
| 356 | ArgumentGraph AG; |
| 357 | |
Bill Wendling | 7d2f249 | 2012-10-10 07:36:45 +0000 | [diff] [blame] | 358 | Attributes::Builder B; |
| 359 | B.addAttribute(Attributes::NoCapture); |
| 360 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 361 | // Check each function in turn, determining which pointer arguments are not |
| 362 | // captured. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 363 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 364 | Function *F = (*I)->getFunction(); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 365 | |
| 366 | if (F == 0) |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 367 | // External node - only a problem for arguments that we pass to it. |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 368 | continue; |
| 369 | |
| 370 | // Definitions with weak linkage may be overridden at linktime with |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 371 | // something that captures pointers, so treat them like declarations. |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 372 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 373 | continue; |
| 374 | |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 375 | // Functions that are readonly (or readnone) and nounwind and don't return |
| 376 | // a value can't capture arguments. Don't analyze them. |
| 377 | if (F->onlyReadsMemory() && F->doesNotThrow() && |
| 378 | F->getReturnType()->isVoidTy()) { |
| 379 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); |
| 380 | A != E; ++A) { |
| 381 | if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame^] | 382 | A->addAttr(Attributes::get(F->getContext(), B)); |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 383 | ++NumNoCapture; |
| 384 | Changed = true; |
| 385 | } |
| 386 | } |
| 387 | continue; |
| 388 | } |
| 389 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 390 | for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A) |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 391 | if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) { |
| 392 | ArgumentUsesTracker Tracker(SCCNodes); |
| 393 | PointerMayBeCaptured(A, &Tracker); |
| 394 | if (!Tracker.Captured) { |
| 395 | if (Tracker.Uses.empty()) { |
| 396 | // If it's trivially not captured, mark it nocapture now. |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame^] | 397 | A->addAttr(Attributes::get(F->getContext(), B)); |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 398 | ++NumNoCapture; |
| 399 | Changed = true; |
| 400 | } else { |
| 401 | // If it's not trivially captured and not trivially not captured, |
| 402 | // then it must be calling into another function in our SCC. Save |
| 403 | // its particulars for Argument-SCC analysis later. |
| 404 | ArgumentGraphNode *Node = AG[A]; |
| 405 | for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(), |
| 406 | UE = Tracker.Uses.end(); UI != UE; ++UI) |
| 407 | Node->Uses.push_back(AG[*UI]); |
| 408 | } |
| 409 | } |
| 410 | // Otherwise, it's captured. Don't bother doing SCC analysis on it. |
| 411 | } |
| 412 | } |
| 413 | |
| 414 | // The graph we've collected is partial because we stopped scanning for |
| 415 | // argument uses once we solved the argument trivially. These partial nodes |
| 416 | // show up as ArgumentGraphNode objects with an empty Uses list, and for |
| 417 | // these nodes the final decision about whether they capture has already been |
| 418 | // made. If the definition doesn't have a 'nocapture' attribute by now, it |
| 419 | // captures. |
| 420 | |
| 421 | for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG), E = scc_end(&AG); |
| 422 | I != E; ++I) { |
| 423 | std::vector<ArgumentGraphNode*> &ArgumentSCC = *I; |
| 424 | if (ArgumentSCC.size() == 1) { |
| 425 | if (!ArgumentSCC[0]->Definition) continue; // synthetic root node |
| 426 | |
| 427 | // eg. "void f(int* x) { if (...) f(x); }" |
| 428 | if (ArgumentSCC[0]->Uses.size() == 1 && |
| 429 | ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) { |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame^] | 430 | ArgumentSCC[0]-> |
| 431 | Definition-> |
| 432 | addAttr(Attributes::get(ArgumentSCC[0]->Definition->getContext(), B)); |
Nick Lewycky | 6b05686 | 2009-01-02 03:46:56 +0000 | [diff] [blame] | 433 | ++NumNoCapture; |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 434 | Changed = true; |
| 435 | } |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 436 | continue; |
| 437 | } |
| 438 | |
| 439 | bool SCCCaptured = false; |
| 440 | for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(), |
| 441 | E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) { |
| 442 | ArgumentGraphNode *Node = *I; |
| 443 | if (Node->Uses.empty()) { |
| 444 | if (!Node->Definition->hasNoCaptureAttr()) |
| 445 | SCCCaptured = true; |
| 446 | } |
| 447 | } |
| 448 | if (SCCCaptured) continue; |
| 449 | |
| 450 | SmallPtrSet<Argument*, 8> ArgumentSCCNodes; |
| 451 | // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for |
| 452 | // quickly looking up whether a given Argument is in this ArgumentSCC. |
| 453 | for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(), |
| 454 | E = ArgumentSCC.end(); I != E; ++I) { |
| 455 | ArgumentSCCNodes.insert((*I)->Definition); |
| 456 | } |
| 457 | |
| 458 | for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(), |
| 459 | E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) { |
| 460 | ArgumentGraphNode *N = *I; |
| 461 | for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(), |
| 462 | UE = N->Uses.end(); UI != UE; ++UI) { |
| 463 | Argument *A = (*UI)->Definition; |
| 464 | if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A)) |
| 465 | continue; |
| 466 | SCCCaptured = true; |
| 467 | break; |
| 468 | } |
| 469 | } |
| 470 | if (SCCCaptured) continue; |
| 471 | |
Nick Lewycky | 720ac91 | 2012-01-05 22:21:45 +0000 | [diff] [blame] | 472 | for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) { |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 473 | Argument *A = ArgumentSCC[i]->Definition; |
Bill Wendling | cb3de0b | 2012-10-15 04:46:55 +0000 | [diff] [blame^] | 474 | A->addAttr(Attributes::get(A->getContext(), B)); |
Nick Lewycky | b48a189 | 2011-12-28 23:24:21 +0000 | [diff] [blame] | 475 | ++NumNoCapture; |
| 476 | Changed = true; |
| 477 | } |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 478 | } |
| 479 | |
| 480 | return Changed; |
| 481 | } |
| 482 | |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 483 | /// IsFunctionMallocLike - A function is malloc-like if it returns either null |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 484 | /// 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] | 485 | bool FunctionAttrs::IsFunctionMallocLike(Function *F, |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 486 | SmallPtrSet<Function*, 8> &SCCNodes) const { |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 487 | UniqueVector<Value *> FlowsToReturn; |
| 488 | for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I) |
| 489 | if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator())) |
| 490 | FlowsToReturn.insert(Ret->getReturnValue()); |
| 491 | |
| 492 | for (unsigned i = 0; i != FlowsToReturn.size(); ++i) { |
| 493 | Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved. |
| 494 | |
| 495 | if (Constant *C = dyn_cast<Constant>(RetVal)) { |
| 496 | if (!C->isNullValue() && !isa<UndefValue>(C)) |
| 497 | return false; |
| 498 | |
| 499 | continue; |
| 500 | } |
| 501 | |
| 502 | if (isa<Argument>(RetVal)) |
| 503 | return false; |
| 504 | |
| 505 | if (Instruction *RVI = dyn_cast<Instruction>(RetVal)) |
| 506 | switch (RVI->getOpcode()) { |
| 507 | // Extend the analysis by looking upwards. |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 508 | case Instruction::BitCast: |
Victor Hernandez | 83d6391 | 2009-09-18 22:35:49 +0000 | [diff] [blame] | 509 | case Instruction::GetElementPtr: |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 510 | FlowsToReturn.insert(RVI->getOperand(0)); |
| 511 | continue; |
| 512 | case Instruction::Select: { |
| 513 | SelectInst *SI = cast<SelectInst>(RVI); |
| 514 | FlowsToReturn.insert(SI->getTrueValue()); |
| 515 | FlowsToReturn.insert(SI->getFalseValue()); |
Chris Lattner | 439044f | 2009-09-27 21:29:28 +0000 | [diff] [blame] | 516 | continue; |
| 517 | } |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 518 | case Instruction::PHI: { |
| 519 | PHINode *PN = cast<PHINode>(RVI); |
| 520 | for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i) |
| 521 | FlowsToReturn.insert(PN->getIncomingValue(i)); |
Chris Lattner | 439044f | 2009-09-27 21:29:28 +0000 | [diff] [blame] | 522 | continue; |
| 523 | } |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 524 | |
| 525 | // Check whether the pointer came from an allocation. |
| 526 | case Instruction::Alloca: |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 527 | break; |
| 528 | case Instruction::Call: |
| 529 | case Instruction::Invoke: { |
| 530 | CallSite CS(RVI); |
Bill Wendling | 3e2d76c | 2012-10-09 21:38:14 +0000 | [diff] [blame] | 531 | if (CS.paramHasAttr(0, Attributes::NoAlias)) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 532 | break; |
| 533 | if (CS.getCalledFunction() && |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 534 | SCCNodes.count(CS.getCalledFunction())) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 535 | break; |
| 536 | } // fall-through |
| 537 | default: |
| 538 | return false; // Did not come from an allocation. |
| 539 | } |
| 540 | |
Dan Gohman | f94b5ed | 2009-11-19 21:57:48 +0000 | [diff] [blame] | 541 | if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false)) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 542 | return false; |
| 543 | } |
| 544 | |
| 545 | return true; |
| 546 | } |
| 547 | |
| 548 | /// AddNoAliasAttrs - Deduce noalias attributes for the SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 549 | bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) { |
Chris Lattner | 98a27ce | 2009-08-31 04:09:04 +0000 | [diff] [blame] | 550 | SmallPtrSet<Function*, 8> SCCNodes; |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 551 | |
| 552 | // Fill SCCNodes with the elements of the SCC. Used for quickly |
| 553 | // looking up whether a given CallGraphNode is in this SCC. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 554 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) |
| 555 | SCCNodes.insert((*I)->getFunction()); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 556 | |
Nick Lewycky | 4bfba9d | 2009-03-08 17:08:09 +0000 | [diff] [blame] | 557 | // Check each function in turn, determining which functions return noalias |
| 558 | // pointers. |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 559 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 560 | Function *F = (*I)->getFunction(); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 561 | |
| 562 | if (F == 0) |
| 563 | // External node - skip it; |
| 564 | return false; |
| 565 | |
| 566 | // Already noalias. |
| 567 | if (F->doesNotAlias(0)) |
| 568 | continue; |
| 569 | |
| 570 | // Definitions with weak linkage may be overridden at linktime, so |
| 571 | // treat them like declarations. |
| 572 | if (F->isDeclaration() || F->mayBeOverridden()) |
| 573 | return false; |
| 574 | |
| 575 | // We annotate noalias return values, which are only applicable to |
| 576 | // pointer types. |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 577 | if (!F->getReturnType()->isPointerTy()) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 578 | continue; |
| 579 | |
| 580 | if (!IsFunctionMallocLike(F, SCCNodes)) |
| 581 | return false; |
| 582 | } |
| 583 | |
| 584 | bool MadeChange = false; |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 585 | for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) { |
| 586 | Function *F = (*I)->getFunction(); |
Duncan Sands | 1df9859 | 2010-02-16 11:11:14 +0000 | [diff] [blame] | 587 | if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy()) |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 588 | continue; |
| 589 | |
| 590 | F->setDoesNotAlias(0); |
| 591 | ++NumNoAlias; |
| 592 | MadeChange = true; |
| 593 | } |
| 594 | |
| 595 | return MadeChange; |
| 596 | } |
| 597 | |
Chris Lattner | 2decb22 | 2010-04-16 22:42:17 +0000 | [diff] [blame] | 598 | bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) { |
Dan Gohman | 3c97f7a | 2010-11-08 16:10:15 +0000 | [diff] [blame] | 599 | AA = &getAnalysis<AliasAnalysis>(); |
| 600 | |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 601 | bool Changed = AddReadAttrs(SCC); |
| 602 | Changed |= AddNoCaptureAttrs(SCC); |
Nick Lewycky | 199aa3c | 2009-03-08 06:20:47 +0000 | [diff] [blame] | 603 | Changed |= AddNoAliasAttrs(SCC); |
Duncan Sands | 9e89ba3 | 2008-12-31 16:14:43 +0000 | [diff] [blame] | 604 | return Changed; |
| 605 | } |