blob: 6024137bfe348a3ce1834854be71c4dfb7c3399f [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
Sanjoy Das98bfe262015-11-05 03:04:40 +0000324 // Note: the callee and the two successor blocks *follow* the argument
325 // operands. This means there is no need to adjust UseIndex to account for
326 // these.
327
328 unsigned UseIndex =
329 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
330
331 assert(UseIndex < CS.arg_size() && "Non-argument use?");
332 if (UseIndex >= F->arg_size()) {
333 assert(F->isVarArg() && "More params than args in non-varargs call");
334 Captured = true;
335 return true;
Chandler Carruth63559d72015-09-13 06:47:20 +0000336 }
Sanjoy Das98bfe262015-11-05 03:04:40 +0000337
Duncan P. N. Exon Smith83c4b682015-11-07 00:01:16 +0000338 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
Chandler Carruth63559d72015-09-13 06:47:20 +0000339 return false;
340 }
341
342 bool Captured; // True only if certainly captured (used outside our SCC).
343 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
344
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000345 const SCCNodeSet &SCCNodes;
Chandler Carruth63559d72015-09-13 06:47:20 +0000346};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000347}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000348
349namespace llvm {
Chandler Carruth63559d72015-09-13 06:47:20 +0000350template <> struct GraphTraits<ArgumentGraphNode *> {
351 typedef ArgumentGraphNode NodeType;
352 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000353
Chandler Carruth63559d72015-09-13 06:47:20 +0000354 static inline NodeType *getEntryNode(NodeType *A) { return A; }
355 static inline ChildIteratorType child_begin(NodeType *N) {
356 return N->Uses.begin();
357 }
358 static inline ChildIteratorType child_end(NodeType *N) {
359 return N->Uses.end();
360 }
361};
362template <>
363struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
364 static NodeType *getEntryNode(ArgumentGraph *AG) {
365 return AG->getEntryNode();
366 }
367 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
368 return AG->begin();
369 }
370 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
371};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000372}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000373
Chandler Carrutha632fb92015-09-13 06:57:25 +0000374/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000375static Attribute::AttrKind
376determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000377 const SmallPtrSet<Argument *, 8> &SCCNodes) {
378
379 SmallVector<Use *, 32> Worklist;
380 SmallSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000381
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000382 // inalloca arguments are always clobbered by the call.
383 if (A->hasInAllocaAttr())
384 return Attribute::None;
385
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000386 bool IsRead = false;
387 // We don't need to track IsWritten. If A is written to, return immediately.
388
Chandler Carruthcdf47882014-03-09 03:16:01 +0000389 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000390 Visited.insert(&U);
391 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000392 }
393
394 while (!Worklist.empty()) {
395 Use *U = Worklist.pop_back_val();
396 Instruction *I = cast<Instruction>(U->getUser());
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000397
398 switch (I->getOpcode()) {
399 case Instruction::BitCast:
400 case Instruction::GetElementPtr:
401 case Instruction::PHI:
402 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000403 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000404 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000405 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000406 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000407 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000408 break;
409
410 case Instruction::Call:
411 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000412 bool Captures = true;
413
414 if (I->getType()->isVoidTy())
415 Captures = false;
416
417 auto AddUsersToWorklistIfCapturing = [&] {
418 if (Captures)
419 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000420 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000421 Worklist.push_back(&UU);
422 };
423
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000424 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000425 if (CS.doesNotAccessMemory()) {
426 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000427 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000428 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000429
430 Function *F = CS.getCalledFunction();
431 if (!F) {
432 if (CS.onlyReadsMemory()) {
433 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000434 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000435 continue;
436 }
437 return Attribute::None;
438 }
439
Sanjoy Das436e2392015-11-07 01:55:53 +0000440 // Note: the callee and the two successor blocks *follow* the argument
441 // operands. This means there is no need to adjust UseIndex to account
442 // for these.
443
444 unsigned UseIndex = std::distance(CS.arg_begin(), U);
445
446 assert(UseIndex < CS.arg_size() && "Non-argument use?");
447 if (UseIndex >= F->arg_size()) {
448 assert(F->isVarArg() && "More params than args in non-varargs call");
449 return Attribute::None;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000450 }
Sanjoy Das436e2392015-11-07 01:55:53 +0000451
452 Captures &= !CS.doesNotCapture(UseIndex);
453 if (!SCCNodes.count(std::next(F->arg_begin(), UseIndex))) {
454 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
455 return Attribute::None;
456 if (!CS.doesNotAccessMemory(UseIndex))
457 IsRead = true;
458 }
459
Nick Lewycky59633cb2014-05-30 02:31:27 +0000460 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000461 break;
462 }
463
464 case Instruction::Load:
465 IsRead = true;
466 break;
467
468 case Instruction::ICmp:
469 case Instruction::Ret:
470 break;
471
472 default:
473 return Attribute::None;
474 }
475 }
476
477 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
478}
479
Chandler Carrutha632fb92015-09-13 06:57:25 +0000480/// Deduce nocapture attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000481static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000482 bool Changed = false;
483
Nick Lewycky4c378a42011-12-28 23:24:21 +0000484 ArgumentGraph AG;
485
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000486 AttrBuilder B;
487 B.addAttribute(Attribute::NoCapture);
488
Duncan Sands44c8cd92008-12-31 16:14:43 +0000489 // Check each function in turn, determining which pointer arguments are not
490 // captured.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000491 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000492 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000493 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000494 if (F->isDeclaration() || F->mayBeOverridden())
495 continue;
496
Nick Lewycky4c378a42011-12-28 23:24:21 +0000497 // Functions that are readonly (or readnone) and nounwind and don't return
498 // a value can't capture arguments. Don't analyze them.
499 if (F->onlyReadsMemory() && F->doesNotThrow() &&
500 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000501 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
502 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000503 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
504 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
505 ++NumNoCapture;
506 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000507 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000508 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000509 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000510 }
511
Chandler Carruth63559d72015-09-13 06:47:20 +0000512 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
513 ++A) {
514 if (!A->getType()->isPointerTy())
515 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000516 bool HasNonLocalUses = false;
517 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000518 ArgumentUsesTracker Tracker(SCCNodes);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000519 PointerMayBeCaptured(&*A, &Tracker);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000520 if (!Tracker.Captured) {
521 if (Tracker.Uses.empty()) {
522 // If it's trivially not captured, mark it nocapture now.
Chandler Carruth63559d72015-09-13 06:47:20 +0000523 A->addAttr(
524 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000525 ++NumNoCapture;
526 Changed = true;
527 } else {
528 // If it's not trivially captured and not trivially not captured,
529 // then it must be calling into another function in our SCC. Save
530 // its particulars for Argument-SCC analysis later.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000531 ArgumentGraphNode *Node = AG[&*A];
Chandler Carruth63559d72015-09-13 06:47:20 +0000532 for (SmallVectorImpl<Argument *>::iterator
533 UI = Tracker.Uses.begin(),
534 UE = Tracker.Uses.end();
535 UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000536 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000537 if (*UI != A)
538 HasNonLocalUses = true;
539 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000540 }
541 }
542 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
543 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000544 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
545 // Can we determine that it's readonly/readnone without doing an SCC?
546 // Note that we don't allow any calls at all here, or else our result
547 // will be dependent on the iteration order through the functions in the
548 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000549 SmallPtrSet<Argument *, 8> Self;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000550 Self.insert(&*A);
551 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000552 if (R != Attribute::None) {
553 AttrBuilder B;
554 B.addAttribute(R);
555 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
556 Changed = true;
557 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
558 }
559 }
560 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000561 }
562
563 // The graph we've collected is partial because we stopped scanning for
564 // argument uses once we solved the argument trivially. These partial nodes
565 // show up as ArgumentGraphNode objects with an empty Uses list, and for
566 // these nodes the final decision about whether they capture has already been
567 // made. If the definition doesn't have a 'nocapture' attribute by now, it
568 // captures.
569
Chandler Carruth63559d72015-09-13 06:47:20 +0000570 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000571 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000572 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000573 if (!ArgumentSCC[0]->Definition)
574 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000575
576 // eg. "void f(int* x) { if (...) f(x); }"
577 if (ArgumentSCC[0]->Uses.size() == 1 &&
578 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000579 Argument *A = ArgumentSCC[0]->Definition;
580 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000581 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000582 Changed = true;
583 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000584 continue;
585 }
586
587 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000588 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
589 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000590 ArgumentGraphNode *Node = *I;
591 if (Node->Uses.empty()) {
592 if (!Node->Definition->hasNoCaptureAttr())
593 SCCCaptured = true;
594 }
595 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000596 if (SCCCaptured)
597 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000598
Chandler Carruth63559d72015-09-13 06:47:20 +0000599 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000600 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
601 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000602 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000603 ArgumentSCCNodes.insert((*I)->Definition);
604 }
605
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000606 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
607 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000608 ArgumentGraphNode *N = *I;
Chandler Carruth63559d72015-09-13 06:47:20 +0000609 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
610 UE = N->Uses.end();
611 UI != UE; ++UI) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000612 Argument *A = (*UI)->Definition;
613 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
614 continue;
615 SCCCaptured = true;
616 break;
617 }
618 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000619 if (SCCCaptured)
620 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000621
Nick Lewyckyf740db32012-01-05 22:21:45 +0000622 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000623 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000624 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000625 ++NumNoCapture;
626 Changed = true;
627 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000628
629 // We also want to compute readonly/readnone. With a small number of false
630 // negatives, we can assume that any pointer which is captured isn't going
631 // to be provably readonly or readnone, since by definition we can't
632 // analyze all uses of a captured pointer.
633 //
634 // The false negatives happen when the pointer is captured by a function
635 // that promises readonly/readnone behaviour on the pointer, then the
636 // pointer's lifetime ends before anything that writes to arbitrary memory.
637 // Also, a readonly/readnone pointer may be returned, but returning a
638 // pointer is capturing it.
639
640 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
641 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
642 Argument *A = ArgumentSCC[i]->Definition;
643 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
644 if (K == Attribute::ReadNone)
645 continue;
646 if (K == Attribute::ReadOnly) {
647 ReadAttr = Attribute::ReadOnly;
648 continue;
649 }
650 ReadAttr = K;
651 break;
652 }
653
654 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000655 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000656 B.addAttribute(ReadAttr);
Chandler Carruth63559d72015-09-13 06:47:20 +0000657 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000658 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
659 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000660 // Clear out existing readonly/readnone attributes
661 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000662 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
663 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
664 Changed = true;
665 }
666 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000667 }
668
669 return Changed;
670}
671
Chandler Carrutha632fb92015-09-13 06:57:25 +0000672/// Tests whether a function is "malloc-like".
673///
674/// A function is "malloc-like" if it returns either null or a pointer that
675/// doesn't alias any other pointer visible to the caller.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000676static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000677 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000678 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
679 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
680 FlowsToReturn.insert(Ret->getReturnValue());
681
682 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000683 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000684
685 if (Constant *C = dyn_cast<Constant>(RetVal)) {
686 if (!C->isNullValue() && !isa<UndefValue>(C))
687 return false;
688
689 continue;
690 }
691
692 if (isa<Argument>(RetVal))
693 return false;
694
695 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
696 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000697 // Extend the analysis by looking upwards.
698 case Instruction::BitCast:
699 case Instruction::GetElementPtr:
700 case Instruction::AddrSpaceCast:
701 FlowsToReturn.insert(RVI->getOperand(0));
702 continue;
703 case Instruction::Select: {
704 SelectInst *SI = cast<SelectInst>(RVI);
705 FlowsToReturn.insert(SI->getTrueValue());
706 FlowsToReturn.insert(SI->getFalseValue());
707 continue;
708 }
709 case Instruction::PHI: {
710 PHINode *PN = cast<PHINode>(RVI);
711 for (Value *IncValue : PN->incoming_values())
712 FlowsToReturn.insert(IncValue);
713 continue;
714 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000715
Chandler Carruth63559d72015-09-13 06:47:20 +0000716 // Check whether the pointer came from an allocation.
717 case Instruction::Alloca:
718 break;
719 case Instruction::Call:
720 case Instruction::Invoke: {
721 CallSite CS(RVI);
722 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000723 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000724 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
725 break;
726 } // fall-through
727 default:
728 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000729 }
730
Dan Gohman94e61762009-11-19 21:57:48 +0000731 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000732 return false;
733 }
734
735 return true;
736}
737
Chandler Carrutha632fb92015-09-13 06:57:25 +0000738/// Deduce noalias attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000739static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000740 // Check each function in turn, determining which functions return noalias
741 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000742 for (Function *F : SCCNodes) {
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000743 // Already noalias.
744 if (F->doesNotAlias(0))
745 continue;
746
747 // Definitions with weak linkage may be overridden at linktime, so
748 // treat them like declarations.
749 if (F->isDeclaration() || F->mayBeOverridden())
750 return false;
751
Chandler Carruth63559d72015-09-13 06:47:20 +0000752 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000753 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000754 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000755 continue;
756
Chandler Carruth3824f852015-09-13 08:23:27 +0000757 if (!isFunctionMallocLike(F, SCCNodes))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000758 return false;
759 }
760
761 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000762 for (Function *F : SCCNodes) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000763 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000764 continue;
765
766 F->setDoesNotAlias(0);
767 ++NumNoAlias;
768 MadeChange = true;
769 }
770
771 return MadeChange;
772}
773
Chandler Carrutha632fb92015-09-13 06:57:25 +0000774/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000775///
776/// Requires that the function returns a pointer.
777///
778/// Returns true if it believes the function will not return a null, and sets
779/// \p Speculative based on whether the returned conclusion is a speculative
780/// conclusion due to SCC calls.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000781static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
Chandler Carruth8874b782015-09-13 08:17:14 +0000782 const TargetLibraryInfo &TLI, bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000783 assert(F->getReturnType()->isPointerTy() &&
784 "nonnull only meaningful on pointer types");
785 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000786
Philip Reamesa88caea2015-08-31 19:44:38 +0000787 SmallSetVector<Value *, 8> FlowsToReturn;
788 for (BasicBlock &BB : *F)
789 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
790 FlowsToReturn.insert(Ret->getReturnValue());
791
792 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
793 Value *RetVal = FlowsToReturn[i];
794
795 // If this value is locally known to be non-null, we're good
Chandler Carruth8874b782015-09-13 08:17:14 +0000796 if (isKnownNonNull(RetVal, &TLI))
Philip Reamesa88caea2015-08-31 19:44:38 +0000797 continue;
798
799 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000800 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000801 Instruction *RVI = dyn_cast<Instruction>(RetVal);
802 if (!RVI)
803 return false;
804 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000805 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000806 case Instruction::BitCast:
807 case Instruction::GetElementPtr:
808 case Instruction::AddrSpaceCast:
809 FlowsToReturn.insert(RVI->getOperand(0));
810 continue;
811 case Instruction::Select: {
812 SelectInst *SI = cast<SelectInst>(RVI);
813 FlowsToReturn.insert(SI->getTrueValue());
814 FlowsToReturn.insert(SI->getFalseValue());
815 continue;
816 }
817 case Instruction::PHI: {
818 PHINode *PN = cast<PHINode>(RVI);
819 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
820 FlowsToReturn.insert(PN->getIncomingValue(i));
821 continue;
822 }
823 case Instruction::Call:
824 case Instruction::Invoke: {
825 CallSite CS(RVI);
826 Function *Callee = CS.getCalledFunction();
827 // A call to a node within the SCC is assumed to return null until
828 // proven otherwise
829 if (Callee && SCCNodes.count(Callee)) {
830 Speculative = true;
831 continue;
832 }
833 return false;
834 }
835 default:
Chandler Carruth63559d72015-09-13 06:47:20 +0000836 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +0000837 };
838 llvm_unreachable("should have either continued or returned");
839 }
840
841 return true;
842}
843
Chandler Carrutha632fb92015-09-13 06:57:25 +0000844/// Deduce nonnull attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000845static bool addNonNullAttrs(const SCCNodeSet &SCCNodes,
846 const TargetLibraryInfo &TLI) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000847 // Speculative that all functions in the SCC return only nonnull
848 // pointers. We may refute this as we analyze functions.
849 bool SCCReturnsNonNull = true;
850
851 bool MadeChange = false;
852
853 // Check each function in turn, determining which functions return nonnull
854 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000855 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000856 // Already nonnull.
857 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
858 Attribute::NonNull))
859 continue;
860
861 // Definitions with weak linkage may be overridden at linktime, so
862 // treat them like declarations.
863 if (F->isDeclaration() || F->mayBeOverridden())
864 return false;
865
Chandler Carruth63559d72015-09-13 06:47:20 +0000866 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +0000867 // pointer types.
868 if (!F->getReturnType()->isPointerTy())
869 continue;
870
871 bool Speculative = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000872 if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000873 if (!Speculative) {
874 // Mark the function eagerly since we may discover a function
875 // which prevents us from speculating about the entire SCC
876 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
877 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
878 ++NumNonNullReturn;
879 MadeChange = true;
880 }
881 continue;
882 }
883 // At least one function returns something which could be null, can't
884 // speculate any more.
885 SCCReturnsNonNull = false;
886 }
887
888 if (SCCReturnsNonNull) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000889 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000890 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
891 Attribute::NonNull) ||
892 !F->getReturnType()->isPointerTy())
893 continue;
894
895 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
896 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
897 ++NumNonNullReturn;
898 MadeChange = true;
899 }
900 }
901
902 return MadeChange;
903}
904
Chandler Carruthd0245202015-09-13 07:50:43 +0000905static void setDoesNotAccessMemory(Function &F) {
906 if (!F.doesNotAccessMemory()) {
907 F.setDoesNotAccessMemory();
908 ++NumAnnotated;
909 }
910}
911
912static void setOnlyReadsMemory(Function &F) {
913 if (!F.onlyReadsMemory()) {
914 F.setOnlyReadsMemory();
915 ++NumAnnotated;
916 }
917}
918
919static void setDoesNotThrow(Function &F) {
920 if (!F.doesNotThrow()) {
921 F.setDoesNotThrow();
922 ++NumAnnotated;
923 }
924}
925
926static void setDoesNotCapture(Function &F, unsigned n) {
927 if (!F.doesNotCapture(n)) {
928 F.setDoesNotCapture(n);
929 ++NumAnnotated;
930 }
931}
932
933static void setOnlyReadsMemory(Function &F, unsigned n) {
934 if (!F.onlyReadsMemory(n)) {
935 F.setOnlyReadsMemory(n);
936 ++NumAnnotated;
937 }
938}
939
940static void setDoesNotAlias(Function &F, unsigned n) {
941 if (!F.doesNotAlias(n)) {
942 F.setDoesNotAlias(n);
943 ++NumAnnotated;
944 }
945}
946
Chandler Carrutha632fb92015-09-13 06:57:25 +0000947/// Analyze the name and prototype of the given function and set any applicable
948/// attributes.
949///
950/// Returns true if any attributes were set and false otherwise.
Chandler Carruth444d0052015-09-13 08:03:23 +0000951static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
Chandler Carruth0fb99812014-08-13 10:49:33 +0000952 if (F.hasFnAttribute(Attribute::OptimizeNone))
953 return false;
954
Meador Inge6b6a1612013-03-21 00:55:59 +0000955 FunctionType *FTy = F.getFunctionType();
956 LibFunc::Func TheLibFunc;
Chandler Carruth444d0052015-09-13 08:03:23 +0000957 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
Meador Inge6b6a1612013-03-21 00:55:59 +0000958 return false;
959
960 switch (TheLibFunc) {
961 case LibFunc::strlen:
962 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
963 return false;
964 setOnlyReadsMemory(F);
965 setDoesNotThrow(F);
966 setDoesNotCapture(F, 1);
967 break;
968 case LibFunc::strchr:
969 case LibFunc::strrchr:
Chandler Carruth63559d72015-09-13 06:47:20 +0000970 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +0000971 !FTy->getParamType(1)->isIntegerTy())
972 return false;
973 setOnlyReadsMemory(F);
974 setDoesNotThrow(F);
975 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000976 case LibFunc::strtol:
977 case LibFunc::strtod:
978 case LibFunc::strtof:
979 case LibFunc::strtoul:
980 case LibFunc::strtoll:
981 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +0000982 case LibFunc::strtoull:
Chandler Carruth63559d72015-09-13 06:47:20 +0000983 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +0000984 return false;
985 setDoesNotThrow(F);
986 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000987 setOnlyReadsMemory(F, 1);
988 break;
989 case LibFunc::strcpy:
990 case LibFunc::stpcpy:
991 case LibFunc::strcat:
992 case LibFunc::strncat:
993 case LibFunc::strncpy:
994 case LibFunc::stpncpy:
Chandler Carruth63559d72015-09-13 06:47:20 +0000995 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000996 return false;
997 setDoesNotThrow(F);
998 setDoesNotCapture(F, 2);
999 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001000 break;
1001 case LibFunc::strxfrm:
Chandler Carruth63559d72015-09-13 06:47:20 +00001002 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001003 !FTy->getParamType(1)->isPointerTy())
1004 return false;
1005 setDoesNotThrow(F);
1006 setDoesNotCapture(F, 1);
1007 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001008 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001009 break;
Chandler Carruth63559d72015-09-13 06:47:20 +00001010 case LibFunc::strcmp: // 0,1
1011 case LibFunc::strspn: // 0,1
1012 case LibFunc::strncmp: // 0,1
1013 case LibFunc::strcspn: // 0,1
1014 case LibFunc::strcoll: // 0,1
1015 case LibFunc::strcasecmp: // 0,1
1016 case LibFunc::strncasecmp: //
1017 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001018 !FTy->getParamType(1)->isPointerTy())
1019 return false;
1020 setOnlyReadsMemory(F);
1021 setDoesNotThrow(F);
1022 setDoesNotCapture(F, 1);
1023 setDoesNotCapture(F, 2);
1024 break;
1025 case LibFunc::strstr:
1026 case LibFunc::strpbrk:
1027 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1028 return false;
1029 setOnlyReadsMemory(F);
1030 setDoesNotThrow(F);
1031 setDoesNotCapture(F, 2);
1032 break;
1033 case LibFunc::strtok:
1034 case LibFunc::strtok_r:
1035 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1036 return false;
1037 setDoesNotThrow(F);
1038 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001039 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001040 break;
1041 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001042 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1043 return false;
1044 setDoesNotThrow(F);
1045 setDoesNotCapture(F, 1);
1046 setOnlyReadsMemory(F, 1);
1047 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001048 case LibFunc::setbuf:
1049 case LibFunc::setvbuf:
1050 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1051 return false;
1052 setDoesNotThrow(F);
1053 setDoesNotCapture(F, 1);
1054 break;
1055 case LibFunc::strdup:
1056 case LibFunc::strndup:
1057 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1058 !FTy->getParamType(0)->isPointerTy())
1059 return false;
1060 setDoesNotThrow(F);
1061 setDoesNotAlias(F, 0);
1062 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001063 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001064 break;
1065 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +00001066 case LibFunc::statvfs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001067 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001068 !FTy->getParamType(1)->isPointerTy())
1069 return false;
1070 setDoesNotThrow(F);
1071 setDoesNotCapture(F, 1);
1072 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001073 setOnlyReadsMemory(F, 1);
1074 break;
1075 case LibFunc::sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001076 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001077 !FTy->getParamType(1)->isPointerTy())
1078 return false;
1079 setDoesNotThrow(F);
1080 setDoesNotCapture(F, 1);
1081 setDoesNotCapture(F, 2);
1082 setOnlyReadsMemory(F, 1);
1083 setOnlyReadsMemory(F, 2);
1084 break;
1085 case LibFunc::sprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001086 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001087 !FTy->getParamType(1)->isPointerTy())
1088 return false;
1089 setDoesNotThrow(F);
1090 setDoesNotCapture(F, 1);
1091 setDoesNotCapture(F, 2);
1092 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001093 break;
1094 case LibFunc::snprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001095 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001096 !FTy->getParamType(2)->isPointerTy())
1097 return false;
1098 setDoesNotThrow(F);
1099 setDoesNotCapture(F, 1);
1100 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001101 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001102 break;
1103 case LibFunc::setitimer:
Chandler Carruth63559d72015-09-13 06:47:20 +00001104 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001105 !FTy->getParamType(2)->isPointerTy())
1106 return false;
1107 setDoesNotThrow(F);
1108 setDoesNotCapture(F, 2);
1109 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001110 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001111 break;
1112 case LibFunc::system:
Chandler Carruth63559d72015-09-13 06:47:20 +00001113 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001114 return false;
1115 // May throw; "system" is a valid pthread cancellation point.
1116 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001117 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001118 break;
1119 case LibFunc::malloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001120 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001121 return false;
1122 setDoesNotThrow(F);
1123 setDoesNotAlias(F, 0);
1124 break;
1125 case LibFunc::memcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001126 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001127 !FTy->getParamType(1)->isPointerTy())
1128 return false;
1129 setOnlyReadsMemory(F);
1130 setDoesNotThrow(F);
1131 setDoesNotCapture(F, 1);
1132 setDoesNotCapture(F, 2);
1133 break;
1134 case LibFunc::memchr:
1135 case LibFunc::memrchr:
1136 if (FTy->getNumParams() != 3)
1137 return false;
1138 setOnlyReadsMemory(F);
1139 setDoesNotThrow(F);
1140 break;
1141 case LibFunc::modf:
1142 case LibFunc::modff:
1143 case LibFunc::modfl:
Chandler Carruth63559d72015-09-13 06:47:20 +00001144 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001145 return false;
1146 setDoesNotThrow(F);
1147 setDoesNotCapture(F, 2);
1148 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001149 case LibFunc::memcpy:
1150 case LibFunc::memccpy:
1151 case LibFunc::memmove:
Chandler Carruth63559d72015-09-13 06:47:20 +00001152 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001153 return false;
1154 setDoesNotThrow(F);
1155 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001156 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001157 break;
1158 case LibFunc::memalign:
1159 if (!FTy->getReturnType()->isPointerTy())
1160 return false;
1161 setDoesNotAlias(F, 0);
1162 break;
1163 case LibFunc::mkdir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001164 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001165 return false;
1166 setDoesNotThrow(F);
1167 setDoesNotCapture(F, 1);
1168 setOnlyReadsMemory(F, 1);
1169 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001170 case LibFunc::mktime:
Chandler Carruth63559d72015-09-13 06:47:20 +00001171 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001172 return false;
1173 setDoesNotThrow(F);
1174 setDoesNotCapture(F, 1);
1175 break;
1176 case LibFunc::realloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001177 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001178 !FTy->getReturnType()->isPointerTy())
1179 return false;
1180 setDoesNotThrow(F);
1181 setDoesNotAlias(F, 0);
1182 setDoesNotCapture(F, 1);
1183 break;
1184 case LibFunc::read:
Chandler Carruth63559d72015-09-13 06:47:20 +00001185 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001186 return false;
1187 // May throw; "read" is a valid pthread cancellation point.
1188 setDoesNotCapture(F, 2);
1189 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001190 case LibFunc::rewind:
Chandler Carruth63559d72015-09-13 06:47:20 +00001191 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001192 return false;
1193 setDoesNotThrow(F);
1194 setDoesNotCapture(F, 1);
1195 break;
1196 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001197 case LibFunc::remove:
1198 case LibFunc::realpath:
Chandler Carruth63559d72015-09-13 06:47:20 +00001199 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001200 return false;
1201 setDoesNotThrow(F);
1202 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001203 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001204 break;
1205 case LibFunc::rename:
Chandler Carruth63559d72015-09-13 06:47:20 +00001206 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001207 !FTy->getParamType(1)->isPointerTy())
1208 return false;
1209 setDoesNotThrow(F);
1210 setDoesNotCapture(F, 1);
1211 setDoesNotCapture(F, 2);
1212 setOnlyReadsMemory(F, 1);
1213 setOnlyReadsMemory(F, 2);
1214 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001215 case LibFunc::readlink:
Chandler Carruth63559d72015-09-13 06:47:20 +00001216 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001217 !FTy->getParamType(1)->isPointerTy())
1218 return false;
1219 setDoesNotThrow(F);
1220 setDoesNotCapture(F, 1);
1221 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001222 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001223 break;
1224 case LibFunc::write:
1225 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1226 return false;
1227 // May throw; "write" is a valid pthread cancellation point.
1228 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001229 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001230 break;
1231 case LibFunc::bcopy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001232 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001233 !FTy->getParamType(1)->isPointerTy())
1234 return false;
1235 setDoesNotThrow(F);
1236 setDoesNotCapture(F, 1);
1237 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001238 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001239 break;
1240 case LibFunc::bcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001241 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001242 !FTy->getParamType(1)->isPointerTy())
1243 return false;
1244 setDoesNotThrow(F);
1245 setOnlyReadsMemory(F);
1246 setDoesNotCapture(F, 1);
1247 setDoesNotCapture(F, 2);
1248 break;
1249 case LibFunc::bzero:
1250 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1251 return false;
1252 setDoesNotThrow(F);
1253 setDoesNotCapture(F, 1);
1254 break;
1255 case LibFunc::calloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001256 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001257 return false;
1258 setDoesNotThrow(F);
1259 setDoesNotAlias(F, 0);
1260 break;
1261 case LibFunc::chmod:
1262 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001263 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1264 return false;
1265 setDoesNotThrow(F);
1266 setDoesNotCapture(F, 1);
1267 setOnlyReadsMemory(F, 1);
1268 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001269 case LibFunc::ctermid:
1270 case LibFunc::clearerr:
1271 case LibFunc::closedir:
1272 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1273 return false;
1274 setDoesNotThrow(F);
1275 setDoesNotCapture(F, 1);
1276 break;
1277 case LibFunc::atoi:
1278 case LibFunc::atol:
1279 case LibFunc::atof:
1280 case LibFunc::atoll:
1281 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1282 return false;
1283 setDoesNotThrow(F);
1284 setOnlyReadsMemory(F);
1285 setDoesNotCapture(F, 1);
1286 break;
1287 case LibFunc::access:
1288 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1289 return false;
1290 setDoesNotThrow(F);
1291 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001292 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001293 break;
1294 case LibFunc::fopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001295 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001296 !FTy->getParamType(0)->isPointerTy() ||
1297 !FTy->getParamType(1)->isPointerTy())
1298 return false;
1299 setDoesNotThrow(F);
1300 setDoesNotAlias(F, 0);
1301 setDoesNotCapture(F, 1);
1302 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001303 setOnlyReadsMemory(F, 1);
1304 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001305 break;
1306 case LibFunc::fdopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001307 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001308 !FTy->getParamType(1)->isPointerTy())
1309 return false;
1310 setDoesNotThrow(F);
1311 setDoesNotAlias(F, 0);
1312 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001313 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001314 break;
1315 case LibFunc::feof:
1316 case LibFunc::free:
1317 case LibFunc::fseek:
1318 case LibFunc::ftell:
1319 case LibFunc::fgetc:
1320 case LibFunc::fseeko:
1321 case LibFunc::ftello:
1322 case LibFunc::fileno:
1323 case LibFunc::fflush:
1324 case LibFunc::fclose:
1325 case LibFunc::fsetpos:
1326 case LibFunc::flockfile:
1327 case LibFunc::funlockfile:
1328 case LibFunc::ftrylockfile:
1329 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1330 return false;
1331 setDoesNotThrow(F);
1332 setDoesNotCapture(F, 1);
1333 break;
1334 case LibFunc::ferror:
1335 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1336 return false;
1337 setDoesNotThrow(F);
1338 setDoesNotCapture(F, 1);
1339 setOnlyReadsMemory(F);
1340 break;
1341 case LibFunc::fputc:
1342 case LibFunc::fstat:
1343 case LibFunc::frexp:
1344 case LibFunc::frexpf:
1345 case LibFunc::frexpl:
1346 case LibFunc::fstatvfs:
1347 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1348 return false;
1349 setDoesNotThrow(F);
1350 setDoesNotCapture(F, 2);
1351 break;
1352 case LibFunc::fgets:
Chandler Carruth63559d72015-09-13 06:47:20 +00001353 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001354 !FTy->getParamType(2)->isPointerTy())
1355 return false;
1356 setDoesNotThrow(F);
1357 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001358 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001359 case LibFunc::fread:
Chandler Carruth63559d72015-09-13 06:47:20 +00001360 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001361 !FTy->getParamType(3)->isPointerTy())
1362 return false;
1363 setDoesNotThrow(F);
1364 setDoesNotCapture(F, 1);
1365 setDoesNotCapture(F, 4);
1366 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001367 case LibFunc::fwrite:
Chandler Carruth63559d72015-09-13 06:47:20 +00001368 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001369 !FTy->getParamType(3)->isPointerTy())
1370 return false;
1371 setDoesNotThrow(F);
1372 setDoesNotCapture(F, 1);
1373 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001374 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001375 case LibFunc::fputs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001376 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001377 !FTy->getParamType(1)->isPointerTy())
1378 return false;
1379 setDoesNotThrow(F);
1380 setDoesNotCapture(F, 1);
1381 setDoesNotCapture(F, 2);
1382 setOnlyReadsMemory(F, 1);
1383 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001384 case LibFunc::fscanf:
1385 case LibFunc::fprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001386 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001387 !FTy->getParamType(1)->isPointerTy())
1388 return false;
1389 setDoesNotThrow(F);
1390 setDoesNotCapture(F, 1);
1391 setDoesNotCapture(F, 2);
1392 setOnlyReadsMemory(F, 2);
1393 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001394 case LibFunc::fgetpos:
Chandler Carruth63559d72015-09-13 06:47:20 +00001395 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001396 !FTy->getParamType(1)->isPointerTy())
1397 return false;
1398 setDoesNotThrow(F);
1399 setDoesNotCapture(F, 1);
1400 setDoesNotCapture(F, 2);
1401 break;
1402 case LibFunc::getc:
1403 case LibFunc::getlogin_r:
1404 case LibFunc::getc_unlocked:
1405 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1406 return false;
1407 setDoesNotThrow(F);
1408 setDoesNotCapture(F, 1);
1409 break;
1410 case LibFunc::getenv:
1411 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1412 return false;
1413 setDoesNotThrow(F);
1414 setOnlyReadsMemory(F);
1415 setDoesNotCapture(F, 1);
1416 break;
1417 case LibFunc::gets:
1418 case LibFunc::getchar:
1419 setDoesNotThrow(F);
1420 break;
1421 case LibFunc::getitimer:
1422 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1423 return false;
1424 setDoesNotThrow(F);
1425 setDoesNotCapture(F, 2);
1426 break;
1427 case LibFunc::getpwnam:
1428 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1429 return false;
1430 setDoesNotThrow(F);
1431 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001432 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001433 break;
1434 case LibFunc::ungetc:
1435 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1436 return false;
1437 setDoesNotThrow(F);
1438 setDoesNotCapture(F, 2);
1439 break;
1440 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001441 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1442 return false;
1443 setDoesNotThrow(F);
1444 setDoesNotCapture(F, 1);
1445 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001446 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001447 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1448 return false;
1449 setDoesNotThrow(F);
1450 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001451 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001452 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001453 case LibFunc::unsetenv:
1454 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1455 return false;
1456 setDoesNotThrow(F);
1457 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001458 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001459 break;
1460 case LibFunc::utime:
1461 case LibFunc::utimes:
Chandler Carruth63559d72015-09-13 06:47:20 +00001462 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001463 !FTy->getParamType(1)->isPointerTy())
1464 return false;
1465 setDoesNotThrow(F);
1466 setDoesNotCapture(F, 1);
1467 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001468 setOnlyReadsMemory(F, 1);
1469 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001470 break;
1471 case LibFunc::putc:
1472 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1473 return false;
1474 setDoesNotThrow(F);
1475 setDoesNotCapture(F, 2);
1476 break;
1477 case LibFunc::puts:
1478 case LibFunc::printf:
1479 case LibFunc::perror:
1480 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1481 return false;
1482 setDoesNotThrow(F);
1483 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001484 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001485 break;
1486 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001487 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1488 return false;
1489 // May throw; "pread" is a valid pthread cancellation point.
1490 setDoesNotCapture(F, 2);
1491 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001492 case LibFunc::pwrite:
1493 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1494 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001495 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001496 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001497 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001498 break;
1499 case LibFunc::putchar:
1500 setDoesNotThrow(F);
1501 break;
1502 case LibFunc::popen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001503 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001504 !FTy->getParamType(0)->isPointerTy() ||
1505 !FTy->getParamType(1)->isPointerTy())
1506 return false;
1507 setDoesNotThrow(F);
1508 setDoesNotAlias(F, 0);
1509 setDoesNotCapture(F, 1);
1510 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001511 setOnlyReadsMemory(F, 1);
1512 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001513 break;
1514 case LibFunc::pclose:
1515 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1516 return false;
1517 setDoesNotThrow(F);
1518 setDoesNotCapture(F, 1);
1519 break;
1520 case LibFunc::vscanf:
1521 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1522 return false;
1523 setDoesNotThrow(F);
1524 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001525 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001526 break;
1527 case LibFunc::vsscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001528 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001529 !FTy->getParamType(2)->isPointerTy())
1530 return false;
1531 setDoesNotThrow(F);
1532 setDoesNotCapture(F, 1);
1533 setDoesNotCapture(F, 2);
1534 setOnlyReadsMemory(F, 1);
1535 setOnlyReadsMemory(F, 2);
1536 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001537 case LibFunc::vfscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001538 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001539 !FTy->getParamType(2)->isPointerTy())
1540 return false;
1541 setDoesNotThrow(F);
1542 setDoesNotCapture(F, 1);
1543 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001544 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001545 break;
1546 case LibFunc::valloc:
1547 if (!FTy->getReturnType()->isPointerTy())
1548 return false;
1549 setDoesNotThrow(F);
1550 setDoesNotAlias(F, 0);
1551 break;
1552 case LibFunc::vprintf:
1553 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1554 return false;
1555 setDoesNotThrow(F);
1556 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001557 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001558 break;
1559 case LibFunc::vfprintf:
1560 case LibFunc::vsprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001561 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001562 !FTy->getParamType(1)->isPointerTy())
1563 return false;
1564 setDoesNotThrow(F);
1565 setDoesNotCapture(F, 1);
1566 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001567 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001568 break;
1569 case LibFunc::vsnprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001570 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001571 !FTy->getParamType(2)->isPointerTy())
1572 return false;
1573 setDoesNotThrow(F);
1574 setDoesNotCapture(F, 1);
1575 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001576 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001577 break;
1578 case LibFunc::open:
1579 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1580 return false;
1581 // May throw; "open" is a valid pthread cancellation point.
1582 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001583 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001584 break;
1585 case LibFunc::opendir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001586 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001587 !FTy->getParamType(0)->isPointerTy())
1588 return false;
1589 setDoesNotThrow(F);
1590 setDoesNotAlias(F, 0);
1591 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001592 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001593 break;
1594 case LibFunc::tmpfile:
1595 if (!FTy->getReturnType()->isPointerTy())
1596 return false;
1597 setDoesNotThrow(F);
1598 setDoesNotAlias(F, 0);
1599 break;
1600 case LibFunc::times:
1601 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1602 return false;
1603 setDoesNotThrow(F);
1604 setDoesNotCapture(F, 1);
1605 break;
1606 case LibFunc::htonl:
1607 case LibFunc::htons:
1608 case LibFunc::ntohl:
1609 case LibFunc::ntohs:
1610 setDoesNotThrow(F);
1611 setDoesNotAccessMemory(F);
1612 break;
1613 case LibFunc::lstat:
Chandler Carruth63559d72015-09-13 06:47:20 +00001614 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001615 !FTy->getParamType(1)->isPointerTy())
1616 return false;
1617 setDoesNotThrow(F);
1618 setDoesNotCapture(F, 1);
1619 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001620 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001621 break;
1622 case LibFunc::lchown:
1623 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1624 return false;
1625 setDoesNotThrow(F);
1626 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001627 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001628 break;
1629 case LibFunc::qsort:
1630 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1631 return false;
1632 // May throw; places call through function pointer.
1633 setDoesNotCapture(F, 4);
1634 break;
1635 case LibFunc::dunder_strdup:
1636 case LibFunc::dunder_strndup:
Chandler Carruth63559d72015-09-13 06:47:20 +00001637 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001638 !FTy->getParamType(0)->isPointerTy())
1639 return false;
1640 setDoesNotThrow(F);
1641 setDoesNotAlias(F, 0);
1642 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001643 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001644 break;
1645 case LibFunc::dunder_strtok_r:
Chandler Carruth63559d72015-09-13 06:47:20 +00001646 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001647 return false;
1648 setDoesNotThrow(F);
1649 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001650 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001651 break;
1652 case LibFunc::under_IO_getc:
1653 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1654 return false;
1655 setDoesNotThrow(F);
1656 setDoesNotCapture(F, 1);
1657 break;
1658 case LibFunc::under_IO_putc:
1659 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1660 return false;
1661 setDoesNotThrow(F);
1662 setDoesNotCapture(F, 2);
1663 break;
1664 case LibFunc::dunder_isoc99_scanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001665 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001666 return false;
1667 setDoesNotThrow(F);
1668 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001669 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001670 break;
1671 case LibFunc::stat64:
1672 case LibFunc::lstat64:
1673 case LibFunc::statvfs64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001674 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001675 !FTy->getParamType(1)->isPointerTy())
1676 return false;
1677 setDoesNotThrow(F);
1678 setDoesNotCapture(F, 1);
1679 setDoesNotCapture(F, 2);
1680 setOnlyReadsMemory(F, 1);
1681 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001682 case LibFunc::dunder_isoc99_sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001683 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001684 !FTy->getParamType(1)->isPointerTy())
1685 return false;
1686 setDoesNotThrow(F);
1687 setDoesNotCapture(F, 1);
1688 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001689 setOnlyReadsMemory(F, 1);
1690 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001691 break;
1692 case LibFunc::fopen64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001693 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001694 !FTy->getParamType(0)->isPointerTy() ||
1695 !FTy->getParamType(1)->isPointerTy())
1696 return false;
1697 setDoesNotThrow(F);
1698 setDoesNotAlias(F, 0);
1699 setDoesNotCapture(F, 1);
1700 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001701 setOnlyReadsMemory(F, 1);
1702 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001703 break;
1704 case LibFunc::fseeko64:
1705 case LibFunc::ftello64:
1706 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1707 return false;
1708 setDoesNotThrow(F);
1709 setDoesNotCapture(F, 1);
1710 break;
1711 case LibFunc::tmpfile64:
1712 if (!FTy->getReturnType()->isPointerTy())
1713 return false;
1714 setDoesNotThrow(F);
1715 setDoesNotAlias(F, 0);
1716 break;
1717 case LibFunc::fstat64:
1718 case LibFunc::fstatvfs64:
1719 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1720 return false;
1721 setDoesNotThrow(F);
1722 setDoesNotCapture(F, 2);
1723 break;
1724 case LibFunc::open64:
1725 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1726 return false;
1727 // May throw; "open" is a valid pthread cancellation point.
1728 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001729 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001730 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001731 case LibFunc::gettimeofday:
1732 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1733 !FTy->getParamType(1)->isPointerTy())
1734 return false;
1735 // Currently some platforms have the restrict keyword on the arguments to
1736 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1737 // arguments.
1738 setDoesNotThrow(F);
1739 setDoesNotCapture(F, 1);
1740 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001741 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001742 default:
1743 // Didn't mark any attributes.
1744 return false;
1745 }
1746
1747 return true;
1748}
1749
Chris Lattner4422d312010-04-16 22:42:17 +00001750bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001751 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruthcada2d82015-10-31 00:28:37 +00001752 bool Changed = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001753
Chandler Carrutha8125352015-10-30 16:48:08 +00001754 // We compute dedicated AA results for each function in the SCC as needed. We
1755 // use a lambda referencing external objects so that they live long enough to
1756 // be queried, but we re-use them each time.
1757 Optional<BasicAAResult> BAR;
1758 Optional<AAResults> AAR;
1759 auto AARGetter = [&](Function &F) -> AAResults & {
1760 BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1761 AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1762 return *AAR;
1763 };
1764
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001765 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1766 // whether a given CallGraphNode is in this SCC. Also track whether there are
1767 // any external or opt-none nodes that will prevent us from optimizing any
1768 // part of the SCC.
1769 SCCNodeSet SCCNodes;
1770 bool ExternalNode = false;
1771 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1772 Function *F = (*I)->getFunction();
1773 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1774 // External node or function we're trying not to optimize - we both avoid
1775 // transform them and avoid leveraging information they provide.
1776 ExternalNode = true;
1777 continue;
1778 }
1779
Chandler Carruthcada2d82015-10-31 00:28:37 +00001780 // When initially processing functions, also infer their prototype
1781 // attributes if they are declarations.
1782 if (F->isDeclaration())
1783 Changed |= inferPrototypeAttributes(*F, *TLI);
1784
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001785 SCCNodes.insert(F);
1786 }
1787
Chandler Carrutha8125352015-10-30 16:48:08 +00001788 Changed |= addReadAttrs(SCCNodes, AARGetter);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001789 Changed |= addArgumentAttrs(SCCNodes);
1790
1791 // If we have no external nodes participating in the SCC, we can infer some
1792 // more precise attributes as well.
1793 if (!ExternalNode) {
1794 Changed |= addNoAliasAttrs(SCCNodes);
1795 Changed |= addNonNullAttrs(SCCNodes, *TLI);
1796 }
1797
Duncan Sands44c8cd92008-12-31 16:14:43 +00001798 return Changed;
1799}