blob: 813a4b6e283198409c067985957d9c50b8157220 [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//===----------------------------------------------------------------------===//
Chandler Carruth1926b702016-01-08 10:55:52 +00009///
10/// \file
11/// This file implements interprocedural passes which walk the
12/// call-graph deducing and/or propagating function attributes.
13///
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"
Benjamin Kramer15591272012-10-31 13:45:49 +000018#include "llvm/ADT/SetVector.h"
Duncan Sandsb193a372009-01-02 11:54:37 +000019#include "llvm/ADT/SmallSet.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000020#include "llvm/ADT/Statistic.h"
James Molloy0ecdbe72015-11-19 08:49:57 +000021#include "llvm/ADT/StringSwitch.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000022#include "llvm/Analysis/AliasAnalysis.h"
Daniel Jasperaec2fa32016-12-19 08:22:17 +000023#include "llvm/Analysis/AssumptionCache.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000024#include "llvm/Analysis/BasicAliasAnalysis.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000025#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000026#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000027#include "llvm/Analysis/CaptureTracking.h"
Chandler Carruth7b560d42015-09-09 17:55:00 +000028#include "llvm/Analysis/TargetLibraryInfo.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000029#include "llvm/Analysis/ValueTracking.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"
Philip Reamesa88caea2015-08-31 19:44:38 +000034#include "llvm/Support/Debug.h"
Hans Wennborg043bf5b2015-08-31 21:19:18 +000035#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000036#include "llvm/Transforms/IPO.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000037using namespace llvm;
38
Chandler Carruth964daaa2014-04-22 02:55:47 +000039#define DEBUG_TYPE "functionattrs"
40
Duncan Sands44c8cd92008-12-31 16:14:43 +000041STATISTIC(NumReadNone, "Number of functions marked readnone");
42STATISTIC(NumReadOnly, "Number of functions marked readonly");
43STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
David Majnemer5246e0b2016-07-19 18:50:26 +000044STATISTIC(NumReturned, "Number of arguments marked returned");
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000045STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
46STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000047STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Philip Reamesa88caea2015-08-31 19:44:38 +000048STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
James Molloy7e9bdd52015-11-12 10:55:20 +000049STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
Duncan Sands44c8cd92008-12-31 16:14:43 +000050
Sanjay Patel4f742162017-02-13 23:10:51 +000051// FIXME: This is disabled by default to avoid exposing security vulnerabilities
52// in C/C++ code compiled by clang:
53// http://lists.llvm.org/pipermail/cfe-dev/2017-January/052066.html
54static cl::opt<bool> EnableNonnullArgPropagation(
55 "enable-nonnull-arg-prop", cl::Hidden,
56 cl::desc("Try to propagate nonnull argument attributes from callsites to "
57 "caller functions."));
58
Duncan Sands44c8cd92008-12-31 16:14:43 +000059namespace {
Chandler Carruthc518ebd2015-10-29 18:29:15 +000060typedef SmallSetVector<Function *, 8> SCCNodeSet;
61}
62
Peter Collingbournec45f7f32017-02-14 00:28:13 +000063/// Returns the memory access attribute for function F using AAR for AA results,
64/// where SCCNodes is the current SCC.
65///
66/// If ThisBody is true, this function may examine the function body and will
67/// return a result pertaining to this copy of the function. If it is false, the
68/// result will be based only on AA results for the function declaration; it
69/// will be assumed that some other (perhaps less optimized) version of the
70/// function may be selected at link time.
71static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
72 AAResults &AAR,
Chandler Carruthc518ebd2015-10-29 18:29:15 +000073 const SCCNodeSet &SCCNodes) {
Chandler Carruth7542d372015-09-21 17:39:41 +000074 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
75 if (MRB == FMRB_DoesNotAccessMemory)
76 // Already perfect!
77 return MAK_ReadNone;
78
Peter Collingbournec45f7f32017-02-14 00:28:13 +000079 if (!ThisBody) {
Chandler Carruth7542d372015-09-21 17:39:41 +000080 if (AliasAnalysis::onlyReadsMemory(MRB))
81 return MAK_ReadOnly;
82
83 // Conservatively assume it writes to memory.
84 return MAK_MayWrite;
85 }
86
87 // Scan the function body for instructions that may read or write memory.
88 bool ReadsMemory = false;
89 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
90 Instruction *I = &*II;
91
92 // Some instructions can be ignored even if they read or write memory.
93 // Detect these now, skipping to the next instruction if one is found.
94 CallSite CS(cast<Value>(I));
95 if (CS) {
Sanjoy Das10c8a042016-02-09 18:40:40 +000096 // Ignore calls to functions in the same SCC, as long as the call sites
97 // don't have operand bundles. Calls with operand bundles are allowed to
98 // have memory effects not described by the memory effects of the call
99 // target.
100 if (!CS.hasOperandBundles() && CS.getCalledFunction() &&
101 SCCNodes.count(CS.getCalledFunction()))
Chandler Carruth7542d372015-09-21 17:39:41 +0000102 continue;
103 FunctionModRefBehavior MRB = AAR.getModRefBehavior(CS);
Chandler Carruth7542d372015-09-21 17:39:41 +0000104
Chandler Carruth69798fb2015-10-27 01:41:43 +0000105 // If the call doesn't access memory, we're done.
106 if (!(MRB & MRI_ModRef))
107 continue;
108
109 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
110 // The call could access any memory. If that includes writes, give up.
111 if (MRB & MRI_Mod)
112 return MAK_MayWrite;
113 // If it reads, note it.
114 if (MRB & MRI_Ref)
115 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000116 continue;
117 }
Chandler Carruth69798fb2015-10-27 01:41:43 +0000118
119 // Check whether all pointer arguments point to local memory, and
120 // ignore calls that only access local memory.
121 for (CallSite::arg_iterator CI = CS.arg_begin(), CE = CS.arg_end();
122 CI != CE; ++CI) {
123 Value *Arg = *CI;
Elena Demikhovsky3ec9e152015-11-17 19:30:51 +0000124 if (!Arg->getType()->isPtrOrPtrVectorTy())
Chandler Carruth69798fb2015-10-27 01:41:43 +0000125 continue;
126
127 AAMDNodes AAInfo;
128 I->getAAMetadata(AAInfo);
129 MemoryLocation Loc(Arg, MemoryLocation::UnknownSize, AAInfo);
130
131 // Skip accesses to local or constant memory as they don't impact the
132 // externally visible mod/ref behavior.
133 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
134 continue;
135
136 if (MRB & MRI_Mod)
137 // Writes non-local memory. Give up.
138 return MAK_MayWrite;
139 if (MRB & MRI_Ref)
140 // Ok, it reads non-local memory.
141 ReadsMemory = true;
142 }
Chandler Carruth7542d372015-09-21 17:39:41 +0000143 continue;
144 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
145 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
146 if (!LI->isVolatile()) {
147 MemoryLocation Loc = MemoryLocation::get(LI);
148 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
149 continue;
150 }
151 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
152 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
153 if (!SI->isVolatile()) {
154 MemoryLocation Loc = MemoryLocation::get(SI);
155 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
156 continue;
157 }
158 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
159 // Ignore vaargs on local memory.
160 MemoryLocation Loc = MemoryLocation::get(VI);
161 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
162 continue;
163 }
164
165 // Any remaining instructions need to be taken seriously! Check if they
166 // read or write memory.
167 if (I->mayWriteToMemory())
168 // Writes memory. Just give up.
169 return MAK_MayWrite;
170
171 // If this instruction may read memory, remember that.
172 ReadsMemory |= I->mayReadFromMemory();
173 }
174
175 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
176}
177
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000178MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
179 AAResults &AAR) {
180 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
181}
182
Chandler Carrutha632fb92015-09-13 06:57:25 +0000183/// Deduce readonly/readnone attributes for the SCC.
Chandler Carrutha8125352015-10-30 16:48:08 +0000184template <typename AARGetterT>
Peter Collingbournecea1e4e2017-02-09 23:11:52 +0000185static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000186 // Check if any of the functions in the SCC read or write memory. If they
187 // write memory then they can't be marked readnone or readonly.
188 bool ReadsMemory = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000189 for (Function *F : SCCNodes) {
Chandler Carrutha8125352015-10-30 16:48:08 +0000190 // Call the callable parameter to look up AA results for this function.
191 AAResults &AAR = AARGetter(*F);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000192
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000193 // Non-exact function definitions may not be selected at link time, and an
194 // alternative version that writes to memory may be selected. See the
195 // comment on GlobalValue::isDefinitionExact for more details.
196 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
197 AAR, SCCNodes)) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000198 case MAK_MayWrite:
199 return false;
200 case MAK_ReadOnly:
Duncan Sands44c8cd92008-12-31 16:14:43 +0000201 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000202 break;
203 case MAK_ReadNone:
204 // Nothing to do!
205 break;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000206 }
207 }
208
209 // Success! Functions in this SCC do not access memory, or only read memory.
210 // Give them the appropriate attribute.
211 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000212 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000213 if (F->doesNotAccessMemory())
214 // Already perfect!
215 continue;
216
217 if (F->onlyReadsMemory() && ReadsMemory)
218 // No change.
219 continue;
220
221 MadeChange = true;
222
223 // Clear out any existing attributes.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000224 F->removeFnAttr(Attribute::ReadOnly);
225 F->removeFnAttr(Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000226
227 // Add in the new attribute.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000228 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000229
230 if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000231 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000232 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000233 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000234 }
235
236 return MadeChange;
237}
238
Nick Lewycky4c378a42011-12-28 23:24:21 +0000239namespace {
Chandler Carrutha632fb92015-09-13 06:57:25 +0000240/// For a given pointer Argument, this retains a list of Arguments of functions
241/// in the same SCC that the pointer data flows into. We use this to build an
242/// SCC of the arguments.
Chandler Carruth63559d72015-09-13 06:47:20 +0000243struct ArgumentGraphNode {
244 Argument *Definition;
245 SmallVector<ArgumentGraphNode *, 4> Uses;
246};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000247
Chandler Carruth63559d72015-09-13 06:47:20 +0000248class ArgumentGraph {
249 // We store pointers to ArgumentGraphNode objects, so it's important that
250 // that they not move around upon insert.
251 typedef std::map<Argument *, ArgumentGraphNode> ArgumentMapTy;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000252
Chandler Carruth63559d72015-09-13 06:47:20 +0000253 ArgumentMapTy ArgumentMap;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000254
Chandler Carruth63559d72015-09-13 06:47:20 +0000255 // There is no root node for the argument graph, in fact:
256 // void f(int *x, int *y) { if (...) f(x, y); }
257 // is an example where the graph is disconnected. The SCCIterator requires a
258 // single entry point, so we maintain a fake ("synthetic") root node that
259 // uses every node. Because the graph is directed and nothing points into
260 // the root, it will not participate in any SCCs (except for its own).
261 ArgumentGraphNode SyntheticRoot;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000262
Chandler Carruth63559d72015-09-13 06:47:20 +0000263public:
264 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000265
Chandler Carruth63559d72015-09-13 06:47:20 +0000266 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000267
Chandler Carruth63559d72015-09-13 06:47:20 +0000268 iterator begin() { return SyntheticRoot.Uses.begin(); }
269 iterator end() { return SyntheticRoot.Uses.end(); }
270 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000271
Chandler Carruth63559d72015-09-13 06:47:20 +0000272 ArgumentGraphNode *operator[](Argument *A) {
273 ArgumentGraphNode &Node = ArgumentMap[A];
274 Node.Definition = A;
275 SyntheticRoot.Uses.push_back(&Node);
276 return &Node;
277 }
278};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000279
Chandler Carrutha632fb92015-09-13 06:57:25 +0000280/// This tracker checks whether callees are in the SCC, and if so it does not
281/// consider that a capture, instead adding it to the "Uses" list and
282/// continuing with the analysis.
Chandler Carruth63559d72015-09-13 06:47:20 +0000283struct ArgumentUsesTracker : public CaptureTracker {
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000284 ArgumentUsesTracker(const SCCNodeSet &SCCNodes)
Nick Lewycky4c378a42011-12-28 23:24:21 +0000285 : Captured(false), SCCNodes(SCCNodes) {}
286
Chandler Carruth63559d72015-09-13 06:47:20 +0000287 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000288
Chandler Carruth63559d72015-09-13 06:47:20 +0000289 bool captured(const Use *U) override {
290 CallSite CS(U->getUser());
291 if (!CS.getInstruction()) {
292 Captured = true;
293 return true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000294 }
295
Chandler Carruth63559d72015-09-13 06:47:20 +0000296 Function *F = CS.getCalledFunction();
Sanjoy Das5ce32722016-04-08 00:48:30 +0000297 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000298 Captured = true;
299 return true;
300 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000301
Sanjoy Das98bfe262015-11-05 03:04:40 +0000302 // Note: the callee and the two successor blocks *follow* the argument
303 // operands. This means there is no need to adjust UseIndex to account for
304 // these.
305
306 unsigned UseIndex =
307 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
308
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000309 assert(UseIndex < CS.data_operands_size() &&
310 "Indirect function calls should have been filtered above!");
311
312 if (UseIndex >= CS.getNumArgOperands()) {
313 // Data operand, but not a argument operand -- must be a bundle operand
314 assert(CS.hasOperandBundles() && "Must be!");
315
316 // CaptureTracking told us that we're being captured by an operand bundle
317 // use. In this case it does not matter if the callee is within our SCC
318 // or not -- we've been captured in some unknown way, and we have to be
319 // conservative.
320 Captured = true;
321 return true;
322 }
323
Sanjoy Das98bfe262015-11-05 03:04:40 +0000324 if (UseIndex >= F->arg_size()) {
325 assert(F->isVarArg() && "More params than args in non-varargs call");
326 Captured = true;
327 return true;
Chandler Carruth63559d72015-09-13 06:47:20 +0000328 }
Sanjoy Das98bfe262015-11-05 03:04:40 +0000329
Duncan P. N. Exon Smith83c4b682015-11-07 00:01:16 +0000330 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
Chandler Carruth63559d72015-09-13 06:47:20 +0000331 return false;
332 }
333
334 bool Captured; // True only if certainly captured (used outside our SCC).
335 SmallVector<Argument *, 4> Uses; // Uses within our SCC.
336
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000337 const SCCNodeSet &SCCNodes;
Chandler Carruth63559d72015-09-13 06:47:20 +0000338};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000339}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000340
341namespace llvm {
Chandler Carruth63559d72015-09-13 06:47:20 +0000342template <> struct GraphTraits<ArgumentGraphNode *> {
Tim Shenb44909e2016-08-01 22:32:20 +0000343 typedef ArgumentGraphNode *NodeRef;
Chandler Carruth63559d72015-09-13 06:47:20 +0000344 typedef SmallVectorImpl<ArgumentGraphNode *>::iterator ChildIteratorType;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000345
Tim Shen48f814e2016-08-31 16:48:13 +0000346 static NodeRef getEntryNode(NodeRef A) { return A; }
347 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
348 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
Chandler Carruth63559d72015-09-13 06:47:20 +0000349};
350template <>
351struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
Tim Shenf2187ed2016-08-22 21:09:30 +0000352 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
Chandler Carruth63559d72015-09-13 06:47:20 +0000353 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
354 return AG->begin();
355 }
356 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
357};
Alexander Kornienkof00654e2015-06-23 09:49:53 +0000358}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000359
Chandler Carrutha632fb92015-09-13 06:57:25 +0000360/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000361static Attribute::AttrKind
362determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000363 const SmallPtrSet<Argument *, 8> &SCCNodes) {
364
365 SmallVector<Use *, 32> Worklist;
366 SmallSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000367
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000368 // inalloca arguments are always clobbered by the call.
369 if (A->hasInAllocaAttr())
370 return Attribute::None;
371
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000372 bool IsRead = false;
373 // We don't need to track IsWritten. If A is written to, return immediately.
374
Chandler Carruthcdf47882014-03-09 03:16:01 +0000375 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000376 Visited.insert(&U);
377 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000378 }
379
380 while (!Worklist.empty()) {
381 Use *U = Worklist.pop_back_val();
382 Instruction *I = cast<Instruction>(U->getUser());
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000383
384 switch (I->getOpcode()) {
385 case Instruction::BitCast:
386 case Instruction::GetElementPtr:
387 case Instruction::PHI:
388 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000389 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000390 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000391 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000392 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000393 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000394 break;
395
396 case Instruction::Call:
397 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000398 bool Captures = true;
399
400 if (I->getType()->isVoidTy())
401 Captures = false;
402
403 auto AddUsersToWorklistIfCapturing = [&] {
404 if (Captures)
405 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000406 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000407 Worklist.push_back(&UU);
408 };
409
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000410 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000411 if (CS.doesNotAccessMemory()) {
412 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000413 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000414 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000415
416 Function *F = CS.getCalledFunction();
417 if (!F) {
418 if (CS.onlyReadsMemory()) {
419 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000420 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000421 continue;
422 }
423 return Attribute::None;
424 }
425
Sanjoy Das436e2392015-11-07 01:55:53 +0000426 // Note: the callee and the two successor blocks *follow* the argument
427 // operands. This means there is no need to adjust UseIndex to account
428 // for these.
429
430 unsigned UseIndex = std::distance(CS.arg_begin(), U);
431
Sanjoy Dasea1df7f2015-11-07 01:56:07 +0000432 // U cannot be the callee operand use: since we're exploring the
433 // transitive uses of an Argument, having such a use be a callee would
434 // imply the CallSite is an indirect call or invoke; and we'd take the
435 // early exit above.
436 assert(UseIndex < CS.data_operands_size() &&
437 "Data operand use expected!");
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000438
439 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
440
441 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
Sanjoy Das436e2392015-11-07 01:55:53 +0000442 assert(F->isVarArg() && "More params than args in non-varargs call");
443 return Attribute::None;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000444 }
Sanjoy Das436e2392015-11-07 01:55:53 +0000445
Tilmann Scheller925b1932015-11-20 19:17:10 +0000446 Captures &= !CS.doesNotCapture(UseIndex);
447
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000448 // Since the optimizer (by design) cannot see the data flow corresponding
449 // to a operand bundle use, these cannot participate in the optimistic SCC
450 // analysis. Instead, we model the operand bundle uses as arguments in
451 // call to a function external to the SCC.
Duncan P. N. Exon Smith9e3edad2016-08-17 01:23:58 +0000452 if (IsOperandBundleUse ||
453 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000454
455 // The accessors used on CallSite here do the right thing for calls and
456 // invokes with operand bundles.
457
Sanjoy Das436e2392015-11-07 01:55:53 +0000458 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
459 return Attribute::None;
460 if (!CS.doesNotAccessMemory(UseIndex))
461 IsRead = true;
462 }
463
Nick Lewycky59633cb2014-05-30 02:31:27 +0000464 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000465 break;
466 }
467
468 case Instruction::Load:
David Majnemer124bdb72016-05-25 05:53:04 +0000469 // A volatile load has side effects beyond what readonly can be relied
470 // upon.
471 if (cast<LoadInst>(I)->isVolatile())
472 return Attribute::None;
473
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000474 IsRead = true;
475 break;
476
477 case Instruction::ICmp:
478 case Instruction::Ret:
479 break;
480
481 default:
482 return Attribute::None;
483 }
484 }
485
486 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
487}
488
David Majnemer5246e0b2016-07-19 18:50:26 +0000489/// Deduce returned attributes for the SCC.
490static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
491 bool Changed = false;
492
David Majnemer5246e0b2016-07-19 18:50:26 +0000493 // Check each function in turn, determining if an argument is always returned.
494 for (Function *F : SCCNodes) {
495 // We can infer and propagate function attributes only when we know that the
496 // definition we'll get at link time is *exactly* the definition we see now.
497 // For more details, see GlobalValue::mayBeDerefined.
498 if (!F->hasExactDefinition())
499 continue;
500
501 if (F->getReturnType()->isVoidTy())
502 continue;
503
David Majnemerc83044d2016-09-12 16:04:59 +0000504 // There is nothing to do if an argument is already marked as 'returned'.
505 if (any_of(F->args(),
506 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
507 continue;
508
David Majnemer5246e0b2016-07-19 18:50:26 +0000509 auto FindRetArg = [&]() -> Value * {
510 Value *RetArg = nullptr;
511 for (BasicBlock &BB : *F)
512 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
513 // Note that stripPointerCasts should look through functions with
514 // returned arguments.
515 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
516 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
517 return nullptr;
518
519 if (!RetArg)
520 RetArg = RetVal;
521 else if (RetArg != RetVal)
522 return nullptr;
523 }
524
525 return RetArg;
526 };
527
528 if (Value *RetArg = FindRetArg()) {
529 auto *A = cast<Argument>(RetArg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000530 A->addAttr(Attribute::Returned);
David Majnemer5246e0b2016-07-19 18:50:26 +0000531 ++NumReturned;
532 Changed = true;
533 }
534 }
535
536 return Changed;
537}
538
Sanjay Patel4f742162017-02-13 23:10:51 +0000539/// If a callsite has arguments that are also arguments to the parent function,
540/// try to propagate attributes from the callsite's arguments to the parent's
541/// arguments. This may be important because inlining can cause information loss
542/// when attribute knowledge disappears with the inlined call.
543static bool addArgumentAttrsFromCallsites(Function &F) {
544 if (!EnableNonnullArgPropagation)
545 return false;
546
547 bool Changed = false;
548
549 // For an argument attribute to transfer from a callsite to the parent, the
550 // call must be guaranteed to execute every time the parent is called.
551 // Conservatively, just check for calls in the entry block that are guaranteed
552 // to execute.
553 // TODO: This could be enhanced by testing if the callsite post-dominates the
554 // entry block or by doing simple forward walks or backward walks to the
555 // callsite.
556 BasicBlock &Entry = F.getEntryBlock();
557 for (Instruction &I : Entry) {
558 if (auto CS = CallSite(&I)) {
559 if (auto *CalledFunc = CS.getCalledFunction()) {
560 for (auto &CSArg : CalledFunc->args()) {
561 if (!CSArg.hasNonNullAttr())
562 continue;
563
564 // If the non-null callsite argument operand is an argument to 'F'
565 // (the caller) and the call is guaranteed to execute, then the value
566 // must be non-null throughout 'F'.
567 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
568 if (FArg && !FArg->hasNonNullAttr()) {
569 FArg->addAttr(Attribute::NonNull);
570 Changed = true;
571 }
572 }
573 }
574 }
575 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
576 break;
577 }
578
579 return Changed;
580}
581
Chandler Carrutha632fb92015-09-13 06:57:25 +0000582/// Deduce nocapture attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000583static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000584 bool Changed = false;
585
Nick Lewycky4c378a42011-12-28 23:24:21 +0000586 ArgumentGraph AG;
587
Duncan Sands44c8cd92008-12-31 16:14:43 +0000588 // Check each function in turn, determining which pointer arguments are not
589 // captured.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000590 for (Function *F : SCCNodes) {
Sanjoy Das5ce32722016-04-08 00:48:30 +0000591 // We can infer and propagate function attributes only when we know that the
592 // definition we'll get at link time is *exactly* the definition we see now.
593 // For more details, see GlobalValue::mayBeDerefined.
594 if (!F->hasExactDefinition())
Duncan Sands44c8cd92008-12-31 16:14:43 +0000595 continue;
596
Sanjay Patel4f742162017-02-13 23:10:51 +0000597 Changed |= addArgumentAttrsFromCallsites(*F);
598
Nick Lewycky4c378a42011-12-28 23:24:21 +0000599 // Functions that are readonly (or readnone) and nounwind and don't return
600 // a value can't capture arguments. Don't analyze them.
601 if (F->onlyReadsMemory() && F->doesNotThrow() &&
602 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000603 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
604 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000605 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000606 A->addAttr(Attribute::NoCapture);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000607 ++NumNoCapture;
608 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000609 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000610 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000611 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000612 }
613
Chandler Carruth63559d72015-09-13 06:47:20 +0000614 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
615 ++A) {
616 if (!A->getType()->isPointerTy())
617 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000618 bool HasNonLocalUses = false;
619 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000620 ArgumentUsesTracker Tracker(SCCNodes);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000621 PointerMayBeCaptured(&*A, &Tracker);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000622 if (!Tracker.Captured) {
623 if (Tracker.Uses.empty()) {
624 // If it's trivially not captured, mark it nocapture now.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000625 A->addAttr(Attribute::NoCapture);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000626 ++NumNoCapture;
627 Changed = true;
628 } else {
629 // If it's not trivially captured and not trivially not captured,
630 // then it must be calling into another function in our SCC. Save
631 // its particulars for Argument-SCC analysis later.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000632 ArgumentGraphNode *Node = AG[&*A];
Benjamin Kramer135f7352016-06-26 12:28:59 +0000633 for (Argument *Use : Tracker.Uses) {
634 Node->Uses.push_back(AG[Use]);
635 if (Use != &*A)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000636 HasNonLocalUses = true;
637 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000638 }
639 }
640 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
641 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000642 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
643 // Can we determine that it's readonly/readnone without doing an SCC?
644 // Note that we don't allow any calls at all here, or else our result
645 // will be dependent on the iteration order through the functions in the
646 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000647 SmallPtrSet<Argument *, 8> Self;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000648 Self.insert(&*A);
649 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000650 if (R != Attribute::None) {
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000651 A->addAttr(R);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000652 Changed = true;
653 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
654 }
655 }
656 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000657 }
658
659 // The graph we've collected is partial because we stopped scanning for
660 // argument uses once we solved the argument trivially. These partial nodes
661 // show up as ArgumentGraphNode objects with an empty Uses list, and for
662 // these nodes the final decision about whether they capture has already been
663 // made. If the definition doesn't have a 'nocapture' attribute by now, it
664 // captures.
665
Chandler Carruth63559d72015-09-13 06:47:20 +0000666 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000667 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000668 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000669 if (!ArgumentSCC[0]->Definition)
670 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000671
672 // eg. "void f(int* x) { if (...) f(x); }"
673 if (ArgumentSCC[0]->Uses.size() == 1 &&
674 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000675 Argument *A = ArgumentSCC[0]->Definition;
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000676 A->addAttr(Attribute::NoCapture);
Nick Lewycky7e820552009-01-02 03:46:56 +0000677 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000678 Changed = true;
679 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000680 continue;
681 }
682
683 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000684 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
685 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000686 ArgumentGraphNode *Node = *I;
687 if (Node->Uses.empty()) {
688 if (!Node->Definition->hasNoCaptureAttr())
689 SCCCaptured = true;
690 }
691 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000692 if (SCCCaptured)
693 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000694
Chandler Carruth63559d72015-09-13 06:47:20 +0000695 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000696 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
697 // quickly looking up whether a given Argument is in this ArgumentSCC.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000698 for (ArgumentGraphNode *I : ArgumentSCC) {
699 ArgumentSCCNodes.insert(I->Definition);
Nick Lewycky4c378a42011-12-28 23:24:21 +0000700 }
701
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000702 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
703 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000704 ArgumentGraphNode *N = *I;
Benjamin Kramer135f7352016-06-26 12:28:59 +0000705 for (ArgumentGraphNode *Use : N->Uses) {
706 Argument *A = Use->Definition;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000707 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
708 continue;
709 SCCCaptured = true;
710 break;
711 }
712 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000713 if (SCCCaptured)
714 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000715
Nick Lewyckyf740db32012-01-05 22:21:45 +0000716 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000717 Argument *A = ArgumentSCC[i]->Definition;
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000718 A->addAttr(Attribute::NoCapture);
Nick Lewycky4c378a42011-12-28 23:24:21 +0000719 ++NumNoCapture;
720 Changed = true;
721 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000722
723 // We also want to compute readonly/readnone. With a small number of false
724 // negatives, we can assume that any pointer which is captured isn't going
725 // to be provably readonly or readnone, since by definition we can't
726 // analyze all uses of a captured pointer.
727 //
728 // The false negatives happen when the pointer is captured by a function
729 // that promises readonly/readnone behaviour on the pointer, then the
730 // pointer's lifetime ends before anything that writes to arbitrary memory.
731 // Also, a readonly/readnone pointer may be returned, but returning a
732 // pointer is capturing it.
733
734 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
735 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
736 Argument *A = ArgumentSCC[i]->Definition;
737 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
738 if (K == Attribute::ReadNone)
739 continue;
740 if (K == Attribute::ReadOnly) {
741 ReadAttr = Attribute::ReadOnly;
742 continue;
743 }
744 ReadAttr = K;
745 break;
746 }
747
748 if (ReadAttr != Attribute::None) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000749 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
750 Argument *A = ArgumentSCC[i]->Definition;
Bjorn Steinbrink236446c2015-05-25 19:46:38 +0000751 // Clear out existing readonly/readnone attributes
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000752 A->removeAttr(Attribute::ReadOnly);
753 A->removeAttr(Attribute::ReadNone);
754 A->addAttr(ReadAttr);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000755 ReadAttr == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
756 Changed = true;
757 }
758 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000759 }
760
761 return Changed;
762}
763
Chandler Carrutha632fb92015-09-13 06:57:25 +0000764/// Tests whether a function is "malloc-like".
765///
766/// A function is "malloc-like" if it returns either null or a pointer that
767/// doesn't alias any other pointer visible to the caller.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000768static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000769 SmallSetVector<Value *, 8> FlowsToReturn;
Benjamin Kramer135f7352016-06-26 12:28:59 +0000770 for (BasicBlock &BB : *F)
771 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000772 FlowsToReturn.insert(Ret->getReturnValue());
773
774 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000775 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000776
777 if (Constant *C = dyn_cast<Constant>(RetVal)) {
778 if (!C->isNullValue() && !isa<UndefValue>(C))
779 return false;
780
781 continue;
782 }
783
784 if (isa<Argument>(RetVal))
785 return false;
786
787 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
788 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000789 // Extend the analysis by looking upwards.
790 case Instruction::BitCast:
791 case Instruction::GetElementPtr:
792 case Instruction::AddrSpaceCast:
793 FlowsToReturn.insert(RVI->getOperand(0));
794 continue;
795 case Instruction::Select: {
796 SelectInst *SI = cast<SelectInst>(RVI);
797 FlowsToReturn.insert(SI->getTrueValue());
798 FlowsToReturn.insert(SI->getFalseValue());
799 continue;
800 }
801 case Instruction::PHI: {
802 PHINode *PN = cast<PHINode>(RVI);
803 for (Value *IncValue : PN->incoming_values())
804 FlowsToReturn.insert(IncValue);
805 continue;
806 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000807
Chandler Carruth63559d72015-09-13 06:47:20 +0000808 // Check whether the pointer came from an allocation.
809 case Instruction::Alloca:
810 break;
811 case Instruction::Call:
812 case Instruction::Invoke: {
813 CallSite CS(RVI);
Reid Klecknerfb502d22017-04-14 20:19:02 +0000814 if (CS.hasRetAttr(Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000815 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000816 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
817 break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000818 LLVM_FALLTHROUGH;
819 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000820 default:
821 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000822 }
823
Dan Gohman94e61762009-11-19 21:57:48 +0000824 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000825 return false;
826 }
827
828 return true;
829}
830
Chandler Carrutha632fb92015-09-13 06:57:25 +0000831/// Deduce noalias attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000832static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000833 // Check each function in turn, determining which functions return noalias
834 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000835 for (Function *F : SCCNodes) {
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000836 // Already noalias.
Reid Klecknera0b45f42017-05-03 18:17:31 +0000837 if (F->returnDoesNotAlias())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000838 continue;
839
Sanjoy Das5ce32722016-04-08 00:48:30 +0000840 // We can infer and propagate function attributes only when we know that the
841 // definition we'll get at link time is *exactly* the definition we see now.
842 // For more details, see GlobalValue::mayBeDerefined.
843 if (!F->hasExactDefinition())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000844 return false;
845
Chandler Carruth63559d72015-09-13 06:47:20 +0000846 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000847 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000848 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000849 continue;
850
Chandler Carruth3824f852015-09-13 08:23:27 +0000851 if (!isFunctionMallocLike(F, SCCNodes))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000852 return false;
853 }
854
855 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000856 for (Function *F : SCCNodes) {
Reid Klecknera0b45f42017-05-03 18:17:31 +0000857 if (F->returnDoesNotAlias() ||
Reid Kleckner6652a522017-04-28 18:37:16 +0000858 !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000859 continue;
860
Reid Klecknera0b45f42017-05-03 18:17:31 +0000861 F->setReturnDoesNotAlias();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000862 ++NumNoAlias;
863 MadeChange = true;
864 }
865
866 return MadeChange;
867}
868
Chandler Carrutha632fb92015-09-13 06:57:25 +0000869/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000870///
871/// Requires that the function returns a pointer.
872///
873/// Returns true if it believes the function will not return a null, and sets
874/// \p Speculative based on whether the returned conclusion is a speculative
875/// conclusion due to SCC calls.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000876static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
Sean Silva45835e72016-07-02 23:47:27 +0000877 bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000878 assert(F->getReturnType()->isPointerTy() &&
879 "nonnull only meaningful on pointer types");
880 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000881
Philip Reamesa88caea2015-08-31 19:44:38 +0000882 SmallSetVector<Value *, 8> FlowsToReturn;
883 for (BasicBlock &BB : *F)
884 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
885 FlowsToReturn.insert(Ret->getReturnValue());
886
887 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
888 Value *RetVal = FlowsToReturn[i];
889
890 // If this value is locally known to be non-null, we're good
Sean Silva45835e72016-07-02 23:47:27 +0000891 if (isKnownNonNull(RetVal))
Philip Reamesa88caea2015-08-31 19:44:38 +0000892 continue;
893
894 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000895 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000896 Instruction *RVI = dyn_cast<Instruction>(RetVal);
897 if (!RVI)
898 return false;
899 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000900 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000901 case Instruction::BitCast:
902 case Instruction::GetElementPtr:
903 case Instruction::AddrSpaceCast:
904 FlowsToReturn.insert(RVI->getOperand(0));
905 continue;
906 case Instruction::Select: {
907 SelectInst *SI = cast<SelectInst>(RVI);
908 FlowsToReturn.insert(SI->getTrueValue());
909 FlowsToReturn.insert(SI->getFalseValue());
910 continue;
911 }
912 case Instruction::PHI: {
913 PHINode *PN = cast<PHINode>(RVI);
914 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
915 FlowsToReturn.insert(PN->getIncomingValue(i));
916 continue;
917 }
918 case Instruction::Call:
919 case Instruction::Invoke: {
920 CallSite CS(RVI);
921 Function *Callee = CS.getCalledFunction();
922 // A call to a node within the SCC is assumed to return null until
923 // proven otherwise
924 if (Callee && SCCNodes.count(Callee)) {
925 Speculative = true;
926 continue;
927 }
928 return false;
929 }
930 default:
Chandler Carruth63559d72015-09-13 06:47:20 +0000931 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +0000932 };
933 llvm_unreachable("should have either continued or returned");
934 }
935
936 return true;
937}
938
Chandler Carrutha632fb92015-09-13 06:57:25 +0000939/// Deduce nonnull attributes for the SCC.
Sean Silva45835e72016-07-02 23:47:27 +0000940static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000941 // Speculative that all functions in the SCC return only nonnull
942 // pointers. We may refute this as we analyze functions.
943 bool SCCReturnsNonNull = true;
944
945 bool MadeChange = false;
946
947 // Check each function in turn, determining which functions return nonnull
948 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000949 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000950 // Already nonnull.
Reid Klecknerb5180542017-03-21 16:57:19 +0000951 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
Philip Reamesa88caea2015-08-31 19:44:38 +0000952 Attribute::NonNull))
953 continue;
954
Sanjoy Das5ce32722016-04-08 00:48:30 +0000955 // We can infer and propagate function attributes only when we know that the
956 // definition we'll get at link time is *exactly* the definition we see now.
957 // For more details, see GlobalValue::mayBeDerefined.
958 if (!F->hasExactDefinition())
Philip Reamesa88caea2015-08-31 19:44:38 +0000959 return false;
960
Chandler Carruth63559d72015-09-13 06:47:20 +0000961 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +0000962 // pointer types.
963 if (!F->getReturnType()->isPointerTy())
964 continue;
965
966 bool Speculative = false;
Sean Silva45835e72016-07-02 23:47:27 +0000967 if (isReturnNonNull(F, SCCNodes, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000968 if (!Speculative) {
969 // Mark the function eagerly since we may discover a function
970 // which prevents us from speculating about the entire SCC
971 DEBUG(dbgs() << "Eagerly marking " << F->getName() << " as nonnull\n");
Reid Klecknerb5180542017-03-21 16:57:19 +0000972 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
Philip Reamesa88caea2015-08-31 19:44:38 +0000973 ++NumNonNullReturn;
974 MadeChange = true;
975 }
976 continue;
977 }
978 // At least one function returns something which could be null, can't
979 // speculate any more.
980 SCCReturnsNonNull = false;
981 }
982
983 if (SCCReturnsNonNull) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000984 for (Function *F : SCCNodes) {
Reid Klecknerb5180542017-03-21 16:57:19 +0000985 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
Philip Reamesa88caea2015-08-31 19:44:38 +0000986 Attribute::NonNull) ||
987 !F->getReturnType()->isPointerTy())
988 continue;
989
990 DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
Reid Klecknerb5180542017-03-21 16:57:19 +0000991 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
Philip Reamesa88caea2015-08-31 19:44:38 +0000992 ++NumNonNullReturn;
993 MadeChange = true;
994 }
995 }
996
997 return MadeChange;
998}
999
Justin Lebar9d943972016-03-14 20:18:54 +00001000/// Remove the convergent attribute from all functions in the SCC if every
1001/// callsite within the SCC is not convergent (except for calls to functions
1002/// within the SCC). Returns true if changes were made.
Chandler Carruth3937bc72016-02-12 09:47:49 +00001003static bool removeConvergentAttrs(const SCCNodeSet &SCCNodes) {
Justin Lebar9d943972016-03-14 20:18:54 +00001004 // For every function in SCC, ensure that either
1005 // * it is not convergent, or
1006 // * we can remove its convergent attribute.
1007 bool HasConvergentFn = false;
Chandler Carruth3937bc72016-02-12 09:47:49 +00001008 for (Function *F : SCCNodes) {
Justin Lebar9d943972016-03-14 20:18:54 +00001009 if (!F->isConvergent()) continue;
1010 HasConvergentFn = true;
1011
1012 // Can't remove convergent from function declarations.
1013 if (F->isDeclaration()) return false;
1014
1015 // Can't remove convergent if any of our functions has a convergent call to a
1016 // function not in the SCC.
1017 for (Instruction &I : instructions(*F)) {
1018 CallSite CS(&I);
1019 // Bail if CS is a convergent call to a function not in the SCC.
1020 if (CS && CS.isConvergent() &&
1021 SCCNodes.count(CS.getCalledFunction()) == 0)
1022 return false;
1023 }
1024 }
1025
1026 // If the SCC doesn't have any convergent functions, we have nothing to do.
1027 if (!HasConvergentFn) return false;
1028
1029 // If we got here, all of the calls the SCC makes to functions not in the SCC
1030 // are non-convergent. Therefore all of the SCC's functions can also be made
1031 // non-convergent. We'll remove the attr from the callsites in
1032 // InstCombineCalls.
1033 for (Function *F : SCCNodes) {
1034 if (!F->isConvergent()) continue;
1035
1036 DEBUG(dbgs() << "Removing convergent attr from fn " << F->getName()
1037 << "\n");
Chandler Carruth3937bc72016-02-12 09:47:49 +00001038 F->setNotConvergent();
1039 }
Justin Lebar260854b2016-02-09 23:03:22 +00001040 return true;
1041}
1042
James Molloy7e9bdd52015-11-12 10:55:20 +00001043static bool setDoesNotRecurse(Function &F) {
1044 if (F.doesNotRecurse())
1045 return false;
1046 F.setDoesNotRecurse();
1047 ++NumNoRecurse;
1048 return true;
1049}
1050
Chandler Carruth632d2082016-02-13 08:47:51 +00001051static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
James Molloy7e9bdd52015-11-12 10:55:20 +00001052 // Try and identify functions that do not recurse.
1053
1054 // If the SCC contains multiple nodes we know for sure there is recursion.
Chandler Carruth632d2082016-02-13 08:47:51 +00001055 if (SCCNodes.size() != 1)
James Molloy7e9bdd52015-11-12 10:55:20 +00001056 return false;
1057
Chandler Carruth632d2082016-02-13 08:47:51 +00001058 Function *F = *SCCNodes.begin();
James Molloy7e9bdd52015-11-12 10:55:20 +00001059 if (!F || F->isDeclaration() || F->doesNotRecurse())
1060 return false;
1061
1062 // If all of the calls in F are identifiable and are to norecurse functions, F
1063 // is norecurse. This check also detects self-recursion as F is not currently
1064 // marked norecurse, so any called from F to F will not be marked norecurse.
Chandler Carruth632d2082016-02-13 08:47:51 +00001065 for (Instruction &I : instructions(*F))
1066 if (auto CS = CallSite(&I)) {
1067 Function *Callee = CS.getCalledFunction();
1068 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1069 // Function calls a potentially recursive function.
1070 return false;
1071 }
James Molloy7e9bdd52015-11-12 10:55:20 +00001072
Chandler Carruth632d2082016-02-13 08:47:51 +00001073 // Every call was to a non-recursive function other than this function, and
1074 // we have no indirect recursion as the SCC size is one. This function cannot
1075 // recurse.
1076 return setDoesNotRecurse(*F);
James Molloy7e9bdd52015-11-12 10:55:20 +00001077}
1078
Chandler Carruthb47f8012016-03-11 11:05:24 +00001079PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
Chandler Carruth88823462016-08-24 09:37:14 +00001080 CGSCCAnalysisManager &AM,
1081 LazyCallGraph &CG,
1082 CGSCCUpdateResult &) {
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001083 FunctionAnalysisManager &FAM =
Chandler Carruth88823462016-08-24 09:37:14 +00001084 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001085
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001086 // We pass a lambda into functions to wire them up to the analysis manager
1087 // for getting function analyses.
1088 auto AARGetter = [&](Function &F) -> AAResults & {
1089 return FAM.getResult<AAManager>(F);
1090 };
1091
1092 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1093 // any external or opt-none nodes that will prevent us from optimizing any
1094 // part of the SCC.
1095 SCCNodeSet SCCNodes;
1096 bool HasUnknownCall = false;
1097 for (LazyCallGraph::Node &N : C) {
1098 Function &F = N.getFunction();
1099 if (F.hasFnAttribute(Attribute::OptimizeNone)) {
1100 // Treat any function we're trying not to optimize as if it were an
1101 // indirect call and omit it from the node set used below.
1102 HasUnknownCall = true;
1103 continue;
1104 }
1105 // Track whether any functions in this SCC have an unknown call edge.
1106 // Note: if this is ever a performance hit, we can common it with
1107 // subsequent routines which also do scans over the instructions of the
1108 // function.
1109 if (!HasUnknownCall)
1110 for (Instruction &I : instructions(F))
1111 if (auto CS = CallSite(&I))
1112 if (!CS.getCalledFunction()) {
1113 HasUnknownCall = true;
1114 break;
1115 }
1116
1117 SCCNodes.insert(&F);
1118 }
1119
1120 bool Changed = false;
David Majnemer5246e0b2016-07-19 18:50:26 +00001121 Changed |= addArgumentReturnedAttrs(SCCNodes);
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001122 Changed |= addReadAttrs(SCCNodes, AARGetter);
1123 Changed |= addArgumentAttrs(SCCNodes);
1124
1125 // If we have no external nodes participating in the SCC, we can deduce some
1126 // more precise attributes as well.
1127 if (!HasUnknownCall) {
1128 Changed |= addNoAliasAttrs(SCCNodes);
Sean Silva45835e72016-07-02 23:47:27 +00001129 Changed |= addNonNullAttrs(SCCNodes);
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001130 Changed |= removeConvergentAttrs(SCCNodes);
1131 Changed |= addNoRecurseAttrs(SCCNodes);
1132 }
1133
1134 return Changed ? PreservedAnalyses::none() : PreservedAnalyses::all();
1135}
1136
1137namespace {
1138struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
1139 static char ID; // Pass identification, replacement for typeid
1140 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
Chad Rosier611b73b2016-11-07 16:28:04 +00001141 initializePostOrderFunctionAttrsLegacyPassPass(
1142 *PassRegistry::getPassRegistry());
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001143 }
1144
1145 bool runOnSCC(CallGraphSCC &SCC) override;
1146
1147 void getAnalysisUsage(AnalysisUsage &AU) const override {
1148 AU.setPreservesCFG();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001149 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruth12884f72016-03-02 15:56:53 +00001150 getAAResultsAnalysisUsage(AU);
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001151 CallGraphSCCPass::getAnalysisUsage(AU);
1152 }
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001153};
1154}
1155
1156char PostOrderFunctionAttrsLegacyPass::ID = 0;
1157INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1158 "Deduce function attributes", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001159INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001160INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001161INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1162 "Deduce function attributes", false, false)
1163
Chad Rosier611b73b2016-11-07 16:28:04 +00001164Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1165 return new PostOrderFunctionAttrsLegacyPass();
1166}
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001167
Sean Silva997cbea2016-07-03 03:35:03 +00001168template <typename AARGetterT>
1169static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
Chandler Carruthcada2d82015-10-31 00:28:37 +00001170 bool Changed = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001171
1172 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1173 // whether a given CallGraphNode is in this SCC. Also track whether there are
1174 // any external or opt-none nodes that will prevent us from optimizing any
1175 // part of the SCC.
1176 SCCNodeSet SCCNodes;
1177 bool ExternalNode = false;
Benjamin Kramer135f7352016-06-26 12:28:59 +00001178 for (CallGraphNode *I : SCC) {
1179 Function *F = I->getFunction();
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001180 if (!F || F->hasFnAttribute(Attribute::OptimizeNone)) {
1181 // External node or function we're trying not to optimize - we both avoid
1182 // transform them and avoid leveraging information they provide.
1183 ExternalNode = true;
1184 continue;
1185 }
1186
1187 SCCNodes.insert(F);
1188 }
1189
David Blaikie6aeacaa2017-06-02 21:24:17 +00001190 // Skip it if the SCC only contains optnone functions.
1191 if (SCCNodes.empty())
1192 return Changed;
1193
David Majnemer5246e0b2016-07-19 18:50:26 +00001194 Changed |= addArgumentReturnedAttrs(SCCNodes);
Chandler Carrutha8125352015-10-30 16:48:08 +00001195 Changed |= addReadAttrs(SCCNodes, AARGetter);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001196 Changed |= addArgumentAttrs(SCCNodes);
1197
Chandler Carruth3a040e62015-12-27 08:41:34 +00001198 // If we have no external nodes participating in the SCC, we can deduce some
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001199 // more precise attributes as well.
1200 if (!ExternalNode) {
1201 Changed |= addNoAliasAttrs(SCCNodes);
Sean Silva45835e72016-07-02 23:47:27 +00001202 Changed |= addNonNullAttrs(SCCNodes);
Chandler Carruth3937bc72016-02-12 09:47:49 +00001203 Changed |= removeConvergentAttrs(SCCNodes);
Chandler Carruth632d2082016-02-13 08:47:51 +00001204 Changed |= addNoRecurseAttrs(SCCNodes);
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001205 }
Chandler Carruth1926b702016-01-08 10:55:52 +00001206
James Molloy7e9bdd52015-11-12 10:55:20 +00001207 return Changed;
1208}
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001209
Sean Silva997cbea2016-07-03 03:35:03 +00001210bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1211 if (skipSCC(SCC))
1212 return false;
Peter Collingbournecea1e4e2017-02-09 23:11:52 +00001213 return runImpl(SCC, LegacyAARGetter(*this));
Sean Silva997cbea2016-07-03 03:35:03 +00001214}
1215
Chandler Carruth1926b702016-01-08 10:55:52 +00001216namespace {
Sean Silvaf5080192016-06-12 07:48:51 +00001217struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
Chandler Carruth1926b702016-01-08 10:55:52 +00001218 static char ID; // Pass identification, replacement for typeid
Sean Silvaf5080192016-06-12 07:48:51 +00001219 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
Chad Rosier611b73b2016-11-07 16:28:04 +00001220 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1221 *PassRegistry::getPassRegistry());
Chandler Carruth1926b702016-01-08 10:55:52 +00001222 }
1223
1224 bool runOnModule(Module &M) override;
1225
1226 void getAnalysisUsage(AnalysisUsage &AU) const override {
1227 AU.setPreservesCFG();
1228 AU.addRequired<CallGraphWrapperPass>();
Mehdi Amini0ddf4042016-05-02 18:03:33 +00001229 AU.addPreserved<CallGraphWrapperPass>();
Chandler Carruth1926b702016-01-08 10:55:52 +00001230 }
1231};
1232}
1233
Sean Silvaf5080192016-06-12 07:48:51 +00001234char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
1235INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
Chandler Carruth1926b702016-01-08 10:55:52 +00001236 "Deduce function attributes in RPO", false, false)
1237INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Sean Silvaf5080192016-06-12 07:48:51 +00001238INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
Chandler Carruth1926b702016-01-08 10:55:52 +00001239 "Deduce function attributes in RPO", false, false)
1240
1241Pass *llvm::createReversePostOrderFunctionAttrsPass() {
Sean Silvaf5080192016-06-12 07:48:51 +00001242 return new ReversePostOrderFunctionAttrsLegacyPass();
Chandler Carruth1926b702016-01-08 10:55:52 +00001243}
1244
1245static bool addNoRecurseAttrsTopDown(Function &F) {
1246 // We check the preconditions for the function prior to calling this to avoid
1247 // the cost of building up a reversible post-order list. We assert them here
1248 // to make sure none of the invariants this relies on were violated.
1249 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1250 assert(!F.doesNotRecurse() &&
1251 "This function has already been deduced as norecurs!");
1252 assert(F.hasInternalLinkage() &&
1253 "Can only do top-down deduction for internal linkage functions!");
1254
1255 // If F is internal and all of its uses are calls from a non-recursive
1256 // functions, then none of its calls could in fact recurse without going
1257 // through a function marked norecurse, and so we can mark this function too
1258 // as norecurse. Note that the uses must actually be calls -- otherwise
1259 // a pointer to this function could be returned from a norecurse function but
1260 // this function could be recursively (indirectly) called. Note that this
1261 // also detects if F is directly recursive as F is not yet marked as
1262 // a norecurse function.
1263 for (auto *U : F.users()) {
1264 auto *I = dyn_cast<Instruction>(U);
1265 if (!I)
1266 return false;
1267 CallSite CS(I);
1268 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1269 return false;
1270 }
1271 return setDoesNotRecurse(F);
1272}
1273
Sean Silvaadc79392016-06-12 05:44:51 +00001274static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
Chandler Carruth1926b702016-01-08 10:55:52 +00001275 // We only have a post-order SCC traversal (because SCCs are inherently
1276 // discovered in post-order), so we accumulate them in a vector and then walk
1277 // it in reverse. This is simpler than using the RPO iterator infrastructure
1278 // because we need to combine SCC detection and the PO walk of the call
1279 // graph. We can also cheat egregiously because we're primarily interested in
1280 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1281 // with multiple functions in them will clearly be recursive.
Chandler Carruth1926b702016-01-08 10:55:52 +00001282 SmallVector<Function *, 16> Worklist;
1283 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1284 if (I->size() != 1)
1285 continue;
1286
1287 Function *F = I->front()->getFunction();
1288 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1289 F->hasInternalLinkage())
1290 Worklist.push_back(F);
1291 }
1292
James Molloy7e9bdd52015-11-12 10:55:20 +00001293 bool Changed = false;
Chandler Carruth1926b702016-01-08 10:55:52 +00001294 for (auto *F : reverse(Worklist))
1295 Changed |= addNoRecurseAttrsTopDown(*F);
1296
Duncan Sands44c8cd92008-12-31 16:14:43 +00001297 return Changed;
1298}
Sean Silvaadc79392016-06-12 05:44:51 +00001299
Sean Silvaf5080192016-06-12 07:48:51 +00001300bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
Sean Silvaadc79392016-06-12 05:44:51 +00001301 if (skipModule(M))
1302 return false;
1303
1304 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1305
1306 return deduceFunctionAttributeInRPO(M, CG);
1307}
Sean Silvaf5080192016-06-12 07:48:51 +00001308
1309PreservedAnalyses
Sean Silvafd03ac62016-08-09 00:28:38 +00001310ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
Sean Silvaf5080192016-06-12 07:48:51 +00001311 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1312
Chandler Carruth6acdca72017-01-24 12:55:57 +00001313 if (!deduceFunctionAttributeInRPO(M, CG))
Sean Silvaf5080192016-06-12 07:48:51 +00001314 return PreservedAnalyses::all();
Chandler Carruth6acdca72017-01-24 12:55:57 +00001315
Sean Silvaf5080192016-06-12 07:48:51 +00001316 PreservedAnalyses PA;
1317 PA.preserve<CallGraphAnalysis>();
1318 return PA;
1319}