blob: b6d0b2e35694ba0f4dc10e55b77b709dc68d6e0c [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//
Chandler Carruth2946cd72019-01-19 08:50:56 +00003// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4// See https://llvm.org/LICENSE.txt for license information.
5// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
Duncan Sands44c8cd92008-12-31 16:14:43 +00006//
7//===----------------------------------------------------------------------===//
Eugene Zelenkof27d1612017-10-19 21:21:30 +00008//
Chandler Carruth1926b702016-01-08 10:55:52 +00009/// \file
10/// This file implements interprocedural passes which walk the
11/// call-graph deducing and/or propagating function attributes.
Eugene Zelenkof27d1612017-10-19 21:21:30 +000012//
Duncan Sands44c8cd92008-12-31 16:14:43 +000013//===----------------------------------------------------------------------===//
14
Chandler Carruth9c4ed172016-02-18 11:03:11 +000015#include "llvm/Transforms/IPO/FunctionAttrs.h"
Nick Lewycky4c378a42011-12-28 23:24:21 +000016#include "llvm/ADT/SCCIterator.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000017#include "llvm/ADT/STLExtras.h"
Benjamin Kramer15591272012-10-31 13:45:49 +000018#include "llvm/ADT/SetVector.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000019#include "llvm/ADT/SmallPtrSet.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000020#include "llvm/ADT/SmallVector.h"
Duncan Sands44c8cd92008-12-31 16:14:43 +000021#include "llvm/ADT/Statistic.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"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000025#include "llvm/Analysis/CGSCCPassManager.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000026#include "llvm/Analysis/CallGraph.h"
Chandler Carruth839a98e2013-01-07 15:26:48 +000027#include "llvm/Analysis/CallGraphSCCPass.h"
Chandler Carruthed0881b2012-12-03 16:50:05 +000028#include "llvm/Analysis/CaptureTracking.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000029#include "llvm/Analysis/LazyCallGraph.h"
Brian Homerdingb4b21d82019-07-08 15:57:56 +000030#include "llvm/Analysis/MemoryBuiltins.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000031#include "llvm/Analysis/MemoryLocation.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000032#include "llvm/Analysis/ValueTracking.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000033#include "llvm/IR/Argument.h"
34#include "llvm/IR/Attributes.h"
35#include "llvm/IR/BasicBlock.h"
36#include "llvm/IR/CallSite.h"
37#include "llvm/IR/Constant.h"
38#include "llvm/IR/Constants.h"
39#include "llvm/IR/Function.h"
Chandler Carruth83948572014-03-04 10:30:26 +000040#include "llvm/IR/InstIterator.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000041#include "llvm/IR/InstrTypes.h"
42#include "llvm/IR/Instruction.h"
43#include "llvm/IR/Instructions.h"
Christian Bruel4ead99b2018-12-05 16:48:00 +000044#include "llvm/IR/IntrinsicInst.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000045#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"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080051#include "llvm/InitializePasses.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000052#include "llvm/Pass.h"
53#include "llvm/Support/Casting.h"
54#include "llvm/Support/CommandLine.h"
55#include "llvm/Support/Compiler.h"
Philip Reamesa88caea2015-08-31 19:44:38 +000056#include "llvm/Support/Debug.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000057#include "llvm/Support/ErrorHandling.h"
Hans Wennborg043bf5b2015-08-31 21:19:18 +000058#include "llvm/Support/raw_ostream.h"
Chandler Carruth6bda14b2017-06-06 11:49:48 +000059#include "llvm/Transforms/IPO.h"
Eugene Zelenkof27d1612017-10-19 21:21:30 +000060#include <cassert>
61#include <iterator>
62#include <map>
63#include <vector>
64
Duncan Sands44c8cd92008-12-31 16:14:43 +000065using namespace llvm;
66
Chandler Carruth964daaa2014-04-22 02:55:47 +000067#define DEBUG_TYPE "functionattrs"
68
Duncan Sands44c8cd92008-12-31 16:14:43 +000069STATISTIC(NumReadNone, "Number of functions marked readnone");
70STATISTIC(NumReadOnly, "Number of functions marked readonly");
Brian Homerding3ecabd72018-08-23 15:05:22 +000071STATISTIC(NumWriteOnly, "Number of functions marked writeonly");
Duncan Sands44c8cd92008-12-31 16:14:43 +000072STATISTIC(NumNoCapture, "Number of arguments marked nocapture");
David Majnemer5246e0b2016-07-19 18:50:26 +000073STATISTIC(NumReturned, "Number of arguments marked returned");
Nick Lewyckyc2ec0722013-07-06 00:29:58 +000074STATISTIC(NumReadNoneArg, "Number of arguments marked readnone");
75STATISTIC(NumReadOnlyArg, "Number of arguments marked readonly");
Nick Lewyckyfbed86a2009-03-08 06:20:47 +000076STATISTIC(NumNoAlias, "Number of function returns marked noalias");
Philip Reamesa88caea2015-08-31 19:44:38 +000077STATISTIC(NumNonNullReturn, "Number of function returns marked nonnull");
James Molloy7e9bdd52015-11-12 10:55:20 +000078STATISTIC(NumNoRecurse, "Number of functions marked as norecurse");
Fedor Sergeev6660fd02018-03-23 21:46:16 +000079STATISTIC(NumNoUnwind, "Number of functions marked as nounwind");
Brian Homerdingb4b21d82019-07-08 15:57:56 +000080STATISTIC(NumNoFree, "Number of functions marked as nofree");
Duncan Sands44c8cd92008-12-31 16:14:43 +000081
Sanjay Patel4f742162017-02-13 23:10:51 +000082static cl::opt<bool> EnableNonnullArgPropagation(
David Bolvanskyd90fd412019-09-23 09:58:02 +000083 "enable-nonnull-arg-prop", cl::init(true), cl::Hidden,
Sanjay Patel4f742162017-02-13 23:10:51 +000084 cl::desc("Try to propagate nonnull argument attributes from callsites to "
85 "caller functions."));
86
Fedor Sergeev6660fd02018-03-23 21:46:16 +000087static cl::opt<bool> DisableNoUnwindInference(
88 "disable-nounwind-inference", cl::Hidden,
89 cl::desc("Stop inferring nounwind attribute during function-attrs pass"));
90
Brian Homerdingb4b21d82019-07-08 15:57:56 +000091static cl::opt<bool> DisableNoFreeInference(
92 "disable-nofree-inference", cl::Hidden,
93 cl::desc("Stop inferring nofree attribute during function-attrs pass"));
94
Duncan Sands44c8cd92008-12-31 16:14:43 +000095namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +000096
97using SCCNodeSet = SmallSetVector<Function *, 8>;
98
99} // end anonymous namespace
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000100
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000101/// Returns the memory access attribute for function F using AAR for AA results,
102/// where SCCNodes is the current SCC.
103///
104/// If ThisBody is true, this function may examine the function body and will
105/// return a result pertaining to this copy of the function. If it is false, the
106/// result will be based only on AA results for the function declaration; it
107/// will be assumed that some other (perhaps less optimized) version of the
108/// function may be selected at link time.
109static MemoryAccessKind checkFunctionMemoryAccess(Function &F, bool ThisBody,
110 AAResults &AAR,
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000111 const SCCNodeSet &SCCNodes) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000112 FunctionModRefBehavior MRB = AAR.getModRefBehavior(&F);
113 if (MRB == FMRB_DoesNotAccessMemory)
114 // Already perfect!
115 return MAK_ReadNone;
116
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000117 if (!ThisBody) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000118 if (AliasAnalysis::onlyReadsMemory(MRB))
119 return MAK_ReadOnly;
120
Brian Homerding3ecabd72018-08-23 15:05:22 +0000121 if (AliasAnalysis::doesNotReadMemory(MRB))
122 return MAK_WriteOnly;
123
124 // Conservatively assume it reads and writes to memory.
Chandler Carruth7542d372015-09-21 17:39:41 +0000125 return MAK_MayWrite;
126 }
127
128 // Scan the function body for instructions that may read or write memory.
129 bool ReadsMemory = false;
Brian Homerding3ecabd72018-08-23 15:05:22 +0000130 bool WritesMemory = false;
Chandler Carruth7542d372015-09-21 17:39:41 +0000131 for (inst_iterator II = inst_begin(F), E = inst_end(F); II != E; ++II) {
132 Instruction *I = &*II;
133
134 // Some instructions can be ignored even if they read or write memory.
135 // Detect these now, skipping to the next instruction if one is found.
Chandler Carruth363ac682019-01-07 05:42:51 +0000136 if (auto *Call = dyn_cast<CallBase>(I)) {
Sanjoy Das10c8a042016-02-09 18:40:40 +0000137 // Ignore calls to functions in the same SCC, as long as the call sites
138 // don't have operand bundles. Calls with operand bundles are allowed to
139 // have memory effects not described by the memory effects of the call
140 // target.
Chandler Carruth363ac682019-01-07 05:42:51 +0000141 if (!Call->hasOperandBundles() && Call->getCalledFunction() &&
142 SCCNodes.count(Call->getCalledFunction()))
Chandler Carruth7542d372015-09-21 17:39:41 +0000143 continue;
Chandler Carruth363ac682019-01-07 05:42:51 +0000144 FunctionModRefBehavior MRB = AAR.getModRefBehavior(Call);
Alina Sbirlea63d22502017-12-05 20:12:23 +0000145 ModRefInfo MRI = createModRefInfo(MRB);
Chandler Carruth7542d372015-09-21 17:39:41 +0000146
Chandler Carruth69798fb2015-10-27 01:41:43 +0000147 // If the call doesn't access memory, we're done.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000148 if (isNoModRef(MRI))
Chandler Carruth69798fb2015-10-27 01:41:43 +0000149 continue;
150
151 if (!AliasAnalysis::onlyAccessesArgPointees(MRB)) {
Brian Homerding3ecabd72018-08-23 15:05:22 +0000152 // The call could access any memory. If that includes writes, note it.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000153 if (isModSet(MRI))
Brian Homerding3ecabd72018-08-23 15:05:22 +0000154 WritesMemory = true;
Chandler Carruth69798fb2015-10-27 01:41:43 +0000155 // If it reads, note it.
Alina Sbirlea63d22502017-12-05 20:12:23 +0000156 if (isRefSet(MRI))
Chandler Carruth69798fb2015-10-27 01:41:43 +0000157 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000158 continue;
159 }
Chandler Carruth69798fb2015-10-27 01:41:43 +0000160
161 // Check whether all pointer arguments point to local memory, and
162 // ignore calls that only access local memory.
Chandler Carruth363ac682019-01-07 05:42:51 +0000163 for (CallSite::arg_iterator CI = Call->arg_begin(), CE = Call->arg_end();
Chandler Carruth69798fb2015-10-27 01:41:43 +0000164 CI != CE; ++CI) {
165 Value *Arg = *CI;
Elena Demikhovsky3ec9e152015-11-17 19:30:51 +0000166 if (!Arg->getType()->isPtrOrPtrVectorTy())
Chandler Carruth69798fb2015-10-27 01:41:43 +0000167 continue;
168
169 AAMDNodes AAInfo;
170 I->getAAMetadata(AAInfo);
George Burgess IV6ef80022018-10-10 21:28:44 +0000171 MemoryLocation Loc(Arg, LocationSize::unknown(), AAInfo);
Chandler Carruth69798fb2015-10-27 01:41:43 +0000172
173 // Skip accesses to local or constant memory as they don't impact the
174 // externally visible mod/ref behavior.
175 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
176 continue;
177
Alina Sbirlea63d22502017-12-05 20:12:23 +0000178 if (isModSet(MRI))
Brian Homerding3ecabd72018-08-23 15:05:22 +0000179 // Writes non-local memory.
180 WritesMemory = true;
Alina Sbirlea63d22502017-12-05 20:12:23 +0000181 if (isRefSet(MRI))
Chandler Carruth69798fb2015-10-27 01:41:43 +0000182 // Ok, it reads non-local memory.
183 ReadsMemory = true;
184 }
Chandler Carruth7542d372015-09-21 17:39:41 +0000185 continue;
186 } else if (LoadInst *LI = dyn_cast<LoadInst>(I)) {
187 // Ignore non-volatile loads from local memory. (Atomic is okay here.)
188 if (!LI->isVolatile()) {
189 MemoryLocation Loc = MemoryLocation::get(LI);
190 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
191 continue;
192 }
193 } else if (StoreInst *SI = dyn_cast<StoreInst>(I)) {
194 // Ignore non-volatile stores to local memory. (Atomic is okay here.)
195 if (!SI->isVolatile()) {
196 MemoryLocation Loc = MemoryLocation::get(SI);
197 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
198 continue;
199 }
200 } else if (VAArgInst *VI = dyn_cast<VAArgInst>(I)) {
201 // Ignore vaargs on local memory.
202 MemoryLocation Loc = MemoryLocation::get(VI);
203 if (AAR.pointsToConstantMemory(Loc, /*OrLocal=*/true))
204 continue;
205 }
206
207 // Any remaining instructions need to be taken seriously! Check if they
208 // read or write memory.
Brian Homerding3ecabd72018-08-23 15:05:22 +0000209 //
210 // Writes memory, remember that.
211 WritesMemory |= I->mayWriteToMemory();
Chandler Carruth7542d372015-09-21 17:39:41 +0000212
213 // If this instruction may read memory, remember that.
214 ReadsMemory |= I->mayReadFromMemory();
215 }
216
Brian Homerding3ecabd72018-08-23 15:05:22 +0000217 if (WritesMemory) {
218 if (!ReadsMemory)
219 return MAK_WriteOnly;
220 else
221 return MAK_MayWrite;
222 }
223
Chandler Carruth7542d372015-09-21 17:39:41 +0000224 return ReadsMemory ? MAK_ReadOnly : MAK_ReadNone;
225}
226
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000227MemoryAccessKind llvm::computeFunctionBodyMemoryAccess(Function &F,
228 AAResults &AAR) {
229 return checkFunctionMemoryAccess(F, /*ThisBody=*/true, AAR, {});
230}
231
Chandler Carrutha632fb92015-09-13 06:57:25 +0000232/// Deduce readonly/readnone attributes for the SCC.
Chandler Carrutha8125352015-10-30 16:48:08 +0000233template <typename AARGetterT>
Peter Collingbournecea1e4e2017-02-09 23:11:52 +0000234static bool addReadAttrs(const SCCNodeSet &SCCNodes, AARGetterT &&AARGetter) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000235 // Check if any of the functions in the SCC read or write memory. If they
236 // write memory then they can't be marked readnone or readonly.
237 bool ReadsMemory = false;
Brian Homerding3ecabd72018-08-23 15:05:22 +0000238 bool WritesMemory = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000239 for (Function *F : SCCNodes) {
Chandler Carrutha8125352015-10-30 16:48:08 +0000240 // Call the callable parameter to look up AA results for this function.
241 AAResults &AAR = AARGetter(*F);
Chandler Carruth7b560d42015-09-09 17:55:00 +0000242
Peter Collingbournec45f7f32017-02-14 00:28:13 +0000243 // Non-exact function definitions may not be selected at link time, and an
244 // alternative version that writes to memory may be selected. See the
245 // comment on GlobalValue::isDefinitionExact for more details.
246 switch (checkFunctionMemoryAccess(*F, F->hasExactDefinition(),
247 AAR, SCCNodes)) {
Chandler Carruth7542d372015-09-21 17:39:41 +0000248 case MAK_MayWrite:
249 return false;
250 case MAK_ReadOnly:
Duncan Sands44c8cd92008-12-31 16:14:43 +0000251 ReadsMemory = true;
Chandler Carruth7542d372015-09-21 17:39:41 +0000252 break;
Brian Homerding3ecabd72018-08-23 15:05:22 +0000253 case MAK_WriteOnly:
254 WritesMemory = true;
255 break;
Chandler Carruth7542d372015-09-21 17:39:41 +0000256 case MAK_ReadNone:
257 // Nothing to do!
258 break;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000259 }
260 }
261
Johannes Doerfert3dcd7992019-07-15 17:31:26 +0000262 // If the SCC contains both functions that read and functions that write, then
263 // we cannot add readonly attributes.
264 if (ReadsMemory && WritesMemory)
265 return false;
266
Duncan Sands44c8cd92008-12-31 16:14:43 +0000267 // Success! Functions in this SCC do not access memory, or only read memory.
268 // Give them the appropriate attribute.
269 bool MadeChange = false;
Brian Homerding3ecabd72018-08-23 15:05:22 +0000270
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000271 for (Function *F : SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000272 if (F->doesNotAccessMemory())
273 // Already perfect!
274 continue;
275
276 if (F->onlyReadsMemory() && ReadsMemory)
277 // No change.
278 continue;
279
Brian Homerding3ecabd72018-08-23 15:05:22 +0000280 if (F->doesNotReadMemory() && WritesMemory)
281 continue;
282
Duncan Sands44c8cd92008-12-31 16:14:43 +0000283 MadeChange = true;
284
285 // Clear out any existing attributes.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000286 F->removeFnAttr(Attribute::ReadOnly);
287 F->removeFnAttr(Attribute::ReadNone);
Brian Homerding3ecabd72018-08-23 15:05:22 +0000288 F->removeFnAttr(Attribute::WriteOnly);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000289
Johannes Doerfertae3cfeb2018-09-11 11:51:29 +0000290 if (!WritesMemory && !ReadsMemory) {
291 // Clear out any "access range attributes" if readnone was deduced.
292 F->removeFnAttr(Attribute::ArgMemOnly);
293 F->removeFnAttr(Attribute::InaccessibleMemOnly);
294 F->removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
295 }
296
Duncan Sands44c8cd92008-12-31 16:14:43 +0000297 // Add in the new attribute.
Brian Homerding3ecabd72018-08-23 15:05:22 +0000298 if (WritesMemory && !ReadsMemory)
299 F->addFnAttr(Attribute::WriteOnly);
300 else
301 F->addFnAttr(ReadsMemory ? Attribute::ReadOnly : Attribute::ReadNone);
Duncan Sands44c8cd92008-12-31 16:14:43 +0000302
Brian Homerding3ecabd72018-08-23 15:05:22 +0000303 if (WritesMemory && !ReadsMemory)
304 ++NumWriteOnly;
305 else if (ReadsMemory)
Duncan Sandscefc8602009-01-02 11:46:24 +0000306 ++NumReadOnly;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000307 else
Duncan Sandscefc8602009-01-02 11:46:24 +0000308 ++NumReadNone;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000309 }
310
311 return MadeChange;
312}
313
Nick Lewycky4c378a42011-12-28 23:24:21 +0000314namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000315
Chandler Carrutha632fb92015-09-13 06:57:25 +0000316/// For a given pointer Argument, this retains a list of Arguments of functions
317/// in the same SCC that the pointer data flows into. We use this to build an
318/// SCC of the arguments.
Chandler Carruth63559d72015-09-13 06:47:20 +0000319struct ArgumentGraphNode {
320 Argument *Definition;
321 SmallVector<ArgumentGraphNode *, 4> Uses;
322};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000323
Chandler Carruth63559d72015-09-13 06:47:20 +0000324class ArgumentGraph {
325 // We store pointers to ArgumentGraphNode objects, so it's important that
326 // that they not move around upon insert.
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000327 using ArgumentMapTy = std::map<Argument *, ArgumentGraphNode>;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000328
Chandler Carruth63559d72015-09-13 06:47:20 +0000329 ArgumentMapTy ArgumentMap;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000330
Chandler Carruth63559d72015-09-13 06:47:20 +0000331 // There is no root node for the argument graph, in fact:
332 // void f(int *x, int *y) { if (...) f(x, y); }
333 // is an example where the graph is disconnected. The SCCIterator requires a
334 // single entry point, so we maintain a fake ("synthetic") root node that
335 // uses every node. Because the graph is directed and nothing points into
336 // the root, it will not participate in any SCCs (except for its own).
337 ArgumentGraphNode SyntheticRoot;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000338
Chandler Carruth63559d72015-09-13 06:47:20 +0000339public:
340 ArgumentGraph() { SyntheticRoot.Definition = nullptr; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000341
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000342 using iterator = SmallVectorImpl<ArgumentGraphNode *>::iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000343
Chandler Carruth63559d72015-09-13 06:47:20 +0000344 iterator begin() { return SyntheticRoot.Uses.begin(); }
345 iterator end() { return SyntheticRoot.Uses.end(); }
346 ArgumentGraphNode *getEntryNode() { return &SyntheticRoot; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000347
Chandler Carruth63559d72015-09-13 06:47:20 +0000348 ArgumentGraphNode *operator[](Argument *A) {
349 ArgumentGraphNode &Node = ArgumentMap[A];
350 Node.Definition = A;
351 SyntheticRoot.Uses.push_back(&Node);
352 return &Node;
353 }
354};
Nick Lewycky4c378a42011-12-28 23:24:21 +0000355
Chandler Carrutha632fb92015-09-13 06:57:25 +0000356/// This tracker checks whether callees are in the SCC, and if so it does not
357/// consider that a capture, instead adding it to the "Uses" list and
358/// continuing with the analysis.
Chandler Carruth63559d72015-09-13 06:47:20 +0000359struct ArgumentUsesTracker : public CaptureTracker {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000360 ArgumentUsesTracker(const SCCNodeSet &SCCNodes) : SCCNodes(SCCNodes) {}
Nick Lewycky4c378a42011-12-28 23:24:21 +0000361
Chandler Carruth63559d72015-09-13 06:47:20 +0000362 void tooManyUses() override { Captured = true; }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000363
Chandler Carruth63559d72015-09-13 06:47:20 +0000364 bool captured(const Use *U) override {
365 CallSite CS(U->getUser());
366 if (!CS.getInstruction()) {
367 Captured = true;
368 return true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000369 }
370
Chandler Carruth63559d72015-09-13 06:47:20 +0000371 Function *F = CS.getCalledFunction();
Sanjoy Das5ce32722016-04-08 00:48:30 +0000372 if (!F || !F->hasExactDefinition() || !SCCNodes.count(F)) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000373 Captured = true;
374 return true;
375 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000376
Sanjoy Das98bfe262015-11-05 03:04:40 +0000377 // Note: the callee and the two successor blocks *follow* the argument
378 // operands. This means there is no need to adjust UseIndex to account for
379 // these.
380
381 unsigned UseIndex =
382 std::distance(const_cast<const Use *>(CS.arg_begin()), U);
383
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000384 assert(UseIndex < CS.data_operands_size() &&
385 "Indirect function calls should have been filtered above!");
386
387 if (UseIndex >= CS.getNumArgOperands()) {
388 // Data operand, but not a argument operand -- must be a bundle operand
389 assert(CS.hasOperandBundles() && "Must be!");
390
391 // CaptureTracking told us that we're being captured by an operand bundle
392 // use. In this case it does not matter if the callee is within our SCC
393 // or not -- we've been captured in some unknown way, and we have to be
394 // conservative.
395 Captured = true;
396 return true;
397 }
398
Sanjoy Das98bfe262015-11-05 03:04:40 +0000399 if (UseIndex >= F->arg_size()) {
400 assert(F->isVarArg() && "More params than args in non-varargs call");
401 Captured = true;
402 return true;
Chandler Carruth63559d72015-09-13 06:47:20 +0000403 }
Sanjoy Das98bfe262015-11-05 03:04:40 +0000404
Duncan P. N. Exon Smith83c4b682015-11-07 00:01:16 +0000405 Uses.push_back(&*std::next(F->arg_begin(), UseIndex));
Chandler Carruth63559d72015-09-13 06:47:20 +0000406 return false;
407 }
408
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000409 // True only if certainly captured (used outside our SCC).
410 bool Captured = false;
411
412 // Uses within our SCC.
413 SmallVector<Argument *, 4> Uses;
Chandler Carruth63559d72015-09-13 06:47:20 +0000414
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000415 const SCCNodeSet &SCCNodes;
Chandler Carruth63559d72015-09-13 06:47:20 +0000416};
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000417
418} // end anonymous namespace
Nick Lewycky4c378a42011-12-28 23:24:21 +0000419
420namespace llvm {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000421
Chandler Carruth63559d72015-09-13 06:47:20 +0000422template <> struct GraphTraits<ArgumentGraphNode *> {
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000423 using NodeRef = ArgumentGraphNode *;
424 using ChildIteratorType = SmallVectorImpl<ArgumentGraphNode *>::iterator;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000425
Tim Shen48f814e2016-08-31 16:48:13 +0000426 static NodeRef getEntryNode(NodeRef A) { return A; }
427 static ChildIteratorType child_begin(NodeRef N) { return N->Uses.begin(); }
428 static ChildIteratorType child_end(NodeRef N) { return N->Uses.end(); }
Chandler Carruth63559d72015-09-13 06:47:20 +0000429};
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000430
Chandler Carruth63559d72015-09-13 06:47:20 +0000431template <>
432struct GraphTraits<ArgumentGraph *> : public GraphTraits<ArgumentGraphNode *> {
Tim Shenf2187ed2016-08-22 21:09:30 +0000433 static NodeRef getEntryNode(ArgumentGraph *AG) { return AG->getEntryNode(); }
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000434
Chandler Carruth63559d72015-09-13 06:47:20 +0000435 static ChildIteratorType nodes_begin(ArgumentGraph *AG) {
436 return AG->begin();
437 }
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000438
Chandler Carruth63559d72015-09-13 06:47:20 +0000439 static ChildIteratorType nodes_end(ArgumentGraph *AG) { return AG->end(); }
440};
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000441
442} // end namespace llvm
Nick Lewycky4c378a42011-12-28 23:24:21 +0000443
Chandler Carrutha632fb92015-09-13 06:57:25 +0000444/// Returns Attribute::None, Attribute::ReadOnly or Attribute::ReadNone.
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000445static Attribute::AttrKind
446determinePointerReadAttrs(Argument *A,
Chandler Carruth63559d72015-09-13 06:47:20 +0000447 const SmallPtrSet<Argument *, 8> &SCCNodes) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000448 SmallVector<Use *, 32> Worklist;
Florian Hahna1cc8482018-06-12 11:16:56 +0000449 SmallPtrSet<Use *, 32> Visited;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000450
Reid Kleckner26af2ca2014-01-28 02:38:36 +0000451 // inalloca arguments are always clobbered by the call.
452 if (A->hasInAllocaAttr())
453 return Attribute::None;
454
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000455 bool IsRead = false;
456 // We don't need to track IsWritten. If A is written to, return immediately.
457
Chandler Carruthcdf47882014-03-09 03:16:01 +0000458 for (Use &U : A->uses()) {
Chandler Carruthcdf47882014-03-09 03:16:01 +0000459 Visited.insert(&U);
460 Worklist.push_back(&U);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000461 }
462
463 while (!Worklist.empty()) {
464 Use *U = Worklist.pop_back_val();
465 Instruction *I = cast<Instruction>(U->getUser());
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000466
467 switch (I->getOpcode()) {
468 case Instruction::BitCast:
469 case Instruction::GetElementPtr:
470 case Instruction::PHI:
471 case Instruction::Select:
Matt Arsenaulte55a2c22014-01-14 19:11:52 +0000472 case Instruction::AddrSpaceCast:
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000473 // The original value is not read/written via this if the new value isn't.
Chandler Carruthcdf47882014-03-09 03:16:01 +0000474 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000475 if (Visited.insert(&UU).second)
Chandler Carruthcdf47882014-03-09 03:16:01 +0000476 Worklist.push_back(&UU);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000477 break;
478
479 case Instruction::Call:
480 case Instruction::Invoke: {
Nick Lewycky59633cb2014-05-30 02:31:27 +0000481 bool Captures = true;
482
483 if (I->getType()->isVoidTy())
484 Captures = false;
485
486 auto AddUsersToWorklistIfCapturing = [&] {
487 if (Captures)
488 for (Use &UU : I->uses())
David Blaikie70573dc2014-11-19 07:49:26 +0000489 if (Visited.insert(&UU).second)
Nick Lewycky59633cb2014-05-30 02:31:27 +0000490 Worklist.push_back(&UU);
491 };
492
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000493 CallSite CS(I);
Nick Lewycky59633cb2014-05-30 02:31:27 +0000494 if (CS.doesNotAccessMemory()) {
495 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000496 continue;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000497 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000498
499 Function *F = CS.getCalledFunction();
500 if (!F) {
501 if (CS.onlyReadsMemory()) {
502 IsRead = true;
Nick Lewycky59633cb2014-05-30 02:31:27 +0000503 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000504 continue;
505 }
506 return Attribute::None;
507 }
508
Sanjoy Das436e2392015-11-07 01:55:53 +0000509 // Note: the callee and the two successor blocks *follow* the argument
510 // operands. This means there is no need to adjust UseIndex to account
511 // for these.
512
513 unsigned UseIndex = std::distance(CS.arg_begin(), U);
514
Sanjoy Dasea1df7f2015-11-07 01:56:07 +0000515 // U cannot be the callee operand use: since we're exploring the
516 // transitive uses of an Argument, having such a use be a callee would
517 // imply the CallSite is an indirect call or invoke; and we'd take the
518 // early exit above.
519 assert(UseIndex < CS.data_operands_size() &&
520 "Data operand use expected!");
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000521
522 bool IsOperandBundleUse = UseIndex >= CS.getNumArgOperands();
523
524 if (UseIndex >= F->arg_size() && !IsOperandBundleUse) {
Sanjoy Das436e2392015-11-07 01:55:53 +0000525 assert(F->isVarArg() && "More params than args in non-varargs call");
526 return Attribute::None;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000527 }
Sanjoy Das436e2392015-11-07 01:55:53 +0000528
Tilmann Scheller925b1932015-11-20 19:17:10 +0000529 Captures &= !CS.doesNotCapture(UseIndex);
530
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000531 // Since the optimizer (by design) cannot see the data flow corresponding
532 // to a operand bundle use, these cannot participate in the optimistic SCC
533 // analysis. Instead, we model the operand bundle uses as arguments in
534 // call to a function external to the SCC.
Duncan P. N. Exon Smith9e3edad2016-08-17 01:23:58 +0000535 if (IsOperandBundleUse ||
536 !SCCNodes.count(&*std::next(F->arg_begin(), UseIndex))) {
Sanjoy Das71fe81f2015-11-07 01:56:00 +0000537
538 // The accessors used on CallSite here do the right thing for calls and
539 // invokes with operand bundles.
540
Sanjoy Das436e2392015-11-07 01:55:53 +0000541 if (!CS.onlyReadsMemory() && !CS.onlyReadsMemory(UseIndex))
542 return Attribute::None;
543 if (!CS.doesNotAccessMemory(UseIndex))
544 IsRead = true;
545 }
546
Nick Lewycky59633cb2014-05-30 02:31:27 +0000547 AddUsersToWorklistIfCapturing();
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000548 break;
549 }
550
551 case Instruction::Load:
David Majnemer124bdb72016-05-25 05:53:04 +0000552 // A volatile load has side effects beyond what readonly can be relied
553 // upon.
554 if (cast<LoadInst>(I)->isVolatile())
555 return Attribute::None;
556
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000557 IsRead = true;
558 break;
559
560 case Instruction::ICmp:
561 case Instruction::Ret:
562 break;
563
564 default:
565 return Attribute::None;
566 }
567 }
568
569 return IsRead ? Attribute::ReadOnly : Attribute::ReadNone;
570}
571
David Majnemer5246e0b2016-07-19 18:50:26 +0000572/// Deduce returned attributes for the SCC.
573static bool addArgumentReturnedAttrs(const SCCNodeSet &SCCNodes) {
574 bool Changed = false;
575
David Majnemer5246e0b2016-07-19 18:50:26 +0000576 // Check each function in turn, determining if an argument is always returned.
577 for (Function *F : SCCNodes) {
578 // We can infer and propagate function attributes only when we know that the
579 // definition we'll get at link time is *exactly* the definition we see now.
580 // For more details, see GlobalValue::mayBeDerefined.
581 if (!F->hasExactDefinition())
582 continue;
583
584 if (F->getReturnType()->isVoidTy())
585 continue;
586
David Majnemerc83044d2016-09-12 16:04:59 +0000587 // There is nothing to do if an argument is already marked as 'returned'.
Eugene Zelenkof27d1612017-10-19 21:21:30 +0000588 if (llvm::any_of(F->args(),
589 [](const Argument &Arg) { return Arg.hasReturnedAttr(); }))
David Majnemerc83044d2016-09-12 16:04:59 +0000590 continue;
591
David Majnemer5246e0b2016-07-19 18:50:26 +0000592 auto FindRetArg = [&]() -> Value * {
593 Value *RetArg = nullptr;
594 for (BasicBlock &BB : *F)
595 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator())) {
596 // Note that stripPointerCasts should look through functions with
597 // returned arguments.
598 Value *RetVal = Ret->getReturnValue()->stripPointerCasts();
599 if (!isa<Argument>(RetVal) || RetVal->getType() != F->getReturnType())
600 return nullptr;
601
602 if (!RetArg)
603 RetArg = RetVal;
604 else if (RetArg != RetVal)
605 return nullptr;
606 }
607
608 return RetArg;
609 };
610
611 if (Value *RetArg = FindRetArg()) {
612 auto *A = cast<Argument>(RetArg);
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000613 A->addAttr(Attribute::Returned);
David Majnemer5246e0b2016-07-19 18:50:26 +0000614 ++NumReturned;
615 Changed = true;
616 }
617 }
618
619 return Changed;
620}
621
Sanjay Patel4f742162017-02-13 23:10:51 +0000622/// If a callsite has arguments that are also arguments to the parent function,
623/// try to propagate attributes from the callsite's arguments to the parent's
624/// arguments. This may be important because inlining can cause information loss
625/// when attribute knowledge disappears with the inlined call.
626static bool addArgumentAttrsFromCallsites(Function &F) {
627 if (!EnableNonnullArgPropagation)
628 return false;
629
630 bool Changed = false;
631
632 // For an argument attribute to transfer from a callsite to the parent, the
633 // call must be guaranteed to execute every time the parent is called.
634 // Conservatively, just check for calls in the entry block that are guaranteed
635 // to execute.
636 // TODO: This could be enhanced by testing if the callsite post-dominates the
637 // entry block or by doing simple forward walks or backward walks to the
638 // callsite.
639 BasicBlock &Entry = F.getEntryBlock();
640 for (Instruction &I : Entry) {
641 if (auto CS = CallSite(&I)) {
642 if (auto *CalledFunc = CS.getCalledFunction()) {
643 for (auto &CSArg : CalledFunc->args()) {
644 if (!CSArg.hasNonNullAttr())
645 continue;
646
647 // If the non-null callsite argument operand is an argument to 'F'
648 // (the caller) and the call is guaranteed to execute, then the value
649 // must be non-null throughout 'F'.
650 auto *FArg = dyn_cast<Argument>(CS.getArgOperand(CSArg.getArgNo()));
651 if (FArg && !FArg->hasNonNullAttr()) {
652 FArg->addAttr(Attribute::NonNull);
653 Changed = true;
654 }
655 }
656 }
657 }
658 if (!isGuaranteedToTransferExecutionToSuccessor(&I))
659 break;
660 }
Fangrui Songf78650a2018-07-30 19:41:25 +0000661
Sanjay Patel4f742162017-02-13 23:10:51 +0000662 return Changed;
663}
664
Whitney Tsang1ccba7c2019-09-11 14:26:22 +0000665static bool addReadAttr(Argument *A, Attribute::AttrKind R) {
666 assert((R == Attribute::ReadOnly || R == Attribute::ReadNone)
667 && "Must be a Read attribute.");
668 assert(A && "Argument must not be null.");
669
670 // If the argument already has the attribute, nothing needs to be done.
671 if (A->hasAttribute(R))
672 return false;
673
674 // Otherwise, remove potentially conflicting attribute, add the new one,
675 // and update statistics.
676 A->removeAttr(Attribute::WriteOnly);
677 A->removeAttr(Attribute::ReadOnly);
678 A->removeAttr(Attribute::ReadNone);
679 A->addAttr(R);
680 R == Attribute::ReadOnly ? ++NumReadOnlyArg : ++NumReadNoneArg;
681 return true;
682}
683
Chandler Carrutha632fb92015-09-13 06:57:25 +0000684/// Deduce nocapture attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000685static bool addArgumentAttrs(const SCCNodeSet &SCCNodes) {
Duncan Sands44c8cd92008-12-31 16:14:43 +0000686 bool Changed = false;
687
Nick Lewycky4c378a42011-12-28 23:24:21 +0000688 ArgumentGraph AG;
689
Duncan Sands44c8cd92008-12-31 16:14:43 +0000690 // Check each function in turn, determining which pointer arguments are not
691 // captured.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000692 for (Function *F : SCCNodes) {
Sanjoy Das5ce32722016-04-08 00:48:30 +0000693 // We can infer and propagate function attributes only when we know that the
694 // definition we'll get at link time is *exactly* the definition we see now.
695 // For more details, see GlobalValue::mayBeDerefined.
696 if (!F->hasExactDefinition())
Duncan Sands44c8cd92008-12-31 16:14:43 +0000697 continue;
698
Sanjay Patel4f742162017-02-13 23:10:51 +0000699 Changed |= addArgumentAttrsFromCallsites(*F);
700
Nick Lewycky4c378a42011-12-28 23:24:21 +0000701 // Functions that are readonly (or readnone) and nounwind and don't return
702 // a value can't capture arguments. Don't analyze them.
703 if (F->onlyReadsMemory() && F->doesNotThrow() &&
704 F->getReturnType()->isVoidTy()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000705 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
706 ++A) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000707 if (A->getType()->isPointerTy() && !A->hasNoCaptureAttr()) {
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000708 A->addAttr(Attribute::NoCapture);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000709 ++NumNoCapture;
710 Changed = true;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000711 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000712 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000713 continue;
Benjamin Kramer76b7bd02013-06-22 15:51:19 +0000714 }
715
Chandler Carruth63559d72015-09-13 06:47:20 +0000716 for (Function::arg_iterator A = F->arg_begin(), E = F->arg_end(); A != E;
717 ++A) {
718 if (!A->getType()->isPointerTy())
719 continue;
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000720 bool HasNonLocalUses = false;
721 if (!A->hasNoCaptureAttr()) {
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000722 ArgumentUsesTracker Tracker(SCCNodes);
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000723 PointerMayBeCaptured(&*A, &Tracker);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000724 if (!Tracker.Captured) {
725 if (Tracker.Uses.empty()) {
726 // If it's trivially not captured, mark it nocapture now.
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000727 A->addAttr(Attribute::NoCapture);
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000728 ++NumNoCapture;
729 Changed = true;
730 } else {
731 // If it's not trivially captured and not trivially not captured,
732 // then it must be calling into another function in our SCC. Save
733 // its particulars for Argument-SCC analysis later.
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000734 ArgumentGraphNode *Node = AG[&*A];
Benjamin Kramer135f7352016-06-26 12:28:59 +0000735 for (Argument *Use : Tracker.Uses) {
736 Node->Uses.push_back(AG[Use]);
737 if (Use != &*A)
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000738 HasNonLocalUses = true;
739 }
Benjamin Kramer40d7f352013-06-22 16:56:32 +0000740 }
741 }
742 // Otherwise, it's captured. Don't bother doing SCC analysis on it.
743 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000744 if (!HasNonLocalUses && !A->onlyReadsMemory()) {
745 // Can we determine that it's readonly/readnone without doing an SCC?
746 // Note that we don't allow any calls at all here, or else our result
747 // will be dependent on the iteration order through the functions in the
748 // SCC.
Chandler Carruth63559d72015-09-13 06:47:20 +0000749 SmallPtrSet<Argument *, 8> Self;
Duncan P. N. Exon Smith17323402015-10-13 17:51:03 +0000750 Self.insert(&*A);
751 Attribute::AttrKind R = determinePointerReadAttrs(&*A, Self);
Whitney Tsang1ccba7c2019-09-11 14:26:22 +0000752 if (R != Attribute::None)
753 Changed = addReadAttr(A, R);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000754 }
755 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000756 }
757
758 // The graph we've collected is partial because we stopped scanning for
759 // argument uses once we solved the argument trivially. These partial nodes
760 // show up as ArgumentGraphNode objects with an empty Uses list, and for
761 // these nodes the final decision about whether they capture has already been
762 // made. If the definition doesn't have a 'nocapture' attribute by now, it
763 // captures.
764
Chandler Carruth63559d72015-09-13 06:47:20 +0000765 for (scc_iterator<ArgumentGraph *> I = scc_begin(&AG); !I.isAtEnd(); ++I) {
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000766 const std::vector<ArgumentGraphNode *> &ArgumentSCC = *I;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000767 if (ArgumentSCC.size() == 1) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000768 if (!ArgumentSCC[0]->Definition)
769 continue; // synthetic root node
Nick Lewycky4c378a42011-12-28 23:24:21 +0000770
771 // eg. "void f(int* x) { if (...) f(x); }"
772 if (ArgumentSCC[0]->Uses.size() == 1 &&
773 ArgumentSCC[0]->Uses[0] == ArgumentSCC[0]) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000774 Argument *A = ArgumentSCC[0]->Definition;
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000775 A->addAttr(Attribute::NoCapture);
Nick Lewycky7e820552009-01-02 03:46:56 +0000776 ++NumNoCapture;
Duncan Sands44c8cd92008-12-31 16:14:43 +0000777 Changed = true;
778 }
Nick Lewycky4c378a42011-12-28 23:24:21 +0000779 continue;
780 }
781
782 bool SCCCaptured = false;
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000783 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
784 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000785 ArgumentGraphNode *Node = *I;
786 if (Node->Uses.empty()) {
787 if (!Node->Definition->hasNoCaptureAttr())
788 SCCCaptured = true;
789 }
790 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000791 if (SCCCaptured)
792 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000793
Chandler Carruth63559d72015-09-13 06:47:20 +0000794 SmallPtrSet<Argument *, 8> ArgumentSCCNodes;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000795 // Fill ArgumentSCCNodes with the elements of the ArgumentSCC. Used for
796 // quickly looking up whether a given Argument is in this ArgumentSCC.
Benjamin Kramer135f7352016-06-26 12:28:59 +0000797 for (ArgumentGraphNode *I : ArgumentSCC) {
798 ArgumentSCCNodes.insert(I->Definition);
Nick Lewycky4c378a42011-12-28 23:24:21 +0000799 }
800
Duncan P. N. Exon Smithd2b2fac2014-04-25 18:24:50 +0000801 for (auto I = ArgumentSCC.begin(), E = ArgumentSCC.end();
802 I != E && !SCCCaptured; ++I) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000803 ArgumentGraphNode *N = *I;
Benjamin Kramer135f7352016-06-26 12:28:59 +0000804 for (ArgumentGraphNode *Use : N->Uses) {
805 Argument *A = Use->Definition;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000806 if (A->hasNoCaptureAttr() || ArgumentSCCNodes.count(A))
807 continue;
808 SCCCaptured = true;
809 break;
810 }
811 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000812 if (SCCCaptured)
813 continue;
Nick Lewycky4c378a42011-12-28 23:24:21 +0000814
Nick Lewyckyf740db32012-01-05 22:21:45 +0000815 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
Nick Lewycky4c378a42011-12-28 23:24:21 +0000816 Argument *A = ArgumentSCC[i]->Definition;
Reid Kleckner9d16fa02017-04-19 17:28:52 +0000817 A->addAttr(Attribute::NoCapture);
Nick Lewycky4c378a42011-12-28 23:24:21 +0000818 ++NumNoCapture;
819 Changed = true;
820 }
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000821
822 // We also want to compute readonly/readnone. With a small number of false
823 // negatives, we can assume that any pointer which is captured isn't going
824 // to be provably readonly or readnone, since by definition we can't
825 // analyze all uses of a captured pointer.
826 //
827 // The false negatives happen when the pointer is captured by a function
828 // that promises readonly/readnone behaviour on the pointer, then the
829 // pointer's lifetime ends before anything that writes to arbitrary memory.
830 // Also, a readonly/readnone pointer may be returned, but returning a
831 // pointer is capturing it.
832
833 Attribute::AttrKind ReadAttr = Attribute::ReadNone;
834 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
835 Argument *A = ArgumentSCC[i]->Definition;
836 Attribute::AttrKind K = determinePointerReadAttrs(A, ArgumentSCCNodes);
837 if (K == Attribute::ReadNone)
838 continue;
839 if (K == Attribute::ReadOnly) {
840 ReadAttr = Attribute::ReadOnly;
841 continue;
842 }
843 ReadAttr = K;
844 break;
845 }
846
847 if (ReadAttr != Attribute::None) {
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000848 for (unsigned i = 0, e = ArgumentSCC.size(); i != e; ++i) {
849 Argument *A = ArgumentSCC[i]->Definition;
Whitney Tsang1ccba7c2019-09-11 14:26:22 +0000850 Changed = addReadAttr(A, ReadAttr);
Nick Lewyckyc2ec0722013-07-06 00:29:58 +0000851 }
852 }
Duncan Sands44c8cd92008-12-31 16:14:43 +0000853 }
854
855 return Changed;
856}
857
Chandler Carrutha632fb92015-09-13 06:57:25 +0000858/// Tests whether a function is "malloc-like".
859///
860/// A function is "malloc-like" if it returns either null or a pointer that
861/// doesn't alias any other pointer visible to the caller.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000862static bool isFunctionMallocLike(Function *F, const SCCNodeSet &SCCNodes) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000863 SmallSetVector<Value *, 8> FlowsToReturn;
Benjamin Kramer135f7352016-06-26 12:28:59 +0000864 for (BasicBlock &BB : *F)
865 if (ReturnInst *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000866 FlowsToReturn.insert(Ret->getReturnValue());
867
868 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
Benjamin Kramer15591272012-10-31 13:45:49 +0000869 Value *RetVal = FlowsToReturn[i];
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000870
871 if (Constant *C = dyn_cast<Constant>(RetVal)) {
872 if (!C->isNullValue() && !isa<UndefValue>(C))
873 return false;
874
875 continue;
876 }
877
878 if (isa<Argument>(RetVal))
879 return false;
880
881 if (Instruction *RVI = dyn_cast<Instruction>(RetVal))
882 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000883 // Extend the analysis by looking upwards.
884 case Instruction::BitCast:
885 case Instruction::GetElementPtr:
886 case Instruction::AddrSpaceCast:
887 FlowsToReturn.insert(RVI->getOperand(0));
888 continue;
889 case Instruction::Select: {
890 SelectInst *SI = cast<SelectInst>(RVI);
891 FlowsToReturn.insert(SI->getTrueValue());
892 FlowsToReturn.insert(SI->getFalseValue());
893 continue;
894 }
895 case Instruction::PHI: {
896 PHINode *PN = cast<PHINode>(RVI);
897 for (Value *IncValue : PN->incoming_values())
898 FlowsToReturn.insert(IncValue);
899 continue;
900 }
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000901
Chandler Carruth63559d72015-09-13 06:47:20 +0000902 // Check whether the pointer came from an allocation.
903 case Instruction::Alloca:
904 break;
905 case Instruction::Call:
906 case Instruction::Invoke: {
907 CallSite CS(RVI);
Reid Klecknerfb502d22017-04-14 20:19:02 +0000908 if (CS.hasRetAttr(Attribute::NoAlias))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000909 break;
Chandler Carruth63559d72015-09-13 06:47:20 +0000910 if (CS.getCalledFunction() && SCCNodes.count(CS.getCalledFunction()))
911 break;
Justin Bognercd1d5aa2016-08-17 20:30:52 +0000912 LLVM_FALLTHROUGH;
913 }
Chandler Carruth63559d72015-09-13 06:47:20 +0000914 default:
915 return false; // Did not come from an allocation.
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000916 }
917
Dan Gohman94e61762009-11-19 21:57:48 +0000918 if (PointerMayBeCaptured(RetVal, false, /*StoreCaptures=*/false))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000919 return false;
920 }
921
922 return true;
923}
924
Chandler Carrutha632fb92015-09-13 06:57:25 +0000925/// Deduce noalias attributes for the SCC.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000926static bool addNoAliasAttrs(const SCCNodeSet &SCCNodes) {
Nick Lewycky9ec96d12009-03-08 17:08:09 +0000927 // Check each function in turn, determining which functions return noalias
928 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000929 for (Function *F : SCCNodes) {
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000930 // Already noalias.
Reid Klecknera0b45f42017-05-03 18:17:31 +0000931 if (F->returnDoesNotAlias())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000932 continue;
933
Sanjoy Das5ce32722016-04-08 00:48:30 +0000934 // We can infer and propagate function attributes only when we know that the
935 // definition we'll get at link time is *exactly* the definition we see now.
936 // For more details, see GlobalValue::mayBeDerefined.
937 if (!F->hasExactDefinition())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000938 return false;
939
Chandler Carruth63559d72015-09-13 06:47:20 +0000940 // We annotate noalias return values, which are only applicable to
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000941 // pointer types.
Duncan Sands19d0b472010-02-16 11:11:14 +0000942 if (!F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000943 continue;
944
Chandler Carruth3824f852015-09-13 08:23:27 +0000945 if (!isFunctionMallocLike(F, SCCNodes))
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000946 return false;
947 }
948
949 bool MadeChange = false;
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000950 for (Function *F : SCCNodes) {
Reid Klecknera0b45f42017-05-03 18:17:31 +0000951 if (F->returnDoesNotAlias() ||
Reid Kleckner6652a522017-04-28 18:37:16 +0000952 !F->getReturnType()->isPointerTy())
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000953 continue;
954
Reid Klecknera0b45f42017-05-03 18:17:31 +0000955 F->setReturnDoesNotAlias();
Nick Lewyckyfbed86a2009-03-08 06:20:47 +0000956 ++NumNoAlias;
957 MadeChange = true;
958 }
959
960 return MadeChange;
961}
962
Chandler Carrutha632fb92015-09-13 06:57:25 +0000963/// Tests whether this function is known to not return null.
Chandler Carruth8874b782015-09-13 08:17:14 +0000964///
965/// Requires that the function returns a pointer.
966///
967/// Returns true if it believes the function will not return a null, and sets
968/// \p Speculative based on whether the returned conclusion is a speculative
969/// conclusion due to SCC calls.
Chandler Carruthc518ebd2015-10-29 18:29:15 +0000970static bool isReturnNonNull(Function *F, const SCCNodeSet &SCCNodes,
Sean Silva45835e72016-07-02 23:47:27 +0000971 bool &Speculative) {
Philip Reamesa88caea2015-08-31 19:44:38 +0000972 assert(F->getReturnType()->isPointerTy() &&
973 "nonnull only meaningful on pointer types");
974 Speculative = false;
Chandler Carruth63559d72015-09-13 06:47:20 +0000975
Philip Reamesa88caea2015-08-31 19:44:38 +0000976 SmallSetVector<Value *, 8> FlowsToReturn;
977 for (BasicBlock &BB : *F)
978 if (auto *Ret = dyn_cast<ReturnInst>(BB.getTerminator()))
979 FlowsToReturn.insert(Ret->getReturnValue());
980
Nuno Lopes404f1062017-09-09 18:23:11 +0000981 auto &DL = F->getParent()->getDataLayout();
982
Philip Reamesa88caea2015-08-31 19:44:38 +0000983 for (unsigned i = 0; i != FlowsToReturn.size(); ++i) {
984 Value *RetVal = FlowsToReturn[i];
985
986 // If this value is locally known to be non-null, we're good
Nuno Lopes404f1062017-09-09 18:23:11 +0000987 if (isKnownNonZero(RetVal, DL))
Philip Reamesa88caea2015-08-31 19:44:38 +0000988 continue;
989
990 // Otherwise, we need to look upwards since we can't make any local
Chandler Carruth63559d72015-09-13 06:47:20 +0000991 // conclusions.
Philip Reamesa88caea2015-08-31 19:44:38 +0000992 Instruction *RVI = dyn_cast<Instruction>(RetVal);
993 if (!RVI)
994 return false;
995 switch (RVI->getOpcode()) {
Chandler Carruth63559d72015-09-13 06:47:20 +0000996 // Extend the analysis by looking upwards.
Philip Reamesa88caea2015-08-31 19:44:38 +0000997 case Instruction::BitCast:
998 case Instruction::GetElementPtr:
999 case Instruction::AddrSpaceCast:
1000 FlowsToReturn.insert(RVI->getOperand(0));
1001 continue;
1002 case Instruction::Select: {
1003 SelectInst *SI = cast<SelectInst>(RVI);
1004 FlowsToReturn.insert(SI->getTrueValue());
1005 FlowsToReturn.insert(SI->getFalseValue());
1006 continue;
1007 }
1008 case Instruction::PHI: {
1009 PHINode *PN = cast<PHINode>(RVI);
1010 for (int i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
1011 FlowsToReturn.insert(PN->getIncomingValue(i));
1012 continue;
1013 }
1014 case Instruction::Call:
1015 case Instruction::Invoke: {
1016 CallSite CS(RVI);
1017 Function *Callee = CS.getCalledFunction();
1018 // A call to a node within the SCC is assumed to return null until
1019 // proven otherwise
1020 if (Callee && SCCNodes.count(Callee)) {
1021 Speculative = true;
1022 continue;
1023 }
1024 return false;
1025 }
1026 default:
Chandler Carruth63559d72015-09-13 06:47:20 +00001027 return false; // Unknown source, may be null
Philip Reamesa88caea2015-08-31 19:44:38 +00001028 };
1029 llvm_unreachable("should have either continued or returned");
1030 }
1031
1032 return true;
1033}
1034
Chandler Carrutha632fb92015-09-13 06:57:25 +00001035/// Deduce nonnull attributes for the SCC.
Sean Silva45835e72016-07-02 23:47:27 +00001036static bool addNonNullAttrs(const SCCNodeSet &SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +00001037 // Speculative that all functions in the SCC return only nonnull
1038 // pointers. We may refute this as we analyze functions.
1039 bool SCCReturnsNonNull = true;
1040
1041 bool MadeChange = false;
1042
1043 // Check each function in turn, determining which functions return nonnull
1044 // pointers.
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001045 for (Function *F : SCCNodes) {
Philip Reamesa88caea2015-08-31 19:44:38 +00001046 // Already nonnull.
Reid Klecknerb5180542017-03-21 16:57:19 +00001047 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
Philip Reamesa88caea2015-08-31 19:44:38 +00001048 Attribute::NonNull))
1049 continue;
1050
Sanjoy Das5ce32722016-04-08 00:48:30 +00001051 // We can infer and propagate function attributes only when we know that the
1052 // definition we'll get at link time is *exactly* the definition we see now.
1053 // For more details, see GlobalValue::mayBeDerefined.
1054 if (!F->hasExactDefinition())
Philip Reamesa88caea2015-08-31 19:44:38 +00001055 return false;
1056
Chandler Carruth63559d72015-09-13 06:47:20 +00001057 // We annotate nonnull return values, which are only applicable to
Philip Reamesa88caea2015-08-31 19:44:38 +00001058 // pointer types.
1059 if (!F->getReturnType()->isPointerTy())
1060 continue;
1061
1062 bool Speculative = false;
Sean Silva45835e72016-07-02 23:47:27 +00001063 if (isReturnNonNull(F, SCCNodes, Speculative)) {
Philip Reamesa88caea2015-08-31 19:44:38 +00001064 if (!Speculative) {
1065 // Mark the function eagerly since we may discover a function
1066 // which prevents us from speculating about the entire SCC
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001067 LLVM_DEBUG(dbgs() << "Eagerly marking " << F->getName()
1068 << " as nonnull\n");
Reid Klecknerb5180542017-03-21 16:57:19 +00001069 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
Philip Reamesa88caea2015-08-31 19:44:38 +00001070 ++NumNonNullReturn;
1071 MadeChange = true;
1072 }
1073 continue;
1074 }
1075 // At least one function returns something which could be null, can't
1076 // speculate any more.
1077 SCCReturnsNonNull = false;
1078 }
1079
1080 if (SCCReturnsNonNull) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001081 for (Function *F : SCCNodes) {
Reid Klecknerb5180542017-03-21 16:57:19 +00001082 if (F->getAttributes().hasAttribute(AttributeList::ReturnIndex,
Philip Reamesa88caea2015-08-31 19:44:38 +00001083 Attribute::NonNull) ||
1084 !F->getReturnType()->isPointerTy())
1085 continue;
1086
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001087 LLVM_DEBUG(dbgs() << "SCC marking " << F->getName() << " as nonnull\n");
Reid Klecknerb5180542017-03-21 16:57:19 +00001088 F->addAttribute(AttributeList::ReturnIndex, Attribute::NonNull);
Philip Reamesa88caea2015-08-31 19:44:38 +00001089 ++NumNonNullReturn;
1090 MadeChange = true;
1091 }
1092 }
1093
1094 return MadeChange;
1095}
1096
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001097namespace {
1098
1099/// Collects a set of attribute inference requests and performs them all in one
1100/// go on a single SCC Node. Inference involves scanning function bodies
1101/// looking for instructions that violate attribute assumptions.
1102/// As soon as all the bodies are fine we are free to set the attribute.
1103/// Customization of inference for individual attributes is performed by
1104/// providing a handful of predicates for each attribute.
1105class AttributeInferer {
1106public:
1107 /// Describes a request for inference of a single attribute.
1108 struct InferenceDescriptor {
1109
1110 /// Returns true if this function does not have to be handled.
1111 /// General intent for this predicate is to provide an optimization
1112 /// for functions that do not need this attribute inference at all
1113 /// (say, for functions that already have the attribute).
1114 std::function<bool(const Function &)> SkipFunction;
1115
1116 /// Returns true if this instruction violates attribute assumptions.
1117 std::function<bool(Instruction &)> InstrBreaksAttribute;
1118
1119 /// Sets the inferred attribute for this function.
1120 std::function<void(Function &)> SetAttribute;
1121
1122 /// Attribute we derive.
1123 Attribute::AttrKind AKind;
1124
1125 /// If true, only "exact" definitions can be used to infer this attribute.
1126 /// See GlobalValue::isDefinitionExact.
1127 bool RequiresExactDefinition;
1128
1129 InferenceDescriptor(Attribute::AttrKind AK,
1130 std::function<bool(const Function &)> SkipFunc,
1131 std::function<bool(Instruction &)> InstrScan,
1132 std::function<void(Function &)> SetAttr,
1133 bool ReqExactDef)
1134 : SkipFunction(SkipFunc), InstrBreaksAttribute(InstrScan),
1135 SetAttribute(SetAttr), AKind(AK),
1136 RequiresExactDefinition(ReqExactDef) {}
1137 };
1138
1139private:
1140 SmallVector<InferenceDescriptor, 4> InferenceDescriptors;
1141
1142public:
1143 void registerAttrInference(InferenceDescriptor AttrInference) {
1144 InferenceDescriptors.push_back(AttrInference);
1145 }
1146
1147 bool run(const SCCNodeSet &SCCNodes);
1148};
1149
1150/// Perform all the requested attribute inference actions according to the
1151/// attribute predicates stored before.
1152bool AttributeInferer::run(const SCCNodeSet &SCCNodes) {
1153 SmallVector<InferenceDescriptor, 4> InferInSCC = InferenceDescriptors;
1154 // Go through all the functions in SCC and check corresponding attribute
1155 // assumptions for each of them. Attributes that are invalid for this SCC
1156 // will be removed from InferInSCC.
Chandler Carruth3937bc72016-02-12 09:47:49 +00001157 for (Function *F : SCCNodes) {
Justin Lebar9d943972016-03-14 20:18:54 +00001158
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001159 // No attributes whose assumptions are still valid - done.
1160 if (InferInSCC.empty())
1161 return false;
Justin Lebar9d943972016-03-14 20:18:54 +00001162
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001163 // Check if our attributes ever need scanning/can be scanned.
1164 llvm::erase_if(InferInSCC, [F](const InferenceDescriptor &ID) {
1165 if (ID.SkipFunction(*F))
Justin Lebar9d943972016-03-14 20:18:54 +00001166 return false;
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001167
1168 // Remove from further inference (invalidate) when visiting a function
1169 // that has no instructions to scan/has an unsuitable definition.
1170 return F->isDeclaration() ||
1171 (ID.RequiresExactDefinition && !F->hasExactDefinition());
1172 });
1173
1174 // For each attribute still in InferInSCC that doesn't explicitly skip F,
1175 // set up the F instructions scan to verify assumptions of the attribute.
1176 SmallVector<InferenceDescriptor, 4> InferInThisFunc;
1177 llvm::copy_if(
1178 InferInSCC, std::back_inserter(InferInThisFunc),
1179 [F](const InferenceDescriptor &ID) { return !ID.SkipFunction(*F); });
1180
1181 if (InferInThisFunc.empty())
1182 continue;
1183
1184 // Start instruction scan.
1185 for (Instruction &I : instructions(*F)) {
1186 llvm::erase_if(InferInThisFunc, [&](const InferenceDescriptor &ID) {
1187 if (!ID.InstrBreaksAttribute(I))
1188 return false;
1189 // Remove attribute from further inference on any other functions
1190 // because attribute assumptions have just been violated.
1191 llvm::erase_if(InferInSCC, [&ID](const InferenceDescriptor &D) {
1192 return D.AKind == ID.AKind;
1193 });
1194 // Remove attribute from the rest of current instruction scan.
1195 return true;
1196 });
1197
1198 if (InferInThisFunc.empty())
1199 break;
Justin Lebar9d943972016-03-14 20:18:54 +00001200 }
1201 }
1202
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001203 if (InferInSCC.empty())
1204 return false;
Justin Lebar9d943972016-03-14 20:18:54 +00001205
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001206 bool Changed = false;
1207 for (Function *F : SCCNodes)
1208 // At this point InferInSCC contains only functions that were either:
1209 // - explicitly skipped from scan/inference, or
1210 // - verified to have no instructions that break attribute assumptions.
1211 // Hence we just go and force the attribute for all non-skipped functions.
1212 for (auto &ID : InferInSCC) {
1213 if (ID.SkipFunction(*F))
1214 continue;
1215 Changed = true;
1216 ID.SetAttribute(*F);
1217 }
1218 return Changed;
1219}
Justin Lebar9d943972016-03-14 20:18:54 +00001220
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001221} // end anonymous namespace
1222
1223/// Helper for non-Convergent inference predicate InstrBreaksAttribute.
1224static bool InstrBreaksNonConvergent(Instruction &I,
1225 const SCCNodeSet &SCCNodes) {
1226 const CallSite CS(&I);
1227 // Breaks non-convergent assumption if CS is a convergent call to a function
1228 // not in the SCC.
1229 return CS && CS.isConvergent() && SCCNodes.count(CS.getCalledFunction()) == 0;
1230}
1231
1232/// Helper for NoUnwind inference predicate InstrBreaksAttribute.
1233static bool InstrBreaksNonThrowing(Instruction &I, const SCCNodeSet &SCCNodes) {
1234 if (!I.mayThrow())
1235 return false;
1236 if (const auto *CI = dyn_cast<CallInst>(&I)) {
1237 if (Function *Callee = CI->getCalledFunction()) {
1238 // I is a may-throw call to a function inside our SCC. This doesn't
1239 // invalidate our current working assumption that the SCC is no-throw; we
1240 // just have to scan that other function.
1241 if (SCCNodes.count(Callee) > 0)
1242 return false;
1243 }
Chandler Carruth3937bc72016-02-12 09:47:49 +00001244 }
Justin Lebar260854b2016-02-09 23:03:22 +00001245 return true;
1246}
1247
Brian Homerdingb4b21d82019-07-08 15:57:56 +00001248/// Helper for NoFree inference predicate InstrBreaksAttribute.
1249static bool InstrBreaksNoFree(Instruction &I, const SCCNodeSet &SCCNodes) {
1250 CallSite CS(&I);
1251 if (!CS)
1252 return false;
1253
1254 Function *Callee = CS.getCalledFunction();
1255 if (!Callee)
1256 return true;
1257
1258 if (Callee->doesNotFreeMemory())
1259 return false;
1260
1261 if (SCCNodes.count(Callee) > 0)
1262 return false;
1263
1264 return true;
1265}
1266
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001267/// Infer attributes from all functions in the SCC by scanning every
1268/// instruction for compliance to the attribute assumptions. Currently it
1269/// does:
1270/// - removal of Convergent attribute
1271/// - addition of NoUnwind attribute
1272///
1273/// Returns true if any changes to function attributes were made.
1274static bool inferAttrsFromFunctionBodies(const SCCNodeSet &SCCNodes) {
1275
1276 AttributeInferer AI;
1277
1278 // Request to remove the convergent attribute from all functions in the SCC
1279 // if every callsite within the SCC is not convergent (except for calls
1280 // to functions within the SCC).
1281 // Note: Removal of the attr from the callsites will happen in
1282 // InstCombineCalls separately.
1283 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1284 Attribute::Convergent,
1285 // Skip non-convergent functions.
1286 [](const Function &F) { return !F.isConvergent(); },
1287 // Instructions that break non-convergent assumption.
1288 [SCCNodes](Instruction &I) {
1289 return InstrBreaksNonConvergent(I, SCCNodes);
1290 },
1291 [](Function &F) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001292 LLVM_DEBUG(dbgs() << "Removing convergent attr from fn " << F.getName()
1293 << "\n");
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001294 F.setNotConvergent();
1295 },
1296 /* RequiresExactDefinition= */ false});
1297
1298 if (!DisableNoUnwindInference)
1299 // Request to infer nounwind attribute for all the functions in the SCC if
1300 // every callsite within the SCC is not throwing (except for calls to
1301 // functions within the SCC). Note that nounwind attribute suffers from
1302 // derefinement - results may change depending on how functions are
1303 // optimized. Thus it can be inferred only from exact definitions.
1304 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1305 Attribute::NoUnwind,
1306 // Skip non-throwing functions.
1307 [](const Function &F) { return F.doesNotThrow(); },
1308 // Instructions that break non-throwing assumption.
1309 [SCCNodes](Instruction &I) {
1310 return InstrBreaksNonThrowing(I, SCCNodes);
1311 },
1312 [](Function &F) {
Nicola Zaghend34e60c2018-05-14 12:53:11 +00001313 LLVM_DEBUG(dbgs()
1314 << "Adding nounwind attr to fn " << F.getName() << "\n");
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001315 F.setDoesNotThrow();
1316 ++NumNoUnwind;
1317 },
1318 /* RequiresExactDefinition= */ true});
1319
Brian Homerdingb4b21d82019-07-08 15:57:56 +00001320 if (!DisableNoFreeInference)
1321 // Request to infer nofree attribute for all the functions in the SCC if
1322 // every callsite within the SCC does not directly or indirectly free
1323 // memory (except for calls to functions within the SCC). Note that nofree
1324 // attribute suffers from derefinement - results may change depending on
1325 // how functions are optimized. Thus it can be inferred only from exact
1326 // definitions.
1327 AI.registerAttrInference(AttributeInferer::InferenceDescriptor{
1328 Attribute::NoFree,
1329 // Skip functions known not to free memory.
1330 [](const Function &F) { return F.doesNotFreeMemory(); },
1331 // Instructions that break non-deallocating assumption.
1332 [SCCNodes](Instruction &I) {
1333 return InstrBreaksNoFree(I, SCCNodes);
1334 },
1335 [](Function &F) {
1336 LLVM_DEBUG(dbgs()
1337 << "Adding nofree attr to fn " << F.getName() << "\n");
1338 F.setDoesNotFreeMemory();
1339 ++NumNoFree;
1340 },
1341 /* RequiresExactDefinition= */ true});
1342
Fedor Sergeev6660fd02018-03-23 21:46:16 +00001343 // Perform all the requested attribute inference actions.
1344 return AI.run(SCCNodes);
1345}
1346
James Molloy7e9bdd52015-11-12 10:55:20 +00001347static bool setDoesNotRecurse(Function &F) {
1348 if (F.doesNotRecurse())
1349 return false;
1350 F.setDoesNotRecurse();
1351 ++NumNoRecurse;
1352 return true;
1353}
1354
Chandler Carruth632d2082016-02-13 08:47:51 +00001355static bool addNoRecurseAttrs(const SCCNodeSet &SCCNodes) {
James Molloy7e9bdd52015-11-12 10:55:20 +00001356 // Try and identify functions that do not recurse.
1357
1358 // If the SCC contains multiple nodes we know for sure there is recursion.
Chandler Carruth632d2082016-02-13 08:47:51 +00001359 if (SCCNodes.size() != 1)
James Molloy7e9bdd52015-11-12 10:55:20 +00001360 return false;
1361
Chandler Carruth632d2082016-02-13 08:47:51 +00001362 Function *F = *SCCNodes.begin();
Vivek Pandya11cb15f2019-06-10 04:16:04 +00001363 if (!F || !F->hasExactDefinition() || F->doesNotRecurse())
James Molloy7e9bdd52015-11-12 10:55:20 +00001364 return false;
1365
1366 // If all of the calls in F are identifiable and are to norecurse functions, F
1367 // is norecurse. This check also detects self-recursion as F is not currently
1368 // marked norecurse, so any called from F to F will not be marked norecurse.
Christian Bruel4ead99b2018-12-05 16:48:00 +00001369 for (auto &BB : *F)
1370 for (auto &I : BB.instructionsWithoutDebug())
1371 if (auto CS = CallSite(&I)) {
1372 Function *Callee = CS.getCalledFunction();
1373 if (!Callee || Callee == F || !Callee->doesNotRecurse())
1374 // Function calls a potentially recursive function.
1375 return false;
1376 }
James Molloy7e9bdd52015-11-12 10:55:20 +00001377
Chandler Carruth632d2082016-02-13 08:47:51 +00001378 // Every call was to a non-recursive function other than this function, and
1379 // we have no indirect recursion as the SCC size is one. This function cannot
1380 // recurse.
1381 return setDoesNotRecurse(*F);
James Molloy7e9bdd52015-11-12 10:55:20 +00001382}
1383
Johannes Doerfertbed4bab2018-08-01 16:37:51 +00001384template <typename AARGetterT>
Brian Homerdingb4b21d82019-07-08 15:57:56 +00001385static bool deriveAttrsInPostOrder(SCCNodeSet &SCCNodes,
1386 AARGetterT &&AARGetter,
Johannes Doerfertbed4bab2018-08-01 16:37:51 +00001387 bool HasUnknownCall) {
1388 bool Changed = false;
1389
1390 // Bail if the SCC only contains optnone functions.
1391 if (SCCNodes.empty())
1392 return Changed;
1393
1394 Changed |= addArgumentReturnedAttrs(SCCNodes);
1395 Changed |= addReadAttrs(SCCNodes, AARGetter);
1396 Changed |= addArgumentAttrs(SCCNodes);
1397
1398 // If we have no external nodes participating in the SCC, we can deduce some
1399 // more precise attributes as well.
1400 if (!HasUnknownCall) {
1401 Changed |= addNoAliasAttrs(SCCNodes);
1402 Changed |= addNonNullAttrs(SCCNodes);
1403 Changed |= inferAttrsFromFunctionBodies(SCCNodes);
1404 Changed |= addNoRecurseAttrs(SCCNodes);
1405 }
1406
1407 return Changed;
1408}
1409
Chandler Carruthb47f8012016-03-11 11:05:24 +00001410PreservedAnalyses PostOrderFunctionAttrsPass::run(LazyCallGraph::SCC &C,
Chandler Carruth88823462016-08-24 09:37:14 +00001411 CGSCCAnalysisManager &AM,
1412 LazyCallGraph &CG,
1413 CGSCCUpdateResult &) {
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001414 FunctionAnalysisManager &FAM =
Chandler Carruth88823462016-08-24 09:37:14 +00001415 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001416
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001417 // We pass a lambda into functions to wire them up to the analysis manager
1418 // for getting function analyses.
1419 auto AARGetter = [&](Function &F) -> AAResults & {
1420 return FAM.getResult<AAManager>(F);
1421 };
1422
1423 // Fill SCCNodes with the elements of the SCC. Also track whether there are
1424 // any external or opt-none nodes that will prevent us from optimizing any
1425 // part of the SCC.
1426 SCCNodeSet SCCNodes;
1427 bool HasUnknownCall = false;
1428 for (LazyCallGraph::Node &N : C) {
1429 Function &F = N.getFunction();
Evandro Menezes85bd3972019-04-04 22:40:06 +00001430 if (F.hasOptNone() || F.hasFnAttribute(Attribute::Naked)) {
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001431 // Treat any function we're trying not to optimize as if it were an
1432 // indirect call and omit it from the node set used below.
1433 HasUnknownCall = true;
1434 continue;
1435 }
1436 // Track whether any functions in this SCC have an unknown call edge.
1437 // Note: if this is ever a performance hit, we can common it with
1438 // subsequent routines which also do scans over the instructions of the
1439 // function.
1440 if (!HasUnknownCall)
1441 for (Instruction &I : instructions(F))
1442 if (auto CS = CallSite(&I))
1443 if (!CS.getCalledFunction()) {
1444 HasUnknownCall = true;
1445 break;
1446 }
1447
1448 SCCNodes.insert(&F);
1449 }
1450
Johannes Doerfertbed4bab2018-08-01 16:37:51 +00001451 if (deriveAttrsInPostOrder(SCCNodes, AARGetter, HasUnknownCall))
1452 return PreservedAnalyses::none();
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001453
Johannes Doerfertbed4bab2018-08-01 16:37:51 +00001454 return PreservedAnalyses::all();
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001455}
1456
1457namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001458
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001459struct PostOrderFunctionAttrsLegacyPass : public CallGraphSCCPass {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001460 // Pass identification, replacement for typeid
1461 static char ID;
1462
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001463 PostOrderFunctionAttrsLegacyPass() : CallGraphSCCPass(ID) {
Chad Rosier611b73b2016-11-07 16:28:04 +00001464 initializePostOrderFunctionAttrsLegacyPassPass(
1465 *PassRegistry::getPassRegistry());
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001466 }
1467
1468 bool runOnSCC(CallGraphSCC &SCC) override;
1469
1470 void getAnalysisUsage(AnalysisUsage &AU) const override {
1471 AU.setPreservesCFG();
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001472 AU.addRequired<AssumptionCacheTracker>();
Chandler Carruth12884f72016-03-02 15:56:53 +00001473 getAAResultsAnalysisUsage(AU);
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001474 CallGraphSCCPass::getAnalysisUsage(AU);
1475 }
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001476};
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001477
1478} // end anonymous namespace
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001479
1480char PostOrderFunctionAttrsLegacyPass::ID = 0;
1481INITIALIZE_PASS_BEGIN(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1482 "Deduce function attributes", false, false)
Daniel Jasperaec2fa32016-12-19 08:22:17 +00001483INITIALIZE_PASS_DEPENDENCY(AssumptionCacheTracker)
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001484INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001485INITIALIZE_PASS_END(PostOrderFunctionAttrsLegacyPass, "functionattrs",
1486 "Deduce function attributes", false, false)
1487
Chad Rosier611b73b2016-11-07 16:28:04 +00001488Pass *llvm::createPostOrderFunctionAttrsLegacyPass() {
1489 return new PostOrderFunctionAttrsLegacyPass();
1490}
Chandler Carruth9c4ed172016-02-18 11:03:11 +00001491
Sean Silva997cbea2016-07-03 03:35:03 +00001492template <typename AARGetterT>
1493static bool runImpl(CallGraphSCC &SCC, AARGetterT AARGetter) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001494
1495 // Fill SCCNodes with the elements of the SCC. Used for quickly looking up
1496 // whether a given CallGraphNode is in this SCC. Also track whether there are
1497 // any external or opt-none nodes that will prevent us from optimizing any
1498 // part of the SCC.
1499 SCCNodeSet SCCNodes;
1500 bool ExternalNode = false;
Benjamin Kramer135f7352016-06-26 12:28:59 +00001501 for (CallGraphNode *I : SCC) {
1502 Function *F = I->getFunction();
Evandro Menezes85bd3972019-04-04 22:40:06 +00001503 if (!F || F->hasOptNone() || F->hasFnAttribute(Attribute::Naked)) {
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001504 // External node or function we're trying not to optimize - we both avoid
1505 // transform them and avoid leveraging information they provide.
1506 ExternalNode = true;
1507 continue;
1508 }
1509
1510 SCCNodes.insert(F);
1511 }
1512
Johannes Doerfertbed4bab2018-08-01 16:37:51 +00001513 return deriveAttrsInPostOrder(SCCNodes, AARGetter, ExternalNode);
James Molloy7e9bdd52015-11-12 10:55:20 +00001514}
Chandler Carruthc518ebd2015-10-29 18:29:15 +00001515
Sean Silva997cbea2016-07-03 03:35:03 +00001516bool PostOrderFunctionAttrsLegacyPass::runOnSCC(CallGraphSCC &SCC) {
1517 if (skipSCC(SCC))
1518 return false;
Peter Collingbournecea1e4e2017-02-09 23:11:52 +00001519 return runImpl(SCC, LegacyAARGetter(*this));
Sean Silva997cbea2016-07-03 03:35:03 +00001520}
1521
Chandler Carruth1926b702016-01-08 10:55:52 +00001522namespace {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001523
Sean Silvaf5080192016-06-12 07:48:51 +00001524struct ReversePostOrderFunctionAttrsLegacyPass : public ModulePass {
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001525 // Pass identification, replacement for typeid
1526 static char ID;
1527
Sean Silvaf5080192016-06-12 07:48:51 +00001528 ReversePostOrderFunctionAttrsLegacyPass() : ModulePass(ID) {
Chad Rosier611b73b2016-11-07 16:28:04 +00001529 initializeReversePostOrderFunctionAttrsLegacyPassPass(
1530 *PassRegistry::getPassRegistry());
Chandler Carruth1926b702016-01-08 10:55:52 +00001531 }
1532
1533 bool runOnModule(Module &M) override;
1534
1535 void getAnalysisUsage(AnalysisUsage &AU) const override {
1536 AU.setPreservesCFG();
1537 AU.addRequired<CallGraphWrapperPass>();
Mehdi Amini0ddf4042016-05-02 18:03:33 +00001538 AU.addPreserved<CallGraphWrapperPass>();
Chandler Carruth1926b702016-01-08 10:55:52 +00001539 }
1540};
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001541
1542} // end anonymous namespace
Chandler Carruth1926b702016-01-08 10:55:52 +00001543
Sean Silvaf5080192016-06-12 07:48:51 +00001544char ReversePostOrderFunctionAttrsLegacyPass::ID = 0;
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001545
Sean Silvaf5080192016-06-12 07:48:51 +00001546INITIALIZE_PASS_BEGIN(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
Chandler Carruth1926b702016-01-08 10:55:52 +00001547 "Deduce function attributes in RPO", false, false)
1548INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
Sean Silvaf5080192016-06-12 07:48:51 +00001549INITIALIZE_PASS_END(ReversePostOrderFunctionAttrsLegacyPass, "rpo-functionattrs",
Chandler Carruth1926b702016-01-08 10:55:52 +00001550 "Deduce function attributes in RPO", false, false)
1551
1552Pass *llvm::createReversePostOrderFunctionAttrsPass() {
Sean Silvaf5080192016-06-12 07:48:51 +00001553 return new ReversePostOrderFunctionAttrsLegacyPass();
Chandler Carruth1926b702016-01-08 10:55:52 +00001554}
1555
1556static bool addNoRecurseAttrsTopDown(Function &F) {
1557 // We check the preconditions for the function prior to calling this to avoid
1558 // the cost of building up a reversible post-order list. We assert them here
1559 // to make sure none of the invariants this relies on were violated.
1560 assert(!F.isDeclaration() && "Cannot deduce norecurse without a definition!");
1561 assert(!F.doesNotRecurse() &&
1562 "This function has already been deduced as norecurs!");
1563 assert(F.hasInternalLinkage() &&
1564 "Can only do top-down deduction for internal linkage functions!");
1565
1566 // If F is internal and all of its uses are calls from a non-recursive
1567 // functions, then none of its calls could in fact recurse without going
1568 // through a function marked norecurse, and so we can mark this function too
1569 // as norecurse. Note that the uses must actually be calls -- otherwise
1570 // a pointer to this function could be returned from a norecurse function but
1571 // this function could be recursively (indirectly) called. Note that this
1572 // also detects if F is directly recursive as F is not yet marked as
1573 // a norecurse function.
1574 for (auto *U : F.users()) {
1575 auto *I = dyn_cast<Instruction>(U);
1576 if (!I)
1577 return false;
1578 CallSite CS(I);
1579 if (!CS || !CS.getParent()->getParent()->doesNotRecurse())
1580 return false;
1581 }
1582 return setDoesNotRecurse(F);
1583}
1584
Sean Silvaadc79392016-06-12 05:44:51 +00001585static bool deduceFunctionAttributeInRPO(Module &M, CallGraph &CG) {
Chandler Carruth1926b702016-01-08 10:55:52 +00001586 // We only have a post-order SCC traversal (because SCCs are inherently
1587 // discovered in post-order), so we accumulate them in a vector and then walk
1588 // it in reverse. This is simpler than using the RPO iterator infrastructure
1589 // because we need to combine SCC detection and the PO walk of the call
1590 // graph. We can also cheat egregiously because we're primarily interested in
1591 // synthesizing norecurse and so we can only save the singular SCCs as SCCs
1592 // with multiple functions in them will clearly be recursive.
Chandler Carruth1926b702016-01-08 10:55:52 +00001593 SmallVector<Function *, 16> Worklist;
1594 for (scc_iterator<CallGraph *> I = scc_begin(&CG); !I.isAtEnd(); ++I) {
1595 if (I->size() != 1)
1596 continue;
1597
1598 Function *F = I->front()->getFunction();
1599 if (F && !F->isDeclaration() && !F->doesNotRecurse() &&
1600 F->hasInternalLinkage())
1601 Worklist.push_back(F);
1602 }
1603
James Molloy7e9bdd52015-11-12 10:55:20 +00001604 bool Changed = false;
Eugene Zelenkof27d1612017-10-19 21:21:30 +00001605 for (auto *F : llvm::reverse(Worklist))
Chandler Carruth1926b702016-01-08 10:55:52 +00001606 Changed |= addNoRecurseAttrsTopDown(*F);
1607
Duncan Sands44c8cd92008-12-31 16:14:43 +00001608 return Changed;
1609}
Sean Silvaadc79392016-06-12 05:44:51 +00001610
Sean Silvaf5080192016-06-12 07:48:51 +00001611bool ReversePostOrderFunctionAttrsLegacyPass::runOnModule(Module &M) {
Sean Silvaadc79392016-06-12 05:44:51 +00001612 if (skipModule(M))
1613 return false;
1614
1615 auto &CG = getAnalysis<CallGraphWrapperPass>().getCallGraph();
1616
1617 return deduceFunctionAttributeInRPO(M, CG);
1618}
Sean Silvaf5080192016-06-12 07:48:51 +00001619
1620PreservedAnalyses
Sean Silvafd03ac62016-08-09 00:28:38 +00001621ReversePostOrderFunctionAttrsPass::run(Module &M, ModuleAnalysisManager &AM) {
Sean Silvaf5080192016-06-12 07:48:51 +00001622 auto &CG = AM.getResult<CallGraphAnalysis>(M);
1623
Chandler Carruth6acdca72017-01-24 12:55:57 +00001624 if (!deduceFunctionAttributeInRPO(M, CG))
Sean Silvaf5080192016-06-12 07:48:51 +00001625 return PreservedAnalyses::all();
Chandler Carruth6acdca72017-01-24 12:55:57 +00001626
Sean Silvaf5080192016-06-12 07:48:51 +00001627 PreservedAnalyses PA;
1628 PA.preserve<CallGraphAnalysis>();
1629 return PA;
1630}