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 &Name = "",
105 Instruction *InsertBefore = 0)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
107 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
108 BasicBlock *InsertAtEnd)
109 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
111 MallocInst(const Type *Ty, const std::string &Name,
112 Instruction *InsertBefore = 0)
113 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
114 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
115 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
118 const std::string &Name, BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
121 const std::string &Name = "",
122 Instruction *InsertBefore = 0)
123 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, 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 &Name = "",
149 Instruction *InsertBefore = 0)
150 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
151 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
152 BasicBlock *InsertAtEnd)
153 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
155 AllocaInst(const Type *Ty, const std::string &Name,
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
158 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
159 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
161 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
162 const std::string &Name = "", Instruction *InsertBefore = 0)
163 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
164 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
165 const std::string &Name, BasicBlock *InsertAtEnd)
166 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
168 virtual AllocaInst *clone() const;
170 // Methods for support type inquiry through isa, cast, and dyn_cast:
171 static inline bool classof(const AllocaInst *) { return true; }
172 static inline bool classof(const Instruction *I) {
173 return (I->getOpcode() == Instruction::Alloca);
175 static inline bool classof(const Value *V) {
176 return isa<Instruction>(V) && classof(cast<Instruction>(V));
181 //===----------------------------------------------------------------------===//
183 //===----------------------------------------------------------------------===//
185 /// FreeInst - an instruction to deallocate memory
187 class FreeInst : public UnaryInstruction {
190 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
191 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
193 virtual FreeInst *clone() const;
195 // Accessor methods for consistency with other memory operations
196 Value *getPointerOperand() { return getOperand(0); }
197 const Value *getPointerOperand() const { return getOperand(0); }
199 // Methods for support type inquiry through isa, cast, and dyn_cast:
200 static inline bool classof(const FreeInst *) { return true; }
201 static inline bool classof(const Instruction *I) {
202 return (I->getOpcode() == Instruction::Free);
204 static inline bool classof(const Value *V) {
205 return isa<Instruction>(V) && classof(cast<Instruction>(V));
210 //===----------------------------------------------------------------------===//
212 //===----------------------------------------------------------------------===//
214 /// LoadInst - an instruction for reading from memory. This uses the
215 /// SubclassData field in Value to store whether or not the load is volatile.
217 class LoadInst : public UnaryInstruction {
219 LoadInst(const LoadInst &LI)
220 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
221 setVolatile(LI.isVolatile());
222 setAlignment(LI.getAlignment());
230 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
231 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
232 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile = false,
233 Instruction *InsertBefore = 0);
234 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
235 Instruction *InsertBefore = 0);
236 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
237 BasicBlock *InsertAtEnd);
238 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile, unsigned Align,
239 BasicBlock *InsertAtEnd);
241 LoadInst(Value *Ptr, const char *Name, Instruction *InsertBefore);
242 LoadInst(Value *Ptr, const char *Name, BasicBlock *InsertAtEnd);
243 explicit LoadInst(Value *Ptr, const char *Name = 0, bool isVolatile = false,
244 Instruction *InsertBefore = 0);
245 LoadInst(Value *Ptr, const char *Name, bool isVolatile,
246 BasicBlock *InsertAtEnd);
248 /// isVolatile - Return true if this is a load from a volatile memory
251 bool isVolatile() const { return SubclassData & 1; }
253 /// setVolatile - Specify whether this is a volatile load or not.
255 void setVolatile(bool V) {
256 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
259 virtual LoadInst *clone() const;
261 /// getAlignment - Return the alignment of the access that is being performed
263 unsigned getAlignment() const {
264 return (1 << (SubclassData>>1)) >> 1;
267 void setAlignment(unsigned Align);
269 Value *getPointerOperand() { return getOperand(0); }
270 const Value *getPointerOperand() const { return getOperand(0); }
271 static unsigned getPointerOperandIndex() { return 0U; }
273 // Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const LoadInst *) { return true; }
275 static inline bool classof(const Instruction *I) {
276 return I->getOpcode() == Instruction::Load;
278 static inline bool classof(const Value *V) {
279 return isa<Instruction>(V) && classof(cast<Instruction>(V));
284 //===----------------------------------------------------------------------===//
286 //===----------------------------------------------------------------------===//
288 /// StoreInst - an instruction for storing to memory
290 class StoreInst : public Instruction {
291 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
293 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
295 Op<0>() = SI.Op<0>();
296 Op<1>() = SI.Op<1>();
297 setVolatile(SI.isVolatile());
298 setAlignment(SI.getAlignment());
306 // allocate space for exactly two operands
307 void *operator new(size_t s) {
308 return User::operator new(s, 2);
310 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
311 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
312 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
313 Instruction *InsertBefore = 0);
314 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
315 unsigned Align, Instruction *InsertBefore = 0);
316 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
318 unsigned Align, BasicBlock *InsertAtEnd);
321 /// isVolatile - Return true if this is a load from a volatile memory
324 bool isVolatile() const { return SubclassData & 1; }
326 /// setVolatile - Specify whether this is a volatile load or not.
328 void setVolatile(bool V) {
329 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
332 /// Transparently provide more efficient getOperand methods.
333 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
335 /// getAlignment - Return the alignment of the access that is being performed
337 unsigned getAlignment() const {
338 return (1 << (SubclassData>>1)) >> 1;
341 void setAlignment(unsigned Align);
343 virtual StoreInst *clone() const;
345 Value *getPointerOperand() { return getOperand(1); }
346 const Value *getPointerOperand() const { return getOperand(1); }
347 static unsigned getPointerOperandIndex() { return 1U; }
349 // Methods for support type inquiry through isa, cast, and dyn_cast:
350 static inline bool classof(const StoreInst *) { return true; }
351 static inline bool classof(const Instruction *I) {
352 return I->getOpcode() == Instruction::Store;
354 static inline bool classof(const Value *V) {
355 return isa<Instruction>(V) && classof(cast<Instruction>(V));
360 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
363 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
365 //===----------------------------------------------------------------------===//
366 // GetElementPtrInst Class
367 //===----------------------------------------------------------------------===//
369 // checkType - Simple wrapper function to give a better assertion failure
370 // message on bad indexes for a gep instruction.
372 static inline const Type *checkType(const Type *Ty) {
373 assert(Ty && "Invalid GetElementPtrInst indices for type!");
377 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
378 /// access elements of arrays and structs
380 class GetElementPtrInst : public Instruction {
381 GetElementPtrInst(const GetElementPtrInst &GEPI);
382 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
383 const std::string &Name);
384 void init(Value *Ptr, Value *Idx, const std::string &Name);
386 template<typename InputIterator>
387 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
388 const std::string &Name,
389 // This argument ensures that we have an iterator we can
390 // do arithmetic on in constant time
391 std::random_access_iterator_tag) {
392 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
395 // This requires that the iterator points to contiguous memory.
396 init(Ptr, &*IdxBegin, NumIdx, Name); // FIXME: for the general case
397 // we have to build an array here
400 init(Ptr, 0, NumIdx, Name);
404 /// getIndexedType - Returns the type of the element that would be loaded with
405 /// a load instruction with the specified parameters.
407 /// Null is returned if the indices are invalid for the specified
410 static const Type *getIndexedType(const Type *Ptr,
411 Value* const *Idx, unsigned NumIdx);
413 template<typename InputIterator>
414 static const Type *getIndexedType(const Type *Ptr,
415 InputIterator IdxBegin,
416 InputIterator IdxEnd,
417 // This argument ensures that we
418 // have an iterator we can do
419 // arithmetic on in constant time
420 std::random_access_iterator_tag) {
421 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
424 // This requires that the iterator points to contiguous memory.
425 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
427 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
430 /// Constructors - Create a getelementptr instruction with a base pointer an
431 /// list of indices. The first ctor can optionally insert before an existing
432 /// instruction, the second appends the new instruction to the specified
434 template<typename InputIterator>
435 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
436 InputIterator IdxEnd,
438 const std::string &Name,
439 Instruction *InsertBefore);
440 template<typename InputIterator>
441 inline GetElementPtrInst(Value *Ptr,
442 InputIterator IdxBegin, InputIterator IdxEnd,
444 const std::string &Name, BasicBlock *InsertAtEnd);
446 /// Constructors - These two constructors are convenience methods because one
447 /// and two index getelementptr instructions are so common.
448 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &Name = "",
449 Instruction *InsertBefore = 0);
450 GetElementPtrInst(Value *Ptr, Value *Idx,
451 const std::string &Name, BasicBlock *InsertAtEnd);
453 template<typename InputIterator>
454 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
455 InputIterator IdxEnd,
456 const std::string &Name = "",
457 Instruction *InsertBefore = 0) {
458 typename std::iterator_traits<InputIterator>::difference_type Values =
459 1 + std::distance(IdxBegin, IdxEnd);
461 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, InsertBefore);
463 template<typename InputIterator>
464 static GetElementPtrInst *Create(Value *Ptr,
465 InputIterator IdxBegin, InputIterator IdxEnd,
466 const std::string &Name,
467 BasicBlock *InsertAtEnd) {
468 typename std::iterator_traits<InputIterator>::difference_type Values =
469 1 + std::distance(IdxBegin, IdxEnd);
471 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, Name, InsertAtEnd);
474 /// Constructors - These two creators are convenience methods because one
475 /// index getelementptr instructions are so common.
476 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
477 const std::string &Name = "",
478 Instruction *InsertBefore = 0) {
479 return new(2) GetElementPtrInst(Ptr, Idx, Name, InsertBefore);
481 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
482 const std::string &Name,
483 BasicBlock *InsertAtEnd) {
484 return new(2) GetElementPtrInst(Ptr, Idx, Name, InsertAtEnd);
487 virtual GetElementPtrInst *clone() const;
489 /// Transparently provide more efficient getOperand methods.
490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
492 // getType - Overload to return most specific pointer type...
493 const PointerType *getType() const {
494 return reinterpret_cast<const PointerType*>(Instruction::getType());
497 /// getIndexedType - Returns the type of the element that would be loaded with
498 /// a load instruction with the specified parameters.
500 /// Null is returned if the indices are invalid for the specified
503 template<typename InputIterator>
504 static const Type *getIndexedType(const Type *Ptr,
505 InputIterator IdxBegin,
506 InputIterator IdxEnd) {
507 return getIndexedType(Ptr, IdxBegin, IdxEnd,
508 typename std::iterator_traits<InputIterator>::
509 iterator_category());
511 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
513 inline op_iterator idx_begin() { return op_begin()+1; }
514 inline const_op_iterator idx_begin() const { return op_begin()+1; }
515 inline op_iterator idx_end() { return op_end(); }
516 inline const_op_iterator idx_end() const { return op_end(); }
518 Value *getPointerOperand() {
519 return getOperand(0);
521 const Value *getPointerOperand() const {
522 return getOperand(0);
524 static unsigned getPointerOperandIndex() {
525 return 0U; // get index for modifying correct operand
528 unsigned getNumIndices() const { // Note: always non-negative
529 return getNumOperands() - 1;
532 bool hasIndices() const {
533 return getNumOperands() > 1;
536 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
537 /// zeros. If so, the result pointer and the first operand have the same
538 /// value, just potentially different types.
539 bool hasAllZeroIndices() const;
541 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
542 /// constant integers. If so, the result pointer and the first operand have
543 /// a constant offset between them.
544 bool hasAllConstantIndices() const;
547 // Methods for support type inquiry through isa, cast, and dyn_cast:
548 static inline bool classof(const GetElementPtrInst *) { return true; }
549 static inline bool classof(const Instruction *I) {
550 return (I->getOpcode() == Instruction::GetElementPtr);
552 static inline bool classof(const Value *V) {
553 return isa<Instruction>(V) && classof(cast<Instruction>(V));
558 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
561 template<typename InputIterator>
562 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
563 InputIterator IdxBegin,
564 InputIterator IdxEnd,
566 const std::string &Name,
567 Instruction *InsertBefore)
568 : Instruction(PointerType::get(checkType(
569 getIndexedType(Ptr->getType(),
571 cast<PointerType>(Ptr->getType())
572 ->getAddressSpace()),
574 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
575 Values, InsertBefore) {
576 init(Ptr, IdxBegin, IdxEnd, Name,
577 typename std::iterator_traits<InputIterator>::iterator_category());
579 template<typename InputIterator>
580 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
581 InputIterator IdxBegin,
582 InputIterator IdxEnd,
584 const std::string &Name,
585 BasicBlock *InsertAtEnd)
586 : Instruction(PointerType::get(checkType(
587 getIndexedType(Ptr->getType(),
589 cast<PointerType>(Ptr->getType())
590 ->getAddressSpace()),
592 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
593 Values, InsertAtEnd) {
594 init(Ptr, IdxBegin, IdxEnd, Name,
595 typename std::iterator_traits<InputIterator>::iterator_category());
599 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
602 //===----------------------------------------------------------------------===//
604 //===----------------------------------------------------------------------===//
606 /// This instruction compares its operands according to the predicate given
607 /// to the constructor. It only operates on integers or pointers. The operands
608 /// must be identical types.
609 /// @brief Represent an integer comparison operator.
610 class ICmpInst: public CmpInst {
612 /// @brief Constructor with insert-before-instruction semantics.
614 Predicate pred, ///< The predicate to use for the comparison
615 Value *LHS, ///< The left-hand-side of the expression
616 Value *RHS, ///< The right-hand-side of the expression
617 const std::string &Name = "", ///< Name of the instruction
618 Instruction *InsertBefore = 0 ///< Where to insert
619 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, Name,
621 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
622 pred <= CmpInst::LAST_ICMP_PREDICATE &&
623 "Invalid ICmp predicate value");
624 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
625 "Both operands to ICmp instruction are not of the same type!");
626 // Check that the operands are the right type
627 assert((getOperand(0)->getType()->isInteger() ||
628 isa<PointerType>(getOperand(0)->getType())) &&
629 "Invalid operand types for ICmp instruction");
632 /// @brief Constructor with insert-at-block-end semantics.
634 Predicate pred, ///< The predicate to use for the comparison
635 Value *LHS, ///< The left-hand-side of the expression
636 Value *RHS, ///< The right-hand-side of the expression
637 const std::string &Name, ///< Name of the instruction
638 BasicBlock *InsertAtEnd ///< Block to insert into.
639 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, Name,
641 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
642 pred <= CmpInst::LAST_ICMP_PREDICATE &&
643 "Invalid ICmp predicate value");
644 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
645 "Both operands to ICmp instruction are not of the same type!");
646 // Check that the operands are the right type
647 assert((getOperand(0)->getType()->isInteger() ||
648 isa<PointerType>(getOperand(0)->getType())) &&
649 "Invalid operand types for ICmp instruction");
652 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
653 /// @returns the predicate that would be the result if the operand were
654 /// regarded as signed.
655 /// @brief Return the signed version of the predicate
656 Predicate getSignedPredicate() const {
657 return getSignedPredicate(getPredicate());
660 /// This is a static version that you can use without an instruction.
661 /// @brief Return the signed version of the predicate.
662 static Predicate getSignedPredicate(Predicate pred);
664 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
665 /// @returns the predicate that would be the result if the operand were
666 /// regarded as unsigned.
667 /// @brief Return the unsigned version of the predicate
668 Predicate getUnsignedPredicate() const {
669 return getUnsignedPredicate(getPredicate());
672 /// This is a static version that you can use without an instruction.
673 /// @brief Return the unsigned version of the predicate.
674 static Predicate getUnsignedPredicate(Predicate pred);
676 /// isEquality - Return true if this predicate is either EQ or NE. This also
677 /// tests for commutativity.
678 static bool isEquality(Predicate P) {
679 return P == ICMP_EQ || P == ICMP_NE;
682 /// isEquality - Return true if this predicate is either EQ or NE. This also
683 /// tests for commutativity.
684 bool isEquality() const {
685 return isEquality(getPredicate());
688 /// @returns true if the predicate of this ICmpInst is commutative
689 /// @brief Determine if this relation is commutative.
690 bool isCommutative() const { return isEquality(); }
692 /// isRelational - Return true if the predicate is relational (not EQ or NE).
694 bool isRelational() const {
695 return !isEquality();
698 /// isRelational - Return true if the predicate is relational (not EQ or NE).
700 static bool isRelational(Predicate P) {
701 return !isEquality(P);
704 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
705 /// @brief Determine if this instruction's predicate is signed.
706 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
708 /// @returns true if the predicate provided is signed, false otherwise
709 /// @brief Determine if the predicate is signed.
710 static bool isSignedPredicate(Predicate pred);
712 /// @returns true if the specified compare predicate is
713 /// true when both operands are equal...
714 /// @brief Determine if the icmp is true when both operands are equal
715 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
716 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
717 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
718 pred == ICmpInst::ICMP_SLE;
721 /// @returns true if the specified compare instruction is
722 /// true when both operands are equal...
723 /// @brief Determine if the ICmpInst returns true when both operands are equal
724 bool isTrueWhenEqual() {
725 return isTrueWhenEqual(getPredicate());
728 /// Initialize a set of values that all satisfy the predicate with C.
729 /// @brief Make a ConstantRange for a relation with a constant value.
730 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
732 /// Exchange the two operands to this instruction in such a way that it does
733 /// not modify the semantics of the instruction. The predicate value may be
734 /// changed to retain the same result if the predicate is order dependent
736 /// @brief Swap operands and adjust predicate.
737 void swapOperands() {
738 SubclassData = getSwappedPredicate();
739 Op<0>().swap(Op<1>());
742 virtual ICmpInst *clone() const;
744 // Methods for support type inquiry through isa, cast, and dyn_cast:
745 static inline bool classof(const ICmpInst *) { return true; }
746 static inline bool classof(const Instruction *I) {
747 return I->getOpcode() == Instruction::ICmp;
749 static inline bool classof(const Value *V) {
750 return isa<Instruction>(V) && classof(cast<Instruction>(V));
754 //===----------------------------------------------------------------------===//
756 //===----------------------------------------------------------------------===//
758 /// This instruction compares its operands according to the predicate given
759 /// to the constructor. It only operates on floating point values or packed
760 /// vectors of floating point values. The operands must be identical types.
761 /// @brief Represents a floating point comparison operator.
762 class FCmpInst: public CmpInst {
764 /// @brief Constructor with insert-before-instruction semantics.
766 Predicate pred, ///< The predicate to use for the comparison
767 Value *LHS, ///< The left-hand-side of the expression
768 Value *RHS, ///< The right-hand-side of the expression
769 const std::string &Name = "", ///< Name of the instruction
770 Instruction *InsertBefore = 0 ///< Where to insert
771 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, Name,
773 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
774 "Invalid FCmp predicate value");
775 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
776 "Both operands to FCmp instruction are not of the same type!");
777 // Check that the operands are the right type
778 assert(getOperand(0)->getType()->isFloatingPoint() &&
779 "Invalid operand types for FCmp instruction");
782 /// @brief Constructor with insert-at-block-end semantics.
784 Predicate pred, ///< The predicate to use for the comparison
785 Value *LHS, ///< The left-hand-side of the expression
786 Value *RHS, ///< The right-hand-side of the expression
787 const std::string &Name, ///< Name of the instruction
788 BasicBlock *InsertAtEnd ///< Block to insert into.
789 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, Name,
791 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
792 "Invalid FCmp predicate value");
793 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
794 "Both operands to FCmp instruction are not of the same type!");
795 // Check that the operands are the right type
796 assert(getOperand(0)->getType()->isFloatingPoint() &&
797 "Invalid operand types for FCmp instruction");
800 /// This also tests for commutativity. If isEquality() returns true then
801 /// the predicate is also commutative. Only the equality predicates are
803 /// @returns true if the predicate of this instruction is EQ or NE.
804 /// @brief Determine if this is an equality predicate.
805 bool isEquality() const {
806 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
807 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
809 bool isCommutative() const { return isEquality(); }
811 /// @returns true if the predicate is relational (not EQ or NE).
812 /// @brief Determine if this a relational predicate.
813 bool isRelational() const { return !isEquality(); }
815 /// Exchange the two operands to this instruction in such a way that it does
816 /// not modify the semantics of the instruction. The predicate value may be
817 /// changed to retain the same result if the predicate is order dependent
819 /// @brief Swap operands and adjust predicate.
820 void swapOperands() {
821 SubclassData = getSwappedPredicate();
822 Op<0>().swap(Op<1>());
825 virtual FCmpInst *clone() const;
827 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
828 static inline bool classof(const FCmpInst *) { return true; }
829 static inline bool classof(const Instruction *I) {
830 return I->getOpcode() == Instruction::FCmp;
832 static inline bool classof(const Value *V) {
833 return isa<Instruction>(V) && classof(cast<Instruction>(V));
837 //===----------------------------------------------------------------------===//
839 //===----------------------------------------------------------------------===//
841 /// This instruction compares its operands according to the predicate given
842 /// to the constructor. It only operates on vectors of integers.
843 /// The operands must be identical types.
844 /// @brief Represents a vector integer comparison operator.
845 class VICmpInst: public CmpInst {
847 /// @brief Constructor with insert-before-instruction semantics.
849 Predicate pred, ///< The predicate to use for the comparison
850 Value *LHS, ///< The left-hand-side of the expression
851 Value *RHS, ///< The right-hand-side of the expression
852 const std::string &Name = "", ///< Name of the instruction
853 Instruction *InsertBefore = 0 ///< Where to insert
854 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, Name,
856 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
857 pred <= CmpInst::LAST_ICMP_PREDICATE &&
858 "Invalid VICmp predicate value");
859 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
860 "Both operands to VICmp instruction are not of the same type!");
863 /// @brief Constructor with insert-at-block-end semantics.
865 Predicate pred, ///< The predicate to use for the comparison
866 Value *LHS, ///< The left-hand-side of the expression
867 Value *RHS, ///< The right-hand-side of the expression
868 const std::string &Name, ///< Name of the instruction
869 BasicBlock *InsertAtEnd ///< Block to insert into.
870 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, Name,
872 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
873 pred <= CmpInst::LAST_ICMP_PREDICATE &&
874 "Invalid VICmp predicate value");
875 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
876 "Both operands to VICmp instruction are not of the same type!");
879 /// @brief Return the predicate for this instruction.
880 Predicate getPredicate() const { return Predicate(SubclassData); }
882 virtual VICmpInst *clone() const;
884 // Methods for support type inquiry through isa, cast, and dyn_cast:
885 static inline bool classof(const VICmpInst *) { return true; }
886 static inline bool classof(const Instruction *I) {
887 return I->getOpcode() == Instruction::VICmp;
889 static inline bool classof(const Value *V) {
890 return isa<Instruction>(V) && classof(cast<Instruction>(V));
894 //===----------------------------------------------------------------------===//
896 //===----------------------------------------------------------------------===//
898 /// This instruction compares its operands according to the predicate given
899 /// to the constructor. It only operates on vectors of floating point values.
900 /// The operands must be identical types.
901 /// @brief Represents a vector floating point comparison operator.
902 class VFCmpInst: public CmpInst {
904 /// @brief Constructor with insert-before-instruction semantics.
906 Predicate pred, ///< The predicate to use for the comparison
907 Value *LHS, ///< The left-hand-side of the expression
908 Value *RHS, ///< The right-hand-side of the expression
909 const std::string &Name = "", ///< Name of the instruction
910 Instruction *InsertBefore = 0 ///< Where to insert
911 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
912 Instruction::VFCmp, pred, LHS, RHS, Name, InsertBefore) {
913 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
914 "Invalid VFCmp predicate value");
915 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
916 "Both operands to VFCmp instruction are not of the same type!");
919 /// @brief Constructor with insert-at-block-end semantics.
921 Predicate pred, ///< The predicate to use for the comparison
922 Value *LHS, ///< The left-hand-side of the expression
923 Value *RHS, ///< The right-hand-side of the expression
924 const std::string &Name, ///< Name of the instruction
925 BasicBlock *InsertAtEnd ///< Block to insert into.
926 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
927 Instruction::VFCmp, pred, LHS, RHS, Name, InsertAtEnd) {
928 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
929 "Invalid VFCmp predicate value");
930 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
931 "Both operands to VFCmp instruction are not of the same type!");
934 /// @brief Return the predicate for this instruction.
935 Predicate getPredicate() const { return Predicate(SubclassData); }
937 virtual VFCmpInst *clone() const;
939 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
940 static inline bool classof(const VFCmpInst *) { return true; }
941 static inline bool classof(const Instruction *I) {
942 return I->getOpcode() == Instruction::VFCmp;
944 static inline bool classof(const Value *V) {
945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
949 //===----------------------------------------------------------------------===//
951 //===----------------------------------------------------------------------===//
952 /// CallInst - This class represents a function call, abstracting a target
953 /// machine's calling convention. This class uses low bit of the SubClassData
954 /// field to indicate whether or not this is a tail call. The rest of the bits
955 /// hold the calling convention of the call.
958 class CallInst : public Instruction {
959 PAListPtr ParamAttrs; ///< parameter attributes for call
960 CallInst(const CallInst &CI);
961 void init(Value *Func, Value* const *Params, unsigned NumParams);
962 void init(Value *Func, Value *Actual1, Value *Actual2);
963 void init(Value *Func, Value *Actual);
964 void init(Value *Func);
966 template<typename InputIterator>
967 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
968 const std::string &Name,
969 // This argument ensures that we have an iterator we can
970 // do arithmetic on in constant time
971 std::random_access_iterator_tag) {
972 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
974 // This requires that the iterator points to contiguous memory.
975 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
979 /// Construct a CallInst given a range of arguments. InputIterator
980 /// must be a random-access iterator pointing to contiguous storage
981 /// (e.g. a std::vector<>::iterator). Checks are made for
982 /// random-accessness but not for contiguous storage as that would
983 /// incur runtime overhead.
984 /// @brief Construct a CallInst from a range of arguments
985 template<typename InputIterator>
986 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
987 const std::string &Name, Instruction *InsertBefore);
989 /// Construct a CallInst given a range of arguments. InputIterator
990 /// must be a random-access iterator pointing to contiguous storage
991 /// (e.g. a std::vector<>::iterator). Checks are made for
992 /// random-accessness but not for contiguous storage as that would
993 /// incur runtime overhead.
994 /// @brief Construct a CallInst from a range of arguments
995 template<typename InputIterator>
996 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
997 const std::string &Name, BasicBlock *InsertAtEnd);
999 CallInst(Value *F, Value *Actual, const std::string& Name,
1000 Instruction *InsertBefore);
1001 CallInst(Value *F, Value *Actual, const std::string& Name,
1002 BasicBlock *InsertAtEnd);
1003 explicit CallInst(Value *F, const std::string &Name,
1004 Instruction *InsertBefore);
1005 CallInst(Value *F, const std::string &Name, BasicBlock *InsertAtEnd);
1007 template<typename InputIterator>
1008 static CallInst *Create(Value *Func,
1009 InputIterator ArgBegin, InputIterator ArgEnd,
1010 const std::string &Name = "",
1011 Instruction *InsertBefore = 0) {
1012 return new((unsigned)(ArgEnd - ArgBegin + 1))
1013 CallInst(Func, ArgBegin, ArgEnd, Name, InsertBefore);
1015 template<typename InputIterator>
1016 static CallInst *Create(Value *Func,
1017 InputIterator ArgBegin, InputIterator ArgEnd,
1018 const std::string &Name, BasicBlock *InsertAtEnd) {
1019 return new((unsigned)(ArgEnd - ArgBegin + 1))
1020 CallInst(Func, ArgBegin, ArgEnd, Name, InsertAtEnd);
1022 static CallInst *Create(Value *F, Value *Actual, const std::string& Name = "",
1023 Instruction *InsertBefore = 0) {
1024 return new(2) CallInst(F, Actual, Name, InsertBefore);
1026 static CallInst *Create(Value *F, Value *Actual, const std::string& Name,
1027 BasicBlock *InsertAtEnd) {
1028 return new(2) CallInst(F, Actual, Name, InsertAtEnd);
1030 static CallInst *Create(Value *F, const std::string &Name = "",
1031 Instruction *InsertBefore = 0) {
1032 return new(1) CallInst(F, Name, InsertBefore);
1034 static CallInst *Create(Value *F, const std::string &Name,
1035 BasicBlock *InsertAtEnd) {
1036 return new(1) CallInst(F, Name, InsertAtEnd);
1041 virtual CallInst *clone() const;
1043 /// Provide fast operand accessors
1044 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1046 bool isTailCall() const { return SubclassData & 1; }
1047 void setTailCall(bool isTailCall = true) {
1048 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
1051 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1053 unsigned getCallingConv() const { return SubclassData >> 1; }
1054 void setCallingConv(unsigned CC) {
1055 SubclassData = (SubclassData & 1) | (CC << 1);
1058 /// getParamAttrs - Return the parameter attributes for this call.
1060 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
1062 /// setParamAttrs - Sets the parameter attributes for this call.
1063 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
1065 /// addParamAttr - adds the attribute to the list of attributes.
1066 void addParamAttr(unsigned i, ParameterAttributes attr);
1068 /// @brief Determine whether the call or the callee has the given attribute.
1069 bool paramHasAttr(unsigned i, unsigned attr) const;
1071 /// @brief Extract the alignment for a call or parameter (0=unknown).
1072 unsigned getParamAlignment(unsigned i) const {
1073 return ParamAttrs.getParamAlignment(i);
1076 /// @brief Determine if the call does not access memory.
1077 bool doesNotAccessMemory() const {
1078 return paramHasAttr(0, ParamAttr::ReadNone);
1081 /// @brief Determine if the call does not access or only reads memory.
1082 bool onlyReadsMemory() const {
1083 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1086 /// @brief Determine if the call cannot return.
1087 bool doesNotReturn() const {
1088 return paramHasAttr(0, ParamAttr::NoReturn);
1091 /// @brief Determine if the call cannot unwind.
1092 bool doesNotThrow() const {
1093 return paramHasAttr(0, ParamAttr::NoUnwind);
1095 void setDoesNotThrow(bool doesNotThrow = true);
1097 /// @brief Determine if the call returns a structure through first
1098 /// pointer argument.
1099 bool hasStructRetAttr() const {
1100 // Be friendly and also check the callee.
1101 return paramHasAttr(1, ParamAttr::StructRet);
1104 /// @brief Determine if any call argument is an aggregate passed by value.
1105 bool hasByValArgument() const {
1106 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1109 /// getCalledFunction - Return the function being called by this instruction
1110 /// if it is a direct call. If it is a call through a function pointer,
1112 Function *getCalledFunction() const {
1113 return dyn_cast<Function>(getOperand(0));
1116 /// getCalledValue - Get a pointer to the function that is invoked by this
1118 const Value *getCalledValue() const { return getOperand(0); }
1119 Value *getCalledValue() { return getOperand(0); }
1121 // Methods for support type inquiry through isa, cast, and dyn_cast:
1122 static inline bool classof(const CallInst *) { return true; }
1123 static inline bool classof(const Instruction *I) {
1124 return I->getOpcode() == Instruction::Call;
1126 static inline bool classof(const Value *V) {
1127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1132 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1135 template<typename InputIterator>
1136 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1137 const std::string &Name, BasicBlock *InsertAtEnd)
1138 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1139 ->getElementType())->getReturnType(),
1141 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1142 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1143 init(Func, ArgBegin, ArgEnd, Name,
1144 typename std::iterator_traits<InputIterator>::iterator_category());
1147 template<typename InputIterator>
1148 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1149 const std::string &Name, Instruction *InsertBefore)
1150 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1151 ->getElementType())->getReturnType(),
1153 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1154 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1155 init(Func, ArgBegin, ArgEnd, Name,
1156 typename std::iterator_traits<InputIterator>::iterator_category());
1159 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1161 //===----------------------------------------------------------------------===//
1163 //===----------------------------------------------------------------------===//
1165 /// SelectInst - This class represents the LLVM 'select' instruction.
1167 class SelectInst : public Instruction {
1168 void init(Value *C, Value *S1, Value *S2) {
1174 SelectInst(const SelectInst &SI)
1175 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1176 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1178 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1179 Instruction *InsertBefore)
1180 : Instruction(S1->getType(), Instruction::Select,
1181 &Op<0>(), 3, InsertBefore) {
1185 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
1186 BasicBlock *InsertAtEnd)
1187 : Instruction(S1->getType(), Instruction::Select,
1188 &Op<0>(), 3, InsertAtEnd) {
1193 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1194 const std::string &Name = "",
1195 Instruction *InsertBefore = 0) {
1196 return new(3) SelectInst(C, S1, S2, Name, InsertBefore);
1198 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1199 const std::string &Name, BasicBlock *InsertAtEnd) {
1200 return new(3) SelectInst(C, S1, S2, Name, InsertAtEnd);
1203 Value *getCondition() const { return Op<0>(); }
1204 Value *getTrueValue() const { return Op<1>(); }
1205 Value *getFalseValue() const { return Op<2>(); }
1207 /// Transparently provide more efficient getOperand methods.
1208 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1210 OtherOps getOpcode() const {
1211 return static_cast<OtherOps>(Instruction::getOpcode());
1214 virtual SelectInst *clone() const;
1216 // Methods for support type inquiry through isa, cast, and dyn_cast:
1217 static inline bool classof(const SelectInst *) { return true; }
1218 static inline bool classof(const Instruction *I) {
1219 return I->getOpcode() == Instruction::Select;
1221 static inline bool classof(const Value *V) {
1222 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1227 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1230 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1232 //===----------------------------------------------------------------------===//
1234 //===----------------------------------------------------------------------===//
1236 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1237 /// an argument of the specified type given a va_list and increments that list
1239 class VAArgInst : public UnaryInstruction {
1240 VAArgInst(const VAArgInst &VAA)
1241 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1243 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
1244 Instruction *InsertBefore = 0)
1245 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1248 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
1249 BasicBlock *InsertAtEnd)
1250 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1254 virtual VAArgInst *clone() const;
1256 // Methods for support type inquiry through isa, cast, and dyn_cast:
1257 static inline bool classof(const VAArgInst *) { return true; }
1258 static inline bool classof(const Instruction *I) {
1259 return I->getOpcode() == VAArg;
1261 static inline bool classof(const Value *V) {
1262 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1266 //===----------------------------------------------------------------------===//
1267 // ExtractElementInst Class
1268 //===----------------------------------------------------------------------===//
1270 /// ExtractElementInst - This instruction extracts a single (scalar)
1271 /// element from a VectorType value
1273 class ExtractElementInst : public Instruction {
1274 ExtractElementInst(const ExtractElementInst &EE) :
1275 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1276 Op<0>() = EE.Op<0>();
1277 Op<1>() = EE.Op<1>();
1281 // allocate space for exactly two operands
1282 void *operator new(size_t s) {
1283 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1285 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
1286 Instruction *InsertBefore = 0);
1287 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name = "",
1288 Instruction *InsertBefore = 0);
1289 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
1290 BasicBlock *InsertAtEnd);
1291 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &Name,
1292 BasicBlock *InsertAtEnd);
1294 /// isValidOperands - Return true if an extractelement instruction can be
1295 /// formed with the specified operands.
1296 static bool isValidOperands(const Value *Vec, const Value *Idx);
1298 virtual ExtractElementInst *clone() const;
1300 /// Transparently provide more efficient getOperand methods.
1301 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1303 // Methods for support type inquiry through isa, cast, and dyn_cast:
1304 static inline bool classof(const ExtractElementInst *) { return true; }
1305 static inline bool classof(const Instruction *I) {
1306 return I->getOpcode() == Instruction::ExtractElement;
1308 static inline bool classof(const Value *V) {
1309 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1314 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1317 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1319 //===----------------------------------------------------------------------===//
1320 // InsertElementInst Class
1321 //===----------------------------------------------------------------------===//
1323 /// InsertElementInst - This instruction inserts a single (scalar)
1324 /// element into a VectorType value
1326 class InsertElementInst : public Instruction {
1327 InsertElementInst(const InsertElementInst &IE);
1328 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1329 const std::string &Name = "",Instruction *InsertBefore = 0);
1330 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1331 const std::string &Name = "",Instruction *InsertBefore = 0);
1332 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1333 const std::string &Name, BasicBlock *InsertAtEnd);
1334 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1335 const std::string &Name, BasicBlock *InsertAtEnd);
1337 static InsertElementInst *Create(const InsertElementInst &IE) {
1338 return new(IE.getNumOperands()) InsertElementInst(IE);
1340 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1341 const std::string &Name = "",
1342 Instruction *InsertBefore = 0) {
1343 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1345 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1346 const std::string &Name = "",
1347 Instruction *InsertBefore = 0) {
1348 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertBefore);
1350 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1351 const std::string &Name,
1352 BasicBlock *InsertAtEnd) {
1353 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1355 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1356 const std::string &Name,
1357 BasicBlock *InsertAtEnd) {
1358 return new(3) InsertElementInst(Vec, NewElt, Idx, Name, InsertAtEnd);
1361 /// isValidOperands - Return true if an insertelement instruction can be
1362 /// formed with the specified operands.
1363 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1366 virtual InsertElementInst *clone() const;
1368 /// getType - Overload to return most specific vector type.
1370 const VectorType *getType() const {
1371 return reinterpret_cast<const VectorType*>(Instruction::getType());
1374 /// Transparently provide more efficient getOperand methods.
1375 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1377 // Methods for support type inquiry through isa, cast, and dyn_cast:
1378 static inline bool classof(const InsertElementInst *) { return true; }
1379 static inline bool classof(const Instruction *I) {
1380 return I->getOpcode() == Instruction::InsertElement;
1382 static inline bool classof(const Value *V) {
1383 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1388 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1391 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1393 //===----------------------------------------------------------------------===//
1394 // ShuffleVectorInst Class
1395 //===----------------------------------------------------------------------===//
1397 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1400 class ShuffleVectorInst : public Instruction {
1401 ShuffleVectorInst(const ShuffleVectorInst &IE);
1403 // allocate space for exactly three operands
1404 void *operator new(size_t s) {
1405 return User::operator new(s, 3);
1407 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1408 const std::string &Name = "", Instruction *InsertBefor = 0);
1409 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1410 const std::string &Name, BasicBlock *InsertAtEnd);
1412 /// isValidOperands - Return true if a shufflevector instruction can be
1413 /// formed with the specified operands.
1414 static bool isValidOperands(const Value *V1, const Value *V2,
1417 virtual ShuffleVectorInst *clone() const;
1419 /// getType - Overload to return most specific vector type.
1421 const VectorType *getType() const {
1422 return reinterpret_cast<const VectorType*>(Instruction::getType());
1425 /// Transparently provide more efficient getOperand methods.
1426 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1428 /// getMaskValue - Return the index from the shuffle mask for the specified
1429 /// output result. This is either -1 if the element is undef or a number less
1430 /// than 2*numelements.
1431 int getMaskValue(unsigned i) const;
1433 // Methods for support type inquiry through isa, cast, and dyn_cast:
1434 static inline bool classof(const ShuffleVectorInst *) { return true; }
1435 static inline bool classof(const Instruction *I) {
1436 return I->getOpcode() == Instruction::ShuffleVector;
1438 static inline bool classof(const Value *V) {
1439 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1444 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1447 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1449 //===----------------------------------------------------------------------===//
1450 // ExtractValueInst Class
1451 //===----------------------------------------------------------------------===//
1453 /// ExtractValueInst - This instruction extracts a struct member or array
1454 /// element value from an aggregate value.
1456 class ExtractValueInst : public UnaryInstruction {
1457 SmallVector<unsigned, 4> Indices;
1459 ExtractValueInst(const ExtractValueInst &EVI);
1460 void init(const unsigned *Idx, unsigned NumIdx,
1461 const std::string &Name);
1462 void init(unsigned Idx, const std::string &Name);
1464 template<typename InputIterator>
1465 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1466 const std::string &Name,
1467 // This argument ensures that we have an iterator we can
1468 // do arithmetic on in constant time
1469 std::random_access_iterator_tag) {
1470 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1472 // There's no fundamental reason why we require at least one index
1473 // (other than weirdness with &*IdxBegin being invalid; see
1474 // getelementptr's init routine for example). But there's no
1475 // present need to support it.
1476 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1478 // This requires that the iterator points to contiguous memory.
1479 init(&*IdxBegin, NumIdx, Name); // FIXME: for the general case
1480 // we have to build an array here
1483 /// getIndexedType - Returns the type of the element that would be extracted
1484 /// with an extractvalue instruction with the specified parameters.
1486 /// Null is returned if the indices are invalid for the specified
1489 static const Type *getIndexedType(const Type *Agg,
1490 const unsigned *Idx, unsigned NumIdx);
1492 template<typename InputIterator>
1493 static const Type *getIndexedType(const Type *Ptr,
1494 InputIterator IdxBegin,
1495 InputIterator IdxEnd,
1496 // This argument ensures that we
1497 // have an iterator we can do
1498 // arithmetic on in constant time
1499 std::random_access_iterator_tag) {
1500 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1503 // This requires that the iterator points to contiguous memory.
1504 return getIndexedType(Ptr, (const unsigned *)&*IdxBegin, NumIdx);
1506 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1509 /// Constructors - Create a extractvalue instruction with a base aggregate
1510 /// value and a list of indices. The first ctor can optionally insert before
1511 /// an existing instruction, the second appends the new instruction to the
1512 /// specified BasicBlock.
1513 template<typename InputIterator>
1514 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1515 InputIterator IdxEnd,
1516 const std::string &Name,
1517 Instruction *InsertBefore);
1518 template<typename InputIterator>
1519 inline ExtractValueInst(Value *Agg,
1520 InputIterator IdxBegin, InputIterator IdxEnd,
1521 const std::string &Name, BasicBlock *InsertAtEnd);
1523 /// Constructors - These two constructors are convenience methods because one
1524 /// and two index extractvalue instructions are so common.
1525 ExtractValueInst(Value *Agg, unsigned Idx, const std::string &Name = "",
1526 Instruction *InsertBefore = 0);
1527 ExtractValueInst(Value *Agg, unsigned Idx,
1528 const std::string &Name, BasicBlock *InsertAtEnd);
1530 // allocate space for exactly one operand
1531 void *operator new(size_t s) {
1532 return User::operator new(s, 1);
1536 template<typename InputIterator>
1537 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1538 InputIterator IdxEnd,
1539 const std::string &Name = "",
1540 Instruction *InsertBefore = 0) {
1542 ExtractValueInst(Agg, IdxBegin, IdxEnd, Name, InsertBefore);
1544 template<typename InputIterator>
1545 static ExtractValueInst *Create(Value *Agg,
1546 InputIterator IdxBegin, InputIterator IdxEnd,
1547 const std::string &Name,
1548 BasicBlock *InsertAtEnd) {
1549 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, Name, InsertAtEnd);
1552 /// Constructors - These two creators are convenience methods because one
1553 /// index extractvalue instructions are much more common than those with
1555 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1556 const std::string &Name = "",
1557 Instruction *InsertBefore = 0) {
1558 return new ExtractValueInst(Agg, Idx, Name, InsertBefore);
1560 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1561 const std::string &Name,
1562 BasicBlock *InsertAtEnd) {
1563 return new ExtractValueInst(Agg, Idx, Name, InsertAtEnd);
1566 virtual ExtractValueInst *clone() const;
1568 // getType - Overload to return most specific pointer type...
1569 const PointerType *getType() const {
1570 return reinterpret_cast<const PointerType*>(Instruction::getType());
1573 /// getIndexedType - Returns the type of the element that would be extracted
1574 /// with an extractvalue instruction with the specified parameters.
1576 /// Null is returned if the indices are invalid for the specified
1579 template<typename InputIterator>
1580 static const Type *getIndexedType(const Type *Ptr,
1581 InputIterator IdxBegin,
1582 InputIterator IdxEnd) {
1583 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1584 typename std::iterator_traits<InputIterator>::
1585 iterator_category());
1587 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1589 typedef const unsigned* idx_iterator;
1590 inline idx_iterator idx_begin() const { return Indices.begin(); }
1591 inline idx_iterator idx_end() const { return Indices.end(); }
1593 Value *getAggregateOperand() {
1594 return getOperand(0);
1596 const Value *getAggregateOperand() const {
1597 return getOperand(0);
1599 static unsigned getAggregateOperandIndex() {
1600 return 0U; // get index for modifying correct operand
1603 unsigned getNumIndices() const { // Note: always non-negative
1604 return (unsigned)Indices.size();
1607 bool hasIndices() const {
1611 // Methods for support type inquiry through isa, cast, and dyn_cast:
1612 static inline bool classof(const ExtractValueInst *) { return true; }
1613 static inline bool classof(const Instruction *I) {
1614 return I->getOpcode() == Instruction::ExtractValue;
1616 static inline bool classof(const Value *V) {
1617 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1621 template<typename InputIterator>
1622 ExtractValueInst::ExtractValueInst(Value *Agg,
1623 InputIterator IdxBegin,
1624 InputIterator IdxEnd,
1625 const std::string &Name,
1626 Instruction *InsertBefore)
1627 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1629 ExtractValue, Agg, InsertBefore) {
1630 init(IdxBegin, IdxEnd, Name,
1631 typename std::iterator_traits<InputIterator>::iterator_category());
1633 template<typename InputIterator>
1634 ExtractValueInst::ExtractValueInst(Value *Agg,
1635 InputIterator IdxBegin,
1636 InputIterator IdxEnd,
1637 const std::string &Name,
1638 BasicBlock *InsertAtEnd)
1639 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1641 ExtractValue, Agg, InsertAtEnd) {
1642 init(IdxBegin, IdxEnd, Name,
1643 typename std::iterator_traits<InputIterator>::iterator_category());
1647 //===----------------------------------------------------------------------===//
1648 // InsertValueInst Class
1649 //===----------------------------------------------------------------------===//
1651 /// InsertValueInst - This instruction inserts a struct field of array element
1652 /// value into an aggregate value.
1654 class InsertValueInst : public Instruction {
1655 SmallVector<unsigned, 4> Indices;
1657 void *operator new(size_t, unsigned); // Do not implement
1658 InsertValueInst(const InsertValueInst &IVI);
1659 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1660 const std::string &Name);
1661 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &Name);
1663 template<typename InputIterator>
1664 void init(Value *Agg, Value *Val,
1665 InputIterator IdxBegin, InputIterator IdxEnd,
1666 const std::string &Name,
1667 // This argument ensures that we have an iterator we can
1668 // do arithmetic on in constant time
1669 std::random_access_iterator_tag) {
1670 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1672 // There's no fundamental reason why we require at least one index
1673 // (other than weirdness with &*IdxBegin being invalid; see
1674 // getelementptr's init routine for example). But there's no
1675 // present need to support it.
1676 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1678 // This requires that the iterator points to contiguous memory.
1679 init(Agg, Val, &*IdxBegin, NumIdx, Name); // FIXME: for the general case
1680 // we have to build an array here
1683 /// Constructors - Create a insertvalue instruction with a base aggregate
1684 /// value, a value to insert, and a list of indices. The first ctor can
1685 /// optionally insert before an existing instruction, the second appends
1686 /// the new instruction to the specified BasicBlock.
1687 template<typename InputIterator>
1688 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1689 InputIterator IdxEnd,
1690 const std::string &Name,
1691 Instruction *InsertBefore);
1692 template<typename InputIterator>
1693 inline InsertValueInst(Value *Agg, Value *Val,
1694 InputIterator IdxBegin, InputIterator IdxEnd,
1695 const std::string &Name, BasicBlock *InsertAtEnd);
1697 /// Constructors - These two constructors are convenience methods because one
1698 /// and two index insertvalue instructions are so common.
1699 InsertValueInst(Value *Agg, Value *Val,
1700 unsigned Idx, const std::string &Name = "",
1701 Instruction *InsertBefore = 0);
1702 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1703 const std::string &Name, BasicBlock *InsertAtEnd);
1705 // allocate space for exactly two operands
1706 void *operator new(size_t s) {
1707 return User::operator new(s, 2);
1710 template<typename InputIterator>
1711 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1712 InputIterator IdxEnd,
1713 const std::string &Name = "",
1714 Instruction *InsertBefore = 0) {
1715 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1716 Name, InsertBefore);
1718 template<typename InputIterator>
1719 static InsertValueInst *Create(Value *Agg, Value *Val,
1720 InputIterator IdxBegin, InputIterator IdxEnd,
1721 const std::string &Name,
1722 BasicBlock *InsertAtEnd) {
1723 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1727 /// Constructors - These two creators are convenience methods because one
1728 /// index insertvalue instructions are much more common than those with
1730 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1731 const std::string &Name = "",
1732 Instruction *InsertBefore = 0) {
1733 return new InsertValueInst(Agg, Val, Idx, Name, InsertBefore);
1735 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1736 const std::string &Name,
1737 BasicBlock *InsertAtEnd) {
1738 return new InsertValueInst(Agg, Val, Idx, Name, InsertAtEnd);
1741 virtual InsertValueInst *clone() const;
1743 /// Transparently provide more efficient getOperand methods.
1744 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1746 // getType - Overload to return most specific pointer type...
1747 const PointerType *getType() const {
1748 return reinterpret_cast<const PointerType*>(Instruction::getType());
1751 typedef const unsigned* idx_iterator;
1752 inline idx_iterator idx_begin() const { return Indices.begin(); }
1753 inline idx_iterator idx_end() const { return Indices.end(); }
1755 Value *getAggregateOperand() {
1756 return getOperand(0);
1758 const Value *getAggregateOperand() const {
1759 return getOperand(0);
1761 static unsigned getAggregateOperandIndex() {
1762 return 0U; // get index for modifying correct operand
1765 Value *getInsertedValueOperand() {
1766 return getOperand(1);
1768 const Value *getInsertedValueOperand() const {
1769 return getOperand(1);
1771 static unsigned getInsertedValueOperandIndex() {
1772 return 1U; // get index for modifying correct operand
1775 unsigned getNumIndices() const { // Note: always non-negative
1776 return (unsigned)Indices.size();
1779 bool hasIndices() const {
1783 // Methods for support type inquiry through isa, cast, and dyn_cast:
1784 static inline bool classof(const InsertValueInst *) { return true; }
1785 static inline bool classof(const Instruction *I) {
1786 return I->getOpcode() == Instruction::InsertValue;
1788 static inline bool classof(const Value *V) {
1789 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1794 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1797 template<typename InputIterator>
1798 InsertValueInst::InsertValueInst(Value *Agg,
1800 InputIterator IdxBegin,
1801 InputIterator IdxEnd,
1802 const std::string &Name,
1803 Instruction *InsertBefore)
1804 : Instruction(Agg->getType(), InsertValue,
1805 OperandTraits<InsertValueInst>::op_begin(this),
1807 init(Agg, Val, IdxBegin, IdxEnd, Name,
1808 typename std::iterator_traits<InputIterator>::iterator_category());
1810 template<typename InputIterator>
1811 InsertValueInst::InsertValueInst(Value *Agg,
1813 InputIterator IdxBegin,
1814 InputIterator IdxEnd,
1815 const std::string &Name,
1816 BasicBlock *InsertAtEnd)
1817 : Instruction(Agg->getType(), InsertValue,
1818 OperandTraits<InsertValueInst>::op_begin(this),
1820 init(Agg, Val, IdxBegin, IdxEnd, Name,
1821 typename std::iterator_traits<InputIterator>::iterator_category());
1824 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1826 //===----------------------------------------------------------------------===//
1828 //===----------------------------------------------------------------------===//
1830 // PHINode - The PHINode class is used to represent the magical mystical PHI
1831 // node, that can not exist in nature, but can be synthesized in a computer
1832 // scientist's overactive imagination.
1834 class PHINode : public Instruction {
1835 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1836 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1837 /// the number actually in use.
1838 unsigned ReservedSpace;
1839 PHINode(const PHINode &PN);
1840 // allocate space for exactly zero operands
1841 void *operator new(size_t s) {
1842 return User::operator new(s, 0);
1844 explicit PHINode(const Type *Ty, const std::string &Name = "",
1845 Instruction *InsertBefore = 0)
1846 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1851 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
1852 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1857 static PHINode *Create(const Type *Ty, const std::string &Name = "",
1858 Instruction *InsertBefore = 0) {
1859 return new PHINode(Ty, Name, InsertBefore);
1861 static PHINode *Create(const Type *Ty, const std::string &Name,
1862 BasicBlock *InsertAtEnd) {
1863 return new PHINode(Ty, Name, InsertAtEnd);
1867 /// reserveOperandSpace - This method can be used to avoid repeated
1868 /// reallocation of PHI operand lists by reserving space for the correct
1869 /// number of operands before adding them. Unlike normal vector reserves,
1870 /// this method can also be used to trim the operand space.
1871 void reserveOperandSpace(unsigned NumValues) {
1872 resizeOperands(NumValues*2);
1875 virtual PHINode *clone() const;
1877 /// Provide fast operand accessors
1878 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1880 /// getNumIncomingValues - Return the number of incoming edges
1882 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1884 /// getIncomingValue - Return incoming value number x
1886 Value *getIncomingValue(unsigned i) const {
1887 assert(i*2 < getNumOperands() && "Invalid value number!");
1888 return getOperand(i*2);
1890 void setIncomingValue(unsigned i, Value *V) {
1891 assert(i*2 < getNumOperands() && "Invalid value number!");
1894 unsigned getOperandNumForIncomingValue(unsigned i) {
1898 /// getIncomingBlock - Return incoming basic block number x
1900 BasicBlock *getIncomingBlock(unsigned i) const {
1901 return static_cast<BasicBlock*>(getOperand(i*2+1));
1903 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1904 setOperand(i*2+1, BB);
1906 unsigned getOperandNumForIncomingBlock(unsigned i) {
1910 /// addIncoming - Add an incoming value to the end of the PHI list
1912 void addIncoming(Value *V, BasicBlock *BB) {
1913 assert(V && "PHI node got a null value!");
1914 assert(BB && "PHI node got a null basic block!");
1915 assert(getType() == V->getType() &&
1916 "All operands to PHI node must be the same type as the PHI node!");
1917 unsigned OpNo = NumOperands;
1918 if (OpNo+2 > ReservedSpace)
1919 resizeOperands(0); // Get more space!
1920 // Initialize some new operands.
1921 NumOperands = OpNo+2;
1922 OperandList[OpNo] = V;
1923 OperandList[OpNo+1] = BB;
1926 /// removeIncomingValue - Remove an incoming value. This is useful if a
1927 /// predecessor basic block is deleted. The value removed is returned.
1929 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1930 /// is true), the PHI node is destroyed and any uses of it are replaced with
1931 /// dummy values. The only time there should be zero incoming values to a PHI
1932 /// node is when the block is dead, so this strategy is sound.
1934 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1936 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1937 int Idx = getBasicBlockIndex(BB);
1938 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1939 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1942 /// getBasicBlockIndex - Return the first index of the specified basic
1943 /// block in the value list for this PHI. Returns -1 if no instance.
1945 int getBasicBlockIndex(const BasicBlock *BB) const {
1946 Use *OL = OperandList;
1947 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1948 if (OL[i+1].get() == BB) return i/2;
1952 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1953 return getIncomingValue(getBasicBlockIndex(BB));
1956 /// hasConstantValue - If the specified PHI node always merges together the
1957 /// same value, return the value, otherwise return null.
1959 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1961 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1962 static inline bool classof(const PHINode *) { return true; }
1963 static inline bool classof(const Instruction *I) {
1964 return I->getOpcode() == Instruction::PHI;
1966 static inline bool classof(const Value *V) {
1967 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1970 void resizeOperands(unsigned NumOperands);
1974 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1977 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1980 //===----------------------------------------------------------------------===//
1982 //===----------------------------------------------------------------------===//
1984 //===---------------------------------------------------------------------------
1985 /// ReturnInst - Return a value (possibly void), from a function. Execution
1986 /// does not continue in this function any longer.
1988 class ReturnInst : public TerminatorInst {
1989 ReturnInst(const ReturnInst &RI);
1990 void init(Value * const* retVals, unsigned N);
1993 // ReturnInst constructors:
1994 // ReturnInst() - 'ret void' instruction
1995 // ReturnInst( null) - 'ret void' instruction
1996 // ReturnInst(Value* X) - 'ret X' instruction
1997 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1998 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1999 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2000 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2001 // ReturnInst(Value* X, N) - 'ret X,X+1...X+N-1' instruction
2002 // ReturnInst(Value* X, N, Inst *I) - 'ret X,X+1...X+N-1', insert before I
2003 // ReturnInst(Value* X, N, BB *B) - 'ret X,X+1...X+N-1', insert @ end of B
2005 // NOTE: If the Value* passed is of type void then the constructor behaves as
2006 // if it was passed NULL.
2007 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2008 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2009 ReturnInst(Value * const* retVals, unsigned N, Instruction *InsertBefore = 0);
2010 ReturnInst(Value * const* retVals, unsigned N, BasicBlock *InsertAtEnd);
2011 explicit ReturnInst(BasicBlock *InsertAtEnd);
2013 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2014 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2016 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2017 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2019 static ReturnInst* Create(Value * const* retVals, unsigned N,
2020 Instruction *InsertBefore = 0) {
2021 return new(N) ReturnInst(retVals, N, InsertBefore);
2023 static ReturnInst* Create(Value * const* retVals, unsigned N,
2024 BasicBlock *InsertAtEnd) {
2025 return new(N) ReturnInst(retVals, N, InsertAtEnd);
2027 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2028 return new(0) ReturnInst(InsertAtEnd);
2030 virtual ~ReturnInst();
2031 inline void operator delete(void*);
2033 virtual ReturnInst *clone() const;
2035 /// Provide fast operand accessors
2036 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2038 /// Convenience accessor
2039 Value *getReturnValue(unsigned n = 0) const {
2040 return n < getNumOperands()
2045 unsigned getNumSuccessors() const { return 0; }
2047 // Methods for support type inquiry through isa, cast, and dyn_cast:
2048 static inline bool classof(const ReturnInst *) { return true; }
2049 static inline bool classof(const Instruction *I) {
2050 return (I->getOpcode() == Instruction::Ret);
2052 static inline bool classof(const Value *V) {
2053 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2056 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2057 virtual unsigned getNumSuccessorsV() const;
2058 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2062 struct OperandTraits<ReturnInst> : VariadicOperandTraits<> {
2065 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2066 void ReturnInst::operator delete(void *it) {
2067 ReturnInst* me(static_cast<ReturnInst*>(it));
2068 Use::zap(OperandTraits<ReturnInst>::op_begin(me),
2069 OperandTraits<ReturnInst>::op_end(me),
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 &Name,
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 &Name, 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 &Name, 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 &Name = "",
2394 Instruction *InsertBefore = 0) {
2395 unsigned Values(ArgEnd - ArgBegin + 3);
2396 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2397 Values, Name, 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 &Name, BasicBlock *InsertAtEnd) {
2404 unsigned Values(ArgEnd - ArgBegin + 3);
2405 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2406 Values, Name, InsertAtEnd);
2409 virtual InvokeInst *clone() const;
2411 /// Provide fast operand accessors
2412 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2414 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2416 unsigned getCallingConv() const { return SubclassData; }
2417 void setCallingConv(unsigned CC) {
2421 /// getParamAttrs - Return the parameter attributes for this invoke.
2423 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
2425 /// setParamAttrs - Set the parameter attributes for this invoke.
2427 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
2429 /// @brief Determine whether the call or the callee has the given attribute.
2430 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
2432 /// addParamAttr - adds the attribute to the list of attributes.
2433 void addParamAttr(unsigned i, ParameterAttributes attr);
2435 /// @brief Extract the alignment for a call or parameter (0=unknown).
2436 unsigned getParamAlignment(unsigned i) const {
2437 return ParamAttrs.getParamAlignment(i);
2440 /// @brief Determine if the call does not access memory.
2441 bool doesNotAccessMemory() const {
2442 return paramHasAttr(0, ParamAttr::ReadNone);
2445 /// @brief Determine if the call does not access or only reads memory.
2446 bool onlyReadsMemory() const {
2447 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
2450 /// @brief Determine if the call cannot return.
2451 bool doesNotReturn() const {
2452 return paramHasAttr(0, ParamAttr::NoReturn);
2455 /// @brief Determine if the call cannot unwind.
2456 bool doesNotThrow() const {
2457 return paramHasAttr(0, ParamAttr::NoUnwind);
2459 void setDoesNotThrow(bool doesNotThrow = true);
2461 /// @brief Determine if the call returns a structure through first
2462 /// pointer argument.
2463 bool hasStructRetAttr() const {
2464 // Be friendly and also check the callee.
2465 return paramHasAttr(1, ParamAttr::StructRet);
2468 /// getCalledFunction - Return the function called, or null if this is an
2469 /// indirect function invocation.
2471 Function *getCalledFunction() const {
2472 return dyn_cast<Function>(getOperand(0));
2475 // getCalledValue - Get a pointer to a function that is invoked by this inst.
2476 Value *getCalledValue() const { return getOperand(0); }
2478 // get*Dest - Return the destination basic blocks...
2479 BasicBlock *getNormalDest() const {
2480 return cast<BasicBlock>(getOperand(1));
2482 BasicBlock *getUnwindDest() const {
2483 return cast<BasicBlock>(getOperand(2));
2485 void setNormalDest(BasicBlock *B) {
2489 void setUnwindDest(BasicBlock *B) {
2493 BasicBlock *getSuccessor(unsigned i) const {
2494 assert(i < 2 && "Successor # out of range for invoke!");
2495 return i == 0 ? getNormalDest() : getUnwindDest();
2498 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2499 assert(idx < 2 && "Successor # out of range for invoke!");
2500 setOperand(idx+1, NewSucc);
2503 unsigned getNumSuccessors() const { return 2; }
2505 // Methods for support type inquiry through isa, cast, and dyn_cast:
2506 static inline bool classof(const InvokeInst *) { return true; }
2507 static inline bool classof(const Instruction *I) {
2508 return (I->getOpcode() == Instruction::Invoke);
2510 static inline bool classof(const Value *V) {
2511 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2514 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2515 virtual unsigned getNumSuccessorsV() const;
2516 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2520 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2523 template<typename InputIterator>
2524 InvokeInst::InvokeInst(Value *Func,
2525 BasicBlock *IfNormal, BasicBlock *IfException,
2526 InputIterator ArgBegin, InputIterator ArgEnd,
2528 const std::string &Name, Instruction *InsertBefore)
2529 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2530 ->getElementType())->getReturnType(),
2531 Instruction::Invoke,
2532 OperandTraits<InvokeInst>::op_end(this) - Values,
2533 Values, InsertBefore) {
2534 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2535 typename std::iterator_traits<InputIterator>::iterator_category());
2537 template<typename InputIterator>
2538 InvokeInst::InvokeInst(Value *Func,
2539 BasicBlock *IfNormal, BasicBlock *IfException,
2540 InputIterator ArgBegin, InputIterator ArgEnd,
2542 const std::string &Name, BasicBlock *InsertAtEnd)
2543 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2544 ->getElementType())->getReturnType(),
2545 Instruction::Invoke,
2546 OperandTraits<InvokeInst>::op_end(this) - Values,
2547 Values, InsertAtEnd) {
2548 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, Name,
2549 typename std::iterator_traits<InputIterator>::iterator_category());
2552 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2554 //===----------------------------------------------------------------------===//
2556 //===----------------------------------------------------------------------===//
2558 //===---------------------------------------------------------------------------
2559 /// UnwindInst - Immediately exit the current function, unwinding the stack
2560 /// until an invoke instruction is found.
2562 class UnwindInst : public TerminatorInst {
2563 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2565 // allocate space for exactly zero operands
2566 void *operator new(size_t s) {
2567 return User::operator new(s, 0);
2569 explicit UnwindInst(Instruction *InsertBefore = 0);
2570 explicit UnwindInst(BasicBlock *InsertAtEnd);
2572 virtual UnwindInst *clone() const;
2574 unsigned getNumSuccessors() const { return 0; }
2576 // Methods for support type inquiry through isa, cast, and dyn_cast:
2577 static inline bool classof(const UnwindInst *) { return true; }
2578 static inline bool classof(const Instruction *I) {
2579 return I->getOpcode() == Instruction::Unwind;
2581 static inline bool classof(const Value *V) {
2582 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2585 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2586 virtual unsigned getNumSuccessorsV() const;
2587 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2590 //===----------------------------------------------------------------------===//
2591 // UnreachableInst Class
2592 //===----------------------------------------------------------------------===//
2594 //===---------------------------------------------------------------------------
2595 /// UnreachableInst - This function has undefined behavior. In particular, the
2596 /// presence of this instruction indicates some higher level knowledge that the
2597 /// end of the block cannot be reached.
2599 class UnreachableInst : public TerminatorInst {
2600 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2602 // allocate space for exactly zero operands
2603 void *operator new(size_t s) {
2604 return User::operator new(s, 0);
2606 explicit UnreachableInst(Instruction *InsertBefore = 0);
2607 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2609 virtual UnreachableInst *clone() const;
2611 unsigned getNumSuccessors() const { return 0; }
2613 // Methods for support type inquiry through isa, cast, and dyn_cast:
2614 static inline bool classof(const UnreachableInst *) { return true; }
2615 static inline bool classof(const Instruction *I) {
2616 return I->getOpcode() == Instruction::Unreachable;
2618 static inline bool classof(const Value *V) {
2619 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2622 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2623 virtual unsigned getNumSuccessorsV() const;
2624 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2627 //===----------------------------------------------------------------------===//
2629 //===----------------------------------------------------------------------===//
2631 /// @brief This class represents a truncation of integer types.
2632 class TruncInst : public CastInst {
2633 /// Private copy constructor
2634 TruncInst(const TruncInst &CI)
2635 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2638 /// @brief Constructor with insert-before-instruction semantics
2640 Value *S, ///< The value to be truncated
2641 const Type *Ty, ///< The (smaller) type to truncate to
2642 const std::string &Name = "", ///< A name for the new instruction
2643 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2646 /// @brief Constructor with insert-at-end-of-block semantics
2648 Value *S, ///< The value to be truncated
2649 const Type *Ty, ///< The (smaller) type to truncate to
2650 const std::string &Name, ///< A name for the new instruction
2651 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2654 /// @brief Clone an identical TruncInst
2655 virtual CastInst *clone() const;
2657 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2658 static inline bool classof(const TruncInst *) { return true; }
2659 static inline bool classof(const Instruction *I) {
2660 return I->getOpcode() == Trunc;
2662 static inline bool classof(const Value *V) {
2663 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2667 //===----------------------------------------------------------------------===//
2669 //===----------------------------------------------------------------------===//
2671 /// @brief This class represents zero extension of integer types.
2672 class ZExtInst : public CastInst {
2673 /// @brief Private copy constructor
2674 ZExtInst(const ZExtInst &CI)
2675 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2678 /// @brief Constructor with insert-before-instruction semantics
2680 Value *S, ///< The value to be zero extended
2681 const Type *Ty, ///< The type to zero extend to
2682 const std::string &Name = "", ///< A name for the new instruction
2683 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2686 /// @brief Constructor with insert-at-end semantics.
2688 Value *S, ///< The value to be zero extended
2689 const Type *Ty, ///< The type to zero extend to
2690 const std::string &Name, ///< A name for the new instruction
2691 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2694 /// @brief Clone an identical ZExtInst
2695 virtual CastInst *clone() const;
2697 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2698 static inline bool classof(const ZExtInst *) { return true; }
2699 static inline bool classof(const Instruction *I) {
2700 return I->getOpcode() == ZExt;
2702 static inline bool classof(const Value *V) {
2703 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2707 //===----------------------------------------------------------------------===//
2709 //===----------------------------------------------------------------------===//
2711 /// @brief This class represents a sign extension of integer types.
2712 class SExtInst : public CastInst {
2713 /// @brief Private copy constructor
2714 SExtInst(const SExtInst &CI)
2715 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2718 /// @brief Constructor with insert-before-instruction semantics
2720 Value *S, ///< The value to be sign extended
2721 const Type *Ty, ///< The type to sign extend to
2722 const std::string &Name = "", ///< A name for the new instruction
2723 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2726 /// @brief Constructor with insert-at-end-of-block semantics
2728 Value *S, ///< The value to be sign extended
2729 const Type *Ty, ///< The type to sign extend to
2730 const std::string &Name, ///< A name for the new instruction
2731 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2734 /// @brief Clone an identical SExtInst
2735 virtual CastInst *clone() const;
2737 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2738 static inline bool classof(const SExtInst *) { return true; }
2739 static inline bool classof(const Instruction *I) {
2740 return I->getOpcode() == SExt;
2742 static inline bool classof(const Value *V) {
2743 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2747 //===----------------------------------------------------------------------===//
2748 // FPTruncInst Class
2749 //===----------------------------------------------------------------------===//
2751 /// @brief This class represents a truncation of floating point types.
2752 class FPTruncInst : public CastInst {
2753 FPTruncInst(const FPTruncInst &CI)
2754 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2757 /// @brief Constructor with insert-before-instruction semantics
2759 Value *S, ///< The value to be truncated
2760 const Type *Ty, ///< The type to truncate to
2761 const std::string &Name = "", ///< A name for the new instruction
2762 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2765 /// @brief Constructor with insert-before-instruction semantics
2767 Value *S, ///< The value to be truncated
2768 const Type *Ty, ///< The type to truncate to
2769 const std::string &Name, ///< A name for the new instruction
2770 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2773 /// @brief Clone an identical FPTruncInst
2774 virtual CastInst *clone() const;
2776 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2777 static inline bool classof(const FPTruncInst *) { return true; }
2778 static inline bool classof(const Instruction *I) {
2779 return I->getOpcode() == FPTrunc;
2781 static inline bool classof(const Value *V) {
2782 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2786 //===----------------------------------------------------------------------===//
2788 //===----------------------------------------------------------------------===//
2790 /// @brief This class represents an extension of floating point types.
2791 class FPExtInst : public CastInst {
2792 FPExtInst(const FPExtInst &CI)
2793 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2796 /// @brief Constructor with insert-before-instruction semantics
2798 Value *S, ///< The value to be extended
2799 const Type *Ty, ///< The type to extend to
2800 const std::string &Name = "", ///< A name for the new instruction
2801 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2804 /// @brief Constructor with insert-at-end-of-block semantics
2806 Value *S, ///< The value to be extended
2807 const Type *Ty, ///< The type to extend to
2808 const std::string &Name, ///< A name for the new instruction
2809 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2812 /// @brief Clone an identical FPExtInst
2813 virtual CastInst *clone() const;
2815 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2816 static inline bool classof(const FPExtInst *) { return true; }
2817 static inline bool classof(const Instruction *I) {
2818 return I->getOpcode() == FPExt;
2820 static inline bool classof(const Value *V) {
2821 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2825 //===----------------------------------------------------------------------===//
2827 //===----------------------------------------------------------------------===//
2829 /// @brief This class represents a cast unsigned integer to floating point.
2830 class UIToFPInst : public CastInst {
2831 UIToFPInst(const UIToFPInst &CI)
2832 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2835 /// @brief Constructor with insert-before-instruction semantics
2837 Value *S, ///< The value to be converted
2838 const Type *Ty, ///< The type to convert to
2839 const std::string &Name = "", ///< A name for the new instruction
2840 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2843 /// @brief Constructor with insert-at-end-of-block semantics
2845 Value *S, ///< The value to be converted
2846 const Type *Ty, ///< The type to convert to
2847 const std::string &Name, ///< A name for the new instruction
2848 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2851 /// @brief Clone an identical UIToFPInst
2852 virtual CastInst *clone() const;
2854 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2855 static inline bool classof(const UIToFPInst *) { return true; }
2856 static inline bool classof(const Instruction *I) {
2857 return I->getOpcode() == UIToFP;
2859 static inline bool classof(const Value *V) {
2860 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2864 //===----------------------------------------------------------------------===//
2866 //===----------------------------------------------------------------------===//
2868 /// @brief This class represents a cast from signed integer to floating point.
2869 class SIToFPInst : public CastInst {
2870 SIToFPInst(const SIToFPInst &CI)
2871 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2874 /// @brief Constructor with insert-before-instruction semantics
2876 Value *S, ///< The value to be converted
2877 const Type *Ty, ///< The type to convert to
2878 const std::string &Name = "", ///< A name for the new instruction
2879 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2882 /// @brief Constructor with insert-at-end-of-block semantics
2884 Value *S, ///< The value to be converted
2885 const Type *Ty, ///< The type to convert to
2886 const std::string &Name, ///< A name for the new instruction
2887 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2890 /// @brief Clone an identical SIToFPInst
2891 virtual CastInst *clone() const;
2893 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2894 static inline bool classof(const SIToFPInst *) { return true; }
2895 static inline bool classof(const Instruction *I) {
2896 return I->getOpcode() == SIToFP;
2898 static inline bool classof(const Value *V) {
2899 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2903 //===----------------------------------------------------------------------===//
2905 //===----------------------------------------------------------------------===//
2907 /// @brief This class represents a cast from floating point to unsigned integer
2908 class FPToUIInst : public CastInst {
2909 FPToUIInst(const FPToUIInst &CI)
2910 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2913 /// @brief Constructor with insert-before-instruction semantics
2915 Value *S, ///< The value to be converted
2916 const Type *Ty, ///< The type to convert to
2917 const std::string &Name = "", ///< A name for the new instruction
2918 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2921 /// @brief Constructor with insert-at-end-of-block semantics
2923 Value *S, ///< The value to be converted
2924 const Type *Ty, ///< The type to convert to
2925 const std::string &Name, ///< A name for the new instruction
2926 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2929 /// @brief Clone an identical FPToUIInst
2930 virtual CastInst *clone() const;
2932 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2933 static inline bool classof(const FPToUIInst *) { return true; }
2934 static inline bool classof(const Instruction *I) {
2935 return I->getOpcode() == FPToUI;
2937 static inline bool classof(const Value *V) {
2938 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2942 //===----------------------------------------------------------------------===//
2944 //===----------------------------------------------------------------------===//
2946 /// @brief This class represents a cast from floating point to signed integer.
2947 class FPToSIInst : public CastInst {
2948 FPToSIInst(const FPToSIInst &CI)
2949 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2952 /// @brief Constructor with insert-before-instruction semantics
2954 Value *S, ///< The value to be converted
2955 const Type *Ty, ///< The type to convert to
2956 const std::string &Name = "", ///< A name for the new instruction
2957 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2960 /// @brief Constructor with insert-at-end-of-block semantics
2962 Value *S, ///< The value to be converted
2963 const Type *Ty, ///< The type to convert to
2964 const std::string &Name, ///< A name for the new instruction
2965 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2968 /// @brief Clone an identical FPToSIInst
2969 virtual CastInst *clone() const;
2971 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2972 static inline bool classof(const FPToSIInst *) { return true; }
2973 static inline bool classof(const Instruction *I) {
2974 return I->getOpcode() == FPToSI;
2976 static inline bool classof(const Value *V) {
2977 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2981 //===----------------------------------------------------------------------===//
2982 // IntToPtrInst Class
2983 //===----------------------------------------------------------------------===//
2985 /// @brief This class represents a cast from an integer to a pointer.
2986 class IntToPtrInst : public CastInst {
2987 IntToPtrInst(const IntToPtrInst &CI)
2988 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2991 /// @brief Constructor with insert-before-instruction semantics
2993 Value *S, ///< The value to be converted
2994 const Type *Ty, ///< The type to convert to
2995 const std::string &Name = "", ///< A name for the new instruction
2996 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2999 /// @brief Constructor with insert-at-end-of-block semantics
3001 Value *S, ///< The value to be converted
3002 const Type *Ty, ///< The type to convert to
3003 const std::string &Name, ///< A name for the new instruction
3004 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3007 /// @brief Clone an identical IntToPtrInst
3008 virtual CastInst *clone() const;
3010 // Methods for support type inquiry through isa, cast, and dyn_cast:
3011 static inline bool classof(const IntToPtrInst *) { return true; }
3012 static inline bool classof(const Instruction *I) {
3013 return I->getOpcode() == IntToPtr;
3015 static inline bool classof(const Value *V) {
3016 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3020 //===----------------------------------------------------------------------===//
3021 // PtrToIntInst Class
3022 //===----------------------------------------------------------------------===//
3024 /// @brief This class represents a cast from a pointer to an integer
3025 class PtrToIntInst : public CastInst {
3026 PtrToIntInst(const PtrToIntInst &CI)
3027 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3030 /// @brief Constructor with insert-before-instruction semantics
3032 Value *S, ///< The value to be converted
3033 const Type *Ty, ///< The type to convert to
3034 const std::string &Name = "", ///< A name for the new instruction
3035 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3038 /// @brief Constructor with insert-at-end-of-block semantics
3040 Value *S, ///< The value to be converted
3041 const Type *Ty, ///< The type to convert to
3042 const std::string &Name, ///< A name for the new instruction
3043 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3046 /// @brief Clone an identical PtrToIntInst
3047 virtual CastInst *clone() const;
3049 // Methods for support type inquiry through isa, cast, and dyn_cast:
3050 static inline bool classof(const PtrToIntInst *) { return true; }
3051 static inline bool classof(const Instruction *I) {
3052 return I->getOpcode() == PtrToInt;
3054 static inline bool classof(const Value *V) {
3055 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3059 //===----------------------------------------------------------------------===//
3060 // BitCastInst Class
3061 //===----------------------------------------------------------------------===//
3063 /// @brief This class represents a no-op cast from one type to another.
3064 class BitCastInst : public CastInst {
3065 BitCastInst(const BitCastInst &CI)
3066 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3069 /// @brief Constructor with insert-before-instruction semantics
3071 Value *S, ///< The value to be casted
3072 const Type *Ty, ///< The type to casted to
3073 const std::string &Name = "", ///< A name for the new instruction
3074 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3077 /// @brief Constructor with insert-at-end-of-block semantics
3079 Value *S, ///< The value to be casted
3080 const Type *Ty, ///< The type to casted to
3081 const std::string &Name, ///< A name for the new instruction
3082 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3085 /// @brief Clone an identical BitCastInst
3086 virtual CastInst *clone() const;
3088 // Methods for support type inquiry through isa, cast, and dyn_cast:
3089 static inline bool classof(const BitCastInst *) { return true; }
3090 static inline bool classof(const Instruction *I) {
3091 return I->getOpcode() == BitCast;
3093 static inline bool classof(const Value *V) {
3094 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3098 //===----------------------------------------------------------------------===//
3099 // GetResultInst Class
3100 //===----------------------------------------------------------------------===//
3102 /// GetResultInst - This instruction extracts individual result value from
3103 /// aggregate value, where aggregate value is returned by CallInst.
3105 class GetResultInst : public UnaryInstruction {
3107 GetResultInst(const GetResultInst &GRI) :
3108 UnaryInstruction(GRI.getType(), Instruction::GetResult, GRI.getOperand(0)),
3113 GetResultInst(Value *Aggr, unsigned index,
3114 const std::string &Name = "",
3115 Instruction *InsertBefore = 0);
3117 /// isValidOperands - Return true if an getresult instruction can be
3118 /// formed with the specified operands.
3119 static bool isValidOperands(const Value *Aggr, unsigned index);
3121 virtual GetResultInst *clone() const;
3123 Value *getAggregateValue() {
3124 return getOperand(0);
3127 const Value *getAggregateValue() const {
3128 return getOperand(0);
3131 unsigned getIndex() const {
3135 // Methods for support type inquiry through isa, cast, and dyn_cast:
3136 static inline bool classof(const GetResultInst *) { return true; }
3137 static inline bool classof(const Instruction *I) {
3138 return (I->getOpcode() == Instruction::GetResult);
3140 static inline bool classof(const Value *V) {
3141 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3145 } // End llvm namespace