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