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/STLExtras.h"
22 #include "llvm/ADT/iterator_range.h"
23 #include "llvm/IR/Attributes.h"
24 #include "llvm/IR/CallingConv.h"
25 #include "llvm/IR/DerivedTypes.h"
26 #include "llvm/IR/Function.h"
27 #include "llvm/IR/InstrTypes.h"
28 #include "llvm/Support/AtomicOrdering.h"
29 #include "llvm/Support/ErrorHandling.h"
40 enum SynchronizationScope {
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
49 /// AllocaInst - an instruction to allocate memory on the stack
51 class AllocaInst : public UnaryInstruction {
55 // Note: Instruction needs to be a friend here to call cloneImpl.
56 friend class Instruction;
57 AllocaInst *cloneImpl() const;
60 explicit AllocaInst(Type *Ty, Value *ArraySize = nullptr,
61 const Twine &Name = "",
62 Instruction *InsertBefore = nullptr);
63 AllocaInst(Type *Ty, Value *ArraySize,
64 const Twine &Name, BasicBlock *InsertAtEnd);
66 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = nullptr);
67 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
69 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
70 const Twine &Name = "", Instruction *InsertBefore = nullptr);
71 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
72 const Twine &Name, BasicBlock *InsertAtEnd);
74 // Out of line virtual method, so the vtable, etc. has a home.
75 ~AllocaInst() override;
77 /// isArrayAllocation - Return true if there is an allocation size parameter
78 /// to the allocation instruction that is not 1.
80 bool isArrayAllocation() const;
82 /// getArraySize - Get the number of elements allocated. For a simple
83 /// allocation of a single element, this will return a constant 1 value.
85 const Value *getArraySize() const { return getOperand(0); }
86 Value *getArraySize() { return getOperand(0); }
88 /// getType - Overload to return most specific pointer type
90 PointerType *getType() const {
91 return cast<PointerType>(Instruction::getType());
94 /// getAllocatedType - Return the type that is being allocated by the
97 Type *getAllocatedType() const { return AllocatedType; }
98 /// \brief for use only in special circumstances that need to generically
99 /// transform a whole instruction (eg: IR linking and vectorization).
100 void setAllocatedType(Type *Ty) { AllocatedType = Ty; }
102 /// getAlignment - Return the alignment of the memory that is being allocated
103 /// by the instruction.
105 unsigned getAlignment() const {
106 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
108 void setAlignment(unsigned Align);
110 /// isStaticAlloca - Return true if this alloca is in the entry block of the
111 /// function and is a constant size. If so, the code generator will fold it
112 /// into the prolog/epilog code, so it is basically free.
113 bool isStaticAlloca() const;
115 /// \brief Return true if this alloca is used as an inalloca argument to a
116 /// call. Such allocas are never considered static even if they are in the
118 bool isUsedWithInAlloca() const {
119 return getSubclassDataFromInstruction() & 32;
122 /// \brief Specify whether this alloca is used to represent the arguments to
124 void setUsedWithInAlloca(bool V) {
125 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
129 /// \brief Return true if this alloca is used as a swifterror argument to a
131 bool isSwiftError() const {
132 return getSubclassDataFromInstruction() & 64;
135 /// \brief Specify whether this alloca is used to represent a swifterror.
136 void setSwiftError(bool V) {
137 setInstructionSubclassData((getSubclassDataFromInstruction() & ~64) |
141 // Methods for support type inquiry through isa, cast, and dyn_cast:
142 static inline bool classof(const Instruction *I) {
143 return (I->getOpcode() == Instruction::Alloca);
145 static inline bool classof(const Value *V) {
146 return isa<Instruction>(V) && classof(cast<Instruction>(V));
150 // Shadow Instruction::setInstructionSubclassData with a private forwarding
151 // method so that subclasses cannot accidentally use it.
152 void setInstructionSubclassData(unsigned short D) {
153 Instruction::setInstructionSubclassData(D);
157 //===----------------------------------------------------------------------===//
159 //===----------------------------------------------------------------------===//
161 /// LoadInst - an instruction for reading from memory. This uses the
162 /// SubclassData field in Value to store whether or not the load is volatile.
164 class LoadInst : public UnaryInstruction {
168 // Note: Instruction needs to be a friend here to call cloneImpl.
169 friend class Instruction;
170 LoadInst *cloneImpl() const;
173 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
174 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
175 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile = false,
176 Instruction *InsertBefore = nullptr);
177 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
178 Instruction *InsertBefore = nullptr)
179 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
180 NameStr, isVolatile, InsertBefore) {}
181 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
182 BasicBlock *InsertAtEnd);
183 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
184 Instruction *InsertBefore = nullptr)
185 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
186 NameStr, isVolatile, Align, InsertBefore) {}
187 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
188 unsigned Align, Instruction *InsertBefore = nullptr);
189 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
190 unsigned Align, BasicBlock *InsertAtEnd);
191 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile, unsigned Align,
192 AtomicOrdering Order, SynchronizationScope SynchScope = CrossThread,
193 Instruction *InsertBefore = nullptr)
194 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
195 NameStr, isVolatile, Align, Order, SynchScope, InsertBefore) {}
196 LoadInst(Type *Ty, Value *Ptr, const Twine &NameStr, bool isVolatile,
197 unsigned Align, AtomicOrdering Order,
198 SynchronizationScope SynchScope = CrossThread,
199 Instruction *InsertBefore = nullptr);
200 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
201 unsigned Align, AtomicOrdering Order,
202 SynchronizationScope SynchScope,
203 BasicBlock *InsertAtEnd);
205 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
206 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
207 LoadInst(Type *Ty, Value *Ptr, const char *NameStr = nullptr,
208 bool isVolatile = false, Instruction *InsertBefore = nullptr);
209 explicit LoadInst(Value *Ptr, const char *NameStr = nullptr,
210 bool isVolatile = false,
211 Instruction *InsertBefore = nullptr)
212 : LoadInst(cast<PointerType>(Ptr->getType())->getElementType(), Ptr,
213 NameStr, isVolatile, InsertBefore) {}
214 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
215 BasicBlock *InsertAtEnd);
217 /// isVolatile - Return true if this is a load from a volatile memory
220 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
222 /// setVolatile - Specify whether this is a volatile load or not.
224 void setVolatile(bool V) {
225 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
229 /// getAlignment - Return the alignment of the access that is being performed
231 unsigned getAlignment() const {
232 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
235 void setAlignment(unsigned Align);
237 /// Returns the ordering effect of this fence.
238 AtomicOrdering getOrdering() const {
239 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
242 /// Set the ordering constraint on this load. May not be Release or
244 void setOrdering(AtomicOrdering Ordering) {
245 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
246 ((unsigned)Ordering << 7));
249 SynchronizationScope getSynchScope() const {
250 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
253 /// Specify whether this load is ordered with respect to all
254 /// concurrently executing threads, or only with respect to signal handlers
255 /// executing in the same thread.
256 void setSynchScope(SynchronizationScope xthread) {
257 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
261 void setAtomic(AtomicOrdering Ordering,
262 SynchronizationScope SynchScope = CrossThread) {
263 setOrdering(Ordering);
264 setSynchScope(SynchScope);
267 bool isSimple() const { return !isAtomic() && !isVolatile(); }
268 bool isUnordered() const {
269 return (getOrdering() == AtomicOrdering::NotAtomic ||
270 getOrdering() == AtomicOrdering::Unordered) &&
274 Value *getPointerOperand() { return getOperand(0); }
275 const Value *getPointerOperand() const { return getOperand(0); }
276 static unsigned getPointerOperandIndex() { return 0U; }
278 /// \brief Returns the address space of the pointer operand.
279 unsigned getPointerAddressSpace() const {
280 return getPointerOperand()->getType()->getPointerAddressSpace();
283 // Methods for support type inquiry through isa, cast, and dyn_cast:
284 static inline bool classof(const Instruction *I) {
285 return I->getOpcode() == Instruction::Load;
287 static inline bool classof(const Value *V) {
288 return isa<Instruction>(V) && classof(cast<Instruction>(V));
292 // Shadow Instruction::setInstructionSubclassData with a private forwarding
293 // method so that subclasses cannot accidentally use it.
294 void setInstructionSubclassData(unsigned short D) {
295 Instruction::setInstructionSubclassData(D);
299 //===----------------------------------------------------------------------===//
301 //===----------------------------------------------------------------------===//
303 /// StoreInst - an instruction for storing to memory
305 class StoreInst : public Instruction {
306 void *operator new(size_t, unsigned) = delete;
310 // Note: Instruction needs to be a friend here to call cloneImpl.
311 friend class Instruction;
312 StoreInst *cloneImpl() const;
315 // allocate space for exactly two operands
316 void *operator new(size_t s) {
317 return User::operator new(s, 2);
319 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
320 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
321 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
322 Instruction *InsertBefore = nullptr);
323 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
324 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
325 unsigned Align, Instruction *InsertBefore = nullptr);
326 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
327 unsigned Align, BasicBlock *InsertAtEnd);
328 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
329 unsigned Align, AtomicOrdering Order,
330 SynchronizationScope SynchScope = CrossThread,
331 Instruction *InsertBefore = nullptr);
332 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
333 unsigned Align, AtomicOrdering Order,
334 SynchronizationScope SynchScope,
335 BasicBlock *InsertAtEnd);
337 /// isVolatile - Return true if this is a store to a volatile memory
340 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
342 /// setVolatile - Specify whether this is a volatile store or not.
344 void setVolatile(bool V) {
345 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
349 /// Transparently provide more efficient getOperand methods.
350 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
352 /// getAlignment - Return the alignment of the access that is being performed
354 unsigned getAlignment() const {
355 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
358 void setAlignment(unsigned Align);
360 /// Returns the ordering effect of this store.
361 AtomicOrdering getOrdering() const {
362 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
365 /// Set the ordering constraint on this store. May not be Acquire or
367 void setOrdering(AtomicOrdering Ordering) {
368 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
369 ((unsigned)Ordering << 7));
372 SynchronizationScope getSynchScope() const {
373 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
376 /// Specify whether this store instruction is ordered with respect to all
377 /// concurrently executing threads, or only with respect to signal handlers
378 /// executing in the same thread.
379 void setSynchScope(SynchronizationScope xthread) {
380 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
384 void setAtomic(AtomicOrdering Ordering,
385 SynchronizationScope SynchScope = CrossThread) {
386 setOrdering(Ordering);
387 setSynchScope(SynchScope);
390 bool isSimple() const { return !isAtomic() && !isVolatile(); }
391 bool isUnordered() const {
392 return (getOrdering() == AtomicOrdering::NotAtomic ||
393 getOrdering() == AtomicOrdering::Unordered) &&
397 Value *getValueOperand() { return getOperand(0); }
398 const Value *getValueOperand() const { return getOperand(0); }
400 Value *getPointerOperand() { return getOperand(1); }
401 const Value *getPointerOperand() const { return getOperand(1); }
402 static unsigned getPointerOperandIndex() { return 1U; }
404 /// \brief Returns the address space of the pointer operand.
405 unsigned getPointerAddressSpace() const {
406 return getPointerOperand()->getType()->getPointerAddressSpace();
409 // Methods for support type inquiry through isa, cast, and dyn_cast:
410 static inline bool classof(const Instruction *I) {
411 return I->getOpcode() == Instruction::Store;
413 static inline bool classof(const Value *V) {
414 return isa<Instruction>(V) && classof(cast<Instruction>(V));
418 // Shadow Instruction::setInstructionSubclassData with a private forwarding
419 // method so that subclasses cannot accidentally use it.
420 void setInstructionSubclassData(unsigned short D) {
421 Instruction::setInstructionSubclassData(D);
426 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
429 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
431 //===----------------------------------------------------------------------===//
433 //===----------------------------------------------------------------------===//
435 /// FenceInst - an instruction for ordering other memory operations
437 class FenceInst : public Instruction {
438 void *operator new(size_t, unsigned) = delete;
439 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
442 // Note: Instruction needs to be a friend here to call cloneImpl.
443 friend class Instruction;
444 FenceInst *cloneImpl() const;
447 // allocate space for exactly zero operands
448 void *operator new(size_t s) {
449 return User::operator new(s, 0);
452 // Ordering may only be Acquire, Release, AcquireRelease, or
453 // SequentiallyConsistent.
454 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
455 SynchronizationScope SynchScope = CrossThread,
456 Instruction *InsertBefore = nullptr);
457 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
458 SynchronizationScope SynchScope,
459 BasicBlock *InsertAtEnd);
461 /// Returns the ordering effect of this fence.
462 AtomicOrdering getOrdering() const {
463 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
466 /// Set the ordering constraint on this fence. May only be Acquire, Release,
467 /// AcquireRelease, or SequentiallyConsistent.
468 void setOrdering(AtomicOrdering Ordering) {
469 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
470 ((unsigned)Ordering << 1));
473 SynchronizationScope getSynchScope() const {
474 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
477 /// Specify whether this fence orders other operations with respect to all
478 /// concurrently executing threads, or only with respect to signal handlers
479 /// executing in the same thread.
480 void setSynchScope(SynchronizationScope xthread) {
481 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
485 // Methods for support type inquiry through isa, cast, and dyn_cast:
486 static inline bool classof(const Instruction *I) {
487 return I->getOpcode() == Instruction::Fence;
489 static inline bool classof(const Value *V) {
490 return isa<Instruction>(V) && classof(cast<Instruction>(V));
494 // Shadow Instruction::setInstructionSubclassData with a private forwarding
495 // method so that subclasses cannot accidentally use it.
496 void setInstructionSubclassData(unsigned short D) {
497 Instruction::setInstructionSubclassData(D);
501 //===----------------------------------------------------------------------===//
502 // AtomicCmpXchgInst Class
503 //===----------------------------------------------------------------------===//
505 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
506 /// specified value is in a memory location, and, if it is, stores a new value
507 /// there. Returns the value that was loaded.
509 class AtomicCmpXchgInst : public Instruction {
510 void *operator new(size_t, unsigned) = delete;
511 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
512 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
513 SynchronizationScope SynchScope);
516 // Note: Instruction needs to be a friend here to call cloneImpl.
517 friend class Instruction;
518 AtomicCmpXchgInst *cloneImpl() const;
521 // allocate space for exactly three operands
522 void *operator new(size_t s) {
523 return User::operator new(s, 3);
525 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
526 AtomicOrdering SuccessOrdering,
527 AtomicOrdering FailureOrdering,
528 SynchronizationScope SynchScope,
529 Instruction *InsertBefore = nullptr);
530 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
531 AtomicOrdering SuccessOrdering,
532 AtomicOrdering FailureOrdering,
533 SynchronizationScope SynchScope,
534 BasicBlock *InsertAtEnd);
536 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
539 bool isVolatile() const {
540 return getSubclassDataFromInstruction() & 1;
543 /// setVolatile - Specify whether this is a volatile cmpxchg.
545 void setVolatile(bool V) {
546 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
550 /// Return true if this cmpxchg may spuriously fail.
551 bool isWeak() const {
552 return getSubclassDataFromInstruction() & 0x100;
555 void setWeak(bool IsWeak) {
556 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x100) |
560 /// Transparently provide more efficient getOperand methods.
561 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
563 /// Set the ordering constraint on this cmpxchg.
564 void setSuccessOrdering(AtomicOrdering Ordering) {
565 assert(Ordering != AtomicOrdering::NotAtomic &&
566 "CmpXchg instructions can only be atomic.");
567 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
568 ((unsigned)Ordering << 2));
571 void setFailureOrdering(AtomicOrdering Ordering) {
572 assert(Ordering != AtomicOrdering::NotAtomic &&
573 "CmpXchg instructions can only be atomic.");
574 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
575 ((unsigned)Ordering << 5));
578 /// Specify whether this cmpxchg is atomic and orders other operations with
579 /// respect to all concurrently executing threads, or only with respect to
580 /// signal handlers executing in the same thread.
581 void setSynchScope(SynchronizationScope SynchScope) {
582 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
586 /// Returns the ordering constraint on this cmpxchg.
587 AtomicOrdering getSuccessOrdering() const {
588 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
591 /// Returns the ordering constraint on this cmpxchg.
592 AtomicOrdering getFailureOrdering() const {
593 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
596 /// Returns whether this cmpxchg is atomic between threads or only within a
598 SynchronizationScope getSynchScope() const {
599 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
602 Value *getPointerOperand() { return getOperand(0); }
603 const Value *getPointerOperand() const { return getOperand(0); }
604 static unsigned getPointerOperandIndex() { return 0U; }
606 Value *getCompareOperand() { return getOperand(1); }
607 const Value *getCompareOperand() const { return getOperand(1); }
609 Value *getNewValOperand() { return getOperand(2); }
610 const Value *getNewValOperand() const { return getOperand(2); }
612 /// \brief Returns the address space of the pointer operand.
613 unsigned getPointerAddressSpace() const {
614 return getPointerOperand()->getType()->getPointerAddressSpace();
617 /// \brief Returns the strongest permitted ordering on failure, given the
618 /// desired ordering on success.
620 /// If the comparison in a cmpxchg operation fails, there is no atomic store
621 /// so release semantics cannot be provided. So this function drops explicit
622 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
623 /// operation would remain SequentiallyConsistent.
624 static AtomicOrdering
625 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
626 switch (SuccessOrdering) {
628 llvm_unreachable("invalid cmpxchg success ordering");
629 case AtomicOrdering::Release:
630 case AtomicOrdering::Monotonic:
631 return AtomicOrdering::Monotonic;
632 case AtomicOrdering::AcquireRelease:
633 case AtomicOrdering::Acquire:
634 return AtomicOrdering::Acquire;
635 case AtomicOrdering::SequentiallyConsistent:
636 return AtomicOrdering::SequentiallyConsistent;
640 // Methods for support type inquiry through isa, cast, and dyn_cast:
641 static inline bool classof(const Instruction *I) {
642 return I->getOpcode() == Instruction::AtomicCmpXchg;
644 static inline bool classof(const Value *V) {
645 return isa<Instruction>(V) && classof(cast<Instruction>(V));
649 // Shadow Instruction::setInstructionSubclassData with a private forwarding
650 // method so that subclasses cannot accidentally use it.
651 void setInstructionSubclassData(unsigned short D) {
652 Instruction::setInstructionSubclassData(D);
657 struct OperandTraits<AtomicCmpXchgInst> :
658 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
661 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
663 //===----------------------------------------------------------------------===//
664 // AtomicRMWInst Class
665 //===----------------------------------------------------------------------===//
667 /// AtomicRMWInst - an instruction that atomically reads a memory location,
668 /// combines it with another value, and then stores the result back. Returns
671 class AtomicRMWInst : public Instruction {
672 void *operator new(size_t, unsigned) = delete;
675 // Note: Instruction needs to be a friend here to call cloneImpl.
676 friend class Instruction;
677 AtomicRMWInst *cloneImpl() const;
680 /// This enumeration lists the possible modifications atomicrmw can make. In
681 /// the descriptions, 'p' is the pointer to the instruction's memory location,
682 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
683 /// instruction. These instructions always return 'old'.
699 /// *p = old >signed v ? old : v
701 /// *p = old <signed v ? old : v
703 /// *p = old >unsigned v ? old : v
705 /// *p = old <unsigned v ? old : v
713 // allocate space for exactly two operands
714 void *operator new(size_t s) {
715 return User::operator new(s, 2);
717 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
718 AtomicOrdering Ordering, SynchronizationScope SynchScope,
719 Instruction *InsertBefore = nullptr);
720 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
721 AtomicOrdering Ordering, SynchronizationScope SynchScope,
722 BasicBlock *InsertAtEnd);
724 BinOp getOperation() const {
725 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
728 void setOperation(BinOp Operation) {
729 unsigned short SubclassData = getSubclassDataFromInstruction();
730 setInstructionSubclassData((SubclassData & 31) |
734 /// isVolatile - Return true if this is a RMW on a volatile memory location.
736 bool isVolatile() const {
737 return getSubclassDataFromInstruction() & 1;
740 /// setVolatile - Specify whether this is a volatile RMW or not.
742 void setVolatile(bool V) {
743 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
747 /// Transparently provide more efficient getOperand methods.
748 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
750 /// Set the ordering constraint on this RMW.
751 void setOrdering(AtomicOrdering Ordering) {
752 assert(Ordering != AtomicOrdering::NotAtomic &&
753 "atomicrmw instructions can only be atomic.");
754 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
755 ((unsigned)Ordering << 2));
758 /// Specify whether this RMW orders other operations with respect to all
759 /// concurrently executing threads, or only with respect to signal handlers
760 /// executing in the same thread.
761 void setSynchScope(SynchronizationScope SynchScope) {
762 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
766 /// Returns the ordering constraint on this RMW.
767 AtomicOrdering getOrdering() const {
768 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
771 /// Returns whether this RMW is atomic between threads or only within a
773 SynchronizationScope getSynchScope() const {
774 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
777 Value *getPointerOperand() { return getOperand(0); }
778 const Value *getPointerOperand() const { return getOperand(0); }
779 static unsigned getPointerOperandIndex() { return 0U; }
781 Value *getValOperand() { return getOperand(1); }
782 const Value *getValOperand() const { return getOperand(1); }
784 /// \brief Returns the address space of the pointer operand.
785 unsigned getPointerAddressSpace() const {
786 return getPointerOperand()->getType()->getPointerAddressSpace();
789 // Methods for support type inquiry through isa, cast, and dyn_cast:
790 static inline bool classof(const Instruction *I) {
791 return I->getOpcode() == Instruction::AtomicRMW;
793 static inline bool classof(const Value *V) {
794 return isa<Instruction>(V) && classof(cast<Instruction>(V));
798 void Init(BinOp Operation, Value *Ptr, Value *Val,
799 AtomicOrdering Ordering, SynchronizationScope SynchScope);
800 // Shadow Instruction::setInstructionSubclassData with a private forwarding
801 // method so that subclasses cannot accidentally use it.
802 void setInstructionSubclassData(unsigned short D) {
803 Instruction::setInstructionSubclassData(D);
808 struct OperandTraits<AtomicRMWInst>
809 : public FixedNumOperandTraits<AtomicRMWInst,2> {
812 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
814 //===----------------------------------------------------------------------===//
815 // GetElementPtrInst Class
816 //===----------------------------------------------------------------------===//
818 // checkGEPType - Simple wrapper function to give a better assertion failure
819 // message on bad indexes for a gep instruction.
821 inline Type *checkGEPType(Type *Ty) {
822 assert(Ty && "Invalid GetElementPtrInst indices for type!");
826 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
827 /// access elements of arrays and structs
829 class GetElementPtrInst : public Instruction {
830 Type *SourceElementType;
831 Type *ResultElementType;
833 void anchor() override;
835 GetElementPtrInst(const GetElementPtrInst &GEPI);
836 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
838 /// Constructors - Create a getelementptr instruction with a base pointer an
839 /// list of indices. The first ctor can optionally insert before an existing
840 /// instruction, the second appends the new instruction to the specified
842 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
843 ArrayRef<Value *> IdxList, unsigned Values,
844 const Twine &NameStr, Instruction *InsertBefore);
845 inline GetElementPtrInst(Type *PointeeType, Value *Ptr,
846 ArrayRef<Value *> IdxList, unsigned Values,
847 const Twine &NameStr, BasicBlock *InsertAtEnd);
850 // Note: Instruction needs to be a friend here to call cloneImpl.
851 friend class Instruction;
852 GetElementPtrInst *cloneImpl() const;
855 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
856 ArrayRef<Value *> IdxList,
857 const Twine &NameStr = "",
858 Instruction *InsertBefore = nullptr) {
859 unsigned Values = 1 + unsigned(IdxList.size());
862 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
866 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
867 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
868 NameStr, InsertBefore);
870 static GetElementPtrInst *Create(Type *PointeeType, Value *Ptr,
871 ArrayRef<Value *> IdxList,
872 const Twine &NameStr,
873 BasicBlock *InsertAtEnd) {
874 unsigned Values = 1 + unsigned(IdxList.size());
877 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType();
881 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType());
882 return new (Values) GetElementPtrInst(PointeeType, Ptr, IdxList, Values,
883 NameStr, InsertAtEnd);
886 /// Create an "inbounds" getelementptr. See the documentation for the
887 /// "inbounds" flag in LangRef.html for details.
888 static GetElementPtrInst *CreateInBounds(Value *Ptr,
889 ArrayRef<Value *> IdxList,
890 const Twine &NameStr = "",
891 Instruction *InsertBefore = nullptr){
892 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertBefore);
894 static GetElementPtrInst *
895 CreateInBounds(Type *PointeeType, Value *Ptr, ArrayRef<Value *> IdxList,
896 const Twine &NameStr = "",
897 Instruction *InsertBefore = nullptr) {
898 GetElementPtrInst *GEP =
899 Create(PointeeType, Ptr, IdxList, NameStr, InsertBefore);
900 GEP->setIsInBounds(true);
903 static GetElementPtrInst *CreateInBounds(Value *Ptr,
904 ArrayRef<Value *> IdxList,
905 const Twine &NameStr,
906 BasicBlock *InsertAtEnd) {
907 return CreateInBounds(nullptr, Ptr, IdxList, NameStr, InsertAtEnd);
909 static GetElementPtrInst *CreateInBounds(Type *PointeeType, Value *Ptr,
910 ArrayRef<Value *> IdxList,
911 const Twine &NameStr,
912 BasicBlock *InsertAtEnd) {
913 GetElementPtrInst *GEP =
914 Create(PointeeType, Ptr, IdxList, NameStr, InsertAtEnd);
915 GEP->setIsInBounds(true);
919 /// Transparently provide more efficient getOperand methods.
920 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
922 // getType - Overload to return most specific sequential type.
923 SequentialType *getType() const {
924 return cast<SequentialType>(Instruction::getType());
927 Type *getSourceElementType() const { return SourceElementType; }
929 void setSourceElementType(Type *Ty) { SourceElementType = Ty; }
930 void setResultElementType(Type *Ty) { ResultElementType = Ty; }
932 Type *getResultElementType() const {
933 assert(ResultElementType ==
934 cast<PointerType>(getType()->getScalarType())->getElementType());
935 return ResultElementType;
938 /// \brief Returns the address space of this instruction's pointer type.
939 unsigned getAddressSpace() const {
940 // Note that this is always the same as the pointer operand's address space
941 // and that is cheaper to compute, so cheat here.
942 return getPointerAddressSpace();
945 /// getIndexedType - Returns the type of the element that would be loaded with
946 /// a load instruction with the specified parameters.
948 /// Null is returned if the indices are invalid for the specified
951 static Type *getIndexedType(Type *Ty, ArrayRef<Value *> IdxList);
952 static Type *getIndexedType(Type *Ty, ArrayRef<Constant *> IdxList);
953 static Type *getIndexedType(Type *Ty, ArrayRef<uint64_t> IdxList);
955 inline op_iterator idx_begin() { return op_begin()+1; }
956 inline const_op_iterator idx_begin() const { return op_begin()+1; }
957 inline op_iterator idx_end() { return op_end(); }
958 inline const_op_iterator idx_end() const { return op_end(); }
960 Value *getPointerOperand() {
961 return getOperand(0);
963 const Value *getPointerOperand() const {
964 return getOperand(0);
966 static unsigned getPointerOperandIndex() {
967 return 0U; // get index for modifying correct operand.
970 /// getPointerOperandType - Method to return the pointer operand as a
972 Type *getPointerOperandType() const {
973 return getPointerOperand()->getType();
976 /// \brief Returns the address space of the pointer operand.
977 unsigned getPointerAddressSpace() const {
978 return getPointerOperandType()->getPointerAddressSpace();
981 /// GetGEPReturnType - Returns the pointer type returned by the GEP
982 /// instruction, which may be a vector of pointers.
983 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
984 return getGEPReturnType(
985 cast<PointerType>(Ptr->getType()->getScalarType())->getElementType(),
988 static Type *getGEPReturnType(Type *ElTy, Value *Ptr,
989 ArrayRef<Value *> IdxList) {
990 Type *PtrTy = PointerType::get(checkGEPType(getIndexedType(ElTy, IdxList)),
991 Ptr->getType()->getPointerAddressSpace());
993 if (Ptr->getType()->isVectorTy()) {
994 unsigned NumElem = Ptr->getType()->getVectorNumElements();
995 return VectorType::get(PtrTy, NumElem);
997 for (Value *Index : IdxList)
998 if (Index->getType()->isVectorTy()) {
999 unsigned NumElem = Index->getType()->getVectorNumElements();
1000 return VectorType::get(PtrTy, NumElem);
1006 unsigned getNumIndices() const { // Note: always non-negative
1007 return getNumOperands() - 1;
1010 bool hasIndices() const {
1011 return getNumOperands() > 1;
1014 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
1015 /// zeros. If so, the result pointer and the first operand have the same
1016 /// value, just potentially different types.
1017 bool hasAllZeroIndices() const;
1019 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
1020 /// constant integers. If so, the result pointer and the first operand have
1021 /// a constant offset between them.
1022 bool hasAllConstantIndices() const;
1024 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
1025 /// See LangRef.html for the meaning of inbounds on a getelementptr.
1026 void setIsInBounds(bool b = true);
1028 /// isInBounds - Determine whether the GEP has the inbounds flag.
1029 bool isInBounds() const;
1031 /// \brief Accumulate the constant address offset of this GEP if possible.
1033 /// This routine accepts an APInt into which it will accumulate the constant
1034 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
1035 /// all-constant, it returns false and the value of the offset APInt is
1036 /// undefined (it is *not* preserved!). The APInt passed into this routine
1037 /// must be at least as wide as the IntPtr type for the address space of
1038 /// the base GEP pointer.
1039 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
1041 // Methods for support type inquiry through isa, cast, and dyn_cast:
1042 static inline bool classof(const Instruction *I) {
1043 return (I->getOpcode() == Instruction::GetElementPtr);
1045 static inline bool classof(const Value *V) {
1046 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1051 struct OperandTraits<GetElementPtrInst> :
1052 public VariadicOperandTraits<GetElementPtrInst, 1> {
1055 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1056 ArrayRef<Value *> IdxList, unsigned Values,
1057 const Twine &NameStr,
1058 Instruction *InsertBefore)
1059 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1060 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1061 Values, InsertBefore),
1062 SourceElementType(PointeeType),
1063 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1064 assert(ResultElementType ==
1065 cast<PointerType>(getType()->getScalarType())->getElementType());
1066 init(Ptr, IdxList, NameStr);
1068 GetElementPtrInst::GetElementPtrInst(Type *PointeeType, Value *Ptr,
1069 ArrayRef<Value *> IdxList, unsigned Values,
1070 const Twine &NameStr,
1071 BasicBlock *InsertAtEnd)
1072 : Instruction(getGEPReturnType(PointeeType, Ptr, IdxList), GetElementPtr,
1073 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
1074 Values, InsertAtEnd),
1075 SourceElementType(PointeeType),
1076 ResultElementType(getIndexedType(PointeeType, IdxList)) {
1077 assert(ResultElementType ==
1078 cast<PointerType>(getType()->getScalarType())->getElementType());
1079 init(Ptr, IdxList, NameStr);
1082 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
1084 //===----------------------------------------------------------------------===//
1086 //===----------------------------------------------------------------------===//
1088 /// This instruction compares its operands according to the predicate given
1089 /// to the constructor. It only operates on integers or pointers. The operands
1090 /// must be identical types.
1091 /// \brief Represent an integer comparison operator.
1092 class ICmpInst: public CmpInst {
1093 void anchor() override;
1096 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
1097 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
1098 "Invalid ICmp predicate value");
1099 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1100 "Both operands to ICmp instruction are not of the same type!");
1101 // Check that the operands are the right type
1102 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
1103 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
1104 "Invalid operand types for ICmp instruction");
1108 // Note: Instruction needs to be a friend here to call cloneImpl.
1109 friend class Instruction;
1110 /// \brief Clone an identical ICmpInst
1111 ICmpInst *cloneImpl() const;
1114 /// \brief Constructor with insert-before-instruction semantics.
1116 Instruction *InsertBefore, ///< Where to insert
1117 Predicate pred, ///< The predicate to use for the comparison
1118 Value *LHS, ///< The left-hand-side of the expression
1119 Value *RHS, ///< The right-hand-side of the expression
1120 const Twine &NameStr = "" ///< Name of the instruction
1121 ) : CmpInst(makeCmpResultType(LHS->getType()),
1122 Instruction::ICmp, pred, LHS, RHS, NameStr,
1129 /// \brief Constructor with insert-at-end semantics.
1131 BasicBlock &InsertAtEnd, ///< Block to insert into.
1132 Predicate pred, ///< The predicate to use for the comparison
1133 Value *LHS, ///< The left-hand-side of the expression
1134 Value *RHS, ///< The right-hand-side of the expression
1135 const Twine &NameStr = "" ///< Name of the instruction
1136 ) : CmpInst(makeCmpResultType(LHS->getType()),
1137 Instruction::ICmp, pred, LHS, RHS, NameStr,
1144 /// \brief Constructor with no-insertion semantics
1146 Predicate pred, ///< The predicate to use for the comparison
1147 Value *LHS, ///< The left-hand-side of the expression
1148 Value *RHS, ///< The right-hand-side of the expression
1149 const Twine &NameStr = "" ///< Name of the instruction
1150 ) : CmpInst(makeCmpResultType(LHS->getType()),
1151 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1157 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1158 /// @returns the predicate that would be the result if the operand were
1159 /// regarded as signed.
1160 /// \brief Return the signed version of the predicate
1161 Predicate getSignedPredicate() const {
1162 return getSignedPredicate(getPredicate());
1165 /// This is a static version that you can use without an instruction.
1166 /// \brief Return the signed version of the predicate.
1167 static Predicate getSignedPredicate(Predicate pred);
1169 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1170 /// @returns the predicate that would be the result if the operand were
1171 /// regarded as unsigned.
1172 /// \brief Return the unsigned version of the predicate
1173 Predicate getUnsignedPredicate() const {
1174 return getUnsignedPredicate(getPredicate());
1177 /// This is a static version that you can use without an instruction.
1178 /// \brief Return the unsigned version of the predicate.
1179 static Predicate getUnsignedPredicate(Predicate pred);
1181 /// isEquality - Return true if this predicate is either EQ or NE. This also
1182 /// tests for commutativity.
1183 static bool isEquality(Predicate P) {
1184 return P == ICMP_EQ || P == ICMP_NE;
1187 /// isEquality - Return true if this predicate is either EQ or NE. This also
1188 /// tests for commutativity.
1189 bool isEquality() const {
1190 return isEquality(getPredicate());
1193 /// @returns true if the predicate of this ICmpInst is commutative
1194 /// \brief Determine if this relation is commutative.
1195 bool isCommutative() const { return isEquality(); }
1197 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1199 bool isRelational() const {
1200 return !isEquality();
1203 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1205 static bool isRelational(Predicate P) {
1206 return !isEquality(P);
1209 /// Initialize a set of values that all satisfy the predicate with C.
1210 /// \brief Make a ConstantRange for a relation with a constant value.
1211 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1213 /// Exchange the two operands to this instruction in such a way that it does
1214 /// not modify the semantics of the instruction. The predicate value may be
1215 /// changed to retain the same result if the predicate is order dependent
1217 /// \brief Swap operands and adjust predicate.
1218 void swapOperands() {
1219 setPredicate(getSwappedPredicate());
1220 Op<0>().swap(Op<1>());
1223 // Methods for support type inquiry through isa, cast, and dyn_cast:
1224 static inline bool classof(const Instruction *I) {
1225 return I->getOpcode() == Instruction::ICmp;
1227 static inline bool classof(const Value *V) {
1228 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1232 //===----------------------------------------------------------------------===//
1234 //===----------------------------------------------------------------------===//
1236 /// This instruction compares its operands according to the predicate given
1237 /// to the constructor. It only operates on floating point values or packed
1238 /// vectors of floating point values. The operands must be identical types.
1239 /// \brief Represents a floating point comparison operator.
1240 class FCmpInst: public CmpInst {
1242 // Note: Instruction needs to be a friend here to call cloneImpl.
1243 friend class Instruction;
1244 /// \brief Clone an identical FCmpInst
1245 FCmpInst *cloneImpl() const;
1248 /// \brief Constructor with insert-before-instruction semantics.
1250 Instruction *InsertBefore, ///< Where to insert
1251 Predicate pred, ///< The predicate to use for the comparison
1252 Value *LHS, ///< The left-hand-side of the expression
1253 Value *RHS, ///< The right-hand-side of the expression
1254 const Twine &NameStr = "" ///< Name of the instruction
1255 ) : CmpInst(makeCmpResultType(LHS->getType()),
1256 Instruction::FCmp, pred, LHS, RHS, NameStr,
1258 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1259 "Invalid FCmp predicate value");
1260 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1261 "Both operands to FCmp instruction are not of the same type!");
1262 // Check that the operands are the right type
1263 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1264 "Invalid operand types for FCmp instruction");
1267 /// \brief Constructor with insert-at-end semantics.
1269 BasicBlock &InsertAtEnd, ///< Block to insert into.
1270 Predicate pred, ///< The predicate to use for the comparison
1271 Value *LHS, ///< The left-hand-side of the expression
1272 Value *RHS, ///< The right-hand-side of the expression
1273 const Twine &NameStr = "" ///< Name of the instruction
1274 ) : CmpInst(makeCmpResultType(LHS->getType()),
1275 Instruction::FCmp, pred, LHS, RHS, NameStr,
1277 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1278 "Invalid FCmp predicate value");
1279 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1280 "Both operands to FCmp instruction are not of the same type!");
1281 // Check that the operands are the right type
1282 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1283 "Invalid operand types for FCmp instruction");
1286 /// \brief Constructor with no-insertion semantics
1288 Predicate pred, ///< The predicate to use for the comparison
1289 Value *LHS, ///< The left-hand-side of the expression
1290 Value *RHS, ///< The right-hand-side of the expression
1291 const Twine &NameStr = "" ///< Name of the instruction
1292 ) : CmpInst(makeCmpResultType(LHS->getType()),
1293 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1294 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1295 "Invalid FCmp predicate value");
1296 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1297 "Both operands to FCmp instruction are not of the same type!");
1298 // Check that the operands are the right type
1299 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1300 "Invalid operand types for FCmp instruction");
1303 /// @returns true if the predicate of this instruction is EQ or NE.
1304 /// \brief Determine if this is an equality predicate.
1305 static bool isEquality(Predicate Pred) {
1306 return Pred == FCMP_OEQ || Pred == FCMP_ONE || Pred == FCMP_UEQ ||
1310 /// @returns true if the predicate of this instruction is EQ or NE.
1311 /// \brief Determine if this is an equality predicate.
1312 bool isEquality() const { return isEquality(getPredicate()); }
1314 /// @returns true if the predicate of this instruction is commutative.
1315 /// \brief Determine if this is a commutative predicate.
1316 bool isCommutative() const {
1317 return isEquality() ||
1318 getPredicate() == FCMP_FALSE ||
1319 getPredicate() == FCMP_TRUE ||
1320 getPredicate() == FCMP_ORD ||
1321 getPredicate() == FCMP_UNO;
1324 /// @returns true if the predicate is relational (not EQ or NE).
1325 /// \brief Determine if this a relational predicate.
1326 bool isRelational() const { return !isEquality(); }
1328 /// Exchange the two operands to this instruction in such a way that it does
1329 /// not modify the semantics of the instruction. The predicate value may be
1330 /// changed to retain the same result if the predicate is order dependent
1332 /// \brief Swap operands and adjust predicate.
1333 void swapOperands() {
1334 setPredicate(getSwappedPredicate());
1335 Op<0>().swap(Op<1>());
1338 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1339 static inline bool classof(const Instruction *I) {
1340 return I->getOpcode() == Instruction::FCmp;
1342 static inline bool classof(const Value *V) {
1343 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1347 //===----------------------------------------------------------------------===//
1348 /// CallInst - This class represents a function call, abstracting a target
1349 /// machine's calling convention. This class uses low bit of the SubClassData
1350 /// field to indicate whether or not this is a tail call. The rest of the bits
1351 /// hold the calling convention of the call.
1353 class CallInst : public Instruction,
1354 public OperandBundleUser<CallInst, User::op_iterator> {
1355 AttributeSet AttributeList; ///< parameter attributes for call
1357 CallInst(const CallInst &CI);
1358 void init(Value *Func, ArrayRef<Value *> Args,
1359 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr) {
1360 init(cast<FunctionType>(
1361 cast<PointerType>(Func->getType())->getElementType()),
1362 Func, Args, Bundles, NameStr);
1364 void init(FunctionType *FTy, Value *Func, ArrayRef<Value *> Args,
1365 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
1366 void init(Value *Func, const Twine &NameStr);
1368 /// Construct a CallInst given a range of arguments.
1369 /// \brief Construct a CallInst from a range of arguments
1370 inline CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1371 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1372 Instruction *InsertBefore);
1373 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1374 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1375 Instruction *InsertBefore)
1376 : CallInst(cast<FunctionType>(
1377 cast<PointerType>(Func->getType())->getElementType()),
1378 Func, Args, Bundles, NameStr, InsertBefore) {}
1380 inline CallInst(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr,
1381 Instruction *InsertBefore)
1382 : CallInst(Func, Args, None, NameStr, InsertBefore) {}
1384 /// Construct a CallInst given a range of arguments.
1385 /// \brief Construct a CallInst from a range of arguments
1386 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1387 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1388 BasicBlock *InsertAtEnd);
1390 explicit CallInst(Value *F, const Twine &NameStr,
1391 Instruction *InsertBefore);
1392 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1394 friend class OperandBundleUser<CallInst, User::op_iterator>;
1395 bool hasDescriptor() const { return HasDescriptor; }
1398 // Note: Instruction needs to be a friend here to call cloneImpl.
1399 friend class Instruction;
1400 CallInst *cloneImpl() const;
1403 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1404 ArrayRef<OperandBundleDef> Bundles = None,
1405 const Twine &NameStr = "",
1406 Instruction *InsertBefore = nullptr) {
1407 return Create(cast<FunctionType>(
1408 cast<PointerType>(Func->getType())->getElementType()),
1409 Func, Args, Bundles, NameStr, InsertBefore);
1411 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1412 const Twine &NameStr,
1413 Instruction *InsertBefore = nullptr) {
1414 return Create(cast<FunctionType>(
1415 cast<PointerType>(Func->getType())->getElementType()),
1416 Func, Args, None, NameStr, InsertBefore);
1418 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1419 const Twine &NameStr,
1420 Instruction *InsertBefore = nullptr) {
1421 return new (unsigned(Args.size() + 1))
1422 CallInst(Ty, Func, Args, None, NameStr, InsertBefore);
1424 static CallInst *Create(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1425 ArrayRef<OperandBundleDef> Bundles = None,
1426 const Twine &NameStr = "",
1427 Instruction *InsertBefore = nullptr) {
1428 const unsigned TotalOps =
1429 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1430 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1432 return new (TotalOps, DescriptorBytes)
1433 CallInst(Ty, Func, Args, Bundles, NameStr, InsertBefore);
1435 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1436 ArrayRef<OperandBundleDef> Bundles,
1437 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1438 const unsigned TotalOps =
1439 unsigned(Args.size()) + CountBundleInputs(Bundles) + 1;
1440 const unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
1442 return new (TotalOps, DescriptorBytes)
1443 CallInst(Func, Args, Bundles, NameStr, InsertAtEnd);
1445 static CallInst *Create(Value *Func, ArrayRef<Value *> Args,
1446 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1447 return new (unsigned(Args.size() + 1))
1448 CallInst(Func, Args, None, NameStr, InsertAtEnd);
1450 static CallInst *Create(Value *F, const Twine &NameStr = "",
1451 Instruction *InsertBefore = nullptr) {
1452 return new(1) CallInst(F, NameStr, InsertBefore);
1454 static CallInst *Create(Value *F, const Twine &NameStr,
1455 BasicBlock *InsertAtEnd) {
1456 return new(1) CallInst(F, NameStr, InsertAtEnd);
1459 /// \brief Create a clone of \p CI with a different set of operand bundles and
1460 /// insert it before \p InsertPt.
1462 /// The returned call instruction is identical \p CI in every way except that
1463 /// the operand bundles for the new instruction are set to the operand bundles
1465 static CallInst *Create(CallInst *CI, ArrayRef<OperandBundleDef> Bundles,
1466 Instruction *InsertPt = nullptr);
1468 /// CreateMalloc - Generate the IR for a call to malloc:
1469 /// 1. Compute the malloc call's argument as the specified type's size,
1470 /// possibly multiplied by the array size if the array size is not
1472 /// 2. Call malloc with that argument.
1473 /// 3. Bitcast the result of the malloc call to the specified type.
1474 static Instruction *CreateMalloc(Instruction *InsertBefore,
1475 Type *IntPtrTy, Type *AllocTy,
1476 Value *AllocSize, Value *ArraySize = nullptr,
1477 Function* MallocF = nullptr,
1478 const Twine &Name = "");
1479 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1480 Type *IntPtrTy, Type *AllocTy,
1481 Value *AllocSize, Value *ArraySize = nullptr,
1482 Function* MallocF = nullptr,
1483 const Twine &Name = "");
1484 static Instruction *CreateMalloc(Instruction *InsertBefore,
1485 Type *IntPtrTy, Type *AllocTy,
1486 Value *AllocSize, Value *ArraySize = nullptr,
1487 ArrayRef<OperandBundleDef> Bundles = None,
1488 Function* MallocF = nullptr,
1489 const Twine &Name = "");
1490 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1491 Type *IntPtrTy, Type *AllocTy,
1492 Value *AllocSize, Value *ArraySize = nullptr,
1493 ArrayRef<OperandBundleDef> Bundles = None,
1494 Function* MallocF = nullptr,
1495 const Twine &Name = "");
1496 /// CreateFree - Generate the IR for a call to the builtin free function.
1497 static Instruction *CreateFree(Value *Source,
1498 Instruction *InsertBefore);
1499 static Instruction *CreateFree(Value *Source,
1500 BasicBlock *InsertAtEnd);
1501 static Instruction *CreateFree(Value *Source,
1502 ArrayRef<OperandBundleDef> Bundles,
1503 Instruction *InsertBefore);
1504 static Instruction *CreateFree(Value *Source,
1505 ArrayRef<OperandBundleDef> Bundles,
1506 BasicBlock *InsertAtEnd);
1508 ~CallInst() override;
1510 FunctionType *getFunctionType() const { return FTy; }
1512 void mutateFunctionType(FunctionType *FTy) {
1513 mutateType(FTy->getReturnType());
1517 // Note that 'musttail' implies 'tail'.
1518 enum TailCallKind { TCK_None = 0, TCK_Tail = 1, TCK_MustTail = 2,
1520 TailCallKind getTailCallKind() const {
1521 return TailCallKind(getSubclassDataFromInstruction() & 3);
1523 bool isTailCall() const {
1524 unsigned Kind = getSubclassDataFromInstruction() & 3;
1525 return Kind == TCK_Tail || Kind == TCK_MustTail;
1527 bool isMustTailCall() const {
1528 return (getSubclassDataFromInstruction() & 3) == TCK_MustTail;
1530 bool isNoTailCall() const {
1531 return (getSubclassDataFromInstruction() & 3) == TCK_NoTail;
1533 void setTailCall(bool isTC = true) {
1534 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1535 unsigned(isTC ? TCK_Tail : TCK_None));
1537 void setTailCallKind(TailCallKind TCK) {
1538 setInstructionSubclassData((getSubclassDataFromInstruction() & ~3) |
1542 /// Provide fast operand accessors
1543 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1545 /// getNumArgOperands - Return the number of call arguments.
1547 unsigned getNumArgOperands() const {
1548 return getNumOperands() - getNumTotalBundleOperands() - 1;
1551 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1553 Value *getArgOperand(unsigned i) const {
1554 assert(i < getNumArgOperands() && "Out of bounds!");
1555 return getOperand(i);
1557 void setArgOperand(unsigned i, Value *v) {
1558 assert(i < getNumArgOperands() && "Out of bounds!");
1562 /// \brief Return the iterator pointing to the beginning of the argument list.
1563 op_iterator arg_begin() { return op_begin(); }
1565 /// \brief Return the iterator pointing to the end of the argument list.
1566 op_iterator arg_end() {
1567 // [ call args ], [ operand bundles ], callee
1568 return op_end() - getNumTotalBundleOperands() - 1;
1571 /// \brief Iteration adapter for range-for loops.
1572 iterator_range<op_iterator> arg_operands() {
1573 return make_range(arg_begin(), arg_end());
1576 /// \brief Return the iterator pointing to the beginning of the argument list.
1577 const_op_iterator arg_begin() const { return op_begin(); }
1579 /// \brief Return the iterator pointing to the end of the argument list.
1580 const_op_iterator arg_end() const {
1581 // [ call args ], [ operand bundles ], callee
1582 return op_end() - getNumTotalBundleOperands() - 1;
1585 /// \brief Iteration adapter for range-for loops.
1586 iterator_range<const_op_iterator> arg_operands() const {
1587 return make_range(arg_begin(), arg_end());
1590 /// \brief Wrappers for getting the \c Use of a call argument.
1591 const Use &getArgOperandUse(unsigned i) const {
1592 assert(i < getNumArgOperands() && "Out of bounds!");
1593 return getOperandUse(i);
1595 Use &getArgOperandUse(unsigned i) {
1596 assert(i < getNumArgOperands() && "Out of bounds!");
1597 return getOperandUse(i);
1600 /// If one of the arguments has the 'returned' attribute, return its
1601 /// operand value. Otherwise, return nullptr.
1602 Value *getReturnedArgOperand() const;
1604 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1606 CallingConv::ID getCallingConv() const {
1607 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 2);
1609 void setCallingConv(CallingConv::ID CC) {
1610 auto ID = static_cast<unsigned>(CC);
1611 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
1612 setInstructionSubclassData((getSubclassDataFromInstruction() & 3) |
1616 /// getAttributes - Return the parameter attributes for this call.
1618 const AttributeSet &getAttributes() const { return AttributeList; }
1620 /// setAttributes - Set the parameter attributes for this call.
1622 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1624 /// addAttribute - adds the attribute to the list of attributes.
1625 void addAttribute(unsigned i, Attribute::AttrKind Kind);
1627 /// addAttribute - adds the attribute to the list of attributes.
1628 void addAttribute(unsigned i, StringRef Kind, StringRef Value);
1630 /// addAttribute - adds the attribute to the list of attributes.
1631 void addAttribute(unsigned i, Attribute Attr);
1633 /// removeAttribute - removes the attribute from the list of attributes.
1634 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
1636 /// removeAttribute - removes the attribute from the list of attributes.
1637 void removeAttribute(unsigned i, StringRef Kind);
1639 /// removeAttribute - removes the attribute from the list of attributes.
1640 void removeAttribute(unsigned i, Attribute Attr);
1642 /// \brief adds the dereferenceable attribute to the list of attributes.
1643 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
1645 /// \brief adds the dereferenceable_or_null attribute to the list of
1647 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
1649 /// \brief Determine whether this call has the given attribute.
1650 bool hasFnAttr(Attribute::AttrKind Kind) const {
1651 assert(Kind != Attribute::NoBuiltin &&
1652 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1653 return hasFnAttrImpl(Kind);
1656 /// \brief Determine whether this call has the given attribute.
1657 bool hasFnAttr(StringRef Kind) const {
1658 return hasFnAttrImpl(Kind);
1661 /// \brief Determine whether the call or the callee has the given attributes.
1662 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const;
1664 /// \brief Get the attribute of a given kind at a position.
1665 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const;
1667 /// \brief Get the attribute of a given kind at a position.
1668 Attribute getAttribute(unsigned i, StringRef Kind) const;
1670 /// \brief Return true if the data operand at index \p i has the attribute \p
1673 /// Data operands include call arguments and values used in operand bundles,
1674 /// but does not include the callee operand. This routine dispatches to the
1675 /// underlying AttributeList or the OperandBundleUser as appropriate.
1677 /// The index \p i is interpreted as
1679 /// \p i == Attribute::ReturnIndex -> the return value
1680 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
1681 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
1682 /// (\p i - 1) in the operand list.
1683 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const;
1685 /// \brief Extract the alignment for a call or parameter (0=unknown).
1686 unsigned getParamAlignment(unsigned i) const {
1687 return AttributeList.getParamAlignment(i);
1690 /// \brief Extract the number of dereferenceable bytes for a call or
1691 /// parameter (0=unknown).
1692 uint64_t getDereferenceableBytes(unsigned i) const {
1693 return AttributeList.getDereferenceableBytes(i);
1696 /// \brief Extract the number of dereferenceable_or_null bytes for a call or
1697 /// parameter (0=unknown).
1698 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
1699 return AttributeList.getDereferenceableOrNullBytes(i);
1702 /// @brief Determine if the parameter or return value is marked with NoAlias
1704 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
1705 bool doesNotAlias(unsigned n) const {
1706 return AttributeList.hasAttribute(n, Attribute::NoAlias);
1709 /// \brief Return true if the call should not be treated as a call to a
1711 bool isNoBuiltin() const {
1712 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1713 !hasFnAttrImpl(Attribute::Builtin);
1716 /// \brief Return true if the call should not be inlined.
1717 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1718 void setIsNoInline() {
1719 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1722 /// \brief Return true if the call can return twice
1723 bool canReturnTwice() const {
1724 return hasFnAttr(Attribute::ReturnsTwice);
1726 void setCanReturnTwice() {
1727 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1730 /// \brief Determine if the call does not access memory.
1731 bool doesNotAccessMemory() const {
1732 return hasFnAttr(Attribute::ReadNone);
1734 void setDoesNotAccessMemory() {
1735 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1738 /// \brief Determine if the call does not access or only reads memory.
1739 bool onlyReadsMemory() const {
1740 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1742 void setOnlyReadsMemory() {
1743 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1746 /// \brief Determine if the call does not access or only writes memory.
1747 bool doesNotReadMemory() const {
1748 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
1750 void setDoesNotReadMemory() {
1751 addAttribute(AttributeSet::FunctionIndex, Attribute::WriteOnly);
1754 /// @brief Determine if the call can access memmory only using pointers based
1755 /// on its arguments.
1756 bool onlyAccessesArgMemory() const {
1757 return hasFnAttr(Attribute::ArgMemOnly);
1759 void setOnlyAccessesArgMemory() {
1760 addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
1763 /// \brief Determine if the call cannot return.
1764 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1765 void setDoesNotReturn() {
1766 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1769 /// \brief Determine if the call cannot unwind.
1770 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1771 void setDoesNotThrow() {
1772 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1775 /// \brief Determine if the call cannot be duplicated.
1776 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1777 void setCannotDuplicate() {
1778 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1781 /// \brief Determine if the call is convergent
1782 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
1783 void setConvergent() {
1784 addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
1786 void setNotConvergent() {
1787 removeAttribute(AttributeSet::FunctionIndex,
1788 Attribute::get(getContext(), Attribute::Convergent));
1791 /// \brief Determine if the call returns a structure through first
1792 /// pointer argument.
1793 bool hasStructRetAttr() const {
1794 if (getNumArgOperands() == 0)
1797 // Be friendly and also check the callee.
1798 return paramHasAttr(1, Attribute::StructRet);
1801 /// \brief Determine if any call argument is an aggregate passed by value.
1802 bool hasByValArgument() const {
1803 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1806 /// getCalledFunction - Return the function called, or null if this is an
1807 /// indirect function invocation.
1809 Function *getCalledFunction() const {
1810 return dyn_cast<Function>(Op<-1>());
1813 /// getCalledValue - Get a pointer to the function that is invoked by this
1815 const Value *getCalledValue() const { return Op<-1>(); }
1816 Value *getCalledValue() { return Op<-1>(); }
1818 /// setCalledFunction - Set the function called.
1819 void setCalledFunction(Value* Fn) {
1821 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
1824 void setCalledFunction(FunctionType *FTy, Value *Fn) {
1826 assert(FTy == cast<FunctionType>(
1827 cast<PointerType>(Fn->getType())->getElementType()));
1831 /// isInlineAsm - Check if this call is an inline asm statement.
1832 bool isInlineAsm() const {
1833 return isa<InlineAsm>(Op<-1>());
1836 // Methods for support type inquiry through isa, cast, and dyn_cast:
1837 static inline bool classof(const Instruction *I) {
1838 return I->getOpcode() == Instruction::Call;
1840 static inline bool classof(const Value *V) {
1841 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1845 template <typename AttrKind> bool hasFnAttrImpl(AttrKind A) const {
1846 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
1849 // Operand bundles override attributes on the called function, but don't
1850 // override attributes directly present on the call instruction.
1851 if (isFnAttrDisallowedByOpBundle(A))
1854 if (const Function *F = getCalledFunction())
1855 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
1859 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1860 // method so that subclasses cannot accidentally use it.
1861 void setInstructionSubclassData(unsigned short D) {
1862 Instruction::setInstructionSubclassData(D);
1867 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1870 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1871 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1872 BasicBlock *InsertAtEnd)
1874 cast<FunctionType>(cast<PointerType>(Func->getType())
1875 ->getElementType())->getReturnType(),
1876 Instruction::Call, OperandTraits<CallInst>::op_end(this) -
1877 (Args.size() + CountBundleInputs(Bundles) + 1),
1878 unsigned(Args.size() + CountBundleInputs(Bundles) + 1), InsertAtEnd) {
1879 init(Func, Args, Bundles, NameStr);
1882 CallInst::CallInst(FunctionType *Ty, Value *Func, ArrayRef<Value *> Args,
1883 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr,
1884 Instruction *InsertBefore)
1885 : Instruction(Ty->getReturnType(), Instruction::Call,
1886 OperandTraits<CallInst>::op_end(this) -
1887 (Args.size() + CountBundleInputs(Bundles) + 1),
1888 unsigned(Args.size() + CountBundleInputs(Bundles) + 1),
1890 init(Ty, Func, Args, Bundles, NameStr);
1893 // Note: if you get compile errors about private methods then
1894 // please update your code to use the high-level operand
1895 // interfaces. See line 943 above.
1896 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1898 //===----------------------------------------------------------------------===//
1900 //===----------------------------------------------------------------------===//
1902 /// SelectInst - This class represents the LLVM 'select' instruction.
1904 class SelectInst : public Instruction {
1905 void init(Value *C, Value *S1, Value *S2) {
1906 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1912 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1913 Instruction *InsertBefore)
1914 : Instruction(S1->getType(), Instruction::Select,
1915 &Op<0>(), 3, InsertBefore) {
1919 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1920 BasicBlock *InsertAtEnd)
1921 : Instruction(S1->getType(), Instruction::Select,
1922 &Op<0>(), 3, InsertAtEnd) {
1928 // Note: Instruction needs to be a friend here to call cloneImpl.
1929 friend class Instruction;
1930 SelectInst *cloneImpl() const;
1933 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1934 const Twine &NameStr = "",
1935 Instruction *InsertBefore = nullptr) {
1936 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1938 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1939 const Twine &NameStr,
1940 BasicBlock *InsertAtEnd) {
1941 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1944 const Value *getCondition() const { return Op<0>(); }
1945 const Value *getTrueValue() const { return Op<1>(); }
1946 const Value *getFalseValue() const { return Op<2>(); }
1947 Value *getCondition() { return Op<0>(); }
1948 Value *getTrueValue() { return Op<1>(); }
1949 Value *getFalseValue() { return Op<2>(); }
1951 void setCondition(Value *V) { Op<0>() = V; }
1952 void setTrueValue(Value *V) { Op<1>() = V; }
1953 void setFalseValue(Value *V) { Op<2>() = V; }
1955 /// areInvalidOperands - Return a string if the specified operands are invalid
1956 /// for a select operation, otherwise return null.
1957 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1959 /// Transparently provide more efficient getOperand methods.
1960 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1962 OtherOps getOpcode() const {
1963 return static_cast<OtherOps>(Instruction::getOpcode());
1966 // Methods for support type inquiry through isa, cast, and dyn_cast:
1967 static inline bool classof(const Instruction *I) {
1968 return I->getOpcode() == Instruction::Select;
1970 static inline bool classof(const Value *V) {
1971 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1976 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1979 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1981 //===----------------------------------------------------------------------===//
1983 //===----------------------------------------------------------------------===//
1985 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1986 /// an argument of the specified type given a va_list and increments that list
1988 class VAArgInst : public UnaryInstruction {
1990 // Note: Instruction needs to be a friend here to call cloneImpl.
1991 friend class Instruction;
1992 VAArgInst *cloneImpl() const;
1995 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1996 Instruction *InsertBefore = nullptr)
1997 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
2000 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
2001 BasicBlock *InsertAtEnd)
2002 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
2006 Value *getPointerOperand() { return getOperand(0); }
2007 const Value *getPointerOperand() const { return getOperand(0); }
2008 static unsigned getPointerOperandIndex() { return 0U; }
2010 // Methods for support type inquiry through isa, cast, and dyn_cast:
2011 static inline bool classof(const Instruction *I) {
2012 return I->getOpcode() == VAArg;
2014 static inline bool classof(const Value *V) {
2015 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2019 //===----------------------------------------------------------------------===//
2020 // ExtractElementInst Class
2021 //===----------------------------------------------------------------------===//
2023 /// ExtractElementInst - This instruction extracts a single (scalar)
2024 /// element from a VectorType value
2026 class ExtractElementInst : public Instruction {
2027 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
2028 Instruction *InsertBefore = nullptr);
2029 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
2030 BasicBlock *InsertAtEnd);
2033 // Note: Instruction needs to be a friend here to call cloneImpl.
2034 friend class Instruction;
2035 ExtractElementInst *cloneImpl() const;
2038 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2039 const Twine &NameStr = "",
2040 Instruction *InsertBefore = nullptr) {
2041 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
2043 static ExtractElementInst *Create(Value *Vec, Value *Idx,
2044 const Twine &NameStr,
2045 BasicBlock *InsertAtEnd) {
2046 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
2049 /// isValidOperands - Return true if an extractelement instruction can be
2050 /// formed with the specified operands.
2051 static bool isValidOperands(const Value *Vec, const Value *Idx);
2053 Value *getVectorOperand() { return Op<0>(); }
2054 Value *getIndexOperand() { return Op<1>(); }
2055 const Value *getVectorOperand() const { return Op<0>(); }
2056 const Value *getIndexOperand() const { return Op<1>(); }
2058 VectorType *getVectorOperandType() const {
2059 return cast<VectorType>(getVectorOperand()->getType());
2062 /// Transparently provide more efficient getOperand methods.
2063 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2065 // Methods for support type inquiry through isa, cast, and dyn_cast:
2066 static inline bool classof(const Instruction *I) {
2067 return I->getOpcode() == Instruction::ExtractElement;
2069 static inline bool classof(const Value *V) {
2070 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2075 struct OperandTraits<ExtractElementInst> :
2076 public FixedNumOperandTraits<ExtractElementInst, 2> {
2079 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
2081 //===----------------------------------------------------------------------===//
2082 // InsertElementInst Class
2083 //===----------------------------------------------------------------------===//
2085 /// InsertElementInst - This instruction inserts a single (scalar)
2086 /// element into a VectorType value
2088 class InsertElementInst : public Instruction {
2089 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
2090 const Twine &NameStr = "",
2091 Instruction *InsertBefore = nullptr);
2092 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx, const Twine &NameStr,
2093 BasicBlock *InsertAtEnd);
2096 // Note: Instruction needs to be a friend here to call cloneImpl.
2097 friend class Instruction;
2098 InsertElementInst *cloneImpl() const;
2101 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2102 const Twine &NameStr = "",
2103 Instruction *InsertBefore = nullptr) {
2104 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
2106 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
2107 const Twine &NameStr,
2108 BasicBlock *InsertAtEnd) {
2109 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
2112 /// isValidOperands - Return true if an insertelement instruction can be
2113 /// formed with the specified operands.
2114 static bool isValidOperands(const Value *Vec, const Value *NewElt,
2117 /// getType - Overload to return most specific vector type.
2119 VectorType *getType() const {
2120 return cast<VectorType>(Instruction::getType());
2123 /// Transparently provide more efficient getOperand methods.
2124 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2126 // Methods for support type inquiry through isa, cast, and dyn_cast:
2127 static inline bool classof(const Instruction *I) {
2128 return I->getOpcode() == Instruction::InsertElement;
2130 static inline bool classof(const Value *V) {
2131 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2136 struct OperandTraits<InsertElementInst> :
2137 public FixedNumOperandTraits<InsertElementInst, 3> {
2140 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
2142 //===----------------------------------------------------------------------===//
2143 // ShuffleVectorInst Class
2144 //===----------------------------------------------------------------------===//
2146 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
2149 class ShuffleVectorInst : public Instruction {
2151 // Note: Instruction needs to be a friend here to call cloneImpl.
2152 friend class Instruction;
2153 ShuffleVectorInst *cloneImpl() const;
2156 // allocate space for exactly three operands
2157 void *operator new(size_t s) {
2158 return User::operator new(s, 3);
2160 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2161 const Twine &NameStr = "",
2162 Instruction *InsertBefor = nullptr);
2163 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
2164 const Twine &NameStr, BasicBlock *InsertAtEnd);
2166 /// isValidOperands - Return true if a shufflevector instruction can be
2167 /// formed with the specified operands.
2168 static bool isValidOperands(const Value *V1, const Value *V2,
2171 /// getType - Overload to return most specific vector type.
2173 VectorType *getType() const {
2174 return cast<VectorType>(Instruction::getType());
2177 /// Transparently provide more efficient getOperand methods.
2178 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2180 Constant *getMask() const {
2181 return cast<Constant>(getOperand(2));
2184 /// getMaskValue - Return the index from the shuffle mask for the specified
2185 /// output result. This is either -1 if the element is undef or a number less
2186 /// than 2*numelements.
2187 static int getMaskValue(Constant *Mask, unsigned i);
2189 int getMaskValue(unsigned i) const {
2190 return getMaskValue(getMask(), i);
2193 /// getShuffleMask - Return the full mask for this instruction, where each
2194 /// element is the element number and undef's are returned as -1.
2195 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
2197 void getShuffleMask(SmallVectorImpl<int> &Result) const {
2198 return getShuffleMask(getMask(), Result);
2201 SmallVector<int, 16> getShuffleMask() const {
2202 SmallVector<int, 16> Mask;
2203 getShuffleMask(Mask);
2207 // Methods for support type inquiry through isa, cast, and dyn_cast:
2208 static inline bool classof(const Instruction *I) {
2209 return I->getOpcode() == Instruction::ShuffleVector;
2211 static inline bool classof(const Value *V) {
2212 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2217 struct OperandTraits<ShuffleVectorInst> :
2218 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
2221 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
2223 //===----------------------------------------------------------------------===//
2224 // ExtractValueInst Class
2225 //===----------------------------------------------------------------------===//
2227 /// ExtractValueInst - This instruction extracts a struct member or array
2228 /// element value from an aggregate value.
2230 class ExtractValueInst : public UnaryInstruction {
2231 SmallVector<unsigned, 4> Indices;
2233 ExtractValueInst(const ExtractValueInst &EVI);
2234 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
2236 /// Constructors - Create a extractvalue instruction with a base aggregate
2237 /// value and a list of indices. The first ctor can optionally insert before
2238 /// an existing instruction, the second appends the new instruction to the
2239 /// specified BasicBlock.
2240 inline ExtractValueInst(Value *Agg,
2241 ArrayRef<unsigned> Idxs,
2242 const Twine &NameStr,
2243 Instruction *InsertBefore);
2244 inline ExtractValueInst(Value *Agg,
2245 ArrayRef<unsigned> Idxs,
2246 const Twine &NameStr, BasicBlock *InsertAtEnd);
2248 // allocate space for exactly one operand
2249 void *operator new(size_t s) { return User::operator new(s, 1); }
2252 // Note: Instruction needs to be a friend here to call cloneImpl.
2253 friend class Instruction;
2254 ExtractValueInst *cloneImpl() const;
2257 static ExtractValueInst *Create(Value *Agg,
2258 ArrayRef<unsigned> Idxs,
2259 const Twine &NameStr = "",
2260 Instruction *InsertBefore = nullptr) {
2262 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
2264 static ExtractValueInst *Create(Value *Agg,
2265 ArrayRef<unsigned> Idxs,
2266 const Twine &NameStr,
2267 BasicBlock *InsertAtEnd) {
2268 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
2271 /// getIndexedType - Returns the type of the element that would be extracted
2272 /// with an extractvalue instruction with the specified parameters.
2274 /// Null is returned if the indices are invalid for the specified type.
2275 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
2277 typedef const unsigned* idx_iterator;
2278 inline idx_iterator idx_begin() const { return Indices.begin(); }
2279 inline idx_iterator idx_end() const { return Indices.end(); }
2280 inline iterator_range<idx_iterator> indices() const {
2281 return make_range(idx_begin(), idx_end());
2284 Value *getAggregateOperand() {
2285 return getOperand(0);
2287 const Value *getAggregateOperand() const {
2288 return getOperand(0);
2290 static unsigned getAggregateOperandIndex() {
2291 return 0U; // get index for modifying correct operand
2294 ArrayRef<unsigned> getIndices() const {
2298 unsigned getNumIndices() const {
2299 return (unsigned)Indices.size();
2302 bool hasIndices() const {
2306 // Methods for support type inquiry through isa, cast, and dyn_cast:
2307 static inline bool classof(const Instruction *I) {
2308 return I->getOpcode() == Instruction::ExtractValue;
2310 static inline bool classof(const Value *V) {
2311 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2315 ExtractValueInst::ExtractValueInst(Value *Agg,
2316 ArrayRef<unsigned> Idxs,
2317 const Twine &NameStr,
2318 Instruction *InsertBefore)
2319 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2320 ExtractValue, Agg, InsertBefore) {
2321 init(Idxs, NameStr);
2323 ExtractValueInst::ExtractValueInst(Value *Agg,
2324 ArrayRef<unsigned> Idxs,
2325 const Twine &NameStr,
2326 BasicBlock *InsertAtEnd)
2327 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
2328 ExtractValue, Agg, InsertAtEnd) {
2329 init(Idxs, NameStr);
2332 //===----------------------------------------------------------------------===//
2333 // InsertValueInst Class
2334 //===----------------------------------------------------------------------===//
2336 /// InsertValueInst - This instruction inserts a struct field of array element
2337 /// value into an aggregate value.
2339 class InsertValueInst : public Instruction {
2340 SmallVector<unsigned, 4> Indices;
2342 void *operator new(size_t, unsigned) = delete;
2343 InsertValueInst(const InsertValueInst &IVI);
2344 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
2345 const Twine &NameStr);
2347 /// Constructors - Create a insertvalue instruction with a base aggregate
2348 /// value, a value to insert, and a list of indices. The first ctor can
2349 /// optionally insert before an existing instruction, the second appends
2350 /// the new instruction to the specified BasicBlock.
2351 inline InsertValueInst(Value *Agg, Value *Val,
2352 ArrayRef<unsigned> Idxs,
2353 const Twine &NameStr,
2354 Instruction *InsertBefore);
2355 inline InsertValueInst(Value *Agg, Value *Val,
2356 ArrayRef<unsigned> Idxs,
2357 const Twine &NameStr, BasicBlock *InsertAtEnd);
2359 /// Constructors - These two constructors are convenience methods because one
2360 /// and two index insertvalue instructions are so common.
2361 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
2362 const Twine &NameStr = "",
2363 Instruction *InsertBefore = nullptr);
2364 InsertValueInst(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr,
2365 BasicBlock *InsertAtEnd);
2368 // Note: Instruction needs to be a friend here to call cloneImpl.
2369 friend class Instruction;
2370 InsertValueInst *cloneImpl() const;
2373 // allocate space for exactly two operands
2374 void *operator new(size_t s) {
2375 return User::operator new(s, 2);
2378 static InsertValueInst *Create(Value *Agg, Value *Val,
2379 ArrayRef<unsigned> Idxs,
2380 const Twine &NameStr = "",
2381 Instruction *InsertBefore = nullptr) {
2382 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
2384 static InsertValueInst *Create(Value *Agg, Value *Val,
2385 ArrayRef<unsigned> Idxs,
2386 const Twine &NameStr,
2387 BasicBlock *InsertAtEnd) {
2388 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
2391 /// Transparently provide more efficient getOperand methods.
2392 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2394 typedef const unsigned* idx_iterator;
2395 inline idx_iterator idx_begin() const { return Indices.begin(); }
2396 inline idx_iterator idx_end() const { return Indices.end(); }
2397 inline iterator_range<idx_iterator> indices() const {
2398 return make_range(idx_begin(), idx_end());
2401 Value *getAggregateOperand() {
2402 return getOperand(0);
2404 const Value *getAggregateOperand() const {
2405 return getOperand(0);
2407 static unsigned getAggregateOperandIndex() {
2408 return 0U; // get index for modifying correct operand
2411 Value *getInsertedValueOperand() {
2412 return getOperand(1);
2414 const Value *getInsertedValueOperand() const {
2415 return getOperand(1);
2417 static unsigned getInsertedValueOperandIndex() {
2418 return 1U; // get index for modifying correct operand
2421 ArrayRef<unsigned> getIndices() const {
2425 unsigned getNumIndices() const {
2426 return (unsigned)Indices.size();
2429 bool hasIndices() const {
2433 // Methods for support type inquiry through isa, cast, and dyn_cast:
2434 static inline bool classof(const Instruction *I) {
2435 return I->getOpcode() == Instruction::InsertValue;
2437 static inline bool classof(const Value *V) {
2438 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2443 struct OperandTraits<InsertValueInst> :
2444 public FixedNumOperandTraits<InsertValueInst, 2> {
2447 InsertValueInst::InsertValueInst(Value *Agg,
2449 ArrayRef<unsigned> Idxs,
2450 const Twine &NameStr,
2451 Instruction *InsertBefore)
2452 : Instruction(Agg->getType(), InsertValue,
2453 OperandTraits<InsertValueInst>::op_begin(this),
2455 init(Agg, Val, Idxs, NameStr);
2457 InsertValueInst::InsertValueInst(Value *Agg,
2459 ArrayRef<unsigned> Idxs,
2460 const Twine &NameStr,
2461 BasicBlock *InsertAtEnd)
2462 : Instruction(Agg->getType(), InsertValue,
2463 OperandTraits<InsertValueInst>::op_begin(this),
2465 init(Agg, Val, Idxs, NameStr);
2468 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2470 //===----------------------------------------------------------------------===//
2472 //===----------------------------------------------------------------------===//
2474 // PHINode - The PHINode class is used to represent the magical mystical PHI
2475 // node, that can not exist in nature, but can be synthesized in a computer
2476 // scientist's overactive imagination.
2478 class PHINode : public Instruction {
2479 void anchor() override;
2481 void *operator new(size_t, unsigned) = delete;
2482 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2483 /// the number actually in use.
2484 unsigned ReservedSpace;
2485 PHINode(const PHINode &PN);
2486 // allocate space for exactly zero operands
2487 void *operator new(size_t s) {
2488 return User::operator new(s);
2490 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2491 const Twine &NameStr = "",
2492 Instruction *InsertBefore = nullptr)
2493 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertBefore),
2494 ReservedSpace(NumReservedValues) {
2496 allocHungoffUses(ReservedSpace);
2499 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2500 BasicBlock *InsertAtEnd)
2501 : Instruction(Ty, Instruction::PHI, nullptr, 0, InsertAtEnd),
2502 ReservedSpace(NumReservedValues) {
2504 allocHungoffUses(ReservedSpace);
2508 // allocHungoffUses - this is more complicated than the generic
2509 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2510 // values and pointers to the incoming blocks, all in one allocation.
2511 void allocHungoffUses(unsigned N) {
2512 User::allocHungoffUses(N, /* IsPhi */ true);
2515 // Note: Instruction needs to be a friend here to call cloneImpl.
2516 friend class Instruction;
2517 PHINode *cloneImpl() const;
2520 /// Constructors - NumReservedValues is a hint for the number of incoming
2521 /// edges that this phi node will have (use 0 if you really have no idea).
2522 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2523 const Twine &NameStr = "",
2524 Instruction *InsertBefore = nullptr) {
2525 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2527 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2528 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2529 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2532 /// Provide fast operand accessors
2533 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2535 // Block iterator interface. This provides access to the list of incoming
2536 // basic blocks, which parallels the list of incoming values.
2538 typedef BasicBlock **block_iterator;
2539 typedef BasicBlock * const *const_block_iterator;
2541 block_iterator block_begin() {
2543 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2544 return reinterpret_cast<block_iterator>(ref + 1);
2547 const_block_iterator block_begin() const {
2548 const Use::UserRef *ref =
2549 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2550 return reinterpret_cast<const_block_iterator>(ref + 1);
2553 block_iterator block_end() {
2554 return block_begin() + getNumOperands();
2557 const_block_iterator block_end() const {
2558 return block_begin() + getNumOperands();
2561 iterator_range<block_iterator> blocks() {
2562 return make_range(block_begin(), block_end());
2565 iterator_range<const_block_iterator> blocks() const {
2566 return make_range(block_begin(), block_end());
2569 op_range incoming_values() { return operands(); }
2571 const_op_range incoming_values() const { return operands(); }
2573 /// getNumIncomingValues - Return the number of incoming edges
2575 unsigned getNumIncomingValues() const { return getNumOperands(); }
2577 /// getIncomingValue - Return incoming value number x
2579 Value *getIncomingValue(unsigned i) const {
2580 return getOperand(i);
2582 void setIncomingValue(unsigned i, Value *V) {
2583 assert(V && "PHI node got a null value!");
2584 assert(getType() == V->getType() &&
2585 "All operands to PHI node must be the same type as the PHI node!");
2588 static unsigned getOperandNumForIncomingValue(unsigned i) {
2591 static unsigned getIncomingValueNumForOperand(unsigned i) {
2595 /// getIncomingBlock - Return incoming basic block number @p i.
2597 BasicBlock *getIncomingBlock(unsigned i) const {
2598 return block_begin()[i];
2601 /// getIncomingBlock - Return incoming basic block corresponding
2602 /// to an operand of the PHI.
2604 BasicBlock *getIncomingBlock(const Use &U) const {
2605 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2606 return getIncomingBlock(unsigned(&U - op_begin()));
2609 /// getIncomingBlock - Return incoming basic block corresponding
2610 /// to value use iterator.
2612 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2613 return getIncomingBlock(I.getUse());
2616 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2617 assert(BB && "PHI node got a null basic block!");
2618 block_begin()[i] = BB;
2621 /// addIncoming - Add an incoming value to the end of the PHI list
2623 void addIncoming(Value *V, BasicBlock *BB) {
2624 if (getNumOperands() == ReservedSpace)
2625 growOperands(); // Get more space!
2626 // Initialize some new operands.
2627 setNumHungOffUseOperands(getNumOperands() + 1);
2628 setIncomingValue(getNumOperands() - 1, V);
2629 setIncomingBlock(getNumOperands() - 1, BB);
2632 /// removeIncomingValue - Remove an incoming value. This is useful if a
2633 /// predecessor basic block is deleted. The value removed is returned.
2635 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2636 /// is true), the PHI node is destroyed and any uses of it are replaced with
2637 /// dummy values. The only time there should be zero incoming values to a PHI
2638 /// node is when the block is dead, so this strategy is sound.
2640 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2642 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2643 int Idx = getBasicBlockIndex(BB);
2644 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2645 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2648 /// getBasicBlockIndex - Return the first index of the specified basic
2649 /// block in the value list for this PHI. Returns -1 if no instance.
2651 int getBasicBlockIndex(const BasicBlock *BB) const {
2652 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2653 if (block_begin()[i] == BB)
2658 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2659 int Idx = getBasicBlockIndex(BB);
2660 assert(Idx >= 0 && "Invalid basic block argument!");
2661 return getIncomingValue(Idx);
2664 /// hasConstantValue - If the specified PHI node always merges together the
2665 /// same value, return the value, otherwise return null.
2666 Value *hasConstantValue() const;
2668 /// hasConstantOrUndefValue - Whether the specified PHI node always merges
2669 /// together the same value, assuming undefs are equal to a unique
2670 /// non-undef value.
2671 bool hasConstantOrUndefValue() const;
2673 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2674 static inline bool classof(const Instruction *I) {
2675 return I->getOpcode() == Instruction::PHI;
2677 static inline bool classof(const Value *V) {
2678 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2682 void growOperands();
2686 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2689 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2691 //===----------------------------------------------------------------------===//
2692 // LandingPadInst Class
2693 //===----------------------------------------------------------------------===//
2695 //===---------------------------------------------------------------------------
2696 /// LandingPadInst - The landingpad instruction holds all of the information
2697 /// necessary to generate correct exception handling. The landingpad instruction
2698 /// cannot be moved from the top of a landing pad block, which itself is
2699 /// accessible only from the 'unwind' edge of an invoke. This uses the
2700 /// SubclassData field in Value to store whether or not the landingpad is a
2703 class LandingPadInst : public Instruction {
2704 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2705 /// the number actually in use.
2706 unsigned ReservedSpace;
2707 LandingPadInst(const LandingPadInst &LP);
2710 enum ClauseType { Catch, Filter };
2713 void *operator new(size_t, unsigned) = delete;
2714 // Allocate space for exactly zero operands.
2715 void *operator new(size_t s) {
2716 return User::operator new(s);
2718 void growOperands(unsigned Size);
2719 void init(unsigned NumReservedValues, const Twine &NameStr);
2721 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2722 const Twine &NameStr, Instruction *InsertBefore);
2723 explicit LandingPadInst(Type *RetTy, unsigned NumReservedValues,
2724 const Twine &NameStr, BasicBlock *InsertAtEnd);
2727 // Note: Instruction needs to be a friend here to call cloneImpl.
2728 friend class Instruction;
2729 LandingPadInst *cloneImpl() const;
2732 /// Constructors - NumReservedClauses is a hint for the number of incoming
2733 /// clauses that this landingpad will have (use 0 if you really have no idea).
2734 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2735 const Twine &NameStr = "",
2736 Instruction *InsertBefore = nullptr);
2737 static LandingPadInst *Create(Type *RetTy, unsigned NumReservedClauses,
2738 const Twine &NameStr, BasicBlock *InsertAtEnd);
2740 /// Provide fast operand accessors
2741 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2743 /// isCleanup - Return 'true' if this landingpad instruction is a
2744 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2745 /// doesn't catch the exception.
2746 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2748 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2749 void setCleanup(bool V) {
2750 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2754 /// Add a catch or filter clause to the landing pad.
2755 void addClause(Constant *ClauseVal);
2757 /// Get the value of the clause at index Idx. Use isCatch/isFilter to
2758 /// determine what type of clause this is.
2759 Constant *getClause(unsigned Idx) const {
2760 return cast<Constant>(getOperandList()[Idx]);
2763 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2764 bool isCatch(unsigned Idx) const {
2765 return !isa<ArrayType>(getOperandList()[Idx]->getType());
2768 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2769 bool isFilter(unsigned Idx) const {
2770 return isa<ArrayType>(getOperandList()[Idx]->getType());
2773 /// getNumClauses - Get the number of clauses for this landing pad.
2774 unsigned getNumClauses() const { return getNumOperands(); }
2776 /// reserveClauses - Grow the size of the operand list to accommodate the new
2777 /// number of clauses.
2778 void reserveClauses(unsigned Size) { growOperands(Size); }
2780 // Methods for support type inquiry through isa, cast, and dyn_cast:
2781 static inline bool classof(const Instruction *I) {
2782 return I->getOpcode() == Instruction::LandingPad;
2784 static inline bool classof(const Value *V) {
2785 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2790 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<1> {
2793 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2795 //===----------------------------------------------------------------------===//
2797 //===----------------------------------------------------------------------===//
2799 //===---------------------------------------------------------------------------
2800 /// ReturnInst - Return a value (possibly void), from a function. Execution
2801 /// does not continue in this function any longer.
2803 class ReturnInst : public TerminatorInst {
2804 ReturnInst(const ReturnInst &RI);
2807 // ReturnInst constructors:
2808 // ReturnInst() - 'ret void' instruction
2809 // ReturnInst( null) - 'ret void' instruction
2810 // ReturnInst(Value* X) - 'ret X' instruction
2811 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2812 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2813 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2814 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2816 // NOTE: If the Value* passed is of type void then the constructor behaves as
2817 // if it was passed NULL.
2818 explicit ReturnInst(LLVMContext &C, Value *retVal = nullptr,
2819 Instruction *InsertBefore = nullptr);
2820 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2821 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2824 // Note: Instruction needs to be a friend here to call cloneImpl.
2825 friend class Instruction;
2826 ReturnInst *cloneImpl() const;
2829 static ReturnInst* Create(LLVMContext &C, Value *retVal = nullptr,
2830 Instruction *InsertBefore = nullptr) {
2831 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2833 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2834 BasicBlock *InsertAtEnd) {
2835 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2837 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2838 return new(0) ReturnInst(C, InsertAtEnd);
2840 ~ReturnInst() override;
2842 /// Provide fast operand accessors
2843 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2845 /// Convenience accessor. Returns null if there is no return value.
2846 Value *getReturnValue() const {
2847 return getNumOperands() != 0 ? getOperand(0) : nullptr;
2850 unsigned getNumSuccessors() const { return 0; }
2852 // Methods for support type inquiry through isa, cast, and dyn_cast:
2853 static inline bool classof(const Instruction *I) {
2854 return (I->getOpcode() == Instruction::Ret);
2856 static inline bool classof(const Value *V) {
2857 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2861 BasicBlock *getSuccessorV(unsigned idx) const override;
2862 unsigned getNumSuccessorsV() const override;
2863 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2867 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2870 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2872 //===----------------------------------------------------------------------===//
2874 //===----------------------------------------------------------------------===//
2876 //===---------------------------------------------------------------------------
2877 /// BranchInst - Conditional or Unconditional Branch instruction.
2879 class BranchInst : public TerminatorInst {
2880 /// Ops list - Branches are strange. The operands are ordered:
2881 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2882 /// they don't have to check for cond/uncond branchness. These are mostly
2883 /// accessed relative from op_end().
2884 BranchInst(const BranchInst &BI);
2886 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2887 // BranchInst(BB *B) - 'br B'
2888 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2889 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2890 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2891 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2892 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2893 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = nullptr);
2894 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2895 Instruction *InsertBefore = nullptr);
2896 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2897 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2898 BasicBlock *InsertAtEnd);
2901 // Note: Instruction needs to be a friend here to call cloneImpl.
2902 friend class Instruction;
2903 BranchInst *cloneImpl() const;
2906 static BranchInst *Create(BasicBlock *IfTrue,
2907 Instruction *InsertBefore = nullptr) {
2908 return new(1) BranchInst(IfTrue, InsertBefore);
2910 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2911 Value *Cond, Instruction *InsertBefore = nullptr) {
2912 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2914 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2915 return new(1) BranchInst(IfTrue, InsertAtEnd);
2917 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2918 Value *Cond, BasicBlock *InsertAtEnd) {
2919 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2922 /// Transparently provide more efficient getOperand methods.
2923 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2925 bool isUnconditional() const { return getNumOperands() == 1; }
2926 bool isConditional() const { return getNumOperands() == 3; }
2928 Value *getCondition() const {
2929 assert(isConditional() && "Cannot get condition of an uncond branch!");
2933 void setCondition(Value *V) {
2934 assert(isConditional() && "Cannot set condition of unconditional branch!");
2938 unsigned getNumSuccessors() const { return 1+isConditional(); }
2940 BasicBlock *getSuccessor(unsigned i) const {
2941 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2942 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2945 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2946 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2947 *(&Op<-1>() - idx) = NewSucc;
2950 /// \brief Swap the successors of this branch instruction.
2952 /// Swaps the successors of the branch instruction. This also swaps any
2953 /// branch weight metadata associated with the instruction so that it
2954 /// continues to map correctly to each operand.
2955 void swapSuccessors();
2957 // Methods for support type inquiry through isa, cast, and dyn_cast:
2958 static inline bool classof(const Instruction *I) {
2959 return (I->getOpcode() == Instruction::Br);
2961 static inline bool classof(const Value *V) {
2962 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2966 BasicBlock *getSuccessorV(unsigned idx) const override;
2967 unsigned getNumSuccessorsV() const override;
2968 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2972 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2975 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2977 //===----------------------------------------------------------------------===//
2979 //===----------------------------------------------------------------------===//
2981 //===---------------------------------------------------------------------------
2984 class SwitchInst : public TerminatorInst {
2985 void *operator new(size_t, unsigned) = delete;
2986 unsigned ReservedSpace;
2987 // Operand[0] = Value to switch on
2988 // Operand[1] = Default basic block destination
2989 // Operand[2n ] = Value to match
2990 // Operand[2n+1] = BasicBlock to go to on match
2991 SwitchInst(const SwitchInst &SI);
2992 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2993 void growOperands();
2994 // allocate space for exactly zero operands
2995 void *operator new(size_t s) {
2996 return User::operator new(s);
2998 /// Create a new switch instruction, specifying a value to switch on and a
2999 /// default destination. The number of additional cases can be specified here
3000 /// to make memory allocation more efficient. This constructor can also
3001 /// auto-insert before another instruction.
3002 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3003 Instruction *InsertBefore);
3005 /// Create a new switch instruction, specifying a value to switch on and a
3006 /// default destination. The number of additional cases can be specified here
3007 /// to make memory allocation more efficient. This constructor also
3008 /// auto-inserts at the end of the specified BasicBlock.
3009 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
3010 BasicBlock *InsertAtEnd);
3013 // Note: Instruction needs to be a friend here to call cloneImpl.
3014 friend class Instruction;
3015 SwitchInst *cloneImpl() const;
3019 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
3021 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
3022 class CaseIteratorT {
3028 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
3030 /// Initializes case iterator for given SwitchInst and for given
3032 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
3037 /// Initializes case iterator for given SwitchInst and for given
3038 /// TerminatorInst's successor index.
3039 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
3040 assert(SuccessorIndex < SI->getNumSuccessors() &&
3041 "Successor index # out of range!");
3042 return SuccessorIndex != 0 ?
3043 Self(SI, SuccessorIndex - 1) :
3044 Self(SI, DefaultPseudoIndex);
3047 /// Resolves case value for current case.
3048 ConstantIntTy *getCaseValue() {
3049 assert(Index < SI->getNumCases() && "Index out the number of cases.");
3050 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
3053 /// Resolves successor for current case.
3054 BasicBlockTy *getCaseSuccessor() {
3055 assert((Index < SI->getNumCases() ||
3056 Index == DefaultPseudoIndex) &&
3057 "Index out the number of cases.");
3058 return SI->getSuccessor(getSuccessorIndex());
3061 /// Returns number of current case.
3062 unsigned getCaseIndex() const { return Index; }
3064 /// Returns TerminatorInst's successor index for current case successor.
3065 unsigned getSuccessorIndex() const {
3066 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
3067 "Index out the number of cases.");
3068 return Index != DefaultPseudoIndex ? Index + 1 : 0;
3072 // Check index correctness after increment.
3073 // Note: Index == getNumCases() means end().
3074 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
3078 Self operator++(int) {
3084 // Check index correctness after decrement.
3085 // Note: Index == getNumCases() means end().
3086 // Also allow "-1" iterator here. That will became valid after ++.
3087 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
3088 "Index out the number of cases.");
3092 Self operator--(int) {
3097 bool operator==(const Self& RHS) const {
3098 assert(RHS.SI == SI && "Incompatible operators.");
3099 return RHS.Index == Index;
3101 bool operator!=(const Self& RHS) const {
3102 assert(RHS.SI == SI && "Incompatible operators.");
3103 return RHS.Index != Index;
3110 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
3113 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
3115 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
3118 CaseIt(const ParentTy &Src) : ParentTy(Src) {}
3119 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
3121 /// Sets the new value for current case.
3122 void setValue(ConstantInt *V) {
3123 assert(Index < SI->getNumCases() && "Index out the number of cases.");
3124 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
3127 /// Sets the new successor for current case.
3128 void setSuccessor(BasicBlock *S) {
3129 SI->setSuccessor(getSuccessorIndex(), S);
3133 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3135 Instruction *InsertBefore = nullptr) {
3136 return new SwitchInst(Value, Default, NumCases, InsertBefore);
3138 static SwitchInst *Create(Value *Value, BasicBlock *Default,
3139 unsigned NumCases, BasicBlock *InsertAtEnd) {
3140 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
3143 /// Provide fast operand accessors
3144 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3146 // Accessor Methods for Switch stmt
3147 Value *getCondition() const { return getOperand(0); }
3148 void setCondition(Value *V) { setOperand(0, V); }
3150 BasicBlock *getDefaultDest() const {
3151 return cast<BasicBlock>(getOperand(1));
3154 void setDefaultDest(BasicBlock *DefaultCase) {
3155 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
3158 /// Return the number of 'cases' in this switch instruction, excluding the
3160 unsigned getNumCases() const {
3161 return getNumOperands()/2 - 1;
3164 /// Returns a read/write iterator that points to the first case in the
3166 CaseIt case_begin() {
3167 return CaseIt(this, 0);
3169 /// Returns a read-only iterator that points to the first case in the
3171 ConstCaseIt case_begin() const {
3172 return ConstCaseIt(this, 0);
3175 /// Returns a read/write iterator that points one past the last in the
3178 return CaseIt(this, getNumCases());
3180 /// Returns a read-only iterator that points one past the last in the
3182 ConstCaseIt case_end() const {
3183 return ConstCaseIt(this, getNumCases());
3186 /// Iteration adapter for range-for loops.
3187 iterator_range<CaseIt> cases() {
3188 return make_range(case_begin(), case_end());
3191 /// Constant iteration adapter for range-for loops.
3192 iterator_range<ConstCaseIt> cases() const {
3193 return make_range(case_begin(), case_end());
3196 /// Returns an iterator that points to the default case.
3197 /// Note: this iterator allows to resolve successor only. Attempt
3198 /// to resolve case value causes an assertion.
3199 /// Also note, that increment and decrement also causes an assertion and
3200 /// makes iterator invalid.
3201 CaseIt case_default() {
3202 return CaseIt(this, DefaultPseudoIndex);
3204 ConstCaseIt case_default() const {
3205 return ConstCaseIt(this, DefaultPseudoIndex);
3208 /// Search all of the case values for the specified constant. If it is
3209 /// explicitly handled, return the case iterator of it, otherwise return
3210 /// default case iterator to indicate that it is handled by the default
3212 CaseIt findCaseValue(const ConstantInt *C) {
3213 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
3214 if (i.getCaseValue() == C)
3216 return case_default();
3218 ConstCaseIt findCaseValue(const ConstantInt *C) const {
3219 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
3220 if (i.getCaseValue() == C)
3222 return case_default();
3225 /// Finds the unique case value for a given successor. Returns null if the
3226 /// successor is not found, not unique, or is the default case.
3227 ConstantInt *findCaseDest(BasicBlock *BB) {
3228 if (BB == getDefaultDest()) return nullptr;
3230 ConstantInt *CI = nullptr;
3231 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
3232 if (i.getCaseSuccessor() == BB) {
3233 if (CI) return nullptr; // Multiple cases lead to BB.
3234 else CI = i.getCaseValue();
3240 /// Add an entry to the switch instruction.
3242 /// This action invalidates case_end(). Old case_end() iterator will
3243 /// point to the added case.
3244 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
3246 /// This method removes the specified case and its successor from the switch
3247 /// instruction. Note that this operation may reorder the remaining cases at
3248 /// index idx and above.
3250 /// This action invalidates iterators for all cases following the one removed,
3251 /// including the case_end() iterator.
3252 void removeCase(CaseIt i);
3254 unsigned getNumSuccessors() const { return getNumOperands()/2; }
3255 BasicBlock *getSuccessor(unsigned idx) const {
3256 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
3257 return cast<BasicBlock>(getOperand(idx*2+1));
3259 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3260 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
3261 setOperand(idx * 2 + 1, NewSucc);
3264 // Methods for support type inquiry through isa, cast, and dyn_cast:
3265 static inline bool classof(const Instruction *I) {
3266 return I->getOpcode() == Instruction::Switch;
3268 static inline bool classof(const Value *V) {
3269 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3273 BasicBlock *getSuccessorV(unsigned idx) const override;
3274 unsigned getNumSuccessorsV() const override;
3275 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3279 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
3282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
3284 //===----------------------------------------------------------------------===//
3285 // IndirectBrInst Class
3286 //===----------------------------------------------------------------------===//
3288 //===---------------------------------------------------------------------------
3289 /// IndirectBrInst - Indirect Branch Instruction.
3291 class IndirectBrInst : public TerminatorInst {
3292 void *operator new(size_t, unsigned) = delete;
3293 unsigned ReservedSpace;
3294 // Operand[0] = Value to switch on
3295 // Operand[1] = Default basic block destination
3296 // Operand[2n ] = Value to match
3297 // Operand[2n+1] = BasicBlock to go to on match
3298 IndirectBrInst(const IndirectBrInst &IBI);
3299 void init(Value *Address, unsigned NumDests);
3300 void growOperands();
3301 // allocate space for exactly zero operands
3302 void *operator new(size_t s) {
3303 return User::operator new(s);
3305 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3306 /// Address to jump to. The number of expected destinations can be specified
3307 /// here to make memory allocation more efficient. This constructor can also
3308 /// autoinsert before another instruction.
3309 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
3311 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
3312 /// Address to jump to. The number of expected destinations can be specified
3313 /// here to make memory allocation more efficient. This constructor also
3314 /// autoinserts at the end of the specified BasicBlock.
3315 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
3318 // Note: Instruction needs to be a friend here to call cloneImpl.
3319 friend class Instruction;
3320 IndirectBrInst *cloneImpl() const;
3323 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3324 Instruction *InsertBefore = nullptr) {
3325 return new IndirectBrInst(Address, NumDests, InsertBefore);
3327 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
3328 BasicBlock *InsertAtEnd) {
3329 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
3332 /// Provide fast operand accessors.
3333 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3335 // Accessor Methods for IndirectBrInst instruction.
3336 Value *getAddress() { return getOperand(0); }
3337 const Value *getAddress() const { return getOperand(0); }
3338 void setAddress(Value *V) { setOperand(0, V); }
3340 /// getNumDestinations - return the number of possible destinations in this
3341 /// indirectbr instruction.
3342 unsigned getNumDestinations() const { return getNumOperands()-1; }
3344 /// getDestination - Return the specified destination.
3345 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
3346 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
3348 /// addDestination - Add a destination.
3350 void addDestination(BasicBlock *Dest);
3352 /// removeDestination - This method removes the specified successor from the
3353 /// indirectbr instruction.
3354 void removeDestination(unsigned i);
3356 unsigned getNumSuccessors() const { return getNumOperands()-1; }
3357 BasicBlock *getSuccessor(unsigned i) const {
3358 return cast<BasicBlock>(getOperand(i+1));
3360 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
3361 setOperand(i + 1, NewSucc);
3364 // Methods for support type inquiry through isa, cast, and dyn_cast:
3365 static inline bool classof(const Instruction *I) {
3366 return I->getOpcode() == Instruction::IndirectBr;
3368 static inline bool classof(const Value *V) {
3369 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3373 BasicBlock *getSuccessorV(unsigned idx) const override;
3374 unsigned getNumSuccessorsV() const override;
3375 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3379 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
3382 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
3384 //===----------------------------------------------------------------------===//
3386 //===----------------------------------------------------------------------===//
3388 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
3389 /// calling convention of the call.
3391 class InvokeInst : public TerminatorInst,
3392 public OperandBundleUser<InvokeInst, User::op_iterator> {
3393 AttributeSet AttributeList;
3395 InvokeInst(const InvokeInst &BI);
3396 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3397 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3398 const Twine &NameStr) {
3399 init(cast<FunctionType>(
3400 cast<PointerType>(Func->getType())->getElementType()),
3401 Func, IfNormal, IfException, Args, Bundles, NameStr);
3403 void init(FunctionType *FTy, Value *Func, BasicBlock *IfNormal,
3404 BasicBlock *IfException, ArrayRef<Value *> Args,
3405 ArrayRef<OperandBundleDef> Bundles, const Twine &NameStr);
3407 /// Construct an InvokeInst given a range of arguments.
3409 /// \brief Construct an InvokeInst from a range of arguments
3410 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3411 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3412 unsigned Values, const Twine &NameStr,
3413 Instruction *InsertBefore)
3414 : InvokeInst(cast<FunctionType>(
3415 cast<PointerType>(Func->getType())->getElementType()),
3416 Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3419 inline InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3420 BasicBlock *IfException, ArrayRef<Value *> Args,
3421 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3422 const Twine &NameStr, Instruction *InsertBefore);
3423 /// Construct an InvokeInst given a range of arguments.
3425 /// \brief Construct an InvokeInst from a range of arguments
3426 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
3427 ArrayRef<Value *> Args, ArrayRef<OperandBundleDef> Bundles,
3428 unsigned Values, const Twine &NameStr,
3429 BasicBlock *InsertAtEnd);
3431 friend class OperandBundleUser<InvokeInst, User::op_iterator>;
3432 bool hasDescriptor() const { return HasDescriptor; }
3435 // Note: Instruction needs to be a friend here to call cloneImpl.
3436 friend class Instruction;
3437 InvokeInst *cloneImpl() const;
3440 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3441 BasicBlock *IfException, ArrayRef<Value *> Args,
3442 const Twine &NameStr,
3443 Instruction *InsertBefore = nullptr) {
3444 return Create(cast<FunctionType>(
3445 cast<PointerType>(Func->getType())->getElementType()),
3446 Func, IfNormal, IfException, Args, None, NameStr,
3449 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3450 BasicBlock *IfException, ArrayRef<Value *> Args,
3451 ArrayRef<OperandBundleDef> Bundles = None,
3452 const Twine &NameStr = "",
3453 Instruction *InsertBefore = nullptr) {
3454 return Create(cast<FunctionType>(
3455 cast<PointerType>(Func->getType())->getElementType()),
3456 Func, IfNormal, IfException, Args, Bundles, NameStr,
3459 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3460 BasicBlock *IfException, ArrayRef<Value *> Args,
3461 const Twine &NameStr,
3462 Instruction *InsertBefore = nullptr) {
3463 unsigned Values = unsigned(Args.size()) + 3;
3464 return new (Values) InvokeInst(Ty, Func, IfNormal, IfException, Args, None,
3465 Values, NameStr, InsertBefore);
3467 static InvokeInst *Create(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3468 BasicBlock *IfException, ArrayRef<Value *> Args,
3469 ArrayRef<OperandBundleDef> Bundles = None,
3470 const Twine &NameStr = "",
3471 Instruction *InsertBefore = nullptr) {
3472 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3473 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3475 return new (Values, DescriptorBytes)
3476 InvokeInst(Ty, Func, IfNormal, IfException, Args, Bundles, Values,
3477 NameStr, InsertBefore);
3479 static InvokeInst *Create(Value *Func,
3480 BasicBlock *IfNormal, BasicBlock *IfException,
3481 ArrayRef<Value *> Args, const Twine &NameStr,
3482 BasicBlock *InsertAtEnd) {
3483 unsigned Values = unsigned(Args.size()) + 3;
3484 return new (Values) InvokeInst(Func, IfNormal, IfException, Args, None,
3485 Values, NameStr, InsertAtEnd);
3487 static InvokeInst *Create(Value *Func, BasicBlock *IfNormal,
3488 BasicBlock *IfException, ArrayRef<Value *> Args,
3489 ArrayRef<OperandBundleDef> Bundles,
3490 const Twine &NameStr, BasicBlock *InsertAtEnd) {
3491 unsigned Values = unsigned(Args.size()) + CountBundleInputs(Bundles) + 3;
3492 unsigned DescriptorBytes = Bundles.size() * sizeof(BundleOpInfo);
3494 return new (Values, DescriptorBytes)
3495 InvokeInst(Func, IfNormal, IfException, Args, Bundles, Values, NameStr,
3499 /// \brief Create a clone of \p II with a different set of operand bundles and
3500 /// insert it before \p InsertPt.
3502 /// The returned invoke instruction is identical to \p II in every way except
3503 /// that the operand bundles for the new instruction are set to the operand
3504 /// bundles in \p Bundles.
3505 static InvokeInst *Create(InvokeInst *II, ArrayRef<OperandBundleDef> Bundles,
3506 Instruction *InsertPt = nullptr);
3508 /// Provide fast operand accessors
3509 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3511 FunctionType *getFunctionType() const { return FTy; }
3513 void mutateFunctionType(FunctionType *FTy) {
3514 mutateType(FTy->getReturnType());
3518 /// getNumArgOperands - Return the number of invoke arguments.
3520 unsigned getNumArgOperands() const {
3521 return getNumOperands() - getNumTotalBundleOperands() - 3;
3524 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
3526 Value *getArgOperand(unsigned i) const {
3527 assert(i < getNumArgOperands() && "Out of bounds!");
3528 return getOperand(i);
3530 void setArgOperand(unsigned i, Value *v) {
3531 assert(i < getNumArgOperands() && "Out of bounds!");
3535 /// \brief Return the iterator pointing to the beginning of the argument list.
3536 op_iterator arg_begin() { return op_begin(); }
3538 /// \brief Return the iterator pointing to the end of the argument list.
3539 op_iterator arg_end() {
3540 // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee
3541 return op_end() - getNumTotalBundleOperands() - 3;
3544 /// \brief Iteration adapter for range-for loops.
3545 iterator_range<op_iterator> arg_operands() {
3546 return make_range(arg_begin(), arg_end());
3549 /// \brief Return the iterator pointing to the beginning of the argument list.
3550 const_op_iterator arg_begin() const { return op_begin(); }
3552 /// \brief Return the iterator pointing to the end of the argument list.
3553 const_op_iterator arg_end() const {
3554 // [ invoke args ], [ operand bundles ], normal dest, unwind dest, callee
3555 return op_end() - getNumTotalBundleOperands() - 3;
3558 /// \brief Iteration adapter for range-for loops.
3559 iterator_range<const_op_iterator> arg_operands() const {
3560 return make_range(arg_begin(), arg_end());
3563 /// \brief Wrappers for getting the \c Use of a invoke argument.
3564 const Use &getArgOperandUse(unsigned i) const {
3565 assert(i < getNumArgOperands() && "Out of bounds!");
3566 return getOperandUse(i);
3568 Use &getArgOperandUse(unsigned i) {
3569 assert(i < getNumArgOperands() && "Out of bounds!");
3570 return getOperandUse(i);
3573 /// If one of the arguments has the 'returned' attribute, return its
3574 /// operand value. Otherwise, return nullptr.
3575 Value *getReturnedArgOperand() const;
3577 /// getCallingConv/setCallingConv - Get or set the calling convention of this
3579 CallingConv::ID getCallingConv() const {
3580 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
3582 void setCallingConv(CallingConv::ID CC) {
3583 auto ID = static_cast<unsigned>(CC);
3584 assert(!(ID & ~CallingConv::MaxID) && "Unsupported calling convention");
3585 setInstructionSubclassData(ID);
3588 /// getAttributes - Return the parameter attributes for this invoke.
3590 const AttributeSet &getAttributes() const { return AttributeList; }
3592 /// setAttributes - Set the parameter attributes for this invoke.
3594 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
3596 /// addAttribute - adds the attribute to the list of attributes.
3597 void addAttribute(unsigned i, Attribute::AttrKind Kind);
3599 /// addAttribute - adds the attribute to the list of attributes.
3600 void addAttribute(unsigned i, Attribute Attr);
3602 /// removeAttribute - removes the attribute from the list of attributes.
3603 void removeAttribute(unsigned i, Attribute::AttrKind Kind);
3605 /// removeAttribute - removes the attribute from the list of attributes.
3606 void removeAttribute(unsigned i, StringRef Kind);
3608 /// removeAttribute - removes the attribute from the list of attributes.
3609 void removeAttribute(unsigned i, Attribute Attr);
3611 /// \brief adds the dereferenceable attribute to the list of attributes.
3612 void addDereferenceableAttr(unsigned i, uint64_t Bytes);
3614 /// \brief adds the dereferenceable_or_null attribute to the list of
3616 void addDereferenceableOrNullAttr(unsigned i, uint64_t Bytes);
3618 /// \brief Determine whether this call has the given attribute.
3619 bool hasFnAttr(Attribute::AttrKind Kind) const {
3620 assert(Kind != Attribute::NoBuiltin &&
3621 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
3622 return hasFnAttrImpl(Kind);
3625 /// \brief Determine whether this call has the given attribute.
3626 bool hasFnAttr(StringRef Kind) const {
3627 return hasFnAttrImpl(Kind);
3630 /// \brief Determine whether the call or the callee has the given attributes.
3631 bool paramHasAttr(unsigned i, Attribute::AttrKind Kind) const;
3633 /// \brief Get the attribute of a given kind at a position.
3634 Attribute getAttribute(unsigned i, Attribute::AttrKind Kind) const;
3636 /// \brief Get the attribute of a given kind at a position.
3637 Attribute getAttribute(unsigned i, StringRef Kind) const;
3639 /// \brief Return true if the data operand at index \p i has the attribute \p
3642 /// Data operands include invoke arguments and values used in operand bundles,
3643 /// but does not include the invokee operand, or the two successor blocks.
3644 /// This routine dispatches to the underlying AttributeList or the
3645 /// OperandBundleUser as appropriate.
3647 /// The index \p i is interpreted as
3649 /// \p i == Attribute::ReturnIndex -> the return value
3650 /// \p i in [1, arg_size + 1) -> argument number (\p i - 1)
3651 /// \p i in [arg_size + 1, data_operand_size + 1) -> bundle operand at index
3652 /// (\p i - 1) in the operand list.
3653 bool dataOperandHasImpliedAttr(unsigned i, Attribute::AttrKind Kind) const;
3655 /// \brief Extract the alignment for a call or parameter (0=unknown).
3656 unsigned getParamAlignment(unsigned i) const {
3657 return AttributeList.getParamAlignment(i);
3660 /// \brief Extract the number of dereferenceable bytes for a call or
3661 /// parameter (0=unknown).
3662 uint64_t getDereferenceableBytes(unsigned i) const {
3663 return AttributeList.getDereferenceableBytes(i);
3666 /// \brief Extract the number of dereferenceable_or_null bytes for a call or
3667 /// parameter (0=unknown).
3668 uint64_t getDereferenceableOrNullBytes(unsigned i) const {
3669 return AttributeList.getDereferenceableOrNullBytes(i);
3672 /// @brief Determine if the parameter or return value is marked with NoAlias
3674 /// @param n The parameter to check. 1 is the first parameter, 0 is the return
3675 bool doesNotAlias(unsigned n) const {
3676 return AttributeList.hasAttribute(n, Attribute::NoAlias);
3679 /// \brief Return true if the call should not be treated as a call to a
3681 bool isNoBuiltin() const {
3682 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3683 // to check it by hand.
3684 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3685 !hasFnAttrImpl(Attribute::Builtin);
3688 /// \brief Return true if the call should not be inlined.
3689 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3690 void setIsNoInline() {
3691 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3694 /// \brief Determine if the call does not access memory.
3695 bool doesNotAccessMemory() const {
3696 return hasFnAttr(Attribute::ReadNone);
3698 void setDoesNotAccessMemory() {
3699 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3702 /// \brief Determine if the call does not access or only reads memory.
3703 bool onlyReadsMemory() const {
3704 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3706 void setOnlyReadsMemory() {
3707 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3710 /// \brief Determine if the call does not access or only writes memory.
3711 bool doesNotReadMemory() const {
3712 return doesNotAccessMemory() || hasFnAttr(Attribute::WriteOnly);
3714 void setDoesNotReadMemory() {
3715 addAttribute(AttributeSet::FunctionIndex, Attribute::WriteOnly);
3718 /// @brief Determine if the call access memmory only using it's pointer
3720 bool onlyAccessesArgMemory() const {
3721 return hasFnAttr(Attribute::ArgMemOnly);
3723 void setOnlyAccessesArgMemory() {
3724 addAttribute(AttributeSet::FunctionIndex, Attribute::ArgMemOnly);
3727 /// \brief Determine if the call cannot return.
3728 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3729 void setDoesNotReturn() {
3730 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3733 /// \brief Determine if the call cannot unwind.
3734 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3735 void setDoesNotThrow() {
3736 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3739 /// \brief Determine if the invoke cannot be duplicated.
3740 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3741 void setCannotDuplicate() {
3742 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3745 /// \brief Determine if the invoke is convergent
3746 bool isConvergent() const { return hasFnAttr(Attribute::Convergent); }
3747 void setConvergent() {
3748 addAttribute(AttributeSet::FunctionIndex, Attribute::Convergent);
3750 void setNotConvergent() {
3751 removeAttribute(AttributeSet::FunctionIndex,
3752 Attribute::get(getContext(), Attribute::Convergent));
3755 /// \brief Determine if the call returns a structure through first
3756 /// pointer argument.
3757 bool hasStructRetAttr() const {
3758 if (getNumArgOperands() == 0)
3761 // Be friendly and also check the callee.
3762 return paramHasAttr(1, Attribute::StructRet);
3765 /// \brief Determine if any call argument is an aggregate passed by value.
3766 bool hasByValArgument() const {
3767 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3770 /// getCalledFunction - Return the function called, or null if this is an
3771 /// indirect function invocation.
3773 Function *getCalledFunction() const {
3774 return dyn_cast<Function>(Op<-3>());
3777 /// getCalledValue - Get a pointer to the function that is invoked by this
3779 const Value *getCalledValue() const { return Op<-3>(); }
3780 Value *getCalledValue() { return Op<-3>(); }
3782 /// setCalledFunction - Set the function called.
3783 void setCalledFunction(Value* Fn) {
3785 cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType()),
3788 void setCalledFunction(FunctionType *FTy, Value *Fn) {
3790 assert(FTy == cast<FunctionType>(
3791 cast<PointerType>(Fn->getType())->getElementType()));
3795 // get*Dest - Return the destination basic blocks...
3796 BasicBlock *getNormalDest() const {
3797 return cast<BasicBlock>(Op<-2>());
3799 BasicBlock *getUnwindDest() const {
3800 return cast<BasicBlock>(Op<-1>());
3802 void setNormalDest(BasicBlock *B) {
3803 Op<-2>() = reinterpret_cast<Value*>(B);
3805 void setUnwindDest(BasicBlock *B) {
3806 Op<-1>() = reinterpret_cast<Value*>(B);
3809 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3810 /// block (the unwind destination).
3811 LandingPadInst *getLandingPadInst() const;
3813 BasicBlock *getSuccessor(unsigned i) const {
3814 assert(i < 2 && "Successor # out of range for invoke!");
3815 return i == 0 ? getNormalDest() : getUnwindDest();
3818 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3819 assert(idx < 2 && "Successor # out of range for invoke!");
3820 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3823 unsigned getNumSuccessors() const { return 2; }
3825 // Methods for support type inquiry through isa, cast, and dyn_cast:
3826 static inline bool classof(const Instruction *I) {
3827 return (I->getOpcode() == Instruction::Invoke);
3829 static inline bool classof(const Value *V) {
3830 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3834 BasicBlock *getSuccessorV(unsigned idx) const override;
3835 unsigned getNumSuccessorsV() const override;
3836 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3838 template <typename AttrKind> bool hasFnAttrImpl(AttrKind A) const {
3839 if (AttributeList.hasAttribute(AttributeSet::FunctionIndex, A))
3842 // Operand bundles override attributes on the called function, but don't
3843 // override attributes directly present on the invoke instruction.
3844 if (isFnAttrDisallowedByOpBundle(A))
3847 if (const Function *F = getCalledFunction())
3848 return F->getAttributes().hasAttribute(AttributeSet::FunctionIndex, A);
3852 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3853 // method so that subclasses cannot accidentally use it.
3854 void setInstructionSubclassData(unsigned short D) {
3855 Instruction::setInstructionSubclassData(D);
3860 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3863 InvokeInst::InvokeInst(FunctionType *Ty, Value *Func, BasicBlock *IfNormal,
3864 BasicBlock *IfException, ArrayRef<Value *> Args,
3865 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3866 const Twine &NameStr, Instruction *InsertBefore)
3867 : TerminatorInst(Ty->getReturnType(), Instruction::Invoke,
3868 OperandTraits<InvokeInst>::op_end(this) - Values, Values,
3870 init(Ty, Func, IfNormal, IfException, Args, Bundles, NameStr);
3872 InvokeInst::InvokeInst(Value *Func, BasicBlock *IfNormal,
3873 BasicBlock *IfException, ArrayRef<Value *> Args,
3874 ArrayRef<OperandBundleDef> Bundles, unsigned Values,
3875 const Twine &NameStr, BasicBlock *InsertAtEnd)
3877 cast<FunctionType>(cast<PointerType>(Func->getType())
3878 ->getElementType())->getReturnType(),
3879 Instruction::Invoke, OperandTraits<InvokeInst>::op_end(this) - Values,
3880 Values, InsertAtEnd) {
3881 init(Func, IfNormal, IfException, Args, Bundles, NameStr);
3884 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3886 //===----------------------------------------------------------------------===//
3888 //===----------------------------------------------------------------------===//
3890 //===---------------------------------------------------------------------------
3891 /// ResumeInst - Resume the propagation of an exception.
3893 class ResumeInst : public TerminatorInst {
3894 ResumeInst(const ResumeInst &RI);
3896 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=nullptr);
3897 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3900 // Note: Instruction needs to be a friend here to call cloneImpl.
3901 friend class Instruction;
3902 ResumeInst *cloneImpl() const;
3905 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = nullptr) {
3906 return new(1) ResumeInst(Exn, InsertBefore);
3908 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3909 return new(1) ResumeInst(Exn, InsertAtEnd);
3912 /// Provide fast operand accessors
3913 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3915 /// Convenience accessor.
3916 Value *getValue() const { return Op<0>(); }
3918 unsigned getNumSuccessors() const { return 0; }
3920 // Methods for support type inquiry through isa, cast, and dyn_cast:
3921 static inline bool classof(const Instruction *I) {
3922 return I->getOpcode() == Instruction::Resume;
3924 static inline bool classof(const Value *V) {
3925 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3929 BasicBlock *getSuccessorV(unsigned idx) const override;
3930 unsigned getNumSuccessorsV() const override;
3931 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3935 struct OperandTraits<ResumeInst> :
3936 public FixedNumOperandTraits<ResumeInst, 1> {
3939 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3941 //===----------------------------------------------------------------------===//
3942 // CatchSwitchInst Class
3943 //===----------------------------------------------------------------------===//
3944 class CatchSwitchInst : public TerminatorInst {
3945 void *operator new(size_t, unsigned) = delete;
3946 /// ReservedSpace - The number of operands actually allocated. NumOperands is
3947 /// the number actually in use.
3948 unsigned ReservedSpace;
3949 // Operand[0] = Outer scope
3950 // Operand[1] = Unwind block destination
3951 // Operand[n] = BasicBlock to go to on match
3952 CatchSwitchInst(const CatchSwitchInst &CSI);
3953 void init(Value *ParentPad, BasicBlock *UnwindDest, unsigned NumReserved);
3954 void growOperands(unsigned Size);
3955 // allocate space for exactly zero operands
3956 void *operator new(size_t s) { return User::operator new(s); }
3957 /// CatchSwitchInst ctor - Create a new switch instruction, specifying a
3958 /// default destination. The number of additional handlers can be specified
3959 /// here to make memory allocation more efficient.
3960 /// This constructor can also autoinsert before another instruction.
3961 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
3962 unsigned NumHandlers, const Twine &NameStr,
3963 Instruction *InsertBefore);
3965 /// CatchSwitchInst ctor - Create a new switch instruction, specifying a
3966 /// default destination. The number of additional handlers can be specified
3967 /// here to make memory allocation more efficient.
3968 /// This constructor also autoinserts at the end of the specified BasicBlock.
3969 CatchSwitchInst(Value *ParentPad, BasicBlock *UnwindDest,
3970 unsigned NumHandlers, const Twine &NameStr,
3971 BasicBlock *InsertAtEnd);
3974 // Note: Instruction needs to be a friend here to call cloneImpl.
3975 friend class Instruction;
3976 CatchSwitchInst *cloneImpl() const;
3979 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
3980 unsigned NumHandlers,
3981 const Twine &NameStr = "",
3982 Instruction *InsertBefore = nullptr) {
3983 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
3986 static CatchSwitchInst *Create(Value *ParentPad, BasicBlock *UnwindDest,
3987 unsigned NumHandlers, const Twine &NameStr,
3988 BasicBlock *InsertAtEnd) {
3989 return new CatchSwitchInst(ParentPad, UnwindDest, NumHandlers, NameStr,
3993 /// Provide fast operand accessors
3994 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3996 // Accessor Methods for CatchSwitch stmt
3997 Value *getParentPad() const { return getOperand(0); }
3998 void setParentPad(Value *ParentPad) { setOperand(0, ParentPad); }
4000 // Accessor Methods for CatchSwitch stmt
4001 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4002 bool unwindsToCaller() const { return !hasUnwindDest(); }
4003 BasicBlock *getUnwindDest() const {
4004 if (hasUnwindDest())
4005 return cast<BasicBlock>(getOperand(1));
4008 void setUnwindDest(BasicBlock *UnwindDest) {
4010 assert(hasUnwindDest());
4011 setOperand(1, UnwindDest);
4014 /// getNumHandlers - return the number of 'handlers' in this catchswitch
4015 /// instruction, except the default handler
4016 unsigned getNumHandlers() const {
4017 if (hasUnwindDest())
4018 return getNumOperands() - 2;
4019 return getNumOperands() - 1;
4023 static BasicBlock *handler_helper(Value *V) { return cast<BasicBlock>(V); }
4024 static const BasicBlock *handler_helper(const Value *V) {
4025 return cast<BasicBlock>(V);
4029 typedef std::pointer_to_unary_function<Value *, BasicBlock *> DerefFnTy;
4030 typedef mapped_iterator<op_iterator, DerefFnTy> handler_iterator;
4031 typedef iterator_range<handler_iterator> handler_range;
4034 typedef std::pointer_to_unary_function<const Value *, const BasicBlock *>
4036 typedef mapped_iterator<const_op_iterator, ConstDerefFnTy> const_handler_iterator;
4037 typedef iterator_range<const_handler_iterator> const_handler_range;
4039 /// Returns an iterator that points to the first handler in CatchSwitchInst.
4040 handler_iterator handler_begin() {
4041 op_iterator It = op_begin() + 1;
4042 if (hasUnwindDest())
4044 return handler_iterator(It, DerefFnTy(handler_helper));
4046 /// Returns an iterator that points to the first handler in the
4047 /// CatchSwitchInst.
4048 const_handler_iterator handler_begin() const {
4049 const_op_iterator It = op_begin() + 1;
4050 if (hasUnwindDest())
4052 return const_handler_iterator(It, ConstDerefFnTy(handler_helper));
4055 /// Returns a read-only iterator that points one past the last
4056 /// handler in the CatchSwitchInst.
4057 handler_iterator handler_end() {
4058 return handler_iterator(op_end(), DerefFnTy(handler_helper));
4060 /// Returns an iterator that points one past the last handler in the
4061 /// CatchSwitchInst.
4062 const_handler_iterator handler_end() const {
4063 return const_handler_iterator(op_end(), ConstDerefFnTy(handler_helper));
4066 /// handlers - iteration adapter for range-for loops.
4067 handler_range handlers() {
4068 return make_range(handler_begin(), handler_end());
4071 /// handlers - iteration adapter for range-for loops.
4072 const_handler_range handlers() const {
4073 return make_range(handler_begin(), handler_end());
4076 /// addHandler - Add an entry to the switch instruction...
4078 /// This action invalidates handler_end(). Old handler_end() iterator will
4079 /// point to the added handler.
4080 void addHandler(BasicBlock *Dest);
4082 void removeHandler(handler_iterator HI);
4084 unsigned getNumSuccessors() const { return getNumOperands() - 1; }
4085 BasicBlock *getSuccessor(unsigned Idx) const {
4086 assert(Idx < getNumSuccessors() &&
4087 "Successor # out of range for catchswitch!");
4088 return cast<BasicBlock>(getOperand(Idx + 1));
4090 void setSuccessor(unsigned Idx, BasicBlock *NewSucc) {
4091 assert(Idx < getNumSuccessors() &&
4092 "Successor # out of range for catchswitch!");
4093 setOperand(Idx + 1, NewSucc);
4096 // Methods for support type inquiry through isa, cast, and dyn_cast:
4097 static inline bool classof(const Instruction *I) {
4098 return I->getOpcode() == Instruction::CatchSwitch;
4100 static inline bool classof(const Value *V) {
4101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4105 BasicBlock *getSuccessorV(unsigned Idx) const override;
4106 unsigned getNumSuccessorsV() const override;
4107 void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4111 struct OperandTraits<CatchSwitchInst> : public HungoffOperandTraits<2> {};
4113 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchSwitchInst, Value)
4115 //===----------------------------------------------------------------------===//
4116 // CleanupPadInst Class
4117 //===----------------------------------------------------------------------===//
4118 class CleanupPadInst : public FuncletPadInst {
4120 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4121 unsigned Values, const Twine &NameStr,
4122 Instruction *InsertBefore)
4123 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4124 NameStr, InsertBefore) {}
4125 explicit CleanupPadInst(Value *ParentPad, ArrayRef<Value *> Args,
4126 unsigned Values, const Twine &NameStr,
4127 BasicBlock *InsertAtEnd)
4128 : FuncletPadInst(Instruction::CleanupPad, ParentPad, Args, Values,
4129 NameStr, InsertAtEnd) {}
4132 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args = None,
4133 const Twine &NameStr = "",
4134 Instruction *InsertBefore = nullptr) {
4135 unsigned Values = 1 + Args.size();
4137 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertBefore);
4139 static CleanupPadInst *Create(Value *ParentPad, ArrayRef<Value *> Args,
4140 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4141 unsigned Values = 1 + Args.size();
4143 CleanupPadInst(ParentPad, Args, Values, NameStr, InsertAtEnd);
4146 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4147 static inline bool classof(const Instruction *I) {
4148 return I->getOpcode() == Instruction::CleanupPad;
4150 static inline bool classof(const Value *V) {
4151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4155 //===----------------------------------------------------------------------===//
4156 // CatchPadInst Class
4157 //===----------------------------------------------------------------------===//
4158 class CatchPadInst : public FuncletPadInst {
4160 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4161 unsigned Values, const Twine &NameStr,
4162 Instruction *InsertBefore)
4163 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4164 NameStr, InsertBefore) {}
4165 explicit CatchPadInst(Value *CatchSwitch, ArrayRef<Value *> Args,
4166 unsigned Values, const Twine &NameStr,
4167 BasicBlock *InsertAtEnd)
4168 : FuncletPadInst(Instruction::CatchPad, CatchSwitch, Args, Values,
4169 NameStr, InsertAtEnd) {}
4172 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4173 const Twine &NameStr = "",
4174 Instruction *InsertBefore = nullptr) {
4175 unsigned Values = 1 + Args.size();
4177 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertBefore);
4179 static CatchPadInst *Create(Value *CatchSwitch, ArrayRef<Value *> Args,
4180 const Twine &NameStr, BasicBlock *InsertAtEnd) {
4181 unsigned Values = 1 + Args.size();
4183 CatchPadInst(CatchSwitch, Args, Values, NameStr, InsertAtEnd);
4186 /// Convenience accessors
4187 CatchSwitchInst *getCatchSwitch() const {
4188 return cast<CatchSwitchInst>(Op<-1>());
4190 void setCatchSwitch(Value *CatchSwitch) {
4191 assert(CatchSwitch);
4192 Op<-1>() = CatchSwitch;
4195 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4196 static inline bool classof(const Instruction *I) {
4197 return I->getOpcode() == Instruction::CatchPad;
4199 static inline bool classof(const Value *V) {
4200 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4204 //===----------------------------------------------------------------------===//
4205 // CatchReturnInst Class
4206 //===----------------------------------------------------------------------===//
4208 class CatchReturnInst : public TerminatorInst {
4209 CatchReturnInst(const CatchReturnInst &RI);
4211 void init(Value *CatchPad, BasicBlock *BB);
4212 CatchReturnInst(Value *CatchPad, BasicBlock *BB, Instruction *InsertBefore);
4213 CatchReturnInst(Value *CatchPad, BasicBlock *BB, BasicBlock *InsertAtEnd);
4216 // Note: Instruction needs to be a friend here to call cloneImpl.
4217 friend class Instruction;
4218 CatchReturnInst *cloneImpl() const;
4221 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4222 Instruction *InsertBefore = nullptr) {
4225 return new (2) CatchReturnInst(CatchPad, BB, InsertBefore);
4227 static CatchReturnInst *Create(Value *CatchPad, BasicBlock *BB,
4228 BasicBlock *InsertAtEnd) {
4231 return new (2) CatchReturnInst(CatchPad, BB, InsertAtEnd);
4234 /// Provide fast operand accessors
4235 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4237 /// Convenience accessors.
4238 CatchPadInst *getCatchPad() const { return cast<CatchPadInst>(Op<0>()); }
4239 void setCatchPad(CatchPadInst *CatchPad) {
4244 BasicBlock *getSuccessor() const { return cast<BasicBlock>(Op<1>()); }
4245 void setSuccessor(BasicBlock *NewSucc) {
4249 unsigned getNumSuccessors() const { return 1; }
4251 /// Get the parentPad of this catchret's catchpad's catchswitch.
4252 /// The successor block is implicitly a member of this funclet.
4253 Value *getCatchSwitchParentPad() const {
4254 return getCatchPad()->getCatchSwitch()->getParentPad();
4257 // Methods for support type inquiry through isa, cast, and dyn_cast:
4258 static inline bool classof(const Instruction *I) {
4259 return (I->getOpcode() == Instruction::CatchRet);
4261 static inline bool classof(const Value *V) {
4262 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4266 BasicBlock *getSuccessorV(unsigned Idx) const override;
4267 unsigned getNumSuccessorsV() const override;
4268 void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4272 struct OperandTraits<CatchReturnInst>
4273 : public FixedNumOperandTraits<CatchReturnInst, 2> {};
4275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CatchReturnInst, Value)
4277 //===----------------------------------------------------------------------===//
4278 // CleanupReturnInst Class
4279 //===----------------------------------------------------------------------===//
4281 class CleanupReturnInst : public TerminatorInst {
4283 CleanupReturnInst(const CleanupReturnInst &RI);
4285 void init(Value *CleanupPad, BasicBlock *UnwindBB);
4286 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4287 Instruction *InsertBefore = nullptr);
4288 CleanupReturnInst(Value *CleanupPad, BasicBlock *UnwindBB, unsigned Values,
4289 BasicBlock *InsertAtEnd);
4292 // Note: Instruction needs to be a friend here to call cloneImpl.
4293 friend class Instruction;
4294 CleanupReturnInst *cloneImpl() const;
4297 static CleanupReturnInst *Create(Value *CleanupPad,
4298 BasicBlock *UnwindBB = nullptr,
4299 Instruction *InsertBefore = nullptr) {
4301 unsigned Values = 1;
4305 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertBefore);
4307 static CleanupReturnInst *Create(Value *CleanupPad, BasicBlock *UnwindBB,
4308 BasicBlock *InsertAtEnd) {
4310 unsigned Values = 1;
4314 CleanupReturnInst(CleanupPad, UnwindBB, Values, InsertAtEnd);
4317 /// Provide fast operand accessors
4318 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
4320 bool hasUnwindDest() const { return getSubclassDataFromInstruction() & 1; }
4321 bool unwindsToCaller() const { return !hasUnwindDest(); }
4323 /// Convenience accessor.
4324 CleanupPadInst *getCleanupPad() const {
4325 return cast<CleanupPadInst>(Op<0>());
4327 void setCleanupPad(CleanupPadInst *CleanupPad) {
4329 Op<0>() = CleanupPad;
4332 unsigned getNumSuccessors() const { return hasUnwindDest() ? 1 : 0; }
4334 BasicBlock *getUnwindDest() const {
4335 return hasUnwindDest() ? cast<BasicBlock>(Op<1>()) : nullptr;
4337 void setUnwindDest(BasicBlock *NewDest) {
4339 assert(hasUnwindDest());
4343 // Methods for support type inquiry through isa, cast, and dyn_cast:
4344 static inline bool classof(const Instruction *I) {
4345 return (I->getOpcode() == Instruction::CleanupRet);
4347 static inline bool classof(const Value *V) {
4348 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4352 BasicBlock *getSuccessorV(unsigned Idx) const override;
4353 unsigned getNumSuccessorsV() const override;
4354 void setSuccessorV(unsigned Idx, BasicBlock *B) override;
4356 // Shadow Instruction::setInstructionSubclassData with a private forwarding
4357 // method so that subclasses cannot accidentally use it.
4358 void setInstructionSubclassData(unsigned short D) {
4359 Instruction::setInstructionSubclassData(D);
4364 struct OperandTraits<CleanupReturnInst>
4365 : public VariadicOperandTraits<CleanupReturnInst, /*MINARITY=*/1> {};
4367 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CleanupReturnInst, Value)
4369 //===----------------------------------------------------------------------===//
4370 // UnreachableInst Class
4371 //===----------------------------------------------------------------------===//
4373 //===---------------------------------------------------------------------------
4374 /// UnreachableInst - This function has undefined behavior. In particular, the
4375 /// presence of this instruction indicates some higher level knowledge that the
4376 /// end of the block cannot be reached.
4378 class UnreachableInst : public TerminatorInst {
4379 void *operator new(size_t, unsigned) = delete;
4382 // Note: Instruction needs to be a friend here to call cloneImpl.
4383 friend class Instruction;
4384 UnreachableInst *cloneImpl() const;
4387 // allocate space for exactly zero operands
4388 void *operator new(size_t s) {
4389 return User::operator new(s, 0);
4391 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = nullptr);
4392 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
4394 unsigned getNumSuccessors() const { return 0; }
4396 // Methods for support type inquiry through isa, cast, and dyn_cast:
4397 static inline bool classof(const Instruction *I) {
4398 return I->getOpcode() == Instruction::Unreachable;
4400 static inline bool classof(const Value *V) {
4401 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4405 BasicBlock *getSuccessorV(unsigned idx) const override;
4406 unsigned getNumSuccessorsV() const override;
4407 void setSuccessorV(unsigned idx, BasicBlock *B) override;
4410 //===----------------------------------------------------------------------===//
4412 //===----------------------------------------------------------------------===//
4414 /// \brief This class represents a truncation of integer types.
4415 class TruncInst : public CastInst {
4417 // Note: Instruction needs to be a friend here to call cloneImpl.
4418 friend class Instruction;
4419 /// \brief Clone an identical TruncInst
4420 TruncInst *cloneImpl() const;
4423 /// \brief Constructor with insert-before-instruction semantics
4425 Value *S, ///< The value to be truncated
4426 Type *Ty, ///< The (smaller) type to truncate to
4427 const Twine &NameStr = "", ///< A name for the new instruction
4428 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4431 /// \brief Constructor with insert-at-end-of-block semantics
4433 Value *S, ///< The value to be truncated
4434 Type *Ty, ///< The (smaller) type to truncate to
4435 const Twine &NameStr, ///< A name for the new instruction
4436 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4439 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4440 static inline bool classof(const Instruction *I) {
4441 return I->getOpcode() == Trunc;
4443 static inline bool classof(const Value *V) {
4444 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4448 //===----------------------------------------------------------------------===//
4450 //===----------------------------------------------------------------------===//
4452 /// \brief This class represents zero extension of integer types.
4453 class ZExtInst : public CastInst {
4455 // Note: Instruction needs to be a friend here to call cloneImpl.
4456 friend class Instruction;
4457 /// \brief Clone an identical ZExtInst
4458 ZExtInst *cloneImpl() const;
4461 /// \brief Constructor with insert-before-instruction semantics
4463 Value *S, ///< The value to be zero extended
4464 Type *Ty, ///< The type to zero extend to
4465 const Twine &NameStr = "", ///< A name for the new instruction
4466 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4469 /// \brief Constructor with insert-at-end semantics.
4471 Value *S, ///< The value to be zero extended
4472 Type *Ty, ///< The type to zero extend to
4473 const Twine &NameStr, ///< A name for the new instruction
4474 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4477 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4478 static inline bool classof(const Instruction *I) {
4479 return I->getOpcode() == ZExt;
4481 static inline bool classof(const Value *V) {
4482 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4486 //===----------------------------------------------------------------------===//
4488 //===----------------------------------------------------------------------===//
4490 /// \brief This class represents a sign extension of integer types.
4491 class SExtInst : public CastInst {
4493 // Note: Instruction needs to be a friend here to call cloneImpl.
4494 friend class Instruction;
4495 /// \brief Clone an identical SExtInst
4496 SExtInst *cloneImpl() const;
4499 /// \brief Constructor with insert-before-instruction semantics
4501 Value *S, ///< The value to be sign extended
4502 Type *Ty, ///< The type to sign extend to
4503 const Twine &NameStr = "", ///< A name for the new instruction
4504 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4507 /// \brief Constructor with insert-at-end-of-block semantics
4509 Value *S, ///< The value to be sign extended
4510 Type *Ty, ///< The type to sign extend to
4511 const Twine &NameStr, ///< A name for the new instruction
4512 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4515 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4516 static inline bool classof(const Instruction *I) {
4517 return I->getOpcode() == SExt;
4519 static inline bool classof(const Value *V) {
4520 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4524 //===----------------------------------------------------------------------===//
4525 // FPTruncInst Class
4526 //===----------------------------------------------------------------------===//
4528 /// \brief This class represents a truncation of floating point types.
4529 class FPTruncInst : public CastInst {
4531 // Note: Instruction needs to be a friend here to call cloneImpl.
4532 friend class Instruction;
4533 /// \brief Clone an identical FPTruncInst
4534 FPTruncInst *cloneImpl() const;
4537 /// \brief Constructor with insert-before-instruction semantics
4539 Value *S, ///< The value to be truncated
4540 Type *Ty, ///< The type to truncate to
4541 const Twine &NameStr = "", ///< A name for the new instruction
4542 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4545 /// \brief Constructor with insert-before-instruction semantics
4547 Value *S, ///< The value to be truncated
4548 Type *Ty, ///< The type to truncate to
4549 const Twine &NameStr, ///< A name for the new instruction
4550 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4553 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4554 static inline bool classof(const Instruction *I) {
4555 return I->getOpcode() == FPTrunc;
4557 static inline bool classof(const Value *V) {
4558 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4562 //===----------------------------------------------------------------------===//
4564 //===----------------------------------------------------------------------===//
4566 /// \brief This class represents an extension of floating point types.
4567 class FPExtInst : public CastInst {
4569 // Note: Instruction needs to be a friend here to call cloneImpl.
4570 friend class Instruction;
4571 /// \brief Clone an identical FPExtInst
4572 FPExtInst *cloneImpl() const;
4575 /// \brief Constructor with insert-before-instruction semantics
4577 Value *S, ///< The value to be extended
4578 Type *Ty, ///< The type to extend to
4579 const Twine &NameStr = "", ///< A name for the new instruction
4580 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4583 /// \brief Constructor with insert-at-end-of-block semantics
4585 Value *S, ///< The value to be extended
4586 Type *Ty, ///< The type to extend to
4587 const Twine &NameStr, ///< A name for the new instruction
4588 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4591 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4592 static inline bool classof(const Instruction *I) {
4593 return I->getOpcode() == FPExt;
4595 static inline bool classof(const Value *V) {
4596 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4600 //===----------------------------------------------------------------------===//
4602 //===----------------------------------------------------------------------===//
4604 /// \brief This class represents a cast unsigned integer to floating point.
4605 class UIToFPInst : public CastInst {
4607 // Note: Instruction needs to be a friend here to call cloneImpl.
4608 friend class Instruction;
4609 /// \brief Clone an identical UIToFPInst
4610 UIToFPInst *cloneImpl() const;
4613 /// \brief Constructor with insert-before-instruction semantics
4615 Value *S, ///< The value to be converted
4616 Type *Ty, ///< The type to convert to
4617 const Twine &NameStr = "", ///< A name for the new instruction
4618 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4621 /// \brief Constructor with insert-at-end-of-block semantics
4623 Value *S, ///< The value to be converted
4624 Type *Ty, ///< The type to convert to
4625 const Twine &NameStr, ///< A name for the new instruction
4626 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4629 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4630 static inline bool classof(const Instruction *I) {
4631 return I->getOpcode() == UIToFP;
4633 static inline bool classof(const Value *V) {
4634 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4638 //===----------------------------------------------------------------------===//
4640 //===----------------------------------------------------------------------===//
4642 /// \brief This class represents a cast from signed integer to floating point.
4643 class SIToFPInst : public CastInst {
4645 // Note: Instruction needs to be a friend here to call cloneImpl.
4646 friend class Instruction;
4647 /// \brief Clone an identical SIToFPInst
4648 SIToFPInst *cloneImpl() const;
4651 /// \brief Constructor with insert-before-instruction semantics
4653 Value *S, ///< The value to be converted
4654 Type *Ty, ///< The type to convert to
4655 const Twine &NameStr = "", ///< A name for the new instruction
4656 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4659 /// \brief Constructor with insert-at-end-of-block semantics
4661 Value *S, ///< The value to be converted
4662 Type *Ty, ///< The type to convert to
4663 const Twine &NameStr, ///< A name for the new instruction
4664 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4667 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4668 static inline bool classof(const Instruction *I) {
4669 return I->getOpcode() == SIToFP;
4671 static inline bool classof(const Value *V) {
4672 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4676 //===----------------------------------------------------------------------===//
4678 //===----------------------------------------------------------------------===//
4680 /// \brief This class represents a cast from floating point to unsigned integer
4681 class FPToUIInst : public CastInst {
4683 // Note: Instruction needs to be a friend here to call cloneImpl.
4684 friend class Instruction;
4685 /// \brief Clone an identical FPToUIInst
4686 FPToUIInst *cloneImpl() const;
4689 /// \brief Constructor with insert-before-instruction semantics
4691 Value *S, ///< The value to be converted
4692 Type *Ty, ///< The type to convert to
4693 const Twine &NameStr = "", ///< A name for the new instruction
4694 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4697 /// \brief Constructor with insert-at-end-of-block semantics
4699 Value *S, ///< The value to be converted
4700 Type *Ty, ///< The type to convert to
4701 const Twine &NameStr, ///< A name for the new instruction
4702 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
4705 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4706 static inline bool classof(const Instruction *I) {
4707 return I->getOpcode() == FPToUI;
4709 static inline bool classof(const Value *V) {
4710 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4714 //===----------------------------------------------------------------------===//
4716 //===----------------------------------------------------------------------===//
4718 /// \brief This class represents a cast from floating point to signed integer.
4719 class FPToSIInst : public CastInst {
4721 // Note: Instruction needs to be a friend here to call cloneImpl.
4722 friend class Instruction;
4723 /// \brief Clone an identical FPToSIInst
4724 FPToSIInst *cloneImpl() const;
4727 /// \brief Constructor with insert-before-instruction semantics
4729 Value *S, ///< The value to be converted
4730 Type *Ty, ///< The type to convert to
4731 const Twine &NameStr = "", ///< A name for the new instruction
4732 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4735 /// \brief Constructor with insert-at-end-of-block semantics
4737 Value *S, ///< The value to be converted
4738 Type *Ty, ///< The type to convert to
4739 const Twine &NameStr, ///< A name for the new instruction
4740 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4743 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
4744 static inline bool classof(const Instruction *I) {
4745 return I->getOpcode() == FPToSI;
4747 static inline bool classof(const Value *V) {
4748 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4752 //===----------------------------------------------------------------------===//
4753 // IntToPtrInst Class
4754 //===----------------------------------------------------------------------===//
4756 /// \brief This class represents a cast from an integer to a pointer.
4757 class IntToPtrInst : public CastInst {
4759 /// \brief Constructor with insert-before-instruction semantics
4761 Value *S, ///< The value to be converted
4762 Type *Ty, ///< The type to convert to
4763 const Twine &NameStr = "", ///< A name for the new instruction
4764 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4767 /// \brief Constructor with insert-at-end-of-block semantics
4769 Value *S, ///< The value to be converted
4770 Type *Ty, ///< The type to convert to
4771 const Twine &NameStr, ///< A name for the new instruction
4772 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4775 // Note: Instruction needs to be a friend here to call cloneImpl.
4776 friend class Instruction;
4777 /// \brief Clone an identical IntToPtrInst
4778 IntToPtrInst *cloneImpl() const;
4780 /// \brief Returns the address space of this instruction's pointer type.
4781 unsigned getAddressSpace() const {
4782 return getType()->getPointerAddressSpace();
4785 // Methods for support type inquiry through isa, cast, and dyn_cast:
4786 static inline bool classof(const Instruction *I) {
4787 return I->getOpcode() == IntToPtr;
4789 static inline bool classof(const Value *V) {
4790 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4794 //===----------------------------------------------------------------------===//
4795 // PtrToIntInst Class
4796 //===----------------------------------------------------------------------===//
4798 /// \brief This class represents a cast from a pointer to an integer
4799 class PtrToIntInst : public CastInst {
4801 // Note: Instruction needs to be a friend here to call cloneImpl.
4802 friend class Instruction;
4803 /// \brief Clone an identical PtrToIntInst
4804 PtrToIntInst *cloneImpl() const;
4807 /// \brief Constructor with insert-before-instruction semantics
4809 Value *S, ///< The value to be converted
4810 Type *Ty, ///< The type to convert to
4811 const Twine &NameStr = "", ///< A name for the new instruction
4812 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4815 /// \brief Constructor with insert-at-end-of-block semantics
4817 Value *S, ///< The value to be converted
4818 Type *Ty, ///< The type to convert to
4819 const Twine &NameStr, ///< A name for the new instruction
4820 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4823 /// \brief Gets the pointer operand.
4824 Value *getPointerOperand() { return getOperand(0); }
4825 /// \brief Gets the pointer operand.
4826 const Value *getPointerOperand() const { return getOperand(0); }
4827 /// \brief Gets the operand index of the pointer operand.
4828 static unsigned getPointerOperandIndex() { return 0U; }
4830 /// \brief Returns the address space of the pointer operand.
4831 unsigned getPointerAddressSpace() const {
4832 return getPointerOperand()->getType()->getPointerAddressSpace();
4835 // Methods for support type inquiry through isa, cast, and dyn_cast:
4836 static inline bool classof(const Instruction *I) {
4837 return I->getOpcode() == PtrToInt;
4839 static inline bool classof(const Value *V) {
4840 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4844 //===----------------------------------------------------------------------===//
4845 // BitCastInst Class
4846 //===----------------------------------------------------------------------===//
4848 /// \brief This class represents a no-op cast from one type to another.
4849 class BitCastInst : public CastInst {
4851 // Note: Instruction needs to be a friend here to call cloneImpl.
4852 friend class Instruction;
4853 /// \brief Clone an identical BitCastInst
4854 BitCastInst *cloneImpl() const;
4857 /// \brief Constructor with insert-before-instruction semantics
4859 Value *S, ///< The value to be casted
4860 Type *Ty, ///< The type to casted to
4861 const Twine &NameStr = "", ///< A name for the new instruction
4862 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4865 /// \brief Constructor with insert-at-end-of-block semantics
4867 Value *S, ///< The value to be casted
4868 Type *Ty, ///< The type to casted to
4869 const Twine &NameStr, ///< A name for the new instruction
4870 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4873 // Methods for support type inquiry through isa, cast, and dyn_cast:
4874 static inline bool classof(const Instruction *I) {
4875 return I->getOpcode() == BitCast;
4877 static inline bool classof(const Value *V) {
4878 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4882 //===----------------------------------------------------------------------===//
4883 // AddrSpaceCastInst Class
4884 //===----------------------------------------------------------------------===//
4886 /// \brief This class represents a conversion between pointers from
4887 /// one address space to another.
4888 class AddrSpaceCastInst : public CastInst {
4890 // Note: Instruction needs to be a friend here to call cloneImpl.
4891 friend class Instruction;
4892 /// \brief Clone an identical AddrSpaceCastInst
4893 AddrSpaceCastInst *cloneImpl() const;
4896 /// \brief Constructor with insert-before-instruction semantics
4898 Value *S, ///< The value to be casted
4899 Type *Ty, ///< The type to casted to
4900 const Twine &NameStr = "", ///< A name for the new instruction
4901 Instruction *InsertBefore = nullptr ///< Where to insert the new instruction
4904 /// \brief Constructor with insert-at-end-of-block semantics
4906 Value *S, ///< The value to be casted
4907 Type *Ty, ///< The type to casted to
4908 const Twine &NameStr, ///< A name for the new instruction
4909 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
4912 // Methods for support type inquiry through isa, cast, and dyn_cast:
4913 static inline bool classof(const Instruction *I) {
4914 return I->getOpcode() == AddrSpaceCast;
4916 static inline bool classof(const Value *V) {
4917 return isa<Instruction>(V) && classof(cast<Instruction>(V));
4920 /// \brief Gets the pointer operand.
4921 Value *getPointerOperand() {
4922 return getOperand(0);
4925 /// \brief Gets the pointer operand.
4926 const Value *getPointerOperand() const {
4927 return getOperand(0);
4930 /// \brief Gets the operand index of the pointer operand.
4931 static unsigned getPointerOperandIndex() {
4935 /// \brief Returns the address space of the pointer operand.
4936 unsigned getSrcAddressSpace() const {
4937 return getPointerOperand()->getType()->getPointerAddressSpace();
4940 /// \brief Returns the address space of the result.
4941 unsigned getDestAddressSpace() const {
4942 return getType()->getPointerAddressSpace();
4946 } // End llvm namespace