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