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