1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
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.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_IR_INSTRUCTIONS_H
17 #define LLVM_IR_INSTRUCTIONS_H
19 #include "llvm/ADT/ArrayRef.h"
20 #include "llvm/ADT/SmallVector.h"
21 #include "llvm/ADT/iterator_range.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/Function.h"
26 #include "llvm/IR/InstrTypes.h"
27 #include "llvm/Support/ErrorHandling.h"
42 // Consume = 3, // Not specified yet.
46 SequentiallyConsistent = 7
49 enum SynchronizationScope {
54 /// Returns true if the ordering is at least as strong as acquire
55 /// (i.e. acquire, acq_rel or seq_cst)
56 inline bool isAtLeastAcquire(AtomicOrdering Ord) {
57 return (Ord == Acquire ||
58 Ord == AcquireRelease ||
59 Ord == SequentiallyConsistent);
62 /// Returns true if the ordering is at least as strong as release
63 /// (i.e. release, acq_rel or seq_cst)
64 inline bool isAtLeastRelease(AtomicOrdering Ord) {
65 return (Ord == Release ||
66 Ord == AcquireRelease ||
67 Ord == SequentiallyConsistent);
70 //===----------------------------------------------------------------------===//
72 //===----------------------------------------------------------------------===//
74 /// AllocaInst - an instruction to allocate memory on the stack
76 class AllocaInst : public UnaryInstruction {
80 // Note: Instruction needs to be a friend here to call cloneImpl.
81 friend class Instruction;
82 AllocaInst *cloneImpl() const;
85 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
86 const Twine &Name = "",
87 Instruction *InsertBefore = nullptr);
88 AllocaInst(Type *Ty, Value *ArraySize,
89 const Twine &Name, BasicBlock *InsertAtEnd);
91 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
92 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
94 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
95 const Twine &Name = "", Instruction *InsertBefore = nullptr);
96 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
97 const Twine &Name, BasicBlock *InsertAtEnd);
99 // Out of line virtual method, so the vtable, etc. has a home.
100 ~AllocaInst() override;
102 /// isArrayAllocation - Return true if there is an allocation size parameter
103 /// to the allocation instruction that is not 1.
105 bool isArrayAllocation() const;
107 /// getArraySize - Get the number of elements allocated. For a simple
108 /// allocation of a single element, this will return a constant 1 value.
110 const Value *getArraySize() const { return getOperand(0); }
111 Value *getArraySize() { return getOperand(0); }
113 /// getType - Overload to return most specific pointer type
115 PointerType *getType() const {
116 return cast<PointerType>(Instruction::getType());
119 /// getAllocatedType - Return the type that is being allocated by the
122 Type *getAllocatedType() const { return AllocatedType; }
123 /// \brief for use only in special circumstances that need to generically
124 /// transform a whole instruction (eg: IR linking and vectorization).
125 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
127 /// getAlignment - Return the alignment of the memory that is being allocated
128 /// by the instruction.
130 unsigned getAlignment() const {
131 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
133 void setAlignment(unsigned Align);
135 /// isStaticAlloca - Return true if this alloca is in the entry block of the
136 /// function and is a constant size. If so, the code generator will fold it
137 /// into the prolog/epilog code, so it is basically free.
138 bool isStaticAlloca() const;
140 /// \brief Return true if this alloca is used as an inalloca argument to a
141 /// call. Such allocas are never considered static even if they are in the
143 bool isUsedWithInAlloca() const {
144 return getSubclassDataFromInstruction() & 32;
147 /// \brief Specify whether this alloca is used to represent the arguments to
149 void setUsedWithInAlloca(bool V) {
150 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
154 // Methods for support type inquiry through isa, cast, and dyn_cast:
155 static inline bool classof(const Instruction *I) {
156 return (I->getOpcode() == Instruction::Alloca);
158 static inline bool classof(const Value *V) {
159 return isa<Instruction>(V) && classof(cast<Instruction>(V));
162 // Shadow Instruction::setInstructionSubclassData with a private forwarding
163 // method so that subclasses cannot accidentally use it.
164 void setInstructionSubclassData(unsigned short D) {
165 Instruction::setInstructionSubclassData(D);
170 //===----------------------------------------------------------------------===//
172 //===----------------------------------------------------------------------===//
174 /// LoadInst - an instruction for reading from memory. This uses the
175 /// SubclassData field in Value to store whether or not the load is volatile.
177 class LoadInst : public UnaryInstruction {
180 // Note: Instruction needs to be a friend here to call cloneImpl.
181 friend class Instruction;
182 LoadInst *cloneImpl() const;
185 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
186 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
187 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile = false,
188 Instruction *InsertBefore = nullptr);
189 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
190 Instruction *InsertBefore = nullptr)
191 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
192 NameStr, isVolatile, InsertBefore) {}
193 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
194 BasicBlock *InsertAtEnd);
195 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
196 Instruction *InsertBefore = nullptr)
197 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
198 NameStr, isVolatile, Align, InsertBefore) {}
199 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
200 unsigned Align, Instruction *InsertBefore = nullptr);
201 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
202 unsigned Align, BasicBlock *InsertAtEnd);
203 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
204 AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread,
205 Instruction *InsertBefore = nullptr)
206 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
207 NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {}
208 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
209 unsigned Align, AtomicOrdering Order,
210 SynchronizationScope SynchScope = CrossThread,
211 Instruction *InsertBefore = nullptr);
212 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
213 unsigned Align, AtomicOrdering Order,
214 SynchronizationScope SynchScope,
215 BasicBlock *InsertAtEnd);
217 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
218 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
219 LoadInst(Type *Ty, Value *Ptr, const char *NameStr = nullptr,
220 bool isVolatile = false, Instruction *InsertBefore = nullptr);
221 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
222 bool isVolatile = false,
223 Instruction *InsertBefore = nullptr)
224 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
225 NameStr, isVolatile, InsertBefore) {}
226 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
227 BasicBlock *InsertAtEnd);
229 /// isVolatile - Return true if this is a load from a volatile memory
232 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
234 /// setVolatile - Specify whether this is a volatile load or not.
236 void setVolatile(bool V) {
237 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
241 /// getAlignment - Return the alignment of the access that is being performed
243 unsigned getAlignment() const {
244 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
247 void setAlignment(unsigned Align);
249 /// Returns the ordering effect of this fence.
250 AtomicOrdering getOrdering() const {
251 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
254 /// Set the ordering constraint on this load. May not be Release or
256 void setOrdering(AtomicOrdering Ordering) {
257 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
261 SynchronizationScope getSynchScope() const {
262 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
265 /// Specify whether this load is ordered with respect to all
266 /// concurrently executing threads, or only with respect to signal handlers
267 /// executing in the same thread.
268 void setSynchScope(SynchronizationScope xthread) {
269 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
273 void setAtomic(AtomicOrdering Ordering,
274 SynchronizationScope SynchScope = CrossThread) {
275 setOrdering(Ordering);
276 setSynchScope(SynchScope);
279 bool isSimple() const { return !isAtomic() && !isVolatile(); }
280 bool isUnordered() const {
281 return getOrdering() <= Unordered && !isVolatile();
284 Value *getPointerOperand() { return getOperand(0); }
285 const Value *getPointerOperand() const { return getOperand(0); }
286 static unsigned getPointerOperandIndex() { return 0U; }
288 /// \brief Returns the address space of the pointer operand.
289 unsigned getPointerAddressSpace() const {
290 return getPointerOperand()->getType()->getPointerAddressSpace();
294 // Methods for support type inquiry through isa, cast, and dyn_cast:
295 static inline bool classof(const Instruction *I) {
296 return I->getOpcode() == Instruction::Load;
298 static inline bool classof(const Value *V) {
299 return isa<Instruction>(V) && classof(cast<Instruction>(V));
302 // Shadow Instruction::setInstructionSubclassData with a private forwarding
303 // method so that subclasses cannot accidentally use it.
304 void setInstructionSubclassData(unsigned short D) {
305 Instruction::setInstructionSubclassData(D);
310 //===----------------------------------------------------------------------===//
312 //===----------------------------------------------------------------------===//
314 /// StoreInst - an instruction for storing to memory
316 class StoreInst : public Instruction {
317 void *operator new(size_t, unsigned) = delete;
320 // Note: Instruction needs to be a friend here to call cloneImpl.
321 friend class Instruction;
322 StoreInst *cloneImpl() const;
325 // allocate space for exactly two operands
326 void *operator new(size_t s) {
327 return User::operator new(s, 2);
329 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
330 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
331 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
332 Instruction *InsertBefore = nullptr);
333 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
334 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
335 unsigned Align, Instruction *InsertBefore = nullptr);
336 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
337 unsigned Align, BasicBlock *InsertAtEnd);
338 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
339 unsigned Align, AtomicOrdering Order,
340 SynchronizationScope SynchScope = CrossThread,
341 Instruction *InsertBefore = nullptr);
342 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
343 unsigned Align, AtomicOrdering Order,
344 SynchronizationScope SynchScope,
345 BasicBlock *InsertAtEnd);
348 /// isVolatile - Return true if this is a store to a volatile memory
351 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
353 /// setVolatile - Specify whether this is a volatile store or not.
355 void setVolatile(bool V) {
356 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
360 /// Transparently provide more efficient getOperand methods.
361 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
363 /// getAlignment - Return the alignment of the access that is being performed
365 unsigned getAlignment() const {
366 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
369 void setAlignment(unsigned Align);
371 /// Returns the ordering effect of this store.
372 AtomicOrdering getOrdering() const {
373 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
376 /// Set the ordering constraint on this store. May not be Acquire or
378 void setOrdering(AtomicOrdering Ordering) {
379 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
383 SynchronizationScope getSynchScope() const {
384 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
387 /// Specify whether this store instruction is ordered with respect to all
388 /// concurrently executing threads, or only with respect to signal handlers
389 /// executing in the same thread.
390 void setSynchScope(SynchronizationScope xthread) {
391 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
395 void setAtomic(AtomicOrdering Ordering,
396 SynchronizationScope SynchScope = CrossThread) {
397 setOrdering(Ordering);
398 setSynchScope(SynchScope);
401 bool isSimple() const { return !isAtomic() && !isVolatile(); }
402 bool isUnordered() const {
403 return getOrdering() <= Unordered && !isVolatile();
406 Value *getValueOperand() { return getOperand(0); }
407 const Value *getValueOperand() const { return getOperand(0); }
409 Value *getPointerOperand() { return getOperand(1); }
410 const Value *getPointerOperand() const { return getOperand(1); }
411 static unsigned getPointerOperandIndex() { return 1U; }
413 /// \brief Returns the address space of the pointer operand.
414 unsigned getPointerAddressSpace() const {
415 return getPointerOperand()->getType()->getPointerAddressSpace();
418 // Methods for support type inquiry through isa, cast, and dyn_cast:
419 static inline bool classof(const Instruction *I) {
420 return I->getOpcode() == Instruction::Store;
422 static inline bool classof(const Value *V) {
423 return isa<Instruction>(V) && classof(cast<Instruction>(V));
426 // Shadow Instruction::setInstructionSubclassData with a private forwarding
427 // method so that subclasses cannot accidentally use it.
428 void setInstructionSubclassData(unsigned short D) {
429 Instruction::setInstructionSubclassData(D);
434 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
437 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
439 //===----------------------------------------------------------------------===//
441 //===----------------------------------------------------------------------===//
443 /// FenceInst - an instruction for ordering other memory operations
445 class FenceInst : public Instruction {
446 void *operator new(size_t, unsigned) = delete;
447 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
449 // Note: Instruction needs to be a friend here to call cloneImpl.
450 friend class Instruction;
451 FenceInst *cloneImpl() const;
454 // allocate space for exactly zero operands
455 void *operator new(size_t s) {
456 return User::operator new(s, 0);
459 // Ordering may only be Acquire, Release, AcquireRelease, or
460 // SequentiallyConsistent.
461 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
462 SynchronizationScope SynchScope = CrossThread,
463 Instruction *InsertBefore = nullptr);
464 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
465 SynchronizationScope SynchScope,
466 BasicBlock *InsertAtEnd);
468 /// Returns the ordering effect of this fence.
469 AtomicOrdering getOrdering() const {
470 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
473 /// Set the ordering constraint on this fence. May only be Acquire, Release,
474 /// AcquireRelease, or SequentiallyConsistent.
475 void setOrdering(AtomicOrdering Ordering) {
476 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
480 SynchronizationScope getSynchScope() const {
481 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
484 /// Specify whether this fence orders other operations with respect to all
485 /// concurrently executing threads, or only with respect to signal handlers
486 /// executing in the same thread.
487 void setSynchScope(SynchronizationScope xthread) {
488 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
492 // Methods for support type inquiry through isa, cast, and dyn_cast:
493 static inline bool classof(const Instruction *I) {
494 return I->getOpcode() == Instruction::Fence;
496 static inline bool classof(const Value *V) {
497 return isa<Instruction>(V) && classof(cast<Instruction>(V));
500 // Shadow Instruction::setInstructionSubclassData with a private forwarding
501 // method so that subclasses cannot accidentally use it.
502 void setInstructionSubclassData(unsigned short D) {
503 Instruction::setInstructionSubclassData(D);
507 //===----------------------------------------------------------------------===//
508 // AtomicCmpXchgInst Class
509 //===----------------------------------------------------------------------===//
511 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
512 /// specified value is in a memory location, and, if it is, stores a new value
513 /// there. Returns the value that was loaded.
515 class AtomicCmpXchgInst : public Instruction {
516 void *operator new(size_t, unsigned) = delete;
517 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
518 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
519 SynchronizationScope SynchScope);
521 // Note: Instruction needs to be a friend here to call cloneImpl.
522 friend class Instruction;
523 AtomicCmpXchgInst *cloneImpl() const;
526 // allocate space for exactly three operands
527 void *operator new(size_t s) {
528 return User::operator new(s, 3);
530 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
531 AtomicOrdering SuccessOrdering,
532 AtomicOrdering FailureOrdering,
533 SynchronizationScope SynchScope,
534 Instruction *InsertBefore = nullptr);
535 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
536 AtomicOrdering SuccessOrdering,
537 AtomicOrdering FailureOrdering,
538 SynchronizationScope SynchScope,
539 BasicBlock *InsertAtEnd);
541 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
544 bool isVolatile() const {
545 return getSubclassDataFromInstruction() & 1;
548 /// setVolatile - Specify whether this is a volatile cmpxchg.
550 void setVolatile(bool V) {
551 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
555 /// Return true if this cmpxchg may spuriously fail.
556 bool isWeak() const {
557 return getSubclassDataFromInstruction() & 0x100;
560 void setWeak(bool IsWeak) {
561 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
565 /// Transparently provide more efficient getOperand methods.
566 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
568 /// Set the ordering constraint on this cmpxchg.
569 void setSuccessOrdering(AtomicOrdering Ordering) {
570 assert(Ordering != NotAtomic &&
571 "CmpXchg instructions can only be atomic.");
572 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
576 void setFailureOrdering(AtomicOrdering Ordering) {
577 assert(Ordering != NotAtomic &&
578 "CmpXchg instructions can only be atomic.");
579 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
583 /// Specify whether this cmpxchg is atomic and orders other operations with
584 /// respect to all concurrently executing threads, or only with respect to
585 /// signal handlers executing in the same thread.
586 void setSynchScope(SynchronizationScope SynchScope) {
587 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
591 /// Returns the ordering constraint on this cmpxchg.
592 AtomicOrdering getSuccessOrdering() const {
593 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
596 /// Returns the ordering constraint on this cmpxchg.
597 AtomicOrdering getFailureOrdering() const {
598 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
601 /// Returns whether this cmpxchg is atomic between threads or only within a
603 SynchronizationScope getSynchScope() const {
604 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
607 Value *getPointerOperand() { return getOperand(0); }
608 const Value *getPointerOperand() const { return getOperand(0); }
609 static unsigned getPointerOperandIndex() { return 0U; }
611 Value *getCompareOperand() { return getOperand(1); }
612 const Value *getCompareOperand() const { return getOperand(1); }
614 Value *getNewValOperand() { return getOperand(2); }
615 const Value *getNewValOperand() const { return getOperand(2); }
617 /// \brief Returns the address space of the pointer operand.
618 unsigned getPointerAddressSpace() const {
619 return getPointerOperand()->getType()->getPointerAddressSpace();
622 /// \brief Returns the strongest permitted ordering on failure, given the
623 /// desired ordering on success.
625 /// If the comparison in a cmpxchg operation fails, there is no atomic store
626 /// so release semantics cannot be provided. So this function drops explicit
627 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
628 /// operation would remain SequentiallyConsistent.
629 static AtomicOrdering
630 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
631 switch (SuccessOrdering) {
632 default: llvm_unreachable("invalid cmpxchg success ordering");
639 case SequentiallyConsistent:
640 return SequentiallyConsistent;
644 // Methods for support type inquiry through isa, cast, and dyn_cast:
645 static inline bool classof(const Instruction *I) {
646 return I->getOpcode() == Instruction::AtomicCmpXchg;
648 static inline bool classof(const Value *V) {
649 return isa<Instruction>(V) && classof(cast<Instruction>(V));
652 // Shadow Instruction::setInstructionSubclassData with a private forwarding
653 // method so that subclasses cannot accidentally use it.
654 void setInstructionSubclassData(unsigned short D) {
655 Instruction::setInstructionSubclassData(D);
660 struct OperandTraits<AtomicCmpXchgInst> :
661 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
664 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
666 //===----------------------------------------------------------------------===//
667 // AtomicRMWInst Class
668 //===----------------------------------------------------------------------===//
670 /// AtomicRMWInst - an instruction that atomically reads a memory location,
671 /// combines it with another value, and then stores the result back. Returns
674 class AtomicRMWInst : public Instruction {
675 void *operator new(size_t, unsigned) = delete;
677 // Note: Instruction needs to be a friend here to call cloneImpl.
678 friend class Instruction;
679 AtomicRMWInst *cloneImpl() const;
682 /// This enumeration lists the possible modifications atomicrmw can make. In
683 /// the descriptions, 'p' is the pointer to the instruction's memory location,
684 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
685 /// instruction. These instructions always return 'old'.
701 /// *p = old >signed v ? old : v
703 /// *p = old <signed v ? old : v
705 /// *p = old >unsigned v ? old : v
707 /// *p = old <unsigned v ? old : v
715 // allocate space for exactly two operands
716 void *operator new(size_t s) {
717 return User::operator new(s, 2);
719 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
720 AtomicOrdering Ordering, SynchronizationScope SynchScope,
721 Instruction *InsertBefore = nullptr);
722 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
723 AtomicOrdering Ordering, SynchronizationScope SynchScope,
724 BasicBlock *InsertAtEnd);
726 BinOp getOperation() const {
727 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
730 void setOperation(BinOp Operation) {
731 unsigned short SubclassData = getSubclassDataFromInstruction();
732 setInstructionSubclassData((SubclassData & 31) |
736 /// isVolatile - Return true if this is a RMW on a volatile memory location.
738 bool isVolatile() const {
739 return getSubclassDataFromInstruction() & 1;
742 /// setVolatile - Specify whether this is a volatile RMW or not.
744 void setVolatile(bool V) {
745 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
749 /// Transparently provide more efficient getOperand methods.
750 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
752 /// Set the ordering constraint on this RMW.
753 void setOrdering(AtomicOrdering Ordering) {
754 assert(Ordering != NotAtomic &&
755 "atomicrmw instructions can only be atomic.");
756 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
760 /// Specify whether this RMW orders other operations with respect to all
761 /// concurrently executing threads, or only with respect to signal handlers
762 /// executing in the same thread.
763 void setSynchScope(SynchronizationScope SynchScope) {
764 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
768 /// Returns the ordering constraint on this RMW.
769 AtomicOrdering getOrdering() const {
770 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
773 /// Returns whether this RMW is atomic between threads or only within a
775 SynchronizationScope getSynchScope() const {
776 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
779 Value *getPointerOperand() { return getOperand(0); }
780 const Value *getPointerOperand() const { return getOperand(0); }
781 static unsigned getPointerOperandIndex() { return 0U; }
783 Value *getValOperand() { return getOperand(1); }
784 const Value *getValOperand() const { return getOperand(1); }
786 /// \brief Returns the address space of the pointer operand.
787 unsigned getPointerAddressSpace() const {
788 return getPointerOperand()->getType()->getPointerAddressSpace();
791 // Methods for support type inquiry through isa, cast, and dyn_cast:
792 static inline bool classof(const Instruction *I) {
793 return I->getOpcode() == Instruction::AtomicRMW;
795 static inline bool classof(const Value *V) {
796 return isa<Instruction>(V) && classof(cast<Instruction>(V));
799 void Init(BinOp Operation, Value *Ptr, Value *Val,
800 AtomicOrdering Ordering, SynchronizationScope SynchScope);
801 // Shadow Instruction::setInstructionSubclassData with a private forwarding
802 // method so that subclasses cannot accidentally use it.
803 void setInstructionSubclassData(unsigned short D) {
804 Instruction::setInstructionSubclassData(D);
809 struct OperandTraits<AtomicRMWInst>
810 : public FixedNumOperandTraits<AtomicRMWInst,2> {
813 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
815 //===----------------------------------------------------------------------===//
816 // GetElementPtrInst Class
817 //===----------------------------------------------------------------------===//
819 // checkGEPType - Simple wrapper function to give a better assertion failure
820 // message on bad indexes for a gep instruction.
822 inline Type *checkGEPType(Type *Ty) {
823 assert(Ty && "Invalid GetElementPtrInst indices for type!");
827 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
828 /// access elements of arrays and structs
830 class GetElementPtrInst : public Instruction {
831 Type *SourceElementType;
832 Type *ResultElementType;
834 GetElementPtrInst(const GetElementPtrInst &GEPI);
835 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
837 /// Constructors - Create a getelementptr instruction with a base pointer an
838 /// list of indices. The first ctor can optionally insert before an existing
839 /// instruction, the second appends the new instruction to the specified
841 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
842 ArrayRef<Value *> IdxList, unsigned Values,
843 const Twine &NameStr, Instruction *InsertBefore);
844 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
845 ArrayRef<Value *> IdxList, unsigned Values,
846 const Twine &NameStr, BasicBlock *InsertAtEnd);
849 // Note: Instruction needs to be a friend here to call cloneImpl.
850 friend class Instruction;
851 GetElementPtrInst *cloneImpl() const;
854 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
855 ArrayRef<Value *> IdxList,
856 const Twine &NameStr = "",
857 Instruction *InsertBefore = nullptr) {
858 unsigned Values = 1 + unsigned(IdxList.size());
861 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
865 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
866 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
867 NameStr, InsertBefore);
869 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
870 ArrayRef<Value *> IdxList,
871 const Twine &NameStr,
872 BasicBlock *InsertAtEnd) {
873 unsigned Values = 1 + unsigned(IdxList.size());
876 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
880 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
881 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
882 NameStr, InsertAtEnd);
885 /// Create an "inbounds" getelementptr. See the documentation for the
886 /// "inbounds" flag in LangRef.html for details.
887 static GetElementPtrInst *CreateInBounds(Value *Ptr,
888 ArrayRef<Value *> IdxList,
889 const Twine &NameStr = "",
890 Instruction *InsertBefore = nullptr){
891 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
893 static GetElementPtrInst *
894 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
895 const Twine &NameStr = "",
896 Instruction *InsertBefore = nullptr) {
897 GetElementPtrInst *GEP =
898 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
899 GEP->setIsInBounds(true);
902 static GetElementPtrInst *CreateInBounds(Value *Ptr,
903 ArrayRef<Value *> IdxList,
904 const Twine &NameStr,
905 BasicBlock *InsertAtEnd) {
906 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
908 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
909 ArrayRef<Value *> IdxList,
910 const Twine &NameStr,
911 BasicBlock *InsertAtEnd) {
912 GetElementPtrInst *GEP =
913 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
914 GEP->setIsInBounds(true);
918 /// Transparently provide more efficient getOperand methods.
919 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
921 // getType - Overload to return most specific sequential type.
922 SequentialType *getType() const {
923 return cast<SequentialType>(Instruction::getType());
926 Type *getSourceElementType() const { return SourceElementType; }
928 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
929 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
931 Type *getResultElementType() const {
932 assert(ResultElementType ==
933 cast<PointerType>(getType()->getScalarType())->getElementType());
934 return ResultElementType;
937 /// \brief Returns the address space of this instruction's pointer type.
938 unsigned getAddressSpace() const {
939 // Note that this is always the same as the pointer operand's address space
940 // and that is cheaper to compute, so cheat here.
941 return getPointerAddressSpace();
944 /// getIndexedType - Returns the type of the element that would be loaded with
945 /// a load instruction with the specified parameters.
947 /// Null is returned if the indices are invalid for the specified
950 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
951 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
952 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
954 inline op_iterator idx_begin() { return op_begin()+1; }
955 inline const_op_iterator idx_begin() const { return op_begin()+1; }
956 inline op_iterator idx_end() { return op_end(); }
957 inline const_op_iterator idx_end() const { return op_end(); }
959 Value *getPointerOperand() {
960 return getOperand(0);
962 const Value *getPointerOperand() const {
963 return getOperand(0);
965 static unsigned getPointerOperandIndex() {
966 return 0U; // get index for modifying correct operand.
969 /// getPointerOperandType - Method to return the pointer operand as a
971 Type *getPointerOperandType() const {
972 return getPointerOperand()->getType();
975 /// \brief Returns the address space of the pointer operand.
976 unsigned getPointerAddressSpace() const {
977 return getPointerOperandType()->getPointerAddressSpace();
980 /// GetGEPReturnType - Returns the pointer type returned by the GEP
981 /// instruction, which may be a vector of pointers.
982 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
983 return getGEPReturnType(
984 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
987 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
988 ArrayRef<Value *> IdxList) {
989 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
990 Ptr->getType()->getPointerAddressSpace());
992 if (Ptr->getType()->isVectorTy()) {
993 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
994 return VectorType::get(PtrTy, NumElem);
1001 unsigned getNumIndices() const { // Note: always non-negative
1002 return getNumOperands() - 1;
1005 bool hasIndices() const {
1006 return getNumOperands() > 1;
1009 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1010 /// zeros. If so, the result pointer and the first operand have the same
1011 /// value, just potentially different types.
1012 bool hasAllZeroIndices() const;
1014 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1015 /// constant integers. If so, the result pointer and the first operand have
1016 /// a constant offset between them.
1017 bool hasAllConstantIndices() const;
1019 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
1020 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1021 void setIsInBounds(bool b = true);
1023 /// isInBounds - Determine whether the GEP has the inbounds flag.
1024 bool isInBounds() const;
1026 /// \brief Accumulate the constant address offset of this GEP if possible.
1028 /// This routine accepts an APInt into which it will accumulate the constant
1029 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1030 /// all-constant, it returns false and the value of the offset APInt is
1031 /// undefined (it is *not* preserved!). The APInt passed into this routine
1032 /// must be at least as wide as the IntPtr type for the address space of
1033 /// the base GEP pointer.
1034 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1036 // Methods for support type inquiry through isa, cast, and dyn_cast:
1037 static inline bool classof(const Instruction *I) {
1038 return (I->getOpcode() == Instruction::GetElementPtr);
1040 static inline bool classof(const Value *V) {
1041 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1046 struct OperandTraits<GetElementPtrInst> :
1047 public VariadicOperandTraits<GetElementPtrInst, 1> {
1050 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1051 ArrayRef<Value *> IdxList, unsigned Values,
1052 const Twine &NameStr,
1053 Instruction *InsertBefore)
1054 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1055 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1056 Values, InsertBefore),
1057 SourceElementType(PointeeType),
1058 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1059 assert(ResultElementType ==
1060 cast<PointerType>(getType()->getScalarType())->getElementType());
1061 init(Ptr, IdxList, NameStr);
1063 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1064 ArrayRef<Value *> IdxList, unsigned Values,
1065 const Twine &NameStr,
1066 BasicBlock *InsertAtEnd)
1067 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1068 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1069 Values, InsertAtEnd),
1070 SourceElementType(PointeeType),
1071 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1072 assert(ResultElementType ==
1073 cast<PointerType>(getType()->getScalarType())->getElementType());
1074 init(Ptr, IdxList, NameStr);
1078 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1081 //===----------------------------------------------------------------------===//
1083 //===----------------------------------------------------------------------===//
1085 /// This instruction compares its operands according to the predicate given
1086 /// to the constructor. It only operates on integers or pointers. The operands
1087 /// must be identical types.
1088 /// \brief Represent an integer comparison operator.
1089 class ICmpInst: public CmpInst {
1091 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1092 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1093 "Invalid ICmp predicate value");
1094 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1095 "Both operands to ICmp instruction are not of the same type!");
1096 // Check that the operands are the right type
1097 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1098 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1099 "Invalid operand types for ICmp instruction");
1103 // Note: Instruction needs to be a friend here to call cloneImpl.
1104 friend class Instruction;
1105 /// \brief Clone an identical ICmpInst
1106 ICmpInst *cloneImpl() const;
1109 /// \brief Constructor with insert-before-instruction semantics.
1111 Instruction *InsertBefore, ///< Where to insert
1112 Predicate pred, ///< The predicate to use for the comparison
1113 Value *LHS, ///< The left-hand-side of the expression
1114 Value *RHS, ///< The right-hand-side of the expression
1115 const Twine &NameStr = "" ///< Name of the instruction
1116 ) : CmpInst(makeCmpResultType(LHS->getType()),
1117 Instruction::ICmp, pred, LHS, RHS, NameStr,
1124 /// \brief Constructor with insert-at-end semantics.
1126 BasicBlock &InsertAtEnd, ///< Block to insert into.
1127 Predicate pred, ///< The predicate to use for the comparison
1128 Value *LHS, ///< The left-hand-side of the expression
1129 Value *RHS, ///< The right-hand-side of the expression
1130 const Twine &NameStr = "" ///< Name of the instruction
1131 ) : CmpInst(makeCmpResultType(LHS->getType()),
1132 Instruction::ICmp, pred, LHS, RHS, NameStr,
1139 /// \brief Constructor with no-insertion semantics
1141 Predicate pred, ///< The predicate to use for the comparison
1142 Value *LHS, ///< The left-hand-side of the expression
1143 Value *RHS, ///< The right-hand-side of the expression
1144 const Twine &NameStr = "" ///< Name of the instruction
1145 ) : CmpInst(makeCmpResultType(LHS->getType()),
1146 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1152 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1153 /// @returns the predicate that would be the result if the operand were
1154 /// regarded as signed.
1155 /// \brief Return the signed version of the predicate
1156 Predicate getSignedPredicate() const {
1157 return getSignedPredicate(getPredicate());
1160 /// This is a static version that you can use without an instruction.
1161 /// \brief Return the signed version of the predicate.
1162 static Predicate getSignedPredicate(Predicate pred);
1164 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1165 /// @returns the predicate that would be the result if the operand were
1166 /// regarded as unsigned.
1167 /// \brief Return the unsigned version of the predicate
1168 Predicate getUnsignedPredicate() const {
1169 return getUnsignedPredicate(getPredicate());
1172 /// This is a static version that you can use without an instruction.
1173 /// \brief Return the unsigned version of the predicate.
1174 static Predicate getUnsignedPredicate(Predicate pred);
1176 /// isEquality - Return true if this predicate is either EQ or NE. This also
1177 /// tests for commutativity.
1178 static bool isEquality(Predicate P) {
1179 return P == ICMP_EQ || P == ICMP_NE;
1182 /// isEquality - Return true if this predicate is either EQ or NE. This also
1183 /// tests for commutativity.
1184 bool isEquality() const {
1185 return isEquality(getPredicate());
1188 /// @returns true if the predicate of this ICmpInst is commutative
1189 /// \brief Determine if this relation is commutative.
1190 bool isCommutative() const { return isEquality(); }
1192 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1194 bool isRelational() const {
1195 return !isEquality();
1198 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1200 static bool isRelational(Predicate P) {
1201 return !isEquality(P);
1204 /// Initialize a set of values that all satisfy the predicate with C.
1205 /// \brief Make a ConstantRange for a relation with a constant value.
1206 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1208 /// Exchange the two operands to this instruction in such a way that it does
1209 /// not modify the semantics of the instruction. The predicate value may be
1210 /// changed to retain the same result if the predicate is order dependent
1212 /// \brief Swap operands and adjust predicate.
1213 void swapOperands() {
1214 setPredicate(getSwappedPredicate());
1215 Op<0>().swap(Op<1>());
1218 // Methods for support type inquiry through isa, cast, and dyn_cast:
1219 static inline bool classof(const Instruction *I) {
1220 return I->getOpcode() == Instruction::ICmp;
1222 static inline bool classof(const Value *V) {
1223 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1228 //===----------------------------------------------------------------------===//
1230 //===----------------------------------------------------------------------===//
1232 /// This instruction compares its operands according to the predicate given
1233 /// to the constructor. It only operates on floating point values or packed
1234 /// vectors of floating point values. The operands must be identical types.
1235 /// \brief Represents a floating point comparison operator.
1236 class FCmpInst: public CmpInst {
1238 // Note: Instruction needs to be a friend here to call cloneImpl.
1239 friend class Instruction;
1240 /// \brief Clone an identical FCmpInst
1241 FCmpInst *cloneImpl() const;
1244 /// \brief Constructor with insert-before-instruction semantics.
1246 Instruction *InsertBefore, ///< Where to insert
1247 Predicate pred, ///< The predicate to use for the comparison
1248 Value *LHS, ///< The left-hand-side of the expression
1249 Value *RHS, ///< The right-hand-side of the expression
1250 const Twine &NameStr = "" ///< Name of the instruction
1251 ) : CmpInst(makeCmpResultType(LHS->getType()),
1252 Instruction::FCmp, pred, LHS, RHS, NameStr,
1254 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1255 "Invalid FCmp predicate value");
1256 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1257 "Both operands to FCmp instruction are not of the same type!");
1258 // Check that the operands are the right type
1259 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1260 "Invalid operand types for FCmp instruction");
1263 /// \brief Constructor with insert-at-end semantics.
1265 BasicBlock &InsertAtEnd, ///< Block to insert into.
1266 Predicate pred, ///< The predicate to use for the comparison
1267 Value *LHS, ///< The left-hand-side of the expression
1268 Value *RHS, ///< The right-hand-side of the expression
1269 const Twine &NameStr = "" ///< Name of the instruction
1270 ) : CmpInst(makeCmpResultType(LHS->getType()),
1271 Instruction::FCmp, pred, LHS, RHS, NameStr,
1273 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1274 "Invalid FCmp predicate value");
1275 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1276 "Both operands to FCmp instruction are not of the same type!");
1277 // Check that the operands are the right type
1278 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1279 "Invalid operand types for FCmp instruction");
1282 /// \brief Constructor with no-insertion semantics
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 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1291 "Invalid FCmp predicate value");
1292 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1293 "Both operands to FCmp instruction are not of the same type!");
1294 // Check that the operands are the right type
1295 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1296 "Invalid operand types for FCmp instruction");
1299 /// @returns true if the predicate of this instruction is EQ or NE.
1300 /// \brief Determine if this is an equality predicate.
1301 static bool isEquality(Predicate Pred) {
1302 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1306 /// @returns true if the predicate of this instruction is EQ or NE.
1307 /// \brief Determine if this is an equality predicate.
1308 bool isEquality() const { return isEquality(getPredicate()); }
1310 /// @returns true if the predicate of this instruction is commutative.
1311 /// \brief Determine if this is a commutative predicate.
1312 bool isCommutative() const {
1313 return isEquality() ||
1314 getPredicate() == FCMP_FALSE ||
1315 getPredicate() == FCMP_TRUE ||
1316 getPredicate() == FCMP_ORD ||
1317 getPredicate() == FCMP_UNO;
1320 /// @returns true if the predicate is relational (not EQ or NE).
1321 /// \brief Determine if this a relational predicate.
1322 bool isRelational() const { return !isEquality(); }
1324 /// Exchange the two operands to this instruction in such a way that it does
1325 /// not modify the semantics of the instruction. The predicate value may be
1326 /// changed to retain the same result if the predicate is order dependent
1328 /// \brief Swap operands and adjust predicate.
1329 void swapOperands() {
1330 setPredicate(getSwappedPredicate());
1331 Op<0>().swap(Op<1>());
1334 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1335 static inline bool classof(const Instruction *I) {
1336 return I->getOpcode() == Instruction::FCmp;
1338 static inline bool classof(const Value *V) {
1339 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1343 //===----------------------------------------------------------------------===//
1344 /// CallInst - This class represents a function call, abstracting a target
1345 /// machine's calling convention. This class uses low bit of the SubClassData
1346 /// field to indicate whether or not this is a tail call. The rest of the bits
1347 /// hold the calling convention of the call.
1349 class CallInst : public Instruction {
1350 AttributeSet AttributeList; ///< parameter attributes for call
1352 CallInst(const CallInst &CI);
1353 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr) {
1354 init(cast<FunctionType>(
1355 cast<PointerType>(Func->getType())->getElementType()),
1356 Func, Args, NameStr);
1358 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1359 const Twine &NameStr);
1360 void init(Value *Func, const Twine &NameStr);
1362 /// Construct a CallInst given a range of arguments.
1363 /// \brief Construct a CallInst from a range of arguments
1364 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1365 const Twine &NameStr, Instruction *InsertBefore);
1366 inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr,
1367 Instruction *InsertBefore)
1368 : CallInst(cast<FunctionType>(
1369 cast<PointerType>(Func->getType())->getElementType()),
1370 Func, Args, NameStr, InsertBefore) {}
1372 /// Construct a CallInst given a range of arguments.
1373 /// \brief Construct a CallInst from a range of arguments
1374 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1375 const Twine &NameStr, BasicBlock *InsertAtEnd);
1377 explicit CallInst(Value *F, const Twine &NameStr,
1378 Instruction *InsertBefore);
1379 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1381 // Note: Instruction needs to be a friend here to call cloneImpl.
1382 friend class Instruction;
1383 CallInst *cloneImpl() const;
1386 static CallInst *Create(Value *Func,
1387 ArrayRef<Value *> Args,
1388 const Twine &NameStr = "",
1389 Instruction *InsertBefore = nullptr) {
1390 return Create(cast<FunctionType>(
1391 cast<PointerType>(Func->getType())->getElementType()),
1392 Func, Args, NameStr, InsertBefore);
1394 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1395 const Twine &NameStr = "",
1396 Instruction *InsertBefore = nullptr) {
1397 return new (unsigned(Args.size() + 1))
1398 CallInst(Ty, Func, Args, NameStr, InsertBefore);
1400 static CallInst *Create(Value *Func,
1401 ArrayRef<Value *> Args,
1402 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1403 return new(unsigned(Args.size() + 1))
1404 CallInst(Func, Args, NameStr, InsertAtEnd);
1406 static CallInst *Create(Value *F, const Twine &NameStr = "",
1407 Instruction *InsertBefore = nullptr) {
1408 return new(1) CallInst(F, NameStr, InsertBefore);
1410 static CallInst *Create(Value *F, const Twine &NameStr,
1411 BasicBlock *InsertAtEnd) {
1412 return new(1) CallInst(F, NameStr, InsertAtEnd);
1414 /// CreateMalloc - Generate the IR for a call to malloc:
1415 /// 1. Compute the malloc call's argument as the specified type's size,
1416 /// possibly multiplied by the array size if the array size is not
1418 /// 2. Call malloc with that argument.
1419 /// 3. Bitcast the result of the malloc call to the specified type.
1420 static Instruction *CreateMalloc(Instruction *InsertBefore,
1421 Type *IntPtrTy, Type *AllocTy,
1422 Value *AllocSize, Value *ArraySize = nullptr,
1423 Function* MallocF = nullptr,
1424 const Twine &Name = "");
1425 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1426 Type *IntPtrTy, Type *AllocTy,
1427 Value *AllocSize, Value *ArraySize = nullptr,
1428 Function* MallocF = nullptr,
1429 const Twine &Name = "");
1430 /// CreateFree - Generate the IR for a call to the builtin free function.
1431 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1432 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1434 ~CallInst() override;
1436 FunctionType *getFunctionType() const { return FTy; }
1438 void mutateFunctionType(FunctionType *FTy) {
1439 mutateType(FTy->getReturnType());
1443 // Note that 'musttail' implies 'tail'.
1444 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2 };
1445 TailCallKind getTailCallKind() const {
1446 return TailCallKind(getSubclassDataFromInstruction() & 3);
1448 bool isTailCall() const {
1449 return (getSubclassDataFromInstruction() & 3) != TCK_None;
1451 bool isMustTailCall() const {
1452 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1454 void setTailCall(bool isTC = true) {
1455 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1456 unsigned(isTC ? TCK_Tail : TCK_None));
1458 void setTailCallKind(TailCallKind TCK) {
1459 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1463 /// Provide fast operand accessors
1464 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1466 /// getNumArgOperands - Return the number of call arguments.
1468 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1470 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1472 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1473 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1475 /// arg_operands - iteration adapter for range-for loops.
1476 iterator_range<op_iterator> arg_operands() {
1477 // The last operand in the op list is the callee - it's not one of the args
1478 // so we don't want to iterate over it.
1479 return iterator_range<op_iterator>(op_begin(), op_end() - 1);
1482 /// arg_operands - iteration adapter for range-for loops.
1483 iterator_range<const_op_iterator> arg_operands() const {
1484 return iterator_range<const_op_iterator>(op_begin(), op_end() - 1);
1487 /// \brief Wrappers for getting the \c Use of a call argument.
1488 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1489 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1491 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1493 CallingConv::ID getCallingConv() const {
1494 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1496 void setCallingConv(CallingConv::ID CC) {
1497 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1498 (static_cast<unsigned>(CC) << 2));
1501 /// getAttributes - Return the parameter attributes for this call.
1503 const AttributeSet &getAttributes() const { return AttributeList; }
1505 /// setAttributes - Set the parameter attributes for this call.
1507 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1509 /// addAttribute - adds the attribute to the list of attributes.
1510 void addAttribute(unsigned i, Attribute::AttrKind attr);
1512 /// addAttribute - adds the attribute to the list of attributes.
1513 void addAttribute(unsigned i, StringRef Kind, StringRef Value);
1515 /// removeAttribute - removes the attribute from the list of attributes.
1516 void removeAttribute(unsigned i, Attribute attr);
1518 /// \brief adds the dereferenceable attribute to the list of attributes.
1519 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1521 /// \brief adds the dereferenceable_or_null attribute to the list of
1523 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
1525 /// \brief Determine whether this call has the given attribute.
1526 bool hasFnAttr(Attribute::AttrKind A) const {
1527 assert(A != Attribute::NoBuiltin &&
1528 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1529 return hasFnAttrImpl(A);
1532 /// \brief Determine whether this call has the given attribute.
1533 bool hasFnAttr(StringRef A) const {
1534 return hasFnAttrImpl(A);
1537 /// \brief Determine whether the call or the callee has the given attributes.
1538 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1540 /// \brief Extract the alignment for a call or parameter (0=unknown).
1541 unsigned getParamAlignment(unsigned i) const {
1542 return AttributeList.getParamAlignment(i);
1545 /// \brief Extract the number of dereferenceable bytes for a call or
1546 /// parameter (0=unknown).
1547 uint64_t getDereferenceableBytes(unsigned i) const {
1548 return AttributeList.getDereferenceableBytes(i);
1551 /// \brief Extract the number of dereferenceable_or_null bytes for a call or
1552 /// parameter (0=unknown).
1553 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1554 return AttributeList.getDereferenceableOrNullBytes(i);
1557 /// \brief Return true if the call should not be treated as a call to a
1559 bool isNoBuiltin() const {
1560 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1561 !hasFnAttrImpl(Attribute::Builtin);
1564 /// \brief Return true if the call should not be inlined.
1565 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1566 void setIsNoInline() {
1567 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1570 /// \brief Return true if the call can return twice
1571 bool canReturnTwice() const {
1572 return hasFnAttr(Attribute::ReturnsTwice);
1574 void setCanReturnTwice() {
1575 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1578 /// \brief Determine if the call does not access memory.
1579 bool doesNotAccessMemory() const {
1580 return hasFnAttr(Attribute::ReadNone);
1582 void setDoesNotAccessMemory() {
1583 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1586 /// \brief Determine if the call does not access or only reads memory.
1587 bool onlyReadsMemory() const {
1588 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1590 void setOnlyReadsMemory() {
1591 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1594 /// \brief Determine if the call cannot return.
1595 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1596 void setDoesNotReturn() {
1597 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1600 /// \brief Determine if the call cannot unwind.
1601 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1602 void setDoesNotThrow() {
1603 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1606 /// \brief Determine if the call cannot be duplicated.
1607 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1608 void setCannotDuplicate() {
1609 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1612 /// \brief Determine if the call returns a structure through first
1613 /// pointer argument.
1614 bool hasStructRetAttr() const {
1615 // Be friendly and also check the callee.
1616 return paramHasAttr(1, Attribute::StructRet);
1619 /// \brief Determine if any call argument is an aggregate passed by value.
1620 bool hasByValArgument() const {
1621 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1624 /// getCalledFunction - Return the function called, or null if this is an
1625 /// indirect function invocation.
1627 Function *getCalledFunction() const {
1628 return dyn_cast<Function>(Op<-1>());
1631 /// getCalledValue - Get a pointer to the function that is invoked by this
1633 const Value *getCalledValue() const { return Op<-1>(); }
1634 Value *getCalledValue() { return Op<-1>(); }
1636 /// setCalledFunction - Set the function called.
1637 void setCalledFunction(Value* Fn) {
1639 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
1642 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1644 assert(FTy == cast<FunctionType>(
1645 cast<PointerType>(Fn->getType())->getElementType()));
1649 /// isInlineAsm - Check if this call is an inline asm statement.
1650 bool isInlineAsm() const {
1651 return isa<InlineAsm>(Op<-1>());
1654 // Methods for support type inquiry through isa, cast, and dyn_cast:
1655 static inline bool classof(const Instruction *I) {
1656 return I->getOpcode() == Instruction::Call;
1658 static inline bool classof(const Value *V) {
1659 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1663 template<typename AttrKind>
1664 bool hasFnAttrImpl(AttrKind A) const {
1665 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
1667 if (const Function *F = getCalledFunction())
1668 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
1672 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1673 // method so that subclasses cannot accidentally use it.
1674 void setInstructionSubclassData(unsigned short D) {
1675 Instruction::setInstructionSubclassData(D);
1680 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1683 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1684 const Twine &NameStr, BasicBlock *InsertAtEnd)
1685 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1686 ->getElementType())->getReturnType(),
1688 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1689 unsigned(Args.size() + 1), InsertAtEnd) {
1690 init(Func, Args, NameStr);
1693 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1694 const Twine &NameStr, Instruction *InsertBefore)
1695 : Instruction(Ty->getReturnType(), Instruction::Call,
1696 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1697 unsigned(Args.size() + 1), InsertBefore) {
1698 init(Ty, Func, Args, NameStr);
1702 // Note: if you get compile errors about private methods then
1703 // please update your code to use the high-level operand
1704 // interfaces. See line 943 above.
1705 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1707 //===----------------------------------------------------------------------===//
1709 //===----------------------------------------------------------------------===//
1711 /// SelectInst - This class represents the LLVM 'select' instruction.
1713 class SelectInst : public Instruction {
1714 void init(Value *C, Value *S1, Value *S2) {
1715 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1721 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1722 Instruction *InsertBefore)
1723 : Instruction(S1->getType(), Instruction::Select,
1724 &Op<0>(), 3, InsertBefore) {
1728 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1729 BasicBlock *InsertAtEnd)
1730 : Instruction(S1->getType(), Instruction::Select,
1731 &Op<0>(), 3, InsertAtEnd) {
1736 // Note: Instruction needs to be a friend here to call cloneImpl.
1737 friend class Instruction;
1738 SelectInst *cloneImpl() const;
1741 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1742 const Twine &NameStr = "",
1743 Instruction *InsertBefore = nullptr) {
1744 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1746 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1747 const Twine &NameStr,
1748 BasicBlock *InsertAtEnd) {
1749 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1752 const Value *getCondition() const { return Op<0>(); }
1753 const Value *getTrueValue() const { return Op<1>(); }
1754 const Value *getFalseValue() const { return Op<2>(); }
1755 Value *getCondition() { return Op<0>(); }
1756 Value *getTrueValue() { return Op<1>(); }
1757 Value *getFalseValue() { return Op<2>(); }
1759 /// areInvalidOperands - Return a string if the specified operands are invalid
1760 /// for a select operation, otherwise return null.
1761 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1763 /// Transparently provide more efficient getOperand methods.
1764 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1766 OtherOps getOpcode() const {
1767 return static_cast<OtherOps>(Instruction::getOpcode());
1770 // Methods for support type inquiry through isa, cast, and dyn_cast:
1771 static inline bool classof(const Instruction *I) {
1772 return I->getOpcode() == Instruction::Select;
1774 static inline bool classof(const Value *V) {
1775 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1780 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1783 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1785 //===----------------------------------------------------------------------===//
1787 //===----------------------------------------------------------------------===//
1789 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1790 /// an argument of the specified type given a va_list and increments that list
1792 class VAArgInst : public UnaryInstruction {
1794 // Note: Instruction needs to be a friend here to call cloneImpl.
1795 friend class Instruction;
1796 VAArgInst *cloneImpl() const;
1799 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1800 Instruction *InsertBefore = nullptr)
1801 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1804 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1805 BasicBlock *InsertAtEnd)
1806 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1810 Value *getPointerOperand() { return getOperand(0); }
1811 const Value *getPointerOperand() const { return getOperand(0); }
1812 static unsigned getPointerOperandIndex() { return 0U; }
1814 // Methods for support type inquiry through isa, cast, and dyn_cast:
1815 static inline bool classof(const Instruction *I) {
1816 return I->getOpcode() == VAArg;
1818 static inline bool classof(const Value *V) {
1819 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1823 //===----------------------------------------------------------------------===//
1824 // ExtractElementInst Class
1825 //===----------------------------------------------------------------------===//
1827 /// ExtractElementInst - This instruction extracts a single (scalar)
1828 /// element from a VectorType value
1830 class ExtractElementInst : public Instruction {
1831 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1832 Instruction *InsertBefore = nullptr);
1833 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1834 BasicBlock *InsertAtEnd);
1836 // Note: Instruction needs to be a friend here to call cloneImpl.
1837 friend class Instruction;
1838 ExtractElementInst *cloneImpl() const;
1841 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1842 const Twine &NameStr = "",
1843 Instruction *InsertBefore = nullptr) {
1844 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1846 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1847 const Twine &NameStr,
1848 BasicBlock *InsertAtEnd) {
1849 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1852 /// isValidOperands - Return true if an extractelement instruction can be
1853 /// formed with the specified operands.
1854 static bool isValidOperands(const Value *Vec, const Value *Idx);
1856 Value *getVectorOperand() { return Op<0>(); }
1857 Value *getIndexOperand() { return Op<1>(); }
1858 const Value *getVectorOperand() const { return Op<0>(); }
1859 const Value *getIndexOperand() const { return Op<1>(); }
1861 VectorType *getVectorOperandType() const {
1862 return cast<VectorType>(getVectorOperand()->getType());
1866 /// Transparently provide more efficient getOperand methods.
1867 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1869 // Methods for support type inquiry through isa, cast, and dyn_cast:
1870 static inline bool classof(const Instruction *I) {
1871 return I->getOpcode() == Instruction::ExtractElement;
1873 static inline bool classof(const Value *V) {
1874 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1879 struct OperandTraits<ExtractElementInst> :
1880 public FixedNumOperandTraits<ExtractElementInst, 2> {
1883 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1885 //===----------------------------------------------------------------------===//
1886 // InsertElementInst Class
1887 //===----------------------------------------------------------------------===//
1889 /// InsertElementInst - This instruction inserts a single (scalar)
1890 /// element into a VectorType value
1892 class InsertElementInst : public Instruction {
1893 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1894 const Twine &NameStr = "",
1895 Instruction *InsertBefore = nullptr);
1896 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1897 const Twine &NameStr, BasicBlock *InsertAtEnd);
1899 // Note: Instruction needs to be a friend here to call cloneImpl.
1900 friend class Instruction;
1901 InsertElementInst *cloneImpl() const;
1904 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1905 const Twine &NameStr = "",
1906 Instruction *InsertBefore = nullptr) {
1907 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1909 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1910 const Twine &NameStr,
1911 BasicBlock *InsertAtEnd) {
1912 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1915 /// isValidOperands - Return true if an insertelement instruction can be
1916 /// formed with the specified operands.
1917 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1920 /// getType - Overload to return most specific vector type.
1922 VectorType *getType() const {
1923 return cast<VectorType>(Instruction::getType());
1926 /// Transparently provide more efficient getOperand methods.
1927 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1929 // Methods for support type inquiry through isa, cast, and dyn_cast:
1930 static inline bool classof(const Instruction *I) {
1931 return I->getOpcode() == Instruction::InsertElement;
1933 static inline bool classof(const Value *V) {
1934 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1939 struct OperandTraits<InsertElementInst> :
1940 public FixedNumOperandTraits<InsertElementInst, 3> {
1943 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1945 //===----------------------------------------------------------------------===//
1946 // ShuffleVectorInst Class
1947 //===----------------------------------------------------------------------===//
1949 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1952 class ShuffleVectorInst : public Instruction {
1954 // Note: Instruction needs to be a friend here to call cloneImpl.
1955 friend class Instruction;
1956 ShuffleVectorInst *cloneImpl() const;
1959 // allocate space for exactly three operands
1960 void *operator new(size_t s) {
1961 return User::operator new(s, 3);
1963 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1964 const Twine &NameStr = "",
1965 Instruction *InsertBefor = nullptr);
1966 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1967 const Twine &NameStr, BasicBlock *InsertAtEnd);
1969 /// isValidOperands - Return true if a shufflevector instruction can be
1970 /// formed with the specified operands.
1971 static bool isValidOperands(const Value *V1, const Value *V2,
1974 /// getType - Overload to return most specific vector type.
1976 VectorType *getType() const {
1977 return cast<VectorType>(Instruction::getType());
1980 /// Transparently provide more efficient getOperand methods.
1981 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1983 Constant *getMask() const {
1984 return cast<Constant>(getOperand(2));
1987 /// getMaskValue - Return the index from the shuffle mask for the specified
1988 /// output result. This is either -1 if the element is undef or a number less
1989 /// than 2*numelements.
1990 static int getMaskValue(Constant *Mask, unsigned i);
1992 int getMaskValue(unsigned i) const {
1993 return getMaskValue(getMask(), i);
1996 /// getShuffleMask - Return the full mask for this instruction, where each
1997 /// element is the element number and undef's are returned as -1.
1998 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
2000 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2001 return getShuffleMask(getMask(), Result);
2004 SmallVector<int, 16> getShuffleMask() const {
2005 SmallVector<int, 16> Mask;
2006 getShuffleMask(Mask);
2011 // Methods for support type inquiry through isa, cast, and dyn_cast:
2012 static inline bool classof(const Instruction *I) {
2013 return I->getOpcode() == Instruction::ShuffleVector;
2015 static inline bool classof(const Value *V) {
2016 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2021 struct OperandTraits<ShuffleVectorInst> :
2022 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2025 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
2027 //===----------------------------------------------------------------------===//
2028 // ExtractValueInst Class
2029 //===----------------------------------------------------------------------===//
2031 /// ExtractValueInst - This instruction extracts a struct member or array
2032 /// element value from an aggregate value.
2034 class ExtractValueInst : public UnaryInstruction {
2035 SmallVector<unsigned, 4> Indices;
2037 ExtractValueInst(const ExtractValueInst &EVI);
2038 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2040 /// Constructors - Create a extractvalue instruction with a base aggregate
2041 /// value and a list of indices. The first ctor can optionally insert before
2042 /// an existing instruction, the second appends the new instruction to the
2043 /// specified BasicBlock.
2044 inline ExtractValueInst(Value *Agg,
2045 ArrayRef<unsigned> Idxs,
2046 const Twine &NameStr,
2047 Instruction *InsertBefore);
2048 inline ExtractValueInst(Value *Agg,
2049 ArrayRef<unsigned> Idxs,
2050 const Twine &NameStr, BasicBlock *InsertAtEnd);
2052 // allocate space for exactly one operand
2053 void *operator new(size_t s) {
2054 return User::operator new(s, 1);
2057 // Note: Instruction needs to be a friend here to call cloneImpl.
2058 friend class Instruction;
2059 ExtractValueInst *cloneImpl() const;
2062 static ExtractValueInst *Create(Value *Agg,
2063 ArrayRef<unsigned> Idxs,
2064 const Twine &NameStr = "",
2065 Instruction *InsertBefore = nullptr) {
2067 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2069 static ExtractValueInst *Create(Value *Agg,
2070 ArrayRef<unsigned> Idxs,
2071 const Twine &NameStr,
2072 BasicBlock *InsertAtEnd) {
2073 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2076 /// getIndexedType - Returns the type of the element that would be extracted
2077 /// with an extractvalue instruction with the specified parameters.
2079 /// Null is returned if the indices are invalid for the specified type.
2080 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2082 typedef const unsigned* idx_iterator;
2083 inline idx_iterator idx_begin() const { return Indices.begin(); }
2084 inline idx_iterator idx_end() const { return Indices.end(); }
2085 inline iterator_range<idx_iterator> indices() const {
2086 return iterator_range<idx_iterator>(idx_begin(), idx_end());
2089 Value *getAggregateOperand() {
2090 return getOperand(0);
2092 const Value *getAggregateOperand() const {
2093 return getOperand(0);
2095 static unsigned getAggregateOperandIndex() {
2096 return 0U; // get index for modifying correct operand
2099 ArrayRef<unsigned> getIndices() const {
2103 unsigned getNumIndices() const {
2104 return (unsigned)Indices.size();
2107 bool hasIndices() const {
2111 // Methods for support type inquiry through isa, cast, and dyn_cast:
2112 static inline bool classof(const Instruction *I) {
2113 return I->getOpcode() == Instruction::ExtractValue;
2115 static inline bool classof(const Value *V) {
2116 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2120 ExtractValueInst::ExtractValueInst(Value *Agg,
2121 ArrayRef<unsigned> Idxs,
2122 const Twine &NameStr,
2123 Instruction *InsertBefore)
2124 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2125 ExtractValue, Agg, InsertBefore) {
2126 init(Idxs, NameStr);
2128 ExtractValueInst::ExtractValueInst(Value *Agg,
2129 ArrayRef<unsigned> Idxs,
2130 const Twine &NameStr,
2131 BasicBlock *InsertAtEnd)
2132 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2133 ExtractValue, Agg, InsertAtEnd) {
2134 init(Idxs, NameStr);
2138 //===----------------------------------------------------------------------===//
2139 // InsertValueInst Class
2140 //===----------------------------------------------------------------------===//
2142 /// InsertValueInst - This instruction inserts a struct field of array element
2143 /// value into an aggregate value.
2145 class InsertValueInst : public Instruction {
2146 SmallVector<unsigned, 4> Indices;
2148 void *operator new(size_t, unsigned) = delete;
2149 InsertValueInst(const InsertValueInst &IVI);
2150 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2151 const Twine &NameStr);
2153 /// Constructors - Create a insertvalue instruction with a base aggregate
2154 /// value, a value to insert, and a list of indices. The first ctor can
2155 /// optionally insert before an existing instruction, the second appends
2156 /// the new instruction to the specified BasicBlock.
2157 inline InsertValueInst(Value *Agg, Value *Val,
2158 ArrayRef<unsigned> Idxs,
2159 const Twine &NameStr,
2160 Instruction *InsertBefore);
2161 inline InsertValueInst(Value *Agg, Value *Val,
2162 ArrayRef<unsigned> Idxs,
2163 const Twine &NameStr, BasicBlock *InsertAtEnd);
2165 /// Constructors - These two constructors are convenience methods because one
2166 /// and two index insertvalue instructions are so common.
2167 InsertValueInst(Value *Agg, Value *Val,
2168 unsigned Idx, const Twine &NameStr = "",
2169 Instruction *InsertBefore = nullptr);
2170 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2171 const Twine &NameStr, BasicBlock *InsertAtEnd);
2173 // Note: Instruction needs to be a friend here to call cloneImpl.
2174 friend class Instruction;
2175 InsertValueInst *cloneImpl() const;
2178 // allocate space for exactly two operands
2179 void *operator new(size_t s) {
2180 return User::operator new(s, 2);
2183 static InsertValueInst *Create(Value *Agg, Value *Val,
2184 ArrayRef<unsigned> Idxs,
2185 const Twine &NameStr = "",
2186 Instruction *InsertBefore = nullptr) {
2187 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2189 static InsertValueInst *Create(Value *Agg, Value *Val,
2190 ArrayRef<unsigned> Idxs,
2191 const Twine &NameStr,
2192 BasicBlock *InsertAtEnd) {
2193 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2196 /// Transparently provide more efficient getOperand methods.
2197 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2199 typedef const unsigned* idx_iterator;
2200 inline idx_iterator idx_begin() const { return Indices.begin(); }
2201 inline idx_iterator idx_end() const { return Indices.end(); }
2202 inline iterator_range<idx_iterator> indices() const {
2203 return iterator_range<idx_iterator>(idx_begin(), idx_end());
2206 Value *getAggregateOperand() {
2207 return getOperand(0);
2209 const Value *getAggregateOperand() const {
2210 return getOperand(0);
2212 static unsigned getAggregateOperandIndex() {
2213 return 0U; // get index for modifying correct operand
2216 Value *getInsertedValueOperand() {
2217 return getOperand(1);
2219 const Value *getInsertedValueOperand() const {
2220 return getOperand(1);
2222 static unsigned getInsertedValueOperandIndex() {
2223 return 1U; // get index for modifying correct operand
2226 ArrayRef<unsigned> getIndices() const {
2230 unsigned getNumIndices() const {
2231 return (unsigned)Indices.size();
2234 bool hasIndices() const {
2238 // Methods for support type inquiry through isa, cast, and dyn_cast:
2239 static inline bool classof(const Instruction *I) {
2240 return I->getOpcode() == Instruction::InsertValue;
2242 static inline bool classof(const Value *V) {
2243 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2248 struct OperandTraits<InsertValueInst> :
2249 public FixedNumOperandTraits<InsertValueInst, 2> {
2252 InsertValueInst::InsertValueInst(Value *Agg,
2254 ArrayRef<unsigned> Idxs,
2255 const Twine &NameStr,
2256 Instruction *InsertBefore)
2257 : Instruction(Agg->getType(), InsertValue,
2258 OperandTraits<InsertValueInst>::op_begin(this),
2260 init(Agg, Val, Idxs, NameStr);
2262 InsertValueInst::InsertValueInst(Value *Agg,
2264 ArrayRef<unsigned> Idxs,
2265 const Twine &NameStr,
2266 BasicBlock *InsertAtEnd)
2267 : Instruction(Agg->getType(), InsertValue,
2268 OperandTraits<InsertValueInst>::op_begin(this),
2270 init(Agg, Val, Idxs, NameStr);
2273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2275 //===----------------------------------------------------------------------===//
2277 //===----------------------------------------------------------------------===//
2279 // PHINode - The PHINode class is used to represent the magical mystical PHI
2280 // node, that can not exist in nature, but can be synthesized in a computer
2281 // scientist's overactive imagination.
2283 class PHINode : public Instruction {
2284 void *operator new(size_t, unsigned) = delete;
2285 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2286 /// the number actually in use.
2287 unsigned ReservedSpace;
2288 PHINode(const PHINode &PN);
2289 // allocate space for exactly zero operands
2290 void *operator new(size_t s) {
2291 return User::operator new(s);
2293 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2294 const Twine &NameStr = "",
2295 Instruction *InsertBefore = nullptr)
2296 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2297 ReservedSpace(NumReservedValues) {
2299 allocHungoffUses(ReservedSpace);
2302 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2303 BasicBlock *InsertAtEnd)
2304 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2305 ReservedSpace(NumReservedValues) {
2307 allocHungoffUses(ReservedSpace);
2310 // allocHungoffUses - this is more complicated than the generic
2311 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2312 // values and pointers to the incoming blocks, all in one allocation.
2313 void allocHungoffUses(unsigned N) {
2314 User::allocHungoffUses(N, /* IsPhi */ true);
2317 // Note: Instruction needs to be a friend here to call cloneImpl.
2318 friend class Instruction;
2319 PHINode *cloneImpl() const;
2322 /// Constructors - NumReservedValues is a hint for the number of incoming
2323 /// edges that this phi node will have (use 0 if you really have no idea).
2324 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2325 const Twine &NameStr = "",
2326 Instruction *InsertBefore = nullptr) {
2327 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2329 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2330 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2331 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2334 /// Provide fast operand accessors
2335 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2337 // Block iterator interface. This provides access to the list of incoming
2338 // basic blocks, which parallels the list of incoming values.
2340 typedef BasicBlock **block_iterator;
2341 typedef BasicBlock * const *const_block_iterator;
2343 block_iterator block_begin() {
2345 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2346 return reinterpret_cast<block_iterator>(ref + 1);
2349 const_block_iterator block_begin() const {
2350 const Use::UserRef *ref =
2351 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2352 return reinterpret_cast<const_block_iterator>(ref + 1);
2355 block_iterator block_end() {
2356 return block_begin() + getNumOperands();
2359 const_block_iterator block_end() const {
2360 return block_begin() + getNumOperands();
2363 op_range incoming_values() { return operands(); }
2365 const_op_range incoming_values() const { return operands(); }
2367 /// getNumIncomingValues - Return the number of incoming edges
2369 unsigned getNumIncomingValues() const { return getNumOperands(); }
2371 /// getIncomingValue - Return incoming value number x
2373 Value *getIncomingValue(unsigned i) const {
2374 return getOperand(i);
2376 void setIncomingValue(unsigned i, Value *V) {
2379 static unsigned getOperandNumForIncomingValue(unsigned i) {
2382 static unsigned getIncomingValueNumForOperand(unsigned i) {
2386 /// getIncomingBlock - Return incoming basic block number @p i.
2388 BasicBlock *getIncomingBlock(unsigned i) const {
2389 return block_begin()[i];
2392 /// getIncomingBlock - Return incoming basic block corresponding
2393 /// to an operand of the PHI.
2395 BasicBlock *getIncomingBlock(const Use &U) const {
2396 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2397 return getIncomingBlock(unsigned(&U - op_begin()));
2400 /// getIncomingBlock - Return incoming basic block corresponding
2401 /// to value use iterator.
2403 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2404 return getIncomingBlock(I.getUse());
2407 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2408 block_begin()[i] = BB;
2411 /// addIncoming - Add an incoming value to the end of the PHI list
2413 void addIncoming(Value *V, BasicBlock *BB) {
2414 assert(V && "PHI node got a null value!");
2415 assert(BB && "PHI node got a null basic block!");
2416 assert(getType() == V->getType() &&
2417 "All operands to PHI node must be the same type as the PHI node!");
2418 if (getNumOperands() == ReservedSpace)
2419 growOperands(); // Get more space!
2420 // Initialize some new operands.
2421 setNumHungOffUseOperands(getNumOperands() + 1);
2422 setIncomingValue(getNumOperands() - 1, V);
2423 setIncomingBlock(getNumOperands() - 1, BB);
2426 /// removeIncomingValue - Remove an incoming value. This is useful if a
2427 /// predecessor basic block is deleted. The value removed is returned.
2429 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2430 /// is true), the PHI node is destroyed and any uses of it are replaced with
2431 /// dummy values. The only time there should be zero incoming values to a PHI
2432 /// node is when the block is dead, so this strategy is sound.
2434 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2436 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2437 int Idx = getBasicBlockIndex(BB);
2438 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2439 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2442 /// getBasicBlockIndex - Return the first index of the specified basic
2443 /// block in the value list for this PHI. Returns -1 if no instance.
2445 int getBasicBlockIndex(const BasicBlock *BB) const {
2446 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2447 if (block_begin()[i] == BB)
2452 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2453 int Idx = getBasicBlockIndex(BB);
2454 assert(Idx >= 0 && "Invalid basic block argument!");
2455 return getIncomingValue(Idx);
2458 /// hasConstantValue - If the specified PHI node always merges together the
2459 /// same value, return the value, otherwise return null.
2460 Value *hasConstantValue() const;
2462 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2463 static inline bool classof(const Instruction *I) {
2464 return I->getOpcode() == Instruction::PHI;
2466 static inline bool classof(const Value *V) {
2467 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2470 void growOperands();
2474 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2477 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2479 //===----------------------------------------------------------------------===//
2480 // LandingPadInst Class
2481 //===----------------------------------------------------------------------===//
2483 //===---------------------------------------------------------------------------
2484 /// LandingPadInst - The landingpad instruction holds all of the information
2485 /// necessary to generate correct exception handling. The landingpad instruction
2486 /// cannot be moved from the top of a landing pad block, which itself is
2487 /// accessible only from the 'unwind' edge of an invoke. This uses the
2488 /// SubclassData field in Value to store whether or not the landingpad is a
2491 class LandingPadInst : public Instruction {
2492 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2493 /// the number actually in use.
2494 unsigned ReservedSpace;
2495 LandingPadInst(const LandingPadInst &LP);
2497 enum ClauseType { Catch, Filter };
2499 void *operator new(size_t, unsigned) = delete;
2500 // Allocate space for exactly zero operands.
2501 void *operator new(size_t s) {
2502 return User::operator new(s);
2504 void growOperands(unsigned Size);
2505 void init(unsigned NumReservedValues, const Twine &NameStr);
2507 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2508 const Twine &NameStr, Instruction *InsertBefore);
2509 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2510 const Twine &NameStr, BasicBlock *InsertAtEnd);
2513 // Note: Instruction needs to be a friend here to call cloneImpl.
2514 friend class Instruction;
2515 LandingPadInst *cloneImpl() const;
2518 /// Constructors - NumReservedClauses is a hint for the number of incoming
2519 /// clauses that this landingpad will have (use 0 if you really have no idea).
2520 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2521 const Twine &NameStr = "",
2522 Instruction *InsertBefore = nullptr);
2523 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2524 const Twine &NameStr, BasicBlock *InsertAtEnd);
2526 /// Provide fast operand accessors
2527 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2529 /// isCleanup - Return 'true' if this landingpad instruction is a
2530 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2531 /// doesn't catch the exception.
2532 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2534 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2535 void setCleanup(bool V) {
2536 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2540 /// Add a catch or filter clause to the landing pad.
2541 void addClause(Constant *ClauseVal);
2543 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2544 /// determine what type of clause this is.
2545 Constant *getClause(unsigned Idx) const {
2546 return cast<Constant>(getOperandList()[Idx]);
2549 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2550 bool isCatch(unsigned Idx) const {
2551 return !isa<ArrayType>(getOperandList()[Idx]->getType());
2554 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2555 bool isFilter(unsigned Idx) const {
2556 return isa<ArrayType>(getOperandList()[Idx]->getType());
2559 /// getNumClauses - Get the number of clauses for this landing pad.
2560 unsigned getNumClauses() const { return getNumOperands(); }
2562 /// reserveClauses - Grow the size of the operand list to accommodate the new
2563 /// number of clauses.
2564 void reserveClauses(unsigned Size) { growOperands(Size); }
2566 // Methods for support type inquiry through isa, cast, and dyn_cast:
2567 static inline bool classof(const Instruction *I) {
2568 return I->getOpcode() == Instruction::LandingPad;
2570 static inline bool classof(const Value *V) {
2571 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2576 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
2579 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2581 //===----------------------------------------------------------------------===//
2583 //===----------------------------------------------------------------------===//
2585 //===---------------------------------------------------------------------------
2586 /// ReturnInst - Return a value (possibly void), from a function. Execution
2587 /// does not continue in this function any longer.
2589 class ReturnInst : public TerminatorInst {
2590 ReturnInst(const ReturnInst &RI);
2593 // ReturnInst constructors:
2594 // ReturnInst() - 'ret void' instruction
2595 // ReturnInst( null) - 'ret void' instruction
2596 // ReturnInst(Value* X) - 'ret X' instruction
2597 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2598 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2599 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2600 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2602 // NOTE: If the Value* passed is of type void then the constructor behaves as
2603 // if it was passed NULL.
2604 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2605 Instruction *InsertBefore = nullptr);
2606 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2607 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2609 // Note: Instruction needs to be a friend here to call cloneImpl.
2610 friend class Instruction;
2611 ReturnInst *cloneImpl() const;
2614 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2615 Instruction *InsertBefore = nullptr) {
2616 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2618 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2619 BasicBlock *InsertAtEnd) {
2620 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2622 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2623 return new(0) ReturnInst(C, InsertAtEnd);
2625 ~ReturnInst() override;
2627 /// Provide fast operand accessors
2628 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2630 /// Convenience accessor. Returns null if there is no return value.
2631 Value *getReturnValue() const {
2632 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2635 unsigned getNumSuccessors() const { return 0; }
2637 // Methods for support type inquiry through isa, cast, and dyn_cast:
2638 static inline bool classof(const Instruction *I) {
2639 return (I->getOpcode() == Instruction::Ret);
2641 static inline bool classof(const Value *V) {
2642 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2645 BasicBlock *getSuccessorV(unsigned idx) const override;
2646 unsigned getNumSuccessorsV() const override;
2647 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2651 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2654 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2656 //===----------------------------------------------------------------------===//
2658 //===----------------------------------------------------------------------===//
2660 //===---------------------------------------------------------------------------
2661 /// BranchInst - Conditional or Unconditional Branch instruction.
2663 class BranchInst : public TerminatorInst {
2664 /// Ops list - Branches are strange. The operands are ordered:
2665 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2666 /// they don't have to check for cond/uncond branchness. These are mostly
2667 /// accessed relative from op_end().
2668 BranchInst(const BranchInst &BI);
2670 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2671 // BranchInst(BB *B) - 'br B'
2672 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2673 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2674 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2675 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2676 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2677 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2678 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2679 Instruction *InsertBefore = nullptr);
2680 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2681 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2682 BasicBlock *InsertAtEnd);
2684 // Note: Instruction needs to be a friend here to call cloneImpl.
2685 friend class Instruction;
2686 BranchInst *cloneImpl() const;
2689 static BranchInst *Create(BasicBlock *IfTrue,
2690 Instruction *InsertBefore = nullptr) {
2691 return new(1) BranchInst(IfTrue, InsertBefore);
2693 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2694 Value *Cond, Instruction *InsertBefore = nullptr) {
2695 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2697 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2698 return new(1) BranchInst(IfTrue, InsertAtEnd);
2700 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2701 Value *Cond, BasicBlock *InsertAtEnd) {
2702 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2705 /// Transparently provide more efficient getOperand methods.
2706 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2708 bool isUnconditional() const { return getNumOperands() == 1; }
2709 bool isConditional() const { return getNumOperands() == 3; }
2711 Value *getCondition() const {
2712 assert(isConditional() && "Cannot get condition of an uncond branch!");
2716 void setCondition(Value *V) {
2717 assert(isConditional() && "Cannot set condition of unconditional branch!");
2721 unsigned getNumSuccessors() const { return 1+isConditional(); }
2723 BasicBlock *getSuccessor(unsigned i) const {
2724 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2725 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2728 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2729 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2730 *(&Op<-1>() - idx) = (Value*)NewSucc;
2733 /// \brief Swap the successors of this branch instruction.
2735 /// Swaps the successors of the branch instruction. This also swaps any
2736 /// branch weight metadata associated with the instruction so that it
2737 /// continues to map correctly to each operand.
2738 void swapSuccessors();
2740 // Methods for support type inquiry through isa, cast, and dyn_cast:
2741 static inline bool classof(const Instruction *I) {
2742 return (I->getOpcode() == Instruction::Br);
2744 static inline bool classof(const Value *V) {
2745 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2748 BasicBlock *getSuccessorV(unsigned idx) const override;
2749 unsigned getNumSuccessorsV() const override;
2750 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2754 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2757 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2759 //===----------------------------------------------------------------------===//
2761 //===----------------------------------------------------------------------===//
2763 //===---------------------------------------------------------------------------
2764 /// SwitchInst - Multiway switch
2766 class SwitchInst : public TerminatorInst {
2767 void *operator new(size_t, unsigned) = delete;
2768 unsigned ReservedSpace;
2769 // Operand[0] = Value to switch on
2770 // Operand[1] = Default basic block destination
2771 // Operand[2n ] = Value to match
2772 // Operand[2n+1] = BasicBlock to go to on match
2773 SwitchInst(const SwitchInst &SI);
2774 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2775 void growOperands();
2776 // allocate space for exactly zero operands
2777 void *operator new(size_t s) {
2778 return User::operator new(s);
2780 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2781 /// switch on and a default destination. The number of additional cases can
2782 /// be specified here to make memory allocation more efficient. This
2783 /// constructor can also autoinsert before another instruction.
2784 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2785 Instruction *InsertBefore);
2787 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2788 /// switch on and a default destination. The number of additional cases can
2789 /// be specified here to make memory allocation more efficient. This
2790 /// constructor also autoinserts at the end of the specified BasicBlock.
2791 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2792 BasicBlock *InsertAtEnd);
2794 // Note: Instruction needs to be a friend here to call cloneImpl.
2795 friend class Instruction;
2796 SwitchInst *cloneImpl() const;
2801 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2803 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2804 class CaseIteratorT {
2812 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2814 /// Initializes case iterator for given SwitchInst and for given
2816 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2821 /// Initializes case iterator for given SwitchInst and for given
2822 /// TerminatorInst's successor index.
2823 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2824 assert(SuccessorIndex < SI->getNumSuccessors() &&
2825 "Successor index # out of range!");
2826 return SuccessorIndex != 0 ?
2827 Self(SI, SuccessorIndex - 1) :
2828 Self(SI, DefaultPseudoIndex);
2831 /// Resolves case value for current case.
2832 ConstantIntTy *getCaseValue() {
2833 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2834 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2837 /// Resolves successor for current case.
2838 BasicBlockTy *getCaseSuccessor() {
2839 assert((Index < SI->getNumCases() ||
2840 Index == DefaultPseudoIndex) &&
2841 "Index out the number of cases.");
2842 return SI->getSuccessor(getSuccessorIndex());
2845 /// Returns number of current case.
2846 unsigned getCaseIndex() const { return Index; }
2848 /// Returns TerminatorInst's successor index for current case successor.
2849 unsigned getSuccessorIndex() const {
2850 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2851 "Index out the number of cases.");
2852 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2856 // Check index correctness after increment.
2857 // Note: Index == getNumCases() means end().
2858 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2862 Self operator++(int) {
2868 // Check index correctness after decrement.
2869 // Note: Index == getNumCases() means end().
2870 // Also allow "-1" iterator here. That will became valid after ++.
2871 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2872 "Index out the number of cases.");
2876 Self operator--(int) {
2881 bool operator==(const Self& RHS) const {
2882 assert(RHS.SI == SI && "Incompatible operators.");
2883 return RHS.Index == Index;
2885 bool operator!=(const Self& RHS) const {
2886 assert(RHS.SI == SI && "Incompatible operators.");
2887 return RHS.Index != Index;
2894 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2897 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2899 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2903 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2904 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2906 /// Sets the new value for current case.
2907 void setValue(ConstantInt *V) {
2908 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2909 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2912 /// Sets the new successor for current case.
2913 void setSuccessor(BasicBlock *S) {
2914 SI->setSuccessor(getSuccessorIndex(), S);
2918 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2920 Instruction *InsertBefore = nullptr) {
2921 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2923 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2924 unsigned NumCases, BasicBlock *InsertAtEnd) {
2925 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2928 /// Provide fast operand accessors
2929 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2931 // Accessor Methods for Switch stmt
2932 Value *getCondition() const { return getOperand(0); }
2933 void setCondition(Value *V) { setOperand(0, V); }
2935 BasicBlock *getDefaultDest() const {
2936 return cast<BasicBlock>(getOperand(1));
2939 void setDefaultDest(BasicBlock *DefaultCase) {
2940 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2943 /// getNumCases - return the number of 'cases' in this switch instruction,
2944 /// except the default case
2945 unsigned getNumCases() const {
2946 return getNumOperands()/2 - 1;
2949 /// Returns a read/write iterator that points to the first
2950 /// case in SwitchInst.
2951 CaseIt case_begin() {
2952 return CaseIt(this, 0);
2954 /// Returns a read-only iterator that points to the first
2955 /// case in the SwitchInst.
2956 ConstCaseIt case_begin() const {
2957 return ConstCaseIt(this, 0);
2960 /// Returns a read/write iterator that points one past the last
2961 /// in the SwitchInst.
2963 return CaseIt(this, getNumCases());
2965 /// Returns a read-only iterator that points one past the last
2966 /// in the SwitchInst.
2967 ConstCaseIt case_end() const {
2968 return ConstCaseIt(this, getNumCases());
2971 /// cases - iteration adapter for range-for loops.
2972 iterator_range<CaseIt> cases() {
2973 return iterator_range<CaseIt>(case_begin(), case_end());
2976 /// cases - iteration adapter for range-for loops.
2977 iterator_range<ConstCaseIt> cases() const {
2978 return iterator_range<ConstCaseIt>(case_begin(), case_end());
2981 /// Returns an iterator that points to the default case.
2982 /// Note: this iterator allows to resolve successor only. Attempt
2983 /// to resolve case value causes an assertion.
2984 /// Also note, that increment and decrement also causes an assertion and
2985 /// makes iterator invalid.
2986 CaseIt case_default() {
2987 return CaseIt(this, DefaultPseudoIndex);
2989 ConstCaseIt case_default() const {
2990 return ConstCaseIt(this, DefaultPseudoIndex);
2993 /// findCaseValue - Search all of the case values for the specified constant.
2994 /// If it is explicitly handled, return the case iterator of it, otherwise
2995 /// return default case iterator to indicate
2996 /// that it is handled by the default handler.
2997 CaseIt findCaseValue(const ConstantInt *C) {
2998 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2999 if (i.getCaseValue() == C)
3001 return case_default();
3003 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3004 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
3005 if (i.getCaseValue() == C)
3007 return case_default();
3010 /// findCaseDest - Finds the unique case value for a given successor. Returns
3011 /// null if the successor is not found, not unique, or is the default case.
3012 ConstantInt *findCaseDest(BasicBlock *BB) {
3013 if (BB == getDefaultDest()) return nullptr;
3015 ConstantInt *CI = nullptr;
3016 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
3017 if (i.getCaseSuccessor() == BB) {
3018 if (CI) return nullptr; // Multiple cases lead to BB.
3019 else CI = i.getCaseValue();
3025 /// addCase - Add an entry to the switch instruction...
3027 /// This action invalidates case_end(). Old case_end() iterator will
3028 /// point to the added case.
3029 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3031 /// removeCase - This method removes the specified case and its successor
3032 /// from the switch instruction. Note that this operation may reorder the
3033 /// remaining cases at index idx and above.
3035 /// This action invalidates iterators for all cases following the one removed,
3036 /// including the case_end() iterator.
3037 void removeCase(CaseIt i);
3039 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3040 BasicBlock *getSuccessor(unsigned idx) const {
3041 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3042 return cast<BasicBlock>(getOperand(idx*2+1));
3044 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3045 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3046 setOperand(idx*2+1, (Value*)NewSucc);
3049 // Methods for support type inquiry through isa, cast, and dyn_cast:
3050 static inline bool classof(const Instruction *I) {
3051 return I->getOpcode() == Instruction::Switch;
3053 static inline bool classof(const Value *V) {
3054 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3057 BasicBlock *getSuccessorV(unsigned idx) const override;
3058 unsigned getNumSuccessorsV() const override;
3059 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3063 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3066 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
3069 //===----------------------------------------------------------------------===//
3070 // IndirectBrInst Class
3071 //===----------------------------------------------------------------------===//
3073 //===---------------------------------------------------------------------------
3074 /// IndirectBrInst - Indirect Branch Instruction.
3076 class IndirectBrInst : public TerminatorInst {
3077 void *operator new(size_t, unsigned) = delete;
3078 unsigned ReservedSpace;
3079 // Operand[0] = Value to switch on
3080 // Operand[1] = Default basic block destination
3081 // Operand[2n ] = Value to match
3082 // Operand[2n+1] = BasicBlock to go to on match
3083 IndirectBrInst(const IndirectBrInst &IBI);
3084 void init(Value *Address, unsigned NumDests);
3085 void growOperands();
3086 // allocate space for exactly zero operands
3087 void *operator new(size_t s) {
3088 return User::operator new(s);
3090 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3091 /// Address to jump to. The number of expected destinations can be specified
3092 /// here to make memory allocation more efficient. This constructor can also
3093 /// autoinsert before another instruction.
3094 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3096 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3097 /// Address to jump to. The number of expected destinations can be specified
3098 /// here to make memory allocation more efficient. This constructor also
3099 /// autoinserts at the end of the specified BasicBlock.
3100 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3102 // Note: Instruction needs to be a friend here to call cloneImpl.
3103 friend class Instruction;
3104 IndirectBrInst *cloneImpl() const;
3107 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3108 Instruction *InsertBefore = nullptr) {
3109 return new IndirectBrInst(Address, NumDests, InsertBefore);
3111 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3112 BasicBlock *InsertAtEnd) {
3113 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3116 /// Provide fast operand accessors.
3117 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3119 // Accessor Methods for IndirectBrInst instruction.
3120 Value *getAddress() { return getOperand(0); }
3121 const Value *getAddress() const { return getOperand(0); }
3122 void setAddress(Value *V) { setOperand(0, V); }
3125 /// getNumDestinations - return the number of possible destinations in this
3126 /// indirectbr instruction.
3127 unsigned getNumDestinations() const { return getNumOperands()-1; }
3129 /// getDestination - Return the specified destination.
3130 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3131 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3133 /// addDestination - Add a destination.
3135 void addDestination(BasicBlock *Dest);
3137 /// removeDestination - This method removes the specified successor from the
3138 /// indirectbr instruction.
3139 void removeDestination(unsigned i);
3141 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3142 BasicBlock *getSuccessor(unsigned i) const {
3143 return cast<BasicBlock>(getOperand(i+1));
3145 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3146 setOperand(i+1, (Value*)NewSucc);
3149 // Methods for support type inquiry through isa, cast, and dyn_cast:
3150 static inline bool classof(const Instruction *I) {
3151 return I->getOpcode() == Instruction::IndirectBr;
3153 static inline bool classof(const Value *V) {
3154 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3157 BasicBlock *getSuccessorV(unsigned idx) const override;
3158 unsigned getNumSuccessorsV() const override;
3159 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3163 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3166 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3169 //===----------------------------------------------------------------------===//
3171 //===----------------------------------------------------------------------===//
3173 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
3174 /// calling convention of the call.
3176 class InvokeInst : public TerminatorInst {
3177 AttributeSet AttributeList;
3179 InvokeInst(const InvokeInst &BI);
3180 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3181 ArrayRef<Value *> Args, const Twine &NameStr) {
3182 init(cast<FunctionType>(
3183 cast<PointerType>(Func->getType())->getElementType()),
3184 Func, IfNormal, IfException, Args, NameStr);
3186 void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
3187 BasicBlock *IfException, ArrayRef<Value *> Args,
3188 const Twine &NameStr);
3190 /// Construct an InvokeInst given a range of arguments.
3192 /// \brief Construct an InvokeInst from a range of arguments
3193 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3194 ArrayRef<Value *> Args, unsigned Values,
3195 const Twine &NameStr, Instruction *InsertBefore)
3196 : InvokeInst(cast<FunctionType>(
3197 cast<PointerType>(Func->getType())->getElementType()),
3198 Func, IfNormal, IfException, Args, Values, NameStr,
3201 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3202 BasicBlock *IfException, ArrayRef<Value *> Args,
3203 unsigned Values, const Twine &NameStr,
3204 Instruction *InsertBefore);
3205 /// Construct an InvokeInst given a range of arguments.
3207 /// \brief Construct an InvokeInst from a range of arguments
3208 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3209 ArrayRef<Value *> Args, unsigned Values,
3210 const Twine &NameStr, BasicBlock *InsertAtEnd);
3212 // Note: Instruction needs to be a friend here to call cloneImpl.
3213 friend class Instruction;
3214 InvokeInst *cloneImpl() const;
3217 static InvokeInst *Create(Value *Func,
3218 BasicBlock *IfNormal, BasicBlock *IfException,
3219 ArrayRef<Value *> Args, const Twine &NameStr = "",
3220 Instruction *InsertBefore = nullptr) {
3221 return Create(cast<FunctionType>(
3222 cast<PointerType>(Func->getType())->getElementType()),
3223 Func, IfNormal, IfException, Args, NameStr, InsertBefore);
3225 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3226 BasicBlock *IfException, ArrayRef<Value *> Args,
3227 const Twine &NameStr = "",
3228 Instruction *InsertBefore = nullptr) {
3229 unsigned Values = unsigned(Args.size()) + 3;
3230 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args,
3231 Values, NameStr, InsertBefore);
3233 static InvokeInst *Create(Value *Func,
3234 BasicBlock *IfNormal, BasicBlock *IfException,
3235 ArrayRef<Value *> Args, const Twine &NameStr,
3236 BasicBlock *InsertAtEnd) {
3237 unsigned Values = unsigned(Args.size()) + 3;
3238 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
3239 Values, NameStr, InsertAtEnd);
3242 /// Provide fast operand accessors
3243 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3245 FunctionType *getFunctionType() const { return FTy; }
3247 void mutateFunctionType(FunctionType *FTy) {
3248 mutateType(FTy->getReturnType());
3252 /// getNumArgOperands - Return the number of invoke arguments.
3254 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
3256 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3258 Value *getArgOperand(unsigned i) const { return getOperand(i); }
3259 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
3261 /// arg_operands - iteration adapter for range-for loops.
3262 iterator_range<op_iterator> arg_operands() {
3263 return iterator_range<op_iterator>(op_begin(), op_end() - 3);
3266 /// arg_operands - iteration adapter for range-for loops.
3267 iterator_range<const_op_iterator> arg_operands() const {
3268 return iterator_range<const_op_iterator>(op_begin(), op_end() - 3);
3271 /// \brief Wrappers for getting the \c Use of a invoke argument.
3272 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
3273 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
3275 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3277 CallingConv::ID getCallingConv() const {
3278 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3280 void setCallingConv(CallingConv::ID CC) {
3281 setInstructionSubclassData(static_cast<unsigned>(CC));
3284 /// getAttributes - Return the parameter attributes for this invoke.
3286 const AttributeSet &getAttributes() const { return AttributeList; }
3288 /// setAttributes - Set the parameter attributes for this invoke.
3290 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3292 /// addAttribute - adds the attribute to the list of attributes.
3293 void addAttribute(unsigned i, Attribute::AttrKind attr);
3295 /// removeAttribute - removes the attribute from the list of attributes.
3296 void removeAttribute(unsigned i, Attribute attr);
3298 /// \brief adds the dereferenceable attribute to the list of attributes.
3299 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3301 /// \brief adds the dereferenceable_or_null attribute to the list of
3303 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
3305 /// \brief Determine whether this call has the given attribute.
3306 bool hasFnAttr(Attribute::AttrKind A) const {
3307 assert(A != Attribute::NoBuiltin &&
3308 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3309 return hasFnAttrImpl(A);
3312 /// \brief Determine whether the call or the callee has the given attributes.
3313 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3315 /// \brief Extract the alignment for a call or parameter (0=unknown).
3316 unsigned getParamAlignment(unsigned i) const {
3317 return AttributeList.getParamAlignment(i);
3320 /// \brief Extract the number of dereferenceable bytes for a call or
3321 /// parameter (0=unknown).
3322 uint64_t getDereferenceableBytes(unsigned i) const {
3323 return AttributeList.getDereferenceableBytes(i);
3326 /// \brief Extract the number of dereferenceable_or_null bytes for a call or
3327 /// parameter (0=unknown).
3328 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
3329 return AttributeList.getDereferenceableOrNullBytes(i);
3332 /// \brief Return true if the call should not be treated as a call to a
3334 bool isNoBuiltin() const {
3335 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3336 // to check it by hand.
3337 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3338 !hasFnAttrImpl(Attribute::Builtin);
3341 /// \brief Return true if the call should not be inlined.
3342 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3343 void setIsNoInline() {
3344 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3347 /// \brief Determine if the call does not access memory.
3348 bool doesNotAccessMemory() const {
3349 return hasFnAttr(Attribute::ReadNone);
3351 void setDoesNotAccessMemory() {
3352 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3355 /// \brief Determine if the call does not access or only reads memory.
3356 bool onlyReadsMemory() const {
3357 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3359 void setOnlyReadsMemory() {
3360 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3363 /// \brief Determine if the call cannot return.
3364 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3365 void setDoesNotReturn() {
3366 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3369 /// \brief Determine if the call cannot unwind.
3370 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3371 void setDoesNotThrow() {
3372 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3375 /// \brief Determine if the invoke cannot be duplicated.
3376 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3377 void setCannotDuplicate() {
3378 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3381 /// \brief Determine if the call returns a structure through first
3382 /// pointer argument.
3383 bool hasStructRetAttr() const {
3384 // Be friendly and also check the callee.
3385 return paramHasAttr(1, Attribute::StructRet);
3388 /// \brief Determine if any call argument is an aggregate passed by value.
3389 bool hasByValArgument() const {
3390 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3393 /// getCalledFunction - Return the function called, or null if this is an
3394 /// indirect function invocation.
3396 Function *getCalledFunction() const {
3397 return dyn_cast<Function>(Op<-3>());
3400 /// getCalledValue - Get a pointer to the function that is invoked by this
3402 const Value *getCalledValue() const { return Op<-3>(); }
3403 Value *getCalledValue() { return Op<-3>(); }
3405 /// setCalledFunction - Set the function called.
3406 void setCalledFunction(Value* Fn) {
3408 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
3411 void setCalledFunction(FunctionType *FTy, Value *Fn) {
3413 assert(FTy == cast<FunctionType>(
3414 cast<PointerType>(Fn->getType())->getElementType()));
3418 // get*Dest - Return the destination basic blocks...
3419 BasicBlock *getNormalDest() const {
3420 return cast<BasicBlock>(Op<-2>());
3422 BasicBlock *getUnwindDest() const {
3423 return cast<BasicBlock>(Op<-1>());
3425 void setNormalDest(BasicBlock *B) {
3426 Op<-2>() = reinterpret_cast<Value*>(B);
3428 void setUnwindDest(BasicBlock *B) {
3429 Op<-1>() = reinterpret_cast<Value*>(B);
3432 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3433 /// block (the unwind destination).
3434 LandingPadInst *getLandingPadInst() const;
3436 BasicBlock *getSuccessor(unsigned i) const {
3437 assert(i < 2 && "Successor # out of range for invoke!");
3438 return i == 0 ? getNormalDest() : getUnwindDest();
3441 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3442 assert(idx < 2 && "Successor # out of range for invoke!");
3443 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3446 unsigned getNumSuccessors() const { return 2; }
3448 // Methods for support type inquiry through isa, cast, and dyn_cast:
3449 static inline bool classof(const Instruction *I) {
3450 return (I->getOpcode() == Instruction::Invoke);
3452 static inline bool classof(const Value *V) {
3453 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3457 BasicBlock *getSuccessorV(unsigned idx) const override;
3458 unsigned getNumSuccessorsV() const override;
3459 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3461 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3463 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3464 // method so that subclasses cannot accidentally use it.
3465 void setInstructionSubclassData(unsigned short D) {
3466 Instruction::setInstructionSubclassData(D);
3471 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3474 InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3475 BasicBlock *IfException, ArrayRef<Value *> Args,
3476 unsigned Values, const Twine &NameStr,
3477 Instruction *InsertBefore)
3478 : TerminatorInst(Ty->getReturnType(), Instruction::Invoke,
3479 OperandTraits<InvokeInst>::op_end(this) - Values, Values,
3481 init(Ty, Func, IfNormal, IfException, Args, NameStr);
3483 InvokeInst::InvokeInst(Value *Func,
3484 BasicBlock *IfNormal, BasicBlock *IfException,
3485 ArrayRef<Value *> Args, unsigned Values,
3486 const Twine &NameStr, BasicBlock *InsertAtEnd)
3487 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3488 ->getElementType())->getReturnType(),
3489 Instruction::Invoke,
3490 OperandTraits<InvokeInst>::op_end(this) - Values,
3491 Values, InsertAtEnd) {
3492 init(Func, IfNormal, IfException, Args, NameStr);
3495 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3497 //===----------------------------------------------------------------------===//
3499 //===----------------------------------------------------------------------===//
3501 //===---------------------------------------------------------------------------
3502 /// ResumeInst - Resume the propagation of an exception.
3504 class ResumeInst : public TerminatorInst {
3505 ResumeInst(const ResumeInst &RI);
3507 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3508 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3510 // Note: Instruction needs to be a friend here to call cloneImpl.
3511 friend class Instruction;
3512 ResumeInst *cloneImpl() const;
3515 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3516 return new(1) ResumeInst(Exn, InsertBefore);
3518 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3519 return new(1) ResumeInst(Exn, InsertAtEnd);
3522 /// Provide fast operand accessors
3523 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3525 /// Convenience accessor.
3526 Value *getValue() const { return Op<0>(); }
3528 unsigned getNumSuccessors() const { return 0; }
3530 // Methods for support type inquiry through isa, cast, and dyn_cast:
3531 static inline bool classof(const Instruction *I) {
3532 return I->getOpcode() == Instruction::Resume;
3534 static inline bool classof(const Value *V) {
3535 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3538 BasicBlock *getSuccessorV(unsigned idx) const override;
3539 unsigned getNumSuccessorsV() const override;
3540 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3544 struct OperandTraits<ResumeInst> :
3545 public FixedNumOperandTraits<ResumeInst, 1> {
3548 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3550 //===----------------------------------------------------------------------===//
3551 // UnreachableInst Class
3552 //===----------------------------------------------------------------------===//
3554 //===---------------------------------------------------------------------------
3555 /// UnreachableInst - This function has undefined behavior. In particular, the
3556 /// presence of this instruction indicates some higher level knowledge that the
3557 /// end of the block cannot be reached.
3559 class UnreachableInst : public TerminatorInst {
3560 void *operator new(size_t, unsigned) = delete;
3562 // Note: Instruction needs to be a friend here to call cloneImpl.
3563 friend class Instruction;
3564 UnreachableInst *cloneImpl() const;
3567 // allocate space for exactly zero operands
3568 void *operator new(size_t s) {
3569 return User::operator new(s, 0);
3571 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
3572 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3574 unsigned getNumSuccessors() const { return 0; }
3576 // Methods for support type inquiry through isa, cast, and dyn_cast:
3577 static inline bool classof(const Instruction *I) {
3578 return I->getOpcode() == Instruction::Unreachable;
3580 static inline bool classof(const Value *V) {
3581 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3584 BasicBlock *getSuccessorV(unsigned idx) const override;
3585 unsigned getNumSuccessorsV() const override;
3586 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3589 //===----------------------------------------------------------------------===//
3591 //===----------------------------------------------------------------------===//
3593 /// \brief This class represents a truncation of integer types.
3594 class TruncInst : public CastInst {
3596 // Note: Instruction needs to be a friend here to call cloneImpl.
3597 friend class Instruction;
3598 /// \brief Clone an identical TruncInst
3599 TruncInst *cloneImpl() const;
3602 /// \brief Constructor with insert-before-instruction semantics
3604 Value *S, ///< The value to be truncated
3605 Type *Ty, ///< The (smaller) type to truncate to
3606 const Twine &NameStr = "", ///< A name for the new instruction
3607 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3610 /// \brief Constructor with insert-at-end-of-block semantics
3612 Value *S, ///< The value to be truncated
3613 Type *Ty, ///< The (smaller) type to truncate to
3614 const Twine &NameStr, ///< A name for the new instruction
3615 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3618 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3619 static inline bool classof(const Instruction *I) {
3620 return I->getOpcode() == Trunc;
3622 static inline bool classof(const Value *V) {
3623 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3627 //===----------------------------------------------------------------------===//
3629 //===----------------------------------------------------------------------===//
3631 /// \brief This class represents zero extension of integer types.
3632 class ZExtInst : public CastInst {
3634 // Note: Instruction needs to be a friend here to call cloneImpl.
3635 friend class Instruction;
3636 /// \brief Clone an identical ZExtInst
3637 ZExtInst *cloneImpl() const;
3640 /// \brief Constructor with insert-before-instruction semantics
3642 Value *S, ///< The value to be zero extended
3643 Type *Ty, ///< The type to zero extend to
3644 const Twine &NameStr = "", ///< A name for the new instruction
3645 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3648 /// \brief Constructor with insert-at-end semantics.
3650 Value *S, ///< The value to be zero extended
3651 Type *Ty, ///< The type to zero extend to
3652 const Twine &NameStr, ///< A name for the new instruction
3653 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3656 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3657 static inline bool classof(const Instruction *I) {
3658 return I->getOpcode() == ZExt;
3660 static inline bool classof(const Value *V) {
3661 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3665 //===----------------------------------------------------------------------===//
3667 //===----------------------------------------------------------------------===//
3669 /// \brief This class represents a sign extension of integer types.
3670 class SExtInst : public CastInst {
3672 // Note: Instruction needs to be a friend here to call cloneImpl.
3673 friend class Instruction;
3674 /// \brief Clone an identical SExtInst
3675 SExtInst *cloneImpl() const;
3678 /// \brief Constructor with insert-before-instruction semantics
3680 Value *S, ///< The value to be sign extended
3681 Type *Ty, ///< The type to sign extend to
3682 const Twine &NameStr = "", ///< A name for the new instruction
3683 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3686 /// \brief Constructor with insert-at-end-of-block semantics
3688 Value *S, ///< The value to be sign extended
3689 Type *Ty, ///< The type to sign extend to
3690 const Twine &NameStr, ///< A name for the new instruction
3691 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3694 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3695 static inline bool classof(const Instruction *I) {
3696 return I->getOpcode() == SExt;
3698 static inline bool classof(const Value *V) {
3699 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3703 //===----------------------------------------------------------------------===//
3704 // FPTruncInst Class
3705 //===----------------------------------------------------------------------===//
3707 /// \brief This class represents a truncation of floating point types.
3708 class FPTruncInst : public CastInst {
3710 // Note: Instruction needs to be a friend here to call cloneImpl.
3711 friend class Instruction;
3712 /// \brief Clone an identical FPTruncInst
3713 FPTruncInst *cloneImpl() const;
3716 /// \brief Constructor with insert-before-instruction semantics
3718 Value *S, ///< The value to be truncated
3719 Type *Ty, ///< The type to truncate to
3720 const Twine &NameStr = "", ///< A name for the new instruction
3721 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3724 /// \brief Constructor with insert-before-instruction semantics
3726 Value *S, ///< The value to be truncated
3727 Type *Ty, ///< The type to truncate to
3728 const Twine &NameStr, ///< A name for the new instruction
3729 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3732 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3733 static inline bool classof(const Instruction *I) {
3734 return I->getOpcode() == FPTrunc;
3736 static inline bool classof(const Value *V) {
3737 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3741 //===----------------------------------------------------------------------===//
3743 //===----------------------------------------------------------------------===//
3745 /// \brief This class represents an extension of floating point types.
3746 class FPExtInst : public CastInst {
3748 // Note: Instruction needs to be a friend here to call cloneImpl.
3749 friend class Instruction;
3750 /// \brief Clone an identical FPExtInst
3751 FPExtInst *cloneImpl() const;
3754 /// \brief Constructor with insert-before-instruction semantics
3756 Value *S, ///< The value to be extended
3757 Type *Ty, ///< The type to extend to
3758 const Twine &NameStr = "", ///< A name for the new instruction
3759 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3762 /// \brief Constructor with insert-at-end-of-block semantics
3764 Value *S, ///< The value to be extended
3765 Type *Ty, ///< The type to extend to
3766 const Twine &NameStr, ///< A name for the new instruction
3767 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3770 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3771 static inline bool classof(const Instruction *I) {
3772 return I->getOpcode() == FPExt;
3774 static inline bool classof(const Value *V) {
3775 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3779 //===----------------------------------------------------------------------===//
3781 //===----------------------------------------------------------------------===//
3783 /// \brief This class represents a cast unsigned integer to floating point.
3784 class UIToFPInst : public CastInst {
3786 // Note: Instruction needs to be a friend here to call cloneImpl.
3787 friend class Instruction;
3788 /// \brief Clone an identical UIToFPInst
3789 UIToFPInst *cloneImpl() const;
3792 /// \brief Constructor with insert-before-instruction semantics
3794 Value *S, ///< The value to be converted
3795 Type *Ty, ///< The type to convert to
3796 const Twine &NameStr = "", ///< A name for the new instruction
3797 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3800 /// \brief Constructor with insert-at-end-of-block semantics
3802 Value *S, ///< The value to be converted
3803 Type *Ty, ///< The type to convert to
3804 const Twine &NameStr, ///< A name for the new instruction
3805 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3808 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3809 static inline bool classof(const Instruction *I) {
3810 return I->getOpcode() == UIToFP;
3812 static inline bool classof(const Value *V) {
3813 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3817 //===----------------------------------------------------------------------===//
3819 //===----------------------------------------------------------------------===//
3821 /// \brief This class represents a cast from signed integer to floating point.
3822 class SIToFPInst : public CastInst {
3824 // Note: Instruction needs to be a friend here to call cloneImpl.
3825 friend class Instruction;
3826 /// \brief Clone an identical SIToFPInst
3827 SIToFPInst *cloneImpl() const;
3830 /// \brief Constructor with insert-before-instruction semantics
3832 Value *S, ///< The value to be converted
3833 Type *Ty, ///< The type to convert to
3834 const Twine &NameStr = "", ///< A name for the new instruction
3835 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3838 /// \brief Constructor with insert-at-end-of-block semantics
3840 Value *S, ///< The value to be converted
3841 Type *Ty, ///< The type to convert to
3842 const Twine &NameStr, ///< A name for the new instruction
3843 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3846 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3847 static inline bool classof(const Instruction *I) {
3848 return I->getOpcode() == SIToFP;
3850 static inline bool classof(const Value *V) {
3851 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3855 //===----------------------------------------------------------------------===//
3857 //===----------------------------------------------------------------------===//
3859 /// \brief This class represents a cast from floating point to unsigned integer
3860 class FPToUIInst : public CastInst {
3862 // Note: Instruction needs to be a friend here to call cloneImpl.
3863 friend class Instruction;
3864 /// \brief Clone an identical FPToUIInst
3865 FPToUIInst *cloneImpl() const;
3868 /// \brief Constructor with insert-before-instruction semantics
3870 Value *S, ///< The value to be converted
3871 Type *Ty, ///< The type to convert to
3872 const Twine &NameStr = "", ///< A name for the new instruction
3873 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3876 /// \brief Constructor with insert-at-end-of-block semantics
3878 Value *S, ///< The value to be converted
3879 Type *Ty, ///< The type to convert to
3880 const Twine &NameStr, ///< A name for the new instruction
3881 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3884 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3885 static inline bool classof(const Instruction *I) {
3886 return I->getOpcode() == FPToUI;
3888 static inline bool classof(const Value *V) {
3889 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3893 //===----------------------------------------------------------------------===//
3895 //===----------------------------------------------------------------------===//
3897 /// \brief This class represents a cast from floating point to signed integer.
3898 class FPToSIInst : public CastInst {
3900 // Note: Instruction needs to be a friend here to call cloneImpl.
3901 friend class Instruction;
3902 /// \brief Clone an identical FPToSIInst
3903 FPToSIInst *cloneImpl() const;
3906 /// \brief Constructor with insert-before-instruction semantics
3908 Value *S, ///< The value to be converted
3909 Type *Ty, ///< The type to convert to
3910 const Twine &NameStr = "", ///< A name for the new instruction
3911 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3914 /// \brief Constructor with insert-at-end-of-block semantics
3916 Value *S, ///< The value to be converted
3917 Type *Ty, ///< The type to convert to
3918 const Twine &NameStr, ///< A name for the new instruction
3919 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3922 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3923 static inline bool classof(const Instruction *I) {
3924 return I->getOpcode() == FPToSI;
3926 static inline bool classof(const Value *V) {
3927 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3931 //===----------------------------------------------------------------------===//
3932 // IntToPtrInst Class
3933 //===----------------------------------------------------------------------===//
3935 /// \brief This class represents a cast from an integer to a pointer.
3936 class IntToPtrInst : public CastInst {
3938 /// \brief Constructor with insert-before-instruction semantics
3940 Value *S, ///< The value to be converted
3941 Type *Ty, ///< The type to convert to
3942 const Twine &NameStr = "", ///< A name for the new instruction
3943 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3946 /// \brief Constructor with insert-at-end-of-block semantics
3948 Value *S, ///< The value to be converted
3949 Type *Ty, ///< The type to convert to
3950 const Twine &NameStr, ///< A name for the new instruction
3951 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3954 // Note: Instruction needs to be a friend here to call cloneImpl.
3955 friend class Instruction;
3956 /// \brief Clone an identical IntToPtrInst
3957 IntToPtrInst *cloneImpl() const;
3959 /// \brief Returns the address space of this instruction's pointer type.
3960 unsigned getAddressSpace() const {
3961 return getType()->getPointerAddressSpace();
3964 // Methods for support type inquiry through isa, cast, and dyn_cast:
3965 static inline bool classof(const Instruction *I) {
3966 return I->getOpcode() == IntToPtr;
3968 static inline bool classof(const Value *V) {
3969 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3973 //===----------------------------------------------------------------------===//
3974 // PtrToIntInst Class
3975 //===----------------------------------------------------------------------===//
3977 /// \brief This class represents a cast from a pointer to an integer
3978 class PtrToIntInst : public CastInst {
3980 // Note: Instruction needs to be a friend here to call cloneImpl.
3981 friend class Instruction;
3982 /// \brief Clone an identical PtrToIntInst
3983 PtrToIntInst *cloneImpl() const;
3986 /// \brief Constructor with insert-before-instruction semantics
3988 Value *S, ///< The value to be converted
3989 Type *Ty, ///< The type to convert to
3990 const Twine &NameStr = "", ///< A name for the new instruction
3991 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
3994 /// \brief Constructor with insert-at-end-of-block semantics
3996 Value *S, ///< The value to be converted
3997 Type *Ty, ///< The type to convert to
3998 const Twine &NameStr, ///< A name for the new instruction
3999 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4002 /// \brief Gets the pointer operand.
4003 Value *getPointerOperand() { return getOperand(0); }
4004 /// \brief Gets the pointer operand.
4005 const Value *getPointerOperand() const { return getOperand(0); }
4006 /// \brief Gets the operand index of the pointer operand.
4007 static unsigned getPointerOperandIndex() { return 0U; }
4009 /// \brief Returns the address space of the pointer operand.
4010 unsigned getPointerAddressSpace() const {
4011 return getPointerOperand()->getType()->getPointerAddressSpace();
4014 // Methods for support type inquiry through isa, cast, and dyn_cast:
4015 static inline bool classof(const Instruction *I) {
4016 return I->getOpcode() == PtrToInt;
4018 static inline bool classof(const Value *V) {
4019 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4023 //===----------------------------------------------------------------------===//
4024 // BitCastInst Class
4025 //===----------------------------------------------------------------------===//
4027 /// \brief This class represents a no-op cast from one type to another.
4028 class BitCastInst : public CastInst {
4030 // Note: Instruction needs to be a friend here to call cloneImpl.
4031 friend class Instruction;
4032 /// \brief Clone an identical BitCastInst
4033 BitCastInst *cloneImpl() const;
4036 /// \brief Constructor with insert-before-instruction semantics
4038 Value *S, ///< The value to be casted
4039 Type *Ty, ///< The type to casted to
4040 const Twine &NameStr = "", ///< A name for the new instruction
4041 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4044 /// \brief Constructor with insert-at-end-of-block semantics
4046 Value *S, ///< The value to be casted
4047 Type *Ty, ///< The type to casted to
4048 const Twine &NameStr, ///< A name for the new instruction
4049 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4052 // Methods for support type inquiry through isa, cast, and dyn_cast:
4053 static inline bool classof(const Instruction *I) {
4054 return I->getOpcode() == BitCast;
4056 static inline bool classof(const Value *V) {
4057 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4061 //===----------------------------------------------------------------------===//
4062 // AddrSpaceCastInst Class
4063 //===----------------------------------------------------------------------===//
4065 /// \brief This class represents a conversion between pointers from
4066 /// one address space to another.
4067 class AddrSpaceCastInst : public CastInst {
4069 // Note: Instruction needs to be a friend here to call cloneImpl.
4070 friend class Instruction;
4071 /// \brief Clone an identical AddrSpaceCastInst
4072 AddrSpaceCastInst *cloneImpl() const;
4075 /// \brief Constructor with insert-before-instruction semantics
4077 Value *S, ///< The value to be casted
4078 Type *Ty, ///< The type to casted to
4079 const Twine &NameStr = "", ///< A name for the new instruction
4080 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4083 /// \brief Constructor with insert-at-end-of-block semantics
4085 Value *S, ///< The value to be casted
4086 Type *Ty, ///< The type to casted to
4087 const Twine &NameStr, ///< A name for the new instruction
4088 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4091 // Methods for support type inquiry through isa, cast, and dyn_cast:
4092 static inline bool classof(const Instruction *I) {
4093 return I->getOpcode() == AddrSpaceCast;
4095 static inline bool classof(const Value *V) {
4096 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4100 } // End llvm namespace