blob: e699c5e0df5cf100581e5dba163b486b2b0c8e10 [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"
James Molloy0ecdbe72015-11-19 08:49:57 +000026#include "llvm/ADT/StringSwitch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Analysis/AliasAnalysis.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000028#include "llvm/Analysis/AssumptionCache.h"
29#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000031#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000032#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000033#include "llvm/Analysis/TargetLibraryInfo.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000034#include "llvm/Analysis/ValueTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000035#include "llvm/IR/GlobalVariable.h"
Chandler Carruth83948572014-03-04 10:30:26 +000036#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000037#include "llvm/IR/IntrinsicInst.h"
38#include "llvm/IR/LLVMContext.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000039#include "llvm/Support/Debug.h"
Hans Wennborg043bf5b2015-08-31 21:19:18 +000040#include "llvm/Support/raw_ostream.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000041#include "llvm/Analysis/TargetLibraryInfo.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000042using namespace llvm;
43
Chandler Carruth964daaa2014-04-22 02:55:47 +000044#define DEBUG_TYPE "functionattrs"
45
Duncan Sands44c8cd92008-12-31 16:14:43 +000046STATISTIC(NumReadNone, "Number of functions marked readnone");
47STATISTIC(NumReadOnly, "Number of functions marked readonly");
48STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000049STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
50STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000051STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Philip Reamesa88caea2015-08-31 19:44:38 +000052STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
Meador Inge6b6a1612013-03-21 00:55:59 +000053STATISTIC(NumAnnotated, "Number of attributes added to library functions");
James Molloy7e9bdd52015-11-12 10:55:20 +000054STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
Duncan Sands44c8cd92008-12-31 16:14:43 +000055
James Molloy0ecdbe72015-11-19 08:49:57 +000056static cl::list<std::string>
57ForceAttributes("force-attribute", cl::Hidden,
58 cl::desc("Add an attribute to a function. This should be a "
59 "pair of 'function-name:attribute-name', for "
60 "example -force-add-attribute=foo:noinline. This "
61 "option can be specified multiple times."));
62
Duncan Sands44c8cd92008-12-31 16:14:43 +000063namespace {
Chandler Carruthc518ebd2015-10-29 18:29:15 +000064typedef SmallSetVector<Function *, 8> SCCNodeSet;
65}
66
67namespace {
Chandler Carruth63559d72015-09-13 06:47:20 +000068struct FunctionAttrs : public CallGraphSCCPass {
69 static char ID; // Pass identification, replacement for typeid
70 FunctionAttrs() : CallGraphSCCPass(ID) {
71 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
72 }
73
Chandler Carruth63559d72015-09-13 06:47:20 +000074 bool runOnSCC(CallGraphSCC &SCC) override;
James Molloy7e9bdd52015-11-12 10:55:20 +000075 bool doInitialization(CallGraph &CG) override {
76 Revisit.clear();
77 return false;
78 }
79 bool doFinalization(CallGraph &CG) override;
80
Chandler Carruth63559d72015-09-13 06:47:20 +000081 void getAnalysisUsage(AnalysisUsage &AU) const override {
82 AU.setPreservesCFG();
83 AU.addRequired<AssumptionCacheTracker>();
84 AU.addRequired<TargetLibraryInfoWrapperPass>();
85 CallGraphSCCPass::getAnalysisUsage(AU);
86 }
Meador Inge6b6a1612013-03-21 00:55:59 +000087
Chandler Carruth63559d72015-09-13 06:47:20 +000088private:
89 TargetLibraryInfo *TLI;
James Molloy7e9bdd52015-11-12 10:55:20 +000090 SmallVector<WeakVH,16> Revisit;
Chandler Carruth63559d72015-09-13 06:47:20 +000091};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000092}
Duncan Sands44c8cd92008-12-31 16:14:43 +000093
94char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000095INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +000096 "Deduce function attributes", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +000097INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +000098INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000099INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +0000100INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +0000101 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000102
103Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
104
Chandler Carruth7542d372015-09-21 17:39:41 +0000105namespace {
106/// The three kinds of memory access relevant to 'readonly' and
107/// 'readnone' attributes.
108enum MemoryAccessKind {
109 MAK_ReadNone = 0,
110 MAK_ReadOnly = 1,
111 MAK_MayWrite = 2
112};
113}
114
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000115static MemoryAccessKind checkFunctionMemoryAccess(Function &F, AAResults &AAR,
116 const SCCNodeSet &SCCNodes) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000117 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
118 if (MRB == FMRB_DoesNotAccessMemory)
119 // Already perfect!
120 return MAK_ReadNone;
121
122 // Definitions with weak linkage may be overridden at linktime with
123 // something that writes memory, so treat them like declarations.
124 if (F.isDeclaration() || F.mayBeOverridden()) {
125 if (AliasAnalysis::onlyReadsMemory(MRB))
126 return MAK_ReadOnly;
127
128 // Conservatively assume it writes to memory.
129 return MAK_MayWrite;
130 }
131
132 // Scan the function body for instructions that may read or write memory.
133 bool ReadsMemory = false;
134 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
135 Instruction *I = &*II;
136
137 // Some instructions can be ignored even if they read or write memory.
138 // Detect these now, skipping to the next instruction if one is found.
139 CallSite CS(cast<Value>(I));
140 if (CS) {
141 // Ignore calls to functions in the same SCC.
142 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
143 continue;
144 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
Chandler Carruth7542d372015-09-21 17:39:41 +0000145
Chandler Carruth69798fb2015-10-27 01:41:43 +0000146 // If the call doesn't access memory, we're done.
147 if (!(MRB & MRI_ModRef))
148 continue;
149
150 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
151 // The call could access any memory. If that includes writes, give up.
152 if (MRB & MRI_Mod)
153 return MAK_MayWrite;
154 // If it reads, note it.
155 if (MRB & MRI_Ref)
156 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000157 continue;
158 }
Chandler Carruth69798fb2015-10-27 01:41:43 +0000159
160 // Check whether all pointer arguments point to local memory, and
161 // ignore calls that only access local memory.
162 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
163 CI != CE; ++CI) {
164 Value *Arg = *CI;
Elena Demikhovsky3ec9e152015-11-17 19:30:51 +0000165 if (!Arg->getType()->isPtrOrPtrVectorTy())
Chandler Carruth69798fb2015-10-27 01:41:43 +0000166 continue;
167
168 AAMDNodes AAInfo;
169 I->getAAMetadata(AAInfo);
170 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
171
172 // Skip accesses to local or constant memory as they don't impact the
173 // externally visible mod/ref behavior.
174 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
175 continue;
176
177 if (MRB & MRI_Mod)
178 // Writes non-local memory. Give up.
179 return MAK_MayWrite;
180 if (MRB & MRI_Ref)
181 // Ok, it reads non-local memory.
182 ReadsMemory = true;
183 }
Chandler Carruth7542d372015-09-21 17:39:41 +0000184 continue;
185 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
186 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
187 if (!LI->isVolatile()) {
188 MemoryLocation Loc = MemoryLocation::get(LI);
189 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
190 continue;
191 }
192 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
193 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
194 if (!SI->isVolatile()) {
195 MemoryLocation Loc = MemoryLocation::get(SI);
196 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
197 continue;
198 }
199 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
200 // Ignore vaargs on local memory.
201 MemoryLocation Loc = MemoryLocation::get(VI);
202 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
203 continue;
204 }
205
206 // Any remaining instructions need to be taken seriously! Check if they
207 // read or write memory.
208 if (I->mayWriteToMemory())
209 // Writes memory. Just give up.
210 return MAK_MayWrite;
211
212 // If this instruction may read memory, remember that.
213 ReadsMemory |= I->mayReadFromMemory();
214 }
215
216 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
217}
218
Chandler Carrutha632fb92015-09-13 06:57:25 +0000219/// Deduce readonly/readnone attributes for the SCC.
Chandler Carrutha8125352015-10-30 16:48:08 +0000220template <typename AARGetterT>
221static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT AARGetter) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000222 // Check if any of the functions in the SCC read or write memory. If they
223 // write memory then they can't be marked readnone or readonly.
224 bool ReadsMemory = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000225 for (Function *F : SCCNodes) {
Chandler Carrutha8125352015-10-30 16:48:08 +0000226 // Call the callable parameter to look up AA results for this function.
227 AAResults &AAR = AARGetter(*F);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000228
Chandler Carruth7542d372015-09-21 17:39:41 +0000229 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
230 case MAK_MayWrite:
231 return false;
232 case MAK_ReadOnly:
Duncan Sands44c8cd92008-12-31 16:14:43 +0000233 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000234 break;
235 case MAK_ReadNone:
236 // Nothing to do!
237 break;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000238 }
239 }
240
241 // Success! Functions in this SCC do not access memory, or only read memory.
242 // Give them the appropriate attribute.
243 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000244 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000245 if (F->doesNotAccessMemory())
246 // Already perfect!
247 continue;
248
249 if (F->onlyReadsMemory() && ReadsMemory)
250 // No change.
251 continue;
252
253 MadeChange = true;
254
255 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000256 AttrBuilder B;
Chandler Carruth63559d72015-09-13 06:47:20 +0000257 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
258 F->removeAttributes(
259 AttributeSet::FunctionIndex,
260 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000261
262 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000263 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000264 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000265
266 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000267 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000268 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000269 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000270 }
271
272 return MadeChange;
273}
274
Nick Lewycky4c378a42011-12-28 23:24:21 +0000275namespace {
Chandler Carrutha632fb92015-09-13 06:57:25 +0000276/// For a given pointer Argument, this retains a list of Arguments of functions
277/// in the same SCC that the pointer data flows into. We use this to build an
278/// SCC of the arguments.
Chandler Carruth63559d72015-09-13 06:47:20 +0000279struct ArgumentGraphNode {
280 Argument *Definition;
281 SmallVector<ArgumentGraphNode *, 4> Uses;
282};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000283
Chandler Carruth63559d72015-09-13 06:47:20 +0000284class ArgumentGraph {
285 // We store pointers to ArgumentGraphNode objects, so it's important that
286 // that they not move around upon insert.
287 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000288
Chandler Carruth63559d72015-09-13 06:47:20 +0000289 ArgumentMapTy ArgumentMap;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000290
Chandler Carruth63559d72015-09-13 06:47:20 +0000291 // There is no root node for the argument graph, in fact:
292 // void f(int *x, int *y) { if (...) f(x, y); }
293 // is an example where the graph is disconnected. The SCCIterator requires a
294 // single entry point, so we maintain a fake ("synthetic") root node that
295 // uses every node. Because the graph is directed and nothing points into
296 // the root, it will not participate in any SCCs (except for its own).
297 ArgumentGraphNode SyntheticRoot;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000298
Chandler Carruth63559d72015-09-13 06:47:20 +0000299public:
300 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000301
Chandler Carruth63559d72015-09-13 06:47:20 +0000302 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000303
Chandler Carruth63559d72015-09-13 06:47:20 +0000304 iterator begin() { return SyntheticRoot.Uses.begin(); }
305 iterator end() { return SyntheticRoot.Uses.end(); }
306 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000307
Chandler Carruth63559d72015-09-13 06:47:20 +0000308 ArgumentGraphNode *operator[](Argument *A) {
309 ArgumentGraphNode &Node = ArgumentMap[A];
310 Node.Definition = A;
311 SyntheticRoot.Uses.push_back(&Node);
312 return &Node;
313 }
314};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000315
Chandler Carrutha632fb92015-09-13 06:57:25 +0000316/// This tracker checks whether callees are in the SCC, and if so it does not
317/// consider that a capture, instead adding it to the "Uses" list and
318/// continuing with the analysis.
Chandler Carruth63559d72015-09-13 06:47:20 +0000319struct ArgumentUsesTracker : public CaptureTracker {
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000320 ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
Nick Lewycky4c378a42011-12-28 23:24:21 +0000321 : Captured(false), SCCNodes(SCCNodes) {}
322
Chandler Carruth63559d72015-09-13 06:47:20 +0000323 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000324
Chandler Carruth63559d72015-09-13 06:47:20 +0000325 bool captured(const Use *U) override {
326 CallSite CS(U->getUser());
327 if (!CS.getInstruction()) {
328 Captured = true;
329 return true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000330 }
331
Chandler Carruth63559d72015-09-13 06:47:20 +0000332 Function *F = CS.getCalledFunction();
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000333 if (!F || F->isDeclaration() || F->mayBeOverridden() ||
334 !SCCNodes.count(F)) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000335 Captured = true;
336 return true;
337 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000338
Sanjoy Das98bfe262015-11-05 03:04:40 +0000339 // Note: the callee and the two successor blocks *follow* the argument
340 // operands. This means there is no need to adjust UseIndex to account for
341 // these.
342
343 unsigned UseIndex =
344 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
345
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000346 assert(UseIndex < CS.data_operands_size() &&
347 "Indirect function calls should have been filtered above!");
348
349 if (UseIndex >= CS.getNumArgOperands()) {
350 // Data operand, but not a argument operand -- must be a bundle operand
351 assert(CS.hasOperandBundles() && "Must be!");
352
353 // CaptureTracking told us that we're being captured by an operand bundle
354 // use. In this case it does not matter if the callee is within our SCC
355 // or not -- we've been captured in some unknown way, and we have to be
356 // conservative.
357 Captured = true;
358 return true;
359 }
360
Sanjoy Das98bfe262015-11-05 03:04:40 +0000361 if (UseIndex >= F->arg_size()) {
362 assert(F->isVarArg() && "More params than args in non-varargs call");
363 Captured = true;
364 return true;
Chandler Carruth63559d72015-09-13 06:47:20 +0000365 }
Sanjoy Das98bfe262015-11-05 03:04:40 +0000366
Duncan P. N. Exon Smith83c4b682015-11-07 00:01:16 +0000367 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
Chandler Carruth63559d72015-09-13 06:47:20 +0000368 return false;
369 }
370
371 bool Captured; // True only if certainly captured (used outside our SCC).
372 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
373
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000374 const SCCNodeSet &SCCNodes;
Chandler Carruth63559d72015-09-13 06:47:20 +0000375};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000376}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000377
378namespace llvm {
Chandler Carruth63559d72015-09-13 06:47:20 +0000379template <> struct GraphTraits<ArgumentGraphNode *> {
380 typedef ArgumentGraphNode NodeType;
381 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000382
Chandler Carruth63559d72015-09-13 06:47:20 +0000383 static inline NodeType *getEntryNode(NodeType *A) { return A; }
384 static inline ChildIteratorType child_begin(NodeType *N) {
385 return N->Uses.begin();
386 }
387 static inline ChildIteratorType child_end(NodeType *N) {
388 return N->Uses.end();
389 }
390};
391template <>
392struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
393 static NodeType *getEntryNode(ArgumentGraph *AG) {
394 return AG->getEntryNode();
395 }
396 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
397 return AG->begin();
398 }
399 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
400};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000401}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000402
Chandler Carrutha632fb92015-09-13 06:57:25 +0000403/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000404static Attribute::AttrKind
405determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000406 const SmallPtrSet<Argument *, 8> &SCCNodes) {
407
408 SmallVector<Use *, 32> Worklist;
409 SmallSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000410
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000411 // inalloca arguments are always clobbered by the call.
412 if (A->hasInAllocaAttr())
413 return Attribute::None;
414
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000415 bool IsRead = false;
416 // We don't need to track IsWritten. If A is written to, return immediately.
417
Chandler Carruthcdf47882014-03-09 03:16:01 +0000418 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000419 Visited.insert(&U);
420 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000421 }
422
423 while (!Worklist.empty()) {
424 Use *U = Worklist.pop_back_val();
425 Instruction *I = cast<Instruction>(U->getUser());
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000426
427 switch (I->getOpcode()) {
428 case Instruction::BitCast:
429 case Instruction::GetElementPtr:
430 case Instruction::PHI:
431 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000432 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000433 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000434 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000435 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000436 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000437 break;
438
439 case Instruction::Call:
440 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000441 bool Captures = true;
442
443 if (I->getType()->isVoidTy())
444 Captures = false;
445
446 auto AddUsersToWorklistIfCapturing = [&] {
447 if (Captures)
448 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000449 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000450 Worklist.push_back(&UU);
451 };
452
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000453 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000454 if (CS.doesNotAccessMemory()) {
455 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000456 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000457 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000458
459 Function *F = CS.getCalledFunction();
460 if (!F) {
461 if (CS.onlyReadsMemory()) {
462 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000463 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000464 continue;
465 }
466 return Attribute::None;
467 }
468
Sanjoy Das436e2392015-11-07 01:55:53 +0000469 // Note: the callee and the two successor blocks *follow* the argument
470 // operands. This means there is no need to adjust UseIndex to account
471 // for these.
472
473 unsigned UseIndex = std::distance(CS.arg_begin(), U);
474
Sanjoy Dasea1df7f2015-11-07 01:56:07 +0000475 // U cannot be the callee operand use: since we're exploring the
476 // transitive uses of an Argument, having such a use be a callee would
477 // imply the CallSite is an indirect call or invoke; and we'd take the
478 // early exit above.
479 assert(UseIndex < CS.data_operands_size() &&
480 "Data operand use expected!");
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000481
482 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
483
484 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
Sanjoy Das436e2392015-11-07 01:55:53 +0000485 assert(F->isVarArg() && "More params than args in non-varargs call");
486 return Attribute::None;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000487 }
Sanjoy Das436e2392015-11-07 01:55:53 +0000488
489 Captures &= !CS.doesNotCapture(UseIndex);
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000490
491 // Since the optimizer (by design) cannot see the data flow corresponding
492 // to a operand bundle use, these cannot participate in the optimistic SCC
493 // analysis. Instead, we model the operand bundle uses as arguments in
494 // call to a function external to the SCC.
Sanjoy Das76dd2432015-11-07 02:26:53 +0000495 if (!SCCNodes.count(&*std::next(F->arg_begin(), UseIndex)) ||
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000496 IsOperandBundleUse) {
497
498 // The accessors used on CallSite here do the right thing for calls and
499 // invokes with operand bundles.
500
Sanjoy Das436e2392015-11-07 01:55:53 +0000501 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
502 return Attribute::None;
503 if (!CS.doesNotAccessMemory(UseIndex))
504 IsRead = true;
505 }
506
Nick Lewycky59633cb2014-05-30 02:31:27 +0000507 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000508 break;
509 }
510
511 case Instruction::Load:
512 IsRead = true;
513 break;
514
515 case Instruction::ICmp:
516 case Instruction::Ret:
517 break;
518
519 default:
520 return Attribute::None;
521 }
522 }
523
524 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
525}
526
Chandler Carrutha632fb92015-09-13 06:57:25 +0000527/// Deduce nocapture attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000528static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000529 bool Changed = false;
530
Nick Lewycky4c378a42011-12-28 23:24:21 +0000531 ArgumentGraph AG;
532
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000533 AttrBuilder B;
534 B.addAttribute(Attribute::NoCapture);
535
Duncan Sands44c8cd92008-12-31 16:14:43 +0000536 // Check each function in turn, determining which pointer arguments are not
537 // captured.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000538 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000539 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000540 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000541 if (F->isDeclaration() || F->mayBeOverridden())
542 continue;
543
Nick Lewycky4c378a42011-12-28 23:24:21 +0000544 // Functions that are readonly (or readnone) and nounwind and don't return
545 // a value can't capture arguments. Don't analyze them.
546 if (F->onlyReadsMemory() && F->doesNotThrow() &&
547 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000548 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
549 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000550 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
551 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
552 ++NumNoCapture;
553 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000554 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000555 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000556 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000557 }
558
Chandler Carruth63559d72015-09-13 06:47:20 +0000559 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
560 ++A) {
561 if (!A->getType()->isPointerTy())
562 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000563 bool HasNonLocalUses = false;
564 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000565 ArgumentUsesTracker Tracker(SCCNodes);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000566 PointerMayBeCaptured(&*A, &Tracker);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000567 if (!Tracker.Captured) {
568 if (Tracker.Uses.empty()) {
569 // If it's trivially not captured, mark it nocapture now.
Chandler Carruth63559d72015-09-13 06:47:20 +0000570 A->addAttr(
571 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000572 ++NumNoCapture;
573 Changed = true;
574 } else {
575 // If it's not trivially captured and not trivially not captured,
576 // then it must be calling into another function in our SCC. Save
577 // its particulars for Argument-SCC analysis later.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000578 ArgumentGraphNode *Node = AG[&*A];
Chandler Carruth63559d72015-09-13 06:47:20 +0000579 for (SmallVectorImpl<Argument *>::iterator
580 UI = Tracker.Uses.begin(),
581 UE = Tracker.Uses.end();
582 UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000583 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000584 if (*UI != A)
585 HasNonLocalUses = true;
586 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000587 }
588 }
589 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
590 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000591 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
592 // Can we determine that it's readonly/readnone without doing an SCC?
593 // Note that we don't allow any calls at all here, or else our result
594 // will be dependent on the iteration order through the functions in the
595 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000596 SmallPtrSet<Argument *, 8> Self;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000597 Self.insert(&*A);
598 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000599 if (R != Attribute::None) {
600 AttrBuilder B;
601 B.addAttribute(R);
602 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
603 Changed = true;
604 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
605 }
606 }
607 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000608 }
609
610 // The graph we've collected is partial because we stopped scanning for
611 // argument uses once we solved the argument trivially. These partial nodes
612 // show up as ArgumentGraphNode objects with an empty Uses list, and for
613 // these nodes the final decision about whether they capture has already been
614 // made. If the definition doesn't have a 'nocapture' attribute by now, it
615 // captures.
616
Chandler Carruth63559d72015-09-13 06:47:20 +0000617 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000618 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000619 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000620 if (!ArgumentSCC[0]->Definition)
621 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000622
623 // eg. "void f(int* x) { if (...) f(x); }"
624 if (ArgumentSCC[0]->Uses.size() == 1 &&
625 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000626 Argument *A = ArgumentSCC[0]->Definition;
627 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000628 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000629 Changed = true;
630 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000631 continue;
632 }
633
634 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000635 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
636 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000637 ArgumentGraphNode *Node = *I;
638 if (Node->Uses.empty()) {
639 if (!Node->Definition->hasNoCaptureAttr())
640 SCCCaptured = true;
641 }
642 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000643 if (SCCCaptured)
644 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000645
Chandler Carruth63559d72015-09-13 06:47:20 +0000646 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000647 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
648 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000649 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000650 ArgumentSCCNodes.insert((*I)->Definition);
651 }
652
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000653 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
654 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000655 ArgumentGraphNode *N = *I;
Chandler Carruth63559d72015-09-13 06:47:20 +0000656 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
657 UE = N->Uses.end();
658 UI != UE; ++UI) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000659 Argument *A = (*UI)->Definition;
660 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
661 continue;
662 SCCCaptured = true;
663 break;
664 }
665 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000666 if (SCCCaptured)
667 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000668
Nick Lewyckyf740db32012-01-05 22:21:45 +0000669 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000670 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000671 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000672 ++NumNoCapture;
673 Changed = true;
674 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000675
676 // We also want to compute readonly/readnone. With a small number of false
677 // negatives, we can assume that any pointer which is captured isn't going
678 // to be provably readonly or readnone, since by definition we can't
679 // analyze all uses of a captured pointer.
680 //
681 // The false negatives happen when the pointer is captured by a function
682 // that promises readonly/readnone behaviour on the pointer, then the
683 // pointer's lifetime ends before anything that writes to arbitrary memory.
684 // Also, a readonly/readnone pointer may be returned, but returning a
685 // pointer is capturing it.
686
687 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
688 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
689 Argument *A = ArgumentSCC[i]->Definition;
690 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
691 if (K == Attribute::ReadNone)
692 continue;
693 if (K == Attribute::ReadOnly) {
694 ReadAttr = Attribute::ReadOnly;
695 continue;
696 }
697 ReadAttr = K;
698 break;
699 }
700
701 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000702 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000703 B.addAttribute(ReadAttr);
Chandler Carruth63559d72015-09-13 06:47:20 +0000704 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000705 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
706 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000707 // Clear out existing readonly/readnone attributes
708 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000709 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
710 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
711 Changed = true;
712 }
713 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000714 }
715
716 return Changed;
717}
718
Chandler Carrutha632fb92015-09-13 06:57:25 +0000719/// Tests whether a function is "malloc-like".
720///
721/// A function is "malloc-like" if it returns either null or a pointer that
722/// doesn't alias any other pointer visible to the caller.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000723static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000724 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000725 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
726 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
727 FlowsToReturn.insert(Ret->getReturnValue());
728
729 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000730 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000731
732 if (Constant *C = dyn_cast<Constant>(RetVal)) {
733 if (!C->isNullValue() && !isa<UndefValue>(C))
734 return false;
735
736 continue;
737 }
738
739 if (isa<Argument>(RetVal))
740 return false;
741
742 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
743 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000744 // Extend the analysis by looking upwards.
745 case Instruction::BitCast:
746 case Instruction::GetElementPtr:
747 case Instruction::AddrSpaceCast:
748 FlowsToReturn.insert(RVI->getOperand(0));
749 continue;
750 case Instruction::Select: {
751 SelectInst *SI = cast<SelectInst>(RVI);
752 FlowsToReturn.insert(SI->getTrueValue());
753 FlowsToReturn.insert(SI->getFalseValue());
754 continue;
755 }
756 case Instruction::PHI: {
757 PHINode *PN = cast<PHINode>(RVI);
758 for (Value *IncValue : PN->incoming_values())
759 FlowsToReturn.insert(IncValue);
760 continue;
761 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000762
Chandler Carruth63559d72015-09-13 06:47:20 +0000763 // Check whether the pointer came from an allocation.
764 case Instruction::Alloca:
765 break;
766 case Instruction::Call:
767 case Instruction::Invoke: {
768 CallSite CS(RVI);
769 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000770 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000771 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
772 break;
773 } // fall-through
774 default:
775 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000776 }
777
Dan Gohman94e61762009-11-19 21:57:48 +0000778 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000779 return false;
780 }
781
782 return true;
783}
784
Chandler Carrutha632fb92015-09-13 06:57:25 +0000785/// Deduce noalias attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000786static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000787 // Check each function in turn, determining which functions return noalias
788 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000789 for (Function *F : SCCNodes) {
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000790 // Already noalias.
791 if (F->doesNotAlias(0))
792 continue;
793
794 // Definitions with weak linkage may be overridden at linktime, so
795 // treat them like declarations.
796 if (F->isDeclaration() || F->mayBeOverridden())
797 return false;
798
Chandler Carruth63559d72015-09-13 06:47:20 +0000799 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000800 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000801 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000802 continue;
803
Chandler Carruth3824f852015-09-13 08:23:27 +0000804 if (!isFunctionMallocLike(F, SCCNodes))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000805 return false;
806 }
807
808 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000809 for (Function *F : SCCNodes) {
Duncan Sands19d0b472010-02-16 11:11:14 +0000810 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000811 continue;
812
813 F->setDoesNotAlias(0);
814 ++NumNoAlias;
815 MadeChange = true;
816 }
817
818 return MadeChange;
819}
820
Chandler Carrutha632fb92015-09-13 06:57:25 +0000821/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000822///
823/// Requires that the function returns a pointer.
824///
825/// Returns true if it believes the function will not return a null, and sets
826/// \p Speculative based on whether the returned conclusion is a speculative
827/// conclusion due to SCC calls.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000828static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
Chandler Carruth8874b782015-09-13 08:17:14 +0000829 const TargetLibraryInfo &TLI, bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000830 assert(F->getReturnType()->isPointerTy() &&
831 "nonnull only meaningful on pointer types");
832 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000833
Philip Reamesa88caea2015-08-31 19:44:38 +0000834 SmallSetVector<Value *, 8> FlowsToReturn;
835 for (BasicBlock &BB : *F)
836 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
837 FlowsToReturn.insert(Ret->getReturnValue());
838
839 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
840 Value *RetVal = FlowsToReturn[i];
841
842 // If this value is locally known to be non-null, we're good
Chandler Carruth8874b782015-09-13 08:17:14 +0000843 if (isKnownNonNull(RetVal, &TLI))
Philip Reamesa88caea2015-08-31 19:44:38 +0000844 continue;
845
846 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000847 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000848 Instruction *RVI = dyn_cast<Instruction>(RetVal);
849 if (!RVI)
850 return false;
851 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000852 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000853 case Instruction::BitCast:
854 case Instruction::GetElementPtr:
855 case Instruction::AddrSpaceCast:
856 FlowsToReturn.insert(RVI->getOperand(0));
857 continue;
858 case Instruction::Select: {
859 SelectInst *SI = cast<SelectInst>(RVI);
860 FlowsToReturn.insert(SI->getTrueValue());
861 FlowsToReturn.insert(SI->getFalseValue());
862 continue;
863 }
864 case Instruction::PHI: {
865 PHINode *PN = cast<PHINode>(RVI);
866 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
867 FlowsToReturn.insert(PN->getIncomingValue(i));
868 continue;
869 }
870 case Instruction::Call:
871 case Instruction::Invoke: {
872 CallSite CS(RVI);
873 Function *Callee = CS.getCalledFunction();
874 // A call to a node within the SCC is assumed to return null until
875 // proven otherwise
876 if (Callee && SCCNodes.count(Callee)) {
877 Speculative = true;
878 continue;
879 }
880 return false;
881 }
882 default:
Chandler Carruth63559d72015-09-13 06:47:20 +0000883 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +0000884 };
885 llvm_unreachable("should have either continued or returned");
886 }
887
888 return true;
889}
890
Chandler Carrutha632fb92015-09-13 06:57:25 +0000891/// Deduce nonnull attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000892static bool addNonNullAttrs(const SCCNodeSet &SCCNodes,
893 const TargetLibraryInfo &TLI) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000894 // Speculative that all functions in the SCC return only nonnull
895 // pointers. We may refute this as we analyze functions.
896 bool SCCReturnsNonNull = true;
897
898 bool MadeChange = false;
899
900 // Check each function in turn, determining which functions return nonnull
901 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000902 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000903 // Already nonnull.
904 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
905 Attribute::NonNull))
906 continue;
907
908 // Definitions with weak linkage may be overridden at linktime, so
909 // treat them like declarations.
910 if (F->isDeclaration() || F->mayBeOverridden())
911 return false;
912
Chandler Carruth63559d72015-09-13 06:47:20 +0000913 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +0000914 // pointer types.
915 if (!F->getReturnType()->isPointerTy())
916 continue;
917
918 bool Speculative = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000919 if (isReturnNonNull(F, SCCNodes, TLI, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000920 if (!Speculative) {
921 // Mark the function eagerly since we may discover a function
922 // which prevents us from speculating about the entire SCC
923 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
924 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
925 ++NumNonNullReturn;
926 MadeChange = true;
927 }
928 continue;
929 }
930 // At least one function returns something which could be null, can't
931 // speculate any more.
932 SCCReturnsNonNull = false;
933 }
934
935 if (SCCReturnsNonNull) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000936 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000937 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
938 Attribute::NonNull) ||
939 !F->getReturnType()->isPointerTy())
940 continue;
941
942 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
943 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
944 ++NumNonNullReturn;
945 MadeChange = true;
946 }
947 }
948
949 return MadeChange;
950}
951
Chandler Carruthd0245202015-09-13 07:50:43 +0000952static void setDoesNotAccessMemory(Function &F) {
953 if (!F.doesNotAccessMemory()) {
954 F.setDoesNotAccessMemory();
955 ++NumAnnotated;
956 }
957}
958
959static void setOnlyReadsMemory(Function &F) {
960 if (!F.onlyReadsMemory()) {
961 F.setOnlyReadsMemory();
962 ++NumAnnotated;
963 }
964}
965
966static void setDoesNotThrow(Function &F) {
967 if (!F.doesNotThrow()) {
968 F.setDoesNotThrow();
969 ++NumAnnotated;
970 }
971}
972
973static void setDoesNotCapture(Function &F, unsigned n) {
974 if (!F.doesNotCapture(n)) {
975 F.setDoesNotCapture(n);
976 ++NumAnnotated;
977 }
978}
979
980static void setOnlyReadsMemory(Function &F, unsigned n) {
981 if (!F.onlyReadsMemory(n)) {
982 F.setOnlyReadsMemory(n);
983 ++NumAnnotated;
984 }
985}
986
987static void setDoesNotAlias(Function &F, unsigned n) {
988 if (!F.doesNotAlias(n)) {
989 F.setDoesNotAlias(n);
990 ++NumAnnotated;
991 }
992}
993
James Molloy7e9bdd52015-11-12 10:55:20 +0000994static bool setDoesNotRecurse(Function &F) {
995 if (F.doesNotRecurse())
996 return false;
997 F.setDoesNotRecurse();
998 ++NumNoRecurse;
999 return true;
1000}
1001
Chandler Carrutha632fb92015-09-13 06:57:25 +00001002/// Analyze the name and prototype of the given function and set any applicable
1003/// attributes.
1004///
1005/// Returns true if any attributes were set and false otherwise.
Chandler Carruth444d0052015-09-13 08:03:23 +00001006static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
Chandler Carruth0fb99812014-08-13 10:49:33 +00001007 if (F.hasFnAttribute(Attribute::OptimizeNone))
1008 return false;
1009
Meador Inge6b6a1612013-03-21 00:55:59 +00001010 FunctionType *FTy = F.getFunctionType();
1011 LibFunc::Func TheLibFunc;
Chandler Carruth444d0052015-09-13 08:03:23 +00001012 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
Meador Inge6b6a1612013-03-21 00:55:59 +00001013 return false;
1014
1015 switch (TheLibFunc) {
1016 case LibFunc::strlen:
1017 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1018 return false;
1019 setOnlyReadsMemory(F);
1020 setDoesNotThrow(F);
1021 setDoesNotCapture(F, 1);
1022 break;
1023 case LibFunc::strchr:
1024 case LibFunc::strrchr:
Chandler Carruth63559d72015-09-13 06:47:20 +00001025 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001026 !FTy->getParamType(1)->isIntegerTy())
1027 return false;
1028 setOnlyReadsMemory(F);
1029 setDoesNotThrow(F);
1030 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001031 case LibFunc::strtol:
1032 case LibFunc::strtod:
1033 case LibFunc::strtof:
1034 case LibFunc::strtoul:
1035 case LibFunc::strtoll:
1036 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +00001037 case LibFunc::strtoull:
Chandler Carruth63559d72015-09-13 06:47:20 +00001038 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001039 return false;
1040 setDoesNotThrow(F);
1041 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001042 setOnlyReadsMemory(F, 1);
1043 break;
1044 case LibFunc::strcpy:
1045 case LibFunc::stpcpy:
1046 case LibFunc::strcat:
1047 case LibFunc::strncat:
1048 case LibFunc::strncpy:
1049 case LibFunc::stpncpy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001050 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001051 return false;
1052 setDoesNotThrow(F);
1053 setDoesNotCapture(F, 2);
1054 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001055 break;
1056 case LibFunc::strxfrm:
Chandler Carruth63559d72015-09-13 06:47:20 +00001057 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001058 !FTy->getParamType(1)->isPointerTy())
1059 return false;
1060 setDoesNotThrow(F);
1061 setDoesNotCapture(F, 1);
1062 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001063 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001064 break;
Chandler Carruth63559d72015-09-13 06:47:20 +00001065 case LibFunc::strcmp: // 0,1
1066 case LibFunc::strspn: // 0,1
1067 case LibFunc::strncmp: // 0,1
1068 case LibFunc::strcspn: // 0,1
1069 case LibFunc::strcoll: // 0,1
1070 case LibFunc::strcasecmp: // 0,1
1071 case LibFunc::strncasecmp: //
1072 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001073 !FTy->getParamType(1)->isPointerTy())
1074 return false;
1075 setOnlyReadsMemory(F);
1076 setDoesNotThrow(F);
1077 setDoesNotCapture(F, 1);
1078 setDoesNotCapture(F, 2);
1079 break;
1080 case LibFunc::strstr:
1081 case LibFunc::strpbrk:
1082 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1083 return false;
1084 setOnlyReadsMemory(F);
1085 setDoesNotThrow(F);
1086 setDoesNotCapture(F, 2);
1087 break;
1088 case LibFunc::strtok:
1089 case LibFunc::strtok_r:
1090 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1091 return false;
1092 setDoesNotThrow(F);
1093 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001094 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001095 break;
1096 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001097 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1098 return false;
1099 setDoesNotThrow(F);
1100 setDoesNotCapture(F, 1);
1101 setOnlyReadsMemory(F, 1);
1102 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001103 case LibFunc::setbuf:
1104 case LibFunc::setvbuf:
1105 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1106 return false;
1107 setDoesNotThrow(F);
1108 setDoesNotCapture(F, 1);
1109 break;
1110 case LibFunc::strdup:
1111 case LibFunc::strndup:
1112 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1113 !FTy->getParamType(0)->isPointerTy())
1114 return false;
1115 setDoesNotThrow(F);
1116 setDoesNotAlias(F, 0);
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::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +00001121 case LibFunc::statvfs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001122 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001123 !FTy->getParamType(1)->isPointerTy())
1124 return false;
1125 setDoesNotThrow(F);
1126 setDoesNotCapture(F, 1);
1127 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001128 setOnlyReadsMemory(F, 1);
1129 break;
1130 case LibFunc::sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001131 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001132 !FTy->getParamType(1)->isPointerTy())
1133 return false;
1134 setDoesNotThrow(F);
1135 setDoesNotCapture(F, 1);
1136 setDoesNotCapture(F, 2);
1137 setOnlyReadsMemory(F, 1);
1138 setOnlyReadsMemory(F, 2);
1139 break;
1140 case LibFunc::sprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001141 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001142 !FTy->getParamType(1)->isPointerTy())
1143 return false;
1144 setDoesNotThrow(F);
1145 setDoesNotCapture(F, 1);
1146 setDoesNotCapture(F, 2);
1147 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001148 break;
1149 case LibFunc::snprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001150 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001151 !FTy->getParamType(2)->isPointerTy())
1152 return false;
1153 setDoesNotThrow(F);
1154 setDoesNotCapture(F, 1);
1155 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001156 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001157 break;
1158 case LibFunc::setitimer:
Chandler Carruth63559d72015-09-13 06:47:20 +00001159 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001160 !FTy->getParamType(2)->isPointerTy())
1161 return false;
1162 setDoesNotThrow(F);
1163 setDoesNotCapture(F, 2);
1164 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001165 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001166 break;
1167 case LibFunc::system:
Chandler Carruth63559d72015-09-13 06:47:20 +00001168 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001169 return false;
1170 // May throw; "system" is a valid pthread cancellation point.
1171 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001172 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001173 break;
1174 case LibFunc::malloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001175 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001176 return false;
1177 setDoesNotThrow(F);
1178 setDoesNotAlias(F, 0);
1179 break;
1180 case LibFunc::memcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001181 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001182 !FTy->getParamType(1)->isPointerTy())
1183 return false;
1184 setOnlyReadsMemory(F);
1185 setDoesNotThrow(F);
1186 setDoesNotCapture(F, 1);
1187 setDoesNotCapture(F, 2);
1188 break;
1189 case LibFunc::memchr:
1190 case LibFunc::memrchr:
1191 if (FTy->getNumParams() != 3)
1192 return false;
1193 setOnlyReadsMemory(F);
1194 setDoesNotThrow(F);
1195 break;
1196 case LibFunc::modf:
1197 case LibFunc::modff:
1198 case LibFunc::modfl:
Chandler Carruth63559d72015-09-13 06:47:20 +00001199 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001200 return false;
1201 setDoesNotThrow(F);
1202 setDoesNotCapture(F, 2);
1203 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001204 case LibFunc::memcpy:
1205 case LibFunc::memccpy:
1206 case LibFunc::memmove:
Chandler Carruth63559d72015-09-13 06:47:20 +00001207 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001208 return false;
1209 setDoesNotThrow(F);
1210 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001211 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001212 break;
1213 case LibFunc::memalign:
1214 if (!FTy->getReturnType()->isPointerTy())
1215 return false;
1216 setDoesNotAlias(F, 0);
1217 break;
1218 case LibFunc::mkdir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001219 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001220 return false;
1221 setDoesNotThrow(F);
1222 setDoesNotCapture(F, 1);
1223 setOnlyReadsMemory(F, 1);
1224 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001225 case LibFunc::mktime:
Chandler Carruth63559d72015-09-13 06:47:20 +00001226 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001227 return false;
1228 setDoesNotThrow(F);
1229 setDoesNotCapture(F, 1);
1230 break;
1231 case LibFunc::realloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001232 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001233 !FTy->getReturnType()->isPointerTy())
1234 return false;
1235 setDoesNotThrow(F);
1236 setDoesNotAlias(F, 0);
1237 setDoesNotCapture(F, 1);
1238 break;
1239 case LibFunc::read:
Chandler Carruth63559d72015-09-13 06:47:20 +00001240 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001241 return false;
1242 // May throw; "read" is a valid pthread cancellation point.
1243 setDoesNotCapture(F, 2);
1244 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001245 case LibFunc::rewind:
Chandler Carruth63559d72015-09-13 06:47:20 +00001246 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001247 return false;
1248 setDoesNotThrow(F);
1249 setDoesNotCapture(F, 1);
1250 break;
1251 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001252 case LibFunc::remove:
1253 case LibFunc::realpath:
Chandler Carruth63559d72015-09-13 06:47:20 +00001254 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001255 return false;
1256 setDoesNotThrow(F);
1257 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001258 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001259 break;
1260 case LibFunc::rename:
Chandler Carruth63559d72015-09-13 06:47:20 +00001261 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001262 !FTy->getParamType(1)->isPointerTy())
1263 return false;
1264 setDoesNotThrow(F);
1265 setDoesNotCapture(F, 1);
1266 setDoesNotCapture(F, 2);
1267 setOnlyReadsMemory(F, 1);
1268 setOnlyReadsMemory(F, 2);
1269 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001270 case LibFunc::readlink:
Chandler Carruth63559d72015-09-13 06:47:20 +00001271 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001272 !FTy->getParamType(1)->isPointerTy())
1273 return false;
1274 setDoesNotThrow(F);
1275 setDoesNotCapture(F, 1);
1276 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001277 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001278 break;
1279 case LibFunc::write:
1280 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1281 return false;
1282 // May throw; "write" is a valid pthread cancellation point.
1283 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001284 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001285 break;
1286 case LibFunc::bcopy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001287 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001288 !FTy->getParamType(1)->isPointerTy())
1289 return false;
1290 setDoesNotThrow(F);
1291 setDoesNotCapture(F, 1);
1292 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001293 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001294 break;
1295 case LibFunc::bcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001296 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001297 !FTy->getParamType(1)->isPointerTy())
1298 return false;
1299 setDoesNotThrow(F);
1300 setOnlyReadsMemory(F);
1301 setDoesNotCapture(F, 1);
1302 setDoesNotCapture(F, 2);
1303 break;
1304 case LibFunc::bzero:
1305 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1306 return false;
1307 setDoesNotThrow(F);
1308 setDoesNotCapture(F, 1);
1309 break;
1310 case LibFunc::calloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001311 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001312 return false;
1313 setDoesNotThrow(F);
1314 setDoesNotAlias(F, 0);
1315 break;
1316 case LibFunc::chmod:
1317 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001318 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1319 return false;
1320 setDoesNotThrow(F);
1321 setDoesNotCapture(F, 1);
1322 setOnlyReadsMemory(F, 1);
1323 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001324 case LibFunc::ctermid:
1325 case LibFunc::clearerr:
1326 case LibFunc::closedir:
1327 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1328 return false;
1329 setDoesNotThrow(F);
1330 setDoesNotCapture(F, 1);
1331 break;
1332 case LibFunc::atoi:
1333 case LibFunc::atol:
1334 case LibFunc::atof:
1335 case LibFunc::atoll:
1336 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1337 return false;
1338 setDoesNotThrow(F);
1339 setOnlyReadsMemory(F);
1340 setDoesNotCapture(F, 1);
1341 break;
1342 case LibFunc::access:
1343 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1344 return false;
1345 setDoesNotThrow(F);
1346 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001347 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001348 break;
1349 case LibFunc::fopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001350 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001351 !FTy->getParamType(0)->isPointerTy() ||
1352 !FTy->getParamType(1)->isPointerTy())
1353 return false;
1354 setDoesNotThrow(F);
1355 setDoesNotAlias(F, 0);
1356 setDoesNotCapture(F, 1);
1357 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001358 setOnlyReadsMemory(F, 1);
1359 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001360 break;
1361 case LibFunc::fdopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001362 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001363 !FTy->getParamType(1)->isPointerTy())
1364 return false;
1365 setDoesNotThrow(F);
1366 setDoesNotAlias(F, 0);
1367 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001368 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001369 break;
1370 case LibFunc::feof:
1371 case LibFunc::free:
1372 case LibFunc::fseek:
1373 case LibFunc::ftell:
1374 case LibFunc::fgetc:
1375 case LibFunc::fseeko:
1376 case LibFunc::ftello:
1377 case LibFunc::fileno:
1378 case LibFunc::fflush:
1379 case LibFunc::fclose:
1380 case LibFunc::fsetpos:
1381 case LibFunc::flockfile:
1382 case LibFunc::funlockfile:
1383 case LibFunc::ftrylockfile:
1384 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1385 return false;
1386 setDoesNotThrow(F);
1387 setDoesNotCapture(F, 1);
1388 break;
1389 case LibFunc::ferror:
1390 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1391 return false;
1392 setDoesNotThrow(F);
1393 setDoesNotCapture(F, 1);
1394 setOnlyReadsMemory(F);
1395 break;
1396 case LibFunc::fputc:
1397 case LibFunc::fstat:
1398 case LibFunc::frexp:
1399 case LibFunc::frexpf:
1400 case LibFunc::frexpl:
1401 case LibFunc::fstatvfs:
1402 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1403 return false;
1404 setDoesNotThrow(F);
1405 setDoesNotCapture(F, 2);
1406 break;
1407 case LibFunc::fgets:
Chandler Carruth63559d72015-09-13 06:47:20 +00001408 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001409 !FTy->getParamType(2)->isPointerTy())
1410 return false;
1411 setDoesNotThrow(F);
1412 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001413 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001414 case LibFunc::fread:
Chandler Carruth63559d72015-09-13 06:47:20 +00001415 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001416 !FTy->getParamType(3)->isPointerTy())
1417 return false;
1418 setDoesNotThrow(F);
1419 setDoesNotCapture(F, 1);
1420 setDoesNotCapture(F, 4);
1421 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001422 case LibFunc::fwrite:
Chandler Carruth63559d72015-09-13 06:47:20 +00001423 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001424 !FTy->getParamType(3)->isPointerTy())
1425 return false;
1426 setDoesNotThrow(F);
1427 setDoesNotCapture(F, 1);
1428 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001429 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001430 case LibFunc::fputs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001431 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001432 !FTy->getParamType(1)->isPointerTy())
1433 return false;
1434 setDoesNotThrow(F);
1435 setDoesNotCapture(F, 1);
1436 setDoesNotCapture(F, 2);
1437 setOnlyReadsMemory(F, 1);
1438 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001439 case LibFunc::fscanf:
1440 case LibFunc::fprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001441 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001442 !FTy->getParamType(1)->isPointerTy())
1443 return false;
1444 setDoesNotThrow(F);
1445 setDoesNotCapture(F, 1);
1446 setDoesNotCapture(F, 2);
1447 setOnlyReadsMemory(F, 2);
1448 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001449 case LibFunc::fgetpos:
Chandler Carruth63559d72015-09-13 06:47:20 +00001450 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001451 !FTy->getParamType(1)->isPointerTy())
1452 return false;
1453 setDoesNotThrow(F);
1454 setDoesNotCapture(F, 1);
1455 setDoesNotCapture(F, 2);
1456 break;
1457 case LibFunc::getc:
1458 case LibFunc::getlogin_r:
1459 case LibFunc::getc_unlocked:
1460 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1461 return false;
1462 setDoesNotThrow(F);
1463 setDoesNotCapture(F, 1);
1464 break;
1465 case LibFunc::getenv:
1466 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1467 return false;
1468 setDoesNotThrow(F);
1469 setOnlyReadsMemory(F);
1470 setDoesNotCapture(F, 1);
1471 break;
1472 case LibFunc::gets:
1473 case LibFunc::getchar:
1474 setDoesNotThrow(F);
1475 break;
1476 case LibFunc::getitimer:
1477 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1478 return false;
1479 setDoesNotThrow(F);
1480 setDoesNotCapture(F, 2);
1481 break;
1482 case LibFunc::getpwnam:
1483 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1484 return false;
1485 setDoesNotThrow(F);
1486 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001487 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001488 break;
1489 case LibFunc::ungetc:
1490 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1491 return false;
1492 setDoesNotThrow(F);
1493 setDoesNotCapture(F, 2);
1494 break;
1495 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001496 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1497 return false;
1498 setDoesNotThrow(F);
1499 setDoesNotCapture(F, 1);
1500 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001501 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001502 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1503 return false;
1504 setDoesNotThrow(F);
1505 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001506 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001507 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001508 case LibFunc::unsetenv:
1509 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1510 return false;
1511 setDoesNotThrow(F);
1512 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001513 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001514 break;
1515 case LibFunc::utime:
1516 case LibFunc::utimes:
Chandler Carruth63559d72015-09-13 06:47:20 +00001517 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001518 !FTy->getParamType(1)->isPointerTy())
1519 return false;
1520 setDoesNotThrow(F);
1521 setDoesNotCapture(F, 1);
1522 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001523 setOnlyReadsMemory(F, 1);
1524 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001525 break;
1526 case LibFunc::putc:
1527 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1528 return false;
1529 setDoesNotThrow(F);
1530 setDoesNotCapture(F, 2);
1531 break;
1532 case LibFunc::puts:
1533 case LibFunc::printf:
1534 case LibFunc::perror:
1535 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1536 return false;
1537 setDoesNotThrow(F);
1538 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001539 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001540 break;
1541 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001542 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1543 return false;
1544 // May throw; "pread" is a valid pthread cancellation point.
1545 setDoesNotCapture(F, 2);
1546 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001547 case LibFunc::pwrite:
1548 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1549 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001550 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001551 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001552 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001553 break;
1554 case LibFunc::putchar:
1555 setDoesNotThrow(F);
1556 break;
1557 case LibFunc::popen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001558 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001559 !FTy->getParamType(0)->isPointerTy() ||
1560 !FTy->getParamType(1)->isPointerTy())
1561 return false;
1562 setDoesNotThrow(F);
1563 setDoesNotAlias(F, 0);
1564 setDoesNotCapture(F, 1);
1565 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001566 setOnlyReadsMemory(F, 1);
1567 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001568 break;
1569 case LibFunc::pclose:
1570 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1571 return false;
1572 setDoesNotThrow(F);
1573 setDoesNotCapture(F, 1);
1574 break;
1575 case LibFunc::vscanf:
1576 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1577 return false;
1578 setDoesNotThrow(F);
1579 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001580 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001581 break;
1582 case LibFunc::vsscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001583 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001584 !FTy->getParamType(2)->isPointerTy())
1585 return false;
1586 setDoesNotThrow(F);
1587 setDoesNotCapture(F, 1);
1588 setDoesNotCapture(F, 2);
1589 setOnlyReadsMemory(F, 1);
1590 setOnlyReadsMemory(F, 2);
1591 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001592 case LibFunc::vfscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001593 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001594 !FTy->getParamType(2)->isPointerTy())
1595 return false;
1596 setDoesNotThrow(F);
1597 setDoesNotCapture(F, 1);
1598 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001599 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001600 break;
1601 case LibFunc::valloc:
1602 if (!FTy->getReturnType()->isPointerTy())
1603 return false;
1604 setDoesNotThrow(F);
1605 setDoesNotAlias(F, 0);
1606 break;
1607 case LibFunc::vprintf:
1608 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1609 return false;
1610 setDoesNotThrow(F);
1611 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001612 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001613 break;
1614 case LibFunc::vfprintf:
1615 case LibFunc::vsprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001616 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001617 !FTy->getParamType(1)->isPointerTy())
1618 return false;
1619 setDoesNotThrow(F);
1620 setDoesNotCapture(F, 1);
1621 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001622 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001623 break;
1624 case LibFunc::vsnprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001625 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001626 !FTy->getParamType(2)->isPointerTy())
1627 return false;
1628 setDoesNotThrow(F);
1629 setDoesNotCapture(F, 1);
1630 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001631 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001632 break;
1633 case LibFunc::open:
1634 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1635 return false;
1636 // May throw; "open" is a valid pthread cancellation point.
1637 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001638 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001639 break;
1640 case LibFunc::opendir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001641 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001642 !FTy->getParamType(0)->isPointerTy())
1643 return false;
1644 setDoesNotThrow(F);
1645 setDoesNotAlias(F, 0);
1646 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001647 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001648 break;
1649 case LibFunc::tmpfile:
1650 if (!FTy->getReturnType()->isPointerTy())
1651 return false;
1652 setDoesNotThrow(F);
1653 setDoesNotAlias(F, 0);
1654 break;
1655 case LibFunc::times:
1656 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1657 return false;
1658 setDoesNotThrow(F);
1659 setDoesNotCapture(F, 1);
1660 break;
1661 case LibFunc::htonl:
1662 case LibFunc::htons:
1663 case LibFunc::ntohl:
1664 case LibFunc::ntohs:
1665 setDoesNotThrow(F);
1666 setDoesNotAccessMemory(F);
1667 break;
1668 case LibFunc::lstat:
Chandler Carruth63559d72015-09-13 06:47:20 +00001669 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001670 !FTy->getParamType(1)->isPointerTy())
1671 return false;
1672 setDoesNotThrow(F);
1673 setDoesNotCapture(F, 1);
1674 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001675 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001676 break;
1677 case LibFunc::lchown:
1678 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1679 return false;
1680 setDoesNotThrow(F);
1681 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001682 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001683 break;
1684 case LibFunc::qsort:
1685 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1686 return false;
1687 // May throw; places call through function pointer.
1688 setDoesNotCapture(F, 4);
1689 break;
1690 case LibFunc::dunder_strdup:
1691 case LibFunc::dunder_strndup:
Chandler Carruth63559d72015-09-13 06:47:20 +00001692 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001693 !FTy->getParamType(0)->isPointerTy())
1694 return false;
1695 setDoesNotThrow(F);
1696 setDoesNotAlias(F, 0);
1697 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001698 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001699 break;
1700 case LibFunc::dunder_strtok_r:
Chandler Carruth63559d72015-09-13 06:47:20 +00001701 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001702 return false;
1703 setDoesNotThrow(F);
1704 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001705 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001706 break;
1707 case LibFunc::under_IO_getc:
1708 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1709 return false;
1710 setDoesNotThrow(F);
1711 setDoesNotCapture(F, 1);
1712 break;
1713 case LibFunc::under_IO_putc:
1714 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1715 return false;
1716 setDoesNotThrow(F);
1717 setDoesNotCapture(F, 2);
1718 break;
1719 case LibFunc::dunder_isoc99_scanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001720 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001721 return false;
1722 setDoesNotThrow(F);
1723 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001724 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001725 break;
1726 case LibFunc::stat64:
1727 case LibFunc::lstat64:
1728 case LibFunc::statvfs64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001729 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001730 !FTy->getParamType(1)->isPointerTy())
1731 return false;
1732 setDoesNotThrow(F);
1733 setDoesNotCapture(F, 1);
1734 setDoesNotCapture(F, 2);
1735 setOnlyReadsMemory(F, 1);
1736 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001737 case LibFunc::dunder_isoc99_sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001738 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001739 !FTy->getParamType(1)->isPointerTy())
1740 return false;
1741 setDoesNotThrow(F);
1742 setDoesNotCapture(F, 1);
1743 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001744 setOnlyReadsMemory(F, 1);
1745 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001746 break;
1747 case LibFunc::fopen64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001748 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001749 !FTy->getParamType(0)->isPointerTy() ||
1750 !FTy->getParamType(1)->isPointerTy())
1751 return false;
1752 setDoesNotThrow(F);
1753 setDoesNotAlias(F, 0);
1754 setDoesNotCapture(F, 1);
1755 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001756 setOnlyReadsMemory(F, 1);
1757 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001758 break;
1759 case LibFunc::fseeko64:
1760 case LibFunc::ftello64:
1761 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1762 return false;
1763 setDoesNotThrow(F);
1764 setDoesNotCapture(F, 1);
1765 break;
1766 case LibFunc::tmpfile64:
1767 if (!FTy->getReturnType()->isPointerTy())
1768 return false;
1769 setDoesNotThrow(F);
1770 setDoesNotAlias(F, 0);
1771 break;
1772 case LibFunc::fstat64:
1773 case LibFunc::fstatvfs64:
1774 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1775 return false;
1776 setDoesNotThrow(F);
1777 setDoesNotCapture(F, 2);
1778 break;
1779 case LibFunc::open64:
1780 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1781 return false;
1782 // May throw; "open" is a valid pthread cancellation point.
1783 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001784 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001785 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001786 case LibFunc::gettimeofday:
1787 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1788 !FTy->getParamType(1)->isPointerTy())
1789 return false;
1790 // Currently some platforms have the restrict keyword on the arguments to
1791 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1792 // arguments.
1793 setDoesNotThrow(F);
1794 setDoesNotCapture(F, 1);
1795 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001796 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001797 default:
1798 // Didn't mark any attributes.
1799 return false;
1800 }
1801
1802 return true;
1803}
1804
James Molloy7e9bdd52015-11-12 10:55:20 +00001805static bool addNoRecurseAttrs(const CallGraphSCC &SCC,
1806 SmallVectorImpl<WeakVH> &Revisit) {
1807 // Try and identify functions that do not recurse.
1808
1809 // If the SCC contains multiple nodes we know for sure there is recursion.
1810 if (!SCC.isSingular())
1811 return false;
1812
1813 const CallGraphNode *CGN = *SCC.begin();
1814 Function *F = CGN->getFunction();
1815 if (!F || F->isDeclaration() || F->doesNotRecurse())
1816 return false;
1817
1818 // If all of the calls in F are identifiable and are to norecurse functions, F
1819 // is norecurse. This check also detects self-recursion as F is not currently
1820 // marked norecurse, so any called from F to F will not be marked norecurse.
1821 if (std::all_of(CGN->begin(), CGN->end(),
1822 [](const CallGraphNode::CallRecord &CR) {
1823 Function *F = CR.second->getFunction();
1824 return F && F->doesNotRecurse();
1825 }))
1826 // Function calls a potentially recursive function.
1827 return setDoesNotRecurse(*F);
1828
1829 // We know that F is not obviously recursive, but we haven't been able to
1830 // prove that it doesn't actually recurse. Add it to the Revisit list to try
1831 // again top-down later.
1832 Revisit.push_back(F);
1833 return false;
1834}
1835
1836static bool addNoRecurseAttrsTopDownOnly(Function *F) {
1837 // If F is internal and all uses are in norecurse functions, then F is also
1838 // norecurse.
1839 if (F->doesNotRecurse())
1840 return false;
1841 if (F->hasInternalLinkage()) {
1842 for (auto *U : F->users())
1843 if (auto *I = dyn_cast<Instruction>(U)) {
1844 if (!I->getParent()->getParent()->doesNotRecurse())
1845 return false;
1846 } else {
1847 return false;
1848 }
1849 return setDoesNotRecurse(*F);
1850 }
1851 return false;
1852}
1853
James Molloy0ecdbe72015-11-19 08:49:57 +00001854static Attribute::AttrKind parseAttrKind(StringRef Kind) {
1855 return StringSwitch<Attribute::AttrKind>(Kind)
1856 .Case("alwaysinline", Attribute::AlwaysInline)
1857 .Case("builtin", Attribute::Builtin)
1858 .Case("cold", Attribute::Cold)
1859 .Case("convergent", Attribute::Convergent)
1860 .Case("inlinehint", Attribute::InlineHint)
1861 .Case("jumptable", Attribute::JumpTable)
1862 .Case("minsize", Attribute::MinSize)
1863 .Case("naked", Attribute::Naked)
1864 .Case("nobuiltin", Attribute::NoBuiltin)
1865 .Case("noduplicate", Attribute::NoDuplicate)
1866 .Case("noimplicitfloat", Attribute::NoImplicitFloat)
1867 .Case("noinline", Attribute::NoInline)
1868 .Case("nonlazybind", Attribute::NonLazyBind)
1869 .Case("noredzone", Attribute::NoRedZone)
1870 .Case("noreturn", Attribute::NoReturn)
1871 .Case("norecurse", Attribute::NoRecurse)
1872 .Case("nounwind", Attribute::NoUnwind)
1873 .Case("optnone", Attribute::OptimizeNone)
1874 .Case("optsize", Attribute::OptimizeForSize)
1875 .Case("readnone", Attribute::ReadNone)
1876 .Case("readonly", Attribute::ReadOnly)
1877 .Case("argmemonly", Attribute::ArgMemOnly)
1878 .Case("returns_twice", Attribute::ReturnsTwice)
1879 .Case("safestack", Attribute::SafeStack)
1880 .Case("sanitize_address", Attribute::SanitizeAddress)
1881 .Case("sanitize_memory", Attribute::SanitizeMemory)
1882 .Case("sanitize_thread", Attribute::SanitizeThread)
1883 .Case("ssp", Attribute::StackProtect)
1884 .Case("sspreq", Attribute::StackProtectReq)
1885 .Case("sspstrong", Attribute::StackProtectStrong)
1886 .Case("uwtable", Attribute::UWTable)
1887 .Default(Attribute::None);
1888}
1889
1890/// If F has any forced attributes given on the command line, add them.
1891static bool addForcedAttributes(Function *F) {
1892 bool Changed = false;
1893 for (auto &S : ForceAttributes) {
1894 auto KV = StringRef(S).split(':');
1895 if (KV.first != F->getName())
1896 continue;
1897
1898 auto Kind = parseAttrKind(KV.second);
1899 if (Kind == Attribute::None) {
1900 DEBUG(dbgs() << "ForcedAttribute: " << KV.second
1901 << " unknown or not handled!\n");
1902 continue;
1903 }
1904 if (F->hasFnAttribute(Kind))
1905 continue;
1906 Changed = true;
1907 F->addFnAttr(Kind);
1908 }
1909 return Changed;
1910}
1911
Chris Lattner4422d312010-04-16 22:42:17 +00001912bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001913 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Chandler Carruthcada2d82015-10-31 00:28:37 +00001914 bool Changed = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001915
Chandler Carrutha8125352015-10-30 16:48:08 +00001916 // We compute dedicated AA results for each function in the SCC as needed. We
1917 // use a lambda referencing external objects so that they live long enough to
1918 // be queried, but we re-use them each time.
1919 Optional<BasicAAResult> BAR;
1920 Optional<AAResults> AAR;
1921 auto AARGetter = [&](Function &F) -> AAResults & {
1922 BAR.emplace(createLegacyPMBasicAAResult(*this, F));
1923 AAR.emplace(createLegacyPMAAResults(*this, F, *BAR));
1924 return *AAR;
1925 };
1926
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001927 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1928 // whether a given CallGraphNode is in this SCC. Also track whether there are
1929 // any external or opt-none nodes that will prevent us from optimizing any
1930 // part of the SCC.
1931 SCCNodeSet SCCNodes;
1932 bool ExternalNode = false;
1933 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1934 Function *F = (*I)->getFunction();
1935 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1936 // External node or function we're trying not to optimize - we both avoid
1937 // transform them and avoid leveraging information they provide.
1938 ExternalNode = true;
1939 continue;
1940 }
1941
Chandler Carruthcada2d82015-10-31 00:28:37 +00001942 // When initially processing functions, also infer their prototype
1943 // attributes if they are declarations.
1944 if (F->isDeclaration())
1945 Changed |= inferPrototypeAttributes(*F, *TLI);
1946
James Molloy0ecdbe72015-11-19 08:49:57 +00001947 Changed |= addForcedAttributes(F);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001948 SCCNodes.insert(F);
1949 }
1950
Chandler Carrutha8125352015-10-30 16:48:08 +00001951 Changed |= addReadAttrs(SCCNodes, AARGetter);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001952 Changed |= addArgumentAttrs(SCCNodes);
1953
1954 // If we have no external nodes participating in the SCC, we can infer some
1955 // more precise attributes as well.
1956 if (!ExternalNode) {
1957 Changed |= addNoAliasAttrs(SCCNodes);
1958 Changed |= addNonNullAttrs(SCCNodes, *TLI);
1959 }
James Molloy7e9bdd52015-11-12 10:55:20 +00001960
1961 Changed |= addNoRecurseAttrs(SCC, Revisit);
1962 return Changed;
1963}
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001964
James Molloy7e9bdd52015-11-12 10:55:20 +00001965bool FunctionAttrs::doFinalization(CallGraph &CG) {
1966 bool Changed = false;
1967 // When iterating over SCCs we visit functions in a bottom-up fashion. Some of
1968 // the rules we have for identifying norecurse functions work best with a
1969 // top-down walk, so look again at all the functions we previously marked as
1970 // worth revisiting, in top-down order.
1971 for (auto &F : reverse(Revisit))
1972 if (F)
1973 Changed |= addNoRecurseAttrsTopDownOnly(cast<Function>((Value*)F));
Duncan Sands44c8cd92008-12-31 16:14:43 +00001974 return Changed;
1975}