LLVM 20.0.0git
Instructions.h
Go to the documentation of this file.
1//===- llvm/Instructions.h - Instruction subclass definitions ---*- 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// This file exposes the class definitions of all of the subclasses of the
10// Instruction class. This is meant to be an easy way to get access to all
11// instruction subclasses.
12//
13//===----------------------------------------------------------------------===//
14
15#ifndef LLVM_IR_INSTRUCTIONS_H
16#define LLVM_IR_INSTRUCTIONS_H
17
18#include "llvm/ADT/ArrayRef.h"
19#include "llvm/ADT/Bitfields.h"
20#include "llvm/ADT/MapVector.h"
21#include "llvm/ADT/STLExtras.h"
23#include "llvm/ADT/Twine.h"
24#include "llvm/ADT/iterator.h"
26#include "llvm/IR/CFG.h"
27#include "llvm/IR/Constant.h"
30#include "llvm/IR/InstrTypes.h"
31#include "llvm/IR/Instruction.h"
32#include "llvm/IR/Intrinsics.h"
34#include "llvm/IR/Use.h"
35#include "llvm/IR/User.h"
38#include <cassert>
39#include <cstddef>
40#include <cstdint>
41#include <iterator>
42#include <optional>
43
44namespace llvm {
45
46class APFloat;
47class APInt;
48class BasicBlock;
49class ConstantInt;
50class DataLayout;
51class StringRef;
52class Type;
53class Value;
54class UnreachableInst;
55
56//===----------------------------------------------------------------------===//
57// AllocaInst Class
58//===----------------------------------------------------------------------===//
59
60/// an instruction to allocate memory on the stack
62 Type *AllocatedType;
63
64 using AlignmentField = AlignmentBitfieldElementT<0>;
65 using UsedWithInAllocaField = BoolBitfieldElementT<AlignmentField::NextBit>;
67 static_assert(Bitfield::areContiguous<AlignmentField, UsedWithInAllocaField,
68 SwiftErrorField>(),
69 "Bitfields must be contiguous");
70
71protected:
72 // Note: Instruction needs to be a friend here to call cloneImpl.
73 friend class Instruction;
74
75 AllocaInst *cloneImpl() const;
76
77public:
78 explicit AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize,
79 const Twine &Name, InsertPosition InsertBefore);
80
81 AllocaInst(Type *Ty, unsigned AddrSpace, const Twine &Name,
82 InsertPosition InsertBefore);
83
84 AllocaInst(Type *Ty, unsigned AddrSpace, Value *ArraySize, Align Align,
85 const Twine &Name = "", InsertPosition InsertBefore = nullptr);
86
87 /// Return true if there is an allocation size parameter to the allocation
88 /// instruction that is not 1.
89 bool isArrayAllocation() const;
90
91 /// Get the number of elements allocated. For a simple allocation of a single
92 /// element, this will return a constant 1 value.
93 const Value *getArraySize() const { return getOperand(0); }
94 Value *getArraySize() { return getOperand(0); }
95
96 /// Overload to return most specific pointer type.
98 return cast<PointerType>(Instruction::getType());
99 }
100
101 /// Return the address space for the allocation.
102 unsigned getAddressSpace() const {
103 return getType()->getAddressSpace();
104 }
105
106 /// Get allocation size in bytes. Returns std::nullopt if size can't be
107 /// determined, e.g. in case of a VLA.
108 std::optional<TypeSize> getAllocationSize(const DataLayout &DL) const;
109
110 /// Get allocation size in bits. Returns std::nullopt if size can't be
111 /// determined, e.g. in case of a VLA.
112 std::optional<TypeSize> getAllocationSizeInBits(const DataLayout &DL) const;
113
114 /// Return the type that is being allocated by the instruction.
115 Type *getAllocatedType() const { return AllocatedType; }
116 /// for use only in special circumstances that need to generically
117 /// transform a whole instruction (eg: IR linking and vectorization).
118 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
119
120 /// Return the alignment of the memory that is being allocated by the
121 /// instruction.
122 Align getAlign() const {
123 return Align(1ULL << getSubclassData<AlignmentField>());
124 }
125
127 setSubclassData<AlignmentField>(Log2(Align));
128 }
129
130 /// Return true if this alloca is in the entry block of the function and is a
131 /// constant size. If so, the code generator will fold it into the
132 /// prolog/epilog code, so it is basically free.
133 bool isStaticAlloca() const;
134
135 /// Return true if this alloca is used as an inalloca argument to a call. Such
136 /// allocas are never considered static even if they are in the entry block.
137 bool isUsedWithInAlloca() const {
138 return getSubclassData<UsedWithInAllocaField>();
139 }
140
141 /// Specify whether this alloca is used to represent the arguments to a call.
142 void setUsedWithInAlloca(bool V) {
143 setSubclassData<UsedWithInAllocaField>(V);
144 }
145
146 /// Return true if this alloca is used as a swifterror argument to a call.
147 bool isSwiftError() const { return getSubclassData<SwiftErrorField>(); }
148 /// Specify whether this alloca is used to represent a swifterror.
149 void setSwiftError(bool V) { setSubclassData<SwiftErrorField>(V); }
150
151 // Methods for support type inquiry through isa, cast, and dyn_cast:
152 static bool classof(const Instruction *I) {
153 return (I->getOpcode() == Instruction::Alloca);
154 }
155 static bool classof(const Value *V) {
156 return isa<Instruction>(V) && classof(cast<Instruction>(V));
157 }
158
159private:
160 // Shadow Instruction::setInstructionSubclassData with a private forwarding
161 // method so that subclasses cannot accidentally use it.
162 template <typename Bitfield>
163 void setSubclassData(typename Bitfield::Type Value) {
164 Instruction::setSubclassData<Bitfield>(Value);
165 }
166};
167
168//===----------------------------------------------------------------------===//
169// LoadInst Class
170//===----------------------------------------------------------------------===//
171
172/// An instruction for reading from memory. This uses the SubclassData field in
173/// Value to store whether or not the load is volatile.
175 using VolatileField = BoolBitfieldElementT<0>;
178 static_assert(
179 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
180 "Bitfields must be contiguous");
181
182 void AssertOK();
183
184protected:
185 // Note: Instruction needs to be a friend here to call cloneImpl.
186 friend class Instruction;
187
188 LoadInst *cloneImpl() const;
189
190public:
191 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr,
192 InsertPosition InsertBefore);
193 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
194 InsertPosition InsertBefore);
195 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
196 Align Align, InsertPosition InsertBefore = nullptr);
197 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
200 InsertPosition InsertBefore = nullptr);
201
202 /// Return true if this is a load from a volatile memory location.
203 bool isVolatile() const { return getSubclassData<VolatileField>(); }
204
205 /// Specify whether this is a volatile load or not.
206 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
207
208 /// Return the alignment of the access that is being performed.
209 Align getAlign() const {
210 return Align(1ULL << (getSubclassData<AlignmentField>()));
211 }
212
214 setSubclassData<AlignmentField>(Log2(Align));
215 }
216
217 /// Returns the ordering constraint of this load instruction.
219 return getSubclassData<OrderingField>();
220 }
221 /// Sets the ordering constraint of this load instruction. May not be Release
222 /// or AcquireRelease.
224 setSubclassData<OrderingField>(Ordering);
225 }
226
227 /// Returns the synchronization scope ID of this load instruction.
229 return SSID;
230 }
231
232 /// Sets the synchronization scope ID of this load instruction.
234 this->SSID = SSID;
235 }
236
237 /// Sets the ordering constraint and the synchronization scope ID of this load
238 /// instruction.
241 setOrdering(Ordering);
242 setSyncScopeID(SSID);
243 }
244
245 bool isSimple() const { return !isAtomic() && !isVolatile(); }
246
247 bool isUnordered() const {
250 !isVolatile();
251 }
252
254 const Value *getPointerOperand() const { return getOperand(0); }
255 static unsigned getPointerOperandIndex() { return 0U; }
257
258 /// Returns the address space of the pointer operand.
259 unsigned getPointerAddressSpace() const {
261 }
262
263 // Methods for support type inquiry through isa, cast, and dyn_cast:
264 static bool classof(const Instruction *I) {
265 return I->getOpcode() == Instruction::Load;
266 }
267 static bool classof(const Value *V) {
268 return isa<Instruction>(V) && classof(cast<Instruction>(V));
269 }
270
271private:
272 // Shadow Instruction::setInstructionSubclassData with a private forwarding
273 // method so that subclasses cannot accidentally use it.
274 template <typename Bitfield>
275 void setSubclassData(typename Bitfield::Type Value) {
276 Instruction::setSubclassData<Bitfield>(Value);
277 }
278
279 /// The synchronization scope ID of this load instruction. Not quite enough
280 /// room in SubClassData for everything, so synchronization scope ID gets its
281 /// own field.
282 SyncScope::ID SSID;
283};
284
285//===----------------------------------------------------------------------===//
286// StoreInst Class
287//===----------------------------------------------------------------------===//
288
289/// An instruction for storing to memory.
290class StoreInst : public Instruction {
291 using VolatileField = BoolBitfieldElementT<0>;
294 static_assert(
295 Bitfield::areContiguous<VolatileField, AlignmentField, OrderingField>(),
296 "Bitfields must be contiguous");
297
298 void AssertOK();
299
300protected:
301 // Note: Instruction needs to be a friend here to call cloneImpl.
302 friend class Instruction;
303
304 StoreInst *cloneImpl() const;
305
306public:
307 StoreInst(Value *Val, Value *Ptr, InsertPosition InsertBefore);
308 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
309 InsertPosition InsertBefore);
311 InsertPosition InsertBefore = nullptr);
314 InsertPosition InsertBefore = nullptr);
315
316 // allocate space for exactly two operands
317 void *operator new(size_t S) { return User::operator new(S, 2); }
318 void operator delete(void *Ptr) { User::operator delete(Ptr); }
319
320 /// Return true if this is a store to a volatile memory location.
321 bool isVolatile() const { return getSubclassData<VolatileField>(); }
322
323 /// Specify whether this is a volatile store or not.
324 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
325
326 /// Transparently provide more efficient getOperand methods.
328
329 Align getAlign() const {
330 return Align(1ULL << (getSubclassData<AlignmentField>()));
331 }
332
334 setSubclassData<AlignmentField>(Log2(Align));
335 }
336
337 /// Returns the ordering constraint of this store instruction.
339 return getSubclassData<OrderingField>();
340 }
341
342 /// Sets the ordering constraint of this store instruction. May not be
343 /// Acquire or AcquireRelease.
345 setSubclassData<OrderingField>(Ordering);
346 }
347
348 /// Returns the synchronization scope ID of this store instruction.
350 return SSID;
351 }
352
353 /// Sets the synchronization scope ID of this store instruction.
355 this->SSID = SSID;
356 }
357
358 /// Sets the ordering constraint and the synchronization scope ID of this
359 /// store instruction.
362 setOrdering(Ordering);
363 setSyncScopeID(SSID);
364 }
365
366 bool isSimple() const { return !isAtomic() && !isVolatile(); }
367
368 bool isUnordered() const {
371 !isVolatile();
372 }
373
375 const Value *getValueOperand() const { return getOperand(0); }
376
378 const Value *getPointerOperand() const { return getOperand(1); }
379 static unsigned getPointerOperandIndex() { return 1U; }
381
382 /// Returns the address space of the pointer operand.
383 unsigned getPointerAddressSpace() const {
385 }
386
387 // Methods for support type inquiry through isa, cast, and dyn_cast:
388 static bool classof(const Instruction *I) {
389 return I->getOpcode() == Instruction::Store;
390 }
391 static bool classof(const Value *V) {
392 return isa<Instruction>(V) && classof(cast<Instruction>(V));
393 }
394
395private:
396 // Shadow Instruction::setInstructionSubclassData with a private forwarding
397 // method so that subclasses cannot accidentally use it.
398 template <typename Bitfield>
399 void setSubclassData(typename Bitfield::Type Value) {
400 Instruction::setSubclassData<Bitfield>(Value);
401 }
402
403 /// The synchronization scope ID of this store instruction. Not quite enough
404 /// room in SubClassData for everything, so synchronization scope ID gets its
405 /// own field.
406 SyncScope::ID SSID;
407};
408
409template <>
410struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
411};
412
414
415//===----------------------------------------------------------------------===//
416// FenceInst Class
417//===----------------------------------------------------------------------===//
418
419/// An instruction for ordering other memory operations.
420class FenceInst : public Instruction {
421 using OrderingField = AtomicOrderingBitfieldElementT<0>;
422
423 void Init(AtomicOrdering Ordering, SyncScope::ID SSID);
424
425protected:
426 // Note: Instruction needs to be a friend here to call cloneImpl.
427 friend class Instruction;
428
429 FenceInst *cloneImpl() const;
430
431public:
432 // Ordering may only be Acquire, Release, AcquireRelease, or
433 // SequentiallyConsistent.
436 InsertPosition InsertBefore = nullptr);
437
438 // allocate space for exactly zero operands
439 void *operator new(size_t S) { return User::operator new(S, 0); }
440 void operator delete(void *Ptr) { User::operator delete(Ptr); }
441
442 /// Returns the ordering constraint of this fence instruction.
444 return getSubclassData<OrderingField>();
445 }
446
447 /// Sets the ordering constraint of this fence instruction. May only be
448 /// Acquire, Release, AcquireRelease, or SequentiallyConsistent.
450 setSubclassData<OrderingField>(Ordering);
451 }
452
453 /// Returns the synchronization scope ID of this fence instruction.
455 return SSID;
456 }
457
458 /// Sets the synchronization scope ID of this fence instruction.
460 this->SSID = SSID;
461 }
462
463 // Methods for support type inquiry through isa, cast, and dyn_cast:
464 static bool classof(const Instruction *I) {
465 return I->getOpcode() == Instruction::Fence;
466 }
467 static bool classof(const Value *V) {
468 return isa<Instruction>(V) && classof(cast<Instruction>(V));
469 }
470
471private:
472 // Shadow Instruction::setInstructionSubclassData with a private forwarding
473 // method so that subclasses cannot accidentally use it.
474 template <typename Bitfield>
475 void setSubclassData(typename Bitfield::Type Value) {
476 Instruction::setSubclassData<Bitfield>(Value);
477 }
478
479 /// The synchronization scope ID of this fence instruction. Not quite enough
480 /// room in SubClassData for everything, so synchronization scope ID gets its
481 /// own field.
482 SyncScope::ID SSID;
483};
484
485//===----------------------------------------------------------------------===//
486// AtomicCmpXchgInst Class
487//===----------------------------------------------------------------------===//
488
489/// An instruction that atomically checks whether a
490/// specified value is in a memory location, and, if it is, stores a new value
491/// there. The value returned by this instruction is a pair containing the
492/// original value as first element, and an i1 indicating success (true) or
493/// failure (false) as second element.
494///
496 void Init(Value *Ptr, Value *Cmp, Value *NewVal, Align Align,
497 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
498 SyncScope::ID SSID);
499
500 template <unsigned Offset>
501 using AtomicOrderingBitfieldElement =
504
505protected:
506 // Note: Instruction needs to be a friend here to call cloneImpl.
507 friend class Instruction;
508
510
511public:
512 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal, Align Alignment,
513 AtomicOrdering SuccessOrdering,
514 AtomicOrdering FailureOrdering, SyncScope::ID SSID,
515 InsertPosition InsertBefore = nullptr);
516
517 // allocate space for exactly three operands
518 void *operator new(size_t S) { return User::operator new(S, 3); }
519 void operator delete(void *Ptr) { User::operator delete(Ptr); }
520
529 static_assert(
532 "Bitfields must be contiguous");
533
534 /// Return the alignment of the memory that is being allocated by the
535 /// instruction.
536 Align getAlign() const {
537 return Align(1ULL << getSubclassData<AlignmentField>());
538 }
539
541 setSubclassData<AlignmentField>(Log2(Align));
542 }
543
544 /// Return true if this is a cmpxchg from a volatile memory
545 /// location.
546 ///
547 bool isVolatile() const { return getSubclassData<VolatileField>(); }
548
549 /// Specify whether this is a volatile cmpxchg.
550 ///
551 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
552
553 /// Return true if this cmpxchg may spuriously fail.
554 bool isWeak() const { return getSubclassData<WeakField>(); }
555
556 void setWeak(bool IsWeak) { setSubclassData<WeakField>(IsWeak); }
557
558 /// Transparently provide more efficient getOperand methods.
560
562 return Ordering != AtomicOrdering::NotAtomic &&
563 Ordering != AtomicOrdering::Unordered;
564 }
565
567 return Ordering != AtomicOrdering::NotAtomic &&
568 Ordering != AtomicOrdering::Unordered &&
569 Ordering != AtomicOrdering::AcquireRelease &&
570 Ordering != AtomicOrdering::Release;
571 }
572
573 /// Returns the success ordering constraint of this cmpxchg instruction.
575 return getSubclassData<SuccessOrderingField>();
576 }
577
578 /// Sets the success ordering constraint of this cmpxchg instruction.
580 assert(isValidSuccessOrdering(Ordering) &&
581 "invalid CmpXchg success ordering");
582 setSubclassData<SuccessOrderingField>(Ordering);
583 }
584
585 /// Returns the failure ordering constraint of this cmpxchg instruction.
587 return getSubclassData<FailureOrderingField>();
588 }
589
590 /// Sets the failure ordering constraint of this cmpxchg instruction.
592 assert(isValidFailureOrdering(Ordering) &&
593 "invalid CmpXchg failure ordering");
594 setSubclassData<FailureOrderingField>(Ordering);
595 }
596
597 /// Returns a single ordering which is at least as strong as both the
598 /// success and failure orderings for this cmpxchg.
607 }
608 return getSuccessOrdering();
609 }
610
611 /// Returns the synchronization scope ID of this cmpxchg instruction.
613 return SSID;
614 }
615
616 /// Sets the synchronization scope ID of this cmpxchg instruction.
618 this->SSID = SSID;
619 }
620
622 const Value *getPointerOperand() const { return getOperand(0); }
623 static unsigned getPointerOperandIndex() { return 0U; }
624
626 const Value *getCompareOperand() const { return getOperand(1); }
627
629 const Value *getNewValOperand() const { return getOperand(2); }
630
631 /// Returns the address space of the pointer operand.
632 unsigned getPointerAddressSpace() const {
634 }
635
636 /// Returns the strongest permitted ordering on failure, given the
637 /// desired ordering on success.
638 ///
639 /// If the comparison in a cmpxchg operation fails, there is no atomic store
640 /// so release semantics cannot be provided. So this function drops explicit
641 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
642 /// operation would remain SequentiallyConsistent.
643 static AtomicOrdering
645 switch (SuccessOrdering) {
646 default:
647 llvm_unreachable("invalid cmpxchg success ordering");
656 }
657 }
658
659 // Methods for support type inquiry through isa, cast, and dyn_cast:
660 static bool classof(const Instruction *I) {
661 return I->getOpcode() == Instruction::AtomicCmpXchg;
662 }
663 static bool classof(const Value *V) {
664 return isa<Instruction>(V) && classof(cast<Instruction>(V));
665 }
666
667private:
668 // Shadow Instruction::setInstructionSubclassData with a private forwarding
669 // method so that subclasses cannot accidentally use it.
670 template <typename Bitfield>
671 void setSubclassData(typename Bitfield::Type Value) {
672 Instruction::setSubclassData<Bitfield>(Value);
673 }
674
675 /// The synchronization scope ID of this cmpxchg instruction. Not quite
676 /// enough room in SubClassData for everything, so synchronization scope ID
677 /// gets its own field.
678 SyncScope::ID SSID;
679};
680
681template <>
683 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
684};
685
687
688//===----------------------------------------------------------------------===//
689// AtomicRMWInst Class
690//===----------------------------------------------------------------------===//
691
692/// an instruction that atomically reads a memory location,
693/// combines it with another value, and then stores the result back. Returns
694/// the old value.
695///
697protected:
698 // Note: Instruction needs to be a friend here to call cloneImpl.
699 friend class Instruction;
700
701 AtomicRMWInst *cloneImpl() const;
702
703public:
704 /// This enumeration lists the possible modifications atomicrmw can make. In
705 /// the descriptions, 'p' is the pointer to the instruction's memory location,
706 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
707 /// instruction. These instructions always return 'old'.
708 enum BinOp : unsigned {
709 /// *p = v
711 /// *p = old + v
713 /// *p = old - v
715 /// *p = old & v
717 /// *p = ~(old & v)
719 /// *p = old | v
721 /// *p = old ^ v
723 /// *p = old >signed v ? old : v
725 /// *p = old <signed v ? old : v
727 /// *p = old >unsigned v ? old : v
729 /// *p = old <unsigned v ? old : v
731
732 /// *p = old + v
734
735 /// *p = old - v
737
738 /// *p = maxnum(old, v)
739 /// \p maxnum matches the behavior of \p llvm.maxnum.*.
741
742 /// *p = minnum(old, v)
743 /// \p minnum matches the behavior of \p llvm.minnum.*.
745
746 /// Increment one up to a maximum value.
747 /// *p = (old u>= v) ? 0 : (old + 1)
749
750 /// Decrement one until a minimum value or zero.
751 /// *p = ((old == 0) || (old u> v)) ? v : (old - 1)
753
754 FIRST_BINOP = Xchg,
755 LAST_BINOP = UDecWrap,
756 BAD_BINOP
757 };
758
759private:
760 template <unsigned Offset>
761 using AtomicOrderingBitfieldElement =
764
765 template <unsigned Offset>
766 using BinOpBitfieldElement =
768
769public:
770 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val, Align Alignment,
771 AtomicOrdering Ordering, SyncScope::ID SSID,
772 InsertPosition InsertBefore = nullptr);
773
774 // allocate space for exactly two operands
775 void *operator new(size_t S) { return User::operator new(S, 2); }
776 void operator delete(void *Ptr) { User::operator delete(Ptr); }
777
781 using OperationField = BinOpBitfieldElement<AtomicOrderingField::NextBit>;
785 "Bitfields must be contiguous");
786
787 BinOp getOperation() const { return getSubclassData<OperationField>(); }
788
789 static StringRef getOperationName(BinOp Op);
790
791 static bool isFPOperation(BinOp Op) {
792 switch (Op) {
797 return true;
798 default:
799 return false;
800 }
801 }
802
804 setSubclassData<OperationField>(Operation);
805 }
806
807 /// Return the alignment of the memory that is being allocated by the
808 /// instruction.
809 Align getAlign() const {
810 return Align(1ULL << getSubclassData<AlignmentField>());
811 }
812
814 setSubclassData<AlignmentField>(Log2(Align));
815 }
816
817 /// Return true if this is a RMW on a volatile memory location.
818 ///
819 bool isVolatile() const { return getSubclassData<VolatileField>(); }
820
821 /// Specify whether this is a volatile RMW or not.
822 ///
823 void setVolatile(bool V) { setSubclassData<VolatileField>(V); }
824
825 /// Transparently provide more efficient getOperand methods.
827
828 /// Returns the ordering constraint of this rmw instruction.
830 return getSubclassData<AtomicOrderingField>();
831 }
832
833 /// Sets the ordering constraint of this rmw instruction.
835 assert(Ordering != AtomicOrdering::NotAtomic &&
836 "atomicrmw instructions can only be atomic.");
837 assert(Ordering != AtomicOrdering::Unordered &&
838 "atomicrmw instructions cannot be unordered.");
839 setSubclassData<AtomicOrderingField>(Ordering);
840 }
841
842 /// Returns the synchronization scope ID of this rmw instruction.
844 return SSID;
845 }
846
847 /// Sets the synchronization scope ID of this rmw instruction.
849 this->SSID = SSID;
850 }
851
852 Value *getPointerOperand() { return getOperand(0); }
853 const Value *getPointerOperand() const { return getOperand(0); }
854 static unsigned getPointerOperandIndex() { return 0U; }
855
856 Value *getValOperand() { return getOperand(1); }
857 const Value *getValOperand() const { return getOperand(1); }
858
859 /// Returns the address space of the pointer operand.
860 unsigned getPointerAddressSpace() const {
862 }
863
865 return isFPOperation(getOperation());
866 }
867
868 // Methods for support type inquiry through isa, cast, and dyn_cast:
869 static bool classof(const Instruction *I) {
870 return I->getOpcode() == Instruction::AtomicRMW;
871 }
872 static bool classof(const Value *V) {
873 return isa<Instruction>(V) && classof(cast<Instruction>(V));
874 }
875
876private:
877 void Init(BinOp Operation, Value *Ptr, Value *Val, Align Align,
878 AtomicOrdering Ordering, SyncScope::ID SSID);
879
880 // Shadow Instruction::setInstructionSubclassData with a private forwarding
881 // method so that subclasses cannot accidentally use it.
882 template <typename Bitfield>
883 void setSubclassData(typename Bitfield::Type Value) {
884 Instruction::setSubclassData<Bitfield>(Value);
885 }
886
887 /// The synchronization scope ID of this rmw instruction. Not quite enough
888 /// room in SubClassData for everything, so synchronization scope ID gets its
889 /// own field.
890 SyncScope::ID SSID;
891};
892
893template <>
895 : public FixedNumOperandTraits<AtomicRMWInst,2> {
896};
897
899
900//===----------------------------------------------------------------------===//
901// GetElementPtrInst Class
902//===----------------------------------------------------------------------===//
903
904// checkGEPType - Simple wrapper function to give a better assertion failure
905// message on bad indexes for a gep instruction.
906//
908 assert(Ty && "Invalid GetElementPtrInst indices for type!");
909 return Ty;
910}
911
912/// an instruction for type-safe pointer arithmetic to
913/// access elements of arrays and structs
914///
916 Type *SourceElementType;
917 Type *ResultElementType;
918
920
921 /// Constructors - Create a getelementptr instruction with a base pointer an
922 /// list of indices. The first and second ctor can optionally insert before an
923 /// existing instruction, the third appends the new instruction to the
924 /// specified BasicBlock.
925 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
926 ArrayRef<Value *> IdxList, unsigned Values,
927 const Twine &NameStr, InsertPosition InsertBefore);
928
929 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
930
931protected:
932 // Note: Instruction needs to be a friend here to call cloneImpl.
933 friend class Instruction;
934
936
937public:
938 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
939 ArrayRef<Value *> IdxList,
940 const Twine &NameStr = "",
941 InsertPosition InsertBefore = nullptr) {
942 unsigned Values = 1 + unsigned(IdxList.size());
943 assert(PointeeType && "Must specify element type");
944 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
945 NameStr, InsertBefore);
946 }
947
948 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
950 const Twine &NameStr = "",
951 InsertPosition InsertBefore = nullptr) {
953 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
954 GEP->setNoWrapFlags(NW);
955 return GEP;
956 }
957
958 /// Create an "inbounds" getelementptr. See the documentation for the
959 /// "inbounds" flag in LangRef.html for details.
960 static GetElementPtrInst *
962 const Twine &NameStr = "",
963 InsertPosition InsertBefore = nullptr) {
964 return Create(PointeeType, Ptr, IdxList, GEPNoWrapFlags::inBounds(),
965 NameStr, InsertBefore);
966 }
967
968 /// Transparently provide more efficient getOperand methods.
970
971 Type *getSourceElementType() const { return SourceElementType; }
972
973 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
974 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
975
977 return ResultElementType;
978 }
979
980 /// Returns the address space of this instruction's pointer type.
981 unsigned getAddressSpace() const {
982 // Note that this is always the same as the pointer operand's address space
983 // and that is cheaper to compute, so cheat here.
984 return getPointerAddressSpace();
985 }
986
987 /// Returns the result type of a getelementptr with the given source
988 /// element type and indexes.
989 ///
990 /// Null is returned if the indices are invalid for the specified
991 /// source element type.
992 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
993 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
994 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
995
996 /// Return the type of the element at the given index of an indexable
997 /// type. This is equivalent to "getIndexedType(Agg, {Zero, Idx})".
998 ///
999 /// Returns null if the type can't be indexed, or the given index is not
1000 /// legal for the given type.
1001 static Type *getTypeAtIndex(Type *Ty, Value *Idx);
1002 static Type *getTypeAtIndex(Type *Ty, uint64_t Idx);
1003
1004 inline op_iterator idx_begin() { return op_begin()+1; }
1005 inline const_op_iterator idx_begin() const { return op_begin()+1; }
1006 inline op_iterator idx_end() { return op_end(); }
1007 inline const_op_iterator idx_end() const { return op_end(); }
1008
1010 return make_range(idx_begin(), idx_end());
1011 }
1012
1014 return make_range(idx_begin(), idx_end());
1015 }
1016
1018 return getOperand(0);
1019 }
1020 const Value *getPointerOperand() const {
1021 return getOperand(0);
1022 }
1023 static unsigned getPointerOperandIndex() {
1024 return 0U; // get index for modifying correct operand.
1025 }
1026
1027 /// Method to return the pointer operand as a
1028 /// PointerType.
1030 return getPointerOperand()->getType();
1031 }
1032
1033 /// Returns the address space of the pointer operand.
1034 unsigned getPointerAddressSpace() const {
1036 }
1037
1038 /// Returns the pointer type returned by the GEP
1039 /// instruction, which may be a vector of pointers.
1041 // Vector GEP
1042 Type *Ty = Ptr->getType();
1043 if (Ty->isVectorTy())
1044 return Ty;
1045
1046 for (Value *Index : IdxList)
1047 if (auto *IndexVTy = dyn_cast<VectorType>(Index->getType())) {
1048 ElementCount EltCount = IndexVTy->getElementCount();
1049 return VectorType::get(Ty, EltCount);
1050 }
1051 // Scalar GEP
1052 return Ty;
1053 }
1054
1055 unsigned getNumIndices() const { // Note: always non-negative
1056 return getNumOperands() - 1;
1057 }
1058
1059 bool hasIndices() const {
1060 return getNumOperands() > 1;
1061 }
1062
1063 /// Return true if all of the indices of this GEP are
1064 /// zeros. If so, the result pointer and the first operand have the same
1065 /// value, just potentially different types.
1066 bool hasAllZeroIndices() const;
1067
1068 /// Return true if all of the indices of this GEP are
1069 /// constant integers. If so, the result pointer and the first operand have
1070 /// a constant offset between them.
1071 bool hasAllConstantIndices() const;
1072
1073 /// Set nowrap flags for GEP instruction.
1075
1076 /// Set or clear the inbounds flag on this GEP instruction.
1077 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1078 /// TODO: Remove this method in favor of setNoWrapFlags().
1079 void setIsInBounds(bool b = true);
1080
1081 /// Get the nowrap flags for the GEP instruction.
1083
1084 /// Determine whether the GEP has the inbounds flag.
1085 bool isInBounds() const;
1086
1087 /// Determine whether the GEP has the nusw flag.
1088 bool hasNoUnsignedSignedWrap() const;
1089
1090 /// Determine whether the GEP has the nuw flag.
1091 bool hasNoUnsignedWrap() const;
1092
1093 /// Accumulate the constant address offset of this GEP if possible.
1094 ///
1095 /// This routine accepts an APInt into which it will accumulate the constant
1096 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1097 /// all-constant, it returns false and the value of the offset APInt is
1098 /// undefined (it is *not* preserved!). The APInt passed into this routine
1099 /// must be at least as wide as the IntPtr type for the address space of
1100 /// the base GEP pointer.
1101 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1102 bool collectOffset(const DataLayout &DL, unsigned BitWidth,
1103 MapVector<Value *, APInt> &VariableOffsets,
1104 APInt &ConstantOffset) const;
1105 // Methods for support type inquiry through isa, cast, and dyn_cast:
1106 static bool classof(const Instruction *I) {
1107 return (I->getOpcode() == Instruction::GetElementPtr);
1108 }
1109 static bool classof(const Value *V) {
1110 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1111 }
1112};
1113
1114template <>
1116 public VariadicOperandTraits<GetElementPtrInst, 1> {
1117};
1118
1119GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1120 ArrayRef<Value *> IdxList, unsigned Values,
1121 const Twine &NameStr,
1122 InsertPosition InsertBefore)
1123 : Instruction(getGEPReturnType(Ptr, IdxList), GetElementPtr,
1124 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1125 Values, InsertBefore),
1126 SourceElementType(PointeeType),
1127 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1128 init(Ptr, IdxList, NameStr);
1129}
1130
1131DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1132
1133//===----------------------------------------------------------------------===//
1134// ICmpInst Class
1135//===----------------------------------------------------------------------===//
1136
1137/// This instruction compares its operands according to the predicate given
1138/// to the constructor. It only operates on integers or pointers. The operands
1139/// must be identical types.
1140/// Represent an integer comparison operator.
1141class ICmpInst: public CmpInst {
1142 void AssertOK() {
1143 assert(isIntPredicate() &&
1144 "Invalid ICmp predicate value");
1145 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1146 "Both operands to ICmp instruction are not of the same type!");
1147 // Check that the operands are the right type
1148 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1149 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1150 "Invalid operand types for ICmp instruction");
1151 }
1152
1153protected:
1154 // Note: Instruction needs to be a friend here to call cloneImpl.
1155 friend class Instruction;
1156
1157 /// Clone an identical ICmpInst
1158 ICmpInst *cloneImpl() const;
1159
1160public:
1161 /// Constructor with insertion semantics.
1162 ICmpInst(InsertPosition InsertBefore, ///< Where to insert
1163 Predicate pred, ///< The predicate to use for the comparison
1164 Value *LHS, ///< The left-hand-side of the expression
1165 Value *RHS, ///< The right-hand-side of the expression
1166 const Twine &NameStr = "" ///< Name of the instruction
1167 )
1168 : CmpInst(makeCmpResultType(LHS->getType()), Instruction::ICmp, pred, LHS,
1169 RHS, NameStr, InsertBefore) {
1170#ifndef NDEBUG
1171 AssertOK();
1172#endif
1173 }
1174
1175 /// Constructor with no-insertion semantics
1177 Predicate pred, ///< The predicate to use for the comparison
1178 Value *LHS, ///< The left-hand-side of the expression
1179 Value *RHS, ///< The right-hand-side of the expression
1180 const Twine &NameStr = "" ///< Name of the instruction
1181 ) : CmpInst(makeCmpResultType(LHS->getType()),
1182 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1183#ifndef NDEBUG
1184 AssertOK();
1185#endif
1186 }
1187
1188 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1189 /// @returns the predicate that would be the result if the operand were
1190 /// regarded as signed.
1191 /// Return the signed version of the predicate
1193 return getSignedPredicate(getPredicate());
1194 }
1195
1196 /// This is a static version that you can use without an instruction.
1197 /// Return the signed version of the predicate.
1198 static Predicate getSignedPredicate(Predicate pred);
1199
1200 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1201 /// @returns the predicate that would be the result if the operand were
1202 /// regarded as unsigned.
1203 /// Return the unsigned version of the predicate
1205 return getUnsignedPredicate(getPredicate());
1206 }
1207
1208 /// This is a static version that you can use without an instruction.
1209 /// Return the unsigned version of the predicate.
1210 static Predicate getUnsignedPredicate(Predicate pred);
1211
1212 /// Return true if this predicate is either EQ or NE. This also
1213 /// tests for commutativity.
1214 static bool isEquality(Predicate P) {
1215 return P == ICMP_EQ || P == ICMP_NE;
1216 }
1217
1218 /// Return true if this predicate is either EQ or NE. This also
1219 /// tests for commutativity.
1220 bool isEquality() const {
1221 return isEquality(getPredicate());
1222 }
1223
1224 /// @returns true if the predicate of this ICmpInst is commutative
1225 /// Determine if this relation is commutative.
1226 bool isCommutative() const { return isEquality(); }
1227
1228 /// Return true if the predicate is relational (not EQ or NE).
1229 ///
1230 bool isRelational() const {
1231 return !isEquality();
1232 }
1233
1234 /// Return true if the predicate is relational (not EQ or NE).
1235 ///
1236 static bool isRelational(Predicate P) {
1237 return !isEquality(P);
1238 }
1239
1240 /// Return true if the predicate is SGT or UGT.
1241 ///
1242 static bool isGT(Predicate P) {
1243 return P == ICMP_SGT || P == ICMP_UGT;
1244 }
1245
1246 /// Return true if the predicate is SLT or ULT.
1247 ///
1248 static bool isLT(Predicate P) {
1249 return P == ICMP_SLT || P == ICMP_ULT;
1250 }
1251
1252 /// Return true if the predicate is SGE or UGE.
1253 ///
1254 static bool isGE(Predicate P) {
1255 return P == ICMP_SGE || P == ICMP_UGE;
1256 }
1257
1258 /// Return true if the predicate is SLE or ULE.
1259 ///
1260 static bool isLE(Predicate P) {
1261 return P == ICMP_SLE || P == ICMP_ULE;
1262 }
1263
1264 /// Returns the sequence of all ICmp predicates.
1265 ///
1266 static auto predicates() { return ICmpPredicates(); }
1267
1268 /// Exchange the two operands to this instruction in such a way that it does
1269 /// not modify the semantics of the instruction. The predicate value may be
1270 /// changed to retain the same result if the predicate is order dependent
1271 /// (e.g. ult).
1272 /// Swap operands and adjust predicate.
1274 setPredicate(getSwappedPredicate());
1275 Op<0>().swap(Op<1>());
1276 }
1277
1278 /// Return result of `LHS Pred RHS` comparison.
1279 static bool compare(const APInt &LHS, const APInt &RHS,
1280 ICmpInst::Predicate Pred);
1281
1282 // Methods for support type inquiry through isa, cast, and dyn_cast:
1283 static bool classof(const Instruction *I) {
1284 return I->getOpcode() == Instruction::ICmp;
1285 }
1286 static bool classof(const Value *V) {
1287 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1288 }
1289};
1290
1291//===----------------------------------------------------------------------===//
1292// FCmpInst Class
1293//===----------------------------------------------------------------------===//
1294
1295/// This instruction compares its operands according to the predicate given
1296/// to the constructor. It only operates on floating point values or packed
1297/// vectors of floating point values. The operands must be identical types.
1298/// Represents a floating point comparison operator.
1299class FCmpInst: public CmpInst {
1300 void AssertOK() {
1301 assert(isFPPredicate() && "Invalid FCmp predicate value");
1302 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1303 "Both operands to FCmp instruction are not of the same type!");
1304 // Check that the operands are the right type
1305 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1306 "Invalid operand types for FCmp instruction");
1307 }
1308
1309protected:
1310 // Note: Instruction needs to be a friend here to call cloneImpl.
1311 friend class Instruction;
1312
1313 /// Clone an identical FCmpInst
1314 FCmpInst *cloneImpl() const;
1315
1316public:
1317 /// Constructor with insertion semantics.
1318 FCmpInst(InsertPosition InsertBefore, ///< Where to insert
1319 Predicate pred, ///< The predicate to use for the comparison
1320 Value *LHS, ///< The left-hand-side of the expression
1321 Value *RHS, ///< The right-hand-side of the expression
1322 const Twine &NameStr = "" ///< Name of the instruction
1323 )
1325 RHS, NameStr, InsertBefore) {
1326 AssertOK();
1327 }
1328
1329 /// Constructor with no-insertion semantics
1330 FCmpInst(Predicate Pred, ///< The predicate to use for the comparison
1331 Value *LHS, ///< The left-hand-side of the expression
1332 Value *RHS, ///< The right-hand-side of the expression
1333 const Twine &NameStr = "", ///< Name of the instruction
1334 Instruction *FlagsSource = nullptr)
1335 : CmpInst(makeCmpResultType(LHS->getType()), Instruction::FCmp, Pred, LHS,
1336 RHS, NameStr, nullptr, FlagsSource) {
1337 AssertOK();
1338 }
1339
1340 /// @returns true if the predicate of this instruction is EQ or NE.
1341 /// Determine if this is an equality predicate.
1342 static bool isEquality(Predicate Pred) {
1343 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1344 Pred == FCMP_UNE;
1345 }
1346
1347 /// @returns true if the predicate of this instruction is EQ or NE.
1348 /// Determine if this is an equality predicate.
1349 bool isEquality() const { return isEquality(getPredicate()); }
1350
1351 /// @returns true if the predicate of this instruction is commutative.
1352 /// Determine if this is a commutative predicate.
1353 bool isCommutative() const {
1354 return isEquality() ||
1355 getPredicate() == FCMP_FALSE ||
1356 getPredicate() == FCMP_TRUE ||
1357 getPredicate() == FCMP_ORD ||
1359 }
1360
1361 /// @returns true if the predicate is relational (not EQ or NE).
1362 /// Determine if this a relational predicate.
1363 bool isRelational() const { return !isEquality(); }
1364
1365 /// Exchange the two operands to this instruction in such a way that it does
1366 /// not modify the semantics of the instruction. The predicate value may be
1367 /// changed to retain the same result if the predicate is order dependent
1368 /// (e.g. ult).
1369 /// Swap operands and adjust predicate.
1372 Op<0>().swap(Op<1>());
1373 }
1374
1375 /// Returns the sequence of all FCmp predicates.
1376 ///
1377 static auto predicates() { return FCmpPredicates(); }
1378
1379 /// Return result of `LHS Pred RHS` comparison.
1380 static bool compare(const APFloat &LHS, const APFloat &RHS,
1381 FCmpInst::Predicate Pred);
1382
1383 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1384 static bool classof(const Instruction *I) {
1385 return I->getOpcode() == Instruction::FCmp;
1386 }
1387 static bool classof(const Value *V) {
1388 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1389 }
1390};
1391
1392//===----------------------------------------------------------------------===//
1393/// This class represents a function call, abstracting a target
1394/// machine's calling convention. This class uses low bit of the SubClassData
1395/// field to indicate whether or not this is a tail call. The rest of the bits
1396/// hold the calling convention of the call.
1397///
1398class CallInst : public CallBase {
1399 CallInst(const CallInst &CI);
1400
1401 /// Construct a CallInst from a range of arguments
1402 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1403 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1404 InsertPosition InsertBefore);
1405
1406 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1407 const Twine &NameStr, InsertPosition InsertBefore)
1408 : CallInst(Ty, Func, Args, std::nullopt, NameStr, InsertBefore) {}
1409
1410 explicit CallInst(FunctionType *Ty, Value *F, const Twine &NameStr,
1411 InsertPosition InsertBefore);
1412
1413 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1414 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1415 void init(FunctionType *FTy, Value *Func, const Twine &NameStr);
1416
1417 /// Compute the number of operands to allocate.
1418 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
1419 // We need one operand for the called function, plus the input operand
1420 // counts provided.
1421 return 1 + NumArgs + NumBundleInputs;
1422 }
1423
1424protected:
1425 // Note: Instruction needs to be a friend here to call cloneImpl.
1426 friend class Instruction;
1427
1428 CallInst *cloneImpl() const;
1429
1430public:
1431 static CallInst *Create(FunctionType *Ty, Value *F, const Twine &NameStr = "",
1432 InsertPosition InsertBefore = nullptr) {
1433 return new (ComputeNumOperands(0)) CallInst(Ty, F, NameStr, InsertBefore);
1434 }
1435
1437 const Twine &NameStr,
1438 InsertPosition InsertBefore = nullptr) {
1439 return new (ComputeNumOperands(Args.size()))
1440 CallInst(Ty, Func, Args, std::nullopt, NameStr, InsertBefore);
1441 }
1442
1444 ArrayRef<OperandBundleDef> Bundles = std::nullopt,
1445 const Twine &NameStr = "",
1446 InsertPosition InsertBefore = nullptr) {
1447 const int NumOperands =
1448 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
1449 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1450
1451 return new (NumOperands, DescriptorBytes)
1452 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1453 }
1454
1455 static CallInst *Create(FunctionCallee Func, const Twine &NameStr = "",
1456 InsertPosition InsertBefore = nullptr) {
1457 return Create(Func.getFunctionType(), Func.getCallee(), NameStr,
1458 InsertBefore);
1459 }
1460
1462 ArrayRef<OperandBundleDef> Bundles = std::nullopt,
1463 const Twine &NameStr = "",
1464 InsertPosition InsertBefore = nullptr) {
1465 return Create(Func.getFunctionType(), Func.getCallee(), Args, Bundles,
1466 NameStr, InsertBefore);
1467 }
1468
1470 const Twine &NameStr,
1471 InsertPosition InsertBefore = nullptr) {
1472 return Create(Func.getFunctionType(), Func.getCallee(), Args, NameStr,
1473 InsertBefore);
1474 }
1475
1476 /// Create a clone of \p CI with a different set of operand bundles and
1477 /// insert it before \p InsertBefore.
1478 ///
1479 /// The returned call instruction is identical \p CI in every way except that
1480 /// the operand bundles for the new instruction are set to the operand bundles
1481 /// in \p Bundles.
1483 InsertPosition InsertPt = nullptr);
1484
1485 // Note that 'musttail' implies 'tail'.
1486 enum TailCallKind : unsigned {
1493
1495 static_assert(
1496 Bitfield::areContiguous<TailCallKindField, CallBase::CallingConvField>(),
1497 "Bitfields must be contiguous");
1498
1500 return getSubclassData<TailCallKindField>();
1501 }
1502
1503 bool isTailCall() const {
1505 return Kind == TCK_Tail || Kind == TCK_MustTail;
1506 }
1507
1508 bool isMustTailCall() const { return getTailCallKind() == TCK_MustTail; }
1509
1510 bool isNoTailCall() const { return getTailCallKind() == TCK_NoTail; }
1511
1513 setSubclassData<TailCallKindField>(TCK);
1514 }
1515
1516 void setTailCall(bool IsTc = true) {
1518 }
1519
1520 /// Return true if the call can return twice
1521 bool canReturnTwice() const { return hasFnAttr(Attribute::ReturnsTwice); }
1522 void setCanReturnTwice() { addFnAttr(Attribute::ReturnsTwice); }
1523
1524 /// Return true if the call is for a noreturn trap intrinsic.
1526 switch (getIntrinsicID()) {
1527 case Intrinsic::trap:
1528 case Intrinsic::ubsantrap:
1529 return !hasFnAttr("trap-func-name");
1530 default:
1531 return false;
1532 }
1533 }
1534
1535 // Methods for support type inquiry through isa, cast, and dyn_cast:
1536 static bool classof(const Instruction *I) {
1537 return I->getOpcode() == Instruction::Call;
1538 }
1539 static bool classof(const Value *V) {
1540 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1541 }
1542
1543 /// Updates profile metadata by scaling it by \p S / \p T.
1545
1546private:
1547 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1548 // method so that subclasses cannot accidentally use it.
1549 template <typename Bitfield>
1550 void setSubclassData(typename Bitfield::Type Value) {
1551 Instruction::setSubclassData<Bitfield>(Value);
1552 }
1553};
1554
1555CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1556 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1557 InsertPosition InsertBefore)
1558 : CallBase(Ty->getReturnType(), Instruction::Call,
1559 OperandTraits<CallBase>::op_end(this) -
1560 (Args.size() + CountBundleInputs(Bundles) + 1),
1561 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1562 InsertBefore) {
1563 init(Ty, Func, Args, Bundles, NameStr);
1564}
1565
1566//===----------------------------------------------------------------------===//
1567// SelectInst Class
1568//===----------------------------------------------------------------------===//
1569
1570/// This class represents the LLVM 'select' instruction.
1571///
1572class SelectInst : public Instruction {
1573
1574 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1575 InsertPosition InsertBefore)
1576 : Instruction(S1->getType(), Instruction::Select, &Op<0>(), 3,
1577 InsertBefore) {
1578 init(C, S1, S2);
1579 setName(NameStr);
1580 }
1581
1582 void init(Value *C, Value *S1, Value *S2) {
1583 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1584 Op<0>() = C;
1585 Op<1>() = S1;
1586 Op<2>() = S2;
1587 }
1588
1589protected:
1590 // Note: Instruction needs to be a friend here to call cloneImpl.
1591 friend class Instruction;
1592
1593 SelectInst *cloneImpl() const;
1594
1595public:
1597 const Twine &NameStr = "",
1598 InsertPosition InsertBefore = nullptr,
1599 Instruction *MDFrom = nullptr) {
1600 SelectInst *Sel = new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1601 if (MDFrom)
1602 Sel->copyMetadata(*MDFrom);
1603 return Sel;
1604 }
1605
1606 const Value *getCondition() const { return Op<0>(); }
1607 const Value *getTrueValue() const { return Op<1>(); }
1608 const Value *getFalseValue() const { return Op<2>(); }
1609 Value *getCondition() { return Op<0>(); }
1610 Value *getTrueValue() { return Op<1>(); }
1611 Value *getFalseValue() { return Op<2>(); }
1612
1613 void setCondition(Value *V) { Op<0>() = V; }
1614 void setTrueValue(Value *V) { Op<1>() = V; }
1615 void setFalseValue(Value *V) { Op<2>() = V; }
1616
1617 /// Swap the true and false values of the select instruction.
1618 /// This doesn't swap prof metadata.
1619 void swapValues() { Op<1>().swap(Op<2>()); }
1620
1621 /// Return a string if the specified operands are invalid
1622 /// for a select operation, otherwise return null.
1623 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1624
1625 /// Transparently provide more efficient getOperand methods.
1627
1629 return static_cast<OtherOps>(Instruction::getOpcode());
1630 }
1631
1632 // Methods for support type inquiry through isa, cast, and dyn_cast:
1633 static bool classof(const Instruction *I) {
1634 return I->getOpcode() == Instruction::Select;
1635 }
1636 static bool classof(const Value *V) {
1637 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1638 }
1639};
1640
1641template <>
1642struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1643};
1644
1646
1647//===----------------------------------------------------------------------===//
1648// VAArgInst Class
1649//===----------------------------------------------------------------------===//
1650
1651/// This class represents the va_arg llvm instruction, which returns
1652/// an argument of the specified type given a va_list and increments that list
1653///
1655protected:
1656 // Note: Instruction needs to be a friend here to call cloneImpl.
1657 friend class Instruction;
1658
1659 VAArgInst *cloneImpl() const;
1660
1661public:
1662 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1663 InsertPosition InsertBefore = nullptr)
1664 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1665 setName(NameStr);
1666 }
1667
1668 Value *getPointerOperand() { return getOperand(0); }
1669 const Value *getPointerOperand() const { return getOperand(0); }
1670 static unsigned getPointerOperandIndex() { return 0U; }
1671
1672 // Methods for support type inquiry through isa, cast, and dyn_cast:
1673 static bool classof(const Instruction *I) {
1674 return I->getOpcode() == VAArg;
1675 }
1676 static bool classof(const Value *V) {
1677 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1678 }
1679};
1680
1681//===----------------------------------------------------------------------===//
1682// ExtractElementInst Class
1683//===----------------------------------------------------------------------===//
1684
1685/// This instruction extracts a single (scalar)
1686/// element from a VectorType value
1687///
1689 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1690 InsertPosition InsertBefore = nullptr);
1691
1692protected:
1693 // Note: Instruction needs to be a friend here to call cloneImpl.
1694 friend class Instruction;
1695
1697
1698public:
1700 const Twine &NameStr = "",
1701 InsertPosition InsertBefore = nullptr) {
1702 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1703 }
1704
1705 /// Return true if an extractelement instruction can be
1706 /// formed with the specified operands.
1707 static bool isValidOperands(const Value *Vec, const Value *Idx);
1708
1710 Value *getIndexOperand() { return Op<1>(); }
1711 const Value *getVectorOperand() const { return Op<0>(); }
1712 const Value *getIndexOperand() const { return Op<1>(); }
1713
1715 return cast<VectorType>(getVectorOperand()->getType());
1716 }
1717
1718 /// Transparently provide more efficient getOperand methods.
1720
1721 // Methods for support type inquiry through isa, cast, and dyn_cast:
1722 static bool classof(const Instruction *I) {
1723 return I->getOpcode() == Instruction::ExtractElement;
1724 }
1725 static bool classof(const Value *V) {
1726 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1727 }
1728};
1729
1730template <>
1732 public FixedNumOperandTraits<ExtractElementInst, 2> {
1733};
1734
1736
1737//===----------------------------------------------------------------------===//
1738// InsertElementInst Class
1739//===----------------------------------------------------------------------===//
1740
1741/// This instruction inserts a single (scalar)
1742/// element into a VectorType value
1743///
1745 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1746 const Twine &NameStr = "",
1747 InsertPosition InsertBefore = nullptr);
1748
1749protected:
1750 // Note: Instruction needs to be a friend here to call cloneImpl.
1751 friend class Instruction;
1752
1753 InsertElementInst *cloneImpl() const;
1754
1755public:
1756 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1757 const Twine &NameStr = "",
1758 InsertPosition InsertBefore = nullptr) {
1759 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1760 }
1761
1762 /// Return true if an insertelement instruction can be
1763 /// formed with the specified operands.
1764 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1765 const Value *Idx);
1766
1767 /// Overload to return most specific vector type.
1768 ///
1770 return cast<VectorType>(Instruction::getType());
1771 }
1772
1773 /// Transparently provide more efficient getOperand methods.
1775
1776 // Methods for support type inquiry through isa, cast, and dyn_cast:
1777 static bool classof(const Instruction *I) {
1778 return I->getOpcode() == Instruction::InsertElement;
1779 }
1780 static bool classof(const Value *V) {
1781 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1782 }
1783};
1784
1785template <>
1787 public FixedNumOperandTraits<InsertElementInst, 3> {
1788};
1789
1791
1792//===----------------------------------------------------------------------===//
1793// ShuffleVectorInst Class
1794//===----------------------------------------------------------------------===//
1795
1796constexpr int PoisonMaskElem = -1;
1797
1798/// This instruction constructs a fixed permutation of two
1799/// input vectors.
1800///
1801/// For each element of the result vector, the shuffle mask selects an element
1802/// from one of the input vectors to copy to the result. Non-negative elements
1803/// in the mask represent an index into the concatenated pair of input vectors.
1804/// PoisonMaskElem (-1) specifies that the result element is poison.
1805///
1806/// For scalable vectors, all the elements of the mask must be 0 or -1. This
1807/// requirement may be relaxed in the future.
1809 SmallVector<int, 4> ShuffleMask;
1810 Constant *ShuffleMaskForBitcode;
1811
1812protected:
1813 // Note: Instruction needs to be a friend here to call cloneImpl.
1814 friend class Instruction;
1815
1816 ShuffleVectorInst *cloneImpl() const;
1817
1818public:
1819 ShuffleVectorInst(Value *V1, Value *Mask, const Twine &NameStr = "",
1820 InsertPosition InsertBefore = nullptr);
1821 ShuffleVectorInst(Value *V1, ArrayRef<int> Mask, const Twine &NameStr = "",
1822 InsertPosition InsertBefore = nullptr);
1823 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1824 const Twine &NameStr = "",
1825 InsertPosition InsertBefore = nullptr);
1827 const Twine &NameStr = "",
1828 InsertPosition InsertBefore = nullptr);
1829
1830 void *operator new(size_t S) { return User::operator new(S, 2); }
1831 void operator delete(void *Ptr) { return User::operator delete(Ptr); }
1832
1833 /// Swap the operands and adjust the mask to preserve the semantics
1834 /// of the instruction.
1835 void commute();
1836
1837 /// Return true if a shufflevector instruction can be
1838 /// formed with the specified operands.
1839 static bool isValidOperands(const Value *V1, const Value *V2,
1840 const Value *Mask);
1841 static bool isValidOperands(const Value *V1, const Value *V2,
1842 ArrayRef<int> Mask);
1843
1844 /// Overload to return most specific vector type.
1845 ///
1847 return cast<VectorType>(Instruction::getType());
1848 }
1849
1850 /// Transparently provide more efficient getOperand methods.
1852
1853 /// Return the shuffle mask value of this instruction for the given element
1854 /// index. Return PoisonMaskElem if the element is undef.
1855 int getMaskValue(unsigned Elt) const { return ShuffleMask[Elt]; }
1856
1857 /// Convert the input shuffle mask operand to a vector of integers. Undefined
1858 /// elements of the mask are returned as PoisonMaskElem.
1859 static void getShuffleMask(const Constant *Mask,
1860 SmallVectorImpl<int> &Result);
1861
1862 /// Return the mask for this instruction as a vector of integers. Undefined
1863 /// elements of the mask are returned as PoisonMaskElem.
1865 Result.assign(ShuffleMask.begin(), ShuffleMask.end());
1866 }
1867
1868 /// Return the mask for this instruction, for use in bitcode.
1869 ///
1870 /// TODO: This is temporary until we decide a new bitcode encoding for
1871 /// shufflevector.
1872 Constant *getShuffleMaskForBitcode() const { return ShuffleMaskForBitcode; }
1873
1874 static Constant *convertShuffleMaskForBitcode(ArrayRef<int> Mask,
1875 Type *ResultTy);
1876
1877 void setShuffleMask(ArrayRef<int> Mask);
1878
1879 ArrayRef<int> getShuffleMask() const { return ShuffleMask; }
1880
1881 /// Return true if this shuffle returns a vector with a different number of
1882 /// elements than its source vectors.
1883 /// Examples: shufflevector <4 x n> A, <4 x n> B, <1,2,3>
1884 /// shufflevector <4 x n> A, <4 x n> B, <1,2,3,4,5>
1885 bool changesLength() const {
1886 unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType())
1887 ->getElementCount()
1888 .getKnownMinValue();
1889 unsigned NumMaskElts = ShuffleMask.size();
1890 return NumSourceElts != NumMaskElts;
1891 }
1892
1893 /// Return true if this shuffle returns a vector with a greater number of
1894 /// elements than its source vectors.
1895 /// Example: shufflevector <2 x n> A, <2 x n> B, <1,2,3>
1896 bool increasesLength() const {
1897 unsigned NumSourceElts = cast<VectorType>(Op<0>()->getType())
1898 ->getElementCount()
1899 .getKnownMinValue();
1900 unsigned NumMaskElts = ShuffleMask.size();
1901 return NumSourceElts < NumMaskElts;
1902 }
1903
1904 /// Return true if this shuffle mask chooses elements from exactly one source
1905 /// vector.
1906 /// Example: <7,5,undef,7>
1907 /// This assumes that vector operands (of length \p NumSrcElts) are the same
1908 /// length as the mask.
1909 static bool isSingleSourceMask(ArrayRef<int> Mask, int NumSrcElts);
1910 static bool isSingleSourceMask(const Constant *Mask, int NumSrcElts) {
1911 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
1912 SmallVector<int, 16> MaskAsInts;
1913 getShuffleMask(Mask, MaskAsInts);
1914 return isSingleSourceMask(MaskAsInts, NumSrcElts);
1915 }
1916
1917 /// Return true if this shuffle chooses elements from exactly one source
1918 /// vector without changing the length of that vector.
1919 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,0,undef,3>
1920 /// TODO: Optionally allow length-changing shuffles.
1921 bool isSingleSource() const {
1922 return !changesLength() &&
1923 isSingleSourceMask(ShuffleMask, ShuffleMask.size());
1924 }
1925
1926 /// Return true if this shuffle mask chooses elements from exactly one source
1927 /// vector without lane crossings. A shuffle using this mask is not
1928 /// necessarily a no-op because it may change the number of elements from its
1929 /// input vectors or it may provide demanded bits knowledge via undef lanes.
1930 /// Example: <undef,undef,2,3>
1931 static bool isIdentityMask(ArrayRef<int> Mask, int NumSrcElts);
1932 static bool isIdentityMask(const Constant *Mask, int NumSrcElts) {
1933 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
1934
1935 // Not possible to express a shuffle mask for a scalable vector for this
1936 // case.
1937 if (isa<ScalableVectorType>(Mask->getType()))
1938 return false;
1939
1940 SmallVector<int, 16> MaskAsInts;
1941 getShuffleMask(Mask, MaskAsInts);
1942 return isIdentityMask(MaskAsInts, NumSrcElts);
1943 }
1944
1945 /// Return true if this shuffle chooses elements from exactly one source
1946 /// vector without lane crossings and does not change the number of elements
1947 /// from its input vectors.
1948 /// Example: shufflevector <4 x n> A, <4 x n> B, <4,undef,6,undef>
1949 bool isIdentity() const {
1950 // Not possible to express a shuffle mask for a scalable vector for this
1951 // case.
1952 if (isa<ScalableVectorType>(getType()))
1953 return false;
1954
1955 return !changesLength() && isIdentityMask(ShuffleMask, ShuffleMask.size());
1956 }
1957
1958 /// Return true if this shuffle lengthens exactly one source vector with
1959 /// undefs in the high elements.
1960 bool isIdentityWithPadding() const;
1961
1962 /// Return true if this shuffle extracts the first N elements of exactly one
1963 /// source vector.
1964 bool isIdentityWithExtract() const;
1965
1966 /// Return true if this shuffle concatenates its 2 source vectors. This
1967 /// returns false if either input is undefined. In that case, the shuffle is
1968 /// is better classified as an identity with padding operation.
1969 bool isConcat() const;
1970
1971 /// Return true if this shuffle mask chooses elements from its source vectors
1972 /// without lane crossings. A shuffle using this mask would be
1973 /// equivalent to a vector select with a constant condition operand.
1974 /// Example: <4,1,6,undef>
1975 /// This returns false if the mask does not choose from both input vectors.
1976 /// In that case, the shuffle is better classified as an identity shuffle.
1977 /// This assumes that vector operands are the same length as the mask
1978 /// (a length-changing shuffle can never be equivalent to a vector select).
1979 static bool isSelectMask(ArrayRef<int> Mask, int NumSrcElts);
1980 static bool isSelectMask(const Constant *Mask, int NumSrcElts) {
1981 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
1982 SmallVector<int, 16> MaskAsInts;
1983 getShuffleMask(Mask, MaskAsInts);
1984 return isSelectMask(MaskAsInts, NumSrcElts);
1985 }
1986
1987 /// Return true if this shuffle chooses elements from its source vectors
1988 /// without lane crossings and all operands have the same number of elements.
1989 /// In other words, this shuffle is equivalent to a vector select with a
1990 /// constant condition operand.
1991 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,1,6,3>
1992 /// This returns false if the mask does not choose from both input vectors.
1993 /// In that case, the shuffle is better classified as an identity shuffle.
1994 /// TODO: Optionally allow length-changing shuffles.
1995 bool isSelect() const {
1996 return !changesLength() && isSelectMask(ShuffleMask, ShuffleMask.size());
1997 }
1998
1999 /// Return true if this shuffle mask swaps the order of elements from exactly
2000 /// one source vector.
2001 /// Example: <7,6,undef,4>
2002 /// This assumes that vector operands (of length \p NumSrcElts) are the same
2003 /// length as the mask.
2004 static bool isReverseMask(ArrayRef<int> Mask, int NumSrcElts);
2005 static bool isReverseMask(const Constant *Mask, int NumSrcElts) {
2006 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2007 SmallVector<int, 16> MaskAsInts;
2008 getShuffleMask(Mask, MaskAsInts);
2009 return isReverseMask(MaskAsInts, NumSrcElts);
2010 }
2011
2012 /// Return true if this shuffle swaps the order of elements from exactly
2013 /// one source vector.
2014 /// Example: shufflevector <4 x n> A, <4 x n> B, <3,undef,1,undef>
2015 /// TODO: Optionally allow length-changing shuffles.
2016 bool isReverse() const {
2017 return !changesLength() && isReverseMask(ShuffleMask, ShuffleMask.size());
2018 }
2019
2020 /// Return true if this shuffle mask chooses all elements with the same value
2021 /// as the first element of exactly one source vector.
2022 /// Example: <4,undef,undef,4>
2023 /// This assumes that vector operands (of length \p NumSrcElts) are the same
2024 /// length as the mask.
2025 static bool isZeroEltSplatMask(ArrayRef<int> Mask, int NumSrcElts);
2026 static bool isZeroEltSplatMask(const Constant *Mask, int NumSrcElts) {
2027 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2028 SmallVector<int, 16> MaskAsInts;
2029 getShuffleMask(Mask, MaskAsInts);
2030 return isZeroEltSplatMask(MaskAsInts, NumSrcElts);
2031 }
2032
2033 /// Return true if all elements of this shuffle are the same value as the
2034 /// first element of exactly one source vector without changing the length
2035 /// of that vector.
2036 /// Example: shufflevector <4 x n> A, <4 x n> B, <undef,0,undef,0>
2037 /// TODO: Optionally allow length-changing shuffles.
2038 /// TODO: Optionally allow splats from other elements.
2039 bool isZeroEltSplat() const {
2040 return !changesLength() &&
2041 isZeroEltSplatMask(ShuffleMask, ShuffleMask.size());
2042 }
2043
2044 /// Return true if this shuffle mask is a transpose mask.
2045 /// Transpose vector masks transpose a 2xn matrix. They read corresponding
2046 /// even- or odd-numbered vector elements from two n-dimensional source
2047 /// vectors and write each result into consecutive elements of an
2048 /// n-dimensional destination vector. Two shuffles are necessary to complete
2049 /// the transpose, one for the even elements and another for the odd elements.
2050 /// This description closely follows how the TRN1 and TRN2 AArch64
2051 /// instructions operate.
2052 ///
2053 /// For example, a simple 2x2 matrix can be transposed with:
2054 ///
2055 /// ; Original matrix
2056 /// m0 = < a, b >
2057 /// m1 = < c, d >
2058 ///
2059 /// ; Transposed matrix
2060 /// t0 = < a, c > = shufflevector m0, m1, < 0, 2 >
2061 /// t1 = < b, d > = shufflevector m0, m1, < 1, 3 >
2062 ///
2063 /// For matrices having greater than n columns, the resulting nx2 transposed
2064 /// matrix is stored in two result vectors such that one vector contains
2065 /// interleaved elements from all the even-numbered rows and the other vector
2066 /// contains interleaved elements from all the odd-numbered rows. For example,
2067 /// a 2x4 matrix can be transposed with:
2068 ///
2069 /// ; Original matrix
2070 /// m0 = < a, b, c, d >
2071 /// m1 = < e, f, g, h >
2072 ///
2073 /// ; Transposed matrix
2074 /// t0 = < a, e, c, g > = shufflevector m0, m1 < 0, 4, 2, 6 >
2075 /// t1 = < b, f, d, h > = shufflevector m0, m1 < 1, 5, 3, 7 >
2076 static bool isTransposeMask(ArrayRef<int> Mask, int NumSrcElts);
2077 static bool isTransposeMask(const Constant *Mask, int NumSrcElts) {
2078 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2079 SmallVector<int, 16> MaskAsInts;
2080 getShuffleMask(Mask, MaskAsInts);
2081 return isTransposeMask(MaskAsInts, NumSrcElts);
2082 }
2083
2084 /// Return true if this shuffle transposes the elements of its inputs without
2085 /// changing the length of the vectors. This operation may also be known as a
2086 /// merge or interleave. See the description for isTransposeMask() for the
2087 /// exact specification.
2088 /// Example: shufflevector <4 x n> A, <4 x n> B, <0,4,2,6>
2089 bool isTranspose() const {
2090 return !changesLength() && isTransposeMask(ShuffleMask, ShuffleMask.size());
2091 }
2092
2093 /// Return true if this shuffle mask is a splice mask, concatenating the two
2094 /// inputs together and then extracts an original width vector starting from
2095 /// the splice index.
2096 /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
2097 /// This assumes that vector operands (of length \p NumSrcElts) are the same
2098 /// length as the mask.
2099 static bool isSpliceMask(ArrayRef<int> Mask, int NumSrcElts, int &Index);
2100 static bool isSpliceMask(const Constant *Mask, int NumSrcElts, int &Index) {
2101 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2102 SmallVector<int, 16> MaskAsInts;
2103 getShuffleMask(Mask, MaskAsInts);
2104 return isSpliceMask(MaskAsInts, NumSrcElts, Index);
2105 }
2106
2107 /// Return true if this shuffle splices two inputs without changing the length
2108 /// of the vectors. This operation concatenates the two inputs together and
2109 /// then extracts an original width vector starting from the splice index.
2110 /// Example: shufflevector <4 x n> A, <4 x n> B, <1,2,3,4>
2111 bool isSplice(int &Index) const {
2112 return !changesLength() &&
2113 isSpliceMask(ShuffleMask, ShuffleMask.size(), Index);
2114 }
2115
2116 /// Return true if this shuffle mask is an extract subvector mask.
2117 /// A valid extract subvector mask returns a smaller vector from a single
2118 /// source operand. The base extraction index is returned as well.
2119 static bool isExtractSubvectorMask(ArrayRef<int> Mask, int NumSrcElts,
2120 int &Index);
2121 static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts,
2122 int &Index) {
2123 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2124 // Not possible to express a shuffle mask for a scalable vector for this
2125 // case.
2126 if (isa<ScalableVectorType>(Mask->getType()))
2127 return false;
2128 SmallVector<int, 16> MaskAsInts;
2129 getShuffleMask(Mask, MaskAsInts);
2130 return isExtractSubvectorMask(MaskAsInts, NumSrcElts, Index);
2131 }
2132
2133 /// Return true if this shuffle mask is an extract subvector mask.
2135 // Not possible to express a shuffle mask for a scalable vector for this
2136 // case.
2137 if (isa<ScalableVectorType>(getType()))
2138 return false;
2139
2140 int NumSrcElts =
2141 cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2142 return isExtractSubvectorMask(ShuffleMask, NumSrcElts, Index);
2143 }
2144
2145 /// Return true if this shuffle mask is an insert subvector mask.
2146 /// A valid insert subvector mask inserts the lowest elements of a second
2147 /// source operand into an in-place first source operand.
2148 /// Both the sub vector width and the insertion index is returned.
2149 static bool isInsertSubvectorMask(ArrayRef<int> Mask, int NumSrcElts,
2150 int &NumSubElts, int &Index);
2151 static bool isInsertSubvectorMask(const Constant *Mask, int NumSrcElts,
2152 int &NumSubElts, int &Index) {
2153 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2154 // Not possible to express a shuffle mask for a scalable vector for this
2155 // case.
2156 if (isa<ScalableVectorType>(Mask->getType()))
2157 return false;
2158 SmallVector<int, 16> MaskAsInts;
2159 getShuffleMask(Mask, MaskAsInts);
2160 return isInsertSubvectorMask(MaskAsInts, NumSrcElts, NumSubElts, Index);
2161 }
2162
2163 /// Return true if this shuffle mask is an insert subvector mask.
2164 bool isInsertSubvectorMask(int &NumSubElts, int &Index) const {
2165 // Not possible to express a shuffle mask for a scalable vector for this
2166 // case.
2167 if (isa<ScalableVectorType>(getType()))
2168 return false;
2169
2170 int NumSrcElts =
2171 cast<FixedVectorType>(Op<0>()->getType())->getNumElements();
2172 return isInsertSubvectorMask(ShuffleMask, NumSrcElts, NumSubElts, Index);
2173 }
2174
2175 /// Return true if this shuffle mask replicates each of the \p VF elements
2176 /// in a vector \p ReplicationFactor times.
2177 /// For example, the mask for \p ReplicationFactor=3 and \p VF=4 is:
2178 /// <0,0,0,1,1,1,2,2,2,3,3,3>
2179 static bool isReplicationMask(ArrayRef<int> Mask, int &ReplicationFactor,
2180 int &VF);
2181 static bool isReplicationMask(const Constant *Mask, int &ReplicationFactor,
2182 int &VF) {
2183 assert(Mask->getType()->isVectorTy() && "Shuffle needs vector constant.");
2184 // Not possible to express a shuffle mask for a scalable vector for this
2185 // case.
2186 if (isa<ScalableVectorType>(Mask->getType()))
2187 return false;
2188 SmallVector<int, 16> MaskAsInts;
2189 getShuffleMask(Mask, MaskAsInts);
2190 return isReplicationMask(MaskAsInts, ReplicationFactor, VF);
2191 }
2192
2193 /// Return true if this shuffle mask is a replication mask.
2194 bool isReplicationMask(int &ReplicationFactor, int &VF) const;
2195
2196 /// Return true if this shuffle mask represents "clustered" mask of size VF,
2197 /// i.e. each index between [0..VF) is used exactly once in each submask of
2198 /// size VF.
2199 /// For example, the mask for \p VF=4 is:
2200 /// 0, 1, 2, 3, 3, 2, 0, 1 - "clustered", because each submask of size 4
2201 /// (0,1,2,3 and 3,2,0,1) uses indices [0..VF) exactly one time.
2202 /// 0, 1, 2, 3, 3, 3, 1, 0 - not "clustered", because
2203 /// element 3 is used twice in the second submask
2204 /// (3,3,1,0) and index 2 is not used at all.
2205 static bool isOneUseSingleSourceMask(ArrayRef<int> Mask, int VF);
2206
2207 /// Return true if this shuffle mask is a one-use-single-source("clustered")
2208 /// mask.
2209 bool isOneUseSingleSourceMask(int VF) const;
2210
2211 /// Change values in a shuffle permute mask assuming the two vector operands
2212 /// of length InVecNumElts have swapped position.
2214 unsigned InVecNumElts) {
2215 for (int &Idx : Mask) {
2216 if (Idx == -1)
2217 continue;
2218 Idx = Idx < (int)InVecNumElts ? Idx + InVecNumElts : Idx - InVecNumElts;
2219 assert(Idx >= 0 && Idx < (int)InVecNumElts * 2 &&
2220 "shufflevector mask index out of range");
2221 }
2222 }
2223
2224 /// Return if this shuffle interleaves its two input vectors together.
2225 bool isInterleave(unsigned Factor);
2226
2227 /// Return true if the mask interleaves one or more input vectors together.
2228 ///
2229 /// I.e. <0, LaneLen, ... , LaneLen*(Factor - 1), 1, LaneLen + 1, ...>
2230 /// E.g. For a Factor of 2 (LaneLen=4):
2231 /// <0, 4, 1, 5, 2, 6, 3, 7>
2232 /// E.g. For a Factor of 3 (LaneLen=4):
2233 /// <4, 0, 9, 5, 1, 10, 6, 2, 11, 7, 3, 12>
2234 /// E.g. For a Factor of 4 (LaneLen=2):
2235 /// <0, 2, 6, 4, 1, 3, 7, 5>
2236 ///
2237 /// NumInputElts is the total number of elements in the input vectors.
2238 ///
2239 /// StartIndexes are the first indexes of each vector being interleaved,
2240 /// substituting any indexes that were undef
2241 /// E.g. <4, -1, 2, 5, 1, 3> (Factor=3): StartIndexes=<4, 0, 2>
2242 ///
2243 /// Note that this does not check if the input vectors are consecutive:
2244 /// It will return true for masks such as
2245 /// <0, 4, 6, 1, 5, 7> (Factor=3, LaneLen=2)
2246 static bool isInterleaveMask(ArrayRef<int> Mask, unsigned Factor,
2247 unsigned NumInputElts,
2248 SmallVectorImpl<unsigned> &StartIndexes);
2249 static bool isInterleaveMask(ArrayRef<int> Mask, unsigned Factor,
2250 unsigned NumInputElts) {
2251 SmallVector<unsigned, 8> StartIndexes;
2252 return isInterleaveMask(Mask, Factor, NumInputElts, StartIndexes);
2253 }
2254
2255 /// Check if the mask is a DE-interleave mask of the given factor
2256 /// \p Factor like:
2257 /// <Index, Index+Factor, ..., Index+(NumElts-1)*Factor>
2258 static bool isDeInterleaveMaskOfFactor(ArrayRef<int> Mask, unsigned Factor,
2259 unsigned &Index);
2260 static bool isDeInterleaveMaskOfFactor(ArrayRef<int> Mask, unsigned Factor) {
2261 unsigned Unused;
2262 return isDeInterleaveMaskOfFactor(Mask, Factor, Unused);
2263 }
2264
2265 /// Checks if the shuffle is a bit rotation of the first operand across
2266 /// multiple subelements, e.g:
2267 ///
2268 /// shuffle <8 x i8> %a, <8 x i8> poison, <8 x i32> <1, 0, 3, 2, 5, 4, 7, 6>
2269 ///
2270 /// could be expressed as
2271 ///
2272 /// rotl <4 x i16> %a, 8
2273 ///
2274 /// If it can be expressed as a rotation, returns the number of subelements to
2275 /// group by in NumSubElts and the number of bits to rotate left in RotateAmt.
2276 static bool isBitRotateMask(ArrayRef<int> Mask, unsigned EltSizeInBits,
2277 unsigned MinSubElts, unsigned MaxSubElts,
2278 unsigned &NumSubElts, unsigned &RotateAmt);
2279
2280 // Methods for support type inquiry through isa, cast, and dyn_cast:
2281 static bool classof(const Instruction *I) {
2282 return I->getOpcode() == Instruction::ShuffleVector;
2283 }
2284 static bool classof(const Value *V) {
2285 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2286 }
2287};
2288
2289template <>
2291 : public FixedNumOperandTraits<ShuffleVectorInst, 2> {};
2292
2294
2295//===----------------------------------------------------------------------===//
2296// ExtractValueInst Class
2297//===----------------------------------------------------------------------===//
2298
2299/// This instruction extracts a struct member or array
2300/// element value from an aggregate value.
2301///
2304
2306
2307 /// Constructors - Create a extractvalue instruction with a base aggregate
2308 /// value and a list of indices. The first and second ctor can optionally
2309 /// insert before an existing instruction, the third appends the new
2310 /// instruction to the specified BasicBlock.
2311 inline ExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
2312 const Twine &NameStr, InsertPosition InsertBefore);
2313
2314 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2315
2316protected:
2317 // Note: Instruction needs to be a friend here to call cloneImpl.
2318 friend class Instruction;
2319
2320 ExtractValueInst *cloneImpl() const;
2321
2322public:
2324 const Twine &NameStr = "",
2325 InsertPosition InsertBefore = nullptr) {
2326 return new
2327 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2328 }
2329
2330 /// Returns the type of the element that would be extracted
2331 /// with an extractvalue instruction with the specified parameters.
2332 ///
2333 /// Null is returned if the indices are invalid for the specified type.
2334 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2335
2336 using idx_iterator = const unsigned*;
2337
2338 inline idx_iterator idx_begin() const { return Indices.begin(); }
2339 inline idx_iterator idx_end() const { return Indices.end(); }
2341 return make_range(idx_begin(), idx_end());
2342 }
2343
2345 return getOperand(0);
2346 }
2348 return getOperand(0);
2349 }
2350 static unsigned getAggregateOperandIndex() {
2351 return 0U; // get index for modifying correct operand
2352 }
2353
2355 return Indices;
2356 }
2357
2358 unsigned getNumIndices() const {
2359 return (unsigned)Indices.size();
2360 }
2361
2362 bool hasIndices() const {
2363 return true;
2364 }
2365
2366 // Methods for support type inquiry through isa, cast, and dyn_cast:
2367 static bool classof(const Instruction *I) {
2368 return I->getOpcode() == Instruction::ExtractValue;
2369 }
2370 static bool classof(const Value *V) {
2371 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2372 }
2373};
2374
2375ExtractValueInst::ExtractValueInst(Value *Agg, ArrayRef<unsigned> Idxs,
2376 const Twine &NameStr,
2377 InsertPosition InsertBefore)
2378 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2379 ExtractValue, Agg, InsertBefore) {
2380 init(Idxs, NameStr);
2381}
2382
2383//===----------------------------------------------------------------------===//
2384// InsertValueInst Class
2385//===----------------------------------------------------------------------===//
2386
2387/// This instruction inserts a struct field of array element
2388/// value into an aggregate value.
2389///
2392
2393 InsertValueInst(const InsertValueInst &IVI);
2394
2395 /// Constructors - Create a insertvalue instruction with a base aggregate
2396 /// value, a value to insert, and a list of indices. The first and second ctor
2397 /// can optionally insert before an existing instruction, the third appends
2398 /// the new instruction to the specified BasicBlock.
2399 inline InsertValueInst(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2400 const Twine &NameStr, InsertPosition InsertBefore);
2401
2402 /// Constructors - These three constructors are convenience methods because
2403 /// one and two index insertvalue instructions are so common.
2404 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2405 const Twine &NameStr = "",
2406 InsertPosition InsertBefore = nullptr);
2407
2408 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2409 const Twine &NameStr);
2410
2411protected:
2412 // Note: Instruction needs to be a friend here to call cloneImpl.
2413 friend class Instruction;
2414
2415 InsertValueInst *cloneImpl() const;
2416
2417public:
2418 // allocate space for exactly two operands
2419 void *operator new(size_t S) { return User::operator new(S, 2); }
2420 void operator delete(void *Ptr) { User::operator delete(Ptr); }
2421
2422 static InsertValueInst *Create(Value *Agg, Value *Val,
2423 ArrayRef<unsigned> Idxs,
2424 const Twine &NameStr = "",
2425 InsertPosition InsertBefore = nullptr) {
2426 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2427 }
2428
2429 /// Transparently provide more efficient getOperand methods.
2431
2432 using idx_iterator = const unsigned*;
2433
2434 inline idx_iterator idx_begin() const { return Indices.begin(); }
2435 inline idx_iterator idx_end() const { return Indices.end(); }
2437 return make_range(idx_begin(), idx_end());
2438 }
2439
2441 return getOperand(0);
2442 }
2444 return getOperand(0);
2445 }
2446 static unsigned getAggregateOperandIndex() {
2447 return 0U; // get index for modifying correct operand
2448 }
2449
2451 return getOperand(1);
2452 }
2454 return getOperand(1);
2455 }
2457 return 1U; // get index for modifying correct operand
2458 }
2459
2461 return Indices;
2462 }
2463
2464 unsigned getNumIndices() const {
2465 return (unsigned)Indices.size();
2466 }
2467
2468 bool hasIndices() const {
2469 return true;
2470 }
2471
2472 // Methods for support type inquiry through isa, cast, and dyn_cast:
2473 static bool classof(const Instruction *I) {
2474 return I->getOpcode() == Instruction::InsertValue;
2475 }
2476 static bool classof(const Value *V) {
2477 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2478 }
2479};
2480
2481template <>
2483 public FixedNumOperandTraits<InsertValueInst, 2> {
2484};
2485
2486InsertValueInst::InsertValueInst(Value *Agg, Value *Val,
2487 ArrayRef<unsigned> Idxs, const Twine &NameStr,
2488 InsertPosition InsertBefore)
2489 : Instruction(Agg->getType(), InsertValue,
2490 OperandTraits<InsertValueInst>::op_begin(this), 2,
2491 InsertBefore) {
2492 init(Agg, Val, Idxs, NameStr);
2493}
2494
2495DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2496
2497//===----------------------------------------------------------------------===//
2498// PHINode Class
2499//===----------------------------------------------------------------------===//
2500
2501// PHINode - The PHINode class is used to represent the magical mystical PHI
2502// node, that can not exist in nature, but can be synthesized in a computer
2503// scientist's overactive imagination.
2504//
2505class PHINode : public Instruction {
2506 /// The number of operands actually allocated. NumOperands is
2507 /// the number actually in use.
2508 unsigned ReservedSpace;
2509
2510 PHINode(const PHINode &PN);
2511
2512 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2513 const Twine &NameStr = "",
2514 InsertPosition InsertBefore = nullptr)
2515 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2516 ReservedSpace(NumReservedValues) {
2517 assert(!Ty->isTokenTy() && "PHI nodes cannot have token type!");
2518 setName(NameStr);
2519 allocHungoffUses(ReservedSpace);
2520 }
2521
2522protected:
2523 // Note: Instruction needs to be a friend here to call cloneImpl.
2524 friend class Instruction;
2525
2526 PHINode *cloneImpl() const;
2527
2528 // allocHungoffUses - this is more complicated than the generic
2529 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2530 // values and pointers to the incoming blocks, all in one allocation.
2531 void allocHungoffUses(unsigned N) {
2532 User::allocHungoffUses(N, /* IsPhi */ true);
2533 }
2534
2535public:
2536 /// Constructors - NumReservedValues is a hint for the number of incoming
2537 /// edges that this phi node will have (use 0 if you really have no idea).
2538 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2539 const Twine &NameStr = "",
2540 InsertPosition InsertBefore = nullptr) {
2541 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2542 }
2543
2544 /// Provide fast operand accessors
2546
2547 // Block iterator interface. This provides access to the list of incoming
2548 // basic blocks, which parallels the list of incoming values.
2549 // Please note that we are not providing non-const iterators for blocks to
2550 // force all updates go through an interface function.
2551
2554
2556 return reinterpret_cast<const_block_iterator>(op_begin() + ReservedSpace);
2557 }
2558
2560 return block_begin() + getNumOperands();
2561 }
2562
2564 return make_range(block_begin(), block_end());
2565 }
2566
2567 op_range incoming_values() { return operands(); }
2568
2569 const_op_range incoming_values() const { return operands(); }
2570
2571 /// Return the number of incoming edges
2572 ///
2573 unsigned getNumIncomingValues() const { return getNumOperands(); }
2574
2575 /// Return incoming value number x
2576 ///
2577 Value *getIncomingValue(unsigned i) const {
2578 return getOperand(i);
2579 }
2580 void setIncomingValue(unsigned i, Value *V) {
2581 assert(V && "PHI node got a null value!");
2582 assert(getType() == V->getType() &&
2583 "All operands to PHI node must be the same type as the PHI node!");
2584 setOperand(i, V);
2585 }
2586
2587 static unsigned getOperandNumForIncomingValue(unsigned i) {
2588 return i;
2589 }
2590
2591 static unsigned getIncomingValueNumForOperand(unsigned i) {
2592 return i;
2593 }
2594
2595 /// Return incoming basic block number @p i.
2596 ///
2597 BasicBlock *getIncomingBlock(unsigned i) const {
2598 return block_begin()[i];
2599 }
2600
2601 /// Return incoming basic block corresponding
2602 /// to an operand of the PHI.
2603 ///
2605 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2606 return getIncomingBlock(unsigned(&U - op_begin()));
2607 }
2608
2609 /// Return incoming basic block corresponding
2610 /// to value use iterator.
2611 ///
2613 return getIncomingBlock(I.getUse());
2614 }
2615
2616 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2617 const_cast<block_iterator>(block_begin())[i] = BB;
2618 }
2619
2620 /// Copies the basic blocks from \p BBRange to the incoming basic block list
2621 /// of this PHINode, starting at \p ToIdx.
2623 uint32_t ToIdx = 0) {
2624 copy(BBRange, const_cast<block_iterator>(block_begin()) + ToIdx);
2625 }
2626
2627 /// Replace every incoming basic block \p Old to basic block \p New.
2629 assert(New && Old && "PHI node got a null basic block!");
2630 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2631 if (getIncomingBlock(Op) == Old)
2632 setIncomingBlock(Op, New);
2633 }
2634
2635 /// Add an incoming value to the end of the PHI list
2636 ///
2638 if (getNumOperands() == ReservedSpace)
2639 growOperands(); // Get more space!
2640 // Initialize some new operands.
2641 setNumHungOffUseOperands(getNumOperands() + 1);
2642 setIncomingValue(getNumOperands() - 1, V);
2643 setIncomingBlock(getNumOperands() - 1, BB);
2644 }
2645
2646 /// Remove an incoming value. This is useful if a
2647 /// predecessor basic block is deleted. The value removed is returned.
2648 ///
2649 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2650 /// is true), the PHI node is destroyed and any uses of it are replaced with
2651 /// dummy values. The only time there should be zero incoming values to a PHI
2652 /// node is when the block is dead, so this strategy is sound.
2653 ///
2654 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2655
2656 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2657 int Idx = getBasicBlockIndex(BB);
2658 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2659 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2660 }
2661
2662 /// Remove all incoming values for which the predicate returns true.
2663 /// The predicate accepts the incoming value index.
2664 void removeIncomingValueIf(function_ref<bool(unsigned)> Predicate,
2665 bool DeletePHIIfEmpty = true);
2666
2667 /// Return the first index of the specified basic
2668 /// block in the value list for this PHI. Returns -1 if no instance.
2669 ///
2670 int getBasicBlockIndex(const BasicBlock *BB) const {
2671 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2672 if (block_begin()[i] == BB)
2673 return i;
2674 return -1;
2675 }
2676
2678 int Idx = getBasicBlockIndex(BB);
2679 assert(Idx >= 0 && "Invalid basic block argument!");
2680 return getIncomingValue(Idx);
2681 }
2682
2683 /// Set every incoming value(s) for block \p BB to \p V.
2685 assert(BB && "PHI node got a null basic block!");
2686 bool Found = false;
2687 for (unsigned Op = 0, NumOps = getNumOperands(); Op != NumOps; ++Op)
2688 if (getIncomingBlock(Op) == BB) {
2689 Found = true;
2690 setIncomingValue(Op, V);
2691 }
2692 (void)Found;
2693 assert(Found && "Invalid basic block argument to set!");
2694 }
2695
2696 /// If the specified PHI node always merges together the
2697 /// same value, return the value, otherwise return null.
2698 Value *hasConstantValue() const;
2699
2700 /// Whether the specified PHI node always merges
2701 /// together the same value, assuming undefs are equal to a unique
2702 /// non-undef value.
2703 bool hasConstantOrUndefValue() const;
2704
2705 /// If the PHI node is complete which means all of its parent's predecessors
2706 /// have incoming value in this PHI, return true, otherwise return false.
2707 bool isComplete() const {
2709 [this](const BasicBlock *Pred) {
2710 return getBasicBlockIndex(Pred) >= 0;
2711 });
2712 }
2713
2714 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2715 static bool classof(const Instruction *I) {
2716 return I->getOpcode() == Instruction::PHI;
2717 }
2718 static bool classof(const Value *V) {
2719 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2720 }
2721
2722private:
2723 void growOperands();
2724};
2725
2726template <>
2728};
2729
2731
2732//===----------------------------------------------------------------------===//
2733// LandingPadInst Class
2734//===----------------------------------------------------------------------===//
2735
2736//===---------------------------------------------------------------------------
2737/// The landingpad instruction holds all of the information
2738/// necessary to generate correct exception handling. The landingpad instruction
2739/// cannot be moved from the top of a landing pad block, which itself is
2740/// accessible only from the 'unwind' edge of an invoke. This uses the
2741/// SubclassData field in Value to store whether or not the landingpad is a
2742/// cleanup.
2743///
2745 using CleanupField = BoolBitfieldElementT<0>;
2746
2747 /// The number of operands actually allocated. NumOperands is
2748 /// the number actually in use.
2749 unsigned ReservedSpace;
2750
2751 LandingPadInst(const LandingPadInst &LP);
2752
2753public:
2755
2756private:
2757 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2758 const Twine &NameStr, InsertPosition InsertBefore);
2759
2760 // Allocate space for exactly zero operands.
2761 void *operator new(size_t S) { return User::operator new(S); }
2762
2763 void growOperands(unsigned Size);
2764 void init(unsigned NumReservedValues, const Twine &NameStr);
2765
2766protected:
2767 // Note: Instruction needs to be a friend here to call cloneImpl.
2768 friend class Instruction;
2769
2770 LandingPadInst *cloneImpl() const;
2771
2772public:
2773 void operator delete(void *Ptr) { User::operator delete(Ptr); }
2774
2775 /// Constructors - NumReservedClauses is a hint for the number of incoming
2776 /// clauses that this landingpad will have (use 0 if you really have no idea).
2777 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2778 const Twine &NameStr = "",
2779 InsertPosition InsertBefore = nullptr);
2780
2781 /// Provide fast operand accessors
2783
2784 /// Return 'true' if this landingpad instruction is a
2785 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2786 /// doesn't catch the exception.
2787 bool isCleanup() const { return getSubclassData<CleanupField>(); }
2788
2789 /// Indicate that this landingpad instruction is a cleanup.
2790 void setCleanup(bool V) { setSubclassData<CleanupField>(V); }
2791
2792 /// Add a catch or filter clause to the landing pad.
2793 void addClause(Constant *ClauseVal);
2794
2795 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2796 /// determine what type of clause this is.
2797 Constant *getClause(unsigned Idx) const {
2798 return cast<Constant>(getOperandList()[Idx]);
2799 }
2800
2801 /// Return 'true' if the clause and index Idx is a catch clause.
2802 bool isCatch(unsigned Idx) const {
2803 return !isa<ArrayType>(getOperandList()[Idx]->getType());
2804 }
2805
2806 /// Return 'true' if the clause and index Idx is a filter clause.
2807 bool isFilter(unsigned Idx) const {
2808 return isa<ArrayType>(getOperandList()[Idx]->getType());
2809 }
2810
2811 /// Get the number of clauses for this landing pad.
2812 unsigned getNumClauses() const { return getNumOperands(); }
2813
2814 /// Grow the size of the operand list to accommodate the new
2815 /// number of clauses.
2816 void reserveClauses(unsigned Size) { growOperands(Size); }
2817
2818 // Methods for support type inquiry through isa, cast, and dyn_cast:
2819 static bool classof(const Instruction *I) {
2820 return I->getOpcode() == Instruction::LandingPad;
2821 }
2822 static bool classof(const Value *V) {
2823 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2824 }
2825};
2826
2827template <>
2829};
2830
2832
2833//===----------------------------------------------------------------------===//
2834// ReturnInst Class
2835//===----------------------------------------------------------------------===//
2836
2837//===---------------------------------------------------------------------------
2838/// Return a value (possibly void), from a function. Execution
2839/// does not continue in this function any longer.
2840///
2841class ReturnInst : public Instruction {
2842 ReturnInst(const ReturnInst &RI);
2843
2844private:
2845 // ReturnInst constructors:
2846 // ReturnInst() - 'ret void' instruction
2847 // ReturnInst( null) - 'ret void' instruction
2848 // ReturnInst(Value* X) - 'ret X' instruction
2849 // ReturnInst(null, Iterator It) - 'ret void' instruction, insert before I
2850 // ReturnInst(Value* X, Iterator It) - 'ret X' instruction, insert before I
2851 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2852 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2853 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2854 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2855 //
2856 // NOTE: If the Value* passed is of type void then the constructor behaves as
2857 // if it was passed NULL.
2858 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2859 InsertPosition InsertBefore = nullptr);
2860
2861protected:
2862 // Note: Instruction needs to be a friend here to call cloneImpl.
2863 friend class Instruction;
2864
2865 ReturnInst *cloneImpl() const;
2866
2867public:
2868 static ReturnInst *Create(LLVMContext &C, Value *retVal = nullptr,
2869 InsertPosition InsertBefore = nullptr) {
2870 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2871 }
2872
2873 static ReturnInst *Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2874 return new (0) ReturnInst(C, nullptr, InsertAtEnd);
2875 }
2876
2877 /// Provide fast operand accessors
2879
2880 /// Convenience accessor. Returns null if there is no return value.
2882 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2883 }
2884
2885 unsigned getNumSuccessors() const { return 0; }
2886
2887 // Methods for support type inquiry through isa, cast, and dyn_cast:
2888 static bool classof(const Instruction *I) {
2889 return (I->getOpcode() == Instruction::Ret);
2890 }
2891 static bool classof(const Value *V) {
2892 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2893 }
2894
2895private:
2896 BasicBlock *getSuccessor(unsigned idx) const {
2897 llvm_unreachable("ReturnInst has no successors!");
2898 }
2899
2900 void setSuccessor(unsigned idx, BasicBlock *B) {
2901 llvm_unreachable("ReturnInst has no successors!");
2902 }
2903};
2904
2905template <>
2906struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2907};
2908
2910
2911//===----------------------------------------------------------------------===//
2912// BranchInst Class
2913//===----------------------------------------------------------------------===//
2914
2915//===---------------------------------------------------------------------------
2916/// Conditional or Unconditional Branch instruction.
2917///
2918class BranchInst : public Instruction {
2919 /// Ops list - Branches are strange. The operands are ordered:
2920 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2921 /// they don't have to check for cond/uncond branchness. These are mostly
2922 /// accessed relative from op_end().
2923 BranchInst(const BranchInst &BI);
2924 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2925 // BranchInst(BB *B) - 'br B'
2926 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2927 // BranchInst(BB* B, Iter It) - 'br B' insert before I
2928 // BranchInst(BB* T, BB *F, Value *C, Iter It) - 'br C, T, F', insert before I
2929 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2930 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2931 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2932 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2933 explicit BranchInst(BasicBlock *IfTrue,
2934 InsertPosition InsertBefore = nullptr);
2935 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2936 InsertPosition InsertBefore = nullptr);
2937
2938 void AssertOK();
2939
2940protected:
2941 // Note: Instruction needs to be a friend here to call cloneImpl.
2942 friend class Instruction;
2943
2944 BranchInst *cloneImpl() const;
2945
2946public:
2947 /// Iterator type that casts an operand to a basic block.
2948 ///
2949 /// This only makes sense because the successors are stored as adjacent
2950 /// operands for branch instructions.
2952 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
2953 std::random_access_iterator_tag, BasicBlock *,
2954 ptrdiff_t, BasicBlock *, BasicBlock *> {
2956
2957 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
2958 BasicBlock *operator->() const { return operator*(); }
2959 };
2960
2961 /// The const version of `succ_op_iterator`.
2963 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
2964 std::random_access_iterator_tag,
2965 const BasicBlock *, ptrdiff_t, const BasicBlock *,
2966 const BasicBlock *> {
2969
2970 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
2971 const BasicBlock *operator->() const { return operator*(); }
2972 };
2973
2975 InsertPosition InsertBefore = nullptr) {
2976 return new(1) BranchInst(IfTrue, InsertBefore);
2977 }
2978
2979 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2980 Value *Cond,
2981 InsertPosition InsertBefore = nullptr) {
2982 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2983 }
2984
2985 /// Transparently provide more efficient getOperand methods.
2987
2988 bool isUnconditional() const { return getNumOperands() == 1; }
2989 bool isConditional() const { return getNumOperands() == 3; }
2990
2992 assert(isConditional() && "Cannot get condition of an uncond branch!");
2993 return Op<-3>();
2994 }
2995
2997 assert(isConditional() && "Cannot set condition of unconditional branch!");
2998 Op<-3>() = V;
2999 }
3000
3001 unsigned getNumSuccessors() const { return 1+isConditional(); }
3002
3003 BasicBlock *getSuccessor(unsigned i) const {
3004 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
3005 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
3006 }
3007
3008 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3009 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
3010 *(&Op<-1>() - idx) = NewSucc;
3011 }
3012
3013 /// Swap the successors of this branch instruction.
3014 ///
3015 /// Swaps the successors of the branch instruction. This also swaps any
3016 /// branch weight metadata associated with the instruction so that it
3017 /// continues to map correctly to each operand.
3018 void swapSuccessors();
3019
3021 return make_range(
3022 succ_op_iterator(std::next(value_op_begin(), isConditional() ? 1 : 0)),
3023 succ_op_iterator(value_op_end()));
3024 }
3025
3028 std::next(value_op_begin(), isConditional() ? 1 : 0)),
3029 const_succ_op_iterator(value_op_end()));
3030 }
3031
3032 // Methods for support type inquiry through isa, cast, and dyn_cast:
3033 static bool classof(const Instruction *I) {
3034 return (I->getOpcode() == Instruction::Br);
3035 }
3036 static bool classof(const Value *V) {
3037 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3038 }
3039};
3040
3041template <>
3042struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
3043};
3044
3046
3047//===----------------------------------------------------------------------===//
3048// SwitchInst Class
3049//===----------------------------------------------------------------------===//
3050
3051//===---------------------------------------------------------------------------
3052/// Multiway switch
3053///
3054class SwitchInst : public Instruction {
3055 unsigned ReservedSpace;
3056
3057 // Operand[0] = Value to switch on
3058 // Operand[1] = Default basic block destination
3059 // Operand[2n ] = Value to match
3060 // Operand[2n+1] = BasicBlock to go to on match
3061 SwitchInst(const SwitchInst &SI);
3062
3063 /// Create a new switch instruction, specifying a value to switch on and a
3064 /// default destination. The number of additional cases can be specified here
3065 /// to make memory allocation more efficient. This constructor can also
3066 /// auto-insert before another instruction.
3067 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3068 InsertPosition InsertBefore);
3069
3070 // allocate space for exactly zero operands
3071 void *operator new(size_t S) { return User::operator new(S); }
3072
3073 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
3074 void growOperands();
3075
3076protected:
3077 // Note: Instruction needs to be a friend here to call cloneImpl.
3078 friend class Instruction;
3079
3080 SwitchInst *cloneImpl() const;
3081
3082public:
3083 void operator delete(void *Ptr) { User::operator delete(Ptr); }
3084
3085 // -2
3086 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3087
3088 template <typename CaseHandleT> class CaseIteratorImpl;
3089
3090 /// A handle to a particular switch case. It exposes a convenient interface
3091 /// to both the case value and the successor block.
3092 ///
3093 /// We define this as a template and instantiate it to form both a const and
3094 /// non-const handle.
3095 template <typename SwitchInstT, typename ConstantIntT, typename BasicBlockT>
3097 // Directly befriend both const and non-const iterators.
3098 friend class SwitchInst::CaseIteratorImpl<
3099 CaseHandleImpl<SwitchInstT, ConstantIntT, BasicBlockT>>;
3100
3101 protected:
3102 // Expose the switch type we're parameterized with to the iterator.
3103 using SwitchInstType = SwitchInstT;
3104
3105 SwitchInstT *SI;
3107
3108 CaseHandleImpl() = default;
3110
3111 public:
3112 /// Resolves case value for current case.
3113 ConstantIntT *getCaseValue() const {
3114 assert((unsigned)Index < SI->getNumCases() &&
3115 "Index out the number of cases.");
3116 return reinterpret_cast<ConstantIntT *>(SI->getOperand(2 + Index * 2));
3117 }
3118
3119 /// Resolves successor for current case.
3120 BasicBlockT *getCaseSuccessor() const {
3121 assert(((unsigned)Index < SI->getNumCases() ||
3122 (unsigned)Index == DefaultPseudoIndex) &&
3123 "Index out the number of cases.");
3124 return SI->getSuccessor(getSuccessorIndex());
3125 }
3126
3127 /// Returns number of current case.
3128 unsigned getCaseIndex() const { return Index; }
3129
3130 /// Returns successor index for current case successor.
3131 unsigned getSuccessorIndex() const {
3132 assert(((unsigned)Index == DefaultPseudoIndex ||
3133 (unsigned)Index < SI->getNumCases()) &&
3134 "Index out the number of cases.");
3135 return (unsigned)Index != DefaultPseudoIndex ? Index + 1 : 0;
3136 }
3137
3138 bool operator==(const CaseHandleImpl &RHS) const {
3139 assert(SI == RHS.SI && "Incompatible operators.");
3140 return Index == RHS.Index;
3141 }
3142 };
3143
3146
3148 : public CaseHandleImpl<SwitchInst, ConstantInt, BasicBlock> {
3150
3151 public:
3153
3154 /// Sets the new value for current case.
3155 void setValue(ConstantInt *V) const {
3156 assert((unsigned)Index < SI->getNumCases() &&
3157 "Index out the number of cases.");
3158 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3159 }
3160
3161 /// Sets the new successor for current case.
3162 void setSuccessor(BasicBlock *S) const {
3163 SI->setSuccessor(getSuccessorIndex(), S);
3164 }
3165 };
3166
3167 template <typename CaseHandleT>
3169 : public iterator_facade_base<CaseIteratorImpl<CaseHandleT>,
3170 std::random_access_iterator_tag,
3171 const CaseHandleT> {
3172 using SwitchInstT = typename CaseHandleT::SwitchInstType;
3173
3174 CaseHandleT Case;
3175
3176 public:
3177 /// Default constructed iterator is in an invalid state until assigned to
3178 /// a case for a particular switch.
3179 CaseIteratorImpl() = default;
3180
3181 /// Initializes case iterator for given SwitchInst and for given
3182 /// case number.
3183 CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum) : Case(SI, CaseNum) {}
3184
3185 /// Initializes case iterator for given SwitchInst and for given
3186 /// successor index.
3188 unsigned SuccessorIndex) {
3189 assert(SuccessorIndex < SI->getNumSuccessors() &&
3190 "Successor index # out of range!");
3191 return SuccessorIndex != 0 ? CaseIteratorImpl(SI, SuccessorIndex - 1)
3192 : CaseIteratorImpl(SI, DefaultPseudoIndex);
3193 }
3194
3195 /// Support converting to the const variant. This will be a no-op for const
3196 /// variant.
3198 return CaseIteratorImpl<ConstCaseHandle>(Case.SI, Case.Index);
3199 }
3200
3202 // Check index correctness after addition.
3203 // Note: Index == getNumCases() means end().
3204 assert(Case.Index + N >= 0 &&
3205 (unsigned)(Case.Index + N) <= Case.SI->getNumCases() &&
3206 "Case.Index out the number of cases.");
3207 Case.Index += N;
3208 return *this;
3209 }
3211 // Check index correctness after subtraction.
3212 // Note: Case.Index == getNumCases() means end().
3213 assert(Case.Index - N >= 0 &&
3214 (unsigned)(Case.Index - N) <= Case.SI->getNumCases() &&
3215 "Case.Index out the number of cases.");
3216 Case.Index -= N;
3217 return *this;
3218 }
3220 assert(Case.SI == RHS.Case.SI && "Incompatible operators.");
3221 return Case.Index - RHS.Case.Index;
3222 }
3223 bool operator==(const CaseIteratorImpl &RHS) const {
3224 return Case == RHS.Case;
3225 }
3226 bool operator<(const CaseIteratorImpl &RHS) const {
3227 assert(Case.SI == RHS.Case.SI && "Incompatible operators.");
3228 return Case.Index < RHS.Case.Index;
3229 }
3230 const CaseHandleT &operator*() const { return Case; }
3231 };
3232
3235
3237 unsigned NumCases,
3238 InsertPosition InsertBefore = nullptr) {
3239 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3240 }
3241
3242 /// Provide fast operand accessors
3244
3245 // Accessor Methods for Switch stmt
3246 Value *getCondition() const { return getOperand(0); }
3247 void setCondition(Value *V) { setOperand(0, V); }
3248
3250 return cast<BasicBlock>(getOperand(1));
3251 }
3252
3253 /// Returns true if the default branch must result in immediate undefined
3254 /// behavior, false otherwise.
3256 return isa<UnreachableInst>(getDefaultDest()->getFirstNonPHIOrDbg());
3257 }
3258
3259 void setDefaultDest(BasicBlock *DefaultCase) {
3260 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3261 }
3262
3263 /// Return the number of 'cases' in this switch instruction, excluding the
3264 /// default case.
3265 unsigned getNumCases() const {
3266 return getNumOperands()/2 - 1;
3267 }
3268
3269 /// Returns a read/write iterator that points to the first case in the
3270 /// SwitchInst.
3272 return CaseIt(this, 0);
3273 }
3274
3275 /// Returns a read-only iterator that points to the first case in the
3276 /// SwitchInst.
3278 return ConstCaseIt(this, 0);
3279 }
3280
3281 /// Returns a read/write iterator that points one past the last in the
3282 /// SwitchInst.
3284 return CaseIt(this, getNumCases());
3285 }
3286
3287 /// Returns a read-only iterator that points one past the last in the
3288 /// SwitchInst.
3290 return ConstCaseIt(this, getNumCases());
3291 }
3292
3293 /// Iteration adapter for range-for loops.
3295 return make_range(case_begin(), case_end());
3296 }
3297
3298 /// Constant iteration adapter for range-for loops.
3300 return make_range(case_begin(), case_end());
3301 }
3302
3303 /// Returns an iterator that points to the default case.
3304 /// Note: this iterator allows to resolve successor only. Attempt
3305 /// to resolve case value causes an assertion.
3306 /// Also note, that increment and decrement also causes an assertion and
3307 /// makes iterator invalid.
3309 return CaseIt(this, DefaultPseudoIndex);
3310 }
3312 return ConstCaseIt(this, DefaultPseudoIndex);
3313 }
3314
3315 /// Search all of the case values for the specified constant. If it is
3316 /// explicitly handled, return the case iterator of it, otherwise return
3317 /// default case iterator to indicate that it is handled by the default
3318 /// handler.
3320 return CaseIt(
3321 this,
3322 const_cast<const SwitchInst *>(this)->findCaseValue(C)->getCaseIndex());
3323 }
3325 ConstCaseIt I = llvm::find_if(cases(), [C](const ConstCaseHandle &Case) {
3326 return Case.getCaseValue() == C;
3327 });
3328 if (I != case_end())
3329 return I;
3330
3331 return case_default();
3332 }
3333
3334 /// Finds the unique case value for a given successor. Returns null if the
3335 /// successor is not found, not unique, or is the default case.
3337 if (BB == getDefaultDest())
3338 return nullptr;
3339
3340 ConstantInt *CI = nullptr;
3341 for (auto Case : cases()) {
3342 if (Case.getCaseSuccessor() != BB)
3343 continue;
3344
3345 if (CI)
3346 return nullptr; // Multiple cases lead to BB.
3347
3348 CI = Case.getCaseValue();
3349 }
3350
3351 return CI;
3352 }
3353
3354 /// Add an entry to the switch instruction.
3355 /// Note:
3356 /// This action invalidates case_end(). Old case_end() iterator will
3357 /// point to the added case.
3358 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3359
3360 /// This method removes the specified case and its successor from the switch
3361 /// instruction. Note that this operation may reorder the remaining cases at
3362 /// index idx and above.
3363 /// Note:
3364 /// This action invalidates iterators for all cases following the one removed,
3365 /// including the case_end() iterator. It returns an iterator for the next
3366 /// case.
3367 CaseIt removeCase(CaseIt I);
3368
3369 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3370 BasicBlock *getSuccessor(unsigned idx) const {
3371 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3372 return cast<BasicBlock>(getOperand(idx*2+1));
3373 }
3374 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3375 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3376 setOperand(idx * 2 + 1, NewSucc);
3377 }
3378
3379 // Methods for support type inquiry through isa, cast, and dyn_cast:
3380 static bool classof(const Instruction *I) {
3381 return I->getOpcode() == Instruction::Switch;
3382 }
3383 static bool classof(const Value *V) {
3384 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3385 }
3386};
3387
3388/// A wrapper class to simplify modification of SwitchInst cases along with
3389/// their prof branch_weights metadata.
3391 SwitchInst &SI;
3392 std::optional<SmallVector<uint32_t, 8>> Weights;
3393 bool Changed = false;
3394
3395protected:
3397
3398 void init();
3399
3400public:
3401 using CaseWeightOpt = std::optional<uint32_t>;
3402 SwitchInst *operator->() { return &SI; }
3403 SwitchInst &operator*() { return SI; }
3404 operator SwitchInst *() { return &SI; }
3405
3407
3409 if (Changed)
3410 SI.setMetadata(LLVMContext::MD_prof, buildProfBranchWeightsMD());
3411 }
3412
3413 /// Delegate the call to the underlying SwitchInst::removeCase() and remove
3414 /// correspondent branch weight.
3416
3417 /// Delegate the call to the underlying SwitchInst::addCase() and set the
3418 /// specified branch weight for the added case.
3419 void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W);
3420
3421 /// Delegate the call to the underlying SwitchInst::eraseFromParent() and mark
3422 /// this object to not touch the underlying SwitchInst in destructor.
3424
3425 void setSuccessorWeight(unsigned idx, CaseWeightOpt W);
3426 CaseWeightOpt getSuccessorWeight(unsigned idx);
3427
3428 static CaseWeightOpt getSuccessorWeight(const SwitchInst &SI, unsigned idx);
3429};
3430
3431template <>
3433};
3434
3436
3437//===----------------------------------------------------------------------===//
3438// IndirectBrInst Class
3439//===----------------------------------------------------------------------===//
3440
3441//===---------------------------------------------------------------------------
3442/// Indirect Branch Instruction.
3443///
3445 unsigned ReservedSpace;
3446
3447 // Operand[0] = Address to jump to
3448 // Operand[n+1] = n-th destination
3449 IndirectBrInst(const IndirectBrInst &IBI);
3450
3451 /// Create a new indirectbr instruction, specifying an
3452 /// Address to jump to. The number of expected destinations can be specified
3453 /// here to make memory allocation more efficient. This constructor can also
3454 /// autoinsert before another instruction.
3455 IndirectBrInst(Value *Address, unsigned NumDests,
3456 InsertPosition InsertBefore);
3457
3458 // allocate space for exactly zero operands
3459 void *operator new(size_t S) { return User::operator new(S); }
3460
3461 void init(Value *Address, unsigned NumDests);
3462 void growOperands();
3463
3464protected:
3465 // Note: Instruction needs to be a friend here to call cloneImpl.
3466 friend class Instruction;
3467
3468 IndirectBrInst *cloneImpl() const;
3469
3470public:
3471 void operator delete(void *Ptr) { User::operator delete(Ptr); }
3472
3473 /// Iterator type that casts an operand to a basic block.
3474 ///
3475 /// This only makes sense because the successors are stored as adjacent
3476 /// operands for indirectbr instructions.
3478 : iterator_adaptor_base<succ_op_iterator, value_op_iterator,
3479 std::random_access_iterator_tag, BasicBlock *,
3480 ptrdiff_t, BasicBlock *, BasicBlock *> {
3482
3483 BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3484 BasicBlock *operator->() const { return operator*(); }
3485 };
3486
3487 /// The const version of `succ_op_iterator`.
3489 : iterator_adaptor_base<const_succ_op_iterator, const_value_op_iterator,
3490 std::random_access_iterator_tag,
3491 const BasicBlock *, ptrdiff_t, const BasicBlock *,
3492 const BasicBlock *> {
3495
3496 const BasicBlock *operator*() const { return cast<BasicBlock>(*I); }
3497 const BasicBlock *operator->() const { return operator*(); }
3498 };
3499
3500 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3501 InsertPosition InsertBefore = nullptr) {
3502 return new IndirectBrInst(Address, NumDests, InsertBefore);
3503 }
3504
3505 /// Provide fast operand accessors.
3507
3508 // Accessor Methods for IndirectBrInst instruction.
3509 Value *getAddress() { return getOperand(0); }
3510 const Value *getAddress() const { return getOperand(0); }
3511 void setAddress(Value *V) { setOperand(0, V); }
3512
3513 /// return the number of possible destinations in this
3514 /// indirectbr instruction.
3515 unsigned getNumDestinations() const { return getNumOperands()-1; }
3516
3517 /// Return the specified destination.
3518 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3519 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3520
3521 /// Add a destination.
3522 ///
3523 void addDestination(BasicBlock *Dest);
3524
3525 /// This method removes the specified successor from the
3526 /// indirectbr instruction.
3527 void removeDestination(unsigned i);
3528
3529 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3530 BasicBlock *getSuccessor(unsigned i) const {
3531 return cast<BasicBlock>(getOperand(i+1));
3532 }
3533 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3534 setOperand(i + 1, NewSucc);
3535 }
3536
3538 return make_range(succ_op_iterator(std::next(value_op_begin())),
3539 succ_op_iterator(value_op_end()));
3540 }
3541
3543 return make_range(const_succ_op_iterator(std::next(value_op_begin())),
3544 const_succ_op_iterator(value_op_end()));
3545 }
3546
3547 // Methods for support type inquiry through isa, cast, and dyn_cast:
3548 static bool classof(const Instruction *I) {
3549 return I->getOpcode() == Instruction::IndirectBr;
3550 }
3551 static bool classof(const Value *V) {
3552 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3553 }
3554};
3555
3556template <>
3558};
3559
3561
3562//===----------------------------------------------------------------------===//
3563// InvokeInst Class
3564//===----------------------------------------------------------------------===//
3565
3566/// Invoke instruction. The SubclassData field is used to hold the
3567/// calling convention of the call.
3568///
3569class InvokeInst : public CallBase {
3570 /// The number of operands for this call beyond the called function,
3571 /// arguments, and operand bundles.
3572 static constexpr int NumExtraOperands = 2;
3573
3574 /// The index from the end of the operand array to the normal destination.
3575 static constexpr int NormalDestOpEndIdx = -3;
3576
3577 /// The index from the end of the operand array to the unwind destination.
3578 static constexpr int UnwindDestOpEndIdx = -2;
3579
3580 InvokeInst(const InvokeInst &BI);
3581
3582 /// Construct an InvokeInst given a range of arguments.
3583 ///
3584 /// Construct an InvokeInst from a range of arguments
3585 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3586 BasicBlock *IfException, ArrayRef<Value *> Args,
3587 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3588 const Twine &NameStr, InsertPosition InsertBefore);
3589
3590 void init(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3591 BasicBlock *IfException, ArrayRef<Value *> Args,
3592 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3593
3594 /// Compute the number of operands to allocate.
3595 static int ComputeNumOperands(int NumArgs, int NumBundleInputs = 0) {
3596 // We need one operand for the called function, plus our extra operands and
3597 // the input operand counts provided.
3598 return 1 + NumExtraOperands + NumArgs + NumBundleInputs;
3599 }
3600
3601protected:
3602 // Note: Instruction needs to be a friend here to call cloneImpl.
3603 friend class Instruction;
3604
3605 InvokeInst *cloneImpl() const;
3606
3607public:
3608 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3609 BasicBlock *IfException, ArrayRef<Value *> Args,
3610 const Twine &NameStr,
3611 InsertPosition InsertBefore = nullptr) {
3612 int NumOperands = ComputeNumOperands(Args.size());
3613 return new (NumOperands)
3614 InvokeInst(Ty, Func, IfNormal, IfException, Args, std::nullopt,
3615 NumOperands, NameStr, InsertBefore);
3616 }
3617
3618 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3619 BasicBlock *IfException, ArrayRef<Value *> Args,
3620 ArrayRef<OperandBundleDef> Bundles = std::nullopt,
3621 const Twine &NameStr = "",
3622 InsertPosition InsertBefore = nullptr) {
3623 int NumOperands =
3624 ComputeNumOperands(Args.size(), CountBundleInputs(Bundles));
3625 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3626
3627 return new (NumOperands, DescriptorBytes)
3628 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, NumOperands,
3629 NameStr, InsertBefore);
3630 }
3631
3633 BasicBlock *IfException, ArrayRef<Value *> Args,
3634 const Twine &NameStr,
3635 InsertPosition InsertBefore = nullptr) {
3636 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3637 IfException, Args, std::nullopt, NameStr, InsertBefore);
3638 }
3639
3641 BasicBlock *IfException, ArrayRef<Value *> Args,
3642 ArrayRef<OperandBundleDef> Bundles = std::nullopt,
3643 const Twine &NameStr = "",
3644 InsertPosition InsertBefore = nullptr) {
3645 return Create(Func.getFunctionType(), Func.getCallee(), IfNormal,
3646 IfException, Args, Bundles, NameStr, InsertBefore);
3647 }
3648
3649 /// Create a clone of \p II with a different set of operand bundles and
3650 /// insert it before \p InsertBefore.
3651 ///
3652 /// The returned invoke instruction is identical to \p II in every way except
3653 /// that the operand bundles for the new instruction are set to the operand
3654 /// bundles in \p Bundles.
3655 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
3656 InsertPosition InsertPt = nullptr);
3657
3658 // get*Dest - Return the destination basic blocks...
3660 return cast<BasicBlock>(Op<NormalDestOpEndIdx>());
3661 }
3663 return cast<BasicBlock>(Op<UnwindDestOpEndIdx>());
3664 }
3666 Op<NormalDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3667 }
3669 Op<UnwindDestOpEndIdx>() = reinterpret_cast<Value *>(B);
3670 }
3671
3672 /// Get the landingpad instruction from the landing pad
3673 /// block (the unwind destination).
3674 LandingPadInst *getLandingPadInst() const;
3675
3676 BasicBlock *getSuccessor(unsigned i) const {
3677 assert(i < 2 && "Successor # out of range for invoke!");
3678 return i == 0 ? getNormalDest() : getUnwindDest();
3679 }
3680
3681 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3682 assert(i < 2 && "Successor # out of range for invoke!");
3683 if (i == 0)
3684 setNormalDest(NewSucc);
3685 else
3686 setUnwindDest(NewSucc);
3687 }
3688
3689 unsigned getNumSuccessors() const { return 2; }
3690
3691 /// Updates profile metadata by scaling it by \p S / \p T.
3692 void updateProfWeight(uint64_t S, uint64_t T);
3693
3694 // Methods for support type inquiry through isa, cast, and dyn_cast:
3695 static bool classof(const Instruction *I) {
3696 return (I->getOpcode() == Instruction::Invoke);
3697 }
3698 static bool classof(const Value *V) {
3699 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3700 }
3701
3702private:
3703 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3704 // method so that subclasses cannot accidentally use it.
3705 template <typename Bitfield>
3706 void setSubclassData(typename Bitfield::Type Value) {
3707 Instruction::setSubclassData<Bitfield>(Value);
3708 }
3709};
3710
3711InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3712 BasicBlock *IfException, ArrayRef<Value *> Args,
3713 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3714 const Twine &NameStr, InsertPosition InsertBefore)
3715 : CallBase(Ty->getReturnType(), Instruction::Invoke,
3716 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3717 InsertBefore) {
3718 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3719}
3720
3721//===----------------------------------------------------------------------===//
3722// CallBrInst Class
3723//===----------------------------------------------------------------------===//
3724
3725/// CallBr instruction, tracking function calls that may not return control but
3726/// instead transfer it to a third location. The SubclassData field is used to
3727/// hold the calling convention of the call.
3728///
3729class CallBrInst : public CallBase {
3730
3731 unsigned NumIndirectDests;
3732
3733 CallBrInst(const CallBrInst &BI);
3734
3735 /// Construct a CallBrInst given a range of arguments.
3736 ///
3737 /// Construct a CallBrInst from a range of arguments
3738 inline CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
3739 ArrayRef<BasicBlock *> IndirectDests,
3741 int NumOperands, const Twine &NameStr,
3742 InsertPosition InsertBefore);
3743
3744 void init(FunctionType *FTy, Value *Func, BasicBlock *DefaultDest,
3745 ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args,
3746 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3747
3748 /// Compute the number of operands to allocate.
3749 static int ComputeNumOperands(int NumArgs, int NumIndirectDests,
3750 int NumBundleInputs = 0) {
3751 // We need one operand for the called function, plus our extra operands and
3752 // the input operand counts provided.
3753 return 2 + NumIndirectDests + NumArgs + NumBundleInputs;
3754 }
3755
3756protected:
3757 // Note: Instruction needs to be a friend here to call cloneImpl.
3758 friend class Instruction;
3759
3760 CallBrInst *cloneImpl() const;
3761
3762public:
3764 BasicBlock *DefaultDest,
3765 ArrayRef<BasicBlock *> IndirectDests,
3766 ArrayRef<Value *> Args, const Twine &NameStr,
3767 InsertPosition InsertBefore = nullptr) {
3768 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size());
3769 return new (NumOperands)
3770 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, std::nullopt,
3771 NumOperands, NameStr, InsertBefore);
3772 }
3773
3774 static CallBrInst *
3775 Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
3776 ArrayRef<BasicBlock *> IndirectDests, ArrayRef<Value *> Args,
3777 ArrayRef<OperandBundleDef> Bundles = std::nullopt,
3778 const Twine &NameStr = "", InsertPosition InsertBefore = nullptr) {
3779 int NumOperands = ComputeNumOperands(Args.size(), IndirectDests.size(),
3780 CountBundleInputs(Bundles));
3781 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3782
3783 return new (NumOperands, DescriptorBytes)
3784 CallBrInst(Ty, Func, DefaultDest, IndirectDests, Args, Bundles,
3785 NumOperands, NameStr, InsertBefore);
3786 }
3787
3788 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
3789 ArrayRef<BasicBlock *> IndirectDests,
3790 ArrayRef<Value *> Args, const Twine &NameStr,
3791 InsertPosition InsertBefore = nullptr) {
3792 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
3793 IndirectDests, Args, NameStr, InsertBefore);
3794 }
3795
3796 static CallBrInst *Create(FunctionCallee Func, BasicBlock *DefaultDest,
3797 ArrayRef<BasicBlock *> IndirectDests,
3798 ArrayRef<Value *> Args,
3799 ArrayRef<OperandBundleDef> Bundles = std::nullopt,
3800 const Twine &NameStr = "",
3801 InsertPosition InsertBefore = nullptr) {
3802 return Create(Func.getFunctionType(), Func.getCallee(), DefaultDest,
3803 IndirectDests, Args, Bundles, NameStr, InsertBefore);
3804 }
3805
3806 /// Create a clone of \p CBI with a different set of operand bundles and
3807 /// insert it before \p InsertBefore.
3808 ///
3809 /// The returned callbr instruction is identical to \p CBI in every way
3810 /// except that the operand bundles for the new instruction are set to the
3811 /// operand bundles in \p Bundles.
3813 InsertPosition InsertBefore = nullptr);
3814
3815 /// Return the number of callbr indirect dest labels.
3816 ///
3817 unsigned getNumIndirectDests() const { return NumIndirectDests; }
3818
3819 /// getIndirectDestLabel - Return the i-th indirect dest label.
3820 ///
3821 Value *getIndirectDestLabel(unsigned i) const {
3822 assert(i < getNumIndirectDests() && "Out of bounds!");
3823 return getOperand(i + arg_size() + getNumTotalBundleOperands() + 1);
3824 }
3825
3826 Value *getIndirectDestLabelUse(unsigned i) const {
3827 assert(i < getNumIndirectDests() && "Out of bounds!");
3828 return getOperandUse(i + arg_size() + getNumTotalBundleOperands() + 1);
3829 }
3830
3831 // Return the destination basic blocks...
3833 return cast<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() - 1));
3834 }
3835 BasicBlock *getIndirectDest(unsigned i) const {
3836 return cast_or_null<BasicBlock>(*(&Op<-1>() - getNumIndirectDests() + i));
3837 }
3839 SmallVector<BasicBlock *, 16> IndirectDests;
3840 for (unsigned i = 0, e = getNumIndirectDests(); i < e; ++i)
3841 IndirectDests.push_back(getIndirectDest(i));
3842 return IndirectDests;
3843 }
3845 *(&Op<-1>() - getNumIndirectDests() - 1) = reinterpret_cast<Value *>(B);
3846 }
3847 void setIndirectDest(unsigned i, BasicBlock *B) {
3848 *(&Op<-1>() - getNumIndirectDests() + i) = reinterpret_cast<Value *>(B);
3849 }
3850
3851 BasicBlock *getSuccessor(unsigned i) const {
3852 assert(i < getNumSuccessors() + 1 &&
3853 "Successor # out of range for callbr!");
3854 return i == 0 ? getDefaultDest() : getIndirectDest(i - 1);
3855 }
3856
3857 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3858 assert(i < getNumIndirectDests() + 1 &&
3859 "Successor # out of range for callbr!");
3860 return i == 0 ? setDefaultDest(NewSucc) : setIndirectDest(i - 1, NewSucc);
3861 }
3862
3863 unsigned getNumSuccessors() const { return getNumIndirectDests() + 1; }
3864
3865 // Methods for support type inquiry through isa, cast, and dyn_cast:
3866 static bool classof(const Instruction *I) {
3867 return (I->getOpcode() == Instruction::CallBr);
3868 }
3869 static bool classof(const Value *V) {
3870 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3871 }
3872
3873private:
3874 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3875 // method so that subclasses cannot accidentally use it.
3876 template <typename Bitfield>
3877 void setSubclassData(typename Bitfield::Type Value) {
3878 Instruction::setSubclassData<Bitfield>(Value);
3879 }
3880};
3881
3882CallBrInst::CallBrInst(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest,
3883 ArrayRef<BasicBlock *> IndirectDests,
3884 ArrayRef<Value *> Args,
3885 ArrayRef<OperandBundleDef> Bundles, int NumOperands,
3886 const Twine &NameStr, InsertPosition InsertBefore)
3887 : CallBase(Ty->getReturnType(), Instruction::CallBr,
3888 OperandTraits<CallBase>::op_end(this) - NumOperands, NumOperands,
3889 InsertBefore) {
3890 init(Ty, Func, DefaultDest, IndirectDests, Args, Bundles, NameStr);
3891}
3892
3893//===----------------------------------------------------------------------===//
3894// ResumeInst Class
3895//===----------------------------------------------------------------------===//
3896
3897//===---------------------------------------------------------------------------
3898/// Resume the propagation of an exception.
3899///
3900class ResumeInst : public Instruction {
3901 ResumeInst(const ResumeInst &RI);
3902
3903 explicit ResumeInst(Value *Exn, InsertPosition InsertBefore = nullptr);
3904
3905protected:
3906 // Note: Instruction needs to be a friend here to call cloneImpl.
3907 friend class Instruction;
3908
3909 ResumeInst *cloneImpl() const;
3910
3911public:
3912 static ResumeInst *Create(Value *Exn, InsertPosition InsertBefore = nullptr) {
3913 return new(1) ResumeInst(Exn, InsertBefore);
3914 }
3915
3916 /// Provide fast operand accessors
3918
3919 /// Convenience accessor.
3920 Value *getValue() const { return Op<0>(); }
3921
3922 unsigned getNumSuccessors() const { return 0; }
3923
3924 // Methods for support type inquiry through isa, cast, and dyn_cast:
3925 static bool classof(const Instruction *I) {
3926 return I->getOpcode() == Instruction::Resume;
3927 }
3928 static bool classof(const Value *V) {
3929 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3930 }
3931
3932private:
3933 BasicBlock *getSuccessor(unsigned idx) const {
3934 llvm_unreachable("ResumeInst has no successors!");
3935 }
3936
3937 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3938 llvm_unreachable("ResumeInst has no successors!");
3939 }
3940};
3941
3942template <>
3944 public FixedNumOperandTraits<ResumeInst, 1> {
3945};
3946
3948
3949//===----------------------------------------------------------------------===//
3950// CatchSwitchInst Class
3951//===----------------------------------------------------------------------===//
3953 using UnwindDestField = BoolBitfieldElementT<0>;
3954
3955 /// The number of operands actually allocated. NumOperands is
3956 /// the number actually in use.
3957 unsigned ReservedSpace;
3958
3959 // Operand[0] = Outer scope
3960 // Operand[1] = Unwind block destination
3961 // Operand[n] = BasicBlock to go to on match
3962 CatchSwitchInst(const CatchSwitchInst &CSI);
3963
3964 /// Create a new switch instruction, specifying a
3965 /// default destination. The number of additional handlers can be specified
3966 /// here to make memory allocation more efficient.
3967 /// This constructor can also autoinsert before another instruction.
3968 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
3969 unsigned NumHandlers, const Twine &NameStr,
3970 InsertPosition InsertBefore);
3971
3972 // allocate space for exactly zero operands
3973 void *operator new(size_t S) { return User::operator new(S); }
3974
3975 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved);
3976 void growOperands(unsigned Size);
3977
3978protected:
3979 // Note: Instruction needs to be a friend here to call cloneImpl.
3980 friend class Instruction;
3981
3982 CatchSwitchInst *cloneImpl() const;
3983
3984public:
3985 void operator delete(void *Ptr) { return User::operator delete(Ptr); }
3986
3987 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
3988 unsigned NumHandlers,
3989 const Twine &NameStr = "",
3990 InsertPosition InsertBefore = nullptr) {
3991 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
3992 InsertBefore);
3993 }
3994
3995 /// Provide fast operand accessors
3997
3998 // Accessor Methods for CatchSwitch stmt
3999 Value *getParentPad() const { return getOperand(0); }
4000 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); }
4001
4002 // Accessor Methods for CatchSwitch stmt
4003 bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); }
4004 bool unwindsToCaller() const { return !hasUnwindDest(); }
4006 if (hasUnwindDest())
4007 return cast<BasicBlock>(getOperand(1));
4008 return nullptr;
4009 }
4010 void setUnwindDest(BasicBlock *UnwindDest) {
4011 assert(UnwindDest);
4012 assert(hasUnwindDest());
4013 setOperand(1, UnwindDest);
4014 }
4015
4016 /// return the number of 'handlers' in this catchswitch
4017 /// instruction, except the default handler
4018 unsigned getNumHandlers() const {
4019 if (hasUnwindDest())
4020 return getNumOperands() - 2;
4021 return getNumOperands() - 1;
4022 }
4023
4024private:
4025 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); }
4026 static const BasicBlock *handler_helper(const Value *V) {
4027 return cast<BasicBlock>(V);
4028 }
4029
4030public:
4031 using DerefFnTy = BasicBlock *(*)(Value *);
4034 using ConstDerefFnTy = const BasicBlock *(*)(const Value *);
4038
4039 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4041 op_iterator It = op_begin() + 1;
4042 if (hasUnwindDest())
4043 ++It;
4044 return handler_iterator(It, DerefFnTy(handler_helper));
4045 }
4046
4047 /// Returns an iterator that points to the first handler in the
4048 /// CatchSwitchInst.
4050 const_op_iterator It = op_begin() + 1;
4051 if (hasUnwindDest())
4052 ++It;
4053 return const_handler_iterator(It, ConstDerefFnTy(handler_helper));
4054 }
4055
4056 /// Returns a read-only iterator that points one past the last
4057 /// handler in the CatchSwitchInst.
4059 return handler_iterator(op_end(), DerefFnTy(handler_helper));
4060 }
4061
4062 /// Returns an iterator that points one past the last handler in the
4063 /// CatchSwitchInst.
4065 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper));
4066 }
4067
4068 /// iteration adapter for range-for loops.
4070 return make_range(handler_begin(), handler_end());
4071 }
4072
4073 /// iteration adapter for range-for loops.
4075 return make_range(handler_begin(), handler_end());
4076 }
4077
4078 /// Add an entry to the switch instruction...
4079 /// Note:
4080 /// This action invalidates handler_end(). Old handler_end() iterator will
4081 /// point to the added handler.
4082 void addHandler(BasicBlock *Dest);
4083
4084 void removeHandler(handler_iterator HI);
4085
4086 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4087 BasicBlock *getSuccessor(unsigned Idx) const {
4088 assert(Idx < getNumSuccessors() &&
4089 "Successor # out of range for catchswitch!");
4090 return cast<BasicBlock>(getOperand(Idx + 1));
4091 }
4092 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
4093 assert(Idx < getNumSuccessors() &&
4094 "Successor # out of range for catchswitch!");
4095 setOperand(Idx + 1, NewSucc);
4096 }
4097
4098 // Methods for support type inquiry through isa, cast, and dyn_cast:
4099 static bool classof(const Instruction *I) {
4100 return I->getOpcode() == Instruction::CatchSwitch;
4101 }
4102 static bool classof(const Value *V) {
4103 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4104 }
4105};
4106
4107template <>
4109
4111
4112//===----------------------------------------------------------------------===//
4113// CleanupPadInst Class
4114//===----------------------------------------------------------------------===//
4116private:
4117 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4118 unsigned Values, const Twine &NameStr,
4119 InsertPosition InsertBefore)
4120 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4121 NameStr, InsertBefore) {}
4122
4123public:
4124 static CleanupPadInst *Create(Value *ParentPad,
4125 ArrayRef<Value *> Args = std::nullopt,
4126 const Twine &NameStr = "",
4127 InsertPosition InsertBefore = nullptr) {
4128 unsigned Values = 1 + Args.size();
4129 return new (Values)
4130 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore);
4131 }
4132
4133 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4134 static bool classof(const Instruction *I) {
4135 return I->getOpcode() == Instruction::CleanupPad;
4136 }
4137 static bool classof(const Value *V) {
4138 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4139 }
4140};
4141
4142//===----------------------------------------------------------------------===//
4143// CatchPadInst Class
4144//===----------------------------------------------------------------------===//
4146private:
4147 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4148 unsigned Values, const Twine &NameStr,
4149 InsertPosition InsertBefore)
4150 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4151 NameStr, InsertBefore) {}
4152
4153public:
4154 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4155 const Twine &NameStr = "",
4156 InsertPosition InsertBefore = nullptr) {
4157 unsigned Values = 1 + Args.size();
4158 return new (Values)
4159 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore);
4160 }
4161
4162 /// Convenience accessors
4164 return cast<CatchSwitchInst>(Op<-1>());
4165 }
4166 void setCatchSwitch(Value *CatchSwitch) {
4167 assert(CatchSwitch);
4168 Op<-1>() = CatchSwitch;
4169 }
4170
4171 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4172 static bool classof(const Instruction *I) {
4173 return I->getOpcode() == Instruction::CatchPad;
4174 }
4175 static bool classof(const Value *V) {
4176 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4177 }
4178};
4179
4180//===----------------------------------------------------------------------===//
4181// CatchReturnInst Class
4182//===----------------------------------------------------------------------===//
4183
4186 CatchReturnInst(Value *CatchPad, BasicBlock *BB, InsertPosition InsertBefore);
4187
4188 void init(Value *CatchPad, BasicBlock *BB);
4189
4190protected:
4191 // Note: Instruction needs to be a friend here to call cloneImpl.
4192 friend class Instruction;
4193
4194 CatchReturnInst *cloneImpl() const;
4195
4196public:
4197 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4198 InsertPosition InsertBefore = nullptr) {
4199 assert(CatchPad);
4200 assert(BB);
4201 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4202 }
4203
4204 /// Provide fast operand accessors
4206
4207 /// Convenience accessors.
4208 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4209 void setCatchPad(CatchPadInst *CatchPad) {
4210 assert(CatchPad);
4211 Op<0>() = CatchPad;
4212 }
4213
4214 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4215 void setSuccessor(BasicBlock *NewSucc) {
4216 assert(NewSucc);
4217 Op<1>() = NewSucc;
4218 }
4219 unsigned getNumSuccessors() const { return 1; }
4220
4221 /// Get the parentPad of this catchret's catchpad's catchswitch.
4222 /// The successor block is implicitly a member of this funclet.
4225 }
4226
4227 // Methods for support type inquiry through isa, cast, and dyn_cast:
4228 static bool classof(const Instruction *I) {
4229 return (I->getOpcode() == Instruction::CatchRet);
4230 }
4231 static bool classof(const Value *V) {
4232 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4233 }
4234
4235private:
4236 BasicBlock *getSuccessor(unsigned Idx) const {
4237 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
4238 return getSuccessor();
4239 }
4240
4241 void setSuccessor(unsigned Idx, BasicBlock *B) {
4242 assert(Idx < getNumSuccessors() && "Successor # out of range for catchret!");
4243 setSuccessor(B);
4244 }
4245};
4246
4247template <>
4249 : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4250
4252
4253//===----------------------------------------------------------------------===//
4254// CleanupReturnInst Class
4255//===----------------------------------------------------------------------===//
4256
4258 using UnwindDestField = BoolBitfieldElementT<0>;
4259
4260private:
4262 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4263 InsertPosition InsertBefore = nullptr);
4264
4265 void init(Value *CleanupPad, BasicBlock *UnwindBB);
4266
4267protected:
4268 // Note: Instruction needs to be a friend here to call cloneImpl.
4269 friend class Instruction;
4270
4271 CleanupReturnInst *cloneImpl() const;
4272
4273public:
4274 static CleanupReturnInst *Create(Value *CleanupPad,
4275 BasicBlock *UnwindBB = nullptr,
4276 InsertPosition InsertBefore = nullptr) {
4277 assert(CleanupPad);
4278 unsigned Values = 1;
4279 if (UnwindBB)
4280 ++Values;
4281 return new (Values)
4282 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4283 }
4284
4285 /// Provide fast operand accessors
4287
4288 bool hasUnwindDest() const { return getSubclassData<UnwindDestField>(); }
4289 bool unwindsToCaller() const { return !hasUnwindDest(); }
4290
4291 /// Convenience accessor.
4293 return cast<CleanupPadInst>(Op<0>());
4294 }
4295 void setCleanupPad(CleanupPadInst *CleanupPad) {
4296 assert(CleanupPad);
4297 Op<0>() = CleanupPad;
4298 }
4299
4300 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4301
4303 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr;
4304 }
4305 void setUnwindDest(BasicBlock *NewDest) {
4306 assert(NewDest);
4307 assert(hasUnwindDest());
4308 Op<1>() = NewDest;
4309 }
4310
4311 // Methods for support type inquiry through isa, cast, and dyn_cast:
4312 static bool classof(const Instruction *I) {
4313 return (I->getOpcode() == Instruction::CleanupRet);
4314 }
4315 static bool classof(const Value *V) {
4316 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4317 }
4318
4319private:
4320 BasicBlock *getSuccessor(unsigned Idx) const {
4321 assert(Idx == 0);
4322 return getUnwindDest();
4323 }
4324
4325 void setSuccessor(unsigned Idx, BasicBlock *B) {
4326 assert(Idx == 0);
4327 setUnwindDest(B);
4328 }
4329
4330 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4331 // method so that subclasses cannot accidentally use it.
4332 template <typename Bitfield>
4333 void setSubclassData(typename Bitfield::Type Value) {
4334 Instruction::setSubclassData<Bitfield>(Value);
4335 }
4336};
4337
4338template <>
4340 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4341
4343
4344//===----------------------------------------------------------------------===//
4345// UnreachableInst Class
4346//===----------------------------------------------------------------------===//
4347
4348//===---------------------------------------------------------------------------
4349/// This function has undefined behavior. In particular, the
4350/// presence of this instruction indicates some higher level knowledge that the
4351/// end of the block cannot be reached.
4352///
4354protected:
4355 // Note: Instruction needs to be a friend here to call cloneImpl.
4356 friend class Instruction;
4357
4358 UnreachableInst *cloneImpl() const;
4359
4360public:
4361 explicit UnreachableInst(LLVMContext &C,
4362 InsertPosition InsertBefore = nullptr);
4363
4364 // allocate space for exactly zero operands
4365 void *operator new(size_t S) { return User::operator new(S, 0); }
4366 void operator delete(void *Ptr) { User::operator delete(Ptr); }
4367
4368 unsigned getNumSuccessors() const { return 0; }
4369
4370 // Methods for support type inquiry through isa, cast, and dyn_cast:
4371 static bool classof(const Instruction *I) {
4372 return I->getOpcode() == Instruction::Unreachable;
4373 }
4374 static bool classof(const Value *V) {
4375 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4376 }
4377
4378private:
4379 BasicBlock *getSuccessor(unsigned idx) const {
4380 llvm_unreachable("UnreachableInst has no successors!");
4381 }
4382
4383 void setSuccessor(unsigned idx, BasicBlock *B) {
4384 llvm_unreachable("UnreachableInst has no successors!");
4385 }
4386};
4387
4388//===----------------------------------------------------------------------===//
4389// TruncInst Class
4390//===----------------------------------------------------------------------===//
4391
4392/// This class represents a truncation of integer types.
4393class TruncInst : public CastInst {
4394protected:
4395 // Note: Instruction needs to be a friend here to call cloneImpl.
4396 friend class Instruction;
4397
4398 /// Clone an identical TruncInst
4399 TruncInst *cloneImpl() const;
4400
4401public:
4402 enum { AnyWrap = 0, NoUnsignedWrap = (1 << 0), NoSignedWrap = (1 << 1) };
4403
4404 /// Constructor with insert-before-instruction semantics
4405 TruncInst(Value *S, ///< The value to be truncated
4406 Type *Ty, ///< The (smaller) type to truncate to
4407 const Twine &NameStr = "", ///< A name for the new instruction
4408 InsertPosition InsertBefore =
4409 nullptr ///< Where to insert the new instruction
4410 );
4411
4412 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4413 static bool classof(const Instruction *I) {
4414 return I->getOpcode() == Trunc;
4415 }
4416 static bool classof(const Value *V) {
4417 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4418 }
4419
4422 (SubclassOptionalData & ~NoUnsignedWrap) | (B * NoUnsignedWrap);
4423 }
4426 (SubclassOptionalData & ~NoSignedWrap) | (B * NoSignedWrap);
4427 }
4428
4429 /// Test whether this operation is known to never
4430 /// undergo unsigned overflow, aka the nuw property.
4431 bool hasNoUnsignedWrap() const {
4433 }
4434
4435 /// Test whether this operation is known to never
4436 /// undergo signed overflow, aka the nsw property.
4437 bool hasNoSignedWrap() const {
4438 return (SubclassOptionalData & NoSignedWrap) != 0;
4439 }
4440
4441 /// Returns the no-wrap kind of the operation.
4442 unsigned getNoWrapKind() const {
4443 unsigned NoWrapKind = 0;
4444 if (hasNoUnsignedWrap())
4445 NoWrapKind |= NoUnsignedWrap;
4446
4447 if (hasNoSignedWrap())
4448 NoWrapKind |= NoSignedWrap;
4449
4450 return NoWrapKind;
4451 }
4452};
4453
4454//===----------------------------------------------------------------------===//
4455// ZExtInst Class
4456//===----------------------------------------------------------------------===//
4457
4458/// This class represents zero extension of integer types.
4459class ZExtInst : public CastInst {
4460protected:
4461 // Note: Instruction needs to be a friend here to call cloneImpl.
4462 friend class Instruction;
4463
4464 /// Clone an identical ZExtInst
4465 ZExtInst *cloneImpl() const;
4466
4467public:
4468 /// Constructor with insert-before-instruction semantics
4469 ZExtInst(Value *S, ///< The value to be zero extended
4470 Type *Ty, ///< The type to zero extend to
4471 const Twine &NameStr = "", ///< A name for the new instruction
4472 InsertPosition InsertBefore =
4473 nullptr ///< Where to insert the new instruction
4474 );
4475
4476 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4477 static bool classof(const Instruction *I) {
4478 return I->getOpcode() == ZExt;
4479 }
4480 static bool classof(const Value *V) {
4481 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4482 }
4483};
4484
4485//===----------------------------------------------------------------------===//
4486// SExtInst Class
4487//===----------------------------------------------------------------------===//
4488
4489/// This class represents a sign extension of integer types.
4490class SExtInst : public CastInst {
4491protected:
4492 // Note: Instruction needs to be a friend here to call cloneImpl.
4493 friend class Instruction;
4494
4495 /// Clone an identical SExtInst
4496 SExtInst *cloneImpl() const;
4497
4498public:
4499 /// Constructor with insert-before-instruction semantics
4500 SExtInst(Value *S, ///< The value to be sign extended
4501 Type *Ty, ///< The type to sign extend to
4502 const Twine &NameStr = "", ///< A name for the new instruction
4503 InsertPosition InsertBefore =
4504 nullptr ///< Where to insert the new instruction
4505 );
4506
4507 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4508 static bool classof(const Instruction *I) {
4509 return I->getOpcode() == SExt;
4510 }
4511 static bool classof(const Value *V) {
4512 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4513 }
4514};
4515
4516//===----------------------------------------------------------------------===//
4517// FPTruncInst Class
4518//===----------------------------------------------------------------------===//
4519
4520/// This class represents a truncation of floating point types.
4521class FPTruncInst : public CastInst {
4522protected:
4523 // Note: Instruction needs to be a friend here to call cloneImpl.
4524 friend class Instruction;
4525
4526 /// Clone an identical FPTruncInst
4527 FPTruncInst *cloneImpl() const;
4528
4529public: /// Constructor with insert-before-instruction semantics
4530 FPTruncInst(Value *S, ///< The value to be truncated
4531 Type *Ty, ///< The type to truncate to
4532 const Twine &NameStr = "", ///< A name for the new instruction
4533 InsertPosition InsertBefore =
4534 nullptr ///< Where to insert the new instruction
4535 );
4536
4537 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4538 static bool classof(const Instruction *I) {
4539 return I->getOpcode() == FPTrunc;
4540 }
4541 static bool classof(const Value *V) {
4542 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4543 }
4544};
4545
4546//===----------------------------------------------------------------------===//
4547// FPExtInst Class
4548//===----------------------------------------------------------------------===//
4549
4550/// This class represents an extension of floating point types.
4551class FPExtInst : public CastInst {
4552protected:
4553 // Note: Instruction needs to be a friend here to call cloneImpl.
4554 friend class Instruction;
4555
4556 /// Clone an identical FPExtInst
4557 FPExtInst *cloneImpl() const;
4558
4559public:
4560 /// Constructor with insert-before-instruction semantics
4561 FPExtInst(Value *S, ///< The value to be extended
4562 Type *Ty, ///< The type to extend to
4563 const Twine &NameStr = "", ///< A name for the new instruction
4564 InsertPosition InsertBefore =
4565 nullptr ///< Where to insert the new instruction
4566 );
4567
4568 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4569 static bool classof(const Instruction *I) {
4570 return I->getOpcode() == FPExt;
4571 }
4572 static bool classof(const Value *V) {
4573 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4574 }
4575};
4576
4577//===----------------------------------------------------------------------===//
4578// UIToFPInst Class
4579//===----------------------------------------------------------------------===//
4580
4581/// This class represents a cast unsigned integer to floating point.
4582class UIToFPInst : public CastInst {
4583protected:
4584 // Note: Instruction needs to be a friend here to call cloneImpl.
4585 friend class Instruction;
4586
4587 /// Clone an identical UIToFPInst
4588 UIToFPInst *cloneImpl() const;
4589
4590public:
4591 /// Constructor with insert-before-instruction semantics
4592 UIToFPInst(Value *S, ///< The value to be converted
4593 Type *Ty, ///< The type to convert to
4594 const Twine &NameStr = "", ///< A name for the new instruction
4595 InsertPosition InsertBefore =
4596 nullptr ///< Where to insert the new instruction
4597 );
4598
4599 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4600 static bool classof(const Instruction *I) {
4601 return I->getOpcode() == UIToFP;
4602 }
4603 static bool classof(const Value *V) {
4604 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4605 }
4606};
4607
4608//===----------------------------------------------------------------------===//
4609// SIToFPInst Class
4610//===----------------------------------------------------------------------===//
4611
4612/// This class represents a cast from signed integer to floating point.
4613class SIToFPInst : public CastInst {
4614protected:
4615 // Note: Instruction needs to be a friend here to call cloneImpl.
4616 friend class Instruction;
4617
4618 /// Clone an identical SIToFPInst
4619 SIToFPInst *cloneImpl() const;
4620
4621public:
4622 /// Constructor with insert-before-instruction semantics
4623 SIToFPInst(Value *S, ///< The value to be converted
4624 Type *Ty, ///< The type to convert to
4625 const Twine &NameStr = "", ///< A name for the new instruction
4626 InsertPosition InsertBefore =
4627 nullptr ///< Where to insert the new instruction
4628 );
4629
4630 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4631 static bool classof(const Instruction *I) {
4632 return I->getOpcode() == SIToFP;
4633 }
4634 static bool classof(const Value *V) {
4635 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4636 }
4637};
4638
4639//===----------------------------------------------------------------------===//
4640// FPToUIInst Class
4641//===----------------------------------------------------------------------===//
4642
4643/// This class represents a cast from floating point to unsigned integer
4644class FPToUIInst : public CastInst {
4645protected:
4646 // Note: Instruction needs to be a friend here to call cloneImpl.
4647 friend class Instruction;
4648
4649 /// Clone an identical FPToUIInst
4650 FPToUIInst *cloneImpl() const;
4651
4652public:
4653 /// Constructor with insert-before-instruction semantics
4654 FPToUIInst(Value *S, ///< The value to be converted
4655 Type *Ty, ///< The type to convert to
4656 const Twine &NameStr = "", ///< A name for the new instruction
4657 InsertPosition InsertBefore =
4658 nullptr ///< Where to insert the new instruction
4659 );
4660
4661 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4662 static bool classof(const Instruction *I) {
4663 return I->getOpcode() == FPToUI;
4664 }
4665 static bool classof(const Value *V) {
4666 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4667 }
4668};
4669
4670//===----------------------------------------------------------------------===//
4671// FPToSIInst Class
4672//===----------------------------------------------------------------------===//
4673
4674/// This class represents a cast from floating point to signed integer.
4675class FPToSIInst : public CastInst {
4676protected:
4677 // Note: Instruction needs to be a friend here to call cloneImpl.
4678 friend class Instruction;
4679
4680 /// Clone an identical FPToSIInst
4681 FPToSIInst *cloneImpl() const;
4682
4683public:
4684 /// Constructor with insert-before-instruction semantics
4685 FPToSIInst(Value *S, ///< The value to be converted
4686 Type *Ty, ///< The type to convert to
4687 const Twine &NameStr = "", ///< A name for the new instruction
4688 InsertPosition InsertBefore =
4689 nullptr ///< Where to insert the new instruction
4690 );
4691
4692 /// Methods for support type inquiry through isa, cast, and dyn_cast:
4693 static bool classof(const Instruction *I) {
4694 return I->getOpcode() == FPToSI;
4695 }
4696 static bool classof(const Value *V) {
4697 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4698 }
4699};
4700
4701//===----------------------------------------------------------------------===//
4702// IntToPtrInst Class
4703//===----------------------------------------------------------------------===//
4704
4705/// This class represents a cast from an integer to a pointer.
4706class IntToPtrInst : public CastInst {
4707public:
4708 // Note: Instruction needs to be a friend here to call cloneImpl.
4709 friend class Instruction;
4710
4711 /// Constructor with insert-before-instruction semantics
4712 IntToPtrInst(Value *S, ///< The value to be converted
4713 Type *Ty, ///< The type to convert to
4714 const Twine &NameStr = "", ///< A name for the new instruction
4715 InsertPosition InsertBefore =
4716 nullptr ///< Where to insert the new instruction
4717 );
4718
4719 /// Clone an identical IntToPtrInst.
4720 IntToPtrInst *cloneImpl() const;
4721
4722 /// Returns the address space of this instruction's pointer type.
4723 unsigned getAddressSpace() const {
4724 return getType()->getPointerAddressSpace();
4725 }
4726
4727 // Methods for support type inquiry through isa, cast, and dyn_cast:
4728 static bool classof(const Instruction *I) {
4729 return I->getOpcode() == IntToPtr;
4730 }
4731 static bool classof(const Value *V) {
4732 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4733 }
4734};
4735
4736//===----------------------------------------------------------------------===//
4737// PtrToIntInst Class
4738//===----------------------------------------------------------------------===//
4739
4740/// This class represents a cast from a pointer to an integer.
4741class PtrToIntInst : public CastInst {
4742protected:
4743 // Note: Instruction needs to be a friend here to call cloneImpl.
4744 friend class Instruction;
4745
4746 /// Clone an identical PtrToIntInst.
4747 PtrToIntInst *cloneImpl() const;
4748
4749public:
4750 /// Constructor with insert-before-instruction semantics
4751 PtrToIntInst(Value *S, ///< The value to be converted
4752 Type *Ty, ///< The type to convert to
4753 const Twine &NameStr = "", ///< A name for the new instruction
4754 InsertPosition InsertBefore =
4755 nullptr ///< Where to insert the new instruction
4756 );
4757
4758 /// Gets the pointer operand.
4760 /// Gets the pointer operand.
4761 const Value *getPointerOperand() const { return getOperand(0); }
4762 /// Gets the operand index of the pointer operand.
4763 static unsigned getPointerOperandIndex() { return 0U; }
4764
4765 /// Returns the address space of the pointer operand.
4766 unsigned getPointerAddressSpace() const {
4768 }
4769
4770 // Methods for support type inquiry through isa, cast, and dyn_cast:
4771 static bool classof(const Instruction *I) {
4772 return I->getOpcode() == PtrToInt;
4773 }
4774 static bool classof(const Value *V) {
4775 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4776 }
4777};
4778
4779//===----------------------------------------------------------------------===//
4780// BitCastInst Class
4781//===----------------------------------------------------------------------===//
4782
4783/// This class represents a no-op cast from one type to another.
4784class BitCastInst : public CastInst {
4785protected:
4786 // Note: Instruction needs to be a friend here to call cloneImpl.
4787 friend class Instruction;
4788
4789 /// Clone an identical BitCastInst.
4790 BitCastInst *cloneImpl() const;
4791
4792public:
4793 /// Constructor with insert-before-instruction semantics
4794 BitCastInst(Value *S, ///< The value to be casted
4795 Type *Ty, ///< The type to casted to
4796 const Twine &NameStr = "", ///< A name for the new instruction
4797 InsertPosition InsertBefore =
4798 nullptr ///< Where to insert the new instruction
4799 );
4800
4801 // Methods for support type inquiry through isa, cast, and dyn_cast:
4802 static bool classof(const Instruction *I) {
4803 return I->getOpcode() == BitCast;
4804 }
4805 static bool classof(const Value *V) {
4806 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4807 }
4808};
4809
4810//===----------------------------------------------------------------------===//
4811// AddrSpaceCastInst Class
4812//===----------------------------------------------------------------------===//
4813
4814/// This class represents a conversion between pointers from one address space
4815/// to another.
4817protected:
4818 // Note: Instruction needs to be a friend here to call cloneImpl.
4819 friend class Instruction;
4820
4821 /// Clone an identical AddrSpaceCastInst.
4823
4824public:
4825 /// Constructor with insert-before-instruction semantics
4827 Value *S, ///< The value to be casted
4828 Type *Ty, ///< The type to casted to
4829 const Twine &NameStr = "", ///< A name for the new instruction
4830 InsertPosition InsertBefore =
4831 nullptr ///< Where to insert the new instruction
4832 );
4833
4834 // Methods for support type inquiry through isa, cast, and dyn_cast:
4835 static bool classof(const Instruction *I) {
4836 return I->getOpcode() == AddrSpaceCast;
4837 }
4838 static bool classof(const Value *V) {
4839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4840 }
4841
4842 /// Gets the pointer operand.
4844 return getOperand(0);
4845 }
4846
4847 /// Gets the pointer operand.
4848 const Value *getPointerOperand() const {
4849 return getOperand(0);
4850 }
4851
4852 /// Gets the operand index of the pointer operand.
4853 static unsigned getPointerOperandIndex() {
4854 return 0U;
4855 }
4856
4857 /// Returns the address space of the pointer operand.
4858 unsigned getSrcAddressSpace() const {
4860 }
4861
4862 /// Returns the address space of the result.
4863 unsigned getDestAddressSpace() const {
4864 return getType()->getPointerAddressSpace();
4865 }
4866};
4867
4868//===----------------------------------------------------------------------===//
4869// Helper functions
4870//===----------------------------------------------------------------------===//
4871
4872/// A helper function that returns the pointer operand of a load or store
4873/// instruction. Returns nullptr if not load or store.
4874inline const Value *getLoadStorePointerOperand(const Value *V) {
4875 if (auto *Load = dyn_cast<LoadInst>(V))
4876 return Load->getPointerOperand();
4877 if (auto *Store = dyn_cast<StoreInst>(V))
4878 return Store->getPointerOperand();
4879 return nullptr;
4880}
4882 return const_cast<Value *>(
4883 getLoadStorePointerOperand(static_cast<const Value *>(V)));
4884}
4885
4886/// A helper function that returns the pointer operand of a load, store
4887/// or GEP instruction. Returns nullptr if not load, store, or GEP.
4888inline const Value *getPointerOperand(const Value *V) {
4889 if (auto *Ptr = getLoadStorePointerOperand(V))
4890 return Ptr;
4891 if (auto *Gep = dyn_cast<GetElementPtrInst>(V))
4892 return Gep->getPointerOperand();
4893 return nullptr;
4894}
4896 return const_cast<Value *>(getPointerOperand(static_cast<const Value *>(V)));
4897}
4898
4899/// A helper function that returns the alignment of load or store instruction.
4901 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
4902 "Expected Load or Store instruction");
4903 if (auto *LI = dyn_cast<LoadInst>(I))
4904 return LI->getAlign();
4905 return cast<StoreInst>(I)->getAlign();
4906}
4907
4908/// A helper function that returns the address space of the pointer operand of
4909/// load or store instruction.
4910inline unsigned getLoadStoreAddressSpace(const Value *I) {
4911 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
4912 "Expected Load or Store instruction");
4913 if (auto *LI = dyn_cast<LoadInst>(I))
4914 return LI->getPointerAddressSpace();
4915 return cast<StoreInst>(I)->getPointerAddressSpace();
4916}
4917
4918/// A helper function that returns the type of a load or store instruction.
4919inline Type *getLoadStoreType(const Value *I) {
4920 assert((isa<LoadInst>(I) || isa<StoreInst>(I)) &&
4921 "Expected Load or Store instruction");
4922 if (auto *LI = dyn_cast<LoadInst>(I))
4923 return LI->getType();
4924 return cast<StoreInst>(I)->getValueOperand()->getType();
4925}
4926
4927/// A helper function that returns an atomic operation's sync scope; returns
4928/// std::nullopt if it is not an atomic operation.
4929inline std::optional<SyncScope::ID> getAtomicSyncScopeID(const Instruction *I) {
4930 if (!I->isAtomic())
4931 return std::nullopt;
4932 if (auto *AI = dyn_cast<LoadInst>(I))
4933 return AI->getSyncScopeID();
4934 if (auto *AI = dyn_cast<StoreInst>(I))
4935 return AI->getSyncScopeID();
4936 if (auto *AI = dyn_cast<FenceInst>(I))
4937 return AI->getSyncScopeID();
4938 if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I))
4939 return AI->getSyncScopeID();
4940 if (auto *AI = dyn_cast<AtomicRMWInst>(I))
4941 return AI->getSyncScopeID();
4942 llvm_unreachable("unhandled atomic operation");
4943}
4944
4945/// A helper function that sets an atomic operation's sync scope.
4947 assert(I->isAtomic());
4948 if (auto *AI = dyn_cast<LoadInst>(I))
4949 AI->setSyncScopeID(SSID);
4950 else if (auto *AI = dyn_cast<StoreInst>(I))
4951 AI->setSyncScopeID(SSID);
4952 else if (auto *AI = dyn_cast<FenceInst>(I))
4953 AI->setSyncScopeID(SSID);
4954 else if (auto *AI = dyn_cast<AtomicCmpXchgInst>(I))
4955 AI->setSyncScopeID(SSID);
4956 else if (auto *AI = dyn_cast<AtomicRMWInst>(I))
4957 AI->setSyncScopeID(SSID);
4958 else
4959 llvm_unreachable("unhandled atomic operation");
4960}
4961
4962//===----------------------------------------------------------------------===//
4963// FreezeInst Class
4964//===----------------------------------------------------------------------===//
4965
4966/// This class represents a freeze function that returns random concrete
4967/// value if an operand is either a poison value or an undef value
4969protected:
4970 // Note: Instruction needs to be a friend here to call cloneImpl.
4971 friend class Instruction;
4972
4973 /// Clone an identical FreezeInst
4974 FreezeInst *cloneImpl() const;
4975
4976public:
4977 explicit FreezeInst(Value *S, const Twine &NameStr = "",
4978 InsertPosition InsertBefore = nullptr);
4979
4980 // Methods for support type inquiry through isa, cast, and dyn_cast:
4981 static inline bool classof(const Instruction *I) {
4982 return I->getOpcode() == Freeze;
4983 }
4984 static inline bool classof(const Value *V) {
4985 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4986 }
4987};
4988
4989} // end namespace llvm
4990
4991#endif // LLVM_IR_INSTRUCTIONS_H
static const LLT S1
static bool isReverseMask(ArrayRef< int > M, EVT VT)
MachineBasicBlock MachineBasicBlock::iterator DebugLoc DL
always inline
Atomic ordering constants.
static const Function * getParent(const Value *V)
This file implements methods to test, set and extract typed bits from packed unsigned integers.
static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")
RelocType Type
Definition: COFFYAML.cpp:391
return RetTy
Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx
uint64_t Align
std::string Name
uint32_t Index
uint64_t Size
Hexagon Common GEP
hexagon gen pred
This file provides various utilities for inspecting and working with the control flow graph in LLVM I...
This defines the Use class.
#define F(x, y, z)
Definition: MD5.cpp:55
#define I(x, y, z)
Definition: MD5.cpp:58
This file implements a map that provides insertion order iteration.
uint64_t IntrinsicInst * II
#define DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CLASS, VALUECLASS)
Macro for generating out-of-class operand accessor definitions.
#define P(N)
PowerPC Reduce CR logical Operation
StandardInstrumentations SI(Mod->getContext(), Debug, VerifyEach)
const SmallVectorImpl< MachineOperand > & Cond
const MachineOperand & RHS
assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())
This file contains some templates that are useful if you are working with the STL at all.
This file defines the SmallVector class.
static SymbolRef::Type getType(const Symbol *Sym)
Definition: TapiFile.cpp:40
Value * LHS
Class for arbitrary precision integers.
Definition: APInt.h:77
This class represents a conversion between pointers from one address space to another.
const Value * getPointerOperand() const
Gets the pointer operand.
AddrSpaceCastInst * cloneImpl() const
Clone an identical AddrSpaceCastInst.
Value * getPointerOperand()
Gets the pointer operand.
static bool classof(const Instruction *I)
static bool classof(const Value *V)
unsigned getSrcAddressSpace() const
Returns the address space of the pointer operand.
unsigned getDestAddressSpace() const
Returns the address space of the result.
static unsigned getPointerOperandIndex()
Gets the operand index of the pointer operand.
an instruction to allocate memory on the stack
Definition: Instructions.h:61
std::optional< TypeSize > getAllocationSizeInBits(const DataLayout &DL) const
Get allocation size in bits.
static bool classof(const Value *V)
Definition: Instructions.h:155
bool isSwiftError() const
Return true if this alloca is used as a swifterror argument to a call.
Definition: Instructions.h:147
void setSwiftError(bool V)
Specify whether this alloca is used to represent a swifterror.
Definition: Instructions.h:149
bool isStaticAlloca() const
Return true if this alloca is in the entry block of the function and is a constant size.
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:122
void setAllocatedType(Type *Ty)
for use only in special circumstances that need to generically transform a whole instruction (eg: IR ...
Definition: Instructions.h:118
static bool classof(const Instruction *I)
Definition: Instructions.h:152
PointerType * getType() const
Overload to return most specific pointer type.
Definition: Instructions.h:97
void setUsedWithInAlloca(bool V)
Specify whether this alloca is used to represent the arguments to a call.
Definition: Instructions.h:142
AllocaInst * cloneImpl() const
Type * getAllocatedType() const
Return the type that is being allocated by the instruction.
Definition: Instructions.h:115
bool isUsedWithInAlloca() const
Return true if this alloca is used as an inalloca argument to a call.
Definition: Instructions.h:137
Value * getArraySize()
Definition: Instructions.h:94
unsigned getAddressSpace() const
Return the address space for the allocation.
Definition: Instructions.h:102
std::optional< TypeSize > getAllocationSize(const DataLayout &DL) const
Get allocation size in bytes.
bool isArrayAllocation() const
Return true if there is an allocation size parameter to the allocation instruction that is not 1.
void setAlignment(Align Align)
Definition: Instructions.h:126
const Value * getArraySize() const
Get the number of elements allocated.
Definition: Instructions.h:93
ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...
Definition: ArrayRef.h:41
size_t size() const
size - Get the array size.
Definition: ArrayRef.h:165
An instruction that atomically checks whether a specified value is in a memory location,...
Definition: Instructions.h:495
BoolBitfieldElementT< 0 > VolatileField
Definition: Instructions.h:521
const Value * getCompareOperand() const
Definition: Instructions.h:626
void setSyncScopeID(SyncScope::ID SSID)
Sets the synchronization scope ID of this cmpxchg instruction.
Definition: Instructions.h:617
AtomicOrdering getMergedOrdering() const
Returns a single ordering which is at least as strong as both the success and failure orderings for t...
Definition: Instructions.h:599
void setWeak(bool IsWeak)
Definition: Instructions.h:556
bool isVolatile() const
Return true if this is a cmpxchg from a volatile memory location.
Definition: Instructions.h:547
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:632
BoolBitfieldElementT< VolatileField::NextBit > WeakField
Definition: Instructions.h:522
AtomicOrderingBitfieldElementT< SuccessOrderingField::NextBit > FailureOrderingField
Definition: Instructions.h:526
void setFailureOrdering(AtomicOrdering Ordering)
Sets the failure ordering constraint of this cmpxchg instruction.
Definition: Instructions.h:591
static bool isValidFailureOrdering(AtomicOrdering Ordering)
Definition: Instructions.h:566
AtomicOrdering getFailureOrdering() const
Returns the failure ordering constraint of this cmpxchg instruction.
Definition: Instructions.h:586
void setSuccessOrdering(AtomicOrdering Ordering)
Sets the success ordering constraint of this cmpxchg instruction.
Definition: Instructions.h:579
AlignmentBitfieldElementT< FailureOrderingField::NextBit > AlignmentField
Definition: Instructions.h:528
static AtomicOrdering getStrongestFailureOrdering(AtomicOrdering SuccessOrdering)
Returns the strongest permitted ordering on failure, given the desired ordering on success.
Definition: Instructions.h:644
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
AtomicCmpXchgInst * cloneImpl() const
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:536
const Value * getPointerOperand() const
Definition: Instructions.h:622
static bool classof(const Value *V)
Definition: Instructions.h:663
bool isWeak() const
Return true if this cmpxchg may spuriously fail.
Definition: Instructions.h:554
void setAlignment(Align Align)
Definition: Instructions.h:540
void setVolatile(bool V)
Specify whether this is a volatile cmpxchg.
Definition: Instructions.h:551
static bool isValidSuccessOrdering(AtomicOrdering Ordering)
Definition: Instructions.h:561
AtomicOrdering getSuccessOrdering() const
Returns the success ordering constraint of this cmpxchg instruction.
Definition: Instructions.h:574
AtomicOrderingBitfieldElementT< WeakField::NextBit > SuccessOrderingField
Definition: Instructions.h:524
static unsigned getPointerOperandIndex()
Definition: Instructions.h:623
const Value * getNewValOperand() const
Definition: Instructions.h:629
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this cmpxchg instruction.
Definition: Instructions.h:612
static bool classof(const Instruction *I)
Definition: Instructions.h:660
an instruction that atomically reads a memory location, combines it with another value,...
Definition: Instructions.h:696
Align getAlign() const
Return the alignment of the memory that is being allocated by the instruction.
Definition: Instructions.h:809
static bool isFPOperation(BinOp Op)
Definition: Instructions.h:791
static unsigned getPointerOperandIndex()
Definition: Instructions.h:854
bool isVolatile() const
Return true if this is a RMW on a volatile memory location.
Definition: Instructions.h:819
void setVolatile(bool V)
Specify whether this is a volatile RMW or not.
Definition: Instructions.h:823
BinOpBitfieldElement< AtomicOrderingField::NextBit > OperationField
Definition: Instructions.h:781
BinOp
This enumeration lists the possible modifications atomicrmw can make.
Definition: Instructions.h:708
@ Add
*p = old + v
Definition: Instructions.h:712
@ FAdd
*p = old + v
Definition: Instructions.h:733
@ Min
*p = old <signed v ? old : v
Definition: Instructions.h:726
@ Or
*p = old | v
Definition: Instructions.h:720
@ Sub
*p = old - v
Definition: Instructions.h:714
@ And
*p = old & v
Definition: Instructions.h:716
@ Xor
*p = old ^ v
Definition: Instructions.h:722
@ FSub
*p = old - v
Definition: Instructions.h:736
@ UIncWrap
Increment one up to a maximum value.
Definition: Instructions.h:748
@ Max
*p = old >signed v ? old : v
Definition: Instructions.h:724
@ UMin
*p = old <unsigned v ? old : v
Definition: Instructions.h:730
@ FMin
*p = minnum(old, v) minnum matches the behavior of llvm.minnum.
Definition: Instructions.h:744
@ UMax
*p = old >unsigned v ? old : v
Definition: Instructions.h:728
@ FMax
*p = maxnum(old, v) maxnum matches the behavior of llvm.maxnum.
Definition: Instructions.h:740
@ UDecWrap
Decrement one until a minimum value or zero.
Definition: Instructions.h:752
@ Nand
*p = ~(old & v)
Definition: Instructions.h:718
AtomicOrderingBitfieldElementT< VolatileField::NextBit > AtomicOrderingField
Definition: Instructions.h:780
void setSyncScopeID(SyncScope::ID SSID)
Sets the synchronization scope ID of this rmw instruction.
Definition: Instructions.h:848
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
Value * getPointerOperand()
Definition: Instructions.h:852
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this rmw instruction.
Definition: Instructions.h:834
bool isFloatingPointOperation() const
Definition: Instructions.h:864
static bool classof(const Instruction *I)
Definition: Instructions.h:869
const Value * getPointerOperand() const
Definition: Instructions.h:853
void setOperation(BinOp Operation)
Definition: Instructions.h:803
static bool classof(const Value *V)
Definition: Instructions.h:872
BinOp getOperation() const
Definition: Instructions.h:787
const Value * getValOperand() const
Definition: Instructions.h:857
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this rmw instruction.
Definition: Instructions.h:843
void setAlignment(Align Align)
Definition: Instructions.h:813
Value * getValOperand()
Definition: Instructions.h:856
AtomicOrdering getOrdering() const
Returns the ordering constraint of this rmw instruction.
Definition: Instructions.h:829
AlignmentBitfieldElementT< OperationField::NextBit > AlignmentField
Definition: Instructions.h:782
BoolBitfieldElementT< 0 > VolatileField
Definition: Instructions.h:778
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:860
LLVM Basic Block Representation.
Definition: BasicBlock.h:61
This class represents a no-op cast from one type to another.
static bool classof(const Instruction *I)
static bool classof(const Value *V)
BitCastInst * cloneImpl() const
Clone an identical BitCastInst.
Conditional or Unconditional Branch instruction.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
iterator_range< succ_op_iterator > successors()
static BranchInst * Create(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond, InsertPosition InsertBefore=nullptr)
void setCondition(Value *V)
static bool classof(const Instruction *I)
bool isConditional() const
unsigned getNumSuccessors() const
static bool classof(const Value *V)
static BranchInst * Create(BasicBlock *IfTrue, InsertPosition InsertBefore=nullptr)
BasicBlock * getSuccessor(unsigned i) const
bool isUnconditional() const
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
Value * getCondition() const
iterator_range< const_succ_op_iterator > successors() const
Base class for all callable instructions (InvokeInst and CallInst) Holds everything related to callin...
Definition: InstrTypes.h:1236
void addFnAttr(Attribute::AttrKind Kind)
Adds the attribute to the function.
Definition: InstrTypes.h:1574
bool hasFnAttr(Attribute::AttrKind Kind) const
Determine whether this call has the given attribute.
Definition: InstrTypes.h:1551
FunctionType * FTy
Definition: InstrTypes.h:1251
Intrinsic::ID getIntrinsicID() const
Returns the intrinsic ID of the intrinsic called or Intrinsic::not_intrinsic if the called function i...
static unsigned CountBundleInputs(ArrayRef< OperandBundleDef > Bundles)
Return the total number of values used in Bundles.
Definition: InstrTypes.h:2372
unsigned arg_size() const
Definition: InstrTypes.h:1408
unsigned getNumTotalBundleOperands() const
Return the total number operands (not operand bundles) used by every operand bundle in this OperandBu...
Definition: InstrTypes.h:2100
CallBr instruction, tracking function calls that may not return control but instead transfer it to a ...
static bool classof(const Value *V)
static bool classof(const Instruction *I)
SmallVector< BasicBlock *, 16 > getIndirectDests() const
static CallBrInst * Create(FunctionCallee Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
void setSuccessor(unsigned i, BasicBlock *NewSucc)
BasicBlock * getSuccessor(unsigned i) const
Value * getIndirectDestLabelUse(unsigned i) const
BasicBlock * getIndirectDest(unsigned i) const
static CallBrInst * Create(FunctionCallee Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=std::nullopt, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
void setDefaultDest(BasicBlock *B)
unsigned getNumSuccessors() const
void setIndirectDest(unsigned i, BasicBlock *B)
Value * getIndirectDestLabel(unsigned i) const
getIndirectDestLabel - Return the i-th indirect dest label.
BasicBlock * getDefaultDest() const
unsigned getNumIndirectDests() const
Return the number of callbr indirect dest labels.
static CallBrInst * Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
CallBrInst * cloneImpl() const
static CallBrInst * Create(FunctionType *Ty, Value *Func, BasicBlock *DefaultDest, ArrayRef< BasicBlock * > IndirectDests, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=std::nullopt, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This class represents a function call, abstracting a target machine's calling convention.
bool isNoTailCall() const
void updateProfWeight(uint64_t S, uint64_t T)
Updates profile metadata by scaling it by S / T.
static bool classof(const Value *V)
bool isTailCall() const
void setCanReturnTwice()
void setTailCallKind(TailCallKind TCK)
static CallInst * Create(FunctionType *Ty, Value *Func, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
bool canReturnTwice() const
Return true if the call can return twice.
TailCallKind getTailCallKind() const
CallInst * cloneImpl() const
static CallInst * Create(FunctionType *Ty, Value *F, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static CallInst * Create(FunctionCallee Func, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=std::nullopt, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static CallInst * Create(FunctionType *Ty, Value *Func, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=std::nullopt, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
void setTailCall(bool IsTc=true)
bool isMustTailCall() const
static CallInst * Create(FunctionCallee Func, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
static bool classof(const Instruction *I)
bool isNonContinuableTrap() const
Return true if the call is for a noreturn trap intrinsic.
static CallInst * Create(FunctionCallee Func, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
This is the base class for all instructions that perform data casts.
Definition: InstrTypes.h:530
CatchSwitchInst * getCatchSwitch() const
Convenience accessors.
void setCatchSwitch(Value *CatchSwitch)
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
static CatchPadInst * Create(Value *CatchSwitch, ArrayRef< Value * > Args, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static bool classof(const Value *V)
static bool classof(const Instruction *I)
BasicBlock * getSuccessor() const
CatchPadInst * getCatchPad() const
Convenience accessors.
void setSuccessor(BasicBlock *NewSucc)
static bool classof(const Value *V)
static CatchReturnInst * Create(Value *CatchPad, BasicBlock *BB, InsertPosition InsertBefore=nullptr)
unsigned getNumSuccessors() const
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
void setCatchPad(CatchPadInst *CatchPad)
CatchReturnInst * cloneImpl() const
Value * getCatchSwitchParentPad() const
Get the parentPad of this catchret's catchpad's catchswitch.
void setUnwindDest(BasicBlock *UnwindDest)
static bool classof(const Instruction *I)
BasicBlock *(*)(Value *) DerefFnTy
const BasicBlock *(*)(const Value *) ConstDerefFnTy
unsigned getNumSuccessors() const
const_handler_iterator handler_begin() const
Returns an iterator that points to the first handler in the CatchSwitchInst.
unsigned getNumHandlers() const
return the number of 'handlers' in this catchswitch instruction, except the default handler
void setSuccessor(unsigned Idx, BasicBlock *NewSucc)
Value * getParentPad() const
void setParentPad(Value *ParentPad)
bool unwindsToCaller() const
static bool classof(const Value *V)
handler_iterator handler_end()
Returns a read-only iterator that points one past the last handler in the CatchSwitchInst.
BasicBlock * getUnwindDest() const
BasicBlock * getSuccessor(unsigned Idx) const
const_handler_iterator handler_end() const
Returns an iterator that points one past the last handler in the CatchSwitchInst.
bool hasUnwindDest() const
handler_iterator handler_begin()
Returns an iterator that points to the first handler in CatchSwitchInst.
static CatchSwitchInst * Create(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumHandlers, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
handler_range handlers()
iteration adapter for range-for loops.
const_handler_range handlers() const
iteration adapter for range-for loops.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
static bool classof(const Value *V)
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
static CleanupPadInst * Create(Value *ParentPad, ArrayRef< Value * > Args=std::nullopt, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static bool classof(const Instruction *I)
CleanupPadInst * getCleanupPad() const
Convenience accessor.
unsigned getNumSuccessors() const
BasicBlock * getUnwindDest() const
bool unwindsToCaller() const
void setCleanupPad(CleanupPadInst *CleanupPad)
static bool classof(const Value *V)
void setUnwindDest(BasicBlock *NewDest)
static CleanupReturnInst * Create(Value *CleanupPad, BasicBlock *UnwindBB=nullptr, InsertPosition InsertBefore=nullptr)
bool hasUnwindDest() const
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
This class is the base class for the comparison instructions.
Definition: InstrTypes.h:747
static Type * makeCmpResultType(Type *opnd_type)
Create a result type for fcmp/icmp.
Definition: InstrTypes.h:1104
void setPredicate(Predicate P)
Set the predicate for this instruction to the specified value.
Definition: InstrTypes.h:850
Predicate
This enumeration lists the possible predicates for CmpInst subclasses.
Definition: InstrTypes.h:757
@ FCMP_OEQ
0 0 0 1 True if ordered and equal
Definition: InstrTypes.h:760
@ FCMP_TRUE
1 1 1 1 Always true (always folded)
Definition: InstrTypes.h:774
@ FCMP_ONE
0 1 1 0 True if ordered and operands are unequal
Definition: InstrTypes.h:765
@ FCMP_UEQ
1 0 0 1 True if unordered or equal
Definition: InstrTypes.h:768
@ FCMP_ORD
0 1 1 1 True if ordered (no nans)
Definition: InstrTypes.h:766
@ FCMP_UNE
1 1 1 0 True if unordered or not equal
Definition: InstrTypes.h:773
@ FCMP_FALSE
0 0 0 0 Always false (always folded)
Definition: InstrTypes.h:759
@ FCMP_UNO
1 0 0 0 True if unordered: isnan(X) | isnan(Y)
Definition: InstrTypes.h:767
Predicate getSwappedPredicate() const
For example, EQ->EQ, SLE->SGE, ULT->UGT, OEQ->OEQ, ULE->UGE, OLT->OGT, etc.
Definition: InstrTypes.h:909
static auto FCmpPredicates()
Returns the sequence of all FCmp predicates.
Definition: InstrTypes.h:796
bool isFPPredicate() const
Definition: InstrTypes.h:864
Predicate getPredicate() const
Return the predicate for this instruction.
Definition: InstrTypes.h:847
This is the shared class of boolean and integer constants.
Definition: Constants.h:81
This is an important base class in LLVM.
Definition: Constant.h:42
This class represents an Operation in the Expression.
A parsed version of the target data layout string in and methods for querying it.
Definition: DataLayout.h:63
This instruction extracts a single (scalar) element from a VectorType value.
const Value * getVectorOperand() const
ExtractElementInst * cloneImpl() const
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
static bool classof(const Value *V)
static ExtractElementInst * Create(Value *Vec, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
const Value * getIndexOperand() const
static bool classof(const Instruction *I)
VectorType * getVectorOperandType() const
static bool isValidOperands(const Value *Vec, const Value *Idx)
Return true if an extractelement instruction can be formed with the specified operands.
This instruction extracts a struct member or array element value from an aggregate value.
ArrayRef< unsigned > getIndices() const
unsigned getNumIndices() const
static bool classof(const Value *V)
static bool classof(const Instruction *I)
iterator_range< idx_iterator > indices() const
idx_iterator idx_end() const
static ExtractValueInst * Create(Value *Agg, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
const Value * getAggregateOperand() const
static unsigned getAggregateOperandIndex()
idx_iterator idx_begin() const
This instruction compares its operands according to the predicate given to the constructor.
bool isRelational() const
FCmpInst(Predicate Pred, Value *LHS, Value *RHS, const Twine &NameStr="", Instruction *FlagsSource=nullptr)
Constructor with no-insertion semantics.
bool isEquality() const
static bool classof(const Value *V)
bool isCommutative() const
static bool compare(const APFloat &LHS, const APFloat &RHS, FCmpInst::Predicate Pred)
Return result of LHS Pred RHS comparison.
static bool isEquality(Predicate Pred)
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
static auto predicates()
Returns the sequence of all FCmp predicates.
FCmpInst * cloneImpl() const
Clone an identical FCmpInst.
void swapOperands()
Exchange the two operands to this instruction in such a way that it does not modify the semantics of ...
FCmpInst(InsertPosition InsertBefore, Predicate pred, Value *LHS, Value *RHS, const Twine &NameStr="")
Constructor with insertion semantics.
This class represents an extension of floating point types.
static bool classof(const Value *V)
FPExtInst * cloneImpl() const
Clone an identical FPExtInst.
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a cast from floating point to signed integer.
static bool classof(const Value *V)
FPToSIInst * cloneImpl() const
Clone an identical FPToSIInst.
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
This class represents a cast from floating point to unsigned integer.
static bool classof(const Value *V)
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
FPToUIInst * cloneImpl() const
Clone an identical FPToUIInst.
This class represents a truncation of floating point types.
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V)
FPTruncInst * cloneImpl() const
Clone an identical FPTruncInst.
An instruction for ordering other memory operations.
Definition: Instructions.h:420
static bool classof(const Value *V)
Definition: Instructions.h:467
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this fence instruction.
Definition: Instructions.h:454
void setSyncScopeID(SyncScope::ID SSID)
Sets the synchronization scope ID of this fence instruction.
Definition: Instructions.h:459
static bool classof(const Instruction *I)
Definition: Instructions.h:464
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this fence instruction.
Definition: Instructions.h:449
AtomicOrdering getOrdering() const
Returns the ordering constraint of this fence instruction.
Definition: Instructions.h:443
This class represents a freeze function that returns random concrete value if an operand is either a ...
static bool classof(const Value *V)
FreezeInst * cloneImpl() const
Clone an identical FreezeInst.
static bool classof(const Instruction *I)
friend class CatchPadInst
Definition: InstrTypes.h:2430
A handy container for a FunctionType+Callee-pointer pair, which can be passed around as a single enti...
Definition: DerivedTypes.h:168
Class to represent function types.
Definition: DerivedTypes.h:103
Represents flags for the getelementptr instruction/expression.
static GEPNoWrapFlags inBounds()
an instruction for type-safe pointer arithmetic to access elements of arrays and structs
Definition: Instructions.h:915
bool isInBounds() const
Determine whether the GEP has the inbounds flag.
bool hasNoUnsignedSignedWrap() const
Determine whether the GEP has the nusw flag.
static Type * getTypeAtIndex(Type *Ty, Value *Idx)
Return the type of the element at the given index of an indexable type.
bool hasAllZeroIndices() const
Return true if all of the indices of this GEP are zeros.
static Type * getGEPReturnType(Value *Ptr, ArrayRef< Value * > IdxList)
Returns the pointer type returned by the GEP instruction, which may be a vector of pointers.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
void setResultElementType(Type *Ty)
Definition: Instructions.h:974
bool hasNoUnsignedWrap() const
Determine whether the GEP has the nuw flag.
bool hasAllConstantIndices() const
Return true if all of the indices of this GEP are constant integers.
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
Definition: Instructions.h:981
iterator_range< const_op_iterator > indices() const
bool collectOffset(const DataLayout &DL, unsigned BitWidth, MapVector< Value *, APInt > &VariableOffsets, APInt &ConstantOffset) const
Type * getResultElementType() const
Definition: Instructions.h:976
static bool classof(const Instruction *I)
static bool classof(const Value *V)
iterator_range< op_iterator > indices()
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Definition: Instructions.h:938
void setIsInBounds(bool b=true)
Set or clear the inbounds flag on this GEP instruction.
void setSourceElementType(Type *Ty)
Definition: Instructions.h:973
static Type * getIndexedType(Type *Ty, ArrayRef< Value * > IdxList)
Returns the result type of a getelementptr with the given source element type and indexes.
Type * getSourceElementType() const
Definition: Instructions.h:971
static GetElementPtrInst * CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Create an "inbounds" getelementptr.
Definition: Instructions.h:961
Type * getPointerOperandType() const
Method to return the pointer operand as a PointerType.
static GetElementPtrInst * Create(Type *PointeeType, Value *Ptr, ArrayRef< Value * > IdxList, GEPNoWrapFlags NW, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Definition: Instructions.h:948
static unsigned getPointerOperandIndex()
bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const
Accumulate the constant address offset of this GEP if possible.
const_op_iterator idx_begin() const
GetElementPtrInst * cloneImpl() const
void setNoWrapFlags(GEPNoWrapFlags NW)
Set nowrap flags for GEP instruction.
unsigned getNumIndices() const
GEPNoWrapFlags getNoWrapFlags() const
Get the nowrap flags for the GEP instruction.
const_op_iterator idx_end() const
const Value * getPointerOperand() const
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
This instruction compares its operands according to the predicate given to the constructor.
static bool classof(const Value *V)
ICmpInst(InsertPosition InsertBefore, Predicate pred, Value *LHS, Value *RHS, const Twine &NameStr="")
Constructor with insertion semantics.
bool isCommutative() const
static bool isGE(Predicate P)
Return true if the predicate is SGE or UGE.
static bool isLT(Predicate P)
Return true if the predicate is SLT or ULT.
static bool isGT(Predicate P)
Return true if the predicate is SGT or UGT.
static bool classof(const Instruction *I)
Predicate getSignedPredicate() const
For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
bool isEquality() const
Return true if this predicate is either EQ or NE.
static bool isEquality(Predicate P)
Return true if this predicate is either EQ or NE.
static bool isRelational(Predicate P)
Return true if the predicate is relational (not EQ or NE).
void swapOperands()
Exchange the two operands to this instruction in such a way that it does not modify the semantics of ...
static auto predicates()
Returns the sequence of all ICmp predicates.
ICmpInst(Predicate pred, Value *LHS, Value *RHS, const Twine &NameStr="")
Constructor with no-insertion semantics.
bool isRelational() const
Return true if the predicate is relational (not EQ or NE).
Predicate getUnsignedPredicate() const
For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
static bool isLE(Predicate P)
Return true if the predicate is SLE or ULE.
Indirect Branch Instruction.
static IndirectBrInst * Create(Value *Address, unsigned NumDests, InsertPosition InsertBefore=nullptr)
BasicBlock * getDestination(unsigned i)
Return the specified destination.
static bool classof(const Value *V)
const Value * getAddress() const
static bool classof(const Instruction *I)
BasicBlock * getSuccessor(unsigned i) const
iterator_range< const_succ_op_iterator > successors() const
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
unsigned getNumDestinations() const
return the number of possible destinations in this indirectbr instruction.
const BasicBlock * getDestination(unsigned i) const
void setSuccessor(unsigned i, BasicBlock *NewSucc)
void setAddress(Value *V)
unsigned getNumSuccessors() const
iterator_range< succ_op_iterator > successors()
This instruction inserts a single (scalar) element into a VectorType value.
static bool classof(const Value *V)
static InsertElementInst * Create(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
VectorType * getType() const
Overload to return most specific vector type.
static bool classof(const Instruction *I)
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
This instruction inserts a struct field of array element value into an aggregate value.
Value * getInsertedValueOperand()
static bool classof(const Instruction *I)
static unsigned getAggregateOperandIndex()
Value * getAggregateOperand()
static bool classof(const Value *V)
unsigned getNumIndices() const
ArrayRef< unsigned > getIndices() const
iterator_range< idx_iterator > indices() const
static unsigned getInsertedValueOperandIndex()
InsertValueInst * cloneImpl() const
idx_iterator idx_end() const
static InsertValueInst * Create(Value *Agg, Value *Val, ArrayRef< unsigned > Idxs, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
const Value * getAggregateOperand() const
bool hasIndices() const
const Value * getInsertedValueOperand() const
idx_iterator idx_begin() const
typename Bitfield::Element< AtomicOrdering, Offset, 3, AtomicOrdering::LAST > AtomicOrderingBitfieldElementT
Definition: Instruction.h:153
typename Bitfield::Element< bool, Offset, 1 > BoolBitfieldElementT
Definition: Instruction.h:148
bool isAtomic() const LLVM_READONLY
Return true if this instruction has an AtomicOrdering of unordered or higher.
typename Bitfield::Element< unsigned, Offset, 6, Value::MaxAlignmentExponent > AlignmentBitfieldElementT
Definition: Instruction.h:145
unsigned getOpcode() const
Returns a member of one of the enums like Instruction::Add.
Definition: Instruction.h:274
void copyMetadata(const Instruction &SrcInst, ArrayRef< unsigned > WL=ArrayRef< unsigned >())
Copy metadata from SrcInst to this instruction.
friend class BasicBlock
Various leaf nodes.
Definition: Instruction.h:997
This class represents a cast from an integer to a pointer.
static bool classof(const Instruction *I)
IntToPtrInst * cloneImpl() const
Clone an identical IntToPtrInst.
unsigned getAddressSpace() const
Returns the address space of this instruction's pointer type.
static bool classof(const Value *V)
Invoke instruction.
static bool classof(const Instruction *I)
BasicBlock * getUnwindDest() const
void setNormalDest(BasicBlock *B)
static bool classof(const Value *V)
static InvokeInst * Create(FunctionCallee Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
void setSuccessor(unsigned i, BasicBlock *NewSucc)
BasicBlock * getSuccessor(unsigned i) const
void setUnwindDest(BasicBlock *B)
BasicBlock * getNormalDest() const
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=std::nullopt, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
static InvokeInst * Create(FunctionCallee Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, ArrayRef< OperandBundleDef > Bundles=std::nullopt, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
unsigned getNumSuccessors() const
static InvokeInst * Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal, BasicBlock *IfException, ArrayRef< Value * > Args, const Twine &NameStr, InsertPosition InsertBefore=nullptr)
This is an important class for using LLVM in a threaded context.
Definition: LLVMContext.h:67
The landingpad instruction holds all of the information necessary to generate correct exception handl...
bool isCleanup() const
Return 'true' if this landingpad instruction is a cleanup.
unsigned getNumClauses() const
Get the number of clauses for this landing pad.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
bool isCatch(unsigned Idx) const
Return 'true' if the clause and index Idx is a catch clause.
bool isFilter(unsigned Idx) const
Return 'true' if the clause and index Idx is a filter clause.
Constant * getClause(unsigned Idx) const
Get the value of the clause at index Idx.
static bool classof(const Value *V)
void setCleanup(bool V)
Indicate that this landingpad instruction is a cleanup.
void reserveClauses(unsigned Size)
Grow the size of the operand list to accommodate the new number of clauses.
static bool classof(const Instruction *I)
An instruction for reading from memory.
Definition: Instructions.h:174
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:259
const Value * getPointerOperand() const
Definition: Instructions.h:254
void setAlignment(Align Align)
Definition: Instructions.h:213
Value * getPointerOperand()
Definition: Instructions.h:253
bool isVolatile() const
Return true if this is a load from a volatile memory location.
Definition: Instructions.h:203
static bool classof(const Instruction *I)
Definition: Instructions.h:264
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this load instruction.
Definition: Instructions.h:223
static bool classof(const Value *V)
Definition: Instructions.h:267
void setSyncScopeID(SyncScope::ID SSID)
Sets the synchronization scope ID of this load instruction.
Definition: Instructions.h:233
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this load instruction.
Definition: Instructions.h:239
LoadInst * cloneImpl() const
AtomicOrdering getOrdering() const
Returns the ordering constraint of this load instruction.
Definition: Instructions.h:218
Type * getPointerOperandType() const
Definition: Instructions.h:256
static unsigned getPointerOperandIndex()
Definition: Instructions.h:255
bool isUnordered() const
Definition: Instructions.h:247
void setVolatile(bool V)
Specify whether this is a volatile load or not.
Definition: Instructions.h:206
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this load instruction.
Definition: Instructions.h:228
bool isSimple() const
Definition: Instructions.h:245
Align getAlign() const
Return the alignment of the access that is being performed.
Definition: Instructions.h:209
Metadata node.
Definition: Metadata.h:1069
This class implements a map that also provides access to all stored values in a deterministic order.
Definition: MapVector.h:36
MutableArrayRef - Represent a mutable reference to an array (0 or more elements consecutively in memo...
Definition: ArrayRef.h:307
BasicBlock * getIncomingBlock(Value::const_user_iterator I) const
Return incoming basic block corresponding to value use iterator.
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
void addIncoming(Value *V, BasicBlock *BB)
Add an incoming value to the end of the PHI list.
bool isComplete() const
If the PHI node is complete which means all of its parent's predecessors have incoming value in this ...
iterator_range< const_block_iterator > blocks() const
op_range incoming_values()
static bool classof(const Value *V)
void allocHungoffUses(unsigned N)
const_block_iterator block_begin() const
void setIncomingValueForBlock(const BasicBlock *BB, Value *V)
Set every incoming value(s) for block BB to V.
void setIncomingBlock(unsigned i, BasicBlock *BB)
BasicBlock *const * const_block_iterator
void setIncomingValue(unsigned i, Value *V)
static unsigned getOperandNumForIncomingValue(unsigned i)
void copyIncomingBlocks(iterator_range< const_block_iterator > BBRange, uint32_t ToIdx=0)
Copies the basic blocks from BBRange to the incoming basic block list of this PHINode,...
const_block_iterator block_end() const
Value * getIncomingValueForBlock(const BasicBlock *BB) const
BasicBlock * getIncomingBlock(unsigned i) const
Return incoming basic block number i.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
Value * getIncomingValue(unsigned i) const
Return incoming value number x.
static unsigned getIncomingValueNumForOperand(unsigned i)
const_op_range incoming_values() const
Value * removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true)
void replaceIncomingBlockWith(const BasicBlock *Old, BasicBlock *New)
Replace every incoming basic block Old to basic block New.
BasicBlock * getIncomingBlock(const Use &U) const
Return incoming basic block corresponding to an operand of the PHI.
int getBasicBlockIndex(const BasicBlock *BB) const
Return the first index of the specified basic block in the value list for this PHI.
unsigned getNumIncomingValues() const
Return the number of incoming edges.
static PHINode * Create(Type *Ty, unsigned NumReservedValues, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
Constructors - NumReservedValues is a hint for the number of incoming edges that this phi node will h...
Class to represent pointers.
Definition: DerivedTypes.h:646
unsigned getAddressSpace() const
Return the address space of the Pointer type.
Definition: DerivedTypes.h:679
This class represents a cast from a pointer to an integer.
Value * getPointerOperand()
Gets the pointer operand.
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
static bool classof(const Value *V)
const Value * getPointerOperand() const
Gets the pointer operand.
static unsigned getPointerOperandIndex()
Gets the operand index of the pointer operand.
static bool classof(const Instruction *I)
PtrToIntInst * cloneImpl() const
Clone an identical PtrToIntInst.
Resume the propagation of an exception.
static ResumeInst * Create(Value *Exn, InsertPosition InsertBefore=nullptr)
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
Value * getValue() const
Convenience accessor.
static bool classof(const Value *V)
unsigned getNumSuccessors() const
ResumeInst * cloneImpl() const
static bool classof(const Instruction *I)
Return a value (possibly void), from a function.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
unsigned getNumSuccessors() const
static bool classof(const Value *V)
static bool classof(const Instruction *I)
static ReturnInst * Create(LLVMContext &C, BasicBlock *InsertAtEnd)
Value * getReturnValue() const
Convenience accessor. Returns null if there is no return value.
static ReturnInst * Create(LLVMContext &C, Value *retVal=nullptr, InsertPosition InsertBefore=nullptr)
This class represents a sign extension of integer types.
static bool classof(const Value *V)
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
SExtInst * cloneImpl() const
Clone an identical SExtInst.
This class represents a cast from signed integer to floating point.
SIToFPInst * cloneImpl() const
Clone an identical SIToFPInst.
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V)
This class represents the LLVM 'select' instruction.
static SelectInst * Create(Value *C, Value *S1, Value *S2, const Twine &NameStr="", InsertPosition InsertBefore=nullptr, Instruction *MDFrom=nullptr)
void setFalseValue(Value *V)
const Value * getFalseValue() const
void setTrueValue(Value *V)
OtherOps getOpcode() const
Value * getCondition()
Value * getTrueValue()
void swapValues()
Swap the true and false values of the select instruction.
Value * getFalseValue()
const Value * getCondition() const
SelectInst * cloneImpl() const
friend class Instruction
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
static const char * areInvalidOperands(Value *Cond, Value *True, Value *False)
Return a string if the specified operands are invalid for a select operation, otherwise return null.
static bool classof(const Value *V)
void setCondition(Value *V)
const Value * getTrueValue() const
static bool classof(const Instruction *I)
This instruction constructs a fixed permutation of two input vectors.
static bool classof(const Value *V)
static bool isInterleaveMask(ArrayRef< int > Mask, unsigned Factor, unsigned NumInputElts)
Constant * getShuffleMaskForBitcode() const
Return the mask for this instruction, for use in bitcode.
bool isSingleSource() const
Return true if this shuffle chooses elements from exactly one source vector without changing the leng...
bool changesLength() const
Return true if this shuffle returns a vector with a different number of elements than its source vect...
bool isExtractSubvectorMask(int &Index) const
Return true if this shuffle mask is an extract subvector mask.
ArrayRef< int > getShuffleMask() const
static bool isInsertSubvectorMask(const Constant *Mask, int NumSrcElts, int &NumSubElts, int &Index)
static bool isSingleSourceMask(const Constant *Mask, int NumSrcElts)
int getMaskValue(unsigned Elt) const
Return the shuffle mask value of this instruction for the given element index.
void getShuffleMask(SmallVectorImpl< int > &Result) const
Return the mask for this instruction as a vector of integers.
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
static bool isDeInterleaveMaskOfFactor(ArrayRef< int > Mask, unsigned Factor)
VectorType * getType() const
Overload to return most specific vector type.
bool isInsertSubvectorMask(int &NumSubElts, int &Index) const
Return true if this shuffle mask is an insert subvector mask.
bool increasesLength() const
Return true if this shuffle returns a vector with a greater number of elements than its source vector...
bool isZeroEltSplat() const
Return true if all elements of this shuffle are the same value as the first element of exactly one so...
static bool isExtractSubvectorMask(const Constant *Mask, int NumSrcElts, int &Index)
bool isSelect() const
Return true if this shuffle chooses elements from its source vectors without lane crossings and all o...
static bool isSpliceMask(const Constant *Mask, int NumSrcElts, int &Index)
bool isTranspose() const
Return true if this shuffle transposes the elements of its inputs without changing the length of the ...
static void commuteShuffleMask(MutableArrayRef< int > Mask, unsigned InVecNumElts)
Change values in a shuffle permute mask assuming the two vector operands of length InVecNumElts have ...
bool isSplice(int &Index) const
Return true if this shuffle splices two inputs without changing the length of the vectors.
static bool isReverseMask(const Constant *Mask, int NumSrcElts)
static bool isSelectMask(const Constant *Mask, int NumSrcElts)
static bool classof(const Instruction *I)
static bool isZeroEltSplatMask(const Constant *Mask, int NumSrcElts)
bool isIdentity() const
Return true if this shuffle chooses elements from exactly one source vector without lane crossings an...
static bool isReplicationMask(const Constant *Mask, int &ReplicationFactor, int &VF)
static bool isIdentityMask(const Constant *Mask, int NumSrcElts)
static bool isTransposeMask(const Constant *Mask, int NumSrcElts)
bool isReverse() const
Return true if this shuffle swaps the order of elements from exactly one source vector.
size_t size() const
Definition: SmallVector.h:91
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: SmallVector.h:586
void push_back(const T &Elt)
Definition: SmallVector.h:426
This is a 'vector' (really, a variable-sized array), optimized for the case when the array is small.
Definition: SmallVector.h:1209
An instruction for storing to memory.
Definition: Instructions.h:290
static bool classof(const Instruction *I)
Definition: Instructions.h:388
AtomicOrdering getOrdering() const
Returns the ordering constraint of this store instruction.
Definition: Instructions.h:338
const Value * getPointerOperand() const
Definition: Instructions.h:378
Align getAlign() const
Definition: Instructions.h:329
Type * getPointerOperandType() const
Definition: Instructions.h:380
void setVolatile(bool V)
Specify whether this is a volatile store or not.
Definition: Instructions.h:324
void setAlignment(Align Align)
Definition: Instructions.h:333
bool isSimple() const
Definition: Instructions.h:366
const Value * getValueOperand() const
Definition: Instructions.h:375
void setOrdering(AtomicOrdering Ordering)
Sets the ordering constraint of this store instruction.
Definition: Instructions.h:344
Value * getValueOperand()
Definition: Instructions.h:374
static bool classof(const Value *V)
Definition: Instructions.h:391
bool isUnordered() const
Definition: Instructions.h:368
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Transparently provide more efficient getOperand methods.
void setSyncScopeID(SyncScope::ID SSID)
Sets the synchronization scope ID of this store instruction.
Definition: Instructions.h:354
StoreInst * cloneImpl() const
unsigned getPointerAddressSpace() const
Returns the address space of the pointer operand.
Definition: Instructions.h:383
static unsigned getPointerOperandIndex()
Definition: Instructions.h:379
SyncScope::ID getSyncScopeID() const
Returns the synchronization scope ID of this store instruction.
Definition: Instructions.h:349
bool isVolatile() const
Return true if this is a store to a volatile memory location.
Definition: Instructions.h:321
Value * getPointerOperand()
Definition: Instructions.h:377
void setAtomic(AtomicOrdering Ordering, SyncScope::ID SSID=SyncScope::System)
Sets the ordering constraint and the synchronization scope ID of this store instruction.
Definition: Instructions.h:360
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:51
A wrapper class to simplify modification of SwitchInst cases along with their prof branch_weights met...
void setSuccessorWeight(unsigned idx, CaseWeightOpt W)
Instruction::InstListType::iterator eraseFromParent()
Delegate the call to the underlying SwitchInst::eraseFromParent() and mark this object to not touch t...
void addCase(ConstantInt *OnVal, BasicBlock *Dest, CaseWeightOpt W)
Delegate the call to the underlying SwitchInst::addCase() and set the specified branch weight for the...
SwitchInstProfUpdateWrapper(SwitchInst &SI)
CaseWeightOpt getSuccessorWeight(unsigned idx)
std::optional< uint32_t > CaseWeightOpt
SwitchInst::CaseIt removeCase(SwitchInst::CaseIt I)
Delegate the call to the underlying SwitchInst::removeCase() and remove correspondent branch weight.
A handle to a particular switch case.
unsigned getCaseIndex() const
Returns number of current case.
unsigned getSuccessorIndex() const
Returns successor index for current case successor.
BasicBlockT * getCaseSuccessor() const
Resolves successor for current case.
CaseHandleImpl(SwitchInstT *SI, ptrdiff_t Index)
bool operator==(const CaseHandleImpl &RHS) const
ConstantIntT * getCaseValue() const
Resolves case value for current case.
CaseHandle(SwitchInst *SI, ptrdiff_t Index)
void setValue(ConstantInt *V) const
Sets the new value for current case.
void setSuccessor(BasicBlock *S) const
Sets the new successor for current case.
const CaseHandleT & operator*() const
CaseIteratorImpl()=default
Default constructed iterator is in an invalid state until assigned to a case for a particular switch.
CaseIteratorImpl & operator-=(ptrdiff_t N)
bool operator==(const CaseIteratorImpl &RHS) const
CaseIteratorImpl & operator+=(ptrdiff_t N)
ptrdiff_t operator-(const CaseIteratorImpl &RHS) const
bool operator<(const CaseIteratorImpl &RHS) const
CaseIteratorImpl(SwitchInstT *SI, unsigned CaseNum)
Initializes case iterator for given SwitchInst and for given case number.
static CaseIteratorImpl fromSuccessorIndex(SwitchInstT *SI, unsigned SuccessorIndex)
Initializes case iterator for given SwitchInst and for given successor index.
Multiway switch.
BasicBlock * getDefaultDest() const
CaseIt case_end()
Returns a read/write iterator that points one past the last in the SwitchInst.
BasicBlock * getSuccessor(unsigned idx) const
ConstCaseIt findCaseValue(const ConstantInt *C) const
DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value)
Provide fast operand accessors.
static SwitchInst * Create(Value *Value, BasicBlock *Default, unsigned NumCases, InsertPosition InsertBefore=nullptr)
void setCondition(Value *V)
ConstCaseIt case_begin() const
Returns a read-only iterator that points to the first case in the SwitchInst.
bool defaultDestUndefined() const
Returns true if the default branch must result in immediate undefined behavior, false otherwise.
iterator_range< ConstCaseIt > cases() const
Constant iteration adapter for range-for loops.
ConstantInt * findCaseDest(BasicBlock *BB)
Finds the unique case value for a given successor.
void setSuccessor(unsigned idx, BasicBlock *NewSucc)
static bool classof(const Value *V)
unsigned getNumSuccessors() const
CaseIt case_default()
Returns an iterator that points to the default case.
void setDefaultDest(BasicBlock *DefaultCase)
unsigned getNumCases() const
Return the number of 'cases' in this switch instruction, excluding the default case.
CaseIt findCaseValue(const ConstantInt *C)
Search all of the case values for the specified constant.
Value * getCondition() const
ConstCaseIt case_default() const
CaseIt case_begin()
Returns a read/write iterator that points to the first case in the SwitchInst.
static bool classof(const Instruction *I)
iterator_range< CaseIt > cases()
Iteration adapter for range-for loops.
ConstCaseIt case_end() const
Returns a read-only iterator that points one past the last in the SwitchInst.
This class represents a truncation of integer types.
void setHasNoSignedWrap(bool B)
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
TruncInst * cloneImpl() const
Clone an identical TruncInst.
void setHasNoUnsignedWrap(bool B)
unsigned getNoWrapKind() const
Returns the no-wrap kind of the operation.
bool hasNoSignedWrap() const
Test whether this operation is known to never undergo signed overflow, aka the nsw property.
static bool classof(const Value *V)
bool hasNoUnsignedWrap() const
Test whether this operation is known to never undergo unsigned overflow, aka the nuw property.
Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...
Definition: Twine.h:81
The instances of the Type class are immutable: once they are created, they are never changed.
Definition: Type.h:45
bool isVectorTy() const
True if this is an instance of VectorType.
Definition: Type.h:261
unsigned getPointerAddressSpace() const
Get the address space of this pointer or pointer vector type.
bool isTokenTy() const
Return true if this is 'token'.
Definition: Type.h:221
This class represents a cast unsigned integer to floating point.
static bool classof(const Value *V)
UIToFPInst * cloneImpl() const
Clone an identical UIToFPInst.
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
This function has undefined behavior.
unsigned getNumSuccessors() const
static bool classof(const Value *V)
static bool classof(const Instruction *I)
A Use represents the edge between a Value definition and its users.
Definition: Use.h:43
void allocHungoffUses(unsigned N, bool IsPhi=false)
Allocate the array of Uses, followed by a pointer (with bottom bit set) to the User.
Definition: User.cpp:50
op_iterator op_begin()
Definition: User.h:234
const Use & getOperandUse(unsigned i) const
Definition: User.h:182
Value * getOperand(unsigned i) const
Definition: User.h:169
unsigned getNumOperands() const
Definition: User.h:191
op_iterator op_end()
Definition: User.h:236
This class represents the va_arg llvm instruction, which returns an argument of the specified type gi...
static bool classof(const Instruction *I)
Value * getPointerOperand()
VAArgInst(Value *List, Type *Ty, const Twine &NameStr="", InsertPosition InsertBefore=nullptr)
const Value * getPointerOperand() const
static bool classof(const Value *V)
static unsigned getPointerOperandIndex()
LLVM Value Representation.
Definition: Value.h:74
Type * getType() const
All values are typed, get the type of this value.
Definition: Value.h:255
user_iterator_impl< const User > const_user_iterator
Definition: Value.h:391
unsigned char SubclassOptionalData
Hold subclass data that can be dropped.
Definition: Value.h:84
void setName(const Twine &Name)
Change the name of the value.
Definition: Value.cpp:377
Base class of all SIMD vector types.
Definition: DerivedTypes.h:403
static VectorType * get(Type *ElementType, ElementCount EC)
This static method is the primary way to construct an VectorType.
Definition: Type.cpp:664
This class represents zero extension of integer types.
static bool classof(const Instruction *I)
Methods for support type inquiry through isa, cast, and dyn_cast:
static bool classof(const Value *V)
ZExtInst * cloneImpl() const
Clone an identical ZExtInst.
An efficient, type-erasing, non-owning reference to a callable.
base_list_type::iterator iterator
Definition: ilist.h:121
CRTP base class for adapting an iterator to a different type.
Definition: iterator.h:237
CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...
Definition: iterator.h:80
A range adaptor for a pair of iterators.
This provides a very simple, boring adaptor for a begin and end iterator into a range type.
#define llvm_unreachable(msg)
Marks that the current location is not supposed to be reachable.
constexpr char Args[]
Key for Kernel::Metadata::mArgs.
@ C
The default llvm calling convention, compatible with C.
Definition: CallingConv.h:34
@ BasicBlock
Various leaf nodes.
Definition: ISDOpcodes.h:71
@ System
Synchronized with respect to all concurrently executing threads.
Definition: LLVMContext.h:57
This is an optimization pass for GlobalISel generic memory operations.
Definition: AddressRanges.h:18
@ Offset
Definition: DWP.cpp:480
Type * checkGEPType(Type *Ty)
Definition: Instructions.h:907
bool all_of(R &&range, UnaryPredicate P)
Provide wrappers to std::all_of which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1722
unsigned getLoadStoreAddressSpace(const Value *I)
A helper function that returns the address space of the pointer operand of load or store instruction.
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:1680
APInt operator*(APInt a, uint64_t RHS)
Definition: APInt.h:2174
const Value * getLoadStorePointerOperand(const Value *V)
A helper function that returns the pointer operand of a load or store instruction.
iterator_range< T > make_range(T x, T y)
Convenience function for iterating over sub-ranges.
void setAtomicSyncScopeID(Instruction *I, SyncScope::ID SSID)
A helper function that sets an atomic operation's sync scope.
Align getLoadStoreAlignment(const Value *I)
A helper function that returns the alignment of load or store instruction.
const Value * getPointerOperand(const Value *V)
A helper function that returns the pointer operand of a load, store or GEP instruction.
decltype(auto) get(const PointerIntPair< PointerTy, IntBits, IntType, PtrTraits, Info > &Pair)
std::optional< SyncScope::ID > getAtomicSyncScopeID(const Instruction *I)
A helper function that returns an atomic operation's sync scope; returns std::nullopt if it is not an...
constexpr int PoisonMaskElem
AtomicOrdering
Atomic ordering for LLVM's memory model.
DWARFExpression::Operation Op
OutputIt copy(R &&Range, OutputIt Out)
Definition: STLExtras.h:1824
constexpr unsigned BitWidth
Definition: BitmaskEnum.h:191
auto find_if(R &&Range, UnaryPredicate P)
Provide wrappers to std::find_if which take ranges instead of having to pass begin/end explicitly.
Definition: STLExtras.h:1749
auto predecessors(const MachineBasicBlock *BB)
Type * getLoadStoreType(const Value *I)
A helper function that returns the type of a load or store instruction.
unsigned Log2(Align A)
Returns the log2 of the alignment.
Definition: Alignment.h:208
@ Default
The result values are uniform if and only if all operands are uniform.
#define N
This struct is a compact representation of a valid (non-zero power of two) alignment.
Definition: Alignment.h:39
Describes an element of a Bitfield.
Definition: Bitfields.h:223
static constexpr bool areContiguous()
Definition: Bitfields.h:280
The const version of succ_op_iterator.
const BasicBlock * operator->() const
const_succ_op_iterator(const_value_op_iterator I)
const BasicBlock * operator*() const
Iterator type that casts an operand to a basic block.
succ_op_iterator(value_op_iterator I)
Used to keep track of an operand bundle.
Definition: InstrTypes.h:2228
FixedNumOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
Definition: OperandTraits.h:30
HungoffOperandTraits - determine the allocation regime of the Use array when it is not a prefix to th...
Definition: OperandTraits.h:95
The const version of succ_op_iterator.
const_succ_op_iterator(const_value_op_iterator I)
Iterator type that casts an operand to a basic block.
Compile-time customization of User operands.
Definition: User.h:42
Iterator for directly iterating over the operand Values.
Definition: User.h:253
VariadicOperandTraits - determine the allocation regime of the Use array when it is a prefix to the U...
Definition: OperandTraits.h:68