blob: 9e55e61c51c1a265ee234f3c0e1ec11831b8e8b0 [file] [log] [blame]
Meador Ingecf47ce62013-03-21 00:55:59 +00001//===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
Duncan Sands9e89ba32008-12-31 16:14:43 +00002//
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.
Meador Ingecf47ce62013-03-21 00:55:59 +000017// Finally, well-known library call declarations are marked with all
18// attributes that are consistent with the function's standard definition.
Duncan Sandsb2f22792009-01-02 11:46:24 +000019// This pass is implemented as a bottom-up traversal of the call-graph.
Duncan Sands9e89ba32008-12-31 16:14:43 +000020//
21//===----------------------------------------------------------------------===//
22
23#define DEBUG_TYPE "functionattrs"
24#include "llvm/Transforms/IPO.h"
Nick Lewyckyb48a1892011-12-28 23:24:21 +000025#include "llvm/ADT/SCCIterator.h"
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +000026#include "llvm/ADT/SetVector.h"
Duncan Sands338cd6b2009-01-02 11:54:37 +000027#include "llvm/ADT/SmallSet.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000028#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Analysis/AliasAnalysis.h"
30#include "llvm/Analysis/CallGraph.h"
Chandler Carruth3251e812013-01-07 15:26:48 +000031#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000032#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/GlobalVariable.h"
34#include "llvm/IR/IntrinsicInst.h"
35#include "llvm/IR/LLVMContext.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000036#include "llvm/Support/InstIterator.h"
Meador Ingecf47ce62013-03-21 00:55:59 +000037#include "llvm/Target/TargetLibraryInfo.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000038using namespace llvm;
39
40STATISTIC(NumReadNone, "Number of functions marked readnone");
41STATISTIC(NumReadOnly, "Number of functions marked readonly");
42STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewycky199aa3c2009-03-08 06:20:47 +000043STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Meador Ingecf47ce62013-03-21 00:55:59 +000044STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Duncan Sands9e89ba32008-12-31 16:14:43 +000045
46namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000047 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands9e89ba32008-12-31 16:14:43 +000048 static char ID; // Pass identification, replacement for typeid
Dan Gohman3c97f7a2010-11-08 16:10:15 +000049 FunctionAttrs() : CallGraphSCCPass(ID), AA(0) {
Owen Anderson081c34b2010-10-19 17:21:58 +000050 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
51 }
Duncan Sands9e89ba32008-12-31 16:14:43 +000052
53 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Chris Lattner2decb222010-04-16 22:42:17 +000054 bool runOnSCC(CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000055
56 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000057 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000058
59 // AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000060 bool AddNoCaptureAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000061
Nick Lewycky199aa3c2009-03-08 06:20:47 +000062 // IsFunctionMallocLike - Does this function allocate new memory?
63 bool IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +000064 SmallPtrSet<Function*, 8> &) const;
Nick Lewycky199aa3c2009-03-08 06:20:47 +000065
66 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000067 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +000068
Meador Ingecf47ce62013-03-21 00:55:59 +000069 // Utility methods used by inferPrototypeAttributes to add attributes
70 // and maintain annotation statistics.
71
72 void setDoesNotAccessMemory(Function &F) {
73 if (!F.doesNotAccessMemory()) {
74 F.setDoesNotAccessMemory();
75 ++NumAnnotated;
76 }
77 }
78
79 void setOnlyReadsMemory(Function &F) {
80 if (!F.onlyReadsMemory()) {
81 F.setOnlyReadsMemory();
82 ++NumAnnotated;
83 }
84 }
85
86 void setDoesNotThrow(Function &F) {
87 if (!F.doesNotThrow()) {
88 F.setDoesNotThrow();
89 ++NumAnnotated;
90 }
91 }
92
93 void setDoesNotCapture(Function &F, unsigned n) {
94 if (!F.doesNotCapture(n)) {
95 F.setDoesNotCapture(n);
96 ++NumAnnotated;
97 }
98 }
99
100 void setDoesNotAlias(Function &F, unsigned n) {
101 if (!F.doesNotAlias(n)) {
102 F.setDoesNotAlias(n);
103 ++NumAnnotated;
104 }
105 }
106
107 // inferPrototypeAttributes - Analyze the name and prototype of the
108 // given function and set any applicable attributes. Returns true
109 // if any attributes were set and false otherwise.
110 bool inferPrototypeAttributes(Function &F);
111
112 // annotateLibraryCalls - Adds attributes to well-known standard library
113 // call declarations.
114 bool annotateLibraryCalls(const CallGraphSCC &SCC);
115
Duncan Sands9e89ba32008-12-31 16:14:43 +0000116 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
117 AU.setPreservesCFG();
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000118 AU.addRequired<AliasAnalysis>();
Meador Ingecf47ce62013-03-21 00:55:59 +0000119 AU.addRequired<TargetLibraryInfo>();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000120 CallGraphSCCPass::getAnalysisUsage(AU);
121 }
122
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000123 private:
124 AliasAnalysis *AA;
Meador Ingecf47ce62013-03-21 00:55:59 +0000125 TargetLibraryInfo *TLI;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000126 };
127}
128
129char FunctionAttrs::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +0000130INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
131 "Deduce function attributes", false, false)
132INITIALIZE_AG_DEPENDENCY(CallGraph)
Meador Ingecf47ce62013-03-21 00:55:59 +0000133INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Owen Andersonae0a7bc2010-10-13 22:00:45 +0000134INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersonce665bd2010-10-07 22:25:06 +0000135 "Deduce function attributes", false, false)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000136
137Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
138
139
Duncan Sands9e89ba32008-12-31 16:14:43 +0000140/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000141bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000142 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000143
144 // Fill SCCNodes with the elements of the SCC. Used for quickly
145 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000146 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
147 SCCNodes.insert((*I)->getFunction());
Duncan Sands9e89ba32008-12-31 16:14:43 +0000148
149 // Check if any of the functions in the SCC read or write memory. If they
150 // write memory then they can't be marked readnone or readonly.
151 bool ReadsMemory = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000152 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
153 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000154
155 if (F == 0)
156 // External node - may write memory. Just give up.
157 return false;
158
Dan Gohman6d44d642010-11-09 20:13:27 +0000159 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
160 if (MRB == AliasAnalysis::DoesNotAccessMemory)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000161 // Already perfect!
162 continue;
163
164 // Definitions with weak linkage may be overridden at linktime with
165 // something that writes memory, so treat them like declarations.
166 if (F->isDeclaration() || F->mayBeOverridden()) {
Dan Gohman6d44d642010-11-09 20:13:27 +0000167 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000168 // May write memory. Just give up.
169 return false;
170
171 ReadsMemory = true;
172 continue;
173 }
174
175 // Scan the function body for instructions that may read or write memory.
176 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
177 Instruction *I = &*II;
178
179 // Some instructions can be ignored even if they read or write memory.
180 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif7d3056b2010-07-28 22:50:26 +0000181 CallSite CS(cast<Value>(I));
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000182 if (CS) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000183 // Ignore calls to functions in the same SCC.
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000184 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000185 continue;
Dan Gohman42c31a72010-11-10 01:02:18 +0000186 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
187 // If the call doesn't access arbitrary memory, we may be able to
188 // figure out something.
Dan Gohman432d08c2010-11-10 17:34:04 +0000189 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
190 // If the call does access argument pointees, check each argument.
Dan Gohman68a60562010-11-10 18:17:28 +0000191 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman42c31a72010-11-10 01:02:18 +0000192 // Check whether all pointer arguments point to local memory, and
193 // ignore calls that only access local memory.
194 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
195 CI != CE; ++CI) {
196 Value *Arg = *CI;
197 if (Arg->getType()->isPointerTy()) {
198 AliasAnalysis::Location Loc(Arg,
199 AliasAnalysis::UnknownSize,
200 I->getMetadata(LLVMContext::MD_tbaa));
201 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
202 if (MRB & AliasAnalysis::Mod)
203 // Writes non-local memory. Give up.
204 return false;
205 if (MRB & AliasAnalysis::Ref)
206 // Ok, it reads non-local memory.
207 ReadsMemory = true;
208 }
Dan Gohman40b6a192010-11-09 19:56:27 +0000209 }
210 }
Dan Gohman40b6a192010-11-09 19:56:27 +0000211 continue;
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000212 }
Dan Gohman42c31a72010-11-10 01:02:18 +0000213 // The call could access any memory. If that includes writes, give up.
214 if (MRB & AliasAnalysis::Mod)
215 return false;
216 // If it reads, note it.
217 if (MRB & AliasAnalysis::Ref)
218 ReadsMemory = true;
219 continue;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000220 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedman2199dfb2011-08-16 01:28:22 +0000221 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
222 if (!LI->isVolatile()) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000223 AliasAnalysis::Location Loc = AA->getLocation(LI);
Dan Gohmanea8900f2010-11-08 17:12:04 +0000224 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
225 continue;
226 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000227 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedman2199dfb2011-08-16 01:28:22 +0000228 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
229 if (!SI->isVolatile()) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000230 AliasAnalysis::Location Loc = AA->getLocation(SI);
Dan Gohmanea8900f2010-11-08 17:12:04 +0000231 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
232 continue;
233 }
Dan Gohman4cf0dcf2010-11-09 20:17:38 +0000234 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
235 // Ignore vaargs on local memory.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000236 AliasAnalysis::Location Loc = AA->getLocation(VI);
Dan Gohman4cf0dcf2010-11-09 20:17:38 +0000237 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
238 continue;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000239 }
240
241 // Any remaining instructions need to be taken seriously! Check if they
242 // read or write memory.
243 if (I->mayWriteToMemory())
244 // Writes memory. Just give up.
245 return false;
Duncan Sandscfd0ebe2009-05-06 08:42:00 +0000246
Duncan Sands9e89ba32008-12-31 16:14:43 +0000247 // If this instruction may read memory, remember that.
248 ReadsMemory |= I->mayReadFromMemory();
249 }
250 }
251
252 // Success! Functions in this SCC do not access memory, or only read memory.
253 // Give them the appropriate attribute.
254 bool MadeChange = false;
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->doesNotAccessMemory())
259 // Already perfect!
260 continue;
261
262 if (F->onlyReadsMemory() && ReadsMemory)
263 // No change.
264 continue;
265
266 MadeChange = true;
267
268 // Clear out any existing attributes.
Bill Wendling702cc912012-10-15 20:35:56 +0000269 AttrBuilder B;
Bill Wendling034b94b2012-12-19 07:18:57 +0000270 B.addAttribute(Attribute::ReadOnly)
271 .addAttribute(Attribute::ReadNone);
Bill Wendling8246df62013-01-23 00:45:55 +0000272 F->removeAttributes(AttributeSet::FunctionIndex,
273 AttributeSet::get(F->getContext(),
274 AttributeSet::FunctionIndex, B));
Duncan Sands9e89ba32008-12-31 16:14:43 +0000275
276 // Add in the new attribute.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000277 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendling70d2ca02013-01-23 00:20:53 +0000278 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands9e89ba32008-12-31 16:14:43 +0000279
280 if (ReadsMemory)
Duncan Sandsb2f22792009-01-02 11:46:24 +0000281 ++NumReadOnly;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000282 else
Duncan Sandsb2f22792009-01-02 11:46:24 +0000283 ++NumReadNone;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000284 }
285
286 return MadeChange;
287}
288
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000289namespace {
290 // For a given pointer Argument, this retains a list of Arguments of functions
291 // in the same SCC that the pointer data flows into. We use this to build an
292 // SCC of the arguments.
293 struct ArgumentGraphNode {
294 Argument *Definition;
295 SmallVector<ArgumentGraphNode*, 4> Uses;
296 };
297
298 class ArgumentGraph {
299 // We store pointers to ArgumentGraphNode objects, so it's important that
300 // that they not move around upon insert.
301 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
302
303 ArgumentMapTy ArgumentMap;
304
305 // There is no root node for the argument graph, in fact:
306 // void f(int *x, int *y) { if (...) f(x, y); }
307 // is an example where the graph is disconnected. The SCCIterator requires a
308 // single entry point, so we maintain a fake ("synthetic") root node that
309 // uses every node. Because the graph is directed and nothing points into
310 // the root, it will not participate in any SCCs (except for its own).
311 ArgumentGraphNode SyntheticRoot;
312
313 public:
314 ArgumentGraph() { SyntheticRoot.Definition = 0; }
315
316 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
317
318 iterator begin() { return SyntheticRoot.Uses.begin(); }
319 iterator end() { return SyntheticRoot.Uses.end(); }
320 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
321
322 ArgumentGraphNode *operator[](Argument *A) {
323 ArgumentGraphNode &Node = ArgumentMap[A];
324 Node.Definition = A;
325 SyntheticRoot.Uses.push_back(&Node);
326 return &Node;
327 }
328 };
329
330 // This tracker checks whether callees are in the SCC, and if so it does not
331 // consider that a capture, instead adding it to the "Uses" list and
332 // continuing with the analysis.
333 struct ArgumentUsesTracker : public CaptureTracker {
334 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
335 : Captured(false), SCCNodes(SCCNodes) {}
336
337 void tooManyUses() { Captured = true; }
338
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000339 bool captured(Use *U) {
340 CallSite CS(U->getUser());
341 if (!CS.getInstruction()) { Captured = true; return true; }
342
343 Function *F = CS.getCalledFunction();
344 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
345
346 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
347 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
348 PI != PE; ++PI, ++AI) {
349 if (AI == AE) {
350 assert(F->isVarArg() && "More params than args in non-varargs call");
351 Captured = true;
352 return true;
353 }
354 if (PI == U) {
355 Uses.push_back(AI);
356 break;
357 }
358 }
359 assert(!Uses.empty() && "Capturing call-site captured nothing?");
360 return false;
361 }
362
363 bool Captured; // True only if certainly captured (used outside our SCC).
364 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
365
366 const SmallPtrSet<Function*, 8> &SCCNodes;
367 };
368}
369
370namespace llvm {
371 template<> struct GraphTraits<ArgumentGraphNode*> {
372 typedef ArgumentGraphNode NodeType;
373 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
374
375 static inline NodeType *getEntryNode(NodeType *A) { return A; }
376 static inline ChildIteratorType child_begin(NodeType *N) {
377 return N->Uses.begin();
378 }
379 static inline ChildIteratorType child_end(NodeType *N) {
380 return N->Uses.end();
381 }
382 };
383 template<> struct GraphTraits<ArgumentGraph*>
384 : public GraphTraits<ArgumentGraphNode*> {
385 static NodeType *getEntryNode(ArgumentGraph *AG) {
386 return AG->getEntryNode();
387 }
388 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
389 return AG->begin();
390 }
391 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
392 return AG->end();
393 }
394 };
395}
396
Duncan Sands9e89ba32008-12-31 16:14:43 +0000397/// AddNoCaptureAttrs - Deduce nocapture attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000398bool FunctionAttrs::AddNoCaptureAttrs(const CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000399 bool Changed = false;
400
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000401 SmallPtrSet<Function*, 8> SCCNodes;
402
403 // Fill SCCNodes with the elements of the SCC. Used for quickly
404 // looking up whether a given CallGraphNode is in this SCC.
405 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
406 Function *F = (*I)->getFunction();
407 if (F && !F->isDeclaration() && !F->mayBeOverridden())
408 SCCNodes.insert(F);
409 }
410
411 ArgumentGraph AG;
412
Duncan Sands9e89ba32008-12-31 16:14:43 +0000413 // Check each function in turn, determining which pointer arguments are not
414 // captured.
Chris Lattner2decb222010-04-16 22:42:17 +0000415 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
416 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000417
418 if (F == 0)
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000419 // External node - only a problem for arguments that we pass to it.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000420 continue;
421
422 // Definitions with weak linkage may be overridden at linktime with
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000423 // something that captures pointers, so treat them like declarations.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000424 if (F->isDeclaration() || F->mayBeOverridden())
425 continue;
426
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000427 SmallVector<AttributeSet, 8> AttrSets;
428
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000429 // Functions that are readonly (or readnone) and nounwind and don't return
430 // a value can't capture arguments. Don't analyze them.
431 if (F->onlyReadsMemory() && F->doesNotThrow() &&
432 F->getReturnType()->isVoidTy()) {
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000433 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
434 ++A) {
435 if (!A->getType()->isPointerTy() || A->hasNoCaptureAttr())
436 continue;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000437
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000438 AttributeSet In =
439 F->getAttributes().getParamAttributes(A->getArgNo() + 1);
440 AttrSets.push_back(In.addAttribute(F->getContext(), A->getArgNo() + 1,
441 Attribute::NoCapture));
442 }
443 } else {
444 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
445 ++A) {
446 if (!A->getType()->isPointerTy() || A->hasNoCaptureAttr())
447 continue;
448
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000449 ArgumentUsesTracker Tracker(SCCNodes);
450 PointerMayBeCaptured(A, &Tracker);
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000451 if (Tracker.Captured)
452 continue; // It's captured. Don't bother doing SCC analysis on it.
453
454 if (Tracker.Uses.empty()) {
455 // If it's trivially not captured, mark it nocapture now.
456 AttributeSet In =
457 F->getAttributes().getParamAttributes(A->getArgNo() + 1);
458 AttrSets.push_back(In.addAttribute(F->getContext(), A->getArgNo() + 1,
459 Attribute::NoCapture));
460 } else {
461 // If it's not trivially captured and not trivially not captured,
462 // then it must be calling into another function in our SCC. Save
463 // its particulars for Argument-SCC analysis later.
464 ArgumentGraphNode *Node = AG[A];
465 for (SmallVectorImpl<Argument *>::iterator UI = Tracker.Uses.begin(),
466 UE = Tracker.Uses.end();
467 UI != UE; ++UI)
468 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000469 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000470 }
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000471 }
472
473 // Merge all attribute sets into one in a single step.
474 if (!AttrSets.empty()) {
475 NumNoCapture += AttrSets.size();
476 AttrSets.push_back(F->getAttributes());
477 F->setAttributes(AttributeSet::get(F->getContext(), AttrSets));
478 Changed = true;
479 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000480 }
481
482 // The graph we've collected is partial because we stopped scanning for
483 // argument uses once we solved the argument trivially. These partial nodes
484 // show up as ArgumentGraphNode objects with an empty Uses list, and for
485 // these nodes the final decision about whether they capture has already been
486 // made. If the definition doesn't have a 'nocapture' attribute by now, it
487 // captures.
488
489 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG), E = scc_end(&AG);
490 I != E; ++I) {
491 std::vector<ArgumentGraphNode*> &ArgumentSCC = *I;
492 if (ArgumentSCC.size() == 1) {
493 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
494
495 // eg. "void f(int* x) { if (...) f(x); }"
496 if (ArgumentSCC[0]->Uses.size() == 1 &&
497 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Bill Wendlingcb3de0b2012-10-15 04:46:55 +0000498 ArgumentSCC[0]->
499 Definition->
Bill Wendling28d65722013-01-23 06:14:59 +0000500 addAttr(AttributeSet::get(ArgumentSCC[0]->Definition->getContext(),
501 ArgumentSCC[0]->Definition->getArgNo() + 1,
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000502 Attribute::NoCapture));
Nick Lewycky6b056862009-01-02 03:46:56 +0000503 ++NumNoCapture;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000504 Changed = true;
505 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000506 continue;
507 }
508
509 bool SCCCaptured = false;
510 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
511 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
512 ArgumentGraphNode *Node = *I;
513 if (Node->Uses.empty()) {
514 if (!Node->Definition->hasNoCaptureAttr())
515 SCCCaptured = true;
516 }
517 }
518 if (SCCCaptured) continue;
519
520 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
521 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
522 // quickly looking up whether a given Argument is in this ArgumentSCC.
523 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
524 E = ArgumentSCC.end(); I != E; ++I) {
525 ArgumentSCCNodes.insert((*I)->Definition);
526 }
527
528 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
529 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
530 ArgumentGraphNode *N = *I;
531 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
532 UE = N->Uses.end(); UI != UE; ++UI) {
533 Argument *A = (*UI)->Definition;
534 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
535 continue;
536 SCCCaptured = true;
537 break;
538 }
539 }
540 if (SCCCaptured) continue;
541
Nick Lewycky720ac912012-01-05 22:21:45 +0000542 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000543 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000544 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1,
545 Attribute::NoCapture));
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000546 ++NumNoCapture;
547 Changed = true;
548 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000549 }
550
551 return Changed;
552}
553
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000554/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000555/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000556bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +0000557 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000558 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000559 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
560 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
561 FlowsToReturn.insert(Ret->getReturnValue());
562
563 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000564 Value *RetVal = FlowsToReturn[i];
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000565
566 if (Constant *C = dyn_cast<Constant>(RetVal)) {
567 if (!C->isNullValue() && !isa<UndefValue>(C))
568 return false;
569
570 continue;
571 }
572
573 if (isa<Argument>(RetVal))
574 return false;
575
576 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
577 switch (RVI->getOpcode()) {
578 // Extend the analysis by looking upwards.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000579 case Instruction::BitCast:
Victor Hernandez83d63912009-09-18 22:35:49 +0000580 case Instruction::GetElementPtr:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000581 FlowsToReturn.insert(RVI->getOperand(0));
582 continue;
583 case Instruction::Select: {
584 SelectInst *SI = cast<SelectInst>(RVI);
585 FlowsToReturn.insert(SI->getTrueValue());
586 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner439044f2009-09-27 21:29:28 +0000587 continue;
588 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000589 case Instruction::PHI: {
590 PHINode *PN = cast<PHINode>(RVI);
591 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
592 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner439044f2009-09-27 21:29:28 +0000593 continue;
594 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000595
596 // Check whether the pointer came from an allocation.
597 case Instruction::Alloca:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000598 break;
599 case Instruction::Call:
600 case Instruction::Invoke: {
601 CallSite CS(RVI);
Bill Wendling034b94b2012-12-19 07:18:57 +0000602 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000603 break;
604 if (CS.getCalledFunction() &&
Chris Lattner98a27ce2009-08-31 04:09:04 +0000605 SCCNodes.count(CS.getCalledFunction()))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000606 break;
607 } // fall-through
608 default:
609 return false; // Did not come from an allocation.
610 }
611
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000612 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000613 return false;
614 }
615
616 return true;
617}
618
619/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000620bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000621 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000622
623 // Fill SCCNodes with the elements of the SCC. Used for quickly
624 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000625 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
626 SCCNodes.insert((*I)->getFunction());
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000627
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000628 // Check each function in turn, determining which functions return noalias
629 // pointers.
Chris Lattner2decb222010-04-16 22:42:17 +0000630 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
631 Function *F = (*I)->getFunction();
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000632
633 if (F == 0)
634 // External node - skip it;
635 return false;
636
637 // Already noalias.
638 if (F->doesNotAlias(0))
639 continue;
640
641 // Definitions with weak linkage may be overridden at linktime, so
642 // treat them like declarations.
643 if (F->isDeclaration() || F->mayBeOverridden())
644 return false;
645
646 // We annotate noalias return values, which are only applicable to
647 // pointer types.
Duncan Sands1df98592010-02-16 11:11:14 +0000648 if (!F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000649 continue;
650
651 if (!IsFunctionMallocLike(F, SCCNodes))
652 return false;
653 }
654
655 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000656 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
657 Function *F = (*I)->getFunction();
Duncan Sands1df98592010-02-16 11:11:14 +0000658 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000659 continue;
660
661 F->setDoesNotAlias(0);
662 ++NumNoAlias;
663 MadeChange = true;
664 }
665
666 return MadeChange;
667}
668
Meador Ingecf47ce62013-03-21 00:55:59 +0000669/// inferPrototypeAttributes - Analyze the name and prototype of the
670/// given function and set any applicable attributes. Returns true
671/// if any attributes were set and false otherwise.
672bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
673 FunctionType *FTy = F.getFunctionType();
674 LibFunc::Func TheLibFunc;
675 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
676 return false;
677
678 switch (TheLibFunc) {
679 case LibFunc::strlen:
680 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
681 return false;
682 setOnlyReadsMemory(F);
683 setDoesNotThrow(F);
684 setDoesNotCapture(F, 1);
685 break;
686 case LibFunc::strchr:
687 case LibFunc::strrchr:
688 if (FTy->getNumParams() != 2 ||
689 !FTy->getParamType(0)->isPointerTy() ||
690 !FTy->getParamType(1)->isIntegerTy())
691 return false;
692 setOnlyReadsMemory(F);
693 setDoesNotThrow(F);
694 break;
695 case LibFunc::strcpy:
696 case LibFunc::stpcpy:
697 case LibFunc::strcat:
698 case LibFunc::strtol:
699 case LibFunc::strtod:
700 case LibFunc::strtof:
701 case LibFunc::strtoul:
702 case LibFunc::strtoll:
703 case LibFunc::strtold:
704 case LibFunc::strncat:
705 case LibFunc::strncpy:
706 case LibFunc::stpncpy:
707 case LibFunc::strtoull:
708 if (FTy->getNumParams() < 2 ||
709 !FTy->getParamType(1)->isPointerTy())
710 return false;
711 setDoesNotThrow(F);
712 setDoesNotCapture(F, 2);
713 break;
714 case LibFunc::strxfrm:
715 if (FTy->getNumParams() != 3 ||
716 !FTy->getParamType(0)->isPointerTy() ||
717 !FTy->getParamType(1)->isPointerTy())
718 return false;
719 setDoesNotThrow(F);
720 setDoesNotCapture(F, 1);
721 setDoesNotCapture(F, 2);
722 break;
723 case LibFunc::strcmp:
724 case LibFunc::strspn:
725 case LibFunc::strncmp:
726 case LibFunc::strcspn:
727 case LibFunc::strcoll:
728 case LibFunc::strcasecmp:
729 case LibFunc::strncasecmp:
730 if (FTy->getNumParams() < 2 ||
731 !FTy->getParamType(0)->isPointerTy() ||
732 !FTy->getParamType(1)->isPointerTy())
733 return false;
734 setOnlyReadsMemory(F);
735 setDoesNotThrow(F);
736 setDoesNotCapture(F, 1);
737 setDoesNotCapture(F, 2);
738 break;
739 case LibFunc::strstr:
740 case LibFunc::strpbrk:
741 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
742 return false;
743 setOnlyReadsMemory(F);
744 setDoesNotThrow(F);
745 setDoesNotCapture(F, 2);
746 break;
747 case LibFunc::strtok:
748 case LibFunc::strtok_r:
749 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
750 return false;
751 setDoesNotThrow(F);
752 setDoesNotCapture(F, 2);
753 break;
754 case LibFunc::scanf:
755 case LibFunc::setbuf:
756 case LibFunc::setvbuf:
757 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
758 return false;
759 setDoesNotThrow(F);
760 setDoesNotCapture(F, 1);
761 break;
762 case LibFunc::strdup:
763 case LibFunc::strndup:
764 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
765 !FTy->getParamType(0)->isPointerTy())
766 return false;
767 setDoesNotThrow(F);
768 setDoesNotAlias(F, 0);
769 setDoesNotCapture(F, 1);
770 break;
771 case LibFunc::stat:
772 case LibFunc::sscanf:
773 case LibFunc::sprintf:
774 case LibFunc::statvfs:
775 if (FTy->getNumParams() < 2 ||
776 !FTy->getParamType(0)->isPointerTy() ||
777 !FTy->getParamType(1)->isPointerTy())
778 return false;
779 setDoesNotThrow(F);
780 setDoesNotCapture(F, 1);
781 setDoesNotCapture(F, 2);
782 break;
783 case LibFunc::snprintf:
784 if (FTy->getNumParams() != 3 ||
785 !FTy->getParamType(0)->isPointerTy() ||
786 !FTy->getParamType(2)->isPointerTy())
787 return false;
788 setDoesNotThrow(F);
789 setDoesNotCapture(F, 1);
790 setDoesNotCapture(F, 3);
791 break;
792 case LibFunc::setitimer:
793 if (FTy->getNumParams() != 3 ||
794 !FTy->getParamType(1)->isPointerTy() ||
795 !FTy->getParamType(2)->isPointerTy())
796 return false;
797 setDoesNotThrow(F);
798 setDoesNotCapture(F, 2);
799 setDoesNotCapture(F, 3);
800 break;
801 case LibFunc::system:
802 if (FTy->getNumParams() != 1 ||
803 !FTy->getParamType(0)->isPointerTy())
804 return false;
805 // May throw; "system" is a valid pthread cancellation point.
806 setDoesNotCapture(F, 1);
807 break;
808 case LibFunc::malloc:
809 if (FTy->getNumParams() != 1 ||
810 !FTy->getReturnType()->isPointerTy())
811 return false;
812 setDoesNotThrow(F);
813 setDoesNotAlias(F, 0);
814 break;
815 case LibFunc::memcmp:
816 if (FTy->getNumParams() != 3 ||
817 !FTy->getParamType(0)->isPointerTy() ||
818 !FTy->getParamType(1)->isPointerTy())
819 return false;
820 setOnlyReadsMemory(F);
821 setDoesNotThrow(F);
822 setDoesNotCapture(F, 1);
823 setDoesNotCapture(F, 2);
824 break;
825 case LibFunc::memchr:
826 case LibFunc::memrchr:
827 if (FTy->getNumParams() != 3)
828 return false;
829 setOnlyReadsMemory(F);
830 setDoesNotThrow(F);
831 break;
832 case LibFunc::modf:
833 case LibFunc::modff:
834 case LibFunc::modfl:
835 case LibFunc::memcpy:
836 case LibFunc::memccpy:
837 case LibFunc::memmove:
838 if (FTy->getNumParams() < 2 ||
839 !FTy->getParamType(1)->isPointerTy())
840 return false;
841 setDoesNotThrow(F);
842 setDoesNotCapture(F, 2);
843 break;
844 case LibFunc::memalign:
845 if (!FTy->getReturnType()->isPointerTy())
846 return false;
847 setDoesNotAlias(F, 0);
848 break;
849 case LibFunc::mkdir:
850 case LibFunc::mktime:
851 if (FTy->getNumParams() == 0 ||
852 !FTy->getParamType(0)->isPointerTy())
853 return false;
854 setDoesNotThrow(F);
855 setDoesNotCapture(F, 1);
856 break;
857 case LibFunc::realloc:
858 if (FTy->getNumParams() != 2 ||
859 !FTy->getParamType(0)->isPointerTy() ||
860 !FTy->getReturnType()->isPointerTy())
861 return false;
862 setDoesNotThrow(F);
863 setDoesNotAlias(F, 0);
864 setDoesNotCapture(F, 1);
865 break;
866 case LibFunc::read:
867 if (FTy->getNumParams() != 3 ||
868 !FTy->getParamType(1)->isPointerTy())
869 return false;
870 // May throw; "read" is a valid pthread cancellation point.
871 setDoesNotCapture(F, 2);
872 break;
873 case LibFunc::rmdir:
874 case LibFunc::rewind:
875 case LibFunc::remove:
876 case LibFunc::realpath:
877 if (FTy->getNumParams() < 1 ||
878 !FTy->getParamType(0)->isPointerTy())
879 return false;
880 setDoesNotThrow(F);
881 setDoesNotCapture(F, 1);
882 break;
883 case LibFunc::rename:
884 case LibFunc::readlink:
885 if (FTy->getNumParams() < 2 ||
886 !FTy->getParamType(0)->isPointerTy() ||
887 !FTy->getParamType(1)->isPointerTy())
888 return false;
889 setDoesNotThrow(F);
890 setDoesNotCapture(F, 1);
891 setDoesNotCapture(F, 2);
892 break;
893 case LibFunc::write:
894 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
895 return false;
896 // May throw; "write" is a valid pthread cancellation point.
897 setDoesNotCapture(F, 2);
898 break;
899 case LibFunc::bcopy:
900 if (FTy->getNumParams() != 3 ||
901 !FTy->getParamType(0)->isPointerTy() ||
902 !FTy->getParamType(1)->isPointerTy())
903 return false;
904 setDoesNotThrow(F);
905 setDoesNotCapture(F, 1);
906 setDoesNotCapture(F, 2);
907 break;
908 case LibFunc::bcmp:
909 if (FTy->getNumParams() != 3 ||
910 !FTy->getParamType(0)->isPointerTy() ||
911 !FTy->getParamType(1)->isPointerTy())
912 return false;
913 setDoesNotThrow(F);
914 setOnlyReadsMemory(F);
915 setDoesNotCapture(F, 1);
916 setDoesNotCapture(F, 2);
917 break;
918 case LibFunc::bzero:
919 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
920 return false;
921 setDoesNotThrow(F);
922 setDoesNotCapture(F, 1);
923 break;
924 case LibFunc::calloc:
925 if (FTy->getNumParams() != 2 ||
926 !FTy->getReturnType()->isPointerTy())
927 return false;
928 setDoesNotThrow(F);
929 setDoesNotAlias(F, 0);
930 break;
931 case LibFunc::chmod:
932 case LibFunc::chown:
933 case LibFunc::ctermid:
934 case LibFunc::clearerr:
935 case LibFunc::closedir:
936 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
937 return false;
938 setDoesNotThrow(F);
939 setDoesNotCapture(F, 1);
940 break;
941 case LibFunc::atoi:
942 case LibFunc::atol:
943 case LibFunc::atof:
944 case LibFunc::atoll:
945 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
946 return false;
947 setDoesNotThrow(F);
948 setOnlyReadsMemory(F);
949 setDoesNotCapture(F, 1);
950 break;
951 case LibFunc::access:
952 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
953 return false;
954 setDoesNotThrow(F);
955 setDoesNotCapture(F, 1);
956 break;
957 case LibFunc::fopen:
958 if (FTy->getNumParams() != 2 ||
959 !FTy->getReturnType()->isPointerTy() ||
960 !FTy->getParamType(0)->isPointerTy() ||
961 !FTy->getParamType(1)->isPointerTy())
962 return false;
963 setDoesNotThrow(F);
964 setDoesNotAlias(F, 0);
965 setDoesNotCapture(F, 1);
966 setDoesNotCapture(F, 2);
967 break;
968 case LibFunc::fdopen:
969 if (FTy->getNumParams() != 2 ||
970 !FTy->getReturnType()->isPointerTy() ||
971 !FTy->getParamType(1)->isPointerTy())
972 return false;
973 setDoesNotThrow(F);
974 setDoesNotAlias(F, 0);
975 setDoesNotCapture(F, 2);
976 break;
977 case LibFunc::feof:
978 case LibFunc::free:
979 case LibFunc::fseek:
980 case LibFunc::ftell:
981 case LibFunc::fgetc:
982 case LibFunc::fseeko:
983 case LibFunc::ftello:
984 case LibFunc::fileno:
985 case LibFunc::fflush:
986 case LibFunc::fclose:
987 case LibFunc::fsetpos:
988 case LibFunc::flockfile:
989 case LibFunc::funlockfile:
990 case LibFunc::ftrylockfile:
991 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
992 return false;
993 setDoesNotThrow(F);
994 setDoesNotCapture(F, 1);
995 break;
996 case LibFunc::ferror:
997 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
998 return false;
999 setDoesNotThrow(F);
1000 setDoesNotCapture(F, 1);
1001 setOnlyReadsMemory(F);
1002 break;
1003 case LibFunc::fputc:
1004 case LibFunc::fstat:
1005 case LibFunc::frexp:
1006 case LibFunc::frexpf:
1007 case LibFunc::frexpl:
1008 case LibFunc::fstatvfs:
1009 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1010 return false;
1011 setDoesNotThrow(F);
1012 setDoesNotCapture(F, 2);
1013 break;
1014 case LibFunc::fgets:
1015 if (FTy->getNumParams() != 3 ||
1016 !FTy->getParamType(0)->isPointerTy() ||
1017 !FTy->getParamType(2)->isPointerTy())
1018 return false;
1019 setDoesNotThrow(F);
1020 setDoesNotCapture(F, 3);
1021 case LibFunc::fread:
1022 case LibFunc::fwrite:
1023 if (FTy->getNumParams() != 4 ||
1024 !FTy->getParamType(0)->isPointerTy() ||
1025 !FTy->getParamType(3)->isPointerTy())
1026 return false;
1027 setDoesNotThrow(F);
1028 setDoesNotCapture(F, 1);
1029 setDoesNotCapture(F, 4);
1030 case LibFunc::fputs:
1031 case LibFunc::fscanf:
1032 case LibFunc::fprintf:
1033 case LibFunc::fgetpos:
1034 if (FTy->getNumParams() < 2 ||
1035 !FTy->getParamType(0)->isPointerTy() ||
1036 !FTy->getParamType(1)->isPointerTy())
1037 return false;
1038 setDoesNotThrow(F);
1039 setDoesNotCapture(F, 1);
1040 setDoesNotCapture(F, 2);
1041 break;
1042 case LibFunc::getc:
1043 case LibFunc::getlogin_r:
1044 case LibFunc::getc_unlocked:
1045 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1046 return false;
1047 setDoesNotThrow(F);
1048 setDoesNotCapture(F, 1);
1049 break;
1050 case LibFunc::getenv:
1051 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1052 return false;
1053 setDoesNotThrow(F);
1054 setOnlyReadsMemory(F);
1055 setDoesNotCapture(F, 1);
1056 break;
1057 case LibFunc::gets:
1058 case LibFunc::getchar:
1059 setDoesNotThrow(F);
1060 break;
1061 case LibFunc::getitimer:
1062 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1063 return false;
1064 setDoesNotThrow(F);
1065 setDoesNotCapture(F, 2);
1066 break;
1067 case LibFunc::getpwnam:
1068 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1069 return false;
1070 setDoesNotThrow(F);
1071 setDoesNotCapture(F, 1);
1072 break;
1073 case LibFunc::ungetc:
1074 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1075 return false;
1076 setDoesNotThrow(F);
1077 setDoesNotCapture(F, 2);
1078 break;
1079 case LibFunc::uname:
1080 case LibFunc::unlink:
1081 case LibFunc::unsetenv:
1082 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1083 return false;
1084 setDoesNotThrow(F);
1085 setDoesNotCapture(F, 1);
1086 break;
1087 case LibFunc::utime:
1088 case LibFunc::utimes:
1089 if (FTy->getNumParams() != 2 ||
1090 !FTy->getParamType(0)->isPointerTy() ||
1091 !FTy->getParamType(1)->isPointerTy())
1092 return false;
1093 setDoesNotThrow(F);
1094 setDoesNotCapture(F, 1);
1095 setDoesNotCapture(F, 2);
1096 break;
1097 case LibFunc::putc:
1098 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1099 return false;
1100 setDoesNotThrow(F);
1101 setDoesNotCapture(F, 2);
1102 break;
1103 case LibFunc::puts:
1104 case LibFunc::printf:
1105 case LibFunc::perror:
1106 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1107 return false;
1108 setDoesNotThrow(F);
1109 setDoesNotCapture(F, 1);
1110 break;
1111 case LibFunc::pread:
1112 case LibFunc::pwrite:
1113 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1114 return false;
1115 // May throw; these are valid pthread cancellation points.
1116 setDoesNotCapture(F, 2);
1117 break;
1118 case LibFunc::putchar:
1119 setDoesNotThrow(F);
1120 break;
1121 case LibFunc::popen:
1122 if (FTy->getNumParams() != 2 ||
1123 !FTy->getReturnType()->isPointerTy() ||
1124 !FTy->getParamType(0)->isPointerTy() ||
1125 !FTy->getParamType(1)->isPointerTy())
1126 return false;
1127 setDoesNotThrow(F);
1128 setDoesNotAlias(F, 0);
1129 setDoesNotCapture(F, 1);
1130 setDoesNotCapture(F, 2);
1131 break;
1132 case LibFunc::pclose:
1133 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1134 return false;
1135 setDoesNotThrow(F);
1136 setDoesNotCapture(F, 1);
1137 break;
1138 case LibFunc::vscanf:
1139 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1140 return false;
1141 setDoesNotThrow(F);
1142 setDoesNotCapture(F, 1);
1143 break;
1144 case LibFunc::vsscanf:
1145 case LibFunc::vfscanf:
1146 if (FTy->getNumParams() != 3 ||
1147 !FTy->getParamType(1)->isPointerTy() ||
1148 !FTy->getParamType(2)->isPointerTy())
1149 return false;
1150 setDoesNotThrow(F);
1151 setDoesNotCapture(F, 1);
1152 setDoesNotCapture(F, 2);
1153 break;
1154 case LibFunc::valloc:
1155 if (!FTy->getReturnType()->isPointerTy())
1156 return false;
1157 setDoesNotThrow(F);
1158 setDoesNotAlias(F, 0);
1159 break;
1160 case LibFunc::vprintf:
1161 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1162 return false;
1163 setDoesNotThrow(F);
1164 setDoesNotCapture(F, 1);
1165 break;
1166 case LibFunc::vfprintf:
1167 case LibFunc::vsprintf:
1168 if (FTy->getNumParams() != 3 ||
1169 !FTy->getParamType(0)->isPointerTy() ||
1170 !FTy->getParamType(1)->isPointerTy())
1171 return false;
1172 setDoesNotThrow(F);
1173 setDoesNotCapture(F, 1);
1174 setDoesNotCapture(F, 2);
1175 break;
1176 case LibFunc::vsnprintf:
1177 if (FTy->getNumParams() != 4 ||
1178 !FTy->getParamType(0)->isPointerTy() ||
1179 !FTy->getParamType(2)->isPointerTy())
1180 return false;
1181 setDoesNotThrow(F);
1182 setDoesNotCapture(F, 1);
1183 setDoesNotCapture(F, 3);
1184 break;
1185 case LibFunc::open:
1186 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1187 return false;
1188 // May throw; "open" is a valid pthread cancellation point.
1189 setDoesNotCapture(F, 1);
1190 break;
1191 case LibFunc::opendir:
1192 if (FTy->getNumParams() != 1 ||
1193 !FTy->getReturnType()->isPointerTy() ||
1194 !FTy->getParamType(0)->isPointerTy())
1195 return false;
1196 setDoesNotThrow(F);
1197 setDoesNotAlias(F, 0);
1198 setDoesNotCapture(F, 1);
1199 break;
1200 case LibFunc::tmpfile:
1201 if (!FTy->getReturnType()->isPointerTy())
1202 return false;
1203 setDoesNotThrow(F);
1204 setDoesNotAlias(F, 0);
1205 break;
1206 case LibFunc::times:
1207 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1208 return false;
1209 setDoesNotThrow(F);
1210 setDoesNotCapture(F, 1);
1211 break;
1212 case LibFunc::htonl:
1213 case LibFunc::htons:
1214 case LibFunc::ntohl:
1215 case LibFunc::ntohs:
1216 setDoesNotThrow(F);
1217 setDoesNotAccessMemory(F);
1218 break;
1219 case LibFunc::lstat:
1220 if (FTy->getNumParams() != 2 ||
1221 !FTy->getParamType(0)->isPointerTy() ||
1222 !FTy->getParamType(1)->isPointerTy())
1223 return false;
1224 setDoesNotThrow(F);
1225 setDoesNotCapture(F, 1);
1226 setDoesNotCapture(F, 2);
1227 break;
1228 case LibFunc::lchown:
1229 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1230 return false;
1231 setDoesNotThrow(F);
1232 setDoesNotCapture(F, 1);
1233 break;
1234 case LibFunc::qsort:
1235 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1236 return false;
1237 // May throw; places call through function pointer.
1238 setDoesNotCapture(F, 4);
1239 break;
1240 case LibFunc::dunder_strdup:
1241 case LibFunc::dunder_strndup:
1242 if (FTy->getNumParams() < 1 ||
1243 !FTy->getReturnType()->isPointerTy() ||
1244 !FTy->getParamType(0)->isPointerTy())
1245 return false;
1246 setDoesNotThrow(F);
1247 setDoesNotAlias(F, 0);
1248 setDoesNotCapture(F, 1);
1249 break;
1250 case LibFunc::dunder_strtok_r:
1251 if (FTy->getNumParams() != 3 ||
1252 !FTy->getParamType(1)->isPointerTy())
1253 return false;
1254 setDoesNotThrow(F);
1255 setDoesNotCapture(F, 2);
1256 break;
1257 case LibFunc::under_IO_getc:
1258 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1259 return false;
1260 setDoesNotThrow(F);
1261 setDoesNotCapture(F, 1);
1262 break;
1263 case LibFunc::under_IO_putc:
1264 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1265 return false;
1266 setDoesNotThrow(F);
1267 setDoesNotCapture(F, 2);
1268 break;
1269 case LibFunc::dunder_isoc99_scanf:
1270 if (FTy->getNumParams() < 1 ||
1271 !FTy->getParamType(0)->isPointerTy())
1272 return false;
1273 setDoesNotThrow(F);
1274 setDoesNotCapture(F, 1);
1275 break;
1276 case LibFunc::stat64:
1277 case LibFunc::lstat64:
1278 case LibFunc::statvfs64:
1279 case LibFunc::dunder_isoc99_sscanf:
1280 if (FTy->getNumParams() < 1 ||
1281 !FTy->getParamType(0)->isPointerTy() ||
1282 !FTy->getParamType(1)->isPointerTy())
1283 return false;
1284 setDoesNotThrow(F);
1285 setDoesNotCapture(F, 1);
1286 setDoesNotCapture(F, 2);
1287 break;
1288 case LibFunc::fopen64:
1289 if (FTy->getNumParams() != 2 ||
1290 !FTy->getReturnType()->isPointerTy() ||
1291 !FTy->getParamType(0)->isPointerTy() ||
1292 !FTy->getParamType(1)->isPointerTy())
1293 return false;
1294 setDoesNotThrow(F);
1295 setDoesNotAlias(F, 0);
1296 setDoesNotCapture(F, 1);
1297 setDoesNotCapture(F, 2);
1298 break;
1299 case LibFunc::fseeko64:
1300 case LibFunc::ftello64:
1301 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1302 return false;
1303 setDoesNotThrow(F);
1304 setDoesNotCapture(F, 1);
1305 break;
1306 case LibFunc::tmpfile64:
1307 if (!FTy->getReturnType()->isPointerTy())
1308 return false;
1309 setDoesNotThrow(F);
1310 setDoesNotAlias(F, 0);
1311 break;
1312 case LibFunc::fstat64:
1313 case LibFunc::fstatvfs64:
1314 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1315 return false;
1316 setDoesNotThrow(F);
1317 setDoesNotCapture(F, 2);
1318 break;
1319 case LibFunc::open64:
1320 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1321 return false;
1322 // May throw; "open" is a valid pthread cancellation point.
1323 setDoesNotCapture(F, 1);
1324 break;
1325 default:
1326 // Didn't mark any attributes.
1327 return false;
1328 }
1329
1330 return true;
1331}
1332
1333/// annotateLibraryCalls - Adds attributes to well-known standard library
1334/// call declarations.
1335bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1336 bool MadeChange = false;
1337
1338 // Check each function in turn annotating well-known library function
1339 // declarations with attributes.
1340 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1341 Function *F = (*I)->getFunction();
1342
1343 if (F != 0 && F->isDeclaration())
1344 MadeChange |= inferPrototypeAttributes(*F);
1345 }
1346
1347 return MadeChange;
1348}
1349
Chris Lattner2decb222010-04-16 22:42:17 +00001350bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman3c97f7a2010-11-08 16:10:15 +00001351 AA = &getAnalysis<AliasAnalysis>();
Meador Ingecf47ce62013-03-21 00:55:59 +00001352 TLI = &getAnalysis<TargetLibraryInfo>();
Dan Gohman3c97f7a2010-11-08 16:10:15 +00001353
Meador Ingecf47ce62013-03-21 00:55:59 +00001354 bool Changed = annotateLibraryCalls(SCC);
1355 Changed |= AddReadAttrs(SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +00001356 Changed |= AddNoCaptureAttrs(SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +00001357 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +00001358 return Changed;
1359}