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