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