blob: d4bce9e48dbe23ea33c38c8d4ecd7772e17d98c8 [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"
Victor Hernandezf006b182009-10-27 20:05:49 +000029#include "llvm/Analysis/MemoryBuiltins.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
Owen Anderson081c34b2010-10-19 17:21:58 +000044 FunctionAttrs() : CallGraphSCCPass(ID) {
45 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();
66 CallGraphSCCPass::getAnalysisUsage(AU);
67 }
68
69 bool PointsToLocalMemory(Value *V);
70 };
71}
72
73char FunctionAttrs::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +000074INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
75 "Deduce function attributes", false, false)
76INITIALIZE_AG_DEPENDENCY(CallGraph)
77INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersonce665bd2010-10-07 22:25:06 +000078 "Deduce function attributes", false, false)
Duncan Sands9e89ba32008-12-31 16:14:43 +000079
80Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
81
82
83/// PointsToLocalMemory - Returns whether the given pointer value points to
84/// memory that is local to the function. Global constants are considered
85/// local to all functions.
86bool FunctionAttrs::PointsToLocalMemory(Value *V) {
Duncan Sands5d8ea112010-01-07 05:48:42 +000087 SmallVector<Value*, 16> Worklist;
88 unsigned MaxLookup = 8;
Duncan Sandse10920d2010-01-06 15:37:47 +000089
90 Worklist.push_back(V);
91
92 do {
93 V = Worklist.pop_back_val()->getUnderlyingObject();
94
95 // An alloca instruction defines local memory.
96 if (isa<AllocaInst>(V))
97 continue;
98
99 // A global constant counts as local memory for our purposes.
100 if (GlobalVariable *GV = dyn_cast<GlobalVariable>(V)) {
101 if (!GV->isConstant())
102 return false;
103 continue;
104 }
105
106 // If both select values point to local memory, then so does the select.
107 if (SelectInst *SI = dyn_cast<SelectInst>(V)) {
108 Worklist.push_back(SI->getTrueValue());
109 Worklist.push_back(SI->getFalseValue());
110 continue;
111 }
112
113 // If all values incoming to a phi node point to local memory, then so does
114 // the phi.
115 if (PHINode *PN = dyn_cast<PHINode>(V)) {
116 // Don't bother inspecting phi nodes with many operands.
117 if (PN->getNumIncomingValues() > MaxLookup)
118 return false;
119 for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
120 Worklist.push_back(PN->getIncomingValue(i));
121 continue;
122 }
123
124 return false;
125 } while (!Worklist.empty() && --MaxLookup);
126
127 return Worklist.empty();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000128}
129
130/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000131bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000132 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000133
134 // Fill SCCNodes with the elements of the SCC. Used for quickly
135 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000136 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
137 SCCNodes.insert((*I)->getFunction());
Duncan Sands9e89ba32008-12-31 16:14:43 +0000138
139 // Check if any of the functions in the SCC read or write memory. If they
140 // write memory then they can't be marked readnone or readonly.
141 bool ReadsMemory = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000142 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
143 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000144
145 if (F == 0)
146 // External node - may write memory. Just give up.
147 return false;
148
149 if (F->doesNotAccessMemory())
150 // Already perfect!
151 continue;
152
153 // Definitions with weak linkage may be overridden at linktime with
154 // something that writes memory, so treat them like declarations.
155 if (F->isDeclaration() || F->mayBeOverridden()) {
156 if (!F->onlyReadsMemory())
157 // May write memory. Just give up.
158 return false;
159
160 ReadsMemory = true;
161 continue;
162 }
163
164 // Scan the function body for instructions that may read or write memory.
165 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
166 Instruction *I = &*II;
167
168 // Some instructions can be ignored even if they read or write memory.
169 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif7d3056b2010-07-28 22:50:26 +0000170 CallSite CS(cast<Value>(I));
171 if (CS && CS.getCalledFunction()) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000172 // Ignore calls to functions in the same SCC.
Chris Lattner98a27ce2009-08-31 04:09:04 +0000173 if (SCCNodes.count(CS.getCalledFunction()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000174 continue;
Duncan Sands7c422ac2010-01-06 08:45:52 +0000175 // Ignore intrinsics that only access local memory.
176 if (unsigned id = CS.getCalledFunction()->getIntrinsicID())
Dan Gohman79fca6f2010-08-03 21:48:53 +0000177 if (AliasAnalysis::getIntrinsicModRefBehavior(id) ==
Duncan Sands7c422ac2010-01-06 08:45:52 +0000178 AliasAnalysis::AccessesArguments) {
179 // Check that all pointer arguments point to local memory.
180 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
181 CI != CE; ++CI) {
182 Value *Arg = *CI;
Duncan Sands1df98592010-02-16 11:11:14 +0000183 if (Arg->getType()->isPointerTy() && !PointsToLocalMemory(Arg))
Duncan Sands7c422ac2010-01-06 08:45:52 +0000184 // Writes memory. Just give up.
185 return false;
186 }
187 // Only reads and writes local memory.
188 continue;
189 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000190 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
191 // Ignore loads from local memory.
192 if (PointsToLocalMemory(LI->getPointerOperand()))
193 continue;
194 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
195 // Ignore stores to local memory.
196 if (PointsToLocalMemory(SI->getPointerOperand()))
197 continue;
198 }
199
200 // Any remaining instructions need to be taken seriously! Check if they
201 // read or write memory.
202 if (I->mayWriteToMemory())
203 // Writes memory. Just give up.
204 return false;
Duncan Sandscfd0ebe2009-05-06 08:42:00 +0000205
Victor Hernandeza276c602009-10-17 01:18:07 +0000206 if (isMalloc(I))
Victor Hernandez83d63912009-09-18 22:35:49 +0000207 // malloc claims not to write memory! PR3754.
Duncan Sandscfd0ebe2009-05-06 08:42:00 +0000208 return false;
209
Duncan Sands9e89ba32008-12-31 16:14:43 +0000210 // If this instruction may read memory, remember that.
211 ReadsMemory |= I->mayReadFromMemory();
212 }
213 }
214
215 // Success! Functions in this SCC do not access memory, or only read memory.
216 // Give them the appropriate attribute.
217 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000218 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
219 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000220
221 if (F->doesNotAccessMemory())
222 // Already perfect!
223 continue;
224
225 if (F->onlyReadsMemory() && ReadsMemory)
226 // No change.
227 continue;
228
229 MadeChange = true;
230
231 // Clear out any existing attributes.
232 F->removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
233
234 // Add in the new attribute.
235 F->addAttribute(~0, ReadsMemory? Attribute::ReadOnly : Attribute::ReadNone);
236
237 if (ReadsMemory)
Duncan Sandsb2f22792009-01-02 11:46:24 +0000238 ++NumReadOnly;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000239 else
Duncan Sandsb2f22792009-01-02 11:46:24 +0000240 ++NumReadNone;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000241 }
242
243 return MadeChange;
244}
245
Duncan Sands9e89ba32008-12-31 16:14:43 +0000246/// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000247bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000248 bool Changed = false;
249
250 // Check each function in turn, determining which pointer arguments are not
251 // captured.
Chris Lattner2decb222010-04-16 22:42:17 +0000252 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
253 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000254
255 if (F == 0)
256 // External node - skip it;
257 continue;
258
259 // Definitions with weak linkage may be overridden at linktime with
260 // something that writes memory, so treat them like declarations.
261 if (F->isDeclaration() || F->mayBeOverridden())
262 continue;
263
264 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A!=E; ++A)
Duncan Sands1df98592010-02-16 11:11:14 +0000265 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr() &&
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000266 !PointerMayBeCaptured(A, true, /*StoreCaptures=*/false)) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000267 A->addAttr(Attribute::NoCapture);
Nick Lewycky6b056862009-01-02 03:46:56 +0000268 ++NumNoCapture;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000269 Changed = true;
270 }
271 }
272
273 return Changed;
274}
275
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000276/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000277/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000278bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +0000279 SmallPtrSet<Function*, 8> &SCCNodes) const {
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000280 UniqueVector<Value *> FlowsToReturn;
281 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
282 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
283 FlowsToReturn.insert(Ret->getReturnValue());
284
285 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
286 Value *RetVal = FlowsToReturn[i+1]; // UniqueVector[0] is reserved.
287
288 if (Constant *C = dyn_cast<Constant>(RetVal)) {
289 if (!C->isNullValue() && !isa<UndefValue>(C))
290 return false;
291
292 continue;
293 }
294
295 if (isa<Argument>(RetVal))
296 return false;
297
298 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
299 switch (RVI->getOpcode()) {
300 // Extend the analysis by looking upwards.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000301 case Instruction::BitCast:
Victor Hernandez83d63912009-09-18 22:35:49 +0000302 case Instruction::GetElementPtr:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000303 FlowsToReturn.insert(RVI->getOperand(0));
304 continue;
305 case Instruction::Select: {
306 SelectInst *SI = cast<SelectInst>(RVI);
307 FlowsToReturn.insert(SI->getTrueValue());
308 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner439044f2009-09-27 21:29:28 +0000309 continue;
310 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000311 case Instruction::PHI: {
312 PHINode *PN = cast<PHINode>(RVI);
313 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
314 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner439044f2009-09-27 21:29:28 +0000315 continue;
316 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000317
318 // Check whether the pointer came from an allocation.
319 case Instruction::Alloca:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000320 break;
321 case Instruction::Call:
322 case Instruction::Invoke: {
323 CallSite CS(RVI);
324 if (CS.paramHasAttr(0, Attribute::NoAlias))
325 break;
326 if (CS.getCalledFunction() &&
Chris Lattner98a27ce2009-08-31 04:09:04 +0000327 SCCNodes.count(CS.getCalledFunction()))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000328 break;
329 } // fall-through
330 default:
331 return false; // Did not come from an allocation.
332 }
333
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000334 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000335 return false;
336 }
337
338 return true;
339}
340
341/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000342bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000343 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000344
345 // Fill SCCNodes with the elements of the SCC. Used for quickly
346 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000347 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
348 SCCNodes.insert((*I)->getFunction());
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000349
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000350 // Check each function in turn, determining which functions return noalias
351 // pointers.
Chris Lattner2decb222010-04-16 22:42:17 +0000352 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
353 Function *F = (*I)->getFunction();
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000354
355 if (F == 0)
356 // External node - skip it;
357 return false;
358
359 // Already noalias.
360 if (F->doesNotAlias(0))
361 continue;
362
363 // Definitions with weak linkage may be overridden at linktime, so
364 // treat them like declarations.
365 if (F->isDeclaration() || F->mayBeOverridden())
366 return false;
367
368 // We annotate noalias return values, which are only applicable to
369 // pointer types.
Duncan Sands1df98592010-02-16 11:11:14 +0000370 if (!F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000371 continue;
372
373 if (!IsFunctionMallocLike(F, SCCNodes))
374 return false;
375 }
376
377 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000378 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
379 Function *F = (*I)->getFunction();
Duncan Sands1df98592010-02-16 11:11:14 +0000380 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000381 continue;
382
383 F->setDoesNotAlias(0);
384 ++NumNoAlias;
385 MadeChange = true;
386 }
387
388 return MadeChange;
389}
390
Chris Lattner2decb222010-04-16 22:42:17 +0000391bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000392 bool Changed = AddReadAttrs(SCC);
393 Changed |= AddNoCaptureAttrs(SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000394 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +0000395 return Changed;
396}