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/iterator_range.h"
21 #include "llvm/ADT/SmallVector.h"
22 #include "llvm/IR/Attributes.h"
23 #include "llvm/IR/CallingConv.h"
24 #include "llvm/IR/DerivedTypes.h"
25 #include "llvm/IR/InstrTypes.h"
26 #include "llvm/Support/ErrorHandling.h"
41 // Consume = 3, // Not specified yet.
45 SequentiallyConsistent = 7
48 enum SynchronizationScope {
53 //===----------------------------------------------------------------------===//
55 //===----------------------------------------------------------------------===//
57 /// AllocaInst - an instruction to allocate memory on the stack
59 class AllocaInst : public UnaryInstruction {
61 AllocaInst *clone_impl() const override;
63 explicit AllocaInst(Type *Ty, Value *ArraySize = 0,
64 const Twine &Name = "", Instruction *InsertBefore = 0);
65 AllocaInst(Type *Ty, Value *ArraySize,
66 const Twine &Name, BasicBlock *InsertAtEnd);
68 AllocaInst(Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
69 AllocaInst(Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
71 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
72 const Twine &Name = "", Instruction *InsertBefore = 0);
73 AllocaInst(Type *Ty, Value *ArraySize, unsigned Align,
74 const Twine &Name, BasicBlock *InsertAtEnd);
76 // Out of line virtual method, so the vtable, etc. has a home.
77 virtual ~AllocaInst();
79 /// isArrayAllocation - Return true if there is an allocation size parameter
80 /// to the allocation instruction that is not 1.
82 bool isArrayAllocation() const;
84 /// getArraySize - Get the number of elements allocated. For a simple
85 /// allocation of a single element, this will return a constant 1 value.
87 const Value *getArraySize() const { return getOperand(0); }
88 Value *getArraySize() { return getOperand(0); }
90 /// getType - Overload to return most specific pointer type
92 PointerType *getType() const {
93 return cast<PointerType>(Instruction::getType());
96 /// getAllocatedType - Return the type that is being allocated by the
99 Type *getAllocatedType() const;
101 /// getAlignment - Return the alignment of the memory that is being allocated
102 /// by the instruction.
104 unsigned getAlignment() const {
105 return (1u << (getSubclassDataFromInstruction() & 31)) >> 1;
107 void setAlignment(unsigned Align);
109 /// isStaticAlloca - Return true if this alloca is in the entry block of the
110 /// function and is a constant size. If so, the code generator will fold it
111 /// into the prolog/epilog code, so it is basically free.
112 bool isStaticAlloca() const;
114 /// \brief Return true if this alloca is used as an inalloca argument to a
115 /// call. Such allocas are never considered static even if they are in the
117 bool isUsedWithInAlloca() const {
118 return getSubclassDataFromInstruction() & 32;
121 /// \brief Specify whether this alloca is used to represent a the arguments to
123 void setUsedWithInAlloca(bool V) {
124 setInstructionSubclassData((getSubclassDataFromInstruction() & ~32) |
128 // Methods for support type inquiry through isa, cast, and dyn_cast:
129 static inline bool classof(const Instruction *I) {
130 return (I->getOpcode() == Instruction::Alloca);
132 static inline bool classof(const Value *V) {
133 return isa<Instruction>(V) && classof(cast<Instruction>(V));
136 // Shadow Instruction::setInstructionSubclassData with a private forwarding
137 // method so that subclasses cannot accidentally use it.
138 void setInstructionSubclassData(unsigned short D) {
139 Instruction::setInstructionSubclassData(D);
144 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
148 /// LoadInst - an instruction for reading from memory. This uses the
149 /// SubclassData field in Value to store whether or not the load is volatile.
151 class LoadInst : public UnaryInstruction {
154 LoadInst *clone_impl() const override;
156 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
157 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
158 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
159 Instruction *InsertBefore = 0);
160 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
161 BasicBlock *InsertAtEnd);
162 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
163 unsigned Align, Instruction *InsertBefore = 0);
164 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
165 unsigned Align, BasicBlock *InsertAtEnd);
166 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
167 unsigned Align, AtomicOrdering Order,
168 SynchronizationScope SynchScope = CrossThread,
169 Instruction *InsertBefore = 0);
170 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
171 unsigned Align, AtomicOrdering Order,
172 SynchronizationScope SynchScope,
173 BasicBlock *InsertAtEnd);
175 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
176 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
177 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
178 bool isVolatile = false, Instruction *InsertBefore = 0);
179 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
180 BasicBlock *InsertAtEnd);
182 /// isVolatile - Return true if this is a load from a volatile memory
185 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
187 /// setVolatile - Specify whether this is a volatile load or not.
189 void setVolatile(bool V) {
190 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
194 /// getAlignment - Return the alignment of the access that is being performed
196 unsigned getAlignment() const {
197 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
200 void setAlignment(unsigned Align);
202 /// Returns the ordering effect of this fence.
203 AtomicOrdering getOrdering() const {
204 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
207 /// Set the ordering constraint on this load. May not be Release or
209 void setOrdering(AtomicOrdering Ordering) {
210 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
214 SynchronizationScope getSynchScope() const {
215 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
218 /// Specify whether this load is ordered with respect to all
219 /// concurrently executing threads, or only with respect to signal handlers
220 /// executing in the same thread.
221 void setSynchScope(SynchronizationScope xthread) {
222 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
226 bool isAtomic() const { return getOrdering() != NotAtomic; }
227 void setAtomic(AtomicOrdering Ordering,
228 SynchronizationScope SynchScope = CrossThread) {
229 setOrdering(Ordering);
230 setSynchScope(SynchScope);
233 bool isSimple() const { return !isAtomic() && !isVolatile(); }
234 bool isUnordered() const {
235 return getOrdering() <= Unordered && !isVolatile();
238 Value *getPointerOperand() { return getOperand(0); }
239 const Value *getPointerOperand() const { return getOperand(0); }
240 static unsigned getPointerOperandIndex() { return 0U; }
242 /// \brief Returns the address space of the pointer operand.
243 unsigned getPointerAddressSpace() const {
244 return getPointerOperand()->getType()->getPointerAddressSpace();
248 // Methods for support type inquiry through isa, cast, and dyn_cast:
249 static inline bool classof(const Instruction *I) {
250 return I->getOpcode() == Instruction::Load;
252 static inline bool classof(const Value *V) {
253 return isa<Instruction>(V) && classof(cast<Instruction>(V));
256 // Shadow Instruction::setInstructionSubclassData with a private forwarding
257 // method so that subclasses cannot accidentally use it.
258 void setInstructionSubclassData(unsigned short D) {
259 Instruction::setInstructionSubclassData(D);
264 //===----------------------------------------------------------------------===//
266 //===----------------------------------------------------------------------===//
268 /// StoreInst - an instruction for storing to memory
270 class StoreInst : public Instruction {
271 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
274 StoreInst *clone_impl() const override;
276 // allocate space for exactly two operands
277 void *operator new(size_t s) {
278 return User::operator new(s, 2);
280 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
281 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
282 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
283 Instruction *InsertBefore = 0);
284 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
285 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
286 unsigned Align, Instruction *InsertBefore = 0);
287 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
288 unsigned Align, BasicBlock *InsertAtEnd);
289 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
290 unsigned Align, AtomicOrdering Order,
291 SynchronizationScope SynchScope = CrossThread,
292 Instruction *InsertBefore = 0);
293 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
294 unsigned Align, AtomicOrdering Order,
295 SynchronizationScope SynchScope,
296 BasicBlock *InsertAtEnd);
299 /// isVolatile - Return true if this is a store to a volatile memory
302 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
304 /// setVolatile - Specify whether this is a volatile store or not.
306 void setVolatile(bool V) {
307 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
311 /// Transparently provide more efficient getOperand methods.
312 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
314 /// getAlignment - Return the alignment of the access that is being performed
316 unsigned getAlignment() const {
317 return (1 << ((getSubclassDataFromInstruction() >> 1) & 31)) >> 1;
320 void setAlignment(unsigned Align);
322 /// Returns the ordering effect of this store.
323 AtomicOrdering getOrdering() const {
324 return AtomicOrdering((getSubclassDataFromInstruction() >> 7) & 7);
327 /// Set the ordering constraint on this store. May not be Acquire or
329 void setOrdering(AtomicOrdering Ordering) {
330 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 7)) |
334 SynchronizationScope getSynchScope() const {
335 return SynchronizationScope((getSubclassDataFromInstruction() >> 6) & 1);
338 /// Specify whether this store instruction is ordered with respect to all
339 /// concurrently executing threads, or only with respect to signal handlers
340 /// executing in the same thread.
341 void setSynchScope(SynchronizationScope xthread) {
342 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(1 << 6)) |
346 bool isAtomic() const { return getOrdering() != NotAtomic; }
347 void setAtomic(AtomicOrdering Ordering,
348 SynchronizationScope SynchScope = CrossThread) {
349 setOrdering(Ordering);
350 setSynchScope(SynchScope);
353 bool isSimple() const { return !isAtomic() && !isVolatile(); }
354 bool isUnordered() const {
355 return getOrdering() <= Unordered && !isVolatile();
358 Value *getValueOperand() { return getOperand(0); }
359 const Value *getValueOperand() const { return getOperand(0); }
361 Value *getPointerOperand() { return getOperand(1); }
362 const Value *getPointerOperand() const { return getOperand(1); }
363 static unsigned getPointerOperandIndex() { return 1U; }
365 /// \brief Returns the address space of the pointer operand.
366 unsigned getPointerAddressSpace() const {
367 return getPointerOperand()->getType()->getPointerAddressSpace();
370 // Methods for support type inquiry through isa, cast, and dyn_cast:
371 static inline bool classof(const Instruction *I) {
372 return I->getOpcode() == Instruction::Store;
374 static inline bool classof(const Value *V) {
375 return isa<Instruction>(V) && classof(cast<Instruction>(V));
378 // Shadow Instruction::setInstructionSubclassData with a private forwarding
379 // method so that subclasses cannot accidentally use it.
380 void setInstructionSubclassData(unsigned short D) {
381 Instruction::setInstructionSubclassData(D);
386 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
389 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
391 //===----------------------------------------------------------------------===//
393 //===----------------------------------------------------------------------===//
395 /// FenceInst - an instruction for ordering other memory operations
397 class FenceInst : public Instruction {
398 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
399 void Init(AtomicOrdering Ordering, SynchronizationScope SynchScope);
401 FenceInst *clone_impl() const override;
403 // allocate space for exactly zero operands
404 void *operator new(size_t s) {
405 return User::operator new(s, 0);
408 // Ordering may only be Acquire, Release, AcquireRelease, or
409 // SequentiallyConsistent.
410 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
411 SynchronizationScope SynchScope = CrossThread,
412 Instruction *InsertBefore = 0);
413 FenceInst(LLVMContext &C, AtomicOrdering Ordering,
414 SynchronizationScope SynchScope,
415 BasicBlock *InsertAtEnd);
417 /// Returns the ordering effect of this fence.
418 AtomicOrdering getOrdering() const {
419 return AtomicOrdering(getSubclassDataFromInstruction() >> 1);
422 /// Set the ordering constraint on this fence. May only be Acquire, Release,
423 /// AcquireRelease, or SequentiallyConsistent.
424 void setOrdering(AtomicOrdering Ordering) {
425 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
429 SynchronizationScope getSynchScope() const {
430 return SynchronizationScope(getSubclassDataFromInstruction() & 1);
433 /// Specify whether this fence orders other operations with respect to all
434 /// concurrently executing threads, or only with respect to signal handlers
435 /// executing in the same thread.
436 void setSynchScope(SynchronizationScope xthread) {
437 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
441 // Methods for support type inquiry through isa, cast, and dyn_cast:
442 static inline bool classof(const Instruction *I) {
443 return I->getOpcode() == Instruction::Fence;
445 static inline bool classof(const Value *V) {
446 return isa<Instruction>(V) && classof(cast<Instruction>(V));
449 // Shadow Instruction::setInstructionSubclassData with a private forwarding
450 // method so that subclasses cannot accidentally use it.
451 void setInstructionSubclassData(unsigned short D) {
452 Instruction::setInstructionSubclassData(D);
456 //===----------------------------------------------------------------------===//
457 // AtomicCmpXchgInst Class
458 //===----------------------------------------------------------------------===//
460 /// AtomicCmpXchgInst - an instruction that atomically checks whether a
461 /// specified value is in a memory location, and, if it is, stores a new value
462 /// there. Returns the value that was loaded.
464 class AtomicCmpXchgInst : public Instruction {
465 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
466 void Init(Value *Ptr, Value *Cmp, Value *NewVal,
467 AtomicOrdering SuccessOrdering, AtomicOrdering FailureOrdering,
468 SynchronizationScope SynchScope);
470 AtomicCmpXchgInst *clone_impl() const override;
472 // allocate space for exactly three operands
473 void *operator new(size_t s) {
474 return User::operator new(s, 3);
476 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
477 AtomicOrdering SuccessOrdering,
478 AtomicOrdering FailureOrdering,
479 SynchronizationScope SynchScope,
480 Instruction *InsertBefore = 0);
481 AtomicCmpXchgInst(Value *Ptr, Value *Cmp, Value *NewVal,
482 AtomicOrdering SuccessOrdering,
483 AtomicOrdering FailureOrdering,
484 SynchronizationScope SynchScope,
485 BasicBlock *InsertAtEnd);
487 /// isVolatile - Return true if this is a cmpxchg from a volatile memory
490 bool isVolatile() const {
491 return getSubclassDataFromInstruction() & 1;
494 /// setVolatile - Specify whether this is a volatile cmpxchg.
496 void setVolatile(bool V) {
497 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
501 /// Transparently provide more efficient getOperand methods.
502 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
504 /// Set the ordering constraint on this cmpxchg.
505 void setSuccessOrdering(AtomicOrdering Ordering) {
506 assert(Ordering != NotAtomic &&
507 "CmpXchg instructions can only be atomic.");
508 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0x1c) |
512 void setFailureOrdering(AtomicOrdering Ordering) {
513 assert(Ordering != NotAtomic &&
514 "CmpXchg instructions can only be atomic.");
515 setInstructionSubclassData((getSubclassDataFromInstruction() & ~0xe0) |
519 /// Specify whether this cmpxchg is atomic and orders other operations with
520 /// respect to all concurrently executing threads, or only with respect to
521 /// signal handlers executing in the same thread.
522 void setSynchScope(SynchronizationScope SynchScope) {
523 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
527 /// Returns the ordering constraint on this cmpxchg.
528 AtomicOrdering getSuccessOrdering() const {
529 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
532 /// Returns the ordering constraint on this cmpxchg.
533 AtomicOrdering getFailureOrdering() const {
534 return AtomicOrdering((getSubclassDataFromInstruction() >> 5) & 7);
537 /// Returns whether this cmpxchg is atomic between threads or only within a
539 SynchronizationScope getSynchScope() const {
540 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
543 Value *getPointerOperand() { return getOperand(0); }
544 const Value *getPointerOperand() const { return getOperand(0); }
545 static unsigned getPointerOperandIndex() { return 0U; }
547 Value *getCompareOperand() { return getOperand(1); }
548 const Value *getCompareOperand() const { return getOperand(1); }
550 Value *getNewValOperand() { return getOperand(2); }
551 const Value *getNewValOperand() const { return getOperand(2); }
553 /// \brief Returns the address space of the pointer operand.
554 unsigned getPointerAddressSpace() const {
555 return getPointerOperand()->getType()->getPointerAddressSpace();
558 /// \brief Returns the strongest permitted ordering on failure, given the
559 /// desired ordering on success.
561 /// If the comparison in a cmpxchg operation fails, there is no atomic store
562 /// so release semantics cannot be provided. So this function drops explicit
563 /// Release requests from the AtomicOrdering. A SequentiallyConsistent
564 /// operation would remain SequentiallyConsistent.
565 static AtomicOrdering
566 getStrongestFailureOrdering(AtomicOrdering SuccessOrdering) {
567 switch (SuccessOrdering) {
568 default: llvm_unreachable("invalid cmpxchg success ordering");
575 case SequentiallyConsistent:
576 return SequentiallyConsistent;
580 // Methods for support type inquiry through isa, cast, and dyn_cast:
581 static inline bool classof(const Instruction *I) {
582 return I->getOpcode() == Instruction::AtomicCmpXchg;
584 static inline bool classof(const Value *V) {
585 return isa<Instruction>(V) && classof(cast<Instruction>(V));
588 // Shadow Instruction::setInstructionSubclassData with a private forwarding
589 // method so that subclasses cannot accidentally use it.
590 void setInstructionSubclassData(unsigned short D) {
591 Instruction::setInstructionSubclassData(D);
596 struct OperandTraits<AtomicCmpXchgInst> :
597 public FixedNumOperandTraits<AtomicCmpXchgInst, 3> {
600 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicCmpXchgInst, Value)
602 //===----------------------------------------------------------------------===//
603 // AtomicRMWInst Class
604 //===----------------------------------------------------------------------===//
606 /// AtomicRMWInst - an instruction that atomically reads a memory location,
607 /// combines it with another value, and then stores the result back. Returns
610 class AtomicRMWInst : public Instruction {
611 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
613 AtomicRMWInst *clone_impl() const override;
615 /// This enumeration lists the possible modifications atomicrmw can make. In
616 /// the descriptions, 'p' is the pointer to the instruction's memory location,
617 /// 'old' is the initial value of *p, and 'v' is the other value passed to the
618 /// instruction. These instructions always return 'old'.
634 /// *p = old >signed v ? old : v
636 /// *p = old <signed v ? old : v
638 /// *p = old >unsigned v ? old : v
640 /// *p = old <unsigned v ? old : v
648 // allocate space for exactly two operands
649 void *operator new(size_t s) {
650 return User::operator new(s, 2);
652 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
653 AtomicOrdering Ordering, SynchronizationScope SynchScope,
654 Instruction *InsertBefore = 0);
655 AtomicRMWInst(BinOp Operation, Value *Ptr, Value *Val,
656 AtomicOrdering Ordering, SynchronizationScope SynchScope,
657 BasicBlock *InsertAtEnd);
659 BinOp getOperation() const {
660 return static_cast<BinOp>(getSubclassDataFromInstruction() >> 5);
663 void setOperation(BinOp Operation) {
664 unsigned short SubclassData = getSubclassDataFromInstruction();
665 setInstructionSubclassData((SubclassData & 31) |
669 /// isVolatile - Return true if this is a RMW on a volatile memory location.
671 bool isVolatile() const {
672 return getSubclassDataFromInstruction() & 1;
675 /// setVolatile - Specify whether this is a volatile RMW or not.
677 void setVolatile(bool V) {
678 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
682 /// Transparently provide more efficient getOperand methods.
683 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
685 /// Set the ordering constraint on this RMW.
686 void setOrdering(AtomicOrdering Ordering) {
687 assert(Ordering != NotAtomic &&
688 "atomicrmw instructions can only be atomic.");
689 setInstructionSubclassData((getSubclassDataFromInstruction() & ~(7 << 2)) |
693 /// Specify whether this RMW orders other operations with respect to all
694 /// concurrently executing threads, or only with respect to signal handlers
695 /// executing in the same thread.
696 void setSynchScope(SynchronizationScope SynchScope) {
697 setInstructionSubclassData((getSubclassDataFromInstruction() & ~2) |
701 /// Returns the ordering constraint on this RMW.
702 AtomicOrdering getOrdering() const {
703 return AtomicOrdering((getSubclassDataFromInstruction() >> 2) & 7);
706 /// Returns whether this RMW is atomic between threads or only within a
708 SynchronizationScope getSynchScope() const {
709 return SynchronizationScope((getSubclassDataFromInstruction() & 2) >> 1);
712 Value *getPointerOperand() { return getOperand(0); }
713 const Value *getPointerOperand() const { return getOperand(0); }
714 static unsigned getPointerOperandIndex() { return 0U; }
716 Value *getValOperand() { return getOperand(1); }
717 const Value *getValOperand() const { return getOperand(1); }
719 /// \brief Returns the address space of the pointer operand.
720 unsigned getPointerAddressSpace() const {
721 return getPointerOperand()->getType()->getPointerAddressSpace();
724 // Methods for support type inquiry through isa, cast, and dyn_cast:
725 static inline bool classof(const Instruction *I) {
726 return I->getOpcode() == Instruction::AtomicRMW;
728 static inline bool classof(const Value *V) {
729 return isa<Instruction>(V) && classof(cast<Instruction>(V));
732 void Init(BinOp Operation, Value *Ptr, Value *Val,
733 AtomicOrdering Ordering, SynchronizationScope SynchScope);
734 // Shadow Instruction::setInstructionSubclassData with a private forwarding
735 // method so that subclasses cannot accidentally use it.
736 void setInstructionSubclassData(unsigned short D) {
737 Instruction::setInstructionSubclassData(D);
742 struct OperandTraits<AtomicRMWInst>
743 : public FixedNumOperandTraits<AtomicRMWInst,2> {
746 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(AtomicRMWInst, Value)
748 //===----------------------------------------------------------------------===//
749 // GetElementPtrInst Class
750 //===----------------------------------------------------------------------===//
752 // checkGEPType - Simple wrapper function to give a better assertion failure
753 // message on bad indexes for a gep instruction.
755 inline Type *checkGEPType(Type *Ty) {
756 assert(Ty && "Invalid GetElementPtrInst indices for type!");
760 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
761 /// access elements of arrays and structs
763 class GetElementPtrInst : public Instruction {
764 GetElementPtrInst(const GetElementPtrInst &GEPI);
765 void init(Value *Ptr, ArrayRef<Value *> IdxList, const Twine &NameStr);
767 /// Constructors - Create a getelementptr instruction with a base pointer an
768 /// list of indices. The first ctor can optionally insert before an existing
769 /// instruction, the second appends the new instruction to the specified
771 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
772 unsigned Values, const Twine &NameStr,
773 Instruction *InsertBefore);
774 inline GetElementPtrInst(Value *Ptr, ArrayRef<Value *> IdxList,
775 unsigned Values, const Twine &NameStr,
776 BasicBlock *InsertAtEnd);
778 GetElementPtrInst *clone_impl() const override;
780 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
781 const Twine &NameStr = "",
782 Instruction *InsertBefore = 0) {
783 unsigned Values = 1 + unsigned(IdxList.size());
785 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertBefore);
787 static GetElementPtrInst *Create(Value *Ptr, ArrayRef<Value *> IdxList,
788 const Twine &NameStr,
789 BasicBlock *InsertAtEnd) {
790 unsigned Values = 1 + unsigned(IdxList.size());
792 GetElementPtrInst(Ptr, IdxList, Values, NameStr, InsertAtEnd);
795 /// Create an "inbounds" getelementptr. See the documentation for the
796 /// "inbounds" flag in LangRef.html for details.
797 static GetElementPtrInst *CreateInBounds(Value *Ptr,
798 ArrayRef<Value *> IdxList,
799 const Twine &NameStr = "",
800 Instruction *InsertBefore = 0) {
801 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertBefore);
802 GEP->setIsInBounds(true);
805 static GetElementPtrInst *CreateInBounds(Value *Ptr,
806 ArrayRef<Value *> IdxList,
807 const Twine &NameStr,
808 BasicBlock *InsertAtEnd) {
809 GetElementPtrInst *GEP = Create(Ptr, IdxList, NameStr, InsertAtEnd);
810 GEP->setIsInBounds(true);
814 /// Transparently provide more efficient getOperand methods.
815 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
817 // getType - Overload to return most specific sequential type.
818 SequentialType *getType() const {
819 return cast<SequentialType>(Instruction::getType());
822 /// \brief Returns the address space of this instruction's pointer type.
823 unsigned getAddressSpace() const {
824 // Note that this is always the same as the pointer operand's address space
825 // and that is cheaper to compute, so cheat here.
826 return getPointerAddressSpace();
829 /// getIndexedType - Returns the type of the element that would be loaded with
830 /// a load instruction with the specified parameters.
832 /// Null is returned if the indices are invalid for the specified
835 static Type *getIndexedType(Type *Ptr, ArrayRef<Value *> IdxList);
836 static Type *getIndexedType(Type *Ptr, ArrayRef<Constant *> IdxList);
837 static Type *getIndexedType(Type *Ptr, ArrayRef<uint64_t> IdxList);
839 inline op_iterator idx_begin() { return op_begin()+1; }
840 inline const_op_iterator idx_begin() const { return op_begin()+1; }
841 inline op_iterator idx_end() { return op_end(); }
842 inline const_op_iterator idx_end() const { return op_end(); }
844 Value *getPointerOperand() {
845 return getOperand(0);
847 const Value *getPointerOperand() const {
848 return getOperand(0);
850 static unsigned getPointerOperandIndex() {
851 return 0U; // get index for modifying correct operand.
854 /// getPointerOperandType - Method to return the pointer operand as a
856 Type *getPointerOperandType() const {
857 return getPointerOperand()->getType();
860 /// \brief Returns the address space of the pointer operand.
861 unsigned getPointerAddressSpace() const {
862 return getPointerOperandType()->getPointerAddressSpace();
865 /// GetGEPReturnType - Returns the pointer type returned by the GEP
866 /// instruction, which may be a vector of pointers.
867 static Type *getGEPReturnType(Value *Ptr, ArrayRef<Value *> IdxList) {
868 Type *PtrTy = PointerType::get(checkGEPType(
869 getIndexedType(Ptr->getType(), IdxList)),
870 Ptr->getType()->getPointerAddressSpace());
872 if (Ptr->getType()->isVectorTy()) {
873 unsigned NumElem = cast<VectorType>(Ptr->getType())->getNumElements();
874 return VectorType::get(PtrTy, NumElem);
881 unsigned getNumIndices() const { // Note: always non-negative
882 return getNumOperands() - 1;
885 bool hasIndices() const {
886 return getNumOperands() > 1;
889 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
890 /// zeros. If so, the result pointer and the first operand have the same
891 /// value, just potentially different types.
892 bool hasAllZeroIndices() const;
894 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
895 /// constant integers. If so, the result pointer and the first operand have
896 /// a constant offset between them.
897 bool hasAllConstantIndices() const;
899 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
900 /// See LangRef.html for the meaning of inbounds on a getelementptr.
901 void setIsInBounds(bool b = true);
903 /// isInBounds - Determine whether the GEP has the inbounds flag.
904 bool isInBounds() const;
906 /// \brief Accumulate the constant address offset of this GEP if possible.
908 /// This routine accepts an APInt into which it will accumulate the constant
909 /// offset of this GEP if the GEP is in fact constant. If the GEP is not
910 /// all-constant, it returns false and the value of the offset APInt is
911 /// undefined (it is *not* preserved!). The APInt passed into this routine
912 /// must be at least as wide as the IntPtr type for the address space of
913 /// the base GEP pointer.
914 bool accumulateConstantOffset(const DataLayout &DL, APInt &Offset) const;
916 // Methods for support type inquiry through isa, cast, and dyn_cast:
917 static inline bool classof(const Instruction *I) {
918 return (I->getOpcode() == Instruction::GetElementPtr);
920 static inline bool classof(const Value *V) {
921 return isa<Instruction>(V) && classof(cast<Instruction>(V));
926 struct OperandTraits<GetElementPtrInst> :
927 public VariadicOperandTraits<GetElementPtrInst, 1> {
930 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
931 ArrayRef<Value *> IdxList,
933 const Twine &NameStr,
934 Instruction *InsertBefore)
935 : Instruction(getGEPReturnType(Ptr, IdxList),
937 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
938 Values, InsertBefore) {
939 init(Ptr, IdxList, NameStr);
941 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
942 ArrayRef<Value *> IdxList,
944 const Twine &NameStr,
945 BasicBlock *InsertAtEnd)
946 : Instruction(getGEPReturnType(Ptr, IdxList),
948 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
949 Values, InsertAtEnd) {
950 init(Ptr, IdxList, NameStr);
954 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
957 //===----------------------------------------------------------------------===//
959 //===----------------------------------------------------------------------===//
961 /// This instruction compares its operands according to the predicate given
962 /// to the constructor. It only operates on integers or pointers. The operands
963 /// must be identical types.
964 /// \brief Represent an integer comparison operator.
965 class ICmpInst: public CmpInst {
967 assert(getPredicate() >= CmpInst::FIRST_ICMP_PREDICATE &&
968 getPredicate() <= CmpInst::LAST_ICMP_PREDICATE &&
969 "Invalid ICmp predicate value");
970 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
971 "Both operands to ICmp instruction are not of the same type!");
972 // Check that the operands are the right type
973 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
974 getOperand(0)->getType()->isPtrOrPtrVectorTy()) &&
975 "Invalid operand types for ICmp instruction");
979 /// \brief Clone an identical ICmpInst
980 ICmpInst *clone_impl() const override;
982 /// \brief Constructor with insert-before-instruction semantics.
984 Instruction *InsertBefore, ///< Where to insert
985 Predicate pred, ///< The predicate to use for the comparison
986 Value *LHS, ///< The left-hand-side of the expression
987 Value *RHS, ///< The right-hand-side of the expression
988 const Twine &NameStr = "" ///< Name of the instruction
989 ) : CmpInst(makeCmpResultType(LHS->getType()),
990 Instruction::ICmp, pred, LHS, RHS, NameStr,
997 /// \brief Constructor with insert-at-end semantics.
999 BasicBlock &InsertAtEnd, ///< Block to insert into.
1000 Predicate pred, ///< The predicate to use for the comparison
1001 Value *LHS, ///< The left-hand-side of the expression
1002 Value *RHS, ///< The right-hand-side of the expression
1003 const Twine &NameStr = "" ///< Name of the instruction
1004 ) : CmpInst(makeCmpResultType(LHS->getType()),
1005 Instruction::ICmp, pred, LHS, RHS, NameStr,
1012 /// \brief Constructor with no-insertion semantics
1014 Predicate pred, ///< The predicate to use for the comparison
1015 Value *LHS, ///< The left-hand-side of the expression
1016 Value *RHS, ///< The right-hand-side of the expression
1017 const Twine &NameStr = "" ///< Name of the instruction
1018 ) : CmpInst(makeCmpResultType(LHS->getType()),
1019 Instruction::ICmp, pred, LHS, RHS, NameStr) {
1025 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
1026 /// @returns the predicate that would be the result if the operand were
1027 /// regarded as signed.
1028 /// \brief Return the signed version of the predicate
1029 Predicate getSignedPredicate() const {
1030 return getSignedPredicate(getPredicate());
1033 /// This is a static version that you can use without an instruction.
1034 /// \brief Return the signed version of the predicate.
1035 static Predicate getSignedPredicate(Predicate pred);
1037 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
1038 /// @returns the predicate that would be the result if the operand were
1039 /// regarded as unsigned.
1040 /// \brief Return the unsigned version of the predicate
1041 Predicate getUnsignedPredicate() const {
1042 return getUnsignedPredicate(getPredicate());
1045 /// This is a static version that you can use without an instruction.
1046 /// \brief Return the unsigned version of the predicate.
1047 static Predicate getUnsignedPredicate(Predicate pred);
1049 /// isEquality - Return true if this predicate is either EQ or NE. This also
1050 /// tests for commutativity.
1051 static bool isEquality(Predicate P) {
1052 return P == ICMP_EQ || P == ICMP_NE;
1055 /// isEquality - Return true if this predicate is either EQ or NE. This also
1056 /// tests for commutativity.
1057 bool isEquality() const {
1058 return isEquality(getPredicate());
1061 /// @returns true if the predicate of this ICmpInst is commutative
1062 /// \brief Determine if this relation is commutative.
1063 bool isCommutative() const { return isEquality(); }
1065 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1067 bool isRelational() const {
1068 return !isEquality();
1071 /// isRelational - Return true if the predicate is relational (not EQ or NE).
1073 static bool isRelational(Predicate P) {
1074 return !isEquality(P);
1077 /// Initialize a set of values that all satisfy the predicate with C.
1078 /// \brief Make a ConstantRange for a relation with a constant value.
1079 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
1081 /// Exchange the two operands to this instruction in such a way that it does
1082 /// not modify the semantics of the instruction. The predicate value may be
1083 /// changed to retain the same result if the predicate is order dependent
1085 /// \brief Swap operands and adjust predicate.
1086 void swapOperands() {
1087 setPredicate(getSwappedPredicate());
1088 Op<0>().swap(Op<1>());
1091 // Methods for support type inquiry through isa, cast, and dyn_cast:
1092 static inline bool classof(const Instruction *I) {
1093 return I->getOpcode() == Instruction::ICmp;
1095 static inline bool classof(const Value *V) {
1096 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1101 //===----------------------------------------------------------------------===//
1103 //===----------------------------------------------------------------------===//
1105 /// This instruction compares its operands according to the predicate given
1106 /// to the constructor. It only operates on floating point values or packed
1107 /// vectors of floating point values. The operands must be identical types.
1108 /// \brief Represents a floating point comparison operator.
1109 class FCmpInst: public CmpInst {
1111 /// \brief Clone an identical FCmpInst
1112 FCmpInst *clone_impl() const override;
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::FCmp, pred, LHS, RHS, NameStr,
1124 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1125 "Invalid FCmp predicate value");
1126 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1127 "Both operands to FCmp instruction are not of the same type!");
1128 // Check that the operands are the right type
1129 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1130 "Invalid operand types for FCmp instruction");
1133 /// \brief Constructor with insert-at-end semantics.
1135 BasicBlock &InsertAtEnd, ///< Block to insert into.
1136 Predicate pred, ///< The predicate to use for the comparison
1137 Value *LHS, ///< The left-hand-side of the expression
1138 Value *RHS, ///< The right-hand-side of the expression
1139 const Twine &NameStr = "" ///< Name of the instruction
1140 ) : CmpInst(makeCmpResultType(LHS->getType()),
1141 Instruction::FCmp, pred, LHS, RHS, NameStr,
1143 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1144 "Invalid FCmp predicate value");
1145 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1146 "Both operands to FCmp instruction are not of the same type!");
1147 // Check that the operands are the right type
1148 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1149 "Invalid operand types for FCmp instruction");
1152 /// \brief Constructor with no-insertion semantics
1154 Predicate pred, ///< The predicate to use for the comparison
1155 Value *LHS, ///< The left-hand-side of the expression
1156 Value *RHS, ///< The right-hand-side of the expression
1157 const Twine &NameStr = "" ///< Name of the instruction
1158 ) : CmpInst(makeCmpResultType(LHS->getType()),
1159 Instruction::FCmp, pred, LHS, RHS, NameStr) {
1160 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
1161 "Invalid FCmp predicate value");
1162 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
1163 "Both operands to FCmp instruction are not of the same type!");
1164 // Check that the operands are the right type
1165 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
1166 "Invalid operand types for FCmp instruction");
1169 /// @returns true if the predicate of this instruction is EQ or NE.
1170 /// \brief Determine if this is an equality predicate.
1171 bool isEquality() const {
1172 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
1173 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
1176 /// @returns true if the predicate of this instruction is commutative.
1177 /// \brief Determine if this is a commutative predicate.
1178 bool isCommutative() const {
1179 return isEquality() ||
1180 getPredicate() == FCMP_FALSE ||
1181 getPredicate() == FCMP_TRUE ||
1182 getPredicate() == FCMP_ORD ||
1183 getPredicate() == FCMP_UNO;
1186 /// @returns true if the predicate is relational (not EQ or NE).
1187 /// \brief Determine if this a relational predicate.
1188 bool isRelational() const { return !isEquality(); }
1190 /// Exchange the two operands to this instruction in such a way that it does
1191 /// not modify the semantics of the instruction. The predicate value may be
1192 /// changed to retain the same result if the predicate is order dependent
1194 /// \brief Swap operands and adjust predicate.
1195 void swapOperands() {
1196 setPredicate(getSwappedPredicate());
1197 Op<0>().swap(Op<1>());
1200 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
1201 static inline bool classof(const Instruction *I) {
1202 return I->getOpcode() == Instruction::FCmp;
1204 static inline bool classof(const Value *V) {
1205 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1209 //===----------------------------------------------------------------------===//
1210 /// CallInst - This class represents a function call, abstracting a target
1211 /// machine's calling convention. This class uses low bit of the SubClassData
1212 /// field to indicate whether or not this is a tail call. The rest of the bits
1213 /// hold the calling convention of the call.
1215 class CallInst : public Instruction {
1216 AttributeSet AttributeList; ///< parameter attributes for call
1217 CallInst(const CallInst &CI);
1218 void init(Value *Func, ArrayRef<Value *> Args, const Twine &NameStr);
1219 void init(Value *Func, const Twine &NameStr);
1221 /// Construct a CallInst given a range of arguments.
1222 /// \brief Construct a CallInst from a range of arguments
1223 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1224 const Twine &NameStr, Instruction *InsertBefore);
1226 /// Construct a CallInst given a range of arguments.
1227 /// \brief Construct a CallInst from a range of arguments
1228 inline CallInst(Value *Func, ArrayRef<Value *> Args,
1229 const Twine &NameStr, BasicBlock *InsertAtEnd);
1231 explicit CallInst(Value *F, const Twine &NameStr,
1232 Instruction *InsertBefore);
1233 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1235 CallInst *clone_impl() const override;
1237 static CallInst *Create(Value *Func,
1238 ArrayRef<Value *> Args,
1239 const Twine &NameStr = "",
1240 Instruction *InsertBefore = 0) {
1241 return new(unsigned(Args.size() + 1))
1242 CallInst(Func, Args, NameStr, InsertBefore);
1244 static CallInst *Create(Value *Func,
1245 ArrayRef<Value *> Args,
1246 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1247 return new(unsigned(Args.size() + 1))
1248 CallInst(Func, Args, NameStr, InsertAtEnd);
1250 static CallInst *Create(Value *F, const Twine &NameStr = "",
1251 Instruction *InsertBefore = 0) {
1252 return new(1) CallInst(F, NameStr, InsertBefore);
1254 static CallInst *Create(Value *F, const Twine &NameStr,
1255 BasicBlock *InsertAtEnd) {
1256 return new(1) CallInst(F, NameStr, InsertAtEnd);
1258 /// CreateMalloc - Generate the IR for a call to malloc:
1259 /// 1. Compute the malloc call's argument as the specified type's size,
1260 /// possibly multiplied by the array size if the array size is not
1262 /// 2. Call malloc with that argument.
1263 /// 3. Bitcast the result of the malloc call to the specified type.
1264 static Instruction *CreateMalloc(Instruction *InsertBefore,
1265 Type *IntPtrTy, Type *AllocTy,
1266 Value *AllocSize, Value *ArraySize = 0,
1267 Function* MallocF = 0,
1268 const Twine &Name = "");
1269 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1270 Type *IntPtrTy, Type *AllocTy,
1271 Value *AllocSize, Value *ArraySize = 0,
1272 Function* MallocF = 0,
1273 const Twine &Name = "");
1274 /// CreateFree - Generate the IR for a call to the builtin free function.
1275 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
1276 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
1280 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
1281 void setTailCall(bool isTC = true) {
1282 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
1286 /// Provide fast operand accessors
1287 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1289 /// getNumArgOperands - Return the number of call arguments.
1291 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
1293 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
1295 Value *getArgOperand(unsigned i) const { return getOperand(i); }
1296 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
1298 /// arg_operands - iteration adapter for range-for loops.
1299 iterator_range<op_iterator> arg_operands() {
1300 return iterator_range<op_iterator>(op_begin(), op_end());
1303 /// arg_operands - iteration adapter for range-for loops.
1304 iterator_range<const_op_iterator> arg_operands() const {
1305 return iterator_range<const_op_iterator>(op_begin(), op_end());
1308 /// \brief Wrappers for getting the \c Use of a call argument.
1309 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
1310 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
1312 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1314 CallingConv::ID getCallingConv() const {
1315 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
1317 void setCallingConv(CallingConv::ID CC) {
1318 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
1319 (static_cast<unsigned>(CC) << 1));
1322 /// getAttributes - Return the parameter attributes for this call.
1324 const AttributeSet &getAttributes() const { return AttributeList; }
1326 /// setAttributes - Set the parameter attributes for this call.
1328 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
1330 /// addAttribute - adds the attribute to the list of attributes.
1331 void addAttribute(unsigned i, Attribute::AttrKind attr);
1333 /// removeAttribute - removes the attribute from the list of attributes.
1334 void removeAttribute(unsigned i, Attribute attr);
1336 /// \brief Determine whether this call has the given attribute.
1337 bool hasFnAttr(Attribute::AttrKind A) const {
1338 assert(A != Attribute::NoBuiltin &&
1339 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
1340 return hasFnAttrImpl(A);
1343 /// \brief Determine whether the call or the callee has the given attributes.
1344 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
1346 /// \brief Extract the alignment for a call or parameter (0=unknown).
1347 unsigned getParamAlignment(unsigned i) const {
1348 return AttributeList.getParamAlignment(i);
1351 /// \brief Return true if the call should not be treated as a call to a
1353 bool isNoBuiltin() const {
1354 return hasFnAttrImpl(Attribute::NoBuiltin) &&
1355 !hasFnAttrImpl(Attribute::Builtin);
1358 /// \brief Return true if the call should not be inlined.
1359 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
1360 void setIsNoInline() {
1361 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
1364 /// \brief Return true if the call can return twice
1365 bool canReturnTwice() const {
1366 return hasFnAttr(Attribute::ReturnsTwice);
1368 void setCanReturnTwice() {
1369 addAttribute(AttributeSet::FunctionIndex, Attribute::ReturnsTwice);
1372 /// \brief Determine if the call does not access memory.
1373 bool doesNotAccessMemory() const {
1374 return hasFnAttr(Attribute::ReadNone);
1376 void setDoesNotAccessMemory() {
1377 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
1380 /// \brief Determine if the call does not access or only reads memory.
1381 bool onlyReadsMemory() const {
1382 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
1384 void setOnlyReadsMemory() {
1385 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
1388 /// \brief Determine if the call cannot return.
1389 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
1390 void setDoesNotReturn() {
1391 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
1394 /// \brief Determine if the call cannot unwind.
1395 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
1396 void setDoesNotThrow() {
1397 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
1400 /// \brief Determine if the call cannot be duplicated.
1401 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
1402 void setCannotDuplicate() {
1403 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
1406 /// \brief Determine if the call returns a structure through first
1407 /// pointer argument.
1408 bool hasStructRetAttr() const {
1409 // Be friendly and also check the callee.
1410 return paramHasAttr(1, Attribute::StructRet);
1413 /// \brief Determine if any call argument is an aggregate passed by value.
1414 bool hasByValArgument() const {
1415 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1418 /// getCalledFunction - Return the function called, or null if this is an
1419 /// indirect function invocation.
1421 Function *getCalledFunction() const {
1422 return dyn_cast<Function>(Op<-1>());
1425 /// getCalledValue - Get a pointer to the function that is invoked by this
1427 const Value *getCalledValue() const { return Op<-1>(); }
1428 Value *getCalledValue() { return Op<-1>(); }
1430 /// setCalledFunction - Set the function called.
1431 void setCalledFunction(Value* Fn) {
1435 /// isInlineAsm - Check if this call is an inline asm statement.
1436 bool isInlineAsm() const {
1437 return isa<InlineAsm>(Op<-1>());
1440 // Methods for support type inquiry through isa, cast, and dyn_cast:
1441 static inline bool classof(const Instruction *I) {
1442 return I->getOpcode() == Instruction::Call;
1444 static inline bool classof(const Value *V) {
1445 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1449 bool hasFnAttrImpl(Attribute::AttrKind A) const;
1451 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1452 // method so that subclasses cannot accidentally use it.
1453 void setInstructionSubclassData(unsigned short D) {
1454 Instruction::setInstructionSubclassData(D);
1459 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1462 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1463 const Twine &NameStr, BasicBlock *InsertAtEnd)
1464 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1465 ->getElementType())->getReturnType(),
1467 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1468 unsigned(Args.size() + 1), InsertAtEnd) {
1469 init(Func, Args, NameStr);
1472 CallInst::CallInst(Value *Func, ArrayRef<Value *> Args,
1473 const Twine &NameStr, Instruction *InsertBefore)
1474 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1475 ->getElementType())->getReturnType(),
1477 OperandTraits<CallInst>::op_end(this) - (Args.size() + 1),
1478 unsigned(Args.size() + 1), InsertBefore) {
1479 init(Func, Args, NameStr);
1483 // Note: if you get compile errors about private methods then
1484 // please update your code to use the high-level operand
1485 // interfaces. See line 943 above.
1486 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1488 //===----------------------------------------------------------------------===//
1490 //===----------------------------------------------------------------------===//
1492 /// SelectInst - This class represents the LLVM 'select' instruction.
1494 class SelectInst : public Instruction {
1495 void init(Value *C, Value *S1, Value *S2) {
1496 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1502 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1503 Instruction *InsertBefore)
1504 : Instruction(S1->getType(), Instruction::Select,
1505 &Op<0>(), 3, InsertBefore) {
1509 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1510 BasicBlock *InsertAtEnd)
1511 : Instruction(S1->getType(), Instruction::Select,
1512 &Op<0>(), 3, InsertAtEnd) {
1517 SelectInst *clone_impl() const override;
1519 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1520 const Twine &NameStr = "",
1521 Instruction *InsertBefore = 0) {
1522 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1524 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1525 const Twine &NameStr,
1526 BasicBlock *InsertAtEnd) {
1527 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1530 const Value *getCondition() const { return Op<0>(); }
1531 const Value *getTrueValue() const { return Op<1>(); }
1532 const Value *getFalseValue() const { return Op<2>(); }
1533 Value *getCondition() { return Op<0>(); }
1534 Value *getTrueValue() { return Op<1>(); }
1535 Value *getFalseValue() { return Op<2>(); }
1537 /// areInvalidOperands - Return a string if the specified operands are invalid
1538 /// for a select operation, otherwise return null.
1539 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1541 /// Transparently provide more efficient getOperand methods.
1542 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1544 OtherOps getOpcode() const {
1545 return static_cast<OtherOps>(Instruction::getOpcode());
1548 // Methods for support type inquiry through isa, cast, and dyn_cast:
1549 static inline bool classof(const Instruction *I) {
1550 return I->getOpcode() == Instruction::Select;
1552 static inline bool classof(const Value *V) {
1553 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1558 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1561 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1563 //===----------------------------------------------------------------------===//
1565 //===----------------------------------------------------------------------===//
1567 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1568 /// an argument of the specified type given a va_list and increments that list
1570 class VAArgInst : public UnaryInstruction {
1572 VAArgInst *clone_impl() const override;
1575 VAArgInst(Value *List, Type *Ty, const Twine &NameStr = "",
1576 Instruction *InsertBefore = 0)
1577 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1580 VAArgInst(Value *List, Type *Ty, const Twine &NameStr,
1581 BasicBlock *InsertAtEnd)
1582 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1586 Value *getPointerOperand() { return getOperand(0); }
1587 const Value *getPointerOperand() const { return getOperand(0); }
1588 static unsigned getPointerOperandIndex() { return 0U; }
1590 // Methods for support type inquiry through isa, cast, and dyn_cast:
1591 static inline bool classof(const Instruction *I) {
1592 return I->getOpcode() == VAArg;
1594 static inline bool classof(const Value *V) {
1595 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1599 //===----------------------------------------------------------------------===//
1600 // ExtractElementInst Class
1601 //===----------------------------------------------------------------------===//
1603 /// ExtractElementInst - This instruction extracts a single (scalar)
1604 /// element from a VectorType value
1606 class ExtractElementInst : public Instruction {
1607 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1608 Instruction *InsertBefore = 0);
1609 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1610 BasicBlock *InsertAtEnd);
1612 ExtractElementInst *clone_impl() const override;
1615 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1616 const Twine &NameStr = "",
1617 Instruction *InsertBefore = 0) {
1618 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1620 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1621 const Twine &NameStr,
1622 BasicBlock *InsertAtEnd) {
1623 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1626 /// isValidOperands - Return true if an extractelement instruction can be
1627 /// formed with the specified operands.
1628 static bool isValidOperands(const Value *Vec, const Value *Idx);
1630 Value *getVectorOperand() { return Op<0>(); }
1631 Value *getIndexOperand() { return Op<1>(); }
1632 const Value *getVectorOperand() const { return Op<0>(); }
1633 const Value *getIndexOperand() const { return Op<1>(); }
1635 VectorType *getVectorOperandType() const {
1636 return cast<VectorType>(getVectorOperand()->getType());
1640 /// Transparently provide more efficient getOperand methods.
1641 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1643 // Methods for support type inquiry through isa, cast, and dyn_cast:
1644 static inline bool classof(const Instruction *I) {
1645 return I->getOpcode() == Instruction::ExtractElement;
1647 static inline bool classof(const Value *V) {
1648 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1653 struct OperandTraits<ExtractElementInst> :
1654 public FixedNumOperandTraits<ExtractElementInst, 2> {
1657 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1659 //===----------------------------------------------------------------------===//
1660 // InsertElementInst Class
1661 //===----------------------------------------------------------------------===//
1663 /// InsertElementInst - This instruction inserts a single (scalar)
1664 /// element into a VectorType value
1666 class InsertElementInst : public Instruction {
1667 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1668 const Twine &NameStr = "",
1669 Instruction *InsertBefore = 0);
1670 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1671 const Twine &NameStr, BasicBlock *InsertAtEnd);
1673 InsertElementInst *clone_impl() const override;
1676 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1677 const Twine &NameStr = "",
1678 Instruction *InsertBefore = 0) {
1679 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1681 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1682 const Twine &NameStr,
1683 BasicBlock *InsertAtEnd) {
1684 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1687 /// isValidOperands - Return true if an insertelement instruction can be
1688 /// formed with the specified operands.
1689 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1692 /// getType - Overload to return most specific vector type.
1694 VectorType *getType() const {
1695 return cast<VectorType>(Instruction::getType());
1698 /// Transparently provide more efficient getOperand methods.
1699 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1701 // Methods for support type inquiry through isa, cast, and dyn_cast:
1702 static inline bool classof(const Instruction *I) {
1703 return I->getOpcode() == Instruction::InsertElement;
1705 static inline bool classof(const Value *V) {
1706 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1711 struct OperandTraits<InsertElementInst> :
1712 public FixedNumOperandTraits<InsertElementInst, 3> {
1715 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1717 //===----------------------------------------------------------------------===//
1718 // ShuffleVectorInst Class
1719 //===----------------------------------------------------------------------===//
1721 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1724 class ShuffleVectorInst : public Instruction {
1726 ShuffleVectorInst *clone_impl() const override;
1729 // allocate space for exactly three operands
1730 void *operator new(size_t s) {
1731 return User::operator new(s, 3);
1733 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1734 const Twine &NameStr = "",
1735 Instruction *InsertBefor = 0);
1736 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1737 const Twine &NameStr, BasicBlock *InsertAtEnd);
1739 /// isValidOperands - Return true if a shufflevector instruction can be
1740 /// formed with the specified operands.
1741 static bool isValidOperands(const Value *V1, const Value *V2,
1744 /// getType - Overload to return most specific vector type.
1746 VectorType *getType() const {
1747 return cast<VectorType>(Instruction::getType());
1750 /// Transparently provide more efficient getOperand methods.
1751 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1753 Constant *getMask() const {
1754 return cast<Constant>(getOperand(2));
1757 /// getMaskValue - Return the index from the shuffle mask for the specified
1758 /// output result. This is either -1 if the element is undef or a number less
1759 /// than 2*numelements.
1760 static int getMaskValue(Constant *Mask, unsigned i);
1762 int getMaskValue(unsigned i) const {
1763 return getMaskValue(getMask(), i);
1766 /// getShuffleMask - Return the full mask for this instruction, where each
1767 /// element is the element number and undef's are returned as -1.
1768 static void getShuffleMask(Constant *Mask, SmallVectorImpl<int> &Result);
1770 void getShuffleMask(SmallVectorImpl<int> &Result) const {
1771 return getShuffleMask(getMask(), Result);
1774 SmallVector<int, 16> getShuffleMask() const {
1775 SmallVector<int, 16> Mask;
1776 getShuffleMask(Mask);
1781 // Methods for support type inquiry through isa, cast, and dyn_cast:
1782 static inline bool classof(const Instruction *I) {
1783 return I->getOpcode() == Instruction::ShuffleVector;
1785 static inline bool classof(const Value *V) {
1786 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1791 struct OperandTraits<ShuffleVectorInst> :
1792 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1795 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1797 //===----------------------------------------------------------------------===//
1798 // ExtractValueInst Class
1799 //===----------------------------------------------------------------------===//
1801 /// ExtractValueInst - This instruction extracts a struct member or array
1802 /// element value from an aggregate value.
1804 class ExtractValueInst : public UnaryInstruction {
1805 SmallVector<unsigned, 4> Indices;
1807 ExtractValueInst(const ExtractValueInst &EVI);
1808 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1810 /// Constructors - Create a extractvalue instruction with a base aggregate
1811 /// value and a list of indices. The first ctor can optionally insert before
1812 /// an existing instruction, the second appends the new instruction to the
1813 /// specified BasicBlock.
1814 inline ExtractValueInst(Value *Agg,
1815 ArrayRef<unsigned> Idxs,
1816 const Twine &NameStr,
1817 Instruction *InsertBefore);
1818 inline ExtractValueInst(Value *Agg,
1819 ArrayRef<unsigned> Idxs,
1820 const Twine &NameStr, BasicBlock *InsertAtEnd);
1822 // allocate space for exactly one operand
1823 void *operator new(size_t s) {
1824 return User::operator new(s, 1);
1827 ExtractValueInst *clone_impl() const override;
1830 static ExtractValueInst *Create(Value *Agg,
1831 ArrayRef<unsigned> Idxs,
1832 const Twine &NameStr = "",
1833 Instruction *InsertBefore = 0) {
1835 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1837 static ExtractValueInst *Create(Value *Agg,
1838 ArrayRef<unsigned> Idxs,
1839 const Twine &NameStr,
1840 BasicBlock *InsertAtEnd) {
1841 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1844 /// getIndexedType - Returns the type of the element that would be extracted
1845 /// with an extractvalue instruction with the specified parameters.
1847 /// Null is returned if the indices are invalid for the specified type.
1848 static Type *getIndexedType(Type *Agg, ArrayRef<unsigned> Idxs);
1850 typedef const unsigned* idx_iterator;
1851 inline idx_iterator idx_begin() const { return Indices.begin(); }
1852 inline idx_iterator idx_end() const { return Indices.end(); }
1854 Value *getAggregateOperand() {
1855 return getOperand(0);
1857 const Value *getAggregateOperand() const {
1858 return getOperand(0);
1860 static unsigned getAggregateOperandIndex() {
1861 return 0U; // get index for modifying correct operand
1864 ArrayRef<unsigned> getIndices() const {
1868 unsigned getNumIndices() const {
1869 return (unsigned)Indices.size();
1872 bool hasIndices() const {
1876 // Methods for support type inquiry through isa, cast, and dyn_cast:
1877 static inline bool classof(const Instruction *I) {
1878 return I->getOpcode() == Instruction::ExtractValue;
1880 static inline bool classof(const Value *V) {
1881 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1885 ExtractValueInst::ExtractValueInst(Value *Agg,
1886 ArrayRef<unsigned> Idxs,
1887 const Twine &NameStr,
1888 Instruction *InsertBefore)
1889 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1890 ExtractValue, Agg, InsertBefore) {
1891 init(Idxs, NameStr);
1893 ExtractValueInst::ExtractValueInst(Value *Agg,
1894 ArrayRef<unsigned> Idxs,
1895 const Twine &NameStr,
1896 BasicBlock *InsertAtEnd)
1897 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1898 ExtractValue, Agg, InsertAtEnd) {
1899 init(Idxs, NameStr);
1903 //===----------------------------------------------------------------------===//
1904 // InsertValueInst Class
1905 //===----------------------------------------------------------------------===//
1907 /// InsertValueInst - This instruction inserts a struct field of array element
1908 /// value into an aggregate value.
1910 class InsertValueInst : public Instruction {
1911 SmallVector<unsigned, 4> Indices;
1913 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
1914 InsertValueInst(const InsertValueInst &IVI);
1915 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1916 const Twine &NameStr);
1918 /// Constructors - Create a insertvalue instruction with a base aggregate
1919 /// value, a value to insert, and a list of indices. The first ctor can
1920 /// optionally insert before an existing instruction, the second appends
1921 /// the new instruction to the specified BasicBlock.
1922 inline InsertValueInst(Value *Agg, Value *Val,
1923 ArrayRef<unsigned> Idxs,
1924 const Twine &NameStr,
1925 Instruction *InsertBefore);
1926 inline InsertValueInst(Value *Agg, Value *Val,
1927 ArrayRef<unsigned> Idxs,
1928 const Twine &NameStr, BasicBlock *InsertAtEnd);
1930 /// Constructors - These two constructors are convenience methods because one
1931 /// and two index insertvalue instructions are so common.
1932 InsertValueInst(Value *Agg, Value *Val,
1933 unsigned Idx, const Twine &NameStr = "",
1934 Instruction *InsertBefore = 0);
1935 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1936 const Twine &NameStr, BasicBlock *InsertAtEnd);
1938 InsertValueInst *clone_impl() const override;
1940 // allocate space for exactly two operands
1941 void *operator new(size_t s) {
1942 return User::operator new(s, 2);
1945 static InsertValueInst *Create(Value *Agg, Value *Val,
1946 ArrayRef<unsigned> Idxs,
1947 const Twine &NameStr = "",
1948 Instruction *InsertBefore = 0) {
1949 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1951 static InsertValueInst *Create(Value *Agg, Value *Val,
1952 ArrayRef<unsigned> Idxs,
1953 const Twine &NameStr,
1954 BasicBlock *InsertAtEnd) {
1955 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1958 /// Transparently provide more efficient getOperand methods.
1959 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1961 typedef const unsigned* idx_iterator;
1962 inline idx_iterator idx_begin() const { return Indices.begin(); }
1963 inline idx_iterator idx_end() const { return Indices.end(); }
1965 Value *getAggregateOperand() {
1966 return getOperand(0);
1968 const Value *getAggregateOperand() const {
1969 return getOperand(0);
1971 static unsigned getAggregateOperandIndex() {
1972 return 0U; // get index for modifying correct operand
1975 Value *getInsertedValueOperand() {
1976 return getOperand(1);
1978 const Value *getInsertedValueOperand() const {
1979 return getOperand(1);
1981 static unsigned getInsertedValueOperandIndex() {
1982 return 1U; // get index for modifying correct operand
1985 ArrayRef<unsigned> getIndices() const {
1989 unsigned getNumIndices() const {
1990 return (unsigned)Indices.size();
1993 bool hasIndices() const {
1997 // Methods for support type inquiry through isa, cast, and dyn_cast:
1998 static inline bool classof(const Instruction *I) {
1999 return I->getOpcode() == Instruction::InsertValue;
2001 static inline bool classof(const Value *V) {
2002 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2007 struct OperandTraits<InsertValueInst> :
2008 public FixedNumOperandTraits<InsertValueInst, 2> {
2011 InsertValueInst::InsertValueInst(Value *Agg,
2013 ArrayRef<unsigned> Idxs,
2014 const Twine &NameStr,
2015 Instruction *InsertBefore)
2016 : Instruction(Agg->getType(), InsertValue,
2017 OperandTraits<InsertValueInst>::op_begin(this),
2019 init(Agg, Val, Idxs, NameStr);
2021 InsertValueInst::InsertValueInst(Value *Agg,
2023 ArrayRef<unsigned> Idxs,
2024 const Twine &NameStr,
2025 BasicBlock *InsertAtEnd)
2026 : Instruction(Agg->getType(), InsertValue,
2027 OperandTraits<InsertValueInst>::op_begin(this),
2029 init(Agg, Val, Idxs, NameStr);
2032 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
2034 //===----------------------------------------------------------------------===//
2036 //===----------------------------------------------------------------------===//
2038 // PHINode - The PHINode class is used to represent the magical mystical PHI
2039 // node, that can not exist in nature, but can be synthesized in a computer
2040 // scientist's overactive imagination.
2042 class PHINode : public Instruction {
2043 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2044 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2045 /// the number actually in use.
2046 unsigned ReservedSpace;
2047 PHINode(const PHINode &PN);
2048 // allocate space for exactly zero operands
2049 void *operator new(size_t s) {
2050 return User::operator new(s, 0);
2052 explicit PHINode(Type *Ty, unsigned NumReservedValues,
2053 const Twine &NameStr = "", Instruction *InsertBefore = 0)
2054 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
2055 ReservedSpace(NumReservedValues) {
2057 OperandList = allocHungoffUses(ReservedSpace);
2060 PHINode(Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
2061 BasicBlock *InsertAtEnd)
2062 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
2063 ReservedSpace(NumReservedValues) {
2065 OperandList = allocHungoffUses(ReservedSpace);
2068 // allocHungoffUses - this is more complicated than the generic
2069 // User::allocHungoffUses, because we have to allocate Uses for the incoming
2070 // values and pointers to the incoming blocks, all in one allocation.
2071 Use *allocHungoffUses(unsigned) const;
2073 PHINode *clone_impl() const override;
2075 /// Constructors - NumReservedValues is a hint for the number of incoming
2076 /// edges that this phi node will have (use 0 if you really have no idea).
2077 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2078 const Twine &NameStr = "",
2079 Instruction *InsertBefore = 0) {
2080 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
2082 static PHINode *Create(Type *Ty, unsigned NumReservedValues,
2083 const Twine &NameStr, BasicBlock *InsertAtEnd) {
2084 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
2088 /// Provide fast operand accessors
2089 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2091 // Block iterator interface. This provides access to the list of incoming
2092 // basic blocks, which parallels the list of incoming values.
2094 typedef BasicBlock **block_iterator;
2095 typedef BasicBlock * const *const_block_iterator;
2097 block_iterator block_begin() {
2099 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
2100 return reinterpret_cast<block_iterator>(ref + 1);
2103 const_block_iterator block_begin() const {
2104 const Use::UserRef *ref =
2105 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
2106 return reinterpret_cast<const_block_iterator>(ref + 1);
2109 block_iterator block_end() {
2110 return block_begin() + getNumOperands();
2113 const_block_iterator block_end() const {
2114 return block_begin() + getNumOperands();
2117 /// getNumIncomingValues - Return the number of incoming edges
2119 unsigned getNumIncomingValues() const { return getNumOperands(); }
2121 /// getIncomingValue - Return incoming value number x
2123 Value *getIncomingValue(unsigned i) const {
2124 return getOperand(i);
2126 void setIncomingValue(unsigned i, Value *V) {
2129 static unsigned getOperandNumForIncomingValue(unsigned i) {
2132 static unsigned getIncomingValueNumForOperand(unsigned i) {
2136 /// getIncomingBlock - Return incoming basic block number @p i.
2138 BasicBlock *getIncomingBlock(unsigned i) const {
2139 return block_begin()[i];
2142 /// getIncomingBlock - Return incoming basic block corresponding
2143 /// to an operand of the PHI.
2145 BasicBlock *getIncomingBlock(const Use &U) const {
2146 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
2147 return getIncomingBlock(unsigned(&U - op_begin()));
2150 /// getIncomingBlock - Return incoming basic block corresponding
2151 /// to value use iterator.
2153 BasicBlock *getIncomingBlock(Value::const_user_iterator I) const {
2154 return getIncomingBlock(I.getUse());
2157 void setIncomingBlock(unsigned i, BasicBlock *BB) {
2158 block_begin()[i] = BB;
2161 /// addIncoming - Add an incoming value to the end of the PHI list
2163 void addIncoming(Value *V, BasicBlock *BB) {
2164 assert(V && "PHI node got a null value!");
2165 assert(BB && "PHI node got a null basic block!");
2166 assert(getType() == V->getType() &&
2167 "All operands to PHI node must be the same type as the PHI node!");
2168 if (NumOperands == ReservedSpace)
2169 growOperands(); // Get more space!
2170 // Initialize some new operands.
2172 setIncomingValue(NumOperands - 1, V);
2173 setIncomingBlock(NumOperands - 1, BB);
2176 /// removeIncomingValue - Remove an incoming value. This is useful if a
2177 /// predecessor basic block is deleted. The value removed is returned.
2179 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
2180 /// is true), the PHI node is destroyed and any uses of it are replaced with
2181 /// dummy values. The only time there should be zero incoming values to a PHI
2182 /// node is when the block is dead, so this strategy is sound.
2184 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
2186 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
2187 int Idx = getBasicBlockIndex(BB);
2188 assert(Idx >= 0 && "Invalid basic block argument to remove!");
2189 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2192 /// getBasicBlockIndex - Return the first index of the specified basic
2193 /// block in the value list for this PHI. Returns -1 if no instance.
2195 int getBasicBlockIndex(const BasicBlock *BB) const {
2196 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
2197 if (block_begin()[i] == BB)
2202 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2203 int Idx = getBasicBlockIndex(BB);
2204 assert(Idx >= 0 && "Invalid basic block argument!");
2205 return getIncomingValue(Idx);
2208 /// hasConstantValue - If the specified PHI node always merges together the
2209 /// same value, return the value, otherwise return null.
2210 Value *hasConstantValue() const;
2212 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2213 static inline bool classof(const Instruction *I) {
2214 return I->getOpcode() == Instruction::PHI;
2216 static inline bool classof(const Value *V) {
2217 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2220 void growOperands();
2224 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2227 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2229 //===----------------------------------------------------------------------===//
2230 // LandingPadInst Class
2231 //===----------------------------------------------------------------------===//
2233 //===---------------------------------------------------------------------------
2234 /// LandingPadInst - The landingpad instruction holds all of the information
2235 /// necessary to generate correct exception handling. The landingpad instruction
2236 /// cannot be moved from the top of a landing pad block, which itself is
2237 /// accessible only from the 'unwind' edge of an invoke. This uses the
2238 /// SubclassData field in Value to store whether or not the landingpad is a
2241 class LandingPadInst : public Instruction {
2242 /// ReservedSpace - The number of operands actually allocated. NumOperands is
2243 /// the number actually in use.
2244 unsigned ReservedSpace;
2245 LandingPadInst(const LandingPadInst &LP);
2247 enum ClauseType { Catch, Filter };
2249 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2250 // Allocate space for exactly zero operands.
2251 void *operator new(size_t s) {
2252 return User::operator new(s, 0);
2254 void growOperands(unsigned Size);
2255 void init(Value *PersFn, unsigned NumReservedValues, const Twine &NameStr);
2257 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2258 unsigned NumReservedValues, const Twine &NameStr,
2259 Instruction *InsertBefore);
2260 explicit LandingPadInst(Type *RetTy, Value *PersonalityFn,
2261 unsigned NumReservedValues, const Twine &NameStr,
2262 BasicBlock *InsertAtEnd);
2264 LandingPadInst *clone_impl() const override;
2266 /// Constructors - NumReservedClauses is a hint for the number of incoming
2267 /// clauses that this landingpad will have (use 0 if you really have no idea).
2268 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2269 unsigned NumReservedClauses,
2270 const Twine &NameStr = "",
2271 Instruction *InsertBefore = 0);
2272 static LandingPadInst *Create(Type *RetTy, Value *PersonalityFn,
2273 unsigned NumReservedClauses,
2274 const Twine &NameStr, BasicBlock *InsertAtEnd);
2277 /// Provide fast operand accessors
2278 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2280 /// getPersonalityFn - Get the personality function associated with this
2282 Value *getPersonalityFn() const { return getOperand(0); }
2284 /// isCleanup - Return 'true' if this landingpad instruction is a
2285 /// cleanup. I.e., it should be run when unwinding even if its landing pad
2286 /// doesn't catch the exception.
2287 bool isCleanup() const { return getSubclassDataFromInstruction() & 1; }
2289 /// setCleanup - Indicate that this landingpad instruction is a cleanup.
2290 void setCleanup(bool V) {
2291 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
2295 /// addClause - Add a catch or filter clause to the landing pad.
2296 void addClause(Value *ClauseVal);
2298 /// getClause - Get the value of the clause at index Idx. Use isCatch/isFilter
2299 /// to determine what type of clause this is.
2300 Value *getClause(unsigned Idx) const { return OperandList[Idx + 1]; }
2302 /// isCatch - Return 'true' if the clause and index Idx is a catch clause.
2303 bool isCatch(unsigned Idx) const {
2304 return !isa<ArrayType>(OperandList[Idx + 1]->getType());
2307 /// isFilter - Return 'true' if the clause and index Idx is a filter clause.
2308 bool isFilter(unsigned Idx) const {
2309 return isa<ArrayType>(OperandList[Idx + 1]->getType());
2312 /// getNumClauses - Get the number of clauses for this landing pad.
2313 unsigned getNumClauses() const { return getNumOperands() - 1; }
2315 /// reserveClauses - Grow the size of the operand list to accommodate the new
2316 /// number of clauses.
2317 void reserveClauses(unsigned Size) { growOperands(Size); }
2319 // Methods for support type inquiry through isa, cast, and dyn_cast:
2320 static inline bool classof(const Instruction *I) {
2321 return I->getOpcode() == Instruction::LandingPad;
2323 static inline bool classof(const Value *V) {
2324 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2329 struct OperandTraits<LandingPadInst> : public HungoffOperandTraits<2> {
2332 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(LandingPadInst, Value)
2334 //===----------------------------------------------------------------------===//
2336 //===----------------------------------------------------------------------===//
2338 //===---------------------------------------------------------------------------
2339 /// ReturnInst - Return a value (possibly void), from a function. Execution
2340 /// does not continue in this function any longer.
2342 class ReturnInst : public TerminatorInst {
2343 ReturnInst(const ReturnInst &RI);
2346 // ReturnInst constructors:
2347 // ReturnInst() - 'ret void' instruction
2348 // ReturnInst( null) - 'ret void' instruction
2349 // ReturnInst(Value* X) - 'ret X' instruction
2350 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2351 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2352 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2353 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2355 // NOTE: If the Value* passed is of type void then the constructor behaves as
2356 // if it was passed NULL.
2357 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2358 Instruction *InsertBefore = 0);
2359 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2360 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2362 ReturnInst *clone_impl() const override;
2364 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2365 Instruction *InsertBefore = 0) {
2366 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2368 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2369 BasicBlock *InsertAtEnd) {
2370 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2372 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2373 return new(0) ReturnInst(C, InsertAtEnd);
2375 virtual ~ReturnInst();
2377 /// Provide fast operand accessors
2378 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2380 /// Convenience accessor. Returns null if there is no return value.
2381 Value *getReturnValue() const {
2382 return getNumOperands() != 0 ? getOperand(0) : 0;
2385 unsigned getNumSuccessors() const { return 0; }
2387 // Methods for support type inquiry through isa, cast, and dyn_cast:
2388 static inline bool classof(const Instruction *I) {
2389 return (I->getOpcode() == Instruction::Ret);
2391 static inline bool classof(const Value *V) {
2392 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2395 BasicBlock *getSuccessorV(unsigned idx) const override;
2396 unsigned getNumSuccessorsV() const override;
2397 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2401 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
2404 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2406 //===----------------------------------------------------------------------===//
2408 //===----------------------------------------------------------------------===//
2410 //===---------------------------------------------------------------------------
2411 /// BranchInst - Conditional or Unconditional Branch instruction.
2413 class BranchInst : public TerminatorInst {
2414 /// Ops list - Branches are strange. The operands are ordered:
2415 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2416 /// they don't have to check for cond/uncond branchness. These are mostly
2417 /// accessed relative from op_end().
2418 BranchInst(const BranchInst &BI);
2420 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2421 // BranchInst(BB *B) - 'br B'
2422 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2423 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2424 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2425 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2426 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2427 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2428 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2429 Instruction *InsertBefore = 0);
2430 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2431 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2432 BasicBlock *InsertAtEnd);
2434 BranchInst *clone_impl() const override;
2436 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2437 return new(1) BranchInst(IfTrue, InsertBefore);
2439 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2440 Value *Cond, Instruction *InsertBefore = 0) {
2441 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2443 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2444 return new(1) BranchInst(IfTrue, InsertAtEnd);
2446 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2447 Value *Cond, BasicBlock *InsertAtEnd) {
2448 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2451 /// Transparently provide more efficient getOperand methods.
2452 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2454 bool isUnconditional() const { return getNumOperands() == 1; }
2455 bool isConditional() const { return getNumOperands() == 3; }
2457 Value *getCondition() const {
2458 assert(isConditional() && "Cannot get condition of an uncond branch!");
2462 void setCondition(Value *V) {
2463 assert(isConditional() && "Cannot set condition of unconditional branch!");
2467 unsigned getNumSuccessors() const { return 1+isConditional(); }
2469 BasicBlock *getSuccessor(unsigned i) const {
2470 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2471 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2474 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2475 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2476 *(&Op<-1>() - idx) = (Value*)NewSucc;
2479 /// \brief Swap the successors of this branch instruction.
2481 /// Swaps the successors of the branch instruction. This also swaps any
2482 /// branch weight metadata associated with the instruction so that it
2483 /// continues to map correctly to each operand.
2484 void swapSuccessors();
2486 // Methods for support type inquiry through isa, cast, and dyn_cast:
2487 static inline bool classof(const Instruction *I) {
2488 return (I->getOpcode() == Instruction::Br);
2490 static inline bool classof(const Value *V) {
2491 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2494 BasicBlock *getSuccessorV(unsigned idx) const override;
2495 unsigned getNumSuccessorsV() const override;
2496 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2500 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2503 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2505 //===----------------------------------------------------------------------===//
2507 //===----------------------------------------------------------------------===//
2509 //===---------------------------------------------------------------------------
2510 /// SwitchInst - Multiway switch
2512 class SwitchInst : public TerminatorInst {
2513 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2514 unsigned ReservedSpace;
2515 // Operand[0] = Value to switch on
2516 // Operand[1] = Default basic block destination
2517 // Operand[2n ] = Value to match
2518 // Operand[2n+1] = BasicBlock to go to on match
2519 SwitchInst(const SwitchInst &SI);
2520 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2521 void growOperands();
2522 // allocate space for exactly zero operands
2523 void *operator new(size_t s) {
2524 return User::operator new(s, 0);
2526 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2527 /// switch on and a default destination. The number of additional cases can
2528 /// be specified here to make memory allocation more efficient. This
2529 /// constructor can also autoinsert before another instruction.
2530 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2531 Instruction *InsertBefore);
2533 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2534 /// switch on and a default destination. The number of additional cases can
2535 /// be specified here to make memory allocation more efficient. This
2536 /// constructor also autoinserts at the end of the specified BasicBlock.
2537 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2538 BasicBlock *InsertAtEnd);
2540 SwitchInst *clone_impl() const override;
2544 static const unsigned DefaultPseudoIndex = static_cast<unsigned>(~0L-1);
2546 template <class SwitchInstTy, class ConstantIntTy, class BasicBlockTy>
2547 class CaseIteratorT {
2555 typedef CaseIteratorT<SwitchInstTy, ConstantIntTy, BasicBlockTy> Self;
2557 /// Initializes case iterator for given SwitchInst and for given
2559 CaseIteratorT(SwitchInstTy *SI, unsigned CaseNum) {
2564 /// Initializes case iterator for given SwitchInst and for given
2565 /// TerminatorInst's successor index.
2566 static Self fromSuccessorIndex(SwitchInstTy *SI, unsigned SuccessorIndex) {
2567 assert(SuccessorIndex < SI->getNumSuccessors() &&
2568 "Successor index # out of range!");
2569 return SuccessorIndex != 0 ?
2570 Self(SI, SuccessorIndex - 1) :
2571 Self(SI, DefaultPseudoIndex);
2574 /// Resolves case value for current case.
2575 ConstantIntTy *getCaseValue() {
2576 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2577 return reinterpret_cast<ConstantIntTy*>(SI->getOperand(2 + Index*2));
2580 /// Resolves successor for current case.
2581 BasicBlockTy *getCaseSuccessor() {
2582 assert((Index < SI->getNumCases() ||
2583 Index == DefaultPseudoIndex) &&
2584 "Index out the number of cases.");
2585 return SI->getSuccessor(getSuccessorIndex());
2588 /// Returns number of current case.
2589 unsigned getCaseIndex() const { return Index; }
2591 /// Returns TerminatorInst's successor index for current case successor.
2592 unsigned getSuccessorIndex() const {
2593 assert((Index == DefaultPseudoIndex || Index < SI->getNumCases()) &&
2594 "Index out the number of cases.");
2595 return Index != DefaultPseudoIndex ? Index + 1 : 0;
2599 // Check index correctness after increment.
2600 // Note: Index == getNumCases() means end().
2601 assert(Index+1 <= SI->getNumCases() && "Index out the number of cases.");
2605 Self operator++(int) {
2611 // Check index correctness after decrement.
2612 // Note: Index == getNumCases() means end().
2613 // Also allow "-1" iterator here. That will became valid after ++.
2614 assert((Index == 0 || Index-1 <= SI->getNumCases()) &&
2615 "Index out the number of cases.");
2619 Self operator--(int) {
2624 bool operator==(const Self& RHS) const {
2625 assert(RHS.SI == SI && "Incompatible operators.");
2626 return RHS.Index == Index;
2628 bool operator!=(const Self& RHS) const {
2629 assert(RHS.SI == SI && "Incompatible operators.");
2630 return RHS.Index != Index;
2634 typedef CaseIteratorT<const SwitchInst, const ConstantInt, const BasicBlock>
2637 class CaseIt : public CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> {
2639 typedef CaseIteratorT<SwitchInst, ConstantInt, BasicBlock> ParentTy;
2643 CaseIt(const ParentTy& Src) : ParentTy(Src) {}
2644 CaseIt(SwitchInst *SI, unsigned CaseNum) : ParentTy(SI, CaseNum) {}
2646 /// Sets the new value for current case.
2647 void setValue(ConstantInt *V) {
2648 assert(Index < SI->getNumCases() && "Index out the number of cases.");
2649 SI->setOperand(2 + Index*2, reinterpret_cast<Value*>(V));
2652 /// Sets the new successor for current case.
2653 void setSuccessor(BasicBlock *S) {
2654 SI->setSuccessor(getSuccessorIndex(), S);
2658 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2659 unsigned NumCases, Instruction *InsertBefore = 0) {
2660 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2662 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2663 unsigned NumCases, BasicBlock *InsertAtEnd) {
2664 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2669 /// Provide fast operand accessors
2670 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2672 // Accessor Methods for Switch stmt
2673 Value *getCondition() const { return getOperand(0); }
2674 void setCondition(Value *V) { setOperand(0, V); }
2676 BasicBlock *getDefaultDest() const {
2677 return cast<BasicBlock>(getOperand(1));
2680 void setDefaultDest(BasicBlock *DefaultCase) {
2681 setOperand(1, reinterpret_cast<Value*>(DefaultCase));
2684 /// getNumCases - return the number of 'cases' in this switch instruction,
2685 /// except the default case
2686 unsigned getNumCases() const {
2687 return getNumOperands()/2 - 1;
2690 /// Returns a read/write iterator that points to the first
2691 /// case in SwitchInst.
2692 CaseIt case_begin() {
2693 return CaseIt(this, 0);
2695 /// Returns a read-only iterator that points to the first
2696 /// case in the SwitchInst.
2697 ConstCaseIt case_begin() const {
2698 return ConstCaseIt(this, 0);
2701 /// Returns a read/write iterator that points one past the last
2702 /// in the SwitchInst.
2704 return CaseIt(this, getNumCases());
2706 /// Returns a read-only iterator that points one past the last
2707 /// in the SwitchInst.
2708 ConstCaseIt case_end() const {
2709 return ConstCaseIt(this, getNumCases());
2711 /// Returns an iterator that points to the default case.
2712 /// Note: this iterator allows to resolve successor only. Attempt
2713 /// to resolve case value causes an assertion.
2714 /// Also note, that increment and decrement also causes an assertion and
2715 /// makes iterator invalid.
2716 CaseIt case_default() {
2717 return CaseIt(this, DefaultPseudoIndex);
2719 ConstCaseIt case_default() const {
2720 return ConstCaseIt(this, DefaultPseudoIndex);
2723 /// findCaseValue - Search all of the case values for the specified constant.
2724 /// If it is explicitly handled, return the case iterator of it, otherwise
2725 /// return default case iterator to indicate
2726 /// that it is handled by the default handler.
2727 CaseIt findCaseValue(const ConstantInt *C) {
2728 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i)
2729 if (i.getCaseValue() == C)
2731 return case_default();
2733 ConstCaseIt findCaseValue(const ConstantInt *C) const {
2734 for (ConstCaseIt i = case_begin(), e = case_end(); i != e; ++i)
2735 if (i.getCaseValue() == C)
2737 return case_default();
2740 /// findCaseDest - Finds the unique case value for a given successor. Returns
2741 /// null if the successor is not found, not unique, or is the default case.
2742 ConstantInt *findCaseDest(BasicBlock *BB) {
2743 if (BB == getDefaultDest()) return NULL;
2745 ConstantInt *CI = NULL;
2746 for (CaseIt i = case_begin(), e = case_end(); i != e; ++i) {
2747 if (i.getCaseSuccessor() == BB) {
2748 if (CI) return NULL; // Multiple cases lead to BB.
2749 else CI = i.getCaseValue();
2755 /// addCase - Add an entry to the switch instruction...
2757 /// This action invalidates case_end(). Old case_end() iterator will
2758 /// point to the added case.
2759 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2761 /// removeCase - This method removes the specified case and its successor
2762 /// from the switch instruction. Note that this operation may reorder the
2763 /// remaining cases at index idx and above.
2765 /// This action invalidates iterators for all cases following the one removed,
2766 /// including the case_end() iterator.
2767 void removeCase(CaseIt i);
2769 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2770 BasicBlock *getSuccessor(unsigned idx) const {
2771 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2772 return cast<BasicBlock>(getOperand(idx*2+1));
2774 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2775 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2776 setOperand(idx*2+1, (Value*)NewSucc);
2779 // Methods for support type inquiry through isa, cast, and dyn_cast:
2780 static inline bool classof(const Instruction *I) {
2781 return I->getOpcode() == Instruction::Switch;
2783 static inline bool classof(const Value *V) {
2784 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2787 BasicBlock *getSuccessorV(unsigned idx) const override;
2788 unsigned getNumSuccessorsV() const override;
2789 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2793 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2796 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2799 //===----------------------------------------------------------------------===//
2800 // IndirectBrInst Class
2801 //===----------------------------------------------------------------------===//
2803 //===---------------------------------------------------------------------------
2804 /// IndirectBrInst - Indirect Branch Instruction.
2806 class IndirectBrInst : public TerminatorInst {
2807 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
2808 unsigned ReservedSpace;
2809 // Operand[0] = Value to switch on
2810 // Operand[1] = Default basic block destination
2811 // Operand[2n ] = Value to match
2812 // Operand[2n+1] = BasicBlock to go to on match
2813 IndirectBrInst(const IndirectBrInst &IBI);
2814 void init(Value *Address, unsigned NumDests);
2815 void growOperands();
2816 // allocate space for exactly zero operands
2817 void *operator new(size_t s) {
2818 return User::operator new(s, 0);
2820 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2821 /// Address to jump to. The number of expected destinations can be specified
2822 /// here to make memory allocation more efficient. This constructor can also
2823 /// autoinsert before another instruction.
2824 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2826 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2827 /// Address to jump to. The number of expected destinations can be specified
2828 /// here to make memory allocation more efficient. This constructor also
2829 /// autoinserts at the end of the specified BasicBlock.
2830 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2832 IndirectBrInst *clone_impl() const override;
2834 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2835 Instruction *InsertBefore = 0) {
2836 return new IndirectBrInst(Address, NumDests, InsertBefore);
2838 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2839 BasicBlock *InsertAtEnd) {
2840 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2844 /// Provide fast operand accessors.
2845 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2847 // Accessor Methods for IndirectBrInst instruction.
2848 Value *getAddress() { return getOperand(0); }
2849 const Value *getAddress() const { return getOperand(0); }
2850 void setAddress(Value *V) { setOperand(0, V); }
2853 /// getNumDestinations - return the number of possible destinations in this
2854 /// indirectbr instruction.
2855 unsigned getNumDestinations() const { return getNumOperands()-1; }
2857 /// getDestination - Return the specified destination.
2858 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2859 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2861 /// addDestination - Add a destination.
2863 void addDestination(BasicBlock *Dest);
2865 /// removeDestination - This method removes the specified successor from the
2866 /// indirectbr instruction.
2867 void removeDestination(unsigned i);
2869 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2870 BasicBlock *getSuccessor(unsigned i) const {
2871 return cast<BasicBlock>(getOperand(i+1));
2873 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2874 setOperand(i+1, (Value*)NewSucc);
2877 // Methods for support type inquiry through isa, cast, and dyn_cast:
2878 static inline bool classof(const Instruction *I) {
2879 return I->getOpcode() == Instruction::IndirectBr;
2881 static inline bool classof(const Value *V) {
2882 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2885 BasicBlock *getSuccessorV(unsigned idx) const override;
2886 unsigned getNumSuccessorsV() const override;
2887 void setSuccessorV(unsigned idx, BasicBlock *B) override;
2891 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2894 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2897 //===----------------------------------------------------------------------===//
2899 //===----------------------------------------------------------------------===//
2901 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2902 /// calling convention of the call.
2904 class InvokeInst : public TerminatorInst {
2905 AttributeSet AttributeList;
2906 InvokeInst(const InvokeInst &BI);
2907 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2908 ArrayRef<Value *> Args, const Twine &NameStr);
2910 /// Construct an InvokeInst given a range of arguments.
2912 /// \brief Construct an InvokeInst from a range of arguments
2913 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2914 ArrayRef<Value *> Args, unsigned Values,
2915 const Twine &NameStr, Instruction *InsertBefore);
2917 /// Construct an InvokeInst given a range of arguments.
2919 /// \brief Construct an InvokeInst from a range of arguments
2920 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2921 ArrayRef<Value *> Args, unsigned Values,
2922 const Twine &NameStr, BasicBlock *InsertAtEnd);
2924 InvokeInst *clone_impl() const override;
2926 static InvokeInst *Create(Value *Func,
2927 BasicBlock *IfNormal, BasicBlock *IfException,
2928 ArrayRef<Value *> Args, const Twine &NameStr = "",
2929 Instruction *InsertBefore = 0) {
2930 unsigned Values = unsigned(Args.size()) + 3;
2931 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2932 Values, NameStr, InsertBefore);
2934 static InvokeInst *Create(Value *Func,
2935 BasicBlock *IfNormal, BasicBlock *IfException,
2936 ArrayRef<Value *> Args, const Twine &NameStr,
2937 BasicBlock *InsertAtEnd) {
2938 unsigned Values = unsigned(Args.size()) + 3;
2939 return new(Values) InvokeInst(Func, IfNormal, IfException, Args,
2940 Values, NameStr, InsertAtEnd);
2943 /// Provide fast operand accessors
2944 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2946 /// getNumArgOperands - Return the number of invoke arguments.
2948 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2950 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2952 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2953 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2955 /// arg_operands - iteration adapter for range-for loops.
2956 iterator_range<op_iterator> arg_operands() {
2957 return iterator_range<op_iterator>(op_begin(), op_end());
2960 /// arg_operands - iteration adapter for range-for loops.
2961 iterator_range<const_op_iterator> arg_operands() const {
2962 return iterator_range<const_op_iterator>(op_begin(), op_end());
2965 /// \brief Wrappers for getting the \c Use of a invoke argument.
2966 const Use &getArgOperandUse(unsigned i) const { return getOperandUse(i); }
2967 Use &getArgOperandUse(unsigned i) { return getOperandUse(i); }
2969 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2971 CallingConv::ID getCallingConv() const {
2972 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2974 void setCallingConv(CallingConv::ID CC) {
2975 setInstructionSubclassData(static_cast<unsigned>(CC));
2978 /// getAttributes - Return the parameter attributes for this invoke.
2980 const AttributeSet &getAttributes() const { return AttributeList; }
2982 /// setAttributes - Set the parameter attributes for this invoke.
2984 void setAttributes(const AttributeSet &Attrs) { AttributeList = Attrs; }
2986 /// addAttribute - adds the attribute to the list of attributes.
2987 void addAttribute(unsigned i, Attribute::AttrKind attr);
2989 /// removeAttribute - removes the attribute from the list of attributes.
2990 void removeAttribute(unsigned i, Attribute attr);
2992 /// \brief Determine whether this call has the given attribute.
2993 bool hasFnAttr(Attribute::AttrKind A) const {
2994 assert(A != Attribute::NoBuiltin &&
2995 "Use CallInst::isNoBuiltin() to check for Attribute::NoBuiltin");
2996 return hasFnAttrImpl(A);
2999 /// \brief Determine whether the call or the callee has the given attributes.
3000 bool paramHasAttr(unsigned i, Attribute::AttrKind A) const;
3002 /// \brief Extract the alignment for a call or parameter (0=unknown).
3003 unsigned getParamAlignment(unsigned i) const {
3004 return AttributeList.getParamAlignment(i);
3007 /// \brief Return true if the call should not be treated as a call to a
3009 bool isNoBuiltin() const {
3010 // We assert in hasFnAttr if one passes in Attribute::NoBuiltin, so we have
3011 // to check it by hand.
3012 return hasFnAttrImpl(Attribute::NoBuiltin) &&
3013 !hasFnAttrImpl(Attribute::Builtin);
3016 /// \brief Return true if the call should not be inlined.
3017 bool isNoInline() const { return hasFnAttr(Attribute::NoInline); }
3018 void setIsNoInline() {
3019 addAttribute(AttributeSet::FunctionIndex, Attribute::NoInline);
3022 /// \brief Determine if the call does not access memory.
3023 bool doesNotAccessMemory() const {
3024 return hasFnAttr(Attribute::ReadNone);
3026 void setDoesNotAccessMemory() {
3027 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadNone);
3030 /// \brief Determine if the call does not access or only reads memory.
3031 bool onlyReadsMemory() const {
3032 return doesNotAccessMemory() || hasFnAttr(Attribute::ReadOnly);
3034 void setOnlyReadsMemory() {
3035 addAttribute(AttributeSet::FunctionIndex, Attribute::ReadOnly);
3038 /// \brief Determine if the call cannot return.
3039 bool doesNotReturn() const { return hasFnAttr(Attribute::NoReturn); }
3040 void setDoesNotReturn() {
3041 addAttribute(AttributeSet::FunctionIndex, Attribute::NoReturn);
3044 /// \brief Determine if the call cannot unwind.
3045 bool doesNotThrow() const { return hasFnAttr(Attribute::NoUnwind); }
3046 void setDoesNotThrow() {
3047 addAttribute(AttributeSet::FunctionIndex, Attribute::NoUnwind);
3050 /// \brief Determine if the invoke cannot be duplicated.
3051 bool cannotDuplicate() const {return hasFnAttr(Attribute::NoDuplicate); }
3052 void setCannotDuplicate() {
3053 addAttribute(AttributeSet::FunctionIndex, Attribute::NoDuplicate);
3056 /// \brief Determine if the call returns a structure through first
3057 /// pointer argument.
3058 bool hasStructRetAttr() const {
3059 // Be friendly and also check the callee.
3060 return paramHasAttr(1, Attribute::StructRet);
3063 /// \brief Determine if any call argument is an aggregate passed by value.
3064 bool hasByValArgument() const {
3065 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
3068 /// getCalledFunction - Return the function called, or null if this is an
3069 /// indirect function invocation.
3071 Function *getCalledFunction() const {
3072 return dyn_cast<Function>(Op<-3>());
3075 /// getCalledValue - Get a pointer to the function that is invoked by this
3077 const Value *getCalledValue() const { return Op<-3>(); }
3078 Value *getCalledValue() { return Op<-3>(); }
3080 /// setCalledFunction - Set the function called.
3081 void setCalledFunction(Value* Fn) {
3085 // get*Dest - Return the destination basic blocks...
3086 BasicBlock *getNormalDest() const {
3087 return cast<BasicBlock>(Op<-2>());
3089 BasicBlock *getUnwindDest() const {
3090 return cast<BasicBlock>(Op<-1>());
3092 void setNormalDest(BasicBlock *B) {
3093 Op<-2>() = reinterpret_cast<Value*>(B);
3095 void setUnwindDest(BasicBlock *B) {
3096 Op<-1>() = reinterpret_cast<Value*>(B);
3099 /// getLandingPadInst - Get the landingpad instruction from the landing pad
3100 /// block (the unwind destination).
3101 LandingPadInst *getLandingPadInst() const;
3103 BasicBlock *getSuccessor(unsigned i) const {
3104 assert(i < 2 && "Successor # out of range for invoke!");
3105 return i == 0 ? getNormalDest() : getUnwindDest();
3108 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
3109 assert(idx < 2 && "Successor # out of range for invoke!");
3110 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
3113 unsigned getNumSuccessors() const { return 2; }
3115 // Methods for support type inquiry through isa, cast, and dyn_cast:
3116 static inline bool classof(const Instruction *I) {
3117 return (I->getOpcode() == Instruction::Invoke);
3119 static inline bool classof(const Value *V) {
3120 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3124 BasicBlock *getSuccessorV(unsigned idx) const override;
3125 unsigned getNumSuccessorsV() const override;
3126 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3128 bool hasFnAttrImpl(Attribute::AttrKind A) const;
3130 // Shadow Instruction::setInstructionSubclassData with a private forwarding
3131 // method so that subclasses cannot accidentally use it.
3132 void setInstructionSubclassData(unsigned short D) {
3133 Instruction::setInstructionSubclassData(D);
3138 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
3141 InvokeInst::InvokeInst(Value *Func,
3142 BasicBlock *IfNormal, BasicBlock *IfException,
3143 ArrayRef<Value *> Args, unsigned Values,
3144 const Twine &NameStr, Instruction *InsertBefore)
3145 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3146 ->getElementType())->getReturnType(),
3147 Instruction::Invoke,
3148 OperandTraits<InvokeInst>::op_end(this) - Values,
3149 Values, InsertBefore) {
3150 init(Func, IfNormal, IfException, Args, NameStr);
3152 InvokeInst::InvokeInst(Value *Func,
3153 BasicBlock *IfNormal, BasicBlock *IfException,
3154 ArrayRef<Value *> Args, unsigned Values,
3155 const Twine &NameStr, BasicBlock *InsertAtEnd)
3156 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
3157 ->getElementType())->getReturnType(),
3158 Instruction::Invoke,
3159 OperandTraits<InvokeInst>::op_end(this) - Values,
3160 Values, InsertAtEnd) {
3161 init(Func, IfNormal, IfException, Args, NameStr);
3164 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
3166 //===----------------------------------------------------------------------===//
3168 //===----------------------------------------------------------------------===//
3170 //===---------------------------------------------------------------------------
3171 /// ResumeInst - Resume the propagation of an exception.
3173 class ResumeInst : public TerminatorInst {
3174 ResumeInst(const ResumeInst &RI);
3176 explicit ResumeInst(Value *Exn, Instruction *InsertBefore=0);
3177 ResumeInst(Value *Exn, BasicBlock *InsertAtEnd);
3179 ResumeInst *clone_impl() const override;
3181 static ResumeInst *Create(Value *Exn, Instruction *InsertBefore = 0) {
3182 return new(1) ResumeInst(Exn, InsertBefore);
3184 static ResumeInst *Create(Value *Exn, BasicBlock *InsertAtEnd) {
3185 return new(1) ResumeInst(Exn, InsertAtEnd);
3188 /// Provide fast operand accessors
3189 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
3191 /// Convenience accessor.
3192 Value *getValue() const { return Op<0>(); }
3194 unsigned getNumSuccessors() const { return 0; }
3196 // Methods for support type inquiry through isa, cast, and dyn_cast:
3197 static inline bool classof(const Instruction *I) {
3198 return I->getOpcode() == Instruction::Resume;
3200 static inline bool classof(const Value *V) {
3201 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3204 BasicBlock *getSuccessorV(unsigned idx) const override;
3205 unsigned getNumSuccessorsV() const override;
3206 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3210 struct OperandTraits<ResumeInst> :
3211 public FixedNumOperandTraits<ResumeInst, 1> {
3214 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ResumeInst, Value)
3216 //===----------------------------------------------------------------------===//
3217 // UnreachableInst Class
3218 //===----------------------------------------------------------------------===//
3220 //===---------------------------------------------------------------------------
3221 /// UnreachableInst - This function has undefined behavior. In particular, the
3222 /// presence of this instruction indicates some higher level knowledge that the
3223 /// end of the block cannot be reached.
3225 class UnreachableInst : public TerminatorInst {
3226 void *operator new(size_t, unsigned) LLVM_DELETED_FUNCTION;
3228 UnreachableInst *clone_impl() const override;
3231 // allocate space for exactly zero operands
3232 void *operator new(size_t s) {
3233 return User::operator new(s, 0);
3235 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
3236 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
3238 unsigned getNumSuccessors() const { return 0; }
3240 // Methods for support type inquiry through isa, cast, and dyn_cast:
3241 static inline bool classof(const Instruction *I) {
3242 return I->getOpcode() == Instruction::Unreachable;
3244 static inline bool classof(const Value *V) {
3245 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3248 BasicBlock *getSuccessorV(unsigned idx) const override;
3249 unsigned getNumSuccessorsV() const override;
3250 void setSuccessorV(unsigned idx, BasicBlock *B) override;
3253 //===----------------------------------------------------------------------===//
3255 //===----------------------------------------------------------------------===//
3257 /// \brief This class represents a truncation of integer types.
3258 class TruncInst : public CastInst {
3260 /// \brief Clone an identical TruncInst
3261 TruncInst *clone_impl() const override;
3264 /// \brief Constructor with insert-before-instruction semantics
3266 Value *S, ///< The value to be truncated
3267 Type *Ty, ///< The (smaller) type to truncate to
3268 const Twine &NameStr = "", ///< A name for the new instruction
3269 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3272 /// \brief Constructor with insert-at-end-of-block semantics
3274 Value *S, ///< The value to be truncated
3275 Type *Ty, ///< The (smaller) type to truncate to
3276 const Twine &NameStr, ///< A name for the new instruction
3277 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3280 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3281 static inline bool classof(const Instruction *I) {
3282 return I->getOpcode() == Trunc;
3284 static inline bool classof(const Value *V) {
3285 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3289 //===----------------------------------------------------------------------===//
3291 //===----------------------------------------------------------------------===//
3293 /// \brief This class represents zero extension of integer types.
3294 class ZExtInst : public CastInst {
3296 /// \brief Clone an identical ZExtInst
3297 ZExtInst *clone_impl() const override;
3300 /// \brief Constructor with insert-before-instruction semantics
3302 Value *S, ///< The value to be zero extended
3303 Type *Ty, ///< The type to zero extend to
3304 const Twine &NameStr = "", ///< A name for the new instruction
3305 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3308 /// \brief Constructor with insert-at-end semantics.
3310 Value *S, ///< The value to be zero extended
3311 Type *Ty, ///< The type to zero extend to
3312 const Twine &NameStr, ///< A name for the new instruction
3313 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3316 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3317 static inline bool classof(const Instruction *I) {
3318 return I->getOpcode() == ZExt;
3320 static inline bool classof(const Value *V) {
3321 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3325 //===----------------------------------------------------------------------===//
3327 //===----------------------------------------------------------------------===//
3329 /// \brief This class represents a sign extension of integer types.
3330 class SExtInst : public CastInst {
3332 /// \brief Clone an identical SExtInst
3333 SExtInst *clone_impl() const override;
3336 /// \brief Constructor with insert-before-instruction semantics
3338 Value *S, ///< The value to be sign extended
3339 Type *Ty, ///< The type to sign extend to
3340 const Twine &NameStr = "", ///< A name for the new instruction
3341 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3344 /// \brief Constructor with insert-at-end-of-block semantics
3346 Value *S, ///< The value to be sign extended
3347 Type *Ty, ///< The type to sign extend to
3348 const Twine &NameStr, ///< A name for the new instruction
3349 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3352 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3353 static inline bool classof(const Instruction *I) {
3354 return I->getOpcode() == SExt;
3356 static inline bool classof(const Value *V) {
3357 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3361 //===----------------------------------------------------------------------===//
3362 // FPTruncInst Class
3363 //===----------------------------------------------------------------------===//
3365 /// \brief This class represents a truncation of floating point types.
3366 class FPTruncInst : public CastInst {
3368 /// \brief Clone an identical FPTruncInst
3369 FPTruncInst *clone_impl() const override;
3372 /// \brief Constructor with insert-before-instruction semantics
3374 Value *S, ///< The value to be truncated
3375 Type *Ty, ///< The type to truncate to
3376 const Twine &NameStr = "", ///< A name for the new instruction
3377 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3380 /// \brief Constructor with insert-before-instruction semantics
3382 Value *S, ///< The value to be truncated
3383 Type *Ty, ///< The type to truncate to
3384 const Twine &NameStr, ///< A name for the new instruction
3385 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3388 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3389 static inline bool classof(const Instruction *I) {
3390 return I->getOpcode() == FPTrunc;
3392 static inline bool classof(const Value *V) {
3393 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3397 //===----------------------------------------------------------------------===//
3399 //===----------------------------------------------------------------------===//
3401 /// \brief This class represents an extension of floating point types.
3402 class FPExtInst : public CastInst {
3404 /// \brief Clone an identical FPExtInst
3405 FPExtInst *clone_impl() const override;
3408 /// \brief Constructor with insert-before-instruction semantics
3410 Value *S, ///< The value to be extended
3411 Type *Ty, ///< The type to extend to
3412 const Twine &NameStr = "", ///< A name for the new instruction
3413 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3416 /// \brief Constructor with insert-at-end-of-block semantics
3418 Value *S, ///< The value to be extended
3419 Type *Ty, ///< The type to extend to
3420 const Twine &NameStr, ///< A name for the new instruction
3421 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3424 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3425 static inline bool classof(const Instruction *I) {
3426 return I->getOpcode() == FPExt;
3428 static inline bool classof(const Value *V) {
3429 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3433 //===----------------------------------------------------------------------===//
3435 //===----------------------------------------------------------------------===//
3437 /// \brief This class represents a cast unsigned integer to floating point.
3438 class UIToFPInst : public CastInst {
3440 /// \brief Clone an identical UIToFPInst
3441 UIToFPInst *clone_impl() const override;
3444 /// \brief Constructor with insert-before-instruction semantics
3446 Value *S, ///< The value to be converted
3447 Type *Ty, ///< The type to convert to
3448 const Twine &NameStr = "", ///< A name for the new instruction
3449 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3452 /// \brief Constructor with insert-at-end-of-block semantics
3454 Value *S, ///< The value to be converted
3455 Type *Ty, ///< The type to convert to
3456 const Twine &NameStr, ///< A name for the new instruction
3457 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3460 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3461 static inline bool classof(const Instruction *I) {
3462 return I->getOpcode() == UIToFP;
3464 static inline bool classof(const Value *V) {
3465 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3469 //===----------------------------------------------------------------------===//
3471 //===----------------------------------------------------------------------===//
3473 /// \brief This class represents a cast from signed integer to floating point.
3474 class SIToFPInst : public CastInst {
3476 /// \brief Clone an identical SIToFPInst
3477 SIToFPInst *clone_impl() const override;
3480 /// \brief Constructor with insert-before-instruction semantics
3482 Value *S, ///< The value to be converted
3483 Type *Ty, ///< The type to convert to
3484 const Twine &NameStr = "", ///< A name for the new instruction
3485 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3488 /// \brief Constructor with insert-at-end-of-block semantics
3490 Value *S, ///< The value to be converted
3491 Type *Ty, ///< The type to convert to
3492 const Twine &NameStr, ///< A name for the new instruction
3493 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3496 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3497 static inline bool classof(const Instruction *I) {
3498 return I->getOpcode() == SIToFP;
3500 static inline bool classof(const Value *V) {
3501 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3505 //===----------------------------------------------------------------------===//
3507 //===----------------------------------------------------------------------===//
3509 /// \brief This class represents a cast from floating point to unsigned integer
3510 class FPToUIInst : public CastInst {
3512 /// \brief Clone an identical FPToUIInst
3513 FPToUIInst *clone_impl() const override;
3516 /// \brief Constructor with insert-before-instruction semantics
3518 Value *S, ///< The value to be converted
3519 Type *Ty, ///< The type to convert to
3520 const Twine &NameStr = "", ///< A name for the new instruction
3521 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3524 /// \brief Constructor with insert-at-end-of-block semantics
3526 Value *S, ///< The value to be converted
3527 Type *Ty, ///< The type to convert to
3528 const Twine &NameStr, ///< A name for the new instruction
3529 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3532 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3533 static inline bool classof(const Instruction *I) {
3534 return I->getOpcode() == FPToUI;
3536 static inline bool classof(const Value *V) {
3537 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3541 //===----------------------------------------------------------------------===//
3543 //===----------------------------------------------------------------------===//
3545 /// \brief This class represents a cast from floating point to signed integer.
3546 class FPToSIInst : public CastInst {
3548 /// \brief Clone an identical FPToSIInst
3549 FPToSIInst *clone_impl() const override;
3552 /// \brief Constructor with insert-before-instruction semantics
3554 Value *S, ///< The value to be converted
3555 Type *Ty, ///< The type to convert to
3556 const Twine &NameStr = "", ///< A name for the new instruction
3557 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3560 /// \brief Constructor with insert-at-end-of-block semantics
3562 Value *S, ///< The value to be converted
3563 Type *Ty, ///< The type to convert to
3564 const Twine &NameStr, ///< A name for the new instruction
3565 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3568 /// \brief Methods for support type inquiry through isa, cast, and dyn_cast:
3569 static inline bool classof(const Instruction *I) {
3570 return I->getOpcode() == FPToSI;
3572 static inline bool classof(const Value *V) {
3573 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3577 //===----------------------------------------------------------------------===//
3578 // IntToPtrInst Class
3579 //===----------------------------------------------------------------------===//
3581 /// \brief This class represents a cast from an integer to a pointer.
3582 class IntToPtrInst : public CastInst {
3584 /// \brief Constructor with insert-before-instruction semantics
3586 Value *S, ///< The value to be converted
3587 Type *Ty, ///< The type to convert to
3588 const Twine &NameStr = "", ///< A name for the new instruction
3589 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3592 /// \brief Constructor with insert-at-end-of-block semantics
3594 Value *S, ///< The value to be converted
3595 Type *Ty, ///< The type to convert to
3596 const Twine &NameStr, ///< A name for the new instruction
3597 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3600 /// \brief Clone an identical IntToPtrInst
3601 IntToPtrInst *clone_impl() const override;
3603 /// \brief Returns the address space of this instruction's pointer type.
3604 unsigned getAddressSpace() const {
3605 return getType()->getPointerAddressSpace();
3608 // Methods for support type inquiry through isa, cast, and dyn_cast:
3609 static inline bool classof(const Instruction *I) {
3610 return I->getOpcode() == IntToPtr;
3612 static inline bool classof(const Value *V) {
3613 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3617 //===----------------------------------------------------------------------===//
3618 // PtrToIntInst Class
3619 //===----------------------------------------------------------------------===//
3621 /// \brief This class represents a cast from a pointer to an integer
3622 class PtrToIntInst : public CastInst {
3624 /// \brief Clone an identical PtrToIntInst
3625 PtrToIntInst *clone_impl() const override;
3628 /// \brief Constructor with insert-before-instruction semantics
3630 Value *S, ///< The value to be converted
3631 Type *Ty, ///< The type to convert to
3632 const Twine &NameStr = "", ///< A name for the new instruction
3633 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3636 /// \brief Constructor with insert-at-end-of-block semantics
3638 Value *S, ///< The value to be converted
3639 Type *Ty, ///< The type to convert to
3640 const Twine &NameStr, ///< A name for the new instruction
3641 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3644 /// \brief Gets the pointer operand.
3645 Value *getPointerOperand() { return getOperand(0); }
3646 /// \brief Gets the pointer operand.
3647 const Value *getPointerOperand() const { return getOperand(0); }
3648 /// \brief Gets the operand index of the pointer operand.
3649 static unsigned getPointerOperandIndex() { return 0U; }
3651 /// \brief Returns the address space of the pointer operand.
3652 unsigned getPointerAddressSpace() const {
3653 return getPointerOperand()->getType()->getPointerAddressSpace();
3656 // Methods for support type inquiry through isa, cast, and dyn_cast:
3657 static inline bool classof(const Instruction *I) {
3658 return I->getOpcode() == PtrToInt;
3660 static inline bool classof(const Value *V) {
3661 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3665 //===----------------------------------------------------------------------===//
3666 // BitCastInst Class
3667 //===----------------------------------------------------------------------===//
3669 /// \brief This class represents a no-op cast from one type to another.
3670 class BitCastInst : public CastInst {
3672 /// \brief Clone an identical BitCastInst
3673 BitCastInst *clone_impl() const override;
3676 /// \brief Constructor with insert-before-instruction semantics
3678 Value *S, ///< The value to be casted
3679 Type *Ty, ///< The type to casted to
3680 const Twine &NameStr = "", ///< A name for the new instruction
3681 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3684 /// \brief Constructor with insert-at-end-of-block semantics
3686 Value *S, ///< The value to be casted
3687 Type *Ty, ///< The type to casted to
3688 const Twine &NameStr, ///< A name for the new instruction
3689 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3692 // Methods for support type inquiry through isa, cast, and dyn_cast:
3693 static inline bool classof(const Instruction *I) {
3694 return I->getOpcode() == BitCast;
3696 static inline bool classof(const Value *V) {
3697 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3701 //===----------------------------------------------------------------------===//
3702 // AddrSpaceCastInst Class
3703 //===----------------------------------------------------------------------===//
3705 /// \brief This class represents a conversion between pointers from
3706 /// one address space to another.
3707 class AddrSpaceCastInst : public CastInst {
3709 /// \brief Clone an identical AddrSpaceCastInst
3710 AddrSpaceCastInst *clone_impl() const override;
3713 /// \brief Constructor with insert-before-instruction semantics
3715 Value *S, ///< The value to be casted
3716 Type *Ty, ///< The type to casted to
3717 const Twine &NameStr = "", ///< A name for the new instruction
3718 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3721 /// \brief Constructor with insert-at-end-of-block semantics
3723 Value *S, ///< The value to be casted
3724 Type *Ty, ///< The type to casted to
3725 const Twine &NameStr, ///< A name for the new instruction
3726 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3729 // Methods for support type inquiry through isa, cast, and dyn_cast:
3730 static inline bool classof(const Instruction *I) {
3731 return I->getOpcode() == AddrSpaceCast;
3733 static inline bool classof(const Value *V) {
3734 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3738 } // End llvm namespace