58d5221dea89abbaf4ec7400bc0019ca44660e9e
[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   /// CreateMalloc - Generate the IR for a call to malloc:
1463   /// 1. Compute the malloc call's argument as the specified type's size,
1464   ///    possibly multiplied by the array size if the array size is not
1465   ///    constant 1.
1466   /// 2. Call malloc with that argument.
1467   /// 3. Bitcast the result of the malloc call to the specified type.
1468   static Instruction *CreateMalloc(Instruction *InsertBefore,
1469                                    Type *IntPtrTy, Type *AllocTy,
1470                                    Value *AllocSize, Value *ArraySize = nullptr,
1471                                    Function* MallocF = nullptr,
1472                                    const Twine &Name = "");
1473   static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1474                                    Type *IntPtrTy, Type *AllocTy,
1475                                    Value *AllocSize, Value *ArraySize = nullptr,
1476                                    Function* MallocF = nullptr,
1477                                    const Twine &Name = "");
1478   /// CreateFree - Generate the IR for a call to the builtin free function.
1479   static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1480   static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1481
1482   ~CallInst() override;
1483
1484   FunctionType *getFunctionType() const { return FTy; }
1485
1486   void mutateFunctionType(FunctionType *FTy) {
1487     mutateType(FTy->getReturnType());
1488     this->FTy = FTy;
1489   }
1490
1491   // Note that 'musttail' implies 'tail'.
1492   enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1493   TailCallKind getTailCallKind() const {
1494     return TailCallKind(getSubclassDataFromInstruction() & 3);
1495   }
1496   bool isTailCall() const {
1497     return (getSubclassDataFromInstruction() & 3) != TCK_None;
1498   }
1499   bool isMustTailCall() const {
1500     return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1501   }
1502   void setTailCall(bool isTC = true) {
1503     setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1504                                unsigned(isTC ? TCK_Tail : TCK_None));
1505   }
1506   void setTailCallKind(TailCallKind TCK) {
1507     setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1508                                unsigned(TCK));
1509   }
1510
1511   /// Provide fast operand accessors
1512   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1513
1514   /// getNumArgOperands - Return the number of call arguments.
1515   ///
1516   unsigned getNumArgOperands() const {
1517     return getNumOperands() - getNumTotalBundleOperands() - 1;
1518   }
1519
1520   /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1521   ///
1522   Value *getArgOperand(unsigned i) const {
1523     assert(i < getNumArgOperands() && "Out of bounds!");
1524     return getOperand(i);
1525   }
1526   void setArgOperand(unsigned i, Value *v) {
1527     assert(i < getNumArgOperands() && "Out of bounds!");
1528     setOperand(i, v);
1529   }
1530
1531   /// arg_operands - iteration adapter for range-for loops.
1532   iterator_range<op_iterator> arg_operands() {
1533     // The last operand in the op list is the callee - it's not one of the args
1534     // so we don't want to iterate over it.
1535     return iterator_range<op_iterator>(
1536         op_begin(), op_end() - getNumTotalBundleOperands() - 1);
1537   }
1538
1539   /// arg_operands - iteration adapter for range-for loops.
1540   iterator_range<const_op_iterator> arg_operands() const {
1541     return iterator_range<const_op_iterator>(
1542         op_begin(), op_end() - getNumTotalBundleOperands() - 1);
1543   }
1544
1545   /// \brief Wrappers for getting the \c Use of a call argument.
1546   const Use &getArgOperandUse(unsigned i) const {
1547     assert(i < getNumArgOperands() && "Out of bounds!");
1548     return getOperandUse(i);
1549   }
1550   Use &getArgOperandUse(unsigned i) {
1551     assert(i < getNumArgOperands() && "Out of bounds!");
1552     return getOperandUse(i);
1553   }
1554
1555   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1556   /// function call.
1557   CallingConv::ID getCallingConv() const {
1558     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1559   }
1560   void setCallingConv(CallingConv::ID CC) {
1561     setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1562                                (static_cast<unsigned>(CC) << 2));
1563   }
1564
1565   /// getAttributes - Return the parameter attributes for this call.
1566   ///
1567   const AttributeSet &getAttributes() const { return AttributeList; }
1568
1569   /// setAttributes - Set the parameter attributes for this call.
1570   ///
1571   void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1572
1573   /// addAttribute - adds the attribute to the list of attributes.
1574   void addAttribute(unsigned i, Attribute::AttrKind attr);
1575
1576   /// addAttribute - adds the attribute to the list of attributes.
1577   void addAttribute(unsigned i, StringRef Kind, StringRef Value);
1578
1579   /// removeAttribute - removes the attribute from the list of attributes.
1580   void removeAttribute(unsigned i, Attribute attr);
1581
1582   /// \brief adds the dereferenceable attribute to the list of attributes.
1583   void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1584
1585   /// \brief adds the dereferenceable_or_null attribute to the list of
1586   /// attributes.
1587   void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
1588
1589   /// \brief Determine whether this call has the given attribute.
1590   bool hasFnAttr(Attribute::AttrKind A) const {
1591     assert(A != Attribute::NoBuiltin &&
1592            "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1593     return hasFnAttrImpl(A);
1594   }
1595
1596   /// \brief Determine whether this call has the given attribute.
1597   bool hasFnAttr(StringRef A) const {
1598     return hasFnAttrImpl(A);
1599   }
1600
1601   /// \brief Determine whether the call or the callee has the given attributes.
1602   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1603
1604   /// \brief Extract the alignment for a call or parameter (0=unknown).
1605   unsigned getParamAlignment(unsigned i) const {
1606     return AttributeList.getParamAlignment(i);
1607   }
1608
1609   /// \brief Extract the number of dereferenceable bytes for a call or
1610   /// parameter (0=unknown).
1611   uint64_t getDereferenceableBytes(unsigned i) const {
1612     return AttributeList.getDereferenceableBytes(i);
1613   }
1614
1615   /// \brief Extract the number of dereferenceable_or_null bytes for a call or
1616   /// parameter (0=unknown).
1617   uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1618     return AttributeList.getDereferenceableOrNullBytes(i);
1619   }
1620
1621   /// @brief Determine if the parameter or return value is marked with NoAlias
1622   /// attribute.
1623   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
1624   bool doesNotAlias(unsigned n) const {
1625     return AttributeList.hasAttribute(n, Attribute::NoAlias);
1626   }
1627
1628   /// \brief Return true if the call should not be treated as a call to a
1629   /// builtin.
1630   bool isNoBuiltin() const {
1631     return hasFnAttrImpl(Attribute::NoBuiltin) &&
1632       !hasFnAttrImpl(Attribute::Builtin);
1633   }
1634
1635   /// \brief Return true if the call should not be inlined.
1636   bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1637   void setIsNoInline() {
1638     addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1639   }
1640
1641   /// \brief Return true if the call can return twice
1642   bool canReturnTwice() const {
1643     return hasFnAttr(Attribute::ReturnsTwice);
1644   }
1645   void setCanReturnTwice() {
1646     addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1647   }
1648
1649   /// \brief Determine if the call does not access memory.
1650   bool doesNotAccessMemory() const {
1651     return hasFnAttr(Attribute::ReadNone);
1652   }
1653   void setDoesNotAccessMemory() {
1654     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1655   }
1656
1657   /// \brief Determine if the call does not access or only reads memory.
1658   bool onlyReadsMemory() const {
1659     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1660   }
1661   void setOnlyReadsMemory() {
1662     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1663   }
1664
1665   /// @brief Determine if the call can access memmory only using pointers based
1666   /// on its arguments.
1667   bool onlyAccessesArgMemory() const {
1668     return hasFnAttr(Attribute::ArgMemOnly);
1669   }
1670   void setOnlyAccessesArgMemory() {
1671     addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
1672   }
1673
1674   /// \brief Determine if the call cannot return.
1675   bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1676   void setDoesNotReturn() {
1677     addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1678   }
1679
1680   /// \brief Determine if the call cannot unwind.
1681   bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1682   void setDoesNotThrow() {
1683     addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1684   }
1685
1686   /// \brief Determine if the call cannot be duplicated.
1687   bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1688   void setCannotDuplicate() {
1689     addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1690   }
1691
1692   /// \brief Determine if the call is convergent
1693   bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1694   void setConvergent() {
1695     addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
1696   }
1697
1698   /// \brief Determine if the call returns a structure through first
1699   /// pointer argument.
1700   bool hasStructRetAttr() const {
1701     // Be friendly and also check the callee.
1702     return paramHasAttr(1, Attribute::StructRet);
1703   }
1704
1705   /// \brief Determine if any call argument is an aggregate passed by value.
1706   bool hasByValArgument() const {
1707     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1708   }
1709
1710   /// getCalledFunction - Return the function called, or null if this is an
1711   /// indirect function invocation.
1712   ///
1713   Function *getCalledFunction() const {
1714     return dyn_cast<Function>(Op<-1>());
1715   }
1716
1717   /// getCalledValue - Get a pointer to the function that is invoked by this
1718   /// instruction.
1719   const Value *getCalledValue() const { return Op<-1>(); }
1720         Value *getCalledValue()       { return Op<-1>(); }
1721
1722   /// setCalledFunction - Set the function called.
1723   void setCalledFunction(Value* Fn) {
1724     setCalledFunction(
1725         cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
1726         Fn);
1727   }
1728   void setCalledFunction(FunctionType *FTy, Value *Fn) {
1729     this->FTy = FTy;
1730     assert(FTy == cast<FunctionType>(
1731                       cast<PointerType>(Fn->getType())->getElementType()));
1732     Op<-1>() = Fn;
1733   }
1734
1735   /// isInlineAsm - Check if this call is an inline asm statement.
1736   bool isInlineAsm() const {
1737     return isa<InlineAsm>(Op<-1>());
1738   }
1739
1740   // Methods for support type inquiry through isa, cast, and dyn_cast:
1741   static inline bool classof(const Instruction *I) {
1742     return I->getOpcode() == Instruction::Call;
1743   }
1744   static inline bool classof(const Value *V) {
1745     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1746   }
1747
1748 private:
1749   template <typename AttrKind> bool hasFnAttrImpl(AttrKind A) const {
1750     if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
1751       return true;
1752
1753     // Operand bundles override attributes on the called function, but don't
1754     // override attributes directly present on the call instruction.
1755     if (isFnAttrDisallowedByOpBundle(A))
1756       return false;
1757
1758     if (const Function *F = getCalledFunction())
1759       return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
1760     return false;
1761   }
1762
1763   // Shadow Instruction::setInstructionSubclassData with a private forwarding
1764   // method so that subclasses cannot accidentally use it.
1765   void setInstructionSubclassData(unsigned short D) {
1766     Instruction::setInstructionSubclassData(D);
1767   }
1768 };
1769
1770 template <>
1771 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1772 };
1773
1774 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1775                    ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1776                    BasicBlock *InsertAtEnd)
1777     : Instruction(
1778           cast<FunctionType>(cast<PointerType>(Func->getType())
1779                                  ->getElementType())->getReturnType(),
1780           Instruction::Call, OperandTraits<CallInst>::op_end(this) -
1781                                  (Args.size() + CountBundleInputs(Bundles) + 1),
1782           unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) {
1783   init(Func, Args, Bundles, NameStr);
1784 }
1785
1786 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1787                    ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1788                    Instruction *InsertBefore)
1789     : Instruction(Ty->getReturnType(), Instruction::Call,
1790                   OperandTraits<CallInst>::op_end(this) -
1791                       (Args.size() + CountBundleInputs(Bundles) + 1),
1792                   unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1793                   InsertBefore) {
1794   init(Ty, Func, Args, Bundles, NameStr);
1795 }
1796
1797 // Note: if you get compile errors about private methods then
1798 //       please update your code to use the high-level operand
1799 //       interfaces. See line 943 above.
1800 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1801
1802 //===----------------------------------------------------------------------===//
1803 //                               SelectInst Class
1804 //===----------------------------------------------------------------------===//
1805
1806 /// SelectInst - This class represents the LLVM 'select' instruction.
1807 ///
1808 class SelectInst : public Instruction {
1809   void init(Value *C, Value *S1, Value *S2) {
1810     assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1811     Op<0>() = C;
1812     Op<1>() = S1;
1813     Op<2>() = S2;
1814   }
1815
1816   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1817              Instruction *InsertBefore)
1818     : Instruction(S1->getType(), Instruction::Select,
1819                   &Op<0>(), 3, InsertBefore) {
1820     init(C, S1, S2);
1821     setName(NameStr);
1822   }
1823   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1824              BasicBlock *InsertAtEnd)
1825     : Instruction(S1->getType(), Instruction::Select,
1826                   &Op<0>(), 3, InsertAtEnd) {
1827     init(C, S1, S2);
1828     setName(NameStr);
1829   }
1830
1831 protected:
1832   // Note: Instruction needs to be a friend here to call cloneImpl.
1833   friend class Instruction;
1834   SelectInst *cloneImpl() const;
1835
1836 public:
1837   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1838                             const Twine &NameStr = "",
1839                             Instruction *InsertBefore = nullptr) {
1840     return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1841   }
1842   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1843                             const Twine &NameStr,
1844                             BasicBlock *InsertAtEnd) {
1845     return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1846   }
1847
1848   const Value *getCondition() const { return Op<0>(); }
1849   const Value *getTrueValue() const { return Op<1>(); }
1850   const Value *getFalseValue() const { return Op<2>(); }
1851   Value *getCondition() { return Op<0>(); }
1852   Value *getTrueValue() { return Op<1>(); }
1853   Value *getFalseValue() { return Op<2>(); }
1854
1855   /// areInvalidOperands - Return a string if the specified operands are invalid
1856   /// for a select operation, otherwise return null.
1857   static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1858
1859   /// Transparently provide more efficient getOperand methods.
1860   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1861
1862   OtherOps getOpcode() const {
1863     return static_cast<OtherOps>(Instruction::getOpcode());
1864   }
1865
1866   // Methods for support type inquiry through isa, cast, and dyn_cast:
1867   static inline bool classof(const Instruction *I) {
1868     return I->getOpcode() == Instruction::Select;
1869   }
1870   static inline bool classof(const Value *V) {
1871     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1872   }
1873 };
1874
1875 template <>
1876 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1877 };
1878
1879 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1880
1881 //===----------------------------------------------------------------------===//
1882 //                                VAArgInst Class
1883 //===----------------------------------------------------------------------===//
1884
1885 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1886 /// an argument of the specified type given a va_list and increments that list
1887 ///
1888 class VAArgInst : public UnaryInstruction {
1889 protected:
1890   // Note: Instruction needs to be a friend here to call cloneImpl.
1891   friend class Instruction;
1892   VAArgInst *cloneImpl() const;
1893
1894 public:
1895   VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1896              Instruction *InsertBefore = nullptr)
1897     : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1898     setName(NameStr);
1899   }
1900   VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1901             BasicBlock *InsertAtEnd)
1902     : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1903     setName(NameStr);
1904   }
1905
1906   Value *getPointerOperand() { return getOperand(0); }
1907   const Value *getPointerOperand() const { return getOperand(0); }
1908   static unsigned getPointerOperandIndex() { return 0U; }
1909
1910   // Methods for support type inquiry through isa, cast, and dyn_cast:
1911   static inline bool classof(const Instruction *I) {
1912     return I->getOpcode() == VAArg;
1913   }
1914   static inline bool classof(const Value *V) {
1915     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1916   }
1917 };
1918
1919 //===----------------------------------------------------------------------===//
1920 //                                ExtractElementInst Class
1921 //===----------------------------------------------------------------------===//
1922
1923 /// ExtractElementInst - This instruction extracts a single (scalar)
1924 /// element from a VectorType value
1925 ///
1926 class ExtractElementInst : public Instruction {
1927   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1928                      Instruction *InsertBefore = nullptr);
1929   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1930                      BasicBlock *InsertAtEnd);
1931
1932 protected:
1933   // Note: Instruction needs to be a friend here to call cloneImpl.
1934   friend class Instruction;
1935   ExtractElementInst *cloneImpl() const;
1936
1937 public:
1938   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1939                                    const Twine &NameStr = "",
1940                                    Instruction *InsertBefore = nullptr) {
1941     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1942   }
1943   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1944                                    const Twine &NameStr,
1945                                    BasicBlock *InsertAtEnd) {
1946     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1947   }
1948
1949   /// isValidOperands - Return true if an extractelement instruction can be
1950   /// formed with the specified operands.
1951   static bool isValidOperands(const Value *Vec, const Value *Idx);
1952
1953   Value *getVectorOperand() { return Op<0>(); }
1954   Value *getIndexOperand() { return Op<1>(); }
1955   const Value *getVectorOperand() const { return Op<0>(); }
1956   const Value *getIndexOperand() const { return Op<1>(); }
1957
1958   VectorType *getVectorOperandType() const {
1959     return cast<VectorType>(getVectorOperand()->getType());
1960   }
1961
1962   /// Transparently provide more efficient getOperand methods.
1963   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1964
1965   // Methods for support type inquiry through isa, cast, and dyn_cast:
1966   static inline bool classof(const Instruction *I) {
1967     return I->getOpcode() == Instruction::ExtractElement;
1968   }
1969   static inline bool classof(const Value *V) {
1970     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1971   }
1972 };
1973
1974 template <>
1975 struct OperandTraits<ExtractElementInst> :
1976   public FixedNumOperandTraits<ExtractElementInst, 2> {
1977 };
1978
1979 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1980
1981 //===----------------------------------------------------------------------===//
1982 //                                InsertElementInst Class
1983 //===----------------------------------------------------------------------===//
1984
1985 /// InsertElementInst - This instruction inserts a single (scalar)
1986 /// element into a VectorType value
1987 ///
1988 class InsertElementInst : public Instruction {
1989   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1990                     const Twine &NameStr = "",
1991                     Instruction *InsertBefore = nullptr);
1992   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
1993                     BasicBlock *InsertAtEnd);
1994
1995 protected:
1996   // Note: Instruction needs to be a friend here to call cloneImpl.
1997   friend class Instruction;
1998   InsertElementInst *cloneImpl() const;
1999
2000 public:
2001   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2002                                    const Twine &NameStr = "",
2003                                    Instruction *InsertBefore = nullptr) {
2004     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
2005   }
2006   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2007                                    const Twine &NameStr,
2008                                    BasicBlock *InsertAtEnd) {
2009     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
2010   }
2011
2012   /// isValidOperands - Return true if an insertelement instruction can be
2013   /// formed with the specified operands.
2014   static bool isValidOperands(const Value *Vec, const Value *NewElt,
2015                               const Value *Idx);
2016
2017   /// getType - Overload to return most specific vector type.
2018   ///
2019   VectorType *getType() const {
2020     return cast<VectorType>(Instruction::getType());
2021   }
2022
2023   /// Transparently provide more efficient getOperand methods.
2024   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2025
2026   // Methods for support type inquiry through isa, cast, and dyn_cast:
2027   static inline bool classof(const Instruction *I) {
2028     return I->getOpcode() == Instruction::InsertElement;
2029   }
2030   static inline bool classof(const Value *V) {
2031     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2032   }
2033 };
2034
2035 template <>
2036 struct OperandTraits<InsertElementInst> :
2037   public FixedNumOperandTraits<InsertElementInst, 3> {
2038 };
2039
2040 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
2041
2042 //===----------------------------------------------------------------------===//
2043 //                           ShuffleVectorInst Class
2044 //===----------------------------------------------------------------------===//
2045
2046 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
2047 /// input vectors.
2048 ///
2049 class ShuffleVectorInst : public Instruction {
2050 protected:
2051   // Note: Instruction needs to be a friend here to call cloneImpl.
2052   friend class Instruction;
2053   ShuffleVectorInst *cloneImpl() const;
2054
2055 public:
2056   // allocate space for exactly three operands
2057   void *operator new(size_t s) {
2058     return User::operator new(s, 3);
2059   }
2060   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2061                     const Twine &NameStr = "",
2062                     Instruction *InsertBefor = nullptr);
2063   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2064                     const Twine &NameStr, BasicBlock *InsertAtEnd);
2065
2066   /// isValidOperands - Return true if a shufflevector instruction can be
2067   /// formed with the specified operands.
2068   static bool isValidOperands(const Value *V1, const Value *V2,
2069                               const Value *Mask);
2070
2071   /// getType - Overload to return most specific vector type.
2072   ///
2073   VectorType *getType() const {
2074     return cast<VectorType>(Instruction::getType());
2075   }
2076
2077   /// Transparently provide more efficient getOperand methods.
2078   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2079
2080   Constant *getMask() const {
2081     return cast<Constant>(getOperand(2));
2082   }
2083
2084   /// getMaskValue - Return the index from the shuffle mask for the specified
2085   /// output result.  This is either -1 if the element is undef or a number less
2086   /// than 2*numelements.
2087   static int getMaskValue(Constant *Mask, unsigned i);
2088
2089   int getMaskValue(unsigned i) const {
2090     return getMaskValue(getMask(), i);
2091   }
2092
2093   /// getShuffleMask - Return the full mask for this instruction, where each
2094   /// element is the element number and undef's are returned as -1.
2095   static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
2096
2097   void getShuffleMask(SmallVectorImpl<int> &Result) const {
2098     return getShuffleMask(getMask(), Result);
2099   }
2100
2101   SmallVector<int, 16> getShuffleMask() const {
2102     SmallVector<int, 16> Mask;
2103     getShuffleMask(Mask);
2104     return Mask;
2105   }
2106
2107   // Methods for support type inquiry through isa, cast, and dyn_cast:
2108   static inline bool classof(const Instruction *I) {
2109     return I->getOpcode() == Instruction::ShuffleVector;
2110   }
2111   static inline bool classof(const Value *V) {
2112     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2113   }
2114 };
2115
2116 template <>
2117 struct OperandTraits<ShuffleVectorInst> :
2118   public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2119 };
2120
2121 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
2122
2123 //===----------------------------------------------------------------------===//
2124 //                                ExtractValueInst Class
2125 //===----------------------------------------------------------------------===//
2126
2127 /// ExtractValueInst - This instruction extracts a struct member or array
2128 /// element value from an aggregate value.
2129 ///
2130 class ExtractValueInst : public UnaryInstruction {
2131   SmallVector<unsigned, 4> Indices;
2132
2133   ExtractValueInst(const ExtractValueInst &EVI);
2134   void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2135
2136   /// Constructors - Create a extractvalue instruction with a base aggregate
2137   /// value and a list of indices.  The first ctor can optionally insert before
2138   /// an existing instruction, the second appends the new instruction to the
2139   /// specified BasicBlock.
2140   inline ExtractValueInst(Value *Agg,
2141                           ArrayRef<unsigned> Idxs,
2142                           const Twine &NameStr,
2143                           Instruction *InsertBefore);
2144   inline ExtractValueInst(Value *Agg,
2145                           ArrayRef<unsigned> Idxs,
2146                           const Twine &NameStr, BasicBlock *InsertAtEnd);
2147
2148   // allocate space for exactly one operand
2149   void *operator new(size_t s) { return User::operator new(s, 1); }
2150
2151 protected:
2152   // Note: Instruction needs to be a friend here to call cloneImpl.
2153   friend class Instruction;
2154   ExtractValueInst *cloneImpl() const;
2155
2156 public:
2157   static ExtractValueInst *Create(Value *Agg,
2158                                   ArrayRef<unsigned> Idxs,
2159                                   const Twine &NameStr = "",
2160                                   Instruction *InsertBefore = nullptr) {
2161     return new
2162       ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2163   }
2164   static ExtractValueInst *Create(Value *Agg,
2165                                   ArrayRef<unsigned> Idxs,
2166                                   const Twine &NameStr,
2167                                   BasicBlock *InsertAtEnd) {
2168     return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2169   }
2170
2171   /// getIndexedType - Returns the type of the element that would be extracted
2172   /// with an extractvalue instruction with the specified parameters.
2173   ///
2174   /// Null is returned if the indices are invalid for the specified type.
2175   static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2176
2177   typedef const unsigned* idx_iterator;
2178   inline idx_iterator idx_begin() const { return Indices.begin(); }
2179   inline idx_iterator idx_end()   const { return Indices.end(); }
2180   inline iterator_range<idx_iterator> indices() const {
2181     return iterator_range<idx_iterator>(idx_begin(), idx_end());
2182   }
2183
2184   Value *getAggregateOperand() {
2185     return getOperand(0);
2186   }
2187   const Value *getAggregateOperand() const {
2188     return getOperand(0);
2189   }
2190   static unsigned getAggregateOperandIndex() {
2191     return 0U;                      // get index for modifying correct operand
2192   }
2193
2194   ArrayRef<unsigned> getIndices() const {
2195     return Indices;
2196   }
2197
2198   unsigned getNumIndices() const {
2199     return (unsigned)Indices.size();
2200   }
2201
2202   bool hasIndices() const {
2203     return true;
2204   }
2205
2206   // Methods for support type inquiry through isa, cast, and dyn_cast:
2207   static inline bool classof(const Instruction *I) {
2208     return I->getOpcode() == Instruction::ExtractValue;
2209   }
2210   static inline bool classof(const Value *V) {
2211     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2212   }
2213 };
2214
2215 ExtractValueInst::ExtractValueInst(Value *Agg,
2216                                    ArrayRef<unsigned> Idxs,
2217                                    const Twine &NameStr,
2218                                    Instruction *InsertBefore)
2219   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2220                      ExtractValue, Agg, InsertBefore) {
2221   init(Idxs, NameStr);
2222 }
2223 ExtractValueInst::ExtractValueInst(Value *Agg,
2224                                    ArrayRef<unsigned> Idxs,
2225                                    const Twine &NameStr,
2226                                    BasicBlock *InsertAtEnd)
2227   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2228                      ExtractValue, Agg, InsertAtEnd) {
2229   init(Idxs, NameStr);
2230 }
2231
2232 //===----------------------------------------------------------------------===//
2233 //                                InsertValueInst Class
2234 //===----------------------------------------------------------------------===//
2235
2236 /// InsertValueInst - This instruction inserts a struct field of array element
2237 /// value into an aggregate value.
2238 ///
2239 class InsertValueInst : public Instruction {
2240   SmallVector<unsigned, 4> Indices;
2241
2242   void *operator new(size_t, unsigned) = delete;
2243   InsertValueInst(const InsertValueInst &IVI);
2244   void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2245             const Twine &NameStr);
2246
2247   /// Constructors - Create a insertvalue instruction with a base aggregate
2248   /// value, a value to insert, and a list of indices.  The first ctor can
2249   /// optionally insert before an existing instruction, the second appends
2250   /// the new instruction to the specified BasicBlock.
2251   inline InsertValueInst(Value *Agg, Value *Val,
2252                          ArrayRef<unsigned> Idxs,
2253                          const Twine &NameStr,
2254                          Instruction *InsertBefore);
2255   inline InsertValueInst(Value *Agg, Value *Val,
2256                          ArrayRef<unsigned> Idxs,
2257                          const Twine &NameStr, BasicBlock *InsertAtEnd);
2258
2259   /// Constructors - These two constructors are convenience methods because one
2260   /// and two index insertvalue instructions are so common.
2261   InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2262                   const Twine &NameStr = "",
2263                   Instruction *InsertBefore = nullptr);
2264   InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2265                   BasicBlock *InsertAtEnd);
2266
2267 protected:
2268   // Note: Instruction needs to be a friend here to call cloneImpl.
2269   friend class Instruction;
2270   InsertValueInst *cloneImpl() const;
2271
2272 public:
2273   // allocate space for exactly two operands
2274   void *operator new(size_t s) {
2275     return User::operator new(s, 2);
2276   }
2277
2278   static InsertValueInst *Create(Value *Agg, Value *Val,
2279                                  ArrayRef<unsigned> Idxs,
2280                                  const Twine &NameStr = "",
2281                                  Instruction *InsertBefore = nullptr) {
2282     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2283   }
2284   static InsertValueInst *Create(Value *Agg, Value *Val,
2285                                  ArrayRef<unsigned> Idxs,
2286                                  const Twine &NameStr,
2287                                  BasicBlock *InsertAtEnd) {
2288     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2289   }
2290
2291   /// Transparently provide more efficient getOperand methods.
2292   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2293
2294   typedef const unsigned* idx_iterator;
2295   inline idx_iterator idx_begin() const { return Indices.begin(); }
2296   inline idx_iterator idx_end()   const { return Indices.end(); }
2297   inline iterator_range<idx_iterator> indices() const {
2298     return iterator_range<idx_iterator>(idx_begin(), idx_end());
2299   }
2300
2301   Value *getAggregateOperand() {
2302     return getOperand(0);
2303   }
2304   const Value *getAggregateOperand() const {
2305     return getOperand(0);
2306   }
2307   static unsigned getAggregateOperandIndex() {
2308     return 0U;                      // get index for modifying correct operand
2309   }
2310
2311   Value *getInsertedValueOperand() {
2312     return getOperand(1);
2313   }
2314   const Value *getInsertedValueOperand() const {
2315     return getOperand(1);
2316   }
2317   static unsigned getInsertedValueOperandIndex() {
2318     return 1U;                      // get index for modifying correct operand
2319   }
2320
2321   ArrayRef<unsigned> getIndices() const {
2322     return Indices;
2323   }
2324
2325   unsigned getNumIndices() const {
2326     return (unsigned)Indices.size();
2327   }
2328
2329   bool hasIndices() const {
2330     return true;
2331   }
2332
2333   // Methods for support type inquiry through isa, cast, and dyn_cast:
2334   static inline bool classof(const Instruction *I) {
2335     return I->getOpcode() == Instruction::InsertValue;
2336   }
2337   static inline bool classof(const Value *V) {
2338     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2339   }
2340 };
2341
2342 template <>
2343 struct OperandTraits<InsertValueInst> :
2344   public FixedNumOperandTraits<InsertValueInst, 2> {
2345 };
2346
2347 InsertValueInst::InsertValueInst(Value *Agg,
2348                                  Value *Val,
2349                                  ArrayRef<unsigned> Idxs,
2350                                  const Twine &NameStr,
2351                                  Instruction *InsertBefore)
2352   : Instruction(Agg->getType(), InsertValue,
2353                 OperandTraits<InsertValueInst>::op_begin(this),
2354                 2, InsertBefore) {
2355   init(Agg, Val, Idxs, NameStr);
2356 }
2357 InsertValueInst::InsertValueInst(Value *Agg,
2358                                  Value *Val,
2359                                  ArrayRef<unsigned> Idxs,
2360                                  const Twine &NameStr,
2361                                  BasicBlock *InsertAtEnd)
2362   : Instruction(Agg->getType(), InsertValue,
2363                 OperandTraits<InsertValueInst>::op_begin(this),
2364                 2, InsertAtEnd) {
2365   init(Agg, Val, Idxs, NameStr);
2366 }
2367
2368 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2369
2370 //===----------------------------------------------------------------------===//
2371 //                               PHINode Class
2372 //===----------------------------------------------------------------------===//
2373
2374 // PHINode - The PHINode class is used to represent the magical mystical PHI
2375 // node, that can not exist in nature, but can be synthesized in a computer
2376 // scientist's overactive imagination.
2377 //
2378 class PHINode : public Instruction {
2379   void *operator new(size_t, unsigned) = delete;
2380   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
2381   /// the number actually in use.
2382   unsigned ReservedSpace;
2383   PHINode(const PHINode &PN);
2384   // allocate space for exactly zero operands
2385   void *operator new(size_t s) {
2386     return User::operator new(s);
2387   }
2388   explicit PHINode(Type *Ty, unsigned NumReservedValues,
2389                    const Twine &NameStr = "",
2390                    Instruction *InsertBefore = nullptr)
2391     : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2392       ReservedSpace(NumReservedValues) {
2393     setName(NameStr);
2394     allocHungoffUses(ReservedSpace);
2395   }
2396
2397   PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2398           BasicBlock *InsertAtEnd)
2399     : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2400       ReservedSpace(NumReservedValues) {
2401     setName(NameStr);
2402     allocHungoffUses(ReservedSpace);
2403   }
2404
2405 protected:
2406   // allocHungoffUses - this is more complicated than the generic
2407   // User::allocHungoffUses, because we have to allocate Uses for the incoming
2408   // values and pointers to the incoming blocks, all in one allocation.
2409   void allocHungoffUses(unsigned N) {
2410     User::allocHungoffUses(N, /* IsPhi */ true);
2411   }
2412
2413   // Note: Instruction needs to be a friend here to call cloneImpl.
2414   friend class Instruction;
2415   PHINode *cloneImpl() const;
2416
2417 public:
2418   /// Constructors - NumReservedValues is a hint for the number of incoming
2419   /// edges that this phi node will have (use 0 if you really have no idea).
2420   static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2421                          const Twine &NameStr = "",
2422                          Instruction *InsertBefore = nullptr) {
2423     return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2424   }
2425   static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2426                          const Twine &NameStr, BasicBlock *InsertAtEnd) {
2427     return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2428   }
2429
2430   /// Provide fast operand accessors
2431   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2432
2433   // Block iterator interface. This provides access to the list of incoming
2434   // basic blocks, which parallels the list of incoming values.
2435
2436   typedef BasicBlock **block_iterator;
2437   typedef BasicBlock * const *const_block_iterator;
2438
2439   block_iterator block_begin() {
2440     Use::UserRef *ref =
2441       reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2442     return reinterpret_cast<block_iterator>(ref + 1);
2443   }
2444
2445   const_block_iterator block_begin() const {
2446     const Use::UserRef *ref =
2447       reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2448     return reinterpret_cast<const_block_iterator>(ref + 1);
2449   }
2450
2451   block_iterator block_end() {
2452     return block_begin() + getNumOperands();
2453   }
2454
2455   const_block_iterator block_end() const {
2456     return block_begin() + getNumOperands();
2457   }
2458
2459   op_range incoming_values() { return operands(); }
2460
2461   const_op_range incoming_values() const { return operands(); }
2462
2463   /// getNumIncomingValues - Return the number of incoming edges
2464   ///
2465   unsigned getNumIncomingValues() const { return getNumOperands(); }
2466
2467   /// getIncomingValue - Return incoming value number x
2468   ///
2469   Value *getIncomingValue(unsigned i) const {
2470     return getOperand(i);
2471   }
2472   void setIncomingValue(unsigned i, Value *V) {
2473     assert(V && "PHI node got a null value!");
2474     assert(getType() == V->getType() &&
2475            "All operands to PHI node must be the same type as the PHI node!");
2476     setOperand(i, V);
2477   }
2478   static unsigned getOperandNumForIncomingValue(unsigned i) {
2479     return i;
2480   }
2481   static unsigned getIncomingValueNumForOperand(unsigned i) {
2482     return i;
2483   }
2484
2485   /// getIncomingBlock - Return incoming basic block number @p i.
2486   ///
2487   BasicBlock *getIncomingBlock(unsigned i) const {
2488     return block_begin()[i];
2489   }
2490
2491   /// getIncomingBlock - Return incoming basic block corresponding
2492   /// to an operand of the PHI.
2493   ///
2494   BasicBlock *getIncomingBlock(const Use &U) const {
2495     assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2496     return getIncomingBlock(unsigned(&U - op_begin()));
2497   }
2498
2499   /// getIncomingBlock - Return incoming basic block corresponding
2500   /// to value use iterator.
2501   ///
2502   BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2503     return getIncomingBlock(I.getUse());
2504   }
2505
2506   void setIncomingBlock(unsigned i, BasicBlock *BB) {
2507     assert(BB && "PHI node got a null basic block!");
2508     block_begin()[i] = BB;
2509   }
2510
2511   /// addIncoming - Add an incoming value to the end of the PHI list
2512   ///
2513   void addIncoming(Value *V, BasicBlock *BB) {
2514     if (getNumOperands() == ReservedSpace)
2515       growOperands();  // Get more space!
2516     // Initialize some new operands.
2517     setNumHungOffUseOperands(getNumOperands() + 1);
2518     setIncomingValue(getNumOperands() - 1, V);
2519     setIncomingBlock(getNumOperands() - 1, BB);
2520   }
2521
2522   /// removeIncomingValue - Remove an incoming value.  This is useful if a
2523   /// predecessor basic block is deleted.  The value removed is returned.
2524   ///
2525   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2526   /// is true), the PHI node is destroyed and any uses of it are replaced with
2527   /// dummy values.  The only time there should be zero incoming values to a PHI
2528   /// node is when the block is dead, so this strategy is sound.
2529   ///
2530   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2531
2532   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2533     int Idx = getBasicBlockIndex(BB);
2534     assert(Idx >= 0 && "Invalid basic block argument to remove!");
2535     return removeIncomingValue(Idx, DeletePHIIfEmpty);
2536   }
2537
2538   /// getBasicBlockIndex - Return the first index of the specified basic
2539   /// block in the value list for this PHI.  Returns -1 if no instance.
2540   ///
2541   int getBasicBlockIndex(const BasicBlock *BB) const {
2542     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2543       if (block_begin()[i] == BB)
2544         return i;
2545     return -1;
2546   }
2547
2548   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2549     int Idx = getBasicBlockIndex(BB);
2550     assert(Idx >= 0 && "Invalid basic block argument!");
2551     return getIncomingValue(Idx);
2552   }
2553
2554   /// hasConstantValue - If the specified PHI node always merges together the
2555   /// same value, return the value, otherwise return null.
2556   Value *hasConstantValue() const;
2557
2558   /// Methods for support type inquiry through isa, cast, and dyn_cast:
2559   static inline bool classof(const Instruction *I) {
2560     return I->getOpcode() == Instruction::PHI;
2561   }
2562   static inline bool classof(const Value *V) {
2563     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2564   }
2565
2566 private:
2567   void growOperands();
2568 };
2569
2570 template <>
2571 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2572 };
2573
2574 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2575
2576 //===----------------------------------------------------------------------===//
2577 //                           LandingPadInst Class
2578 //===----------------------------------------------------------------------===//
2579
2580 //===---------------------------------------------------------------------------
2581 /// LandingPadInst - The landingpad instruction holds all of the information
2582 /// necessary to generate correct exception handling. The landingpad instruction
2583 /// cannot be moved from the top of a landing pad block, which itself is
2584 /// accessible only from the 'unwind' edge of an invoke. This uses the
2585 /// SubclassData field in Value to store whether or not the landingpad is a
2586 /// cleanup.
2587 ///
2588 class LandingPadInst : public Instruction {
2589   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
2590   /// the number actually in use.
2591   unsigned ReservedSpace;
2592   LandingPadInst(const LandingPadInst &LP);
2593
2594 public:
2595   enum ClauseType { Catch, Filter };
2596
2597 private:
2598   void *operator new(size_t, unsigned) = delete;
2599   // Allocate space for exactly zero operands.
2600   void *operator new(size_t s) {
2601     return User::operator new(s);
2602   }
2603   void growOperands(unsigned Size);
2604   void init(unsigned NumReservedValues, const Twine &NameStr);
2605
2606   explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2607                           const Twine &NameStr, Instruction *InsertBefore);
2608   explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2609                           const Twine &NameStr, BasicBlock *InsertAtEnd);
2610
2611 protected:
2612   // Note: Instruction needs to be a friend here to call cloneImpl.
2613   friend class Instruction;
2614   LandingPadInst *cloneImpl() const;
2615
2616 public:
2617   /// Constructors - NumReservedClauses is a hint for the number of incoming
2618   /// clauses that this landingpad will have (use 0 if you really have no idea).
2619   static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2620                                 const Twine &NameStr = "",
2621                                 Instruction *InsertBefore = nullptr);
2622   static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2623                                 const Twine &NameStr, BasicBlock *InsertAtEnd);
2624
2625   /// Provide fast operand accessors
2626   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2627
2628   /// isCleanup - Return 'true' if this landingpad instruction is a
2629   /// cleanup. I.e., it should be run when unwinding even if its landing pad
2630   /// doesn't catch the exception.
2631   bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2632
2633   /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2634   void setCleanup(bool V) {
2635     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2636                                (V ? 1 : 0));
2637   }
2638
2639   /// Add a catch or filter clause to the landing pad.
2640   void addClause(Constant *ClauseVal);
2641
2642   /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2643   /// determine what type of clause this is.
2644   Constant *getClause(unsigned Idx) const {
2645     return cast<Constant>(getOperandList()[Idx]);
2646   }
2647
2648   /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2649   bool isCatch(unsigned Idx) const {
2650     return !isa<ArrayType>(getOperandList()[Idx]->getType());
2651   }
2652
2653   /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2654   bool isFilter(unsigned Idx) const {
2655     return isa<ArrayType>(getOperandList()[Idx]->getType());
2656   }
2657
2658   /// getNumClauses - Get the number of clauses for this landing pad.
2659   unsigned getNumClauses() const { return getNumOperands(); }
2660
2661   /// reserveClauses - Grow the size of the operand list to accommodate the new
2662   /// number of clauses.
2663   void reserveClauses(unsigned Size) { growOperands(Size); }
2664
2665   // Methods for support type inquiry through isa, cast, and dyn_cast:
2666   static inline bool classof(const Instruction *I) {
2667     return I->getOpcode() == Instruction::LandingPad;
2668   }
2669   static inline bool classof(const Value *V) {
2670     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2671   }
2672 };
2673
2674 template <>
2675 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
2676 };
2677
2678 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2679
2680 //===----------------------------------------------------------------------===//
2681 //                               ReturnInst Class
2682 //===----------------------------------------------------------------------===//
2683
2684 //===---------------------------------------------------------------------------
2685 /// ReturnInst - Return a value (possibly void), from a function.  Execution
2686 /// does not continue in this function any longer.
2687 ///
2688 class ReturnInst : public TerminatorInst {
2689   ReturnInst(const ReturnInst &RI);
2690
2691 private:
2692   // ReturnInst constructors:
2693   // ReturnInst()                  - 'ret void' instruction
2694   // ReturnInst(    null)          - 'ret void' instruction
2695   // ReturnInst(Value* X)          - 'ret X'    instruction
2696   // ReturnInst(    null, Inst *I) - 'ret void' instruction, insert before I
2697   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
2698   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of B
2699   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of B
2700   //
2701   // NOTE: If the Value* passed is of type void then the constructor behaves as
2702   // if it was passed NULL.
2703   explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2704                       Instruction *InsertBefore = nullptr);
2705   ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2706   explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2707
2708 protected:
2709   // Note: Instruction needs to be a friend here to call cloneImpl.
2710   friend class Instruction;
2711   ReturnInst *cloneImpl() const;
2712
2713 public:
2714   static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2715                             Instruction *InsertBefore = nullptr) {
2716     return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2717   }
2718   static ReturnInst* Create(LLVMContext &C, Value *retVal,
2719                             BasicBlock *InsertAtEnd) {
2720     return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2721   }
2722   static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2723     return new(0) ReturnInst(C, InsertAtEnd);
2724   }
2725   ~ReturnInst() override;
2726
2727   /// Provide fast operand accessors
2728   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2729
2730   /// Convenience accessor. Returns null if there is no return value.
2731   Value *getReturnValue() const {
2732     return getNumOperands() != 0 ? getOperand(0) : nullptr;
2733   }
2734
2735   unsigned getNumSuccessors() const { return 0; }
2736
2737   // Methods for support type inquiry through isa, cast, and dyn_cast:
2738   static inline bool classof(const Instruction *I) {
2739     return (I->getOpcode() == Instruction::Ret);
2740   }
2741   static inline bool classof(const Value *V) {
2742     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2743   }
2744
2745 private:
2746   BasicBlock *getSuccessorV(unsigned idx) const override;
2747   unsigned getNumSuccessorsV() const override;
2748   void setSuccessorV(unsigned idx, BasicBlock *B) override;
2749 };
2750
2751 template <>
2752 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2753 };
2754
2755 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2756
2757 //===----------------------------------------------------------------------===//
2758 //                               BranchInst Class
2759 //===----------------------------------------------------------------------===//
2760
2761 //===---------------------------------------------------------------------------
2762 /// BranchInst - Conditional or Unconditional Branch instruction.
2763 ///
2764 class BranchInst : public TerminatorInst {
2765   /// Ops list - Branches are strange.  The operands are ordered:
2766   ///  [Cond, FalseDest,] TrueDest.  This makes some accessors faster because
2767   /// they don't have to check for cond/uncond branchness. These are mostly
2768   /// accessed relative from op_end().
2769   BranchInst(const BranchInst &BI);
2770   void AssertOK();
2771   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2772   // BranchInst(BB *B)                           - 'br B'
2773   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
2774   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
2775   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2776   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
2777   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
2778   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2779   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2780              Instruction *InsertBefore = nullptr);
2781   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2782   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2783              BasicBlock *InsertAtEnd);
2784
2785 protected:
2786   // Note: Instruction needs to be a friend here to call cloneImpl.
2787   friend class Instruction;
2788   BranchInst *cloneImpl() const;
2789
2790 public:
2791   static BranchInst *Create(BasicBlock *IfTrue,
2792                             Instruction *InsertBefore = nullptr) {
2793     return new(1) BranchInst(IfTrue, InsertBefore);
2794   }
2795   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2796                             Value *Cond, Instruction *InsertBefore = nullptr) {
2797     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2798   }
2799   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2800     return new(1) BranchInst(IfTrue, InsertAtEnd);
2801   }
2802   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2803                             Value *Cond, BasicBlock *InsertAtEnd) {
2804     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2805   }
2806
2807   /// Transparently provide more efficient getOperand methods.
2808   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2809
2810   bool isUnconditional() const { return getNumOperands() == 1; }
2811   bool isConditional()   const { return getNumOperands() == 3; }
2812
2813   Value *getCondition() const {
2814     assert(isConditional() && "Cannot get condition of an uncond branch!");
2815     return Op<-3>();
2816   }
2817
2818   void setCondition(Value *V) {
2819     assert(isConditional() && "Cannot set condition of unconditional branch!");
2820     Op<-3>() = V;
2821   }
2822
2823   unsigned getNumSuccessors() const { return 1+isConditional(); }
2824
2825   BasicBlock *getSuccessor(unsigned i) const {
2826     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2827     return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2828   }
2829
2830   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2831     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2832     *(&Op<-1>() - idx) = NewSucc;
2833   }
2834
2835   /// \brief Swap the successors of this branch instruction.
2836   ///
2837   /// Swaps the successors of the branch instruction. This also swaps any
2838   /// branch weight metadata associated with the instruction so that it
2839   /// continues to map correctly to each operand.
2840   void swapSuccessors();
2841
2842   // Methods for support type inquiry through isa, cast, and dyn_cast:
2843   static inline bool classof(const Instruction *I) {
2844     return (I->getOpcode() == Instruction::Br);
2845   }
2846   static inline bool classof(const Value *V) {
2847     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2848   }
2849
2850 private:
2851   BasicBlock *getSuccessorV(unsigned idx) const override;
2852   unsigned getNumSuccessorsV() const override;
2853   void setSuccessorV(unsigned idx, BasicBlock *B) override;
2854 };
2855
2856 template <>
2857 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2858 };
2859
2860 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2861
2862 //===----------------------------------------------------------------------===//
2863 //                               SwitchInst Class
2864 //===----------------------------------------------------------------------===//
2865
2866 //===---------------------------------------------------------------------------
2867 /// SwitchInst - Multiway switch
2868 ///
2869 class SwitchInst : public TerminatorInst {
2870   void *operator new(size_t, unsigned) = delete;
2871   unsigned ReservedSpace;
2872   // Operand[0]    = Value to switch on
2873   // Operand[1]    = Default basic block destination
2874   // Operand[2n  ] = Value to match
2875   // Operand[2n+1] = BasicBlock to go to on match
2876   SwitchInst(const SwitchInst &SI);
2877   void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2878   void growOperands();
2879   // allocate space for exactly zero operands
2880   void *operator new(size_t s) {
2881     return User::operator new(s);
2882   }
2883   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2884   /// switch on and a default destination.  The number of additional cases can
2885   /// be specified here to make memory allocation more efficient.  This
2886   /// constructor can also autoinsert before another instruction.
2887   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2888              Instruction *InsertBefore);
2889
2890   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2891   /// switch on and a default destination.  The number of additional cases can
2892   /// be specified here to make memory allocation more efficient.  This
2893   /// constructor also autoinserts at the end of the specified BasicBlock.
2894   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2895              BasicBlock *InsertAtEnd);
2896
2897 protected:
2898   // Note: Instruction needs to be a friend here to call cloneImpl.
2899   friend class Instruction;
2900   SwitchInst *cloneImpl() const;
2901
2902 public:
2903   // -2
2904   static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2905
2906   template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2907   class CaseIteratorT {
2908   protected:
2909     SwitchInstTy *SI;
2910     unsigned Index;
2911
2912   public:
2913     typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2914
2915     /// Initializes case iterator for given SwitchInst and for given
2916     /// case number.
2917     CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2918       this->SI = SI;
2919       Index = CaseNum;
2920     }
2921
2922     /// Initializes case iterator for given SwitchInst and for given
2923     /// TerminatorInst's successor index.
2924     static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2925       assert(SuccessorIndex < SI->getNumSuccessors() &&
2926              "Successor index # out of range!");
2927       return SuccessorIndex != 0 ?
2928              Self(SI, SuccessorIndex - 1) :
2929              Self(SI, DefaultPseudoIndex);
2930     }
2931
2932     /// Resolves case value for current case.
2933     ConstantIntTy *getCaseValue() {
2934       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2935       return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2936     }
2937
2938     /// Resolves successor for current case.
2939     BasicBlockTy *getCaseSuccessor() {
2940       assert((Index < SI->getNumCases() ||
2941               Index == DefaultPseudoIndex) &&
2942              "Index out the number of cases.");
2943       return SI->getSuccessor(getSuccessorIndex());
2944     }
2945
2946     /// Returns number of current case.
2947     unsigned getCaseIndex() const { return Index; }
2948
2949     /// Returns TerminatorInst's successor index for current case successor.
2950     unsigned getSuccessorIndex() const {
2951       assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2952              "Index out the number of cases.");
2953       return Index != DefaultPseudoIndex ? Index + 1 : 0;
2954     }
2955
2956     Self operator++() {
2957       // Check index correctness after increment.
2958       // Note: Index == getNumCases() means end().
2959       assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2960       ++Index;
2961       return *this;
2962     }
2963     Self operator++(int) {
2964       Self tmp = *this;
2965       ++(*this);
2966       return tmp;
2967     }
2968     Self operator--() {
2969       // Check index correctness after decrement.
2970       // Note: Index == getNumCases() means end().
2971       // Also allow "-1" iterator here. That will became valid after ++.
2972       assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2973              "Index out the number of cases.");
2974       --Index;
2975       return *this;
2976     }
2977     Self operator--(int) {
2978       Self tmp = *this;
2979       --(*this);
2980       return tmp;
2981     }
2982     bool operator==(const Self& RHS) const {
2983       assert(RHS.SI == SI && "Incompatible operators.");
2984       return RHS.Index == Index;
2985     }
2986     bool operator!=(const Self& RHS) const {
2987       assert(RHS.SI == SI && "Incompatible operators.");
2988       return RHS.Index != Index;
2989     }
2990     Self &operator*() {
2991       return *this;
2992     }
2993   };
2994
2995   typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2996     ConstCaseIt;
2997
2998   class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2999
3000     typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
3001
3002   public:
3003     CaseIt(const ParentTy &Src) : ParentTy(Src) {}
3004     CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
3005
3006     /// Sets the new value for current case.
3007     void setValue(ConstantInt *V) {
3008       assert(Index < SI->getNumCases() && "Index out the number of cases.");
3009       SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3010     }
3011
3012     /// Sets the new successor for current case.
3013     void setSuccessor(BasicBlock *S) {
3014       SI->setSuccessor(getSuccessorIndex(), S);
3015     }
3016   };
3017
3018   static SwitchInst *Create(Value *Value, BasicBlock *Default,
3019                             unsigned NumCases,
3020                             Instruction *InsertBefore = nullptr) {
3021     return new SwitchInst(Value, Default, NumCases, InsertBefore);
3022   }
3023   static SwitchInst *Create(Value *Value, BasicBlock *Default,
3024                             unsigned NumCases, BasicBlock *InsertAtEnd) {
3025     return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3026   }
3027
3028   /// Provide fast operand accessors
3029   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3030
3031   // Accessor Methods for Switch stmt
3032   Value *getCondition() const { return getOperand(0); }
3033   void setCondition(Value *V) { setOperand(0, V); }
3034
3035   BasicBlock *getDefaultDest() const {
3036     return cast<BasicBlock>(getOperand(1));
3037   }
3038
3039   void setDefaultDest(BasicBlock *DefaultCase) {
3040     setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3041   }
3042
3043   /// getNumCases - return the number of 'cases' in this switch instruction,
3044   /// except the default case
3045   unsigned getNumCases() const {
3046     return getNumOperands()/2 - 1;
3047   }
3048
3049   /// Returns a read/write iterator that points to the first
3050   /// case in SwitchInst.
3051   CaseIt case_begin() {
3052     return CaseIt(this, 0);
3053   }
3054   /// Returns a read-only iterator that points to the first
3055   /// case in the SwitchInst.
3056   ConstCaseIt case_begin() const {
3057     return ConstCaseIt(this, 0);
3058   }
3059
3060   /// Returns a read/write iterator that points one past the last
3061   /// in the SwitchInst.
3062   CaseIt case_end() {
3063     return CaseIt(this, getNumCases());
3064   }
3065   /// Returns a read-only iterator that points one past the last
3066   /// in the SwitchInst.
3067   ConstCaseIt case_end() const {
3068     return ConstCaseIt(this, getNumCases());
3069   }
3070
3071   /// cases - iteration adapter for range-for loops.
3072   iterator_range<CaseIt> cases() {
3073     return iterator_range<CaseIt>(case_begin(), case_end());
3074   }
3075
3076   /// cases - iteration adapter for range-for loops.
3077   iterator_range<ConstCaseIt> cases() const {
3078     return iterator_range<ConstCaseIt>(case_begin(), case_end());
3079   }
3080
3081   /// Returns an iterator that points to the default case.
3082   /// Note: this iterator allows to resolve successor only. Attempt
3083   /// to resolve case value causes an assertion.
3084   /// Also note, that increment and decrement also causes an assertion and
3085   /// makes iterator invalid.
3086   CaseIt case_default() {
3087     return CaseIt(this, DefaultPseudoIndex);
3088   }
3089   ConstCaseIt case_default() const {
3090     return ConstCaseIt(this, DefaultPseudoIndex);
3091   }
3092
3093   /// findCaseValue - Search all of the case values for the specified constant.
3094   /// If it is explicitly handled, return the case iterator of it, otherwise
3095   /// return default case iterator to indicate
3096   /// that it is handled by the default handler.
3097   CaseIt findCaseValue(const ConstantInt *C) {
3098     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
3099       if (i.getCaseValue() == C)
3100         return i;
3101     return case_default();
3102   }
3103   ConstCaseIt findCaseValue(const ConstantInt *C) const {
3104     for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
3105       if (i.getCaseValue() == C)
3106         return i;
3107     return case_default();
3108   }
3109
3110   /// findCaseDest - Finds the unique case value for a given successor. Returns
3111   /// null if the successor is not found, not unique, or is the default case.
3112   ConstantInt *findCaseDest(BasicBlock *BB) {
3113     if (BB == getDefaultDest()) return nullptr;
3114
3115     ConstantInt *CI = nullptr;
3116     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
3117       if (i.getCaseSuccessor() == BB) {
3118         if (CI) return nullptr;   // Multiple cases lead to BB.
3119         else CI = i.getCaseValue();
3120       }
3121     }
3122     return CI;
3123   }
3124
3125   /// addCase - Add an entry to the switch instruction...
3126   /// Note:
3127   /// This action invalidates case_end(). Old case_end() iterator will
3128   /// point to the added case.
3129   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3130
3131   /// removeCase - This method removes the specified case and its successor
3132   /// from the switch instruction. Note that this operation may reorder the
3133   /// remaining cases at index idx and above.
3134   /// Note:
3135   /// This action invalidates iterators for all cases following the one removed,
3136   /// including the case_end() iterator.
3137   void removeCase(CaseIt i);
3138
3139   unsigned getNumSuccessors() const { return getNumOperands()/2; }
3140   BasicBlock *getSuccessor(unsigned idx) const {
3141     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3142     return cast<BasicBlock>(getOperand(idx*2+1));
3143   }
3144   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3145     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3146     setOperand(idx * 2 + 1, NewSucc);
3147   }
3148
3149   // Methods for support type inquiry through isa, cast, and dyn_cast:
3150   static inline bool classof(const Instruction *I) {
3151     return I->getOpcode() == Instruction::Switch;
3152   }
3153   static inline bool classof(const Value *V) {
3154     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3155   }
3156
3157 private:
3158   BasicBlock *getSuccessorV(unsigned idx) const override;
3159   unsigned getNumSuccessorsV() const override;
3160   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3161 };
3162
3163 template <>
3164 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3165 };
3166
3167 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
3168
3169 //===----------------------------------------------------------------------===//
3170 //                             IndirectBrInst Class
3171 //===----------------------------------------------------------------------===//
3172
3173 //===---------------------------------------------------------------------------
3174 /// IndirectBrInst - Indirect Branch Instruction.
3175 ///
3176 class IndirectBrInst : public TerminatorInst {
3177   void *operator new(size_t, unsigned) = delete;
3178   unsigned ReservedSpace;
3179   // Operand[0]    = Value to switch on
3180   // Operand[1]    = Default basic block destination
3181   // Operand[2n  ] = Value to match
3182   // Operand[2n+1] = BasicBlock to go to on match
3183   IndirectBrInst(const IndirectBrInst &IBI);
3184   void init(Value *Address, unsigned NumDests);
3185   void growOperands();
3186   // allocate space for exactly zero operands
3187   void *operator new(size_t s) {
3188     return User::operator new(s);
3189   }
3190   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3191   /// Address to jump to.  The number of expected destinations can be specified
3192   /// here to make memory allocation more efficient.  This constructor can also
3193   /// autoinsert before another instruction.
3194   IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3195
3196   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3197   /// Address to jump to.  The number of expected destinations can be specified
3198   /// here to make memory allocation more efficient.  This constructor also
3199   /// autoinserts at the end of the specified BasicBlock.
3200   IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3201
3202 protected:
3203   // Note: Instruction needs to be a friend here to call cloneImpl.
3204   friend class Instruction;
3205   IndirectBrInst *cloneImpl() const;
3206
3207 public:
3208   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3209                                 Instruction *InsertBefore = nullptr) {
3210     return new IndirectBrInst(Address, NumDests, InsertBefore);
3211   }
3212   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3213                                 BasicBlock *InsertAtEnd) {
3214     return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3215   }
3216
3217   /// Provide fast operand accessors.
3218   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3219
3220   // Accessor Methods for IndirectBrInst instruction.
3221   Value *getAddress() { return getOperand(0); }
3222   const Value *getAddress() const { return getOperand(0); }
3223   void setAddress(Value *V) { setOperand(0, V); }
3224
3225   /// getNumDestinations - return the number of possible destinations in this
3226   /// indirectbr instruction.
3227   unsigned getNumDestinations() const { return getNumOperands()-1; }
3228
3229   /// getDestination - Return the specified destination.
3230   BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3231   const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3232
3233   /// addDestination - Add a destination.
3234   ///
3235   void addDestination(BasicBlock *Dest);
3236
3237   /// removeDestination - This method removes the specified successor from the
3238   /// indirectbr instruction.
3239   void removeDestination(unsigned i);
3240
3241   unsigned getNumSuccessors() const { return getNumOperands()-1; }
3242   BasicBlock *getSuccessor(unsigned i) const {
3243     return cast<BasicBlock>(getOperand(i+1));
3244   }
3245   void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3246     setOperand(i + 1, NewSucc);
3247   }
3248
3249   // Methods for support type inquiry through isa, cast, and dyn_cast:
3250   static inline bool classof(const Instruction *I) {
3251     return I->getOpcode() == Instruction::IndirectBr;
3252   }
3253   static inline bool classof(const Value *V) {
3254     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3255   }
3256
3257 private:
3258   BasicBlock *getSuccessorV(unsigned idx) const override;
3259   unsigned getNumSuccessorsV() const override;
3260   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3261 };
3262
3263 template <>
3264 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3265 };
3266
3267 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3268
3269 //===----------------------------------------------------------------------===//
3270 //                               InvokeInst Class
3271 //===----------------------------------------------------------------------===//
3272
3273 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
3274 /// calling convention of the call.
3275 ///
3276 class InvokeInst : public TerminatorInst,
3277                    public OperandBundleUser<InvokeInst, User::op_iterator> {
3278   AttributeSet AttributeList;
3279   FunctionType *FTy;
3280   InvokeInst(const InvokeInst &BI);
3281   void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3282             ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3283             const Twine &NameStr) {
3284     init(cast<FunctionType>(
3285              cast<PointerType>(Func->getType())->getElementType()),
3286          Func, IfNormal, IfException, Args, Bundles, NameStr);
3287   }
3288   void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
3289             BasicBlock *IfException, ArrayRef<Value *> Args,
3290             ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3291
3292   /// Construct an InvokeInst given a range of arguments.
3293   ///
3294   /// \brief Construct an InvokeInst from a range of arguments
3295   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3296                     ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3297                     unsigned Values, const Twine &NameStr,
3298                     Instruction *InsertBefore)
3299       : InvokeInst(cast<FunctionType>(
3300                        cast<PointerType>(Func->getType())->getElementType()),
3301                    Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3302                    InsertBefore) {}
3303
3304   inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3305                     BasicBlock *IfException, ArrayRef<Value *> Args,
3306                     ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3307                     const Twine &NameStr, Instruction *InsertBefore);
3308   /// Construct an InvokeInst given a range of arguments.
3309   ///
3310   /// \brief Construct an InvokeInst from a range of arguments
3311   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3312                     ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3313                     unsigned Values, const Twine &NameStr,
3314                     BasicBlock *InsertAtEnd);
3315
3316   friend class OperandBundleUser<InvokeInst, User::op_iterator>;
3317   bool hasDescriptor() const { return HasDescriptor; }
3318
3319 protected:
3320   // Note: Instruction needs to be a friend here to call cloneImpl.
3321   friend class Instruction;
3322   InvokeInst *cloneImpl() const;
3323
3324 public:
3325   static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3326                             BasicBlock *IfException, ArrayRef<Value *> Args,
3327                             const Twine &NameStr,
3328                             Instruction *InsertBefore = nullptr) {
3329     return Create(cast<FunctionType>(
3330                       cast<PointerType>(Func->getType())->getElementType()),
3331                   Func, IfNormal, IfException, Args, None, NameStr,
3332                   InsertBefore);
3333   }
3334   static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3335                             BasicBlock *IfException, ArrayRef<Value *> Args,
3336                             ArrayRef<OperandBundleDef> Bundles = None,
3337                             const Twine &NameStr = "",
3338                             Instruction *InsertBefore = nullptr) {
3339     return Create(cast<FunctionType>(
3340                       cast<PointerType>(Func->getType())->getElementType()),
3341                   Func, IfNormal, IfException, Args, Bundles, NameStr,
3342                   InsertBefore);
3343   }
3344   static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3345                             BasicBlock *IfException, ArrayRef<Value *> Args,
3346                             const Twine &NameStr,
3347                             Instruction *InsertBefore = nullptr) {
3348     unsigned Values = unsigned(Args.size()) + 3;
3349     return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None,
3350                                    Values, NameStr, InsertBefore);
3351   }
3352   static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3353                             BasicBlock *IfException, ArrayRef<Value *> Args,
3354                             ArrayRef<OperandBundleDef> Bundles = None,
3355                             const Twine &NameStr = "",
3356                             Instruction *InsertBefore = nullptr) {
3357     unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3358     unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3359
3360     return new (Values, DescriptorBytes)
3361         InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values,
3362                    NameStr, InsertBefore);
3363   }
3364   static InvokeInst *Create(Value *Func,
3365                             BasicBlock *IfNormal, BasicBlock *IfException,
3366                             ArrayRef<Value *> Args, const Twine &NameStr,
3367                             BasicBlock *InsertAtEnd) {
3368     unsigned Values = unsigned(Args.size()) + 3;
3369     return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None,
3370                                    Values, NameStr, InsertAtEnd);
3371   }
3372   static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3373                             BasicBlock *IfException, ArrayRef<Value *> Args,
3374                             ArrayRef<OperandBundleDef> Bundles,
3375                             const Twine &NameStr, BasicBlock *InsertAtEnd) {
3376     unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3377     unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3378
3379     return new (Values, DescriptorBytes)
3380         InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3381                    InsertAtEnd);
3382   }
3383
3384   /// Provide fast operand accessors
3385   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3386
3387   FunctionType *getFunctionType() const { return FTy; }
3388
3389   void mutateFunctionType(FunctionType *FTy) {
3390     mutateType(FTy->getReturnType());
3391     this->FTy = FTy;
3392   }
3393
3394   /// getNumArgOperands - Return the number of invoke arguments.
3395   ///
3396   unsigned getNumArgOperands() const {
3397     return getNumOperands() - getNumTotalBundleOperands() - 3;
3398   }
3399
3400   /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3401   ///
3402   Value *getArgOperand(unsigned i) const {
3403     assert(i < getNumArgOperands() && "Out of bounds!");
3404     return getOperand(i);
3405   }
3406   void setArgOperand(unsigned i, Value *v) {
3407     assert(i < getNumArgOperands() && "Out of bounds!");
3408     setOperand(i, v);
3409   }
3410
3411   /// arg_operands - iteration adapter for range-for loops.
3412   iterator_range<op_iterator> arg_operands() {
3413     return iterator_range<op_iterator>(
3414         op_begin(), op_end() - getNumTotalBundleOperands() - 3);
3415   }
3416
3417   /// arg_operands - iteration adapter for range-for loops.
3418   iterator_range<const_op_iterator> arg_operands() const {
3419     return iterator_range<const_op_iterator>(
3420         op_begin(), op_end() - getNumTotalBundleOperands() - 3);
3421   }
3422
3423   /// \brief Wrappers for getting the \c Use of a invoke argument.
3424   const Use &getArgOperandUse(unsigned i) const {
3425     assert(i < getNumArgOperands() && "Out of bounds!");
3426     return getOperandUse(i);
3427   }
3428   Use &getArgOperandUse(unsigned i) {
3429     assert(i < getNumArgOperands() && "Out of bounds!");
3430     return getOperandUse(i);
3431   }
3432
3433   /// getCallingConv/setCallingConv - Get or set the calling convention of this
3434   /// function call.
3435   CallingConv::ID getCallingConv() const {
3436     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3437   }
3438   void setCallingConv(CallingConv::ID CC) {
3439     setInstructionSubclassData(static_cast<unsigned>(CC));
3440   }
3441
3442   /// getAttributes - Return the parameter attributes for this invoke.
3443   ///
3444   const AttributeSet &getAttributes() const { return AttributeList; }
3445
3446   /// setAttributes - Set the parameter attributes for this invoke.
3447   ///
3448   void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3449
3450   /// addAttribute - adds the attribute to the list of attributes.
3451   void addAttribute(unsigned i, Attribute::AttrKind attr);
3452
3453   /// removeAttribute - removes the attribute from the list of attributes.
3454   void removeAttribute(unsigned i, Attribute attr);
3455
3456   /// \brief adds the dereferenceable attribute to the list of attributes.
3457   void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3458
3459   /// \brief adds the dereferenceable_or_null attribute to the list of
3460   /// attributes.
3461   void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
3462
3463   /// \brief Determine whether this call has the given attribute.
3464   bool hasFnAttr(Attribute::AttrKind A) const {
3465     assert(A != Attribute::NoBuiltin &&
3466            "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3467     return hasFnAttrImpl(A);
3468   }
3469
3470   /// \brief Determine whether the call or the callee has the given attributes.
3471   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3472
3473   /// \brief Extract the alignment for a call or parameter (0=unknown).
3474   unsigned getParamAlignment(unsigned i) const {
3475     return AttributeList.getParamAlignment(i);
3476   }
3477
3478   /// \brief Extract the number of dereferenceable bytes for a call or
3479   /// parameter (0=unknown).
3480   uint64_t getDereferenceableBytes(unsigned i) const {
3481     return AttributeList.getDereferenceableBytes(i);
3482   }
3483
3484   /// \brief Extract the number of dereferenceable_or_null bytes for a call or
3485   /// parameter (0=unknown).
3486   uint64_t getDereferenceableOrNullBytes(unsigned i) const {
3487     return AttributeList.getDereferenceableOrNullBytes(i);
3488   }
3489
3490   /// @brief Determine if the parameter or return value is marked with NoAlias
3491   /// attribute.
3492   /// @param n The parameter to check. 1 is the first parameter, 0 is the return
3493   bool doesNotAlias(unsigned n) const {
3494     return AttributeList.hasAttribute(n, Attribute::NoAlias);
3495   }
3496
3497   /// \brief Return true if the call should not be treated as a call to a
3498   /// builtin.
3499   bool isNoBuiltin() const {
3500     // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3501     // to check it by hand.
3502     return hasFnAttrImpl(Attribute::NoBuiltin) &&
3503       !hasFnAttrImpl(Attribute::Builtin);
3504   }
3505
3506   /// \brief Return true if the call should not be inlined.
3507   bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3508   void setIsNoInline() {
3509     addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3510   }
3511
3512   /// \brief Determine if the call does not access memory.
3513   bool doesNotAccessMemory() const {
3514     return hasFnAttr(Attribute::ReadNone);
3515   }
3516   void setDoesNotAccessMemory() {
3517     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3518   }
3519
3520   /// \brief Determine if the call does not access or only reads memory.
3521   bool onlyReadsMemory() const {
3522     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3523   }
3524   void setOnlyReadsMemory() {
3525     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3526   }
3527
3528   /// @brief Determine if the call access memmory only using it's pointer
3529   /// arguments.
3530   bool onlyAccessesArgMemory() const {
3531     return hasFnAttr(Attribute::ArgMemOnly);
3532   }
3533   void setOnlyAccessesArgMemory() {
3534     addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
3535   }
3536
3537   /// \brief Determine if the call cannot return.
3538   bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3539   void setDoesNotReturn() {
3540     addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3541   }
3542
3543   /// \brief Determine if the call cannot unwind.
3544   bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3545   void setDoesNotThrow() {
3546     addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3547   }
3548
3549   /// \brief Determine if the invoke cannot be duplicated.
3550   bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3551   void setCannotDuplicate() {
3552     addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3553   }
3554
3555   /// \brief Determine if the call returns a structure through first
3556   /// pointer argument.
3557   bool hasStructRetAttr() const {
3558     // Be friendly and also check the callee.
3559     return paramHasAttr(1, Attribute::StructRet);
3560   }
3561
3562   /// \brief Determine if any call argument is an aggregate passed by value.
3563   bool hasByValArgument() const {
3564     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3565   }
3566
3567   /// getCalledFunction - Return the function called, or null if this is an
3568   /// indirect function invocation.
3569   ///
3570   Function *getCalledFunction() const {
3571     return dyn_cast<Function>(Op<-3>());
3572   }
3573
3574   /// getCalledValue - Get a pointer to the function that is invoked by this
3575   /// instruction
3576   const Value *getCalledValue() const { return Op<-3>(); }
3577         Value *getCalledValue()       { return Op<-3>(); }
3578
3579   /// setCalledFunction - Set the function called.
3580   void setCalledFunction(Value* Fn) {
3581     setCalledFunction(
3582         cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
3583         Fn);
3584   }
3585   void setCalledFunction(FunctionType *FTy, Value *Fn) {
3586     this->FTy = FTy;
3587     assert(FTy == cast<FunctionType>(
3588                       cast<PointerType>(Fn->getType())->getElementType()));
3589     Op<-3>() = Fn;
3590   }
3591
3592   // get*Dest - Return the destination basic blocks...
3593   BasicBlock *getNormalDest() const {
3594     return cast<BasicBlock>(Op<-2>());
3595   }
3596   BasicBlock *getUnwindDest() const {
3597     return cast<BasicBlock>(Op<-1>());
3598   }
3599   void setNormalDest(BasicBlock *B) {
3600     Op<-2>() = reinterpret_cast<Value*>(B);
3601   }
3602   void setUnwindDest(BasicBlock *B) {
3603     Op<-1>() = reinterpret_cast<Value*>(B);
3604   }
3605
3606   /// getLandingPadInst - Get the landingpad instruction from the landing pad
3607   /// block (the unwind destination).
3608   LandingPadInst *getLandingPadInst() const;
3609
3610   BasicBlock *getSuccessor(unsigned i) const {
3611     assert(i < 2 && "Successor # out of range for invoke!");
3612     return i == 0 ? getNormalDest() : getUnwindDest();
3613   }
3614
3615   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3616     assert(idx < 2 && "Successor # out of range for invoke!");
3617     *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3618   }
3619
3620   unsigned getNumSuccessors() const { return 2; }
3621
3622   // Methods for support type inquiry through isa, cast, and dyn_cast:
3623   static inline bool classof(const Instruction *I) {
3624     return (I->getOpcode() == Instruction::Invoke);
3625   }
3626   static inline bool classof(const Value *V) {
3627     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3628   }
3629
3630 private:
3631   BasicBlock *getSuccessorV(unsigned idx) const override;
3632   unsigned getNumSuccessorsV() const override;
3633   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3634
3635   bool hasFnAttrImpl(Attribute::AttrKind A) const;
3636
3637   // Shadow Instruction::setInstructionSubclassData with a private forwarding
3638   // method so that subclasses cannot accidentally use it.
3639   void setInstructionSubclassData(unsigned short D) {
3640     Instruction::setInstructionSubclassData(D);
3641   }
3642 };
3643
3644 template <>
3645 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3646 };
3647
3648 InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3649                        BasicBlock *IfException, ArrayRef<Value *> Args,
3650                        ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3651                        const Twine &NameStr, Instruction *InsertBefore)
3652     : TerminatorInst(Ty->getReturnType(), Instruction::Invoke,
3653                      OperandTraits<InvokeInst>::op_end(this) - Values, Values,
3654                      InsertBefore) {
3655   init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3656 }
3657 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
3658                        BasicBlock *IfException, ArrayRef<Value *> Args,
3659                        ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3660                        const Twine &NameStr, BasicBlock *InsertAtEnd)
3661     : TerminatorInst(
3662           cast<FunctionType>(cast<PointerType>(Func->getType())
3663                                  ->getElementType())->getReturnType(),
3664           Instruction::Invoke, OperandTraits<InvokeInst>::op_end(this) - Values,
3665           Values, InsertAtEnd) {
3666   init(Func, IfNormal, IfException, Args, Bundles, NameStr);
3667 }
3668
3669 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3670
3671 //===----------------------------------------------------------------------===//
3672 //                              ResumeInst Class
3673 //===----------------------------------------------------------------------===//
3674
3675 //===---------------------------------------------------------------------------
3676 /// ResumeInst - Resume the propagation of an exception.
3677 ///
3678 class ResumeInst : public TerminatorInst {
3679   ResumeInst(const ResumeInst &RI);
3680
3681   explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3682   ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3683
3684 protected:
3685   // Note: Instruction needs to be a friend here to call cloneImpl.
3686   friend class Instruction;
3687   ResumeInst *cloneImpl() const;
3688
3689 public:
3690   static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3691     return new(1) ResumeInst(Exn, InsertBefore);
3692   }
3693   static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3694     return new(1) ResumeInst(Exn, InsertAtEnd);
3695   }
3696
3697   /// Provide fast operand accessors
3698   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3699
3700   /// Convenience accessor.
3701   Value *getValue() const { return Op<0>(); }
3702
3703   unsigned getNumSuccessors() const { return 0; }
3704
3705   // Methods for support type inquiry through isa, cast, and dyn_cast:
3706   static inline bool classof(const Instruction *I) {
3707     return I->getOpcode() == Instruction::Resume;
3708   }
3709   static inline bool classof(const Value *V) {
3710     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3711   }
3712
3713 private:
3714   BasicBlock *getSuccessorV(unsigned idx) const override;
3715   unsigned getNumSuccessorsV() const override;
3716   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3717 };
3718
3719 template <>
3720 struct OperandTraits<ResumeInst> :
3721     public FixedNumOperandTraits<ResumeInst, 1> {
3722 };
3723
3724 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3725
3726 //===----------------------------------------------------------------------===//
3727 //                               CatchEndPadInst Class
3728 //===----------------------------------------------------------------------===//
3729
3730 class CatchEndPadInst : public TerminatorInst {
3731 private:
3732   CatchEndPadInst(const CatchEndPadInst &RI);
3733
3734   void init(BasicBlock *UnwindBB);
3735   CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB, unsigned Values,
3736                   Instruction *InsertBefore = nullptr);
3737   CatchEndPadInst(LLVMContext &C, BasicBlock *UnwindBB, unsigned Values,
3738                   BasicBlock *InsertAtEnd);
3739
3740 protected:
3741   // Note: Instruction needs to be a friend here to call cloneImpl.
3742   friend class Instruction;
3743   CatchEndPadInst *cloneImpl() const;
3744
3745 public:
3746   static CatchEndPadInst *Create(LLVMContext &C, BasicBlock *UnwindBB = nullptr,
3747                                  Instruction *InsertBefore = nullptr) {
3748     unsigned Values = UnwindBB ? 1 : 0;
3749     return new (Values) CatchEndPadInst(C, UnwindBB, Values, InsertBefore);
3750   }
3751   static CatchEndPadInst *Create(LLVMContext &C, BasicBlock *UnwindBB,
3752                                  BasicBlock *InsertAtEnd) {
3753     unsigned Values = UnwindBB ? 1 : 0;
3754     return new (Values) CatchEndPadInst(C, UnwindBB, Values, InsertAtEnd);
3755   }
3756
3757   /// Provide fast operand accessors
3758   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3759
3760   bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
3761   bool unwindsToCaller() const { return !hasUnwindDest(); }
3762
3763   /// Convenience accessor. Returns null if there is no return value.
3764   unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
3765
3766   BasicBlock *getUnwindDest() const {
3767     return hasUnwindDest() ? cast<BasicBlock>(Op<-1>()) : nullptr;
3768   }
3769   void setUnwindDest(BasicBlock *NewDest) {
3770     assert(NewDest);
3771     Op<-1>() = NewDest;
3772   }
3773
3774   // Methods for support type inquiry through isa, cast, and dyn_cast:
3775   static inline bool classof(const Instruction *I) {
3776     return (I->getOpcode() == Instruction::CatchEndPad);
3777   }
3778   static inline bool classof(const Value *V) {
3779     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3780   }
3781
3782 private:
3783   BasicBlock *getSuccessorV(unsigned Idx) const override;
3784   unsigned getNumSuccessorsV() const override;
3785   void setSuccessorV(unsigned Idx, BasicBlock *B) override;
3786
3787   // Shadow Instruction::setInstructionSubclassData with a private forwarding
3788   // method so that subclasses cannot accidentally use it.
3789   void setInstructionSubclassData(unsigned short D) {
3790     Instruction::setInstructionSubclassData(D);
3791   }
3792 };
3793
3794 template <>
3795 struct OperandTraits<CatchEndPadInst>
3796     : public VariadicOperandTraits<CatchEndPadInst> {};
3797
3798 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchEndPadInst, Value)
3799
3800 //===----------------------------------------------------------------------===//
3801 //                           CatchPadInst Class
3802 //===----------------------------------------------------------------------===//
3803
3804 class CatchPadInst : public TerminatorInst {
3805 private:
3806   void init(BasicBlock *IfNormal, BasicBlock *IfException,
3807             ArrayRef<Value *> Args, const Twine &NameStr);
3808
3809   CatchPadInst(const CatchPadInst &CPI);
3810
3811   explicit CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
3812                         ArrayRef<Value *> Args, unsigned Values,
3813                         const Twine &NameStr, Instruction *InsertBefore);
3814   explicit CatchPadInst(BasicBlock *IfNormal, BasicBlock *IfException,
3815                         ArrayRef<Value *> Args, unsigned Values,
3816                         const Twine &NameStr, BasicBlock *InsertAtEnd);
3817
3818 protected:
3819   // Note: Instruction needs to be a friend here to call cloneImpl.
3820   friend class Instruction;
3821   CatchPadInst *cloneImpl() const;
3822
3823 public:
3824   static CatchPadInst *Create(BasicBlock *IfNormal, BasicBlock *IfException,
3825                               ArrayRef<Value *> Args, const Twine &NameStr = "",
3826                               Instruction *InsertBefore = nullptr) {
3827     unsigned Values = unsigned(Args.size()) + 2;
3828     return new (Values) CatchPadInst(IfNormal, IfException, Args, Values,
3829                                      NameStr, InsertBefore);
3830   }
3831   static CatchPadInst *Create(BasicBlock *IfNormal, BasicBlock *IfException,
3832                               ArrayRef<Value *> Args, const Twine &NameStr,
3833                               BasicBlock *InsertAtEnd) {
3834     unsigned Values = unsigned(Args.size()) + 2;
3835     return new (Values)
3836         CatchPadInst(IfNormal, IfException, Args, Values, NameStr, InsertAtEnd);
3837   }
3838
3839   /// Provide fast operand accessors
3840   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3841
3842   /// getNumArgOperands - Return the number of catchpad arguments.
3843   ///
3844   unsigned getNumArgOperands() const { return getNumOperands() - 2; }
3845
3846   /// getArgOperand/setArgOperand - Return/set the i-th catchpad argument.
3847   ///
3848   Value *getArgOperand(unsigned i) const { return getOperand(i); }
3849   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3850
3851   /// arg_operands - iteration adapter for range-for loops.
3852   iterator_range<op_iterator> arg_operands() {
3853     return iterator_range<op_iterator>(op_begin(), op_end() - 2);
3854   }
3855
3856   /// arg_operands - iteration adapter for range-for loops.
3857   iterator_range<const_op_iterator> arg_operands() const {
3858     return iterator_range<const_op_iterator>(op_begin(), op_end() - 2);
3859   }
3860
3861   /// \brief Wrappers for getting the \c Use of a catchpad argument.
3862   const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3863   Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3864
3865   // get*Dest - Return the destination basic blocks...
3866   BasicBlock *getNormalDest() const { return cast<BasicBlock>(Op<-2>()); }
3867   BasicBlock *getUnwindDest() const { return cast<BasicBlock>(Op<-1>()); }
3868   void setNormalDest(BasicBlock *B) { Op<-2>() = B; }
3869   void setUnwindDest(BasicBlock *B) { Op<-1>() = B; }
3870
3871   BasicBlock *getSuccessor(unsigned i) const {
3872     assert(i < 2 && "Successor # out of range for catchpad!");
3873     return i == 0 ? getNormalDest() : getUnwindDest();
3874   }
3875
3876   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3877     assert(idx < 2 && "Successor # out of range for catchpad!");
3878     *(&Op<-2>() + idx) = NewSucc;
3879   }
3880
3881   unsigned getNumSuccessors() const { return 2; }
3882
3883   // Methods for support type inquiry through isa, cast, and dyn_cast:
3884   static inline bool classof(const Instruction *I) {
3885     return I->getOpcode() == Instruction::CatchPad;
3886   }
3887   static inline bool classof(const Value *V) {
3888     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3889   }
3890
3891 private:
3892   BasicBlock *getSuccessorV(unsigned idx) const override;
3893   unsigned getNumSuccessorsV() const override;
3894   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3895 };
3896
3897 template <>
3898 struct OperandTraits<CatchPadInst>
3899     : public VariadicOperandTraits<CatchPadInst, /*MINARITY=*/2> {};
3900
3901 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchPadInst, Value)
3902
3903 //===----------------------------------------------------------------------===//
3904 //                           TerminatePadInst Class
3905 //===----------------------------------------------------------------------===//
3906
3907 class TerminatePadInst : public TerminatorInst {
3908 private:
3909   void init(BasicBlock *BB, ArrayRef<Value *> Args);
3910
3911   TerminatePadInst(const TerminatePadInst &TPI);
3912
3913   explicit TerminatePadInst(LLVMContext &C, BasicBlock *BB,
3914                             ArrayRef<Value *> Args, unsigned Values,
3915                             Instruction *InsertBefore);
3916   explicit TerminatePadInst(LLVMContext &C, BasicBlock *BB,
3917                             ArrayRef<Value *> Args, unsigned Values,
3918                             BasicBlock *InsertAtEnd);
3919
3920 protected:
3921   // Note: Instruction needs to be a friend here to call cloneImpl.
3922   friend class Instruction;
3923   TerminatePadInst *cloneImpl() const;
3924
3925 public:
3926   static TerminatePadInst *Create(LLVMContext &C, BasicBlock *BB = nullptr,
3927                                   ArrayRef<Value *> Args = None,
3928                                   Instruction *InsertBefore = nullptr) {
3929     unsigned Values = unsigned(Args.size());
3930     if (BB)
3931       ++Values;
3932     return new (Values) TerminatePadInst(C, BB, Args, Values, InsertBefore);
3933   }
3934   static TerminatePadInst *Create(LLVMContext &C, BasicBlock *BB,
3935                                   ArrayRef<Value *> Args,
3936                                   BasicBlock *InsertAtEnd) {
3937     unsigned Values = unsigned(Args.size());
3938     if (BB)
3939       ++Values;
3940     return new (Values) TerminatePadInst(C, BB, Args, Values, InsertAtEnd);
3941   }
3942
3943   /// Provide fast operand accessors
3944   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3945
3946   bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
3947   bool unwindsToCaller() const { return !hasUnwindDest(); }
3948
3949   /// getNumArgOperands - Return the number of terminatepad arguments.
3950   ///
3951   unsigned getNumArgOperands() const {
3952     unsigned NumOperands = getNumOperands();
3953     if (hasUnwindDest())
3954       return NumOperands - 1;
3955     return NumOperands;
3956   }
3957
3958   /// getArgOperand/setArgOperand - Return/set the i-th terminatepad argument.
3959   ///
3960   Value *getArgOperand(unsigned i) const { return getOperand(i); }
3961   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3962
3963   const_op_iterator arg_end() const {
3964     if (hasUnwindDest())
3965       return op_end() - 1;
3966     return op_end();
3967   }
3968
3969   op_iterator arg_end() {
3970     if (hasUnwindDest())
3971       return op_end() - 1;
3972     return op_end();
3973   }
3974
3975   /// arg_operands - iteration adapter for range-for loops.
3976   iterator_range<op_iterator> arg_operands() {
3977     return iterator_range<op_iterator>(op_begin(), arg_end());
3978   }
3979
3980   /// arg_operands - iteration adapter for range-for loops.
3981   iterator_range<const_op_iterator> arg_operands() const {
3982     return iterator_range<const_op_iterator>(op_begin(), arg_end());
3983   }
3984
3985   /// \brief Wrappers for getting the \c Use of a terminatepad argument.
3986   const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3987   Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3988
3989   // get*Dest - Return the destination basic blocks...
3990   BasicBlock *getUnwindDest() const {
3991     if (!hasUnwindDest())
3992       return nullptr;
3993     return cast<BasicBlock>(Op<-1>());
3994   }
3995   void setUnwindDest(BasicBlock *B) {
3996     assert(B && hasUnwindDest());
3997     Op<-1>() = B;
3998   }
3999
4000   unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4001
4002   // Methods for support type inquiry through isa, cast, and dyn_cast:
4003   static inline bool classof(const Instruction *I) {
4004     return I->getOpcode() == Instruction::TerminatePad;
4005   }
4006   static inline bool classof(const Value *V) {
4007     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4008   }
4009
4010 private:
4011   BasicBlock *getSuccessorV(unsigned idx) const override;
4012   unsigned getNumSuccessorsV() const override;
4013   void setSuccessorV(unsigned idx, BasicBlock *B) override;
4014
4015   // Shadow Instruction::setInstructionSubclassData with a private forwarding
4016   // method so that subclasses cannot accidentally use it.
4017   void setInstructionSubclassData(unsigned short D) {
4018     Instruction::setInstructionSubclassData(D);
4019   }
4020 };
4021
4022 template <>
4023 struct OperandTraits<TerminatePadInst>
4024     : public VariadicOperandTraits<TerminatePadInst, /*MINARITY=*/1> {};
4025
4026 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(TerminatePadInst, Value)
4027
4028 //===----------------------------------------------------------------------===//
4029 //                           CleanupPadInst Class
4030 //===----------------------------------------------------------------------===//
4031
4032 class CleanupPadInst : public Instruction {
4033 private:
4034   void init(ArrayRef<Value *> Args, const Twine &NameStr);
4035
4036   CleanupPadInst(const CleanupPadInst &CPI);
4037
4038   explicit CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
4039                           const Twine &NameStr, Instruction *InsertBefore);
4040   explicit CleanupPadInst(LLVMContext &C, ArrayRef<Value *> Args,
4041                           const Twine &NameStr, BasicBlock *InsertAtEnd);
4042
4043 protected:
4044   // Note: Instruction needs to be a friend here to call cloneImpl.
4045   friend class Instruction;
4046   CleanupPadInst *cloneImpl() const;
4047
4048 public:
4049   static CleanupPadInst *Create(LLVMContext &C, ArrayRef<Value *> Args,
4050                                 const Twine &NameStr = "",
4051                                 Instruction *InsertBefore = nullptr) {
4052     return new (Args.size()) CleanupPadInst(C, Args, NameStr, InsertBefore);
4053   }
4054   static CleanupPadInst *Create(LLVMContext &C, ArrayRef<Value *> Args,
4055                                 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4056     return new (Args.size()) CleanupPadInst(C, Args, NameStr, InsertAtEnd);
4057   }
4058
4059   /// Provide fast operand accessors
4060   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4061
4062   // Methods for support type inquiry through isa, cast, and dyn_cast:
4063   static inline bool classof(const Instruction *I) {
4064     return I->getOpcode() == Instruction::CleanupPad;
4065   }
4066   static inline bool classof(const Value *V) {
4067     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4068   }
4069 };
4070
4071 template <>
4072 struct OperandTraits<CleanupPadInst>
4073     : public VariadicOperandTraits<CleanupPadInst, /*MINARITY=*/0> {};
4074
4075 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupPadInst, Value)
4076
4077 //===----------------------------------------------------------------------===//
4078 //                               CatchReturnInst Class
4079 //===----------------------------------------------------------------------===//
4080
4081 class CatchReturnInst : public TerminatorInst {
4082   CatchReturnInst(const CatchReturnInst &RI);
4083
4084   void init(CatchPadInst *CatchPad, BasicBlock *BB);
4085   CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
4086                   Instruction *InsertBefore);
4087   CatchReturnInst(CatchPadInst *CatchPad, BasicBlock *BB,
4088                   BasicBlock *InsertAtEnd);
4089
4090 protected:
4091   // Note: Instruction needs to be a friend here to call cloneImpl.
4092   friend class Instruction;
4093   CatchReturnInst *cloneImpl() const;
4094
4095 public:
4096   static CatchReturnInst *Create(CatchPadInst *CatchPad, BasicBlock *BB,
4097                                  Instruction *InsertBefore = nullptr) {
4098     assert(CatchPad);
4099     assert(BB);
4100     return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4101   }
4102   static CatchReturnInst *Create(CatchPadInst *CatchPad, BasicBlock *BB,
4103                                  BasicBlock *InsertAtEnd) {
4104     assert(CatchPad);
4105     assert(BB);
4106     return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd);
4107   }
4108
4109   /// Provide fast operand accessors
4110   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4111
4112   /// Convenience accessors.
4113   CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4114   void setCatchPad(CatchPadInst *CatchPad) {
4115     assert(CatchPad);
4116     Op<0>() = CatchPad;
4117   }
4118
4119   BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4120   void setSuccessor(BasicBlock *NewSucc) {
4121     assert(NewSucc);
4122     Op<1>() = NewSucc;
4123   }
4124   unsigned getNumSuccessors() const { return 1; }
4125
4126   // Methods for support type inquiry through isa, cast, and dyn_cast:
4127   static inline bool classof(const Instruction *I) {
4128     return (I->getOpcode() == Instruction::CatchRet);
4129   }
4130   static inline bool classof(const Value *V) {
4131     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4132   }
4133
4134 private:
4135   BasicBlock *getSuccessorV(unsigned Idx) const override;
4136   unsigned getNumSuccessorsV() const override;
4137   void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4138 };
4139
4140 template <>
4141 struct OperandTraits<CatchReturnInst>
4142     : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4143
4144 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)
4145
4146 //===----------------------------------------------------------------------===//
4147 //                               CleanupEndPadInst Class
4148 //===----------------------------------------------------------------------===//
4149
4150 class CleanupEndPadInst : public TerminatorInst {
4151 private:
4152   CleanupEndPadInst(const CleanupEndPadInst &CEPI);
4153
4154   void init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB);
4155   CleanupEndPadInst(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB,
4156                     unsigned Values, Instruction *InsertBefore = nullptr);
4157   CleanupEndPadInst(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB,
4158                     unsigned Values, BasicBlock *InsertAtEnd);
4159
4160 protected:
4161   // Note: Instruction needs to be a friend here to call cloneImpl.
4162   friend class Instruction;
4163   CleanupEndPadInst *cloneImpl() const;
4164
4165 public:
4166   static CleanupEndPadInst *Create(CleanupPadInst *CleanupPad,
4167                                    BasicBlock *UnwindBB = nullptr,
4168                                    Instruction *InsertBefore = nullptr) {
4169     unsigned Values = UnwindBB ? 2 : 1;
4170     return new (Values)
4171         CleanupEndPadInst(CleanupPad, UnwindBB, Values, InsertBefore);
4172   }
4173   static CleanupEndPadInst *Create(CleanupPadInst *CleanupPad,
4174                                    BasicBlock *UnwindBB,
4175                                    BasicBlock *InsertAtEnd) {
4176     unsigned Values = UnwindBB ? 2 : 1;
4177     return new (Values)
4178         CleanupEndPadInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4179   }
4180
4181   /// Provide fast operand accessors
4182   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4183
4184   bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4185   bool unwindsToCaller() const { return !hasUnwindDest(); }
4186
4187   unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4188
4189   /// Convenience accessors
4190   CleanupPadInst *getCleanupPad() const {
4191     return cast<CleanupPadInst>(Op<-1>());
4192   }
4193   void setCleanupPad(CleanupPadInst *CleanupPad) {
4194     assert(CleanupPad);
4195     Op<-1>() = CleanupPad;
4196   }
4197
4198   BasicBlock *getUnwindDest() const {
4199     return hasUnwindDest() ? cast<BasicBlock>(Op<-2>()) : nullptr;
4200   }
4201   void setUnwindDest(BasicBlock *NewDest) {
4202     assert(hasUnwindDest());
4203     assert(NewDest);
4204     Op<-2>() = NewDest;
4205   }
4206
4207   // Methods for support type inquiry through isa, cast, and dyn_cast:
4208   static inline bool classof(const Instruction *I) {
4209     return (I->getOpcode() == Instruction::CleanupEndPad);
4210   }
4211   static inline bool classof(const Value *V) {
4212     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4213   }
4214
4215 private:
4216   BasicBlock *getSuccessorV(unsigned Idx) const override;
4217   unsigned getNumSuccessorsV() const override;
4218   void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4219
4220   // Shadow Instruction::setInstructionSubclassData with a private forwarding
4221   // method so that subclasses cannot accidentally use it.
4222   void setInstructionSubclassData(unsigned short D) {
4223     Instruction::setInstructionSubclassData(D);
4224   }
4225 };
4226
4227 template <>
4228 struct OperandTraits<CleanupEndPadInst>
4229     : public VariadicOperandTraits<CleanupEndPadInst, /*MINARITY=*/1> {};
4230
4231 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupEndPadInst, Value)
4232
4233 //===----------------------------------------------------------------------===//
4234 //                               CleanupReturnInst Class
4235 //===----------------------------------------------------------------------===//
4236
4237 class CleanupReturnInst : public TerminatorInst {
4238 private:
4239   CleanupReturnInst(const CleanupReturnInst &RI);
4240
4241   void init(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB);
4242   CleanupReturnInst(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB,
4243                     unsigned Values, Instruction *InsertBefore = nullptr);
4244   CleanupReturnInst(CleanupPadInst *CleanupPad, BasicBlock *UnwindBB,
4245                     unsigned Values, BasicBlock *InsertAtEnd);
4246
4247 protected:
4248   // Note: Instruction needs to be a friend here to call cloneImpl.
4249   friend class Instruction;
4250   CleanupReturnInst *cloneImpl() const;
4251
4252 public:
4253   static CleanupReturnInst *Create(CleanupPadInst *CleanupPad,
4254                                    BasicBlock *UnwindBB = nullptr,
4255                                    Instruction *InsertBefore = nullptr) {
4256     assert(CleanupPad);
4257     unsigned Values = 1;
4258     if (UnwindBB)
4259       ++Values;
4260     return new (Values)
4261         CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4262   }
4263   static CleanupReturnInst *Create(CleanupPadInst *CleanupPad,
4264                                    BasicBlock *UnwindBB,
4265                                    BasicBlock *InsertAtEnd) {
4266     assert(CleanupPad);
4267     unsigned Values = 1;
4268     if (UnwindBB)
4269       ++Values;
4270     return new (Values)
4271         CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4272   }
4273
4274   /// Provide fast operand accessors
4275   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4276
4277   bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4278   bool unwindsToCaller() const { return !hasUnwindDest(); }
4279
4280   /// Convenience accessor.
4281   CleanupPadInst *getCleanupPad() const {
4282     return cast<CleanupPadInst>(Op<-1>());
4283   }
4284   void setCleanupPad(CleanupPadInst *CleanupPad) {
4285     assert(CleanupPad);
4286     Op<-1>() = CleanupPad;
4287   }
4288
4289   unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4290
4291   BasicBlock *getUnwindDest() const {
4292     return hasUnwindDest() ? cast<BasicBlock>(Op<-2>()) : nullptr;
4293   }
4294   void setUnwindDest(BasicBlock *NewDest) {
4295     assert(NewDest);
4296     assert(hasUnwindDest());
4297     Op<-2>() = NewDest;
4298   }
4299
4300   // Methods for support type inquiry through isa, cast, and dyn_cast:
4301   static inline bool classof(const Instruction *I) {
4302     return (I->getOpcode() == Instruction::CleanupRet);
4303   }
4304   static inline bool classof(const Value *V) {
4305     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4306   }
4307
4308 private:
4309   BasicBlock *getSuccessorV(unsigned Idx) const override;
4310   unsigned getNumSuccessorsV() const override;
4311   void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4312
4313   // Shadow Instruction::setInstructionSubclassData with a private forwarding
4314   // method so that subclasses cannot accidentally use it.
4315   void setInstructionSubclassData(unsigned short D) {
4316     Instruction::setInstructionSubclassData(D);
4317   }
4318 };
4319
4320 template <>
4321 struct OperandTraits<CleanupReturnInst>
4322     : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4323
4324 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)
4325
4326 //===----------------------------------------------------------------------===//
4327 //                           UnreachableInst Class
4328 //===----------------------------------------------------------------------===//
4329
4330 //===---------------------------------------------------------------------------
4331 /// UnreachableInst - This function has undefined behavior.  In particular, the
4332 /// presence of this instruction indicates some higher level knowledge that the
4333 /// end of the block cannot be reached.
4334 ///
4335 class UnreachableInst : public TerminatorInst {
4336   void *operator new(size_t, unsigned) = delete;
4337
4338 protected:
4339   // Note: Instruction needs to be a friend here to call cloneImpl.
4340   friend class Instruction;
4341   UnreachableInst *cloneImpl() const;
4342
4343 public:
4344   // allocate space for exactly zero operands
4345   void *operator new(size_t s) {
4346     return User::operator new(s, 0);
4347   }
4348   explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
4349   explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
4350
4351   unsigned getNumSuccessors() const { return 0; }
4352
4353   // Methods for support type inquiry through isa, cast, and dyn_cast:
4354   static inline bool classof(const Instruction *I) {
4355     return I->getOpcode() == Instruction::Unreachable;
4356   }
4357   static inline bool classof(const Value *V) {
4358     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4359   }
4360
4361 private:
4362   BasicBlock *getSuccessorV(unsigned idx) const override;
4363   unsigned getNumSuccessorsV() const override;
4364   void setSuccessorV(unsigned idx, BasicBlock *B) override;
4365 };
4366
4367 //===----------------------------------------------------------------------===//
4368 //                                 TruncInst Class
4369 //===----------------------------------------------------------------------===//
4370
4371 /// \brief This class represents a truncation of integer types.
4372 class TruncInst : public CastInst {
4373 protected:
4374   // Note: Instruction needs to be a friend here to call cloneImpl.
4375   friend class Instruction;
4376   /// \brief Clone an identical TruncInst
4377   TruncInst *cloneImpl() const;
4378
4379 public:
4380   /// \brief Constructor with insert-before-instruction semantics
4381   TruncInst(
4382     Value *S,                           ///< The value to be truncated
4383     Type *Ty,                           ///< The (smaller) type to truncate to
4384     const Twine &NameStr = "",          ///< A name for the new instruction
4385     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4386   );
4387
4388   /// \brief Constructor with insert-at-end-of-block semantics
4389   TruncInst(
4390     Value *S,                     ///< The value to be truncated
4391     Type *Ty,                     ///< The (smaller) type to truncate to
4392     const Twine &NameStr,         ///< A name for the new instruction
4393     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4394   );
4395
4396   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4397   static inline bool classof(const Instruction *I) {
4398     return I->getOpcode() == Trunc;
4399   }
4400   static inline bool classof(const Value *V) {
4401     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4402   }
4403 };
4404
4405 //===----------------------------------------------------------------------===//
4406 //                                 ZExtInst Class
4407 //===----------------------------------------------------------------------===//
4408
4409 /// \brief This class represents zero extension of integer types.
4410 class ZExtInst : public CastInst {
4411 protected:
4412   // Note: Instruction needs to be a friend here to call cloneImpl.
4413   friend class Instruction;
4414   /// \brief Clone an identical ZExtInst
4415   ZExtInst *cloneImpl() const;
4416
4417 public:
4418   /// \brief Constructor with insert-before-instruction semantics
4419   ZExtInst(
4420     Value *S,                           ///< The value to be zero extended
4421     Type *Ty,                           ///< The type to zero extend to
4422     const Twine &NameStr = "",          ///< A name for the new instruction
4423     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4424   );
4425
4426   /// \brief Constructor with insert-at-end semantics.
4427   ZExtInst(
4428     Value *S,                     ///< The value to be zero extended
4429     Type *Ty,                     ///< The type to zero extend to
4430     const Twine &NameStr,         ///< A name for the new instruction
4431     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4432   );
4433
4434   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4435   static inline bool classof(const Instruction *I) {
4436     return I->getOpcode() == ZExt;
4437   }
4438   static inline bool classof(const Value *V) {
4439     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4440   }
4441 };
4442
4443 //===----------------------------------------------------------------------===//
4444 //                                 SExtInst Class
4445 //===----------------------------------------------------------------------===//
4446
4447 /// \brief This class represents a sign extension of integer types.
4448 class SExtInst : public CastInst {
4449 protected:
4450   // Note: Instruction needs to be a friend here to call cloneImpl.
4451   friend class Instruction;
4452   /// \brief Clone an identical SExtInst
4453   SExtInst *cloneImpl() const;
4454
4455 public:
4456   /// \brief Constructor with insert-before-instruction semantics
4457   SExtInst(
4458     Value *S,                           ///< The value to be sign extended
4459     Type *Ty,                           ///< The type to sign extend to
4460     const Twine &NameStr = "",          ///< A name for the new instruction
4461     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4462   );
4463
4464   /// \brief Constructor with insert-at-end-of-block semantics
4465   SExtInst(
4466     Value *S,                     ///< The value to be sign extended
4467     Type *Ty,                     ///< The type to sign extend to
4468     const Twine &NameStr,         ///< A name for the new instruction
4469     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4470   );
4471
4472   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4473   static inline bool classof(const Instruction *I) {
4474     return I->getOpcode() == SExt;
4475   }
4476   static inline bool classof(const Value *V) {
4477     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4478   }
4479 };
4480
4481 //===----------------------------------------------------------------------===//
4482 //                                 FPTruncInst Class
4483 //===----------------------------------------------------------------------===//
4484
4485 /// \brief This class represents a truncation of floating point types.
4486 class FPTruncInst : public CastInst {
4487 protected:
4488   // Note: Instruction needs to be a friend here to call cloneImpl.
4489   friend class Instruction;
4490   /// \brief Clone an identical FPTruncInst
4491   FPTruncInst *cloneImpl() const;
4492
4493 public:
4494   /// \brief Constructor with insert-before-instruction semantics
4495   FPTruncInst(
4496     Value *S,                           ///< The value to be truncated
4497     Type *Ty,                           ///< The type to truncate to
4498     const Twine &NameStr = "",          ///< A name for the new instruction
4499     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4500   );
4501
4502   /// \brief Constructor with insert-before-instruction semantics
4503   FPTruncInst(
4504     Value *S,                     ///< The value to be truncated
4505     Type *Ty,                     ///< The type to truncate to
4506     const Twine &NameStr,         ///< A name for the new instruction
4507     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4508   );
4509
4510   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4511   static inline bool classof(const Instruction *I) {
4512     return I->getOpcode() == FPTrunc;
4513   }
4514   static inline bool classof(const Value *V) {
4515     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4516   }
4517 };
4518
4519 //===----------------------------------------------------------------------===//
4520 //                                 FPExtInst Class
4521 //===----------------------------------------------------------------------===//
4522
4523 /// \brief This class represents an extension of floating point types.
4524 class FPExtInst : public CastInst {
4525 protected:
4526   // Note: Instruction needs to be a friend here to call cloneImpl.
4527   friend class Instruction;
4528   /// \brief Clone an identical FPExtInst
4529   FPExtInst *cloneImpl() const;
4530
4531 public:
4532   /// \brief Constructor with insert-before-instruction semantics
4533   FPExtInst(
4534     Value *S,                           ///< The value to be extended
4535     Type *Ty,                           ///< The type to extend to
4536     const Twine &NameStr = "",          ///< A name for the new instruction
4537     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4538   );
4539
4540   /// \brief Constructor with insert-at-end-of-block semantics
4541   FPExtInst(
4542     Value *S,                     ///< The value to be extended
4543     Type *Ty,                     ///< The type to extend to
4544     const Twine &NameStr,         ///< A name for the new instruction
4545     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4546   );
4547
4548   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4549   static inline bool classof(const Instruction *I) {
4550     return I->getOpcode() == FPExt;
4551   }
4552   static inline bool classof(const Value *V) {
4553     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4554   }
4555 };
4556
4557 //===----------------------------------------------------------------------===//
4558 //                                 UIToFPInst Class
4559 //===----------------------------------------------------------------------===//
4560
4561 /// \brief This class represents a cast unsigned integer to floating point.
4562 class UIToFPInst : public CastInst {
4563 protected:
4564   // Note: Instruction needs to be a friend here to call cloneImpl.
4565   friend class Instruction;
4566   /// \brief Clone an identical UIToFPInst
4567   UIToFPInst *cloneImpl() const;
4568
4569 public:
4570   /// \brief Constructor with insert-before-instruction semantics
4571   UIToFPInst(
4572     Value *S,                           ///< The value to be converted
4573     Type *Ty,                           ///< The type to convert to
4574     const Twine &NameStr = "",          ///< A name for the new instruction
4575     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4576   );
4577
4578   /// \brief Constructor with insert-at-end-of-block semantics
4579   UIToFPInst(
4580     Value *S,                     ///< The value to be converted
4581     Type *Ty,                     ///< The type to convert to
4582     const Twine &NameStr,         ///< A name for the new instruction
4583     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4584   );
4585
4586   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4587   static inline bool classof(const Instruction *I) {
4588     return I->getOpcode() == UIToFP;
4589   }
4590   static inline bool classof(const Value *V) {
4591     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4592   }
4593 };
4594
4595 //===----------------------------------------------------------------------===//
4596 //                                 SIToFPInst Class
4597 //===----------------------------------------------------------------------===//
4598
4599 /// \brief This class represents a cast from signed integer to floating point.
4600 class SIToFPInst : public CastInst {
4601 protected:
4602   // Note: Instruction needs to be a friend here to call cloneImpl.
4603   friend class Instruction;
4604   /// \brief Clone an identical SIToFPInst
4605   SIToFPInst *cloneImpl() const;
4606
4607 public:
4608   /// \brief Constructor with insert-before-instruction semantics
4609   SIToFPInst(
4610     Value *S,                           ///< The value to be converted
4611     Type *Ty,                           ///< The type to convert to
4612     const Twine &NameStr = "",          ///< A name for the new instruction
4613     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4614   );
4615
4616   /// \brief Constructor with insert-at-end-of-block semantics
4617   SIToFPInst(
4618     Value *S,                     ///< The value to be converted
4619     Type *Ty,                     ///< The type to convert to
4620     const Twine &NameStr,         ///< A name for the new instruction
4621     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4622   );
4623
4624   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4625   static inline bool classof(const Instruction *I) {
4626     return I->getOpcode() == SIToFP;
4627   }
4628   static inline bool classof(const Value *V) {
4629     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4630   }
4631 };
4632
4633 //===----------------------------------------------------------------------===//
4634 //                                 FPToUIInst Class
4635 //===----------------------------------------------------------------------===//
4636
4637 /// \brief This class represents a cast from floating point to unsigned integer
4638 class FPToUIInst  : public CastInst {
4639 protected:
4640   // Note: Instruction needs to be a friend here to call cloneImpl.
4641   friend class Instruction;
4642   /// \brief Clone an identical FPToUIInst
4643   FPToUIInst *cloneImpl() const;
4644
4645 public:
4646   /// \brief Constructor with insert-before-instruction semantics
4647   FPToUIInst(
4648     Value *S,                           ///< The value to be converted
4649     Type *Ty,                           ///< The type to convert to
4650     const Twine &NameStr = "",          ///< A name for the new instruction
4651     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4652   );
4653
4654   /// \brief Constructor with insert-at-end-of-block semantics
4655   FPToUIInst(
4656     Value *S,                     ///< The value to be converted
4657     Type *Ty,                     ///< The type to convert to
4658     const Twine &NameStr,         ///< A name for the new instruction
4659     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
4660   );
4661
4662   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4663   static inline bool classof(const Instruction *I) {
4664     return I->getOpcode() == FPToUI;
4665   }
4666   static inline bool classof(const Value *V) {
4667     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4668   }
4669 };
4670
4671 //===----------------------------------------------------------------------===//
4672 //                                 FPToSIInst Class
4673 //===----------------------------------------------------------------------===//
4674
4675 /// \brief This class represents a cast from floating point to signed integer.
4676 class FPToSIInst  : public CastInst {
4677 protected:
4678   // Note: Instruction needs to be a friend here to call cloneImpl.
4679   friend class Instruction;
4680   /// \brief Clone an identical FPToSIInst
4681   FPToSIInst *cloneImpl() const;
4682
4683 public:
4684   /// \brief Constructor with insert-before-instruction semantics
4685   FPToSIInst(
4686     Value *S,                           ///< The value to be converted
4687     Type *Ty,                           ///< The type to convert to
4688     const Twine &NameStr = "",          ///< A name for the new instruction
4689     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4690   );
4691
4692   /// \brief Constructor with insert-at-end-of-block semantics
4693   FPToSIInst(
4694     Value *S,                     ///< The value to be converted
4695     Type *Ty,                     ///< The type to convert to
4696     const Twine &NameStr,         ///< A name for the new instruction
4697     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4698   );
4699
4700   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4701   static inline bool classof(const Instruction *I) {
4702     return I->getOpcode() == FPToSI;
4703   }
4704   static inline bool classof(const Value *V) {
4705     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4706   }
4707 };
4708
4709 //===----------------------------------------------------------------------===//
4710 //                                 IntToPtrInst Class
4711 //===----------------------------------------------------------------------===//
4712
4713 /// \brief This class represents a cast from an integer to a pointer.
4714 class IntToPtrInst : public CastInst {
4715 public:
4716   /// \brief Constructor with insert-before-instruction semantics
4717   IntToPtrInst(
4718     Value *S,                           ///< The value to be converted
4719     Type *Ty,                           ///< The type to convert to
4720     const Twine &NameStr = "",          ///< A name for the new instruction
4721     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4722   );
4723
4724   /// \brief Constructor with insert-at-end-of-block semantics
4725   IntToPtrInst(
4726     Value *S,                     ///< The value to be converted
4727     Type *Ty,                     ///< The type to convert to
4728     const Twine &NameStr,         ///< A name for the new instruction
4729     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4730   );
4731
4732   // Note: Instruction needs to be a friend here to call cloneImpl.
4733   friend class Instruction;
4734   /// \brief Clone an identical IntToPtrInst
4735   IntToPtrInst *cloneImpl() const;
4736
4737   /// \brief Returns the address space of this instruction's pointer type.
4738   unsigned getAddressSpace() const {
4739     return getType()->getPointerAddressSpace();
4740   }
4741
4742   // Methods for support type inquiry through isa, cast, and dyn_cast:
4743   static inline bool classof(const Instruction *I) {
4744     return I->getOpcode() == IntToPtr;
4745   }
4746   static inline bool classof(const Value *V) {
4747     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4748   }
4749 };
4750
4751 //===----------------------------------------------------------------------===//
4752 //                                 PtrToIntInst Class
4753 //===----------------------------------------------------------------------===//
4754
4755 /// \brief This class represents a cast from a pointer to an integer
4756 class PtrToIntInst : public CastInst {
4757 protected:
4758   // Note: Instruction needs to be a friend here to call cloneImpl.
4759   friend class Instruction;
4760   /// \brief Clone an identical PtrToIntInst
4761   PtrToIntInst *cloneImpl() const;
4762
4763 public:
4764   /// \brief Constructor with insert-before-instruction semantics
4765   PtrToIntInst(
4766     Value *S,                           ///< The value to be converted
4767     Type *Ty,                           ///< The type to convert to
4768     const Twine &NameStr = "",          ///< A name for the new instruction
4769     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4770   );
4771
4772   /// \brief Constructor with insert-at-end-of-block semantics
4773   PtrToIntInst(
4774     Value *S,                     ///< The value to be converted
4775     Type *Ty,                     ///< The type to convert to
4776     const Twine &NameStr,         ///< A name for the new instruction
4777     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4778   );
4779
4780   /// \brief Gets the pointer operand.
4781   Value *getPointerOperand() { return getOperand(0); }
4782   /// \brief Gets the pointer operand.
4783   const Value *getPointerOperand() const { return getOperand(0); }
4784   /// \brief Gets the operand index of the pointer operand.
4785   static unsigned getPointerOperandIndex() { return 0U; }
4786
4787   /// \brief Returns the address space of the pointer operand.
4788   unsigned getPointerAddressSpace() const {
4789     return getPointerOperand()->getType()->getPointerAddressSpace();
4790   }
4791
4792   // Methods for support type inquiry through isa, cast, and dyn_cast:
4793   static inline bool classof(const Instruction *I) {
4794     return I->getOpcode() == PtrToInt;
4795   }
4796   static inline bool classof(const Value *V) {
4797     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4798   }
4799 };
4800
4801 //===----------------------------------------------------------------------===//
4802 //                             BitCastInst Class
4803 //===----------------------------------------------------------------------===//
4804
4805 /// \brief This class represents a no-op cast from one type to another.
4806 class BitCastInst : public CastInst {
4807 protected:
4808   // Note: Instruction needs to be a friend here to call cloneImpl.
4809   friend class Instruction;
4810   /// \brief Clone an identical BitCastInst
4811   BitCastInst *cloneImpl() const;
4812
4813 public:
4814   /// \brief Constructor with insert-before-instruction semantics
4815   BitCastInst(
4816     Value *S,                           ///< The value to be casted
4817     Type *Ty,                           ///< The type to casted to
4818     const Twine &NameStr = "",          ///< A name for the new instruction
4819     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4820   );
4821
4822   /// \brief Constructor with insert-at-end-of-block semantics
4823   BitCastInst(
4824     Value *S,                     ///< The value to be casted
4825     Type *Ty,                     ///< The type to casted to
4826     const Twine &NameStr,         ///< A name for the new instruction
4827     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4828   );
4829
4830   // Methods for support type inquiry through isa, cast, and dyn_cast:
4831   static inline bool classof(const Instruction *I) {
4832     return I->getOpcode() == BitCast;
4833   }
4834   static inline bool classof(const Value *V) {
4835     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4836   }
4837 };
4838
4839 //===----------------------------------------------------------------------===//
4840 //                          AddrSpaceCastInst Class
4841 //===----------------------------------------------------------------------===//
4842
4843 /// \brief This class represents a conversion between pointers from
4844 /// one address space to another.
4845 class AddrSpaceCastInst : public CastInst {
4846 protected:
4847   // Note: Instruction needs to be a friend here to call cloneImpl.
4848   friend class Instruction;
4849   /// \brief Clone an identical AddrSpaceCastInst
4850   AddrSpaceCastInst *cloneImpl() const;
4851
4852 public:
4853   /// \brief Constructor with insert-before-instruction semantics
4854   AddrSpaceCastInst(
4855     Value *S,                           ///< The value to be casted
4856     Type *Ty,                           ///< The type to casted to
4857     const Twine &NameStr = "",          ///< A name for the new instruction
4858     Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4859   );
4860
4861   /// \brief Constructor with insert-at-end-of-block semantics
4862   AddrSpaceCastInst(
4863     Value *S,                     ///< The value to be casted
4864     Type *Ty,                     ///< The type to casted to
4865     const Twine &NameStr,         ///< A name for the new instruction
4866     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
4867   );
4868
4869   // Methods for support type inquiry through isa, cast, and dyn_cast:
4870   static inline bool classof(const Instruction *I) {
4871     return I->getOpcode() == AddrSpaceCast;
4872   }
4873   static inline bool classof(const Value *V) {
4874     return isa<Instruction>(V) && classof(cast<Instruction>(V));
4875   }
4876 };
4877
4878 } // End llvm namespace
4879
4880 #endif