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_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
21 #include "llvm/InstrTypes.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/ParameterAttributes.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/ADT/SmallVector.h"
35 //===----------------------------------------------------------------------===//
36 // AllocationInst Class
37 //===----------------------------------------------------------------------===//
39 /// AllocationInst - This class is the common base class of MallocInst and
42 class AllocationInst : public UnaryInstruction {
44 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
45 const std::string &Name = "", Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
47 const std::string &Name, BasicBlock *InsertAtEnd);
49 // Out of line virtual method, so the vtable, etc. has a home.
50 virtual ~AllocationInst();
52 /// isArrayAllocation - Return true if there is an allocation size parameter
53 /// to the allocation instruction that is not 1.
55 bool isArrayAllocation() const;
57 /// getArraySize - Get the number of element allocated, for a simple
58 /// allocation of a single element, this will return a constant 1 value.
60 const Value *getArraySize() const { return getOperand(0); }
61 Value *getArraySize() { return getOperand(0); }
63 /// getType - Overload to return most specific pointer type
65 const PointerType *getType() const {
66 return reinterpret_cast<const PointerType*>(Instruction::getType());
69 /// getAllocatedType - Return the type that is being allocated by the
72 const Type *getAllocatedType() const;
74 /// getAlignment - Return the alignment of the memory that is being allocated
75 /// by the instruction.
77 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
78 void setAlignment(unsigned Align);
80 virtual Instruction *clone() const = 0;
82 // Methods for support type inquiry through isa, cast, and dyn_cast:
83 static inline bool classof(const AllocationInst *) { return true; }
84 static inline bool classof(const Instruction *I) {
85 return I->getOpcode() == Instruction::Alloca ||
86 I->getOpcode() == Instruction::Malloc;
88 static inline bool classof(const Value *V) {
89 return isa<Instruction>(V) && classof(cast<Instruction>(V));
94 //===----------------------------------------------------------------------===//
96 //===----------------------------------------------------------------------===//
98 /// MallocInst - an instruction to allocated memory on the heap
100 class MallocInst : public AllocationInst {
101 MallocInst(const MallocInst &MI);
103 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
104 const std::string &NameStr = "",
105 Instruction *InsertBefore = 0)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
107 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
108 BasicBlock *InsertAtEnd)
109 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
111 MallocInst(const Type *Ty, const std::string &NameStr,
112 Instruction *InsertBefore = 0)
113 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
114 MallocInst(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
118 const std::string &NameStr, BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
121 const std::string &NameStr = "",
122 Instruction *InsertBefore = 0)
123 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
125 virtual MallocInst *clone() const;
127 // Methods for support type inquiry through isa, cast, and dyn_cast:
128 static inline bool classof(const MallocInst *) { return true; }
129 static inline bool classof(const Instruction *I) {
130 return (I->getOpcode() == Instruction::Malloc);
132 static inline bool classof(const Value *V) {
133 return isa<Instruction>(V) && classof(cast<Instruction>(V));
138 //===----------------------------------------------------------------------===//
140 //===----------------------------------------------------------------------===//
142 /// AllocaInst - an instruction to allocate memory on the stack
144 class AllocaInst : public AllocationInst {
145 AllocaInst(const AllocaInst &);
147 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
148 const std::string &NameStr = "",
149 Instruction *InsertBefore = 0)
150 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
151 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
152 BasicBlock *InsertAtEnd)
153 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
155 AllocaInst(const Type *Ty, const std::string &NameStr,
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
158 AllocaInst(const Type *Ty, const std::string &NameStr,
159 BasicBlock *InsertAtEnd)
160 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
163 const std::string &NameStr = "", Instruction *InsertBefore = 0)
164 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
165 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
166 const std::string &NameStr, BasicBlock *InsertAtEnd)
167 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
169 virtual AllocaInst *clone() const;
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const AllocaInst *) { return true; }
173 static inline bool classof(const Instruction *I) {
174 return (I->getOpcode() == Instruction::Alloca);
176 static inline bool classof(const Value *V) {
177 return isa<Instruction>(V) && classof(cast<Instruction>(V));
182 //===----------------------------------------------------------------------===//
184 //===----------------------------------------------------------------------===//
186 /// FreeInst - an instruction to deallocate memory
188 class FreeInst : public UnaryInstruction {
191 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
192 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
194 virtual FreeInst *clone() const;
196 // Accessor methods for consistency with other memory operations
197 Value *getPointerOperand() { return getOperand(0); }
198 const Value *getPointerOperand() const { return getOperand(0); }
200 // Methods for support type inquiry through isa, cast, and dyn_cast:
201 static inline bool classof(const FreeInst *) { return true; }
202 static inline bool classof(const Instruction *I) {
203 return (I->getOpcode() == Instruction::Free);
205 static inline bool classof(const Value *V) {
206 return isa<Instruction>(V) && classof(cast<Instruction>(V));
211 //===----------------------------------------------------------------------===//
213 //===----------------------------------------------------------------------===//
215 /// LoadInst - an instruction for reading from memory. This uses the
216 /// SubclassData field in Value to store whether or not the load is volatile.
218 class LoadInst : public UnaryInstruction {
220 LoadInst(const LoadInst &LI)
221 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
222 setVolatile(LI.isVolatile());
223 setAlignment(LI.getAlignment());
231 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
232 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
233 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
234 Instruction *InsertBefore = 0);
235 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
236 unsigned Align, Instruction *InsertBefore = 0);
237 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
238 BasicBlock *InsertAtEnd);
239 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
240 unsigned Align, BasicBlock *InsertAtEnd);
242 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
243 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
244 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
245 bool isVolatile = false, Instruction *InsertBefore = 0);
246 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
247 BasicBlock *InsertAtEnd);
249 /// isVolatile - Return true if this is a load from a volatile memory
252 bool isVolatile() const { return SubclassData & 1; }
254 /// setVolatile - Specify whether this is a volatile load or not.
256 void setVolatile(bool V) {
257 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
260 virtual LoadInst *clone() const;
262 /// getAlignment - Return the alignment of the access that is being performed
264 unsigned getAlignment() const {
265 return (1 << (SubclassData>>1)) >> 1;
268 void setAlignment(unsigned Align);
270 Value *getPointerOperand() { return getOperand(0); }
271 const Value *getPointerOperand() const { return getOperand(0); }
272 static unsigned getPointerOperandIndex() { return 0U; }
274 // Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const LoadInst *) { return true; }
276 static inline bool classof(const Instruction *I) {
277 return I->getOpcode() == Instruction::Load;
279 static inline bool classof(const Value *V) {
280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
285 //===----------------------------------------------------------------------===//
287 //===----------------------------------------------------------------------===//
289 /// StoreInst - an instruction for storing to memory
291 class StoreInst : public Instruction {
292 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
294 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
296 Op<0>() = SI.Op<0>();
297 Op<1>() = SI.Op<1>();
298 setVolatile(SI.isVolatile());
299 setAlignment(SI.getAlignment());
307 // allocate space for exactly two operands
308 void *operator new(size_t s) {
309 return User::operator new(s, 2);
311 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
312 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
313 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
314 Instruction *InsertBefore = 0);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, Instruction *InsertBefore = 0);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
318 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
319 unsigned Align, BasicBlock *InsertAtEnd);
322 /// isVolatile - Return true if this is a load from a volatile memory
325 bool isVolatile() const { return SubclassData & 1; }
327 /// setVolatile - Specify whether this is a volatile load or not.
329 void setVolatile(bool V) {
330 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
333 /// Transparently provide more efficient getOperand methods.
334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
336 /// getAlignment - Return the alignment of the access that is being performed
338 unsigned getAlignment() const {
339 return (1 << (SubclassData>>1)) >> 1;
342 void setAlignment(unsigned Align);
344 virtual StoreInst *clone() const;
346 Value *getPointerOperand() { return getOperand(1); }
347 const Value *getPointerOperand() const { return getOperand(1); }
348 static unsigned getPointerOperandIndex() { return 1U; }
350 // Methods for support type inquiry through isa, cast, and dyn_cast:
351 static inline bool classof(const StoreInst *) { return true; }
352 static inline bool classof(const Instruction *I) {
353 return I->getOpcode() == Instruction::Store;
355 static inline bool classof(const Value *V) {
356 return isa<Instruction>(V) && classof(cast<Instruction>(V));
361 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
364 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
366 //===----------------------------------------------------------------------===//
367 // GetElementPtrInst Class
368 //===----------------------------------------------------------------------===//
370 // checkType - Simple wrapper function to give a better assertion failure
371 // message on bad indexes for a gep instruction.
373 static inline const Type *checkType(const Type *Ty) {
374 assert(Ty && "Invalid GetElementPtrInst indices for type!");
378 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
379 /// access elements of arrays and structs
381 class GetElementPtrInst : public Instruction {
382 GetElementPtrInst(const GetElementPtrInst &GEPI);
383 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
384 const std::string &NameStr);
385 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
387 template<typename InputIterator>
388 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
389 const std::string &NameStr,
390 // This argument ensures that we have an iterator we can
391 // do arithmetic on in constant time
392 std::random_access_iterator_tag) {
393 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
396 // This requires that the iterator points to contiguous memory.
397 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
398 // we have to build an array here
401 init(Ptr, 0, NumIdx, NameStr);
405 /// getIndexedType - Returns the type of the element that would be loaded with
406 /// a load instruction with the specified parameters.
408 /// Null is returned if the indices are invalid for the specified
411 template<typename InputIterator>
412 static const Type *getIndexedType(const Type *Ptr,
413 InputIterator IdxBegin,
414 InputIterator IdxEnd,
415 // This argument ensures that we
416 // have an iterator we can do
417 // arithmetic on in constant time
418 std::random_access_iterator_tag) {
419 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
422 // This requires that the iterator points to contiguous memory.
423 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
425 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
428 /// Constructors - Create a getelementptr instruction with a base pointer an
429 /// list of indices. The first ctor can optionally insert before an existing
430 /// instruction, the second appends the new instruction to the specified
432 template<typename InputIterator>
433 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
434 InputIterator IdxEnd,
436 const std::string &NameStr,
437 Instruction *InsertBefore);
438 template<typename InputIterator>
439 inline GetElementPtrInst(Value *Ptr,
440 InputIterator IdxBegin, InputIterator IdxEnd,
442 const std::string &NameStr, BasicBlock *InsertAtEnd);
444 /// Constructors - These two constructors are convenience methods because one
445 /// and two index getelementptr instructions are so common.
446 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
447 Instruction *InsertBefore = 0);
448 GetElementPtrInst(Value *Ptr, Value *Idx,
449 const std::string &NameStr, BasicBlock *InsertAtEnd);
451 template<typename InputIterator>
452 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
453 InputIterator IdxEnd,
454 const std::string &NameStr = "",
455 Instruction *InsertBefore = 0) {
456 typename std::iterator_traits<InputIterator>::difference_type Values =
457 1 + std::distance(IdxBegin, IdxEnd);
459 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
461 template<typename InputIterator>
462 static GetElementPtrInst *Create(Value *Ptr,
463 InputIterator IdxBegin, InputIterator IdxEnd,
464 const std::string &NameStr,
465 BasicBlock *InsertAtEnd) {
466 typename std::iterator_traits<InputIterator>::difference_type Values =
467 1 + std::distance(IdxBegin, IdxEnd);
469 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
472 /// Constructors - These two creators are convenience methods because one
473 /// index getelementptr instructions are so common.
474 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
475 const std::string &NameStr = "",
476 Instruction *InsertBefore = 0) {
477 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
479 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
480 const std::string &NameStr,
481 BasicBlock *InsertAtEnd) {
482 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
485 virtual GetElementPtrInst *clone() const;
487 /// Transparently provide more efficient getOperand methods.
488 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
490 // getType - Overload to return most specific pointer type...
491 const PointerType *getType() const {
492 return reinterpret_cast<const PointerType*>(Instruction::getType());
495 /// getIndexedType - Returns the type of the element that would be loaded with
496 /// a load instruction with the specified parameters.
498 /// Null is returned if the indices are invalid for the specified
501 template<typename InputIterator>
502 static const Type *getIndexedType(const Type *Ptr,
503 InputIterator IdxBegin,
504 InputIterator IdxEnd) {
505 return getIndexedType(Ptr, IdxBegin, IdxEnd,
506 typename std::iterator_traits<InputIterator>::
507 iterator_category());
510 static const Type *getIndexedType(const Type *Ptr,
511 Value* const *Idx, unsigned NumIdx);
513 static const Type *getIndexedType(const Type *Ptr,
514 uint64_t const *Idx, unsigned NumIdx);
516 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
518 inline op_iterator idx_begin() { return op_begin()+1; }
519 inline const_op_iterator idx_begin() const { return op_begin()+1; }
520 inline op_iterator idx_end() { return op_end(); }
521 inline const_op_iterator idx_end() const { return op_end(); }
523 Value *getPointerOperand() {
524 return getOperand(0);
526 const Value *getPointerOperand() const {
527 return getOperand(0);
529 static unsigned getPointerOperandIndex() {
530 return 0U; // get index for modifying correct operand
533 unsigned getNumIndices() const { // Note: always non-negative
534 return getNumOperands() - 1;
537 bool hasIndices() const {
538 return getNumOperands() > 1;
541 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
542 /// zeros. If so, the result pointer and the first operand have the same
543 /// value, just potentially different types.
544 bool hasAllZeroIndices() const;
546 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
547 /// constant integers. If so, the result pointer and the first operand have
548 /// a constant offset between them.
549 bool hasAllConstantIndices() const;
552 // Methods for support type inquiry through isa, cast, and dyn_cast:
553 static inline bool classof(const GetElementPtrInst *) { return true; }
554 static inline bool classof(const Instruction *I) {
555 return (I->getOpcode() == Instruction::GetElementPtr);
557 static inline bool classof(const Value *V) {
558 return isa<Instruction>(V) && classof(cast<Instruction>(V));
563 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
566 template<typename InputIterator>
567 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
568 InputIterator IdxBegin,
569 InputIterator IdxEnd,
571 const std::string &NameStr,
572 Instruction *InsertBefore)
573 : Instruction(PointerType::get(checkType(
574 getIndexedType(Ptr->getType(),
576 cast<PointerType>(Ptr->getType())
577 ->getAddressSpace()),
579 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
580 Values, InsertBefore) {
581 init(Ptr, IdxBegin, IdxEnd, NameStr,
582 typename std::iterator_traits<InputIterator>::iterator_category());
584 template<typename InputIterator>
585 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
586 InputIterator IdxBegin,
587 InputIterator IdxEnd,
589 const std::string &NameStr,
590 BasicBlock *InsertAtEnd)
591 : Instruction(PointerType::get(checkType(
592 getIndexedType(Ptr->getType(),
594 cast<PointerType>(Ptr->getType())
595 ->getAddressSpace()),
597 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
598 Values, InsertAtEnd) {
599 init(Ptr, IdxBegin, IdxEnd, NameStr,
600 typename std::iterator_traits<InputIterator>::iterator_category());
604 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
607 //===----------------------------------------------------------------------===//
609 //===----------------------------------------------------------------------===//
611 /// This instruction compares its operands according to the predicate given
612 /// to the constructor. It only operates on integers or pointers. The operands
613 /// must be identical types.
614 /// @brief Represent an integer comparison operator.
615 class ICmpInst: public CmpInst {
617 /// @brief Constructor with insert-before-instruction semantics.
619 Predicate pred, ///< The predicate to use for the comparison
620 Value *LHS, ///< The left-hand-side of the expression
621 Value *RHS, ///< The right-hand-side of the expression
622 const std::string &NameStr = "", ///< Name of the instruction
623 Instruction *InsertBefore = 0 ///< Where to insert
624 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, NameStr,
626 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
627 pred <= CmpInst::LAST_ICMP_PREDICATE &&
628 "Invalid ICmp predicate value");
629 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
630 "Both operands to ICmp instruction are not of the same type!");
631 // Check that the operands are the right type
632 assert((getOperand(0)->getType()->isInteger() ||
633 isa<PointerType>(getOperand(0)->getType())) &&
634 "Invalid operand types for ICmp instruction");
637 /// @brief Constructor with insert-at-block-end semantics.
639 Predicate pred, ///< The predicate to use for the comparison
640 Value *LHS, ///< The left-hand-side of the expression
641 Value *RHS, ///< The right-hand-side of the expression
642 const std::string &NameStr, ///< Name of the instruction
643 BasicBlock *InsertAtEnd ///< Block to insert into.
644 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, NameStr,
646 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
647 pred <= CmpInst::LAST_ICMP_PREDICATE &&
648 "Invalid ICmp predicate value");
649 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
650 "Both operands to ICmp instruction are not of the same type!");
651 // Check that the operands are the right type
652 assert((getOperand(0)->getType()->isInteger() ||
653 isa<PointerType>(getOperand(0)->getType())) &&
654 "Invalid operand types for ICmp instruction");
657 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
658 /// @returns the predicate that would be the result if the operand were
659 /// regarded as signed.
660 /// @brief Return the signed version of the predicate
661 Predicate getSignedPredicate() const {
662 return getSignedPredicate(getPredicate());
665 /// This is a static version that you can use without an instruction.
666 /// @brief Return the signed version of the predicate.
667 static Predicate getSignedPredicate(Predicate pred);
669 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
670 /// @returns the predicate that would be the result if the operand were
671 /// regarded as unsigned.
672 /// @brief Return the unsigned version of the predicate
673 Predicate getUnsignedPredicate() const {
674 return getUnsignedPredicate(getPredicate());
677 /// This is a static version that you can use without an instruction.
678 /// @brief Return the unsigned version of the predicate.
679 static Predicate getUnsignedPredicate(Predicate pred);
681 /// isEquality - Return true if this predicate is either EQ or NE. This also
682 /// tests for commutativity.
683 static bool isEquality(Predicate P) {
684 return P == ICMP_EQ || P == ICMP_NE;
687 /// isEquality - Return true if this predicate is either EQ or NE. This also
688 /// tests for commutativity.
689 bool isEquality() const {
690 return isEquality(getPredicate());
693 /// @returns true if the predicate of this ICmpInst is commutative
694 /// @brief Determine if this relation is commutative.
695 bool isCommutative() const { return isEquality(); }
697 /// isRelational - Return true if the predicate is relational (not EQ or NE).
699 bool isRelational() const {
700 return !isEquality();
703 /// isRelational - Return true if the predicate is relational (not EQ or NE).
705 static bool isRelational(Predicate P) {
706 return !isEquality(P);
709 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
710 /// @brief Determine if this instruction's predicate is signed.
711 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
713 /// @returns true if the predicate provided is signed, false otherwise
714 /// @brief Determine if the predicate is signed.
715 static bool isSignedPredicate(Predicate pred);
717 /// @returns true if the specified compare predicate is
718 /// true when both operands are equal...
719 /// @brief Determine if the icmp is true when both operands are equal
720 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
721 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
722 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
723 pred == ICmpInst::ICMP_SLE;
726 /// @returns true if the specified compare instruction is
727 /// true when both operands are equal...
728 /// @brief Determine if the ICmpInst returns true when both operands are equal
729 bool isTrueWhenEqual() {
730 return isTrueWhenEqual(getPredicate());
733 /// Initialize a set of values that all satisfy the predicate with C.
734 /// @brief Make a ConstantRange for a relation with a constant value.
735 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
737 /// Exchange the two operands to this instruction in such a way that it does
738 /// not modify the semantics of the instruction. The predicate value may be
739 /// changed to retain the same result if the predicate is order dependent
741 /// @brief Swap operands and adjust predicate.
742 void swapOperands() {
743 SubclassData = getSwappedPredicate();
744 Op<0>().swap(Op<1>());
747 virtual ICmpInst *clone() const;
749 // Methods for support type inquiry through isa, cast, and dyn_cast:
750 static inline bool classof(const ICmpInst *) { return true; }
751 static inline bool classof(const Instruction *I) {
752 return I->getOpcode() == Instruction::ICmp;
754 static inline bool classof(const Value *V) {
755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
759 //===----------------------------------------------------------------------===//
761 //===----------------------------------------------------------------------===//
763 /// This instruction compares its operands according to the predicate given
764 /// to the constructor. It only operates on floating point values or packed
765 /// vectors of floating point values. The operands must be identical types.
766 /// @brief Represents a floating point comparison operator.
767 class FCmpInst: public CmpInst {
769 /// @brief Constructor with insert-before-instruction semantics.
771 Predicate pred, ///< The predicate to use for the comparison
772 Value *LHS, ///< The left-hand-side of the expression
773 Value *RHS, ///< The right-hand-side of the expression
774 const std::string &NameStr = "", ///< Name of the instruction
775 Instruction *InsertBefore = 0 ///< Where to insert
776 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, NameStr,
778 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
779 "Invalid FCmp predicate value");
780 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
781 "Both operands to FCmp instruction are not of the same type!");
782 // Check that the operands are the right type
783 assert(getOperand(0)->getType()->isFloatingPoint() &&
784 "Invalid operand types for FCmp instruction");
787 /// @brief Constructor with insert-at-block-end semantics.
789 Predicate pred, ///< The predicate to use for the comparison
790 Value *LHS, ///< The left-hand-side of the expression
791 Value *RHS, ///< The right-hand-side of the expression
792 const std::string &NameStr, ///< Name of the instruction
793 BasicBlock *InsertAtEnd ///< Block to insert into.
794 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, NameStr,
796 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
797 "Invalid FCmp predicate value");
798 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
799 "Both operands to FCmp instruction are not of the same type!");
800 // Check that the operands are the right type
801 assert(getOperand(0)->getType()->isFloatingPoint() &&
802 "Invalid operand types for FCmp instruction");
805 /// This also tests for commutativity. If isEquality() returns true then
806 /// the predicate is also commutative. Only the equality predicates are
808 /// @returns true if the predicate of this instruction is EQ or NE.
809 /// @brief Determine if this is an equality predicate.
810 bool isEquality() const {
811 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
812 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
814 bool isCommutative() const { return isEquality(); }
816 /// @returns true if the predicate is relational (not EQ or NE).
817 /// @brief Determine if this a relational predicate.
818 bool isRelational() const { return !isEquality(); }
820 /// Exchange the two operands to this instruction in such a way that it does
821 /// not modify the semantics of the instruction. The predicate value may be
822 /// changed to retain the same result if the predicate is order dependent
824 /// @brief Swap operands and adjust predicate.
825 void swapOperands() {
826 SubclassData = getSwappedPredicate();
827 Op<0>().swap(Op<1>());
830 virtual FCmpInst *clone() const;
832 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
833 static inline bool classof(const FCmpInst *) { return true; }
834 static inline bool classof(const Instruction *I) {
835 return I->getOpcode() == Instruction::FCmp;
837 static inline bool classof(const Value *V) {
838 return isa<Instruction>(V) && classof(cast<Instruction>(V));
842 //===----------------------------------------------------------------------===//
844 //===----------------------------------------------------------------------===//
846 /// This instruction compares its operands according to the predicate given
847 /// to the constructor. It only operates on vectors of integers.
848 /// The operands must be identical types.
849 /// @brief Represents a vector integer comparison operator.
850 class VICmpInst: public CmpInst {
852 /// @brief Constructor with insert-before-instruction semantics.
854 Predicate pred, ///< The predicate to use for the comparison
855 Value *LHS, ///< The left-hand-side of the expression
856 Value *RHS, ///< The right-hand-side of the expression
857 const std::string &NameStr = "", ///< Name of the instruction
858 Instruction *InsertBefore = 0 ///< Where to insert
859 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
861 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
862 pred <= CmpInst::LAST_ICMP_PREDICATE &&
863 "Invalid VICmp predicate value");
864 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
865 "Both operands to VICmp instruction are not of the same type!");
868 /// @brief Constructor with insert-at-block-end semantics.
870 Predicate pred, ///< The predicate to use for the comparison
871 Value *LHS, ///< The left-hand-side of the expression
872 Value *RHS, ///< The right-hand-side of the expression
873 const std::string &NameStr, ///< Name of the instruction
874 BasicBlock *InsertAtEnd ///< Block to insert into.
875 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
877 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
878 pred <= CmpInst::LAST_ICMP_PREDICATE &&
879 "Invalid VICmp predicate value");
880 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
881 "Both operands to VICmp instruction are not of the same type!");
884 /// @brief Return the predicate for this instruction.
885 Predicate getPredicate() const { return Predicate(SubclassData); }
887 virtual VICmpInst *clone() const;
889 // Methods for support type inquiry through isa, cast, and dyn_cast:
890 static inline bool classof(const VICmpInst *) { return true; }
891 static inline bool classof(const Instruction *I) {
892 return I->getOpcode() == Instruction::VICmp;
894 static inline bool classof(const Value *V) {
895 return isa<Instruction>(V) && classof(cast<Instruction>(V));
899 //===----------------------------------------------------------------------===//
901 //===----------------------------------------------------------------------===//
903 /// This instruction compares its operands according to the predicate given
904 /// to the constructor. It only operates on vectors of floating point values.
905 /// The operands must be identical types.
906 /// @brief Represents a vector floating point comparison operator.
907 class VFCmpInst: public CmpInst {
909 /// @brief Constructor with insert-before-instruction semantics.
911 Predicate pred, ///< The predicate to use for the comparison
912 Value *LHS, ///< The left-hand-side of the expression
913 Value *RHS, ///< The right-hand-side of the expression
914 const std::string &NameStr = "", ///< Name of the instruction
915 Instruction *InsertBefore = 0 ///< Where to insert
916 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
917 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertBefore) {
918 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
919 "Invalid VFCmp predicate value");
920 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
921 "Both operands to VFCmp instruction are not of the same type!");
924 /// @brief Constructor with insert-at-block-end semantics.
926 Predicate pred, ///< The predicate to use for the comparison
927 Value *LHS, ///< The left-hand-side of the expression
928 Value *RHS, ///< The right-hand-side of the expression
929 const std::string &NameStr, ///< Name of the instruction
930 BasicBlock *InsertAtEnd ///< Block to insert into.
931 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
932 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertAtEnd) {
933 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
934 "Invalid VFCmp predicate value");
935 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
936 "Both operands to VFCmp instruction are not of the same type!");
939 /// @brief Return the predicate for this instruction.
940 Predicate getPredicate() const { return Predicate(SubclassData); }
942 virtual VFCmpInst *clone() const;
944 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
945 static inline bool classof(const VFCmpInst *) { return true; }
946 static inline bool classof(const Instruction *I) {
947 return I->getOpcode() == Instruction::VFCmp;
949 static inline bool classof(const Value *V) {
950 return isa<Instruction>(V) && classof(cast<Instruction>(V));
954 //===----------------------------------------------------------------------===//
956 //===----------------------------------------------------------------------===//
957 /// CallInst - This class represents a function call, abstracting a target
958 /// machine's calling convention. This class uses low bit of the SubClassData
959 /// field to indicate whether or not this is a tail call. The rest of the bits
960 /// hold the calling convention of the call.
963 class CallInst : public Instruction {
964 PAListPtr ParamAttrs; ///< parameter attributes for call
965 CallInst(const CallInst &CI);
966 void init(Value *Func, Value* const *Params, unsigned NumParams);
967 void init(Value *Func, Value *Actual1, Value *Actual2);
968 void init(Value *Func, Value *Actual);
969 void init(Value *Func);
971 template<typename InputIterator>
972 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
973 const std::string &NameStr,
974 // This argument ensures that we have an iterator we can
975 // do arithmetic on in constant time
976 std::random_access_iterator_tag) {
977 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
979 // This requires that the iterator points to contiguous memory.
980 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
984 /// Construct a CallInst given a range of arguments. InputIterator
985 /// must be a random-access iterator pointing to contiguous storage
986 /// (e.g. a std::vector<>::iterator). Checks are made for
987 /// random-accessness but not for contiguous storage as that would
988 /// incur runtime overhead.
989 /// @brief Construct a CallInst from a range of arguments
990 template<typename InputIterator>
991 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
992 const std::string &NameStr, Instruction *InsertBefore);
994 /// Construct a CallInst given a range of arguments. InputIterator
995 /// must be a random-access iterator pointing to contiguous storage
996 /// (e.g. a std::vector<>::iterator). Checks are made for
997 /// random-accessness but not for contiguous storage as that would
998 /// incur runtime overhead.
999 /// @brief Construct a CallInst from a range of arguments
1000 template<typename InputIterator>
1001 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1002 const std::string &NameStr, BasicBlock *InsertAtEnd);
1004 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1005 Instruction *InsertBefore);
1006 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1007 BasicBlock *InsertAtEnd);
1008 explicit CallInst(Value *F, const std::string &NameStr,
1009 Instruction *InsertBefore);
1010 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
1012 template<typename InputIterator>
1013 static CallInst *Create(Value *Func,
1014 InputIterator ArgBegin, InputIterator ArgEnd,
1015 const std::string &NameStr = "",
1016 Instruction *InsertBefore = 0) {
1017 return new((unsigned)(ArgEnd - ArgBegin + 1))
1018 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1020 template<typename InputIterator>
1021 static CallInst *Create(Value *Func,
1022 InputIterator ArgBegin, InputIterator ArgEnd,
1023 const std::string &NameStr, BasicBlock *InsertAtEnd) {
1024 return new((unsigned)(ArgEnd - ArgBegin + 1))
1025 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1027 static CallInst *Create(Value *F, Value *Actual,
1028 const std::string& NameStr = "",
1029 Instruction *InsertBefore = 0) {
1030 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1032 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
1033 BasicBlock *InsertAtEnd) {
1034 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1036 static CallInst *Create(Value *F, const std::string &NameStr = "",
1037 Instruction *InsertBefore = 0) {
1038 return new(1) CallInst(F, NameStr, InsertBefore);
1040 static CallInst *Create(Value *F, const std::string &NameStr,
1041 BasicBlock *InsertAtEnd) {
1042 return new(1) CallInst(F, NameStr, InsertAtEnd);
1047 virtual CallInst *clone() const;
1049 /// Provide fast operand accessors
1050 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1052 bool isTailCall() const { return SubclassData & 1; }
1053 void setTailCall(bool isTC = true) {
1054 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1057 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1059 unsigned getCallingConv() const { return SubclassData >> 1; }
1060 void setCallingConv(unsigned CC) {
1061 SubclassData = (SubclassData & 1) | (CC << 1);
1064 /// getParamAttrs - Return the parameter attributes for this call.
1066 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
1068 /// setParamAttrs - Sets the parameter attributes for this call.
1069 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
1071 /// addParamAttr - adds the attribute to the list of attributes.
1072 void addParamAttr(unsigned i, ParameterAttributes attr);
1074 /// removeParamAttr - removes the attribute from the list of attributes.
1075 void removeParamAttr(unsigned i, ParameterAttributes attr);
1077 /// @brief Determine whether the call or the callee has the given attribute.
1078 bool paramHasAttr(unsigned i, unsigned attr) const;
1080 /// @brief Extract the alignment for a call or parameter (0=unknown).
1081 unsigned getParamAlignment(unsigned i) const {
1082 return ParamAttrs.getParamAlignment(i);
1085 /// @brief Determine if the call does not access memory.
1086 bool doesNotAccessMemory() const {
1087 return paramHasAttr(0, ParamAttr::ReadNone);
1089 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1090 if (NotAccessMemory) addParamAttr(0, ParamAttr::ReadNone);
1091 else removeParamAttr(0, ParamAttr::ReadNone);
1094 /// @brief Determine if the call does not access or only reads memory.
1095 bool onlyReadsMemory() const {
1096 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1098 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1099 if (OnlyReadsMemory) addParamAttr(0, ParamAttr::ReadOnly);
1100 else removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
1103 /// @brief Determine if the call cannot return.
1104 bool doesNotReturn() const {
1105 return paramHasAttr(0, ParamAttr::NoReturn);
1107 void setDoesNotReturn(bool DoesNotReturn = true) {
1108 if (DoesNotReturn) addParamAttr(0, ParamAttr::NoReturn);
1109 else removeParamAttr(0, ParamAttr::NoReturn);
1112 /// @brief Determine if the call cannot unwind.
1113 bool doesNotThrow() const {
1114 return paramHasAttr(0, ParamAttr::NoUnwind);
1116 void setDoesNotThrow(bool DoesNotThrow = true) {
1117 if (DoesNotThrow) addParamAttr(0, ParamAttr::NoUnwind);
1118 else removeParamAttr(0, ParamAttr::NoUnwind);
1121 /// @brief Determine if the call returns a structure through first
1122 /// pointer argument.
1123 bool hasStructRetAttr() const {
1124 // Be friendly and also check the callee.
1125 return paramHasAttr(1, ParamAttr::StructRet);
1128 /// @brief Determine if any call argument is an aggregate passed by value.
1129 bool hasByValArgument() const {
1130 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1133 /// getCalledFunction - Return the function being called by this instruction
1134 /// if it is a direct call. If it is a call through a function pointer,
1136 Function *getCalledFunction() const {
1137 return dyn_cast<Function>(getOperand(0));
1140 /// getCalledValue - Get a pointer to the function that is invoked by this
1142 const Value *getCalledValue() const { return getOperand(0); }
1143 Value *getCalledValue() { return getOperand(0); }
1145 // Methods for support type inquiry through isa, cast, and dyn_cast:
1146 static inline bool classof(const CallInst *) { return true; }
1147 static inline bool classof(const Instruction *I) {
1148 return I->getOpcode() == Instruction::Call;
1150 static inline bool classof(const Value *V) {
1151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1156 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1159 template<typename InputIterator>
1160 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1161 const std::string &NameStr, BasicBlock *InsertAtEnd)
1162 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1163 ->getElementType())->getReturnType(),
1165 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1166 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1167 init(Func, ArgBegin, ArgEnd, NameStr,
1168 typename std::iterator_traits<InputIterator>::iterator_category());
1171 template<typename InputIterator>
1172 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1173 const std::string &NameStr, Instruction *InsertBefore)
1174 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1175 ->getElementType())->getReturnType(),
1177 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1178 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1179 init(Func, ArgBegin, ArgEnd, NameStr,
1180 typename std::iterator_traits<InputIterator>::iterator_category());
1183 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1185 //===----------------------------------------------------------------------===//
1187 //===----------------------------------------------------------------------===//
1189 /// SelectInst - This class represents the LLVM 'select' instruction.
1191 class SelectInst : public Instruction {
1192 void init(Value *C, Value *S1, Value *S2) {
1198 SelectInst(const SelectInst &SI)
1199 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1200 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1202 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1203 Instruction *InsertBefore)
1204 : Instruction(S1->getType(), Instruction::Select,
1205 &Op<0>(), 3, InsertBefore) {
1209 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1210 BasicBlock *InsertAtEnd)
1211 : Instruction(S1->getType(), Instruction::Select,
1212 &Op<0>(), 3, InsertAtEnd) {
1217 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1218 const std::string &NameStr = "",
1219 Instruction *InsertBefore = 0) {
1220 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1222 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1223 const std::string &NameStr,
1224 BasicBlock *InsertAtEnd) {
1225 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1228 Value *getCondition() const { return Op<0>(); }
1229 Value *getTrueValue() const { return Op<1>(); }
1230 Value *getFalseValue() const { return Op<2>(); }
1232 /// Transparently provide more efficient getOperand methods.
1233 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1235 OtherOps getOpcode() const {
1236 return static_cast<OtherOps>(Instruction::getOpcode());
1239 virtual SelectInst *clone() const;
1241 // Methods for support type inquiry through isa, cast, and dyn_cast:
1242 static inline bool classof(const SelectInst *) { return true; }
1243 static inline bool classof(const Instruction *I) {
1244 return I->getOpcode() == Instruction::Select;
1246 static inline bool classof(const Value *V) {
1247 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1252 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1255 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1257 //===----------------------------------------------------------------------===//
1259 //===----------------------------------------------------------------------===//
1261 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1262 /// an argument of the specified type given a va_list and increments that list
1264 class VAArgInst : public UnaryInstruction {
1265 VAArgInst(const VAArgInst &VAA)
1266 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1268 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1269 Instruction *InsertBefore = 0)
1270 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1273 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1274 BasicBlock *InsertAtEnd)
1275 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1279 virtual VAArgInst *clone() const;
1281 // Methods for support type inquiry through isa, cast, and dyn_cast:
1282 static inline bool classof(const VAArgInst *) { return true; }
1283 static inline bool classof(const Instruction *I) {
1284 return I->getOpcode() == VAArg;
1286 static inline bool classof(const Value *V) {
1287 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1291 //===----------------------------------------------------------------------===//
1292 // ExtractElementInst Class
1293 //===----------------------------------------------------------------------===//
1295 /// ExtractElementInst - This instruction extracts a single (scalar)
1296 /// element from a VectorType value
1298 class ExtractElementInst : public Instruction {
1299 ExtractElementInst(const ExtractElementInst &EE) :
1300 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1301 Op<0>() = EE.Op<0>();
1302 Op<1>() = EE.Op<1>();
1306 // allocate space for exactly two operands
1307 void *operator new(size_t s) {
1308 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1310 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1311 Instruction *InsertBefore = 0);
1312 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
1313 Instruction *InsertBefore = 0);
1314 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1315 BasicBlock *InsertAtEnd);
1316 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
1317 BasicBlock *InsertAtEnd);
1319 /// isValidOperands - Return true if an extractelement instruction can be
1320 /// formed with the specified operands.
1321 static bool isValidOperands(const Value *Vec, const Value *Idx);
1323 virtual ExtractElementInst *clone() const;
1325 /// Transparently provide more efficient getOperand methods.
1326 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1328 // Methods for support type inquiry through isa, cast, and dyn_cast:
1329 static inline bool classof(const ExtractElementInst *) { return true; }
1330 static inline bool classof(const Instruction *I) {
1331 return I->getOpcode() == Instruction::ExtractElement;
1333 static inline bool classof(const Value *V) {
1334 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1339 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1342 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1344 //===----------------------------------------------------------------------===//
1345 // InsertElementInst Class
1346 //===----------------------------------------------------------------------===//
1348 /// InsertElementInst - This instruction inserts a single (scalar)
1349 /// element into a VectorType value
1351 class InsertElementInst : public Instruction {
1352 InsertElementInst(const InsertElementInst &IE);
1353 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1354 const std::string &NameStr = "",Instruction *InsertBefore = 0);
1355 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1356 const std::string &NameStr = "",Instruction *InsertBefore = 0);
1357 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1358 const std::string &NameStr, BasicBlock *InsertAtEnd);
1359 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1360 const std::string &NameStr, BasicBlock *InsertAtEnd);
1362 static InsertElementInst *Create(const InsertElementInst &IE) {
1363 return new(IE.getNumOperands()) InsertElementInst(IE);
1365 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1366 const std::string &NameStr = "",
1367 Instruction *InsertBefore = 0) {
1368 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1370 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1371 const std::string &NameStr = "",
1372 Instruction *InsertBefore = 0) {
1373 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1375 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1376 const std::string &NameStr,
1377 BasicBlock *InsertAtEnd) {
1378 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1380 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1381 const std::string &NameStr,
1382 BasicBlock *InsertAtEnd) {
1383 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1386 /// isValidOperands - Return true if an insertelement instruction can be
1387 /// formed with the specified operands.
1388 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1391 virtual InsertElementInst *clone() const;
1393 /// getType - Overload to return most specific vector type.
1395 const VectorType *getType() const {
1396 return reinterpret_cast<const VectorType*>(Instruction::getType());
1399 /// Transparently provide more efficient getOperand methods.
1400 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1402 // Methods for support type inquiry through isa, cast, and dyn_cast:
1403 static inline bool classof(const InsertElementInst *) { return true; }
1404 static inline bool classof(const Instruction *I) {
1405 return I->getOpcode() == Instruction::InsertElement;
1407 static inline bool classof(const Value *V) {
1408 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1413 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1416 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1418 //===----------------------------------------------------------------------===//
1419 // ShuffleVectorInst Class
1420 //===----------------------------------------------------------------------===//
1422 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1425 class ShuffleVectorInst : public Instruction {
1426 ShuffleVectorInst(const ShuffleVectorInst &IE);
1428 // allocate space for exactly three operands
1429 void *operator new(size_t s) {
1430 return User::operator new(s, 3);
1432 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1433 const std::string &NameStr = "",
1434 Instruction *InsertBefor = 0);
1435 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1436 const std::string &NameStr, BasicBlock *InsertAtEnd);
1438 /// isValidOperands - Return true if a shufflevector instruction can be
1439 /// formed with the specified operands.
1440 static bool isValidOperands(const Value *V1, const Value *V2,
1443 virtual ShuffleVectorInst *clone() const;
1445 /// getType - Overload to return most specific vector type.
1447 const VectorType *getType() const {
1448 return reinterpret_cast<const VectorType*>(Instruction::getType());
1451 /// Transparently provide more efficient getOperand methods.
1452 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1454 /// getMaskValue - Return the index from the shuffle mask for the specified
1455 /// output result. This is either -1 if the element is undef or a number less
1456 /// than 2*numelements.
1457 int getMaskValue(unsigned i) const;
1459 // Methods for support type inquiry through isa, cast, and dyn_cast:
1460 static inline bool classof(const ShuffleVectorInst *) { return true; }
1461 static inline bool classof(const Instruction *I) {
1462 return I->getOpcode() == Instruction::ShuffleVector;
1464 static inline bool classof(const Value *V) {
1465 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1470 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1473 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1475 //===----------------------------------------------------------------------===//
1476 // ExtractValueInst Class
1477 //===----------------------------------------------------------------------===//
1479 /// ExtractValueInst - This instruction extracts a struct member or array
1480 /// element value from an aggregate value.
1482 class ExtractValueInst : public UnaryInstruction {
1483 SmallVector<unsigned, 4> Indices;
1485 ExtractValueInst(const ExtractValueInst &EVI);
1486 void init(const unsigned *Idx, unsigned NumIdx,
1487 const std::string &NameStr);
1488 void init(unsigned Idx, const std::string &NameStr);
1490 template<typename InputIterator>
1491 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1492 const std::string &NameStr,
1493 // This argument ensures that we have an iterator we can
1494 // do arithmetic on in constant time
1495 std::random_access_iterator_tag) {
1496 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1498 // There's no fundamental reason why we require at least one index
1499 // (other than weirdness with &*IdxBegin being invalid; see
1500 // getelementptr's init routine for example). But there's no
1501 // present need to support it.
1502 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1504 // This requires that the iterator points to contiguous memory.
1505 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1506 // we have to build an array here
1509 /// getIndexedType - Returns the type of the element that would be extracted
1510 /// with an extractvalue instruction with the specified parameters.
1512 /// Null is returned if the indices are invalid for the specified
1515 static const Type *getIndexedType(const Type *Agg,
1516 const unsigned *Idx, unsigned NumIdx);
1518 template<typename InputIterator>
1519 static const Type *getIndexedType(const Type *Ptr,
1520 InputIterator IdxBegin,
1521 InputIterator IdxEnd,
1522 // This argument ensures that we
1523 // have an iterator we can do
1524 // arithmetic on in constant time
1525 std::random_access_iterator_tag) {
1526 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1529 // This requires that the iterator points to contiguous memory.
1530 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1532 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1535 /// Constructors - Create a extractvalue instruction with a base aggregate
1536 /// value and a list of indices. The first ctor can optionally insert before
1537 /// an existing instruction, the second appends the new instruction to the
1538 /// specified BasicBlock.
1539 template<typename InputIterator>
1540 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1541 InputIterator IdxEnd,
1542 const std::string &NameStr,
1543 Instruction *InsertBefore);
1544 template<typename InputIterator>
1545 inline ExtractValueInst(Value *Agg,
1546 InputIterator IdxBegin, InputIterator IdxEnd,
1547 const std::string &NameStr, BasicBlock *InsertAtEnd);
1549 // allocate space for exactly one operand
1550 void *operator new(size_t s) {
1551 return User::operator new(s, 1);
1555 template<typename InputIterator>
1556 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1557 InputIterator IdxEnd,
1558 const std::string &NameStr = "",
1559 Instruction *InsertBefore = 0) {
1561 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1563 template<typename InputIterator>
1564 static ExtractValueInst *Create(Value *Agg,
1565 InputIterator IdxBegin, InputIterator IdxEnd,
1566 const std::string &NameStr,
1567 BasicBlock *InsertAtEnd) {
1568 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1571 /// Constructors - These two creators are convenience methods because one
1572 /// index extractvalue instructions are much more common than those with
1574 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1575 const std::string &NameStr = "",
1576 Instruction *InsertBefore = 0) {
1577 unsigned Idxs[1] = { Idx };
1578 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1580 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1581 const std::string &NameStr,
1582 BasicBlock *InsertAtEnd) {
1583 unsigned Idxs[1] = { Idx };
1584 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1587 virtual ExtractValueInst *clone() const;
1589 // getType - Overload to return most specific pointer type...
1590 const PointerType *getType() const {
1591 return reinterpret_cast<const PointerType*>(Instruction::getType());
1594 /// getIndexedType - Returns the type of the element that would be extracted
1595 /// with an extractvalue instruction with the specified parameters.
1597 /// Null is returned if the indices are invalid for the specified
1600 template<typename InputIterator>
1601 static const Type *getIndexedType(const Type *Ptr,
1602 InputIterator IdxBegin,
1603 InputIterator IdxEnd) {
1604 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1605 typename std::iterator_traits<InputIterator>::
1606 iterator_category());
1608 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1610 typedef const unsigned* idx_iterator;
1611 inline idx_iterator idx_begin() const { return Indices.begin(); }
1612 inline idx_iterator idx_end() const { return Indices.end(); }
1614 Value *getAggregateOperand() {
1615 return getOperand(0);
1617 const Value *getAggregateOperand() const {
1618 return getOperand(0);
1620 static unsigned getAggregateOperandIndex() {
1621 return 0U; // get index for modifying correct operand
1624 unsigned getNumIndices() const { // Note: always non-negative
1625 return (unsigned)Indices.size();
1628 bool hasIndices() const {
1632 // Methods for support type inquiry through isa, cast, and dyn_cast:
1633 static inline bool classof(const ExtractValueInst *) { return true; }
1634 static inline bool classof(const Instruction *I) {
1635 return I->getOpcode() == Instruction::ExtractValue;
1637 static inline bool classof(const Value *V) {
1638 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1642 template<typename InputIterator>
1643 ExtractValueInst::ExtractValueInst(Value *Agg,
1644 InputIterator IdxBegin,
1645 InputIterator IdxEnd,
1646 const std::string &NameStr,
1647 Instruction *InsertBefore)
1648 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1650 ExtractValue, Agg, InsertBefore) {
1651 init(IdxBegin, IdxEnd, NameStr,
1652 typename std::iterator_traits<InputIterator>::iterator_category());
1654 template<typename InputIterator>
1655 ExtractValueInst::ExtractValueInst(Value *Agg,
1656 InputIterator IdxBegin,
1657 InputIterator IdxEnd,
1658 const std::string &NameStr,
1659 BasicBlock *InsertAtEnd)
1660 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1662 ExtractValue, Agg, InsertAtEnd) {
1663 init(IdxBegin, IdxEnd, NameStr,
1664 typename std::iterator_traits<InputIterator>::iterator_category());
1668 //===----------------------------------------------------------------------===//
1669 // InsertValueInst Class
1670 //===----------------------------------------------------------------------===//
1672 /// InsertValueInst - This instruction inserts a struct field of array element
1673 /// value into an aggregate value.
1675 class InsertValueInst : public Instruction {
1676 SmallVector<unsigned, 4> Indices;
1678 void *operator new(size_t, unsigned); // Do not implement
1679 InsertValueInst(const InsertValueInst &IVI);
1680 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1681 const std::string &NameStr);
1682 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1684 template<typename InputIterator>
1685 void init(Value *Agg, Value *Val,
1686 InputIterator IdxBegin, InputIterator IdxEnd,
1687 const std::string &NameStr,
1688 // This argument ensures that we have an iterator we can
1689 // do arithmetic on in constant time
1690 std::random_access_iterator_tag) {
1691 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1693 // There's no fundamental reason why we require at least one index
1694 // (other than weirdness with &*IdxBegin being invalid; see
1695 // getelementptr's init routine for example). But there's no
1696 // present need to support it.
1697 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1699 // This requires that the iterator points to contiguous memory.
1700 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1701 // we have to build an array here
1704 /// Constructors - Create a insertvalue instruction with a base aggregate
1705 /// value, a value to insert, and a list of indices. The first ctor can
1706 /// optionally insert before an existing instruction, the second appends
1707 /// the new instruction to the specified BasicBlock.
1708 template<typename InputIterator>
1709 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1710 InputIterator IdxEnd,
1711 const std::string &NameStr,
1712 Instruction *InsertBefore);
1713 template<typename InputIterator>
1714 inline InsertValueInst(Value *Agg, Value *Val,
1715 InputIterator IdxBegin, InputIterator IdxEnd,
1716 const std::string &NameStr, BasicBlock *InsertAtEnd);
1718 /// Constructors - These two constructors are convenience methods because one
1719 /// and two index insertvalue instructions are so common.
1720 InsertValueInst(Value *Agg, Value *Val,
1721 unsigned Idx, const std::string &NameStr = "",
1722 Instruction *InsertBefore = 0);
1723 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1724 const std::string &NameStr, BasicBlock *InsertAtEnd);
1726 // allocate space for exactly two operands
1727 void *operator new(size_t s) {
1728 return User::operator new(s, 2);
1731 template<typename InputIterator>
1732 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1733 InputIterator IdxEnd,
1734 const std::string &NameStr = "",
1735 Instruction *InsertBefore = 0) {
1736 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1737 NameStr, InsertBefore);
1739 template<typename InputIterator>
1740 static InsertValueInst *Create(Value *Agg, Value *Val,
1741 InputIterator IdxBegin, InputIterator IdxEnd,
1742 const std::string &NameStr,
1743 BasicBlock *InsertAtEnd) {
1744 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1745 NameStr, InsertAtEnd);
1748 /// Constructors - These two creators are convenience methods because one
1749 /// index insertvalue instructions are much more common than those with
1751 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1752 const std::string &NameStr = "",
1753 Instruction *InsertBefore = 0) {
1754 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1756 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1757 const std::string &NameStr,
1758 BasicBlock *InsertAtEnd) {
1759 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1762 virtual InsertValueInst *clone() const;
1764 /// Transparently provide more efficient getOperand methods.
1765 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1767 // getType - Overload to return most specific pointer type...
1768 const PointerType *getType() const {
1769 return reinterpret_cast<const PointerType*>(Instruction::getType());
1772 typedef const unsigned* idx_iterator;
1773 inline idx_iterator idx_begin() const { return Indices.begin(); }
1774 inline idx_iterator idx_end() const { return Indices.end(); }
1776 Value *getAggregateOperand() {
1777 return getOperand(0);
1779 const Value *getAggregateOperand() const {
1780 return getOperand(0);
1782 static unsigned getAggregateOperandIndex() {
1783 return 0U; // get index for modifying correct operand
1786 Value *getInsertedValueOperand() {
1787 return getOperand(1);
1789 const Value *getInsertedValueOperand() const {
1790 return getOperand(1);
1792 static unsigned getInsertedValueOperandIndex() {
1793 return 1U; // get index for modifying correct operand
1796 unsigned getNumIndices() const { // Note: always non-negative
1797 return (unsigned)Indices.size();
1800 bool hasIndices() const {
1804 // Methods for support type inquiry through isa, cast, and dyn_cast:
1805 static inline bool classof(const InsertValueInst *) { return true; }
1806 static inline bool classof(const Instruction *I) {
1807 return I->getOpcode() == Instruction::InsertValue;
1809 static inline bool classof(const Value *V) {
1810 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1815 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1818 template<typename InputIterator>
1819 InsertValueInst::InsertValueInst(Value *Agg,
1821 InputIterator IdxBegin,
1822 InputIterator IdxEnd,
1823 const std::string &NameStr,
1824 Instruction *InsertBefore)
1825 : Instruction(Agg->getType(), InsertValue,
1826 OperandTraits<InsertValueInst>::op_begin(this),
1828 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1829 typename std::iterator_traits<InputIterator>::iterator_category());
1831 template<typename InputIterator>
1832 InsertValueInst::InsertValueInst(Value *Agg,
1834 InputIterator IdxBegin,
1835 InputIterator IdxEnd,
1836 const std::string &NameStr,
1837 BasicBlock *InsertAtEnd)
1838 : Instruction(Agg->getType(), InsertValue,
1839 OperandTraits<InsertValueInst>::op_begin(this),
1841 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1842 typename std::iterator_traits<InputIterator>::iterator_category());
1845 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1847 //===----------------------------------------------------------------------===//
1849 //===----------------------------------------------------------------------===//
1851 // PHINode - The PHINode class is used to represent the magical mystical PHI
1852 // node, that can not exist in nature, but can be synthesized in a computer
1853 // scientist's overactive imagination.
1855 class PHINode : public Instruction {
1856 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1857 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1858 /// the number actually in use.
1859 unsigned ReservedSpace;
1860 PHINode(const PHINode &PN);
1861 // allocate space for exactly zero operands
1862 void *operator new(size_t s) {
1863 return User::operator new(s, 0);
1865 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1866 Instruction *InsertBefore = 0)
1867 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1872 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1873 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1878 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1879 Instruction *InsertBefore = 0) {
1880 return new PHINode(Ty, NameStr, InsertBefore);
1882 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1883 BasicBlock *InsertAtEnd) {
1884 return new PHINode(Ty, NameStr, InsertAtEnd);
1888 /// reserveOperandSpace - This method can be used to avoid repeated
1889 /// reallocation of PHI operand lists by reserving space for the correct
1890 /// number of operands before adding them. Unlike normal vector reserves,
1891 /// this method can also be used to trim the operand space.
1892 void reserveOperandSpace(unsigned NumValues) {
1893 resizeOperands(NumValues*2);
1896 virtual PHINode *clone() const;
1898 /// Provide fast operand accessors
1899 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1901 /// getNumIncomingValues - Return the number of incoming edges
1903 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1905 /// getIncomingValue - Return incoming value number x
1907 Value *getIncomingValue(unsigned i) const {
1908 assert(i*2 < getNumOperands() && "Invalid value number!");
1909 return getOperand(i*2);
1911 void setIncomingValue(unsigned i, Value *V) {
1912 assert(i*2 < getNumOperands() && "Invalid value number!");
1915 unsigned getOperandNumForIncomingValue(unsigned i) {
1919 /// getIncomingBlock - Return incoming basic block number x
1921 BasicBlock *getIncomingBlock(unsigned i) const {
1922 return static_cast<BasicBlock*>(getOperand(i*2+1));
1924 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1925 setOperand(i*2+1, BB);
1927 unsigned getOperandNumForIncomingBlock(unsigned i) {
1931 /// addIncoming - Add an incoming value to the end of the PHI list
1933 void addIncoming(Value *V, BasicBlock *BB) {
1934 assert(V && "PHI node got a null value!");
1935 assert(BB && "PHI node got a null basic block!");
1936 assert(getType() == V->getType() &&
1937 "All operands to PHI node must be the same type as the PHI node!");
1938 unsigned OpNo = NumOperands;
1939 if (OpNo+2 > ReservedSpace)
1940 resizeOperands(0); // Get more space!
1941 // Initialize some new operands.
1942 NumOperands = OpNo+2;
1943 OperandList[OpNo] = V;
1944 OperandList[OpNo+1] = BB;
1947 /// removeIncomingValue - Remove an incoming value. This is useful if a
1948 /// predecessor basic block is deleted. The value removed is returned.
1950 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1951 /// is true), the PHI node is destroyed and any uses of it are replaced with
1952 /// dummy values. The only time there should be zero incoming values to a PHI
1953 /// node is when the block is dead, so this strategy is sound.
1955 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1957 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1958 int Idx = getBasicBlockIndex(BB);
1959 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1960 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1963 /// getBasicBlockIndex - Return the first index of the specified basic
1964 /// block in the value list for this PHI. Returns -1 if no instance.
1966 int getBasicBlockIndex(const BasicBlock *BB) const {
1967 Use *OL = OperandList;
1968 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1969 if (OL[i+1].get() == BB) return i/2;
1973 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1974 return getIncomingValue(getBasicBlockIndex(BB));
1977 /// hasConstantValue - If the specified PHI node always merges together the
1978 /// same value, return the value, otherwise return null.
1980 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1982 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1983 static inline bool classof(const PHINode *) { return true; }
1984 static inline bool classof(const Instruction *I) {
1985 return I->getOpcode() == Instruction::PHI;
1987 static inline bool classof(const Value *V) {
1988 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1991 void resizeOperands(unsigned NumOperands);
1995 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1998 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2001 //===----------------------------------------------------------------------===//
2003 //===----------------------------------------------------------------------===//
2005 //===---------------------------------------------------------------------------
2006 /// ReturnInst - Return a value (possibly void), from a function. Execution
2007 /// does not continue in this function any longer.
2009 class ReturnInst : public TerminatorInst {
2010 ReturnInst(const ReturnInst &RI);
2013 // ReturnInst constructors:
2014 // ReturnInst() - 'ret void' instruction
2015 // ReturnInst( null) - 'ret void' instruction
2016 // ReturnInst(Value* X) - 'ret X' instruction
2017 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2018 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2019 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2020 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2022 // NOTE: If the Value* passed is of type void then the constructor behaves as
2023 // if it was passed NULL.
2024 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2025 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2026 explicit ReturnInst(BasicBlock *InsertAtEnd);
2028 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2029 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2031 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2032 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2034 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2035 return new(0) ReturnInst(InsertAtEnd);
2037 virtual ~ReturnInst();
2039 virtual ReturnInst *clone() const;
2041 /// Provide fast operand accessors
2042 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2044 /// Convenience accessor
2045 Value *getReturnValue(unsigned n = 0) const {
2046 return n < getNumOperands()
2051 unsigned getNumSuccessors() const { return 0; }
2053 // Methods for support type inquiry through isa, cast, and dyn_cast:
2054 static inline bool classof(const ReturnInst *) { return true; }
2055 static inline bool classof(const Instruction *I) {
2056 return (I->getOpcode() == Instruction::Ret);
2058 static inline bool classof(const Value *V) {
2059 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2062 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2063 virtual unsigned getNumSuccessorsV() const;
2064 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2068 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2071 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2073 //===----------------------------------------------------------------------===//
2075 //===----------------------------------------------------------------------===//
2077 //===---------------------------------------------------------------------------
2078 /// BranchInst - Conditional or Unconditional Branch instruction.
2080 class BranchInst : public TerminatorInst {
2081 /// Ops list - Branches are strange. The operands are ordered:
2082 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2083 /// they don't have to check for cond/uncond branchness.
2084 BranchInst(const BranchInst &BI);
2086 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2087 // BranchInst(BB *B) - 'br B'
2088 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2089 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2090 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2091 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2092 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2093 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2094 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2095 Instruction *InsertBefore = 0);
2096 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2097 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2098 BasicBlock *InsertAtEnd);
2100 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2101 return new(1) BranchInst(IfTrue, InsertBefore);
2103 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2104 Value *Cond, Instruction *InsertBefore = 0) {
2105 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2107 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2108 return new(1) BranchInst(IfTrue, InsertAtEnd);
2110 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2111 Value *Cond, BasicBlock *InsertAtEnd) {
2112 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2116 if (NumOperands == 1)
2117 NumOperands = (unsigned)((Use*)this - OperandList);
2120 /// Transparently provide more efficient getOperand methods.
2121 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2123 virtual BranchInst *clone() const;
2125 bool isUnconditional() const { return getNumOperands() == 1; }
2126 bool isConditional() const { return getNumOperands() == 3; }
2128 Value *getCondition() const {
2129 assert(isConditional() && "Cannot get condition of an uncond branch!");
2130 return getOperand(2);
2133 void setCondition(Value *V) {
2134 assert(isConditional() && "Cannot set condition of unconditional branch!");
2138 // setUnconditionalDest - Change the current branch to an unconditional branch
2139 // targeting the specified block.
2140 // FIXME: Eliminate this ugly method.
2141 void setUnconditionalDest(BasicBlock *Dest) {
2143 if (isConditional()) { // Convert this to an uncond branch.
2150 unsigned getNumSuccessors() const { return 1+isConditional(); }
2152 BasicBlock *getSuccessor(unsigned i) const {
2153 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2154 return cast<BasicBlock>(getOperand(i));
2157 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2158 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2159 setOperand(idx, NewSucc);
2162 // Methods for support type inquiry through isa, cast, and dyn_cast:
2163 static inline bool classof(const BranchInst *) { return true; }
2164 static inline bool classof(const Instruction *I) {
2165 return (I->getOpcode() == Instruction::Br);
2167 static inline bool classof(const Value *V) {
2168 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2171 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2172 virtual unsigned getNumSuccessorsV() const;
2173 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2177 struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2178 // we need to access operands via OperandList, since
2179 // the NumOperands may change from 3 to 1
2180 static inline void *allocate(unsigned); // FIXME
2183 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2185 //===----------------------------------------------------------------------===//
2187 //===----------------------------------------------------------------------===//
2189 //===---------------------------------------------------------------------------
2190 /// SwitchInst - Multiway switch
2192 class SwitchInst : public TerminatorInst {
2193 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2194 unsigned ReservedSpace;
2195 // Operand[0] = Value to switch on
2196 // Operand[1] = Default basic block destination
2197 // Operand[2n ] = Value to match
2198 // Operand[2n+1] = BasicBlock to go to on match
2199 SwitchInst(const SwitchInst &RI);
2200 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2201 void resizeOperands(unsigned No);
2202 // allocate space for exactly zero operands
2203 void *operator new(size_t s) {
2204 return User::operator new(s, 0);
2206 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2207 /// switch on and a default destination. The number of additional cases can
2208 /// be specified here to make memory allocation more efficient. This
2209 /// constructor can also autoinsert before another instruction.
2210 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2211 Instruction *InsertBefore = 0);
2213 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2214 /// switch on and a default destination. The number of additional cases can
2215 /// be specified here to make memory allocation more efficient. This
2216 /// constructor also autoinserts at the end of the specified BasicBlock.
2217 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2218 BasicBlock *InsertAtEnd);
2220 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2221 unsigned NumCases, Instruction *InsertBefore = 0) {
2222 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2224 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2225 unsigned NumCases, BasicBlock *InsertAtEnd) {
2226 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2230 /// Provide fast operand accessors
2231 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2233 // Accessor Methods for Switch stmt
2234 Value *getCondition() const { return getOperand(0); }
2235 void setCondition(Value *V) { setOperand(0, V); }
2237 BasicBlock *getDefaultDest() const {
2238 return cast<BasicBlock>(getOperand(1));
2241 /// getNumCases - return the number of 'cases' in this switch instruction.
2242 /// Note that case #0 is always the default case.
2243 unsigned getNumCases() const {
2244 return getNumOperands()/2;
2247 /// getCaseValue - Return the specified case value. Note that case #0, the
2248 /// default destination, does not have a case value.
2249 ConstantInt *getCaseValue(unsigned i) {
2250 assert(i && i < getNumCases() && "Illegal case value to get!");
2251 return getSuccessorValue(i);
2254 /// getCaseValue - Return the specified case value. Note that case #0, the
2255 /// default destination, does not have a case value.
2256 const ConstantInt *getCaseValue(unsigned i) const {
2257 assert(i && i < getNumCases() && "Illegal case value to get!");
2258 return getSuccessorValue(i);
2261 /// findCaseValue - Search all of the case values for the specified constant.
2262 /// If it is explicitly handled, return the case number of it, otherwise
2263 /// return 0 to indicate that it is handled by the default handler.
2264 unsigned findCaseValue(const ConstantInt *C) const {
2265 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2266 if (getCaseValue(i) == C)
2271 /// findCaseDest - Finds the unique case value for a given successor. Returns
2272 /// null if the successor is not found, not unique, or is the default case.
2273 ConstantInt *findCaseDest(BasicBlock *BB) {
2274 if (BB == getDefaultDest()) return NULL;
2276 ConstantInt *CI = NULL;
2277 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2278 if (getSuccessor(i) == BB) {
2279 if (CI) return NULL; // Multiple cases lead to BB.
2280 else CI = getCaseValue(i);
2286 /// addCase - Add an entry to the switch instruction...
2288 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2290 /// removeCase - This method removes the specified successor from the switch
2291 /// instruction. Note that this cannot be used to remove the default
2292 /// destination (successor #0).
2294 void removeCase(unsigned idx);
2296 virtual SwitchInst *clone() const;
2298 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2299 BasicBlock *getSuccessor(unsigned idx) const {
2300 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2301 return cast<BasicBlock>(getOperand(idx*2+1));
2303 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2304 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2305 setOperand(idx*2+1, NewSucc);
2308 // getSuccessorValue - Return the value associated with the specified
2310 ConstantInt *getSuccessorValue(unsigned idx) const {
2311 assert(idx < getNumSuccessors() && "Successor # out of range!");
2312 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2315 // Methods for support type inquiry through isa, cast, and dyn_cast:
2316 static inline bool classof(const SwitchInst *) { return true; }
2317 static inline bool classof(const Instruction *I) {
2318 return I->getOpcode() == Instruction::Switch;
2320 static inline bool classof(const Value *V) {
2321 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2324 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2325 virtual unsigned getNumSuccessorsV() const;
2326 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2330 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2333 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2336 //===----------------------------------------------------------------------===//
2338 //===----------------------------------------------------------------------===//
2340 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2341 /// calling convention of the call.
2343 class InvokeInst : public TerminatorInst {
2344 PAListPtr ParamAttrs;
2345 InvokeInst(const InvokeInst &BI);
2346 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2347 Value* const *Args, unsigned NumArgs);
2349 template<typename InputIterator>
2350 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2351 InputIterator ArgBegin, InputIterator ArgEnd,
2352 const std::string &NameStr,
2353 // This argument ensures that we have an iterator we can
2354 // do arithmetic on in constant time
2355 std::random_access_iterator_tag) {
2356 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2358 // This requires that the iterator points to contiguous memory.
2359 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2363 /// Construct an InvokeInst given a range of arguments.
2364 /// InputIterator must be a random-access iterator pointing to
2365 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2366 /// made for random-accessness but not for contiguous storage as
2367 /// that would incur runtime overhead.
2369 /// @brief Construct an InvokeInst from a range of arguments
2370 template<typename InputIterator>
2371 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2372 InputIterator ArgBegin, InputIterator ArgEnd,
2374 const std::string &NameStr, Instruction *InsertBefore);
2376 /// Construct an InvokeInst given a range of arguments.
2377 /// InputIterator must be a random-access iterator pointing to
2378 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2379 /// made for random-accessness but not for contiguous storage as
2380 /// that would incur runtime overhead.
2382 /// @brief Construct an InvokeInst from a range of arguments
2383 template<typename InputIterator>
2384 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2385 InputIterator ArgBegin, InputIterator ArgEnd,
2387 const std::string &NameStr, BasicBlock *InsertAtEnd);
2389 template<typename InputIterator>
2390 static InvokeInst *Create(Value *Func,
2391 BasicBlock *IfNormal, BasicBlock *IfException,
2392 InputIterator ArgBegin, InputIterator ArgEnd,
2393 const std::string &NameStr = "",
2394 Instruction *InsertBefore = 0) {
2395 unsigned Values(ArgEnd - ArgBegin + 3);
2396 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2397 Values, NameStr, InsertBefore);
2399 template<typename InputIterator>
2400 static InvokeInst *Create(Value *Func,
2401 BasicBlock *IfNormal, BasicBlock *IfException,
2402 InputIterator ArgBegin, InputIterator ArgEnd,
2403 const std::string &NameStr,
2404 BasicBlock *InsertAtEnd) {
2405 unsigned Values(ArgEnd - ArgBegin + 3);
2406 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2407 Values, NameStr, InsertAtEnd);
2410 virtual InvokeInst *clone() const;
2412 /// Provide fast operand accessors
2413 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2415 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2417 unsigned getCallingConv() const { return SubclassData; }
2418 void setCallingConv(unsigned CC) {
2422 /// getParamAttrs - Return the parameter attributes for this invoke.
2424 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
2426 /// setParamAttrs - Set the parameter attributes for this invoke.
2428 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
2430 /// @brief Determine whether the call or the callee has the given attribute.
2431 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
2433 /// addParamAttr - adds the attribute to the list of attributes.
2434 void addParamAttr(unsigned i, ParameterAttributes attr);
2436 /// removeParamAttr - removes the attribute from the list of attributes.
2437 void removeParamAttr(unsigned i, ParameterAttributes attr);
2439 /// @brief Extract the alignment for a call or parameter (0=unknown).
2440 unsigned getParamAlignment(unsigned i) const {
2441 return ParamAttrs.getParamAlignment(i);
2444 /// @brief Determine if the call does not access memory.
2445 bool doesNotAccessMemory() const {
2446 return paramHasAttr(0, ParamAttr::ReadNone);
2448 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2449 if (NotAccessMemory) addParamAttr(0, ParamAttr::ReadNone);
2450 else removeParamAttr(0, ParamAttr::ReadNone);
2453 /// @brief Determine if the call does not access or only reads memory.
2454 bool onlyReadsMemory() const {
2455 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
2457 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2458 if (OnlyReadsMemory) addParamAttr(0, ParamAttr::ReadOnly);
2459 else removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
2462 /// @brief Determine if the call cannot return.
2463 bool doesNotReturn() const {
2464 return paramHasAttr(0, ParamAttr::NoReturn);
2466 void setDoesNotReturn(bool DoesNotReturn = true) {
2467 if (DoesNotReturn) addParamAttr(0, ParamAttr::NoReturn);
2468 else removeParamAttr(0, ParamAttr::NoReturn);
2471 /// @brief Determine if the call cannot unwind.
2472 bool doesNotThrow() const {
2473 return paramHasAttr(0, ParamAttr::NoUnwind);
2475 void setDoesNotThrow(bool DoesNotThrow = true) {
2476 if (DoesNotThrow) addParamAttr(0, ParamAttr::NoUnwind);
2477 else removeParamAttr(0, ParamAttr::NoUnwind);
2480 /// @brief Determine if the call returns a structure through first
2481 /// pointer argument.
2482 bool hasStructRetAttr() const {
2483 // Be friendly and also check the callee.
2484 return paramHasAttr(1, ParamAttr::StructRet);
2487 /// getCalledFunction - Return the function called, or null if this is an
2488 /// indirect function invocation.
2490 Function *getCalledFunction() const {
2491 return dyn_cast<Function>(getOperand(0));
2494 // getCalledValue - Get a pointer to a function that is invoked by this inst.
2495 Value *getCalledValue() const { return getOperand(0); }
2497 // get*Dest - Return the destination basic blocks...
2498 BasicBlock *getNormalDest() const {
2499 return cast<BasicBlock>(getOperand(1));
2501 BasicBlock *getUnwindDest() const {
2502 return cast<BasicBlock>(getOperand(2));
2504 void setNormalDest(BasicBlock *B) {
2508 void setUnwindDest(BasicBlock *B) {
2512 BasicBlock *getSuccessor(unsigned i) const {
2513 assert(i < 2 && "Successor # out of range for invoke!");
2514 return i == 0 ? getNormalDest() : getUnwindDest();
2517 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2518 assert(idx < 2 && "Successor # out of range for invoke!");
2519 setOperand(idx+1, NewSucc);
2522 unsigned getNumSuccessors() const { return 2; }
2524 // Methods for support type inquiry through isa, cast, and dyn_cast:
2525 static inline bool classof(const InvokeInst *) { return true; }
2526 static inline bool classof(const Instruction *I) {
2527 return (I->getOpcode() == Instruction::Invoke);
2529 static inline bool classof(const Value *V) {
2530 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2533 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2534 virtual unsigned getNumSuccessorsV() const;
2535 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2539 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2542 template<typename InputIterator>
2543 InvokeInst::InvokeInst(Value *Func,
2544 BasicBlock *IfNormal, BasicBlock *IfException,
2545 InputIterator ArgBegin, InputIterator ArgEnd,
2547 const std::string &NameStr, Instruction *InsertBefore)
2548 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2549 ->getElementType())->getReturnType(),
2550 Instruction::Invoke,
2551 OperandTraits<InvokeInst>::op_end(this) - Values,
2552 Values, InsertBefore) {
2553 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2554 typename std::iterator_traits<InputIterator>::iterator_category());
2556 template<typename InputIterator>
2557 InvokeInst::InvokeInst(Value *Func,
2558 BasicBlock *IfNormal, BasicBlock *IfException,
2559 InputIterator ArgBegin, InputIterator ArgEnd,
2561 const std::string &NameStr, BasicBlock *InsertAtEnd)
2562 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2563 ->getElementType())->getReturnType(),
2564 Instruction::Invoke,
2565 OperandTraits<InvokeInst>::op_end(this) - Values,
2566 Values, InsertAtEnd) {
2567 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2568 typename std::iterator_traits<InputIterator>::iterator_category());
2571 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2573 //===----------------------------------------------------------------------===//
2575 //===----------------------------------------------------------------------===//
2577 //===---------------------------------------------------------------------------
2578 /// UnwindInst - Immediately exit the current function, unwinding the stack
2579 /// until an invoke instruction is found.
2581 class UnwindInst : public TerminatorInst {
2582 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2584 // allocate space for exactly zero operands
2585 void *operator new(size_t s) {
2586 return User::operator new(s, 0);
2588 explicit UnwindInst(Instruction *InsertBefore = 0);
2589 explicit UnwindInst(BasicBlock *InsertAtEnd);
2591 virtual UnwindInst *clone() const;
2593 unsigned getNumSuccessors() const { return 0; }
2595 // Methods for support type inquiry through isa, cast, and dyn_cast:
2596 static inline bool classof(const UnwindInst *) { return true; }
2597 static inline bool classof(const Instruction *I) {
2598 return I->getOpcode() == Instruction::Unwind;
2600 static inline bool classof(const Value *V) {
2601 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2604 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2605 virtual unsigned getNumSuccessorsV() const;
2606 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2609 //===----------------------------------------------------------------------===//
2610 // UnreachableInst Class
2611 //===----------------------------------------------------------------------===//
2613 //===---------------------------------------------------------------------------
2614 /// UnreachableInst - This function has undefined behavior. In particular, the
2615 /// presence of this instruction indicates some higher level knowledge that the
2616 /// end of the block cannot be reached.
2618 class UnreachableInst : public TerminatorInst {
2619 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2621 // allocate space for exactly zero operands
2622 void *operator new(size_t s) {
2623 return User::operator new(s, 0);
2625 explicit UnreachableInst(Instruction *InsertBefore = 0);
2626 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2628 virtual UnreachableInst *clone() const;
2630 unsigned getNumSuccessors() const { return 0; }
2632 // Methods for support type inquiry through isa, cast, and dyn_cast:
2633 static inline bool classof(const UnreachableInst *) { return true; }
2634 static inline bool classof(const Instruction *I) {
2635 return I->getOpcode() == Instruction::Unreachable;
2637 static inline bool classof(const Value *V) {
2638 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2641 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2642 virtual unsigned getNumSuccessorsV() const;
2643 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2646 //===----------------------------------------------------------------------===//
2648 //===----------------------------------------------------------------------===//
2650 /// @brief This class represents a truncation of integer types.
2651 class TruncInst : public CastInst {
2652 /// Private copy constructor
2653 TruncInst(const TruncInst &CI)
2654 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2657 /// @brief Constructor with insert-before-instruction semantics
2659 Value *S, ///< The value to be truncated
2660 const Type *Ty, ///< The (smaller) type to truncate to
2661 const std::string &NameStr = "", ///< A name for the new instruction
2662 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2665 /// @brief Constructor with insert-at-end-of-block semantics
2667 Value *S, ///< The value to be truncated
2668 const Type *Ty, ///< The (smaller) type to truncate to
2669 const std::string &NameStr, ///< A name for the new instruction
2670 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2673 /// @brief Clone an identical TruncInst
2674 virtual CastInst *clone() const;
2676 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2677 static inline bool classof(const TruncInst *) { return true; }
2678 static inline bool classof(const Instruction *I) {
2679 return I->getOpcode() == Trunc;
2681 static inline bool classof(const Value *V) {
2682 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2686 //===----------------------------------------------------------------------===//
2688 //===----------------------------------------------------------------------===//
2690 /// @brief This class represents zero extension of integer types.
2691 class ZExtInst : public CastInst {
2692 /// @brief Private copy constructor
2693 ZExtInst(const ZExtInst &CI)
2694 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2697 /// @brief Constructor with insert-before-instruction semantics
2699 Value *S, ///< The value to be zero extended
2700 const Type *Ty, ///< The type to zero extend to
2701 const std::string &NameStr = "", ///< A name for the new instruction
2702 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2705 /// @brief Constructor with insert-at-end semantics.
2707 Value *S, ///< The value to be zero extended
2708 const Type *Ty, ///< The type to zero extend to
2709 const std::string &NameStr, ///< A name for the new instruction
2710 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2713 /// @brief Clone an identical ZExtInst
2714 virtual CastInst *clone() const;
2716 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2717 static inline bool classof(const ZExtInst *) { return true; }
2718 static inline bool classof(const Instruction *I) {
2719 return I->getOpcode() == ZExt;
2721 static inline bool classof(const Value *V) {
2722 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2726 //===----------------------------------------------------------------------===//
2728 //===----------------------------------------------------------------------===//
2730 /// @brief This class represents a sign extension of integer types.
2731 class SExtInst : public CastInst {
2732 /// @brief Private copy constructor
2733 SExtInst(const SExtInst &CI)
2734 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2737 /// @brief Constructor with insert-before-instruction semantics
2739 Value *S, ///< The value to be sign extended
2740 const Type *Ty, ///< The type to sign extend to
2741 const std::string &NameStr = "", ///< A name for the new instruction
2742 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2745 /// @brief Constructor with insert-at-end-of-block semantics
2747 Value *S, ///< The value to be sign extended
2748 const Type *Ty, ///< The type to sign extend to
2749 const std::string &NameStr, ///< A name for the new instruction
2750 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2753 /// @brief Clone an identical SExtInst
2754 virtual CastInst *clone() const;
2756 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2757 static inline bool classof(const SExtInst *) { return true; }
2758 static inline bool classof(const Instruction *I) {
2759 return I->getOpcode() == SExt;
2761 static inline bool classof(const Value *V) {
2762 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2766 //===----------------------------------------------------------------------===//
2767 // FPTruncInst Class
2768 //===----------------------------------------------------------------------===//
2770 /// @brief This class represents a truncation of floating point types.
2771 class FPTruncInst : public CastInst {
2772 FPTruncInst(const FPTruncInst &CI)
2773 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2776 /// @brief Constructor with insert-before-instruction semantics
2778 Value *S, ///< The value to be truncated
2779 const Type *Ty, ///< The type to truncate to
2780 const std::string &NameStr = "", ///< A name for the new instruction
2781 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2784 /// @brief Constructor with insert-before-instruction semantics
2786 Value *S, ///< The value to be truncated
2787 const Type *Ty, ///< The type to truncate to
2788 const std::string &NameStr, ///< A name for the new instruction
2789 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2792 /// @brief Clone an identical FPTruncInst
2793 virtual CastInst *clone() const;
2795 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2796 static inline bool classof(const FPTruncInst *) { return true; }
2797 static inline bool classof(const Instruction *I) {
2798 return I->getOpcode() == FPTrunc;
2800 static inline bool classof(const Value *V) {
2801 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2805 //===----------------------------------------------------------------------===//
2807 //===----------------------------------------------------------------------===//
2809 /// @brief This class represents an extension of floating point types.
2810 class FPExtInst : public CastInst {
2811 FPExtInst(const FPExtInst &CI)
2812 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2815 /// @brief Constructor with insert-before-instruction semantics
2817 Value *S, ///< The value to be extended
2818 const Type *Ty, ///< The type to extend to
2819 const std::string &NameStr = "", ///< A name for the new instruction
2820 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2823 /// @brief Constructor with insert-at-end-of-block semantics
2825 Value *S, ///< The value to be extended
2826 const Type *Ty, ///< The type to extend to
2827 const std::string &NameStr, ///< A name for the new instruction
2828 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2831 /// @brief Clone an identical FPExtInst
2832 virtual CastInst *clone() const;
2834 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2835 static inline bool classof(const FPExtInst *) { return true; }
2836 static inline bool classof(const Instruction *I) {
2837 return I->getOpcode() == FPExt;
2839 static inline bool classof(const Value *V) {
2840 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2844 //===----------------------------------------------------------------------===//
2846 //===----------------------------------------------------------------------===//
2848 /// @brief This class represents a cast unsigned integer to floating point.
2849 class UIToFPInst : public CastInst {
2850 UIToFPInst(const UIToFPInst &CI)
2851 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2854 /// @brief Constructor with insert-before-instruction semantics
2856 Value *S, ///< The value to be converted
2857 const Type *Ty, ///< The type to convert to
2858 const std::string &NameStr = "", ///< A name for the new instruction
2859 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2862 /// @brief Constructor with insert-at-end-of-block semantics
2864 Value *S, ///< The value to be converted
2865 const Type *Ty, ///< The type to convert to
2866 const std::string &NameStr, ///< A name for the new instruction
2867 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2870 /// @brief Clone an identical UIToFPInst
2871 virtual CastInst *clone() const;
2873 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2874 static inline bool classof(const UIToFPInst *) { return true; }
2875 static inline bool classof(const Instruction *I) {
2876 return I->getOpcode() == UIToFP;
2878 static inline bool classof(const Value *V) {
2879 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2883 //===----------------------------------------------------------------------===//
2885 //===----------------------------------------------------------------------===//
2887 /// @brief This class represents a cast from signed integer to floating point.
2888 class SIToFPInst : public CastInst {
2889 SIToFPInst(const SIToFPInst &CI)
2890 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2893 /// @brief Constructor with insert-before-instruction semantics
2895 Value *S, ///< The value to be converted
2896 const Type *Ty, ///< The type to convert to
2897 const std::string &NameStr = "", ///< A name for the new instruction
2898 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2901 /// @brief Constructor with insert-at-end-of-block semantics
2903 Value *S, ///< The value to be converted
2904 const Type *Ty, ///< The type to convert to
2905 const std::string &NameStr, ///< A name for the new instruction
2906 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2909 /// @brief Clone an identical SIToFPInst
2910 virtual CastInst *clone() const;
2912 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2913 static inline bool classof(const SIToFPInst *) { return true; }
2914 static inline bool classof(const Instruction *I) {
2915 return I->getOpcode() == SIToFP;
2917 static inline bool classof(const Value *V) {
2918 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2922 //===----------------------------------------------------------------------===//
2924 //===----------------------------------------------------------------------===//
2926 /// @brief This class represents a cast from floating point to unsigned integer
2927 class FPToUIInst : public CastInst {
2928 FPToUIInst(const FPToUIInst &CI)
2929 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2932 /// @brief Constructor with insert-before-instruction semantics
2934 Value *S, ///< The value to be converted
2935 const Type *Ty, ///< The type to convert to
2936 const std::string &NameStr = "", ///< A name for the new instruction
2937 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2940 /// @brief Constructor with insert-at-end-of-block semantics
2942 Value *S, ///< The value to be converted
2943 const Type *Ty, ///< The type to convert to
2944 const std::string &NameStr, ///< A name for the new instruction
2945 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2948 /// @brief Clone an identical FPToUIInst
2949 virtual CastInst *clone() const;
2951 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2952 static inline bool classof(const FPToUIInst *) { return true; }
2953 static inline bool classof(const Instruction *I) {
2954 return I->getOpcode() == FPToUI;
2956 static inline bool classof(const Value *V) {
2957 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2961 //===----------------------------------------------------------------------===//
2963 //===----------------------------------------------------------------------===//
2965 /// @brief This class represents a cast from floating point to signed integer.
2966 class FPToSIInst : public CastInst {
2967 FPToSIInst(const FPToSIInst &CI)
2968 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2971 /// @brief Constructor with insert-before-instruction semantics
2973 Value *S, ///< The value to be converted
2974 const Type *Ty, ///< The type to convert to
2975 const std::string &NameStr = "", ///< A name for the new instruction
2976 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2979 /// @brief Constructor with insert-at-end-of-block semantics
2981 Value *S, ///< The value to be converted
2982 const Type *Ty, ///< The type to convert to
2983 const std::string &NameStr, ///< A name for the new instruction
2984 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2987 /// @brief Clone an identical FPToSIInst
2988 virtual CastInst *clone() const;
2990 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2991 static inline bool classof(const FPToSIInst *) { return true; }
2992 static inline bool classof(const Instruction *I) {
2993 return I->getOpcode() == FPToSI;
2995 static inline bool classof(const Value *V) {
2996 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3000 //===----------------------------------------------------------------------===//
3001 // IntToPtrInst Class
3002 //===----------------------------------------------------------------------===//
3004 /// @brief This class represents a cast from an integer to a pointer.
3005 class IntToPtrInst : public CastInst {
3006 IntToPtrInst(const IntToPtrInst &CI)
3007 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3010 /// @brief Constructor with insert-before-instruction semantics
3012 Value *S, ///< The value to be converted
3013 const Type *Ty, ///< The type to convert to
3014 const std::string &NameStr = "", ///< A name for the new instruction
3015 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3018 /// @brief Constructor with insert-at-end-of-block semantics
3020 Value *S, ///< The value to be converted
3021 const Type *Ty, ///< The type to convert to
3022 const std::string &NameStr, ///< A name for the new instruction
3023 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3026 /// @brief Clone an identical IntToPtrInst
3027 virtual CastInst *clone() const;
3029 // Methods for support type inquiry through isa, cast, and dyn_cast:
3030 static inline bool classof(const IntToPtrInst *) { return true; }
3031 static inline bool classof(const Instruction *I) {
3032 return I->getOpcode() == IntToPtr;
3034 static inline bool classof(const Value *V) {
3035 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3039 //===----------------------------------------------------------------------===//
3040 // PtrToIntInst Class
3041 //===----------------------------------------------------------------------===//
3043 /// @brief This class represents a cast from a pointer to an integer
3044 class PtrToIntInst : public CastInst {
3045 PtrToIntInst(const PtrToIntInst &CI)
3046 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3049 /// @brief Constructor with insert-before-instruction semantics
3051 Value *S, ///< The value to be converted
3052 const Type *Ty, ///< The type to convert to
3053 const std::string &NameStr = "", ///< A name for the new instruction
3054 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3057 /// @brief Constructor with insert-at-end-of-block semantics
3059 Value *S, ///< The value to be converted
3060 const Type *Ty, ///< The type to convert to
3061 const std::string &NameStr, ///< A name for the new instruction
3062 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3065 /// @brief Clone an identical PtrToIntInst
3066 virtual CastInst *clone() const;
3068 // Methods for support type inquiry through isa, cast, and dyn_cast:
3069 static inline bool classof(const PtrToIntInst *) { return true; }
3070 static inline bool classof(const Instruction *I) {
3071 return I->getOpcode() == PtrToInt;
3073 static inline bool classof(const Value *V) {
3074 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3078 //===----------------------------------------------------------------------===//
3079 // BitCastInst Class
3080 //===----------------------------------------------------------------------===//
3082 /// @brief This class represents a no-op cast from one type to another.
3083 class BitCastInst : public CastInst {
3084 BitCastInst(const BitCastInst &CI)
3085 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3088 /// @brief Constructor with insert-before-instruction semantics
3090 Value *S, ///< The value to be casted
3091 const Type *Ty, ///< The type to casted to
3092 const std::string &NameStr = "", ///< A name for the new instruction
3093 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3096 /// @brief Constructor with insert-at-end-of-block semantics
3098 Value *S, ///< The value to be casted
3099 const Type *Ty, ///< The type to casted to
3100 const std::string &NameStr, ///< A name for the new instruction
3101 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3104 /// @brief Clone an identical BitCastInst
3105 virtual CastInst *clone() const;
3107 // Methods for support type inquiry through isa, cast, and dyn_cast:
3108 static inline bool classof(const BitCastInst *) { return true; }
3109 static inline bool classof(const Instruction *I) {
3110 return I->getOpcode() == BitCast;
3112 static inline bool classof(const Value *V) {
3113 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3117 } // End llvm namespace