blob: 8bdf5d75dadf2b07de0f8bfb66dac949bf130908 [file] [log] [blame]
Duncan Sands9e89ba32008-12-31 16:14:43 +00001//===- 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 Sandsb2f22792009-01-02 11:46:24 +000012// 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 Sands9e89ba32008-12-31 16:14:43 +000018//
19//===----------------------------------------------------------------------===//
20
21#define DEBUG_TYPE "functionattrs"
22#include "llvm/Transforms/IPO.h"
23#include "llvm/CallGraphSCCPass.h"
24#include "llvm/GlobalVariable.h"
Devang Patelcd119912009-03-03 00:28:44 +000025#include "llvm/IntrinsicInst.h"
Dan Gohmanea8900f2010-11-08 17:12:04 +000026#include "llvm/LLVMContext.h"
Nick Lewycky199aa3c2009-03-08 06:20:47 +000027#include "llvm/Analysis/AliasAnalysis.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000028#include "llvm/Analysis/CallGraph.h"
Duncan Sands8556d2a2009-01-18 12:19:30 +000029#include "llvm/Analysis/CaptureTracking.h"
Duncan Sands338cd6b2009-01-02 11:54:37 +000030#include "llvm/ADT/SmallSet.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000031#include "llvm/ADT/Statistic.h"
Nick Lewycky199aa3c2009-03-08 06:20:47 +000032#include "llvm/ADT/UniqueVector.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000033#include "llvm/Support/InstIterator.h"
34using namespace llvm;
35
36STATISTIC(NumReadNone, "Number of functions marked readnone");
37STATISTIC(NumReadOnly, "Number of functions marked readonly");
38STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewycky199aa3c2009-03-08 06:20:47 +000039STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Duncan Sands9e89ba32008-12-31 16:14:43 +000040
41namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000042 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands9e89ba32008-12-31 16:14:43 +000043 static char ID; // Pass identification, replacement for typeid
Dan Gohman3c97f7a2010-11-08 16:10:15 +000044 FunctionAttrs() : CallGraphSCCPass(ID), AA(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000045 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
46 }
Duncan Sands9e89ba32008-12-31 16:14:43 +000047
48 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Chris Lattner2decb222010-04-16 22:42:17 +000049 bool runOnSCC(CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000050
51 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000052 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000053
54 // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000055 bool AddNoCaptureAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000056
Nick Lewycky199aa3c2009-03-08 06:20:47 +000057 // IsFunctionMallocLike - Does this function allocate new memory?
58 bool IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +000059 SmallPtrSet<Function*, 8> &) const;
Nick Lewycky199aa3c2009-03-08 06:20:47 +000060
61 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000062 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +000063
Duncan Sands9e89ba32008-12-31 16:14:43 +000064 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
65 AU.setPreservesCFG();
Dan Gohman3c97f7a2010-11-08 16:10:15 +000066 AU.addRequired<AliasAnalysis>();
Duncan Sands9e89ba32008-12-31 16:14:43 +000067 CallGraphSCCPass::getAnalysisUsage(AU);
68 }
69
Dan Gohman3c97f7a2010-11-08 16:10:15 +000070 private:
71 AliasAnalysis *AA;
Duncan Sands9e89ba32008-12-31 16:14:43 +000072 };
73}
74
75char FunctionAttrs::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +000076INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
77 "Deduce function attributes", false, false)
78INITIALIZE_AG_DEPENDENCY(CallGraph)
79INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersonce665bd2010-10-07 22:25:06 +000080 "Deduce function attributes", false, false)
Duncan Sands9e89ba32008-12-31 16:14:43 +000081
82Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
83
84
Duncan Sands9e89ba32008-12-31 16:14:43 +000085/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000086bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +000087 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands9e89ba32008-12-31 16:14:43 +000088
89 // Fill SCCNodes with the elements of the SCC. Used for quickly
90 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000091 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
92 SCCNodes.insert((*I)->getFunction());
Duncan Sands9e89ba32008-12-31 16:14:43 +000093
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 Lattner2decb222010-04-16 22:42:17 +000097 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
98 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +000099
100 if (F == 0)
101 // External node - may write memory. Just give up.
102 return false;
103
Dan Gohman6d44d642010-11-09 20:13:27 +0000104 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
105 if (MRB == AliasAnalysis::DoesNotAccessMemory)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000106 // 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 Gohman6d44d642010-11-09 20:13:27 +0000112 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000113 // 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 Greif7d3056b2010-07-28 22:50:26 +0000126 CallSite CS(cast<Value>(I));
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000127 if (CS) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000128 // Ignore calls to functions in the same SCC.
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000129 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000130 continue;
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000131 switch (AA->getModRefBehavior(CS)) {
132 case AliasAnalysis::DoesNotAccessMemory:
133 // Ignore calls that don't access memory.
134 continue;
135 case AliasAnalysis::OnlyReadsMemory:
136 // Handle calls that only read from memory.
137 ReadsMemory = true;
138 continue;
139 case AliasAnalysis::AccessesArguments:
140 // Check whether all pointer arguments point to local memory, and
141 // ignore calls that only access local memory.
142 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
143 CI != CE; ++CI) {
144 Value *Arg = *CI;
Dan Gohmanea8900f2010-11-08 17:12:04 +0000145 if (Arg->getType()->isPointerTy()) {
146 AliasAnalysis::Location Loc(Arg,
147 AliasAnalysis::UnknownSize,
148 I->getMetadata(LLVMContext::MD_tbaa));
Dan Gohman554375b2010-11-08 19:24:47 +0000149 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
Dan Gohmanea8900f2010-11-08 17:12:04 +0000150 // Writes memory. Just give up.
151 return false;
152 }
Duncan Sands7c422ac2010-01-06 08:45:52 +0000153 }
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000154 // Only reads and writes local memory.
155 continue;
Dan Gohman40b6a192010-11-09 19:56:27 +0000156 case AliasAnalysis::AccessesArgumentsReadonly:
157 // Check whether all pointer arguments point to local memory, and
158 // ignore calls that only access local memory.
159 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
160 CI != CE; ++CI) {
161 Value *Arg = *CI;
162 if (Arg->getType()->isPointerTy()) {
163 AliasAnalysis::Location Loc(Arg,
164 AliasAnalysis::UnknownSize,
165 I->getMetadata(LLVMContext::MD_tbaa));
166 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
167 // Reads non-local memory.
168 ReadsMemory = true;
169 break;
170 }
171 }
172 }
173 // Only reads memory.
174 continue;
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000175 default:
176 // Otherwise, be conservative.
177 break;
178 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000179 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Duncan Sandsad6f5412010-10-30 12:59:44 +0000180 // Ignore non-volatile loads from local memory.
Dan Gohmanea8900f2010-11-08 17:12:04 +0000181 if (!LI->isVolatile()) {
182 AliasAnalysis::Location Loc(LI->getPointerOperand(),
183 AA->getTypeStoreSize(LI->getType()),
184 LI->getMetadata(LLVMContext::MD_tbaa));
185 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
186 continue;
187 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000188 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Duncan Sandsad6f5412010-10-30 12:59:44 +0000189 // Ignore non-volatile stores to local memory.
Dan Gohmanea8900f2010-11-08 17:12:04 +0000190 if (!SI->isVolatile()) {
191 const Type *StoredType = SI->getValueOperand()->getType();
192 AliasAnalysis::Location Loc(SI->getPointerOperand(),
193 AA->getTypeStoreSize(StoredType),
194 SI->getMetadata(LLVMContext::MD_tbaa));
195 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
196 continue;
197 }
Dan Gohman4cf0dcf2010-11-09 20:17:38 +0000198 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
199 // Ignore vaargs on local memory.
200 AliasAnalysis::Location Loc(VI->getPointerOperand(),
201 AliasAnalysis::UnknownSize,
202 VI->getMetadata(LLVMContext::MD_tbaa));
203 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
204 continue;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000205 }
206
207 // Any remaining instructions need to be taken seriously! Check if they
208 // read or write memory.
209 if (I->mayWriteToMemory())
210 // Writes memory. Just give up.
211 return false;
Duncan Sandscfd0ebe2009-05-06 08:42:00 +0000212
Duncan Sands9e89ba32008-12-31 16:14:43 +0000213 // If this instruction may read memory, remember that.
214 ReadsMemory |= I->mayReadFromMemory();
215 }
216 }
217
218 // Success! Functions in this SCC do not access memory, or only read memory.
219 // Give them the appropriate attribute.
220 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000221 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
222 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000223
224 if (F->doesNotAccessMemory())
225 // Already perfect!
226 continue;
227
228 if (F->onlyReadsMemory() && ReadsMemory)
229 // No change.
230 continue;
231
232 MadeChange = true;
233
234 // Clear out any existing attributes.
235 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
236
237 // Add in the new attribute.
238 F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone);
239
240 if (ReadsMemory)
Duncan Sandsb2f22792009-01-02 11:46:24 +0000241 ++NumReadOnly;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000242 else
Duncan Sandsb2f22792009-01-02 11:46:24 +0000243 ++NumReadNone;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000244 }
245
246 return MadeChange;
247}
248
Duncan Sands9e89ba32008-12-31 16:14:43 +0000249/// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000250bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000251 bool Changed = false;
252
253 // Check each function in turn, determining which pointer arguments are not
254 // captured.
Chris Lattner2decb222010-04-16 22:42:17 +0000255 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
256 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000257
258 if (F == 0)
259 // External node - skip it;
260 continue;
261
262 // Definitions with weak linkage may be overridden at linktime with
263 // something that writes memory, so treat them like declarations.
264 if (F->isDeclaration() || F->mayBeOverridden())
265 continue;
266
267 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A)
Duncan Sands1df98592010-02-16 11:11:14 +0000268 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr() &&
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000269 !PointerMayBeCaptured(A, true, /*StoreCaptures=*/false)) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000270 A->addAttr(Attribute::NoCapture);
Nick Lewycky6b056862009-01-02 03:46:56 +0000271 ++NumNoCapture;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000272 Changed = true;
273 }
274 }
275
276 return Changed;
277}
278
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000279/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000280/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000281bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +0000282 SmallPtrSet<Function*, 8> &SCCNodes) const {
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000283 UniqueVector<Value *> FlowsToReturn;
284 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
285 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
286 FlowsToReturn.insert(Ret->getReturnValue());
287
288 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
289 Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved.
290
291 if (Constant *C = dyn_cast<Constant>(RetVal)) {
292 if (!C->isNullValue() && !isa<UndefValue>(C))
293 return false;
294
295 continue;
296 }
297
298 if (isa<Argument>(RetVal))
299 return false;
300
301 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
302 switch (RVI->getOpcode()) {
303 // Extend the analysis by looking upwards.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000304 case Instruction::BitCast:
Victor Hernandez83d63912009-09-18 22:35:49 +0000305 case Instruction::GetElementPtr:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000306 FlowsToReturn.insert(RVI->getOperand(0));
307 continue;
308 case Instruction::Select: {
309 SelectInst *SI = cast<SelectInst>(RVI);
310 FlowsToReturn.insert(SI->getTrueValue());
311 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner439044f2009-09-27 21:29:28 +0000312 continue;
313 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000314 case Instruction::PHI: {
315 PHINode *PN = cast<PHINode>(RVI);
316 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
317 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner439044f2009-09-27 21:29:28 +0000318 continue;
319 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000320
321 // Check whether the pointer came from an allocation.
322 case Instruction::Alloca:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000323 break;
324 case Instruction::Call:
325 case Instruction::Invoke: {
326 CallSite CS(RVI);
327 if (CS.paramHasAttr(0, Attribute::NoAlias))
328 break;
329 if (CS.getCalledFunction() &&
Chris Lattner98a27ce2009-08-31 04:09:04 +0000330 SCCNodes.count(CS.getCalledFunction()))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000331 break;
332 } // fall-through
333 default:
334 return false; // Did not come from an allocation.
335 }
336
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000337 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000338 return false;
339 }
340
341 return true;
342}
343
344/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000345bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000346 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000347
348 // Fill SCCNodes with the elements of the SCC. Used for quickly
349 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000350 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
351 SCCNodes.insert((*I)->getFunction());
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000352
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000353 // Check each function in turn, determining which functions return noalias
354 // pointers.
Chris Lattner2decb222010-04-16 22:42:17 +0000355 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
356 Function *F = (*I)->getFunction();
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000357
358 if (F == 0)
359 // External node - skip it;
360 return false;
361
362 // Already noalias.
363 if (F->doesNotAlias(0))
364 continue;
365
366 // Definitions with weak linkage may be overridden at linktime, so
367 // treat them like declarations.
368 if (F->isDeclaration() || F->mayBeOverridden())
369 return false;
370
371 // We annotate noalias return values, which are only applicable to
372 // pointer types.
Duncan Sands1df98592010-02-16 11:11:14 +0000373 if (!F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000374 continue;
375
376 if (!IsFunctionMallocLike(F, SCCNodes))
377 return false;
378 }
379
380 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000381 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
382 Function *F = (*I)->getFunction();
Duncan Sands1df98592010-02-16 11:11:14 +0000383 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000384 continue;
385
386 F->setDoesNotAlias(0);
387 ++NumNoAlias;
388 MadeChange = true;
389 }
390
391 return MadeChange;
392}
393
Chris Lattner2decb222010-04-16 22:42:17 +0000394bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000395 AA = &getAnalysis<AliasAnalysis>();
396
Duncan Sands9e89ba32008-12-31 16:14:43 +0000397 bool Changed = AddReadAttrs(SCC);
398 Changed |= AddNoCaptureAttrs(SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000399 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +0000400 return Changed;
401}