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