blob: 4860608d239574bd381cdf8a650367a3ce0e185c [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"
Chandler Carruth7b560d42015-09-09 17:55:00 +000027#include "llvm/Analysis/AssumptionCache.h"
28#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000030#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000031#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000032#include "llvm/Analysis/TargetLibraryInfo.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000033#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000034#include "llvm/IR/GlobalVariable.h"
Chandler Carruth83948572014-03-04 10:30:26 +000035#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000036#include "llvm/IR/IntrinsicInst.h"
37#include "llvm/IR/LLVMContext.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000038#include "llvm/Support/Debug.h"
Hans Wennborg043bf5b2015-08-31 21:19:18 +000039#include "llvm/Support/raw_ostream.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000040#include "llvm/Analysis/TargetLibraryInfo.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000041using namespace llvm;
42
Chandler Carruth964daaa2014-04-22 02:55:47 +000043#define DEBUG_TYPE "functionattrs"
44
Duncan Sands44c8cd92008-12-31 16:14:43 +000045STATISTIC(NumReadNone, "Number of functions marked readnone");
46STATISTIC(NumReadOnly, "Number of functions marked readonly");
47STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000048STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
49STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000050STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Philip Reamesa88caea2015-08-31 19:44:38 +000051STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
Meador Inge6b6a1612013-03-21 00:55:59 +000052STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Duncan Sands44c8cd92008-12-31 16:14:43 +000053
54namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000055 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands44c8cd92008-12-31 16:14:43 +000056 static char ID; // Pass identification, replacement for typeid
Chandler Carruth7b560d42015-09-09 17:55:00 +000057 FunctionAttrs() : CallGraphSCCPass(ID) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000058 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
59 }
Duncan Sands44c8cd92008-12-31 16:14:43 +000060
61 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Craig Topper3e4c6972014-03-05 09:10:37 +000062 bool runOnSCC(CallGraphSCC &SCC) override;
Duncan Sands44c8cd92008-12-31 16:14:43 +000063
64 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000065 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000066
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000067 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
68 bool AddArgumentAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000069
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000070 // IsFunctionMallocLike - Does this function allocate new memory?
71 bool IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +000072 SmallPtrSet<Function*, 8> &) const;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000073
74 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000075 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000076
Philip Reamesa88caea2015-08-31 19:44:38 +000077 /// \brief Does this function return null?
78 bool ReturnsNonNull(Function *F, SmallPtrSet<Function*, 8> &,
79 bool &Speculative) const;
80
81 /// \brief Deduce nonnull attributes for the SCC.
82 bool AddNonNullAttrs(const CallGraphSCC &SCC);
83
Meador Inge6b6a1612013-03-21 00:55:59 +000084 // Utility methods used by inferPrototypeAttributes to add attributes
85 // and maintain annotation statistics.
86
87 void setDoesNotAccessMemory(Function &F) {
88 if (!F.doesNotAccessMemory()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000089 F.setDoesNotAccessMemory();
90 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000091 }
92 }
93
94 void setOnlyReadsMemory(Function &F) {
95 if (!F.onlyReadsMemory()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000096 F.setOnlyReadsMemory();
97 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000098 }
99 }
100
101 void setDoesNotThrow(Function &F) {
102 if (!F.doesNotThrow()) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000103 F.setDoesNotThrow();
104 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +0000105 }
106 }
107
108 void setDoesNotCapture(Function &F, unsigned n) {
109 if (!F.doesNotCapture(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000110 F.setDoesNotCapture(n);
111 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +0000112 }
113 }
114
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000115 void setOnlyReadsMemory(Function &F, unsigned n) {
116 if (!F.onlyReadsMemory(n)) {
117 F.setOnlyReadsMemory(n);
118 ++NumAnnotated;
119 }
120 }
121
Meador Inge6b6a1612013-03-21 00:55:59 +0000122 void setDoesNotAlias(Function &F, unsigned n) {
123 if (!F.doesNotAlias(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000124 F.setDoesNotAlias(n);
125 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +0000126 }
127 }
128
129 // inferPrototypeAttributes - Analyze the name and prototype of the
130 // given function and set any applicable attributes. Returns true
131 // if any attributes were set and false otherwise.
132 bool inferPrototypeAttributes(Function &F);
133
134 // annotateLibraryCalls - Adds attributes to well-known standard library
135 // call declarations.
136 bool annotateLibraryCalls(const CallGraphSCC &SCC);
137
Craig Topper3e4c6972014-03-05 09:10:37 +0000138 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000139 AU.setPreservesCFG();
Chandler Carruth7b560d42015-09-09 17:55:00 +0000140 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000141 AU.addRequired<TargetLibraryInfoWrapperPass>();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000142 CallGraphSCCPass::getAnalysisUsage(AU);
143 }
144
Dan Gohman86449d72010-11-08 16:10:15 +0000145 private:
Meador Inge6b6a1612013-03-21 00:55:59 +0000146 TargetLibraryInfo *TLI;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000147 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000148}
Duncan Sands44c8cd92008-12-31 16:14:43 +0000149
150char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +0000151INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
152 "Deduce function attributes", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +0000153INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000154INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000155INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +0000156INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000157 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000158
159Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
160
161
Duncan Sands44c8cd92008-12-31 16:14:43 +0000162/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000163bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000164 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000165
166 // Fill SCCNodes with the elements of the SCC. Used for quickly
167 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000168 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
169 SCCNodes.insert((*I)->getFunction());
Duncan Sands44c8cd92008-12-31 16:14:43 +0000170
171 // Check if any of the functions in the SCC read or write memory. If they
172 // write memory then they can't be marked readnone or readonly.
173 bool ReadsMemory = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000174 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
175 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000176
Chandler Carruth0fb99812014-08-13 10:49:33 +0000177 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
178 // External node or node we don't want to optimize - assume it may write
179 // memory and give up.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000180 return false;
181
Chandler Carruth7b560d42015-09-09 17:55:00 +0000182 // We need to manually construct BasicAA directly in order to disable its
183 // use of other function analyses.
184 BasicAAResult BAR(createLegacyPMBasicAAResult(*this, *F));
185
186 // Construct our own AA results for this function. We do this manually to
187 // work around the limitations of the legacy pass manager.
188 AAResults AAR(createLegacyPMAAResults(*this, *F, BAR));
189
190 FunctionModRefBehavior MRB = AAR.getModRefBehavior(F);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000191 if (MRB == FMRB_DoesNotAccessMemory)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000192 // Already perfect!
193 continue;
194
195 // Definitions with weak linkage may be overridden at linktime with
196 // something that writes memory, so treat them like declarations.
197 if (F->isDeclaration() || F->mayBeOverridden()) {
Dan Gohman35814e62010-11-09 20:13:27 +0000198 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000199 // May write memory. Just give up.
200 return false;
201
202 ReadsMemory = true;
203 continue;
204 }
205
206 // Scan the function body for instructions that may read or write memory.
207 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
208 Instruction *I = &*II;
209
210 // Some instructions can be ignored even if they read or write memory.
211 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif62f0aac2010-07-28 22:50:26 +0000212 CallSite CS(cast<Value>(I));
Dan Gohman86449d72010-11-08 16:10:15 +0000213 if (CS) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000214 // Ignore calls to functions in the same SCC.
Dan Gohman86449d72010-11-08 16:10:15 +0000215 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000216 continue;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000217 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
Dan Gohman2694e142010-11-10 01:02:18 +0000218 // If the call doesn't access arbitrary memory, we may be able to
219 // figure out something.
Dan Gohman25775802010-11-10 17:34:04 +0000220 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
221 // If the call does access argument pointees, check each argument.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000222 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman2694e142010-11-10 01:02:18 +0000223 // Check whether all pointer arguments point to local memory, and
224 // ignore calls that only access local memory.
225 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
226 CI != CE; ++CI) {
227 Value *Arg = *CI;
228 if (Arg->getType()->isPointerTy()) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000229 AAMDNodes AAInfo;
230 I->getAAMetadata(AAInfo);
231
Chandler Carruthecbd1682015-06-17 07:21:38 +0000232 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000233 if (!AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000234 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000235 // Writes non-local memory. Give up.
236 return false;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000237 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000238 // Ok, it reads non-local memory.
239 ReadsMemory = true;
240 }
Dan Gohmande521552010-11-09 19:56:27 +0000241 }
242 }
Dan Gohmande521552010-11-09 19:56:27 +0000243 continue;
Dan Gohman86449d72010-11-08 16:10:15 +0000244 }
Dan Gohman2694e142010-11-10 01:02:18 +0000245 // The call could access any memory. If that includes writes, give up.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000246 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000247 return false;
248 // If it reads, note it.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000249 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000250 ReadsMemory = true;
251 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000252 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000253 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
254 if (!LI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000255 MemoryLocation Loc = MemoryLocation::get(LI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000256 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000257 continue;
258 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000259 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000260 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
261 if (!SI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000262 MemoryLocation Loc = MemoryLocation::get(SI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000263 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000264 continue;
265 }
Dan Gohmane3467a72010-11-09 20:17:38 +0000266 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
267 // Ignore vaargs on local memory.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000268 MemoryLocation Loc = MemoryLocation::get(VI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000269 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
Dan Gohmane3467a72010-11-09 20:17:38 +0000270 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000271 }
272
273 // Any remaining instructions need to be taken seriously! Check if they
274 // read or write memory.
275 if (I->mayWriteToMemory())
276 // Writes memory. Just give up.
277 return false;
Duncan Sands9759f2e2009-05-06 08:42:00 +0000278
Duncan Sands44c8cd92008-12-31 16:14:43 +0000279 // If this instruction may read memory, remember that.
280 ReadsMemory |= I->mayReadFromMemory();
281 }
282 }
283
284 // Success! Functions in this SCC do not access memory, or only read memory.
285 // Give them the appropriate attribute.
286 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000287 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
288 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000289
290 if (F->doesNotAccessMemory())
291 // Already perfect!
292 continue;
293
294 if (F->onlyReadsMemory() && ReadsMemory)
295 // No change.
296 continue;
297
298 MadeChange = true;
299
300 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000301 AttrBuilder B;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000302 B.addAttribute(Attribute::ReadOnly)
303 .addAttribute(Attribute::ReadNone);
Bill Wendling430fa9b2013-01-23 00:45:55 +0000304 F->removeAttributes(AttributeSet::FunctionIndex,
305 AttributeSet::get(F->getContext(),
306 AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000307
308 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000309 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000310 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000311
312 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000313 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000314 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000315 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000316 }
317
318 return MadeChange;
319}
320
Nick Lewycky4c378a42011-12-28 23:24:21 +0000321namespace {
322 // For a given pointer Argument, this retains a list of Arguments of functions
323 // in the same SCC that the pointer data flows into. We use this to build an
324 // SCC of the arguments.
325 struct ArgumentGraphNode {
326 Argument *Definition;
327 SmallVector<ArgumentGraphNode*, 4> Uses;
328 };
329
330 class ArgumentGraph {
331 // We store pointers to ArgumentGraphNode objects, so it's important that
332 // that they not move around upon insert.
333 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
334
335 ArgumentMapTy ArgumentMap;
336
337 // There is no root node for the argument graph, in fact:
338 // void f(int *x, int *y) { if (...) f(x, y); }
339 // is an example where the graph is disconnected. The SCCIterator requires a
340 // single entry point, so we maintain a fake ("synthetic") root node that
341 // uses every node. Because the graph is directed and nothing points into
342 // the root, it will not participate in any SCCs (except for its own).
343 ArgumentGraphNode SyntheticRoot;
344
345 public:
Craig Topperf40110f2014-04-25 05:29:35 +0000346 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000347
348 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
349
350 iterator begin() { return SyntheticRoot.Uses.begin(); }
351 iterator end() { return SyntheticRoot.Uses.end(); }
352 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
353
354 ArgumentGraphNode *operator[](Argument *A) {
355 ArgumentGraphNode &Node = ArgumentMap[A];
356 Node.Definition = A;
357 SyntheticRoot.Uses.push_back(&Node);
358 return &Node;
359 }
360 };
361
362 // This tracker checks whether callees are in the SCC, and if so it does not
363 // consider that a capture, instead adding it to the "Uses" list and
364 // continuing with the analysis.
365 struct ArgumentUsesTracker : public CaptureTracker {
366 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
367 : Captured(false), SCCNodes(SCCNodes) {}
368
Craig Topper3e4c6972014-03-05 09:10:37 +0000369 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000370
Chandler Carruth64e9aa52014-03-05 10:21:48 +0000371 bool captured(const Use *U) override {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000372 CallSite CS(U->getUser());
373 if (!CS.getInstruction()) { Captured = true; return true; }
374
375 Function *F = CS.getCalledFunction();
376 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
377
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000378 bool Found = false;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000379 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
380 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
381 PI != PE; ++PI, ++AI) {
382 if (AI == AE) {
383 assert(F->isVarArg() && "More params than args in non-varargs call");
384 Captured = true;
385 return true;
386 }
387 if (PI == U) {
388 Uses.push_back(AI);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000389 Found = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000390 break;
391 }
392 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000393 assert(Found && "Capturing call-site captured nothing?");
Duncan Sandsc9e95ad2013-09-13 08:16:06 +0000394 (void)Found;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000395 return false;
396 }
397
398 bool Captured; // True only if certainly captured (used outside our SCC).
399 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
400
401 const SmallPtrSet<Function*, 8> &SCCNodes;
402 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000403}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000404
405namespace llvm {
406 template<> struct GraphTraits<ArgumentGraphNode*> {
407 typedef ArgumentGraphNode NodeType;
408 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
409
410 static inline NodeType *getEntryNode(NodeType *A) { return A; }
411 static inline ChildIteratorType child_begin(NodeType *N) {
412 return N->Uses.begin();
413 }
414 static inline ChildIteratorType child_end(NodeType *N) {
415 return N->Uses.end();
416 }
417 };
418 template<> struct GraphTraits<ArgumentGraph*>
419 : public GraphTraits<ArgumentGraphNode*> {
420 static NodeType *getEntryNode(ArgumentGraph *AG) {
421 return AG->getEntryNode();
422 }
423 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
424 return AG->begin();
425 }
426 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
427 return AG->end();
428 }
429 };
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000430}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000431
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000432// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
433static Attribute::AttrKind
434determinePointerReadAttrs(Argument *A,
435 const SmallPtrSet<Argument*, 8> &SCCNodes) {
436
437 SmallVector<Use*, 32> Worklist;
438 SmallSet<Use*, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000439
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000440 // inalloca arguments are always clobbered by the call.
441 if (A->hasInAllocaAttr())
442 return Attribute::None;
443
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000444 bool IsRead = false;
445 // We don't need to track IsWritten. If A is written to, return immediately.
446
Chandler Carruthcdf47882014-03-09 03:16:01 +0000447 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000448 Visited.insert(&U);
449 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000450 }
451
452 while (!Worklist.empty()) {
453 Use *U = Worklist.pop_back_val();
454 Instruction *I = cast<Instruction>(U->getUser());
455 Value *V = U->get();
456
457 switch (I->getOpcode()) {
458 case Instruction::BitCast:
459 case Instruction::GetElementPtr:
460 case Instruction::PHI:
461 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000462 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000463 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000464 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000465 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000466 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000467 break;
468
469 case Instruction::Call:
470 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000471 bool Captures = true;
472
473 if (I->getType()->isVoidTy())
474 Captures = false;
475
476 auto AddUsersToWorklistIfCapturing = [&] {
477 if (Captures)
478 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000479 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000480 Worklist.push_back(&UU);
481 };
482
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000483 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000484 if (CS.doesNotAccessMemory()) {
485 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000486 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000487 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000488
489 Function *F = CS.getCalledFunction();
490 if (!F) {
491 if (CS.onlyReadsMemory()) {
492 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000493 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000494 continue;
495 }
496 return Attribute::None;
497 }
498
499 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
500 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
501 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
502 if (A->get() == V) {
503 if (AI == AE) {
504 assert(F->isVarArg() &&
505 "More params than args in non-varargs call.");
506 return Attribute::None;
507 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000508 Captures &= !CS.doesNotCapture(A - B);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000509 if (SCCNodes.count(AI))
510 continue;
511 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
512 return Attribute::None;
513 if (!CS.doesNotAccessMemory(A - B))
514 IsRead = true;
515 }
516 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000517 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000518 break;
519 }
520
521 case Instruction::Load:
522 IsRead = true;
523 break;
524
525 case Instruction::ICmp:
526 case Instruction::Ret:
527 break;
528
529 default:
530 return Attribute::None;
531 }
532 }
533
534 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
535}
536
537/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
538bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000539 bool Changed = false;
540
Nick Lewycky4c378a42011-12-28 23:24:21 +0000541 SmallPtrSet<Function*, 8> SCCNodes;
542
543 // Fill SCCNodes with the elements of the SCC. Used for quickly
544 // looking up whether a given CallGraphNode is in this SCC.
545 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
546 Function *F = (*I)->getFunction();
Chandler Carruth0fb99812014-08-13 10:49:33 +0000547 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
548 !F->hasFnAttribute(Attribute::OptimizeNone))
Nick Lewycky4c378a42011-12-28 23:24:21 +0000549 SCCNodes.insert(F);
550 }
551
552 ArgumentGraph AG;
553
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000554 AttrBuilder B;
555 B.addAttribute(Attribute::NoCapture);
556
Duncan Sands44c8cd92008-12-31 16:14:43 +0000557 // Check each function in turn, determining which pointer arguments are not
558 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000559 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
560 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000561
Chandler Carruth0fb99812014-08-13 10:49:33 +0000562 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
563 // External node or function we're trying not to optimize - only a problem
564 // for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000565 continue;
566
567 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000568 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000569 if (F->isDeclaration() || F->mayBeOverridden())
570 continue;
571
Nick Lewycky4c378a42011-12-28 23:24:21 +0000572 // Functions that are readonly (or readnone) and nounwind and don't return
573 // a value can't capture arguments. Don't analyze them.
574 if (F->onlyReadsMemory() && F->doesNotThrow() &&
575 F->getReturnType()->isVoidTy()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000576 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
577 A != E; ++A) {
578 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
579 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
580 ++NumNoCapture;
581 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000582 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000583 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000584 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000585 }
586
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000587 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
588 A != E; ++A) {
589 if (!A->getType()->isPointerTy()) continue;
590 bool HasNonLocalUses = false;
591 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000592 ArgumentUsesTracker Tracker(SCCNodes);
593 PointerMayBeCaptured(A, &Tracker);
594 if (!Tracker.Captured) {
595 if (Tracker.Uses.empty()) {
596 // If it's trivially not captured, mark it nocapture now.
597 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
598 ++NumNoCapture;
599 Changed = true;
600 } else {
601 // If it's not trivially captured and not trivially not captured,
602 // then it must be calling into another function in our SCC. Save
603 // its particulars for Argument-SCC analysis later.
604 ArgumentGraphNode *Node = AG[A];
605 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000606 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000607 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000608 if (*UI != A)
609 HasNonLocalUses = true;
610 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000611 }
612 }
613 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
614 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000615 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
616 // Can we determine that it's readonly/readnone without doing an SCC?
617 // Note that we don't allow any calls at all here, or else our result
618 // will be dependent on the iteration order through the functions in the
619 // SCC.
620 SmallPtrSet<Argument*, 8> Self;
621 Self.insert(A);
622 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
623 if (R != Attribute::None) {
624 AttrBuilder B;
625 B.addAttribute(R);
626 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
627 Changed = true;
628 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
629 }
630 }
631 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000632 }
633
634 // The graph we've collected is partial because we stopped scanning for
635 // argument uses once we solved the argument trivially. These partial nodes
636 // show up as ArgumentGraphNode objects with an empty Uses list, and for
637 // these nodes the final decision about whether they capture has already been
638 // made. If the definition doesn't have a 'nocapture' attribute by now, it
639 // captures.
640
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000641 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000642 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000643 if (ArgumentSCC.size() == 1) {
644 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
645
646 // eg. "void f(int* x) { if (...) f(x); }"
647 if (ArgumentSCC[0]->Uses.size() == 1 &&
648 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000649 Argument *A = ArgumentSCC[0]->Definition;
650 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000651 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000652 Changed = true;
653 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000654 continue;
655 }
656
657 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000658 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
659 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000660 ArgumentGraphNode *Node = *I;
661 if (Node->Uses.empty()) {
662 if (!Node->Definition->hasNoCaptureAttr())
663 SCCCaptured = true;
664 }
665 }
666 if (SCCCaptured) continue;
667
668 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
669 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
670 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000671 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000672 ArgumentSCCNodes.insert((*I)->Definition);
673 }
674
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000675 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
676 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000677 ArgumentGraphNode *N = *I;
678 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
679 UE = N->Uses.end(); UI != UE; ++UI) {
680 Argument *A = (*UI)->Definition;
681 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
682 continue;
683 SCCCaptured = true;
684 break;
685 }
686 }
687 if (SCCCaptured) continue;
688
Nick Lewyckyf740db32012-01-05 22:21:45 +0000689 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000690 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000691 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000692 ++NumNoCapture;
693 Changed = true;
694 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000695
696 // We also want to compute readonly/readnone. With a small number of false
697 // negatives, we can assume that any pointer which is captured isn't going
698 // to be provably readonly or readnone, since by definition we can't
699 // analyze all uses of a captured pointer.
700 //
701 // The false negatives happen when the pointer is captured by a function
702 // that promises readonly/readnone behaviour on the pointer, then the
703 // pointer's lifetime ends before anything that writes to arbitrary memory.
704 // Also, a readonly/readnone pointer may be returned, but returning a
705 // pointer is capturing it.
706
707 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
708 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
709 Argument *A = ArgumentSCC[i]->Definition;
710 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
711 if (K == Attribute::ReadNone)
712 continue;
713 if (K == Attribute::ReadOnly) {
714 ReadAttr = Attribute::ReadOnly;
715 continue;
716 }
717 ReadAttr = K;
718 break;
719 }
720
721 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000722 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000723 B.addAttribute(ReadAttr);
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000724 R.addAttribute(Attribute::ReadOnly)
725 .addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000726 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
727 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000728 // Clear out existing readonly/readnone attributes
729 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000730 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
731 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
732 Changed = true;
733 }
734 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000735 }
736
737 return Changed;
738}
739
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000740/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000741/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000742bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +0000743 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramer15591272012-10-31 13:45:49 +0000744 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000745 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
746 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
747 FlowsToReturn.insert(Ret->getReturnValue());
748
749 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000750 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000751
752 if (Constant *C = dyn_cast<Constant>(RetVal)) {
753 if (!C->isNullValue() && !isa<UndefValue>(C))
754 return false;
755
756 continue;
757 }
758
759 if (isa<Argument>(RetVal))
760 return false;
761
762 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
763 switch (RVI->getOpcode()) {
764 // Extend the analysis by looking upwards.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000765 case Instruction::BitCast:
Victor Hernandez5d034492009-09-18 22:35:49 +0000766 case Instruction::GetElementPtr:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000767 case Instruction::AddrSpaceCast:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000768 FlowsToReturn.insert(RVI->getOperand(0));
769 continue;
770 case Instruction::Select: {
771 SelectInst *SI = cast<SelectInst>(RVI);
772 FlowsToReturn.insert(SI->getTrueValue());
773 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner43d0db72009-09-27 21:29:28 +0000774 continue;
775 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000776 case Instruction::PHI: {
777 PHINode *PN = cast<PHINode>(RVI);
Pete Cooper833f34d2015-05-12 20:05:31 +0000778 for (Value *IncValue : PN->incoming_values())
779 FlowsToReturn.insert(IncValue);
Chris Lattner43d0db72009-09-27 21:29:28 +0000780 continue;
781 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000782
783 // Check whether the pointer came from an allocation.
784 case Instruction::Alloca:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000785 break;
786 case Instruction::Call:
787 case Instruction::Invoke: {
788 CallSite CS(RVI);
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000789 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000790 break;
791 if (CS.getCalledFunction() &&
Chris Lattner2f2110a2009-08-31 04:09:04 +0000792 SCCNodes.count(CS.getCalledFunction()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000793 break;
794 } // fall-through
795 default:
796 return false; // Did not come from an allocation.
797 }
798
Dan Gohman94e61762009-11-19 21:57:48 +0000799 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000800 return false;
801 }
802
803 return true;
804}
805
806/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000807bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000808 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000809
810 // Fill SCCNodes with the elements of the SCC. Used for quickly
811 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000812 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
813 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000814
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000815 // Check each function in turn, determining which functions return noalias
816 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000817 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
818 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000819
Chandler Carruth0fb99812014-08-13 10:49:33 +0000820 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
821 // External node or node we don't want to optimize - skip it;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000822 return false;
823
824 // Already noalias.
825 if (F->doesNotAlias(0))
826 continue;
827
828 // Definitions with weak linkage may be overridden at linktime, so
829 // treat them like declarations.
830 if (F->isDeclaration() || F->mayBeOverridden())
831 return false;
832
833 // We annotate noalias return values, which are only applicable to
834 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000835 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000836 continue;
837
838 if (!IsFunctionMallocLike(F, SCCNodes))
839 return false;
840 }
841
842 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000843 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
844 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000845 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000846 continue;
847
848 F->setDoesNotAlias(0);
849 ++NumNoAlias;
850 MadeChange = true;
851 }
852
853 return MadeChange;
854}
855
Philip Reamesa88caea2015-08-31 19:44:38 +0000856bool FunctionAttrs::ReturnsNonNull(Function *F,
857 SmallPtrSet<Function*, 8> &SCCNodes,
858 bool &Speculative) const {
859 assert(F->getReturnType()->isPointerTy() &&
860 "nonnull only meaningful on pointer types");
861 Speculative = false;
862
863 SmallSetVector<Value *, 8> FlowsToReturn;
864 for (BasicBlock &BB : *F)
865 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
866 FlowsToReturn.insert(Ret->getReturnValue());
867
868 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
869 Value *RetVal = FlowsToReturn[i];
870
871 // If this value is locally known to be non-null, we're good
872 if (isKnownNonNull(RetVal, TLI))
873 continue;
874
875 // Otherwise, we need to look upwards since we can't make any local
876 // conclusions.
877 Instruction *RVI = dyn_cast<Instruction>(RetVal);
878 if (!RVI)
879 return false;
880 switch (RVI->getOpcode()) {
881 // Extend the analysis by looking upwards.
882 case Instruction::BitCast:
883 case Instruction::GetElementPtr:
884 case Instruction::AddrSpaceCast:
885 FlowsToReturn.insert(RVI->getOperand(0));
886 continue;
887 case Instruction::Select: {
888 SelectInst *SI = cast<SelectInst>(RVI);
889 FlowsToReturn.insert(SI->getTrueValue());
890 FlowsToReturn.insert(SI->getFalseValue());
891 continue;
892 }
893 case Instruction::PHI: {
894 PHINode *PN = cast<PHINode>(RVI);
895 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
896 FlowsToReturn.insert(PN->getIncomingValue(i));
897 continue;
898 }
899 case Instruction::Call:
900 case Instruction::Invoke: {
901 CallSite CS(RVI);
902 Function *Callee = CS.getCalledFunction();
903 // A call to a node within the SCC is assumed to return null until
904 // proven otherwise
905 if (Callee && SCCNodes.count(Callee)) {
906 Speculative = true;
907 continue;
908 }
909 return false;
910 }
911 default:
912 return false; // Unknown source, may be null
913 };
914 llvm_unreachable("should have either continued or returned");
915 }
916
917 return true;
918}
919
920bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
921 SmallPtrSet<Function*, 8> SCCNodes;
922
923 // Fill SCCNodes with the elements of the SCC. Used for quickly
924 // looking up whether a given CallGraphNode is in this SCC.
925 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
926 SCCNodes.insert((*I)->getFunction());
927
928 // Speculative that all functions in the SCC return only nonnull
929 // pointers. We may refute this as we analyze functions.
930 bool SCCReturnsNonNull = true;
931
932 bool MadeChange = false;
933
934 // Check each function in turn, determining which functions return nonnull
935 // pointers.
936 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
937 Function *F = (*I)->getFunction();
938
939 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
940 // External node or node we don't want to optimize - skip it;
941 return false;
942
943 // Already nonnull.
944 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
945 Attribute::NonNull))
946 continue;
947
948 // Definitions with weak linkage may be overridden at linktime, so
949 // treat them like declarations.
950 if (F->isDeclaration() || F->mayBeOverridden())
951 return false;
952
953 // We annotate nonnull return values, which are only applicable to
954 // pointer types.
955 if (!F->getReturnType()->isPointerTy())
956 continue;
957
958 bool Speculative = false;
959 if (ReturnsNonNull(F, SCCNodes, Speculative)) {
960 if (!Speculative) {
961 // Mark the function eagerly since we may discover a function
962 // which prevents us from speculating about the entire SCC
963 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
964 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
965 ++NumNonNullReturn;
966 MadeChange = true;
967 }
968 continue;
969 }
970 // At least one function returns something which could be null, can't
971 // speculate any more.
972 SCCReturnsNonNull = false;
973 }
974
975 if (SCCReturnsNonNull) {
976 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
977 Function *F = (*I)->getFunction();
978 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
979 Attribute::NonNull) ||
980 !F->getReturnType()->isPointerTy())
981 continue;
982
983 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
984 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
985 ++NumNonNullReturn;
986 MadeChange = true;
987 }
988 }
989
990 return MadeChange;
991}
992
Meador Inge6b6a1612013-03-21 00:55:59 +0000993/// inferPrototypeAttributes - Analyze the name and prototype of the
994/// given function and set any applicable attributes. Returns true
995/// if any attributes were set and false otherwise.
996bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
Chandler Carruth0fb99812014-08-13 10:49:33 +0000997 if (F.hasFnAttribute(Attribute::OptimizeNone))
998 return false;
999
Meador Inge6b6a1612013-03-21 00:55:59 +00001000 FunctionType *FTy = F.getFunctionType();
1001 LibFunc::Func TheLibFunc;
1002 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
1003 return false;
1004
1005 switch (TheLibFunc) {
1006 case LibFunc::strlen:
1007 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1008 return false;
1009 setOnlyReadsMemory(F);
1010 setDoesNotThrow(F);
1011 setDoesNotCapture(F, 1);
1012 break;
1013 case LibFunc::strchr:
1014 case LibFunc::strrchr:
1015 if (FTy->getNumParams() != 2 ||
1016 !FTy->getParamType(0)->isPointerTy() ||
1017 !FTy->getParamType(1)->isIntegerTy())
1018 return false;
1019 setOnlyReadsMemory(F);
1020 setDoesNotThrow(F);
1021 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001022 case LibFunc::strtol:
1023 case LibFunc::strtod:
1024 case LibFunc::strtof:
1025 case LibFunc::strtoul:
1026 case LibFunc::strtoll:
1027 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +00001028 case LibFunc::strtoull:
1029 if (FTy->getNumParams() < 2 ||
1030 !FTy->getParamType(1)->isPointerTy())
1031 return false;
1032 setDoesNotThrow(F);
1033 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001034 setOnlyReadsMemory(F, 1);
1035 break;
1036 case LibFunc::strcpy:
1037 case LibFunc::stpcpy:
1038 case LibFunc::strcat:
1039 case LibFunc::strncat:
1040 case LibFunc::strncpy:
1041 case LibFunc::stpncpy:
1042 if (FTy->getNumParams() < 2 ||
1043 !FTy->getParamType(1)->isPointerTy())
1044 return false;
1045 setDoesNotThrow(F);
1046 setDoesNotCapture(F, 2);
1047 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001048 break;
1049 case LibFunc::strxfrm:
1050 if (FTy->getNumParams() != 3 ||
1051 !FTy->getParamType(0)->isPointerTy() ||
1052 !FTy->getParamType(1)->isPointerTy())
1053 return false;
1054 setDoesNotThrow(F);
1055 setDoesNotCapture(F, 1);
1056 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001057 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001058 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001059 case LibFunc::strcmp: //0,1
1060 case LibFunc::strspn: // 0,1
1061 case LibFunc::strncmp: // 0,1
1062 case LibFunc::strcspn: //0,1
1063 case LibFunc::strcoll: //0,1
1064 case LibFunc::strcasecmp: // 0,1
1065 case LibFunc::strncasecmp: //
Meador Inge6b6a1612013-03-21 00:55:59 +00001066 if (FTy->getNumParams() < 2 ||
1067 !FTy->getParamType(0)->isPointerTy() ||
1068 !FTy->getParamType(1)->isPointerTy())
1069 return false;
1070 setOnlyReadsMemory(F);
1071 setDoesNotThrow(F);
1072 setDoesNotCapture(F, 1);
1073 setDoesNotCapture(F, 2);
1074 break;
1075 case LibFunc::strstr:
1076 case LibFunc::strpbrk:
1077 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1078 return false;
1079 setOnlyReadsMemory(F);
1080 setDoesNotThrow(F);
1081 setDoesNotCapture(F, 2);
1082 break;
1083 case LibFunc::strtok:
1084 case LibFunc::strtok_r:
1085 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1086 return false;
1087 setDoesNotThrow(F);
1088 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001089 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001090 break;
1091 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001092 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1093 return false;
1094 setDoesNotThrow(F);
1095 setDoesNotCapture(F, 1);
1096 setOnlyReadsMemory(F, 1);
1097 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001098 case LibFunc::setbuf:
1099 case LibFunc::setvbuf:
1100 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1101 return false;
1102 setDoesNotThrow(F);
1103 setDoesNotCapture(F, 1);
1104 break;
1105 case LibFunc::strdup:
1106 case LibFunc::strndup:
1107 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1108 !FTy->getParamType(0)->isPointerTy())
1109 return false;
1110 setDoesNotThrow(F);
1111 setDoesNotAlias(F, 0);
1112 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001113 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001114 break;
1115 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +00001116 case LibFunc::statvfs:
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);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001124 setOnlyReadsMemory(F, 1);
1125 break;
1126 case LibFunc::sscanf:
1127 if (FTy->getNumParams() < 2 ||
1128 !FTy->getParamType(0)->isPointerTy() ||
1129 !FTy->getParamType(1)->isPointerTy())
1130 return false;
1131 setDoesNotThrow(F);
1132 setDoesNotCapture(F, 1);
1133 setDoesNotCapture(F, 2);
1134 setOnlyReadsMemory(F, 1);
1135 setOnlyReadsMemory(F, 2);
1136 break;
1137 case LibFunc::sprintf:
1138 if (FTy->getNumParams() < 2 ||
1139 !FTy->getParamType(0)->isPointerTy() ||
1140 !FTy->getParamType(1)->isPointerTy())
1141 return false;
1142 setDoesNotThrow(F);
1143 setDoesNotCapture(F, 1);
1144 setDoesNotCapture(F, 2);
1145 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001146 break;
1147 case LibFunc::snprintf:
1148 if (FTy->getNumParams() != 3 ||
1149 !FTy->getParamType(0)->isPointerTy() ||
1150 !FTy->getParamType(2)->isPointerTy())
1151 return false;
1152 setDoesNotThrow(F);
1153 setDoesNotCapture(F, 1);
1154 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001155 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001156 break;
1157 case LibFunc::setitimer:
1158 if (FTy->getNumParams() != 3 ||
1159 !FTy->getParamType(1)->isPointerTy() ||
1160 !FTy->getParamType(2)->isPointerTy())
1161 return false;
1162 setDoesNotThrow(F);
1163 setDoesNotCapture(F, 2);
1164 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001165 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001166 break;
1167 case LibFunc::system:
1168 if (FTy->getNumParams() != 1 ||
1169 !FTy->getParamType(0)->isPointerTy())
1170 return false;
1171 // May throw; "system" is a valid pthread cancellation point.
1172 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001173 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001174 break;
1175 case LibFunc::malloc:
1176 if (FTy->getNumParams() != 1 ||
1177 !FTy->getReturnType()->isPointerTy())
1178 return false;
1179 setDoesNotThrow(F);
1180 setDoesNotAlias(F, 0);
1181 break;
1182 case LibFunc::memcmp:
1183 if (FTy->getNumParams() != 3 ||
1184 !FTy->getParamType(0)->isPointerTy() ||
1185 !FTy->getParamType(1)->isPointerTy())
1186 return false;
1187 setOnlyReadsMemory(F);
1188 setDoesNotThrow(F);
1189 setDoesNotCapture(F, 1);
1190 setDoesNotCapture(F, 2);
1191 break;
1192 case LibFunc::memchr:
1193 case LibFunc::memrchr:
1194 if (FTy->getNumParams() != 3)
1195 return false;
1196 setOnlyReadsMemory(F);
1197 setDoesNotThrow(F);
1198 break;
1199 case LibFunc::modf:
1200 case LibFunc::modff:
1201 case LibFunc::modfl:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001202 if (FTy->getNumParams() < 2 ||
1203 !FTy->getParamType(1)->isPointerTy())
1204 return false;
1205 setDoesNotThrow(F);
1206 setDoesNotCapture(F, 2);
1207 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001208 case LibFunc::memcpy:
1209 case LibFunc::memccpy:
1210 case LibFunc::memmove:
1211 if (FTy->getNumParams() < 2 ||
1212 !FTy->getParamType(1)->isPointerTy())
1213 return false;
1214 setDoesNotThrow(F);
1215 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001216 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001217 break;
1218 case LibFunc::memalign:
1219 if (!FTy->getReturnType()->isPointerTy())
1220 return false;
1221 setDoesNotAlias(F, 0);
1222 break;
1223 case LibFunc::mkdir:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001224 if (FTy->getNumParams() == 0 ||
1225 !FTy->getParamType(0)->isPointerTy())
1226 return false;
1227 setDoesNotThrow(F);
1228 setDoesNotCapture(F, 1);
1229 setOnlyReadsMemory(F, 1);
1230 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001231 case LibFunc::mktime:
1232 if (FTy->getNumParams() == 0 ||
1233 !FTy->getParamType(0)->isPointerTy())
1234 return false;
1235 setDoesNotThrow(F);
1236 setDoesNotCapture(F, 1);
1237 break;
1238 case LibFunc::realloc:
1239 if (FTy->getNumParams() != 2 ||
1240 !FTy->getParamType(0)->isPointerTy() ||
1241 !FTy->getReturnType()->isPointerTy())
1242 return false;
1243 setDoesNotThrow(F);
1244 setDoesNotAlias(F, 0);
1245 setDoesNotCapture(F, 1);
1246 break;
1247 case LibFunc::read:
1248 if (FTy->getNumParams() != 3 ||
1249 !FTy->getParamType(1)->isPointerTy())
1250 return false;
1251 // May throw; "read" is a valid pthread cancellation point.
1252 setDoesNotCapture(F, 2);
1253 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001254 case LibFunc::rewind:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001255 if (FTy->getNumParams() < 1 ||
1256 !FTy->getParamType(0)->isPointerTy())
1257 return false;
1258 setDoesNotThrow(F);
1259 setDoesNotCapture(F, 1);
1260 break;
1261 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001262 case LibFunc::remove:
1263 case LibFunc::realpath:
1264 if (FTy->getNumParams() < 1 ||
1265 !FTy->getParamType(0)->isPointerTy())
1266 return false;
1267 setDoesNotThrow(F);
1268 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001269 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001270 break;
1271 case LibFunc::rename:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001272 if (FTy->getNumParams() < 2 ||
1273 !FTy->getParamType(0)->isPointerTy() ||
1274 !FTy->getParamType(1)->isPointerTy())
1275 return false;
1276 setDoesNotThrow(F);
1277 setDoesNotCapture(F, 1);
1278 setDoesNotCapture(F, 2);
1279 setOnlyReadsMemory(F, 1);
1280 setOnlyReadsMemory(F, 2);
1281 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001282 case LibFunc::readlink:
1283 if (FTy->getNumParams() < 2 ||
1284 !FTy->getParamType(0)->isPointerTy() ||
1285 !FTy->getParamType(1)->isPointerTy())
1286 return false;
1287 setDoesNotThrow(F);
1288 setDoesNotCapture(F, 1);
1289 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001290 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001291 break;
1292 case LibFunc::write:
1293 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1294 return false;
1295 // May throw; "write" is a valid pthread cancellation point.
1296 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001297 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001298 break;
1299 case LibFunc::bcopy:
1300 if (FTy->getNumParams() != 3 ||
1301 !FTy->getParamType(0)->isPointerTy() ||
1302 !FTy->getParamType(1)->isPointerTy())
1303 return false;
1304 setDoesNotThrow(F);
1305 setDoesNotCapture(F, 1);
1306 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001307 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001308 break;
1309 case LibFunc::bcmp:
1310 if (FTy->getNumParams() != 3 ||
1311 !FTy->getParamType(0)->isPointerTy() ||
1312 !FTy->getParamType(1)->isPointerTy())
1313 return false;
1314 setDoesNotThrow(F);
1315 setOnlyReadsMemory(F);
1316 setDoesNotCapture(F, 1);
1317 setDoesNotCapture(F, 2);
1318 break;
1319 case LibFunc::bzero:
1320 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1321 return false;
1322 setDoesNotThrow(F);
1323 setDoesNotCapture(F, 1);
1324 break;
1325 case LibFunc::calloc:
1326 if (FTy->getNumParams() != 2 ||
1327 !FTy->getReturnType()->isPointerTy())
1328 return false;
1329 setDoesNotThrow(F);
1330 setDoesNotAlias(F, 0);
1331 break;
1332 case LibFunc::chmod:
1333 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001334 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1335 return false;
1336 setDoesNotThrow(F);
1337 setDoesNotCapture(F, 1);
1338 setOnlyReadsMemory(F, 1);
1339 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001340 case LibFunc::ctermid:
1341 case LibFunc::clearerr:
1342 case LibFunc::closedir:
1343 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1344 return false;
1345 setDoesNotThrow(F);
1346 setDoesNotCapture(F, 1);
1347 break;
1348 case LibFunc::atoi:
1349 case LibFunc::atol:
1350 case LibFunc::atof:
1351 case LibFunc::atoll:
1352 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1353 return false;
1354 setDoesNotThrow(F);
1355 setOnlyReadsMemory(F);
1356 setDoesNotCapture(F, 1);
1357 break;
1358 case LibFunc::access:
1359 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1360 return false;
1361 setDoesNotThrow(F);
1362 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001363 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001364 break;
1365 case LibFunc::fopen:
1366 if (FTy->getNumParams() != 2 ||
1367 !FTy->getReturnType()->isPointerTy() ||
1368 !FTy->getParamType(0)->isPointerTy() ||
1369 !FTy->getParamType(1)->isPointerTy())
1370 return false;
1371 setDoesNotThrow(F);
1372 setDoesNotAlias(F, 0);
1373 setDoesNotCapture(F, 1);
1374 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001375 setOnlyReadsMemory(F, 1);
1376 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001377 break;
1378 case LibFunc::fdopen:
1379 if (FTy->getNumParams() != 2 ||
1380 !FTy->getReturnType()->isPointerTy() ||
1381 !FTy->getParamType(1)->isPointerTy())
1382 return false;
1383 setDoesNotThrow(F);
1384 setDoesNotAlias(F, 0);
1385 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001386 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001387 break;
1388 case LibFunc::feof:
1389 case LibFunc::free:
1390 case LibFunc::fseek:
1391 case LibFunc::ftell:
1392 case LibFunc::fgetc:
1393 case LibFunc::fseeko:
1394 case LibFunc::ftello:
1395 case LibFunc::fileno:
1396 case LibFunc::fflush:
1397 case LibFunc::fclose:
1398 case LibFunc::fsetpos:
1399 case LibFunc::flockfile:
1400 case LibFunc::funlockfile:
1401 case LibFunc::ftrylockfile:
1402 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1403 return false;
1404 setDoesNotThrow(F);
1405 setDoesNotCapture(F, 1);
1406 break;
1407 case LibFunc::ferror:
1408 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1409 return false;
1410 setDoesNotThrow(F);
1411 setDoesNotCapture(F, 1);
1412 setOnlyReadsMemory(F);
1413 break;
1414 case LibFunc::fputc:
1415 case LibFunc::fstat:
1416 case LibFunc::frexp:
1417 case LibFunc::frexpf:
1418 case LibFunc::frexpl:
1419 case LibFunc::fstatvfs:
1420 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1421 return false;
1422 setDoesNotThrow(F);
1423 setDoesNotCapture(F, 2);
1424 break;
1425 case LibFunc::fgets:
1426 if (FTy->getNumParams() != 3 ||
1427 !FTy->getParamType(0)->isPointerTy() ||
1428 !FTy->getParamType(2)->isPointerTy())
1429 return false;
1430 setDoesNotThrow(F);
1431 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001432 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001433 case LibFunc::fread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001434 if (FTy->getNumParams() != 4 ||
1435 !FTy->getParamType(0)->isPointerTy() ||
1436 !FTy->getParamType(3)->isPointerTy())
1437 return false;
1438 setDoesNotThrow(F);
1439 setDoesNotCapture(F, 1);
1440 setDoesNotCapture(F, 4);
1441 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001442 case LibFunc::fwrite:
1443 if (FTy->getNumParams() != 4 ||
1444 !FTy->getParamType(0)->isPointerTy() ||
1445 !FTy->getParamType(3)->isPointerTy())
1446 return false;
1447 setDoesNotThrow(F);
1448 setDoesNotCapture(F, 1);
1449 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001450 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001451 case LibFunc::fputs:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001452 if (FTy->getNumParams() < 2 ||
1453 !FTy->getParamType(0)->isPointerTy() ||
1454 !FTy->getParamType(1)->isPointerTy())
1455 return false;
1456 setDoesNotThrow(F);
1457 setDoesNotCapture(F, 1);
1458 setDoesNotCapture(F, 2);
1459 setOnlyReadsMemory(F, 1);
1460 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001461 case LibFunc::fscanf:
1462 case LibFunc::fprintf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001463 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 setOnlyReadsMemory(F, 2);
1471 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001472 case LibFunc::fgetpos:
1473 if (FTy->getNumParams() < 2 ||
1474 !FTy->getParamType(0)->isPointerTy() ||
1475 !FTy->getParamType(1)->isPointerTy())
1476 return false;
1477 setDoesNotThrow(F);
1478 setDoesNotCapture(F, 1);
1479 setDoesNotCapture(F, 2);
1480 break;
1481 case LibFunc::getc:
1482 case LibFunc::getlogin_r:
1483 case LibFunc::getc_unlocked:
1484 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1485 return false;
1486 setDoesNotThrow(F);
1487 setDoesNotCapture(F, 1);
1488 break;
1489 case LibFunc::getenv:
1490 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1491 return false;
1492 setDoesNotThrow(F);
1493 setOnlyReadsMemory(F);
1494 setDoesNotCapture(F, 1);
1495 break;
1496 case LibFunc::gets:
1497 case LibFunc::getchar:
1498 setDoesNotThrow(F);
1499 break;
1500 case LibFunc::getitimer:
1501 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1502 return false;
1503 setDoesNotThrow(F);
1504 setDoesNotCapture(F, 2);
1505 break;
1506 case LibFunc::getpwnam:
1507 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1508 return false;
1509 setDoesNotThrow(F);
1510 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001511 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001512 break;
1513 case LibFunc::ungetc:
1514 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1515 return false;
1516 setDoesNotThrow(F);
1517 setDoesNotCapture(F, 2);
1518 break;
1519 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001520 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1521 return false;
1522 setDoesNotThrow(F);
1523 setDoesNotCapture(F, 1);
1524 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001525 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001526 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1527 return false;
1528 setDoesNotThrow(F);
1529 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001530 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001531 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001532 case LibFunc::unsetenv:
1533 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1534 return false;
1535 setDoesNotThrow(F);
1536 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001537 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001538 break;
1539 case LibFunc::utime:
1540 case LibFunc::utimes:
1541 if (FTy->getNumParams() != 2 ||
1542 !FTy->getParamType(0)->isPointerTy() ||
1543 !FTy->getParamType(1)->isPointerTy())
1544 return false;
1545 setDoesNotThrow(F);
1546 setDoesNotCapture(F, 1);
1547 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001548 setOnlyReadsMemory(F, 1);
1549 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001550 break;
1551 case LibFunc::putc:
1552 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1553 return false;
1554 setDoesNotThrow(F);
1555 setDoesNotCapture(F, 2);
1556 break;
1557 case LibFunc::puts:
1558 case LibFunc::printf:
1559 case LibFunc::perror:
1560 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1561 return false;
1562 setDoesNotThrow(F);
1563 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001564 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001565 break;
1566 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001567 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1568 return false;
1569 // May throw; "pread" is a valid pthread cancellation point.
1570 setDoesNotCapture(F, 2);
1571 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001572 case LibFunc::pwrite:
1573 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1574 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001575 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001576 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001577 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001578 break;
1579 case LibFunc::putchar:
1580 setDoesNotThrow(F);
1581 break;
1582 case LibFunc::popen:
1583 if (FTy->getNumParams() != 2 ||
1584 !FTy->getReturnType()->isPointerTy() ||
1585 !FTy->getParamType(0)->isPointerTy() ||
1586 !FTy->getParamType(1)->isPointerTy())
1587 return false;
1588 setDoesNotThrow(F);
1589 setDoesNotAlias(F, 0);
1590 setDoesNotCapture(F, 1);
1591 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001592 setOnlyReadsMemory(F, 1);
1593 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001594 break;
1595 case LibFunc::pclose:
1596 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1597 return false;
1598 setDoesNotThrow(F);
1599 setDoesNotCapture(F, 1);
1600 break;
1601 case LibFunc::vscanf:
1602 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1603 return false;
1604 setDoesNotThrow(F);
1605 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001606 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001607 break;
1608 case LibFunc::vsscanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001609 if (FTy->getNumParams() != 3 ||
1610 !FTy->getParamType(1)->isPointerTy() ||
1611 !FTy->getParamType(2)->isPointerTy())
1612 return false;
1613 setDoesNotThrow(F);
1614 setDoesNotCapture(F, 1);
1615 setDoesNotCapture(F, 2);
1616 setOnlyReadsMemory(F, 1);
1617 setOnlyReadsMemory(F, 2);
1618 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001619 case LibFunc::vfscanf:
1620 if (FTy->getNumParams() != 3 ||
1621 !FTy->getParamType(1)->isPointerTy() ||
1622 !FTy->getParamType(2)->isPointerTy())
1623 return false;
1624 setDoesNotThrow(F);
1625 setDoesNotCapture(F, 1);
1626 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001627 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001628 break;
1629 case LibFunc::valloc:
1630 if (!FTy->getReturnType()->isPointerTy())
1631 return false;
1632 setDoesNotThrow(F);
1633 setDoesNotAlias(F, 0);
1634 break;
1635 case LibFunc::vprintf:
1636 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1637 return false;
1638 setDoesNotThrow(F);
1639 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001640 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001641 break;
1642 case LibFunc::vfprintf:
1643 case LibFunc::vsprintf:
1644 if (FTy->getNumParams() != 3 ||
1645 !FTy->getParamType(0)->isPointerTy() ||
1646 !FTy->getParamType(1)->isPointerTy())
1647 return false;
1648 setDoesNotThrow(F);
1649 setDoesNotCapture(F, 1);
1650 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001651 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001652 break;
1653 case LibFunc::vsnprintf:
1654 if (FTy->getNumParams() != 4 ||
1655 !FTy->getParamType(0)->isPointerTy() ||
1656 !FTy->getParamType(2)->isPointerTy())
1657 return false;
1658 setDoesNotThrow(F);
1659 setDoesNotCapture(F, 1);
1660 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001661 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001662 break;
1663 case LibFunc::open:
1664 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1665 return false;
1666 // May throw; "open" is a valid pthread cancellation point.
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::opendir:
1671 if (FTy->getNumParams() != 1 ||
1672 !FTy->getReturnType()->isPointerTy() ||
1673 !FTy->getParamType(0)->isPointerTy())
1674 return false;
1675 setDoesNotThrow(F);
1676 setDoesNotAlias(F, 0);
1677 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001678 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001679 break;
1680 case LibFunc::tmpfile:
1681 if (!FTy->getReturnType()->isPointerTy())
1682 return false;
1683 setDoesNotThrow(F);
1684 setDoesNotAlias(F, 0);
1685 break;
1686 case LibFunc::times:
1687 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1688 return false;
1689 setDoesNotThrow(F);
1690 setDoesNotCapture(F, 1);
1691 break;
1692 case LibFunc::htonl:
1693 case LibFunc::htons:
1694 case LibFunc::ntohl:
1695 case LibFunc::ntohs:
1696 setDoesNotThrow(F);
1697 setDoesNotAccessMemory(F);
1698 break;
1699 case LibFunc::lstat:
1700 if (FTy->getNumParams() != 2 ||
1701 !FTy->getParamType(0)->isPointerTy() ||
1702 !FTy->getParamType(1)->isPointerTy())
1703 return false;
1704 setDoesNotThrow(F);
1705 setDoesNotCapture(F, 1);
1706 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001707 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001708 break;
1709 case LibFunc::lchown:
1710 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1711 return false;
1712 setDoesNotThrow(F);
1713 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001714 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001715 break;
1716 case LibFunc::qsort:
1717 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1718 return false;
1719 // May throw; places call through function pointer.
1720 setDoesNotCapture(F, 4);
1721 break;
1722 case LibFunc::dunder_strdup:
1723 case LibFunc::dunder_strndup:
1724 if (FTy->getNumParams() < 1 ||
1725 !FTy->getReturnType()->isPointerTy() ||
1726 !FTy->getParamType(0)->isPointerTy())
1727 return false;
1728 setDoesNotThrow(F);
1729 setDoesNotAlias(F, 0);
1730 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001731 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001732 break;
1733 case LibFunc::dunder_strtok_r:
1734 if (FTy->getNumParams() != 3 ||
1735 !FTy->getParamType(1)->isPointerTy())
1736 return false;
1737 setDoesNotThrow(F);
1738 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001739 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001740 break;
1741 case LibFunc::under_IO_getc:
1742 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1743 return false;
1744 setDoesNotThrow(F);
1745 setDoesNotCapture(F, 1);
1746 break;
1747 case LibFunc::under_IO_putc:
1748 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1749 return false;
1750 setDoesNotThrow(F);
1751 setDoesNotCapture(F, 2);
1752 break;
1753 case LibFunc::dunder_isoc99_scanf:
1754 if (FTy->getNumParams() < 1 ||
1755 !FTy->getParamType(0)->isPointerTy())
1756 return false;
1757 setDoesNotThrow(F);
1758 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001759 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001760 break;
1761 case LibFunc::stat64:
1762 case LibFunc::lstat64:
1763 case LibFunc::statvfs64:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001764 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);
1771 setOnlyReadsMemory(F, 1);
1772 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001773 case LibFunc::dunder_isoc99_sscanf:
1774 if (FTy->getNumParams() < 1 ||
1775 !FTy->getParamType(0)->isPointerTy() ||
1776 !FTy->getParamType(1)->isPointerTy())
1777 return false;
1778 setDoesNotThrow(F);
1779 setDoesNotCapture(F, 1);
1780 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001781 setOnlyReadsMemory(F, 1);
1782 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001783 break;
1784 case LibFunc::fopen64:
1785 if (FTy->getNumParams() != 2 ||
1786 !FTy->getReturnType()->isPointerTy() ||
1787 !FTy->getParamType(0)->isPointerTy() ||
1788 !FTy->getParamType(1)->isPointerTy())
1789 return false;
1790 setDoesNotThrow(F);
1791 setDoesNotAlias(F, 0);
1792 setDoesNotCapture(F, 1);
1793 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001794 setOnlyReadsMemory(F, 1);
1795 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001796 break;
1797 case LibFunc::fseeko64:
1798 case LibFunc::ftello64:
1799 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1800 return false;
1801 setDoesNotThrow(F);
1802 setDoesNotCapture(F, 1);
1803 break;
1804 case LibFunc::tmpfile64:
1805 if (!FTy->getReturnType()->isPointerTy())
1806 return false;
1807 setDoesNotThrow(F);
1808 setDoesNotAlias(F, 0);
1809 break;
1810 case LibFunc::fstat64:
1811 case LibFunc::fstatvfs64:
1812 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1813 return false;
1814 setDoesNotThrow(F);
1815 setDoesNotCapture(F, 2);
1816 break;
1817 case LibFunc::open64:
1818 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1819 return false;
1820 // May throw; "open" is a valid pthread cancellation point.
1821 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001822 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001823 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001824 case LibFunc::gettimeofday:
1825 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1826 !FTy->getParamType(1)->isPointerTy())
1827 return false;
1828 // Currently some platforms have the restrict keyword on the arguments to
1829 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1830 // arguments.
1831 setDoesNotThrow(F);
1832 setDoesNotCapture(F, 1);
1833 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001834 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001835 default:
1836 // Didn't mark any attributes.
1837 return false;
1838 }
1839
1840 return true;
1841}
1842
1843/// annotateLibraryCalls - Adds attributes to well-known standard library
1844/// call declarations.
1845bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1846 bool MadeChange = false;
1847
1848 // Check each function in turn annotating well-known library function
1849 // declarations with attributes.
1850 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1851 Function *F = (*I)->getFunction();
1852
Craig Topperf40110f2014-04-25 05:29:35 +00001853 if (F && F->isDeclaration())
Meador Inge6b6a1612013-03-21 00:55:59 +00001854 MadeChange |= inferPrototypeAttributes(*F);
1855 }
1856
1857 return MadeChange;
1858}
1859
Chris Lattner4422d312010-04-16 22:42:17 +00001860bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001861 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohman86449d72010-11-08 16:10:15 +00001862
Meador Inge6b6a1612013-03-21 00:55:59 +00001863 bool Changed = annotateLibraryCalls(SCC);
1864 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001865 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001866 Changed |= AddNoAliasAttrs(SCC);
Philip Reamesa88caea2015-08-31 19:44:38 +00001867 Changed |= AddNonNullAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001868 return Changed;
1869}