blob: 80050220c8bf1bffc47bb8460c2db03e9463e536 [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 {
Chandler Carruthc518ebd2015-10-29 18:29:15 +000055typedef SmallSetVector<Function *, 8> SCCNodeSet;
56}
57
58namespace {
Chandler Carruth63559d72015-09-13 06:47:20 +000059struct FunctionAttrs : public CallGraphSCCPass {
60 static char ID; // Pass identification, replacement for typeid
61 FunctionAttrs() : CallGraphSCCPass(ID) {
62 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
63 }
64
Chandler Carruth63559d72015-09-13 06:47:20 +000065 bool runOnSCC(CallGraphSCC &SCC) override;
66
Chandler Carruth63559d72015-09-13 06:47:20 +000067 void getAnalysisUsage(AnalysisUsage &AU) const override {
68 AU.setPreservesCFG();
69 AU.addRequired<AssumptionCacheTracker>();
70 AU.addRequired<TargetLibraryInfoWrapperPass>();
71 CallGraphSCCPass::getAnalysisUsage(AU);
72 }
Meador Inge6b6a1612013-03-21 00:55:59 +000073
Chandler Carruth63559d72015-09-13 06:47:20 +000074private:
75 TargetLibraryInfo *TLI;
76};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000077}
Duncan Sands44c8cd92008-12-31 16:14:43 +000078
79char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000080INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +000081 "Deduce function attributes", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +000082INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +000083INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000084INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +000085INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +000086 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +000087
88Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
89
Chandler Carruth7542d372015-09-21 17:39:41 +000090namespace {
91/// The three kinds of memory access relevant to 'readonly' and
92/// 'readnone' attributes.
93enum MemoryAccessKind {
94 MAK_ReadNone = 0,
95 MAK_ReadOnly = 1,
96 MAK_MayWrite = 2
97};
98}
99
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000100static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR,
101 const SCCNodeSet &SCCNodes) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000102 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
103 if (MRB == FMRB_DoesNotAccessMemory)
104 // Already perfect!
105 return MAK_ReadNone;
106
107 // Definitions with weak linkage may be overridden at linktime with
108 // something that writes memory, so treat them like declarations.
109 if (F.isDeclaration() || F.mayBeOverridden()) {
110 if (AliasAnalysis::onlyReadsMemory(MRB))
111 return MAK_ReadOnly;
112
113 // Conservatively assume it writes to memory.
114 return MAK_MayWrite;
115 }
116
117 // Scan the function body for instructions that may read or write memory.
118 bool ReadsMemory = false;
119 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
120 Instruction *I = &*II;
121
122 // Some instructions can be ignored even if they read or write memory.
123 // Detect these now, skipping to the next instruction if one is found.
124 CallSite CS(cast<Value>(I));
125 if (CS) {
126 // Ignore calls to functions in the same SCC.
127 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
128 continue;
129 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
Chandler Carruth7542d372015-09-21 17:39:41 +0000130
Chandler Carruth69798fb2015-10-27 01:41:43 +0000131 // If the call doesn't access memory, we're done.
132 if (!(MRB & MRI_ModRef))
133 continue;
134
135 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
136 // The call could access any memory. If that includes writes, give up.
137 if (MRB & MRI_Mod)
138 return MAK_MayWrite;
139 // If it reads, note it.
140 if (MRB & MRI_Ref)
141 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000142 continue;
143 }
Chandler Carruth69798fb2015-10-27 01:41:43 +0000144
145 // Check whether all pointer arguments point to local memory, and
146 // ignore calls that only access local memory.
147 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
148 CI != CE; ++CI) {
149 Value *Arg = *CI;
150 if (!Arg->getType()->isPointerTy())
151 continue;
152
153 AAMDNodes AAInfo;
154 I->getAAMetadata(AAInfo);
155 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
156
157 // Skip accesses to local or constant memory as they don't impact the
158 // externally visible mod/ref behavior.
159 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
160 continue;
161
162 if (MRB & MRI_Mod)
163 // Writes non-local memory. Give up.
164 return MAK_MayWrite;
165 if (MRB & MRI_Ref)
166 // Ok, it reads non-local memory.
167 ReadsMemory = true;
168 }
Chandler Carruth7542d372015-09-21 17:39:41 +0000169 continue;
170 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
171 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
172 if (!LI->isVolatile()) {
173 MemoryLocation Loc = MemoryLocation::get(LI);
174 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
175 continue;
176 }
177 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
178 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
179 if (!SI->isVolatile()) {
180 MemoryLocation Loc = MemoryLocation::get(SI);
181 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
182 continue;
183 }
184 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
185 // Ignore vaargs on local memory.
186 MemoryLocation Loc = MemoryLocation::get(VI);
187 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
188 continue;
189 }
190
191 // Any remaining instructions need to be taken seriously! Check if they
192 // read or write memory.
193 if (I->mayWriteToMemory())
194 // Writes memory. Just give up.
195 return MAK_MayWrite;
196
197 // If this instruction may read memory, remember that.
198 ReadsMemory |= I->mayReadFromMemory();
199 }
200
201 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
202}
203
Chandler Carrutha632fb92015-09-13 06:57:25 +0000204/// Deduce readonly/readnone attributes for the SCC.
Chandler Carrutha8125352015-10-30 16:48:08 +0000205template <typename AARGetterT>
206static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000207 // Check if any of the functions in the SCC read or write memory. If they
208 // write memory then they can't be marked readnone or readonly.
209 bool ReadsMemory = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000210 for (Function *F : SCCNodes) {
Chandler Carrutha8125352015-10-30 16:48:08 +0000211 // Call the callable parameter to look up AA results for this function.
212 AAResults &AAR = AARGetter(*F);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000213
Chandler Carruth7542d372015-09-21 17:39:41 +0000214 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
215 case MAK_MayWrite:
216 return false;
217 case MAK_ReadOnly:
Duncan Sands44c8cd92008-12-31 16:14:43 +0000218 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000219 break;
220 case MAK_ReadNone:
221 // Nothing to do!
222 break;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000223 }
224 }
225
226 // Success! Functions in this SCC do not access memory, or only read memory.
227 // Give them the appropriate attribute.
228 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000229 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000230 if (F->doesNotAccessMemory())
231 // Already perfect!
232 continue;
233
234 if (F->onlyReadsMemory() && ReadsMemory)
235 // No change.
236 continue;
237
238 MadeChange = true;
239
240 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000241 AttrBuilder B;
Chandler Carruth63559d72015-09-13 06:47:20 +0000242 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
243 F->removeAttributes(
244 AttributeSet::FunctionIndex,
245 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000246
247 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000248 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000249 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000250
251 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000252 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000253 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000254 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000255 }
256
257 return MadeChange;
258}
259
Nick Lewycky4c378a42011-12-28 23:24:21 +0000260namespace {
Chandler Carrutha632fb92015-09-13 06:57:25 +0000261/// For a given pointer Argument, this retains a list of Arguments of functions
262/// in the same SCC that the pointer data flows into. We use this to build an
263/// SCC of the arguments.
Chandler Carruth63559d72015-09-13 06:47:20 +0000264struct ArgumentGraphNode {
265 Argument *Definition;
266 SmallVector<ArgumentGraphNode *, 4> Uses;
267};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000268
Chandler Carruth63559d72015-09-13 06:47:20 +0000269class ArgumentGraph {
270 // We store pointers to ArgumentGraphNode objects, so it's important that
271 // that they not move around upon insert.
272 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000273
Chandler Carruth63559d72015-09-13 06:47:20 +0000274 ArgumentMapTy ArgumentMap;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000275
Chandler Carruth63559d72015-09-13 06:47:20 +0000276 // There is no root node for the argument graph, in fact:
277 // void f(int *x, int *y) { if (...) f(x, y); }
278 // is an example where the graph is disconnected. The SCCIterator requires a
279 // single entry point, so we maintain a fake ("synthetic") root node that
280 // uses every node. Because the graph is directed and nothing points into
281 // the root, it will not participate in any SCCs (except for its own).
282 ArgumentGraphNode SyntheticRoot;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000283
Chandler Carruth63559d72015-09-13 06:47:20 +0000284public:
285 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000286
Chandler Carruth63559d72015-09-13 06:47:20 +0000287 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000288
Chandler Carruth63559d72015-09-13 06:47:20 +0000289 iterator begin() { return SyntheticRoot.Uses.begin(); }
290 iterator end() { return SyntheticRoot.Uses.end(); }
291 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000292
Chandler Carruth63559d72015-09-13 06:47:20 +0000293 ArgumentGraphNode *operator[](Argument *A) {
294 ArgumentGraphNode &Node = ArgumentMap[A];
295 Node.Definition = A;
296 SyntheticRoot.Uses.push_back(&Node);
297 return &Node;
298 }
299};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000300
Chandler Carrutha632fb92015-09-13 06:57:25 +0000301/// This tracker checks whether callees are in the SCC, and if so it does not
302/// consider that a capture, instead adding it to the "Uses" list and
303/// continuing with the analysis.
Chandler Carruth63559d72015-09-13 06:47:20 +0000304struct ArgumentUsesTracker : public CaptureTracker {
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000305 ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
Nick Lewycky4c378a42011-12-28 23:24:21 +0000306 : Captured(false), SCCNodes(SCCNodes) {}
307
Chandler Carruth63559d72015-09-13 06:47:20 +0000308 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000309
Chandler Carruth63559d72015-09-13 06:47:20 +0000310 bool captured(const Use *U) override {
311 CallSite CS(U->getUser());
312 if (!CS.getInstruction()) {
313 Captured = true;
314 return true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000315 }
316
Chandler Carruth63559d72015-09-13 06:47:20 +0000317 Function *F = CS.getCalledFunction();
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000318 if (!F || F->isDeclaration() || F->mayBeOverridden() ||
319 !SCCNodes.count(F)) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000320 Captured = true;
321 return true;
322 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000323
Chandler Carruth63559d72015-09-13 06:47:20 +0000324 bool Found = false;
325 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
326 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
327 PI != PE; ++PI, ++AI) {
328 if (AI == AE) {
329 assert(F->isVarArg() && "More params than args in non-varargs call");
330 Captured = true;
331 return true;
332 }
333 if (PI == U) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000334 Uses.push_back(&*AI);
Chandler Carruth63559d72015-09-13 06:47:20 +0000335 Found = true;
336 break;
337 }
338 }
339 assert(Found && "Capturing call-site captured nothing?");
340 (void)Found;
341 return false;
342 }
343
344 bool Captured; // True only if certainly captured (used outside our SCC).
345 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
346
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000347 const SCCNodeSet &SCCNodes;
Chandler Carruth63559d72015-09-13 06:47:20 +0000348};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000349}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000350
351namespace llvm {
Chandler Carruth63559d72015-09-13 06:47:20 +0000352template <> struct GraphTraits<ArgumentGraphNode *> {
353 typedef ArgumentGraphNode NodeType;
354 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000355
Chandler Carruth63559d72015-09-13 06:47:20 +0000356 static inline NodeType *getEntryNode(NodeType *A) { return A; }
357 static inline ChildIteratorType child_begin(NodeType *N) {
358 return N->Uses.begin();
359 }
360 static inline ChildIteratorType child_end(NodeType *N) {
361 return N->Uses.end();
362 }
363};
364template <>
365struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
366 static NodeType *getEntryNode(ArgumentGraph *AG) {
367 return AG->getEntryNode();
368 }
369 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
370 return AG->begin();
371 }
372 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
373};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000374}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000375
Chandler Carrutha632fb92015-09-13 06:57:25 +0000376/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000377static Attribute::AttrKind
378determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000379 const SmallPtrSet<Argument *, 8> &SCCNodes) {
380
381 SmallVector<Use *, 32> Worklist;
382 SmallSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000383
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000384 // inalloca arguments are always clobbered by the call.
385 if (A->hasInAllocaAttr())
386 return Attribute::None;
387
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000388 bool IsRead = false;
389 // We don't need to track IsWritten. If A is written to, return immediately.
390
Chandler Carruthcdf47882014-03-09 03:16:01 +0000391 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000392 Visited.insert(&U);
393 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000394 }
395
396 while (!Worklist.empty()) {
397 Use *U = Worklist.pop_back_val();
398 Instruction *I = cast<Instruction>(U->getUser());
399 Value *V = U->get();
400
401 switch (I->getOpcode()) {
402 case Instruction::BitCast:
403 case Instruction::GetElementPtr:
404 case Instruction::PHI:
405 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000406 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000407 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000408 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000409 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000410 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000411 break;
412
413 case Instruction::Call:
414 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000415 bool Captures = true;
416
417 if (I->getType()->isVoidTy())
418 Captures = false;
419
420 auto AddUsersToWorklistIfCapturing = [&] {
421 if (Captures)
422 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000423 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000424 Worklist.push_back(&UU);
425 };
426
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000427 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000428 if (CS.doesNotAccessMemory()) {
429 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000430 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000431 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000432
433 Function *F = CS.getCalledFunction();
434 if (!F) {
435 if (CS.onlyReadsMemory()) {
436 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000437 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000438 continue;
439 }
440 return Attribute::None;
441 }
442
443 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
444 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
445 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
446 if (A->get() == V) {
447 if (AI == AE) {
448 assert(F->isVarArg() &&
449 "More params than args in non-varargs call.");
450 return Attribute::None;
451 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000452 Captures &= !CS.doesNotCapture(A - B);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000453 if (SCCNodes.count(&*AI))
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000454 continue;
455 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
456 return Attribute::None;
457 if (!CS.doesNotAccessMemory(A - B))
458 IsRead = true;
459 }
460 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000461 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000462 break;
463 }
464
465 case Instruction::Load:
466 IsRead = true;
467 break;
468
469 case Instruction::ICmp:
470 case Instruction::Ret:
471 break;
472
473 default:
474 return Attribute::None;
475 }
476 }
477
478 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
479}
480
Chandler Carrutha632fb92015-09-13 06:57:25 +0000481/// Deduce nocapture attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000482static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000483 bool Changed = false;
484
Nick Lewycky4c378a42011-12-28 23:24:21 +0000485 ArgumentGraph AG;
486
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000487 AttrBuilder B;
488 B.addAttribute(Attribute::NoCapture);
489
Duncan Sands44c8cd92008-12-31 16:14:43 +0000490 // Check each function in turn, determining which pointer arguments are not
491 // captured.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000492 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000493 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000494 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000495 if (F->isDeclaration() || F->mayBeOverridden())
496 continue;
497
Nick Lewycky4c378a42011-12-28 23:24:21 +0000498 // Functions that are readonly (or readnone) and nounwind and don't return
499 // a value can't capture arguments. Don't analyze them.
500 if (F->onlyReadsMemory() && F->doesNotThrow() &&
501 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000502 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
503 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000504 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
505 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
506 ++NumNoCapture;
507 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000508 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000509 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000510 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000511 }
512
Chandler Carruth63559d72015-09-13 06:47:20 +0000513 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
514 ++A) {
515 if (!A->getType()->isPointerTy())
516 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000517 bool HasNonLocalUses = false;
518 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000519 ArgumentUsesTracker Tracker(SCCNodes);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000520 PointerMayBeCaptured(&*A, &Tracker);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000521 if (!Tracker.Captured) {
522 if (Tracker.Uses.empty()) {
523 // If it's trivially not captured, mark it nocapture now.
Chandler Carruth63559d72015-09-13 06:47:20 +0000524 A->addAttr(
525 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000526 ++NumNoCapture;
527 Changed = true;
528 } else {
529 // If it's not trivially captured and not trivially not captured,
530 // then it must be calling into another function in our SCC. Save
531 // its particulars for Argument-SCC analysis later.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000532 ArgumentGraphNode *Node = AG[&*A];
Chandler Carruth63559d72015-09-13 06:47:20 +0000533 for (SmallVectorImpl<Argument *>::iterator
534 UI = Tracker.Uses.begin(),
535 UE = Tracker.Uses.end();
536 UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000537 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000538 if (*UI != A)
539 HasNonLocalUses = true;
540 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000541 }
542 }
543 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
544 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000545 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
546 // Can we determine that it's readonly/readnone without doing an SCC?
547 // Note that we don't allow any calls at all here, or else our result
548 // will be dependent on the iteration order through the functions in the
549 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000550 SmallPtrSet<Argument *, 8> Self;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000551 Self.insert(&*A);
552 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000553 if (R != Attribute::None) {
554 AttrBuilder B;
555 B.addAttribute(R);
556 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
557 Changed = true;
558 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
559 }
560 }
561 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000562 }
563
564 // The graph we've collected is partial because we stopped scanning for
565 // argument uses once we solved the argument trivially. These partial nodes
566 // show up as ArgumentGraphNode objects with an empty Uses list, and for
567 // these nodes the final decision about whether they capture has already been
568 // made. If the definition doesn't have a 'nocapture' attribute by now, it
569 // captures.
570
Chandler Carruth63559d72015-09-13 06:47:20 +0000571 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000572 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000573 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000574 if (!ArgumentSCC[0]->Definition)
575 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000576
577 // eg. "void f(int* x) { if (...) f(x); }"
578 if (ArgumentSCC[0]->Uses.size() == 1 &&
579 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000580 Argument *A = ArgumentSCC[0]->Definition;
581 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000582 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000583 Changed = true;
584 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000585 continue;
586 }
587
588 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000589 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
590 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000591 ArgumentGraphNode *Node = *I;
592 if (Node->Uses.empty()) {
593 if (!Node->Definition->hasNoCaptureAttr())
594 SCCCaptured = true;
595 }
596 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000597 if (SCCCaptured)
598 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000599
Chandler Carruth63559d72015-09-13 06:47:20 +0000600 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000601 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
602 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000603 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000604 ArgumentSCCNodes.insert((*I)->Definition);
605 }
606
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000607 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
608 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000609 ArgumentGraphNode *N = *I;
Chandler Carruth63559d72015-09-13 06:47:20 +0000610 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
611 UE = N->Uses.end();
612 UI != UE; ++UI) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000613 Argument *A = (*UI)->Definition;
614 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
615 continue;
616 SCCCaptured = true;
617 break;
618 }
619 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000620 if (SCCCaptured)
621 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000622
Nick Lewyckyf740db32012-01-05 22:21:45 +0000623 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000624 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000625 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000626 ++NumNoCapture;
627 Changed = true;
628 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000629
630 // We also want to compute readonly/readnone. With a small number of false
631 // negatives, we can assume that any pointer which is captured isn't going
632 // to be provably readonly or readnone, since by definition we can't
633 // analyze all uses of a captured pointer.
634 //
635 // The false negatives happen when the pointer is captured by a function
636 // that promises readonly/readnone behaviour on the pointer, then the
637 // pointer's lifetime ends before anything that writes to arbitrary memory.
638 // Also, a readonly/readnone pointer may be returned, but returning a
639 // pointer is capturing it.
640
641 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
642 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
643 Argument *A = ArgumentSCC[i]->Definition;
644 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
645 if (K == Attribute::ReadNone)
646 continue;
647 if (K == Attribute::ReadOnly) {
648 ReadAttr = Attribute::ReadOnly;
649 continue;
650 }
651 ReadAttr = K;
652 break;
653 }
654
655 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000656 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000657 B.addAttribute(ReadAttr);
Chandler Carruth63559d72015-09-13 06:47:20 +0000658 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000659 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
660 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000661 // Clear out existing readonly/readnone attributes
662 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000663 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
664 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
665 Changed = true;
666 }
667 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000668 }
669
670 return Changed;
671}
672
Chandler Carrutha632fb92015-09-13 06:57:25 +0000673/// Tests whether a function is "malloc-like".
674///
675/// A function is "malloc-like" if it returns either null or a pointer that
676/// doesn't alias any other pointer visible to the caller.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000677static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000678 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000679 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
680 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
681 FlowsToReturn.insert(Ret->getReturnValue());
682
683 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000684 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000685
686 if (Constant *C = dyn_cast<Constant>(RetVal)) {
687 if (!C->isNullValue() && !isa<UndefValue>(C))
688 return false;
689
690 continue;
691 }
692
693 if (isa<Argument>(RetVal))
694 return false;
695
696 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
697 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000698 // Extend the analysis by looking upwards.
699 case Instruction::BitCast:
700 case Instruction::GetElementPtr:
701 case Instruction::AddrSpaceCast:
702 FlowsToReturn.insert(RVI->getOperand(0));
703 continue;
704 case Instruction::Select: {
705 SelectInst *SI = cast<SelectInst>(RVI);
706 FlowsToReturn.insert(SI->getTrueValue());
707 FlowsToReturn.insert(SI->getFalseValue());
708 continue;
709 }
710 case Instruction::PHI: {
711 PHINode *PN = cast<PHINode>(RVI);
712 for (Value *IncValue : PN->incoming_values())
713 FlowsToReturn.insert(IncValue);
714 continue;
715 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000716
Chandler Carruth63559d72015-09-13 06:47:20 +0000717 // Check whether the pointer came from an allocation.
718 case Instruction::Alloca:
719 break;
720 case Instruction::Call:
721 case Instruction::Invoke: {
722 CallSite CS(RVI);
723 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000724 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000725 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
726 break;
727 } // fall-through
728 default:
729 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000730 }
731
Dan Gohman94e61762009-11-19 21:57:48 +0000732 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000733 return false;
734 }
735
736 return true;
737}
738
Chandler Carrutha632fb92015-09-13 06:57:25 +0000739/// Deduce noalias attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000740static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000741 // Check each function in turn, determining which functions return noalias
742 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000743 for (Function *F : SCCNodes) {
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000744 // Already noalias.
745 if (F->doesNotAlias(0))
746 continue;
747
748 // Definitions with weak linkage may be overridden at linktime, so
749 // treat them like declarations.
750 if (F->isDeclaration() || F->mayBeOverridden())
751 return false;
752
Chandler Carruth63559d72015-09-13 06:47:20 +0000753 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000754 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000755 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000756 continue;
757
Chandler Carruth3824f852015-09-13 08:23:27 +0000758 if (!isFunctionMallocLike(F, SCCNodes))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000759 return false;
760 }
761
762 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000763 for (Function *F : SCCNodes) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000764 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000765 continue;
766
767 F->setDoesNotAlias(0);
768 ++NumNoAlias;
769 MadeChange = true;
770 }
771
772 return MadeChange;
773}
774
Chandler Carrutha632fb92015-09-13 06:57:25 +0000775/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000776///
777/// Requires that the function returns a pointer.
778///
779/// Returns true if it believes the function will not return a null, and sets
780/// \p Speculative based on whether the returned conclusion is a speculative
781/// conclusion due to SCC calls.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000782static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
Chandler Carruth8874b782015-09-13 08:17:14 +0000783 const TargetLibraryInfo &TLI, bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000784 assert(F->getReturnType()->isPointerTy() &&
785 "nonnull only meaningful on pointer types");
786 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000787
Philip Reamesa88caea2015-08-31 19:44:38 +0000788 SmallSetVector<Value *, 8> FlowsToReturn;
789 for (BasicBlock &BB : *F)
790 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
791 FlowsToReturn.insert(Ret->getReturnValue());
792
793 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
794 Value *RetVal = FlowsToReturn[i];
795
796 // If this value is locally known to be non-null, we're good
Chandler Carruth8874b782015-09-13 08:17:14 +0000797 if (isKnownNonNull(RetVal, &TLI))
Philip Reamesa88caea2015-08-31 19:44:38 +0000798 continue;
799
800 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000801 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000802 Instruction *RVI = dyn_cast<Instruction>(RetVal);
803 if (!RVI)
804 return false;
805 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000806 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000807 case Instruction::BitCast:
808 case Instruction::GetElementPtr:
809 case Instruction::AddrSpaceCast:
810 FlowsToReturn.insert(RVI->getOperand(0));
811 continue;
812 case Instruction::Select: {
813 SelectInst *SI = cast<SelectInst>(RVI);
814 FlowsToReturn.insert(SI->getTrueValue());
815 FlowsToReturn.insert(SI->getFalseValue());
816 continue;
817 }
818 case Instruction::PHI: {
819 PHINode *PN = cast<PHINode>(RVI);
820 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
821 FlowsToReturn.insert(PN->getIncomingValue(i));
822 continue;
823 }
824 case Instruction::Call:
825 case Instruction::Invoke: {
826 CallSite CS(RVI);
827 Function *Callee = CS.getCalledFunction();
828 // A call to a node within the SCC is assumed to return null until
829 // proven otherwise
830 if (Callee && SCCNodes.count(Callee)) {
831 Speculative = true;
832 continue;
833 }
834 return false;
835 }
836 default:
Chandler Carruth63559d72015-09-13 06:47:20 +0000837 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +0000838 };
839 llvm_unreachable("should have either continued or returned");
840 }
841
842 return true;
843}
844
Chandler Carrutha632fb92015-09-13 06:57:25 +0000845/// Deduce nonnull attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000846static bool addNonNullAttrs(const SCCNodeSet &SCCNodes,
847 const TargetLibraryInfo &TLI) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000848 // Speculative that all functions in the SCC return only nonnull
849 // pointers. We may refute this as we analyze functions.
850 bool SCCReturnsNonNull = true;
851
852 bool MadeChange = false;
853
854 // Check each function in turn, determining which functions return nonnull
855 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000856 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000857 // Already nonnull.
858 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
859 Attribute::NonNull))
860 continue;
861
862 // Definitions with weak linkage may be overridden at linktime, so
863 // treat them like declarations.
864 if (F->isDeclaration() || F->mayBeOverridden())
865 return false;
866
Chandler Carruth63559d72015-09-13 06:47:20 +0000867 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +0000868 // pointer types.
869 if (!F->getReturnType()->isPointerTy())
870 continue;
871
872 bool Speculative = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000873 if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000874 if (!Speculative) {
875 // Mark the function eagerly since we may discover a function
876 // which prevents us from speculating about the entire SCC
877 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
878 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
879 ++NumNonNullReturn;
880 MadeChange = true;
881 }
882 continue;
883 }
884 // At least one function returns something which could be null, can't
885 // speculate any more.
886 SCCReturnsNonNull = false;
887 }
888
889 if (SCCReturnsNonNull) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000890 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000891 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
892 Attribute::NonNull) ||
893 !F->getReturnType()->isPointerTy())
894 continue;
895
896 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
897 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
898 ++NumNonNullReturn;
899 MadeChange = true;
900 }
901 }
902
903 return MadeChange;
904}
905
Chandler Carruthd0245202015-09-13 07:50:43 +0000906static void setDoesNotAccessMemory(Function &F) {
907 if (!F.doesNotAccessMemory()) {
908 F.setDoesNotAccessMemory();
909 ++NumAnnotated;
910 }
911}
912
913static void setOnlyReadsMemory(Function &F) {
914 if (!F.onlyReadsMemory()) {
915 F.setOnlyReadsMemory();
916 ++NumAnnotated;
917 }
918}
919
920static void setDoesNotThrow(Function &F) {
921 if (!F.doesNotThrow()) {
922 F.setDoesNotThrow();
923 ++NumAnnotated;
924 }
925}
926
927static void setDoesNotCapture(Function &F, unsigned n) {
928 if (!F.doesNotCapture(n)) {
929 F.setDoesNotCapture(n);
930 ++NumAnnotated;
931 }
932}
933
934static void setOnlyReadsMemory(Function &F, unsigned n) {
935 if (!F.onlyReadsMemory(n)) {
936 F.setOnlyReadsMemory(n);
937 ++NumAnnotated;
938 }
939}
940
941static void setDoesNotAlias(Function &F, unsigned n) {
942 if (!F.doesNotAlias(n)) {
943 F.setDoesNotAlias(n);
944 ++NumAnnotated;
945 }
946}
947
Chandler Carrutha632fb92015-09-13 06:57:25 +0000948/// Analyze the name and prototype of the given function and set any applicable
949/// attributes.
950///
951/// Returns true if any attributes were set and false otherwise.
Chandler Carruth444d0052015-09-13 08:03:23 +0000952static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
Chandler Carruth0fb99812014-08-13 10:49:33 +0000953 if (F.hasFnAttribute(Attribute::OptimizeNone))
954 return false;
955
Meador Inge6b6a1612013-03-21 00:55:59 +0000956 FunctionType *FTy = F.getFunctionType();
957 LibFunc::Func TheLibFunc;
Chandler Carruth444d0052015-09-13 08:03:23 +0000958 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
Meador Inge6b6a1612013-03-21 00:55:59 +0000959 return false;
960
961 switch (TheLibFunc) {
962 case LibFunc::strlen:
963 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
964 return false;
965 setOnlyReadsMemory(F);
966 setDoesNotThrow(F);
967 setDoesNotCapture(F, 1);
968 break;
969 case LibFunc::strchr:
970 case LibFunc::strrchr:
Chandler Carruth63559d72015-09-13 06:47:20 +0000971 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +0000972 !FTy->getParamType(1)->isIntegerTy())
973 return false;
974 setOnlyReadsMemory(F);
975 setDoesNotThrow(F);
976 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000977 case LibFunc::strtol:
978 case LibFunc::strtod:
979 case LibFunc::strtof:
980 case LibFunc::strtoul:
981 case LibFunc::strtoll:
982 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +0000983 case LibFunc::strtoull:
Chandler Carruth63559d72015-09-13 06:47:20 +0000984 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +0000985 return false;
986 setDoesNotThrow(F);
987 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000988 setOnlyReadsMemory(F, 1);
989 break;
990 case LibFunc::strcpy:
991 case LibFunc::stpcpy:
992 case LibFunc::strcat:
993 case LibFunc::strncat:
994 case LibFunc::strncpy:
995 case LibFunc::stpncpy:
Chandler Carruth63559d72015-09-13 06:47:20 +0000996 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000997 return false;
998 setDoesNotThrow(F);
999 setDoesNotCapture(F, 2);
1000 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001001 break;
1002 case LibFunc::strxfrm:
Chandler Carruth63559d72015-09-13 06:47:20 +00001003 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001004 !FTy->getParamType(1)->isPointerTy())
1005 return false;
1006 setDoesNotThrow(F);
1007 setDoesNotCapture(F, 1);
1008 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001009 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001010 break;
Chandler Carruth63559d72015-09-13 06:47:20 +00001011 case LibFunc::strcmp: // 0,1
1012 case LibFunc::strspn: // 0,1
1013 case LibFunc::strncmp: // 0,1
1014 case LibFunc::strcspn: // 0,1
1015 case LibFunc::strcoll: // 0,1
1016 case LibFunc::strcasecmp: // 0,1
1017 case LibFunc::strncasecmp: //
1018 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001019 !FTy->getParamType(1)->isPointerTy())
1020 return false;
1021 setOnlyReadsMemory(F);
1022 setDoesNotThrow(F);
1023 setDoesNotCapture(F, 1);
1024 setDoesNotCapture(F, 2);
1025 break;
1026 case LibFunc::strstr:
1027 case LibFunc::strpbrk:
1028 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1029 return false;
1030 setOnlyReadsMemory(F);
1031 setDoesNotThrow(F);
1032 setDoesNotCapture(F, 2);
1033 break;
1034 case LibFunc::strtok:
1035 case LibFunc::strtok_r:
1036 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1037 return false;
1038 setDoesNotThrow(F);
1039 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001040 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001041 break;
1042 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001043 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1044 return false;
1045 setDoesNotThrow(F);
1046 setDoesNotCapture(F, 1);
1047 setOnlyReadsMemory(F, 1);
1048 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001049 case LibFunc::setbuf:
1050 case LibFunc::setvbuf:
1051 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1052 return false;
1053 setDoesNotThrow(F);
1054 setDoesNotCapture(F, 1);
1055 break;
1056 case LibFunc::strdup:
1057 case LibFunc::strndup:
1058 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1059 !FTy->getParamType(0)->isPointerTy())
1060 return false;
1061 setDoesNotThrow(F);
1062 setDoesNotAlias(F, 0);
1063 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001064 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001065 break;
1066 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +00001067 case LibFunc::statvfs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001068 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001069 !FTy->getParamType(1)->isPointerTy())
1070 return false;
1071 setDoesNotThrow(F);
1072 setDoesNotCapture(F, 1);
1073 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001074 setOnlyReadsMemory(F, 1);
1075 break;
1076 case LibFunc::sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001077 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001078 !FTy->getParamType(1)->isPointerTy())
1079 return false;
1080 setDoesNotThrow(F);
1081 setDoesNotCapture(F, 1);
1082 setDoesNotCapture(F, 2);
1083 setOnlyReadsMemory(F, 1);
1084 setOnlyReadsMemory(F, 2);
1085 break;
1086 case LibFunc::sprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001087 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001088 !FTy->getParamType(1)->isPointerTy())
1089 return false;
1090 setDoesNotThrow(F);
1091 setDoesNotCapture(F, 1);
1092 setDoesNotCapture(F, 2);
1093 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001094 break;
1095 case LibFunc::snprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001096 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001097 !FTy->getParamType(2)->isPointerTy())
1098 return false;
1099 setDoesNotThrow(F);
1100 setDoesNotCapture(F, 1);
1101 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001102 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001103 break;
1104 case LibFunc::setitimer:
Chandler Carruth63559d72015-09-13 06:47:20 +00001105 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001106 !FTy->getParamType(2)->isPointerTy())
1107 return false;
1108 setDoesNotThrow(F);
1109 setDoesNotCapture(F, 2);
1110 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001111 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001112 break;
1113 case LibFunc::system:
Chandler Carruth63559d72015-09-13 06:47:20 +00001114 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001115 return false;
1116 // May throw; "system" is a valid pthread cancellation point.
1117 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001118 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001119 break;
1120 case LibFunc::malloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001121 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001122 return false;
1123 setDoesNotThrow(F);
1124 setDoesNotAlias(F, 0);
1125 break;
1126 case LibFunc::memcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001127 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001128 !FTy->getParamType(1)->isPointerTy())
1129 return false;
1130 setOnlyReadsMemory(F);
1131 setDoesNotThrow(F);
1132 setDoesNotCapture(F, 1);
1133 setDoesNotCapture(F, 2);
1134 break;
1135 case LibFunc::memchr:
1136 case LibFunc::memrchr:
1137 if (FTy->getNumParams() != 3)
1138 return false;
1139 setOnlyReadsMemory(F);
1140 setDoesNotThrow(F);
1141 break;
1142 case LibFunc::modf:
1143 case LibFunc::modff:
1144 case LibFunc::modfl:
Chandler Carruth63559d72015-09-13 06:47:20 +00001145 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001146 return false;
1147 setDoesNotThrow(F);
1148 setDoesNotCapture(F, 2);
1149 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001150 case LibFunc::memcpy:
1151 case LibFunc::memccpy:
1152 case LibFunc::memmove:
Chandler Carruth63559d72015-09-13 06:47:20 +00001153 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001154 return false;
1155 setDoesNotThrow(F);
1156 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001157 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001158 break;
1159 case LibFunc::memalign:
1160 if (!FTy->getReturnType()->isPointerTy())
1161 return false;
1162 setDoesNotAlias(F, 0);
1163 break;
1164 case LibFunc::mkdir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001165 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001166 return false;
1167 setDoesNotThrow(F);
1168 setDoesNotCapture(F, 1);
1169 setOnlyReadsMemory(F, 1);
1170 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001171 case LibFunc::mktime:
Chandler Carruth63559d72015-09-13 06:47:20 +00001172 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001173 return false;
1174 setDoesNotThrow(F);
1175 setDoesNotCapture(F, 1);
1176 break;
1177 case LibFunc::realloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001178 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001179 !FTy->getReturnType()->isPointerTy())
1180 return false;
1181 setDoesNotThrow(F);
1182 setDoesNotAlias(F, 0);
1183 setDoesNotCapture(F, 1);
1184 break;
1185 case LibFunc::read:
Chandler Carruth63559d72015-09-13 06:47:20 +00001186 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001187 return false;
1188 // May throw; "read" is a valid pthread cancellation point.
1189 setDoesNotCapture(F, 2);
1190 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001191 case LibFunc::rewind:
Chandler Carruth63559d72015-09-13 06:47:20 +00001192 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001193 return false;
1194 setDoesNotThrow(F);
1195 setDoesNotCapture(F, 1);
1196 break;
1197 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001198 case LibFunc::remove:
1199 case LibFunc::realpath:
Chandler Carruth63559d72015-09-13 06:47:20 +00001200 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001201 return false;
1202 setDoesNotThrow(F);
1203 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001204 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001205 break;
1206 case LibFunc::rename:
Chandler Carruth63559d72015-09-13 06:47:20 +00001207 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001208 !FTy->getParamType(1)->isPointerTy())
1209 return false;
1210 setDoesNotThrow(F);
1211 setDoesNotCapture(F, 1);
1212 setDoesNotCapture(F, 2);
1213 setOnlyReadsMemory(F, 1);
1214 setOnlyReadsMemory(F, 2);
1215 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001216 case LibFunc::readlink:
Chandler Carruth63559d72015-09-13 06:47:20 +00001217 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001218 !FTy->getParamType(1)->isPointerTy())
1219 return false;
1220 setDoesNotThrow(F);
1221 setDoesNotCapture(F, 1);
1222 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001223 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001224 break;
1225 case LibFunc::write:
1226 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1227 return false;
1228 // May throw; "write" is a valid pthread cancellation point.
1229 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001230 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001231 break;
1232 case LibFunc::bcopy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001233 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001234 !FTy->getParamType(1)->isPointerTy())
1235 return false;
1236 setDoesNotThrow(F);
1237 setDoesNotCapture(F, 1);
1238 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001239 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001240 break;
1241 case LibFunc::bcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001242 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001243 !FTy->getParamType(1)->isPointerTy())
1244 return false;
1245 setDoesNotThrow(F);
1246 setOnlyReadsMemory(F);
1247 setDoesNotCapture(F, 1);
1248 setDoesNotCapture(F, 2);
1249 break;
1250 case LibFunc::bzero:
1251 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1252 return false;
1253 setDoesNotThrow(F);
1254 setDoesNotCapture(F, 1);
1255 break;
1256 case LibFunc::calloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001257 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001258 return false;
1259 setDoesNotThrow(F);
1260 setDoesNotAlias(F, 0);
1261 break;
1262 case LibFunc::chmod:
1263 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001264 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1265 return false;
1266 setDoesNotThrow(F);
1267 setDoesNotCapture(F, 1);
1268 setOnlyReadsMemory(F, 1);
1269 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001270 case LibFunc::ctermid:
1271 case LibFunc::clearerr:
1272 case LibFunc::closedir:
1273 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1274 return false;
1275 setDoesNotThrow(F);
1276 setDoesNotCapture(F, 1);
1277 break;
1278 case LibFunc::atoi:
1279 case LibFunc::atol:
1280 case LibFunc::atof:
1281 case LibFunc::atoll:
1282 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1283 return false;
1284 setDoesNotThrow(F);
1285 setOnlyReadsMemory(F);
1286 setDoesNotCapture(F, 1);
1287 break;
1288 case LibFunc::access:
1289 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1290 return false;
1291 setDoesNotThrow(F);
1292 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001293 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001294 break;
1295 case LibFunc::fopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001296 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001297 !FTy->getParamType(0)->isPointerTy() ||
1298 !FTy->getParamType(1)->isPointerTy())
1299 return false;
1300 setDoesNotThrow(F);
1301 setDoesNotAlias(F, 0);
1302 setDoesNotCapture(F, 1);
1303 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001304 setOnlyReadsMemory(F, 1);
1305 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001306 break;
1307 case LibFunc::fdopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001308 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001309 !FTy->getParamType(1)->isPointerTy())
1310 return false;
1311 setDoesNotThrow(F);
1312 setDoesNotAlias(F, 0);
1313 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001314 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001315 break;
1316 case LibFunc::feof:
1317 case LibFunc::free:
1318 case LibFunc::fseek:
1319 case LibFunc::ftell:
1320 case LibFunc::fgetc:
1321 case LibFunc::fseeko:
1322 case LibFunc::ftello:
1323 case LibFunc::fileno:
1324 case LibFunc::fflush:
1325 case LibFunc::fclose:
1326 case LibFunc::fsetpos:
1327 case LibFunc::flockfile:
1328 case LibFunc::funlockfile:
1329 case LibFunc::ftrylockfile:
1330 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1331 return false;
1332 setDoesNotThrow(F);
1333 setDoesNotCapture(F, 1);
1334 break;
1335 case LibFunc::ferror:
1336 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1337 return false;
1338 setDoesNotThrow(F);
1339 setDoesNotCapture(F, 1);
1340 setOnlyReadsMemory(F);
1341 break;
1342 case LibFunc::fputc:
1343 case LibFunc::fstat:
1344 case LibFunc::frexp:
1345 case LibFunc::frexpf:
1346 case LibFunc::frexpl:
1347 case LibFunc::fstatvfs:
1348 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1349 return false;
1350 setDoesNotThrow(F);
1351 setDoesNotCapture(F, 2);
1352 break;
1353 case LibFunc::fgets:
Chandler Carruth63559d72015-09-13 06:47:20 +00001354 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001355 !FTy->getParamType(2)->isPointerTy())
1356 return false;
1357 setDoesNotThrow(F);
1358 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001359 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001360 case LibFunc::fread:
Chandler Carruth63559d72015-09-13 06:47:20 +00001361 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001362 !FTy->getParamType(3)->isPointerTy())
1363 return false;
1364 setDoesNotThrow(F);
1365 setDoesNotCapture(F, 1);
1366 setDoesNotCapture(F, 4);
1367 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001368 case LibFunc::fwrite:
Chandler Carruth63559d72015-09-13 06:47:20 +00001369 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001370 !FTy->getParamType(3)->isPointerTy())
1371 return false;
1372 setDoesNotThrow(F);
1373 setDoesNotCapture(F, 1);
1374 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001375 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001376 case LibFunc::fputs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001377 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001378 !FTy->getParamType(1)->isPointerTy())
1379 return false;
1380 setDoesNotThrow(F);
1381 setDoesNotCapture(F, 1);
1382 setDoesNotCapture(F, 2);
1383 setOnlyReadsMemory(F, 1);
1384 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001385 case LibFunc::fscanf:
1386 case LibFunc::fprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001387 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001388 !FTy->getParamType(1)->isPointerTy())
1389 return false;
1390 setDoesNotThrow(F);
1391 setDoesNotCapture(F, 1);
1392 setDoesNotCapture(F, 2);
1393 setOnlyReadsMemory(F, 2);
1394 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001395 case LibFunc::fgetpos:
Chandler Carruth63559d72015-09-13 06:47:20 +00001396 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001397 !FTy->getParamType(1)->isPointerTy())
1398 return false;
1399 setDoesNotThrow(F);
1400 setDoesNotCapture(F, 1);
1401 setDoesNotCapture(F, 2);
1402 break;
1403 case LibFunc::getc:
1404 case LibFunc::getlogin_r:
1405 case LibFunc::getc_unlocked:
1406 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1407 return false;
1408 setDoesNotThrow(F);
1409 setDoesNotCapture(F, 1);
1410 break;
1411 case LibFunc::getenv:
1412 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1413 return false;
1414 setDoesNotThrow(F);
1415 setOnlyReadsMemory(F);
1416 setDoesNotCapture(F, 1);
1417 break;
1418 case LibFunc::gets:
1419 case LibFunc::getchar:
1420 setDoesNotThrow(F);
1421 break;
1422 case LibFunc::getitimer:
1423 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1424 return false;
1425 setDoesNotThrow(F);
1426 setDoesNotCapture(F, 2);
1427 break;
1428 case LibFunc::getpwnam:
1429 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1430 return false;
1431 setDoesNotThrow(F);
1432 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001433 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001434 break;
1435 case LibFunc::ungetc:
1436 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1437 return false;
1438 setDoesNotThrow(F);
1439 setDoesNotCapture(F, 2);
1440 break;
1441 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001442 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1443 return false;
1444 setDoesNotThrow(F);
1445 setDoesNotCapture(F, 1);
1446 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001447 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001448 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1449 return false;
1450 setDoesNotThrow(F);
1451 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001452 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001453 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001454 case LibFunc::unsetenv:
1455 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1456 return false;
1457 setDoesNotThrow(F);
1458 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001459 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001460 break;
1461 case LibFunc::utime:
1462 case LibFunc::utimes:
Chandler Carruth63559d72015-09-13 06:47:20 +00001463 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001464 !FTy->getParamType(1)->isPointerTy())
1465 return false;
1466 setDoesNotThrow(F);
1467 setDoesNotCapture(F, 1);
1468 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001469 setOnlyReadsMemory(F, 1);
1470 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001471 break;
1472 case LibFunc::putc:
1473 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1474 return false;
1475 setDoesNotThrow(F);
1476 setDoesNotCapture(F, 2);
1477 break;
1478 case LibFunc::puts:
1479 case LibFunc::printf:
1480 case LibFunc::perror:
1481 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1482 return false;
1483 setDoesNotThrow(F);
1484 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001485 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001486 break;
1487 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001488 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1489 return false;
1490 // May throw; "pread" is a valid pthread cancellation point.
1491 setDoesNotCapture(F, 2);
1492 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001493 case LibFunc::pwrite:
1494 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1495 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001496 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001497 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001498 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001499 break;
1500 case LibFunc::putchar:
1501 setDoesNotThrow(F);
1502 break;
1503 case LibFunc::popen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001504 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001505 !FTy->getParamType(0)->isPointerTy() ||
1506 !FTy->getParamType(1)->isPointerTy())
1507 return false;
1508 setDoesNotThrow(F);
1509 setDoesNotAlias(F, 0);
1510 setDoesNotCapture(F, 1);
1511 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001512 setOnlyReadsMemory(F, 1);
1513 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001514 break;
1515 case LibFunc::pclose:
1516 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1517 return false;
1518 setDoesNotThrow(F);
1519 setDoesNotCapture(F, 1);
1520 break;
1521 case LibFunc::vscanf:
1522 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1523 return false;
1524 setDoesNotThrow(F);
1525 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001526 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001527 break;
1528 case LibFunc::vsscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001529 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001530 !FTy->getParamType(2)->isPointerTy())
1531 return false;
1532 setDoesNotThrow(F);
1533 setDoesNotCapture(F, 1);
1534 setDoesNotCapture(F, 2);
1535 setOnlyReadsMemory(F, 1);
1536 setOnlyReadsMemory(F, 2);
1537 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001538 case LibFunc::vfscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001539 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001540 !FTy->getParamType(2)->isPointerTy())
1541 return false;
1542 setDoesNotThrow(F);
1543 setDoesNotCapture(F, 1);
1544 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001545 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001546 break;
1547 case LibFunc::valloc:
1548 if (!FTy->getReturnType()->isPointerTy())
1549 return false;
1550 setDoesNotThrow(F);
1551 setDoesNotAlias(F, 0);
1552 break;
1553 case LibFunc::vprintf:
1554 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1555 return false;
1556 setDoesNotThrow(F);
1557 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001558 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001559 break;
1560 case LibFunc::vfprintf:
1561 case LibFunc::vsprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001562 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001563 !FTy->getParamType(1)->isPointerTy())
1564 return false;
1565 setDoesNotThrow(F);
1566 setDoesNotCapture(F, 1);
1567 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001568 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001569 break;
1570 case LibFunc::vsnprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001571 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001572 !FTy->getParamType(2)->isPointerTy())
1573 return false;
1574 setDoesNotThrow(F);
1575 setDoesNotCapture(F, 1);
1576 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001577 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001578 break;
1579 case LibFunc::open:
1580 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1581 return false;
1582 // May throw; "open" is a valid pthread cancellation point.
1583 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001584 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001585 break;
1586 case LibFunc::opendir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001587 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001588 !FTy->getParamType(0)->isPointerTy())
1589 return false;
1590 setDoesNotThrow(F);
1591 setDoesNotAlias(F, 0);
1592 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001593 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001594 break;
1595 case LibFunc::tmpfile:
1596 if (!FTy->getReturnType()->isPointerTy())
1597 return false;
1598 setDoesNotThrow(F);
1599 setDoesNotAlias(F, 0);
1600 break;
1601 case LibFunc::times:
1602 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1603 return false;
1604 setDoesNotThrow(F);
1605 setDoesNotCapture(F, 1);
1606 break;
1607 case LibFunc::htonl:
1608 case LibFunc::htons:
1609 case LibFunc::ntohl:
1610 case LibFunc::ntohs:
1611 setDoesNotThrow(F);
1612 setDoesNotAccessMemory(F);
1613 break;
1614 case LibFunc::lstat:
Chandler Carruth63559d72015-09-13 06:47:20 +00001615 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001616 !FTy->getParamType(1)->isPointerTy())
1617 return false;
1618 setDoesNotThrow(F);
1619 setDoesNotCapture(F, 1);
1620 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001621 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001622 break;
1623 case LibFunc::lchown:
1624 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1625 return false;
1626 setDoesNotThrow(F);
1627 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001628 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001629 break;
1630 case LibFunc::qsort:
1631 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1632 return false;
1633 // May throw; places call through function pointer.
1634 setDoesNotCapture(F, 4);
1635 break;
1636 case LibFunc::dunder_strdup:
1637 case LibFunc::dunder_strndup:
Chandler Carruth63559d72015-09-13 06:47:20 +00001638 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001639 !FTy->getParamType(0)->isPointerTy())
1640 return false;
1641 setDoesNotThrow(F);
1642 setDoesNotAlias(F, 0);
1643 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001644 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001645 break;
1646 case LibFunc::dunder_strtok_r:
Chandler Carruth63559d72015-09-13 06:47:20 +00001647 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001648 return false;
1649 setDoesNotThrow(F);
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::under_IO_getc:
1654 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1655 return false;
1656 setDoesNotThrow(F);
1657 setDoesNotCapture(F, 1);
1658 break;
1659 case LibFunc::under_IO_putc:
1660 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1661 return false;
1662 setDoesNotThrow(F);
1663 setDoesNotCapture(F, 2);
1664 break;
1665 case LibFunc::dunder_isoc99_scanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001666 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001667 return false;
1668 setDoesNotThrow(F);
1669 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001670 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001671 break;
1672 case LibFunc::stat64:
1673 case LibFunc::lstat64:
1674 case LibFunc::statvfs64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001675 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001676 !FTy->getParamType(1)->isPointerTy())
1677 return false;
1678 setDoesNotThrow(F);
1679 setDoesNotCapture(F, 1);
1680 setDoesNotCapture(F, 2);
1681 setOnlyReadsMemory(F, 1);
1682 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001683 case LibFunc::dunder_isoc99_sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001684 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001685 !FTy->getParamType(1)->isPointerTy())
1686 return false;
1687 setDoesNotThrow(F);
1688 setDoesNotCapture(F, 1);
1689 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001690 setOnlyReadsMemory(F, 1);
1691 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001692 break;
1693 case LibFunc::fopen64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001694 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001695 !FTy->getParamType(0)->isPointerTy() ||
1696 !FTy->getParamType(1)->isPointerTy())
1697 return false;
1698 setDoesNotThrow(F);
1699 setDoesNotAlias(F, 0);
1700 setDoesNotCapture(F, 1);
1701 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001702 setOnlyReadsMemory(F, 1);
1703 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001704 break;
1705 case LibFunc::fseeko64:
1706 case LibFunc::ftello64:
1707 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1708 return false;
1709 setDoesNotThrow(F);
1710 setDoesNotCapture(F, 1);
1711 break;
1712 case LibFunc::tmpfile64:
1713 if (!FTy->getReturnType()->isPointerTy())
1714 return false;
1715 setDoesNotThrow(F);
1716 setDoesNotAlias(F, 0);
1717 break;
1718 case LibFunc::fstat64:
1719 case LibFunc::fstatvfs64:
1720 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1721 return false;
1722 setDoesNotThrow(F);
1723 setDoesNotCapture(F, 2);
1724 break;
1725 case LibFunc::open64:
1726 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1727 return false;
1728 // May throw; "open" is a valid pthread cancellation point.
1729 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001730 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001731 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001732 case LibFunc::gettimeofday:
1733 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1734 !FTy->getParamType(1)->isPointerTy())
1735 return false;
1736 // Currently some platforms have the restrict keyword on the arguments to
1737 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1738 // arguments.
1739 setDoesNotThrow(F);
1740 setDoesNotCapture(F, 1);
1741 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001742 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001743 default:
1744 // Didn't mark any attributes.
1745 return false;
1746 }
1747
1748 return true;
1749}
1750
Chris Lattner4422d312010-04-16 22:42:17 +00001751bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001752 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruthcada2d82015-10-31 00:28:37 +00001753 bool Changed = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001754
Chandler Carrutha8125352015-10-30 16:48:08 +00001755 // We compute dedicated AA results for each function in the SCC as needed. We
1756 // use a lambda referencing external objects so that they live long enough to
1757 // be queried, but we re-use them each time.
1758 Optional<BasicAAResult> BAR;
1759 Optional<AAResults> AAR;
1760 auto AARGetter = [&](Function &F) -> AAResults & {
1761 BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1762 AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1763 return *AAR;
1764 };
1765
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001766 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1767 // whether a given CallGraphNode is in this SCC. Also track whether there are
1768 // any external or opt-none nodes that will prevent us from optimizing any
1769 // part of the SCC.
1770 SCCNodeSet SCCNodes;
1771 bool ExternalNode = false;
1772 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1773 Function *F = (*I)->getFunction();
1774 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1775 // External node or function we're trying not to optimize - we both avoid
1776 // transform them and avoid leveraging information they provide.
1777 ExternalNode = true;
1778 continue;
1779 }
1780
Chandler Carruthcada2d82015-10-31 00:28:37 +00001781 // When initially processing functions, also infer their prototype
1782 // attributes if they are declarations.
1783 if (F->isDeclaration())
1784 Changed |= inferPrototypeAttributes(*F, *TLI);
1785
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001786 SCCNodes.insert(F);
1787 }
1788
Chandler Carrutha8125352015-10-30 16:48:08 +00001789 Changed |= addReadAttrs(SCCNodes, AARGetter);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001790 Changed |= addArgumentAttrs(SCCNodes);
1791
1792 // If we have no external nodes participating in the SCC, we can infer some
1793 // more precise attributes as well.
1794 if (!ExternalNode) {
1795 Changed |= addNoAliasAttrs(SCCNodes);
1796 Changed |= addNonNullAttrs(SCCNodes, *TLI);
1797 }
1798
Duncan Sands44c8cd92008-12-31 16:14:43 +00001799 return Changed;
1800}