LLVM 24.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/IntervalMap.h"
103#include "llvm/ADT/MapVector.h"
104#include "llvm/ADT/STLExtras.h"
106#include "llvm/ADT/SetVector.h"
107#include "llvm/ADT/SmallSet.h"
108#include "llvm/ADT/iterator.h"
110#include "llvm/Analysis/CFG.h"
120#include "llvm/IR/Attributes.h"
122#include "llvm/IR/Constants.h"
123#include "llvm/IR/GlobalValue.h"
124#include "llvm/IR/InstIterator.h"
125#include "llvm/IR/Instruction.h"
126#include "llvm/IR/Instructions.h"
127#include "llvm/IR/Module.h"
128#include "llvm/IR/PassManager.h"
129#include "llvm/IR/Value.h"
132#include "llvm/Support/Casting.h"
136#include "llvm/Support/ModRef.h"
141
142#include <limits>
143#include <map>
144#include <optional>
145
146namespace llvm {
147
148class DataLayout;
149class LLVMContext;
150class Pass;
151template <typename Fn> class function_ref;
152struct AADepGraphNode;
153struct AADepGraph;
154struct Attributor;
155struct AbstractAttribute;
156struct InformationCache;
157struct AAIsDead;
159struct IRPosition;
160
161class Function;
162
163/// Abstract Attribute helper functions.
164namespace AA {
166
167/// Return true iff \p M target a GPU (and we can use GPU AS reasoning).
168LLVM_ABI bool isGPU(const Module &M);
169
170/// Check if the given address space \p AS corresponds to a GPU shared
171/// address space for the target triple in module \p M.
172LLVM_ABI bool isGPUSharedAddressSpace(const Module &M, unsigned AS);
173
174/// Check if the given address space \p AS corresponds to a GPU constant
175/// address space for the target triple in module \p M.
176LLVM_ABI bool isGPUConstantAddressSpace(const Module &M, unsigned AS);
177
178/// Check if the given address space \p AS corresponds to a GPU local/private
179/// address space for the target triple in module \p M.
180LLVM_ABI bool isGPULocalAddressSpace(const Module &M, unsigned AS);
181
182/// Flags to distinguish intra-procedural queries from *potentially*
183/// inter-procedural queries. Not that information can be valid for both and
184/// therefore both bits might be set.
190
191struct ValueAndContext : public std::pair<Value *, const Instruction *> {
192 using Base = std::pair<Value *, const Instruction *>;
194 ValueAndContext(Value &V, const Instruction *CtxI) : Base(&V, CtxI) {}
195 ValueAndContext(Value &V, const Instruction &CtxI) : Base(&V, &CtxI) {}
196
197 Value *getValue() const { return this->first; }
198 const Instruction *getCtxI() const { return this->second; }
199};
200
201/// Return true if \p I is a `nosync` instruction. Use generic reasoning and
202/// potentially the corresponding AANoSync.
204 const AbstractAttribute &QueryingAA);
205
206/// Return true if \p V is dynamically unique, that is, there are no two
207/// "instances" of \p V at runtime with different values.
208/// Note: If \p ForAnalysisOnly is set we only check that the Attributor will
209/// never use \p V to represent two "instances" not that \p V could not
210/// technically represent them.
212 const AbstractAttribute &QueryingAA,
213 const Value &V, bool ForAnalysisOnly = true);
214
215/// Return true if \p V is a valid value in \p Scope, that is a constant or an
216/// instruction/argument of \p Scope.
217LLVM_ABI bool isValidInScope(const Value &V, const Function *Scope);
218
219/// Return true if the value of \p VAC is a valid at the position of \p VAC,
220/// that is a constant, an argument of the same function, or an instruction in
221/// that function that dominates the position.
222LLVM_ABI bool isValidAtPosition(const ValueAndContext &VAC,
223 InformationCache &InfoCache);
224
225/// Try to convert \p V to type \p Ty without introducing new instructions. If
226/// this is not possible return `nullptr`. Note: this function basically knows
227/// how to cast various constants.
229
230/// Return the combination of \p A and \p B such that the result is a possible
231/// value of both. \p B is potentially casted to match the type \p Ty or the
232/// type of \p A if \p Ty is null.
233///
234/// Examples:
235/// X + none => X
236/// not_none + undef => not_none
237/// V1 + V2 => nullptr
238LLVM_ABI std::optional<Value *>
239combineOptionalValuesInAAValueLatice(const std::optional<Value *> &A,
240 const std::optional<Value *> &B, Type *Ty);
241
242/// Helper to represent an access offset and size, with logic to deal with
243/// uncertainty and check for overlapping accesses.
244struct RangeTy {
246 int64_t Size = Unassigned;
247
248 RangeTy(int64_t Offset, int64_t Size) : Offset(Offset), Size(Size) {}
249 RangeTy() = default;
250 static RangeTy getUnknown() { return RangeTy{Unknown, Unknown}; }
251
252 /// Return true if offset or size are unknown.
255 }
256
257 /// Return true if offset and size are unknown, thus this is the default
258 /// unknown object.
261 }
262
263 /// Return true if the offset and size are unassigned.
264 bool isUnassigned() const {
266 "Inconsistent state!");
267 return Offset == RangeTy::Unassigned;
268 }
269
270 /// Return true if this offset and size pair might describe an address that
271 /// overlaps with \p Range.
272 bool mayOverlap(const RangeTy &Range) const {
273 // Any unknown value and we are giving up -> overlap.
274 if (offsetOrSizeAreUnknown() || Range.offsetOrSizeAreUnknown())
275 return true;
276
277 // Check if one offset point is in the other interval [offset,
278 // offset+size].
279 return Range.Offset + Range.Size > Offset && Range.Offset < Offset + Size;
280 }
281
283 if (R.isUnassigned())
284 return *this;
285 if (isUnassigned())
286 return *this = R;
287 if (Offset == Unknown || R.Offset == Unknown)
288 Offset = Unknown;
289 if (Size == Unknown || R.Size == Unknown)
290 Size = Unknown;
292 return *this;
293 if (Offset == Unknown) {
294 Size = std::max(Size, R.Size);
295 } else if (Size == Unknown) {
296 Offset = std::min(Offset, R.Offset);
297 } else {
298 Offset = std::min(Offset, R.Offset);
299 Size = std::max(Offset + Size, R.Offset + R.Size) - Offset;
300 }
301 return *this;
302 }
303
304 /// Comparison for sorting ranges.
305 ///
306 /// Returns true if the offset of \p L is less than that of \p R. If the two
307 /// offsets are same, compare the sizes instead.
308 inline static bool LessThan(const RangeTy &L, const RangeTy &R) {
309 if (L.Offset < R.Offset)
310 return true;
311 if (L.Offset == R.Offset)
312 return L.Size < R.Size;
313 return false;
314 }
315
316 /// Constants used to represent special offsets or sizes.
317 /// - We cannot assume that Offsets and Size are non-negative.
318 /// - The constants should not clash with DenseMapInfo, such as EmptyKey
319 /// (INT64_MAX).
320 /// We use values "in the middle" of the 64 bit range to represent these
321 /// special cases.
322 static constexpr int64_t Unassigned = std::numeric_limits<int32_t>::min();
323 static constexpr int64_t Unknown = std::numeric_limits<int32_t>::max();
324};
325
327 OS << "[" << R.Offset << ", " << R.Size << "]";
328 return OS;
329}
330
331inline bool operator==(const RangeTy &A, const RangeTy &B) {
332 return A.Offset == B.Offset && A.Size == B.Size;
333}
334
335inline bool operator!=(const RangeTy &A, const RangeTy &B) { return !(A == B); }
336
337/// Return the initial value of \p Obj with type \p Ty if that is a constant.
340 Value &Obj, Type &Ty, const TargetLibraryInfo *TLI,
341 const DataLayout &DL, RangeTy *RangePtr = nullptr);
342
343/// Collect all potential values \p LI could read into \p PotentialValues. That
344/// is, the only values read by \p LI are assumed to be known and all are in
345/// \p PotentialValues. \p PotentialValueOrigins will contain all the
346/// instructions that might have put a potential value into \p PotentialValues.
347/// Dependences onto \p QueryingAA are properly tracked, \p
348/// UsedAssumedInformation will inform the caller if assumed information was
349/// used.
350///
351/// \returns True if the assumed potential copies are all in \p PotentialValues,
352/// false if something went wrong and the copies could not be
353/// determined.
355 Attributor &A, LoadInst &LI, SmallSetVector<Value *, 4> &PotentialValues,
356 SmallSetVector<Instruction *, 4> &PotentialValueOrigins,
357 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
358 bool OnlyExact = false);
359
360/// Collect all potential values of the one stored by \p SI into
361/// \p PotentialCopies. That is, the only copies that were made via the
362/// store are assumed to be known and all are in \p PotentialCopies. Dependences
363/// onto \p QueryingAA are properly tracked, \p UsedAssumedInformation will
364/// inform the caller if assumed information was used.
365///
366/// \returns True if the assumed potential copies are all in \p PotentialCopies,
367/// false if something went wrong and the copies could not be
368/// determined.
371 const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation,
372 bool OnlyExact = false);
373
374/// Return true if \p IRP is readonly. This will query respective AAs that
375/// deduce the information and introduce dependences for \p QueryingAA.
377 const AbstractAttribute &QueryingAA,
378 bool &IsKnown);
379
380/// Return true if \p IRP is readnone. This will query respective AAs that
381/// deduce the information and introduce dependences for \p QueryingAA.
383 const AbstractAttribute &QueryingAA,
384 bool &IsKnown);
385
386/// Return true if \p ToI is potentially reachable from \p FromI without running
387/// into any instruction in \p ExclusionSet The two instructions do not need to
388/// be in the same function. \p GoBackwardsCB can be provided to convey domain
389/// knowledge about the "lifespan" the user is interested in. By default, the
390/// callers of \p FromI are checked as well to determine if \p ToI can be
391/// reached. If the query is not interested in callers beyond a certain point,
392/// e.g., a GPU kernel entry or the function containing an alloca, the
393/// \p GoBackwardsCB should return false.
395 Attributor &A, const Instruction &FromI, const Instruction &ToI,
396 const AbstractAttribute &QueryingAA,
397 const AA::InstExclusionSetTy *ExclusionSet = nullptr,
398 std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
399
400/// Same as above but it is sufficient to reach any instruction in \p ToFn.
402 Attributor &A, const Instruction &FromI, const Function &ToFn,
403 const AbstractAttribute &QueryingAA,
404 const AA::InstExclusionSetTy *ExclusionSet = nullptr,
405 std::function<bool(const Function &F)> GoBackwardsCB = nullptr);
406
407/// Return true if \p Obj is assumed to be a thread local object.
409 const AbstractAttribute &QueryingAA);
410
411/// Return true if \p I is potentially affected by a barrier.
412LLVM_ABI bool
414 const AbstractAttribute &QueryingAA);
415LLVM_ABI bool
417 const AbstractAttribute &QueryingAA,
418 const Instruction *CtxI);
419} // namespace AA
420
421template <>
422struct DenseMapInfo<AA::ValueAndContext>
423 : public DenseMapInfo<AA::ValueAndContext::Base> {
425 static unsigned getHashValue(const AA::ValueAndContext &VAC) {
426 return Base::getHashValue(VAC);
427 }
428
429 static bool isEqual(const AA::ValueAndContext &LHS,
430 const AA::ValueAndContext &RHS) {
431 return Base::isEqual(LHS, RHS);
432 }
433};
434
435template <>
436struct DenseMapInfo<AA::ValueScope> : public DenseMapInfo<unsigned char> {
438 static unsigned getHashValue(const AA::ValueScope &S) {
439 return Base::getHashValue(S);
440 }
441
442 static bool isEqual(const AA::ValueScope &LHS, const AA::ValueScope &RHS) {
443 return Base::isEqual(LHS, RHS);
444 }
445};
446
447template <>
448struct DenseMapInfo<const AA::InstExclusionSetTy *>
449 : public DenseMapInfo<void *> {
450 static unsigned getHashValue(const AA::InstExclusionSetTy *BES) {
451 unsigned H = 0;
452 if (BES)
453 for (const auto *II : *BES)
454 H += DenseMapInfo<const Instruction *>::getHashValue(II);
455 return H;
456 }
457 static bool isEqual(const AA::InstExclusionSetTy *LHS,
459 if (LHS == RHS)
460 return true;
461 auto SizeLHS = LHS ? LHS->size() : 0;
462 auto SizeRHS = RHS ? RHS->size() : 0;
463 if (SizeLHS != SizeRHS)
464 return false;
465 if (SizeRHS == 0)
466 return true;
467 return llvm::set_is_subset(*LHS, *RHS);
468 }
469};
470
471/// The value passed to the line option that defines the maximal initialization
472/// chain length.
474
475///{
480
485
486enum class DepClassTy {
487 REQUIRED, ///< The target cannot be valid if the source is not.
488 OPTIONAL, ///< The target may be valid if the source is not.
489 NONE, ///< Do not track a dependence between source and target.
490};
491///}
492
493/// The data structure for the nodes of a dependency graph
495public:
496 virtual ~AADepGraphNode() = default;
499
500protected:
501 /// Set of dependency graph nodes which should be updated if this one
502 /// is updated. The bit encodes if it is optional.
504
505 static AADepGraphNode *DepGetVal(const DepTy &DT) { return DT.getPointer(); }
508 }
509
510 operator AbstractAttribute *() { return cast<AbstractAttribute>(this); }
511
512public:
516
517 aaiterator begin() { return aaiterator(Deps.begin(), &DepGetValAA); }
518 aaiterator end() { return aaiterator(Deps.end(), &DepGetValAA); }
519 iterator child_begin() { return iterator(Deps.begin(), &DepGetVal); }
520 iterator child_end() { return iterator(Deps.end(), &DepGetVal); }
521
522 void print(raw_ostream &OS) const { print(nullptr, OS); }
523 virtual void print(Attributor *, raw_ostream &OS) const {
524 OS << "AADepNode Impl\n";
525 }
526
527 friend struct Attributor;
528 friend struct AADepGraph;
529};
530
531/// The data structure for the dependency graph
532///
533/// Note that in this graph if there is an edge from A to B (A -> B),
534/// then it means that B depends on A, and when the state of A is
535/// updated, node B should also be updated
537 AADepGraph() = default;
538 ~AADepGraph() = default;
539
541 static AADepGraphNode *DepGetVal(const DepTy &DT) { return DT.getPointer(); }
542 using iterator =
544
545 /// There is no root node for the dependency graph. But the SCCIterator
546 /// requires a single entry point, so we maintain a fake("synthetic") root
547 /// node that depends on every node.
550
551 iterator begin() { return SyntheticRoot.child_begin(); }
552 iterator end() { return SyntheticRoot.child_end(); }
553
554 LLVM_ABI void viewGraph();
555
556 /// Dump graph to file
557 LLVM_ABI void dumpGraph();
558
559 /// Print dependency graph
560 LLVM_ABI void print();
561};
562
563/// Helper to describe and deal with positions in the LLVM-IR.
564///
565/// A position in the IR is described by an anchor value and an "offset" that
566/// could be the argument number, for call sites and arguments, or an indicator
567/// of the "position kind". The kinds, specified in the Kind enum below, include
568/// the locations in the attribute list, i.a., function scope and return value,
569/// as well as a distinction between call sites and functions. Finally, there
570/// are floating values that do not have a corresponding attribute list
571/// position.
573 // NOTE: In the future this definition can be changed to support recursive
574 // functions.
576
577 /// The positions we distinguish in the IR.
578 enum Kind : char {
579 IRP_INVALID, ///< An invalid position.
580 IRP_FLOAT, ///< A position that is not associated with a spot suitable
581 ///< for attributes. This could be any value or instruction.
582 IRP_RETURNED, ///< An attribute for the function return value.
583 IRP_CALL_SITE_RETURNED, ///< An attribute for a call site return value.
584 IRP_FUNCTION, ///< An attribute for a function (scope).
585 IRP_CALL_SITE, ///< An attribute for a call site (function scope).
586 IRP_ARGUMENT, ///< An attribute for a function argument.
587 IRP_CALL_SITE_ARGUMENT, ///< An attribute for a call site argument.
588 };
589
590 /// Default constructor available to create invalid positions implicitly. All
591 /// other positions need to be created explicitly through the appropriate
592 /// static member function.
593 IRPosition() : Enc(nullptr, ENC_VALUE) { verify(); }
594
595 /// Create a position describing the value of \p V.
596 static const IRPosition value(const Value &V,
597 const CallBaseContext *CBContext = nullptr) {
598 if (auto *Arg = dyn_cast<Argument>(&V))
599 return IRPosition::argument(*Arg, CBContext);
600 if (auto *CB = dyn_cast<CallBase>(&V))
602 return IRPosition(const_cast<Value &>(V), IRP_FLOAT, CBContext);
603 }
604
605 /// Create a position describing the instruction \p I. This is different from
606 /// the value version because call sites are treated as intrusctions rather
607 /// than their return value in this function.
608 static const IRPosition inst(const Instruction &I,
609 const CallBaseContext *CBContext = nullptr) {
610 return IRPosition(const_cast<Instruction &>(I), IRP_FLOAT, CBContext);
611 }
612
613 /// Create a position describing the function scope of \p F.
614 /// \p CBContext is used for call base specific analysis.
615 static const IRPosition function(const Function &F,
616 const CallBaseContext *CBContext = nullptr) {
617 return IRPosition(const_cast<Function &>(F), IRP_FUNCTION, CBContext);
618 }
619
620 /// Create a position describing the returned value of \p F.
621 /// \p CBContext is used for call base specific analysis.
622 static const IRPosition returned(const Function &F,
623 const CallBaseContext *CBContext = nullptr) {
624 return IRPosition(const_cast<Function &>(F), IRP_RETURNED, CBContext);
625 }
626
627 /// Create a position describing the argument \p Arg.
628 /// \p CBContext is used for call base specific analysis.
629 static const IRPosition argument(const Argument &Arg,
630 const CallBaseContext *CBContext = nullptr) {
631 return IRPosition(const_cast<Argument &>(Arg), IRP_ARGUMENT, CBContext);
632 }
633
634 /// Create a position describing the function scope of \p CB.
635 static const IRPosition callsite_function(const CallBase &CB) {
636 return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE);
637 }
638
639 /// Create a position describing the returned value of \p CB.
640 static const IRPosition callsite_returned(const CallBase &CB) {
641 return IRPosition(const_cast<CallBase &>(CB), IRP_CALL_SITE_RETURNED);
642 }
643
644 /// Create a position describing the argument of \p CB at position \p ArgNo.
645 static const IRPosition callsite_argument(const CallBase &CB,
646 unsigned ArgNo) {
647 return IRPosition(const_cast<Use &>(CB.getArgOperandUse(ArgNo)),
649 }
650
651 /// Create a position describing the argument of \p ACS at position \p ArgNo.
653 unsigned ArgNo) {
654 if (ACS.getNumArgOperands() <= ArgNo)
655 return IRPosition();
656 int CSArgNo = ACS.getCallArgOperandNo(ArgNo);
657 if (CSArgNo >= 0)
659 cast<CallBase>(*ACS.getInstruction()), CSArgNo);
660 return IRPosition();
661 }
662
663 /// Create a position with function scope matching the "context" of \p IRP.
664 /// If \p IRP is a call site (see isAnyCallSitePosition()) then the result
665 /// will be a call site position, otherwise the function position of the
666 /// associated function.
667 static const IRPosition
669 const CallBaseContext *CBContext = nullptr) {
670 if (IRP.isAnyCallSitePosition()) {
673 }
675 return IRPosition::function(*IRP.getAssociatedFunction(), CBContext);
676 }
677
678 bool operator==(const IRPosition &RHS) const {
679 return Enc == RHS.Enc && RHS.CBContext == CBContext;
680 }
681 bool operator!=(const IRPosition &RHS) const { return !(*this == RHS); }
682
683 /// Return the value this abstract attribute is anchored with.
684 ///
685 /// The anchor value might not be the associated value if the latter is not
686 /// sufficient to determine where arguments will be manifested. This is, so
687 /// far, only the case for call site arguments as the value is not sufficient
688 /// to pinpoint them. Instead, we can use the call site as an anchor.
690 switch (getEncodingBits()) {
691 case ENC_VALUE:
692 case ENC_RETURNED_VALUE:
693 case ENC_FLOATING_FUNCTION:
694 return *getAsValuePtr();
695 case ENC_CALL_SITE_ARGUMENT_USE:
696 return *(getAsUsePtr()->getUser());
697 default:
698 llvm_unreachable("Unkown encoding!");
699 };
700 }
701
702 /// Return the associated function, if any.
704 if (auto *CB = dyn_cast<CallBase>(&getAnchorValue())) {
705 // We reuse the logic that associates callback calles to arguments of a
706 // call site here to identify the callback callee as the associated
707 // function.
708 if (Argument *Arg = getAssociatedArgument())
709 return Arg->getParent();
711 CB->getCalledOperand()->stripPointerCasts());
712 }
713 return getAnchorScope();
714 }
715
716 /// Return the associated argument, if any.
718
719 /// Return true if the position refers to a function interface, that is the
720 /// function scope, the function return, or an argument.
721 bool isFnInterfaceKind() const {
722 switch (getPositionKind()) {
726 return true;
727 default:
728 return false;
729 }
730 }
731
732 /// Return true if this is a function or call site position.
733 bool isFunctionScope() const {
734 switch (getPositionKind()) {
737 return true;
738 default:
739 return false;
740 };
741 }
742
743 /// Return the Function surrounding the anchor value.
745 Value &V = getAnchorValue();
746 if (isa<Function>(V))
747 return &cast<Function>(V);
748 if (isa<Argument>(V))
749 return cast<Argument>(V).getParent();
750 if (isa<Instruction>(V))
751 return cast<Instruction>(V).getFunction();
752 return nullptr;
753 }
754
755 /// Return the context instruction, if any.
757 Value &V = getAnchorValue();
758 if (auto *I = dyn_cast<Instruction>(&V))
759 return I;
760 if (auto *Arg = dyn_cast<Argument>(&V))
761 if (!Arg->getParent()->isDeclaration())
762 return &Arg->getParent()->getEntryBlock().front();
763 if (auto *F = dyn_cast<Function>(&V))
764 if (!F->isDeclaration())
765 return &(F->getEntryBlock().front());
766 return nullptr;
767 }
768
769 /// Return the value this abstract attribute is associated with.
772 return getAnchorValue();
773 assert(isa<CallBase>(&getAnchorValue()) && "Expected a call base!");
775 ->getArgOperand(getCallSiteArgNo());
776 }
777
778 /// Return the type this abstract attribute is associated with.
784
785 /// Return the callee argument number of the associated value if it is an
786 /// argument or call site argument, otherwise a negative value. In contrast to
787 /// `getCallSiteArgNo` this method will always return the "argument number"
788 /// from the perspective of the callee. This may not the same as the call site
789 /// if this is a callback call.
790 int getCalleeArgNo() const {
791 return getArgNo(/* CallbackCalleeArgIfApplicable */ true);
792 }
793
794 /// Return the call site argument number of the associated value if it is an
795 /// argument or call site argument, otherwise a negative value. In contrast to
796 /// `getCalleArgNo` this method will always return the "operand number" from
797 /// the perspective of the call site. This may not the same as the callee
798 /// perspective if this is a callback call.
799 int getCallSiteArgNo() const {
800 return getArgNo(/* CallbackCalleeArgIfApplicable */ false);
801 }
802
803 /// Return the index in the attribute list for this position.
804 unsigned getAttrIdx() const {
805 switch (getPositionKind()) {
808 break;
811 return AttributeList::FunctionIndex;
814 return AttributeList::ReturnIndex;
816 return getCalleeArgNo() + AttributeList::FirstArgIndex;
818 return getCallSiteArgNo() + AttributeList::FirstArgIndex;
819 }
821 "There is no attribute index for a floating or invalid position!");
822 }
823
824 /// Return the value attributes are attached to.
826 if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
827 return CB;
828 return getAssociatedFunction();
829 }
830
831 /// Return the attributes associated with this function or call site scope.
833 if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
834 return CB->getAttributes();
836 }
837
838 /// Update the attributes associated with this function or call site scope.
839 void setAttrList(const AttributeList &AttrList) const {
840 if (auto *CB = dyn_cast<CallBase>(&getAnchorValue()))
841 return CB->setAttributes(AttrList);
842 return getAssociatedFunction()->setAttributes(AttrList);
843 }
844
845 /// Return the associated position kind.
847 char EncodingBits = getEncodingBits();
848 if (EncodingBits == ENC_CALL_SITE_ARGUMENT_USE)
850 if (EncodingBits == ENC_FLOATING_FUNCTION)
851 return IRP_FLOAT;
852
853 Value *V = getAsValuePtr();
854 if (!V)
855 return IRP_INVALID;
856 if (isa<Argument>(V))
857 return IRP_ARGUMENT;
858 if (isa<Function>(V))
859 return isReturnPosition(EncodingBits) ? IRP_RETURNED : IRP_FUNCTION;
860 if (isa<CallBase>(V))
861 return isReturnPosition(EncodingBits) ? IRP_CALL_SITE_RETURNED
863 return IRP_FLOAT;
864 }
865
867 switch (getPositionKind()) {
871 return true;
872 default:
873 return false;
874 }
875 }
876
877 /// Return true if the position is an argument or call site argument.
878 bool isArgumentPosition() const {
879 switch (getPositionKind()) {
882 return true;
883 default:
884 return false;
885 }
886 }
887
888 /// Return the same position without the call base context.
890 IRPosition Result = *this;
891 Result.CBContext = nullptr;
892 return Result;
893 }
894
895 /// Get the call base context from the position.
896 const CallBaseContext *getCallBaseContext() const { return CBContext; }
897
898 /// Check if the position has any call base context.
899 bool hasCallBaseContext() const { return CBContext != nullptr; }
900
901 /// Conversion into a void * to allow reuse of pointer hashing.
902 operator void *() const { return Enc.getOpaqueValue(); }
903
904private:
905 /// Private constructor for special values only!
906 explicit IRPosition(void *Ptr, const CallBaseContext *CBContext = nullptr)
907 : CBContext(CBContext) {
908 Enc.setFromOpaqueValue(Ptr);
909 }
910
911 /// IRPosition anchored at \p AnchorVal with kind/argument numbet \p PK.
912 explicit IRPosition(Value &AnchorVal, Kind PK,
913 const CallBaseContext *CBContext = nullptr)
914 : CBContext(CBContext) {
915 switch (PK) {
917 llvm_unreachable("Cannot create invalid IRP with an anchor value!");
918 break;
920 // Special case for floating functions.
921 if (isa<Function>(AnchorVal) || isa<CallBase>(AnchorVal))
922 Enc = {&AnchorVal, ENC_FLOATING_FUNCTION};
923 else
924 Enc = {&AnchorVal, ENC_VALUE};
925 break;
928 Enc = {&AnchorVal, ENC_VALUE};
929 break;
932 Enc = {&AnchorVal, ENC_RETURNED_VALUE};
933 break;
935 Enc = {&AnchorVal, ENC_VALUE};
936 break;
939 "Cannot create call site argument IRP with an anchor value!");
940 break;
941 }
942 verify();
943 }
944
945 /// Return the callee argument number of the associated value if it is an
946 /// argument or call site argument. See also `getCalleeArgNo` and
947 /// `getCallSiteArgNo`.
948 int getArgNo(bool CallbackCalleeArgIfApplicable) const {
949 if (CallbackCalleeArgIfApplicable)
950 if (Argument *Arg = getAssociatedArgument())
951 return Arg->getArgNo();
952 switch (getPositionKind()) {
954 return cast<Argument>(getAsValuePtr())->getArgNo();
956 Use &U = *getAsUsePtr();
957 return cast<CallBase>(U.getUser())->getArgOperandNo(&U);
958 }
959 default:
960 return -1;
961 }
962 }
963
964 /// IRPosition for the use \p U. The position kind \p PK needs to be
965 /// IRP_CALL_SITE_ARGUMENT, the anchor value is the user, the associated value
966 /// the used value.
967 explicit IRPosition(Use &U, Kind PK) {
969 "Use constructor is for call site arguments only!");
970 Enc = {&U, ENC_CALL_SITE_ARGUMENT_USE};
971 verify();
972 }
973
974 /// Verify internal invariants.
975 LLVM_ABI void verify();
976
977 /// Return the underlying pointer as Value *, valid for all positions but
978 /// IRP_CALL_SITE_ARGUMENT.
979 Value *getAsValuePtr() const {
980 assert(getEncodingBits() != ENC_CALL_SITE_ARGUMENT_USE &&
981 "Not a value pointer!");
982 return reinterpret_cast<Value *>(Enc.getPointer());
983 }
984
985 /// Return the underlying pointer as Use *, valid only for
986 /// IRP_CALL_SITE_ARGUMENT positions.
987 Use *getAsUsePtr() const {
988 assert(getEncodingBits() == ENC_CALL_SITE_ARGUMENT_USE &&
989 "Not a value pointer!");
990 return reinterpret_cast<Use *>(Enc.getPointer());
991 }
992
993 /// Return true if \p EncodingBits describe a returned or call site returned
994 /// position.
995 static bool isReturnPosition(char EncodingBits) {
996 return EncodingBits == ENC_RETURNED_VALUE;
997 }
998
999 /// The encoding of the IRPosition is a combination of a pointer and two
1000 /// encoding bits. The values of the encoding bits are defined in the enum
1001 /// below. The pointer is either a Value* (for the first three encoding bit
1002 /// combinations) or Use* (for ENC_CALL_SITE_ARGUMENT_USE).
1003 ///
1004 ///{
1005 enum {
1006 ENC_VALUE = 0b00,
1007 ENC_RETURNED_VALUE = 0b01,
1008 ENC_FLOATING_FUNCTION = 0b10,
1009 ENC_CALL_SITE_ARGUMENT_USE = 0b11,
1010 };
1011
1012 // Reserve the maximal amount of bits so there is no need to mask out the
1013 // remaining ones. We will not encode anything else in the pointer anyway.
1014 static constexpr int NumEncodingBits =
1016 static_assert(NumEncodingBits >= 2, "At least two bits are required!");
1017
1018 /// The pointer with the encoding bits.
1019 PointerIntPair<void *, NumEncodingBits, char> Enc;
1020 ///}
1021
1022 /// Call base context. Used for callsite specific analysis.
1023 const CallBaseContext *CBContext = nullptr;
1024
1025 /// Return the encoding bits.
1026 char getEncodingBits() const { return Enc.getInt(); }
1027};
1028
1029/// Helper that allows IRPosition as a key in a DenseMap.
1030template <> struct DenseMapInfo<IRPosition> {
1031 static unsigned getHashValue(const IRPosition &IRP) {
1032 return (DenseMapInfo<void *>::getHashValue(IRP) << 4) ^
1034 }
1035
1036 static bool isEqual(const IRPosition &a, const IRPosition &b) {
1037 return a == b;
1038 }
1039};
1040
1041/// A visitor class for IR positions.
1042///
1043/// Given a position P, the SubsumingPositionIterator allows to visit "subsuming
1044/// positions" wrt. attributes/information. Thus, if a piece of information
1045/// holds for a subsuming position, it also holds for the position P.
1046///
1047/// The subsuming positions always include the initial position and then,
1048/// depending on the position kind, additionally the following ones:
1049/// - for IRP_RETURNED:
1050/// - the function (IRP_FUNCTION)
1051/// - for IRP_ARGUMENT:
1052/// - the function (IRP_FUNCTION)
1053/// - for IRP_CALL_SITE:
1054/// - the callee (IRP_FUNCTION), if known
1055/// - for IRP_CALL_SITE_RETURNED:
1056/// - the callee (IRP_RETURNED), if known
1057/// - the call site (IRP_FUNCTION)
1058/// - the callee (IRP_FUNCTION), if known
1059/// - for IRP_CALL_SITE_ARGUMENT:
1060/// - the argument of the callee (IRP_ARGUMENT), if known
1061/// - the callee (IRP_FUNCTION), if known
1062/// - the position the call site argument is associated with if it is not
1063/// anchored to the call site, e.g., if it is an argument then the argument
1064/// (IRP_ARGUMENT)
1066 SmallVector<IRPosition, 4> IRPositions;
1067 using iterator = decltype(IRPositions)::iterator;
1068
1069public:
1071 iterator begin() { return IRPositions.begin(); }
1072 iterator end() { return IRPositions.end(); }
1073};
1074
1075/// Wrapper for FunctionAnalysisManager.
1077 // The client may be running the old pass manager, in which case, we need to
1078 // map the requested Analysis to its equivalent wrapper in the old pass
1079 // manager. The scheme implemented here does not require every Analysis to be
1080 // updated. Only those new analyses that the client cares about in the old
1081 // pass manager need to expose a LegacyWrapper type, and that wrapper should
1082 // support a getResult() method that matches the new Analysis.
1083 //
1084 // We need SFINAE to check for the LegacyWrapper, but function templates don't
1085 // allow partial specialization, which is needed in this case. So instead, we
1086 // use a constexpr bool to perform the SFINAE, and then use this information
1087 // inside the function template.
1088 template <typename, typename = void>
1089 static constexpr bool HasLegacyWrapper = false;
1090
1091 template <typename Analysis>
1092 typename Analysis::Result *getAnalysis(const Function &F,
1093 bool RequestCachedOnly = false) {
1094 if (!LegacyPass && !FAM)
1095 return nullptr;
1096 if (FAM) {
1097 if (CachedOnly || RequestCachedOnly)
1098 return FAM->getCachedResult<Analysis>(const_cast<Function &>(F));
1099 return &FAM->getResult<Analysis>(const_cast<Function &>(F));
1100 }
1101 if constexpr (HasLegacyWrapper<Analysis>) {
1102 if (!CachedOnly && !RequestCachedOnly)
1103 return &LegacyPass
1104 ->getAnalysis<typename Analysis::LegacyWrapper>(
1105 const_cast<Function &>(F))
1106 .getResult();
1107 if (auto *P =
1108 LegacyPass
1109 ->getAnalysisIfAvailable<typename Analysis::LegacyWrapper>())
1110 return &P->getResult();
1111 }
1112 return nullptr;
1113 }
1114
1115 /// Invalidates the analyses. Valid only when using the new pass manager.
1117 assert(FAM && "Can only be used from the new PM!");
1118 FAM->clear();
1119 }
1120
1121 AnalysisGetter(FunctionAnalysisManager &FAM, bool CachedOnly = false)
1122 : FAM(&FAM), CachedOnly(CachedOnly) {}
1123 AnalysisGetter(Pass *P, bool CachedOnly = false)
1124 : LegacyPass(P), CachedOnly(CachedOnly) {}
1125 AnalysisGetter() = default;
1126
1127private:
1128 FunctionAnalysisManager *FAM = nullptr;
1129 Pass *LegacyPass = nullptr;
1130
1131 /// If \p CachedOnly is true, no pass is created, just existing results are
1132 /// used. Also available per request.
1133 bool CachedOnly = false;
1134};
1135
1136template <typename Analysis>
1138 Analysis, std::void_t<typename Analysis::LegacyWrapper>> = true;
1139
1140/// Data structure to hold cached (LLVM-IR) information.
1141///
1142/// All attributes are given an InformationCache object at creation time to
1143/// avoid inspection of the IR by all of them individually. This default
1144/// InformationCache will hold information required by 'default' attributes,
1145/// thus the ones deduced when Attributor::identifyDefaultAbstractAttributes(..)
1146/// is called.
1147///
1148/// If custom abstract attributes, registered manually through
1149/// Attributor::registerAA(...), need more information, especially if it is not
1150/// reusable, it is advised to inherit from the InformationCache and cast the
1151/// instance down in the abstract attributes.
1155 bool UseExplorer = true)
1156 : CGSCC(CGSCC), M(M), Allocator(Allocator), AG(AG) {
1157 if (UseExplorer)
1158 Explorer = new (Allocator) MustBeExecutedContextExplorer(
1159 /* ExploreInterBlock */
1160 true, /* ExploreCFGForward */ true,
1161 /* ExploreCFGBackward */ true,
1162 /* LIGetter */
1163 [&](const Function &F) { return AG.getAnalysis<LoopAnalysis>(F); },
1164 /* DTGetter */
1165 [&](const Function &F) {
1166 return AG.getAnalysis<DominatorTreeAnalysis>(F);
1167 },
1168 /* PDTGetter */
1169 [&](const Function &F) {
1170 return AG.getAnalysis<PostDominatorTreeAnalysis>(F);
1171 });
1172 }
1173
1175 // The FunctionInfo objects are allocated via a BumpPtrAllocator, we call
1176 // the destructor manually.
1177 for (auto &It : FuncInfoMap)
1178 It.getSecond()->~FunctionInfo();
1179 // Same is true for the instruction exclusions sets.
1181 for (auto *BES : BESets)
1182 BES->~InstExclusionSetTy();
1183 if (Explorer)
1184 Explorer->~MustBeExecutedContextExplorer();
1185 }
1186
1187 /// Apply \p CB to all uses of \p F. If \p LookThroughConstantExprUses is
1188 /// true, constant expression users are not given to \p CB but their uses are
1189 /// traversed transitively.
1190 template <typename CBTy>
1191 static void foreachUse(Function &F, CBTy CB,
1192 bool LookThroughConstantExprUses = true) {
1193 SmallVector<Use *, 8> Worklist(make_pointer_range(F.uses()));
1194
1195 for (unsigned Idx = 0; Idx < Worklist.size(); ++Idx) {
1196 Use &U = *Worklist[Idx];
1197
1198 // Allow use in constant bitcasts and simply look through them.
1199 if (LookThroughConstantExprUses && isa<ConstantExpr>(U.getUser())) {
1200 for (Use &CEU : cast<ConstantExpr>(U.getUser())->uses())
1201 Worklist.push_back(&CEU);
1202 continue;
1203 }
1204
1205 CB(U);
1206 }
1207 }
1208
1209 /// The CG-SCC the pass is run on, or nullptr if it is a module pass.
1210 const SetVector<Function *> *const CGSCC = nullptr;
1211
1212 /// A vector type to hold instructions.
1214
1215 /// A map type from opcodes to instructions with this opcode.
1217
1218 /// Return the map that relates "interesting" opcodes with all instructions
1219 /// with that opcode in \p F.
1221 return getFunctionInfo(F).OpcodeInstMap;
1222 }
1223
1224 /// Return the instructions in \p F that may read or write memory.
1226 return getFunctionInfo(F).RWInsts;
1227 }
1228
1229 /// Return MustBeExecutedContextExplorer
1233
1234 /// Return TargetLibraryInfo for function \p F.
1238
1239 /// Return true if \p F has the "kernel" function attribute
1240 bool isKernel(const Function &F) {
1241 FunctionInfo &FI = getFunctionInfo(F);
1242 return FI.IsKernel;
1243 }
1244
1245 /// Return true if \p Arg is involved in a must-tail call, thus the argument
1246 /// of the caller or callee.
1248 FunctionInfo &FI = getFunctionInfo(*Arg.getParent());
1249 return FI.CalledViaMustTail || FI.ContainsMustTailCall;
1250 }
1251
1252 bool isOnlyUsedByAssume(const Instruction &I) const {
1253 return AssumeOnlyValues.contains(&I);
1254 }
1255
1256 /// Invalidates the cached analyses. Valid only when using the new pass
1257 /// manager.
1258 void invalidateAnalyses() { AG.invalidateAnalyses(); }
1259
1260 /// Return the analysis result from a pass \p AP for function \p F.
1261 template <typename AP>
1262 typename AP::Result *getAnalysisResultForFunction(const Function &F,
1263 bool CachedOnly = false) {
1264 return AG.getAnalysis<AP>(F, CachedOnly);
1265 }
1266
1267 const Module &getModule() const { return M; }
1268
1269 /// Return datalayout used in the module.
1270 const DataLayout &getDL() const { return M.getDataLayout(); }
1271
1272 /// Return the map conaining all the knowledge we have from `llvm.assume`s.
1273 const RetainedKnowledgeMap &getKnowledgeMap() const { return KnowledgeMap; }
1274
1275 /// Given \p BES, return a uniqued version.
1278 auto It = BESets.find(BES);
1279 if (It != BESets.end())
1280 return *It;
1281 auto *UniqueBES = new (Allocator) AA::InstExclusionSetTy(*BES);
1282 bool Success = BESets.insert(UniqueBES).second;
1283 (void)Success;
1284 assert(Success && "Expected only new entries to be added");
1285 return UniqueBES;
1286 }
1287
1288 /// Return true if the stack (llvm::Alloca) can be accessed by other threads.
1290
1291 /// Return true if the target is a GPU.
1292 bool IsTargetGPU() const { return M.getTargetTriple().isGPU(); }
1293
1294 /// Return all functions that might be called indirectly, only valid for
1295 /// closed world modules (see isClosedWorldModule).
1298
1299 /// Return the flat address space if the associated target has.
1300 LLVM_ABI std::optional<unsigned> getFlatAddressSpace() const;
1301
1302 virtual unsigned getMaxAddrSpace() const { return ~0U; }
1303
1304private:
1305 struct FunctionInfo {
1306 LLVM_ABI ~FunctionInfo();
1307
1308 /// A nested map that remembers all instructions in a function with a
1309 /// certain instruction opcode (Instruction::getOpcode()).
1310 OpcodeInstMapTy OpcodeInstMap;
1311
1312 /// A map from functions to their instructions that may read or write
1313 /// memory.
1314 InstructionVectorTy RWInsts;
1315
1316 /// Function is called by a `musttail` call.
1317 bool CalledViaMustTail;
1318
1319 /// Function contains a `musttail` call.
1320 bool ContainsMustTailCall;
1321
1322 /// Function has the `"kernel"` attribute
1323 bool IsKernel;
1324 };
1325
1326 /// A map type from functions to informatio about it.
1327 DenseMap<const Function *, FunctionInfo *> FuncInfoMap;
1328
1329 /// Return information about the function \p F, potentially by creating it.
1330 FunctionInfo &getFunctionInfo(const Function &F) {
1331 FunctionInfo *&FI = FuncInfoMap[&F];
1332 if (!FI) {
1333 FI = new (Allocator) FunctionInfo();
1334 initializeInformationCache(F, *FI);
1335 }
1336 return *FI;
1337 }
1338
1339 /// Vector of functions that might be callable indirectly, i.a., via a
1340 /// function pointer.
1341 SmallVector<Function *> IndirectlyCallableFunctions;
1342
1343 /// Initialize the function information cache \p FI for the function \p F.
1344 ///
1345 /// This method needs to be called for all function that might be looked at
1346 /// through the information cache interface *prior* to looking at them.
1347 LLVM_ABI void initializeInformationCache(const Function &F, FunctionInfo &FI);
1348
1349 /// The module.
1350 const Module &M;
1351
1352 /// The allocator used to allocate memory, e.g. for `FunctionInfo`s.
1353 BumpPtrAllocator &Allocator;
1354
1355 /// MustBeExecutedContextExplorer
1356 MustBeExecutedContextExplorer *Explorer = nullptr;
1357
1358 /// A map with knowledge retained in `llvm.assume` instructions.
1359 RetainedKnowledgeMap KnowledgeMap;
1360
1361 /// A container for all instructions that are only used by `llvm.assume`.
1362 SetVector<const Instruction *> AssumeOnlyValues;
1363
1364 /// Cache for block sets to allow reuse.
1365 DenseSet<const AA::InstExclusionSetTy *> BESets;
1366
1367 /// Getters for analysis.
1368 AnalysisGetter &AG;
1369
1370 /// Set of inlineable functions
1371 SmallPtrSet<const Function *, 8> InlineableFunctions;
1372
1373 /// Give the Attributor access to the members so
1374 /// Attributor::identifyDefaultAbstractAttributes(...) can initialize them.
1375 friend struct Attributor;
1376};
1377
1378/// Configuration for the Attributor.
1380
1382
1383 /// Is the user of the Attributor a module pass or not. This determines what
1384 /// IR we can look at and modify. If it is a module pass we might deduce facts
1385 /// outside the initial function set and modify functions outside that set,
1386 /// but only as part of the optimization of the functions in the initial
1387 /// function set. For CGSCC passes we can look at the IR of the module slice
1388 /// but never run any deduction, or perform any modification, outside the
1389 /// initial function set (which we assume is the SCC).
1390 bool IsModulePass = true;
1391
1392 /// Flag to determine if we can delete functions or keep dead ones around.
1393 bool DeleteFns = true;
1394
1395 /// Flag to determine if we rewrite function signatures.
1397
1398 /// Flag to determine if we want to initialize all default AAs for an internal
1399 /// function marked live. See also: InitializationCallback>
1401
1402 /// Flag to determine if we should skip all liveness checks early on.
1403 bool UseLiveness = true;
1404
1405 /// Flag to indicate if the entire world is contained in this module, that
1406 /// is, no outside functions exist.
1408
1409 /// Callback function to be invoked on internal functions marked live.
1410 std::function<void(Attributor &A, const Function &F)> InitializationCallback =
1411 nullptr;
1412
1413 /// Callback function to determine if an indirect call targets should be made
1414 /// direct call targets (with an if-cascade).
1415 std::function<bool(Attributor &A, const AbstractAttribute &AA, CallBase &CB,
1416 Function &AssumedCallee, unsigned NumAssumedCallees)>
1418
1419 /// Helper to update an underlying call graph and to delete functions.
1421
1422 /// If not null, a set limiting the attribute opportunities.
1424
1425 /// Maximum number of iterations to run until fixpoint.
1426 std::optional<unsigned> MaxFixpointIterations;
1427
1428 /// A callback function that returns an ORE object from a Function pointer.
1429 ///{
1433 ///}
1434
1435 /// The name of the pass running the attributor, used to emit remarks.
1436 const char *PassName = nullptr;
1437
1438 using IPOAmendableCBTy = std::function<bool(const Function &F)>;
1440};
1441
1442/// Check the debug counter used to limit the number of AAs created. Returns
1443/// false if no further AA should be created.
1445
1446/// The fixpoint analysis framework that orchestrates the attribute deduction.
1447///
1448/// The Attributor provides a general abstract analysis framework (guided
1449/// fixpoint iteration) as well as helper functions for the deduction of
1450/// (LLVM-IR) attributes. However, also other code properties can be deduced,
1451/// propagated, and ultimately manifested through the Attributor framework. This
1452/// is particularly useful if these properties interact with attributes and a
1453/// co-scheduled deduction allows to improve the solution. Even if not, thus if
1454/// attributes/properties are completely isolated, they should use the
1455/// Attributor framework to reduce the number of fixpoint iteration frameworks
1456/// in the code base. Note that the Attributor design makes sure that isolated
1457/// attributes are not impacted, in any way, by others derived at the same time
1458/// if there is no cross-reasoning performed.
1459///
1460/// The public facing interface of the Attributor is kept simple and basically
1461/// allows abstract attributes to one thing, query abstract attributes
1462/// in-flight. There are two reasons to do this:
1463/// a) The optimistic state of one abstract attribute can justify an
1464/// optimistic state of another, allowing to framework to end up with an
1465/// optimistic (=best possible) fixpoint instead of one based solely on
1466/// information in the IR.
1467/// b) This avoids reimplementing various kinds of lookups, e.g., to check
1468/// for existing IR attributes, in favor of a single lookups interface
1469/// provided by an abstract attribute subclass.
1470///
1471/// NOTE: The mechanics of adding a new "concrete" abstract attribute are
1472/// described in the file comment.
1474
1475 /// Constructor
1476 ///
1477 /// \param Functions The set of functions we are deriving attributes for.
1478 /// \param InfoCache Cache to hold various information accessible for
1479 /// the abstract attributes.
1480 /// \param Configuration The Attributor configuration which determines what
1481 /// generic features to use.
1483 InformationCache &InfoCache,
1484 AttributorConfig Configuration);
1485
1487
1488 /// Run the analyses until a fixpoint is reached or enforced (timeout).
1489 ///
1490 /// The attributes registered with this Attributor can be used after as long
1491 /// as the Attributor is not destroyed (it owns the attributes now).
1492 ///
1493 /// \Returns CHANGED if the IR was changed, otherwise UNCHANGED.
1495
1496 /// Lookup an abstract attribute of type \p AAType at position \p IRP. While
1497 /// no abstract attribute is found equivalent positions are checked, see
1498 /// SubsumingPositionIterator. Thus, the returned abstract attribute
1499 /// might be anchored at a different position, e.g., the callee if \p IRP is a
1500 /// call base.
1501 ///
1502 /// This method is the only (supported) way an abstract attribute can retrieve
1503 /// information from another abstract attribute. As an example, take an
1504 /// abstract attribute that determines the memory access behavior for a
1505 /// argument (readnone, readonly, ...). It should use `getAAFor` to get the
1506 /// most optimistic information for other abstract attributes in-flight, e.g.
1507 /// the one reasoning about the "captured" state for the argument or the one
1508 /// reasoning on the memory access behavior of the function as a whole.
1509 ///
1510 /// If the DepClass enum is set to `DepClassTy::None` the dependence from
1511 /// \p QueryingAA to the return abstract attribute is not automatically
1512 /// recorded. This should only be used if the caller will record the
1513 /// dependence explicitly if necessary, thus if it the returned abstract
1514 /// attribute is used for reasoning. To record the dependences explicitly use
1515 /// the `Attributor::recordDependence` method.
1516 template <typename AAType>
1517 const AAType *getAAFor(const AbstractAttribute &QueryingAA,
1518 const IRPosition &IRP, DepClassTy DepClass) {
1519 return getOrCreateAAFor<AAType>(IRP, &QueryingAA, DepClass,
1520 /* ForceUpdate */ false);
1521 }
1522
1523 /// The version of getAAFor that allows to omit a querying abstract
1524 /// attribute. Using this after Attributor started running is restricted to
1525 /// only the Attributor itself. Initial seeding of AAs can be done via this
1526 /// function.
1527 /// NOTE: ForceUpdate is ignored in any stage other than the update stage.
1528 template <typename AAType>
1529 const AAType *getOrCreateAAFor(IRPosition IRP,
1530 const AbstractAttribute *QueryingAA,
1531 DepClassTy DepClass, bool ForceUpdate = false,
1532 bool UpdateAfterInit = true) {
1533 if (!shouldPropagateCallBaseContext(IRP))
1534 IRP = IRP.stripCallBaseContext();
1535
1536 if (AAType *AAPtr = lookupAAFor<AAType>(IRP, QueryingAA, DepClass,
1537 /* AllowInvalidState */ true)) {
1538 if (ForceUpdate && Phase == AttributorPhase::UPDATE)
1539 updateAA(*AAPtr);
1540 return AAPtr;
1541 }
1542
1543 bool ShouldUpdateAA;
1544 if (!shouldInitialize<AAType>(IRP, ShouldUpdateAA))
1545 return nullptr;
1546
1548 return nullptr;
1549
1550 // No matching attribute found, create one.
1551 // Use the static create method.
1552 auto &AA = AAType::createForPosition(IRP, *this);
1553
1554 // Always register a new attribute to make sure we clean up the allocated
1555 // memory properly.
1556 registerAA(AA);
1557
1558 // If we are currenty seeding attributes, enforce seeding rules.
1559 if (Phase == AttributorPhase::SEEDING && !shouldSeedAttribute(AA)) {
1560 AA.getState().indicatePessimisticFixpoint();
1561 return &AA;
1562 }
1563
1564 // Bootstrap the new attribute with an initial update to propagate
1565 // information, e.g., function -> call site.
1566 {
1567 TimeTraceScope TimeScope("initialize", [&]() {
1568 return AA.getName().str() +
1569 std::to_string(AA.getIRPosition().getPositionKind());
1570 });
1571 ++InitializationChainLength;
1572 AA.initialize(*this);
1573 --InitializationChainLength;
1574 }
1575
1576 if (!ShouldUpdateAA) {
1577 AA.getState().indicatePessimisticFixpoint();
1578 return &AA;
1579 }
1580
1581 // Allow seeded attributes to declare dependencies.
1582 // Remember the seeding state.
1583 if (UpdateAfterInit) {
1584 AttributorPhase OldPhase = Phase;
1585 Phase = AttributorPhase::UPDATE;
1586
1587 updateAA(AA);
1588
1589 Phase = OldPhase;
1590 }
1591
1592 if (QueryingAA && AA.getState().isValidState())
1593 recordDependence(AA, const_cast<AbstractAttribute &>(*QueryingAA),
1594 DepClass);
1595 return &AA;
1596 }
1597
1598 template <typename AAType>
1599 const AAType *getOrCreateAAFor(const IRPosition &IRP) {
1600 return getOrCreateAAFor<AAType>(IRP, /* QueryingAA */ nullptr,
1602 }
1603
1604 /// Return the attribute of \p AAType for \p IRP if existing and valid. This
1605 /// also allows non-AA users lookup.
1606 template <typename AAType>
1607 AAType *lookupAAFor(const IRPosition &IRP,
1608 const AbstractAttribute *QueryingAA = nullptr,
1610 bool AllowInvalidState = false) {
1611 static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1612 "Cannot query an attribute with a type not derived from "
1613 "'AbstractAttribute'!");
1614 // Lookup the abstract attribute of type AAType. If found, return it after
1615 // registering a dependence of QueryingAA on the one returned attribute.
1616 AbstractAttribute *AAPtr = AAMap.lookup({&AAType::ID, IRP});
1617 if (!AAPtr)
1618 return nullptr;
1619
1620 AAType *AA = static_cast<AAType *>(AAPtr);
1621
1622 // Do not register a dependence on an attribute with an invalid state.
1623 if (DepClass != DepClassTy::NONE && QueryingAA &&
1624 AA->getState().isValidState())
1625 recordDependence(*AA, const_cast<AbstractAttribute &>(*QueryingAA),
1626 DepClass);
1627
1628 // Return nullptr if this attribute has an invalid state.
1629 if (!AllowInvalidState && !AA->getState().isValidState())
1630 return nullptr;
1631 return AA;
1632 }
1633
1634 /// Allows a query AA to request an update if a new query was received.
1636
1637 /// Explicitly record a dependence from \p FromAA to \p ToAA, that is if
1638 /// \p FromAA changes \p ToAA should be updated as well.
1639 ///
1640 /// This method should be used in conjunction with the `getAAFor` method and
1641 /// with the DepClass enum passed to the method set to None. This can
1642 /// be beneficial to avoid false dependences but it requires the users of
1643 /// `getAAFor` to explicitly record true dependences through this method.
1644 /// The \p DepClass flag indicates if the dependence is striclty necessary.
1645 /// That means for required dependences, if \p FromAA changes to an invalid
1646 /// state, \p ToAA can be moved to a pessimistic fixpoint because it required
1647 /// information from \p FromAA but none are available anymore.
1648 LLVM_ABI void recordDependence(const AbstractAttribute &FromAA,
1649 const AbstractAttribute &ToAA,
1650 DepClassTy DepClass);
1651
1652 /// Introduce a new abstract attribute into the fixpoint analysis.
1653 ///
1654 /// Note that ownership of the attribute is given to the Attributor. It will
1655 /// invoke delete for the Attributor on destruction of the Attributor.
1656 ///
1657 /// Attributes are identified by their IR position (AAType::getIRPosition())
1658 /// and the address of their static member (see AAType::ID).
1659 template <typename AAType> AAType &registerAA(AAType &AA) {
1660 static_assert(std::is_base_of<AbstractAttribute, AAType>::value,
1661 "Cannot register an attribute with a type not derived from "
1662 "'AbstractAttribute'!");
1663 // Put the attribute in the lookup map structure and the container we use to
1664 // keep track of all attributes.
1665 const IRPosition &IRP = AA.getIRPosition();
1666 AbstractAttribute *&AAPtr = AAMap[{&AAType::ID, IRP}];
1667
1668 assert(!AAPtr && "Attribute already in map!");
1669 AAPtr = &AA;
1670
1671 // Register AA with the synthetic root only before the manifest stage.
1672 if (isDuringDeduction())
1673 DG.SyntheticRoot.Deps.insert(
1675
1676 return AA;
1677 }
1678
1679 /// Return the internal information cache.
1680 InformationCache &getInfoCache() { return InfoCache; }
1681
1682 /// Return the module.
1683 const Module &getModule() { return InfoCache.getModule(); }
1684
1685 /// Return true if this is a module pass, false otherwise.
1686 bool isModulePass() const { return Configuration.IsModulePass; }
1687
1688 /// Return true if we should specialize the call site \b CB for the potential
1689 /// callee \p Fn.
1691 CallBase &CB, Function &Callee,
1692 unsigned NumAssumedCallees) {
1693 return Configuration.IndirectCalleeSpecializationCallback
1694 ? Configuration.IndirectCalleeSpecializationCallback(
1695 *this, AA, CB, Callee, NumAssumedCallees)
1696 : true;
1697 }
1698
1699 /// Return true if the module contains the whole world, thus, no outside
1700 /// functions exist.
1701 LLVM_ABI bool isClosedWorldModule() const;
1702
1703 /// Return true if we derive attributes for \p Fn
1704 bool isRunOn(Function &Fn) const { return isRunOn(&Fn); }
1705 bool isRunOn(Function *Fn) const {
1706 return Functions.empty() || Functions.count(Fn);
1707 }
1708
1709 template <typename AAType> bool shouldUpdateAA(const IRPosition &IRP) {
1710 // If this is queried in the manifest stage, we force the AA to indicate
1711 // pessimistic fixpoint immediately.
1712 if (!isDuringDeduction())
1713 return false;
1714
1715 Function *AssociatedFn = IRP.getAssociatedFunction();
1716
1717 if (IRP.isAnyCallSitePosition()) {
1718 // Check if we require a callee but there is none.
1719 if (!AssociatedFn && AAType::requiresCalleeForCallBase())
1720 return false;
1721
1722 // Check if we require non-asm but it is inline asm.
1723 if (AAType::requiresNonAsmForCallBase() &&
1724 cast<CallBase>(IRP.getAnchorValue()).isInlineAsm())
1725 return false;
1726 }
1727
1728 // Check if we require a calles but we can't see all.
1729 if (AAType::requiresCallersForArgOrFunction())
1732 if (!AssociatedFn->hasLocalLinkage())
1733 return false;
1734
1735 if (!AAType::isValidIRPositionForUpdate(*this, IRP))
1736 return false;
1737
1738 // We update only AAs associated with functions in the Functions set or
1739 // call sites of them.
1740 return (!AssociatedFn || isModulePass() || isRunOn(AssociatedFn) ||
1741 isRunOn(IRP.getAnchorScope()));
1742 }
1743
1744 template <typename AAType>
1745 bool shouldInitialize(const IRPosition &IRP, bool &ShouldUpdateAA) {
1746 if (!AAType::isValidIRPositionForInit(*this, IRP))
1747 return false;
1748
1749 if (Configuration.Allowed && !Configuration.Allowed->count(&AAType::ID))
1750 return false;
1751
1752 // For now we skip anything in naked and optnone functions.
1753 const Function *AnchorFn = IRP.getAnchorScope();
1754 if (AnchorFn && (AnchorFn->hasFnAttribute(Attribute::Naked) ||
1755 AnchorFn->hasFnAttribute(Attribute::OptimizeNone)))
1756 return false;
1757
1758 // Avoid too many nested initializations to prevent a stack overflow.
1759 if (InitializationChainLength > MaxInitializationChainLength)
1760 return false;
1761
1762 ShouldUpdateAA = shouldUpdateAA<AAType>(IRP);
1763
1764 return !AAType::hasTrivialInitializer() || ShouldUpdateAA;
1765 }
1766
1767 /// Determine opportunities to derive 'default' attributes in \p F and create
1768 /// abstract attribute objects for them.
1769 ///
1770 /// \param F The function that is checked for attribute opportunities.
1771 ///
1772 /// Note that abstract attribute instances are generally created even if the
1773 /// IR already contains the information they would deduce. The most important
1774 /// reason for this is the single interface, the one of the abstract attribute
1775 /// instance, which can be queried without the need to look at the IR in
1776 /// various places.
1778
1779 /// Determine whether the function \p F is IPO amendable
1780 ///
1781 /// If a function is exactly defined or it has alwaysinline attribute
1782 /// and is viable to be inlined, we say it is IPO amendable
1784 return F.hasExactDefinition() || InfoCache.InlineableFunctions.count(&F) ||
1785 (Configuration.IPOAmendableCB && Configuration.IPOAmendableCB(F));
1786 }
1787
1788 /// Return whether attributes can participate in fixed-point deduction.
1789 bool isDuringDeduction() const {
1790 return Phase == AttributorPhase::SEEDING ||
1791 Phase == AttributorPhase::UPDATE;
1792 }
1793
1794 /// Mark the internal function \p F as live.
1795 ///
1796 /// This will trigger the identification and initialization of attributes for
1797 /// \p F.
1799 assert(F.hasLocalLinkage() &&
1800 "Only local linkage is assumed dead initially.");
1801
1802 if (Configuration.DefaultInitializeLiveInternals)
1804 if (Configuration.InitializationCallback)
1805 Configuration.InitializationCallback(*this, F);
1806 }
1807
1808 /// Record that \p U is to be replaces with \p NV after information was
1809 /// manifested. This also triggers deletion of trivially dead istructions.
1811 Value *&V = ToBeChangedUses[&U];
1812 if (V && (V->stripPointerCasts() == NV.stripPointerCasts() ||
1814 return false;
1815 assert((!V || V == &NV || isa<UndefValue>(NV)) &&
1816 "Use was registered twice for replacement with different values!");
1817 V = &NV;
1818 return true;
1819 }
1820
1821 /// Helper function to replace all uses associated with \p IRP with \p NV.
1822 /// Return true if there is any change. The flag \p ChangeDroppable indicates
1823 /// if dropppable uses should be changed too.
1825 bool ChangeDroppable = true) {
1827 auto *CB = cast<CallBase>(IRP.getCtxI());
1829 CB->getArgOperandUse(IRP.getCallSiteArgNo()), NV);
1830 }
1831 Value &V = IRP.getAssociatedValue();
1832 auto &Entry = ToBeChangedValues[&V];
1833 Value *CurNV = get<0>(Entry);
1834 if (CurNV && (CurNV->stripPointerCasts() == NV.stripPointerCasts() ||
1835 isa<UndefValue>(CurNV)))
1836 return false;
1837 assert((!CurNV || CurNV == &NV || isa<UndefValue>(NV)) &&
1838 "Value replacement was registered twice with different values!");
1839 Entry = {&NV, ChangeDroppable};
1840 return true;
1841 }
1842
1843 /// Record that \p I is to be replaced with `unreachable` after information
1844 /// was manifested.
1846 ToBeChangedToUnreachableInsts.insert(I);
1847 }
1848
1849 /// Record that \p II has at least one dead successor block. This information
1850 /// is used, e.g., to replace \p II with a call, after information was
1851 /// manifested.
1853 InvokeWithDeadSuccessor.insert(&II);
1854 }
1855
1856 /// Record that \p I is deleted after information was manifested. This also
1857 /// triggers deletion of trivially dead istructions.
1858 void deleteAfterManifest(Instruction &I) { ToBeDeletedInsts.insert(&I); }
1859
1860 /// Record that \p BB is deleted after information was manifested. This also
1861 /// triggers deletion of trivially dead istructions.
1862 void deleteAfterManifest(BasicBlock &BB) { ToBeDeletedBlocks.insert(&BB); }
1863
1864 // Record that \p BB is added during the manifest of an AA. Added basic blocks
1865 // are preserved in the IR.
1867 ManifestAddedBlocks.insert(&BB);
1868 }
1869
1870 /// Record that \p F is deleted after information was manifested.
1872 if (Configuration.DeleteFns)
1873 ToBeDeletedFunctions.insert(&F);
1874 }
1875
1876 /// Return the attributes of kind \p AK existing in the IR as operand bundles
1877 /// of an llvm.assume.
1878 LLVM_ABI bool getAttrsFromAssumes(const IRPosition &IRP,
1881
1882 /// Return true if any kind in \p AKs existing in the IR at a position that
1883 /// will affect this one. See also getAttrs(...).
1884 /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
1885 /// e.g., the function position if this is an
1886 /// argument position, should be ignored.
1887 LLVM_ABI bool
1889 bool IgnoreSubsumingPositions = false,
1890 Attribute::AttrKind ImpliedAttributeKind = Attribute::None);
1891
1892 /// Return the attributes of any kind in \p AKs existing in the IR at a
1893 /// position that will affect this one. While each position can only have a
1894 /// single attribute of any kind in \p AKs, there are "subsuming" positions
1895 /// that could have an attribute as well. This method returns all attributes
1896 /// found in \p Attrs.
1897 /// \param IgnoreSubsumingPositions Flag to determine if subsuming positions,
1898 /// e.g., the function position if this is an
1899 /// argument position, should be ignored.
1900 LLVM_ABI void getAttrs(const IRPosition &IRP,
1903 bool IgnoreSubsumingPositions = false);
1904
1905 /// Remove all \p AttrKinds attached to \p IRP.
1909 ArrayRef<StringRef> Attrs);
1910
1911 /// Attach \p DeducedAttrs to \p IRP, if \p ForceReplace is set we do this
1912 /// even if the same attribute kind was already present. \p DeducedAttrs
1913 /// must be sorted like an attribute set, with at most one attribute of each
1914 /// kind.
1916 ArrayRef<Attribute> DeducedAttrs,
1917 bool ForceReplace = false);
1918
1919private:
1920 /// Helper to check \p Attrs for \p AK, if not found, check if \p
1921 /// AAType::isImpliedByIR is true, and if not, create AAType for \p IRP.
1922 /// If \p SkipHasAttrCheck is true, don't check whether the attribute is set
1923 /// first. This should be used if only some values of a complex IR attribute
1924 /// imply the AAType.
1925 template <Attribute::AttrKind AK, typename AAType>
1926 void checkAndQueryIRAttr(const IRPosition &IRP, AttributeSet Attrs,
1927 bool SkipHasAttrCheck = false);
1928
1929 /// Helper to apply \p CB on all attributes of type \p AttrDescs of \p IRP.
1930 template <typename DescTy>
1931 ChangeStatus updateAttrMap(const IRPosition &IRP, ArrayRef<DescTy> AttrDescs,
1932 function_ref<bool(const DescTy &, AttributeSet,
1933 AttributeMask &, AttrBuilder &)>
1934 CB);
1935
1936 /// Mapping from functions/call sites to their attributes.
1938
1939public:
1940 /// If \p IRP is assumed to be a constant, return it, if it is unclear yet,
1941 /// return std::nullopt, otherwise return `nullptr`.
1942 LLVM_ABI std::optional<Constant *>
1944 bool &UsedAssumedInformation);
1945 std::optional<Constant *> getAssumedConstant(const Value &V,
1946 const AbstractAttribute &AA,
1947 bool &UsedAssumedInformation) {
1948 return getAssumedConstant(IRPosition::value(V), AA, UsedAssumedInformation);
1949 }
1950
1951 /// If \p V is assumed simplified, return it, if it is unclear yet,
1952 /// return std::nullopt, otherwise return `nullptr`.
1953 std::optional<Value *> getAssumedSimplified(const IRPosition &IRP,
1954 const AbstractAttribute &AA,
1955 bool &UsedAssumedInformation,
1956 AA::ValueScope S) {
1957 return getAssumedSimplified(IRP, &AA, UsedAssumedInformation, S);
1958 }
1959 std::optional<Value *> getAssumedSimplified(const Value &V,
1960 const AbstractAttribute &AA,
1961 bool &UsedAssumedInformation,
1962 AA::ValueScope S) {
1964 UsedAssumedInformation, S);
1965 }
1966
1967 /// If \p V is assumed simplified, return it, if it is unclear yet,
1968 /// return std::nullopt, otherwise return `nullptr`. Same as the public
1969 /// version except that it can be used without recording dependences on any \p
1970 /// AA.
1971 LLVM_ABI std::optional<Value *>
1973 bool &UsedAssumedInformation, AA::ValueScope S);
1974
1975 /// Try to simplify \p IRP and in the scope \p S. If successful, true is
1976 /// returned and all potential values \p IRP can take are put into \p Values.
1977 /// If the result in \p Values contains select or PHI instructions it means
1978 /// those could not be simplified to a single value. Recursive calls with
1979 /// these instructions will yield their respective potential values. If false
1980 /// is returned no other information is valid.
1981 LLVM_ABI bool
1984 AA::ValueScope S, bool &UsedAssumedInformation,
1985 bool RecurseForSelectAndPHI = true);
1986
1987 /// Register \p CB as a simplification callback.
1988 /// `Attributor::getAssumedSimplified` will use these callbacks before
1989 /// we it will ask `AAValueSimplify`. It is important to ensure this
1990 /// is called before `identifyDefaultAbstractAttributes`, assuming the
1991 /// latter is called at all.
1992 using SimplifictionCallbackTy = std::function<std::optional<Value *>(
1993 const IRPosition &, const AbstractAttribute *, bool &)>;
1995 const SimplifictionCallbackTy &CB) {
1996 SimplificationCallbacks[IRP].emplace_back(CB);
1997 }
1998
1999 /// Return true if there is a simplification callback for \p IRP.
2001 return SimplificationCallbacks.count(IRP);
2002 }
2003
2004 /// Register \p CB as a simplification callback.
2005 /// Similar to \p registerSimplificationCallback, the call back will be called
2006 /// first when we simplify a global variable \p GV.
2008 std::function<std::optional<Constant *>(
2009 const GlobalVariable &, const AbstractAttribute *, bool &)>;
2011 const GlobalVariable &GV,
2013 GlobalVariableSimplificationCallbacks[&GV].emplace_back(CB);
2014 }
2015
2016 /// Return true if there is a simplification callback for \p GV.
2018 return GlobalVariableSimplificationCallbacks.count(&GV);
2019 }
2020
2021 /// Return \p std::nullopt if there is no call back registered for \p GV or
2022 /// the call back is still not sure if \p GV can be simplified. Return \p
2023 /// nullptr if \p GV can't be simplified.
2024 std::optional<Constant *>
2026 const AbstractAttribute *AA,
2027 bool &UsedAssumedInformation) {
2028 assert(GlobalVariableSimplificationCallbacks.contains(&GV));
2029 for (auto &CB : GlobalVariableSimplificationCallbacks.lookup(&GV)) {
2030 auto SimplifiedGV = CB(GV, AA, UsedAssumedInformation);
2031 // For now we assume the call back will not return a std::nullopt.
2032 assert(SimplifiedGV.has_value() && "SimplifiedGV has not value");
2033 return *SimplifiedGV;
2034 }
2035 llvm_unreachable("there must be a callback registered");
2036 }
2037
2039 std::function<bool(Attributor &, const AbstractAttribute *)>;
2041 const VirtualUseCallbackTy &CB) {
2042 VirtualUseCallbacks[&V].emplace_back(CB);
2043 }
2044
2045private:
2046 /// The vector with all simplification callbacks registered by outside AAs.
2048 SimplificationCallbacks;
2049
2050 /// The vector with all simplification callbacks for global variables
2051 /// registered by outside AAs.
2052 DenseMap<const GlobalVariable *,
2054 GlobalVariableSimplificationCallbacks;
2055
2057 VirtualUseCallbacks;
2058
2059public:
2060 /// Translate \p V from the callee context into the call site context.
2061 LLVM_ABI std::optional<Value *>
2062 translateArgumentToCallSiteContent(std::optional<Value *> V, CallBase &CB,
2063 const AbstractAttribute &AA,
2064 bool &UsedAssumedInformation);
2065
2066 /// Return true if \p AA (or its context instruction) is assumed dead.
2067 ///
2068 /// If \p LivenessAA is not provided it is queried.
2070 const AAIsDead *LivenessAA,
2071 bool &UsedAssumedInformation,
2072 bool CheckBBLivenessOnly = false,
2073 DepClassTy DepClass = DepClassTy::OPTIONAL);
2074
2075 /// Return true if \p I is assumed dead.
2076 ///
2077 /// If \p LivenessAA is not provided it is queried.
2078 LLVM_ABI bool isAssumedDead(const Instruction &I,
2079 const AbstractAttribute *QueryingAA,
2080 const AAIsDead *LivenessAA,
2081 bool &UsedAssumedInformation,
2082 bool CheckBBLivenessOnly = false,
2084 bool CheckForDeadStore = false);
2085
2086 /// Return true if \p U is assumed dead.
2087 ///
2088 /// If \p FnLivenessAA is not provided it is queried.
2089 LLVM_ABI bool isAssumedDead(const Use &U, const AbstractAttribute *QueryingAA,
2090 const AAIsDead *FnLivenessAA,
2091 bool &UsedAssumedInformation,
2092 bool CheckBBLivenessOnly = false,
2093 DepClassTy DepClass = DepClassTy::OPTIONAL);
2094
2095 /// Return true if \p IRP is assumed dead.
2096 ///
2097 /// If \p FnLivenessAA is not provided it is queried.
2098 LLVM_ABI bool isAssumedDead(const IRPosition &IRP,
2099 const AbstractAttribute *QueryingAA,
2100 const AAIsDead *FnLivenessAA,
2101 bool &UsedAssumedInformation,
2102 bool CheckBBLivenessOnly = false,
2103 DepClassTy DepClass = DepClassTy::OPTIONAL);
2104
2105 /// Return true if \p BB is assumed dead.
2106 ///
2107 /// If \p LivenessAA is not provided it is queried.
2108 LLVM_ABI bool isAssumedDead(const BasicBlock &BB,
2109 const AbstractAttribute *QueryingAA,
2110 const AAIsDead *FnLivenessAA,
2111 DepClassTy DepClass = DepClassTy::OPTIONAL);
2112
2113 /// Check \p Pred on all potential Callees of \p CB.
2114 ///
2115 /// This method will evaluate \p Pred with all potential callees of \p CB as
2116 /// input and return true if \p Pred does. If some callees might be unknown
2117 /// this function will return false.
2119 function_ref<bool(ArrayRef<const Function *> Callees)> Pred,
2120 const AbstractAttribute &QueryingAA, const CallBase &CB);
2121
2122 /// Check \p Pred on all (transitive) uses of \p V.
2123 ///
2124 /// This method will evaluate \p Pred on all (transitive) uses of the
2125 /// associated value and return true if \p Pred holds every time.
2126 /// If uses are skipped in favor of equivalent ones, e.g., if we look through
2127 /// memory, the \p EquivalentUseCB will be used to give the caller an idea
2128 /// what original used was replaced by a new one (or new ones). The visit is
2129 /// cut short if \p EquivalentUseCB returns false and the function will return
2130 /// false as well.
2132 function_ref<bool(const Use &, bool &)> Pred,
2133 const AbstractAttribute &QueryingAA, const Value &V,
2134 bool CheckBBLivenessOnly = false,
2135 DepClassTy LivenessDepClass = DepClassTy::OPTIONAL,
2136 bool IgnoreDroppableUses = true,
2137 function_ref<bool(const Use &OldU, const Use &NewU)> EquivalentUseCB =
2138 nullptr);
2139
2140 /// Emit a remark generically.
2141 ///
2142 /// This template function can be used to generically emit a remark. The
2143 /// RemarkKind should be one of the following:
2144 /// - OptimizationRemark to indicate a successful optimization attempt
2145 /// - OptimizationRemarkMissed to report a failed optimization attempt
2146 /// - OptimizationRemarkAnalysis to provide additional information about an
2147 /// optimization attempt
2148 ///
2149 /// The remark is built using a callback function \p RemarkCB that takes a
2150 /// RemarkKind as input and returns a RemarkKind.
2151 template <typename RemarkKind, typename RemarkCallBack>
2153 RemarkCallBack &&RemarkCB) const {
2154 if (!Configuration.OREGetter)
2155 return;
2156
2157 Function *F = I->getFunction();
2158 auto &ORE = Configuration.OREGetter(F);
2159
2160 if (RemarkName.starts_with("OMP"))
2161 ORE.emit([&]() {
2162 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I))
2163 << " [" << RemarkName << "]";
2164 });
2165 else
2166 ORE.emit([&]() {
2167 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, I));
2168 });
2169 }
2170
2171 /// Emit a remark on a function.
2172 template <typename RemarkKind, typename RemarkCallBack>
2173 void emitRemark(Function *F, StringRef RemarkName,
2174 RemarkCallBack &&RemarkCB) const {
2175 if (!Configuration.OREGetter)
2176 return;
2177
2178 auto &ORE = Configuration.OREGetter(F);
2179
2180 if (RemarkName.starts_with("OMP"))
2181 ORE.emit([&]() {
2182 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F))
2183 << " [" << RemarkName << "]";
2184 });
2185 else
2186 ORE.emit([&]() {
2187 return RemarkCB(RemarkKind(Configuration.PassName, RemarkName, F));
2188 });
2189 }
2190
2191 /// Helper struct used in the communication between an abstract attribute (AA)
2192 /// that wants to change the signature of a function and the Attributor which
2193 /// applies the changes. The struct is partially initialized with the
2194 /// information from the AA (see the constructor). All other members are
2195 /// provided by the Attributor prior to invoking any callbacks.
2196 struct ArgumentReplacementInfo {
2197 /// Callee repair callback type
2198 ///
2199 /// The function repair callback is invoked once to rewire the replacement
2200 /// arguments in the body of the new function. The argument replacement info
2201 /// is passed, as build from the registerFunctionSignatureRewrite call, as
2202 /// well as the replacement function and an iteratore to the first
2203 /// replacement argument.
2204 using CalleeRepairCBTy = std::function<void(
2205 const ArgumentReplacementInfo &, Function &, Function::arg_iterator)>;
2206
2207 /// Abstract call site (ACS) repair callback type
2208 ///
2209 /// The abstract call site repair callback is invoked once on every abstract
2210 /// call site of the replaced function (\see ReplacedFn). The callback needs
2211 /// to provide the operands for the call to the new replacement function.
2212 /// The number and type of the operands appended to the provided vector
2213 /// (second argument) is defined by the number and types determined through
2214 /// the replacement type vector (\see ReplacementTypes). The first argument
2215 /// is the ArgumentReplacementInfo object registered with the Attributor
2216 /// through the registerFunctionSignatureRewrite call.
2218 std::function<void(const ArgumentReplacementInfo &, AbstractCallSite,
2220
2221 /// Simple getters, see the corresponding members for details.
2222 ///{
2223
2224 Attributor &getAttributor() const { return A; }
2225 const Function &getReplacedFn() const { return ReplacedFn; }
2226 const Argument &getReplacedArg() const { return ReplacedArg; }
2227 unsigned getNumReplacementArgs() const { return ReplacementTypes.size(); }
2228
2229 ///}
2230
2231 private:
2232 /// Constructor that takes the argument to be replaced, the types of
2233 /// the replacement arguments, as well as callbacks to repair the call sites
2234 /// and new function after the replacement happened.
2236 ArrayRef<Type *> ReplacementTypes,
2237 CalleeRepairCBTy &&CalleeRepairCB,
2238 ACSRepairCBTy &&ACSRepairCB)
2239 : A(A), ReplacedFn(*Arg.getParent()), ReplacedArg(Arg),
2240 ReplacementTypes(ReplacementTypes),
2241 CalleeRepairCB(std::move(CalleeRepairCB)),
2242 ACSRepairCB(std::move(ACSRepairCB)) {}
2243
2244 /// Reference to the attributor to allow access from the callbacks.
2245 Attributor &A;
2246
2247 /// The "old" function replaced by ReplacementFn.
2248 const Function &ReplacedFn;
2249
2250 /// The "old" argument replaced by new ones defined via ReplacementTypes.
2251 const Argument &ReplacedArg;
2252
2253 /// The types of the arguments replacing ReplacedArg.
2254 const SmallVector<Type *, 8> ReplacementTypes;
2255
2256 /// Callee repair callback, see CalleeRepairCBTy.
2257 const CalleeRepairCBTy CalleeRepairCB;
2258
2259 /// Abstract call site (ACS) repair callback, see ACSRepairCBTy.
2260 const ACSRepairCBTy ACSRepairCB;
2261
2262 /// Allow access to the private members from the Attributor.
2263 friend struct Attributor;
2264 };
2265
2266 /// Check if we can rewrite a function signature.
2267 ///
2268 /// The argument \p Arg is replaced with new ones defined by the number,
2269 /// order, and types in \p ReplacementTypes.
2270 ///
2271 /// \returns True, if the replacement can be registered, via
2272 /// registerFunctionSignatureRewrite, false otherwise.
2273 LLVM_ABI bool
2275 ArrayRef<Type *> ReplacementTypes);
2276
2277 /// Register a rewrite for a function signature.
2278 ///
2279 /// The argument \p Arg is replaced with new ones defined by the number,
2280 /// order, and types in \p ReplacementTypes. The rewiring at the call sites is
2281 /// done through \p ACSRepairCB and at the callee site through
2282 /// \p CalleeRepairCB.
2283 ///
2284 /// \returns True, if the replacement was registered, false otherwise.
2286 Argument &Arg, ArrayRef<Type *> ReplacementTypes,
2289
2290 /// Check \p Pred on all function call sites.
2291 ///
2292 /// This method will evaluate \p Pred on call sites and return
2293 /// true if \p Pred holds in every call sites. However, this is only possible
2294 /// all call sites are known, hence the function has internal linkage.
2295 /// If true is returned, \p UsedAssumedInformation is set if assumed
2296 /// information was used to skip or simplify potential call sites.
2298 const AbstractAttribute &QueryingAA,
2299 bool RequireAllCallSites,
2300 bool &UsedAssumedInformation);
2301
2302 /// Check \p Pred on all call sites of \p Fn.
2303 ///
2304 /// This method will evaluate \p Pred on call sites and return
2305 /// true if \p Pred holds in every call sites. However, this is only possible
2306 /// all call sites are known, hence the function has internal linkage.
2307 /// If true is returned, \p UsedAssumedInformation is set if assumed
2308 /// information was used to skip or simplify potential call sites.
2310 const Function &Fn,
2311 bool RequireAllCallSites,
2312 const AbstractAttribute *QueryingAA,
2313 bool &UsedAssumedInformation,
2314 bool CheckPotentiallyDead = false);
2315
2316 /// Check \p Pred on all values potentially returned by the function
2317 /// associated with \p QueryingAA.
2318 ///
2319 /// This is the context insensitive version of the method above.
2320 LLVM_ABI bool
2322 const AbstractAttribute &QueryingAA,
2324 bool RecurseForSelectAndPHI = true);
2325
2326 /// Check \p Pred on all instructions in \p Fn with an opcode present in
2327 /// \p Opcodes.
2328 ///
2329 /// This method will evaluate \p Pred on all instructions with an opcode
2330 /// present in \p Opcode and return true if \p Pred holds on all of them.
2332 const Function *Fn,
2333 const AbstractAttribute *QueryingAA,
2334 ArrayRef<unsigned> Opcodes,
2335 bool &UsedAssumedInformation,
2336 bool CheckBBLivenessOnly = false,
2337 bool CheckPotentiallyDead = false);
2338
2339 /// Check \p Pred on all instructions with an opcode present in \p Opcodes.
2340 ///
2341 /// This method will evaluate \p Pred on all instructions with an opcode
2342 /// present in \p Opcode and return true if \p Pred holds on all of them.
2344 const AbstractAttribute &QueryingAA,
2345 ArrayRef<unsigned> Opcodes,
2346 bool &UsedAssumedInformation,
2347 bool CheckBBLivenessOnly = false,
2348 bool CheckPotentiallyDead = false);
2349
2350 /// Check \p Pred on all call-like instructions (=CallBased derived).
2351 ///
2352 /// See checkForAllCallLikeInstructions(...) for more information.
2354 const AbstractAttribute &QueryingAA,
2355 bool &UsedAssumedInformation,
2356 bool CheckBBLivenessOnly = false,
2357 bool CheckPotentiallyDead = false) {
2359 Pred, QueryingAA,
2360 {(unsigned)Instruction::Invoke, (unsigned)Instruction::CallBr,
2361 (unsigned)Instruction::Call},
2362 UsedAssumedInformation, CheckBBLivenessOnly, CheckPotentiallyDead);
2363 }
2364
2365 /// Check \p Pred on all Read/Write instructions.
2366 ///
2367 /// This method will evaluate \p Pred on all instructions that read or write
2368 /// to memory present in the information cache and return true if \p Pred
2369 /// holds on all of them.
2370 LLVM_ABI bool
2372 AbstractAttribute &QueryingAA,
2373 bool &UsedAssumedInformation);
2374
2375 /// Create a shallow wrapper for \p F such that \p F has internal linkage
2376 /// afterwards. It also sets the original \p F 's name to anonymous
2377 ///
2378 /// A wrapper is a function with the same type (and attributes) as \p F
2379 /// that will only call \p F and return the result, if any.
2380 ///
2381 /// Assuming the declaration of looks like:
2382 /// rty F(aty0 arg0, ..., atyN argN);
2383 ///
2384 /// The wrapper will then look as follows:
2385 /// rty wrapper(aty0 arg0, ..., atyN argN) {
2386 /// return F(arg0, ..., argN);
2387 /// }
2388 ///
2390
2391 /// Returns true if the function \p F can be internalized. i.e. it has a
2392 /// compatible linkage.
2393 LLVM_ABI static bool isInternalizable(Function &F);
2394
2395 /// Make another copy of the function \p F such that the copied version has
2396 /// internal linkage afterwards and can be analysed. Then we replace all uses
2397 /// of the original function to the copied one
2398 ///
2399 /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
2400 /// linkage can be internalized because these linkages guarantee that other
2401 /// definitions with the same name have the same semantics as this one.
2402 ///
2403 /// This will only be run if the `attributor-allow-deep-wrappers` option is
2404 /// set, or if the function is called with \p Force set to true.
2405 ///
2406 /// If the function \p F failed to be internalized the return value will be a
2407 /// null pointer.
2409 bool Force = false);
2410
2411 /// Make copies of each function in the set \p FnSet such that the copied
2412 /// version has internal linkage afterwards and can be analysed. Then we
2413 /// replace all uses of the original function to the copied one. The map
2414 /// \p FnMap contains a mapping of functions to their internalized versions.
2415 ///
2416 /// Only non-locally linked functions that have `linkonce_odr` or `weak_odr`
2417 /// linkage can be internalized because these linkages guarantee that other
2418 /// definitions with the same name have the same semantics as this one.
2419 ///
2420 /// This version will internalize all the functions in the set \p FnSet at
2421 /// once and then replace the uses. This prevents internalized functions being
2422 /// called by external functions when there is an internalized version in the
2423 /// module.
2424 LLVM_ABI static bool
2427
2428 /// Return the data layout associated with the anchor scope.
2429 const DataLayout &getDataLayout() const { return InfoCache.getDL(); }
2430
2431 /// The allocator used to allocate memory, e.g. for `AbstractAttribute`s.
2433
2435 return CGModifiedFunctions;
2436 }
2437
2438private:
2439 /// This method will do fixpoint iteration until fixpoint or the
2440 /// maximum iteration count is reached.
2441 ///
2442 /// If the maximum iteration count is reached, This method will
2443 /// indicate pessimistic fixpoint on attributes that transitively depend
2444 /// on attributes that were scheduled for an update.
2445 void runTillFixpoint();
2446
2447 /// Gets called after scheduling, manifests attributes to the LLVM IR.
2448 ChangeStatus manifestAttributes();
2449
2450 /// Gets called after attributes have been manifested, cleans up the IR.
2451 /// Deletes dead functions, blocks and instructions.
2452 /// Rewrites function signitures and updates the call graph.
2453 ChangeStatus cleanupIR();
2454
2455 /// Identify internal functions that are effectively dead, thus not reachable
2456 /// from a live entry point. The functions are added to ToBeDeletedFunctions.
2457 void identifyDeadInternalFunctions();
2458
2459 /// Run `::update` on \p AA and track the dependences queried while doing so.
2460 /// Also adjust the state if we know further updates are not necessary.
2462
2463 /// Remember the dependences on the top of the dependence stack such that they
2464 /// may trigger further updates. (\see DependenceStack)
2465 void rememberDependences();
2466
2467 /// Determine if CallBase context in \p IRP should be propagated.
2468 LLVM_ABI bool shouldPropagateCallBaseContext(const IRPosition &IRP);
2469
2470 /// Apply all requested function signature rewrites
2471 /// (\see registerFunctionSignatureRewrite) and return Changed if the module
2472 /// was altered.
2474 rewriteFunctionSignatures(SmallSetVector<Function *, 8> &ModifiedFns);
2475
2476 /// Check if the Attribute \p AA should be seeded.
2477 /// See getOrCreateAAFor.
2478 LLVM_ABI bool shouldSeedAttribute(AbstractAttribute &AA);
2479
2480 /// A nested map to lookup abstract attributes based on the argument position
2481 /// on the outer level, and the addresses of the static member (AAType::ID) on
2482 /// the inner level.
2483 ///{
2484 using AAMapKeyTy = std::pair<const char *, IRPosition>;
2486 ///}
2487
2488 /// Map to remember all requested signature changes (= argument replacements).
2489 /// A MapVector so the functions are rewritten in a deterministic order.
2492 ArgumentReplacementMap;
2493
2494 /// The set of functions we are deriving attributes for.
2495 SetVector<Function *> &Functions;
2496
2497 /// The information cache that holds pre-processed (LLVM-IR) information.
2498 InformationCache &InfoCache;
2499
2500 /// Abstract Attribute dependency graph
2501 AADepGraph DG;
2502
2503 /// Set of functions for which we modified the content such that it might
2504 /// impact the call graph.
2505 SmallSetVector<Function *, 8> CGModifiedFunctions;
2506
2507 /// Information about a dependence. If FromAA is changed ToAA needs to be
2508 /// updated as well.
2509 struct DepInfo {
2510 const AbstractAttribute *FromAA;
2511 const AbstractAttribute *ToAA;
2512 DepClassTy DepClass;
2513 };
2514
2515 /// The dependence stack is used to track dependences during an
2516 /// `AbstractAttribute::update` call. As `AbstractAttribute::update` can be
2517 /// recursive we might have multiple vectors of dependences in here. The stack
2518 /// size, should be adjusted according to the expected recursion depth and the
2519 /// inner dependence vector size to the expected number of dependences per
2520 /// abstract attribute. Since the inner vectors are actually allocated on the
2521 /// stack we can be generous with their size.
2522 using DependenceVector = SmallVector<DepInfo, 8>;
2524
2525 /// A set to remember the functions we already assume to be live and visited.
2526 DenseSet<const Function *> VisitedFunctions;
2527
2528 /// Uses we replace with a new value after manifest is done. We will remove
2529 /// then trivially dead instructions as well.
2530 SmallMapVector<Use *, Value *, 32> ToBeChangedUses;
2531
2532 /// Values we replace with a new value after manifest is done. We will remove
2533 /// then trivially dead instructions as well.
2534 SmallMapVector<Value *, PointerIntPair<Value *, 1, bool>, 32>
2535 ToBeChangedValues;
2536
2537 /// Instructions we replace with `unreachable` insts after manifest is done.
2538 SmallSetVector<WeakVH, 16> ToBeChangedToUnreachableInsts;
2539
2540 /// Invoke instructions with at least a single dead successor block.
2541 SmallSetVector<WeakVH, 16> InvokeWithDeadSuccessor;
2542
2543 /// A flag that indicates which stage of the process we are in. Initially, the
2544 /// phase is SEEDING. Phase is changed in `Attributor::run()`
2545 enum class AttributorPhase {
2546 SEEDING,
2547 UPDATE,
2548 MANIFEST,
2549 CLEANUP,
2550 } Phase = AttributorPhase::SEEDING;
2551
2552 /// The current initialization chain length. Tracked to avoid stack overflows.
2553 unsigned InitializationChainLength = 0;
2554
2555 /// Functions, blocks, and instructions we delete after manifest is done.
2556 ///
2557 ///{
2558 SmallPtrSet<BasicBlock *, 8> ManifestAddedBlocks;
2559 SmallSetVector<Function *, 8> ToBeDeletedFunctions;
2560 SmallSetVector<BasicBlock *, 8> ToBeDeletedBlocks;
2561 SmallSetVector<WeakVH, 8> ToBeDeletedInsts;
2562 ///}
2563
2564 /// Container with all the query AAs that requested an update via
2565 /// registerForUpdate.
2566 SmallSetVector<AbstractAttribute *, 16> QueryAAsAwaitingUpdate;
2567
2568 /// User provided configuration for this Attributor instance.
2569 const AttributorConfig Configuration;
2570
2571 friend AADepGraph;
2572 friend AttributorCallGraph;
2573};
2574
2575/// An interface to query the internal state of an abstract attribute.
2576///
2577/// The abstract state is a minimal interface that allows the Attributor to
2578/// communicate with the abstract attributes about their internal state without
2579/// enforcing or exposing implementation details, e.g., the (existence of an)
2580/// underlying lattice.
2581///
2582/// It is sufficient to be able to query if a state is (1) valid or invalid, (2)
2583/// at a fixpoint, and to indicate to the state that (3) an optimistic fixpoint
2584/// was reached or (4) a pessimistic fixpoint was enforced.
2585///
2586/// All methods need to be implemented by the subclass. For the common use case,
2587/// a single boolean state or a bit-encoded state, the BooleanState and
2588/// {Inc,Dec,Bit}IntegerState classes are already provided. An abstract
2589/// attribute can inherit from them to get the abstract state interface and
2590/// additional methods to directly modify the state based if needed. See the
2591/// class comments for help.
2593 virtual ~AbstractState() = default;
2594
2595 /// Return if this abstract state is in a valid state. If false, no
2596 /// information provided should be used.
2597 virtual bool isValidState() const = 0;
2598
2599 /// Return if this abstract state is fixed, thus does not need to be updated
2600 /// if information changes as it cannot change itself.
2601 virtual bool isAtFixpoint() const = 0;
2602
2603 /// Indicate that the abstract state should converge to the optimistic state.
2604 ///
2605 /// This will usually make the optimistically assumed state the known to be
2606 /// true state.
2607 ///
2608 /// \returns ChangeStatus::UNCHANGED as the assumed value should not change.
2610
2611 /// Indicate that the abstract state should converge to the pessimistic state.
2612 ///
2613 /// This will usually revert the optimistically assumed state to the known to
2614 /// be true state.
2615 ///
2616 /// \returns ChangeStatus::CHANGED as the assumed value may change.
2618};
2619
2620/// Simple state with integers encoding.
2621///
2622/// The interface ensures that the assumed bits are always a subset of the known
2623/// bits. Users can only add known bits and, except through adding known bits,
2624/// they can only remove assumed bits. This should guarantee monotonicity and
2625/// thereby the existence of a fixpoint (if used correctly). The fixpoint is
2626/// reached when the assumed and known state/bits are equal. Users can
2627/// force/inidicate a fixpoint. If an optimistic one is indicated, the known
2628/// state will catch up with the assumed one, for a pessimistic fixpoint it is
2629/// the other way around.
2630template <typename base_ty, base_ty BestState, base_ty WorstState>
2632 using base_t = base_ty;
2633
2634 IntegerStateBase() = default;
2636
2637 /// Return the best possible representable state.
2638 static constexpr base_t getBestState() { return BestState; }
2639 static constexpr base_t getBestState(const IntegerStateBase &) {
2640 return getBestState();
2641 }
2642
2643 /// Return the worst possible representable state.
2644 static constexpr base_t getWorstState() { return WorstState; }
2645 static constexpr base_t getWorstState(const IntegerStateBase &) {
2646 return getWorstState();
2647 }
2648
2649 /// See AbstractState::isValidState()
2650 /// NOTE: For now we simply pretend that the worst possible state is invalid.
2651 bool isValidState() const override { return Assumed != getWorstState(); }
2652
2653 /// See AbstractState::isAtFixpoint()
2654 bool isAtFixpoint() const override { return Assumed == Known; }
2655
2656 /// See AbstractState::indicateOptimisticFixpoint(...)
2661
2662 /// See AbstractState::indicatePessimisticFixpoint(...)
2667
2668 /// Return the known state encoding
2669 base_t getKnown() const { return Known; }
2670
2671 /// Return the assumed state encoding.
2672 base_t getAssumed() const { return Assumed; }
2673
2674 /// Equality for IntegerStateBase.
2675 bool
2677 return this->getAssumed() == R.getAssumed() &&
2678 this->getKnown() == R.getKnown();
2679 }
2680
2681 /// Inequality for IntegerStateBase.
2682 bool
2684 return !(*this == R);
2685 }
2686
2687 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2688 /// intended that only information assumed in both states will be assumed in
2689 /// this one afterwards.
2693
2694 /// "Clamp" this state with \p R. The result is subtype dependent but it is
2695 /// intended that information known in either state will be known in
2696 /// this one afterwards.
2700
2702 joinOR(R.getAssumed(), R.getKnown());
2703 }
2704
2706 joinAND(R.getAssumed(), R.getKnown());
2707 }
2708
2709protected:
2710 /// Handle a new assumed value \p Value. Subtype dependent.
2712
2713 /// Handle a new known value \p Value. Subtype dependent.
2715
2716 /// Handle a value \p Value. Subtype dependent.
2717 virtual void joinOR(base_t AssumedValue, base_t KnownValue) = 0;
2718
2719 /// Handle a new assumed value \p Value. Subtype dependent.
2720 virtual void joinAND(base_t AssumedValue, base_t KnownValue) = 0;
2721
2722 /// The known state encoding in an integer of type base_t.
2724
2725 /// The assumed state encoding in an integer of type base_t.
2727};
2728
2729/// Specialization of the integer state for a bit-wise encoding.
2730template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2731 base_ty WorstState = 0>
2733 : public IntegerStateBase<base_ty, BestState, WorstState> {
2735 using base_t = base_ty;
2736 BitIntegerState() = default;
2738
2739 /// Return true if the bits set in \p BitsEncoding are "known bits".
2740 bool isKnown(base_t BitsEncoding = BestState) const {
2741 return (this->Known & BitsEncoding) == BitsEncoding;
2742 }
2743
2744 /// Return true if the bits set in \p BitsEncoding are "assumed bits".
2745 bool isAssumed(base_t BitsEncoding = BestState) const {
2746 return (this->Assumed & BitsEncoding) == BitsEncoding;
2747 }
2748
2749 /// Add the bits in \p BitsEncoding to the "known bits".
2751 // Make sure we never miss any "known bits".
2752 this->Assumed |= Bits;
2753 this->Known |= Bits;
2754 return *this;
2755 }
2756
2757 /// Remove the bits in \p BitsEncoding from the "assumed bits" if not known.
2759 return intersectAssumedBits(~BitsEncoding);
2760 }
2761
2762 /// Remove the bits in \p BitsEncoding from the "known bits".
2764 this->Known = (this->Known & ~BitsEncoding);
2765 return *this;
2766 }
2767
2768 /// Keep only "assumed bits" also set in \p BitsEncoding but all known ones.
2770 // Make sure we never lose any "known bits".
2771 this->Assumed = (this->Assumed & BitsEncoding) | this->Known;
2772 return *this;
2773 }
2774
2775private:
2776 void handleNewAssumedValue(base_t Value) override {
2778 }
2779 void handleNewKnownValue(base_t Value) override { addKnownBits(Value); }
2780 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2781 this->Known |= KnownValue;
2782 this->Assumed |= AssumedValue;
2783 }
2784 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2785 this->Known &= KnownValue;
2786 this->Assumed &= AssumedValue;
2787 }
2788};
2789
2790/// Specialization of the integer state for an increasing value, hence ~0u is
2791/// the best state and 0 the worst.
2792template <typename base_ty = uint32_t, base_ty BestState = ~base_ty(0),
2793 base_ty WorstState = 0>
2795 : public IntegerStateBase<base_ty, BestState, WorstState> {
2797 using base_t = base_ty;
2798
2801
2802 /// Return the best possible representable state.
2803 static constexpr base_t getBestState() { return BestState; }
2804 static constexpr base_t
2808
2809 /// Take minimum of assumed and \p Value.
2811 // Make sure we never lose "known value".
2812 this->Assumed = std::max(std::min(this->Assumed, Value), this->Known);
2813 return *this;
2814 }
2815
2816 /// Take maximum of known and \p Value.
2818 // Make sure we never lose "known value".
2819 this->Assumed = std::max(Value, this->Assumed);
2820 this->Known = std::max(Value, this->Known);
2821 return *this;
2822 }
2823
2824private:
2825 void handleNewAssumedValue(base_t Value) override {
2827 }
2828 void handleNewKnownValue(base_t Value) override { takeKnownMaximum(Value); }
2829 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2830 this->Known = std::max(this->Known, KnownValue);
2831 this->Assumed = std::max(this->Assumed, AssumedValue);
2832 }
2833 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2834 this->Known = std::min(this->Known, KnownValue);
2835 this->Assumed = std::min(this->Assumed, AssumedValue);
2836 }
2837};
2838
2839/// Specialization of the integer state for a decreasing value, hence 0 is the
2840/// best state and ~0u the worst.
2841template <typename base_ty = uint32_t>
2842struct DecIntegerState : public IntegerStateBase<base_ty, 0, ~base_ty(0)> {
2843 using base_t = base_ty;
2844
2845 /// Take maximum of assumed and \p Value.
2847 // Make sure we never lose "known value".
2848 this->Assumed = std::min(std::max(this->Assumed, Value), this->Known);
2849 return *this;
2850 }
2851
2852 /// Take minimum of known and \p Value.
2854 // Make sure we never lose "known value".
2855 this->Assumed = std::min(Value, this->Assumed);
2856 this->Known = std::min(Value, this->Known);
2857 return *this;
2858 }
2859
2860private:
2861 void handleNewAssumedValue(base_t Value) override {
2863 }
2864 void handleNewKnownValue(base_t Value) override { takeKnownMinimum(Value); }
2865 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2866 this->Assumed = std::min(this->Assumed, KnownValue);
2867 this->Assumed = std::min(this->Assumed, AssumedValue);
2868 }
2869 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2870 this->Assumed = std::max(this->Assumed, KnownValue);
2871 this->Assumed = std::max(this->Assumed, AssumedValue);
2872 }
2873};
2874
2875/// Simple wrapper for a single bit (boolean) state.
2876struct BooleanState : public IntegerStateBase<bool, true, false> {
2879
2880 BooleanState() = default;
2882
2883 /// Set the known and asssumed value to \p Value.
2884 void setKnown(bool Value) {
2885 Known |= Value;
2886 Assumed |= Value;
2887 }
2888
2889 /// Return true if the state is assumed to hold.
2890 bool isAssumed() const { return getAssumed(); }
2891
2892 /// Return true if the state is known to hold.
2893 bool isKnown() const { return getKnown(); }
2894
2895private:
2896 void handleNewAssumedValue(base_t Value) override {
2897 if (!Value)
2898 Assumed = Known;
2899 }
2900 void handleNewKnownValue(base_t Value) override {
2901 if (Value)
2902 Known = (Assumed = Value);
2903 }
2904 void joinOR(base_t AssumedValue, base_t KnownValue) override {
2905 Known |= KnownValue;
2906 Assumed |= AssumedValue;
2907 }
2908 void joinAND(base_t AssumedValue, base_t KnownValue) override {
2909 Known &= KnownValue;
2910 Assumed &= AssumedValue;
2911 }
2912};
2913
2914/// State for an integer range.
2916
2917 /// Bitwidth of the associated value.
2919
2920 /// State representing assumed range, initially set to empty.
2922
2923 /// State representing known range, initially set to [-inf, inf].
2925
2929
2933
2934 /// Return the worst possible representable state.
2936 return ConstantRange::getFull(BitWidth);
2937 }
2938
2939 /// Return the best possible representable state.
2941 return ConstantRange::getEmpty(BitWidth);
2942 }
2944 return getBestState(IRS.getBitWidth());
2945 }
2946
2947 /// Return associated values' bit width.
2948 uint32_t getBitWidth() const { return BitWidth; }
2949
2950 /// See AbstractState::isValidState()
2951 bool isValidState() const override {
2952 return BitWidth > 0 && !Assumed.isFullSet();
2953 }
2954
2955 /// See AbstractState::isAtFixpoint()
2956 bool isAtFixpoint() const override { return Assumed == Known; }
2957
2958 /// See AbstractState::indicateOptimisticFixpoint(...)
2963
2964 /// See AbstractState::indicatePessimisticFixpoint(...)
2969
2970 /// Return the known state encoding
2971 ConstantRange getKnown() const { return Known; }
2972
2973 /// Return the assumed state encoding.
2975
2976 /// Unite assumed range with the passed state.
2978 // Don't lose a known range.
2979 Assumed = Assumed.unionWith(R).intersectWith(Known);
2980 }
2981
2982 /// See IntegerRangeState::unionAssumed(..).
2984 unionAssumed(R.getAssumed());
2985 }
2986
2987 /// Intersect known range with the passed state.
2989 Assumed = Assumed.intersectWith(R);
2990 Known = Known.intersectWith(R);
2991 }
2992
2993 /// See IntegerRangeState::intersectKnown(..).
2995 intersectKnown(R.getKnown());
2996 }
2997
2998 /// Equality for IntegerRangeState.
2999 bool operator==(const IntegerRangeState &R) const {
3000 return getAssumed() == R.getAssumed() && getKnown() == R.getKnown();
3001 }
3002
3003 /// "Clamp" this state with \p R. The result is subtype dependent but it is
3004 /// intended that only information assumed in both states will be assumed in
3005 /// this one afterwards.
3007 // NOTE: `^=` operator seems like `intersect` but in this case, we need to
3008 // take `union`.
3009 unionAssumed(R);
3010 return *this;
3011 }
3012
3014 // NOTE: `&=` operator seems like `intersect` but in this case, we need to
3015 // take `union`.
3016 Known = Known.unionWith(R.getKnown());
3017 Assumed = Assumed.unionWith(R.getAssumed());
3018 return *this;
3019 }
3020};
3021
3022/// Simple state for a set.
3023///
3024/// This represents a state containing a set of values. The interface supports
3025/// modelling sets that contain all possible elements. The state's internal
3026/// value is modified using union or intersection operations.
3027template <typename BaseTy> struct SetState : public AbstractState {
3028 /// A wrapper around a set that has semantics for handling unions and
3029 /// intersections with a "universal" set that contains all elements.
3031 /// Creates a universal set with no concrete elements or an empty set.
3032 SetContents(bool Universal) : Universal(Universal) {}
3033
3034 /// Creates a non-universal set with concrete values.
3035 SetContents(const DenseSet<BaseTy> &Assumptions)
3036 : Universal(false), Set(Assumptions) {}
3037
3038 SetContents(bool Universal, const DenseSet<BaseTy> &Assumptions)
3039 : Universal(Universal), Set(Assumptions) {}
3040
3041 const DenseSet<BaseTy> &getSet() const { return Set; }
3042
3043 bool isUniversal() const { return Universal; }
3044
3045 bool empty() const { return Set.empty() && !Universal; }
3046
3047 /// Finds A := A ^ B where A or B could be the "Universal" set which
3048 /// contains every possible attribute. Returns true if changes were made.
3050 bool IsUniversal = Universal;
3051 unsigned Size = Set.size();
3052
3053 // A := A ^ U = A
3054 if (RHS.isUniversal())
3055 return false;
3056
3057 // A := U ^ B = B
3058 if (Universal)
3059 Set = RHS.getSet();
3060 else
3061 set_intersect(Set, RHS.getSet());
3062
3063 Universal &= RHS.isUniversal();
3064 return IsUniversal != Universal || Size != Set.size();
3065 }
3066
3067 /// Finds A := A u B where A or B could be the "Universal" set which
3068 /// contains every possible attribute. returns true if changes were made.
3069 bool getUnion(const SetContents &RHS) {
3070 bool IsUniversal = Universal;
3071 unsigned Size = Set.size();
3072
3073 // A := A u U = U = U u B
3074 if (!RHS.isUniversal() && !Universal)
3075 set_union(Set, RHS.getSet());
3076
3077 Universal |= RHS.isUniversal();
3078 return IsUniversal != Universal || Size != Set.size();
3079 }
3080
3081 private:
3082 /// Indicates if this set is "universal", containing every possible element.
3083 bool Universal;
3084
3085 /// The set of currently active assumptions.
3086 DenseSet<BaseTy> Set;
3087 };
3088
3089 SetState() : Known(false), Assumed(true), IsAtFixedpoint(false) {}
3090
3091 /// Initializes the known state with an initial set and initializes the
3092 /// assumed state as universal.
3094 : Known(Known), Assumed(true), IsAtFixedpoint(false) {}
3095
3096 /// See AbstractState::isValidState()
3097 bool isValidState() const override { return !Assumed.empty(); }
3098
3099 /// See AbstractState::isAtFixpoint()
3100 bool isAtFixpoint() const override { return IsAtFixedpoint; }
3101
3102 /// See AbstractState::indicateOptimisticFixpoint(...)
3104 IsAtFixedpoint = true;
3105 Known = Assumed;
3107 }
3108
3109 /// See AbstractState::indicatePessimisticFixpoint(...)
3111 IsAtFixedpoint = true;
3112 Assumed = Known;
3113 return ChangeStatus::CHANGED;
3114 }
3115
3116 /// Return the known state encoding.
3117 const SetContents &getKnown() const { return Known; }
3118
3119 /// Return the assumed state encoding.
3120 const SetContents &getAssumed() const { return Assumed; }
3121
3122 /// Returns if the set state contains the element.
3123 bool setContains(const BaseTy &Elem) const {
3124 return Assumed.getSet().contains(Elem) || Known.getSet().contains(Elem);
3125 }
3126
3127 /// Performs the set intersection between this set and \p RHS. Returns true if
3128 /// changes were made.
3130 bool IsUniversal = Assumed.isUniversal();
3131 unsigned SizeBefore = Assumed.getSet().size();
3132
3133 // Get intersection and make sure that the known set is still a proper
3134 // subset of the assumed set. A := K u (A ^ R).
3135 Assumed.getIntersection(RHS);
3136 Assumed.getUnion(Known);
3137
3138 return SizeBefore != Assumed.getSet().size() ||
3139 IsUniversal != Assumed.isUniversal();
3140 }
3141
3142 /// Performs the set union between this set and \p RHS. Returns true if
3143 /// changes were made.
3144 bool getUnion(const SetContents &RHS) { return Assumed.getUnion(RHS); }
3145
3146private:
3147 /// The set of values known for this state.
3148 SetContents Known;
3149
3150 /// The set of assumed values for this state.
3151 SetContents Assumed;
3152
3153 bool IsAtFixedpoint;
3154};
3155
3156/// Helper to tie a abstract state implementation to an abstract attribute.
3157template <typename StateTy, typename BaseType, class... Ts>
3158struct StateWrapper : public BaseType, public StateTy {
3159 /// Provide static access to the type of the state.
3161
3162 StateWrapper(const IRPosition &IRP, Ts... Args)
3163 : BaseType(IRP), StateTy(Args...) {}
3164
3165 /// See AbstractAttribute::getState(...).
3166 StateType &getState() override { return *this; }
3167
3168 /// See AbstractAttribute::getState(...).
3169 const StateType &getState() const override { return *this; }
3170};
3171
3172/// Helper class that provides common functionality to manifest IR attributes.
3173template <Attribute::AttrKind AK, typename BaseType, typename AAType>
3174struct IRAttribute : public BaseType {
3175 IRAttribute(const IRPosition &IRP) : BaseType(IRP) {}
3176
3177 /// Most boolean IRAttribute AAs don't do anything non-trivial
3178 /// in their initializers while non-boolean ones often do. Subclasses can
3179 /// change this.
3181
3182 /// Compile time access to the IR attribute kind.
3184
3185 /// Return true if the IR attribute(s) associated with this AA are implied for
3186 /// an undef value.
3187 static bool isImpliedByUndef() { return true; }
3188
3189 /// Return true if the IR attribute(s) associated with this AA are implied for
3190 /// an poison value.
3191 static bool isImpliedByPoison() { return true; }
3192
3193 static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3194 Attribute::AttrKind ImpliedAttributeKind = AK,
3195 bool IgnoreSubsumingPositions = false) {
3196 if (AAType::isImpliedByUndef() && isa<UndefValue>(IRP.getAssociatedValue()))
3197 return true;
3198 if (AAType::isImpliedByPoison() &&
3200 return true;
3201 return A.hasAttr(IRP, {ImpliedAttributeKind}, IgnoreSubsumingPositions,
3202 ImpliedAttributeKind);
3203 }
3204
3205 /// See AbstractAttribute::manifest(...).
3207 if (isa<UndefValue>(this->getIRPosition().getAssociatedValue()))
3209 SmallVector<Attribute, 4> DeducedAttrs;
3210 getDeducedAttributes(A, this->getAnchorValue().getContext(), DeducedAttrs);
3211 if (DeducedAttrs.empty())
3213 return A.manifestAttrs(this->getIRPosition(), DeducedAttrs);
3214 }
3215
3216 /// Return the kind that identifies the abstract attribute implementation.
3217 Attribute::AttrKind getAttrKind() const { return AK; }
3218
3219 /// Return the deduced attributes in \p Attrs.
3221 SmallVectorImpl<Attribute> &Attrs) const {
3222 Attrs.emplace_back(Attribute::get(Ctx, getAttrKind()));
3223 }
3224};
3225
3226/// Base struct for all "concrete attribute" deductions.
3227///
3228/// The abstract attribute is a minimal interface that allows the Attributor to
3229/// orchestrate the abstract/fixpoint analysis. The design allows to hide away
3230/// implementation choices made for the subclasses but also to structure their
3231/// implementation and simplify the use of other abstract attributes in-flight.
3232///
3233/// To allow easy creation of new attributes, most methods have default
3234/// implementations. The ones that do not are generally straight forward, except
3235/// `AbstractAttribute::updateImpl` which is the location of most reasoning
3236/// associated with the abstract attribute. The update is invoked by the
3237/// Attributor in case the situation used to justify the current optimistic
3238/// state might have changed. The Attributor determines this automatically
3239/// by monitoring the `Attributor::getAAFor` calls made by abstract attributes.
3240///
3241/// The `updateImpl` method should inspect the IR and other abstract attributes
3242/// in-flight to justify the best possible (=optimistic) state. The actual
3243/// implementation is, similar to the underlying abstract state encoding, not
3244/// exposed. In the most common case, the `updateImpl` will go through a list of
3245/// reasons why its optimistic state is valid given the current information. If
3246/// any combination of them holds and is sufficient to justify the current
3247/// optimistic state, the method shall return UNCHAGED. If not, the optimistic
3248/// state is adjusted to the situation and the method shall return CHANGED.
3249///
3250/// If the manifestation of the "concrete attribute" deduced by the subclass
3251/// differs from the "default" behavior, which is a (set of) LLVM-IR
3252/// attribute(s) for an argument, call site argument, function return value, or
3253/// function, the `AbstractAttribute::manifest` method should be overloaded.
3254///
3255/// NOTE: If the state obtained via getState() is INVALID, thus if
3256/// AbstractAttribute::getState().isValidState() returns false, no
3257/// information provided by the methods of this class should be used.
3258/// NOTE: The Attributor currently has certain limitations to what we can do.
3259/// As a general rule of thumb, "concrete" abstract attributes should *for
3260/// now* only perform "backward" information propagation. That means
3261/// optimistic information obtained through abstract attributes should
3262/// only be used at positions that precede the origin of the information
3263/// with regards to the program flow. More practically, information can
3264/// *now* be propagated from instructions to their enclosing function, but
3265/// *not* from call sites to the called function. The mechanisms to allow
3266/// both directions will be added in the future.
3267/// NOTE: The mechanics of adding a new "concrete" abstract attribute are
3268/// described in the file comment.
3271
3273
3274 /// Virtual destructor.
3275 ~AbstractAttribute() override = default;
3276
3277 /// Compile time access to the IR attribute kind.
3279
3280 /// This function is used to identify if an \p DGN is of type
3281 /// AbstractAttribute so that the dyn_cast and cast can use such information
3282 /// to cast an AADepGraphNode to an AbstractAttribute.
3283 ///
3284 /// We eagerly return true here because all AADepGraphNodes except for the
3285 /// Synthethis Node are of type AbstractAttribute
3286 static bool classof(const AADepGraphNode *DGN) { return true; }
3287
3288 /// Return false if this AA does anything non-trivial (hence not done by
3289 /// default) in its initializer.
3290 static bool hasTrivialInitializer() { return false; }
3291
3292 /// Return true if this AA requires a "callee" (or an associted function) for
3293 /// a call site positon. Default is optimistic to minimize AAs.
3294 static bool requiresCalleeForCallBase() { return false; }
3295
3296 /// Return true if this AA requires non-asm "callee" for a call site positon.
3297 static bool requiresNonAsmForCallBase() { return true; }
3298
3299 /// Return true if this AA requires all callees for an argument or function
3300 /// positon.
3301 static bool requiresCallersForArgOrFunction() { return false; }
3302
3303 /// Return false if an AA should not be created for \p IRP.
3305 return true;
3306 }
3307
3308 /// Return false if an AA should not be updated for \p IRP.
3310 Function *AssociatedFn = IRP.getAssociatedFunction();
3311 bool IsFnInterface = IRP.isFnInterfaceKind();
3312 assert((!IsFnInterface || AssociatedFn) &&
3313 "Function interface without a function?");
3314
3315 // TODO: Not all attributes require an exact definition. Find a way to
3316 // enable deduction for some but not all attributes in case the
3317 // definition might be changed at runtime, see also
3318 // http://lists.llvm.org/pipermail/llvm-dev/2018-February/121275.html.
3319 // TODO: We could always determine abstract attributes and if sufficient
3320 // information was found we could duplicate the functions that do not
3321 // have an exact definition.
3322 return !IsFnInterface || A.isFunctionIPOAmendable(*AssociatedFn);
3323 }
3324
3325 /// Initialize the state with the information in the Attributor \p A.
3326 ///
3327 /// This function is called by the Attributor once all abstract attributes
3328 /// have been identified. It can and shall be used for task like:
3329 /// - identify existing knowledge in the IR and use it for the "known state"
3330 /// - perform any work that is not going to change over time, e.g., determine
3331 /// a subset of the IR, or attributes in-flight, that have to be looked at
3332 /// in the `updateImpl` method.
3333 virtual void initialize(Attributor &A) {}
3334
3335 /// A query AA is always scheduled as long as we do updates because it does
3336 /// lazy computation that cannot be determined to be done from the outside.
3337 /// However, while query AAs will not be fixed if they do not have outstanding
3338 /// dependences, we will only schedule them like other AAs. If a query AA that
3339 /// received a new query it needs to request an update via
3340 /// `Attributor::requestUpdateForAA`.
3341 virtual bool isQueryAA() const { return false; }
3342
3343 /// Return the internal abstract state for inspection.
3344 virtual StateType &getState() = 0;
3345 virtual const StateType &getState() const = 0;
3346
3347 /// Return an IR position, see struct IRPosition.
3348 const IRPosition &getIRPosition() const { return *this; };
3349 IRPosition &getIRPosition() { return *this; };
3350
3351 /// Helper functions, for debug purposes only.
3352 ///{
3353 void print(raw_ostream &OS) const { print(nullptr, OS); }
3354 void print(Attributor *, raw_ostream &OS) const override;
3355 virtual void printWithDeps(raw_ostream &OS) const;
3356 void dump() const { this->print(dbgs()); }
3357
3358 /// This function should return the "summarized" assumed state as string.
3359 virtual const std::string getAsStr(Attributor *A) const = 0;
3360
3361 /// This function should return the name of the AbstractAttribute
3362 virtual StringRef getName() const = 0;
3363
3364 /// This function should return the address of the ID of the AbstractAttribute
3365 virtual const char *getIdAddr() const = 0;
3366 ///}
3367
3368 /// Allow the Attributor access to the protected methods.
3369 friend struct Attributor;
3370
3371protected:
3372 /// Hook for the Attributor to trigger an update of the internal state.
3373 ///
3374 /// If this attribute is already fixed, this method will return UNCHANGED,
3375 /// otherwise it delegates to `AbstractAttribute::updateImpl`.
3376 ///
3377 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
3379
3380 /// Hook for the Attributor to trigger the manifestation of the information
3381 /// represented by the abstract attribute in the LLVM-IR.
3382 ///
3383 /// \Return CHANGED if the IR was altered, otherwise UNCHANGED.
3387
3388 /// Hook to enable custom statistic tracking, called after manifest that
3389 /// resulted in a change if statistics are enabled.
3390 ///
3391 /// We require subclasses to provide an implementation so we remember to
3392 /// add statistics for them.
3393 virtual void trackStatistics() const = 0;
3394
3395 /// The actual update/transfer function which has to be implemented by the
3396 /// derived classes.
3397 ///
3398 /// If it is called, the environment has changed and we have to determine if
3399 /// the current information is still valid or adjust it otherwise.
3400 ///
3401 /// \Return CHANGED if the internal state changed, otherwise UNCHANGED.
3403};
3404
3405/// Forward declarations of output streams for debug purposes.
3406///
3407///{
3413template <typename base_ty, base_ty BestState, base_ty WorstState>
3417 return OS << "(" << S.getKnown() << "-" << S.getAssumed() << ")"
3418 << static_cast<const AbstractState &>(S);
3419}
3420LLVM_ABI raw_ostream &operator<<(raw_ostream &OS,
3421 const IntegerRangeState &State);
3422///}
3423
3432
3433/// A more lightweight version of the Attributor which only runs attribute
3434/// inference but no simplifications.
3435struct AttributorLightPass : public OptionalPassInfoMixin<AttributorLightPass> {
3437};
3438
3439/// A more lightweight version of the Attributor which only runs attribute
3440/// inference but no simplifications.
3442 : public OptionalPassInfoMixin<AttributorLightCGSCCPass> {
3445 CGSCCUpdateResult &UR);
3446};
3447
3448/// Helper function to clamp a state \p S of type \p StateType with the
3449/// information in \p R and indicate/return if \p S did change (as-in update is
3450/// required to be run again).
3451template <typename StateType>
3452ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R) {
3453 auto Assumed = S.getAssumed();
3454 S ^= R;
3455 return Assumed == S.getAssumed() ? ChangeStatus::UNCHANGED
3457}
3458
3459/// ----------------------------------------------------------------------------
3460/// Abstract Attribute Classes
3461/// ----------------------------------------------------------------------------
3462
3464 : public IRAttribute<Attribute::NoUnwind,
3465 StateWrapper<BooleanState, AbstractAttribute>,
3466 AANoUnwind> {
3468
3469 /// Returns true if nounwind is assumed.
3470 bool isAssumedNoUnwind() const { return getAssumed(); }
3471
3472 /// Returns true if nounwind is known.
3473 bool isKnownNoUnwind() const { return getKnown(); }
3474
3475 /// Create an abstract attribute view for the position \p IRP.
3477 Attributor &A);
3478
3479 /// See AbstractAttribute::getName()
3480 StringRef getName() const override { return "AANoUnwind"; }
3481
3482 /// See AbstractAttribute::getIdAddr()
3483 const char *getIdAddr() const override { return &ID; }
3484
3485 /// This function should return true if the type of the \p AA is AANoUnwind
3486 static bool classof(const AbstractAttribute *AA) {
3487 return (AA->getIdAddr() == &ID);
3488 }
3489
3490 /// Unique ID (due to the unique address)
3491 LLVM_ABI static const char ID;
3492};
3493
3495 : public IRAttribute<Attribute::NoSync,
3496 StateWrapper<BooleanState, AbstractAttribute>,
3497 AANoSync> {
3499
3500 static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3501 Attribute::AttrKind ImpliedAttributeKind,
3502 bool IgnoreSubsumingPositions = false) {
3503 // Note: This is also run for non-IPO amendable functions.
3504 assert(ImpliedAttributeKind == Attribute::NoSync);
3505 if (A.hasAttr(IRP, {Attribute::NoSync}, IgnoreSubsumingPositions,
3506 Attribute::NoSync))
3507 return true;
3508
3509 // Check for readonly + non-convergent.
3510 // TODO: We should be able to use hasAttr for Attributes, not only
3511 // AttrKinds.
3513 if (!F || F->isConvergent())
3514 return false;
3515
3517 A.getAttrs(IRP, {Attribute::Memory}, Attrs, IgnoreSubsumingPositions);
3518
3520 for (const Attribute &Attr : Attrs)
3521 ME &= Attr.getMemoryEffects();
3522
3523 if (!ME.onlyReadsMemory())
3524 return false;
3525
3526 A.manifestAttrs(IRP, Attribute::get(F->getContext(), Attribute::NoSync));
3527 return true;
3528 }
3529
3530 /// See AbstractAttribute::isValidIRPositionForInit
3532 if (!IRP.isFunctionScope() &&
3534 return false;
3535 return IRAttribute::isValidIRPositionForInit(A, IRP);
3536 }
3537
3538 /// Returns true if "nosync" is assumed.
3539 bool isAssumedNoSync() const { return getAssumed(); }
3540
3541 /// Returns true if "nosync" is known.
3542 bool isKnownNoSync() const { return getKnown(); }
3543
3544 /// Helper function used to determine whether an instruction is non-relaxed
3545 /// atomic. In other words, if an atomic instruction does not have unordered
3546 /// or monotonic ordering
3547 LLVM_ABI static bool isNonRelaxedAtomic(const Instruction *I);
3548
3549 /// Helper function to determine if \p CB is an aligned (GPU) barrier. Aligned
3550 /// barriers have to be executed by all threads. The flag \p ExecutedAligned
3551 /// indicates if the call is executed by all threads in a (thread) block in an
3552 /// aligned way. If that is the case, non-aligned barriers are effectively
3553 /// aligned barriers.
3554 LLVM_ABI static bool isAlignedBarrier(const CallBase &CB,
3555 bool ExecutedAligned);
3556
3557 /// Create an abstract attribute view for the position \p IRP.
3559 Attributor &A);
3560
3561 /// See AbstractAttribute::getName()
3562 StringRef getName() const override { return "AANoSync"; }
3563
3564 /// See AbstractAttribute::getIdAddr()
3565 const char *getIdAddr() const override { return &ID; }
3566
3567 /// This function should return true if the type of the \p AA is AANoSync
3568 static bool classof(const AbstractAttribute *AA) {
3569 return (AA->getIdAddr() == &ID);
3570 }
3571
3572 /// Unique ID (due to the unique address)
3573 LLVM_ABI static const char ID;
3574};
3575
3576/// An abstract interface for all nonnull attributes.
3578 : public IRAttribute<Attribute::MustProgress,
3579 StateWrapper<BooleanState, AbstractAttribute>,
3580 AAMustProgress> {
3582
3583 static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3584 Attribute::AttrKind ImpliedAttributeKind,
3585 bool IgnoreSubsumingPositions = false) {
3586 // Note: This is also run for non-IPO amendable functions.
3587 assert(ImpliedAttributeKind == Attribute::MustProgress);
3588 return A.hasAttr(IRP, {Attribute::MustProgress, Attribute::WillReturn},
3589 IgnoreSubsumingPositions, Attribute::MustProgress);
3590 }
3591
3592 /// Return true if we assume that the underlying value is nonnull.
3593 bool isAssumedMustProgress() const { return getAssumed(); }
3594
3595 /// Return true if we know that underlying value is nonnull.
3596 bool isKnownMustProgress() const { return getKnown(); }
3597
3598 /// Create an abstract attribute view for the position \p IRP.
3600 Attributor &A);
3601
3602 /// See AbstractAttribute::getName()
3603 StringRef getName() const override { return "AAMustProgress"; }
3604
3605 /// See AbstractAttribute::getIdAddr()
3606 const char *getIdAddr() const override { return &ID; }
3607
3608 /// This function should return true if the type of the \p AA is
3609 /// AAMustProgress
3610 static bool classof(const AbstractAttribute *AA) {
3611 return (AA->getIdAddr() == &ID);
3612 }
3613
3614 /// Unique ID (due to the unique address)
3615 LLVM_ABI static const char ID;
3616};
3617
3618/// An abstract interface for all nonnull attributes.
3620 : public IRAttribute<Attribute::NonNull,
3621 StateWrapper<BooleanState, AbstractAttribute>,
3622 AANonNull> {
3624
3625 /// See AbstractAttribute::hasTrivialInitializer.
3626 static bool hasTrivialInitializer() { return false; }
3627
3628 /// See IRAttribute::isImpliedByUndef.
3629 /// Undef is not necessarily nonnull as nonnull + noundef would cause poison.
3630 /// Poison implies nonnull though.
3631 static bool isImpliedByUndef() { return false; }
3632
3633 /// See AbstractAttribute::isValidIRPositionForInit
3636 return false;
3637 return IRAttribute::isValidIRPositionForInit(A, IRP);
3638 }
3639
3640 /// See AbstractAttribute::isImpliedByIR(...).
3641 LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3642 Attribute::AttrKind ImpliedAttributeKind,
3643 bool IgnoreSubsumingPositions = false);
3644
3645 /// Return true if we assume that the underlying value is nonnull.
3646 bool isAssumedNonNull() const { return getAssumed(); }
3647
3648 /// Return true if we know that underlying value is nonnull.
3649 bool isKnownNonNull() const { return getKnown(); }
3650
3651 /// Create an abstract attribute view for the position \p IRP.
3653 Attributor &A);
3654
3655 /// See AbstractAttribute::getName()
3656 StringRef getName() const override { return "AANonNull"; }
3657
3658 /// See AbstractAttribute::getIdAddr()
3659 const char *getIdAddr() const override { return &ID; }
3660
3661 /// This function should return true if the type of the \p AA is AANonNull
3662 static bool classof(const AbstractAttribute *AA) {
3663 return (AA->getIdAddr() == &ID);
3664 }
3665
3666 /// Unique ID (due to the unique address)
3667 LLVM_ABI static const char ID;
3668};
3669
3670/// An abstract attribute for norecurse.
3672 : public IRAttribute<Attribute::NoRecurse,
3673 StateWrapper<BooleanState, AbstractAttribute>,
3674 AANoRecurse> {
3676
3677 /// Return true if "norecurse" is assumed.
3678 bool isAssumedNoRecurse() const { return getAssumed(); }
3679
3680 /// Return true if "norecurse" is known.
3681 bool isKnownNoRecurse() const { return getKnown(); }
3682
3683 /// Create an abstract attribute view for the position \p IRP.
3685 Attributor &A);
3686
3687 /// See AbstractAttribute::getName()
3688 StringRef getName() const override { return "AANoRecurse"; }
3689
3690 /// See AbstractAttribute::getIdAddr()
3691 const char *getIdAddr() const override { return &ID; }
3692
3693 /// This function should return true if the type of the \p AA is AANoRecurse
3694 static bool classof(const AbstractAttribute *AA) {
3695 return (AA->getIdAddr() == &ID);
3696 }
3697
3698 /// Unique ID (due to the unique address)
3699 LLVM_ABI static const char ID;
3700};
3701
3702/// An abstract attribute for willreturn.
3704 : public IRAttribute<Attribute::WillReturn,
3705 StateWrapper<BooleanState, AbstractAttribute>,
3706 AAWillReturn> {
3708
3709 static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3710 Attribute::AttrKind ImpliedAttributeKind,
3711 bool IgnoreSubsumingPositions = false) {
3712 // Note: This is also run for non-IPO amendable functions.
3713 assert(ImpliedAttributeKind == Attribute::WillReturn);
3714 if (IRAttribute::isImpliedByIR(A, IRP, ImpliedAttributeKind,
3715 IgnoreSubsumingPositions))
3716 return true;
3718 return false;
3719 A.manifestAttrs(IRP, Attribute::get(IRP.getAnchorValue().getContext(),
3720 Attribute::WillReturn));
3721 return true;
3722 }
3723
3724 /// Check for `mustprogress` and `readonly` as they imply `willreturn`.
3726 const IRPosition &IRP) {
3727 // Check for `mustprogress` in the scope and the associated function which
3728 // might be different if this is a call site.
3729 if (!A.hasAttr(IRP, {Attribute::MustProgress}))
3730 return false;
3731
3733 A.getAttrs(IRP, {Attribute::Memory}, Attrs,
3734 /* IgnoreSubsumingPositions */ false);
3735
3737 for (const Attribute &Attr : Attrs)
3738 ME &= Attr.getMemoryEffects();
3739 return ME.onlyReadsMemory();
3740 }
3741
3742 /// Return true if "willreturn" is assumed.
3743 bool isAssumedWillReturn() const { return getAssumed(); }
3744
3745 /// Return true if "willreturn" is known.
3746 bool isKnownWillReturn() const { return getKnown(); }
3747
3748 /// Create an abstract attribute view for the position \p IRP.
3750 Attributor &A);
3751
3752 /// See AbstractAttribute::getName()
3753 StringRef getName() const override { return "AAWillReturn"; }
3754
3755 /// See AbstractAttribute::getIdAddr()
3756 const char *getIdAddr() const override { return &ID; }
3757
3758 /// This function should return true if the type of the \p AA is AAWillReturn
3759 static bool classof(const AbstractAttribute *AA) {
3760 return (AA->getIdAddr() == &ID);
3761 }
3762
3763 /// Unique ID (due to the unique address)
3764 LLVM_ABI static const char ID;
3765};
3766
3767/// An abstract attribute for undefined behavior.
3769 : public StateWrapper<BooleanState, AbstractAttribute> {
3772
3773 /// Return true if "undefined behavior" is assumed for a specific instruction.
3774 virtual bool isAssumedToCauseUB(Instruction *I) const = 0;
3775
3776 /// Return true if "undefined behavior" is known for a specific instruction.
3777 virtual bool isKnownToCauseUB(Instruction *I) const = 0;
3778
3779 /// Create an abstract attribute view for the position \p IRP.
3781 Attributor &A);
3782
3783 /// See AbstractAttribute::getName()
3784 StringRef getName() const override { return "AAUndefinedBehavior"; }
3785
3786 /// See AbstractAttribute::getIdAddr()
3787 const char *getIdAddr() const override { return &ID; }
3788
3789 /// This function should return true if the type of the \p AA is
3790 /// AAUndefineBehavior
3791 static bool classof(const AbstractAttribute *AA) {
3792 return (AA->getIdAddr() == &ID);
3793 }
3794
3795 /// Unique ID (due to the unique address)
3796 LLVM_ABI static const char ID;
3797};
3798
3799/// An abstract interface to determine reachability of point A to B.
3801 : public StateWrapper<BooleanState, AbstractAttribute> {
3804
3805 /// Returns true if 'From' instruction is assumed to reach, 'To' instruction.
3806 /// Users should provide two positions they are interested in, and the class
3807 /// determines (and caches) reachability.
3809 Attributor &A, const Instruction &From, const Instruction &To,
3810 const AA::InstExclusionSetTy *ExclusionSet = nullptr) const = 0;
3811
3812 /// Create an abstract attribute view for the position \p IRP.
3815
3816 /// See AbstractAttribute::getName()
3817 StringRef getName() const override { return "AAIntraFnReachability"; }
3818
3819 /// See AbstractAttribute::getIdAddr()
3820 const char *getIdAddr() const override { return &ID; }
3821
3822 /// This function should return true if the type of the \p AA is
3823 /// AAIntraFnReachability
3824 static bool classof(const AbstractAttribute *AA) {
3825 return (AA->getIdAddr() == &ID);
3826 }
3827
3828 /// Unique ID (due to the unique address)
3829 LLVM_ABI static const char ID;
3830};
3831
3832/// An abstract interface for all noalias attributes.
3834 : public IRAttribute<Attribute::NoAlias,
3835 StateWrapper<BooleanState, AbstractAttribute>,
3836 AANoAlias> {
3838
3839 /// See AbstractAttribute::isValidIRPositionForInit
3841 if (!IRP.getAssociatedType()->isPointerTy())
3842 return false;
3843 return IRAttribute::isValidIRPositionForInit(A, IRP);
3844 }
3845
3846 /// See IRAttribute::isImpliedByIR
3847 LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3848 Attribute::AttrKind ImpliedAttributeKind,
3849 bool IgnoreSubsumingPositions = false);
3850
3851 /// See AbstractAttribute::requiresCallersForArgOrFunction
3852 static bool requiresCallersForArgOrFunction() { return true; }
3853
3854 /// Return true if we assume that the underlying value is alias.
3855 bool isAssumedNoAlias() const { return getAssumed(); }
3856
3857 /// Return true if we know that underlying value is noalias.
3858 bool isKnownNoAlias() const { return getKnown(); }
3859
3860 /// Create an abstract attribute view for the position \p IRP.
3862 Attributor &A);
3863
3864 /// See AbstractAttribute::getName()
3865 StringRef getName() const override { return "AANoAlias"; }
3866
3867 /// See AbstractAttribute::getIdAddr()
3868 const char *getIdAddr() const override { return &ID; }
3869
3870 /// This function should return true if the type of the \p AA is AANoAlias
3871 static bool classof(const AbstractAttribute *AA) {
3872 return (AA->getIdAddr() == &ID);
3873 }
3874
3875 /// Unique ID (due to the unique address)
3876 LLVM_ABI static const char ID;
3877};
3878
3879/// An AbstractAttribute for nofree.
3881 : public IRAttribute<Attribute::NoFree,
3882 StateWrapper<BooleanState, AbstractAttribute>,
3883 AANoFree> {
3885
3886 /// See IRAttribute::isImpliedByIR
3887 static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
3888 Attribute::AttrKind ImpliedAttributeKind,
3889 bool IgnoreSubsumingPositions = false) {
3890 // Note: This is also run for non-IPO amendable functions.
3891 assert(ImpliedAttributeKind == Attribute::NoFree);
3892 return A.hasAttr(
3893 IRP, {Attribute::ReadNone, Attribute::ReadOnly, Attribute::NoFree},
3894 IgnoreSubsumingPositions, Attribute::NoFree);
3895 }
3896
3897 /// See AbstractAttribute::isValidIRPositionForInit
3899 if (!IRP.isFunctionScope() &&
3901 return false;
3902 return IRAttribute::isValidIRPositionForInit(A, IRP);
3903 }
3904
3905 /// Return true if "nofree" is assumed.
3906 bool isAssumedNoFree() const { return getAssumed(); }
3907
3908 /// Return true if "nofree" is known.
3909 bool isKnownNoFree() const { return getKnown(); }
3910
3911 /// Create an abstract attribute view for the position \p IRP.
3913 Attributor &A);
3914
3915 /// See AbstractAttribute::getName()
3916 StringRef getName() const override { return "AANoFree"; }
3917
3918 /// See AbstractAttribute::getIdAddr()
3919 const char *getIdAddr() const override { return &ID; }
3920
3921 /// This function should return true if the type of the \p AA is AANoFree
3922 static bool classof(const AbstractAttribute *AA) {
3923 return (AA->getIdAddr() == &ID);
3924 }
3925
3926 /// Unique ID (due to the unique address)
3927 LLVM_ABI static const char ID;
3928};
3929
3930/// An AbstractAttribute for noreturn.
3932 : public IRAttribute<Attribute::NoReturn,
3933 StateWrapper<BooleanState, AbstractAttribute>,
3934 AANoReturn> {
3936
3937 /// Return true if the underlying object is assumed to never return.
3938 bool isAssumedNoReturn() const { return getAssumed(); }
3939
3940 /// Return true if the underlying object is known to never return.
3941 bool isKnownNoReturn() const { return getKnown(); }
3942
3943 /// Create an abstract attribute view for the position \p IRP.
3945 Attributor &A);
3946
3947 /// See AbstractAttribute::getName()
3948 StringRef getName() const override { return "AANoReturn"; }
3949
3950 /// See AbstractAttribute::getIdAddr()
3951 const char *getIdAddr() const override { return &ID; }
3952
3953 /// This function should return true if the type of the \p AA is AANoReturn
3954 static bool classof(const AbstractAttribute *AA) {
3955 return (AA->getIdAddr() == &ID);
3956 }
3957
3958 /// Unique ID (due to the unique address)
3959 LLVM_ABI static const char ID;
3960};
3961
3962/// An abstract interface for liveness abstract attribute.
3964 : public StateWrapper<BitIntegerState<uint8_t, 3, 0>, AbstractAttribute> {
3966 AAIsDead(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
3967
3968 /// See AbstractAttribute::isValidIRPositionForInit
3971 return isa<Function>(IRP.getAnchorValue()) &&
3972 !cast<Function>(IRP.getAnchorValue()).isDeclaration();
3973 return true;
3974 }
3975
3976 /// State encoding bits. A set bit in the state means the property holds.
3977 enum {
3980
3982 };
3983 static_assert(IS_DEAD == getBestState(), "Unexpected BEST_STATE value");
3984
3985protected:
3986 /// The query functions are protected such that other attributes need to go
3987 /// through the Attributor interfaces: `Attributor::isAssumedDead(...)`
3988
3989 /// Returns true if the underlying value is assumed dead.
3990 virtual bool isAssumedDead() const = 0;
3991
3992 /// Returns true if the underlying value is known dead.
3993 virtual bool isKnownDead() const = 0;
3994
3995 /// Returns true if \p BB is known dead.
3996 virtual bool isKnownDead(const BasicBlock *BB) const = 0;
3997
3998 /// Returns true if \p I is assumed dead.
3999 virtual bool isAssumedDead(const Instruction *I) const = 0;
4000
4001 /// Returns true if \p I is known dead.
4002 virtual bool isKnownDead(const Instruction *I) const = 0;
4003
4004 /// Return true if the underlying value is a store that is known to be
4005 /// removable. This is different from dead stores as the removable store
4006 /// can have an effect on live values, especially loads, but that effect
4007 /// is propagated which allows us to remove the store in turn.
4008 virtual bool isRemovableStore() const { return false; }
4009
4010public:
4011 /// Create an abstract attribute view for the position \p IRP.
4013 Attributor &A);
4014
4015 /// Determine if \p F might catch asynchronous exceptions.
4017 return F.hasPersonalityFn() && !canSimplifyInvokeNoUnwind(&F);
4018 }
4019
4020 /// Returns true if \p BB is assumed dead.
4021 virtual bool isAssumedDead(const BasicBlock *BB) const = 0;
4022
4023 /// Return if the edge from \p From BB to \p To BB is assumed dead.
4024 /// This is specifically useful in AAReachability.
4025 virtual bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const {
4026 return false;
4027 }
4028
4029 /// See AbstractAttribute::getName()
4030 StringRef getName() const override { return "AAIsDead"; }
4031
4032 /// See AbstractAttribute::getIdAddr()
4033 const char *getIdAddr() const override { return &ID; }
4034
4035 /// This function should return true if the type of the \p AA is AAIsDead
4036 static bool classof(const AbstractAttribute *AA) {
4037 return (AA->getIdAddr() == &ID);
4038 }
4039
4040 /// Unique ID (due to the unique address)
4041 LLVM_ABI static const char ID;
4042
4043 friend struct Attributor;
4044};
4045
4046/// State for dereferenceable attribute
4048
4049 static DerefState getBestState() { return DerefState(); }
4050 static DerefState getBestState(const DerefState &) { return getBestState(); }
4051
4052 /// Return the worst possible representable state.
4054 DerefState DS;
4055 DS.indicatePessimisticFixpoint();
4056 return DS;
4057 }
4059 return getWorstState();
4060 }
4061
4062 /// State representing for dereferenceable bytes.
4064
4065 /// Map representing for accessed memory offsets and sizes.
4066 /// A key is Offset and a value is size.
4067 /// If there is a load/store instruction something like,
4068 /// p[offset] = v;
4069 /// (offset, sizeof(v)) will be inserted to this map.
4070 /// std::map is used because we want to iterate keys in ascending order.
4071 std::map<int64_t, uint64_t> AccessedBytesMap;
4072
4073 /// Helper function to calculate dereferenceable bytes from current known
4074 /// bytes and accessed bytes.
4075 ///
4076 /// int f(int *A){
4077 /// *A = 0;
4078 /// *(A+2) = 2;
4079 /// *(A+1) = 1;
4080 /// *(A+10) = 10;
4081 /// }
4082 /// ```
4083 /// In that case, AccessedBytesMap is `{0:4, 4:4, 8:4, 40:4}`.
4084 /// AccessedBytesMap is std::map so it is iterated in accending order on
4085 /// key(Offset). So KnownBytes will be updated like this:
4086 ///
4087 /// |Access | KnownBytes
4088 /// |(0, 4)| 0 -> 4
4089 /// |(4, 4)| 4 -> 8
4090 /// |(8, 4)| 8 -> 12
4091 /// |(40, 4) | 12 (break)
4092 void computeKnownDerefBytesFromAccessedMap() {
4093 int64_t KnownBytes = DerefBytesState.getKnown();
4094 for (auto &Access : AccessedBytesMap) {
4095 if (KnownBytes < Access.first)
4096 break;
4097 KnownBytes = std::max(KnownBytes, Access.first + (int64_t)Access.second);
4098 }
4099
4100 DerefBytesState.takeKnownMaximum(KnownBytes);
4101 }
4102
4103 /// State representing that whether the value is globaly dereferenceable.
4104 BooleanState GlobalState;
4105
4106 /// See AbstractState::isValidState()
4107 bool isValidState() const override { return DerefBytesState.isValidState(); }
4108
4109 /// See AbstractState::isAtFixpoint()
4110 bool isAtFixpoint() const override {
4111 return !isValidState() ||
4112 (DerefBytesState.isAtFixpoint() && GlobalState.isAtFixpoint());
4113 }
4114
4115 /// See AbstractState::indicateOptimisticFixpoint(...)
4117 DerefBytesState.indicateOptimisticFixpoint();
4118 GlobalState.indicateOptimisticFixpoint();
4120 }
4121
4122 /// See AbstractState::indicatePessimisticFixpoint(...)
4124 DerefBytesState.indicatePessimisticFixpoint();
4125 GlobalState.indicatePessimisticFixpoint();
4126 return ChangeStatus::CHANGED;
4127 }
4128
4129 /// Update known dereferenceable bytes.
4130 void takeKnownDerefBytesMaximum(uint64_t Bytes) {
4131 DerefBytesState.takeKnownMaximum(Bytes);
4132
4133 // Known bytes might increase.
4134 computeKnownDerefBytesFromAccessedMap();
4135 }
4136
4137 /// Update assumed dereferenceable bytes.
4138 void takeAssumedDerefBytesMinimum(uint64_t Bytes) {
4139 DerefBytesState.takeAssumedMinimum(Bytes);
4140 }
4141
4142 /// Add accessed bytes to the map.
4143 void addAccessedBytes(int64_t Offset, uint64_t Size) {
4144 uint64_t &AccessedBytes = AccessedBytesMap[Offset];
4145 AccessedBytes = std::max(AccessedBytes, Size);
4146
4147 // Known bytes might increase.
4148 computeKnownDerefBytesFromAccessedMap();
4149 }
4150
4151 /// Equality for DerefState.
4152 bool operator==(const DerefState &R) const {
4153 return this->DerefBytesState == R.DerefBytesState &&
4154 this->GlobalState == R.GlobalState;
4155 }
4156
4157 /// Inequality for DerefState.
4158 bool operator!=(const DerefState &R) const { return !(*this == R); }
4159
4160 /// See IntegerStateBase::operator^=
4161 DerefState operator^=(const DerefState &R) {
4162 DerefBytesState ^= R.DerefBytesState;
4163 GlobalState ^= R.GlobalState;
4164 return *this;
4165 }
4166
4167 /// See IntegerStateBase::operator+=
4168 DerefState operator+=(const DerefState &R) {
4169 DerefBytesState += R.DerefBytesState;
4170 GlobalState += R.GlobalState;
4171 return *this;
4172 }
4173
4174 /// See IntegerStateBase::operator&=
4175 DerefState operator&=(const DerefState &R) {
4176 DerefBytesState &= R.DerefBytesState;
4177 GlobalState &= R.GlobalState;
4178 return *this;
4179 }
4180
4181 /// See IntegerStateBase::operator|=
4182 DerefState operator|=(const DerefState &R) {
4183 DerefBytesState |= R.DerefBytesState;
4184 GlobalState |= R.GlobalState;
4185 return *this;
4186 }
4187};
4188
4189/// An abstract interface for all dereferenceable attribute.
4191 : public IRAttribute<Attribute::Dereferenceable,
4192 StateWrapper<DerefState, AbstractAttribute>,
4193 AADereferenceable> {
4195
4196 /// See AbstractAttribute::isValidIRPositionForInit
4198 if (!IRP.getAssociatedType()->isPointerTy())
4199 return false;
4200 return IRAttribute::isValidIRPositionForInit(A, IRP);
4201 }
4202
4203 /// Return true if we assume that underlying value is
4204 /// dereferenceable(_or_null) globally.
4205 bool isAssumedGlobal() const { return GlobalState.getAssumed(); }
4206
4207 /// Return true if we know that underlying value is
4208 /// dereferenceable(_or_null) globally.
4209 bool isKnownGlobal() const { return GlobalState.getKnown(); }
4210
4211 /// Return assumed dereferenceable bytes.
4213 return DerefBytesState.getAssumed();
4214 }
4215
4216 /// Return known dereferenceable bytes.
4218 return DerefBytesState.getKnown();
4219 }
4220
4221 /// Create an abstract attribute view for the position \p IRP.
4223 Attributor &A);
4224
4225 /// See AbstractAttribute::getName()
4226 StringRef getName() const override { return "AADereferenceable"; }
4227
4228 /// See AbstractAttribute::getIdAddr()
4229 const char *getIdAddr() const override { return &ID; }
4230
4231 /// This function should return true if the type of the \p AA is
4232 /// AADereferenceable
4233 static bool classof(const AbstractAttribute *AA) {
4234 return (AA->getIdAddr() == &ID);
4235 }
4236
4237 /// Unique ID (due to the unique address)
4238 LLVM_ABI static const char ID;
4239};
4240
4243/// An abstract interface for all align attributes.
4245 : public IRAttribute<Attribute::Alignment,
4246 StateWrapper<AAAlignmentStateType, AbstractAttribute>,
4247 AAAlign> {
4249
4250 /// See AbstractAttribute::isValidIRPositionForInit
4253 return false;
4254 return IRAttribute::isValidIRPositionForInit(A, IRP);
4255 }
4256
4257 /// Return assumed alignment.
4258 Align getAssumedAlign() const { return Align(getAssumed()); }
4259
4260 /// Return known alignment.
4261 Align getKnownAlign() const { return Align(getKnown()); }
4262
4263 /// See AbstractAttribute::getName()
4264 StringRef getName() const override { return "AAAlign"; }
4265
4266 /// See AbstractAttribute::getIdAddr()
4267 const char *getIdAddr() const override { return &ID; }
4268
4269 /// This function should return true if the type of the \p AA is AAAlign
4270 static bool classof(const AbstractAttribute *AA) {
4271 return (AA->getIdAddr() == &ID);
4272 }
4273
4274 /// Create an abstract attribute view for the position \p IRP.
4276 Attributor &A);
4277
4278 /// Unique ID (due to the unique address)
4279 LLVM_ABI static const char ID;
4280};
4281
4282/// An abstract interface to track if a value leaves it's defining function
4283/// instance.
4284/// TODO: We should make it a ternary AA tracking uniqueness, and uniqueness
4285/// wrt. the Attributor analysis separately.
4286struct AAInstanceInfo : public StateWrapper<BooleanState, AbstractAttribute> {
4289
4290 /// Return true if we assume that the underlying value is unique in its scope
4291 /// wrt. the Attributor analysis. That means it might not be unique but we can
4292 /// still use pointer equality without risking to represent two instances with
4293 /// one `llvm::Value`.
4294 bool isAssumedUniqueForAnalysis() const { return isAssumed(); }
4295
4296 /// Create an abstract attribute view for the position \p IRP.
4298 Attributor &A);
4299
4300 /// See AbstractAttribute::getName()
4301 StringRef getName() const override { return "AAInstanceInfo"; }
4302
4303 /// See AbstractAttribute::getIdAddr()
4304 const char *getIdAddr() const override { return &ID; }
4305
4306 /// This function should return true if the type of the \p AA is
4307 /// AAInstanceInfo
4308 static bool classof(const AbstractAttribute *AA) {
4309 return (AA->getIdAddr() == &ID);
4310 }
4311
4312 /// Unique ID (due to the unique address)
4313 LLVM_ABI static const char ID;
4314};
4315
4316/// An abstract interface for all nocapture attributes.
4318 : public IRAttribute<
4319 Attribute::Captures,
4320 StateWrapper<BitIntegerState<uint16_t, 7, 0>, AbstractAttribute>,
4321 AANoCapture> {
4323
4324 /// See IRAttribute::isImpliedByIR
4325 LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
4326 Attribute::AttrKind ImpliedAttributeKind,
4327 bool IgnoreSubsumingPositions = false);
4328
4329 /// Update \p State according to the capture capabilities of \p F for position
4330 /// \p IRP.
4331 LLVM_ABI static void
4333 BitIntegerState &State);
4334
4335 /// See AbstractAttribute::isValidIRPositionForInit
4337 if (!IRP.getAssociatedType()->isPointerTy())
4338 return false;
4339 return IRAttribute::isValidIRPositionForInit(A, IRP);
4340 }
4341
4342 /// State encoding bits. A set bit in the state means the property holds.
4343 /// NO_CAPTURE is the best possible state, 0 the worst possible state.
4344 enum {
4348
4349 /// If we do not capture the value in memory or through integers we can only
4350 /// communicate it back as a derived pointer.
4352
4353 /// If we do not capture the value in memory, through integers, or as a
4354 /// derived pointer we know it is not captured.
4357 };
4358
4359 /// Return true if we know that the underlying value is not captured in its
4360 /// respective scope.
4361 bool isKnownNoCapture() const { return isKnown(NO_CAPTURE); }
4362
4363 /// Return true if we assume that the underlying value is not captured in its
4364 /// respective scope.
4365 bool isAssumedNoCapture() const { return isAssumed(NO_CAPTURE); }
4366
4367 /// Return true if we know that the underlying value is not captured in its
4368 /// respective scope but we allow it to escape through a "return".
4372
4373 /// Return true if we assume that the underlying value is not captured in its
4374 /// respective scope but we allow it to escape through a "return".
4378
4379 /// Create an abstract attribute view for the position \p IRP.
4381 Attributor &A);
4382
4383 /// See AbstractAttribute::getName()
4384 StringRef getName() const override { return "AANoCapture"; }
4385
4386 /// See AbstractAttribute::getIdAddr()
4387 const char *getIdAddr() const override { return &ID; }
4388
4389 /// This function should return true if the type of the \p AA is AANoCapture
4390 static bool classof(const AbstractAttribute *AA) {
4391 return (AA->getIdAddr() == &ID);
4392 }
4393
4394 /// Unique ID (due to the unique address)
4395 LLVM_ABI static const char ID;
4396};
4397
4399
4401
4406 return getBestState(VS.Ty);
4407 }
4408
4409 /// Return the worst possible representable state.
4412 DS.indicatePessimisticFixpoint();
4413 return DS;
4414 }
4417 return getWorstState(VS.Ty);
4418 }
4419
4420 /// See AbstractState::isValidState(...)
4421 bool isValidState() const override { return BS.isValidState(); }
4422
4423 /// See AbstractState::isAtFixpoint(...)
4424 bool isAtFixpoint() const override { return BS.isAtFixpoint(); }
4425
4426 /// Return the assumed state encoding.
4428 const ValueSimplifyStateType &getAssumed() const { return *this; }
4429
4430 /// See AbstractState::indicatePessimisticFixpoint(...)
4432 return BS.indicatePessimisticFixpoint();
4433 }
4434
4435 /// See AbstractState::indicateOptimisticFixpoint(...)
4437 return BS.indicateOptimisticFixpoint();
4438 }
4439
4440 /// "Clamp" this state with \p PVS.
4442 BS ^= VS.BS;
4443 unionAssumed(VS.SimplifiedAssociatedValue);
4444 return *this;
4445 }
4446
4448 if (isValidState() != RHS.isValidState())
4449 return false;
4450 if (!isValidState() && !RHS.isValidState())
4451 return true;
4452 return SimplifiedAssociatedValue == RHS.SimplifiedAssociatedValue;
4453 }
4454
4455protected:
4456 /// The type of the original value.
4458
4459 /// Merge \p Other into the currently assumed simplified value
4460 LLVM_ABI bool unionAssumed(std::optional<Value *> Other);
4461
4462 /// Helper to track validity and fixpoint
4464
4465 /// An assumed simplified value. Initially, it is set to std::nullopt, which
4466 /// means that the value is not clear under current assumption. If in the
4467 /// pessimistic state, getAssumedSimplifiedValue doesn't return this value but
4468 /// returns orignal associated value.
4469 std::optional<Value *> SimplifiedAssociatedValue;
4470};
4471
4472/// An abstract interface for value simplify abstract attribute.
4474 : public StateWrapper<ValueSimplifyStateType, AbstractAttribute, Type *> {
4477 : Base(IRP, IRP.getAssociatedType()) {}
4478
4479 /// Create an abstract attribute view for the position \p IRP.
4481 Attributor &A);
4482
4483 /// See AbstractAttribute::getName()
4484 StringRef getName() const override { return "AAValueSimplify"; }
4485
4486 /// See AbstractAttribute::getIdAddr()
4487 const char *getIdAddr() const override { return &ID; }
4488
4489 /// This function should return true if the type of the \p AA is
4490 /// AAValueSimplify
4491 static bool classof(const AbstractAttribute *AA) {
4492 return (AA->getIdAddr() == &ID);
4493 }
4494
4495 /// Unique ID (due to the unique address)
4496 LLVM_ABI static const char ID;
4497
4498private:
4499 /// Return an assumed simplified value if a single candidate is found. If
4500 /// there cannot be one, return original value. If it is not clear yet, return
4501 /// std::nullopt.
4502 ///
4503 /// Use `Attributor::getAssumedSimplified` for value simplification.
4504 virtual std::optional<Value *>
4505 getAssumedSimplifiedValue(Attributor &A) const = 0;
4506
4507 friend struct Attributor;
4508};
4509
4510struct AAHeapToStack : public StateWrapper<BooleanState, AbstractAttribute> {
4512 AAHeapToStack(const IRPosition &IRP, Attributor &A) : Base(IRP) {}
4513
4514 /// Returns true if HeapToStack conversion is assumed to be possible.
4515 virtual bool isAssumedHeapToStack(const CallBase &CB) const = 0;
4516
4517 /// Returns true if HeapToStack conversion is assumed and the CB is a
4518 /// callsite to a free operation to be removed.
4519 virtual bool isAssumedHeapToStackRemovedFree(CallBase &CB) const = 0;
4520
4521 /// Create an abstract attribute view for the position \p IRP.
4523 Attributor &A);
4524
4525 /// See AbstractAttribute::getName()
4526 StringRef getName() const override { return "AAHeapToStack"; }
4527
4528 /// See AbstractAttribute::getIdAddr()
4529 const char *getIdAddr() const override { return &ID; }
4530
4531 /// This function should return true if the type of the \p AA is AAHeapToStack
4532 static bool classof(const AbstractAttribute *AA) {
4533 return (AA->getIdAddr() == &ID);
4534 }
4535
4536 /// Unique ID (due to the unique address)
4537 LLVM_ABI static const char ID;
4538};
4539
4540/// An abstract interface for privatizability.
4541///
4542/// A pointer is privatizable if it can be replaced by a new, private one.
4543/// Privatizing pointer reduces the use count, interaction between unrelated
4544/// code parts.
4545///
4546/// In order for a pointer to be privatizable its value cannot be observed
4547/// (=nocapture), it is (for now) not written (=readonly & noalias), we know
4548/// what values are necessary to make the private copy look like the original
4549/// one, and the values we need can be loaded (=dereferenceable).
4551 : public StateWrapper<BooleanState, AbstractAttribute> {
4554
4555 /// See AbstractAttribute::isValidIRPositionForInit
4558 return false;
4560 }
4561
4562 /// Returns true if pointer privatization is assumed to be possible.
4563 bool isAssumedPrivatizablePtr() const { return getAssumed(); }
4564
4565 /// See AbstractAttribute::requiresCallersForArgOrFunction
4566 static bool requiresCallersForArgOrFunction() { return true; }
4567
4568 /// Return the type we can choose for a private copy of the underlying
4569 /// value. std::nullopt means it is not clear yet, nullptr means there is
4570 /// none.
4571 virtual std::optional<Type *> getPrivatizableType() const = 0;
4572
4573 /// Create an abstract attribute view for the position \p IRP.
4575 Attributor &A);
4576
4577 /// See AbstractAttribute::getName()
4578 StringRef getName() const override { return "AAPrivatizablePtr"; }
4579
4580 /// See AbstractAttribute::getIdAddr()
4581 const char *getIdAddr() const override { return &ID; }
4582
4583 /// This function should return true if the type of the \p AA is
4584 /// AAPricatizablePtr
4585 static bool classof(const AbstractAttribute *AA) {
4586 return (AA->getIdAddr() == &ID);
4587 }
4588
4589 /// Unique ID (due to the unique address)
4590 LLVM_ABI static const char ID;
4591};
4592
4593/// An abstract interface for memory access kind related attributes
4594/// (readnone/readonly/writeonly).
4596 : public IRAttribute<
4597 Attribute::None,
4598 StateWrapper<BitIntegerState<uint8_t, 3>, AbstractAttribute>,
4599 AAMemoryBehavior> {
4601
4602 /// See AbstractAttribute::hasTrivialInitializer.
4603 static bool hasTrivialInitializer() { return false; }
4604
4605 /// See AbstractAttribute::isValidIRPositionForInit
4607 if (!IRP.isFunctionScope() && !IRP.getAssociatedType()->isPointerTy())
4608 return false;
4609 return IRAttribute::isValidIRPositionForInit(A, IRP);
4610 }
4611
4612 /// State encoding bits. A set bit in the state means the property holds.
4613 /// BEST_STATE is the best possible state, 0 the worst possible state.
4614 enum {
4615 NO_READS = 1 << 0,
4616 NO_WRITES = 1 << 1,
4618
4620 };
4621 static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
4622
4623 /// Return true if we know that the underlying value is not read or accessed
4624 /// in its respective scope.
4625 bool isKnownReadNone() const { return isKnown(NO_ACCESSES); }
4626
4627 /// Return true if we assume that the underlying value is not read or accessed
4628 /// in its respective scope.
4629 bool isAssumedReadNone() const { return isAssumed(NO_ACCESSES); }
4630
4631 /// Return true if we know that the underlying value is not accessed
4632 /// (=written) in its respective scope.
4633 bool isKnownReadOnly() const { return isKnown(NO_WRITES); }
4634
4635 /// Return true if we assume that the underlying value is not accessed
4636 /// (=written) in its respective scope.
4637 bool isAssumedReadOnly() const { return isAssumed(NO_WRITES); }
4638
4639 /// Return true if we know that the underlying value is not read in its
4640 /// respective scope.
4641 bool isKnownWriteOnly() const { return isKnown(NO_READS); }
4642
4643 /// Return true if we assume that the underlying value is not read in its
4644 /// respective scope.
4645 bool isAssumedWriteOnly() const { return isAssumed(NO_READS); }
4646
4647 /// Create an abstract attribute view for the position \p IRP.
4649 Attributor &A);
4650
4651 /// See AbstractAttribute::getName()
4652 StringRef getName() const override { return "AAMemoryBehavior"; }
4653
4654 /// See AbstractAttribute::getIdAddr()
4655 const char *getIdAddr() const override { return &ID; }
4656
4657 /// This function should return true if the type of the \p AA is
4658 /// AAMemoryBehavior
4659 static bool classof(const AbstractAttribute *AA) {
4660 return (AA->getIdAddr() == &ID);
4661 }
4662
4663 /// Unique ID (due to the unique address)
4664 LLVM_ABI static const char ID;
4665};
4666
4667/// An abstract interface for all memory location attributes
4668/// (readnone/argmemonly/inaccessiblememonly/inaccessibleorargmemonly).
4670 : public IRAttribute<
4671 Attribute::None,
4672 StateWrapper<BitIntegerState<uint32_t, 511>, AbstractAttribute>,
4673 AAMemoryLocation> {
4674 using MemoryLocationsKind = StateType::base_t;
4675
4677
4678 /// See AbstractAttribute::requiresCalleeForCallBase.
4679 static bool requiresCalleeForCallBase() { return true; }
4680
4681 /// See AbstractAttribute::hasTrivialInitializer.
4682 static bool hasTrivialInitializer() { return false; }
4683
4684 /// See AbstractAttribute::isValidIRPositionForInit
4686 if (!IRP.isFunctionScope() &&
4688 return false;
4689 return IRAttribute::isValidIRPositionForInit(A, IRP);
4690 }
4691
4692 /// Encoding of different locations that could be accessed by a memory
4693 /// access.
4694 enum {
4708
4709 // Helper bit to track if we gave up or not.
4711
4713 };
4714 static_assert(BEST_STATE == getBestState(), "Unexpected BEST_STATE value");
4715
4716 /// Return true if we know that the associated functions has no observable
4717 /// accesses.
4718 bool isKnownReadNone() const { return isKnown(NO_LOCATIONS); }
4719
4720 /// Return true if we assume that the associated functions has no observable
4721 /// accesses.
4722 bool isAssumedReadNone() const {
4724 }
4725
4726 /// Return true if we know that the associated functions has at most
4727 /// local/stack accesses.
4728 bool isKnowStackOnly() const {
4729 return isKnown(inverseLocation(NO_LOCAL_MEM, true, true));
4730 }
4731
4732 /// Return true if we assume that the associated functions has at most
4733 /// local/stack accesses.
4734 bool isAssumedStackOnly() const {
4735 return isAssumed(inverseLocation(NO_LOCAL_MEM, true, true));
4736 }
4737
4738 /// Return true if we know that the underlying value will only access
4739 /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
4741 return isKnown(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
4742 }
4743
4744 /// Return true if we assume that the underlying value will only access
4745 /// inaccesible memory only (see Attribute::InaccessibleMemOnly).
4747 return isAssumed(inverseLocation(NO_INACCESSIBLE_MEM, true, true));
4748 }
4749
4750 /// Return true if we know that the underlying value will only access
4751 /// argument pointees (see Attribute::ArgMemOnly).
4752 bool isKnownArgMemOnly() const {
4753 return isKnown(inverseLocation(NO_ARGUMENT_MEM, true, true));
4754 }
4755
4756 /// Return true if we assume that the underlying value will only access
4757 /// argument pointees (see Attribute::ArgMemOnly).
4758 bool isAssumedArgMemOnly() const {
4759 return isAssumed(inverseLocation(NO_ARGUMENT_MEM, true, true));
4760 }
4761
4762 /// Return true if we know that the underlying value will only access
4763 /// inaccesible memory or argument pointees (see
4764 /// Attribute::InaccessibleOrArgMemOnly).
4766 return isKnown(
4768 }
4769
4770 /// Return true if we assume that the underlying value will only access
4771 /// inaccesible memory or argument pointees (see
4772 /// Attribute::InaccessibleOrArgMemOnly).
4777
4778 /// Return true if the underlying value may access memory through arguement
4779 /// pointers of the associated function, if any.
4780 bool mayAccessArgMem() const { return !isAssumed(NO_ARGUMENT_MEM); }
4781
4782 /// Return true if only the memory locations specififed by \p MLK are assumed
4783 /// to be accessed by the associated function.
4785 return isAssumed(MLK);
4786 }
4787
4788 /// Return the locations that are assumed to be not accessed by the associated
4789 /// function, if any.
4793
4794 /// Return the inverse of location \p Loc, thus for NO_XXX the return
4795 /// describes ONLY_XXX. The flags \p AndLocalMem and \p AndConstMem determine
4796 /// if local (=stack) and constant memory are allowed as well. Most of the
4797 /// time we do want them to be included, e.g., argmemonly allows accesses via
4798 /// argument pointers or local or constant memory accesses.
4799 static MemoryLocationsKind
4800 inverseLocation(MemoryLocationsKind Loc, bool AndLocalMem, bool AndConstMem) {
4801 return NO_LOCATIONS & ~(Loc | (AndLocalMem ? NO_LOCAL_MEM : 0) |
4802 (AndConstMem ? NO_CONST_MEM : 0));
4803 };
4804
4805 /// Return the locations encoded by \p MLK as a readable string.
4807
4808 /// Simple enum to distinguish read/write/read-write accesses.
4810 NONE = 0,
4811 READ = 1 << 0,
4812 WRITE = 1 << 1,
4814 };
4815
4816 /// Check \p Pred on all accesses to the memory kinds specified by \p MLK.
4817 ///
4818 /// This method will evaluate \p Pred on all accesses (access instruction +
4819 /// underlying accessed memory pointer) and it will return true if \p Pred
4820 /// holds every time.
4822 function_ref<bool(const Instruction *, const Value *, AccessKind,
4824 Pred,
4825 MemoryLocationsKind MLK) const = 0;
4826
4827 /// Create an abstract attribute view for the position \p IRP.
4829 Attributor &A);
4830
4831 /// See AbstractState::getAsStr(Attributor).
4832 const std::string getAsStr(Attributor *A) const override {
4834 }
4835
4836 /// See AbstractAttribute::getName()
4837 StringRef getName() const override { return "AAMemoryLocation"; }
4838
4839 /// See AbstractAttribute::getIdAddr()
4840 const char *getIdAddr() const override { return &ID; }
4841
4842 /// This function should return true if the type of the \p AA is
4843 /// AAMemoryLocation
4844 static bool classof(const AbstractAttribute *AA) {
4845 return (AA->getIdAddr() == &ID);
4846 }
4847
4848 /// Unique ID (due to the unique address)
4849 LLVM_ABI static const char ID;
4850};
4851
4852/// An abstract interface for range value analysis.
4854 : public StateWrapper<IntegerRangeState, AbstractAttribute, uint32_t> {
4857 : Base(IRP, IRP.getAssociatedType()->getIntegerBitWidth()) {}
4858
4859 /// See AbstractAttribute::isValidIRPositionForInit
4861 if (!IRP.getAssociatedType()->isIntegerTy())
4862 return false;
4864 }
4865
4866 /// See AbstractAttribute::requiresCallersForArgOrFunction
4867 static bool requiresCallersForArgOrFunction() { return true; }
4868
4869 /// See AbstractAttribute::getState(...).
4870 IntegerRangeState &getState() override { return *this; }
4871 const IntegerRangeState &getState() const override { return *this; }
4872
4873 /// Create an abstract attribute view for the position \p IRP.
4875 Attributor &A);
4876
4877 /// Return an assumed range for the associated value a program point \p CtxI.
4878 /// If \p I is nullptr, simply return an assumed range.
4879 virtual ConstantRange
4881 const Instruction *CtxI = nullptr) const = 0;
4882
4883 /// Return an assumed constant for the associated value a program point \p
4884 /// CtxI.
4885 std::optional<Constant *>
4886 getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
4887 ConstantRange RangeV = getAssumedConstantRange(A, CtxI);
4888 if (auto *C = RangeV.getSingleElement()) {
4891 AA::getWithType(*ConstantInt::get(Ty->getContext(), *C), *Ty));
4892 }
4893 if (RangeV.isEmptySet())
4894 return std::nullopt;
4895 return nullptr;
4896 }
4897
4898 /// See AbstractAttribute::getName()
4899 StringRef getName() const override { return "AAValueConstantRange"; }
4900
4901 /// See AbstractAttribute::getIdAddr()
4902 const char *getIdAddr() const override { return &ID; }
4903
4904 /// This function should return true if the type of the \p AA is
4905 /// AAValueConstantRange
4906 static bool classof(const AbstractAttribute *AA) {
4907 return (AA->getIdAddr() == &ID);
4908 }
4909
4910 /// Unique ID (due to the unique address)
4911 LLVM_ABI static const char ID;
4912};
4913
4914/// A class for a set state.
4915/// The assumed boolean state indicates whether the corresponding set is full
4916/// set or not. If the assumed state is false, this is the worst state. The
4917/// worst state (invalid state) of set of potential values is when the set
4918/// contains every possible value (i.e. we cannot in any way limit the value
4919/// that the target position can take). That never happens naturally, we only
4920/// force it. As for the conditions under which we force it, see
4921/// AAPotentialConstantValues.
4922template <typename MemberTy> struct PotentialValuesState : AbstractState {
4924
4925 PotentialValuesState() : IsValidState(true), UndefIsContained(false) {}
4926
4928 : IsValidState(IsValid), UndefIsContained(false) {}
4929
4930 /// See AbstractState::isValidState(...)
4931 bool isValidState() const override { return IsValidState.isValidState(); }
4932
4933 /// See AbstractState::isAtFixpoint(...)
4934 bool isAtFixpoint() const override { return IsValidState.isAtFixpoint(); }
4935
4936 /// See AbstractState::indicatePessimisticFixpoint(...)
4938 return IsValidState.indicatePessimisticFixpoint();
4939 }
4940
4941 /// See AbstractState::indicateOptimisticFixpoint(...)
4943 return IsValidState.indicateOptimisticFixpoint();
4944 }
4945
4946 /// Return the assumed state
4948 const PotentialValuesState &getAssumed() const { return *this; }
4949
4950 /// Return this set. We should check whether this set is valid or not by
4951 /// isValidState() before calling this function.
4952 const SetTy &getAssumedSet() const {
4953 assert(isValidState() && "This set shoud not be used when it is invalid!");
4954 return Set;
4955 }
4956
4957 /// Returns whether this state contains an undef value or not.
4958 bool undefIsContained() const {
4959 assert(isValidState() && "This flag shoud not be used when it is invalid!");
4960 return UndefIsContained;
4961 }
4962
4964 if (isValidState() != RHS.isValidState())
4965 return false;
4966 if (!isValidState() && !RHS.isValidState())
4967 return true;
4968 if (undefIsContained() != RHS.undefIsContained())
4969 return false;
4970 return Set == RHS.getAssumedSet();
4971 }
4972
4973 /// Maximum number of potential values to be tracked.
4974 /// This is set by -attributor-max-potential-values command line option
4975 static unsigned MaxPotentialValues;
4976
4977 /// Return empty set as the best state of potential values.
4979 return PotentialValuesState(true);
4980 }
4981
4983 return getBestState();
4984 }
4985
4986 /// Return full set as the worst state of potential values.
4988 return PotentialValuesState(false);
4989 }
4990
4991 /// Union assumed set with the passed value.
4992 void unionAssumed(const MemberTy &C) { insert(C); }
4993
4994 /// Union assumed set with assumed set of the passed state \p PVS.
4995 void unionAssumed(const PotentialValuesState &PVS) { unionWith(PVS); }
4996
4997 /// Union assumed set with an undef value.
4998 void unionAssumedWithUndef() { unionWithUndef(); }
4999
5000 /// "Clamp" this state with \p PVS.
5002 IsValidState ^= PVS.IsValidState;
5003 unionAssumed(PVS);
5004 return *this;
5005 }
5006
5008 IsValidState &= PVS.IsValidState;
5009 unionAssumed(PVS);
5010 return *this;
5011 }
5012
5013protected:
5015 assert(isValidState() && "This set shoud not be used when it is invalid!");
5016 return Set;
5017 }
5018
5019private:
5020 /// Check the size of this set, and invalidate when the size is no
5021 /// less than \p MaxPotentialValues threshold.
5022 void checkAndInvalidate() {
5023 if (Set.size() >= MaxPotentialValues)
5025 else
5026 reduceUndefValue();
5027 }
5028
5029 /// If this state contains both undef and not undef, we can reduce
5030 /// undef to the not undef value.
5031 void reduceUndefValue() { UndefIsContained = UndefIsContained & Set.empty(); }
5032
5033 /// Insert an element into this set.
5034 void insert(const MemberTy &C) {
5035 if (!isValidState())
5036 return;
5037 Set.insert(C);
5038 checkAndInvalidate();
5039 }
5040
5041 /// Take union with R.
5042 void unionWith(const PotentialValuesState &R) {
5043 /// If this is a full set, do nothing.
5044 if (!isValidState())
5045 return;
5046 /// If R is full set, change L to a full set.
5047 if (!R.isValidState()) {
5049 return;
5050 }
5051 Set.insert_range(R.Set);
5052 UndefIsContained |= R.undefIsContained();
5053 checkAndInvalidate();
5054 }
5055
5056 /// Take union with an undef value.
5057 void unionWithUndef() {
5058 UndefIsContained = true;
5059 reduceUndefValue();
5060 }
5061
5062 /// A helper state which indicate whether this state is valid or not.
5063 BooleanState IsValidState;
5064
5065 /// Container for potential values
5066 SetTy Set;
5067
5068 /// Flag for undef value
5069 bool UndefIsContained;
5070};
5071
5076
5077 bool operator==(const DenormalState Other) const {
5078 return Mode == Other.Mode && ModeF32 == Other.ModeF32;
5079 }
5080
5081 bool operator!=(const DenormalState Other) const {
5082 return Mode != Other.Mode || ModeF32 != Other.ModeF32;
5083 }
5084
5085 bool isValid() const { return Mode.isValid() && ModeF32.isValid(); }
5086
5090 if (Caller == Callee)
5091 return Caller;
5092 if (Callee == DenormalMode::Dynamic)
5093 return Caller;
5094 if (Caller == DenormalMode::Dynamic)
5095 return Callee;
5096 return DenormalMode::Invalid;
5097 }
5098
5100 return DenormalMode{unionDenormalKind(Callee.Output, Caller.Output),
5101 unionDenormalKind(Callee.Input, Caller.Input)};
5102 }
5103
5105 DenormalState Callee(*this);
5106 Callee.Mode = unionAssumed(Callee.Mode, Caller.Mode);
5107 Callee.ModeF32 = unionAssumed(Callee.ModeF32, Caller.ModeF32);
5108 return Callee;
5109 }
5110 };
5111
5113
5114 /// Explicitly track whether we've hit a fixed point.
5115 bool IsAtFixedpoint = false;
5116
5118
5119 DenormalState getKnown() const { return Known; }
5120
5121 // There's only really known or unknown, there's no speculatively assumable
5122 // state.
5123 DenormalState getAssumed() const { return Known; }
5124
5125 bool isValidState() const override { return Known.isValid(); }
5126
5127 /// Return true if there are no dynamic components to the denormal mode worth
5128 /// specializing.
5129 bool isModeFixed() const {
5130 return Known.Mode.Input != DenormalMode::Dynamic &&
5131 Known.Mode.Output != DenormalMode::Dynamic &&
5132 Known.ModeF32.Input != DenormalMode::Dynamic &&
5133 Known.ModeF32.Output != DenormalMode::Dynamic;
5134 }
5135
5136 bool isAtFixpoint() const override { return IsAtFixedpoint; }
5137
5143
5147
5151
5153 Known = Known.unionWith(Caller.getKnown());
5154 return *this;
5155 }
5156};
5157
5161
5165 const PotentialLLVMValuesState &R);
5166
5167/// An abstract interface for potential values analysis.
5168///
5169/// This AA collects potential values for each IR position.
5170/// An assumed set of potential values is initialized with the empty set (the
5171/// best state) and it will grow monotonically as we find more potential values
5172/// for this position.
5173/// The set might be forced to the worst state, that is, to contain every
5174/// possible value for this position in 2 cases.
5175/// 1. We surpassed the \p MaxPotentialValues threshold. This includes the
5176/// case that this position is affected (e.g. because of an operation) by a
5177/// Value that is in the worst state.
5178/// 2. We tried to initialize on a Value that we cannot handle (e.g. an
5179/// operator we do not currently handle).
5180///
5181/// For non constant integers see AAPotentialValues.
5183 : public StateWrapper<PotentialConstantIntValuesState, AbstractAttribute> {
5186
5187 /// See AbstractAttribute::isValidIRPositionForInit
5189 if (!IRP.getAssociatedType()->isIntegerTy())
5190 return false;
5192 }
5193
5194 /// See AbstractAttribute::requiresCallersForArgOrFunction
5195 static bool requiresCallersForArgOrFunction() { return true; }
5196
5197 /// See AbstractAttribute::getState(...).
5198 PotentialConstantIntValuesState &getState() override { return *this; }
5200 return *this;
5201 }
5202
5203 /// Create an abstract attribute view for the position \p IRP.
5206
5207 /// Return assumed constant for the associated value
5208 std::optional<Constant *>
5209 getAssumedConstant(Attributor &A, const Instruction *CtxI = nullptr) const {
5210 if (!isValidState())
5211 return nullptr;
5212 if (getAssumedSet().size() == 1) {
5215 *ConstantInt::get(Ty->getContext(), *(getAssumedSet().begin())),
5216 *Ty));
5217 }
5218 if (getAssumedSet().size() == 0) {
5219 if (undefIsContained())
5221 return std::nullopt;
5222 }
5223
5224 return nullptr;
5225 }
5226
5227 /// Return the minimum trailing zeros of potential constants
5229 if (!isValidState() || getAssumedSet().empty())
5230 return 0;
5231 unsigned TrailingZeros = getAssumedSet().begin()->getBitWidth() + 1;
5232 for (const APInt &It : getAssumedSet()) {
5233 if (It.countTrailingZeros() < TrailingZeros)
5234 TrailingZeros = It.countTrailingZeros();
5235 }
5236 if (TrailingZeros > getAssumedSet().begin()->getBitWidth())
5237 return 0;
5238 return TrailingZeros;
5239 }
5240 /// See AbstractAttribute::getName()
5241 StringRef getName() const override { return "AAPotentialConstantValues"; }
5242
5243 /// See AbstractAttribute::getIdAddr()
5244 const char *getIdAddr() const override { return &ID; }
5245
5246 /// This function should return true if the type of the \p AA is
5247 /// AAPotentialConstantValues
5248 static bool classof(const AbstractAttribute *AA) {
5249 return (AA->getIdAddr() == &ID);
5250 }
5251
5252 /// Unique ID (due to the unique address)
5253 LLVM_ABI static const char ID;
5254};
5255
5257 : public StateWrapper<PotentialLLVMValuesState, AbstractAttribute> {
5260
5261 /// See AbstractAttribute::requiresCallersForArgOrFunction
5262 static bool requiresCallersForArgOrFunction() { return true; }
5263
5264 /// See AbstractAttribute::getState(...).
5265 PotentialLLVMValuesState &getState() override { return *this; }
5266 const PotentialLLVMValuesState &getState() const override { return *this; }
5267
5268 /// Create an abstract attribute view for the position \p IRP.
5270 Attributor &A);
5271
5272 /// Extract the single value in \p Values if any.
5273 LLVM_ABI static Value *
5275 const IRPosition &IRP,
5277
5278 /// See AbstractAttribute::getName()
5279 StringRef getName() const override { return "AAPotentialValues"; }
5280
5281 /// See AbstractAttribute::getIdAddr()
5282 const char *getIdAddr() const override { return &ID; }
5283
5284 /// This function should return true if the type of the \p AA is
5285 /// AAPotentialValues
5286 static bool classof(const AbstractAttribute *AA) {
5287 return (AA->getIdAddr() == &ID);
5288 }
5289
5290 /// Unique ID (due to the unique address)
5291 LLVM_ABI static const char ID;
5292
5293private:
5294 virtual bool getAssumedSimplifiedValues(
5296 AA::ValueScope, bool RecurseForSelectAndPHI = false) const = 0;
5297
5298 friend struct Attributor;
5299};
5300
5301/// An abstract interface for all noundef attributes.
5303 : public IRAttribute<Attribute::NoUndef,
5304 StateWrapper<BooleanState, AbstractAttribute>,
5305 AANoUndef> {
5307
5308 /// See IRAttribute::isImpliedByUndef
5309 static bool isImpliedByUndef() { return false; }
5310
5311 /// See IRAttribute::isImpliedByPoison
5312 static bool isImpliedByPoison() { return false; }
5313
5314 /// See IRAttribute::isImpliedByIR
5315 LLVM_ABI static bool isImpliedByIR(Attributor &A, const IRPosition &IRP,
5316 Attribute::AttrKind ImpliedAttributeKind,
5317 bool IgnoreSubsumingPositions = false);
5318
5319 /// Return true if we assume that the underlying value is noundef.
5320 bool isAssumedNoUndef() const { return getAssumed(); }
5321
5322 /// Return true if we know that underlying value is noundef.
5323 bool isKnownNoUndef() const { return getKnown(); }
5324
5325 /// Create an abstract attribute view for the position \p IRP.
5327 Attributor &A);
5328
5329 /// See AbstractAttribute::getName()
5330 StringRef getName() const override { return "AANoUndef"; }
5331
5332 /// See AbstractAttribute::getIdAddr()
5333 const char *getIdAddr() const override { return &ID; }
5334
5335 /// This function should return true if the type of the \p AA is AANoUndef
5336 static bool classof(const AbstractAttribute *AA) {
5337 return (AA->getIdAddr() == &ID);
5338 }
5339
5340 /// Unique ID (due to the unique address)
5341 LLVM_ABI static const char ID;
5342};
5343
5345 : public IRAttribute<
5346 Attribute::NoFPClass,
5347 StateWrapper<BitIntegerState<uint32_t, fcAllFlags, fcNone>,
5348 AbstractAttribute>,
5349 AANoFPClass> {
5352
5354
5355 /// See AbstractAttribute::isValidIRPositionForInit
5357 return AttributeFuncs::isNoFPClassCompatibleType(IRP.getAssociatedType());
5358 }
5359
5360 /// Return the underlying assumed nofpclass.
5362 return static_cast<FPClassTest>(getAssumed());
5363 }
5364 /// Return the underlying known nofpclass.
5366 return static_cast<FPClassTest>(getKnown());
5367 }
5368
5369 /// Create an abstract attribute view for the position \p IRP.
5371 Attributor &A);
5372
5373 /// See AbstractAttribute::getName()
5374 StringRef getName() const override { return "AANoFPClass"; }
5375
5376 /// See AbstractAttribute::getIdAddr()
5377 const char *getIdAddr() const override { return &ID; }
5378
5379 /// This function should return true if the type of the \p AA is AANoFPClass
5380 static bool classof(const AbstractAttribute *AA) {
5381 return (AA->getIdAddr() == &ID);
5382 }
5383
5384 /// Unique ID (due to the unique address)
5385 LLVM_ABI static const char ID;
5386};
5387
5388struct AACallGraphNode;
5389struct AACallEdges;
5390
5391/// An Iterator for call edges, creates AACallEdges attributes in a lazy way.
5392/// This iterator becomes invalid if the underlying edge list changes.
5393/// So This shouldn't outlive a iteration of Attributor.
5394class AACallEdgeIterator
5395 : public iterator_adaptor_base<AACallEdgeIterator,
5396 SetVector<Function *>::iterator> {
5397 AACallEdgeIterator(Attributor &A, SetVector<Function *>::iterator Begin)
5398 : iterator_adaptor_base(Begin), A(A) {}
5399
5400public:
5402
5403private:
5404 Attributor &A;
5405 friend AACallEdges;
5406 friend AttributorCallGraph;
5407};
5408
5411 virtual ~AACallGraphNode() = default;
5412
5415
5416 /// Iterator range for exploring the call graph.
5421
5422protected:
5423 /// Reference to Attributor needed for GraphTraits implementation.
5425};
5426
5427/// An abstract state for querying live call edges.
5428/// This interface uses the Attributor's optimistic liveness
5429/// information to compute the edges that are alive.
5430struct AACallEdges : public StateWrapper<BooleanState, AbstractAttribute>,
5433
5435 : Base(IRP), AACallGraphNode(A) {}
5436
5437 /// See AbstractAttribute::requiresNonAsmForCallBase.
5438 static bool requiresNonAsmForCallBase() { return false; }
5439
5440 /// Get the optimistic edges.
5441 virtual const SetVector<Function *> &getOptimisticEdges() const = 0;
5442
5443 /// Is there any call with a unknown callee.
5444 virtual bool hasUnknownCallee() const = 0;
5445
5446 /// Is there any call with a unknown callee, excluding any inline asm.
5447 virtual bool hasNonAsmUnknownCallee() const = 0;
5448
5449 /// Iterator for exploring the call graph.
5453
5454 /// Iterator for exploring the call graph.
5458
5459 /// Create an abstract attribute view for the position \p IRP.
5461 Attributor &A);
5462
5463 /// See AbstractAttribute::getName()
5464 StringRef getName() const override { return "AACallEdges"; }
5465
5466 /// See AbstractAttribute::getIdAddr()
5467 const char *getIdAddr() const override { return &ID; }
5468
5469 /// This function should return true if the type of the \p AA is AACallEdges.
5470 static bool classof(const AbstractAttribute *AA) {
5471 return (AA->getIdAddr() == &ID);
5472 }
5473
5474 /// Unique ID (due to the unique address)
5475 LLVM_ABI static const char ID;
5476};
5477
5478// Synthetic root node for the Attributor's internal call graph.
5481 ~AttributorCallGraph() override = default;
5482
5484 return AACallEdgeIterator(A, A.Functions.begin());
5485 }
5486
5488 return AACallEdgeIterator(A, A.Functions.end());
5489 }
5490
5491 /// Force populate the entire call graph.
5492 void populateAll() const {
5493 for (const AACallGraphNode *AA : optimisticEdgesRange()) {
5494 // Nothing else to do here.
5495 (void)AA;
5496 }
5497 }
5498
5499 LLVM_ABI void print();
5500};
5501
5502template <> struct GraphTraits<AACallGraphNode *> {
5505
5507 return Node->optimisticEdgesBegin();
5508 }
5509
5511 return Node->optimisticEdgesEnd();
5512 }
5513};
5514
5515template <>
5519
5521 return static_cast<AACallGraphNode *>(G);
5522 }
5523
5525 return G->optimisticEdgesBegin();
5526 }
5527
5529 return G->optimisticEdgesEnd();
5530 }
5531};
5532
5533template <>
5536
5538 const AttributorCallGraph *Graph) {
5539 const AACallEdges *AACE = static_cast<const AACallEdges *>(Node);
5540 return AACE->getAssociatedFunction()->getName().str();
5541 }
5542
5544 const AttributorCallGraph *Graph) {
5545 // Hide the synth root.
5546 return static_cast<const AACallGraphNode *>(Graph) == Node;
5547 }
5548};
5549
5551 : public StateWrapper<BooleanState, AbstractAttribute> {
5554
5555 /// Summary about the execution domain of a block or instruction.
5580
5581 /// Create an abstract attribute view for the position \p IRP.
5583 Attributor &A);
5584
5585 /// See AbstractAttribute::getName().
5586 StringRef getName() const override { return "AAExecutionDomain"; }
5587
5588 /// See AbstractAttribute::getIdAddr().
5589 const char *getIdAddr() const override { return &ID; }
5590
5591 /// Check if an instruction is executed only by the initial thread.
5593 return isExecutedByInitialThreadOnly(*I.getParent());
5594 }
5595
5596 /// Check if a basic block is executed only by the initial thread.
5597 virtual bool isExecutedByInitialThreadOnly(const BasicBlock &) const = 0;
5598
5599 /// Check if the instruction \p I is executed in an aligned region, that is,
5600 /// the synchronizing effects before and after \p I are both aligned barriers.
5601 /// This effectively means all threads execute \p I together.
5603 const Instruction &I) const = 0;
5604
5606 /// Return the execution domain with which the call \p CB is entered and the
5607 /// one with which it is left.
5608 virtual std::pair<ExecutionDomainTy, ExecutionDomainTy>
5609 getExecutionDomain(const CallBase &CB) const = 0;
5611
5612 /// Helper function to determine if \p FI is a no-op given the information
5613 /// about its execution from \p ExecDomainAA.
5614 virtual bool isNoOpFence(const FenceInst &FI) const = 0;
5615
5616 /// This function should return true if the type of the \p AA is
5617 /// AAExecutionDomain.
5618 static bool classof(const AbstractAttribute *AA) {
5619 return (AA->getIdAddr() == &ID);
5620 }
5621
5622 /// Unique ID (due to the unique address)
5623 LLVM_ABI static const char ID;
5624};
5625
5626/// An abstract Attribute for computing reachability between functions.
5628 : public StateWrapper<BooleanState, AbstractAttribute> {
5630
5632
5633 /// If the function represented by this possition can reach \p Fn.
5634 bool canReach(Attributor &A, const Function &Fn) const {
5635 Function *Scope = getAnchorScope();
5636 if (!Scope || Scope->isDeclaration())
5637 return true;
5638 return instructionCanReach(A, Scope->getEntryBlock().front(), Fn);
5639 }
5640
5641 /// Can \p Inst reach \p Fn.
5642 /// See also AA::isPotentiallyReachable.
5644 Attributor &A, const Instruction &Inst, const Function &Fn,
5645 const AA::InstExclusionSetTy *ExclusionSet = nullptr) const = 0;
5646
5647 /// Create an abstract attribute view for the position \p IRP.
5650
5651 /// See AbstractAttribute::getName()
5652 StringRef getName() const override { return "AAInterFnReachability"; }
5653
5654 /// See AbstractAttribute::getIdAddr()
5655 const char *getIdAddr() const override { return &ID; }
5656
5657 /// This function should return true if the type of the \p AA is AACallEdges.
5658 static bool classof(const AbstractAttribute *AA) {
5659 return (AA->getIdAddr() == &ID);
5660 }
5661
5662 /// Unique ID (due to the unique address)
5663 LLVM_ABI static const char ID;
5664};
5665
5666/// An abstract Attribute for determining the necessity of the convergent
5667/// attribute.
5668struct AANonConvergent : public StateWrapper<BooleanState, AbstractAttribute> {
5670
5672
5673 /// Create an abstract attribute view for the position \p IRP.
5675 Attributor &A);
5676
5677 /// Return true if "non-convergent" is assumed.
5678 bool isAssumedNotConvergent() const { return getAssumed(); }
5679
5680 /// Return true if "non-convergent" is known.
5681 bool isKnownNotConvergent() const { return getKnown(); }
5682
5683 /// See AbstractAttribute::getName()
5684 StringRef getName() const override { return "AANonConvergent"; }
5685
5686 /// See AbstractAttribute::getIdAddr()
5687 const char *getIdAddr() const override { return &ID; }
5688
5689 /// This function should return true if the type of the \p AA is
5690 /// AANonConvergent.
5691 static bool classof(const AbstractAttribute *AA) {
5692 return (AA->getIdAddr() == &ID);
5693 }
5694
5695 /// Unique ID (due to the unique address)
5696 LLVM_ABI static const char ID;
5697};
5698
5699/// An abstract interface for struct information.
5702
5703 /// See AbstractAttribute::isValidIRPositionForInit
5706 return false;
5708 }
5709
5711 // First two bits to distinguish may and must accesses.
5712 AK_MUST = 1 << 0,
5713 AK_MAY = 1 << 1,
5714
5715 // Then two bits for read and write. These are not exclusive.
5716 AK_R = 1 << 2,
5717 AK_W = 1 << 3,
5719
5720 // One special case for assumptions about memory content. These
5721 // are neither reads nor writes. They are however always modeled
5722 // as read to avoid using them for write removal.
5724
5725 // Helper for easy access.
5732 };
5733
5734 /// A helper containing a list of offsets computed for a Use. Ideally this
5735 /// list should be strictly ascending, but we ensure that only when we
5736 /// actually translate the list of offsets to a RangeList.
5737 struct OffsetInfo {
5741
5742 const_iterator begin() const { return Offsets.begin(); }
5743 const_iterator end() const { return Offsets.end(); }
5744
5745 bool operator==(const OffsetInfo &RHS) const {
5746 return Offsets == RHS.Offsets;
5747 }
5748
5749 bool operator!=(const OffsetInfo &RHS) const { return !(*this == RHS); }
5750
5751 bool insert(int64_t Offset) { return Offsets.insert(Offset).second; }
5752 bool isUnassigned() const { return Offsets.size() == 0; }
5753
5754 bool isUnknown() const {
5755 if (isUnassigned())
5756 return false;
5757 if (Offsets.size() == 1)
5758 return *Offsets.begin() == AA::RangeTy::Unknown;
5759 return false;
5760 }
5761
5762 void setUnknown() {
5763 Offsets.clear();
5765 }
5766
5767 void addToAll(int64_t Inc) {
5768 VecTy NewOffsets;
5769 for (auto &Offset : Offsets)
5770 NewOffsets.insert(Offset + Inc);
5771 Offsets = std::move(NewOffsets);
5772 }
5773
5774 /// Copy offsets from \p R into the current list.
5775 ///
5776 /// Ideally all lists should be strictly ascending, but we defer that to the
5777 /// actual use of the list. So we just blindly append here.
5778 bool merge(const OffsetInfo &R) { return set_union(Offsets, R.Offsets); }
5779 };
5780
5781 /// A container for a list of ranges.
5782 struct RangeList {
5783 // The set of ranges rarely contains more than one element, and is unlikely
5784 // to contain more than say four elements. So we find the middle-ground with
5785 // a sorted vector. This avoids hard-coding a rarely used number like "four"
5786 // into every instance of a SmallSet.
5792
5793 RangeList(const RangeTy &R) { Ranges.push_back(R); }
5795 Ranges.reserve(Offsets.size());
5796 for (unsigned i = 0, e = Offsets.size(); i != e; ++i) {
5797 assert(((i + 1 == e) || Offsets[i] < Offsets[i + 1]) &&
5798 "Expected strictly ascending offsets.");
5799 Ranges.emplace_back(Offsets[i], Size);
5800 }
5801 }
5802 RangeList() = default;
5803
5804 iterator begin() { return Ranges.begin(); }
5805 iterator end() { return Ranges.end(); }
5806 const_iterator begin() const { return Ranges.begin(); }
5807 const_iterator end() const { return Ranges.end(); }
5808
5809 // Helpers required for std::set_difference
5811 void push_back(const RangeTy &R) {
5812 assert((Ranges.empty() || RangeTy::LessThan(Ranges.back(), R)) &&
5813 "Ensure the last element is the greatest.");
5814 Ranges.push_back(R);
5815 }
5816
5817 /// Copy ranges from \p L that are not in \p R, into \p D.
5818 static void set_difference(const RangeList &L, const RangeList &R,
5819 RangeList &D) {
5820 std::set_difference(L.begin(), L.end(), R.begin(), R.end(),
5821 std::back_inserter(D), RangeTy::LessThan);
5822 }
5823
5824 unsigned size() const { return Ranges.size(); }
5825
5826 bool operator==(const RangeList &OI) const { return Ranges == OI.Ranges; }
5827
5828 /// Merge the ranges in \p RHS into the current ranges.
5829 /// - Merging a list of unknown ranges makes the current list unknown.
5830 /// - Ranges with the same offset are merged according to RangeTy::operator&
5831 /// \return true if the current RangeList changed.
5832 bool merge(const RangeList &RHS) {
5833 if (isUnknown())
5834 return false;
5835 if (RHS.isUnknown()) {
5836 setUnknown();
5837 return true;
5838 }
5839
5840 if (Ranges.empty()) {
5841 Ranges = RHS.Ranges;
5842 return true;
5843 }
5844
5845 bool Changed = false;
5846 auto LPos = Ranges.begin();
5847 for (auto &R : RHS.Ranges) {
5848 auto Result = insert(LPos, R);
5849 if (isUnknown())
5850 return true;
5851 LPos = Result.first;
5852 Changed |= Result.second;
5853 }
5854 return Changed;
5855 }
5856
5857 /// Insert \p R at the given iterator \p Pos, and merge if necessary.
5858 ///
5859 /// This assumes that all ranges before \p Pos are LessThan \p R, and
5860 /// then maintains the sorted order for the suffix list.
5861 ///
5862 /// \return The place of insertion and true iff anything changed.
5863 std::pair<iterator, bool> insert(iterator Pos, const RangeTy &R) {
5864 if (isUnknown())
5865 return std::make_pair(Ranges.begin(), false);
5866 if (R.offsetOrSizeAreUnknown()) {
5867 return std::make_pair(setUnknown(), true);
5868 }
5869
5870 // Maintain this as a sorted vector of unique entries.
5871 auto LB = std::lower_bound(Pos, Ranges.end(), R, RangeTy::LessThan);
5872 if (LB == Ranges.end() || LB->Offset != R.Offset)
5873 return std::make_pair(Ranges.insert(LB, R), true);
5874 bool Changed = *LB != R;
5875 *LB &= R;
5876 if (LB->offsetOrSizeAreUnknown())
5877 return std::make_pair(setUnknown(), true);
5878 return std::make_pair(LB, Changed);
5879 }
5880
5881 /// Add the increment \p Inc to the offset of every range.
5882 void addToAllOffsets(int64_t Inc) {
5883 assert(!isUnassigned() &&
5884 "Cannot increment if the offset is not yet computed!");
5885 if (isUnknown())
5886 return;
5887 for (auto &R : Ranges) {
5888 R.Offset += Inc;
5889 }
5890 }
5891
5892 /// Return true iff the list contains an unknown range.
5893 bool isUnknown() const {
5894 if (isUnassigned())
5895 return false;
5896 if (Ranges.front().offsetOrSizeAreUnknown()) {
5897 assert(Ranges.size() == 1 && "Unknown is a singleton range.");
5898 return true;
5899 }
5900 return false;
5901 }
5902
5903 /// Discard all ranges and insert a single unknown range.
5905 Ranges.clear();
5906 Ranges.push_back(RangeTy::getUnknown());
5907 return Ranges.begin();
5908 }
5909
5910 /// Return true if no ranges have been inserted.
5911 bool isUnassigned() const { return Ranges.size() == 0; }
5912 };
5913
5914 /// An access description.
5915 struct Access {
5916 Access(Instruction *I, int64_t Offset, int64_t Size,
5917 std::optional<Value *> Content, AccessKind Kind, Type *Ty)
5918 : LocalI(I), RemoteI(I), Content(Content), Ranges(Offset, Size),
5919 Kind(Kind), Ty(Ty) {
5920 verify();
5921 }
5922 Access(Instruction *LocalI, Instruction *RemoteI, const RangeList &Ranges,
5923 std::optional<Value *> Content, AccessKind K, Type *Ty)
5924 : LocalI(LocalI), RemoteI(RemoteI), Content(Content), Ranges(Ranges),
5925 Kind(K), Ty(Ty) {
5926 if (Ranges.size() > 1) {
5927 Kind = AccessKind(Kind | AK_MAY);
5928 Kind = AccessKind(Kind & ~AK_MUST);
5929 }
5930 verify();
5931 }
5932 Access(Instruction *LocalI, Instruction *RemoteI, int64_t Offset,
5933 int64_t Size, std::optional<Value *> Content, AccessKind Kind,
5934 Type *Ty)
5935 : LocalI(LocalI), RemoteI(RemoteI), Content(Content),
5936 Ranges(Offset, Size), Kind(Kind), Ty(Ty) {
5937 verify();
5938 }
5939 Access(const Access &Other) = default;
5940
5941 Access &operator=(const Access &Other) = default;
5942 bool operator==(const Access &R) const {
5943 return LocalI == R.LocalI && RemoteI == R.RemoteI && Ranges == R.Ranges &&
5944 Content == R.Content && Kind == R.Kind;
5945 }
5946 bool operator!=(const Access &R) const { return !(*this == R); }
5947
5949 assert(RemoteI == R.RemoteI && "Expected same instruction!");
5950 assert(LocalI == R.LocalI && "Expected same instruction!");
5951
5952 // Note that every Access object corresponds to a unique Value, and only
5953 // accesses to the same Value are merged. Hence we assume that all ranges
5954 // are the same size. If ranges can be different size, then the contents
5955 // must be dropped.
5956 Ranges.merge(R.Ranges);
5957 Content =
5958 AA::combineOptionalValuesInAAValueLatice(Content, R.Content, Ty);
5959
5960 // Combine the access kind, which results in a bitwise union.
5961 // If there is more than one range, then this must be a MAY.
5962 // If we combine a may and a must access we clear the must bit.
5963 Kind = AccessKind(Kind | R.Kind);
5964 if ((Kind & AK_MAY) || Ranges.size() > 1) {
5965 Kind = AccessKind(Kind | AK_MAY);
5966 Kind = AccessKind(Kind & ~AK_MUST);
5967 }
5968 verify();
5969 return *this;
5970 }
5971
5972 void verify() {
5973 assert(isMustAccess() + isMayAccess() == 1 &&
5974 "Expect must or may access, not both.");
5975 assert(isAssumption() + isWrite() <= 1 &&
5976 "Expect assumption access or write access, never both.");
5977 assert((isMayAccess() || Ranges.size() == 1) &&
5978 "Cannot be a must access if there are multiple ranges.");
5979 }
5980
5981 /// Return the access kind.
5982 AccessKind getKind() const { return Kind; }
5983
5984 /// Return true if this is a read access.
5985 bool isRead() const { return Kind & AK_R; }
5986
5987 /// Return true if this is a write access.
5988 bool isWrite() const { return Kind & AK_W; }
5989
5990 /// Return true if this is a write access.
5991 bool isWriteOrAssumption() const { return isWrite() || isAssumption(); }
5992
5993 /// Return true if this is an assumption access.
5994 bool isAssumption() const { return Kind == AK_ASSUMPTION; }
5995
5996 bool isMustAccess() const {
5997 bool MustAccess = Kind & AK_MUST;
5998 assert((!MustAccess || Ranges.size() < 2) &&
5999 "Cannot be a must access if there are multiple ranges.");
6000 return MustAccess;
6001 }
6002
6003 bool isMayAccess() const {
6004 bool MayAccess = Kind & AK_MAY;
6005 assert((MayAccess || Ranges.size() < 2) &&
6006 "Cannot be a must access if there are multiple ranges.");
6007 return MayAccess;
6008 }
6009
6010 /// Return the instruction that causes the access with respect to the local
6011 /// scope of the associated attribute.
6012 Instruction *getLocalInst() const { return LocalI; }
6013
6014 /// Return the actual instruction that causes the access.
6015 Instruction *getRemoteInst() const { return RemoteI; }
6016
6017 /// Return true if the value written is not known yet.
6018 bool isWrittenValueYetUndetermined() const { return !Content; }
6019
6020 /// Return true if the value written cannot be determined at all.
6022 return Content.has_value() && !*Content;
6023 }
6024
6025 /// Return the type associated with the access, if known.
6026 Type *getType() const { return Ty; }
6027
6028 /// Return the value writen, if any.
6031 "Value needs to be determined before accessing it.");
6032 return *Content;
6033 }
6034
6035 /// Return the written value which can be `llvm::null` if it is not yet
6036 /// determined.
6037 std::optional<Value *> getContent() const { return Content; }
6038
6039 const RangeList &getRanges() const { return Ranges; }
6040
6042 const_iterator begin() const { return Ranges.begin(); }
6043 const_iterator end() const { return Ranges.end(); }
6044
6045 private:
6046 /// The instruction responsible for the access with respect to the local
6047 /// scope of the associated attribute.
6048 Instruction *LocalI;
6049
6050 /// The instruction responsible for the access.
6051 Instruction *RemoteI;
6052
6053 /// The value written, if any. `std::nullopt` means "not known yet",
6054 /// `nullptr` cannot be determined.
6055 std::optional<Value *> Content;
6056
6057 /// Set of potential ranges accessed from the base pointer.
6058 RangeList Ranges;
6059
6060 /// The access kind, e.g., READ, as bitset (could be more than one).
6062
6063 /// The type of the content, thus the type read/written, can be null if not
6064 /// available.
6065 Type *Ty;
6066 };
6067
6068 /// Create an abstract attribute view for the position \p IRP.
6070 Attributor &A);
6071
6072 /// See AbstractAttribute::getName()
6073 StringRef getName() const override { return "AAPointerInfo"; }
6074
6075 /// See AbstractAttribute::getIdAddr()
6076 const char *getIdAddr() const override { return &ID; }
6077
6079 using const_bin_iterator = OffsetBinsTy::const_iterator;
6080 virtual const_bin_iterator begin() const = 0;
6081 virtual const_bin_iterator end() const = 0;
6082 virtual int64_t numOffsetBins() const = 0;
6083 virtual bool reachesReturn() const = 0;
6084 virtual void addReturnedOffsetsTo(OffsetInfo &) const = 0;
6085
6086 /// Call \p CB on all accesses that might interfere with \p Range and return
6087 /// true if all such accesses were known and the callback returned true for
6088 /// all of them, false otherwise. An access interferes with an offset-size
6089 /// pair if it might read or write that memory region.
6091 AA::RangeTy Range, function_ref<bool(const Access &, bool)> CB) const = 0;
6092
6093 /// Call \p CB on all accesses that might interfere with \p I and
6094 /// return true if all such accesses were known and the callback returned true
6095 /// for all of them, false otherwise. In contrast to forallInterferingAccesses
6096 /// this function will perform reasoning to exclude write accesses that cannot
6097 /// affect the load even if they on the surface look as if they would. The
6098 /// flag \p HasBeenWrittenTo will be set to true if we know that \p I does not
6099 /// read the initial value of the underlying memory. If \p SkipCB is given and
6100 /// returns false for a potentially interfering access, that access is not
6101 /// checked for actual interference.
6103 Attributor &A, const AbstractAttribute &QueryingAA, Instruction &I,
6104 bool FindInterferingWrites, bool FindInterferingReads,
6105 function_ref<bool(const Access &, bool)> CB, bool &HasBeenWrittenTo,
6107 function_ref<bool(const Access &)> SkipCB = nullptr) const = 0;
6108
6109 /// This function should return true if the type of the \p AA is AAPointerInfo
6110 static bool classof(const AbstractAttribute *AA) {
6111 return (AA->getIdAddr() == &ID);
6112 }
6113
6114 /// Unique ID (due to the unique address)
6115 LLVM_ABI static const char ID;
6116};
6117
6119
6120/// An abstract attribute for getting assumption information.
6122 : public StateWrapper<SetState<StringRef>, AbstractAttribute,
6123 DenseSet<StringRef>> {
6124 using Base =
6126
6128 const DenseSet<StringRef> &Known)
6129 : Base(IRP, Known) {}
6130
6131 /// Returns true if the assumption set contains the assumption \p Assumption.
6132 virtual bool hasAssumption(const StringRef Assumption) const = 0;
6133
6134 /// Create an abstract attribute view for the position \p IRP.
6136 Attributor &A);
6137
6138 /// See AbstractAttribute::getName()
6139 StringRef getName() const override { return "AAAssumptionInfo"; }
6140
6141 /// See AbstractAttribute::getIdAddr()
6142 const char *getIdAddr() const override { return &ID; }
6143
6144 /// This function should return true if the type of the \p AA is
6145 /// AAAssumptionInfo
6146 static bool classof(const AbstractAttribute *AA) {
6147 return (AA->getIdAddr() == &ID);
6148 }
6149
6150 /// Unique ID (due to the unique address)
6151 LLVM_ABI static const char ID;
6152};
6153
6154/// An abstract attribute for getting all assumption underlying objects.
6157
6158 /// See AbstractAttribute::isValidIRPositionForInit
6161 return false;
6163 }
6164
6165 /// See AbstractAttribute::requiresCallersForArgOrFunction
6166 static bool requiresCallersForArgOrFunction() { return true; }
6167
6168 /// Create an abstract attribute biew for the position \p IRP.
6170 Attributor &A);
6171
6172 /// See AbstractAttribute::getName()
6173 StringRef getName() const override { return "AAUnderlyingObjects"; }
6174
6175 /// See AbstractAttribute::getIdAddr()
6176 const char *getIdAddr() const override { return &ID; }
6177
6178 /// This function should return true if the type of the \p AA is
6179 /// AAUnderlyingObjects.
6180 static bool classof(const AbstractAttribute *AA) {
6181 return (AA->getIdAddr() == &ID);
6182 }
6183
6184 /// Unique ID (due to the unique address)
6185 LLVM_ABI static const char ID;
6186
6187 /// Check \p Pred on all underlying objects in \p Scope collected so far.
6188 ///
6189 /// This method will evaluate \p Pred on all underlying objects in \p Scope
6190 /// collected so far and return true if \p Pred holds on all of them.
6191 virtual bool
6193 AA::ValueScope Scope = AA::Interprocedural) const = 0;
6194};
6195
6196/// An abstract interface for identifying pointers from which loads can be
6197/// marked invariant.
6200
6201 /// See AbstractAttribute::isValidIRPositionForInit
6203 if (!IRP.getAssociatedType()->isPointerTy())
6204 return false;
6205
6207 }
6208
6209 /// Create an abstract attribute view for the position \p IRP.
6212
6213 /// Return true if the pointer's contents are known to remain invariant.
6214 virtual bool isKnownInvariant() const = 0;
6215 virtual bool isKnownLocallyInvariant() const = 0;
6216
6217 /// Return true if the pointer's contents are assumed to remain invariant.
6218 virtual bool isAssumedInvariant() const = 0;
6219 virtual bool isAssumedLocallyInvariant() const = 0;
6220
6221 /// See AbstractAttribute::getName().
6222 StringRef getName() const override { return "AAInvariantLoadPointer"; }
6223
6224 /// See AbstractAttribute::getIdAddr().
6225 const char *getIdAddr() const override { return &ID; }
6226
6227 /// This function should return true if the type of the \p AA is
6228 /// AAInvariantLoadPointer
6229 static bool classof(const AbstractAttribute *AA) {
6230 return (AA->getIdAddr() == &ID);
6231 }
6232
6233 /// Unique ID (due to the unique address).
6234 LLVM_ABI static const char ID;
6235};
6236
6237/// An abstract interface for address space information.
6238struct AAAddressSpace : public StateWrapper<BooleanState, AbstractAttribute> {
6241
6242 /// See AbstractAttribute::isValidIRPositionForInit
6245 return false;
6247 }
6248
6249 /// See AbstractAttribute::requiresCallersForArgOrFunction
6250 static bool requiresCallersForArgOrFunction() { return true; }
6251
6252 /// Return the address space of the associated value. \p NoAddressSpace is
6253 /// returned if the associated value is dead. This functions is not supposed
6254 /// to be called if the AA is invalid.
6255 virtual uint32_t getAddressSpace() const = 0;
6256
6257 /// Create an abstract attribute view for the position \p IRP.
6259 Attributor &A);
6260
6261 /// See AbstractAttribute::getName()
6262 StringRef getName() const override { return "AAAddressSpace"; }
6263
6264 /// See AbstractAttribute::getIdAddr()
6265 const char *getIdAddr() const override { return &ID; }
6266
6267 /// This function should return true if the type of the \p AA is
6268 /// AAAssumptionInfo
6269 static bool classof(const AbstractAttribute *AA) {
6270 return (AA->getIdAddr() == &ID);
6271 }
6272
6273 /// Unique ID (due to the unique address)
6274 LLVM_ABI static const char ID;
6275
6276protected:
6277 // Invalid address space which indicates the associated value is dead.
6278 static const uint32_t InvalidAddressSpace = ~0U;
6279};
6280
6281/// An abstract interface for potential address space information.
6283 : public StateWrapper<BooleanState, AbstractAttribute> {
6288
6289 /// See AbstractAttribute::isValidIRPositionForInit
6292 return false;
6294 }
6295
6296 /// See AbstractAttribute::requiresCallersForArgOrFunction
6297 static bool requiresCallersForArgOrFunction() { return true; }
6298
6299 /// Create an abstract attribute view for the position \p IRP.
6301 Attributor &A);
6302 /// See AbstractAttribute::getName()
6303 StringRef getName() const override { return "AANoAliasAddrSpace"; }
6304
6305 /// See AbstractAttribute::getIdAddr()
6306 const char *getIdAddr() const override { return &ID; }
6307
6308 /// This function should return true if the type of the \p AA is
6309 /// AAAssumptionInfo
6310 static bool classof(const AbstractAttribute *AA) {
6311 return (AA->getIdAddr() == &ID);
6312 }
6313
6314 /// Unique ID (due to the unique address)
6315 LLVM_ABI static const char ID;
6316
6317protected:
6320};
6321
6322struct AAAllocationInfo : public StateWrapper<BooleanState, AbstractAttribute> {
6325
6326 /// See AbstractAttribute::isValidIRPositionForInit
6329 return false;
6331 }
6332
6333 /// Create an abstract attribute view for the position \p IRP.
6335 Attributor &A);
6336
6337 virtual std::optional<TypeSize> getAllocatedSize() const = 0;
6338
6339 /// See AbstractAttribute::getName()
6340 StringRef getName() const override { return "AAAllocationInfo"; }
6341
6342 /// See AbstractAttribute::getIdAddr()
6343 const char *getIdAddr() const override { return &ID; }
6344
6345 /// This function should return true if the type of the \p AA is
6346 /// AAAllocationInfo
6347 static bool classof(const AbstractAttribute *AA) {
6348 return (AA->getIdAddr() == &ID);
6349 }
6350
6351 constexpr static const std::optional<TypeSize> HasNoAllocationSize =
6352 std::make_optional<TypeSize>(-1, true);
6353
6354 LLVM_ABI static const char ID;
6355};
6356
6357/// An abstract interface for llvm::GlobalValue information interference.
6359 : public StateWrapper<BooleanState, AbstractAttribute> {
6362
6363 /// See AbstractAttribute::isValidIRPositionForInit
6366 return false;
6367 auto *GV = dyn_cast<GlobalValue>(&IRP.getAnchorValue());
6368 if (!GV)
6369 return false;
6370 return GV->hasLocalLinkage();
6371 }
6372
6373 /// Create an abstract attribute view for the position \p IRP.
6375 Attributor &A);
6376
6377 /// Return true iff \p U is a potential use of the associated global value.
6378 virtual bool isPotentialUse(const Use &U) const = 0;
6379
6380 /// See AbstractAttribute::getName()
6381 StringRef getName() const override { return "AAGlobalValueInfo"; }
6382
6383 /// See AbstractAttribute::getIdAddr()
6384 const char *getIdAddr() const override { return &ID; }
6385
6386 /// This function should return true if the type of the \p AA is
6387 /// AAGlobalValueInfo
6388 static bool classof(const AbstractAttribute *AA) {
6389 return (AA->getIdAddr() == &ID);
6390 }
6391
6392 /// Unique ID (due to the unique address)
6393 LLVM_ABI static const char ID;
6394};
6395
6396/// An abstract interface for indirect call information interference.
6398 : public StateWrapper<BooleanState, AbstractAttribute> {
6401
6402 /// See AbstractAttribute::isValidIRPositionForInit
6405 return false;
6406 auto *CB = cast<CallBase>(IRP.getCtxI());
6407 return CB->getOpcode() == Instruction::Call && CB->isIndirectCall() &&
6408 !CB->isMustTailCall();
6409 }
6410
6411 /// Create an abstract attribute view for the position \p IRP.
6413 Attributor &A);
6414
6415 /// Call \CB on each potential callee value and return true if all were known
6416 /// and \p CB returned true on all of them. Otherwise, return false.
6417 virtual bool foreachCallee(function_ref<bool(Function *)> CB) const = 0;
6418
6419 /// See AbstractAttribute::getName()
6420 StringRef getName() const override { return "AAIndirectCallInfo"; }
6421
6422 /// See AbstractAttribute::getIdAddr()
6423 const char *getIdAddr() const override { return &ID; }
6424
6425 /// This function should return true if the type of the \p AA is
6426 /// AAIndirectCallInfo
6427 /// This function should return true if the type of the \p AA is
6428 /// AADenormalFPMath.
6429 static bool classof(const AbstractAttribute *AA) {
6430 return (AA->getIdAddr() == &ID);
6431 }
6432
6433 /// Unique ID (due to the unique address)
6434 LLVM_ABI static const char ID;
6435};
6436
6437/// An abstract Attribute for specializing "dynamic" components of
6438/// denormal_fpenv to a known denormal mode.
6440 : public StateWrapper<DenormalFPMathState, AbstractAttribute> {
6442
6444
6445 /// Create an abstract attribute view for the position \p IRP.
6447 Attributor &A);
6448
6449 /// See AbstractAttribute::getName()
6450 StringRef getName() const override { return "AADenormalFPMath"; }
6451
6452 /// See AbstractAttribute::getIdAddr()
6453 const char *getIdAddr() const override { return &ID; }
6454
6455 /// This function should return true if the type of the \p AA is
6456 /// AADenormalFPMath.
6457 static bool classof(const AbstractAttribute *AA) {
6458 return (AA->getIdAddr() == &ID);
6459 }
6460
6461 /// Unique ID (due to the unique address)
6462 LLVM_ABI static const char ID;
6463};
6464
6465/// Run options, used by the pass manager.
6476
6477namespace AA {
6478/// Helper to avoid creating an AA for IR Attributes that might already be set.
6479template <Attribute::AttrKind AK, typename AAType = AbstractAttribute>
6481 const IRPosition &IRP, DepClassTy DepClass, bool &IsKnown,
6482 bool IgnoreSubsumingPositions = false,
6483 const AAType **AAPtr = nullptr) {
6484 IsKnown = false;
6485 switch (AK) {
6486#define CASE(ATTRNAME, AANAME, ...) \
6487 case Attribute::ATTRNAME: { \
6488 if (AANAME::isImpliedByIR(A, IRP, AK, IgnoreSubsumingPositions)) \
6489 return IsKnown = true; \
6490 if (!QueryingAA) \
6491 return false; \
6492 const auto *AA = A.getAAFor<AANAME>(*QueryingAA, IRP, DepClass); \
6493 if (AAPtr) \
6494 *AAPtr = reinterpret_cast<const AAType *>(AA); \
6495 if (!AA || !AA->isAssumed(__VA_ARGS__)) \
6496 return false; \
6497 IsKnown = AA->isKnown(__VA_ARGS__); \
6498 return true; \
6499 }
6500 CASE(NoUnwind, AANoUnwind, );
6501 CASE(WillReturn, AAWillReturn, );
6502 CASE(NoFree, AANoFree, );
6503 CASE(Captures, AANoCapture, );
6504 CASE(NoRecurse, AANoRecurse, );
6505 CASE(NoReturn, AANoReturn, );
6506 CASE(NoSync, AANoSync, );
6507 CASE(NoAlias, AANoAlias, );
6508 CASE(NonNull, AANonNull, );
6509 CASE(MustProgress, AAMustProgress, );
6510 CASE(NoUndef, AANoUndef, );
6514#undef CASE
6515 default:
6516 llvm_unreachable("hasAssumedIRAttr not available for this attribute kind");
6517 };
6518}
6519} // namespace AA
6520
6521} // end namespace llvm
6522
6523#endif // LLVM_TRANSFORMS_IPO_ATTRIBUTOR_H
assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")
aarch64 promote const
uint64_t uint64_t
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
This file defines the BumpPtrAllocator interface.
static void print(raw_ostream &Out, object::Archive::Kind Kind, T Val)
This file contains the simple types necessary to represent the attributes associated with functions a...
static cl::opt< unsigned, true > MaxPotentialValues("attributor-max-potential-values", cl::Hidden, cl::desc("Maximum number of potential values to be " "tracked for each position."), cl::location(llvm::PotentialConstantIntValuesState::MaxPotentialValues), cl::init(7))
#define CASE(ATTRNAME, AANAME,...)
static const Function * getParent(const Value *V)
block Block Frequency Analysis
static GCRegistry::Add< ShadowStackGC > C("shadow-stack", "Very portable GC for uncooperative code generators")
static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")
static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
This header provides classes for managing passes over SCCs of the call graph.
This file provides interfaces used to manipulate a call graph, regardless if it is a "old style" Call...
#define LLVM_ABI
Definition Compiler.h:211
This file contains the declarations for the subclasses of Constant, which represent the different fla...
DXIL Resource Access
This file defines the DenseSet and SmallDenseSet classes.
This file defines the little GraphTraits<X> template class that should be specialized by classes that...
Module.h This file contains the declarations for the Module class.
This header defines various interfaces for pass management in LLVM.
This file implements a coalescing interval map for small objects.
Implements a lazy call graph analysis and related passes for the new pass manager.
#define F(x, y, z)
Definition MD5.cpp:54
#define I(x, y, z)
Definition MD5.cpp:57
#define G(x, y, z)
Definition MD5.cpp:55
#define H(x, y, z)
Definition MD5.cpp:56
This file implements a map that provides insertion order iteration.
This file provides utility analysis objects describing memory locations.
Contains a collection of routines for determining if a given instruction is guaranteed to execute if ...
ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))
uint64_t IntrinsicInst * II
#define P(N)
ppc ctr loops verify
Basic Register Allocator
Func getContext().diagnose(DiagnosticInfoUnsupported(Func
This file contains some templates that are useful if you are working with the STL at all.
BaseType
A given derived pointer can have multiple base pointers through phi/selects.
This file defines generic set operations that may be used on set's of different types,...
This file implements a set that has insertion order iteration characteristics.
This file defines the SmallSet class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition TapiFile.cpp:39
static unsigned getBitWidth(Type *Ty, const DataLayout &DL)
Returns the bitwidth of the given scalar or pointer type.
Value * RHS
Value * LHS
An Iterator for call edges, creates AACallEdges attributes in a lazy way.
LLVM_ABI AACallGraphNode * operator*() const
Class for arbitrary precision integers.
Definition APInt.h:78
CallBase * getInstruction() const
Return the underlying instruction.
int getCallArgOperandNo(Argument &Arg) const
Return the operand index of the underlying instruction associated with Arg.
unsigned getNumArgOperands() const
Return the number of parameters of the callee.
This class represents an incoming formal argument to a Function.
Definition Argument.h:32
const Function * getParent() const
Definition Argument.h:44
Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition ArrayRef.h:40
This represents the llvm.assume intrinsic.
This class stores enough information to efficiently remove some attributes from an existing AttrBuild...
This class holds the attributes for a particular argument, parameter, function, or return value.
Definition Attributes.h:406
Functions, function parameters, and return types can have attributes to indicate how they should be t...
Definition Attributes.h:106
static LLVM_ABI Attribute get(LLVMContext &Context, AttrKind Kind, uint64_t Val=0)
Return a uniquified Attribute object.
AttrKind
This enumeration lists the attributes that can be associated with parameters, function results,...
Definition Attributes.h:125
@ None
No attributes have been set.
Definition Attributes.h:127
static bool isEnumAttrKind(AttrKind Kind)
Definition Attributes.h:139
LLVM Basic Block Representation.
Definition BasicBlock.h:62
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
const Use & getArgOperandUse(unsigned i) const
Wrappers for getting the Use of a call argument.
Wrapper to unify "old style" CallGraph and "new style" LazyCallGraph.
This class represents a range of values.
const APInt * getSingleElement() const
If this set contains a single element, return it, otherwise return null.
LLVM_ABI bool isEmptySet() const
Return true if this set contains no members.
This is an important base class in LLVM.
Definition Constant.h:44
A parsed version of the target data layout string in and methods for querying it.
Definition DataLayout.h:64
Implements a dense probed hash-table based set.
Definition DenseSet.h:281
Analysis pass which computes a DominatorTree.
Definition Dominators.h:241
An instruction for ordering other memory operations.
Argument * arg_iterator
Definition Function.h:73
AttributeList getAttributes() const
Return the attribute list for this Function.
Definition Function.h:329
void setAttributes(AttributeList Attrs)
Set the attribute list for this Function.
Definition Function.h:332
Type * getReturnType() const
Returns the type of the ret val.
Definition Function.h:217
bool hasFnAttribute(Attribute::AttrKind Kind) const
Return true if the function has the attribute.
Definition Function.cpp:731
bool hasLocalLinkage() const
typename Sizer::Allocator Allocator
Invoke instruction.
This is an important class for using LLVM in a threaded context.
Definition LLVMContext.h:76
An SCC of the call graph.
A lazily constructed view of the call graph of a module.
An instruction for reading from memory.
Analysis pass that exposes the LoopInfo for a function.
Definition LoopInfo.h:594
This class implements a map that also provides access to all stored values in a deterministic order.
Definition MapVector.h:38
bool onlyReadsMemory() const
Whether this function only (at most) reads memory.
Definition ModRef.h:249
static MemoryEffectsBase unknown()
Definition ModRef.h:123
A Module instance is used to store all the information related to an LLVM module.
Definition Module.h:68
The optimization diagnostic interface.
Pass interface - Implemented by all 'passes'.
Definition Pass.h:97
PointerIntPair - This class implements a pair of a pointer and small integer.
PointerTy getPointer() const
void setFromOpaqueValue(void *Val) &
Analysis pass which computes a PostDominatorTree.
A set of analyses that are preserved following a run of a transformation pass.
Definition Analysis.h:112
A vector that has set insertion semantics.
Definition SetVector.h:57
size_type size() const
Determine the number of elements in the SetVector.
Definition SetVector.h:103
typename vector_type::const_iterator iterator
Definition SetVector.h:72
bool empty() const
Determine if the SetVector is empty or not.
Definition SetVector.h:100
iterator begin()
Get an iterator to the beginning of the SetVector.
Definition SetVector.h:112
A templated base class for SmallPtrSet which provides the typesafe interface that is common across al...
iterator find(ConstPtrType Ptr) const
SmallPtrSet - This class implements a set which is optimized for holding SmallSize or less elements.
A SetVector that performs no allocations if smaller than a certain size.
Definition SetVector.h:345
SmallSet - This maintains a set of unique values, optimizing for the case when the set is small (less...
Definition SmallSet.h:134
SmallSetIterator< int64_t, N, std::less< int64_t > > const_iterator
Definition SmallSet.h:150
std::pair< const_iterator, bool > insert(const T &V)
insert - Insert an element into the set if it isn't already there.
Definition SmallSet.h:184
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
void push_back(const T &Elt)
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
An instruction for storing to memory.
Represent a constant reference to a string, i.e.
Definition StringRef.h:56
std::string str() const
Get the contents as an std::string.
Definition StringRef.h:222
bool starts_with(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition StringRef.h:258
LLVM_ABI SubsumingPositionIterator(const IRPosition &IRP)
Analysis pass providing the TargetLibraryInfo.
Provides information about what library functions are available for the current target.
The TimeTraceScope is a helper class to call the begin and end functions of the time trace profiler.
The instances of the Type class are immutable: once they are created, they are never changed.
Definition Type.h:46
bool isPointerTy() const
True if this is an instance of PointerType.
Definition Type.h:277
bool isPtrOrPtrVectorTy() const
Return true if this is a pointer type or a vector of pointer types.
Definition Type.h:280
bool isIntegerTy() const
True if this is an instance of IntegerType.
Definition Type.h:252
static LLVM_ABI UndefValue * get(Type *T)
Static factory methods - Return an 'undef' object of the specified type.
A Use represents the edge between a Value definition and its users.
Definition Use.h:35
LLVM Value Representation.
Definition Value.h:75
Type * getType() const
All values are typed, get the type of this value.
Definition Value.h:257
LLVMContext & getContext() const
All values hold a context through their type.
Definition Value.h:260
LLVM_ABI const Value * stripPointerCasts() const
Strip off pointer casts, all-zero GEPs and address space casts.
Definition Value.cpp:708
LLVM_ABI StringRef getName() const
Return a constant reference to the value's name.
Definition Value.cpp:315
An efficient, type-erasing, non-owning reference to a callable.
A range adaptor for a pair of iterators.
This class implements an extremely fast bulk output stream that can only output to a stream.
Definition raw_ostream.h:53
Changed
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
Abstract Attribute helper functions.
Definition Attributor.h:164
LLVM_ABI bool isAssumedReadNone(Attributor &A, const IRPosition &IRP, const AbstractAttribute &QueryingAA, bool &IsKnown)
Return true if IRP is readnone.
LLVM_ABI bool isAssumedReadOnly(Attributor &A, const IRPosition &IRP, const AbstractAttribute &QueryingAA, bool &IsKnown)
Return true if IRP is readonly.
raw_ostream & operator<<(raw_ostream &OS, const RangeTy &R)
Definition Attributor.h:326
LLVM_ABI std::optional< Value * > combineOptionalValuesInAAValueLatice(const std::optional< Value * > &A, const std::optional< Value * > &B, Type *Ty)
Return the combination of A and B such that the result is a possible value of both.
LLVM_ABI bool isValidAtPosition(const ValueAndContext &VAC, InformationCache &InfoCache)
Return true if the value of VAC is a valid at the position of VAC, that is a constant,...
LLVM_ABI bool isAssumedThreadLocalObject(Attributor &A, Value &Obj, const AbstractAttribute &QueryingAA)
Return true if Obj is assumed to be a thread local object.
LLVM_ABI bool isGPUConstantAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU constant address space for the target triple...
LLVM_ABI bool isDynamicallyUnique(Attributor &A, const AbstractAttribute &QueryingAA, const Value &V, bool ForAnalysisOnly=true)
Return true if V is dynamically unique, that is, there are no two "instances" of V at runtime with di...
LLVM_ABI bool getPotentialCopiesOfStoredValue(Attributor &A, StoreInst &SI, SmallSetVector< Value *, 4 > &PotentialCopies, const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation, bool OnlyExact=false)
Collect all potential values of the one stored by SI into PotentialCopies.
LLVM_ABI bool isGPUSharedAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU shared address space for the target triple i...
bool operator!=(const RangeTy &A, const RangeTy &B)
Definition Attributor.h:335
LLVM_ABI bool isGPULocalAddressSpace(const Module &M, unsigned AS)
Check if the given address space AS corresponds to a GPU local/private address space for the target t...
LLVM_ABI bool isPotentiallyAffectedByBarrier(Attributor &A, const Instruction &I, const AbstractAttribute &QueryingAA)
Return true if I is potentially affected by a barrier.
SmallPtrSet< Instruction *, 4 > InstExclusionSetTy
Definition Attributor.h:165
LLVM_ABI bool isGPU(const Module &M)
Return true iff M target a GPU (and we can use GPU AS reasoning).
LLVM_ABI Constant * getInitialValueForObj(Attributor &A, const AbstractAttribute &QueryingAA, Value &Obj, Type &Ty, const TargetLibraryInfo *TLI, const DataLayout &DL, RangeTy *RangePtr=nullptr)
Return the initial value of Obj with type Ty if that is a constant.
bool operator==(const RangeTy &A, const RangeTy &B)
Definition Attributor.h:331
ValueScope
Flags to distinguish intra-procedural queries from potentially inter-procedural queries.
Definition Attributor.h:185
@ Intraprocedural
Definition Attributor.h:186
@ Interprocedural
Definition Attributor.h:187
LLVM_ABI bool isValidInScope(const Value &V, const Function *Scope)
Return true if V is a valid value in Scope, that is a constant or an instruction/argument of Scope.
LLVM_ABI bool isPotentiallyReachable(Attributor &A, const Instruction &FromI, const Instruction &ToI, const AbstractAttribute &QueryingAA, const AA::InstExclusionSetTy *ExclusionSet=nullptr, std::function< bool(const Function &F)> GoBackwardsCB=nullptr)
Return true if ToI is potentially reachable from FromI without running into any instruction in Exclus...
LLVM_ABI bool isNoSyncInst(Attributor &A, const Instruction &I, const AbstractAttribute &QueryingAA)
Return true if I is a nosync instruction.
bool hasAssumedIRAttr(Attributor &A, const AbstractAttribute *QueryingAA, const IRPosition &IRP, DepClassTy DepClass, bool &IsKnown, bool IgnoreSubsumingPositions=false, const AAType **AAPtr=nullptr)
Helper to avoid creating an AA for IR Attributes that might already be set.
LLVM_ABI bool getPotentiallyLoadedValues(Attributor &A, LoadInst &LI, SmallSetVector< Value *, 4 > &PotentialValues, SmallSetVector< Instruction *, 4 > &PotentialValueOrigins, const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation, bool OnlyExact=false)
Collect all potential values LI could read into PotentialValues.
LLVM_ABI Value * getWithType(Value &V, Type &Ty)
Try to convert V to type Ty without introducing new instructions.
E & operator^=(E &LHS, E RHS)
NodeAddr< UseNode * > Use
Definition RDFGraph.h:385
This is an optimization pass for GlobalISel generic memory operations.
@ Offset
Definition DWP.cpp:577
bool isEqual(const GCNRPTracker::LiveRegSet &S1, const GCNRPTracker::LiveRegSet &S2)
auto size(R &&Range, std::enable_if_t< std::is_base_of< std::random_access_iterator_tag, typename std::iterator_traits< decltype(Range.begin())>::iterator_category >::value, void > *=nullptr)
Get the size of a range.
Definition STLExtras.h:1685
LLVM_ABI unsigned MaxInitializationChainLength
The value passed to the line option that defines the maximal initialization chain length.
RelativeUniformCounterPtr Values
Definition InstrProf.h:91
APInt operator&(APInt a, const APInt &b)
Definition APInt.h:2149
@ Known
Known to have no common set bits.
void set_intersect(S1Ty &S1, const S2Ty &S2)
set_intersect(A, B) - Compute A := A ^ B Identical to set_intersection, except that it works on set<>...
decltype(auto) dyn_cast(const From &Val)
dyn_cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:643
bool operator!=(uint64_t V1, const APInt &V2)
Definition APInt.h:2139
auto dyn_cast_if_present(const Y &Val)
dyn_cast_if_present<X> - Functionally identical to dyn_cast, except that a null (or none in the case ...
Definition Casting.h:732
LLVM_ATTRIBUTE_ALWAYS_INLINE DynamicAPInt & operator+=(DynamicAPInt &A, int64_t B)
bool set_is_subset(const S1Ty &S1, const S2Ty &S2)
set_is_subset(A, B) - Return true iff A in B
auto cast_or_null(const Y &Val)
Definition Casting.h:714
MemoryEffectsBase< IRMemLocation > MemoryEffects
Summary of how a function affects memory in the program.
Definition ModRef.h:356
bool isa_and_nonnull(const Y &Val)
Definition Casting.h:676
bool operator==(const AddressRangeValuePair &LHS, const AddressRangeValuePair &RHS)
AttributorRunOption
Run options, used by the pass manager.
@ CGSCC_LIGHT
@ MODULE_LIGHT
RelativeUniformCounterPtr ValuesPtrExpr VTableAddr Value
Definition InstrProf.h:143
AnalysisManager< LazyCallGraph::SCC, LazyCallGraph & > CGSCCAnalysisManager
The CGSCC analysis manager.
LLVM_ABI bool canSimplifyInvokeNoUnwind(const Function *F)
LLVM_ABI bool shouldCreateAbstractAttribute()
Check the debug counter used to limit the number of AAs created.
DenseMap< RetainedKnowledgeKey, Assume2KnowledgeMap > RetainedKnowledgeMap
PotentialValuesState< std::pair< AA::ValueAndContext, AA::ValueScope > > PotentialLLVMValuesState
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
FPClassTest
Floating-point class tests, supported by 'is_fpclass' intrinsic.
LLVM_ABI raw_ostream & dbgs()
dbgs() - This returns a reference to a raw_ostream for debugging messages.
Definition Debug.cpp:209
bool set_union(S1Ty &S1, const S2Ty &S2)
set_union(A, B) - Compute A := A u B, return whether A changed.
class LLVM_GSL_OWNER SmallVector
Forward declaration of SmallVector so that calculateSmallVectorDefaultInlinedElements can reference s...
bool isa(const From &Val)
isa<X> - Return true if the parameter to the template is an instance of one of the template type argu...
Definition Casting.h:547
@ Success
The lock was released successfully.
iterator_range(Container &&) -> iterator_range< llvm::detail::IterOfRange< Container > >
@ Other
Any other memory.
Definition ModRef.h:68
PotentialValuesState< APInt > PotentialConstantIntValuesState
bool operator&=(SparseBitVector< ElementSize > *LHS, const SparseBitVector< ElementSize > &RHS)
raw_ostream & operator<<(raw_ostream &OS, const APFixedPoint &FX)
ChangeStatus clampStateAndIndicateChange(StateType &S, const StateType &R)
Helper function to clamp a state S of type StateType with the information in R and indicate/return if...
OutputIt move(R &&Range, OutputIt Out)
Provide wrappers to std::move which take ranges instead of having to pass begin/end explicitly.
Definition STLExtras.h:1933
decltype(auto) cast(const From &Val)
cast<X> - Return the argument parameter cast to the specified type.
Definition Casting.h:559
iterator_range< pointer_iterator< WrappedIteratorT > > make_pointer_range(RangeT &&Range)
Definition iterator.h:368
ChangeStatus
{
Definition Attributor.h:476
bool operator|=(SparseBitVector< ElementSize > &LHS, const SparseBitVector< ElementSize > *RHS)
@ OPTIONAL
The target may be valid if the source is not.
Definition Attributor.h:488
@ NONE
Do not track a dependence between source and target.
Definition Attributor.h:489
@ REQUIRED
The target cannot be valid if the source is not.
Definition Attributor.h:487
AnalysisManager< Function > FunctionAnalysisManager
Convenience typedef for the Function analysis manager.
APInt operator|(APInt a, const APInt &b)
Definition APInt.h:2169
BumpPtrAllocatorImpl<> BumpPtrAllocator
The standard BumpPtrAllocator which just uses the default template parameters.
Definition Allocator.h:391
IncIntegerState< uint64_t, Value::MaximumAlignment, 1 > AAAlignmentStateType
AnalysisManager< Module > ModuleAnalysisManager
Convenience typedef for the Module analysis manager.
Definition MIRParser.h:39
Implement std::hash so that hash_code can be used in STL containers.
Definition BitVector.h:878
static LLVM_ABI AAAddressSpace & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
StringRef getName() const override
See AbstractAttribute::getName()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAAssumptionInfo.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
virtual uint32_t getAddressSpace() const =0
Return the address space of the associated value.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
AAAddressSpace(const IRPosition &IRP, Attributor &A)
static const uint32_t InvalidAddressSpace
AAAlign(const IRPosition &IRP, Attributor &A)
static LLVM_ABI AAAlign & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
Align getAssumedAlign() const
Return assumed alignment.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAAlign.
Align getKnownAlign() const
Return known alignment.
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI const char ID
virtual std::optional< TypeSize > getAllocatedSize() const =0
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI AAAllocationInfo & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
StringRef getName() const override
See AbstractAttribute::getName()
AAAllocationInfo(const IRPosition &IRP, Attributor &A)
static constexpr const std::optional< TypeSize > HasNoAllocationSize
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAAllocationInfo.
AAAssumptionInfo(const IRPosition &IRP, Attributor &A, const DenseSet< StringRef > &Known)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI AAAssumptionInfo & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
StateWrapper< SetState< StringRef >, AbstractAttribute, DenseSet< StringRef > > Base
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAAssumptionInfo.
virtual bool hasAssumption(const StringRef Assumption) const =0
Returns true if the assumption set contains the assumption Assumption.
An abstract state for querying live call edges.
AACallEdges(const IRPosition &IRP, Attributor &A)
virtual const SetVector< Function * > & getOptimisticEdges() const =0
Get the optimistic edges.
static bool requiresNonAsmForCallBase()
See AbstractAttribute::requiresNonAsmForCallBase.
StateWrapper< BooleanState, AbstractAttribute > Base
AACallEdgeIterator optimisticEdgesBegin() const override
Iterator for exploring the call graph.
virtual bool hasUnknownCallee() const =0
Is there any call with a unknown callee.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
StringRef getName() const override
See AbstractAttribute::getName()
virtual bool hasNonAsmUnknownCallee() const =0
Is there any call with a unknown callee, excluding any inline asm.
AACallEdgeIterator optimisticEdgesEnd() const override
Iterator for exploring the call graph.
static LLVM_ABI AACallEdges & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AACallEdges.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
iterator_range< AACallEdgeIterator > optimisticEdgesRange() const
Iterator range for exploring the call graph.
virtual AACallEdgeIterator optimisticEdgesBegin() const =0
AACallGraphNode(Attributor &A)
virtual AACallEdgeIterator optimisticEdgesEnd() const =0
virtual ~AACallGraphNode()=default
Attributor & A
Reference to Attributor needed for GraphTraits implementation.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
StringRef getName() const override
See AbstractAttribute::getName()
AADenormalFPMath(const IRPosition &IRP, Attributor &A)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
StateWrapper< DenormalFPMathState, AbstractAttribute > Base
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AADenormalFPMath.
static LLVM_ABI AADenormalFPMath & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static AbstractAttribute * DepGetValAA(const DepTy &DT)
Definition Attributor.h:506
friend struct Attributor
Definition Attributor.h:527
mapped_iterator< DepSetTy::iterator, decltype(&DepGetVal)> iterator
Definition Attributor.h:513
mapped_iterator< DepSetTy::iterator, decltype(&DepGetValAA)> aaiterator
Definition Attributor.h:514
virtual ~AADepGraphNode()=default
SmallSetVector< DepTy, 2 > DepSetTy
Definition Attributor.h:498
static AADepGraphNode * DepGetVal(const DepTy &DT)
Definition Attributor.h:505
iterator child_begin()
Definition Attributor.h:519
DepSetTy Deps
Set of dependency graph nodes which should be updated if this one is updated.
Definition Attributor.h:503
friend struct AADepGraph
Definition Attributor.h:528
virtual void print(Attributor *, raw_ostream &OS) const
Definition Attributor.h:523
aaiterator begin()
Definition Attributor.h:517
PointerIntPair< AADepGraphNode *, 1 > DepTy
Definition Attributor.h:497
void print(raw_ostream &OS) const
Definition Attributor.h:522
The data structure for the dependency graph.
Definition Attributor.h:536
~AADepGraph()=default
iterator begin()
Definition Attributor.h:551
mapped_iterator< AADepGraphNode::DepSetTy::iterator, decltype(&DepGetVal)> iterator
Definition Attributor.h:542
LLVM_ABI void viewGraph()
AADepGraphNode SyntheticRoot
There is no root node for the dependency graph.
Definition Attributor.h:548
LLVM_ABI void print()
Print dependency graph.
static AADepGraphNode * DepGetVal(const DepTy &DT)
Definition Attributor.h:541
iterator end()
Definition Attributor.h:552
LLVM_ABI void dumpGraph()
Dump graph to file.
AADepGraphNode * GetEntryNode()
Definition Attributor.h:549
AADepGraph()=default
AADepGraphNode::DepTy DepTy
Definition Attributor.h:540
uint32_t getKnownDereferenceableBytes() const
Return known dereferenceable bytes.
bool isAssumedGlobal() const
Return true if we assume that underlying value is dereferenceable(_or_null) globally.
bool isKnownGlobal() const
Return true if we know that underlying value is dereferenceable(_or_null) globally.
AADereferenceable(const IRPosition &IRP, Attributor &A)
uint32_t getAssumedDereferenceableBytes() const
Return assumed dereferenceable bytes.
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AADereferenceable.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
static LLVM_ABI AADereferenceable & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
Summary about the execution domain of a block or instruction.
void addAssumeInst(Attributor &A, AssumeInst &AI)
SmallPtrSet< AssumeInst *, 4 > AssumesSetTy
void addAlignedBarrier(Attributor &A, CallBase &CB)
SmallPtrSet< CallBase *, 2 > BarriersSetTy
virtual bool isExecutedByInitialThreadOnly(const BasicBlock &) const =0
Check if a basic block is executed only by the initial thread.
StateWrapper< BooleanState, AbstractAttribute > Base
bool isExecutedByInitialThreadOnly(const Instruction &I) const
Check if an instruction is executed only by the initial thread.
static LLVM_ABI AAExecutionDomain & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAExecutionDomain.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr().
virtual ExecutionDomainTy getFunctionExecutionDomain() const =0
virtual ExecutionDomainTy getExecutionDomain(const BasicBlock &) const =0
virtual bool isExecutedInAlignedRegion(Attributor &A, const Instruction &I) const =0
Check if the instruction I is executed in an aligned region, that is, the synchronizing effects befor...
AAExecutionDomain(const IRPosition &IRP, Attributor &A)
virtual bool isNoOpFence(const FenceInst &FI) const =0
Helper function to determine if FI is a no-op given the information about its execution from ExecDoma...
StringRef getName() const override
See AbstractAttribute::getName().
virtual std::pair< ExecutionDomainTy, ExecutionDomainTy > getExecutionDomain(const CallBase &CB) const =0
Return the execution domain with which the call CB is entered and the one with which it is left.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAGlobalValueInfo.
AAGlobalValueInfo(const IRPosition &IRP, Attributor &A)
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI AAGlobalValueInfo & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
virtual bool isPotentialUse(const Use &U) const =0
Return true iff U is a potential use of the associated global value.
StringRef getName() const override
See AbstractAttribute::getName()
StateWrapper< BooleanState, AbstractAttribute > Base
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAHeapToStack.
virtual bool isAssumedHeapToStack(const CallBase &CB) const =0
Returns true if HeapToStack conversion is assumed to be possible.
virtual bool isAssumedHeapToStackRemovedFree(CallBase &CB) const =0
Returns true if HeapToStack conversion is assumed and the CB is a callsite to a free operation to be ...
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI AAHeapToStack & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AAHeapToStack(const IRPosition &IRP, Attributor &A)
StringRef getName() const override
See AbstractAttribute::getName()
virtual bool foreachCallee(function_ref< bool(Function *)> CB) const =0
Call \CB on each potential callee value and return true if all were known and CB returned true on all...
AAIndirectCallInfo(const IRPosition &IRP, Attributor &A)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAIndirectCallInfo This function should ret...
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI AAIndirectCallInfo & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
StringRef getName() const override
See AbstractAttribute::getName()
AAInstanceInfo(const IRPosition &IRP, Attributor &A)
bool isAssumedUniqueForAnalysis() const
Return true if we assume that the underlying value is unique in its scope wrt.
static LLVM_ABI AAInstanceInfo & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAInstanceInfo.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AACallEdges.
static LLVM_ABI AAInterFnReachability & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
bool canReach(Attributor &A, const Function &Fn) const
If the function represented by this possition can reach Fn.
StringRef getName() const override
See AbstractAttribute::getName()
virtual bool instructionCanReach(Attributor &A, const Instruction &Inst, const Function &Fn, const AA::InstExclusionSetTy *ExclusionSet=nullptr) const =0
Can Inst reach Fn.
AAInterFnReachability(const IRPosition &IRP, Attributor &A)
StateWrapper< BooleanState, AbstractAttribute > Base
static LLVM_ABI AAIntraFnReachability & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAIntraFnReachability.
AAIntraFnReachability(const IRPosition &IRP, Attributor &A)
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
virtual bool isAssumedReachable(Attributor &A, const Instruction &From, const Instruction &To, const AA::InstExclusionSetTy *ExclusionSet=nullptr) const =0
Returns true if 'From' instruction is assumed to reach, 'To' instruction.
StateWrapper< BooleanState, AbstractAttribute > Base
An abstract interface for identifying pointers from which loads can be marked invariant.
virtual bool isAssumedInvariant() const =0
Return true if the pointer's contents are assumed to remain invariant.
static LLVM_ABI AAInvariantLoadPointer & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
virtual bool isAssumedLocallyInvariant() const =0
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAInvariantLoadPointer.
static LLVM_ABI const char ID
Unique ID (due to the unique address).
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr().
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
StringRef getName() const override
See AbstractAttribute::getName().
virtual bool isKnownInvariant() const =0
Return true if the pointer's contents are known to remain invariant.
AAInvariantLoadPointer(const IRPosition &IRP)
virtual bool isKnownLocallyInvariant() const =0
An abstract interface for liveness abstract attribute.
friend struct Attributor
virtual bool isKnownDead(const BasicBlock *BB) const =0
Returns true if BB is known dead.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAIsDead.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
virtual bool isKnownDead() const =0
Returns true if the underlying value is known dead.
virtual bool isEdgeDead(const BasicBlock *From, const BasicBlock *To) const
Return if the edge from From BB to To BB is assumed dead.
virtual bool isAssumedDead(const Instruction *I) const =0
Returns true if I is assumed dead.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
StringRef getName() const override
See AbstractAttribute::getName()
virtual bool isAssumedDead() const =0
The query functions are protected such that other attributes need to go through the Attributor interf...
virtual bool isRemovableStore() const
Return true if the underlying value is a store that is known to be removable.
virtual bool isAssumedDead(const BasicBlock *BB) const =0
Returns true if BB is assumed dead.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
StateWrapper< BitIntegerState< uint8_t, 3, 0 >, AbstractAttribute > Base
static LLVM_ABI AAIsDead & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool mayCatchAsynchronousExceptions(const Function &F)
Determine if F might catch asynchronous exceptions.
AAIsDead(const IRPosition &IRP, Attributor &A)
virtual bool isKnownDead(const Instruction *I) const =0
Returns true if I is known dead.
An abstract interface for memory access kind related attributes (readnone/readonly/writeonly).
StringRef getName() const override
See AbstractAttribute::getName()
bool isAssumedReadOnly() const
Return true if we assume that the underlying value is not accessed (=written) in its respective scope...
static LLVM_ABI AAMemoryBehavior & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool hasTrivialInitializer()
See AbstractAttribute::hasTrivialInitializer.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAMemoryBehavior.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
bool isKnownReadNone() const
Return true if we know that the underlying value is not read or accessed in its respective scope.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
bool isKnownWriteOnly() const
Return true if we know that the underlying value is not read in its respective scope.
bool isAssumedReadNone() const
Return true if we assume that the underlying value is not read or accessed in its respective scope.
bool isAssumedWriteOnly() const
Return true if we assume that the underlying value is not read in its respective scope.
AAMemoryBehavior(const IRPosition &IRP, Attributor &A)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
bool isKnownReadOnly() const
Return true if we know that the underlying value is not accessed (=written) in its respective scope.
StringRef getName() const override
See AbstractAttribute::getName()
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
bool isAssumedStackOnly() const
Return true if we assume that the associated functions has at most local/stack accesses.
static LLVM_ABI std::string getMemoryLocationsAsStr(MemoryLocationsKind MLK)
Return the locations encoded by MLK as a readable string.
bool isKnownArgMemOnly() const
Return true if we know that the underlying value will only access argument pointees (see Attribute::A...
bool isKnownInaccessibleOrArgMemOnly() const
Return true if we know that the underlying value will only access inaccesible memory or argument poin...
static LLVM_ABI const char ID
Unique ID (due to the unique address)
AAMemoryLocation(const IRPosition &IRP, Attributor &A)
bool isKnownReadNone() const
Return true if we know that the associated functions has no observable accesses.
bool isAssumedSpecifiedMemOnly(MemoryLocationsKind MLK) const
Return true if only the memory locations specififed by MLK are assumed to be accessed by the associat...
bool isAssumedInaccessibleMemOnly() const
Return true if we assume that the underlying value will only access inaccesible memory only (see Attr...
bool isKnownInaccessibleMemOnly() const
Return true if we know that the underlying value will only access inaccesible memory only (see Attrib...
static LLVM_ABI AAMemoryLocation & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
bool isAssumedInaccessibleOrArgMemOnly() const
Return true if we assume that the underlying value will only access inaccesible memory or argument po...
bool isKnowStackOnly() const
Return true if we know that the associated functions has at most local/stack accesses.
static bool requiresCalleeForCallBase()
See AbstractAttribute::requiresCalleeForCallBase.
AccessKind
Simple enum to distinguish read/write/read-write accesses.
StateType::base_t MemoryLocationsKind
MemoryLocationsKind getAssumedNotAccessedLocation() const
Return the locations that are assumed to be not accessed by the associated function,...
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAMemoryLocation.
static bool hasTrivialInitializer()
See AbstractAttribute::hasTrivialInitializer.
bool isAssumedReadNone() const
Return true if we assume that the associated functions has no observable accesses.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
virtual bool checkForAllAccessesToMemoryKind(function_ref< bool(const Instruction *, const Value *, AccessKind, MemoryLocationsKind)> Pred, MemoryLocationsKind MLK) const =0
Check Pred on all accesses to the memory kinds specified by MLK.
const std::string getAsStr(Attributor *A) const override
See AbstractState::getAsStr(Attributor).
bool mayAccessArgMem() const
Return true if the underlying value may access memory through arguement pointers of the associated fu...
bool isAssumedArgMemOnly() const
Return true if we assume that the underlying value will only access argument pointees (see Attribute:...
static MemoryLocationsKind inverseLocation(MemoryLocationsKind Loc, bool AndLocalMem, bool AndConstMem)
Return the inverse of location Loc, thus for NO_XXX the return describes ONLY_XXX.
An abstract interface for all nonnull attributes.
StringRef getName() const override
See AbstractAttribute::getName()
bool isKnownMustProgress() const
Return true if we know that underlying value is nonnull.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAMustProgress.
bool isAssumedMustProgress() const
Return true if we assume that the underlying value is nonnull.
static bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI AAMustProgress & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
AAMustProgress(const IRPosition &IRP, Attributor &A)
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAAssumptionInfo.
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
IntervalMap< unsigned, bool > RangeMap
static LLVM_ABI AANoAliasAddrSpace & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
AANoAliasAddrSpace(const IRPosition &IRP, Attributor &A)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
StateWrapper< BooleanState, AbstractAttribute > Base
RangeMap::Allocator Allocator
StringRef getName() const override
See AbstractAttribute::getName()
An abstract interface for all noalias attributes.
static LLVM_ABI bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
See IRAttribute::isImpliedByIR.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoAlias.
StringRef getName() const override
See AbstractAttribute::getName()
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
bool isKnownNoAlias() const
Return true if we know that underlying value is noalias.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
bool isAssumedNoAlias() const
Return true if we assume that the underlying value is alias.
static LLVM_ABI AANoAlias & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AANoAlias(const IRPosition &IRP, Attributor &A)
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
An abstract interface for all nocapture attributes.
static LLVM_ABI AANoCapture & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
@ NO_CAPTURE_MAYBE_RETURNED
If we do not capture the value in memory or through integers we can only communicate it back as a der...
@ NO_CAPTURE
If we do not capture the value in memory, through integers, or as a derived pointer we know it is not...
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
AANoCapture(const IRPosition &IRP, Attributor &A)
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
bool isAssumedNoCaptureMaybeReturned() const
Return true if we assume that the underlying value is not captured in its respective scope but we all...
bool isKnownNoCapture() const
Return true if we know that the underlying value is not captured in its respective scope.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoCapture.
bool isKnownNoCaptureMaybeReturned() const
Return true if we know that the underlying value is not captured in its respective scope but we allow...
static LLVM_ABI bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
See IRAttribute::isImpliedByIR.
bool isAssumedNoCapture() const
Return true if we assume that the underlying value is not captured in its respective scope.
static LLVM_ABI void determineFunctionCaptureCapabilities(const IRPosition &IRP, const Function &F, BitIntegerState &State)
Update State according to the capture capabilities of F for position IRP.
FPClassTest getAssumedNoFPClass() const
Return the underlying assumed nofpclass.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
AANoFPClass(const IRPosition &IRP, Attributor &A)
StringRef getName() const override
See AbstractAttribute::getName()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoFPClass.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
FPClassTest getKnownNoFPClass() const
Return the underlying known nofpclass.
StateWrapper< BitIntegerState< uint32_t, fcAllFlags, fcNone >, AbstractAttribute > Base
static LLVM_ABI AANoFPClass & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
An AbstractAttribute for nofree.
bool isKnownNoFree() const
Return true if "nofree" is known.
static LLVM_ABI AANoFree & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AANoFree(const IRPosition &IRP, Attributor &A)
static bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
See IRAttribute::isImpliedByIR.
bool isAssumedNoFree() const
Return true if "nofree" is assumed.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoFree.
StringRef getName() const override
See AbstractAttribute::getName()
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
An abstract attribute for norecurse.
StringRef getName() const override
See AbstractAttribute::getName()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoRecurse.
AANoRecurse(const IRPosition &IRP, Attributor &A)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
bool isAssumedNoRecurse() const
Return true if "norecurse" is assumed.
static LLVM_ABI AANoRecurse & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
bool isKnownNoRecurse() const
Return true if "norecurse" is known.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
An AbstractAttribute for noreturn.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoReturn.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
bool isAssumedNoReturn() const
Return true if the underlying object is assumed to never return.
static LLVM_ABI AANoReturn & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
StringRef getName() const override
See AbstractAttribute::getName()
AANoReturn(const IRPosition &IRP, Attributor &A)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
bool isKnownNoReturn() const
Return true if the underlying object is known to never return.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
AANoSync(const IRPosition &IRP, Attributor &A)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
StringRef getName() const override
See AbstractAttribute::getName()
static bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI bool isAlignedBarrier(const CallBase &CB, bool ExecutedAligned)
Helper function to determine if CB is an aligned (GPU) barrier.
bool isAssumedNoSync() const
Returns true if "nosync" is assumed.
static LLVM_ABI bool isNonRelaxedAtomic(const Instruction *I)
Helper function used to determine whether an instruction is non-relaxed atomic.
bool isKnownNoSync() const
Returns true if "nosync" is known.
static LLVM_ABI AANoSync & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoSync.
An abstract interface for all noundef attributes.
bool isKnownNoUndef() const
Return true if we know that underlying value is noundef.
static bool isImpliedByUndef()
See IRAttribute::isImpliedByUndef.
StringRef getName() const override
See AbstractAttribute::getName()
bool isAssumedNoUndef() const
Return true if we assume that the underlying value is noundef.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoUndef.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI AANoUndef & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AANoUndef(const IRPosition &IRP, Attributor &A)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static bool isImpliedByPoison()
See IRAttribute::isImpliedByPoison.
static LLVM_ABI bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
See IRAttribute::isImpliedByIR.
AANoUnwind(const IRPosition &IRP, Attributor &A)
bool isAssumedNoUnwind() const
Returns true if nounwind is assumed.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANoUnwind.
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI AANoUnwind & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
bool isKnownNoUnwind() const
Returns true if nounwind is known.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
StateWrapper< BooleanState, AbstractAttribute > Base
static LLVM_ABI AANonConvergent & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
StringRef getName() const override
See AbstractAttribute::getName()
bool isAssumedNotConvergent() const
Return true if "non-convergent" is assumed.
bool isKnownNotConvergent() const
Return true if "non-convergent" is known.
AANonConvergent(const IRPosition &IRP, Attributor &A)
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANonConvergent.
An abstract interface for all nonnull attributes.
static bool isImpliedByUndef()
See IRAttribute::isImpliedByUndef.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AANonNull.
static LLVM_ABI AANonNull & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AANonNull(const IRPosition &IRP, Attributor &A)
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
See AbstractAttribute::isImpliedByIR(...).
bool isAssumedNonNull() const
Return true if we assume that the underlying value is nonnull.
bool isKnownNonNull() const
Return true if we know that underlying value is nonnull.
static bool hasTrivialInitializer()
See AbstractAttribute::hasTrivialInitializer.
An access description.
bool isWrittenValueUnknown() const
Return true if the value written cannot be determined at all.
const_iterator end() const
bool operator!=(const Access &R) const
std::optional< Value * > getContent() const
Return the written value which can be llvm::null if it is not yet determined.
Access & operator=(const Access &Other)=default
bool isAssumption() const
Return true if this is an assumption access.
const RangeList & getRanges() const
bool isWriteOrAssumption() const
Return true if this is a write access.
bool isRead() const
Return true if this is a read access.
bool isWrite() const
Return true if this is a write access.
Value * getWrittenValue() const
Return the value writen, if any.
Instruction * getLocalInst() const
Return the instruction that causes the access with respect to the local scope of the associated attri...
Access(Instruction *LocalI, Instruction *RemoteI, const RangeList &Ranges, std::optional< Value * > Content, AccessKind K, Type *Ty)
Access(Instruction *LocalI, Instruction *RemoteI, int64_t Offset, int64_t Size, std::optional< Value * > Content, AccessKind Kind, Type *Ty)
Access(Instruction *I, int64_t Offset, int64_t Size, std::optional< Value * > Content, AccessKind Kind, Type *Ty)
Access(const Access &Other)=default
Type * getType() const
Return the type associated with the access, if known.
Access & operator&=(const Access &R)
const_iterator begin() const
RangeList::const_iterator const_iterator
Instruction * getRemoteInst() const
Return the actual instruction that causes the access.
bool operator==(const Access &R) const
bool isWrittenValueYetUndetermined() const
Return true if the value written is not known yet.
AccessKind getKind() const
Return the access kind.
A helper containing a list of offsets computed for a Use.
SmallSet< int64_t, 4 > VecTy
VecTy::const_iterator const_iterator
bool operator==(const OffsetInfo &RHS) const
bool insert(int64_t Offset)
bool operator!=(const OffsetInfo &RHS) const
const_iterator begin() const
const_iterator end() const
bool merge(const OffsetInfo &R)
Copy offsets from R into the current list.
A container for a list of ranges.
const_iterator end() const
SmallVector< RangeTy > VecTy
void addToAllOffsets(int64_t Inc)
Add the increment Inc to the offset of every range.
bool operator==(const RangeList &OI) const
RangeList(ArrayRef< int64_t > Offsets, int64_t Size)
std::pair< iterator, bool > insert(iterator Pos, const RangeTy &R)
Insert R at the given iterator Pos, and merge if necessary.
bool isUnknown() const
Return true iff the list contains an unknown range.
VecTy::const_iterator const_iterator
const_iterator begin() const
bool isUnassigned() const
Return true if no ranges have been inserted.
static void set_difference(const RangeList &L, const RangeList &R, RangeList &D)
Copy ranges from L that are not in R, into D.
bool merge(const RangeList &RHS)
Merge the ranges in RHS into the current ranges.
iterator setUnknown()
Discard all ranges and insert a single unknown range.
void push_back(const RangeTy &R)
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
virtual bool reachesReturn() const =0
StringRef getName() const override
See AbstractAttribute::getName()
virtual bool forallInterferingAccesses(Attributor &A, const AbstractAttribute &QueryingAA, Instruction &I, bool FindInterferingWrites, bool FindInterferingReads, function_ref< bool(const Access &, bool)> CB, bool &HasBeenWrittenTo, AA::RangeTy &Range, function_ref< bool(const Access &)> SkipCB=nullptr) const =0
Call CB on all accesses that might interfere with I and return true if all such accesses were known a...
virtual void addReturnedOffsetsTo(OffsetInfo &) const =0
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAPointerInfo.
OffsetBinsTy::const_iterator const_bin_iterator
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
virtual const_bin_iterator begin() const =0
DenseMap< AA::RangeTy, SmallSet< unsigned, 4 > > OffsetBinsTy
virtual bool forallInterferingAccesses(AA::RangeTy Range, function_ref< bool(const Access &, bool)> CB) const =0
Call CB on all accesses that might interfere with Range and return true if all such accesses were kno...
virtual const_bin_iterator end() const =0
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI AAPointerInfo & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AAPointerInfo(const IRPosition &IRP)
virtual int64_t numOffsetBins() const =0
An abstract interface for potential values analysis.
PotentialConstantIntValuesState & getState() override
See AbstractAttribute::getState(...).
const PotentialConstantIntValuesState & getState() const override
AAPotentialConstantValues(const IRPosition &IRP, Attributor &A)
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAPotentialConstantValues.
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
unsigned getAssumedMinTrailingZeros() const
Return the minimum trailing zeros of potential constants.
std::optional< Constant * > getAssumedConstant(Attributor &A, const Instruction *CtxI=nullptr) const
Return assumed constant for the associated value.
StateWrapper< PotentialConstantIntValuesState, AbstractAttribute > Base
static LLVM_ABI AAPotentialConstantValues & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
StringRef getName() const override
See AbstractAttribute::getName()
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
friend struct Attributor
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAPotentialValues.
PotentialLLVMValuesState & getState() override
See AbstractAttribute::getState(...).
AAPotentialValues(const IRPosition &IRP, Attributor &A)
static LLVM_ABI AAPotentialValues & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
StateWrapper< PotentialLLVMValuesState, AbstractAttribute > Base
const PotentialLLVMValuesState & getState() const override
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
StringRef getName() const override
See AbstractAttribute::getName()
static LLVM_ABI Value * getSingleValue(Attributor &A, const AbstractAttribute &AA, const IRPosition &IRP, SmallVectorImpl< AA::ValueAndContext > &Values)
Extract the single value in Values if any.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
bool isAssumedPrivatizablePtr() const
Returns true if pointer privatization is assumed to be possible.
virtual std::optional< Type * > getPrivatizableType() const =0
Return the type we can choose for a private copy of the underlying value.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
StringRef getName() const override
See AbstractAttribute::getName()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAPricatizablePtr.
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
static LLVM_ABI AAPrivatizablePtr & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AAPrivatizablePtr(const IRPosition &IRP, Attributor &A)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
StateWrapper< BooleanState, AbstractAttribute > Base
static LLVM_ABI AAUndefinedBehavior & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
virtual bool isAssumedToCauseUB(Instruction *I) const =0
Return true if "undefined behavior" is assumed for a specific instruction.
StateWrapper< BooleanState, AbstractAttribute > Base
AAUndefinedBehavior(const IRPosition &IRP, Attributor &A)
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAUndefineBehavior.
StringRef getName() const override
See AbstractAttribute::getName()
virtual bool isKnownToCauseUB(Instruction *I) const =0
Return true if "undefined behavior" is known for a specific instruction.
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
virtual bool forallUnderlyingObjects(function_ref< bool(Value &)> Pred, AA::ValueScope Scope=AA::Interprocedural) const =0
Check Pred on all underlying objects in Scope collected so far.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAUnderlyingObjects.
static LLVM_ABI AAUnderlyingObjects & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute biew for the position IRP.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
AAUnderlyingObjects(const IRPosition &IRP)
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
StringRef getName() const override
See AbstractAttribute::getName()
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
See AbstractAttribute::isValidIRPositionForInit.
StringRef getName() const override
See AbstractAttribute::getName()
std::optional< Constant * > getAssumedConstant(Attributor &A, const Instruction *CtxI=nullptr) const
Return an assumed constant for the associated value a program point CtxI.
virtual ConstantRange getAssumedConstantRange(Attributor &A, const Instruction *CtxI=nullptr) const =0
Return an assumed range for the associated value a program point CtxI.
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static bool requiresCallersForArgOrFunction()
See AbstractAttribute::requiresCallersForArgOrFunction.
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAValueConstantRange.
AAValueConstantRange(const IRPosition &IRP, Attributor &A)
static LLVM_ABI AAValueConstantRange & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
StateWrapper< IntegerRangeState, AbstractAttribute, uint32_t > Base
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
IntegerRangeState & getState() override
See AbstractAttribute::getState(...).
const IntegerRangeState & getState() const override
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAValueSimplify.
friend struct Attributor
StringRef getName() const override
See AbstractAttribute::getName()
StateWrapper< ValueSimplifyStateType, AbstractAttribute, Type * > Base
static LLVM_ABI const char ID
Unique ID (due to the unique address)
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI AAValueSimplify & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
AAValueSimplify(const IRPosition &IRP, Attributor &A)
An abstract attribute for willreturn.
bool isKnownWillReturn() const
Return true if "willreturn" is known.
const char * getIdAddr() const override
See AbstractAttribute::getIdAddr()
static LLVM_ABI const char ID
Unique ID (due to the unique address)
static LLVM_ABI AAWillReturn & createForPosition(const IRPosition &IRP, Attributor &A)
Create an abstract attribute view for the position IRP.
StringRef getName() const override
See AbstractAttribute::getName()
static bool classof(const AbstractAttribute *AA)
This function should return true if the type of the AA is AAWillReturn.
AAWillReturn(const IRPosition &IRP, Attributor &A)
static bool isImpliedByMustprogressAndReadonly(Attributor &A, const IRPosition &IRP)
Check for mustprogress and readonly as they imply willreturn.
static bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind, bool IgnoreSubsumingPositions=false)
bool isAssumedWillReturn() const
Return true if "willreturn" is assumed.
Helper to represent an access offset and size, with logic to deal with uncertainty and check for over...
Definition Attributor.h:244
bool offsetAndSizeAreUnknown() const
Return true if offset and size are unknown, thus this is the default unknown object.
Definition Attributor.h:259
static constexpr int64_t Unknown
Definition Attributor.h:323
bool offsetOrSizeAreUnknown() const
Return true if offset or size are unknown.
Definition Attributor.h:253
static constexpr int64_t Unassigned
Constants used to represent special offsets or sizes.
Definition Attributor.h:322
RangeTy & operator&=(const RangeTy &R)
Definition Attributor.h:282
static RangeTy getUnknown()
Definition Attributor.h:250
bool isUnassigned() const
Return true if the offset and size are unassigned.
Definition Attributor.h:264
static bool LessThan(const RangeTy &L, const RangeTy &R)
Comparison for sorting ranges.
Definition Attributor.h:308
bool mayOverlap(const RangeTy &Range) const
Return true if this offset and size pair might describe an address that overlaps with Range.
Definition Attributor.h:272
RangeTy(int64_t Offset, int64_t Size)
Definition Attributor.h:248
Value * getValue() const
Definition Attributor.h:197
std::pair< Value *, const Instruction * > Base
Definition Attributor.h:192
ValueAndContext(Value &V, const Instruction *CtxI)
Definition Attributor.h:194
const Instruction * getCtxI() const
Definition Attributor.h:198
ValueAndContext(Value &V, const Instruction &CtxI)
Definition Attributor.h:195
ValueAndContext(const Base &B)
Definition Attributor.h:193
Base struct for all "concrete attribute" deductions.
ChangeStatus update(Attributor &A)
Hook for the Attributor to trigger an update of the internal state.
friend struct Attributor
}
static bool isValidIRPositionForInit(Attributor &A, const IRPosition &IRP)
Return false if an AA should not be created for IRP.
virtual ChangeStatus manifest(Attributor &A)
Hook for the Attributor to trigger the manifestation of the information represented by the abstract a...
static bool classof(const AADepGraphNode *DGN)
This function is used to identify if an DGN is of type AbstractAttribute so that the dyn_cast and cas...
static bool requiresCalleeForCallBase()
Return true if this AA requires a "callee" (or an associted function) for a call site positon.
void print(raw_ostream &OS) const
Helper functions, for debug purposes only.
IRPosition & getIRPosition()
virtual StateType & getState()=0
Return the internal abstract state for inspection.
virtual void initialize(Attributor &A)
Initialize the state with the information in the Attributor A.
static bool isValidIRPositionForUpdate(Attributor &A, const IRPosition &IRP)
Return false if an AA should not be updated for IRP.
AbstractState StateType
virtual const std::string getAsStr(Attributor *A) const =0
This function should return the "summarized" assumed state as string.
~AbstractAttribute() override=default
Virtual destructor.
static constexpr Attribute::AttrKind IRAttributeKind
Compile time access to the IR attribute kind.
virtual bool isQueryAA() const
A query AA is always scheduled as long as we do updates because it does lazy computation that cannot ...
virtual const StateType & getState() const =0
AbstractAttribute(const IRPosition &IRP)
static bool requiresNonAsmForCallBase()
Return true if this AA requires non-asm "callee" for a call site positon.
virtual ChangeStatus updateImpl(Attributor &A)=0
The actual update/transfer function which has to be implemented by the derived classes.
virtual void trackStatistics() const =0
Hook to enable custom statistic tracking, called after manifest that resulted in a change if statisti...
static bool requiresCallersForArgOrFunction()
Return true if this AA requires all callees for an argument or function positon.
virtual StringRef getName() const =0
This function should return the name of the AbstractAttribute.
const IRPosition & getIRPosition() const
Return an IR position, see struct IRPosition.
virtual const char * getIdAddr() const =0
This function should return the address of the ID of the AbstractAttribute.
static bool hasTrivialInitializer()
Return false if this AA does anything non-trivial (hence not done by default) in its initializer.
An interface to query the internal state of an abstract attribute.
virtual ~AbstractState()=default
virtual ChangeStatus indicatePessimisticFixpoint()=0
Indicate that the abstract state should converge to the pessimistic state.
virtual bool isAtFixpoint() const =0
Return if this abstract state is fixed, thus does not need to be updated if information changes as it...
virtual bool isValidState() const =0
Return if this abstract state is in a valid state.
virtual ChangeStatus indicateOptimisticFixpoint()=0
Indicate that the abstract state should converge to the optimistic state.
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition Alignment.h:39
Wrapper for FunctionAnalysisManager.
AnalysisGetter()=default
static constexpr bool HasLegacyWrapper
Analysis::Result * getAnalysis(const Function &F, bool RequestCachedOnly=false)
AnalysisGetter(FunctionAnalysisManager &FAM, bool CachedOnly=false)
void invalidateAnalyses()
Invalidates the analyses. Valid only when using the new pass manager.
AnalysisGetter(Pass *P, bool CachedOnly=false)
LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
~AttributorCallGraph() override=default
AACallEdgeIterator optimisticEdgesEnd() const override
AttributorCallGraph(Attributor &A)
AACallEdgeIterator optimisticEdgesBegin() const override
void populateAll() const
Force populate the entire call graph.
Configuration for the Attributor.
bool UseLiveness
Flag to determine if we should skip all liveness checks early on.
std::function< void(Attributor &A, const Function &F)> InitializationCallback
Callback function to be invoked on internal functions marked live.
std::optional< unsigned > MaxFixpointIterations
Maximum number of iterations to run until fixpoint.
DenseSet< const char * > * Allowed
If not null, a set limiting the attribute opportunities.
bool RewriteSignatures
Flag to determine if we rewrite function signatures.
const char * PassName
}
OptimizationRemarkGetter OREGetter
bool DeleteFns
Flag to determine if we can delete functions or keep dead ones around.
std::function< bool(const Function &F)> IPOAmendableCBTy
bool IsClosedWorldModule
Flag to indicate if the entire world is contained in this module, that is, no outside functions exist...
function_ref< OptimizationRemarkEmitter &(Function *)> OptimizationRemarkGetter
A callback function that returns an ORE object from a Function pointer.
CallGraphUpdater & CGUpdater
Helper to update an underlying call graph and to delete functions.
IPOAmendableCBTy IPOAmendableCB
bool IsModulePass
Is the user of the Attributor a module pass or not.
std::function< bool(Attributor &A, const AbstractAttribute &AA, CallBase &CB, Function &AssumedCallee, unsigned NumAssumedCallees)> IndirectCalleeSpecializationCallback
Callback function to determine if an indirect call targets should be made direct call targets (with a...
bool DefaultInitializeLiveInternals
Flag to determine if we want to initialize all default AAs for an internal function marked live.
AttributorConfig(CallGraphUpdater &CGUpdater)
A more lightweight version of the Attributor which only runs attribute inference but no simplificatio...
LLVM_ABI PreservedAnalyses run(LazyCallGraph::SCC &C, CGSCCAnalysisManager &AM, LazyCallGraph &CG, CGSCCUpdateResult &UR)
A more lightweight version of the Attributor which only runs attribute inference but no simplificatio...
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
LLVM_ABI PreservedAnalyses run(Module &M, ModuleAnalysisManager &AM)
Helper struct used in the communication between an abstract attribute (AA) that wants to change the s...
friend struct Attributor
Allow access to the private members from the Attributor.
std::function< void( const ArgumentReplacementInfo &, Function &, Function::arg_iterator)> CalleeRepairCBTy
Callee repair callback type.
const Argument & getReplacedArg() const
Attributor & getAttributor() const
Simple getters, see the corresponding members for details.
const Function & getReplacedFn() const
std::function< void(const ArgumentReplacementInfo &, AbstractCallSite, SmallVectorImpl< Value * > &)> ACSRepairCBTy
Abstract call site (ACS) repair callback type.
The fixpoint analysis framework that orchestrates the attribute deduction.
LLVM_ABI bool registerFunctionSignatureRewrite(Argument &Arg, ArrayRef< Type * > ReplacementTypes, ArgumentReplacementInfo::CalleeRepairCBTy &&CalleeRepairCB, ArgumentReplacementInfo::ACSRepairCBTy &&ACSRepairCB)
Register a rewrite for a function signature.
AAType & registerAA(AAType &AA)
Introduce a new abstract attribute into the fixpoint analysis.
LLVM_ABI ~Attributor()
LLVM_ABI bool checkForAllCallees(function_ref< bool(ArrayRef< const Function * > Callees)> Pred, const AbstractAttribute &QueryingAA, const CallBase &CB)
Check Pred on all potential Callees of CB.
bool isModulePass() const
Return true if this is a module pass, false otherwise.
void registerInvokeWithDeadSuccessor(InvokeInst &II)
Record that II has at least one dead successor block.
void registerSimplificationCallback(const IRPosition &IRP, const SimplifictionCallbackTy &CB)
bool changeAfterManifest(const IRPosition IRP, Value &NV, bool ChangeDroppable=true)
Helper function to replace all uses associated with IRP with NV.
LLVM_ABI bool isValidFunctionSignatureRewrite(Argument &Arg, ArrayRef< Type * > ReplacementTypes)
Check if we can rewrite a function signature.
static LLVM_ABI bool isInternalizable(Function &F)
Returns true if the function F can be internalized.
LLVM_ABI ChangeStatus removeAttrs(const IRPosition &IRP, ArrayRef< Attribute::AttrKind > AttrKinds)
Remove all AttrKinds attached to IRP.
void emitRemark(Instruction *I, StringRef RemarkName, RemarkCallBack &&RemarkCB) const
Emit a remark generically.
bool isRunOn(Function &Fn) const
Return true if we derive attributes for Fn.
LLVM_ABI bool isAssumedDead(const AbstractAttribute &AA, const AAIsDead *LivenessAA, bool &UsedAssumedInformation, bool CheckBBLivenessOnly=false, DepClassTy DepClass=DepClassTy::OPTIONAL)
Return true if AA (or its context instruction) is assumed dead.
LLVM_ABI bool checkForAllInstructions(function_ref< bool(Instruction &)> Pred, const Function *Fn, const AbstractAttribute *QueryingAA, ArrayRef< unsigned > Opcodes, bool &UsedAssumedInformation, bool CheckBBLivenessOnly=false, bool CheckPotentiallyDead=false)
Check Pred on all instructions in Fn with an opcode present in Opcodes.
LLVM_ABI void recordDependence(const AbstractAttribute &FromAA, const AbstractAttribute &ToAA, DepClassTy DepClass)
Explicitly record a dependence from FromAA to ToAA, that is if FromAA changes ToAA should be updated ...
static LLVM_ABI void createShallowWrapper(Function &F)
Create a shallow wrapper for F such that F has internal linkage afterwards.
bool isRunOn(Function *Fn) const
bool isDuringDeduction() const
Return whether attributes can participate in fixed-point deduction.
const AAType * getAAFor(const AbstractAttribute &QueryingAA, const IRPosition &IRP, DepClassTy DepClass)
Lookup an abstract attribute of type AAType at position IRP.
std::function< std::optional< Value * >( const IRPosition &, const AbstractAttribute *, bool &)> SimplifictionCallbackTy
Register CB as a simplification callback.
std::optional< Constant * > getAssumedInitializerFromCallBack(const GlobalVariable &GV, const AbstractAttribute *AA, bool &UsedAssumedInformation)
Return std::nullopt if there is no call back registered for GV or the call back is still not sure if ...
void deleteAfterManifest(Function &F)
Record that F is deleted after information was manifested.
std::optional< Value * > getAssumedSimplified(const IRPosition &IRP, const AbstractAttribute &AA, bool &UsedAssumedInformation, AA::ValueScope S)
If V is assumed simplified, return it, if it is unclear yet, return std::nullopt, otherwise return nu...
void emitRemark(Function *F, StringRef RemarkName, RemarkCallBack &&RemarkCB) const
Emit a remark on a function.
static LLVM_ABI Function * internalizeFunction(Function &F, bool Force=false)
Make another copy of the function F such that the copied version has internal linkage afterwards and ...
bool isFunctionIPOAmendable(const Function &F)
Determine whether the function F is IPO amendable.
const AAType * getOrCreateAAFor(IRPosition IRP, const AbstractAttribute *QueryingAA, DepClassTy DepClass, bool ForceUpdate=false, bool UpdateAfterInit=true)
The version of getAAFor that allows to omit a querying abstract attribute.
const SmallSetVector< Function *, 8 > & getModifiedFunctions()
LLVM_ABI bool checkForAllReadWriteInstructions(function_ref< bool(Instruction &)> Pred, AbstractAttribute &QueryingAA, bool &UsedAssumedInformation)
Check Pred on all Read/Write instructions.
void changeToUnreachableAfterManifest(Instruction *I)
Record that I is to be replaced with unreachable after information was manifested.
bool hasGlobalVariableSimplificationCallback(const GlobalVariable &GV)
Return true if there is a simplification callback for GV.
std::optional< Constant * > getAssumedConstant(const Value &V, const AbstractAttribute &AA, bool &UsedAssumedInformation)
LLVM_ABI bool checkForAllReturnedValues(function_ref< bool(Value &)> Pred, const AbstractAttribute &QueryingAA, AA::ValueScope S=AA::ValueScope::Intraprocedural, bool RecurseForSelectAndPHI=true)
Check Pred on all values potentially returned by the function associated with QueryingAA.
bool hasSimplificationCallback(const IRPosition &IRP)
Return true if there is a simplification callback for IRP.
std::function< std::optional< Constant * >( const GlobalVariable &, const AbstractAttribute *, bool &)> GlobalVariableSimplifictionCallbackTy
Register CB as a simplification callback.
LLVM_ABI bool isClosedWorldModule() const
Return true if the module contains the whole world, thus, no outside functions exist.
LLVM_ABI std::optional< Constant * > getAssumedConstant(const IRPosition &IRP, const AbstractAttribute &AA, bool &UsedAssumedInformation)
If IRP is assumed to be a constant, return it, if it is unclear yet, return std::nullopt,...
const AAType * getOrCreateAAFor(const IRPosition &IRP)
LLVM_ABI Attributor(SetVector< Function * > &Functions, InformationCache &InfoCache, AttributorConfig Configuration)
Constructor.
void registerGlobalVariableSimplificationCallback(const GlobalVariable &GV, const GlobalVariableSimplifictionCallbackTy &CB)
const DataLayout & getDataLayout() const
Return the data layout associated with the anchor scope.
LLVM_ABI void getAttrs(const IRPosition &IRP, ArrayRef< Attribute::AttrKind > AKs, SmallVectorImpl< Attribute > &Attrs, bool IgnoreSubsumingPositions=false)
Return the attributes of any kind in AKs existing in the IR at a position that will affect this one.
InformationCache & getInfoCache()
Return the internal information cache.
bool changeUseAfterManifest(Use &U, Value &NV)
Record that U is to be replaces with NV after information was manifested.
LLVM_ABI std::optional< Value * > translateArgumentToCallSiteContent(std::optional< Value * > V, CallBase &CB, const AbstractAttribute &AA, bool &UsedAssumedInformation)
Translate V from the callee context into the call site context.
AAType * lookupAAFor(const IRPosition &IRP, const AbstractAttribute *QueryingAA=nullptr, DepClassTy DepClass=DepClassTy::OPTIONAL, bool AllowInvalidState=false)
Return the attribute of AAType for IRP if existing and valid.
void markLiveInternalFunction(const Function &F)
Mark the internal function F as live.
void registerManifestAddedBasicBlock(BasicBlock &BB)
void registerVirtualUseCallback(const Value &V, const VirtualUseCallbackTy &CB)
LLVM_ABI bool checkForAllUses(function_ref< bool(const Use &, bool &)> Pred, const AbstractAttribute &QueryingAA, const Value &V, bool CheckBBLivenessOnly=false, DepClassTy LivenessDepClass=DepClassTy::OPTIONAL, bool IgnoreDroppableUses=true, function_ref< bool(const Use &OldU, const Use &NewU)> EquivalentUseCB=nullptr)
Check Pred on all (transitive) uses of V.
LLVM_ABI ChangeStatus manifestAttrs(const IRPosition &IRP, ArrayRef< Attribute > DeducedAttrs, bool ForceReplace=false)
Attach DeducedAttrs to IRP, if ForceReplace is set we do this even if the same attribute kind was alr...
LLVM_ABI bool hasAttr(const IRPosition &IRP, ArrayRef< Attribute::AttrKind > AKs, bool IgnoreSubsumingPositions=false, Attribute::AttrKind ImpliedAttributeKind=Attribute::None)
Return true if any kind in AKs existing in the IR at a position that will affect this one.
LLVM_ABI void registerForUpdate(AbstractAttribute &AA)
Allows a query AA to request an update if a new query was received.
const Module & getModule()
Return the module.
void deleteAfterManifest(Instruction &I)
Record that I is deleted after information was manifested.
std::function< bool(Attributor &, const AbstractAttribute *)> VirtualUseCallbackTy
void deleteAfterManifest(BasicBlock &BB)
Record that BB is deleted after information was manifested.
LLVM_ABI void identifyDefaultAbstractAttributes(Function &F)
Determine opportunities to derive 'default' attributes in F and create abstract attribute objects for...
bool shouldInitialize(const IRPosition &IRP, bool &ShouldUpdateAA)
LLVM_ABI bool getAssumedSimplifiedValues(const IRPosition &IRP, const AbstractAttribute *AA, SmallVectorImpl< AA::ValueAndContext > &Values, AA::ValueScope S, bool &UsedAssumedInformation, bool RecurseForSelectAndPHI=true)
Try to simplify IRP and in the scope S.
BumpPtrAllocator & Allocator
The allocator used to allocate memory, e.g. for AbstractAttributes.
bool checkForAllCallLikeInstructions(function_ref< bool(Instruction &)> Pred, const AbstractAttribute &QueryingAA, bool &UsedAssumedInformation, bool CheckBBLivenessOnly=false, bool CheckPotentiallyDead=false)
Check Pred on all call-like instructions (=CallBased derived).
bool shouldSpecializeCallSiteForCallee(const AbstractAttribute &AA, CallBase &CB, Function &Callee, unsigned NumAssumedCallees)
Return true if we should specialize the call site CB for the potential callee Fn.
LLVM_ABI ChangeStatus run()
Run the analyses until a fixpoint is reached or enforced (timeout).
static LLVM_ABI bool internalizeFunctions(SmallPtrSetImpl< Function * > &FnSet, DenseMap< Function *, Function * > &FnMap)
Make copies of each function in the set FnSet such that the copied version has internal linkage after...
std::optional< Value * > getAssumedSimplified(const Value &V, const AbstractAttribute &AA, bool &UsedAssumedInformation, AA::ValueScope S)
bool shouldUpdateAA(const IRPosition &IRP)
LLVM_ABI bool checkForAllCallSites(function_ref< bool(AbstractCallSite)> Pred, const AbstractAttribute &QueryingAA, bool RequireAllCallSites, bool &UsedAssumedInformation)
Check Pred on all function call sites.
LLVM_ABI bool getAttrsFromAssumes(const IRPosition &IRP, Attribute::AttrKind AK, SmallVectorImpl< Attribute > &Attrs)
Return the attributes of kind AK existing in the IR as operand bundles of an llvm....
Specialization of the integer state for a bit-wise encoding.
BitIntegerState & removeKnownBits(base_t BitsEncoding)
Remove the bits in BitsEncoding from the "known bits".
bool isAssumed(base_t BitsEncoding=BestState) const
Return true if the bits set in BitsEncoding are "assumed bits".
IntegerStateBase< base_ty, BestState, WorstState > super
BitIntegerState(base_t Assumed)
BitIntegerState & removeAssumedBits(base_t BitsEncoding)
Remove the bits in BitsEncoding from the "assumed bits" if not known.
BitIntegerState & intersectAssumedBits(base_t BitsEncoding)
Keep only "assumed bits" also set in BitsEncoding but all known ones.
bool isKnown(base_t BitsEncoding=BestState) const
Return true if the bits set in BitsEncoding are "known bits".
BitIntegerState & addKnownBits(base_t Bits)
Add the bits in BitsEncoding to the "known bits".
Simple wrapper for a single bit (boolean) state.
IntegerStateBase< bool, true, false > super
bool isKnown() const
Return true if the state is known to hold.
void setKnown(bool Value)
Set the known and asssumed value to Value.
IntegerStateBase::base_t base_t
BooleanState(base_t Assumed)
BooleanState()=default
bool isAssumed() const
Return true if the state is assumed to hold.
Support structure for SCC passes to communicate updates the call graph back to the CGSCC pass manager...
static bool isNodeHidden(const AACallGraphNode *Node, const AttributorCallGraph *Graph)
std::string getNodeLabel(const AACallGraphNode *Node, const AttributorCallGraph *Graph)
Specialization of the integer state for a decreasing value, hence 0 is the best state and ~0u the wor...
DecIntegerState & takeKnownMinimum(base_t Value)
Take minimum of known and Value.
DecIntegerState & takeAssumedMaximum(base_t Value)
Take maximum of assumed and Value.
static DenormalMode unionAssumed(DenormalMode Callee, DenormalMode Caller)
DenormalState unionWith(DenormalState Caller) const
bool operator!=(const DenormalState Other) const
bool operator==(const DenormalState Other) const
static DenormalMode::DenormalModeKind unionDenormalKind(DenormalMode::DenormalModeKind Callee, DenormalMode::DenormalModeKind Caller)
bool IsAtFixedpoint
Explicitly track whether we've hit a fixed point.
ChangeStatus indicateOptimisticFixpoint() override
Indicate that the abstract state should converge to the optimistic state.
DenormalState getKnown() const
DenormalState getAssumed() const
bool isValidState() const override
Return if this abstract state is in a valid state.
ChangeStatus indicatePessimisticFixpoint() override
Indicate that the abstract state should converge to the pessimistic state.
DenormalFPMathState operator^=(const DenormalFPMathState &Caller)
ChangeStatus indicateFixpoint()
bool isModeFixed() const
Return true if there are no dynamic components to the denormal mode worth specializing.
bool isAtFixpoint() const override
Return if this abstract state is fixed, thus does not need to be updated if information changes as it...
Represent subnormal handling kind for floating point instruction inputs and outputs.
DenormalModeKind
Represent handled modes for denormal (aka subnormal) modes in the floating point environment.
@ Dynamic
Denormals have unknown treatment.
static constexpr DenormalMode getInvalid()
static bool isEqual(const AA::ValueAndContext &LHS, const AA::ValueAndContext &RHS)
Definition Attributor.h:429
static unsigned getHashValue(const AA::ValueAndContext &VAC)
Definition Attributor.h:425
DenseMapInfo< AA::ValueAndContext::Base > Base
Definition Attributor.h:424
static bool isEqual(const AA::ValueScope &LHS, const AA::ValueScope &RHS)
Definition Attributor.h:442
DenseMapInfo< unsigned char > Base
Definition Attributor.h:437
static unsigned getHashValue(const AA::ValueScope &S)
Definition Attributor.h:438
static bool isEqual(const IRPosition &a, const IRPosition &b)
static unsigned getHashValue(const IRPosition &IRP)
An information struct used to provide DenseMap with the various necessary components for a given valu...
State for dereferenceable attribute.
IncIntegerState DerefBytesState
State representing for dereferenceable bytes.
static DerefState getBestState(const DerefState &)
static DerefState getWorstState()
Return the worst possible representable state.
static DerefState getBestState()
static DerefState getWorstState(const DerefState &)
std::map< int64_t, uint64_t > AccessedBytesMap
Map representing for accessed memory offsets and sizes.
static AACallEdgeIterator child_end(AACallGraphNode *Node)
static AACallEdgeIterator child_begin(AACallGraphNode *Node)
static AACallGraphNode * getEntryNode(AttributorCallGraph *G)
static AACallEdgeIterator nodes_begin(const AttributorCallGraph *G)
static AACallEdgeIterator nodes_end(const AttributorCallGraph *G)
Attribute::AttrKind getAttrKind() const
Return the kind that identifies the abstract attribute implementation.
static constexpr Attribute::AttrKind IRAttributeKind
Compile time access to the IR attribute kind.
static bool hasTrivialInitializer()
Most boolean IRAttribute AAs don't do anything non-trivial in their initializers while non-boolean on...
static bool isImpliedByUndef()
Return true if the IR attribute(s) associated with this AA are implied for an undef value.
ChangeStatus manifest(Attributor &A) override
See AbstractAttribute::manifest(...).
static bool isImpliedByPoison()
Return true if the IR attribute(s) associated with this AA are implied for an poison value.
static bool isImpliedByIR(Attributor &A, const IRPosition &IRP, Attribute::AttrKind ImpliedAttributeKind=AK, bool IgnoreSubsumingPositions=false)
IRAttribute(const IRPosition &IRP)
virtual void getDeducedAttributes(Attributor &A, LLVMContext &Ctx, SmallVectorImpl< Attribute > &Attrs) const
Return the deduced attributes in Attrs.
Helper to describe and deal with positions in the LLVM-IR.
Definition Attributor.h:572
Function * getAssociatedFunction() const
Return the associated function, if any.
Definition Attributor.h:703
void setAttrList(const AttributeList &AttrList) const
Update the attributes associated with this function or call site scope.
Definition Attributor.h:839
unsigned getAttrIdx() const
Return the index in the attribute list for this position.
Definition Attributor.h:804
bool hasCallBaseContext() const
Check if the position has any call base context.
Definition Attributor.h:899
static const IRPosition callsite_returned(const CallBase &CB)
Create a position describing the returned value of CB.
Definition Attributor.h:640
static const IRPosition returned(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the returned value of F.
Definition Attributor.h:622
LLVM_ABI Argument * getAssociatedArgument() const
Return the associated argument, if any.
bool isAnyCallSitePosition() const
Definition Attributor.h:866
bool operator!=(const IRPosition &RHS) const
Definition Attributor.h:681
static const IRPosition value(const Value &V, const CallBaseContext *CBContext=nullptr)
Create a position describing the value of V.
Definition Attributor.h:596
CallBase CallBaseContext
Definition Attributor.h:575
AttributeList getAttrList() const
Return the attributes associated with this function or call site scope.
Definition Attributor.h:832
int getCalleeArgNo() const
Return the callee argument number of the associated value if it is an argument or call site argument,...
Definition Attributor.h:790
static const IRPosition inst(const Instruction &I, const CallBaseContext *CBContext=nullptr)
Create a position describing the instruction I.
Definition Attributor.h:608
static const IRPosition callsite_argument(const CallBase &CB, unsigned ArgNo)
Create a position describing the argument of CB at position ArgNo.
Definition Attributor.h:645
Kind
The positions we distinguish in the IR.
Definition Attributor.h:578
@ IRP_ARGUMENT
An attribute for a function argument.
Definition Attributor.h:586
@ IRP_RETURNED
An attribute for the function return value.
Definition Attributor.h:582
@ IRP_CALL_SITE
An attribute for a call site (function scope).
Definition Attributor.h:585
@ IRP_CALL_SITE_RETURNED
An attribute for a call site return value.
Definition Attributor.h:583
@ IRP_FUNCTION
An attribute for a function (scope).
Definition Attributor.h:584
@ IRP_FLOAT
A position that is not associated with a spot suitable for attributes.
Definition Attributor.h:580
@ IRP_CALL_SITE_ARGUMENT
An attribute for a call site argument.
Definition Attributor.h:587
@ IRP_INVALID
An invalid position.
Definition Attributor.h:579
Instruction * getCtxI() const
Return the context instruction, if any.
Definition Attributor.h:756
static const IRPosition argument(const Argument &Arg, const CallBaseContext *CBContext=nullptr)
Create a position describing the argument Arg.
Definition Attributor.h:629
Type * getAssociatedType() const
Return the type this abstract attribute is associated with.
Definition Attributor.h:779
bool isFunctionScope() const
Return true if this is a function or call site position.
Definition Attributor.h:733
bool operator==(const IRPosition &RHS) const
Definition Attributor.h:678
static const IRPosition callsite_argument(AbstractCallSite ACS, unsigned ArgNo)
Create a position describing the argument of ACS at position ArgNo.
Definition Attributor.h:652
static const IRPosition function(const Function &F, const CallBaseContext *CBContext=nullptr)
Create a position describing the function scope of F.
Definition Attributor.h:615
const CallBaseContext * getCallBaseContext() const
Get the call base context from the position.
Definition Attributor.h:896
Value & getAssociatedValue() const
Return the value this abstract attribute is associated with.
Definition Attributor.h:770
Value & getAnchorValue() const
Return the value this abstract attribute is anchored with.
Definition Attributor.h:689
Value * getAttrListAnchor() const
Return the value attributes are attached to.
Definition Attributor.h:825
int getCallSiteArgNo() const
Return the call site argument number of the associated value if it is an argument or call site argume...
Definition Attributor.h:799
bool isFnInterfaceKind() const
Return true if the position refers to a function interface, that is the function scope,...
Definition Attributor.h:721
static const IRPosition function_scope(const IRPosition &IRP, const CallBaseContext *CBContext=nullptr)
Create a position with function scope matching the "context" of IRP.
Definition Attributor.h:668
IRPosition stripCallBaseContext() const
Return the same position without the call base context.
Definition Attributor.h:889
Kind getPositionKind() const
Return the associated position kind.
Definition Attributor.h:846
bool isArgumentPosition() const
Return true if the position is an argument or call site argument.
Definition Attributor.h:878
static const IRPosition callsite_function(const CallBase &CB)
Create a position describing the function scope of CB.
Definition Attributor.h:635
IRPosition()
Default constructor available to create invalid positions implicitly.
Definition Attributor.h:593
Function * getAnchorScope() const
Return the Function surrounding the anchor value.
Definition Attributor.h:744
Specialization of the integer state for an increasing value, hence ~0u is the best state and 0 the wo...
IntegerStateBase< base_ty, BestState, WorstState > super
static constexpr base_t getBestState(const IncIntegerState< base_ty, BestState, WorstState > &)
IncIntegerState(base_t Assumed)
static constexpr base_t getBestState()
Return the best possible representable state.
IncIntegerState & takeAssumedMinimum(base_t Value)
Take minimum of assumed and Value.
IncIntegerState & takeKnownMaximum(base_t Value)
Take maximum of known and Value.
Data structure to hold cached (LLVM-IR) information.
bool IsTargetGPU() const
Return true if the target is a GPU.
friend struct Attributor
Give the Attributor access to the members so Attributor::identifyDefaultAbstractAttributes(....
const SetVector< Function * > *const CGSCC
The CG-SCC the pass is run on, or nullptr if it is a module pass.
bool stackIsAccessibleByOtherThreads()
Return true if the stack (llvm::Alloca) can be accessed by other threads.
bool isInvolvedInMustTailCall(const Argument &Arg)
Return true if Arg is involved in a must-tail call, thus the argument of the caller or callee.
MustBeExecutedContextExplorer * getMustBeExecutedContextExplorer()
Return MustBeExecutedContextExplorer.
void invalidateAnalyses()
Invalidates the cached analyses.
virtual unsigned getMaxAddrSpace() const
const AA::InstExclusionSetTy * getOrCreateUniqueBlockExecutionSet(const AA::InstExclusionSetTy *BES)
Given BES, return a uniqued version.
static void foreachUse(Function &F, CBTy CB, bool LookThroughConstantExprUses=true)
Apply CB to all uses of F.
TargetLibraryInfo * getTargetLibraryInfoForFunction(const Function &F)
Return TargetLibraryInfo for function F.
InformationCache(const Module &M, AnalysisGetter &AG, BumpPtrAllocator &Allocator, SetVector< Function * > *CGSCC, bool UseExplorer=true)
const Module & getModule() const
const DataLayout & getDL() const
Return datalayout used in the module.
LLVM_ABI std::optional< unsigned > getFlatAddressSpace() const
Return the flat address space if the associated target has.
OpcodeInstMapTy & getOpcodeInstMapForFunction(const Function &F)
Return the map that relates "interesting" opcodes with all instructions with that opcode in F.
DenseMap< unsigned, InstructionVectorTy * > OpcodeInstMapTy
A map type from opcodes to instructions with this opcode.
const RetainedKnowledgeMap & getKnowledgeMap() const
Return the map conaining all the knowledge we have from llvm.assumes.
LLVM_ABI ArrayRef< Function * > getIndirectlyCallableFunctions(Attributor &A) const
Return all functions that might be called indirectly, only valid for closed world modules (see isClos...
SmallVector< Instruction *, 8 > InstructionVectorTy
A vector type to hold instructions.
InstructionVectorTy & getReadOrWriteInstsForFunction(const Function &F)
Return the instructions in F that may read or write memory.
bool isOnlyUsedByAssume(const Instruction &I) const
bool isKernel(const Function &F)
Return true if F has the "kernel" function attribute.
AP::Result * getAnalysisResultForFunction(const Function &F, bool CachedOnly=false)
Return the analysis result from a pass AP for function F.
State for an integer range.
IntegerRangeState operator^=(const IntegerRangeState &R)
"Clamp" this state with R.
IntegerRangeState operator&=(const IntegerRangeState &R)
bool isAtFixpoint() const override
See AbstractState::isAtFixpoint()
void unionAssumed(const IntegerRangeState &R)
See IntegerRangeState::unionAssumed(..).
ConstantRange Assumed
State representing assumed range, initially set to empty.
IntegerRangeState(const ConstantRange &CR)
IntegerRangeState(uint32_t BitWidth)
bool operator==(const IntegerRangeState &R) const
Equality for IntegerRangeState.
void intersectKnown(const IntegerRangeState &R)
See IntegerRangeState::intersectKnown(..).
static ConstantRange getBestState(const IntegerRangeState &IRS)
bool isValidState() const override
See AbstractState::isValidState()
uint32_t BitWidth
Bitwidth of the associated value.
ConstantRange Known
State representing known range, initially set to [-inf, inf].
void unionAssumed(const ConstantRange &R)
Unite assumed range with the passed state.
ChangeStatus indicatePessimisticFixpoint() override
See AbstractState::indicatePessimisticFixpoint(...)
ConstantRange getKnown() const
Return the known state encoding.
void intersectKnown(const ConstantRange &R)
Intersect known range with the passed state.
ConstantRange getAssumed() const
Return the assumed state encoding.
uint32_t getBitWidth() const
Return associated values' bit width.
ChangeStatus indicateOptimisticFixpoint() override
See AbstractState::indicateOptimisticFixpoint(...)
static ConstantRange getWorstState(uint32_t BitWidth)
Return the worst possible representable state.
static ConstantRange getBestState(uint32_t BitWidth)
Return the best possible representable state.
Simple state with integers encoding.
bool isValidState() const override
See AbstractState::isValidState() NOTE: For now we simply pretend that the worst possible state is in...
virtual void handleNewAssumedValue(base_t Value)=0
Handle a new assumed value Value. Subtype dependent.
bool isAtFixpoint() const override
See AbstractState::isAtFixpoint()
void operator|=(const IntegerStateBase< base_t, BestState, WorstState > &R)
virtual void handleNewKnownValue(base_t Value)=0
Handle a new known value Value. Subtype dependent.
base_t getKnown() const
Return the known state encoding.
ChangeStatus indicateOptimisticFixpoint() override
See AbstractState::indicateOptimisticFixpoint(...)
void operator^=(const IntegerStateBase< base_t, BestState, WorstState > &R)
"Clamp" this state with R.
virtual void joinOR(base_t AssumedValue, base_t KnownValue)=0
Handle a value Value. Subtype dependent.
virtual void joinAND(base_t AssumedValue, base_t KnownValue)=0
Handle a new assumed value Value. Subtype dependent.
IntegerStateBase(base_t Assumed)
void operator+=(const IntegerStateBase< base_t, BestState, WorstState > &R)
"Clamp" this state with R.
base_t getAssumed() const
Return the assumed state encoding.
static constexpr base_t getWorstState()
Return the worst possible representable state.
static constexpr base_t getBestState()
Return the best possible representable state.
ChangeStatus indicatePessimisticFixpoint() override
See AbstractState::indicatePessimisticFixpoint(...)
static constexpr base_t getWorstState(const IntegerStateBase &)
bool operator!=(const IntegerStateBase< base_t, BestState, WorstState > &R) const
Inequality for IntegerStateBase.
bool operator==(const IntegerStateBase< base_t, BestState, WorstState > &R) const
Equality for IntegerStateBase.
void operator&=(const IntegerStateBase< base_t, BestState, WorstState > &R)
static constexpr base_t getBestState(const IntegerStateBase &)
A "must be executed context" for a given program point PP is the set of instructions,...
A CRTP mix-in for passes that can be skipped.
static constexpr int NumLowBitsAvailable
Note, we assume here that void* is related to raw malloc'ed memory and that malloc returns objects at...
A class for a set state.
static PotentialValuesState getBestState(const PotentialValuesState &PVS)
PotentialValuesState & getAssumed()
Return the assumed state.
bool undefIsContained() const
Returns whether this state contains an undef value or not.
bool isAtFixpoint() const override
See AbstractState::isAtFixpoint(...)
void unionAssumed(const MemberTy &C)
Union assumed set with the passed value.
static PotentialValuesState getBestState()
Return empty set as the best state of potential values.
SmallSetVector< MemberTy, 8 > SetTy
ChangeStatus indicatePessimisticFixpoint() override
See AbstractState::indicatePessimisticFixpoint(...)
void unionAssumed(const PotentialValuesState &PVS)
Union assumed set with assumed set of the passed state PVS.
PotentialValuesState(bool IsValid)
bool operator==(const PotentialValuesState &RHS) const
bool isValidState() const override
See AbstractState::isValidState(...)
PotentialValuesState operator&=(const PotentialValuesState &PVS)
static PotentialValuesState getWorstState()
Return full set as the worst state of potential values.
const PotentialValuesState & getAssumed() const
ChangeStatus indicateOptimisticFixpoint() override
See AbstractState::indicateOptimisticFixpoint(...)
PotentialValuesState operator^=(const PotentialValuesState &PVS)
"Clamp" this state with PVS.
void unionAssumedWithUndef()
Union assumed set with an undef value.
const SetTy & getAssumedSet() const
Return this set.
A wrapper around a set that has semantics for handling unions and intersections with a "universal" se...
SetContents(bool Universal)
Creates a universal set with no concrete elements or an empty set.
SetContents(bool Universal, const DenseSet< BaseTy > &Assumptions)
bool getIntersection(const SetContents &RHS)
Finds A := A ^ B where A or B could be the "Universal" set which contains every possible attribute.
bool getUnion(const SetContents &RHS)
Finds A := A u B where A or B could be the "Universal" set which contains every possible attribute.
const DenseSet< BaseTy > & getSet() const
SetContents(const DenseSet< BaseTy > &Assumptions)
Creates a non-universal set with concrete values.
bool setContains(const BaseTy &Elem) const
Returns if the set state contains the element.
bool getIntersection(const SetContents &RHS)
Performs the set intersection between this set and RHS.
bool isValidState() const override
See AbstractState::isValidState()
bool isAtFixpoint() const override
See AbstractState::isAtFixpoint()
const SetContents & getAssumed() const
Return the assumed state encoding.
const SetContents & getKnown() const
Return the known state encoding.
ChangeStatus indicateOptimisticFixpoint() override
See AbstractState::indicateOptimisticFixpoint(...)
bool getUnion(const SetContents &RHS)
Performs the set union between this set and RHS.
SetState(const DenseSet< BaseTy > &Known)
Initializes the known state with an initial set and initializes the assumed state as universal.
ChangeStatus indicatePessimisticFixpoint() override
See AbstractState::indicatePessimisticFixpoint(...)
Helper to tie a abstract state implementation to an abstract attribute.
StateType & getState() override
See AbstractAttribute::getState(...).
StateTy StateType
Provide static access to the type of the state.
StateWrapper(const IRPosition &IRP, Ts... Args)
const StateType & getState() const override
See AbstractAttribute::getState(...).
static ValueSimplifyStateType getWorstState(Type *Ty)
Return the worst possible representable state.
static ValueSimplifyStateType getBestState(const ValueSimplifyStateType &VS)
LLVM_ABI bool unionAssumed(std::optional< Value * > Other)
Merge Other into the currently assumed simplified value.
ChangeStatus indicateOptimisticFixpoint() override
See AbstractState::indicateOptimisticFixpoint(...)
ValueSimplifyStateType operator^=(const ValueSimplifyStateType &VS)
"Clamp" this state with PVS.
ChangeStatus indicatePessimisticFixpoint() override
See AbstractState::indicatePessimisticFixpoint(...)
static ValueSimplifyStateType getBestState(Type *Ty)
bool isAtFixpoint() const override
See AbstractState::isAtFixpoint(...)
bool isValidState() const override
See AbstractState::isValidState(...)
std::optional< Value * > SimplifiedAssociatedValue
An assumed simplified value.
static ValueSimplifyStateType getWorstState(const ValueSimplifyStateType &VS)
BooleanState BS
Helper to track validity and fixpoint.
Type * Ty
The type of the original value.
bool operator==(const ValueSimplifyStateType &RHS) const
ValueSimplifyStateType getAssumed()
Return the assumed state encoding.
const ValueSimplifyStateType & getAssumed() const