blob: aaa912b7c02d8b7b3eff341a0bbd78a13f9eea29 [file] [log] [blame]
Johannes Doerfertaade7822019-06-05 03:02:24 +00001//===- Attributor.cpp - Module-wide attribute deduction -------------------===//
2//
3// 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
6//
7//===----------------------------------------------------------------------===//
8//
9// This file implements an inter procedural pass that deduces and/or propagating
10// attributes. This is done in an abstract interpretation style fixpoint
11// iteration. See the Attributor.h file comment and the class descriptions in
12// that file for more information.
13//
14//===----------------------------------------------------------------------===//
15
Johannes Doerfert368f7ee2019-12-30 16:12:36 -060016#include "llvm/Transforms/IPO/Attributor.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000017
Hideto Ueno11d37102019-07-17 15:15:43 +000018#include "llvm/ADT/DepthFirstIterator.h"
Stefan Stipanovic6058b862019-07-22 23:58:23 +000019#include "llvm/ADT/STLExtras.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000020#include "llvm/ADT/SmallPtrSet.h"
21#include "llvm/ADT/SmallVector.h"
22#include "llvm/ADT/Statistic.h"
Johannes Doerfertb0c77c32019-11-27 00:30:12 -060023#include "llvm/Analysis/CallGraph.h"
24#include "llvm/Analysis/CallGraphSCCPass.h"
Stefan Stipanovic69ebb022019-07-22 19:36:27 +000025#include "llvm/Analysis/CaptureTracking.h"
Johannes Doerfert924d2132019-08-05 21:34:45 +000026#include "llvm/Analysis/EHPersonalities.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000027#include "llvm/Analysis/GlobalsModRef.h"
Hideto Ueno188f9a32020-01-15 15:25:52 +090028#include "llvm/Analysis/LazyValueInfo.h"
Hideto Ueno19c07af2019-07-23 08:16:17 +000029#include "llvm/Analysis/Loads.h"
Stefan Stipanovic431141c2019-09-15 21:47:41 +000030#include "llvm/Analysis/MemoryBuiltins.h"
Hideto Ueno188f9a32020-01-15 15:25:52 +090031#include "llvm/Analysis/ScalarEvolution.h"
Hideto Ueno54869ec2019-07-15 06:49:04 +000032#include "llvm/Analysis/ValueTracking.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000033#include "llvm/IR/Argument.h"
34#include "llvm/IR/Attributes.h"
Hideto Ueno11d37102019-07-17 15:15:43 +000035#include "llvm/IR/CFG.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050036#include "llvm/IR/IRBuilder.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000037#include "llvm/IR/InstIterator.h"
Stefan Stipanovic06263672019-07-11 21:37:40 +000038#include "llvm/IR/IntrinsicInst.h"
Johannes Doerferta4088c72020-01-07 16:01:57 -060039#include "llvm/IR/Verifier.h"
Reid Kleckner05da2fe2019-11-13 13:15:01 -080040#include "llvm/InitializePasses.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050041#include "llvm/IR/NoFolder.h"
Johannes Doerfertaade7822019-06-05 03:02:24 +000042#include "llvm/Support/CommandLine.h"
43#include "llvm/Support/Debug.h"
44#include "llvm/Support/raw_ostream.h"
Johannes Doerfert89c2e732019-10-30 17:20:20 -050045#include "llvm/Transforms/IPO/ArgumentPromotion.h"
Stefan Stipanovic6058b862019-07-22 23:58:23 +000046#include "llvm/Transforms/Utils/BasicBlockUtils.h"
47#include "llvm/Transforms/Utils/Local.h"
48
Johannes Doerfertaade7822019-06-05 03:02:24 +000049#include <cassert>
50
51using namespace llvm;
52
53#define DEBUG_TYPE "attributor"
54
55STATISTIC(NumFnWithExactDefinition,
56 "Number of function with exact definitions");
57STATISTIC(NumFnWithoutExactDefinition,
58 "Number of function without exact definitions");
59STATISTIC(NumAttributesTimedOut,
60 "Number of abstract attributes timed out before fixpoint");
61STATISTIC(NumAttributesValidFixpoint,
62 "Number of abstract attributes in a valid fixpoint state");
63STATISTIC(NumAttributesManifested,
64 "Number of abstract attributes manifested in IR");
Johannes Doerfert680f6382019-11-02 02:48:05 -050065STATISTIC(NumAttributesFixedDueToRequiredDependences,
66 "Number of abstract attributes fixed due to required dependences");
Johannes Doerfertaade7822019-06-05 03:02:24 +000067
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000068// Some helper macros to deal with statistics tracking.
69//
70// Usage:
71// For simple IR attribute tracking overload trackStatistics in the abstract
Johannes Doerfert17b578b2019-08-14 21:46:25 +000072// attribute and choose the right STATS_DECLTRACK_********* macro,
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000073// e.g.,:
74// void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +000075// STATS_DECLTRACK_ARG_ATTR(returned)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000076// }
77// If there is a single "increment" side one can use the macro
Johannes Doerfert17b578b2019-08-14 21:46:25 +000078// STATS_DECLTRACK with a custom message. If there are multiple increment
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000079// sides, STATS_DECL and STATS_TRACK can also be used separatly.
80//
81#define BUILD_STAT_MSG_IR_ATTR(TYPE, NAME) \
82 ("Number of " #TYPE " marked '" #NAME "'")
83#define BUILD_STAT_NAME(NAME, TYPE) NumIR##TYPE##_##NAME
Johannes Doerferta7a3b3a2019-09-04 19:01:08 +000084#define STATS_DECL_(NAME, MSG) STATISTIC(NAME, MSG);
85#define STATS_DECL(NAME, TYPE, MSG) \
86 STATS_DECL_(BUILD_STAT_NAME(NAME, TYPE), MSG);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +000087#define STATS_TRACK(NAME, TYPE) ++(BUILD_STAT_NAME(NAME, TYPE));
Johannes Doerfert17b578b2019-08-14 21:46:25 +000088#define STATS_DECLTRACK(NAME, TYPE, MSG) \
Johannes Doerfert169af992019-08-20 06:09:56 +000089 { \
90 STATS_DECL(NAME, TYPE, MSG) \
91 STATS_TRACK(NAME, TYPE) \
92 }
Johannes Doerfert17b578b2019-08-14 21:46:25 +000093#define STATS_DECLTRACK_ARG_ATTR(NAME) \
94 STATS_DECLTRACK(NAME, Arguments, BUILD_STAT_MSG_IR_ATTR(arguments, NAME))
95#define STATS_DECLTRACK_CSARG_ATTR(NAME) \
96 STATS_DECLTRACK(NAME, CSArguments, \
97 BUILD_STAT_MSG_IR_ATTR(call site arguments, NAME))
98#define STATS_DECLTRACK_FN_ATTR(NAME) \
99 STATS_DECLTRACK(NAME, Function, BUILD_STAT_MSG_IR_ATTR(functions, NAME))
100#define STATS_DECLTRACK_CS_ATTR(NAME) \
101 STATS_DECLTRACK(NAME, CS, BUILD_STAT_MSG_IR_ATTR(call site, NAME))
102#define STATS_DECLTRACK_FNRET_ATTR(NAME) \
103 STATS_DECLTRACK(NAME, FunctionReturn, \
Johannes Doerfert2db85282019-08-21 20:56:56 +0000104 BUILD_STAT_MSG_IR_ATTR(function returns, NAME))
Johannes Doerfert17b578b2019-08-14 21:46:25 +0000105#define STATS_DECLTRACK_CSRET_ATTR(NAME) \
106 STATS_DECLTRACK(NAME, CSReturn, \
107 BUILD_STAT_MSG_IR_ATTR(call site returns, NAME))
108#define STATS_DECLTRACK_FLOATING_ATTR(NAME) \
109 STATS_DECLTRACK(NAME, Floating, \
110 ("Number of floating values known to be '" #NAME "'"))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000111
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600112// Specialization of the operator<< for abstract attributes subclasses. This
113// disambiguates situations where multiple operators are applicable.
114namespace llvm {
115#define PIPE_OPERATOR(CLASS) \
116 raw_ostream &operator<<(raw_ostream &OS, const CLASS &AA) { \
117 return OS << static_cast<const AbstractAttribute &>(AA); \
118 }
119
120PIPE_OPERATOR(AAIsDead)
121PIPE_OPERATOR(AANoUnwind)
122PIPE_OPERATOR(AANoSync)
123PIPE_OPERATOR(AANoRecurse)
124PIPE_OPERATOR(AAWillReturn)
125PIPE_OPERATOR(AANoReturn)
126PIPE_OPERATOR(AAReturnedValues)
127PIPE_OPERATOR(AANonNull)
128PIPE_OPERATOR(AANoAlias)
129PIPE_OPERATOR(AADereferenceable)
130PIPE_OPERATOR(AAAlign)
131PIPE_OPERATOR(AANoCapture)
132PIPE_OPERATOR(AAValueSimplify)
133PIPE_OPERATOR(AANoFree)
134PIPE_OPERATOR(AAHeapToStack)
135PIPE_OPERATOR(AAReachability)
136PIPE_OPERATOR(AAMemoryBehavior)
Johannes Doerfert282f5d72020-01-26 02:51:57 -0600137PIPE_OPERATOR(AAMemoryLocation)
Hideto Ueno188f9a32020-01-15 15:25:52 +0900138PIPE_OPERATOR(AAValueConstantRange)
Johannes Doerfert89c2e732019-10-30 17:20:20 -0500139PIPE_OPERATOR(AAPrivatizablePtr)
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600140
141#undef PIPE_OPERATOR
142} // namespace llvm
143
Johannes Doerfertaade7822019-06-05 03:02:24 +0000144// TODO: Determine a good default value.
145//
146// In the LLVM-TS and SPEC2006, 32 seems to not induce compile time overheads
147// (when run with the first 5 abstract attributes). The results also indicate
148// that we never reach 32 iterations but always find a fixpoint sooner.
149//
150// This will become more evolved once we perform two interleaved fixpoint
151// iterations: bottom-up and top-down.
152static cl::opt<unsigned>
153 MaxFixpointIterations("attributor-max-iterations", cl::Hidden,
154 cl::desc("Maximal number of fixpoint iterations."),
155 cl::init(32));
Johannes Doerfertb504eb82019-08-26 18:55:47 +0000156static cl::opt<bool> VerifyMaxFixpointIterations(
157 "attributor-max-iterations-verify", cl::Hidden,
158 cl::desc("Verify that max-iterations is a tight bound for a fixpoint"),
159 cl::init(false));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000160
161static cl::opt<bool> DisableAttributor(
162 "attributor-disable", cl::Hidden,
163 cl::desc("Disable the attributor inter-procedural deduction pass."),
Johannes Doerfert282d34e2019-06-14 14:53:41 +0000164 cl::init(true));
Johannes Doerfertaade7822019-06-05 03:02:24 +0000165
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -0500166static cl::opt<bool> AnnotateDeclarationCallSites(
167 "attributor-annotate-decl-cs", cl::Hidden,
James Hendersond68904f2020-01-06 10:15:44 +0000168 cl::desc("Annotate call sites of function declarations."), cl::init(false));
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -0500169
Johannes Doerfert7516a5e2019-09-03 20:37:24 +0000170static cl::opt<bool> ManifestInternal(
171 "attributor-manifest-internal", cl::Hidden,
172 cl::desc("Manifest Attributor internal string attributes."),
173 cl::init(false));
174
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +0000175static cl::opt<unsigned> DepRecInterval(
176 "attributor-dependence-recompute-interval", cl::Hidden,
177 cl::desc("Number of iterations until dependences are recomputed."),
178 cl::init(4));
179
Stefan Stipanovic431141c2019-09-15 21:47:41 +0000180static cl::opt<bool> EnableHeapToStack("enable-heap-to-stack-conversion",
181 cl::init(true), cl::Hidden);
182
Johannes Doerfert1c2afae2019-10-10 05:34:21 +0000183static cl::opt<int> MaxHeapToStackSize("max-heap-to-stack-size", cl::init(128),
184 cl::Hidden);
Stefan Stipanovic431141c2019-09-15 21:47:41 +0000185
Johannes Doerfertaade7822019-06-05 03:02:24 +0000186/// Logic operators for the change status enum class.
187///
188///{
189ChangeStatus llvm::operator|(ChangeStatus l, ChangeStatus r) {
190 return l == ChangeStatus::CHANGED ? l : r;
191}
192ChangeStatus llvm::operator&(ChangeStatus l, ChangeStatus r) {
193 return l == ChangeStatus::UNCHANGED ? l : r;
194}
195///}
196
Johannes Doerfertb1b441d2019-10-10 01:19:57 -0500197Argument *IRPosition::getAssociatedArgument() const {
198 if (getPositionKind() == IRP_ARGUMENT)
199 return cast<Argument>(&getAnchorValue());
200
201 // Not an Argument and no argument number means this is not a call site
202 // argument, thus we cannot find a callback argument to return.
203 int ArgNo = getArgNo();
204 if (ArgNo < 0)
205 return nullptr;
206
207 // Use abstract call sites to make the connection between the call site
208 // values and the ones in callbacks. If a callback was found that makes use
209 // of the underlying call site operand, we want the corresponding callback
210 // callee argument and not the direct callee argument.
211 Optional<Argument *> CBCandidateArg;
212 SmallVector<const Use *, 4> CBUses;
213 ImmutableCallSite ICS(&getAnchorValue());
214 AbstractCallSite::getCallbackUses(ICS, CBUses);
215 for (const Use *U : CBUses) {
216 AbstractCallSite ACS(U);
217 assert(ACS && ACS.isCallbackCall());
218 if (!ACS.getCalledFunction())
219 continue;
220
221 for (unsigned u = 0, e = ACS.getNumArgOperands(); u < e; u++) {
222
223 // Test if the underlying call site operand is argument number u of the
224 // callback callee.
225 if (ACS.getCallArgOperandNo(u) != ArgNo)
226 continue;
227
228 assert(ACS.getCalledFunction()->arg_size() > u &&
229 "ACS mapped into var-args arguments!");
230 if (CBCandidateArg.hasValue()) {
231 CBCandidateArg = nullptr;
232 break;
233 }
234 CBCandidateArg = ACS.getCalledFunction()->getArg(u);
235 }
236 }
237
238 // If we found a unique callback candidate argument, return it.
239 if (CBCandidateArg.hasValue() && CBCandidateArg.getValue())
240 return CBCandidateArg.getValue();
241
242 // If no callbacks were found, or none used the underlying call site operand
243 // exclusively, use the direct callee argument if available.
244 const Function *Callee = ICS.getCalledFunction();
245 if (Callee && Callee->arg_size() > unsigned(ArgNo))
246 return Callee->getArg(ArgNo);
247
248 return nullptr;
249}
250
Johannes Doerfert214ed3f2020-01-11 23:35:45 -0600251static Optional<ConstantInt *>
252getAssumedConstant(Attributor &A, const Value &V, const AbstractAttribute &AA,
253 bool &UsedAssumedInformation) {
254 const auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
255 AA, IRPosition::value(V), /* TrackDependence */ false);
256 Optional<Value *> SimplifiedV = ValueSimplifyAA.getAssumedSimplifiedValue(A);
257 bool IsKnown = ValueSimplifyAA.isKnown();
258 UsedAssumedInformation |= !IsKnown;
259 if (!SimplifiedV.hasValue()) {
260 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
261 return llvm::None;
262 }
263 if (isa_and_nonnull<UndefValue>(SimplifiedV.getValue())) {
264 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
265 return llvm::None;
266 }
267 ConstantInt *CI = dyn_cast_or_null<ConstantInt>(SimplifiedV.getValue());
268 if (CI)
269 A.recordDependence(ValueSimplifyAA, AA, DepClassTy::OPTIONAL);
270 return CI;
271}
272
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -0600273/// Get pointer operand of memory accessing instruction. If \p I is
274/// not a memory accessing instruction, return nullptr. If \p AllowVolatile,
275/// is set to false and the instruction is volatile, return nullptr.
276static const Value *getPointerOperand(const Instruction *I,
277 bool AllowVolatile) {
278 if (auto *LI = dyn_cast<LoadInst>(I)) {
279 if (!AllowVolatile && LI->isVolatile())
280 return nullptr;
281 return LI->getPointerOperand();
282 }
283
284 if (auto *SI = dyn_cast<StoreInst>(I)) {
285 if (!AllowVolatile && SI->isVolatile())
286 return nullptr;
287 return SI->getPointerOperand();
288 }
289
290 if (auto *CXI = dyn_cast<AtomicCmpXchgInst>(I)) {
291 if (!AllowVolatile && CXI->isVolatile())
292 return nullptr;
293 return CXI->getPointerOperand();
294 }
295
296 if (auto *RMWI = dyn_cast<AtomicRMWInst>(I)) {
297 if (!AllowVolatile && RMWI->isVolatile())
298 return nullptr;
299 return RMWI->getPointerOperand();
300 }
301
302 return nullptr;
303}
304
Johannes Doerfert89c2e732019-10-30 17:20:20 -0500305/// Helper function to create a pointer of type \p ResTy, based on \p Ptr, and
306/// advanced by \p Offset bytes. To aid later analysis the method tries to build
307/// getelement pointer instructions that traverse the natural type of \p Ptr if
308/// possible. If that fails, the remaining offset is adjusted byte-wise, hence
309/// through a cast to i8*.
310///
311/// TODO: This could probably live somewhere more prominantly if it doesn't
312/// already exist.
313static Value *constructPointer(Type *ResTy, Value *Ptr, int64_t Offset,
314 IRBuilder<NoFolder> &IRB, const DataLayout &DL) {
315 assert(Offset >= 0 && "Negative offset not supported yet!");
316 LLVM_DEBUG(dbgs() << "Construct pointer: " << *Ptr << " + " << Offset
317 << "-bytes as " << *ResTy << "\n");
318
319 // The initial type we are trying to traverse to get nice GEPs.
320 Type *Ty = Ptr->getType();
321
322 SmallVector<Value *, 4> Indices;
323 std::string GEPName = Ptr->getName().str();
324 while (Offset) {
325 uint64_t Idx, Rem;
326
327 if (auto *STy = dyn_cast<StructType>(Ty)) {
328 const StructLayout *SL = DL.getStructLayout(STy);
329 if (int64_t(SL->getSizeInBytes()) < Offset)
330 break;
331 Idx = SL->getElementContainingOffset(Offset);
332 assert(Idx < STy->getNumElements() && "Offset calculation error!");
333 Rem = Offset - SL->getElementOffset(Idx);
334 Ty = STy->getElementType(Idx);
335 } else if (auto *PTy = dyn_cast<PointerType>(Ty)) {
336 Ty = PTy->getElementType();
337 if (!Ty->isSized())
338 break;
339 uint64_t ElementSize = DL.getTypeAllocSize(Ty);
340 assert(ElementSize && "Expected type with size!");
341 Idx = Offset / ElementSize;
342 Rem = Offset % ElementSize;
343 } else {
344 // Non-aggregate type, we cast and make byte-wise progress now.
345 break;
346 }
347
348 LLVM_DEBUG(errs() << "Ty: " << *Ty << " Offset: " << Offset
349 << " Idx: " << Idx << " Rem: " << Rem << "\n");
350
351 GEPName += "." + std::to_string(Idx);
352 Indices.push_back(ConstantInt::get(IRB.getInt32Ty(), Idx));
353 Offset = Rem;
354 }
355
356 // Create a GEP if we collected indices above.
357 if (Indices.size())
358 Ptr = IRB.CreateGEP(Ptr, Indices, GEPName);
359
360 // If an offset is left we use byte-wise adjustment.
361 if (Offset) {
362 Ptr = IRB.CreateBitCast(Ptr, IRB.getInt8PtrTy());
363 Ptr = IRB.CreateGEP(Ptr, IRB.getInt32(Offset),
364 GEPName + ".b" + Twine(Offset));
365 }
366
367 // Ensure the result has the requested type.
368 Ptr = IRB.CreateBitOrPointerCast(Ptr, ResTy, Ptr->getName() + ".cast");
369
370 LLVM_DEBUG(dbgs() << "Constructed pointer: " << *Ptr << "\n");
371 return Ptr;
372}
373
Johannes Doerfertdef99282019-08-14 21:29:37 +0000374/// Recursively visit all values that might become \p IRP at some point. This
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000375/// will be done by looking through cast instructions, selects, phis, and calls
Johannes Doerfertdef99282019-08-14 21:29:37 +0000376/// with the "returned" attribute. Once we cannot look through the value any
377/// further, the callback \p VisitValueCB is invoked and passed the current
Johannes Doerfert52aec322020-02-11 15:11:32 -0600378/// value, the \p State, and a flag to indicate if we stripped anything.
379/// Stripped means that we unpacked the value associated with \p IRP at least
380/// once. Note that the value used for the callback may still be the value
381/// associated with \p IRP (due to PHIs). To limit how much effort is invested,
382/// we will never visit more values than specified by \p MaxValues.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000383template <typename AAType, typename StateTy>
Benjamin Kramerdf4b9a32019-09-17 12:56:29 +0000384static bool genericValueTraversal(
Johannes Doerfertdef99282019-08-14 21:29:37 +0000385 Attributor &A, IRPosition IRP, const AAType &QueryingAA, StateTy &State,
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000386 const function_ref<bool(Value &, StateTy &, bool)> &VisitValueCB,
Johannes Doerfert282f5d72020-01-26 02:51:57 -0600387 int MaxValues = 8, const function_ref<Value *(Value *)> StripCB = nullptr) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000388
Johannes Doerfertdef99282019-08-14 21:29:37 +0000389 const AAIsDead *LivenessAA = nullptr;
390 if (IRP.getAnchorScope())
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000391 LivenessAA = &A.getAAFor<AAIsDead>(
Johannes Doerfert19b00432019-08-26 17:48:05 +0000392 QueryingAA, IRPosition::function(*IRP.getAnchorScope()),
393 /* TrackDependence */ false);
394 bool AnyDead = false;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000395
396 // TODO: Use Positions here to allow context sensitivity in VisitValueCB
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000397 SmallPtrSet<Value *, 16> Visited;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000398 SmallVector<Value *, 16> Worklist;
Johannes Doerfertdef99282019-08-14 21:29:37 +0000399 Worklist.push_back(&IRP.getAssociatedValue());
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000400
401 int Iteration = 0;
402 do {
403 Value *V = Worklist.pop_back_val();
Johannes Doerfert282f5d72020-01-26 02:51:57 -0600404 if (StripCB)
405 V = StripCB(V);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000406
407 // Check if we should process the current value. To prevent endless
408 // recursion keep a record of the values we followed!
Johannes Doerfertdef99282019-08-14 21:29:37 +0000409 if (!Visited.insert(V).second)
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000410 continue;
411
412 // Make sure we limit the compile time for complex expressions.
413 if (Iteration++ >= MaxValues)
414 return false;
415
416 // Explicitly look through calls with a "returned" attribute if we do
417 // not have a pointer as stripPointerCasts only works on them.
Johannes Doerfertdef99282019-08-14 21:29:37 +0000418 Value *NewV = nullptr;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000419 if (V->getType()->isPointerTy()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +0000420 NewV = V->stripPointerCasts();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000421 } else {
422 CallSite CS(V);
423 if (CS && CS.getCalledFunction()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000424 for (Argument &Arg : CS.getCalledFunction()->args())
425 if (Arg.hasReturnedAttr()) {
426 NewV = CS.getArgOperand(Arg.getArgNo());
427 break;
428 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000429 }
430 }
Johannes Doerfertdef99282019-08-14 21:29:37 +0000431 if (NewV && NewV != V) {
432 Worklist.push_back(NewV);
433 continue;
434 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000435
436 // Look through select instructions, visit both potential values.
437 if (auto *SI = dyn_cast<SelectInst>(V)) {
438 Worklist.push_back(SI->getTrueValue());
439 Worklist.push_back(SI->getFalseValue());
440 continue;
441 }
442
Johannes Doerfertdef99282019-08-14 21:29:37 +0000443 // Look through phi nodes, visit all live operands.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000444 if (auto *PHI = dyn_cast<PHINode>(V)) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000445 assert(LivenessAA &&
446 "Expected liveness in the presence of instructions!");
Johannes Doerfertdef99282019-08-14 21:29:37 +0000447 for (unsigned u = 0, e = PHI->getNumIncomingValues(); u < e; u++) {
448 const BasicBlock *IncomingBB = PHI->getIncomingBlock(u);
Johannes Doerfert23f41f12020-01-12 01:09:22 -0600449 if (A.isAssumedDead(*IncomingBB->getTerminator(), &QueryingAA,
450 LivenessAA,
451 /* CheckBBLivenessOnly */ true)) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +0000452 AnyDead = true;
Johannes Doerfert19b00432019-08-26 17:48:05 +0000453 continue;
454 }
455 Worklist.push_back(PHI->getIncomingValue(u));
Johannes Doerfertdef99282019-08-14 21:29:37 +0000456 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000457 continue;
458 }
459
460 // Once a leaf is reached we inform the user through the callback.
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000461 if (!VisitValueCB(*V, State, Iteration > 1))
462 return false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000463 } while (!Worklist.empty());
464
Johannes Doerfert19b00432019-08-26 17:48:05 +0000465 // If we actually used liveness information so we have to record a dependence.
466 if (AnyDead)
Johannes Doerfert680f6382019-11-02 02:48:05 -0500467 A.recordDependence(*LivenessAA, QueryingAA, DepClassTy::OPTIONAL);
Johannes Doerfert19b00432019-08-26 17:48:05 +0000468
Johannes Doerfertaccd3e82019-07-08 23:27:20 +0000469 // All values have been visited.
470 return true;
471}
472
Johannes Doerfertaade7822019-06-05 03:02:24 +0000473/// Return true if \p New is equal or worse than \p Old.
474static bool isEqualOrWorse(const Attribute &New, const Attribute &Old) {
475 if (!Old.isIntAttribute())
476 return true;
477
478 return Old.getValueAsInt() >= New.getValueAsInt();
479}
480
481/// Return true if the information provided by \p Attr was added to the
482/// attribute list \p Attrs. This is only the case if it was not already present
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000483/// in \p Attrs at the position describe by \p PK and \p AttrIdx.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000484static bool addIfNotExistent(LLVMContext &Ctx, const Attribute &Attr,
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000485 AttributeList &Attrs, int AttrIdx) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000486
487 if (Attr.isEnumAttribute()) {
488 Attribute::AttrKind Kind = Attr.getKindAsEnum();
489 if (Attrs.hasAttribute(AttrIdx, Kind))
490 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
491 return false;
492 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
493 return true;
494 }
495 if (Attr.isStringAttribute()) {
496 StringRef Kind = Attr.getKindAsString();
497 if (Attrs.hasAttribute(AttrIdx, Kind))
498 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
499 return false;
500 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
501 return true;
502 }
Hideto Ueno19c07af2019-07-23 08:16:17 +0000503 if (Attr.isIntAttribute()) {
504 Attribute::AttrKind Kind = Attr.getKindAsEnum();
505 if (Attrs.hasAttribute(AttrIdx, Kind))
506 if (isEqualOrWorse(Attr, Attrs.getAttribute(AttrIdx, Kind)))
507 return false;
508 Attrs = Attrs.removeAttribute(Ctx, AttrIdx, Kind);
509 Attrs = Attrs.addAttribute(Ctx, AttrIdx, Attr);
510 return true;
511 }
Johannes Doerfertaade7822019-06-05 03:02:24 +0000512
513 llvm_unreachable("Expected enum or string attribute!");
514}
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000515
Hideto Ueno0f4383f2019-11-27 14:41:12 +0000516static const Value *
517getBasePointerOfAccessPointerOperand(const Instruction *I, int64_t &BytesOffset,
518 const DataLayout &DL,
519 bool AllowNonInbounds = false) {
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -0600520 const Value *Ptr = getPointerOperand(I, /* AllowVolatile */ false);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000521 if (!Ptr)
522 return nullptr;
523
524 return GetPointerBaseWithConstantOffset(Ptr, BytesOffset, DL,
Hideto Ueno0f4383f2019-11-27 14:41:12 +0000525 AllowNonInbounds);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000526}
Johannes Doerfertaade7822019-06-05 03:02:24 +0000527
Johannes Doerfertece81902019-08-12 22:05:53 +0000528ChangeStatus AbstractAttribute::update(Attributor &A) {
Johannes Doerfertaade7822019-06-05 03:02:24 +0000529 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
530 if (getState().isAtFixpoint())
531 return HasChanged;
532
533 LLVM_DEBUG(dbgs() << "[Attributor] Update: " << *this << "\n");
534
Johannes Doerfertece81902019-08-12 22:05:53 +0000535 HasChanged = updateImpl(A);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000536
537 LLVM_DEBUG(dbgs() << "[Attributor] Update " << HasChanged << " " << *this
538 << "\n");
539
540 return HasChanged;
541}
542
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000543ChangeStatus
Johannes Doerfertb2083c52019-10-20 22:46:48 -0500544IRAttributeManifest::manifestAttrs(Attributor &A, const IRPosition &IRP,
Johannes Doerfertd1b79e02019-08-07 22:46:11 +0000545 const ArrayRef<Attribute> &DeducedAttrs) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000546 Function *ScopeFn = IRP.getAssociatedFunction();
Kristina Brooks26e60f02019-08-06 19:53:19 +0000547 IRPosition::Kind PK = IRP.getPositionKind();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000548
Johannes Doerfertaade7822019-06-05 03:02:24 +0000549 // In the following some generic code that will manifest attributes in
550 // DeducedAttrs if they improve the current IR. Due to the different
551 // annotation positions we use the underlying AttributeList interface.
Johannes Doerfertaade7822019-06-05 03:02:24 +0000552
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000553 AttributeList Attrs;
554 switch (PK) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000555 case IRPosition::IRP_INVALID:
556 case IRPosition::IRP_FLOAT:
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000557 return ChangeStatus::UNCHANGED;
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000558 case IRPosition::IRP_ARGUMENT:
559 case IRPosition::IRP_FUNCTION:
560 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000561 Attrs = ScopeFn->getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000562 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000563 case IRPosition::IRP_CALL_SITE:
564 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000565 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000566 Attrs = ImmutableCallSite(&IRP.getAnchorValue()).getAttributes();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000567 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000568 }
569
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000570 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000571 LLVMContext &Ctx = IRP.getAnchorValue().getContext();
Johannes Doerfertaade7822019-06-05 03:02:24 +0000572 for (const Attribute &Attr : DeducedAttrs) {
Kristina Brooks26e60f02019-08-06 19:53:19 +0000573 if (!addIfNotExistent(Ctx, Attr, Attrs, IRP.getAttrIdx()))
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000574 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000575
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000576 HasChanged = ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000577 }
578
579 if (HasChanged == ChangeStatus::UNCHANGED)
580 return HasChanged;
581
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000582 switch (PK) {
583 case IRPosition::IRP_ARGUMENT:
584 case IRPosition::IRP_FUNCTION:
585 case IRPosition::IRP_RETURNED:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000586 ScopeFn->setAttributes(Attrs);
Johannes Doerfertaade7822019-06-05 03:02:24 +0000587 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000588 case IRPosition::IRP_CALL_SITE:
589 case IRPosition::IRP_CALL_SITE_RETURNED:
Johannes Doerfertfb69f762019-08-05 23:32:31 +0000590 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Kristina Brooks26e60f02019-08-06 19:53:19 +0000591 CallSite(&IRP.getAnchorValue()).setAttributes(Attrs);
Johannes Doerfert4395b312019-08-14 21:46:28 +0000592 break;
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000593 case IRPosition::IRP_INVALID:
Johannes Doerfert4395b312019-08-14 21:46:28 +0000594 case IRPosition::IRP_FLOAT:
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000595 break;
Johannes Doerfertaade7822019-06-05 03:02:24 +0000596 }
597
598 return HasChanged;
599}
600
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000601const IRPosition IRPosition::EmptyKey(255);
602const IRPosition IRPosition::TombstoneKey(256);
603
604SubsumingPositionIterator::SubsumingPositionIterator(const IRPosition &IRP) {
605 IRPositions.emplace_back(IRP);
606
607 ImmutableCallSite ICS(&IRP.getAnchorValue());
608 switch (IRP.getPositionKind()) {
609 case IRPosition::IRP_INVALID:
610 case IRPosition::IRP_FLOAT:
611 case IRPosition::IRP_FUNCTION:
612 return;
613 case IRPosition::IRP_ARGUMENT:
614 case IRPosition::IRP_RETURNED:
615 IRPositions.emplace_back(
616 IRPosition::function(*IRP.getAssociatedFunction()));
617 return;
618 case IRPosition::IRP_CALL_SITE:
619 assert(ICS && "Expected call site!");
620 // TODO: We need to look at the operand bundles similar to the redirection
621 // in CallBase.
622 if (!ICS.hasOperandBundles())
623 if (const Function *Callee = ICS.getCalledFunction())
624 IRPositions.emplace_back(IRPosition::function(*Callee));
625 return;
626 case IRPosition::IRP_CALL_SITE_RETURNED:
627 assert(ICS && "Expected call site!");
628 // TODO: We need to look at the operand bundles similar to the redirection
629 // in CallBase.
630 if (!ICS.hasOperandBundles()) {
631 if (const Function *Callee = ICS.getCalledFunction()) {
632 IRPositions.emplace_back(IRPosition::returned(*Callee));
633 IRPositions.emplace_back(IRPosition::function(*Callee));
634 }
635 }
636 IRPositions.emplace_back(
637 IRPosition::callsite_function(cast<CallBase>(*ICS.getInstruction())));
638 return;
639 case IRPosition::IRP_CALL_SITE_ARGUMENT: {
640 int ArgNo = IRP.getArgNo();
641 assert(ICS && ArgNo >= 0 && "Expected call site!");
642 // TODO: We need to look at the operand bundles similar to the redirection
643 // in CallBase.
644 if (!ICS.hasOperandBundles()) {
645 const Function *Callee = ICS.getCalledFunction();
646 if (Callee && Callee->arg_size() > unsigned(ArgNo))
647 IRPositions.emplace_back(IRPosition::argument(*Callee->getArg(ArgNo)));
648 if (Callee)
649 IRPositions.emplace_back(IRPosition::function(*Callee));
650 }
651 IRPositions.emplace_back(IRPosition::value(IRP.getAssociatedValue()));
652 return;
653 }
654 }
655}
656
Johannes Doerfert1097fab2019-10-07 21:07:57 +0000657bool IRPosition::hasAttr(ArrayRef<Attribute::AttrKind> AKs,
658 bool IgnoreSubsumingPositions) const {
659 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000660 for (Attribute::AttrKind AK : AKs)
661 if (EquivIRP.getAttr(AK).getKindAsEnum() == AK)
662 return true;
Johannes Doerfert1097fab2019-10-07 21:07:57 +0000663 // The first position returned by the SubsumingPositionIterator is
664 // always the position itself. If we ignore subsuming positions we
665 // are done after the first iteration.
666 if (IgnoreSubsumingPositions)
667 break;
668 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000669 return false;
670}
671
672void IRPosition::getAttrs(ArrayRef<Attribute::AttrKind> AKs,
Johannes Doerfert6abd01e2019-12-12 15:02:36 -0600673 SmallVectorImpl<Attribute> &Attrs,
674 bool IgnoreSubsumingPositions) const {
675 for (const IRPosition &EquivIRP : SubsumingPositionIterator(*this)) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000676 for (Attribute::AttrKind AK : AKs) {
677 const Attribute &Attr = EquivIRP.getAttr(AK);
678 if (Attr.getKindAsEnum() == AK)
679 Attrs.push_back(Attr);
680 }
Johannes Doerfert6abd01e2019-12-12 15:02:36 -0600681 // The first position returned by the SubsumingPositionIterator is
682 // always the position itself. If we ignore subsuming positions we
683 // are done after the first iteration.
684 if (IgnoreSubsumingPositions)
685 break;
686 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000687}
688
689void IRPosition::verify() {
690 switch (KindOrArgNo) {
691 default:
692 assert(KindOrArgNo >= 0 && "Expected argument or call site argument!");
693 assert((isa<CallBase>(AnchorVal) || isa<Argument>(AnchorVal)) &&
694 "Expected call base or argument for positive attribute index!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000695 if (isa<Argument>(AnchorVal)) {
696 assert(cast<Argument>(AnchorVal)->getArgNo() == unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000697 "Argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000698 assert(cast<Argument>(AnchorVal) == &getAssociatedValue() &&
699 "Associated value mismatch!");
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000700 } else {
Simon Pilgrim920b0402019-08-29 10:08:45 +0000701 assert(cast<CallBase>(*AnchorVal).arg_size() > unsigned(getArgNo()) &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000702 "Call site argument number mismatch!");
Simon Pilgrim920b0402019-08-29 10:08:45 +0000703 assert(cast<CallBase>(*AnchorVal).getArgOperand(getArgNo()) ==
704 &getAssociatedValue() &&
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000705 "Associated value mismatch!");
706 }
707 break;
708 case IRP_INVALID:
709 assert(!AnchorVal && "Expected no value for an invalid position!");
710 break;
711 case IRP_FLOAT:
712 assert((!isa<CallBase>(&getAssociatedValue()) &&
713 !isa<Argument>(&getAssociatedValue())) &&
714 "Expected specialized kind for call base and argument values!");
715 break;
716 case IRP_RETURNED:
717 assert(isa<Function>(AnchorVal) &&
718 "Expected function for a 'returned' position!");
719 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
720 break;
721 case IRP_CALL_SITE_RETURNED:
722 assert((isa<CallBase>(AnchorVal)) &&
723 "Expected call base for 'call site returned' position!");
724 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
725 break;
726 case IRP_CALL_SITE:
727 assert((isa<CallBase>(AnchorVal)) &&
728 "Expected call base for 'call site function' position!");
729 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
730 break;
731 case IRP_FUNCTION:
732 assert(isa<Function>(AnchorVal) &&
733 "Expected function for a 'function' position!");
734 assert(AnchorVal == &getAssociatedValue() && "Associated value mismatch!");
735 break;
736 }
737}
738
Benjamin Kramer564a9de2020-02-17 17:55:03 +0100739namespace {
740
Johannes Doerfert1a746452019-10-20 22:28:49 -0500741/// Helper function to clamp a state \p S of type \p StateType with the
Johannes Doerfert234eda52019-08-16 19:51:23 +0000742/// information in \p R and indicate/return if \p S did change (as-in update is
743/// required to be run again).
Johannes Doerfert234eda52019-08-16 19:51:23 +0000744template <typename StateType>
Johannes Doerfert1a746452019-10-20 22:28:49 -0500745ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
Johannes Doerfert234eda52019-08-16 19:51:23 +0000746 auto Assumed = S.getAssumed();
747 S ^= R;
748 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
749 : ChangeStatus::CHANGED;
750}
Johannes Doerfertb9b87912019-08-20 06:02:39 +0000751
Johannes Doerfert234eda52019-08-16 19:51:23 +0000752/// Clamp the information known for all returned values of a function
753/// (identified by \p QueryingAA) into \p S.
754template <typename AAType, typename StateType = typename AAType::StateType>
755static void clampReturnedValueStates(Attributor &A, const AAType &QueryingAA,
756 StateType &S) {
757 LLVM_DEBUG(dbgs() << "[Attributor] Clamp return value states for "
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600758 << QueryingAA << " into " << S << "\n");
Johannes Doerfert234eda52019-08-16 19:51:23 +0000759
760 assert((QueryingAA.getIRPosition().getPositionKind() ==
761 IRPosition::IRP_RETURNED ||
762 QueryingAA.getIRPosition().getPositionKind() ==
763 IRPosition::IRP_CALL_SITE_RETURNED) &&
764 "Can only clamp returned value states for a function returned or call "
765 "site returned position!");
766
767 // Use an optional state as there might not be any return values and we want
768 // to join (IntegerState::operator&) the state of all there are.
769 Optional<StateType> T;
770
771 // Callback for each possibly returned value.
772 auto CheckReturnValue = [&](Value &RV) -> bool {
773 const IRPosition &RVPos = IRPosition::value(RV);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000774 const AAType &AA = A.getAAFor<AAType>(QueryingAA, RVPos);
775 LLVM_DEBUG(dbgs() << "[Attributor] RV: " << RV << " AA: " << AA.getAsStr()
776 << " @ " << RVPos << "\n");
777 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000778 if (T.hasValue())
779 *T &= AAS;
780 else
781 T = AAS;
782 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " RV State: " << T
783 << "\n");
784 return T->isValidState();
785 };
786
787 if (!A.checkForAllReturnedValues(CheckReturnValue, QueryingAA))
788 S.indicatePessimisticFixpoint();
789 else if (T.hasValue())
790 S ^= *T;
791}
792
Hideto Ueno08daf8c2019-10-08 15:20:19 +0000793/// Helper class to compose two generic deduction
794template <typename AAType, typename Base, typename StateType,
795 template <typename...> class F, template <typename...> class G>
796struct AAComposeTwoGenericDeduction
797 : public F<AAType, G<AAType, Base, StateType>, StateType> {
798 AAComposeTwoGenericDeduction(const IRPosition &IRP)
799 : F<AAType, G<AAType, Base, StateType>, StateType>(IRP) {}
800
801 /// See AbstractAttribute::updateImpl(...).
802 ChangeStatus updateImpl(Attributor &A) override {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +0100803 ChangeStatus ChangedF =
804 F<AAType, G<AAType, Base, StateType>, StateType>::updateImpl(A);
Johannes Doerfertdb6efb02019-10-13 20:40:10 +0000805 ChangeStatus ChangedG = G<AAType, Base, StateType>::updateImpl(A);
806 return ChangedF | ChangedG;
Hideto Ueno08daf8c2019-10-08 15:20:19 +0000807 }
808};
809
Johannes Doerfert234eda52019-08-16 19:51:23 +0000810/// Helper class for generic deduction: return value -> returned position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000811template <typename AAType, typename Base,
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600812 typename StateType = typename Base::StateType>
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000813struct AAReturnedFromReturnedValues : public Base {
814 AAReturnedFromReturnedValues(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000815
816 /// See AbstractAttribute::updateImpl(...).
817 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600818 StateType S(StateType::getBestState(this->getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000819 clampReturnedValueStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000820 // TODO: If we know we visited all returned values, thus no are assumed
821 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000822 return clampStateAndIndicateChange<StateType>(this->getState(), S);
823 }
824};
825
826/// Clamp the information known at all call sites for a given argument
827/// (identified by \p QueryingAA) into \p S.
828template <typename AAType, typename StateType = typename AAType::StateType>
829static void clampCallSiteArgumentStates(Attributor &A, const AAType &QueryingAA,
830 StateType &S) {
831 LLVM_DEBUG(dbgs() << "[Attributor] Clamp call site argument states for "
Johannes Doerfert3d347e22019-12-13 23:35:45 -0600832 << QueryingAA << " into " << S << "\n");
Johannes Doerfert234eda52019-08-16 19:51:23 +0000833
834 assert(QueryingAA.getIRPosition().getPositionKind() ==
835 IRPosition::IRP_ARGUMENT &&
836 "Can only clamp call site argument states for an argument position!");
837
838 // Use an optional state as there might not be any return values and we want
839 // to join (IntegerState::operator&) the state of all there are.
840 Optional<StateType> T;
841
842 // The argument number which is also the call site argument number.
843 unsigned ArgNo = QueryingAA.getIRPosition().getArgNo();
844
Johannes Doerfert661db042019-10-07 23:14:58 +0000845 auto CallSiteCheck = [&](AbstractCallSite ACS) {
846 const IRPosition &ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
847 // Check if a coresponding argument was found or if it is on not associated
848 // (which can happen for callback calls).
849 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
850 return false;
851
852 const AAType &AA = A.getAAFor<AAType>(QueryingAA, ACSArgPos);
853 LLVM_DEBUG(dbgs() << "[Attributor] ACS: " << *ACS.getInstruction()
854 << " AA: " << AA.getAsStr() << " @" << ACSArgPos << "\n");
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000855 const StateType &AAS = static_cast<const StateType &>(AA.getState());
Johannes Doerfert234eda52019-08-16 19:51:23 +0000856 if (T.hasValue())
857 *T &= AAS;
858 else
859 T = AAS;
860 LLVM_DEBUG(dbgs() << "[Attributor] AA State: " << AAS << " CSA State: " << T
861 << "\n");
862 return T->isValidState();
863 };
864
Johannes Doerfert368f7ee2019-12-30 16:12:36 -0600865 bool AllCallSitesKnown;
866 if (!A.checkForAllCallSites(CallSiteCheck, QueryingAA, true,
867 AllCallSitesKnown))
Johannes Doerfert234eda52019-08-16 19:51:23 +0000868 S.indicatePessimisticFixpoint();
869 else if (T.hasValue())
870 S ^= *T;
871}
872
873/// Helper class for generic deduction: call site argument -> argument position.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000874template <typename AAType, typename Base,
875 typename StateType = typename AAType::StateType>
876struct AAArgumentFromCallSiteArguments : public Base {
877 AAArgumentFromCallSiteArguments(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000878
879 /// See AbstractAttribute::updateImpl(...).
880 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertea5fabe2020-01-28 09:46:15 -0600881 StateType S(StateType::getBestState(this->getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000882 clampCallSiteArgumentStates<AAType, StateType>(A, *this, S);
Johannes Doerfert028b2aa2019-08-20 05:57:01 +0000883 // TODO: If we know we visited all incoming values, thus no are assumed
884 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +0000885 return clampStateAndIndicateChange<StateType>(this->getState(), S);
886 }
887};
888
889/// Helper class for generic replication: function returned -> cs returned.
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000890template <typename AAType, typename Base,
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600891 typename StateType = typename Base::StateType>
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000892struct AACallSiteReturnedFromReturned : public Base {
893 AACallSiteReturnedFromReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert234eda52019-08-16 19:51:23 +0000894
895 /// See AbstractAttribute::updateImpl(...).
896 ChangeStatus updateImpl(Attributor &A) override {
897 assert(this->getIRPosition().getPositionKind() ==
898 IRPosition::IRP_CALL_SITE_RETURNED &&
899 "Can only wrap function returned positions for call site returned "
900 "positions!");
901 auto &S = this->getState();
902
903 const Function *AssociatedFunction =
904 this->getIRPosition().getAssociatedFunction();
905 if (!AssociatedFunction)
906 return S.indicatePessimisticFixpoint();
907
908 IRPosition FnPos = IRPosition::returned(*AssociatedFunction);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +0000909 const AAType &AA = A.getAAFor<AAType>(*this, FnPos);
Johannes Doerfert234eda52019-08-16 19:51:23 +0000910 return clampStateAndIndicateChange(
Johannes Doerfert791c9f12020-01-29 18:02:42 -0600911 S, static_cast<const StateType &>(AA.getState()));
Johannes Doerfert234eda52019-08-16 19:51:23 +0000912 }
913};
914
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000915/// Helper class for generic deduction using must-be-executed-context
916/// Base class is required to have `followUse` method.
917
918/// bool followUse(Attributor &A, const Use *U, const Instruction *I)
Simon Pilgrimf7aee612019-10-10 15:25:16 +0000919/// U - Underlying use.
920/// I - The user of the \p U.
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000921/// `followUse` returns true if the value should be tracked transitively.
922
923template <typename AAType, typename Base,
924 typename StateType = typename AAType::StateType>
925struct AAFromMustBeExecutedContext : public Base {
926 AAFromMustBeExecutedContext(const IRPosition &IRP) : Base(IRP) {}
927
928 void initialize(Attributor &A) override {
929 Base::initialize(A);
Johannes Doerfertb2083c52019-10-20 22:46:48 -0500930 const IRPosition &IRP = this->getIRPosition();
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000931 Instruction *CtxI = IRP.getCtxI();
932
933 if (!CtxI)
934 return;
935
936 for (const Use &U : IRP.getAssociatedValue().uses())
937 Uses.insert(&U);
938 }
939
940 /// See AbstractAttribute::updateImpl(...).
941 ChangeStatus updateImpl(Attributor &A) override {
942 auto BeforeState = this->getState();
943 auto &S = this->getState();
944 Instruction *CtxI = this->getIRPosition().getCtxI();
945 if (!CtxI)
946 return ChangeStatus::UNCHANGED;
947
948 MustBeExecutedContextExplorer &Explorer =
949 A.getInfoCache().getMustBeExecutedContextExplorer();
950
Johannes Doerfert0be9cf22019-10-14 17:29:05 -0500951 auto EIt = Explorer.begin(CtxI), EEnd = Explorer.end(CtxI);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +0100952 for (unsigned u = 0; u < Uses.size(); ++u) {
Johannes Doerferteb4f41d2019-10-30 17:21:53 -0500953 const Use *U = Uses[u];
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000954 if (const Instruction *UserI = dyn_cast<Instruction>(U->getUser())) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -0500955 bool Found = Explorer.findInContextOf(UserI, EIt, EEnd);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000956 if (Found && Base::followUse(A, U, UserI))
957 for (const Use &Us : UserI->uses())
Johannes Doerferteb4f41d2019-10-30 17:21:53 -0500958 Uses.insert(&Us);
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000959 }
960 }
Hideto Ueno96e6ce42019-10-08 15:25:56 +0000961
962 return BeforeState == S ? ChangeStatus::UNCHANGED : ChangeStatus::CHANGED;
963 }
964
965private:
966 /// Container for (transitive) uses of the associated value.
967 SetVector<const Use *> Uses;
968};
969
970template <typename AAType, typename Base,
971 typename StateType = typename AAType::StateType>
972using AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext =
973 AAComposeTwoGenericDeduction<AAType, Base, StateType,
974 AAFromMustBeExecutedContext,
975 AAArgumentFromCallSiteArguments>;
976
977template <typename AAType, typename Base,
978 typename StateType = typename AAType::StateType>
979using AACallSiteReturnedFromReturnedAndMustBeExecutedContext =
980 AAComposeTwoGenericDeduction<AAType, Base, StateType,
981 AAFromMustBeExecutedContext,
982 AACallSiteReturnedFromReturned>;
983
Stefan Stipanovic53605892019-06-27 11:27:54 +0000984/// -----------------------NoUnwind Function Attribute--------------------------
985
Johannes Doerfert344d0382019-08-07 22:34:26 +0000986struct AANoUnwindImpl : AANoUnwind {
Johannes Doerfert710ebb02019-08-14 21:18:01 +0000987 AANoUnwindImpl(const IRPosition &IRP) : AANoUnwind(IRP) {}
Stefan Stipanovic53605892019-06-27 11:27:54 +0000988
Stefan Stipanovic15e86f72019-07-12 17:42:14 +0000989 const std::string getAsStr() const override {
Stefan Stipanovic53605892019-06-27 11:27:54 +0000990 return getAssumed() ? "nounwind" : "may-unwind";
991 }
992
993 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +0000994 ChangeStatus updateImpl(Attributor &A) override {
995 auto Opcodes = {
996 (unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
997 (unsigned)Instruction::Call, (unsigned)Instruction::CleanupRet,
998 (unsigned)Instruction::CatchSwitch, (unsigned)Instruction::Resume};
999
1000 auto CheckForNoUnwind = [&](Instruction &I) {
1001 if (!I.mayThrow())
1002 return true;
1003
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001004 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1005 const auto &NoUnwindAA =
1006 A.getAAFor<AANoUnwind>(*this, IRPosition::callsite_function(ICS));
1007 return NoUnwindAA.isAssumedNoUnwind();
1008 }
1009 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001010 };
1011
1012 if (!A.checkForAllInstructions(CheckForNoUnwind, *this, Opcodes))
1013 return indicatePessimisticFixpoint();
1014
1015 return ChangeStatus::UNCHANGED;
1016 }
Stefan Stipanovic53605892019-06-27 11:27:54 +00001017};
1018
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001019struct AANoUnwindFunction final : public AANoUnwindImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001020 AANoUnwindFunction(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001021
1022 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001023 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nounwind) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001024};
1025
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001026/// NoUnwind attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001027struct AANoUnwindCallSite final : AANoUnwindImpl {
1028 AANoUnwindCallSite(const IRPosition &IRP) : AANoUnwindImpl(IRP) {}
1029
1030 /// See AbstractAttribute::initialize(...).
1031 void initialize(Attributor &A) override {
1032 AANoUnwindImpl::initialize(A);
1033 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001034 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001035 indicatePessimisticFixpoint();
1036 }
1037
1038 /// See AbstractAttribute::updateImpl(...).
1039 ChangeStatus updateImpl(Attributor &A) override {
1040 // TODO: Once we have call site specific value information we can provide
1041 // call site specific liveness information and then it makes
1042 // sense to specialize attributes for call sites arguments instead of
1043 // redirecting requests to the callee argument.
1044 Function *F = getAssociatedFunction();
1045 const IRPosition &FnPos = IRPosition::function(*F);
1046 auto &FnAA = A.getAAFor<AANoUnwind>(*this, FnPos);
1047 return clampStateAndIndicateChange(
1048 getState(),
1049 static_cast<const AANoUnwind::StateType &>(FnAA.getState()));
1050 }
1051
1052 /// See AbstractAttribute::trackStatistics()
1053 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nounwind); }
1054};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001055
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001056/// --------------------- Function Return Values -------------------------------
1057
1058/// "Attribute" that collects all potential returned values and the return
1059/// instructions that they arise from.
1060///
1061/// If there is a unique returned value R, the manifest method will:
1062/// - mark R with the "returned" attribute, if R is an argument.
Johannes Doerferteccdf082019-08-05 23:35:12 +00001063class AAReturnedValuesImpl : public AAReturnedValues, public AbstractState {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001064
1065 /// Mapping of values potentially returned by the associated function to the
1066 /// return instructions that might return them.
Johannes Doerferta4a308c2019-08-26 17:51:23 +00001067 MapVector<Value *, SmallSetVector<ReturnInst *, 4>> ReturnedValues;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001068
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001069 /// Mapping to remember the number of returned values for a call site such
1070 /// that we can avoid updates if nothing changed.
1071 DenseMap<const CallBase *, unsigned> NumReturnedValuesPerKnownAA;
1072
1073 /// Set of unresolved calls returned by the associated function.
Johannes Doerfert695089e2019-08-23 15:23:49 +00001074 SmallSetVector<CallBase *, 4> UnresolvedCalls;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001075
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001076 /// State flags
1077 ///
1078 ///{
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001079 bool IsFixed = false;
1080 bool IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001081 ///}
1082
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001083public:
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001084 AAReturnedValuesImpl(const IRPosition &IRP) : AAReturnedValues(IRP) {}
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001085
1086 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001087 void initialize(Attributor &A) override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001088 // Reset the state.
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001089 IsFixed = false;
1090 IsValidState = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001091 ReturnedValues.clear();
1092
Johannes Doerfertdef99282019-08-14 21:29:37 +00001093 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001094 if (!F) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001095 indicatePessimisticFixpoint();
1096 return;
1097 }
Johannes Doerferte273ac42020-01-12 00:13:03 -06001098 assert(!F->getReturnType()->isVoidTy() &&
1099 "Did not expect a void return type!");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001100
1101 // The map from instruction opcodes to those instructions in the function.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001102 auto &OpcodeInstMap = A.getInfoCache().getOpcodeInstMapForFunction(*F);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001103
1104 // Look through all arguments, if one is marked as returned we are done.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001105 for (Argument &Arg : F->args()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001106 if (Arg.hasReturnedAttr()) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001107 auto &ReturnInstSet = ReturnedValues[&Arg];
1108 for (Instruction *RI : OpcodeInstMap[Instruction::Ret])
1109 ReturnInstSet.insert(cast<ReturnInst>(RI));
1110
1111 indicateOptimisticFixpoint();
1112 return;
1113 }
1114 }
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001115
1116 if (!F->hasExactDefinition())
1117 indicatePessimisticFixpoint();
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001118 }
1119
1120 /// See AbstractAttribute::manifest(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001121 ChangeStatus manifest(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001122
1123 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001124 AbstractState &getState() override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001125
1126 /// See AbstractAttribute::getState(...).
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001127 const AbstractState &getState() const override { return *this; }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001128
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001129 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00001130 ChangeStatus updateImpl(Attributor &A) override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001131
Johannes Doerfertdef99282019-08-14 21:29:37 +00001132 llvm::iterator_range<iterator> returned_values() override {
1133 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1134 }
1135
1136 llvm::iterator_range<const_iterator> returned_values() const override {
1137 return llvm::make_range(ReturnedValues.begin(), ReturnedValues.end());
1138 }
1139
Johannes Doerfert695089e2019-08-23 15:23:49 +00001140 const SmallSetVector<CallBase *, 4> &getUnresolvedCalls() const override {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001141 return UnresolvedCalls;
1142 }
1143
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001144 /// Return the number of potential return values, -1 if unknown.
Johannes Doerfertdef99282019-08-14 21:29:37 +00001145 size_t getNumReturnValues() const override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001146 return isValidState() ? ReturnedValues.size() : -1;
1147 }
1148
1149 /// Return an assumed unique return value if a single candidate is found. If
1150 /// there cannot be one, return a nullptr. If it is not clear yet, return the
1151 /// Optional::NoneType.
Johannes Doerfert14a04932019-08-07 22:27:24 +00001152 Optional<Value *> getAssumedUniqueReturnValue(Attributor &A) const;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001153
Johannes Doerfert14a04932019-08-07 22:27:24 +00001154 /// See AbstractState::checkForAllReturnedValues(...).
1155 bool checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00001156 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00001157 &Pred) const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001158
1159 /// Pretty print the attribute similar to the IR representation.
Stefan Stipanovic15e86f72019-07-12 17:42:14 +00001160 const std::string getAsStr() const override;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001161
1162 /// See AbstractState::isAtFixpoint().
1163 bool isAtFixpoint() const override { return IsFixed; }
1164
1165 /// See AbstractState::isValidState().
1166 bool isValidState() const override { return IsValidState; }
1167
1168 /// See AbstractState::indicateOptimisticFixpoint(...).
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001169 ChangeStatus indicateOptimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001170 IsFixed = true;
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001171 return ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001172 }
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001173
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001174 ChangeStatus indicatePessimisticFixpoint() override {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001175 IsFixed = true;
1176 IsValidState = false;
Johannes Doerfertd1c37932019-08-04 18:37:38 +00001177 return ChangeStatus::CHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001178 }
1179};
1180
1181ChangeStatus AAReturnedValuesImpl::manifest(Attributor &A) {
1182 ChangeStatus Changed = ChangeStatus::UNCHANGED;
1183
1184 // Bookkeeping.
1185 assert(isValidState());
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001186 STATS_DECLTRACK(KnownReturnValues, FunctionReturn,
1187 "Number of function with known return values");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001188
1189 // Check if we have an assumed unique return value that we could manifest.
Johannes Doerfert14a04932019-08-07 22:27:24 +00001190 Optional<Value *> UniqueRV = getAssumedUniqueReturnValue(A);
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001191
1192 if (!UniqueRV.hasValue() || !UniqueRV.getValue())
1193 return Changed;
1194
1195 // Bookkeeping.
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001196 STATS_DECLTRACK(UniqueReturnValue, FunctionReturn,
1197 "Number of function with unique return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001198
Johannes Doerfert23400e612019-08-23 17:41:37 +00001199 // Callback to replace the uses of CB with the constant C.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06001200 auto ReplaceCallSiteUsersWith = [&A](CallBase &CB, Constant &C) {
Johannes Doerfert8fa56c42019-10-11 01:45:32 +00001201 if (CB.getNumUses() == 0 || CB.isMustTailCall())
Johannes Doerfert23400e612019-08-23 17:41:37 +00001202 return ChangeStatus::UNCHANGED;
Johannes Doerfert4c62a352020-01-12 00:17:08 -06001203 if (A.changeValueAfterManifest(CB, C))
1204 return ChangeStatus::CHANGED;
1205 return ChangeStatus::UNCHANGED;
Johannes Doerfert23400e612019-08-23 17:41:37 +00001206 };
1207
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001208 // If the assumed unique return value is an argument, annotate it.
1209 if (auto *UniqueRVArg = dyn_cast<Argument>(UniqueRV.getValue())) {
Johannes Doerfertb2083c52019-10-20 22:46:48 -05001210 // TODO: This should be handled differently!
1211 this->AnchorVal = UniqueRVArg;
1212 this->KindOrArgNo = UniqueRVArg->getArgNo();
Johannes Doerfert23400e612019-08-23 17:41:37 +00001213 Changed = IRAttribute::manifest(A);
1214 } else if (auto *RVC = dyn_cast<Constant>(UniqueRV.getValue())) {
1215 // We can replace the returned value with the unique returned constant.
1216 Value &AnchorValue = getAnchorValue();
1217 if (Function *F = dyn_cast<Function>(&AnchorValue)) {
1218 for (const Use &U : F->uses())
1219 if (CallBase *CB = dyn_cast<CallBase>(U.getUser()))
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001220 if (CB->isCallee(&U)) {
1221 Constant *RVCCast =
Johannes Doerfert07d16422019-11-01 23:03:35 -05001222 CB->getType() == RVC->getType()
1223 ? RVC
1224 : ConstantExpr::getTruncOrBitCast(RVC, CB->getType());
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001225 Changed = ReplaceCallSiteUsersWith(*CB, *RVCCast) | Changed;
1226 }
Johannes Doerfert23400e612019-08-23 17:41:37 +00001227 } else {
1228 assert(isa<CallBase>(AnchorValue) &&
1229 "Expcected a function or call base anchor!");
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001230 Constant *RVCCast =
Johannes Doerfert07d16422019-11-01 23:03:35 -05001231 AnchorValue.getType() == RVC->getType()
1232 ? RVC
1233 : ConstantExpr::getTruncOrBitCast(RVC, AnchorValue.getType());
Johannes Doerferte7c6f972019-09-14 02:57:50 +00001234 Changed = ReplaceCallSiteUsersWith(cast<CallBase>(AnchorValue), *RVCCast);
Johannes Doerfert23400e612019-08-23 17:41:37 +00001235 }
1236 if (Changed == ChangeStatus::CHANGED)
1237 STATS_DECLTRACK(UniqueConstantReturnValue, FunctionReturn,
1238 "Number of function returns replaced by constant return");
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001239 }
1240
1241 return Changed;
1242}
1243
1244const std::string AAReturnedValuesImpl::getAsStr() const {
1245 return (isAtFixpoint() ? "returns(#" : "may-return(#") +
Johannes Doerfert6471bb62019-08-04 18:39:28 +00001246 (isValidState() ? std::to_string(getNumReturnValues()) : "?") +
Johannes Doerfertdef99282019-08-14 21:29:37 +00001247 ")[#UC: " + std::to_string(UnresolvedCalls.size()) + "]";
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001248}
1249
Johannes Doerfert14a04932019-08-07 22:27:24 +00001250Optional<Value *>
1251AAReturnedValuesImpl::getAssumedUniqueReturnValue(Attributor &A) const {
1252 // If checkForAllReturnedValues provides a unique value, ignoring potential
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001253 // undef values that can also be present, it is assumed to be the actual
1254 // return value and forwarded to the caller of this method. If there are
1255 // multiple, a nullptr is returned indicating there cannot be a unique
1256 // returned value.
1257 Optional<Value *> UniqueRV;
1258
Johannes Doerfert14a04932019-08-07 22:27:24 +00001259 auto Pred = [&](Value &RV) -> bool {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001260 // If we found a second returned value and neither the current nor the saved
1261 // one is an undef, there is no unique returned value. Undefs are special
1262 // since we can pretend they have any value.
1263 if (UniqueRV.hasValue() && UniqueRV != &RV &&
1264 !(isa<UndefValue>(RV) || isa<UndefValue>(UniqueRV.getValue()))) {
1265 UniqueRV = nullptr;
1266 return false;
1267 }
1268
1269 // Do not overwrite a value with an undef.
1270 if (!UniqueRV.hasValue() || !isa<UndefValue>(RV))
1271 UniqueRV = &RV;
1272
1273 return true;
1274 };
1275
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001276 if (!A.checkForAllReturnedValues(Pred, *this))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001277 UniqueRV = nullptr;
1278
1279 return UniqueRV;
1280}
1281
Johannes Doerfert14a04932019-08-07 22:27:24 +00001282bool AAReturnedValuesImpl::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00001283 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00001284 &Pred) const {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001285 if (!isValidState())
1286 return false;
1287
1288 // Check all returned values but ignore call sites as long as we have not
1289 // encountered an overdefined one during an update.
1290 for (auto &It : ReturnedValues) {
1291 Value *RV = It.first;
1292
Johannes Doerfertdef99282019-08-14 21:29:37 +00001293 CallBase *CB = dyn_cast<CallBase>(RV);
1294 if (CB && !UnresolvedCalls.count(CB))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001295 continue;
1296
Johannes Doerfert695089e2019-08-23 15:23:49 +00001297 if (!Pred(*RV, It.second))
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001298 return false;
1299 }
1300
1301 return true;
1302}
1303
Johannes Doerfertece81902019-08-12 22:05:53 +00001304ChangeStatus AAReturnedValuesImpl::updateImpl(Attributor &A) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001305 size_t NumUnresolvedCalls = UnresolvedCalls.size();
1306 bool Changed = false;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001307
Johannes Doerfertdef99282019-08-14 21:29:37 +00001308 // State used in the value traversals starting in returned values.
1309 struct RVState {
1310 // The map in which we collect return values -> return instrs.
1311 decltype(ReturnedValues) &RetValsMap;
1312 // The flag to indicate a change.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001313 bool &Changed;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001314 // The return instrs we come from.
Johannes Doerfert695089e2019-08-23 15:23:49 +00001315 SmallSetVector<ReturnInst *, 4> RetInsts;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001316 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001317
Johannes Doerfertdef99282019-08-14 21:29:37 +00001318 // Callback for a leaf value returned by the associated function.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001319 auto VisitValueCB = [](Value &Val, RVState &RVS, bool) -> bool {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001320 auto Size = RVS.RetValsMap[&Val].size();
1321 RVS.RetValsMap[&Val].insert(RVS.RetInsts.begin(), RVS.RetInsts.end());
1322 bool Inserted = RVS.RetValsMap[&Val].size() != Size;
1323 RVS.Changed |= Inserted;
1324 LLVM_DEBUG({
1325 if (Inserted)
1326 dbgs() << "[AAReturnedValues] 1 Add new returned value " << Val
1327 << " => " << RVS.RetInsts.size() << "\n";
1328 });
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001329 return true;
Johannes Doerfertdef99282019-08-14 21:29:37 +00001330 };
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001331
Johannes Doerfertdef99282019-08-14 21:29:37 +00001332 // Helper method to invoke the generic value traversal.
1333 auto VisitReturnedValue = [&](Value &RV, RVState &RVS) {
1334 IRPosition RetValPos = IRPosition::value(RV);
1335 return genericValueTraversal<AAReturnedValues, RVState>(A, RetValPos, *this,
1336 RVS, VisitValueCB);
1337 };
Johannes Doerfertda4d8112019-08-01 16:21:54 +00001338
Johannes Doerfertdef99282019-08-14 21:29:37 +00001339 // Callback for all "return intructions" live in the associated function.
1340 auto CheckReturnInst = [this, &VisitReturnedValue, &Changed](Instruction &I) {
1341 ReturnInst &Ret = cast<ReturnInst>(I);
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001342 RVState RVS({ReturnedValues, Changed, {}});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001343 RVS.RetInsts.insert(&Ret);
Johannes Doerfertdef99282019-08-14 21:29:37 +00001344 return VisitReturnedValue(*Ret.getReturnValue(), RVS);
1345 };
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001346
Johannes Doerfertdef99282019-08-14 21:29:37 +00001347 // Start by discovering returned values from all live returned instructions in
1348 // the associated function.
1349 if (!A.checkForAllInstructions(CheckReturnInst, *this, {Instruction::Ret}))
1350 return indicatePessimisticFixpoint();
1351
1352 // Once returned values "directly" present in the code are handled we try to
1353 // resolve returned calls.
1354 decltype(ReturnedValues) NewRVsMap;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001355 for (auto &It : ReturnedValues) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001356 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Returned value: " << *It.first
1357 << " by #" << It.second.size() << " RIs\n");
1358 CallBase *CB = dyn_cast<CallBase>(It.first);
1359 if (!CB || UnresolvedCalls.count(CB))
1360 continue;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001361
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001362 if (!CB->getCalledFunction()) {
1363 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1364 << "\n");
1365 UnresolvedCalls.insert(CB);
1366 continue;
1367 }
1368
1369 // TODO: use the function scope once we have call site AAReturnedValues.
1370 const auto &RetValAA = A.getAAFor<AAReturnedValues>(
1371 *this, IRPosition::function(*CB->getCalledFunction()));
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001372 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Found another AAReturnedValues: "
Johannes Doerfert3d347e22019-12-13 23:35:45 -06001373 << RetValAA << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001374
1375 // Skip dead ends, thus if we do not know anything about the returned
1376 // call we mark it as unresolved and it will stay that way.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001377 if (!RetValAA.getState().isValidState()) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00001378 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Unresolved call: " << *CB
1379 << "\n");
1380 UnresolvedCalls.insert(CB);
1381 continue;
1382 }
1383
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001384 // Do not try to learn partial information. If the callee has unresolved
1385 // return values we will treat the call as unresolved/opaque.
1386 auto &RetValAAUnresolvedCalls = RetValAA.getUnresolvedCalls();
1387 if (!RetValAAUnresolvedCalls.empty()) {
1388 UnresolvedCalls.insert(CB);
1389 continue;
1390 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001391
Johannes Doerfertde7674c2019-08-19 21:35:31 +00001392 // Now check if we can track transitively returned values. If possible, thus
1393 // if all return value can be represented in the current scope, do so.
1394 bool Unresolved = false;
1395 for (auto &RetValAAIt : RetValAA.returned_values()) {
1396 Value *RetVal = RetValAAIt.first;
1397 if (isa<Argument>(RetVal) || isa<CallBase>(RetVal) ||
1398 isa<Constant>(RetVal))
1399 continue;
1400 // Anything that did not fit in the above categories cannot be resolved,
1401 // mark the call as unresolved.
1402 LLVM_DEBUG(dbgs() << "[AAReturnedValues] transitively returned value "
1403 "cannot be translated: "
1404 << *RetVal << "\n");
1405 UnresolvedCalls.insert(CB);
1406 Unresolved = true;
1407 break;
1408 }
1409
1410 if (Unresolved)
1411 continue;
1412
Johannes Doerfertdeb9ea32019-08-23 15:42:19 +00001413 // Now track transitively returned values.
1414 unsigned &NumRetAA = NumReturnedValuesPerKnownAA[CB];
1415 if (NumRetAA == RetValAA.getNumReturnValues()) {
1416 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Skip call as it has not "
1417 "changed since it was seen last\n");
1418 continue;
1419 }
1420 NumRetAA = RetValAA.getNumReturnValues();
1421
Johannes Doerfertdef99282019-08-14 21:29:37 +00001422 for (auto &RetValAAIt : RetValAA.returned_values()) {
1423 Value *RetVal = RetValAAIt.first;
1424 if (Argument *Arg = dyn_cast<Argument>(RetVal)) {
1425 // Arguments are mapped to call site operands and we begin the traversal
1426 // again.
Johannes Doerfert056f1b52019-08-19 19:14:10 +00001427 bool Unused = false;
1428 RVState RVS({NewRVsMap, Unused, RetValAAIt.second});
Johannes Doerfertdef99282019-08-14 21:29:37 +00001429 VisitReturnedValue(*CB->getArgOperand(Arg->getArgNo()), RVS);
1430 continue;
1431 } else if (isa<CallBase>(RetVal)) {
1432 // Call sites are resolved by the callee attribute over time, no need to
1433 // do anything for us.
1434 continue;
1435 } else if (isa<Constant>(RetVal)) {
1436 // Constants are valid everywhere, we can simply take them.
1437 NewRVsMap[RetVal].insert(It.second.begin(), It.second.end());
1438 continue;
1439 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00001440 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001441 }
1442
Johannes Doerfertdef99282019-08-14 21:29:37 +00001443 // To avoid modifications to the ReturnedValues map while we iterate over it
1444 // we kept record of potential new entries in a copy map, NewRVsMap.
1445 for (auto &It : NewRVsMap) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001446 assert(!It.second.empty() && "Entry does not add anything.");
1447 auto &ReturnInsts = ReturnedValues[It.first];
1448 for (ReturnInst *RI : It.second)
Johannes Doerfert695089e2019-08-23 15:23:49 +00001449 if (ReturnInsts.insert(RI)) {
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001450 LLVM_DEBUG(dbgs() << "[AAReturnedValues] Add new returned value "
1451 << *It.first << " => " << *RI << "\n");
Johannes Doerfertdef99282019-08-14 21:29:37 +00001452 Changed = true;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001453 }
1454 }
1455
Johannes Doerfertdef99282019-08-14 21:29:37 +00001456 Changed |= (NumUnresolvedCalls != UnresolvedCalls.size());
1457 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00001458}
1459
Johannes Doerfertdef99282019-08-14 21:29:37 +00001460struct AAReturnedValuesFunction final : public AAReturnedValuesImpl {
1461 AAReturnedValuesFunction(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1462
1463 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001464 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(returned) }
Johannes Doerfertdef99282019-08-14 21:29:37 +00001465};
1466
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001467/// Returned values information for a call sites.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001468struct AAReturnedValuesCallSite final : AAReturnedValuesImpl {
1469 AAReturnedValuesCallSite(const IRPosition &IRP) : AAReturnedValuesImpl(IRP) {}
1470
1471 /// See AbstractAttribute::initialize(...).
1472 void initialize(Attributor &A) override {
1473 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001474 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00001475 // sense to specialize attributes for call sites instead of
1476 // redirecting requests to the callee.
1477 llvm_unreachable("Abstract attributes for returned values are not "
1478 "supported for call sites yet!");
1479 }
1480
1481 /// See AbstractAttribute::updateImpl(...).
1482 ChangeStatus updateImpl(Attributor &A) override {
1483 return indicatePessimisticFixpoint();
1484 }
1485
1486 /// See AbstractAttribute::trackStatistics()
1487 void trackStatistics() const override {}
1488};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001489
Stefan Stipanovic06263672019-07-11 21:37:40 +00001490/// ------------------------ NoSync Function Attribute -------------------------
1491
Johannes Doerfert344d0382019-08-07 22:34:26 +00001492struct AANoSyncImpl : AANoSync {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001493 AANoSyncImpl(const IRPosition &IRP) : AANoSync(IRP) {}
Stefan Stipanovic06263672019-07-11 21:37:40 +00001494
Stefan Stipanoviccb5ecae2019-07-12 18:34:06 +00001495 const std::string getAsStr() const override {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001496 return getAssumed() ? "nosync" : "may-sync";
1497 }
1498
1499 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00001500 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001501
Stefan Stipanovic06263672019-07-11 21:37:40 +00001502 /// Helper function used to determine whether an instruction is non-relaxed
1503 /// atomic. In other words, if an atomic instruction does not have unordered
1504 /// or monotonic ordering
1505 static bool isNonRelaxedAtomic(Instruction *I);
1506
1507 /// Helper function used to determine whether an instruction is volatile.
1508 static bool isVolatile(Instruction *I);
1509
Johannes Doerfertc7a1db32019-07-13 01:09:27 +00001510 /// Helper function uset to check if intrinsic is volatile (memcpy, memmove,
1511 /// memset).
Stefan Stipanovic06263672019-07-11 21:37:40 +00001512 static bool isNoSyncIntrinsic(Instruction *I);
1513};
1514
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001515bool AANoSyncImpl::isNonRelaxedAtomic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001516 if (!I->isAtomic())
1517 return false;
1518
1519 AtomicOrdering Ordering;
1520 switch (I->getOpcode()) {
1521 case Instruction::AtomicRMW:
1522 Ordering = cast<AtomicRMWInst>(I)->getOrdering();
1523 break;
1524 case Instruction::Store:
1525 Ordering = cast<StoreInst>(I)->getOrdering();
1526 break;
1527 case Instruction::Load:
1528 Ordering = cast<LoadInst>(I)->getOrdering();
1529 break;
1530 case Instruction::Fence: {
1531 auto *FI = cast<FenceInst>(I);
1532 if (FI->getSyncScopeID() == SyncScope::SingleThread)
1533 return false;
1534 Ordering = FI->getOrdering();
1535 break;
1536 }
1537 case Instruction::AtomicCmpXchg: {
1538 AtomicOrdering Success = cast<AtomicCmpXchgInst>(I)->getSuccessOrdering();
1539 AtomicOrdering Failure = cast<AtomicCmpXchgInst>(I)->getFailureOrdering();
1540 // Only if both are relaxed, than it can be treated as relaxed.
1541 // Otherwise it is non-relaxed.
1542 if (Success != AtomicOrdering::Unordered &&
1543 Success != AtomicOrdering::Monotonic)
1544 return true;
1545 if (Failure != AtomicOrdering::Unordered &&
1546 Failure != AtomicOrdering::Monotonic)
1547 return true;
1548 return false;
1549 }
1550 default:
1551 llvm_unreachable(
1552 "New atomic operations need to be known in the attributor.");
1553 }
1554
1555 // Relaxed.
1556 if (Ordering == AtomicOrdering::Unordered ||
1557 Ordering == AtomicOrdering::Monotonic)
1558 return false;
1559 return true;
1560}
1561
1562/// Checks if an intrinsic is nosync. Currently only checks mem* intrinsics.
1563/// FIXME: We should ipmrove the handling of intrinsics.
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001564bool AANoSyncImpl::isNoSyncIntrinsic(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001565 if (auto *II = dyn_cast<IntrinsicInst>(I)) {
1566 switch (II->getIntrinsicID()) {
1567 /// Element wise atomic memory intrinsics are can only be unordered,
1568 /// therefore nosync.
1569 case Intrinsic::memset_element_unordered_atomic:
1570 case Intrinsic::memmove_element_unordered_atomic:
1571 case Intrinsic::memcpy_element_unordered_atomic:
1572 return true;
1573 case Intrinsic::memset:
1574 case Intrinsic::memmove:
1575 case Intrinsic::memcpy:
1576 if (!cast<MemIntrinsic>(II)->isVolatile())
1577 return true;
1578 return false;
1579 default:
1580 return false;
1581 }
1582 }
1583 return false;
1584}
1585
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001586bool AANoSyncImpl::isVolatile(Instruction *I) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001587 assert(!ImmutableCallSite(I) && !isa<CallBase>(I) &&
1588 "Calls should not be checked here");
1589
1590 switch (I->getOpcode()) {
1591 case Instruction::AtomicRMW:
1592 return cast<AtomicRMWInst>(I)->isVolatile();
1593 case Instruction::Store:
1594 return cast<StoreInst>(I)->isVolatile();
1595 case Instruction::Load:
1596 return cast<LoadInst>(I)->isVolatile();
1597 case Instruction::AtomicCmpXchg:
1598 return cast<AtomicCmpXchgInst>(I)->isVolatile();
1599 default:
1600 return false;
1601 }
1602}
1603
Johannes Doerfertece81902019-08-12 22:05:53 +00001604ChangeStatus AANoSyncImpl::updateImpl(Attributor &A) {
Stefan Stipanovic06263672019-07-11 21:37:40 +00001605
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001606 auto CheckRWInstForNoSync = [&](Instruction &I) {
1607 /// We are looking for volatile instructions or Non-Relaxed atomics.
Pankaj Godeb9a26a82019-11-22 14:46:43 +05301608 /// FIXME: We should improve the handling of intrinsics.
Stefan Stipanovicd0216172019-08-02 21:31:22 +00001609
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001610 if (isa<IntrinsicInst>(&I) && isNoSyncIntrinsic(&I))
1611 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001612
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001613 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
1614 if (ICS.hasFnAttr(Attribute::NoSync))
1615 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001616
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001617 const auto &NoSyncAA =
1618 A.getAAFor<AANoSync>(*this, IRPosition::callsite_function(ICS));
1619 if (NoSyncAA.isAssumedNoSync())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001620 return true;
1621 return false;
1622 }
Stefan Stipanovic06263672019-07-11 21:37:40 +00001623
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001624 if (!isVolatile(&I) && !isNonRelaxedAtomic(&I))
1625 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001626
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001627 return false;
1628 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001629
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001630 auto CheckForNoSync = [&](Instruction &I) {
1631 // At this point we handled all read/write effects and they are all
1632 // nosync, so they can be skipped.
1633 if (I.mayReadOrWriteMemory())
1634 return true;
Stefan Stipanovic06263672019-07-11 21:37:40 +00001635
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001636 // non-convergent and readnone imply nosync.
1637 return !ImmutableCallSite(&I).isConvergent();
1638 };
Stefan Stipanovic06263672019-07-11 21:37:40 +00001639
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001640 if (!A.checkForAllReadWriteInstructions(CheckRWInstForNoSync, *this) ||
1641 !A.checkForAllCallLikeInstructions(CheckForNoSync, *this))
Johannes Doerfertd0f64002019-08-06 00:32:43 +00001642 return indicatePessimisticFixpoint();
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00001643
Stefan Stipanovic06263672019-07-11 21:37:40 +00001644 return ChangeStatus::UNCHANGED;
1645}
1646
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001647struct AANoSyncFunction final : public AANoSyncImpl {
1648 AANoSyncFunction(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1649
1650 /// See AbstractAttribute::trackStatistics()
1651 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nosync) }
1652};
1653
1654/// NoSync attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001655struct AANoSyncCallSite final : AANoSyncImpl {
1656 AANoSyncCallSite(const IRPosition &IRP) : AANoSyncImpl(IRP) {}
1657
1658 /// See AbstractAttribute::initialize(...).
1659 void initialize(Attributor &A) override {
1660 AANoSyncImpl::initialize(A);
1661 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001662 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001663 indicatePessimisticFixpoint();
1664 }
1665
1666 /// See AbstractAttribute::updateImpl(...).
1667 ChangeStatus updateImpl(Attributor &A) override {
1668 // TODO: Once we have call site specific value information we can provide
1669 // call site specific liveness information and then it makes
1670 // sense to specialize attributes for call sites arguments instead of
1671 // redirecting requests to the callee argument.
1672 Function *F = getAssociatedFunction();
1673 const IRPosition &FnPos = IRPosition::function(*F);
1674 auto &FnAA = A.getAAFor<AANoSync>(*this, FnPos);
1675 return clampStateAndIndicateChange(
1676 getState(), static_cast<const AANoSync::StateType &>(FnAA.getState()));
1677 }
1678
1679 /// See AbstractAttribute::trackStatistics()
1680 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nosync); }
1681};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001682
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001683/// ------------------------ No-Free Attributes ----------------------------
1684
Johannes Doerfert344d0382019-08-07 22:34:26 +00001685struct AANoFreeImpl : public AANoFree {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001686 AANoFreeImpl(const IRPosition &IRP) : AANoFree(IRP) {}
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001687
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001688 /// See AbstractAttribute::updateImpl(...).
1689 ChangeStatus updateImpl(Attributor &A) override {
1690 auto CheckForNoFree = [&](Instruction &I) {
1691 ImmutableCallSite ICS(&I);
1692 if (ICS.hasFnAttr(Attribute::NoFree))
1693 return true;
1694
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001695 const auto &NoFreeAA =
1696 A.getAAFor<AANoFree>(*this, IRPosition::callsite_function(ICS));
1697 return NoFreeAA.isAssumedNoFree();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001698 };
1699
1700 if (!A.checkForAllCallLikeInstructions(CheckForNoFree, *this))
1701 return indicatePessimisticFixpoint();
1702 return ChangeStatus::UNCHANGED;
1703 }
1704
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001705 /// See AbstractAttribute::getAsStr().
1706 const std::string getAsStr() const override {
1707 return getAssumed() ? "nofree" : "may-free";
1708 }
Hideto Ueno65bbaf92019-07-12 17:38:51 +00001709};
1710
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001711struct AANoFreeFunction final : public AANoFreeImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001712 AANoFreeFunction(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00001713
1714 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00001715 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(nofree) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00001716};
1717
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001718/// NoFree attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001719struct AANoFreeCallSite final : AANoFreeImpl {
1720 AANoFreeCallSite(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1721
1722 /// See AbstractAttribute::initialize(...).
1723 void initialize(Attributor &A) override {
1724 AANoFreeImpl::initialize(A);
1725 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001726 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00001727 indicatePessimisticFixpoint();
1728 }
1729
1730 /// See AbstractAttribute::updateImpl(...).
1731 ChangeStatus updateImpl(Attributor &A) override {
1732 // TODO: Once we have call site specific value information we can provide
1733 // call site specific liveness information and then it makes
1734 // sense to specialize attributes for call sites arguments instead of
1735 // redirecting requests to the callee argument.
1736 Function *F = getAssociatedFunction();
1737 const IRPosition &FnPos = IRPosition::function(*F);
1738 auto &FnAA = A.getAAFor<AANoFree>(*this, FnPos);
1739 return clampStateAndIndicateChange(
1740 getState(), static_cast<const AANoFree::StateType &>(FnAA.getState()));
1741 }
1742
1743 /// See AbstractAttribute::trackStatistics()
1744 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(nofree); }
1745};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00001746
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001747/// NoFree attribute for floating values.
1748struct AANoFreeFloating : AANoFreeImpl {
1749 AANoFreeFloating(const IRPosition &IRP) : AANoFreeImpl(IRP) {}
1750
1751 /// See AbstractAttribute::trackStatistics()
1752 void trackStatistics() const override{STATS_DECLTRACK_FLOATING_ATTR(nofree)}
1753
1754 /// See Abstract Attribute::updateImpl(...).
1755 ChangeStatus updateImpl(Attributor &A) override {
1756 const IRPosition &IRP = getIRPosition();
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001757
1758 const auto &NoFreeAA =
1759 A.getAAFor<AANoFree>(*this, IRPosition::function_scope(IRP));
1760 if (NoFreeAA.isAssumedNoFree())
1761 return ChangeStatus::UNCHANGED;
1762
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001763 Value &AssociatedValue = getIRPosition().getAssociatedValue();
Hideto Ueno63599bd2019-12-12 12:26:09 +00001764 auto Pred = [&](const Use &U, bool &Follow) -> bool {
1765 Instruction *UserI = cast<Instruction>(U.getUser());
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001766 if (auto *CB = dyn_cast<CallBase>(UserI)) {
Hideto Ueno63599bd2019-12-12 12:26:09 +00001767 if (CB->isBundleOperand(&U))
1768 return false;
1769 if (!CB->isArgOperand(&U))
1770 return true;
1771 unsigned ArgNo = CB->getArgOperandNo(&U);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001772
1773 const auto &NoFreeArg = A.getAAFor<AANoFree>(
1774 *this, IRPosition::callsite_argument(*CB, ArgNo));
Hideto Ueno63599bd2019-12-12 12:26:09 +00001775 return NoFreeArg.isAssumedNoFree();
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001776 }
1777
1778 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
1779 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
Hideto Ueno63599bd2019-12-12 12:26:09 +00001780 Follow = true;
1781 return true;
Hideto Ueno4ecf2552019-12-12 13:42:40 +00001782 }
Johannes Doerfertf9555392020-01-10 14:49:45 -06001783 if (isa<ReturnInst>(UserI))
1784 return true;
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001785
1786 // Unknown user.
Hideto Ueno63599bd2019-12-12 12:26:09 +00001787 return false;
1788 };
1789 if (!A.checkForAllUses(Pred, *this, AssociatedValue))
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001790 return indicatePessimisticFixpoint();
Hideto Ueno63599bd2019-12-12 12:26:09 +00001791
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001792 return ChangeStatus::UNCHANGED;
1793 }
1794};
1795
1796/// NoFree attribute for a call site argument.
1797struct AANoFreeArgument final : AANoFreeFloating {
1798 AANoFreeArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1799
1800 /// See AbstractAttribute::trackStatistics()
1801 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nofree) }
1802};
1803
1804/// NoFree attribute for call site arguments.
1805struct AANoFreeCallSiteArgument final : AANoFreeFloating {
1806 AANoFreeCallSiteArgument(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1807
1808 /// See AbstractAttribute::updateImpl(...).
1809 ChangeStatus updateImpl(Attributor &A) override {
1810 // TODO: Once we have call site specific value information we can provide
1811 // call site specific liveness information and then it makes
1812 // sense to specialize attributes for call sites arguments instead of
1813 // redirecting requests to the callee argument.
1814 Argument *Arg = getAssociatedArgument();
1815 if (!Arg)
1816 return indicatePessimisticFixpoint();
1817 const IRPosition &ArgPos = IRPosition::argument(*Arg);
1818 auto &ArgAA = A.getAAFor<AANoFree>(*this, ArgPos);
1819 return clampStateAndIndicateChange(
1820 getState(), static_cast<const AANoFree::StateType &>(ArgAA.getState()));
1821 }
1822
1823 /// See AbstractAttribute::trackStatistics()
1824 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nofree)};
1825};
1826
1827/// NoFree attribute for function return value.
1828struct AANoFreeReturned final : AANoFreeFloating {
1829 AANoFreeReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {
1830 llvm_unreachable("NoFree is not applicable to function returns!");
1831 }
1832
1833 /// See AbstractAttribute::initialize(...).
1834 void initialize(Attributor &A) override {
1835 llvm_unreachable("NoFree is not applicable to function returns!");
1836 }
1837
1838 /// See AbstractAttribute::updateImpl(...).
1839 ChangeStatus updateImpl(Attributor &A) override {
1840 llvm_unreachable("NoFree is not applicable to function returns!");
1841 }
1842
1843 /// See AbstractAttribute::trackStatistics()
1844 void trackStatistics() const override {}
1845};
1846
1847/// NoFree attribute deduction for a call site return value.
1848struct AANoFreeCallSiteReturned final : AANoFreeFloating {
1849 AANoFreeCallSiteReturned(const IRPosition &IRP) : AANoFreeFloating(IRP) {}
1850
1851 ChangeStatus manifest(Attributor &A) override {
1852 return ChangeStatus::UNCHANGED;
1853 }
1854 /// See AbstractAttribute::trackStatistics()
1855 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nofree) }
1856};
1857
Hideto Ueno54869ec2019-07-15 06:49:04 +00001858/// ------------------------ NonNull Argument Attribute ------------------------
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001859static int64_t getKnownNonNullAndDerefBytesForUse(
1860 Attributor &A, AbstractAttribute &QueryingAA, Value &AssociatedValue,
1861 const Use *U, const Instruction *I, bool &IsNonNull, bool &TrackUse) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001862 TrackUse = false;
1863
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001864 const Value *UseV = U->get();
1865 if (!UseV->getType()->isPointerTy())
1866 return 0;
1867
1868 Type *PtrTy = UseV->getType();
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001869 const Function *F = I->getFunction();
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001870 bool NullPointerIsDefined =
1871 F ? llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()) : true;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001872 const DataLayout &DL = A.getInfoCache().getDL();
1873 if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
1874 if (ICS.isBundleOperand(U))
1875 return 0;
1876
1877 if (ICS.isCallee(U)) {
1878 IsNonNull |= !NullPointerIsDefined;
1879 return 0;
1880 }
1881
1882 unsigned ArgNo = ICS.getArgumentNo(U);
1883 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
Johannes Doerfert77a6b352019-11-02 14:47:45 -05001884 // As long as we only use known information there is no need to track
1885 // dependences here.
1886 auto &DerefAA = A.getAAFor<AADereferenceable>(QueryingAA, IRP,
1887 /* TrackDependence */ false);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001888 IsNonNull |= DerefAA.isKnownNonNull();
1889 return DerefAA.getKnownDereferenceableBytes();
1890 }
1891
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001892 // We need to follow common pointer manipulation uses to the accesses they
1893 // feed into. We can try to be smart to avoid looking through things we do not
1894 // like for now, e.g., non-inbounds GEPs.
1895 if (isa<CastInst>(I)) {
1896 TrackUse = true;
1897 return 0;
1898 }
1899 if (auto *GEP = dyn_cast<GetElementPtrInst>(I))
Hideto Ueno0f4383f2019-11-27 14:41:12 +00001900 if (GEP->hasAllConstantIndices()) {
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001901 TrackUse = true;
1902 return 0;
1903 }
1904
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001905 int64_t Offset;
1906 if (const Value *Base = getBasePointerOfAccessPointerOperand(I, Offset, DL)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09001907 if (Base == &AssociatedValue &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06001908 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001909 int64_t DerefBytes =
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001910 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType()) + Offset;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001911
1912 IsNonNull |= !NullPointerIsDefined;
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001913 return std::max(int64_t(0), DerefBytes);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001914 }
1915 }
Hideto Ueno0f4383f2019-11-27 14:41:12 +00001916
1917 /// Corner case when an offset is 0.
1918 if (const Value *Base = getBasePointerOfAccessPointerOperand(
1919 I, Offset, DL, /*AllowNonInbounds*/ true)) {
1920 if (Offset == 0 && Base == &AssociatedValue &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06001921 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno0f4383f2019-11-27 14:41:12 +00001922 int64_t DerefBytes =
1923 (int64_t)DL.getTypeStoreSize(PtrTy->getPointerElementType());
1924 IsNonNull |= !NullPointerIsDefined;
1925 return std::max(int64_t(0), DerefBytes);
1926 }
1927 }
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001928
1929 return 0;
1930}
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00001931
Johannes Doerfert344d0382019-08-07 22:34:26 +00001932struct AANonNullImpl : AANonNull {
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001933 AANonNullImpl(const IRPosition &IRP)
1934 : AANonNull(IRP),
1935 NullIsDefined(NullPointerIsDefined(
1936 getAnchorScope(),
1937 getAssociatedValue().getType()->getPointerAddressSpace())) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001938
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001939 /// See AbstractAttribute::initialize(...).
1940 void initialize(Attributor &A) override {
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001941 if (!NullIsDefined &&
1942 hasAttr({Attribute::NonNull, Attribute::Dereferenceable}))
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001943 indicateOptimisticFixpoint();
Johannes Doerferteb4f41d2019-10-30 17:21:53 -05001944 else if (isa<ConstantPointerNull>(getAssociatedValue()))
1945 indicatePessimisticFixpoint();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00001946 else
1947 AANonNull::initialize(A);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00001948 }
1949
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001950 /// See AAFromMustBeExecutedContext
1951 bool followUse(Attributor &A, const Use *U, const Instruction *I) {
1952 bool IsNonNull = false;
1953 bool TrackUse = false;
1954 getKnownNonNullAndDerefBytesForUse(A, *this, getAssociatedValue(), U, I,
1955 IsNonNull, TrackUse);
Johannes Doerfert1a746452019-10-20 22:28:49 -05001956 setKnown(IsNonNull);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001957 return TrackUse;
1958 }
1959
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001960 /// See AbstractAttribute::getAsStr().
1961 const std::string getAsStr() const override {
1962 return getAssumed() ? "nonnull" : "may-null";
1963 }
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001964
1965 /// Flag to determine if the underlying value can be null and still allow
1966 /// valid accesses.
1967 const bool NullIsDefined;
Hideto Ueno54869ec2019-07-15 06:49:04 +00001968};
1969
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001970/// NonNull attribute for a floating value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001971struct AANonNullFloating
1972 : AAFromMustBeExecutedContext<AANonNull, AANonNullImpl> {
1973 using Base = AAFromMustBeExecutedContext<AANonNull, AANonNullImpl>;
1974 AANonNullFloating(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno54869ec2019-07-15 06:49:04 +00001975
Hideto Ueno54869ec2019-07-15 06:49:04 +00001976 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001977 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00001978 ChangeStatus Change = Base::updateImpl(A);
1979 if (isKnownNonNull())
1980 return Change;
1981
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001982 if (!NullIsDefined) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01001983 const auto &DerefAA =
1984 A.getAAFor<AADereferenceable>(*this, getIRPosition());
Johannes Doerfertd82385b2019-10-13 20:48:26 +00001985 if (DerefAA.getAssumedDereferenceableBytes())
1986 return Change;
1987 }
1988
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001989 const DataLayout &DL = A.getDataLayout();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00001990
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05001991 DominatorTree *DT = nullptr;
1992 InformationCache &InfoCache = A.getInfoCache();
1993 if (const Function *Fn = getAnchorScope())
1994 DT = InfoCache.getAnalysisResultForFunction<DominatorTreeAnalysis>(*Fn);
1995
Johannes Doerfert1a746452019-10-20 22:28:49 -05001996 auto VisitValueCB = [&](Value &V, AANonNull::StateType &T,
Johannes Doerfertb9b87912019-08-20 06:02:39 +00001997 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00001998 const auto &AA = A.getAAFor<AANonNull>(*this, IRPosition::value(V));
1999 if (!Stripped && this == &AA) {
Johannes Doerfert2d6d6512019-10-29 11:46:00 -05002000 if (!isKnownNonZero(&V, DL, 0, /* TODO: AC */ nullptr, getCtxI(), DT))
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002001 T.indicatePessimisticFixpoint();
2002 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002003 // Use abstract attribute information.
2004 const AANonNull::StateType &NS =
2005 static_cast<const AANonNull::StateType &>(AA.getState());
2006 T ^= NS;
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002007 }
2008 return T.isValidState();
2009 };
2010
2011 StateType T;
2012 if (!genericValueTraversal<AANonNull, StateType>(A, getIRPosition(), *this,
2013 T, VisitValueCB))
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002014 return indicatePessimisticFixpoint();
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002015
2016 return clampStateAndIndicateChange(getState(), T);
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002017 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002018
2019 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002020 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002021};
2022
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002023/// NonNull attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002024struct AANonNullReturned final
2025 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002026 AANonNullReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002027 : AAReturnedFromReturnedValues<AANonNull, AANonNullImpl>(IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002028
2029 /// See AbstractAttribute::trackStatistics()
2030 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(nonnull) }
2031};
2032
Hideto Ueno54869ec2019-07-15 06:49:04 +00002033/// NonNull attribute for function argument.
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002034struct AANonNullArgument final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002035 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2036 AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002037 AANonNullArgument(const IRPosition &IRP)
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002038 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AANonNull,
2039 AANonNullImpl>(
2040 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002041
2042 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002043 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002044};
2045
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002046struct AANonNullCallSiteArgument final : AANonNullFloating {
2047 AANonNullCallSiteArgument(const IRPosition &IRP) : AANonNullFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002048
2049 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert1aac1822019-08-29 01:26:09 +00002050 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(nonnull) }
Hideto Ueno54869ec2019-07-15 06:49:04 +00002051};
Johannes Doerfert007153e2019-08-05 23:26:06 +00002052
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002053/// NonNull attribute for a call site return position.
2054struct AANonNullCallSiteReturned final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002055 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2056 AANonNullImpl> {
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002057 AANonNullCallSiteReturned(const IRPosition &IRP)
Hideto Ueno96e6ce42019-10-08 15:25:56 +00002058 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AANonNull,
2059 AANonNullImpl>(
2060 IRP) {}
Johannes Doerfertb9b87912019-08-20 06:02:39 +00002061
2062 /// See AbstractAttribute::trackStatistics()
2063 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(nonnull) }
2064};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002065
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002066/// ------------------------ No-Recurse Attributes ----------------------------
2067
2068struct AANoRecurseImpl : public AANoRecurse {
2069 AANoRecurseImpl(const IRPosition &IRP) : AANoRecurse(IRP) {}
2070
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002071 /// See AbstractAttribute::getAsStr()
2072 const std::string getAsStr() const override {
2073 return getAssumed() ? "norecurse" : "may-recurse";
2074 }
2075};
2076
2077struct AANoRecurseFunction final : AANoRecurseImpl {
2078 AANoRecurseFunction(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2079
Hideto Ueno63f60662019-09-21 15:13:19 +00002080 /// See AbstractAttribute::initialize(...).
2081 void initialize(Attributor &A) override {
2082 AANoRecurseImpl::initialize(A);
2083 if (const Function *F = getAnchorScope())
Johannes Doerfert26d02b02019-12-30 16:15:38 -06002084 if (A.getInfoCache().getSccSize(*F) != 1)
2085 indicatePessimisticFixpoint();
Hideto Ueno63f60662019-09-21 15:13:19 +00002086 }
2087
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002088 /// See AbstractAttribute::updateImpl(...).
2089 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno63f60662019-09-21 15:13:19 +00002090
Johannes Doerfert26d02b02019-12-30 16:15:38 -06002091 // If all live call sites are known to be no-recurse, we are as well.
2092 auto CallSitePred = [&](AbstractCallSite ACS) {
2093 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(
2094 *this, IRPosition::function(*ACS.getInstruction()->getFunction()),
2095 /* TrackDependence */ false, DepClassTy::OPTIONAL);
2096 return NoRecurseAA.isKnownNoRecurse();
2097 };
2098 bool AllCallSitesKnown;
2099 if (A.checkForAllCallSites(CallSitePred, *this, true, AllCallSitesKnown)) {
2100 // If we know all call sites and all are known no-recurse, we are done.
2101 // If all known call sites, which might not be all that exist, are known
2102 // to be no-recurse, we are not done but we can continue to assume
2103 // no-recurse. If one of the call sites we have not visited will become
2104 // live, another update is triggered.
2105 if (AllCallSitesKnown)
2106 indicateOptimisticFixpoint();
2107 return ChangeStatus::UNCHANGED;
2108 }
2109
2110 // If the above check does not hold anymore we look at the calls.
Hideto Ueno63f60662019-09-21 15:13:19 +00002111 auto CheckForNoRecurse = [&](Instruction &I) {
2112 ImmutableCallSite ICS(&I);
2113 if (ICS.hasFnAttr(Attribute::NoRecurse))
2114 return true;
2115
2116 const auto &NoRecurseAA =
2117 A.getAAFor<AANoRecurse>(*this, IRPosition::callsite_function(ICS));
2118 if (!NoRecurseAA.isAssumedNoRecurse())
2119 return false;
2120
2121 // Recursion to the same function
2122 if (ICS.getCalledFunction() == getAnchorScope())
2123 return false;
2124
2125 return true;
2126 };
2127
2128 if (!A.checkForAllCallLikeInstructions(CheckForNoRecurse, *this))
2129 return indicatePessimisticFixpoint();
2130 return ChangeStatus::UNCHANGED;
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002131 }
2132
2133 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(norecurse) }
2134};
2135
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002136/// NoRecurse attribute deduction for a call sites.
2137struct AANoRecurseCallSite final : AANoRecurseImpl {
2138 AANoRecurseCallSite(const IRPosition &IRP) : AANoRecurseImpl(IRP) {}
2139
2140 /// See AbstractAttribute::initialize(...).
2141 void initialize(Attributor &A) override {
2142 AANoRecurseImpl::initialize(A);
2143 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002144 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002145 indicatePessimisticFixpoint();
2146 }
2147
2148 /// See AbstractAttribute::updateImpl(...).
2149 ChangeStatus updateImpl(Attributor &A) override {
2150 // TODO: Once we have call site specific value information we can provide
2151 // call site specific liveness information and then it makes
2152 // sense to specialize attributes for call sites arguments instead of
2153 // redirecting requests to the callee argument.
2154 Function *F = getAssociatedFunction();
2155 const IRPosition &FnPos = IRPosition::function(*F);
2156 auto &FnAA = A.getAAFor<AANoRecurse>(*this, FnPos);
2157 return clampStateAndIndicateChange(
2158 getState(),
2159 static_cast<const AANoRecurse::StateType &>(FnAA.getState()));
2160 }
2161
2162 /// See AbstractAttribute::trackStatistics()
2163 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(norecurse); }
2164};
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002165
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002166/// -------------------- Undefined-Behavior Attributes ------------------------
2167
2168struct AAUndefinedBehaviorImpl : public AAUndefinedBehavior {
2169 AAUndefinedBehaviorImpl(const IRPosition &IRP) : AAUndefinedBehavior(IRP) {}
2170
2171 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert5732f562019-12-24 19:25:08 -06002172 // through a pointer (i.e. also branches etc.)
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002173 ChangeStatus updateImpl(Attributor &A) override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002174 const size_t UBPrevSize = KnownUBInsts.size();
2175 const size_t NoUBPrevSize = AssumedNoUBInsts.size();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002176
Johannes Doerfert5732f562019-12-24 19:25:08 -06002177 auto InspectMemAccessInstForUB = [&](Instruction &I) {
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002178 // Skip instructions that are already saved.
Hideto Uenoef4febd2019-12-29 17:34:08 +09002179 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002180 return true;
2181
Hideto Uenoef4febd2019-12-29 17:34:08 +09002182 // If we reach here, we know we have an instruction
2183 // that accesses memory through a pointer operand,
2184 // for which getPointerOperand() should give it to us.
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06002185 const Value *PtrOp = getPointerOperand(&I, /* AllowVolatile */ true);
Hideto Uenoef4febd2019-12-29 17:34:08 +09002186 assert(PtrOp &&
2187 "Expected pointer operand of memory accessing instruction");
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002188
Johannes Doerfert5732f562019-12-24 19:25:08 -06002189 // A memory access through a pointer is considered UB
2190 // only if the pointer has constant null value.
2191 // TODO: Expand it to not only check constant values.
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002192 if (!isa<ConstantPointerNull>(PtrOp)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002193 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002194 return true;
2195 }
Johannes Doerfert5732f562019-12-24 19:25:08 -06002196 const Type *PtrTy = PtrOp->getType();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002197
Johannes Doerfert5732f562019-12-24 19:25:08 -06002198 // Because we only consider instructions inside functions,
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002199 // assume that a parent function exists.
2200 const Function *F = I.getFunction();
2201
Johannes Doerfert5732f562019-12-24 19:25:08 -06002202 // A memory access using constant null pointer is only considered UB
2203 // if null pointer is _not_ defined for the target platform.
Hideto Uenoef4febd2019-12-29 17:34:08 +09002204 if (llvm::NullPointerIsDefined(F, PtrTy->getPointerAddressSpace()))
2205 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002206 else
Hideto Uenoef4febd2019-12-29 17:34:08 +09002207 KnownUBInsts.insert(&I);
2208 return true;
2209 };
2210
2211 auto InspectBrInstForUB = [&](Instruction &I) {
2212 // A conditional branch instruction is considered UB if it has `undef`
2213 // condition.
2214
2215 // Skip instructions that are already saved.
2216 if (AssumedNoUBInsts.count(&I) || KnownUBInsts.count(&I))
2217 return true;
2218
2219 // We know we have a branch instruction.
2220 auto BrInst = cast<BranchInst>(&I);
2221
2222 // Unconditional branches are never considered UB.
2223 if (BrInst->isUnconditional())
2224 return true;
2225
2226 // Either we stopped and the appropriate action was taken,
2227 // or we got back a simplified value to continue.
2228 Optional<Value *> SimplifiedCond =
2229 stopOnUndefOrAssumed(A, BrInst->getCondition(), BrInst);
2230 if (!SimplifiedCond.hasValue())
2231 return true;
2232 AssumedNoUBInsts.insert(&I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002233 return true;
2234 };
2235
Johannes Doerfert5732f562019-12-24 19:25:08 -06002236 A.checkForAllInstructions(InspectMemAccessInstForUB, *this,
2237 {Instruction::Load, Instruction::Store,
2238 Instruction::AtomicCmpXchg,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06002239 Instruction::AtomicRMW},
2240 /* CheckBBLivenessOnly */ true);
2241 A.checkForAllInstructions(InspectBrInstForUB, *this, {Instruction::Br},
2242 /* CheckBBLivenessOnly */ true);
Hideto Uenoef4febd2019-12-29 17:34:08 +09002243 if (NoUBPrevSize != AssumedNoUBInsts.size() ||
2244 UBPrevSize != KnownUBInsts.size())
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002245 return ChangeStatus::CHANGED;
2246 return ChangeStatus::UNCHANGED;
2247 }
2248
Hideto Uenoef4febd2019-12-29 17:34:08 +09002249 bool isKnownToCauseUB(Instruction *I) const override {
2250 return KnownUBInsts.count(I);
2251 }
2252
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002253 bool isAssumedToCauseUB(Instruction *I) const override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002254 // In simple words, if an instruction is not in the assumed to _not_
2255 // cause UB, then it is assumed UB (that includes those
2256 // in the KnownUBInsts set). The rest is boilerplate
2257 // is to ensure that it is one of the instructions we test
2258 // for UB.
2259
2260 switch (I->getOpcode()) {
2261 case Instruction::Load:
2262 case Instruction::Store:
2263 case Instruction::AtomicCmpXchg:
2264 case Instruction::AtomicRMW:
2265 return !AssumedNoUBInsts.count(I);
2266 case Instruction::Br: {
2267 auto BrInst = cast<BranchInst>(I);
2268 if (BrInst->isUnconditional())
2269 return false;
2270 return !AssumedNoUBInsts.count(I);
2271 } break;
2272 default:
2273 return false;
2274 }
2275 return false;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002276 }
2277
2278 ChangeStatus manifest(Attributor &A) override {
Hideto Uenoef4febd2019-12-29 17:34:08 +09002279 if (KnownUBInsts.empty())
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002280 return ChangeStatus::UNCHANGED;
Hideto Uenoef4febd2019-12-29 17:34:08 +09002281 for (Instruction *I : KnownUBInsts)
Hideto Uenocb5eb132019-12-27 02:39:37 +09002282 A.changeToUnreachableAfterManifest(I);
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002283 return ChangeStatus::CHANGED;
2284 }
2285
2286 /// See AbstractAttribute::getAsStr()
2287 const std::string getAsStr() const override {
2288 return getAssumed() ? "undefined-behavior" : "no-ub";
2289 }
2290
Hideto Uenoef4febd2019-12-29 17:34:08 +09002291 /// Note: The correctness of this analysis depends on the fact that the
2292 /// following 2 sets will stop changing after some point.
2293 /// "Change" here means that their size changes.
2294 /// The size of each set is monotonically increasing
2295 /// (we only add items to them) and it is upper bounded by the number of
2296 /// instructions in the processed function (we can never save more
2297 /// elements in either set than this number). Hence, at some point,
2298 /// they will stop increasing.
2299 /// Consequently, at some point, both sets will have stopped
2300 /// changing, effectively making the analysis reach a fixpoint.
2301
2302 /// Note: These 2 sets are disjoint and an instruction can be considered
2303 /// one of 3 things:
2304 /// 1) Known to cause UB (AAUndefinedBehavior could prove it) and put it in
2305 /// the KnownUBInsts set.
2306 /// 2) Assumed to cause UB (in every updateImpl, AAUndefinedBehavior
2307 /// has a reason to assume it).
2308 /// 3) Assumed to not cause UB. very other instruction - AAUndefinedBehavior
2309 /// could not find a reason to assume or prove that it can cause UB,
2310 /// hence it assumes it doesn't. We have a set for these instructions
2311 /// so that we don't reprocess them in every update.
2312 /// Note however that instructions in this set may cause UB.
2313
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002314protected:
Hideto Uenoef4febd2019-12-29 17:34:08 +09002315 /// A set of all live instructions _known_ to cause UB.
2316 SmallPtrSet<Instruction *, 8> KnownUBInsts;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002317
2318private:
Hideto Uenoef4febd2019-12-29 17:34:08 +09002319 /// A set of all the (live) instructions that are assumed to _not_ cause UB.
2320 SmallPtrSet<Instruction *, 8> AssumedNoUBInsts;
2321
2322 // Should be called on updates in which if we're processing an instruction
2323 // \p I that depends on a value \p V, one of the following has to happen:
2324 // - If the value is assumed, then stop.
2325 // - If the value is known but undef, then consider it UB.
2326 // - Otherwise, do specific processing with the simplified value.
2327 // We return None in the first 2 cases to signify that an appropriate
2328 // action was taken and the caller should stop.
2329 // Otherwise, we return the simplified value that the caller should
2330 // use for specific processing.
2331 Optional<Value *> stopOnUndefOrAssumed(Attributor &A, const Value *V,
2332 Instruction *I) {
2333 const auto &ValueSimplifyAA =
2334 A.getAAFor<AAValueSimplify>(*this, IRPosition::value(*V));
2335 Optional<Value *> SimplifiedV =
2336 ValueSimplifyAA.getAssumedSimplifiedValue(A);
2337 if (!ValueSimplifyAA.isKnown()) {
2338 // Don't depend on assumed values.
2339 return llvm::None;
2340 }
2341 if (!SimplifiedV.hasValue()) {
2342 // If it is known (which we tested above) but it doesn't have a value,
2343 // then we can assume `undef` and hence the instruction is UB.
2344 KnownUBInsts.insert(I);
2345 return llvm::None;
2346 }
2347 Value *Val = SimplifiedV.getValue();
2348 if (isa<UndefValue>(Val)) {
2349 KnownUBInsts.insert(I);
2350 return llvm::None;
2351 }
2352 return Val;
2353 }
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002354};
2355
2356struct AAUndefinedBehaviorFunction final : AAUndefinedBehaviorImpl {
2357 AAUndefinedBehaviorFunction(const IRPosition &IRP)
2358 : AAUndefinedBehaviorImpl(IRP) {}
2359
2360 /// See AbstractAttribute::trackStatistics()
2361 void trackStatistics() const override {
2362 STATS_DECL(UndefinedBehaviorInstruction, Instruction,
2363 "Number of instructions known to have UB");
2364 BUILD_STAT_NAME(UndefinedBehaviorInstruction, Instruction) +=
Hideto Uenoef4febd2019-12-29 17:34:08 +09002365 KnownUBInsts.size();
Johannes Doerfert58f324a2019-12-24 18:48:50 -06002366 }
2367};
2368
Hideto Ueno11d37102019-07-17 15:15:43 +00002369/// ------------------------ Will-Return Attributes ----------------------------
2370
Hideto Ueno11d37102019-07-17 15:15:43 +00002371// Helper function that checks whether a function has any cycle.
2372// TODO: Replace with more efficent code
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002373static bool containsCycle(Function &F) {
Hideto Ueno11d37102019-07-17 15:15:43 +00002374 SmallPtrSet<BasicBlock *, 32> Visited;
2375
2376 // Traverse BB by dfs and check whether successor is already visited.
2377 for (BasicBlock *BB : depth_first(&F)) {
2378 Visited.insert(BB);
2379 for (auto *SuccBB : successors(BB)) {
2380 if (Visited.count(SuccBB))
2381 return true;
2382 }
2383 }
2384 return false;
2385}
2386
2387// Helper function that checks the function have a loop which might become an
2388// endless loop
2389// FIXME: Any cycle is regarded as endless loop for now.
2390// We have to allow some patterns.
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002391static bool containsPossiblyEndlessLoop(Function *F) {
2392 return !F || !F->hasExactDefinition() || containsCycle(*F);
Hideto Ueno11d37102019-07-17 15:15:43 +00002393}
2394
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002395struct AAWillReturnImpl : public AAWillReturn {
2396 AAWillReturnImpl(const IRPosition &IRP) : AAWillReturn(IRP) {}
Hideto Ueno11d37102019-07-17 15:15:43 +00002397
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002398 /// See AbstractAttribute::initialize(...).
2399 void initialize(Attributor &A) override {
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002400 AAWillReturn::initialize(A);
Hideto Ueno11d37102019-07-17 15:15:43 +00002401
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002402 Function *F = getAssociatedFunction();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002403 if (containsPossiblyEndlessLoop(F))
2404 indicatePessimisticFixpoint();
2405 }
Hideto Ueno11d37102019-07-17 15:15:43 +00002406
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002407 /// See AbstractAttribute::updateImpl(...).
2408 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002409 auto CheckForWillReturn = [&](Instruction &I) {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002410 IRPosition IPos = IRPosition::callsite_function(ImmutableCallSite(&I));
2411 const auto &WillReturnAA = A.getAAFor<AAWillReturn>(*this, IPos);
2412 if (WillReturnAA.isKnownWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002413 return true;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002414 if (!WillReturnAA.isAssumedWillReturn())
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002415 return false;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002416 const auto &NoRecurseAA = A.getAAFor<AANoRecurse>(*this, IPos);
2417 return NoRecurseAA.isAssumedNoRecurse();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002418 };
2419
2420 if (!A.checkForAllCallLikeInstructions(CheckForWillReturn, *this))
2421 return indicatePessimisticFixpoint();
2422
2423 return ChangeStatus::UNCHANGED;
2424 }
2425
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002426 /// See AbstractAttribute::getAsStr()
2427 const std::string getAsStr() const override {
2428 return getAssumed() ? "willreturn" : "may-noreturn";
2429 }
2430};
2431
2432struct AAWillReturnFunction final : AAWillReturnImpl {
2433 AAWillReturnFunction(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2434
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002435 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002436 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(willreturn) }
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002437};
Hideto Ueno11d37102019-07-17 15:15:43 +00002438
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002439/// WillReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002440struct AAWillReturnCallSite final : AAWillReturnImpl {
2441 AAWillReturnCallSite(const IRPosition &IRP) : AAWillReturnImpl(IRP) {}
2442
2443 /// See AbstractAttribute::initialize(...).
2444 void initialize(Attributor &A) override {
2445 AAWillReturnImpl::initialize(A);
2446 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002447 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002448 indicatePessimisticFixpoint();
2449 }
2450
2451 /// See AbstractAttribute::updateImpl(...).
2452 ChangeStatus updateImpl(Attributor &A) override {
2453 // TODO: Once we have call site specific value information we can provide
2454 // call site specific liveness information and then it makes
2455 // sense to specialize attributes for call sites arguments instead of
2456 // redirecting requests to the callee argument.
2457 Function *F = getAssociatedFunction();
2458 const IRPosition &FnPos = IRPosition::function(*F);
2459 auto &FnAA = A.getAAFor<AAWillReturn>(*this, FnPos);
2460 return clampStateAndIndicateChange(
2461 getState(),
2462 static_cast<const AAWillReturn::StateType &>(FnAA.getState()));
2463 }
2464
2465 /// See AbstractAttribute::trackStatistics()
2466 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(willreturn); }
2467};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002468
Pankaj Gode04945c92019-11-22 18:40:47 +05302469/// -------------------AAReachability Attribute--------------------------
2470
2471struct AAReachabilityImpl : AAReachability {
2472 AAReachabilityImpl(const IRPosition &IRP) : AAReachability(IRP) {}
2473
2474 const std::string getAsStr() const override {
2475 // TODO: Return the number of reachable queries.
2476 return "reachable";
2477 }
2478
2479 /// See AbstractAttribute::initialize(...).
Johannes Doerfert0bc33362019-12-16 21:03:18 -06002480 void initialize(Attributor &A) override { indicatePessimisticFixpoint(); }
Pankaj Gode04945c92019-11-22 18:40:47 +05302481
2482 /// See AbstractAttribute::updateImpl(...).
2483 ChangeStatus updateImpl(Attributor &A) override {
2484 return indicatePessimisticFixpoint();
2485 }
2486};
2487
2488struct AAReachabilityFunction final : public AAReachabilityImpl {
2489 AAReachabilityFunction(const IRPosition &IRP) : AAReachabilityImpl(IRP) {}
2490
2491 /// See AbstractAttribute::trackStatistics()
2492 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(reachable); }
2493};
2494
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002495/// ------------------------ NoAlias Argument Attribute ------------------------
2496
Johannes Doerfert344d0382019-08-07 22:34:26 +00002497struct AANoAliasImpl : AANoAlias {
Johannes Doerfert32e98a72020-02-14 19:23:21 -06002498 AANoAliasImpl(const IRPosition &IRP) : AANoAlias(IRP) {
2499 assert(getAssociatedType()->isPointerTy() &&
2500 "Noalias is a pointer attribute");
2501 }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002502
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002503 const std::string getAsStr() const override {
2504 return getAssumed() ? "noalias" : "may-alias";
2505 }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002506};
2507
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002508/// NoAlias attribute for a floating value.
2509struct AANoAliasFloating final : AANoAliasImpl {
2510 AANoAliasFloating(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2511
Hideto Uenocbab3342019-08-29 05:52:00 +00002512 /// See AbstractAttribute::initialize(...).
2513 void initialize(Attributor &A) override {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002514 AANoAliasImpl::initialize(A);
Johannes Doerfert87ddf1f2020-01-25 20:21:41 -06002515 Value *Val = &getAssociatedValue();
2516 do {
2517 CastInst *CI = dyn_cast<CastInst>(Val);
2518 if (!CI)
2519 break;
2520 Value *Base = CI->getOperand(0);
2521 if (Base->getNumUses() != 1)
2522 break;
2523 Val = Base;
2524 } while (true);
2525
Johannes Doerfert32e98a72020-02-14 19:23:21 -06002526 if (!Val->getType()->isPointerTy()) {
2527 indicatePessimisticFixpoint();
2528 return;
2529 }
2530
Johannes Doerfert72adda12019-10-10 05:33:21 +00002531 if (isa<AllocaInst>(Val))
2532 indicateOptimisticFixpoint();
Johannes Doerfert24ae77e2020-01-27 22:19:11 -06002533 else if (isa<ConstantPointerNull>(Val) &&
2534 !NullPointerIsDefined(getAnchorScope(),
Johannes Doerfert87ddf1f2020-01-25 20:21:41 -06002535 Val->getType()->getPointerAddressSpace()))
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002536 indicateOptimisticFixpoint();
Johannes Doerfert87ddf1f2020-01-25 20:21:41 -06002537 else if (Val != &getAssociatedValue()) {
2538 const auto &ValNoAliasAA =
2539 A.getAAFor<AANoAlias>(*this, IRPosition::value(*Val));
2540 if (ValNoAliasAA.isKnownNoAlias())
2541 indicateOptimisticFixpoint();
2542 }
Hideto Uenocbab3342019-08-29 05:52:00 +00002543 }
2544
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002545 /// See AbstractAttribute::updateImpl(...).
2546 ChangeStatus updateImpl(Attributor &A) override {
2547 // TODO: Implement this.
2548 return indicatePessimisticFixpoint();
2549 }
2550
2551 /// See AbstractAttribute::trackStatistics()
2552 void trackStatistics() const override {
2553 STATS_DECLTRACK_FLOATING_ATTR(noalias)
2554 }
2555};
2556
2557/// NoAlias attribute for an argument.
Hideto Uenocbab3342019-08-29 05:52:00 +00002558struct AANoAliasArgument final
2559 : AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl> {
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002560 using Base = AAArgumentFromCallSiteArguments<AANoAlias, AANoAliasImpl>;
2561 AANoAliasArgument(const IRPosition &IRP) : Base(IRP) {}
2562
Johannes Doerfert2baf0002020-01-12 00:18:07 -06002563 /// See AbstractAttribute::initialize(...).
2564 void initialize(Attributor &A) override {
2565 Base::initialize(A);
2566 // See callsite argument attribute and callee argument attribute.
2567 if (hasAttr({Attribute::ByVal}))
2568 indicateOptimisticFixpoint();
2569 }
2570
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002571 /// See AbstractAttribute::update(...).
2572 ChangeStatus updateImpl(Attributor &A) override {
2573 // We have to make sure no-alias on the argument does not break
2574 // synchronization when this is a callback argument, see also [1] below.
2575 // If synchronization cannot be affected, we delegate to the base updateImpl
2576 // function, otherwise we give up for now.
2577
2578 // If the function is no-sync, no-alias cannot break synchronization.
2579 const auto &NoSyncAA = A.getAAFor<AANoSync>(
2580 *this, IRPosition::function_scope(getIRPosition()));
2581 if (NoSyncAA.isAssumedNoSync())
2582 return Base::updateImpl(A);
2583
2584 // If the argument is read-only, no-alias cannot break synchronization.
2585 const auto &MemBehaviorAA =
2586 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
2587 if (MemBehaviorAA.isAssumedReadOnly())
2588 return Base::updateImpl(A);
2589
2590 // If the argument is never passed through callbacks, no-alias cannot break
2591 // synchronization.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002592 bool AllCallSitesKnown;
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002593 if (A.checkForAllCallSites(
2594 [](AbstractCallSite ACS) { return !ACS.isCallbackCall(); }, *this,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002595 true, AllCallSitesKnown))
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002596 return Base::updateImpl(A);
2597
2598 // TODO: add no-alias but make sure it doesn't break synchronization by
2599 // introducing fake uses. See:
2600 // [1] Compiler Optimizations for OpenMP, J. Doerfert and H. Finkel,
2601 // International Workshop on OpenMP 2018,
2602 // http://compilers.cs.uni-saarland.de/people/doerfert/par_opt18.pdf
2603
2604 return indicatePessimisticFixpoint();
2605 }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002606
2607 /// See AbstractAttribute::trackStatistics()
2608 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(noalias) }
2609};
2610
2611struct AANoAliasCallSiteArgument final : AANoAliasImpl {
2612 AANoAliasCallSiteArgument(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2613
Hideto Uenocbab3342019-08-29 05:52:00 +00002614 /// See AbstractAttribute::initialize(...).
2615 void initialize(Attributor &A) override {
Hideto Ueno6381b142019-08-30 10:00:32 +00002616 // See callsite argument attribute and callee argument attribute.
2617 ImmutableCallSite ICS(&getAnchorValue());
2618 if (ICS.paramHasAttr(getArgNo(), Attribute::NoAlias))
2619 indicateOptimisticFixpoint();
Johannes Doerfert24ae77e2020-01-27 22:19:11 -06002620 Value &Val = getAssociatedValue();
2621 if (isa<ConstantPointerNull>(Val) &&
2622 !NullPointerIsDefined(getAnchorScope(),
2623 Val.getType()->getPointerAddressSpace()))
2624 indicateOptimisticFixpoint();
Hideto Uenocbab3342019-08-29 05:52:00 +00002625 }
2626
Johannes Doerfert53992c72020-01-27 22:24:32 -06002627 /// Determine if the underlying value may alias with the call site argument
2628 /// \p OtherArgNo of \p ICS (= the underlying call site).
2629 bool mayAliasWithArgument(Attributor &A, AAResults *&AAR,
2630 const AAMemoryBehavior &MemBehaviorAA,
2631 ImmutableCallSite ICS, unsigned OtherArgNo) {
2632 // We do not need to worry about aliasing with the underlying IRP.
2633 if (this->getArgNo() == (int)OtherArgNo)
2634 return false;
2635
2636 // If it is not a pointer or pointer vector we do not alias.
2637 const Value *ArgOp = ICS.getArgOperand(OtherArgNo);
2638 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
2639 return false;
2640
2641 auto &ICSArgMemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
2642 *this, IRPosition::callsite_argument(ICS, OtherArgNo),
2643 /* TrackDependence */ false);
2644
2645 // If the argument is readnone, there is no read-write aliasing.
2646 if (ICSArgMemBehaviorAA.isAssumedReadNone()) {
2647 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2648 return false;
2649 }
2650
2651 // If the argument is readonly and the underlying value is readonly, there
2652 // is no read-write aliasing.
2653 bool IsReadOnly = MemBehaviorAA.isAssumedReadOnly();
2654 if (ICSArgMemBehaviorAA.isAssumedReadOnly() && IsReadOnly) {
2655 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2656 A.recordDependence(ICSArgMemBehaviorAA, *this, DepClassTy::OPTIONAL);
2657 return false;
2658 }
2659
2660 // We have to utilize actual alias analysis queries so we need the object.
2661 if (!AAR)
2662 AAR = A.getInfoCache().getAAResultsForFunction(*getAnchorScope());
2663
2664 // Try to rule it out at the call site.
2665 bool IsAliasing = !AAR || !AAR->isNoAlias(&getAssociatedValue(), ArgOp);
2666 LLVM_DEBUG(dbgs() << "[NoAliasCSArg] Check alias between "
2667 "callsite arguments: "
2668 << getAssociatedValue() << " " << *ArgOp << " => "
2669 << (IsAliasing ? "" : "no-") << "alias \n");
2670
2671 return IsAliasing;
2672 }
2673
2674 bool
2675 isKnownNoAliasDueToNoAliasPreservation(Attributor &A, AAResults *&AAR,
2676 const AAMemoryBehavior &MemBehaviorAA,
2677 const AANoAlias &NoAliasAA) {
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002678 // We can deduce "noalias" if the following conditions hold.
2679 // (i) Associated value is assumed to be noalias in the definition.
2680 // (ii) Associated value is assumed to be no-capture in all the uses
2681 // possibly executed before this callsite.
2682 // (iii) There is no other pointer argument which could alias with the
2683 // value.
2684
Johannes Doerfert53992c72020-01-27 22:24:32 -06002685 bool AssociatedValueIsNoAliasAtDef = NoAliasAA.isAssumedNoAlias();
2686 if (!AssociatedValueIsNoAliasAtDef) {
2687 LLVM_DEBUG(dbgs() << "[AANoAlias] " << getAssociatedValue()
2688 << " is not no-alias at the definition\n");
2689 return false;
2690 }
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002691
Johannes Doerfert53992c72020-01-27 22:24:32 -06002692 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2693 auto &NoCaptureAA =
2694 A.getAAFor<AANoCapture>(*this, VIRP, /* TrackDependence */ false);
2695 // Check whether the value is captured in the scope using AANoCapture.
2696 // FIXME: This is conservative though, it is better to look at CFG and
2697 // check only uses possibly executed before this callsite.
Johannes Doerfert72adda12019-10-10 05:33:21 +00002698 if (!NoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
2699 LLVM_DEBUG(
Johannes Doerfert53992c72020-01-27 22:24:32 -06002700 dbgs() << "[AANoAliasCSArg] " << getAssociatedValue()
Johannes Doerfert72adda12019-10-10 05:33:21 +00002701 << " cannot be noalias as it is potentially captured\n");
Johannes Doerfert53992c72020-01-27 22:24:32 -06002702 return false;
Johannes Doerfert72adda12019-10-10 05:33:21 +00002703 }
Johannes Doerfert53992c72020-01-27 22:24:32 -06002704 A.recordDependence(NoCaptureAA, *this, DepClassTy::OPTIONAL);
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002705
Johannes Doerfert53992c72020-01-27 22:24:32 -06002706 // Check there is no other pointer argument which could alias with the
2707 // value passed at this call site.
Johannes Doerfertb1b441d2019-10-10 01:19:57 -05002708 // TODO: AbstractCallSite
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002709 ImmutableCallSite ICS(&getAnchorValue());
Johannes Doerfert53992c72020-01-27 22:24:32 -06002710 for (unsigned OtherArgNo = 0; OtherArgNo < ICS.getNumArgOperands();
2711 OtherArgNo++)
2712 if (mayAliasWithArgument(A, AAR, MemBehaviorAA, ICS, OtherArgNo))
2713 return false;
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002714
Johannes Doerfert53992c72020-01-27 22:24:32 -06002715 return true;
2716 }
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002717
Johannes Doerfert53992c72020-01-27 22:24:32 -06002718 /// See AbstractAttribute::updateImpl(...).
2719 ChangeStatus updateImpl(Attributor &A) override {
2720 // If the argument is readnone we are done as there are no accesses via the
2721 // argument.
2722 auto &MemBehaviorAA =
2723 A.getAAFor<AAMemoryBehavior>(*this, getIRPosition(),
2724 /* TrackDependence */ false);
2725 if (MemBehaviorAA.isAssumedReadNone()) {
2726 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
2727 return ChangeStatus::UNCHANGED;
Hideto Ueno1d68ed82019-09-11 07:00:33 +00002728 }
2729
Johannes Doerfert53992c72020-01-27 22:24:32 -06002730 const IRPosition &VIRP = IRPosition::value(getAssociatedValue());
2731 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, VIRP,
2732 /* TrackDependence */ false);
2733
2734 AAResults *AAR = nullptr;
2735 if (isKnownNoAliasDueToNoAliasPreservation(A, AAR, MemBehaviorAA,
2736 NoAliasAA)) {
2737 LLVM_DEBUG(
2738 dbgs() << "[AANoAlias] No-Alias deduced via no-alias preservation\n");
2739 return ChangeStatus::UNCHANGED;
2740 }
2741
2742 return indicatePessimisticFixpoint();
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002743 }
2744
2745 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00002746 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(noalias) }
Johannes Doerfert6dedc782019-08-16 21:31:11 +00002747};
2748
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002749/// NoAlias attribute for function return value.
Johannes Doerfertbeb51502019-08-07 22:36:15 +00002750struct AANoAliasReturned final : AANoAliasImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00002751 AANoAliasReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002752
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002753 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002754 virtual ChangeStatus updateImpl(Attributor &A) override {
2755
2756 auto CheckReturnValue = [&](Value &RV) -> bool {
2757 if (Constant *C = dyn_cast<Constant>(&RV))
2758 if (C->isNullValue() || isa<UndefValue>(C))
2759 return true;
2760
2761 /// For now, we can only deduce noalias if we have call sites.
2762 /// FIXME: add more support.
2763 ImmutableCallSite ICS(&RV);
2764 if (!ICS)
2765 return false;
2766
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002767 const IRPosition &RVPos = IRPosition::value(RV);
2768 const auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, RVPos);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00002769 if (!NoAliasAA.isAssumedNoAlias())
2770 return false;
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002771
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00002772 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, RVPos);
2773 return NoCaptureAA.isAssumedNoCaptureMaybeReturned();
Johannes Doerfertfe6dbad2019-08-16 19:36:17 +00002774 };
2775
2776 if (!A.checkForAllReturnedValues(CheckReturnValue, *this))
2777 return indicatePessimisticFixpoint();
2778
2779 return ChangeStatus::UNCHANGED;
2780 }
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00002781
2782 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00002783 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(noalias) }
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00002784};
2785
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002786/// NoAlias attribute deduction for a call site return value.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002787struct AANoAliasCallSiteReturned final : AANoAliasImpl {
2788 AANoAliasCallSiteReturned(const IRPosition &IRP) : AANoAliasImpl(IRP) {}
2789
2790 /// See AbstractAttribute::initialize(...).
2791 void initialize(Attributor &A) override {
2792 AANoAliasImpl::initialize(A);
2793 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00002794 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002795 indicatePessimisticFixpoint();
2796 }
2797
2798 /// See AbstractAttribute::updateImpl(...).
2799 ChangeStatus updateImpl(Attributor &A) override {
2800 // TODO: Once we have call site specific value information we can provide
2801 // call site specific liveness information and then it makes
2802 // sense to specialize attributes for call sites arguments instead of
2803 // redirecting requests to the callee argument.
2804 Function *F = getAssociatedFunction();
2805 const IRPosition &FnPos = IRPosition::returned(*F);
2806 auto &FnAA = A.getAAFor<AANoAlias>(*this, FnPos);
2807 return clampStateAndIndicateChange(
2808 getState(), static_cast<const AANoAlias::StateType &>(FnAA.getState()));
2809 }
2810
2811 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert56e9b602019-09-04 20:34:57 +00002812 void trackStatistics() const override { STATS_DECLTRACK_CSRET_ATTR(noalias); }
Johannes Doerfert3fac6682019-08-30 15:24:52 +00002813};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00002814
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002815/// -------------------AAIsDead Function Attribute-----------------------
2816
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002817struct AAIsDeadValueImpl : public AAIsDead {
2818 AAIsDeadValueImpl(const IRPosition &IRP) : AAIsDead(IRP) {}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00002819
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002820 /// See AAIsDead::isAssumedDead().
2821 bool isAssumedDead() const override { return getAssumed(); }
2822
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06002823 /// See AAIsDead::isKnownDead().
2824 bool isKnownDead() const override { return getKnown(); }
2825
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002826 /// See AAIsDead::isAssumedDead(BasicBlock *).
2827 bool isAssumedDead(const BasicBlock *BB) const override { return false; }
2828
2829 /// See AAIsDead::isKnownDead(BasicBlock *).
2830 bool isKnownDead(const BasicBlock *BB) const override { return false; }
2831
2832 /// See AAIsDead::isAssumedDead(Instruction *I).
2833 bool isAssumedDead(const Instruction *I) const override {
2834 return I == getCtxI() && isAssumedDead();
2835 }
2836
2837 /// See AAIsDead::isKnownDead(Instruction *I).
2838 bool isKnownDead(const Instruction *I) const override {
Johannes Doerfert86509e82020-01-12 00:34:38 -06002839 return isAssumedDead(I) && getKnown();
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002840 }
2841
2842 /// See AbstractAttribute::getAsStr().
2843 const std::string getAsStr() const override {
2844 return isAssumedDead() ? "assumed-dead" : "assumed-live";
2845 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002846
Johannes Doerfert86509e82020-01-12 00:34:38 -06002847 /// Check if all uses are assumed dead.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06002848 bool areAllUsesAssumedDead(Attributor &A, Value &V) {
2849 auto UsePred = [&](const Use &U, bool &Follow) { return false; };
2850 // Explicitly set the dependence class to required because we want a long
2851 // chain of N dependent instructions to be considered live as soon as one is
2852 // without going through N update cycles. This is not required for
2853 // correctness.
2854 return A.checkForAllUses(UsePred, *this, V, DepClassTy::REQUIRED);
Johannes Doerfert86509e82020-01-12 00:34:38 -06002855 }
2856
2857 /// Determine if \p I is assumed to be side-effect free.
2858 bool isAssumedSideEffectFree(Attributor &A, Instruction *I) {
2859 if (!I || wouldInstructionBeTriviallyDead(I))
2860 return true;
2861
2862 auto *CB = dyn_cast<CallBase>(I);
2863 if (!CB || isa<IntrinsicInst>(CB))
2864 return false;
2865
2866 const IRPosition &CallIRP = IRPosition::callsite_function(*CB);
2867 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(*this, CallIRP);
2868 if (!NoUnwindAA.isAssumedNoUnwind())
2869 return false;
2870
2871 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, CallIRP);
2872 if (!MemBehaviorAA.isAssumedReadOnly())
2873 return false;
2874
2875 return true;
2876 }
2877};
2878
2879struct AAIsDeadFloating : public AAIsDeadValueImpl {
2880 AAIsDeadFloating(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
2881
2882 /// See AbstractAttribute::initialize(...).
2883 void initialize(Attributor &A) override {
2884 if (isa<UndefValue>(getAssociatedValue())) {
2885 indicatePessimisticFixpoint();
2886 return;
2887 }
2888
2889 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
2890 if (!isAssumedSideEffectFree(A, I))
2891 indicatePessimisticFixpoint();
2892 }
2893
2894 /// See AbstractAttribute::updateImpl(...).
2895 ChangeStatus updateImpl(Attributor &A) override {
2896 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
2897 if (!isAssumedSideEffectFree(A, I))
2898 return indicatePessimisticFixpoint();
2899
Johannes Doerfert23f41f12020-01-12 01:09:22 -06002900 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002901 return indicatePessimisticFixpoint();
2902 return ChangeStatus::UNCHANGED;
2903 }
2904
2905 /// See AbstractAttribute::manifest(...).
2906 ChangeStatus manifest(Attributor &A) override {
2907 Value &V = getAssociatedValue();
Johannes Doerfert86509e82020-01-12 00:34:38 -06002908 if (auto *I = dyn_cast<Instruction>(&V)) {
2909 // If we get here we basically know the users are all dead. We check if
2910 // isAssumedSideEffectFree returns true here again because it might not be
2911 // the case and only the users are dead but the instruction (=call) is
2912 // still needed.
2913 if (isAssumedSideEffectFree(A, I) && !isa<InvokeInst>(I)) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05002914 A.deleteAfterManifest(*I);
2915 return ChangeStatus::CHANGED;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002916 }
Johannes Doerfert86509e82020-01-12 00:34:38 -06002917 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002918 if (V.use_empty())
2919 return ChangeStatus::UNCHANGED;
2920
Johannes Doerfertb2c76002020-01-23 17:12:56 -06002921 bool UsedAssumedInformation = false;
2922 Optional<ConstantInt *> CI =
2923 getAssumedConstant(A, V, *this, UsedAssumedInformation);
2924 if (CI.hasValue() && CI.getValue())
2925 return ChangeStatus::UNCHANGED;
2926
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002927 UndefValue &UV = *UndefValue::get(V.getType());
Hideto Ueno34fe8d02019-12-30 17:08:48 +09002928 bool AnyChange = A.changeValueAfterManifest(V, UV);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002929 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
2930 }
2931
2932 /// See AbstractAttribute::trackStatistics()
2933 void trackStatistics() const override {
2934 STATS_DECLTRACK_FLOATING_ATTR(IsDead)
2935 }
2936};
2937
2938struct AAIsDeadArgument : public AAIsDeadFloating {
2939 AAIsDeadArgument(const IRPosition &IRP) : AAIsDeadFloating(IRP) {}
2940
2941 /// See AbstractAttribute::initialize(...).
2942 void initialize(Attributor &A) override {
2943 if (!getAssociatedFunction()->hasExactDefinition())
2944 indicatePessimisticFixpoint();
2945 }
2946
Johannes Doerfert75133632019-10-10 01:39:16 -05002947 /// See AbstractAttribute::manifest(...).
2948 ChangeStatus manifest(Attributor &A) override {
2949 ChangeStatus Changed = AAIsDeadFloating::manifest(A);
2950 Argument &Arg = *getAssociatedArgument();
Johannes Doerfert89c2e732019-10-30 17:20:20 -05002951 if (A.isValidFunctionSignatureRewrite(Arg, /* ReplacementTypes */ {}))
Johannes Doerfert75133632019-10-10 01:39:16 -05002952 if (A.registerFunctionSignatureRewrite(
2953 Arg, /* ReplacementTypes */ {},
2954 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy{},
2955 Attributor::ArgumentReplacementInfo::ACSRepairCBTy{}))
2956 return ChangeStatus::CHANGED;
2957 return Changed;
2958 }
2959
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05002960 /// See AbstractAttribute::trackStatistics()
2961 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(IsDead) }
2962};
2963
2964struct AAIsDeadCallSiteArgument : public AAIsDeadValueImpl {
2965 AAIsDeadCallSiteArgument(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
2966
2967 /// See AbstractAttribute::initialize(...).
2968 void initialize(Attributor &A) override {
2969 if (isa<UndefValue>(getAssociatedValue()))
2970 indicatePessimisticFixpoint();
2971 }
2972
2973 /// See AbstractAttribute::updateImpl(...).
2974 ChangeStatus updateImpl(Attributor &A) override {
2975 // TODO: Once we have call site specific value information we can provide
2976 // call site specific liveness information and then it makes
2977 // sense to specialize attributes for call sites arguments instead of
2978 // redirecting requests to the callee argument.
2979 Argument *Arg = getAssociatedArgument();
2980 if (!Arg)
2981 return indicatePessimisticFixpoint();
2982 const IRPosition &ArgPos = IRPosition::argument(*Arg);
2983 auto &ArgAA = A.getAAFor<AAIsDead>(*this, ArgPos);
2984 return clampStateAndIndicateChange(
2985 getState(), static_cast<const AAIsDead::StateType &>(ArgAA.getState()));
2986 }
2987
2988 /// See AbstractAttribute::manifest(...).
2989 ChangeStatus manifest(Attributor &A) override {
2990 CallBase &CB = cast<CallBase>(getAnchorValue());
2991 Use &U = CB.getArgOperandUse(getArgNo());
2992 assert(!isa<UndefValue>(U.get()) &&
2993 "Expected undef values to be filtered out!");
2994 UndefValue &UV = *UndefValue::get(U->getType());
2995 if (A.changeUseAfterManifest(U, UV))
2996 return ChangeStatus::CHANGED;
2997 return ChangeStatus::UNCHANGED;
2998 }
2999
3000 /// See AbstractAttribute::trackStatistics()
3001 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(IsDead) }
3002};
3003
Johannes Doerfert86509e82020-01-12 00:34:38 -06003004struct AAIsDeadCallSiteReturned : public AAIsDeadFloating {
3005 AAIsDeadCallSiteReturned(const IRPosition &IRP)
3006 : AAIsDeadFloating(IRP), IsAssumedSideEffectFree(true) {}
3007
3008 /// See AAIsDead::isAssumedDead().
3009 bool isAssumedDead() const override {
3010 return AAIsDeadFloating::isAssumedDead() && IsAssumedSideEffectFree;
3011 }
3012
Johannes Doerfert86509e82020-01-12 00:34:38 -06003013 /// See AbstractAttribute::initialize(...).
3014 void initialize(Attributor &A) override {
3015 if (isa<UndefValue>(getAssociatedValue())) {
3016 indicatePessimisticFixpoint();
3017 return;
3018 }
3019
3020 // We track this separately as a secondary state.
3021 IsAssumedSideEffectFree = isAssumedSideEffectFree(A, getCtxI());
3022 }
3023
3024 /// See AbstractAttribute::updateImpl(...).
3025 ChangeStatus updateImpl(Attributor &A) override {
3026 ChangeStatus Changed = ChangeStatus::UNCHANGED;
3027 if (IsAssumedSideEffectFree && !isAssumedSideEffectFree(A, getCtxI())) {
3028 IsAssumedSideEffectFree = false;
3029 Changed = ChangeStatus::CHANGED;
3030 }
3031
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003032 if (!areAllUsesAssumedDead(A, getAssociatedValue()))
Johannes Doerfert86509e82020-01-12 00:34:38 -06003033 return indicatePessimisticFixpoint();
3034 return Changed;
3035 }
3036
3037 /// See AbstractAttribute::manifest(...).
3038 ChangeStatus manifest(Attributor &A) override {
3039 if (auto *CI = dyn_cast<CallInst>(&getAssociatedValue()))
3040 if (CI->isMustTailCall())
3041 return ChangeStatus::UNCHANGED;
3042 return AAIsDeadFloating::manifest(A);
3043 }
3044
3045 /// See AbstractAttribute::trackStatistics()
3046 void trackStatistics() const override {
3047 if (IsAssumedSideEffectFree)
3048 STATS_DECLTRACK_CSRET_ATTR(IsDead)
3049 else
3050 STATS_DECLTRACK_CSRET_ATTR(UnusedResult)
3051 }
3052
3053 /// See AbstractAttribute::getAsStr().
3054 const std::string getAsStr() const override {
3055 return isAssumedDead()
3056 ? "assumed-dead"
3057 : (getAssumed() ? "assumed-dead-users" : "assumed-live");
3058 }
3059
3060private:
3061 bool IsAssumedSideEffectFree;
3062};
3063
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003064struct AAIsDeadReturned : public AAIsDeadValueImpl {
3065 AAIsDeadReturned(const IRPosition &IRP) : AAIsDeadValueImpl(IRP) {}
3066
3067 /// See AbstractAttribute::updateImpl(...).
3068 ChangeStatus updateImpl(Attributor &A) override {
3069
Johannes Doerfertb70297a2020-02-14 10:34:31 -06003070 A.checkForAllInstructions([](Instruction &) { return true; }, *this,
3071 {Instruction::Ret});
3072
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003073 auto PredForCallSite = [&](AbstractCallSite ACS) {
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003074 if (ACS.isCallbackCall() || !ACS.getInstruction())
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003075 return false;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06003076 return areAllUsesAssumedDead(A, *ACS.getInstruction());
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003077 };
3078
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06003079 bool AllCallSitesKnown;
3080 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
3081 AllCallSitesKnown))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003082 return indicatePessimisticFixpoint();
3083
3084 return ChangeStatus::UNCHANGED;
3085 }
3086
3087 /// See AbstractAttribute::manifest(...).
3088 ChangeStatus manifest(Attributor &A) override {
3089 // TODO: Rewrite the signature to return void?
3090 bool AnyChange = false;
3091 UndefValue &UV = *UndefValue::get(getAssociatedFunction()->getReturnType());
3092 auto RetInstPred = [&](Instruction &I) {
3093 ReturnInst &RI = cast<ReturnInst>(I);
Johannes Doerfertb2c76002020-01-23 17:12:56 -06003094 if (auto *CI = dyn_cast<CallInst>(RI.getReturnValue()))
3095 if (CI->isMustTailCall())
3096 return true;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003097 if (!isa<UndefValue>(RI.getReturnValue()))
3098 AnyChange |= A.changeUseAfterManifest(RI.getOperandUse(0), UV);
3099 return true;
3100 };
3101 A.checkForAllInstructions(RetInstPred, *this, {Instruction::Ret});
3102 return AnyChange ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
3103 }
3104
3105 /// See AbstractAttribute::trackStatistics()
3106 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(IsDead) }
3107};
3108
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003109struct AAIsDeadFunction : public AAIsDead {
3110 AAIsDeadFunction(const IRPosition &IRP) : AAIsDead(IRP) {}
3111
3112 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003113 void initialize(Attributor &A) override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003114 const Function *F = getAssociatedFunction();
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003115 if (F && !F->isDeclaration()) {
3116 ToBeExploredFrom.insert(&F->getEntryBlock().front());
3117 assumeLive(A, F->getEntryBlock());
3118 }
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003119 }
3120
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003121 /// See AbstractAttribute::getAsStr().
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003122 const std::string getAsStr() const override {
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003123 return "Live[#BB " + std::to_string(AssumedLiveBlocks.size()) + "/" +
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003124 std::to_string(getAssociatedFunction()->size()) + "][#TBEP " +
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003125 std::to_string(ToBeExploredFrom.size()) + "][#KDE " +
3126 std::to_string(KnownDeadEnds.size()) + "]";
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003127 }
3128
3129 /// See AbstractAttribute::manifest(...).
3130 ChangeStatus manifest(Attributor &A) override {
3131 assert(getState().isValidState() &&
3132 "Attempted to manifest an invalid state!");
3133
3134 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003135 Function &F = *getAssociatedFunction();
3136
3137 if (AssumedLiveBlocks.empty()) {
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003138 A.deleteAfterManifest(F);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003139 return ChangeStatus::CHANGED;
3140 }
Johannes Doerfert924d2132019-08-05 21:34:45 +00003141
Johannes Doerfertbeb51502019-08-07 22:36:15 +00003142 // Flag to determine if we can change an invoke to a call assuming the
3143 // callee is nounwind. This is not possible if the personality of the
3144 // function allows to catch asynchronous exceptions.
Johannes Doerfert924d2132019-08-05 21:34:45 +00003145 bool Invoke2CallAllowed = !mayCatchAsynchronousExceptions(F);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003146
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003147 KnownDeadEnds.set_union(ToBeExploredFrom);
3148 for (const Instruction *DeadEndI : KnownDeadEnds) {
3149 auto *CB = dyn_cast<CallBase>(DeadEndI);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003150 if (!CB)
3151 continue;
3152 const auto &NoReturnAA =
3153 A.getAAFor<AANoReturn>(*this, IRPosition::callsite_function(*CB));
Johannes Doerfertc7ab19d2019-11-01 21:59:32 -05003154 bool MayReturn = !NoReturnAA.isAssumedNoReturn();
3155 if (MayReturn && (!Invoke2CallAllowed || !isa<InvokeInst>(CB)))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003156 continue;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003157
Johannes Doerferta4088c72020-01-07 16:01:57 -06003158 if (auto *II = dyn_cast<InvokeInst>(DeadEndI))
3159 A.registerInvokeWithDeadSuccessor(const_cast<InvokeInst &>(*II));
3160 else
3161 A.changeToUnreachableAfterManifest(
3162 const_cast<Instruction *>(DeadEndI->getNextNode()));
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003163 HasChanged = ChangeStatus::CHANGED;
3164 }
3165
Johannes Doerfertb19cd272019-09-03 20:42:16 +00003166 for (BasicBlock &BB : F)
3167 if (!AssumedLiveBlocks.count(&BB))
3168 A.deleteAfterManifest(BB);
3169
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003170 return HasChanged;
3171 }
3172
3173 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003174 ChangeStatus updateImpl(Attributor &A) override;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003175
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003176 /// See AbstractAttribute::trackStatistics()
3177 void trackStatistics() const override {}
3178
3179 /// Returns true if the function is assumed dead.
3180 bool isAssumedDead() const override { return false; }
3181
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06003182 /// See AAIsDead::isKnownDead().
3183 bool isKnownDead() const override { return false; }
3184
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003185 /// See AAIsDead::isAssumedDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003186 bool isAssumedDead(const BasicBlock *BB) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003187 assert(BB->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003188 "BB must be in the same anchor scope function.");
3189
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003190 if (!getAssumed())
3191 return false;
3192 return !AssumedLiveBlocks.count(BB);
3193 }
3194
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003195 /// See AAIsDead::isKnownDead(BasicBlock *).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003196 bool isKnownDead(const BasicBlock *BB) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003197 return getKnown() && isAssumedDead(BB);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003198 }
3199
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003200 /// See AAIsDead::isAssumed(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003201 bool isAssumedDead(const Instruction *I) const override {
Johannes Doerfert6dedc782019-08-16 21:31:11 +00003202 assert(I->getParent()->getParent() == getAssociatedFunction() &&
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003203 "Instruction must be in the same anchor scope function.");
3204
Stefan Stipanovic7849e412019-08-03 15:27:41 +00003205 if (!getAssumed())
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003206 return false;
3207
3208 // If it is not in AssumedLiveBlocks then it for sure dead.
3209 // Otherwise, it can still be after noreturn call in a live block.
3210 if (!AssumedLiveBlocks.count(I->getParent()))
3211 return true;
3212
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003213 // If it is not after a liveness barrier it is live.
3214 const Instruction *PrevI = I->getPrevNode();
3215 while (PrevI) {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003216 if (KnownDeadEnds.count(PrevI) || ToBeExploredFrom.count(PrevI))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003217 return true;
3218 PrevI = PrevI->getPrevNode();
3219 }
3220 return false;
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003221 }
3222
3223 /// See AAIsDead::isKnownDead(Instruction *I).
Johannes Doerfert4361da22019-08-04 18:38:53 +00003224 bool isKnownDead(const Instruction *I) const override {
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003225 return getKnown() && isAssumedDead(I);
3226 }
3227
Johannes Doerfert924d2132019-08-05 21:34:45 +00003228 /// Determine if \p F might catch asynchronous exceptions.
3229 static bool mayCatchAsynchronousExceptions(const Function &F) {
3230 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
3231 }
3232
Johannes Doerfert2f622062019-09-04 16:35:20 +00003233 /// Assume \p BB is (partially) live now and indicate to the Attributor \p A
3234 /// that internal function called from \p BB should now be looked at.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003235 bool assumeLive(Attributor &A, const BasicBlock &BB) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00003236 if (!AssumedLiveBlocks.insert(&BB).second)
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003237 return false;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003238
3239 // We assume that all of BB is (probably) live now and if there are calls to
3240 // internal functions we will assume that those are now live as well. This
3241 // is a performance optimization for blocks with calls to a lot of internal
3242 // functions. It can however cause dead functions to be treated as live.
3243 for (const Instruction &I : BB)
3244 if (ImmutableCallSite ICS = ImmutableCallSite(&I))
3245 if (const Function *F = ICS.getCalledFunction())
Johannes Doerfert766f2cc2019-10-07 23:21:52 +00003246 if (F->hasLocalLinkage())
Johannes Doerfert2f622062019-09-04 16:35:20 +00003247 A.markLiveInternalFunction(*F);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003248 return true;
Johannes Doerfert2f622062019-09-04 16:35:20 +00003249 }
3250
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003251 /// Collection of instructions that need to be explored again, e.g., we
3252 /// did assume they do not transfer control to (one of their) successors.
3253 SmallSetVector<const Instruction *, 8> ToBeExploredFrom;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003254
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003255 /// Collection of instructions that are known to not transfer control.
3256 SmallSetVector<const Instruction *, 8> KnownDeadEnds;
3257
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003258 /// Collection of all assumed live BasicBlocks.
Johannes Doerfert4361da22019-08-04 18:38:53 +00003259 DenseSet<const BasicBlock *> AssumedLiveBlocks;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003260};
3261
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003262static bool
3263identifyAliveSuccessors(Attributor &A, const CallBase &CB,
3264 AbstractAttribute &AA,
3265 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3266 const IRPosition &IPos = IRPosition::callsite_function(CB);
3267
3268 const auto &NoReturnAA = A.getAAFor<AANoReturn>(AA, IPos);
3269 if (NoReturnAA.isAssumedNoReturn())
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003270 return !NoReturnAA.isKnownNoReturn();
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003271 if (CB.isTerminator())
3272 AliveSuccessors.push_back(&CB.getSuccessor(0)->front());
3273 else
3274 AliveSuccessors.push_back(CB.getNextNode());
Stefan Stipanovicd0216172019-08-02 21:31:22 +00003275 return false;
3276}
3277
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003278static bool
3279identifyAliveSuccessors(Attributor &A, const InvokeInst &II,
3280 AbstractAttribute &AA,
3281 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3282 bool UsedAssumedInformation =
3283 identifyAliveSuccessors(A, cast<CallBase>(II), AA, AliveSuccessors);
Johannes Doerfert924d2132019-08-05 21:34:45 +00003284
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003285 // First, determine if we can change an invoke to a call assuming the
3286 // callee is nounwind. This is not possible if the personality of the
3287 // function allows to catch asynchronous exceptions.
3288 if (AAIsDeadFunction::mayCatchAsynchronousExceptions(*II.getFunction())) {
3289 AliveSuccessors.push_back(&II.getUnwindDest()->front());
3290 } else {
3291 const IRPosition &IPos = IRPosition::callsite_function(II);
3292 const auto &AANoUnw = A.getAAFor<AANoUnwind>(AA, IPos);
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003293 if (AANoUnw.isAssumedNoUnwind()) {
3294 UsedAssumedInformation |= !AANoUnw.isKnownNoUnwind();
3295 } else {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003296 AliveSuccessors.push_back(&II.getUnwindDest()->front());
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003297 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003298 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003299 return UsedAssumedInformation;
3300}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003301
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003302static bool
3303identifyAliveSuccessors(Attributor &A, const BranchInst &BI,
3304 AbstractAttribute &AA,
3305 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
3306 bool UsedAssumedInformation = false;
3307 if (BI.getNumSuccessors() == 1) {
3308 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3309 } else {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003310 Optional<ConstantInt *> CI =
3311 getAssumedConstant(A, *BI.getCondition(), AA, UsedAssumedInformation);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003312 if (!CI.hasValue()) {
3313 // No value yet, assume both edges are dead.
3314 } else if (CI.getValue()) {
3315 const BasicBlock *SuccBB =
3316 BI.getSuccessor(1 - CI.getValue()->getZExtValue());
3317 AliveSuccessors.push_back(&SuccBB->front());
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003318 } else {
3319 AliveSuccessors.push_back(&BI.getSuccessor(0)->front());
3320 AliveSuccessors.push_back(&BI.getSuccessor(1)->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003321 UsedAssumedInformation = false;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003322 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003323 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003324 return UsedAssumedInformation;
3325}
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003326
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003327static bool
3328identifyAliveSuccessors(Attributor &A, const SwitchInst &SI,
3329 AbstractAttribute &AA,
3330 SmallVectorImpl<const Instruction *> &AliveSuccessors) {
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003331 bool UsedAssumedInformation = false;
3332 Optional<ConstantInt *> CI =
3333 getAssumedConstant(A, *SI.getCondition(), AA, UsedAssumedInformation);
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003334 if (!CI.hasValue()) {
3335 // No value yet, assume all edges are dead.
3336 } else if (CI.getValue()) {
3337 for (auto &CaseIt : SI.cases()) {
3338 if (CaseIt.getCaseValue() == CI.getValue()) {
3339 AliveSuccessors.push_back(&CaseIt.getCaseSuccessor()->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003340 return UsedAssumedInformation;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003341 }
3342 }
Johannes Doerferted47a9c2019-11-01 13:57:49 -05003343 AliveSuccessors.push_back(&SI.getDefaultDest()->front());
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003344 return UsedAssumedInformation;
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003345 } else {
3346 for (const BasicBlock *SuccBB : successors(SI.getParent()))
3347 AliveSuccessors.push_back(&SuccBB->front());
3348 }
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003349 return UsedAssumedInformation;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003350}
3351
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003352ChangeStatus AAIsDeadFunction::updateImpl(Attributor &A) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003353 ChangeStatus Change = ChangeStatus::UNCHANGED;
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00003354
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003355 LLVM_DEBUG(dbgs() << "[AAIsDead] Live [" << AssumedLiveBlocks.size() << "/"
3356 << getAssociatedFunction()->size() << "] BBs and "
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003357 << ToBeExploredFrom.size() << " exploration points and "
3358 << KnownDeadEnds.size() << " known dead ends\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003359
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003360 // Copy and clear the list of instructions we need to explore from. It is
3361 // refilled with instructions the next update has to look at.
3362 SmallVector<const Instruction *, 8> Worklist(ToBeExploredFrom.begin(),
3363 ToBeExploredFrom.end());
3364 decltype(ToBeExploredFrom) NewToBeExploredFrom;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003365
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003366 SmallVector<const Instruction *, 8> AliveSuccessors;
3367 while (!Worklist.empty()) {
3368 const Instruction *I = Worklist.pop_back_val();
3369 LLVM_DEBUG(dbgs() << "[AAIsDead] Exploration inst: " << *I << "\n");
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003370
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003371 AliveSuccessors.clear();
3372
3373 bool UsedAssumedInformation = false;
3374 switch (I->getOpcode()) {
3375 // TODO: look for (assumed) UB to backwards propagate "deadness".
3376 default:
3377 if (I->isTerminator()) {
3378 for (const BasicBlock *SuccBB : successors(I->getParent()))
3379 AliveSuccessors.push_back(&SuccBB->front());
3380 } else {
3381 AliveSuccessors.push_back(I->getNextNode());
3382 }
3383 break;
3384 case Instruction::Call:
3385 UsedAssumedInformation = identifyAliveSuccessors(A, cast<CallInst>(*I),
3386 *this, AliveSuccessors);
3387 break;
3388 case Instruction::Invoke:
3389 UsedAssumedInformation = identifyAliveSuccessors(A, cast<InvokeInst>(*I),
3390 *this, AliveSuccessors);
3391 break;
3392 case Instruction::Br:
3393 UsedAssumedInformation = identifyAliveSuccessors(A, cast<BranchInst>(*I),
3394 *this, AliveSuccessors);
3395 break;
3396 case Instruction::Switch:
3397 UsedAssumedInformation = identifyAliveSuccessors(A, cast<SwitchInst>(*I),
3398 *this, AliveSuccessors);
3399 break;
Johannes Doerfert4361da22019-08-04 18:38:53 +00003400 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003401
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003402 if (UsedAssumedInformation) {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003403 NewToBeExploredFrom.insert(I);
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003404 } else {
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003405 Change = ChangeStatus::CHANGED;
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003406 if (AliveSuccessors.empty() ||
3407 (I->isTerminator() && AliveSuccessors.size() < I->getNumSuccessors()))
3408 KnownDeadEnds.insert(I);
3409 }
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003410
3411 LLVM_DEBUG(dbgs() << "[AAIsDead] #AliveSuccessors: "
3412 << AliveSuccessors.size() << " UsedAssumedInformation: "
3413 << UsedAssumedInformation << "\n");
3414
3415 for (const Instruction *AliveSuccessor : AliveSuccessors) {
3416 if (!I->isTerminator()) {
3417 assert(AliveSuccessors.size() == 1 &&
3418 "Non-terminator expected to have a single successor!");
3419 Worklist.push_back(AliveSuccessor);
3420 } else {
3421 if (assumeLive(A, *AliveSuccessor->getParent()))
3422 Worklist.push_back(AliveSuccessor);
3423 }
Johannes Doerfert4361da22019-08-04 18:38:53 +00003424 }
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003425 }
3426
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003427 ToBeExploredFrom = std::move(NewToBeExploredFrom);
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003428
Johannes Doerfertd6207812019-08-07 22:32:38 +00003429 // If we know everything is live there is no need to query for liveness.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003430 // Instead, indicating a pessimistic fixpoint will cause the state to be
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003431 // "invalid" and all queries to be answered conservatively without lookups.
3432 // To be in this state we have to (1) finished the exploration and (3) not
3433 // discovered any non-trivial dead end and (2) not ruled unreachable code
3434 // dead.
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003435 if (ToBeExploredFrom.empty() &&
Johannes Doerfert3cbe3312019-11-01 21:04:54 -05003436 getAssociatedFunction()->size() == AssumedLiveBlocks.size() &&
3437 llvm::all_of(KnownDeadEnds, [](const Instruction *DeadEndI) {
3438 return DeadEndI->isTerminator() && DeadEndI->getNumSuccessors() == 0;
3439 }))
Johannes Doerfertdac2d402019-10-29 11:47:47 -05003440 return indicatePessimisticFixpoint();
3441 return Change;
Stefan Stipanovic6058b862019-07-22 23:58:23 +00003442}
3443
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003444/// Liveness information for a call sites.
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05003445struct AAIsDeadCallSite final : AAIsDeadFunction {
3446 AAIsDeadCallSite(const IRPosition &IRP) : AAIsDeadFunction(IRP) {}
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003447
3448 /// See AbstractAttribute::initialize(...).
3449 void initialize(Attributor &A) override {
3450 // TODO: Once we have call site specific value information we can provide
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003451 // call site specific liveness information and then it makes
Johannes Doerfert07a5c122019-08-28 14:09:14 +00003452 // sense to specialize attributes for call sites instead of
3453 // redirecting requests to the callee.
3454 llvm_unreachable("Abstract attributes for liveness are not "
3455 "supported for call sites yet!");
3456 }
3457
3458 /// See AbstractAttribute::updateImpl(...).
3459 ChangeStatus updateImpl(Attributor &A) override {
3460 return indicatePessimisticFixpoint();
3461 }
3462
3463 /// See AbstractAttribute::trackStatistics()
3464 void trackStatistics() const override {}
3465};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003466
Hideto Ueno19c07af2019-07-23 08:16:17 +00003467/// -------------------- Dereferenceable Argument Attribute --------------------
3468
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003469template <>
3470ChangeStatus clampStateAndIndicateChange<DerefState>(DerefState &S,
3471 const DerefState &R) {
Johannes Doerfert31784242019-10-29 23:18:49 -05003472 ChangeStatus CS0 =
3473 clampStateAndIndicateChange(S.DerefBytesState, R.DerefBytesState);
3474 ChangeStatus CS1 = clampStateAndIndicateChange(S.GlobalState, R.GlobalState);
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003475 return CS0 | CS1;
3476}
3477
Hideto Ueno70576ca2019-08-22 14:18:29 +00003478struct AADereferenceableImpl : AADereferenceable {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003479 AADereferenceableImpl(const IRPosition &IRP) : AADereferenceable(IRP) {}
Johannes Doerfert344d0382019-08-07 22:34:26 +00003480 using StateType = DerefState;
Hideto Ueno19c07af2019-07-23 08:16:17 +00003481
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00003482 void initialize(Attributor &A) override {
3483 SmallVector<Attribute, 4> Attrs;
3484 getAttrs({Attribute::Dereferenceable, Attribute::DereferenceableOrNull},
3485 Attrs);
3486 for (const Attribute &Attr : Attrs)
3487 takeKnownDerefBytesMaximum(Attr.getValueAsInt());
3488
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06003489 NonNullAA = &A.getAAFor<AANonNull>(*this, getIRPosition(),
3490 /* TrackDependence */ false);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003491
3492 const IRPosition &IRP = this->getIRPosition();
3493 bool IsFnInterface = IRP.isFnInterfaceKind();
3494 const Function *FnScope = IRP.getAnchorScope();
3495 if (IsFnInterface && (!FnScope || !FnScope->hasExactDefinition()))
3496 indicatePessimisticFixpoint();
Johannes Doerfert6a1274a2019-08-14 21:31:32 +00003497 }
3498
Hideto Ueno19c07af2019-07-23 08:16:17 +00003499 /// See AbstractAttribute::getState()
3500 /// {
Johannes Doerfert344d0382019-08-07 22:34:26 +00003501 StateType &getState() override { return *this; }
3502 const StateType &getState() const override { return *this; }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003503 /// }
3504
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003505 /// Helper function for collecting accessed bytes in must-be-executed-context
3506 void addAccessedBytesForUse(Attributor &A, const Use *U,
3507 const Instruction *I) {
3508 const Value *UseV = U->get();
3509 if (!UseV->getType()->isPointerTy())
3510 return;
3511
3512 Type *PtrTy = UseV->getType();
3513 const DataLayout &DL = A.getDataLayout();
3514 int64_t Offset;
3515 if (const Value *Base = getBasePointerOfAccessPointerOperand(
3516 I, Offset, DL, /*AllowNonInbounds*/ true)) {
Hideto Uenoef4febd2019-12-29 17:34:08 +09003517 if (Base == &getAssociatedValue() &&
Johannes Doerfertb6dbd0f2020-01-26 02:49:58 -06003518 getPointerOperand(I, /* AllowVolatile */ false) == UseV) {
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003519 uint64_t Size = DL.getTypeStoreSize(PtrTy->getPointerElementType());
3520 addAccessedBytes(Offset, Size);
3521 }
3522 }
3523 return;
3524 }
3525
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003526 /// See AAFromMustBeExecutedContext
3527 bool followUse(Attributor &A, const Use *U, const Instruction *I) {
3528 bool IsNonNull = false;
3529 bool TrackUse = false;
3530 int64_t DerefBytes = getKnownNonNullAndDerefBytesForUse(
3531 A, *this, getAssociatedValue(), U, I, IsNonNull, TrackUse);
Hideto Ueno6c742fd2019-11-29 06:55:58 +00003532
3533 addAccessedBytesForUse(A, U, I);
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003534 takeKnownDerefBytesMaximum(DerefBytes);
3535 return TrackUse;
3536 }
3537
Hideto Uenodfedae52019-11-29 06:45:07 +00003538 /// See AbstractAttribute::manifest(...).
3539 ChangeStatus manifest(Attributor &A) override {
3540 ChangeStatus Change = AADereferenceable::manifest(A);
3541 if (isAssumedNonNull() && hasAttr(Attribute::DereferenceableOrNull)) {
3542 removeAttrs({Attribute::DereferenceableOrNull});
3543 return ChangeStatus::CHANGED;
3544 }
3545 return Change;
3546 }
3547
Johannes Doerferteccdf082019-08-05 23:35:12 +00003548 void getDeducedAttributes(LLVMContext &Ctx,
3549 SmallVectorImpl<Attribute> &Attrs) const override {
Hideto Ueno19c07af2019-07-23 08:16:17 +00003550 // TODO: Add *_globally support
3551 if (isAssumedNonNull())
3552 Attrs.emplace_back(Attribute::getWithDereferenceableBytes(
3553 Ctx, getAssumedDereferenceableBytes()));
3554 else
3555 Attrs.emplace_back(Attribute::getWithDereferenceableOrNullBytes(
3556 Ctx, getAssumedDereferenceableBytes()));
3557 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003558
3559 /// See AbstractAttribute::getAsStr().
3560 const std::string getAsStr() const override {
3561 if (!getAssumedDereferenceableBytes())
3562 return "unknown-dereferenceable";
3563 return std::string("dereferenceable") +
3564 (isAssumedNonNull() ? "" : "_or_null") +
3565 (isAssumedGlobal() ? "_globally" : "") + "<" +
3566 std::to_string(getKnownDereferenceableBytes()) + "-" +
3567 std::to_string(getAssumedDereferenceableBytes()) + ">";
3568 }
3569};
3570
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003571/// Dereferenceable attribute for a floating value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003572struct AADereferenceableFloating
3573 : AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl> {
3574 using Base =
3575 AAFromMustBeExecutedContext<AADereferenceable, AADereferenceableImpl>;
3576 AADereferenceableFloating(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno19c07af2019-07-23 08:16:17 +00003577
3578 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003579 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003580 ChangeStatus Change = Base::updateImpl(A);
3581
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003582 const DataLayout &DL = A.getDataLayout();
3583
3584 auto VisitValueCB = [&](Value &V, DerefState &T, bool Stripped) -> bool {
3585 unsigned IdxWidth =
3586 DL.getIndexSizeInBits(V.getType()->getPointerAddressSpace());
3587 APInt Offset(IdxWidth, 0);
3588 const Value *Base =
3589 V.stripAndAccumulateInBoundsConstantOffsets(DL, Offset);
3590
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003591 const auto &AA =
3592 A.getAAFor<AADereferenceable>(*this, IRPosition::value(*Base));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003593 int64_t DerefBytes = 0;
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003594 if (!Stripped && this == &AA) {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003595 // Use IR information if we did not strip anything.
3596 // TODO: track globally.
3597 bool CanBeNull;
3598 DerefBytes = Base->getPointerDereferenceableBytes(DL, CanBeNull);
3599 T.GlobalState.indicatePessimisticFixpoint();
3600 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003601 const DerefState &DS = static_cast<const DerefState &>(AA.getState());
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003602 DerefBytes = DS.DerefBytesState.getAssumed();
3603 T.GlobalState &= DS.GlobalState;
3604 }
3605
Hideto Ueno188f9a32020-01-15 15:25:52 +09003606 // TODO: Use `AAConstantRange` to infer dereferenceable bytes.
3607
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00003608 // For now we do not try to "increase" dereferenceability due to negative
3609 // indices as we first have to come up with code to deal with loops and
3610 // for overflows of the dereferenceable bytes.
Johannes Doerfert785fad32019-08-23 17:29:23 +00003611 int64_t OffsetSExt = Offset.getSExtValue();
3612 if (OffsetSExt < 0)
Johannes Doerfertdb6efb02019-10-13 20:40:10 +00003613 OffsetSExt = 0;
Johannes Doerfert2f2d7c32019-08-23 15:45:46 +00003614
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003615 T.takeAssumedDerefBytesMinimum(
Johannes Doerfert785fad32019-08-23 17:29:23 +00003616 std::max(int64_t(0), DerefBytes - OffsetSExt));
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003617
Johannes Doerfert785fad32019-08-23 17:29:23 +00003618 if (this == &AA) {
3619 if (!Stripped) {
3620 // If nothing was stripped IR information is all we got.
3621 T.takeKnownDerefBytesMaximum(
3622 std::max(int64_t(0), DerefBytes - OffsetSExt));
3623 T.indicatePessimisticFixpoint();
3624 } else if (OffsetSExt > 0) {
3625 // If something was stripped but there is circular reasoning we look
3626 // for the offset. If it is positive we basically decrease the
3627 // dereferenceable bytes in a circluar loop now, which will simply
3628 // drive them down to the known value in a very slow way which we
3629 // can accelerate.
3630 T.indicatePessimisticFixpoint();
3631 }
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003632 }
3633
3634 return T.isValidState();
3635 };
3636
3637 DerefState T;
3638 if (!genericValueTraversal<AADereferenceable, DerefState>(
3639 A, getIRPosition(), *this, T, VisitValueCB))
3640 return indicatePessimisticFixpoint();
3641
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003642 return Change | clampStateAndIndicateChange(getState(), T);
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003643 }
3644
3645 /// See AbstractAttribute::trackStatistics()
3646 void trackStatistics() const override {
3647 STATS_DECLTRACK_FLOATING_ATTR(dereferenceable)
3648 }
3649};
3650
3651/// Dereferenceable attribute for a return value.
3652struct AADereferenceableReturned final
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003653 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl> {
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003654 AADereferenceableReturned(const IRPosition &IRP)
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003655 : AAReturnedFromReturnedValues<AADereferenceable, AADereferenceableImpl>(
3656 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003657
3658 /// See AbstractAttribute::trackStatistics()
3659 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003660 STATS_DECLTRACK_FNRET_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003661 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003662};
3663
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003664/// Dereferenceable attribute for an argument
3665struct AADereferenceableArgument final
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003666 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003667 AADereferenceable, AADereferenceableImpl> {
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003668 using Base = AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<
Johannes Doerfert791c9f12020-01-29 18:02:42 -06003669 AADereferenceable, AADereferenceableImpl>;
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003670 AADereferenceableArgument(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003671
3672 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003673 void trackStatistics() const override {
Johannes Doerfert169af992019-08-20 06:09:56 +00003674 STATS_DECLTRACK_ARG_ATTR(dereferenceable)
3675 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003676};
3677
Hideto Ueno19c07af2019-07-23 08:16:17 +00003678/// Dereferenceable attribute for a call site argument.
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003679struct AADereferenceableCallSiteArgument final : AADereferenceableFloating {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003680 AADereferenceableCallSiteArgument(const IRPosition &IRP)
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003681 : AADereferenceableFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003682
3683 /// See AbstractAttribute::trackStatistics()
3684 void trackStatistics() const override {
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003685 STATS_DECLTRACK_CSARG_ATTR(dereferenceable)
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003686 }
Hideto Ueno19c07af2019-07-23 08:16:17 +00003687};
3688
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003689/// Dereferenceable attribute deduction for a call site return value.
Hideto Ueno96e6ce42019-10-08 15:25:56 +00003690struct AADereferenceableCallSiteReturned final
3691 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3692 AADereferenceable, AADereferenceableImpl> {
3693 using Base = AACallSiteReturnedFromReturnedAndMustBeExecutedContext<
3694 AADereferenceable, AADereferenceableImpl>;
3695 AADereferenceableCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003696
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003697 /// See AbstractAttribute::trackStatistics()
3698 void trackStatistics() const override {
3699 STATS_DECLTRACK_CS_ATTR(dereferenceable);
3700 }
3701};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003702
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003703// ------------------------ Align Argument Attribute ------------------------
3704
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003705static unsigned int getKnownAlignForUse(Attributor &A,
3706 AbstractAttribute &QueryingAA,
3707 Value &AssociatedValue, const Use *U,
3708 const Instruction *I, bool &TrackUse) {
Hideto Ueno78a75022019-11-26 07:51:59 +00003709 // We need to follow common pointer manipulation uses to the accesses they
3710 // feed into.
3711 if (isa<CastInst>(I)) {
Johannes Doerfertc90681b2020-01-02 16:41:17 -06003712 // Follow all but ptr2int casts.
3713 TrackUse = !isa<PtrToIntInst>(I);
Hideto Ueno78a75022019-11-26 07:51:59 +00003714 return 0;
3715 }
3716 if (auto *GEP = dyn_cast<GetElementPtrInst>(I)) {
3717 if (GEP->hasAllConstantIndices()) {
3718 TrackUse = true;
3719 return 0;
3720 }
3721 }
3722
3723 unsigned Alignment = 0;
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003724 if (ImmutableCallSite ICS = ImmutableCallSite(I)) {
3725 if (ICS.isBundleOperand(U) || ICS.isCallee(U))
3726 return 0;
3727
3728 unsigned ArgNo = ICS.getArgumentNo(U);
3729 IRPosition IRP = IRPosition::callsite_argument(ICS, ArgNo);
3730 // As long as we only use known information there is no need to track
3731 // dependences here.
3732 auto &AlignAA = A.getAAFor<AAAlign>(QueryingAA, IRP,
3733 /* TrackDependence */ false);
Hideto Ueno78a75022019-11-26 07:51:59 +00003734 Alignment = AlignAA.getKnownAlign();
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003735 }
3736
Hideto Ueno78a75022019-11-26 07:51:59 +00003737 const Value *UseV = U->get();
Johannes Doerfert30ae8592020-01-10 12:13:10 -06003738 if (auto *SI = dyn_cast<StoreInst>(I)) {
3739 if (SI->getPointerOperand() == UseV)
3740 Alignment = SI->getAlignment();
3741 } else if (auto *LI = dyn_cast<LoadInst>(I))
Hideto Ueno78a75022019-11-26 07:51:59 +00003742 Alignment = LI->getAlignment();
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003743
Hideto Ueno78a75022019-11-26 07:51:59 +00003744 if (Alignment <= 1)
3745 return 0;
3746
3747 auto &DL = A.getDataLayout();
3748 int64_t Offset;
3749
3750 if (const Value *Base = GetPointerBaseWithConstantOffset(UseV, Offset, DL)) {
3751 if (Base == &AssociatedValue) {
3752 // BasePointerAddr + Offset = Alignment * Q for some integer Q.
3753 // So we can say that the maximum power of two which is a divisor of
3754 // gcd(Offset, Alignment) is an alignment.
3755
3756 uint32_t gcd =
3757 greatestCommonDivisor(uint32_t(abs((int32_t)Offset)), Alignment);
3758 Alignment = llvm::PowerOf2Floor(gcd);
3759 }
3760 }
3761
3762 return Alignment;
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003763}
Johannes Doerfert344d0382019-08-07 22:34:26 +00003764struct AAAlignImpl : AAAlign {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003765 AAAlignImpl(const IRPosition &IRP) : AAAlign(IRP) {}
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003766
Johannes Doerfert234eda52019-08-16 19:51:23 +00003767 /// See AbstractAttribute::initialize(...).
Johannes Doerfertece81902019-08-12 22:05:53 +00003768 void initialize(Attributor &A) override {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003769 SmallVector<Attribute, 4> Attrs;
3770 getAttrs({Attribute::Alignment}, Attrs);
3771 for (const Attribute &Attr : Attrs)
3772 takeKnownMaximum(Attr.getValueAsInt());
Johannes Doerfert97fd5822019-09-04 16:26:20 +00003773
3774 if (getIRPosition().isFnInterfaceKind() &&
3775 (!getAssociatedFunction() ||
3776 !getAssociatedFunction()->hasExactDefinition()))
3777 indicatePessimisticFixpoint();
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003778 }
3779
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003780 /// See AbstractAttribute::manifest(...).
3781 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert30179d72020-01-12 00:25:45 -06003782 ChangeStatus LoadStoreChanged = ChangeStatus::UNCHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003783
3784 // Check for users that allow alignment annotations.
3785 Value &AnchorVal = getIRPosition().getAnchorValue();
3786 for (const Use &U : AnchorVal.uses()) {
3787 if (auto *SI = dyn_cast<StoreInst>(U.getUser())) {
3788 if (SI->getPointerOperand() == &AnchorVal)
3789 if (SI->getAlignment() < getAssumedAlign()) {
3790 STATS_DECLTRACK(AAAlign, Store,
James Hendersond68904f2020-01-06 10:15:44 +00003791 "Number of times alignment added to a store");
Guillaume Chateletd400d452019-10-03 13:17:21 +00003792 SI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert30179d72020-01-12 00:25:45 -06003793 LoadStoreChanged = ChangeStatus::CHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003794 }
3795 } else if (auto *LI = dyn_cast<LoadInst>(U.getUser())) {
3796 if (LI->getPointerOperand() == &AnchorVal)
3797 if (LI->getAlignment() < getAssumedAlign()) {
Guillaume Chatelet17380222019-09-30 09:37:05 +00003798 LI->setAlignment(Align(getAssumedAlign()));
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003799 STATS_DECLTRACK(AAAlign, Load,
James Hendersond68904f2020-01-06 10:15:44 +00003800 "Number of times alignment added to a load");
Johannes Doerfert30179d72020-01-12 00:25:45 -06003801 LoadStoreChanged = ChangeStatus::CHANGED;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003802 }
3803 }
3804 }
3805
Johannes Doerfert30179d72020-01-12 00:25:45 -06003806 ChangeStatus Changed = AAAlign::manifest(A);
3807
3808 MaybeAlign InheritAlign =
3809 getAssociatedValue().getPointerAlignment(A.getDataLayout());
3810 if (InheritAlign.valueOrOne() >= getAssumedAlign())
3811 return LoadStoreChanged;
3812 return Changed | LoadStoreChanged;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003813 }
3814
Johannes Doerfert81df4522019-08-30 15:22:28 +00003815 // TODO: Provide a helper to determine the implied ABI alignment and check in
3816 // the existing manifest method and a new one for AAAlignImpl that value
3817 // to avoid making the alignment explicit if it did not improve.
3818
3819 /// See AbstractAttribute::getDeducedAttributes
3820 virtual void
3821 getDeducedAttributes(LLVMContext &Ctx,
3822 SmallVectorImpl<Attribute> &Attrs) const override {
3823 if (getAssumedAlign() > 1)
Guillaume Chateletb65fa482019-10-15 12:56:24 +00003824 Attrs.emplace_back(
3825 Attribute::getWithAlignment(Ctx, Align(getAssumedAlign())));
Johannes Doerfert81df4522019-08-30 15:22:28 +00003826 }
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003827 /// See AAFromMustBeExecutedContext
3828 bool followUse(Attributor &A, const Use *U, const Instruction *I) {
3829 bool TrackUse = false;
3830
Hideto Ueno4ecf2552019-12-12 13:42:40 +00003831 unsigned int KnownAlign =
3832 getKnownAlignForUse(A, *this, getAssociatedValue(), U, I, TrackUse);
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003833 takeKnownMaximum(KnownAlign);
3834
3835 return TrackUse;
3836 }
Johannes Doerfert81df4522019-08-30 15:22:28 +00003837
3838 /// See AbstractAttribute::getAsStr().
3839 const std::string getAsStr() const override {
3840 return getAssumedAlign() ? ("align<" + std::to_string(getKnownAlign()) +
3841 "-" + std::to_string(getAssumedAlign()) + ">")
3842 : "unknown-align";
3843 }
3844};
3845
3846/// Align attribute for a floating value.
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003847struct AAAlignFloating : AAFromMustBeExecutedContext<AAAlign, AAAlignImpl> {
3848 using Base = AAFromMustBeExecutedContext<AAAlign, AAAlignImpl>;
3849 AAAlignFloating(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert81df4522019-08-30 15:22:28 +00003850
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003851 /// See AbstractAttribute::updateImpl(...).
Johannes Doerfert234eda52019-08-16 19:51:23 +00003852 ChangeStatus updateImpl(Attributor &A) override {
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003853 Base::updateImpl(A);
3854
Johannes Doerfert234eda52019-08-16 19:51:23 +00003855 const DataLayout &DL = A.getDataLayout();
3856
Johannes Doerfertb9b87912019-08-20 06:02:39 +00003857 auto VisitValueCB = [&](Value &V, AAAlign::StateType &T,
3858 bool Stripped) -> bool {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003859 const auto &AA = A.getAAFor<AAAlign>(*this, IRPosition::value(V));
3860 if (!Stripped && this == &AA) {
Johannes Doerfert234eda52019-08-16 19:51:23 +00003861 // Use only IR information if we did not strip anything.
Guillaume Chateletbae629b2019-10-15 13:58:22 +00003862 const MaybeAlign PA = V.getPointerAlignment(DL);
3863 T.takeKnownMaximum(PA ? PA->value() : 0);
Johannes Doerfert234eda52019-08-16 19:51:23 +00003864 T.indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00003865 } else {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003866 // Use abstract attribute information.
3867 const AAAlign::StateType &DS =
3868 static_cast<const AAAlign::StateType &>(AA.getState());
3869 T ^= DS;
Johannes Doerfert234eda52019-08-16 19:51:23 +00003870 }
Johannes Doerfertb9b87912019-08-20 06:02:39 +00003871 return T.isValidState();
Johannes Doerfert234eda52019-08-16 19:51:23 +00003872 };
3873
3874 StateType T;
3875 if (!genericValueTraversal<AAAlign, StateType>(A, getIRPosition(), *this, T,
3876 VisitValueCB))
Johannes Doerfertcfcca1a2019-08-20 06:08:35 +00003877 return indicatePessimisticFixpoint();
Johannes Doerfert234eda52019-08-16 19:51:23 +00003878
Johannes Doerfert028b2aa2019-08-20 05:57:01 +00003879 // TODO: If we know we visited all incoming values, thus no are assumed
3880 // dead, we can take the known information from the state T.
Johannes Doerfert234eda52019-08-16 19:51:23 +00003881 return clampStateAndIndicateChange(getState(), T);
3882 }
3883
3884 /// See AbstractAttribute::trackStatistics()
3885 void trackStatistics() const override { STATS_DECLTRACK_FLOATING_ATTR(align) }
3886};
3887
3888/// Align attribute for function return value.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003889struct AAAlignReturned final
3890 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00003891 AAAlignReturned(const IRPosition &IRP)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003892 : AAReturnedFromReturnedValues<AAAlign, AAAlignImpl>(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003893
3894 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003895 void trackStatistics() const override { STATS_DECLTRACK_FNRET_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003896};
3897
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003898/// Align attribute for function argument.
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00003899struct AAAlignArgument final
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003900 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
3901 AAAlignImpl> {
Johannes Doerfert234eda52019-08-16 19:51:23 +00003902 AAAlignArgument(const IRPosition &IRP)
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003903 : AAArgumentFromCallSiteArgumentsAndMustBeExecutedContext<AAAlign,
3904 AAAlignImpl>(
3905 IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003906
3907 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert169af992019-08-20 06:09:56 +00003908 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003909};
3910
Johannes Doerfert234eda52019-08-16 19:51:23 +00003911struct AAAlignCallSiteArgument final : AAAlignFloating {
3912 AAAlignCallSiteArgument(const IRPosition &IRP) : AAAlignFloating(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003913
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003914 /// See AbstractAttribute::manifest(...).
3915 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert30179d72020-01-12 00:25:45 -06003916 ChangeStatus Changed = AAAlignImpl::manifest(A);
3917 MaybeAlign InheritAlign =
3918 getAssociatedValue().getPointerAlignment(A.getDataLayout());
3919 if (InheritAlign.valueOrOne() >= getAssumedAlign())
3920 Changed = ChangeStatus::UNCHANGED;
3921 return Changed;
Johannes Doerfert5a5a1392019-08-23 20:20:10 +00003922 }
3923
Johannes Doerfertdada8132019-12-31 01:27:50 -06003924 /// See AbstractAttribute::updateImpl(Attributor &A).
3925 ChangeStatus updateImpl(Attributor &A) override {
3926 ChangeStatus Changed = AAAlignFloating::updateImpl(A);
3927 if (Argument *Arg = getAssociatedArgument()) {
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06003928 // We only take known information from the argument
3929 // so we do not need to track a dependence.
Johannes Doerfertdada8132019-12-31 01:27:50 -06003930 const auto &ArgAlignAA = A.getAAFor<AAAlign>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06003931 *this, IRPosition::argument(*Arg), /* TrackDependence */ false);
Johannes Doerfertdada8132019-12-31 01:27:50 -06003932 takeKnownMaximum(ArgAlignAA.getKnownAlign());
3933 }
3934 return Changed;
3935 }
3936
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003937 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003938 void trackStatistics() const override { STATS_DECLTRACK_CSARG_ATTR(aligned) }
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00003939};
3940
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003941/// Align attribute deduction for a call site return value.
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003942struct AAAlignCallSiteReturned final
3943 : AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
3944 AAAlignImpl> {
3945 using Base =
3946 AACallSiteReturnedFromReturnedAndMustBeExecutedContext<AAAlign,
3947 AAAlignImpl>;
3948 AAAlignCallSiteReturned(const IRPosition &IRP) : Base(IRP) {}
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003949
3950 /// See AbstractAttribute::initialize(...).
3951 void initialize(Attributor &A) override {
Hideto Ueno88b04ef2019-11-12 06:36:49 +00003952 Base::initialize(A);
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003953 Function *F = getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00003954 if (!F)
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003955 indicatePessimisticFixpoint();
3956 }
3957
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003958 /// See AbstractAttribute::trackStatistics()
3959 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(align); }
3960};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003961
Johannes Doerferte83f3032019-08-05 23:22:05 +00003962/// ------------------ Function No-Return Attribute ----------------------------
Johannes Doerfert344d0382019-08-07 22:34:26 +00003963struct AANoReturnImpl : public AANoReturn {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003964 AANoReturnImpl(const IRPosition &IRP) : AANoReturn(IRP) {}
Johannes Doerferte83f3032019-08-05 23:22:05 +00003965
Johannes Doerfert0cc2b612019-10-13 21:25:53 +00003966 /// See AbstractAttribute::initialize(...).
3967 void initialize(Attributor &A) override {
3968 AANoReturn::initialize(A);
3969 Function *F = getAssociatedFunction();
Johannes Doerfert1b6041a2019-11-01 20:17:48 -05003970 if (!F)
Johannes Doerfert0cc2b612019-10-13 21:25:53 +00003971 indicatePessimisticFixpoint();
3972 }
3973
Johannes Doerferte83f3032019-08-05 23:22:05 +00003974 /// See AbstractAttribute::getAsStr().
3975 const std::string getAsStr() const override {
3976 return getAssumed() ? "noreturn" : "may-return";
3977 }
3978
Johannes Doerferte83f3032019-08-05 23:22:05 +00003979 /// See AbstractAttribute::updateImpl(Attributor &A).
Johannes Doerfertece81902019-08-12 22:05:53 +00003980 virtual ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003981 auto CheckForNoReturn = [](Instruction &) { return false; };
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003982 if (!A.checkForAllInstructions(CheckForNoReturn, *this,
Johannes Doerfertd0f64002019-08-06 00:32:43 +00003983 {(unsigned)Instruction::Ret}))
Johannes Doerferte83f3032019-08-05 23:22:05 +00003984 return indicatePessimisticFixpoint();
Johannes Doerferte83f3032019-08-05 23:22:05 +00003985 return ChangeStatus::UNCHANGED;
3986 }
3987};
3988
Johannes Doerfertfb69f762019-08-05 23:32:31 +00003989struct AANoReturnFunction final : AANoReturnImpl {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00003990 AANoReturnFunction(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00003991
3992 /// See AbstractAttribute::trackStatistics()
Johannes Doerfert17b578b2019-08-14 21:46:25 +00003993 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(noreturn) }
Johannes Doerfertfb69f762019-08-05 23:32:31 +00003994};
3995
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00003996/// NoReturn attribute deduction for a call sites.
Johannes Doerfert3fac6682019-08-30 15:24:52 +00003997struct AANoReturnCallSite final : AANoReturnImpl {
3998 AANoReturnCallSite(const IRPosition &IRP) : AANoReturnImpl(IRP) {}
3999
Johannes Doerfert3fac6682019-08-30 15:24:52 +00004000 /// See AbstractAttribute::updateImpl(...).
4001 ChangeStatus updateImpl(Attributor &A) override {
4002 // TODO: Once we have call site specific value information we can provide
4003 // call site specific liveness information and then it makes
4004 // sense to specialize attributes for call sites arguments instead of
4005 // redirecting requests to the callee argument.
4006 Function *F = getAssociatedFunction();
4007 const IRPosition &FnPos = IRPosition::function(*F);
4008 auto &FnAA = A.getAAFor<AANoReturn>(*this, FnPos);
4009 return clampStateAndIndicateChange(
4010 getState(),
4011 static_cast<const AANoReturn::StateType &>(FnAA.getState()));
4012 }
4013
4014 /// See AbstractAttribute::trackStatistics()
4015 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(noreturn); }
4016};
Johannes Doerfert66cf87e2019-08-16 19:49:00 +00004017
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004018/// ----------------------- Variable Capturing ---------------------------------
4019
4020/// A class to hold the state of for no-capture attributes.
4021struct AANoCaptureImpl : public AANoCapture {
4022 AANoCaptureImpl(const IRPosition &IRP) : AANoCapture(IRP) {}
4023
4024 /// See AbstractAttribute::initialize(...).
4025 void initialize(Attributor &A) override {
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05004026 if (hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ true)) {
4027 indicateOptimisticFixpoint();
4028 return;
4029 }
4030 Function *AnchorScope = getAnchorScope();
4031 if (isFnInterfaceKind() &&
4032 (!AnchorScope || !AnchorScope->hasExactDefinition())) {
4033 indicatePessimisticFixpoint();
4034 return;
4035 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004036
Johannes Doerfert72adda12019-10-10 05:33:21 +00004037 // You cannot "capture" null in the default address space.
4038 if (isa<ConstantPointerNull>(getAssociatedValue()) &&
4039 getAssociatedValue().getType()->getPointerAddressSpace() == 0) {
4040 indicateOptimisticFixpoint();
4041 return;
4042 }
4043
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05004044 const Function *F = getArgNo() >= 0 ? getAssociatedFunction() : AnchorScope;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004045
4046 // Check what state the associated function can actually capture.
4047 if (F)
Johannes Doerfert0437bfc2019-10-31 20:03:13 -05004048 determineFunctionCaptureCapabilities(getIRPosition(), *F, *this);
Johannes Doerfertb0412e42019-09-04 16:16:13 +00004049 else
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004050 indicatePessimisticFixpoint();
4051 }
4052
4053 /// See AbstractAttribute::updateImpl(...).
4054 ChangeStatus updateImpl(Attributor &A) override;
4055
4056 /// see AbstractAttribute::isAssumedNoCaptureMaybeReturned(...).
4057 virtual void
4058 getDeducedAttributes(LLVMContext &Ctx,
4059 SmallVectorImpl<Attribute> &Attrs) const override {
4060 if (!isAssumedNoCaptureMaybeReturned())
4061 return;
4062
Hideto Ueno37367642019-09-11 06:52:11 +00004063 if (getArgNo() >= 0) {
4064 if (isAssumedNoCapture())
4065 Attrs.emplace_back(Attribute::get(Ctx, Attribute::NoCapture));
4066 else if (ManifestInternal)
4067 Attrs.emplace_back(Attribute::get(Ctx, "no-capture-maybe-returned"));
4068 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004069 }
4070
4071 /// Set the NOT_CAPTURED_IN_MEM and NOT_CAPTURED_IN_RET bits in \p Known
4072 /// depending on the ability of the function associated with \p IRP to capture
4073 /// state in memory and through "returning/throwing", respectively.
Johannes Doerfert3839b572019-10-21 00:48:42 +00004074 static void determineFunctionCaptureCapabilities(const IRPosition &IRP,
4075 const Function &F,
Johannes Doerfert1a746452019-10-20 22:28:49 -05004076 BitIntegerState &State) {
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004077 // TODO: Once we have memory behavior attributes we should use them here.
4078
4079 // If we know we cannot communicate or write to memory, we do not care about
4080 // ptr2int anymore.
4081 if (F.onlyReadsMemory() && F.doesNotThrow() &&
4082 F.getReturnType()->isVoidTy()) {
4083 State.addKnownBits(NO_CAPTURE);
4084 return;
4085 }
4086
4087 // A function cannot capture state in memory if it only reads memory, it can
4088 // however return/throw state and the state might be influenced by the
4089 // pointer value, e.g., loading from a returned pointer might reveal a bit.
4090 if (F.onlyReadsMemory())
4091 State.addKnownBits(NOT_CAPTURED_IN_MEM);
4092
4093 // A function cannot communicate state back if it does not through
4094 // exceptions and doesn not return values.
4095 if (F.doesNotThrow() && F.getReturnType()->isVoidTy())
4096 State.addKnownBits(NOT_CAPTURED_IN_RET);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004097
4098 // Check existing "returned" attributes.
4099 int ArgNo = IRP.getArgNo();
4100 if (F.doesNotThrow() && ArgNo >= 0) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01004101 for (unsigned u = 0, e = F.arg_size(); u < e; ++u)
Johannes Doerfert3839b572019-10-21 00:48:42 +00004102 if (F.hasParamAttribute(u, Attribute::Returned)) {
Johannes Doerfert9d5ad5e2019-10-21 01:29:10 +00004103 if (u == unsigned(ArgNo))
Johannes Doerfert3839b572019-10-21 00:48:42 +00004104 State.removeAssumedBits(NOT_CAPTURED_IN_RET);
4105 else if (F.onlyReadsMemory())
4106 State.addKnownBits(NO_CAPTURE);
4107 else
4108 State.addKnownBits(NOT_CAPTURED_IN_RET);
4109 break;
4110 }
4111 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004112 }
4113
4114 /// See AbstractState::getAsStr().
4115 const std::string getAsStr() const override {
4116 if (isKnownNoCapture())
4117 return "known not-captured";
4118 if (isAssumedNoCapture())
4119 return "assumed not-captured";
4120 if (isKnownNoCaptureMaybeReturned())
4121 return "known not-captured-maybe-returned";
4122 if (isAssumedNoCaptureMaybeReturned())
4123 return "assumed not-captured-maybe-returned";
4124 return "assumed-captured";
4125 }
4126};
4127
4128/// Attributor-aware capture tracker.
4129struct AACaptureUseTracker final : public CaptureTracker {
4130
4131 /// Create a capture tracker that can lookup in-flight abstract attributes
4132 /// through the Attributor \p A.
4133 ///
4134 /// If a use leads to a potential capture, \p CapturedInMemory is set and the
4135 /// search is stopped. If a use leads to a return instruction,
4136 /// \p CommunicatedBack is set to true and \p CapturedInMemory is not changed.
4137 /// If a use leads to a ptr2int which may capture the value,
4138 /// \p CapturedInInteger is set. If a use is found that is currently assumed
4139 /// "no-capture-maybe-returned", the user is added to the \p PotentialCopies
4140 /// set. All values in \p PotentialCopies are later tracked as well. For every
4141 /// explored use we decrement \p RemainingUsesToExplore. Once it reaches 0,
4142 /// the search is stopped with \p CapturedInMemory and \p CapturedInInteger
4143 /// conservatively set to true.
4144 AACaptureUseTracker(Attributor &A, AANoCapture &NoCaptureAA,
Johannes Doerfert31784242019-10-29 23:18:49 -05004145 const AAIsDead &IsDeadAA, AANoCapture::StateType &State,
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004146 SmallVectorImpl<const Value *> &PotentialCopies,
4147 unsigned &RemainingUsesToExplore)
4148 : A(A), NoCaptureAA(NoCaptureAA), IsDeadAA(IsDeadAA), State(State),
4149 PotentialCopies(PotentialCopies),
4150 RemainingUsesToExplore(RemainingUsesToExplore) {}
4151
4152 /// Determine if \p V maybe captured. *Also updates the state!*
4153 bool valueMayBeCaptured(const Value *V) {
4154 if (V->getType()->isPointerTy()) {
4155 PointerMayBeCaptured(V, this);
4156 } else {
4157 State.indicatePessimisticFixpoint();
4158 }
4159 return State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4160 }
4161
4162 /// See CaptureTracker::tooManyUses().
4163 void tooManyUses() override {
4164 State.removeAssumedBits(AANoCapture::NO_CAPTURE);
4165 }
4166
4167 bool isDereferenceableOrNull(Value *O, const DataLayout &DL) override {
4168 if (CaptureTracker::isDereferenceableOrNull(O, DL))
4169 return true;
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004170 const auto &DerefAA = A.getAAFor<AADereferenceable>(
4171 NoCaptureAA, IRPosition::value(*O), /* TrackDependence */ true,
4172 DepClassTy::OPTIONAL);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004173 return DerefAA.getAssumedDereferenceableBytes();
4174 }
4175
4176 /// See CaptureTracker::captured(...).
4177 bool captured(const Use *U) override {
4178 Instruction *UInst = cast<Instruction>(U->getUser());
4179 LLVM_DEBUG(dbgs() << "Check use: " << *U->get() << " in " << *UInst
4180 << "\n");
4181
4182 // Because we may reuse the tracker multiple times we keep track of the
4183 // number of explored uses ourselves as well.
4184 if (RemainingUsesToExplore-- == 0) {
4185 LLVM_DEBUG(dbgs() << " - too many uses to explore!\n");
4186 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4187 /* Return */ true);
4188 }
4189
4190 // Deal with ptr2int by following uses.
4191 if (isa<PtrToIntInst>(UInst)) {
4192 LLVM_DEBUG(dbgs() << " - ptr2int assume the worst!\n");
4193 return valueMayBeCaptured(UInst);
4194 }
4195
4196 // Explicitly catch return instructions.
4197 if (isa<ReturnInst>(UInst))
4198 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4199 /* Return */ true);
4200
4201 // For now we only use special logic for call sites. However, the tracker
4202 // itself knows about a lot of other non-capturing cases already.
4203 CallSite CS(UInst);
4204 if (!CS || !CS.isArgOperand(U))
4205 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4206 /* Return */ true);
4207
4208 unsigned ArgNo = CS.getArgumentNo(U);
4209 const IRPosition &CSArgPos = IRPosition::callsite_argument(CS, ArgNo);
4210 // If we have a abstract no-capture attribute for the argument we can use
4211 // it to justify a non-capture attribute here. This allows recursion!
4212 auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(NoCaptureAA, CSArgPos);
4213 if (ArgNoCaptureAA.isAssumedNoCapture())
4214 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4215 /* Return */ false);
4216 if (ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
4217 addPotentialCopy(CS);
4218 return isCapturedIn(/* Memory */ false, /* Integer */ false,
4219 /* Return */ false);
4220 }
4221
4222 // Lastly, we could not find a reason no-capture can be assumed so we don't.
4223 return isCapturedIn(/* Memory */ true, /* Integer */ true,
4224 /* Return */ true);
4225 }
4226
4227 /// Register \p CS as potential copy of the value we are checking.
4228 void addPotentialCopy(CallSite CS) {
4229 PotentialCopies.push_back(CS.getInstruction());
4230 }
4231
4232 /// See CaptureTracker::shouldExplore(...).
4233 bool shouldExplore(const Use *U) override {
Johannes Doerfert23f41f12020-01-12 01:09:22 -06004234 // Check liveness.
4235 return !A.isAssumedDead(*U, &NoCaptureAA, &IsDeadAA);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004236 }
4237
4238 /// Update the state according to \p CapturedInMem, \p CapturedInInt, and
4239 /// \p CapturedInRet, then return the appropriate value for use in the
4240 /// CaptureTracker::captured() interface.
4241 bool isCapturedIn(bool CapturedInMem, bool CapturedInInt,
4242 bool CapturedInRet) {
4243 LLVM_DEBUG(dbgs() << " - captures [Mem " << CapturedInMem << "|Int "
4244 << CapturedInInt << "|Ret " << CapturedInRet << "]\n");
4245 if (CapturedInMem)
4246 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_MEM);
4247 if (CapturedInInt)
4248 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_INT);
4249 if (CapturedInRet)
4250 State.removeAssumedBits(AANoCapture::NOT_CAPTURED_IN_RET);
4251 return !State.isAssumed(AANoCapture::NO_CAPTURE_MAYBE_RETURNED);
4252 }
4253
4254private:
4255 /// The attributor providing in-flight abstract attributes.
4256 Attributor &A;
4257
4258 /// The abstract attribute currently updated.
4259 AANoCapture &NoCaptureAA;
4260
4261 /// The abstract liveness state.
4262 const AAIsDead &IsDeadAA;
4263
4264 /// The state currently updated.
Johannes Doerfert31784242019-10-29 23:18:49 -05004265 AANoCapture::StateType &State;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004266
4267 /// Set of potential copies of the tracked value.
4268 SmallVectorImpl<const Value *> &PotentialCopies;
4269
4270 /// Global counter to limit the number of explored uses.
4271 unsigned &RemainingUsesToExplore;
4272};
4273
4274ChangeStatus AANoCaptureImpl::updateImpl(Attributor &A) {
4275 const IRPosition &IRP = getIRPosition();
4276 const Value *V =
4277 getArgNo() >= 0 ? IRP.getAssociatedArgument() : &IRP.getAssociatedValue();
4278 if (!V)
4279 return indicatePessimisticFixpoint();
4280
4281 const Function *F =
4282 getArgNo() >= 0 ? IRP.getAssociatedFunction() : IRP.getAnchorScope();
4283 assert(F && "Expected a function!");
Johannes Doerfert3839b572019-10-21 00:48:42 +00004284 const IRPosition &FnPos = IRPosition::function(*F);
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004285 const auto &IsDeadAA =
4286 A.getAAFor<AAIsDead>(*this, FnPos, /* TrackDependence */ false);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004287
4288 AANoCapture::StateType T;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004289
Johannes Doerfert3839b572019-10-21 00:48:42 +00004290 // Readonly means we cannot capture through memory.
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004291 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
4292 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004293 if (FnMemAA.isAssumedReadOnly()) {
4294 T.addKnownBits(NOT_CAPTURED_IN_MEM);
4295 if (FnMemAA.isKnownReadOnly())
4296 addKnownBits(NOT_CAPTURED_IN_MEM);
4297 }
4298
4299 // Make sure all returned values are different than the underlying value.
4300 // TODO: we could do this in a more sophisticated way inside
4301 // AAReturnedValues, e.g., track all values that escape through returns
4302 // directly somehow.
4303 auto CheckReturnedArgs = [&](const AAReturnedValues &RVAA) {
4304 bool SeenConstant = false;
4305 for (auto &It : RVAA.returned_values()) {
4306 if (isa<Constant>(It.first)) {
4307 if (SeenConstant)
4308 return false;
4309 SeenConstant = true;
4310 } else if (!isa<Argument>(It.first) ||
4311 It.first == getAssociatedArgument())
4312 return false;
4313 }
4314 return true;
4315 };
4316
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004317 const auto &NoUnwindAA = A.getAAFor<AANoUnwind>(
4318 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004319 if (NoUnwindAA.isAssumedNoUnwind()) {
4320 bool IsVoidTy = F->getReturnType()->isVoidTy();
4321 const AAReturnedValues *RVAA =
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06004322 IsVoidTy ? nullptr
4323 : &A.getAAFor<AAReturnedValues>(*this, FnPos,
4324 /* TrackDependence */ true,
4325 DepClassTy::OPTIONAL);
Johannes Doerfert3839b572019-10-21 00:48:42 +00004326 if (IsVoidTy || CheckReturnedArgs(*RVAA)) {
4327 T.addKnownBits(NOT_CAPTURED_IN_RET);
4328 if (T.isKnown(NOT_CAPTURED_IN_MEM))
4329 return ChangeStatus::UNCHANGED;
4330 if (NoUnwindAA.isKnownNoUnwind() &&
4331 (IsVoidTy || RVAA->getState().isAtFixpoint())) {
4332 addKnownBits(NOT_CAPTURED_IN_RET);
4333 if (isKnown(NOT_CAPTURED_IN_MEM))
4334 return indicateOptimisticFixpoint();
4335 }
4336 }
4337 }
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004338
4339 // Use the CaptureTracker interface and logic with the specialized tracker,
4340 // defined in AACaptureUseTracker, that can look at in-flight abstract
4341 // attributes and directly updates the assumed state.
4342 SmallVector<const Value *, 4> PotentialCopies;
4343 unsigned RemainingUsesToExplore = DefaultMaxUsesToExplore;
4344 AACaptureUseTracker Tracker(A, *this, IsDeadAA, T, PotentialCopies,
4345 RemainingUsesToExplore);
4346
4347 // Check all potential copies of the associated value until we can assume
4348 // none will be captured or we have to assume at least one might be.
4349 unsigned Idx = 0;
4350 PotentialCopies.push_back(V);
4351 while (T.isAssumed(NO_CAPTURE_MAYBE_RETURNED) && Idx < PotentialCopies.size())
4352 Tracker.valueMayBeCaptured(PotentialCopies[Idx++]);
4353
Johannes Doerfert1a746452019-10-20 22:28:49 -05004354 AANoCapture::StateType &S = getState();
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004355 auto Assumed = S.getAssumed();
4356 S.intersectAssumedBits(T.getAssumed());
Johannes Doerfert31784242019-10-29 23:18:49 -05004357 if (!isAssumedNoCaptureMaybeReturned())
4358 return indicatePessimisticFixpoint();
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004359 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
4360 : ChangeStatus::CHANGED;
4361}
4362
4363/// NoCapture attribute for function arguments.
4364struct AANoCaptureArgument final : AANoCaptureImpl {
4365 AANoCaptureArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4366
4367 /// See AbstractAttribute::trackStatistics()
4368 void trackStatistics() const override { STATS_DECLTRACK_ARG_ATTR(nocapture) }
4369};
4370
4371/// NoCapture attribute for call site arguments.
4372struct AANoCaptureCallSiteArgument final : AANoCaptureImpl {
4373 AANoCaptureCallSiteArgument(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4374
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06004375 /// See AbstractAttribute::initialize(...).
4376 void initialize(Attributor &A) override {
4377 if (Argument *Arg = getAssociatedArgument())
4378 if (Arg->hasByValAttr())
4379 indicateOptimisticFixpoint();
4380 AANoCaptureImpl::initialize(A);
4381 }
4382
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00004383 /// See AbstractAttribute::updateImpl(...).
4384 ChangeStatus updateImpl(Attributor &A) override {
4385 // TODO: Once we have call site specific value information we can provide
4386 // call site specific liveness information and then it makes
4387 // sense to specialize attributes for call sites arguments instead of
4388 // redirecting requests to the callee argument.
4389 Argument *Arg = getAssociatedArgument();
4390 if (!Arg)
4391 return indicatePessimisticFixpoint();
4392 const IRPosition &ArgPos = IRPosition::argument(*Arg);
4393 auto &ArgAA = A.getAAFor<AANoCapture>(*this, ArgPos);
4394 return clampStateAndIndicateChange(
4395 getState(),
4396 static_cast<const AANoCapture::StateType &>(ArgAA.getState()));
4397 }
4398
4399 /// See AbstractAttribute::trackStatistics()
4400 void trackStatistics() const override{STATS_DECLTRACK_CSARG_ATTR(nocapture)};
4401};
4402
4403/// NoCapture attribute for floating values.
4404struct AANoCaptureFloating final : AANoCaptureImpl {
4405 AANoCaptureFloating(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4406
4407 /// See AbstractAttribute::trackStatistics()
4408 void trackStatistics() const override {
4409 STATS_DECLTRACK_FLOATING_ATTR(nocapture)
4410 }
4411};
4412
4413/// NoCapture attribute for function return value.
4414struct AANoCaptureReturned final : AANoCaptureImpl {
4415 AANoCaptureReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {
4416 llvm_unreachable("NoCapture is not applicable to function returns!");
4417 }
4418
4419 /// See AbstractAttribute::initialize(...).
4420 void initialize(Attributor &A) override {
4421 llvm_unreachable("NoCapture is not applicable to function returns!");
4422 }
4423
4424 /// See AbstractAttribute::updateImpl(...).
4425 ChangeStatus updateImpl(Attributor &A) override {
4426 llvm_unreachable("NoCapture is not applicable to function returns!");
4427 }
4428
4429 /// See AbstractAttribute::trackStatistics()
4430 void trackStatistics() const override {}
4431};
4432
4433/// NoCapture attribute deduction for a call site return value.
4434struct AANoCaptureCallSiteReturned final : AANoCaptureImpl {
4435 AANoCaptureCallSiteReturned(const IRPosition &IRP) : AANoCaptureImpl(IRP) {}
4436
4437 /// See AbstractAttribute::trackStatistics()
4438 void trackStatistics() const override {
4439 STATS_DECLTRACK_CSRET_ATTR(nocapture)
4440 }
4441};
4442
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004443/// ------------------ Value Simplify Attribute ----------------------------
4444struct AAValueSimplifyImpl : AAValueSimplify {
4445 AAValueSimplifyImpl(const IRPosition &IRP) : AAValueSimplify(IRP) {}
4446
Johannes Doerfert9dcf8892020-01-11 23:59:36 -06004447 /// See AbstractAttribute::initialize(...).
4448 void initialize(Attributor &A) override {
4449 if (getAssociatedValue().getType()->isVoidTy())
4450 indicatePessimisticFixpoint();
4451 }
4452
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004453 /// See AbstractAttribute::getAsStr().
4454 const std::string getAsStr() const override {
4455 return getAssumed() ? (getKnown() ? "simplified" : "maybe-simple")
4456 : "not-simple";
4457 }
4458
4459 /// See AbstractAttribute::trackStatistics()
4460 void trackStatistics() const override {}
4461
4462 /// See AAValueSimplify::getAssumedSimplifiedValue()
4463 Optional<Value *> getAssumedSimplifiedValue(Attributor &A) const override {
4464 if (!getAssumed())
4465 return const_cast<Value *>(&getAssociatedValue());
4466 return SimplifiedAssociatedValue;
4467 }
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004468
4469 /// Helper function for querying AAValueSimplify and updating candicate.
4470 /// \param QueryingValue Value trying to unify with SimplifiedValue
4471 /// \param AccumulatedSimplifiedValue Current simplification result.
4472 static bool checkAndUpdate(Attributor &A, const AbstractAttribute &QueryingAA,
4473 Value &QueryingValue,
4474 Optional<Value *> &AccumulatedSimplifiedValue) {
4475 // FIXME: Add a typecast support.
4476
Johannes Doerfertd07b5a52020-01-12 00:00:33 -06004477 auto &ValueSimplifyAA = A.getAAFor<AAValueSimplify>(
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004478 QueryingAA, IRPosition::value(QueryingValue));
4479
4480 Optional<Value *> QueryingValueSimplified =
Johannes Doerfertd07b5a52020-01-12 00:00:33 -06004481 ValueSimplifyAA.getAssumedSimplifiedValue(A);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004482
4483 if (!QueryingValueSimplified.hasValue())
4484 return true;
4485
4486 if (!QueryingValueSimplified.getValue())
4487 return false;
4488
4489 Value &QueryingValueSimplifiedUnwrapped =
4490 *QueryingValueSimplified.getValue();
4491
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004492 if (AccumulatedSimplifiedValue.hasValue() &&
4493 !isa<UndefValue>(AccumulatedSimplifiedValue.getValue()) &&
4494 !isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004495 return AccumulatedSimplifiedValue == QueryingValueSimplified;
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004496 if (AccumulatedSimplifiedValue.hasValue() &&
4497 isa<UndefValue>(QueryingValueSimplifiedUnwrapped))
4498 return true;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004499
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004500 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << QueryingValue
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004501 << " is assumed to be "
4502 << QueryingValueSimplifiedUnwrapped << "\n");
4503
4504 AccumulatedSimplifiedValue = QueryingValueSimplified;
4505 return true;
4506 }
4507
Hideto Ueno188f9a32020-01-15 15:25:52 +09004508 bool askSimplifiedValueForAAValueConstantRange(Attributor &A) {
4509 if (!getAssociatedValue().getType()->isIntegerTy())
4510 return false;
4511
4512 const auto &ValueConstantRangeAA =
4513 A.getAAFor<AAValueConstantRange>(*this, getIRPosition());
4514
4515 Optional<ConstantInt *> COpt =
4516 ValueConstantRangeAA.getAssumedConstantInt(A);
4517 if (COpt.hasValue()) {
4518 if (auto *C = COpt.getValue())
4519 SimplifiedAssociatedValue = C;
4520 else
4521 return false;
4522 } else {
Johannes Doerfert63adbb92020-02-09 20:21:56 -06004523 SimplifiedAssociatedValue = llvm::None;
Hideto Ueno188f9a32020-01-15 15:25:52 +09004524 }
4525 return true;
4526 }
4527
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004528 /// See AbstractAttribute::manifest(...).
4529 ChangeStatus manifest(Attributor &A) override {
4530 ChangeStatus Changed = ChangeStatus::UNCHANGED;
4531
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004532 if (SimplifiedAssociatedValue.hasValue() &&
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004533 !SimplifiedAssociatedValue.getValue())
4534 return Changed;
4535
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004536 Value &V = getAssociatedValue();
4537 auto *C = SimplifiedAssociatedValue.hasValue()
4538 ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())
4539 : UndefValue::get(V.getType());
4540 if (C) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004541 // We can replace the AssociatedValue with the constant.
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004542 if (!V.user_empty() && &V != C && V.getType() == C->getType()) {
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004543 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C
4544 << " :: " << *this << "\n");
Johannes Doerfert4c62a352020-01-12 00:17:08 -06004545 if (A.changeValueAfterManifest(V, *C))
4546 Changed = ChangeStatus::CHANGED;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004547 }
4548 }
4549
4550 return Changed | AAValueSimplify::manifest(A);
4551 }
4552
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004553 /// See AbstractState::indicatePessimisticFixpoint(...).
4554 ChangeStatus indicatePessimisticFixpoint() override {
4555 // NOTE: Associated value will be returned in a pessimistic fixpoint and is
4556 // regarded as known. That's why`indicateOptimisticFixpoint` is called.
4557 SimplifiedAssociatedValue = &getAssociatedValue();
Johannes Doerferta4b35882019-12-31 13:25:47 -06004558 indicateOptimisticFixpoint();
4559 return ChangeStatus::CHANGED;
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004560 }
4561
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004562protected:
4563 // An assumed simplified value. Initially, it is set to Optional::None, which
4564 // means that the value is not clear under current assumption. If in the
4565 // pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4566 // returns orignal associated value.
4567 Optional<Value *> SimplifiedAssociatedValue;
4568};
4569
4570struct AAValueSimplifyArgument final : AAValueSimplifyImpl {
4571 AAValueSimplifyArgument(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4572
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004573 void initialize(Attributor &A) override {
4574 AAValueSimplifyImpl::initialize(A);
4575 if (!getAssociatedFunction() || getAssociatedFunction()->isDeclaration())
4576 indicatePessimisticFixpoint();
4577 if (hasAttr({Attribute::InAlloca, Attribute::StructRet, Attribute::Nest},
4578 /* IgnoreSubsumingPositions */ true))
4579 indicatePessimisticFixpoint();
4580 }
4581
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004582 /// See AbstractAttribute::updateImpl(...).
4583 ChangeStatus updateImpl(Attributor &A) override {
Johannes Doerfert15cd90a2019-11-01 13:42:54 -05004584 // Byval is only replacable if it is readonly otherwise we would write into
4585 // the replaced value and not the copy that byval creates implicitly.
4586 Argument *Arg = getAssociatedArgument();
4587 if (Arg->hasByValAttr()) {
4588 const auto &MemAA = A.getAAFor<AAMemoryBehavior>(*this, getIRPosition());
4589 if (!MemAA.isAssumedReadOnly())
4590 return indicatePessimisticFixpoint();
4591 }
4592
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004593 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4594
Johannes Doerfert661db042019-10-07 23:14:58 +00004595 auto PredForCallSite = [&](AbstractCallSite ACS) {
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004596 const IRPosition &ACSArgPos =
4597 IRPosition::callsite_argument(ACS, getArgNo());
4598 // Check if a coresponding argument was found or if it is on not
4599 // associated (which can happen for callback calls).
4600 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
Hideto Ueno4ecf2552019-12-12 13:42:40 +00004601 return false;
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004602
Johannes Doerferte360ee62019-11-01 18:45:25 -05004603 // We can only propagate thread independent values through callbacks.
4604 // This is different to direct/indirect call sites because for them we
4605 // know the thread executing the caller and callee is the same. For
4606 // callbacks this is not guaranteed, thus a thread dependent value could
4607 // be different for the caller and callee, making it invalid to propagate.
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004608 Value &ArgOp = ACSArgPos.getAssociatedValue();
Johannes Doerferte360ee62019-11-01 18:45:25 -05004609 if (ACS.isCallbackCall())
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004610 if (auto *C = dyn_cast<Constant>(&ArgOp))
Johannes Doerferte360ee62019-11-01 18:45:25 -05004611 if (C->isThreadDependent())
4612 return false;
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06004613 return checkAndUpdate(A, *this, ArgOp, SimplifiedAssociatedValue);
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004614 };
4615
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06004616 bool AllCallSitesKnown;
4617 if (!A.checkForAllCallSites(PredForCallSite, *this, true,
4618 AllCallSitesKnown))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004619 if (!askSimplifiedValueForAAValueConstantRange(A))
4620 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004621
4622 // If a candicate was found in this update, return CHANGED.
4623 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4624 ? ChangeStatus::UNCHANGED
4625 : ChangeStatus ::CHANGED;
4626 }
4627
4628 /// See AbstractAttribute::trackStatistics()
4629 void trackStatistics() const override {
4630 STATS_DECLTRACK_ARG_ATTR(value_simplify)
4631 }
4632};
4633
4634struct AAValueSimplifyReturned : AAValueSimplifyImpl {
4635 AAValueSimplifyReturned(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4636
4637 /// See AbstractAttribute::updateImpl(...).
4638 ChangeStatus updateImpl(Attributor &A) override {
4639 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4640
4641 auto PredForReturned = [&](Value &V) {
4642 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4643 };
4644
4645 if (!A.checkForAllReturnedValues(PredForReturned, *this))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004646 if (!askSimplifiedValueForAAValueConstantRange(A))
4647 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004648
4649 // If a candicate was found in this update, return CHANGED.
4650 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4651 ? ChangeStatus::UNCHANGED
4652 : ChangeStatus ::CHANGED;
4653 }
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004654
4655 ChangeStatus manifest(Attributor &A) override {
4656 ChangeStatus Changed = ChangeStatus::UNCHANGED;
4657
4658 if (SimplifiedAssociatedValue.hasValue() &&
4659 !SimplifiedAssociatedValue.getValue())
4660 return Changed;
4661
4662 Value &V = getAssociatedValue();
4663 auto *C = SimplifiedAssociatedValue.hasValue()
4664 ? dyn_cast<Constant>(SimplifiedAssociatedValue.getValue())
4665 : UndefValue::get(V.getType());
4666 if (C) {
4667 auto PredForReturned =
4668 [&](Value &V, const SmallSetVector<ReturnInst *, 4> &RetInsts) {
4669 // We can replace the AssociatedValue with the constant.
4670 if (&V == C || V.getType() != C->getType() || isa<UndefValue>(V))
4671 return true;
4672 if (auto *CI = dyn_cast<CallInst>(&V))
4673 if (CI->isMustTailCall())
4674 return true;
4675
4676 for (ReturnInst *RI : RetInsts) {
4677 if (RI->getFunction() != getAnchorScope())
4678 continue;
4679 LLVM_DEBUG(dbgs() << "[ValueSimplify] " << V << " -> " << *C
4680 << " in " << *RI << " :: " << *this << "\n");
4681 if (A.changeUseAfterManifest(RI->getOperandUse(0), *C))
4682 Changed = ChangeStatus::CHANGED;
4683 }
4684 return true;
4685 };
4686 A.checkForAllReturnedValuesAndReturnInsts(PredForReturned, *this);
4687 }
4688
4689 return Changed | AAValueSimplify::manifest(A);
4690 }
4691
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004692 /// See AbstractAttribute::trackStatistics()
4693 void trackStatistics() const override {
4694 STATS_DECLTRACK_FNRET_ATTR(value_simplify)
4695 }
4696};
4697
4698struct AAValueSimplifyFloating : AAValueSimplifyImpl {
4699 AAValueSimplifyFloating(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4700
4701 /// See AbstractAttribute::initialize(...).
4702 void initialize(Attributor &A) override {
4703 Value &V = getAnchorValue();
4704
4705 // TODO: add other stuffs
Hideto Ueno1d5d0742019-12-25 14:14:32 +09004706 if (isa<Constant>(V))
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004707 indicatePessimisticFixpoint();
4708 }
4709
4710 /// See AbstractAttribute::updateImpl(...).
4711 ChangeStatus updateImpl(Attributor &A) override {
4712 bool HasValueBefore = SimplifiedAssociatedValue.hasValue();
4713
Johannes Doerfert76843ba2020-01-28 23:51:25 -06004714 auto VisitValueCB = [&](Value &V, bool &, bool Stripped) -> bool {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004715 auto &AA = A.getAAFor<AAValueSimplify>(*this, IRPosition::value(V));
4716 if (!Stripped && this == &AA) {
4717 // TODO: Look the instruction and check recursively.
Hideto Ueno188f9a32020-01-15 15:25:52 +09004718
Johannes Doerfert02bd8182020-01-28 11:49:35 -06004719 LLVM_DEBUG(dbgs() << "[ValueSimplify] Can't be stripped more : " << V
4720 << "\n");
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004721 return false;
4722 }
4723 return checkAndUpdate(A, *this, V, SimplifiedAssociatedValue);
4724 };
4725
Johannes Doerfert76843ba2020-01-28 23:51:25 -06004726 bool Dummy = false;
Johannes Doerfert6626d1b2020-01-28 11:49:59 -06004727 if (!genericValueTraversal<AAValueSimplify, bool>(A, getIRPosition(), *this,
4728 Dummy, VisitValueCB))
Hideto Ueno188f9a32020-01-15 15:25:52 +09004729 if (!askSimplifiedValueForAAValueConstantRange(A))
4730 return indicatePessimisticFixpoint();
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004731
4732 // If a candicate was found in this update, return CHANGED.
4733
4734 return HasValueBefore == SimplifiedAssociatedValue.hasValue()
4735 ? ChangeStatus::UNCHANGED
4736 : ChangeStatus ::CHANGED;
4737 }
4738
4739 /// See AbstractAttribute::trackStatistics()
4740 void trackStatistics() const override {
4741 STATS_DECLTRACK_FLOATING_ATTR(value_simplify)
4742 }
4743};
4744
4745struct AAValueSimplifyFunction : AAValueSimplifyImpl {
4746 AAValueSimplifyFunction(const IRPosition &IRP) : AAValueSimplifyImpl(IRP) {}
4747
4748 /// See AbstractAttribute::initialize(...).
4749 void initialize(Attributor &A) override {
4750 SimplifiedAssociatedValue = &getAnchorValue();
4751 indicateOptimisticFixpoint();
4752 }
4753 /// See AbstractAttribute::initialize(...).
4754 ChangeStatus updateImpl(Attributor &A) override {
4755 llvm_unreachable(
4756 "AAValueSimplify(Function|CallSite)::updateImpl will not be called");
4757 }
4758 /// See AbstractAttribute::trackStatistics()
4759 void trackStatistics() const override {
4760 STATS_DECLTRACK_FN_ATTR(value_simplify)
4761 }
4762};
4763
4764struct AAValueSimplifyCallSite : AAValueSimplifyFunction {
4765 AAValueSimplifyCallSite(const IRPosition &IRP)
4766 : AAValueSimplifyFunction(IRP) {}
4767 /// See AbstractAttribute::trackStatistics()
4768 void trackStatistics() const override {
4769 STATS_DECLTRACK_CS_ATTR(value_simplify)
4770 }
4771};
4772
4773struct AAValueSimplifyCallSiteReturned : AAValueSimplifyReturned {
4774 AAValueSimplifyCallSiteReturned(const IRPosition &IRP)
4775 : AAValueSimplifyReturned(IRP) {}
4776
Johannes Doerfertb2c76002020-01-23 17:12:56 -06004777 /// See AbstractAttribute::manifest(...).
4778 ChangeStatus manifest(Attributor &A) override {
4779 if (auto *CI = dyn_cast<CallInst>(&getAssociatedValue()))
4780 if (CI->isMustTailCall())
4781 return ChangeStatus::UNCHANGED;
Johannes Doerfertad121ea2020-02-14 20:11:22 -06004782 return AAValueSimplifyImpl::manifest(A);
Johannes Doerfertb2c76002020-01-23 17:12:56 -06004783 }
4784
Hideto Uenof2b9dc42019-09-07 07:03:05 +00004785 void trackStatistics() const override {
4786 STATS_DECLTRACK_CSRET_ATTR(value_simplify)
4787 }
4788};
4789struct AAValueSimplifyCallSiteArgument : AAValueSimplifyFloating {
4790 AAValueSimplifyCallSiteArgument(const IRPosition &IRP)
4791 : AAValueSimplifyFloating(IRP) {}
4792
4793 void trackStatistics() const override {
4794 STATS_DECLTRACK_CSARG_ATTR(value_simplify)
4795 }
4796};
4797
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004798/// ----------------------- Heap-To-Stack Conversion ---------------------------
4799struct AAHeapToStackImpl : public AAHeapToStack {
4800 AAHeapToStackImpl(const IRPosition &IRP) : AAHeapToStack(IRP) {}
4801
4802 const std::string getAsStr() const override {
4803 return "[H2S] Mallocs: " + std::to_string(MallocCalls.size());
4804 }
4805
4806 ChangeStatus manifest(Attributor &A) override {
4807 assert(getState().isValidState() &&
4808 "Attempted to manifest an invalid state!");
4809
4810 ChangeStatus HasChanged = ChangeStatus::UNCHANGED;
4811 Function *F = getAssociatedFunction();
4812 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
4813
4814 for (Instruction *MallocCall : MallocCalls) {
4815 // This malloc cannot be replaced.
4816 if (BadMallocCalls.count(MallocCall))
4817 continue;
4818
4819 for (Instruction *FreeCall : FreesForMalloc[MallocCall]) {
4820 LLVM_DEBUG(dbgs() << "H2S: Removing free call: " << *FreeCall << "\n");
4821 A.deleteAfterManifest(*FreeCall);
4822 HasChanged = ChangeStatus::CHANGED;
4823 }
4824
4825 LLVM_DEBUG(dbgs() << "H2S: Removing malloc call: " << *MallocCall
4826 << "\n");
4827
4828 Constant *Size;
4829 if (isCallocLikeFn(MallocCall, TLI)) {
4830 auto *Num = cast<ConstantInt>(MallocCall->getOperand(0));
4831 auto *SizeT = dyn_cast<ConstantInt>(MallocCall->getOperand(1));
4832 APInt TotalSize = SizeT->getValue() * Num->getValue();
4833 Size =
4834 ConstantInt::get(MallocCall->getOperand(0)->getType(), TotalSize);
4835 } else {
4836 Size = cast<ConstantInt>(MallocCall->getOperand(0));
4837 }
4838
4839 unsigned AS = cast<PointerType>(MallocCall->getType())->getAddressSpace();
4840 Instruction *AI = new AllocaInst(Type::getInt8Ty(F->getContext()), AS,
4841 Size, "", MallocCall->getNextNode());
4842
4843 if (AI->getType() != MallocCall->getType())
4844 AI = new BitCastInst(AI, MallocCall->getType(), "malloc_bc",
4845 AI->getNextNode());
4846
Johannes Doerfert4c62a352020-01-12 00:17:08 -06004847 A.changeValueAfterManifest(*MallocCall, *AI);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004848
4849 if (auto *II = dyn_cast<InvokeInst>(MallocCall)) {
4850 auto *NBB = II->getNormalDest();
4851 BranchInst::Create(NBB, MallocCall->getParent());
4852 A.deleteAfterManifest(*MallocCall);
4853 } else {
4854 A.deleteAfterManifest(*MallocCall);
4855 }
4856
4857 if (isCallocLikeFn(MallocCall, TLI)) {
4858 auto *BI = new BitCastInst(AI, MallocCall->getType(), "calloc_bc",
4859 AI->getNextNode());
4860 Value *Ops[] = {
4861 BI, ConstantInt::get(F->getContext(), APInt(8, 0, false)), Size,
4862 ConstantInt::get(Type::getInt1Ty(F->getContext()), false)};
4863
4864 Type *Tys[] = {BI->getType(), MallocCall->getOperand(0)->getType()};
4865 Module *M = F->getParent();
4866 Function *Fn = Intrinsic::getDeclaration(M, Intrinsic::memset, Tys);
4867 CallInst::Create(Fn, Ops, "", BI->getNextNode());
4868 }
4869 HasChanged = ChangeStatus::CHANGED;
4870 }
4871
4872 return HasChanged;
4873 }
4874
4875 /// Collection of all malloc calls in a function.
4876 SmallSetVector<Instruction *, 4> MallocCalls;
4877
4878 /// Collection of malloc calls that cannot be converted.
4879 DenseSet<const Instruction *> BadMallocCalls;
4880
4881 /// A map for each malloc call to the set of associated free calls.
4882 DenseMap<Instruction *, SmallPtrSet<Instruction *, 4>> FreesForMalloc;
4883
4884 ChangeStatus updateImpl(Attributor &A) override;
4885};
4886
4887ChangeStatus AAHeapToStackImpl::updateImpl(Attributor &A) {
4888 const Function *F = getAssociatedFunction();
4889 const auto *TLI = A.getInfoCache().getTargetLibraryInfoForFunction(*F);
4890
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004891 MustBeExecutedContextExplorer &Explorer =
4892 A.getInfoCache().getMustBeExecutedContextExplorer();
4893
4894 auto FreeCheck = [&](Instruction &I) {
4895 const auto &Frees = FreesForMalloc.lookup(&I);
4896 if (Frees.size() != 1)
4897 return false;
4898 Instruction *UniqueFree = *Frees.begin();
4899 return Explorer.findInContextOf(UniqueFree, I.getNextNode());
4900 };
4901
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004902 auto UsesCheck = [&](Instruction &I) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004903 bool ValidUsesOnly = true;
4904 bool MustUse = true;
Hideto Ueno827bade2019-12-12 12:26:30 +00004905 auto Pred = [&](const Use &U, bool &Follow) -> bool {
4906 Instruction *UserI = cast<Instruction>(U.getUser());
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004907 if (isa<LoadInst>(UserI))
Hideto Ueno827bade2019-12-12 12:26:30 +00004908 return true;
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004909 if (auto *SI = dyn_cast<StoreInst>(UserI)) {
Hideto Ueno827bade2019-12-12 12:26:30 +00004910 if (SI->getValueOperand() == U.get()) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004911 LLVM_DEBUG(dbgs()
4912 << "[H2S] escaping store to memory: " << *UserI << "\n");
4913 ValidUsesOnly = false;
4914 } else {
4915 // A store into the malloc'ed memory is fine.
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004916 }
Hideto Ueno827bade2019-12-12 12:26:30 +00004917 return true;
Johannes Doerfertaf6e4792019-10-13 04:14:15 +00004918 }
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004919 if (auto *CB = dyn_cast<CallBase>(UserI)) {
Hideto Ueno827bade2019-12-12 12:26:30 +00004920 if (!CB->isArgOperand(&U) || CB->isLifetimeStartOrEnd())
4921 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004922 // Record malloc.
4923 if (isFreeCall(UserI, TLI)) {
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004924 if (MustUse) {
Hideto Ueno827bade2019-12-12 12:26:30 +00004925 FreesForMalloc[&I].insert(UserI);
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004926 } else {
4927 LLVM_DEBUG(dbgs() << "[H2S] free potentially on different mallocs: "
4928 << *UserI << "\n");
4929 ValidUsesOnly = false;
4930 }
Hideto Ueno827bade2019-12-12 12:26:30 +00004931 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004932 }
4933
Hideto Ueno827bade2019-12-12 12:26:30 +00004934 unsigned ArgNo = CB->getArgOperandNo(&U);
Stefan Stipanovica516fba2019-11-17 21:35:04 +01004935
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004936 const auto &NoCaptureAA = A.getAAFor<AANoCapture>(
4937 *this, IRPosition::callsite_argument(*CB, ArgNo));
4938
Stefan Stipanovica516fba2019-11-17 21:35:04 +01004939 // If a callsite argument use is nofree, we are fine.
4940 const auto &ArgNoFreeAA = A.getAAFor<AANoFree>(
4941 *this, IRPosition::callsite_argument(*CB, ArgNo));
4942
Hideto Ueno827bade2019-12-12 12:26:30 +00004943 if (!NoCaptureAA.isAssumedNoCapture() ||
4944 !ArgNoFreeAA.isAssumedNoFree()) {
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004945 LLVM_DEBUG(dbgs() << "[H2S] Bad user: " << *UserI << "\n");
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004946 ValidUsesOnly = false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004947 }
Hideto Ueno827bade2019-12-12 12:26:30 +00004948 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004949 }
4950
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004951 if (isa<GetElementPtrInst>(UserI) || isa<BitCastInst>(UserI) ||
4952 isa<PHINode>(UserI) || isa<SelectInst>(UserI)) {
4953 MustUse &= !(isa<PHINode>(UserI) || isa<SelectInst>(UserI));
Hideto Ueno827bade2019-12-12 12:26:30 +00004954 Follow = true;
4955 return true;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004956 }
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004957 // Unknown user for which we can not track uses further (in a way that
4958 // makes sense).
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004959 LLVM_DEBUG(dbgs() << "[H2S] Unknown user: " << *UserI << "\n");
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004960 ValidUsesOnly = false;
Hideto Ueno827bade2019-12-12 12:26:30 +00004961 return true;
4962 };
4963 A.checkForAllUses(Pred, *this, I);
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004964 return ValidUsesOnly;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004965 };
4966
4967 auto MallocCallocCheck = [&](Instruction &I) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004968 if (BadMallocCalls.count(&I))
4969 return true;
4970
4971 bool IsMalloc = isMallocLikeFn(&I, TLI);
4972 bool IsCalloc = !IsMalloc && isCallocLikeFn(&I, TLI);
4973 if (!IsMalloc && !IsCalloc) {
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004974 BadMallocCalls.insert(&I);
4975 return true;
4976 }
4977
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004978 if (IsMalloc) {
4979 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(0)))
Stefan Stipanovicfff8ec92019-12-17 20:41:09 +01004980 if (Size->getValue().ule(MaxHeapToStackSize))
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004981 if (UsesCheck(I) || FreeCheck(I)) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004982 MallocCalls.insert(&I);
4983 return true;
4984 }
4985 } else if (IsCalloc) {
4986 bool Overflow = false;
4987 if (auto *Num = dyn_cast<ConstantInt>(I.getOperand(0)))
4988 if (auto *Size = dyn_cast<ConstantInt>(I.getOperand(1)))
4989 if ((Size->getValue().umul_ov(Num->getValue(), Overflow))
Stefan Stipanovicfff8ec92019-12-17 20:41:09 +01004990 .ule(MaxHeapToStackSize))
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05004991 if (!Overflow && (UsesCheck(I) || FreeCheck(I))) {
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004992 MallocCalls.insert(&I);
4993 return true;
4994 }
4995 }
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004996
Johannes Doerfertd20f8072019-10-13 03:54:08 +00004997 BadMallocCalls.insert(&I);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00004998 return true;
4999 };
5000
5001 size_t NumBadMallocs = BadMallocCalls.size();
5002
5003 A.checkForAllCallLikeInstructions(MallocCallocCheck, *this);
5004
5005 if (NumBadMallocs != BadMallocCalls.size())
5006 return ChangeStatus::CHANGED;
5007
5008 return ChangeStatus::UNCHANGED;
5009}
5010
5011struct AAHeapToStackFunction final : public AAHeapToStackImpl {
5012 AAHeapToStackFunction(const IRPosition &IRP) : AAHeapToStackImpl(IRP) {}
5013
5014 /// See AbstractAttribute::trackStatistics()
5015 void trackStatistics() const override {
5016 STATS_DECL(MallocCalls, Function,
Johannes Doerfert0be9cf22019-10-14 17:29:05 -05005017 "Number of malloc calls converted to allocas");
5018 for (auto *C : MallocCalls)
5019 if (!BadMallocCalls.count(C))
5020 ++BUILD_STAT_NAME(MallocCalls, Function);
Stefan Stipanovic431141c2019-09-15 21:47:41 +00005021 }
5022};
5023
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005024/// ----------------------- Privatizable Pointers ------------------------------
5025struct AAPrivatizablePtrImpl : public AAPrivatizablePtr {
5026 AAPrivatizablePtrImpl(const IRPosition &IRP)
5027 : AAPrivatizablePtr(IRP), PrivatizableType(llvm::None) {}
5028
5029 ChangeStatus indicatePessimisticFixpoint() override {
5030 AAPrivatizablePtr::indicatePessimisticFixpoint();
5031 PrivatizableType = nullptr;
5032 return ChangeStatus::CHANGED;
5033 }
5034
5035 /// Identify the type we can chose for a private copy of the underlying
5036 /// argument. None means it is not clear yet, nullptr means there is none.
5037 virtual Optional<Type *> identifyPrivatizableType(Attributor &A) = 0;
5038
5039 /// Return a privatizable type that encloses both T0 and T1.
5040 /// TODO: This is merely a stub for now as we should manage a mapping as well.
5041 Optional<Type *> combineTypes(Optional<Type *> T0, Optional<Type *> T1) {
5042 if (!T0.hasValue())
5043 return T1;
5044 if (!T1.hasValue())
5045 return T0;
5046 if (T0 == T1)
5047 return T0;
5048 return nullptr;
5049 }
5050
5051 Optional<Type *> getPrivatizableType() const override {
5052 return PrivatizableType;
5053 }
5054
5055 const std::string getAsStr() const override {
5056 return isAssumedPrivatizablePtr() ? "[priv]" : "[no-priv]";
5057 }
5058
5059protected:
5060 Optional<Type *> PrivatizableType;
5061};
5062
5063// TODO: Do this for call site arguments (probably also other values) as well.
5064
5065struct AAPrivatizablePtrArgument final : public AAPrivatizablePtrImpl {
5066 AAPrivatizablePtrArgument(const IRPosition &IRP)
5067 : AAPrivatizablePtrImpl(IRP) {}
5068
5069 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5070 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5071 // If this is a byval argument and we know all the call sites (so we can
5072 // rewrite them), there is no need to check them explicitly.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005073 bool AllCallSitesKnown;
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005074 if (getIRPosition().hasAttr(Attribute::ByVal) &&
5075 A.checkForAllCallSites([](AbstractCallSite ACS) { return true; }, *this,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005076 true, AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005077 return getAssociatedValue().getType()->getPointerElementType();
5078
5079 Optional<Type *> Ty;
5080 unsigned ArgNo = getIRPosition().getArgNo();
5081
5082 // Make sure the associated call site argument has the same type at all call
5083 // sites and it is an allocation we know is safe to privatize, for now that
5084 // means we only allow alloca instructions.
5085 // TODO: We can additionally analyze the accesses in the callee to create
5086 // the type from that information instead. That is a little more
5087 // involved and will be done in a follow up patch.
5088 auto CallSiteCheck = [&](AbstractCallSite ACS) {
5089 IRPosition ACSArgPos = IRPosition::callsite_argument(ACS, ArgNo);
5090 // Check if a coresponding argument was found or if it is one not
5091 // associated (which can happen for callback calls).
5092 if (ACSArgPos.getPositionKind() == IRPosition::IRP_INVALID)
5093 return false;
5094
5095 // Check that all call sites agree on a type.
5096 auto &PrivCSArgAA = A.getAAFor<AAPrivatizablePtr>(*this, ACSArgPos);
5097 Optional<Type *> CSTy = PrivCSArgAA.getPrivatizableType();
5098
5099 LLVM_DEBUG({
5100 dbgs() << "[AAPrivatizablePtr] ACSPos: " << ACSArgPos << ", CSTy: ";
5101 if (CSTy.hasValue() && CSTy.getValue())
5102 CSTy.getValue()->print(dbgs());
5103 else if (CSTy.hasValue())
5104 dbgs() << "<nullptr>";
5105 else
5106 dbgs() << "<none>";
5107 });
5108
5109 Ty = combineTypes(Ty, CSTy);
5110
5111 LLVM_DEBUG({
5112 dbgs() << " : New Type: ";
5113 if (Ty.hasValue() && Ty.getValue())
5114 Ty.getValue()->print(dbgs());
5115 else if (Ty.hasValue())
5116 dbgs() << "<nullptr>";
5117 else
5118 dbgs() << "<none>";
5119 dbgs() << "\n";
5120 });
5121
5122 return !Ty.hasValue() || Ty.getValue();
5123 };
5124
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005125 if (!A.checkForAllCallSites(CallSiteCheck, *this, true, AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005126 return nullptr;
5127 return Ty;
5128 }
5129
5130 /// See AbstractAttribute::updateImpl(...).
5131 ChangeStatus updateImpl(Attributor &A) override {
5132 PrivatizableType = identifyPrivatizableType(A);
5133 if (!PrivatizableType.hasValue())
5134 return ChangeStatus::UNCHANGED;
5135 if (!PrivatizableType.getValue())
5136 return indicatePessimisticFixpoint();
5137
5138 // Avoid arguments with padding for now.
5139 if (!getIRPosition().hasAttr(Attribute::ByVal) &&
5140 !ArgumentPromotionPass::isDenselyPacked(PrivatizableType.getValue(),
5141 A.getInfoCache().getDL())) {
5142 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Padding detected\n");
5143 return indicatePessimisticFixpoint();
5144 }
5145
5146 // Verify callee and caller agree on how the promoted argument would be
5147 // passed.
5148 // TODO: The use of the ArgumentPromotion interface here is ugly, we need a
5149 // specialized form of TargetTransformInfo::areFunctionArgsABICompatible
5150 // which doesn't require the arguments ArgumentPromotion wanted to pass.
5151 Function &Fn = *getIRPosition().getAnchorScope();
5152 SmallPtrSet<Argument *, 1> ArgsToPromote, Dummy;
5153 ArgsToPromote.insert(getAssociatedArgument());
5154 const auto *TTI =
5155 A.getInfoCache().getAnalysisResultForFunction<TargetIRAnalysis>(Fn);
5156 if (!TTI ||
5157 !ArgumentPromotionPass::areFunctionArgsABICompatible(
5158 Fn, *TTI, ArgsToPromote, Dummy) ||
5159 ArgsToPromote.empty()) {
5160 LLVM_DEBUG(
5161 dbgs() << "[AAPrivatizablePtr] ABI incompatibility detected for "
5162 << Fn.getName() << "\n");
5163 return indicatePessimisticFixpoint();
5164 }
5165
5166 // Collect the types that will replace the privatizable type in the function
5167 // signature.
5168 SmallVector<Type *, 16> ReplacementTypes;
5169 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5170
5171 // Register a rewrite of the argument.
5172 Argument *Arg = getAssociatedArgument();
5173 if (!A.isValidFunctionSignatureRewrite(*Arg, ReplacementTypes)) {
5174 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Rewrite not valid\n");
5175 return indicatePessimisticFixpoint();
5176 }
5177
5178 unsigned ArgNo = Arg->getArgNo();
5179
5180 // Helper to check if for the given call site the associated argument is
5181 // passed to a callback where the privatization would be different.
5182 auto IsCompatiblePrivArgOfCallback = [&](CallSite CS) {
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005183 SmallVector<const Use *, 4> CBUses;
5184 AbstractCallSite::getCallbackUses(CS, CBUses);
5185 for (const Use *U : CBUses) {
5186 AbstractCallSite CBACS(U);
5187 assert(CBACS && CBACS.isCallbackCall());
5188 for (Argument &CBArg : CBACS.getCalledFunction()->args()) {
5189 int CBArgNo = CBACS.getCallArgOperandNo(CBArg);
5190
5191 LLVM_DEBUG({
5192 dbgs()
5193 << "[AAPrivatizablePtr] Argument " << *Arg
5194 << "check if can be privatized in the context of its parent ("
5195 << Arg->getParent()->getName()
5196 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5197 "callback ("
5198 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5199 << ")\n[AAPrivatizablePtr] " << CBArg << " : "
Michael Forster676c2962020-01-30 10:20:49 +01005200 << CBACS.getCallArgOperand(CBArg) << " vs "
5201 << CS.getArgOperand(ArgNo) << "\n"
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005202 << "[AAPrivatizablePtr] " << CBArg << " : "
5203 << CBACS.getCallArgOperandNo(CBArg) << " vs " << ArgNo << "\n";
5204 });
5205
5206 if (CBArgNo != int(ArgNo))
5207 continue;
5208 const auto &CBArgPrivAA =
5209 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(CBArg));
5210 if (CBArgPrivAA.isValidState()) {
5211 auto CBArgPrivTy = CBArgPrivAA.getPrivatizableType();
5212 if (!CBArgPrivTy.hasValue())
5213 continue;
5214 if (CBArgPrivTy.getValue() == PrivatizableType)
5215 continue;
5216 }
5217
5218 LLVM_DEBUG({
5219 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5220 << " cannot be privatized in the context of its parent ("
5221 << Arg->getParent()->getName()
5222 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5223 "callback ("
5224 << CBArgNo << "@" << CBACS.getCalledFunction()->getName()
5225 << ").\n[AAPrivatizablePtr] for which the argument "
5226 "privatization is not compatible.\n";
5227 });
5228 return false;
5229 }
5230 }
5231 return true;
5232 };
5233
5234 // Helper to check if for the given call site the associated argument is
5235 // passed to a direct call where the privatization would be different.
5236 auto IsCompatiblePrivArgOfDirectCS = [&](AbstractCallSite ACS) {
5237 CallBase *DC = cast<CallBase>(ACS.getInstruction());
5238 int DCArgNo = ACS.getCallArgOperandNo(ArgNo);
5239 assert(DCArgNo >= 0 && unsigned(DCArgNo) < DC->getNumArgOperands() &&
5240 "Expected a direct call operand for callback call operand");
5241
5242 LLVM_DEBUG({
5243 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5244 << " check if be privatized in the context of its parent ("
5245 << Arg->getParent()->getName()
5246 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5247 "direct call of ("
5248 << DCArgNo << "@" << DC->getCalledFunction()->getName()
5249 << ").\n";
5250 });
5251
5252 Function *DCCallee = DC->getCalledFunction();
5253 if (unsigned(DCArgNo) < DCCallee->arg_size()) {
5254 const auto &DCArgPrivAA = A.getAAFor<AAPrivatizablePtr>(
5255 *this, IRPosition::argument(*DCCallee->getArg(DCArgNo)));
5256 if (DCArgPrivAA.isValidState()) {
5257 auto DCArgPrivTy = DCArgPrivAA.getPrivatizableType();
5258 if (!DCArgPrivTy.hasValue())
5259 return true;
5260 if (DCArgPrivTy.getValue() == PrivatizableType)
5261 return true;
5262 }
5263 }
5264
5265 LLVM_DEBUG({
5266 dbgs() << "[AAPrivatizablePtr] Argument " << *Arg
5267 << " cannot be privatized in the context of its parent ("
5268 << Arg->getParent()->getName()
5269 << ")\n[AAPrivatizablePtr] because it is an argument in a "
5270 "direct call of ("
5271 << ACS.getCallSite().getCalledFunction()->getName()
5272 << ").\n[AAPrivatizablePtr] for which the argument "
5273 "privatization is not compatible.\n";
5274 });
5275 return false;
5276 };
5277
5278 // Helper to check if the associated argument is used at the given abstract
5279 // call site in a way that is incompatible with the privatization assumed
5280 // here.
5281 auto IsCompatiblePrivArgOfOtherCallSite = [&](AbstractCallSite ACS) {
5282 if (ACS.isDirectCall())
5283 return IsCompatiblePrivArgOfCallback(ACS.getCallSite());
5284 if (ACS.isCallbackCall())
5285 return IsCompatiblePrivArgOfDirectCS(ACS);
5286 return false;
5287 };
5288
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06005289 bool AllCallSitesKnown;
5290 if (!A.checkForAllCallSites(IsCompatiblePrivArgOfOtherCallSite, *this, true,
5291 AllCallSitesKnown))
Johannes Doerfert89c2e732019-10-30 17:20:20 -05005292 return indicatePessimisticFixpoint();
5293
5294 return ChangeStatus::UNCHANGED;
5295 }
5296
5297 /// Given a type to private \p PrivType, collect the constituates (which are
5298 /// used) in \p ReplacementTypes.
5299 static void
5300 identifyReplacementTypes(Type *PrivType,
5301 SmallVectorImpl<Type *> &ReplacementTypes) {
5302 // TODO: For now we expand the privatization type to the fullest which can
5303 // lead to dead arguments that need to be removed later.
5304 assert(PrivType && "Expected privatizable type!");
5305
5306 // Traverse the type, extract constituate types on the outermost level.
5307 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5308 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++)
5309 ReplacementTypes.push_back(PrivStructType->getElementType(u));
5310 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5311 ReplacementTypes.append(PrivArrayType->getNumElements(),
5312 PrivArrayType->getElementType());
5313 } else {
5314 ReplacementTypes.push_back(PrivType);
5315 }
5316 }
5317
5318 /// Initialize \p Base according to the type \p PrivType at position \p IP.
5319 /// The values needed are taken from the arguments of \p F starting at
5320 /// position \p ArgNo.
5321 static void createInitialization(Type *PrivType, Value &Base, Function &F,
5322 unsigned ArgNo, Instruction &IP) {
5323 assert(PrivType && "Expected privatizable type!");
5324
5325 IRBuilder<NoFolder> IRB(&IP);
5326 const DataLayout &DL = F.getParent()->getDataLayout();
5327
5328 // Traverse the type, build GEPs and stores.
5329 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5330 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5331 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5332 Type *PointeeTy = PrivStructType->getElementType(u)->getPointerTo();
5333 Value *Ptr = constructPointer(
5334 PointeeTy, &Base, PrivStructLayout->getElementOffset(u), IRB, DL);
5335 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5336 }
5337 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5338 Type *PointeePtrTy = PrivArrayType->getElementType()->getPointerTo();
5339 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeePtrTy);
5340 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5341 Value *Ptr =
5342 constructPointer(PointeePtrTy, &Base, u * PointeeTySize, IRB, DL);
5343 new StoreInst(F.getArg(ArgNo + u), Ptr, &IP);
5344 }
5345 } else {
5346 new StoreInst(F.getArg(ArgNo), &Base, &IP);
5347 }
5348 }
5349
5350 /// Extract values from \p Base according to the type \p PrivType at the
5351 /// call position \p ACS. The values are appended to \p ReplacementValues.
5352 void createReplacementValues(Type *PrivType, AbstractCallSite ACS,
5353 Value *Base,
5354 SmallVectorImpl<Value *> &ReplacementValues) {
5355 assert(Base && "Expected base value!");
5356 assert(PrivType && "Expected privatizable type!");
5357 Instruction *IP = ACS.getInstruction();
5358
5359 IRBuilder<NoFolder> IRB(IP);
5360 const DataLayout &DL = IP->getModule()->getDataLayout();
5361
5362 if (Base->getType()->getPointerElementType() != PrivType)
5363 Base = BitCastInst::CreateBitOrPointerCast(Base, PrivType->getPointerTo(),
5364 "", ACS.getInstruction());
5365
5366 // TODO: Improve the alignment of the loads.
5367 // Traverse the type, build GEPs and loads.
5368 if (auto *PrivStructType = dyn_cast<StructType>(PrivType)) {
5369 const StructLayout *PrivStructLayout = DL.getStructLayout(PrivStructType);
5370 for (unsigned u = 0, e = PrivStructType->getNumElements(); u < e; u++) {
5371 Type *PointeeTy = PrivStructType->getElementType(u);
5372 Value *Ptr =
5373 constructPointer(PointeeTy->getPointerTo(), Base,
5374 PrivStructLayout->getElementOffset(u), IRB, DL);
5375 LoadInst *L = new LoadInst(PointeeTy, Ptr, "", IP);
5376 L->setAlignment(MaybeAlign(1));
5377 ReplacementValues.push_back(L);
5378 }
5379 } else if (auto *PrivArrayType = dyn_cast<ArrayType>(PrivType)) {
5380 Type *PointeeTy = PrivArrayType->getElementType();
5381 uint64_t PointeeTySize = DL.getTypeStoreSize(PointeeTy);
5382 Type *PointeePtrTy = PointeeTy->getPointerTo();
5383 for (unsigned u = 0, e = PrivArrayType->getNumElements(); u < e; u++) {
5384 Value *Ptr =
5385 constructPointer(PointeePtrTy, Base, u * PointeeTySize, IRB, DL);
5386 LoadInst *L = new LoadInst(PointeePtrTy, Ptr, "", IP);
5387 L->setAlignment(MaybeAlign(1));
5388 ReplacementValues.push_back(L);
5389 }
5390 } else {
5391 LoadInst *L = new LoadInst(PrivType, Base, "", IP);
5392 L->setAlignment(MaybeAlign(1));
5393 ReplacementValues.push_back(L);
5394 }
5395 }
5396
5397 /// See AbstractAttribute::manifest(...)
5398 ChangeStatus manifest(Attributor &A) override {
5399 if (!PrivatizableType.hasValue())
5400 return ChangeStatus::UNCHANGED;
5401 assert(PrivatizableType.getValue() && "Expected privatizable type!");
5402
5403 // Collect all tail calls in the function as we cannot allow new allocas to
5404 // escape into tail recursion.
5405 // TODO: Be smarter about new allocas escaping into tail calls.
5406 SmallVector<CallInst *, 16> TailCalls;
5407 if (!A.checkForAllInstructions(
5408 [&](Instruction &I) {
5409 CallInst &CI = cast<CallInst>(I);
5410 if (CI.isTailCall())
5411 TailCalls.push_back(&CI);
5412 return true;
5413 },
5414 *this, {Instruction::Call}))
5415 return ChangeStatus::UNCHANGED;
5416
5417 Argument *Arg = getAssociatedArgument();
5418
5419 // Callback to repair the associated function. A new alloca is placed at the
5420 // beginning and initialized with the values passed through arguments. The
5421 // new alloca replaces the use of the old pointer argument.
5422 Attributor::ArgumentReplacementInfo::CalleeRepairCBTy FnRepairCB =
5423 [=](const Attributor::ArgumentReplacementInfo &ARI,
5424 Function &ReplacementFn, Function::arg_iterator ArgIt) {
5425 BasicBlock &EntryBB = ReplacementFn.getEntryBlock();
5426 Instruction *IP = &*EntryBB.getFirstInsertionPt();
5427 auto *AI = new AllocaInst(PrivatizableType.getValue(), 0,
5428 Arg->getName() + ".priv", IP);
5429 createInitialization(PrivatizableType.getValue(), *AI, ReplacementFn,
5430 ArgIt->getArgNo(), *IP);
5431 Arg->replaceAllUsesWith(AI);
5432
5433 for (CallInst *CI : TailCalls)
5434 CI->setTailCall(false);
5435 };
5436
5437 // Callback to repair a call site of the associated function. The elements
5438 // of the privatizable type are loaded prior to the call and passed to the
5439 // new function version.
5440 Attributor::ArgumentReplacementInfo::ACSRepairCBTy ACSRepairCB =
5441 [=](const Attributor::ArgumentReplacementInfo &ARI,
5442 AbstractCallSite ACS, SmallVectorImpl<Value *> &NewArgOperands) {
5443 createReplacementValues(
5444 PrivatizableType.getValue(), ACS,
5445 ACS.getCallArgOperand(ARI.getReplacedArg().getArgNo()),
5446 NewArgOperands);
5447 };
5448
5449 // Collect the types that will replace the privatizable type in the function
5450 // signature.
5451 SmallVector<Type *, 16> ReplacementTypes;
5452 identifyReplacementTypes(PrivatizableType.getValue(), ReplacementTypes);
5453
5454 // Register a rewrite of the argument.
5455 if (A.registerFunctionSignatureRewrite(*Arg, ReplacementTypes,
5456 std::move(FnRepairCB),
5457 std::move(ACSRepairCB)))
5458 return ChangeStatus::CHANGED;
5459 return ChangeStatus::UNCHANGED;
5460 }
5461
5462 /// See AbstractAttribute::trackStatistics()
5463 void trackStatistics() const override {
5464 STATS_DECLTRACK_ARG_ATTR(privatizable_ptr);
5465 }
5466};
5467
5468struct AAPrivatizablePtrFloating : public AAPrivatizablePtrImpl {
5469 AAPrivatizablePtrFloating(const IRPosition &IRP)
5470 : AAPrivatizablePtrImpl(IRP) {}
5471
5472 /// See AbstractAttribute::initialize(...).
5473 virtual void initialize(Attributor &A) override {
5474 // TODO: We can privatize more than arguments.
5475 indicatePessimisticFixpoint();
5476 }
5477
5478 ChangeStatus updateImpl(Attributor &A) override {
5479 llvm_unreachable("AAPrivatizablePtr(Floating|Returned|CallSiteReturned)::"
5480 "updateImpl will not be called");
5481 }
5482
5483 /// See AAPrivatizablePtrImpl::identifyPrivatizableType(...)
5484 Optional<Type *> identifyPrivatizableType(Attributor &A) override {
5485 Value *Obj =
5486 GetUnderlyingObject(&getAssociatedValue(), A.getInfoCache().getDL());
5487 if (!Obj) {
5488 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] No underlying object found!\n");
5489 return nullptr;
5490 }
5491
5492 if (auto *AI = dyn_cast<AllocaInst>(Obj))
5493 if (auto *CI = dyn_cast<ConstantInt>(AI->getArraySize()))
5494 if (CI->isOne())
5495 return Obj->getType()->getPointerElementType();
5496 if (auto *Arg = dyn_cast<Argument>(Obj)) {
5497 auto &PrivArgAA =
5498 A.getAAFor<AAPrivatizablePtr>(*this, IRPosition::argument(*Arg));
5499 if (PrivArgAA.isAssumedPrivatizablePtr())
5500 return Obj->getType()->getPointerElementType();
5501 }
5502
5503 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] Underlying object neither valid "
5504 "alloca nor privatizable argument: "
5505 << *Obj << "!\n");
5506 return nullptr;
5507 }
5508
5509 /// See AbstractAttribute::trackStatistics()
5510 void trackStatistics() const override {
5511 STATS_DECLTRACK_FLOATING_ATTR(privatizable_ptr);
5512 }
5513};
5514
5515struct AAPrivatizablePtrCallSiteArgument final
5516 : public AAPrivatizablePtrFloating {
5517 AAPrivatizablePtrCallSiteArgument(const IRPosition &IRP)
5518 : AAPrivatizablePtrFloating(IRP) {}
5519
5520 /// See AbstractAttribute::initialize(...).
5521 void initialize(Attributor &A) override {
5522 if (getIRPosition().hasAttr(Attribute::ByVal))
5523 indicateOptimisticFixpoint();
5524 }
5525
5526 /// See AbstractAttribute::updateImpl(...).
5527 ChangeStatus updateImpl(Attributor &A) override {
5528 PrivatizableType = identifyPrivatizableType(A);
5529 if (!PrivatizableType.hasValue())
5530 return ChangeStatus::UNCHANGED;
5531 if (!PrivatizableType.getValue())
5532 return indicatePessimisticFixpoint();
5533
5534 const IRPosition &IRP = getIRPosition();
5535 auto &NoCaptureAA = A.getAAFor<AANoCapture>(*this, IRP);
5536 if (!NoCaptureAA.isAssumedNoCapture()) {
5537 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might be captured!\n");
5538 return indicatePessimisticFixpoint();
5539 }
5540
5541 auto &NoAliasAA = A.getAAFor<AANoAlias>(*this, IRP);
5542 if (!NoAliasAA.isAssumedNoAlias()) {
5543 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer might alias!\n");
5544 return indicatePessimisticFixpoint();
5545 }
5546
5547 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(*this, IRP);
5548 if (!MemBehaviorAA.isAssumedReadOnly()) {
5549 LLVM_DEBUG(dbgs() << "[AAPrivatizablePtr] pointer is written!\n");
5550 return indicatePessimisticFixpoint();
5551 }
5552
5553 return ChangeStatus::UNCHANGED;
5554 }
5555
5556 /// See AbstractAttribute::trackStatistics()
5557 void trackStatistics() const override {
5558 STATS_DECLTRACK_CSARG_ATTR(privatizable_ptr);
5559 }
5560};
5561
5562struct AAPrivatizablePtrCallSiteReturned final
5563 : public AAPrivatizablePtrFloating {
5564 AAPrivatizablePtrCallSiteReturned(const IRPosition &IRP)
5565 : AAPrivatizablePtrFloating(IRP) {}
5566
5567 /// See AbstractAttribute::initialize(...).
5568 void initialize(Attributor &A) override {
5569 // TODO: We can privatize more than arguments.
5570 indicatePessimisticFixpoint();
5571 }
5572
5573 /// See AbstractAttribute::trackStatistics()
5574 void trackStatistics() const override {
5575 STATS_DECLTRACK_CSRET_ATTR(privatizable_ptr);
5576 }
5577};
5578
5579struct AAPrivatizablePtrReturned final : public AAPrivatizablePtrFloating {
5580 AAPrivatizablePtrReturned(const IRPosition &IRP)
5581 : AAPrivatizablePtrFloating(IRP) {}
5582
5583 /// See AbstractAttribute::initialize(...).
5584 void initialize(Attributor &A) override {
5585 // TODO: We can privatize more than arguments.
5586 indicatePessimisticFixpoint();
5587 }
5588
5589 /// See AbstractAttribute::trackStatistics()
5590 void trackStatistics() const override {
5591 STATS_DECLTRACK_FNRET_ATTR(privatizable_ptr);
5592 }
5593};
5594
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005595/// -------------------- Memory Behavior Attributes ----------------------------
5596/// Includes read-none, read-only, and write-only.
5597/// ----------------------------------------------------------------------------
5598struct AAMemoryBehaviorImpl : public AAMemoryBehavior {
5599 AAMemoryBehaviorImpl(const IRPosition &IRP) : AAMemoryBehavior(IRP) {}
5600
5601 /// See AbstractAttribute::initialize(...).
5602 void initialize(Attributor &A) override {
5603 intersectAssumedBits(BEST_STATE);
5604 getKnownStateFromValue(getIRPosition(), getState());
5605 IRAttribute::initialize(A);
5606 }
5607
5608 /// Return the memory behavior information encoded in the IR for \p IRP.
5609 static void getKnownStateFromValue(const IRPosition &IRP,
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005610 BitIntegerState &State,
5611 bool IgnoreSubsumingPositions = false) {
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005612 SmallVector<Attribute, 2> Attrs;
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005613 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005614 for (const Attribute &Attr : Attrs) {
5615 switch (Attr.getKindAsEnum()) {
5616 case Attribute::ReadNone:
5617 State.addKnownBits(NO_ACCESSES);
5618 break;
5619 case Attribute::ReadOnly:
5620 State.addKnownBits(NO_WRITES);
5621 break;
5622 case Attribute::WriteOnly:
5623 State.addKnownBits(NO_READS);
5624 break;
5625 default:
Johannes Doerfert282f5d72020-01-26 02:51:57 -06005626 llvm_unreachable("Unexpected attribute!");
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005627 }
5628 }
5629
5630 if (auto *I = dyn_cast<Instruction>(&IRP.getAnchorValue())) {
5631 if (!I->mayReadFromMemory())
5632 State.addKnownBits(NO_READS);
5633 if (!I->mayWriteToMemory())
5634 State.addKnownBits(NO_WRITES);
5635 }
5636 }
5637
5638 /// See AbstractAttribute::getDeducedAttributes(...).
5639 void getDeducedAttributes(LLVMContext &Ctx,
5640 SmallVectorImpl<Attribute> &Attrs) const override {
5641 assert(Attrs.size() == 0);
5642 if (isAssumedReadNone())
5643 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
5644 else if (isAssumedReadOnly())
5645 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadOnly));
5646 else if (isAssumedWriteOnly())
5647 Attrs.push_back(Attribute::get(Ctx, Attribute::WriteOnly));
5648 assert(Attrs.size() <= 1);
5649 }
5650
5651 /// See AbstractAttribute::manifest(...).
5652 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert282f5d72020-01-26 02:51:57 -06005653 if (hasAttr(Attribute::ReadNone, /* IgnoreSubsumingPositions */ true))
5654 return ChangeStatus::UNCHANGED;
5655
Johannes Doerfertb2083c52019-10-20 22:46:48 -05005656 const IRPosition &IRP = getIRPosition();
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005657
5658 // Check if we would improve the existing attributes first.
5659 SmallVector<Attribute, 4> DeducedAttrs;
5660 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
5661 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
5662 return IRP.hasAttr(Attr.getKindAsEnum(),
5663 /* IgnoreSubsumingPositions */ true);
5664 }))
5665 return ChangeStatus::UNCHANGED;
5666
5667 // Clear existing attributes.
5668 IRP.removeAttrs(AttrKinds);
5669
5670 // Use the generic manifest method.
5671 return IRAttribute::manifest(A);
5672 }
5673
5674 /// See AbstractState::getAsStr().
5675 const std::string getAsStr() const override {
5676 if (isAssumedReadNone())
5677 return "readnone";
5678 if (isAssumedReadOnly())
5679 return "readonly";
5680 if (isAssumedWriteOnly())
5681 return "writeonly";
5682 return "may-read/write";
5683 }
5684
5685 /// The set of IR attributes AAMemoryBehavior deals with.
5686 static const Attribute::AttrKind AttrKinds[3];
5687};
5688
5689const Attribute::AttrKind AAMemoryBehaviorImpl::AttrKinds[] = {
5690 Attribute::ReadNone, Attribute::ReadOnly, Attribute::WriteOnly};
5691
5692/// Memory behavior attribute for a floating value.
5693struct AAMemoryBehaviorFloating : AAMemoryBehaviorImpl {
5694 AAMemoryBehaviorFloating(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5695
5696 /// See AbstractAttribute::initialize(...).
5697 void initialize(Attributor &A) override {
5698 AAMemoryBehaviorImpl::initialize(A);
5699 // Initialize the use vector with all direct uses of the associated value.
5700 for (const Use &U : getAssociatedValue().uses())
5701 Uses.insert(&U);
5702 }
5703
5704 /// See AbstractAttribute::updateImpl(...).
5705 ChangeStatus updateImpl(Attributor &A) override;
5706
5707 /// See AbstractAttribute::trackStatistics()
5708 void trackStatistics() const override {
5709 if (isAssumedReadNone())
5710 STATS_DECLTRACK_FLOATING_ATTR(readnone)
5711 else if (isAssumedReadOnly())
5712 STATS_DECLTRACK_FLOATING_ATTR(readonly)
5713 else if (isAssumedWriteOnly())
5714 STATS_DECLTRACK_FLOATING_ATTR(writeonly)
5715 }
5716
5717private:
5718 /// Return true if users of \p UserI might access the underlying
5719 /// variable/location described by \p U and should therefore be analyzed.
5720 bool followUsersOfUseIn(Attributor &A, const Use *U,
5721 const Instruction *UserI);
5722
5723 /// Update the state according to the effect of use \p U in \p UserI.
5724 void analyzeUseIn(Attributor &A, const Use *U, const Instruction *UserI);
5725
5726protected:
5727 /// Container for (transitive) uses of the associated argument.
5728 SetVector<const Use *> Uses;
5729};
5730
5731/// Memory behavior attribute for function argument.
5732struct AAMemoryBehaviorArgument : AAMemoryBehaviorFloating {
5733 AAMemoryBehaviorArgument(const IRPosition &IRP)
5734 : AAMemoryBehaviorFloating(IRP) {}
5735
5736 /// See AbstractAttribute::initialize(...).
5737 void initialize(Attributor &A) override {
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005738 intersectAssumedBits(BEST_STATE);
5739 const IRPosition &IRP = getIRPosition();
5740 // TODO: Make IgnoreSubsumingPositions a property of an IRAttribute so we
5741 // can query it when we use has/getAttr. That would allow us to reuse the
5742 // initialize of the base class here.
5743 bool HasByVal =
5744 IRP.hasAttr({Attribute::ByVal}, /* IgnoreSubsumingPositions */ true);
5745 getKnownStateFromValue(IRP, getState(),
5746 /* IgnoreSubsumingPositions */ HasByVal);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005747
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005748 // Initialize the use vector with all direct uses of the associated value.
5749 Argument *Arg = getAssociatedArgument();
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005750 if (!Arg || !Arg->getParent()->hasExactDefinition()) {
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005751 indicatePessimisticFixpoint();
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005752 } else {
5753 // Initialize the use vector with all direct uses of the associated value.
5754 for (const Use &U : Arg->uses())
5755 Uses.insert(&U);
5756 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005757 }
5758
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005759 ChangeStatus manifest(Attributor &A) override {
Johannes Doerfert282f5d72020-01-26 02:51:57 -06005760 // TODO: Pointer arguments are not supported on vectors of pointers yet.
5761 if (!getAssociatedValue().getType()->isPointerTy())
5762 return ChangeStatus::UNCHANGED;
5763
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005764 // TODO: From readattrs.ll: "inalloca parameters are always
5765 // considered written"
5766 if (hasAttr({Attribute::InAlloca})) {
5767 removeKnownBits(NO_WRITES);
5768 removeAssumedBits(NO_WRITES);
5769 }
5770 return AAMemoryBehaviorFloating::manifest(A);
5771 }
5772
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005773 /// See AbstractAttribute::trackStatistics()
5774 void trackStatistics() const override {
5775 if (isAssumedReadNone())
5776 STATS_DECLTRACK_ARG_ATTR(readnone)
5777 else if (isAssumedReadOnly())
5778 STATS_DECLTRACK_ARG_ATTR(readonly)
5779 else if (isAssumedWriteOnly())
5780 STATS_DECLTRACK_ARG_ATTR(writeonly)
5781 }
5782};
5783
5784struct AAMemoryBehaviorCallSiteArgument final : AAMemoryBehaviorArgument {
5785 AAMemoryBehaviorCallSiteArgument(const IRPosition &IRP)
5786 : AAMemoryBehaviorArgument(IRP) {}
5787
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005788 /// See AbstractAttribute::initialize(...).
5789 void initialize(Attributor &A) override {
5790 if (Argument *Arg = getAssociatedArgument()) {
5791 if (Arg->hasByValAttr()) {
5792 addKnownBits(NO_WRITES);
5793 removeKnownBits(NO_READS);
5794 removeAssumedBits(NO_READS);
5795 }
5796 } else {
5797 }
5798 AAMemoryBehaviorArgument::initialize(A);
5799 }
5800
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005801 /// See AbstractAttribute::updateImpl(...).
5802 ChangeStatus updateImpl(Attributor &A) override {
5803 // TODO: Once we have call site specific value information we can provide
5804 // call site specific liveness liveness information and then it makes
5805 // sense to specialize attributes for call sites arguments instead of
5806 // redirecting requests to the callee argument.
5807 Argument *Arg = getAssociatedArgument();
5808 const IRPosition &ArgPos = IRPosition::argument(*Arg);
5809 auto &ArgAA = A.getAAFor<AAMemoryBehavior>(*this, ArgPos);
5810 return clampStateAndIndicateChange(
5811 getState(),
Johannes Doerfert31784242019-10-29 23:18:49 -05005812 static_cast<const AAMemoryBehavior::StateType &>(ArgAA.getState()));
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005813 }
5814
5815 /// See AbstractAttribute::trackStatistics()
5816 void trackStatistics() const override {
5817 if (isAssumedReadNone())
5818 STATS_DECLTRACK_CSARG_ATTR(readnone)
5819 else if (isAssumedReadOnly())
5820 STATS_DECLTRACK_CSARG_ATTR(readonly)
5821 else if (isAssumedWriteOnly())
5822 STATS_DECLTRACK_CSARG_ATTR(writeonly)
5823 }
5824};
5825
5826/// Memory behavior attribute for a call site return position.
5827struct AAMemoryBehaviorCallSiteReturned final : AAMemoryBehaviorFloating {
5828 AAMemoryBehaviorCallSiteReturned(const IRPosition &IRP)
5829 : AAMemoryBehaviorFloating(IRP) {}
5830
5831 /// See AbstractAttribute::manifest(...).
5832 ChangeStatus manifest(Attributor &A) override {
5833 // We do not annotate returned values.
5834 return ChangeStatus::UNCHANGED;
5835 }
5836
5837 /// See AbstractAttribute::trackStatistics()
5838 void trackStatistics() const override {}
5839};
5840
5841/// An AA to represent the memory behavior function attributes.
5842struct AAMemoryBehaviorFunction final : public AAMemoryBehaviorImpl {
5843 AAMemoryBehaviorFunction(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5844
5845 /// See AbstractAttribute::updateImpl(Attributor &A).
5846 virtual ChangeStatus updateImpl(Attributor &A) override;
5847
5848 /// See AbstractAttribute::manifest(...).
5849 ChangeStatus manifest(Attributor &A) override {
5850 Function &F = cast<Function>(getAnchorValue());
5851 if (isAssumedReadNone()) {
5852 F.removeFnAttr(Attribute::ArgMemOnly);
5853 F.removeFnAttr(Attribute::InaccessibleMemOnly);
5854 F.removeFnAttr(Attribute::InaccessibleMemOrArgMemOnly);
5855 }
5856 return AAMemoryBehaviorImpl::manifest(A);
5857 }
5858
5859 /// See AbstractAttribute::trackStatistics()
5860 void trackStatistics() const override {
5861 if (isAssumedReadNone())
5862 STATS_DECLTRACK_FN_ATTR(readnone)
5863 else if (isAssumedReadOnly())
5864 STATS_DECLTRACK_FN_ATTR(readonly)
5865 else if (isAssumedWriteOnly())
5866 STATS_DECLTRACK_FN_ATTR(writeonly)
5867 }
5868};
5869
5870/// AAMemoryBehavior attribute for call sites.
5871struct AAMemoryBehaviorCallSite final : AAMemoryBehaviorImpl {
5872 AAMemoryBehaviorCallSite(const IRPosition &IRP) : AAMemoryBehaviorImpl(IRP) {}
5873
5874 /// See AbstractAttribute::initialize(...).
5875 void initialize(Attributor &A) override {
5876 AAMemoryBehaviorImpl::initialize(A);
5877 Function *F = getAssociatedFunction();
5878 if (!F || !F->hasExactDefinition())
5879 indicatePessimisticFixpoint();
5880 }
5881
5882 /// See AbstractAttribute::updateImpl(...).
5883 ChangeStatus updateImpl(Attributor &A) override {
5884 // TODO: Once we have call site specific value information we can provide
5885 // call site specific liveness liveness information and then it makes
5886 // sense to specialize attributes for call sites arguments instead of
5887 // redirecting requests to the callee argument.
5888 Function *F = getAssociatedFunction();
5889 const IRPosition &FnPos = IRPosition::function(*F);
5890 auto &FnAA = A.getAAFor<AAMemoryBehavior>(*this, FnPos);
5891 return clampStateAndIndicateChange(
Johannes Doerfert1a746452019-10-20 22:28:49 -05005892 getState(),
5893 static_cast<const AAMemoryBehavior::StateType &>(FnAA.getState()));
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005894 }
5895
5896 /// See AbstractAttribute::trackStatistics()
5897 void trackStatistics() const override {
5898 if (isAssumedReadNone())
5899 STATS_DECLTRACK_CS_ATTR(readnone)
5900 else if (isAssumedReadOnly())
5901 STATS_DECLTRACK_CS_ATTR(readonly)
5902 else if (isAssumedWriteOnly())
5903 STATS_DECLTRACK_CS_ATTR(writeonly)
5904 }
5905};
5906
5907ChangeStatus AAMemoryBehaviorFunction::updateImpl(Attributor &A) {
5908
5909 // The current assumed state used to determine a change.
5910 auto AssumedState = getAssumed();
5911
5912 auto CheckRWInst = [&](Instruction &I) {
5913 // If the instruction has an own memory behavior state, use it to restrict
5914 // the local state. No further analysis is required as the other memory
5915 // state is as optimistic as it gets.
5916 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
5917 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
5918 *this, IRPosition::callsite_function(ICS));
5919 intersectAssumedBits(MemBehaviorAA.getAssumed());
5920 return !isAtFixpoint();
5921 }
5922
5923 // Remove access kind modifiers if necessary.
5924 if (I.mayReadFromMemory())
5925 removeAssumedBits(NO_READS);
5926 if (I.mayWriteToMemory())
5927 removeAssumedBits(NO_WRITES);
5928 return !isAtFixpoint();
5929 };
5930
5931 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
5932 return indicatePessimisticFixpoint();
5933
5934 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
5935 : ChangeStatus::UNCHANGED;
5936}
5937
5938ChangeStatus AAMemoryBehaviorFloating::updateImpl(Attributor &A) {
5939
5940 const IRPosition &IRP = getIRPosition();
5941 const IRPosition &FnPos = IRPosition::function_scope(IRP);
5942 AAMemoryBehavior::StateType &S = getState();
5943
5944 // First, check the function scope. We take the known information and we avoid
5945 // work if the assumed information implies the current assumed information for
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005946 // this attribute. This is a valid for all but byval arguments.
5947 Argument *Arg = IRP.getAssociatedArgument();
5948 AAMemoryBehavior::base_t FnMemAssumedState =
5949 AAMemoryBehavior::StateType::getWorstState();
5950 if (!Arg || !Arg->hasByValAttr()) {
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005951 const auto &FnMemAA = A.getAAFor<AAMemoryBehavior>(
5952 *this, FnPos, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005953 FnMemAssumedState = FnMemAA.getAssumed();
5954 S.addKnownBits(FnMemAA.getKnown());
5955 if ((S.getAssumed() & FnMemAA.getAssumed()) == S.getAssumed())
5956 return ChangeStatus::UNCHANGED;
5957 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005958
5959 // Make sure the value is not captured (except through "return"), if
5960 // it is, any information derived would be irrelevant anyway as we cannot
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005961 // check the potential aliases introduced by the capture. However, no need
5962 // to fall back to anythign less optimistic than the function state.
Johannes Doerfert28880192019-12-31 00:57:00 -06005963 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
5964 *this, IRP, /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005965 if (!ArgNoCaptureAA.isAssumedNoCaptureMaybeReturned()) {
Johannes Doerfert6abd01e2019-12-12 15:02:36 -06005966 S.intersectAssumedBits(FnMemAssumedState);
Johannes Doerfert8ee410c2019-10-13 20:47:16 +00005967 return ChangeStatus::CHANGED;
5968 }
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005969
5970 // The current assumed state used to determine a change.
5971 auto AssumedState = S.getAssumed();
5972
5973 // Liveness information to exclude dead users.
5974 // TODO: Take the FnPos once we have call site specific liveness information.
5975 const auto &LivenessAA = A.getAAFor<AAIsDead>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06005976 *this, IRPosition::function(*IRP.getAssociatedFunction()),
5977 /* TrackDependence */ false);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005978
5979 // Visit and expand uses until all are analyzed or a fixpoint is reached.
5980 for (unsigned i = 0; i < Uses.size() && !isAtFixpoint(); i++) {
5981 const Use *U = Uses[i];
5982 Instruction *UserI = cast<Instruction>(U->getUser());
5983 LLVM_DEBUG(dbgs() << "[AAMemoryBehavior] Use: " << **U << " in " << *UserI
Johannes Doerfert23f41f12020-01-12 01:09:22 -06005984 << " [Dead: " << (A.isAssumedDead(*U, this, &LivenessAA))
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005985 << "]\n");
Johannes Doerfert23f41f12020-01-12 01:09:22 -06005986 if (A.isAssumedDead(*U, this, &LivenessAA))
Johannes Doerfert1097fab2019-10-07 21:07:57 +00005987 continue;
5988
5989 // Check if the users of UserI should also be visited.
5990 if (followUsersOfUseIn(A, U, UserI))
5991 for (const Use &UserIUse : UserI->uses())
5992 Uses.insert(&UserIUse);
5993
5994 // If UserI might touch memory we analyze the use in detail.
5995 if (UserI->mayReadOrWriteMemory())
5996 analyzeUseIn(A, U, UserI);
5997 }
5998
5999 return (AssumedState != getAssumed()) ? ChangeStatus::CHANGED
6000 : ChangeStatus::UNCHANGED;
6001}
6002
6003bool AAMemoryBehaviorFloating::followUsersOfUseIn(Attributor &A, const Use *U,
6004 const Instruction *UserI) {
6005 // The loaded value is unrelated to the pointer argument, no need to
6006 // follow the users of the load.
6007 if (isa<LoadInst>(UserI))
6008 return false;
6009
6010 // By default we follow all uses assuming UserI might leak information on U,
6011 // we have special handling for call sites operands though.
6012 ImmutableCallSite ICS(UserI);
6013 if (!ICS || !ICS.isArgOperand(U))
6014 return true;
6015
6016 // If the use is a call argument known not to be captured, the users of
6017 // the call do not need to be visited because they have to be unrelated to
6018 // the input. Note that this check is not trivial even though we disallow
6019 // general capturing of the underlying argument. The reason is that the
6020 // call might the argument "through return", which we allow and for which we
6021 // need to check call users.
Johannes Doerfertff6254d2020-01-10 12:32:24 -06006022 if (U->get()->getType()->isPointerTy()) {
6023 unsigned ArgNo = ICS.getArgumentNo(U);
6024 const auto &ArgNoCaptureAA = A.getAAFor<AANoCapture>(
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06006025 *this, IRPosition::callsite_argument(ICS, ArgNo),
6026 /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfertff6254d2020-01-10 12:32:24 -06006027 return !ArgNoCaptureAA.isAssumedNoCapture();
6028 }
6029
6030 return true;
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006031}
6032
6033void AAMemoryBehaviorFloating::analyzeUseIn(Attributor &A, const Use *U,
6034 const Instruction *UserI) {
6035 assert(UserI->mayReadOrWriteMemory());
6036
6037 switch (UserI->getOpcode()) {
6038 default:
6039 // TODO: Handle all atomics and other side-effect operations we know of.
6040 break;
6041 case Instruction::Load:
6042 // Loads cause the NO_READS property to disappear.
6043 removeAssumedBits(NO_READS);
6044 return;
6045
6046 case Instruction::Store:
6047 // Stores cause the NO_WRITES property to disappear if the use is the
6048 // pointer operand. Note that we do assume that capturing was taken care of
6049 // somewhere else.
6050 if (cast<StoreInst>(UserI)->getPointerOperand() == U->get())
6051 removeAssumedBits(NO_WRITES);
6052 return;
6053
6054 case Instruction::Call:
6055 case Instruction::CallBr:
6056 case Instruction::Invoke: {
6057 // For call sites we look at the argument memory behavior attribute (this
6058 // could be recursive!) in order to restrict our own state.
6059 ImmutableCallSite ICS(UserI);
6060
6061 // Give up on operand bundles.
6062 if (ICS.isBundleOperand(U)) {
6063 indicatePessimisticFixpoint();
6064 return;
6065 }
6066
6067 // Calling a function does read the function pointer, maybe write it if the
6068 // function is self-modifying.
6069 if (ICS.isCallee(U)) {
6070 removeAssumedBits(NO_READS);
6071 break;
6072 }
6073
6074 // Adjust the possible access behavior based on the information on the
6075 // argument.
Johannes Doerfertff6254d2020-01-10 12:32:24 -06006076 IRPosition Pos;
6077 if (U->get()->getType()->isPointerTy())
6078 Pos = IRPosition::callsite_argument(ICS, ICS.getArgumentNo(U));
6079 else
6080 Pos = IRPosition::callsite_function(ICS);
Johannes Doerfert7ad17e02020-01-12 00:11:56 -06006081 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
6082 *this, Pos,
6083 /* TrackDependence */ true, DepClassTy::OPTIONAL);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00006084 // "assumed" has at most the same bits as the MemBehaviorAA assumed
6085 // and at least "known".
6086 intersectAssumedBits(MemBehaviorAA.getAssumed());
6087 return;
6088 }
6089 };
6090
6091 // Generally, look at the "may-properties" and adjust the assumed state if we
6092 // did not trigger special handling before.
6093 if (UserI->mayReadFromMemory())
6094 removeAssumedBits(NO_READS);
6095 if (UserI->mayWriteToMemory())
6096 removeAssumedBits(NO_WRITES);
6097}
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006098
Benjamin Kramer564a9de2020-02-17 17:55:03 +01006099} // namespace
6100
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006101/// -------------------- Memory Locations Attributes ---------------------------
6102/// Includes read-none, argmemonly, inaccessiblememonly,
6103/// inaccessiblememorargmemonly
6104/// ----------------------------------------------------------------------------
6105
6106std::string AAMemoryLocation::getMemoryLocationsAsStr(
6107 AAMemoryLocation::MemoryLocationsKind MLK) {
6108 if (0 == (MLK & AAMemoryLocation::NO_LOCATIONS))
6109 return "all memory";
6110 if (MLK == AAMemoryLocation::NO_LOCATIONS)
6111 return "no memory";
6112 std::string S = "memory:";
6113 if (0 == (MLK & AAMemoryLocation::NO_LOCAL_MEM))
6114 S += "stack,";
6115 if (0 == (MLK & AAMemoryLocation::NO_CONST_MEM))
6116 S += "constant,";
6117 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_INTERNAL_MEM))
6118 S += "internal global,";
6119 if (0 == (MLK & AAMemoryLocation::NO_GLOBAL_EXTERNAL_MEM))
6120 S += "external global,";
6121 if (0 == (MLK & AAMemoryLocation::NO_ARGUMENT_MEM))
6122 S += "argument,";
6123 if (0 == (MLK & AAMemoryLocation::NO_INACCESSIBLE_MEM))
6124 S += "inaccessible,";
6125 if (0 == (MLK & AAMemoryLocation::NO_MALLOCED_MEM))
6126 S += "malloced,";
6127 if (0 == (MLK & AAMemoryLocation::NO_UNKOWN_MEM))
6128 S += "unknown,";
6129 S.pop_back();
6130 return S;
6131}
6132
Benjamin Kramer564a9de2020-02-17 17:55:03 +01006133namespace {
6134
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006135struct AAMemoryLocationImpl : public AAMemoryLocation {
6136
6137 AAMemoryLocationImpl(const IRPosition &IRP) : AAMemoryLocation(IRP) {}
6138
6139 /// See AbstractAttribute::initialize(...).
6140 void initialize(Attributor &A) override {
6141 intersectAssumedBits(BEST_STATE);
6142 getKnownStateFromValue(getIRPosition(), getState());
6143 IRAttribute::initialize(A);
6144 }
6145
6146 /// Return the memory behavior information encoded in the IR for \p IRP.
6147 static void getKnownStateFromValue(const IRPosition &IRP,
6148 BitIntegerState &State,
6149 bool IgnoreSubsumingPositions = false) {
6150 SmallVector<Attribute, 2> Attrs;
6151 IRP.getAttrs(AttrKinds, Attrs, IgnoreSubsumingPositions);
6152 for (const Attribute &Attr : Attrs) {
6153 switch (Attr.getKindAsEnum()) {
6154 case Attribute::ReadNone:
6155 State.addKnownBits(NO_LOCAL_MEM | NO_CONST_MEM);
6156 break;
6157 case Attribute::InaccessibleMemOnly:
6158 State.addKnownBits(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
6159 break;
6160 case Attribute::ArgMemOnly:
6161 State.addKnownBits(inverseLocation(NO_ARGUMENT_MEM, true, true));
6162 break;
6163 case Attribute::InaccessibleMemOrArgMemOnly:
6164 State.addKnownBits(
6165 inverseLocation(NO_INACCESSIBLE_MEM | NO_ARGUMENT_MEM, true, true));
6166 break;
6167 default:
6168 llvm_unreachable("Unexpected attribute!");
6169 }
6170 }
6171 }
6172
6173 /// See AbstractAttribute::getDeducedAttributes(...).
6174 void getDeducedAttributes(LLVMContext &Ctx,
6175 SmallVectorImpl<Attribute> &Attrs) const override {
6176 assert(Attrs.size() == 0);
6177 if (isAssumedReadNone()) {
6178 Attrs.push_back(Attribute::get(Ctx, Attribute::ReadNone));
6179 } else if (getIRPosition().getPositionKind() == IRPosition::IRP_FUNCTION) {
6180 if (isAssumedInaccessibleMemOnly())
6181 Attrs.push_back(Attribute::get(Ctx, Attribute::InaccessibleMemOnly));
6182 else if (isAssumedArgMemOnly())
6183 Attrs.push_back(Attribute::get(Ctx, Attribute::ArgMemOnly));
6184 else if (isAssumedInaccessibleOrArgMemOnly())
6185 Attrs.push_back(
6186 Attribute::get(Ctx, Attribute::InaccessibleMemOrArgMemOnly));
6187 }
6188 assert(Attrs.size() <= 1);
6189 }
6190
6191 /// See AbstractAttribute::manifest(...).
6192 ChangeStatus manifest(Attributor &A) override {
6193 const IRPosition &IRP = getIRPosition();
6194
6195 // Check if we would improve the existing attributes first.
6196 SmallVector<Attribute, 4> DeducedAttrs;
6197 getDeducedAttributes(IRP.getAnchorValue().getContext(), DeducedAttrs);
6198 if (llvm::all_of(DeducedAttrs, [&](const Attribute &Attr) {
6199 return IRP.hasAttr(Attr.getKindAsEnum(),
6200 /* IgnoreSubsumingPositions */ true);
6201 }))
6202 return ChangeStatus::UNCHANGED;
6203
6204 // Clear existing attributes.
6205 IRP.removeAttrs(AttrKinds);
6206 if (isAssumedReadNone())
6207 IRP.removeAttrs(AAMemoryBehaviorImpl::AttrKinds);
6208
6209 // Use the generic manifest method.
6210 return IRAttribute::manifest(A);
6211 }
6212
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006213 /// See AAMemoryLocation::checkForAllAccessesToMemoryKind(...).
6214 bool checkForAllAccessesToMemoryKind(
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006215 const function_ref<bool(const Instruction *, const Value *, AccessKind,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006216 MemoryLocationsKind)> &Pred,
6217 MemoryLocationsKind RequestedMLK) const override {
6218 if (!isValidState())
6219 return false;
6220
6221 MemoryLocationsKind AssumedMLK = getAssumedNotAccessedLocation();
6222 if (AssumedMLK == NO_LOCATIONS)
6223 return true;
6224
6225 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
6226 if (CurMLK & RequestedMLK)
6227 continue;
6228
6229 const auto &Accesses = AccessKindAccessesMap.lookup(CurMLK);
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006230 for (const AccessInfo &AI : Accesses) {
6231 if (!Pred(AI.I, AI.Ptr, AI.Kind, CurMLK))
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006232 return false;
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006233 }
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006234 }
6235
6236 return true;
6237 }
6238
6239 ChangeStatus indicatePessimisticFixpoint() override {
6240 // If we give up and indicate a pessimistic fixpoint this instruction will
6241 // become an access for all potential access kinds:
6242 // TODO: Add pointers for argmemonly and globals to improve the results of
6243 // checkForAllAccessesToMemoryKind.
6244 bool Changed = false;
6245 MemoryLocationsKind KnownMLK = getKnown();
6246 Instruction *I = dyn_cast<Instruction>(&getAssociatedValue());
6247 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2)
6248 if (!(CurMLK & KnownMLK))
6249 updateStateAndAccessesMap(getState(), AccessKindAccessesMap, CurMLK, I,
6250 nullptr, Changed);
6251 return AAMemoryLocation::indicatePessimisticFixpoint();
6252 }
6253
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006254protected:
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006255 /// Helper struct to tie together an instruction that has a read or write
6256 /// effect with the pointer it accesses (if any).
6257 struct AccessInfo {
6258
6259 /// The instruction that caused the access.
6260 const Instruction *I;
6261
6262 /// The base pointer that is accessed, or null if unknown.
6263 const Value *Ptr;
6264
6265 /// The kind of access (read/write/read+write).
6266 AccessKind Kind;
6267
6268 bool operator==(const AccessInfo &RHS) const {
Simon Pilgrim8a48c4a2020-02-15 13:53:18 +00006269 return I == RHS.I && Ptr == RHS.Ptr && Kind == RHS.Kind;
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006270 }
6271 bool operator()(const AccessInfo &LHS, const AccessInfo &RHS) const {
6272 if (LHS.I != RHS.I)
6273 return LHS.I < RHS.I;
6274 if (LHS.Ptr != RHS.Ptr)
6275 return LHS.Ptr < RHS.Ptr;
6276 if (LHS.Kind != RHS.Kind)
6277 return LHS.Kind < RHS.Kind;
6278 return false;
6279 }
6280 };
6281
6282 /// Mapping from *single* memory location kinds, e.g., LOCAL_MEM with the
6283 /// value of NO_LOCAL_MEM, to the accesses encountered for this memory kind.
6284 using AccessKindAccessesMapTy =
6285 DenseMap<unsigned, SmallSet<AccessInfo, 8, AccessInfo>>;
6286 AccessKindAccessesMapTy AccessKindAccessesMap;
6287
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006288 /// Return the kind(s) of location that may be accessed by \p V.
6289 AAMemoryLocation::MemoryLocationsKind
6290 categorizeAccessedLocations(Attributor &A, Instruction &I, bool &Changed);
6291
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006292 /// Update the state \p State and the AccessKindAccessesMap given that \p I is
6293 /// an access to a \p MLK memory location with the access pointer \p Ptr.
6294 static void updateStateAndAccessesMap(AAMemoryLocation::StateType &State,
6295 AccessKindAccessesMapTy &AccessMap,
6296 MemoryLocationsKind MLK,
6297 const Instruction *I, const Value *Ptr,
6298 bool &Changed) {
6299 // TODO: The kind should be determined at the call sites based on the
6300 // information we have there.
6301 AccessKind Kind = READ_WRITE;
6302 if (I) {
6303 Kind = I->mayReadFromMemory() ? READ : NONE;
6304 Kind = AccessKind(Kind | (I->mayWriteToMemory() ? WRITE : NONE));
6305 }
6306
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006307 assert(isPowerOf2_32(MLK) && "Expected a single location set!");
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006308 Changed |= AccessMap[MLK].insert(AccessInfo{I, Ptr, Kind}).second;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006309 State.removeAssumedBits(MLK);
6310 }
6311
6312 /// Determine the underlying locations kinds for \p Ptr, e.g., globals or
6313 /// arguments, and update the state and access map accordingly.
6314 void categorizePtrValue(Attributor &A, const Instruction &I, const Value &Ptr,
6315 AAMemoryLocation::StateType &State, bool &Changed);
6316
6317 /// The set of IR attributes AAMemoryLocation deals with.
6318 static const Attribute::AttrKind AttrKinds[4];
6319};
6320
6321const Attribute::AttrKind AAMemoryLocationImpl::AttrKinds[] = {
6322 Attribute::ReadNone, Attribute::InaccessibleMemOnly, Attribute::ArgMemOnly,
6323 Attribute::InaccessibleMemOrArgMemOnly};
6324
6325void AAMemoryLocationImpl::categorizePtrValue(
6326 Attributor &A, const Instruction &I, const Value &Ptr,
6327 AAMemoryLocation::StateType &State, bool &Changed) {
6328 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize pointer locations for "
6329 << Ptr << " ["
6330 << getMemoryLocationsAsStr(State.getAssumed()) << "]\n");
6331
6332 auto StripGEPCB = [](Value *V) -> Value * {
6333 auto *GEP = dyn_cast<GEPOperator>(V);
6334 while (GEP) {
6335 V = GEP->getPointerOperand();
6336 GEP = dyn_cast<GEPOperator>(V);
6337 }
6338 return V;
6339 };
6340
6341 auto VisitValueCB = [&](Value &V, AAMemoryLocation::StateType &T,
6342 bool Stripped) -> bool {
6343 assert(!isa<GEPOperator>(V) && "GEPs should have been stripped.");
6344 if (isa<UndefValue>(V))
6345 return true;
6346 if (auto *Arg = dyn_cast<Argument>(&V)) {
6347 if (Arg->hasByValAttr())
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006348 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I,
6349 &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006350 else
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006351 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_ARGUMENT_MEM, &I,
6352 &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006353 return true;
6354 }
6355 if (auto *GV = dyn_cast<GlobalValue>(&V)) {
6356 if (GV->hasLocalLinkage())
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006357 updateStateAndAccessesMap(T, AccessKindAccessesMap,
6358 NO_GLOBAL_INTERNAL_MEM, &I, &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006359 else
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006360 updateStateAndAccessesMap(T, AccessKindAccessesMap,
6361 NO_GLOBAL_EXTERNAL_MEM, &I, &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006362 return true;
6363 }
6364 if (isa<AllocaInst>(V)) {
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006365 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_LOCAL_MEM, &I, &V,
6366 Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006367 return true;
6368 }
6369 if (ImmutableCallSite ICS = ImmutableCallSite(&V)) {
6370 const auto &NoAliasAA =
6371 A.getAAFor<AANoAlias>(*this, IRPosition::callsite_returned(ICS));
6372 if (NoAliasAA.isAssumedNoAlias()) {
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006373 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_MALLOCED_MEM, &I,
6374 &V, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006375 return true;
6376 }
6377 }
6378
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006379 updateStateAndAccessesMap(T, AccessKindAccessesMap, NO_UNKOWN_MEM, &I, &V,
6380 Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006381 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Ptr value cannot be categorized: "
6382 << V << " -> " << getMemoryLocationsAsStr(T.getAssumed())
6383 << "\n");
6384 return true;
6385 };
6386
6387 if (!genericValueTraversal<AAMemoryLocation, AAMemoryLocation::StateType>(
6388 A, IRPosition::value(Ptr), *this, State, VisitValueCB,
6389 /* MaxValues */ 32, StripGEPCB)) {
6390 LLVM_DEBUG(
6391 dbgs() << "[AAMemoryLocation] Pointer locations not categorized\n");
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006392 updateStateAndAccessesMap(State, AccessKindAccessesMap, NO_UNKOWN_MEM, &I,
6393 nullptr, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006394 } else {
6395 LLVM_DEBUG(
6396 dbgs()
6397 << "[AAMemoryLocation] Accessed locations with pointer locations: "
6398 << getMemoryLocationsAsStr(State.getAssumed()) << "\n");
6399 }
6400}
6401
6402AAMemoryLocation::MemoryLocationsKind
6403AAMemoryLocationImpl::categorizeAccessedLocations(Attributor &A, Instruction &I,
6404 bool &Changed) {
6405 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize accessed locations for "
6406 << I << "\n");
6407
6408 AAMemoryLocation::StateType AccessedLocs;
6409 AccessedLocs.intersectAssumedBits(NO_LOCATIONS);
6410
6411 if (ImmutableCallSite ICS = ImmutableCallSite(&I)) {
6412
6413 // First check if we assume any memory is access is visible.
6414 const auto &ICSMemLocationAA =
6415 A.getAAFor<AAMemoryLocation>(*this, IRPosition::callsite_function(ICS));
6416 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Categorize call site: " << I
6417 << " [" << ICSMemLocationAA << "]\n");
6418
6419 if (ICSMemLocationAA.isAssumedReadNone())
6420 return NO_LOCATIONS;
6421
6422 if (ICSMemLocationAA.isAssumedInaccessibleMemOnly()) {
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006423 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap,
6424 NO_INACCESSIBLE_MEM, &I, nullptr, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006425 return AccessedLocs.getAssumed();
6426 }
6427
6428 uint32_t ICSAssumedNotAccessedLocs =
6429 ICSMemLocationAA.getAssumedNotAccessedLocation();
6430
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006431 // Set the argmemonly and global bit as we handle them separately below.
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006432 uint32_t ICSAssumedNotAccessedLocsNoArgMem =
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006433 ICSAssumedNotAccessedLocs | NO_ARGUMENT_MEM | NO_GLOBAL_MEM;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006434
6435 for (MemoryLocationsKind CurMLK = 1; CurMLK < NO_LOCATIONS; CurMLK *= 2) {
6436 if (ICSAssumedNotAccessedLocsNoArgMem & CurMLK)
6437 continue;
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006438 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, CurMLK, &I,
6439 nullptr, Changed);
6440 }
6441
6442 // Now handle global memory if it might be accessed.
6443 bool HasGlobalAccesses = !(ICSAssumedNotAccessedLocs & NO_GLOBAL_MEM);
6444 if (HasGlobalAccesses) {
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006445 auto AccessPred = [&](const Instruction *, const Value *Ptr,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006446 AccessKind Kind, MemoryLocationsKind MLK) {
6447 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, MLK, &I,
6448 Ptr, Changed);
6449 return true;
6450 };
6451 if (!ICSMemLocationAA.checkForAllAccessesToMemoryKind(
6452 AccessPred, inverseLocation(NO_GLOBAL_MEM, false, false)))
6453 return AccessedLocs.getWorstState();
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006454 }
6455
6456 LLVM_DEBUG(
6457 dbgs() << "[AAMemoryLocation] Accessed state before argument handling: "
6458 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
6459
6460 // Now handle argument memory if it might be accessed.
6461 bool HasArgAccesses = !(ICSAssumedNotAccessedLocs & NO_ARGUMENT_MEM);
6462 if (HasArgAccesses) {
6463 for (unsigned ArgNo = 0, e = ICS.getNumArgOperands(); ArgNo < e;
6464 ++ArgNo) {
6465
6466 // Skip non-pointer arguments.
6467 const Value *ArgOp = ICS.getArgOperand(ArgNo);
6468 if (!ArgOp->getType()->isPtrOrPtrVectorTy())
6469 continue;
6470
6471 // Skip readnone arguments.
6472 const IRPosition &ArgOpIRP = IRPosition::callsite_argument(ICS, ArgNo);
6473 const auto &ArgOpMemLocationAA = A.getAAFor<AAMemoryBehavior>(
6474 *this, ArgOpIRP, /* TrackDependence */ true, DepClassTy::OPTIONAL);
6475
6476 if (ArgOpMemLocationAA.isAssumedReadNone())
6477 continue;
6478
6479 // Categorize potentially accessed pointer arguments as if there was an
6480 // access instruction with them as pointer.
6481 categorizePtrValue(A, I, *ArgOp, AccessedLocs, Changed);
6482 }
6483 }
6484
6485 LLVM_DEBUG(
6486 dbgs() << "[AAMemoryLocation] Accessed state after argument handling: "
6487 << getMemoryLocationsAsStr(AccessedLocs.getAssumed()) << "\n");
6488
6489 return AccessedLocs.getAssumed();
6490 }
6491
6492 if (const Value *Ptr = getPointerOperand(&I, /* AllowVolatile */ true)) {
6493 LLVM_DEBUG(
6494 dbgs() << "[AAMemoryLocation] Categorize memory access with pointer: "
6495 << I << " [" << *Ptr << "]\n");
6496 categorizePtrValue(A, I, *Ptr, AccessedLocs, Changed);
6497 return AccessedLocs.getAssumed();
6498 }
6499
6500 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Failed to categorize instruction: "
6501 << I << "\n");
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006502 updateStateAndAccessesMap(AccessedLocs, AccessKindAccessesMap, NO_UNKOWN_MEM,
6503 &I, nullptr, Changed);
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006504 return AccessedLocs.getAssumed();
6505}
6506
6507/// An AA to represent the memory behavior function attributes.
6508struct AAMemoryLocationFunction final : public AAMemoryLocationImpl {
6509 AAMemoryLocationFunction(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {}
6510
6511 /// See AbstractAttribute::updateImpl(Attributor &A).
6512 virtual ChangeStatus updateImpl(Attributor &A) override {
6513
6514 const auto &MemBehaviorAA = A.getAAFor<AAMemoryBehavior>(
6515 *this, getIRPosition(), /* TrackDependence */ false);
6516 if (MemBehaviorAA.isAssumedReadNone()) {
6517 if (MemBehaviorAA.isKnownReadNone())
6518 return indicateOptimisticFixpoint();
6519 assert(isAssumedReadNone() &&
6520 "AAMemoryLocation was not read-none but AAMemoryBehavior was!");
6521 A.recordDependence(MemBehaviorAA, *this, DepClassTy::OPTIONAL);
6522 return ChangeStatus::UNCHANGED;
6523 }
6524
6525 // The current assumed state used to determine a change.
6526 auto AssumedState = getAssumed();
6527 bool Changed = false;
6528
6529 auto CheckRWInst = [&](Instruction &I) {
6530 MemoryLocationsKind MLK = categorizeAccessedLocations(A, I, Changed);
6531 LLVM_DEBUG(dbgs() << "[AAMemoryLocation] Accessed locations for " << I
6532 << ": " << getMemoryLocationsAsStr(MLK) << "\n");
6533 removeAssumedBits(inverseLocation(MLK, false, false));
6534 return true;
6535 };
6536
6537 if (!A.checkForAllReadWriteInstructions(CheckRWInst, *this))
6538 return indicatePessimisticFixpoint();
6539
6540 Changed |= AssumedState != getAssumed();
6541 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
6542 }
6543
6544 /// See AbstractAttribute::trackStatistics()
6545 void trackStatistics() const override {
6546 if (isAssumedReadNone())
6547 STATS_DECLTRACK_FN_ATTR(readnone)
6548 else if (isAssumedArgMemOnly())
6549 STATS_DECLTRACK_FN_ATTR(argmemonly)
6550 else if (isAssumedInaccessibleMemOnly())
6551 STATS_DECLTRACK_FN_ATTR(inaccessiblememonly)
6552 else if (isAssumedInaccessibleOrArgMemOnly())
6553 STATS_DECLTRACK_FN_ATTR(inaccessiblememorargmemonly)
6554 }
6555};
6556
6557/// AAMemoryLocation attribute for call sites.
6558struct AAMemoryLocationCallSite final : AAMemoryLocationImpl {
6559 AAMemoryLocationCallSite(const IRPosition &IRP) : AAMemoryLocationImpl(IRP) {}
6560
6561 /// See AbstractAttribute::initialize(...).
6562 void initialize(Attributor &A) override {
6563 AAMemoryLocationImpl::initialize(A);
6564 Function *F = getAssociatedFunction();
6565 if (!F || !F->hasExactDefinition())
6566 indicatePessimisticFixpoint();
6567 }
6568
6569 /// See AbstractAttribute::updateImpl(...).
6570 ChangeStatus updateImpl(Attributor &A) override {
6571 // TODO: Once we have call site specific value information we can provide
6572 // call site specific liveness liveness information and then it makes
6573 // sense to specialize attributes for call sites arguments instead of
6574 // redirecting requests to the callee argument.
6575 Function *F = getAssociatedFunction();
6576 const IRPosition &FnPos = IRPosition::function(*F);
6577 auto &FnAA = A.getAAFor<AAMemoryLocation>(*this, FnPos);
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006578 bool Changed = false;
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006579 auto AccessPred = [&](const Instruction *I, const Value *Ptr,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006580 AccessKind Kind, MemoryLocationsKind MLK) {
Johannes Doerfert1d5da8c2020-02-15 20:38:49 -06006581 updateStateAndAccessesMap(getState(), AccessKindAccessesMap, MLK, I, Ptr,
Johannes Doerfertef746aa2020-01-27 22:49:36 -06006582 Changed);
6583 return true;
6584 };
6585 if (!FnAA.checkForAllAccessesToMemoryKind(AccessPred, ALL_LOCATIONS))
6586 return indicatePessimisticFixpoint();
6587 return Changed ? ChangeStatus::CHANGED : ChangeStatus::UNCHANGED;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06006588 }
6589
6590 /// See AbstractAttribute::trackStatistics()
6591 void trackStatistics() const override {
6592 if (isAssumedReadNone())
6593 STATS_DECLTRACK_CS_ATTR(readnone)
6594 }
6595};
6596
Hideto Ueno188f9a32020-01-15 15:25:52 +09006597/// ------------------ Value Constant Range Attribute -------------------------
Hideto Uenoe9963032020-01-01 15:25:19 +09006598
Hideto Ueno188f9a32020-01-15 15:25:52 +09006599struct AAValueConstantRangeImpl : AAValueConstantRange {
6600 using StateType = IntegerRangeState;
6601 AAValueConstantRangeImpl(const IRPosition &IRP) : AAValueConstantRange(IRP) {}
6602
6603 /// See AbstractAttribute::getAsStr().
6604 const std::string getAsStr() const override {
6605 std::string Str;
6606 llvm::raw_string_ostream OS(Str);
6607 OS << "range(" << getBitWidth() << ")<";
6608 getKnown().print(OS);
6609 OS << " / ";
6610 getAssumed().print(OS);
6611 OS << ">";
6612 return OS.str();
6613 }
6614
6615 /// Helper function to get a SCEV expr for the associated value at program
6616 /// point \p I.
6617 const SCEV *getSCEV(Attributor &A, const Instruction *I = nullptr) const {
6618 if (!getAnchorScope())
6619 return nullptr;
6620
6621 ScalarEvolution *SE =
6622 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
6623 *getAnchorScope());
6624
6625 LoopInfo *LI = A.getInfoCache().getAnalysisResultForFunction<LoopAnalysis>(
6626 *getAnchorScope());
6627
6628 if (!SE || !LI)
6629 return nullptr;
6630
6631 const SCEV *S = SE->getSCEV(&getAssociatedValue());
6632 if (!I)
6633 return S;
6634
6635 return SE->getSCEVAtScope(S, LI->getLoopFor(I->getParent()));
6636 }
6637
6638 /// Helper function to get a range from SCEV for the associated value at
6639 /// program point \p I.
6640 ConstantRange getConstantRangeFromSCEV(Attributor &A,
6641 const Instruction *I = nullptr) const {
6642 if (!getAnchorScope())
6643 return getWorstState(getBitWidth());
6644
6645 ScalarEvolution *SE =
6646 A.getInfoCache().getAnalysisResultForFunction<ScalarEvolutionAnalysis>(
6647 *getAnchorScope());
6648
6649 const SCEV *S = getSCEV(A, I);
6650 if (!SE || !S)
6651 return getWorstState(getBitWidth());
6652
6653 return SE->getUnsignedRange(S);
6654 }
6655
6656 /// Helper function to get a range from LVI for the associated value at
6657 /// program point \p I.
6658 ConstantRange
6659 getConstantRangeFromLVI(Attributor &A,
6660 const Instruction *CtxI = nullptr) const {
6661 if (!getAnchorScope())
6662 return getWorstState(getBitWidth());
6663
6664 LazyValueInfo *LVI =
6665 A.getInfoCache().getAnalysisResultForFunction<LazyValueAnalysis>(
6666 *getAnchorScope());
6667
6668 if (!LVI || !CtxI)
6669 return getWorstState(getBitWidth());
6670 return LVI->getConstantRange(&getAssociatedValue(),
6671 const_cast<BasicBlock *>(CtxI->getParent()),
6672 const_cast<Instruction *>(CtxI));
6673 }
6674
6675 /// See AAValueConstantRange::getKnownConstantRange(..).
6676 ConstantRange
6677 getKnownConstantRange(Attributor &A,
6678 const Instruction *CtxI = nullptr) const override {
6679 if (!CtxI || CtxI == getCtxI())
6680 return getKnown();
6681
6682 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6683 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6684 return getKnown().intersectWith(SCEVR).intersectWith(LVIR);
6685 }
6686
6687 /// See AAValueConstantRange::getAssumedConstantRange(..).
6688 ConstantRange
6689 getAssumedConstantRange(Attributor &A,
6690 const Instruction *CtxI = nullptr) const override {
6691 // TODO: Make SCEV use Attributor assumption.
6692 // We may be able to bound a variable range via assumptions in
6693 // Attributor. ex.) If x is assumed to be in [1, 3] and y is known to
6694 // evolve to x^2 + x, then we can say that y is in [2, 12].
6695
6696 if (!CtxI || CtxI == getCtxI())
6697 return getAssumed();
6698
6699 ConstantRange LVIR = getConstantRangeFromLVI(A, CtxI);
6700 ConstantRange SCEVR = getConstantRangeFromSCEV(A, CtxI);
6701 return getAssumed().intersectWith(SCEVR).intersectWith(LVIR);
6702 }
6703
6704 /// See AbstractAttribute::initialize(..).
6705 void initialize(Attributor &A) override {
6706 // Intersect a range given by SCEV.
6707 intersectKnown(getConstantRangeFromSCEV(A, getCtxI()));
6708
6709 // Intersect a range given by LVI.
6710 intersectKnown(getConstantRangeFromLVI(A, getCtxI()));
6711 }
6712
6713 /// Helper function to create MDNode for range metadata.
6714 static MDNode *
6715 getMDNodeForConstantRange(Type *Ty, LLVMContext &Ctx,
6716 const ConstantRange &AssumedConstantRange) {
6717 Metadata *LowAndHigh[] = {ConstantAsMetadata::get(ConstantInt::get(
6718 Ty, AssumedConstantRange.getLower())),
6719 ConstantAsMetadata::get(ConstantInt::get(
6720 Ty, AssumedConstantRange.getUpper()))};
6721 return MDNode::get(Ctx, LowAndHigh);
6722 }
6723
6724 /// Return true if \p Assumed is included in \p KnownRanges.
6725 static bool isBetterRange(const ConstantRange &Assumed, MDNode *KnownRanges) {
6726
6727 if (Assumed.isFullSet())
6728 return false;
6729
6730 if (!KnownRanges)
6731 return true;
6732
6733 // If multiple ranges are annotated in IR, we give up to annotate assumed
6734 // range for now.
6735
6736 // TODO: If there exists a known range which containts assumed range, we
6737 // can say assumed range is better.
6738 if (KnownRanges->getNumOperands() > 2)
6739 return false;
6740
6741 ConstantInt *Lower =
6742 mdconst::extract<ConstantInt>(KnownRanges->getOperand(0));
6743 ConstantInt *Upper =
6744 mdconst::extract<ConstantInt>(KnownRanges->getOperand(1));
6745
6746 ConstantRange Known(Lower->getValue(), Upper->getValue());
6747 return Known.contains(Assumed) && Known != Assumed;
6748 }
6749
6750 /// Helper function to set range metadata.
6751 static bool
6752 setRangeMetadataIfisBetterRange(Instruction *I,
6753 const ConstantRange &AssumedConstantRange) {
6754 auto *OldRangeMD = I->getMetadata(LLVMContext::MD_range);
6755 if (isBetterRange(AssumedConstantRange, OldRangeMD)) {
6756 if (!AssumedConstantRange.isEmptySet()) {
6757 I->setMetadata(LLVMContext::MD_range,
6758 getMDNodeForConstantRange(I->getType(), I->getContext(),
6759 AssumedConstantRange));
6760 return true;
6761 }
6762 }
6763 return false;
6764 }
6765
6766 /// See AbstractAttribute::manifest()
6767 ChangeStatus manifest(Attributor &A) override {
6768 ChangeStatus Changed = ChangeStatus::UNCHANGED;
6769 ConstantRange AssumedConstantRange = getAssumedConstantRange(A);
6770 assert(!AssumedConstantRange.isFullSet() && "Invalid state");
6771
6772 auto &V = getAssociatedValue();
6773 if (!AssumedConstantRange.isEmptySet() &&
6774 !AssumedConstantRange.isSingleElement()) {
6775 if (Instruction *I = dyn_cast<Instruction>(&V))
6776 if (isa<CallInst>(I) || isa<LoadInst>(I))
6777 if (setRangeMetadataIfisBetterRange(I, AssumedConstantRange))
6778 Changed = ChangeStatus::CHANGED;
6779 }
6780
6781 return Changed;
6782 }
6783};
6784
Johannes Doerfertea5fabe2020-01-28 09:46:15 -06006785struct AAValueConstantRangeArgument final
6786 : AAArgumentFromCallSiteArguments<
6787 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState> {
Hideto Ueno188f9a32020-01-15 15:25:52 +09006788 AAValueConstantRangeArgument(const IRPosition &IRP)
Johannes Doerfertea5fabe2020-01-28 09:46:15 -06006789 : AAArgumentFromCallSiteArguments<
6790 AAValueConstantRange, AAValueConstantRangeImpl, IntegerRangeState>(
6791 IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006792
6793 /// See AbstractAttribute::trackStatistics()
6794 void trackStatistics() const override {
6795 STATS_DECLTRACK_ARG_ATTR(value_range)
6796 }
6797};
6798
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006799struct AAValueConstantRangeReturned
6800 : AAReturnedFromReturnedValues<AAValueConstantRange,
6801 AAValueConstantRangeImpl> {
6802 using Base = AAReturnedFromReturnedValues<AAValueConstantRange,
6803 AAValueConstantRangeImpl>;
6804 AAValueConstantRangeReturned(const IRPosition &IRP) : Base(IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006805
Johannes Doerfert791c9f12020-01-29 18:02:42 -06006806 /// See AbstractAttribute::initialize(...).
6807 void initialize(Attributor &A) override {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09006808
6809 /// See AbstractAttribute::trackStatistics()
6810 void trackStatistics() const override {
6811 STATS_DECLTRACK_FNRET_ATTR(value_range)
6812 }
6813};
6814
6815struct AAValueConstantRangeFloating : AAValueConstantRangeImpl {
6816 AAValueConstantRangeFloating(const IRPosition &IRP)
6817 : AAValueConstantRangeImpl(IRP) {}
6818
6819 /// See AbstractAttribute::initialize(...).
6820 void initialize(Attributor &A) override {
Johannes Doerfert028db8c42020-02-09 19:05:15 -06006821 AAValueConstantRangeImpl::initialize(A);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006822 Value &V = getAssociatedValue();
6823
6824 if (auto *C = dyn_cast<ConstantInt>(&V)) {
6825 unionAssumed(ConstantRange(C->getValue()));
6826 indicateOptimisticFixpoint();
6827 return;
6828 }
6829
6830 if (isa<UndefValue>(&V)) {
Johannes Doerfertb53af0e2020-02-14 20:08:20 -06006831 // Collapse the undef state to 0.
6832 unionAssumed(ConstantRange(APInt(getBitWidth(), 0)));
Hideto Ueno188f9a32020-01-15 15:25:52 +09006833 indicateOptimisticFixpoint();
6834 return;
6835 }
6836
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06006837 if (isa<BinaryOperator>(&V) || isa<CmpInst>(&V) || isa<CastInst>(&V))
6838 return;
Hideto Ueno188f9a32020-01-15 15:25:52 +09006839 // If it is a load instruction with range metadata, use it.
6840 if (LoadInst *LI = dyn_cast<LoadInst>(&V))
6841 if (auto *RangeMD = LI->getMetadata(LLVMContext::MD_range)) {
6842 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
6843 return;
6844 }
6845
Johannes Doerfert81554392020-02-09 20:14:35 -06006846 // We can work with PHI and select instruction as we traverse their operands
Johannes Doerfert7e7e6592020-02-09 19:08:04 -06006847 // during update.
Johannes Doerfert81554392020-02-09 20:14:35 -06006848 if (isa<SelectInst>(V) || isa<PHINode>(V))
Johannes Doerfert7e7e6592020-02-09 19:08:04 -06006849 return;
6850
Hideto Ueno188f9a32020-01-15 15:25:52 +09006851 // Otherwise we give up.
6852 indicatePessimisticFixpoint();
6853
Johannes Doerfert02bd8182020-01-28 11:49:35 -06006854 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] We give up: "
6855 << getAssociatedValue() << "\n");
Hideto Ueno188f9a32020-01-15 15:25:52 +09006856 }
6857
Johannes Doerfert81554392020-02-09 20:14:35 -06006858 bool calculateBinaryOperator(
6859 Attributor &A, BinaryOperator *BinOp, IntegerRangeState &T,
6860 Instruction *CtxI,
6861 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
Hideto Ueno188f9a32020-01-15 15:25:52 +09006862 Value *LHS = BinOp->getOperand(0);
6863 Value *RHS = BinOp->getOperand(1);
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06006864 // TODO: Allow non integers as well.
6865 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
6866 return false;
Hideto Ueno188f9a32020-01-15 15:25:52 +09006867
6868 auto &LHSAA =
6869 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06006870 QuerriedAAs.push_back(&LHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006871 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
6872
6873 auto &RHSAA =
6874 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06006875 QuerriedAAs.push_back(&RHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006876 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
6877
6878 auto AssumedRange = LHSAARange.binaryOp(BinOp->getOpcode(), RHSAARange);
6879
6880 T.unionAssumed(AssumedRange);
6881
6882 // TODO: Track a known state too.
6883
6884 return T.isValidState();
6885 }
6886
Johannes Doerfert81554392020-02-09 20:14:35 -06006887 bool calculateCastInst(
6888 Attributor &A, CastInst *CastI, IntegerRangeState &T, Instruction *CtxI,
6889 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06006890 assert(CastI->getNumOperands() == 1 && "Expected cast to be unary!");
6891 // TODO: Allow non integers as well.
6892 Value &OpV = *CastI->getOperand(0);
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06006893 if (!OpV.getType()->isIntegerTy())
6894 return false;
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06006895
6896 auto &OpAA =
6897 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(OpV));
Johannes Doerfert81554392020-02-09 20:14:35 -06006898 QuerriedAAs.push_back(&OpAA);
Johannes Doerfertffdbd2a2020-02-09 19:07:30 -06006899 T.unionAssumed(
6900 OpAA.getAssumed().castOp(CastI->getOpcode(), getState().getBitWidth()));
6901 return T.isValidState();
6902 }
6903
Johannes Doerfert81554392020-02-09 20:14:35 -06006904 bool
6905 calculateCmpInst(Attributor &A, CmpInst *CmpI, IntegerRangeState &T,
6906 Instruction *CtxI,
6907 SmallVectorImpl<const AAValueConstantRange *> &QuerriedAAs) {
Hideto Ueno188f9a32020-01-15 15:25:52 +09006908 Value *LHS = CmpI->getOperand(0);
6909 Value *RHS = CmpI->getOperand(1);
Johannes Doerfert7cbb1072020-02-13 14:22:26 -06006910 // TODO: Allow non integers as well.
6911 if (!LHS->getType()->isIntegerTy() || !RHS->getType()->isIntegerTy())
6912 return false;
Hideto Ueno188f9a32020-01-15 15:25:52 +09006913
6914 auto &LHSAA =
6915 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*LHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06006916 QuerriedAAs.push_back(&LHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006917 auto &RHSAA =
6918 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(*RHS));
Johannes Doerfert81554392020-02-09 20:14:35 -06006919 QuerriedAAs.push_back(&RHSAA);
Hideto Ueno188f9a32020-01-15 15:25:52 +09006920
6921 auto LHSAARange = LHSAA.getAssumedConstantRange(A, CtxI);
6922 auto RHSAARange = RHSAA.getAssumedConstantRange(A, CtxI);
6923
6924 // If one of them is empty set, we can't decide.
6925 if (LHSAARange.isEmptySet() || RHSAARange.isEmptySet())
6926 return true;
6927
6928 bool MustTrue = false, MustFalse = false;
6929
6930 auto AllowedRegion =
6931 ConstantRange::makeAllowedICmpRegion(CmpI->getPredicate(), RHSAARange);
6932
6933 auto SatisfyingRegion = ConstantRange::makeSatisfyingICmpRegion(
6934 CmpI->getPredicate(), RHSAARange);
6935
6936 if (AllowedRegion.intersectWith(LHSAARange).isEmptySet())
6937 MustFalse = true;
6938
6939 if (SatisfyingRegion.contains(LHSAARange))
6940 MustTrue = true;
6941
6942 assert((!MustTrue || !MustFalse) &&
6943 "Either MustTrue or MustFalse should be false!");
6944
6945 if (MustTrue)
6946 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 1)));
6947 else if (MustFalse)
6948 T.unionAssumed(ConstantRange(APInt(/* numBits */ 1, /* val */ 0)));
6949 else
6950 T.unionAssumed(ConstantRange(/* BitWidth */ 1, /* isFullSet */ true));
6951
6952 LLVM_DEBUG(dbgs() << "[AAValueConstantRange] " << *CmpI << " " << LHSAA
6953 << " " << RHSAA << "\n");
6954
6955 // TODO: Track a known state too.
6956 return T.isValidState();
6957 }
6958
6959 /// See AbstractAttribute::updateImpl(...).
6960 ChangeStatus updateImpl(Attributor &A) override {
6961 Instruction *CtxI = getCtxI();
6962 auto VisitValueCB = [&](Value &V, IntegerRangeState &T,
6963 bool Stripped) -> bool {
6964 Instruction *I = dyn_cast<Instruction>(&V);
6965 if (!I) {
6966
6967 // If the value is not instruction, we query AA to Attributor.
6968 const auto &AA =
6969 A.getAAFor<AAValueConstantRange>(*this, IRPosition::value(V));
6970
6971 // Clamp operator is not used to utilize a program point CtxI.
6972 T.unionAssumed(AA.getAssumedConstantRange(A, CtxI));
6973
6974 return T.isValidState();
6975 }
6976
Johannes Doerfert81554392020-02-09 20:14:35 -06006977 SmallVector<const AAValueConstantRange *, 4> QuerriedAAs;
6978 if (auto *BinOp = dyn_cast<BinaryOperator>(I)) {
6979 if (!calculateBinaryOperator(A, BinOp, T, CtxI, QuerriedAAs))
6980 return false;
6981 } else if (auto *CmpI = dyn_cast<CmpInst>(I)) {
6982 if (!calculateCmpInst(A, CmpI, T, CtxI, QuerriedAAs))
6983 return false;
6984 } else if (auto *CastI = dyn_cast<CastInst>(I)) {
6985 if (!calculateCastInst(A, CastI, T, CtxI, QuerriedAAs))
6986 return false;
6987 } else {
Hideto Ueno188f9a32020-01-15 15:25:52 +09006988 // Give up with other instructions.
6989 // TODO: Add other instructions
6990
6991 T.indicatePessimisticFixpoint();
6992 return false;
6993 }
Johannes Doerfert81554392020-02-09 20:14:35 -06006994
6995 // Catch circular reasoning in a pessimistic way for now.
6996 // TODO: Check how the range evolves and if we stripped anything, see also
6997 // AADereferenceable or AAAlign for similar situations.
6998 for (const AAValueConstantRange *QueriedAA : QuerriedAAs) {
6999 if (QueriedAA != this)
7000 continue;
7001 // If we are in a stady state we do not need to worry.
7002 if (T.getAssumed() == getState().getAssumed())
7003 continue;
7004 T.indicatePessimisticFixpoint();
7005 }
7006
7007 return T.isValidState();
Hideto Ueno188f9a32020-01-15 15:25:52 +09007008 };
7009
7010 IntegerRangeState T(getBitWidth());
7011
7012 if (!genericValueTraversal<AAValueConstantRange, IntegerRangeState>(
7013 A, getIRPosition(), *this, T, VisitValueCB))
7014 return indicatePessimisticFixpoint();
7015
7016 return clampStateAndIndicateChange(getState(), T);
7017 }
7018
7019 /// See AbstractAttribute::trackStatistics()
7020 void trackStatistics() const override {
7021 STATS_DECLTRACK_FLOATING_ATTR(value_range)
7022 }
7023};
7024
7025struct AAValueConstantRangeFunction : AAValueConstantRangeImpl {
7026 AAValueConstantRangeFunction(const IRPosition &IRP)
7027 : AAValueConstantRangeImpl(IRP) {}
7028
7029 /// See AbstractAttribute::initialize(...).
7030 ChangeStatus updateImpl(Attributor &A) override {
7031 llvm_unreachable("AAValueConstantRange(Function|CallSite)::updateImpl will "
7032 "not be called");
7033 }
7034
7035 /// See AbstractAttribute::trackStatistics()
7036 void trackStatistics() const override { STATS_DECLTRACK_FN_ATTR(value_range) }
7037};
7038
7039struct AAValueConstantRangeCallSite : AAValueConstantRangeFunction {
7040 AAValueConstantRangeCallSite(const IRPosition &IRP)
7041 : AAValueConstantRangeFunction(IRP) {}
7042
7043 /// See AbstractAttribute::trackStatistics()
7044 void trackStatistics() const override { STATS_DECLTRACK_CS_ATTR(value_range) }
7045};
7046
Johannes Doerfert791c9f12020-01-29 18:02:42 -06007047struct AAValueConstantRangeCallSiteReturned
7048 : AACallSiteReturnedFromReturned<AAValueConstantRange,
7049 AAValueConstantRangeImpl> {
Hideto Ueno188f9a32020-01-15 15:25:52 +09007050 AAValueConstantRangeCallSiteReturned(const IRPosition &IRP)
Johannes Doerfert791c9f12020-01-29 18:02:42 -06007051 : AACallSiteReturnedFromReturned<AAValueConstantRange,
7052 AAValueConstantRangeImpl>(IRP) {}
Hideto Ueno188f9a32020-01-15 15:25:52 +09007053
7054 /// See AbstractAttribute::initialize(...).
7055 void initialize(Attributor &A) override {
7056 // If it is a load instruction with range metadata, use the metadata.
7057 if (CallInst *CI = dyn_cast<CallInst>(&getAssociatedValue()))
7058 if (auto *RangeMD = CI->getMetadata(LLVMContext::MD_range))
7059 intersectKnown(getConstantRangeFromMetadata(*RangeMD));
7060
Johannes Doerfert791c9f12020-01-29 18:02:42 -06007061 AAValueConstantRangeImpl::initialize(A);
Hideto Ueno188f9a32020-01-15 15:25:52 +09007062 }
7063
7064 /// See AbstractAttribute::trackStatistics()
7065 void trackStatistics() const override {
7066 STATS_DECLTRACK_CSRET_ATTR(value_range)
7067 }
7068};
7069struct AAValueConstantRangeCallSiteArgument : AAValueConstantRangeFloating {
7070 AAValueConstantRangeCallSiteArgument(const IRPosition &IRP)
7071 : AAValueConstantRangeFloating(IRP) {}
7072
7073 /// See AbstractAttribute::trackStatistics()
7074 void trackStatistics() const override {
7075 STATS_DECLTRACK_CSARG_ATTR(value_range)
7076 }
7077};
Benjamin Kramer564a9de2020-02-17 17:55:03 +01007078
7079} // namespace
Johannes Doerfertaade7822019-06-05 03:02:24 +00007080/// ----------------------------------------------------------------------------
7081/// Attributor
7082/// ----------------------------------------------------------------------------
7083
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007084bool Attributor::isAssumedDead(const AbstractAttribute &AA,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007085 const AAIsDead *FnLivenessAA,
7086 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7087 const IRPosition &IRP = AA.getIRPosition();
7088 if (!Functions.count(IRP.getAnchorScope()))
7089 return false;
7090 return isAssumedDead(IRP, &AA, FnLivenessAA, CheckBBLivenessOnly, DepClass);
7091}
7092
7093bool Attributor::isAssumedDead(const Use &U,
7094 const AbstractAttribute *QueryingAA,
7095 const AAIsDead *FnLivenessAA,
7096 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7097 Instruction *UserI = dyn_cast<Instruction>(U.getUser());
7098 if (!UserI)
7099 return isAssumedDead(IRPosition::value(*U.get()), QueryingAA, FnLivenessAA,
7100 CheckBBLivenessOnly, DepClass);
7101
7102 if (CallSite CS = CallSite(UserI)) {
7103 // For call site argument uses we can check if the argument is
7104 // unused/dead.
7105 if (CS.isArgOperand(&U)) {
7106 const IRPosition &CSArgPos =
7107 IRPosition::callsite_argument(CS, CS.getArgumentNo(&U));
7108 return isAssumedDead(CSArgPos, QueryingAA, FnLivenessAA,
7109 CheckBBLivenessOnly, DepClass);
7110 }
7111 } else if (ReturnInst *RI = dyn_cast<ReturnInst>(UserI)) {
7112 const IRPosition &RetPos = IRPosition::returned(*RI->getFunction());
7113 return isAssumedDead(RetPos, QueryingAA, FnLivenessAA, CheckBBLivenessOnly,
7114 DepClass);
7115 } else if (PHINode *PHI = dyn_cast<PHINode>(UserI)) {
7116 BasicBlock *IncomingBB = PHI->getIncomingBlock(U);
7117 return isAssumedDead(*IncomingBB->getTerminator(), QueryingAA, FnLivenessAA,
7118 CheckBBLivenessOnly, DepClass);
7119 }
7120
7121 return isAssumedDead(IRPosition::value(*UserI), QueryingAA, FnLivenessAA,
7122 CheckBBLivenessOnly, DepClass);
7123}
7124
7125bool Attributor::isAssumedDead(const Instruction &I,
7126 const AbstractAttribute *QueryingAA,
7127 const AAIsDead *FnLivenessAA,
7128 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7129 if (!FnLivenessAA)
7130 FnLivenessAA = lookupAAFor<AAIsDead>(IRPosition::function(*I.getFunction()),
7131 QueryingAA,
7132 /* TrackDependence */ false);
7133
7134 // If we have a context instruction and a liveness AA we use it.
7135 if (FnLivenessAA &&
7136 FnLivenessAA->getIRPosition().getAnchorScope() == I.getFunction() &&
7137 FnLivenessAA->isAssumedDead(&I)) {
7138 if (QueryingAA)
7139 recordDependence(*FnLivenessAA, *QueryingAA, DepClass);
7140 return true;
7141 }
7142
7143 if (CheckBBLivenessOnly)
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007144 return false;
7145
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007146 const AAIsDead &IsDeadAA = getOrCreateAAFor<AAIsDead>(
7147 IRPosition::value(I), QueryingAA, /* TrackDependence */ false);
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00007148 // Don't check liveness for AAIsDead.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007149 if (QueryingAA == &IsDeadAA)
Stefan Stipanovic26121ae2019-08-20 23:16:57 +00007150 return false;
7151
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007152 if (IsDeadAA.isAssumedDead()) {
7153 if (QueryingAA)
7154 recordDependence(IsDeadAA, *QueryingAA, DepClass);
7155 return true;
7156 }
7157
7158 return false;
7159}
7160
7161bool Attributor::isAssumedDead(const IRPosition &IRP,
7162 const AbstractAttribute *QueryingAA,
7163 const AAIsDead *FnLivenessAA,
7164 bool CheckBBLivenessOnly, DepClassTy DepClass) {
7165 Instruction *CtxI = IRP.getCtxI();
7166 if (CtxI &&
7167 isAssumedDead(*CtxI, QueryingAA, FnLivenessAA,
7168 /* CheckBBLivenessOnly */ true,
7169 CheckBBLivenessOnly ? DepClass : DepClassTy::OPTIONAL))
7170 return true;
7171
7172 if (CheckBBLivenessOnly)
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007173 return false;
7174
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007175 // If we haven't succeeded we query the specific liveness info for the IRP.
7176 const AAIsDead *IsDeadAA;
7177 if (IRP.getPositionKind() == IRPosition::IRP_CALL_SITE)
7178 IsDeadAA = &getOrCreateAAFor<AAIsDead>(
7179 IRPosition::callsite_returned(cast<CallBase>(IRP.getAssociatedValue())),
7180 QueryingAA, /* TrackDependence */ false);
7181 else
7182 IsDeadAA = &getOrCreateAAFor<AAIsDead>(IRP, QueryingAA,
7183 /* TrackDependence */ false);
7184 // Don't check liveness for AAIsDead.
7185 if (QueryingAA == IsDeadAA)
7186 return false;
Johannes Doerfert19b00432019-08-26 17:48:05 +00007187
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007188 if (IsDeadAA->isAssumedDead()) {
7189 if (QueryingAA)
7190 recordDependence(*IsDeadAA, *QueryingAA, DepClass);
7191 return true;
7192 }
7193
7194 return false;
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007195}
7196
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007197bool Attributor::checkForAllUses(
7198 const function_ref<bool(const Use &, bool &)> &Pred,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007199 const AbstractAttribute &QueryingAA, const Value &V,
7200 DepClassTy LivenessDepClass) {
Johannes Doerfertb2c76002020-01-23 17:12:56 -06007201
7202 // Check the trivial case first as it catches void values.
7203 if (V.use_empty())
7204 return true;
7205
7206 // If the value is replaced by another one, for now a constant, we do not have
7207 // uses. Note that this requires users of `checkForAllUses` to not recurse but
7208 // instead use the `follow` callback argument to look at transitive users,
7209 // however, that should be clear from the presence of the argument.
7210 bool UsedAssumedInformation = false;
7211 Optional<ConstantInt *> CI =
7212 getAssumedConstant(*this, V, QueryingAA, UsedAssumedInformation);
7213 if (CI.hasValue() && CI.getValue()) {
7214 LLVM_DEBUG(dbgs() << "[Attributor] Value is simplified, uses skipped: " << V
7215 << " -> " << *CI.getValue() << "\n");
7216 return true;
7217 }
7218
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007219 const IRPosition &IRP = QueryingAA.getIRPosition();
7220 SmallVector<const Use *, 16> Worklist;
7221 SmallPtrSet<const Use *, 16> Visited;
7222
7223 for (const Use &U : V.uses())
7224 Worklist.push_back(&U);
7225
7226 LLVM_DEBUG(dbgs() << "[Attributor] Got " << Worklist.size()
7227 << " initial uses to check\n");
7228
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007229 const Function *ScopeFn = IRP.getAnchorScope();
7230 const auto *LivenessAA =
7231 ScopeFn ? &getAAFor<AAIsDead>(QueryingAA, IRPosition::function(*ScopeFn),
7232 /* TrackDependence */ false)
7233 : nullptr;
7234
7235 while (!Worklist.empty()) {
7236 const Use *U = Worklist.pop_back_val();
7237 if (!Visited.insert(U).second)
7238 continue;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007239 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << **U << " in "
7240 << *U->getUser() << "\n");
7241 if (isAssumedDead(*U, &QueryingAA, LivenessAA,
7242 /* CheckBBLivenessOnly */ false, LivenessDepClass)) {
7243 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
7244 continue;
Johannes Doerfert8e629682020-02-11 00:10:35 -06007245 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007246
7247 bool Follow = false;
7248 if (!Pred(*U, Follow))
7249 return false;
7250 if (!Follow)
7251 continue;
7252 for (const Use &UU : U->getUser()->uses())
7253 Worklist.push_back(&UU);
7254 }
7255
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007256 return true;
7257}
7258
Johannes Doerfert661db042019-10-07 23:14:58 +00007259bool Attributor::checkForAllCallSites(
7260 const function_ref<bool(AbstractCallSite)> &Pred,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007261 const AbstractAttribute &QueryingAA, bool RequireAllCallSites,
7262 bool &AllCallSitesKnown) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00007263 // We can try to determine information from
7264 // the call sites. However, this is only possible all call sites are known,
7265 // hence the function has internal linkage.
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007266 const IRPosition &IRP = QueryingAA.getIRPosition();
7267 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfert748538e2019-10-07 23:30:04 +00007268 if (!AssociatedFunction) {
7269 LLVM_DEBUG(dbgs() << "[Attributor] No function associated with " << IRP
7270 << "\n");
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007271 AllCallSitesKnown = false;
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007272 return false;
Johannes Doerfert748538e2019-10-07 23:30:04 +00007273 }
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007274
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007275 return checkForAllCallSites(Pred, *AssociatedFunction, RequireAllCallSites,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007276 &QueryingAA, AllCallSitesKnown);
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007277}
7278
7279bool Attributor::checkForAllCallSites(
7280 const function_ref<bool(AbstractCallSite)> &Pred, const Function &Fn,
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007281 bool RequireAllCallSites, const AbstractAttribute *QueryingAA,
7282 bool &AllCallSitesKnown) {
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007283 if (RequireAllCallSites && !Fn.hasLocalLinkage()) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00007284 LLVM_DEBUG(
7285 dbgs()
Johannes Doerfert3753aa72019-10-13 04:16:02 +00007286 << "[Attributor] Function " << Fn.getName()
Hideto Ueno54869ec2019-07-15 06:49:04 +00007287 << " has no internal linkage, hence not all call sites are known\n");
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007288 AllCallSitesKnown = false;
Hideto Ueno54869ec2019-07-15 06:49:04 +00007289 return false;
7290 }
7291
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007292 // If we do not require all call sites we might not see all.
7293 AllCallSitesKnown = RequireAllCallSites;
7294
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06007295 SmallVector<const Use *, 8> Uses(make_pointer_range(Fn.uses()));
7296 for (unsigned u = 0; u < Uses.size(); ++u) {
7297 const Use &U = *Uses[u];
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007298 LLVM_DEBUG(dbgs() << "[Attributor] Check use: " << *U << " in "
7299 << *U.getUser() << "\n");
7300 if (isAssumedDead(U, QueryingAA, nullptr, /* CheckBBLivenessOnly */ true)) {
7301 LLVM_DEBUG(dbgs() << "[Attributor] Dead use, skip!\n");
7302 continue;
7303 }
Johannes Doerfertc6ac7172020-01-25 20:24:38 -06007304 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(U.getUser())) {
7305 if (CE->isCast() && CE->getType()->isPointerTy() &&
7306 CE->getType()->getPointerElementType()->isFunctionTy()) {
7307 for (const Use &CEU : CE->uses())
7308 Uses.push_back(&CEU);
7309 continue;
7310 }
7311 }
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007312
Johannes Doerfert661db042019-10-07 23:14:58 +00007313 AbstractCallSite ACS(&U);
7314 if (!ACS) {
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007315 LLVM_DEBUG(dbgs() << "[Attributor] Function " << Fn.getName()
Johannes Doerfert661db042019-10-07 23:14:58 +00007316 << " has non call site use " << *U.get() << " in "
7317 << *U.getUser() << "\n");
Johannes Doerfert2d77b0c2019-11-01 22:35:18 -05007318 // BlockAddress users are allowed.
7319 if (isa<BlockAddress>(U.getUser()))
7320 continue;
Johannes Doerfertd98f9752019-08-21 21:48:56 +00007321 return false;
Johannes Doerfert661db042019-10-07 23:14:58 +00007322 }
Johannes Doerfertd98f9752019-08-21 21:48:56 +00007323
Johannes Doerfert661db042019-10-07 23:14:58 +00007324 const Use *EffectiveUse =
7325 ACS.isCallbackCall() ? &ACS.getCalleeUseForCallback() : &U;
7326 if (!ACS.isCallee(EffectiveUse)) {
Hideto Ueno54869ec2019-07-15 06:49:04 +00007327 if (!RequireAllCallSites)
7328 continue;
Johannes Doerfert661db042019-10-07 23:14:58 +00007329 LLVM_DEBUG(dbgs() << "[Attributor] User " << EffectiveUse->getUser()
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007330 << " is an invalid use of " << Fn.getName() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00007331 return false;
7332 }
7333
Johannes Doerfert661db042019-10-07 23:14:58 +00007334 if (Pred(ACS))
Hideto Ueno54869ec2019-07-15 06:49:04 +00007335 continue;
7336
Johannes Doerfert5304b722019-08-14 22:04:28 +00007337 LLVM_DEBUG(dbgs() << "[Attributor] Call site callback failed for "
Johannes Doerfert661db042019-10-07 23:14:58 +00007338 << *ACS.getInstruction() << "\n");
Hideto Ueno54869ec2019-07-15 06:49:04 +00007339 return false;
7340 }
7341
7342 return true;
7343}
7344
Johannes Doerfert14a04932019-08-07 22:27:24 +00007345bool Attributor::checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00007346 const function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)>
Johannes Doerfert14a04932019-08-07 22:27:24 +00007347 &Pred,
7348 const AbstractAttribute &QueryingAA) {
7349
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007350 const IRPosition &IRP = QueryingAA.getIRPosition();
7351 // Since we need to provide return instructions we have to have an exact
7352 // definition.
7353 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00007354 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00007355 return false;
7356
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007357 // If this is a call site query we use the call site specific return values
7358 // and liveness information.
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007359 // TODO: use the function scope once we have call site AAReturnedValues.
7360 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007361 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007362 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007363 return false;
7364
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007365 return AARetVal.checkForAllReturnedValuesAndReturnInsts(Pred);
Johannes Doerfert14a04932019-08-07 22:27:24 +00007366}
7367
7368bool Attributor::checkForAllReturnedValues(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007369 const function_ref<bool(Value &)> &Pred,
Johannes Doerfert14a04932019-08-07 22:27:24 +00007370 const AbstractAttribute &QueryingAA) {
7371
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007372 const IRPosition &IRP = QueryingAA.getIRPosition();
7373 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00007374 if (!AssociatedFunction)
Johannes Doerfert14a04932019-08-07 22:27:24 +00007375 return false;
7376
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007377 // TODO: use the function scope once we have call site AAReturnedValues.
7378 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007379 const auto &AARetVal = getAAFor<AAReturnedValues>(QueryingAA, QueryIRP);
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007380 if (!AARetVal.getState().isValidState())
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007381 return false;
7382
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007383 return AARetVal.checkForAllReturnedValuesAndReturnInsts(
Johannes Doerfert695089e2019-08-23 15:23:49 +00007384 [&](Value &RV, const SmallSetVector<ReturnInst *, 4> &) {
Johannes Doerfertdef99282019-08-14 21:29:37 +00007385 return Pred(RV);
7386 });
Johannes Doerfert14a04932019-08-07 22:27:24 +00007387}
7388
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007389static bool checkForAllInstructionsImpl(
7390 Attributor *A, InformationCache::OpcodeInstMapTy &OpcodeInstMap,
7391 const function_ref<bool(Instruction &)> &Pred,
7392 const AbstractAttribute *QueryingAA, const AAIsDead *LivenessAA,
7393 const ArrayRef<unsigned> &Opcodes, bool CheckBBLivenessOnly = false) {
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007394 for (unsigned Opcode : Opcodes) {
7395 for (Instruction *I : OpcodeInstMap[Opcode]) {
7396 // Skip dead instructions.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007397 if (A && A->isAssumedDead(IRPosition::value(*I), QueryingAA, LivenessAA,
7398 CheckBBLivenessOnly))
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007399 continue;
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007400
7401 if (!Pred(*I))
7402 return false;
7403 }
7404 }
7405 return true;
7406}
7407
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007408bool Attributor::checkForAllInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007409 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007410 const AbstractAttribute &QueryingAA, const ArrayRef<unsigned> &Opcodes,
7411 bool CheckBBLivenessOnly) {
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007412
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007413 const IRPosition &IRP = QueryingAA.getIRPosition();
7414 // Since we need to provide instructions we have to have an exact definition.
7415 const Function *AssociatedFunction = IRP.getAssociatedFunction();
Johannes Doerfertb0412e42019-09-04 16:16:13 +00007416 if (!AssociatedFunction)
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007417 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007418
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007419 // TODO: use the function scope once we have call site AAReturnedValues.
7420 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
Johannes Doerfert19b00432019-08-26 17:48:05 +00007421 const auto &LivenessAA =
7422 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007423
7424 auto &OpcodeInstMap =
7425 InfoCache.getOpcodeInstMapForFunction(*AssociatedFunction);
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007426 if (!checkForAllInstructionsImpl(this, OpcodeInstMap, Pred, &QueryingAA,
7427 &LivenessAA, Opcodes, CheckBBLivenessOnly))
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00007428 return false;
Johannes Doerfertd0f64002019-08-06 00:32:43 +00007429
7430 return true;
7431}
7432
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007433bool Attributor::checkForAllReadWriteInstructions(
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007434 const llvm::function_ref<bool(Instruction &)> &Pred,
Johannes Doerfertece81902019-08-12 22:05:53 +00007435 AbstractAttribute &QueryingAA) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007436
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007437 const Function *AssociatedFunction =
7438 QueryingAA.getIRPosition().getAssociatedFunction();
7439 if (!AssociatedFunction)
7440 return false;
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007441
Johannes Doerfert07a5c122019-08-28 14:09:14 +00007442 // TODO: use the function scope once we have call site AAReturnedValues.
7443 const IRPosition &QueryIRP = IRPosition::function(*AssociatedFunction);
7444 const auto &LivenessAA =
7445 getAAFor<AAIsDead>(QueryingAA, QueryIRP, /* TrackDependence */ false);
Johannes Doerfert710ebb02019-08-14 21:18:01 +00007446
7447 for (Instruction *I :
7448 InfoCache.getReadOrWriteInstsForFunction(*AssociatedFunction)) {
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007449 // Skip dead instructions.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007450 if (isAssumedDead(IRPosition::value(*I), &QueryingAA, &LivenessAA))
Stefan Stipanovicaaa52702019-08-07 18:26:02 +00007451 continue;
7452
7453 if (!Pred(*I))
7454 return false;
7455 }
7456
7457 return true;
7458}
7459
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007460ChangeStatus Attributor::run() {
Johannes Doerfertaade7822019-06-05 03:02:24 +00007461 LLVM_DEBUG(dbgs() << "[Attributor] Identified and initialized "
7462 << AllAbstractAttributes.size()
7463 << " abstract attributes.\n");
7464
Stefan Stipanovic53605892019-06-27 11:27:54 +00007465 // Now that all abstract attributes are collected and initialized we start
7466 // the abstract analysis.
Johannes Doerfertaade7822019-06-05 03:02:24 +00007467
7468 unsigned IterationCounter = 1;
7469
7470 SmallVector<AbstractAttribute *, 64> ChangedAAs;
Johannes Doerfert680f6382019-11-02 02:48:05 -05007471 SetVector<AbstractAttribute *> Worklist, InvalidAAs;
Johannes Doerfertaade7822019-06-05 03:02:24 +00007472 Worklist.insert(AllAbstractAttributes.begin(), AllAbstractAttributes.end());
7473
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007474 bool RecomputeDependences = false;
7475
Johannes Doerfertaade7822019-06-05 03:02:24 +00007476 do {
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00007477 // Remember the size to determine new attributes.
7478 size_t NumAAs = AllAbstractAttributes.size();
Johannes Doerfertaade7822019-06-05 03:02:24 +00007479 LLVM_DEBUG(dbgs() << "\n\n[Attributor] #Iteration: " << IterationCounter
7480 << ", Worklist size: " << Worklist.size() << "\n");
7481
Johannes Doerfert680f6382019-11-02 02:48:05 -05007482 // For invalid AAs we can fix dependent AAs that have a required dependence,
7483 // thereby folding long dependence chains in a single step without the need
7484 // to run updates.
7485 for (unsigned u = 0; u < InvalidAAs.size(); ++u) {
7486 AbstractAttribute *InvalidAA = InvalidAAs[u];
7487 auto &QuerriedAAs = QueryMap[InvalidAA];
7488 LLVM_DEBUG(dbgs() << "[Attributor] InvalidAA: " << *InvalidAA << " has "
7489 << QuerriedAAs.RequiredAAs.size() << "/"
7490 << QuerriedAAs.OptionalAAs.size()
7491 << " required/optional dependences\n");
7492 for (AbstractAttribute *DepOnInvalidAA : QuerriedAAs.RequiredAAs) {
7493 AbstractState &DOIAAState = DepOnInvalidAA->getState();
7494 DOIAAState.indicatePessimisticFixpoint();
7495 ++NumAttributesFixedDueToRequiredDependences;
7496 assert(DOIAAState.isAtFixpoint() && "Expected fixpoint state!");
7497 if (!DOIAAState.isValidState())
7498 InvalidAAs.insert(DepOnInvalidAA);
Johannes Doerfert22408542020-01-28 09:38:07 -06007499 else
7500 ChangedAAs.push_back(DepOnInvalidAA);
Johannes Doerfert680f6382019-11-02 02:48:05 -05007501 }
7502 if (!RecomputeDependences)
7503 Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
7504 QuerriedAAs.OptionalAAs.end());
7505 }
7506
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007507 // If dependences (=QueryMap) are recomputed we have to look at all abstract
7508 // attributes again, regardless of what changed in the last iteration.
7509 if (RecomputeDependences) {
7510 LLVM_DEBUG(
7511 dbgs() << "[Attributor] Run all AAs to recompute dependences\n");
7512 QueryMap.clear();
7513 ChangedAAs.clear();
7514 Worklist.insert(AllAbstractAttributes.begin(),
7515 AllAbstractAttributes.end());
7516 }
7517
Johannes Doerfertaade7822019-06-05 03:02:24 +00007518 // Add all abstract attributes that are potentially dependent on one that
7519 // changed to the work list.
7520 for (AbstractAttribute *ChangedAA : ChangedAAs) {
7521 auto &QuerriedAAs = QueryMap[ChangedAA];
Johannes Doerfert680f6382019-11-02 02:48:05 -05007522 Worklist.insert(QuerriedAAs.OptionalAAs.begin(),
7523 QuerriedAAs.OptionalAAs.end());
7524 Worklist.insert(QuerriedAAs.RequiredAAs.begin(),
7525 QuerriedAAs.RequiredAAs.end());
Johannes Doerfertaade7822019-06-05 03:02:24 +00007526 }
7527
Johannes Doerfertb504eb82019-08-26 18:55:47 +00007528 LLVM_DEBUG(dbgs() << "[Attributor] #Iteration: " << IterationCounter
7529 << ", Worklist+Dependent size: " << Worklist.size()
7530 << "\n");
7531
Johannes Doerfert680f6382019-11-02 02:48:05 -05007532 // Reset the changed and invalid set.
Johannes Doerfertaade7822019-06-05 03:02:24 +00007533 ChangedAAs.clear();
Johannes Doerfert680f6382019-11-02 02:48:05 -05007534 InvalidAAs.clear();
Johannes Doerfertaade7822019-06-05 03:02:24 +00007535
7536 // Update all abstract attribute in the work list and record the ones that
7537 // changed.
7538 for (AbstractAttribute *AA : Worklist)
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007539 if (!AA->getState().isAtFixpoint() &&
7540 !isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true)) {
Johannes Doerfert2dad7292019-10-13 21:10:31 -05007541 QueriedNonFixAA = false;
7542 if (AA->update(*this) == ChangeStatus::CHANGED) {
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007543 ChangedAAs.push_back(AA);
Johannes Doerfert680f6382019-11-02 02:48:05 -05007544 if (!AA->getState().isValidState())
7545 InvalidAAs.insert(AA);
Johannes Doerfert2dad7292019-10-13 21:10:31 -05007546 } else if (!QueriedNonFixAA) {
7547 // If the attribute did not query any non-fix information, the state
7548 // will not change and we can indicate that right away.
7549 AA->getState().indicateOptimisticFixpoint();
7550 }
7551 }
Johannes Doerfertaade7822019-06-05 03:02:24 +00007552
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007553 // Check if we recompute the dependences in the next iteration.
7554 RecomputeDependences = (DepRecomputeInterval > 0 &&
7555 IterationCounter % DepRecomputeInterval == 0);
7556
Johannes Doerfert9543f142019-08-23 15:24:57 +00007557 // Add attributes to the changed set if they have been created in the last
7558 // iteration.
7559 ChangedAAs.append(AllAbstractAttributes.begin() + NumAAs,
7560 AllAbstractAttributes.end());
7561
Johannes Doerfertaade7822019-06-05 03:02:24 +00007562 // Reset the work list and repopulate with the changed abstract attributes.
7563 // Note that dependent ones are added above.
7564 Worklist.clear();
7565 Worklist.insert(ChangedAAs.begin(), ChangedAAs.end());
7566
Johannes Doerfertbf112132019-08-29 01:29:44 +00007567 } while (!Worklist.empty() && (IterationCounter++ < MaxFixpointIterations ||
7568 VerifyMaxFixpointIterations));
Johannes Doerfertf7ca0fe2019-08-28 16:58:52 +00007569
Johannes Doerfertaade7822019-06-05 03:02:24 +00007570 LLVM_DEBUG(dbgs() << "\n[Attributor] Fixpoint iteration done after: "
7571 << IterationCounter << "/" << MaxFixpointIterations
7572 << " iterations\n");
7573
Johannes Doerfertbf112132019-08-29 01:29:44 +00007574 size_t NumFinalAAs = AllAbstractAttributes.size();
Johannes Doerfertb504eb82019-08-26 18:55:47 +00007575
Johannes Doerfertaade7822019-06-05 03:02:24 +00007576 // Reset abstract arguments not settled in a sound fixpoint by now. This
7577 // happens when we stopped the fixpoint iteration early. Note that only the
7578 // ones marked as "changed" *and* the ones transitively depending on them
7579 // need to be reverted to a pessimistic state. Others might not be in a
7580 // fixpoint state but we can use the optimistic results for them anyway.
7581 SmallPtrSet<AbstractAttribute *, 32> Visited;
7582 for (unsigned u = 0; u < ChangedAAs.size(); u++) {
7583 AbstractAttribute *ChangedAA = ChangedAAs[u];
7584 if (!Visited.insert(ChangedAA).second)
7585 continue;
7586
7587 AbstractState &State = ChangedAA->getState();
7588 if (!State.isAtFixpoint()) {
7589 State.indicatePessimisticFixpoint();
7590
7591 NumAttributesTimedOut++;
7592 }
7593
7594 auto &QuerriedAAs = QueryMap[ChangedAA];
Johannes Doerfert680f6382019-11-02 02:48:05 -05007595 ChangedAAs.append(QuerriedAAs.OptionalAAs.begin(),
7596 QuerriedAAs.OptionalAAs.end());
7597 ChangedAAs.append(QuerriedAAs.RequiredAAs.begin(),
7598 QuerriedAAs.RequiredAAs.end());
Johannes Doerfertaade7822019-06-05 03:02:24 +00007599 }
7600
7601 LLVM_DEBUG({
7602 if (!Visited.empty())
7603 dbgs() << "\n[Attributor] Finalized " << Visited.size()
7604 << " abstract attributes.\n";
7605 });
7606
7607 unsigned NumManifested = 0;
7608 unsigned NumAtFixpoint = 0;
7609 ChangeStatus ManifestChange = ChangeStatus::UNCHANGED;
7610 for (AbstractAttribute *AA : AllAbstractAttributes) {
7611 AbstractState &State = AA->getState();
7612
7613 // If there is not already a fixpoint reached, we can now take the
7614 // optimistic state. This is correct because we enforced a pessimistic one
7615 // on abstract attributes that were transitively dependent on a changed one
7616 // already above.
7617 if (!State.isAtFixpoint())
7618 State.indicateOptimisticFixpoint();
7619
7620 // If the state is invalid, we do not try to manifest it.
7621 if (!State.isValidState())
7622 continue;
7623
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007624 // Skip dead code.
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007625 if (isAssumedDead(*AA, nullptr, /* CheckBBLivenessOnly */ true))
Johannes Doerfert9a1a1f92019-08-14 21:25:08 +00007626 continue;
Johannes Doerfertaade7822019-06-05 03:02:24 +00007627 // Manifest the state and record if we changed the IR.
7628 ChangeStatus LocalChange = AA->manifest(*this);
Johannes Doerfertd1b79e02019-08-07 22:46:11 +00007629 if (LocalChange == ChangeStatus::CHANGED && AreStatisticsEnabled())
7630 AA->trackStatistics();
7631
Johannes Doerfertaade7822019-06-05 03:02:24 +00007632 ManifestChange = ManifestChange | LocalChange;
7633
7634 NumAtFixpoint++;
7635 NumManifested += (LocalChange == ChangeStatus::CHANGED);
7636 }
7637
7638 (void)NumManifested;
7639 (void)NumAtFixpoint;
7640 LLVM_DEBUG(dbgs() << "\n[Attributor] Manifested " << NumManifested
7641 << " arguments while " << NumAtFixpoint
7642 << " were in a valid fixpoint state\n");
7643
Johannes Doerfertaade7822019-06-05 03:02:24 +00007644 NumAttributesManifested += NumManifested;
7645 NumAttributesValidFixpoint += NumAtFixpoint;
7646
Fangrui Songf1826172019-08-20 07:21:43 +00007647 (void)NumFinalAAs;
Johannes Doerfertb70297a2020-02-14 10:34:31 -06007648 if (NumFinalAAs != AllAbstractAttributes.size()) {
7649 for (unsigned u = NumFinalAAs; u < AllAbstractAttributes.size(); ++u)
7650 errs() << "Unexpected abstract attribute: " << *AllAbstractAttributes[u]
7651 << " :: "
7652 << AllAbstractAttributes[u]->getIRPosition().getAssociatedValue()
7653 << "\n";
7654 llvm_unreachable("Expected the final number of abstract attributes to "
7655 "remain unchanged!");
7656 }
Johannes Doerfert39681e72019-08-27 04:57:54 +00007657
7658 // Delete stuff at the end to avoid invalid references and a nice order.
Johannes Doerfert2f622062019-09-04 16:35:20 +00007659 {
7660 LLVM_DEBUG(dbgs() << "\n[Attributor] Delete at least "
7661 << ToBeDeletedFunctions.size() << " functions and "
7662 << ToBeDeletedBlocks.size() << " blocks and "
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007663 << ToBeDeletedInsts.size() << " instructions and "
7664 << ToBeChangedUses.size() << " uses\n");
7665
Alina Sbirlea9e66c4e2020-01-23 14:21:08 -08007666 SmallVector<WeakTrackingVH, 32> DeadInsts;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007667 SmallVector<Instruction *, 32> TerminatorsToFold;
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007668
7669 for (auto &It : ToBeChangedUses) {
7670 Use *U = It.first;
7671 Value *NewV = It.second;
7672 Value *OldV = U->get();
7673 LLVM_DEBUG(dbgs() << "Use " << *NewV << " in " << *U->getUser()
7674 << " instead of " << *OldV << "\n");
7675 U->set(NewV);
Johannes Doerfert137c99a2020-02-14 20:06:34 -06007676 // Do not modify call instructions outside the SCC.
7677 if (auto *CB = dyn_cast<CallBase>(OldV))
7678 if (!Functions.count(CB->getCaller()))
7679 continue;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007680 if (Instruction *I = dyn_cast<Instruction>(OldV)) {
7681 CGModifiedFunctions.insert(I->getFunction());
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01007682 if (!isa<PHINode>(I) && !ToBeDeletedInsts.count(I) &&
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007683 isInstructionTriviallyDead(I))
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007684 DeadInsts.push_back(I);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007685 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007686 if (isa<Constant>(NewV) && isa<BranchInst>(U->getUser())) {
7687 Instruction *UserI = cast<Instruction>(U->getUser());
7688 if (isa<UndefValue>(NewV)) {
Hideto Uenocb5eb132019-12-27 02:39:37 +09007689 ToBeChangedToUnreachableInsts.insert(UserI);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007690 } else {
7691 TerminatorsToFold.push_back(UserI);
7692 }
7693 }
Johannes Doerfert2f622062019-09-04 16:35:20 +00007694 }
Johannes Doerferta4088c72020-01-07 16:01:57 -06007695 for (auto &V : InvokeWithDeadSuccessor)
7696 if (InvokeInst *II = dyn_cast_or_null<InvokeInst>(V)) {
7697 bool UnwindBBIsDead = II->hasFnAttr(Attribute::NoUnwind);
7698 bool NormalBBIsDead = II->hasFnAttr(Attribute::NoReturn);
7699 bool Invoke2CallAllowed =
7700 !AAIsDeadFunction::mayCatchAsynchronousExceptions(
7701 *II->getFunction());
7702 assert((UnwindBBIsDead || NormalBBIsDead) &&
7703 "Invoke does not have dead successors!");
7704 BasicBlock *BB = II->getParent();
7705 BasicBlock *NormalDestBB = II->getNormalDest();
7706 if (UnwindBBIsDead) {
7707 Instruction *NormalNextIP = &NormalDestBB->front();
7708 if (Invoke2CallAllowed) {
7709 changeToCall(II);
7710 NormalNextIP = BB->getTerminator();
7711 }
7712 if (NormalBBIsDead)
7713 ToBeChangedToUnreachableInsts.insert(NormalNextIP);
7714 } else {
7715 assert(NormalBBIsDead && "Broken invariant!");
7716 if (!NormalDestBB->getUniquePredecessor())
7717 NormalDestBB = SplitBlockPredecessors(NormalDestBB, {BB}, ".dead");
7718 ToBeChangedToUnreachableInsts.insert(&NormalDestBB->front());
7719 }
7720 }
Johannes Doerfert1e46eb72020-01-07 15:10:30 -06007721 for (auto &V : ToBeChangedToUnreachableInsts)
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007722 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
7723 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfert1e46eb72020-01-07 15:10:30 -06007724 changeToUnreachable(I, /* UseLLVMTrap */ false);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007725 }
7726 for (Instruction *I : TerminatorsToFold) {
7727 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007728 ConstantFoldTerminator(I->getParent());
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007729 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007730
Johannes Doerfert5429c822020-01-11 23:30:36 -06007731 for (auto &V : ToBeDeletedInsts) {
7732 if (Instruction *I = dyn_cast_or_null<Instruction>(V)) {
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007733 CGModifiedFunctions.insert(I->getFunction());
Johannes Doerfertb4352e42020-02-13 20:10:59 -06007734 if (!I->getType()->isVoidTy())
7735 I->replaceAllUsesWith(UndefValue::get(I->getType()));
Johannes Doerfert5429c822020-01-11 23:30:36 -06007736 if (!isa<PHINode>(I) && isInstructionTriviallyDead(I))
7737 DeadInsts.push_back(I);
7738 else
7739 I->eraseFromParent();
7740 }
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007741 }
7742
7743 RecursivelyDeleteTriviallyDeadInstructions(DeadInsts);
Johannes Doerfertb19cd272019-09-03 20:42:16 +00007744
Johannes Doerfert2f622062019-09-04 16:35:20 +00007745 if (unsigned NumDeadBlocks = ToBeDeletedBlocks.size()) {
7746 SmallVector<BasicBlock *, 8> ToBeDeletedBBs;
7747 ToBeDeletedBBs.reserve(NumDeadBlocks);
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007748 for (BasicBlock *BB : ToBeDeletedBlocks) {
7749 CGModifiedFunctions.insert(BB->getParent());
7750 ToBeDeletedBBs.push_back(BB);
7751 }
Johannes Doerfert5e442a52019-10-30 17:34:59 -05007752 // Actually we do not delete the blocks but squash them into a single
7753 // unreachable but untangling branches that jump here is something we need
7754 // to do in a more generic way.
7755 DetatchDeadBlocks(ToBeDeletedBBs, nullptr);
7756 STATS_DECL(AAIsDead, BasicBlock, "Number of dead basic blocks deleted.");
7757 BUILD_STAT_NAME(AAIsDead, BasicBlock) += ToBeDeletedBlocks.size();
Johannes Doerfert2f622062019-09-04 16:35:20 +00007758 }
Johannes Doerfertb19cd272019-09-03 20:42:16 +00007759
Johannes Doerfert2f622062019-09-04 16:35:20 +00007760 // Identify dead internal functions and delete them. This happens outside
7761 // the other fixpoint analysis as we might treat potentially dead functions
7762 // as live to lower the number of iterations. If they happen to be dead, the
7763 // below fixpoint loop will identify and eliminate them.
7764 SmallVector<Function *, 8> InternalFns;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007765 for (Function *F : Functions)
7766 if (F->hasLocalLinkage())
7767 InternalFns.push_back(F);
Johannes Doerfert2f622062019-09-04 16:35:20 +00007768
7769 bool FoundDeadFn = true;
7770 while (FoundDeadFn) {
7771 FoundDeadFn = false;
7772 for (unsigned u = 0, e = InternalFns.size(); u < e; ++u) {
7773 Function *F = InternalFns[u];
7774 if (!F)
7775 continue;
7776
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007777 bool AllCallSitesKnown;
Johannes Doerfert6b9ee2d2020-01-02 16:53:37 -06007778 if (!checkForAllCallSites(
7779 [this](AbstractCallSite ACS) {
7780 return ToBeDeletedFunctions.count(
7781 ACS.getInstruction()->getFunction());
7782 },
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007783 *F, true, nullptr, AllCallSitesKnown))
Johannes Doerfert2f622062019-09-04 16:35:20 +00007784 continue;
7785
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05007786 ToBeDeletedFunctions.insert(F);
Johannes Doerfert2f622062019-09-04 16:35:20 +00007787 InternalFns[u] = nullptr;
7788 FoundDeadFn = true;
7789 }
7790 }
Johannes Doerfert39681e72019-08-27 04:57:54 +00007791 }
7792
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007793 // Rewrite the functions as requested during manifest.
7794 ManifestChange =
7795 ManifestChange | rewriteFunctionSignatures(CGModifiedFunctions);
7796
7797 for (Function *Fn : CGModifiedFunctions)
7798 CGUpdater.reanalyzeFunction(*Fn);
7799
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007800 STATS_DECL(AAIsDead, Function, "Number of dead functions deleted.");
7801 BUILD_STAT_NAME(AAIsDead, Function) += ToBeDeletedFunctions.size();
7802
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007803 for (Function *Fn : ToBeDeletedFunctions)
7804 CGUpdater.removeFunction(*Fn);
Johannes Doerfert6b9ee2d2020-01-02 16:53:37 -06007805
Johannes Doerfertbf112132019-08-29 01:29:44 +00007806 if (VerifyMaxFixpointIterations &&
7807 IterationCounter != MaxFixpointIterations) {
7808 errs() << "\n[Attributor] Fixpoint iteration done after: "
7809 << IterationCounter << "/" << MaxFixpointIterations
7810 << " iterations\n";
7811 llvm_unreachable("The fixpoint was not reached with exactly the number of "
7812 "specified iterations!");
7813 }
7814
Johannes Doerfertaade7822019-06-05 03:02:24 +00007815 return ManifestChange;
7816}
7817
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007818bool Attributor::isValidFunctionSignatureRewrite(
7819 Argument &Arg, ArrayRef<Type *> ReplacementTypes) {
Johannes Doerfert75133632019-10-10 01:39:16 -05007820
7821 auto CallSiteCanBeChanged = [](AbstractCallSite ACS) {
7822 // Forbid must-tail calls for now.
7823 return !ACS.isCallbackCall() && !ACS.getCallSite().isMustTailCall();
7824 };
7825
7826 Function *Fn = Arg.getParent();
7827 // Avoid var-arg functions for now.
7828 if (Fn->isVarArg()) {
7829 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite var-args functions\n");
7830 return false;
7831 }
7832
7833 // Avoid functions with complicated argument passing semantics.
7834 AttributeList FnAttributeList = Fn->getAttributes();
7835 if (FnAttributeList.hasAttrSomewhere(Attribute::Nest) ||
7836 FnAttributeList.hasAttrSomewhere(Attribute::StructRet) ||
7837 FnAttributeList.hasAttrSomewhere(Attribute::InAlloca)) {
7838 LLVM_DEBUG(
7839 dbgs() << "[Attributor] Cannot rewrite due to complex attribute\n");
7840 return false;
7841 }
7842
7843 // Avoid callbacks for now.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06007844 bool AllCallSitesKnown;
7845 if (!checkForAllCallSites(CallSiteCanBeChanged, *Fn, true, nullptr,
7846 AllCallSitesKnown)) {
Johannes Doerfert75133632019-10-10 01:39:16 -05007847 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite all call sites\n");
7848 return false;
7849 }
7850
7851 auto InstPred = [](Instruction &I) {
7852 if (auto *CI = dyn_cast<CallInst>(&I))
7853 return !CI->isMustTailCall();
7854 return true;
7855 };
7856
7857 // Forbid must-tail calls for now.
7858 // TODO:
Johannes Doerfert75133632019-10-10 01:39:16 -05007859 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(*Fn);
Johannes Doerfert23f41f12020-01-12 01:09:22 -06007860 if (!checkForAllInstructionsImpl(nullptr, OpcodeInstMap, InstPred, nullptr,
7861 nullptr, {Instruction::Call})) {
Johannes Doerfert75133632019-10-10 01:39:16 -05007862 LLVM_DEBUG(dbgs() << "[Attributor] Cannot rewrite due to instructions\n");
7863 return false;
7864 }
7865
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007866 return true;
7867}
7868
7869bool Attributor::registerFunctionSignatureRewrite(
7870 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
7871 ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB,
7872 ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB) {
7873 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
7874 << Arg.getParent()->getName() << " with "
7875 << ReplacementTypes.size() << " replacements\n");
7876 assert(isValidFunctionSignatureRewrite(Arg, ReplacementTypes) &&
7877 "Cannot register an invalid rewrite");
7878
7879 Function *Fn = Arg.getParent();
Johannes Doerfert75133632019-10-10 01:39:16 -05007880 SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = ArgumentReplacementMap[Fn];
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007881 if (ARIs.empty())
Johannes Doerfert75133632019-10-10 01:39:16 -05007882 ARIs.resize(Fn->arg_size());
7883
7884 // If we have a replacement already with less than or equal new arguments,
7885 // ignore this request.
7886 ArgumentReplacementInfo *&ARI = ARIs[Arg.getArgNo()];
7887 if (ARI && ARI->getNumReplacementArgs() <= ReplacementTypes.size()) {
7888 LLVM_DEBUG(dbgs() << "[Attributor] Existing rewrite is preferred\n");
7889 return false;
7890 }
7891
7892 // If we have a replacement already but we like the new one better, delete
7893 // the old.
7894 if (ARI)
7895 delete ARI;
7896
Johannes Doerfert89c2e732019-10-30 17:20:20 -05007897 LLVM_DEBUG(dbgs() << "[Attributor] Register new rewrite of " << Arg << " in "
7898 << Arg.getParent()->getName() << " with "
7899 << ReplacementTypes.size() << " replacements\n");
7900
Johannes Doerfert75133632019-10-10 01:39:16 -05007901 // Remember the replacement.
7902 ARI = new ArgumentReplacementInfo(*this, Arg, ReplacementTypes,
7903 std::move(CalleeRepairCB),
7904 std::move(ACSRepairCB));
7905
7906 return true;
7907}
7908
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06007909ChangeStatus Attributor::rewriteFunctionSignatures(
7910 SmallPtrSetImpl<Function *> &ModifiedFns) {
Johannes Doerfert75133632019-10-10 01:39:16 -05007911 ChangeStatus Changed = ChangeStatus::UNCHANGED;
7912
7913 for (auto &It : ArgumentReplacementMap) {
7914 Function *OldFn = It.getFirst();
7915
7916 // Deleted functions do not require rewrites.
7917 if (ToBeDeletedFunctions.count(OldFn))
7918 continue;
7919
7920 const SmallVectorImpl<ArgumentReplacementInfo *> &ARIs = It.getSecond();
7921 assert(ARIs.size() == OldFn->arg_size() && "Inconsistent state!");
7922
7923 SmallVector<Type *, 16> NewArgumentTypes;
7924 SmallVector<AttributeSet, 16> NewArgumentAttributes;
7925
7926 // Collect replacement argument types and copy over existing attributes.
7927 AttributeList OldFnAttributeList = OldFn->getAttributes();
7928 for (Argument &Arg : OldFn->args()) {
7929 if (ArgumentReplacementInfo *ARI = ARIs[Arg.getArgNo()]) {
7930 NewArgumentTypes.append(ARI->ReplacementTypes.begin(),
7931 ARI->ReplacementTypes.end());
7932 NewArgumentAttributes.append(ARI->getNumReplacementArgs(),
7933 AttributeSet());
7934 } else {
7935 NewArgumentTypes.push_back(Arg.getType());
7936 NewArgumentAttributes.push_back(
7937 OldFnAttributeList.getParamAttributes(Arg.getArgNo()));
7938 }
7939 }
7940
7941 FunctionType *OldFnTy = OldFn->getFunctionType();
7942 Type *RetTy = OldFnTy->getReturnType();
7943
7944 // Construct the new function type using the new arguments types.
7945 FunctionType *NewFnTy =
7946 FunctionType::get(RetTy, NewArgumentTypes, OldFnTy->isVarArg());
7947
7948 LLVM_DEBUG(dbgs() << "[Attributor] Function rewrite '" << OldFn->getName()
7949 << "' from " << *OldFn->getFunctionType() << " to "
7950 << *NewFnTy << "\n");
7951
7952 // Create the new function body and insert it into the module.
7953 Function *NewFn = Function::Create(NewFnTy, OldFn->getLinkage(),
7954 OldFn->getAddressSpace(), "");
7955 OldFn->getParent()->getFunctionList().insert(OldFn->getIterator(), NewFn);
7956 NewFn->takeName(OldFn);
7957 NewFn->copyAttributesFrom(OldFn);
7958
7959 // Patch the pointer to LLVM function in debug info descriptor.
7960 NewFn->setSubprogram(OldFn->getSubprogram());
7961 OldFn->setSubprogram(nullptr);
7962
7963 // Recompute the parameter attributes list based on the new arguments for
7964 // the function.
7965 LLVMContext &Ctx = OldFn->getContext();
7966 NewFn->setAttributes(AttributeList::get(
7967 Ctx, OldFnAttributeList.getFnAttributes(),
7968 OldFnAttributeList.getRetAttributes(), NewArgumentAttributes));
7969
7970 // Since we have now created the new function, splice the body of the old
7971 // function right into the new function, leaving the old rotting hulk of the
7972 // function empty.
7973 NewFn->getBasicBlockList().splice(NewFn->begin(),
7974 OldFn->getBasicBlockList());
7975
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06007976 // Set of all "call-like" instructions that invoke the old function mapped
7977 // to their new replacements.
7978 SmallVector<std::pair<CallBase *, CallBase *>, 8> CallSitePairs;
Johannes Doerfert75133632019-10-10 01:39:16 -05007979
7980 // Callback to create a new "call-like" instruction for a given one.
7981 auto CallSiteReplacementCreator = [&](AbstractCallSite ACS) {
7982 CallBase *OldCB = cast<CallBase>(ACS.getInstruction());
7983 const AttributeList &OldCallAttributeList = OldCB->getAttributes();
7984
7985 // Collect the new argument operands for the replacement call site.
7986 SmallVector<Value *, 16> NewArgOperands;
7987 SmallVector<AttributeSet, 16> NewArgOperandAttributes;
7988 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size(); ++OldArgNum) {
7989 unsigned NewFirstArgNum = NewArgOperands.size();
Ilya Biryukov4f82af82019-12-31 10:21:52 +01007990 (void)NewFirstArgNum; // only used inside assert.
Johannes Doerfert75133632019-10-10 01:39:16 -05007991 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
7992 if (ARI->ACSRepairCB)
7993 ARI->ACSRepairCB(*ARI, ACS, NewArgOperands);
7994 assert(ARI->getNumReplacementArgs() + NewFirstArgNum ==
7995 NewArgOperands.size() &&
7996 "ACS repair callback did not provide as many operand as new "
7997 "types were registered!");
7998 // TODO: Exose the attribute set to the ACS repair callback
7999 NewArgOperandAttributes.append(ARI->ReplacementTypes.size(),
8000 AttributeSet());
8001 } else {
8002 NewArgOperands.push_back(ACS.getCallArgOperand(OldArgNum));
8003 NewArgOperandAttributes.push_back(
8004 OldCallAttributeList.getParamAttributes(OldArgNum));
8005 }
8006 }
8007
8008 assert(NewArgOperands.size() == NewArgOperandAttributes.size() &&
8009 "Mismatch # argument operands vs. # argument operand attributes!");
8010 assert(NewArgOperands.size() == NewFn->arg_size() &&
8011 "Mismatch # argument operands vs. # function arguments!");
8012
8013 SmallVector<OperandBundleDef, 4> OperandBundleDefs;
8014 OldCB->getOperandBundlesAsDefs(OperandBundleDefs);
8015
8016 // Create a new call or invoke instruction to replace the old one.
8017 CallBase *NewCB;
8018 if (InvokeInst *II = dyn_cast<InvokeInst>(OldCB)) {
8019 NewCB =
8020 InvokeInst::Create(NewFn, II->getNormalDest(), II->getUnwindDest(),
8021 NewArgOperands, OperandBundleDefs, "", OldCB);
8022 } else {
8023 auto *NewCI = CallInst::Create(NewFn, NewArgOperands, OperandBundleDefs,
8024 "", OldCB);
8025 NewCI->setTailCallKind(cast<CallInst>(OldCB)->getTailCallKind());
8026 NewCB = NewCI;
8027 }
8028
8029 // Copy over various properties and the new attributes.
Johannes Doerfert75133632019-10-10 01:39:16 -05008030 uint64_t W;
8031 if (OldCB->extractProfTotalWeight(W))
8032 NewCB->setProfWeight(W);
8033 NewCB->setCallingConv(OldCB->getCallingConv());
8034 NewCB->setDebugLoc(OldCB->getDebugLoc());
8035 NewCB->takeName(OldCB);
8036 NewCB->setAttributes(AttributeList::get(
8037 Ctx, OldCallAttributeList.getFnAttributes(),
8038 OldCallAttributeList.getRetAttributes(), NewArgOperandAttributes));
8039
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008040 CallSitePairs.push_back({OldCB, NewCB});
Johannes Doerfert75133632019-10-10 01:39:16 -05008041 return true;
8042 };
8043
8044 // Use the CallSiteReplacementCreator to create replacement call sites.
Johannes Doerfert368f7ee2019-12-30 16:12:36 -06008045 bool AllCallSitesKnown;
8046 bool Success = checkForAllCallSites(CallSiteReplacementCreator, *OldFn,
8047 true, nullptr, AllCallSitesKnown);
Ilya Biryukov4f82af82019-12-31 10:21:52 +01008048 (void)Success;
Johannes Doerfert75133632019-10-10 01:39:16 -05008049 assert(Success && "Assumed call site replacement to succeed!");
8050
8051 // Rewire the arguments.
8052 auto OldFnArgIt = OldFn->arg_begin();
8053 auto NewFnArgIt = NewFn->arg_begin();
8054 for (unsigned OldArgNum = 0; OldArgNum < ARIs.size();
8055 ++OldArgNum, ++OldFnArgIt) {
8056 if (ArgumentReplacementInfo *ARI = ARIs[OldArgNum]) {
8057 if (ARI->CalleeRepairCB)
8058 ARI->CalleeRepairCB(*ARI, *NewFn, NewFnArgIt);
8059 NewFnArgIt += ARI->ReplacementTypes.size();
8060 } else {
8061 NewFnArgIt->takeName(&*OldFnArgIt);
8062 OldFnArgIt->replaceAllUsesWith(&*NewFnArgIt);
8063 ++NewFnArgIt;
8064 }
8065 }
8066
8067 // Eliminate the instructions *after* we visited all of them.
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008068 for (auto &CallSitePair : CallSitePairs) {
8069 CallBase &OldCB = *CallSitePair.first;
8070 CallBase &NewCB = *CallSitePair.second;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008071 // We do not modify the call graph here but simply reanalyze the old
8072 // function. This should be revisited once the old PM is gone.
8073 ModifiedFns.insert(OldCB.getFunction());
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008074 OldCB.replaceAllUsesWith(&NewCB);
8075 OldCB.eraseFromParent();
8076 }
Johannes Doerfert75133632019-10-10 01:39:16 -05008077
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008078 // Replace the function in the call graph (if any).
8079 CGUpdater.replaceFunctionWith(*OldFn, *NewFn);
8080
8081 // If the old function was modified and needed to be reanalyzed, the new one
8082 // does now.
8083 if (ModifiedFns.erase(OldFn))
8084 ModifiedFns.insert(NewFn);
Johannes Doerfertd2d2fb12020-01-02 17:20:47 -06008085
Johannes Doerfert75133632019-10-10 01:39:16 -05008086 Changed = ChangeStatus::CHANGED;
8087 }
8088
8089 return Changed;
8090}
8091
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008092void Attributor::initializeInformationCache(Function &F) {
8093
8094 // Walk all instructions to find interesting instructions that might be
8095 // queried by abstract attributes during their initialization or update.
8096 // This has to happen before we create attributes.
8097 auto &ReadOrWriteInsts = InfoCache.FuncRWInstsMap[&F];
8098 auto &InstOpcodeMap = InfoCache.FuncInstOpcodeMap[&F];
8099
8100 for (Instruction &I : instructions(&F)) {
8101 bool IsInterestingOpcode = false;
8102
8103 // To allow easy access to all instructions in a function with a given
8104 // opcode we store them in the InfoCache. As not all opcodes are interesting
8105 // to concrete attributes we only cache the ones that are as identified in
8106 // the following switch.
8107 // Note: There are no concrete attributes now so this is initially empty.
8108 switch (I.getOpcode()) {
8109 default:
8110 assert((!ImmutableCallSite(&I)) && (!isa<CallBase>(&I)) &&
8111 "New call site/base instruction type needs to be known int the "
8112 "Attributor.");
8113 break;
8114 case Instruction::Load:
8115 // The alignment of a pointer is interesting for loads.
8116 case Instruction::Store:
8117 // The alignment of a pointer is interesting for stores.
8118 case Instruction::Call:
8119 case Instruction::CallBr:
8120 case Instruction::Invoke:
8121 case Instruction::CleanupRet:
8122 case Instruction::CatchSwitch:
Johannes Doerfert5732f562019-12-24 19:25:08 -06008123 case Instruction::AtomicRMW:
8124 case Instruction::AtomicCmpXchg:
Hideto Uenoef4febd2019-12-29 17:34:08 +09008125 case Instruction::Br:
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008126 case Instruction::Resume:
8127 case Instruction::Ret:
8128 IsInterestingOpcode = true;
8129 }
8130 if (IsInterestingOpcode)
8131 InstOpcodeMap[I.getOpcode()].push_back(&I);
8132 if (I.mayReadOrWriteMemory())
8133 ReadOrWriteInsts.push_back(&I);
8134 }
8135}
8136
Johannes Doerfert12173e62019-10-13 20:25:25 -05008137void Attributor::recordDependence(const AbstractAttribute &FromAA,
Johannes Doerfert680f6382019-11-02 02:48:05 -05008138 const AbstractAttribute &ToAA,
8139 DepClassTy DepClass) {
Johannes Doerfert2dad7292019-10-13 21:10:31 -05008140 if (FromAA.getState().isAtFixpoint())
8141 return;
8142
Johannes Doerfert680f6382019-11-02 02:48:05 -05008143 if (DepClass == DepClassTy::REQUIRED)
8144 QueryMap[&FromAA].RequiredAAs.insert(
8145 const_cast<AbstractAttribute *>(&ToAA));
8146 else
8147 QueryMap[&FromAA].OptionalAAs.insert(
8148 const_cast<AbstractAttribute *>(&ToAA));
Johannes Doerfert2dad7292019-10-13 21:10:31 -05008149 QueriedNonFixAA = true;
Johannes Doerfert12173e62019-10-13 20:25:25 -05008150}
8151
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00008152void Attributor::identifyDefaultAbstractAttributes(Function &F) {
Johannes Doerfert2f622062019-09-04 16:35:20 +00008153 if (!VisitedFunctions.insert(&F).second)
8154 return;
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05008155 if (F.isDeclaration())
8156 return;
Johannes Doerfertaade7822019-06-05 03:02:24 +00008157
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008158 IRPosition FPos = IRPosition::function(F);
8159
Johannes Doerfert305b9612019-08-04 18:40:01 +00008160 // Check for dead BasicBlocks in every function.
Johannes Doerfert21fe0a32019-08-06 00:55:11 +00008161 // We need dead instruction detection because we do not want to deal with
8162 // broken IR in which SSA rules do not apply.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008163 getOrCreateAAFor<AAIsDead>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00008164
8165 // Every function might be "will-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008166 getOrCreateAAFor<AAWillReturn>(FPos);
Johannes Doerfert305b9612019-08-04 18:40:01 +00008167
Johannes Doerfert58f324a2019-12-24 18:48:50 -06008168 // Every function might contain instructions that cause "undefined behavior".
8169 getOrCreateAAFor<AAUndefinedBehavior>(FPos);
8170
Stefan Stipanovic53605892019-06-27 11:27:54 +00008171 // Every function can be nounwind.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008172 getOrCreateAAFor<AANoUnwind>(FPos);
Stefan Stipanovic53605892019-06-27 11:27:54 +00008173
Stefan Stipanovic06263672019-07-11 21:37:40 +00008174 // Every function might be marked "nosync"
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008175 getOrCreateAAFor<AANoSync>(FPos);
Stefan Stipanovic06263672019-07-11 21:37:40 +00008176
Hideto Ueno65bbaf92019-07-12 17:38:51 +00008177 // Every function might be "no-free".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008178 getOrCreateAAFor<AANoFree>(FPos);
Hideto Ueno65bbaf92019-07-12 17:38:51 +00008179
Johannes Doerferte83f3032019-08-05 23:22:05 +00008180 // Every function might be "no-return".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008181 getOrCreateAAFor<AANoReturn>(FPos);
Johannes Doerferte83f3032019-08-05 23:22:05 +00008182
Hideto Ueno63f60662019-09-21 15:13:19 +00008183 // Every function might be "no-recurse".
8184 getOrCreateAAFor<AANoRecurse>(FPos);
8185
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008186 // Every function might be "readnone/readonly/writeonly/...".
8187 getOrCreateAAFor<AAMemoryBehavior>(FPos);
8188
Johannes Doerfert282f5d72020-01-26 02:51:57 -06008189 // Every function can be "readnone/argmemonly/inaccessiblememonly/...".
8190 getOrCreateAAFor<AAMemoryLocation>(FPos);
8191
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008192 // Every function might be applicable for Heap-To-Stack conversion.
8193 if (EnableHeapToStack)
8194 getOrCreateAAFor<AAHeapToStack>(FPos);
8195
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00008196 // Return attributes are only appropriate if the return type is non void.
8197 Type *ReturnType = F.getReturnType();
8198 if (!ReturnType->isVoidTy()) {
8199 // Argument attribute "returned" --- Create only one per function even
8200 // though it is an argument attribute.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008201 getOrCreateAAFor<AAReturnedValues>(FPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00008202
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008203 IRPosition RetPos = IRPosition::returned(F);
8204
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008205 // Every returned value might be dead.
8206 getOrCreateAAFor<AAIsDead>(RetPos);
8207
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008208 // Every function might be simplified.
8209 getOrCreateAAFor<AAValueSimplify>(RetPos);
8210
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008211 if (ReturnType->isPointerTy()) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008212
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008213 // Every function with pointer return type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008214 getOrCreateAAFor<AAAlign>(RetPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008215
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008216 // Every function with pointer return type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008217 getOrCreateAAFor<AANonNull>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008218
8219 // Every function with pointer return type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008220 getOrCreateAAFor<AANoAlias>(RetPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008221
8222 // Every function with pointer return type might be marked
8223 // dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008224 getOrCreateAAFor<AADereferenceable>(RetPos);
Stefan Stipanovic69ebb022019-07-22 19:36:27 +00008225 }
Hideto Ueno54869ec2019-07-15 06:49:04 +00008226 }
8227
Hideto Ueno54869ec2019-07-15 06:49:04 +00008228 for (Argument &Arg : F.args()) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008229 IRPosition ArgPos = IRPosition::argument(Arg);
8230
8231 // Every argument might be simplified.
8232 getOrCreateAAFor<AAValueSimplify>(ArgPos);
8233
Hideto Ueno19c07af2019-07-23 08:16:17 +00008234 if (Arg.getType()->isPointerTy()) {
8235 // Every argument with pointer type might be marked nonnull.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008236 getOrCreateAAFor<AANonNull>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008237
Hideto Uenocbab3342019-08-29 05:52:00 +00008238 // Every argument with pointer type might be marked noalias.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008239 getOrCreateAAFor<AANoAlias>(ArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00008240
Hideto Ueno19c07af2019-07-23 08:16:17 +00008241 // Every argument with pointer type might be marked dereferenceable.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008242 getOrCreateAAFor<AADereferenceable>(ArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008243
8244 // Every argument with pointer type might be marked align.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008245 getOrCreateAAFor<AAAlign>(ArgPos);
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00008246
8247 // Every argument with pointer type might be marked nocapture.
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008248 getOrCreateAAFor<AANoCapture>(ArgPos);
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008249
8250 // Every argument with pointer type might be marked
8251 // "readnone/readonly/writeonly/..."
8252 getOrCreateAAFor<AAMemoryBehavior>(ArgPos);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01008253
8254 // Every argument with pointer type might be marked nofree.
8255 getOrCreateAAFor<AANoFree>(ArgPos);
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008256
8257 // Every argument with pointer type might be privatizable (or promotable)
8258 getOrCreateAAFor<AAPrivatizablePtr>(ArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008259 }
Johannes Doerfertaccd3e82019-07-08 23:27:20 +00008260 }
8261
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008262 auto CallSitePred = [&](Instruction &I) -> bool {
Hideto Ueno54869ec2019-07-15 06:49:04 +00008263 CallSite CS(&I);
Johannes Doerfert86509e82020-01-12 00:34:38 -06008264 IRPosition CSRetPos = IRPosition::callsite_returned(CS);
8265
8266 // Call sites might be dead if they do not have side effects and no live
8267 // users. The return value might be dead if there are no live users.
8268 getOrCreateAAFor<AAIsDead>(CSRetPos);
8269
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008270 if (Function *Callee = CS.getCalledFunction()) {
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05008271 // Skip declerations except if annotations on their call sites were
8272 // explicitly requested.
Johannes Doerfert139c9ef2019-12-13 23:41:02 -06008273 if (!AnnotateDeclarationCallSites && Callee->isDeclaration() &&
8274 !Callee->hasMetadata(LLVMContext::MD_callback))
Johannes Doerfertc36e2eb2019-10-31 20:15:02 -05008275 return true;
8276
8277 if (!Callee->getReturnType()->isVoidTy() && !CS->use_empty()) {
Hideto Ueno188f9a32020-01-15 15:25:52 +09008278
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008279 IRPosition CSRetPos = IRPosition::callsite_returned(CS);
8280
Hideto Ueno188f9a32020-01-15 15:25:52 +09008281 // Call site return integer values might be limited by a constant range.
Johannes Doerfert86509e82020-01-12 00:34:38 -06008282 if (Callee->getReturnType()->isIntegerTy())
Hideto Ueno188f9a32020-01-15 15:25:52 +09008283 getOrCreateAAFor<AAValueConstantRange>(CSRetPos);
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008284 }
8285
Johannes Doerfert28880192019-12-31 00:57:00 -06008286 for (int i = 0, e = CS.getNumArgOperands(); i < e; i++) {
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008287
8288 IRPosition CSArgPos = IRPosition::callsite_argument(CS, i);
8289
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008290 // Every call site argument might be dead.
8291 getOrCreateAAFor<AAIsDead>(CSArgPos);
8292
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008293 // Call site argument might be simplified.
8294 getOrCreateAAFor<AAValueSimplify>(CSArgPos);
8295
Hideto Ueno54869ec2019-07-15 06:49:04 +00008296 if (!CS.getArgument(i)->getType()->isPointerTy())
8297 continue;
8298
8299 // Call site argument attribute "non-null".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008300 getOrCreateAAFor<AANonNull>(CSArgPos);
Hideto Ueno19c07af2019-07-23 08:16:17 +00008301
Hideto Uenocbab3342019-08-29 05:52:00 +00008302 // Call site argument attribute "no-alias".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008303 getOrCreateAAFor<AANoAlias>(CSArgPos);
Hideto Uenocbab3342019-08-29 05:52:00 +00008304
Hideto Ueno19c07af2019-07-23 08:16:17 +00008305 // Call site argument attribute "dereferenceable".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008306 getOrCreateAAFor<AADereferenceable>(CSArgPos);
Hideto Uenoe7bea9b2019-07-28 07:04:01 +00008307
8308 // Call site argument attribute "align".
Johannes Doerfert97fd5822019-09-04 16:26:20 +00008309 getOrCreateAAFor<AAAlign>(CSArgPos);
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01008310
Johannes Doerfert28880192019-12-31 00:57:00 -06008311 // Call site argument attribute
8312 // "readnone/readonly/writeonly/..."
8313 getOrCreateAAFor<AAMemoryBehavior>(CSArgPos);
8314
Hideto Ueno4ecf2552019-12-12 13:42:40 +00008315 // Call site argument attribute "nofree".
8316 getOrCreateAAFor<AANoFree>(CSArgPos);
Hideto Ueno54869ec2019-07-15 06:49:04 +00008317 }
8318 }
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008319 return true;
8320 };
8321
8322 auto &OpcodeInstMap = InfoCache.getOpcodeInstMapForFunction(F);
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008323 bool Success;
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008324 Success = checkForAllInstructionsImpl(
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008325 nullptr, OpcodeInstMap, CallSitePred, nullptr, nullptr,
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008326 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
8327 (unsigned)Instruction::Call});
8328 (void)Success;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008329 assert(Success && "Expected the check call to be successful!");
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008330
8331 auto LoadStorePred = [&](Instruction &I) -> bool {
8332 if (isa<LoadInst>(I))
8333 getOrCreateAAFor<AAAlign>(
8334 IRPosition::value(*cast<LoadInst>(I).getPointerOperand()));
8335 else
8336 getOrCreateAAFor<AAAlign>(
8337 IRPosition::value(*cast<StoreInst>(I).getPointerOperand()));
8338 return true;
8339 };
8340 Success = checkForAllInstructionsImpl(
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008341 nullptr, OpcodeInstMap, LoadStorePred, nullptr, nullptr,
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008342 {(unsigned)Instruction::Load, (unsigned)Instruction::Store});
8343 (void)Success;
Johannes Doerfert23f41f12020-01-12 01:09:22 -06008344 assert(Success && "Expected the check call to be successful!");
Johannes Doerfertaade7822019-06-05 03:02:24 +00008345}
8346
8347/// Helpers to ease debugging through output streams and print calls.
8348///
8349///{
8350raw_ostream &llvm::operator<<(raw_ostream &OS, ChangeStatus S) {
8351 return OS << (S == ChangeStatus::CHANGED ? "changed" : "unchanged");
8352}
8353
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008354raw_ostream &llvm::operator<<(raw_ostream &OS, IRPosition::Kind AP) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00008355 switch (AP) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008356 case IRPosition::IRP_INVALID:
8357 return OS << "inv";
8358 case IRPosition::IRP_FLOAT:
8359 return OS << "flt";
8360 case IRPosition::IRP_RETURNED:
8361 return OS << "fn_ret";
8362 case IRPosition::IRP_CALL_SITE_RETURNED:
8363 return OS << "cs_ret";
8364 case IRPosition::IRP_FUNCTION:
8365 return OS << "fn";
8366 case IRPosition::IRP_CALL_SITE:
8367 return OS << "cs";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008368 case IRPosition::IRP_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00008369 return OS << "arg";
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008370 case IRPosition::IRP_CALL_SITE_ARGUMENT:
Johannes Doerfertaade7822019-06-05 03:02:24 +00008371 return OS << "cs_arg";
Johannes Doerfertaade7822019-06-05 03:02:24 +00008372 }
8373 llvm_unreachable("Unknown attribute position!");
8374}
8375
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008376raw_ostream &llvm::operator<<(raw_ostream &OS, const IRPosition &Pos) {
Johannes Doerfert710ebb02019-08-14 21:18:01 +00008377 const Value &AV = Pos.getAssociatedValue();
8378 return OS << "{" << Pos.getPositionKind() << ":" << AV.getName() << " ["
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008379 << Pos.getAnchorValue().getName() << "@" << Pos.getArgNo() << "]}";
8380}
8381
Johannes Doerfert1a746452019-10-20 22:28:49 -05008382template <typename base_ty, base_ty BestState, base_ty WorstState>
Hideto Ueno188f9a32020-01-15 15:25:52 +09008383raw_ostream &
8384llvm::operator<<(raw_ostream &OS,
8385 const IntegerStateBase<base_ty, BestState, WorstState> &S) {
Johannes Doerfertacc80792019-08-12 22:07:34 +00008386 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
8387 << static_cast<const AbstractState &>(S);
8388}
8389
Hideto Ueno188f9a32020-01-15 15:25:52 +09008390raw_ostream &llvm::operator<<(raw_ostream &OS, const IntegerRangeState &S) {
8391 OS << "range-state(" << S.getBitWidth() << ")<";
8392 S.getKnown().print(OS);
8393 OS << " / ";
8394 S.getAssumed().print(OS);
8395 OS << ">";
8396
8397 return OS << static_cast<const AbstractState &>(S);
8398}
8399
Johannes Doerfertaade7822019-06-05 03:02:24 +00008400raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractState &S) {
8401 return OS << (!S.isValidState() ? "top" : (S.isAtFixpoint() ? "fix" : ""));
8402}
8403
8404raw_ostream &llvm::operator<<(raw_ostream &OS, const AbstractAttribute &AA) {
8405 AA.print(OS);
8406 return OS;
8407}
8408
8409void AbstractAttribute::print(raw_ostream &OS) const {
Johannes Doerfertfb69f762019-08-05 23:32:31 +00008410 OS << "[P: " << getIRPosition() << "][" << getAsStr() << "][S: " << getState()
8411 << "]";
Johannes Doerfertaade7822019-06-05 03:02:24 +00008412}
8413///}
8414
8415/// ----------------------------------------------------------------------------
8416/// Pass (Manager) Boilerplate
8417/// ----------------------------------------------------------------------------
8418
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008419static bool runAttributorOnFunctions(InformationCache &InfoCache,
8420 SetVector<Function *> &Functions,
8421 AnalysisGetter &AG,
8422 CallGraphUpdater &CGUpdater) {
8423 if (DisableAttributor || Functions.empty())
Johannes Doerfertaade7822019-06-05 03:02:24 +00008424 return false;
8425
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008426 LLVM_DEBUG(dbgs() << "[Attributor] Run on module with " << Functions.size()
Johannes Doerfertaade7822019-06-05 03:02:24 +00008427 << " functions.\n");
8428
8429 // Create an Attributor and initially empty information cache that is filled
8430 // while we identify default attribute opportunities.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008431 Attributor A(Functions, InfoCache, CGUpdater, DepRecInterval);
Johannes Doerfertaade7822019-06-05 03:02:24 +00008432
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008433 for (Function *F : Functions)
8434 A.initializeInformationCache(*F);
Johannes Doerfert3ab9e8b2019-09-17 10:52:41 +00008435
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008436 for (Function *F : Functions) {
8437 if (F->hasExactDefinition())
Johannes Doerfertb0412e42019-09-04 16:16:13 +00008438 NumFnWithExactDefinition++;
8439 else
Johannes Doerfertaade7822019-06-05 03:02:24 +00008440 NumFnWithoutExactDefinition++;
Johannes Doerfertaade7822019-06-05 03:02:24 +00008441
Johannes Doerfert2f622062019-09-04 16:35:20 +00008442 // We look at internal functions only on-demand but if any use is not a
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008443 // direct call or outside the current set of analyzed functions, we have to
8444 // do it eagerly.
8445 if (F->hasLocalLinkage()) {
8446 if (llvm::all_of(F->uses(), [&Functions](const Use &U) {
8447 ImmutableCallSite ICS(U.getUser());
8448 return ICS && ICS.isCallee(&U) &&
8449 Functions.count(const_cast<Function *>(ICS.getCaller()));
Johannes Doerfert2f622062019-09-04 16:35:20 +00008450 }))
8451 continue;
8452 }
8453
Johannes Doerfertaade7822019-06-05 03:02:24 +00008454 // Populate the Attributor with abstract attribute opportunities in the
8455 // function and the information cache with IR information.
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008456 A.identifyDefaultAbstractAttributes(*F);
Johannes Doerfertaade7822019-06-05 03:02:24 +00008457 }
8458
Johannes Doerfert77a9e612020-01-11 23:36:17 -06008459 ChangeStatus Changed = A.run();
Fangrui Songfd5665a2020-02-14 21:47:19 -08008460 assert(!verifyModule(*Functions.front()->getParent(), &errs()) &&
8461 "Module verification failed!");
Johannes Doerfert77a9e612020-01-11 23:36:17 -06008462 LLVM_DEBUG(dbgs() << "[Attributor] Done with " << Functions.size()
8463 << " functions, result: " << Changed << ".\n");
8464 return Changed == ChangeStatus::CHANGED;
Johannes Doerfertaade7822019-06-05 03:02:24 +00008465}
8466
8467PreservedAnalyses AttributorPass::run(Module &M, ModuleAnalysisManager &AM) {
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008468 FunctionAnalysisManager &FAM =
8469 AM.getResult<FunctionAnalysisManagerModuleProxy>(M).getManager();
8470 AnalysisGetter AG(FAM);
8471
8472 SetVector<Function *> Functions;
8473 for (Function &F : M)
8474 Functions.insert(&F);
8475
8476 CallGraphUpdater CGUpdater;
8477 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
8478 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
8479 // FIXME: Think about passes we will preserve and add them here.
8480 return PreservedAnalyses::none();
8481 }
8482 return PreservedAnalyses::all();
8483}
8484
8485PreservedAnalyses AttributorCGSCCPass::run(LazyCallGraph::SCC &C,
8486 CGSCCAnalysisManager &AM,
8487 LazyCallGraph &CG,
8488 CGSCCUpdateResult &UR) {
8489 FunctionAnalysisManager &FAM =
8490 AM.getResult<FunctionAnalysisManagerCGSCCProxy>(C, CG).getManager();
8491 AnalysisGetter AG(FAM);
8492
8493 SetVector<Function *> Functions;
8494 for (LazyCallGraph::Node &N : C)
8495 Functions.insert(&N.getFunction());
8496
8497 if (Functions.empty())
8498 return PreservedAnalyses::all();
8499
8500 Module &M = *Functions.back()->getParent();
8501 CallGraphUpdater CGUpdater;
8502 CGUpdater.initialize(CG, C, AM, UR);
8503 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
8504 if (runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater)) {
Johannes Doerfertaade7822019-06-05 03:02:24 +00008505 // FIXME: Think about passes we will preserve and add them here.
8506 return PreservedAnalyses::none();
8507 }
8508 return PreservedAnalyses::all();
8509}
8510
8511namespace {
8512
8513struct AttributorLegacyPass : public ModulePass {
8514 static char ID;
8515
8516 AttributorLegacyPass() : ModulePass(ID) {
8517 initializeAttributorLegacyPassPass(*PassRegistry::getPassRegistry());
8518 }
8519
8520 bool runOnModule(Module &M) override {
8521 if (skipModule(M))
8522 return false;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008523
Hideto Ueno3bb5cbc2019-09-17 05:45:18 +00008524 AnalysisGetter AG;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008525 SetVector<Function *> Functions;
8526 for (Function &F : M)
8527 Functions.insert(&F);
8528
8529 CallGraphUpdater CGUpdater;
8530 InformationCache InfoCache(M, AG, /* CGSCC */ nullptr);
8531 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
Johannes Doerfertaade7822019-06-05 03:02:24 +00008532 }
8533
8534 void getAnalysisUsage(AnalysisUsage &AU) const override {
8535 // FIXME: Think about passes we will preserve and add them here.
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008536 AU.addRequired<TargetLibraryInfoWrapperPass>();
Johannes Doerfertaade7822019-06-05 03:02:24 +00008537 }
8538};
8539
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008540struct AttributorCGSCCLegacyPass : public CallGraphSCCPass {
8541 CallGraphUpdater CGUpdater;
8542 static char ID;
8543
8544 AttributorCGSCCLegacyPass() : CallGraphSCCPass(ID) {
8545 initializeAttributorCGSCCLegacyPassPass(*PassRegistry::getPassRegistry());
8546 }
8547
8548 bool runOnSCC(CallGraphSCC &SCC) override {
8549 if (skipSCC(SCC))
8550 return false;
8551
8552 SetVector<Function *> Functions;
8553 for (CallGraphNode *CGN : SCC)
8554 if (Function *Fn = CGN->getFunction())
8555 if (!Fn->isDeclaration())
8556 Functions.insert(Fn);
8557
8558 if (Functions.empty())
8559 return false;
8560
8561 AnalysisGetter AG;
8562 CallGraph &CG = const_cast<CallGraph &>(SCC.getCallGraph());
8563 CGUpdater.initialize(CG, SCC);
8564 Module &M = *Functions.back()->getParent();
8565 InformationCache InfoCache(M, AG, /* CGSCC */ &Functions);
8566 return runAttributorOnFunctions(InfoCache, Functions, AG, CGUpdater);
8567 }
8568
8569 bool doFinalization(CallGraph &CG) override { return CGUpdater.finalize(); }
8570
8571 void getAnalysisUsage(AnalysisUsage &AU) const override {
8572 // FIXME: Think about passes we will preserve and add them here.
8573 AU.addRequired<TargetLibraryInfoWrapperPass>();
8574 CallGraphSCCPass::getAnalysisUsage(AU);
8575 }
8576};
8577
Johannes Doerfertaade7822019-06-05 03:02:24 +00008578} // end anonymous namespace
8579
8580Pass *llvm::createAttributorLegacyPass() { return new AttributorLegacyPass(); }
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008581Pass *llvm::createAttributorCGSCCLegacyPass() {
8582 return new AttributorCGSCCLegacyPass();
8583}
Johannes Doerfertaade7822019-06-05 03:02:24 +00008584
8585char AttributorLegacyPass::ID = 0;
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008586char AttributorCGSCCLegacyPass::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008587
8588const char AAReturnedValues::ID = 0;
8589const char AANoUnwind::ID = 0;
8590const char AANoSync::ID = 0;
Johannes Doerferteccdf082019-08-05 23:35:12 +00008591const char AANoFree::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008592const char AANonNull::ID = 0;
8593const char AANoRecurse::ID = 0;
8594const char AAWillReturn::ID = 0;
Johannes Doerfert58f324a2019-12-24 18:48:50 -06008595const char AAUndefinedBehavior::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008596const char AANoAlias::ID = 0;
Pankaj Gode04945c92019-11-22 18:40:47 +05308597const char AAReachability::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008598const char AANoReturn::ID = 0;
8599const char AAIsDead::ID = 0;
8600const char AADereferenceable::ID = 0;
8601const char AAAlign::ID = 0;
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00008602const char AANoCapture::ID = 0;
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008603const char AAValueSimplify::ID = 0;
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008604const char AAHeapToStack::ID = 0;
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008605const char AAPrivatizablePtr::ID = 0;
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008606const char AAMemoryBehavior::ID = 0;
Johannes Doerfert282f5d72020-01-26 02:51:57 -06008607const char AAMemoryLocation::ID = 0;
Hideto Ueno188f9a32020-01-15 15:25:52 +09008608const char AAValueConstantRange::ID = 0;
Johannes Doerfert24020622019-08-05 23:30:01 +00008609
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008610// Macro magic to create the static generator function for attributes that
8611// follow the naming scheme.
8612
8613#define SWITCH_PK_INV(CLASS, PK, POS_NAME) \
8614 case IRPosition::PK: \
8615 llvm_unreachable("Cannot create " #CLASS " for a " POS_NAME " position!");
8616
8617#define SWITCH_PK_CREATE(CLASS, IRP, PK, SUFFIX) \
8618 case IRPosition::PK: \
8619 AA = new CLASS##SUFFIX(IRP); \
8620 break;
8621
8622#define CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8623 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8624 CLASS *AA = nullptr; \
8625 switch (IRP.getPositionKind()) { \
8626 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8627 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
8628 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
8629 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
8630 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
8631 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
8632 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8633 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
8634 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008635 return *AA; \
8636 }
8637
8638#define CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8639 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8640 CLASS *AA = nullptr; \
8641 switch (IRP.getPositionKind()) { \
8642 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8643 SWITCH_PK_INV(CLASS, IRP_FUNCTION, "function") \
8644 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
8645 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
8646 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
8647 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
8648 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
8649 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
8650 } \
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008651 return *AA; \
8652 }
8653
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008654#define CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8655 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8656 CLASS *AA = nullptr; \
8657 switch (IRP.getPositionKind()) { \
8658 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8659 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8660 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
8661 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
8662 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
8663 SWITCH_PK_CREATE(CLASS, IRP, IRP_RETURNED, Returned) \
8664 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
8665 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
8666 } \
8667 return *AA; \
8668 }
8669
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008670#define CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8671 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8672 CLASS *AA = nullptr; \
8673 switch (IRP.getPositionKind()) { \
8674 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8675 SWITCH_PK_INV(CLASS, IRP_ARGUMENT, "argument") \
8676 SWITCH_PK_INV(CLASS, IRP_FLOAT, "floating") \
8677 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
8678 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_RETURNED, "call site returned") \
8679 SWITCH_PK_INV(CLASS, IRP_CALL_SITE_ARGUMENT, "call site argument") \
8680 SWITCH_PK_INV(CLASS, IRP_CALL_SITE, "call site") \
8681 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8682 } \
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008683 return *AA; \
8684 }
8685
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008686#define CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(CLASS) \
8687 CLASS &CLASS::createForPosition(const IRPosition &IRP, Attributor &A) { \
8688 CLASS *AA = nullptr; \
8689 switch (IRP.getPositionKind()) { \
8690 SWITCH_PK_INV(CLASS, IRP_INVALID, "invalid") \
8691 SWITCH_PK_INV(CLASS, IRP_RETURNED, "returned") \
8692 SWITCH_PK_CREATE(CLASS, IRP, IRP_FUNCTION, Function) \
8693 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE, CallSite) \
8694 SWITCH_PK_CREATE(CLASS, IRP, IRP_FLOAT, Floating) \
8695 SWITCH_PK_CREATE(CLASS, IRP, IRP_ARGUMENT, Argument) \
8696 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_RETURNED, CallSiteReturned) \
8697 SWITCH_PK_CREATE(CLASS, IRP, IRP_CALL_SITE_ARGUMENT, CallSiteArgument) \
8698 } \
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008699 return *AA; \
8700 }
8701
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008702CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoUnwind)
8703CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoSync)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008704CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoRecurse)
8705CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAWillReturn)
8706CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoReturn)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008707CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReturnedValues)
Johannes Doerfert282f5d72020-01-26 02:51:57 -06008708CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryLocation)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008709
8710CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANonNull)
8711CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoAlias)
Johannes Doerfert89c2e732019-10-30 17:20:20 -05008712CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAPrivatizablePtr)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008713CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AADereferenceable)
8714CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAAlign)
Johannes Doerfert7516a5e2019-09-03 20:37:24 +00008715CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoCapture)
Hideto Ueno188f9a32020-01-15 15:25:52 +09008716CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueConstantRange)
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008717
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008718CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAValueSimplify)
Johannes Doerfertcd4aab42019-10-13 03:08:18 -05008719CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAIsDead)
Stefan Stipanovicf35740d2019-11-02 16:35:38 +01008720CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION(AANoFree)
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008721
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008722CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAHeapToStack)
Pankaj Gode04945c92019-11-22 18:40:47 +05308723CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAReachability)
Johannes Doerfert58f324a2019-12-24 18:48:50 -06008724CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAUndefinedBehavior)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008725
Johannes Doerfert1097fab2019-10-07 21:07:57 +00008726CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION(AAMemoryBehavior)
8727
Johannes Doerfertd4bea882019-10-07 23:28:54 +00008728#undef CREATE_FUNCTION_ONLY_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008729#undef CREATE_FUNCTION_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfertd4bea882019-10-07 23:28:54 +00008730#undef CREATE_NON_RET_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008731#undef CREATE_VALUE_ABSTRACT_ATTRIBUTE_FOR_POSITION
Hideto Uenof2b9dc42019-09-07 07:03:05 +00008732#undef CREATE_ALL_ABSTRACT_ATTRIBUTE_FOR_POSITION
Johannes Doerfert12cbbab2019-08-20 06:15:50 +00008733#undef SWITCH_PK_CREATE
8734#undef SWITCH_PK_INV
8735
Johannes Doerfertaade7822019-06-05 03:02:24 +00008736INITIALIZE_PASS_BEGIN(AttributorLegacyPass, "attributor",
8737 "Deduce and propagate attributes", false, false)
Stefan Stipanovic431141c2019-09-15 21:47:41 +00008738INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
Johannes Doerfertaade7822019-06-05 03:02:24 +00008739INITIALIZE_PASS_END(AttributorLegacyPass, "attributor",
8740 "Deduce and propagate attributes", false, false)
Johannes Doerfertb0c77c32019-11-27 00:30:12 -06008741INITIALIZE_PASS_BEGIN(AttributorCGSCCLegacyPass, "attributor-cgscc",
8742 "Deduce and propagate attributes (CGSCC pass)", false,
8743 false)
8744INITIALIZE_PASS_DEPENDENCY(TargetLibraryInfoWrapperPass)
8745INITIALIZE_PASS_DEPENDENCY(CallGraphWrapperPass)
8746INITIALIZE_PASS_END(AttributorCGSCCLegacyPass, "attributor-cgscc",
8747 "Deduce and propagate attributes (CGSCC pass)", false,
8748 false)