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