blob: b716718574db30612feb845d35b2ea1fa1c2cddb [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"
Stephen Hines36b56882014-04-23 16:57:46 -070032#include "llvm/IR/InstIterator.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000033#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/LLVMContext.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.
Stephen Hines36b56882014-04-23 16:57:46 -070054 bool runOnSCC(CallGraphSCC &SCC) override;
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
Stephen Hines36b56882014-04-23 16:57:46 -0700123 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000124 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)
Stephen Hines36b56882014-04-23 16:57:46 -0700140INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
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
Stephen Hines36b56882014-04-23 16:57:46 -0700345 void tooManyUses() override { Captured = true; }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000346
Stephen Hines36b56882014-04-23 16:57:46 -0700347 bool captured(const Use *U) override {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000348 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?");
Duncan Sands9a8392b2013-09-13 08:16:06 +0000370 (void)Found;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000371 return false;
372 }
373
374 bool Captured; // True only if certainly captured (used outside our SCC).
375 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
376
377 const SmallPtrSet<Function*, 8> &SCCNodes;
378 };
379}
380
381namespace llvm {
382 template<> struct GraphTraits<ArgumentGraphNode*> {
383 typedef ArgumentGraphNode NodeType;
384 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
385
386 static inline NodeType *getEntryNode(NodeType *A) { return A; }
387 static inline ChildIteratorType child_begin(NodeType *N) {
388 return N->Uses.begin();
389 }
390 static inline ChildIteratorType child_end(NodeType *N) {
391 return N->Uses.end();
392 }
393 };
394 template<> struct GraphTraits<ArgumentGraph*>
395 : public GraphTraits<ArgumentGraphNode*> {
396 static NodeType *getEntryNode(ArgumentGraph *AG) {
397 return AG->getEntryNode();
398 }
399 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
400 return AG->begin();
401 }
402 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
403 return AG->end();
404 }
405 };
406}
407
Nick Lewyckydc897372013-07-06 00:29:58 +0000408// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
409static Attribute::AttrKind
410determinePointerReadAttrs(Argument *A,
411 const SmallPtrSet<Argument*, 8> &SCCNodes) {
412
413 SmallVector<Use*, 32> Worklist;
414 SmallSet<Use*, 32> Visited;
415 int Count = 0;
416
Stephen Hines36b56882014-04-23 16:57:46 -0700417 // inalloca arguments are always clobbered by the call.
418 if (A->hasInAllocaAttr())
419 return Attribute::None;
420
Nick Lewyckydc897372013-07-06 00:29:58 +0000421 bool IsRead = false;
422 // We don't need to track IsWritten. If A is written to, return immediately.
423
Stephen Hines36b56882014-04-23 16:57:46 -0700424 for (Use &U : A->uses()) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000425 if (Count++ >= 20)
426 return Attribute::None;
427
Stephen Hines36b56882014-04-23 16:57:46 -0700428 Visited.insert(&U);
429 Worklist.push_back(&U);
Nick Lewyckydc897372013-07-06 00:29:58 +0000430 }
431
432 while (!Worklist.empty()) {
433 Use *U = Worklist.pop_back_val();
434 Instruction *I = cast<Instruction>(U->getUser());
435 Value *V = U->get();
436
437 switch (I->getOpcode()) {
438 case Instruction::BitCast:
439 case Instruction::GetElementPtr:
440 case Instruction::PHI:
441 case Instruction::Select:
Stephen Hines36b56882014-04-23 16:57:46 -0700442 case Instruction::AddrSpaceCast:
Nick Lewyckydc897372013-07-06 00:29:58 +0000443 // The original value is not read/written via this if the new value isn't.
Stephen Hines36b56882014-04-23 16:57:46 -0700444 for (Use &UU : I->uses())
445 if (Visited.insert(&UU))
446 Worklist.push_back(&UU);
Nick Lewyckydc897372013-07-06 00:29:58 +0000447 break;
448
449 case Instruction::Call:
450 case Instruction::Invoke: {
451 CallSite CS(I);
452 if (CS.doesNotAccessMemory())
453 continue;
454
455 Function *F = CS.getCalledFunction();
456 if (!F) {
457 if (CS.onlyReadsMemory()) {
458 IsRead = true;
459 continue;
460 }
461 return Attribute::None;
462 }
463
464 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
465 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
466 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
467 if (A->get() == V) {
468 if (AI == AE) {
469 assert(F->isVarArg() &&
470 "More params than args in non-varargs call.");
471 return Attribute::None;
472 }
473 if (SCCNodes.count(AI))
474 continue;
475 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
476 return Attribute::None;
477 if (!CS.doesNotAccessMemory(A - B))
478 IsRead = true;
479 }
480 }
481 break;
482 }
483
484 case Instruction::Load:
485 IsRead = true;
486 break;
487
488 case Instruction::ICmp:
489 case Instruction::Ret:
490 break;
491
492 default:
493 return Attribute::None;
494 }
495 }
496
497 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
498}
499
500/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
501bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000502 bool Changed = false;
503
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000504 SmallPtrSet<Function*, 8> SCCNodes;
505
506 // Fill SCCNodes with the elements of the SCC. Used for quickly
507 // looking up whether a given CallGraphNode is in this SCC.
508 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
509 Function *F = (*I)->getFunction();
510 if (F && !F->isDeclaration() && !F->mayBeOverridden())
511 SCCNodes.insert(F);
512 }
513
514 ArgumentGraph AG;
515
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000516 AttrBuilder B;
517 B.addAttribute(Attribute::NoCapture);
518
Duncan Sands9e89ba32008-12-31 16:14:43 +0000519 // Check each function in turn, determining which pointer arguments are not
520 // captured.
Chris Lattner2decb222010-04-16 22:42:17 +0000521 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
522 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000523
524 if (F == 0)
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000525 // External node - only a problem for arguments that we pass to it.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000526 continue;
527
528 // Definitions with weak linkage may be overridden at linktime with
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000529 // something that captures pointers, so treat them like declarations.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000530 if (F->isDeclaration() || F->mayBeOverridden())
531 continue;
532
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000533 // Functions that are readonly (or readnone) and nounwind and don't return
534 // a value can't capture arguments. Don't analyze them.
535 if (F->onlyReadsMemory() && F->doesNotThrow() &&
536 F->getReturnType()->isVoidTy()) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000537 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
538 A != E; ++A) {
539 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
540 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
541 ++NumNoCapture;
542 Changed = true;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000543 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000544 }
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000545 continue;
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000546 }
547
Nick Lewyckydc897372013-07-06 00:29:58 +0000548 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
549 A != E; ++A) {
550 if (!A->getType()->isPointerTy()) continue;
551 bool HasNonLocalUses = false;
552 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000553 ArgumentUsesTracker Tracker(SCCNodes);
554 PointerMayBeCaptured(A, &Tracker);
555 if (!Tracker.Captured) {
556 if (Tracker.Uses.empty()) {
557 // If it's trivially not captured, mark it nocapture now.
558 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
559 ++NumNoCapture;
560 Changed = true;
561 } else {
562 // If it's not trivially captured and not trivially not captured,
563 // then it must be calling into another function in our SCC. Save
564 // its particulars for Argument-SCC analysis later.
565 ArgumentGraphNode *Node = AG[A];
566 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckydc897372013-07-06 00:29:58 +0000567 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000568 Node->Uses.push_back(AG[*UI]);
Nick Lewyckydc897372013-07-06 00:29:58 +0000569 if (*UI != A)
570 HasNonLocalUses = true;
571 }
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000572 }
573 }
574 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
575 }
Nick Lewyckydc897372013-07-06 00:29:58 +0000576 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
577 // Can we determine that it's readonly/readnone without doing an SCC?
578 // Note that we don't allow any calls at all here, or else our result
579 // will be dependent on the iteration order through the functions in the
580 // SCC.
581 SmallPtrSet<Argument*, 8> Self;
582 Self.insert(A);
583 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
584 if (R != Attribute::None) {
585 AttrBuilder B;
586 B.addAttribute(R);
587 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
588 Changed = true;
589 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
590 }
591 }
592 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000593 }
594
595 // The graph we've collected is partial because we stopped scanning for
596 // argument uses once we solved the argument trivially. These partial nodes
597 // show up as ArgumentGraphNode objects with an empty Uses list, and for
598 // these nodes the final decision about whether they capture has already been
599 // made. If the definition doesn't have a 'nocapture' attribute by now, it
600 // captures.
601
Stephen Hines36b56882014-04-23 16:57:46 -0700602 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000603 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:
Stephen Hines36b56882014-04-23 16:57:46 -0700725 case Instruction::AddrSpaceCast:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000726 FlowsToReturn.insert(RVI->getOperand(0));
727 continue;
728 case Instruction::Select: {
729 SelectInst *SI = cast<SelectInst>(RVI);
730 FlowsToReturn.insert(SI->getTrueValue());
731 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner439044f2009-09-27 21:29:28 +0000732 continue;
733 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000734 case Instruction::PHI: {
735 PHINode *PN = cast<PHINode>(RVI);
736 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
737 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner439044f2009-09-27 21:29:28 +0000738 continue;
739 }
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000740
741 // Check whether the pointer came from an allocation.
742 case Instruction::Alloca:
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000743 break;
744 case Instruction::Call:
745 case Instruction::Invoke: {
746 CallSite CS(RVI);
Bill Wendling034b94b2012-12-19 07:18:57 +0000747 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000748 break;
749 if (CS.getCalledFunction() &&
Chris Lattner98a27ce2009-08-31 04:09:04 +0000750 SCCNodes.count(CS.getCalledFunction()))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000751 break;
752 } // fall-through
753 default:
754 return false; // Did not come from an allocation.
755 }
756
Dan Gohmanf94b5ed2009-11-19 21:57:48 +0000757 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000758 return false;
759 }
760
761 return true;
762}
763
764/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000765bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000766 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000767
768 // Fill SCCNodes with the elements of the SCC. Used for quickly
769 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000770 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
771 SCCNodes.insert((*I)->getFunction());
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000772
Nick Lewycky4bfba9d2009-03-08 17:08:09 +0000773 // Check each function in turn, determining which functions return noalias
774 // pointers.
Chris Lattner2decb222010-04-16 22:42:17 +0000775 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
776 Function *F = (*I)->getFunction();
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000777
778 if (F == 0)
779 // External node - skip it;
780 return false;
781
782 // Already noalias.
783 if (F->doesNotAlias(0))
784 continue;
785
786 // Definitions with weak linkage may be overridden at linktime, so
787 // treat them like declarations.
788 if (F->isDeclaration() || F->mayBeOverridden())
789 return false;
790
791 // We annotate noalias return values, which are only applicable to
792 // pointer types.
Duncan Sands1df98592010-02-16 11:11:14 +0000793 if (!F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000794 continue;
795
796 if (!IsFunctionMallocLike(F, SCCNodes))
797 return false;
798 }
799
800 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000801 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
802 Function *F = (*I)->getFunction();
Duncan Sands1df98592010-02-16 11:11:14 +0000803 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000804 continue;
805
806 F->setDoesNotAlias(0);
807 ++NumNoAlias;
808 MadeChange = true;
809 }
810
811 return MadeChange;
812}
813
Meador Ingecf47ce62013-03-21 00:55:59 +0000814/// inferPrototypeAttributes - Analyze the name and prototype of the
815/// given function and set any applicable attributes. Returns true
816/// if any attributes were set and false otherwise.
817bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
818 FunctionType *FTy = F.getFunctionType();
819 LibFunc::Func TheLibFunc;
820 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
821 return false;
822
823 switch (TheLibFunc) {
824 case LibFunc::strlen:
825 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
826 return false;
827 setOnlyReadsMemory(F);
828 setDoesNotThrow(F);
829 setDoesNotCapture(F, 1);
830 break;
831 case LibFunc::strchr:
832 case LibFunc::strrchr:
833 if (FTy->getNumParams() != 2 ||
834 !FTy->getParamType(0)->isPointerTy() ||
835 !FTy->getParamType(1)->isIntegerTy())
836 return false;
837 setOnlyReadsMemory(F);
838 setDoesNotThrow(F);
839 break;
Meador Ingecf47ce62013-03-21 00:55:59 +0000840 case LibFunc::strtol:
841 case LibFunc::strtod:
842 case LibFunc::strtof:
843 case LibFunc::strtoul:
844 case LibFunc::strtoll:
845 case LibFunc::strtold:
Meador Ingecf47ce62013-03-21 00:55:59 +0000846 case LibFunc::strtoull:
847 if (FTy->getNumParams() < 2 ||
848 !FTy->getParamType(1)->isPointerTy())
849 return false;
850 setDoesNotThrow(F);
851 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000852 setOnlyReadsMemory(F, 1);
853 break;
854 case LibFunc::strcpy:
855 case LibFunc::stpcpy:
856 case LibFunc::strcat:
857 case LibFunc::strncat:
858 case LibFunc::strncpy:
859 case LibFunc::stpncpy:
860 if (FTy->getNumParams() < 2 ||
861 !FTy->getParamType(1)->isPointerTy())
862 return false;
863 setDoesNotThrow(F);
864 setDoesNotCapture(F, 2);
865 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000866 break;
867 case LibFunc::strxfrm:
868 if (FTy->getNumParams() != 3 ||
869 !FTy->getParamType(0)->isPointerTy() ||
870 !FTy->getParamType(1)->isPointerTy())
871 return false;
872 setDoesNotThrow(F);
873 setDoesNotCapture(F, 1);
874 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000875 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000876 break;
Nick Lewyckydc897372013-07-06 00:29:58 +0000877 case LibFunc::strcmp: //0,1
878 case LibFunc::strspn: // 0,1
879 case LibFunc::strncmp: // 0,1
880 case LibFunc::strcspn: //0,1
881 case LibFunc::strcoll: //0,1
882 case LibFunc::strcasecmp: // 0,1
883 case LibFunc::strncasecmp: //
Meador Ingecf47ce62013-03-21 00:55:59 +0000884 if (FTy->getNumParams() < 2 ||
885 !FTy->getParamType(0)->isPointerTy() ||
886 !FTy->getParamType(1)->isPointerTy())
887 return false;
888 setOnlyReadsMemory(F);
889 setDoesNotThrow(F);
890 setDoesNotCapture(F, 1);
891 setDoesNotCapture(F, 2);
892 break;
893 case LibFunc::strstr:
894 case LibFunc::strpbrk:
895 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
896 return false;
897 setOnlyReadsMemory(F);
898 setDoesNotThrow(F);
899 setDoesNotCapture(F, 2);
900 break;
901 case LibFunc::strtok:
902 case LibFunc::strtok_r:
903 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
904 return false;
905 setDoesNotThrow(F);
906 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000907 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000908 break;
909 case LibFunc::scanf:
Nick Lewyckydc897372013-07-06 00:29:58 +0000910 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
911 return false;
912 setDoesNotThrow(F);
913 setDoesNotCapture(F, 1);
914 setOnlyReadsMemory(F, 1);
915 break;
Meador Ingecf47ce62013-03-21 00:55:59 +0000916 case LibFunc::setbuf:
917 case LibFunc::setvbuf:
918 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
919 return false;
920 setDoesNotThrow(F);
921 setDoesNotCapture(F, 1);
922 break;
923 case LibFunc::strdup:
924 case LibFunc::strndup:
925 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
926 !FTy->getParamType(0)->isPointerTy())
927 return false;
928 setDoesNotThrow(F);
929 setDoesNotAlias(F, 0);
930 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +0000931 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +0000932 break;
933 case LibFunc::stat:
Meador Ingecf47ce62013-03-21 00:55:59 +0000934 case LibFunc::statvfs:
935 if (FTy->getNumParams() < 2 ||
936 !FTy->getParamType(0)->isPointerTy() ||
937 !FTy->getParamType(1)->isPointerTy())
938 return false;
939 setDoesNotThrow(F);
940 setDoesNotCapture(F, 1);
941 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +0000942 setOnlyReadsMemory(F, 1);
943 break;
944 case LibFunc::sscanf:
945 if (FTy->getNumParams() < 2 ||
946 !FTy->getParamType(0)->isPointerTy() ||
947 !FTy->getParamType(1)->isPointerTy())
948 return false;
949 setDoesNotThrow(F);
950 setDoesNotCapture(F, 1);
951 setDoesNotCapture(F, 2);
952 setOnlyReadsMemory(F, 1);
953 setOnlyReadsMemory(F, 2);
954 break;
955 case LibFunc::sprintf:
956 if (FTy->getNumParams() < 2 ||
957 !FTy->getParamType(0)->isPointerTy() ||
958 !FTy->getParamType(1)->isPointerTy())
959 return false;
960 setDoesNotThrow(F);
961 setDoesNotCapture(F, 1);
962 setDoesNotCapture(F, 2);
963 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000964 break;
965 case LibFunc::snprintf:
966 if (FTy->getNumParams() != 3 ||
967 !FTy->getParamType(0)->isPointerTy() ||
968 !FTy->getParamType(2)->isPointerTy())
969 return false;
970 setDoesNotThrow(F);
971 setDoesNotCapture(F, 1);
972 setDoesNotCapture(F, 3);
Nick Lewyckydc897372013-07-06 00:29:58 +0000973 setOnlyReadsMemory(F, 3);
Meador Ingecf47ce62013-03-21 00:55:59 +0000974 break;
975 case LibFunc::setitimer:
976 if (FTy->getNumParams() != 3 ||
977 !FTy->getParamType(1)->isPointerTy() ||
978 !FTy->getParamType(2)->isPointerTy())
979 return false;
980 setDoesNotThrow(F);
981 setDoesNotCapture(F, 2);
982 setDoesNotCapture(F, 3);
Nick Lewyckydc897372013-07-06 00:29:58 +0000983 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +0000984 break;
985 case LibFunc::system:
986 if (FTy->getNumParams() != 1 ||
987 !FTy->getParamType(0)->isPointerTy())
988 return false;
989 // May throw; "system" is a valid pthread cancellation point.
990 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +0000991 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +0000992 break;
993 case LibFunc::malloc:
994 if (FTy->getNumParams() != 1 ||
995 !FTy->getReturnType()->isPointerTy())
996 return false;
997 setDoesNotThrow(F);
998 setDoesNotAlias(F, 0);
999 break;
1000 case LibFunc::memcmp:
1001 if (FTy->getNumParams() != 3 ||
1002 !FTy->getParamType(0)->isPointerTy() ||
1003 !FTy->getParamType(1)->isPointerTy())
1004 return false;
1005 setOnlyReadsMemory(F);
1006 setDoesNotThrow(F);
1007 setDoesNotCapture(F, 1);
1008 setDoesNotCapture(F, 2);
1009 break;
1010 case LibFunc::memchr:
1011 case LibFunc::memrchr:
1012 if (FTy->getNumParams() != 3)
1013 return false;
1014 setOnlyReadsMemory(F);
1015 setDoesNotThrow(F);
1016 break;
1017 case LibFunc::modf:
1018 case LibFunc::modff:
1019 case LibFunc::modfl:
Nick Lewyckydc897372013-07-06 00:29:58 +00001020 if (FTy->getNumParams() < 2 ||
1021 !FTy->getParamType(1)->isPointerTy())
1022 return false;
1023 setDoesNotThrow(F);
1024 setDoesNotCapture(F, 2);
1025 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001026 case LibFunc::memcpy:
1027 case LibFunc::memccpy:
1028 case LibFunc::memmove:
1029 if (FTy->getNumParams() < 2 ||
1030 !FTy->getParamType(1)->isPointerTy())
1031 return false;
1032 setDoesNotThrow(F);
1033 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001034 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001035 break;
1036 case LibFunc::memalign:
1037 if (!FTy->getReturnType()->isPointerTy())
1038 return false;
1039 setDoesNotAlias(F, 0);
1040 break;
1041 case LibFunc::mkdir:
Nick Lewyckydc897372013-07-06 00:29:58 +00001042 if (FTy->getNumParams() == 0 ||
1043 !FTy->getParamType(0)->isPointerTy())
1044 return false;
1045 setDoesNotThrow(F);
1046 setDoesNotCapture(F, 1);
1047 setOnlyReadsMemory(F, 1);
1048 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001049 case LibFunc::mktime:
1050 if (FTy->getNumParams() == 0 ||
1051 !FTy->getParamType(0)->isPointerTy())
1052 return false;
1053 setDoesNotThrow(F);
1054 setDoesNotCapture(F, 1);
1055 break;
1056 case LibFunc::realloc:
1057 if (FTy->getNumParams() != 2 ||
1058 !FTy->getParamType(0)->isPointerTy() ||
1059 !FTy->getReturnType()->isPointerTy())
1060 return false;
1061 setDoesNotThrow(F);
1062 setDoesNotAlias(F, 0);
1063 setDoesNotCapture(F, 1);
1064 break;
1065 case LibFunc::read:
1066 if (FTy->getNumParams() != 3 ||
1067 !FTy->getParamType(1)->isPointerTy())
1068 return false;
1069 // May throw; "read" is a valid pthread cancellation point.
1070 setDoesNotCapture(F, 2);
1071 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001072 case LibFunc::rewind:
Nick Lewyckydc897372013-07-06 00:29:58 +00001073 if (FTy->getNumParams() < 1 ||
1074 !FTy->getParamType(0)->isPointerTy())
1075 return false;
1076 setDoesNotThrow(F);
1077 setDoesNotCapture(F, 1);
1078 break;
1079 case LibFunc::rmdir:
Meador Ingecf47ce62013-03-21 00:55:59 +00001080 case LibFunc::remove:
1081 case LibFunc::realpath:
1082 if (FTy->getNumParams() < 1 ||
1083 !FTy->getParamType(0)->isPointerTy())
1084 return false;
1085 setDoesNotThrow(F);
1086 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001087 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001088 break;
1089 case LibFunc::rename:
Nick Lewyckydc897372013-07-06 00:29:58 +00001090 if (FTy->getNumParams() < 2 ||
1091 !FTy->getParamType(0)->isPointerTy() ||
1092 !FTy->getParamType(1)->isPointerTy())
1093 return false;
1094 setDoesNotThrow(F);
1095 setDoesNotCapture(F, 1);
1096 setDoesNotCapture(F, 2);
1097 setOnlyReadsMemory(F, 1);
1098 setOnlyReadsMemory(F, 2);
1099 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001100 case LibFunc::readlink:
1101 if (FTy->getNumParams() < 2 ||
1102 !FTy->getParamType(0)->isPointerTy() ||
1103 !FTy->getParamType(1)->isPointerTy())
1104 return false;
1105 setDoesNotThrow(F);
1106 setDoesNotCapture(F, 1);
1107 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001108 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001109 break;
1110 case LibFunc::write:
1111 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1112 return false;
1113 // May throw; "write" is a valid pthread cancellation point.
1114 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001115 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001116 break;
1117 case LibFunc::bcopy:
1118 if (FTy->getNumParams() != 3 ||
1119 !FTy->getParamType(0)->isPointerTy() ||
1120 !FTy->getParamType(1)->isPointerTy())
1121 return false;
1122 setDoesNotThrow(F);
1123 setDoesNotCapture(F, 1);
1124 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001125 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001126 break;
1127 case LibFunc::bcmp:
1128 if (FTy->getNumParams() != 3 ||
1129 !FTy->getParamType(0)->isPointerTy() ||
1130 !FTy->getParamType(1)->isPointerTy())
1131 return false;
1132 setDoesNotThrow(F);
1133 setOnlyReadsMemory(F);
1134 setDoesNotCapture(F, 1);
1135 setDoesNotCapture(F, 2);
1136 break;
1137 case LibFunc::bzero:
1138 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1139 return false;
1140 setDoesNotThrow(F);
1141 setDoesNotCapture(F, 1);
1142 break;
1143 case LibFunc::calloc:
1144 if (FTy->getNumParams() != 2 ||
1145 !FTy->getReturnType()->isPointerTy())
1146 return false;
1147 setDoesNotThrow(F);
1148 setDoesNotAlias(F, 0);
1149 break;
1150 case LibFunc::chmod:
1151 case LibFunc::chown:
Nick Lewyckydc897372013-07-06 00:29:58 +00001152 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1153 return false;
1154 setDoesNotThrow(F);
1155 setDoesNotCapture(F, 1);
1156 setOnlyReadsMemory(F, 1);
1157 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001158 case LibFunc::ctermid:
1159 case LibFunc::clearerr:
1160 case LibFunc::closedir:
1161 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1162 return false;
1163 setDoesNotThrow(F);
1164 setDoesNotCapture(F, 1);
1165 break;
1166 case LibFunc::atoi:
1167 case LibFunc::atol:
1168 case LibFunc::atof:
1169 case LibFunc::atoll:
1170 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1171 return false;
1172 setDoesNotThrow(F);
1173 setOnlyReadsMemory(F);
1174 setDoesNotCapture(F, 1);
1175 break;
1176 case LibFunc::access:
1177 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1178 return false;
1179 setDoesNotThrow(F);
1180 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001181 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001182 break;
1183 case LibFunc::fopen:
1184 if (FTy->getNumParams() != 2 ||
1185 !FTy->getReturnType()->isPointerTy() ||
1186 !FTy->getParamType(0)->isPointerTy() ||
1187 !FTy->getParamType(1)->isPointerTy())
1188 return false;
1189 setDoesNotThrow(F);
1190 setDoesNotAlias(F, 0);
1191 setDoesNotCapture(F, 1);
1192 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001193 setOnlyReadsMemory(F, 1);
1194 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001195 break;
1196 case LibFunc::fdopen:
1197 if (FTy->getNumParams() != 2 ||
1198 !FTy->getReturnType()->isPointerTy() ||
1199 !FTy->getParamType(1)->isPointerTy())
1200 return false;
1201 setDoesNotThrow(F);
1202 setDoesNotAlias(F, 0);
1203 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001204 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001205 break;
1206 case LibFunc::feof:
1207 case LibFunc::free:
1208 case LibFunc::fseek:
1209 case LibFunc::ftell:
1210 case LibFunc::fgetc:
1211 case LibFunc::fseeko:
1212 case LibFunc::ftello:
1213 case LibFunc::fileno:
1214 case LibFunc::fflush:
1215 case LibFunc::fclose:
1216 case LibFunc::fsetpos:
1217 case LibFunc::flockfile:
1218 case LibFunc::funlockfile:
1219 case LibFunc::ftrylockfile:
1220 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1221 return false;
1222 setDoesNotThrow(F);
1223 setDoesNotCapture(F, 1);
1224 break;
1225 case LibFunc::ferror:
1226 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1227 return false;
1228 setDoesNotThrow(F);
1229 setDoesNotCapture(F, 1);
1230 setOnlyReadsMemory(F);
1231 break;
1232 case LibFunc::fputc:
1233 case LibFunc::fstat:
1234 case LibFunc::frexp:
1235 case LibFunc::frexpf:
1236 case LibFunc::frexpl:
1237 case LibFunc::fstatvfs:
1238 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1239 return false;
1240 setDoesNotThrow(F);
1241 setDoesNotCapture(F, 2);
1242 break;
1243 case LibFunc::fgets:
1244 if (FTy->getNumParams() != 3 ||
1245 !FTy->getParamType(0)->isPointerTy() ||
1246 !FTy->getParamType(2)->isPointerTy())
1247 return false;
1248 setDoesNotThrow(F);
1249 setDoesNotCapture(F, 3);
Nick Lewyckye7dd3af2013-07-02 05:02:56 +00001250 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001251 case LibFunc::fread:
Nick Lewyckydc897372013-07-06 00:29:58 +00001252 if (FTy->getNumParams() != 4 ||
1253 !FTy->getParamType(0)->isPointerTy() ||
1254 !FTy->getParamType(3)->isPointerTy())
1255 return false;
1256 setDoesNotThrow(F);
1257 setDoesNotCapture(F, 1);
1258 setDoesNotCapture(F, 4);
1259 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001260 case LibFunc::fwrite:
1261 if (FTy->getNumParams() != 4 ||
1262 !FTy->getParamType(0)->isPointerTy() ||
1263 !FTy->getParamType(3)->isPointerTy())
1264 return false;
1265 setDoesNotThrow(F);
1266 setDoesNotCapture(F, 1);
1267 setDoesNotCapture(F, 4);
Nick Lewyckye7dd3af2013-07-02 05:02:56 +00001268 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001269 case LibFunc::fputs:
Nick Lewyckydc897372013-07-06 00:29:58 +00001270 if (FTy->getNumParams() < 2 ||
1271 !FTy->getParamType(0)->isPointerTy() ||
1272 !FTy->getParamType(1)->isPointerTy())
1273 return false;
1274 setDoesNotThrow(F);
1275 setDoesNotCapture(F, 1);
1276 setDoesNotCapture(F, 2);
1277 setOnlyReadsMemory(F, 1);
1278 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001279 case LibFunc::fscanf:
1280 case LibFunc::fprintf:
Nick Lewyckydc897372013-07-06 00:29:58 +00001281 if (FTy->getNumParams() < 2 ||
1282 !FTy->getParamType(0)->isPointerTy() ||
1283 !FTy->getParamType(1)->isPointerTy())
1284 return false;
1285 setDoesNotThrow(F);
1286 setDoesNotCapture(F, 1);
1287 setDoesNotCapture(F, 2);
1288 setOnlyReadsMemory(F, 2);
1289 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001290 case LibFunc::fgetpos:
1291 if (FTy->getNumParams() < 2 ||
1292 !FTy->getParamType(0)->isPointerTy() ||
1293 !FTy->getParamType(1)->isPointerTy())
1294 return false;
1295 setDoesNotThrow(F);
1296 setDoesNotCapture(F, 1);
1297 setDoesNotCapture(F, 2);
1298 break;
1299 case LibFunc::getc:
1300 case LibFunc::getlogin_r:
1301 case LibFunc::getc_unlocked:
1302 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1303 return false;
1304 setDoesNotThrow(F);
1305 setDoesNotCapture(F, 1);
1306 break;
1307 case LibFunc::getenv:
1308 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1309 return false;
1310 setDoesNotThrow(F);
1311 setOnlyReadsMemory(F);
1312 setDoesNotCapture(F, 1);
1313 break;
1314 case LibFunc::gets:
1315 case LibFunc::getchar:
1316 setDoesNotThrow(F);
1317 break;
1318 case LibFunc::getitimer:
1319 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1320 return false;
1321 setDoesNotThrow(F);
1322 setDoesNotCapture(F, 2);
1323 break;
1324 case LibFunc::getpwnam:
1325 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1326 return false;
1327 setDoesNotThrow(F);
1328 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001329 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001330 break;
1331 case LibFunc::ungetc:
1332 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1333 return false;
1334 setDoesNotThrow(F);
1335 setDoesNotCapture(F, 2);
1336 break;
1337 case LibFunc::uname:
Nick Lewyckydc897372013-07-06 00:29:58 +00001338 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1339 return false;
1340 setDoesNotThrow(F);
1341 setDoesNotCapture(F, 1);
1342 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001343 case LibFunc::unlink:
Nick Lewyckydc897372013-07-06 00:29:58 +00001344 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1345 return false;
1346 setDoesNotThrow(F);
1347 setDoesNotCapture(F, 1);
Nick Lewycky60ceb6e2013-07-06 00:59:28 +00001348 setOnlyReadsMemory(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001349 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001350 case LibFunc::unsetenv:
1351 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1352 return false;
1353 setDoesNotThrow(F);
1354 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001355 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001356 break;
1357 case LibFunc::utime:
1358 case LibFunc::utimes:
1359 if (FTy->getNumParams() != 2 ||
1360 !FTy->getParamType(0)->isPointerTy() ||
1361 !FTy->getParamType(1)->isPointerTy())
1362 return false;
1363 setDoesNotThrow(F);
1364 setDoesNotCapture(F, 1);
1365 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001366 setOnlyReadsMemory(F, 1);
1367 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001368 break;
1369 case LibFunc::putc:
1370 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1371 return false;
1372 setDoesNotThrow(F);
1373 setDoesNotCapture(F, 2);
1374 break;
1375 case LibFunc::puts:
1376 case LibFunc::printf:
1377 case LibFunc::perror:
1378 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1379 return false;
1380 setDoesNotThrow(F);
1381 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001382 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001383 break;
1384 case LibFunc::pread:
Nick Lewyckydc897372013-07-06 00:29:58 +00001385 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1386 return false;
1387 // May throw; "pread" is a valid pthread cancellation point.
1388 setDoesNotCapture(F, 2);
1389 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001390 case LibFunc::pwrite:
1391 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1392 return false;
Nick Lewyckydc897372013-07-06 00:29:58 +00001393 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Ingecf47ce62013-03-21 00:55:59 +00001394 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001395 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001396 break;
1397 case LibFunc::putchar:
1398 setDoesNotThrow(F);
1399 break;
1400 case LibFunc::popen:
1401 if (FTy->getNumParams() != 2 ||
1402 !FTy->getReturnType()->isPointerTy() ||
1403 !FTy->getParamType(0)->isPointerTy() ||
1404 !FTy->getParamType(1)->isPointerTy())
1405 return false;
1406 setDoesNotThrow(F);
1407 setDoesNotAlias(F, 0);
1408 setDoesNotCapture(F, 1);
1409 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001410 setOnlyReadsMemory(F, 1);
1411 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001412 break;
1413 case LibFunc::pclose:
1414 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1415 return false;
1416 setDoesNotThrow(F);
1417 setDoesNotCapture(F, 1);
1418 break;
1419 case LibFunc::vscanf:
1420 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1421 return false;
1422 setDoesNotThrow(F);
1423 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001424 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001425 break;
1426 case LibFunc::vsscanf:
Nick Lewyckydc897372013-07-06 00:29:58 +00001427 if (FTy->getNumParams() != 3 ||
1428 !FTy->getParamType(1)->isPointerTy() ||
1429 !FTy->getParamType(2)->isPointerTy())
1430 return false;
1431 setDoesNotThrow(F);
1432 setDoesNotCapture(F, 1);
1433 setDoesNotCapture(F, 2);
1434 setOnlyReadsMemory(F, 1);
1435 setOnlyReadsMemory(F, 2);
1436 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001437 case LibFunc::vfscanf:
1438 if (FTy->getNumParams() != 3 ||
1439 !FTy->getParamType(1)->isPointerTy() ||
1440 !FTy->getParamType(2)->isPointerTy())
1441 return false;
1442 setDoesNotThrow(F);
1443 setDoesNotCapture(F, 1);
1444 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001445 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001446 break;
1447 case LibFunc::valloc:
1448 if (!FTy->getReturnType()->isPointerTy())
1449 return false;
1450 setDoesNotThrow(F);
1451 setDoesNotAlias(F, 0);
1452 break;
1453 case LibFunc::vprintf:
1454 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1455 return false;
1456 setDoesNotThrow(F);
1457 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001458 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001459 break;
1460 case LibFunc::vfprintf:
1461 case LibFunc::vsprintf:
1462 if (FTy->getNumParams() != 3 ||
1463 !FTy->getParamType(0)->isPointerTy() ||
1464 !FTy->getParamType(1)->isPointerTy())
1465 return false;
1466 setDoesNotThrow(F);
1467 setDoesNotCapture(F, 1);
1468 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001469 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001470 break;
1471 case LibFunc::vsnprintf:
1472 if (FTy->getNumParams() != 4 ||
1473 !FTy->getParamType(0)->isPointerTy() ||
1474 !FTy->getParamType(2)->isPointerTy())
1475 return false;
1476 setDoesNotThrow(F);
1477 setDoesNotCapture(F, 1);
1478 setDoesNotCapture(F, 3);
Nick Lewyckydc897372013-07-06 00:29:58 +00001479 setOnlyReadsMemory(F, 3);
Meador Ingecf47ce62013-03-21 00:55:59 +00001480 break;
1481 case LibFunc::open:
1482 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1483 return false;
1484 // May throw; "open" is a valid pthread cancellation point.
1485 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001486 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001487 break;
1488 case LibFunc::opendir:
1489 if (FTy->getNumParams() != 1 ||
1490 !FTy->getReturnType()->isPointerTy() ||
1491 !FTy->getParamType(0)->isPointerTy())
1492 return false;
1493 setDoesNotThrow(F);
1494 setDoesNotAlias(F, 0);
1495 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001496 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001497 break;
1498 case LibFunc::tmpfile:
1499 if (!FTy->getReturnType()->isPointerTy())
1500 return false;
1501 setDoesNotThrow(F);
1502 setDoesNotAlias(F, 0);
1503 break;
1504 case LibFunc::times:
1505 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1506 return false;
1507 setDoesNotThrow(F);
1508 setDoesNotCapture(F, 1);
1509 break;
1510 case LibFunc::htonl:
1511 case LibFunc::htons:
1512 case LibFunc::ntohl:
1513 case LibFunc::ntohs:
1514 setDoesNotThrow(F);
1515 setDoesNotAccessMemory(F);
1516 break;
1517 case LibFunc::lstat:
1518 if (FTy->getNumParams() != 2 ||
1519 !FTy->getParamType(0)->isPointerTy() ||
1520 !FTy->getParamType(1)->isPointerTy())
1521 return false;
1522 setDoesNotThrow(F);
1523 setDoesNotCapture(F, 1);
1524 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001525 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001526 break;
1527 case LibFunc::lchown:
1528 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1529 return false;
1530 setDoesNotThrow(F);
1531 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001532 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001533 break;
1534 case LibFunc::qsort:
1535 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1536 return false;
1537 // May throw; places call through function pointer.
1538 setDoesNotCapture(F, 4);
1539 break;
1540 case LibFunc::dunder_strdup:
1541 case LibFunc::dunder_strndup:
1542 if (FTy->getNumParams() < 1 ||
1543 !FTy->getReturnType()->isPointerTy() ||
1544 !FTy->getParamType(0)->isPointerTy())
1545 return false;
1546 setDoesNotThrow(F);
1547 setDoesNotAlias(F, 0);
1548 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001549 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001550 break;
1551 case LibFunc::dunder_strtok_r:
1552 if (FTy->getNumParams() != 3 ||
1553 !FTy->getParamType(1)->isPointerTy())
1554 return false;
1555 setDoesNotThrow(F);
1556 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001557 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001558 break;
1559 case LibFunc::under_IO_getc:
1560 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1561 return false;
1562 setDoesNotThrow(F);
1563 setDoesNotCapture(F, 1);
1564 break;
1565 case LibFunc::under_IO_putc:
1566 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1567 return false;
1568 setDoesNotThrow(F);
1569 setDoesNotCapture(F, 2);
1570 break;
1571 case LibFunc::dunder_isoc99_scanf:
1572 if (FTy->getNumParams() < 1 ||
1573 !FTy->getParamType(0)->isPointerTy())
1574 return false;
1575 setDoesNotThrow(F);
1576 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001577 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001578 break;
1579 case LibFunc::stat64:
1580 case LibFunc::lstat64:
1581 case LibFunc::statvfs64:
Nick Lewyckydc897372013-07-06 00:29:58 +00001582 if (FTy->getNumParams() < 1 ||
1583 !FTy->getParamType(0)->isPointerTy() ||
1584 !FTy->getParamType(1)->isPointerTy())
1585 return false;
1586 setDoesNotThrow(F);
1587 setDoesNotCapture(F, 1);
1588 setDoesNotCapture(F, 2);
1589 setOnlyReadsMemory(F, 1);
1590 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001591 case LibFunc::dunder_isoc99_sscanf:
1592 if (FTy->getNumParams() < 1 ||
1593 !FTy->getParamType(0)->isPointerTy() ||
1594 !FTy->getParamType(1)->isPointerTy())
1595 return false;
1596 setDoesNotThrow(F);
1597 setDoesNotCapture(F, 1);
1598 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001599 setOnlyReadsMemory(F, 1);
1600 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001601 break;
1602 case LibFunc::fopen64:
1603 if (FTy->getNumParams() != 2 ||
1604 !FTy->getReturnType()->isPointerTy() ||
1605 !FTy->getParamType(0)->isPointerTy() ||
1606 !FTy->getParamType(1)->isPointerTy())
1607 return false;
1608 setDoesNotThrow(F);
1609 setDoesNotAlias(F, 0);
1610 setDoesNotCapture(F, 1);
1611 setDoesNotCapture(F, 2);
Nick Lewyckydc897372013-07-06 00:29:58 +00001612 setOnlyReadsMemory(F, 1);
1613 setOnlyReadsMemory(F, 2);
Meador Ingecf47ce62013-03-21 00:55:59 +00001614 break;
1615 case LibFunc::fseeko64:
1616 case LibFunc::ftello64:
1617 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1618 return false;
1619 setDoesNotThrow(F);
1620 setDoesNotCapture(F, 1);
1621 break;
1622 case LibFunc::tmpfile64:
1623 if (!FTy->getReturnType()->isPointerTy())
1624 return false;
1625 setDoesNotThrow(F);
1626 setDoesNotAlias(F, 0);
1627 break;
1628 case LibFunc::fstat64:
1629 case LibFunc::fstatvfs64:
1630 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1631 return false;
1632 setDoesNotThrow(F);
1633 setDoesNotCapture(F, 2);
1634 break;
1635 case LibFunc::open64:
1636 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1637 return false;
1638 // May throw; "open" is a valid pthread cancellation point.
1639 setDoesNotCapture(F, 1);
Nick Lewyckydc897372013-07-06 00:29:58 +00001640 setOnlyReadsMemory(F, 1);
Meador Ingecf47ce62013-03-21 00:55:59 +00001641 break;
Michael Gottesman7cb03212013-07-03 04:00:54 +00001642 case LibFunc::gettimeofday:
1643 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1644 !FTy->getParamType(1)->isPointerTy())
1645 return false;
1646 // Currently some platforms have the restrict keyword on the arguments to
1647 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1648 // arguments.
1649 setDoesNotThrow(F);
1650 setDoesNotCapture(F, 1);
1651 setDoesNotCapture(F, 2);
Stephen Hines36b56882014-04-23 16:57:46 -07001652 break;
Meador Ingecf47ce62013-03-21 00:55:59 +00001653 default:
1654 // Didn't mark any attributes.
1655 return false;
1656 }
1657
1658 return true;
1659}
1660
1661/// annotateLibraryCalls - Adds attributes to well-known standard library
1662/// call declarations.
1663bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1664 bool MadeChange = false;
1665
1666 // Check each function in turn annotating well-known library function
1667 // declarations with attributes.
1668 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1669 Function *F = (*I)->getFunction();
1670
1671 if (F != 0 && F->isDeclaration())
1672 MadeChange |= inferPrototypeAttributes(*F);
1673 }
1674
1675 return MadeChange;
1676}
1677
Chris Lattner2decb222010-04-16 22:42:17 +00001678bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman3c97f7a2010-11-08 16:10:15 +00001679 AA = &getAnalysis<AliasAnalysis>();
Meador Ingecf47ce62013-03-21 00:55:59 +00001680 TLI = &getAnalysis<TargetLibraryInfo>();
Dan Gohman3c97f7a2010-11-08 16:10:15 +00001681
Meador Ingecf47ce62013-03-21 00:55:59 +00001682 bool Changed = annotateLibraryCalls(SCC);
1683 Changed |= AddReadAttrs(SCC);
Nick Lewyckydc897372013-07-06 00:29:58 +00001684 Changed |= AddArgumentAttrs(SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +00001685 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +00001686 return Changed;
1687}