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