blob: cfeedf6d1db999cfeefb66540f01a8f29523882e [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);
75 bool IsFunctionMallocLike(Function *F, SmallPtrSet<Function *, 8> &) const;
76 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Chandler Carruthd0245202015-09-13 07:50:43 +000077 bool AddNonNullAttrs(const CallGraphSCC &SCC);
Chandler Carruthd0245202015-09-13 07:50:43 +000078 bool annotateLibraryCalls(const CallGraphSCC &SCC);
Chandler Carruth63559d72015-09-13 06:47:20 +000079};
Alexander Kornienkof00654e2015-06-23 09:49:53 +000080}
Duncan Sands44c8cd92008-12-31 16:14:43 +000081
82char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +000083INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +000084 "Deduce function attributes", false, false)
Chandler Carruth7b560d42015-09-09 17:55:00 +000085INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth6378cf52013-11-26 04:19:30 +000086INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +000087INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +000088INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Chandler Carruth63559d72015-09-13 06:47:20 +000089 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +000090
91Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
92
Chandler Carrutha632fb92015-09-13 06:57:25 +000093/// Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000094bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chandler Carruth63559d72015-09-13 06:47:20 +000095 SmallPtrSet<Function *, 8> SCCNodes;
Duncan Sands44c8cd92008-12-31 16:14:43 +000096
97 // Fill SCCNodes with the elements of the SCC. Used for quickly
98 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000099 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
100 SCCNodes.insert((*I)->getFunction());
Duncan Sands44c8cd92008-12-31 16:14:43 +0000101
102 // Check if any of the functions in the SCC read or write memory. If they
103 // write memory then they can't be marked readnone or readonly.
104 bool ReadsMemory = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000105 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
106 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000107
Chandler Carruth0fb99812014-08-13 10:49:33 +0000108 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
109 // External node or node we don't want to optimize - assume it may write
110 // memory and give up.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000111 return false;
112
Chandler Carruth7b560d42015-09-09 17:55:00 +0000113 // We need to manually construct BasicAA directly in order to disable its
114 // use of other function analyses.
115 BasicAAResult BAR(createLegacyPMBasicAAResult(*this, *F));
116
117 // Construct our own AA results for this function. We do this manually to
118 // work around the limitations of the legacy pass manager.
119 AAResults AAR(createLegacyPMAAResults(*this, *F, BAR));
120
121 FunctionModRefBehavior MRB = AAR.getModRefBehavior(F);
Chandler Carruth194f59c2015-07-22 23:15:57 +0000122 if (MRB == FMRB_DoesNotAccessMemory)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000123 // Already perfect!
124 continue;
125
126 // Definitions with weak linkage may be overridden at linktime with
127 // something that writes memory, so treat them like declarations.
128 if (F->isDeclaration() || F->mayBeOverridden()) {
Dan Gohman35814e62010-11-09 20:13:27 +0000129 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000130 // May write memory. Just give up.
131 return false;
132
133 ReadsMemory = true;
134 continue;
135 }
136
137 // Scan the function body for instructions that may read or write memory.
138 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
139 Instruction *I = &*II;
140
141 // Some instructions can be ignored even if they read or write memory.
142 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif62f0aac2010-07-28 22:50:26 +0000143 CallSite CS(cast<Value>(I));
Dan Gohman86449d72010-11-08 16:10:15 +0000144 if (CS) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000145 // Ignore calls to functions in the same SCC.
Dan Gohman86449d72010-11-08 16:10:15 +0000146 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000147 continue;
Chandler Carruth7b560d42015-09-09 17:55:00 +0000148 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
Dan Gohman2694e142010-11-10 01:02:18 +0000149 // If the call doesn't access arbitrary memory, we may be able to
150 // figure out something.
Dan Gohman25775802010-11-10 17:34:04 +0000151 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
152 // If the call does access argument pointees, check each argument.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000153 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman2694e142010-11-10 01:02:18 +0000154 // Check whether all pointer arguments point to local memory, and
155 // ignore calls that only access local memory.
156 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
157 CI != CE; ++CI) {
158 Value *Arg = *CI;
159 if (Arg->getType()->isPointerTy()) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000160 AAMDNodes AAInfo;
161 I->getAAMetadata(AAInfo);
162
Chandler Carruthecbd1682015-06-17 07:21:38 +0000163 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000164 if (!AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000165 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000166 // Writes non-local memory. Give up.
167 return false;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000168 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000169 // Ok, it reads non-local memory.
170 ReadsMemory = true;
171 }
Dan Gohmande521552010-11-09 19:56:27 +0000172 }
173 }
Dan Gohmande521552010-11-09 19:56:27 +0000174 continue;
Dan Gohman86449d72010-11-08 16:10:15 +0000175 }
Dan Gohman2694e142010-11-10 01:02:18 +0000176 // The call could access any memory. If that includes writes, give up.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000177 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000178 return false;
179 // If it reads, note it.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000180 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000181 ReadsMemory = true;
182 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000183 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000184 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
185 if (!LI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000186 MemoryLocation Loc = MemoryLocation::get(LI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000187 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000188 continue;
189 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000190 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000191 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
192 if (!SI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000193 MemoryLocation Loc = MemoryLocation::get(SI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000194 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000195 continue;
196 }
Dan Gohmane3467a72010-11-09 20:17:38 +0000197 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
198 // Ignore vaargs on local memory.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000199 MemoryLocation Loc = MemoryLocation::get(VI);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000200 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
Dan Gohmane3467a72010-11-09 20:17:38 +0000201 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000202 }
203
204 // Any remaining instructions need to be taken seriously! Check if they
205 // read or write memory.
206 if (I->mayWriteToMemory())
207 // Writes memory. Just give up.
208 return false;
Duncan Sands9759f2e2009-05-06 08:42:00 +0000209
Duncan Sands44c8cd92008-12-31 16:14:43 +0000210 // If this instruction may read memory, remember that.
211 ReadsMemory |= I->mayReadFromMemory();
212 }
213 }
214
215 // Success! Functions in this SCC do not access memory, or only read memory.
216 // Give them the appropriate attribute.
217 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000218 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
219 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000220
221 if (F->doesNotAccessMemory())
222 // Already perfect!
223 continue;
224
225 if (F->onlyReadsMemory() && ReadsMemory)
226 // No change.
227 continue;
228
229 MadeChange = true;
230
231 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000232 AttrBuilder B;
Chandler Carruth63559d72015-09-13 06:47:20 +0000233 B.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
234 F->removeAttributes(
235 AttributeSet::FunctionIndex,
236 AttributeSet::get(F->getContext(), AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000237
238 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000239 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000240 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000241
242 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000243 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000244 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000245 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000246 }
247
248 return MadeChange;
249}
250
Nick Lewycky4c378a42011-12-28 23:24:21 +0000251namespace {
Chandler Carrutha632fb92015-09-13 06:57:25 +0000252/// For a given pointer Argument, this retains a list of Arguments of functions
253/// in the same SCC that the pointer data flows into. We use this to build an
254/// SCC of the arguments.
Chandler Carruth63559d72015-09-13 06:47:20 +0000255struct ArgumentGraphNode {
256 Argument *Definition;
257 SmallVector<ArgumentGraphNode *, 4> Uses;
258};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000259
Chandler Carruth63559d72015-09-13 06:47:20 +0000260class ArgumentGraph {
261 // We store pointers to ArgumentGraphNode objects, so it's important that
262 // that they not move around upon insert.
263 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000264
Chandler Carruth63559d72015-09-13 06:47:20 +0000265 ArgumentMapTy ArgumentMap;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000266
Chandler Carruth63559d72015-09-13 06:47:20 +0000267 // There is no root node for the argument graph, in fact:
268 // void f(int *x, int *y) { if (...) f(x, y); }
269 // is an example where the graph is disconnected. The SCCIterator requires a
270 // single entry point, so we maintain a fake ("synthetic") root node that
271 // uses every node. Because the graph is directed and nothing points into
272 // the root, it will not participate in any SCCs (except for its own).
273 ArgumentGraphNode SyntheticRoot;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000274
Chandler Carruth63559d72015-09-13 06:47:20 +0000275public:
276 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000277
Chandler Carruth63559d72015-09-13 06:47:20 +0000278 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000279
Chandler Carruth63559d72015-09-13 06:47:20 +0000280 iterator begin() { return SyntheticRoot.Uses.begin(); }
281 iterator end() { return SyntheticRoot.Uses.end(); }
282 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000283
Chandler Carruth63559d72015-09-13 06:47:20 +0000284 ArgumentGraphNode *operator[](Argument *A) {
285 ArgumentGraphNode &Node = ArgumentMap[A];
286 Node.Definition = A;
287 SyntheticRoot.Uses.push_back(&Node);
288 return &Node;
289 }
290};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000291
Chandler Carrutha632fb92015-09-13 06:57:25 +0000292/// This tracker checks whether callees are in the SCC, and if so it does not
293/// consider that a capture, instead adding it to the "Uses" list and
294/// continuing with the analysis.
Chandler Carruth63559d72015-09-13 06:47:20 +0000295struct ArgumentUsesTracker : public CaptureTracker {
296 ArgumentUsesTracker(const SmallPtrSet<Function *, 8> &SCCNodes)
Nick Lewycky4c378a42011-12-28 23:24:21 +0000297 : Captured(false), SCCNodes(SCCNodes) {}
298
Chandler Carruth63559d72015-09-13 06:47:20 +0000299 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000300
Chandler Carruth63559d72015-09-13 06:47:20 +0000301 bool captured(const Use *U) override {
302 CallSite CS(U->getUser());
303 if (!CS.getInstruction()) {
304 Captured = true;
305 return true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000306 }
307
Chandler Carruth63559d72015-09-13 06:47:20 +0000308 Function *F = CS.getCalledFunction();
309 if (!F || !SCCNodes.count(F)) {
310 Captured = true;
311 return true;
312 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000313
Chandler Carruth63559d72015-09-13 06:47:20 +0000314 bool Found = false;
315 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
316 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
317 PI != PE; ++PI, ++AI) {
318 if (AI == AE) {
319 assert(F->isVarArg() && "More params than args in non-varargs call");
320 Captured = true;
321 return true;
322 }
323 if (PI == U) {
324 Uses.push_back(AI);
325 Found = true;
326 break;
327 }
328 }
329 assert(Found && "Capturing call-site captured nothing?");
330 (void)Found;
331 return false;
332 }
333
334 bool Captured; // True only if certainly captured (used outside our SCC).
335 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
336
337 const SmallPtrSet<Function *, 8> &SCCNodes;
338};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000339}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000340
341namespace llvm {
Chandler Carruth63559d72015-09-13 06:47:20 +0000342template <> struct GraphTraits<ArgumentGraphNode *> {
343 typedef ArgumentGraphNode NodeType;
344 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000345
Chandler Carruth63559d72015-09-13 06:47:20 +0000346 static inline NodeType *getEntryNode(NodeType *A) { return A; }
347 static inline ChildIteratorType child_begin(NodeType *N) {
348 return N->Uses.begin();
349 }
350 static inline ChildIteratorType child_end(NodeType *N) {
351 return N->Uses.end();
352 }
353};
354template <>
355struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
356 static NodeType *getEntryNode(ArgumentGraph *AG) {
357 return AG->getEntryNode();
358 }
359 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
360 return AG->begin();
361 }
362 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
363};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000364}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000365
Chandler Carrutha632fb92015-09-13 06:57:25 +0000366/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000367static Attribute::AttrKind
368determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000369 const SmallPtrSet<Argument *, 8> &SCCNodes) {
370
371 SmallVector<Use *, 32> Worklist;
372 SmallSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000373
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000374 // inalloca arguments are always clobbered by the call.
375 if (A->hasInAllocaAttr())
376 return Attribute::None;
377
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000378 bool IsRead = false;
379 // We don't need to track IsWritten. If A is written to, return immediately.
380
Chandler Carruthcdf47882014-03-09 03:16:01 +0000381 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000382 Visited.insert(&U);
383 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000384 }
385
386 while (!Worklist.empty()) {
387 Use *U = Worklist.pop_back_val();
388 Instruction *I = cast<Instruction>(U->getUser());
389 Value *V = U->get();
390
391 switch (I->getOpcode()) {
392 case Instruction::BitCast:
393 case Instruction::GetElementPtr:
394 case Instruction::PHI:
395 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000396 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000397 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000398 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000399 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000400 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000401 break;
402
403 case Instruction::Call:
404 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000405 bool Captures = true;
406
407 if (I->getType()->isVoidTy())
408 Captures = false;
409
410 auto AddUsersToWorklistIfCapturing = [&] {
411 if (Captures)
412 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000413 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000414 Worklist.push_back(&UU);
415 };
416
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000417 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000418 if (CS.doesNotAccessMemory()) {
419 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000420 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000421 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000422
423 Function *F = CS.getCalledFunction();
424 if (!F) {
425 if (CS.onlyReadsMemory()) {
426 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000427 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000428 continue;
429 }
430 return Attribute::None;
431 }
432
433 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
434 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
435 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
436 if (A->get() == V) {
437 if (AI == AE) {
438 assert(F->isVarArg() &&
439 "More params than args in non-varargs call.");
440 return Attribute::None;
441 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000442 Captures &= !CS.doesNotCapture(A - B);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000443 if (SCCNodes.count(AI))
444 continue;
445 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
446 return Attribute::None;
447 if (!CS.doesNotAccessMemory(A - B))
448 IsRead = true;
449 }
450 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000451 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000452 break;
453 }
454
455 case Instruction::Load:
456 IsRead = true;
457 break;
458
459 case Instruction::ICmp:
460 case Instruction::Ret:
461 break;
462
463 default:
464 return Attribute::None;
465 }
466 }
467
468 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
469}
470
Chandler Carrutha632fb92015-09-13 06:57:25 +0000471/// Deduce nocapture attributes for the SCC.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000472bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000473 bool Changed = false;
474
Chandler Carruth63559d72015-09-13 06:47:20 +0000475 SmallPtrSet<Function *, 8> SCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000476
477 // Fill SCCNodes with the elements of the SCC. Used for quickly
478 // looking up whether a given CallGraphNode is in this SCC.
479 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
480 Function *F = (*I)->getFunction();
Chandler Carruth0fb99812014-08-13 10:49:33 +0000481 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
482 !F->hasFnAttribute(Attribute::OptimizeNone))
Nick Lewycky4c378a42011-12-28 23:24:21 +0000483 SCCNodes.insert(F);
484 }
485
486 ArgumentGraph AG;
487
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000488 AttrBuilder B;
489 B.addAttribute(Attribute::NoCapture);
490
Duncan Sands44c8cd92008-12-31 16:14:43 +0000491 // Check each function in turn, determining which pointer arguments are not
492 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000493 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
494 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000495
Chandler Carruth0fb99812014-08-13 10:49:33 +0000496 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
497 // External node or function we're trying not to optimize - only a problem
498 // for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000499 continue;
500
501 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000502 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000503 if (F->isDeclaration() || F->mayBeOverridden())
504 continue;
505
Nick Lewycky4c378a42011-12-28 23:24:21 +0000506 // Functions that are readonly (or readnone) and nounwind and don't return
507 // a value can't capture arguments. Don't analyze them.
508 if (F->onlyReadsMemory() && F->doesNotThrow() &&
509 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000510 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
511 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000512 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
513 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
514 ++NumNoCapture;
515 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000516 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000517 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000518 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000519 }
520
Chandler Carruth63559d72015-09-13 06:47:20 +0000521 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
522 ++A) {
523 if (!A->getType()->isPointerTy())
524 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000525 bool HasNonLocalUses = false;
526 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000527 ArgumentUsesTracker Tracker(SCCNodes);
528 PointerMayBeCaptured(A, &Tracker);
529 if (!Tracker.Captured) {
530 if (Tracker.Uses.empty()) {
531 // If it's trivially not captured, mark it nocapture now.
Chandler Carruth63559d72015-09-13 06:47:20 +0000532 A->addAttr(
533 AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000534 ++NumNoCapture;
535 Changed = true;
536 } else {
537 // If it's not trivially captured and not trivially not captured,
538 // then it must be calling into another function in our SCC. Save
539 // its particulars for Argument-SCC analysis later.
540 ArgumentGraphNode *Node = AG[A];
Chandler Carruth63559d72015-09-13 06:47:20 +0000541 for (SmallVectorImpl<Argument *>::iterator
542 UI = Tracker.Uses.begin(),
543 UE = Tracker.Uses.end();
544 UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000545 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000546 if (*UI != A)
547 HasNonLocalUses = true;
548 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000549 }
550 }
551 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
552 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000553 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
554 // Can we determine that it's readonly/readnone without doing an SCC?
555 // Note that we don't allow any calls at all here, or else our result
556 // will be dependent on the iteration order through the functions in the
557 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000558 SmallPtrSet<Argument *, 8> Self;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000559 Self.insert(A);
560 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
561 if (R != Attribute::None) {
562 AttrBuilder B;
563 B.addAttribute(R);
564 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
565 Changed = true;
566 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
567 }
568 }
569 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000570 }
571
572 // The graph we've collected is partial because we stopped scanning for
573 // argument uses once we solved the argument trivially. These partial nodes
574 // show up as ArgumentGraphNode objects with an empty Uses list, and for
575 // these nodes the final decision about whether they capture has already been
576 // made. If the definition doesn't have a 'nocapture' attribute by now, it
577 // captures.
578
Chandler Carruth63559d72015-09-13 06:47:20 +0000579 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000580 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000581 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000582 if (!ArgumentSCC[0]->Definition)
583 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000584
585 // eg. "void f(int* x) { if (...) f(x); }"
586 if (ArgumentSCC[0]->Uses.size() == 1 &&
587 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000588 Argument *A = ArgumentSCC[0]->Definition;
589 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000590 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000591 Changed = true;
592 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000593 continue;
594 }
595
596 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000597 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
598 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000599 ArgumentGraphNode *Node = *I;
600 if (Node->Uses.empty()) {
601 if (!Node->Definition->hasNoCaptureAttr())
602 SCCCaptured = true;
603 }
604 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000605 if (SCCCaptured)
606 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000607
Chandler Carruth63559d72015-09-13 06:47:20 +0000608 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000609 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
610 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000611 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000612 ArgumentSCCNodes.insert((*I)->Definition);
613 }
614
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000615 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
616 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000617 ArgumentGraphNode *N = *I;
Chandler Carruth63559d72015-09-13 06:47:20 +0000618 for (SmallVectorImpl<ArgumentGraphNode *>::iterator UI = N->Uses.begin(),
619 UE = N->Uses.end();
620 UI != UE; ++UI) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000621 Argument *A = (*UI)->Definition;
622 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
623 continue;
624 SCCCaptured = true;
625 break;
626 }
627 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000628 if (SCCCaptured)
629 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000630
Nick Lewyckyf740db32012-01-05 22:21:45 +0000631 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000632 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000633 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000634 ++NumNoCapture;
635 Changed = true;
636 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000637
638 // We also want to compute readonly/readnone. With a small number of false
639 // negatives, we can assume that any pointer which is captured isn't going
640 // to be provably readonly or readnone, since by definition we can't
641 // analyze all uses of a captured pointer.
642 //
643 // The false negatives happen when the pointer is captured by a function
644 // that promises readonly/readnone behaviour on the pointer, then the
645 // pointer's lifetime ends before anything that writes to arbitrary memory.
646 // Also, a readonly/readnone pointer may be returned, but returning a
647 // pointer is capturing it.
648
649 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
650 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
651 Argument *A = ArgumentSCC[i]->Definition;
652 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
653 if (K == Attribute::ReadNone)
654 continue;
655 if (K == Attribute::ReadOnly) {
656 ReadAttr = Attribute::ReadOnly;
657 continue;
658 }
659 ReadAttr = K;
660 break;
661 }
662
663 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000664 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000665 B.addAttribute(ReadAttr);
Chandler Carruth63559d72015-09-13 06:47:20 +0000666 R.addAttribute(Attribute::ReadOnly).addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000667 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
668 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000669 // Clear out existing readonly/readnone attributes
670 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000671 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
672 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
673 Changed = true;
674 }
675 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000676 }
677
678 return Changed;
679}
680
Chandler Carrutha632fb92015-09-13 06:57:25 +0000681/// Tests whether a function is "malloc-like".
682///
683/// A function is "malloc-like" if it returns either null or a pointer that
684/// doesn't alias any other pointer visible to the caller.
Chandler Carruth63559d72015-09-13 06:47:20 +0000685bool FunctionAttrs::IsFunctionMallocLike(
686 Function *F, SmallPtrSet<Function *, 8> &SCCNodes) const {
Benjamin Kramer15591272012-10-31 13:45:49 +0000687 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000688 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
689 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
690 FlowsToReturn.insert(Ret->getReturnValue());
691
692 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000693 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000694
695 if (Constant *C = dyn_cast<Constant>(RetVal)) {
696 if (!C->isNullValue() && !isa<UndefValue>(C))
697 return false;
698
699 continue;
700 }
701
702 if (isa<Argument>(RetVal))
703 return false;
704
705 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
706 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000707 // Extend the analysis by looking upwards.
708 case Instruction::BitCast:
709 case Instruction::GetElementPtr:
710 case Instruction::AddrSpaceCast:
711 FlowsToReturn.insert(RVI->getOperand(0));
712 continue;
713 case Instruction::Select: {
714 SelectInst *SI = cast<SelectInst>(RVI);
715 FlowsToReturn.insert(SI->getTrueValue());
716 FlowsToReturn.insert(SI->getFalseValue());
717 continue;
718 }
719 case Instruction::PHI: {
720 PHINode *PN = cast<PHINode>(RVI);
721 for (Value *IncValue : PN->incoming_values())
722 FlowsToReturn.insert(IncValue);
723 continue;
724 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000725
Chandler Carruth63559d72015-09-13 06:47:20 +0000726 // Check whether the pointer came from an allocation.
727 case Instruction::Alloca:
728 break;
729 case Instruction::Call:
730 case Instruction::Invoke: {
731 CallSite CS(RVI);
732 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000733 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000734 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
735 break;
736 } // fall-through
737 default:
738 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000739 }
740
Dan Gohman94e61762009-11-19 21:57:48 +0000741 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000742 return false;
743 }
744
745 return true;
746}
747
Chandler Carrutha632fb92015-09-13 06:57:25 +0000748/// Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000749bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000750 SmallPtrSet<Function *, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000751
752 // Fill SCCNodes with the elements of the SCC. Used for quickly
753 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000754 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
755 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000756
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000757 // Check each function in turn, determining which functions return noalias
758 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000759 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
760 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000761
Chandler Carruth0fb99812014-08-13 10:49:33 +0000762 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
763 // External node or node we don't want to optimize - skip it;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000764 return false;
765
766 // Already noalias.
767 if (F->doesNotAlias(0))
768 continue;
769
770 // Definitions with weak linkage may be overridden at linktime, so
771 // treat them like declarations.
772 if (F->isDeclaration() || F->mayBeOverridden())
773 return false;
774
Chandler Carruth63559d72015-09-13 06:47:20 +0000775 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000776 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000777 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000778 continue;
779
780 if (!IsFunctionMallocLike(F, SCCNodes))
781 return false;
782 }
783
784 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000785 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
786 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000787 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000788 continue;
789
790 F->setDoesNotAlias(0);
791 ++NumNoAlias;
792 MadeChange = true;
793 }
794
795 return MadeChange;
796}
797
Chandler Carrutha632fb92015-09-13 06:57:25 +0000798/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000799///
800/// Requires that the function returns a pointer.
801///
802/// Returns true if it believes the function will not return a null, and sets
803/// \p Speculative based on whether the returned conclusion is a speculative
804/// conclusion due to SCC calls.
805static bool isReturnNonNull(Function *F, SmallPtrSet<Function *, 8> &SCCNodes,
806 const TargetLibraryInfo &TLI, bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000807 assert(F->getReturnType()->isPointerTy() &&
808 "nonnull only meaningful on pointer types");
809 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000810
Philip Reamesa88caea2015-08-31 19:44:38 +0000811 SmallSetVector<Value *, 8> FlowsToReturn;
812 for (BasicBlock &BB : *F)
813 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
814 FlowsToReturn.insert(Ret->getReturnValue());
815
816 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
817 Value *RetVal = FlowsToReturn[i];
818
819 // If this value is locally known to be non-null, we're good
Chandler Carruth8874b782015-09-13 08:17:14 +0000820 if (isKnownNonNull(RetVal, &TLI))
Philip Reamesa88caea2015-08-31 19:44:38 +0000821 continue;
822
823 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000824 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000825 Instruction *RVI = dyn_cast<Instruction>(RetVal);
826 if (!RVI)
827 return false;
828 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000829 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000830 case Instruction::BitCast:
831 case Instruction::GetElementPtr:
832 case Instruction::AddrSpaceCast:
833 FlowsToReturn.insert(RVI->getOperand(0));
834 continue;
835 case Instruction::Select: {
836 SelectInst *SI = cast<SelectInst>(RVI);
837 FlowsToReturn.insert(SI->getTrueValue());
838 FlowsToReturn.insert(SI->getFalseValue());
839 continue;
840 }
841 case Instruction::PHI: {
842 PHINode *PN = cast<PHINode>(RVI);
843 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
844 FlowsToReturn.insert(PN->getIncomingValue(i));
845 continue;
846 }
847 case Instruction::Call:
848 case Instruction::Invoke: {
849 CallSite CS(RVI);
850 Function *Callee = CS.getCalledFunction();
851 // A call to a node within the SCC is assumed to return null until
852 // proven otherwise
853 if (Callee && SCCNodes.count(Callee)) {
854 Speculative = true;
855 continue;
856 }
857 return false;
858 }
859 default:
Chandler Carruth63559d72015-09-13 06:47:20 +0000860 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +0000861 };
862 llvm_unreachable("should have either continued or returned");
863 }
864
865 return true;
866}
867
Chandler Carrutha632fb92015-09-13 06:57:25 +0000868/// Deduce nonnull attributes for the SCC.
Philip Reamesa88caea2015-08-31 19:44:38 +0000869bool FunctionAttrs::AddNonNullAttrs(const CallGraphSCC &SCC) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000870 SmallPtrSet<Function *, 8> SCCNodes;
Philip Reamesa88caea2015-08-31 19:44:38 +0000871
872 // Fill SCCNodes with the elements of the SCC. Used for quickly
873 // looking up whether a given CallGraphNode is in this SCC.
874 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
875 SCCNodes.insert((*I)->getFunction());
876
877 // Speculative that all functions in the SCC return only nonnull
878 // pointers. We may refute this as we analyze functions.
879 bool SCCReturnsNonNull = true;
880
881 bool MadeChange = false;
882
883 // Check each function in turn, determining which functions return nonnull
884 // pointers.
885 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
886 Function *F = (*I)->getFunction();
887
888 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
889 // External node or node we don't want to optimize - skip it;
890 return false;
891
892 // Already nonnull.
893 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
894 Attribute::NonNull))
895 continue;
896
897 // Definitions with weak linkage may be overridden at linktime, so
898 // treat them like declarations.
899 if (F->isDeclaration() || F->mayBeOverridden())
900 return false;
901
Chandler Carruth63559d72015-09-13 06:47:20 +0000902 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +0000903 // pointer types.
904 if (!F->getReturnType()->isPointerTy())
905 continue;
906
907 bool Speculative = false;
Chandler Carruth8874b782015-09-13 08:17:14 +0000908 if (isReturnNonNull(F, SCCNodes, *TLI, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000909 if (!Speculative) {
910 // Mark the function eagerly since we may discover a function
911 // which prevents us from speculating about the entire SCC
912 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
913 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
914 ++NumNonNullReturn;
915 MadeChange = true;
916 }
917 continue;
918 }
919 // At least one function returns something which could be null, can't
920 // speculate any more.
921 SCCReturnsNonNull = false;
922 }
923
924 if (SCCReturnsNonNull) {
925 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
926 Function *F = (*I)->getFunction();
927 if (F->getAttributes().hasAttribute(AttributeSet::ReturnIndex,
928 Attribute::NonNull) ||
929 !F->getReturnType()->isPointerTy())
930 continue;
931
932 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
933 F->addAttribute(AttributeSet::ReturnIndex, Attribute::NonNull);
934 ++NumNonNullReturn;
935 MadeChange = true;
936 }
937 }
938
939 return MadeChange;
940}
941
Chandler Carruthd0245202015-09-13 07:50:43 +0000942static void setDoesNotAccessMemory(Function &F) {
943 if (!F.doesNotAccessMemory()) {
944 F.setDoesNotAccessMemory();
945 ++NumAnnotated;
946 }
947}
948
949static void setOnlyReadsMemory(Function &F) {
950 if (!F.onlyReadsMemory()) {
951 F.setOnlyReadsMemory();
952 ++NumAnnotated;
953 }
954}
955
956static void setDoesNotThrow(Function &F) {
957 if (!F.doesNotThrow()) {
958 F.setDoesNotThrow();
959 ++NumAnnotated;
960 }
961}
962
963static void setDoesNotCapture(Function &F, unsigned n) {
964 if (!F.doesNotCapture(n)) {
965 F.setDoesNotCapture(n);
966 ++NumAnnotated;
967 }
968}
969
970static void setOnlyReadsMemory(Function &F, unsigned n) {
971 if (!F.onlyReadsMemory(n)) {
972 F.setOnlyReadsMemory(n);
973 ++NumAnnotated;
974 }
975}
976
977static void setDoesNotAlias(Function &F, unsigned n) {
978 if (!F.doesNotAlias(n)) {
979 F.setDoesNotAlias(n);
980 ++NumAnnotated;
981 }
982}
983
Chandler Carrutha632fb92015-09-13 06:57:25 +0000984/// Analyze the name and prototype of the given function and set any applicable
985/// attributes.
986///
987/// Returns true if any attributes were set and false otherwise.
Chandler Carruth444d0052015-09-13 08:03:23 +0000988static bool inferPrototypeAttributes(Function &F, const TargetLibraryInfo &TLI) {
Chandler Carruth0fb99812014-08-13 10:49:33 +0000989 if (F.hasFnAttribute(Attribute::OptimizeNone))
990 return false;
991
Meador Inge6b6a1612013-03-21 00:55:59 +0000992 FunctionType *FTy = F.getFunctionType();
993 LibFunc::Func TheLibFunc;
Chandler Carruth444d0052015-09-13 08:03:23 +0000994 if (!(TLI.getLibFunc(F.getName(), TheLibFunc) && TLI.has(TheLibFunc)))
Meador Inge6b6a1612013-03-21 00:55:59 +0000995 return false;
996
997 switch (TheLibFunc) {
998 case LibFunc::strlen:
999 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1000 return false;
1001 setOnlyReadsMemory(F);
1002 setDoesNotThrow(F);
1003 setDoesNotCapture(F, 1);
1004 break;
1005 case LibFunc::strchr:
1006 case LibFunc::strrchr:
Chandler Carruth63559d72015-09-13 06:47:20 +00001007 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001008 !FTy->getParamType(1)->isIntegerTy())
1009 return false;
1010 setOnlyReadsMemory(F);
1011 setDoesNotThrow(F);
1012 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001013 case LibFunc::strtol:
1014 case LibFunc::strtod:
1015 case LibFunc::strtof:
1016 case LibFunc::strtoul:
1017 case LibFunc::strtoll:
1018 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +00001019 case LibFunc::strtoull:
Chandler Carruth63559d72015-09-13 06:47:20 +00001020 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001021 return false;
1022 setDoesNotThrow(F);
1023 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001024 setOnlyReadsMemory(F, 1);
1025 break;
1026 case LibFunc::strcpy:
1027 case LibFunc::stpcpy:
1028 case LibFunc::strcat:
1029 case LibFunc::strncat:
1030 case LibFunc::strncpy:
1031 case LibFunc::stpncpy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001032 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001033 return false;
1034 setDoesNotThrow(F);
1035 setDoesNotCapture(F, 2);
1036 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001037 break;
1038 case LibFunc::strxfrm:
Chandler Carruth63559d72015-09-13 06:47:20 +00001039 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001040 !FTy->getParamType(1)->isPointerTy())
1041 return false;
1042 setDoesNotThrow(F);
1043 setDoesNotCapture(F, 1);
1044 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001045 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001046 break;
Chandler Carruth63559d72015-09-13 06:47:20 +00001047 case LibFunc::strcmp: // 0,1
1048 case LibFunc::strspn: // 0,1
1049 case LibFunc::strncmp: // 0,1
1050 case LibFunc::strcspn: // 0,1
1051 case LibFunc::strcoll: // 0,1
1052 case LibFunc::strcasecmp: // 0,1
1053 case LibFunc::strncasecmp: //
1054 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001055 !FTy->getParamType(1)->isPointerTy())
1056 return false;
1057 setOnlyReadsMemory(F);
1058 setDoesNotThrow(F);
1059 setDoesNotCapture(F, 1);
1060 setDoesNotCapture(F, 2);
1061 break;
1062 case LibFunc::strstr:
1063 case LibFunc::strpbrk:
1064 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1065 return false;
1066 setOnlyReadsMemory(F);
1067 setDoesNotThrow(F);
1068 setDoesNotCapture(F, 2);
1069 break;
1070 case LibFunc::strtok:
1071 case LibFunc::strtok_r:
1072 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
1073 return false;
1074 setDoesNotThrow(F);
1075 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001076 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001077 break;
1078 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001079 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1080 return false;
1081 setDoesNotThrow(F);
1082 setDoesNotCapture(F, 1);
1083 setOnlyReadsMemory(F, 1);
1084 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001085 case LibFunc::setbuf:
1086 case LibFunc::setvbuf:
1087 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
1088 return false;
1089 setDoesNotThrow(F);
1090 setDoesNotCapture(F, 1);
1091 break;
1092 case LibFunc::strdup:
1093 case LibFunc::strndup:
1094 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
1095 !FTy->getParamType(0)->isPointerTy())
1096 return false;
1097 setDoesNotThrow(F);
1098 setDoesNotAlias(F, 0);
1099 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001100 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001101 break;
1102 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +00001103 case LibFunc::statvfs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001104 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001105 !FTy->getParamType(1)->isPointerTy())
1106 return false;
1107 setDoesNotThrow(F);
1108 setDoesNotCapture(F, 1);
1109 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001110 setOnlyReadsMemory(F, 1);
1111 break;
1112 case LibFunc::sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001113 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001114 !FTy->getParamType(1)->isPointerTy())
1115 return false;
1116 setDoesNotThrow(F);
1117 setDoesNotCapture(F, 1);
1118 setDoesNotCapture(F, 2);
1119 setOnlyReadsMemory(F, 1);
1120 setOnlyReadsMemory(F, 2);
1121 break;
1122 case LibFunc::sprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001123 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001124 !FTy->getParamType(1)->isPointerTy())
1125 return false;
1126 setDoesNotThrow(F);
1127 setDoesNotCapture(F, 1);
1128 setDoesNotCapture(F, 2);
1129 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001130 break;
1131 case LibFunc::snprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001132 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001133 !FTy->getParamType(2)->isPointerTy())
1134 return false;
1135 setDoesNotThrow(F);
1136 setDoesNotCapture(F, 1);
1137 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001138 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001139 break;
1140 case LibFunc::setitimer:
Chandler Carruth63559d72015-09-13 06:47:20 +00001141 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001142 !FTy->getParamType(2)->isPointerTy())
1143 return false;
1144 setDoesNotThrow(F);
1145 setDoesNotCapture(F, 2);
1146 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001147 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001148 break;
1149 case LibFunc::system:
Chandler Carruth63559d72015-09-13 06:47:20 +00001150 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001151 return false;
1152 // May throw; "system" is a valid pthread cancellation point.
1153 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001154 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001155 break;
1156 case LibFunc::malloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001157 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001158 return false;
1159 setDoesNotThrow(F);
1160 setDoesNotAlias(F, 0);
1161 break;
1162 case LibFunc::memcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001163 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001164 !FTy->getParamType(1)->isPointerTy())
1165 return false;
1166 setOnlyReadsMemory(F);
1167 setDoesNotThrow(F);
1168 setDoesNotCapture(F, 1);
1169 setDoesNotCapture(F, 2);
1170 break;
1171 case LibFunc::memchr:
1172 case LibFunc::memrchr:
1173 if (FTy->getNumParams() != 3)
1174 return false;
1175 setOnlyReadsMemory(F);
1176 setDoesNotThrow(F);
1177 break;
1178 case LibFunc::modf:
1179 case LibFunc::modff:
1180 case LibFunc::modfl:
Chandler Carruth63559d72015-09-13 06:47:20 +00001181 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001182 return false;
1183 setDoesNotThrow(F);
1184 setDoesNotCapture(F, 2);
1185 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001186 case LibFunc::memcpy:
1187 case LibFunc::memccpy:
1188 case LibFunc::memmove:
Chandler Carruth63559d72015-09-13 06:47:20 +00001189 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001190 return false;
1191 setDoesNotThrow(F);
1192 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001193 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001194 break;
1195 case LibFunc::memalign:
1196 if (!FTy->getReturnType()->isPointerTy())
1197 return false;
1198 setDoesNotAlias(F, 0);
1199 break;
1200 case LibFunc::mkdir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001201 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001202 return false;
1203 setDoesNotThrow(F);
1204 setDoesNotCapture(F, 1);
1205 setOnlyReadsMemory(F, 1);
1206 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001207 case LibFunc::mktime:
Chandler Carruth63559d72015-09-13 06:47:20 +00001208 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001209 return false;
1210 setDoesNotThrow(F);
1211 setDoesNotCapture(F, 1);
1212 break;
1213 case LibFunc::realloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001214 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001215 !FTy->getReturnType()->isPointerTy())
1216 return false;
1217 setDoesNotThrow(F);
1218 setDoesNotAlias(F, 0);
1219 setDoesNotCapture(F, 1);
1220 break;
1221 case LibFunc::read:
Chandler Carruth63559d72015-09-13 06:47:20 +00001222 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001223 return false;
1224 // May throw; "read" is a valid pthread cancellation point.
1225 setDoesNotCapture(F, 2);
1226 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001227 case LibFunc::rewind:
Chandler Carruth63559d72015-09-13 06:47:20 +00001228 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001229 return false;
1230 setDoesNotThrow(F);
1231 setDoesNotCapture(F, 1);
1232 break;
1233 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001234 case LibFunc::remove:
1235 case LibFunc::realpath:
Chandler Carruth63559d72015-09-13 06:47:20 +00001236 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001237 return false;
1238 setDoesNotThrow(F);
1239 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001240 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001241 break;
1242 case LibFunc::rename:
Chandler Carruth63559d72015-09-13 06:47:20 +00001243 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001244 !FTy->getParamType(1)->isPointerTy())
1245 return false;
1246 setDoesNotThrow(F);
1247 setDoesNotCapture(F, 1);
1248 setDoesNotCapture(F, 2);
1249 setOnlyReadsMemory(F, 1);
1250 setOnlyReadsMemory(F, 2);
1251 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001252 case LibFunc::readlink:
Chandler Carruth63559d72015-09-13 06:47:20 +00001253 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001254 !FTy->getParamType(1)->isPointerTy())
1255 return false;
1256 setDoesNotThrow(F);
1257 setDoesNotCapture(F, 1);
1258 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001259 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001260 break;
1261 case LibFunc::write:
1262 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1263 return false;
1264 // May throw; "write" is a valid pthread cancellation point.
1265 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001266 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001267 break;
1268 case LibFunc::bcopy:
Chandler Carruth63559d72015-09-13 06:47:20 +00001269 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001270 !FTy->getParamType(1)->isPointerTy())
1271 return false;
1272 setDoesNotThrow(F);
1273 setDoesNotCapture(F, 1);
1274 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001275 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001276 break;
1277 case LibFunc::bcmp:
Chandler Carruth63559d72015-09-13 06:47:20 +00001278 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001279 !FTy->getParamType(1)->isPointerTy())
1280 return false;
1281 setDoesNotThrow(F);
1282 setOnlyReadsMemory(F);
1283 setDoesNotCapture(F, 1);
1284 setDoesNotCapture(F, 2);
1285 break;
1286 case LibFunc::bzero:
1287 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1288 return false;
1289 setDoesNotThrow(F);
1290 setDoesNotCapture(F, 1);
1291 break;
1292 case LibFunc::calloc:
Chandler Carruth63559d72015-09-13 06:47:20 +00001293 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001294 return false;
1295 setDoesNotThrow(F);
1296 setDoesNotAlias(F, 0);
1297 break;
1298 case LibFunc::chmod:
1299 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001300 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1301 return false;
1302 setDoesNotThrow(F);
1303 setDoesNotCapture(F, 1);
1304 setOnlyReadsMemory(F, 1);
1305 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001306 case LibFunc::ctermid:
1307 case LibFunc::clearerr:
1308 case LibFunc::closedir:
1309 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1310 return false;
1311 setDoesNotThrow(F);
1312 setDoesNotCapture(F, 1);
1313 break;
1314 case LibFunc::atoi:
1315 case LibFunc::atol:
1316 case LibFunc::atof:
1317 case LibFunc::atoll:
1318 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1319 return false;
1320 setDoesNotThrow(F);
1321 setOnlyReadsMemory(F);
1322 setDoesNotCapture(F, 1);
1323 break;
1324 case LibFunc::access:
1325 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1326 return false;
1327 setDoesNotThrow(F);
1328 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001329 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001330 break;
1331 case LibFunc::fopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001332 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001333 !FTy->getParamType(0)->isPointerTy() ||
1334 !FTy->getParamType(1)->isPointerTy())
1335 return false;
1336 setDoesNotThrow(F);
1337 setDoesNotAlias(F, 0);
1338 setDoesNotCapture(F, 1);
1339 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001340 setOnlyReadsMemory(F, 1);
1341 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001342 break;
1343 case LibFunc::fdopen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001344 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001345 !FTy->getParamType(1)->isPointerTy())
1346 return false;
1347 setDoesNotThrow(F);
1348 setDoesNotAlias(F, 0);
1349 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001350 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001351 break;
1352 case LibFunc::feof:
1353 case LibFunc::free:
1354 case LibFunc::fseek:
1355 case LibFunc::ftell:
1356 case LibFunc::fgetc:
1357 case LibFunc::fseeko:
1358 case LibFunc::ftello:
1359 case LibFunc::fileno:
1360 case LibFunc::fflush:
1361 case LibFunc::fclose:
1362 case LibFunc::fsetpos:
1363 case LibFunc::flockfile:
1364 case LibFunc::funlockfile:
1365 case LibFunc::ftrylockfile:
1366 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1367 return false;
1368 setDoesNotThrow(F);
1369 setDoesNotCapture(F, 1);
1370 break;
1371 case LibFunc::ferror:
1372 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1373 return false;
1374 setDoesNotThrow(F);
1375 setDoesNotCapture(F, 1);
1376 setOnlyReadsMemory(F);
1377 break;
1378 case LibFunc::fputc:
1379 case LibFunc::fstat:
1380 case LibFunc::frexp:
1381 case LibFunc::frexpf:
1382 case LibFunc::frexpl:
1383 case LibFunc::fstatvfs:
1384 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1385 return false;
1386 setDoesNotThrow(F);
1387 setDoesNotCapture(F, 2);
1388 break;
1389 case LibFunc::fgets:
Chandler Carruth63559d72015-09-13 06:47:20 +00001390 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001391 !FTy->getParamType(2)->isPointerTy())
1392 return false;
1393 setDoesNotThrow(F);
1394 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001395 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001396 case LibFunc::fread:
Chandler Carruth63559d72015-09-13 06:47:20 +00001397 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001398 !FTy->getParamType(3)->isPointerTy())
1399 return false;
1400 setDoesNotThrow(F);
1401 setDoesNotCapture(F, 1);
1402 setDoesNotCapture(F, 4);
1403 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001404 case LibFunc::fwrite:
Chandler Carruth63559d72015-09-13 06:47:20 +00001405 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001406 !FTy->getParamType(3)->isPointerTy())
1407 return false;
1408 setDoesNotThrow(F);
1409 setDoesNotCapture(F, 1);
1410 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001411 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001412 case LibFunc::fputs:
Chandler Carruth63559d72015-09-13 06:47:20 +00001413 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001414 !FTy->getParamType(1)->isPointerTy())
1415 return false;
1416 setDoesNotThrow(F);
1417 setDoesNotCapture(F, 1);
1418 setDoesNotCapture(F, 2);
1419 setOnlyReadsMemory(F, 1);
1420 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001421 case LibFunc::fscanf:
1422 case LibFunc::fprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001423 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001424 !FTy->getParamType(1)->isPointerTy())
1425 return false;
1426 setDoesNotThrow(F);
1427 setDoesNotCapture(F, 1);
1428 setDoesNotCapture(F, 2);
1429 setOnlyReadsMemory(F, 2);
1430 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001431 case LibFunc::fgetpos:
Chandler Carruth63559d72015-09-13 06:47:20 +00001432 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001433 !FTy->getParamType(1)->isPointerTy())
1434 return false;
1435 setDoesNotThrow(F);
1436 setDoesNotCapture(F, 1);
1437 setDoesNotCapture(F, 2);
1438 break;
1439 case LibFunc::getc:
1440 case LibFunc::getlogin_r:
1441 case LibFunc::getc_unlocked:
1442 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1443 return false;
1444 setDoesNotThrow(F);
1445 setDoesNotCapture(F, 1);
1446 break;
1447 case LibFunc::getenv:
1448 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1449 return false;
1450 setDoesNotThrow(F);
1451 setOnlyReadsMemory(F);
1452 setDoesNotCapture(F, 1);
1453 break;
1454 case LibFunc::gets:
1455 case LibFunc::getchar:
1456 setDoesNotThrow(F);
1457 break;
1458 case LibFunc::getitimer:
1459 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1460 return false;
1461 setDoesNotThrow(F);
1462 setDoesNotCapture(F, 2);
1463 break;
1464 case LibFunc::getpwnam:
1465 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1466 return false;
1467 setDoesNotThrow(F);
1468 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001469 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001470 break;
1471 case LibFunc::ungetc:
1472 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1473 return false;
1474 setDoesNotThrow(F);
1475 setDoesNotCapture(F, 2);
1476 break;
1477 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001478 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1479 return false;
1480 setDoesNotThrow(F);
1481 setDoesNotCapture(F, 1);
1482 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001483 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001484 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1485 return false;
1486 setDoesNotThrow(F);
1487 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001488 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001489 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001490 case LibFunc::unsetenv:
1491 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1492 return false;
1493 setDoesNotThrow(F);
1494 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001495 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001496 break;
1497 case LibFunc::utime:
1498 case LibFunc::utimes:
Chandler Carruth63559d72015-09-13 06:47:20 +00001499 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001500 !FTy->getParamType(1)->isPointerTy())
1501 return false;
1502 setDoesNotThrow(F);
1503 setDoesNotCapture(F, 1);
1504 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001505 setOnlyReadsMemory(F, 1);
1506 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001507 break;
1508 case LibFunc::putc:
1509 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1510 return false;
1511 setDoesNotThrow(F);
1512 setDoesNotCapture(F, 2);
1513 break;
1514 case LibFunc::puts:
1515 case LibFunc::printf:
1516 case LibFunc::perror:
1517 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1518 return false;
1519 setDoesNotThrow(F);
1520 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001521 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001522 break;
1523 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001524 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1525 return false;
1526 // May throw; "pread" is a valid pthread cancellation point.
1527 setDoesNotCapture(F, 2);
1528 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001529 case LibFunc::pwrite:
1530 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1531 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001532 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001533 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001534 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001535 break;
1536 case LibFunc::putchar:
1537 setDoesNotThrow(F);
1538 break;
1539 case LibFunc::popen:
Chandler Carruth63559d72015-09-13 06:47:20 +00001540 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001541 !FTy->getParamType(0)->isPointerTy() ||
1542 !FTy->getParamType(1)->isPointerTy())
1543 return false;
1544 setDoesNotThrow(F);
1545 setDoesNotAlias(F, 0);
1546 setDoesNotCapture(F, 1);
1547 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001548 setOnlyReadsMemory(F, 1);
1549 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001550 break;
1551 case LibFunc::pclose:
1552 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1553 return false;
1554 setDoesNotThrow(F);
1555 setDoesNotCapture(F, 1);
1556 break;
1557 case LibFunc::vscanf:
1558 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1559 return false;
1560 setDoesNotThrow(F);
1561 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001562 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001563 break;
1564 case LibFunc::vsscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001565 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001566 !FTy->getParamType(2)->isPointerTy())
1567 return false;
1568 setDoesNotThrow(F);
1569 setDoesNotCapture(F, 1);
1570 setDoesNotCapture(F, 2);
1571 setOnlyReadsMemory(F, 1);
1572 setOnlyReadsMemory(F, 2);
1573 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001574 case LibFunc::vfscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001575 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001576 !FTy->getParamType(2)->isPointerTy())
1577 return false;
1578 setDoesNotThrow(F);
1579 setDoesNotCapture(F, 1);
1580 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001581 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001582 break;
1583 case LibFunc::valloc:
1584 if (!FTy->getReturnType()->isPointerTy())
1585 return false;
1586 setDoesNotThrow(F);
1587 setDoesNotAlias(F, 0);
1588 break;
1589 case LibFunc::vprintf:
1590 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->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::vfprintf:
1597 case LibFunc::vsprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001598 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001599 !FTy->getParamType(1)->isPointerTy())
1600 return false;
1601 setDoesNotThrow(F);
1602 setDoesNotCapture(F, 1);
1603 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001604 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001605 break;
1606 case LibFunc::vsnprintf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001607 if (FTy->getNumParams() != 4 || !FTy->getParamType(0)->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, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001613 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001614 break;
1615 case LibFunc::open:
1616 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1617 return false;
1618 // May throw; "open" is a valid pthread cancellation point.
1619 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001620 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001621 break;
1622 case LibFunc::opendir:
Chandler Carruth63559d72015-09-13 06:47:20 +00001623 if (FTy->getNumParams() != 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001624 !FTy->getParamType(0)->isPointerTy())
1625 return false;
1626 setDoesNotThrow(F);
1627 setDoesNotAlias(F, 0);
1628 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001629 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001630 break;
1631 case LibFunc::tmpfile:
1632 if (!FTy->getReturnType()->isPointerTy())
1633 return false;
1634 setDoesNotThrow(F);
1635 setDoesNotAlias(F, 0);
1636 break;
1637 case LibFunc::times:
1638 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1639 return false;
1640 setDoesNotThrow(F);
1641 setDoesNotCapture(F, 1);
1642 break;
1643 case LibFunc::htonl:
1644 case LibFunc::htons:
1645 case LibFunc::ntohl:
1646 case LibFunc::ntohs:
1647 setDoesNotThrow(F);
1648 setDoesNotAccessMemory(F);
1649 break;
1650 case LibFunc::lstat:
Chandler Carruth63559d72015-09-13 06:47:20 +00001651 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001652 !FTy->getParamType(1)->isPointerTy())
1653 return false;
1654 setDoesNotThrow(F);
1655 setDoesNotCapture(F, 1);
1656 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001657 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001658 break;
1659 case LibFunc::lchown:
1660 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1661 return false;
1662 setDoesNotThrow(F);
1663 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001664 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001665 break;
1666 case LibFunc::qsort:
1667 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1668 return false;
1669 // May throw; places call through function pointer.
1670 setDoesNotCapture(F, 4);
1671 break;
1672 case LibFunc::dunder_strdup:
1673 case LibFunc::dunder_strndup:
Chandler Carruth63559d72015-09-13 06:47:20 +00001674 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001675 !FTy->getParamType(0)->isPointerTy())
1676 return false;
1677 setDoesNotThrow(F);
1678 setDoesNotAlias(F, 0);
1679 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001680 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001681 break;
1682 case LibFunc::dunder_strtok_r:
Chandler Carruth63559d72015-09-13 06:47:20 +00001683 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001684 return false;
1685 setDoesNotThrow(F);
1686 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001687 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001688 break;
1689 case LibFunc::under_IO_getc:
1690 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1691 return false;
1692 setDoesNotThrow(F);
1693 setDoesNotCapture(F, 1);
1694 break;
1695 case LibFunc::under_IO_putc:
1696 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1697 return false;
1698 setDoesNotThrow(F);
1699 setDoesNotCapture(F, 2);
1700 break;
1701 case LibFunc::dunder_isoc99_scanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001702 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
Meador Inge6b6a1612013-03-21 00:55:59 +00001703 return false;
1704 setDoesNotThrow(F);
1705 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001706 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001707 break;
1708 case LibFunc::stat64:
1709 case LibFunc::lstat64:
1710 case LibFunc::statvfs64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001711 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001712 !FTy->getParamType(1)->isPointerTy())
1713 return false;
1714 setDoesNotThrow(F);
1715 setDoesNotCapture(F, 1);
1716 setDoesNotCapture(F, 2);
1717 setOnlyReadsMemory(F, 1);
1718 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001719 case LibFunc::dunder_isoc99_sscanf:
Chandler Carruth63559d72015-09-13 06:47:20 +00001720 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001721 !FTy->getParamType(1)->isPointerTy())
1722 return false;
1723 setDoesNotThrow(F);
1724 setDoesNotCapture(F, 1);
1725 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001726 setOnlyReadsMemory(F, 1);
1727 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001728 break;
1729 case LibFunc::fopen64:
Chandler Carruth63559d72015-09-13 06:47:20 +00001730 if (FTy->getNumParams() != 2 || !FTy->getReturnType()->isPointerTy() ||
Meador Inge6b6a1612013-03-21 00:55:59 +00001731 !FTy->getParamType(0)->isPointerTy() ||
1732 !FTy->getParamType(1)->isPointerTy())
1733 return false;
1734 setDoesNotThrow(F);
1735 setDoesNotAlias(F, 0);
1736 setDoesNotCapture(F, 1);
1737 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001738 setOnlyReadsMemory(F, 1);
1739 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001740 break;
1741 case LibFunc::fseeko64:
1742 case LibFunc::ftello64:
1743 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1744 return false;
1745 setDoesNotThrow(F);
1746 setDoesNotCapture(F, 1);
1747 break;
1748 case LibFunc::tmpfile64:
1749 if (!FTy->getReturnType()->isPointerTy())
1750 return false;
1751 setDoesNotThrow(F);
1752 setDoesNotAlias(F, 0);
1753 break;
1754 case LibFunc::fstat64:
1755 case LibFunc::fstatvfs64:
1756 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1757 return false;
1758 setDoesNotThrow(F);
1759 setDoesNotCapture(F, 2);
1760 break;
1761 case LibFunc::open64:
1762 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1763 return false;
1764 // May throw; "open" is a valid pthread cancellation point.
1765 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001766 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001767 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001768 case LibFunc::gettimeofday:
1769 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1770 !FTy->getParamType(1)->isPointerTy())
1771 return false;
1772 // Currently some platforms have the restrict keyword on the arguments to
1773 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1774 // arguments.
1775 setDoesNotThrow(F);
1776 setDoesNotCapture(F, 1);
1777 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001778 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001779 default:
1780 // Didn't mark any attributes.
1781 return false;
1782 }
1783
1784 return true;
1785}
1786
Chandler Carrutha632fb92015-09-13 06:57:25 +00001787/// Adds attributes to well-known standard library call declarations.
Meador Inge6b6a1612013-03-21 00:55:59 +00001788bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1789 bool MadeChange = false;
1790
1791 // Check each function in turn annotating well-known library function
1792 // declarations with attributes.
1793 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1794 Function *F = (*I)->getFunction();
1795
Craig Topperf40110f2014-04-25 05:29:35 +00001796 if (F && F->isDeclaration())
Chandler Carruth444d0052015-09-13 08:03:23 +00001797 MadeChange |= inferPrototypeAttributes(*F, *TLI);
Meador Inge6b6a1612013-03-21 00:55:59 +00001798 }
1799
1800 return MadeChange;
1801}
1802
Chris Lattner4422d312010-04-16 22:42:17 +00001803bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001804 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohman86449d72010-11-08 16:10:15 +00001805
Meador Inge6b6a1612013-03-21 00:55:59 +00001806 bool Changed = annotateLibraryCalls(SCC);
1807 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001808 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001809 Changed |= AddNoAliasAttrs(SCC);
Philip Reamesa88caea2015-08-31 19:44:38 +00001810 Changed |= AddNonNullAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001811 return Changed;
1812}