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/LLVMContext.h"
24 #include "llvm/ADT/SmallVector.h"
33 //===----------------------------------------------------------------------===//
34 // AllocationInst Class
35 //===----------------------------------------------------------------------===//
37 /// AllocationInst - This class is the common base class of MallocInst and
40 class AllocationInst : public UnaryInstruction {
42 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
43 const std::string &Name = "", Instruction *InsertBefore = 0);
44 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
45 const std::string &Name, BasicBlock *InsertAtEnd);
47 // Out of line virtual method, so the vtable, etc. has a home.
48 virtual ~AllocationInst();
50 /// isArrayAllocation - Return true if there is an allocation size parameter
51 /// to the allocation instruction that is not 1.
53 bool isArrayAllocation() const;
55 /// getArraySize - Get the number of elements allocated. For a simple
56 /// allocation of a single element, this will return a constant 1 value.
58 const Value *getArraySize() const { return getOperand(0); }
59 Value *getArraySize() { return getOperand(0); }
61 /// getType - Overload to return most specific pointer type
63 const PointerType *getType() const {
64 return reinterpret_cast<const PointerType*>(Instruction::getType());
67 /// getAllocatedType - Return the type that is being allocated by the
70 const Type *getAllocatedType() const;
72 /// getAlignment - Return the alignment of the memory that is being allocated
73 /// by the instruction.
75 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
76 void setAlignment(unsigned Align);
78 virtual Instruction *clone(LLVMContext &Context) const = 0;
80 // Methods for support type inquiry through isa, cast, and dyn_cast:
81 static inline bool classof(const AllocationInst *) { return true; }
82 static inline bool classof(const Instruction *I) {
83 return I->getOpcode() == Instruction::Alloca ||
84 I->getOpcode() == Instruction::Malloc;
86 static inline bool classof(const Value *V) {
87 return isa<Instruction>(V) && classof(cast<Instruction>(V));
92 //===----------------------------------------------------------------------===//
94 //===----------------------------------------------------------------------===//
96 /// MallocInst - an instruction to allocated memory on the heap
98 class MallocInst : public AllocationInst {
99 MallocInst(const MallocInst &MI);
101 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
102 const std::string &NameStr = "",
103 Instruction *InsertBefore = 0)
104 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
105 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
106 BasicBlock *InsertAtEnd)
107 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
109 MallocInst(const Type *Ty, const std::string &NameStr,
110 Instruction *InsertBefore = 0)
111 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
112 MallocInst(const Type *Ty, const std::string &NameStr,
113 BasicBlock *InsertAtEnd)
114 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
116 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
117 const std::string &NameStr, BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
119 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
120 const std::string &NameStr = "",
121 Instruction *InsertBefore = 0)
122 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
124 virtual MallocInst *clone(LLVMContext &Context) const;
126 // Methods for support type inquiry through isa, cast, and dyn_cast:
127 static inline bool classof(const MallocInst *) { return true; }
128 static inline bool classof(const Instruction *I) {
129 return (I->getOpcode() == Instruction::Malloc);
131 static inline bool classof(const Value *V) {
132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
137 //===----------------------------------------------------------------------===//
139 //===----------------------------------------------------------------------===//
141 /// AllocaInst - an instruction to allocate memory on the stack
143 class AllocaInst : public AllocationInst {
144 AllocaInst(const AllocaInst &);
146 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
147 const std::string &NameStr = "",
148 Instruction *InsertBefore = 0)
149 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
150 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
151 BasicBlock *InsertAtEnd)
152 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
154 AllocaInst(const Type *Ty, const std::string &NameStr,
155 Instruction *InsertBefore = 0)
156 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
157 AllocaInst(const Type *Ty, const std::string &NameStr,
158 BasicBlock *InsertAtEnd)
159 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
161 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
162 const std::string &NameStr = "", Instruction *InsertBefore = 0)
163 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
164 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
165 const std::string &NameStr, BasicBlock *InsertAtEnd)
166 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
168 virtual AllocaInst *clone(LLVMContext &Context) const;
170 /// isStaticAlloca - Return true if this alloca is in the entry block of the
171 /// function and is a constant size. If so, the code generator will fold it
172 /// into the prolog/epilog code, so it is basically free.
173 bool isStaticAlloca() const;
175 // Methods for support type inquiry through isa, cast, and dyn_cast:
176 static inline bool classof(const AllocaInst *) { return true; }
177 static inline bool classof(const Instruction *I) {
178 return (I->getOpcode() == Instruction::Alloca);
180 static inline bool classof(const Value *V) {
181 return isa<Instruction>(V) && classof(cast<Instruction>(V));
186 //===----------------------------------------------------------------------===//
188 //===----------------------------------------------------------------------===//
190 /// FreeInst - an instruction to deallocate memory
192 class FreeInst : public UnaryInstruction {
195 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
196 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
198 virtual FreeInst *clone(LLVMContext &Context) const;
200 // Accessor methods for consistency with other memory operations
201 Value *getPointerOperand() { return getOperand(0); }
202 const Value *getPointerOperand() const { return getOperand(0); }
204 // Methods for support type inquiry through isa, cast, and dyn_cast:
205 static inline bool classof(const FreeInst *) { return true; }
206 static inline bool classof(const Instruction *I) {
207 return (I->getOpcode() == Instruction::Free);
209 static inline bool classof(const Value *V) {
210 return isa<Instruction>(V) && classof(cast<Instruction>(V));
215 //===----------------------------------------------------------------------===//
217 //===----------------------------------------------------------------------===//
219 /// LoadInst - an instruction for reading from memory. This uses the
220 /// SubclassData field in Value to store whether or not the load is volatile.
222 class LoadInst : public UnaryInstruction {
224 LoadInst(const LoadInst &LI)
225 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
226 setVolatile(LI.isVolatile());
227 setAlignment(LI.getAlignment());
235 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
236 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
237 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
238 Instruction *InsertBefore = 0);
239 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
240 unsigned Align, Instruction *InsertBefore = 0);
241 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
242 BasicBlock *InsertAtEnd);
243 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
244 unsigned Align, BasicBlock *InsertAtEnd);
246 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
247 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
248 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
249 bool isVolatile = false, Instruction *InsertBefore = 0);
250 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
251 BasicBlock *InsertAtEnd);
253 /// isVolatile - Return true if this is a load from a volatile memory
256 bool isVolatile() const { return SubclassData & 1; }
258 /// setVolatile - Specify whether this is a volatile load or not.
260 void setVolatile(bool V) {
261 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
264 virtual LoadInst *clone(LLVMContext &Context) const;
266 /// getAlignment - Return the alignment of the access that is being performed
268 unsigned getAlignment() const {
269 return (1 << (SubclassData>>1)) >> 1;
272 void setAlignment(unsigned Align);
274 Value *getPointerOperand() { return getOperand(0); }
275 const Value *getPointerOperand() const { return getOperand(0); }
276 static unsigned getPointerOperandIndex() { return 0U; }
278 // Methods for support type inquiry through isa, cast, and dyn_cast:
279 static inline bool classof(const LoadInst *) { return true; }
280 static inline bool classof(const Instruction *I) {
281 return I->getOpcode() == Instruction::Load;
283 static inline bool classof(const Value *V) {
284 return isa<Instruction>(V) && classof(cast<Instruction>(V));
289 //===----------------------------------------------------------------------===//
291 //===----------------------------------------------------------------------===//
293 /// StoreInst - an instruction for storing to memory
295 class StoreInst : public Instruction {
296 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
298 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
300 Op<0>() = SI.Op<0>();
301 Op<1>() = SI.Op<1>();
302 setVolatile(SI.isVolatile());
303 setAlignment(SI.getAlignment());
311 // allocate space for exactly two operands
312 void *operator new(size_t s) {
313 return User::operator new(s, 2);
315 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
316 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
318 Instruction *InsertBefore = 0);
319 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
320 unsigned Align, Instruction *InsertBefore = 0);
321 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
322 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
323 unsigned Align, BasicBlock *InsertAtEnd);
326 /// isVolatile - Return true if this is a load from a volatile memory
329 bool isVolatile() const { return SubclassData & 1; }
331 /// setVolatile - Specify whether this is a volatile load or not.
333 void setVolatile(bool V) {
334 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
337 /// Transparently provide more efficient getOperand methods.
338 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
340 /// getAlignment - Return the alignment of the access that is being performed
342 unsigned getAlignment() const {
343 return (1 << (SubclassData>>1)) >> 1;
346 void setAlignment(unsigned Align);
348 virtual StoreInst *clone(LLVMContext &Context) const;
350 Value *getPointerOperand() { return getOperand(1); }
351 const Value *getPointerOperand() const { return getOperand(1); }
352 static unsigned getPointerOperandIndex() { return 1U; }
354 // Methods for support type inquiry through isa, cast, and dyn_cast:
355 static inline bool classof(const StoreInst *) { return true; }
356 static inline bool classof(const Instruction *I) {
357 return I->getOpcode() == Instruction::Store;
359 static inline bool classof(const Value *V) {
360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
365 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
368 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
370 //===----------------------------------------------------------------------===//
371 // GetElementPtrInst Class
372 //===----------------------------------------------------------------------===//
374 // checkType - Simple wrapper function to give a better assertion failure
375 // message on bad indexes for a gep instruction.
377 static inline const Type *checkType(const Type *Ty) {
378 assert(Ty && "Invalid GetElementPtrInst indices for type!");
382 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
383 /// access elements of arrays and structs
385 class GetElementPtrInst : public Instruction {
386 GetElementPtrInst(const GetElementPtrInst &GEPI);
387 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
388 const std::string &NameStr);
389 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
391 template<typename InputIterator>
392 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
393 const std::string &NameStr,
394 // This argument ensures that we have an iterator we can
395 // do arithmetic on in constant time
396 std::random_access_iterator_tag) {
397 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
400 // This requires that the iterator points to contiguous memory.
401 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
402 // we have to build an array here
405 init(Ptr, 0, NumIdx, NameStr);
409 /// getIndexedType - Returns the type of the element that would be loaded with
410 /// a load instruction with the specified parameters.
412 /// Null is returned if the indices are invalid for the specified
415 template<typename InputIterator>
416 static const Type *getIndexedType(const Type *Ptr,
417 InputIterator IdxBegin,
418 InputIterator IdxEnd,
419 // This argument ensures that we
420 // have an iterator we can do
421 // arithmetic on in constant time
422 std::random_access_iterator_tag) {
423 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
426 // This requires that the iterator points to contiguous memory.
427 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
429 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
432 /// Constructors - Create a getelementptr instruction with a base pointer an
433 /// list of indices. The first ctor can optionally insert before an existing
434 /// instruction, the second appends the new instruction to the specified
436 template<typename InputIterator>
437 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
438 InputIterator IdxEnd,
440 const std::string &NameStr,
441 Instruction *InsertBefore);
442 template<typename InputIterator>
443 inline GetElementPtrInst(Value *Ptr,
444 InputIterator IdxBegin, InputIterator IdxEnd,
446 const std::string &NameStr, BasicBlock *InsertAtEnd);
448 /// Constructors - These two constructors are convenience methods because one
449 /// and two index getelementptr instructions are so common.
450 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
451 Instruction *InsertBefore = 0);
452 GetElementPtrInst(Value *Ptr, Value *Idx,
453 const std::string &NameStr, BasicBlock *InsertAtEnd);
455 template<typename InputIterator>
456 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
457 InputIterator IdxEnd,
458 const std::string &NameStr = "",
459 Instruction *InsertBefore = 0) {
460 typename std::iterator_traits<InputIterator>::difference_type Values =
461 1 + std::distance(IdxBegin, IdxEnd);
463 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
465 template<typename InputIterator>
466 static GetElementPtrInst *Create(Value *Ptr,
467 InputIterator IdxBegin, InputIterator IdxEnd,
468 const std::string &NameStr,
469 BasicBlock *InsertAtEnd) {
470 typename std::iterator_traits<InputIterator>::difference_type Values =
471 1 + std::distance(IdxBegin, IdxEnd);
473 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
476 /// Constructors - These two creators are convenience methods because one
477 /// index getelementptr instructions are so common.
478 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
479 const std::string &NameStr = "",
480 Instruction *InsertBefore = 0) {
481 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
483 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
484 const std::string &NameStr,
485 BasicBlock *InsertAtEnd) {
486 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
489 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
491 /// Transparently provide more efficient getOperand methods.
492 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
494 // getType - Overload to return most specific pointer type...
495 const PointerType *getType() const {
496 return reinterpret_cast<const PointerType*>(Instruction::getType());
499 /// getIndexedType - Returns the type of the element that would be loaded with
500 /// a load instruction with the specified parameters.
502 /// Null is returned if the indices are invalid for the specified
505 template<typename InputIterator>
506 static const Type *getIndexedType(const Type *Ptr,
507 InputIterator IdxBegin,
508 InputIterator IdxEnd) {
509 return getIndexedType(Ptr, IdxBegin, IdxEnd,
510 typename std::iterator_traits<InputIterator>::
511 iterator_category());
514 static const Type *getIndexedType(const Type *Ptr,
515 Value* const *Idx, unsigned NumIdx);
517 static const Type *getIndexedType(const Type *Ptr,
518 uint64_t const *Idx, unsigned NumIdx);
520 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
522 inline op_iterator idx_begin() { return op_begin()+1; }
523 inline const_op_iterator idx_begin() const { return op_begin()+1; }
524 inline op_iterator idx_end() { return op_end(); }
525 inline const_op_iterator idx_end() const { return op_end(); }
527 Value *getPointerOperand() {
528 return getOperand(0);
530 const Value *getPointerOperand() const {
531 return getOperand(0);
533 static unsigned getPointerOperandIndex() {
534 return 0U; // get index for modifying correct operand
537 /// getPointerOperandType - Method to return the pointer operand as a
539 const PointerType *getPointerOperandType() const {
540 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
544 unsigned getNumIndices() const { // Note: always non-negative
545 return getNumOperands() - 1;
548 bool hasIndices() const {
549 return getNumOperands() > 1;
552 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
553 /// zeros. If so, the result pointer and the first operand have the same
554 /// value, just potentially different types.
555 bool hasAllZeroIndices() const;
557 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
558 /// constant integers. If so, the result pointer and the first operand have
559 /// a constant offset between them.
560 bool hasAllConstantIndices() const;
563 // Methods for support type inquiry through isa, cast, and dyn_cast:
564 static inline bool classof(const GetElementPtrInst *) { return true; }
565 static inline bool classof(const Instruction *I) {
566 return (I->getOpcode() == Instruction::GetElementPtr);
568 static inline bool classof(const Value *V) {
569 return isa<Instruction>(V) && classof(cast<Instruction>(V));
574 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
577 template<typename InputIterator>
578 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
579 InputIterator IdxBegin,
580 InputIterator IdxEnd,
582 const std::string &NameStr,
583 Instruction *InsertBefore)
584 : Instruction(PointerType::get(checkType(
585 getIndexedType(Ptr->getType(),
587 cast<PointerType>(Ptr->getType())
588 ->getAddressSpace()),
590 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
591 Values, InsertBefore) {
592 init(Ptr, IdxBegin, IdxEnd, NameStr,
593 typename std::iterator_traits<InputIterator>::iterator_category());
595 template<typename InputIterator>
596 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
597 InputIterator IdxBegin,
598 InputIterator IdxEnd,
600 const std::string &NameStr,
601 BasicBlock *InsertAtEnd)
602 : Instruction(PointerType::get(checkType(
603 getIndexedType(Ptr->getType(),
605 cast<PointerType>(Ptr->getType())
606 ->getAddressSpace()),
608 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
609 Values, InsertAtEnd) {
610 init(Ptr, IdxBegin, IdxEnd, NameStr,
611 typename std::iterator_traits<InputIterator>::iterator_category());
615 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
618 //===----------------------------------------------------------------------===//
620 //===----------------------------------------------------------------------===//
622 /// This instruction compares its operands according to the predicate given
623 /// to the constructor. It only operates on integers or pointers. The operands
624 /// must be identical types.
625 /// @brief Represent an integer comparison operator.
626 class ICmpInst: public CmpInst {
628 /// @brief Constructor with insert-before-instruction semantics.
630 Instruction *InsertBefore, ///< Where to insert
631 Predicate pred, ///< The predicate to use for the comparison
632 Value *LHS, ///< The left-hand-side of the expression
633 Value *RHS, ///< The right-hand-side of the expression
634 const std::string &NameStr = "" ///< Name of the instruction
635 ) : CmpInst(InsertBefore->getParent()->getContext()->
636 makeCmpResultType(LHS->getType()),
637 Instruction::ICmp, pred, LHS, RHS, NameStr,
639 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
640 pred <= CmpInst::LAST_ICMP_PREDICATE &&
641 "Invalid ICmp predicate value");
642 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
643 "Both operands to ICmp instruction are not of the same type!");
644 // Check that the operands are the right type
645 assert((getOperand(0)->getType()->isIntOrIntVector() ||
646 isa<PointerType>(getOperand(0)->getType())) &&
647 "Invalid operand types for ICmp instruction");
650 /// @brief Constructor with insert-at-end semantics.
652 BasicBlock &InsertAtEnd, ///< Block to insert into.
653 Predicate pred, ///< The predicate to use for the comparison
654 Value *LHS, ///< The left-hand-side of the expression
655 Value *RHS, ///< The right-hand-side of the expression
656 const std::string &NameStr = "" ///< Name of the instruction
657 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
658 Instruction::ICmp, pred, LHS, RHS, NameStr,
660 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
661 pred <= CmpInst::LAST_ICMP_PREDICATE &&
662 "Invalid ICmp predicate value");
663 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
664 "Both operands to ICmp instruction are not of the same type!");
665 // Check that the operands are the right type
666 assert((getOperand(0)->getType()->isIntOrIntVector() ||
667 isa<PointerType>(getOperand(0)->getType())) &&
668 "Invalid operand types for ICmp instruction");
671 /// @brief Constructor with no-insertion semantics
673 LLVMContext &Context, ///< Context to construct within
674 Predicate pred, ///< The predicate to use for the comparison
675 Value *LHS, ///< The left-hand-side of the expression
676 Value *RHS, ///< The right-hand-side of the expression
677 const std::string &NameStr = "" ///< Name of the instruction
678 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
679 Instruction::ICmp, pred, LHS, RHS, NameStr) {
680 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
681 pred <= CmpInst::LAST_ICMP_PREDICATE &&
682 "Invalid ICmp predicate value");
683 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
684 "Both operands to ICmp instruction are not of the same type!");
685 // Check that the operands are the right type
686 assert((getOperand(0)->getType()->isIntOrIntVector() ||
687 isa<PointerType>(getOperand(0)->getType())) &&
688 "Invalid operand types for ICmp instruction");
691 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
692 /// @returns the predicate that would be the result if the operand were
693 /// regarded as signed.
694 /// @brief Return the signed version of the predicate
695 Predicate getSignedPredicate() const {
696 return getSignedPredicate(getPredicate());
699 /// This is a static version that you can use without an instruction.
700 /// @brief Return the signed version of the predicate.
701 static Predicate getSignedPredicate(Predicate pred);
703 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
704 /// @returns the predicate that would be the result if the operand were
705 /// regarded as unsigned.
706 /// @brief Return the unsigned version of the predicate
707 Predicate getUnsignedPredicate() const {
708 return getUnsignedPredicate(getPredicate());
711 /// This is a static version that you can use without an instruction.
712 /// @brief Return the unsigned version of the predicate.
713 static Predicate getUnsignedPredicate(Predicate pred);
715 /// isEquality - Return true if this predicate is either EQ or NE. This also
716 /// tests for commutativity.
717 static bool isEquality(Predicate P) {
718 return P == ICMP_EQ || P == ICMP_NE;
721 /// isEquality - Return true if this predicate is either EQ or NE. This also
722 /// tests for commutativity.
723 bool isEquality() const {
724 return isEquality(getPredicate());
727 /// @returns true if the predicate of this ICmpInst is commutative
728 /// @brief Determine if this relation is commutative.
729 bool isCommutative() const { return isEquality(); }
731 /// isRelational - Return true if the predicate is relational (not EQ or NE).
733 bool isRelational() const {
734 return !isEquality();
737 /// isRelational - Return true if the predicate is relational (not EQ or NE).
739 static bool isRelational(Predicate P) {
740 return !isEquality(P);
743 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
744 /// @brief Determine if this instruction's predicate is signed.
745 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
747 /// @returns true if the predicate provided is signed, false otherwise
748 /// @brief Determine if the predicate is signed.
749 static bool isSignedPredicate(Predicate pred);
751 /// @returns true if the specified compare predicate is
752 /// true when both operands are equal...
753 /// @brief Determine if the icmp is true when both operands are equal
754 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
755 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
756 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
757 pred == ICmpInst::ICMP_SLE;
760 /// @returns true if the specified compare instruction is
761 /// true when both operands are equal...
762 /// @brief Determine if the ICmpInst returns true when both operands are equal
763 bool isTrueWhenEqual() {
764 return isTrueWhenEqual(getPredicate());
767 /// Initialize a set of values that all satisfy the predicate with C.
768 /// @brief Make a ConstantRange for a relation with a constant value.
769 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
771 /// Exchange the two operands to this instruction in such a way that it does
772 /// not modify the semantics of the instruction. The predicate value may be
773 /// changed to retain the same result if the predicate is order dependent
775 /// @brief Swap operands and adjust predicate.
776 void swapOperands() {
777 SubclassData = getSwappedPredicate();
778 Op<0>().swap(Op<1>());
781 virtual ICmpInst *clone(LLVMContext &Context) const;
783 // Methods for support type inquiry through isa, cast, and dyn_cast:
784 static inline bool classof(const ICmpInst *) { return true; }
785 static inline bool classof(const Instruction *I) {
786 return I->getOpcode() == Instruction::ICmp;
788 static inline bool classof(const Value *V) {
789 return isa<Instruction>(V) && classof(cast<Instruction>(V));
794 //===----------------------------------------------------------------------===//
796 //===----------------------------------------------------------------------===//
798 /// This instruction compares its operands according to the predicate given
799 /// to the constructor. It only operates on floating point values or packed
800 /// vectors of floating point values. The operands must be identical types.
801 /// @brief Represents a floating point comparison operator.
802 class FCmpInst: public CmpInst {
804 /// @brief Constructor with insert-before-instruction semantics.
806 Instruction *InsertBefore, ///< Where to insert
807 Predicate pred, ///< The predicate to use for the comparison
808 Value *LHS, ///< The left-hand-side of the expression
809 Value *RHS, ///< The right-hand-side of the expression
810 const std::string &NameStr = "" ///< Name of the instruction
811 ) : CmpInst(InsertBefore->getParent()->getContext()->
812 makeCmpResultType(LHS->getType()),
813 Instruction::FCmp, pred, LHS, RHS, NameStr,
815 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
816 "Invalid FCmp predicate value");
817 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
818 "Both operands to FCmp instruction are not of the same type!");
819 // Check that the operands are the right type
820 assert(getOperand(0)->getType()->isFPOrFPVector() &&
821 "Invalid operand types for FCmp instruction");
824 /// @brief Constructor with insert-at-end semantics.
826 BasicBlock &InsertAtEnd, ///< Block to insert into.
827 Predicate pred, ///< The predicate to use for the comparison
828 Value *LHS, ///< The left-hand-side of the expression
829 Value *RHS, ///< The right-hand-side of the expression
830 const std::string &NameStr = "" ///< Name of the instruction
831 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
832 Instruction::FCmp, pred, LHS, RHS, NameStr,
834 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
835 "Invalid FCmp predicate value");
836 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
837 "Both operands to FCmp instruction are not of the same type!");
838 // Check that the operands are the right type
839 assert(getOperand(0)->getType()->isFPOrFPVector() &&
840 "Invalid operand types for FCmp instruction");
843 /// @brief Constructor with no-insertion semantics
845 LLVMContext &Context, ///< Context to build in
846 Predicate pred, ///< The predicate to use for the comparison
847 Value *LHS, ///< The left-hand-side of the expression
848 Value *RHS, ///< The right-hand-side of the expression
849 const std::string &NameStr = "" ///< Name of the instruction
850 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
851 Instruction::FCmp, pred, LHS, RHS, NameStr) {
852 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
853 "Invalid FCmp predicate value");
854 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
855 "Both operands to FCmp instruction are not of the same type!");
856 // Check that the operands are the right type
857 assert(getOperand(0)->getType()->isFPOrFPVector() &&
858 "Invalid operand types for FCmp instruction");
861 /// @returns true if the predicate of this instruction is EQ or NE.
862 /// @brief Determine if this is an equality predicate.
863 bool isEquality() const {
864 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
865 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
868 /// @returns true if the predicate of this instruction is commutative.
869 /// @brief Determine if this is a commutative predicate.
870 bool isCommutative() const {
871 return isEquality() ||
872 SubclassData == FCMP_FALSE ||
873 SubclassData == FCMP_TRUE ||
874 SubclassData == FCMP_ORD ||
875 SubclassData == FCMP_UNO;
878 /// @returns true if the predicate is relational (not EQ or NE).
879 /// @brief Determine if this a relational predicate.
880 bool isRelational() const { return !isEquality(); }
882 /// Exchange the two operands to this instruction in such a way that it does
883 /// not modify the semantics of the instruction. The predicate value may be
884 /// changed to retain the same result if the predicate is order dependent
886 /// @brief Swap operands and adjust predicate.
887 void swapOperands() {
888 SubclassData = getSwappedPredicate();
889 Op<0>().swap(Op<1>());
892 virtual FCmpInst *clone(LLVMContext &Context) const;
894 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
895 static inline bool classof(const FCmpInst *) { return true; }
896 static inline bool classof(const Instruction *I) {
897 return I->getOpcode() == Instruction::FCmp;
899 static inline bool classof(const Value *V) {
900 return isa<Instruction>(V) && classof(cast<Instruction>(V));
905 //===----------------------------------------------------------------------===//
907 //===----------------------------------------------------------------------===//
908 /// CallInst - This class represents a function call, abstracting a target
909 /// machine's calling convention. This class uses low bit of the SubClassData
910 /// field to indicate whether or not this is a tail call. The rest of the bits
911 /// hold the calling convention of the call.
914 class CallInst : public Instruction {
915 AttrListPtr AttributeList; ///< parameter attributes for call
916 CallInst(const CallInst &CI);
917 void init(Value *Func, Value* const *Params, unsigned NumParams);
918 void init(Value *Func, Value *Actual1, Value *Actual2);
919 void init(Value *Func, Value *Actual);
920 void init(Value *Func);
922 template<typename InputIterator>
923 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
924 const std::string &NameStr,
925 // This argument ensures that we have an iterator we can
926 // do arithmetic on in constant time
927 std::random_access_iterator_tag) {
928 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
930 // This requires that the iterator points to contiguous memory.
931 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
935 /// Construct a CallInst given a range of arguments. InputIterator
936 /// must be a random-access iterator pointing to contiguous storage
937 /// (e.g. a std::vector<>::iterator). Checks are made for
938 /// random-accessness but not for contiguous storage as that would
939 /// incur runtime overhead.
940 /// @brief Construct a CallInst from a range of arguments
941 template<typename InputIterator>
942 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
943 const std::string &NameStr, Instruction *InsertBefore);
945 /// Construct a CallInst given a range of arguments. InputIterator
946 /// must be a random-access iterator pointing to contiguous storage
947 /// (e.g. a std::vector<>::iterator). Checks are made for
948 /// random-accessness but not for contiguous storage as that would
949 /// incur runtime overhead.
950 /// @brief Construct a CallInst from a range of arguments
951 template<typename InputIterator>
952 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
953 const std::string &NameStr, BasicBlock *InsertAtEnd);
955 CallInst(Value *F, Value *Actual, const std::string& NameStr,
956 Instruction *InsertBefore);
957 CallInst(Value *F, Value *Actual, const std::string& NameStr,
958 BasicBlock *InsertAtEnd);
959 explicit CallInst(Value *F, const std::string &NameStr,
960 Instruction *InsertBefore);
961 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
963 template<typename InputIterator>
964 static CallInst *Create(Value *Func,
965 InputIterator ArgBegin, InputIterator ArgEnd,
966 const std::string &NameStr = "",
967 Instruction *InsertBefore = 0) {
968 return new((unsigned)(ArgEnd - ArgBegin + 1))
969 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
971 template<typename InputIterator>
972 static CallInst *Create(Value *Func,
973 InputIterator ArgBegin, InputIterator ArgEnd,
974 const std::string &NameStr, BasicBlock *InsertAtEnd) {
975 return new((unsigned)(ArgEnd - ArgBegin + 1))
976 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
978 static CallInst *Create(Value *F, Value *Actual,
979 const std::string& NameStr = "",
980 Instruction *InsertBefore = 0) {
981 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
983 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
984 BasicBlock *InsertAtEnd) {
985 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
987 static CallInst *Create(Value *F, const std::string &NameStr = "",
988 Instruction *InsertBefore = 0) {
989 return new(1) CallInst(F, NameStr, InsertBefore);
991 static CallInst *Create(Value *F, const std::string &NameStr,
992 BasicBlock *InsertAtEnd) {
993 return new(1) CallInst(F, NameStr, InsertAtEnd);
998 bool isTailCall() const { return SubclassData & 1; }
999 void setTailCall(bool isTC = true) {
1000 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1003 virtual CallInst *clone(LLVMContext &Context) const;
1005 /// Provide fast operand accessors
1006 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1008 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1010 unsigned getCallingConv() const { return SubclassData >> 1; }
1011 void setCallingConv(unsigned CC) {
1012 SubclassData = (SubclassData & 1) | (CC << 1);
1015 /// getAttributes - Return the parameter attributes for this call.
1017 const AttrListPtr &getAttributes() const { return AttributeList; }
1019 /// setAttributes - Set the parameter attributes for this call.
1021 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1023 /// addAttribute - adds the attribute to the list of attributes.
1024 void addAttribute(unsigned i, Attributes attr);
1026 /// removeAttribute - removes the attribute from the list of attributes.
1027 void removeAttribute(unsigned i, Attributes attr);
1029 /// @brief Determine whether the call or the callee has the given attribute.
1030 bool paramHasAttr(unsigned i, Attributes attr) const;
1032 /// @brief Extract the alignment for a call or parameter (0=unknown).
1033 unsigned getParamAlignment(unsigned i) const {
1034 return AttributeList.getParamAlignment(i);
1037 /// @brief Determine if the call does not access memory.
1038 bool doesNotAccessMemory() const {
1039 return paramHasAttr(~0, Attribute::ReadNone);
1041 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1042 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1043 else removeAttribute(~0, Attribute::ReadNone);
1046 /// @brief Determine if the call does not access or only reads memory.
1047 bool onlyReadsMemory() const {
1048 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1050 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1051 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1052 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1055 /// @brief Determine if the call cannot return.
1056 bool doesNotReturn() const {
1057 return paramHasAttr(~0, Attribute::NoReturn);
1059 void setDoesNotReturn(bool DoesNotReturn = true) {
1060 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1061 else removeAttribute(~0, Attribute::NoReturn);
1064 /// @brief Determine if the call cannot unwind.
1065 bool doesNotThrow() const {
1066 return paramHasAttr(~0, Attribute::NoUnwind);
1068 void setDoesNotThrow(bool DoesNotThrow = true) {
1069 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1070 else removeAttribute(~0, Attribute::NoUnwind);
1073 /// @brief Determine if the call returns a structure through first
1074 /// pointer argument.
1075 bool hasStructRetAttr() const {
1076 // Be friendly and also check the callee.
1077 return paramHasAttr(1, Attribute::StructRet);
1080 /// @brief Determine if any call argument is an aggregate passed by value.
1081 bool hasByValArgument() const {
1082 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1085 /// getCalledFunction - Return the function called, or null if this is an
1086 /// indirect function invocation.
1088 Function *getCalledFunction() const {
1089 return dyn_cast<Function>(Op<0>());
1092 /// getCalledValue - Get a pointer to the function that is invoked by this
1094 const Value *getCalledValue() const { return Op<0>(); }
1095 Value *getCalledValue() { return Op<0>(); }
1097 // Methods for support type inquiry through isa, cast, and dyn_cast:
1098 static inline bool classof(const CallInst *) { return true; }
1099 static inline bool classof(const Instruction *I) {
1100 return I->getOpcode() == Instruction::Call;
1102 static inline bool classof(const Value *V) {
1103 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1108 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1111 template<typename InputIterator>
1112 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1113 const std::string &NameStr, BasicBlock *InsertAtEnd)
1114 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1115 ->getElementType())->getReturnType(),
1117 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1118 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1119 init(Func, ArgBegin, ArgEnd, NameStr,
1120 typename std::iterator_traits<InputIterator>::iterator_category());
1123 template<typename InputIterator>
1124 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1125 const std::string &NameStr, Instruction *InsertBefore)
1126 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1127 ->getElementType())->getReturnType(),
1129 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1130 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1131 init(Func, ArgBegin, ArgEnd, NameStr,
1132 typename std::iterator_traits<InputIterator>::iterator_category());
1135 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1137 //===----------------------------------------------------------------------===//
1139 //===----------------------------------------------------------------------===//
1141 /// SelectInst - This class represents the LLVM 'select' instruction.
1143 class SelectInst : public Instruction {
1144 void init(Value *C, Value *S1, Value *S2) {
1145 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1151 SelectInst(const SelectInst &SI)
1152 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1153 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1155 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1156 Instruction *InsertBefore)
1157 : Instruction(S1->getType(), Instruction::Select,
1158 &Op<0>(), 3, InsertBefore) {
1162 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1163 BasicBlock *InsertAtEnd)
1164 : Instruction(S1->getType(), Instruction::Select,
1165 &Op<0>(), 3, InsertAtEnd) {
1170 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1171 const std::string &NameStr = "",
1172 Instruction *InsertBefore = 0) {
1173 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1175 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1176 const std::string &NameStr,
1177 BasicBlock *InsertAtEnd) {
1178 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1181 Value *getCondition() const { return Op<0>(); }
1182 Value *getTrueValue() const { return Op<1>(); }
1183 Value *getFalseValue() const { return Op<2>(); }
1185 /// areInvalidOperands - Return a string if the specified operands are invalid
1186 /// for a select operation, otherwise return null.
1187 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1189 /// Transparently provide more efficient getOperand methods.
1190 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1192 OtherOps getOpcode() const {
1193 return static_cast<OtherOps>(Instruction::getOpcode());
1196 virtual SelectInst *clone(LLVMContext &Context) const;
1198 // Methods for support type inquiry through isa, cast, and dyn_cast:
1199 static inline bool classof(const SelectInst *) { return true; }
1200 static inline bool classof(const Instruction *I) {
1201 return I->getOpcode() == Instruction::Select;
1203 static inline bool classof(const Value *V) {
1204 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1209 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1212 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1214 //===----------------------------------------------------------------------===//
1216 //===----------------------------------------------------------------------===//
1218 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1219 /// an argument of the specified type given a va_list and increments that list
1221 class VAArgInst : public UnaryInstruction {
1222 VAArgInst(const VAArgInst &VAA)
1223 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1225 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1226 Instruction *InsertBefore = 0)
1227 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1230 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1231 BasicBlock *InsertAtEnd)
1232 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1236 virtual VAArgInst *clone(LLVMContext &Context) const;
1238 // Methods for support type inquiry through isa, cast, and dyn_cast:
1239 static inline bool classof(const VAArgInst *) { return true; }
1240 static inline bool classof(const Instruction *I) {
1241 return I->getOpcode() == VAArg;
1243 static inline bool classof(const Value *V) {
1244 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1248 //===----------------------------------------------------------------------===//
1249 // ExtractElementInst Class
1250 //===----------------------------------------------------------------------===//
1252 /// ExtractElementInst - This instruction extracts a single (scalar)
1253 /// element from a VectorType value
1255 class ExtractElementInst : public Instruction {
1256 ExtractElementInst(const ExtractElementInst &EE) :
1257 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1258 Op<0>() = EE.Op<0>();
1259 Op<1>() = EE.Op<1>();
1263 // allocate space for exactly two operands
1264 void *operator new(size_t s) {
1265 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1267 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1268 Instruction *InsertBefore = 0);
1269 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
1270 Instruction *InsertBefore = 0);
1271 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1272 BasicBlock *InsertAtEnd);
1273 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
1274 BasicBlock *InsertAtEnd);
1276 /// isValidOperands - Return true if an extractelement instruction can be
1277 /// formed with the specified operands.
1278 static bool isValidOperands(const Value *Vec, const Value *Idx);
1280 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1282 /// Transparently provide more efficient getOperand methods.
1283 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1285 // Methods for support type inquiry through isa, cast, and dyn_cast:
1286 static inline bool classof(const ExtractElementInst *) { return true; }
1287 static inline bool classof(const Instruction *I) {
1288 return I->getOpcode() == Instruction::ExtractElement;
1290 static inline bool classof(const Value *V) {
1291 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1296 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1299 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1301 //===----------------------------------------------------------------------===//
1302 // InsertElementInst Class
1303 //===----------------------------------------------------------------------===//
1305 /// InsertElementInst - This instruction inserts a single (scalar)
1306 /// element into a VectorType value
1308 class InsertElementInst : public Instruction {
1309 InsertElementInst(const InsertElementInst &IE);
1310 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1311 const std::string &NameStr = "",
1312 Instruction *InsertBefore = 0);
1313 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1314 const std::string &NameStr = "",
1315 Instruction *InsertBefore = 0);
1316 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1317 const std::string &NameStr, BasicBlock *InsertAtEnd);
1318 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1319 const std::string &NameStr, BasicBlock *InsertAtEnd);
1321 static InsertElementInst *Create(const InsertElementInst &IE) {
1322 return new(IE.getNumOperands()) InsertElementInst(IE);
1324 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1325 const std::string &NameStr = "",
1326 Instruction *InsertBefore = 0) {
1327 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1329 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1330 const std::string &NameStr = "",
1331 Instruction *InsertBefore = 0) {
1332 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1334 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1335 const std::string &NameStr,
1336 BasicBlock *InsertAtEnd) {
1337 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1339 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1340 const std::string &NameStr,
1341 BasicBlock *InsertAtEnd) {
1342 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1345 /// isValidOperands - Return true if an insertelement instruction can be
1346 /// formed with the specified operands.
1347 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1350 virtual InsertElementInst *clone(LLVMContext &Context) const;
1352 /// getType - Overload to return most specific vector type.
1354 const VectorType *getType() const {
1355 return reinterpret_cast<const VectorType*>(Instruction::getType());
1358 /// Transparently provide more efficient getOperand methods.
1359 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1361 // Methods for support type inquiry through isa, cast, and dyn_cast:
1362 static inline bool classof(const InsertElementInst *) { return true; }
1363 static inline bool classof(const Instruction *I) {
1364 return I->getOpcode() == Instruction::InsertElement;
1366 static inline bool classof(const Value *V) {
1367 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1372 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1375 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1377 //===----------------------------------------------------------------------===//
1378 // ShuffleVectorInst Class
1379 //===----------------------------------------------------------------------===//
1381 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1384 class ShuffleVectorInst : public Instruction {
1385 ShuffleVectorInst(const ShuffleVectorInst &IE);
1387 // allocate space for exactly three operands
1388 void *operator new(size_t s) {
1389 return User::operator new(s, 3);
1391 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1392 const std::string &NameStr = "",
1393 Instruction *InsertBefor = 0);
1394 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1395 const std::string &NameStr, BasicBlock *InsertAtEnd);
1397 /// isValidOperands - Return true if a shufflevector instruction can be
1398 /// formed with the specified operands.
1399 static bool isValidOperands(const Value *V1, const Value *V2,
1402 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1404 /// getType - Overload to return most specific vector type.
1406 const VectorType *getType() const {
1407 return reinterpret_cast<const VectorType*>(Instruction::getType());
1410 /// Transparently provide more efficient getOperand methods.
1411 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1413 /// getMaskValue - Return the index from the shuffle mask for the specified
1414 /// output result. This is either -1 if the element is undef or a number less
1415 /// than 2*numelements.
1416 int getMaskValue(unsigned i) const;
1418 // Methods for support type inquiry through isa, cast, and dyn_cast:
1419 static inline bool classof(const ShuffleVectorInst *) { return true; }
1420 static inline bool classof(const Instruction *I) {
1421 return I->getOpcode() == Instruction::ShuffleVector;
1423 static inline bool classof(const Value *V) {
1424 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1429 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1432 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1434 //===----------------------------------------------------------------------===//
1435 // ExtractValueInst Class
1436 //===----------------------------------------------------------------------===//
1438 /// ExtractValueInst - This instruction extracts a struct member or array
1439 /// element value from an aggregate value.
1441 class ExtractValueInst : public UnaryInstruction {
1442 SmallVector<unsigned, 4> Indices;
1444 ExtractValueInst(const ExtractValueInst &EVI);
1445 void init(const unsigned *Idx, unsigned NumIdx,
1446 const std::string &NameStr);
1447 void init(unsigned Idx, const std::string &NameStr);
1449 template<typename InputIterator>
1450 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1451 const std::string &NameStr,
1452 // This argument ensures that we have an iterator we can
1453 // do arithmetic on in constant time
1454 std::random_access_iterator_tag) {
1455 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1457 // There's no fundamental reason why we require at least one index
1458 // (other than weirdness with &*IdxBegin being invalid; see
1459 // getelementptr's init routine for example). But there's no
1460 // present need to support it.
1461 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1463 // This requires that the iterator points to contiguous memory.
1464 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1465 // we have to build an array here
1468 /// getIndexedType - Returns the type of the element that would be extracted
1469 /// with an extractvalue instruction with the specified parameters.
1471 /// Null is returned if the indices are invalid for the specified
1474 static const Type *getIndexedType(const Type *Agg,
1475 const unsigned *Idx, unsigned NumIdx);
1477 template<typename InputIterator>
1478 static const Type *getIndexedType(const Type *Ptr,
1479 InputIterator IdxBegin,
1480 InputIterator IdxEnd,
1481 // This argument ensures that we
1482 // have an iterator we can do
1483 // arithmetic on in constant time
1484 std::random_access_iterator_tag) {
1485 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1488 // This requires that the iterator points to contiguous memory.
1489 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1491 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1494 /// Constructors - Create a extractvalue instruction with a base aggregate
1495 /// value and a list of indices. The first ctor can optionally insert before
1496 /// an existing instruction, the second appends the new instruction to the
1497 /// specified BasicBlock.
1498 template<typename InputIterator>
1499 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1500 InputIterator IdxEnd,
1501 const std::string &NameStr,
1502 Instruction *InsertBefore);
1503 template<typename InputIterator>
1504 inline ExtractValueInst(Value *Agg,
1505 InputIterator IdxBegin, InputIterator IdxEnd,
1506 const std::string &NameStr, BasicBlock *InsertAtEnd);
1508 // allocate space for exactly one operand
1509 void *operator new(size_t s) {
1510 return User::operator new(s, 1);
1514 template<typename InputIterator>
1515 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1516 InputIterator IdxEnd,
1517 const std::string &NameStr = "",
1518 Instruction *InsertBefore = 0) {
1520 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1522 template<typename InputIterator>
1523 static ExtractValueInst *Create(Value *Agg,
1524 InputIterator IdxBegin, InputIterator IdxEnd,
1525 const std::string &NameStr,
1526 BasicBlock *InsertAtEnd) {
1527 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1530 /// Constructors - These two creators are convenience methods because one
1531 /// index extractvalue instructions are much more common than those with
1533 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1534 const std::string &NameStr = "",
1535 Instruction *InsertBefore = 0) {
1536 unsigned Idxs[1] = { Idx };
1537 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1539 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1540 const std::string &NameStr,
1541 BasicBlock *InsertAtEnd) {
1542 unsigned Idxs[1] = { Idx };
1543 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1546 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1548 /// getIndexedType - Returns the type of the element that would be extracted
1549 /// with an extractvalue instruction with the specified parameters.
1551 /// Null is returned if the indices are invalid for the specified
1554 template<typename InputIterator>
1555 static const Type *getIndexedType(const Type *Ptr,
1556 InputIterator IdxBegin,
1557 InputIterator IdxEnd) {
1558 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1559 typename std::iterator_traits<InputIterator>::
1560 iterator_category());
1562 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1564 typedef const unsigned* idx_iterator;
1565 inline idx_iterator idx_begin() const { return Indices.begin(); }
1566 inline idx_iterator idx_end() const { return Indices.end(); }
1568 Value *getAggregateOperand() {
1569 return getOperand(0);
1571 const Value *getAggregateOperand() const {
1572 return getOperand(0);
1574 static unsigned getAggregateOperandIndex() {
1575 return 0U; // get index for modifying correct operand
1578 unsigned getNumIndices() const { // Note: always non-negative
1579 return (unsigned)Indices.size();
1582 bool hasIndices() const {
1586 // Methods for support type inquiry through isa, cast, and dyn_cast:
1587 static inline bool classof(const ExtractValueInst *) { return true; }
1588 static inline bool classof(const Instruction *I) {
1589 return I->getOpcode() == Instruction::ExtractValue;
1591 static inline bool classof(const Value *V) {
1592 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1596 template<typename InputIterator>
1597 ExtractValueInst::ExtractValueInst(Value *Agg,
1598 InputIterator IdxBegin,
1599 InputIterator IdxEnd,
1600 const std::string &NameStr,
1601 Instruction *InsertBefore)
1602 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1604 ExtractValue, Agg, InsertBefore) {
1605 init(IdxBegin, IdxEnd, NameStr,
1606 typename std::iterator_traits<InputIterator>::iterator_category());
1608 template<typename InputIterator>
1609 ExtractValueInst::ExtractValueInst(Value *Agg,
1610 InputIterator IdxBegin,
1611 InputIterator IdxEnd,
1612 const std::string &NameStr,
1613 BasicBlock *InsertAtEnd)
1614 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1616 ExtractValue, Agg, InsertAtEnd) {
1617 init(IdxBegin, IdxEnd, NameStr,
1618 typename std::iterator_traits<InputIterator>::iterator_category());
1622 //===----------------------------------------------------------------------===//
1623 // InsertValueInst Class
1624 //===----------------------------------------------------------------------===//
1626 /// InsertValueInst - This instruction inserts a struct field of array element
1627 /// value into an aggregate value.
1629 class InsertValueInst : public Instruction {
1630 SmallVector<unsigned, 4> Indices;
1632 void *operator new(size_t, unsigned); // Do not implement
1633 InsertValueInst(const InsertValueInst &IVI);
1634 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1635 const std::string &NameStr);
1636 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1638 template<typename InputIterator>
1639 void init(Value *Agg, Value *Val,
1640 InputIterator IdxBegin, InputIterator IdxEnd,
1641 const std::string &NameStr,
1642 // This argument ensures that we have an iterator we can
1643 // do arithmetic on in constant time
1644 std::random_access_iterator_tag) {
1645 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1647 // There's no fundamental reason why we require at least one index
1648 // (other than weirdness with &*IdxBegin being invalid; see
1649 // getelementptr's init routine for example). But there's no
1650 // present need to support it.
1651 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1653 // This requires that the iterator points to contiguous memory.
1654 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1655 // we have to build an array here
1658 /// Constructors - Create a insertvalue instruction with a base aggregate
1659 /// value, a value to insert, and a list of indices. The first ctor can
1660 /// optionally insert before an existing instruction, the second appends
1661 /// the new instruction to the specified BasicBlock.
1662 template<typename InputIterator>
1663 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1664 InputIterator IdxEnd,
1665 const std::string &NameStr,
1666 Instruction *InsertBefore);
1667 template<typename InputIterator>
1668 inline InsertValueInst(Value *Agg, Value *Val,
1669 InputIterator IdxBegin, InputIterator IdxEnd,
1670 const std::string &NameStr, BasicBlock *InsertAtEnd);
1672 /// Constructors - These two constructors are convenience methods because one
1673 /// and two index insertvalue instructions are so common.
1674 InsertValueInst(Value *Agg, Value *Val,
1675 unsigned Idx, const std::string &NameStr = "",
1676 Instruction *InsertBefore = 0);
1677 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1678 const std::string &NameStr, BasicBlock *InsertAtEnd);
1680 // allocate space for exactly two operands
1681 void *operator new(size_t s) {
1682 return User::operator new(s, 2);
1685 template<typename InputIterator>
1686 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1687 InputIterator IdxEnd,
1688 const std::string &NameStr = "",
1689 Instruction *InsertBefore = 0) {
1690 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1691 NameStr, InsertBefore);
1693 template<typename InputIterator>
1694 static InsertValueInst *Create(Value *Agg, Value *Val,
1695 InputIterator IdxBegin, InputIterator IdxEnd,
1696 const std::string &NameStr,
1697 BasicBlock *InsertAtEnd) {
1698 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1699 NameStr, InsertAtEnd);
1702 /// Constructors - These two creators are convenience methods because one
1703 /// index insertvalue instructions are much more common than those with
1705 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1706 const std::string &NameStr = "",
1707 Instruction *InsertBefore = 0) {
1708 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1710 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1711 const std::string &NameStr,
1712 BasicBlock *InsertAtEnd) {
1713 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1716 virtual InsertValueInst *clone(LLVMContext &Context) const;
1718 /// Transparently provide more efficient getOperand methods.
1719 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1721 typedef const unsigned* idx_iterator;
1722 inline idx_iterator idx_begin() const { return Indices.begin(); }
1723 inline idx_iterator idx_end() const { return Indices.end(); }
1725 Value *getAggregateOperand() {
1726 return getOperand(0);
1728 const Value *getAggregateOperand() const {
1729 return getOperand(0);
1731 static unsigned getAggregateOperandIndex() {
1732 return 0U; // get index for modifying correct operand
1735 Value *getInsertedValueOperand() {
1736 return getOperand(1);
1738 const Value *getInsertedValueOperand() const {
1739 return getOperand(1);
1741 static unsigned getInsertedValueOperandIndex() {
1742 return 1U; // get index for modifying correct operand
1745 unsigned getNumIndices() const { // Note: always non-negative
1746 return (unsigned)Indices.size();
1749 bool hasIndices() const {
1753 // Methods for support type inquiry through isa, cast, and dyn_cast:
1754 static inline bool classof(const InsertValueInst *) { return true; }
1755 static inline bool classof(const Instruction *I) {
1756 return I->getOpcode() == Instruction::InsertValue;
1758 static inline bool classof(const Value *V) {
1759 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1764 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1767 template<typename InputIterator>
1768 InsertValueInst::InsertValueInst(Value *Agg,
1770 InputIterator IdxBegin,
1771 InputIterator IdxEnd,
1772 const std::string &NameStr,
1773 Instruction *InsertBefore)
1774 : Instruction(Agg->getType(), InsertValue,
1775 OperandTraits<InsertValueInst>::op_begin(this),
1777 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1778 typename std::iterator_traits<InputIterator>::iterator_category());
1780 template<typename InputIterator>
1781 InsertValueInst::InsertValueInst(Value *Agg,
1783 InputIterator IdxBegin,
1784 InputIterator IdxEnd,
1785 const std::string &NameStr,
1786 BasicBlock *InsertAtEnd)
1787 : Instruction(Agg->getType(), InsertValue,
1788 OperandTraits<InsertValueInst>::op_begin(this),
1790 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1791 typename std::iterator_traits<InputIterator>::iterator_category());
1794 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1796 //===----------------------------------------------------------------------===//
1798 //===----------------------------------------------------------------------===//
1800 // PHINode - The PHINode class is used to represent the magical mystical PHI
1801 // node, that can not exist in nature, but can be synthesized in a computer
1802 // scientist's overactive imagination.
1804 class PHINode : public Instruction {
1805 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1806 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1807 /// the number actually in use.
1808 unsigned ReservedSpace;
1809 PHINode(const PHINode &PN);
1810 // allocate space for exactly zero operands
1811 void *operator new(size_t s) {
1812 return User::operator new(s, 0);
1814 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1815 Instruction *InsertBefore = 0)
1816 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1821 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1822 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1827 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1828 Instruction *InsertBefore = 0) {
1829 return new PHINode(Ty, NameStr, InsertBefore);
1831 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1832 BasicBlock *InsertAtEnd) {
1833 return new PHINode(Ty, NameStr, InsertAtEnd);
1837 /// reserveOperandSpace - This method can be used to avoid repeated
1838 /// reallocation of PHI operand lists by reserving space for the correct
1839 /// number of operands before adding them. Unlike normal vector reserves,
1840 /// this method can also be used to trim the operand space.
1841 void reserveOperandSpace(unsigned NumValues) {
1842 resizeOperands(NumValues*2);
1845 virtual PHINode *clone(LLVMContext &Context) const;
1847 /// Provide fast operand accessors
1848 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1850 /// getNumIncomingValues - Return the number of incoming edges
1852 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1854 /// getIncomingValue - Return incoming value number x
1856 Value *getIncomingValue(unsigned i) const {
1857 assert(i*2 < getNumOperands() && "Invalid value number!");
1858 return getOperand(i*2);
1860 void setIncomingValue(unsigned i, Value *V) {
1861 assert(i*2 < getNumOperands() && "Invalid value number!");
1864 static unsigned getOperandNumForIncomingValue(unsigned i) {
1867 static unsigned getIncomingValueNumForOperand(unsigned i) {
1868 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1872 /// getIncomingBlock - Return incoming basic block corresponding
1873 /// to value use iterator
1875 template <typename U>
1876 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1877 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1878 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1880 /// getIncomingBlock - Return incoming basic block number x
1882 BasicBlock *getIncomingBlock(unsigned i) const {
1883 return static_cast<BasicBlock*>(getOperand(i*2+1));
1885 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1886 setOperand(i*2+1, BB);
1888 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1891 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1892 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1896 /// addIncoming - Add an incoming value to the end of the PHI list
1898 void addIncoming(Value *V, BasicBlock *BB) {
1899 assert(V && "PHI node got a null value!");
1900 assert(BB && "PHI node got a null basic block!");
1901 assert(getType() == V->getType() &&
1902 "All operands to PHI node must be the same type as the PHI node!");
1903 unsigned OpNo = NumOperands;
1904 if (OpNo+2 > ReservedSpace)
1905 resizeOperands(0); // Get more space!
1906 // Initialize some new operands.
1907 NumOperands = OpNo+2;
1908 OperandList[OpNo] = V;
1909 OperandList[OpNo+1] = BB;
1912 /// removeIncomingValue - Remove an incoming value. This is useful if a
1913 /// predecessor basic block is deleted. The value removed is returned.
1915 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1916 /// is true), the PHI node is destroyed and any uses of it are replaced with
1917 /// dummy values. The only time there should be zero incoming values to a PHI
1918 /// node is when the block is dead, so this strategy is sound.
1920 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1922 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1923 int Idx = getBasicBlockIndex(BB);
1924 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1925 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1928 /// getBasicBlockIndex - Return the first index of the specified basic
1929 /// block in the value list for this PHI. Returns -1 if no instance.
1931 int getBasicBlockIndex(const BasicBlock *BB) const {
1932 Use *OL = OperandList;
1933 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1934 if (OL[i+1].get() == BB) return i/2;
1938 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1939 return getIncomingValue(getBasicBlockIndex(BB));
1942 /// hasConstantValue - If the specified PHI node always merges together the
1943 /// same value, return the value, otherwise return null.
1945 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1947 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1948 static inline bool classof(const PHINode *) { return true; }
1949 static inline bool classof(const Instruction *I) {
1950 return I->getOpcode() == Instruction::PHI;
1952 static inline bool classof(const Value *V) {
1953 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1956 void resizeOperands(unsigned NumOperands);
1960 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1963 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1966 //===----------------------------------------------------------------------===//
1968 //===----------------------------------------------------------------------===//
1970 //===---------------------------------------------------------------------------
1971 /// ReturnInst - Return a value (possibly void), from a function. Execution
1972 /// does not continue in this function any longer.
1974 class ReturnInst : public TerminatorInst {
1975 ReturnInst(const ReturnInst &RI);
1978 // ReturnInst constructors:
1979 // ReturnInst() - 'ret void' instruction
1980 // ReturnInst( null) - 'ret void' instruction
1981 // ReturnInst(Value* X) - 'ret X' instruction
1982 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1983 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1984 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1985 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1987 // NOTE: If the Value* passed is of type void then the constructor behaves as
1988 // if it was passed NULL.
1989 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1990 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1991 explicit ReturnInst(BasicBlock *InsertAtEnd);
1993 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1994 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1996 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1997 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1999 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2000 return new(0) ReturnInst(InsertAtEnd);
2002 virtual ~ReturnInst();
2004 virtual ReturnInst *clone(LLVMContext &Context) const;
2006 /// Provide fast operand accessors
2007 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2009 /// Convenience accessor
2010 Value *getReturnValue(unsigned n = 0) const {
2011 return n < getNumOperands()
2016 unsigned getNumSuccessors() const { return 0; }
2018 // Methods for support type inquiry through isa, cast, and dyn_cast:
2019 static inline bool classof(const ReturnInst *) { return true; }
2020 static inline bool classof(const Instruction *I) {
2021 return (I->getOpcode() == Instruction::Ret);
2023 static inline bool classof(const Value *V) {
2024 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2027 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2028 virtual unsigned getNumSuccessorsV() const;
2029 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2033 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2036 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2038 //===----------------------------------------------------------------------===//
2040 //===----------------------------------------------------------------------===//
2042 //===---------------------------------------------------------------------------
2043 /// BranchInst - Conditional or Unconditional Branch instruction.
2045 class BranchInst : public TerminatorInst {
2046 /// Ops list - Branches are strange. The operands are ordered:
2047 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2048 /// they don't have to check for cond/uncond branchness. These are mostly
2049 /// accessed relative from op_end().
2050 BranchInst(const BranchInst &BI);
2052 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2053 // BranchInst(BB *B) - 'br B'
2054 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2055 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2056 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2057 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2058 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2059 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2060 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2061 Instruction *InsertBefore = 0);
2062 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2063 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2064 BasicBlock *InsertAtEnd);
2066 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2067 return new(1, true) BranchInst(IfTrue, InsertBefore);
2069 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2070 Value *Cond, Instruction *InsertBefore = 0) {
2071 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2073 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2074 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2076 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2077 Value *Cond, BasicBlock *InsertAtEnd) {
2078 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2083 /// Transparently provide more efficient getOperand methods.
2084 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2086 virtual BranchInst *clone(LLVMContext &Context) const;
2088 bool isUnconditional() const { return getNumOperands() == 1; }
2089 bool isConditional() const { return getNumOperands() == 3; }
2091 Value *getCondition() const {
2092 assert(isConditional() && "Cannot get condition of an uncond branch!");
2096 void setCondition(Value *V) {
2097 assert(isConditional() && "Cannot set condition of unconditional branch!");
2101 // setUnconditionalDest - Change the current branch to an unconditional branch
2102 // targeting the specified block.
2103 // FIXME: Eliminate this ugly method.
2104 void setUnconditionalDest(BasicBlock *Dest) {
2106 if (isConditional()) { // Convert this to an uncond branch.
2110 OperandList = op_begin();
2114 unsigned getNumSuccessors() const { return 1+isConditional(); }
2116 BasicBlock *getSuccessor(unsigned i) const {
2117 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2118 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2121 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2122 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2123 *(&Op<-1>() - idx) = NewSucc;
2126 // Methods for support type inquiry through isa, cast, and dyn_cast:
2127 static inline bool classof(const BranchInst *) { return true; }
2128 static inline bool classof(const Instruction *I) {
2129 return (I->getOpcode() == Instruction::Br);
2131 static inline bool classof(const Value *V) {
2132 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2135 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2136 virtual unsigned getNumSuccessorsV() const;
2137 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2141 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2143 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2145 //===----------------------------------------------------------------------===//
2147 //===----------------------------------------------------------------------===//
2149 //===---------------------------------------------------------------------------
2150 /// SwitchInst - Multiway switch
2152 class SwitchInst : public TerminatorInst {
2153 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2154 unsigned ReservedSpace;
2155 // Operand[0] = Value to switch on
2156 // Operand[1] = Default basic block destination
2157 // Operand[2n ] = Value to match
2158 // Operand[2n+1] = BasicBlock to go to on match
2159 SwitchInst(const SwitchInst &RI);
2160 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2161 void resizeOperands(unsigned No);
2162 // allocate space for exactly zero operands
2163 void *operator new(size_t s) {
2164 return User::operator new(s, 0);
2166 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2167 /// switch on and a default destination. The number of additional cases can
2168 /// be specified here to make memory allocation more efficient. This
2169 /// constructor can also autoinsert before another instruction.
2170 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2171 Instruction *InsertBefore = 0);
2173 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2174 /// switch on and a default destination. The number of additional cases can
2175 /// be specified here to make memory allocation more efficient. This
2176 /// constructor also autoinserts at the end of the specified BasicBlock.
2177 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2178 BasicBlock *InsertAtEnd);
2180 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2181 unsigned NumCases, Instruction *InsertBefore = 0) {
2182 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2184 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2185 unsigned NumCases, BasicBlock *InsertAtEnd) {
2186 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2190 /// Provide fast operand accessors
2191 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2193 // Accessor Methods for Switch stmt
2194 Value *getCondition() const { return getOperand(0); }
2195 void setCondition(Value *V) { setOperand(0, V); }
2197 BasicBlock *getDefaultDest() const {
2198 return cast<BasicBlock>(getOperand(1));
2201 /// getNumCases - return the number of 'cases' in this switch instruction.
2202 /// Note that case #0 is always the default case.
2203 unsigned getNumCases() const {
2204 return getNumOperands()/2;
2207 /// getCaseValue - Return the specified case value. Note that case #0, the
2208 /// default destination, does not have a case value.
2209 ConstantInt *getCaseValue(unsigned i) {
2210 assert(i && i < getNumCases() && "Illegal case value to get!");
2211 return getSuccessorValue(i);
2214 /// getCaseValue - Return the specified case value. Note that case #0, the
2215 /// default destination, does not have a case value.
2216 const ConstantInt *getCaseValue(unsigned i) const {
2217 assert(i && i < getNumCases() && "Illegal case value to get!");
2218 return getSuccessorValue(i);
2221 /// findCaseValue - Search all of the case values for the specified constant.
2222 /// If it is explicitly handled, return the case number of it, otherwise
2223 /// return 0 to indicate that it is handled by the default handler.
2224 unsigned findCaseValue(const ConstantInt *C) const {
2225 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2226 if (getCaseValue(i) == C)
2231 /// findCaseDest - Finds the unique case value for a given successor. Returns
2232 /// null if the successor is not found, not unique, or is the default case.
2233 ConstantInt *findCaseDest(BasicBlock *BB) {
2234 if (BB == getDefaultDest()) return NULL;
2236 ConstantInt *CI = NULL;
2237 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2238 if (getSuccessor(i) == BB) {
2239 if (CI) return NULL; // Multiple cases lead to BB.
2240 else CI = getCaseValue(i);
2246 /// addCase - Add an entry to the switch instruction...
2248 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2250 /// removeCase - This method removes the specified successor from the switch
2251 /// instruction. Note that this cannot be used to remove the default
2252 /// destination (successor #0).
2254 void removeCase(unsigned idx);
2256 virtual SwitchInst *clone(LLVMContext &Context) const;
2258 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2259 BasicBlock *getSuccessor(unsigned idx) const {
2260 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2261 return cast<BasicBlock>(getOperand(idx*2+1));
2263 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2264 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2265 setOperand(idx*2+1, NewSucc);
2268 // getSuccessorValue - Return the value associated with the specified
2270 ConstantInt *getSuccessorValue(unsigned idx) const {
2271 assert(idx < getNumSuccessors() && "Successor # out of range!");
2272 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2275 // Methods for support type inquiry through isa, cast, and dyn_cast:
2276 static inline bool classof(const SwitchInst *) { return true; }
2277 static inline bool classof(const Instruction *I) {
2278 return I->getOpcode() == Instruction::Switch;
2280 static inline bool classof(const Value *V) {
2281 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2284 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2285 virtual unsigned getNumSuccessorsV() const;
2286 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2290 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2293 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2296 //===----------------------------------------------------------------------===//
2298 //===----------------------------------------------------------------------===//
2300 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2301 /// calling convention of the call.
2303 class InvokeInst : public TerminatorInst {
2304 AttrListPtr AttributeList;
2305 InvokeInst(const InvokeInst &BI);
2306 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2307 Value* const *Args, unsigned NumArgs);
2309 template<typename InputIterator>
2310 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2311 InputIterator ArgBegin, InputIterator ArgEnd,
2312 const std::string &NameStr,
2313 // This argument ensures that we have an iterator we can
2314 // do arithmetic on in constant time
2315 std::random_access_iterator_tag) {
2316 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2318 // This requires that the iterator points to contiguous memory.
2319 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2323 /// Construct an InvokeInst given a range of arguments.
2324 /// InputIterator must be a random-access iterator pointing to
2325 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2326 /// made for random-accessness but not for contiguous storage as
2327 /// that would incur runtime overhead.
2329 /// @brief Construct an InvokeInst from a range of arguments
2330 template<typename InputIterator>
2331 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2332 InputIterator ArgBegin, InputIterator ArgEnd,
2334 const std::string &NameStr, Instruction *InsertBefore);
2336 /// Construct an InvokeInst given a range of arguments.
2337 /// InputIterator must be a random-access iterator pointing to
2338 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2339 /// made for random-accessness but not for contiguous storage as
2340 /// that would incur runtime overhead.
2342 /// @brief Construct an InvokeInst from a range of arguments
2343 template<typename InputIterator>
2344 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2345 InputIterator ArgBegin, InputIterator ArgEnd,
2347 const std::string &NameStr, BasicBlock *InsertAtEnd);
2349 template<typename InputIterator>
2350 static InvokeInst *Create(Value *Func,
2351 BasicBlock *IfNormal, BasicBlock *IfException,
2352 InputIterator ArgBegin, InputIterator ArgEnd,
2353 const std::string &NameStr = "",
2354 Instruction *InsertBefore = 0) {
2355 unsigned Values(ArgEnd - ArgBegin + 3);
2356 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2357 Values, NameStr, InsertBefore);
2359 template<typename InputIterator>
2360 static InvokeInst *Create(Value *Func,
2361 BasicBlock *IfNormal, BasicBlock *IfException,
2362 InputIterator ArgBegin, InputIterator ArgEnd,
2363 const std::string &NameStr,
2364 BasicBlock *InsertAtEnd) {
2365 unsigned Values(ArgEnd - ArgBegin + 3);
2366 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2367 Values, NameStr, InsertAtEnd);
2370 virtual InvokeInst *clone(LLVMContext &Context) const;
2372 /// Provide fast operand accessors
2373 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2375 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2377 unsigned getCallingConv() const { return SubclassData; }
2378 void setCallingConv(unsigned CC) {
2382 /// getAttributes - Return the parameter attributes for this invoke.
2384 const AttrListPtr &getAttributes() const { return AttributeList; }
2386 /// setAttributes - Set the parameter attributes for this invoke.
2388 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2390 /// addAttribute - adds the attribute to the list of attributes.
2391 void addAttribute(unsigned i, Attributes attr);
2393 /// removeAttribute - removes the attribute from the list of attributes.
2394 void removeAttribute(unsigned i, Attributes attr);
2396 /// @brief Determine whether the call or the callee has the given attribute.
2397 bool paramHasAttr(unsigned i, Attributes attr) const;
2399 /// @brief Extract the alignment for a call or parameter (0=unknown).
2400 unsigned getParamAlignment(unsigned i) const {
2401 return AttributeList.getParamAlignment(i);
2404 /// @brief Determine if the call does not access memory.
2405 bool doesNotAccessMemory() const {
2406 return paramHasAttr(0, Attribute::ReadNone);
2408 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2409 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2410 else removeAttribute(~0, Attribute::ReadNone);
2413 /// @brief Determine if the call does not access or only reads memory.
2414 bool onlyReadsMemory() const {
2415 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2417 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2418 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2419 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2422 /// @brief Determine if the call cannot return.
2423 bool doesNotReturn() const {
2424 return paramHasAttr(~0, Attribute::NoReturn);
2426 void setDoesNotReturn(bool DoesNotReturn = true) {
2427 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2428 else removeAttribute(~0, Attribute::NoReturn);
2431 /// @brief Determine if the call cannot unwind.
2432 bool doesNotThrow() const {
2433 return paramHasAttr(~0, Attribute::NoUnwind);
2435 void setDoesNotThrow(bool DoesNotThrow = true) {
2436 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2437 else removeAttribute(~0, Attribute::NoUnwind);
2440 /// @brief Determine if the call returns a structure through first
2441 /// pointer argument.
2442 bool hasStructRetAttr() const {
2443 // Be friendly and also check the callee.
2444 return paramHasAttr(1, Attribute::StructRet);
2447 /// @brief Determine if any call argument is an aggregate passed by value.
2448 bool hasByValArgument() const {
2449 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2452 /// getCalledFunction - Return the function called, or null if this is an
2453 /// indirect function invocation.
2455 Function *getCalledFunction() const {
2456 return dyn_cast<Function>(getOperand(0));
2459 /// getCalledValue - Get a pointer to the function that is invoked by this
2461 const Value *getCalledValue() const { return getOperand(0); }
2462 Value *getCalledValue() { return getOperand(0); }
2464 // get*Dest - Return the destination basic blocks...
2465 BasicBlock *getNormalDest() const {
2466 return cast<BasicBlock>(getOperand(1));
2468 BasicBlock *getUnwindDest() const {
2469 return cast<BasicBlock>(getOperand(2));
2471 void setNormalDest(BasicBlock *B) {
2475 void setUnwindDest(BasicBlock *B) {
2479 BasicBlock *getSuccessor(unsigned i) const {
2480 assert(i < 2 && "Successor # out of range for invoke!");
2481 return i == 0 ? getNormalDest() : getUnwindDest();
2484 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2485 assert(idx < 2 && "Successor # out of range for invoke!");
2486 setOperand(idx+1, NewSucc);
2489 unsigned getNumSuccessors() const { return 2; }
2491 // Methods for support type inquiry through isa, cast, and dyn_cast:
2492 static inline bool classof(const InvokeInst *) { return true; }
2493 static inline bool classof(const Instruction *I) {
2494 return (I->getOpcode() == Instruction::Invoke);
2496 static inline bool classof(const Value *V) {
2497 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2500 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2501 virtual unsigned getNumSuccessorsV() const;
2502 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2506 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2509 template<typename InputIterator>
2510 InvokeInst::InvokeInst(Value *Func,
2511 BasicBlock *IfNormal, BasicBlock *IfException,
2512 InputIterator ArgBegin, InputIterator ArgEnd,
2514 const std::string &NameStr, Instruction *InsertBefore)
2515 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2516 ->getElementType())->getReturnType(),
2517 Instruction::Invoke,
2518 OperandTraits<InvokeInst>::op_end(this) - Values,
2519 Values, InsertBefore) {
2520 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2521 typename std::iterator_traits<InputIterator>::iterator_category());
2523 template<typename InputIterator>
2524 InvokeInst::InvokeInst(Value *Func,
2525 BasicBlock *IfNormal, BasicBlock *IfException,
2526 InputIterator ArgBegin, InputIterator ArgEnd,
2528 const std::string &NameStr, BasicBlock *InsertAtEnd)
2529 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2530 ->getElementType())->getReturnType(),
2531 Instruction::Invoke,
2532 OperandTraits<InvokeInst>::op_end(this) - Values,
2533 Values, InsertAtEnd) {
2534 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2535 typename std::iterator_traits<InputIterator>::iterator_category());
2538 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2540 //===----------------------------------------------------------------------===//
2542 //===----------------------------------------------------------------------===//
2544 //===---------------------------------------------------------------------------
2545 /// UnwindInst - Immediately exit the current function, unwinding the stack
2546 /// until an invoke instruction is found.
2548 class UnwindInst : public TerminatorInst {
2549 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2551 // allocate space for exactly zero operands
2552 void *operator new(size_t s) {
2553 return User::operator new(s, 0);
2555 explicit UnwindInst(Instruction *InsertBefore = 0);
2556 explicit UnwindInst(BasicBlock *InsertAtEnd);
2558 virtual UnwindInst *clone(LLVMContext &Context) const;
2560 unsigned getNumSuccessors() const { return 0; }
2562 // Methods for support type inquiry through isa, cast, and dyn_cast:
2563 static inline bool classof(const UnwindInst *) { return true; }
2564 static inline bool classof(const Instruction *I) {
2565 return I->getOpcode() == Instruction::Unwind;
2567 static inline bool classof(const Value *V) {
2568 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2571 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2572 virtual unsigned getNumSuccessorsV() const;
2573 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2576 //===----------------------------------------------------------------------===//
2577 // UnreachableInst Class
2578 //===----------------------------------------------------------------------===//
2580 //===---------------------------------------------------------------------------
2581 /// UnreachableInst - This function has undefined behavior. In particular, the
2582 /// presence of this instruction indicates some higher level knowledge that the
2583 /// end of the block cannot be reached.
2585 class UnreachableInst : public TerminatorInst {
2586 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2588 // allocate space for exactly zero operands
2589 void *operator new(size_t s) {
2590 return User::operator new(s, 0);
2592 explicit UnreachableInst(Instruction *InsertBefore = 0);
2593 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2595 virtual UnreachableInst *clone(LLVMContext &Context) const;
2597 unsigned getNumSuccessors() const { return 0; }
2599 // Methods for support type inquiry through isa, cast, and dyn_cast:
2600 static inline bool classof(const UnreachableInst *) { return true; }
2601 static inline bool classof(const Instruction *I) {
2602 return I->getOpcode() == Instruction::Unreachable;
2604 static inline bool classof(const Value *V) {
2605 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2608 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2609 virtual unsigned getNumSuccessorsV() const;
2610 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2613 //===----------------------------------------------------------------------===//
2615 //===----------------------------------------------------------------------===//
2617 /// @brief This class represents a truncation of integer types.
2618 class TruncInst : public CastInst {
2619 /// Private copy constructor
2620 TruncInst(const TruncInst &CI)
2621 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2624 /// @brief Constructor with insert-before-instruction semantics
2626 Value *S, ///< The value to be truncated
2627 const Type *Ty, ///< The (smaller) type to truncate to
2628 const std::string &NameStr = "", ///< A name for the new instruction
2629 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2632 /// @brief Constructor with insert-at-end-of-block semantics
2634 Value *S, ///< The value to be truncated
2635 const Type *Ty, ///< The (smaller) type to truncate to
2636 const std::string &NameStr, ///< A name for the new instruction
2637 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2640 /// @brief Clone an identical TruncInst
2641 virtual CastInst *clone(LLVMContext &Context) const;
2643 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2644 static inline bool classof(const TruncInst *) { return true; }
2645 static inline bool classof(const Instruction *I) {
2646 return I->getOpcode() == Trunc;
2648 static inline bool classof(const Value *V) {
2649 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2653 //===----------------------------------------------------------------------===//
2655 //===----------------------------------------------------------------------===//
2657 /// @brief This class represents zero extension of integer types.
2658 class ZExtInst : public CastInst {
2659 /// @brief Private copy constructor
2660 ZExtInst(const ZExtInst &CI)
2661 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2664 /// @brief Constructor with insert-before-instruction semantics
2666 Value *S, ///< The value to be zero extended
2667 const Type *Ty, ///< The type to zero extend to
2668 const std::string &NameStr = "", ///< A name for the new instruction
2669 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2672 /// @brief Constructor with insert-at-end semantics.
2674 Value *S, ///< The value to be zero extended
2675 const Type *Ty, ///< The type to zero extend to
2676 const std::string &NameStr, ///< A name for the new instruction
2677 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2680 /// @brief Clone an identical ZExtInst
2681 virtual CastInst *clone(LLVMContext &Context) const;
2683 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2684 static inline bool classof(const ZExtInst *) { return true; }
2685 static inline bool classof(const Instruction *I) {
2686 return I->getOpcode() == ZExt;
2688 static inline bool classof(const Value *V) {
2689 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2693 //===----------------------------------------------------------------------===//
2695 //===----------------------------------------------------------------------===//
2697 /// @brief This class represents a sign extension of integer types.
2698 class SExtInst : public CastInst {
2699 /// @brief Private copy constructor
2700 SExtInst(const SExtInst &CI)
2701 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2704 /// @brief Constructor with insert-before-instruction semantics
2706 Value *S, ///< The value to be sign extended
2707 const Type *Ty, ///< The type to sign extend to
2708 const std::string &NameStr = "", ///< A name for the new instruction
2709 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2712 /// @brief Constructor with insert-at-end-of-block semantics
2714 Value *S, ///< The value to be sign extended
2715 const Type *Ty, ///< The type to sign extend to
2716 const std::string &NameStr, ///< A name for the new instruction
2717 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2720 /// @brief Clone an identical SExtInst
2721 virtual CastInst *clone(LLVMContext &Context) const;
2723 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2724 static inline bool classof(const SExtInst *) { return true; }
2725 static inline bool classof(const Instruction *I) {
2726 return I->getOpcode() == SExt;
2728 static inline bool classof(const Value *V) {
2729 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2733 //===----------------------------------------------------------------------===//
2734 // FPTruncInst Class
2735 //===----------------------------------------------------------------------===//
2737 /// @brief This class represents a truncation of floating point types.
2738 class FPTruncInst : public CastInst {
2739 FPTruncInst(const FPTruncInst &CI)
2740 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2743 /// @brief Constructor with insert-before-instruction semantics
2745 Value *S, ///< The value to be truncated
2746 const Type *Ty, ///< The type to truncate to
2747 const std::string &NameStr = "", ///< A name for the new instruction
2748 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2751 /// @brief Constructor with insert-before-instruction semantics
2753 Value *S, ///< The value to be truncated
2754 const Type *Ty, ///< The type to truncate to
2755 const std::string &NameStr, ///< A name for the new instruction
2756 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2759 /// @brief Clone an identical FPTruncInst
2760 virtual CastInst *clone(LLVMContext &Context) const;
2762 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2763 static inline bool classof(const FPTruncInst *) { return true; }
2764 static inline bool classof(const Instruction *I) {
2765 return I->getOpcode() == FPTrunc;
2767 static inline bool classof(const Value *V) {
2768 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2772 //===----------------------------------------------------------------------===//
2774 //===----------------------------------------------------------------------===//
2776 /// @brief This class represents an extension of floating point types.
2777 class FPExtInst : public CastInst {
2778 FPExtInst(const FPExtInst &CI)
2779 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2782 /// @brief Constructor with insert-before-instruction semantics
2784 Value *S, ///< The value to be extended
2785 const Type *Ty, ///< The type to extend to
2786 const std::string &NameStr = "", ///< A name for the new instruction
2787 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2790 /// @brief Constructor with insert-at-end-of-block semantics
2792 Value *S, ///< The value to be extended
2793 const Type *Ty, ///< The type to extend to
2794 const std::string &NameStr, ///< A name for the new instruction
2795 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2798 /// @brief Clone an identical FPExtInst
2799 virtual CastInst *clone(LLVMContext &Context) const;
2801 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2802 static inline bool classof(const FPExtInst *) { return true; }
2803 static inline bool classof(const Instruction *I) {
2804 return I->getOpcode() == FPExt;
2806 static inline bool classof(const Value *V) {
2807 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2811 //===----------------------------------------------------------------------===//
2813 //===----------------------------------------------------------------------===//
2815 /// @brief This class represents a cast unsigned integer to floating point.
2816 class UIToFPInst : public CastInst {
2817 UIToFPInst(const UIToFPInst &CI)
2818 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2821 /// @brief Constructor with insert-before-instruction semantics
2823 Value *S, ///< The value to be converted
2824 const Type *Ty, ///< The type to convert to
2825 const std::string &NameStr = "", ///< A name for the new instruction
2826 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2829 /// @brief Constructor with insert-at-end-of-block semantics
2831 Value *S, ///< The value to be converted
2832 const Type *Ty, ///< The type to convert to
2833 const std::string &NameStr, ///< A name for the new instruction
2834 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2837 /// @brief Clone an identical UIToFPInst
2838 virtual CastInst *clone(LLVMContext &Context) const;
2840 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2841 static inline bool classof(const UIToFPInst *) { return true; }
2842 static inline bool classof(const Instruction *I) {
2843 return I->getOpcode() == UIToFP;
2845 static inline bool classof(const Value *V) {
2846 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2850 //===----------------------------------------------------------------------===//
2852 //===----------------------------------------------------------------------===//
2854 /// @brief This class represents a cast from signed integer to floating point.
2855 class SIToFPInst : public CastInst {
2856 SIToFPInst(const SIToFPInst &CI)
2857 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2860 /// @brief Constructor with insert-before-instruction semantics
2862 Value *S, ///< The value to be converted
2863 const Type *Ty, ///< The type to convert to
2864 const std::string &NameStr = "", ///< A name for the new instruction
2865 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2868 /// @brief Constructor with insert-at-end-of-block semantics
2870 Value *S, ///< The value to be converted
2871 const Type *Ty, ///< The type to convert to
2872 const std::string &NameStr, ///< A name for the new instruction
2873 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2876 /// @brief Clone an identical SIToFPInst
2877 virtual CastInst *clone(LLVMContext &Context) const;
2879 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2880 static inline bool classof(const SIToFPInst *) { return true; }
2881 static inline bool classof(const Instruction *I) {
2882 return I->getOpcode() == SIToFP;
2884 static inline bool classof(const Value *V) {
2885 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2889 //===----------------------------------------------------------------------===//
2891 //===----------------------------------------------------------------------===//
2893 /// @brief This class represents a cast from floating point to unsigned integer
2894 class FPToUIInst : public CastInst {
2895 FPToUIInst(const FPToUIInst &CI)
2896 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2899 /// @brief Constructor with insert-before-instruction semantics
2901 Value *S, ///< The value to be converted
2902 const Type *Ty, ///< The type to convert to
2903 const std::string &NameStr = "", ///< A name for the new instruction
2904 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2907 /// @brief Constructor with insert-at-end-of-block semantics
2909 Value *S, ///< The value to be converted
2910 const Type *Ty, ///< The type to convert to
2911 const std::string &NameStr, ///< A name for the new instruction
2912 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2915 /// @brief Clone an identical FPToUIInst
2916 virtual CastInst *clone(LLVMContext &Context) const;
2918 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2919 static inline bool classof(const FPToUIInst *) { return true; }
2920 static inline bool classof(const Instruction *I) {
2921 return I->getOpcode() == FPToUI;
2923 static inline bool classof(const Value *V) {
2924 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2928 //===----------------------------------------------------------------------===//
2930 //===----------------------------------------------------------------------===//
2932 /// @brief This class represents a cast from floating point to signed integer.
2933 class FPToSIInst : public CastInst {
2934 FPToSIInst(const FPToSIInst &CI)
2935 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2938 /// @brief Constructor with insert-before-instruction semantics
2940 Value *S, ///< The value to be converted
2941 const Type *Ty, ///< The type to convert to
2942 const std::string &NameStr = "", ///< A name for the new instruction
2943 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2946 /// @brief Constructor with insert-at-end-of-block semantics
2948 Value *S, ///< The value to be converted
2949 const Type *Ty, ///< The type to convert to
2950 const std::string &NameStr, ///< A name for the new instruction
2951 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2954 /// @brief Clone an identical FPToSIInst
2955 virtual CastInst *clone(LLVMContext &Context) const;
2957 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2958 static inline bool classof(const FPToSIInst *) { return true; }
2959 static inline bool classof(const Instruction *I) {
2960 return I->getOpcode() == FPToSI;
2962 static inline bool classof(const Value *V) {
2963 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2967 //===----------------------------------------------------------------------===//
2968 // IntToPtrInst Class
2969 //===----------------------------------------------------------------------===//
2971 /// @brief This class represents a cast from an integer to a pointer.
2972 class IntToPtrInst : public CastInst {
2973 IntToPtrInst(const IntToPtrInst &CI)
2974 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2977 /// @brief Constructor with insert-before-instruction semantics
2979 Value *S, ///< The value to be converted
2980 const Type *Ty, ///< The type to convert to
2981 const std::string &NameStr = "", ///< A name for the new instruction
2982 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2985 /// @brief Constructor with insert-at-end-of-block semantics
2987 Value *S, ///< The value to be converted
2988 const Type *Ty, ///< The type to convert to
2989 const std::string &NameStr, ///< A name for the new instruction
2990 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2993 /// @brief Clone an identical IntToPtrInst
2994 virtual CastInst *clone(LLVMContext &Context) const;
2996 // Methods for support type inquiry through isa, cast, and dyn_cast:
2997 static inline bool classof(const IntToPtrInst *) { return true; }
2998 static inline bool classof(const Instruction *I) {
2999 return I->getOpcode() == IntToPtr;
3001 static inline bool classof(const Value *V) {
3002 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3006 //===----------------------------------------------------------------------===//
3007 // PtrToIntInst Class
3008 //===----------------------------------------------------------------------===//
3010 /// @brief This class represents a cast from a pointer to an integer
3011 class PtrToIntInst : public CastInst {
3012 PtrToIntInst(const PtrToIntInst &CI)
3013 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3016 /// @brief Constructor with insert-before-instruction semantics
3018 Value *S, ///< The value to be converted
3019 const Type *Ty, ///< The type to convert to
3020 const std::string &NameStr = "", ///< A name for the new instruction
3021 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3024 /// @brief Constructor with insert-at-end-of-block semantics
3026 Value *S, ///< The value to be converted
3027 const Type *Ty, ///< The type to convert to
3028 const std::string &NameStr, ///< A name for the new instruction
3029 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3032 /// @brief Clone an identical PtrToIntInst
3033 virtual CastInst *clone(LLVMContext &Context) const;
3035 // Methods for support type inquiry through isa, cast, and dyn_cast:
3036 static inline bool classof(const PtrToIntInst *) { return true; }
3037 static inline bool classof(const Instruction *I) {
3038 return I->getOpcode() == PtrToInt;
3040 static inline bool classof(const Value *V) {
3041 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3045 //===----------------------------------------------------------------------===//
3046 // BitCastInst Class
3047 //===----------------------------------------------------------------------===//
3049 /// @brief This class represents a no-op cast from one type to another.
3050 class BitCastInst : public CastInst {
3051 BitCastInst(const BitCastInst &CI)
3052 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3055 /// @brief Constructor with insert-before-instruction semantics
3057 Value *S, ///< The value to be casted
3058 const Type *Ty, ///< The type to casted to
3059 const std::string &NameStr = "", ///< A name for the new instruction
3060 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3063 /// @brief Constructor with insert-at-end-of-block semantics
3065 Value *S, ///< The value to be casted
3066 const Type *Ty, ///< The type to casted to
3067 const std::string &NameStr, ///< A name for the new instruction
3068 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3071 /// @brief Clone an identical BitCastInst
3072 virtual CastInst *clone(LLVMContext &Context) const;
3074 // Methods for support type inquiry through isa, cast, and dyn_cast:
3075 static inline bool classof(const BitCastInst *) { return true; }
3076 static inline bool classof(const Instruction *I) {
3077 return I->getOpcode() == BitCast;
3079 static inline bool classof(const Value *V) {
3080 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3084 } // End llvm namespace