blob: 206b630c7922b26f3f644442d7461a340ecc3fed [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 Kornienkof00654e2015-06-23 09:49:53 +0000135}
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
Chandler Carruth194f59c2015-07-22 23:15:57 +0000169 FunctionModRefBehavior MRB = AA->getModRefBehavior(F);
170 if (MRB == FMRB_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;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000196 FunctionModRefBehavior MRB = AA->getModRefBehavior(CS);
Dan Gohman2694e142010-11-10 01:02:18 +0000197 // 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)) {
Chandler Carruth194f59c2015-07-22 23:15:57 +0000213 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000214 // Writes non-local memory. Give up.
215 return false;
Chandler Carruth194f59c2015-07-22 23:15:57 +0000216 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000217 // 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.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000225 if (MRB & MRI_Mod)
Dan Gohman2694e142010-11-10 01:02:18 +0000226 return false;
227 // If it reads, note it.
Chandler Carruth194f59c2015-07-22 23:15:57 +0000228 if (MRB & MRI_Ref)
Dan Gohman2694e142010-11-10 01:02:18 +0000229 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 Kornienkof00654e2015-06-23 09:49:53 +0000382}
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 Kornienkof00654e2015-06-23 09:49:53 +0000409}
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;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000418
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000419 // inalloca arguments are always clobbered by the call.
420 if (A->hasInAllocaAttr())
421 return Attribute::None;
422
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000423 bool IsRead = false;
424 // We don't need to track IsWritten. If A is written to, return immediately.
425
Chandler Carruthcdf47882014-03-09 03:16:01 +0000426 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000427 Visited.insert(&U);
428 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000429 }
430
431 while (!Worklist.empty()) {
432 Use *U = Worklist.pop_back_val();
433 Instruction *I = cast<Instruction>(U->getUser());
434 Value *V = U->get();
435
436 switch (I->getOpcode()) {
437 case Instruction::BitCast:
438 case Instruction::GetElementPtr:
439 case Instruction::PHI:
440 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000441 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000442 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000443 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000444 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000445 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000446 break;
447
448 case Instruction::Call:
449 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000450 bool Captures = true;
451
452 if (I->getType()->isVoidTy())
453 Captures = false;
454
455 auto AddUsersToWorklistIfCapturing = [&] {
456 if (Captures)
457 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000458 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000459 Worklist.push_back(&UU);
460 };
461
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000462 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000463 if (CS.doesNotAccessMemory()) {
464 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000465 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000466 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000467
468 Function *F = CS.getCalledFunction();
469 if (!F) {
470 if (CS.onlyReadsMemory()) {
471 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000472 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000473 continue;
474 }
475 return Attribute::None;
476 }
477
478 Function::arg_iterator AI = F->arg_begin(), AE = F->arg_end();
479 CallSite::arg_iterator B = CS.arg_begin(), E = CS.arg_end();
480 for (CallSite::arg_iterator A = B; A != E; ++A, ++AI) {
481 if (A->get() == V) {
482 if (AI == AE) {
483 assert(F->isVarArg() &&
484 "More params than args in non-varargs call.");
485 return Attribute::None;
486 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000487 Captures &= !CS.doesNotCapture(A - B);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000488 if (SCCNodes.count(AI))
489 continue;
490 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(A - B))
491 return Attribute::None;
492 if (!CS.doesNotAccessMemory(A - B))
493 IsRead = true;
494 }
495 }
Nick Lewycky59633cb2014-05-30 02:31:27 +0000496 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000497 break;
498 }
499
500 case Instruction::Load:
501 IsRead = true;
502 break;
503
504 case Instruction::ICmp:
505 case Instruction::Ret:
506 break;
507
508 default:
509 return Attribute::None;
510 }
511 }
512
513 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
514}
515
516/// AddArgumentAttrs - Deduce nocapture attributes for the SCC.
517bool FunctionAttrs::AddArgumentAttrs(const CallGraphSCC &SCC) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000518 bool Changed = false;
519
Nick Lewycky4c378a42011-12-28 23:24:21 +0000520 SmallPtrSet<Function*, 8> SCCNodes;
521
522 // Fill SCCNodes with the elements of the SCC. Used for quickly
523 // looking up whether a given CallGraphNode is in this SCC.
524 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
525 Function *F = (*I)->getFunction();
Chandler Carruth0fb99812014-08-13 10:49:33 +0000526 if (F && !F->isDeclaration() && !F->mayBeOverridden() &&
527 !F->hasFnAttribute(Attribute::OptimizeNone))
Nick Lewycky4c378a42011-12-28 23:24:21 +0000528 SCCNodes.insert(F);
529 }
530
531 ArgumentGraph AG;
532
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000533 AttrBuilder B;
534 B.addAttribute(Attribute::NoCapture);
535
Duncan Sands44c8cd92008-12-31 16:14:43 +0000536 // Check each function in turn, determining which pointer arguments are not
537 // captured.
Chris Lattner4422d312010-04-16 22:42:17 +0000538 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
539 Function *F = (*I)->getFunction();
Duncan Sands44c8cd92008-12-31 16:14:43 +0000540
Chandler Carruth0fb99812014-08-13 10:49:33 +0000541 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
542 // External node or function we're trying not to optimize - only a problem
543 // for arguments that we pass to it.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000544 continue;
545
546 // Definitions with weak linkage may be overridden at linktime with
Nick Lewycky4c378a42011-12-28 23:24:21 +0000547 // something that captures pointers, so treat them like declarations.
Duncan Sands44c8cd92008-12-31 16:14:43 +0000548 if (F->isDeclaration() || F->mayBeOverridden())
549 continue;
550
Nick Lewycky4c378a42011-12-28 23:24:21 +0000551 // Functions that are readonly (or readnone) and nounwind and don't return
552 // a value can't capture arguments. Don't analyze them.
553 if (F->onlyReadsMemory() && F->doesNotThrow() &&
554 F->getReturnType()->isVoidTy()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000555 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
556 A != E; ++A) {
557 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
558 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo() + 1, B));
559 ++NumNoCapture;
560 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000561 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000562 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000563 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000564 }
565
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000566 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end();
567 A != E; ++A) {
568 if (!A->getType()->isPointerTy()) continue;
569 bool HasNonLocalUses = false;
570 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000571 ArgumentUsesTracker Tracker(SCCNodes);
572 PointerMayBeCaptured(A, &Tracker);
573 if (!Tracker.Captured) {
574 if (Tracker.Uses.empty()) {
575 // If it's trivially not captured, mark it nocapture now.
576 A->addAttr(AttributeSet::get(F->getContext(), A->getArgNo()+1, B));
577 ++NumNoCapture;
578 Changed = true;
579 } else {
580 // If it's not trivially captured and not trivially not captured,
581 // then it must be calling into another function in our SCC. Save
582 // its particulars for Argument-SCC analysis later.
583 ArgumentGraphNode *Node = AG[A];
584 for (SmallVectorImpl<Argument*>::iterator UI = Tracker.Uses.begin(),
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000585 UE = Tracker.Uses.end(); UI != UE; ++UI) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000586 Node->Uses.push_back(AG[*UI]);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000587 if (*UI != A)
588 HasNonLocalUses = true;
589 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000590 }
591 }
592 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
593 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000594 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
595 // Can we determine that it's readonly/readnone without doing an SCC?
596 // Note that we don't allow any calls at all here, or else our result
597 // will be dependent on the iteration order through the functions in the
598 // SCC.
599 SmallPtrSet<Argument*, 8> Self;
600 Self.insert(A);
601 Attribute::AttrKind R = determinePointerReadAttrs(A, Self);
602 if (R != Attribute::None) {
603 AttrBuilder B;
604 B.addAttribute(R);
605 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
606 Changed = true;
607 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
608 }
609 }
610 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000611 }
612
613 // The graph we've collected is partial because we stopped scanning for
614 // argument uses once we solved the argument trivially. These partial nodes
615 // show up as ArgumentGraphNode objects with an empty Uses list, and for
616 // these nodes the final decision about whether they capture has already been
617 // made. If the definition doesn't have a 'nocapture' attribute by now, it
618 // captures.
619
Duncan P. N. Exon Smith8e661ef2014-02-04 19:19:07 +0000620 for (scc_iterator<ArgumentGraph*> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000621 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000622 if (ArgumentSCC.size() == 1) {
623 if (!ArgumentSCC[0]->Definition) continue; // synthetic root node
624
625 // eg. "void f(int* x) { if (...) f(x); }"
626 if (ArgumentSCC[0]->Uses.size() == 1 &&
627 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000628 Argument *A = ArgumentSCC[0]->Definition;
629 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky7e820552009-01-02 03:46:56 +0000630 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000631 Changed = true;
632 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000633 continue;
634 }
635
636 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000637 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
638 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000639 ArgumentGraphNode *Node = *I;
640 if (Node->Uses.empty()) {
641 if (!Node->Definition->hasNoCaptureAttr())
642 SCCCaptured = true;
643 }
644 }
645 if (SCCCaptured) continue;
646
647 SmallPtrSet<Argument*, 8> ArgumentSCCNodes;
648 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
649 // quickly looking up whether a given Argument is in this ArgumentSCC.
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000650 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end(); I != E; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000651 ArgumentSCCNodes.insert((*I)->Definition);
652 }
653
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000654 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
655 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000656 ArgumentGraphNode *N = *I;
657 for (SmallVectorImpl<ArgumentGraphNode*>::iterator UI = N->Uses.begin(),
658 UE = N->Uses.end(); UI != UE; ++UI) {
659 Argument *A = (*UI)->Definition;
660 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
661 continue;
662 SCCCaptured = true;
663 break;
664 }
665 }
666 if (SCCCaptured) continue;
667
Nick Lewyckyf740db32012-01-05 22:21:45 +0000668 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000669 Argument *A = ArgumentSCC[i]->Definition;
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000670 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
Nick Lewycky4c378a42011-12-28 23:24:21 +0000671 ++NumNoCapture;
672 Changed = true;
673 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000674
675 // We also want to compute readonly/readnone. With a small number of false
676 // negatives, we can assume that any pointer which is captured isn't going
677 // to be provably readonly or readnone, since by definition we can't
678 // analyze all uses of a captured pointer.
679 //
680 // The false negatives happen when the pointer is captured by a function
681 // that promises readonly/readnone behaviour on the pointer, then the
682 // pointer's lifetime ends before anything that writes to arbitrary memory.
683 // Also, a readonly/readnone pointer may be returned, but returning a
684 // pointer is capturing it.
685
686 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
687 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
688 Argument *A = ArgumentSCC[i]->Definition;
689 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
690 if (K == Attribute::ReadNone)
691 continue;
692 if (K == Attribute::ReadOnly) {
693 ReadAttr = Attribute::ReadOnly;
694 continue;
695 }
696 ReadAttr = K;
697 break;
698 }
699
700 if (ReadAttr != Attribute::None) {
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000701 AttrBuilder B, R;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000702 B.addAttribute(ReadAttr);
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000703 R.addAttribute(Attribute::ReadOnly)
704 .addAttribute(Attribute::ReadNone);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000705 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
706 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000707 // Clear out existing readonly/readnone attributes
708 A->removeAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, R));
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000709 A->addAttr(AttributeSet::get(A->getContext(), A->getArgNo() + 1, B));
710 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
711 Changed = true;
712 }
713 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000714 }
715
716 return Changed;
717}
718
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000719/// IsFunctionMallocLike - A function is malloc-like if it returns either null
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000720/// or a pointer that doesn't alias any other pointer visible to the caller.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000721bool FunctionAttrs::IsFunctionMallocLike(Function *F,
Chris Lattner2f2110a2009-08-31 04:09:04 +0000722 SmallPtrSet<Function*, 8> &SCCNodes) const {
Benjamin Kramer15591272012-10-31 13:45:49 +0000723 SmallSetVector<Value *, 8> FlowsToReturn;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000724 for (Function::iterator I = F->begin(), E = F->end(); I != E; ++I)
725 if (ReturnInst *Ret = dyn_cast<ReturnInst>(I->getTerminator()))
726 FlowsToReturn.insert(Ret->getReturnValue());
727
728 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000729 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000730
731 if (Constant *C = dyn_cast<Constant>(RetVal)) {
732 if (!C->isNullValue() && !isa<UndefValue>(C))
733 return false;
734
735 continue;
736 }
737
738 if (isa<Argument>(RetVal))
739 return false;
740
741 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
742 switch (RVI->getOpcode()) {
743 // Extend the analysis by looking upwards.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000744 case Instruction::BitCast:
Victor Hernandez5d034492009-09-18 22:35:49 +0000745 case Instruction::GetElementPtr:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000746 case Instruction::AddrSpaceCast:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000747 FlowsToReturn.insert(RVI->getOperand(0));
748 continue;
749 case Instruction::Select: {
750 SelectInst *SI = cast<SelectInst>(RVI);
751 FlowsToReturn.insert(SI->getTrueValue());
752 FlowsToReturn.insert(SI->getFalseValue());
Chris Lattner43d0db72009-09-27 21:29:28 +0000753 continue;
754 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000755 case Instruction::PHI: {
756 PHINode *PN = cast<PHINode>(RVI);
Pete Cooper833f34d2015-05-12 20:05:31 +0000757 for (Value *IncValue : PN->incoming_values())
758 FlowsToReturn.insert(IncValue);
Chris Lattner43d0db72009-09-27 21:29:28 +0000759 continue;
760 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000761
762 // Check whether the pointer came from an allocation.
763 case Instruction::Alloca:
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000764 break;
765 case Instruction::Call:
766 case Instruction::Invoke: {
767 CallSite CS(RVI);
Bill Wendling3d7b0b82012-12-19 07:18:57 +0000768 if (CS.paramHasAttr(0, Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000769 break;
770 if (CS.getCalledFunction() &&
Chris Lattner2f2110a2009-08-31 04:09:04 +0000771 SCCNodes.count(CS.getCalledFunction()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000772 break;
773 } // fall-through
774 default:
775 return false; // Did not come from an allocation.
776 }
777
Dan Gohman94e61762009-11-19 21:57:48 +0000778 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000779 return false;
780 }
781
782 return true;
783}
784
785/// AddNoAliasAttrs - Deduce noalias attributes for the SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000786bool FunctionAttrs::AddNoAliasAttrs(const CallGraphSCC &SCC) {
Chris Lattner2f2110a2009-08-31 04:09:04 +0000787 SmallPtrSet<Function*, 8> SCCNodes;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000788
789 // Fill SCCNodes with the elements of the SCC. Used for quickly
790 // looking up whether a given CallGraphNode is in this SCC.
Chris Lattner4422d312010-04-16 22:42:17 +0000791 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I)
792 SCCNodes.insert((*I)->getFunction());
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000793
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000794 // Check each function in turn, determining which functions return noalias
795 // pointers.
Chris Lattner4422d312010-04-16 22:42:17 +0000796 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
797 Function *F = (*I)->getFunction();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000798
Chandler Carruth0fb99812014-08-13 10:49:33 +0000799 if (!F || F->hasFnAttribute(Attribute::OptimizeNone))
800 // External node or node we don't want to optimize - skip it;
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000801 return false;
802
803 // Already noalias.
804 if (F->doesNotAlias(0))
805 continue;
806
807 // Definitions with weak linkage may be overridden at linktime, so
808 // treat them like declarations.
809 if (F->isDeclaration() || F->mayBeOverridden())
810 return false;
811
812 // We annotate noalias return values, which are only applicable to
813 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000814 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000815 continue;
816
817 if (!IsFunctionMallocLike(F, SCCNodes))
818 return false;
819 }
820
821 bool MadeChange = false;
Chris Lattner4422d312010-04-16 22:42:17 +0000822 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
823 Function *F = (*I)->getFunction();
Duncan Sands19d0b472010-02-16 11:11:14 +0000824 if (F->doesNotAlias(0) || !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000825 continue;
826
827 F->setDoesNotAlias(0);
828 ++NumNoAlias;
829 MadeChange = true;
830 }
831
832 return MadeChange;
833}
834
Meador Inge6b6a1612013-03-21 00:55:59 +0000835/// inferPrototypeAttributes - Analyze the name and prototype of the
836/// given function and set any applicable attributes. Returns true
837/// if any attributes were set and false otherwise.
838bool FunctionAttrs::inferPrototypeAttributes(Function &F) {
Chandler Carruth0fb99812014-08-13 10:49:33 +0000839 if (F.hasFnAttribute(Attribute::OptimizeNone))
840 return false;
841
Meador Inge6b6a1612013-03-21 00:55:59 +0000842 FunctionType *FTy = F.getFunctionType();
843 LibFunc::Func TheLibFunc;
844 if (!(TLI->getLibFunc(F.getName(), TheLibFunc) && TLI->has(TheLibFunc)))
845 return false;
846
847 switch (TheLibFunc) {
848 case LibFunc::strlen:
849 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
850 return false;
851 setOnlyReadsMemory(F);
852 setDoesNotThrow(F);
853 setDoesNotCapture(F, 1);
854 break;
855 case LibFunc::strchr:
856 case LibFunc::strrchr:
857 if (FTy->getNumParams() != 2 ||
858 !FTy->getParamType(0)->isPointerTy() ||
859 !FTy->getParamType(1)->isIntegerTy())
860 return false;
861 setOnlyReadsMemory(F);
862 setDoesNotThrow(F);
863 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000864 case LibFunc::strtol:
865 case LibFunc::strtod:
866 case LibFunc::strtof:
867 case LibFunc::strtoul:
868 case LibFunc::strtoll:
869 case LibFunc::strtold:
Meador Inge6b6a1612013-03-21 00:55:59 +0000870 case LibFunc::strtoull:
871 if (FTy->getNumParams() < 2 ||
872 !FTy->getParamType(1)->isPointerTy())
873 return false;
874 setDoesNotThrow(F);
875 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000876 setOnlyReadsMemory(F, 1);
877 break;
878 case LibFunc::strcpy:
879 case LibFunc::stpcpy:
880 case LibFunc::strcat:
881 case LibFunc::strncat:
882 case LibFunc::strncpy:
883 case LibFunc::stpncpy:
884 if (FTy->getNumParams() < 2 ||
885 !FTy->getParamType(1)->isPointerTy())
886 return false;
887 setDoesNotThrow(F);
888 setDoesNotCapture(F, 2);
889 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000890 break;
891 case LibFunc::strxfrm:
892 if (FTy->getNumParams() != 3 ||
893 !FTy->getParamType(0)->isPointerTy() ||
894 !FTy->getParamType(1)->isPointerTy())
895 return false;
896 setDoesNotThrow(F);
897 setDoesNotCapture(F, 1);
898 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000899 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000900 break;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000901 case LibFunc::strcmp: //0,1
902 case LibFunc::strspn: // 0,1
903 case LibFunc::strncmp: // 0,1
904 case LibFunc::strcspn: //0,1
905 case LibFunc::strcoll: //0,1
906 case LibFunc::strcasecmp: // 0,1
907 case LibFunc::strncasecmp: //
Meador Inge6b6a1612013-03-21 00:55:59 +0000908 if (FTy->getNumParams() < 2 ||
909 !FTy->getParamType(0)->isPointerTy() ||
910 !FTy->getParamType(1)->isPointerTy())
911 return false;
912 setOnlyReadsMemory(F);
913 setDoesNotThrow(F);
914 setDoesNotCapture(F, 1);
915 setDoesNotCapture(F, 2);
916 break;
917 case LibFunc::strstr:
918 case LibFunc::strpbrk:
919 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
920 return false;
921 setOnlyReadsMemory(F);
922 setDoesNotThrow(F);
923 setDoesNotCapture(F, 2);
924 break;
925 case LibFunc::strtok:
926 case LibFunc::strtok_r:
927 if (FTy->getNumParams() < 2 || !FTy->getParamType(1)->isPointerTy())
928 return false;
929 setDoesNotThrow(F);
930 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000931 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000932 break;
933 case LibFunc::scanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000934 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
935 return false;
936 setDoesNotThrow(F);
937 setDoesNotCapture(F, 1);
938 setOnlyReadsMemory(F, 1);
939 break;
Meador Inge6b6a1612013-03-21 00:55:59 +0000940 case LibFunc::setbuf:
941 case LibFunc::setvbuf:
942 if (FTy->getNumParams() < 1 || !FTy->getParamType(0)->isPointerTy())
943 return false;
944 setDoesNotThrow(F);
945 setDoesNotCapture(F, 1);
946 break;
947 case LibFunc::strdup:
948 case LibFunc::strndup:
949 if (FTy->getNumParams() < 1 || !FTy->getReturnType()->isPointerTy() ||
950 !FTy->getParamType(0)->isPointerTy())
951 return false;
952 setDoesNotThrow(F);
953 setDoesNotAlias(F, 0);
954 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000955 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +0000956 break;
957 case LibFunc::stat:
Meador Inge6b6a1612013-03-21 00:55:59 +0000958 case LibFunc::statvfs:
959 if (FTy->getNumParams() < 2 ||
960 !FTy->getParamType(0)->isPointerTy() ||
961 !FTy->getParamType(1)->isPointerTy())
962 return false;
963 setDoesNotThrow(F);
964 setDoesNotCapture(F, 1);
965 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000966 setOnlyReadsMemory(F, 1);
967 break;
968 case LibFunc::sscanf:
969 if (FTy->getNumParams() < 2 ||
970 !FTy->getParamType(0)->isPointerTy() ||
971 !FTy->getParamType(1)->isPointerTy())
972 return false;
973 setDoesNotThrow(F);
974 setDoesNotCapture(F, 1);
975 setDoesNotCapture(F, 2);
976 setOnlyReadsMemory(F, 1);
977 setOnlyReadsMemory(F, 2);
978 break;
979 case LibFunc::sprintf:
980 if (FTy->getNumParams() < 2 ||
981 !FTy->getParamType(0)->isPointerTy() ||
982 !FTy->getParamType(1)->isPointerTy())
983 return false;
984 setDoesNotThrow(F);
985 setDoesNotCapture(F, 1);
986 setDoesNotCapture(F, 2);
987 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +0000988 break;
989 case LibFunc::snprintf:
990 if (FTy->getNumParams() != 3 ||
991 !FTy->getParamType(0)->isPointerTy() ||
992 !FTy->getParamType(2)->isPointerTy())
993 return false;
994 setDoesNotThrow(F);
995 setDoesNotCapture(F, 1);
996 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000997 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +0000998 break;
999 case LibFunc::setitimer:
1000 if (FTy->getNumParams() != 3 ||
1001 !FTy->getParamType(1)->isPointerTy() ||
1002 !FTy->getParamType(2)->isPointerTy())
1003 return false;
1004 setDoesNotThrow(F);
1005 setDoesNotCapture(F, 2);
1006 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001007 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001008 break;
1009 case LibFunc::system:
1010 if (FTy->getNumParams() != 1 ||
1011 !FTy->getParamType(0)->isPointerTy())
1012 return false;
1013 // May throw; "system" is a valid pthread cancellation point.
1014 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001015 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001016 break;
1017 case LibFunc::malloc:
1018 if (FTy->getNumParams() != 1 ||
1019 !FTy->getReturnType()->isPointerTy())
1020 return false;
1021 setDoesNotThrow(F);
1022 setDoesNotAlias(F, 0);
1023 break;
1024 case LibFunc::memcmp:
1025 if (FTy->getNumParams() != 3 ||
1026 !FTy->getParamType(0)->isPointerTy() ||
1027 !FTy->getParamType(1)->isPointerTy())
1028 return false;
1029 setOnlyReadsMemory(F);
1030 setDoesNotThrow(F);
1031 setDoesNotCapture(F, 1);
1032 setDoesNotCapture(F, 2);
1033 break;
1034 case LibFunc::memchr:
1035 case LibFunc::memrchr:
1036 if (FTy->getNumParams() != 3)
1037 return false;
1038 setOnlyReadsMemory(F);
1039 setDoesNotThrow(F);
1040 break;
1041 case LibFunc::modf:
1042 case LibFunc::modff:
1043 case LibFunc::modfl:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001044 if (FTy->getNumParams() < 2 ||
1045 !FTy->getParamType(1)->isPointerTy())
1046 return false;
1047 setDoesNotThrow(F);
1048 setDoesNotCapture(F, 2);
1049 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001050 case LibFunc::memcpy:
1051 case LibFunc::memccpy:
1052 case LibFunc::memmove:
1053 if (FTy->getNumParams() < 2 ||
1054 !FTy->getParamType(1)->isPointerTy())
1055 return false;
1056 setDoesNotThrow(F);
1057 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001058 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001059 break;
1060 case LibFunc::memalign:
1061 if (!FTy->getReturnType()->isPointerTy())
1062 return false;
1063 setDoesNotAlias(F, 0);
1064 break;
1065 case LibFunc::mkdir:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001066 if (FTy->getNumParams() == 0 ||
1067 !FTy->getParamType(0)->isPointerTy())
1068 return false;
1069 setDoesNotThrow(F);
1070 setDoesNotCapture(F, 1);
1071 setOnlyReadsMemory(F, 1);
1072 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001073 case LibFunc::mktime:
1074 if (FTy->getNumParams() == 0 ||
1075 !FTy->getParamType(0)->isPointerTy())
1076 return false;
1077 setDoesNotThrow(F);
1078 setDoesNotCapture(F, 1);
1079 break;
1080 case LibFunc::realloc:
1081 if (FTy->getNumParams() != 2 ||
1082 !FTy->getParamType(0)->isPointerTy() ||
1083 !FTy->getReturnType()->isPointerTy())
1084 return false;
1085 setDoesNotThrow(F);
1086 setDoesNotAlias(F, 0);
1087 setDoesNotCapture(F, 1);
1088 break;
1089 case LibFunc::read:
1090 if (FTy->getNumParams() != 3 ||
1091 !FTy->getParamType(1)->isPointerTy())
1092 return false;
1093 // May throw; "read" is a valid pthread cancellation point.
1094 setDoesNotCapture(F, 2);
1095 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001096 case LibFunc::rewind:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001097 if (FTy->getNumParams() < 1 ||
1098 !FTy->getParamType(0)->isPointerTy())
1099 return false;
1100 setDoesNotThrow(F);
1101 setDoesNotCapture(F, 1);
1102 break;
1103 case LibFunc::rmdir:
Meador Inge6b6a1612013-03-21 00:55:59 +00001104 case LibFunc::remove:
1105 case LibFunc::realpath:
1106 if (FTy->getNumParams() < 1 ||
1107 !FTy->getParamType(0)->isPointerTy())
1108 return false;
1109 setDoesNotThrow(F);
1110 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001111 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001112 break;
1113 case LibFunc::rename:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001114 if (FTy->getNumParams() < 2 ||
1115 !FTy->getParamType(0)->isPointerTy() ||
1116 !FTy->getParamType(1)->isPointerTy())
1117 return false;
1118 setDoesNotThrow(F);
1119 setDoesNotCapture(F, 1);
1120 setDoesNotCapture(F, 2);
1121 setOnlyReadsMemory(F, 1);
1122 setOnlyReadsMemory(F, 2);
1123 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001124 case LibFunc::readlink:
1125 if (FTy->getNumParams() < 2 ||
1126 !FTy->getParamType(0)->isPointerTy() ||
1127 !FTy->getParamType(1)->isPointerTy())
1128 return false;
1129 setDoesNotThrow(F);
1130 setDoesNotCapture(F, 1);
1131 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001132 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001133 break;
1134 case LibFunc::write:
1135 if (FTy->getNumParams() != 3 || !FTy->getParamType(1)->isPointerTy())
1136 return false;
1137 // May throw; "write" is a valid pthread cancellation point.
1138 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001139 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001140 break;
1141 case LibFunc::bcopy:
1142 if (FTy->getNumParams() != 3 ||
1143 !FTy->getParamType(0)->isPointerTy() ||
1144 !FTy->getParamType(1)->isPointerTy())
1145 return false;
1146 setDoesNotThrow(F);
1147 setDoesNotCapture(F, 1);
1148 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001149 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001150 break;
1151 case LibFunc::bcmp:
1152 if (FTy->getNumParams() != 3 ||
1153 !FTy->getParamType(0)->isPointerTy() ||
1154 !FTy->getParamType(1)->isPointerTy())
1155 return false;
1156 setDoesNotThrow(F);
1157 setOnlyReadsMemory(F);
1158 setDoesNotCapture(F, 1);
1159 setDoesNotCapture(F, 2);
1160 break;
1161 case LibFunc::bzero:
1162 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1163 return false;
1164 setDoesNotThrow(F);
1165 setDoesNotCapture(F, 1);
1166 break;
1167 case LibFunc::calloc:
1168 if (FTy->getNumParams() != 2 ||
1169 !FTy->getReturnType()->isPointerTy())
1170 return false;
1171 setDoesNotThrow(F);
1172 setDoesNotAlias(F, 0);
1173 break;
1174 case LibFunc::chmod:
1175 case LibFunc::chown:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001176 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1177 return false;
1178 setDoesNotThrow(F);
1179 setDoesNotCapture(F, 1);
1180 setOnlyReadsMemory(F, 1);
1181 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001182 case LibFunc::ctermid:
1183 case LibFunc::clearerr:
1184 case LibFunc::closedir:
1185 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1186 return false;
1187 setDoesNotThrow(F);
1188 setDoesNotCapture(F, 1);
1189 break;
1190 case LibFunc::atoi:
1191 case LibFunc::atol:
1192 case LibFunc::atof:
1193 case LibFunc::atoll:
1194 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1195 return false;
1196 setDoesNotThrow(F);
1197 setOnlyReadsMemory(F);
1198 setDoesNotCapture(F, 1);
1199 break;
1200 case LibFunc::access:
1201 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1202 return false;
1203 setDoesNotThrow(F);
1204 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001205 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001206 break;
1207 case LibFunc::fopen:
1208 if (FTy->getNumParams() != 2 ||
1209 !FTy->getReturnType()->isPointerTy() ||
1210 !FTy->getParamType(0)->isPointerTy() ||
1211 !FTy->getParamType(1)->isPointerTy())
1212 return false;
1213 setDoesNotThrow(F);
1214 setDoesNotAlias(F, 0);
1215 setDoesNotCapture(F, 1);
1216 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001217 setOnlyReadsMemory(F, 1);
1218 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001219 break;
1220 case LibFunc::fdopen:
1221 if (FTy->getNumParams() != 2 ||
1222 !FTy->getReturnType()->isPointerTy() ||
1223 !FTy->getParamType(1)->isPointerTy())
1224 return false;
1225 setDoesNotThrow(F);
1226 setDoesNotAlias(F, 0);
1227 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001228 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001229 break;
1230 case LibFunc::feof:
1231 case LibFunc::free:
1232 case LibFunc::fseek:
1233 case LibFunc::ftell:
1234 case LibFunc::fgetc:
1235 case LibFunc::fseeko:
1236 case LibFunc::ftello:
1237 case LibFunc::fileno:
1238 case LibFunc::fflush:
1239 case LibFunc::fclose:
1240 case LibFunc::fsetpos:
1241 case LibFunc::flockfile:
1242 case LibFunc::funlockfile:
1243 case LibFunc::ftrylockfile:
1244 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1245 return false;
1246 setDoesNotThrow(F);
1247 setDoesNotCapture(F, 1);
1248 break;
1249 case LibFunc::ferror:
1250 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1251 return false;
1252 setDoesNotThrow(F);
1253 setDoesNotCapture(F, 1);
1254 setOnlyReadsMemory(F);
1255 break;
1256 case LibFunc::fputc:
1257 case LibFunc::fstat:
1258 case LibFunc::frexp:
1259 case LibFunc::frexpf:
1260 case LibFunc::frexpl:
1261 case LibFunc::fstatvfs:
1262 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1263 return false;
1264 setDoesNotThrow(F);
1265 setDoesNotCapture(F, 2);
1266 break;
1267 case LibFunc::fgets:
1268 if (FTy->getNumParams() != 3 ||
1269 !FTy->getParamType(0)->isPointerTy() ||
1270 !FTy->getParamType(2)->isPointerTy())
1271 return false;
1272 setDoesNotThrow(F);
1273 setDoesNotCapture(F, 3);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001274 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001275 case LibFunc::fread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001276 if (FTy->getNumParams() != 4 ||
1277 !FTy->getParamType(0)->isPointerTy() ||
1278 !FTy->getParamType(3)->isPointerTy())
1279 return false;
1280 setDoesNotThrow(F);
1281 setDoesNotCapture(F, 1);
1282 setDoesNotCapture(F, 4);
1283 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001284 case LibFunc::fwrite:
1285 if (FTy->getNumParams() != 4 ||
1286 !FTy->getParamType(0)->isPointerTy() ||
1287 !FTy->getParamType(3)->isPointerTy())
1288 return false;
1289 setDoesNotThrow(F);
1290 setDoesNotCapture(F, 1);
1291 setDoesNotCapture(F, 4);
Nick Lewycky26fcc512013-07-02 05:02:56 +00001292 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001293 case LibFunc::fputs:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001294 if (FTy->getNumParams() < 2 ||
1295 !FTy->getParamType(0)->isPointerTy() ||
1296 !FTy->getParamType(1)->isPointerTy())
1297 return false;
1298 setDoesNotThrow(F);
1299 setDoesNotCapture(F, 1);
1300 setDoesNotCapture(F, 2);
1301 setOnlyReadsMemory(F, 1);
1302 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001303 case LibFunc::fscanf:
1304 case LibFunc::fprintf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001305 if (FTy->getNumParams() < 2 ||
1306 !FTy->getParamType(0)->isPointerTy() ||
1307 !FTy->getParamType(1)->isPointerTy())
1308 return false;
1309 setDoesNotThrow(F);
1310 setDoesNotCapture(F, 1);
1311 setDoesNotCapture(F, 2);
1312 setOnlyReadsMemory(F, 2);
1313 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001314 case LibFunc::fgetpos:
1315 if (FTy->getNumParams() < 2 ||
1316 !FTy->getParamType(0)->isPointerTy() ||
1317 !FTy->getParamType(1)->isPointerTy())
1318 return false;
1319 setDoesNotThrow(F);
1320 setDoesNotCapture(F, 1);
1321 setDoesNotCapture(F, 2);
1322 break;
1323 case LibFunc::getc:
1324 case LibFunc::getlogin_r:
1325 case LibFunc::getc_unlocked:
1326 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1327 return false;
1328 setDoesNotThrow(F);
1329 setDoesNotCapture(F, 1);
1330 break;
1331 case LibFunc::getenv:
1332 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1333 return false;
1334 setDoesNotThrow(F);
1335 setOnlyReadsMemory(F);
1336 setDoesNotCapture(F, 1);
1337 break;
1338 case LibFunc::gets:
1339 case LibFunc::getchar:
1340 setDoesNotThrow(F);
1341 break;
1342 case LibFunc::getitimer:
1343 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1344 return false;
1345 setDoesNotThrow(F);
1346 setDoesNotCapture(F, 2);
1347 break;
1348 case LibFunc::getpwnam:
1349 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1350 return false;
1351 setDoesNotThrow(F);
1352 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001353 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001354 break;
1355 case LibFunc::ungetc:
1356 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1357 return false;
1358 setDoesNotThrow(F);
1359 setDoesNotCapture(F, 2);
1360 break;
1361 case LibFunc::uname:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001362 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1363 return false;
1364 setDoesNotThrow(F);
1365 setDoesNotCapture(F, 1);
1366 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001367 case LibFunc::unlink:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001368 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1369 return false;
1370 setDoesNotThrow(F);
1371 setDoesNotCapture(F, 1);
Nick Lewyckycff2cf82013-07-06 00:59:28 +00001372 setOnlyReadsMemory(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001373 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001374 case LibFunc::unsetenv:
1375 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1376 return false;
1377 setDoesNotThrow(F);
1378 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001379 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001380 break;
1381 case LibFunc::utime:
1382 case LibFunc::utimes:
1383 if (FTy->getNumParams() != 2 ||
1384 !FTy->getParamType(0)->isPointerTy() ||
1385 !FTy->getParamType(1)->isPointerTy())
1386 return false;
1387 setDoesNotThrow(F);
1388 setDoesNotCapture(F, 1);
1389 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001390 setOnlyReadsMemory(F, 1);
1391 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001392 break;
1393 case LibFunc::putc:
1394 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1395 return false;
1396 setDoesNotThrow(F);
1397 setDoesNotCapture(F, 2);
1398 break;
1399 case LibFunc::puts:
1400 case LibFunc::printf:
1401 case LibFunc::perror:
1402 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1403 return false;
1404 setDoesNotThrow(F);
1405 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001406 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001407 break;
1408 case LibFunc::pread:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001409 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1410 return false;
1411 // May throw; "pread" is a valid pthread cancellation point.
1412 setDoesNotCapture(F, 2);
1413 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001414 case LibFunc::pwrite:
1415 if (FTy->getNumParams() != 4 || !FTy->getParamType(1)->isPointerTy())
1416 return false;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001417 // May throw; "pwrite" is a valid pthread cancellation point.
Meador Inge6b6a1612013-03-21 00:55:59 +00001418 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001419 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001420 break;
1421 case LibFunc::putchar:
1422 setDoesNotThrow(F);
1423 break;
1424 case LibFunc::popen:
1425 if (FTy->getNumParams() != 2 ||
1426 !FTy->getReturnType()->isPointerTy() ||
1427 !FTy->getParamType(0)->isPointerTy() ||
1428 !FTy->getParamType(1)->isPointerTy())
1429 return false;
1430 setDoesNotThrow(F);
1431 setDoesNotAlias(F, 0);
1432 setDoesNotCapture(F, 1);
1433 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001434 setOnlyReadsMemory(F, 1);
1435 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001436 break;
1437 case LibFunc::pclose:
1438 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1439 return false;
1440 setDoesNotThrow(F);
1441 setDoesNotCapture(F, 1);
1442 break;
1443 case LibFunc::vscanf:
1444 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1445 return false;
1446 setDoesNotThrow(F);
1447 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001448 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001449 break;
1450 case LibFunc::vsscanf:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001451 if (FTy->getNumParams() != 3 ||
1452 !FTy->getParamType(1)->isPointerTy() ||
1453 !FTy->getParamType(2)->isPointerTy())
1454 return false;
1455 setDoesNotThrow(F);
1456 setDoesNotCapture(F, 1);
1457 setDoesNotCapture(F, 2);
1458 setOnlyReadsMemory(F, 1);
1459 setOnlyReadsMemory(F, 2);
1460 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001461 case LibFunc::vfscanf:
1462 if (FTy->getNumParams() != 3 ||
1463 !FTy->getParamType(1)->isPointerTy() ||
1464 !FTy->getParamType(2)->isPointerTy())
1465 return false;
1466 setDoesNotThrow(F);
1467 setDoesNotCapture(F, 1);
1468 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001469 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001470 break;
1471 case LibFunc::valloc:
1472 if (!FTy->getReturnType()->isPointerTy())
1473 return false;
1474 setDoesNotThrow(F);
1475 setDoesNotAlias(F, 0);
1476 break;
1477 case LibFunc::vprintf:
1478 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy())
1479 return false;
1480 setDoesNotThrow(F);
1481 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001482 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001483 break;
1484 case LibFunc::vfprintf:
1485 case LibFunc::vsprintf:
1486 if (FTy->getNumParams() != 3 ||
1487 !FTy->getParamType(0)->isPointerTy() ||
1488 !FTy->getParamType(1)->isPointerTy())
1489 return false;
1490 setDoesNotThrow(F);
1491 setDoesNotCapture(F, 1);
1492 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001493 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001494 break;
1495 case LibFunc::vsnprintf:
1496 if (FTy->getNumParams() != 4 ||
1497 !FTy->getParamType(0)->isPointerTy() ||
1498 !FTy->getParamType(2)->isPointerTy())
1499 return false;
1500 setDoesNotThrow(F);
1501 setDoesNotCapture(F, 1);
1502 setDoesNotCapture(F, 3);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001503 setOnlyReadsMemory(F, 3);
Meador Inge6b6a1612013-03-21 00:55:59 +00001504 break;
1505 case LibFunc::open:
1506 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1507 return false;
1508 // May throw; "open" is a valid pthread cancellation point.
1509 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001510 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001511 break;
1512 case LibFunc::opendir:
1513 if (FTy->getNumParams() != 1 ||
1514 !FTy->getReturnType()->isPointerTy() ||
1515 !FTy->getParamType(0)->isPointerTy())
1516 return false;
1517 setDoesNotThrow(F);
1518 setDoesNotAlias(F, 0);
1519 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001520 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001521 break;
1522 case LibFunc::tmpfile:
1523 if (!FTy->getReturnType()->isPointerTy())
1524 return false;
1525 setDoesNotThrow(F);
1526 setDoesNotAlias(F, 0);
1527 break;
1528 case LibFunc::times:
1529 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1530 return false;
1531 setDoesNotThrow(F);
1532 setDoesNotCapture(F, 1);
1533 break;
1534 case LibFunc::htonl:
1535 case LibFunc::htons:
1536 case LibFunc::ntohl:
1537 case LibFunc::ntohs:
1538 setDoesNotThrow(F);
1539 setDoesNotAccessMemory(F);
1540 break;
1541 case LibFunc::lstat:
1542 if (FTy->getNumParams() != 2 ||
1543 !FTy->getParamType(0)->isPointerTy() ||
1544 !FTy->getParamType(1)->isPointerTy())
1545 return false;
1546 setDoesNotThrow(F);
1547 setDoesNotCapture(F, 1);
1548 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001549 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001550 break;
1551 case LibFunc::lchown:
1552 if (FTy->getNumParams() != 3 || !FTy->getParamType(0)->isPointerTy())
1553 return false;
1554 setDoesNotThrow(F);
1555 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001556 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001557 break;
1558 case LibFunc::qsort:
1559 if (FTy->getNumParams() != 4 || !FTy->getParamType(3)->isPointerTy())
1560 return false;
1561 // May throw; places call through function pointer.
1562 setDoesNotCapture(F, 4);
1563 break;
1564 case LibFunc::dunder_strdup:
1565 case LibFunc::dunder_strndup:
1566 if (FTy->getNumParams() < 1 ||
1567 !FTy->getReturnType()->isPointerTy() ||
1568 !FTy->getParamType(0)->isPointerTy())
1569 return false;
1570 setDoesNotThrow(F);
1571 setDoesNotAlias(F, 0);
1572 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001573 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001574 break;
1575 case LibFunc::dunder_strtok_r:
1576 if (FTy->getNumParams() != 3 ||
1577 !FTy->getParamType(1)->isPointerTy())
1578 return false;
1579 setDoesNotThrow(F);
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::under_IO_getc:
1584 if (FTy->getNumParams() != 1 || !FTy->getParamType(0)->isPointerTy())
1585 return false;
1586 setDoesNotThrow(F);
1587 setDoesNotCapture(F, 1);
1588 break;
1589 case LibFunc::under_IO_putc:
1590 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1591 return false;
1592 setDoesNotThrow(F);
1593 setDoesNotCapture(F, 2);
1594 break;
1595 case LibFunc::dunder_isoc99_scanf:
1596 if (FTy->getNumParams() < 1 ||
1597 !FTy->getParamType(0)->isPointerTy())
1598 return false;
1599 setDoesNotThrow(F);
1600 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001601 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001602 break;
1603 case LibFunc::stat64:
1604 case LibFunc::lstat64:
1605 case LibFunc::statvfs64:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001606 if (FTy->getNumParams() < 1 ||
1607 !FTy->getParamType(0)->isPointerTy() ||
1608 !FTy->getParamType(1)->isPointerTy())
1609 return false;
1610 setDoesNotThrow(F);
1611 setDoesNotCapture(F, 1);
1612 setDoesNotCapture(F, 2);
1613 setOnlyReadsMemory(F, 1);
1614 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001615 case LibFunc::dunder_isoc99_sscanf:
1616 if (FTy->getNumParams() < 1 ||
1617 !FTy->getParamType(0)->isPointerTy() ||
1618 !FTy->getParamType(1)->isPointerTy())
1619 return false;
1620 setDoesNotThrow(F);
1621 setDoesNotCapture(F, 1);
1622 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001623 setOnlyReadsMemory(F, 1);
1624 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001625 break;
1626 case LibFunc::fopen64:
1627 if (FTy->getNumParams() != 2 ||
1628 !FTy->getReturnType()->isPointerTy() ||
1629 !FTy->getParamType(0)->isPointerTy() ||
1630 !FTy->getParamType(1)->isPointerTy())
1631 return false;
1632 setDoesNotThrow(F);
1633 setDoesNotAlias(F, 0);
1634 setDoesNotCapture(F, 1);
1635 setDoesNotCapture(F, 2);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001636 setOnlyReadsMemory(F, 1);
1637 setOnlyReadsMemory(F, 2);
Meador Inge6b6a1612013-03-21 00:55:59 +00001638 break;
1639 case LibFunc::fseeko64:
1640 case LibFunc::ftello64:
1641 if (FTy->getNumParams() == 0 || !FTy->getParamType(0)->isPointerTy())
1642 return false;
1643 setDoesNotThrow(F);
1644 setDoesNotCapture(F, 1);
1645 break;
1646 case LibFunc::tmpfile64:
1647 if (!FTy->getReturnType()->isPointerTy())
1648 return false;
1649 setDoesNotThrow(F);
1650 setDoesNotAlias(F, 0);
1651 break;
1652 case LibFunc::fstat64:
1653 case LibFunc::fstatvfs64:
1654 if (FTy->getNumParams() != 2 || !FTy->getParamType(1)->isPointerTy())
1655 return false;
1656 setDoesNotThrow(F);
1657 setDoesNotCapture(F, 2);
1658 break;
1659 case LibFunc::open64:
1660 if (FTy->getNumParams() < 2 || !FTy->getParamType(0)->isPointerTy())
1661 return false;
1662 // May throw; "open" is a valid pthread cancellation point.
1663 setDoesNotCapture(F, 1);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001664 setOnlyReadsMemory(F, 1);
Meador Inge6b6a1612013-03-21 00:55:59 +00001665 break;
Michael Gottesman2db11162013-07-03 04:00:54 +00001666 case LibFunc::gettimeofday:
1667 if (FTy->getNumParams() != 2 || !FTy->getParamType(0)->isPointerTy() ||
1668 !FTy->getParamType(1)->isPointerTy())
1669 return false;
1670 // Currently some platforms have the restrict keyword on the arguments to
1671 // gettimeofday. To be conservative, do not add noalias to gettimeofday's
1672 // arguments.
1673 setDoesNotThrow(F);
1674 setDoesNotCapture(F, 1);
1675 setDoesNotCapture(F, 2);
Rafael Espindola5e66a7e2014-03-30 03:26:17 +00001676 break;
Meador Inge6b6a1612013-03-21 00:55:59 +00001677 default:
1678 // Didn't mark any attributes.
1679 return false;
1680 }
1681
1682 return true;
1683}
1684
1685/// annotateLibraryCalls - Adds attributes to well-known standard library
1686/// call declarations.
1687bool FunctionAttrs::annotateLibraryCalls(const CallGraphSCC &SCC) {
1688 bool MadeChange = false;
1689
1690 // Check each function in turn annotating well-known library function
1691 // declarations with attributes.
1692 for (CallGraphSCC::iterator I = SCC.begin(), E = SCC.end(); I != E; ++I) {
1693 Function *F = (*I)->getFunction();
1694
Craig Topperf40110f2014-04-25 05:29:35 +00001695 if (F && F->isDeclaration())
Meador Inge6b6a1612013-03-21 00:55:59 +00001696 MadeChange |= inferPrototypeAttributes(*F);
1697 }
1698
1699 return MadeChange;
1700}
1701
Chris Lattner4422d312010-04-16 22:42:17 +00001702bool FunctionAttrs::runOnSCC(CallGraphSCC &SCC) {
Dan Gohman86449d72010-11-08 16:10:15 +00001703 AA = &getAnalysis<AliasAnalysis>();
Chandler Carruthb98f63d2015-01-15 10:41:28 +00001704 TLI = &getAnalysis<TargetLibraryInfoWrapperPass>().getTLI();
Dan Gohman86449d72010-11-08 16:10:15 +00001705
Meador Inge6b6a1612013-03-21 00:55:59 +00001706 bool Changed = annotateLibraryCalls(SCC);
1707 Changed |= AddReadAttrs(SCC);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +00001708 Changed |= AddArgumentAttrs(SCC);
Nick Lewyckyfbed86a2009-03-08 06:20:47 +00001709 Changed |= AddNoAliasAttrs(SCC);
Duncan Sands44c8cd92008-12-31 16:14:43 +00001710 return Changed;
1711}