blob: 749ff9920a822745feb362276b474ad0565a505d [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"
27#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000028#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000029#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000030#include "llvm/IR/GlobalVariable.h"
Chandler Carruth83948572014-03-04 10:30:26 +000031#include "llvm/IR/InstIterator.h"
Chandler Carruth9fb823b2013-01-02 11:36:10 +000032#include "llvm/IR/IntrinsicInst.h"
33#include "llvm/IR/LLVMContext.h"
Chandler Carruth62d42152015-01-15 02:16:27 +000034#include "llvm/Analysis/TargetLibraryInfo.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000035using namespace llvm;
36
Chandler Carruth964daaa2014-04-22 02:55:47 +000037#define DEBUG_TYPE "functionattrs"
38
Duncan Sands44c8cd92008-12-31 16:14:43 +000039STATISTIC(NumReadNone, "Number of functions marked readnone");
40STATISTIC(NumReadOnly, "Number of functions marked readonly");
41STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000042STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
43STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000044STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Meador Inge6b6a1612013-03-21 00:55:59 +000045STATISTIC(NumAnnotated, "Number of attributes added to library functions");
Duncan Sands44c8cd92008-12-31 16:14:43 +000046
47namespace {
Nick Lewycky02d5f772009-10-25 06:33:48 +000048 struct FunctionAttrs : public CallGraphSCCPass {
Duncan Sands44c8cd92008-12-31 16:14:43 +000049 static char ID; // Pass identification, replacement for typeid
Craig Topperf40110f2014-04-25 05:29:35 +000050 FunctionAttrs() : CallGraphSCCPass(ID), AA(nullptr) {
Owen Anderson6c18d1a2010-10-19 17:21:58 +000051 initializeFunctionAttrsPass(*PassRegistry::getPassRegistry());
52 }
Duncan Sands44c8cd92008-12-31 16:14:43 +000053
54 // runOnSCC - Analyze the SCC, performing the transformation if possible.
Craig Topper3e4c6972014-03-05 09:10:37 +000055 bool runOnSCC(CallGraphSCC &SCC) override;
Duncan Sands44c8cd92008-12-31 16:14:43 +000056
57 // AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000058 bool AddReadAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000059
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000060 // AddArgumentAttrs - Deduce nocapture attributes for the SCC.
61 bool AddArgumentAttrs(const CallGraphSCC &SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +000062
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000063 // IsFunctionMallocLike - Does this function allocate new memory?
64 bool IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +000065 SmallPtrSet<Function*, 8> &) const;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000066
67 // AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +000068 bool AddNoAliasAttrs(const CallGraphSCC &SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000069
Meador Inge6b6a1612013-03-21 00:55:59 +000070 // Utility methods used by inferPrototypeAttributes to add attributes
71 // and maintain annotation statistics.
72
73 void setDoesNotAccessMemory(Function &F) {
74 if (!F.doesNotAccessMemory()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000075 F.setDoesNotAccessMemory();
76 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000077 }
78 }
79
80 void setOnlyReadsMemory(Function &F) {
81 if (!F.onlyReadsMemory()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000082 F.setOnlyReadsMemory();
83 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000084 }
85 }
86
87 void setDoesNotThrow(Function &F) {
88 if (!F.doesNotThrow()) {
Nick Lewyckya833c662013-07-04 03:51:53 +000089 F.setDoesNotThrow();
90 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000091 }
92 }
93
94 void setDoesNotCapture(Function &F, unsigned n) {
95 if (!F.doesNotCapture(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +000096 F.setDoesNotCapture(n);
97 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +000098 }
99 }
100
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000101 void setOnlyReadsMemory(Function &F, unsigned n) {
102 if (!F.onlyReadsMemory(n)) {
103 F.setOnlyReadsMemory(n);
104 ++NumAnnotated;
105 }
106 }
107
Meador Inge6b6a1612013-03-21 00:55:59 +0000108 void setDoesNotAlias(Function &F, unsigned n) {
109 if (!F.doesNotAlias(n)) {
Nick Lewyckya833c662013-07-04 03:51:53 +0000110 F.setDoesNotAlias(n);
111 ++NumAnnotated;
Meador Inge6b6a1612013-03-21 00:55:59 +0000112 }
113 }
114
115 // inferPrototypeAttributes - Analyze the name and prototype of the
116 // given function and set any applicable attributes. Returns true
117 // if any attributes were set and false otherwise.
118 bool inferPrototypeAttributes(Function &F);
119
120 // annotateLibraryCalls - Adds attributes to well-known standard library
121 // call declarations.
122 bool annotateLibraryCalls(const CallGraphSCC &SCC);
123
Craig Topper3e4c6972014-03-05 09:10:37 +0000124 void getAnalysisUsage(AnalysisUsage &AU) const override {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000125 AU.setPreservesCFG();
Dan Gohman86449d72010-11-08 16:10:15 +0000126 AU.addRequired<AliasAnalysis>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000127 AU.addRequired<TargetLibraryInfoWrapperPass>();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000128 CallGraphSCCPass::getAnalysisUsage(AU);
129 }
130
Dan Gohman86449d72010-11-08 16:10:15 +0000131 private:
132 AliasAnalysis *AA;
Meador Inge6b6a1612013-03-21 00:55:59 +0000133 TargetLibraryInfo *TLI;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000134 };
Alexander Kornienko70bc5f12015-06-19 15:57:42 +0000135} // namespace
Duncan Sands44c8cd92008-12-31 16:14:43 +0000136
137char FunctionAttrs::ID = 0;
Owen Anderson071cee02010-10-13 22:00:45 +0000138INITIALIZE_PASS_BEGIN(FunctionAttrs, "functionattrs",
139 "Deduce function attributes", false, false)
Nick Lewycky2c880672013-09-05 08:19:58 +0000140INITIALIZE_AG_DEPENDENCY(AliasAnalysis)
Chandler Carruth6378cf52013-11-26 04:19:30 +0000141INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruthb98f63d2015-01-15 10:41:28 +0000142INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Owen Anderson071cee02010-10-13 22:00:45 +0000143INITIALIZE_PASS_END(FunctionAttrs, "functionattrs",
Owen Andersondf7a4f22010-10-07 22:25:06 +0000144 "Deduce function attributes", false, false)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000145
146Pass *llvm::createFunctionAttrsPass() { return new FunctionAttrs(); }
147
148
Duncan Sands44c8cd92008-12-31 16:14:43 +0000149/// AddReadAttrs - Deduce readonly/readnone attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000150bool FunctionAttrs::AddReadAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000151 SmallPtrSet<Function*, 8> SCCNodes;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000152
153 // Fill SCCNodes with the elements of the SCC. Used for quickly
154 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000155 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
156 SCCNodes.insert((*I)->getFunction());
Duncan Sands44c8cd92008-12-31 16:14:43 +0000157
158 // Check if any of the functions in the SCC read or write memory. If they
159 // write memory then they can't be marked readnone or readonly.
160 bool ReadsMemory = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000161 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
162 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000163
Chandler Carruth0fb99812014-08-13 10:49:33 +0000164 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
165 // External node or node we don't want to optimize - assume it may write
166 // memory and give up.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000167 return false;
168
Dan Gohman35814e62010-11-09 20:13:27 +0000169 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(F);
170 if (MRB == AliasAnalysis::DoesNotAccessMemory)
Duncan Sands44c8cd92008-12-31 16:14:43 +0000171 // Already perfect!
172 continue;
173
174 // Definitions with weak linkage may be overridden at linktime with
175 // something that writes memory, so treat them like declarations.
176 if (F->isDeclaration() || F->mayBeOverridden()) {
Dan Gohman35814e62010-11-09 20:13:27 +0000177 if (!AliasAnalysis::onlyReadsMemory(MRB))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000178 // May write memory. Just give up.
179 return false;
180
181 ReadsMemory = true;
182 continue;
183 }
184
185 // Scan the function body for instructions that may read or write memory.
186 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
187 Instruction *I = &*II;
188
189 // Some instructions can be ignored even if they read or write memory.
190 // Detect these now, skipping to the next instruction if one is found.
Gabor Greif62f0aac2010-07-28 22:50:26 +0000191 CallSite CS(cast<Value>(I));
Dan Gohman86449d72010-11-08 16:10:15 +0000192 if (CS) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000193 // Ignore calls to functions in the same SCC.
Dan Gohman86449d72010-11-08 16:10:15 +0000194 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
Duncan Sands44c8cd92008-12-31 16:14:43 +0000195 continue;
Dan Gohman2694e142010-11-10 01:02:18 +0000196 AliasAnalysis::ModRefBehavior MRB = AA->getModRefBehavior(CS);
197 // If the call doesn't access arbitrary memory, we may be able to
198 // figure out something.
Dan Gohman25775802010-11-10 17:34:04 +0000199 if (AliasAnalysis::onlyAccessesArgPointees(MRB)) {
200 // If the call does access argument pointees, check each argument.
Dan Gohman066c1bb2010-11-10 18:17:28 +0000201 if (AliasAnalysis::doesAccessArgPointees(MRB))
Dan Gohman2694e142010-11-10 01:02:18 +0000202 // Check whether all pointer arguments point to local memory, and
203 // ignore calls that only access local memory.
204 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
205 CI != CE; ++CI) {
206 Value *Arg = *CI;
207 if (Arg->getType()->isPointerTy()) {
Hal Finkelcc39b672014-07-24 12:16:19 +0000208 AAMDNodes AAInfo;
209 I->getAAMetadata(AAInfo);
210
Chandler Carruthecbd1682015-06-17 07:21:38 +0000211 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
Dan Gohman2694e142010-11-10 01:02:18 +0000212 if (!AA->pointsToConstantMemory(Loc, /*OrLocal=*/true)) {
213 if (MRB & AliasAnalysis::Mod)
214 // Writes non-local memory. Give up.
215 return false;
216 if (MRB & AliasAnalysis::Ref)
217 // Ok, it reads non-local memory.
218 ReadsMemory = true;
219 }
Dan Gohmande521552010-11-09 19:56:27 +0000220 }
221 }
Dan Gohmande521552010-11-09 19:56:27 +0000222 continue;
Dan Gohman86449d72010-11-08 16:10:15 +0000223 }
Dan Gohman2694e142010-11-10 01:02:18 +0000224 // The call could access any memory. If that includes writes, give up.
225 if (MRB & AliasAnalysis::Mod)
226 return false;
227 // If it reads, note it.
228 if (MRB & AliasAnalysis::Ref)
229 ReadsMemory = true;
230 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000231 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000232 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
233 if (!LI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000234 MemoryLocation Loc = MemoryLocation::get(LI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000235 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
236 continue;
237 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000238 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
Eli Friedmana917d4f2011-08-16 01:28:22 +0000239 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
240 if (!SI->isVolatile()) {
Chandler Carruthac80dc72015-06-17 07:18:54 +0000241 MemoryLocation Loc = MemoryLocation::get(SI);
Dan Gohman2cd1fd42010-11-08 17:12:04 +0000242 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
243 continue;
244 }
Dan Gohmane3467a72010-11-09 20:17:38 +0000245 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
246 // Ignore vaargs on local memory.
Chandler Carruthac80dc72015-06-17 07:18:54 +0000247 MemoryLocation Loc = MemoryLocation::get(VI);
Dan Gohmane3467a72010-11-09 20:17:38 +0000248 if (AA->pointsToConstantMemory(Loc, /*OrLocal=*/true))
249 continue;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000250 }
251
252 // Any remaining instructions need to be taken seriously! Check if they
253 // read or write memory.
254 if (I->mayWriteToMemory())
255 // Writes memory. Just give up.
256 return false;
Duncan Sands9759f2e2009-05-06 08:42:00 +0000257
Duncan Sands44c8cd92008-12-31 16:14:43 +0000258 // If this instruction may read memory, remember that.
259 ReadsMemory |= I->mayReadFromMemory();
260 }
261 }
262
263 // Success! Functions in this SCC do not access memory, or only read memory.
264 // Give them the appropriate attribute.
265 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000266 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
267 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000268
269 if (F->doesNotAccessMemory())
270 // Already perfect!
271 continue;
272
273 if (F->onlyReadsMemory() && ReadsMemory)
274 // No change.
275 continue;
276
277 MadeChange = true;
278
279 // Clear out any existing attributes.
Bill Wendling50d27842012-10-15 20:35:56 +0000280 AttrBuilder B;
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000281 B.addAttribute(Attribute::ReadOnly)
282 .addAttribute(Attribute::ReadNone);
Bill Wendling430fa9b2013-01-23 00:45:55 +0000283 F->removeAttributes(AttributeSet::FunctionIndex,
284 AttributeSet::get(F->getContext(),
285 AttributeSet::FunctionIndex, B));
Duncan Sands44c8cd92008-12-31 16:14:43 +0000286
287 // Add in the new attribute.
Bill Wendlinge94d8432012-12-07 23:16:57 +0000288 F->addAttribute(AttributeSet::FunctionIndex,
Bill Wendlingc0e2a1f2013-01-23 00:20:53 +0000289 ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000290
291 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000292 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000293 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000294 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000295 }
296
297 return MadeChange;
298}
299
Nick Lewycky4c378a42011-12-28 23:24:21 +0000300namespace {
301 // For a given pointer Argument, this retains a list of Arguments of functions
302 // in the same SCC that the pointer data flows into. We use this to build an
303 // SCC of the arguments.
304 struct ArgumentGraphNode {
305 Argument *Definition;
306 SmallVector<ArgumentGraphNode*, 4> Uses;
307 };
308
309 class ArgumentGraph {
310 // We store pointers to ArgumentGraphNode objects, so it's important that
311 // that they not move around upon insert.
312 typedef std::map<Argument*, ArgumentGraphNode> ArgumentMapTy;
313
314 ArgumentMapTy ArgumentMap;
315
316 // There is no root node for the argument graph, in fact:
317 // void f(int *x, int *y) { if (...) f(x, y); }
318 // is an example where the graph is disconnected. The SCCIterator requires a
319 // single entry point, so we maintain a fake ("synthetic") root node that
320 // uses every node. Because the graph is directed and nothing points into
321 // the root, it will not participate in any SCCs (except for its own).
322 ArgumentGraphNode SyntheticRoot;
323
324 public:
Craig Topperf40110f2014-04-25 05:29:35 +0000325 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000326
327 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator iterator;
328
329 iterator begin() { return SyntheticRoot.Uses.begin(); }
330 iterator end() { return SyntheticRoot.Uses.end(); }
331 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
332
333 ArgumentGraphNode *operator[](Argument *A) {
334 ArgumentGraphNode &Node = ArgumentMap[A];
335 Node.Definition = A;
336 SyntheticRoot.Uses.push_back(&Node);
337 return &Node;
338 }
339 };
340
341 // This tracker checks whether callees are in the SCC, and if so it does not
342 // consider that a capture, instead adding it to the "Uses" list and
343 // continuing with the analysis.
344 struct ArgumentUsesTracker : public CaptureTracker {
345 ArgumentUsesTracker(const SmallPtrSet<Function*, 8> &SCCNodes)
346 : Captured(false), SCCNodes(SCCNodes) {}
347
Craig Topper3e4c6972014-03-05 09:10:37 +0000348 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000349
Chandler Carruth64e9aa52014-03-05 10:21:48 +0000350 bool captured(const Use *U) override {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000351 CallSite CS(U->getUser());
352 if (!CS.getInstruction()) { Captured = true; return true; }
353
354 Function *F = CS.getCalledFunction();
355 if (!F || !SCCNodes.count(F)) { Captured = true; return true; }
356
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000357 bool Found = false;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000358 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
359 for (CallSite::arg_iterator PI = CS.arg_begin(), PE = CS.arg_end();
360 PI != PE; ++PI, ++AI) {
361 if (AI == AE) {
362 assert(F->isVarArg() && "More params than args in non-varargs call");
363 Captured = true;
364 return true;
365 }
366 if (PI == U) {
367 Uses.push_back(AI);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000368 Found = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000369 break;
370 }
371 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000372 assert(Found && "Capturing call-site captured nothing?");
Duncan Sandsc9e95ad2013-09-13 08:16:06 +0000373 (void)Found;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000374 return false;
375 }
376
377 bool Captured; // True only if certainly captured (used outside our SCC).
378 SmallVector<Argument*, 4> Uses; // Uses within our SCC.
379
380 const SmallPtrSet<Function*, 8> &SCCNodes;
381 };
Alexander Kornienko70bc5f12015-06-19 15:57:42 +0000382} // namespace
Nick Lewycky4c378a42011-12-28 23:24:21 +0000383
384namespace llvm {
385 template<> struct GraphTraits<ArgumentGraphNode*> {
386 typedef ArgumentGraphNode NodeType;
387 typedef SmallVectorImpl<ArgumentGraphNode*>::iterator ChildIteratorType;
388
389 static inline NodeType *getEntryNode(NodeType *A) { return A; }
390 static inline ChildIteratorType child_begin(NodeType *N) {
391 return N->Uses.begin();
392 }
393 static inline ChildIteratorType child_end(NodeType *N) {
394 return N->Uses.end();
395 }
396 };
397 template<> struct GraphTraits<ArgumentGraph*>
398 : public GraphTraits<ArgumentGraphNode*> {
399 static NodeType *getEntryNode(ArgumentGraph *AG) {
400 return AG->getEntryNode();
401 }
402 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
403 return AG->begin();
404 }
405 static ChildIteratorType nodes_end(ArgumentGraph *AG) {
406 return AG->end();
407 }
408 };
Alexander Kornienko70bc5f12015-06-19 15:57:42 +0000409} // namespace llvm
Nick Lewycky4c378a42011-12-28 23:24:21 +0000410
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000411// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
412static Attribute::AttrKind
413determinePointerReadAttrs(Argument *A,
414 const SmallPtrSet<Argument*, 8> &SCCNodes) {
415
416 SmallVector<Use*, 32> Worklist;
417 SmallSet<Use*, 32> Visited;
418 int Count = 0;
419
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000420 // inalloca arguments are always clobbered by the call.
421 if (A->hasInAllocaAttr())
422 return Attribute::None;
423
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000424 bool IsRead = false;
425 // We don't need to track IsWritten. If A is written to, return immediately.
426
Chandler Carruthcdf47882014-03-09 03:16:01 +0000427 for (Use &U : A->uses()) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000428 if (Count++ >= 20)
429 return Attribute::None;
430
Chandler Carruthcdf47882014-03-09 03:16:01 +0000431 Visited.insert(&U);
432 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000433 }
434
435 while (!Worklist.empty()) {
436 Use *U = Worklist.pop_back_val();
437 Instruction *I = cast<Instruction>(U->getUser());
438 Value *V = U->get();
439
440 switch (I->getOpcode()) {
441 case Instruction::BitCast:
442 case Instruction::GetElementPtr:
443 case Instruction::PHI:
444 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000445 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000446 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000447 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000448 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000449 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000450 break;
451
452 case Instruction::Call:
453 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000454 bool Captures = true;
455
456 if (I->getType()->isVoidTy())
457 Captures = false;
458
459 auto AddUsersToWorklistIfCapturing = [&] {
460 if (Captures)
461 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000462 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000463 Worklist.push_back(&UU);
464 };
465
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000466 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000467 if (CS.doesNotAccessMemory()) {
468 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000469 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000470 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000471
472 Function *F = CS.getCalledFunction();
473 if (!F) {
474 if (CS.onlyReadsMemory()) {
475 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000476 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000477 continue;
478 }
479 return Attribute::None;
480 }
481
482 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
483 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
484 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
485 if (A->get() == V) {
486 if (AI == AE) {
487 assert(F->isVarArg() &&
488 "More params than args in non-varargs call.");
489 return Attribute::None;
490 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000491 Captures &= !CS.doesNotCapture(A - B);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000492 if (SCCNodes.count(AI))
493 continue;
494 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
495 return Attribute::None;
496 if (!CS.doesNotAccessMemory(A - B))
497 IsRead = true;
498 }
499 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000500 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000501 break;
502 }
503
504 case Instruction::Load:
505 IsRead = true;
506 break;
507
508 case Instruction::ICmp:
509 case Instruction::Ret:
510 break;
511
512 default:
513 return Attribute::None;
514 }
515 }
516
517 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
518}
519
520/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
521bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000522 bool Changed = false;
523
Nick Lewycky4c378a42011-12-28 23:24:21 +0000524 SmallPtrSet<Function*, 8> SCCNodes;
525
526 // Fill SCCNodes with the elements of the SCC. Used for quickly
527 // looking up whether a given CallGraphNode is in this SCC.
528 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
529 Function *F = (*I)->getFunction();
Chandler Carruth0fb99812014-08-13 10:49:33 +0000530 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
531 !F->hasFnAttribute(Attribute::OptimizeNone))
Nick Lewycky4c378a42011-12-28 23:24:21 +0000532 SCCNodes.insert(F);
533 }
534
535 ArgumentGraph AG;
536
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000537 AttrBuilder B;
538 B.addAttribute(Attribute::NoCapture);
539
Duncan Sands44c8cd92008-12-31 16:14:43 +0000540 // Check each function in turn, determining which pointer arguments are not
541 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000542 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
543 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000544
Chandler Carruth0fb99812014-08-13 10:49:33 +0000545 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
546 // External node or function we're trying not to optimize - only a problem
547 // for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000548 continue;
549
550 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000551 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000552 if (F->isDeclaration() || F->mayBeOverridden())
553 continue;
554
Nick Lewycky4c378a42011-12-28 23:24:21 +0000555 // Functions that are readonly (or readnone) and nounwind and don't return
556 // a value can't capture arguments. Don't analyze them.
557 if (F->onlyReadsMemory() && F->doesNotThrow() &&
558 F->getReturnType()->isVoidTy()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000559 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
560 A != E; ++A) {
561 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
562 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
563 ++NumNoCapture;
564 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000565 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000566 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000567 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000568 }
569
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000570 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
571 A != E; ++A) {
572 if (!A->getType()->isPointerTy()) continue;
573 bool HasNonLocalUses = false;
574 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000575 ArgumentUsesTracker Tracker(SCCNodes);
576 PointerMayBeCaptured(A, &Tracker);
577 if (!Tracker.Captured) {
578 if (Tracker.Uses.empty()) {
579 // If it's trivially not captured, mark it nocapture now.
580 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
581 ++NumNoCapture;
582 Changed = true;
583 } else {
584 // If it's not trivially captured and not trivially not captured,
585 // then it must be calling into another function in our SCC. Save
586 // its particulars for Argument-SCC analysis later.
587 ArgumentGraphNode *Node = AG[A];
588 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000589 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000590 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000591 if (*UI != A)
592 HasNonLocalUses = true;
593 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000594 }
595 }
596 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
597 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000598 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
599 // Can we determine that it's readonly/readnone without doing an SCC?
600 // Note that we don't allow any calls at all here, or else our result
601 // will be dependent on the iteration order through the functions in the
602 // SCC.
603 SmallPtrSet<Argument*, 8> Self;
604 Self.insert(A);
605 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
606 if (R != Attribute::None) {
607 AttrBuilder B;
608 B.addAttribute(R);
609 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
610 Changed = true;
611 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
612 }
613 }
614 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000615 }
616
617 // The graph we've collected is partial because we stopped scanning for
618 // argument uses once we solved the argument trivially. These partial nodes
619 // show up as ArgumentGraphNode objects with an empty Uses list, and for
620 // these nodes the final decision about whether they capture has already been
621 // made. If the definition doesn't have a 'nocapture' attribute by now, it
622 // captures.
623
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000624 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000625 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000626 if (ArgumentSCC.size() == 1) {
627 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
628
629 // eg. "void f(int* x) { if (...) f(x); }"
630 if (ArgumentSCC[0]->Uses.size() == 1 &&
631 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000632 Argument *A = ArgumentSCC[0]->Definition;
633 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000634 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000635 Changed = true;
636 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000637 continue;
638 }
639
640 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000641 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
642 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000643 ArgumentGraphNode *Node = *I;
644 if (Node->Uses.empty()) {
645 if (!Node->Definition->hasNoCaptureAttr())
646 SCCCaptured = true;
647 }
648 }
649 if (SCCCaptured) continue;
650
651 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
652 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
653 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000654 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000655 ArgumentSCCNodes.insert((*I)->Definition);
656 }
657
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000658 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
659 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000660 ArgumentGraphNode *N = *I;
661 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
662 UE = N->Uses.end(); UI != UE; ++UI) {
663 Argument *A = (*UI)->Definition;
664 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
665 continue;
666 SCCCaptured = true;
667 break;
668 }
669 }
670 if (SCCCaptured) continue;
671
Nick Lewyckyf740db32012-01-05 22:21:45 +0000672 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000673 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000674 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000675 ++NumNoCapture;
676 Changed = true;
677 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000678
679 // We also want to compute readonly/readnone. With a small number of false
680 // negatives, we can assume that any pointer which is captured isn't going
681 // to be provably readonly or readnone, since by definition we can't
682 // analyze all uses of a captured pointer.
683 //
684 // The false negatives happen when the pointer is captured by a function
685 // that promises readonly/readnone behaviour on the pointer, then the
686 // pointer's lifetime ends before anything that writes to arbitrary memory.
687 // Also, a readonly/readnone pointer may be returned, but returning a
688 // pointer is capturing it.
689
690 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
691 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
692 Argument *A = ArgumentSCC[i]->Definition;
693 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
694 if (K == Attribute::ReadNone)
695 continue;
696 if (K == Attribute::ReadOnly) {
697 ReadAttr = Attribute::ReadOnly;
698 continue;
699 }
700 ReadAttr = K;
701 break;
702 }
703
704 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000705 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000706 B.addAttribute(ReadAttr);
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000707 R.addAttribute(Attribute::ReadOnly)
708 .addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000709 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
710 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000711 // Clear out existing readonly/readnone attributes
712 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000713 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
714 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
715 Changed = true;
716 }
717 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000718 }
719
720 return Changed;
721}
722
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000723/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000724/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000725bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +0000726 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramer15591272012-10-31 13:45:49 +0000727 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000728 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
729 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
730 FlowsToReturn.insert(Ret->getReturnValue());
731
732 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000733 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000734
735 if (Constant *C = dyn_cast<Constant>(RetVal)) {
736 if (!C->isNullValue() && !isa<UndefValue>(C))
737 return false;
738
739 continue;
740 }
741
742 if (isa<Argument>(RetVal))
743 return false;
744
745 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
746 switch (RVI->getOpcode()) {
747 // Extend the analysis by looking upwards.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000748 case Instruction::BitCast:
Victor Hernandez5d034492009-09-18 22:35:49 +0000749 case Instruction::GetElementPtr:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000750 case Instruction::AddrSpaceCast:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000751 FlowsToReturn.insert(RVI->getOperand(0));
752 continue;
753 case Instruction::Select: {
754 SelectInst *SI = cast<SelectInst>(RVI);
755 FlowsToReturn.insert(SI->getTrueValue());
756 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner43d0db72009-09-27 21:29:28 +0000757 continue;
758 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000759 case Instruction::PHI: {
760 PHINode *PN = cast<PHINode>(RVI);
Pete Cooper833f34d2015-05-12 20:05:31 +0000761 for (Value *IncValue : PN->incoming_values())
762 FlowsToReturn.insert(IncValue);
Chris Lattner43d0db72009-09-27 21:29:28 +0000763 continue;
764 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000765
766 // Check whether the pointer came from an allocation.
767 case Instruction::Alloca:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000768 break;
769 case Instruction::Call:
770 case Instruction::Invoke: {
771 CallSite CS(RVI);
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000772 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000773 break;
774 if (CS.getCalledFunction() &&
Chris Lattner2f2110a2009-08-31 04:09:04 +0000775 SCCNodes.count(CS.getCalledFunction()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000776 break;
777 } // fall-through
778 default:
779 return false; // Did not come from an allocation.
780 }
781
Dan Gohman94e61762009-11-19 21:57:48 +0000782 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000783 return false;
784 }
785
786 return true;
787}
788
789/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000790bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000791 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000792
793 // Fill SCCNodes with the elements of the SCC. Used for quickly
794 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000795 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
796 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000797
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000798 // Check each function in turn, determining which functions return noalias
799 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000800 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
801 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000802
Chandler Carruth0fb99812014-08-13 10:49:33 +0000803 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
804 // External node or node we don't want to optimize - skip it;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000805 return false;
806
807 // Already noalias.
808 if (F->doesNotAlias(0))
809 continue;
810
811 // Definitions with weak linkage may be overridden at linktime, so
812 // treat them like declarations.
813 if (F->isDeclaration() || F->mayBeOverridden())
814 return false;
815
816 // We annotate noalias return values, which are only applicable to
817 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000818 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000819 continue;
820
821 if (!IsFunctionMallocLike(F, SCCNodes))
822 return false;
823 }
824
825 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000826 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
827 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000828 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000829 continue;
830
831 F->setDoesNotAlias(0);
832 ++NumNoAlias;
833 MadeChange = true;
834 }
835
836 return MadeChange;
837}
838
Meador Inge6b6a1612013-03-21 00:55:59 +0000839/// inferPrototypeAttributes - Analyze the name and prototype of the
840/// given function and set any applicable attributes. Returns true
841/// if any attributes were set and false otherwise.
842bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
Chandler Carruth0fb99812014-08-13 10:49:33 +0000843 if (F.hasFnAttribute(Attribute::OptimizeNone))
844 return false;
845
Meador Inge6b6a1612013-03-21 00:55:59 +0000846 FunctionType *FTy = F.getFunctionType();
847 LibFunc::Func TheLibFunc;
848 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
849 return false;
850
851 switch (TheLibFunc) {
852 case LibFunc::strlen:
853 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
854 return false;
855 setOnlyReadsMemory(F);
856 setDoesNotThrow(F);
857 setDoesNotCapture(F, 1);
858 break;
859 case LibFunc::strchr:
860 case LibFunc::strrchr:
861 if (FTy->getNumParams() != 2 ||
862 !FTy->getParamType(0)->isPointerTy() ||
863 !FTy->getParamType(1)->isIntegerTy())
864 return false;
865 setOnlyReadsMemory(F);
866 setDoesNotThrow(F);
867 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000868 case LibFunc::strtol:
869 case LibFunc::strtod:
870 case LibFunc::strtof:
871 case LibFunc::strtoul:
872 case LibFunc::strtoll:
873 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +0000874 case LibFunc::strtoull:
875 if (FTy->getNumParams() < 2 ||
876 !FTy->getParamType(1)->isPointerTy())
877 return false;
878 setDoesNotThrow(F);
879 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000880 setOnlyReadsMemory(F, 1);
881 break;
882 case LibFunc::strcpy:
883 case LibFunc::stpcpy:
884 case LibFunc::strcat:
885 case LibFunc::strncat:
886 case LibFunc::strncpy:
887 case LibFunc::stpncpy:
888 if (FTy->getNumParams() < 2 ||
889 !FTy->getParamType(1)->isPointerTy())
890 return false;
891 setDoesNotThrow(F);
892 setDoesNotCapture(F, 2);
893 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000894 break;
895 case LibFunc::strxfrm:
896 if (FTy->getNumParams() != 3 ||
897 !FTy->getParamType(0)->isPointerTy() ||
898 !FTy->getParamType(1)->isPointerTy())
899 return false;
900 setDoesNotThrow(F);
901 setDoesNotCapture(F, 1);
902 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000903 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000904 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000905 case LibFunc::strcmp: //0,1
906 case LibFunc::strspn: // 0,1
907 case LibFunc::strncmp: // 0,1
908 case LibFunc::strcspn: //0,1
909 case LibFunc::strcoll: //0,1
910 case LibFunc::strcasecmp: // 0,1
911 case LibFunc::strncasecmp: //
Meador Inge6b6a1612013-03-21 00:55:59 +0000912 if (FTy->getNumParams() < 2 ||
913 !FTy->getParamType(0)->isPointerTy() ||
914 !FTy->getParamType(1)->isPointerTy())
915 return false;
916 setOnlyReadsMemory(F);
917 setDoesNotThrow(F);
918 setDoesNotCapture(F, 1);
919 setDoesNotCapture(F, 2);
920 break;
921 case LibFunc::strstr:
922 case LibFunc::strpbrk:
923 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
924 return false;
925 setOnlyReadsMemory(F);
926 setDoesNotThrow(F);
927 setDoesNotCapture(F, 2);
928 break;
929 case LibFunc::strtok:
930 case LibFunc::strtok_r:
931 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
932 return false;
933 setDoesNotThrow(F);
934 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000935 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000936 break;
937 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000938 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
939 return false;
940 setDoesNotThrow(F);
941 setDoesNotCapture(F, 1);
942 setOnlyReadsMemory(F, 1);
943 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000944 case LibFunc::setbuf:
945 case LibFunc::setvbuf:
946 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
947 return false;
948 setDoesNotThrow(F);
949 setDoesNotCapture(F, 1);
950 break;
951 case LibFunc::strdup:
952 case LibFunc::strndup:
953 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
954 !FTy->getParamType(0)->isPointerTy())
955 return false;
956 setDoesNotThrow(F);
957 setDoesNotAlias(F, 0);
958 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000959 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +0000960 break;
961 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +0000962 case LibFunc::statvfs:
963 if (FTy->getNumParams() < 2 ||
964 !FTy->getParamType(0)->isPointerTy() ||
965 !FTy->getParamType(1)->isPointerTy())
966 return false;
967 setDoesNotThrow(F);
968 setDoesNotCapture(F, 1);
969 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000970 setOnlyReadsMemory(F, 1);
971 break;
972 case LibFunc::sscanf:
973 if (FTy->getNumParams() < 2 ||
974 !FTy->getParamType(0)->isPointerTy() ||
975 !FTy->getParamType(1)->isPointerTy())
976 return false;
977 setDoesNotThrow(F);
978 setDoesNotCapture(F, 1);
979 setDoesNotCapture(F, 2);
980 setOnlyReadsMemory(F, 1);
981 setOnlyReadsMemory(F, 2);
982 break;
983 case LibFunc::sprintf:
984 if (FTy->getNumParams() < 2 ||
985 !FTy->getParamType(0)->isPointerTy() ||
986 !FTy->getParamType(1)->isPointerTy())
987 return false;
988 setDoesNotThrow(F);
989 setDoesNotCapture(F, 1);
990 setDoesNotCapture(F, 2);
991 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000992 break;
993 case LibFunc::snprintf:
994 if (FTy->getNumParams() != 3 ||
995 !FTy->getParamType(0)->isPointerTy() ||
996 !FTy->getParamType(2)->isPointerTy())
997 return false;
998 setDoesNotThrow(F);
999 setDoesNotCapture(F, 1);
1000 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001001 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001002 break;
1003 case LibFunc::setitimer:
1004 if (FTy->getNumParams() != 3 ||
1005 !FTy->getParamType(1)->isPointerTy() ||
1006 !FTy->getParamType(2)->isPointerTy())
1007 return false;
1008 setDoesNotThrow(F);
1009 setDoesNotCapture(F, 2);
1010 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001011 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001012 break;
1013 case LibFunc::system:
1014 if (FTy->getNumParams() != 1 ||
1015 !FTy->getParamType(0)->isPointerTy())
1016 return false;
1017 // May throw; "system" is a valid pthread cancellation point.
1018 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001019 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001020 break;
1021 case LibFunc::malloc:
1022 if (FTy->getNumParams() != 1 ||
1023 !FTy->getReturnType()->isPointerTy())
1024 return false;
1025 setDoesNotThrow(F);
1026 setDoesNotAlias(F, 0);
1027 break;
1028 case LibFunc::memcmp:
1029 if (FTy->getNumParams() != 3 ||
1030 !FTy->getParamType(0)->isPointerTy() ||
1031 !FTy->getParamType(1)->isPointerTy())
1032 return false;
1033 setOnlyReadsMemory(F);
1034 setDoesNotThrow(F);
1035 setDoesNotCapture(F, 1);
1036 setDoesNotCapture(F, 2);
1037 break;
1038 case LibFunc::memchr:
1039 case LibFunc::memrchr:
1040 if (FTy->getNumParams() != 3)
1041 return false;
1042 setOnlyReadsMemory(F);
1043 setDoesNotThrow(F);
1044 break;
1045 case LibFunc::modf:
1046 case LibFunc::modff:
1047 case LibFunc::modfl:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001048 if (FTy->getNumParams() < 2 ||
1049 !FTy->getParamType(1)->isPointerTy())
1050 return false;
1051 setDoesNotThrow(F);
1052 setDoesNotCapture(F, 2);
1053 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001054 case LibFunc::memcpy:
1055 case LibFunc::memccpy:
1056 case LibFunc::memmove:
1057 if (FTy->getNumParams() < 2 ||
1058 !FTy->getParamType(1)->isPointerTy())
1059 return false;
1060 setDoesNotThrow(F);
1061 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001062 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001063 break;
1064 case LibFunc::memalign:
1065 if (!FTy->getReturnType()->isPointerTy())
1066 return false;
1067 setDoesNotAlias(F, 0);
1068 break;
1069 case LibFunc::mkdir:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001070 if (FTy->getNumParams() == 0 ||
1071 !FTy->getParamType(0)->isPointerTy())
1072 return false;
1073 setDoesNotThrow(F);
1074 setDoesNotCapture(F, 1);
1075 setOnlyReadsMemory(F, 1);
1076 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001077 case LibFunc::mktime:
1078 if (FTy->getNumParams() == 0 ||
1079 !FTy->getParamType(0)->isPointerTy())
1080 return false;
1081 setDoesNotThrow(F);
1082 setDoesNotCapture(F, 1);
1083 break;
1084 case LibFunc::realloc:
1085 if (FTy->getNumParams() != 2 ||
1086 !FTy->getParamType(0)->isPointerTy() ||
1087 !FTy->getReturnType()->isPointerTy())
1088 return false;
1089 setDoesNotThrow(F);
1090 setDoesNotAlias(F, 0);
1091 setDoesNotCapture(F, 1);
1092 break;
1093 case LibFunc::read:
1094 if (FTy->getNumParams() != 3 ||
1095 !FTy->getParamType(1)->isPointerTy())
1096 return false;
1097 // May throw; "read" is a valid pthread cancellation point.
1098 setDoesNotCapture(F, 2);
1099 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001100 case LibFunc::rewind:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001101 if (FTy->getNumParams() < 1 ||
1102 !FTy->getParamType(0)->isPointerTy())
1103 return false;
1104 setDoesNotThrow(F);
1105 setDoesNotCapture(F, 1);
1106 break;
1107 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001108 case LibFunc::remove:
1109 case LibFunc::realpath:
1110 if (FTy->getNumParams() < 1 ||
1111 !FTy->getParamType(0)->isPointerTy())
1112 return false;
1113 setDoesNotThrow(F);
1114 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001115 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001116 break;
1117 case LibFunc::rename:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001118 if (FTy->getNumParams() < 2 ||
1119 !FTy->getParamType(0)->isPointerTy() ||
1120 !FTy->getParamType(1)->isPointerTy())
1121 return false;
1122 setDoesNotThrow(F);
1123 setDoesNotCapture(F, 1);
1124 setDoesNotCapture(F, 2);
1125 setOnlyReadsMemory(F, 1);
1126 setOnlyReadsMemory(F, 2);
1127 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001128 case LibFunc::readlink:
1129 if (FTy->getNumParams() < 2 ||
1130 !FTy->getParamType(0)->isPointerTy() ||
1131 !FTy->getParamType(1)->isPointerTy())
1132 return false;
1133 setDoesNotThrow(F);
1134 setDoesNotCapture(F, 1);
1135 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001136 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001137 break;
1138 case LibFunc::write:
1139 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1140 return false;
1141 // May throw; "write" is a valid pthread cancellation point.
1142 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001143 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001144 break;
1145 case LibFunc::bcopy:
1146 if (FTy->getNumParams() != 3 ||
1147 !FTy->getParamType(0)->isPointerTy() ||
1148 !FTy->getParamType(1)->isPointerTy())
1149 return false;
1150 setDoesNotThrow(F);
1151 setDoesNotCapture(F, 1);
1152 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001153 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001154 break;
1155 case LibFunc::bcmp:
1156 if (FTy->getNumParams() != 3 ||
1157 !FTy->getParamType(0)->isPointerTy() ||
1158 !FTy->getParamType(1)->isPointerTy())
1159 return false;
1160 setDoesNotThrow(F);
1161 setOnlyReadsMemory(F);
1162 setDoesNotCapture(F, 1);
1163 setDoesNotCapture(F, 2);
1164 break;
1165 case LibFunc::bzero:
1166 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1167 return false;
1168 setDoesNotThrow(F);
1169 setDoesNotCapture(F, 1);
1170 break;
1171 case LibFunc::calloc:
1172 if (FTy->getNumParams() != 2 ||
1173 !FTy->getReturnType()->isPointerTy())
1174 return false;
1175 setDoesNotThrow(F);
1176 setDoesNotAlias(F, 0);
1177 break;
1178 case LibFunc::chmod:
1179 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001180 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1181 return false;
1182 setDoesNotThrow(F);
1183 setDoesNotCapture(F, 1);
1184 setOnlyReadsMemory(F, 1);
1185 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001186 case LibFunc::ctermid:
1187 case LibFunc::clearerr:
1188 case LibFunc::closedir:
1189 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1190 return false;
1191 setDoesNotThrow(F);
1192 setDoesNotCapture(F, 1);
1193 break;
1194 case LibFunc::atoi:
1195 case LibFunc::atol:
1196 case LibFunc::atof:
1197 case LibFunc::atoll:
1198 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1199 return false;
1200 setDoesNotThrow(F);
1201 setOnlyReadsMemory(F);
1202 setDoesNotCapture(F, 1);
1203 break;
1204 case LibFunc::access:
1205 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1206 return false;
1207 setDoesNotThrow(F);
1208 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001209 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001210 break;
1211 case LibFunc::fopen:
1212 if (FTy->getNumParams() != 2 ||
1213 !FTy->getReturnType()->isPointerTy() ||
1214 !FTy->getParamType(0)->isPointerTy() ||
1215 !FTy->getParamType(1)->isPointerTy())
1216 return false;
1217 setDoesNotThrow(F);
1218 setDoesNotAlias(F, 0);
1219 setDoesNotCapture(F, 1);
1220 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001221 setOnlyReadsMemory(F, 1);
1222 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001223 break;
1224 case LibFunc::fdopen:
1225 if (FTy->getNumParams() != 2 ||
1226 !FTy->getReturnType()->isPointerTy() ||
1227 !FTy->getParamType(1)->isPointerTy())
1228 return false;
1229 setDoesNotThrow(F);
1230 setDoesNotAlias(F, 0);
1231 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001232 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001233 break;
1234 case LibFunc::feof:
1235 case LibFunc::free:
1236 case LibFunc::fseek:
1237 case LibFunc::ftell:
1238 case LibFunc::fgetc:
1239 case LibFunc::fseeko:
1240 case LibFunc::ftello:
1241 case LibFunc::fileno:
1242 case LibFunc::fflush:
1243 case LibFunc::fclose:
1244 case LibFunc::fsetpos:
1245 case LibFunc::flockfile:
1246 case LibFunc::funlockfile:
1247 case LibFunc::ftrylockfile:
1248 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1249 return false;
1250 setDoesNotThrow(F);
1251 setDoesNotCapture(F, 1);
1252 break;
1253 case LibFunc::ferror:
1254 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1255 return false;
1256 setDoesNotThrow(F);
1257 setDoesNotCapture(F, 1);
1258 setOnlyReadsMemory(F);
1259 break;
1260 case LibFunc::fputc:
1261 case LibFunc::fstat:
1262 case LibFunc::frexp:
1263 case LibFunc::frexpf:
1264 case LibFunc::frexpl:
1265 case LibFunc::fstatvfs:
1266 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1267 return false;
1268 setDoesNotThrow(F);
1269 setDoesNotCapture(F, 2);
1270 break;
1271 case LibFunc::fgets:
1272 if (FTy->getNumParams() != 3 ||
1273 !FTy->getParamType(0)->isPointerTy() ||
1274 !FTy->getParamType(2)->isPointerTy())
1275 return false;
1276 setDoesNotThrow(F);
1277 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001278 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001279 case LibFunc::fread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001280 if (FTy->getNumParams() != 4 ||
1281 !FTy->getParamType(0)->isPointerTy() ||
1282 !FTy->getParamType(3)->isPointerTy())
1283 return false;
1284 setDoesNotThrow(F);
1285 setDoesNotCapture(F, 1);
1286 setDoesNotCapture(F, 4);
1287 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001288 case LibFunc::fwrite:
1289 if (FTy->getNumParams() != 4 ||
1290 !FTy->getParamType(0)->isPointerTy() ||
1291 !FTy->getParamType(3)->isPointerTy())
1292 return false;
1293 setDoesNotThrow(F);
1294 setDoesNotCapture(F, 1);
1295 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001296 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001297 case LibFunc::fputs:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001298 if (FTy->getNumParams() < 2 ||
1299 !FTy->getParamType(0)->isPointerTy() ||
1300 !FTy->getParamType(1)->isPointerTy())
1301 return false;
1302 setDoesNotThrow(F);
1303 setDoesNotCapture(F, 1);
1304 setDoesNotCapture(F, 2);
1305 setOnlyReadsMemory(F, 1);
1306 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001307 case LibFunc::fscanf:
1308 case LibFunc::fprintf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001309 if (FTy->getNumParams() < 2 ||
1310 !FTy->getParamType(0)->isPointerTy() ||
1311 !FTy->getParamType(1)->isPointerTy())
1312 return false;
1313 setDoesNotThrow(F);
1314 setDoesNotCapture(F, 1);
1315 setDoesNotCapture(F, 2);
1316 setOnlyReadsMemory(F, 2);
1317 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001318 case LibFunc::fgetpos:
1319 if (FTy->getNumParams() < 2 ||
1320 !FTy->getParamType(0)->isPointerTy() ||
1321 !FTy->getParamType(1)->isPointerTy())
1322 return false;
1323 setDoesNotThrow(F);
1324 setDoesNotCapture(F, 1);
1325 setDoesNotCapture(F, 2);
1326 break;
1327 case LibFunc::getc:
1328 case LibFunc::getlogin_r:
1329 case LibFunc::getc_unlocked:
1330 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1331 return false;
1332 setDoesNotThrow(F);
1333 setDoesNotCapture(F, 1);
1334 break;
1335 case LibFunc::getenv:
1336 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1337 return false;
1338 setDoesNotThrow(F);
1339 setOnlyReadsMemory(F);
1340 setDoesNotCapture(F, 1);
1341 break;
1342 case LibFunc::gets:
1343 case LibFunc::getchar:
1344 setDoesNotThrow(F);
1345 break;
1346 case LibFunc::getitimer:
1347 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1348 return false;
1349 setDoesNotThrow(F);
1350 setDoesNotCapture(F, 2);
1351 break;
1352 case LibFunc::getpwnam:
1353 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1354 return false;
1355 setDoesNotThrow(F);
1356 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001357 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001358 break;
1359 case LibFunc::ungetc:
1360 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1361 return false;
1362 setDoesNotThrow(F);
1363 setDoesNotCapture(F, 2);
1364 break;
1365 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001366 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1367 return false;
1368 setDoesNotThrow(F);
1369 setDoesNotCapture(F, 1);
1370 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001371 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001372 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1373 return false;
1374 setDoesNotThrow(F);
1375 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001376 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001377 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001378 case LibFunc::unsetenv:
1379 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1380 return false;
1381 setDoesNotThrow(F);
1382 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001383 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001384 break;
1385 case LibFunc::utime:
1386 case LibFunc::utimes:
1387 if (FTy->getNumParams() != 2 ||
1388 !FTy->getParamType(0)->isPointerTy() ||
1389 !FTy->getParamType(1)->isPointerTy())
1390 return false;
1391 setDoesNotThrow(F);
1392 setDoesNotCapture(F, 1);
1393 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001394 setOnlyReadsMemory(F, 1);
1395 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001396 break;
1397 case LibFunc::putc:
1398 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1399 return false;
1400 setDoesNotThrow(F);
1401 setDoesNotCapture(F, 2);
1402 break;
1403 case LibFunc::puts:
1404 case LibFunc::printf:
1405 case LibFunc::perror:
1406 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1407 return false;
1408 setDoesNotThrow(F);
1409 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001410 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001411 break;
1412 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001413 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1414 return false;
1415 // May throw; "pread" is a valid pthread cancellation point.
1416 setDoesNotCapture(F, 2);
1417 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001418 case LibFunc::pwrite:
1419 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1420 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001421 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001422 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001423 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001424 break;
1425 case LibFunc::putchar:
1426 setDoesNotThrow(F);
1427 break;
1428 case LibFunc::popen:
1429 if (FTy->getNumParams() != 2 ||
1430 !FTy->getReturnType()->isPointerTy() ||
1431 !FTy->getParamType(0)->isPointerTy() ||
1432 !FTy->getParamType(1)->isPointerTy())
1433 return false;
1434 setDoesNotThrow(F);
1435 setDoesNotAlias(F, 0);
1436 setDoesNotCapture(F, 1);
1437 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001438 setOnlyReadsMemory(F, 1);
1439 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001440 break;
1441 case LibFunc::pclose:
1442 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1443 return false;
1444 setDoesNotThrow(F);
1445 setDoesNotCapture(F, 1);
1446 break;
1447 case LibFunc::vscanf:
1448 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1449 return false;
1450 setDoesNotThrow(F);
1451 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001452 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001453 break;
1454 case LibFunc::vsscanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001455 if (FTy->getNumParams() != 3 ||
1456 !FTy->getParamType(1)->isPointerTy() ||
1457 !FTy->getParamType(2)->isPointerTy())
1458 return false;
1459 setDoesNotThrow(F);
1460 setDoesNotCapture(F, 1);
1461 setDoesNotCapture(F, 2);
1462 setOnlyReadsMemory(F, 1);
1463 setOnlyReadsMemory(F, 2);
1464 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001465 case LibFunc::vfscanf:
1466 if (FTy->getNumParams() != 3 ||
1467 !FTy->getParamType(1)->isPointerTy() ||
1468 !FTy->getParamType(2)->isPointerTy())
1469 return false;
1470 setDoesNotThrow(F);
1471 setDoesNotCapture(F, 1);
1472 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001473 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001474 break;
1475 case LibFunc::valloc:
1476 if (!FTy->getReturnType()->isPointerTy())
1477 return false;
1478 setDoesNotThrow(F);
1479 setDoesNotAlias(F, 0);
1480 break;
1481 case LibFunc::vprintf:
1482 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1483 return false;
1484 setDoesNotThrow(F);
1485 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001486 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001487 break;
1488 case LibFunc::vfprintf:
1489 case LibFunc::vsprintf:
1490 if (FTy->getNumParams() != 3 ||
1491 !FTy->getParamType(0)->isPointerTy() ||
1492 !FTy->getParamType(1)->isPointerTy())
1493 return false;
1494 setDoesNotThrow(F);
1495 setDoesNotCapture(F, 1);
1496 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001497 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001498 break;
1499 case LibFunc::vsnprintf:
1500 if (FTy->getNumParams() != 4 ||
1501 !FTy->getParamType(0)->isPointerTy() ||
1502 !FTy->getParamType(2)->isPointerTy())
1503 return false;
1504 setDoesNotThrow(F);
1505 setDoesNotCapture(F, 1);
1506 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001507 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001508 break;
1509 case LibFunc::open:
1510 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1511 return false;
1512 // May throw; "open" is a valid pthread cancellation point.
1513 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001514 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001515 break;
1516 case LibFunc::opendir:
1517 if (FTy->getNumParams() != 1 ||
1518 !FTy->getReturnType()->isPointerTy() ||
1519 !FTy->getParamType(0)->isPointerTy())
1520 return false;
1521 setDoesNotThrow(F);
1522 setDoesNotAlias(F, 0);
1523 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001524 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001525 break;
1526 case LibFunc::tmpfile:
1527 if (!FTy->getReturnType()->isPointerTy())
1528 return false;
1529 setDoesNotThrow(F);
1530 setDoesNotAlias(F, 0);
1531 break;
1532 case LibFunc::times:
1533 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1534 return false;
1535 setDoesNotThrow(F);
1536 setDoesNotCapture(F, 1);
1537 break;
1538 case LibFunc::htonl:
1539 case LibFunc::htons:
1540 case LibFunc::ntohl:
1541 case LibFunc::ntohs:
1542 setDoesNotThrow(F);
1543 setDoesNotAccessMemory(F);
1544 break;
1545 case LibFunc::lstat:
1546 if (FTy->getNumParams() != 2 ||
1547 !FTy->getParamType(0)->isPointerTy() ||
1548 !FTy->getParamType(1)->isPointerTy())
1549 return false;
1550 setDoesNotThrow(F);
1551 setDoesNotCapture(F, 1);
1552 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001553 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001554 break;
1555 case LibFunc::lchown:
1556 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1557 return false;
1558 setDoesNotThrow(F);
1559 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001560 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001561 break;
1562 case LibFunc::qsort:
1563 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1564 return false;
1565 // May throw; places call through function pointer.
1566 setDoesNotCapture(F, 4);
1567 break;
1568 case LibFunc::dunder_strdup:
1569 case LibFunc::dunder_strndup:
1570 if (FTy->getNumParams() < 1 ||
1571 !FTy->getReturnType()->isPointerTy() ||
1572 !FTy->getParamType(0)->isPointerTy())
1573 return false;
1574 setDoesNotThrow(F);
1575 setDoesNotAlias(F, 0);
1576 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001577 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001578 break;
1579 case LibFunc::dunder_strtok_r:
1580 if (FTy->getNumParams() != 3 ||
1581 !FTy->getParamType(1)->isPointerTy())
1582 return false;
1583 setDoesNotThrow(F);
1584 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001585 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001586 break;
1587 case LibFunc::under_IO_getc:
1588 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1589 return false;
1590 setDoesNotThrow(F);
1591 setDoesNotCapture(F, 1);
1592 break;
1593 case LibFunc::under_IO_putc:
1594 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1595 return false;
1596 setDoesNotThrow(F);
1597 setDoesNotCapture(F, 2);
1598 break;
1599 case LibFunc::dunder_isoc99_scanf:
1600 if (FTy->getNumParams() < 1 ||
1601 !FTy->getParamType(0)->isPointerTy())
1602 return false;
1603 setDoesNotThrow(F);
1604 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001605 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001606 break;
1607 case LibFunc::stat64:
1608 case LibFunc::lstat64:
1609 case LibFunc::statvfs64:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001610 if (FTy->getNumParams() < 1 ||
1611 !FTy->getParamType(0)->isPointerTy() ||
1612 !FTy->getParamType(1)->isPointerTy())
1613 return false;
1614 setDoesNotThrow(F);
1615 setDoesNotCapture(F, 1);
1616 setDoesNotCapture(F, 2);
1617 setOnlyReadsMemory(F, 1);
1618 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001619 case LibFunc::dunder_isoc99_sscanf:
1620 if (FTy->getNumParams() < 1 ||
1621 !FTy->getParamType(0)->isPointerTy() ||
1622 !FTy->getParamType(1)->isPointerTy())
1623 return false;
1624 setDoesNotThrow(F);
1625 setDoesNotCapture(F, 1);
1626 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001627 setOnlyReadsMemory(F, 1);
1628 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001629 break;
1630 case LibFunc::fopen64:
1631 if (FTy->getNumParams() != 2 ||
1632 !FTy->getReturnType()->isPointerTy() ||
1633 !FTy->getParamType(0)->isPointerTy() ||
1634 !FTy->getParamType(1)->isPointerTy())
1635 return false;
1636 setDoesNotThrow(F);
1637 setDoesNotAlias(F, 0);
1638 setDoesNotCapture(F, 1);
1639 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001640 setOnlyReadsMemory(F, 1);
1641 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001642 break;
1643 case LibFunc::fseeko64:
1644 case LibFunc::ftello64:
1645 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1646 return false;
1647 setDoesNotThrow(F);
1648 setDoesNotCapture(F, 1);
1649 break;
1650 case LibFunc::tmpfile64:
1651 if (!FTy->getReturnType()->isPointerTy())
1652 return false;
1653 setDoesNotThrow(F);
1654 setDoesNotAlias(F, 0);
1655 break;
1656 case LibFunc::fstat64:
1657 case LibFunc::fstatvfs64:
1658 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1659 return false;
1660 setDoesNotThrow(F);
1661 setDoesNotCapture(F, 2);
1662 break;
1663 case LibFunc::open64:
1664 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1665 return false;
1666 // May throw; "open" is a valid pthread cancellation point.
1667 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001668 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001669 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001670 case LibFunc::gettimeofday:
1671 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1672 !FTy->getParamType(1)->isPointerTy())
1673 return false;
1674 // Currently some platforms have the restrict keyword on the arguments to
1675 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1676 // arguments.
1677 setDoesNotThrow(F);
1678 setDoesNotCapture(F, 1);
1679 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001680 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001681 default:
1682 // Didn't mark any attributes.
1683 return false;
1684 }
1685
1686 return true;
1687}
1688
1689/// annotateLibraryCalls - Adds attributes to well-known standard library
1690/// call declarations.
1691bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1692 bool MadeChange = false;
1693
1694 // Check each function in turn annotating well-known library function
1695 // declarations with attributes.
1696 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1697 Function *F = (*I)->getFunction();
1698
Craig Topperf40110f2014-04-25 05:29:35 +00001699 if (F && F->isDeclaration())
Meador Inge6b6a1612013-03-21 00:55:59 +00001700 MadeChange |= inferPrototypeAttributes(*F);
1701 }
1702
1703 return MadeChange;
1704}
1705
Chris Lattner4422d312010-04-16 22:42:17 +00001706bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman86449d72010-11-08 16:10:15 +00001707 AA = &getAnalysis<AliasAnalysis>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001708 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohman86449d72010-11-08 16:10:15 +00001709
Meador Inge6b6a1612013-03-21 00:55:59 +00001710 bool Changed = annotateLibraryCalls(SCC);
1711 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001712 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001713 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001714 return Changed;
1715}