blob: f9850619f96384a1d6dd796d26ffaec686fca434 [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//===----------------------------------------------------------------------===//
Eugene Zelenkof27d1612017-10-19 21:21:30 +00009//
Chandler Carruth1926b702016-01-08 10:55:52 +000010/// \file
11/// This file implements interprocedural passes which walk the
12/// call-graph deducing and/or propagating function attributes.
Eugene Zelenkof27d1612017-10-19 21:21:30 +000013//
Duncan Sands44c8cd92008-12-31 16:14:43 +000014//===----------------------------------------------------------------------===//
15
Chandler Carruth9c4ed172016-02-18 11:03:11 +000016#include "llvm/Transforms/IPO/FunctionAttrs.h"
Nick Lewycky4c378a42011-12-28 23:24:21 +000017#include "llvm/ADT/SCCIterator.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000018#include "llvm/ADT/STLExtras.h"
Benjamin Kramer15591272012-10-31 13:45:49 +000019#include "llvm/ADT/SetVector.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000020#include "llvm/ADT/SmallPtrSet.h"
Duncan Sandsb193a372009-01-02 11:54:37 +000021#include "llvm/ADT/SmallSet.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000022#include "llvm/ADT/SmallVector.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000023#include "llvm/ADT/Statistic.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000024#include "llvm/Analysis/AliasAnalysis.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000025#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000026#include "llvm/Analysis/BasicAliasAnalysis.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000027#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000029#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000030#include "llvm/Analysis/CaptureTracking.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000031#include "llvm/Analysis/LazyCallGraph.h"
32#include "llvm/Analysis/MemoryLocation.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000033#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000034#include "llvm/IR/Argument.h"
35#include "llvm/IR/Attributes.h"
36#include "llvm/IR/BasicBlock.h"
37#include "llvm/IR/CallSite.h"
38#include "llvm/IR/Constant.h"
39#include "llvm/IR/Constants.h"
40#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000041#include "llvm/IR/InstIterator.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000042#include "llvm/IR/InstrTypes.h"
43#include "llvm/IR/Instruction.h"
44#include "llvm/IR/Instructions.h"
45#include "llvm/IR/Metadata.h"
46#include "llvm/IR/PassManager.h"
47#include "llvm/IR/Type.h"
48#include "llvm/IR/Use.h"
49#include "llvm/IR/User.h"
50#include "llvm/IR/Value.h"
51#include "llvm/Pass.h"
52#include "llvm/Support/Casting.h"
53#include "llvm/Support/CommandLine.h"
54#include "llvm/Support/Compiler.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000055#include "llvm/Support/Debug.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000056#include "llvm/Support/ErrorHandling.h"
Hans Wennborg043bf5b2015-08-31 21:19:18 +000057#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000058#include "llvm/Transforms/IPO.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000059#include <cassert>
60#include <iterator>
61#include <map>
62#include <vector>
63
Duncan Sands44c8cd92008-12-31 16:14:43 +000064using namespace llvm;
65
Chandler Carruth964daaa2014-04-22 02:55:47 +000066#define DEBUG_TYPE "functionattrs"
67
Duncan Sands44c8cd92008-12-31 16:14:43 +000068STATISTIC(NumReadNone, "Number of functions marked readnone");
69STATISTIC(NumReadOnly, "Number of functions marked readonly");
70STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
David Majnemer5246e0b2016-07-19 18:50:26 +000071STATISTIC(NumReturned, "Number of arguments marked returned");
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000072STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
73STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000074STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Philip Reamesa88caea2015-08-31 19:44:38 +000075STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
James Molloy7e9bdd52015-11-12 10:55:20 +000076STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
Duncan Sands44c8cd92008-12-31 16:14:43 +000077
Sanjay Patel4f742162017-02-13 23:10:51 +000078// FIXME: This is disabled by default to avoid exposing security vulnerabilities
79// in C/C++ code compiled by clang:
80// http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
81static cl::opt<bool> EnableNonnullArgPropagation(
82 "enable-nonnull-arg-prop", cl::Hidden,
83 cl::desc("Try to propagate nonnull argument attributes from callsites to "
84 "caller functions."));
85
Duncan Sands44c8cd92008-12-31 16:14:43 +000086namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +000087
88using SCCNodeSet = SmallSetVector<Function *, 8>;
89
90} // end anonymous namespace
Chandler Carruthc518ebd2015-10-29 18:29:15 +000091
Peter Collingbournec45f7f32017-02-14 00:28:13 +000092/// Returns the memory access attribute for function F using AAR for AA results,
93/// where SCCNodes is the current SCC.
94///
95/// If ThisBody is true, this function may examine the function body and will
96/// return a result pertaining to this copy of the function. If it is false, the
97/// result will be based only on AA results for the function declaration; it
98/// will be assumed that some other (perhaps less optimized) version of the
99/// function may be selected at link time.
100static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
101 AAResults &AAR,
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000102 const SCCNodeSet &SCCNodes) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000103 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
104 if (MRB == FMRB_DoesNotAccessMemory)
105 // Already perfect!
106 return MAK_ReadNone;
107
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000108 if (!ThisBody) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000109 if (AliasAnalysis::onlyReadsMemory(MRB))
110 return MAK_ReadOnly;
111
112 // Conservatively assume it writes to memory.
113 return MAK_MayWrite;
114 }
115
116 // Scan the function body for instructions that may read or write memory.
117 bool ReadsMemory = false;
118 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
119 Instruction *I = &*II;
120
121 // Some instructions can be ignored even if they read or write memory.
122 // Detect these now, skipping to the next instruction if one is found.
123 CallSite CS(cast<Value>(I));
124 if (CS) {
Sanjoy Das10c8a042016-02-09 18:40:40 +0000125 // Ignore calls to functions in the same SCC, as long as the call sites
126 // don't have operand bundles. Calls with operand bundles are allowed to
127 // have memory effects not described by the memory effects of the call
128 // target.
129 if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
130 SCCNodes.count(CS.getCalledFunction()))
Chandler Carruth7542d372015-09-21 17:39:41 +0000131 continue;
132 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
Chandler Carruth7542d372015-09-21 17:39:41 +0000133
Chandler Carruth69798fb2015-10-27 01:41:43 +0000134 // If the call doesn't access memory, we're done.
135 if (!(MRB & MRI_ModRef))
136 continue;
137
138 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
139 // The call could access any memory. If that includes writes, give up.
140 if (MRB & MRI_Mod)
141 return MAK_MayWrite;
142 // If it reads, note it.
143 if (MRB & MRI_Ref)
144 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000145 continue;
146 }
Chandler Carruth69798fb2015-10-27 01:41:43 +0000147
148 // Check whether all pointer arguments point to local memory, and
149 // ignore calls that only access local memory.
150 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
151 CI != CE; ++CI) {
152 Value *Arg = *CI;
Elena Demikhovsky3ec9e152015-11-17 19:30:51 +0000153 if (!Arg->getType()->isPtrOrPtrVectorTy())
Chandler Carruth69798fb2015-10-27 01:41:43 +0000154 continue;
155
156 AAMDNodes AAInfo;
157 I->getAAMetadata(AAInfo);
158 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
159
160 // Skip accesses to local or constant memory as they don't impact the
161 // externally visible mod/ref behavior.
162 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
163 continue;
164
165 if (MRB & MRI_Mod)
166 // Writes non-local memory. Give up.
167 return MAK_MayWrite;
168 if (MRB & MRI_Ref)
169 // Ok, it reads non-local memory.
170 ReadsMemory = true;
171 }
Chandler Carruth7542d372015-09-21 17:39:41 +0000172 continue;
173 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
174 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
175 if (!LI->isVolatile()) {
176 MemoryLocation Loc = MemoryLocation::get(LI);
177 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
178 continue;
179 }
180 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
181 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
182 if (!SI->isVolatile()) {
183 MemoryLocation Loc = MemoryLocation::get(SI);
184 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
185 continue;
186 }
187 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
188 // Ignore vaargs on local memory.
189 MemoryLocation Loc = MemoryLocation::get(VI);
190 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
191 continue;
192 }
193
194 // Any remaining instructions need to be taken seriously! Check if they
195 // read or write memory.
196 if (I->mayWriteToMemory())
197 // Writes memory. Just give up.
198 return MAK_MayWrite;
199
200 // If this instruction may read memory, remember that.
201 ReadsMemory |= I->mayReadFromMemory();
202 }
203
204 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
205}
206
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000207MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
208 AAResults &AAR) {
209 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
210}
211
Chandler Carrutha632fb92015-09-13 06:57:25 +0000212/// Deduce readonly/readnone attributes for the SCC.
Chandler Carrutha8125352015-10-30 16:48:08 +0000213template <typename AARGetterT>
Peter Collingbournecea1e4e2017-02-09 23:11:52 +0000214static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000215 // Check if any of the functions in the SCC read or write memory. If they
216 // write memory then they can't be marked readnone or readonly.
217 bool ReadsMemory = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000218 for (Function *F : SCCNodes) {
Chandler Carrutha8125352015-10-30 16:48:08 +0000219 // Call the callable parameter to look up AA results for this function.
220 AAResults &AAR = AARGetter(*F);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000221
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000222 // Non-exact function definitions may not be selected at link time, and an
223 // alternative version that writes to memory may be selected. See the
224 // comment on GlobalValue::isDefinitionExact for more details.
225 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
226 AAR, SCCNodes)) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000227 case MAK_MayWrite:
228 return false;
229 case MAK_ReadOnly:
Duncan Sands44c8cd92008-12-31 16:14:43 +0000230 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000231 break;
232 case MAK_ReadNone:
233 // Nothing to do!
234 break;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000235 }
236 }
237
238 // Success! Functions in this SCC do not access memory, or only read memory.
239 // Give them the appropriate attribute.
240 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000241 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000242 if (F->doesNotAccessMemory())
243 // Already perfect!
244 continue;
245
246 if (F->onlyReadsMemory() && ReadsMemory)
247 // No change.
248 continue;
249
250 MadeChange = true;
251
252 // Clear out any existing attributes.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000253 F->removeFnAttr(Attribute::ReadOnly);
254 F->removeFnAttr(Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000255
256 // Add in the new attribute.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000257 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000258
259 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000260 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000261 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000262 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000263 }
264
265 return MadeChange;
266}
267
Nick Lewycky4c378a42011-12-28 23:24:21 +0000268namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000269
Chandler Carrutha632fb92015-09-13 06:57:25 +0000270/// For a given pointer Argument, this retains a list of Arguments of functions
271/// in the same SCC that the pointer data flows into. We use this to build an
272/// SCC of the arguments.
Chandler Carruth63559d72015-09-13 06:47:20 +0000273struct ArgumentGraphNode {
274 Argument *Definition;
275 SmallVector<ArgumentGraphNode *, 4> Uses;
276};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000277
Chandler Carruth63559d72015-09-13 06:47:20 +0000278class ArgumentGraph {
279 // We store pointers to ArgumentGraphNode objects, so it's important that
280 // that they not move around upon insert.
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000281 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000282
Chandler Carruth63559d72015-09-13 06:47:20 +0000283 ArgumentMapTy ArgumentMap;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000284
Chandler Carruth63559d72015-09-13 06:47:20 +0000285 // There is no root node for the argument graph, in fact:
286 // void f(int *x, int *y) { if (...) f(x, y); }
287 // is an example where the graph is disconnected. The SCCIterator requires a
288 // single entry point, so we maintain a fake ("synthetic") root node that
289 // uses every node. Because the graph is directed and nothing points into
290 // the root, it will not participate in any SCCs (except for its own).
291 ArgumentGraphNode SyntheticRoot;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000292
Chandler Carruth63559d72015-09-13 06:47:20 +0000293public:
294 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000295
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000296 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000297
Chandler Carruth63559d72015-09-13 06:47:20 +0000298 iterator begin() { return SyntheticRoot.Uses.begin(); }
299 iterator end() { return SyntheticRoot.Uses.end(); }
300 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000301
Chandler Carruth63559d72015-09-13 06:47:20 +0000302 ArgumentGraphNode *operator[](Argument *A) {
303 ArgumentGraphNode &Node = ArgumentMap[A];
304 Node.Definition = A;
305 SyntheticRoot.Uses.push_back(&Node);
306 return &Node;
307 }
308};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000309
Chandler Carrutha632fb92015-09-13 06:57:25 +0000310/// This tracker checks whether callees are in the SCC, and if so it does not
311/// consider that a capture, instead adding it to the "Uses" list and
312/// continuing with the analysis.
Chandler Carruth63559d72015-09-13 06:47:20 +0000313struct ArgumentUsesTracker : public CaptureTracker {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000314 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000315
Chandler Carruth63559d72015-09-13 06:47:20 +0000316 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000317
Chandler Carruth63559d72015-09-13 06:47:20 +0000318 bool captured(const Use *U) override {
319 CallSite CS(U->getUser());
320 if (!CS.getInstruction()) {
321 Captured = true;
322 return true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000323 }
324
Chandler Carruth63559d72015-09-13 06:47:20 +0000325 Function *F = CS.getCalledFunction();
Sanjoy Das5ce32722016-04-08 00:48:30 +0000326 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000327 Captured = true;
328 return true;
329 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000330
Sanjoy Das98bfe262015-11-05 03:04:40 +0000331 // Note: the callee and the two successor blocks *follow* the argument
332 // operands. This means there is no need to adjust UseIndex to account for
333 // these.
334
335 unsigned UseIndex =
336 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
337
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000338 assert(UseIndex < CS.data_operands_size() &&
339 "Indirect function calls should have been filtered above!");
340
341 if (UseIndex >= CS.getNumArgOperands()) {
342 // Data operand, but not a argument operand -- must be a bundle operand
343 assert(CS.hasOperandBundles() && "Must be!");
344
345 // CaptureTracking told us that we're being captured by an operand bundle
346 // use. In this case it does not matter if the callee is within our SCC
347 // or not -- we've been captured in some unknown way, and we have to be
348 // conservative.
349 Captured = true;
350 return true;
351 }
352
Sanjoy Das98bfe262015-11-05 03:04:40 +0000353 if (UseIndex >= F->arg_size()) {
354 assert(F->isVarArg() && "More params than args in non-varargs call");
355 Captured = true;
356 return true;
Chandler Carruth63559d72015-09-13 06:47:20 +0000357 }
Sanjoy Das98bfe262015-11-05 03:04:40 +0000358
Duncan P. N. Exon Smith83c4b682015-11-07 00:01:16 +0000359 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
Chandler Carruth63559d72015-09-13 06:47:20 +0000360 return false;
361 }
362
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000363 // True only if certainly captured (used outside our SCC).
364 bool Captured = false;
365
366 // Uses within our SCC.
367 SmallVector<Argument *, 4> Uses;
Chandler Carruth63559d72015-09-13 06:47:20 +0000368
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000369 const SCCNodeSet &SCCNodes;
Chandler Carruth63559d72015-09-13 06:47:20 +0000370};
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000371
372} // end anonymous namespace
Nick Lewycky4c378a42011-12-28 23:24:21 +0000373
374namespace llvm {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000375
Chandler Carruth63559d72015-09-13 06:47:20 +0000376template <> struct GraphTraits<ArgumentGraphNode *> {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000377 using NodeRef = ArgumentGraphNode *;
378 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000379
Tim Shen48f814e2016-08-31 16:48:13 +0000380 static NodeRef getEntryNode(NodeRef A) { return A; }
381 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
382 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
Chandler Carruth63559d72015-09-13 06:47:20 +0000383};
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000384
Chandler Carruth63559d72015-09-13 06:47:20 +0000385template <>
386struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
Tim Shenf2187ed2016-08-22 21:09:30 +0000387 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000388
Chandler Carruth63559d72015-09-13 06:47:20 +0000389 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
390 return AG->begin();
391 }
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000392
Chandler Carruth63559d72015-09-13 06:47:20 +0000393 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
394};
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000395
396} // end namespace llvm
Nick Lewycky4c378a42011-12-28 23:24:21 +0000397
Chandler Carrutha632fb92015-09-13 06:57:25 +0000398/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000399static Attribute::AttrKind
400determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000401 const SmallPtrSet<Argument *, 8> &SCCNodes) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000402 SmallVector<Use *, 32> Worklist;
403 SmallSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000404
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000405 // inalloca arguments are always clobbered by the call.
406 if (A->hasInAllocaAttr())
407 return Attribute::None;
408
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000409 bool IsRead = false;
410 // We don't need to track IsWritten. If A is written to, return immediately.
411
Chandler Carruthcdf47882014-03-09 03:16:01 +0000412 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000413 Visited.insert(&U);
414 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000415 }
416
417 while (!Worklist.empty()) {
418 Use *U = Worklist.pop_back_val();
419 Instruction *I = cast<Instruction>(U->getUser());
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000420
421 switch (I->getOpcode()) {
422 case Instruction::BitCast:
423 case Instruction::GetElementPtr:
424 case Instruction::PHI:
425 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000426 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000427 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000428 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000429 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000430 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000431 break;
432
433 case Instruction::Call:
434 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000435 bool Captures = true;
436
437 if (I->getType()->isVoidTy())
438 Captures = false;
439
440 auto AddUsersToWorklistIfCapturing = [&] {
441 if (Captures)
442 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000443 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000444 Worklist.push_back(&UU);
445 };
446
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000447 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000448 if (CS.doesNotAccessMemory()) {
449 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000450 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000451 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000452
453 Function *F = CS.getCalledFunction();
454 if (!F) {
455 if (CS.onlyReadsMemory()) {
456 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000457 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000458 continue;
459 }
460 return Attribute::None;
461 }
462
Sanjoy Das436e2392015-11-07 01:55:53 +0000463 // Note: the callee and the two successor blocks *follow* the argument
464 // operands. This means there is no need to adjust UseIndex to account
465 // for these.
466
467 unsigned UseIndex = std::distance(CS.arg_begin(), U);
468
Sanjoy Dasea1df7f2015-11-07 01:56:07 +0000469 // U cannot be the callee operand use: since we're exploring the
470 // transitive uses of an Argument, having such a use be a callee would
471 // imply the CallSite is an indirect call or invoke; and we'd take the
472 // early exit above.
473 assert(UseIndex < CS.data_operands_size() &&
474 "Data operand use expected!");
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000475
476 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
477
478 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
Sanjoy Das436e2392015-11-07 01:55:53 +0000479 assert(F->isVarArg() && "More params than args in non-varargs call");
480 return Attribute::None;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000481 }
Sanjoy Das436e2392015-11-07 01:55:53 +0000482
Tilmann Scheller925b1932015-11-20 19:17:10 +0000483 Captures &= !CS.doesNotCapture(UseIndex);
484
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000485 // Since the optimizer (by design) cannot see the data flow corresponding
486 // to a operand bundle use, these cannot participate in the optimistic SCC
487 // analysis. Instead, we model the operand bundle uses as arguments in
488 // call to a function external to the SCC.
Duncan P. N. Exon Smith9e3edad2016-08-17 01:23:58 +0000489 if (IsOperandBundleUse ||
490 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000491
492 // The accessors used on CallSite here do the right thing for calls and
493 // invokes with operand bundles.
494
Sanjoy Das436e2392015-11-07 01:55:53 +0000495 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
496 return Attribute::None;
497 if (!CS.doesNotAccessMemory(UseIndex))
498 IsRead = true;
499 }
500
Nick Lewycky59633cb2014-05-30 02:31:27 +0000501 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000502 break;
503 }
504
505 case Instruction::Load:
David Majnemer124bdb72016-05-25 05:53:04 +0000506 // A volatile load has side effects beyond what readonly can be relied
507 // upon.
508 if (cast<LoadInst>(I)->isVolatile())
509 return Attribute::None;
510
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000511 IsRead = true;
512 break;
513
514 case Instruction::ICmp:
515 case Instruction::Ret:
516 break;
517
518 default:
519 return Attribute::None;
520 }
521 }
522
523 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
524}
525
David Majnemer5246e0b2016-07-19 18:50:26 +0000526/// Deduce returned attributes for the SCC.
527static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
528 bool Changed = false;
529
David Majnemer5246e0b2016-07-19 18:50:26 +0000530 // Check each function in turn, determining if an argument is always returned.
531 for (Function *F : SCCNodes) {
532 // We can infer and propagate function attributes only when we know that the
533 // definition we'll get at link time is *exactly* the definition we see now.
534 // For more details, see GlobalValue::mayBeDerefined.
535 if (!F->hasExactDefinition())
536 continue;
537
538 if (F->getReturnType()->isVoidTy())
539 continue;
540
David Majnemerc83044d2016-09-12 16:04:59 +0000541 // There is nothing to do if an argument is already marked as 'returned'.
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000542 if (llvm::any_of(F->args(),
543 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
David Majnemerc83044d2016-09-12 16:04:59 +0000544 continue;
545
David Majnemer5246e0b2016-07-19 18:50:26 +0000546 auto FindRetArg = [&]() -> Value * {
547 Value *RetArg = nullptr;
548 for (BasicBlock &BB : *F)
549 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
550 // Note that stripPointerCasts should look through functions with
551 // returned arguments.
552 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
553 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
554 return nullptr;
555
556 if (!RetArg)
557 RetArg = RetVal;
558 else if (RetArg != RetVal)
559 return nullptr;
560 }
561
562 return RetArg;
563 };
564
565 if (Value *RetArg = FindRetArg()) {
566 auto *A = cast<Argument>(RetArg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000567 A->addAttr(Attribute::Returned);
David Majnemer5246e0b2016-07-19 18:50:26 +0000568 ++NumReturned;
569 Changed = true;
570 }
571 }
572
573 return Changed;
574}
575
Sanjay Patel4f742162017-02-13 23:10:51 +0000576/// If a callsite has arguments that are also arguments to the parent function,
577/// try to propagate attributes from the callsite's arguments to the parent's
578/// arguments. This may be important because inlining can cause information loss
579/// when attribute knowledge disappears with the inlined call.
580static bool addArgumentAttrsFromCallsites(Function &F) {
581 if (!EnableNonnullArgPropagation)
582 return false;
583
584 bool Changed = false;
585
586 // For an argument attribute to transfer from a callsite to the parent, the
587 // call must be guaranteed to execute every time the parent is called.
588 // Conservatively, just check for calls in the entry block that are guaranteed
589 // to execute.
590 // TODO: This could be enhanced by testing if the callsite post-dominates the
591 // entry block or by doing simple forward walks or backward walks to the
592 // callsite.
593 BasicBlock &Entry = F.getEntryBlock();
594 for (Instruction &I : Entry) {
595 if (auto CS = CallSite(&I)) {
596 if (auto *CalledFunc = CS.getCalledFunction()) {
597 for (auto &CSArg : CalledFunc->args()) {
598 if (!CSArg.hasNonNullAttr())
599 continue;
600
601 // If the non-null callsite argument operand is an argument to 'F'
602 // (the caller) and the call is guaranteed to execute, then the value
603 // must be non-null throughout 'F'.
604 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
605 if (FArg && !FArg->hasNonNullAttr()) {
606 FArg->addAttr(Attribute::NonNull);
607 Changed = true;
608 }
609 }
610 }
611 }
612 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
613 break;
614 }
615
616 return Changed;
617}
618
Chandler Carrutha632fb92015-09-13 06:57:25 +0000619/// Deduce nocapture attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000620static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000621 bool Changed = false;
622
Nick Lewycky4c378a42011-12-28 23:24:21 +0000623 ArgumentGraph AG;
624
Duncan Sands44c8cd92008-12-31 16:14:43 +0000625 // Check each function in turn, determining which pointer arguments are not
626 // captured.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000627 for (Function *F : SCCNodes) {
Sanjoy Das5ce32722016-04-08 00:48:30 +0000628 // We can infer and propagate function attributes only when we know that the
629 // definition we'll get at link time is *exactly* the definition we see now.
630 // For more details, see GlobalValue::mayBeDerefined.
631 if (!F->hasExactDefinition())
Duncan Sands44c8cd92008-12-31 16:14:43 +0000632 continue;
633
Sanjay Patel4f742162017-02-13 23:10:51 +0000634 Changed |= addArgumentAttrsFromCallsites(*F);
635
Nick Lewycky4c378a42011-12-28 23:24:21 +0000636 // Functions that are readonly (or readnone) and nounwind and don't return
637 // a value can't capture arguments. Don't analyze them.
638 if (F->onlyReadsMemory() && F->doesNotThrow() &&
639 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000640 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
641 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000642 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000643 A->addAttr(Attribute::NoCapture);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000644 ++NumNoCapture;
645 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000646 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000647 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000648 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000649 }
650
Chandler Carruth63559d72015-09-13 06:47:20 +0000651 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
652 ++A) {
653 if (!A->getType()->isPointerTy())
654 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000655 bool HasNonLocalUses = false;
656 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000657 ArgumentUsesTracker Tracker(SCCNodes);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000658 PointerMayBeCaptured(&*A, &Tracker);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000659 if (!Tracker.Captured) {
660 if (Tracker.Uses.empty()) {
661 // If it's trivially not captured, mark it nocapture now.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000662 A->addAttr(Attribute::NoCapture);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000663 ++NumNoCapture;
664 Changed = true;
665 } else {
666 // If it's not trivially captured and not trivially not captured,
667 // then it must be calling into another function in our SCC. Save
668 // its particulars for Argument-SCC analysis later.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000669 ArgumentGraphNode *Node = AG[&*A];
Benjamin Kramer135f7352016-06-26 12:28:59 +0000670 for (Argument *Use : Tracker.Uses) {
671 Node->Uses.push_back(AG[Use]);
672 if (Use != &*A)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000673 HasNonLocalUses = true;
674 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000675 }
676 }
677 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
678 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000679 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
680 // Can we determine that it's readonly/readnone without doing an SCC?
681 // Note that we don't allow any calls at all here, or else our result
682 // will be dependent on the iteration order through the functions in the
683 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000684 SmallPtrSet<Argument *, 8> Self;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000685 Self.insert(&*A);
686 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000687 if (R != Attribute::None) {
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000688 A->addAttr(R);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000689 Changed = true;
690 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
691 }
692 }
693 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000694 }
695
696 // The graph we've collected is partial because we stopped scanning for
697 // argument uses once we solved the argument trivially. These partial nodes
698 // show up as ArgumentGraphNode objects with an empty Uses list, and for
699 // these nodes the final decision about whether they capture has already been
700 // made. If the definition doesn't have a 'nocapture' attribute by now, it
701 // captures.
702
Chandler Carruth63559d72015-09-13 06:47:20 +0000703 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000704 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000705 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000706 if (!ArgumentSCC[0]->Definition)
707 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000708
709 // eg. "void f(int* x) { if (...) f(x); }"
710 if (ArgumentSCC[0]->Uses.size() == 1 &&
711 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000712 Argument *A = ArgumentSCC[0]->Definition;
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000713 A->addAttr(Attribute::NoCapture);
Nick Lewycky7e820552009-01-02 03:46:56 +0000714 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000715 Changed = true;
716 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000717 continue;
718 }
719
720 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000721 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
722 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000723 ArgumentGraphNode *Node = *I;
724 if (Node->Uses.empty()) {
725 if (!Node->Definition->hasNoCaptureAttr())
726 SCCCaptured = true;
727 }
728 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000729 if (SCCCaptured)
730 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000731
Chandler Carruth63559d72015-09-13 06:47:20 +0000732 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000733 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
734 // quickly looking up whether a given Argument is in this ArgumentSCC.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000735 for (ArgumentGraphNode *I : ArgumentSCC) {
736 ArgumentSCCNodes.insert(I->Definition);
Nick Lewycky4c378a42011-12-28 23:24:21 +0000737 }
738
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000739 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
740 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000741 ArgumentGraphNode *N = *I;
Benjamin Kramer135f7352016-06-26 12:28:59 +0000742 for (ArgumentGraphNode *Use : N->Uses) {
743 Argument *A = Use->Definition;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000744 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
745 continue;
746 SCCCaptured = true;
747 break;
748 }
749 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000750 if (SCCCaptured)
751 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000752
Nick Lewyckyf740db32012-01-05 22:21:45 +0000753 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000754 Argument *A = ArgumentSCC[i]->Definition;
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000755 A->addAttr(Attribute::NoCapture);
Nick Lewycky4c378a42011-12-28 23:24:21 +0000756 ++NumNoCapture;
757 Changed = true;
758 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000759
760 // We also want to compute readonly/readnone. With a small number of false
761 // negatives, we can assume that any pointer which is captured isn't going
762 // to be provably readonly or readnone, since by definition we can't
763 // analyze all uses of a captured pointer.
764 //
765 // The false negatives happen when the pointer is captured by a function
766 // that promises readonly/readnone behaviour on the pointer, then the
767 // pointer's lifetime ends before anything that writes to arbitrary memory.
768 // Also, a readonly/readnone pointer may be returned, but returning a
769 // pointer is capturing it.
770
771 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
772 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
773 Argument *A = ArgumentSCC[i]->Definition;
774 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
775 if (K == Attribute::ReadNone)
776 continue;
777 if (K == Attribute::ReadOnly) {
778 ReadAttr = Attribute::ReadOnly;
779 continue;
780 }
781 ReadAttr = K;
782 break;
783 }
784
785 if (ReadAttr != Attribute::None) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000786 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
787 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000788 // Clear out existing readonly/readnone attributes
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000789 A->removeAttr(Attribute::ReadOnly);
790 A->removeAttr(Attribute::ReadNone);
791 A->addAttr(ReadAttr);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000792 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
793 Changed = true;
794 }
795 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000796 }
797
798 return Changed;
799}
800
Chandler Carrutha632fb92015-09-13 06:57:25 +0000801/// Tests whether a function is "malloc-like".
802///
803/// A function is "malloc-like" if it returns either null or a pointer that
804/// doesn't alias any other pointer visible to the caller.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000805static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000806 SmallSetVector<Value *, 8> FlowsToReturn;
Benjamin Kramer135f7352016-06-26 12:28:59 +0000807 for (BasicBlock &BB : *F)
808 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000809 FlowsToReturn.insert(Ret->getReturnValue());
810
811 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000812 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000813
814 if (Constant *C = dyn_cast<Constant>(RetVal)) {
815 if (!C->isNullValue() && !isa<UndefValue>(C))
816 return false;
817
818 continue;
819 }
820
821 if (isa<Argument>(RetVal))
822 return false;
823
824 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
825 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000826 // Extend the analysis by looking upwards.
827 case Instruction::BitCast:
828 case Instruction::GetElementPtr:
829 case Instruction::AddrSpaceCast:
830 FlowsToReturn.insert(RVI->getOperand(0));
831 continue;
832 case Instruction::Select: {
833 SelectInst *SI = cast<SelectInst>(RVI);
834 FlowsToReturn.insert(SI->getTrueValue());
835 FlowsToReturn.insert(SI->getFalseValue());
836 continue;
837 }
838 case Instruction::PHI: {
839 PHINode *PN = cast<PHINode>(RVI);
840 for (Value *IncValue : PN->incoming_values())
841 FlowsToReturn.insert(IncValue);
842 continue;
843 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000844
Chandler Carruth63559d72015-09-13 06:47:20 +0000845 // Check whether the pointer came from an allocation.
846 case Instruction::Alloca:
847 break;
848 case Instruction::Call:
849 case Instruction::Invoke: {
850 CallSite CS(RVI);
Reid Klecknerfb502d22017-04-14 20:19:02 +0000851 if (CS.hasRetAttr(Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000852 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000853 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
854 break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000855 LLVM_FALLTHROUGH;
856 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000857 default:
858 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000859 }
860
Dan Gohman94e61762009-11-19 21:57:48 +0000861 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000862 return false;
863 }
864
865 return true;
866}
867
Chandler Carrutha632fb92015-09-13 06:57:25 +0000868/// Deduce noalias attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000869static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000870 // Check each function in turn, determining which functions return noalias
871 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000872 for (Function *F : SCCNodes) {
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000873 // Already noalias.
Reid Klecknera0b45f42017-05-03 18:17:31 +0000874 if (F->returnDoesNotAlias())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000875 continue;
876
Sanjoy Das5ce32722016-04-08 00:48:30 +0000877 // We can infer and propagate function attributes only when we know that the
878 // definition we'll get at link time is *exactly* the definition we see now.
879 // For more details, see GlobalValue::mayBeDerefined.
880 if (!F->hasExactDefinition())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000881 return false;
882
Chandler Carruth63559d72015-09-13 06:47:20 +0000883 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000884 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000885 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000886 continue;
887
Chandler Carruth3824f852015-09-13 08:23:27 +0000888 if (!isFunctionMallocLike(F, SCCNodes))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000889 return false;
890 }
891
892 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000893 for (Function *F : SCCNodes) {
Reid Klecknera0b45f42017-05-03 18:17:31 +0000894 if (F->returnDoesNotAlias() ||
Reid Kleckner6652a522017-04-28 18:37:16 +0000895 !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000896 continue;
897
Reid Klecknera0b45f42017-05-03 18:17:31 +0000898 F->setReturnDoesNotAlias();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000899 ++NumNoAlias;
900 MadeChange = true;
901 }
902
903 return MadeChange;
904}
905
Chandler Carrutha632fb92015-09-13 06:57:25 +0000906/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000907///
908/// Requires that the function returns a pointer.
909///
910/// Returns true if it believes the function will not return a null, and sets
911/// \p Speculative based on whether the returned conclusion is a speculative
912/// conclusion due to SCC calls.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000913static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
Sean Silva45835e72016-07-02 23:47:27 +0000914 bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000915 assert(F->getReturnType()->isPointerTy() &&
916 "nonnull only meaningful on pointer types");
917 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000918
Philip Reamesa88caea2015-08-31 19:44:38 +0000919 SmallSetVector<Value *, 8> FlowsToReturn;
920 for (BasicBlock &BB : *F)
921 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
922 FlowsToReturn.insert(Ret->getReturnValue());
923
Nuno Lopes404f1062017-09-09 18:23:11 +0000924 auto &DL = F->getParent()->getDataLayout();
925
Philip Reamesa88caea2015-08-31 19:44:38 +0000926 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
927 Value *RetVal = FlowsToReturn[i];
928
929 // If this value is locally known to be non-null, we're good
Nuno Lopes404f1062017-09-09 18:23:11 +0000930 if (isKnownNonZero(RetVal, DL))
Philip Reamesa88caea2015-08-31 19:44:38 +0000931 continue;
932
933 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000934 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000935 Instruction *RVI = dyn_cast<Instruction>(RetVal);
936 if (!RVI)
937 return false;
938 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000939 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000940 case Instruction::BitCast:
941 case Instruction::GetElementPtr:
942 case Instruction::AddrSpaceCast:
943 FlowsToReturn.insert(RVI->getOperand(0));
944 continue;
945 case Instruction::Select: {
946 SelectInst *SI = cast<SelectInst>(RVI);
947 FlowsToReturn.insert(SI->getTrueValue());
948 FlowsToReturn.insert(SI->getFalseValue());
949 continue;
950 }
951 case Instruction::PHI: {
952 PHINode *PN = cast<PHINode>(RVI);
953 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
954 FlowsToReturn.insert(PN->getIncomingValue(i));
955 continue;
956 }
957 case Instruction::Call:
958 case Instruction::Invoke: {
959 CallSite CS(RVI);
960 Function *Callee = CS.getCalledFunction();
961 // A call to a node within the SCC is assumed to return null until
962 // proven otherwise
963 if (Callee && SCCNodes.count(Callee)) {
964 Speculative = true;
965 continue;
966 }
967 return false;
968 }
969 default:
Chandler Carruth63559d72015-09-13 06:47:20 +0000970 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +0000971 };
972 llvm_unreachable("should have either continued or returned");
973 }
974
975 return true;
976}
977
Chandler Carrutha632fb92015-09-13 06:57:25 +0000978/// Deduce nonnull attributes for the SCC.
Sean Silva45835e72016-07-02 23:47:27 +0000979static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000980 // Speculative that all functions in the SCC return only nonnull
981 // pointers. We may refute this as we analyze functions.
982 bool SCCReturnsNonNull = true;
983
984 bool MadeChange = false;
985
986 // Check each function in turn, determining which functions return nonnull
987 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000988 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000989 // Already nonnull.
Reid Klecknerb5180542017-03-21 16:57:19 +0000990 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
Philip Reamesa88caea2015-08-31 19:44:38 +0000991 Attribute::NonNull))
992 continue;
993
Sanjoy Das5ce32722016-04-08 00:48:30 +0000994 // We can infer and propagate function attributes only when we know that the
995 // definition we'll get at link time is *exactly* the definition we see now.
996 // For more details, see GlobalValue::mayBeDerefined.
997 if (!F->hasExactDefinition())
Philip Reamesa88caea2015-08-31 19:44:38 +0000998 return false;
999
Chandler Carruth63559d72015-09-13 06:47:20 +00001000 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +00001001 // pointer types.
1002 if (!F->getReturnType()->isPointerTy())
1003 continue;
1004
1005 bool Speculative = false;
Sean Silva45835e72016-07-02 23:47:27 +00001006 if (isReturnNonNull(F, SCCNodes, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +00001007 if (!Speculative) {
1008 // Mark the function eagerly since we may discover a function
1009 // which prevents us from speculating about the entire SCC
1010 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
Reid Klecknerb5180542017-03-21 16:57:19 +00001011 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
Philip Reamesa88caea2015-08-31 19:44:38 +00001012 ++NumNonNullReturn;
1013 MadeChange = true;
1014 }
1015 continue;
1016 }
1017 // At least one function returns something which could be null, can't
1018 // speculate any more.
1019 SCCReturnsNonNull = false;
1020 }
1021
1022 if (SCCReturnsNonNull) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001023 for (Function *F : SCCNodes) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001024 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
Philip Reamesa88caea2015-08-31 19:44:38 +00001025 Attribute::NonNull) ||
1026 !F->getReturnType()->isPointerTy())
1027 continue;
1028
1029 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
Reid Klecknerb5180542017-03-21 16:57:19 +00001030 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
Philip Reamesa88caea2015-08-31 19:44:38 +00001031 ++NumNonNullReturn;
1032 MadeChange = true;
1033 }
1034 }
1035
1036 return MadeChange;
1037}
1038
Justin Lebar9d943972016-03-14 20:18:54 +00001039/// Remove the convergent attribute from all functions in the SCC if every
1040/// callsite within the SCC is not convergent (except for calls to functions
1041/// within the SCC). Returns true if changes were made.
Chandler Carruth3937bc72016-02-12 09:47:49 +00001042static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) {
Justin Lebar9d943972016-03-14 20:18:54 +00001043 // For every function in SCC, ensure that either
1044 // * it is not convergent, or
1045 // * we can remove its convergent attribute.
1046 bool HasConvergentFn = false;
Chandler Carruth3937bc72016-02-12 09:47:49 +00001047 for (Function *F : SCCNodes) {
Justin Lebar9d943972016-03-14 20:18:54 +00001048 if (!F->isConvergent()) continue;
1049 HasConvergentFn = true;
1050
1051 // Can't remove convergent from function declarations.
1052 if (F->isDeclaration()) return false;
1053
1054 // Can't remove convergent if any of our functions has a convergent call to a
1055 // function not in the SCC.
1056 for (Instruction &I : instructions(*F)) {
1057 CallSite CS(&I);
1058 // Bail if CS is a convergent call to a function not in the SCC.
1059 if (CS && CS.isConvergent() &&
1060 SCCNodes.count(CS.getCalledFunction()) == 0)
1061 return false;
1062 }
1063 }
1064
1065 // If the SCC doesn't have any convergent functions, we have nothing to do.
1066 if (!HasConvergentFn) return false;
1067
1068 // If we got here, all of the calls the SCC makes to functions not in the SCC
1069 // are non-convergent. Therefore all of the SCC's functions can also be made
1070 // non-convergent. We'll remove the attr from the callsites in
1071 // InstCombineCalls.
1072 for (Function *F : SCCNodes) {
1073 if (!F->isConvergent()) continue;
1074
1075 DEBUG(dbgs() << "Removing convergent attr from fn " << F->getName()
1076 << "\n");
Chandler Carruth3937bc72016-02-12 09:47:49 +00001077 F->setNotConvergent();
1078 }
Justin Lebar260854b2016-02-09 23:03:22 +00001079 return true;
1080}
1081
James Molloy7e9bdd52015-11-12 10:55:20 +00001082static bool setDoesNotRecurse(Function &F) {
1083 if (F.doesNotRecurse())
1084 return false;
1085 F.setDoesNotRecurse();
1086 ++NumNoRecurse;
1087 return true;
1088}
1089
Chandler Carruth632d2082016-02-13 08:47:51 +00001090static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
James Molloy7e9bdd52015-11-12 10:55:20 +00001091 // Try and identify functions that do not recurse.
1092
1093 // If the SCC contains multiple nodes we know for sure there is recursion.
Chandler Carruth632d2082016-02-13 08:47:51 +00001094 if (SCCNodes.size() != 1)
James Molloy7e9bdd52015-11-12 10:55:20 +00001095 return false;
1096
Chandler Carruth632d2082016-02-13 08:47:51 +00001097 Function *F = *SCCNodes.begin();
James Molloy7e9bdd52015-11-12 10:55:20 +00001098 if (!F || F->isDeclaration() || F->doesNotRecurse())
1099 return false;
1100
1101 // If all of the calls in F are identifiable and are to norecurse functions, F
1102 // is norecurse. This check also detects self-recursion as F is not currently
1103 // marked norecurse, so any called from F to F will not be marked norecurse.
Chandler Carruth632d2082016-02-13 08:47:51 +00001104 for (Instruction &I : instructions(*F))
1105 if (auto CS = CallSite(&I)) {
1106 Function *Callee = CS.getCalledFunction();
1107 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1108 // Function calls a potentially recursive function.
1109 return false;
1110 }
James Molloy7e9bdd52015-11-12 10:55:20 +00001111
Chandler Carruth632d2082016-02-13 08:47:51 +00001112 // Every call was to a non-recursive function other than this function, and
1113 // we have no indirect recursion as the SCC size is one. This function cannot
1114 // recurse.
1115 return setDoesNotRecurse(*F);
James Molloy7e9bdd52015-11-12 10:55:20 +00001116}
1117
Chandler Carruthb47f8012016-03-11 11:05:24 +00001118PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
Chandler Carruth88823462016-08-24 09:37:14 +00001119 CGSCCAnalysisManager &AM,
1120 LazyCallGraph &CG,
1121 CGSCCUpdateResult &) {
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001122 FunctionAnalysisManager &FAM =
Chandler Carruth88823462016-08-24 09:37:14 +00001123 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001124
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001125 // We pass a lambda into functions to wire them up to the analysis manager
1126 // for getting function analyses.
1127 auto AARGetter = [&](Function &F) -> AAResults & {
1128 return FAM.getResult<AAManager>(F);
1129 };
1130
1131 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1132 // any external or opt-none nodes that will prevent us from optimizing any
1133 // part of the SCC.
1134 SCCNodeSet SCCNodes;
1135 bool HasUnknownCall = false;
1136 for (LazyCallGraph::Node &N : C) {
1137 Function &F = N.getFunction();
1138 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
1139 // Treat any function we're trying not to optimize as if it were an
1140 // indirect call and omit it from the node set used below.
1141 HasUnknownCall = true;
1142 continue;
1143 }
1144 // Track whether any functions in this SCC have an unknown call edge.
1145 // Note: if this is ever a performance hit, we can common it with
1146 // subsequent routines which also do scans over the instructions of the
1147 // function.
1148 if (!HasUnknownCall)
1149 for (Instruction &I : instructions(F))
1150 if (auto CS = CallSite(&I))
1151 if (!CS.getCalledFunction()) {
1152 HasUnknownCall = true;
1153 break;
1154 }
1155
1156 SCCNodes.insert(&F);
1157 }
1158
1159 bool Changed = false;
David Majnemer5246e0b2016-07-19 18:50:26 +00001160 Changed |= addArgumentReturnedAttrs(SCCNodes);
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001161 Changed |= addReadAttrs(SCCNodes, AARGetter);
1162 Changed |= addArgumentAttrs(SCCNodes);
1163
1164 // If we have no external nodes participating in the SCC, we can deduce some
1165 // more precise attributes as well.
1166 if (!HasUnknownCall) {
1167 Changed |= addNoAliasAttrs(SCCNodes);
Sean Silva45835e72016-07-02 23:47:27 +00001168 Changed |= addNonNullAttrs(SCCNodes);
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001169 Changed |= removeConvergentAttrs(SCCNodes);
1170 Changed |= addNoRecurseAttrs(SCCNodes);
1171 }
1172
1173 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1174}
1175
1176namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001177
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001178struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001179 // Pass identification, replacement for typeid
1180 static char ID;
1181
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001182 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
Chad Rosier611b73b2016-11-07 16:28:04 +00001183 initializePostOrderFunctionAttrsLegacyPassPass(
1184 *PassRegistry::getPassRegistry());
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001185 }
1186
1187 bool runOnSCC(CallGraphSCC &SCC) override;
1188
1189 void getAnalysisUsage(AnalysisUsage &AU) const override {
1190 AU.setPreservesCFG();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001191 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruth12884f72016-03-02 15:56:53 +00001192 getAAResultsAnalysisUsage(AU);
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001193 CallGraphSCCPass::getAnalysisUsage(AU);
1194 }
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001195};
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001196
1197} // end anonymous namespace
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001198
1199char PostOrderFunctionAttrsLegacyPass::ID = 0;
1200INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1201 "Deduce function attributes", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001202INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001203INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001204INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1205 "Deduce function attributes", false, false)
1206
Chad Rosier611b73b2016-11-07 16:28:04 +00001207Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1208 return new PostOrderFunctionAttrsLegacyPass();
1209}
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001210
Sean Silva997cbea2016-07-03 03:35:03 +00001211template <typename AARGetterT>
1212static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
Chandler Carruthcada2d82015-10-31 00:28:37 +00001213 bool Changed = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001214
1215 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1216 // whether a given CallGraphNode is in this SCC. Also track whether there are
1217 // any external or opt-none nodes that will prevent us from optimizing any
1218 // part of the SCC.
1219 SCCNodeSet SCCNodes;
1220 bool ExternalNode = false;
Benjamin Kramer135f7352016-06-26 12:28:59 +00001221 for (CallGraphNode *I : SCC) {
1222 Function *F = I->getFunction();
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001223 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1224 // External node or function we're trying not to optimize - we both avoid
1225 // transform them and avoid leveraging information they provide.
1226 ExternalNode = true;
1227 continue;
1228 }
1229
1230 SCCNodes.insert(F);
1231 }
1232
David Blaikie6aeacaa2017-06-02 21:24:17 +00001233 // Skip it if the SCC only contains optnone functions.
1234 if (SCCNodes.empty())
1235 return Changed;
1236
David Majnemer5246e0b2016-07-19 18:50:26 +00001237 Changed |= addArgumentReturnedAttrs(SCCNodes);
Chandler Carrutha8125352015-10-30 16:48:08 +00001238 Changed |= addReadAttrs(SCCNodes, AARGetter);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001239 Changed |= addArgumentAttrs(SCCNodes);
1240
Chandler Carruth3a040e62015-12-27 08:41:34 +00001241 // If we have no external nodes participating in the SCC, we can deduce some
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001242 // more precise attributes as well.
1243 if (!ExternalNode) {
1244 Changed |= addNoAliasAttrs(SCCNodes);
Sean Silva45835e72016-07-02 23:47:27 +00001245 Changed |= addNonNullAttrs(SCCNodes);
Chandler Carruth3937bc72016-02-12 09:47:49 +00001246 Changed |= removeConvergentAttrs(SCCNodes);
Chandler Carruth632d2082016-02-13 08:47:51 +00001247 Changed |= addNoRecurseAttrs(SCCNodes);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001248 }
Chandler Carruth1926b702016-01-08 10:55:52 +00001249
James Molloy7e9bdd52015-11-12 10:55:20 +00001250 return Changed;
1251}
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001252
Sean Silva997cbea2016-07-03 03:35:03 +00001253bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1254 if (skipSCC(SCC))
1255 return false;
Peter Collingbournecea1e4e2017-02-09 23:11:52 +00001256 return runImpl(SCC, LegacyAARGetter(*this));
Sean Silva997cbea2016-07-03 03:35:03 +00001257}
1258
Chandler Carruth1926b702016-01-08 10:55:52 +00001259namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001260
Sean Silvaf5080192016-06-12 07:48:51 +00001261struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001262 // Pass identification, replacement for typeid
1263 static char ID;
1264
Sean Silvaf5080192016-06-12 07:48:51 +00001265 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
Chad Rosier611b73b2016-11-07 16:28:04 +00001266 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1267 *PassRegistry::getPassRegistry());
Chandler Carruth1926b702016-01-08 10:55:52 +00001268 }
1269
1270 bool runOnModule(Module &M) override;
1271
1272 void getAnalysisUsage(AnalysisUsage &AU) const override {
1273 AU.setPreservesCFG();
1274 AU.addRequired<CallGraphWrapperPass>();
Mehdi Amini0ddf4042016-05-02 18:03:33 +00001275 AU.addPreserved<CallGraphWrapperPass>();
Chandler Carruth1926b702016-01-08 10:55:52 +00001276 }
1277};
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001278
1279} // end anonymous namespace
Chandler Carruth1926b702016-01-08 10:55:52 +00001280
Sean Silvaf5080192016-06-12 07:48:51 +00001281char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001282
Sean Silvaf5080192016-06-12 07:48:51 +00001283INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
Chandler Carruth1926b702016-01-08 10:55:52 +00001284 "Deduce function attributes in RPO", false, false)
1285INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Sean Silvaf5080192016-06-12 07:48:51 +00001286INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
Chandler Carruth1926b702016-01-08 10:55:52 +00001287 "Deduce function attributes in RPO", false, false)
1288
1289Pass *llvm::createReversePostOrderFunctionAttrsPass() {
Sean Silvaf5080192016-06-12 07:48:51 +00001290 return new ReversePostOrderFunctionAttrsLegacyPass();
Chandler Carruth1926b702016-01-08 10:55:52 +00001291}
1292
1293static bool addNoRecurseAttrsTopDown(Function &F) {
1294 // We check the preconditions for the function prior to calling this to avoid
1295 // the cost of building up a reversible post-order list. We assert them here
1296 // to make sure none of the invariants this relies on were violated.
1297 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1298 assert(!F.doesNotRecurse() &&
1299 "This function has already been deduced as norecurs!");
1300 assert(F.hasInternalLinkage() &&
1301 "Can only do top-down deduction for internal linkage functions!");
1302
1303 // If F is internal and all of its uses are calls from a non-recursive
1304 // functions, then none of its calls could in fact recurse without going
1305 // through a function marked norecurse, and so we can mark this function too
1306 // as norecurse. Note that the uses must actually be calls -- otherwise
1307 // a pointer to this function could be returned from a norecurse function but
1308 // this function could be recursively (indirectly) called. Note that this
1309 // also detects if F is directly recursive as F is not yet marked as
1310 // a norecurse function.
1311 for (auto *U : F.users()) {
1312 auto *I = dyn_cast<Instruction>(U);
1313 if (!I)
1314 return false;
1315 CallSite CS(I);
1316 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1317 return false;
1318 }
1319 return setDoesNotRecurse(F);
1320}
1321
Sean Silvaadc79392016-06-12 05:44:51 +00001322static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
Chandler Carruth1926b702016-01-08 10:55:52 +00001323 // We only have a post-order SCC traversal (because SCCs are inherently
1324 // discovered in post-order), so we accumulate them in a vector and then walk
1325 // it in reverse. This is simpler than using the RPO iterator infrastructure
1326 // because we need to combine SCC detection and the PO walk of the call
1327 // graph. We can also cheat egregiously because we're primarily interested in
1328 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1329 // with multiple functions in them will clearly be recursive.
Chandler Carruth1926b702016-01-08 10:55:52 +00001330 SmallVector<Function *, 16> Worklist;
1331 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1332 if (I->size() != 1)
1333 continue;
1334
1335 Function *F = I->front()->getFunction();
1336 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1337 F->hasInternalLinkage())
1338 Worklist.push_back(F);
1339 }
1340
James Molloy7e9bdd52015-11-12 10:55:20 +00001341 bool Changed = false;
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001342 for (auto *F : llvm::reverse(Worklist))
Chandler Carruth1926b702016-01-08 10:55:52 +00001343 Changed |= addNoRecurseAttrsTopDown(*F);
1344
Duncan Sands44c8cd92008-12-31 16:14:43 +00001345 return Changed;
1346}
Sean Silvaadc79392016-06-12 05:44:51 +00001347
Sean Silvaf5080192016-06-12 07:48:51 +00001348bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
Sean Silvaadc79392016-06-12 05:44:51 +00001349 if (skipModule(M))
1350 return false;
1351
1352 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1353
1354 return deduceFunctionAttributeInRPO(M, CG);
1355}
Sean Silvaf5080192016-06-12 07:48:51 +00001356
1357PreservedAnalyses
Sean Silvafd03ac62016-08-09 00:28:38 +00001358ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
Sean Silvaf5080192016-06-12 07:48:51 +00001359 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1360
Chandler Carruth6acdca72017-01-24 12:55:57 +00001361 if (!deduceFunctionAttributeInRPO(M, CG))
Sean Silvaf5080192016-06-12 07:48:51 +00001362 return PreservedAnalyses::all();
Chandler Carruth6acdca72017-01-24 12:55:57 +00001363
Sean Silvaf5080192016-06-12 07:48:51 +00001364 PreservedAnalyses PA;
1365 PA.preserve<CallGraphAnalysis>();
1366 return PA;
1367}