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