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