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