1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
21 #include "llvm/InstrTypes.h"
22 #include "llvm/DerivedTypes.h"
23 #include "llvm/ParameterAttributes.h"
24 #include "llvm/BasicBlock.h"
25 #include "llvm/ADT/SmallVector.h"
35 //===----------------------------------------------------------------------===//
36 // AllocationInst Class
37 //===----------------------------------------------------------------------===//
39 /// AllocationInst - This class is the common base class of MallocInst and
42 class AllocationInst : public UnaryInstruction {
44 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
45 const std::string &Name = "", Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
47 const std::string &Name, BasicBlock *InsertAtEnd);
49 // Out of line virtual method, so the vtable, etc. has a home.
50 virtual ~AllocationInst();
52 /// isArrayAllocation - Return true if there is an allocation size parameter
53 /// to the allocation instruction that is not 1.
55 bool isArrayAllocation() const;
57 /// getArraySize - Get the number of element allocated, for a simple
58 /// allocation of a single element, this will return a constant 1 value.
60 const Value *getArraySize() const { return getOperand(0); }
61 Value *getArraySize() { return getOperand(0); }
63 /// getType - Overload to return most specific pointer type
65 const PointerType *getType() const {
66 return reinterpret_cast<const PointerType*>(Instruction::getType());
69 /// getAllocatedType - Return the type that is being allocated by the
72 const Type *getAllocatedType() const;
74 /// getAlignment - Return the alignment of the memory that is being allocated
75 /// by the instruction.
77 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
78 void setAlignment(unsigned Align);
80 virtual Instruction *clone() const = 0;
82 // Methods for support type inquiry through isa, cast, and dyn_cast:
83 static inline bool classof(const AllocationInst *) { return true; }
84 static inline bool classof(const Instruction *I) {
85 return I->getOpcode() == Instruction::Alloca ||
86 I->getOpcode() == Instruction::Malloc;
88 static inline bool classof(const Value *V) {
89 return isa<Instruction>(V) && classof(cast<Instruction>(V));
94 //===----------------------------------------------------------------------===//
96 //===----------------------------------------------------------------------===//
98 /// MallocInst - an instruction to allocated memory on the heap
100 class MallocInst : public AllocationInst {
101 MallocInst(const MallocInst &MI);
103 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
104 const std::string &NameStr = "",
105 Instruction *InsertBefore = 0)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
107 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
108 BasicBlock *InsertAtEnd)
109 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
111 MallocInst(const Type *Ty, const std::string &NameStr,
112 Instruction *InsertBefore = 0)
113 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
114 MallocInst(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
118 const std::string &NameStr, BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
121 const std::string &NameStr = "",
122 Instruction *InsertBefore = 0)
123 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
125 virtual MallocInst *clone() const;
127 // Methods for support type inquiry through isa, cast, and dyn_cast:
128 static inline bool classof(const MallocInst *) { return true; }
129 static inline bool classof(const Instruction *I) {
130 return (I->getOpcode() == Instruction::Malloc);
132 static inline bool classof(const Value *V) {
133 return isa<Instruction>(V) && classof(cast<Instruction>(V));
138 //===----------------------------------------------------------------------===//
140 //===----------------------------------------------------------------------===//
142 /// AllocaInst - an instruction to allocate memory on the stack
144 class AllocaInst : public AllocationInst {
145 AllocaInst(const AllocaInst &);
147 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
148 const std::string &NameStr = "",
149 Instruction *InsertBefore = 0)
150 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
151 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
152 BasicBlock *InsertAtEnd)
153 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
155 AllocaInst(const Type *Ty, const std::string &NameStr,
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
158 AllocaInst(const Type *Ty, const std::string &NameStr,
159 BasicBlock *InsertAtEnd)
160 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
163 const std::string &NameStr = "", Instruction *InsertBefore = 0)
164 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
165 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
166 const std::string &NameStr, BasicBlock *InsertAtEnd)
167 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
169 virtual AllocaInst *clone() const;
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const AllocaInst *) { return true; }
173 static inline bool classof(const Instruction *I) {
174 return (I->getOpcode() == Instruction::Alloca);
176 static inline bool classof(const Value *V) {
177 return isa<Instruction>(V) && classof(cast<Instruction>(V));
182 //===----------------------------------------------------------------------===//
184 //===----------------------------------------------------------------------===//
186 /// FreeInst - an instruction to deallocate memory
188 class FreeInst : public UnaryInstruction {
191 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
192 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
194 virtual FreeInst *clone() const;
196 // Accessor methods for consistency with other memory operations
197 Value *getPointerOperand() { return getOperand(0); }
198 const Value *getPointerOperand() const { return getOperand(0); }
200 // Methods for support type inquiry through isa, cast, and dyn_cast:
201 static inline bool classof(const FreeInst *) { return true; }
202 static inline bool classof(const Instruction *I) {
203 return (I->getOpcode() == Instruction::Free);
205 static inline bool classof(const Value *V) {
206 return isa<Instruction>(V) && classof(cast<Instruction>(V));
211 //===----------------------------------------------------------------------===//
213 //===----------------------------------------------------------------------===//
215 /// LoadInst - an instruction for reading from memory. This uses the
216 /// SubclassData field in Value to store whether or not the load is volatile.
218 class LoadInst : public UnaryInstruction {
220 LoadInst(const LoadInst &LI)
221 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
222 setVolatile(LI.isVolatile());
223 setAlignment(LI.getAlignment());
231 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
232 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
233 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
234 Instruction *InsertBefore = 0);
235 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
236 unsigned Align, Instruction *InsertBefore = 0);
237 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
238 BasicBlock *InsertAtEnd);
239 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
240 unsigned Align, BasicBlock *InsertAtEnd);
242 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
243 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
244 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
245 bool isVolatile = false, Instruction *InsertBefore = 0);
246 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
247 BasicBlock *InsertAtEnd);
249 /// isVolatile - Return true if this is a load from a volatile memory
252 bool isVolatile() const { return SubclassData & 1; }
254 /// setVolatile - Specify whether this is a volatile load or not.
256 void setVolatile(bool V) {
257 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
260 virtual LoadInst *clone() const;
262 /// getAlignment - Return the alignment of the access that is being performed
264 unsigned getAlignment() const {
265 return (1 << (SubclassData>>1)) >> 1;
268 void setAlignment(unsigned Align);
270 Value *getPointerOperand() { return getOperand(0); }
271 const Value *getPointerOperand() const { return getOperand(0); }
272 static unsigned getPointerOperandIndex() { return 0U; }
274 // Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const LoadInst *) { return true; }
276 static inline bool classof(const Instruction *I) {
277 return I->getOpcode() == Instruction::Load;
279 static inline bool classof(const Value *V) {
280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
285 //===----------------------------------------------------------------------===//
287 //===----------------------------------------------------------------------===//
289 /// StoreInst - an instruction for storing to memory
291 class StoreInst : public Instruction {
292 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
294 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
296 Op<0>() = SI.Op<0>();
297 Op<1>() = SI.Op<1>();
298 setVolatile(SI.isVolatile());
299 setAlignment(SI.getAlignment());
307 // allocate space for exactly two operands
308 void *operator new(size_t s) {
309 return User::operator new(s, 2);
311 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
312 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
313 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
314 Instruction *InsertBefore = 0);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, Instruction *InsertBefore = 0);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
318 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
319 unsigned Align, BasicBlock *InsertAtEnd);
322 /// isVolatile - Return true if this is a load from a volatile memory
325 bool isVolatile() const { return SubclassData & 1; }
327 /// setVolatile - Specify whether this is a volatile load or not.
329 void setVolatile(bool V) {
330 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
333 /// Transparently provide more efficient getOperand methods.
334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
336 /// getAlignment - Return the alignment of the access that is being performed
338 unsigned getAlignment() const {
339 return (1 << (SubclassData>>1)) >> 1;
342 void setAlignment(unsigned Align);
344 virtual StoreInst *clone() const;
346 Value *getPointerOperand() { return getOperand(1); }
347 const Value *getPointerOperand() const { return getOperand(1); }
348 static unsigned getPointerOperandIndex() { return 1U; }
350 // Methods for support type inquiry through isa, cast, and dyn_cast:
351 static inline bool classof(const StoreInst *) { return true; }
352 static inline bool classof(const Instruction *I) {
353 return I->getOpcode() == Instruction::Store;
355 static inline bool classof(const Value *V) {
356 return isa<Instruction>(V) && classof(cast<Instruction>(V));
361 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
364 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
366 //===----------------------------------------------------------------------===//
367 // GetElementPtrInst Class
368 //===----------------------------------------------------------------------===//
370 // checkType - Simple wrapper function to give a better assertion failure
371 // message on bad indexes for a gep instruction.
373 static inline const Type *checkType(const Type *Ty) {
374 assert(Ty && "Invalid GetElementPtrInst indices for type!");
378 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
379 /// access elements of arrays and structs
381 class GetElementPtrInst : public Instruction {
382 GetElementPtrInst(const GetElementPtrInst &GEPI);
383 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
384 const std::string &NameStr);
385 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
387 template<typename InputIterator>
388 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
389 const std::string &NameStr,
390 // This argument ensures that we have an iterator we can
391 // do arithmetic on in constant time
392 std::random_access_iterator_tag) {
393 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
396 // This requires that the iterator points to contiguous memory.
397 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
398 // we have to build an array here
401 init(Ptr, 0, NumIdx, NameStr);
405 /// getIndexedType - Returns the type of the element that would be loaded with
406 /// a load instruction with the specified parameters.
408 /// Null is returned if the indices are invalid for the specified
411 static const Type *getIndexedType(const Type *Ptr,
412 Value* const *Idx, unsigned NumIdx);
414 template<typename InputIterator>
415 static const Type *getIndexedType(const Type *Ptr,
416 InputIterator IdxBegin,
417 InputIterator IdxEnd,
418 // This argument ensures that we
419 // have an iterator we can do
420 // arithmetic on in constant time
421 std::random_access_iterator_tag) {
422 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
425 // This requires that the iterator points to contiguous memory.
426 return getIndexedType(Ptr, (Value *const *)&*IdxBegin, NumIdx);
428 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
431 /// Constructors - Create a getelementptr instruction with a base pointer an
432 /// list of indices. The first ctor can optionally insert before an existing
433 /// instruction, the second appends the new instruction to the specified
435 template<typename InputIterator>
436 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
437 InputIterator IdxEnd,
439 const std::string &NameStr,
440 Instruction *InsertBefore);
441 template<typename InputIterator>
442 inline GetElementPtrInst(Value *Ptr,
443 InputIterator IdxBegin, InputIterator IdxEnd,
445 const std::string &NameStr, BasicBlock *InsertAtEnd);
447 /// Constructors - These two constructors are convenience methods because one
448 /// and two index getelementptr instructions are so common.
449 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
450 Instruction *InsertBefore = 0);
451 GetElementPtrInst(Value *Ptr, Value *Idx,
452 const std::string &NameStr, BasicBlock *InsertAtEnd);
454 template<typename InputIterator>
455 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
456 InputIterator IdxEnd,
457 const std::string &NameStr = "",
458 Instruction *InsertBefore = 0) {
459 typename std::iterator_traits<InputIterator>::difference_type Values =
460 1 + std::distance(IdxBegin, IdxEnd);
462 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
464 template<typename InputIterator>
465 static GetElementPtrInst *Create(Value *Ptr,
466 InputIterator IdxBegin, InputIterator IdxEnd,
467 const std::string &NameStr,
468 BasicBlock *InsertAtEnd) {
469 typename std::iterator_traits<InputIterator>::difference_type Values =
470 1 + std::distance(IdxBegin, IdxEnd);
472 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
475 /// Constructors - These two creators are convenience methods because one
476 /// index getelementptr instructions are so common.
477 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
478 const std::string &NameStr = "",
479 Instruction *InsertBefore = 0) {
480 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
482 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
483 const std::string &NameStr,
484 BasicBlock *InsertAtEnd) {
485 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
488 virtual GetElementPtrInst *clone() const;
490 /// Transparently provide more efficient getOperand methods.
491 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
493 // getType - Overload to return most specific pointer type...
494 const PointerType *getType() const {
495 return reinterpret_cast<const PointerType*>(Instruction::getType());
498 /// getIndexedType - Returns the type of the element that would be loaded with
499 /// a load instruction with the specified parameters.
501 /// Null is returned if the indices are invalid for the specified
504 template<typename InputIterator>
505 static const Type *getIndexedType(const Type *Ptr,
506 InputIterator IdxBegin,
507 InputIterator IdxEnd) {
508 return getIndexedType(Ptr, IdxBegin, IdxEnd,
509 typename std::iterator_traits<InputIterator>::
510 iterator_category());
512 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
514 inline op_iterator idx_begin() { return op_begin()+1; }
515 inline const_op_iterator idx_begin() const { return op_begin()+1; }
516 inline op_iterator idx_end() { return op_end(); }
517 inline const_op_iterator idx_end() const { return op_end(); }
519 Value *getPointerOperand() {
520 return getOperand(0);
522 const Value *getPointerOperand() const {
523 return getOperand(0);
525 static unsigned getPointerOperandIndex() {
526 return 0U; // get index for modifying correct operand
529 unsigned getNumIndices() const { // Note: always non-negative
530 return getNumOperands() - 1;
533 bool hasIndices() const {
534 return getNumOperands() > 1;
537 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
538 /// zeros. If so, the result pointer and the first operand have the same
539 /// value, just potentially different types.
540 bool hasAllZeroIndices() const;
542 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
543 /// constant integers. If so, the result pointer and the first operand have
544 /// a constant offset between them.
545 bool hasAllConstantIndices() const;
548 // Methods for support type inquiry through isa, cast, and dyn_cast:
549 static inline bool classof(const GetElementPtrInst *) { return true; }
550 static inline bool classof(const Instruction *I) {
551 return (I->getOpcode() == Instruction::GetElementPtr);
553 static inline bool classof(const Value *V) {
554 return isa<Instruction>(V) && classof(cast<Instruction>(V));
559 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
562 template<typename InputIterator>
563 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
564 InputIterator IdxBegin,
565 InputIterator IdxEnd,
567 const std::string &NameStr,
568 Instruction *InsertBefore)
569 : Instruction(PointerType::get(checkType(
570 getIndexedType(Ptr->getType(),
572 cast<PointerType>(Ptr->getType())
573 ->getAddressSpace()),
575 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
576 Values, InsertBefore) {
577 init(Ptr, IdxBegin, IdxEnd, NameStr,
578 typename std::iterator_traits<InputIterator>::iterator_category());
580 template<typename InputIterator>
581 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
582 InputIterator IdxBegin,
583 InputIterator IdxEnd,
585 const std::string &NameStr,
586 BasicBlock *InsertAtEnd)
587 : Instruction(PointerType::get(checkType(
588 getIndexedType(Ptr->getType(),
590 cast<PointerType>(Ptr->getType())
591 ->getAddressSpace()),
593 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
594 Values, InsertAtEnd) {
595 init(Ptr, IdxBegin, IdxEnd, NameStr,
596 typename std::iterator_traits<InputIterator>::iterator_category());
600 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
603 //===----------------------------------------------------------------------===//
605 //===----------------------------------------------------------------------===//
607 /// This instruction compares its operands according to the predicate given
608 /// to the constructor. It only operates on integers or pointers. The operands
609 /// must be identical types.
610 /// @brief Represent an integer comparison operator.
611 class ICmpInst: public CmpInst {
613 /// @brief Constructor with insert-before-instruction semantics.
615 Predicate pred, ///< The predicate to use for the comparison
616 Value *LHS, ///< The left-hand-side of the expression
617 Value *RHS, ///< The right-hand-side of the expression
618 const std::string &NameStr = "", ///< Name of the instruction
619 Instruction *InsertBefore = 0 ///< Where to insert
620 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, NameStr,
622 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
623 pred <= CmpInst::LAST_ICMP_PREDICATE &&
624 "Invalid ICmp predicate value");
625 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
626 "Both operands to ICmp instruction are not of the same type!");
627 // Check that the operands are the right type
628 assert((getOperand(0)->getType()->isInteger() ||
629 isa<PointerType>(getOperand(0)->getType())) &&
630 "Invalid operand types for ICmp instruction");
633 /// @brief Constructor with insert-at-block-end semantics.
635 Predicate pred, ///< The predicate to use for the comparison
636 Value *LHS, ///< The left-hand-side of the expression
637 Value *RHS, ///< The right-hand-side of the expression
638 const std::string &NameStr, ///< Name of the instruction
639 BasicBlock *InsertAtEnd ///< Block to insert into.
640 ) : CmpInst(Type::Int1Ty, Instruction::ICmp, pred, LHS, RHS, NameStr,
642 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
643 pred <= CmpInst::LAST_ICMP_PREDICATE &&
644 "Invalid ICmp predicate value");
645 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
646 "Both operands to ICmp instruction are not of the same type!");
647 // Check that the operands are the right type
648 assert((getOperand(0)->getType()->isInteger() ||
649 isa<PointerType>(getOperand(0)->getType())) &&
650 "Invalid operand types for ICmp instruction");
653 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
654 /// @returns the predicate that would be the result if the operand were
655 /// regarded as signed.
656 /// @brief Return the signed version of the predicate
657 Predicate getSignedPredicate() const {
658 return getSignedPredicate(getPredicate());
661 /// This is a static version that you can use without an instruction.
662 /// @brief Return the signed version of the predicate.
663 static Predicate getSignedPredicate(Predicate pred);
665 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
666 /// @returns the predicate that would be the result if the operand were
667 /// regarded as unsigned.
668 /// @brief Return the unsigned version of the predicate
669 Predicate getUnsignedPredicate() const {
670 return getUnsignedPredicate(getPredicate());
673 /// This is a static version that you can use without an instruction.
674 /// @brief Return the unsigned version of the predicate.
675 static Predicate getUnsignedPredicate(Predicate pred);
677 /// isEquality - Return true if this predicate is either EQ or NE. This also
678 /// tests for commutativity.
679 static bool isEquality(Predicate P) {
680 return P == ICMP_EQ || P == ICMP_NE;
683 /// isEquality - Return true if this predicate is either EQ or NE. This also
684 /// tests for commutativity.
685 bool isEquality() const {
686 return isEquality(getPredicate());
689 /// @returns true if the predicate of this ICmpInst is commutative
690 /// @brief Determine if this relation is commutative.
691 bool isCommutative() const { return isEquality(); }
693 /// isRelational - Return true if the predicate is relational (not EQ or NE).
695 bool isRelational() const {
696 return !isEquality();
699 /// isRelational - Return true if the predicate is relational (not EQ or NE).
701 static bool isRelational(Predicate P) {
702 return !isEquality(P);
705 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
706 /// @brief Determine if this instruction's predicate is signed.
707 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
709 /// @returns true if the predicate provided is signed, false otherwise
710 /// @brief Determine if the predicate is signed.
711 static bool isSignedPredicate(Predicate pred);
713 /// @returns true if the specified compare predicate is
714 /// true when both operands are equal...
715 /// @brief Determine if the icmp is true when both operands are equal
716 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
717 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
718 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
719 pred == ICmpInst::ICMP_SLE;
722 /// @returns true if the specified compare instruction is
723 /// true when both operands are equal...
724 /// @brief Determine if the ICmpInst returns true when both operands are equal
725 bool isTrueWhenEqual() {
726 return isTrueWhenEqual(getPredicate());
729 /// Initialize a set of values that all satisfy the predicate with C.
730 /// @brief Make a ConstantRange for a relation with a constant value.
731 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
733 /// Exchange the two operands to this instruction in such a way that it does
734 /// not modify the semantics of the instruction. The predicate value may be
735 /// changed to retain the same result if the predicate is order dependent
737 /// @brief Swap operands and adjust predicate.
738 void swapOperands() {
739 SubclassData = getSwappedPredicate();
740 Op<0>().swap(Op<1>());
743 virtual ICmpInst *clone() const;
745 // Methods for support type inquiry through isa, cast, and dyn_cast:
746 static inline bool classof(const ICmpInst *) { return true; }
747 static inline bool classof(const Instruction *I) {
748 return I->getOpcode() == Instruction::ICmp;
750 static inline bool classof(const Value *V) {
751 return isa<Instruction>(V) && classof(cast<Instruction>(V));
755 //===----------------------------------------------------------------------===//
757 //===----------------------------------------------------------------------===//
759 /// This instruction compares its operands according to the predicate given
760 /// to the constructor. It only operates on floating point values or packed
761 /// vectors of floating point values. The operands must be identical types.
762 /// @brief Represents a floating point comparison operator.
763 class FCmpInst: public CmpInst {
765 /// @brief Constructor with insert-before-instruction semantics.
767 Predicate pred, ///< The predicate to use for the comparison
768 Value *LHS, ///< The left-hand-side of the expression
769 Value *RHS, ///< The right-hand-side of the expression
770 const std::string &NameStr = "", ///< Name of the instruction
771 Instruction *InsertBefore = 0 ///< Where to insert
772 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, NameStr,
774 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
775 "Invalid FCmp predicate value");
776 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
777 "Both operands to FCmp instruction are not of the same type!");
778 // Check that the operands are the right type
779 assert(getOperand(0)->getType()->isFloatingPoint() &&
780 "Invalid operand types for FCmp instruction");
783 /// @brief Constructor with insert-at-block-end semantics.
785 Predicate pred, ///< The predicate to use for the comparison
786 Value *LHS, ///< The left-hand-side of the expression
787 Value *RHS, ///< The right-hand-side of the expression
788 const std::string &NameStr, ///< Name of the instruction
789 BasicBlock *InsertAtEnd ///< Block to insert into.
790 ) : CmpInst(Type::Int1Ty, Instruction::FCmp, pred, LHS, RHS, NameStr,
792 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
793 "Invalid FCmp predicate value");
794 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
795 "Both operands to FCmp instruction are not of the same type!");
796 // Check that the operands are the right type
797 assert(getOperand(0)->getType()->isFloatingPoint() &&
798 "Invalid operand types for FCmp instruction");
801 /// This also tests for commutativity. If isEquality() returns true then
802 /// the predicate is also commutative. Only the equality predicates are
804 /// @returns true if the predicate of this instruction is EQ or NE.
805 /// @brief Determine if this is an equality predicate.
806 bool isEquality() const {
807 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
808 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
810 bool isCommutative() const { return isEquality(); }
812 /// @returns true if the predicate is relational (not EQ or NE).
813 /// @brief Determine if this a relational predicate.
814 bool isRelational() const { return !isEquality(); }
816 /// Exchange the two operands to this instruction in such a way that it does
817 /// not modify the semantics of the instruction. The predicate value may be
818 /// changed to retain the same result if the predicate is order dependent
820 /// @brief Swap operands and adjust predicate.
821 void swapOperands() {
822 SubclassData = getSwappedPredicate();
823 Op<0>().swap(Op<1>());
826 virtual FCmpInst *clone() const;
828 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
829 static inline bool classof(const FCmpInst *) { return true; }
830 static inline bool classof(const Instruction *I) {
831 return I->getOpcode() == Instruction::FCmp;
833 static inline bool classof(const Value *V) {
834 return isa<Instruction>(V) && classof(cast<Instruction>(V));
838 //===----------------------------------------------------------------------===//
840 //===----------------------------------------------------------------------===//
842 /// This instruction compares its operands according to the predicate given
843 /// to the constructor. It only operates on vectors of integers.
844 /// The operands must be identical types.
845 /// @brief Represents a vector integer comparison operator.
846 class VICmpInst: public CmpInst {
848 /// @brief Constructor with insert-before-instruction semantics.
850 Predicate pred, ///< The predicate to use for the comparison
851 Value *LHS, ///< The left-hand-side of the expression
852 Value *RHS, ///< The right-hand-side of the expression
853 const std::string &NameStr = "", ///< Name of the instruction
854 Instruction *InsertBefore = 0 ///< Where to insert
855 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
857 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
858 pred <= CmpInst::LAST_ICMP_PREDICATE &&
859 "Invalid VICmp predicate value");
860 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
861 "Both operands to VICmp instruction are not of the same type!");
864 /// @brief Constructor with insert-at-block-end semantics.
866 Predicate pred, ///< The predicate to use for the comparison
867 Value *LHS, ///< The left-hand-side of the expression
868 Value *RHS, ///< The right-hand-side of the expression
869 const std::string &NameStr, ///< Name of the instruction
870 BasicBlock *InsertAtEnd ///< Block to insert into.
871 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
873 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
874 pred <= CmpInst::LAST_ICMP_PREDICATE &&
875 "Invalid VICmp predicate value");
876 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
877 "Both operands to VICmp instruction are not of the same type!");
880 /// @brief Return the predicate for this instruction.
881 Predicate getPredicate() const { return Predicate(SubclassData); }
883 virtual VICmpInst *clone() const;
885 // Methods for support type inquiry through isa, cast, and dyn_cast:
886 static inline bool classof(const VICmpInst *) { return true; }
887 static inline bool classof(const Instruction *I) {
888 return I->getOpcode() == Instruction::VICmp;
890 static inline bool classof(const Value *V) {
891 return isa<Instruction>(V) && classof(cast<Instruction>(V));
895 //===----------------------------------------------------------------------===//
897 //===----------------------------------------------------------------------===//
899 /// This instruction compares its operands according to the predicate given
900 /// to the constructor. It only operates on vectors of floating point values.
901 /// The operands must be identical types.
902 /// @brief Represents a vector floating point comparison operator.
903 class VFCmpInst: public CmpInst {
905 /// @brief Constructor with insert-before-instruction semantics.
907 Predicate pred, ///< The predicate to use for the comparison
908 Value *LHS, ///< The left-hand-side of the expression
909 Value *RHS, ///< The right-hand-side of the expression
910 const std::string &NameStr = "", ///< Name of the instruction
911 Instruction *InsertBefore = 0 ///< Where to insert
912 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
913 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertBefore) {
914 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
915 "Invalid VFCmp predicate value");
916 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
917 "Both operands to VFCmp instruction are not of the same type!");
920 /// @brief Constructor with insert-at-block-end semantics.
922 Predicate pred, ///< The predicate to use for the comparison
923 Value *LHS, ///< The left-hand-side of the expression
924 Value *RHS, ///< The right-hand-side of the expression
925 const std::string &NameStr, ///< Name of the instruction
926 BasicBlock *InsertAtEnd ///< Block to insert into.
927 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
928 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertAtEnd) {
929 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
930 "Invalid VFCmp predicate value");
931 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
932 "Both operands to VFCmp instruction are not of the same type!");
935 /// @brief Return the predicate for this instruction.
936 Predicate getPredicate() const { return Predicate(SubclassData); }
938 virtual VFCmpInst *clone() const;
940 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
941 static inline bool classof(const VFCmpInst *) { return true; }
942 static inline bool classof(const Instruction *I) {
943 return I->getOpcode() == Instruction::VFCmp;
945 static inline bool classof(const Value *V) {
946 return isa<Instruction>(V) && classof(cast<Instruction>(V));
950 //===----------------------------------------------------------------------===//
952 //===----------------------------------------------------------------------===//
953 /// CallInst - This class represents a function call, abstracting a target
954 /// machine's calling convention. This class uses low bit of the SubClassData
955 /// field to indicate whether or not this is a tail call. The rest of the bits
956 /// hold the calling convention of the call.
959 class CallInst : public Instruction {
960 PAListPtr ParamAttrs; ///< parameter attributes for call
961 CallInst(const CallInst &CI);
962 void init(Value *Func, Value* const *Params, unsigned NumParams);
963 void init(Value *Func, Value *Actual1, Value *Actual2);
964 void init(Value *Func, Value *Actual);
965 void init(Value *Func);
967 template<typename InputIterator>
968 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
969 const std::string &NameStr,
970 // This argument ensures that we have an iterator we can
971 // do arithmetic on in constant time
972 std::random_access_iterator_tag) {
973 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
975 // This requires that the iterator points to contiguous memory.
976 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
980 /// Construct a CallInst given a range of arguments. InputIterator
981 /// must be a random-access iterator pointing to contiguous storage
982 /// (e.g. a std::vector<>::iterator). Checks are made for
983 /// random-accessness but not for contiguous storage as that would
984 /// incur runtime overhead.
985 /// @brief Construct a CallInst from a range of arguments
986 template<typename InputIterator>
987 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
988 const std::string &NameStr, Instruction *InsertBefore);
990 /// Construct a CallInst given a range of arguments. InputIterator
991 /// must be a random-access iterator pointing to contiguous storage
992 /// (e.g. a std::vector<>::iterator). Checks are made for
993 /// random-accessness but not for contiguous storage as that would
994 /// incur runtime overhead.
995 /// @brief Construct a CallInst from a range of arguments
996 template<typename InputIterator>
997 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
998 const std::string &NameStr, BasicBlock *InsertAtEnd);
1000 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1001 Instruction *InsertBefore);
1002 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1003 BasicBlock *InsertAtEnd);
1004 explicit CallInst(Value *F, const std::string &NameStr,
1005 Instruction *InsertBefore);
1006 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
1008 template<typename InputIterator>
1009 static CallInst *Create(Value *Func,
1010 InputIterator ArgBegin, InputIterator ArgEnd,
1011 const std::string &NameStr = "",
1012 Instruction *InsertBefore = 0) {
1013 return new((unsigned)(ArgEnd - ArgBegin + 1))
1014 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1016 template<typename InputIterator>
1017 static CallInst *Create(Value *Func,
1018 InputIterator ArgBegin, InputIterator ArgEnd,
1019 const std::string &NameStr, BasicBlock *InsertAtEnd) {
1020 return new((unsigned)(ArgEnd - ArgBegin + 1))
1021 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1023 static CallInst *Create(Value *F, Value *Actual,
1024 const std::string& NameStr = "",
1025 Instruction *InsertBefore = 0) {
1026 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1028 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
1029 BasicBlock *InsertAtEnd) {
1030 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1032 static CallInst *Create(Value *F, const std::string &NameStr = "",
1033 Instruction *InsertBefore = 0) {
1034 return new(1) CallInst(F, NameStr, InsertBefore);
1036 static CallInst *Create(Value *F, const std::string &NameStr,
1037 BasicBlock *InsertAtEnd) {
1038 return new(1) CallInst(F, NameStr, InsertAtEnd);
1043 virtual CallInst *clone() const;
1045 /// Provide fast operand accessors
1046 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1048 bool isTailCall() const { return SubclassData & 1; }
1049 void setTailCall(bool isTC = true) {
1050 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1053 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1055 unsigned getCallingConv() const { return SubclassData >> 1; }
1056 void setCallingConv(unsigned CC) {
1057 SubclassData = (SubclassData & 1) | (CC << 1);
1060 /// getParamAttrs - Return the parameter attributes for this call.
1062 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
1064 /// setParamAttrs - Sets the parameter attributes for this call.
1065 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
1067 /// addParamAttr - adds the attribute to the list of attributes.
1068 void addParamAttr(unsigned i, ParameterAttributes attr);
1070 /// removeParamAttr - removes the attribute from the list of attributes.
1071 void removeParamAttr(unsigned i, ParameterAttributes attr);
1073 /// @brief Determine whether the call or the callee has the given attribute.
1074 bool paramHasAttr(unsigned i, unsigned attr) const;
1076 /// @brief Extract the alignment for a call or parameter (0=unknown).
1077 unsigned getParamAlignment(unsigned i) const {
1078 return ParamAttrs.getParamAlignment(i);
1081 /// @brief Determine if the call does not access memory.
1082 bool doesNotAccessMemory() const {
1083 return paramHasAttr(0, ParamAttr::ReadNone);
1085 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1086 if (NotAccessMemory) addParamAttr(0, ParamAttr::ReadNone);
1087 else removeParamAttr(0, ParamAttr::ReadNone);
1090 /// @brief Determine if the call does not access or only reads memory.
1091 bool onlyReadsMemory() const {
1092 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
1094 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1095 if (OnlyReadsMemory) addParamAttr(0, ParamAttr::ReadOnly);
1096 else removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
1099 /// @brief Determine if the call cannot return.
1100 bool doesNotReturn() const {
1101 return paramHasAttr(0, ParamAttr::NoReturn);
1103 void setDoesNotReturn(bool DoesNotReturn = true) {
1104 if (DoesNotReturn) addParamAttr(0, ParamAttr::NoReturn);
1105 else removeParamAttr(0, ParamAttr::NoReturn);
1108 /// @brief Determine if the call cannot unwind.
1109 bool doesNotThrow() const {
1110 return paramHasAttr(0, ParamAttr::NoUnwind);
1112 void setDoesNotThrow(bool DoesNotThrow = true) {
1113 if (DoesNotThrow) addParamAttr(0, ParamAttr::NoUnwind);
1114 else removeParamAttr(0, ParamAttr::NoUnwind);
1117 /// @brief Determine if the call returns a structure through first
1118 /// pointer argument.
1119 bool hasStructRetAttr() const {
1120 // Be friendly and also check the callee.
1121 return paramHasAttr(1, ParamAttr::StructRet);
1124 /// @brief Determine if any call argument is an aggregate passed by value.
1125 bool hasByValArgument() const {
1126 return ParamAttrs.hasAttrSomewhere(ParamAttr::ByVal);
1129 /// getCalledFunction - Return the function being called by this instruction
1130 /// if it is a direct call. If it is a call through a function pointer,
1132 Function *getCalledFunction() const {
1133 return dyn_cast<Function>(getOperand(0));
1136 /// getCalledValue - Get a pointer to the function that is invoked by this
1138 const Value *getCalledValue() const { return getOperand(0); }
1139 Value *getCalledValue() { return getOperand(0); }
1141 // Methods for support type inquiry through isa, cast, and dyn_cast:
1142 static inline bool classof(const CallInst *) { return true; }
1143 static inline bool classof(const Instruction *I) {
1144 return I->getOpcode() == Instruction::Call;
1146 static inline bool classof(const Value *V) {
1147 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1152 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1155 template<typename InputIterator>
1156 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1157 const std::string &NameStr, BasicBlock *InsertAtEnd)
1158 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1159 ->getElementType())->getReturnType(),
1161 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1162 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1163 init(Func, ArgBegin, ArgEnd, NameStr,
1164 typename std::iterator_traits<InputIterator>::iterator_category());
1167 template<typename InputIterator>
1168 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1169 const std::string &NameStr, Instruction *InsertBefore)
1170 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1171 ->getElementType())->getReturnType(),
1173 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1174 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1175 init(Func, ArgBegin, ArgEnd, NameStr,
1176 typename std::iterator_traits<InputIterator>::iterator_category());
1179 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1181 //===----------------------------------------------------------------------===//
1183 //===----------------------------------------------------------------------===//
1185 /// SelectInst - This class represents the LLVM 'select' instruction.
1187 class SelectInst : public Instruction {
1188 void init(Value *C, Value *S1, Value *S2) {
1194 SelectInst(const SelectInst &SI)
1195 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1196 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1198 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1199 Instruction *InsertBefore)
1200 : Instruction(S1->getType(), Instruction::Select,
1201 &Op<0>(), 3, InsertBefore) {
1205 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1206 BasicBlock *InsertAtEnd)
1207 : Instruction(S1->getType(), Instruction::Select,
1208 &Op<0>(), 3, InsertAtEnd) {
1213 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1214 const std::string &NameStr = "",
1215 Instruction *InsertBefore = 0) {
1216 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1218 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1219 const std::string &NameStr,
1220 BasicBlock *InsertAtEnd) {
1221 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1224 Value *getCondition() const { return Op<0>(); }
1225 Value *getTrueValue() const { return Op<1>(); }
1226 Value *getFalseValue() const { return Op<2>(); }
1228 /// Transparently provide more efficient getOperand methods.
1229 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1231 OtherOps getOpcode() const {
1232 return static_cast<OtherOps>(Instruction::getOpcode());
1235 virtual SelectInst *clone() const;
1237 // Methods for support type inquiry through isa, cast, and dyn_cast:
1238 static inline bool classof(const SelectInst *) { return true; }
1239 static inline bool classof(const Instruction *I) {
1240 return I->getOpcode() == Instruction::Select;
1242 static inline bool classof(const Value *V) {
1243 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1248 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1251 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1253 //===----------------------------------------------------------------------===//
1255 //===----------------------------------------------------------------------===//
1257 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1258 /// an argument of the specified type given a va_list and increments that list
1260 class VAArgInst : public UnaryInstruction {
1261 VAArgInst(const VAArgInst &VAA)
1262 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1264 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1265 Instruction *InsertBefore = 0)
1266 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1269 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1270 BasicBlock *InsertAtEnd)
1271 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1275 virtual VAArgInst *clone() const;
1277 // Methods for support type inquiry through isa, cast, and dyn_cast:
1278 static inline bool classof(const VAArgInst *) { return true; }
1279 static inline bool classof(const Instruction *I) {
1280 return I->getOpcode() == VAArg;
1282 static inline bool classof(const Value *V) {
1283 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1287 //===----------------------------------------------------------------------===//
1288 // ExtractElementInst Class
1289 //===----------------------------------------------------------------------===//
1291 /// ExtractElementInst - This instruction extracts a single (scalar)
1292 /// element from a VectorType value
1294 class ExtractElementInst : public Instruction {
1295 ExtractElementInst(const ExtractElementInst &EE) :
1296 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1297 Op<0>() = EE.Op<0>();
1298 Op<1>() = EE.Op<1>();
1302 // allocate space for exactly two operands
1303 void *operator new(size_t s) {
1304 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1306 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1307 Instruction *InsertBefore = 0);
1308 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
1309 Instruction *InsertBefore = 0);
1310 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1311 BasicBlock *InsertAtEnd);
1312 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
1313 BasicBlock *InsertAtEnd);
1315 /// isValidOperands - Return true if an extractelement instruction can be
1316 /// formed with the specified operands.
1317 static bool isValidOperands(const Value *Vec, const Value *Idx);
1319 virtual ExtractElementInst *clone() const;
1321 /// Transparently provide more efficient getOperand methods.
1322 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1324 // Methods for support type inquiry through isa, cast, and dyn_cast:
1325 static inline bool classof(const ExtractElementInst *) { return true; }
1326 static inline bool classof(const Instruction *I) {
1327 return I->getOpcode() == Instruction::ExtractElement;
1329 static inline bool classof(const Value *V) {
1330 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1335 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1338 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1340 //===----------------------------------------------------------------------===//
1341 // InsertElementInst Class
1342 //===----------------------------------------------------------------------===//
1344 /// InsertElementInst - This instruction inserts a single (scalar)
1345 /// element into a VectorType value
1347 class InsertElementInst : public Instruction {
1348 InsertElementInst(const InsertElementInst &IE);
1349 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1350 const std::string &NameStr = "",Instruction *InsertBefore = 0);
1351 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1352 const std::string &NameStr = "",Instruction *InsertBefore = 0);
1353 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1354 const std::string &NameStr, BasicBlock *InsertAtEnd);
1355 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1356 const std::string &NameStr, BasicBlock *InsertAtEnd);
1358 static InsertElementInst *Create(const InsertElementInst &IE) {
1359 return new(IE.getNumOperands()) InsertElementInst(IE);
1361 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1362 const std::string &NameStr = "",
1363 Instruction *InsertBefore = 0) {
1364 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1366 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1367 const std::string &NameStr = "",
1368 Instruction *InsertBefore = 0) {
1369 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1371 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1372 const std::string &NameStr,
1373 BasicBlock *InsertAtEnd) {
1374 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1376 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1377 const std::string &NameStr,
1378 BasicBlock *InsertAtEnd) {
1379 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1382 /// isValidOperands - Return true if an insertelement instruction can be
1383 /// formed with the specified operands.
1384 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1387 virtual InsertElementInst *clone() const;
1389 /// getType - Overload to return most specific vector type.
1391 const VectorType *getType() const {
1392 return reinterpret_cast<const VectorType*>(Instruction::getType());
1395 /// Transparently provide more efficient getOperand methods.
1396 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1398 // Methods for support type inquiry through isa, cast, and dyn_cast:
1399 static inline bool classof(const InsertElementInst *) { return true; }
1400 static inline bool classof(const Instruction *I) {
1401 return I->getOpcode() == Instruction::InsertElement;
1403 static inline bool classof(const Value *V) {
1404 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1409 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1412 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1414 //===----------------------------------------------------------------------===//
1415 // ShuffleVectorInst Class
1416 //===----------------------------------------------------------------------===//
1418 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1421 class ShuffleVectorInst : public Instruction {
1422 ShuffleVectorInst(const ShuffleVectorInst &IE);
1424 // allocate space for exactly three operands
1425 void *operator new(size_t s) {
1426 return User::operator new(s, 3);
1428 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1429 const std::string &NameStr = "",
1430 Instruction *InsertBefor = 0);
1431 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1432 const std::string &NameStr, BasicBlock *InsertAtEnd);
1434 /// isValidOperands - Return true if a shufflevector instruction can be
1435 /// formed with the specified operands.
1436 static bool isValidOperands(const Value *V1, const Value *V2,
1439 virtual ShuffleVectorInst *clone() const;
1441 /// getType - Overload to return most specific vector type.
1443 const VectorType *getType() const {
1444 return reinterpret_cast<const VectorType*>(Instruction::getType());
1447 /// Transparently provide more efficient getOperand methods.
1448 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1450 /// getMaskValue - Return the index from the shuffle mask for the specified
1451 /// output result. This is either -1 if the element is undef or a number less
1452 /// than 2*numelements.
1453 int getMaskValue(unsigned i) const;
1455 // Methods for support type inquiry through isa, cast, and dyn_cast:
1456 static inline bool classof(const ShuffleVectorInst *) { return true; }
1457 static inline bool classof(const Instruction *I) {
1458 return I->getOpcode() == Instruction::ShuffleVector;
1460 static inline bool classof(const Value *V) {
1461 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1466 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1469 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1471 //===----------------------------------------------------------------------===//
1472 // ExtractValueInst Class
1473 //===----------------------------------------------------------------------===//
1475 /// ExtractValueInst - This instruction extracts a struct member or array
1476 /// element value from an aggregate value.
1478 class ExtractValueInst : public UnaryInstruction {
1479 SmallVector<unsigned, 4> Indices;
1481 ExtractValueInst(const ExtractValueInst &EVI);
1482 void init(const unsigned *Idx, unsigned NumIdx,
1483 const std::string &NameStr);
1484 void init(unsigned Idx, const std::string &NameStr);
1486 template<typename InputIterator>
1487 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1488 const std::string &NameStr,
1489 // This argument ensures that we have an iterator we can
1490 // do arithmetic on in constant time
1491 std::random_access_iterator_tag) {
1492 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1494 // There's no fundamental reason why we require at least one index
1495 // (other than weirdness with &*IdxBegin being invalid; see
1496 // getelementptr's init routine for example). But there's no
1497 // present need to support it.
1498 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1500 // This requires that the iterator points to contiguous memory.
1501 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1502 // we have to build an array here
1505 /// getIndexedType - Returns the type of the element that would be extracted
1506 /// with an extractvalue instruction with the specified parameters.
1508 /// Null is returned if the indices are invalid for the specified
1511 static const Type *getIndexedType(const Type *Agg,
1512 const unsigned *Idx, unsigned NumIdx);
1514 template<typename InputIterator>
1515 static const Type *getIndexedType(const Type *Ptr,
1516 InputIterator IdxBegin,
1517 InputIterator IdxEnd,
1518 // This argument ensures that we
1519 // have an iterator we can do
1520 // arithmetic on in constant time
1521 std::random_access_iterator_tag) {
1522 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1525 // This requires that the iterator points to contiguous memory.
1526 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1528 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1531 /// Constructors - Create a extractvalue instruction with a base aggregate
1532 /// value and a list of indices. The first ctor can optionally insert before
1533 /// an existing instruction, the second appends the new instruction to the
1534 /// specified BasicBlock.
1535 template<typename InputIterator>
1536 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1537 InputIterator IdxEnd,
1538 const std::string &NameStr,
1539 Instruction *InsertBefore);
1540 template<typename InputIterator>
1541 inline ExtractValueInst(Value *Agg,
1542 InputIterator IdxBegin, InputIterator IdxEnd,
1543 const std::string &NameStr, BasicBlock *InsertAtEnd);
1545 // allocate space for exactly one operand
1546 void *operator new(size_t s) {
1547 return User::operator new(s, 1);
1551 template<typename InputIterator>
1552 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1553 InputIterator IdxEnd,
1554 const std::string &NameStr = "",
1555 Instruction *InsertBefore = 0) {
1557 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1559 template<typename InputIterator>
1560 static ExtractValueInst *Create(Value *Agg,
1561 InputIterator IdxBegin, InputIterator IdxEnd,
1562 const std::string &NameStr,
1563 BasicBlock *InsertAtEnd) {
1564 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1567 /// Constructors - These two creators are convenience methods because one
1568 /// index extractvalue instructions are much more common than those with
1570 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1571 const std::string &NameStr = "",
1572 Instruction *InsertBefore = 0) {
1573 unsigned Idxs[1] = { Idx };
1574 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1576 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1577 const std::string &NameStr,
1578 BasicBlock *InsertAtEnd) {
1579 unsigned Idxs[1] = { Idx };
1580 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1583 virtual ExtractValueInst *clone() const;
1585 // getType - Overload to return most specific pointer type...
1586 const PointerType *getType() const {
1587 return reinterpret_cast<const PointerType*>(Instruction::getType());
1590 /// getIndexedType - Returns the type of the element that would be extracted
1591 /// with an extractvalue instruction with the specified parameters.
1593 /// Null is returned if the indices are invalid for the specified
1596 template<typename InputIterator>
1597 static const Type *getIndexedType(const Type *Ptr,
1598 InputIterator IdxBegin,
1599 InputIterator IdxEnd) {
1600 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1601 typename std::iterator_traits<InputIterator>::
1602 iterator_category());
1604 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1606 typedef const unsigned* idx_iterator;
1607 inline idx_iterator idx_begin() const { return Indices.begin(); }
1608 inline idx_iterator idx_end() const { return Indices.end(); }
1610 Value *getAggregateOperand() {
1611 return getOperand(0);
1613 const Value *getAggregateOperand() const {
1614 return getOperand(0);
1616 static unsigned getAggregateOperandIndex() {
1617 return 0U; // get index for modifying correct operand
1620 unsigned getNumIndices() const { // Note: always non-negative
1621 return (unsigned)Indices.size();
1624 bool hasIndices() const {
1628 // Methods for support type inquiry through isa, cast, and dyn_cast:
1629 static inline bool classof(const ExtractValueInst *) { return true; }
1630 static inline bool classof(const Instruction *I) {
1631 return I->getOpcode() == Instruction::ExtractValue;
1633 static inline bool classof(const Value *V) {
1634 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1638 template<typename InputIterator>
1639 ExtractValueInst::ExtractValueInst(Value *Agg,
1640 InputIterator IdxBegin,
1641 InputIterator IdxEnd,
1642 const std::string &NameStr,
1643 Instruction *InsertBefore)
1644 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1646 ExtractValue, Agg, InsertBefore) {
1647 init(IdxBegin, IdxEnd, NameStr,
1648 typename std::iterator_traits<InputIterator>::iterator_category());
1650 template<typename InputIterator>
1651 ExtractValueInst::ExtractValueInst(Value *Agg,
1652 InputIterator IdxBegin,
1653 InputIterator IdxEnd,
1654 const std::string &NameStr,
1655 BasicBlock *InsertAtEnd)
1656 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1658 ExtractValue, Agg, InsertAtEnd) {
1659 init(IdxBegin, IdxEnd, NameStr,
1660 typename std::iterator_traits<InputIterator>::iterator_category());
1664 //===----------------------------------------------------------------------===//
1665 // InsertValueInst Class
1666 //===----------------------------------------------------------------------===//
1668 /// InsertValueInst - This instruction inserts a struct field of array element
1669 /// value into an aggregate value.
1671 class InsertValueInst : public Instruction {
1672 SmallVector<unsigned, 4> Indices;
1674 void *operator new(size_t, unsigned); // Do not implement
1675 InsertValueInst(const InsertValueInst &IVI);
1676 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1677 const std::string &NameStr);
1678 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1680 template<typename InputIterator>
1681 void init(Value *Agg, Value *Val,
1682 InputIterator IdxBegin, InputIterator IdxEnd,
1683 const std::string &NameStr,
1684 // This argument ensures that we have an iterator we can
1685 // do arithmetic on in constant time
1686 std::random_access_iterator_tag) {
1687 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1689 // There's no fundamental reason why we require at least one index
1690 // (other than weirdness with &*IdxBegin being invalid; see
1691 // getelementptr's init routine for example). But there's no
1692 // present need to support it.
1693 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1695 // This requires that the iterator points to contiguous memory.
1696 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1697 // we have to build an array here
1700 /// Constructors - Create a insertvalue instruction with a base aggregate
1701 /// value, a value to insert, and a list of indices. The first ctor can
1702 /// optionally insert before an existing instruction, the second appends
1703 /// the new instruction to the specified BasicBlock.
1704 template<typename InputIterator>
1705 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1706 InputIterator IdxEnd,
1707 const std::string &NameStr,
1708 Instruction *InsertBefore);
1709 template<typename InputIterator>
1710 inline InsertValueInst(Value *Agg, Value *Val,
1711 InputIterator IdxBegin, InputIterator IdxEnd,
1712 const std::string &NameStr, BasicBlock *InsertAtEnd);
1714 /// Constructors - These two constructors are convenience methods because one
1715 /// and two index insertvalue instructions are so common.
1716 InsertValueInst(Value *Agg, Value *Val,
1717 unsigned Idx, const std::string &NameStr = "",
1718 Instruction *InsertBefore = 0);
1719 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1720 const std::string &NameStr, BasicBlock *InsertAtEnd);
1722 // allocate space for exactly two operands
1723 void *operator new(size_t s) {
1724 return User::operator new(s, 2);
1727 template<typename InputIterator>
1728 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1729 InputIterator IdxEnd,
1730 const std::string &NameStr = "",
1731 Instruction *InsertBefore = 0) {
1732 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1733 NameStr, InsertBefore);
1735 template<typename InputIterator>
1736 static InsertValueInst *Create(Value *Agg, Value *Val,
1737 InputIterator IdxBegin, InputIterator IdxEnd,
1738 const std::string &NameStr,
1739 BasicBlock *InsertAtEnd) {
1740 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1741 NameStr, InsertAtEnd);
1744 /// Constructors - These two creators are convenience methods because one
1745 /// index insertvalue instructions are much more common than those with
1747 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1748 const std::string &NameStr = "",
1749 Instruction *InsertBefore = 0) {
1750 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1752 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1753 const std::string &NameStr,
1754 BasicBlock *InsertAtEnd) {
1755 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1758 virtual InsertValueInst *clone() const;
1760 /// Transparently provide more efficient getOperand methods.
1761 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1763 // getType - Overload to return most specific pointer type...
1764 const PointerType *getType() const {
1765 return reinterpret_cast<const PointerType*>(Instruction::getType());
1768 typedef const unsigned* idx_iterator;
1769 inline idx_iterator idx_begin() const { return Indices.begin(); }
1770 inline idx_iterator idx_end() const { return Indices.end(); }
1772 Value *getAggregateOperand() {
1773 return getOperand(0);
1775 const Value *getAggregateOperand() const {
1776 return getOperand(0);
1778 static unsigned getAggregateOperandIndex() {
1779 return 0U; // get index for modifying correct operand
1782 Value *getInsertedValueOperand() {
1783 return getOperand(1);
1785 const Value *getInsertedValueOperand() const {
1786 return getOperand(1);
1788 static unsigned getInsertedValueOperandIndex() {
1789 return 1U; // get index for modifying correct operand
1792 unsigned getNumIndices() const { // Note: always non-negative
1793 return (unsigned)Indices.size();
1796 bool hasIndices() const {
1800 // Methods for support type inquiry through isa, cast, and dyn_cast:
1801 static inline bool classof(const InsertValueInst *) { return true; }
1802 static inline bool classof(const Instruction *I) {
1803 return I->getOpcode() == Instruction::InsertValue;
1805 static inline bool classof(const Value *V) {
1806 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1811 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1814 template<typename InputIterator>
1815 InsertValueInst::InsertValueInst(Value *Agg,
1817 InputIterator IdxBegin,
1818 InputIterator IdxEnd,
1819 const std::string &NameStr,
1820 Instruction *InsertBefore)
1821 : Instruction(Agg->getType(), InsertValue,
1822 OperandTraits<InsertValueInst>::op_begin(this),
1824 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1825 typename std::iterator_traits<InputIterator>::iterator_category());
1827 template<typename InputIterator>
1828 InsertValueInst::InsertValueInst(Value *Agg,
1830 InputIterator IdxBegin,
1831 InputIterator IdxEnd,
1832 const std::string &NameStr,
1833 BasicBlock *InsertAtEnd)
1834 : Instruction(Agg->getType(), InsertValue,
1835 OperandTraits<InsertValueInst>::op_begin(this),
1837 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1838 typename std::iterator_traits<InputIterator>::iterator_category());
1841 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1843 //===----------------------------------------------------------------------===//
1845 //===----------------------------------------------------------------------===//
1847 // PHINode - The PHINode class is used to represent the magical mystical PHI
1848 // node, that can not exist in nature, but can be synthesized in a computer
1849 // scientist's overactive imagination.
1851 class PHINode : public Instruction {
1852 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1853 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1854 /// the number actually in use.
1855 unsigned ReservedSpace;
1856 PHINode(const PHINode &PN);
1857 // allocate space for exactly zero operands
1858 void *operator new(size_t s) {
1859 return User::operator new(s, 0);
1861 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1862 Instruction *InsertBefore = 0)
1863 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1868 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1869 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1874 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1875 Instruction *InsertBefore = 0) {
1876 return new PHINode(Ty, NameStr, InsertBefore);
1878 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1879 BasicBlock *InsertAtEnd) {
1880 return new PHINode(Ty, NameStr, InsertAtEnd);
1884 /// reserveOperandSpace - This method can be used to avoid repeated
1885 /// reallocation of PHI operand lists by reserving space for the correct
1886 /// number of operands before adding them. Unlike normal vector reserves,
1887 /// this method can also be used to trim the operand space.
1888 void reserveOperandSpace(unsigned NumValues) {
1889 resizeOperands(NumValues*2);
1892 virtual PHINode *clone() const;
1894 /// Provide fast operand accessors
1895 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1897 /// getNumIncomingValues - Return the number of incoming edges
1899 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1901 /// getIncomingValue - Return incoming value number x
1903 Value *getIncomingValue(unsigned i) const {
1904 assert(i*2 < getNumOperands() && "Invalid value number!");
1905 return getOperand(i*2);
1907 void setIncomingValue(unsigned i, Value *V) {
1908 assert(i*2 < getNumOperands() && "Invalid value number!");
1911 unsigned getOperandNumForIncomingValue(unsigned i) {
1915 /// getIncomingBlock - Return incoming basic block number x
1917 BasicBlock *getIncomingBlock(unsigned i) const {
1918 return static_cast<BasicBlock*>(getOperand(i*2+1));
1920 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1921 setOperand(i*2+1, BB);
1923 unsigned getOperandNumForIncomingBlock(unsigned i) {
1927 /// addIncoming - Add an incoming value to the end of the PHI list
1929 void addIncoming(Value *V, BasicBlock *BB) {
1930 assert(V && "PHI node got a null value!");
1931 assert(BB && "PHI node got a null basic block!");
1932 assert(getType() == V->getType() &&
1933 "All operands to PHI node must be the same type as the PHI node!");
1934 unsigned OpNo = NumOperands;
1935 if (OpNo+2 > ReservedSpace)
1936 resizeOperands(0); // Get more space!
1937 // Initialize some new operands.
1938 NumOperands = OpNo+2;
1939 OperandList[OpNo] = V;
1940 OperandList[OpNo+1] = BB;
1943 /// removeIncomingValue - Remove an incoming value. This is useful if a
1944 /// predecessor basic block is deleted. The value removed is returned.
1946 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1947 /// is true), the PHI node is destroyed and any uses of it are replaced with
1948 /// dummy values. The only time there should be zero incoming values to a PHI
1949 /// node is when the block is dead, so this strategy is sound.
1951 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1953 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1954 int Idx = getBasicBlockIndex(BB);
1955 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1956 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1959 /// getBasicBlockIndex - Return the first index of the specified basic
1960 /// block in the value list for this PHI. Returns -1 if no instance.
1962 int getBasicBlockIndex(const BasicBlock *BB) const {
1963 Use *OL = OperandList;
1964 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1965 if (OL[i+1].get() == BB) return i/2;
1969 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1970 return getIncomingValue(getBasicBlockIndex(BB));
1973 /// hasConstantValue - If the specified PHI node always merges together the
1974 /// same value, return the value, otherwise return null.
1976 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1978 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1979 static inline bool classof(const PHINode *) { return true; }
1980 static inline bool classof(const Instruction *I) {
1981 return I->getOpcode() == Instruction::PHI;
1983 static inline bool classof(const Value *V) {
1984 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1987 void resizeOperands(unsigned NumOperands);
1991 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1994 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1997 //===----------------------------------------------------------------------===//
1999 //===----------------------------------------------------------------------===//
2001 //===---------------------------------------------------------------------------
2002 /// ReturnInst - Return a value (possibly void), from a function. Execution
2003 /// does not continue in this function any longer.
2005 class ReturnInst : public TerminatorInst {
2006 ReturnInst(const ReturnInst &RI);
2009 // ReturnInst constructors:
2010 // ReturnInst() - 'ret void' instruction
2011 // ReturnInst( null) - 'ret void' instruction
2012 // ReturnInst(Value* X) - 'ret X' instruction
2013 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2014 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2015 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2016 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2018 // NOTE: If the Value* passed is of type void then the constructor behaves as
2019 // if it was passed NULL.
2020 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2021 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2022 explicit ReturnInst(BasicBlock *InsertAtEnd);
2024 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2025 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2027 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2028 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2030 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2031 return new(0) ReturnInst(InsertAtEnd);
2033 virtual ~ReturnInst();
2035 virtual ReturnInst *clone() const;
2037 /// Provide fast operand accessors
2038 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2040 /// Convenience accessor
2041 Value *getReturnValue(unsigned n = 0) const {
2042 return n < getNumOperands()
2047 unsigned getNumSuccessors() const { return 0; }
2049 // Methods for support type inquiry through isa, cast, and dyn_cast:
2050 static inline bool classof(const ReturnInst *) { return true; }
2051 static inline bool classof(const Instruction *I) {
2052 return (I->getOpcode() == Instruction::Ret);
2054 static inline bool classof(const Value *V) {
2055 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2058 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2059 virtual unsigned getNumSuccessorsV() const;
2060 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2064 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2067 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2069 //===----------------------------------------------------------------------===//
2071 //===----------------------------------------------------------------------===//
2073 //===---------------------------------------------------------------------------
2074 /// BranchInst - Conditional or Unconditional Branch instruction.
2076 class BranchInst : public TerminatorInst {
2077 /// Ops list - Branches are strange. The operands are ordered:
2078 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2079 /// they don't have to check for cond/uncond branchness.
2080 BranchInst(const BranchInst &BI);
2082 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2083 // BranchInst(BB *B) - 'br B'
2084 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2085 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2086 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2087 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2088 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2089 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2090 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2091 Instruction *InsertBefore = 0);
2092 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2093 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2094 BasicBlock *InsertAtEnd);
2096 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2097 return new(1) BranchInst(IfTrue, InsertBefore);
2099 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2100 Value *Cond, Instruction *InsertBefore = 0) {
2101 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2103 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2104 return new(1) BranchInst(IfTrue, InsertAtEnd);
2106 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2107 Value *Cond, BasicBlock *InsertAtEnd) {
2108 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2112 if (NumOperands == 1)
2113 NumOperands = (unsigned)((Use*)this - OperandList);
2116 /// Transparently provide more efficient getOperand methods.
2117 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2119 virtual BranchInst *clone() const;
2121 bool isUnconditional() const { return getNumOperands() == 1; }
2122 bool isConditional() const { return getNumOperands() == 3; }
2124 Value *getCondition() const {
2125 assert(isConditional() && "Cannot get condition of an uncond branch!");
2126 return getOperand(2);
2129 void setCondition(Value *V) {
2130 assert(isConditional() && "Cannot set condition of unconditional branch!");
2134 // setUnconditionalDest - Change the current branch to an unconditional branch
2135 // targeting the specified block.
2136 // FIXME: Eliminate this ugly method.
2137 void setUnconditionalDest(BasicBlock *Dest) {
2139 if (isConditional()) { // Convert this to an uncond branch.
2146 unsigned getNumSuccessors() const { return 1+isConditional(); }
2148 BasicBlock *getSuccessor(unsigned i) const {
2149 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2150 return cast<BasicBlock>(getOperand(i));
2153 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2154 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2155 setOperand(idx, NewSucc);
2158 // Methods for support type inquiry through isa, cast, and dyn_cast:
2159 static inline bool classof(const BranchInst *) { return true; }
2160 static inline bool classof(const Instruction *I) {
2161 return (I->getOpcode() == Instruction::Br);
2163 static inline bool classof(const Value *V) {
2164 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2167 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2168 virtual unsigned getNumSuccessorsV() const;
2169 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2173 struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2174 // we need to access operands via OperandList, since
2175 // the NumOperands may change from 3 to 1
2176 static inline void *allocate(unsigned); // FIXME
2179 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2181 //===----------------------------------------------------------------------===//
2183 //===----------------------------------------------------------------------===//
2185 //===---------------------------------------------------------------------------
2186 /// SwitchInst - Multiway switch
2188 class SwitchInst : public TerminatorInst {
2189 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2190 unsigned ReservedSpace;
2191 // Operand[0] = Value to switch on
2192 // Operand[1] = Default basic block destination
2193 // Operand[2n ] = Value to match
2194 // Operand[2n+1] = BasicBlock to go to on match
2195 SwitchInst(const SwitchInst &RI);
2196 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2197 void resizeOperands(unsigned No);
2198 // allocate space for exactly zero operands
2199 void *operator new(size_t s) {
2200 return User::operator new(s, 0);
2202 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2203 /// switch on and a default destination. The number of additional cases can
2204 /// be specified here to make memory allocation more efficient. This
2205 /// constructor can also autoinsert before another instruction.
2206 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2207 Instruction *InsertBefore = 0);
2209 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2210 /// switch on and a default destination. The number of additional cases can
2211 /// be specified here to make memory allocation more efficient. This
2212 /// constructor also autoinserts at the end of the specified BasicBlock.
2213 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2214 BasicBlock *InsertAtEnd);
2216 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2217 unsigned NumCases, Instruction *InsertBefore = 0) {
2218 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2220 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2221 unsigned NumCases, BasicBlock *InsertAtEnd) {
2222 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2226 /// Provide fast operand accessors
2227 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2229 // Accessor Methods for Switch stmt
2230 Value *getCondition() const { return getOperand(0); }
2231 void setCondition(Value *V) { setOperand(0, V); }
2233 BasicBlock *getDefaultDest() const {
2234 return cast<BasicBlock>(getOperand(1));
2237 /// getNumCases - return the number of 'cases' in this switch instruction.
2238 /// Note that case #0 is always the default case.
2239 unsigned getNumCases() const {
2240 return getNumOperands()/2;
2243 /// getCaseValue - Return the specified case value. Note that case #0, the
2244 /// default destination, does not have a case value.
2245 ConstantInt *getCaseValue(unsigned i) {
2246 assert(i && i < getNumCases() && "Illegal case value to get!");
2247 return getSuccessorValue(i);
2250 /// getCaseValue - Return the specified case value. Note that case #0, the
2251 /// default destination, does not have a case value.
2252 const ConstantInt *getCaseValue(unsigned i) const {
2253 assert(i && i < getNumCases() && "Illegal case value to get!");
2254 return getSuccessorValue(i);
2257 /// findCaseValue - Search all of the case values for the specified constant.
2258 /// If it is explicitly handled, return the case number of it, otherwise
2259 /// return 0 to indicate that it is handled by the default handler.
2260 unsigned findCaseValue(const ConstantInt *C) const {
2261 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2262 if (getCaseValue(i) == C)
2267 /// findCaseDest - Finds the unique case value for a given successor. Returns
2268 /// null if the successor is not found, not unique, or is the default case.
2269 ConstantInt *findCaseDest(BasicBlock *BB) {
2270 if (BB == getDefaultDest()) return NULL;
2272 ConstantInt *CI = NULL;
2273 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2274 if (getSuccessor(i) == BB) {
2275 if (CI) return NULL; // Multiple cases lead to BB.
2276 else CI = getCaseValue(i);
2282 /// addCase - Add an entry to the switch instruction...
2284 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2286 /// removeCase - This method removes the specified successor from the switch
2287 /// instruction. Note that this cannot be used to remove the default
2288 /// destination (successor #0).
2290 void removeCase(unsigned idx);
2292 virtual SwitchInst *clone() const;
2294 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2295 BasicBlock *getSuccessor(unsigned idx) const {
2296 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2297 return cast<BasicBlock>(getOperand(idx*2+1));
2299 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2300 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2301 setOperand(idx*2+1, NewSucc);
2304 // getSuccessorValue - Return the value associated with the specified
2306 ConstantInt *getSuccessorValue(unsigned idx) const {
2307 assert(idx < getNumSuccessors() && "Successor # out of range!");
2308 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2311 // Methods for support type inquiry through isa, cast, and dyn_cast:
2312 static inline bool classof(const SwitchInst *) { return true; }
2313 static inline bool classof(const Instruction *I) {
2314 return I->getOpcode() == Instruction::Switch;
2316 static inline bool classof(const Value *V) {
2317 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2320 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2321 virtual unsigned getNumSuccessorsV() const;
2322 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2326 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2329 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2332 //===----------------------------------------------------------------------===//
2334 //===----------------------------------------------------------------------===//
2336 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2337 /// calling convention of the call.
2339 class InvokeInst : public TerminatorInst {
2340 PAListPtr ParamAttrs;
2341 InvokeInst(const InvokeInst &BI);
2342 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2343 Value* const *Args, unsigned NumArgs);
2345 template<typename InputIterator>
2346 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2347 InputIterator ArgBegin, InputIterator ArgEnd,
2348 const std::string &NameStr,
2349 // This argument ensures that we have an iterator we can
2350 // do arithmetic on in constant time
2351 std::random_access_iterator_tag) {
2352 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2354 // This requires that the iterator points to contiguous memory.
2355 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2359 /// Construct an InvokeInst given a range of arguments.
2360 /// InputIterator must be a random-access iterator pointing to
2361 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2362 /// made for random-accessness but not for contiguous storage as
2363 /// that would incur runtime overhead.
2365 /// @brief Construct an InvokeInst from a range of arguments
2366 template<typename InputIterator>
2367 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2368 InputIterator ArgBegin, InputIterator ArgEnd,
2370 const std::string &NameStr, Instruction *InsertBefore);
2372 /// Construct an InvokeInst given a range of arguments.
2373 /// InputIterator must be a random-access iterator pointing to
2374 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2375 /// made for random-accessness but not for contiguous storage as
2376 /// that would incur runtime overhead.
2378 /// @brief Construct an InvokeInst from a range of arguments
2379 template<typename InputIterator>
2380 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2381 InputIterator ArgBegin, InputIterator ArgEnd,
2383 const std::string &NameStr, BasicBlock *InsertAtEnd);
2385 template<typename InputIterator>
2386 static InvokeInst *Create(Value *Func,
2387 BasicBlock *IfNormal, BasicBlock *IfException,
2388 InputIterator ArgBegin, InputIterator ArgEnd,
2389 const std::string &NameStr = "",
2390 Instruction *InsertBefore = 0) {
2391 unsigned Values(ArgEnd - ArgBegin + 3);
2392 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2393 Values, NameStr, InsertBefore);
2395 template<typename InputIterator>
2396 static InvokeInst *Create(Value *Func,
2397 BasicBlock *IfNormal, BasicBlock *IfException,
2398 InputIterator ArgBegin, InputIterator ArgEnd,
2399 const std::string &NameStr,
2400 BasicBlock *InsertAtEnd) {
2401 unsigned Values(ArgEnd - ArgBegin + 3);
2402 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2403 Values, NameStr, InsertAtEnd);
2406 virtual InvokeInst *clone() const;
2408 /// Provide fast operand accessors
2409 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2411 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2413 unsigned getCallingConv() const { return SubclassData; }
2414 void setCallingConv(unsigned CC) {
2418 /// getParamAttrs - Return the parameter attributes for this invoke.
2420 const PAListPtr &getParamAttrs() const { return ParamAttrs; }
2422 /// setParamAttrs - Set the parameter attributes for this invoke.
2424 void setParamAttrs(const PAListPtr &Attrs) { ParamAttrs = Attrs; }
2426 /// @brief Determine whether the call or the callee has the given attribute.
2427 bool paramHasAttr(unsigned i, ParameterAttributes attr) const;
2429 /// addParamAttr - adds the attribute to the list of attributes.
2430 void addParamAttr(unsigned i, ParameterAttributes attr);
2432 /// removeParamAttr - removes the attribute from the list of attributes.
2433 void removeParamAttr(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);
2444 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2445 if (NotAccessMemory) addParamAttr(0, ParamAttr::ReadNone);
2446 else removeParamAttr(0, ParamAttr::ReadNone);
2449 /// @brief Determine if the call does not access or only reads memory.
2450 bool onlyReadsMemory() const {
2451 return doesNotAccessMemory() || paramHasAttr(0, ParamAttr::ReadOnly);
2453 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2454 if (OnlyReadsMemory) addParamAttr(0, ParamAttr::ReadOnly);
2455 else removeParamAttr(0, ParamAttr::ReadOnly | ParamAttr::ReadNone);
2458 /// @brief Determine if the call cannot return.
2459 bool doesNotReturn() const {
2460 return paramHasAttr(0, ParamAttr::NoReturn);
2462 void setDoesNotReturn(bool DoesNotReturn = true) {
2463 if (DoesNotReturn) addParamAttr(0, ParamAttr::NoReturn);
2464 else removeParamAttr(0, ParamAttr::NoReturn);
2467 /// @brief Determine if the call cannot unwind.
2468 bool doesNotThrow() const {
2469 return paramHasAttr(0, ParamAttr::NoUnwind);
2471 void setDoesNotThrow(bool DoesNotThrow = true) {
2472 if (DoesNotThrow) addParamAttr(0, ParamAttr::NoUnwind);
2473 else removeParamAttr(0, ParamAttr::NoUnwind);
2476 /// @brief Determine if the call returns a structure through first
2477 /// pointer argument.
2478 bool hasStructRetAttr() const {
2479 // Be friendly and also check the callee.
2480 return paramHasAttr(1, ParamAttr::StructRet);
2483 /// getCalledFunction - Return the function called, or null if this is an
2484 /// indirect function invocation.
2486 Function *getCalledFunction() const {
2487 return dyn_cast<Function>(getOperand(0));
2490 // getCalledValue - Get a pointer to a function that is invoked by this inst.
2491 Value *getCalledValue() const { return getOperand(0); }
2493 // get*Dest - Return the destination basic blocks...
2494 BasicBlock *getNormalDest() const {
2495 return cast<BasicBlock>(getOperand(1));
2497 BasicBlock *getUnwindDest() const {
2498 return cast<BasicBlock>(getOperand(2));
2500 void setNormalDest(BasicBlock *B) {
2504 void setUnwindDest(BasicBlock *B) {
2508 BasicBlock *getSuccessor(unsigned i) const {
2509 assert(i < 2 && "Successor # out of range for invoke!");
2510 return i == 0 ? getNormalDest() : getUnwindDest();
2513 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2514 assert(idx < 2 && "Successor # out of range for invoke!");
2515 setOperand(idx+1, NewSucc);
2518 unsigned getNumSuccessors() const { return 2; }
2520 // Methods for support type inquiry through isa, cast, and dyn_cast:
2521 static inline bool classof(const InvokeInst *) { return true; }
2522 static inline bool classof(const Instruction *I) {
2523 return (I->getOpcode() == Instruction::Invoke);
2525 static inline bool classof(const Value *V) {
2526 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2529 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2530 virtual unsigned getNumSuccessorsV() const;
2531 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2535 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2538 template<typename InputIterator>
2539 InvokeInst::InvokeInst(Value *Func,
2540 BasicBlock *IfNormal, BasicBlock *IfException,
2541 InputIterator ArgBegin, InputIterator ArgEnd,
2543 const std::string &NameStr, Instruction *InsertBefore)
2544 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2545 ->getElementType())->getReturnType(),
2546 Instruction::Invoke,
2547 OperandTraits<InvokeInst>::op_end(this) - Values,
2548 Values, InsertBefore) {
2549 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2550 typename std::iterator_traits<InputIterator>::iterator_category());
2552 template<typename InputIterator>
2553 InvokeInst::InvokeInst(Value *Func,
2554 BasicBlock *IfNormal, BasicBlock *IfException,
2555 InputIterator ArgBegin, InputIterator ArgEnd,
2557 const std::string &NameStr, BasicBlock *InsertAtEnd)
2558 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2559 ->getElementType())->getReturnType(),
2560 Instruction::Invoke,
2561 OperandTraits<InvokeInst>::op_end(this) - Values,
2562 Values, InsertAtEnd) {
2563 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2564 typename std::iterator_traits<InputIterator>::iterator_category());
2567 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2569 //===----------------------------------------------------------------------===//
2571 //===----------------------------------------------------------------------===//
2573 //===---------------------------------------------------------------------------
2574 /// UnwindInst - Immediately exit the current function, unwinding the stack
2575 /// until an invoke instruction is found.
2577 class UnwindInst : public TerminatorInst {
2578 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2580 // allocate space for exactly zero operands
2581 void *operator new(size_t s) {
2582 return User::operator new(s, 0);
2584 explicit UnwindInst(Instruction *InsertBefore = 0);
2585 explicit UnwindInst(BasicBlock *InsertAtEnd);
2587 virtual UnwindInst *clone() const;
2589 unsigned getNumSuccessors() const { return 0; }
2591 // Methods for support type inquiry through isa, cast, and dyn_cast:
2592 static inline bool classof(const UnwindInst *) { return true; }
2593 static inline bool classof(const Instruction *I) {
2594 return I->getOpcode() == Instruction::Unwind;
2596 static inline bool classof(const Value *V) {
2597 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2600 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2601 virtual unsigned getNumSuccessorsV() const;
2602 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2605 //===----------------------------------------------------------------------===//
2606 // UnreachableInst Class
2607 //===----------------------------------------------------------------------===//
2609 //===---------------------------------------------------------------------------
2610 /// UnreachableInst - This function has undefined behavior. In particular, the
2611 /// presence of this instruction indicates some higher level knowledge that the
2612 /// end of the block cannot be reached.
2614 class UnreachableInst : public TerminatorInst {
2615 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2617 // allocate space for exactly zero operands
2618 void *operator new(size_t s) {
2619 return User::operator new(s, 0);
2621 explicit UnreachableInst(Instruction *InsertBefore = 0);
2622 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2624 virtual UnreachableInst *clone() const;
2626 unsigned getNumSuccessors() const { return 0; }
2628 // Methods for support type inquiry through isa, cast, and dyn_cast:
2629 static inline bool classof(const UnreachableInst *) { return true; }
2630 static inline bool classof(const Instruction *I) {
2631 return I->getOpcode() == Instruction::Unreachable;
2633 static inline bool classof(const Value *V) {
2634 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2637 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2638 virtual unsigned getNumSuccessorsV() const;
2639 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2642 //===----------------------------------------------------------------------===//
2644 //===----------------------------------------------------------------------===//
2646 /// @brief This class represents a truncation of integer types.
2647 class TruncInst : public CastInst {
2648 /// Private copy constructor
2649 TruncInst(const TruncInst &CI)
2650 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2653 /// @brief Constructor with insert-before-instruction semantics
2655 Value *S, ///< The value to be truncated
2656 const Type *Ty, ///< The (smaller) type to truncate to
2657 const std::string &NameStr = "", ///< A name for the new instruction
2658 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2661 /// @brief Constructor with insert-at-end-of-block semantics
2663 Value *S, ///< The value to be truncated
2664 const Type *Ty, ///< The (smaller) type to truncate to
2665 const std::string &NameStr, ///< A name for the new instruction
2666 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2669 /// @brief Clone an identical TruncInst
2670 virtual CastInst *clone() const;
2672 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2673 static inline bool classof(const TruncInst *) { return true; }
2674 static inline bool classof(const Instruction *I) {
2675 return I->getOpcode() == Trunc;
2677 static inline bool classof(const Value *V) {
2678 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2682 //===----------------------------------------------------------------------===//
2684 //===----------------------------------------------------------------------===//
2686 /// @brief This class represents zero extension of integer types.
2687 class ZExtInst : public CastInst {
2688 /// @brief Private copy constructor
2689 ZExtInst(const ZExtInst &CI)
2690 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2693 /// @brief Constructor with insert-before-instruction semantics
2695 Value *S, ///< The value to be zero extended
2696 const Type *Ty, ///< The type to zero extend to
2697 const std::string &NameStr = "", ///< A name for the new instruction
2698 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2701 /// @brief Constructor with insert-at-end semantics.
2703 Value *S, ///< The value to be zero extended
2704 const Type *Ty, ///< The type to zero extend to
2705 const std::string &NameStr, ///< A name for the new instruction
2706 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2709 /// @brief Clone an identical ZExtInst
2710 virtual CastInst *clone() const;
2712 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2713 static inline bool classof(const ZExtInst *) { return true; }
2714 static inline bool classof(const Instruction *I) {
2715 return I->getOpcode() == ZExt;
2717 static inline bool classof(const Value *V) {
2718 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2722 //===----------------------------------------------------------------------===//
2724 //===----------------------------------------------------------------------===//
2726 /// @brief This class represents a sign extension of integer types.
2727 class SExtInst : public CastInst {
2728 /// @brief Private copy constructor
2729 SExtInst(const SExtInst &CI)
2730 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2733 /// @brief Constructor with insert-before-instruction semantics
2735 Value *S, ///< The value to be sign extended
2736 const Type *Ty, ///< The type to sign extend to
2737 const std::string &NameStr = "", ///< A name for the new instruction
2738 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2741 /// @brief Constructor with insert-at-end-of-block semantics
2743 Value *S, ///< The value to be sign extended
2744 const Type *Ty, ///< The type to sign extend to
2745 const std::string &NameStr, ///< A name for the new instruction
2746 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2749 /// @brief Clone an identical SExtInst
2750 virtual CastInst *clone() const;
2752 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2753 static inline bool classof(const SExtInst *) { return true; }
2754 static inline bool classof(const Instruction *I) {
2755 return I->getOpcode() == SExt;
2757 static inline bool classof(const Value *V) {
2758 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2762 //===----------------------------------------------------------------------===//
2763 // FPTruncInst Class
2764 //===----------------------------------------------------------------------===//
2766 /// @brief This class represents a truncation of floating point types.
2767 class FPTruncInst : public CastInst {
2768 FPTruncInst(const FPTruncInst &CI)
2769 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2772 /// @brief Constructor with insert-before-instruction semantics
2774 Value *S, ///< The value to be truncated
2775 const Type *Ty, ///< The type to truncate to
2776 const std::string &NameStr = "", ///< A name for the new instruction
2777 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2780 /// @brief Constructor with insert-before-instruction semantics
2782 Value *S, ///< The value to be truncated
2783 const Type *Ty, ///< The type to truncate to
2784 const std::string &NameStr, ///< A name for the new instruction
2785 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2788 /// @brief Clone an identical FPTruncInst
2789 virtual CastInst *clone() const;
2791 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2792 static inline bool classof(const FPTruncInst *) { return true; }
2793 static inline bool classof(const Instruction *I) {
2794 return I->getOpcode() == FPTrunc;
2796 static inline bool classof(const Value *V) {
2797 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2801 //===----------------------------------------------------------------------===//
2803 //===----------------------------------------------------------------------===//
2805 /// @brief This class represents an extension of floating point types.
2806 class FPExtInst : public CastInst {
2807 FPExtInst(const FPExtInst &CI)
2808 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2811 /// @brief Constructor with insert-before-instruction semantics
2813 Value *S, ///< The value to be extended
2814 const Type *Ty, ///< The type to extend to
2815 const std::string &NameStr = "", ///< A name for the new instruction
2816 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2819 /// @brief Constructor with insert-at-end-of-block semantics
2821 Value *S, ///< The value to be extended
2822 const Type *Ty, ///< The type to extend to
2823 const std::string &NameStr, ///< A name for the new instruction
2824 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2827 /// @brief Clone an identical FPExtInst
2828 virtual CastInst *clone() const;
2830 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2831 static inline bool classof(const FPExtInst *) { return true; }
2832 static inline bool classof(const Instruction *I) {
2833 return I->getOpcode() == FPExt;
2835 static inline bool classof(const Value *V) {
2836 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2840 //===----------------------------------------------------------------------===//
2842 //===----------------------------------------------------------------------===//
2844 /// @brief This class represents a cast unsigned integer to floating point.
2845 class UIToFPInst : public CastInst {
2846 UIToFPInst(const UIToFPInst &CI)
2847 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2850 /// @brief Constructor with insert-before-instruction semantics
2852 Value *S, ///< The value to be converted
2853 const Type *Ty, ///< The type to convert to
2854 const std::string &NameStr = "", ///< A name for the new instruction
2855 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2858 /// @brief Constructor with insert-at-end-of-block semantics
2860 Value *S, ///< The value to be converted
2861 const Type *Ty, ///< The type to convert to
2862 const std::string &NameStr, ///< A name for the new instruction
2863 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2866 /// @brief Clone an identical UIToFPInst
2867 virtual CastInst *clone() const;
2869 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2870 static inline bool classof(const UIToFPInst *) { return true; }
2871 static inline bool classof(const Instruction *I) {
2872 return I->getOpcode() == UIToFP;
2874 static inline bool classof(const Value *V) {
2875 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2879 //===----------------------------------------------------------------------===//
2881 //===----------------------------------------------------------------------===//
2883 /// @brief This class represents a cast from signed integer to floating point.
2884 class SIToFPInst : public CastInst {
2885 SIToFPInst(const SIToFPInst &CI)
2886 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2889 /// @brief Constructor with insert-before-instruction semantics
2891 Value *S, ///< The value to be converted
2892 const Type *Ty, ///< The type to convert to
2893 const std::string &NameStr = "", ///< A name for the new instruction
2894 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2897 /// @brief Constructor with insert-at-end-of-block semantics
2899 Value *S, ///< The value to be converted
2900 const Type *Ty, ///< The type to convert to
2901 const std::string &NameStr, ///< A name for the new instruction
2902 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2905 /// @brief Clone an identical SIToFPInst
2906 virtual CastInst *clone() const;
2908 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2909 static inline bool classof(const SIToFPInst *) { return true; }
2910 static inline bool classof(const Instruction *I) {
2911 return I->getOpcode() == SIToFP;
2913 static inline bool classof(const Value *V) {
2914 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2918 //===----------------------------------------------------------------------===//
2920 //===----------------------------------------------------------------------===//
2922 /// @brief This class represents a cast from floating point to unsigned integer
2923 class FPToUIInst : public CastInst {
2924 FPToUIInst(const FPToUIInst &CI)
2925 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2928 /// @brief Constructor with insert-before-instruction semantics
2930 Value *S, ///< The value to be converted
2931 const Type *Ty, ///< The type to convert to
2932 const std::string &NameStr = "", ///< A name for the new instruction
2933 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2936 /// @brief Constructor with insert-at-end-of-block semantics
2938 Value *S, ///< The value to be converted
2939 const Type *Ty, ///< The type to convert to
2940 const std::string &NameStr, ///< A name for the new instruction
2941 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2944 /// @brief Clone an identical FPToUIInst
2945 virtual CastInst *clone() const;
2947 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2948 static inline bool classof(const FPToUIInst *) { return true; }
2949 static inline bool classof(const Instruction *I) {
2950 return I->getOpcode() == FPToUI;
2952 static inline bool classof(const Value *V) {
2953 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2957 //===----------------------------------------------------------------------===//
2959 //===----------------------------------------------------------------------===//
2961 /// @brief This class represents a cast from floating point to signed integer.
2962 class FPToSIInst : public CastInst {
2963 FPToSIInst(const FPToSIInst &CI)
2964 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2967 /// @brief Constructor with insert-before-instruction semantics
2969 Value *S, ///< The value to be converted
2970 const Type *Ty, ///< The type to convert to
2971 const std::string &NameStr = "", ///< A name for the new instruction
2972 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2975 /// @brief Constructor with insert-at-end-of-block semantics
2977 Value *S, ///< The value to be converted
2978 const Type *Ty, ///< The type to convert to
2979 const std::string &NameStr, ///< A name for the new instruction
2980 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2983 /// @brief Clone an identical FPToSIInst
2984 virtual CastInst *clone() const;
2986 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2987 static inline bool classof(const FPToSIInst *) { return true; }
2988 static inline bool classof(const Instruction *I) {
2989 return I->getOpcode() == FPToSI;
2991 static inline bool classof(const Value *V) {
2992 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2996 //===----------------------------------------------------------------------===//
2997 // IntToPtrInst Class
2998 //===----------------------------------------------------------------------===//
3000 /// @brief This class represents a cast from an integer to a pointer.
3001 class IntToPtrInst : public CastInst {
3002 IntToPtrInst(const IntToPtrInst &CI)
3003 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3006 /// @brief Constructor with insert-before-instruction semantics
3008 Value *S, ///< The value to be converted
3009 const Type *Ty, ///< The type to convert to
3010 const std::string &NameStr = "", ///< A name for the new instruction
3011 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3014 /// @brief Constructor with insert-at-end-of-block semantics
3016 Value *S, ///< The value to be converted
3017 const Type *Ty, ///< The type to convert to
3018 const std::string &NameStr, ///< A name for the new instruction
3019 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3022 /// @brief Clone an identical IntToPtrInst
3023 virtual CastInst *clone() const;
3025 // Methods for support type inquiry through isa, cast, and dyn_cast:
3026 static inline bool classof(const IntToPtrInst *) { return true; }
3027 static inline bool classof(const Instruction *I) {
3028 return I->getOpcode() == IntToPtr;
3030 static inline bool classof(const Value *V) {
3031 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3035 //===----------------------------------------------------------------------===//
3036 // PtrToIntInst Class
3037 //===----------------------------------------------------------------------===//
3039 /// @brief This class represents a cast from a pointer to an integer
3040 class PtrToIntInst : public CastInst {
3041 PtrToIntInst(const PtrToIntInst &CI)
3042 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3045 /// @brief Constructor with insert-before-instruction semantics
3047 Value *S, ///< The value to be converted
3048 const Type *Ty, ///< The type to convert to
3049 const std::string &NameStr = "", ///< A name for the new instruction
3050 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3053 /// @brief Constructor with insert-at-end-of-block semantics
3055 Value *S, ///< The value to be converted
3056 const Type *Ty, ///< The type to convert to
3057 const std::string &NameStr, ///< A name for the new instruction
3058 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3061 /// @brief Clone an identical PtrToIntInst
3062 virtual CastInst *clone() const;
3064 // Methods for support type inquiry through isa, cast, and dyn_cast:
3065 static inline bool classof(const PtrToIntInst *) { return true; }
3066 static inline bool classof(const Instruction *I) {
3067 return I->getOpcode() == PtrToInt;
3069 static inline bool classof(const Value *V) {
3070 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3074 //===----------------------------------------------------------------------===//
3075 // BitCastInst Class
3076 //===----------------------------------------------------------------------===//
3078 /// @brief This class represents a no-op cast from one type to another.
3079 class BitCastInst : public CastInst {
3080 BitCastInst(const BitCastInst &CI)
3081 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3084 /// @brief Constructor with insert-before-instruction semantics
3086 Value *S, ///< The value to be casted
3087 const Type *Ty, ///< The type to casted to
3088 const std::string &NameStr = "", ///< A name for the new instruction
3089 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3092 /// @brief Constructor with insert-at-end-of-block semantics
3094 Value *S, ///< The value to be casted
3095 const Type *Ty, ///< The type to casted to
3096 const std::string &NameStr, ///< A name for the new instruction
3097 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3100 /// @brief Clone an identical BitCastInst
3101 virtual CastInst *clone() const;
3103 // Methods for support type inquiry through isa, cast, and dyn_cast:
3104 static inline bool classof(const BitCastInst *) { return true; }
3105 static inline bool classof(const Instruction *I) {
3106 return I->getOpcode() == BitCast;
3108 static inline bool classof(const Value *V) {
3109 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3113 } // End llvm namespace