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