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