LLVM 17.0.0git
Attributor.h
Go to the documentation of this file.
1//===- Attributor.h --- Module-wide attribute deduction ---------*- C++ -*-===//
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// Attributor: An inter procedural (abstract) "attribute" deduction framework.
10//
11// The Attributor framework is an inter procedural abstract analysis (fixpoint
12// iteration analysis). The goal is to allow easy deduction of new attributes as
13// well as information exchange between abstract attributes in-flight.
14//
15// The Attributor class is the driver and the link between the various abstract
16// attributes. The Attributor will iterate until a fixpoint state is reached by
17// all abstract attributes in-flight, or until it will enforce a pessimistic fix
18// point because an iteration limit is reached.
19//
20// Abstract attributes, derived from the AbstractAttribute class, actually
21// describe properties of the code. They can correspond to actual LLVM-IR
22// attributes, or they can be more general, ultimately unrelated to LLVM-IR
23// attributes. The latter is useful when an abstract attributes provides
24// information to other abstract attributes in-flight but we might not want to
25// manifest the information. The Attributor allows to query in-flight abstract
26// attributes through the `Attributor::getAAFor` method (see the method
27// description for an example). If the method is used by an abstract attribute
28// P, and it results in an abstract attribute Q, the Attributor will
29// automatically capture a potential dependence from Q to P. This dependence
30// will cause P to be reevaluated whenever Q changes in the future.
31//
32// The Attributor will only reevaluate abstract attributes that might have
33// changed since the last iteration. That means that the Attribute will not
34// revisit all instructions/blocks/functions in the module but only query
35// an update from a subset of the abstract attributes.
36//
37// The update method `AbstractAttribute::updateImpl` is implemented by the
38// specific "abstract attribute" subclasses. The method is invoked whenever the
39// currently assumed state (see the AbstractState class) might not be valid
40// anymore. This can, for example, happen if the state was dependent on another
41// abstract attribute that changed. In every invocation, the update method has
42// to adjust the internal state of an abstract attribute to a point that is
43// justifiable by the underlying IR and the current state of abstract attributes
44// in-flight. Since the IR is given and assumed to be valid, the information
45// derived from it can be assumed to hold. However, information derived from
46// other abstract attributes is conditional on various things. If the justifying
47// state changed, the `updateImpl` has to revisit the situation and potentially
48// find another justification or limit the optimistic assumes made.
49//
50// Change is the key in this framework. Until a state of no-change, thus a
51// fixpoint, is reached, the Attributor will query the abstract attributes
52// in-flight to re-evaluate their state. If the (current) state is too
53// optimistic, hence it cannot be justified anymore through other abstract
54// attributes or the state of the IR, the state of the abstract attribute will
55// have to change. Generally, we assume abstract attribute state to be a finite
56// height lattice and the update function to be monotone. However, these
57// conditions are not enforced because the iteration limit will guarantee
58// termination. If an optimistic fixpoint is reached, or a pessimistic fix
59// point is enforced after a timeout, the abstract attributes are tasked to
60// manifest their result in the IR for passes to come.
61//
62// Attribute manifestation is not mandatory. If desired, there is support to
63// generate a single or multiple LLVM-IR attributes already in the helper struct
64// IRAttribute. In the simplest case, a subclass inherits from IRAttribute with
65// a proper Attribute::AttrKind as template parameter. The Attributor
66// manifestation framework will then create and place a new attribute if it is
67// allowed to do so (based on the abstract state). Other use cases can be
68// achieved by overloading AbstractAttribute or IRAttribute methods.
69//
70//
71// The "mechanics" of adding a new "abstract attribute":
72// - Define a class (transitively) inheriting from AbstractAttribute and one
73// (which could be the same) that (transitively) inherits from AbstractState.
74// For the latter, consider the already available BooleanState and
75// {Inc,Dec,Bit}IntegerState if they fit your needs, e.g., you require only a
76// number tracking or bit-encoding.
77// - Implement all pure methods. Also use overloading if the attribute is not
78// conforming with the "default" behavior: A (set of) LLVM-IR attribute(s) for
79// an argument, call site argument, function return value, or function. See
80// the class and method descriptions for more information on the two
81// "Abstract" classes and their respective methods.
82// - Register opportunities for the new abstract attribute in the
83// `Attributor::identifyDefaultAbstractAttributes` method if it should be
84// counted as a 'default' attribute.
85// - Add sufficient tests.
86// - Add a Statistics object for bookkeeping. If it is a simple (set of)
87// attribute(s) manifested through the Attributor manifestation framework, see
88// the bookkeeping function in Attributor.cpp.
89// - If instructions with a certain opcode are interesting to the attribute, add
90// that opcode to the switch in `Attributor::identifyAbstractAttributes`. This
91// will make it possible to query all those instructions through the
92// `InformationCache::getOpcodeInstMapForFunction` interface and eliminate the
93// need to traverse the IR repeatedly.
94//
95//===----------------------------------------------------------------------===//
96
97#ifndef LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
98#define LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
99
100#include "llvm/ADT/DenseSet.h"
101#include "llvm/ADT/GraphTraits.h"
102#include "llvm/ADT/MapVector.h"
103#include "llvm/ADT/STLExtras.h"
105#include "llvm/ADT/SetVector.h"
106#include "llvm/ADT/iterator.h"
108#include "llvm/Analysis/CFG.h"
119#include "llvm/IR/Constants.h"
120#include "llvm/IR/InstIterator.h"
121#include "llvm/IR/Instruction.h"
122#include "llvm/IR/PassManager.h"
123#include "llvm/IR/Value.h"
126#include "llvm/Support/Casting.h"
131
132#include <limits>
133#include <map>
134#include <optional>
135
136namespace llvm {
137
138class DataLayout;
139class LLVMContext;
140class Pass;
141template <typename Fn> class function_ref;
142struct AADepGraphNode;
143struct AADepGraph;
144struct Attributor;
145struct AbstractAttribute;
146struct InformationCache;
147struct AAIsDead;
148struct AttributorCallGraph;
149struct IRPosition;
150
151class AAResults;
152class Function;
153
154/// Abstract Attribute helper functions.
155namespace AA {
157
158enum class GPUAddressSpace : unsigned {
159 Generic = 0,
160 Global = 1,
161 Shared = 3,
162 Constant = 4,
163 Local = 5,
164};
165
166/// Flags to distinguish intra-procedural queries from *potentially*
167/// inter-procedural queries. Not that information can be valid for both and
168/// therefore both bits might be set.
169enum ValueScope : uint8_t {
173};
174
175struct ValueAndContext : public std::pair<Value *, const Instruction *> {
176 using Base = std::pair<Value *, const Instruction *>;
178 ValueAndContext(Value &V, const Instruction *CtxI) : Base(&V, CtxI) {}
179 ValueAndContext(Value &V, const Instruction &CtxI) : Base(&V, &CtxI) {}
180
181 Value *getValue() const { return this->first; }
182 const Instruction *getCtxI() const { return this->second; }
183};
184
185/// Return true if \p I is a `nosync` instruction. Use generic reasoning and
186/// potentially the corresponding AANoSync.
188 const AbstractAttribute &QueryingAA);
189
190/// Return true if \p V is dynamically unique, that is, there are no two
191/// "instances" of \p V at runtime with different values.
192/// Note: If \p ForAnalysisOnly is set we only check that the Attributor will
193/// never use \p V to represent two "instances" not that \p V could not
194/// technically represent them.
195bool isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA,
196 const Value &V, bool ForAnalysisOnly = true);
197
198/// Return true if \p V is a valid value in \p Scope, that is a constant or an
199/// instruction/argument of \p Scope.
200bool isValidInScope(const Value &V, const Function *Scope);
201
202/// Return true if the value of \p VAC is a valid at the position of \p VAC,
203/// that is a constant, an argument of the same function, or an instruction in
204/// that function that dominates the position.
205bool isValidAtPosition(const ValueAndContext &VAC, InformationCache &InfoCache);
206
207/// Try to convert \p V to type \p Ty without introducing new instructions. If
208/// this is not possible return `nullptr`. Note: this function basically knows
209/// how to cast various constants.
210Value *getWithType(Value &V, Type &Ty);
211
212/// Return the combination of \p A and \p B such that the result is a possible
213/// value of both. \p B is potentially casted to match the type \p Ty or the
214/// type of \p A if \p Ty is null.
215///
216/// Examples:
217/// X + none => X
218/// not_none + undef => not_none
219/// V1 + V2 => nullptr
220std::optional<Value *>
221combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
222 const std::optional<Value *> &B, Type *Ty);
223
224/// Helper to represent an access offset and size, with logic to deal with
225/// uncertainty and check for overlapping accesses.
226struct RangeTy {
228 int64_t Size = Unassigned;
229
230 RangeTy(int64_t Offset, int64_t Size) : Offset(Offset), Size(Size) {}
231 RangeTy() = default;
232 static RangeTy getUnknown() { return RangeTy{Unknown, Unknown}; }
233
234 /// Return true if offset or size are unknown.
237 }
238
239 /// Return true if offset and size are unknown, thus this is the default
240 /// unknown object.
243 }
244
245 /// Return true if the offset and size are unassigned.
246 bool isUnassigned() const {
248 "Inconsistent state!");
249 return Offset == RangeTy::Unassigned;
250 }
251
252 /// Return true if this offset and size pair might describe an address that
253 /// overlaps with \p Range.
254 bool mayOverlap(const RangeTy &Range) const {
255 // Any unknown value and we are giving up -> overlap.
256 if (offsetOrSizeAreUnknown() || Range.offsetOrSizeAreUnknown())
257 return true;
258
259 // Check if one offset point is in the other interval [offset,
260 // offset+size].
261 return Range.Offset + Range.Size > Offset && Range.Offset < Offset + Size;
262 }
263
265 if (Offset == Unassigned)
266 Offset = R.Offset;
267 else if (R.Offset != Unassigned && R.Offset != Offset)
268 Offset = Unknown;
269
270 if (Size == Unassigned)
271 Size = R.Size;
272 else if (Size == Unknown || R.Size == Unknown)
273 Size = Unknown;
274 else if (R.Size != Unassigned)
275 Size = std::max(Size, R.Size);
276
277 return *this;
278 }
279
280 /// Comparison for sorting ranges by offset.
281 ///
282 /// Returns true if the offset \p L is less than that of \p R.
283 inline static bool OffsetLessThan(const RangeTy &L, const RangeTy &R) {
284 return L.Offset < R.Offset;
285 }
286
287 /// Constants used to represent special offsets or sizes.
288 /// - We cannot assume that Offsets and Size are non-negative.
289 /// - The constants should not clash with DenseMapInfo, such as EmptyKey
290 /// (INT64_MAX) and TombstoneKey (INT64_MIN).
291 /// We use values "in the middle" of the 64 bit range to represent these
292 /// special cases.
293 static constexpr int64_t Unassigned = std::numeric_limits<int32_t>::min();
294 static constexpr int64_t Unknown = std::numeric_limits<int32_t>::max();
295};
296
298 OS << "[" << R.Offset << ", " << R.Size << "]";
299 return OS;
300}
301
302inline bool operator==(const RangeTy &A, const RangeTy &B) {
303 return A.Offset == B.Offset && A.Size == B.Size;
304}
305
306inline bool operator!=(const RangeTy &A, const RangeTy &B) { return !(A == B); }
307
308/// Return the initial value of \p Obj with type \p Ty if that is a constant.
310 const TargetLibraryInfo *TLI,
311 const DataLayout &DL,
312 RangeTy *RangePtr = nullptr);
313
314/// Collect all potential values \p LI could read into \p PotentialValues. That
315/// is, the only values read by \p LI are assumed to be known and all are in
316/// \p PotentialValues. \p PotentialValueOrigins will contain all the
317/// instructions that might have put a potential value into \p PotentialValues.
318/// Dependences onto \p QueryingAA are properly tracked, \p
319/// UsedAssumedInformation will inform the caller if assumed information was
320/// used.
321///
322/// \returns True if the assumed potential copies are all in \p PotentialValues,
323/// false if something went wrong and the copies could not be
324/// determined.
326 Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
327 SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
328 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
329 bool OnlyExact = false);
330
331/// Collect all potential values of the one stored by \p SI into
332/// \p PotentialCopies. That is, the only copies that were made via the
333/// store are assumed to be known and all are in \p PotentialCopies. Dependences
334/// onto \p QueryingAA are properly tracked, \p UsedAssumedInformation will
335/// inform the caller if assumed information was used.
336///
337/// \returns True if the assumed potential copies are all in \p PotentialCopies,
338/// false if something went wrong and the copies could not be
339/// determined.
341 Attributor &A, StoreInst &SI, SmallSetVector<Value *, 4> &PotentialCopies,
342 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
343 bool OnlyExact = false);
344
345/// Return true if \p IRP is readonly. This will query respective AAs that
346/// deduce the information and introduce dependences for \p QueryingAA.
347bool isAssumedReadOnly(Attributor &A, const IRPosition &IRP,
348 const AbstractAttribute &QueryingAA, bool &IsKnown);
349
350/// Return true if \p IRP is readnone. This will query respective AAs that
351/// deduce the information and introduce dependences for \p QueryingAA.
352bool isAssumedReadNone(Attributor &A, const IRPosition &IRP,
353 const AbstractAttribute &QueryingAA, bool &IsKnown);
354
355/// Return true if \p ToI is potentially reachable from \p FromI without running
356/// into any instruction in \p ExclusionSet The two instructions do not need to
357/// be in the same function. \p GoBackwardsCB can be provided to convey domain
358/// knowledge about the "lifespan" the user is interested in. By default, the
359/// callers of \p FromI are checked as well to determine if \p ToI can be
360/// reached. If the query is not interested in callers beyond a certain point,
361/// e.g., a GPU kernel entry or the function containing an alloca, the
362/// \p GoBackwardsCB should return false.
364 Attributor &A, const Instruction &FromI, const Instruction &ToI,
365 const AbstractAttribute &QueryingAA,
366 const AA::InstExclusionSetTy *ExclusionSet = nullptr,
367 std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
368
369/// Same as above but it is sufficient to reach any instruction in \p ToFn.
371 Attributor &A, const Instruction &FromI, const Function &ToFn,
372 const AbstractAttribute &QueryingAA,
373 const AA::InstExclusionSetTy *ExclusionSet = nullptr,
374 std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
375
376/// Return true if \p Obj is assumed to be a thread local object.
378 const AbstractAttribute &QueryingAA);
379
380/// Return true if \p I is potentially affected by a barrier.
382 const AbstractAttribute &QueryingAA);
384 const AbstractAttribute &QueryingAA,
385 const Instruction *CtxI);
386} // namespace AA
387
388template <>
389struct DenseMapInfo<AA::ValueAndContext>
390 : public DenseMapInfo<AA::ValueAndContext::Base> {
393 return Base::getEmptyKey();
394 }
396 return Base::getTombstoneKey();
397 }
398 static unsigned getHashValue(const AA::ValueAndContext &VAC) {
399 return Base::getHashValue(VAC);
400 }
401
402 static bool isEqual(const AA::ValueAndContext &LHS,
403 const AA::ValueAndContext &RHS) {
404 return Base::isEqual(LHS, RHS);
405 }
406};
407
408template <>
409struct DenseMapInfo<AA::ValueScope> : public DenseMapInfo<unsigned char> {
411 static inline AA::ValueScope getEmptyKey() {
412 return AA::ValueScope(Base::getEmptyKey());
413 }
415 return AA::ValueScope(Base::getTombstoneKey());
416 }
417 static unsigned getHashValue(const AA::ValueScope &S) {
418 return Base::getHashValue(S);
419 }
420
421 static bool isEqual(const AA::ValueScope &LHS, const AA::ValueScope &RHS) {
422 return Base::isEqual(LHS, RHS);
423 }
424};
425
426template <>
427struct DenseMapInfo<const AA::InstExclusionSetTy *>
428 : public DenseMapInfo<void *> {
430 static inline const AA::InstExclusionSetTy *getEmptyKey() {
431 return static_cast<const AA::InstExclusionSetTy *>(super::getEmptyKey());
432 }
434 return static_cast<const AA::InstExclusionSetTy *>(
435 super::getTombstoneKey());
436 }
437 static unsigned getHashValue(const AA::InstExclusionSetTy *BES) {
438 unsigned H = 0;
439 if (BES)
440 for (const auto *II : *BES)
442 return H;
443 }
446 if (LHS == RHS)
447 return true;
448 if (LHS == getEmptyKey() || RHS == getEmptyKey() ||
449 LHS == getTombstoneKey() || RHS == getTombstoneKey())
450 return false;
451 auto SizeLHS = LHS ? LHS->size() : 0;
452 auto SizeRHS = RHS ? RHS->size() : 0;
453 if (SizeLHS != SizeRHS)
454 return false;
455 if (SizeRHS == 0)
456 return true;
457 return llvm::set_is_subset(*LHS, *RHS);
458 }
459};
460
461/// The value passed to the line option that defines the maximal initialization
462/// chain length.
463extern unsigned MaxInitializationChainLength;
464
465///{
466enum class ChangeStatus {
467 CHANGED,
468 UNCHANGED,
469};
470
475
476enum class DepClassTy {
477 REQUIRED, ///< The target cannot be valid if the source is not.
478 OPTIONAL, ///< The target may be valid if the source is not.
479 NONE, ///< Do not track a dependence between source and target.
480};
481///}
482
483/// The data structure for the nodes of a dependency graph
485public:
486 virtual ~AADepGraphNode() = default;
489
490protected:
491 /// Set of dependency graph nodes which should be updated if this one
492 /// is updated. The bit encodes if it is optional.
494
495 static AADepGraphNode *DepGetVal(const DepTy &DT) { return DT.getPointer(); }
497 return cast<AbstractAttribute>(DT.getPointer());
498 }
499
500 operator AbstractAttribute *() { return cast<AbstractAttribute>(this); }
501
502public:
506
511
512 virtual void print(raw_ostream &OS) const { OS << "AADepNode Impl\n"; }
513 DepSetTy &getDeps() { return Deps; }
514
515 friend struct Attributor;
516 friend struct AADepGraph;
517};
518
519/// The data structure for the dependency graph
520///
521/// Note that in this graph if there is an edge from A to B (A -> B),
522/// then it means that B depends on A, and when the state of A is
523/// updated, node B should also be updated
525 AADepGraph() = default;
526 ~AADepGraph() = default;
527
529 static AADepGraphNode *DepGetVal(const DepTy &DT) { return DT.getPointer(); }
530 using iterator =
532
533 /// There is no root node for the dependency graph. But the SCCIterator
534 /// requires a single entry point, so we maintain a fake("synthetic") root
535 /// node that depends on every node.
538
541
542 void viewGraph();
543
544 /// Dump graph to file
545 void dumpGraph();
546
547 /// Print dependency graph
548 void print();
549};
550
551/// Helper to describe and deal with positions in the LLVM-IR.
552///
553/// A position in the IR is described by an anchor value and an "offset" that
554/// could be the argument number, for call sites and arguments, or an indicator
555/// of the "position kind". The kinds, specified in the Kind enum below, include
556/// the locations in the attribute list, i.a., function scope and return value,
557/// as well as a distinction between call sites and functions. Finally, there
558/// are floating values that do not have a corresponding attribute list
559/// position.
561 // NOTE: In the future this definition can be changed to support recursive
562 // functions.
564
565 /// The positions we distinguish in the IR.
566 enum Kind : char {
567 IRP_INVALID, ///< An invalid position.
568 IRP_FLOAT, ///< A position that is not associated with a spot suitable
569 ///< for attributes. This could be any value or instruction.
570 IRP_RETURNED, ///< An attribute for the function return value.
571 IRP_CALL_SITE_RETURNED, ///< An attribute for a call site return value.
572 IRP_FUNCTION, ///< An attribute for a function (scope).
573 IRP_CALL_SITE, ///< An attribute for a call site (function scope).
574 IRP_ARGUMENT, ///< An attribute for a function argument.
575 IRP_CALL_SITE_ARGUMENT, ///< An attribute for a call site argument.
576 };
577
578 /// Default constructor available to create invalid positions implicitly. All
579 /// other positions need to be created explicitly through the appropriate
580 /// static member function.
581 IRPosition() : Enc(nullptr, ENC_VALUE) { verify(); }
582
583 /// Create a position describing the value of \p V.
584 static const IRPosition value(const Value &V,
585 const CallBaseContext *CBContext = nullptr) {
586 if (auto *Arg = dyn_cast<Argument>(&V))
587 return IRPosition::argument(*Arg, CBContext);
588 if (auto *CB = dyn_cast<CallBase>(&V))
590 return IRPosition(const_cast<Value &>(V), IRP_FLOAT, CBContext);
591 }
592
593 /// Create a position describing the instruction \p I. This is different from
594 /// the value version because call sites are treated as intrusctions rather
595 /// than their return value in this function.
596 static const IRPosition inst(const Instruction &I,
597 const CallBaseContext *CBContext = nullptr) {
598 return IRPosition(const_cast<Instruction &>(I), IRP_FLOAT, CBContext);
599 }
600
601 /// Create a position describing the function scope of \p F.
602 /// \p CBContext is used for call base specific analysis.
603 static const IRPosition function(const Function &F,
604 const CallBaseContext *CBContext = nullptr) {
605 return IRPosition(const_cast<Function &>(F), IRP_FUNCTION, CBContext);
606 }
607
608 /// Create a position describing the returned value of \p F.
609 /// \p CBContext is used for call base specific analysis.
610 static const IRPosition returned(const Function &F,
611 const CallBaseContext *CBContext = nullptr) {
612 return IRPosition(const_cast<Function &>(F), IRP_RETURNED, CBContext);
613 }
614
615 /// Create a position describing the argument \p Arg.
616 /// \p CBContext is used for call base specific analysis.
617 static const IRPosition argument(const Argument &Arg,
618 const CallBaseContext *CBContext = nullptr) {
619 return IRPosition(const_cast<Argument &>(Arg), IRP_ARGUMENT, CBContext);
620 }
621
622 /// Create a position describing the function scope of \p CB.
623 static const IRPosition callsite_function(const CallBase &CB) {
624 return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE);
625 }
626
627 /// Create a position describing the returned value of \p CB.
628 static const IRPosition callsite_returned(const CallBase &CB) {
629 return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE_RETURNED);
630 }
631
632 /// Create a position describing the argument of \p CB at position \p ArgNo.
633 static const IRPosition callsite_argument(const CallBase &CB,
634 unsigned ArgNo) {
635 return IRPosition(const_cast<Use &>(CB.getArgOperandUse(ArgNo)),
637 }
638
639 /// Create a position describing the argument of \p ACS at position \p ArgNo.
641 unsigned ArgNo) {
642 if (ACS.getNumArgOperands() <= ArgNo)
643 return IRPosition();
644 int CSArgNo = ACS.getCallArgOperandNo(ArgNo);
645 if (CSArgNo >= 0)
647 cast<CallBase>(*ACS.getInstruction()), CSArgNo);
648 return IRPosition();
649 }
650
651 /// Create a position with function scope matching the "context" of \p IRP.
652 /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
653 /// will be a call site position, otherwise the function position of the
654 /// associated function.
655 static const IRPosition
657 const CallBaseContext *CBContext = nullptr) {
658 if (IRP.isAnyCallSitePosition()) {
660 cast<CallBase>(IRP.getAnchorValue()));
661 }
663 return IRPosition::function(*IRP.getAssociatedFunction(), CBContext);
664 }
665
666 bool operator==(const IRPosition &RHS) const {
667 return Enc == RHS.Enc && RHS.CBContext == CBContext;
668 }
669 bool operator!=(const IRPosition &RHS) const { return !(*this == RHS); }
670
671 /// Return the value this abstract attribute is anchored with.
672 ///
673 /// The anchor value might not be the associated value if the latter is not
674 /// sufficient to determine where arguments will be manifested. This is, so
675 /// far, only the case for call site arguments as the value is not sufficient
676 /// to pinpoint them. Instead, we can use the call site as an anchor.
678 switch (getEncodingBits()) {
679 case ENC_VALUE:
680 case ENC_RETURNED_VALUE:
681 case ENC_FLOATING_FUNCTION:
682 return *getAsValuePtr();
683 case ENC_CALL_SITE_ARGUMENT_USE:
684 return *(getAsUsePtr()->getUser());
685 default:
686 llvm_unreachable("Unkown encoding!");
687 };
688 }
689
690 /// Return the associated function, if any.
692 if (auto *CB = dyn_cast<CallBase>(&getAnchorValue())) {
693 // We reuse the logic that associates callback calles to arguments of a
694 // call site here to identify the callback callee as the associated
695 // function.
697 return Arg->getParent();
698 return CB->getCalledFunction();
699 }
700 return getAnchorScope();
701 }
702
703 /// Return the associated argument, if any.
705
706 /// Return true if the position refers to a function interface, that is the
707 /// function scope, the function return, or an argument.
708 bool isFnInterfaceKind() const {
709 switch (getPositionKind()) {
713 return true;
714 default:
715 return false;
716 }
717 }
718
719 /// Return the Function surrounding the anchor value.
721 Value &V = getAnchorValue();
722 if (isa<Function>(V))
723 return &cast<Function>(V);
724 if (isa<Argument>(V))
725 return cast<Argument>(V).getParent();
726 if (isa<Instruction>(V))
727 return cast<Instruction>(V).getFunction();
728 return nullptr;
729 }
730
731 /// Return the context instruction, if any.
733 Value &V = getAnchorValue();
734 if (auto *I = dyn_cast<Instruction>(&V))
735 return I;
736 if (auto *Arg = dyn_cast<Argument>(&V))
737 if (!Arg->getParent()->isDeclaration())
738 return &Arg->getParent()->getEntryBlock().front();
739 if (auto *F = dyn_cast<Function>(&V))
740 if (!F->isDeclaration())
741 return &(F->getEntryBlock().front());
742 return nullptr;
743 }
744
745 /// Return the value this abstract attribute is associated with.
747 if (getCallSiteArgNo() < 0 || isa<Argument>(&getAnchorValue()))
748 return getAnchorValue();
749 assert(isa<CallBase>(&getAnchorValue()) && "Expected a call base!");
750 return *cast<CallBase>(&getAnchorValue())
751 ->getArgOperand(getCallSiteArgNo());
752 }
753
754 /// Return the type this abstract attribute is associated with.
758 return getAssociatedValue().getType();
759 }
760
761 /// Return the callee argument number of the associated value if it is an
762 /// argument or call site argument, otherwise a negative value. In contrast to
763 /// `getCallSiteArgNo` this method will always return the "argument number"
764 /// from the perspective of the callee. This may not the same as the call site
765 /// if this is a callback call.
766 int getCalleeArgNo() const {
767 return getArgNo(/* CallbackCalleeArgIfApplicable */ true);
768 }
769
770 /// Return the call site argument number of the associated value if it is an
771 /// argument or call site argument, otherwise a negative value. In contrast to
772 /// `getCalleArgNo` this method will always return the "operand number" from
773 /// the perspective of the call site. This may not the same as the callee
774 /// perspective if this is a callback call.
775 int getCallSiteArgNo() const {
776 return getArgNo(/* CallbackCalleeArgIfApplicable */ false);
777 }
778
779 /// Return the index in the attribute list for this position.
780 unsigned getAttrIdx() const {
781 switch (getPositionKind()) {
784 break;
794 }
796 "There is no attribute index for a floating or invalid position!");
797 }
798
799 /// Return the associated position kind.
801 char EncodingBits = getEncodingBits();
802 if (EncodingBits == ENC_CALL_SITE_ARGUMENT_USE)
804 if (EncodingBits == ENC_FLOATING_FUNCTION)
805 return IRP_FLOAT;
806
807 Value *V = getAsValuePtr();
808 if (!V)
809 return IRP_INVALID;
810 if (isa<Argument>(V))
811 return IRP_ARGUMENT;
812 if (isa<Function>(V))
813 return isReturnPosition(EncodingBits) ? IRP_RETURNED : IRP_FUNCTION;
814 if (isa<CallBase>(V))
815 return isReturnPosition(EncodingBits) ? IRP_CALL_SITE_RETURNED
817 return IRP_FLOAT;
818 }
819
820 /// TODO: Figure out if the attribute related helper functions should live
821 /// here or somewhere else.
822
823 /// Return true if any kind in \p AKs existing in the IR at a position that
824 /// will affect this one. See also getAttrs(...).
825 /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
826 /// e.g., the function position if this is an
827 /// argument position, should be ignored.
829 bool IgnoreSubsumingPositions = false,
830 Attributor *A = nullptr) const;
831
832 /// Return the attributes of any kind in \p AKs existing in the IR at a
833 /// position that will affect this one. While each position can only have a
834 /// single attribute of any kind in \p AKs, there are "subsuming" positions
835 /// that could have an attribute as well. This method returns all attributes
836 /// found in \p Attrs.
837 /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
838 /// e.g., the function position if this is an
839 /// argument position, should be ignored.
842 bool IgnoreSubsumingPositions = false,
843 Attributor *A = nullptr) const;
844
845 /// Remove the attribute of kind \p AKs existing in the IR at this position.
848 return;
849
850 AttributeList AttrList;
851 auto *CB = dyn_cast<CallBase>(&getAnchorValue());
852 if (CB)
853 AttrList = CB->getAttributes();
854 else
855 AttrList = getAssociatedFunction()->getAttributes();
856
858 for (Attribute::AttrKind AK : AKs)
859 AttrList = AttrList.removeAttributeAtIndex(Ctx, getAttrIdx(), AK);
860
861 if (CB)
862 CB->setAttributes(AttrList);
863 else
865 }
866
868 switch (getPositionKind()) {
872 return true;
873 default:
874 return false;
875 }
876 }
877
878 /// Return true if the position is an argument or call site argument.
879 bool isArgumentPosition() const {
880 switch (getPositionKind()) {
883 return true;
884 default:
885 return false;
886 }
887 }
888
889 /// Return the same position without the call base context.
891 IRPosition Result = *this;
892 Result.CBContext = nullptr;
893 return Result;
894 }
895
896 /// Get the call base context from the position.
897 const CallBaseContext *getCallBaseContext() const { return CBContext; }
898
899 /// Check if the position has any call base context.
900 bool hasCallBaseContext() const { return CBContext != nullptr; }
901
902 /// Special DenseMap key values.
903 ///
904 ///{
905 static const IRPosition EmptyKey;
907 ///}
908
909 /// Conversion into a void * to allow reuse of pointer hashing.
910 operator void *() const { return Enc.getOpaqueValue(); }
911
912private:
913 /// Private constructor for special values only!
914 explicit IRPosition(void *Ptr, const CallBaseContext *CBContext = nullptr)
915 : CBContext(CBContext) {
917 }
918
919 /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
920 explicit IRPosition(Value &AnchorVal, Kind PK,
921 const CallBaseContext *CBContext = nullptr)
922 : CBContext(CBContext) {
923 switch (PK) {
925 llvm_unreachable("Cannot create invalid IRP with an anchor value!");
926 break;
928 // Special case for floating functions.
929 if (isa<Function>(AnchorVal) || isa<CallBase>(AnchorVal))
930 Enc = {&AnchorVal, ENC_FLOATING_FUNCTION};
931 else
932 Enc = {&AnchorVal, ENC_VALUE};
933 break;
936 Enc = {&AnchorVal, ENC_VALUE};
937 break;
940 Enc = {&AnchorVal, ENC_RETURNED_VALUE};
941 break;
943 Enc = {&AnchorVal, ENC_VALUE};
944 break;
947 "Cannot create call site argument IRP with an anchor value!");
948 break;
949 }
950 verify();
951 }
952
953 /// Return the callee argument number of the associated value if it is an
954 /// argument or call site argument. See also `getCalleeArgNo` and
955 /// `getCallSiteArgNo`.
956 int getArgNo(bool CallbackCalleeArgIfApplicable) const {
957 if (CallbackCalleeArgIfApplicable)
958 if (Argument *Arg = getAssociatedArgument())
959 return Arg->getArgNo();
960 switch (getPositionKind()) {
962 return cast<Argument>(getAsValuePtr())->getArgNo();
964 Use &U = *getAsUsePtr();
965 return cast<CallBase>(U.getUser())->getArgOperandNo(&U);
966 }
967 default:
968 return -1;
969 }
970 }
971
972 /// IRPosition for the use \p U. The position kind \p PK needs to be
973 /// IRP_CALL_SITE_ARGUMENT, the anchor value is the user, the associated value
974 /// the used value.
975 explicit IRPosition(Use &U, Kind PK) {
977 "Use constructor is for call site arguments only!");
978 Enc = {&U, ENC_CALL_SITE_ARGUMENT_USE};
979 verify();
980 }
981
982 /// Verify internal invariants.
983 void verify();
984
985 /// Return the attributes of kind \p AK existing in the IR as attribute.
986 bool getAttrsFromIRAttr(Attribute::AttrKind AK,
987 SmallVectorImpl<Attribute> &Attrs) const;
988
989 /// Return the attributes of kind \p AK existing in the IR as operand bundles
990 /// of an llvm.assume.
991 bool getAttrsFromAssumes(Attribute::AttrKind AK,
992 SmallVectorImpl<Attribute> &Attrs,
993 Attributor &A) const;
994
995 /// Return the underlying pointer as Value *, valid for all positions but
996 /// IRP_CALL_SITE_ARGUMENT.
997 Value *getAsValuePtr() const {
998 assert(getEncodingBits() != ENC_CALL_SITE_ARGUMENT_USE &&
999 "Not a value pointer!");
1000 return reinterpret_cast<Value *>(Enc.getPointer());
1001 }
1002
1003 /// Return the underlying pointer as Use *, valid only for
1004 /// IRP_CALL_SITE_ARGUMENT positions.
1005 Use *getAsUsePtr() const {
1006 assert(getEncodingBits() == ENC_CALL_SITE_ARGUMENT_USE &&
1007 "Not a value pointer!");
1008 return reinterpret_cast<Use *>(Enc.getPointer());
1009 }
1010
1011 /// Return true if \p EncodingBits describe a returned or call site returned
1012 /// position.
1013 static bool isReturnPosition(char EncodingBits) {
1014 return EncodingBits == ENC_RETURNED_VALUE;
1015 }
1016
1017 /// Return true if the encoding bits describe a returned or call site returned
1018 /// position.
1019 bool isReturnPosition() const { return isReturnPosition(getEncodingBits()); }
1020
1021 /// The encoding of the IRPosition is a combination of a pointer and two
1022 /// encoding bits. The values of the encoding bits are defined in the enum
1023 /// below. The pointer is either a Value* (for the first three encoding bit
1024 /// combinations) or Use* (for ENC_CALL_SITE_ARGUMENT_USE).
1025 ///
1026 ///{
1027 enum {
1028 ENC_VALUE = 0b00,
1029 ENC_RETURNED_VALUE = 0b01,
1030 ENC_FLOATING_FUNCTION = 0b10,
1031 ENC_CALL_SITE_ARGUMENT_USE = 0b11,
1032 };
1033
1034 // Reserve the maximal amount of bits so there is no need to mask out the
1035 // remaining ones. We will not encode anything else in the pointer anyway.
1036 static constexpr int NumEncodingBits =
1037 PointerLikeTypeTraits<void *>::NumLowBitsAvailable;
1038 static_assert(NumEncodingBits >= 2, "At least two bits are required!");
1039
1040 /// The pointer with the encoding bits.
1041 PointerIntPair<void *, NumEncodingBits, char> Enc;
1042 ///}
1043
1044 /// Call base context. Used for callsite specific analysis.
1045 const CallBaseContext *CBContext = nullptr;
1046
1047 /// Return the encoding bits.
1048 char getEncodingBits() const { return Enc.getInt(); }
1049};
1050
1051/// Helper that allows IRPosition as a key in a DenseMap.
1052template <> struct DenseMapInfo<IRPosition> {
1053 static inline IRPosition getEmptyKey() { return IRPosition::EmptyKey; }
1054 static inline IRPosition getTombstoneKey() {
1056 }
1057 static unsigned getHashValue(const IRPosition &IRP) {
1058 return (DenseMapInfo<void *>::getHashValue(IRP) << 4) ^
1060 }
1061
1062 static bool isEqual(const IRPosition &a, const IRPosition &b) {
1063 return a == b;
1064 }
1065};
1066
1067/// A visitor class for IR positions.
1068///
1069/// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
1070/// positions" wrt. attributes/information. Thus, if a piece of information
1071/// holds for a subsuming position, it also holds for the position P.
1072///
1073/// The subsuming positions always include the initial position and then,
1074/// depending on the position kind, additionally the following ones:
1075/// - for IRP_RETURNED:
1076/// - the function (IRP_FUNCTION)
1077/// - for IRP_ARGUMENT:
1078/// - the function (IRP_FUNCTION)
1079/// - for IRP_CALL_SITE:
1080/// - the callee (IRP_FUNCTION), if known
1081/// - for IRP_CALL_SITE_RETURNED:
1082/// - the callee (IRP_RETURNED), if known
1083/// - the call site (IRP_FUNCTION)
1084/// - the callee (IRP_FUNCTION), if known
1085/// - for IRP_CALL_SITE_ARGUMENT:
1086/// - the argument of the callee (IRP_ARGUMENT), if known
1087/// - the callee (IRP_FUNCTION), if known
1088/// - the position the call site argument is associated with if it is not
1089/// anchored to the call site, e.g., if it is an argument then the argument
1090/// (IRP_ARGUMENT)
1092 SmallVector<IRPosition, 4> IRPositions;
1093 using iterator = decltype(IRPositions)::iterator;
1094
1095public:
1097 iterator begin() { return IRPositions.begin(); }
1098 iterator end() { return IRPositions.end(); }
1099};
1100
1101/// Wrapper for FunctionAnalysisManager.
1103 // The client may be running the old pass manager, in which case, we need to
1104 // map the requested Analysis to its equivalent wrapper in the old pass
1105 // manager. The scheme implemented here does not require every Analysis to be
1106 // updated. Only those new analyses that the client cares about in the old
1107 // pass manager need to expose a LegacyWrapper type, and that wrapper should
1108 // support a getResult() method that matches the new Analysis.
1109 //
1110 // We need SFINAE to check for the LegacyWrapper, but function templates don't
1111 // allow partial specialization, which is needed in this case. So instead, we
1112 // use a constexpr bool to perform the SFINAE, and then use this information
1113 // inside the function template.
1114 template <typename, typename = void> static constexpr bool HasLegacyWrapper = false;
1115
1116 template <typename Analysis>
1117 typename Analysis::Result *getAnalysis(const Function &F) {
1118 if (FAM)
1119 return &FAM->getResult<Analysis>(const_cast<Function &>(F));
1120 if constexpr (HasLegacyWrapper<Analysis>)
1121 if (LegacyPass)
1122 return &LegacyPass
1123 ->getAnalysis<typename Analysis::LegacyWrapper>(
1124 const_cast<Function &>(F))
1125 .getResult();
1126 return nullptr;
1127 }
1128
1130 AnalysisGetter(Pass *P) : LegacyPass(P) {}
1131 AnalysisGetter() = default;
1132
1133private:
1134 FunctionAnalysisManager *FAM = nullptr;
1135 Pass *LegacyPass = nullptr;
1136};
1137
1138template <typename Analysis>
1140 Analysis, std::void_t<typename Analysis::LegacyWrapper>> = true;
1141
1142/// Data structure to hold cached (LLVM-IR) information.
1143///
1144/// All attributes are given an InformationCache object at creation time to
1145/// avoid inspection of the IR by all of them individually. This default
1146/// InformationCache will hold information required by 'default' attributes,
1147/// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..)
1148/// is called.
1149///
1150/// If custom abstract attributes, registered manually through
1151/// Attributor::registerAA(...), need more information, especially if it is not
1152/// reusable, it is advised to inherit from the InformationCache and cast the
1153/// instance down in the abstract attributes.
1157 : DL(M.getDataLayout()), Allocator(Allocator),
1158 Explorer(
1159 /* ExploreInterBlock */ true, /* ExploreCFGForward */ true,
1160 /* ExploreCFGBackward */ true,
1161 /* LIGetter */
1162 [&](const Function &F) { return AG.getAnalysis<LoopAnalysis>(F); },
1163 /* DTGetter */
1164 [&](const Function &F) {
1166 },
1167 /* PDTGetter */
1168 [&](const Function &F) {
1169 return AG.getAnalysis<PostDominatorTreeAnalysis>(F);
1170 }),
1171 AG(AG), TargetTriple(M.getTargetTriple()) {
1172 if (CGSCC)
1174 }
1175
1177 // The FunctionInfo objects are allocated via a BumpPtrAllocator, we call
1178 // the destructor manually.
1179 for (auto &It : FuncInfoMap)
1180 It.getSecond()->~FunctionInfo();
1181 // Same is true for the instruction exclusions sets.
1183 for (auto *BES : BESets)
1184 BES->~InstExclusionSetTy();
1185 }
1186
1187 /// Apply \p CB to all uses of \p F. If \p LookThroughConstantExprUses is
1188 /// true, constant expression users are not given to \p CB but their uses are
1189 /// traversed transitively.
1190 template <typename CBTy>
1191 static void foreachUse(Function &F, CBTy CB,
1192 bool LookThroughConstantExprUses = true) {
1193 SmallVector<Use *, 8> Worklist(make_pointer_range(F.uses()));
1194
1195 for (unsigned Idx = 0; Idx < Worklist.size(); ++Idx) {
1196 Use &U = *Worklist[Idx];
1197
1198 // Allow use in constant bitcasts and simply look through them.
1199 if (LookThroughConstantExprUses && isa<ConstantExpr>(U.getUser())) {
1200 for (Use &CEU : cast<ConstantExpr>(U.getUser())->uses())
1201 Worklist.push_back(&CEU);
1202 continue;
1203 }
1204
1205 CB(U);
1206 }
1207 }
1208
1209 /// Initialize the ModuleSlice member based on \p SCC. ModuleSlices contains
1210 /// (a subset of) all functions that we can look at during this SCC traversal.
1211 /// This includes functions (transitively) called from the SCC and the
1212 /// (transitive) callers of SCC functions. We also can look at a function if
1213 /// there is a "reference edge", i.a., if the function somehow uses (!=calls)
1214 /// a function in the SCC or a caller of a function in the SCC.
1216 ModuleSlice.insert(SCC.begin(), SCC.end());
1217
1219 SmallVector<Function *, 16> Worklist(SCC.begin(), SCC.end());
1220 while (!Worklist.empty()) {
1221 Function *F = Worklist.pop_back_val();
1222 ModuleSlice.insert(F);
1223
1224 for (Instruction &I : instructions(*F))
1225 if (auto *CB = dyn_cast<CallBase>(&I))
1226 if (Function *Callee = CB->getCalledFunction())
1227 if (Seen.insert(Callee).second)
1228 Worklist.push_back(Callee);
1229 }
1230
1231 Seen.clear();
1232 Worklist.append(SCC.begin(), SCC.end());
1233 while (!Worklist.empty()) {
1234 Function *F = Worklist.pop_back_val();
1235 ModuleSlice.insert(F);
1236
1237 // Traverse all transitive uses.
1238 foreachUse(*F, [&](Use &U) {
1239 if (auto *UsrI = dyn_cast<Instruction>(U.getUser()))
1240 if (Seen.insert(UsrI->getFunction()).second)
1241 Worklist.push_back(UsrI->getFunction());
1242 });
1243 }
1244 }
1245
1246 /// The slice of the module we are allowed to look at.
1248
1249 /// A vector type to hold instructions.
1251
1252 /// A map type from opcodes to instructions with this opcode.
1254
1255 /// Return the map that relates "interesting" opcodes with all instructions
1256 /// with that opcode in \p F.
1258 return getFunctionInfo(F).OpcodeInstMap;
1259 }
1260
1261 /// Return the instructions in \p F that may read or write memory.
1263 return getFunctionInfo(F).RWInsts;
1264 }
1265
1266 /// Return MustBeExecutedContextExplorer
1268 return Explorer;
1269 }
1270
1271 /// Return TargetLibraryInfo for function \p F.
1274 }
1275
1276 /// Return AliasAnalysis Result for function \p F.
1278
1279 /// Return true if \p Arg is involved in a must-tail call, thus the argument
1280 /// of the caller or callee.
1282 FunctionInfo &FI = getFunctionInfo(*Arg.getParent());
1283 return FI.CalledViaMustTail || FI.ContainsMustTailCall;
1284 }
1285
1286 bool isOnlyUsedByAssume(const Instruction &I) const {
1287 return AssumeOnlyValues.contains(&I);
1288 }
1289
1290 /// Return the analysis result from a pass \p AP for function \p F.
1291 template <typename AP>
1292 typename AP::Result *getAnalysisResultForFunction(const Function &F) {
1293 return AG.getAnalysis<AP>(F);
1294 }
1295
1296 /// Return datalayout used in the module.
1297 const DataLayout &getDL() { return DL; }
1298
1299 /// Return the map conaining all the knowledge we have from `llvm.assume`s.
1300 const RetainedKnowledgeMap &getKnowledgeMap() const { return KnowledgeMap; }
1301
1302 /// Given \p BES, return a uniqued version.
1305 auto It = BESets.find(BES);
1306 if (It != BESets.end())
1307 return *It;
1308 auto *UniqueBES = new (Allocator) AA::InstExclusionSetTy(*BES);
1309 bool Success = BESets.insert(UniqueBES).second;
1310 (void)Success;
1311 assert(Success && "Expected only new entries to be added");
1312 return UniqueBES;
1313 }
1314
1315 /// Check whether \p F is part of module slice.
1317 return ModuleSlice.empty() || ModuleSlice.count(const_cast<Function *>(&F));
1318 }
1319
1320 /// Return true if the stack (llvm::Alloca) can be accessed by other threads.
1322
1323 /// Return true if the target is a GPU.
1325 return TargetTriple.isAMDGPU() || TargetTriple.isNVPTX();
1326 }
1327
1328private:
1329 struct FunctionInfo {
1330 ~FunctionInfo();
1331
1332 /// A nested map that remembers all instructions in a function with a
1333 /// certain instruction opcode (Instruction::getOpcode()).
1334 OpcodeInstMapTy OpcodeInstMap;
1335
1336 /// A map from functions to their instructions that may read or write
1337 /// memory.
1338 InstructionVectorTy RWInsts;
1339
1340 /// Function is called by a `musttail` call.
1341 bool CalledViaMustTail;
1342
1343 /// Function contains a `musttail` call.
1344 bool ContainsMustTailCall;
1345 };
1346
1347 /// A map type from functions to informatio about it.
1348 DenseMap<const Function *, FunctionInfo *> FuncInfoMap;
1349
1350 /// Return information about the function \p F, potentially by creating it.
1351 FunctionInfo &getFunctionInfo(const Function &F) {
1352 FunctionInfo *&FI = FuncInfoMap[&F];
1353 if (!FI) {
1354 FI = new (Allocator) FunctionInfo();
1355 initializeInformationCache(F, *FI);
1356 }
1357 return *FI;
1358 }
1359
1360 /// Initialize the function information cache \p FI for the function \p F.
1361 ///
1362 /// This method needs to be called for all function that might be looked at
1363 /// through the information cache interface *prior* to looking at them.
1364 void initializeInformationCache(const Function &F, FunctionInfo &FI);
1365
1366 /// The datalayout used in the module.
1367 const DataLayout &DL;
1368
1369 /// The allocator used to allocate memory, e.g. for `FunctionInfo`s.
1370 BumpPtrAllocator &Allocator;
1371
1372 /// MustBeExecutedContextExplorer
1373 MustBeExecutedContextExplorer Explorer;
1374
1375 /// A map with knowledge retained in `llvm.assume` instructions.
1376 RetainedKnowledgeMap KnowledgeMap;
1377
1378 /// A container for all instructions that are only used by `llvm.assume`.
1379 SetVector<const Instruction *> AssumeOnlyValues;
1380
1381 /// Cache for block sets to allow reuse.
1382 DenseSet<const AA::InstExclusionSetTy *> BESets;
1383
1384 /// Getters for analysis.
1385 AnalysisGetter &AG;
1386
1387 /// Set of inlineable functions
1388 SmallPtrSet<const Function *, 8> InlineableFunctions;
1389
1390 /// The triple describing the target machine.
1391 Triple TargetTriple;
1392
1393 /// Give the Attributor access to the members so
1394 /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them.
1395 friend struct Attributor;
1396};
1397
1398/// Configuration for the Attributor.
1400
1402
1403 /// Is the user of the Attributor a module pass or not. This determines what
1404 /// IR we can look at and modify. If it is a module pass we might deduce facts
1405 /// outside the initial function set and modify functions outside that set,
1406 /// but only as part of the optimization of the functions in the initial
1407 /// function set. For CGSCC passes we can look at the IR of the module slice
1408 /// but never run any deduction, or perform any modification, outside the
1409 /// initial function set (which we assume is the SCC).
1410 bool IsModulePass = true;
1411
1412 /// Flag to determine if we can delete functions or keep dead ones around.
1413 bool DeleteFns = true;
1414
1415 /// Flag to determine if we rewrite function signatures.
1417
1418 /// Flag to determine if we want to initialize all default AAs for an internal
1419 /// function marked live. See also: InitializationCallback>
1421
1422 /// Callback function to be invoked on internal functions marked live.
1423 std::function<void(Attributor &A, const Function &F)> InitializationCallback =
1424 nullptr;
1425
1426 /// Helper to update an underlying call graph and to delete functions.
1428
1429 /// If not null, a set limiting the attribute opportunities.
1431
1432 /// Maximum number of iterations to run until fixpoint.
1433 std::optional<unsigned> MaxFixpointIterations;
1434
1435 /// A callback function that returns an ORE object from a Function pointer.
1436 ///{
1440 ///}
1441
1442 /// The name of the pass running the attributor, used to emit remarks.
1443 const char *PassName = nullptr;
1444};
1445
1446/// The fixpoint analysis framework that orchestrates the attribute deduction.
1447///
1448/// The Attributor provides a general abstract analysis framework (guided
1449/// fixpoint iteration) as well as helper functions for the deduction of
1450/// (LLVM-IR) attributes. However, also other code properties can be deduced,
1451/// propagated, and ultimately manifested through the Attributor framework. This
1452/// is particularly useful if these properties interact with attributes and a
1453/// co-scheduled deduction allows to improve the solution. Even if not, thus if
1454/// attributes/properties are completely isolated, they should use the
1455/// Attributor framework to reduce the number of fixpoint iteration frameworks
1456/// in the code base. Note that the Attributor design makes sure that isolated
1457/// attributes are not impacted, in any way, by others derived at the same time
1458/// if there is no cross-reasoning performed.
1459///
1460/// The public facing interface of the Attributor is kept simple and basically
1461/// allows abstract attributes to one thing, query abstract attributes
1462/// in-flight. There are two reasons to do this:
1463/// a) The optimistic state of one abstract attribute can justify an
1464/// optimistic state of another, allowing to framework to end up with an
1465/// optimistic (=best possible) fixpoint instead of one based solely on
1466/// information in the IR.
1467/// b) This avoids reimplementing various kinds of lookups, e.g., to check
1468/// for existing IR attributes, in favor of a single lookups interface
1469/// provided by an abstract attribute subclass.
1470///
1471/// NOTE: The mechanics of adding a new "concrete" abstract attribute are
1472/// described in the file comment.
1474
1475 /// Constructor
1476 ///
1477 /// \param Functions The set of functions we are deriving attributes for.
1478 /// \param InfoCache Cache to hold various information accessible for
1479 /// the abstract attributes.
1480 /// \param Configuration The Attributor configuration which determines what
1481 /// generic features to use.
1483 AttributorConfig Configuration)
1484 : Allocator(InfoCache.Allocator), Functions(Functions),
1485 InfoCache(InfoCache), Configuration(Configuration) {}
1486
1487 ~Attributor();
1488
1489 /// Run the analyses until a fixpoint is reached or enforced (timeout).
1490 ///
1491 /// The attributes registered with this Attributor can be used after as long
1492 /// as the Attributor is not destroyed (it owns the attributes now).
1493 ///
1494 /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
1495 ChangeStatus run();
1496
1497 /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
1498 /// no abstract attribute is found equivalent positions are checked, see
1499 /// SubsumingPositionIterator. Thus, the returned abstract attribute
1500 /// might be anchored at a different position, e.g., the callee if \p IRP is a
1501 /// call base.
1502 ///
1503 /// This method is the only (supported) way an abstract attribute can retrieve
1504 /// information from another abstract attribute. As an example, take an
1505 /// abstract attribute that determines the memory access behavior for a
1506 /// argument (readnone, readonly, ...). It should use `getAAFor` to get the
1507 /// most optimistic information for other abstract attributes in-flight, e.g.
1508 /// the one reasoning about the "captured" state for the argument or the one
1509 /// reasoning on the memory access behavior of the function as a whole.
1510 ///
1511 /// If the DepClass enum is set to `DepClassTy::None` the dependence from
1512 /// \p QueryingAA to the return abstract attribute is not automatically
1513 /// recorded. This should only be used if the caller will record the
1514 /// dependence explicitly if necessary, thus if it the returned abstract
1515 /// attribute is used for reasoning. To record the dependences explicitly use
1516 /// the `Attributor::recordDependence` method.
1517 template <typename AAType>
1518 const AAType &getAAFor(const AbstractAttribute &QueryingAA,
1519 const IRPosition &IRP, DepClassTy DepClass) {
1520 return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
1521 /* ForceUpdate */ false);
1522 }
1523
1524 /// Similar to getAAFor but the return abstract attribute will be updated (via
1525 /// `AbstractAttribute::update`) even if it is found in the cache. This is
1526 /// especially useful for AAIsDead as changes in liveness can make updates
1527 /// possible/useful that were not happening before as the abstract attribute
1528 /// was assumed dead.
1529 template <typename AAType>
1530 const AAType &getAndUpdateAAFor(const AbstractAttribute &QueryingAA,
1531 const IRPosition &IRP, DepClassTy DepClass) {
1532 return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
1533 /* ForceUpdate */ true);
1534 }
1535
1536 /// The version of getAAFor that allows to omit a querying abstract
1537 /// attribute. Using this after Attributor started running is restricted to
1538 /// only the Attributor itself. Initial seeding of AAs can be done via this
1539 /// function.
1540 /// NOTE: ForceUpdate is ignored in any stage other than the update stage.
1541 template <typename AAType>
1542 const AAType &getOrCreateAAFor(IRPosition IRP,
1543 const AbstractAttribute *QueryingAA,
1544 DepClassTy DepClass, bool ForceUpdate = false,
1545 bool UpdateAfterInit = true) {
1546 if (!shouldPropagateCallBaseContext(IRP))
1547 IRP = IRP.stripCallBaseContext();
1548
1549 if (AAType *AAPtr = lookupAAFor<AAType>(IRP, QueryingAA, DepClass,
1550 /* AllowInvalidState */ true)) {
1551 if (ForceUpdate && Phase == AttributorPhase::UPDATE)
1552 updateAA(*AAPtr);
1553 return *AAPtr;
1554 }
1555
1556 // No matching attribute found, create one.
1557 // Use the static create method.
1558 auto &AA = AAType::createForPosition(IRP, *this);
1559
1560 // Always register a new attribute to make sure we clean up the allocated
1561 // memory properly.
1562 registerAA(AA);
1563
1564 // If we are currenty seeding attributes, enforce seeding rules.
1565 if (Phase == AttributorPhase::SEEDING && !shouldSeedAttribute(AA)) {
1566 AA.getState().indicatePessimisticFixpoint();
1567 return AA;
1568 }
1569
1570 // For now we ignore naked and optnone functions.
1571 bool Invalidate =
1572 Configuration.Allowed && !Configuration.Allowed->count(&AAType::ID);
1573 const Function *AnchorFn = IRP.getAnchorScope();
1574 if (AnchorFn) {
1575 Invalidate |=
1576 AnchorFn->hasFnAttribute(Attribute::Naked) ||
1577 AnchorFn->hasFnAttribute(Attribute::OptimizeNone) ||
1578 (!isModulePass() && !getInfoCache().isInModuleSlice(*AnchorFn));
1579 }
1580
1581 // Avoid too many nested initializations to prevent a stack overflow.
1582 Invalidate |= InitializationChainLength > MaxInitializationChainLength;
1583
1584 // Bootstrap the new attribute with an initial update to propagate
1585 // information, e.g., function -> call site. If it is not on a given
1586 // Allowed we will not perform updates at all.
1587 if (Invalidate) {
1588 AA.getState().indicatePessimisticFixpoint();
1589 return AA;
1590 }
1591
1592 {
1593 TimeTraceScope TimeScope(AA.getName() + "::initialize");
1594 ++InitializationChainLength;
1595 AA.initialize(*this);
1596 --InitializationChainLength;
1597 }
1598
1599 // We update only AAs associated with functions in the Functions set or
1600 // call sites of them.
1601 if ((AnchorFn && !isRunOn(const_cast<Function *>(AnchorFn))) &&
1603 AA.getState().indicatePessimisticFixpoint();
1604 return AA;
1605 }
1606
1607 // If this is queried in the manifest stage, we force the AA to indicate
1608 // pessimistic fixpoint immediately.
1609 if (Phase == AttributorPhase::MANIFEST ||
1610 Phase == AttributorPhase::CLEANUP) {
1611 AA.getState().indicatePessimisticFixpoint();
1612 return AA;
1613 }
1614
1615 // Allow seeded attributes to declare dependencies.
1616 // Remember the seeding state.
1617 if (UpdateAfterInit) {
1618 AttributorPhase OldPhase = Phase;
1619 Phase = AttributorPhase::UPDATE;
1620
1621 updateAA(AA);
1622
1623 Phase = OldPhase;
1624 }
1625
1626 if (QueryingAA && AA.getState().isValidState())
1627 recordDependence(AA, const_cast<AbstractAttribute &>(*QueryingAA),
1628 DepClass);
1629 return AA;
1630 }
1631 template <typename AAType>
1632 const AAType &getOrCreateAAFor(const IRPosition &IRP) {
1633 return getOrCreateAAFor<AAType>(IRP, /* QueryingAA */ nullptr,
1635 }
1636
1637 /// Return the attribute of \p AAType for \p IRP if existing and valid. This
1638 /// also allows non-AA users lookup.
1639 template <typename AAType>
1640 AAType *lookupAAFor(const IRPosition &IRP,
1641 const AbstractAttribute *QueryingAA = nullptr,
1643 bool AllowInvalidState = false) {
1644 static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1645 "Cannot query an attribute with a type not derived from "
1646 "'AbstractAttribute'!");
1647 // Lookup the abstract attribute of type AAType. If found, return it after
1648 // registering a dependence of QueryingAA on the one returned attribute.
1649 AbstractAttribute *AAPtr = AAMap.lookup({&AAType::ID, IRP});
1650 if (!AAPtr)
1651 return nullptr;
1652
1653 AAType *AA = static_cast<AAType *>(AAPtr);
1654
1655 // Do not register a dependence on an attribute with an invalid state.
1656 if (DepClass != DepClassTy::NONE && QueryingAA &&
1657 AA->getState().isValidState())
1658 recordDependence(*AA, const_cast<AbstractAttribute &>(*QueryingAA),
1659 DepClass);
1660
1661 // Return nullptr if this attribute has an invalid state.
1662 if (!AllowInvalidState && !AA->getState().isValidState())
1663 return nullptr;
1664 return AA;
1665 }
1666
1667 /// Allows a query AA to request an update if a new query was received.
1669
1670 /// Explicitly record a dependence from \p FromAA to \p ToAA, that is if
1671 /// \p FromAA changes \p ToAA should be updated as well.
1672 ///
1673 /// This method should be used in conjunction with the `getAAFor` method and
1674 /// with the DepClass enum passed to the method set to None. This can
1675 /// be beneficial to avoid false dependences but it requires the users of
1676 /// `getAAFor` to explicitly record true dependences through this method.
1677 /// The \p DepClass flag indicates if the dependence is striclty necessary.
1678 /// That means for required dependences, if \p FromAA changes to an invalid
1679 /// state, \p ToAA can be moved to a pessimistic fixpoint because it required
1680 /// information from \p FromAA but none are available anymore.
1681 void recordDependence(const AbstractAttribute &FromAA,
1682 const AbstractAttribute &ToAA, DepClassTy DepClass);
1683
1684 /// Introduce a new abstract attribute into the fixpoint analysis.
1685 ///
1686 /// Note that ownership of the attribute is given to the Attributor. It will
1687 /// invoke delete for the Attributor on destruction of the Attributor.
1688 ///
1689 /// Attributes are identified by their IR position (AAType::getIRPosition())
1690 /// and the address of their static member (see AAType::ID).
1691 template <typename AAType> AAType &registerAA(AAType &AA) {
1692 static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1693 "Cannot register an attribute with a type not derived from "
1694 "'AbstractAttribute'!");
1695 // Put the attribute in the lookup map structure and the container we use to
1696 // keep track of all attributes.
1697 const IRPosition &IRP = AA.getIRPosition();
1698 AbstractAttribute *&AAPtr = AAMap[{&AAType::ID, IRP}];
1699
1700 assert(!AAPtr && "Attribute already in map!");
1701 AAPtr = &AA;
1702
1703 // Register AA with the synthetic root only before the manifest stage.
1704 if (Phase == AttributorPhase::SEEDING || Phase == AttributorPhase::UPDATE)
1707
1708 return AA;
1709 }
1710
1711 /// Return the internal information cache.
1712 InformationCache &getInfoCache() { return InfoCache; }
1713
1714 /// Return true if this is a module pass, false otherwise.
1715 bool isModulePass() const { return Configuration.IsModulePass; }
1716
1717 /// Return true if we derive attributes for \p Fn
1718 bool isRunOn(Function &Fn) const { return isRunOn(&Fn); }
1719 bool isRunOn(Function *Fn) const {
1720 return Functions.empty() || Functions.count(Fn);
1721 }
1722
1723 /// Determine opportunities to derive 'default' attributes in \p F and create
1724 /// abstract attribute objects for them.
1725 ///
1726 /// \param F The function that is checked for attribute opportunities.
1727 ///
1728 /// Note that abstract attribute instances are generally created even if the
1729 /// IR already contains the information they would deduce. The most important
1730 /// reason for this is the single interface, the one of the abstract attribute
1731 /// instance, which can be queried without the need to look at the IR in
1732 /// various places.
1734
1735 /// Determine whether the function \p F is IPO amendable
1736 ///
1737 /// If a function is exactly defined or it has alwaysinline attribute
1738 /// and is viable to be inlined, we say it is IPO amendable
1740 return F.hasExactDefinition() || InfoCache.InlineableFunctions.count(&F);
1741 }
1742
1743 /// Mark the internal function \p F as live.
1744 ///
1745 /// This will trigger the identification and initialization of attributes for
1746 /// \p F.
1748 assert(F.hasLocalLinkage() &&
1749 "Only local linkage is assumed dead initially.");
1750
1751 if (Configuration.DefaultInitializeLiveInternals)
1753 if (Configuration.InitializationCallback)
1754 Configuration.InitializationCallback(*this, F);
1755 }
1756
1757 /// Helper function to remove callsite.
1759 if (!CI)
1760 return;
1761
1762 Configuration.CGUpdater.removeCallSite(*CI);
1763 }
1764
1765 /// Record that \p U is to be replaces with \p NV after information was
1766 /// manifested. This also triggers deletion of trivially dead istructions.
1768 Value *&V = ToBeChangedUses[&U];
1769 if (V && (V->stripPointerCasts() == NV.stripPointerCasts() ||
1770 isa_and_nonnull<UndefValue>(V)))
1771 return false;
1772 assert((!V || V == &NV || isa<UndefValue>(NV)) &&
1773 "Use was registered twice for replacement with different values!");
1774 V = &NV;
1775 return true;
1776 }
1777
1778 /// Helper function to replace all uses associated with \p IRP with \p NV.
1779 /// Return true if there is any change. The flag \p ChangeDroppable indicates
1780 /// if dropppable uses should be changed too.
1782 bool ChangeDroppable = true) {
1784 auto *CB = cast<CallBase>(IRP.getCtxI());
1786 CB->getArgOperandUse(IRP.getCallSiteArgNo()), NV);
1787 }
1788 Value &V = IRP.getAssociatedValue();
1789 auto &Entry = ToBeChangedValues[&V];
1790 Value *CurNV = get<0>(Entry);
1791 if (CurNV && (CurNV->stripPointerCasts() == NV.stripPointerCasts() ||
1792 isa<UndefValue>(CurNV)))
1793 return false;
1794 assert((!CurNV || CurNV == &NV || isa<UndefValue>(NV)) &&
1795 "Value replacement was registered twice with different values!");
1796 Entry = {&NV, ChangeDroppable};
1797 return true;
1798 }
1799
1800 /// Record that \p I is to be replaced with `unreachable` after information
1801 /// was manifested.
1803 ToBeChangedToUnreachableInsts.insert(I);
1804 }
1805
1806 /// Record that \p II has at least one dead successor block. This information
1807 /// is used, e.g., to replace \p II with a call, after information was
1808 /// manifested.
1810 InvokeWithDeadSuccessor.insert(&II);
1811 }
1812
1813 /// Record that \p I is deleted after information was manifested. This also
1814 /// triggers deletion of trivially dead istructions.
1815 void deleteAfterManifest(Instruction &I) { ToBeDeletedInsts.insert(&I); }
1816
1817 /// Record that \p BB is deleted after information was manifested. This also
1818 /// triggers deletion of trivially dead istructions.
1819 void deleteAfterManifest(BasicBlock &BB) { ToBeDeletedBlocks.insert(&BB); }
1820
1821 // Record that \p BB is added during the manifest of an AA. Added basic blocks
1822 // are preserved in the IR.
1824 ManifestAddedBlocks.insert(&BB);
1825 }
1826
1827 /// Record that \p F is deleted after information was manifested.
1829 if (Configuration.DeleteFns)
1830 ToBeDeletedFunctions.insert(&F);
1831 }
1832
1833 /// If \p IRP is assumed to be a constant, return it, if it is unclear yet,
1834 /// return std::nullopt, otherwise return `nullptr`.
1835 std::optional<Constant *> getAssumedConstant(const IRPosition &IRP,
1836 const AbstractAttribute &AA,
1837 bool &UsedAssumedInformation);
1838 std::optional<Constant *> getAssumedConstant(const Value &V,
1839 const AbstractAttribute &AA,
1840 bool &UsedAssumedInformation) {
1841 return getAssumedConstant(IRPosition::value(V), AA, UsedAssumedInformation);
1842 }
1843
1844 /// If \p V is assumed simplified, return it, if it is unclear yet,
1845 /// return std::nullopt, otherwise return `nullptr`.
1846 std::optional<Value *> getAssumedSimplified(const IRPosition &IRP,
1847 const AbstractAttribute &AA,
1848 bool &UsedAssumedInformation,
1849 AA::ValueScope S) {
1850 return getAssumedSimplified(IRP, &AA, UsedAssumedInformation, S);
1851 }
1852 std::optional<Value *> getAssumedSimplified(const Value &V,
1853 const AbstractAttribute &AA,
1854 bool &UsedAssumedInformation,
1855 AA::ValueScope S) {
1857 UsedAssumedInformation, S);
1858 }
1859
1860 /// If \p V is assumed simplified, return it, if it is unclear yet,
1861 /// return std::nullopt, otherwise return `nullptr`. Same as the public
1862 /// version except that it can be used without recording dependences on any \p
1863 /// AA.
1864 std::optional<Value *> getAssumedSimplified(const IRPosition &V,
1865 const AbstractAttribute *AA,
1866 bool &UsedAssumedInformation,
1867 AA::ValueScope S);
1868
1869 /// Try to simplify \p IRP and in the scope \p S. If successful, true is
1870 /// returned and all potential values \p IRP can take are put into \p Values.
1871 /// If the result in \p Values contains select or PHI instructions it means
1872 /// those could not be simplified to a single value. Recursive calls with
1873 /// these instructions will yield their respective potential values. If false
1874 /// is returned no other information is valid.
1875 bool getAssumedSimplifiedValues(const IRPosition &IRP,
1876 const AbstractAttribute *AA,
1879 bool &UsedAssumedInformation);
1880
1881 /// Register \p CB as a simplification callback.
1882 /// `Attributor::getAssumedSimplified` will use these callbacks before
1883 /// we it will ask `AAValueSimplify`. It is important to ensure this
1884 /// is called before `identifyDefaultAbstractAttributes`, assuming the
1885 /// latter is called at all.
1886 using SimplifictionCallbackTy = std::function<std::optional<Value *>(
1887 const IRPosition &, const AbstractAttribute *, bool &)>;
1889 const SimplifictionCallbackTy &CB) {
1890 SimplificationCallbacks[IRP].emplace_back(CB);
1891 }
1892
1893 /// Return true if there is a simplification callback for \p IRP.
1895 return SimplificationCallbacks.count(IRP);
1896 }
1897
1899 std::function<bool(Attributor &, const AbstractAttribute *)>;
1901 const VirtualUseCallbackTy &CB) {
1902 VirtualUseCallbacks[&V].emplace_back(CB);
1903 }
1904
1905private:
1906 /// The vector with all simplification callbacks registered by outside AAs.
1908 SimplificationCallbacks;
1909
1911 VirtualUseCallbacks;
1912
1913public:
1914 /// Translate \p V from the callee context into the call site context.
1915 std::optional<Value *>
1916 translateArgumentToCallSiteContent(std::optional<Value *> V, CallBase &CB,
1917 const AbstractAttribute &AA,
1918 bool &UsedAssumedInformation);
1919
1920 /// Return true if \p AA (or its context instruction) is assumed dead.
1921 ///
1922 /// If \p LivenessAA is not provided it is queried.
1923 bool isAssumedDead(const AbstractAttribute &AA, const AAIsDead *LivenessAA,
1924 bool &UsedAssumedInformation,
1925 bool CheckBBLivenessOnly = false,
1926 DepClassTy DepClass = DepClassTy::OPTIONAL);
1927
1928 /// Return true if \p I is assumed dead.
1929 ///
1930 /// If \p LivenessAA is not provided it is queried.
1931 bool isAssumedDead(const Instruction &I, const AbstractAttribute *QueryingAA,
1932 const AAIsDead *LivenessAA, bool &UsedAssumedInformation,
1933 bool CheckBBLivenessOnly = false,
1935 bool CheckForDeadStore = false);
1936
1937 /// Return true if \p U is assumed dead.
1938 ///
1939 /// If \p FnLivenessAA is not provided it is queried.
1940 bool isAssumedDead(const Use &U, const AbstractAttribute *QueryingAA,
1941 const AAIsDead *FnLivenessAA, bool &UsedAssumedInformation,
1942 bool CheckBBLivenessOnly = false,
1943 DepClassTy DepClass = DepClassTy::OPTIONAL);
1944
1945 /// Return true if \p IRP is assumed dead.
1946 ///
1947 /// If \p FnLivenessAA is not provided it is queried.
1948 bool isAssumedDead(const IRPosition &IRP, const AbstractAttribute *QueryingAA,
1949 const AAIsDead *FnLivenessAA, bool &UsedAssumedInformation,
1950 bool CheckBBLivenessOnly = false,
1951 DepClassTy DepClass = DepClassTy::OPTIONAL);
1952
1953 /// Return true if \p BB is assumed dead.
1954 ///
1955 /// If \p LivenessAA is not provided it is queried.
1956 bool isAssumedDead(const BasicBlock &BB, const AbstractAttribute *QueryingAA,
1957 const AAIsDead *FnLivenessAA,
1958 DepClassTy DepClass = DepClassTy::OPTIONAL);
1959
1960 /// Check \p Pred on all (transitive) uses of \p V.
1961 ///
1962 /// This method will evaluate \p Pred on all (transitive) uses of the
1963 /// associated value and return true if \p Pred holds every time.
1964 /// If uses are skipped in favor of equivalent ones, e.g., if we look through
1965 /// memory, the \p EquivalentUseCB will be used to give the caller an idea
1966 /// what original used was replaced by a new one (or new ones). The visit is
1967 /// cut short if \p EquivalentUseCB returns false and the function will return
1968 /// false as well.
1969 bool checkForAllUses(function_ref<bool(const Use &, bool &)> Pred,
1970 const AbstractAttribute &QueryingAA, const Value &V,
1971 bool CheckBBLivenessOnly = false,
1972 DepClassTy LivenessDepClass = DepClassTy::OPTIONAL,
1973 bool IgnoreDroppableUses = true,
1974 function_ref<bool(const Use &OldU, const Use &NewU)>
1975 EquivalentUseCB = nullptr);
1976
1977 /// Emit a remark generically.
1978 ///
1979 /// This template function can be used to generically emit a remark. The
1980 /// RemarkKind should be one of the following:
1981 /// - OptimizationRemark to indicate a successful optimization attempt
1982 /// - OptimizationRemarkMissed to report a failed optimization attempt
1983 /// - OptimizationRemarkAnalysis to provide additional information about an
1984 /// optimization attempt
1985 ///
1986 /// The remark is built using a callback function \p RemarkCB that takes a
1987 /// RemarkKind as input and returns a RemarkKind.
1988 template <typename RemarkKind, typename RemarkCallBack>
1990 RemarkCallBack &&RemarkCB) const {
1991 if (!Configuration.OREGetter)
1992 return;
1993
1994 Function *F = I->getFunction();
1995 auto &ORE = Configuration.OREGetter(F);
1996
1997 if (RemarkName.startswith("OMP"))
1998 ORE.emit([&]() {
1999 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I))
2000 << " [" << RemarkName << "]";
2001 });
2002 else
2003 ORE.emit([&]() {
2004 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I));
2005 });
2006 }
2007
2008 /// Emit a remark on a function.
2009 template <typename RemarkKind, typename RemarkCallBack>
2010 void emitRemark(Function *F, StringRef RemarkName,
2011 RemarkCallBack &&RemarkCB) const {
2012 if (!Configuration.OREGetter)
2013 return;
2014
2015 auto &ORE = Configuration.OREGetter(F);
2016
2017 if (RemarkName.startswith("OMP"))
2018 ORE.emit([&]() {
2019 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F))
2020 << " [" << RemarkName << "]";
2021 });
2022 else
2023 ORE.emit([&]() {
2024 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F));
2025 });
2026 }
2027
2028 /// Helper struct used in the communication between an abstract attribute (AA)
2029 /// that wants to change the signature of a function and the Attributor which
2030 /// applies the changes. The struct is partially initialized with the
2031 /// information from the AA (see the constructor). All other members are
2032 /// provided by the Attributor prior to invoking any callbacks.
2034 /// Callee repair callback type
2035 ///
2036 /// The function repair callback is invoked once to rewire the replacement
2037 /// arguments in the body of the new function. The argument replacement info
2038 /// is passed, as build from the registerFunctionSignatureRewrite call, as
2039 /// well as the replacement function and an iteratore to the first
2040 /// replacement argument.
2041 using CalleeRepairCBTy = std::function<void(
2043
2044 /// Abstract call site (ACS) repair callback type
2045 ///
2046 /// The abstract call site repair callback is invoked once on every abstract
2047 /// call site of the replaced function (\see ReplacedFn). The callback needs
2048 /// to provide the operands for the call to the new replacement function.
2049 /// The number and type of the operands appended to the provided vector
2050 /// (second argument) is defined by the number and types determined through
2051 /// the replacement type vector (\see ReplacementTypes). The first argument
2052 /// is the ArgumentReplacementInfo object registered with the Attributor
2053 /// through the registerFunctionSignatureRewrite call.
2055 std::function<void(const ArgumentReplacementInfo &, AbstractCallSite,
2057
2058 /// Simple getters, see the corresponding members for details.
2059 ///{
2060
2061 Attributor &getAttributor() const { return A; }
2062 const Function &getReplacedFn() const { return ReplacedFn; }
2063 const Argument &getReplacedArg() const { return ReplacedArg; }
2064 unsigned getNumReplacementArgs() const { return ReplacementTypes.size(); }
2066 return ReplacementTypes;
2067 }
2068
2069 ///}
2070
2071 private:
2072 /// Constructor that takes the argument to be replaced, the types of
2073 /// the replacement arguments, as well as callbacks to repair the call sites
2074 /// and new function after the replacement happened.
2076 ArrayRef<Type *> ReplacementTypes,
2077 CalleeRepairCBTy &&CalleeRepairCB,
2078 ACSRepairCBTy &&ACSRepairCB)
2079 : A(A), ReplacedFn(*Arg.getParent()), ReplacedArg(Arg),
2080 ReplacementTypes(ReplacementTypes.begin(), ReplacementTypes.end()),
2081 CalleeRepairCB(std::move(CalleeRepairCB)),
2082 ACSRepairCB(std::move(ACSRepairCB)) {}
2083
2084 /// Reference to the attributor to allow access from the callbacks.
2085 Attributor &A;
2086
2087 /// The "old" function replaced by ReplacementFn.
2088 const Function &ReplacedFn;
2089
2090 /// The "old" argument replaced by new ones defined via ReplacementTypes.
2091 const Argument &ReplacedArg;
2092
2093 /// The types of the arguments replacing ReplacedArg.
2094 const SmallVector<Type *, 8> ReplacementTypes;
2095
2096 /// Callee repair callback, see CalleeRepairCBTy.
2097 const CalleeRepairCBTy CalleeRepairCB;
2098
2099 /// Abstract call site (ACS) repair callback, see ACSRepairCBTy.
2100 const ACSRepairCBTy ACSRepairCB;
2101
2102 /// Allow access to the private members from the Attributor.
2103 friend struct Attributor;
2104 };
2105
2106 /// Check if we can rewrite a function signature.
2107 ///
2108 /// The argument \p Arg is replaced with new ones defined by the number,
2109 /// order, and types in \p ReplacementTypes.
2110 ///
2111 /// \returns True, if the replacement can be registered, via
2112 /// registerFunctionSignatureRewrite, false otherwise.
2114 ArrayRef<Type *> ReplacementTypes);
2115
2116 /// Register a rewrite for a function signature.
2117 ///
2118 /// The argument \p Arg is replaced with new ones defined by the number,
2119 /// order, and types in \p ReplacementTypes. The rewiring at the call sites is
2120 /// done through \p ACSRepairCB and at the callee site through
2121 /// \p CalleeRepairCB.
2122 ///
2123 /// \returns True, if the replacement was registered, false otherwise.
2125 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2128
2129 /// Check \p Pred on all function call sites.
2130 ///
2131 /// This method will evaluate \p Pred on call sites and return
2132 /// true if \p Pred holds in every call sites. However, this is only possible
2133 /// all call sites are known, hence the function has internal linkage.
2134 /// If true is returned, \p UsedAssumedInformation is set if assumed
2135 /// information was used to skip or simplify potential call sites.
2137 const AbstractAttribute &QueryingAA,
2138 bool RequireAllCallSites,
2139 bool &UsedAssumedInformation);
2140
2141 /// Check \p Pred on all call sites of \p Fn.
2142 ///
2143 /// This method will evaluate \p Pred on call sites and return
2144 /// true if \p Pred holds in every call sites. However, this is only possible
2145 /// all call sites are known, hence the function has internal linkage.
2146 /// If true is returned, \p UsedAssumedInformation is set if assumed
2147 /// information was used to skip or simplify potential call sites.
2149 const Function &Fn, bool RequireAllCallSites,
2150 const AbstractAttribute *QueryingAA,
2151 bool &UsedAssumedInformation,
2152 bool CheckPotentiallyDead = false);
2153
2154 /// Check \p Pred on all values potentially returned by \p F.
2155 ///
2156 /// This method will evaluate \p Pred on all values potentially returned by
2157 /// the function associated with \p QueryingAA. The returned values are
2158 /// matched with their respective return instructions. Returns true if \p Pred
2159 /// holds on all of them.
2161 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred,
2162 const AbstractAttribute &QueryingAA);
2163
2164 /// Check \p Pred on all values potentially returned by the function
2165 /// associated with \p QueryingAA.
2166 ///
2167 /// This is the context insensitive version of the method above.
2168 bool checkForAllReturnedValues(function_ref<bool(Value &)> Pred,
2169 const AbstractAttribute &QueryingAA);
2170
2171 /// Check \p Pred on all instructions in \p Fn with an opcode present in
2172 /// \p Opcodes.
2173 ///
2174 /// This method will evaluate \p Pred on all instructions with an opcode
2175 /// present in \p Opcode and return true if \p Pred holds on all of them.
2177 const Function *Fn,
2178 const AbstractAttribute &QueryingAA,
2179 const ArrayRef<unsigned> &Opcodes,
2180 bool &UsedAssumedInformation,
2181 bool CheckBBLivenessOnly = false,
2182 bool CheckPotentiallyDead = false);
2183
2184 /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
2185 ///
2186 /// This method will evaluate \p Pred on all instructions with an opcode
2187 /// present in \p Opcode and return true if \p Pred holds on all of them.
2189 const AbstractAttribute &QueryingAA,
2190 const ArrayRef<unsigned> &Opcodes,
2191 bool &UsedAssumedInformation,
2192 bool CheckBBLivenessOnly = false,
2193 bool CheckPotentiallyDead = false);
2194
2195 /// Check \p Pred on all call-like instructions (=CallBased derived).
2196 ///
2197 /// See checkForAllCallLikeInstructions(...) for more information.
2199 const AbstractAttribute &QueryingAA,
2200 bool &UsedAssumedInformation,
2201 bool CheckBBLivenessOnly = false,
2202 bool CheckPotentiallyDead = false) {
2204 Pred, QueryingAA,
2205 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
2206 (unsigned)Instruction::Call},
2207 UsedAssumedInformation, CheckBBLivenessOnly, CheckPotentiallyDead);
2208 }
2209
2210 /// Check \p Pred on all Read/Write instructions.
2211 ///
2212 /// This method will evaluate \p Pred on all instructions that read or write
2213 /// to memory present in the information cache and return true if \p Pred
2214 /// holds on all of them.
2216 AbstractAttribute &QueryingAA,
2217 bool &UsedAssumedInformation);
2218
2219 /// Create a shallow wrapper for \p F such that \p F has internal linkage
2220 /// afterwards. It also sets the original \p F 's name to anonymous
2221 ///
2222 /// A wrapper is a function with the same type (and attributes) as \p F
2223 /// that will only call \p F and return the result, if any.
2224 ///
2225 /// Assuming the declaration of looks like:
2226 /// rty F(aty0 arg0, ..., atyN argN);
2227 ///
2228 /// The wrapper will then look as follows:
2229 /// rty wrapper(aty0 arg0, ..., atyN argN) {
2230 /// return F(arg0, ..., argN);
2231 /// }
2232 ///
2233 static void createShallowWrapper(Function &F);
2234
2235 /// Returns true if the function \p F can be internalized. i.e. it has a
2236 /// compatible linkage.
2237 static bool isInternalizable(Function &F);
2238
2239 /// Make another copy of the function \p F such that the copied version has
2240 /// internal linkage afterwards and can be analysed. Then we replace all uses
2241 /// of the original function to the copied one
2242 ///
2243 /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
2244 /// linkage can be internalized because these linkages guarantee that other
2245 /// definitions with the same name have the same semantics as this one.
2246 ///
2247 /// This will only be run if the `attributor-allow-deep-wrappers` option is
2248 /// set, or if the function is called with \p Force set to true.
2249 ///
2250 /// If the function \p F failed to be internalized the return value will be a
2251 /// null pointer.
2252 static Function *internalizeFunction(Function &F, bool Force = false);
2253
2254 /// Make copies of each function in the set \p FnSet such that the copied
2255 /// version has internal linkage afterwards and can be analysed. Then we
2256 /// replace all uses of the original function to the copied one. The map
2257 /// \p FnMap contains a mapping of functions to their internalized versions.
2258 ///
2259 /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
2260 /// linkage can be internalized because these linkages guarantee that other
2261 /// definitions with the same name have the same semantics as this one.
2262 ///
2263 /// This version will internalize all the functions in the set \p FnSet at
2264 /// once and then replace the uses. This prevents internalized functions being
2265 /// called by external functions when there is an internalized version in the
2266 /// module.
2269
2270 /// Return the data layout associated with the anchor scope.
2271 const DataLayout &getDataLayout() const { return InfoCache.DL; }
2272
2273 /// The allocator used to allocate memory, e.g. for `AbstractAttribute`s.
2275
2276private:
2277 /// This method will do fixpoint iteration until fixpoint or the
2278 /// maximum iteration count is reached.
2279 ///
2280 /// If the maximum iteration count is reached, This method will
2281 /// indicate pessimistic fixpoint on attributes that transitively depend
2282 /// on attributes that were scheduled for an update.
2283 void runTillFixpoint();
2284
2285 /// Gets called after scheduling, manifests attributes to the LLVM IR.
2286 ChangeStatus manifestAttributes();
2287
2288 /// Gets called after attributes have been manifested, cleans up the IR.
2289 /// Deletes dead functions, blocks and instructions.
2290 /// Rewrites function signitures and updates the call graph.
2291 ChangeStatus cleanupIR();
2292
2293 /// Identify internal functions that are effectively dead, thus not reachable
2294 /// from a live entry point. The functions are added to ToBeDeletedFunctions.
2295 void identifyDeadInternalFunctions();
2296
2297 /// Run `::update` on \p AA and track the dependences queried while doing so.
2298 /// Also adjust the state if we know further updates are not necessary.
2299 ChangeStatus updateAA(AbstractAttribute &AA);
2300
2301 /// Remember the dependences on the top of the dependence stack such that they
2302 /// may trigger further updates. (\see DependenceStack)
2303 void rememberDependences();
2304
2305 /// Determine if CallBase context in \p IRP should be propagated.
2306 bool shouldPropagateCallBaseContext(const IRPosition &IRP);
2307
2308 /// Apply all requested function signature rewrites
2309 /// (\see registerFunctionSignatureRewrite) and return Changed if the module
2310 /// was altered.
2312 rewriteFunctionSignatures(SmallSetVector<Function *, 8> &ModifiedFns);
2313
2314 /// Check if the Attribute \p AA should be seeded.
2315 /// See getOrCreateAAFor.
2316 bool shouldSeedAttribute(AbstractAttribute &AA);
2317
2318 /// A nested map to lookup abstract attributes based on the argument position
2319 /// on the outer level, and the addresses of the static member (AAType::ID) on
2320 /// the inner level.
2321 ///{
2322 using AAMapKeyTy = std::pair<const char *, IRPosition>;
2324 ///}
2325
2326 /// Map to remember all requested signature changes (= argument replacements).
2328 ArgumentReplacementMap;
2329
2330 /// The set of functions we are deriving attributes for.
2331 SetVector<Function *> &Functions;
2332
2333 /// The information cache that holds pre-processed (LLVM-IR) information.
2334 InformationCache &InfoCache;
2335
2336 /// Abstract Attribute dependency graph
2337 AADepGraph DG;
2338
2339 /// Set of functions for which we modified the content such that it might
2340 /// impact the call graph.
2341 SmallSetVector<Function *, 8> CGModifiedFunctions;
2342
2343 /// Information about a dependence. If FromAA is changed ToAA needs to be
2344 /// updated as well.
2345 struct DepInfo {
2346 const AbstractAttribute *FromAA;
2347 const AbstractAttribute *ToAA;
2348 DepClassTy DepClass;
2349 };
2350
2351 /// The dependence stack is used to track dependences during an
2352 /// `AbstractAttribute::update` call. As `AbstractAttribute::update` can be
2353 /// recursive we might have multiple vectors of dependences in here. The stack
2354 /// size, should be adjusted according to the expected recursion depth and the
2355 /// inner dependence vector size to the expected number of dependences per
2356 /// abstract attribute. Since the inner vectors are actually allocated on the
2357 /// stack we can be generous with their size.
2358 using DependenceVector = SmallVector<DepInfo, 8>;
2360
2361 /// A set to remember the functions we already assume to be live and visited.
2362 DenseSet<const Function *> VisitedFunctions;
2363
2364 /// Uses we replace with a new value after manifest is done. We will remove
2365 /// then trivially dead instructions as well.
2366 SmallMapVector<Use *, Value *, 32> ToBeChangedUses;
2367
2368 /// Values we replace with a new value after manifest is done. We will remove
2369 /// then trivially dead instructions as well.
2371 ToBeChangedValues;
2372
2373 /// Instructions we replace with `unreachable` insts after manifest is done.
2374 SmallSetVector<WeakVH, 16> ToBeChangedToUnreachableInsts;
2375
2376 /// Invoke instructions with at least a single dead successor block.
2377 SmallSetVector<WeakVH, 16> InvokeWithDeadSuccessor;
2378
2379 /// A flag that indicates which stage of the process we are in. Initially, the
2380 /// phase is SEEDING. Phase is changed in `Attributor::run()`
2381 enum class AttributorPhase {
2382 SEEDING,
2383 UPDATE,
2384 MANIFEST,
2385 CLEANUP,
2386 } Phase = AttributorPhase::SEEDING;
2387
2388 /// The current initialization chain length. Tracked to avoid stack overflows.
2389 unsigned InitializationChainLength = 0;
2390
2391 /// Functions, blocks, and instructions we delete after manifest is done.
2392 ///
2393 ///{
2394 SmallPtrSet<BasicBlock *, 8> ManifestAddedBlocks;
2395 SmallSetVector<Function *, 8> ToBeDeletedFunctions;
2396 SmallSetVector<BasicBlock *, 8> ToBeDeletedBlocks;
2397 SmallSetVector<WeakVH, 8> ToBeDeletedInsts;
2398 ///}
2399
2400 /// Container with all the query AAs that requested an update via
2401 /// registerForUpdate.
2402 SmallSetVector<AbstractAttribute *, 16> QueryAAsAwaitingUpdate;
2403
2404 /// User provided configuration for this Attributor instance.
2405 const AttributorConfig Configuration;
2406
2407 friend AADepGraph;
2408 friend AttributorCallGraph;
2409};
2410
2411/// An interface to query the internal state of an abstract attribute.
2412///
2413/// The abstract state is a minimal interface that allows the Attributor to
2414/// communicate with the abstract attributes about their internal state without
2415/// enforcing or exposing implementation details, e.g., the (existence of an)
2416/// underlying lattice.
2417///
2418/// It is sufficient to be able to query if a state is (1) valid or invalid, (2)
2419/// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint
2420/// was reached or (4) a pessimistic fixpoint was enforced.
2421///
2422/// All methods need to be implemented by the subclass. For the common use case,
2423/// a single boolean state or a bit-encoded state, the BooleanState and
2424/// {Inc,Dec,Bit}IntegerState classes are already provided. An abstract
2425/// attribute can inherit from them to get the abstract state interface and
2426/// additional methods to directly modify the state based if needed. See the
2427/// class comments for help.
2429 virtual ~AbstractState() = default;
2430
2431 /// Return if this abstract state is in a valid state. If false, no
2432 /// information provided should be used.
2433 virtual bool isValidState() const = 0;
2434
2435 /// Return if this abstract state is fixed, thus does not need to be updated
2436 /// if information changes as it cannot change itself.
2437 virtual bool isAtFixpoint() const = 0;
2438
2439 /// Indicate that the abstract state should converge to the optimistic state.
2440 ///
2441 /// This will usually make the optimistically assumed state the known to be
2442 /// true state.
2443 ///
2444 /// \returns ChangeStatus::UNCHANGED as the assumed value should not change.
2446
2447 /// Indicate that the abstract state should converge to the pessimistic state.
2448 ///
2449 /// This will usually revert the optimistically assumed state to the known to
2450 /// be true state.
2451 ///
2452 /// \returns ChangeStatus::CHANGED as the assumed value may change.
2454};
2455
2456/// Simple state with integers encoding.
2457///
2458/// The interface ensures that the assumed bits are always a subset of the known
2459/// bits. Users can only add known bits and, except through adding known bits,
2460/// they can only remove assumed bits. This should guarantee monotoniticy and
2461/// thereby the existence of a fixpoint (if used corretly). The fixpoint is
2462/// reached when the assumed and known state/bits are equal. Users can
2463/// force/inidicate a fixpoint. If an optimistic one is indicated, the known
2464/// state will catch up with the assumed one, for a pessimistic fixpoint it is
2465/// the other way around.
2466template <typename base_ty, base_ty BestState, base_ty WorstState>
2468 using base_t = base_ty;
2469
2470 IntegerStateBase() = default;
2472
2473 /// Return the best possible representable state.
2474 static constexpr base_t getBestState() { return BestState; }
2475 static constexpr base_t getBestState(const IntegerStateBase &) {
2476 return getBestState();
2477 }
2478
2479 /// Return the worst possible representable state.
2480 static constexpr base_t getWorstState() { return WorstState; }
2481 static constexpr base_t getWorstState(const IntegerStateBase &) {
2482 return getWorstState();
2483 }
2484
2485 /// See AbstractState::isValidState()
2486 /// NOTE: For now we simply pretend that the worst possible state is invalid.
2487 bool isValidState() const override { return Assumed != getWorstState(); }
2488
2489 /// See AbstractState::isAtFixpoint()
2490 bool isAtFixpoint() const override { return Assumed == Known; }
2491
2492 /// See AbstractState::indicateOptimisticFixpoint(...)
2494 Known = Assumed;
2496 }
2497
2498 /// See AbstractState::indicatePessimisticFixpoint(...)
2500 Assumed = Known;
2501 return ChangeStatus::CHANGED;
2502 }
2503
2504 /// Return the known state encoding
2505 base_t getKnown() const { return Known; }
2506
2507 /// Return the assumed state encoding.
2508 base_t getAssumed() const { return Assumed; }
2509
2510 /// Equality for IntegerStateBase.
2511 bool
2513 return this->getAssumed() == R.getAssumed() &&
2514 this->getKnown() == R.getKnown();
2515 }
2516
2517 /// Inequality for IntegerStateBase.
2518 bool
2520 return !(*this == R);
2521 }
2522
2523 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2524 /// intended that only information assumed in both states will be assumed in
2525 /// this one afterwards.
2527 handleNewAssumedValue(R.getAssumed());
2528 }
2529
2530 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2531 /// intended that information known in either state will be known in
2532 /// this one afterwards.
2534 handleNewKnownValue(R.getKnown());
2535 }
2536
2538 joinOR(R.getAssumed(), R.getKnown());
2539 }
2540
2542 joinAND(R.getAssumed(), R.getKnown());
2543 }
2544
2545protected:
2546 /// Handle a new assumed value \p Value. Subtype dependent.
2548
2549 /// Handle a new known value \p Value. Subtype dependent.
2551
2552 /// Handle a value \p Value. Subtype dependent.
2553 virtual void joinOR(base_t AssumedValue, base_t KnownValue) = 0;
2554
2555 /// Handle a new assumed value \p Value. Subtype dependent.
2556 virtual void joinAND(base_t AssumedValue, base_t KnownValue) = 0;
2557
2558 /// The known state encoding in an integer of type base_t.
2560
2561 /// The assumed state encoding in an integer of type base_t.
2563};
2564
2565/// Specialization of the integer state for a bit-wise encoding.
2566template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2567 base_ty WorstState = 0>
2569 : public IntegerStateBase<base_ty, BestState, WorstState> {
2571 using base_t = base_ty;
2572 BitIntegerState() = default;
2574
2575 /// Return true if the bits set in \p BitsEncoding are "known bits".
2576 bool isKnown(base_t BitsEncoding) const {
2577 return (this->Known & BitsEncoding) == BitsEncoding;
2578 }
2579
2580 /// Return true if the bits set in \p BitsEncoding are "assumed bits".
2581 bool isAssumed(base_t BitsEncoding) const {
2582 return (this->Assumed & BitsEncoding) == BitsEncoding;
2583 }
2584
2585 /// Add the bits in \p BitsEncoding to the "known bits".
2587 // Make sure we never miss any "known bits".
2588 this->Assumed |= Bits;
2589 this->Known |= Bits;
2590 return *this;
2591 }
2592
2593 /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known.
2595 return intersectAssumedBits(~BitsEncoding);
2596 }
2597
2598 /// Remove the bits in \p BitsEncoding from the "known bits".
2600 this->Known = (this->Known & ~BitsEncoding);
2601 return *this;
2602 }
2603
2604 /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones.
2606 // Make sure we never lose any "known bits".
2607 this->Assumed = (this->Assumed & BitsEncoding) | this->Known;
2608 return *this;
2609 }
2610
2611private:
2612 void handleNewAssumedValue(base_t Value) override {
2614 }
2615 void handleNewKnownValue(base_t Value) override { addKnownBits(Value); }
2616 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2617 this->Known |= KnownValue;
2618 this->Assumed |= AssumedValue;
2619 }
2620 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2621 this->Known &= KnownValue;
2622 this->Assumed &= AssumedValue;
2623 }
2624};
2625
2626/// Specialization of the integer state for an increasing value, hence ~0u is
2627/// the best state and 0 the worst.
2628template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2629 base_ty WorstState = 0>
2631 : public IntegerStateBase<base_ty, BestState, WorstState> {
2633 using base_t = base_ty;
2634
2637
2638 /// Return the best possible representable state.
2639 static constexpr base_t getBestState() { return BestState; }
2640 static constexpr base_t
2642 return getBestState();
2643 }
2644
2645 /// Take minimum of assumed and \p Value.
2647 // Make sure we never lose "known value".
2648 this->Assumed = std::max(std::min(this->Assumed, Value), this->Known);
2649 return *this;
2650 }
2651
2652 /// Take maximum of known and \p Value.
2654 // Make sure we never lose "known value".
2655 this->Assumed = std::max(Value, this->Assumed);
2656 this->Known = std::max(Value, this->Known);
2657 return *this;
2658 }
2659
2660private:
2661 void handleNewAssumedValue(base_t Value) override {
2663 }
2664 void handleNewKnownValue(base_t Value) override { takeKnownMaximum(Value); }
2665 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2666 this->Known = std::max(this->Known, KnownValue);
2667 this->Assumed = std::max(this->Assumed, AssumedValue);
2668 }
2669 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2670 this->Known = std::min(this->Known, KnownValue);
2671 this->Assumed = std::min(this->Assumed, AssumedValue);
2672 }
2673};
2674
2675/// Specialization of the integer state for a decreasing value, hence 0 is the
2676/// best state and ~0u the worst.
2677template <typename base_ty = uint32_t>
2678struct DecIntegerState : public IntegerStateBase<base_ty, 0, ~base_ty(0)> {
2679 using base_t = base_ty;
2680
2681 /// Take maximum of assumed and \p Value.
2683 // Make sure we never lose "known value".
2684 this->Assumed = std::min(std::max(this->Assumed, Value), this->Known);
2685 return *this;
2686 }
2687
2688 /// Take minimum of known and \p Value.
2690 // Make sure we never lose "known value".
2691 this->Assumed = std::min(Value, this->Assumed);
2692 this->Known = std::min(Value, this->Known);
2693 return *this;
2694 }
2695
2696private:
2697 void handleNewAssumedValue(base_t Value) override {
2699 }
2700 void handleNewKnownValue(base_t Value) override { takeKnownMinimum(Value); }
2701 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2702 this->Assumed = std::min(this->Assumed, KnownValue);
2703 this->Assumed = std::min(this->Assumed, AssumedValue);
2704 }
2705 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2706 this->Assumed = std::max(this->Assumed, KnownValue);
2707 this->Assumed = std::max(this->Assumed, AssumedValue);
2708 }
2709};
2710
2711/// Simple wrapper for a single bit (boolean) state.
2712struct BooleanState : public IntegerStateBase<bool, true, false> {
2715
2716 BooleanState() = default;
2718
2719 /// Set the assumed value to \p Value but never below the known one.
2720 void setAssumed(bool Value) { Assumed &= (Known | Value); }
2721
2722 /// Set the known and asssumed value to \p Value.
2723 void setKnown(bool Value) {
2724 Known |= Value;
2725 Assumed |= Value;
2726 }
2727
2728 /// Return true if the state is assumed to hold.
2729 bool isAssumed() const { return getAssumed(); }
2730
2731 /// Return true if the state is known to hold.
2732 bool isKnown() const { return getKnown(); }
2733
2734private:
2735 void handleNewAssumedValue(base_t Value) override {
2736 if (!Value)
2737 Assumed = Known;
2738 }
2739 void handleNewKnownValue(base_t Value) override {
2740 if (Value)
2741 Known = (Assumed = Value);
2742 }
2743 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2744 Known |= KnownValue;
2745 Assumed |= AssumedValue;
2746 }
2747 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2748 Known &= KnownValue;
2749 Assumed &= AssumedValue;
2750 }
2751};
2752
2753/// State for an integer range.
2755
2756 /// Bitwidth of the associated value.
2758
2759 /// State representing assumed range, initially set to empty.
2761
2762 /// State representing known range, initially set to [-inf, inf].
2764
2767 Known(ConstantRange::getFull(BitWidth)) {}
2768
2770 : BitWidth(CR.getBitWidth()), Assumed(CR),
2772
2773 /// Return the worst possible representable state.
2775 return ConstantRange::getFull(BitWidth);
2776 }
2777
2778 /// Return the best possible representable state.
2780 return ConstantRange::getEmpty(BitWidth);
2781 }
2783 return getBestState(IRS.getBitWidth());
2784 }
2785
2786 /// Return associated values' bit width.
2787 uint32_t getBitWidth() const { return BitWidth; }
2788
2789 /// See AbstractState::isValidState()
2790 bool isValidState() const override {
2791 return BitWidth > 0 && !Assumed.isFullSet();
2792 }
2793
2794 /// See AbstractState::isAtFixpoint()
2795 bool isAtFixpoint() const override { return Assumed == Known; }
2796
2797 /// See AbstractState::indicateOptimisticFixpoint(...)
2799 Known = Assumed;
2800 return ChangeStatus::CHANGED;
2801 }
2802
2803 /// See AbstractState::indicatePessimisticFixpoint(...)
2805 Assumed = Known;
2806 return ChangeStatus::CHANGED;
2807 }
2808
2809 /// Return the known state encoding
2810 ConstantRange getKnown() const { return Known; }
2811
2812 /// Return the assumed state encoding.
2814
2815 /// Unite assumed range with the passed state.
2817 // Don't lose a known range.
2819 }
2820
2821 /// See IntegerRangeState::unionAssumed(..).
2823 unionAssumed(R.getAssumed());
2824 }
2825
2826 /// Intersect known range with the passed state.
2830 }
2831
2832 /// See IntegerRangeState::intersectKnown(..).
2834 intersectKnown(R.getKnown());
2835 }
2836
2837 /// Equality for IntegerRangeState.
2838 bool operator==(const IntegerRangeState &R) const {
2839 return getAssumed() == R.getAssumed() && getKnown() == R.getKnown();
2840 }
2841
2842 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2843 /// intended that only information assumed in both states will be assumed in
2844 /// this one afterwards.
2846 // NOTE: `^=` operator seems like `intersect` but in this case, we need to
2847 // take `union`.
2848 unionAssumed(R);
2849 return *this;
2850 }
2851
2853 // NOTE: `&=` operator seems like `intersect` but in this case, we need to
2854 // take `union`.
2855 Known = Known.unionWith(R.getKnown());
2856 Assumed = Assumed.unionWith(R.getAssumed());
2857 return *this;
2858 }
2859};
2860
2861/// Simple state for a set.
2862///
2863/// This represents a state containing a set of values. The interface supports
2864/// modelling sets that contain all possible elements. The state's internal
2865/// value is modified using union or intersection operations.
2866template <typename BaseTy> struct SetState : public AbstractState {
2867 /// A wrapper around a set that has semantics for handling unions and
2868 /// intersections with a "universal" set that contains all elements.
2870 /// Creates a universal set with no concrete elements or an empty set.
2871 SetContents(bool Universal) : Universal(Universal) {}
2872
2873 /// Creates a non-universal set with concrete values.
2874 SetContents(const DenseSet<BaseTy> &Assumptions)
2875 : Universal(false), Set(Assumptions) {}
2876
2877 SetContents(bool Universal, const DenseSet<BaseTy> &Assumptions)
2878 : Universal(Universal), Set(Assumptions) {}
2879
2880 const DenseSet<BaseTy> &getSet() const { return Set; }
2881
2882 bool isUniversal() const { return Universal; }
2883
2884 bool empty() const { return Set.empty() && !Universal; }
2885
2886 /// Finds A := A ^ B where A or B could be the "Universal" set which
2887 /// contains every possible attribute. Returns true if changes were made.
2889 bool IsUniversal = Universal;
2890 unsigned Size = Set.size();
2891
2892 // A := A ^ U = A
2893 if (RHS.isUniversal())
2894 return false;
2895
2896 // A := U ^ B = B
2897 if (Universal)
2898 Set = RHS.getSet();
2899 else
2900 set_intersect(Set, RHS.getSet());
2901
2902 Universal &= RHS.isUniversal();
2903 return IsUniversal != Universal || Size != Set.size();
2904 }
2905
2906 /// Finds A := A u B where A or B could be the "Universal" set which
2907 /// contains every possible attribute. returns true if changes were made.
2908 bool getUnion(const SetContents &RHS) {
2909 bool IsUniversal = Universal;
2910 unsigned Size = Set.size();
2911
2912 // A := A u U = U = U u B
2913 if (!RHS.isUniversal() && !Universal)
2914 set_union(Set, RHS.getSet());
2915
2916 Universal |= RHS.isUniversal();
2917 return IsUniversal != Universal || Size != Set.size();
2918 }
2919
2920 private:
2921 /// Indicates if this set is "universal", containing every possible element.
2922 bool Universal;
2923
2924 /// The set of currently active assumptions.
2925 DenseSet<BaseTy> Set;
2926 };
2927
2928 SetState() : Known(false), Assumed(true), IsAtFixedpoint(false) {}
2929
2930 /// Initializes the known state with an initial set and initializes the
2931 /// assumed state as universal.
2933 : Known(Known), Assumed(true), IsAtFixedpoint(false) {}
2934
2935 /// See AbstractState::isValidState()
2936 bool isValidState() const override { return !Assumed.empty(); }
2937
2938 /// See AbstractState::isAtFixpoint()
2939 bool isAtFixpoint() const override { return IsAtFixedpoint; }
2940
2941 /// See AbstractState::indicateOptimisticFixpoint(...)
2943 IsAtFixedpoint = true;
2944 Known = Assumed;
2946 }
2947
2948 /// See AbstractState::indicatePessimisticFixpoint(...)
2950 IsAtFixedpoint = true;
2951 Assumed = Known;
2952 return ChangeStatus::CHANGED;
2953 }
2954
2955 /// Return the known state encoding.
2956 const SetContents &getKnown() const { return Known; }
2957
2958 /// Return the assumed state encoding.
2959 const SetContents &getAssumed() const { return Assumed; }
2960
2961 /// Returns if the set state contains the element.
2962 bool setContains(const BaseTy &Elem) const {
2963 return Assumed.getSet().contains(Elem) || Known.getSet().contains(Elem);
2964 }
2965
2966 /// Performs the set intersection between this set and \p RHS. Returns true if
2967 /// changes were made.
2969 unsigned SizeBefore = Assumed.getSet().size();
2970
2971 // Get intersection and make sure that the known set is still a proper
2972 // subset of the assumed set. A := K u (A ^ R).
2973 Assumed.getIntersection(RHS);
2974 Assumed.getUnion(Known);
2975
2976 return SizeBefore != Assumed.getSet().size();
2977 }
2978
2979 /// Performs the set union between this set and \p RHS. Returns true if
2980 /// changes were made.
2981 bool getUnion(const SetContents &RHS) { return Assumed.getUnion(RHS); }
2982
2983private:
2984 /// The set of values known for this state.
2985 SetContents Known;
2986
2987 /// The set of assumed values for this state.
2988 SetContents Assumed;
2989
2990 bool IsAtFixedpoint;
2991};
2992
2993/// Helper struct necessary as the modular build fails if the virtual method
2994/// IRAttribute::manifest is defined in the Attributor.cpp.
2996 static ChangeStatus manifestAttrs(Attributor &A, const IRPosition &IRP,
2997 const ArrayRef<Attribute> &DeducedAttrs,
2998 bool ForceReplace = false);
2999};
3000
3001/// Helper to tie a abstract state implementation to an abstract attribute.
3002template <typename StateTy, typename BaseType, class... Ts>
3003struct StateWrapper : public BaseType, public StateTy {
3004 /// Provide static access to the type of the state.
3006
3007 StateWrapper(const IRPosition &IRP, Ts... Args)
3008 : BaseType(IRP), StateTy(Args...) {}
3009
3010 /// See AbstractAttribute::getState(...).
3011 StateType &getState() override { return *this; }
3012
3013 /// See AbstractAttribute::getState(...).
3014 const StateType &getState() const override { return *this; }
3015};
3016
3017/// Helper class that provides common functionality to manifest IR attributes.
3018template <Attribute::AttrKind AK, typename BaseType>
3019struct IRAttribute : public BaseType {
3020 IRAttribute(const IRPosition &IRP) : BaseType(IRP) {}
3021
3022 /// See AbstractAttribute::initialize(...).
3023 void initialize(Attributor &A) override {
3024 const IRPosition &IRP = this->getIRPosition();
3025 if (isa<UndefValue>(IRP.getAssociatedValue()) ||
3026 this->hasAttr(getAttrKind(), /* IgnoreSubsumingPositions */ false,
3027 &A)) {
3028 this->getState().indicateOptimisticFixpoint();
3029 return;
3030 }
3031
3032 bool IsFnInterface = IRP.isFnInterfaceKind();
3033 const Function *FnScope = IRP.getAnchorScope();
3034 // TODO: Not all attributes require an exact definition. Find a way to
3035 // enable deduction for some but not all attributes in case the
3036 // definition might be changed at runtime, see also
3037 // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
3038 // TODO: We could always determine abstract attributes and if sufficient
3039 // information was found we could duplicate the functions that do not
3040 // have an exact definition.
3041 if (IsFnInterface && (!FnScope || !A.isFunctionIPOAmendable(*FnScope)))
3042 this->getState().indicatePessimisticFixpoint();
3043 }
3044
3045 /// See AbstractAttribute::manifest(...).
3047 if (isa<UndefValue>(this->getIRPosition().getAssociatedValue()))
3049 SmallVector<Attribute, 4> DeducedAttrs;
3050 getDeducedAttributes(this->getAnchorValue().getContext(), DeducedAttrs);
3051 return IRAttributeManifest::manifestAttrs(A, this->getIRPosition(),
3052 DeducedAttrs);
3053 }
3054
3055 /// Return the kind that identifies the abstract attribute implementation.
3056 Attribute::AttrKind getAttrKind() const { return AK; }
3057
3058 /// Return the deduced attributes in \p Attrs.
3060 SmallVectorImpl<Attribute> &Attrs) const {
3061 Attrs.emplace_back(Attribute::get(Ctx, getAttrKind()));
3062 }
3063};
3064
3065/// Base struct for all "concrete attribute" deductions.
3066///
3067/// The abstract attribute is a minimal interface that allows the Attributor to
3068/// orchestrate the abstract/fixpoint analysis. The design allows to hide away
3069/// implementation choices made for the subclasses but also to structure their
3070/// implementation and simplify the use of other abstract attributes in-flight.
3071///
3072/// To allow easy creation of new attributes, most methods have default
3073/// implementations. The ones that do not are generally straight forward, except
3074/// `AbstractAttribute::updateImpl` which is the location of most reasoning
3075/// associated with the abstract attribute. The update is invoked by the
3076/// Attributor in case the situation used to justify the current optimistic
3077/// state might have changed. The Attributor determines this automatically
3078/// by monitoring the `Attributor::getAAFor` calls made by abstract attributes.
3079///
3080/// The `updateImpl` method should inspect the IR and other abstract attributes
3081/// in-flight to justify the best possible (=optimistic) state. The actual
3082/// implementation is, similar to the underlying abstract state encoding, not
3083/// exposed. In the most common case, the `updateImpl` will go through a list of
3084/// reasons why its optimistic state is valid given the current information. If
3085/// any combination of them holds and is sufficient to justify the current
3086/// optimistic state, the method shall return UNCHAGED. If not, the optimistic
3087/// state is adjusted to the situation and the method shall return CHANGED.
3088///
3089/// If the manifestation of the "concrete attribute" deduced by the subclass
3090/// differs from the "default" behavior, which is a (set of) LLVM-IR
3091/// attribute(s) for an argument, call site argument, function return value, or
3092/// function, the `AbstractAttribute::manifest` method should be overloaded.
3093///
3094/// NOTE: If the state obtained via getState() is INVALID, thus if
3095/// AbstractAttribute::getState().isValidState() returns false, no
3096/// information provided by the methods of this class should be used.
3097/// NOTE: The Attributor currently has certain limitations to what we can do.
3098/// As a general rule of thumb, "concrete" abstract attributes should *for
3099/// now* only perform "backward" information propagation. That means
3100/// optimistic information obtained through abstract attributes should
3101/// only be used at positions that precede the origin of the information
3102/// with regards to the program flow. More practically, information can
3103/// *now* be propagated from instructions to their enclosing function, but
3104/// *not* from call sites to the called function. The mechanisms to allow
3105/// both directions will be added in the future.
3106/// NOTE: The mechanics of adding a new "concrete" abstract attribute are
3107/// described in the file comment.
3110
3112
3113 /// Virtual destructor.
3114 virtual ~AbstractAttribute() = default;
3115
3116 /// This function is used to identify if an \p DGN is of type
3117 /// AbstractAttribute so that the dyn_cast and cast can use such information
3118 /// to cast an AADepGraphNode to an AbstractAttribute.
3119 ///
3120 /// We eagerly return true here because all AADepGraphNodes except for the
3121 /// Synthethis Node are of type AbstractAttribute
3122 static bool classof(const AADepGraphNode *DGN) { return true; }
3123
3124 /// Initialize the state with the information in the Attributor \p A.
3125 ///
3126 /// This function is called by the Attributor once all abstract attributes
3127 /// have been identified. It can and shall be used for task like:
3128 /// - identify existing knowledge in the IR and use it for the "known state"
3129 /// - perform any work that is not going to change over time, e.g., determine
3130 /// a subset of the IR, or attributes in-flight, that have to be looked at
3131 /// in the `updateImpl` method.
3132 virtual void initialize(Attributor &A) {}
3133
3134 /// A query AA is always scheduled as long as we do updates because it does
3135 /// lazy computation that cannot be determined to be done from the outside.
3136 /// However, while query AAs will not be fixed if they do not have outstanding
3137 /// dependences, we will only schedule them like other AAs. If a query AA that
3138 /// received a new query it needs to request an update via
3139 /// `Attributor::requestUpdateForAA`.
3140 virtual bool isQueryAA() const { return false; }
3141
3142 /// Return the internal abstract state for inspection.
3143 virtual StateType &getState() = 0;
3144 virtual const StateType &getState() const = 0;
3145
3146 /// Return an IR position, see struct IRPosition.
3147 const IRPosition &getIRPosition() const { return *this; };
3148 IRPosition &getIRPosition() { return *this; };
3149
3150 /// Helper functions, for debug purposes only.
3151 ///{
3152 void print(raw_ostream &OS) const override;
3153 virtual void printWithDeps(raw_ostream &OS) const;
3154 void dump() const { print(dbgs()); }
3155
3156 /// This function should return the "summarized" assumed state as string.
3157 virtual const std::string getAsStr() const = 0;
3158
3159 /// This function should return the name of the AbstractAttribute
3160 virtual const std::string getName() const = 0;
3161
3162 /// This function should return the address of the ID of the AbstractAttribute
3163 virtual const char *getIdAddr() const = 0;
3164 ///}
3165
3166 /// Allow the Attributor access to the protected methods.
3167 friend struct Attributor;
3168
3169protected:
3170 /// Hook for the Attributor to trigger an update of the internal state.
3171 ///
3172 /// If this attribute is already fixed, this method will return UNCHANGED,
3173 /// otherwise it delegates to `AbstractAttribute::updateImpl`.
3174 ///
3175 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
3177
3178 /// Hook for the Attributor to trigger the manifestation of the information
3179 /// represented by the abstract attribute in the LLVM-IR.
3180 ///
3181 /// \Return CHANGED if the IR was altered, otherwise UNCHANGED.
3184 }
3185
3186 /// Hook to enable custom statistic tracking, called after manifest that
3187 /// resulted in a change if statistics are enabled.
3188 ///
3189 /// We require subclasses to provide an implementation so we remember to
3190 /// add statistics for them.
3191 virtual void trackStatistics() const = 0;
3192
3193 /// The actual update/transfer function which has to be implemented by the
3194 /// derived classes.
3195 ///
3196 /// If it is called, the environment has changed and we have to determine if
3197 /// the current information is still valid or adjust it otherwise.
3198 ///
3199 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
3201};
3202
3203/// Forward declarations of output streams for debug purposes.
3204///
3205///{
3211template <typename base_ty, base_ty BestState, base_ty WorstState>
3215 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
3216 << static_cast<const AbstractState &>(S);
3217}
3218raw_ostream &operator<<(raw_ostream &OS, const IntegerRangeState &State);
3219///}
3220
3221struct AttributorPass : public PassInfoMixin<AttributorPass> {
3223};
3224struct AttributorCGSCCPass : public PassInfoMixin<AttributorCGSCCPass> {
3227};
3228
3229/// Helper function to clamp a state \p S of type \p StateType with the
3230/// information in \p R and indicate/return if \p S did change (as-in update is
3231/// required to be run again).
3232template <typename StateType>
3233ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
3234 auto Assumed = S.getAssumed();
3235 S ^= R;
3236 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
3238}
3239
3240/// ----------------------------------------------------------------------------
3241/// Abstract Attribute Classes
3242/// ----------------------------------------------------------------------------
3243
3244/// An abstract attribute for the returned values of a function.
3246 : public IRAttribute<Attribute::Returned, AbstractAttribute> {
3248
3249 /// Check \p Pred on all returned values.
3250 ///
3251 /// This method will evaluate \p Pred on returned values and return
3252 /// true if (1) all returned values are known, and (2) \p Pred returned true
3253 /// for all returned values.
3254 ///
3255 /// Note: Unlike the Attributor::checkForAllReturnedValuesAndReturnInsts
3256 /// method, this one will not filter dead return instructions.
3258 function_ref<bool(Value &, const SmallSetVector<ReturnInst *, 4> &)> Pred)
3259 const = 0;
3260
3261 using iterator =
3267
3268 virtual size_t getNumReturnValues() const = 0;
3269
3270 /// Create an abstract attribute view for the position \p IRP.
3272 Attributor &A);
3273
3274 /// See AbstractAttribute::getName()
3275 const std::string getName() const override { return "AAReturnedValues"; }
3276
3277 /// See AbstractAttribute::getIdAddr()
3278 const char *getIdAddr() const override { return &ID; }
3279
3280 /// This function should return true if the type of the \p AA is
3281 /// AAReturnedValues
3282 static bool classof(const AbstractAttribute *AA) {
3283 return (AA->getIdAddr() == &ID);
3284 }
3285
3286 /// Unique ID (due to the unique address)
3287 static const char ID;
3288};
3289
3291 : public IRAttribute<Attribute::NoUnwind,
3292 StateWrapper<BooleanState, AbstractAttribute>> {
3294
3295 /// Returns true if nounwind is assumed.
3296 bool isAssumedNoUnwind() const { return getAssumed(); }
3297
3298 /// Returns true if nounwind is known.
3299 bool isKnownNoUnwind() const { return getKnown(); }
3300
3301 /// Create an abstract attribute view for the position \p IRP.
3303
3304 /// See AbstractAttribute::getName()
3305 const std::string getName() const override { return "AANoUnwind"; }
3306
3307 /// See AbstractAttribute::getIdAddr()
3308 const char *getIdAddr() const override { return &ID; }
3309
3310 /// This function should return true if the type of the \p AA is AANoUnwind
3311 static bool classof(const AbstractAttribute *AA) {
3312 return (AA->getIdAddr() == &ID);
3313 }
3314
3315 /// Unique ID (due to the unique address)
3316 static const char ID;
3317};
3318
3320 : public IRAttribute<Attribute::NoSync,
3321 StateWrapper<BooleanState, AbstractAttribute>> {
3323
3324 /// Returns true if "nosync" is assumed.
3325 bool isAssumedNoSync() const { return getAssumed(); }
3326
3327 /// Returns true if "nosync" is known.
3328 bool isKnownNoSync() const { return getKnown(); }
3329
3330 /// Helper function used to determine whether an instruction is non-relaxed
3331 /// atomic. In other words, if an atomic instruction does not have unordered
3332 /// or monotonic ordering
3333 static bool isNonRelaxedAtomic(const Instruction *I);
3334
3335 /// Helper function specific for intrinsics which are potentially volatile.
3336 static bool isNoSyncIntrinsic(const Instruction *I);
3337
3338 /// Helper function to determine if \p CB is an aligned (GPU) barrier. Aligned
3339 /// barriers have to be executed by all threads. The flag \p ExecutedAligned
3340 /// indicates if the call is executed by all threads in a (thread) block in an
3341 /// aligned way. If that is the case, non-aligned barriers are effectively
3342 /// aligned barriers.
3343 static bool isAlignedBarrier(const CallBase &CB, bool ExecutedAligned);
3344
3345 /// Create an abstract attribute view for the position \p IRP.
3347
3348 /// See AbstractAttribute::getName()
3349 const std::string getName() const override { return "AANoSync"; }
3350
3351 /// See AbstractAttribute::getIdAddr()
3352 const char *getIdAddr() const override { return &ID; }
3353
3354 /// This function should return true if the type of the \p AA is AANoSync
3355 static bool classof(const AbstractAttribute *AA) {
3356 return (AA->getIdAddr() == &ID);
3357 }
3358
3359 /// Unique ID (due to the unique address)
3360 static const char ID;
3361};
3362
3363/// An abstract interface for all nonnull attributes.
3365 : public IRAttribute<Attribute::NonNull,
3366 StateWrapper<BooleanState, AbstractAttribute>> {
3368
3369 /// Return true if we assume that the underlying value is nonnull.
3370 bool isAssumedNonNull() const { return getAssumed(); }
3371
3372 /// Return true if we know that underlying value is nonnull.
3373 bool isKnownNonNull() const { return getKnown(); }
3374
3375 /// Create an abstract attribute view for the position \p IRP.
3377
3378 /// See AbstractAttribute::getName()
3379 const std::string getName() const override { return "AANonNull"; }
3380
3381 /// See AbstractAttribute::getIdAddr()
3382 const char *getIdAddr() const override { return &ID; }
3383
3384 /// This function should return true if the type of the \p AA is AANonNull
3385 static bool classof(const AbstractAttribute *AA) {
3386 return (AA->getIdAddr() == &ID);
3387 }
3388
3389 /// Unique ID (due to the unique address)
3390 static const char ID;
3391};
3392
3393/// An abstract attribute for norecurse.
3395 : public IRAttribute<Attribute::NoRecurse,
3396 StateWrapper<BooleanState, AbstractAttribute>> {
3398
3399 /// Return true if "norecurse" is assumed.
3400 bool isAssumedNoRecurse() const { return getAssumed(); }
3401
3402 /// Return true if "norecurse" is known.
3403 bool isKnownNoRecurse() const { return getKnown(); }
3404
3405 /// Create an abstract attribute view for the position \p IRP.
3407
3408 /// See AbstractAttribute::getName()
3409 const std::string getName() const override { return "AANoRecurse"; }
3410
3411 /// See AbstractAttribute::getIdAddr()
3412 const char *getIdAddr() const override { return &ID; }
3413
3414 /// This function should return true if the type of the \p AA is AANoRecurse
3415 static bool classof(const AbstractAttribute *AA) {
3416 return (AA->getIdAddr() == &ID);
3417 }
3418
3419 /// Unique ID (due to the unique address)
3420 static const char ID;
3421};
3422
3423/// An abstract attribute for willreturn.
3425 : public IRAttribute<Attribute::WillReturn,
3426 StateWrapper<BooleanState, AbstractAttribute>> {
3428
3429 /// Return true if "willreturn" is assumed.
3430 bool isAssumedWillReturn() const { return getAssumed(); }
3431
3432 /// Return true if "willreturn" is known.
3433 bool isKnownWillReturn() const { return getKnown(); }
3434
3435 /// Create an abstract attribute view for the position \p IRP.
3437
3438 /// See AbstractAttribute::getName()
3439 const std::string getName() const override { return "AAWillReturn"; }
3440
3441 /// See AbstractAttribute::getIdAddr()
3442 const char *getIdAddr() const override { return &ID; }
3443
3444 /// This function should return true if the type of the \p AA is AAWillReturn
3445 static bool classof(const AbstractAttribute *AA) {
3446 return (AA->getIdAddr() == &ID);
3447 }
3448
3449 /// Unique ID (due to the unique address)
3450 static const char ID;
3451};
3452
3453/// An abstract attribute for undefined behavior.
3455 : public StateWrapper<BooleanState, AbstractAttribute> {
3458
3459 /// Return true if "undefined behavior" is assumed.
3460 bool isAssumedToCauseUB() const { return getAssumed(); }
3461
3462 /// Return true if "undefined behavior" is assumed for a specific instruction.
3463 virtual bool isAssumedToCauseUB(Instruction *I) const = 0;
3464
3465 /// Return true if "undefined behavior" is known.
3466 bool isKnownToCauseUB() const { return getKnown(); }
3467
3468 /// Return true if "undefined behavior" is known for a specific instruction.
3469 virtual bool isKnownToCauseUB(Instruction *I) const = 0;
3470
3471 /// Create an abstract attribute view for the position \p IRP.
3473 Attributor &A);
3474
3475 /// See AbstractAttribute::getName()
3476 const std::string getName() const override { return "AAUndefinedBehavior"; }
3477
3478 /// See AbstractAttribute::getIdAddr()
3479 const char *getIdAddr() const override { return &ID; }
3480
3481 /// This function should return true if the type of the \p AA is
3482 /// AAUndefineBehavior
3483 static bool classof(const AbstractAttribute *AA) {
3484 return (AA->getIdAddr() == &ID);
3485 }
3486
3487 /// Unique ID (due to the unique address)
3488 static const char ID;
3489};
3490
3491/// An abstract interface to determine reachability of point A to B.
3493 : public StateWrapper<BooleanState, AbstractAttribute> {
3496
3497 /// Returns true if 'From' instruction is assumed to reach, 'To' instruction.
3498 /// Users should provide two positions they are interested in, and the class
3499 /// determines (and caches) reachability.
3501 Attributor &A, const Instruction &From, const Instruction &To,
3502 const AA::InstExclusionSetTy *ExclusionSet = nullptr) const = 0;
3503
3504 /// Create an abstract attribute view for the position \p IRP.
3506 Attributor &A);
3507
3508 /// See AbstractAttribute::getName()
3509 const std::string getName() const override { return "AAIntraFnReachability"; }
3510
3511 /// See AbstractAttribute::getIdAddr()
3512 const char *getIdAddr() const override { return &ID; }
3513
3514 /// This function should return true if the type of the \p AA is
3515 /// AAIntraFnReachability
3516 static bool classof(const AbstractAttribute *AA) {
3517 return (AA->getIdAddr() == &ID);
3518 }
3519
3520 /// Unique ID (due to the unique address)
3521 static const char ID;
3522};
3523
3524/// An abstract interface for all noalias attributes.
3526 : public IRAttribute<Attribute::NoAlias,
3527 StateWrapper<BooleanState, AbstractAttribute>> {
3529
3530 /// Return true if we assume that the underlying value is alias.
3531 bool isAssumedNoAlias() const { return getAssumed(); }
3532
3533 /// Return true if we know that underlying value is noalias.
3534 bool isKnownNoAlias() const { return getKnown(); }
3535
3536 /// Create an abstract attribute view for the position \p IRP.
3538
3539 /// See AbstractAttribute::getName()
3540 const std::string getName() const override { return "AANoAlias"; }
3541
3542 /// See AbstractAttribute::getIdAddr()
3543 const char *getIdAddr() const override { return &ID; }
3544
3545 /// This function should return true if the type of the \p AA is AANoAlias
3546 static bool classof(const AbstractAttribute *AA) {
3547 return (AA->getIdAddr() == &ID);
3548 }
3549
3550 /// Unique ID (due to the unique address)
3551 static const char ID;
3552};
3553
3554/// An AbstractAttribute for nofree.
3556 : public IRAttribute<Attribute::NoFree,
3557 StateWrapper<BooleanState, AbstractAttribute>> {
3559
3560 /// Return true if "nofree" is assumed.
3561 bool isAssumedNoFree() const { return getAssumed(); }
3562
3563 /// Return true if "nofree" is known.
3564 bool isKnownNoFree() const { return getKnown(); }
3565
3566 /// Create an abstract attribute view for the position \p IRP.
3568
3569 /// See AbstractAttribute::getName()
3570 const std::string getName() const override { return "AANoFree"; }
3571
3572 /// See AbstractAttribute::getIdAddr()
3573 const char *getIdAddr() const override { return &ID; }
3574
3575 /// This function should return true if the type of the \p AA is AANoFree
3576 static bool classof(const AbstractAttribute *AA) {
3577 return (AA->getIdAddr() == &ID);
3578 }
3579
3580 /// Unique ID (due to the unique address)
3581 static const char ID;
3582};
3583
3584/// An AbstractAttribute for noreturn.
3586 : public IRAttribute<Attribute::NoReturn,
3587 StateWrapper<BooleanState, AbstractAttribute>> {
3589
3590 /// Return true if the underlying object is assumed to never return.
3591 bool isAssumedNoReturn() const { return getAssumed(); }
3592
3593 /// Return true if the underlying object is known to never return.
3594 bool isKnownNoReturn() const { return getKnown(); }
3595
3596 /// Create an abstract attribute view for the position \p IRP.
3598
3599 /// See AbstractAttribute::getName()
3600 const std::string getName() const override { return "AANoReturn"; }
3601
3602 /// See AbstractAttribute::getIdAddr()
3603 const char *getIdAddr() const override { return &ID; }
3604
3605 /// This function should return true if the type of the \p AA is AANoReturn
3606 static bool classof(const AbstractAttribute *AA) {
3607 return (AA->getIdAddr() == &ID);
3608 }
3609
3610 /// Unique ID (due to the unique address)
3611 static const char ID;
3612};
3613
3614/// An abstract interface for liveness abstract attribute.
3616 : public StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute> {
3618 AAIsDead(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3619
3620 /// State encoding bits. A set bit in the state means the property holds.
3621 enum {
3624
3626 };
3627 static_assert(IS_DEAD == getBestState(), "Unexpected BEST_STATE value");
3628
3629protected:
3630 /// The query functions are protected such that other attributes need to go
3631 /// through the Attributor interfaces: `Attributor::isAssumedDead(...)`
3632
3633 /// Returns true if the underlying value is assumed dead.
3634 virtual bool isAssumedDead() const = 0;
3635
3636 /// Returns true if the underlying value is known dead.
3637 virtual bool isKnownDead() const = 0;
3638
3639 /// Returns true if \p BB is known dead.
3640 virtual bool isKnownDead(const BasicBlock *BB) const = 0;
3641
3642 /// Returns true if \p I is assumed dead.
3643 virtual bool isAssumedDead(const Instruction *I) const = 0;
3644
3645 /// Returns true if \p I is known dead.
3646 virtual bool isKnownDead(const Instruction *I) const = 0;
3647
3648 /// Return true if the underlying value is a store that is known to be
3649 /// removable. This is different from dead stores as the removable store
3650 /// can have an effect on live values, especially loads, but that effect
3651 /// is propagated which allows us to remove the store in turn.
3652 virtual bool isRemovableStore() const { return false; }
3653
3654 /// This method is used to check if at least one instruction in a collection
3655 /// of instructions is live.
3656 template <typename T> bool isLiveInstSet(T begin, T end) const {
3657 for (const auto &I : llvm::make_range(begin, end)) {
3658 assert(I->getFunction() == getIRPosition().getAssociatedFunction() &&
3659 "Instruction must be in the same anchor scope function.");
3660
3661 if (!isAssumedDead(I))
3662 return true;
3663 }
3664
3665 return false;
3666 }
3667
3668public:
3669 /// Create an abstract attribute view for the position \p IRP.
3671
3672 /// Determine if \p F might catch asynchronous exceptions.
3674 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
3675 }
3676
3677 /// Returns true if \p BB is assumed dead.
3678 virtual bool isAssumedDead(const BasicBlock *BB) const = 0;
3679
3680 /// Return if the edge from \p From BB to \p To BB is assumed dead.
3681 /// This is specifically useful in AAReachability.
3682 virtual bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const {
3683 return false;
3684 }
3685
3686 /// See AbstractAttribute::getName()
3687 const std::string getName() const override { return "AAIsDead"; }
3688
3689 /// See AbstractAttribute::getIdAddr()
3690 const char *getIdAddr() const override { return &ID; }
3691
3692 /// This function should return true if the type of the \p AA is AAIsDead
3693 static bool classof(const AbstractAttribute *AA) {
3694 return (AA->getIdAddr() == &ID);
3695 }
3696
3697 /// Unique ID (due to the unique address)
3698 static const char ID;
3699
3700 friend struct Attributor;
3701};
3702
3703/// State for dereferenceable attribute
3705
3706 static DerefState getBestState() { return DerefState(); }
3707 static DerefState getBestState(const DerefState &) { return getBestState(); }
3708
3709 /// Return the worst possible representable state.
3711 DerefState DS;
3712 DS.indicatePessimisticFixpoint();
3713 return DS;
3714 }
3716 return getWorstState();
3717 }
3718
3719 /// State representing for dereferenceable bytes.
3721
3722 /// Map representing for accessed memory offsets and sizes.
3723 /// A key is Offset and a value is size.
3724 /// If there is a load/store instruction something like,
3725 /// p[offset] = v;
3726 /// (offset, sizeof(v)) will be inserted to this map.
3727 /// std::map is used because we want to iterate keys in ascending order.
3728 std::map<int64_t, uint64_t> AccessedBytesMap;
3729
3730 /// Helper function to calculate dereferenceable bytes from current known
3731 /// bytes and accessed bytes.
3732 ///
3733 /// int f(int *A){
3734 /// *A = 0;
3735 /// *(A+2) = 2;
3736 /// *(A+1) = 1;
3737 /// *(A+10) = 10;
3738 /// }
3739 /// ```
3740 /// In that case, AccessedBytesMap is `{0:4, 4:4, 8:4, 40:4}`.
3741 /// AccessedBytesMap is std::map so it is iterated in accending order on
3742 /// key(Offset). So KnownBytes will be updated like this:
3743 ///
3744 /// |Access | KnownBytes
3745 /// |(0, 4)| 0 -> 4
3746 /// |(4, 4)| 4 -> 8
3747 /// |(8, 4)| 8 -> 12
3748 /// |(40, 4) | 12 (break)
3749 void computeKnownDerefBytesFromAccessedMap() {
3750 int64_t KnownBytes = DerefBytesState.getKnown();
3751 for (auto &Access : AccessedBytesMap) {
3752 if (KnownBytes < Access.first)
3753 break;
3754 KnownBytes = std::max(KnownBytes, Access.first + (int64_t)Access.second);
3755 }
3756
3758 }
3759
3760 /// State representing that whether the value is globaly dereferenceable.
3761 BooleanState GlobalState;
3762
3763 /// See AbstractState::isValidState()
3764 bool isValidState() const override { return DerefBytesState.isValidState(); }
3765
3766 /// See AbstractState::isAtFixpoint()
3767 bool isAtFixpoint() const override {
3768 return !isValidState() ||
3769 (DerefBytesState.isAtFixpoint() && GlobalState.isAtFixpoint());
3770 }
3771
3772 /// See AbstractState::indicateOptimisticFixpoint(...)
3775 GlobalState.indicateOptimisticFixpoint();
3777 }
3778
3779 /// See AbstractState::indicatePessimisticFixpoint(...)
3782 GlobalState.indicatePessimisticFixpoint();
3783 return ChangeStatus::CHANGED;
3784 }
3785
3786 /// Update known dereferenceable bytes.
3787 void takeKnownDerefBytesMaximum(uint64_t Bytes) {
3789
3790 // Known bytes might increase.
3791 computeKnownDerefBytesFromAccessedMap();
3792 }
3793
3794 /// Update assumed dereferenceable bytes.
3795 void takeAssumedDerefBytesMinimum(uint64_t Bytes) {
3797 }
3798
3799 /// Add accessed bytes to the map.
3800 void addAccessedBytes(int64_t Offset, uint64_t Size) {
3801 uint64_t &AccessedBytes = AccessedBytesMap[Offset];
3802 AccessedBytes = std::max(AccessedBytes, Size);
3803
3804 // Known bytes might increase.
3805 computeKnownDerefBytesFromAccessedMap();
3806 }
3807
3808 /// Equality for DerefState.
3809 bool operator==(const DerefState &R) const {
3810 return this->DerefBytesState == R.DerefBytesState &&
3811 this->GlobalState == R.GlobalState;
3812 }
3813
3814 /// Inequality for DerefState.
3815 bool operator!=(const DerefState &R) const { return !(*this == R); }
3816
3817 /// See IntegerStateBase::operator^=
3818 DerefState operator^=(const DerefState &R) {
3819 DerefBytesState ^= R.DerefBytesState;
3820 GlobalState ^= R.GlobalState;
3821 return *this;
3822 }
3823
3824 /// See IntegerStateBase::operator+=
3825 DerefState operator+=(const DerefState &R) {
3826 DerefBytesState += R.DerefBytesState;
3827 GlobalState += R.GlobalState;
3828 return *this;
3829 }
3830
3831 /// See IntegerStateBase::operator&=
3832 DerefState operator&=(const DerefState &R) {
3833 DerefBytesState &= R.DerefBytesState;
3834 GlobalState &= R.GlobalState;
3835 return *this;
3836 }
3837
3838 /// See IntegerStateBase::operator|=
3839 DerefState operator|=(const DerefState &R) {
3840 DerefBytesState |= R.DerefBytesState;
3841 GlobalState |= R.GlobalState;
3842 return *this;
3843 }
3844
3845protected:
3846 const AANonNull *NonNullAA = nullptr;
3847};
3848
3849/// An abstract interface for all dereferenceable attribute.
3851 : public IRAttribute<Attribute::Dereferenceable,
3852 StateWrapper<DerefState, AbstractAttribute>> {
3854
3855 /// Return true if we assume that the underlying value is nonnull.
3856 bool isAssumedNonNull() const {
3857 return NonNullAA && NonNullAA->isAssumedNonNull();
3858 }
3859
3860 /// Return true if we know that the underlying value is nonnull.
3861 bool isKnownNonNull() const {
3862 return NonNullAA && NonNullAA->isKnownNonNull();
3863 }
3864
3865 /// Return true if we assume that underlying value is
3866 /// dereferenceable(_or_null) globally.
3867 bool isAssumedGlobal() const { return GlobalState.getAssumed(); }
3868
3869 /// Return true if we know that underlying value is
3870 /// dereferenceable(_or_null) globally.
3871 bool isKnownGlobal() const { return GlobalState.getKnown(); }
3872
3873 /// Return assumed dereferenceable bytes.
3875 return DerefBytesState.getAssumed();
3876 }
3877
3878 /// Return known dereferenceable bytes.
3880 return DerefBytesState.getKnown();
3881 }
3882
3883 /// Create an abstract attribute view for the position \p IRP.
3885 Attributor &A);
3886
3887 /// See AbstractAttribute::getName()
3888 const std::string getName() const override { return "AADereferenceable"; }
3889
3890 /// See AbstractAttribute::getIdAddr()
3891 const char *getIdAddr() const override { return &ID; }
3892
3893 /// This function should return true if the type of the \p AA is
3894 /// AADereferenceable
3895 static bool classof(const AbstractAttribute *AA) {
3896 return (AA->getIdAddr() == &ID);
3897 }
3898
3899 /// Unique ID (due to the unique address)
3900 static const char ID;
3901};
3902
3905/// An abstract interface for all align attributes.
3906struct AAAlign : public IRAttribute<
3907 Attribute::Alignment,
3908 StateWrapper<AAAlignmentStateType, AbstractAttribute>> {
3910
3911 /// Return assumed alignment.
3912 Align getAssumedAlign() const { return Align(getAssumed()); }
3913
3914 /// Return known alignment.
3915 Align getKnownAlign() const { return Align(getKnown()); }
3916
3917 /// See AbstractAttribute::getName()
3918 const std::string getName() const override { return "AAAlign"; }
3919
3920 /// See AbstractAttribute::getIdAddr()
3921 const char *getIdAddr() const override { return &ID; }
3922
3923 /// This function should return true if the type of the \p AA is AAAlign
3924 static bool classof(const AbstractAttribute *AA) {
3925 return (AA->getIdAddr() == &ID);
3926 }
3927
3928 /// Create an abstract attribute view for the position \p IRP.
3930
3931 /// Unique ID (due to the unique address)
3932 static const char ID;
3933};
3934
3935/// An abstract interface to track if a value leaves it's defining function
3936/// instance.
3937/// TODO: We should make it a ternary AA tracking uniqueness, and uniqueness
3938/// wrt. the Attributor analysis separately.
3939struct AAInstanceInfo : public StateWrapper<BooleanState, AbstractAttribute> {
3942
3943 /// Return true if we know that the underlying value is unique in its scope
3944 /// wrt. the Attributor analysis. That means it might not be unique but we can
3945 /// still use pointer equality without risking to represent two instances with
3946 /// one `llvm::Value`.
3947 bool isKnownUniqueForAnalysis() const { return isKnown(); }
3948
3949 /// Return true if we assume that the underlying value is unique in its scope
3950 /// wrt. the Attributor analysis. That means it might not be unique but we can
3951 /// still use pointer equality without risking to represent two instances with
3952 /// one `llvm::Value`.
3953 bool isAssumedUniqueForAnalysis() const { return isAssumed(); }
3954
3955 /// Create an abstract attribute view for the position \p IRP.
3957 Attributor &A);
3958
3959 /// See AbstractAttribute::getName()
3960 const std::string getName() const override { return "AAInstanceInfo"; }
3961
3962 /// See AbstractAttribute::getIdAddr()
3963 const char *getIdAddr() const override { return &ID; }
3964
3965 /// This function should return true if the type of the \p AA is
3966 /// AAInstanceInfo
3967 static bool classof(const AbstractAttribute *AA) {
3968 return (AA->getIdAddr() == &ID);
3969 }
3970
3971 /// Unique ID (due to the unique address)
3972 static const char ID;
3973};
3974
3975/// An abstract interface for all nocapture attributes.
3977 : public IRAttribute<
3978 Attribute::NoCapture,
3979 StateWrapper<BitIntegerState<uint16_t, 7, 0>, AbstractAttribute>> {
3981
3982 /// State encoding bits. A set bit in the state means the property holds.
3983 /// NO_CAPTURE is the best possible state, 0 the worst possible state.
3984 enum {
3988
3989 /// If we do not capture the value in memory or through integers we can only
3990 /// communicate it back as a derived pointer.
3992
3993 /// If we do not capture the value in memory, through integers, or as a
3994 /// derived pointer we know it is not captured.
3995 NO_CAPTURE =
3997 };
3998
3999 /// Return true if we know that the underlying value is not captured in its
4000 /// respective scope.
4001 bool isKnownNoCapture() const { return isKnown(NO_CAPTURE); }
4002
4003 /// Return true if we assume that the underlying value is not captured in its
4004 /// respective scope.
4005 bool isAssumedNoCapture() const { return isAssumed(NO_CAPTURE); }
4006
4007 /// Return true if we know that the underlying value is not captured in its
4008 /// respective scope but we allow it to escape through a "return".
4011 }
4012
4013 /// Return true if we assume that the underlying value is not captured in its
4014 /// respective scope but we allow it to escape through a "return".
4017 }
4018
4019 /// Create an abstract attribute view for the position \p IRP.
4021
4022 /// See AbstractAttribute::getName()
4023 const std::string getName() const override { return "AANoCapture"; }
4024
4025 /// See AbstractAttribute::getIdAddr()
4026 const char *getIdAddr() const override { return &ID; }
4027
4028 /// This function should return true if the type of the \p AA is AANoCapture
4029 static bool classof(const AbstractAttribute *AA) {
4030 return (AA->getIdAddr() == &ID);
4031 }
4032
4033 /// Unique ID (due to the unique address)
4034 static const char ID;
4035};
4036
4038
4040
4042 return ValueSimplifyStateType(Ty);
4043 }
4045 return getBestState(VS.Ty);
4046 }
4047
4048 /// Return the worst possible representable state.
4051 DS.indicatePessimisticFixpoint();
4052 return DS;
4053 }
4056 return getWorstState(VS.Ty);
4057 }
4058
4059 /// See AbstractState::isValidState(...)
4060 bool isValidState() const override { return BS.isValidState(); }
4061
4062 /// See AbstractState::isAtFixpoint(...)
4063 bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
4064
4065 /// Return the assumed state encoding.
4067 const ValueSimplifyStateType &getAssumed() const { return *this; }
4068
4069 /// See AbstractState::indicatePessimisticFixpoint(...)
4072 }
4073
4074 /// See AbstractState::indicateOptimisticFixpoint(...)
4077 }
4078
4079 /// "Clamp" this state with \p PVS.
4081 BS ^= VS.BS;
4082 unionAssumed(VS.SimplifiedAssociatedValue);
4083 return *this;
4084 }
4085
4087 if (isValidState() != RHS.isValidState())
4088 return false;
4089 if (!isValidState() && !RHS.isValidState())
4090 return true;
4091 return SimplifiedAssociatedValue == RHS.SimplifiedAssociatedValue;
4092 }
4093
4094protected:
4095 /// The type of the original value.
4097
4098 /// Merge \p Other into the currently assumed simplified value
4099 bool unionAssumed(std::optional<Value *> Other);
4100
4101 /// Helper to track validity and fixpoint
4103
4104 /// An assumed simplified value. Initially, it is set to std::nullopt, which
4105 /// means that the value is not clear under current assumption. If in the
4106 /// pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4107 /// returns orignal associated value.
4108 std::optional<Value *> SimplifiedAssociatedValue;
4109};
4110
4111/// An abstract interface for value simplify abstract attribute.
4113 : public StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *> {
4116 : Base(IRP, IRP.getAssociatedType()) {}
4117
4118 /// Create an abstract attribute view for the position \p IRP.
4120 Attributor &A);
4121
4122 /// See AbstractAttribute::getName()
4123 const std::string getName() const override { return "AAValueSimplify"; }
4124
4125 /// See AbstractAttribute::getIdAddr()
4126 const char *getIdAddr() const override { return &ID; }
4127
4128 /// This function should return true if the type of the \p AA is
4129 /// AAValueSimplify
4130 static bool classof(const AbstractAttribute *AA) {
4131 return (AA->getIdAddr() == &ID);
4132 }
4133
4134 /// Unique ID (due to the unique address)
4135 static const char ID;
4136
4137private:
4138 /// Return an assumed simplified value if a single candidate is found. If
4139 /// there cannot be one, return original value. If it is not clear yet, return
4140 /// std::nullopt.
4141 ///
4142 /// Use `Attributor::getAssumedSimplified` for value simplification.
4143 virtual std::optional<Value *>
4144 getAssumedSimplifiedValue(Attributor &A) const = 0;
4145
4146 friend struct Attributor;
4147};
4148
4149struct AAHeapToStack : public StateWrapper<BooleanState, AbstractAttribute> {
4151 AAHeapToStack(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4152
4153 /// Returns true if HeapToStack conversion is assumed to be possible.
4154 virtual bool isAssumedHeapToStack(const CallBase &CB) const = 0;
4155
4156 /// Returns true if HeapToStack conversion is assumed and the CB is a
4157 /// callsite to a free operation to be removed.
4158 virtual bool isAssumedHeapToStackRemovedFree(CallBase &CB) const = 0;
4159
4160 /// Create an abstract attribute view for the position \p IRP.
4162
4163 /// See AbstractAttribute::getName()
4164 const std::string getName() const override { return "AAHeapToStack"; }
4165
4166 /// See AbstractAttribute::getIdAddr()
4167 const char *getIdAddr() const override { return &ID; }
4168
4169 /// This function should return true if the type of the \p AA is AAHeapToStack
4170 static bool classof(const AbstractAttribute *AA) {
4171 return (AA->getIdAddr() == &ID);
4172 }
4173
4174 /// Unique ID (due to the unique address)
4175 static const char ID;
4176};
4177
4178/// An abstract interface for privatizability.
4179///
4180/// A pointer is privatizable if it can be replaced by a new, private one.
4181/// Privatizing pointer reduces the use count, interaction between unrelated
4182/// code parts.
4183///
4184/// In order for a pointer to be privatizable its value cannot be observed
4185/// (=nocapture), it is (for now) not written (=readonly & noalias), we know
4186/// what values are necessary to make the private copy look like the original
4187/// one, and the values we need can be loaded (=dereferenceable).
4189 : public StateWrapper<BooleanState, AbstractAttribute> {
4192
4193 /// Returns true if pointer privatization is assumed to be possible.
4194 bool isAssumedPrivatizablePtr() const { return getAssumed(); }
4195
4196 /// Returns true if pointer privatization is known to be possible.
4197 bool isKnownPrivatizablePtr() const { return getKnown(); }
4198
4199 /// Return the type we can choose for a private copy of the underlying
4200 /// value. std::nullopt means it is not clear yet, nullptr means there is
4201 /// none.
4202 virtual std::optional<Type *> getPrivatizableType() const = 0;
4203
4204 /// Create an abstract attribute view for the position \p IRP.
4206 Attributor &A);
4207
4208 /// See AbstractAttribute::getName()
4209 const std::string getName() const override { return "AAPrivatizablePtr"; }
4210
4211 /// See AbstractAttribute::getIdAddr()
4212 const char *getIdAddr() const override { return &ID; }
4213
4214 /// This function should return true if the type of the \p AA is
4215 /// AAPricatizablePtr
4216 static bool classof(const AbstractAttribute *AA) {
4217 return (AA->getIdAddr() == &ID);
4218 }
4219
4220 /// Unique ID (due to the unique address)
4221 static const char ID;
4222};
4223
4224/// An abstract interface for memory access kind related attributes
4225/// (readnone/readonly/writeonly).
4227 : public IRAttribute<
4228 Attribute::ReadNone,
4229 StateWrapper<BitIntegerState<uint8_t, 3>, AbstractAttribute>> {
4231
4232 /// State encoding bits. A set bit in the state means the property holds.
4233 /// BEST_STATE is the best possible state, 0 the worst possible state.
4234 enum {
4235 NO_READS = 1 << 0,
4236 NO_WRITES = 1 << 1,
4238
4240 };
4241 static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
4242
4243 /// Return true if we know that the underlying value is not read or accessed
4244 /// in its respective scope.
4245 bool isKnownReadNone() const { return isKnown(NO_ACCESSES); }
4246
4247 /// Return true if we assume that the underlying value is not read or accessed
4248 /// in its respective scope.
4249 bool isAssumedReadNone() const { return isAssumed(NO_ACCESSES); }
4250
4251 /// Return true if we know that the underlying value is not accessed
4252 /// (=written) in its respective scope.
4253 bool isKnownReadOnly() const { return isKnown(NO_WRITES); }
4254
4255 /// Return true if we assume that the underlying value is not accessed
4256 /// (=written) in its respective scope.
4257 bool isAssumedReadOnly() const { return isAssumed(NO_WRITES); }
4258
4259 /// Return true if we know that the underlying value is not read in its
4260 /// respective scope.
4261 bool isKnownWriteOnly() const { return isKnown(NO_READS); }
4262
4263 /// Return true if we assume that the underlying value is not read in its
4264 /// respective scope.
4265 bool isAssumedWriteOnly() const { return isAssumed(NO_READS); }
4266
4267 /// Create an abstract attribute view for the position \p IRP.
4269 Attributor &A);
4270
4271 /// See AbstractAttribute::getName()
4272 const std::string getName() const override { return "AAMemoryBehavior"; }
4273
4274 /// See AbstractAttribute::getIdAddr()
4275 const char *getIdAddr() const override { return &ID; }
4276
4277 /// This function should return true if the type of the \p AA is
4278 /// AAMemoryBehavior
4279 static bool classof(const AbstractAttribute *AA) {
4280 return (AA->getIdAddr() == &ID);
4281 }
4282
4283 /// Unique ID (due to the unique address)
4284 static const char ID;
4285};
4286
4287/// An abstract interface for all memory location attributes
4288/// (readnone/argmemonly/inaccessiblememonly/inaccessibleorargmemonly).
4290 : public IRAttribute<
4291 Attribute::ReadNone,
4292 StateWrapper<BitIntegerState<uint32_t, 511>, AbstractAttribute>> {
4294
4296
4297 /// Encoding of different locations that could be accessed by a memory
4298 /// access.
4299 enum {
4313
4314 // Helper bit to track if we gave up or not.
4316
4318 };
4319 static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
4320
4321 /// Return true if we know that the associated functions has no observable
4322 /// accesses.
4323 bool isKnownReadNone() const { return isKnown(NO_LOCATIONS); }
4324
4325 /// Return true if we assume that the associated functions has no observable
4326 /// accesses.
4327 bool isAssumedReadNone() const {
4329 }
4330
4331 /// Return true if we know that the associated functions has at most
4332 /// local/stack accesses.
4333 bool isKnowStackOnly() const {
4334 return isKnown(inverseLocation(NO_LOCAL_MEM, true, true));
4335 }
4336
4337 /// Return true if we assume that the associated functions has at most
4338 /// local/stack accesses.
4339 bool isAssumedStackOnly() const {
4340 return isAssumed(inverseLocation(NO_LOCAL_MEM, true, true));
4341 }
4342
4343 /// Return true if we know that the underlying value will only access
4344 /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
4346 return isKnown(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
4347 }
4348
4349 /// Return true if we assume that the underlying value will only access
4350 /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
4352 return isAssumed(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
4353 }
4354
4355 /// Return true if we know that the underlying value will only access
4356 /// argument pointees (see Attribute::ArgMemOnly).
4357 bool isKnownArgMemOnly() const {
4358 return isKnown(inverseLocation(NO_ARGUMENT_MEM, true, true));
4359 }
4360
4361 /// Return true if we assume that the underlying value will only access
4362 /// argument pointees (see Attribute::ArgMemOnly).
4363 bool isAssumedArgMemOnly() const {
4364 return isAssumed(inverseLocation(NO_ARGUMENT_MEM, true, true));
4365 }
4366
4367 /// Return true if we know that the underlying value will only access
4368 /// inaccesible memory or argument pointees (see
4369 /// Attribute::InaccessibleOrArgMemOnly).
4371 return isKnown(