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