blob: 062e366158a84a2e80a8c246fe6841f49364c125 [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"
Nick Lewycky199aa3c2009-03-08 06:20:47 +000026#include "llvm/Analysis/AliasAnalysis.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000027#include "llvm/Analysis/CallGraph.h"
Duncan Sands8556d2a2009-01-18 12:19:30 +000028#include "llvm/Analysis/CaptureTracking.h"
Duncan Sands338cd6b2009-01-02 11:54:37 +000029#include "llvm/ADT/SmallSet.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000030#include "llvm/ADT/Statistic.h"
Nick Lewycky199aa3c2009-03-08 06:20:47 +000031#include "llvm/ADT/UniqueVector.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000032#include "llvm/Support/InstIterator.h"
33using namespace llvm;
34
35STATISTIC(NumReadNone, "Number of functions marked readnone");
36STATISTIC(NumReadOnly, "Number of functions marked readonly");
37STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewycky199aa3c2009-03-08 06:20:47 +000038STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Duncan Sands9e89ba32008-12-31 16:14:43 +000039
40namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000041 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands9e89ba32008-12-31 16:14:43 +000042 static char ID; // Pass identification, replacement for typeid
Dan Gohman3c97f7a2010-11-08 16:10:15 +000043 FunctionAttrs() : CallGraphSCCPass(ID), AA(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000044 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
45 }
Duncan Sands9e89ba32008-12-31 16:14:43 +000046
47 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Chris Lattner2decb222010-04-16 22:42:17 +000048 bool runOnSCC(CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000049
50 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000051 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000052
53 // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000054 bool AddNoCaptureAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000055
Nick Lewycky199aa3c2009-03-08 06:20:47 +000056 // IsFunctionMallocLike - Does this function allocate new memory?
57 bool IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +000058 SmallPtrSet<Function*, 8> &) const;
Nick Lewycky199aa3c2009-03-08 06:20:47 +000059
60 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000061 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +000062
Duncan Sands9e89ba32008-12-31 16:14:43 +000063 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
64 AU.setPreservesCFG();
Dan Gohman3c97f7a2010-11-08 16:10:15 +000065 AU.addRequired<AliasAnalysis>();
Duncan Sands9e89ba32008-12-31 16:14:43 +000066 CallGraphSCCPass::getAnalysisUsage(AU);
67 }
68
Duncan Sands391f5bc2010-11-03 14:45:05 +000069 bool PointsToLocalOrConstantMemory(Value *V);
Dan Gohman3c97f7a2010-11-08 16:10:15 +000070
71 private:
72 AliasAnalysis *AA;
Duncan Sands9e89ba32008-12-31 16:14:43 +000073 };
74}
75
76char FunctionAttrs::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +000077INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
78 "Deduce function attributes", false, false)
79INITIALIZE_AG_DEPENDENCY(CallGraph)
80INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersonce665bd2010-10-07 22:25:06 +000081 "Deduce function attributes", false, false)
Duncan Sands9e89ba32008-12-31 16:14:43 +000082
83Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
84
85
Duncan Sands391f5bc2010-11-03 14:45:05 +000086/// PointsToLocalOrConstantMemory - Returns whether the given pointer value
87/// points to memory that is local to the function, with global constants being
88/// considered local to all functions.
89bool FunctionAttrs::PointsToLocalOrConstantMemory(Value *V) {
Duncan Sands5d8ea112010-01-07 05:48:42 +000090 SmallVector<Value*, 16> Worklist;
91 unsigned MaxLookup = 8;
Duncan Sandse10920d2010-01-06 15:37:47 +000092
93 Worklist.push_back(V);
94
95 do {
96 V = Worklist.pop_back_val()->getUnderlyingObject();
97
98 // An alloca instruction defines local memory.
99 if (isa<AllocaInst>(V))
100 continue;
101
102 // A global constant counts as local memory for our purposes.
103 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
104 if (!GV->isConstant())
105 return false;
106 continue;
107 }
108
109 // If both select values point to local memory, then so does the select.
110 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
111 Worklist.push_back(SI->getTrueValue());
112 Worklist.push_back(SI->getFalseValue());
113 continue;
114 }
115
116 // If all values incoming to a phi node point to local memory, then so does
117 // the phi.
118 if (PHINode *PN = dyn_cast<PHINode>(V)) {
119 // Don't bother inspecting phi nodes with many operands.
120 if (PN->getNumIncomingValues() > MaxLookup)
121 return false;
122 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
123 Worklist.push_back(PN->getIncomingValue(i));
124 continue;
125 }
126
127 return false;
128 } while (!Worklist.empty() && --MaxLookup);
129
130 return Worklist.empty();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000131}
132
133/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000134bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000135 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000136
137 // Fill SCCNodes with the elements of the SCC. Used for quickly
138 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000139 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
140 SCCNodes.insert((*I)->getFunction());
Duncan Sands9e89ba32008-12-31 16:14:43 +0000141
142 // Check if any of the functions in the SCC read or write memory. If they
143 // write memory then they can't be marked readnone or readonly.
144 bool ReadsMemory = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000145 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
146 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000147
148 if (F == 0)
149 // External node - may write memory. Just give up.
150 return false;
151
152 if (F->doesNotAccessMemory())
153 // Already perfect!
154 continue;
155
156 // Definitions with weak linkage may be overridden at linktime with
157 // something that writes memory, so treat them like declarations.
158 if (F->isDeclaration() || F->mayBeOverridden()) {
159 if (!F->onlyReadsMemory())
160 // May write memory. Just give up.
161 return false;
162
163 ReadsMemory = true;
164 continue;
165 }
166
167 // Scan the function body for instructions that may read or write memory.
168 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
169 Instruction *I = &*II;
170
171 // Some instructions can be ignored even if they read or write memory.
172 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif7d3056b2010-07-28 22:50:26 +0000173 CallSite CS(cast<Value>(I));
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000174 if (CS) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000175 // Ignore calls to functions in the same SCC.
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000176 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000177 continue;
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000178 switch (AA->getModRefBehavior(CS)) {
179 case AliasAnalysis::DoesNotAccessMemory:
180 // Ignore calls that don't access memory.
181 continue;
182 case AliasAnalysis::OnlyReadsMemory:
183 // Handle calls that only read from memory.
184 ReadsMemory = true;
185 continue;
186 case AliasAnalysis::AccessesArguments:
187 // Check whether all pointer arguments point to local memory, and
188 // ignore calls that only access local memory.
189 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
190 CI != CE; ++CI) {
191 Value *Arg = *CI;
192 if (Arg->getType()->isPointerTy() &&
193 !PointsToLocalOrConstantMemory(Arg))
194 // Writes memory. Just give up.
195 return false;
Duncan Sands7c422ac2010-01-06 08:45:52 +0000196 }
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000197 // Only reads and writes local memory.
198 continue;
199 default:
200 // Otherwise, be conservative.
201 break;
202 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000203 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Duncan Sandsad6f5412010-10-30 12:59:44 +0000204 // Ignore non-volatile loads from local memory.
Duncan Sands391f5bc2010-11-03 14:45:05 +0000205 if (!LI->isVolatile() &&
206 PointsToLocalOrConstantMemory(LI->getPointerOperand()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000207 continue;
208 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Duncan Sandsad6f5412010-10-30 12:59:44 +0000209 // Ignore non-volatile stores to local memory.
Duncan Sands391f5bc2010-11-03 14:45:05 +0000210 if (!SI->isVolatile() &&
211 PointsToLocalOrConstantMemory(SI->getPointerOperand()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000212 continue;
213 }
214
215 // Any remaining instructions need to be taken seriously! Check if they
216 // read or write memory.
217 if (I->mayWriteToMemory())
218 // Writes memory. Just give up.
219 return false;
Duncan Sandscfd0ebe2009-05-06 08:42:00 +0000220
Duncan Sands9e89ba32008-12-31 16:14:43 +0000221 // If this instruction may read memory, remember that.
222 ReadsMemory |= I->mayReadFromMemory();
223 }
224 }
225
226 // Success! Functions in this SCC do not access memory, or only read memory.
227 // Give them the appropriate attribute.
228 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000229 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
230 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000231
232 if (F->doesNotAccessMemory())
233 // Already perfect!
234 continue;
235
236 if (F->onlyReadsMemory() && ReadsMemory)
237 // No change.
238 continue;
239
240 MadeChange = true;
241
242 // Clear out any existing attributes.
243 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
244
245 // Add in the new attribute.
246 F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone);
247
248 if (ReadsMemory)
Duncan Sandsb2f22792009-01-02 11:46:24 +0000249 ++NumReadOnly;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000250 else
Duncan Sandsb2f22792009-01-02 11:46:24 +0000251 ++NumReadNone;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000252 }
253
254 return MadeChange;
255}
256
Duncan Sands9e89ba32008-12-31 16:14:43 +0000257/// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000258bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000259 bool Changed = false;
260
261 // Check each function in turn, determining which pointer arguments are not
262 // captured.
Chris Lattner2decb222010-04-16 22:42:17 +0000263 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
264 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000265
266 if (F == 0)
267 // External node - skip it;
268 continue;
269
270 // Definitions with weak linkage may be overridden at linktime with
271 // something that writes memory, so treat them like declarations.
272 if (F->isDeclaration() || F->mayBeOverridden())
273 continue;
274
275 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A)
Duncan Sands1df98592010-02-16 11:11:14 +0000276 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr() &&
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000277 !PointerMayBeCaptured(A, true, /*StoreCaptures=*/false)) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000278 A->addAttr(Attribute::NoCapture);
Nick Lewycky6b056862009-01-02 03:46:56 +0000279 ++NumNoCapture;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000280 Changed = true;
281 }
282 }
283
284 return Changed;
285}
286
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000287/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000288/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000289bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +0000290 SmallPtrSet<Function*, 8> &SCCNodes) const {
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000291 UniqueVector<Value *> FlowsToReturn;
292 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
293 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
294 FlowsToReturn.insert(Ret->getReturnValue());
295
296 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
297 Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved.
298
299 if (Constant *C = dyn_cast<Constant>(RetVal)) {
300 if (!C->isNullValue() && !isa<UndefValue>(C))
301 return false;
302
303 continue;
304 }
305
306 if (isa<Argument>(RetVal))
307 return false;
308
309 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
310 switch (RVI->getOpcode()) {
311 // Extend the analysis by looking upwards.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000312 case Instruction::BitCast:
Victor Hernandez83d63912009-09-18 22:35:49 +0000313 case Instruction::GetElementPtr:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000314 FlowsToReturn.insert(RVI->getOperand(0));
315 continue;
316 case Instruction::Select: {
317 SelectInst *SI = cast<SelectInst>(RVI);
318 FlowsToReturn.insert(SI->getTrueValue());
319 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner439044f2009-09-27 21:29:28 +0000320 continue;
321 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000322 case Instruction::PHI: {
323 PHINode *PN = cast<PHINode>(RVI);
324 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
325 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner439044f2009-09-27 21:29:28 +0000326 continue;
327 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000328
329 // Check whether the pointer came from an allocation.
330 case Instruction::Alloca:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000331 break;
332 case Instruction::Call:
333 case Instruction::Invoke: {
334 CallSite CS(RVI);
335 if (CS.paramHasAttr(0, Attribute::NoAlias))
336 break;
337 if (CS.getCalledFunction() &&
Chris Lattner98a27ce2009-08-31 04:09:04 +0000338 SCCNodes.count(CS.getCalledFunction()))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000339 break;
340 } // fall-through
341 default:
342 return false; // Did not come from an allocation.
343 }
344
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000345 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000346 return false;
347 }
348
349 return true;
350}
351
352/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000353bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000354 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000355
356 // Fill SCCNodes with the elements of the SCC. Used for quickly
357 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000358 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
359 SCCNodes.insert((*I)->getFunction());
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000360
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000361 // Check each function in turn, determining which functions return noalias
362 // pointers.
Chris Lattner2decb222010-04-16 22:42:17 +0000363 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
364 Function *F = (*I)->getFunction();
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000365
366 if (F == 0)
367 // External node - skip it;
368 return false;
369
370 // Already noalias.
371 if (F->doesNotAlias(0))
372 continue;
373
374 // Definitions with weak linkage may be overridden at linktime, so
375 // treat them like declarations.
376 if (F->isDeclaration() || F->mayBeOverridden())
377 return false;
378
379 // We annotate noalias return values, which are only applicable to
380 // pointer types.
Duncan Sands1df98592010-02-16 11:11:14 +0000381 if (!F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000382 continue;
383
384 if (!IsFunctionMallocLike(F, SCCNodes))
385 return false;
386 }
387
388 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000389 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
390 Function *F = (*I)->getFunction();
Duncan Sands1df98592010-02-16 11:11:14 +0000391 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000392 continue;
393
394 F->setDoesNotAlias(0);
395 ++NumNoAlias;
396 MadeChange = true;
397 }
398
399 return MadeChange;
400}
401
Chris Lattner2decb222010-04-16 22:42:17 +0000402bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000403 AA = &getAnalysis<AliasAnalysis>();
404
Duncan Sands9e89ba32008-12-31 16:14:43 +0000405 bool Changed = AddReadAttrs(SCC);
406 Changed |= AddNoCaptureAttrs(SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000407 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +0000408 return Changed;
409}