blob: fed8839e6f0f708a3b4b3de4a3477dd6d5b23f5b [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
Duncan Sands9e89ba32008-12-31 16:14:43 +000021#include "llvm/Transforms/IPO.h"
Nick Lewyckyb48a1892011-12-28 23:24:21 +000022#include "llvm/ADT/SCCIterator.h"
Benjamin Kramerb4c9d9c2012-10-31 13:45:49 +000023#include "llvm/ADT/SetVector.h"
Duncan Sands338cd6b2009-01-02 11:54:37 +000024#include "llvm/ADT/SmallSet.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000025#include "llvm/ADT/Statistic.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000026#include "llvm/Analysis/AliasAnalysis.h"
27#include "llvm/Analysis/CallGraph.h"
Chandler Carruth3251e812013-01-07 15:26:48 +000028#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthd04a8d42012-12-03 16:50:05 +000029#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000030#include "llvm/IR/GlobalVariable.h"
Stephen Hines36b56882014-04-23 16:57:46 -070031#include "llvm/IR/InstIterator.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000032#include "llvm/IR/IntrinsicInst.h"
33#include "llvm/IR/LLVMContext.h"
Meador Ingecf47ce62013-03-21 00:55:59 +000034#include "llvm/Target/TargetLibraryInfo.h"
Duncan Sands9e89ba32008-12-31 16:14:43 +000035using namespace llvm;
36
Stephen Hinesdce4a402014-05-29 02:49:00 -070037#define DEBUG_TYPE "functionattrs"
38
Duncan Sands9e89ba32008-12-31 16:14:43 +000039STATISTIC(NumReadNone, "Number of functions marked readnone");
40STATISTIC(NumReadOnly, "Number of functions marked readonly");
41STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewyckydc897372013-07-06 00:29:58 +000042STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
43STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewycky199aa3c2009-03-08 06:20:47 +000044STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Meador Ingecf47ce62013-03-21 00:55:59 +000045STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Duncan Sands9e89ba32008-12-31 16:14:43 +000046
47namespace {
Nick Lewycky6726b6d2009-10-25 06:33:48 +000048 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands9e89ba32008-12-31 16:14:43 +000049 static char ID; // Pass identification, replacement for typeid
Stephen Hinesdce4a402014-05-29 02:49:00 -070050 FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) {
Owen Anderson081c34b2010-10-19 17:21:58 +000051 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
52 }
Duncan Sands9e89ba32008-12-31 16:14:43 +000053
54 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Stephen Hines36b56882014-04-23 16:57:46 -070055 bool runOnSCC(CallGraphSCC &SCC) override;
Duncan Sands9e89ba32008-12-31 16:14:43 +000056
57 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000058 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000059
Nick Lewyckydc897372013-07-06 00:29:58 +000060 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
61 bool AddArgumentAttrs(const CallGraphSCC &SCC);
Duncan Sands9e89ba32008-12-31 16:14:43 +000062
Nick Lewycky199aa3c2009-03-08 06:20:47 +000063 // IsFunctionMallocLike - Does this function allocate new memory?
64 bool IsFunctionMallocLike(Function *F,
Chris Lattner98a27ce2009-08-31 04:09:04 +000065 SmallPtrSet<Function*, 8> &) const;
Nick Lewycky199aa3c2009-03-08 06:20:47 +000066
67 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +000068 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewycky199aa3c2009-03-08 06:20:47 +000069
Meador Ingecf47ce62013-03-21 00:55:59 +000070 // Utility methods used by inferPrototypeAttributes to add attributes
71 // and maintain annotation statistics.
72
73 void setDoesNotAccessMemory(Function &F) {
74 if (!F.doesNotAccessMemory()) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000075 F.setDoesNotAccessMemory();
76 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000077 }
78 }
79
80 void setOnlyReadsMemory(Function &F) {
81 if (!F.onlyReadsMemory()) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000082 F.setOnlyReadsMemory();
83 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000084 }
85 }
86
87 void setDoesNotThrow(Function &F) {
88 if (!F.doesNotThrow()) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000089 F.setDoesNotThrow();
90 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000091 }
92 }
93
94 void setDoesNotCapture(Function &F, unsigned n) {
95 if (!F.doesNotCapture(n)) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +000096 F.setDoesNotCapture(n);
97 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +000098 }
99 }
100
Nick Lewyckydc897372013-07-06 00:29:58 +0000101 void setOnlyReadsMemory(Function &F, unsigned n) {
102 if (!F.onlyReadsMemory(n)) {
103 F.setOnlyReadsMemory(n);
104 ++NumAnnotated;
105 }
106 }
107
Meador Ingecf47ce62013-03-21 00:55:59 +0000108 void setDoesNotAlias(Function &F, unsigned n) {
109 if (!F.doesNotAlias(n)) {
Nick Lewycky08bdfe22013-07-04 03:51:53 +0000110 F.setDoesNotAlias(n);
111 ++NumAnnotated;
Meador Ingecf47ce62013-03-21 00:55:59 +0000112 }
113 }
114
115 // inferPrototypeAttributes - Analyze the name and prototype of the
116 // given function and set any applicable attributes. Returns true
117 // if any attributes were set and false otherwise.
118 bool inferPrototypeAttributes(Function &F);
119
120 // annotateLibraryCalls - Adds attributes to well-known standard library
121 // call declarations.
122 bool annotateLibraryCalls(const CallGraphSCC &SCC);
123
Stephen Hines36b56882014-04-23 16:57:46 -0700124 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000125 AU.setPreservesCFG();
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000126 AU.addRequired<AliasAnalysis>();
Meador Ingecf47ce62013-03-21 00:55:59 +0000127 AU.addRequired<TargetLibraryInfo>();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000128 CallGraphSCCPass::getAnalysisUsage(AU);
129 }
130
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000131 private:
132 AliasAnalysis *AA;
Meador Ingecf47ce62013-03-21 00:55:59 +0000133 TargetLibraryInfo *TLI;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000134 };
135}
136
137char FunctionAttrs::ID = 0;
Owen Andersonae0a7bc2010-10-13 22:00:45 +0000138INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
139 "Deduce function attributes", false, false)
Nick Lewyckya83aeae2013-09-05 08:19:58 +0000140INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Stephen Hines36b56882014-04-23 16:57:46 -0700141INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Meador Ingecf47ce62013-03-21 00:55:59 +0000142INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Owen Andersonae0a7bc2010-10-13 22:00:45 +0000143INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersonce665bd2010-10-07 22:25:06 +0000144 "Deduce function attributes", false, false)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000145
146Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
147
148
Duncan Sands9e89ba32008-12-31 16:14:43 +0000149/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000150bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner98a27ce2009-08-31 04:09:04 +0000151 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000152
153 // Fill SCCNodes with the elements of the SCC. Used for quickly
154 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner2decb222010-04-16 22:42:17 +0000155 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
156 SCCNodes.insert((*I)->getFunction());
Duncan Sands9e89ba32008-12-31 16:14:43 +0000157
158 // Check if any of the functions in the SCC read or write memory. If they
159 // write memory then they can't be marked readnone or readonly.
160 bool ReadsMemory = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000161 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
162 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000163
Stephen Hinesdce4a402014-05-29 02:49:00 -0700164 if (!F)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000165 // External node - may write memory. Just give up.
166 return false;
167
Dan Gohman6d44d642010-11-09 20:13:27 +0000168 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
169 if (MRB == AliasAnalysis::DoesNotAccessMemory)
Duncan Sands9e89ba32008-12-31 16:14:43 +0000170 // Already perfect!
171 continue;
172
173 // Definitions with weak linkage may be overridden at linktime with
174 // something that writes memory, so treat them like declarations.
175 if (F->isDeclaration() || F->mayBeOverridden()) {
Dan Gohman6d44d642010-11-09 20:13:27 +0000176 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000177 // May write memory. Just give up.
178 return false;
179
180 ReadsMemory = true;
181 continue;
182 }
183
184 // Scan the function body for instructions that may read or write memory.
185 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
186 Instruction *I = &*II;
187
188 // Some instructions can be ignored even if they read or write memory.
189 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif7d3056b2010-07-28 22:50:26 +0000190 CallSite CS(cast<Value>(I));
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000191 if (CS) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000192 // Ignore calls to functions in the same SCC.
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000193 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands9e89ba32008-12-31 16:14:43 +0000194 continue;
Dan Gohman42c31a72010-11-10 01:02:18 +0000195 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
196 // If the call doesn't access arbitrary memory, we may be able to
197 // figure out something.
Dan Gohman432d08c2010-11-10 17:34:04 +0000198 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
199 // If the call does access argument pointees, check each argument.
Dan Gohman68a60562010-11-10 18:17:28 +0000200 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman42c31a72010-11-10 01:02:18 +0000201 // Check whether all pointer arguments point to local memory, and
202 // ignore calls that only access local memory.
203 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
204 CI != CE; ++CI) {
205 Value *Arg = *CI;
206 if (Arg->getType()->isPointerTy()) {
207 AliasAnalysis::Location Loc(Arg,
208 AliasAnalysis::UnknownSize,
209 I->getMetadata(LLVMContext::MD_tbaa));
210 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
211 if (MRB & AliasAnalysis::Mod)
212 // Writes non-local memory. Give up.
213 return false;
214 if (MRB & AliasAnalysis::Ref)
215 // Ok, it reads non-local memory.
216 ReadsMemory = true;
217 }
Dan Gohman40b6a192010-11-09 19:56:27 +0000218 }
219 }
Dan Gohman40b6a192010-11-09 19:56:27 +0000220 continue;
Dan Gohman3c97f7a2010-11-08 16:10:15 +0000221 }
Dan Gohman42c31a72010-11-10 01:02:18 +0000222 // The call could access any memory. If that includes writes, give up.
223 if (MRB & AliasAnalysis::Mod)
224 return false;
225 // If it reads, note it.
226 if (MRB & AliasAnalysis::Ref)
227 ReadsMemory = true;
228 continue;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000229 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedman2199dfb2011-08-16 01:28:22 +0000230 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
231 if (!LI->isVolatile()) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000232 AliasAnalysis::Location Loc = AA->getLocation(LI);
Dan Gohmanea8900f2010-11-08 17:12:04 +0000233 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
234 continue;
235 }
Duncan Sands9e89ba32008-12-31 16:14:43 +0000236 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedman2199dfb2011-08-16 01:28:22 +0000237 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
238 if (!SI->isVolatile()) {
Dan Gohman6d8eb152010-11-11 21:50:19 +0000239 AliasAnalysis::Location Loc = AA->getLocation(SI);
Dan Gohmanea8900f2010-11-08 17:12:04 +0000240 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
241 continue;
242 }
Dan Gohman4cf0dcf2010-11-09 20:17:38 +0000243 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
244 // Ignore vaargs on local memory.
Dan Gohman6d8eb152010-11-11 21:50:19 +0000245 AliasAnalysis::Location Loc = AA->getLocation(VI);
Dan Gohman4cf0dcf2010-11-09 20:17:38 +0000246 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
247 continue;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000248 }
249
250 // Any remaining instructions need to be taken seriously! Check if they
251 // read or write memory.
252 if (I->mayWriteToMemory())
253 // Writes memory. Just give up.
254 return false;
Duncan Sandscfd0ebe2009-05-06 08:42:00 +0000255
Duncan Sands9e89ba32008-12-31 16:14:43 +0000256 // If this instruction may read memory, remember that.
257 ReadsMemory |= I->mayReadFromMemory();
258 }
259 }
260
261 // Success! Functions in this SCC do not access memory, or only read memory.
262 // Give them the appropriate attribute.
263 bool MadeChange = false;
Chris Lattner2decb222010-04-16 22:42:17 +0000264 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
265 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000266
267 if (F->doesNotAccessMemory())
268 // Already perfect!
269 continue;
270
271 if (F->onlyReadsMemory() && ReadsMemory)
272 // No change.
273 continue;
274
275 MadeChange = true;
276
277 // Clear out any existing attributes.
Bill Wendling702cc912012-10-15 20:35:56 +0000278 AttrBuilder B;
Bill Wendling034b94b2012-12-19 07:18:57 +0000279 B.addAttribute(Attribute::ReadOnly)
280 .addAttribute(Attribute::ReadNone);
Bill Wendling8246df62013-01-23 00:45:55 +0000281 F->removeAttributes(AttributeSet::FunctionIndex,
282 AttributeSet::get(F->getContext(),
283 AttributeSet::FunctionIndex, B));
Duncan Sands9e89ba32008-12-31 16:14:43 +0000284
285 // Add in the new attribute.
Bill Wendling99faa3b2012-12-07 23:16:57 +0000286 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendling70d2ca02013-01-23 00:20:53 +0000287 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands9e89ba32008-12-31 16:14:43 +0000288
289 if (ReadsMemory)
Duncan Sandsb2f22792009-01-02 11:46:24 +0000290 ++NumReadOnly;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000291 else
Duncan Sandsb2f22792009-01-02 11:46:24 +0000292 ++NumReadNone;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000293 }
294
295 return MadeChange;
296}
297
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000298namespace {
299 // For a given pointer Argument, this retains a list of Arguments of functions
300 // in the same SCC that the pointer data flows into. We use this to build an
301 // SCC of the arguments.
302 struct ArgumentGraphNode {
303 Argument *Definition;
304 SmallVector<ArgumentGraphNode*, 4> Uses;
305 };
306
307 class ArgumentGraph {
308 // We store pointers to ArgumentGraphNode objects, so it's important that
309 // that they not move around upon insert.
310 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
311
312 ArgumentMapTy ArgumentMap;
313
314 // There is no root node for the argument graph, in fact:
315 // void f(int *x, int *y) { if (...) f(x, y); }
316 // is an example where the graph is disconnected. The SCCIterator requires a
317 // single entry point, so we maintain a fake ("synthetic") root node that
318 // uses every node. Because the graph is directed and nothing points into
319 // the root, it will not participate in any SCCs (except for its own).
320 ArgumentGraphNode SyntheticRoot;
321
322 public:
Stephen Hinesdce4a402014-05-29 02:49:00 -0700323 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000324
325 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
326
327 iterator begin() { return SyntheticRoot.Uses.begin(); }
328 iterator end() { return SyntheticRoot.Uses.end(); }
329 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
330
331 ArgumentGraphNode *operator[](Argument *A) {
332 ArgumentGraphNode &Node = ArgumentMap[A];
333 Node.Definition = A;
334 SyntheticRoot.Uses.push_back(&Node);
335 return &Node;
336 }
337 };
338
339 // This tracker checks whether callees are in the SCC, and if so it does not
340 // consider that a capture, instead adding it to the "Uses" list and
341 // continuing with the analysis.
342 struct ArgumentUsesTracker : public CaptureTracker {
343 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
344 : Captured(false), SCCNodes(SCCNodes) {}
345
Stephen Hines36b56882014-04-23 16:57:46 -0700346 void tooManyUses() override { Captured = true; }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000347
Stephen Hines36b56882014-04-23 16:57:46 -0700348 bool captured(const Use *U) override {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000349 CallSite CS(U->getUser());
350 if (!CS.getInstruction()) { Captured = true; return true; }
351
352 Function *F = CS.getCalledFunction();
353 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
354
Nick Lewyckydc897372013-07-06 00:29:58 +0000355 bool Found = false;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000356 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
357 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
358 PI != PE; ++PI, ++AI) {
359 if (AI == AE) {
360 assert(F->isVarArg() && "More params than args in non-varargs call");
361 Captured = true;
362 return true;
363 }
364 if (PI == U) {
365 Uses.push_back(AI);
Nick Lewyckydc897372013-07-06 00:29:58 +0000366 Found = true;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000367 break;
368 }
369 }
Nick Lewyckydc897372013-07-06 00:29:58 +0000370 assert(Found && "Capturing call-site captured nothing?");
Duncan Sands9a8392b2013-09-13 08:16:06 +0000371 (void)Found;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000372 return false;
373 }
374
375 bool Captured; // True only if certainly captured (used outside our SCC).
376 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
377
378 const SmallPtrSet<Function*, 8> &SCCNodes;
379 };
380}
381
382namespace llvm {
383 template<> struct GraphTraits<ArgumentGraphNode*> {
384 typedef ArgumentGraphNode NodeType;
385 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
386
387 static inline NodeType *getEntryNode(NodeType *A) { return A; }
388 static inline ChildIteratorType child_begin(NodeType *N) {
389 return N->Uses.begin();
390 }
391 static inline ChildIteratorType child_end(NodeType *N) {
392 return N->Uses.end();
393 }
394 };
395 template<> struct GraphTraits<ArgumentGraph*>
396 : public GraphTraits<ArgumentGraphNode*> {
397 static NodeType *getEntryNode(ArgumentGraph *AG) {
398 return AG->getEntryNode();
399 }
400 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
401 return AG->begin();
402 }
403 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
404 return AG->end();
405 }
406 };
407}
408
Nick Lewyckydc897372013-07-06 00:29:58 +0000409// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
410static Attribute::AttrKind
411determinePointerReadAttrs(Argument *A,
412 const SmallPtrSet<Argument*, 8> &SCCNodes) {
413
414 SmallVector<Use*, 32> Worklist;
415 SmallSet<Use*, 32> Visited;
416 int Count = 0;
417
Stephen Hines36b56882014-04-23 16:57:46 -0700418 // inalloca arguments are always clobbered by the call.
419 if (A->hasInAllocaAttr())
420 return Attribute::None;
421
Nick Lewyckydc897372013-07-06 00:29:58 +0000422 bool IsRead = false;
423 // We don't need to track IsWritten. If A is written to, return immediately.
424
Stephen Hines36b56882014-04-23 16:57:46 -0700425 for (Use &U : A->uses()) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000426 if (Count++ >= 20)
427 return Attribute::None;
428
Stephen Hines36b56882014-04-23 16:57:46 -0700429 Visited.insert(&U);
430 Worklist.push_back(&U);
Nick Lewyckydc897372013-07-06 00:29:58 +0000431 }
432
433 while (!Worklist.empty()) {
434 Use *U = Worklist.pop_back_val();
435 Instruction *I = cast<Instruction>(U->getUser());
436 Value *V = U->get();
437
438 switch (I->getOpcode()) {
439 case Instruction::BitCast:
440 case Instruction::GetElementPtr:
441 case Instruction::PHI:
442 case Instruction::Select:
Stephen Hines36b56882014-04-23 16:57:46 -0700443 case Instruction::AddrSpaceCast:
Nick Lewyckydc897372013-07-06 00:29:58 +0000444 // The original value is not read/written via this if the new value isn't.
Stephen Hines36b56882014-04-23 16:57:46 -0700445 for (Use &UU : I->uses())
446 if (Visited.insert(&UU))
447 Worklist.push_back(&UU);
Nick Lewyckydc897372013-07-06 00:29:58 +0000448 break;
449
450 case Instruction::Call:
451 case Instruction::Invoke: {
452 CallSite CS(I);
453 if (CS.doesNotAccessMemory())
454 continue;
455
456 Function *F = CS.getCalledFunction();
457 if (!F) {
458 if (CS.onlyReadsMemory()) {
459 IsRead = true;
460 continue;
461 }
462 return Attribute::None;
463 }
464
465 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
466 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
467 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
468 if (A->get() == V) {
469 if (AI == AE) {
470 assert(F->isVarArg() &&
471 "More params than args in non-varargs call.");
472 return Attribute::None;
473 }
474 if (SCCNodes.count(AI))
475 continue;
476 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
477 return Attribute::None;
478 if (!CS.doesNotAccessMemory(A - B))
479 IsRead = true;
480 }
481 }
482 break;
483 }
484
485 case Instruction::Load:
486 IsRead = true;
487 break;
488
489 case Instruction::ICmp:
490 case Instruction::Ret:
491 break;
492
493 default:
494 return Attribute::None;
495 }
496 }
497
498 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
499}
500
501/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
502bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands9e89ba32008-12-31 16:14:43 +0000503 bool Changed = false;
504
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000505 SmallPtrSet<Function*, 8> SCCNodes;
506
507 // Fill SCCNodes with the elements of the SCC. Used for quickly
508 // looking up whether a given CallGraphNode is in this SCC.
509 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
510 Function *F = (*I)->getFunction();
511 if (F && !F->isDeclaration() && !F->mayBeOverridden())
512 SCCNodes.insert(F);
513 }
514
515 ArgumentGraph AG;
516
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000517 AttrBuilder B;
518 B.addAttribute(Attribute::NoCapture);
519
Duncan Sands9e89ba32008-12-31 16:14:43 +0000520 // Check each function in turn, determining which pointer arguments are not
521 // captured.
Chris Lattner2decb222010-04-16 22:42:17 +0000522 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
523 Function *F = (*I)->getFunction();
Duncan Sands9e89ba32008-12-31 16:14:43 +0000524
Stephen Hinesdce4a402014-05-29 02:49:00 -0700525 if (!F)
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000526 // External node - only a problem for arguments that we pass to it.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000527 continue;
528
529 // Definitions with weak linkage may be overridden at linktime with
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000530 // something that captures pointers, so treat them like declarations.
Duncan Sands9e89ba32008-12-31 16:14:43 +0000531 if (F->isDeclaration() || F->mayBeOverridden())
532 continue;
533
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000534 // Functions that are readonly (or readnone) and nounwind and don't return
535 // a value can't capture arguments. Don't analyze them.
536 if (F->onlyReadsMemory() && F->doesNotThrow() &&
537 F->getReturnType()->isVoidTy()) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000538 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
539 A != E; ++A) {
540 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
541 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
542 ++NumNoCapture;
543 Changed = true;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000544 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000545 }
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000546 continue;
Benjamin Kramer39bab0e2013-06-22 15:51:19 +0000547 }
548
Nick Lewyckydc897372013-07-06 00:29:58 +0000549 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
550 A != E; ++A) {
551 if (!A->getType()->isPointerTy()) continue;
552 bool HasNonLocalUses = false;
553 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000554 ArgumentUsesTracker Tracker(SCCNodes);
555 PointerMayBeCaptured(A, &Tracker);
556 if (!Tracker.Captured) {
557 if (Tracker.Uses.empty()) {
558 // If it's trivially not captured, mark it nocapture now.
559 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
560 ++NumNoCapture;
561 Changed = true;
562 } else {
563 // If it's not trivially captured and not trivially not captured,
564 // then it must be calling into another function in our SCC. Save
565 // its particulars for Argument-SCC analysis later.
566 ArgumentGraphNode *Node = AG[A];
567 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckydc897372013-07-06 00:29:58 +0000568 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000569 Node->Uses.push_back(AG[*UI]);
Nick Lewyckydc897372013-07-06 00:29:58 +0000570 if (*UI != A)
571 HasNonLocalUses = true;
572 }
Benjamin Kramer5729b8e2013-06-22 16:56:32 +0000573 }
574 }
575 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
576 }
Nick Lewyckydc897372013-07-06 00:29:58 +0000577 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
578 // Can we determine that it's readonly/readnone without doing an SCC?
579 // Note that we don't allow any calls at all here, or else our result
580 // will be dependent on the iteration order through the functions in the
581 // SCC.
582 SmallPtrSet<Argument*, 8> Self;
583 Self.insert(A);
584 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
585 if (R != Attribute::None) {
586 AttrBuilder B;
587 B.addAttribute(R);
588 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
589 Changed = true;
590 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
591 }
592 }
593 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000594 }
595
596 // The graph we've collected is partial because we stopped scanning for
597 // argument uses once we solved the argument trivially. These partial nodes
598 // show up as ArgumentGraphNode objects with an empty Uses list, and for
599 // these nodes the final decision about whether they capture has already been
600 // made. If the definition doesn't have a 'nocapture' attribute by now, it
601 // captures.
602
Stephen Hines36b56882014-04-23 16:57:46 -0700603 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Stephen Hinesdce4a402014-05-29 02:49:00 -0700604 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000605 if (ArgumentSCC.size() == 1) {
606 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
607
608 // eg. "void f(int* x) { if (...) f(x); }"
609 if (ArgumentSCC[0]->Uses.size() == 1 &&
610 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckydc897372013-07-06 00:29:58 +0000611 Argument *A = ArgumentSCC[0]->Definition;
612 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky6b056862009-01-02 03:46:56 +0000613 ++NumNoCapture;
Duncan Sands9e89ba32008-12-31 16:14:43 +0000614 Changed = true;
615 }
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000616 continue;
617 }
618
619 bool SCCCaptured = false;
Stephen Hinesdce4a402014-05-29 02:49:00 -0700620 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
621 I != E && !SCCCaptured; ++I) {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000622 ArgumentGraphNode *Node = *I;
623 if (Node->Uses.empty()) {
624 if (!Node->Definition->hasNoCaptureAttr())
625 SCCCaptured = true;
626 }
627 }
628 if (SCCCaptured) continue;
629
630 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
631 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
632 // quickly looking up whether a given Argument is in this ArgumentSCC.
Stephen Hinesdce4a402014-05-29 02:49:00 -0700633 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000634 ArgumentSCCNodes.insert((*I)->Definition);
635 }
636
Stephen Hinesdce4a402014-05-29 02:49:00 -0700637 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
638 I != E && !SCCCaptured; ++I) {
Nick Lewyckyb48a1892011-12-28 23:24:21 +0000639 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
Stephen Hinesdce4a402014-05-29 02:49:00 -0700778 if (!F)
Nick Lewycky199aa3c2009-03-08 06:20:47 +0000779 // 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
Stephen Hinesdce4a402014-05-29 02:49:00 -07001671 if (F && F->isDeclaration())
Meador Ingecf47ce62013-03-21 00:55:59 +00001672 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}