blob: 6c357ed03a805be0b5a8d6c5928417dce7da2a01 [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 Carruth63559d72015-09-13 06:47:20 +000055struct FunctionAttrs : public CallGraphSCCPass {
56 static char ID; // Pass identification, replacement for typeid
57 FunctionAttrs() : CallGraphSCCPass(ID) {
58 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
59 }
60
Chandler Carruth63559d72015-09-13 06:47:20 +000061 bool runOnSCC(CallGraphSCC &SCC) override;
62
Chandler Carruth63559d72015-09-13 06:47:20 +000063 void getAnalysisUsage(AnalysisUsage &AU) const override {
64 AU.setPreservesCFG();
65 AU.addRequired<AssumptionCacheTracker>();
66 AU.addRequired<TargetLibraryInfoWrapperPass>();
67 CallGraphSCCPass::getAnalysisUsage(AU);
68 }
Meador Inge6b6a1612013-03-21 00:55:59 +000069
Chandler Carruth63559d72015-09-13 06:47:20 +000070private:
71 TargetLibraryInfo *TLI;
Chandler Carruthd0245202015-09-13 07:50:43 +000072
73 bool AddReadAttrs(const CallGraphSCC &SCC);
74 bool AddArgumentAttrs(const CallGraphSCC &SCC);
Chandler Carruthd0245202015-09-13 07:50:43 +000075 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Chandler Carruthd0245202015-09-13 07:50:43 +000076 bool AddNonNullAttrs(const CallGraphSCC &SCC);
Chandler Carruthd0245202015-09-13 07:50:43 +000077 bool annotateLibraryCalls(const CallGraphSCC &SCC);
Chandler Carruth63559d72015-09-13 06:47:20 +000078};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000079}
Duncan Sands44c8cd92008-12-31 16:14:43 +000080
81char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000082INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +000083 "Deduce function attributes", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +000084INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +000085INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000086INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +000087INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +000088 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +000089
90Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
91
Chandler Carruth7542d372015-09-21 17:39:41 +000092namespace {
93/// The three kinds of memory access relevant to 'readonly' and
94/// 'readnone' attributes.
95enum MemoryAccessKind {
96 MAK_ReadNone = 0,
97 MAK_ReadOnly = 1,
98 MAK_MayWrite = 2
99};
100}
101
102static MemoryAccessKind
103checkFunctionMemoryAccess(Function &F, AAResults &AAR,
104 const SmallPtrSetImpl<Function *> &SCCNodes) {
105 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
106 if (MRB == FMRB_DoesNotAccessMemory)
107 // Already perfect!
108 return MAK_ReadNone;
109
110 // Definitions with weak linkage may be overridden at linktime with
111 // something that writes memory, so treat them like declarations.
112 if (F.isDeclaration() || F.mayBeOverridden()) {
113 if (AliasAnalysis::onlyReadsMemory(MRB))
114 return MAK_ReadOnly;
115
116 // Conservatively assume it writes to memory.
117 return MAK_MayWrite;
118 }
119
120 // Scan the function body for instructions that may read or write memory.
121 bool ReadsMemory = false;
122 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
123 Instruction *I = &*II;
124
125 // Some instructions can be ignored even if they read or write memory.
126 // Detect these now, skipping to the next instruction if one is found.
127 CallSite CS(cast<Value>(I));
128 if (CS) {
129 // Ignore calls to functions in the same SCC.
130 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
131 continue;
132 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
Chandler Carruth7542d372015-09-21 17:39:41 +0000133
Chandler Carruth69798fb2015-10-27 01:41:43 +0000134 // If the call doesn't access memory, we're done.
135 if (!(MRB & MRI_ModRef))
136 continue;
137
138 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
139 // The call could access any memory. If that includes writes, give up.
140 if (MRB & MRI_Mod)
141 return MAK_MayWrite;
142 // If it reads, note it.
143 if (MRB & MRI_Ref)
144 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000145 continue;
146 }
Chandler Carruth69798fb2015-10-27 01:41:43 +0000147
148 // Check whether all pointer arguments point to local memory, and
149 // ignore calls that only access local memory.
150 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
151 CI != CE; ++CI) {
152 Value *Arg = *CI;
153 if (!Arg->getType()->isPointerTy())
154 continue;
155
156 AAMDNodes AAInfo;
157 I->getAAMetadata(AAInfo);
158 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
159
160 // Skip accesses to local or constant memory as they don't impact the
161 // externally visible mod/ref behavior.
162 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
163 continue;
164
165 if (MRB & MRI_Mod)
166 // Writes non-local memory. Give up.
167 return MAK_MayWrite;
168 if (MRB & MRI_Ref)
169 // Ok, it reads non-local memory.
170 ReadsMemory = true;
171 }
Chandler Carruth7542d372015-09-21 17:39:41 +0000172 continue;
173 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
174 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
175 if (!LI->isVolatile()) {
176 MemoryLocation Loc = MemoryLocation::get(LI);
177 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
178 continue;
179 }
180 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
181 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
182 if (!SI->isVolatile()) {
183 MemoryLocation Loc = MemoryLocation::get(SI);
184 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
185 continue;
186 }
187 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
188 // Ignore vaargs on local memory.
189 MemoryLocation Loc = MemoryLocation::get(VI);
190 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
191 continue;
192 }
193
194 // Any remaining instructions need to be taken seriously! Check if they
195 // read or write memory.
196 if (I->mayWriteToMemory())
197 // Writes memory. Just give up.
198 return MAK_MayWrite;
199
200 // If this instruction may read memory, remember that.
201 ReadsMemory |= I->mayReadFromMemory();
202 }
203
204 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
205}
206
Chandler Carrutha632fb92015-09-13 06:57:25 +0000207/// Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000208bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000209 SmallPtrSet<Function *, 8> SCCNodes;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000210
211 // Fill SCCNodes with the elements of the SCC. Used for quickly
212 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000213 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
214 SCCNodes.insert((*I)->getFunction());
Duncan Sands44c8cd92008-12-31 16:14:43 +0000215
216 // Check if any of the functions in the SCC read or write memory. If they
217 // write memory then they can't be marked readnone or readonly.
218 bool ReadsMemory = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000219 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
220 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000221
Chandler Carruth0fb99812014-08-13 10:49:33 +0000222 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
223 // External node or node we don't want to optimize - assume it may write
224 // memory and give up.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000225 return false;
226
Chandler Carruth7b560d42015-09-09 17:55:00 +0000227 // We need to manually construct BasicAA directly in order to disable its
228 // use of other function analyses.
229 BasicAAResult BAR(createLegacyPMBasicAAResult(*this, *F));
230
231 // Construct our own AA results for this function. We do this manually to
232 // work around the limitations of the legacy pass manager.
233 AAResults AAR(createLegacyPMAAResults(*this, *F, BAR));
234
Chandler Carruth7542d372015-09-21 17:39:41 +0000235 switch (checkFunctionMemoryAccess(*F, AAR, SCCNodes)) {
236 case MAK_MayWrite:
237 return false;
238 case MAK_ReadOnly:
Duncan Sands44c8cd92008-12-31 16:14:43 +0000239 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000240 break;
241 case MAK_ReadNone:
242 // Nothing to do!
243 break;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000244 }
245 }
246
247 // Success! Functions in this SCC do not access memory, or only read memory.
248 // Give them the appropriate attribute.
249 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000250 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
251 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000252
253 if (F->doesNotAccessMemory())
254 // Already perfect!
255 continue;
256
257 if (F->onlyReadsMemory() && ReadsMemory)
258 // No change.
259 continue;
260
261 MadeChange = true;
262
263 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000264 AttrBuilder B;
Chandler Carruth63559d72015-09-13 06:47:20 +0000265 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
266 F->removeAttributes(
267 AttributeSet::FunctionIndex,
268 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000269
270 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000271 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000272 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000273
274 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000275 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000276 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000277 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000278 }
279
280 return MadeChange;
281}
282
Nick Lewycky4c378a42011-12-28 23:24:21 +0000283namespace {
Chandler Carrutha632fb92015-09-13 06:57:25 +0000284/// For a given pointer Argument, this retains a list of Arguments of functions
285/// in the same SCC that the pointer data flows into. We use this to build an
286/// SCC of the arguments.
Chandler Carruth63559d72015-09-13 06:47:20 +0000287struct ArgumentGraphNode {
288 Argument *Definition;
289 SmallVector<ArgumentGraphNode *, 4> Uses;
290};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000291
Chandler Carruth63559d72015-09-13 06:47:20 +0000292class ArgumentGraph {
293 // We store pointers to ArgumentGraphNode objects, so it's important that
294 // that they not move around upon insert.
295 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000296
Chandler Carruth63559d72015-09-13 06:47:20 +0000297 ArgumentMapTy ArgumentMap;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000298
Chandler Carruth63559d72015-09-13 06:47:20 +0000299 // There is no root node for the argument graph, in fact:
300 // void f(int *x, int *y) { if (...) f(x, y); }
301 // is an example where the graph is disconnected. The SCCIterator requires a
302 // single entry point, so we maintain a fake ("synthetic") root node that
303 // uses every node. Because the graph is directed and nothing points into
304 // the root, it will not participate in any SCCs (except for its own).
305 ArgumentGraphNode SyntheticRoot;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000306
Chandler Carruth63559d72015-09-13 06:47:20 +0000307public:
308 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000309
Chandler Carruth63559d72015-09-13 06:47:20 +0000310 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000311
Chandler Carruth63559d72015-09-13 06:47:20 +0000312 iterator begin() { return SyntheticRoot.Uses.begin(); }
313 iterator end() { return SyntheticRoot.Uses.end(); }
314 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000315
Chandler Carruth63559d72015-09-13 06:47:20 +0000316 ArgumentGraphNode *operator[](Argument *A) {
317 ArgumentGraphNode &Node = ArgumentMap[A];
318 Node.Definition = A;
319 SyntheticRoot.Uses.push_back(&Node);
320 return &Node;
321 }
322};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000323
Chandler Carrutha632fb92015-09-13 06:57:25 +0000324/// This tracker checks whether callees are in the SCC, and if so it does not
325/// consider that a capture, instead adding it to the "Uses" list and
326/// continuing with the analysis.
Chandler Carruth63559d72015-09-13 06:47:20 +0000327struct ArgumentUsesTracker : public CaptureTracker {
328 ArgumentUsesTracker(const SmallPtrSet<Function *, 8> &SCCNodes)
Nick Lewycky4c378a42011-12-28 23:24:21 +0000329 : Captured(false), SCCNodes(SCCNodes) {}
330
Chandler Carruth63559d72015-09-13 06:47:20 +0000331 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000332
Chandler Carruth63559d72015-09-13 06:47:20 +0000333 bool captured(const Use *U) override {
334 CallSite CS(U->getUser());
335 if (!CS.getInstruction()) {
336 Captured = true;
337 return true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000338 }
339
Chandler Carruth63559d72015-09-13 06:47:20 +0000340 Function *F = CS.getCalledFunction();
341 if (!F || !SCCNodes.count(F)) {
342 Captured = true;
343 return true;
344 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000345
Chandler Carruth63559d72015-09-13 06:47:20 +0000346 bool Found = false;
347 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
348 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
349 PI != PE; ++PI, ++AI) {
350 if (AI == AE) {
351 assert(F->isVarArg() && "More params than args in non-varargs call");
352 Captured = true;
353 return true;
354 }
355 if (PI == U) {
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000356 Uses.push_back(&*AI);
Chandler Carruth63559d72015-09-13 06:47:20 +0000357 Found = true;
358 break;
359 }
360 }
361 assert(Found && "Capturing call-site captured nothing?");
362 (void)Found;
363 return false;
364 }
365
366 bool Captured; // True only if certainly captured (used outside our SCC).
367 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
368
369 const SmallPtrSet<Function *, 8> &SCCNodes;
370};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000371}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000372
373namespace llvm {
Chandler Carruth63559d72015-09-13 06:47:20 +0000374template <> struct GraphTraits<ArgumentGraphNode *> {
375 typedef ArgumentGraphNode NodeType;
376 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000377
Chandler Carruth63559d72015-09-13 06:47:20 +0000378 static inline NodeType *getEntryNode(NodeType *A) { return A; }
379 static inline ChildIteratorType child_begin(NodeType *N) {
380 return N->Uses.begin();
381 }
382 static inline ChildIteratorType child_end(NodeType *N) {
383 return N->Uses.end();
384 }
385};
386template <>
387struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
388 static NodeType *getEntryNode(ArgumentGraph *AG) {
389 return AG->getEntryNode();
390 }
391 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
392 return AG->begin();
393 }
394 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
395};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000396}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000397
Chandler Carrutha632fb92015-09-13 06:57:25 +0000398/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000399static Attribute::AttrKind
400determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000401 const SmallPtrSet<Argument *, 8> &SCCNodes) {
402
403 SmallVector<Use *, 32> Worklist;
404 SmallSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000405
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000406 // inalloca arguments are always clobbered by the call.
407 if (A->hasInAllocaAttr())
408 return Attribute::None;
409
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000410 bool IsRead = false;
411 // We don't need to track IsWritten. If A is written to, return immediately.
412
Chandler Carruthcdf47882014-03-09 03:16:01 +0000413 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000414 Visited.insert(&U);
415 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000416 }
417
418 while (!Worklist.empty()) {
419 Use *U = Worklist.pop_back_val();
420 Instruction *I = cast<Instruction>(U->getUser());
421 Value *V = U->get();
422
423 switch (I->getOpcode()) {
424 case Instruction::BitCast:
425 case Instruction::GetElementPtr:
426 case Instruction::PHI:
427 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000428 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000429 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000430 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000431 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000432 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000433 break;
434
435 case Instruction::Call:
436 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000437 bool Captures = true;
438
439 if (I->getType()->isVoidTy())
440 Captures = false;
441
442 auto AddUsersToWorklistIfCapturing = [&] {
443 if (Captures)
444 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000445 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000446 Worklist.push_back(&UU);
447 };
448
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000449 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000450 if (CS.doesNotAccessMemory()) {
451 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000452 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000453 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000454
455 Function *F = CS.getCalledFunction();
456 if (!F) {
457 if (CS.onlyReadsMemory()) {
458 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000459 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000460 continue;
461 }
462 return Attribute::None;
463 }
464
465 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
466 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
467 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
468 if (A->get() == V) {
469 if (AI == AE) {
470 assert(F->isVarArg() &&
471 "More params than args in non-varargs call.");
472 return Attribute::None;
473 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000474 Captures &= !CS.doesNotCapture(A - B);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000475 if (SCCNodes.count(&*AI))
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000476 continue;
477 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
478 return Attribute::None;
479 if (!CS.doesNotAccessMemory(A - B))
480 IsRead = true;
481 }
482 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000483 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000484 break;
485 }
486
487 case Instruction::Load:
488 IsRead = true;
489 break;
490
491 case Instruction::ICmp:
492 case Instruction::Ret:
493 break;
494
495 default:
496 return Attribute::None;
497 }
498 }
499
500 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
501}
502
Chandler Carrutha632fb92015-09-13 06:57:25 +0000503/// Deduce nocapture attributes for the SCC.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000504bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000505 bool Changed = false;
506
Chandler Carruth63559d72015-09-13 06:47:20 +0000507 SmallPtrSet<Function *, 8> SCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000508
509 // Fill SCCNodes with the elements of the SCC. Used for quickly
510 // looking up whether a given CallGraphNode is in this SCC.
511 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
512 Function *F = (*I)->getFunction();
Chandler Carruth0fb99812014-08-13 10:49:33 +0000513 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
514 !F->hasFnAttribute(Attribute::OptimizeNone))
Nick Lewycky4c378a42011-12-28 23:24:21 +0000515 SCCNodes.insert(F);
516 }
517
518 ArgumentGraph AG;
519
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000520 AttrBuilder B;
521 B.addAttribute(Attribute::NoCapture);
522
Duncan Sands44c8cd92008-12-31 16:14:43 +0000523 // Check each function in turn, determining which pointer arguments are not
524 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000525 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
526 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000527
Chandler Carruth0fb99812014-08-13 10:49:33 +0000528 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
529 // External node or function we're trying not to optimize - only a problem
530 // for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000531 continue;
532
533 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000534 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000535 if (F->isDeclaration() || F->mayBeOverridden())
536 continue;
537
Nick Lewycky4c378a42011-12-28 23:24:21 +0000538 // Functions that are readonly (or readnone) and nounwind and don't return
539 // a value can't capture arguments. Don't analyze them.
540 if (F->onlyReadsMemory() && F->doesNotThrow() &&
541 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000542 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
543 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000544 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
545 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
546 ++NumNoCapture;
547 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000548 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000549 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000550 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000551 }
552
Chandler Carruth63559d72015-09-13 06:47:20 +0000553 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
554 ++A) {
555 if (!A->getType()->isPointerTy())
556 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000557 bool HasNonLocalUses = false;
558 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000559 ArgumentUsesTracker Tracker(SCCNodes);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000560 PointerMayBeCaptured(&*A, &Tracker);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000561 if (!Tracker.Captured) {
562 if (Tracker.Uses.empty()) {
563 // If it's trivially not captured, mark it nocapture now.
Chandler Carruth63559d72015-09-13 06:47:20 +0000564 A->addAttr(
565 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000566 ++NumNoCapture;
567 Changed = true;
568 } else {
569 // If it's not trivially captured and not trivially not captured,
570 // then it must be calling into another function in our SCC. Save
571 // its particulars for Argument-SCC analysis later.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000572 ArgumentGraphNode *Node = AG[&*A];
Chandler Carruth63559d72015-09-13 06:47:20 +0000573 for (SmallVectorImpl<Argument *>::iterator
574 UI = Tracker.Uses.begin(),
575 UE = Tracker.Uses.end();
576 UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000577 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000578 if (*UI != A)
579 HasNonLocalUses = true;
580 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000581 }
582 }
583 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
584 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000585 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
586 // Can we determine that it's readonly/readnone without doing an SCC?
587 // Note that we don't allow any calls at all here, or else our result
588 // will be dependent on the iteration order through the functions in the
589 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000590 SmallPtrSet<Argument *, 8> Self;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000591 Self.insert(&*A);
592 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000593 if (R != Attribute::None) {
594 AttrBuilder B;
595 B.addAttribute(R);
596 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
597 Changed = true;
598 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
599 }
600 }
601 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000602 }
603
604 // The graph we've collected is partial because we stopped scanning for
605 // argument uses once we solved the argument trivially. These partial nodes
606 // show up as ArgumentGraphNode objects with an empty Uses list, and for
607 // these nodes the final decision about whether they capture has already been
608 // made. If the definition doesn't have a 'nocapture' attribute by now, it
609 // captures.
610
Chandler Carruth63559d72015-09-13 06:47:20 +0000611 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000612 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000613 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000614 if (!ArgumentSCC[0]->Definition)
615 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000616
617 // eg. "void f(int* x) { if (...) f(x); }"
618 if (ArgumentSCC[0]->Uses.size() == 1 &&
619 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000620 Argument *A = ArgumentSCC[0]->Definition;
621 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000622 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000623 Changed = true;
624 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000625 continue;
626 }
627
628 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000629 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
630 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000631 ArgumentGraphNode *Node = *I;
632 if (Node->Uses.empty()) {
633 if (!Node->Definition->hasNoCaptureAttr())
634 SCCCaptured = true;
635 }
636 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000637 if (SCCCaptured)
638 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000639
Chandler Carruth63559d72015-09-13 06:47:20 +0000640 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000641 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
642 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000643 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000644 ArgumentSCCNodes.insert((*I)->Definition);
645 }
646
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000647 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
648 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000649 ArgumentGraphNode *N = *I;
Chandler Carruth63559d72015-09-13 06:47:20 +0000650 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
651 UE = N->Uses.end();
652 UI != UE; ++UI) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000653 Argument *A = (*UI)->Definition;
654 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
655 continue;
656 SCCCaptured = true;
657 break;
658 }
659 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000660 if (SCCCaptured)
661 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000662
Nick Lewyckyf740db32012-01-05 22:21:45 +0000663 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000664 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000665 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000666 ++NumNoCapture;
667 Changed = true;
668 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000669
670 // We also want to compute readonly/readnone. With a small number of false
671 // negatives, we can assume that any pointer which is captured isn't going
672 // to be provably readonly or readnone, since by definition we can't
673 // analyze all uses of a captured pointer.
674 //
675 // The false negatives happen when the pointer is captured by a function
676 // that promises readonly/readnone behaviour on the pointer, then the
677 // pointer's lifetime ends before anything that writes to arbitrary memory.
678 // Also, a readonly/readnone pointer may be returned, but returning a
679 // pointer is capturing it.
680
681 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
682 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
683 Argument *A = ArgumentSCC[i]->Definition;
684 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
685 if (K == Attribute::ReadNone)
686 continue;
687 if (K == Attribute::ReadOnly) {
688 ReadAttr = Attribute::ReadOnly;
689 continue;
690 }
691 ReadAttr = K;
692 break;
693 }
694
695 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000696 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000697 B.addAttribute(ReadAttr);
Chandler Carruth63559d72015-09-13 06:47:20 +0000698 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000699 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
700 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000701 // Clear out existing readonly/readnone attributes
702 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000703 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
704 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
705 Changed = true;
706 }
707 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000708 }
709
710 return Changed;
711}
712
Chandler Carrutha632fb92015-09-13 06:57:25 +0000713/// Tests whether a function is "malloc-like".
714///
715/// A function is "malloc-like" if it returns either null or a pointer that
716/// doesn't alias any other pointer visible to the caller.
Chandler Carruth3824f852015-09-13 08:23:27 +0000717static bool isFunctionMallocLike(Function *F,
718 SmallPtrSet<Function *, 8> &SCCNodes) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000719 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000720 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
721 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
722 FlowsToReturn.insert(Ret->getReturnValue());
723
724 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000725 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000726
727 if (Constant *C = dyn_cast<Constant>(RetVal)) {
728 if (!C->isNullValue() && !isa<UndefValue>(C))
729 return false;
730
731 continue;
732 }
733
734 if (isa<Argument>(RetVal))
735 return false;
736
737 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
738 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000739 // Extend the analysis by looking upwards.
740 case Instruction::BitCast:
741 case Instruction::GetElementPtr:
742 case Instruction::AddrSpaceCast:
743 FlowsToReturn.insert(RVI->getOperand(0));
744 continue;
745 case Instruction::Select: {
746 SelectInst *SI = cast<SelectInst>(RVI);
747 FlowsToReturn.insert(SI->getTrueValue());
748 FlowsToReturn.insert(SI->getFalseValue());
749 continue;
750 }
751 case Instruction::PHI: {
752 PHINode *PN = cast<PHINode>(RVI);
753 for (Value *IncValue : PN->incoming_values())
754 FlowsToReturn.insert(IncValue);
755 continue;
756 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000757
Chandler Carruth63559d72015-09-13 06:47:20 +0000758 // Check whether the pointer came from an allocation.
759 case Instruction::Alloca:
760 break;
761 case Instruction::Call:
762 case Instruction::Invoke: {
763 CallSite CS(RVI);
764 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000765 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000766 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
767 break;
768 } // fall-through
769 default:
770 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000771 }
772
Dan Gohman94e61762009-11-19 21:57:48 +0000773 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000774 return false;
775 }
776
777 return true;
778}
779
Chandler Carrutha632fb92015-09-13 06:57:25 +0000780/// Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000781bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000782 SmallPtrSet<Function *, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000783
784 // Fill SCCNodes with the elements of the SCC. Used for quickly
785 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000786 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
787 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000788
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000789 // Check each function in turn, determining which functions return noalias
790 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000791 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
792 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000793
Chandler Carruth0fb99812014-08-13 10:49:33 +0000794 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
795 // External node or node we don't want to optimize - skip it;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000796 return false;
797
798 // Already noalias.
799 if (F->doesNotAlias(0))
800 continue;
801
802 // Definitions with weak linkage may be overridden at linktime, so
803 // treat them like declarations.
804 if (F->isDeclaration() || F->mayBeOverridden())
805 return false;
806
Chandler Carruth63559d72015-09-13 06:47:20 +0000807 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000808 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000809 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000810 continue;
811
Chandler Carruth3824f852015-09-13 08:23:27 +0000812 if (!isFunctionMallocLike(F, SCCNodes))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000813 return false;
814 }
815
816 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000817 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
818 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000819 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000820 continue;
821
822 F->setDoesNotAlias(0);
823 ++NumNoAlias;
824 MadeChange = true;
825 }
826
827 return MadeChange;
828}
829
Chandler Carrutha632fb92015-09-13 06:57:25 +0000830/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000831///
832/// Requires that the function returns a pointer.
833///
834/// Returns true if it believes the function will not return a null, and sets
835/// \p Speculative based on whether the returned conclusion is a speculative
836/// conclusion due to SCC calls.
837static bool isReturnNonNull(Function *F, SmallPtrSet<Function *, 8> &SCCNodes,
838 const TargetLibraryInfo &TLI, bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000839 assert(F->getReturnType()->isPointerTy() &&
840 "nonnull only meaningful on pointer types");
841 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000842
Philip Reamesa88caea2015-08-31 19:44:38 +0000843 SmallSetVector<Value *, 8> FlowsToReturn;
844 for (BasicBlock &BB : *F)
845 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
846 FlowsToReturn.insert(Ret->getReturnValue());
847
848 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
849 Value *RetVal = FlowsToReturn[i];
850
851 // If this value is locally known to be non-null, we're good
Chandler Carruth8874b782015-09-13 08:17:14 +0000852 if (isKnownNonNull(RetVal, &TLI))
Philip Reamesa88caea2015-08-31 19:44:38 +0000853 continue;
854
855 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000856 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000857 Instruction *RVI = dyn_cast<Instruction>(RetVal);
858 if (!RVI)
859 return false;
860 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000861 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000862 case Instruction::BitCast:
863 case Instruction::GetElementPtr:
864 case Instruction::AddrSpaceCast:
865 FlowsToReturn.insert(RVI->getOperand(0));
866 continue;
867 case Instruction::Select: {
868 SelectInst *SI = cast<SelectInst>(RVI);
869 FlowsToReturn.insert(SI->getTrueValue());
870 FlowsToReturn.insert(SI->getFalseValue());
871 continue;
872 }
873 case Instruction::PHI: {
874 PHINode *PN = cast<PHINode>(RVI);
875 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
876 FlowsToReturn.insert(PN->getIncomingValue(i));
877 continue;
878 }
879 case Instruction::Call:
880 case Instruction::Invoke: {
881 CallSite CS(RVI);
882 Function *Callee = CS.getCalledFunction();
883 // A call to a node within the SCC is assumed to return null until
884 // proven otherwise
885 if (Callee && SCCNodes.count(Callee)) {
886 Speculative = true;
887 continue;
888 }
889 return false;
890 }
891 default:
Chandler Carruth63559d72015-09-13 06:47:20 +0000892 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +0000893 };
894 llvm_unreachable("should have either continued or returned");
895 }
896
897 return true;
898}
899
Chandler Carrutha632fb92015-09-13 06:57:25 +0000900/// Deduce nonnull attributes for the SCC.
Philip Reamesa88caea2015-08-31 19:44:38 +0000901bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000902 SmallPtrSet<Function *, 8> SCCNodes;
Philip Reamesa88caea2015-08-31 19:44:38 +0000903
904 // Fill SCCNodes with the elements of the SCC. Used for quickly
905 // looking up whether a given CallGraphNode is in this SCC.
906 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
907 SCCNodes.insert((*I)->getFunction());
908
909 // Speculative that all functions in the SCC return only nonnull
910 // pointers. We may refute this as we analyze functions.
911 bool SCCReturnsNonNull = true;
912
913 bool MadeChange = false;
914
915 // Check each function in turn, determining which functions return nonnull
916 // pointers.
917 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
918 Function *F = (*I)->getFunction();
919
920 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
921 // External node or node we don't want to optimize - skip it;
922 return false;
923
924 // Already nonnull.
925 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
926 Attribute::NonNull))
927 continue;
928
929 // Definitions with weak linkage may be overridden at linktime, so
930 // treat them like declarations.
931 if (F->isDeclaration() || F->mayBeOverridden())
932 return false;
933
Chandler Carruth63559d72015-09-13 06:47:20 +0000934 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +0000935 // pointer types.
936 if (!F->getReturnType()->isPointerTy())
937 continue;
938
939 bool Speculative = false;
Chandler Carruth8874b782015-09-13 08:17:14 +0000940 if (isReturnNonNull(F, SCCNodes, *TLI, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000941 if (!Speculative) {
942 // Mark the function eagerly since we may discover a function
943 // which prevents us from speculating about the entire SCC
944 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
945 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
946 ++NumNonNullReturn;
947 MadeChange = true;
948 }
949 continue;
950 }
951 // At least one function returns something which could be null, can't
952 // speculate any more.
953 SCCReturnsNonNull = false;
954 }
955
956 if (SCCReturnsNonNull) {
957 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
958 Function *F = (*I)->getFunction();
959 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
960 Attribute::NonNull) ||
961 !F->getReturnType()->isPointerTy())
962 continue;
963
964 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
965 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
966 ++NumNonNullReturn;
967 MadeChange = true;
968 }
969 }
970
971 return MadeChange;
972}
973
Chandler Carruthd0245202015-09-13 07:50:43 +0000974static void setDoesNotAccessMemory(Function &F) {
975 if (!F.doesNotAccessMemory()) {
976 F.setDoesNotAccessMemory();
977 ++NumAnnotated;
978 }
979}
980
981static void setOnlyReadsMemory(Function &F) {
982 if (!F.onlyReadsMemory()) {
983 F.setOnlyReadsMemory();
984 ++NumAnnotated;
985 }
986}
987
988static void setDoesNotThrow(Function &F) {
989 if (!F.doesNotThrow()) {
990 F.setDoesNotThrow();
991 ++NumAnnotated;
992 }
993}
994
995static void setDoesNotCapture(Function &F, unsigned n) {
996 if (!F.doesNotCapture(n)) {
997 F.setDoesNotCapture(n);
998 ++NumAnnotated;
999 }
1000}
1001
1002static void setOnlyReadsMemory(Function &F, unsigned n) {
1003 if (!F.onlyReadsMemory(n)) {
1004 F.setOnlyReadsMemory(n);
1005 ++NumAnnotated;
1006 }
1007}
1008
1009static void setDoesNotAlias(Function &F, unsigned n) {
1010 if (!F.doesNotAlias(n)) {
1011 F.setDoesNotAlias(n);
1012 ++NumAnnotated;
1013 }
1014}
1015
Chandler Carrutha632fb92015-09-13 06:57:25 +00001016/// Analyze the name and prototype of the given function and set any applicable
1017/// attributes.
1018///
1019/// Returns true if any attributes were set and false otherwise.
Chandler Carruth444d0052015-09-13 08:03:23 +00001020static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
Chandler Carruth0fb99812014-08-13 10:49:33 +00001021 if (F.hasFnAttribute(Attribute::OptimizeNone))
1022 return false;
1023
Meador Inge6b6a1612013-03-21 00:55:59 +00001024 FunctionType *FTy = F.getFunctionType();
1025 LibFunc::Func TheLibFunc;
Chandler Carruth444d0052015-09-13 08:03:23 +00001026 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
Meador Inge6b6a1612013-03-21 00:55:59 +00001027 return false;
1028
1029 switch (TheLibFunc) {
1030 case LibFunc::strlen:
1031 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1032 return false;
1033 setOnlyReadsMemory(F);
1034 setDoesNotThrow(F);
1035 setDoesNotCapture(F, 1);
1036 break;
1037 case LibFunc::strchr:
1038 case LibFunc::strrchr:
Chandler Carruth63559d72015-09-13 06:47:20 +00001039 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001040 !FTy->getParamType(1)->isIntegerTy())
1041 return false;
1042 setOnlyReadsMemory(F);
1043 setDoesNotThrow(F);
1044 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001045 case LibFunc::strtol:
1046 case LibFunc::strtod:
1047 case LibFunc::strtof:
1048 case LibFunc::strtoul:
1049 case LibFunc::strtoll:
1050 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +00001051 case LibFunc::strtoull:
Chandler Carruth63559d72015-09-13 06:47:20 +00001052 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001053 return false;
1054 setDoesNotThrow(F);
1055 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001056 setOnlyReadsMemory(F, 1);
1057 break;
1058 case LibFunc::strcpy:
1059 case LibFunc::stpcpy:
1060 case LibFunc::strcat:
1061 case LibFunc::strncat:
1062 case LibFunc::strncpy:
1063 case LibFunc::stpncpy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001064 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001065 return false;
1066 setDoesNotThrow(F);
1067 setDoesNotCapture(F, 2);
1068 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001069 break;
1070 case LibFunc::strxfrm:
Chandler Carruth63559d72015-09-13 06:47:20 +00001071 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001072 !FTy->getParamType(1)->isPointerTy())
1073 return false;
1074 setDoesNotThrow(F);
1075 setDoesNotCapture(F, 1);
1076 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001077 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001078 break;
Chandler Carruth63559d72015-09-13 06:47:20 +00001079 case LibFunc::strcmp: // 0,1
1080 case LibFunc::strspn: // 0,1
1081 case LibFunc::strncmp: // 0,1
1082 case LibFunc::strcspn: // 0,1
1083 case LibFunc::strcoll: // 0,1
1084 case LibFunc::strcasecmp: // 0,1
1085 case LibFunc::strncasecmp: //
1086 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001087 !FTy->getParamType(1)->isPointerTy())
1088 return false;
1089 setOnlyReadsMemory(F);
1090 setDoesNotThrow(F);
1091 setDoesNotCapture(F, 1);
1092 setDoesNotCapture(F, 2);
1093 break;
1094 case LibFunc::strstr:
1095 case LibFunc::strpbrk:
1096 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1097 return false;
1098 setOnlyReadsMemory(F);
1099 setDoesNotThrow(F);
1100 setDoesNotCapture(F, 2);
1101 break;
1102 case LibFunc::strtok:
1103 case LibFunc::strtok_r:
1104 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1105 return false;
1106 setDoesNotThrow(F);
1107 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001108 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001109 break;
1110 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001111 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1112 return false;
1113 setDoesNotThrow(F);
1114 setDoesNotCapture(F, 1);
1115 setOnlyReadsMemory(F, 1);
1116 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001117 case LibFunc::setbuf:
1118 case LibFunc::setvbuf:
1119 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1120 return false;
1121 setDoesNotThrow(F);
1122 setDoesNotCapture(F, 1);
1123 break;
1124 case LibFunc::strdup:
1125 case LibFunc::strndup:
1126 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1127 !FTy->getParamType(0)->isPointerTy())
1128 return false;
1129 setDoesNotThrow(F);
1130 setDoesNotAlias(F, 0);
1131 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001132 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001133 break;
1134 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +00001135 case LibFunc::statvfs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001136 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001137 !FTy->getParamType(1)->isPointerTy())
1138 return false;
1139 setDoesNotThrow(F);
1140 setDoesNotCapture(F, 1);
1141 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001142 setOnlyReadsMemory(F, 1);
1143 break;
1144 case LibFunc::sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001145 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001146 !FTy->getParamType(1)->isPointerTy())
1147 return false;
1148 setDoesNotThrow(F);
1149 setDoesNotCapture(F, 1);
1150 setDoesNotCapture(F, 2);
1151 setOnlyReadsMemory(F, 1);
1152 setOnlyReadsMemory(F, 2);
1153 break;
1154 case LibFunc::sprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001155 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001156 !FTy->getParamType(1)->isPointerTy())
1157 return false;
1158 setDoesNotThrow(F);
1159 setDoesNotCapture(F, 1);
1160 setDoesNotCapture(F, 2);
1161 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001162 break;
1163 case LibFunc::snprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001164 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001165 !FTy->getParamType(2)->isPointerTy())
1166 return false;
1167 setDoesNotThrow(F);
1168 setDoesNotCapture(F, 1);
1169 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001170 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001171 break;
1172 case LibFunc::setitimer:
Chandler Carruth63559d72015-09-13 06:47:20 +00001173 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001174 !FTy->getParamType(2)->isPointerTy())
1175 return false;
1176 setDoesNotThrow(F);
1177 setDoesNotCapture(F, 2);
1178 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001179 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001180 break;
1181 case LibFunc::system:
Chandler Carruth63559d72015-09-13 06:47:20 +00001182 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001183 return false;
1184 // May throw; "system" is a valid pthread cancellation point.
1185 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001186 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001187 break;
1188 case LibFunc::malloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001189 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001190 return false;
1191 setDoesNotThrow(F);
1192 setDoesNotAlias(F, 0);
1193 break;
1194 case LibFunc::memcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001195 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001196 !FTy->getParamType(1)->isPointerTy())
1197 return false;
1198 setOnlyReadsMemory(F);
1199 setDoesNotThrow(F);
1200 setDoesNotCapture(F, 1);
1201 setDoesNotCapture(F, 2);
1202 break;
1203 case LibFunc::memchr:
1204 case LibFunc::memrchr:
1205 if (FTy->getNumParams() != 3)
1206 return false;
1207 setOnlyReadsMemory(F);
1208 setDoesNotThrow(F);
1209 break;
1210 case LibFunc::modf:
1211 case LibFunc::modff:
1212 case LibFunc::modfl:
Chandler Carruth63559d72015-09-13 06:47:20 +00001213 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001214 return false;
1215 setDoesNotThrow(F);
1216 setDoesNotCapture(F, 2);
1217 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001218 case LibFunc::memcpy:
1219 case LibFunc::memccpy:
1220 case LibFunc::memmove:
Chandler Carruth63559d72015-09-13 06:47:20 +00001221 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001222 return false;
1223 setDoesNotThrow(F);
1224 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001225 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001226 break;
1227 case LibFunc::memalign:
1228 if (!FTy->getReturnType()->isPointerTy())
1229 return false;
1230 setDoesNotAlias(F, 0);
1231 break;
1232 case LibFunc::mkdir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001233 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001234 return false;
1235 setDoesNotThrow(F);
1236 setDoesNotCapture(F, 1);
1237 setOnlyReadsMemory(F, 1);
1238 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001239 case LibFunc::mktime:
Chandler Carruth63559d72015-09-13 06:47:20 +00001240 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001241 return false;
1242 setDoesNotThrow(F);
1243 setDoesNotCapture(F, 1);
1244 break;
1245 case LibFunc::realloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001246 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001247 !FTy->getReturnType()->isPointerTy())
1248 return false;
1249 setDoesNotThrow(F);
1250 setDoesNotAlias(F, 0);
1251 setDoesNotCapture(F, 1);
1252 break;
1253 case LibFunc::read:
Chandler Carruth63559d72015-09-13 06:47:20 +00001254 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001255 return false;
1256 // May throw; "read" is a valid pthread cancellation point.
1257 setDoesNotCapture(F, 2);
1258 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001259 case LibFunc::rewind:
Chandler Carruth63559d72015-09-13 06:47:20 +00001260 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001261 return false;
1262 setDoesNotThrow(F);
1263 setDoesNotCapture(F, 1);
1264 break;
1265 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001266 case LibFunc::remove:
1267 case LibFunc::realpath:
Chandler Carruth63559d72015-09-13 06:47:20 +00001268 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001269 return false;
1270 setDoesNotThrow(F);
1271 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001272 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001273 break;
1274 case LibFunc::rename:
Chandler Carruth63559d72015-09-13 06:47:20 +00001275 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001276 !FTy->getParamType(1)->isPointerTy())
1277 return false;
1278 setDoesNotThrow(F);
1279 setDoesNotCapture(F, 1);
1280 setDoesNotCapture(F, 2);
1281 setOnlyReadsMemory(F, 1);
1282 setOnlyReadsMemory(F, 2);
1283 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001284 case LibFunc::readlink:
Chandler Carruth63559d72015-09-13 06:47:20 +00001285 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001286 !FTy->getParamType(1)->isPointerTy())
1287 return false;
1288 setDoesNotThrow(F);
1289 setDoesNotCapture(F, 1);
1290 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001291 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001292 break;
1293 case LibFunc::write:
1294 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1295 return false;
1296 // May throw; "write" is a valid pthread cancellation point.
1297 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001298 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001299 break;
1300 case LibFunc::bcopy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001301 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001302 !FTy->getParamType(1)->isPointerTy())
1303 return false;
1304 setDoesNotThrow(F);
1305 setDoesNotCapture(F, 1);
1306 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001307 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001308 break;
1309 case LibFunc::bcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001310 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001311 !FTy->getParamType(1)->isPointerTy())
1312 return false;
1313 setDoesNotThrow(F);
1314 setOnlyReadsMemory(F);
1315 setDoesNotCapture(F, 1);
1316 setDoesNotCapture(F, 2);
1317 break;
1318 case LibFunc::bzero:
1319 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1320 return false;
1321 setDoesNotThrow(F);
1322 setDoesNotCapture(F, 1);
1323 break;
1324 case LibFunc::calloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001325 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001326 return false;
1327 setDoesNotThrow(F);
1328 setDoesNotAlias(F, 0);
1329 break;
1330 case LibFunc::chmod:
1331 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001332 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1333 return false;
1334 setDoesNotThrow(F);
1335 setDoesNotCapture(F, 1);
1336 setOnlyReadsMemory(F, 1);
1337 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001338 case LibFunc::ctermid:
1339 case LibFunc::clearerr:
1340 case LibFunc::closedir:
1341 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1342 return false;
1343 setDoesNotThrow(F);
1344 setDoesNotCapture(F, 1);
1345 break;
1346 case LibFunc::atoi:
1347 case LibFunc::atol:
1348 case LibFunc::atof:
1349 case LibFunc::atoll:
1350 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1351 return false;
1352 setDoesNotThrow(F);
1353 setOnlyReadsMemory(F);
1354 setDoesNotCapture(F, 1);
1355 break;
1356 case LibFunc::access:
1357 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1358 return false;
1359 setDoesNotThrow(F);
1360 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001361 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001362 break;
1363 case LibFunc::fopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001364 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001365 !FTy->getParamType(0)->isPointerTy() ||
1366 !FTy->getParamType(1)->isPointerTy())
1367 return false;
1368 setDoesNotThrow(F);
1369 setDoesNotAlias(F, 0);
1370 setDoesNotCapture(F, 1);
1371 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001372 setOnlyReadsMemory(F, 1);
1373 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001374 break;
1375 case LibFunc::fdopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001376 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001377 !FTy->getParamType(1)->isPointerTy())
1378 return false;
1379 setDoesNotThrow(F);
1380 setDoesNotAlias(F, 0);
1381 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001382 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001383 break;
1384 case LibFunc::feof:
1385 case LibFunc::free:
1386 case LibFunc::fseek:
1387 case LibFunc::ftell:
1388 case LibFunc::fgetc:
1389 case LibFunc::fseeko:
1390 case LibFunc::ftello:
1391 case LibFunc::fileno:
1392 case LibFunc::fflush:
1393 case LibFunc::fclose:
1394 case LibFunc::fsetpos:
1395 case LibFunc::flockfile:
1396 case LibFunc::funlockfile:
1397 case LibFunc::ftrylockfile:
1398 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1399 return false;
1400 setDoesNotThrow(F);
1401 setDoesNotCapture(F, 1);
1402 break;
1403 case LibFunc::ferror:
1404 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1405 return false;
1406 setDoesNotThrow(F);
1407 setDoesNotCapture(F, 1);
1408 setOnlyReadsMemory(F);
1409 break;
1410 case LibFunc::fputc:
1411 case LibFunc::fstat:
1412 case LibFunc::frexp:
1413 case LibFunc::frexpf:
1414 case LibFunc::frexpl:
1415 case LibFunc::fstatvfs:
1416 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1417 return false;
1418 setDoesNotThrow(F);
1419 setDoesNotCapture(F, 2);
1420 break;
1421 case LibFunc::fgets:
Chandler Carruth63559d72015-09-13 06:47:20 +00001422 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001423 !FTy->getParamType(2)->isPointerTy())
1424 return false;
1425 setDoesNotThrow(F);
1426 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001427 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001428 case LibFunc::fread:
Chandler Carruth63559d72015-09-13 06:47:20 +00001429 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001430 !FTy->getParamType(3)->isPointerTy())
1431 return false;
1432 setDoesNotThrow(F);
1433 setDoesNotCapture(F, 1);
1434 setDoesNotCapture(F, 4);
1435 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001436 case LibFunc::fwrite:
Chandler Carruth63559d72015-09-13 06:47:20 +00001437 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001438 !FTy->getParamType(3)->isPointerTy())
1439 return false;
1440 setDoesNotThrow(F);
1441 setDoesNotCapture(F, 1);
1442 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001443 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001444 case LibFunc::fputs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001445 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001446 !FTy->getParamType(1)->isPointerTy())
1447 return false;
1448 setDoesNotThrow(F);
1449 setDoesNotCapture(F, 1);
1450 setDoesNotCapture(F, 2);
1451 setOnlyReadsMemory(F, 1);
1452 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001453 case LibFunc::fscanf:
1454 case LibFunc::fprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001455 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001456 !FTy->getParamType(1)->isPointerTy())
1457 return false;
1458 setDoesNotThrow(F);
1459 setDoesNotCapture(F, 1);
1460 setDoesNotCapture(F, 2);
1461 setOnlyReadsMemory(F, 2);
1462 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001463 case LibFunc::fgetpos:
Chandler Carruth63559d72015-09-13 06:47:20 +00001464 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001465 !FTy->getParamType(1)->isPointerTy())
1466 return false;
1467 setDoesNotThrow(F);
1468 setDoesNotCapture(F, 1);
1469 setDoesNotCapture(F, 2);
1470 break;
1471 case LibFunc::getc:
1472 case LibFunc::getlogin_r:
1473 case LibFunc::getc_unlocked:
1474 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1475 return false;
1476 setDoesNotThrow(F);
1477 setDoesNotCapture(F, 1);
1478 break;
1479 case LibFunc::getenv:
1480 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1481 return false;
1482 setDoesNotThrow(F);
1483 setOnlyReadsMemory(F);
1484 setDoesNotCapture(F, 1);
1485 break;
1486 case LibFunc::gets:
1487 case LibFunc::getchar:
1488 setDoesNotThrow(F);
1489 break;
1490 case LibFunc::getitimer:
1491 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1492 return false;
1493 setDoesNotThrow(F);
1494 setDoesNotCapture(F, 2);
1495 break;
1496 case LibFunc::getpwnam:
1497 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1498 return false;
1499 setDoesNotThrow(F);
1500 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001501 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001502 break;
1503 case LibFunc::ungetc:
1504 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1505 return false;
1506 setDoesNotThrow(F);
1507 setDoesNotCapture(F, 2);
1508 break;
1509 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001510 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1511 return false;
1512 setDoesNotThrow(F);
1513 setDoesNotCapture(F, 1);
1514 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001515 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001516 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1517 return false;
1518 setDoesNotThrow(F);
1519 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001520 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001521 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001522 case LibFunc::unsetenv:
1523 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1524 return false;
1525 setDoesNotThrow(F);
1526 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001527 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001528 break;
1529 case LibFunc::utime:
1530 case LibFunc::utimes:
Chandler Carruth63559d72015-09-13 06:47:20 +00001531 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001532 !FTy->getParamType(1)->isPointerTy())
1533 return false;
1534 setDoesNotThrow(F);
1535 setDoesNotCapture(F, 1);
1536 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001537 setOnlyReadsMemory(F, 1);
1538 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001539 break;
1540 case LibFunc::putc:
1541 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1542 return false;
1543 setDoesNotThrow(F);
1544 setDoesNotCapture(F, 2);
1545 break;
1546 case LibFunc::puts:
1547 case LibFunc::printf:
1548 case LibFunc::perror:
1549 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1550 return false;
1551 setDoesNotThrow(F);
1552 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001553 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001554 break;
1555 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001556 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1557 return false;
1558 // May throw; "pread" is a valid pthread cancellation point.
1559 setDoesNotCapture(F, 2);
1560 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001561 case LibFunc::pwrite:
1562 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1563 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001564 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001565 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001566 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001567 break;
1568 case LibFunc::putchar:
1569 setDoesNotThrow(F);
1570 break;
1571 case LibFunc::popen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001572 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001573 !FTy->getParamType(0)->isPointerTy() ||
1574 !FTy->getParamType(1)->isPointerTy())
1575 return false;
1576 setDoesNotThrow(F);
1577 setDoesNotAlias(F, 0);
1578 setDoesNotCapture(F, 1);
1579 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001580 setOnlyReadsMemory(F, 1);
1581 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001582 break;
1583 case LibFunc::pclose:
1584 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1585 return false;
1586 setDoesNotThrow(F);
1587 setDoesNotCapture(F, 1);
1588 break;
1589 case LibFunc::vscanf:
1590 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1591 return false;
1592 setDoesNotThrow(F);
1593 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001594 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001595 break;
1596 case LibFunc::vsscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001597 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001598 !FTy->getParamType(2)->isPointerTy())
1599 return false;
1600 setDoesNotThrow(F);
1601 setDoesNotCapture(F, 1);
1602 setDoesNotCapture(F, 2);
1603 setOnlyReadsMemory(F, 1);
1604 setOnlyReadsMemory(F, 2);
1605 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001606 case LibFunc::vfscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001607 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001608 !FTy->getParamType(2)->isPointerTy())
1609 return false;
1610 setDoesNotThrow(F);
1611 setDoesNotCapture(F, 1);
1612 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001613 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001614 break;
1615 case LibFunc::valloc:
1616 if (!FTy->getReturnType()->isPointerTy())
1617 return false;
1618 setDoesNotThrow(F);
1619 setDoesNotAlias(F, 0);
1620 break;
1621 case LibFunc::vprintf:
1622 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1623 return false;
1624 setDoesNotThrow(F);
1625 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001626 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001627 break;
1628 case LibFunc::vfprintf:
1629 case LibFunc::vsprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001630 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001631 !FTy->getParamType(1)->isPointerTy())
1632 return false;
1633 setDoesNotThrow(F);
1634 setDoesNotCapture(F, 1);
1635 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001636 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001637 break;
1638 case LibFunc::vsnprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001639 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001640 !FTy->getParamType(2)->isPointerTy())
1641 return false;
1642 setDoesNotThrow(F);
1643 setDoesNotCapture(F, 1);
1644 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001645 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001646 break;
1647 case LibFunc::open:
1648 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1649 return false;
1650 // May throw; "open" is a valid pthread cancellation point.
1651 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001652 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001653 break;
1654 case LibFunc::opendir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001655 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001656 !FTy->getParamType(0)->isPointerTy())
1657 return false;
1658 setDoesNotThrow(F);
1659 setDoesNotAlias(F, 0);
1660 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001661 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001662 break;
1663 case LibFunc::tmpfile:
1664 if (!FTy->getReturnType()->isPointerTy())
1665 return false;
1666 setDoesNotThrow(F);
1667 setDoesNotAlias(F, 0);
1668 break;
1669 case LibFunc::times:
1670 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1671 return false;
1672 setDoesNotThrow(F);
1673 setDoesNotCapture(F, 1);
1674 break;
1675 case LibFunc::htonl:
1676 case LibFunc::htons:
1677 case LibFunc::ntohl:
1678 case LibFunc::ntohs:
1679 setDoesNotThrow(F);
1680 setDoesNotAccessMemory(F);
1681 break;
1682 case LibFunc::lstat:
Chandler Carruth63559d72015-09-13 06:47:20 +00001683 if (FTy->getNumParams() != 2 || !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);
Meador Inge6b6a1612013-03-21 00:55:59 +00001690 break;
1691 case LibFunc::lchown:
1692 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1693 return false;
1694 setDoesNotThrow(F);
1695 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001696 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001697 break;
1698 case LibFunc::qsort:
1699 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1700 return false;
1701 // May throw; places call through function pointer.
1702 setDoesNotCapture(F, 4);
1703 break;
1704 case LibFunc::dunder_strdup:
1705 case LibFunc::dunder_strndup:
Chandler Carruth63559d72015-09-13 06:47:20 +00001706 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001707 !FTy->getParamType(0)->isPointerTy())
1708 return false;
1709 setDoesNotThrow(F);
1710 setDoesNotAlias(F, 0);
1711 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001712 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001713 break;
1714 case LibFunc::dunder_strtok_r:
Chandler Carruth63559d72015-09-13 06:47:20 +00001715 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001716 return false;
1717 setDoesNotThrow(F);
1718 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001719 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001720 break;
1721 case LibFunc::under_IO_getc:
1722 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1723 return false;
1724 setDoesNotThrow(F);
1725 setDoesNotCapture(F, 1);
1726 break;
1727 case LibFunc::under_IO_putc:
1728 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1729 return false;
1730 setDoesNotThrow(F);
1731 setDoesNotCapture(F, 2);
1732 break;
1733 case LibFunc::dunder_isoc99_scanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001734 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001735 return false;
1736 setDoesNotThrow(F);
1737 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001738 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001739 break;
1740 case LibFunc::stat64:
1741 case LibFunc::lstat64:
1742 case LibFunc::statvfs64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001743 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001744 !FTy->getParamType(1)->isPointerTy())
1745 return false;
1746 setDoesNotThrow(F);
1747 setDoesNotCapture(F, 1);
1748 setDoesNotCapture(F, 2);
1749 setOnlyReadsMemory(F, 1);
1750 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001751 case LibFunc::dunder_isoc99_sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001752 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001753 !FTy->getParamType(1)->isPointerTy())
1754 return false;
1755 setDoesNotThrow(F);
1756 setDoesNotCapture(F, 1);
1757 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001758 setOnlyReadsMemory(F, 1);
1759 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001760 break;
1761 case LibFunc::fopen64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001762 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001763 !FTy->getParamType(0)->isPointerTy() ||
1764 !FTy->getParamType(1)->isPointerTy())
1765 return false;
1766 setDoesNotThrow(F);
1767 setDoesNotAlias(F, 0);
1768 setDoesNotCapture(F, 1);
1769 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001770 setOnlyReadsMemory(F, 1);
1771 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001772 break;
1773 case LibFunc::fseeko64:
1774 case LibFunc::ftello64:
1775 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1776 return false;
1777 setDoesNotThrow(F);
1778 setDoesNotCapture(F, 1);
1779 break;
1780 case LibFunc::tmpfile64:
1781 if (!FTy->getReturnType()->isPointerTy())
1782 return false;
1783 setDoesNotThrow(F);
1784 setDoesNotAlias(F, 0);
1785 break;
1786 case LibFunc::fstat64:
1787 case LibFunc::fstatvfs64:
1788 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1789 return false;
1790 setDoesNotThrow(F);
1791 setDoesNotCapture(F, 2);
1792 break;
1793 case LibFunc::open64:
1794 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1795 return false;
1796 // May throw; "open" is a valid pthread cancellation point.
1797 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001798 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001799 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001800 case LibFunc::gettimeofday:
1801 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1802 !FTy->getParamType(1)->isPointerTy())
1803 return false;
1804 // Currently some platforms have the restrict keyword on the arguments to
1805 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1806 // arguments.
1807 setDoesNotThrow(F);
1808 setDoesNotCapture(F, 1);
1809 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001810 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001811 default:
1812 // Didn't mark any attributes.
1813 return false;
1814 }
1815
1816 return true;
1817}
1818
Chandler Carrutha632fb92015-09-13 06:57:25 +00001819/// Adds attributes to well-known standard library call declarations.
Meador Inge6b6a1612013-03-21 00:55:59 +00001820bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1821 bool MadeChange = false;
1822
1823 // Check each function in turn annotating well-known library function
1824 // declarations with attributes.
1825 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1826 Function *F = (*I)->getFunction();
1827
Craig Topperf40110f2014-04-25 05:29:35 +00001828 if (F && F->isDeclaration())
Chandler Carruth444d0052015-09-13 08:03:23 +00001829 MadeChange |= inferPrototypeAttributes(*F, *TLI);
Meador Inge6b6a1612013-03-21 00:55:59 +00001830 }
1831
1832 return MadeChange;
1833}
1834
Chris Lattner4422d312010-04-16 22:42:17 +00001835bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001836 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohman86449d72010-11-08 16:10:15 +00001837
Meador Inge6b6a1612013-03-21 00:55:59 +00001838 bool Changed = annotateLibraryCalls(SCC);
1839 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001840 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001841 Changed |= AddNoAliasAttrs(SCC);
Philip Reamesa88caea2015-08-31 19:44:38 +00001842 Changed |= AddNonNullAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001843 return Changed;
1844}