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