blob: d1a4beec1f06d440d089be501229987bba143ee1 [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"
Philip Reamesa88caea2015-08-31 19:44:38 +000030#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000031#include "llvm/IR/GlobalVariable.h"
Chandler Carruth83948572014-03-04 10:30:26 +000032#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000033#include "llvm/IR/IntrinsicInst.h"
34#include "llvm/IR/LLVMContext.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000035#include "llvm/Support/Debug.h"
Hans Wennborg043bf5b2015-08-31 21:19:18 +000036#include "llvm/Support/raw_ostream.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000037#include "llvm/Analysis/TargetLibraryInfo.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000038using namespace llvm;
39
Chandler Carruth964daaa2014-04-22 02:55:47 +000040#define DEBUG_TYPE "functionattrs"
41
Duncan Sands44c8cd92008-12-31 16:14:43 +000042STATISTIC(NumReadNone, "Number of functions marked readnone");
43STATISTIC(NumReadOnly, "Number of functions marked readonly");
44STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000045STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
46STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000047STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Philip Reamesa88caea2015-08-31 19:44:38 +000048STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
Meador Inge6b6a1612013-03-21 00:55:59 +000049STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Duncan Sands44c8cd92008-12-31 16:14:43 +000050
51namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000052 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands44c8cd92008-12-31 16:14:43 +000053 static char ID; // Pass identification, replacement for typeid
Craig Topperf40110f2014-04-25 05:29:35 +000054 FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000055 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
56 }
Duncan Sands44c8cd92008-12-31 16:14:43 +000057
58 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Craig Topper3e4c6972014-03-05 09:10:37 +000059 bool runOnSCC(CallGraphSCC &SCC) override;
Duncan Sands44c8cd92008-12-31 16:14:43 +000060
61 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000062 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000063
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000064 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
65 bool AddArgumentAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000066
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000067 // IsFunctionMallocLike - Does this function allocate new memory?
68 bool IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +000069 SmallPtrSet<Function*, 8> &) const;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000070
71 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000072 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000073
Philip Reamesa88caea2015-08-31 19:44:38 +000074 /// \brief Does this function return null?
75 bool ReturnsNonNull(Function *F, SmallPtrSet<Function*, 8> &,
76 bool &Speculative) const;
77
78 /// \brief Deduce nonnull attributes for the SCC.
79 bool AddNonNullAttrs(const CallGraphSCC &SCC);
80
Meador Inge6b6a1612013-03-21 00:55:59 +000081 // Utility methods used by inferPrototypeAttributes to add attributes
82 // and maintain annotation statistics.
83
84 void setDoesNotAccessMemory(Function &F) {
85 if (!F.doesNotAccessMemory()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000086 F.setDoesNotAccessMemory();
87 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000088 }
89 }
90
91 void setOnlyReadsMemory(Function &F) {
92 if (!F.onlyReadsMemory()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000093 F.setOnlyReadsMemory();
94 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000095 }
96 }
97
98 void setDoesNotThrow(Function &F) {
99 if (!F.doesNotThrow()) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000100 F.setDoesNotThrow();
101 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +0000102 }
103 }
104
105 void setDoesNotCapture(Function &F, unsigned n) {
106 if (!F.doesNotCapture(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000107 F.setDoesNotCapture(n);
108 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +0000109 }
110 }
111
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000112 void setOnlyReadsMemory(Function &F, unsigned n) {
113 if (!F.onlyReadsMemory(n)) {
114 F.setOnlyReadsMemory(n);
115 ++NumAnnotated;
116 }
117 }
118
Meador Inge6b6a1612013-03-21 00:55:59 +0000119 void setDoesNotAlias(Function &F, unsigned n) {
120 if (!F.doesNotAlias(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000121 F.setDoesNotAlias(n);
122 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +0000123 }
124 }
125
126 // inferPrototypeAttributes - Analyze the name and prototype of the
127 // given function and set any applicable attributes. Returns true
128 // if any attributes were set and false otherwise.
129 bool inferPrototypeAttributes(Function &F);
130
131 // annotateLibraryCalls - Adds attributes to well-known standard library
132 // call declarations.
133 bool annotateLibraryCalls(const CallGraphSCC &SCC);
134
Craig Topper3e4c6972014-03-05 09:10:37 +0000135 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000136 AU.setPreservesCFG();
Dan Gohman86449d72010-11-08 16:10:15 +0000137 AU.addRequired<AliasAnalysis>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000138 AU.addRequired<TargetLibraryInfoWrapperPass>();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000139 CallGraphSCCPass::getAnalysisUsage(AU);
140 }
141
Dan Gohman86449d72010-11-08 16:10:15 +0000142 private:
143 AliasAnalysis *AA;
Meador Inge6b6a1612013-03-21 00:55:59 +0000144 TargetLibraryInfo *TLI;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000145 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000146}
Duncan Sands44c8cd92008-12-31 16:14:43 +0000147
148char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +0000149INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
150 "Deduce function attributes", false, false)
Nick Lewycky2c880672013-09-05 08:19:58 +0000151INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000152INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000153INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +0000154INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000155 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000156
157Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
158
159
Duncan Sands44c8cd92008-12-31 16:14:43 +0000160/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000161bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000162 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000163
164 // Fill SCCNodes with the elements of the SCC. Used for quickly
165 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000166 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
167 SCCNodes.insert((*I)->getFunction());
Duncan Sands44c8cd92008-12-31 16:14:43 +0000168
169 // Check if any of the functions in the SCC read or write memory. If they
170 // write memory then they can't be marked readnone or readonly.
171 bool ReadsMemory = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000172 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
173 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000174
Chandler Carruth0fb99812014-08-13 10:49:33 +0000175 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
176 // External node or node we don't want to optimize - assume it may write
177 // memory and give up.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000178 return false;
179
Chandler Carruth194f59c2015-07-22 23:15:57 +0000180 FunctionModRefBehavior MRB = AA->getModRefBehavior(F);
181 if (MRB == FMRB_DoesNotAccessMemory)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000182 // Already perfect!
183 continue;
184
185 // Definitions with weak linkage may be overridden at linktime with
186 // something that writes memory, so treat them like declarations.
187 if (F->isDeclaration() || F->mayBeOverridden()) {
Dan Gohman35814e62010-11-09 20:13:27 +0000188 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000189 // May write memory. Just give up.
190 return false;
191
192 ReadsMemory = true;
193 continue;
194 }
195
196 // Scan the function body for instructions that may read or write memory.
197 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
198 Instruction *I = &*II;
199
200 // Some instructions can be ignored even if they read or write memory.
201 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif62f0aac2010-07-28 22:50:26 +0000202 CallSite CS(cast<Value>(I));
Dan Gohman86449d72010-11-08 16:10:15 +0000203 if (CS) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000204 // Ignore calls to functions in the same SCC.
Dan Gohman86449d72010-11-08 16:10:15 +0000205 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000206 continue;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000207 FunctionModRefBehavior MRB = AA->getModRefBehavior(CS);
Dan Gohman2694e142010-11-10 01:02:18 +0000208 // If the call doesn't access arbitrary memory, we may be able to
209 // figure out something.
Dan Gohman25775802010-11-10 17:34:04 +0000210 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
211 // If the call does access argument pointees, check each argument.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000212 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman2694e142010-11-10 01:02:18 +0000213 // Check whether all pointer arguments point to local memory, and
214 // ignore calls that only access local memory.
215 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
216 CI != CE; ++CI) {
217 Value *Arg = *CI;
218 if (Arg->getType()->isPointerTy()) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000219 AAMDNodes AAInfo;
220 I->getAAMetadata(AAInfo);
221
Chandler Carruthecbd1682015-06-17 07:21:38 +0000222 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
Dan Gohman2694e142010-11-10 01:02:18 +0000223 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000224 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000225 // Writes non-local memory. Give up.
226 return false;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000227 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000228 // Ok, it reads non-local memory.
229 ReadsMemory = true;
230 }
Dan Gohmande521552010-11-09 19:56:27 +0000231 }
232 }
Dan Gohmande521552010-11-09 19:56:27 +0000233 continue;
Dan Gohman86449d72010-11-08 16:10:15 +0000234 }
Dan Gohman2694e142010-11-10 01:02:18 +0000235 // The call could access any memory. If that includes writes, give up.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000236 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000237 return false;
238 // If it reads, note it.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000239 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000240 ReadsMemory = true;
241 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000242 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000243 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
244 if (!LI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000245 MemoryLocation Loc = MemoryLocation::get(LI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000246 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
247 continue;
248 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000249 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000250 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
251 if (!SI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000252 MemoryLocation Loc = MemoryLocation::get(SI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000253 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
254 continue;
255 }
Dan Gohmane3467a72010-11-09 20:17:38 +0000256 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
257 // Ignore vaargs on local memory.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000258 MemoryLocation Loc = MemoryLocation::get(VI);
Dan Gohmane3467a72010-11-09 20:17:38 +0000259 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
260 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000261 }
262
263 // Any remaining instructions need to be taken seriously! Check if they
264 // read or write memory.
265 if (I->mayWriteToMemory())
266 // Writes memory. Just give up.
267 return false;
Duncan Sands9759f2e2009-05-06 08:42:00 +0000268
Duncan Sands44c8cd92008-12-31 16:14:43 +0000269 // If this instruction may read memory, remember that.
270 ReadsMemory |= I->mayReadFromMemory();
271 }
272 }
273
274 // Success! Functions in this SCC do not access memory, or only read memory.
275 // Give them the appropriate attribute.
276 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000277 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
278 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000279
280 if (F->doesNotAccessMemory())
281 // Already perfect!
282 continue;
283
284 if (F->onlyReadsMemory() && ReadsMemory)
285 // No change.
286 continue;
287
288 MadeChange = true;
289
290 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000291 AttrBuilder B;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000292 B.addAttribute(Attribute::ReadOnly)
293 .addAttribute(Attribute::ReadNone);
Bill Wendling430fa9b2013-01-23 00:45:55 +0000294 F->removeAttributes(AttributeSet::FunctionIndex,
295 AttributeSet::get(F->getContext(),
296 AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000297
298 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000299 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000300 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000301
302 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000303 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000304 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000305 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000306 }
307
308 return MadeChange;
309}
310
Nick Lewycky4c378a42011-12-28 23:24:21 +0000311namespace {
312 // For a given pointer Argument, this retains a list of Arguments of functions
313 // in the same SCC that the pointer data flows into. We use this to build an
314 // SCC of the arguments.
315 struct ArgumentGraphNode {
316 Argument *Definition;
317 SmallVector<ArgumentGraphNode*, 4> Uses;
318 };
319
320 class ArgumentGraph {
321 // We store pointers to ArgumentGraphNode objects, so it's important that
322 // that they not move around upon insert.
323 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
324
325 ArgumentMapTy ArgumentMap;
326
327 // There is no root node for the argument graph, in fact:
328 // void f(int *x, int *y) { if (...) f(x, y); }
329 // is an example where the graph is disconnected. The SCCIterator requires a
330 // single entry point, so we maintain a fake ("synthetic") root node that
331 // uses every node. Because the graph is directed and nothing points into
332 // the root, it will not participate in any SCCs (except for its own).
333 ArgumentGraphNode SyntheticRoot;
334
335 public:
Craig Topperf40110f2014-04-25 05:29:35 +0000336 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000337
338 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
339
340 iterator begin() { return SyntheticRoot.Uses.begin(); }
341 iterator end() { return SyntheticRoot.Uses.end(); }
342 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
343
344 ArgumentGraphNode *operator[](Argument *A) {
345 ArgumentGraphNode &Node = ArgumentMap[A];
346 Node.Definition = A;
347 SyntheticRoot.Uses.push_back(&Node);
348 return &Node;
349 }
350 };
351
352 // This tracker checks whether callees are in the SCC, and if so it does not
353 // consider that a capture, instead adding it to the "Uses" list and
354 // continuing with the analysis.
355 struct ArgumentUsesTracker : public CaptureTracker {
356 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
357 : Captured(false), SCCNodes(SCCNodes) {}
358
Craig Topper3e4c6972014-03-05 09:10:37 +0000359 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000360
Chandler Carruth64e9aa52014-03-05 10:21:48 +0000361 bool captured(const Use *U) override {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000362 CallSite CS(U->getUser());
363 if (!CS.getInstruction()) { Captured = true; return true; }
364
365 Function *F = CS.getCalledFunction();
366 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
367
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000368 bool Found = false;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000369 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
370 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
371 PI != PE; ++PI, ++AI) {
372 if (AI == AE) {
373 assert(F->isVarArg() && "More params than args in non-varargs call");
374 Captured = true;
375 return true;
376 }
377 if (PI == U) {
378 Uses.push_back(AI);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000379 Found = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000380 break;
381 }
382 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000383 assert(Found && "Capturing call-site captured nothing?");
Duncan Sandsc9e95ad2013-09-13 08:16:06 +0000384 (void)Found;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000385 return false;
386 }
387
388 bool Captured; // True only if certainly captured (used outside our SCC).
389 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
390
391 const SmallPtrSet<Function*, 8> &SCCNodes;
392 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000393}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000394
395namespace llvm {
396 template<> struct GraphTraits<ArgumentGraphNode*> {
397 typedef ArgumentGraphNode NodeType;
398 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
399
400 static inline NodeType *getEntryNode(NodeType *A) { return A; }
401 static inline ChildIteratorType child_begin(NodeType *N) {
402 return N->Uses.begin();
403 }
404 static inline ChildIteratorType child_end(NodeType *N) {
405 return N->Uses.end();
406 }
407 };
408 template<> struct GraphTraits<ArgumentGraph*>
409 : public GraphTraits<ArgumentGraphNode*> {
410 static NodeType *getEntryNode(ArgumentGraph *AG) {
411 return AG->getEntryNode();
412 }
413 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
414 return AG->begin();
415 }
416 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
417 return AG->end();
418 }
419 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000420}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000421
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000422// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
423static Attribute::AttrKind
424determinePointerReadAttrs(Argument *A,
425 const SmallPtrSet<Argument*, 8> &SCCNodes) {
426
427 SmallVector<Use*, 32> Worklist;
428 SmallSet<Use*, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000429
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000430 // inalloca arguments are always clobbered by the call.
431 if (A->hasInAllocaAttr())
432 return Attribute::None;
433
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000434 bool IsRead = false;
435 // We don't need to track IsWritten. If A is written to, return immediately.
436
Chandler Carruthcdf47882014-03-09 03:16:01 +0000437 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000438 Visited.insert(&U);
439 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000440 }
441
442 while (!Worklist.empty()) {
443 Use *U = Worklist.pop_back_val();
444 Instruction *I = cast<Instruction>(U->getUser());
445 Value *V = U->get();
446
447 switch (I->getOpcode()) {
448 case Instruction::BitCast:
449 case Instruction::GetElementPtr:
450 case Instruction::PHI:
451 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000452 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000453 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000454 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000455 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000456 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000457 break;
458
459 case Instruction::Call:
460 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000461 bool Captures = true;
462
463 if (I->getType()->isVoidTy())
464 Captures = false;
465
466 auto AddUsersToWorklistIfCapturing = [&] {
467 if (Captures)
468 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000469 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000470 Worklist.push_back(&UU);
471 };
472
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000473 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000474 if (CS.doesNotAccessMemory()) {
475 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000476 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000477 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000478
479 Function *F = CS.getCalledFunction();
480 if (!F) {
481 if (CS.onlyReadsMemory()) {
482 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000483 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000484 continue;
485 }
486 return Attribute::None;
487 }
488
489 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
490 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
491 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
492 if (A->get() == V) {
493 if (AI == AE) {
494 assert(F->isVarArg() &&
495 "More params than args in non-varargs call.");
496 return Attribute::None;
497 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000498 Captures &= !CS.doesNotCapture(A - B);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000499 if (SCCNodes.count(AI))
500 continue;
501 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
502 return Attribute::None;
503 if (!CS.doesNotAccessMemory(A - B))
504 IsRead = true;
505 }
506 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000507 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000508 break;
509 }
510
511 case Instruction::Load:
512 IsRead = true;
513 break;
514
515 case Instruction::ICmp:
516 case Instruction::Ret:
517 break;
518
519 default:
520 return Attribute::None;
521 }
522 }
523
524 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
525}
526
527/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
528bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000529 bool Changed = false;
530
Nick Lewycky4c378a42011-12-28 23:24:21 +0000531 SmallPtrSet<Function*, 8> SCCNodes;
532
533 // Fill SCCNodes with the elements of the SCC. Used for quickly
534 // looking up whether a given CallGraphNode is in this SCC.
535 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
536 Function *F = (*I)->getFunction();
Chandler Carruth0fb99812014-08-13 10:49:33 +0000537 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
538 !F->hasFnAttribute(Attribute::OptimizeNone))
Nick Lewycky4c378a42011-12-28 23:24:21 +0000539 SCCNodes.insert(F);
540 }
541
542 ArgumentGraph AG;
543
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000544 AttrBuilder B;
545 B.addAttribute(Attribute::NoCapture);
546
Duncan Sands44c8cd92008-12-31 16:14:43 +0000547 // Check each function in turn, determining which pointer arguments are not
548 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000549 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
550 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000551
Chandler Carruth0fb99812014-08-13 10:49:33 +0000552 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
553 // External node or function we're trying not to optimize - only a problem
554 // for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000555 continue;
556
557 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000558 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000559 if (F->isDeclaration() || F->mayBeOverridden())
560 continue;
561
Nick Lewycky4c378a42011-12-28 23:24:21 +0000562 // Functions that are readonly (or readnone) and nounwind and don't return
563 // a value can't capture arguments. Don't analyze them.
564 if (F->onlyReadsMemory() && F->doesNotThrow() &&
565 F->getReturnType()->isVoidTy()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000566 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
567 A != E; ++A) {
568 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
569 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
570 ++NumNoCapture;
571 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000572 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000573 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000574 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000575 }
576
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000577 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
578 A != E; ++A) {
579 if (!A->getType()->isPointerTy()) continue;
580 bool HasNonLocalUses = false;
581 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000582 ArgumentUsesTracker Tracker(SCCNodes);
583 PointerMayBeCaptured(A, &Tracker);
584 if (!Tracker.Captured) {
585 if (Tracker.Uses.empty()) {
586 // If it's trivially not captured, mark it nocapture now.
587 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
588 ++NumNoCapture;
589 Changed = true;
590 } else {
591 // If it's not trivially captured and not trivially not captured,
592 // then it must be calling into another function in our SCC. Save
593 // its particulars for Argument-SCC analysis later.
594 ArgumentGraphNode *Node = AG[A];
595 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000596 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000597 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000598 if (*UI != A)
599 HasNonLocalUses = true;
600 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000601 }
602 }
603 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
604 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000605 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
606 // Can we determine that it's readonly/readnone without doing an SCC?
607 // Note that we don't allow any calls at all here, or else our result
608 // will be dependent on the iteration order through the functions in the
609 // SCC.
610 SmallPtrSet<Argument*, 8> Self;
611 Self.insert(A);
612 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
613 if (R != Attribute::None) {
614 AttrBuilder B;
615 B.addAttribute(R);
616 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
617 Changed = true;
618 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
619 }
620 }
621 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000622 }
623
624 // The graph we've collected is partial because we stopped scanning for
625 // argument uses once we solved the argument trivially. These partial nodes
626 // show up as ArgumentGraphNode objects with an empty Uses list, and for
627 // these nodes the final decision about whether they capture has already been
628 // made. If the definition doesn't have a 'nocapture' attribute by now, it
629 // captures.
630
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000631 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000632 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000633 if (ArgumentSCC.size() == 1) {
634 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
635
636 // eg. "void f(int* x) { if (...) f(x); }"
637 if (ArgumentSCC[0]->Uses.size() == 1 &&
638 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000639 Argument *A = ArgumentSCC[0]->Definition;
640 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000641 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000642 Changed = true;
643 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000644 continue;
645 }
646
647 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000648 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
649 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000650 ArgumentGraphNode *Node = *I;
651 if (Node->Uses.empty()) {
652 if (!Node->Definition->hasNoCaptureAttr())
653 SCCCaptured = true;
654 }
655 }
656 if (SCCCaptured) continue;
657
658 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
659 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
660 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000661 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000662 ArgumentSCCNodes.insert((*I)->Definition);
663 }
664
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000665 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
666 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000667 ArgumentGraphNode *N = *I;
668 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
669 UE = N->Uses.end(); UI != UE; ++UI) {
670 Argument *A = (*UI)->Definition;
671 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
672 continue;
673 SCCCaptured = true;
674 break;
675 }
676 }
677 if (SCCCaptured) continue;
678
Nick Lewyckyf740db32012-01-05 22:21:45 +0000679 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000680 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000681 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000682 ++NumNoCapture;
683 Changed = true;
684 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000685
686 // We also want to compute readonly/readnone. With a small number of false
687 // negatives, we can assume that any pointer which is captured isn't going
688 // to be provably readonly or readnone, since by definition we can't
689 // analyze all uses of a captured pointer.
690 //
691 // The false negatives happen when the pointer is captured by a function
692 // that promises readonly/readnone behaviour on the pointer, then the
693 // pointer's lifetime ends before anything that writes to arbitrary memory.
694 // Also, a readonly/readnone pointer may be returned, but returning a
695 // pointer is capturing it.
696
697 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
698 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
699 Argument *A = ArgumentSCC[i]->Definition;
700 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
701 if (K == Attribute::ReadNone)
702 continue;
703 if (K == Attribute::ReadOnly) {
704 ReadAttr = Attribute::ReadOnly;
705 continue;
706 }
707 ReadAttr = K;
708 break;
709 }
710
711 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000712 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000713 B.addAttribute(ReadAttr);
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000714 R.addAttribute(Attribute::ReadOnly)
715 .addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000716 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
717 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000718 // Clear out existing readonly/readnone attributes
719 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000720 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
721 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
722 Changed = true;
723 }
724 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000725 }
726
727 return Changed;
728}
729
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000730/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000731/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000732bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +0000733 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramer15591272012-10-31 13:45:49 +0000734 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000735 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
736 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
737 FlowsToReturn.insert(Ret->getReturnValue());
738
739 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000740 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000741
742 if (Constant *C = dyn_cast<Constant>(RetVal)) {
743 if (!C->isNullValue() && !isa<UndefValue>(C))
744 return false;
745
746 continue;
747 }
748
749 if (isa<Argument>(RetVal))
750 return false;
751
752 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
753 switch (RVI->getOpcode()) {
754 // Extend the analysis by looking upwards.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000755 case Instruction::BitCast:
Victor Hernandez5d034492009-09-18 22:35:49 +0000756 case Instruction::GetElementPtr:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000757 case Instruction::AddrSpaceCast:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000758 FlowsToReturn.insert(RVI->getOperand(0));
759 continue;
760 case Instruction::Select: {
761 SelectInst *SI = cast<SelectInst>(RVI);
762 FlowsToReturn.insert(SI->getTrueValue());
763 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner43d0db72009-09-27 21:29:28 +0000764 continue;
765 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000766 case Instruction::PHI: {
767 PHINode *PN = cast<PHINode>(RVI);
Pete Cooper833f34d2015-05-12 20:05:31 +0000768 for (Value *IncValue : PN->incoming_values())
769 FlowsToReturn.insert(IncValue);
Chris Lattner43d0db72009-09-27 21:29:28 +0000770 continue;
771 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000772
773 // Check whether the pointer came from an allocation.
774 case Instruction::Alloca:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000775 break;
776 case Instruction::Call:
777 case Instruction::Invoke: {
778 CallSite CS(RVI);
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000779 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000780 break;
781 if (CS.getCalledFunction() &&
Chris Lattner2f2110a2009-08-31 04:09:04 +0000782 SCCNodes.count(CS.getCalledFunction()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000783 break;
784 } // fall-through
785 default:
786 return false; // Did not come from an allocation.
787 }
788
Dan Gohman94e61762009-11-19 21:57:48 +0000789 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000790 return false;
791 }
792
793 return true;
794}
795
796/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000797bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000798 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000799
800 // Fill SCCNodes with the elements of the SCC. Used for quickly
801 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000802 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
803 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000804
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000805 // Check each function in turn, determining which functions return noalias
806 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000807 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
808 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000809
Chandler Carruth0fb99812014-08-13 10:49:33 +0000810 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
811 // External node or node we don't want to optimize - skip it;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000812 return false;
813
814 // Already noalias.
815 if (F->doesNotAlias(0))
816 continue;
817
818 // Definitions with weak linkage may be overridden at linktime, so
819 // treat them like declarations.
820 if (F->isDeclaration() || F->mayBeOverridden())
821 return false;
822
823 // We annotate noalias return values, which are only applicable to
824 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000825 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000826 continue;
827
828 if (!IsFunctionMallocLike(F, SCCNodes))
829 return false;
830 }
831
832 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000833 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
834 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000835 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000836 continue;
837
838 F->setDoesNotAlias(0);
839 ++NumNoAlias;
840 MadeChange = true;
841 }
842
843 return MadeChange;
844}
845
Philip Reamesa88caea2015-08-31 19:44:38 +0000846bool FunctionAttrs::ReturnsNonNull(Function *F,
847 SmallPtrSet<Function*, 8> &SCCNodes,
848 bool &Speculative) const {
849 assert(F->getReturnType()->isPointerTy() &&
850 "nonnull only meaningful on pointer types");
851 Speculative = false;
852
853 SmallSetVector<Value *, 8> FlowsToReturn;
854 for (BasicBlock &BB : *F)
855 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
856 FlowsToReturn.insert(Ret->getReturnValue());
857
858 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
859 Value *RetVal = FlowsToReturn[i];
860
861 // If this value is locally known to be non-null, we're good
862 if (isKnownNonNull(RetVal, TLI))
863 continue;
864
865 // Otherwise, we need to look upwards since we can't make any local
866 // conclusions.
867 Instruction *RVI = dyn_cast<Instruction>(RetVal);
868 if (!RVI)
869 return false;
870 switch (RVI->getOpcode()) {
871 // Extend the analysis by looking upwards.
872 case Instruction::BitCast:
873 case Instruction::GetElementPtr:
874 case Instruction::AddrSpaceCast:
875 FlowsToReturn.insert(RVI->getOperand(0));
876 continue;
877 case Instruction::Select: {
878 SelectInst *SI = cast<SelectInst>(RVI);
879 FlowsToReturn.insert(SI->getTrueValue());
880 FlowsToReturn.insert(SI->getFalseValue());
881 continue;
882 }
883 case Instruction::PHI: {
884 PHINode *PN = cast<PHINode>(RVI);
885 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
886 FlowsToReturn.insert(PN->getIncomingValue(i));
887 continue;
888 }
889 case Instruction::Call:
890 case Instruction::Invoke: {
891 CallSite CS(RVI);
892 Function *Callee = CS.getCalledFunction();
893 // A call to a node within the SCC is assumed to return null until
894 // proven otherwise
895 if (Callee && SCCNodes.count(Callee)) {
896 Speculative = true;
897 continue;
898 }
899 return false;
900 }
901 default:
902 return false; // Unknown source, may be null
903 };
904 llvm_unreachable("should have either continued or returned");
905 }
906
907 return true;
908}
909
910bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
911 SmallPtrSet<Function*, 8> SCCNodes;
912
913 // Fill SCCNodes with the elements of the SCC. Used for quickly
914 // looking up whether a given CallGraphNode is in this SCC.
915 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
916 SCCNodes.insert((*I)->getFunction());
917
918 // Speculative that all functions in the SCC return only nonnull
919 // pointers. We may refute this as we analyze functions.
920 bool SCCReturnsNonNull = true;
921
922 bool MadeChange = false;
923
924 // Check each function in turn, determining which functions return nonnull
925 // pointers.
926 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
927 Function *F = (*I)->getFunction();
928
929 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
930 // External node or node we don't want to optimize - skip it;
931 return false;
932
933 // Already nonnull.
934 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
935 Attribute::NonNull))
936 continue;
937
938 // Definitions with weak linkage may be overridden at linktime, so
939 // treat them like declarations.
940 if (F->isDeclaration() || F->mayBeOverridden())
941 return false;
942
943 // We annotate nonnull return values, which are only applicable to
944 // pointer types.
945 if (!F->getReturnType()->isPointerTy())
946 continue;
947
948 bool Speculative = false;
949 if (ReturnsNonNull(F, SCCNodes, Speculative)) {
950 if (!Speculative) {
951 // Mark the function eagerly since we may discover a function
952 // which prevents us from speculating about the entire SCC
953 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
954 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
955 ++NumNonNullReturn;
956 MadeChange = true;
957 }
958 continue;
959 }
960 // At least one function returns something which could be null, can't
961 // speculate any more.
962 SCCReturnsNonNull = false;
963 }
964
965 if (SCCReturnsNonNull) {
966 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
967 Function *F = (*I)->getFunction();
968 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
969 Attribute::NonNull) ||
970 !F->getReturnType()->isPointerTy())
971 continue;
972
973 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
974 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
975 ++NumNonNullReturn;
976 MadeChange = true;
977 }
978 }
979
980 return MadeChange;
981}
982
Meador Inge6b6a1612013-03-21 00:55:59 +0000983/// inferPrototypeAttributes - Analyze the name and prototype of the
984/// given function and set any applicable attributes. Returns true
985/// if any attributes were set and false otherwise.
986bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
Chandler Carruth0fb99812014-08-13 10:49:33 +0000987 if (F.hasFnAttribute(Attribute::OptimizeNone))
988 return false;
989
Meador Inge6b6a1612013-03-21 00:55:59 +0000990 FunctionType *FTy = F.getFunctionType();
991 LibFunc::Func TheLibFunc;
992 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
993 return false;
994
995 switch (TheLibFunc) {
996 case LibFunc::strlen:
997 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
998 return false;
999 setOnlyReadsMemory(F);
1000 setDoesNotThrow(F);
1001 setDoesNotCapture(F, 1);
1002 break;
1003 case LibFunc::strchr:
1004 case LibFunc::strrchr:
1005 if (FTy->getNumParams() != 2 ||
1006 !FTy->getParamType(0)->isPointerTy() ||
1007 !FTy->getParamType(1)->isIntegerTy())
1008 return false;
1009 setOnlyReadsMemory(F);
1010 setDoesNotThrow(F);
1011 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001012 case LibFunc::strtol:
1013 case LibFunc::strtod:
1014 case LibFunc::strtof:
1015 case LibFunc::strtoul:
1016 case LibFunc::strtoll:
1017 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +00001018 case LibFunc::strtoull:
1019 if (FTy->getNumParams() < 2 ||
1020 !FTy->getParamType(1)->isPointerTy())
1021 return false;
1022 setDoesNotThrow(F);
1023 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001024 setOnlyReadsMemory(F, 1);
1025 break;
1026 case LibFunc::strcpy:
1027 case LibFunc::stpcpy:
1028 case LibFunc::strcat:
1029 case LibFunc::strncat:
1030 case LibFunc::strncpy:
1031 case LibFunc::stpncpy:
1032 if (FTy->getNumParams() < 2 ||
1033 !FTy->getParamType(1)->isPointerTy())
1034 return false;
1035 setDoesNotThrow(F);
1036 setDoesNotCapture(F, 2);
1037 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001038 break;
1039 case LibFunc::strxfrm:
1040 if (FTy->getNumParams() != 3 ||
1041 !FTy->getParamType(0)->isPointerTy() ||
1042 !FTy->getParamType(1)->isPointerTy())
1043 return false;
1044 setDoesNotThrow(F);
1045 setDoesNotCapture(F, 1);
1046 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001047 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001048 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001049 case LibFunc::strcmp: //0,1
1050 case LibFunc::strspn: // 0,1
1051 case LibFunc::strncmp: // 0,1
1052 case LibFunc::strcspn: //0,1
1053 case LibFunc::strcoll: //0,1
1054 case LibFunc::strcasecmp: // 0,1
1055 case LibFunc::strncasecmp: //
Meador Inge6b6a1612013-03-21 00:55:59 +00001056 if (FTy->getNumParams() < 2 ||
1057 !FTy->getParamType(0)->isPointerTy() ||
1058 !FTy->getParamType(1)->isPointerTy())
1059 return false;
1060 setOnlyReadsMemory(F);
1061 setDoesNotThrow(F);
1062 setDoesNotCapture(F, 1);
1063 setDoesNotCapture(F, 2);
1064 break;
1065 case LibFunc::strstr:
1066 case LibFunc::strpbrk:
1067 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1068 return false;
1069 setOnlyReadsMemory(F);
1070 setDoesNotThrow(F);
1071 setDoesNotCapture(F, 2);
1072 break;
1073 case LibFunc::strtok:
1074 case LibFunc::strtok_r:
1075 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1076 return false;
1077 setDoesNotThrow(F);
1078 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001079 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001080 break;
1081 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001082 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1083 return false;
1084 setDoesNotThrow(F);
1085 setDoesNotCapture(F, 1);
1086 setOnlyReadsMemory(F, 1);
1087 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001088 case LibFunc::setbuf:
1089 case LibFunc::setvbuf:
1090 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1091 return false;
1092 setDoesNotThrow(F);
1093 setDoesNotCapture(F, 1);
1094 break;
1095 case LibFunc::strdup:
1096 case LibFunc::strndup:
1097 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1098 !FTy->getParamType(0)->isPointerTy())
1099 return false;
1100 setDoesNotThrow(F);
1101 setDoesNotAlias(F, 0);
1102 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001103 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001104 break;
1105 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +00001106 case LibFunc::statvfs:
1107 if (FTy->getNumParams() < 2 ||
1108 !FTy->getParamType(0)->isPointerTy() ||
1109 !FTy->getParamType(1)->isPointerTy())
1110 return false;
1111 setDoesNotThrow(F);
1112 setDoesNotCapture(F, 1);
1113 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001114 setOnlyReadsMemory(F, 1);
1115 break;
1116 case LibFunc::sscanf:
1117 if (FTy->getNumParams() < 2 ||
1118 !FTy->getParamType(0)->isPointerTy() ||
1119 !FTy->getParamType(1)->isPointerTy())
1120 return false;
1121 setDoesNotThrow(F);
1122 setDoesNotCapture(F, 1);
1123 setDoesNotCapture(F, 2);
1124 setOnlyReadsMemory(F, 1);
1125 setOnlyReadsMemory(F, 2);
1126 break;
1127 case LibFunc::sprintf:
1128 if (FTy->getNumParams() < 2 ||
1129 !FTy->getParamType(0)->isPointerTy() ||
1130 !FTy->getParamType(1)->isPointerTy())
1131 return false;
1132 setDoesNotThrow(F);
1133 setDoesNotCapture(F, 1);
1134 setDoesNotCapture(F, 2);
1135 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001136 break;
1137 case LibFunc::snprintf:
1138 if (FTy->getNumParams() != 3 ||
1139 !FTy->getParamType(0)->isPointerTy() ||
1140 !FTy->getParamType(2)->isPointerTy())
1141 return false;
1142 setDoesNotThrow(F);
1143 setDoesNotCapture(F, 1);
1144 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001145 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001146 break;
1147 case LibFunc::setitimer:
1148 if (FTy->getNumParams() != 3 ||
1149 !FTy->getParamType(1)->isPointerTy() ||
1150 !FTy->getParamType(2)->isPointerTy())
1151 return false;
1152 setDoesNotThrow(F);
1153 setDoesNotCapture(F, 2);
1154 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001155 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001156 break;
1157 case LibFunc::system:
1158 if (FTy->getNumParams() != 1 ||
1159 !FTy->getParamType(0)->isPointerTy())
1160 return false;
1161 // May throw; "system" is a valid pthread cancellation point.
1162 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001163 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001164 break;
1165 case LibFunc::malloc:
1166 if (FTy->getNumParams() != 1 ||
1167 !FTy->getReturnType()->isPointerTy())
1168 return false;
1169 setDoesNotThrow(F);
1170 setDoesNotAlias(F, 0);
1171 break;
1172 case LibFunc::memcmp:
1173 if (FTy->getNumParams() != 3 ||
1174 !FTy->getParamType(0)->isPointerTy() ||
1175 !FTy->getParamType(1)->isPointerTy())
1176 return false;
1177 setOnlyReadsMemory(F);
1178 setDoesNotThrow(F);
1179 setDoesNotCapture(F, 1);
1180 setDoesNotCapture(F, 2);
1181 break;
1182 case LibFunc::memchr:
1183 case LibFunc::memrchr:
1184 if (FTy->getNumParams() != 3)
1185 return false;
1186 setOnlyReadsMemory(F);
1187 setDoesNotThrow(F);
1188 break;
1189 case LibFunc::modf:
1190 case LibFunc::modff:
1191 case LibFunc::modfl:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001192 if (FTy->getNumParams() < 2 ||
1193 !FTy->getParamType(1)->isPointerTy())
1194 return false;
1195 setDoesNotThrow(F);
1196 setDoesNotCapture(F, 2);
1197 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001198 case LibFunc::memcpy:
1199 case LibFunc::memccpy:
1200 case LibFunc::memmove:
1201 if (FTy->getNumParams() < 2 ||
1202 !FTy->getParamType(1)->isPointerTy())
1203 return false;
1204 setDoesNotThrow(F);
1205 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001206 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001207 break;
1208 case LibFunc::memalign:
1209 if (!FTy->getReturnType()->isPointerTy())
1210 return false;
1211 setDoesNotAlias(F, 0);
1212 break;
1213 case LibFunc::mkdir:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001214 if (FTy->getNumParams() == 0 ||
1215 !FTy->getParamType(0)->isPointerTy())
1216 return false;
1217 setDoesNotThrow(F);
1218 setDoesNotCapture(F, 1);
1219 setOnlyReadsMemory(F, 1);
1220 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001221 case LibFunc::mktime:
1222 if (FTy->getNumParams() == 0 ||
1223 !FTy->getParamType(0)->isPointerTy())
1224 return false;
1225 setDoesNotThrow(F);
1226 setDoesNotCapture(F, 1);
1227 break;
1228 case LibFunc::realloc:
1229 if (FTy->getNumParams() != 2 ||
1230 !FTy->getParamType(0)->isPointerTy() ||
1231 !FTy->getReturnType()->isPointerTy())
1232 return false;
1233 setDoesNotThrow(F);
1234 setDoesNotAlias(F, 0);
1235 setDoesNotCapture(F, 1);
1236 break;
1237 case LibFunc::read:
1238 if (FTy->getNumParams() != 3 ||
1239 !FTy->getParamType(1)->isPointerTy())
1240 return false;
1241 // May throw; "read" is a valid pthread cancellation point.
1242 setDoesNotCapture(F, 2);
1243 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001244 case LibFunc::rewind:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001245 if (FTy->getNumParams() < 1 ||
1246 !FTy->getParamType(0)->isPointerTy())
1247 return false;
1248 setDoesNotThrow(F);
1249 setDoesNotCapture(F, 1);
1250 break;
1251 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001252 case LibFunc::remove:
1253 case LibFunc::realpath:
1254 if (FTy->getNumParams() < 1 ||
1255 !FTy->getParamType(0)->isPointerTy())
1256 return false;
1257 setDoesNotThrow(F);
1258 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001259 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001260 break;
1261 case LibFunc::rename:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001262 if (FTy->getNumParams() < 2 ||
1263 !FTy->getParamType(0)->isPointerTy() ||
1264 !FTy->getParamType(1)->isPointerTy())
1265 return false;
1266 setDoesNotThrow(F);
1267 setDoesNotCapture(F, 1);
1268 setDoesNotCapture(F, 2);
1269 setOnlyReadsMemory(F, 1);
1270 setOnlyReadsMemory(F, 2);
1271 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001272 case LibFunc::readlink:
1273 if (FTy->getNumParams() < 2 ||
1274 !FTy->getParamType(0)->isPointerTy() ||
1275 !FTy->getParamType(1)->isPointerTy())
1276 return false;
1277 setDoesNotThrow(F);
1278 setDoesNotCapture(F, 1);
1279 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001280 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001281 break;
1282 case LibFunc::write:
1283 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1284 return false;
1285 // May throw; "write" is a valid pthread cancellation point.
1286 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001287 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001288 break;
1289 case LibFunc::bcopy:
1290 if (FTy->getNumParams() != 3 ||
1291 !FTy->getParamType(0)->isPointerTy() ||
1292 !FTy->getParamType(1)->isPointerTy())
1293 return false;
1294 setDoesNotThrow(F);
1295 setDoesNotCapture(F, 1);
1296 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001297 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001298 break;
1299 case LibFunc::bcmp:
1300 if (FTy->getNumParams() != 3 ||
1301 !FTy->getParamType(0)->isPointerTy() ||
1302 !FTy->getParamType(1)->isPointerTy())
1303 return false;
1304 setDoesNotThrow(F);
1305 setOnlyReadsMemory(F);
1306 setDoesNotCapture(F, 1);
1307 setDoesNotCapture(F, 2);
1308 break;
1309 case LibFunc::bzero:
1310 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1311 return false;
1312 setDoesNotThrow(F);
1313 setDoesNotCapture(F, 1);
1314 break;
1315 case LibFunc::calloc:
1316 if (FTy->getNumParams() != 2 ||
1317 !FTy->getReturnType()->isPointerTy())
1318 return false;
1319 setDoesNotThrow(F);
1320 setDoesNotAlias(F, 0);
1321 break;
1322 case LibFunc::chmod:
1323 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001324 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1325 return false;
1326 setDoesNotThrow(F);
1327 setDoesNotCapture(F, 1);
1328 setOnlyReadsMemory(F, 1);
1329 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001330 case LibFunc::ctermid:
1331 case LibFunc::clearerr:
1332 case LibFunc::closedir:
1333 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1334 return false;
1335 setDoesNotThrow(F);
1336 setDoesNotCapture(F, 1);
1337 break;
1338 case LibFunc::atoi:
1339 case LibFunc::atol:
1340 case LibFunc::atof:
1341 case LibFunc::atoll:
1342 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1343 return false;
1344 setDoesNotThrow(F);
1345 setOnlyReadsMemory(F);
1346 setDoesNotCapture(F, 1);
1347 break;
1348 case LibFunc::access:
1349 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1350 return false;
1351 setDoesNotThrow(F);
1352 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001353 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001354 break;
1355 case LibFunc::fopen:
1356 if (FTy->getNumParams() != 2 ||
1357 !FTy->getReturnType()->isPointerTy() ||
1358 !FTy->getParamType(0)->isPointerTy() ||
1359 !FTy->getParamType(1)->isPointerTy())
1360 return false;
1361 setDoesNotThrow(F);
1362 setDoesNotAlias(F, 0);
1363 setDoesNotCapture(F, 1);
1364 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001365 setOnlyReadsMemory(F, 1);
1366 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001367 break;
1368 case LibFunc::fdopen:
1369 if (FTy->getNumParams() != 2 ||
1370 !FTy->getReturnType()->isPointerTy() ||
1371 !FTy->getParamType(1)->isPointerTy())
1372 return false;
1373 setDoesNotThrow(F);
1374 setDoesNotAlias(F, 0);
1375 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001376 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001377 break;
1378 case LibFunc::feof:
1379 case LibFunc::free:
1380 case LibFunc::fseek:
1381 case LibFunc::ftell:
1382 case LibFunc::fgetc:
1383 case LibFunc::fseeko:
1384 case LibFunc::ftello:
1385 case LibFunc::fileno:
1386 case LibFunc::fflush:
1387 case LibFunc::fclose:
1388 case LibFunc::fsetpos:
1389 case LibFunc::flockfile:
1390 case LibFunc::funlockfile:
1391 case LibFunc::ftrylockfile:
1392 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1393 return false;
1394 setDoesNotThrow(F);
1395 setDoesNotCapture(F, 1);
1396 break;
1397 case LibFunc::ferror:
1398 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1399 return false;
1400 setDoesNotThrow(F);
1401 setDoesNotCapture(F, 1);
1402 setOnlyReadsMemory(F);
1403 break;
1404 case LibFunc::fputc:
1405 case LibFunc::fstat:
1406 case LibFunc::frexp:
1407 case LibFunc::frexpf:
1408 case LibFunc::frexpl:
1409 case LibFunc::fstatvfs:
1410 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1411 return false;
1412 setDoesNotThrow(F);
1413 setDoesNotCapture(F, 2);
1414 break;
1415 case LibFunc::fgets:
1416 if (FTy->getNumParams() != 3 ||
1417 !FTy->getParamType(0)->isPointerTy() ||
1418 !FTy->getParamType(2)->isPointerTy())
1419 return false;
1420 setDoesNotThrow(F);
1421 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001422 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001423 case LibFunc::fread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001424 if (FTy->getNumParams() != 4 ||
1425 !FTy->getParamType(0)->isPointerTy() ||
1426 !FTy->getParamType(3)->isPointerTy())
1427 return false;
1428 setDoesNotThrow(F);
1429 setDoesNotCapture(F, 1);
1430 setDoesNotCapture(F, 4);
1431 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001432 case LibFunc::fwrite:
1433 if (FTy->getNumParams() != 4 ||
1434 !FTy->getParamType(0)->isPointerTy() ||
1435 !FTy->getParamType(3)->isPointerTy())
1436 return false;
1437 setDoesNotThrow(F);
1438 setDoesNotCapture(F, 1);
1439 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001440 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001441 case LibFunc::fputs:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001442 if (FTy->getNumParams() < 2 ||
1443 !FTy->getParamType(0)->isPointerTy() ||
1444 !FTy->getParamType(1)->isPointerTy())
1445 return false;
1446 setDoesNotThrow(F);
1447 setDoesNotCapture(F, 1);
1448 setDoesNotCapture(F, 2);
1449 setOnlyReadsMemory(F, 1);
1450 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001451 case LibFunc::fscanf:
1452 case LibFunc::fprintf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001453 if (FTy->getNumParams() < 2 ||
1454 !FTy->getParamType(0)->isPointerTy() ||
1455 !FTy->getParamType(1)->isPointerTy())
1456 return false;
1457 setDoesNotThrow(F);
1458 setDoesNotCapture(F, 1);
1459 setDoesNotCapture(F, 2);
1460 setOnlyReadsMemory(F, 2);
1461 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001462 case LibFunc::fgetpos:
1463 if (FTy->getNumParams() < 2 ||
1464 !FTy->getParamType(0)->isPointerTy() ||
1465 !FTy->getParamType(1)->isPointerTy())
1466 return false;
1467 setDoesNotThrow(F);
1468 setDoesNotCapture(F, 1);
1469 setDoesNotCapture(F, 2);
1470 break;
1471 case LibFunc::getc:
1472 case LibFunc::getlogin_r:
1473 case LibFunc::getc_unlocked:
1474 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1475 return false;
1476 setDoesNotThrow(F);
1477 setDoesNotCapture(F, 1);
1478 break;
1479 case LibFunc::getenv:
1480 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1481 return false;
1482 setDoesNotThrow(F);
1483 setOnlyReadsMemory(F);
1484 setDoesNotCapture(F, 1);
1485 break;
1486 case LibFunc::gets:
1487 case LibFunc::getchar:
1488 setDoesNotThrow(F);
1489 break;
1490 case LibFunc::getitimer:
1491 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1492 return false;
1493 setDoesNotThrow(F);
1494 setDoesNotCapture(F, 2);
1495 break;
1496 case LibFunc::getpwnam:
1497 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1498 return false;
1499 setDoesNotThrow(F);
1500 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001501 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001502 break;
1503 case LibFunc::ungetc:
1504 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1505 return false;
1506 setDoesNotThrow(F);
1507 setDoesNotCapture(F, 2);
1508 break;
1509 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001510 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1511 return false;
1512 setDoesNotThrow(F);
1513 setDoesNotCapture(F, 1);
1514 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001515 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001516 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1517 return false;
1518 setDoesNotThrow(F);
1519 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001520 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001521 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001522 case LibFunc::unsetenv:
1523 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1524 return false;
1525 setDoesNotThrow(F);
1526 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001527 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001528 break;
1529 case LibFunc::utime:
1530 case LibFunc::utimes:
1531 if (FTy->getNumParams() != 2 ||
1532 !FTy->getParamType(0)->isPointerTy() ||
1533 !FTy->getParamType(1)->isPointerTy())
1534 return false;
1535 setDoesNotThrow(F);
1536 setDoesNotCapture(F, 1);
1537 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001538 setOnlyReadsMemory(F, 1);
1539 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001540 break;
1541 case LibFunc::putc:
1542 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1543 return false;
1544 setDoesNotThrow(F);
1545 setDoesNotCapture(F, 2);
1546 break;
1547 case LibFunc::puts:
1548 case LibFunc::printf:
1549 case LibFunc::perror:
1550 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1551 return false;
1552 setDoesNotThrow(F);
1553 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001554 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001555 break;
1556 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001557 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1558 return false;
1559 // May throw; "pread" is a valid pthread cancellation point.
1560 setDoesNotCapture(F, 2);
1561 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001562 case LibFunc::pwrite:
1563 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1564 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001565 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001566 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001567 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001568 break;
1569 case LibFunc::putchar:
1570 setDoesNotThrow(F);
1571 break;
1572 case LibFunc::popen:
1573 if (FTy->getNumParams() != 2 ||
1574 !FTy->getReturnType()->isPointerTy() ||
1575 !FTy->getParamType(0)->isPointerTy() ||
1576 !FTy->getParamType(1)->isPointerTy())
1577 return false;
1578 setDoesNotThrow(F);
1579 setDoesNotAlias(F, 0);
1580 setDoesNotCapture(F, 1);
1581 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001582 setOnlyReadsMemory(F, 1);
1583 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001584 break;
1585 case LibFunc::pclose:
1586 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1587 return false;
1588 setDoesNotThrow(F);
1589 setDoesNotCapture(F, 1);
1590 break;
1591 case LibFunc::vscanf:
1592 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->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::vsscanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001599 if (FTy->getNumParams() != 3 ||
1600 !FTy->getParamType(1)->isPointerTy() ||
1601 !FTy->getParamType(2)->isPointerTy())
1602 return false;
1603 setDoesNotThrow(F);
1604 setDoesNotCapture(F, 1);
1605 setDoesNotCapture(F, 2);
1606 setOnlyReadsMemory(F, 1);
1607 setOnlyReadsMemory(F, 2);
1608 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001609 case LibFunc::vfscanf:
1610 if (FTy->getNumParams() != 3 ||
1611 !FTy->getParamType(1)->isPointerTy() ||
1612 !FTy->getParamType(2)->isPointerTy())
1613 return false;
1614 setDoesNotThrow(F);
1615 setDoesNotCapture(F, 1);
1616 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001617 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001618 break;
1619 case LibFunc::valloc:
1620 if (!FTy->getReturnType()->isPointerTy())
1621 return false;
1622 setDoesNotThrow(F);
1623 setDoesNotAlias(F, 0);
1624 break;
1625 case LibFunc::vprintf:
1626 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1627 return false;
1628 setDoesNotThrow(F);
1629 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001630 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001631 break;
1632 case LibFunc::vfprintf:
1633 case LibFunc::vsprintf:
1634 if (FTy->getNumParams() != 3 ||
1635 !FTy->getParamType(0)->isPointerTy() ||
1636 !FTy->getParamType(1)->isPointerTy())
1637 return false;
1638 setDoesNotThrow(F);
1639 setDoesNotCapture(F, 1);
1640 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001641 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001642 break;
1643 case LibFunc::vsnprintf:
1644 if (FTy->getNumParams() != 4 ||
1645 !FTy->getParamType(0)->isPointerTy() ||
1646 !FTy->getParamType(2)->isPointerTy())
1647 return false;
1648 setDoesNotThrow(F);
1649 setDoesNotCapture(F, 1);
1650 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001651 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001652 break;
1653 case LibFunc::open:
1654 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1655 return false;
1656 // May throw; "open" is a valid pthread cancellation point.
1657 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001658 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001659 break;
1660 case LibFunc::opendir:
1661 if (FTy->getNumParams() != 1 ||
1662 !FTy->getReturnType()->isPointerTy() ||
1663 !FTy->getParamType(0)->isPointerTy())
1664 return false;
1665 setDoesNotThrow(F);
1666 setDoesNotAlias(F, 0);
1667 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001668 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001669 break;
1670 case LibFunc::tmpfile:
1671 if (!FTy->getReturnType()->isPointerTy())
1672 return false;
1673 setDoesNotThrow(F);
1674 setDoesNotAlias(F, 0);
1675 break;
1676 case LibFunc::times:
1677 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1678 return false;
1679 setDoesNotThrow(F);
1680 setDoesNotCapture(F, 1);
1681 break;
1682 case LibFunc::htonl:
1683 case LibFunc::htons:
1684 case LibFunc::ntohl:
1685 case LibFunc::ntohs:
1686 setDoesNotThrow(F);
1687 setDoesNotAccessMemory(F);
1688 break;
1689 case LibFunc::lstat:
1690 if (FTy->getNumParams() != 2 ||
1691 !FTy->getParamType(0)->isPointerTy() ||
1692 !FTy->getParamType(1)->isPointerTy())
1693 return false;
1694 setDoesNotThrow(F);
1695 setDoesNotCapture(F, 1);
1696 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001697 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001698 break;
1699 case LibFunc::lchown:
1700 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1701 return false;
1702 setDoesNotThrow(F);
1703 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001704 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001705 break;
1706 case LibFunc::qsort:
1707 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1708 return false;
1709 // May throw; places call through function pointer.
1710 setDoesNotCapture(F, 4);
1711 break;
1712 case LibFunc::dunder_strdup:
1713 case LibFunc::dunder_strndup:
1714 if (FTy->getNumParams() < 1 ||
1715 !FTy->getReturnType()->isPointerTy() ||
1716 !FTy->getParamType(0)->isPointerTy())
1717 return false;
1718 setDoesNotThrow(F);
1719 setDoesNotAlias(F, 0);
1720 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001721 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001722 break;
1723 case LibFunc::dunder_strtok_r:
1724 if (FTy->getNumParams() != 3 ||
1725 !FTy->getParamType(1)->isPointerTy())
1726 return false;
1727 setDoesNotThrow(F);
1728 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001729 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001730 break;
1731 case LibFunc::under_IO_getc:
1732 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1733 return false;
1734 setDoesNotThrow(F);
1735 setDoesNotCapture(F, 1);
1736 break;
1737 case LibFunc::under_IO_putc:
1738 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1739 return false;
1740 setDoesNotThrow(F);
1741 setDoesNotCapture(F, 2);
1742 break;
1743 case LibFunc::dunder_isoc99_scanf:
1744 if (FTy->getNumParams() < 1 ||
1745 !FTy->getParamType(0)->isPointerTy())
1746 return false;
1747 setDoesNotThrow(F);
1748 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001749 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001750 break;
1751 case LibFunc::stat64:
1752 case LibFunc::lstat64:
1753 case LibFunc::statvfs64:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001754 if (FTy->getNumParams() < 1 ||
1755 !FTy->getParamType(0)->isPointerTy() ||
1756 !FTy->getParamType(1)->isPointerTy())
1757 return false;
1758 setDoesNotThrow(F);
1759 setDoesNotCapture(F, 1);
1760 setDoesNotCapture(F, 2);
1761 setOnlyReadsMemory(F, 1);
1762 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001763 case LibFunc::dunder_isoc99_sscanf:
1764 if (FTy->getNumParams() < 1 ||
1765 !FTy->getParamType(0)->isPointerTy() ||
1766 !FTy->getParamType(1)->isPointerTy())
1767 return false;
1768 setDoesNotThrow(F);
1769 setDoesNotCapture(F, 1);
1770 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001771 setOnlyReadsMemory(F, 1);
1772 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001773 break;
1774 case LibFunc::fopen64:
1775 if (FTy->getNumParams() != 2 ||
1776 !FTy->getReturnType()->isPointerTy() ||
1777 !FTy->getParamType(0)->isPointerTy() ||
1778 !FTy->getParamType(1)->isPointerTy())
1779 return false;
1780 setDoesNotThrow(F);
1781 setDoesNotAlias(F, 0);
1782 setDoesNotCapture(F, 1);
1783 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001784 setOnlyReadsMemory(F, 1);
1785 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001786 break;
1787 case LibFunc::fseeko64:
1788 case LibFunc::ftello64:
1789 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1790 return false;
1791 setDoesNotThrow(F);
1792 setDoesNotCapture(F, 1);
1793 break;
1794 case LibFunc::tmpfile64:
1795 if (!FTy->getReturnType()->isPointerTy())
1796 return false;
1797 setDoesNotThrow(F);
1798 setDoesNotAlias(F, 0);
1799 break;
1800 case LibFunc::fstat64:
1801 case LibFunc::fstatvfs64:
1802 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1803 return false;
1804 setDoesNotThrow(F);
1805 setDoesNotCapture(F, 2);
1806 break;
1807 case LibFunc::open64:
1808 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1809 return false;
1810 // May throw; "open" is a valid pthread cancellation point.
1811 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001812 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001813 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001814 case LibFunc::gettimeofday:
1815 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1816 !FTy->getParamType(1)->isPointerTy())
1817 return false;
1818 // Currently some platforms have the restrict keyword on the arguments to
1819 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1820 // arguments.
1821 setDoesNotThrow(F);
1822 setDoesNotCapture(F, 1);
1823 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001824 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001825 default:
1826 // Didn't mark any attributes.
1827 return false;
1828 }
1829
1830 return true;
1831}
1832
1833/// annotateLibraryCalls - Adds attributes to well-known standard library
1834/// call declarations.
1835bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1836 bool MadeChange = false;
1837
1838 // Check each function in turn annotating well-known library function
1839 // declarations with attributes.
1840 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1841 Function *F = (*I)->getFunction();
1842
Craig Topperf40110f2014-04-25 05:29:35 +00001843 if (F && F->isDeclaration())
Meador Inge6b6a1612013-03-21 00:55:59 +00001844 MadeChange |= inferPrototypeAttributes(*F);
1845 }
1846
1847 return MadeChange;
1848}
1849
Chris Lattner4422d312010-04-16 22:42:17 +00001850bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman86449d72010-11-08 16:10:15 +00001851 AA = &getAnalysis<AliasAnalysis>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001852 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohman86449d72010-11-08 16:10:15 +00001853
Meador Inge6b6a1612013-03-21 00:55:59 +00001854 bool Changed = annotateLibraryCalls(SCC);
1855 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001856 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001857 Changed |= AddNoAliasAttrs(SCC);
Philip Reamesa88caea2015-08-31 19:44:38 +00001858 Changed |= AddNonNullAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001859 return Changed;
1860}