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