blob: 5299e648b6318363383fb7d9b1e8347b6325ae1d [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()) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000207 AAMDNodes AAInfo;
208 I->getAAMetadata(AAInfo);
209
Dan Gohman2694e142010-11-10 01:02:18 +0000210 AliasAnalysis::Location Loc(Arg,
Hal Finkelcc39b672014-07-24 12:16:19 +0000211 AliasAnalysis::UnknownSize, AAInfo);
Dan Gohman2694e142010-11-10 01:02:18 +0000212 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
213 if (MRB & AliasAnalysis::Mod)
214 // Writes non-local memory. Give up.
215 return false;
216 if (MRB & AliasAnalysis::Ref)
217 // Ok, it reads non-local memory.
218 ReadsMemory = true;
219 }
Dan Gohmande521552010-11-09 19:56:27 +0000220 }
221 }
Dan Gohmande521552010-11-09 19:56:27 +0000222 continue;
Dan Gohman86449d72010-11-08 16:10:15 +0000223 }
Dan Gohman2694e142010-11-10 01:02:18 +0000224 // The call could access any memory. If that includes writes, give up.
225 if (MRB & AliasAnalysis::Mod)
226 return false;
227 // If it reads, note it.
228 if (MRB & AliasAnalysis::Ref)
229 ReadsMemory = true;
230 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000231 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000232 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
233 if (!LI->isVolatile()) {
Dan Gohman65316d62010-11-11 21:50:19 +0000234 AliasAnalysis::Location Loc = AA->getLocation(LI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000235 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
236 continue;
237 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000238 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000239 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
240 if (!SI->isVolatile()) {
Dan Gohman65316d62010-11-11 21:50:19 +0000241 AliasAnalysis::Location Loc = AA->getLocation(SI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000242 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
243 continue;
244 }
Dan Gohmane3467a72010-11-09 20:17:38 +0000245 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
246 // Ignore vaargs on local memory.
Dan Gohman65316d62010-11-11 21:50:19 +0000247 AliasAnalysis::Location Loc = AA->getLocation(VI);
Dan Gohmane3467a72010-11-09 20:17:38 +0000248 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
249 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000250 }
251
252 // Any remaining instructions need to be taken seriously! Check if they
253 // read or write memory.
254 if (I->mayWriteToMemory())
255 // Writes memory. Just give up.
256 return false;
Duncan Sands9759f2e2009-05-06 08:42:00 +0000257
Duncan Sands44c8cd92008-12-31 16:14:43 +0000258 // If this instruction may read memory, remember that.
259 ReadsMemory |= I->mayReadFromMemory();
260 }
261 }
262
263 // Success! Functions in this SCC do not access memory, or only read memory.
264 // Give them the appropriate attribute.
265 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000266 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
267 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000268
269 if (F->doesNotAccessMemory())
270 // Already perfect!
271 continue;
272
273 if (F->onlyReadsMemory() && ReadsMemory)
274 // No change.
275 continue;
276
277 MadeChange = true;
278
279 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000280 AttrBuilder B;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000281 B.addAttribute(Attribute::ReadOnly)
282 .addAttribute(Attribute::ReadNone);
Bill Wendling430fa9b2013-01-23 00:45:55 +0000283 F->removeAttributes(AttributeSet::FunctionIndex,
284 AttributeSet::get(F->getContext(),
285 AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000286
287 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000288 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000289 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000290
291 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000292 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000293 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000294 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000295 }
296
297 return MadeChange;
298}
299
Nick Lewycky4c378a42011-12-28 23:24:21 +0000300namespace {
301 // For a given pointer Argument, this retains a list of Arguments of functions
302 // in the same SCC that the pointer data flows into. We use this to build an
303 // SCC of the arguments.
304 struct ArgumentGraphNode {
305 Argument *Definition;
306 SmallVector<ArgumentGraphNode*, 4> Uses;
307 };
308
309 class ArgumentGraph {
310 // We store pointers to ArgumentGraphNode objects, so it's important that
311 // that they not move around upon insert.
312 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
313
314 ArgumentMapTy ArgumentMap;
315
316 // There is no root node for the argument graph, in fact:
317 // void f(int *x, int *y) { if (...) f(x, y); }
318 // is an example where the graph is disconnected. The SCCIterator requires a
319 // single entry point, so we maintain a fake ("synthetic") root node that
320 // uses every node. Because the graph is directed and nothing points into
321 // the root, it will not participate in any SCCs (except for its own).
322 ArgumentGraphNode SyntheticRoot;
323
324 public:
Craig Topperf40110f2014-04-25 05:29:35 +0000325 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000326
327 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
328
329 iterator begin() { return SyntheticRoot.Uses.begin(); }
330 iterator end() { return SyntheticRoot.Uses.end(); }
331 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
332
333 ArgumentGraphNode *operator[](Argument *A) {
334 ArgumentGraphNode &Node = ArgumentMap[A];
335 Node.Definition = A;
336 SyntheticRoot.Uses.push_back(&Node);
337 return &Node;
338 }
339 };
340
341 // This tracker checks whether callees are in the SCC, and if so it does not
342 // consider that a capture, instead adding it to the "Uses" list and
343 // continuing with the analysis.
344 struct ArgumentUsesTracker : public CaptureTracker {
345 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
346 : Captured(false), SCCNodes(SCCNodes) {}
347
Craig Topper3e4c6972014-03-05 09:10:37 +0000348 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000349
Chandler Carruth64e9aa52014-03-05 10:21:48 +0000350 bool captured(const Use *U) override {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000351 CallSite CS(U->getUser());
352 if (!CS.getInstruction()) { Captured = true; return true; }
353
354 Function *F = CS.getCalledFunction();
355 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
356
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000357 bool Found = false;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000358 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
359 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
360 PI != PE; ++PI, ++AI) {
361 if (AI == AE) {
362 assert(F->isVarArg() && "More params than args in non-varargs call");
363 Captured = true;
364 return true;
365 }
366 if (PI == U) {
367 Uses.push_back(AI);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000368 Found = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000369 break;
370 }
371 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000372 assert(Found && "Capturing call-site captured nothing?");
Duncan Sandsc9e95ad2013-09-13 08:16:06 +0000373 (void)Found;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000374 return false;
375 }
376
377 bool Captured; // True only if certainly captured (used outside our SCC).
378 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
379
380 const SmallPtrSet<Function*, 8> &SCCNodes;
381 };
382}
383
384namespace llvm {
385 template<> struct GraphTraits<ArgumentGraphNode*> {
386 typedef ArgumentGraphNode NodeType;
387 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
388
389 static inline NodeType *getEntryNode(NodeType *A) { return A; }
390 static inline ChildIteratorType child_begin(NodeType *N) {
391 return N->Uses.begin();
392 }
393 static inline ChildIteratorType child_end(NodeType *N) {
394 return N->Uses.end();
395 }
396 };
397 template<> struct GraphTraits<ArgumentGraph*>
398 : public GraphTraits<ArgumentGraphNode*> {
399 static NodeType *getEntryNode(ArgumentGraph *AG) {
400 return AG->getEntryNode();
401 }
402 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
403 return AG->begin();
404 }
405 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
406 return AG->end();
407 }
408 };
409}
410
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000411// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
412static Attribute::AttrKind
413determinePointerReadAttrs(Argument *A,
414 const SmallPtrSet<Argument*, 8> &SCCNodes) {
415
416 SmallVector<Use*, 32> Worklist;
417 SmallSet<Use*, 32> Visited;
418 int Count = 0;
419
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000420 // inalloca arguments are always clobbered by the call.
421 if (A->hasInAllocaAttr())
422 return Attribute::None;
423
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000424 bool IsRead = false;
425 // We don't need to track IsWritten. If A is written to, return immediately.
426
Chandler Carruthcdf47882014-03-09 03:16:01 +0000427 for (Use &U : A->uses()) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000428 if (Count++ >= 20)
429 return Attribute::None;
430
Chandler Carruthcdf47882014-03-09 03:16:01 +0000431 Visited.insert(&U);
432 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000433 }
434
435 while (!Worklist.empty()) {
436 Use *U = Worklist.pop_back_val();
437 Instruction *I = cast<Instruction>(U->getUser());
438 Value *V = U->get();
439
440 switch (I->getOpcode()) {
441 case Instruction::BitCast:
442 case Instruction::GetElementPtr:
443 case Instruction::PHI:
444 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000445 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000446 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000447 for (Use &UU : I->uses())
448 if (Visited.insert(&UU))
449 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000450 break;
451
452 case Instruction::Call:
453 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000454 bool Captures = true;
455
456 if (I->getType()->isVoidTy())
457 Captures = false;
458
459 auto AddUsersToWorklistIfCapturing = [&] {
460 if (Captures)
461 for (Use &UU : I->uses())
462 if (Visited.insert(&UU))
463 Worklist.push_back(&UU);
464 };
465
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000466 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000467 if (CS.doesNotAccessMemory()) {
468 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000469 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000470 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000471
472 Function *F = CS.getCalledFunction();
473 if (!F) {
474 if (CS.onlyReadsMemory()) {
475 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000476 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000477 continue;
478 }
479 return Attribute::None;
480 }
481
482 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
483 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
484 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
485 if (A->get() == V) {
486 if (AI == AE) {
487 assert(F->isVarArg() &&
488 "More params than args in non-varargs call.");
489 return Attribute::None;
490 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000491 Captures &= !CS.doesNotCapture(A - B);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000492 if (SCCNodes.count(AI))
493 continue;
494 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
495 return Attribute::None;
496 if (!CS.doesNotAccessMemory(A - B))
497 IsRead = true;
498 }
499 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000500 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000501 break;
502 }
503
504 case Instruction::Load:
505 IsRead = true;
506 break;
507
508 case Instruction::ICmp:
509 case Instruction::Ret:
510 break;
511
512 default:
513 return Attribute::None;
514 }
515 }
516
517 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
518}
519
520/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
521bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000522 bool Changed = false;
523
Nick Lewycky4c378a42011-12-28 23:24:21 +0000524 SmallPtrSet<Function*, 8> SCCNodes;
525
526 // Fill SCCNodes with the elements of the SCC. Used for quickly
527 // looking up whether a given CallGraphNode is in this SCC.
528 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
529 Function *F = (*I)->getFunction();
530 if (F && !F->isDeclaration() && !F->mayBeOverridden())
531 SCCNodes.insert(F);
532 }
533
534 ArgumentGraph AG;
535
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000536 AttrBuilder B;
537 B.addAttribute(Attribute::NoCapture);
538
Duncan Sands44c8cd92008-12-31 16:14:43 +0000539 // Check each function in turn, determining which pointer arguments are not
540 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000541 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
542 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000543
Craig Topperf40110f2014-04-25 05:29:35 +0000544 if (!F)
Nick Lewycky4c378a42011-12-28 23:24:21 +0000545 // External node - only a problem for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000546 continue;
547
548 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000549 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000550 if (F->isDeclaration() || F->mayBeOverridden())
551 continue;
552
Nick Lewycky4c378a42011-12-28 23:24:21 +0000553 // Functions that are readonly (or readnone) and nounwind and don't return
554 // a value can't capture arguments. Don't analyze them.
555 if (F->onlyReadsMemory() && F->doesNotThrow() &&
556 F->getReturnType()->isVoidTy()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000557 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
558 A != E; ++A) {
559 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
560 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
561 ++NumNoCapture;
562 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000563 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000564 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000565 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000566 }
567
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000568 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
569 A != E; ++A) {
570 if (!A->getType()->isPointerTy()) continue;
571 bool HasNonLocalUses = false;
572 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000573 ArgumentUsesTracker Tracker(SCCNodes);
574 PointerMayBeCaptured(A, &Tracker);
575 if (!Tracker.Captured) {
576 if (Tracker.Uses.empty()) {
577 // If it's trivially not captured, mark it nocapture now.
578 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
579 ++NumNoCapture;
580 Changed = true;
581 } else {
582 // If it's not trivially captured and not trivially not captured,
583 // then it must be calling into another function in our SCC. Save
584 // its particulars for Argument-SCC analysis later.
585 ArgumentGraphNode *Node = AG[A];
586 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000587 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000588 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000589 if (*UI != A)
590 HasNonLocalUses = true;
591 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000592 }
593 }
594 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
595 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000596 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
597 // Can we determine that it's readonly/readnone without doing an SCC?
598 // Note that we don't allow any calls at all here, or else our result
599 // will be dependent on the iteration order through the functions in the
600 // SCC.
601 SmallPtrSet<Argument*, 8> Self;
602 Self.insert(A);
603 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
604 if (R != Attribute::None) {
605 AttrBuilder B;
606 B.addAttribute(R);
607 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
608 Changed = true;
609 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
610 }
611 }
612 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000613 }
614
615 // The graph we've collected is partial because we stopped scanning for
616 // argument uses once we solved the argument trivially. These partial nodes
617 // show up as ArgumentGraphNode objects with an empty Uses list, and for
618 // these nodes the final decision about whether they capture has already been
619 // made. If the definition doesn't have a 'nocapture' attribute by now, it
620 // captures.
621
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000622 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000623 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000624 if (ArgumentSCC.size() == 1) {
625 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
626
627 // eg. "void f(int* x) { if (...) f(x); }"
628 if (ArgumentSCC[0]->Uses.size() == 1 &&
629 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000630 Argument *A = ArgumentSCC[0]->Definition;
631 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000632 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000633 Changed = true;
634 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000635 continue;
636 }
637
638 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000639 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
640 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000641 ArgumentGraphNode *Node = *I;
642 if (Node->Uses.empty()) {
643 if (!Node->Definition->hasNoCaptureAttr())
644 SCCCaptured = true;
645 }
646 }
647 if (SCCCaptured) continue;
648
649 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
650 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
651 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000652 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000653 ArgumentSCCNodes.insert((*I)->Definition);
654 }
655
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000656 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
657 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000658 ArgumentGraphNode *N = *I;
659 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
660 UE = N->Uses.end(); UI != UE; ++UI) {
661 Argument *A = (*UI)->Definition;
662 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
663 continue;
664 SCCCaptured = true;
665 break;
666 }
667 }
668 if (SCCCaptured) continue;
669
Nick Lewyckyf740db32012-01-05 22:21:45 +0000670 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000671 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000672 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000673 ++NumNoCapture;
674 Changed = true;
675 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000676
677 // We also want to compute readonly/readnone. With a small number of false
678 // negatives, we can assume that any pointer which is captured isn't going
679 // to be provably readonly or readnone, since by definition we can't
680 // analyze all uses of a captured pointer.
681 //
682 // The false negatives happen when the pointer is captured by a function
683 // that promises readonly/readnone behaviour on the pointer, then the
684 // pointer's lifetime ends before anything that writes to arbitrary memory.
685 // Also, a readonly/readnone pointer may be returned, but returning a
686 // pointer is capturing it.
687
688 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
689 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
690 Argument *A = ArgumentSCC[i]->Definition;
691 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
692 if (K == Attribute::ReadNone)
693 continue;
694 if (K == Attribute::ReadOnly) {
695 ReadAttr = Attribute::ReadOnly;
696 continue;
697 }
698 ReadAttr = K;
699 break;
700 }
701
702 if (ReadAttr != Attribute::None) {
703 AttrBuilder B;
704 B.addAttribute(ReadAttr);
705 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
706 Argument *A = ArgumentSCC[i]->Definition;
707 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
708 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
709 Changed = true;
710 }
711 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000712 }
713
714 return Changed;
715}
716
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000717/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000718/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000719bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +0000720 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramer15591272012-10-31 13:45:49 +0000721 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000722 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
723 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
724 FlowsToReturn.insert(Ret->getReturnValue());
725
726 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000727 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000728
729 if (Constant *C = dyn_cast<Constant>(RetVal)) {
730 if (!C->isNullValue() && !isa<UndefValue>(C))
731 return false;
732
733 continue;
734 }
735
736 if (isa<Argument>(RetVal))
737 return false;
738
739 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
740 switch (RVI->getOpcode()) {
741 // Extend the analysis by looking upwards.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000742 case Instruction::BitCast:
Victor Hernandez5d034492009-09-18 22:35:49 +0000743 case Instruction::GetElementPtr:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000744 case Instruction::AddrSpaceCast:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000745 FlowsToReturn.insert(RVI->getOperand(0));
746 continue;
747 case Instruction::Select: {
748 SelectInst *SI = cast<SelectInst>(RVI);
749 FlowsToReturn.insert(SI->getTrueValue());
750 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner43d0db72009-09-27 21:29:28 +0000751 continue;
752 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000753 case Instruction::PHI: {
754 PHINode *PN = cast<PHINode>(RVI);
755 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
756 FlowsToReturn.insert(PN->getIncomingValue(i));
Chris Lattner43d0db72009-09-27 21:29:28 +0000757 continue;
758 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000759
760 // Check whether the pointer came from an allocation.
761 case Instruction::Alloca:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000762 break;
763 case Instruction::Call:
764 case Instruction::Invoke: {
765 CallSite CS(RVI);
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000766 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000767 break;
768 if (CS.getCalledFunction() &&
Chris Lattner2f2110a2009-08-31 04:09:04 +0000769 SCCNodes.count(CS.getCalledFunction()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000770 break;
771 } // fall-through
772 default:
773 return false; // Did not come from an allocation.
774 }
775
Dan Gohman94e61762009-11-19 21:57:48 +0000776 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000777 return false;
778 }
779
780 return true;
781}
782
783/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000784bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000785 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000786
787 // Fill SCCNodes with the elements of the SCC. Used for quickly
788 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000789 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
790 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000791
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000792 // Check each function in turn, determining which functions return noalias
793 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000794 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
795 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000796
Craig Topperf40110f2014-04-25 05:29:35 +0000797 if (!F)
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000798 // External node - skip it;
799 return false;
800
801 // Already noalias.
802 if (F->doesNotAlias(0))
803 continue;
804
805 // Definitions with weak linkage may be overridden at linktime, so
806 // treat them like declarations.
807 if (F->isDeclaration() || F->mayBeOverridden())
808 return false;
809
810 // We annotate noalias return values, which are only applicable to
811 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000812 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000813 continue;
814
815 if (!IsFunctionMallocLike(F, SCCNodes))
816 return false;
817 }
818
819 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000820 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
821 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000822 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000823 continue;
824
825 F->setDoesNotAlias(0);
826 ++NumNoAlias;
827 MadeChange = true;
828 }
829
830 return MadeChange;
831}
832
Meador Inge6b6a1612013-03-21 00:55:59 +0000833/// inferPrototypeAttributes - Analyze the name and prototype of the
834/// given function and set any applicable attributes. Returns true
835/// if any attributes were set and false otherwise.
836bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
837 FunctionType *FTy = F.getFunctionType();
838 LibFunc::Func TheLibFunc;
839 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
840 return false;
841
842 switch (TheLibFunc) {
843 case LibFunc::strlen:
844 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
845 return false;
846 setOnlyReadsMemory(F);
847 setDoesNotThrow(F);
848 setDoesNotCapture(F, 1);
849 break;
850 case LibFunc::strchr:
851 case LibFunc::strrchr:
852 if (FTy->getNumParams() != 2 ||
853 !FTy->getParamType(0)->isPointerTy() ||
854 !FTy->getParamType(1)->isIntegerTy())
855 return false;
856 setOnlyReadsMemory(F);
857 setDoesNotThrow(F);
858 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000859 case LibFunc::strtol:
860 case LibFunc::strtod:
861 case LibFunc::strtof:
862 case LibFunc::strtoul:
863 case LibFunc::strtoll:
864 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +0000865 case LibFunc::strtoull:
866 if (FTy->getNumParams() < 2 ||
867 !FTy->getParamType(1)->isPointerTy())
868 return false;
869 setDoesNotThrow(F);
870 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000871 setOnlyReadsMemory(F, 1);
872 break;
873 case LibFunc::strcpy:
874 case LibFunc::stpcpy:
875 case LibFunc::strcat:
876 case LibFunc::strncat:
877 case LibFunc::strncpy:
878 case LibFunc::stpncpy:
879 if (FTy->getNumParams() < 2 ||
880 !FTy->getParamType(1)->isPointerTy())
881 return false;
882 setDoesNotThrow(F);
883 setDoesNotCapture(F, 2);
884 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000885 break;
886 case LibFunc::strxfrm:
887 if (FTy->getNumParams() != 3 ||
888 !FTy->getParamType(0)->isPointerTy() ||
889 !FTy->getParamType(1)->isPointerTy())
890 return false;
891 setDoesNotThrow(F);
892 setDoesNotCapture(F, 1);
893 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000894 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000895 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000896 case LibFunc::strcmp: //0,1
897 case LibFunc::strspn: // 0,1
898 case LibFunc::strncmp: // 0,1
899 case LibFunc::strcspn: //0,1
900 case LibFunc::strcoll: //0,1
901 case LibFunc::strcasecmp: // 0,1
902 case LibFunc::strncasecmp: //
Meador Inge6b6a1612013-03-21 00:55:59 +0000903 if (FTy->getNumParams() < 2 ||
904 !FTy->getParamType(0)->isPointerTy() ||
905 !FTy->getParamType(1)->isPointerTy())
906 return false;
907 setOnlyReadsMemory(F);
908 setDoesNotThrow(F);
909 setDoesNotCapture(F, 1);
910 setDoesNotCapture(F, 2);
911 break;
912 case LibFunc::strstr:
913 case LibFunc::strpbrk:
914 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
915 return false;
916 setOnlyReadsMemory(F);
917 setDoesNotThrow(F);
918 setDoesNotCapture(F, 2);
919 break;
920 case LibFunc::strtok:
921 case LibFunc::strtok_r:
922 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
923 return false;
924 setDoesNotThrow(F);
925 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000926 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000927 break;
928 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000929 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
930 return false;
931 setDoesNotThrow(F);
932 setDoesNotCapture(F, 1);
933 setOnlyReadsMemory(F, 1);
934 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000935 case LibFunc::setbuf:
936 case LibFunc::setvbuf:
937 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
938 return false;
939 setDoesNotThrow(F);
940 setDoesNotCapture(F, 1);
941 break;
942 case LibFunc::strdup:
943 case LibFunc::strndup:
944 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
945 !FTy->getParamType(0)->isPointerTy())
946 return false;
947 setDoesNotThrow(F);
948 setDoesNotAlias(F, 0);
949 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000950 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +0000951 break;
952 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +0000953 case LibFunc::statvfs:
954 if (FTy->getNumParams() < 2 ||
955 !FTy->getParamType(0)->isPointerTy() ||
956 !FTy->getParamType(1)->isPointerTy())
957 return false;
958 setDoesNotThrow(F);
959 setDoesNotCapture(F, 1);
960 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000961 setOnlyReadsMemory(F, 1);
962 break;
963 case LibFunc::sscanf:
964 if (FTy->getNumParams() < 2 ||
965 !FTy->getParamType(0)->isPointerTy() ||
966 !FTy->getParamType(1)->isPointerTy())
967 return false;
968 setDoesNotThrow(F);
969 setDoesNotCapture(F, 1);
970 setDoesNotCapture(F, 2);
971 setOnlyReadsMemory(F, 1);
972 setOnlyReadsMemory(F, 2);
973 break;
974 case LibFunc::sprintf:
975 if (FTy->getNumParams() < 2 ||
976 !FTy->getParamType(0)->isPointerTy() ||
977 !FTy->getParamType(1)->isPointerTy())
978 return false;
979 setDoesNotThrow(F);
980 setDoesNotCapture(F, 1);
981 setDoesNotCapture(F, 2);
982 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000983 break;
984 case LibFunc::snprintf:
985 if (FTy->getNumParams() != 3 ||
986 !FTy->getParamType(0)->isPointerTy() ||
987 !FTy->getParamType(2)->isPointerTy())
988 return false;
989 setDoesNotThrow(F);
990 setDoesNotCapture(F, 1);
991 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000992 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +0000993 break;
994 case LibFunc::setitimer:
995 if (FTy->getNumParams() != 3 ||
996 !FTy->getParamType(1)->isPointerTy() ||
997 !FTy->getParamType(2)->isPointerTy())
998 return false;
999 setDoesNotThrow(F);
1000 setDoesNotCapture(F, 2);
1001 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001002 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001003 break;
1004 case LibFunc::system:
1005 if (FTy->getNumParams() != 1 ||
1006 !FTy->getParamType(0)->isPointerTy())
1007 return false;
1008 // May throw; "system" is a valid pthread cancellation point.
1009 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001010 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001011 break;
1012 case LibFunc::malloc:
1013 if (FTy->getNumParams() != 1 ||
1014 !FTy->getReturnType()->isPointerTy())
1015 return false;
1016 setDoesNotThrow(F);
1017 setDoesNotAlias(F, 0);
1018 break;
1019 case LibFunc::memcmp:
1020 if (FTy->getNumParams() != 3 ||
1021 !FTy->getParamType(0)->isPointerTy() ||
1022 !FTy->getParamType(1)->isPointerTy())
1023 return false;
1024 setOnlyReadsMemory(F);
1025 setDoesNotThrow(F);
1026 setDoesNotCapture(F, 1);
1027 setDoesNotCapture(F, 2);
1028 break;
1029 case LibFunc::memchr:
1030 case LibFunc::memrchr:
1031 if (FTy->getNumParams() != 3)
1032 return false;
1033 setOnlyReadsMemory(F);
1034 setDoesNotThrow(F);
1035 break;
1036 case LibFunc::modf:
1037 case LibFunc::modff:
1038 case LibFunc::modfl:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001039 if (FTy->getNumParams() < 2 ||
1040 !FTy->getParamType(1)->isPointerTy())
1041 return false;
1042 setDoesNotThrow(F);
1043 setDoesNotCapture(F, 2);
1044 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001045 case LibFunc::memcpy:
1046 case LibFunc::memccpy:
1047 case LibFunc::memmove:
1048 if (FTy->getNumParams() < 2 ||
1049 !FTy->getParamType(1)->isPointerTy())
1050 return false;
1051 setDoesNotThrow(F);
1052 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001053 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001054 break;
1055 case LibFunc::memalign:
1056 if (!FTy->getReturnType()->isPointerTy())
1057 return false;
1058 setDoesNotAlias(F, 0);
1059 break;
1060 case LibFunc::mkdir:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001061 if (FTy->getNumParams() == 0 ||
1062 !FTy->getParamType(0)->isPointerTy())
1063 return false;
1064 setDoesNotThrow(F);
1065 setDoesNotCapture(F, 1);
1066 setOnlyReadsMemory(F, 1);
1067 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001068 case LibFunc::mktime:
1069 if (FTy->getNumParams() == 0 ||
1070 !FTy->getParamType(0)->isPointerTy())
1071 return false;
1072 setDoesNotThrow(F);
1073 setDoesNotCapture(F, 1);
1074 break;
1075 case LibFunc::realloc:
1076 if (FTy->getNumParams() != 2 ||
1077 !FTy->getParamType(0)->isPointerTy() ||
1078 !FTy->getReturnType()->isPointerTy())
1079 return false;
1080 setDoesNotThrow(F);
1081 setDoesNotAlias(F, 0);
1082 setDoesNotCapture(F, 1);
1083 break;
1084 case LibFunc::read:
1085 if (FTy->getNumParams() != 3 ||
1086 !FTy->getParamType(1)->isPointerTy())
1087 return false;
1088 // May throw; "read" is a valid pthread cancellation point.
1089 setDoesNotCapture(F, 2);
1090 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001091 case LibFunc::rewind:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001092 if (FTy->getNumParams() < 1 ||
1093 !FTy->getParamType(0)->isPointerTy())
1094 return false;
1095 setDoesNotThrow(F);
1096 setDoesNotCapture(F, 1);
1097 break;
1098 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001099 case LibFunc::remove:
1100 case LibFunc::realpath:
1101 if (FTy->getNumParams() < 1 ||
1102 !FTy->getParamType(0)->isPointerTy())
1103 return false;
1104 setDoesNotThrow(F);
1105 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001106 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001107 break;
1108 case LibFunc::rename:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001109 if (FTy->getNumParams() < 2 ||
1110 !FTy->getParamType(0)->isPointerTy() ||
1111 !FTy->getParamType(1)->isPointerTy())
1112 return false;
1113 setDoesNotThrow(F);
1114 setDoesNotCapture(F, 1);
1115 setDoesNotCapture(F, 2);
1116 setOnlyReadsMemory(F, 1);
1117 setOnlyReadsMemory(F, 2);
1118 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001119 case LibFunc::readlink:
1120 if (FTy->getNumParams() < 2 ||
1121 !FTy->getParamType(0)->isPointerTy() ||
1122 !FTy->getParamType(1)->isPointerTy())
1123 return false;
1124 setDoesNotThrow(F);
1125 setDoesNotCapture(F, 1);
1126 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001127 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001128 break;
1129 case LibFunc::write:
1130 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1131 return false;
1132 // May throw; "write" is a valid pthread cancellation point.
1133 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001134 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001135 break;
1136 case LibFunc::bcopy:
1137 if (FTy->getNumParams() != 3 ||
1138 !FTy->getParamType(0)->isPointerTy() ||
1139 !FTy->getParamType(1)->isPointerTy())
1140 return false;
1141 setDoesNotThrow(F);
1142 setDoesNotCapture(F, 1);
1143 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001144 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001145 break;
1146 case LibFunc::bcmp:
1147 if (FTy->getNumParams() != 3 ||
1148 !FTy->getParamType(0)->isPointerTy() ||
1149 !FTy->getParamType(1)->isPointerTy())
1150 return false;
1151 setDoesNotThrow(F);
1152 setOnlyReadsMemory(F);
1153 setDoesNotCapture(F, 1);
1154 setDoesNotCapture(F, 2);
1155 break;
1156 case LibFunc::bzero:
1157 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1158 return false;
1159 setDoesNotThrow(F);
1160 setDoesNotCapture(F, 1);
1161 break;
1162 case LibFunc::calloc:
1163 if (FTy->getNumParams() != 2 ||
1164 !FTy->getReturnType()->isPointerTy())
1165 return false;
1166 setDoesNotThrow(F);
1167 setDoesNotAlias(F, 0);
1168 break;
1169 case LibFunc::chmod:
1170 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001171 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1172 return false;
1173 setDoesNotThrow(F);
1174 setDoesNotCapture(F, 1);
1175 setOnlyReadsMemory(F, 1);
1176 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001177 case LibFunc::ctermid:
1178 case LibFunc::clearerr:
1179 case LibFunc::closedir:
1180 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1181 return false;
1182 setDoesNotThrow(F);
1183 setDoesNotCapture(F, 1);
1184 break;
1185 case LibFunc::atoi:
1186 case LibFunc::atol:
1187 case LibFunc::atof:
1188 case LibFunc::atoll:
1189 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1190 return false;
1191 setDoesNotThrow(F);
1192 setOnlyReadsMemory(F);
1193 setDoesNotCapture(F, 1);
1194 break;
1195 case LibFunc::access:
1196 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1197 return false;
1198 setDoesNotThrow(F);
1199 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001200 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001201 break;
1202 case LibFunc::fopen:
1203 if (FTy->getNumParams() != 2 ||
1204 !FTy->getReturnType()->isPointerTy() ||
1205 !FTy->getParamType(0)->isPointerTy() ||
1206 !FTy->getParamType(1)->isPointerTy())
1207 return false;
1208 setDoesNotThrow(F);
1209 setDoesNotAlias(F, 0);
1210 setDoesNotCapture(F, 1);
1211 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001212 setOnlyReadsMemory(F, 1);
1213 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001214 break;
1215 case LibFunc::fdopen:
1216 if (FTy->getNumParams() != 2 ||
1217 !FTy->getReturnType()->isPointerTy() ||
1218 !FTy->getParamType(1)->isPointerTy())
1219 return false;
1220 setDoesNotThrow(F);
1221 setDoesNotAlias(F, 0);
1222 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001223 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001224 break;
1225 case LibFunc::feof:
1226 case LibFunc::free:
1227 case LibFunc::fseek:
1228 case LibFunc::ftell:
1229 case LibFunc::fgetc:
1230 case LibFunc::fseeko:
1231 case LibFunc::ftello:
1232 case LibFunc::fileno:
1233 case LibFunc::fflush:
1234 case LibFunc::fclose:
1235 case LibFunc::fsetpos:
1236 case LibFunc::flockfile:
1237 case LibFunc::funlockfile:
1238 case LibFunc::ftrylockfile:
1239 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1240 return false;
1241 setDoesNotThrow(F);
1242 setDoesNotCapture(F, 1);
1243 break;
1244 case LibFunc::ferror:
1245 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1246 return false;
1247 setDoesNotThrow(F);
1248 setDoesNotCapture(F, 1);
1249 setOnlyReadsMemory(F);
1250 break;
1251 case LibFunc::fputc:
1252 case LibFunc::fstat:
1253 case LibFunc::frexp:
1254 case LibFunc::frexpf:
1255 case LibFunc::frexpl:
1256 case LibFunc::fstatvfs:
1257 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1258 return false;
1259 setDoesNotThrow(F);
1260 setDoesNotCapture(F, 2);
1261 break;
1262 case LibFunc::fgets:
1263 if (FTy->getNumParams() != 3 ||
1264 !FTy->getParamType(0)->isPointerTy() ||
1265 !FTy->getParamType(2)->isPointerTy())
1266 return false;
1267 setDoesNotThrow(F);
1268 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001269 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001270 case LibFunc::fread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001271 if (FTy->getNumParams() != 4 ||
1272 !FTy->getParamType(0)->isPointerTy() ||
1273 !FTy->getParamType(3)->isPointerTy())
1274 return false;
1275 setDoesNotThrow(F);
1276 setDoesNotCapture(F, 1);
1277 setDoesNotCapture(F, 4);
1278 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001279 case LibFunc::fwrite:
1280 if (FTy->getNumParams() != 4 ||
1281 !FTy->getParamType(0)->isPointerTy() ||
1282 !FTy->getParamType(3)->isPointerTy())
1283 return false;
1284 setDoesNotThrow(F);
1285 setDoesNotCapture(F, 1);
1286 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001287 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001288 case LibFunc::fputs:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001289 if (FTy->getNumParams() < 2 ||
1290 !FTy->getParamType(0)->isPointerTy() ||
1291 !FTy->getParamType(1)->isPointerTy())
1292 return false;
1293 setDoesNotThrow(F);
1294 setDoesNotCapture(F, 1);
1295 setDoesNotCapture(F, 2);
1296 setOnlyReadsMemory(F, 1);
1297 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001298 case LibFunc::fscanf:
1299 case LibFunc::fprintf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001300 if (FTy->getNumParams() < 2 ||
1301 !FTy->getParamType(0)->isPointerTy() ||
1302 !FTy->getParamType(1)->isPointerTy())
1303 return false;
1304 setDoesNotThrow(F);
1305 setDoesNotCapture(F, 1);
1306 setDoesNotCapture(F, 2);
1307 setOnlyReadsMemory(F, 2);
1308 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001309 case LibFunc::fgetpos:
1310 if (FTy->getNumParams() < 2 ||
1311 !FTy->getParamType(0)->isPointerTy() ||
1312 !FTy->getParamType(1)->isPointerTy())
1313 return false;
1314 setDoesNotThrow(F);
1315 setDoesNotCapture(F, 1);
1316 setDoesNotCapture(F, 2);
1317 break;
1318 case LibFunc::getc:
1319 case LibFunc::getlogin_r:
1320 case LibFunc::getc_unlocked:
1321 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1322 return false;
1323 setDoesNotThrow(F);
1324 setDoesNotCapture(F, 1);
1325 break;
1326 case LibFunc::getenv:
1327 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1328 return false;
1329 setDoesNotThrow(F);
1330 setOnlyReadsMemory(F);
1331 setDoesNotCapture(F, 1);
1332 break;
1333 case LibFunc::gets:
1334 case LibFunc::getchar:
1335 setDoesNotThrow(F);
1336 break;
1337 case LibFunc::getitimer:
1338 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1339 return false;
1340 setDoesNotThrow(F);
1341 setDoesNotCapture(F, 2);
1342 break;
1343 case LibFunc::getpwnam:
1344 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1345 return false;
1346 setDoesNotThrow(F);
1347 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001348 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001349 break;
1350 case LibFunc::ungetc:
1351 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1352 return false;
1353 setDoesNotThrow(F);
1354 setDoesNotCapture(F, 2);
1355 break;
1356 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001357 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1358 return false;
1359 setDoesNotThrow(F);
1360 setDoesNotCapture(F, 1);
1361 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001362 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001363 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1364 return false;
1365 setDoesNotThrow(F);
1366 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001367 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001368 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001369 case LibFunc::unsetenv:
1370 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1371 return false;
1372 setDoesNotThrow(F);
1373 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001374 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001375 break;
1376 case LibFunc::utime:
1377 case LibFunc::utimes:
1378 if (FTy->getNumParams() != 2 ||
1379 !FTy->getParamType(0)->isPointerTy() ||
1380 !FTy->getParamType(1)->isPointerTy())
1381 return false;
1382 setDoesNotThrow(F);
1383 setDoesNotCapture(F, 1);
1384 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001385 setOnlyReadsMemory(F, 1);
1386 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001387 break;
1388 case LibFunc::putc:
1389 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1390 return false;
1391 setDoesNotThrow(F);
1392 setDoesNotCapture(F, 2);
1393 break;
1394 case LibFunc::puts:
1395 case LibFunc::printf:
1396 case LibFunc::perror:
1397 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1398 return false;
1399 setDoesNotThrow(F);
1400 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001401 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001402 break;
1403 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001404 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1405 return false;
1406 // May throw; "pread" is a valid pthread cancellation point.
1407 setDoesNotCapture(F, 2);
1408 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001409 case LibFunc::pwrite:
1410 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1411 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001412 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001413 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001414 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001415 break;
1416 case LibFunc::putchar:
1417 setDoesNotThrow(F);
1418 break;
1419 case LibFunc::popen:
1420 if (FTy->getNumParams() != 2 ||
1421 !FTy->getReturnType()->isPointerTy() ||
1422 !FTy->getParamType(0)->isPointerTy() ||
1423 !FTy->getParamType(1)->isPointerTy())
1424 return false;
1425 setDoesNotThrow(F);
1426 setDoesNotAlias(F, 0);
1427 setDoesNotCapture(F, 1);
1428 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001429 setOnlyReadsMemory(F, 1);
1430 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001431 break;
1432 case LibFunc::pclose:
1433 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1434 return false;
1435 setDoesNotThrow(F);
1436 setDoesNotCapture(F, 1);
1437 break;
1438 case LibFunc::vscanf:
1439 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1440 return false;
1441 setDoesNotThrow(F);
1442 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001443 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001444 break;
1445 case LibFunc::vsscanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001446 if (FTy->getNumParams() != 3 ||
1447 !FTy->getParamType(1)->isPointerTy() ||
1448 !FTy->getParamType(2)->isPointerTy())
1449 return false;
1450 setDoesNotThrow(F);
1451 setDoesNotCapture(F, 1);
1452 setDoesNotCapture(F, 2);
1453 setOnlyReadsMemory(F, 1);
1454 setOnlyReadsMemory(F, 2);
1455 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001456 case LibFunc::vfscanf:
1457 if (FTy->getNumParams() != 3 ||
1458 !FTy->getParamType(1)->isPointerTy() ||
1459 !FTy->getParamType(2)->isPointerTy())
1460 return false;
1461 setDoesNotThrow(F);
1462 setDoesNotCapture(F, 1);
1463 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001464 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001465 break;
1466 case LibFunc::valloc:
1467 if (!FTy->getReturnType()->isPointerTy())
1468 return false;
1469 setDoesNotThrow(F);
1470 setDoesNotAlias(F, 0);
1471 break;
1472 case LibFunc::vprintf:
1473 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1474 return false;
1475 setDoesNotThrow(F);
1476 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001477 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001478 break;
1479 case LibFunc::vfprintf:
1480 case LibFunc::vsprintf:
1481 if (FTy->getNumParams() != 3 ||
1482 !FTy->getParamType(0)->isPointerTy() ||
1483 !FTy->getParamType(1)->isPointerTy())
1484 return false;
1485 setDoesNotThrow(F);
1486 setDoesNotCapture(F, 1);
1487 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001488 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001489 break;
1490 case LibFunc::vsnprintf:
1491 if (FTy->getNumParams() != 4 ||
1492 !FTy->getParamType(0)->isPointerTy() ||
1493 !FTy->getParamType(2)->isPointerTy())
1494 return false;
1495 setDoesNotThrow(F);
1496 setDoesNotCapture(F, 1);
1497 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001498 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001499 break;
1500 case LibFunc::open:
1501 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1502 return false;
1503 // May throw; "open" is a valid pthread cancellation point.
1504 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001505 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001506 break;
1507 case LibFunc::opendir:
1508 if (FTy->getNumParams() != 1 ||
1509 !FTy->getReturnType()->isPointerTy() ||
1510 !FTy->getParamType(0)->isPointerTy())
1511 return false;
1512 setDoesNotThrow(F);
1513 setDoesNotAlias(F, 0);
1514 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001515 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001516 break;
1517 case LibFunc::tmpfile:
1518 if (!FTy->getReturnType()->isPointerTy())
1519 return false;
1520 setDoesNotThrow(F);
1521 setDoesNotAlias(F, 0);
1522 break;
1523 case LibFunc::times:
1524 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1525 return false;
1526 setDoesNotThrow(F);
1527 setDoesNotCapture(F, 1);
1528 break;
1529 case LibFunc::htonl:
1530 case LibFunc::htons:
1531 case LibFunc::ntohl:
1532 case LibFunc::ntohs:
1533 setDoesNotThrow(F);
1534 setDoesNotAccessMemory(F);
1535 break;
1536 case LibFunc::lstat:
1537 if (FTy->getNumParams() != 2 ||
1538 !FTy->getParamType(0)->isPointerTy() ||
1539 !FTy->getParamType(1)->isPointerTy())
1540 return false;
1541 setDoesNotThrow(F);
1542 setDoesNotCapture(F, 1);
1543 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001544 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001545 break;
1546 case LibFunc::lchown:
1547 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1548 return false;
1549 setDoesNotThrow(F);
1550 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001551 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001552 break;
1553 case LibFunc::qsort:
1554 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1555 return false;
1556 // May throw; places call through function pointer.
1557 setDoesNotCapture(F, 4);
1558 break;
1559 case LibFunc::dunder_strdup:
1560 case LibFunc::dunder_strndup:
1561 if (FTy->getNumParams() < 1 ||
1562 !FTy->getReturnType()->isPointerTy() ||
1563 !FTy->getParamType(0)->isPointerTy())
1564 return false;
1565 setDoesNotThrow(F);
1566 setDoesNotAlias(F, 0);
1567 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001568 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001569 break;
1570 case LibFunc::dunder_strtok_r:
1571 if (FTy->getNumParams() != 3 ||
1572 !FTy->getParamType(1)->isPointerTy())
1573 return false;
1574 setDoesNotThrow(F);
1575 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001576 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001577 break;
1578 case LibFunc::under_IO_getc:
1579 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1580 return false;
1581 setDoesNotThrow(F);
1582 setDoesNotCapture(F, 1);
1583 break;
1584 case LibFunc::under_IO_putc:
1585 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1586 return false;
1587 setDoesNotThrow(F);
1588 setDoesNotCapture(F, 2);
1589 break;
1590 case LibFunc::dunder_isoc99_scanf:
1591 if (FTy->getNumParams() < 1 ||
1592 !FTy->getParamType(0)->isPointerTy())
1593 return false;
1594 setDoesNotThrow(F);
1595 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001596 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001597 break;
1598 case LibFunc::stat64:
1599 case LibFunc::lstat64:
1600 case LibFunc::statvfs64:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001601 if (FTy->getNumParams() < 1 ||
1602 !FTy->getParamType(0)->isPointerTy() ||
1603 !FTy->getParamType(1)->isPointerTy())
1604 return false;
1605 setDoesNotThrow(F);
1606 setDoesNotCapture(F, 1);
1607 setDoesNotCapture(F, 2);
1608 setOnlyReadsMemory(F, 1);
1609 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001610 case LibFunc::dunder_isoc99_sscanf:
1611 if (FTy->getNumParams() < 1 ||
1612 !FTy->getParamType(0)->isPointerTy() ||
1613 !FTy->getParamType(1)->isPointerTy())
1614 return false;
1615 setDoesNotThrow(F);
1616 setDoesNotCapture(F, 1);
1617 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001618 setOnlyReadsMemory(F, 1);
1619 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001620 break;
1621 case LibFunc::fopen64:
1622 if (FTy->getNumParams() != 2 ||
1623 !FTy->getReturnType()->isPointerTy() ||
1624 !FTy->getParamType(0)->isPointerTy() ||
1625 !FTy->getParamType(1)->isPointerTy())
1626 return false;
1627 setDoesNotThrow(F);
1628 setDoesNotAlias(F, 0);
1629 setDoesNotCapture(F, 1);
1630 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001631 setOnlyReadsMemory(F, 1);
1632 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001633 break;
1634 case LibFunc::fseeko64:
1635 case LibFunc::ftello64:
1636 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1637 return false;
1638 setDoesNotThrow(F);
1639 setDoesNotCapture(F, 1);
1640 break;
1641 case LibFunc::tmpfile64:
1642 if (!FTy->getReturnType()->isPointerTy())
1643 return false;
1644 setDoesNotThrow(F);
1645 setDoesNotAlias(F, 0);
1646 break;
1647 case LibFunc::fstat64:
1648 case LibFunc::fstatvfs64:
1649 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1650 return false;
1651 setDoesNotThrow(F);
1652 setDoesNotCapture(F, 2);
1653 break;
1654 case LibFunc::open64:
1655 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1656 return false;
1657 // May throw; "open" is a valid pthread cancellation point.
1658 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001659 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001660 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001661 case LibFunc::gettimeofday:
1662 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1663 !FTy->getParamType(1)->isPointerTy())
1664 return false;
1665 // Currently some platforms have the restrict keyword on the arguments to
1666 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1667 // arguments.
1668 setDoesNotThrow(F);
1669 setDoesNotCapture(F, 1);
1670 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001671 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001672 default:
1673 // Didn't mark any attributes.
1674 return false;
1675 }
1676
1677 return true;
1678}
1679
1680/// annotateLibraryCalls - Adds attributes to well-known standard library
1681/// call declarations.
1682bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1683 bool MadeChange = false;
1684
1685 // Check each function in turn annotating well-known library function
1686 // declarations with attributes.
1687 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1688 Function *F = (*I)->getFunction();
1689
Craig Topperf40110f2014-04-25 05:29:35 +00001690 if (F && F->isDeclaration())
Meador Inge6b6a1612013-03-21 00:55:59 +00001691 MadeChange |= inferPrototypeAttributes(*F);
1692 }
1693
1694 return MadeChange;
1695}
1696
Chris Lattner4422d312010-04-16 22:42:17 +00001697bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman86449d72010-11-08 16:10:15 +00001698 AA = &getAnalysis<AliasAnalysis>();
Meador Inge6b6a1612013-03-21 00:55:59 +00001699 TLI = &getAnalysis<TargetLibraryInfo>();
Dan Gohman86449d72010-11-08 16:10:15 +00001700
Meador Inge6b6a1612013-03-21 00:55:59 +00001701 bool Changed = annotateLibraryCalls(SCC);
1702 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001703 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001704 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001705 return Changed;
1706}