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