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