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
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/ADT/SmallVector.h"
32 //===----------------------------------------------------------------------===//
33 // AllocationInst Class
34 //===----------------------------------------------------------------------===//
36 /// AllocationInst - This class is the common base class of MallocInst and
39 class AllocationInst : public UnaryInstruction {
41 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
42 const std::string &Name = "", Instruction *InsertBefore = 0);
43 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
44 const std::string &Name, BasicBlock *InsertAtEnd);
46 // Out of line virtual method, so the vtable, etc. has a home.
47 virtual ~AllocationInst();
49 /// isArrayAllocation - Return true if there is an allocation size parameter
50 /// to the allocation instruction that is not 1.
52 bool isArrayAllocation() const;
54 /// getArraySize - Get the number of elements allocated. For a simple
55 /// allocation of a single element, this will return a constant 1 value.
57 const Value *getArraySize() const { return getOperand(0); }
58 Value *getArraySize() { return getOperand(0); }
60 /// getType - Overload to return most specific pointer type
62 const PointerType *getType() const {
63 return reinterpret_cast<const PointerType*>(Instruction::getType());
66 /// getAllocatedType - Return the type that is being allocated by the
69 const Type *getAllocatedType() const;
71 /// getAlignment - Return the alignment of the memory that is being allocated
72 /// by the instruction.
74 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
75 void setAlignment(unsigned Align);
77 virtual Instruction *clone() const = 0;
79 // Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const AllocationInst *) { return true; }
81 static inline bool classof(const Instruction *I) {
82 return I->getOpcode() == Instruction::Alloca ||
83 I->getOpcode() == Instruction::Malloc;
85 static inline bool classof(const Value *V) {
86 return isa<Instruction>(V) && classof(cast<Instruction>(V));
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
95 /// MallocInst - an instruction to allocated memory on the heap
97 class MallocInst : public AllocationInst {
98 MallocInst(const MallocInst &MI);
100 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
101 const std::string &NameStr = "",
102 Instruction *InsertBefore = 0)
103 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
104 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
105 BasicBlock *InsertAtEnd)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
108 MallocInst(const Type *Ty, const std::string &NameStr,
109 Instruction *InsertBefore = 0)
110 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
111 MallocInst(const Type *Ty, const std::string &NameStr,
112 BasicBlock *InsertAtEnd)
113 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
115 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
116 const std::string &NameStr, BasicBlock *InsertAtEnd)
117 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
118 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
119 const std::string &NameStr = "",
120 Instruction *InsertBefore = 0)
121 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
123 virtual MallocInst *clone() const;
125 // Methods for support type inquiry through isa, cast, and dyn_cast:
126 static inline bool classof(const MallocInst *) { return true; }
127 static inline bool classof(const Instruction *I) {
128 return (I->getOpcode() == Instruction::Malloc);
130 static inline bool classof(const Value *V) {
131 return isa<Instruction>(V) && classof(cast<Instruction>(V));
136 //===----------------------------------------------------------------------===//
138 //===----------------------------------------------------------------------===//
140 /// AllocaInst - an instruction to allocate memory on the stack
142 class AllocaInst : public AllocationInst {
143 AllocaInst(const AllocaInst &);
145 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
146 const std::string &NameStr = "",
147 Instruction *InsertBefore = 0)
148 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
149 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
150 BasicBlock *InsertAtEnd)
151 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
153 AllocaInst(const Type *Ty, const std::string &NameStr,
154 Instruction *InsertBefore = 0)
155 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
156 AllocaInst(const Type *Ty, const std::string &NameStr,
157 BasicBlock *InsertAtEnd)
158 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
160 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
161 const std::string &NameStr = "", Instruction *InsertBefore = 0)
162 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
163 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
164 const std::string &NameStr, BasicBlock *InsertAtEnd)
165 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
167 virtual AllocaInst *clone() const;
169 /// isStaticAlloca - Return true if this alloca is in the entry block of the
170 /// function and is a constant size. If so, the code generator will fold it
171 /// into the prolog/epilog code, so it is basically free.
172 bool isStaticAlloca() const;
174 // Methods for support type inquiry through isa, cast, and dyn_cast:
175 static inline bool classof(const AllocaInst *) { return true; }
176 static inline bool classof(const Instruction *I) {
177 return (I->getOpcode() == Instruction::Alloca);
179 static inline bool classof(const Value *V) {
180 return isa<Instruction>(V) && classof(cast<Instruction>(V));
185 //===----------------------------------------------------------------------===//
187 //===----------------------------------------------------------------------===//
189 /// FreeInst - an instruction to deallocate memory
191 class FreeInst : public UnaryInstruction {
194 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
195 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
197 virtual FreeInst *clone() const;
199 // Accessor methods for consistency with other memory operations
200 Value *getPointerOperand() { return getOperand(0); }
201 const Value *getPointerOperand() const { return getOperand(0); }
203 // Methods for support type inquiry through isa, cast, and dyn_cast:
204 static inline bool classof(const FreeInst *) { return true; }
205 static inline bool classof(const Instruction *I) {
206 return (I->getOpcode() == Instruction::Free);
208 static inline bool classof(const Value *V) {
209 return isa<Instruction>(V) && classof(cast<Instruction>(V));
214 //===----------------------------------------------------------------------===//
216 //===----------------------------------------------------------------------===//
218 /// LoadInst - an instruction for reading from memory. This uses the
219 /// SubclassData field in Value to store whether or not the load is volatile.
221 class LoadInst : public UnaryInstruction {
223 LoadInst(const LoadInst &LI)
224 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
225 setVolatile(LI.isVolatile());
226 setAlignment(LI.getAlignment());
234 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
235 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
236 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
237 Instruction *InsertBefore = 0);
238 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
239 unsigned Align, Instruction *InsertBefore = 0);
240 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
241 BasicBlock *InsertAtEnd);
242 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
243 unsigned Align, BasicBlock *InsertAtEnd);
245 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
246 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
247 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
248 bool isVolatile = false, Instruction *InsertBefore = 0);
249 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
250 BasicBlock *InsertAtEnd);
252 /// isVolatile - Return true if this is a load from a volatile memory
255 bool isVolatile() const { return SubclassData & 1; }
257 /// setVolatile - Specify whether this is a volatile load or not.
259 void setVolatile(bool V) {
260 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
263 virtual LoadInst *clone() const;
265 /// getAlignment - Return the alignment of the access that is being performed
267 unsigned getAlignment() const {
268 return (1 << (SubclassData>>1)) >> 1;
271 void setAlignment(unsigned Align);
273 Value *getPointerOperand() { return getOperand(0); }
274 const Value *getPointerOperand() const { return getOperand(0); }
275 static unsigned getPointerOperandIndex() { return 0U; }
277 // Methods for support type inquiry through isa, cast, and dyn_cast:
278 static inline bool classof(const LoadInst *) { return true; }
279 static inline bool classof(const Instruction *I) {
280 return I->getOpcode() == Instruction::Load;
282 static inline bool classof(const Value *V) {
283 return isa<Instruction>(V) && classof(cast<Instruction>(V));
288 //===----------------------------------------------------------------------===//
290 //===----------------------------------------------------------------------===//
292 /// StoreInst - an instruction for storing to memory
294 class StoreInst : public Instruction {
295 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
297 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
299 Op<0>() = SI.Op<0>();
300 Op<1>() = SI.Op<1>();
301 setVolatile(SI.isVolatile());
302 setAlignment(SI.getAlignment());
310 // allocate space for exactly two operands
311 void *operator new(size_t s) {
312 return User::operator new(s, 2);
314 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
315 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
316 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
317 Instruction *InsertBefore = 0);
318 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
319 unsigned Align, Instruction *InsertBefore = 0);
320 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
321 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
322 unsigned Align, BasicBlock *InsertAtEnd);
325 /// isVolatile - Return true if this is a load from a volatile memory
328 bool isVolatile() const { return SubclassData & 1; }
330 /// setVolatile - Specify whether this is a volatile load or not.
332 void setVolatile(bool V) {
333 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
336 /// Transparently provide more efficient getOperand methods.
337 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
339 /// getAlignment - Return the alignment of the access that is being performed
341 unsigned getAlignment() const {
342 return (1 << (SubclassData>>1)) >> 1;
345 void setAlignment(unsigned Align);
347 virtual StoreInst *clone() const;
349 Value *getPointerOperand() { return getOperand(1); }
350 const Value *getPointerOperand() const { return getOperand(1); }
351 static unsigned getPointerOperandIndex() { return 1U; }
353 // Methods for support type inquiry through isa, cast, and dyn_cast:
354 static inline bool classof(const StoreInst *) { return true; }
355 static inline bool classof(const Instruction *I) {
356 return I->getOpcode() == Instruction::Store;
358 static inline bool classof(const Value *V) {
359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
364 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
367 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
369 //===----------------------------------------------------------------------===//
370 // GetElementPtrInst Class
371 //===----------------------------------------------------------------------===//
373 // checkType - Simple wrapper function to give a better assertion failure
374 // message on bad indexes for a gep instruction.
376 static inline const Type *checkType(const Type *Ty) {
377 assert(Ty && "Invalid GetElementPtrInst indices for type!");
381 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
382 /// access elements of arrays and structs
384 class GetElementPtrInst : public Instruction {
385 GetElementPtrInst(const GetElementPtrInst &GEPI);
386 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
387 const std::string &NameStr);
388 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
390 template<typename InputIterator>
391 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
392 const std::string &NameStr,
393 // This argument ensures that we have an iterator we can
394 // do arithmetic on in constant time
395 std::random_access_iterator_tag) {
396 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
399 // This requires that the iterator points to contiguous memory.
400 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
401 // we have to build an array here
404 init(Ptr, 0, NumIdx, NameStr);
408 /// getIndexedType - Returns the type of the element that would be loaded with
409 /// a load instruction with the specified parameters.
411 /// Null is returned if the indices are invalid for the specified
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, &*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());
513 static const Type *getIndexedType(const Type *Ptr,
514 Value* const *Idx, unsigned NumIdx);
516 static const Type *getIndexedType(const Type *Ptr,
517 uint64_t const *Idx, unsigned NumIdx);
519 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
521 inline op_iterator idx_begin() { return op_begin()+1; }
522 inline const_op_iterator idx_begin() const { return op_begin()+1; }
523 inline op_iterator idx_end() { return op_end(); }
524 inline const_op_iterator idx_end() const { return op_end(); }
526 Value *getPointerOperand() {
527 return getOperand(0);
529 const Value *getPointerOperand() const {
530 return getOperand(0);
532 static unsigned getPointerOperandIndex() {
533 return 0U; // get index for modifying correct operand
536 /// getPointerOperandType - Method to return the pointer operand as a
538 const PointerType *getPointerOperandType() const {
539 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
543 unsigned getNumIndices() const { // Note: always non-negative
544 return getNumOperands() - 1;
547 bool hasIndices() const {
548 return getNumOperands() > 1;
551 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
552 /// zeros. If so, the result pointer and the first operand have the same
553 /// value, just potentially different types.
554 bool hasAllZeroIndices() const;
556 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
557 /// constant integers. If so, the result pointer and the first operand have
558 /// a constant offset between them.
559 bool hasAllConstantIndices() const;
562 // Methods for support type inquiry through isa, cast, and dyn_cast:
563 static inline bool classof(const GetElementPtrInst *) { return true; }
564 static inline bool classof(const Instruction *I) {
565 return (I->getOpcode() == Instruction::GetElementPtr);
567 static inline bool classof(const Value *V) {
568 return isa<Instruction>(V) && classof(cast<Instruction>(V));
573 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
576 template<typename InputIterator>
577 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
578 InputIterator IdxBegin,
579 InputIterator IdxEnd,
581 const std::string &NameStr,
582 Instruction *InsertBefore)
583 : Instruction(PointerType::get(checkType(
584 getIndexedType(Ptr->getType(),
586 cast<PointerType>(Ptr->getType())
587 ->getAddressSpace()),
589 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
590 Values, InsertBefore) {
591 init(Ptr, IdxBegin, IdxEnd, NameStr,
592 typename std::iterator_traits<InputIterator>::iterator_category());
594 template<typename InputIterator>
595 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
596 InputIterator IdxBegin,
597 InputIterator IdxEnd,
599 const std::string &NameStr,
600 BasicBlock *InsertAtEnd)
601 : Instruction(PointerType::get(checkType(
602 getIndexedType(Ptr->getType(),
604 cast<PointerType>(Ptr->getType())
605 ->getAddressSpace()),
607 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
608 Values, InsertAtEnd) {
609 init(Ptr, IdxBegin, IdxEnd, NameStr,
610 typename std::iterator_traits<InputIterator>::iterator_category());
614 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
617 //===----------------------------------------------------------------------===//
619 //===----------------------------------------------------------------------===//
621 /// This instruction compares its operands according to the predicate given
622 /// to the constructor. It only operates on integers or pointers. The operands
623 /// must be identical types.
624 /// @brief Represent an integer comparison operator.
625 class ICmpInst: public CmpInst {
627 /// @brief Constructor with insert-before-instruction semantics.
629 Predicate pred, ///< The predicate to use for the comparison
630 Value *LHS, ///< The left-hand-side of the expression
631 Value *RHS, ///< The right-hand-side of the expression
632 const std::string &NameStr = "", ///< Name of the instruction
633 Instruction *InsertBefore = 0 ///< Where to insert
634 ) : CmpInst(makeCmpResultType(LHS->getType()),
635 Instruction::ICmp, pred, LHS, RHS, NameStr,
637 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
638 pred <= CmpInst::LAST_ICMP_PREDICATE &&
639 "Invalid ICmp predicate value");
640 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
641 "Both operands to ICmp instruction are not of the same type!");
642 // Check that the operands are the right type
643 assert((getOperand(0)->getType()->isIntOrIntVector() ||
644 isa<PointerType>(getOperand(0)->getType())) &&
645 "Invalid operand types for ICmp instruction");
648 /// @brief Constructor with insert-at-block-end semantics.
650 Predicate pred, ///< The predicate to use for the comparison
651 Value *LHS, ///< The left-hand-side of the expression
652 Value *RHS, ///< The right-hand-side of the expression
653 const std::string &NameStr, ///< Name of the instruction
654 BasicBlock *InsertAtEnd ///< Block to insert into.
655 ) : CmpInst(makeCmpResultType(LHS->getType()),
656 Instruction::ICmp, pred, LHS, RHS, NameStr,
658 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
659 pred <= CmpInst::LAST_ICMP_PREDICATE &&
660 "Invalid ICmp predicate value");
661 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
662 "Both operands to ICmp instruction are not of the same type!");
663 // Check that the operands are the right type
664 assert((getOperand(0)->getType()->isIntOrIntVector() ||
665 isa<PointerType>(getOperand(0)->getType())) &&
666 "Invalid operand types for ICmp instruction");
669 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
670 /// @returns the predicate that would be the result if the operand were
671 /// regarded as signed.
672 /// @brief Return the signed version of the predicate
673 Predicate getSignedPredicate() const {
674 return getSignedPredicate(getPredicate());
677 /// This is a static version that you can use without an instruction.
678 /// @brief Return the signed version of the predicate.
679 static Predicate getSignedPredicate(Predicate pred);
681 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
682 /// @returns the predicate that would be the result if the operand were
683 /// regarded as unsigned.
684 /// @brief Return the unsigned version of the predicate
685 Predicate getUnsignedPredicate() const {
686 return getUnsignedPredicate(getPredicate());
689 /// This is a static version that you can use without an instruction.
690 /// @brief Return the unsigned version of the predicate.
691 static Predicate getUnsignedPredicate(Predicate pred);
693 /// isEquality - Return true if this predicate is either EQ or NE. This also
694 /// tests for commutativity.
695 static bool isEquality(Predicate P) {
696 return P == ICMP_EQ || P == ICMP_NE;
699 /// isEquality - Return true if this predicate is either EQ or NE. This also
700 /// tests for commutativity.
701 bool isEquality() const {
702 return isEquality(getPredicate());
705 /// @returns true if the predicate of this ICmpInst is commutative
706 /// @brief Determine if this relation is commutative.
707 bool isCommutative() const { return isEquality(); }
709 /// isRelational - Return true if the predicate is relational (not EQ or NE).
711 bool isRelational() const {
712 return !isEquality();
715 /// isRelational - Return true if the predicate is relational (not EQ or NE).
717 static bool isRelational(Predicate P) {
718 return !isEquality(P);
721 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
722 /// @brief Determine if this instruction's predicate is signed.
723 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
725 /// @returns true if the predicate provided is signed, false otherwise
726 /// @brief Determine if the predicate is signed.
727 static bool isSignedPredicate(Predicate pred);
729 /// @returns true if the specified compare predicate is
730 /// true when both operands are equal...
731 /// @brief Determine if the icmp is true when both operands are equal
732 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
733 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
734 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
735 pred == ICmpInst::ICMP_SLE;
738 /// @returns true if the specified compare instruction is
739 /// true when both operands are equal...
740 /// @brief Determine if the ICmpInst returns true when both operands are equal
741 bool isTrueWhenEqual() {
742 return isTrueWhenEqual(getPredicate());
745 /// Initialize a set of values that all satisfy the predicate with C.
746 /// @brief Make a ConstantRange for a relation with a constant value.
747 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
749 /// Exchange the two operands to this instruction in such a way that it does
750 /// not modify the semantics of the instruction. The predicate value may be
751 /// changed to retain the same result if the predicate is order dependent
753 /// @brief Swap operands and adjust predicate.
754 void swapOperands() {
755 SubclassData = getSwappedPredicate();
756 Op<0>().swap(Op<1>());
759 virtual ICmpInst *clone() const;
761 // Methods for support type inquiry through isa, cast, and dyn_cast:
762 static inline bool classof(const ICmpInst *) { return true; }
763 static inline bool classof(const Instruction *I) {
764 return I->getOpcode() == Instruction::ICmp;
766 static inline bool classof(const Value *V) {
767 return isa<Instruction>(V) && classof(cast<Instruction>(V));
772 //===----------------------------------------------------------------------===//
774 //===----------------------------------------------------------------------===//
776 /// This instruction compares its operands according to the predicate given
777 /// to the constructor. It only operates on floating point values or packed
778 /// vectors of floating point values. The operands must be identical types.
779 /// @brief Represents a floating point comparison operator.
780 class FCmpInst: public CmpInst {
782 /// @brief Constructor with insert-before-instruction semantics.
784 Predicate pred, ///< The predicate to use for the comparison
785 Value *LHS, ///< The left-hand-side of the expression
786 Value *RHS, ///< The right-hand-side of the expression
787 const std::string &NameStr = "", ///< Name of the instruction
788 Instruction *InsertBefore = 0 ///< Where to insert
789 ) : CmpInst(makeCmpResultType(LHS->getType()),
790 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()->isFPOrFPVector() &&
798 "Invalid operand types for FCmp instruction");
801 /// @brief Constructor with insert-at-block-end semantics.
803 Predicate pred, ///< The predicate to use for the comparison
804 Value *LHS, ///< The left-hand-side of the expression
805 Value *RHS, ///< The right-hand-side of the expression
806 const std::string &NameStr, ///< Name of the instruction
807 BasicBlock *InsertAtEnd ///< Block to insert into.
808 ) : CmpInst(makeCmpResultType(LHS->getType()),
809 Instruction::FCmp, pred, LHS, RHS, NameStr,
811 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
812 "Invalid FCmp predicate value");
813 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
814 "Both operands to FCmp instruction are not of the same type!");
815 // Check that the operands are the right type
816 assert(getOperand(0)->getType()->isFPOrFPVector() &&
817 "Invalid operand types for FCmp instruction");
820 /// @returns true if the predicate of this instruction is EQ or NE.
821 /// @brief Determine if this is an equality predicate.
822 bool isEquality() const {
823 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
824 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
827 /// @returns true if the predicate of this instruction is commutative.
828 /// @brief Determine if this is a commutative predicate.
829 bool isCommutative() const {
830 return isEquality() ||
831 SubclassData == FCMP_FALSE ||
832 SubclassData == FCMP_TRUE ||
833 SubclassData == FCMP_ORD ||
834 SubclassData == FCMP_UNO;
837 /// @returns true if the predicate is relational (not EQ or NE).
838 /// @brief Determine if this a relational predicate.
839 bool isRelational() const { return !isEquality(); }
841 /// Exchange the two operands to this instruction in such a way that it does
842 /// not modify the semantics of the instruction. The predicate value may be
843 /// changed to retain the same result if the predicate is order dependent
845 /// @brief Swap operands and adjust predicate.
846 void swapOperands() {
847 SubclassData = getSwappedPredicate();
848 Op<0>().swap(Op<1>());
851 virtual FCmpInst *clone() const;
853 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
854 static inline bool classof(const FCmpInst *) { return true; }
855 static inline bool classof(const Instruction *I) {
856 return I->getOpcode() == Instruction::FCmp;
858 static inline bool classof(const Value *V) {
859 return isa<Instruction>(V) && classof(cast<Instruction>(V));
864 //===----------------------------------------------------------------------===//
866 //===----------------------------------------------------------------------===//
867 /// CallInst - This class represents a function call, abstracting a target
868 /// machine's calling convention. This class uses low bit of the SubClassData
869 /// field to indicate whether or not this is a tail call. The rest of the bits
870 /// hold the calling convention of the call.
873 class CallInst : public Instruction {
874 AttrListPtr AttributeList; ///< parameter attributes for call
875 CallInst(const CallInst &CI);
876 void init(Value *Func, Value* const *Params, unsigned NumParams);
877 void init(Value *Func, Value *Actual1, Value *Actual2);
878 void init(Value *Func, Value *Actual);
879 void init(Value *Func);
881 template<typename InputIterator>
882 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
883 const std::string &NameStr,
884 // This argument ensures that we have an iterator we can
885 // do arithmetic on in constant time
886 std::random_access_iterator_tag) {
887 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
889 // This requires that the iterator points to contiguous memory.
890 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
894 /// Construct a CallInst given a range of arguments. InputIterator
895 /// must be a random-access iterator pointing to contiguous storage
896 /// (e.g. a std::vector<>::iterator). Checks are made for
897 /// random-accessness but not for contiguous storage as that would
898 /// incur runtime overhead.
899 /// @brief Construct a CallInst from a range of arguments
900 template<typename InputIterator>
901 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
902 const std::string &NameStr, Instruction *InsertBefore);
904 /// Construct a CallInst given a range of arguments. InputIterator
905 /// must be a random-access iterator pointing to contiguous storage
906 /// (e.g. a std::vector<>::iterator). Checks are made for
907 /// random-accessness but not for contiguous storage as that would
908 /// incur runtime overhead.
909 /// @brief Construct a CallInst from a range of arguments
910 template<typename InputIterator>
911 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
912 const std::string &NameStr, BasicBlock *InsertAtEnd);
914 CallInst(Value *F, Value *Actual, const std::string& NameStr,
915 Instruction *InsertBefore);
916 CallInst(Value *F, Value *Actual, const std::string& NameStr,
917 BasicBlock *InsertAtEnd);
918 explicit CallInst(Value *F, const std::string &NameStr,
919 Instruction *InsertBefore);
920 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
922 template<typename InputIterator>
923 static CallInst *Create(Value *Func,
924 InputIterator ArgBegin, InputIterator ArgEnd,
925 const std::string &NameStr = "",
926 Instruction *InsertBefore = 0) {
927 return new((unsigned)(ArgEnd - ArgBegin + 1))
928 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
930 template<typename InputIterator>
931 static CallInst *Create(Value *Func,
932 InputIterator ArgBegin, InputIterator ArgEnd,
933 const std::string &NameStr, BasicBlock *InsertAtEnd) {
934 return new((unsigned)(ArgEnd - ArgBegin + 1))
935 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
937 static CallInst *Create(Value *F, Value *Actual,
938 const std::string& NameStr = "",
939 Instruction *InsertBefore = 0) {
940 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
942 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
943 BasicBlock *InsertAtEnd) {
944 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
946 static CallInst *Create(Value *F, const std::string &NameStr = "",
947 Instruction *InsertBefore = 0) {
948 return new(1) CallInst(F, NameStr, InsertBefore);
950 static CallInst *Create(Value *F, const std::string &NameStr,
951 BasicBlock *InsertAtEnd) {
952 return new(1) CallInst(F, NameStr, InsertAtEnd);
957 bool isTailCall() const { return SubclassData & 1; }
958 void setTailCall(bool isTC = true) {
959 SubclassData = (SubclassData & ~1) | unsigned(isTC);
962 virtual CallInst *clone() const;
964 /// Provide fast operand accessors
965 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
967 /// getCallingConv/setCallingConv - Get or set the calling convention of this
969 unsigned getCallingConv() const { return SubclassData >> 1; }
970 void setCallingConv(unsigned CC) {
971 SubclassData = (SubclassData & 1) | (CC << 1);
974 /// getAttributes - Return the parameter attributes for this call.
976 const AttrListPtr &getAttributes() const { return AttributeList; }
978 /// setAttributes - Set the parameter attributes for this call.
980 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
982 /// addAttribute - adds the attribute to the list of attributes.
983 void addAttribute(unsigned i, Attributes attr);
985 /// removeAttribute - removes the attribute from the list of attributes.
986 void removeAttribute(unsigned i, Attributes attr);
988 /// @brief Determine whether the call or the callee has the given attribute.
989 bool paramHasAttr(unsigned i, Attributes attr) const;
991 /// @brief Extract the alignment for a call or parameter (0=unknown).
992 unsigned getParamAlignment(unsigned i) const {
993 return AttributeList.getParamAlignment(i);
996 /// @brief Determine if the call does not access memory.
997 bool doesNotAccessMemory() const {
998 return paramHasAttr(~0, Attribute::ReadNone);
1000 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1001 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1002 else removeAttribute(~0, Attribute::ReadNone);
1005 /// @brief Determine if the call does not access or only reads memory.
1006 bool onlyReadsMemory() const {
1007 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1009 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1010 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1011 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1014 /// @brief Determine if the call cannot return.
1015 bool doesNotReturn() const {
1016 return paramHasAttr(~0, Attribute::NoReturn);
1018 void setDoesNotReturn(bool DoesNotReturn = true) {
1019 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1020 else removeAttribute(~0, Attribute::NoReturn);
1023 /// @brief Determine if the call cannot unwind.
1024 bool doesNotThrow() const {
1025 return paramHasAttr(~0, Attribute::NoUnwind);
1027 void setDoesNotThrow(bool DoesNotThrow = true) {
1028 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1029 else removeAttribute(~0, Attribute::NoUnwind);
1032 /// @brief Determine if the call returns a structure through first
1033 /// pointer argument.
1034 bool hasStructRetAttr() const {
1035 // Be friendly and also check the callee.
1036 return paramHasAttr(1, Attribute::StructRet);
1039 /// @brief Determine if any call argument is an aggregate passed by value.
1040 bool hasByValArgument() const {
1041 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1044 /// getCalledFunction - Return the function called, or null if this is an
1045 /// indirect function invocation.
1047 Function *getCalledFunction() const {
1048 return dyn_cast<Function>(Op<0>());
1051 /// getCalledValue - Get a pointer to the function that is invoked by this
1053 const Value *getCalledValue() const { return Op<0>(); }
1054 Value *getCalledValue() { return Op<0>(); }
1056 // Methods for support type inquiry through isa, cast, and dyn_cast:
1057 static inline bool classof(const CallInst *) { return true; }
1058 static inline bool classof(const Instruction *I) {
1059 return I->getOpcode() == Instruction::Call;
1061 static inline bool classof(const Value *V) {
1062 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1067 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1070 template<typename InputIterator>
1071 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1072 const std::string &NameStr, BasicBlock *InsertAtEnd)
1073 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1074 ->getElementType())->getReturnType(),
1076 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1077 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1078 init(Func, ArgBegin, ArgEnd, NameStr,
1079 typename std::iterator_traits<InputIterator>::iterator_category());
1082 template<typename InputIterator>
1083 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1084 const std::string &NameStr, Instruction *InsertBefore)
1085 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1086 ->getElementType())->getReturnType(),
1088 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1089 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1090 init(Func, ArgBegin, ArgEnd, NameStr,
1091 typename std::iterator_traits<InputIterator>::iterator_category());
1094 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1096 //===----------------------------------------------------------------------===//
1098 //===----------------------------------------------------------------------===//
1100 /// SelectInst - This class represents the LLVM 'select' instruction.
1102 class SelectInst : public Instruction {
1103 void init(Value *C, Value *S1, Value *S2) {
1104 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1110 SelectInst(const SelectInst &SI)
1111 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1112 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1114 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1115 Instruction *InsertBefore)
1116 : Instruction(S1->getType(), Instruction::Select,
1117 &Op<0>(), 3, InsertBefore) {
1121 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1122 BasicBlock *InsertAtEnd)
1123 : Instruction(S1->getType(), Instruction::Select,
1124 &Op<0>(), 3, InsertAtEnd) {
1129 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1130 const std::string &NameStr = "",
1131 Instruction *InsertBefore = 0) {
1132 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1134 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1135 const std::string &NameStr,
1136 BasicBlock *InsertAtEnd) {
1137 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1140 Value *getCondition() const { return Op<0>(); }
1141 Value *getTrueValue() const { return Op<1>(); }
1142 Value *getFalseValue() const { return Op<2>(); }
1144 /// areInvalidOperands - Return a string if the specified operands are invalid
1145 /// for a select operation, otherwise return null.
1146 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1148 /// Transparently provide more efficient getOperand methods.
1149 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1151 OtherOps getOpcode() const {
1152 return static_cast<OtherOps>(Instruction::getOpcode());
1155 virtual SelectInst *clone() const;
1157 // Methods for support type inquiry through isa, cast, and dyn_cast:
1158 static inline bool classof(const SelectInst *) { return true; }
1159 static inline bool classof(const Instruction *I) {
1160 return I->getOpcode() == Instruction::Select;
1162 static inline bool classof(const Value *V) {
1163 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1168 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1171 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1173 //===----------------------------------------------------------------------===//
1175 //===----------------------------------------------------------------------===//
1177 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1178 /// an argument of the specified type given a va_list and increments that list
1180 class VAArgInst : public UnaryInstruction {
1181 VAArgInst(const VAArgInst &VAA)
1182 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1184 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1185 Instruction *InsertBefore = 0)
1186 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1189 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1190 BasicBlock *InsertAtEnd)
1191 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1195 virtual VAArgInst *clone() const;
1197 // Methods for support type inquiry through isa, cast, and dyn_cast:
1198 static inline bool classof(const VAArgInst *) { return true; }
1199 static inline bool classof(const Instruction *I) {
1200 return I->getOpcode() == VAArg;
1202 static inline bool classof(const Value *V) {
1203 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1207 //===----------------------------------------------------------------------===//
1208 // ExtractElementInst Class
1209 //===----------------------------------------------------------------------===//
1211 /// ExtractElementInst - This instruction extracts a single (scalar)
1212 /// element from a VectorType value
1214 class ExtractElementInst : public Instruction {
1215 ExtractElementInst(const ExtractElementInst &EE) :
1216 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1217 Op<0>() = EE.Op<0>();
1218 Op<1>() = EE.Op<1>();
1222 // allocate space for exactly two operands
1223 void *operator new(size_t s) {
1224 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1226 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1227 Instruction *InsertBefore = 0);
1228 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
1229 Instruction *InsertBefore = 0);
1230 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1231 BasicBlock *InsertAtEnd);
1232 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
1233 BasicBlock *InsertAtEnd);
1235 /// isValidOperands - Return true if an extractelement instruction can be
1236 /// formed with the specified operands.
1237 static bool isValidOperands(const Value *Vec, const Value *Idx);
1239 virtual ExtractElementInst *clone() const;
1241 /// Transparently provide more efficient getOperand methods.
1242 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1244 // Methods for support type inquiry through isa, cast, and dyn_cast:
1245 static inline bool classof(const ExtractElementInst *) { return true; }
1246 static inline bool classof(const Instruction *I) {
1247 return I->getOpcode() == Instruction::ExtractElement;
1249 static inline bool classof(const Value *V) {
1250 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1255 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1258 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1260 //===----------------------------------------------------------------------===//
1261 // InsertElementInst Class
1262 //===----------------------------------------------------------------------===//
1264 /// InsertElementInst - This instruction inserts a single (scalar)
1265 /// element into a VectorType value
1267 class InsertElementInst : public Instruction {
1268 InsertElementInst(const InsertElementInst &IE);
1269 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1270 const std::string &NameStr = "",
1271 Instruction *InsertBefore = 0);
1272 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1273 const std::string &NameStr = "",
1274 Instruction *InsertBefore = 0);
1275 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1276 const std::string &NameStr, BasicBlock *InsertAtEnd);
1277 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1278 const std::string &NameStr, BasicBlock *InsertAtEnd);
1280 static InsertElementInst *Create(const InsertElementInst &IE) {
1281 return new(IE.getNumOperands()) InsertElementInst(IE);
1283 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1284 const std::string &NameStr = "",
1285 Instruction *InsertBefore = 0) {
1286 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1288 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1289 const std::string &NameStr = "",
1290 Instruction *InsertBefore = 0) {
1291 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1293 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1294 const std::string &NameStr,
1295 BasicBlock *InsertAtEnd) {
1296 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1298 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1299 const std::string &NameStr,
1300 BasicBlock *InsertAtEnd) {
1301 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1304 /// isValidOperands - Return true if an insertelement instruction can be
1305 /// formed with the specified operands.
1306 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1309 virtual InsertElementInst *clone() const;
1311 /// getType - Overload to return most specific vector type.
1313 const VectorType *getType() const {
1314 return reinterpret_cast<const VectorType*>(Instruction::getType());
1317 /// Transparently provide more efficient getOperand methods.
1318 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1320 // Methods for support type inquiry through isa, cast, and dyn_cast:
1321 static inline bool classof(const InsertElementInst *) { return true; }
1322 static inline bool classof(const Instruction *I) {
1323 return I->getOpcode() == Instruction::InsertElement;
1325 static inline bool classof(const Value *V) {
1326 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1331 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1334 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1336 //===----------------------------------------------------------------------===//
1337 // ShuffleVectorInst Class
1338 //===----------------------------------------------------------------------===//
1340 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1343 class ShuffleVectorInst : public Instruction {
1344 ShuffleVectorInst(const ShuffleVectorInst &IE);
1346 // allocate space for exactly three operands
1347 void *operator new(size_t s) {
1348 return User::operator new(s, 3);
1350 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1351 const std::string &NameStr = "",
1352 Instruction *InsertBefor = 0);
1353 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1354 const std::string &NameStr, BasicBlock *InsertAtEnd);
1356 /// isValidOperands - Return true if a shufflevector instruction can be
1357 /// formed with the specified operands.
1358 static bool isValidOperands(const Value *V1, const Value *V2,
1361 virtual ShuffleVectorInst *clone() const;
1363 /// getType - Overload to return most specific vector type.
1365 const VectorType *getType() const {
1366 return reinterpret_cast<const VectorType*>(Instruction::getType());
1369 /// Transparently provide more efficient getOperand methods.
1370 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1372 /// getMaskValue - Return the index from the shuffle mask for the specified
1373 /// output result. This is either -1 if the element is undef or a number less
1374 /// than 2*numelements.
1375 int getMaskValue(unsigned i) const;
1377 // Methods for support type inquiry through isa, cast, and dyn_cast:
1378 static inline bool classof(const ShuffleVectorInst *) { return true; }
1379 static inline bool classof(const Instruction *I) {
1380 return I->getOpcode() == Instruction::ShuffleVector;
1382 static inline bool classof(const Value *V) {
1383 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1388 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1391 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1393 //===----------------------------------------------------------------------===//
1394 // ExtractValueInst Class
1395 //===----------------------------------------------------------------------===//
1397 /// ExtractValueInst - This instruction extracts a struct member or array
1398 /// element value from an aggregate value.
1400 class ExtractValueInst : public UnaryInstruction {
1401 SmallVector<unsigned, 4> Indices;
1403 ExtractValueInst(const ExtractValueInst &EVI);
1404 void init(const unsigned *Idx, unsigned NumIdx,
1405 const std::string &NameStr);
1406 void init(unsigned Idx, const std::string &NameStr);
1408 template<typename InputIterator>
1409 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1410 const std::string &NameStr,
1411 // This argument ensures that we have an iterator we can
1412 // do arithmetic on in constant time
1413 std::random_access_iterator_tag) {
1414 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1416 // There's no fundamental reason why we require at least one index
1417 // (other than weirdness with &*IdxBegin being invalid; see
1418 // getelementptr's init routine for example). But there's no
1419 // present need to support it.
1420 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1422 // This requires that the iterator points to contiguous memory.
1423 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1424 // we have to build an array here
1427 /// getIndexedType - Returns the type of the element that would be extracted
1428 /// with an extractvalue instruction with the specified parameters.
1430 /// Null is returned if the indices are invalid for the specified
1433 static const Type *getIndexedType(const Type *Agg,
1434 const unsigned *Idx, unsigned NumIdx);
1436 template<typename InputIterator>
1437 static const Type *getIndexedType(const Type *Ptr,
1438 InputIterator IdxBegin,
1439 InputIterator IdxEnd,
1440 // This argument ensures that we
1441 // have an iterator we can do
1442 // arithmetic on in constant time
1443 std::random_access_iterator_tag) {
1444 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1447 // This requires that the iterator points to contiguous memory.
1448 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1450 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1453 /// Constructors - Create a extractvalue instruction with a base aggregate
1454 /// value and a list of indices. The first ctor can optionally insert before
1455 /// an existing instruction, the second appends the new instruction to the
1456 /// specified BasicBlock.
1457 template<typename InputIterator>
1458 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1459 InputIterator IdxEnd,
1460 const std::string &NameStr,
1461 Instruction *InsertBefore);
1462 template<typename InputIterator>
1463 inline ExtractValueInst(Value *Agg,
1464 InputIterator IdxBegin, InputIterator IdxEnd,
1465 const std::string &NameStr, BasicBlock *InsertAtEnd);
1467 // allocate space for exactly one operand
1468 void *operator new(size_t s) {
1469 return User::operator new(s, 1);
1473 template<typename InputIterator>
1474 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1475 InputIterator IdxEnd,
1476 const std::string &NameStr = "",
1477 Instruction *InsertBefore = 0) {
1479 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1481 template<typename InputIterator>
1482 static ExtractValueInst *Create(Value *Agg,
1483 InputIterator IdxBegin, InputIterator IdxEnd,
1484 const std::string &NameStr,
1485 BasicBlock *InsertAtEnd) {
1486 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1489 /// Constructors - These two creators are convenience methods because one
1490 /// index extractvalue instructions are much more common than those with
1492 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1493 const std::string &NameStr = "",
1494 Instruction *InsertBefore = 0) {
1495 unsigned Idxs[1] = { Idx };
1496 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1498 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1499 const std::string &NameStr,
1500 BasicBlock *InsertAtEnd) {
1501 unsigned Idxs[1] = { Idx };
1502 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1505 virtual ExtractValueInst *clone() const;
1507 /// getIndexedType - Returns the type of the element that would be extracted
1508 /// with an extractvalue instruction with the specified parameters.
1510 /// Null is returned if the indices are invalid for the specified
1513 template<typename InputIterator>
1514 static const Type *getIndexedType(const Type *Ptr,
1515 InputIterator IdxBegin,
1516 InputIterator IdxEnd) {
1517 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1518 typename std::iterator_traits<InputIterator>::
1519 iterator_category());
1521 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1523 typedef const unsigned* idx_iterator;
1524 inline idx_iterator idx_begin() const { return Indices.begin(); }
1525 inline idx_iterator idx_end() const { return Indices.end(); }
1527 Value *getAggregateOperand() {
1528 return getOperand(0);
1530 const Value *getAggregateOperand() const {
1531 return getOperand(0);
1533 static unsigned getAggregateOperandIndex() {
1534 return 0U; // get index for modifying correct operand
1537 unsigned getNumIndices() const { // Note: always non-negative
1538 return (unsigned)Indices.size();
1541 bool hasIndices() const {
1545 // Methods for support type inquiry through isa, cast, and dyn_cast:
1546 static inline bool classof(const ExtractValueInst *) { return true; }
1547 static inline bool classof(const Instruction *I) {
1548 return I->getOpcode() == Instruction::ExtractValue;
1550 static inline bool classof(const Value *V) {
1551 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1555 template<typename InputIterator>
1556 ExtractValueInst::ExtractValueInst(Value *Agg,
1557 InputIterator IdxBegin,
1558 InputIterator IdxEnd,
1559 const std::string &NameStr,
1560 Instruction *InsertBefore)
1561 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1563 ExtractValue, Agg, InsertBefore) {
1564 init(IdxBegin, IdxEnd, NameStr,
1565 typename std::iterator_traits<InputIterator>::iterator_category());
1567 template<typename InputIterator>
1568 ExtractValueInst::ExtractValueInst(Value *Agg,
1569 InputIterator IdxBegin,
1570 InputIterator IdxEnd,
1571 const std::string &NameStr,
1572 BasicBlock *InsertAtEnd)
1573 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1575 ExtractValue, Agg, InsertAtEnd) {
1576 init(IdxBegin, IdxEnd, NameStr,
1577 typename std::iterator_traits<InputIterator>::iterator_category());
1581 //===----------------------------------------------------------------------===//
1582 // InsertValueInst Class
1583 //===----------------------------------------------------------------------===//
1585 /// InsertValueInst - This instruction inserts a struct field of array element
1586 /// value into an aggregate value.
1588 class InsertValueInst : public Instruction {
1589 SmallVector<unsigned, 4> Indices;
1591 void *operator new(size_t, unsigned); // Do not implement
1592 InsertValueInst(const InsertValueInst &IVI);
1593 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1594 const std::string &NameStr);
1595 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1597 template<typename InputIterator>
1598 void init(Value *Agg, Value *Val,
1599 InputIterator IdxBegin, InputIterator IdxEnd,
1600 const std::string &NameStr,
1601 // This argument ensures that we have an iterator we can
1602 // do arithmetic on in constant time
1603 std::random_access_iterator_tag) {
1604 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1606 // There's no fundamental reason why we require at least one index
1607 // (other than weirdness with &*IdxBegin being invalid; see
1608 // getelementptr's init routine for example). But there's no
1609 // present need to support it.
1610 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1612 // This requires that the iterator points to contiguous memory.
1613 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1614 // we have to build an array here
1617 /// Constructors - Create a insertvalue instruction with a base aggregate
1618 /// value, a value to insert, and a list of indices. The first ctor can
1619 /// optionally insert before an existing instruction, the second appends
1620 /// the new instruction to the specified BasicBlock.
1621 template<typename InputIterator>
1622 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1623 InputIterator IdxEnd,
1624 const std::string &NameStr,
1625 Instruction *InsertBefore);
1626 template<typename InputIterator>
1627 inline InsertValueInst(Value *Agg, Value *Val,
1628 InputIterator IdxBegin, InputIterator IdxEnd,
1629 const std::string &NameStr, BasicBlock *InsertAtEnd);
1631 /// Constructors - These two constructors are convenience methods because one
1632 /// and two index insertvalue instructions are so common.
1633 InsertValueInst(Value *Agg, Value *Val,
1634 unsigned Idx, const std::string &NameStr = "",
1635 Instruction *InsertBefore = 0);
1636 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1637 const std::string &NameStr, BasicBlock *InsertAtEnd);
1639 // allocate space for exactly two operands
1640 void *operator new(size_t s) {
1641 return User::operator new(s, 2);
1644 template<typename InputIterator>
1645 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1646 InputIterator IdxEnd,
1647 const std::string &NameStr = "",
1648 Instruction *InsertBefore = 0) {
1649 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1650 NameStr, InsertBefore);
1652 template<typename InputIterator>
1653 static InsertValueInst *Create(Value *Agg, Value *Val,
1654 InputIterator IdxBegin, InputIterator IdxEnd,
1655 const std::string &NameStr,
1656 BasicBlock *InsertAtEnd) {
1657 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1658 NameStr, InsertAtEnd);
1661 /// Constructors - These two creators are convenience methods because one
1662 /// index insertvalue instructions are much more common than those with
1664 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1665 const std::string &NameStr = "",
1666 Instruction *InsertBefore = 0) {
1667 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1669 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1670 const std::string &NameStr,
1671 BasicBlock *InsertAtEnd) {
1672 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1675 virtual InsertValueInst *clone() const;
1677 /// Transparently provide more efficient getOperand methods.
1678 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1680 typedef const unsigned* idx_iterator;
1681 inline idx_iterator idx_begin() const { return Indices.begin(); }
1682 inline idx_iterator idx_end() const { return Indices.end(); }
1684 Value *getAggregateOperand() {
1685 return getOperand(0);
1687 const Value *getAggregateOperand() const {
1688 return getOperand(0);
1690 static unsigned getAggregateOperandIndex() {
1691 return 0U; // get index for modifying correct operand
1694 Value *getInsertedValueOperand() {
1695 return getOperand(1);
1697 const Value *getInsertedValueOperand() const {
1698 return getOperand(1);
1700 static unsigned getInsertedValueOperandIndex() {
1701 return 1U; // get index for modifying correct operand
1704 unsigned getNumIndices() const { // Note: always non-negative
1705 return (unsigned)Indices.size();
1708 bool hasIndices() const {
1712 // Methods for support type inquiry through isa, cast, and dyn_cast:
1713 static inline bool classof(const InsertValueInst *) { return true; }
1714 static inline bool classof(const Instruction *I) {
1715 return I->getOpcode() == Instruction::InsertValue;
1717 static inline bool classof(const Value *V) {
1718 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1723 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1726 template<typename InputIterator>
1727 InsertValueInst::InsertValueInst(Value *Agg,
1729 InputIterator IdxBegin,
1730 InputIterator IdxEnd,
1731 const std::string &NameStr,
1732 Instruction *InsertBefore)
1733 : Instruction(Agg->getType(), InsertValue,
1734 OperandTraits<InsertValueInst>::op_begin(this),
1736 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1737 typename std::iterator_traits<InputIterator>::iterator_category());
1739 template<typename InputIterator>
1740 InsertValueInst::InsertValueInst(Value *Agg,
1742 InputIterator IdxBegin,
1743 InputIterator IdxEnd,
1744 const std::string &NameStr,
1745 BasicBlock *InsertAtEnd)
1746 : Instruction(Agg->getType(), InsertValue,
1747 OperandTraits<InsertValueInst>::op_begin(this),
1749 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1750 typename std::iterator_traits<InputIterator>::iterator_category());
1753 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1755 //===----------------------------------------------------------------------===//
1757 //===----------------------------------------------------------------------===//
1759 // PHINode - The PHINode class is used to represent the magical mystical PHI
1760 // node, that can not exist in nature, but can be synthesized in a computer
1761 // scientist's overactive imagination.
1763 class PHINode : public Instruction {
1764 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1765 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1766 /// the number actually in use.
1767 unsigned ReservedSpace;
1768 PHINode(const PHINode &PN);
1769 // allocate space for exactly zero operands
1770 void *operator new(size_t s) {
1771 return User::operator new(s, 0);
1773 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1774 Instruction *InsertBefore = 0)
1775 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1780 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1781 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1786 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1787 Instruction *InsertBefore = 0) {
1788 return new PHINode(Ty, NameStr, InsertBefore);
1790 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1791 BasicBlock *InsertAtEnd) {
1792 return new PHINode(Ty, NameStr, InsertAtEnd);
1796 /// reserveOperandSpace - This method can be used to avoid repeated
1797 /// reallocation of PHI operand lists by reserving space for the correct
1798 /// number of operands before adding them. Unlike normal vector reserves,
1799 /// this method can also be used to trim the operand space.
1800 void reserveOperandSpace(unsigned NumValues) {
1801 resizeOperands(NumValues*2);
1804 virtual PHINode *clone() const;
1806 /// Provide fast operand accessors
1807 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1809 /// getNumIncomingValues - Return the number of incoming edges
1811 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1813 /// getIncomingValue - Return incoming value number x
1815 Value *getIncomingValue(unsigned i) const {
1816 assert(i*2 < getNumOperands() && "Invalid value number!");
1817 return getOperand(i*2);
1819 void setIncomingValue(unsigned i, Value *V) {
1820 assert(i*2 < getNumOperands() && "Invalid value number!");
1823 static unsigned getOperandNumForIncomingValue(unsigned i) {
1826 static unsigned getIncomingValueNumForOperand(unsigned i) {
1827 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1831 /// getIncomingBlock - Return incoming basic block corresponding
1832 /// to value use iterator
1834 template <typename U>
1835 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1836 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1837 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1839 /// getIncomingBlock - Return incoming basic block number x
1841 BasicBlock *getIncomingBlock(unsigned i) const {
1842 return static_cast<BasicBlock*>(getOperand(i*2+1));
1844 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1845 setOperand(i*2+1, BB);
1847 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1850 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1851 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1855 /// addIncoming - Add an incoming value to the end of the PHI list
1857 void addIncoming(Value *V, BasicBlock *BB) {
1858 assert(V && "PHI node got a null value!");
1859 assert(BB && "PHI node got a null basic block!");
1860 assert(getType() == V->getType() &&
1861 "All operands to PHI node must be the same type as the PHI node!");
1862 unsigned OpNo = NumOperands;
1863 if (OpNo+2 > ReservedSpace)
1864 resizeOperands(0); // Get more space!
1865 // Initialize some new operands.
1866 NumOperands = OpNo+2;
1867 OperandList[OpNo] = V;
1868 OperandList[OpNo+1] = BB;
1871 /// removeIncomingValue - Remove an incoming value. This is useful if a
1872 /// predecessor basic block is deleted. The value removed is returned.
1874 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1875 /// is true), the PHI node is destroyed and any uses of it are replaced with
1876 /// dummy values. The only time there should be zero incoming values to a PHI
1877 /// node is when the block is dead, so this strategy is sound.
1879 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1881 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1882 int Idx = getBasicBlockIndex(BB);
1883 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1884 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1887 /// getBasicBlockIndex - Return the first index of the specified basic
1888 /// block in the value list for this PHI. Returns -1 if no instance.
1890 int getBasicBlockIndex(const BasicBlock *BB) const {
1891 Use *OL = OperandList;
1892 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1893 if (OL[i+1].get() == BB) return i/2;
1897 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1898 return getIncomingValue(getBasicBlockIndex(BB));
1901 /// hasConstantValue - If the specified PHI node always merges together the
1902 /// same value, return the value, otherwise return null.
1904 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1906 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1907 static inline bool classof(const PHINode *) { return true; }
1908 static inline bool classof(const Instruction *I) {
1909 return I->getOpcode() == Instruction::PHI;
1911 static inline bool classof(const Value *V) {
1912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1915 void resizeOperands(unsigned NumOperands);
1919 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1922 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1925 //===----------------------------------------------------------------------===//
1927 //===----------------------------------------------------------------------===//
1929 //===---------------------------------------------------------------------------
1930 /// ReturnInst - Return a value (possibly void), from a function. Execution
1931 /// does not continue in this function any longer.
1933 class ReturnInst : public TerminatorInst {
1934 ReturnInst(const ReturnInst &RI);
1937 // ReturnInst constructors:
1938 // ReturnInst() - 'ret void' instruction
1939 // ReturnInst( null) - 'ret void' instruction
1940 // ReturnInst(Value* X) - 'ret X' instruction
1941 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1942 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1943 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1944 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1946 // NOTE: If the Value* passed is of type void then the constructor behaves as
1947 // if it was passed NULL.
1948 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1949 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1950 explicit ReturnInst(BasicBlock *InsertAtEnd);
1952 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1953 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1955 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1956 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1958 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1959 return new(0) ReturnInst(InsertAtEnd);
1961 virtual ~ReturnInst();
1963 virtual ReturnInst *clone() const;
1965 /// Provide fast operand accessors
1966 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1968 /// Convenience accessor
1969 Value *getReturnValue(unsigned n = 0) const {
1970 return n < getNumOperands()
1975 unsigned getNumSuccessors() const { return 0; }
1977 // Methods for support type inquiry through isa, cast, and dyn_cast:
1978 static inline bool classof(const ReturnInst *) { return true; }
1979 static inline bool classof(const Instruction *I) {
1980 return (I->getOpcode() == Instruction::Ret);
1982 static inline bool classof(const Value *V) {
1983 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1986 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1987 virtual unsigned getNumSuccessorsV() const;
1988 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1992 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
1995 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1997 //===----------------------------------------------------------------------===//
1999 //===----------------------------------------------------------------------===//
2001 //===---------------------------------------------------------------------------
2002 /// BranchInst - Conditional or Unconditional Branch instruction.
2004 class BranchInst : public TerminatorInst {
2005 /// Ops list - Branches are strange. The operands are ordered:
2006 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2007 /// they don't have to check for cond/uncond branchness. These are mostly
2008 /// accessed relative from op_end().
2009 BranchInst(const BranchInst &BI);
2011 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2012 // BranchInst(BB *B) - 'br B'
2013 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2014 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2015 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2016 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2017 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2018 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2019 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2020 Instruction *InsertBefore = 0);
2021 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2022 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2023 BasicBlock *InsertAtEnd);
2025 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2026 return new(1, true) BranchInst(IfTrue, InsertBefore);
2028 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2029 Value *Cond, Instruction *InsertBefore = 0) {
2030 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2032 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2033 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2035 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2036 Value *Cond, BasicBlock *InsertAtEnd) {
2037 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2042 /// Transparently provide more efficient getOperand methods.
2043 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2045 virtual BranchInst *clone() const;
2047 bool isUnconditional() const { return getNumOperands() == 1; }
2048 bool isConditional() const { return getNumOperands() == 3; }
2050 Value *getCondition() const {
2051 assert(isConditional() && "Cannot get condition of an uncond branch!");
2055 void setCondition(Value *V) {
2056 assert(isConditional() && "Cannot set condition of unconditional branch!");
2060 // setUnconditionalDest - Change the current branch to an unconditional branch
2061 // targeting the specified block.
2062 // FIXME: Eliminate this ugly method.
2063 void setUnconditionalDest(BasicBlock *Dest) {
2065 if (isConditional()) { // Convert this to an uncond branch.
2069 OperandList = op_begin();
2073 unsigned getNumSuccessors() const { return 1+isConditional(); }
2075 BasicBlock *getSuccessor(unsigned i) const {
2076 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2077 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2080 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2081 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2082 *(&Op<-1>() - idx) = NewSucc;
2085 // Methods for support type inquiry through isa, cast, and dyn_cast:
2086 static inline bool classof(const BranchInst *) { return true; }
2087 static inline bool classof(const Instruction *I) {
2088 return (I->getOpcode() == Instruction::Br);
2090 static inline bool classof(const Value *V) {
2091 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2094 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2095 virtual unsigned getNumSuccessorsV() const;
2096 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2100 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2102 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2104 //===----------------------------------------------------------------------===//
2106 //===----------------------------------------------------------------------===//
2108 //===---------------------------------------------------------------------------
2109 /// SwitchInst - Multiway switch
2111 class SwitchInst : public TerminatorInst {
2112 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2113 unsigned ReservedSpace;
2114 // Operand[0] = Value to switch on
2115 // Operand[1] = Default basic block destination
2116 // Operand[2n ] = Value to match
2117 // Operand[2n+1] = BasicBlock to go to on match
2118 SwitchInst(const SwitchInst &RI);
2119 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2120 void resizeOperands(unsigned No);
2121 // allocate space for exactly zero operands
2122 void *operator new(size_t s) {
2123 return User::operator new(s, 0);
2125 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2126 /// switch on and a default destination. The number of additional cases can
2127 /// be specified here to make memory allocation more efficient. This
2128 /// constructor can also autoinsert before another instruction.
2129 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2130 Instruction *InsertBefore = 0);
2132 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2133 /// switch on and a default destination. The number of additional cases can
2134 /// be specified here to make memory allocation more efficient. This
2135 /// constructor also autoinserts at the end of the specified BasicBlock.
2136 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2137 BasicBlock *InsertAtEnd);
2139 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2140 unsigned NumCases, Instruction *InsertBefore = 0) {
2141 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2143 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2144 unsigned NumCases, BasicBlock *InsertAtEnd) {
2145 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2149 /// Provide fast operand accessors
2150 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2152 // Accessor Methods for Switch stmt
2153 Value *getCondition() const { return getOperand(0); }
2154 void setCondition(Value *V) { setOperand(0, V); }
2156 BasicBlock *getDefaultDest() const {
2157 return cast<BasicBlock>(getOperand(1));
2160 /// getNumCases - return the number of 'cases' in this switch instruction.
2161 /// Note that case #0 is always the default case.
2162 unsigned getNumCases() const {
2163 return getNumOperands()/2;
2166 /// getCaseValue - Return the specified case value. Note that case #0, the
2167 /// default destination, does not have a case value.
2168 ConstantInt *getCaseValue(unsigned i) {
2169 assert(i && i < getNumCases() && "Illegal case value to get!");
2170 return getSuccessorValue(i);
2173 /// getCaseValue - Return the specified case value. Note that case #0, the
2174 /// default destination, does not have a case value.
2175 const ConstantInt *getCaseValue(unsigned i) const {
2176 assert(i && i < getNumCases() && "Illegal case value to get!");
2177 return getSuccessorValue(i);
2180 /// findCaseValue - Search all of the case values for the specified constant.
2181 /// If it is explicitly handled, return the case number of it, otherwise
2182 /// return 0 to indicate that it is handled by the default handler.
2183 unsigned findCaseValue(const ConstantInt *C) const {
2184 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2185 if (getCaseValue(i) == C)
2190 /// findCaseDest - Finds the unique case value for a given successor. Returns
2191 /// null if the successor is not found, not unique, or is the default case.
2192 ConstantInt *findCaseDest(BasicBlock *BB) {
2193 if (BB == getDefaultDest()) return NULL;
2195 ConstantInt *CI = NULL;
2196 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2197 if (getSuccessor(i) == BB) {
2198 if (CI) return NULL; // Multiple cases lead to BB.
2199 else CI = getCaseValue(i);
2205 /// addCase - Add an entry to the switch instruction...
2207 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2209 /// removeCase - This method removes the specified successor from the switch
2210 /// instruction. Note that this cannot be used to remove the default
2211 /// destination (successor #0).
2213 void removeCase(unsigned idx);
2215 virtual SwitchInst *clone() const;
2217 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2218 BasicBlock *getSuccessor(unsigned idx) const {
2219 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2220 return cast<BasicBlock>(getOperand(idx*2+1));
2222 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2223 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2224 setOperand(idx*2+1, NewSucc);
2227 // getSuccessorValue - Return the value associated with the specified
2229 ConstantInt *getSuccessorValue(unsigned idx) const {
2230 assert(idx < getNumSuccessors() && "Successor # out of range!");
2231 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2234 // Methods for support type inquiry through isa, cast, and dyn_cast:
2235 static inline bool classof(const SwitchInst *) { return true; }
2236 static inline bool classof(const Instruction *I) {
2237 return I->getOpcode() == Instruction::Switch;
2239 static inline bool classof(const Value *V) {
2240 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2243 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2244 virtual unsigned getNumSuccessorsV() const;
2245 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2249 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2252 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2255 //===----------------------------------------------------------------------===//
2257 //===----------------------------------------------------------------------===//
2259 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2260 /// calling convention of the call.
2262 class InvokeInst : public TerminatorInst {
2263 AttrListPtr AttributeList;
2264 InvokeInst(const InvokeInst &BI);
2265 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2266 Value* const *Args, unsigned NumArgs);
2268 template<typename InputIterator>
2269 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2270 InputIterator ArgBegin, InputIterator ArgEnd,
2271 const std::string &NameStr,
2272 // This argument ensures that we have an iterator we can
2273 // do arithmetic on in constant time
2274 std::random_access_iterator_tag) {
2275 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2277 // This requires that the iterator points to contiguous memory.
2278 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2282 /// Construct an InvokeInst given a range of arguments.
2283 /// InputIterator must be a random-access iterator pointing to
2284 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2285 /// made for random-accessness but not for contiguous storage as
2286 /// that would incur runtime overhead.
2288 /// @brief Construct an InvokeInst from a range of arguments
2289 template<typename InputIterator>
2290 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2291 InputIterator ArgBegin, InputIterator ArgEnd,
2293 const std::string &NameStr, Instruction *InsertBefore);
2295 /// Construct an InvokeInst given a range of arguments.
2296 /// InputIterator must be a random-access iterator pointing to
2297 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2298 /// made for random-accessness but not for contiguous storage as
2299 /// that would incur runtime overhead.
2301 /// @brief Construct an InvokeInst from a range of arguments
2302 template<typename InputIterator>
2303 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2304 InputIterator ArgBegin, InputIterator ArgEnd,
2306 const std::string &NameStr, BasicBlock *InsertAtEnd);
2308 template<typename InputIterator>
2309 static InvokeInst *Create(Value *Func,
2310 BasicBlock *IfNormal, BasicBlock *IfException,
2311 InputIterator ArgBegin, InputIterator ArgEnd,
2312 const std::string &NameStr = "",
2313 Instruction *InsertBefore = 0) {
2314 unsigned Values(ArgEnd - ArgBegin + 3);
2315 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2316 Values, NameStr, InsertBefore);
2318 template<typename InputIterator>
2319 static InvokeInst *Create(Value *Func,
2320 BasicBlock *IfNormal, BasicBlock *IfException,
2321 InputIterator ArgBegin, InputIterator ArgEnd,
2322 const std::string &NameStr,
2323 BasicBlock *InsertAtEnd) {
2324 unsigned Values(ArgEnd - ArgBegin + 3);
2325 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2326 Values, NameStr, InsertAtEnd);
2329 virtual InvokeInst *clone() const;
2331 /// Provide fast operand accessors
2332 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2334 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2336 unsigned getCallingConv() const { return SubclassData; }
2337 void setCallingConv(unsigned CC) {
2341 /// getAttributes - Return the parameter attributes for this invoke.
2343 const AttrListPtr &getAttributes() const { return AttributeList; }
2345 /// setAttributes - Set the parameter attributes for this invoke.
2347 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2349 /// addAttribute - adds the attribute to the list of attributes.
2350 void addAttribute(unsigned i, Attributes attr);
2352 /// removeAttribute - removes the attribute from the list of attributes.
2353 void removeAttribute(unsigned i, Attributes attr);
2355 /// @brief Determine whether the call or the callee has the given attribute.
2356 bool paramHasAttr(unsigned i, Attributes attr) const;
2358 /// @brief Extract the alignment for a call or parameter (0=unknown).
2359 unsigned getParamAlignment(unsigned i) const {
2360 return AttributeList.getParamAlignment(i);
2363 /// @brief Determine if the call does not access memory.
2364 bool doesNotAccessMemory() const {
2365 return paramHasAttr(0, Attribute::ReadNone);
2367 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2368 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2369 else removeAttribute(~0, Attribute::ReadNone);
2372 /// @brief Determine if the call does not access or only reads memory.
2373 bool onlyReadsMemory() const {
2374 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2376 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2377 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2378 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2381 /// @brief Determine if the call cannot return.
2382 bool doesNotReturn() const {
2383 return paramHasAttr(~0, Attribute::NoReturn);
2385 void setDoesNotReturn(bool DoesNotReturn = true) {
2386 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2387 else removeAttribute(~0, Attribute::NoReturn);
2390 /// @brief Determine if the call cannot unwind.
2391 bool doesNotThrow() const {
2392 return paramHasAttr(~0, Attribute::NoUnwind);
2394 void setDoesNotThrow(bool DoesNotThrow = true) {
2395 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2396 else removeAttribute(~0, Attribute::NoUnwind);
2399 /// @brief Determine if the call returns a structure through first
2400 /// pointer argument.
2401 bool hasStructRetAttr() const {
2402 // Be friendly and also check the callee.
2403 return paramHasAttr(1, Attribute::StructRet);
2406 /// @brief Determine if any call argument is an aggregate passed by value.
2407 bool hasByValArgument() const {
2408 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2411 /// getCalledFunction - Return the function called, or null if this is an
2412 /// indirect function invocation.
2414 Function *getCalledFunction() const {
2415 return dyn_cast<Function>(getOperand(0));
2418 /// getCalledValue - Get a pointer to the function that is invoked by this
2420 const Value *getCalledValue() const { return getOperand(0); }
2421 Value *getCalledValue() { return getOperand(0); }
2423 // get*Dest - Return the destination basic blocks...
2424 BasicBlock *getNormalDest() const {
2425 return cast<BasicBlock>(getOperand(1));
2427 BasicBlock *getUnwindDest() const {
2428 return cast<BasicBlock>(getOperand(2));
2430 void setNormalDest(BasicBlock *B) {
2434 void setUnwindDest(BasicBlock *B) {
2438 BasicBlock *getSuccessor(unsigned i) const {
2439 assert(i < 2 && "Successor # out of range for invoke!");
2440 return i == 0 ? getNormalDest() : getUnwindDest();
2443 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2444 assert(idx < 2 && "Successor # out of range for invoke!");
2445 setOperand(idx+1, NewSucc);
2448 unsigned getNumSuccessors() const { return 2; }
2450 // Methods for support type inquiry through isa, cast, and dyn_cast:
2451 static inline bool classof(const InvokeInst *) { return true; }
2452 static inline bool classof(const Instruction *I) {
2453 return (I->getOpcode() == Instruction::Invoke);
2455 static inline bool classof(const Value *V) {
2456 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2459 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2460 virtual unsigned getNumSuccessorsV() const;
2461 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2465 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2468 template<typename InputIterator>
2469 InvokeInst::InvokeInst(Value *Func,
2470 BasicBlock *IfNormal, BasicBlock *IfException,
2471 InputIterator ArgBegin, InputIterator ArgEnd,
2473 const std::string &NameStr, Instruction *InsertBefore)
2474 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2475 ->getElementType())->getReturnType(),
2476 Instruction::Invoke,
2477 OperandTraits<InvokeInst>::op_end(this) - Values,
2478 Values, InsertBefore) {
2479 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2480 typename std::iterator_traits<InputIterator>::iterator_category());
2482 template<typename InputIterator>
2483 InvokeInst::InvokeInst(Value *Func,
2484 BasicBlock *IfNormal, BasicBlock *IfException,
2485 InputIterator ArgBegin, InputIterator ArgEnd,
2487 const std::string &NameStr, BasicBlock *InsertAtEnd)
2488 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2489 ->getElementType())->getReturnType(),
2490 Instruction::Invoke,
2491 OperandTraits<InvokeInst>::op_end(this) - Values,
2492 Values, InsertAtEnd) {
2493 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2494 typename std::iterator_traits<InputIterator>::iterator_category());
2497 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2499 //===----------------------------------------------------------------------===//
2501 //===----------------------------------------------------------------------===//
2503 //===---------------------------------------------------------------------------
2504 /// UnwindInst - Immediately exit the current function, unwinding the stack
2505 /// until an invoke instruction is found.
2507 class UnwindInst : public TerminatorInst {
2508 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2510 // allocate space for exactly zero operands
2511 void *operator new(size_t s) {
2512 return User::operator new(s, 0);
2514 explicit UnwindInst(Instruction *InsertBefore = 0);
2515 explicit UnwindInst(BasicBlock *InsertAtEnd);
2517 virtual UnwindInst *clone() const;
2519 unsigned getNumSuccessors() const { return 0; }
2521 // Methods for support type inquiry through isa, cast, and dyn_cast:
2522 static inline bool classof(const UnwindInst *) { return true; }
2523 static inline bool classof(const Instruction *I) {
2524 return I->getOpcode() == Instruction::Unwind;
2526 static inline bool classof(const Value *V) {
2527 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2530 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2531 virtual unsigned getNumSuccessorsV() const;
2532 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2535 //===----------------------------------------------------------------------===//
2536 // UnreachableInst Class
2537 //===----------------------------------------------------------------------===//
2539 //===---------------------------------------------------------------------------
2540 /// UnreachableInst - This function has undefined behavior. In particular, the
2541 /// presence of this instruction indicates some higher level knowledge that the
2542 /// end of the block cannot be reached.
2544 class UnreachableInst : public TerminatorInst {
2545 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2547 // allocate space for exactly zero operands
2548 void *operator new(size_t s) {
2549 return User::operator new(s, 0);
2551 explicit UnreachableInst(Instruction *InsertBefore = 0);
2552 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2554 virtual UnreachableInst *clone() const;
2556 unsigned getNumSuccessors() const { return 0; }
2558 // Methods for support type inquiry through isa, cast, and dyn_cast:
2559 static inline bool classof(const UnreachableInst *) { return true; }
2560 static inline bool classof(const Instruction *I) {
2561 return I->getOpcode() == Instruction::Unreachable;
2563 static inline bool classof(const Value *V) {
2564 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2567 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2568 virtual unsigned getNumSuccessorsV() const;
2569 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2572 //===----------------------------------------------------------------------===//
2574 //===----------------------------------------------------------------------===//
2576 /// @brief This class represents a truncation of integer types.
2577 class TruncInst : public CastInst {
2578 /// Private copy constructor
2579 TruncInst(const TruncInst &CI)
2580 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2583 /// @brief Constructor with insert-before-instruction semantics
2585 Value *S, ///< The value to be truncated
2586 const Type *Ty, ///< The (smaller) type to truncate to
2587 const std::string &NameStr = "", ///< A name for the new instruction
2588 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2591 /// @brief Constructor with insert-at-end-of-block semantics
2593 Value *S, ///< The value to be truncated
2594 const Type *Ty, ///< The (smaller) type to truncate to
2595 const std::string &NameStr, ///< A name for the new instruction
2596 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2599 /// @brief Clone an identical TruncInst
2600 virtual CastInst *clone() const;
2602 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2603 static inline bool classof(const TruncInst *) { return true; }
2604 static inline bool classof(const Instruction *I) {
2605 return I->getOpcode() == Trunc;
2607 static inline bool classof(const Value *V) {
2608 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2612 //===----------------------------------------------------------------------===//
2614 //===----------------------------------------------------------------------===//
2616 /// @brief This class represents zero extension of integer types.
2617 class ZExtInst : public CastInst {
2618 /// @brief Private copy constructor
2619 ZExtInst(const ZExtInst &CI)
2620 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2623 /// @brief Constructor with insert-before-instruction semantics
2625 Value *S, ///< The value to be zero extended
2626 const Type *Ty, ///< The type to zero extend to
2627 const std::string &NameStr = "", ///< A name for the new instruction
2628 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2631 /// @brief Constructor with insert-at-end semantics.
2633 Value *S, ///< The value to be zero extended
2634 const Type *Ty, ///< The type to zero extend to
2635 const std::string &NameStr, ///< A name for the new instruction
2636 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2639 /// @brief Clone an identical ZExtInst
2640 virtual CastInst *clone() const;
2642 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2643 static inline bool classof(const ZExtInst *) { return true; }
2644 static inline bool classof(const Instruction *I) {
2645 return I->getOpcode() == ZExt;
2647 static inline bool classof(const Value *V) {
2648 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2652 //===----------------------------------------------------------------------===//
2654 //===----------------------------------------------------------------------===//
2656 /// @brief This class represents a sign extension of integer types.
2657 class SExtInst : public CastInst {
2658 /// @brief Private copy constructor
2659 SExtInst(const SExtInst &CI)
2660 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2663 /// @brief Constructor with insert-before-instruction semantics
2665 Value *S, ///< The value to be sign extended
2666 const Type *Ty, ///< The type to sign extend to
2667 const std::string &NameStr = "", ///< A name for the new instruction
2668 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2671 /// @brief Constructor with insert-at-end-of-block semantics
2673 Value *S, ///< The value to be sign extended
2674 const Type *Ty, ///< The type to sign extend to
2675 const std::string &NameStr, ///< A name for the new instruction
2676 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2679 /// @brief Clone an identical SExtInst
2680 virtual CastInst *clone() const;
2682 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2683 static inline bool classof(const SExtInst *) { return true; }
2684 static inline bool classof(const Instruction *I) {
2685 return I->getOpcode() == SExt;
2687 static inline bool classof(const Value *V) {
2688 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2692 //===----------------------------------------------------------------------===//
2693 // FPTruncInst Class
2694 //===----------------------------------------------------------------------===//
2696 /// @brief This class represents a truncation of floating point types.
2697 class FPTruncInst : public CastInst {
2698 FPTruncInst(const FPTruncInst &CI)
2699 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2702 /// @brief Constructor with insert-before-instruction semantics
2704 Value *S, ///< The value to be truncated
2705 const Type *Ty, ///< The type to truncate to
2706 const std::string &NameStr = "", ///< A name for the new instruction
2707 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2710 /// @brief Constructor with insert-before-instruction semantics
2712 Value *S, ///< The value to be truncated
2713 const Type *Ty, ///< The type to truncate to
2714 const std::string &NameStr, ///< A name for the new instruction
2715 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2718 /// @brief Clone an identical FPTruncInst
2719 virtual CastInst *clone() const;
2721 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2722 static inline bool classof(const FPTruncInst *) { return true; }
2723 static inline bool classof(const Instruction *I) {
2724 return I->getOpcode() == FPTrunc;
2726 static inline bool classof(const Value *V) {
2727 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2731 //===----------------------------------------------------------------------===//
2733 //===----------------------------------------------------------------------===//
2735 /// @brief This class represents an extension of floating point types.
2736 class FPExtInst : public CastInst {
2737 FPExtInst(const FPExtInst &CI)
2738 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2741 /// @brief Constructor with insert-before-instruction semantics
2743 Value *S, ///< The value to be extended
2744 const Type *Ty, ///< The type to extend to
2745 const std::string &NameStr = "", ///< A name for the new instruction
2746 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2749 /// @brief Constructor with insert-at-end-of-block semantics
2751 Value *S, ///< The value to be extended
2752 const Type *Ty, ///< The type to extend to
2753 const std::string &NameStr, ///< A name for the new instruction
2754 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2757 /// @brief Clone an identical FPExtInst
2758 virtual CastInst *clone() const;
2760 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2761 static inline bool classof(const FPExtInst *) { return true; }
2762 static inline bool classof(const Instruction *I) {
2763 return I->getOpcode() == FPExt;
2765 static inline bool classof(const Value *V) {
2766 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2770 //===----------------------------------------------------------------------===//
2772 //===----------------------------------------------------------------------===//
2774 /// @brief This class represents a cast unsigned integer to floating point.
2775 class UIToFPInst : public CastInst {
2776 UIToFPInst(const UIToFPInst &CI)
2777 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2780 /// @brief Constructor with insert-before-instruction semantics
2782 Value *S, ///< The value to be converted
2783 const Type *Ty, ///< The type to convert to
2784 const std::string &NameStr = "", ///< A name for the new instruction
2785 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2788 /// @brief Constructor with insert-at-end-of-block semantics
2790 Value *S, ///< The value to be converted
2791 const Type *Ty, ///< The type to convert to
2792 const std::string &NameStr, ///< A name for the new instruction
2793 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2796 /// @brief Clone an identical UIToFPInst
2797 virtual CastInst *clone() const;
2799 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2800 static inline bool classof(const UIToFPInst *) { return true; }
2801 static inline bool classof(const Instruction *I) {
2802 return I->getOpcode() == UIToFP;
2804 static inline bool classof(const Value *V) {
2805 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2809 //===----------------------------------------------------------------------===//
2811 //===----------------------------------------------------------------------===//
2813 /// @brief This class represents a cast from signed integer to floating point.
2814 class SIToFPInst : public CastInst {
2815 SIToFPInst(const SIToFPInst &CI)
2816 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2819 /// @brief Constructor with insert-before-instruction semantics
2821 Value *S, ///< The value to be converted
2822 const Type *Ty, ///< The type to convert to
2823 const std::string &NameStr = "", ///< A name for the new instruction
2824 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2827 /// @brief Constructor with insert-at-end-of-block semantics
2829 Value *S, ///< The value to be converted
2830 const Type *Ty, ///< The type to convert to
2831 const std::string &NameStr, ///< A name for the new instruction
2832 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2835 /// @brief Clone an identical SIToFPInst
2836 virtual CastInst *clone() const;
2838 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2839 static inline bool classof(const SIToFPInst *) { return true; }
2840 static inline bool classof(const Instruction *I) {
2841 return I->getOpcode() == SIToFP;
2843 static inline bool classof(const Value *V) {
2844 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2848 //===----------------------------------------------------------------------===//
2850 //===----------------------------------------------------------------------===//
2852 /// @brief This class represents a cast from floating point to unsigned integer
2853 class FPToUIInst : public CastInst {
2854 FPToUIInst(const FPToUIInst &CI)
2855 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2858 /// @brief Constructor with insert-before-instruction 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 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2866 /// @brief Constructor with insert-at-end-of-block semantics
2868 Value *S, ///< The value to be converted
2869 const Type *Ty, ///< The type to convert to
2870 const std::string &NameStr, ///< A name for the new instruction
2871 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2874 /// @brief Clone an identical FPToUIInst
2875 virtual CastInst *clone() const;
2877 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2878 static inline bool classof(const FPToUIInst *) { return true; }
2879 static inline bool classof(const Instruction *I) {
2880 return I->getOpcode() == FPToUI;
2882 static inline bool classof(const Value *V) {
2883 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2887 //===----------------------------------------------------------------------===//
2889 //===----------------------------------------------------------------------===//
2891 /// @brief This class represents a cast from floating point to signed integer.
2892 class FPToSIInst : public CastInst {
2893 FPToSIInst(const FPToSIInst &CI)
2894 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2897 /// @brief Constructor with insert-before-instruction 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 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2905 /// @brief Constructor with insert-at-end-of-block semantics
2907 Value *S, ///< The value to be converted
2908 const Type *Ty, ///< The type to convert to
2909 const std::string &NameStr, ///< A name for the new instruction
2910 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2913 /// @brief Clone an identical FPToSIInst
2914 virtual CastInst *clone() const;
2916 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2917 static inline bool classof(const FPToSIInst *) { return true; }
2918 static inline bool classof(const Instruction *I) {
2919 return I->getOpcode() == FPToSI;
2921 static inline bool classof(const Value *V) {
2922 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2926 //===----------------------------------------------------------------------===//
2927 // IntToPtrInst Class
2928 //===----------------------------------------------------------------------===//
2930 /// @brief This class represents a cast from an integer to a pointer.
2931 class IntToPtrInst : public CastInst {
2932 IntToPtrInst(const IntToPtrInst &CI)
2933 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2936 /// @brief Constructor with insert-before-instruction 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 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2944 /// @brief Constructor with insert-at-end-of-block semantics
2946 Value *S, ///< The value to be converted
2947 const Type *Ty, ///< The type to convert to
2948 const std::string &NameStr, ///< A name for the new instruction
2949 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2952 /// @brief Clone an identical IntToPtrInst
2953 virtual CastInst *clone() const;
2955 // Methods for support type inquiry through isa, cast, and dyn_cast:
2956 static inline bool classof(const IntToPtrInst *) { return true; }
2957 static inline bool classof(const Instruction *I) {
2958 return I->getOpcode() == IntToPtr;
2960 static inline bool classof(const Value *V) {
2961 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2965 //===----------------------------------------------------------------------===//
2966 // PtrToIntInst Class
2967 //===----------------------------------------------------------------------===//
2969 /// @brief This class represents a cast from a pointer to an integer
2970 class PtrToIntInst : public CastInst {
2971 PtrToIntInst(const PtrToIntInst &CI)
2972 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
2975 /// @brief Constructor with insert-before-instruction 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 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2983 /// @brief Constructor with insert-at-end-of-block semantics
2985 Value *S, ///< The value to be converted
2986 const Type *Ty, ///< The type to convert to
2987 const std::string &NameStr, ///< A name for the new instruction
2988 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2991 /// @brief Clone an identical PtrToIntInst
2992 virtual CastInst *clone() const;
2994 // Methods for support type inquiry through isa, cast, and dyn_cast:
2995 static inline bool classof(const PtrToIntInst *) { return true; }
2996 static inline bool classof(const Instruction *I) {
2997 return I->getOpcode() == PtrToInt;
2999 static inline bool classof(const Value *V) {
3000 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3004 //===----------------------------------------------------------------------===//
3005 // BitCastInst Class
3006 //===----------------------------------------------------------------------===//
3008 /// @brief This class represents a no-op cast from one type to another.
3009 class BitCastInst : public CastInst {
3010 BitCastInst(const BitCastInst &CI)
3011 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3014 /// @brief Constructor with insert-before-instruction semantics
3016 Value *S, ///< The value to be casted
3017 const Type *Ty, ///< The type to casted to
3018 const std::string &NameStr = "", ///< A name for the new instruction
3019 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3022 /// @brief Constructor with insert-at-end-of-block semantics
3024 Value *S, ///< The value to be casted
3025 const Type *Ty, ///< The type to casted to
3026 const std::string &NameStr, ///< A name for the new instruction
3027 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3030 /// @brief Clone an identical BitCastInst
3031 virtual CastInst *clone() const;
3033 // Methods for support type inquiry through isa, cast, and dyn_cast:
3034 static inline bool classof(const BitCastInst *) { return true; }
3035 static inline bool classof(const Instruction *I) {
3036 return I->getOpcode() == BitCast;
3038 static inline bool classof(const Value *V) {
3039 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3043 } // End llvm namespace