blob: 20d168056caefddd2c857cfb9c99bdbc82d242ed [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
Nick Lewyckydc897372013-07-06 00:29:58 +000012// non-local memory, and marking them readnone/readonly. It does the
13// same with function arguments independently, marking them readonly/
14// readnone/nocapture. Finally, well-known library call declarations
15// are marked with all attributes that are consistent with the
16// function's standard definition. This pass is implemented as a
17// 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"
Nick Lewyckyb48a1892011-12-28 23:24:21 +000023#include "llvm/ADT/SCCIterator.h"
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +000024#include "llvm/ADT/SetVector.h"
Duncan Sands338cd6b2009-01-02 11:54:37 +000025#include "llvm/ADT/SmallSet.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000026#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000027#include "llvm/Analysis/AliasAnalysis.h"
28#include "llvm/Analysis/CallGraph.h"
Chandler Carruth3251e812013-01-07 15:26:48 +000029#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000030#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000031#include "llvm/IR/GlobalVariable.h"
32#include "llvm/IR/IntrinsicInst.h"
33#include "llvm/IR/LLVMContext.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000034#include "llvm/Support/InstIterator.h"
Meador Ingecf47ce62013-03-21 00:55:59 +000035#include "llvm/Target/TargetLibraryInfo.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000036using namespace llvm;
37
38STATISTIC(NumReadNone, "Number of functions marked readnone");
39STATISTIC(NumReadOnly, "Number of functions marked readonly");
40STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewyckydc897372013-07-06 00:29:58 +000041STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
42STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
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
Nick Lewyckydc897372013-07-06 00:29:58 +000059 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
60 bool AddArgumentAttrs(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()) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000074 F.setDoesNotAccessMemory();
75 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000076 }
77 }
78
79 void setOnlyReadsMemory(Function &F) {
80 if (!F.onlyReadsMemory()) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000081 F.setOnlyReadsMemory();
82 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000083 }
84 }
85
86 void setDoesNotThrow(Function &F) {
87 if (!F.doesNotThrow()) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000088 F.setDoesNotThrow();
89 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000090 }
91 }
92
93 void setDoesNotCapture(Function &F, unsigned n) {
94 if (!F.doesNotCapture(n)) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000095 F.setDoesNotCapture(n);
96 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000097 }
98 }
99
Nick Lewyckydc897372013-07-06 00:29:58 +0000100 void setOnlyReadsMemory(Function &F, unsigned n) {
101 if (!F.onlyReadsMemory(n)) {
102 F.setOnlyReadsMemory(n);
103 ++NumAnnotated;
104 }
105 }
106
Meador Ingecf47ce62013-03-21 00:55:59 +0000107 void setDoesNotAlias(Function &F, unsigned n) {
108 if (!F.doesNotAlias(n)) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +0000109 F.setDoesNotAlias(n);
110 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +0000111 }
112 }
113
114 // inferPrototypeAttributes - Analyze the name and prototype of the
115 // given function and set any applicable attributes. Returns true
116 // if any attributes were set and false otherwise.
117 bool inferPrototypeAttributes(Function &F);
118
119 // annotateLibraryCalls - Adds attributes to well-known standard library
120 // call declarations.
121 bool annotateLibraryCalls(const CallGraphSCC &SCC);
122
Duncan Sands9e89ba32008-12-31 16:14:43 +0000123 virtual void getAnalysisUsage(AnalysisUsage &AU) const {
124 AU.setPreservesCFG();
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000125 AU.addRequired<AliasAnalysis>();
Meador Ingecf47ce62013-03-21 00:55:59 +0000126 AU.addRequired<TargetLibraryInfo>();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000127 CallGraphSCCPass::getAnalysisUsage(AU);
128 }
129
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000130 private:
131 AliasAnalysis *AA;
Meador Ingecf47ce62013-03-21 00:55:59 +0000132 TargetLibraryInfo *TLI;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000133 };
134}
135
136char FunctionAttrs::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +0000137INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
138 "Deduce function attributes", false, false)
Nick Lewyckya83aeae2013-09-05 08:19:58 +0000139INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Owen Andersonae0a7bc2010-10-13 22:00:45 +0000140INITIALIZE_AG_DEPENDENCY(CallGraph)
Meador Ingecf47ce62013-03-21 00:55:59 +0000141INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Owen Andersonae0a7bc2010-10-13 22:00:45 +0000142INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersonce665bd2010-10-07 22:25:06 +0000143 "Deduce function attributes", false, false)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000144
145Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
146
147
Duncan Sands9e89ba32008-12-31 16:14:43 +0000148/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000149bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000150 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000151
152 // Fill SCCNodes with the elements of the SCC. Used for quickly
153 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000154 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
155 SCCNodes.insert((*I)->getFunction());
Duncan Sands9e89ba32008-12-31 16:14:43 +0000156
157 // Check if any of the functions in the SCC read or write memory. If they
158 // write memory then they can't be marked readnone or readonly.
159 bool ReadsMemory = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000160 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
161 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000162
163 if (F == 0)
164 // External node - may write memory. Just give up.
165 return false;
166
Dan Gohman6d44d642010-11-09 20:13:27 +0000167 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
168 if (MRB == AliasAnalysis::DoesNotAccessMemory)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000169 // Already perfect!
170 continue;
171
172 // Definitions with weak linkage may be overridden at linktime with
173 // something that writes memory, so treat them like declarations.
174 if (F->isDeclaration() || F->mayBeOverridden()) {
Dan Gohman6d44d642010-11-09 20:13:27 +0000175 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000176 // May write memory. Just give up.
177 return false;
178
179 ReadsMemory = true;
180 continue;
181 }
182
183 // Scan the function body for instructions that may read or write memory.
184 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
185 Instruction *I = &*II;
186
187 // Some instructions can be ignored even if they read or write memory.
188 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif7d3056b2010-07-28 22:50:26 +0000189 CallSite CS(cast<Value>(I));
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000190 if (CS) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000191 // Ignore calls to functions in the same SCC.
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000192 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000193 continue;
Dan Gohman42c31a72010-11-10 01:02:18 +0000194 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
195 // If the call doesn't access arbitrary memory, we may be able to
196 // figure out something.
Dan Gohman432d08c2010-11-10 17:34:04 +0000197 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
198 // If the call does access argument pointees, check each argument.
Dan Gohman68a60562010-11-10 18:17:28 +0000199 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman42c31a72010-11-10 01:02:18 +0000200 // Check whether all pointer arguments point to local memory, and
201 // ignore calls that only access local memory.
202 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
203 CI != CE; ++CI) {
204 Value *Arg = *CI;
205 if (Arg->getType()->isPointerTy()) {
206 AliasAnalysis::Location Loc(Arg,
207 AliasAnalysis::UnknownSize,
208 I->getMetadata(LLVMContext::MD_tbaa));
209 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
210 if (MRB & AliasAnalysis::Mod)
211 // Writes non-local memory. Give up.
212 return false;
213 if (MRB & AliasAnalysis::Ref)
214 // Ok, it reads non-local memory.
215 ReadsMemory = true;
216 }
Dan Gohman40b6a192010-11-09 19:56:27 +0000217 }
218 }
Dan Gohman40b6a192010-11-09 19:56:27 +0000219 continue;
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000220 }
Dan Gohman42c31a72010-11-10 01:02:18 +0000221 // The call could access any memory. If that includes writes, give up.
222 if (MRB & AliasAnalysis::Mod)
223 return false;
224 // If it reads, note it.
225 if (MRB & AliasAnalysis::Ref)
226 ReadsMemory = true;
227 continue;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000228 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedman2199dfb2011-08-16 01:28:22 +0000229 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
230 if (!LI->isVolatile()) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000231 AliasAnalysis::Location Loc = AA->getLocation(LI);
Dan Gohmanea8900f2010-11-08 17:12:04 +0000232 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
233 continue;
234 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000235 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedman2199dfb2011-08-16 01:28:22 +0000236 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
237 if (!SI->isVolatile()) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000238 AliasAnalysis::Location Loc = AA->getLocation(SI);
Dan Gohmanea8900f2010-11-08 17:12:04 +0000239 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
240 continue;
241 }
Dan Gohman4cf0dcf2010-11-09 20:17:38 +0000242 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
243 // Ignore vaargs on local memory.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000244 AliasAnalysis::Location Loc = AA->getLocation(VI);
Dan Gohman4cf0dcf2010-11-09 20:17:38 +0000245 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
246 continue;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000247 }
248
249 // Any remaining instructions need to be taken seriously! Check if they
250 // read or write memory.
251 if (I->mayWriteToMemory())
252 // Writes memory. Just give up.
253 return false;
Duncan Sandscfd0ebe2009-05-06 08:42:00 +0000254
Duncan Sands9e89ba32008-12-31 16:14:43 +0000255 // If this instruction may read memory, remember that.
256 ReadsMemory |= I->mayReadFromMemory();
257 }
258 }
259
260 // Success! Functions in this SCC do not access memory, or only read memory.
261 // Give them the appropriate attribute.
262 bool MadeChange = false;
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->doesNotAccessMemory())
267 // Already perfect!
268 continue;
269
270 if (F->onlyReadsMemory() && ReadsMemory)
271 // No change.
272 continue;
273
274 MadeChange = true;
275
276 // Clear out any existing attributes.
Bill Wendling702cc912012-10-15 20:35:56 +0000277 AttrBuilder B;
Bill Wendling034b94b2012-12-19 07:18:57 +0000278 B.addAttribute(Attribute::ReadOnly)
279 .addAttribute(Attribute::ReadNone);
Bill Wendling8246df62013-01-23 00:45:55 +0000280 F->removeAttributes(AttributeSet::FunctionIndex,
281 AttributeSet::get(F->getContext(),
282 AttributeSet::FunctionIndex, B));
Duncan Sands9e89ba32008-12-31 16:14:43 +0000283
284 // Add in the new attribute.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000285 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendling70d2ca02013-01-23 00:20:53 +0000286 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands9e89ba32008-12-31 16:14:43 +0000287
288 if (ReadsMemory)
Duncan Sandsb2f22792009-01-02 11:46:24 +0000289 ++NumReadOnly;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000290 else
Duncan Sandsb2f22792009-01-02 11:46:24 +0000291 ++NumReadNone;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000292 }
293
294 return MadeChange;
295}
296
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000297namespace {
298 // For a given pointer Argument, this retains a list of Arguments of functions
299 // in the same SCC that the pointer data flows into. We use this to build an
300 // SCC of the arguments.
301 struct ArgumentGraphNode {
302 Argument *Definition;
303 SmallVector<ArgumentGraphNode*, 4> Uses;
304 };
305
306 class ArgumentGraph {
307 // We store pointers to ArgumentGraphNode objects, so it's important that
308 // that they not move around upon insert.
309 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
310
311 ArgumentMapTy ArgumentMap;
312
313 // There is no root node for the argument graph, in fact:
314 // void f(int *x, int *y) { if (...) f(x, y); }
315 // is an example where the graph is disconnected. The SCCIterator requires a
316 // single entry point, so we maintain a fake ("synthetic") root node that
317 // uses every node. Because the graph is directed and nothing points into
318 // the root, it will not participate in any SCCs (except for its own).
319 ArgumentGraphNode SyntheticRoot;
320
321 public:
322 ArgumentGraph() { SyntheticRoot.Definition = 0; }
323
324 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
325
326 iterator begin() { return SyntheticRoot.Uses.begin(); }
327 iterator end() { return SyntheticRoot.Uses.end(); }
328 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
329
330 ArgumentGraphNode *operator[](Argument *A) {
331 ArgumentGraphNode &Node = ArgumentMap[A];
332 Node.Definition = A;
333 SyntheticRoot.Uses.push_back(&Node);
334 return &Node;
335 }
336 };
337
338 // This tracker checks whether callees are in the SCC, and if so it does not
339 // consider that a capture, instead adding it to the "Uses" list and
340 // continuing with the analysis.
341 struct ArgumentUsesTracker : public CaptureTracker {
342 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
343 : Captured(false), SCCNodes(SCCNodes) {}
344
345 void tooManyUses() { Captured = true; }
346
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000347 bool captured(Use *U) {
348 CallSite CS(U->getUser());
349 if (!CS.getInstruction()) { Captured = true; return true; }
350
351 Function *F = CS.getCalledFunction();
352 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
353
Nick Lewyckydc897372013-07-06 00:29:58 +0000354 bool Found = false;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000355 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
356 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
357 PI != PE; ++PI, ++AI) {
358 if (AI == AE) {
359 assert(F->isVarArg() && "More params than args in non-varargs call");
360 Captured = true;
361 return true;
362 }
363 if (PI == U) {
364 Uses.push_back(AI);
Nick Lewyckydc897372013-07-06 00:29:58 +0000365 Found = true;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000366 break;
367 }
368 }
Nick Lewyckydc897372013-07-06 00:29:58 +0000369 assert(Found && "Capturing call-site captured nothing?");
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000370 return false;
371 }
372
373 bool Captured; // True only if certainly captured (used outside our SCC).
374 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
375
376 const SmallPtrSet<Function*, 8> &SCCNodes;
377 };
378}
379
380namespace llvm {
381 template<> struct GraphTraits<ArgumentGraphNode*> {
382 typedef ArgumentGraphNode NodeType;
383 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
384
385 static inline NodeType *getEntryNode(NodeType *A) { return A; }
386 static inline ChildIteratorType child_begin(NodeType *N) {
387 return N->Uses.begin();
388 }
389 static inline ChildIteratorType child_end(NodeType *N) {
390 return N->Uses.end();
391 }
392 };
393 template<> struct GraphTraits<ArgumentGraph*>
394 : public GraphTraits<ArgumentGraphNode*> {
395 static NodeType *getEntryNode(ArgumentGraph *AG) {
396 return AG->getEntryNode();
397 }
398 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
399 return AG->begin();
400 }
401 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
402 return AG->end();
403 }
404 };
405}
406
Nick Lewyckydc897372013-07-06 00:29:58 +0000407// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
408static Attribute::AttrKind
409determinePointerReadAttrs(Argument *A,
410 const SmallPtrSet<Argument*, 8> &SCCNodes) {
411
412 SmallVector<Use*, 32> Worklist;
413 SmallSet<Use*, 32> Visited;
414 int Count = 0;
415
416 bool IsRead = false;
417 // We don't need to track IsWritten. If A is written to, return immediately.
418
419 for (Value::use_iterator UI = A->use_begin(), UE = A->use_end();
420 UI != UE; ++UI) {
421 if (Count++ >= 20)
422 return Attribute::None;
423
424 Use *U = &UI.getUse();
425 Visited.insert(U);
426 Worklist.push_back(U);
427 }
428
429 while (!Worklist.empty()) {
430 Use *U = Worklist.pop_back_val();
431 Instruction *I = cast<Instruction>(U->getUser());
432 Value *V = U->get();
433
434 switch (I->getOpcode()) {
435 case Instruction::BitCast:
436 case Instruction::GetElementPtr:
437 case Instruction::PHI:
438 case Instruction::Select:
439 // The original value is not read/written via this if the new value isn't.
440 for (Instruction::use_iterator UI = I->use_begin(), UE = I->use_end();
441 UI != UE; ++UI) {
442 Use *U = &UI.getUse();
443 if (Visited.insert(U))
444 Worklist.push_back(U);
445 }
446 break;
447
448 case Instruction::Call:
449 case Instruction::Invoke: {
450 CallSite CS(I);
451 if (CS.doesNotAccessMemory())
452 continue;
453
454 Function *F = CS.getCalledFunction();
455 if (!F) {
456 if (CS.onlyReadsMemory()) {
457 IsRead = true;
458 continue;
459 }
460 return Attribute::None;
461 }
462
463 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
464 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
465 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
466 if (A->get() == V) {
467 if (AI == AE) {
468 assert(F->isVarArg() &&
469 "More params than args in non-varargs call.");
470 return Attribute::None;
471 }
472 if (SCCNodes.count(AI))
473 continue;
474 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
475 return Attribute::None;
476 if (!CS.doesNotAccessMemory(A - B))
477 IsRead = true;
478 }
479 }
480 break;
481 }
482
483 case Instruction::Load:
484 IsRead = true;
485 break;
486
487 case Instruction::ICmp:
488 case Instruction::Ret:
489 break;
490
491 default:
492 return Attribute::None;
493 }
494 }
495
496 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
497}
498
499/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
500bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000501 bool Changed = false;
502
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000503 SmallPtrSet<Function*, 8> SCCNodes;
504
505 // Fill SCCNodes with the elements of the SCC. Used for quickly
506 // looking up whether a given CallGraphNode is in this SCC.
507 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
508 Function *F = (*I)->getFunction();
509 if (F && !F->isDeclaration() && !F->mayBeOverridden())
510 SCCNodes.insert(F);
511 }
512
513 ArgumentGraph AG;
514
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000515 AttrBuilder B;
516 B.addAttribute(Attribute::NoCapture);
517
Duncan Sands9e89ba32008-12-31 16:14:43 +0000518 // Check each function in turn, determining which pointer arguments are not
519 // captured.
Chris Lattner2decb222010-04-16 22:42:17 +0000520 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
521 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000522
523 if (F == 0)
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000524 // External node - only a problem for arguments that we pass to it.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000525 continue;
526
527 // Definitions with weak linkage may be overridden at linktime with
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000528 // something that captures pointers, so treat them like declarations.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000529 if (F->isDeclaration() || F->mayBeOverridden())
530 continue;
531
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000532 // Functions that are readonly (or readnone) and nounwind and don't return
533 // a value can't capture arguments. Don't analyze them.
534 if (F->onlyReadsMemory() && F->doesNotThrow() &&
535 F->getReturnType()->isVoidTy()) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000536 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
537 A != E; ++A) {
538 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
539 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
540 ++NumNoCapture;
541 Changed = true;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000542 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000543 }
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000544 continue;
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000545 }
546
Nick Lewyckydc897372013-07-06 00:29:58 +0000547 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
548 A != E; ++A) {
549 if (!A->getType()->isPointerTy()) continue;
550 bool HasNonLocalUses = false;
551 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000552 ArgumentUsesTracker Tracker(SCCNodes);
553 PointerMayBeCaptured(A, &Tracker);
554 if (!Tracker.Captured) {
555 if (Tracker.Uses.empty()) {
556 // If it's trivially not captured, mark it nocapture now.
557 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
558 ++NumNoCapture;
559 Changed = true;
560 } else {
561 // If it's not trivially captured and not trivially not captured,
562 // then it must be calling into another function in our SCC. Save
563 // its particulars for Argument-SCC analysis later.
564 ArgumentGraphNode *Node = AG[A];
565 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckydc897372013-07-06 00:29:58 +0000566 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000567 Node->Uses.push_back(AG[*UI]);
Nick Lewyckydc897372013-07-06 00:29:58 +0000568 if (*UI != A)
569 HasNonLocalUses = true;
570 }
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000571 }
572 }
573 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
574 }
Nick Lewyckydc897372013-07-06 00:29:58 +0000575 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
576 // Can we determine that it's readonly/readnone without doing an SCC?
577 // Note that we don't allow any calls at all here, or else our result
578 // will be dependent on the iteration order through the functions in the
579 // SCC.
580 SmallPtrSet<Argument*, 8> Self;
581 Self.insert(A);
582 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
583 if (R != Attribute::None) {
584 AttrBuilder B;
585 B.addAttribute(R);
586 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
587 Changed = true;
588 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
589 }
590 }
591 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000592 }
593
594 // The graph we've collected is partial because we stopped scanning for
595 // argument uses once we solved the argument trivially. These partial nodes
596 // show up as ArgumentGraphNode objects with an empty Uses list, and for
597 // these nodes the final decision about whether they capture has already been
598 // made. If the definition doesn't have a 'nocapture' attribute by now, it
599 // captures.
600
601 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG), E = scc_end(&AG);
602 I != E; ++I) {
603 std::vector<ArgumentGraphNode*> &ArgumentSCC = *I;
604 if (ArgumentSCC.size() == 1) {
605 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
606
607 // eg. "void f(int* x) { if (...) f(x); }"
608 if (ArgumentSCC[0]->Uses.size() == 1 &&
609 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000610 Argument *A = ArgumentSCC[0]->Definition;
611 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky6b056862009-01-02 03:46:56 +0000612 ++NumNoCapture;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000613 Changed = true;
614 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000615 continue;
616 }
617
618 bool SCCCaptured = false;
619 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
620 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
621 ArgumentGraphNode *Node = *I;
622 if (Node->Uses.empty()) {
623 if (!Node->Definition->hasNoCaptureAttr())
624 SCCCaptured = true;
625 }
626 }
627 if (SCCCaptured) continue;
628
629 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
630 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
631 // quickly looking up whether a given Argument is in this ArgumentSCC.
632 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
633 E = ArgumentSCC.end(); I != E; ++I) {
634 ArgumentSCCNodes.insert((*I)->Definition);
635 }
636
637 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
638 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
639 ArgumentGraphNode *N = *I;
640 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
641 UE = N->Uses.end(); UI != UE; ++UI) {
642 Argument *A = (*UI)->Definition;
643 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
644 continue;
645 SCCCaptured = true;
646 break;
647 }
648 }
649 if (SCCCaptured) continue;
650
Nick Lewycky720ac912012-01-05 22:21:45 +0000651 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000652 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000653 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000654 ++NumNoCapture;
655 Changed = true;
656 }
Nick Lewyckydc897372013-07-06 00:29:58 +0000657
658 // We also want to compute readonly/readnone. With a small number of false
659 // negatives, we can assume that any pointer which is captured isn't going
660 // to be provably readonly or readnone, since by definition we can't
661 // analyze all uses of a captured pointer.
662 //
663 // The false negatives happen when the pointer is captured by a function
664 // that promises readonly/readnone behaviour on the pointer, then the
665 // pointer's lifetime ends before anything that writes to arbitrary memory.
666 // Also, a readonly/readnone pointer may be returned, but returning a
667 // pointer is capturing it.
668
669 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
670 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
671 Argument *A = ArgumentSCC[i]->Definition;
672 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
673 if (K == Attribute::ReadNone)
674 continue;
675 if (K == Attribute::ReadOnly) {
676 ReadAttr = Attribute::ReadOnly;
677 continue;
678 }
679 ReadAttr = K;
680 break;
681 }
682
683 if (ReadAttr != Attribute::None) {
684 AttrBuilder B;
685 B.addAttribute(ReadAttr);
686 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
687 Argument *A = ArgumentSCC[i]->Definition;
688 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
689 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
690 Changed = true;
691 }
692 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000693 }
694
695 return Changed;
696}
697
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000698/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000699/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000700bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +0000701 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000702 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000703 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
704 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
705 FlowsToReturn.insert(Ret->getReturnValue());
706
707 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +0000708 Value *RetVal = FlowsToReturn[i];
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000709
710 if (Constant *C = dyn_cast<Constant>(RetVal)) {
711 if (!C->isNullValue() && !isa<UndefValue>(C))
712 return false;
713
714 continue;
715 }
716
717 if (isa<Argument>(RetVal))
718 return false;
719
720 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
721 switch (RVI->getOpcode()) {
722 // Extend the analysis by looking upwards.
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000723 case Instruction::BitCast:
Victor Hernandez83d63912009-09-18 22:35:49 +0000724 case Instruction::GetElementPtr:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000725 FlowsToReturn.insert(RVI->getOperand(0));
726 continue;
727 case Instruction::Select: {
728 SelectInst *SI = cast<SelectInst>(RVI);
729 FlowsToReturn.insert(SI->getTrueValue());
730 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner439044f2009-09-27 21:29:28 +0000731 continue;
732 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000733 case Instruction::PHI: {
734 PHINode *PN = cast<PHINode>(RVI);
735 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
736 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner439044f2009-09-27 21:29:28 +0000737 continue;
738 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000739
740 // Check whether the pointer came from an allocation.
741 case Instruction::Alloca:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000742 break;
743 case Instruction::Call:
744 case Instruction::Invoke: {
745 CallSite CS(RVI);
Bill Wendling034b94b2012-12-19 07:18:57 +0000746 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000747 break;
748 if (CS.getCalledFunction() &&
Chris Lattner98a27ce2009-08-31 04:09:04 +0000749 SCCNodes.count(CS.getCalledFunction()))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000750 break;
751 } // fall-through
752 default:
753 return false; // Did not come from an allocation.
754 }
755
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000756 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000757 return false;
758 }
759
760 return true;
761}
762
763/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000764bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000765 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000766
767 // Fill SCCNodes with the elements of the SCC. Used for quickly
768 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000769 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
770 SCCNodes.insert((*I)->getFunction());
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000771
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000772 // Check each function in turn, determining which functions return noalias
773 // pointers.
Chris Lattner2decb222010-04-16 22:42:17 +0000774 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
775 Function *F = (*I)->getFunction();
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000776
777 if (F == 0)
778 // External node - skip it;
779 return false;
780
781 // Already noalias.
782 if (F->doesNotAlias(0))
783 continue;
784
785 // Definitions with weak linkage may be overridden at linktime, so
786 // treat them like declarations.
787 if (F->isDeclaration() || F->mayBeOverridden())
788 return false;
789
790 // We annotate noalias return values, which are only applicable to
791 // pointer types.
Duncan Sands1df98592010-02-16 11:11:14 +0000792 if (!F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000793 continue;
794
795 if (!IsFunctionMallocLike(F, SCCNodes))
796 return false;
797 }
798
799 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000800 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
801 Function *F = (*I)->getFunction();
Duncan Sands1df98592010-02-16 11:11:14 +0000802 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000803 continue;
804
805 F->setDoesNotAlias(0);
806 ++NumNoAlias;
807 MadeChange = true;
808 }
809
810 return MadeChange;
811}
812
Meador Ingecf47ce62013-03-21 00:55:59 +0000813/// inferPrototypeAttributes - Analyze the name and prototype of the
814/// given function and set any applicable attributes. Returns true
815/// if any attributes were set and false otherwise.
816bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
817 FunctionType *FTy = F.getFunctionType();
818 LibFunc::Func TheLibFunc;
819 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
820 return false;
821
822 switch (TheLibFunc) {
823 case LibFunc::strlen:
824 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
825 return false;
826 setOnlyReadsMemory(F);
827 setDoesNotThrow(F);
828 setDoesNotCapture(F, 1);
829 break;
830 case LibFunc::strchr:
831 case LibFunc::strrchr:
832 if (FTy->getNumParams() != 2 ||
833 !FTy->getParamType(0)->isPointerTy() ||
834 !FTy->getParamType(1)->isIntegerTy())
835 return false;
836 setOnlyReadsMemory(F);
837 setDoesNotThrow(F);
838 break;
Meador Ingecf47ce62013-03-21 00:55:59 +0000839 case LibFunc::strtol:
840 case LibFunc::strtod:
841 case LibFunc::strtof:
842 case LibFunc::strtoul:
843 case LibFunc::strtoll:
844 case LibFunc::strtold:
Meador Ingecf47ce62013-03-21 00:55:59 +0000845 case LibFunc::strtoull:
846 if (FTy->getNumParams() < 2 ||
847 !FTy->getParamType(1)->isPointerTy())
848 return false;
849 setDoesNotThrow(F);
850 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000851 setOnlyReadsMemory(F, 1);
852 break;
853 case LibFunc::strcpy:
854 case LibFunc::stpcpy:
855 case LibFunc::strcat:
856 case LibFunc::strncat:
857 case LibFunc::strncpy:
858 case LibFunc::stpncpy:
859 if (FTy->getNumParams() < 2 ||
860 !FTy->getParamType(1)->isPointerTy())
861 return false;
862 setDoesNotThrow(F);
863 setDoesNotCapture(F, 2);
864 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000865 break;
866 case LibFunc::strxfrm:
867 if (FTy->getNumParams() != 3 ||
868 !FTy->getParamType(0)->isPointerTy() ||
869 !FTy->getParamType(1)->isPointerTy())
870 return false;
871 setDoesNotThrow(F);
872 setDoesNotCapture(F, 1);
873 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000874 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000875 break;
Nick Lewyckydc897372013-07-06 00:29:58 +0000876 case LibFunc::strcmp: //0,1
877 case LibFunc::strspn: // 0,1
878 case LibFunc::strncmp: // 0,1
879 case LibFunc::strcspn: //0,1
880 case LibFunc::strcoll: //0,1
881 case LibFunc::strcasecmp: // 0,1
882 case LibFunc::strncasecmp: //
Meador Ingecf47ce62013-03-21 00:55:59 +0000883 if (FTy->getNumParams() < 2 ||
884 !FTy->getParamType(0)->isPointerTy() ||
885 !FTy->getParamType(1)->isPointerTy())
886 return false;
887 setOnlyReadsMemory(F);
888 setDoesNotThrow(F);
889 setDoesNotCapture(F, 1);
890 setDoesNotCapture(F, 2);
891 break;
892 case LibFunc::strstr:
893 case LibFunc::strpbrk:
894 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
895 return false;
896 setOnlyReadsMemory(F);
897 setDoesNotThrow(F);
898 setDoesNotCapture(F, 2);
899 break;
900 case LibFunc::strtok:
901 case LibFunc::strtok_r:
902 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
903 return false;
904 setDoesNotThrow(F);
905 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000906 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000907 break;
908 case LibFunc::scanf:
Nick Lewyckydc897372013-07-06 00:29:58 +0000909 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
910 return false;
911 setDoesNotThrow(F);
912 setDoesNotCapture(F, 1);
913 setOnlyReadsMemory(F, 1);
914 break;
Meador Ingecf47ce62013-03-21 00:55:59 +0000915 case LibFunc::setbuf:
916 case LibFunc::setvbuf:
917 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
918 return false;
919 setDoesNotThrow(F);
920 setDoesNotCapture(F, 1);
921 break;
922 case LibFunc::strdup:
923 case LibFunc::strndup:
924 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
925 !FTy->getParamType(0)->isPointerTy())
926 return false;
927 setDoesNotThrow(F);
928 setDoesNotAlias(F, 0);
929 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +0000930 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +0000931 break;
932 case LibFunc::stat:
Meador Ingecf47ce62013-03-21 00:55:59 +0000933 case LibFunc::statvfs:
934 if (FTy->getNumParams() < 2 ||
935 !FTy->getParamType(0)->isPointerTy() ||
936 !FTy->getParamType(1)->isPointerTy())
937 return false;
938 setDoesNotThrow(F);
939 setDoesNotCapture(F, 1);
940 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000941 setOnlyReadsMemory(F, 1);
942 break;
943 case LibFunc::sscanf:
944 if (FTy->getNumParams() < 2 ||
945 !FTy->getParamType(0)->isPointerTy() ||
946 !FTy->getParamType(1)->isPointerTy())
947 return false;
948 setDoesNotThrow(F);
949 setDoesNotCapture(F, 1);
950 setDoesNotCapture(F, 2);
951 setOnlyReadsMemory(F, 1);
952 setOnlyReadsMemory(F, 2);
953 break;
954 case LibFunc::sprintf:
955 if (FTy->getNumParams() < 2 ||
956 !FTy->getParamType(0)->isPointerTy() ||
957 !FTy->getParamType(1)->isPointerTy())
958 return false;
959 setDoesNotThrow(F);
960 setDoesNotCapture(F, 1);
961 setDoesNotCapture(F, 2);
962 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000963 break;
964 case LibFunc::snprintf:
965 if (FTy->getNumParams() != 3 ||
966 !FTy->getParamType(0)->isPointerTy() ||
967 !FTy->getParamType(2)->isPointerTy())
968 return false;
969 setDoesNotThrow(F);
970 setDoesNotCapture(F, 1);
971 setDoesNotCapture(F, 3);
Nick Lewyckydc897372013-07-06 00:29:58 +0000972 setOnlyReadsMemory(F, 3);
Meador Ingecf47ce62013-03-21 00:55:59 +0000973 break;
974 case LibFunc::setitimer:
975 if (FTy->getNumParams() != 3 ||
976 !FTy->getParamType(1)->isPointerTy() ||
977 !FTy->getParamType(2)->isPointerTy())
978 return false;
979 setDoesNotThrow(F);
980 setDoesNotCapture(F, 2);
981 setDoesNotCapture(F, 3);
Nick Lewyckydc897372013-07-06 00:29:58 +0000982 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000983 break;
984 case LibFunc::system:
985 if (FTy->getNumParams() != 1 ||
986 !FTy->getParamType(0)->isPointerTy())
987 return false;
988 // May throw; "system" is a valid pthread cancellation point.
989 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +0000990 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +0000991 break;
992 case LibFunc::malloc:
993 if (FTy->getNumParams() != 1 ||
994 !FTy->getReturnType()->isPointerTy())
995 return false;
996 setDoesNotThrow(F);
997 setDoesNotAlias(F, 0);
998 break;
999 case LibFunc::memcmp:
1000 if (FTy->getNumParams() != 3 ||
1001 !FTy->getParamType(0)->isPointerTy() ||
1002 !FTy->getParamType(1)->isPointerTy())
1003 return false;
1004 setOnlyReadsMemory(F);
1005 setDoesNotThrow(F);
1006 setDoesNotCapture(F, 1);
1007 setDoesNotCapture(F, 2);
1008 break;
1009 case LibFunc::memchr:
1010 case LibFunc::memrchr:
1011 if (FTy->getNumParams() != 3)
1012 return false;
1013 setOnlyReadsMemory(F);
1014 setDoesNotThrow(F);
1015 break;
1016 case LibFunc::modf:
1017 case LibFunc::modff:
1018 case LibFunc::modfl:
Nick Lewyckydc897372013-07-06 00:29:58 +00001019 if (FTy->getNumParams() < 2 ||
1020 !FTy->getParamType(1)->isPointerTy())
1021 return false;
1022 setDoesNotThrow(F);
1023 setDoesNotCapture(F, 2);
1024 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001025 case LibFunc::memcpy:
1026 case LibFunc::memccpy:
1027 case LibFunc::memmove:
1028 if (FTy->getNumParams() < 2 ||
1029 !FTy->getParamType(1)->isPointerTy())
1030 return false;
1031 setDoesNotThrow(F);
1032 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001033 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001034 break;
1035 case LibFunc::memalign:
1036 if (!FTy->getReturnType()->isPointerTy())
1037 return false;
1038 setDoesNotAlias(F, 0);
1039 break;
1040 case LibFunc::mkdir:
Nick Lewyckydc897372013-07-06 00:29:58 +00001041 if (FTy->getNumParams() == 0 ||
1042 !FTy->getParamType(0)->isPointerTy())
1043 return false;
1044 setDoesNotThrow(F);
1045 setDoesNotCapture(F, 1);
1046 setOnlyReadsMemory(F, 1);
1047 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001048 case LibFunc::mktime:
1049 if (FTy->getNumParams() == 0 ||
1050 !FTy->getParamType(0)->isPointerTy())
1051 return false;
1052 setDoesNotThrow(F);
1053 setDoesNotCapture(F, 1);
1054 break;
1055 case LibFunc::realloc:
1056 if (FTy->getNumParams() != 2 ||
1057 !FTy->getParamType(0)->isPointerTy() ||
1058 !FTy->getReturnType()->isPointerTy())
1059 return false;
1060 setDoesNotThrow(F);
1061 setDoesNotAlias(F, 0);
1062 setDoesNotCapture(F, 1);
1063 break;
1064 case LibFunc::read:
1065 if (FTy->getNumParams() != 3 ||
1066 !FTy->getParamType(1)->isPointerTy())
1067 return false;
1068 // May throw; "read" is a valid pthread cancellation point.
1069 setDoesNotCapture(F, 2);
1070 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001071 case LibFunc::rewind:
Nick Lewyckydc897372013-07-06 00:29:58 +00001072 if (FTy->getNumParams() < 1 ||
1073 !FTy->getParamType(0)->isPointerTy())
1074 return false;
1075 setDoesNotThrow(F);
1076 setDoesNotCapture(F, 1);
1077 break;
1078 case LibFunc::rmdir:
Meador Ingecf47ce62013-03-21 00:55:59 +00001079 case LibFunc::remove:
1080 case LibFunc::realpath:
1081 if (FTy->getNumParams() < 1 ||
1082 !FTy->getParamType(0)->isPointerTy())
1083 return false;
1084 setDoesNotThrow(F);
1085 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001086 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001087 break;
1088 case LibFunc::rename:
Nick Lewyckydc897372013-07-06 00:29:58 +00001089 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 setOnlyReadsMemory(F, 1);
1097 setOnlyReadsMemory(F, 2);
1098 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001099 case LibFunc::readlink:
1100 if (FTy->getNumParams() < 2 ||
1101 !FTy->getParamType(0)->isPointerTy() ||
1102 !FTy->getParamType(1)->isPointerTy())
1103 return false;
1104 setDoesNotThrow(F);
1105 setDoesNotCapture(F, 1);
1106 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001107 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001108 break;
1109 case LibFunc::write:
1110 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1111 return false;
1112 // May throw; "write" is a valid pthread cancellation point.
1113 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001114 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001115 break;
1116 case LibFunc::bcopy:
1117 if (FTy->getNumParams() != 3 ||
1118 !FTy->getParamType(0)->isPointerTy() ||
1119 !FTy->getParamType(1)->isPointerTy())
1120 return false;
1121 setDoesNotThrow(F);
1122 setDoesNotCapture(F, 1);
1123 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001124 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001125 break;
1126 case LibFunc::bcmp:
1127 if (FTy->getNumParams() != 3 ||
1128 !FTy->getParamType(0)->isPointerTy() ||
1129 !FTy->getParamType(1)->isPointerTy())
1130 return false;
1131 setDoesNotThrow(F);
1132 setOnlyReadsMemory(F);
1133 setDoesNotCapture(F, 1);
1134 setDoesNotCapture(F, 2);
1135 break;
1136 case LibFunc::bzero:
1137 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1138 return false;
1139 setDoesNotThrow(F);
1140 setDoesNotCapture(F, 1);
1141 break;
1142 case LibFunc::calloc:
1143 if (FTy->getNumParams() != 2 ||
1144 !FTy->getReturnType()->isPointerTy())
1145 return false;
1146 setDoesNotThrow(F);
1147 setDoesNotAlias(F, 0);
1148 break;
1149 case LibFunc::chmod:
1150 case LibFunc::chown:
Nick Lewyckydc897372013-07-06 00:29:58 +00001151 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1152 return false;
1153 setDoesNotThrow(F);
1154 setDoesNotCapture(F, 1);
1155 setOnlyReadsMemory(F, 1);
1156 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001157 case LibFunc::ctermid:
1158 case LibFunc::clearerr:
1159 case LibFunc::closedir:
1160 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1161 return false;
1162 setDoesNotThrow(F);
1163 setDoesNotCapture(F, 1);
1164 break;
1165 case LibFunc::atoi:
1166 case LibFunc::atol:
1167 case LibFunc::atof:
1168 case LibFunc::atoll:
1169 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1170 return false;
1171 setDoesNotThrow(F);
1172 setOnlyReadsMemory(F);
1173 setDoesNotCapture(F, 1);
1174 break;
1175 case LibFunc::access:
1176 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1177 return false;
1178 setDoesNotThrow(F);
1179 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001180 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001181 break;
1182 case LibFunc::fopen:
1183 if (FTy->getNumParams() != 2 ||
1184 !FTy->getReturnType()->isPointerTy() ||
1185 !FTy->getParamType(0)->isPointerTy() ||
1186 !FTy->getParamType(1)->isPointerTy())
1187 return false;
1188 setDoesNotThrow(F);
1189 setDoesNotAlias(F, 0);
1190 setDoesNotCapture(F, 1);
1191 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001192 setOnlyReadsMemory(F, 1);
1193 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001194 break;
1195 case LibFunc::fdopen:
1196 if (FTy->getNumParams() != 2 ||
1197 !FTy->getReturnType()->isPointerTy() ||
1198 !FTy->getParamType(1)->isPointerTy())
1199 return false;
1200 setDoesNotThrow(F);
1201 setDoesNotAlias(F, 0);
1202 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001203 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001204 break;
1205 case LibFunc::feof:
1206 case LibFunc::free:
1207 case LibFunc::fseek:
1208 case LibFunc::ftell:
1209 case LibFunc::fgetc:
1210 case LibFunc::fseeko:
1211 case LibFunc::ftello:
1212 case LibFunc::fileno:
1213 case LibFunc::fflush:
1214 case LibFunc::fclose:
1215 case LibFunc::fsetpos:
1216 case LibFunc::flockfile:
1217 case LibFunc::funlockfile:
1218 case LibFunc::ftrylockfile:
1219 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1220 return false;
1221 setDoesNotThrow(F);
1222 setDoesNotCapture(F, 1);
1223 break;
1224 case LibFunc::ferror:
1225 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1226 return false;
1227 setDoesNotThrow(F);
1228 setDoesNotCapture(F, 1);
1229 setOnlyReadsMemory(F);
1230 break;
1231 case LibFunc::fputc:
1232 case LibFunc::fstat:
1233 case LibFunc::frexp:
1234 case LibFunc::frexpf:
1235 case LibFunc::frexpl:
1236 case LibFunc::fstatvfs:
1237 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1238 return false;
1239 setDoesNotThrow(F);
1240 setDoesNotCapture(F, 2);
1241 break;
1242 case LibFunc::fgets:
1243 if (FTy->getNumParams() != 3 ||
1244 !FTy->getParamType(0)->isPointerTy() ||
1245 !FTy->getParamType(2)->isPointerTy())
1246 return false;
1247 setDoesNotThrow(F);
1248 setDoesNotCapture(F, 3);
Nick Lewyckye7dd3af2013-07-02 05:02:56 +00001249 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001250 case LibFunc::fread:
Nick Lewyckydc897372013-07-06 00:29:58 +00001251 if (FTy->getNumParams() != 4 ||
1252 !FTy->getParamType(0)->isPointerTy() ||
1253 !FTy->getParamType(3)->isPointerTy())
1254 return false;
1255 setDoesNotThrow(F);
1256 setDoesNotCapture(F, 1);
1257 setDoesNotCapture(F, 4);
1258 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001259 case LibFunc::fwrite:
1260 if (FTy->getNumParams() != 4 ||
1261 !FTy->getParamType(0)->isPointerTy() ||
1262 !FTy->getParamType(3)->isPointerTy())
1263 return false;
1264 setDoesNotThrow(F);
1265 setDoesNotCapture(F, 1);
1266 setDoesNotCapture(F, 4);
Nick Lewyckye7dd3af2013-07-02 05:02:56 +00001267 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001268 case LibFunc::fputs:
Nick Lewyckydc897372013-07-06 00:29:58 +00001269 if (FTy->getNumParams() < 2 ||
1270 !FTy->getParamType(0)->isPointerTy() ||
1271 !FTy->getParamType(1)->isPointerTy())
1272 return false;
1273 setDoesNotThrow(F);
1274 setDoesNotCapture(F, 1);
1275 setDoesNotCapture(F, 2);
1276 setOnlyReadsMemory(F, 1);
1277 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001278 case LibFunc::fscanf:
1279 case LibFunc::fprintf:
Nick Lewyckydc897372013-07-06 00:29:58 +00001280 if (FTy->getNumParams() < 2 ||
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 setOnlyReadsMemory(F, 2);
1288 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001289 case LibFunc::fgetpos:
1290 if (FTy->getNumParams() < 2 ||
1291 !FTy->getParamType(0)->isPointerTy() ||
1292 !FTy->getParamType(1)->isPointerTy())
1293 return false;
1294 setDoesNotThrow(F);
1295 setDoesNotCapture(F, 1);
1296 setDoesNotCapture(F, 2);
1297 break;
1298 case LibFunc::getc:
1299 case LibFunc::getlogin_r:
1300 case LibFunc::getc_unlocked:
1301 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1302 return false;
1303 setDoesNotThrow(F);
1304 setDoesNotCapture(F, 1);
1305 break;
1306 case LibFunc::getenv:
1307 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1308 return false;
1309 setDoesNotThrow(F);
1310 setOnlyReadsMemory(F);
1311 setDoesNotCapture(F, 1);
1312 break;
1313 case LibFunc::gets:
1314 case LibFunc::getchar:
1315 setDoesNotThrow(F);
1316 break;
1317 case LibFunc::getitimer:
1318 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1319 return false;
1320 setDoesNotThrow(F);
1321 setDoesNotCapture(F, 2);
1322 break;
1323 case LibFunc::getpwnam:
1324 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1325 return false;
1326 setDoesNotThrow(F);
1327 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001328 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001329 break;
1330 case LibFunc::ungetc:
1331 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1332 return false;
1333 setDoesNotThrow(F);
1334 setDoesNotCapture(F, 2);
1335 break;
1336 case LibFunc::uname:
Nick Lewyckydc897372013-07-06 00:29:58 +00001337 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1338 return false;
1339 setDoesNotThrow(F);
1340 setDoesNotCapture(F, 1);
1341 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001342 case LibFunc::unlink:
Nick Lewyckydc897372013-07-06 00:29:58 +00001343 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1344 return false;
1345 setDoesNotThrow(F);
1346 setDoesNotCapture(F, 1);
Nick Lewycky60ceb6e2013-07-06 00:59:28 +00001347 setOnlyReadsMemory(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001348 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001349 case LibFunc::unsetenv:
1350 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1351 return false;
1352 setDoesNotThrow(F);
1353 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001354 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001355 break;
1356 case LibFunc::utime:
1357 case LibFunc::utimes:
1358 if (FTy->getNumParams() != 2 ||
1359 !FTy->getParamType(0)->isPointerTy() ||
1360 !FTy->getParamType(1)->isPointerTy())
1361 return false;
1362 setDoesNotThrow(F);
1363 setDoesNotCapture(F, 1);
1364 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001365 setOnlyReadsMemory(F, 1);
1366 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001367 break;
1368 case LibFunc::putc:
1369 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1370 return false;
1371 setDoesNotThrow(F);
1372 setDoesNotCapture(F, 2);
1373 break;
1374 case LibFunc::puts:
1375 case LibFunc::printf:
1376 case LibFunc::perror:
1377 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1378 return false;
1379 setDoesNotThrow(F);
1380 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001381 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001382 break;
1383 case LibFunc::pread:
Nick Lewyckydc897372013-07-06 00:29:58 +00001384 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1385 return false;
1386 // May throw; "pread" is a valid pthread cancellation point.
1387 setDoesNotCapture(F, 2);
1388 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001389 case LibFunc::pwrite:
1390 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1391 return false;
Nick Lewyckydc897372013-07-06 00:29:58 +00001392 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Ingecf47ce62013-03-21 00:55:59 +00001393 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001394 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001395 break;
1396 case LibFunc::putchar:
1397 setDoesNotThrow(F);
1398 break;
1399 case LibFunc::popen:
1400 if (FTy->getNumParams() != 2 ||
1401 !FTy->getReturnType()->isPointerTy() ||
1402 !FTy->getParamType(0)->isPointerTy() ||
1403 !FTy->getParamType(1)->isPointerTy())
1404 return false;
1405 setDoesNotThrow(F);
1406 setDoesNotAlias(F, 0);
1407 setDoesNotCapture(F, 1);
1408 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001409 setOnlyReadsMemory(F, 1);
1410 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001411 break;
1412 case LibFunc::pclose:
1413 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1414 return false;
1415 setDoesNotThrow(F);
1416 setDoesNotCapture(F, 1);
1417 break;
1418 case LibFunc::vscanf:
1419 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1420 return false;
1421 setDoesNotThrow(F);
1422 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001423 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001424 break;
1425 case LibFunc::vsscanf:
Nick Lewyckydc897372013-07-06 00:29:58 +00001426 if (FTy->getNumParams() != 3 ||
1427 !FTy->getParamType(1)->isPointerTy() ||
1428 !FTy->getParamType(2)->isPointerTy())
1429 return false;
1430 setDoesNotThrow(F);
1431 setDoesNotCapture(F, 1);
1432 setDoesNotCapture(F, 2);
1433 setOnlyReadsMemory(F, 1);
1434 setOnlyReadsMemory(F, 2);
1435 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001436 case LibFunc::vfscanf:
1437 if (FTy->getNumParams() != 3 ||
1438 !FTy->getParamType(1)->isPointerTy() ||
1439 !FTy->getParamType(2)->isPointerTy())
1440 return false;
1441 setDoesNotThrow(F);
1442 setDoesNotCapture(F, 1);
1443 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001444 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001445 break;
1446 case LibFunc::valloc:
1447 if (!FTy->getReturnType()->isPointerTy())
1448 return false;
1449 setDoesNotThrow(F);
1450 setDoesNotAlias(F, 0);
1451 break;
1452 case LibFunc::vprintf:
1453 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1454 return false;
1455 setDoesNotThrow(F);
1456 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001457 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001458 break;
1459 case LibFunc::vfprintf:
1460 case LibFunc::vsprintf:
1461 if (FTy->getNumParams() != 3 ||
1462 !FTy->getParamType(0)->isPointerTy() ||
1463 !FTy->getParamType(1)->isPointerTy())
1464 return false;
1465 setDoesNotThrow(F);
1466 setDoesNotCapture(F, 1);
1467 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001468 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001469 break;
1470 case LibFunc::vsnprintf:
1471 if (FTy->getNumParams() != 4 ||
1472 !FTy->getParamType(0)->isPointerTy() ||
1473 !FTy->getParamType(2)->isPointerTy())
1474 return false;
1475 setDoesNotThrow(F);
1476 setDoesNotCapture(F, 1);
1477 setDoesNotCapture(F, 3);
Nick Lewyckydc897372013-07-06 00:29:58 +00001478 setOnlyReadsMemory(F, 3);
Meador Ingecf47ce62013-03-21 00:55:59 +00001479 break;
1480 case LibFunc::open:
1481 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1482 return false;
1483 // May throw; "open" is a valid pthread cancellation point.
1484 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001485 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001486 break;
1487 case LibFunc::opendir:
1488 if (FTy->getNumParams() != 1 ||
1489 !FTy->getReturnType()->isPointerTy() ||
1490 !FTy->getParamType(0)->isPointerTy())
1491 return false;
1492 setDoesNotThrow(F);
1493 setDoesNotAlias(F, 0);
1494 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001495 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001496 break;
1497 case LibFunc::tmpfile:
1498 if (!FTy->getReturnType()->isPointerTy())
1499 return false;
1500 setDoesNotThrow(F);
1501 setDoesNotAlias(F, 0);
1502 break;
1503 case LibFunc::times:
1504 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1505 return false;
1506 setDoesNotThrow(F);
1507 setDoesNotCapture(F, 1);
1508 break;
1509 case LibFunc::htonl:
1510 case LibFunc::htons:
1511 case LibFunc::ntohl:
1512 case LibFunc::ntohs:
1513 setDoesNotThrow(F);
1514 setDoesNotAccessMemory(F);
1515 break;
1516 case LibFunc::lstat:
1517 if (FTy->getNumParams() != 2 ||
1518 !FTy->getParamType(0)->isPointerTy() ||
1519 !FTy->getParamType(1)->isPointerTy())
1520 return false;
1521 setDoesNotThrow(F);
1522 setDoesNotCapture(F, 1);
1523 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001524 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001525 break;
1526 case LibFunc::lchown:
1527 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1528 return false;
1529 setDoesNotThrow(F);
1530 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001531 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001532 break;
1533 case LibFunc::qsort:
1534 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1535 return false;
1536 // May throw; places call through function pointer.
1537 setDoesNotCapture(F, 4);
1538 break;
1539 case LibFunc::dunder_strdup:
1540 case LibFunc::dunder_strndup:
1541 if (FTy->getNumParams() < 1 ||
1542 !FTy->getReturnType()->isPointerTy() ||
1543 !FTy->getParamType(0)->isPointerTy())
1544 return false;
1545 setDoesNotThrow(F);
1546 setDoesNotAlias(F, 0);
1547 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001548 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001549 break;
1550 case LibFunc::dunder_strtok_r:
1551 if (FTy->getNumParams() != 3 ||
1552 !FTy->getParamType(1)->isPointerTy())
1553 return false;
1554 setDoesNotThrow(F);
1555 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001556 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001557 break;
1558 case LibFunc::under_IO_getc:
1559 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1560 return false;
1561 setDoesNotThrow(F);
1562 setDoesNotCapture(F, 1);
1563 break;
1564 case LibFunc::under_IO_putc:
1565 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1566 return false;
1567 setDoesNotThrow(F);
1568 setDoesNotCapture(F, 2);
1569 break;
1570 case LibFunc::dunder_isoc99_scanf:
1571 if (FTy->getNumParams() < 1 ||
1572 !FTy->getParamType(0)->isPointerTy())
1573 return false;
1574 setDoesNotThrow(F);
1575 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001576 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001577 break;
1578 case LibFunc::stat64:
1579 case LibFunc::lstat64:
1580 case LibFunc::statvfs64:
Nick Lewyckydc897372013-07-06 00:29:58 +00001581 if (FTy->getNumParams() < 1 ||
1582 !FTy->getParamType(0)->isPointerTy() ||
1583 !FTy->getParamType(1)->isPointerTy())
1584 return false;
1585 setDoesNotThrow(F);
1586 setDoesNotCapture(F, 1);
1587 setDoesNotCapture(F, 2);
1588 setOnlyReadsMemory(F, 1);
1589 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001590 case LibFunc::dunder_isoc99_sscanf:
1591 if (FTy->getNumParams() < 1 ||
1592 !FTy->getParamType(0)->isPointerTy() ||
1593 !FTy->getParamType(1)->isPointerTy())
1594 return false;
1595 setDoesNotThrow(F);
1596 setDoesNotCapture(F, 1);
1597 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001598 setOnlyReadsMemory(F, 1);
1599 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001600 break;
1601 case LibFunc::fopen64:
1602 if (FTy->getNumParams() != 2 ||
1603 !FTy->getReturnType()->isPointerTy() ||
1604 !FTy->getParamType(0)->isPointerTy() ||
1605 !FTy->getParamType(1)->isPointerTy())
1606 return false;
1607 setDoesNotThrow(F);
1608 setDoesNotAlias(F, 0);
1609 setDoesNotCapture(F, 1);
1610 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001611 setOnlyReadsMemory(F, 1);
1612 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001613 break;
1614 case LibFunc::fseeko64:
1615 case LibFunc::ftello64:
1616 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1617 return false;
1618 setDoesNotThrow(F);
1619 setDoesNotCapture(F, 1);
1620 break;
1621 case LibFunc::tmpfile64:
1622 if (!FTy->getReturnType()->isPointerTy())
1623 return false;
1624 setDoesNotThrow(F);
1625 setDoesNotAlias(F, 0);
1626 break;
1627 case LibFunc::fstat64:
1628 case LibFunc::fstatvfs64:
1629 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1630 return false;
1631 setDoesNotThrow(F);
1632 setDoesNotCapture(F, 2);
1633 break;
1634 case LibFunc::open64:
1635 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1636 return false;
1637 // May throw; "open" is a valid pthread cancellation point.
1638 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001639 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001640 break;
Michael Gottesman7cb03212013-07-03 04:00:54 +00001641 case LibFunc::gettimeofday:
1642 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1643 !FTy->getParamType(1)->isPointerTy())
1644 return false;
1645 // Currently some platforms have the restrict keyword on the arguments to
1646 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1647 // arguments.
1648 setDoesNotThrow(F);
1649 setDoesNotCapture(F, 1);
1650 setDoesNotCapture(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001651 default:
1652 // Didn't mark any attributes.
1653 return false;
1654 }
1655
1656 return true;
1657}
1658
1659/// annotateLibraryCalls - Adds attributes to well-known standard library
1660/// call declarations.
1661bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1662 bool MadeChange = false;
1663
1664 // Check each function in turn annotating well-known library function
1665 // declarations with attributes.
1666 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1667 Function *F = (*I)->getFunction();
1668
1669 if (F != 0 && F->isDeclaration())
1670 MadeChange |= inferPrototypeAttributes(*F);
1671 }
1672
1673 return MadeChange;
1674}
1675
Chris Lattner2decb222010-04-16 22:42:17 +00001676bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman3c97f7a2010-11-08 16:10:15 +00001677 AA = &getAnalysis<AliasAnalysis>();
Meador Ingecf47ce62013-03-21 00:55:59 +00001678 TLI = &getAnalysis<TargetLibraryInfo>();
Dan Gohman3c97f7a2010-11-08 16:10:15 +00001679
Meador Ingecf47ce62013-03-21 00:55:59 +00001680 bool Changed = annotateLibraryCalls(SCC);
1681 Changed |= AddReadAttrs(SCC);
Nick Lewyckydc897372013-07-06 00:29:58 +00001682 Changed |= AddArgumentAttrs(SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +00001683 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +00001684 return Changed;
1685}