blob: 1819fe1f82fa8f0099fa15fa167ce1426083d198 [file] [log] [blame]
Meador Inge6b6a1612013-03-21 00:55:59 +00001//===- FunctionAttrs.cpp - Pass which marks functions attributes ----------===//
Duncan Sands44c8cd92008-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 Lewyckyc2ec0722013-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 Sands44c8cd92008-12-31 16:14:43 +000018//
19//===----------------------------------------------------------------------===//
20
Duncan Sands44c8cd92008-12-31 16:14:43 +000021#include "llvm/Transforms/IPO.h"
Nick Lewycky4c378a42011-12-28 23:24:21 +000022#include "llvm/ADT/SCCIterator.h"
Benjamin Kramer15591272012-10-31 13:45:49 +000023#include "llvm/ADT/SetVector.h"
Duncan Sandsb193a372009-01-02 11:54:37 +000024#include "llvm/ADT/SmallSet.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000025#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Analysis/AliasAnalysis.h"
27#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000028#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/GlobalVariable.h"
Chandler Carruth83948572014-03-04 10:30:26 +000031#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/IntrinsicInst.h"
33#include "llvm/IR/LLVMContext.h"
Meador Inge6b6a1612013-03-21 00:55:59 +000034#include "llvm/Target/TargetLibraryInfo.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000035using namespace llvm;
36
Chandler Carruth964daaa2014-04-22 02:55:47 +000037#define DEBUG_TYPE "functionattrs"
38
Duncan Sands44c8cd92008-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 Lewyckyc2ec0722013-07-06 00:29:58 +000042STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
43STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000044STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Meador Inge6b6a1612013-03-21 00:55:59 +000045STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Duncan Sands44c8cd92008-12-31 16:14:43 +000046
47namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000048 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands44c8cd92008-12-31 16:14:43 +000049 static char ID; // Pass identification, replacement for typeid
Craig Topperf40110f2014-04-25 05:29:35 +000050 FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000051 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
52 }
Duncan Sands44c8cd92008-12-31 16:14:43 +000053
54 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Craig Topper3e4c6972014-03-05 09:10:37 +000055 bool runOnSCC(CallGraphSCC &SCC) override;
Duncan Sands44c8cd92008-12-31 16:14:43 +000056
57 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000058 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000059
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000060 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
61 bool AddArgumentAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000062
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000063 // IsFunctionMallocLike - Does this function allocate new memory?
64 bool IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +000065 SmallPtrSet<Function*, 8> &) const;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000066
67 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000068 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000069
Meador Inge6b6a1612013-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 Lewyckya833c662013-07-04 03:51:53 +000075 F.setDoesNotAccessMemory();
76 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000077 }
78 }
79
80 void setOnlyReadsMemory(Function &F) {
81 if (!F.onlyReadsMemory()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000082 F.setOnlyReadsMemory();
83 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000084 }
85 }
86
87 void setDoesNotThrow(Function &F) {
88 if (!F.doesNotThrow()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000089 F.setDoesNotThrow();
90 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000091 }
92 }
93
94 void setDoesNotCapture(Function &F, unsigned n) {
95 if (!F.doesNotCapture(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +000096 F.setDoesNotCapture(n);
97 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000098 }
99 }
100
Nick Lewyckyc2ec0722013-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 Inge6b6a1612013-03-21 00:55:59 +0000108 void setDoesNotAlias(Function &F, unsigned n) {
109 if (!F.doesNotAlias(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000110 F.setDoesNotAlias(n);
111 ++NumAnnotated;
Meador Inge6b6a1612013-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
Craig Topper3e4c6972014-03-05 09:10:37 +0000124 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000125 AU.setPreservesCFG();
Dan Gohman86449d72010-11-08 16:10:15 +0000126 AU.addRequired<AliasAnalysis>();
Meador Inge6b6a1612013-03-21 00:55:59 +0000127 AU.addRequired<TargetLibraryInfo>();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000128 CallGraphSCCPass::getAnalysisUsage(AU);
129 }
130
Dan Gohman86449d72010-11-08 16:10:15 +0000131 private:
132 AliasAnalysis *AA;
Meador Inge6b6a1612013-03-21 00:55:59 +0000133 TargetLibraryInfo *TLI;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000134 };
135}
136
137char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +0000138INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
139 "Deduce function attributes", false, false)
Nick Lewycky2c880672013-09-05 08:19:58 +0000140INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000141INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Meador Inge6b6a1612013-03-21 00:55:59 +0000142INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfo)
Owen Anderson071cee02010-10-13 22:00:45 +0000143INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000144 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000145
146Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
147
148
Duncan Sands44c8cd92008-12-31 16:14:43 +0000149/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000150bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000151 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands44c8cd92008-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 Lattner4422d312010-04-16 22:42:17 +0000155 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
156 SCCNodes.insert((*I)->getFunction());
Duncan Sands44c8cd92008-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 Lattner4422d312010-04-16 22:42:17 +0000161 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
162 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000163
Craig Topperf40110f2014-04-25 05:29:35 +0000164 if (!F)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000165 // External node - may write memory. Just give up.
166 return false;
167
Dan Gohman35814e62010-11-09 20:13:27 +0000168 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
169 if (MRB == AliasAnalysis::DoesNotAccessMemory)
Duncan Sands44c8cd92008-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 Gohman35814e62010-11-09 20:13:27 +0000176 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands44c8cd92008-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 Greif62f0aac2010-07-28 22:50:26 +0000190 CallSite CS(cast<Value>(I));
Dan Gohman86449d72010-11-08 16:10:15 +0000191 if (CS) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000192 // Ignore calls to functions in the same SCC.
Dan Gohman86449d72010-11-08 16:10:15 +0000193 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000194 continue;
Dan Gohman2694e142010-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 Gohman25775802010-11-10 17:34:04 +0000198 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
199 // If the call does access argument pointees, check each argument.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000200 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman2694e142010-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 Gohmande521552010-11-09 19:56:27 +0000218 }
219 }
Dan Gohmande521552010-11-09 19:56:27 +0000220 continue;
Dan Gohman86449d72010-11-08 16:10:15 +0000221 }
Dan Gohman2694e142010-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 Sands44c8cd92008-12-31 16:14:43 +0000229 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000230 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
231 if (!LI->isVolatile()) {
Dan Gohman65316d62010-11-11 21:50:19 +0000232 AliasAnalysis::Location Loc = AA->getLocation(LI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000233 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
234 continue;
235 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000236 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000237 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
238 if (!SI->isVolatile()) {
Dan Gohman65316d62010-11-11 21:50:19 +0000239 AliasAnalysis::Location Loc = AA->getLocation(SI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000240 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
241 continue;
242 }
Dan Gohmane3467a72010-11-09 20:17:38 +0000243 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
244 // Ignore vaargs on local memory.
Dan Gohman65316d62010-11-11 21:50:19 +0000245 AliasAnalysis::Location Loc = AA->getLocation(VI);
Dan Gohmane3467a72010-11-09 20:17:38 +0000246 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
247 continue;
Duncan Sands44c8cd92008-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 Sands9759f2e2009-05-06 08:42:00 +0000255
Duncan Sands44c8cd92008-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 Lattner4422d312010-04-16 22:42:17 +0000264 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
265 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-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 Wendling50d27842012-10-15 20:35:56 +0000278 AttrBuilder B;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000279 B.addAttribute(Attribute::ReadOnly)
280 .addAttribute(Attribute::ReadNone);
Bill Wendling430fa9b2013-01-23 00:45:55 +0000281 F->removeAttributes(AttributeSet::FunctionIndex,
282 AttributeSet::get(F->getContext(),
283 AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000284
285 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000286 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000287 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000288
289 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000290 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000291 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000292 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000293 }
294
295 return MadeChange;
296}
297
Nick Lewycky4c378a42011-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:
Craig Topperf40110f2014-04-25 05:29:35 +0000323 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-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
Craig Topper3e4c6972014-03-05 09:10:37 +0000346 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000347
Chandler Carruth64e9aa52014-03-05 10:21:48 +0000348 bool captured(const Use *U) override {
Nick Lewycky4c378a42011-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 Lewyckyc2ec0722013-07-06 00:29:58 +0000355 bool Found = false;
Nick Lewycky4c378a42011-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 Lewyckyc2ec0722013-07-06 00:29:58 +0000366 Found = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000367 break;
368 }
369 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000370 assert(Found && "Capturing call-site captured nothing?");
Duncan Sandsc9e95ad2013-09-13 08:16:06 +0000371 (void)Found;
Nick Lewycky4c378a42011-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 Lewyckyc2ec0722013-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
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000418 // inalloca arguments are always clobbered by the call.
419 if (A->hasInAllocaAttr())
420 return Attribute::None;
421
Nick Lewyckyc2ec0722013-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
Chandler Carruthcdf47882014-03-09 03:16:01 +0000425 for (Use &U : A->uses()) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000426 if (Count++ >= 20)
427 return Attribute::None;
428
Chandler Carruthcdf47882014-03-09 03:16:01 +0000429 Visited.insert(&U);
430 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-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:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000443 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000444 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000445 for (Use &UU : I->uses())
446 if (Visited.insert(&UU))
447 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-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 Sands44c8cd92008-12-31 16:14:43 +0000503 bool Changed = false;
504
Nick Lewycky4c378a42011-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 Kramer40d7f352013-06-22 16:56:32 +0000517 AttrBuilder B;
518 B.addAttribute(Attribute::NoCapture);
519
Duncan Sands44c8cd92008-12-31 16:14:43 +0000520 // Check each function in turn, determining which pointer arguments are not
521 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000522 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
523 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000524
Craig Topperf40110f2014-04-25 05:29:35 +0000525 if (!F)
Nick Lewycky4c378a42011-12-28 23:24:21 +0000526 // External node - only a problem for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000527 continue;
528
529 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000530 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000531 if (F->isDeclaration() || F->mayBeOverridden())
532 continue;
533
Nick Lewycky4c378a42011-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 Kramer40d7f352013-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 Lewycky4c378a42011-12-28 23:24:21 +0000544 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000545 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000546 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000547 }
548
Nick Lewyckyc2ec0722013-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 Kramer40d7f352013-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 Lewyckyc2ec0722013-07-06 00:29:58 +0000568 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000569 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000570 if (*UI != A)
571 HasNonLocalUses = true;
572 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000573 }
574 }
575 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
576 }
Nick Lewyckyc2ec0722013-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 Lewycky4c378a42011-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
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000603 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000604 std::vector<ArgumentGraphNode*> &ArgumentSCC = *I;
605 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 Lewyckyc2ec0722013-07-06 00:29:58 +0000611 Argument *A = ArgumentSCC[0]->Definition;
612 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000613 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000614 Changed = true;
615 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000616 continue;
617 }
618
619 bool SCCCaptured = false;
620 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
621 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
622 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.
633 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
634 E = ArgumentSCC.end(); I != E; ++I) {
635 ArgumentSCCNodes.insert((*I)->Definition);
636 }
637
638 for (std::vector<ArgumentGraphNode*>::iterator I = ArgumentSCC.begin(),
639 E = ArgumentSCC.end(); I != E && !SCCCaptured; ++I) {
640 ArgumentGraphNode *N = *I;
641 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
642 UE = N->Uses.end(); UI != UE; ++UI) {
643 Argument *A = (*UI)->Definition;
644 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
645 continue;
646 SCCCaptured = true;
647 break;
648 }
649 }
650 if (SCCCaptured) continue;
651
Nick Lewyckyf740db32012-01-05 22:21:45 +0000652 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000653 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000654 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000655 ++NumNoCapture;
656 Changed = true;
657 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000658
659 // We also want to compute readonly/readnone. With a small number of false
660 // negatives, we can assume that any pointer which is captured isn't going
661 // to be provably readonly or readnone, since by definition we can't
662 // analyze all uses of a captured pointer.
663 //
664 // The false negatives happen when the pointer is captured by a function
665 // that promises readonly/readnone behaviour on the pointer, then the
666 // pointer's lifetime ends before anything that writes to arbitrary memory.
667 // Also, a readonly/readnone pointer may be returned, but returning a
668 // pointer is capturing it.
669
670 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
671 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
672 Argument *A = ArgumentSCC[i]->Definition;
673 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
674 if (K == Attribute::ReadNone)
675 continue;
676 if (K == Attribute::ReadOnly) {
677 ReadAttr = Attribute::ReadOnly;
678 continue;
679 }
680 ReadAttr = K;
681 break;
682 }
683
684 if (ReadAttr != Attribute::None) {
685 AttrBuilder B;
686 B.addAttribute(ReadAttr);
687 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
688 Argument *A = ArgumentSCC[i]->Definition;
689 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
690 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
691 Changed = true;
692 }
693 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000694 }
695
696 return Changed;
697}
698
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000699/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000700/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000701bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +0000702 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramer15591272012-10-31 13:45:49 +0000703 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000704 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
705 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
706 FlowsToReturn.insert(Ret->getReturnValue());
707
708 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000709 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000710
711 if (Constant *C = dyn_cast<Constant>(RetVal)) {
712 if (!C->isNullValue() && !isa<UndefValue>(C))
713 return false;
714
715 continue;
716 }
717
718 if (isa<Argument>(RetVal))
719 return false;
720
721 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
722 switch (RVI->getOpcode()) {
723 // Extend the analysis by looking upwards.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000724 case Instruction::BitCast:
Victor Hernandez5d034492009-09-18 22:35:49 +0000725 case Instruction::GetElementPtr:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000726 case Instruction::AddrSpaceCast:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000727 FlowsToReturn.insert(RVI->getOperand(0));
728 continue;
729 case Instruction::Select: {
730 SelectInst *SI = cast<SelectInst>(RVI);
731 FlowsToReturn.insert(SI->getTrueValue());
732 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner43d0db72009-09-27 21:29:28 +0000733 continue;
734 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000735 case Instruction::PHI: {
736 PHINode *PN = cast<PHINode>(RVI);
737 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
738 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner43d0db72009-09-27 21:29:28 +0000739 continue;
740 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000741
742 // Check whether the pointer came from an allocation.
743 case Instruction::Alloca:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000744 break;
745 case Instruction::Call:
746 case Instruction::Invoke: {
747 CallSite CS(RVI);
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000748 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000749 break;
750 if (CS.getCalledFunction() &&
Chris Lattner2f2110a2009-08-31 04:09:04 +0000751 SCCNodes.count(CS.getCalledFunction()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000752 break;
753 } // fall-through
754 default:
755 return false; // Did not come from an allocation.
756 }
757
Dan Gohman94e61762009-11-19 21:57:48 +0000758 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000759 return false;
760 }
761
762 return true;
763}
764
765/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000766bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000767 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000768
769 // Fill SCCNodes with the elements of the SCC. Used for quickly
770 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000771 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
772 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000773
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000774 // Check each function in turn, determining which functions return noalias
775 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000776 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
777 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000778
Craig Topperf40110f2014-04-25 05:29:35 +0000779 if (!F)
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000780 // External node - skip it;
781 return false;
782
783 // Already noalias.
784 if (F->doesNotAlias(0))
785 continue;
786
787 // Definitions with weak linkage may be overridden at linktime, so
788 // treat them like declarations.
789 if (F->isDeclaration() || F->mayBeOverridden())
790 return false;
791
792 // We annotate noalias return values, which are only applicable to
793 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000794 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000795 continue;
796
797 if (!IsFunctionMallocLike(F, SCCNodes))
798 return false;
799 }
800
801 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000802 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
803 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000804 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000805 continue;
806
807 F->setDoesNotAlias(0);
808 ++NumNoAlias;
809 MadeChange = true;
810 }
811
812 return MadeChange;
813}
814
Meador Inge6b6a1612013-03-21 00:55:59 +0000815/// inferPrototypeAttributes - Analyze the name and prototype of the
816/// given function and set any applicable attributes. Returns true
817/// if any attributes were set and false otherwise.
818bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
819 FunctionType *FTy = F.getFunctionType();
820 LibFunc::Func TheLibFunc;
821 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
822 return false;
823
824 switch (TheLibFunc) {
825 case LibFunc::strlen:
826 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
827 return false;
828 setOnlyReadsMemory(F);
829 setDoesNotThrow(F);
830 setDoesNotCapture(F, 1);
831 break;
832 case LibFunc::strchr:
833 case LibFunc::strrchr:
834 if (FTy->getNumParams() != 2 ||
835 !FTy->getParamType(0)->isPointerTy() ||
836 !FTy->getParamType(1)->isIntegerTy())
837 return false;
838 setOnlyReadsMemory(F);
839 setDoesNotThrow(F);
840 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000841 case LibFunc::strtol:
842 case LibFunc::strtod:
843 case LibFunc::strtof:
844 case LibFunc::strtoul:
845 case LibFunc::strtoll:
846 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +0000847 case LibFunc::strtoull:
848 if (FTy->getNumParams() < 2 ||
849 !FTy->getParamType(1)->isPointerTy())
850 return false;
851 setDoesNotThrow(F);
852 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000853 setOnlyReadsMemory(F, 1);
854 break;
855 case LibFunc::strcpy:
856 case LibFunc::stpcpy:
857 case LibFunc::strcat:
858 case LibFunc::strncat:
859 case LibFunc::strncpy:
860 case LibFunc::stpncpy:
861 if (FTy->getNumParams() < 2 ||
862 !FTy->getParamType(1)->isPointerTy())
863 return false;
864 setDoesNotThrow(F);
865 setDoesNotCapture(F, 2);
866 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000867 break;
868 case LibFunc::strxfrm:
869 if (FTy->getNumParams() != 3 ||
870 !FTy->getParamType(0)->isPointerTy() ||
871 !FTy->getParamType(1)->isPointerTy())
872 return false;
873 setDoesNotThrow(F);
874 setDoesNotCapture(F, 1);
875 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000876 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000877 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000878 case LibFunc::strcmp: //0,1
879 case LibFunc::strspn: // 0,1
880 case LibFunc::strncmp: // 0,1
881 case LibFunc::strcspn: //0,1
882 case LibFunc::strcoll: //0,1
883 case LibFunc::strcasecmp: // 0,1
884 case LibFunc::strncasecmp: //
Meador Inge6b6a1612013-03-21 00:55:59 +0000885 if (FTy->getNumParams() < 2 ||
886 !FTy->getParamType(0)->isPointerTy() ||
887 !FTy->getParamType(1)->isPointerTy())
888 return false;
889 setOnlyReadsMemory(F);
890 setDoesNotThrow(F);
891 setDoesNotCapture(F, 1);
892 setDoesNotCapture(F, 2);
893 break;
894 case LibFunc::strstr:
895 case LibFunc::strpbrk:
896 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
897 return false;
898 setOnlyReadsMemory(F);
899 setDoesNotThrow(F);
900 setDoesNotCapture(F, 2);
901 break;
902 case LibFunc::strtok:
903 case LibFunc::strtok_r:
904 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
905 return false;
906 setDoesNotThrow(F);
907 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000908 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000909 break;
910 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000911 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
912 return false;
913 setDoesNotThrow(F);
914 setDoesNotCapture(F, 1);
915 setOnlyReadsMemory(F, 1);
916 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000917 case LibFunc::setbuf:
918 case LibFunc::setvbuf:
919 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
920 return false;
921 setDoesNotThrow(F);
922 setDoesNotCapture(F, 1);
923 break;
924 case LibFunc::strdup:
925 case LibFunc::strndup:
926 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
927 !FTy->getParamType(0)->isPointerTy())
928 return false;
929 setDoesNotThrow(F);
930 setDoesNotAlias(F, 0);
931 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000932 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +0000933 break;
934 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +0000935 case LibFunc::statvfs:
936 if (FTy->getNumParams() < 2 ||
937 !FTy->getParamType(0)->isPointerTy() ||
938 !FTy->getParamType(1)->isPointerTy())
939 return false;
940 setDoesNotThrow(F);
941 setDoesNotCapture(F, 1);
942 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000943 setOnlyReadsMemory(F, 1);
944 break;
945 case LibFunc::sscanf:
946 if (FTy->getNumParams() < 2 ||
947 !FTy->getParamType(0)->isPointerTy() ||
948 !FTy->getParamType(1)->isPointerTy())
949 return false;
950 setDoesNotThrow(F);
951 setDoesNotCapture(F, 1);
952 setDoesNotCapture(F, 2);
953 setOnlyReadsMemory(F, 1);
954 setOnlyReadsMemory(F, 2);
955 break;
956 case LibFunc::sprintf:
957 if (FTy->getNumParams() < 2 ||
958 !FTy->getParamType(0)->isPointerTy() ||
959 !FTy->getParamType(1)->isPointerTy())
960 return false;
961 setDoesNotThrow(F);
962 setDoesNotCapture(F, 1);
963 setDoesNotCapture(F, 2);
964 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000965 break;
966 case LibFunc::snprintf:
967 if (FTy->getNumParams() != 3 ||
968 !FTy->getParamType(0)->isPointerTy() ||
969 !FTy->getParamType(2)->isPointerTy())
970 return false;
971 setDoesNotThrow(F);
972 setDoesNotCapture(F, 1);
973 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000974 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +0000975 break;
976 case LibFunc::setitimer:
977 if (FTy->getNumParams() != 3 ||
978 !FTy->getParamType(1)->isPointerTy() ||
979 !FTy->getParamType(2)->isPointerTy())
980 return false;
981 setDoesNotThrow(F);
982 setDoesNotCapture(F, 2);
983 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000984 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000985 break;
986 case LibFunc::system:
987 if (FTy->getNumParams() != 1 ||
988 !FTy->getParamType(0)->isPointerTy())
989 return false;
990 // May throw; "system" is a valid pthread cancellation point.
991 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000992 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +0000993 break;
994 case LibFunc::malloc:
995 if (FTy->getNumParams() != 1 ||
996 !FTy->getReturnType()->isPointerTy())
997 return false;
998 setDoesNotThrow(F);
999 setDoesNotAlias(F, 0);
1000 break;
1001 case LibFunc::memcmp:
1002 if (FTy->getNumParams() != 3 ||
1003 !FTy->getParamType(0)->isPointerTy() ||
1004 !FTy->getParamType(1)->isPointerTy())
1005 return false;
1006 setOnlyReadsMemory(F);
1007 setDoesNotThrow(F);
1008 setDoesNotCapture(F, 1);
1009 setDoesNotCapture(F, 2);
1010 break;
1011 case LibFunc::memchr:
1012 case LibFunc::memrchr:
1013 if (FTy->getNumParams() != 3)
1014 return false;
1015 setOnlyReadsMemory(F);
1016 setDoesNotThrow(F);
1017 break;
1018 case LibFunc::modf:
1019 case LibFunc::modff:
1020 case LibFunc::modfl:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001021 if (FTy->getNumParams() < 2 ||
1022 !FTy->getParamType(1)->isPointerTy())
1023 return false;
1024 setDoesNotThrow(F);
1025 setDoesNotCapture(F, 2);
1026 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001027 case LibFunc::memcpy:
1028 case LibFunc::memccpy:
1029 case LibFunc::memmove:
1030 if (FTy->getNumParams() < 2 ||
1031 !FTy->getParamType(1)->isPointerTy())
1032 return false;
1033 setDoesNotThrow(F);
1034 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001035 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001036 break;
1037 case LibFunc::memalign:
1038 if (!FTy->getReturnType()->isPointerTy())
1039 return false;
1040 setDoesNotAlias(F, 0);
1041 break;
1042 case LibFunc::mkdir:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001043 if (FTy->getNumParams() == 0 ||
1044 !FTy->getParamType(0)->isPointerTy())
1045 return false;
1046 setDoesNotThrow(F);
1047 setDoesNotCapture(F, 1);
1048 setOnlyReadsMemory(F, 1);
1049 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001050 case LibFunc::mktime:
1051 if (FTy->getNumParams() == 0 ||
1052 !FTy->getParamType(0)->isPointerTy())
1053 return false;
1054 setDoesNotThrow(F);
1055 setDoesNotCapture(F, 1);
1056 break;
1057 case LibFunc::realloc:
1058 if (FTy->getNumParams() != 2 ||
1059 !FTy->getParamType(0)->isPointerTy() ||
1060 !FTy->getReturnType()->isPointerTy())
1061 return false;
1062 setDoesNotThrow(F);
1063 setDoesNotAlias(F, 0);
1064 setDoesNotCapture(F, 1);
1065 break;
1066 case LibFunc::read:
1067 if (FTy->getNumParams() != 3 ||
1068 !FTy->getParamType(1)->isPointerTy())
1069 return false;
1070 // May throw; "read" is a valid pthread cancellation point.
1071 setDoesNotCapture(F, 2);
1072 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001073 case LibFunc::rewind:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001074 if (FTy->getNumParams() < 1 ||
1075 !FTy->getParamType(0)->isPointerTy())
1076 return false;
1077 setDoesNotThrow(F);
1078 setDoesNotCapture(F, 1);
1079 break;
1080 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001081 case LibFunc::remove:
1082 case LibFunc::realpath:
1083 if (FTy->getNumParams() < 1 ||
1084 !FTy->getParamType(0)->isPointerTy())
1085 return false;
1086 setDoesNotThrow(F);
1087 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001088 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001089 break;
1090 case LibFunc::rename:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001091 if (FTy->getNumParams() < 2 ||
1092 !FTy->getParamType(0)->isPointerTy() ||
1093 !FTy->getParamType(1)->isPointerTy())
1094 return false;
1095 setDoesNotThrow(F);
1096 setDoesNotCapture(F, 1);
1097 setDoesNotCapture(F, 2);
1098 setOnlyReadsMemory(F, 1);
1099 setOnlyReadsMemory(F, 2);
1100 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001101 case LibFunc::readlink:
1102 if (FTy->getNumParams() < 2 ||
1103 !FTy->getParamType(0)->isPointerTy() ||
1104 !FTy->getParamType(1)->isPointerTy())
1105 return false;
1106 setDoesNotThrow(F);
1107 setDoesNotCapture(F, 1);
1108 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001109 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001110 break;
1111 case LibFunc::write:
1112 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1113 return false;
1114 // May throw; "write" is a valid pthread cancellation point.
1115 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001116 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001117 break;
1118 case LibFunc::bcopy:
1119 if (FTy->getNumParams() != 3 ||
1120 !FTy->getParamType(0)->isPointerTy() ||
1121 !FTy->getParamType(1)->isPointerTy())
1122 return false;
1123 setDoesNotThrow(F);
1124 setDoesNotCapture(F, 1);
1125 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001126 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001127 break;
1128 case LibFunc::bcmp:
1129 if (FTy->getNumParams() != 3 ||
1130 !FTy->getParamType(0)->isPointerTy() ||
1131 !FTy->getParamType(1)->isPointerTy())
1132 return false;
1133 setDoesNotThrow(F);
1134 setOnlyReadsMemory(F);
1135 setDoesNotCapture(F, 1);
1136 setDoesNotCapture(F, 2);
1137 break;
1138 case LibFunc::bzero:
1139 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1140 return false;
1141 setDoesNotThrow(F);
1142 setDoesNotCapture(F, 1);
1143 break;
1144 case LibFunc::calloc:
1145 if (FTy->getNumParams() != 2 ||
1146 !FTy->getReturnType()->isPointerTy())
1147 return false;
1148 setDoesNotThrow(F);
1149 setDoesNotAlias(F, 0);
1150 break;
1151 case LibFunc::chmod:
1152 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001153 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1154 return false;
1155 setDoesNotThrow(F);
1156 setDoesNotCapture(F, 1);
1157 setOnlyReadsMemory(F, 1);
1158 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001159 case LibFunc::ctermid:
1160 case LibFunc::clearerr:
1161 case LibFunc::closedir:
1162 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1163 return false;
1164 setDoesNotThrow(F);
1165 setDoesNotCapture(F, 1);
1166 break;
1167 case LibFunc::atoi:
1168 case LibFunc::atol:
1169 case LibFunc::atof:
1170 case LibFunc::atoll:
1171 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1172 return false;
1173 setDoesNotThrow(F);
1174 setOnlyReadsMemory(F);
1175 setDoesNotCapture(F, 1);
1176 break;
1177 case LibFunc::access:
1178 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1179 return false;
1180 setDoesNotThrow(F);
1181 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001182 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001183 break;
1184 case LibFunc::fopen:
1185 if (FTy->getNumParams() != 2 ||
1186 !FTy->getReturnType()->isPointerTy() ||
1187 !FTy->getParamType(0)->isPointerTy() ||
1188 !FTy->getParamType(1)->isPointerTy())
1189 return false;
1190 setDoesNotThrow(F);
1191 setDoesNotAlias(F, 0);
1192 setDoesNotCapture(F, 1);
1193 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001194 setOnlyReadsMemory(F, 1);
1195 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001196 break;
1197 case LibFunc::fdopen:
1198 if (FTy->getNumParams() != 2 ||
1199 !FTy->getReturnType()->isPointerTy() ||
1200 !FTy->getParamType(1)->isPointerTy())
1201 return false;
1202 setDoesNotThrow(F);
1203 setDoesNotAlias(F, 0);
1204 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001205 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001206 break;
1207 case LibFunc::feof:
1208 case LibFunc::free:
1209 case LibFunc::fseek:
1210 case LibFunc::ftell:
1211 case LibFunc::fgetc:
1212 case LibFunc::fseeko:
1213 case LibFunc::ftello:
1214 case LibFunc::fileno:
1215 case LibFunc::fflush:
1216 case LibFunc::fclose:
1217 case LibFunc::fsetpos:
1218 case LibFunc::flockfile:
1219 case LibFunc::funlockfile:
1220 case LibFunc::ftrylockfile:
1221 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1222 return false;
1223 setDoesNotThrow(F);
1224 setDoesNotCapture(F, 1);
1225 break;
1226 case LibFunc::ferror:
1227 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1228 return false;
1229 setDoesNotThrow(F);
1230 setDoesNotCapture(F, 1);
1231 setOnlyReadsMemory(F);
1232 break;
1233 case LibFunc::fputc:
1234 case LibFunc::fstat:
1235 case LibFunc::frexp:
1236 case LibFunc::frexpf:
1237 case LibFunc::frexpl:
1238 case LibFunc::fstatvfs:
1239 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1240 return false;
1241 setDoesNotThrow(F);
1242 setDoesNotCapture(F, 2);
1243 break;
1244 case LibFunc::fgets:
1245 if (FTy->getNumParams() != 3 ||
1246 !FTy->getParamType(0)->isPointerTy() ||
1247 !FTy->getParamType(2)->isPointerTy())
1248 return false;
1249 setDoesNotThrow(F);
1250 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001251 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001252 case LibFunc::fread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001253 if (FTy->getNumParams() != 4 ||
1254 !FTy->getParamType(0)->isPointerTy() ||
1255 !FTy->getParamType(3)->isPointerTy())
1256 return false;
1257 setDoesNotThrow(F);
1258 setDoesNotCapture(F, 1);
1259 setDoesNotCapture(F, 4);
1260 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001261 case LibFunc::fwrite:
1262 if (FTy->getNumParams() != 4 ||
1263 !FTy->getParamType(0)->isPointerTy() ||
1264 !FTy->getParamType(3)->isPointerTy())
1265 return false;
1266 setDoesNotThrow(F);
1267 setDoesNotCapture(F, 1);
1268 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001269 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001270 case LibFunc::fputs:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001271 if (FTy->getNumParams() < 2 ||
1272 !FTy->getParamType(0)->isPointerTy() ||
1273 !FTy->getParamType(1)->isPointerTy())
1274 return false;
1275 setDoesNotThrow(F);
1276 setDoesNotCapture(F, 1);
1277 setDoesNotCapture(F, 2);
1278 setOnlyReadsMemory(F, 1);
1279 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001280 case LibFunc::fscanf:
1281 case LibFunc::fprintf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001282 if (FTy->getNumParams() < 2 ||
1283 !FTy->getParamType(0)->isPointerTy() ||
1284 !FTy->getParamType(1)->isPointerTy())
1285 return false;
1286 setDoesNotThrow(F);
1287 setDoesNotCapture(F, 1);
1288 setDoesNotCapture(F, 2);
1289 setOnlyReadsMemory(F, 2);
1290 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001291 case LibFunc::fgetpos:
1292 if (FTy->getNumParams() < 2 ||
1293 !FTy->getParamType(0)->isPointerTy() ||
1294 !FTy->getParamType(1)->isPointerTy())
1295 return false;
1296 setDoesNotThrow(F);
1297 setDoesNotCapture(F, 1);
1298 setDoesNotCapture(F, 2);
1299 break;
1300 case LibFunc::getc:
1301 case LibFunc::getlogin_r:
1302 case LibFunc::getc_unlocked:
1303 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1304 return false;
1305 setDoesNotThrow(F);
1306 setDoesNotCapture(F, 1);
1307 break;
1308 case LibFunc::getenv:
1309 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1310 return false;
1311 setDoesNotThrow(F);
1312 setOnlyReadsMemory(F);
1313 setDoesNotCapture(F, 1);
1314 break;
1315 case LibFunc::gets:
1316 case LibFunc::getchar:
1317 setDoesNotThrow(F);
1318 break;
1319 case LibFunc::getitimer:
1320 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1321 return false;
1322 setDoesNotThrow(F);
1323 setDoesNotCapture(F, 2);
1324 break;
1325 case LibFunc::getpwnam:
1326 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1327 return false;
1328 setDoesNotThrow(F);
1329 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001330 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001331 break;
1332 case LibFunc::ungetc:
1333 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1334 return false;
1335 setDoesNotThrow(F);
1336 setDoesNotCapture(F, 2);
1337 break;
1338 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001339 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1340 return false;
1341 setDoesNotThrow(F);
1342 setDoesNotCapture(F, 1);
1343 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001344 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001345 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1346 return false;
1347 setDoesNotThrow(F);
1348 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001349 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001350 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001351 case LibFunc::unsetenv:
1352 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1353 return false;
1354 setDoesNotThrow(F);
1355 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001356 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001357 break;
1358 case LibFunc::utime:
1359 case LibFunc::utimes:
1360 if (FTy->getNumParams() != 2 ||
1361 !FTy->getParamType(0)->isPointerTy() ||
1362 !FTy->getParamType(1)->isPointerTy())
1363 return false;
1364 setDoesNotThrow(F);
1365 setDoesNotCapture(F, 1);
1366 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001367 setOnlyReadsMemory(F, 1);
1368 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001369 break;
1370 case LibFunc::putc:
1371 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1372 return false;
1373 setDoesNotThrow(F);
1374 setDoesNotCapture(F, 2);
1375 break;
1376 case LibFunc::puts:
1377 case LibFunc::printf:
1378 case LibFunc::perror:
1379 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1380 return false;
1381 setDoesNotThrow(F);
1382 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001383 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001384 break;
1385 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001386 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1387 return false;
1388 // May throw; "pread" is a valid pthread cancellation point.
1389 setDoesNotCapture(F, 2);
1390 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001391 case LibFunc::pwrite:
1392 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1393 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001394 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001395 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001396 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001397 break;
1398 case LibFunc::putchar:
1399 setDoesNotThrow(F);
1400 break;
1401 case LibFunc::popen:
1402 if (FTy->getNumParams() != 2 ||
1403 !FTy->getReturnType()->isPointerTy() ||
1404 !FTy->getParamType(0)->isPointerTy() ||
1405 !FTy->getParamType(1)->isPointerTy())
1406 return false;
1407 setDoesNotThrow(F);
1408 setDoesNotAlias(F, 0);
1409 setDoesNotCapture(F, 1);
1410 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001411 setOnlyReadsMemory(F, 1);
1412 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001413 break;
1414 case LibFunc::pclose:
1415 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1416 return false;
1417 setDoesNotThrow(F);
1418 setDoesNotCapture(F, 1);
1419 break;
1420 case LibFunc::vscanf:
1421 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1422 return false;
1423 setDoesNotThrow(F);
1424 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001425 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001426 break;
1427 case LibFunc::vsscanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001428 if (FTy->getNumParams() != 3 ||
1429 !FTy->getParamType(1)->isPointerTy() ||
1430 !FTy->getParamType(2)->isPointerTy())
1431 return false;
1432 setDoesNotThrow(F);
1433 setDoesNotCapture(F, 1);
1434 setDoesNotCapture(F, 2);
1435 setOnlyReadsMemory(F, 1);
1436 setOnlyReadsMemory(F, 2);
1437 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001438 case LibFunc::vfscanf:
1439 if (FTy->getNumParams() != 3 ||
1440 !FTy->getParamType(1)->isPointerTy() ||
1441 !FTy->getParamType(2)->isPointerTy())
1442 return false;
1443 setDoesNotThrow(F);
1444 setDoesNotCapture(F, 1);
1445 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001446 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001447 break;
1448 case LibFunc::valloc:
1449 if (!FTy->getReturnType()->isPointerTy())
1450 return false;
1451 setDoesNotThrow(F);
1452 setDoesNotAlias(F, 0);
1453 break;
1454 case LibFunc::vprintf:
1455 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1456 return false;
1457 setDoesNotThrow(F);
1458 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001459 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001460 break;
1461 case LibFunc::vfprintf:
1462 case LibFunc::vsprintf:
1463 if (FTy->getNumParams() != 3 ||
1464 !FTy->getParamType(0)->isPointerTy() ||
1465 !FTy->getParamType(1)->isPointerTy())
1466 return false;
1467 setDoesNotThrow(F);
1468 setDoesNotCapture(F, 1);
1469 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001470 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001471 break;
1472 case LibFunc::vsnprintf:
1473 if (FTy->getNumParams() != 4 ||
1474 !FTy->getParamType(0)->isPointerTy() ||
1475 !FTy->getParamType(2)->isPointerTy())
1476 return false;
1477 setDoesNotThrow(F);
1478 setDoesNotCapture(F, 1);
1479 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001480 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001481 break;
1482 case LibFunc::open:
1483 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1484 return false;
1485 // May throw; "open" is a valid pthread cancellation point.
1486 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001487 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001488 break;
1489 case LibFunc::opendir:
1490 if (FTy->getNumParams() != 1 ||
1491 !FTy->getReturnType()->isPointerTy() ||
1492 !FTy->getParamType(0)->isPointerTy())
1493 return false;
1494 setDoesNotThrow(F);
1495 setDoesNotAlias(F, 0);
1496 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001497 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001498 break;
1499 case LibFunc::tmpfile:
1500 if (!FTy->getReturnType()->isPointerTy())
1501 return false;
1502 setDoesNotThrow(F);
1503 setDoesNotAlias(F, 0);
1504 break;
1505 case LibFunc::times:
1506 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1507 return false;
1508 setDoesNotThrow(F);
1509 setDoesNotCapture(F, 1);
1510 break;
1511 case LibFunc::htonl:
1512 case LibFunc::htons:
1513 case LibFunc::ntohl:
1514 case LibFunc::ntohs:
1515 setDoesNotThrow(F);
1516 setDoesNotAccessMemory(F);
1517 break;
1518 case LibFunc::lstat:
1519 if (FTy->getNumParams() != 2 ||
1520 !FTy->getParamType(0)->isPointerTy() ||
1521 !FTy->getParamType(1)->isPointerTy())
1522 return false;
1523 setDoesNotThrow(F);
1524 setDoesNotCapture(F, 1);
1525 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001526 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001527 break;
1528 case LibFunc::lchown:
1529 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1530 return false;
1531 setDoesNotThrow(F);
1532 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001533 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001534 break;
1535 case LibFunc::qsort:
1536 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1537 return false;
1538 // May throw; places call through function pointer.
1539 setDoesNotCapture(F, 4);
1540 break;
1541 case LibFunc::dunder_strdup:
1542 case LibFunc::dunder_strndup:
1543 if (FTy->getNumParams() < 1 ||
1544 !FTy->getReturnType()->isPointerTy() ||
1545 !FTy->getParamType(0)->isPointerTy())
1546 return false;
1547 setDoesNotThrow(F);
1548 setDoesNotAlias(F, 0);
1549 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001550 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001551 break;
1552 case LibFunc::dunder_strtok_r:
1553 if (FTy->getNumParams() != 3 ||
1554 !FTy->getParamType(1)->isPointerTy())
1555 return false;
1556 setDoesNotThrow(F);
1557 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001558 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001559 break;
1560 case LibFunc::under_IO_getc:
1561 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1562 return false;
1563 setDoesNotThrow(F);
1564 setDoesNotCapture(F, 1);
1565 break;
1566 case LibFunc::under_IO_putc:
1567 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1568 return false;
1569 setDoesNotThrow(F);
1570 setDoesNotCapture(F, 2);
1571 break;
1572 case LibFunc::dunder_isoc99_scanf:
1573 if (FTy->getNumParams() < 1 ||
1574 !FTy->getParamType(0)->isPointerTy())
1575 return false;
1576 setDoesNotThrow(F);
1577 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001578 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001579 break;
1580 case LibFunc::stat64:
1581 case LibFunc::lstat64:
1582 case LibFunc::statvfs64:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001583 if (FTy->getNumParams() < 1 ||
1584 !FTy->getParamType(0)->isPointerTy() ||
1585 !FTy->getParamType(1)->isPointerTy())
1586 return false;
1587 setDoesNotThrow(F);
1588 setDoesNotCapture(F, 1);
1589 setDoesNotCapture(F, 2);
1590 setOnlyReadsMemory(F, 1);
1591 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001592 case LibFunc::dunder_isoc99_sscanf:
1593 if (FTy->getNumParams() < 1 ||
1594 !FTy->getParamType(0)->isPointerTy() ||
1595 !FTy->getParamType(1)->isPointerTy())
1596 return false;
1597 setDoesNotThrow(F);
1598 setDoesNotCapture(F, 1);
1599 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001600 setOnlyReadsMemory(F, 1);
1601 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001602 break;
1603 case LibFunc::fopen64:
1604 if (FTy->getNumParams() != 2 ||
1605 !FTy->getReturnType()->isPointerTy() ||
1606 !FTy->getParamType(0)->isPointerTy() ||
1607 !FTy->getParamType(1)->isPointerTy())
1608 return false;
1609 setDoesNotThrow(F);
1610 setDoesNotAlias(F, 0);
1611 setDoesNotCapture(F, 1);
1612 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001613 setOnlyReadsMemory(F, 1);
1614 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001615 break;
1616 case LibFunc::fseeko64:
1617 case LibFunc::ftello64:
1618 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1619 return false;
1620 setDoesNotThrow(F);
1621 setDoesNotCapture(F, 1);
1622 break;
1623 case LibFunc::tmpfile64:
1624 if (!FTy->getReturnType()->isPointerTy())
1625 return false;
1626 setDoesNotThrow(F);
1627 setDoesNotAlias(F, 0);
1628 break;
1629 case LibFunc::fstat64:
1630 case LibFunc::fstatvfs64:
1631 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1632 return false;
1633 setDoesNotThrow(F);
1634 setDoesNotCapture(F, 2);
1635 break;
1636 case LibFunc::open64:
1637 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1638 return false;
1639 // May throw; "open" is a valid pthread cancellation point.
1640 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001641 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001642 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001643 case LibFunc::gettimeofday:
1644 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1645 !FTy->getParamType(1)->isPointerTy())
1646 return false;
1647 // Currently some platforms have the restrict keyword on the arguments to
1648 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1649 // arguments.
1650 setDoesNotThrow(F);
1651 setDoesNotCapture(F, 1);
1652 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001653 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001654 default:
1655 // Didn't mark any attributes.
1656 return false;
1657 }
1658
1659 return true;
1660}
1661
1662/// annotateLibraryCalls - Adds attributes to well-known standard library
1663/// call declarations.
1664bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1665 bool MadeChange = false;
1666
1667 // Check each function in turn annotating well-known library function
1668 // declarations with attributes.
1669 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1670 Function *F = (*I)->getFunction();
1671
Craig Topperf40110f2014-04-25 05:29:35 +00001672 if (F && F->isDeclaration())
Meador Inge6b6a1612013-03-21 00:55:59 +00001673 MadeChange |= inferPrototypeAttributes(*F);
1674 }
1675
1676 return MadeChange;
1677}
1678
Chris Lattner4422d312010-04-16 22:42:17 +00001679bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman86449d72010-11-08 16:10:15 +00001680 AA = &getAnalysis<AliasAnalysis>();
Meador Inge6b6a1612013-03-21 00:55:59 +00001681 TLI = &getAnalysis<TargetLibraryInfo>();
Dan Gohman86449d72010-11-08 16:10:15 +00001682
Meador Inge6b6a1612013-03-21 00:55:59 +00001683 bool Changed = annotateLibraryCalls(SCC);
1684 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001685 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001686 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001687 return Changed;
1688}