c249f878c4c4174fe98604a76313d6cf0a35cb4d
[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/IR/Attributes.h"
22 #include "llvm/IR/CallingConv.h"
23 #include "llvm/IR/DerivedTypes.h"
24 #include "llvm/IR/InstrTypes.h"
25 #include "llvm/Support/ErrorHandling.h"
26 #include <iterator>
27
28 namespace llvm {
29
30 class APInt;
31 class ConstantInt;
32 class ConstantRange;
33 class DataLayout;
34 class LLVMContext;
35
36 enum AtomicOrdering {
37   NotAtomic = 0,
38   Unordered = 1,
39   Monotonic = 2,
40   // Consume = 3,  // Not specified yet.
41   Acquire = 4,
42   Release = 5,
43   AcquireRelease = 6,
44   SequentiallyConsistent = 7
45 };
46
47 enum SynchronizationScope {
48   SingleThread = 0,
49   CrossThread = 1
50 };
51
52 //===----------------------------------------------------------------------===//
53 //                                AllocaInst Class
54 //===----------------------------------------------------------------------===//
55
56 /// AllocaInst - an instruction to allocate memory on the stack
57 ///
58 class AllocaInst : public UnaryInstruction {
59 protected:
60   AllocaInst *clone_impl() const override;
61 public:
62   explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
63                       const Twine &Name = "", Instruction *InsertBefore = 0);
64   AllocaInst(Type *Ty, Value *ArraySize,
65              const Twine &Name, BasicBlock *InsertAtEnd);
66
67   AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
68   AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
69
70   AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
71              const Twine &Name = "", Instruction *InsertBefore = 0);
72   AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
73              const Twine &Name, BasicBlock *InsertAtEnd);
74
75   // Out of line virtual method, so the vtable, etc. has a home.
76   virtual ~AllocaInst();
77
78   /// isArrayAllocation - Return true if there is an allocation size parameter
79   /// to the allocation instruction that is not 1.
80   ///
81   bool isArrayAllocation() const;
82
83   /// getArraySize - Get the number of elements allocated. For a simple
84   /// allocation of a single element, this will return a constant 1 value.
85   ///
86   const Value *getArraySize() const { return getOperand(0); }
87   Value *getArraySize() { return getOperand(0); }
88
89   /// getType - Overload to return most specific pointer type
90   ///
91   PointerType *getType() const {
92     return cast<PointerType>(Instruction::getType());
93   }
94
95   /// getAllocatedType - Return the type that is being allocated by the
96   /// instruction.
97   ///
98   Type *getAllocatedType() const;
99
100   /// getAlignment - Return the alignment of the memory that is being allocated
101   /// by the instruction.
102   ///
103   unsigned getAlignment() const {
104     return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
105   }
106   void setAlignment(unsigned Align);
107
108   /// isStaticAlloca - Return true if this alloca is in the entry block of the
109   /// function and is a constant size.  If so, the code generator will fold it
110   /// into the prolog/epilog code, so it is basically free.
111   bool isStaticAlloca() const;
112
113   /// \brief Return true if this alloca is used as an inalloca argument to a
114   /// call.  Such allocas are never considered static even if they are in the
115   /// entry block.
116   bool isUsedWithInAlloca() const {
117     return getSubclassDataFromInstruction() & 32;
118   }
119
120   /// \brief Specify whether this alloca is used to represent a the arguments to
121   /// a call.
122   void setUsedWithInAlloca(bool V) {
123     setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
124                                (V ? 32 : 0));
125   }
126
127   // Methods for support type inquiry through isa, cast, and dyn_cast:
128   static inline bool classof(const Instruction *I) {
129     return (I->getOpcode() == Instruction::Alloca);
130   }
131   static inline bool classof(const Value *V) {
132     return isa<Instruction>(V) && classof(cast<Instruction>(V));
133   }
134 private:
135   // Shadow Instruction::setInstructionSubclassData with a private forwarding
136   // method so that subclasses cannot accidentally use it.
137   void setInstructionSubclassData(unsigned short D) {
138     Instruction::setInstructionSubclassData(D);
139   }
140 };
141
142
143 //===----------------------------------------------------------------------===//
144 //                                LoadInst Class
145 //===----------------------------------------------------------------------===//
146
147 /// LoadInst - an instruction for reading from memory.  This uses the
148 /// SubclassData field in Value to store whether or not the load is volatile.
149 ///
150 class LoadInst : public UnaryInstruction {
151   void AssertOK();
152 protected:
153   LoadInst *clone_impl() const override;
154 public:
155   LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
156   LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
157   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
158            Instruction *InsertBefore = 0);
159   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
160            BasicBlock *InsertAtEnd);
161   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
162            unsigned Align, Instruction *InsertBefore = 0);
163   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
164            unsigned Align, BasicBlock *InsertAtEnd);
165   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
166            unsigned Align, AtomicOrdering Order,
167            SynchronizationScope SynchScope = CrossThread,
168            Instruction *InsertBefore = 0);
169   LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
170            unsigned Align, AtomicOrdering Order,
171            SynchronizationScope SynchScope,
172            BasicBlock *InsertAtEnd);
173
174   LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
175   LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
176   explicit LoadInst(Value *Ptr, const char *NameStr = 0,
177                     bool isVolatile = false,  Instruction *InsertBefore = 0);
178   LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
179            BasicBlock *InsertAtEnd);
180
181   /// isVolatile - Return true if this is a load from a volatile memory
182   /// location.
183   ///
184   bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
185
186   /// setVolatile - Specify whether this is a volatile load or not.
187   ///
188   void setVolatile(bool V) {
189     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
190                                (V ? 1 : 0));
191   }
192
193   /// getAlignment - Return the alignment of the access that is being performed
194   ///
195   unsigned getAlignment() const {
196     return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
197   }
198
199   void setAlignment(unsigned Align);
200
201   /// Returns the ordering effect of this fence.
202   AtomicOrdering getOrdering() const {
203     return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
204   }
205
206   /// Set the ordering constraint on this load. May not be Release or
207   /// AcquireRelease.
208   void setOrdering(AtomicOrdering Ordering) {
209     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
210                                (Ordering << 7));
211   }
212
213   SynchronizationScope getSynchScope() const {
214     return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
215   }
216
217   /// Specify whether this load is ordered with respect to all
218   /// concurrently executing threads, or only with respect to signal handlers
219   /// executing in the same thread.
220   void setSynchScope(SynchronizationScope xthread) {
221     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
222                                (xthread << 6));
223   }
224
225   bool isAtomic() const { return getOrdering() != NotAtomic; }
226   void setAtomic(AtomicOrdering Ordering,
227                  SynchronizationScope SynchScope = CrossThread) {
228     setOrdering(Ordering);
229     setSynchScope(SynchScope);
230   }
231
232   bool isSimple() const { return !isAtomic() && !isVolatile(); }
233   bool isUnordered() const {
234     return getOrdering() <= Unordered && !isVolatile();
235   }
236
237   Value *getPointerOperand() { return getOperand(0); }
238   const Value *getPointerOperand() const { return getOperand(0); }
239   static unsigned getPointerOperandIndex() { return 0U; }
240
241   /// \brief Returns the address space of the pointer operand.
242   unsigned getPointerAddressSpace() const {
243     return getPointerOperand()->getType()->getPointerAddressSpace();
244   }
245
246
247   // Methods for support type inquiry through isa, cast, and dyn_cast:
248   static inline bool classof(const Instruction *I) {
249     return I->getOpcode() == Instruction::Load;
250   }
251   static inline bool classof(const Value *V) {
252     return isa<Instruction>(V) && classof(cast<Instruction>(V));
253   }
254 private:
255   // Shadow Instruction::setInstructionSubclassData with a private forwarding
256   // method so that subclasses cannot accidentally use it.
257   void setInstructionSubclassData(unsigned short D) {
258     Instruction::setInstructionSubclassData(D);
259   }
260 };
261
262
263 //===----------------------------------------------------------------------===//
264 //                                StoreInst Class
265 //===----------------------------------------------------------------------===//
266
267 /// StoreInst - an instruction for storing to memory
268 ///
269 class StoreInst : public Instruction {
270   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
271   void AssertOK();
272 protected:
273   StoreInst *clone_impl() const override;
274 public:
275   // allocate space for exactly two operands
276   void *operator new(size_t s) {
277     return User::operator new(s, 2);
278   }
279   StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
280   StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
281   StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
282             Instruction *InsertBefore = 0);
283   StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
284   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
285             unsigned Align, Instruction *InsertBefore = 0);
286   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
287             unsigned Align, BasicBlock *InsertAtEnd);
288   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
289             unsigned Align, AtomicOrdering Order,
290             SynchronizationScope SynchScope = CrossThread,
291             Instruction *InsertBefore = 0);
292   StoreInst(Value *Val, Value *Ptr, bool isVolatile,
293             unsigned Align, AtomicOrdering Order,
294             SynchronizationScope SynchScope,
295             BasicBlock *InsertAtEnd);
296
297
298   /// isVolatile - Return true if this is a store to a volatile memory
299   /// location.
300   ///
301   bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
302
303   /// setVolatile - Specify whether this is a volatile store or not.
304   ///
305   void setVolatile(bool V) {
306     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
307                                (V ? 1 : 0));
308   }
309
310   /// Transparently provide more efficient getOperand methods.
311   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
312
313   /// getAlignment - Return the alignment of the access that is being performed
314   ///
315   unsigned getAlignment() const {
316     return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
317   }
318
319   void setAlignment(unsigned Align);
320
321   /// Returns the ordering effect of this store.
322   AtomicOrdering getOrdering() const {
323     return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
324   }
325
326   /// Set the ordering constraint on this store.  May not be Acquire or
327   /// AcquireRelease.
328   void setOrdering(AtomicOrdering Ordering) {
329     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
330                                (Ordering << 7));
331   }
332
333   SynchronizationScope getSynchScope() const {
334     return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
335   }
336
337   /// Specify whether this store instruction is ordered with respect to all
338   /// concurrently executing threads, or only with respect to signal handlers
339   /// executing in the same thread.
340   void setSynchScope(SynchronizationScope xthread) {
341     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
342                                (xthread << 6));
343   }
344
345   bool isAtomic() const { return getOrdering() != NotAtomic; }
346   void setAtomic(AtomicOrdering Ordering,
347                  SynchronizationScope SynchScope = CrossThread) {
348     setOrdering(Ordering);
349     setSynchScope(SynchScope);
350   }
351
352   bool isSimple() const { return !isAtomic() && !isVolatile(); }
353   bool isUnordered() const {
354     return getOrdering() <= Unordered && !isVolatile();
355   }
356
357   Value *getValueOperand() { return getOperand(0); }
358   const Value *getValueOperand() const { return getOperand(0); }
359
360   Value *getPointerOperand() { return getOperand(1); }
361   const Value *getPointerOperand() const { return getOperand(1); }
362   static unsigned getPointerOperandIndex() { return 1U; }
363
364   /// \brief Returns the address space of the pointer operand.
365   unsigned getPointerAddressSpace() const {
366     return getPointerOperand()->getType()->getPointerAddressSpace();
367   }
368
369   // Methods for support type inquiry through isa, cast, and dyn_cast:
370   static inline bool classof(const Instruction *I) {
371     return I->getOpcode() == Instruction::Store;
372   }
373   static inline bool classof(const Value *V) {
374     return isa<Instruction>(V) && classof(cast<Instruction>(V));
375   }
376 private:
377   // Shadow Instruction::setInstructionSubclassData with a private forwarding
378   // method so that subclasses cannot accidentally use it.
379   void setInstructionSubclassData(unsigned short D) {
380     Instruction::setInstructionSubclassData(D);
381   }
382 };
383
384 template <>
385 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
386 };
387
388 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
389
390 //===----------------------------------------------------------------------===//
391 //                                FenceInst Class
392 //===----------------------------------------------------------------------===//
393
394 /// FenceInst - an instruction for ordering other memory operations
395 ///
396 class FenceInst : public Instruction {
397   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
398   void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
399 protected:
400   FenceInst *clone_impl() const override;
401 public:
402   // allocate space for exactly zero operands
403   void *operator new(size_t s) {
404     return User::operator new(s, 0);
405   }
406
407   // Ordering may only be Acquire, Release, AcquireRelease, or
408   // SequentiallyConsistent.
409   FenceInst(LLVMContext &C, AtomicOrdering Ordering,
410             SynchronizationScope SynchScope = CrossThread,
411             Instruction *InsertBefore = 0);
412   FenceInst(LLVMContext &C, AtomicOrdering Ordering,
413             SynchronizationScope SynchScope,
414             BasicBlock *InsertAtEnd);
415
416   /// Returns the ordering effect of this fence.
417   AtomicOrdering getOrdering() const {
418     return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
419   }
420
421   /// Set the ordering constraint on this fence.  May only be Acquire, Release,
422   /// AcquireRelease, or SequentiallyConsistent.
423   void setOrdering(AtomicOrdering Ordering) {
424     setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
425                                (Ordering << 1));
426   }
427
428   SynchronizationScope getSynchScope() const {
429     return SynchronizationScope(getSubclassDataFromInstruction() & 1);
430   }
431
432   /// Specify whether this fence orders other operations with respect to all
433   /// concurrently executing threads, or only with respect to signal handlers
434   /// executing in the same thread.
435   void setSynchScope(SynchronizationScope xthread) {
436     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
437                                xthread);
438   }
439
440   // Methods for support type inquiry through isa, cast, and dyn_cast:
441   static inline bool classof(const Instruction *I) {
442     return I->getOpcode() == Instruction::Fence;
443   }
444   static inline bool classof(const Value *V) {
445     return isa<Instruction>(V) && classof(cast<Instruction>(V));
446   }
447 private:
448   // Shadow Instruction::setInstructionSubclassData with a private forwarding
449   // method so that subclasses cannot accidentally use it.
450   void setInstructionSubclassData(unsigned short D) {
451     Instruction::setInstructionSubclassData(D);
452   }
453 };
454
455 //===----------------------------------------------------------------------===//
456 //                                AtomicCmpXchgInst Class
457 //===----------------------------------------------------------------------===//
458
459 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
460 /// specified value is in a memory location, and, if it is, stores a new value
461 /// there.  Returns the value that was loaded.
462 ///
463 class AtomicCmpXchgInst : public Instruction {
464   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
465   void Init(Value *Ptr, Value *Cmp, Value *NewVal,
466             AtomicOrdering Ordering, SynchronizationScope SynchScope);
467 protected:
468   AtomicCmpXchgInst *clone_impl() const override;
469 public:
470   // allocate space for exactly three operands
471   void *operator new(size_t s) {
472     return User::operator new(s, 3);
473   }
474   AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
475                     AtomicOrdering Ordering, SynchronizationScope SynchScope,
476                     Instruction *InsertBefore = 0);
477   AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
478                     AtomicOrdering Ordering, SynchronizationScope SynchScope,
479                     BasicBlock *InsertAtEnd);
480
481   /// isVolatile - Return true if this is a cmpxchg from a volatile memory
482   /// location.
483   ///
484   bool isVolatile() const {
485     return getSubclassDataFromInstruction() & 1;
486   }
487
488   /// setVolatile - Specify whether this is a volatile cmpxchg.
489   ///
490   void setVolatile(bool V) {
491      setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
492                                 (unsigned)V);
493   }
494
495   /// Transparently provide more efficient getOperand methods.
496   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
497
498   /// Set the ordering constraint on this cmpxchg.
499   void setOrdering(AtomicOrdering Ordering) {
500     assert(Ordering != NotAtomic &&
501            "CmpXchg instructions can only be atomic.");
502     setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
503                                (Ordering << 2));
504   }
505
506   /// Specify whether this cmpxchg is atomic and orders other operations with
507   /// respect to all concurrently executing threads, or only with respect to
508   /// signal handlers executing in the same thread.
509   void setSynchScope(SynchronizationScope SynchScope) {
510     setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
511                                (SynchScope << 1));
512   }
513
514   /// Returns the ordering constraint on this cmpxchg.
515   AtomicOrdering getOrdering() const {
516     return AtomicOrdering(getSubclassDataFromInstruction() >> 2);
517   }
518
519   /// Returns whether this cmpxchg is atomic between threads or only within a
520   /// single thread.
521   SynchronizationScope getSynchScope() const {
522     return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
523   }
524
525   Value *getPointerOperand() { return getOperand(0); }
526   const Value *getPointerOperand() const { return getOperand(0); }
527   static unsigned getPointerOperandIndex() { return 0U; }
528
529   Value *getCompareOperand() { return getOperand(1); }
530   const Value *getCompareOperand() const { return getOperand(1); }
531
532   Value *getNewValOperand() { return getOperand(2); }
533   const Value *getNewValOperand() const { return getOperand(2); }
534
535   /// \brief Returns the address space of the pointer operand.
536   unsigned getPointerAddressSpace() const {
537     return getPointerOperand()->getType()->getPointerAddressSpace();
538   }
539
540   // Methods for support type inquiry through isa, cast, and dyn_cast:
541   static inline bool classof(const Instruction *I) {
542     return I->getOpcode() == Instruction::AtomicCmpXchg;
543   }
544   static inline bool classof(const Value *V) {
545     return isa<Instruction>(V) && classof(cast<Instruction>(V));
546   }
547 private:
548   // Shadow Instruction::setInstructionSubclassData with a private forwarding
549   // method so that subclasses cannot accidentally use it.
550   void setInstructionSubclassData(unsigned short D) {
551     Instruction::setInstructionSubclassData(D);
552   }
553 };
554
555 template <>
556 struct OperandTraits<AtomicCmpXchgInst> :
557     public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
558 };
559
560 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
561
562 //===----------------------------------------------------------------------===//
563 //                                AtomicRMWInst Class
564 //===----------------------------------------------------------------------===//
565
566 /// AtomicRMWInst - an instruction that atomically reads a memory location,
567 /// combines it with another value, and then stores the result back.  Returns
568 /// the old value.
569 ///
570 class AtomicRMWInst : public Instruction {
571   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
572 protected:
573   AtomicRMWInst *clone_impl() const override;
574 public:
575   /// This enumeration lists the possible modifications atomicrmw can make.  In
576   /// the descriptions, 'p' is the pointer to the instruction's memory location,
577   /// 'old' is the initial value of *p, and 'v' is the other value passed to the
578   /// instruction.  These instructions always return 'old'.
579   enum BinOp {
580     /// *p = v
581     Xchg,
582     /// *p = old + v
583     Add,
584     /// *p = old - v
585     Sub,
586     /// *p = old & v
587     And,
588     /// *p = ~old & v
589     Nand,
590     /// *p = old | v
591     Or,
592     /// *p = old ^ v
593     Xor,
594     /// *p = old >signed v ? old : v
595     Max,
596     /// *p = old <signed v ? old : v
597     Min,
598     /// *p = old >unsigned v ? old : v
599     UMax,
600     /// *p = old <unsigned v ? old : v
601     UMin,
602
603     FIRST_BINOP = Xchg,
604     LAST_BINOP = UMin,
605     BAD_BINOP
606   };
607
608   // allocate space for exactly two operands
609   void *operator new(size_t s) {
610     return User::operator new(s, 2);
611   }
612   AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
613                 AtomicOrdering Ordering, SynchronizationScope SynchScope,
614                 Instruction *InsertBefore = 0);
615   AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
616                 AtomicOrdering Ordering, SynchronizationScope SynchScope,
617                 BasicBlock *InsertAtEnd);
618
619   BinOp getOperation() const {
620     return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
621   }
622
623   void setOperation(BinOp Operation) {
624     unsigned short SubclassData = getSubclassDataFromInstruction();
625     setInstructionSubclassData((SubclassData & 31) |
626                                (Operation << 5));
627   }
628
629   /// isVolatile - Return true if this is a RMW on a volatile memory location.
630   ///
631   bool isVolatile() const {
632     return getSubclassDataFromInstruction() & 1;
633   }
634
635   /// setVolatile - Specify whether this is a volatile RMW or not.
636   ///
637   void setVolatile(bool V) {
638      setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
639                                 (unsigned)V);
640   }
641
642   /// Transparently provide more efficient getOperand methods.
643   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
644
645   /// Set the ordering constraint on this RMW.
646   void setOrdering(AtomicOrdering Ordering) {
647     assert(Ordering != NotAtomic &&
648            "atomicrmw instructions can only be atomic.");
649     setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
650                                (Ordering << 2));
651   }
652
653   /// Specify whether this RMW orders other operations with respect to all
654   /// concurrently executing threads, or only with respect to signal handlers
655   /// executing in the same thread.
656   void setSynchScope(SynchronizationScope SynchScope) {
657     setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
658                                (SynchScope << 1));
659   }
660
661   /// Returns the ordering constraint on this RMW.
662   AtomicOrdering getOrdering() const {
663     return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
664   }
665
666   /// Returns whether this RMW is atomic between threads or only within a
667   /// single thread.
668   SynchronizationScope getSynchScope() const {
669     return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
670   }
671
672   Value *getPointerOperand() { return getOperand(0); }
673   const Value *getPointerOperand() const { return getOperand(0); }
674   static unsigned getPointerOperandIndex() { return 0U; }
675
676   Value *getValOperand() { return getOperand(1); }
677   const Value *getValOperand() const { return getOperand(1); }
678
679   /// \brief Returns the address space of the pointer operand.
680   unsigned getPointerAddressSpace() const {
681     return getPointerOperand()->getType()->getPointerAddressSpace();
682   }
683
684   // Methods for support type inquiry through isa, cast, and dyn_cast:
685   static inline bool classof(const Instruction *I) {
686     return I->getOpcode() == Instruction::AtomicRMW;
687   }
688   static inline bool classof(const Value *V) {
689     return isa<Instruction>(V) && classof(cast<Instruction>(V));
690   }
691 private:
692   void Init(BinOp Operation, Value *Ptr, Value *Val,
693             AtomicOrdering Ordering, SynchronizationScope SynchScope);
694   // Shadow Instruction::setInstructionSubclassData with a private forwarding
695   // method so that subclasses cannot accidentally use it.
696   void setInstructionSubclassData(unsigned short D) {
697     Instruction::setInstructionSubclassData(D);
698   }
699 };
700
701 template <>
702 struct OperandTraits<AtomicRMWInst>
703     : public FixedNumOperandTraits<AtomicRMWInst,2> {
704 };
705
706 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
707
708 //===----------------------------------------------------------------------===//
709 //                             GetElementPtrInst Class
710 //===----------------------------------------------------------------------===//
711
712 // checkGEPType - Simple wrapper function to give a better assertion failure
713 // message on bad indexes for a gep instruction.
714 //
715 inline Type *checkGEPType(Type *Ty) {
716   assert(Ty && "Invalid GetElementPtrInst indices for type!");
717   return Ty;
718 }
719
720 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
721 /// access elements of arrays and structs
722 ///
723 class GetElementPtrInst : public Instruction {
724   GetElementPtrInst(const GetElementPtrInst &GEPI);
725   void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
726
727   /// Constructors - Create a getelementptr instruction with a base pointer an
728   /// list of indices. The first ctor can optionally insert before an existing
729   /// instruction, the second appends the new instruction to the specified
730   /// BasicBlock.
731   inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
732                            unsigned Values, const Twine &NameStr,
733                            Instruction *InsertBefore);
734   inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
735                            unsigned Values, const Twine &NameStr,
736                            BasicBlock *InsertAtEnd);
737 protected:
738   GetElementPtrInst *clone_impl() const override;
739 public:
740   static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
741                                    const Twine &NameStr = "",
742                                    Instruction *InsertBefore = 0) {
743     unsigned Values = 1 + unsigned(IdxList.size());
744     return new(Values)
745       GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
746   }
747   static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
748                                    const Twine &NameStr,
749                                    BasicBlock *InsertAtEnd) {
750     unsigned Values = 1 + unsigned(IdxList.size());
751     return new(Values)
752       GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
753   }
754
755   /// Create an "inbounds" getelementptr. See the documentation for the
756   /// "inbounds" flag in LangRef.html for details.
757   static GetElementPtrInst *CreateInBounds(Value *Ptr,
758                                            ArrayRef<Value *> IdxList,
759                                            const Twine &NameStr = "",
760                                            Instruction *InsertBefore = 0) {
761     GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
762     GEP->setIsInBounds(true);
763     return GEP;
764   }
765   static GetElementPtrInst *CreateInBounds(Value *Ptr,
766                                            ArrayRef<Value *> IdxList,
767                                            const Twine &NameStr,
768                                            BasicBlock *InsertAtEnd) {
769     GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
770     GEP->setIsInBounds(true);
771     return GEP;
772   }
773
774   /// Transparently provide more efficient getOperand methods.
775   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
776
777   // getType - Overload to return most specific sequential type.
778   SequentialType *getType() const {
779     return cast<SequentialType>(Instruction::getType());
780   }
781
782   /// \brief Returns the address space of this instruction's pointer type.
783   unsigned getAddressSpace() const {
784     // Note that this is always the same as the pointer operand's address space
785     // and that is cheaper to compute, so cheat here.
786     return getPointerAddressSpace();
787   }
788
789   /// getIndexedType - Returns the type of the element that would be loaded with
790   /// a load instruction with the specified parameters.
791   ///
792   /// Null is returned if the indices are invalid for the specified
793   /// pointer type.
794   ///
795   static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
796   static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
797   static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
798
799   inline op_iterator       idx_begin()       { return op_begin()+1; }
800   inline const_op_iterator idx_begin() const { return op_begin()+1; }
801   inline op_iterator       idx_end()         { return op_end(); }
802   inline const_op_iterator idx_end()   const { return op_end(); }
803
804   Value *getPointerOperand() {
805     return getOperand(0);
806   }
807   const Value *getPointerOperand() const {
808     return getOperand(0);
809   }
810   static unsigned getPointerOperandIndex() {
811     return 0U;    // get index for modifying correct operand.
812   }
813
814   /// getPointerOperandType - Method to return the pointer operand as a
815   /// PointerType.
816   Type *getPointerOperandType() const {
817     return getPointerOperand()->getType();
818   }
819
820   /// \brief Returns the address space of the pointer operand.
821   unsigned getPointerAddressSpace() const {
822     return getPointerOperandType()->getPointerAddressSpace();
823   }
824
825   /// GetGEPReturnType - Returns the pointer type returned by the GEP
826   /// instruction, which may be a vector of pointers.
827   static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
828     Type *PtrTy = PointerType::get(checkGEPType(
829                                    getIndexedType(Ptr->getType(), IdxList)),
830                                    Ptr->getType()->getPointerAddressSpace());
831     // Vector GEP
832     if (Ptr->getType()->isVectorTy()) {
833       unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
834       return VectorType::get(PtrTy, NumElem);
835     }
836
837     // Scalar GEP
838     return PtrTy;
839   }
840
841   unsigned getNumIndices() const {  // Note: always non-negative
842     return getNumOperands() - 1;
843   }
844
845   bool hasIndices() const {
846     return getNumOperands() > 1;
847   }
848
849   /// hasAllZeroIndices - Return true if all of the indices of this GEP are
850   /// zeros.  If so, the result pointer and the first operand have the same
851   /// value, just potentially different types.
852   bool hasAllZeroIndices() const;
853
854   /// hasAllConstantIndices - Return true if all of the indices of this GEP are
855   /// constant integers.  If so, the result pointer and the first operand have
856   /// a constant offset between them.
857   bool hasAllConstantIndices() const;
858
859   /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
860   /// See LangRef.html for the meaning of inbounds on a getelementptr.
861   void setIsInBounds(bool b = true);
862
863   /// isInBounds - Determine whether the GEP has the inbounds flag.
864   bool isInBounds() const;
865
866   /// \brief Accumulate the constant address offset of this GEP if possible.
867   ///
868   /// This routine accepts an APInt into which it will accumulate the constant
869   /// offset of this GEP if the GEP is in fact constant. If the GEP is not
870   /// all-constant, it returns false and the value of the offset APInt is
871   /// undefined (it is *not* preserved!). The APInt passed into this routine
872   /// must be at least as wide as the IntPtr type for the address space of
873   /// the base GEP pointer.
874   bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
875
876   // Methods for support type inquiry through isa, cast, and dyn_cast:
877   static inline bool classof(const Instruction *I) {
878     return (I->getOpcode() == Instruction::GetElementPtr);
879   }
880   static inline bool classof(const Value *V) {
881     return isa<Instruction>(V) && classof(cast<Instruction>(V));
882   }
883 };
884
885 template <>
886 struct OperandTraits<GetElementPtrInst> :
887   public VariadicOperandTraits<GetElementPtrInst, 1> {
888 };
889
890 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
891                                      ArrayRef<Value *> IdxList,
892                                      unsigned Values,
893                                      const Twine &NameStr,
894                                      Instruction *InsertBefore)
895   : Instruction(getGEPReturnType(Ptr, IdxList),
896                 GetElementPtr,
897                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
898                 Values, InsertBefore) {
899   init(Ptr, IdxList, NameStr);
900 }
901 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
902                                      ArrayRef<Value *> IdxList,
903                                      unsigned Values,
904                                      const Twine &NameStr,
905                                      BasicBlock *InsertAtEnd)
906   : Instruction(getGEPReturnType(Ptr, IdxList),
907                 GetElementPtr,
908                 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
909                 Values, InsertAtEnd) {
910   init(Ptr, IdxList, NameStr);
911 }
912
913
914 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
915
916
917 //===----------------------------------------------------------------------===//
918 //                               ICmpInst Class
919 //===----------------------------------------------------------------------===//
920
921 /// This instruction compares its operands according to the predicate given
922 /// to the constructor. It only operates on integers or pointers. The operands
923 /// must be identical types.
924 /// \brief Represent an integer comparison operator.
925 class ICmpInst: public CmpInst {
926   void AssertOK() {
927     assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
928            getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
929            "Invalid ICmp predicate value");
930     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
931           "Both operands to ICmp instruction are not of the same type!");
932     // Check that the operands are the right type
933     assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
934             getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
935            "Invalid operand types for ICmp instruction");
936   }
937
938 protected:
939   /// \brief Clone an identical ICmpInst
940   ICmpInst *clone_impl() const override;
941 public:
942   /// \brief Constructor with insert-before-instruction semantics.
943   ICmpInst(
944     Instruction *InsertBefore,  ///< Where to insert
945     Predicate pred,  ///< The predicate to use for the comparison
946     Value *LHS,      ///< The left-hand-side of the expression
947     Value *RHS,      ///< The right-hand-side of the expression
948     const Twine &NameStr = ""  ///< Name of the instruction
949   ) : CmpInst(makeCmpResultType(LHS->getType()),
950               Instruction::ICmp, pred, LHS, RHS, NameStr,
951               InsertBefore) {
952 #ifndef NDEBUG
953   AssertOK();
954 #endif
955   }
956
957   /// \brief Constructor with insert-at-end semantics.
958   ICmpInst(
959     BasicBlock &InsertAtEnd, ///< Block to insert into.
960     Predicate pred,  ///< The predicate to use for the comparison
961     Value *LHS,      ///< The left-hand-side of the expression
962     Value *RHS,      ///< The right-hand-side of the expression
963     const Twine &NameStr = ""  ///< Name of the instruction
964   ) : CmpInst(makeCmpResultType(LHS->getType()),
965               Instruction::ICmp, pred, LHS, RHS, NameStr,
966               &InsertAtEnd) {
967 #ifndef NDEBUG
968   AssertOK();
969 #endif
970   }
971
972   /// \brief Constructor with no-insertion semantics
973   ICmpInst(
974     Predicate pred, ///< The predicate to use for the comparison
975     Value *LHS,     ///< The left-hand-side of the expression
976     Value *RHS,     ///< The right-hand-side of the expression
977     const Twine &NameStr = "" ///< Name of the instruction
978   ) : CmpInst(makeCmpResultType(LHS->getType()),
979               Instruction::ICmp, pred, LHS, RHS, NameStr) {
980 #ifndef NDEBUG
981   AssertOK();
982 #endif
983   }
984
985   /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
986   /// @returns the predicate that would be the result if the operand were
987   /// regarded as signed.
988   /// \brief Return the signed version of the predicate
989   Predicate getSignedPredicate() const {
990     return getSignedPredicate(getPredicate());
991   }
992
993   /// This is a static version that you can use without an instruction.
994   /// \brief Return the signed version of the predicate.
995   static Predicate getSignedPredicate(Predicate pred);
996
997   /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
998   /// @returns the predicate that would be the result if the operand were
999   /// regarded as unsigned.
1000   /// \brief Return the unsigned version of the predicate
1001   Predicate getUnsignedPredicate() const {
1002     return getUnsignedPredicate(getPredicate());
1003   }
1004
1005   /// This is a static version that you can use without an instruction.
1006   /// \brief Return the unsigned version of the predicate.
1007   static Predicate getUnsignedPredicate(Predicate pred);
1008
1009   /// isEquality - Return true if this predicate is either EQ or NE.  This also
1010   /// tests for commutativity.
1011   static bool isEquality(Predicate P) {
1012     return P == ICMP_EQ || P == ICMP_NE;
1013   }
1014
1015   /// isEquality - Return true if this predicate is either EQ or NE.  This also
1016   /// tests for commutativity.
1017   bool isEquality() const {
1018     return isEquality(getPredicate());
1019   }
1020
1021   /// @returns true if the predicate of this ICmpInst is commutative
1022   /// \brief Determine if this relation is commutative.
1023   bool isCommutative() const { return isEquality(); }
1024
1025   /// isRelational - Return true if the predicate is relational (not EQ or NE).
1026   ///
1027   bool isRelational() const {
1028     return !isEquality();
1029   }
1030
1031   /// isRelational - Return true if the predicate is relational (not EQ or NE).
1032   ///
1033   static bool isRelational(Predicate P) {
1034     return !isEquality(P);
1035   }
1036
1037   /// Initialize a set of values that all satisfy the predicate with C.
1038   /// \brief Make a ConstantRange for a relation with a constant value.
1039   static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1040
1041   /// Exchange the two operands to this instruction in such a way that it does
1042   /// not modify the semantics of the instruction. The predicate value may be
1043   /// changed to retain the same result if the predicate is order dependent
1044   /// (e.g. ult).
1045   /// \brief Swap operands and adjust predicate.
1046   void swapOperands() {
1047     setPredicate(getSwappedPredicate());
1048     Op<0>().swap(Op<1>());
1049   }
1050
1051   // Methods for support type inquiry through isa, cast, and dyn_cast:
1052   static inline bool classof(const Instruction *I) {
1053     return I->getOpcode() == Instruction::ICmp;
1054   }
1055   static inline bool classof(const Value *V) {
1056     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1057   }
1058
1059 };
1060
1061 //===----------------------------------------------------------------------===//
1062 //                               FCmpInst Class
1063 //===----------------------------------------------------------------------===//
1064
1065 /// This instruction compares its operands according to the predicate given
1066 /// to the constructor. It only operates on floating point values or packed
1067 /// vectors of floating point values. The operands must be identical types.
1068 /// \brief Represents a floating point comparison operator.
1069 class FCmpInst: public CmpInst {
1070 protected:
1071   /// \brief Clone an identical FCmpInst
1072   FCmpInst *clone_impl() const override;
1073 public:
1074   /// \brief Constructor with insert-before-instruction semantics.
1075   FCmpInst(
1076     Instruction *InsertBefore, ///< Where to insert
1077     Predicate pred,  ///< The predicate to use for the comparison
1078     Value *LHS,      ///< The left-hand-side of the expression
1079     Value *RHS,      ///< The right-hand-side of the expression
1080     const Twine &NameStr = ""  ///< Name of the instruction
1081   ) : CmpInst(makeCmpResultType(LHS->getType()),
1082               Instruction::FCmp, pred, LHS, RHS, NameStr,
1083               InsertBefore) {
1084     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1085            "Invalid FCmp predicate value");
1086     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1087            "Both operands to FCmp instruction are not of the same type!");
1088     // Check that the operands are the right type
1089     assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1090            "Invalid operand types for FCmp instruction");
1091   }
1092
1093   /// \brief Constructor with insert-at-end semantics.
1094   FCmpInst(
1095     BasicBlock &InsertAtEnd, ///< Block to insert into.
1096     Predicate pred,  ///< The predicate to use for the comparison
1097     Value *LHS,      ///< The left-hand-side of the expression
1098     Value *RHS,      ///< The right-hand-side of the expression
1099     const Twine &NameStr = ""  ///< Name of the instruction
1100   ) : CmpInst(makeCmpResultType(LHS->getType()),
1101               Instruction::FCmp, pred, LHS, RHS, NameStr,
1102               &InsertAtEnd) {
1103     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1104            "Invalid FCmp predicate value");
1105     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1106            "Both operands to FCmp instruction are not of the same type!");
1107     // Check that the operands are the right type
1108     assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1109            "Invalid operand types for FCmp instruction");
1110   }
1111
1112   /// \brief Constructor with no-insertion semantics
1113   FCmpInst(
1114     Predicate pred, ///< The predicate to use for the comparison
1115     Value *LHS,     ///< The left-hand-side of the expression
1116     Value *RHS,     ///< The right-hand-side of the expression
1117     const Twine &NameStr = "" ///< Name of the instruction
1118   ) : CmpInst(makeCmpResultType(LHS->getType()),
1119               Instruction::FCmp, pred, LHS, RHS, NameStr) {
1120     assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1121            "Invalid FCmp predicate value");
1122     assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1123            "Both operands to FCmp instruction are not of the same type!");
1124     // Check that the operands are the right type
1125     assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1126            "Invalid operand types for FCmp instruction");
1127   }
1128
1129   /// @returns true if the predicate of this instruction is EQ or NE.
1130   /// \brief Determine if this is an equality predicate.
1131   bool isEquality() const {
1132     return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1133            getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1134   }
1135
1136   /// @returns true if the predicate of this instruction is commutative.
1137   /// \brief Determine if this is a commutative predicate.
1138   bool isCommutative() const {
1139     return isEquality() ||
1140            getPredicate() == FCMP_FALSE ||
1141            getPredicate() == FCMP_TRUE ||
1142            getPredicate() == FCMP_ORD ||
1143            getPredicate() == FCMP_UNO;
1144   }
1145
1146   /// @returns true if the predicate is relational (not EQ or NE).
1147   /// \brief Determine if this a relational predicate.
1148   bool isRelational() const { return !isEquality(); }
1149
1150   /// Exchange the two operands to this instruction in such a way that it does
1151   /// not modify the semantics of the instruction. The predicate value may be
1152   /// changed to retain the same result if the predicate is order dependent
1153   /// (e.g. ult).
1154   /// \brief Swap operands and adjust predicate.
1155   void swapOperands() {
1156     setPredicate(getSwappedPredicate());
1157     Op<0>().swap(Op<1>());
1158   }
1159
1160   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1161   static inline bool classof(const Instruction *I) {
1162     return I->getOpcode() == Instruction::FCmp;
1163   }
1164   static inline bool classof(const Value *V) {
1165     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1166   }
1167 };
1168
1169 //===----------------------------------------------------------------------===//
1170 /// CallInst - This class represents a function call, abstracting a target
1171 /// machine's calling convention.  This class uses low bit of the SubClassData
1172 /// field to indicate whether or not this is a tail call.  The rest of the bits
1173 /// hold the calling convention of the call.
1174 ///
1175 class CallInst : public Instruction {
1176   AttributeSet AttributeList; ///< parameter attributes for call
1177   CallInst(const CallInst &CI);
1178   void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1179   void init(Value *Func, const Twine &NameStr);
1180
1181   /// Construct a CallInst given a range of arguments.
1182   /// \brief Construct a CallInst from a range of arguments
1183   inline CallInst(Value *Func, ArrayRef<Value *> Args,
1184                   const Twine &NameStr, Instruction *InsertBefore);
1185
1186   /// Construct a CallInst given a range of arguments.
1187   /// \brief Construct a CallInst from a range of arguments
1188   inline CallInst(Value *Func, ArrayRef<Value *> Args,
1189                   const Twine &NameStr, BasicBlock *InsertAtEnd);
1190
1191   explicit CallInst(Value *F, const Twine &NameStr,
1192                     Instruction *InsertBefore);
1193   CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1194 protected:
1195   CallInst *clone_impl() const override;
1196 public:
1197   static CallInst *Create(Value *Func,
1198                           ArrayRef<Value *> Args,
1199                           const Twine &NameStr = "",
1200                           Instruction *InsertBefore = 0) {
1201     return new(unsigned(Args.size() + 1))
1202       CallInst(Func, Args, NameStr, InsertBefore);
1203   }
1204   static CallInst *Create(Value *Func,
1205                           ArrayRef<Value *> Args,
1206                           const Twine &NameStr, BasicBlock *InsertAtEnd) {
1207     return new(unsigned(Args.size() + 1))
1208       CallInst(Func, Args, NameStr, InsertAtEnd);
1209   }
1210   static CallInst *Create(Value *F, const Twine &NameStr = "",
1211                           Instruction *InsertBefore = 0) {
1212     return new(1) CallInst(F, NameStr, InsertBefore);
1213   }
1214   static CallInst *Create(Value *F, const Twine &NameStr,
1215                           BasicBlock *InsertAtEnd) {
1216     return new(1) CallInst(F, NameStr, InsertAtEnd);
1217   }
1218   /// CreateMalloc - Generate the IR for a call to malloc:
1219   /// 1. Compute the malloc call's argument as the specified type's size,
1220   ///    possibly multiplied by the array size if the array size is not
1221   ///    constant 1.
1222   /// 2. Call malloc with that argument.
1223   /// 3. Bitcast the result of the malloc call to the specified type.
1224   static Instruction *CreateMalloc(Instruction *InsertBefore,
1225                                    Type *IntPtrTy, Type *AllocTy,
1226                                    Value *AllocSize, Value *ArraySize = 0,
1227                                    Function* MallocF = 0,
1228                                    const Twine &Name = "");
1229   static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1230                                    Type *IntPtrTy, Type *AllocTy,
1231                                    Value *AllocSize, Value *ArraySize = 0,
1232                                    Function* MallocF = 0,
1233                                    const Twine &Name = "");
1234   /// CreateFree - Generate the IR for a call to the builtin free function.
1235   static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1236   static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1237
1238   ~CallInst();
1239
1240   bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1241   void setTailCall(bool isTC = true) {
1242     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1243                                unsigned(isTC));
1244   }
1245
1246   /// Provide fast operand accessors
1247   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1248
1249   /// getNumArgOperands - Return the number of call arguments.
1250   ///
1251   unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1252
1253   /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1254   ///
1255   Value *getArgOperand(unsigned i) const { return getOperand(i); }
1256   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1257
1258   /// \brief Wrappers for getting the \c Use of a call argument.
1259   const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1260   Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1261
1262   /// getCallingConv/setCallingConv - Get or set the calling convention of this
1263   /// function call.
1264   CallingConv::ID getCallingConv() const {
1265     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1266   }
1267   void setCallingConv(CallingConv::ID CC) {
1268     setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1269                                (static_cast<unsigned>(CC) << 1));
1270   }
1271
1272   /// getAttributes - Return the parameter attributes for this call.
1273   ///
1274   const AttributeSet &getAttributes() const { return AttributeList; }
1275
1276   /// setAttributes - Set the parameter attributes for this call.
1277   ///
1278   void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1279
1280   /// addAttribute - adds the attribute to the list of attributes.
1281   void addAttribute(unsigned i, Attribute::AttrKind attr);
1282
1283   /// removeAttribute - removes the attribute from the list of attributes.
1284   void removeAttribute(unsigned i, Attribute attr);
1285
1286   /// \brief Determine whether this call has the given attribute.
1287   bool hasFnAttr(Attribute::AttrKind A) const {
1288     assert(A != Attribute::NoBuiltin &&
1289            "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1290     return hasFnAttrImpl(A);
1291   }
1292
1293   /// \brief Determine whether the call or the callee has the given attributes.
1294   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1295
1296   /// \brief Extract the alignment for a call or parameter (0=unknown).
1297   unsigned getParamAlignment(unsigned i) const {
1298     return AttributeList.getParamAlignment(i);
1299   }
1300
1301   /// \brief Return true if the call should not be treated as a call to a
1302   /// builtin.
1303   bool isNoBuiltin() const {
1304     return hasFnAttrImpl(Attribute::NoBuiltin) &&
1305       !hasFnAttrImpl(Attribute::Builtin);
1306   }
1307
1308   /// \brief Return true if the call should not be inlined.
1309   bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1310   void setIsNoInline() {
1311     addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1312   }
1313
1314   /// \brief Return true if the call can return twice
1315   bool canReturnTwice() const {
1316     return hasFnAttr(Attribute::ReturnsTwice);
1317   }
1318   void setCanReturnTwice() {
1319     addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1320   }
1321
1322   /// \brief Determine if the call does not access memory.
1323   bool doesNotAccessMemory() const {
1324     return hasFnAttr(Attribute::ReadNone);
1325   }
1326   void setDoesNotAccessMemory() {
1327     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1328   }
1329
1330   /// \brief Determine if the call does not access or only reads memory.
1331   bool onlyReadsMemory() const {
1332     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1333   }
1334   void setOnlyReadsMemory() {
1335     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1336   }
1337
1338   /// \brief Determine if the call cannot return.
1339   bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1340   void setDoesNotReturn() {
1341     addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1342   }
1343
1344   /// \brief Determine if the call cannot unwind.
1345   bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1346   void setDoesNotThrow() {
1347     addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1348   }
1349
1350   /// \brief Determine if the call cannot be duplicated.
1351   bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1352   void setCannotDuplicate() {
1353     addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1354   }
1355
1356   /// \brief Determine if the call returns a structure through first
1357   /// pointer argument.
1358   bool hasStructRetAttr() const {
1359     // Be friendly and also check the callee.
1360     return paramHasAttr(1, Attribute::StructRet);
1361   }
1362
1363   /// \brief Determine if any call argument is an aggregate passed by value.
1364   bool hasByValArgument() const {
1365     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1366   }
1367
1368   /// getCalledFunction - Return the function called, or null if this is an
1369   /// indirect function invocation.
1370   ///
1371   Function *getCalledFunction() const {
1372     return dyn_cast<Function>(Op<-1>());
1373   }
1374
1375   /// getCalledValue - Get a pointer to the function that is invoked by this
1376   /// instruction.
1377   const Value *getCalledValue() const { return Op<-1>(); }
1378         Value *getCalledValue()       { return Op<-1>(); }
1379
1380   /// setCalledFunction - Set the function called.
1381   void setCalledFunction(Value* Fn) {
1382     Op<-1>() = Fn;
1383   }
1384
1385   /// isInlineAsm - Check if this call is an inline asm statement.
1386   bool isInlineAsm() const {
1387     return isa<InlineAsm>(Op<-1>());
1388   }
1389
1390   // Methods for support type inquiry through isa, cast, and dyn_cast:
1391   static inline bool classof(const Instruction *I) {
1392     return I->getOpcode() == Instruction::Call;
1393   }
1394   static inline bool classof(const Value *V) {
1395     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1396   }
1397 private:
1398
1399   bool hasFnAttrImpl(Attribute::AttrKind A) const;
1400
1401   // Shadow Instruction::setInstructionSubclassData with a private forwarding
1402   // method so that subclasses cannot accidentally use it.
1403   void setInstructionSubclassData(unsigned short D) {
1404     Instruction::setInstructionSubclassData(D);
1405   }
1406 };
1407
1408 template <>
1409 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1410 };
1411
1412 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1413                    const Twine &NameStr, BasicBlock *InsertAtEnd)
1414   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1415                                    ->getElementType())->getReturnType(),
1416                 Instruction::Call,
1417                 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1418                 unsigned(Args.size() + 1), InsertAtEnd) {
1419   init(Func, Args, NameStr);
1420 }
1421
1422 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1423                    const Twine &NameStr, Instruction *InsertBefore)
1424   : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1425                                    ->getElementType())->getReturnType(),
1426                 Instruction::Call,
1427                 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1428                 unsigned(Args.size() + 1), InsertBefore) {
1429   init(Func, Args, NameStr);
1430 }
1431
1432
1433 // Note: if you get compile errors about private methods then
1434 //       please update your code to use the high-level operand
1435 //       interfaces. See line 943 above.
1436 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1437
1438 //===----------------------------------------------------------------------===//
1439 //                               SelectInst Class
1440 //===----------------------------------------------------------------------===//
1441
1442 /// SelectInst - This class represents the LLVM 'select' instruction.
1443 ///
1444 class SelectInst : public Instruction {
1445   void init(Value *C, Value *S1, Value *S2) {
1446     assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1447     Op<0>() = C;
1448     Op<1>() = S1;
1449     Op<2>() = S2;
1450   }
1451
1452   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1453              Instruction *InsertBefore)
1454     : Instruction(S1->getType(), Instruction::Select,
1455                   &Op<0>(), 3, InsertBefore) {
1456     init(C, S1, S2);
1457     setName(NameStr);
1458   }
1459   SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1460              BasicBlock *InsertAtEnd)
1461     : Instruction(S1->getType(), Instruction::Select,
1462                   &Op<0>(), 3, InsertAtEnd) {
1463     init(C, S1, S2);
1464     setName(NameStr);
1465   }
1466 protected:
1467   SelectInst *clone_impl() const override;
1468 public:
1469   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1470                             const Twine &NameStr = "",
1471                             Instruction *InsertBefore = 0) {
1472     return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1473   }
1474   static SelectInst *Create(Value *C, Value *S1, Value *S2,
1475                             const Twine &NameStr,
1476                             BasicBlock *InsertAtEnd) {
1477     return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1478   }
1479
1480   const Value *getCondition() const { return Op<0>(); }
1481   const Value *getTrueValue() const { return Op<1>(); }
1482   const Value *getFalseValue() const { return Op<2>(); }
1483   Value *getCondition() { return Op<0>(); }
1484   Value *getTrueValue() { return Op<1>(); }
1485   Value *getFalseValue() { return Op<2>(); }
1486
1487   /// areInvalidOperands - Return a string if the specified operands are invalid
1488   /// for a select operation, otherwise return null.
1489   static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1490
1491   /// Transparently provide more efficient getOperand methods.
1492   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1493
1494   OtherOps getOpcode() const {
1495     return static_cast<OtherOps>(Instruction::getOpcode());
1496   }
1497
1498   // Methods for support type inquiry through isa, cast, and dyn_cast:
1499   static inline bool classof(const Instruction *I) {
1500     return I->getOpcode() == Instruction::Select;
1501   }
1502   static inline bool classof(const Value *V) {
1503     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1504   }
1505 };
1506
1507 template <>
1508 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1509 };
1510
1511 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1512
1513 //===----------------------------------------------------------------------===//
1514 //                                VAArgInst Class
1515 //===----------------------------------------------------------------------===//
1516
1517 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1518 /// an argument of the specified type given a va_list and increments that list
1519 ///
1520 class VAArgInst : public UnaryInstruction {
1521 protected:
1522   VAArgInst *clone_impl() const override;
1523
1524 public:
1525   VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1526              Instruction *InsertBefore = 0)
1527     : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1528     setName(NameStr);
1529   }
1530   VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1531             BasicBlock *InsertAtEnd)
1532     : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1533     setName(NameStr);
1534   }
1535
1536   Value *getPointerOperand() { return getOperand(0); }
1537   const Value *getPointerOperand() const { return getOperand(0); }
1538   static unsigned getPointerOperandIndex() { return 0U; }
1539
1540   // Methods for support type inquiry through isa, cast, and dyn_cast:
1541   static inline bool classof(const Instruction *I) {
1542     return I->getOpcode() == VAArg;
1543   }
1544   static inline bool classof(const Value *V) {
1545     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1546   }
1547 };
1548
1549 //===----------------------------------------------------------------------===//
1550 //                                ExtractElementInst Class
1551 //===----------------------------------------------------------------------===//
1552
1553 /// ExtractElementInst - This instruction extracts a single (scalar)
1554 /// element from a VectorType value
1555 ///
1556 class ExtractElementInst : public Instruction {
1557   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1558                      Instruction *InsertBefore = 0);
1559   ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1560                      BasicBlock *InsertAtEnd);
1561 protected:
1562   ExtractElementInst *clone_impl() const override;
1563
1564 public:
1565   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1566                                    const Twine &NameStr = "",
1567                                    Instruction *InsertBefore = 0) {
1568     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1569   }
1570   static ExtractElementInst *Create(Value *Vec, Value *Idx,
1571                                    const Twine &NameStr,
1572                                    BasicBlock *InsertAtEnd) {
1573     return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1574   }
1575
1576   /// isValidOperands - Return true if an extractelement instruction can be
1577   /// formed with the specified operands.
1578   static bool isValidOperands(const Value *Vec, const Value *Idx);
1579
1580   Value *getVectorOperand() { return Op<0>(); }
1581   Value *getIndexOperand() { return Op<1>(); }
1582   const Value *getVectorOperand() const { return Op<0>(); }
1583   const Value *getIndexOperand() const { return Op<1>(); }
1584
1585   VectorType *getVectorOperandType() const {
1586     return cast<VectorType>(getVectorOperand()->getType());
1587   }
1588
1589
1590   /// Transparently provide more efficient getOperand methods.
1591   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1592
1593   // Methods for support type inquiry through isa, cast, and dyn_cast:
1594   static inline bool classof(const Instruction *I) {
1595     return I->getOpcode() == Instruction::ExtractElement;
1596   }
1597   static inline bool classof(const Value *V) {
1598     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1599   }
1600 };
1601
1602 template <>
1603 struct OperandTraits<ExtractElementInst> :
1604   public FixedNumOperandTraits<ExtractElementInst, 2> {
1605 };
1606
1607 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1608
1609 //===----------------------------------------------------------------------===//
1610 //                                InsertElementInst Class
1611 //===----------------------------------------------------------------------===//
1612
1613 /// InsertElementInst - This instruction inserts a single (scalar)
1614 /// element into a VectorType value
1615 ///
1616 class InsertElementInst : public Instruction {
1617   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1618                     const Twine &NameStr = "",
1619                     Instruction *InsertBefore = 0);
1620   InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1621                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1622 protected:
1623   InsertElementInst *clone_impl() const override;
1624
1625 public:
1626   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1627                                    const Twine &NameStr = "",
1628                                    Instruction *InsertBefore = 0) {
1629     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1630   }
1631   static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1632                                    const Twine &NameStr,
1633                                    BasicBlock *InsertAtEnd) {
1634     return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1635   }
1636
1637   /// isValidOperands - Return true if an insertelement instruction can be
1638   /// formed with the specified operands.
1639   static bool isValidOperands(const Value *Vec, const Value *NewElt,
1640                               const Value *Idx);
1641
1642   /// getType - Overload to return most specific vector type.
1643   ///
1644   VectorType *getType() const {
1645     return cast<VectorType>(Instruction::getType());
1646   }
1647
1648   /// Transparently provide more efficient getOperand methods.
1649   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1650
1651   // Methods for support type inquiry through isa, cast, and dyn_cast:
1652   static inline bool classof(const Instruction *I) {
1653     return I->getOpcode() == Instruction::InsertElement;
1654   }
1655   static inline bool classof(const Value *V) {
1656     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1657   }
1658 };
1659
1660 template <>
1661 struct OperandTraits<InsertElementInst> :
1662   public FixedNumOperandTraits<InsertElementInst, 3> {
1663 };
1664
1665 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1666
1667 //===----------------------------------------------------------------------===//
1668 //                           ShuffleVectorInst Class
1669 //===----------------------------------------------------------------------===//
1670
1671 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1672 /// input vectors.
1673 ///
1674 class ShuffleVectorInst : public Instruction {
1675 protected:
1676   ShuffleVectorInst *clone_impl() const override;
1677
1678 public:
1679   // allocate space for exactly three operands
1680   void *operator new(size_t s) {
1681     return User::operator new(s, 3);
1682   }
1683   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1684                     const Twine &NameStr = "",
1685                     Instruction *InsertBefor = 0);
1686   ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1687                     const Twine &NameStr, BasicBlock *InsertAtEnd);
1688
1689   /// isValidOperands - Return true if a shufflevector instruction can be
1690   /// formed with the specified operands.
1691   static bool isValidOperands(const Value *V1, const Value *V2,
1692                               const Value *Mask);
1693
1694   /// getType - Overload to return most specific vector type.
1695   ///
1696   VectorType *getType() const {
1697     return cast<VectorType>(Instruction::getType());
1698   }
1699
1700   /// Transparently provide more efficient getOperand methods.
1701   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1702
1703   Constant *getMask() const {
1704     return cast<Constant>(getOperand(2));
1705   }
1706
1707   /// getMaskValue - Return the index from the shuffle mask for the specified
1708   /// output result.  This is either -1 if the element is undef or a number less
1709   /// than 2*numelements.
1710   static int getMaskValue(Constant *Mask, unsigned i);
1711
1712   int getMaskValue(unsigned i) const {
1713     return getMaskValue(getMask(), i);
1714   }
1715
1716   /// getShuffleMask - Return the full mask for this instruction, where each
1717   /// element is the element number and undef's are returned as -1.
1718   static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1719
1720   void getShuffleMask(SmallVectorImpl<int> &Result) const {
1721     return getShuffleMask(getMask(), Result);
1722   }
1723
1724   SmallVector<int, 16> getShuffleMask() const {
1725     SmallVector<int, 16> Mask;
1726     getShuffleMask(Mask);
1727     return Mask;
1728   }
1729
1730
1731   // Methods for support type inquiry through isa, cast, and dyn_cast:
1732   static inline bool classof(const Instruction *I) {
1733     return I->getOpcode() == Instruction::ShuffleVector;
1734   }
1735   static inline bool classof(const Value *V) {
1736     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1737   }
1738 };
1739
1740 template <>
1741 struct OperandTraits<ShuffleVectorInst> :
1742   public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1743 };
1744
1745 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1746
1747 //===----------------------------------------------------------------------===//
1748 //                                ExtractValueInst Class
1749 //===----------------------------------------------------------------------===//
1750
1751 /// ExtractValueInst - This instruction extracts a struct member or array
1752 /// element value from an aggregate value.
1753 ///
1754 class ExtractValueInst : public UnaryInstruction {
1755   SmallVector<unsigned, 4> Indices;
1756
1757   ExtractValueInst(const ExtractValueInst &EVI);
1758   void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1759
1760   /// Constructors - Create a extractvalue instruction with a base aggregate
1761   /// value and a list of indices.  The first ctor can optionally insert before
1762   /// an existing instruction, the second appends the new instruction to the
1763   /// specified BasicBlock.
1764   inline ExtractValueInst(Value *Agg,
1765                           ArrayRef<unsigned> Idxs,
1766                           const Twine &NameStr,
1767                           Instruction *InsertBefore);
1768   inline ExtractValueInst(Value *Agg,
1769                           ArrayRef<unsigned> Idxs,
1770                           const Twine &NameStr, BasicBlock *InsertAtEnd);
1771
1772   // allocate space for exactly one operand
1773   void *operator new(size_t s) {
1774     return User::operator new(s, 1);
1775   }
1776 protected:
1777   ExtractValueInst *clone_impl() const override;
1778
1779 public:
1780   static ExtractValueInst *Create(Value *Agg,
1781                                   ArrayRef<unsigned> Idxs,
1782                                   const Twine &NameStr = "",
1783                                   Instruction *InsertBefore = 0) {
1784     return new
1785       ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1786   }
1787   static ExtractValueInst *Create(Value *Agg,
1788                                   ArrayRef<unsigned> Idxs,
1789                                   const Twine &NameStr,
1790                                   BasicBlock *InsertAtEnd) {
1791     return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1792   }
1793
1794   /// getIndexedType - Returns the type of the element that would be extracted
1795   /// with an extractvalue instruction with the specified parameters.
1796   ///
1797   /// Null is returned if the indices are invalid for the specified type.
1798   static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1799
1800   typedef const unsigned* idx_iterator;
1801   inline idx_iterator idx_begin() const { return Indices.begin(); }
1802   inline idx_iterator idx_end()   const { return Indices.end(); }
1803
1804   Value *getAggregateOperand() {
1805     return getOperand(0);
1806   }
1807   const Value *getAggregateOperand() const {
1808     return getOperand(0);
1809   }
1810   static unsigned getAggregateOperandIndex() {
1811     return 0U;                      // get index for modifying correct operand
1812   }
1813
1814   ArrayRef<unsigned> getIndices() const {
1815     return Indices;
1816   }
1817
1818   unsigned getNumIndices() const {
1819     return (unsigned)Indices.size();
1820   }
1821
1822   bool hasIndices() const {
1823     return true;
1824   }
1825
1826   // Methods for support type inquiry through isa, cast, and dyn_cast:
1827   static inline bool classof(const Instruction *I) {
1828     return I->getOpcode() == Instruction::ExtractValue;
1829   }
1830   static inline bool classof(const Value *V) {
1831     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1832   }
1833 };
1834
1835 ExtractValueInst::ExtractValueInst(Value *Agg,
1836                                    ArrayRef<unsigned> Idxs,
1837                                    const Twine &NameStr,
1838                                    Instruction *InsertBefore)
1839   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1840                      ExtractValue, Agg, InsertBefore) {
1841   init(Idxs, NameStr);
1842 }
1843 ExtractValueInst::ExtractValueInst(Value *Agg,
1844                                    ArrayRef<unsigned> Idxs,
1845                                    const Twine &NameStr,
1846                                    BasicBlock *InsertAtEnd)
1847   : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1848                      ExtractValue, Agg, InsertAtEnd) {
1849   init(Idxs, NameStr);
1850 }
1851
1852
1853 //===----------------------------------------------------------------------===//
1854 //                                InsertValueInst Class
1855 //===----------------------------------------------------------------------===//
1856
1857 /// InsertValueInst - This instruction inserts a struct field of array element
1858 /// value into an aggregate value.
1859 ///
1860 class InsertValueInst : public Instruction {
1861   SmallVector<unsigned, 4> Indices;
1862
1863   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1864   InsertValueInst(const InsertValueInst &IVI);
1865   void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1866             const Twine &NameStr);
1867
1868   /// Constructors - Create a insertvalue instruction with a base aggregate
1869   /// value, a value to insert, and a list of indices.  The first ctor can
1870   /// optionally insert before an existing instruction, the second appends
1871   /// the new instruction to the specified BasicBlock.
1872   inline InsertValueInst(Value *Agg, Value *Val,
1873                          ArrayRef<unsigned> Idxs,
1874                          const Twine &NameStr,
1875                          Instruction *InsertBefore);
1876   inline InsertValueInst(Value *Agg, Value *Val,
1877                          ArrayRef<unsigned> Idxs,
1878                          const Twine &NameStr, BasicBlock *InsertAtEnd);
1879
1880   /// Constructors - These two constructors are convenience methods because one
1881   /// and two index insertvalue instructions are so common.
1882   InsertValueInst(Value *Agg, Value *Val,
1883                   unsigned Idx, const Twine &NameStr = "",
1884                   Instruction *InsertBefore = 0);
1885   InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1886                   const Twine &NameStr, BasicBlock *InsertAtEnd);
1887 protected:
1888   InsertValueInst *clone_impl() const override;
1889 public:
1890   // allocate space for exactly two operands
1891   void *operator new(size_t s) {
1892     return User::operator new(s, 2);
1893   }
1894
1895   static InsertValueInst *Create(Value *Agg, Value *Val,
1896                                  ArrayRef<unsigned> Idxs,
1897                                  const Twine &NameStr = "",
1898                                  Instruction *InsertBefore = 0) {
1899     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1900   }
1901   static InsertValueInst *Create(Value *Agg, Value *Val,
1902                                  ArrayRef<unsigned> Idxs,
1903                                  const Twine &NameStr,
1904                                  BasicBlock *InsertAtEnd) {
1905     return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1906   }
1907
1908   /// Transparently provide more efficient getOperand methods.
1909   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1910
1911   typedef const unsigned* idx_iterator;
1912   inline idx_iterator idx_begin() const { return Indices.begin(); }
1913   inline idx_iterator idx_end()   const { return Indices.end(); }
1914
1915   Value *getAggregateOperand() {
1916     return getOperand(0);
1917   }
1918   const Value *getAggregateOperand() const {
1919     return getOperand(0);
1920   }
1921   static unsigned getAggregateOperandIndex() {
1922     return 0U;                      // get index for modifying correct operand
1923   }
1924
1925   Value *getInsertedValueOperand() {
1926     return getOperand(1);
1927   }
1928   const Value *getInsertedValueOperand() const {
1929     return getOperand(1);
1930   }
1931   static unsigned getInsertedValueOperandIndex() {
1932     return 1U;                      // get index for modifying correct operand
1933   }
1934
1935   ArrayRef<unsigned> getIndices() const {
1936     return Indices;
1937   }
1938
1939   unsigned getNumIndices() const {
1940     return (unsigned)Indices.size();
1941   }
1942
1943   bool hasIndices() const {
1944     return true;
1945   }
1946
1947   // Methods for support type inquiry through isa, cast, and dyn_cast:
1948   static inline bool classof(const Instruction *I) {
1949     return I->getOpcode() == Instruction::InsertValue;
1950   }
1951   static inline bool classof(const Value *V) {
1952     return isa<Instruction>(V) && classof(cast<Instruction>(V));
1953   }
1954 };
1955
1956 template <>
1957 struct OperandTraits<InsertValueInst> :
1958   public FixedNumOperandTraits<InsertValueInst, 2> {
1959 };
1960
1961 InsertValueInst::InsertValueInst(Value *Agg,
1962                                  Value *Val,
1963                                  ArrayRef<unsigned> Idxs,
1964                                  const Twine &NameStr,
1965                                  Instruction *InsertBefore)
1966   : Instruction(Agg->getType(), InsertValue,
1967                 OperandTraits<InsertValueInst>::op_begin(this),
1968                 2, InsertBefore) {
1969   init(Agg, Val, Idxs, NameStr);
1970 }
1971 InsertValueInst::InsertValueInst(Value *Agg,
1972                                  Value *Val,
1973                                  ArrayRef<unsigned> Idxs,
1974                                  const Twine &NameStr,
1975                                  BasicBlock *InsertAtEnd)
1976   : Instruction(Agg->getType(), InsertValue,
1977                 OperandTraits<InsertValueInst>::op_begin(this),
1978                 2, InsertAtEnd) {
1979   init(Agg, Val, Idxs, NameStr);
1980 }
1981
1982 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1983
1984 //===----------------------------------------------------------------------===//
1985 //                               PHINode Class
1986 //===----------------------------------------------------------------------===//
1987
1988 // PHINode - The PHINode class is used to represent the magical mystical PHI
1989 // node, that can not exist in nature, but can be synthesized in a computer
1990 // scientist's overactive imagination.
1991 //
1992 class PHINode : public Instruction {
1993   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1994   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
1995   /// the number actually in use.
1996   unsigned ReservedSpace;
1997   PHINode(const PHINode &PN);
1998   // allocate space for exactly zero operands
1999   void *operator new(size_t s) {
2000     return User::operator new(s, 0);
2001   }
2002   explicit PHINode(Type *Ty, unsigned NumReservedValues,
2003                    const Twine &NameStr = "", Instruction *InsertBefore = 0)
2004     : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
2005       ReservedSpace(NumReservedValues) {
2006     setName(NameStr);
2007     OperandList = allocHungoffUses(ReservedSpace);
2008   }
2009
2010   PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2011           BasicBlock *InsertAtEnd)
2012     : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
2013       ReservedSpace(NumReservedValues) {
2014     setName(NameStr);
2015     OperandList = allocHungoffUses(ReservedSpace);
2016   }
2017 protected:
2018   // allocHungoffUses - this is more complicated than the generic
2019   // User::allocHungoffUses, because we have to allocate Uses for the incoming
2020   // values and pointers to the incoming blocks, all in one allocation.
2021   Use *allocHungoffUses(unsigned) const;
2022
2023   PHINode *clone_impl() const override;
2024 public:
2025   /// Constructors - NumReservedValues is a hint for the number of incoming
2026   /// edges that this phi node will have (use 0 if you really have no idea).
2027   static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2028                          const Twine &NameStr = "",
2029                          Instruction *InsertBefore = 0) {
2030     return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2031   }
2032   static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2033                          const Twine &NameStr, BasicBlock *InsertAtEnd) {
2034     return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2035   }
2036   ~PHINode();
2037
2038   /// Provide fast operand accessors
2039   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2040
2041   // Block iterator interface. This provides access to the list of incoming
2042   // basic blocks, which parallels the list of incoming values.
2043
2044   typedef BasicBlock **block_iterator;
2045   typedef BasicBlock * const *const_block_iterator;
2046
2047   block_iterator block_begin() {
2048     Use::UserRef *ref =
2049       reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2050     return reinterpret_cast<block_iterator>(ref + 1);
2051   }
2052
2053   const_block_iterator block_begin() const {
2054     const Use::UserRef *ref =
2055       reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2056     return reinterpret_cast<const_block_iterator>(ref + 1);
2057   }
2058
2059   block_iterator block_end() {
2060     return block_begin() + getNumOperands();
2061   }
2062
2063   const_block_iterator block_end() const {
2064     return block_begin() + getNumOperands();
2065   }
2066
2067   /// getNumIncomingValues - Return the number of incoming edges
2068   ///
2069   unsigned getNumIncomingValues() const { return getNumOperands(); }
2070
2071   /// getIncomingValue - Return incoming value number x
2072   ///
2073   Value *getIncomingValue(unsigned i) const {
2074     return getOperand(i);
2075   }
2076   void setIncomingValue(unsigned i, Value *V) {
2077     setOperand(i, V);
2078   }
2079   static unsigned getOperandNumForIncomingValue(unsigned i) {
2080     return i;
2081   }
2082   static unsigned getIncomingValueNumForOperand(unsigned i) {
2083     return i;
2084   }
2085
2086   /// getIncomingBlock - Return incoming basic block number @p i.
2087   ///
2088   BasicBlock *getIncomingBlock(unsigned i) const {
2089     return block_begin()[i];
2090   }
2091
2092   /// getIncomingBlock - Return incoming basic block corresponding
2093   /// to an operand of the PHI.
2094   ///
2095   BasicBlock *getIncomingBlock(const Use &U) const {
2096     assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2097     return getIncomingBlock(unsigned(&U - op_begin()));
2098   }
2099
2100   /// getIncomingBlock - Return incoming basic block corresponding
2101   /// to value use iterator.
2102   ///
2103   BasicBlock *getIncomingBlock(Value::const_use_iterator I) const {
2104     return getIncomingBlock(I.getUse());
2105   }
2106
2107   void setIncomingBlock(unsigned i, BasicBlock *BB) {
2108     block_begin()[i] = BB;
2109   }
2110
2111   /// addIncoming - Add an incoming value to the end of the PHI list
2112   ///
2113   void addIncoming(Value *V, BasicBlock *BB) {
2114     assert(V && "PHI node got a null value!");
2115     assert(BB && "PHI node got a null basic block!");
2116     assert(getType() == V->getType() &&
2117            "All operands to PHI node must be the same type as the PHI node!");
2118     if (NumOperands == ReservedSpace)
2119       growOperands();  // Get more space!
2120     // Initialize some new operands.
2121     ++NumOperands;
2122     setIncomingValue(NumOperands - 1, V);
2123     setIncomingBlock(NumOperands - 1, BB);
2124   }
2125
2126   /// removeIncomingValue - Remove an incoming value.  This is useful if a
2127   /// predecessor basic block is deleted.  The value removed is returned.
2128   ///
2129   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2130   /// is true), the PHI node is destroyed and any uses of it are replaced with
2131   /// dummy values.  The only time there should be zero incoming values to a PHI
2132   /// node is when the block is dead, so this strategy is sound.
2133   ///
2134   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2135
2136   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2137     int Idx = getBasicBlockIndex(BB);
2138     assert(Idx >= 0 && "Invalid basic block argument to remove!");
2139     return removeIncomingValue(Idx, DeletePHIIfEmpty);
2140   }
2141
2142   /// getBasicBlockIndex - Return the first index of the specified basic
2143   /// block in the value list for this PHI.  Returns -1 if no instance.
2144   ///
2145   int getBasicBlockIndex(const BasicBlock *BB) const {
2146     for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2147       if (block_begin()[i] == BB)
2148         return i;
2149     return -1;
2150   }
2151
2152   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2153     int Idx = getBasicBlockIndex(BB);
2154     assert(Idx >= 0 && "Invalid basic block argument!");
2155     return getIncomingValue(Idx);
2156   }
2157
2158   /// hasConstantValue - If the specified PHI node always merges together the
2159   /// same value, return the value, otherwise return null.
2160   Value *hasConstantValue() const;
2161
2162   /// Methods for support type inquiry through isa, cast, and dyn_cast:
2163   static inline bool classof(const Instruction *I) {
2164     return I->getOpcode() == Instruction::PHI;
2165   }
2166   static inline bool classof(const Value *V) {
2167     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2168   }
2169  private:
2170   void growOperands();
2171 };
2172
2173 template <>
2174 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2175 };
2176
2177 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2178
2179 //===----------------------------------------------------------------------===//
2180 //                           LandingPadInst Class
2181 //===----------------------------------------------------------------------===//
2182
2183 //===---------------------------------------------------------------------------
2184 /// LandingPadInst - The landingpad instruction holds all of the information
2185 /// necessary to generate correct exception handling. The landingpad instruction
2186 /// cannot be moved from the top of a landing pad block, which itself is
2187 /// accessible only from the 'unwind' edge of an invoke. This uses the
2188 /// SubclassData field in Value to store whether or not the landingpad is a
2189 /// cleanup.
2190 ///
2191 class LandingPadInst : public Instruction {
2192   /// ReservedSpace - The number of operands actually allocated.  NumOperands is
2193   /// the number actually in use.
2194   unsigned ReservedSpace;
2195   LandingPadInst(const LandingPadInst &LP);
2196 public:
2197   enum ClauseType { Catch, Filter };
2198 private:
2199   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2200   // Allocate space for exactly zero operands.
2201   void *operator new(size_t s) {
2202     return User::operator new(s, 0);
2203   }
2204   void growOperands(unsigned Size);
2205   void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2206
2207   explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2208                           unsigned NumReservedValues, const Twine &NameStr,
2209                           Instruction *InsertBefore);
2210   explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2211                           unsigned NumReservedValues, const Twine &NameStr,
2212                           BasicBlock *InsertAtEnd);
2213 protected:
2214   LandingPadInst *clone_impl() const override;
2215 public:
2216   /// Constructors - NumReservedClauses is a hint for the number of incoming
2217   /// clauses that this landingpad will have (use 0 if you really have no idea).
2218   static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2219                                 unsigned NumReservedClauses,
2220                                 const Twine &NameStr = "",
2221                                 Instruction *InsertBefore = 0);
2222   static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2223                                 unsigned NumReservedClauses,
2224                                 const Twine &NameStr, BasicBlock *InsertAtEnd);
2225   ~LandingPadInst();
2226
2227   /// Provide fast operand accessors
2228   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2229
2230   /// getPersonalityFn - Get the personality function associated with this
2231   /// landing pad.
2232   Value *getPersonalityFn() const { return getOperand(0); }
2233
2234   /// isCleanup - Return 'true' if this landingpad instruction is a
2235   /// cleanup. I.e., it should be run when unwinding even if its landing pad
2236   /// doesn't catch the exception.
2237   bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2238
2239   /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2240   void setCleanup(bool V) {
2241     setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2242                                (V ? 1 : 0));
2243   }
2244
2245   /// addClause - Add a catch or filter clause to the landing pad.
2246   void addClause(Value *ClauseVal);
2247
2248   /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2249   /// to determine what type of clause this is.
2250   Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2251
2252   /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2253   bool isCatch(unsigned Idx) const {
2254     return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2255   }
2256
2257   /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2258   bool isFilter(unsigned Idx) const {
2259     return isa<ArrayType>(OperandList[Idx + 1]->getType());
2260   }
2261
2262   /// getNumClauses - Get the number of clauses for this landing pad.
2263   unsigned getNumClauses() const { return getNumOperands() - 1; }
2264
2265   /// reserveClauses - Grow the size of the operand list to accommodate the new
2266   /// number of clauses.
2267   void reserveClauses(unsigned Size) { growOperands(Size); }
2268
2269   // Methods for support type inquiry through isa, cast, and dyn_cast:
2270   static inline bool classof(const Instruction *I) {
2271     return I->getOpcode() == Instruction::LandingPad;
2272   }
2273   static inline bool classof(const Value *V) {
2274     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2275   }
2276 };
2277
2278 template <>
2279 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2280 };
2281
2282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2283
2284 //===----------------------------------------------------------------------===//
2285 //                               ReturnInst Class
2286 //===----------------------------------------------------------------------===//
2287
2288 //===---------------------------------------------------------------------------
2289 /// ReturnInst - Return a value (possibly void), from a function.  Execution
2290 /// does not continue in this function any longer.
2291 ///
2292 class ReturnInst : public TerminatorInst {
2293   ReturnInst(const ReturnInst &RI);
2294
2295 private:
2296   // ReturnInst constructors:
2297   // ReturnInst()                  - 'ret void' instruction
2298   // ReturnInst(    null)          - 'ret void' instruction
2299   // ReturnInst(Value* X)          - 'ret X'    instruction
2300   // ReturnInst(    null, Inst *I) - 'ret void' instruction, insert before I
2301   // ReturnInst(Value* X, Inst *I) - 'ret X'    instruction, insert before I
2302   // ReturnInst(    null, BB *B)   - 'ret void' instruction, insert @ end of B
2303   // ReturnInst(Value* X, BB *B)   - 'ret X'    instruction, insert @ end of B
2304   //
2305   // NOTE: If the Value* passed is of type void then the constructor behaves as
2306   // if it was passed NULL.
2307   explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2308                       Instruction *InsertBefore = 0);
2309   ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2310   explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2311 protected:
2312   ReturnInst *clone_impl() const override;
2313 public:
2314   static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2315                             Instruction *InsertBefore = 0) {
2316     return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2317   }
2318   static ReturnInst* Create(LLVMContext &C, Value *retVal,
2319                             BasicBlock *InsertAtEnd) {
2320     return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2321   }
2322   static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2323     return new(0) ReturnInst(C, InsertAtEnd);
2324   }
2325   virtual ~ReturnInst();
2326
2327   /// Provide fast operand accessors
2328   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2329
2330   /// Convenience accessor. Returns null if there is no return value.
2331   Value *getReturnValue() const {
2332     return getNumOperands() != 0 ? getOperand(0) : 0;
2333   }
2334
2335   unsigned getNumSuccessors() const { return 0; }
2336
2337   // Methods for support type inquiry through isa, cast, and dyn_cast:
2338   static inline bool classof(const Instruction *I) {
2339     return (I->getOpcode() == Instruction::Ret);
2340   }
2341   static inline bool classof(const Value *V) {
2342     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2343   }
2344  private:
2345   BasicBlock *getSuccessorV(unsigned idx) const override;
2346   unsigned getNumSuccessorsV() const override;
2347   void setSuccessorV(unsigned idx, BasicBlock *B) override;
2348 };
2349
2350 template <>
2351 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2352 };
2353
2354 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2355
2356 //===----------------------------------------------------------------------===//
2357 //                               BranchInst Class
2358 //===----------------------------------------------------------------------===//
2359
2360 //===---------------------------------------------------------------------------
2361 /// BranchInst - Conditional or Unconditional Branch instruction.
2362 ///
2363 class BranchInst : public TerminatorInst {
2364   /// Ops list - Branches are strange.  The operands are ordered:
2365   ///  [Cond, FalseDest,] TrueDest.  This makes some accessors faster because
2366   /// they don't have to check for cond/uncond branchness. These are mostly
2367   /// accessed relative from op_end().
2368   BranchInst(const BranchInst &BI);
2369   void AssertOK();
2370   // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2371   // BranchInst(BB *B)                           - 'br B'
2372   // BranchInst(BB* T, BB *F, Value *C)          - 'br C, T, F'
2373   // BranchInst(BB* B, Inst *I)                  - 'br B'        insert before I
2374   // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2375   // BranchInst(BB* B, BB *I)                    - 'br B'        insert at end
2376   // BranchInst(BB* T, BB *F, Value *C, BB *I)   - 'br C, T, F', insert at end
2377   explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2378   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2379              Instruction *InsertBefore = 0);
2380   BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2381   BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2382              BasicBlock *InsertAtEnd);
2383 protected:
2384   BranchInst *clone_impl() const override;
2385 public:
2386   static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2387     return new(1) BranchInst(IfTrue, InsertBefore);
2388   }
2389   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2390                             Value *Cond, Instruction *InsertBefore = 0) {
2391     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2392   }
2393   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2394     return new(1) BranchInst(IfTrue, InsertAtEnd);
2395   }
2396   static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2397                             Value *Cond, BasicBlock *InsertAtEnd) {
2398     return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2399   }
2400
2401   /// Transparently provide more efficient getOperand methods.
2402   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2403
2404   bool isUnconditional() const { return getNumOperands() == 1; }
2405   bool isConditional()   const { return getNumOperands() == 3; }
2406
2407   Value *getCondition() const {
2408     assert(isConditional() && "Cannot get condition of an uncond branch!");
2409     return Op<-3>();
2410   }
2411
2412   void setCondition(Value *V) {
2413     assert(isConditional() && "Cannot set condition of unconditional branch!");
2414     Op<-3>() = V;
2415   }
2416
2417   unsigned getNumSuccessors() const { return 1+isConditional(); }
2418
2419   BasicBlock *getSuccessor(unsigned i) const {
2420     assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2421     return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2422   }
2423
2424   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2425     assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2426     *(&Op<-1>() - idx) = (Value*)NewSucc;
2427   }
2428
2429   /// \brief Swap the successors of this branch instruction.
2430   ///
2431   /// Swaps the successors of the branch instruction. This also swaps any
2432   /// branch weight metadata associated with the instruction so that it
2433   /// continues to map correctly to each operand.
2434   void swapSuccessors();
2435
2436   // Methods for support type inquiry through isa, cast, and dyn_cast:
2437   static inline bool classof(const Instruction *I) {
2438     return (I->getOpcode() == Instruction::Br);
2439   }
2440   static inline bool classof(const Value *V) {
2441     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2442   }
2443 private:
2444   BasicBlock *getSuccessorV(unsigned idx) const override;
2445   unsigned getNumSuccessorsV() const override;
2446   void setSuccessorV(unsigned idx, BasicBlock *B) override;
2447 };
2448
2449 template <>
2450 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2451 };
2452
2453 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2454
2455 //===----------------------------------------------------------------------===//
2456 //                               SwitchInst Class
2457 //===----------------------------------------------------------------------===//
2458
2459 //===---------------------------------------------------------------------------
2460 /// SwitchInst - Multiway switch
2461 ///
2462 class SwitchInst : public TerminatorInst {
2463   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2464   unsigned ReservedSpace;
2465   // Operand[0]    = Value to switch on
2466   // Operand[1]    = Default basic block destination
2467   // Operand[2n  ] = Value to match
2468   // Operand[2n+1] = BasicBlock to go to on match
2469   SwitchInst(const SwitchInst &SI);
2470   void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2471   void growOperands();
2472   // allocate space for exactly zero operands
2473   void *operator new(size_t s) {
2474     return User::operator new(s, 0);
2475   }
2476   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2477   /// switch on and a default destination.  The number of additional cases can
2478   /// be specified here to make memory allocation more efficient.  This
2479   /// constructor can also autoinsert before another instruction.
2480   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2481              Instruction *InsertBefore);
2482
2483   /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2484   /// switch on and a default destination.  The number of additional cases can
2485   /// be specified here to make memory allocation more efficient.  This
2486   /// constructor also autoinserts at the end of the specified BasicBlock.
2487   SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2488              BasicBlock *InsertAtEnd);
2489 protected:
2490   SwitchInst *clone_impl() const override;
2491 public:
2492
2493   // -2
2494   static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2495
2496   template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2497   class CaseIteratorT {
2498   protected:
2499
2500     SwitchInstTy *SI;
2501     unsigned Index;
2502
2503   public:
2504
2505     typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2506
2507     /// Initializes case iterator for given SwitchInst and for given
2508     /// case number.
2509     CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2510       this->SI = SI;
2511       Index = CaseNum;
2512     }
2513
2514     /// Initializes case iterator for given SwitchInst and for given
2515     /// TerminatorInst's successor index.
2516     static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2517       assert(SuccessorIndex < SI->getNumSuccessors() &&
2518              "Successor index # out of range!");
2519       return SuccessorIndex != 0 ?
2520              Self(SI, SuccessorIndex - 1) :
2521              Self(SI, DefaultPseudoIndex);
2522     }
2523
2524     /// Resolves case value for current case.
2525     ConstantIntTy *getCaseValue() {
2526       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2527       return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2528     }
2529
2530     /// Resolves successor for current case.
2531     BasicBlockTy *getCaseSuccessor() {
2532       assert((Index < SI->getNumCases() ||
2533               Index == DefaultPseudoIndex) &&
2534              "Index out the number of cases.");
2535       return SI->getSuccessor(getSuccessorIndex());
2536     }
2537
2538     /// Returns number of current case.
2539     unsigned getCaseIndex() const { return Index; }
2540
2541     /// Returns TerminatorInst's successor index for current case successor.
2542     unsigned getSuccessorIndex() const {
2543       assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2544              "Index out the number of cases.");
2545       return Index != DefaultPseudoIndex ? Index + 1 : 0;
2546     }
2547
2548     Self operator++() {
2549       // Check index correctness after increment.
2550       // Note: Index == getNumCases() means end().
2551       assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2552       ++Index;
2553       return *this;
2554     }
2555     Self operator++(int) {
2556       Self tmp = *this;
2557       ++(*this);
2558       return tmp;
2559     }
2560     Self operator--() {
2561       // Check index correctness after decrement.
2562       // Note: Index == getNumCases() means end().
2563       // Also allow "-1" iterator here. That will became valid after ++.
2564       assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2565              "Index out the number of cases.");
2566       --Index;
2567       return *this;
2568     }
2569     Self operator--(int) {
2570       Self tmp = *this;
2571       --(*this);
2572       return tmp;
2573     }
2574     bool operator==(const Self& RHS) const {
2575       assert(RHS.SI == SI && "Incompatible operators.");
2576       return RHS.Index == Index;
2577     }
2578     bool operator!=(const Self& RHS) const {
2579       assert(RHS.SI == SI && "Incompatible operators.");
2580       return RHS.Index != Index;
2581     }
2582   };
2583
2584   typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2585     ConstCaseIt;
2586
2587   class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2588
2589     typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2590
2591   public:
2592
2593     CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2594     CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2595
2596     /// Sets the new value for current case.
2597     void setValue(ConstantInt *V) {
2598       assert(Index < SI->getNumCases() && "Index out the number of cases.");
2599       SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2600     }
2601
2602     /// Sets the new successor for current case.
2603     void setSuccessor(BasicBlock *S) {
2604       SI->setSuccessor(getSuccessorIndex(), S);
2605     }
2606   };
2607
2608   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2609                             unsigned NumCases, Instruction *InsertBefore = 0) {
2610     return new SwitchInst(Value, Default, NumCases, InsertBefore);
2611   }
2612   static SwitchInst *Create(Value *Value, BasicBlock *Default,
2613                             unsigned NumCases, BasicBlock *InsertAtEnd) {
2614     return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2615   }
2616
2617   ~SwitchInst();
2618
2619   /// Provide fast operand accessors
2620   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2621
2622   // Accessor Methods for Switch stmt
2623   Value *getCondition() const { return getOperand(0); }
2624   void setCondition(Value *V) { setOperand(0, V); }
2625
2626   BasicBlock *getDefaultDest() const {
2627     return cast<BasicBlock>(getOperand(1));
2628   }
2629
2630   void setDefaultDest(BasicBlock *DefaultCase) {
2631     setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2632   }
2633
2634   /// getNumCases - return the number of 'cases' in this switch instruction,
2635   /// except the default case
2636   unsigned getNumCases() const {
2637     return getNumOperands()/2 - 1;
2638   }
2639
2640   /// Returns a read/write iterator that points to the first
2641   /// case in SwitchInst.
2642   CaseIt case_begin() {
2643     return CaseIt(this, 0);
2644   }
2645   /// Returns a read-only iterator that points to the first
2646   /// case in the SwitchInst.
2647   ConstCaseIt case_begin() const {
2648     return ConstCaseIt(this, 0);
2649   }
2650
2651   /// Returns a read/write iterator that points one past the last
2652   /// in the SwitchInst.
2653   CaseIt case_end() {
2654     return CaseIt(this, getNumCases());
2655   }
2656   /// Returns a read-only iterator that points one past the last
2657   /// in the SwitchInst.
2658   ConstCaseIt case_end() const {
2659     return ConstCaseIt(this, getNumCases());
2660   }
2661   /// Returns an iterator that points to the default case.
2662   /// Note: this iterator allows to resolve successor only. Attempt
2663   /// to resolve case value causes an assertion.
2664   /// Also note, that increment and decrement also causes an assertion and
2665   /// makes iterator invalid.
2666   CaseIt case_default() {
2667     return CaseIt(this, DefaultPseudoIndex);
2668   }
2669   ConstCaseIt case_default() const {
2670     return ConstCaseIt(this, DefaultPseudoIndex);
2671   }
2672
2673   /// findCaseValue - Search all of the case values for the specified constant.
2674   /// If it is explicitly handled, return the case iterator of it, otherwise
2675   /// return default case iterator to indicate
2676   /// that it is handled by the default handler.
2677   CaseIt findCaseValue(const ConstantInt *C) {
2678     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2679       if (i.getCaseValue() == C)
2680         return i;
2681     return case_default();
2682   }
2683   ConstCaseIt findCaseValue(const ConstantInt *C) const {
2684     for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2685       if (i.getCaseValue() == C)
2686         return i;
2687     return case_default();
2688   }
2689
2690   /// findCaseDest - Finds the unique case value for a given successor. Returns
2691   /// null if the successor is not found, not unique, or is the default case.
2692   ConstantInt *findCaseDest(BasicBlock *BB) {
2693     if (BB == getDefaultDest()) return NULL;
2694
2695     ConstantInt *CI = NULL;
2696     for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2697       if (i.getCaseSuccessor() == BB) {
2698         if (CI) return NULL;   // Multiple cases lead to BB.
2699         else CI = i.getCaseValue();
2700       }
2701     }
2702     return CI;
2703   }
2704
2705   /// addCase - Add an entry to the switch instruction...
2706   /// Note:
2707   /// This action invalidates case_end(). Old case_end() iterator will
2708   /// point to the added case.
2709   void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2710
2711   /// removeCase - This method removes the specified case and its successor
2712   /// from the switch instruction. Note that this operation may reorder the
2713   /// remaining cases at index idx and above.
2714   /// Note:
2715   /// This action invalidates iterators for all cases following the one removed,
2716   /// including the case_end() iterator.
2717   void removeCase(CaseIt i);
2718
2719   unsigned getNumSuccessors() const { return getNumOperands()/2; }
2720   BasicBlock *getSuccessor(unsigned idx) const {
2721     assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2722     return cast<BasicBlock>(getOperand(idx*2+1));
2723   }
2724   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2725     assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2726     setOperand(idx*2+1, (Value*)NewSucc);
2727   }
2728
2729   // Methods for support type inquiry through isa, cast, and dyn_cast:
2730   static inline bool classof(const Instruction *I) {
2731     return I->getOpcode() == Instruction::Switch;
2732   }
2733   static inline bool classof(const Value *V) {
2734     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2735   }
2736 private:
2737   BasicBlock *getSuccessorV(unsigned idx) const override;
2738   unsigned getNumSuccessorsV() const override;
2739   void setSuccessorV(unsigned idx, BasicBlock *B) override;
2740 };
2741
2742 template <>
2743 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2744 };
2745
2746 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2747
2748
2749 //===----------------------------------------------------------------------===//
2750 //                             IndirectBrInst Class
2751 //===----------------------------------------------------------------------===//
2752
2753 //===---------------------------------------------------------------------------
2754 /// IndirectBrInst - Indirect Branch Instruction.
2755 ///
2756 class IndirectBrInst : public TerminatorInst {
2757   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2758   unsigned ReservedSpace;
2759   // Operand[0]    = Value to switch on
2760   // Operand[1]    = Default basic block destination
2761   // Operand[2n  ] = Value to match
2762   // Operand[2n+1] = BasicBlock to go to on match
2763   IndirectBrInst(const IndirectBrInst &IBI);
2764   void init(Value *Address, unsigned NumDests);
2765   void growOperands();
2766   // allocate space for exactly zero operands
2767   void *operator new(size_t s) {
2768     return User::operator new(s, 0);
2769   }
2770   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2771   /// Address to jump to.  The number of expected destinations can be specified
2772   /// here to make memory allocation more efficient.  This constructor can also
2773   /// autoinsert before another instruction.
2774   IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2775
2776   /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2777   /// Address to jump to.  The number of expected destinations can be specified
2778   /// here to make memory allocation more efficient.  This constructor also
2779   /// autoinserts at the end of the specified BasicBlock.
2780   IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2781 protected:
2782   IndirectBrInst *clone_impl() const override;
2783 public:
2784   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2785                                 Instruction *InsertBefore = 0) {
2786     return new IndirectBrInst(Address, NumDests, InsertBefore);
2787   }
2788   static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2789                                 BasicBlock *InsertAtEnd) {
2790     return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2791   }
2792   ~IndirectBrInst();
2793
2794   /// Provide fast operand accessors.
2795   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2796
2797   // Accessor Methods for IndirectBrInst instruction.
2798   Value *getAddress() { return getOperand(0); }
2799   const Value *getAddress() const { return getOperand(0); }
2800   void setAddress(Value *V) { setOperand(0, V); }
2801
2802
2803   /// getNumDestinations - return the number of possible destinations in this
2804   /// indirectbr instruction.
2805   unsigned getNumDestinations() const { return getNumOperands()-1; }
2806
2807   /// getDestination - Return the specified destination.
2808   BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2809   const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2810
2811   /// addDestination - Add a destination.
2812   ///
2813   void addDestination(BasicBlock *Dest);
2814
2815   /// removeDestination - This method removes the specified successor from the
2816   /// indirectbr instruction.
2817   void removeDestination(unsigned i);
2818
2819   unsigned getNumSuccessors() const { return getNumOperands()-1; }
2820   BasicBlock *getSuccessor(unsigned i) const {
2821     return cast<BasicBlock>(getOperand(i+1));
2822   }
2823   void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2824     setOperand(i+1, (Value*)NewSucc);
2825   }
2826
2827   // Methods for support type inquiry through isa, cast, and dyn_cast:
2828   static inline bool classof(const Instruction *I) {
2829     return I->getOpcode() == Instruction::IndirectBr;
2830   }
2831   static inline bool classof(const Value *V) {
2832     return isa<Instruction>(V) && classof(cast<Instruction>(V));
2833   }
2834 private:
2835   BasicBlock *getSuccessorV(unsigned idx) const override;
2836   unsigned getNumSuccessorsV() const override;
2837   void setSuccessorV(unsigned idx, BasicBlock *B) override;
2838 };
2839
2840 template <>
2841 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2842 };
2843
2844 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2845
2846
2847 //===----------------------------------------------------------------------===//
2848 //                               InvokeInst Class
2849 //===----------------------------------------------------------------------===//
2850
2851 /// InvokeInst - Invoke instruction.  The SubclassData field is used to hold the
2852 /// calling convention of the call.
2853 ///
2854 class InvokeInst : public TerminatorInst {
2855   AttributeSet AttributeList;
2856   InvokeInst(const InvokeInst &BI);
2857   void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2858             ArrayRef<Value *> Args, const Twine &NameStr);
2859
2860   /// Construct an InvokeInst given a range of arguments.
2861   ///
2862   /// \brief Construct an InvokeInst from a range of arguments
2863   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2864                     ArrayRef<Value *> Args, unsigned Values,
2865                     const Twine &NameStr, Instruction *InsertBefore);
2866
2867   /// Construct an InvokeInst given a range of arguments.
2868   ///
2869   /// \brief Construct an InvokeInst from a range of arguments
2870   inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2871                     ArrayRef<Value *> Args, unsigned Values,
2872                     const Twine &NameStr, BasicBlock *InsertAtEnd);
2873 protected:
2874   InvokeInst *clone_impl() const override;
2875 public:
2876   static InvokeInst *Create(Value *Func,
2877                             BasicBlock *IfNormal, BasicBlock *IfException,
2878                             ArrayRef<Value *> Args, const Twine &NameStr = "",
2879                             Instruction *InsertBefore = 0) {
2880     unsigned Values = unsigned(Args.size()) + 3;
2881     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2882                                   Values, NameStr, InsertBefore);
2883   }
2884   static InvokeInst *Create(Value *Func,
2885                             BasicBlock *IfNormal, BasicBlock *IfException,
2886                             ArrayRef<Value *> Args, const Twine &NameStr,
2887                             BasicBlock *InsertAtEnd) {
2888     unsigned Values = unsigned(Args.size()) + 3;
2889     return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2890                                   Values, NameStr, InsertAtEnd);
2891   }
2892
2893   /// Provide fast operand accessors
2894   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2895
2896   /// getNumArgOperands - Return the number of invoke arguments.
2897   ///
2898   unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2899
2900   /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2901   ///
2902   Value *getArgOperand(unsigned i) const { return getOperand(i); }
2903   void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2904
2905   /// \brief Wrappers for getting the \c Use of a call argument.
2906   const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
2907   Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
2908
2909   /// getCallingConv/setCallingConv - Get or set the calling convention of this
2910   /// function call.
2911   CallingConv::ID getCallingConv() const {
2912     return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2913   }
2914   void setCallingConv(CallingConv::ID CC) {
2915     setInstructionSubclassData(static_cast<unsigned>(CC));
2916   }
2917
2918   /// getAttributes - Return the parameter attributes for this invoke.
2919   ///
2920   const AttributeSet &getAttributes() const { return AttributeList; }
2921
2922   /// setAttributes - Set the parameter attributes for this invoke.
2923   ///
2924   void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
2925
2926   /// addAttribute - adds the attribute to the list of attributes.
2927   void addAttribute(unsigned i, Attribute::AttrKind attr);
2928
2929   /// removeAttribute - removes the attribute from the list of attributes.
2930   void removeAttribute(unsigned i, Attribute attr);
2931
2932   /// \brief Determine whether this call has the given attribute.
2933   bool hasFnAttr(Attribute::AttrKind A) const {
2934     assert(A != Attribute::NoBuiltin &&
2935            "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
2936     return hasFnAttrImpl(A);
2937   }
2938
2939   /// \brief Determine whether the call or the callee has the given attributes.
2940   bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
2941
2942   /// \brief Extract the alignment for a call or parameter (0=unknown).
2943   unsigned getParamAlignment(unsigned i) const {
2944     return AttributeList.getParamAlignment(i);
2945   }
2946
2947   /// \brief Return true if the call should not be treated as a call to a
2948   /// builtin.
2949   bool isNoBuiltin() const {
2950     // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
2951     // to check it by hand.
2952     return hasFnAttrImpl(Attribute::NoBuiltin) &&
2953       !hasFnAttrImpl(Attribute::Builtin);
2954   }
2955
2956   /// \brief Return true if the call should not be inlined.
2957   bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
2958   void setIsNoInline() {
2959     addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
2960   }
2961
2962   /// \brief Determine if the call does not access memory.
2963   bool doesNotAccessMemory() const {
2964     return hasFnAttr(Attribute::ReadNone);
2965   }
2966   void setDoesNotAccessMemory() {
2967     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
2968   }
2969
2970   /// \brief Determine if the call does not access or only reads memory.
2971   bool onlyReadsMemory() const {
2972     return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
2973   }
2974   void setOnlyReadsMemory() {
2975     addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
2976   }
2977
2978   /// \brief Determine if the call cannot return.
2979   bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
2980   void setDoesNotReturn() {
2981     addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
2982   }
2983
2984   /// \brief Determine if the call cannot unwind.
2985   bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
2986   void setDoesNotThrow() {
2987     addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
2988   }
2989
2990   /// \brief Determine if the call returns a structure through first
2991   /// pointer argument.
2992   bool hasStructRetAttr() const {
2993     // Be friendly and also check the callee.
2994     return paramHasAttr(1, Attribute::StructRet);
2995   }
2996
2997   /// \brief Determine if any call argument is an aggregate passed by value.
2998   bool hasByValArgument() const {
2999     return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3000   }
3001
3002   /// getCalledFunction - Return the function called, or null if this is an
3003   /// indirect function invocation.
3004   ///
3005   Function *getCalledFunction() const {
3006     return dyn_cast<Function>(Op<-3>());
3007   }
3008
3009   /// getCalledValue - Get a pointer to the function that is invoked by this
3010   /// instruction
3011   const Value *getCalledValue() const { return Op<-3>(); }
3012         Value *getCalledValue()       { return Op<-3>(); }
3013
3014   /// setCalledFunction - Set the function called.
3015   void setCalledFunction(Value* Fn) {
3016     Op<-3>() = Fn;
3017   }
3018
3019   // get*Dest - Return the destination basic blocks...
3020   BasicBlock *getNormalDest() const {
3021     return cast<BasicBlock>(Op<-2>());
3022   }
3023   BasicBlock *getUnwindDest() const {
3024     return cast<BasicBlock>(Op<-1>());
3025   }
3026   void setNormalDest(BasicBlock *B) {
3027     Op<-2>() = reinterpret_cast<Value*>(B);
3028   }
3029   void setUnwindDest(BasicBlock *B) {
3030     Op<-1>() = reinterpret_cast<Value*>(B);
3031   }
3032
3033   /// getLandingPadInst - Get the landingpad instruction from the landing pad
3034   /// block (the unwind destination).
3035   LandingPadInst *getLandingPadInst() const;
3036
3037   BasicBlock *getSuccessor(unsigned i) const {
3038     assert(i < 2 && "Successor # out of range for invoke!");
3039     return i == 0 ? getNormalDest() : getUnwindDest();
3040   }
3041
3042   void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3043     assert(idx < 2 && "Successor # out of range for invoke!");
3044     *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3045   }
3046
3047   unsigned getNumSuccessors() const { return 2; }
3048
3049   // Methods for support type inquiry through isa, cast, and dyn_cast:
3050   static inline bool classof(const Instruction *I) {
3051     return (I->getOpcode() == Instruction::Invoke);
3052   }
3053   static inline bool classof(const Value *V) {
3054     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3055   }
3056
3057 private:
3058   BasicBlock *getSuccessorV(unsigned idx) const override;
3059   unsigned getNumSuccessorsV() const override;
3060   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3061
3062   bool hasFnAttrImpl(Attribute::AttrKind A) const;
3063
3064   // Shadow Instruction::setInstructionSubclassData with a private forwarding
3065   // method so that subclasses cannot accidentally use it.
3066   void setInstructionSubclassData(unsigned short D) {
3067     Instruction::setInstructionSubclassData(D);
3068   }
3069 };
3070
3071 template <>
3072 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3073 };
3074
3075 InvokeInst::InvokeInst(Value *Func,
3076                        BasicBlock *IfNormal, BasicBlock *IfException,
3077                        ArrayRef<Value *> Args, unsigned Values,
3078                        const Twine &NameStr, Instruction *InsertBefore)
3079   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3080                                       ->getElementType())->getReturnType(),
3081                    Instruction::Invoke,
3082                    OperandTraits<InvokeInst>::op_end(this) - Values,
3083                    Values, InsertBefore) {
3084   init(Func, IfNormal, IfException, Args, NameStr);
3085 }
3086 InvokeInst::InvokeInst(Value *Func,
3087                        BasicBlock *IfNormal, BasicBlock *IfException,
3088                        ArrayRef<Value *> Args, unsigned Values,
3089                        const Twine &NameStr, BasicBlock *InsertAtEnd)
3090   : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3091                                       ->getElementType())->getReturnType(),
3092                    Instruction::Invoke,
3093                    OperandTraits<InvokeInst>::op_end(this) - Values,
3094                    Values, InsertAtEnd) {
3095   init(Func, IfNormal, IfException, Args, NameStr);
3096 }
3097
3098 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3099
3100 //===----------------------------------------------------------------------===//
3101 //                              ResumeInst Class
3102 //===----------------------------------------------------------------------===//
3103
3104 //===---------------------------------------------------------------------------
3105 /// ResumeInst - Resume the propagation of an exception.
3106 ///
3107 class ResumeInst : public TerminatorInst {
3108   ResumeInst(const ResumeInst &RI);
3109
3110   explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3111   ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3112 protected:
3113   ResumeInst *clone_impl() const override;
3114 public:
3115   static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3116     return new(1) ResumeInst(Exn, InsertBefore);
3117   }
3118   static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3119     return new(1) ResumeInst(Exn, InsertAtEnd);
3120   }
3121
3122   /// Provide fast operand accessors
3123   DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3124
3125   /// Convenience accessor.
3126   Value *getValue() const { return Op<0>(); }
3127
3128   unsigned getNumSuccessors() const { return 0; }
3129
3130   // Methods for support type inquiry through isa, cast, and dyn_cast:
3131   static inline bool classof(const Instruction *I) {
3132     return I->getOpcode() == Instruction::Resume;
3133   }
3134   static inline bool classof(const Value *V) {
3135     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3136   }
3137 private:
3138   BasicBlock *getSuccessorV(unsigned idx) const override;
3139   unsigned getNumSuccessorsV() const override;
3140   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3141 };
3142
3143 template <>
3144 struct OperandTraits<ResumeInst> :
3145     public FixedNumOperandTraits<ResumeInst, 1> {
3146 };
3147
3148 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3149
3150 //===----------------------------------------------------------------------===//
3151 //                           UnreachableInst Class
3152 //===----------------------------------------------------------------------===//
3153
3154 //===---------------------------------------------------------------------------
3155 /// UnreachableInst - This function has undefined behavior.  In particular, the
3156 /// presence of this instruction indicates some higher level knowledge that the
3157 /// end of the block cannot be reached.
3158 ///
3159 class UnreachableInst : public TerminatorInst {
3160   void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3161 protected:
3162   UnreachableInst *clone_impl() const override;
3163
3164 public:
3165   // allocate space for exactly zero operands
3166   void *operator new(size_t s) {
3167     return User::operator new(s, 0);
3168   }
3169   explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3170   explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3171
3172   unsigned getNumSuccessors() const { return 0; }
3173
3174   // Methods for support type inquiry through isa, cast, and dyn_cast:
3175   static inline bool classof(const Instruction *I) {
3176     return I->getOpcode() == Instruction::Unreachable;
3177   }
3178   static inline bool classof(const Value *V) {
3179     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3180   }
3181 private:
3182   BasicBlock *getSuccessorV(unsigned idx) const override;
3183   unsigned getNumSuccessorsV() const override;
3184   void setSuccessorV(unsigned idx, BasicBlock *B) override;
3185 };
3186
3187 //===----------------------------------------------------------------------===//
3188 //                                 TruncInst Class
3189 //===----------------------------------------------------------------------===//
3190
3191 /// \brief This class represents a truncation of integer types.
3192 class TruncInst : public CastInst {
3193 protected:
3194   /// \brief Clone an identical TruncInst
3195   TruncInst *clone_impl() const override;
3196
3197 public:
3198   /// \brief Constructor with insert-before-instruction semantics
3199   TruncInst(
3200     Value *S,                     ///< The value to be truncated
3201     Type *Ty,               ///< The (smaller) type to truncate to
3202     const Twine &NameStr = "",    ///< A name for the new instruction
3203     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3204   );
3205
3206   /// \brief Constructor with insert-at-end-of-block semantics
3207   TruncInst(
3208     Value *S,                     ///< The value to be truncated
3209     Type *Ty,               ///< The (smaller) type to truncate to
3210     const Twine &NameStr,         ///< A name for the new instruction
3211     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3212   );
3213
3214   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3215   static inline bool classof(const Instruction *I) {
3216     return I->getOpcode() == Trunc;
3217   }
3218   static inline bool classof(const Value *V) {
3219     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3220   }
3221 };
3222
3223 //===----------------------------------------------------------------------===//
3224 //                                 ZExtInst Class
3225 //===----------------------------------------------------------------------===//
3226
3227 /// \brief This class represents zero extension of integer types.
3228 class ZExtInst : public CastInst {
3229 protected:
3230   /// \brief Clone an identical ZExtInst
3231   ZExtInst *clone_impl() const override;
3232
3233 public:
3234   /// \brief Constructor with insert-before-instruction semantics
3235   ZExtInst(
3236     Value *S,                     ///< The value to be zero extended
3237     Type *Ty,               ///< The type to zero extend to
3238     const Twine &NameStr = "",    ///< A name for the new instruction
3239     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3240   );
3241
3242   /// \brief Constructor with insert-at-end semantics.
3243   ZExtInst(
3244     Value *S,                     ///< The value to be zero extended
3245     Type *Ty,               ///< The type to zero extend to
3246     const Twine &NameStr,         ///< A name for the new instruction
3247     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3248   );
3249
3250   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3251   static inline bool classof(const Instruction *I) {
3252     return I->getOpcode() == ZExt;
3253   }
3254   static inline bool classof(const Value *V) {
3255     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3256   }
3257 };
3258
3259 //===----------------------------------------------------------------------===//
3260 //                                 SExtInst Class
3261 //===----------------------------------------------------------------------===//
3262
3263 /// \brief This class represents a sign extension of integer types.
3264 class SExtInst : public CastInst {
3265 protected:
3266   /// \brief Clone an identical SExtInst
3267   SExtInst *clone_impl() const override;
3268
3269 public:
3270   /// \brief Constructor with insert-before-instruction semantics
3271   SExtInst(
3272     Value *S,                     ///< The value to be sign extended
3273     Type *Ty,               ///< The type to sign extend to
3274     const Twine &NameStr = "",    ///< A name for the new instruction
3275     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3276   );
3277
3278   /// \brief Constructor with insert-at-end-of-block semantics
3279   SExtInst(
3280     Value *S,                     ///< The value to be sign extended
3281     Type *Ty,               ///< The type to sign extend to
3282     const Twine &NameStr,         ///< A name for the new instruction
3283     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3284   );
3285
3286   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3287   static inline bool classof(const Instruction *I) {
3288     return I->getOpcode() == SExt;
3289   }
3290   static inline bool classof(const Value *V) {
3291     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3292   }
3293 };
3294
3295 //===----------------------------------------------------------------------===//
3296 //                                 FPTruncInst Class
3297 //===----------------------------------------------------------------------===//
3298
3299 /// \brief This class represents a truncation of floating point types.
3300 class FPTruncInst : public CastInst {
3301 protected:
3302   /// \brief Clone an identical FPTruncInst
3303   FPTruncInst *clone_impl() const override;
3304
3305 public:
3306   /// \brief Constructor with insert-before-instruction semantics
3307   FPTruncInst(
3308     Value *S,                     ///< The value to be truncated
3309     Type *Ty,               ///< The type to truncate to
3310     const Twine &NameStr = "",    ///< A name for the new instruction
3311     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3312   );
3313
3314   /// \brief Constructor with insert-before-instruction semantics
3315   FPTruncInst(
3316     Value *S,                     ///< The value to be truncated
3317     Type *Ty,               ///< The type to truncate to
3318     const Twine &NameStr,         ///< A name for the new instruction
3319     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3320   );
3321
3322   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3323   static inline bool classof(const Instruction *I) {
3324     return I->getOpcode() == FPTrunc;
3325   }
3326   static inline bool classof(const Value *V) {
3327     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3328   }
3329 };
3330
3331 //===----------------------------------------------------------------------===//
3332 //                                 FPExtInst Class
3333 //===----------------------------------------------------------------------===//
3334
3335 /// \brief This class represents an extension of floating point types.
3336 class FPExtInst : public CastInst {
3337 protected:
3338   /// \brief Clone an identical FPExtInst
3339   FPExtInst *clone_impl() const override;
3340
3341 public:
3342   /// \brief Constructor with insert-before-instruction semantics
3343   FPExtInst(
3344     Value *S,                     ///< The value to be extended
3345     Type *Ty,               ///< The type to extend to
3346     const Twine &NameStr = "",    ///< A name for the new instruction
3347     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3348   );
3349
3350   /// \brief Constructor with insert-at-end-of-block semantics
3351   FPExtInst(
3352     Value *S,                     ///< The value to be extended
3353     Type *Ty,               ///< The type to extend to
3354     const Twine &NameStr,         ///< A name for the new instruction
3355     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3356   );
3357
3358   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3359   static inline bool classof(const Instruction *I) {
3360     return I->getOpcode() == FPExt;
3361   }
3362   static inline bool classof(const Value *V) {
3363     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3364   }
3365 };
3366
3367 //===----------------------------------------------------------------------===//
3368 //                                 UIToFPInst Class
3369 //===----------------------------------------------------------------------===//
3370
3371 /// \brief This class represents a cast unsigned integer to floating point.
3372 class UIToFPInst : public CastInst {
3373 protected:
3374   /// \brief Clone an identical UIToFPInst
3375   UIToFPInst *clone_impl() const override;
3376
3377 public:
3378   /// \brief Constructor with insert-before-instruction semantics
3379   UIToFPInst(
3380     Value *S,                     ///< The value to be converted
3381     Type *Ty,               ///< The type to convert to
3382     const Twine &NameStr = "",    ///< A name for the new instruction
3383     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3384   );
3385
3386   /// \brief Constructor with insert-at-end-of-block semantics
3387   UIToFPInst(
3388     Value *S,                     ///< The value to be converted
3389     Type *Ty,               ///< The type to convert to
3390     const Twine &NameStr,         ///< A name for the new instruction
3391     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3392   );
3393
3394   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3395   static inline bool classof(const Instruction *I) {
3396     return I->getOpcode() == UIToFP;
3397   }
3398   static inline bool classof(const Value *V) {
3399     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3400   }
3401 };
3402
3403 //===----------------------------------------------------------------------===//
3404 //                                 SIToFPInst Class
3405 //===----------------------------------------------------------------------===//
3406
3407 /// \brief This class represents a cast from signed integer to floating point.
3408 class SIToFPInst : public CastInst {
3409 protected:
3410   /// \brief Clone an identical SIToFPInst
3411   SIToFPInst *clone_impl() const override;
3412
3413 public:
3414   /// \brief Constructor with insert-before-instruction semantics
3415   SIToFPInst(
3416     Value *S,                     ///< The value to be converted
3417     Type *Ty,               ///< The type to convert to
3418     const Twine &NameStr = "",    ///< A name for the new instruction
3419     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3420   );
3421
3422   /// \brief Constructor with insert-at-end-of-block semantics
3423   SIToFPInst(
3424     Value *S,                     ///< The value to be converted
3425     Type *Ty,               ///< The type to convert to
3426     const Twine &NameStr,         ///< A name for the new instruction
3427     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3428   );
3429
3430   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3431   static inline bool classof(const Instruction *I) {
3432     return I->getOpcode() == SIToFP;
3433   }
3434   static inline bool classof(const Value *V) {
3435     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3436   }
3437 };
3438
3439 //===----------------------------------------------------------------------===//
3440 //                                 FPToUIInst Class
3441 //===----------------------------------------------------------------------===//
3442
3443 /// \brief This class represents a cast from floating point to unsigned integer
3444 class FPToUIInst  : public CastInst {
3445 protected:
3446   /// \brief Clone an identical FPToUIInst
3447   FPToUIInst *clone_impl() const override;
3448
3449 public:
3450   /// \brief Constructor with insert-before-instruction semantics
3451   FPToUIInst(
3452     Value *S,                     ///< The value to be converted
3453     Type *Ty,               ///< The type to convert to
3454     const Twine &NameStr = "",    ///< A name for the new instruction
3455     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3456   );
3457
3458   /// \brief Constructor with insert-at-end-of-block semantics
3459   FPToUIInst(
3460     Value *S,                     ///< The value to be converted
3461     Type *Ty,               ///< The type to convert to
3462     const Twine &NameStr,         ///< A name for the new instruction
3463     BasicBlock *InsertAtEnd       ///< Where to insert the new instruction
3464   );
3465
3466   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3467   static inline bool classof(const Instruction *I) {
3468     return I->getOpcode() == FPToUI;
3469   }
3470   static inline bool classof(const Value *V) {
3471     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3472   }
3473 };
3474
3475 //===----------------------------------------------------------------------===//
3476 //                                 FPToSIInst Class
3477 //===----------------------------------------------------------------------===//
3478
3479 /// \brief This class represents a cast from floating point to signed integer.
3480 class FPToSIInst  : public CastInst {
3481 protected:
3482   /// \brief Clone an identical FPToSIInst
3483   FPToSIInst *clone_impl() const override;
3484
3485 public:
3486   /// \brief Constructor with insert-before-instruction semantics
3487   FPToSIInst(
3488     Value *S,                     ///< The value to be converted
3489     Type *Ty,               ///< The type to convert to
3490     const Twine &NameStr = "",    ///< A name for the new instruction
3491     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3492   );
3493
3494   /// \brief Constructor with insert-at-end-of-block semantics
3495   FPToSIInst(
3496     Value *S,                     ///< The value to be converted
3497     Type *Ty,               ///< The type to convert to
3498     const Twine &NameStr,         ///< A name for the new instruction
3499     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3500   );
3501
3502   /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3503   static inline bool classof(const Instruction *I) {
3504     return I->getOpcode() == FPToSI;
3505   }
3506   static inline bool classof(const Value *V) {
3507     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3508   }
3509 };
3510
3511 //===----------------------------------------------------------------------===//
3512 //                                 IntToPtrInst Class
3513 //===----------------------------------------------------------------------===//
3514
3515 /// \brief This class represents a cast from an integer to a pointer.
3516 class IntToPtrInst : public CastInst {
3517 public:
3518   /// \brief Constructor with insert-before-instruction semantics
3519   IntToPtrInst(
3520     Value *S,                     ///< The value to be converted
3521     Type *Ty,               ///< The type to convert to
3522     const Twine &NameStr = "",    ///< A name for the new instruction
3523     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3524   );
3525
3526   /// \brief Constructor with insert-at-end-of-block semantics
3527   IntToPtrInst(
3528     Value *S,                     ///< The value to be converted
3529     Type *Ty,               ///< The type to convert to
3530     const Twine &NameStr,         ///< A name for the new instruction
3531     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3532   );
3533
3534   /// \brief Clone an identical IntToPtrInst
3535   IntToPtrInst *clone_impl() const override;
3536
3537   /// \brief Returns the address space of this instruction's pointer type.
3538   unsigned getAddressSpace() const {
3539     return getType()->getPointerAddressSpace();
3540   }
3541
3542   // Methods for support type inquiry through isa, cast, and dyn_cast:
3543   static inline bool classof(const Instruction *I) {
3544     return I->getOpcode() == IntToPtr;
3545   }
3546   static inline bool classof(const Value *V) {
3547     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3548   }
3549 };
3550
3551 //===----------------------------------------------------------------------===//
3552 //                                 PtrToIntInst Class
3553 //===----------------------------------------------------------------------===//
3554
3555 /// \brief This class represents a cast from a pointer to an integer
3556 class PtrToIntInst : public CastInst {
3557 protected:
3558   /// \brief Clone an identical PtrToIntInst
3559   PtrToIntInst *clone_impl() const override;
3560
3561 public:
3562   /// \brief Constructor with insert-before-instruction semantics
3563   PtrToIntInst(
3564     Value *S,                     ///< The value to be converted
3565     Type *Ty,               ///< The type to convert to
3566     const Twine &NameStr = "",    ///< A name for the new instruction
3567     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3568   );
3569
3570   /// \brief Constructor with insert-at-end-of-block semantics
3571   PtrToIntInst(
3572     Value *S,                     ///< The value to be converted
3573     Type *Ty,               ///< The type to convert to
3574     const Twine &NameStr,         ///< A name for the new instruction
3575     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3576   );
3577
3578   /// \brief Gets the pointer operand.
3579   Value *getPointerOperand() { return getOperand(0); }
3580   /// \brief Gets the pointer operand.
3581   const Value *getPointerOperand() const { return getOperand(0); }
3582   /// \brief Gets the operand index of the pointer operand.
3583   static unsigned getPointerOperandIndex() { return 0U; }
3584
3585   /// \brief Returns the address space of the pointer operand.
3586   unsigned getPointerAddressSpace() const {
3587     return getPointerOperand()->getType()->getPointerAddressSpace();
3588   }
3589
3590   // Methods for support type inquiry through isa, cast, and dyn_cast:
3591   static inline bool classof(const Instruction *I) {
3592     return I->getOpcode() == PtrToInt;
3593   }
3594   static inline bool classof(const Value *V) {
3595     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3596   }
3597 };
3598
3599 //===----------------------------------------------------------------------===//
3600 //                             BitCastInst Class
3601 //===----------------------------------------------------------------------===//
3602
3603 /// \brief This class represents a no-op cast from one type to another.
3604 class BitCastInst : public CastInst {
3605 protected:
3606   /// \brief Clone an identical BitCastInst
3607   BitCastInst *clone_impl() const override;
3608
3609 public:
3610   /// \brief Constructor with insert-before-instruction semantics
3611   BitCastInst(
3612     Value *S,                     ///< The value to be casted
3613     Type *Ty,               ///< The type to casted to
3614     const Twine &NameStr = "",    ///< A name for the new instruction
3615     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3616   );
3617
3618   /// \brief Constructor with insert-at-end-of-block semantics
3619   BitCastInst(
3620     Value *S,                     ///< The value to be casted
3621     Type *Ty,               ///< The type to casted to
3622     const Twine &NameStr,         ///< A name for the new instruction
3623     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3624   );
3625
3626   // Methods for support type inquiry through isa, cast, and dyn_cast:
3627   static inline bool classof(const Instruction *I) {
3628     return I->getOpcode() == BitCast;
3629   }
3630   static inline bool classof(const Value *V) {
3631     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3632   }
3633 };
3634
3635 //===----------------------------------------------------------------------===//
3636 //                          AddrSpaceCastInst Class
3637 //===----------------------------------------------------------------------===//
3638
3639 /// \brief This class represents a conversion between pointers from
3640 /// one address space to another.
3641 class AddrSpaceCastInst : public CastInst {
3642 protected:
3643   /// \brief Clone an identical AddrSpaceCastInst
3644   AddrSpaceCastInst *clone_impl() const override;
3645
3646 public:
3647   /// \brief Constructor with insert-before-instruction semantics
3648   AddrSpaceCastInst(
3649     Value *S,                     ///< The value to be casted
3650     Type *Ty,                     ///< The type to casted to
3651     const Twine &NameStr = "",    ///< A name for the new instruction
3652     Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3653   );
3654
3655   /// \brief Constructor with insert-at-end-of-block semantics
3656   AddrSpaceCastInst(
3657     Value *S,                     ///< The value to be casted
3658     Type *Ty,                     ///< The type to casted to
3659     const Twine &NameStr,         ///< A name for the new instruction
3660     BasicBlock *InsertAtEnd       ///< The block to insert the instruction into
3661   );
3662
3663   // Methods for support type inquiry through isa, cast, and dyn_cast:
3664   static inline bool classof(const Instruction *I) {
3665     return I->getOpcode() == AddrSpaceCast;
3666   }
3667   static inline bool classof(const Value *V) {
3668     return isa<Instruction>(V) && classof(cast<Instruction>(V));
3669   }
3670 };
3671
3672 } // End llvm namespace
3673
3674 #endif