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