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