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