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"
34 //===----------------------------------------------------------------------===//
35 // AllocationInst Class
36 //===----------------------------------------------------------------------===//
38 /// AllocationInst - This class is the common base class of MallocInst and
41 class AllocationInst : public UnaryInstruction {
43 AllocationInst(const Type *Ty, Value *ArraySize,
44 unsigned iTy, unsigned Align, const std::string &Name = "",
45 Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize,
47 unsigned iTy, unsigned Align, const std::string &Name,
48 BasicBlock *InsertAtEnd);
50 // Out of line virtual method, so the vtable, etc. has a home.
51 virtual ~AllocationInst();
53 /// isArrayAllocation - Return true if there is an allocation size parameter
54 /// to the allocation instruction that is not 1.
56 bool isArrayAllocation() const;
58 /// getArraySize - Get the number of elements allocated. For a simple
59 /// allocation of a single element, this will return a constant 1 value.
61 const Value *getArraySize() const { return getOperand(0); }
62 Value *getArraySize() { return getOperand(0); }
64 /// getType - Overload to return most specific pointer type
66 const PointerType *getType() const {
67 return reinterpret_cast<const PointerType*>(Instruction::getType());
70 /// getAllocatedType - Return the type that is being allocated by the
73 const Type *getAllocatedType() const;
75 /// getAlignment - Return the alignment of the memory that is being allocated
76 /// by the instruction.
78 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
79 void setAlignment(unsigned Align);
81 virtual Instruction *clone(LLVMContext &Context) const = 0;
83 // Methods for support type inquiry through isa, cast, and dyn_cast:
84 static inline bool classof(const AllocationInst *) { return true; }
85 static inline bool classof(const Instruction *I) {
86 return I->getOpcode() == Instruction::Alloca ||
87 I->getOpcode() == Instruction::Malloc;
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) && classof(cast<Instruction>(V));
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 /// MallocInst - an instruction to allocated memory on the heap
101 class MallocInst : public AllocationInst {
102 MallocInst(const MallocInst &MI);
104 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
105 const std::string &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc,
108 0, NameStr, InsertBefore) {}
109 MallocInst(const Type *Ty, Value *ArraySize,
110 const std::string &NameStr, BasicBlock *InsertAtEnd)
111 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
113 MallocInst(const Type *Ty, const std::string &NameStr,
114 Instruction *InsertBefore = 0)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
116 MallocInst(const Type *Ty, const std::string &NameStr,
117 BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize,
121 unsigned Align, const std::string &NameStr,
122 BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc,
124 Align, NameStr, InsertAtEnd) {}
125 MallocInst(const Type *Ty, Value *ArraySize,
126 unsigned Align, const std::string &NameStr = "",
127 Instruction *InsertBefore = 0)
128 : AllocationInst(Ty, ArraySize,
129 Malloc, Align, NameStr, InsertBefore) {}
131 virtual MallocInst *clone(LLVMContext &Context) const;
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const MallocInst *) { return true; }
135 static inline bool classof(const Instruction *I) {
136 return (I->getOpcode() == Instruction::Malloc);
138 static inline bool classof(const Value *V) {
139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
144 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
148 /// AllocaInst - an instruction to allocate memory on the stack
150 class AllocaInst : public AllocationInst {
151 AllocaInst(const AllocaInst &);
153 explicit AllocaInst(const Type *Ty,
154 Value *ArraySize = 0,
155 const std::string &NameStr = "",
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, ArraySize, Alloca,
158 0, NameStr, InsertBefore) {}
159 AllocaInst(const Type *Ty,
160 Value *ArraySize, const std::string &NameStr,
161 BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, const std::string &NameStr,
165 Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167 AllocaInst(const Type *Ty, const std::string &NameStr,
168 BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
171 AllocaInst(const Type *Ty, Value *ArraySize,
172 unsigned Align, const std::string &NameStr = "",
173 Instruction *InsertBefore = 0)
174 : AllocationInst(Ty, ArraySize, Alloca,
175 Align, NameStr, InsertBefore) {}
176 AllocaInst(const Type *Ty, Value *ArraySize,
177 unsigned Align, const std::string &NameStr,
178 BasicBlock *InsertAtEnd)
179 : AllocationInst(Ty, ArraySize, Alloca,
180 Align, NameStr, InsertAtEnd) {}
182 virtual AllocaInst *clone(LLVMContext &Context) const;
184 /// isStaticAlloca - Return true if this alloca is in the entry block of the
185 /// function and is a constant size. If so, the code generator will fold it
186 /// into the prolog/epilog code, so it is basically free.
187 bool isStaticAlloca() const;
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const AllocaInst *) { return true; }
191 static inline bool classof(const Instruction *I) {
192 return (I->getOpcode() == Instruction::Alloca);
194 static inline bool classof(const Value *V) {
195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
200 //===----------------------------------------------------------------------===//
202 //===----------------------------------------------------------------------===//
204 /// FreeInst - an instruction to deallocate memory
206 class FreeInst : public UnaryInstruction {
209 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
210 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
212 virtual FreeInst *clone(LLVMContext &Context) const;
214 // Accessor methods for consistency with other memory operations
215 Value *getPointerOperand() { return getOperand(0); }
216 const Value *getPointerOperand() const { return getOperand(0); }
218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const FreeInst *) { return true; }
220 static inline bool classof(const Instruction *I) {
221 return (I->getOpcode() == Instruction::Free);
223 static inline bool classof(const Value *V) {
224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
229 //===----------------------------------------------------------------------===//
231 //===----------------------------------------------------------------------===//
233 /// LoadInst - an instruction for reading from memory. This uses the
234 /// SubclassData field in Value to store whether or not the load is volatile.
236 class LoadInst : public UnaryInstruction {
238 LoadInst(const LoadInst &LI)
239 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
240 setVolatile(LI.isVolatile());
241 setAlignment(LI.getAlignment());
249 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
250 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
251 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
252 Instruction *InsertBefore = 0);
253 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
254 unsigned Align, Instruction *InsertBefore = 0);
255 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
256 BasicBlock *InsertAtEnd);
257 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
258 unsigned Align, BasicBlock *InsertAtEnd);
260 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
261 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
262 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
263 bool isVolatile = false, Instruction *InsertBefore = 0);
264 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
265 BasicBlock *InsertAtEnd);
267 /// isVolatile - Return true if this is a load from a volatile memory
270 bool isVolatile() const { return SubclassData & 1; }
272 /// setVolatile - Specify whether this is a volatile load or not.
274 void setVolatile(bool V) {
275 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
278 virtual LoadInst *clone(LLVMContext &Context) const;
280 /// getAlignment - Return the alignment of the access that is being performed
282 unsigned getAlignment() const {
283 return (1 << (SubclassData>>1)) >> 1;
286 void setAlignment(unsigned Align);
288 Value *getPointerOperand() { return getOperand(0); }
289 const Value *getPointerOperand() const { return getOperand(0); }
290 static unsigned getPointerOperandIndex() { return 0U; }
292 // Methods for support type inquiry through isa, cast, and dyn_cast:
293 static inline bool classof(const LoadInst *) { return true; }
294 static inline bool classof(const Instruction *I) {
295 return I->getOpcode() == Instruction::Load;
297 static inline bool classof(const Value *V) {
298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
303 //===----------------------------------------------------------------------===//
305 //===----------------------------------------------------------------------===//
307 /// StoreInst - an instruction for storing to memory
309 class StoreInst : public Instruction {
310 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
312 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
314 Op<0>() = SI.Op<0>();
315 Op<1>() = SI.Op<1>();
316 setVolatile(SI.isVolatile());
317 setAlignment(SI.getAlignment());
325 // allocate space for exactly two operands
326 void *operator new(size_t s) {
327 return User::operator new(s, 2);
329 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
330 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
331 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
332 Instruction *InsertBefore = 0);
333 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
334 unsigned Align, Instruction *InsertBefore = 0);
335 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
336 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
337 unsigned Align, BasicBlock *InsertAtEnd);
340 /// isVolatile - Return true if this is a load from a volatile memory
343 bool isVolatile() const { return SubclassData & 1; }
345 /// setVolatile - Specify whether this is a volatile load or not.
347 void setVolatile(bool V) {
348 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
351 /// Transparently provide more efficient getOperand methods.
352 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
354 /// getAlignment - Return the alignment of the access that is being performed
356 unsigned getAlignment() const {
357 return (1 << (SubclassData>>1)) >> 1;
360 void setAlignment(unsigned Align);
362 virtual StoreInst *clone(LLVMContext &Context) const;
364 Value *getPointerOperand() { return getOperand(1); }
365 const Value *getPointerOperand() const { return getOperand(1); }
366 static unsigned getPointerOperandIndex() { return 1U; }
368 // Methods for support type inquiry through isa, cast, and dyn_cast:
369 static inline bool classof(const StoreInst *) { return true; }
370 static inline bool classof(const Instruction *I) {
371 return I->getOpcode() == Instruction::Store;
373 static inline bool classof(const Value *V) {
374 return isa<Instruction>(V) && classof(cast<Instruction>(V));
379 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
382 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
384 //===----------------------------------------------------------------------===//
385 // GetElementPtrInst Class
386 //===----------------------------------------------------------------------===//
388 // checkType - Simple wrapper function to give a better assertion failure
389 // message on bad indexes for a gep instruction.
391 static inline const Type *checkType(const Type *Ty) {
392 assert(Ty && "Invalid GetElementPtrInst indices for type!");
396 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
397 /// access elements of arrays and structs
399 class GetElementPtrInst : public Instruction {
400 GetElementPtrInst(const GetElementPtrInst &GEPI);
401 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
402 const std::string &NameStr);
403 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
405 template<typename InputIterator>
406 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
407 const std::string &NameStr,
408 // This argument ensures that we have an iterator we can
409 // do arithmetic on in constant time
410 std::random_access_iterator_tag) {
411 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
414 // This requires that the iterator points to contiguous memory.
415 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
416 // we have to build an array here
419 init(Ptr, 0, NumIdx, NameStr);
423 /// getIndexedType - Returns the type of the element that would be loaded with
424 /// a load instruction with the specified parameters.
426 /// Null is returned if the indices are invalid for the specified
429 template<typename InputIterator>
430 static const Type *getIndexedType(const Type *Ptr,
431 InputIterator IdxBegin,
432 InputIterator IdxEnd,
433 // This argument ensures that we
434 // have an iterator we can do
435 // arithmetic on in constant time
436 std::random_access_iterator_tag) {
437 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
440 // This requires that the iterator points to contiguous memory.
441 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
443 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
446 /// Constructors - Create a getelementptr instruction with a base pointer an
447 /// list of indices. The first ctor can optionally insert before an existing
448 /// instruction, the second appends the new instruction to the specified
450 template<typename InputIterator>
451 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
452 InputIterator IdxEnd,
454 const std::string &NameStr,
455 Instruction *InsertBefore);
456 template<typename InputIterator>
457 inline GetElementPtrInst(Value *Ptr,
458 InputIterator IdxBegin, InputIterator IdxEnd,
460 const std::string &NameStr, BasicBlock *InsertAtEnd);
462 /// Constructors - These two constructors are convenience methods because one
463 /// and two index getelementptr instructions are so common.
464 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
465 Instruction *InsertBefore = 0);
466 GetElementPtrInst(Value *Ptr, Value *Idx,
467 const std::string &NameStr, BasicBlock *InsertAtEnd);
469 template<typename InputIterator>
470 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
471 InputIterator IdxEnd,
472 const std::string &NameStr = "",
473 Instruction *InsertBefore = 0) {
474 typename std::iterator_traits<InputIterator>::difference_type Values =
475 1 + std::distance(IdxBegin, IdxEnd);
477 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
479 template<typename InputIterator>
480 static GetElementPtrInst *Create(Value *Ptr,
481 InputIterator IdxBegin, InputIterator IdxEnd,
482 const std::string &NameStr,
483 BasicBlock *InsertAtEnd) {
484 typename std::iterator_traits<InputIterator>::difference_type Values =
485 1 + std::distance(IdxBegin, IdxEnd);
487 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
490 /// Constructors - These two creators are convenience methods because one
491 /// index getelementptr instructions are so common.
492 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
493 const std::string &NameStr = "",
494 Instruction *InsertBefore = 0) {
495 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
497 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
498 const std::string &NameStr,
499 BasicBlock *InsertAtEnd) {
500 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
503 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
505 /// Transparently provide more efficient getOperand methods.
506 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
508 // getType - Overload to return most specific pointer type...
509 const PointerType *getType() const {
510 return reinterpret_cast<const PointerType*>(Instruction::getType());
513 /// getIndexedType - Returns the type of the element that would be loaded with
514 /// a load instruction with the specified parameters.
516 /// Null is returned if the indices are invalid for the specified
519 template<typename InputIterator>
520 static const Type *getIndexedType(const Type *Ptr,
521 InputIterator IdxBegin,
522 InputIterator IdxEnd) {
523 return getIndexedType(Ptr, IdxBegin, IdxEnd,
524 typename std::iterator_traits<InputIterator>::
525 iterator_category());
528 static const Type *getIndexedType(const Type *Ptr,
529 Value* const *Idx, unsigned NumIdx);
531 static const Type *getIndexedType(const Type *Ptr,
532 uint64_t const *Idx, unsigned NumIdx);
534 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
536 inline op_iterator idx_begin() { return op_begin()+1; }
537 inline const_op_iterator idx_begin() const { return op_begin()+1; }
538 inline op_iterator idx_end() { return op_end(); }
539 inline const_op_iterator idx_end() const { return op_end(); }
541 Value *getPointerOperand() {
542 return getOperand(0);
544 const Value *getPointerOperand() const {
545 return getOperand(0);
547 static unsigned getPointerOperandIndex() {
548 return 0U; // get index for modifying correct operand
551 /// getPointerOperandType - Method to return the pointer operand as a
553 const PointerType *getPointerOperandType() const {
554 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
558 unsigned getNumIndices() const { // Note: always non-negative
559 return getNumOperands() - 1;
562 bool hasIndices() const {
563 return getNumOperands() > 1;
566 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
567 /// zeros. If so, the result pointer and the first operand have the same
568 /// value, just potentially different types.
569 bool hasAllZeroIndices() const;
571 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
572 /// constant integers. If so, the result pointer and the first operand have
573 /// a constant offset between them.
574 bool hasAllConstantIndices() const;
576 // Methods for support type inquiry through isa, cast, and dyn_cast:
577 static inline bool classof(const GetElementPtrInst *) { return true; }
578 static inline bool classof(const Instruction *I) {
579 return (I->getOpcode() == Instruction::GetElementPtr);
581 static inline bool classof(const Value *V) {
582 return isa<Instruction>(V) && classof(cast<Instruction>(V));
587 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
590 template<typename InputIterator>
591 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
592 InputIterator IdxBegin,
593 InputIterator IdxEnd,
595 const std::string &NameStr,
596 Instruction *InsertBefore)
597 : Instruction(PointerType::get(checkType(
598 getIndexedType(Ptr->getType(),
600 cast<PointerType>(Ptr->getType())
601 ->getAddressSpace()),
603 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
604 Values, InsertBefore) {
605 init(Ptr, IdxBegin, IdxEnd, NameStr,
606 typename std::iterator_traits<InputIterator>::iterator_category());
608 template<typename InputIterator>
609 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
610 InputIterator IdxBegin,
611 InputIterator IdxEnd,
613 const std::string &NameStr,
614 BasicBlock *InsertAtEnd)
615 : Instruction(PointerType::get(checkType(
616 getIndexedType(Ptr->getType(),
618 cast<PointerType>(Ptr->getType())
619 ->getAddressSpace()),
621 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
622 Values, InsertAtEnd) {
623 init(Ptr, IdxBegin, IdxEnd, NameStr,
624 typename std::iterator_traits<InputIterator>::iterator_category());
628 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
631 //===----------------------------------------------------------------------===//
633 //===----------------------------------------------------------------------===//
635 /// This instruction compares its operands according to the predicate given
636 /// to the constructor. It only operates on integers or pointers. The operands
637 /// must be identical types.
638 /// @brief Represent an integer comparison operator.
639 class ICmpInst: public CmpInst {
641 /// @brief Constructor with insert-before-instruction semantics.
643 Instruction *InsertBefore, ///< Where to insert
644 Predicate pred, ///< The predicate to use for the comparison
645 Value *LHS, ///< The left-hand-side of the expression
646 Value *RHS, ///< The right-hand-side of the expression
647 const std::string &NameStr = "" ///< Name of the instruction
648 ) : CmpInst(InsertBefore->getContext().makeCmpResultType(LHS->getType()),
649 Instruction::ICmp, pred, LHS, RHS, NameStr,
651 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
652 pred <= CmpInst::LAST_ICMP_PREDICATE &&
653 "Invalid ICmp predicate value");
654 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
655 "Both operands to ICmp instruction are not of the same type!");
656 // Check that the operands are the right type
657 assert((getOperand(0)->getType()->isIntOrIntVector() ||
658 isa<PointerType>(getOperand(0)->getType())) &&
659 "Invalid operand types for ICmp instruction");
662 /// @brief Constructor with insert-at-end semantics.
664 BasicBlock &InsertAtEnd, ///< Block to insert into.
665 Predicate pred, ///< The predicate to use for the comparison
666 Value *LHS, ///< The left-hand-side of the expression
667 Value *RHS, ///< The right-hand-side of the expression
668 const std::string &NameStr = "" ///< Name of the instruction
669 ) : CmpInst(InsertAtEnd.getContext().makeCmpResultType(LHS->getType()),
670 Instruction::ICmp, pred, LHS, RHS, NameStr,
672 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
673 pred <= CmpInst::LAST_ICMP_PREDICATE &&
674 "Invalid ICmp predicate value");
675 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
676 "Both operands to ICmp instruction are not of the same type!");
677 // Check that the operands are the right type
678 assert((getOperand(0)->getType()->isIntOrIntVector() ||
679 isa<PointerType>(getOperand(0)->getType())) &&
680 "Invalid operand types for ICmp instruction");
683 /// @brief Constructor with no-insertion semantics
685 LLVMContext &Context, ///< Context to construct within
686 Predicate pred, ///< The predicate to use for the comparison
687 Value *LHS, ///< The left-hand-side of the expression
688 Value *RHS, ///< The right-hand-side of the expression
689 const std::string &NameStr = "" ///< Name of the instruction
690 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
691 Instruction::ICmp, pred, LHS, RHS, NameStr) {
692 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
693 pred <= CmpInst::LAST_ICMP_PREDICATE &&
694 "Invalid ICmp predicate value");
695 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
696 "Both operands to ICmp instruction are not of the same type!");
697 // Check that the operands are the right type
698 assert((getOperand(0)->getType()->isIntOrIntVector() ||
699 isa<PointerType>(getOperand(0)->getType())) &&
700 "Invalid operand types for ICmp instruction");
703 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
704 /// @returns the predicate that would be the result if the operand were
705 /// regarded as signed.
706 /// @brief Return the signed version of the predicate
707 Predicate getSignedPredicate() const {
708 return getSignedPredicate(getPredicate());
711 /// This is a static version that you can use without an instruction.
712 /// @brief Return the signed version of the predicate.
713 static Predicate getSignedPredicate(Predicate pred);
715 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
716 /// @returns the predicate that would be the result if the operand were
717 /// regarded as unsigned.
718 /// @brief Return the unsigned version of the predicate
719 Predicate getUnsignedPredicate() const {
720 return getUnsignedPredicate(getPredicate());
723 /// This is a static version that you can use without an instruction.
724 /// @brief Return the unsigned version of the predicate.
725 static Predicate getUnsignedPredicate(Predicate pred);
727 /// isEquality - Return true if this predicate is either EQ or NE. This also
728 /// tests for commutativity.
729 static bool isEquality(Predicate P) {
730 return P == ICMP_EQ || P == ICMP_NE;
733 /// isEquality - Return true if this predicate is either EQ or NE. This also
734 /// tests for commutativity.
735 bool isEquality() const {
736 return isEquality(getPredicate());
739 /// @returns true if the predicate of this ICmpInst is commutative
740 /// @brief Determine if this relation is commutative.
741 bool isCommutative() const { return isEquality(); }
743 /// isRelational - Return true if the predicate is relational (not EQ or NE).
745 bool isRelational() const {
746 return !isEquality();
749 /// isRelational - Return true if the predicate is relational (not EQ or NE).
751 static bool isRelational(Predicate P) {
752 return !isEquality(P);
755 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
756 /// @brief Determine if this instruction's predicate is signed.
757 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
759 /// @returns true if the predicate provided is signed, false otherwise
760 /// @brief Determine if the predicate is signed.
761 static bool isSignedPredicate(Predicate pred);
763 /// @returns true if the specified compare predicate is
764 /// true when both operands are equal...
765 /// @brief Determine if the icmp is true when both operands are equal
766 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
767 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
768 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
769 pred == ICmpInst::ICMP_SLE;
772 /// @returns true if the specified compare instruction is
773 /// true when both operands are equal...
774 /// @brief Determine if the ICmpInst returns true when both operands are equal
775 bool isTrueWhenEqual() {
776 return isTrueWhenEqual(getPredicate());
779 /// Initialize a set of values that all satisfy the predicate with C.
780 /// @brief Make a ConstantRange for a relation with a constant value.
781 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
783 /// Exchange the two operands to this instruction in such a way that it does
784 /// not modify the semantics of the instruction. The predicate value may be
785 /// changed to retain the same result if the predicate is order dependent
787 /// @brief Swap operands and adjust predicate.
788 void swapOperands() {
789 SubclassData = getSwappedPredicate();
790 Op<0>().swap(Op<1>());
793 virtual ICmpInst *clone(LLVMContext &Context) const;
795 // Methods for support type inquiry through isa, cast, and dyn_cast:
796 static inline bool classof(const ICmpInst *) { return true; }
797 static inline bool classof(const Instruction *I) {
798 return I->getOpcode() == Instruction::ICmp;
800 static inline bool classof(const Value *V) {
801 return isa<Instruction>(V) && classof(cast<Instruction>(V));
806 //===----------------------------------------------------------------------===//
808 //===----------------------------------------------------------------------===//
810 /// This instruction compares its operands according to the predicate given
811 /// to the constructor. It only operates on floating point values or packed
812 /// vectors of floating point values. The operands must be identical types.
813 /// @brief Represents a floating point comparison operator.
814 class FCmpInst: public CmpInst {
816 /// @brief Constructor with insert-before-instruction semantics.
818 Instruction *InsertBefore, ///< Where to insert
819 Predicate pred, ///< The predicate to use for the comparison
820 Value *LHS, ///< The left-hand-side of the expression
821 Value *RHS, ///< The right-hand-side of the expression
822 const std::string &NameStr = "" ///< Name of the instruction
823 ) : CmpInst(InsertBefore->getContext().makeCmpResultType(LHS->getType()),
824 Instruction::FCmp, pred, LHS, RHS, NameStr,
826 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
827 "Invalid FCmp predicate value");
828 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
829 "Both operands to FCmp instruction are not of the same type!");
830 // Check that the operands are the right type
831 assert(getOperand(0)->getType()->isFPOrFPVector() &&
832 "Invalid operand types for FCmp instruction");
835 /// @brief Constructor with insert-at-end semantics.
837 BasicBlock &InsertAtEnd, ///< Block to insert into.
838 Predicate pred, ///< The predicate to use for the comparison
839 Value *LHS, ///< The left-hand-side of the expression
840 Value *RHS, ///< The right-hand-side of the expression
841 const std::string &NameStr = "" ///< Name of the instruction
842 ) : CmpInst(InsertAtEnd.getContext().makeCmpResultType(LHS->getType()),
843 Instruction::FCmp, pred, LHS, RHS, NameStr,
845 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
846 "Invalid FCmp predicate value");
847 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
848 "Both operands to FCmp instruction are not of the same type!");
849 // Check that the operands are the right type
850 assert(getOperand(0)->getType()->isFPOrFPVector() &&
851 "Invalid operand types for FCmp instruction");
854 /// @brief Constructor with no-insertion semantics
856 LLVMContext &Context, ///< Context to build in
857 Predicate pred, ///< The predicate to use for the comparison
858 Value *LHS, ///< The left-hand-side of the expression
859 Value *RHS, ///< The right-hand-side of the expression
860 const std::string &NameStr = "" ///< Name of the instruction
861 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
862 Instruction::FCmp, pred, LHS, RHS, NameStr) {
863 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
864 "Invalid FCmp predicate value");
865 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
866 "Both operands to FCmp instruction are not of the same type!");
867 // Check that the operands are the right type
868 assert(getOperand(0)->getType()->isFPOrFPVector() &&
869 "Invalid operand types for FCmp instruction");
872 /// @returns true if the predicate of this instruction is EQ or NE.
873 /// @brief Determine if this is an equality predicate.
874 bool isEquality() const {
875 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
876 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
879 /// @returns true if the predicate of this instruction is commutative.
880 /// @brief Determine if this is a commutative predicate.
881 bool isCommutative() const {
882 return isEquality() ||
883 SubclassData == FCMP_FALSE ||
884 SubclassData == FCMP_TRUE ||
885 SubclassData == FCMP_ORD ||
886 SubclassData == FCMP_UNO;
889 /// @returns true if the predicate is relational (not EQ or NE).
890 /// @brief Determine if this a relational predicate.
891 bool isRelational() const { return !isEquality(); }
893 /// Exchange the two operands to this instruction in such a way that it does
894 /// not modify the semantics of the instruction. The predicate value may be
895 /// changed to retain the same result if the predicate is order dependent
897 /// @brief Swap operands and adjust predicate.
898 void swapOperands() {
899 SubclassData = getSwappedPredicate();
900 Op<0>().swap(Op<1>());
903 virtual FCmpInst *clone(LLVMContext &Context) const;
905 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
906 static inline bool classof(const FCmpInst *) { return true; }
907 static inline bool classof(const Instruction *I) {
908 return I->getOpcode() == Instruction::FCmp;
910 static inline bool classof(const Value *V) {
911 return isa<Instruction>(V) && classof(cast<Instruction>(V));
916 //===----------------------------------------------------------------------===//
918 //===----------------------------------------------------------------------===//
919 /// CallInst - This class represents a function call, abstracting a target
920 /// machine's calling convention. This class uses low bit of the SubClassData
921 /// field to indicate whether or not this is a tail call. The rest of the bits
922 /// hold the calling convention of the call.
925 class CallInst : public Instruction {
926 AttrListPtr AttributeList; ///< parameter attributes for call
927 CallInst(const CallInst &CI);
928 void init(Value *Func, Value* const *Params, unsigned NumParams);
929 void init(Value *Func, Value *Actual1, Value *Actual2);
930 void init(Value *Func, Value *Actual);
931 void init(Value *Func);
933 template<typename InputIterator>
934 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
935 const std::string &NameStr,
936 // This argument ensures that we have an iterator we can
937 // do arithmetic on in constant time
938 std::random_access_iterator_tag) {
939 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
941 // This requires that the iterator points to contiguous memory.
942 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
946 /// Construct a CallInst given a range of arguments. InputIterator
947 /// must be a random-access iterator pointing to contiguous storage
948 /// (e.g. a std::vector<>::iterator). Checks are made for
949 /// random-accessness but not for contiguous storage as that would
950 /// incur runtime overhead.
951 /// @brief Construct a CallInst from a range of arguments
952 template<typename InputIterator>
953 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
954 const std::string &NameStr, Instruction *InsertBefore);
956 /// Construct a CallInst given a range of arguments. InputIterator
957 /// must be a random-access iterator pointing to contiguous storage
958 /// (e.g. a std::vector<>::iterator). Checks are made for
959 /// random-accessness but not for contiguous storage as that would
960 /// incur runtime overhead.
961 /// @brief Construct a CallInst from a range of arguments
962 template<typename InputIterator>
963 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
964 const std::string &NameStr, BasicBlock *InsertAtEnd);
966 CallInst(Value *F, Value *Actual, const std::string& NameStr,
967 Instruction *InsertBefore);
968 CallInst(Value *F, Value *Actual, const std::string& NameStr,
969 BasicBlock *InsertAtEnd);
970 explicit CallInst(Value *F, const std::string &NameStr,
971 Instruction *InsertBefore);
972 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
974 template<typename InputIterator>
975 static CallInst *Create(Value *Func,
976 InputIterator ArgBegin, InputIterator ArgEnd,
977 const std::string &NameStr = "",
978 Instruction *InsertBefore = 0) {
979 return new((unsigned)(ArgEnd - ArgBegin + 1))
980 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
982 template<typename InputIterator>
983 static CallInst *Create(Value *Func,
984 InputIterator ArgBegin, InputIterator ArgEnd,
985 const std::string &NameStr, BasicBlock *InsertAtEnd) {
986 return new((unsigned)(ArgEnd - ArgBegin + 1))
987 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
989 static CallInst *Create(Value *F, Value *Actual,
990 const std::string& NameStr = "",
991 Instruction *InsertBefore = 0) {
992 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
994 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
995 BasicBlock *InsertAtEnd) {
996 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
998 static CallInst *Create(Value *F, const std::string &NameStr = "",
999 Instruction *InsertBefore = 0) {
1000 return new(1) CallInst(F, NameStr, InsertBefore);
1002 static CallInst *Create(Value *F, const std::string &NameStr,
1003 BasicBlock *InsertAtEnd) {
1004 return new(1) CallInst(F, NameStr, InsertAtEnd);
1009 bool isTailCall() const { return SubclassData & 1; }
1010 void setTailCall(bool isTC = true) {
1011 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1014 virtual CallInst *clone(LLVMContext &Context) const;
1016 /// Provide fast operand accessors
1017 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1019 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1021 unsigned getCallingConv() const { return SubclassData >> 1; }
1022 void setCallingConv(unsigned CC) {
1023 SubclassData = (SubclassData & 1) | (CC << 1);
1026 /// getAttributes - Return the parameter attributes for this call.
1028 const AttrListPtr &getAttributes() const { return AttributeList; }
1030 /// setAttributes - Set the parameter attributes for this call.
1032 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1034 /// addAttribute - adds the attribute to the list of attributes.
1035 void addAttribute(unsigned i, Attributes attr);
1037 /// removeAttribute - removes the attribute from the list of attributes.
1038 void removeAttribute(unsigned i, Attributes attr);
1040 /// @brief Determine whether the call or the callee has the given attribute.
1041 bool paramHasAttr(unsigned i, Attributes attr) const;
1043 /// @brief Extract the alignment for a call or parameter (0=unknown).
1044 unsigned getParamAlignment(unsigned i) const {
1045 return AttributeList.getParamAlignment(i);
1048 /// @brief Determine if the call does not access memory.
1049 bool doesNotAccessMemory() const {
1050 return paramHasAttr(~0, Attribute::ReadNone);
1052 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1053 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1054 else removeAttribute(~0, Attribute::ReadNone);
1057 /// @brief Determine if the call does not access or only reads memory.
1058 bool onlyReadsMemory() const {
1059 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1061 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1062 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1063 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1066 /// @brief Determine if the call cannot return.
1067 bool doesNotReturn() const {
1068 return paramHasAttr(~0, Attribute::NoReturn);
1070 void setDoesNotReturn(bool DoesNotReturn = true) {
1071 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1072 else removeAttribute(~0, Attribute::NoReturn);
1075 /// @brief Determine if the call cannot unwind.
1076 bool doesNotThrow() const {
1077 return paramHasAttr(~0, Attribute::NoUnwind);
1079 void setDoesNotThrow(bool DoesNotThrow = true) {
1080 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1081 else removeAttribute(~0, Attribute::NoUnwind);
1084 /// @brief Determine if the call returns a structure through first
1085 /// pointer argument.
1086 bool hasStructRetAttr() const {
1087 // Be friendly and also check the callee.
1088 return paramHasAttr(1, Attribute::StructRet);
1091 /// @brief Determine if any call argument is an aggregate passed by value.
1092 bool hasByValArgument() const {
1093 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1096 /// getCalledFunction - Return the function called, or null if this is an
1097 /// indirect function invocation.
1099 Function *getCalledFunction() const {
1100 return dyn_cast<Function>(Op<0>());
1103 /// getCalledValue - Get a pointer to the function that is invoked by this
1105 const Value *getCalledValue() const { return Op<0>(); }
1106 Value *getCalledValue() { return Op<0>(); }
1108 // Methods for support type inquiry through isa, cast, and dyn_cast:
1109 static inline bool classof(const CallInst *) { return true; }
1110 static inline bool classof(const Instruction *I) {
1111 return I->getOpcode() == Instruction::Call;
1113 static inline bool classof(const Value *V) {
1114 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1119 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1122 template<typename InputIterator>
1123 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1124 const std::string &NameStr, BasicBlock *InsertAtEnd)
1125 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1126 ->getElementType())->getReturnType(),
1128 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1129 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1130 init(Func, ArgBegin, ArgEnd, NameStr,
1131 typename std::iterator_traits<InputIterator>::iterator_category());
1134 template<typename InputIterator>
1135 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1136 const std::string &NameStr, Instruction *InsertBefore)
1137 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1138 ->getElementType())->getReturnType(),
1140 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1141 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1142 init(Func, ArgBegin, ArgEnd, NameStr,
1143 typename std::iterator_traits<InputIterator>::iterator_category());
1146 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1148 //===----------------------------------------------------------------------===//
1150 //===----------------------------------------------------------------------===//
1152 /// SelectInst - This class represents the LLVM 'select' instruction.
1154 class SelectInst : public Instruction {
1155 void init(Value *C, Value *S1, Value *S2) {
1156 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1162 SelectInst(const SelectInst &SI)
1163 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1164 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1166 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1167 Instruction *InsertBefore)
1168 : Instruction(S1->getType(), Instruction::Select,
1169 &Op<0>(), 3, InsertBefore) {
1173 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1174 BasicBlock *InsertAtEnd)
1175 : Instruction(S1->getType(), Instruction::Select,
1176 &Op<0>(), 3, InsertAtEnd) {
1181 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1182 const std::string &NameStr = "",
1183 Instruction *InsertBefore = 0) {
1184 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1186 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1187 const std::string &NameStr,
1188 BasicBlock *InsertAtEnd) {
1189 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1192 Value *getCondition() const { return Op<0>(); }
1193 Value *getTrueValue() const { return Op<1>(); }
1194 Value *getFalseValue() const { return Op<2>(); }
1196 /// areInvalidOperands - Return a string if the specified operands are invalid
1197 /// for a select operation, otherwise return null.
1198 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1200 /// Transparently provide more efficient getOperand methods.
1201 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1203 OtherOps getOpcode() const {
1204 return static_cast<OtherOps>(Instruction::getOpcode());
1207 virtual SelectInst *clone(LLVMContext &Context) const;
1209 // Methods for support type inquiry through isa, cast, and dyn_cast:
1210 static inline bool classof(const SelectInst *) { return true; }
1211 static inline bool classof(const Instruction *I) {
1212 return I->getOpcode() == Instruction::Select;
1214 static inline bool classof(const Value *V) {
1215 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1220 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1223 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1225 //===----------------------------------------------------------------------===//
1227 //===----------------------------------------------------------------------===//
1229 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1230 /// an argument of the specified type given a va_list and increments that list
1232 class VAArgInst : public UnaryInstruction {
1233 VAArgInst(const VAArgInst &VAA)
1234 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1236 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1237 Instruction *InsertBefore = 0)
1238 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1241 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1242 BasicBlock *InsertAtEnd)
1243 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1247 virtual VAArgInst *clone(LLVMContext &Context) const;
1249 // Methods for support type inquiry through isa, cast, and dyn_cast:
1250 static inline bool classof(const VAArgInst *) { return true; }
1251 static inline bool classof(const Instruction *I) {
1252 return I->getOpcode() == VAArg;
1254 static inline bool classof(const Value *V) {
1255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1259 //===----------------------------------------------------------------------===//
1260 // ExtractElementInst Class
1261 //===----------------------------------------------------------------------===//
1263 /// ExtractElementInst - This instruction extracts a single (scalar)
1264 /// element from a VectorType value
1266 class ExtractElementInst : public Instruction {
1267 ExtractElementInst(const ExtractElementInst &EE) :
1268 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1269 Op<0>() = EE.Op<0>();
1270 Op<1>() = EE.Op<1>();
1274 // allocate space for exactly two operands
1275 void *operator new(size_t s) {
1276 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1278 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1279 Instruction *InsertBefore = 0);
1280 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1281 BasicBlock *InsertAtEnd);
1283 /// isValidOperands - Return true if an extractelement instruction can be
1284 /// formed with the specified operands.
1285 static bool isValidOperands(const Value *Vec, const Value *Idx);
1287 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1289 /// Transparently provide more efficient getOperand methods.
1290 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1292 // Methods for support type inquiry through isa, cast, and dyn_cast:
1293 static inline bool classof(const ExtractElementInst *) { return true; }
1294 static inline bool classof(const Instruction *I) {
1295 return I->getOpcode() == Instruction::ExtractElement;
1297 static inline bool classof(const Value *V) {
1298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1303 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1306 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1308 //===----------------------------------------------------------------------===//
1309 // InsertElementInst Class
1310 //===----------------------------------------------------------------------===//
1312 /// InsertElementInst - This instruction inserts a single (scalar)
1313 /// element into a VectorType value
1315 class InsertElementInst : public Instruction {
1316 InsertElementInst(const InsertElementInst &IE);
1317 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1318 const std::string &NameStr = "",
1319 Instruction *InsertBefore = 0);
1320 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1321 const std::string &NameStr, BasicBlock *InsertAtEnd);
1323 static InsertElementInst *Create(const InsertElementInst &IE) {
1324 return new(IE.getNumOperands()) InsertElementInst(IE);
1326 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1327 const std::string &NameStr = "",
1328 Instruction *InsertBefore = 0) {
1329 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1331 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1332 const std::string &NameStr,
1333 BasicBlock *InsertAtEnd) {
1334 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1337 /// isValidOperands - Return true if an insertelement instruction can be
1338 /// formed with the specified operands.
1339 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1342 virtual InsertElementInst *clone(LLVMContext &Context) const;
1344 /// getType - Overload to return most specific vector type.
1346 const VectorType *getType() const {
1347 return reinterpret_cast<const VectorType*>(Instruction::getType());
1350 /// Transparently provide more efficient getOperand methods.
1351 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1353 // Methods for support type inquiry through isa, cast, and dyn_cast:
1354 static inline bool classof(const InsertElementInst *) { return true; }
1355 static inline bool classof(const Instruction *I) {
1356 return I->getOpcode() == Instruction::InsertElement;
1358 static inline bool classof(const Value *V) {
1359 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1364 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1367 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1369 //===----------------------------------------------------------------------===//
1370 // ShuffleVectorInst Class
1371 //===----------------------------------------------------------------------===//
1373 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1376 class ShuffleVectorInst : public Instruction {
1377 ShuffleVectorInst(const ShuffleVectorInst &IE);
1379 // allocate space for exactly three operands
1380 void *operator new(size_t s) {
1381 return User::operator new(s, 3);
1383 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1384 const std::string &NameStr = "",
1385 Instruction *InsertBefor = 0);
1386 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1387 const std::string &NameStr, BasicBlock *InsertAtEnd);
1389 /// isValidOperands - Return true if a shufflevector instruction can be
1390 /// formed with the specified operands.
1391 static bool isValidOperands(const Value *V1, const Value *V2,
1394 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1396 /// getType - Overload to return most specific vector type.
1398 const VectorType *getType() const {
1399 return reinterpret_cast<const VectorType*>(Instruction::getType());
1402 /// Transparently provide more efficient getOperand methods.
1403 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1405 /// getMaskValue - Return the index from the shuffle mask for the specified
1406 /// output result. This is either -1 if the element is undef or a number less
1407 /// than 2*numelements.
1408 int getMaskValue(unsigned i) const;
1410 // Methods for support type inquiry through isa, cast, and dyn_cast:
1411 static inline bool classof(const ShuffleVectorInst *) { return true; }
1412 static inline bool classof(const Instruction *I) {
1413 return I->getOpcode() == Instruction::ShuffleVector;
1415 static inline bool classof(const Value *V) {
1416 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1421 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1424 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1426 //===----------------------------------------------------------------------===//
1427 // ExtractValueInst Class
1428 //===----------------------------------------------------------------------===//
1430 /// ExtractValueInst - This instruction extracts a struct member or array
1431 /// element value from an aggregate value.
1433 class ExtractValueInst : public UnaryInstruction {
1434 SmallVector<unsigned, 4> Indices;
1436 ExtractValueInst(const ExtractValueInst &EVI);
1437 void init(const unsigned *Idx, unsigned NumIdx,
1438 const std::string &NameStr);
1439 void init(unsigned Idx, const std::string &NameStr);
1441 template<typename InputIterator>
1442 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1443 const std::string &NameStr,
1444 // This argument ensures that we have an iterator we can
1445 // do arithmetic on in constant time
1446 std::random_access_iterator_tag) {
1447 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1449 // There's no fundamental reason why we require at least one index
1450 // (other than weirdness with &*IdxBegin being invalid; see
1451 // getelementptr's init routine for example). But there's no
1452 // present need to support it.
1453 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1455 // This requires that the iterator points to contiguous memory.
1456 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1457 // we have to build an array here
1460 /// getIndexedType - Returns the type of the element that would be extracted
1461 /// with an extractvalue instruction with the specified parameters.
1463 /// Null is returned if the indices are invalid for the specified
1466 static const Type *getIndexedType(const Type *Agg,
1467 const unsigned *Idx, unsigned NumIdx);
1469 template<typename InputIterator>
1470 static const Type *getIndexedType(const Type *Ptr,
1471 InputIterator IdxBegin,
1472 InputIterator IdxEnd,
1473 // This argument ensures that we
1474 // have an iterator we can do
1475 // arithmetic on in constant time
1476 std::random_access_iterator_tag) {
1477 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1480 // This requires that the iterator points to contiguous memory.
1481 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1483 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1486 /// Constructors - Create a extractvalue instruction with a base aggregate
1487 /// value and a list of indices. The first ctor can optionally insert before
1488 /// an existing instruction, the second appends the new instruction to the
1489 /// specified BasicBlock.
1490 template<typename InputIterator>
1491 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1492 InputIterator IdxEnd,
1493 const std::string &NameStr,
1494 Instruction *InsertBefore);
1495 template<typename InputIterator>
1496 inline ExtractValueInst(Value *Agg,
1497 InputIterator IdxBegin, InputIterator IdxEnd,
1498 const std::string &NameStr, BasicBlock *InsertAtEnd);
1500 // allocate space for exactly one operand
1501 void *operator new(size_t s) {
1502 return User::operator new(s, 1);
1506 template<typename InputIterator>
1507 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1508 InputIterator IdxEnd,
1509 const std::string &NameStr = "",
1510 Instruction *InsertBefore = 0) {
1512 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1514 template<typename InputIterator>
1515 static ExtractValueInst *Create(Value *Agg,
1516 InputIterator IdxBegin, InputIterator IdxEnd,
1517 const std::string &NameStr,
1518 BasicBlock *InsertAtEnd) {
1519 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1522 /// Constructors - These two creators are convenience methods because one
1523 /// index extractvalue instructions are much more common than those with
1525 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1526 const std::string &NameStr = "",
1527 Instruction *InsertBefore = 0) {
1528 unsigned Idxs[1] = { Idx };
1529 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1531 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1532 const std::string &NameStr,
1533 BasicBlock *InsertAtEnd) {
1534 unsigned Idxs[1] = { Idx };
1535 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1538 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1540 /// getIndexedType - Returns the type of the element that would be extracted
1541 /// with an extractvalue instruction with the specified parameters.
1543 /// Null is returned if the indices are invalid for the specified
1546 template<typename InputIterator>
1547 static const Type *getIndexedType(const Type *Ptr,
1548 InputIterator IdxBegin,
1549 InputIterator IdxEnd) {
1550 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1551 typename std::iterator_traits<InputIterator>::
1552 iterator_category());
1554 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1556 typedef const unsigned* idx_iterator;
1557 inline idx_iterator idx_begin() const { return Indices.begin(); }
1558 inline idx_iterator idx_end() const { return Indices.end(); }
1560 Value *getAggregateOperand() {
1561 return getOperand(0);
1563 const Value *getAggregateOperand() const {
1564 return getOperand(0);
1566 static unsigned getAggregateOperandIndex() {
1567 return 0U; // get index for modifying correct operand
1570 unsigned getNumIndices() const { // Note: always non-negative
1571 return (unsigned)Indices.size();
1574 bool hasIndices() const {
1578 // Methods for support type inquiry through isa, cast, and dyn_cast:
1579 static inline bool classof(const ExtractValueInst *) { return true; }
1580 static inline bool classof(const Instruction *I) {
1581 return I->getOpcode() == Instruction::ExtractValue;
1583 static inline bool classof(const Value *V) {
1584 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1588 template<typename InputIterator>
1589 ExtractValueInst::ExtractValueInst(Value *Agg,
1590 InputIterator IdxBegin,
1591 InputIterator IdxEnd,
1592 const std::string &NameStr,
1593 Instruction *InsertBefore)
1594 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1596 ExtractValue, Agg, InsertBefore) {
1597 init(IdxBegin, IdxEnd, NameStr,
1598 typename std::iterator_traits<InputIterator>::iterator_category());
1600 template<typename InputIterator>
1601 ExtractValueInst::ExtractValueInst(Value *Agg,
1602 InputIterator IdxBegin,
1603 InputIterator IdxEnd,
1604 const std::string &NameStr,
1605 BasicBlock *InsertAtEnd)
1606 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1608 ExtractValue, Agg, InsertAtEnd) {
1609 init(IdxBegin, IdxEnd, NameStr,
1610 typename std::iterator_traits<InputIterator>::iterator_category());
1614 //===----------------------------------------------------------------------===//
1615 // InsertValueInst Class
1616 //===----------------------------------------------------------------------===//
1618 /// InsertValueInst - This instruction inserts a struct field of array element
1619 /// value into an aggregate value.
1621 class InsertValueInst : public Instruction {
1622 SmallVector<unsigned, 4> Indices;
1624 void *operator new(size_t, unsigned); // Do not implement
1625 InsertValueInst(const InsertValueInst &IVI);
1626 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1627 const std::string &NameStr);
1628 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1630 template<typename InputIterator>
1631 void init(Value *Agg, Value *Val,
1632 InputIterator IdxBegin, InputIterator IdxEnd,
1633 const std::string &NameStr,
1634 // This argument ensures that we have an iterator we can
1635 // do arithmetic on in constant time
1636 std::random_access_iterator_tag) {
1637 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1639 // There's no fundamental reason why we require at least one index
1640 // (other than weirdness with &*IdxBegin being invalid; see
1641 // getelementptr's init routine for example). But there's no
1642 // present need to support it.
1643 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1645 // This requires that the iterator points to contiguous memory.
1646 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1647 // we have to build an array here
1650 /// Constructors - Create a insertvalue instruction with a base aggregate
1651 /// value, a value to insert, and a list of indices. The first ctor can
1652 /// optionally insert before an existing instruction, the second appends
1653 /// the new instruction to the specified BasicBlock.
1654 template<typename InputIterator>
1655 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1656 InputIterator IdxEnd,
1657 const std::string &NameStr,
1658 Instruction *InsertBefore);
1659 template<typename InputIterator>
1660 inline InsertValueInst(Value *Agg, Value *Val,
1661 InputIterator IdxBegin, InputIterator IdxEnd,
1662 const std::string &NameStr, BasicBlock *InsertAtEnd);
1664 /// Constructors - These two constructors are convenience methods because one
1665 /// and two index insertvalue instructions are so common.
1666 InsertValueInst(Value *Agg, Value *Val,
1667 unsigned Idx, const std::string &NameStr = "",
1668 Instruction *InsertBefore = 0);
1669 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1670 const std::string &NameStr, BasicBlock *InsertAtEnd);
1672 // allocate space for exactly two operands
1673 void *operator new(size_t s) {
1674 return User::operator new(s, 2);
1677 template<typename InputIterator>
1678 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1679 InputIterator IdxEnd,
1680 const std::string &NameStr = "",
1681 Instruction *InsertBefore = 0) {
1682 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1683 NameStr, InsertBefore);
1685 template<typename InputIterator>
1686 static InsertValueInst *Create(Value *Agg, Value *Val,
1687 InputIterator IdxBegin, InputIterator IdxEnd,
1688 const std::string &NameStr,
1689 BasicBlock *InsertAtEnd) {
1690 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1691 NameStr, InsertAtEnd);
1694 /// Constructors - These two creators are convenience methods because one
1695 /// index insertvalue instructions are much more common than those with
1697 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1698 const std::string &NameStr = "",
1699 Instruction *InsertBefore = 0) {
1700 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1702 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1703 const std::string &NameStr,
1704 BasicBlock *InsertAtEnd) {
1705 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1708 virtual InsertValueInst *clone(LLVMContext &Context) const;
1710 /// Transparently provide more efficient getOperand methods.
1711 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1713 typedef const unsigned* idx_iterator;
1714 inline idx_iterator idx_begin() const { return Indices.begin(); }
1715 inline idx_iterator idx_end() const { return Indices.end(); }
1717 Value *getAggregateOperand() {
1718 return getOperand(0);
1720 const Value *getAggregateOperand() const {
1721 return getOperand(0);
1723 static unsigned getAggregateOperandIndex() {
1724 return 0U; // get index for modifying correct operand
1727 Value *getInsertedValueOperand() {
1728 return getOperand(1);
1730 const Value *getInsertedValueOperand() const {
1731 return getOperand(1);
1733 static unsigned getInsertedValueOperandIndex() {
1734 return 1U; // get index for modifying correct operand
1737 unsigned getNumIndices() const { // Note: always non-negative
1738 return (unsigned)Indices.size();
1741 bool hasIndices() const {
1745 // Methods for support type inquiry through isa, cast, and dyn_cast:
1746 static inline bool classof(const InsertValueInst *) { return true; }
1747 static inline bool classof(const Instruction *I) {
1748 return I->getOpcode() == Instruction::InsertValue;
1750 static inline bool classof(const Value *V) {
1751 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1756 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1759 template<typename InputIterator>
1760 InsertValueInst::InsertValueInst(Value *Agg,
1762 InputIterator IdxBegin,
1763 InputIterator IdxEnd,
1764 const std::string &NameStr,
1765 Instruction *InsertBefore)
1766 : Instruction(Agg->getType(), InsertValue,
1767 OperandTraits<InsertValueInst>::op_begin(this),
1769 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1770 typename std::iterator_traits<InputIterator>::iterator_category());
1772 template<typename InputIterator>
1773 InsertValueInst::InsertValueInst(Value *Agg,
1775 InputIterator IdxBegin,
1776 InputIterator IdxEnd,
1777 const std::string &NameStr,
1778 BasicBlock *InsertAtEnd)
1779 : Instruction(Agg->getType(), InsertValue,
1780 OperandTraits<InsertValueInst>::op_begin(this),
1782 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1783 typename std::iterator_traits<InputIterator>::iterator_category());
1786 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1788 //===----------------------------------------------------------------------===//
1790 //===----------------------------------------------------------------------===//
1792 // PHINode - The PHINode class is used to represent the magical mystical PHI
1793 // node, that can not exist in nature, but can be synthesized in a computer
1794 // scientist's overactive imagination.
1796 class PHINode : public Instruction {
1797 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1798 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1799 /// the number actually in use.
1800 unsigned ReservedSpace;
1801 PHINode(const PHINode &PN);
1802 // allocate space for exactly zero operands
1803 void *operator new(size_t s) {
1804 return User::operator new(s, 0);
1806 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1807 Instruction *InsertBefore = 0)
1808 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1813 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1814 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1819 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1820 Instruction *InsertBefore = 0) {
1821 return new PHINode(Ty, NameStr, InsertBefore);
1823 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1824 BasicBlock *InsertAtEnd) {
1825 return new PHINode(Ty, NameStr, InsertAtEnd);
1829 /// reserveOperandSpace - This method can be used to avoid repeated
1830 /// reallocation of PHI operand lists by reserving space for the correct
1831 /// number of operands before adding them. Unlike normal vector reserves,
1832 /// this method can also be used to trim the operand space.
1833 void reserveOperandSpace(unsigned NumValues) {
1834 resizeOperands(NumValues*2);
1837 virtual PHINode *clone(LLVMContext &Context) const;
1839 /// Provide fast operand accessors
1840 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1842 /// getNumIncomingValues - Return the number of incoming edges
1844 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1846 /// getIncomingValue - Return incoming value number x
1848 Value *getIncomingValue(unsigned i) const {
1849 assert(i*2 < getNumOperands() && "Invalid value number!");
1850 return getOperand(i*2);
1852 void setIncomingValue(unsigned i, Value *V) {
1853 assert(i*2 < getNumOperands() && "Invalid value number!");
1856 static unsigned getOperandNumForIncomingValue(unsigned i) {
1859 static unsigned getIncomingValueNumForOperand(unsigned i) {
1860 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1864 /// getIncomingBlock - Return incoming basic block corresponding
1865 /// to value use iterator
1867 template <typename U>
1868 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1869 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1870 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1872 /// getIncomingBlock - Return incoming basic block number x
1874 BasicBlock *getIncomingBlock(unsigned i) const {
1875 return static_cast<BasicBlock*>(getOperand(i*2+1));
1877 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1878 setOperand(i*2+1, BB);
1880 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1883 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1884 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1888 /// addIncoming - Add an incoming value to the end of the PHI list
1890 void addIncoming(Value *V, BasicBlock *BB) {
1891 assert(V && "PHI node got a null value!");
1892 assert(BB && "PHI node got a null basic block!");
1893 assert(getType() == V->getType() &&
1894 "All operands to PHI node must be the same type as the PHI node!");
1895 unsigned OpNo = NumOperands;
1896 if (OpNo+2 > ReservedSpace)
1897 resizeOperands(0); // Get more space!
1898 // Initialize some new operands.
1899 NumOperands = OpNo+2;
1900 OperandList[OpNo] = V;
1901 OperandList[OpNo+1] = BB;
1904 /// removeIncomingValue - Remove an incoming value. This is useful if a
1905 /// predecessor basic block is deleted. The value removed is returned.
1907 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1908 /// is true), the PHI node is destroyed and any uses of it are replaced with
1909 /// dummy values. The only time there should be zero incoming values to a PHI
1910 /// node is when the block is dead, so this strategy is sound.
1912 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1914 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1915 int Idx = getBasicBlockIndex(BB);
1916 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1917 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1920 /// getBasicBlockIndex - Return the first index of the specified basic
1921 /// block in the value list for this PHI. Returns -1 if no instance.
1923 int getBasicBlockIndex(const BasicBlock *BB) const {
1924 Use *OL = OperandList;
1925 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1926 if (OL[i+1].get() == BB) return i/2;
1930 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1931 return getIncomingValue(getBasicBlockIndex(BB));
1934 /// hasConstantValue - If the specified PHI node always merges together the
1935 /// same value, return the value, otherwise return null.
1937 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1939 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1940 static inline bool classof(const PHINode *) { return true; }
1941 static inline bool classof(const Instruction *I) {
1942 return I->getOpcode() == Instruction::PHI;
1944 static inline bool classof(const Value *V) {
1945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1948 void resizeOperands(unsigned NumOperands);
1952 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1955 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1958 //===----------------------------------------------------------------------===//
1960 //===----------------------------------------------------------------------===//
1962 //===---------------------------------------------------------------------------
1963 /// ReturnInst - Return a value (possibly void), from a function. Execution
1964 /// does not continue in this function any longer.
1966 class ReturnInst : public TerminatorInst {
1967 ReturnInst(const ReturnInst &RI);
1970 // ReturnInst constructors:
1971 // ReturnInst() - 'ret void' instruction
1972 // ReturnInst( null) - 'ret void' instruction
1973 // ReturnInst(Value* X) - 'ret X' instruction
1974 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1975 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1976 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1977 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1979 // NOTE: If the Value* passed is of type void then the constructor behaves as
1980 // if it was passed NULL.
1981 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1982 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1983 explicit ReturnInst(BasicBlock *InsertAtEnd);
1985 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1986 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1988 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1989 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1991 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1992 return new(0) ReturnInst(InsertAtEnd);
1994 virtual ~ReturnInst();
1996 virtual ReturnInst *clone(LLVMContext &Context) const;
1998 /// Provide fast operand accessors
1999 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2001 /// Convenience accessor
2002 Value *getReturnValue(unsigned n = 0) const {
2003 return n < getNumOperands()
2008 unsigned getNumSuccessors() const { return 0; }
2010 // Methods for support type inquiry through isa, cast, and dyn_cast:
2011 static inline bool classof(const ReturnInst *) { return true; }
2012 static inline bool classof(const Instruction *I) {
2013 return (I->getOpcode() == Instruction::Ret);
2015 static inline bool classof(const Value *V) {
2016 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2019 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2020 virtual unsigned getNumSuccessorsV() const;
2021 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2025 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2028 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2030 //===----------------------------------------------------------------------===//
2032 //===----------------------------------------------------------------------===//
2034 //===---------------------------------------------------------------------------
2035 /// BranchInst - Conditional or Unconditional Branch instruction.
2037 class BranchInst : public TerminatorInst {
2038 /// Ops list - Branches are strange. The operands are ordered:
2039 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2040 /// they don't have to check for cond/uncond branchness. These are mostly
2041 /// accessed relative from op_end().
2042 BranchInst(const BranchInst &BI);
2044 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2045 // BranchInst(BB *B) - 'br B'
2046 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2047 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2048 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2049 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2050 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2051 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2052 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2053 Instruction *InsertBefore = 0);
2054 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2055 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2056 BasicBlock *InsertAtEnd);
2058 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2059 return new(1, true) BranchInst(IfTrue, InsertBefore);
2061 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2062 Value *Cond, Instruction *InsertBefore = 0) {
2063 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2065 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2066 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2068 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2069 Value *Cond, BasicBlock *InsertAtEnd) {
2070 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2075 /// Transparently provide more efficient getOperand methods.
2076 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2078 virtual BranchInst *clone(LLVMContext &Context) const;
2080 bool isUnconditional() const { return getNumOperands() == 1; }
2081 bool isConditional() const { return getNumOperands() == 3; }
2083 Value *getCondition() const {
2084 assert(isConditional() && "Cannot get condition of an uncond branch!");
2088 void setCondition(Value *V) {
2089 assert(isConditional() && "Cannot set condition of unconditional branch!");
2093 // setUnconditionalDest - Change the current branch to an unconditional branch
2094 // targeting the specified block.
2095 // FIXME: Eliminate this ugly method.
2096 void setUnconditionalDest(BasicBlock *Dest) {
2098 if (isConditional()) { // Convert this to an uncond branch.
2102 OperandList = op_begin();
2106 unsigned getNumSuccessors() const { return 1+isConditional(); }
2108 BasicBlock *getSuccessor(unsigned i) const {
2109 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2110 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2113 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2114 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2115 *(&Op<-1>() - idx) = NewSucc;
2118 // Methods for support type inquiry through isa, cast, and dyn_cast:
2119 static inline bool classof(const BranchInst *) { return true; }
2120 static inline bool classof(const Instruction *I) {
2121 return (I->getOpcode() == Instruction::Br);
2123 static inline bool classof(const Value *V) {
2124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2127 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2128 virtual unsigned getNumSuccessorsV() const;
2129 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2133 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2135 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2137 //===----------------------------------------------------------------------===//
2139 //===----------------------------------------------------------------------===//
2141 //===---------------------------------------------------------------------------
2142 /// SwitchInst - Multiway switch
2144 class SwitchInst : public TerminatorInst {
2145 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2146 unsigned ReservedSpace;
2147 // Operand[0] = Value to switch on
2148 // Operand[1] = Default basic block destination
2149 // Operand[2n ] = Value to match
2150 // Operand[2n+1] = BasicBlock to go to on match
2151 SwitchInst(const SwitchInst &RI);
2152 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2153 void resizeOperands(unsigned No);
2154 // allocate space for exactly zero operands
2155 void *operator new(size_t s) {
2156 return User::operator new(s, 0);
2158 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2159 /// switch on and a default destination. The number of additional cases can
2160 /// be specified here to make memory allocation more efficient. This
2161 /// constructor can also autoinsert before another instruction.
2162 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2163 Instruction *InsertBefore = 0);
2165 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2166 /// switch on and a default destination. The number of additional cases can
2167 /// be specified here to make memory allocation more efficient. This
2168 /// constructor also autoinserts at the end of the specified BasicBlock.
2169 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2170 BasicBlock *InsertAtEnd);
2172 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2173 unsigned NumCases, Instruction *InsertBefore = 0) {
2174 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2176 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2177 unsigned NumCases, BasicBlock *InsertAtEnd) {
2178 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2182 /// Provide fast operand accessors
2183 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2185 // Accessor Methods for Switch stmt
2186 Value *getCondition() const { return getOperand(0); }
2187 void setCondition(Value *V) { setOperand(0, V); }
2189 BasicBlock *getDefaultDest() const {
2190 return cast<BasicBlock>(getOperand(1));
2193 /// getNumCases - return the number of 'cases' in this switch instruction.
2194 /// Note that case #0 is always the default case.
2195 unsigned getNumCases() const {
2196 return getNumOperands()/2;
2199 /// getCaseValue - Return the specified case value. Note that case #0, the
2200 /// default destination, does not have a case value.
2201 ConstantInt *getCaseValue(unsigned i) {
2202 assert(i && i < getNumCases() && "Illegal case value to get!");
2203 return getSuccessorValue(i);
2206 /// getCaseValue - Return the specified case value. Note that case #0, the
2207 /// default destination, does not have a case value.
2208 const ConstantInt *getCaseValue(unsigned i) const {
2209 assert(i && i < getNumCases() && "Illegal case value to get!");
2210 return getSuccessorValue(i);
2213 /// findCaseValue - Search all of the case values for the specified constant.
2214 /// If it is explicitly handled, return the case number of it, otherwise
2215 /// return 0 to indicate that it is handled by the default handler.
2216 unsigned findCaseValue(const ConstantInt *C) const {
2217 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2218 if (getCaseValue(i) == C)
2223 /// findCaseDest - Finds the unique case value for a given successor. Returns
2224 /// null if the successor is not found, not unique, or is the default case.
2225 ConstantInt *findCaseDest(BasicBlock *BB) {
2226 if (BB == getDefaultDest()) return NULL;
2228 ConstantInt *CI = NULL;
2229 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2230 if (getSuccessor(i) == BB) {
2231 if (CI) return NULL; // Multiple cases lead to BB.
2232 else CI = getCaseValue(i);
2238 /// addCase - Add an entry to the switch instruction...
2240 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2242 /// removeCase - This method removes the specified successor from the switch
2243 /// instruction. Note that this cannot be used to remove the default
2244 /// destination (successor #0).
2246 void removeCase(unsigned idx);
2248 virtual SwitchInst *clone(LLVMContext &Context) const;
2250 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2251 BasicBlock *getSuccessor(unsigned idx) const {
2252 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2253 return cast<BasicBlock>(getOperand(idx*2+1));
2255 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2256 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2257 setOperand(idx*2+1, NewSucc);
2260 // getSuccessorValue - Return the value associated with the specified
2262 ConstantInt *getSuccessorValue(unsigned idx) const {
2263 assert(idx < getNumSuccessors() && "Successor # out of range!");
2264 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2267 // Methods for support type inquiry through isa, cast, and dyn_cast:
2268 static inline bool classof(const SwitchInst *) { return true; }
2269 static inline bool classof(const Instruction *I) {
2270 return I->getOpcode() == Instruction::Switch;
2272 static inline bool classof(const Value *V) {
2273 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2276 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2277 virtual unsigned getNumSuccessorsV() const;
2278 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2282 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2285 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2288 //===----------------------------------------------------------------------===//
2290 //===----------------------------------------------------------------------===//
2292 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2293 /// calling convention of the call.
2295 class InvokeInst : public TerminatorInst {
2296 AttrListPtr AttributeList;
2297 InvokeInst(const InvokeInst &BI);
2298 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2299 Value* const *Args, unsigned NumArgs);
2301 template<typename InputIterator>
2302 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2303 InputIterator ArgBegin, InputIterator ArgEnd,
2304 const std::string &NameStr,
2305 // This argument ensures that we have an iterator we can
2306 // do arithmetic on in constant time
2307 std::random_access_iterator_tag) {
2308 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2310 // This requires that the iterator points to contiguous memory.
2311 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2315 /// Construct an InvokeInst given a range of arguments.
2316 /// InputIterator must be a random-access iterator pointing to
2317 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2318 /// made for random-accessness but not for contiguous storage as
2319 /// that would incur runtime overhead.
2321 /// @brief Construct an InvokeInst from a range of arguments
2322 template<typename InputIterator>
2323 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2324 InputIterator ArgBegin, InputIterator ArgEnd,
2326 const std::string &NameStr, Instruction *InsertBefore);
2328 /// Construct an InvokeInst given a range of arguments.
2329 /// InputIterator must be a random-access iterator pointing to
2330 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2331 /// made for random-accessness but not for contiguous storage as
2332 /// that would incur runtime overhead.
2334 /// @brief Construct an InvokeInst from a range of arguments
2335 template<typename InputIterator>
2336 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2337 InputIterator ArgBegin, InputIterator ArgEnd,
2339 const std::string &NameStr, BasicBlock *InsertAtEnd);
2341 template<typename InputIterator>
2342 static InvokeInst *Create(Value *Func,
2343 BasicBlock *IfNormal, BasicBlock *IfException,
2344 InputIterator ArgBegin, InputIterator ArgEnd,
2345 const std::string &NameStr = "",
2346 Instruction *InsertBefore = 0) {
2347 unsigned Values(ArgEnd - ArgBegin + 3);
2348 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2349 Values, NameStr, InsertBefore);
2351 template<typename InputIterator>
2352 static InvokeInst *Create(Value *Func,
2353 BasicBlock *IfNormal, BasicBlock *IfException,
2354 InputIterator ArgBegin, InputIterator ArgEnd,
2355 const std::string &NameStr,
2356 BasicBlock *InsertAtEnd) {
2357 unsigned Values(ArgEnd - ArgBegin + 3);
2358 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2359 Values, NameStr, InsertAtEnd);
2362 virtual InvokeInst *clone(LLVMContext &Context) const;
2364 /// Provide fast operand accessors
2365 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2367 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2369 unsigned getCallingConv() const { return SubclassData; }
2370 void setCallingConv(unsigned CC) {
2374 /// getAttributes - Return the parameter attributes for this invoke.
2376 const AttrListPtr &getAttributes() const { return AttributeList; }
2378 /// setAttributes - Set the parameter attributes for this invoke.
2380 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2382 /// addAttribute - adds the attribute to the list of attributes.
2383 void addAttribute(unsigned i, Attributes attr);
2385 /// removeAttribute - removes the attribute from the list of attributes.
2386 void removeAttribute(unsigned i, Attributes attr);
2388 /// @brief Determine whether the call or the callee has the given attribute.
2389 bool paramHasAttr(unsigned i, Attributes attr) const;
2391 /// @brief Extract the alignment for a call or parameter (0=unknown).
2392 unsigned getParamAlignment(unsigned i) const {
2393 return AttributeList.getParamAlignment(i);
2396 /// @brief Determine if the call does not access memory.
2397 bool doesNotAccessMemory() const {
2398 return paramHasAttr(~0, Attribute::ReadNone);
2400 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2401 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2402 else removeAttribute(~0, Attribute::ReadNone);
2405 /// @brief Determine if the call does not access or only reads memory.
2406 bool onlyReadsMemory() const {
2407 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2409 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2410 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2411 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2414 /// @brief Determine if the call cannot return.
2415 bool doesNotReturn() const {
2416 return paramHasAttr(~0, Attribute::NoReturn);
2418 void setDoesNotReturn(bool DoesNotReturn = true) {
2419 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2420 else removeAttribute(~0, Attribute::NoReturn);
2423 /// @brief Determine if the call cannot unwind.
2424 bool doesNotThrow() const {
2425 return paramHasAttr(~0, Attribute::NoUnwind);
2427 void setDoesNotThrow(bool DoesNotThrow = true) {
2428 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2429 else removeAttribute(~0, Attribute::NoUnwind);
2432 /// @brief Determine if the call returns a structure through first
2433 /// pointer argument.
2434 bool hasStructRetAttr() const {
2435 // Be friendly and also check the callee.
2436 return paramHasAttr(1, Attribute::StructRet);
2439 /// @brief Determine if any call argument is an aggregate passed by value.
2440 bool hasByValArgument() const {
2441 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2444 /// getCalledFunction - Return the function called, or null if this is an
2445 /// indirect function invocation.
2447 Function *getCalledFunction() const {
2448 return dyn_cast<Function>(getOperand(0));
2451 /// getCalledValue - Get a pointer to the function that is invoked by this
2453 const Value *getCalledValue() const { return getOperand(0); }
2454 Value *getCalledValue() { return getOperand(0); }
2456 // get*Dest - Return the destination basic blocks...
2457 BasicBlock *getNormalDest() const {
2458 return cast<BasicBlock>(getOperand(1));
2460 BasicBlock *getUnwindDest() const {
2461 return cast<BasicBlock>(getOperand(2));
2463 void setNormalDest(BasicBlock *B) {
2467 void setUnwindDest(BasicBlock *B) {
2471 BasicBlock *getSuccessor(unsigned i) const {
2472 assert(i < 2 && "Successor # out of range for invoke!");
2473 return i == 0 ? getNormalDest() : getUnwindDest();
2476 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2477 assert(idx < 2 && "Successor # out of range for invoke!");
2478 setOperand(idx+1, NewSucc);
2481 unsigned getNumSuccessors() const { return 2; }
2483 // Methods for support type inquiry through isa, cast, and dyn_cast:
2484 static inline bool classof(const InvokeInst *) { return true; }
2485 static inline bool classof(const Instruction *I) {
2486 return (I->getOpcode() == Instruction::Invoke);
2488 static inline bool classof(const Value *V) {
2489 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2492 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2493 virtual unsigned getNumSuccessorsV() const;
2494 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2498 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2501 template<typename InputIterator>
2502 InvokeInst::InvokeInst(Value *Func,
2503 BasicBlock *IfNormal, BasicBlock *IfException,
2504 InputIterator ArgBegin, InputIterator ArgEnd,
2506 const std::string &NameStr, Instruction *InsertBefore)
2507 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2508 ->getElementType())->getReturnType(),
2509 Instruction::Invoke,
2510 OperandTraits<InvokeInst>::op_end(this) - Values,
2511 Values, InsertBefore) {
2512 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2513 typename std::iterator_traits<InputIterator>::iterator_category());
2515 template<typename InputIterator>
2516 InvokeInst::InvokeInst(Value *Func,
2517 BasicBlock *IfNormal, BasicBlock *IfException,
2518 InputIterator ArgBegin, InputIterator ArgEnd,
2520 const std::string &NameStr, BasicBlock *InsertAtEnd)
2521 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2522 ->getElementType())->getReturnType(),
2523 Instruction::Invoke,
2524 OperandTraits<InvokeInst>::op_end(this) - Values,
2525 Values, InsertAtEnd) {
2526 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2527 typename std::iterator_traits<InputIterator>::iterator_category());
2530 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2532 //===----------------------------------------------------------------------===//
2534 //===----------------------------------------------------------------------===//
2536 //===---------------------------------------------------------------------------
2537 /// UnwindInst - Immediately exit the current function, unwinding the stack
2538 /// until an invoke instruction is found.
2540 class UnwindInst : public TerminatorInst {
2541 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2543 // allocate space for exactly zero operands
2544 void *operator new(size_t s) {
2545 return User::operator new(s, 0);
2547 explicit UnwindInst(Instruction *InsertBefore = 0);
2548 explicit UnwindInst(BasicBlock *InsertAtEnd);
2550 virtual UnwindInst *clone(LLVMContext &Context) const;
2552 unsigned getNumSuccessors() const { return 0; }
2554 // Methods for support type inquiry through isa, cast, and dyn_cast:
2555 static inline bool classof(const UnwindInst *) { return true; }
2556 static inline bool classof(const Instruction *I) {
2557 return I->getOpcode() == Instruction::Unwind;
2559 static inline bool classof(const Value *V) {
2560 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2563 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2564 virtual unsigned getNumSuccessorsV() const;
2565 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2568 //===----------------------------------------------------------------------===//
2569 // UnreachableInst Class
2570 //===----------------------------------------------------------------------===//
2572 //===---------------------------------------------------------------------------
2573 /// UnreachableInst - This function has undefined behavior. In particular, the
2574 /// presence of this instruction indicates some higher level knowledge that the
2575 /// end of the block cannot be reached.
2577 class UnreachableInst : public TerminatorInst {
2578 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2580 // allocate space for exactly zero operands
2581 void *operator new(size_t s) {
2582 return User::operator new(s, 0);
2584 explicit UnreachableInst(Instruction *InsertBefore = 0);
2585 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2587 virtual UnreachableInst *clone(LLVMContext &Context) const;
2589 unsigned getNumSuccessors() const { return 0; }
2591 // Methods for support type inquiry through isa, cast, and dyn_cast:
2592 static inline bool classof(const UnreachableInst *) { return true; }
2593 static inline bool classof(const Instruction *I) {
2594 return I->getOpcode() == Instruction::Unreachable;
2596 static inline bool classof(const Value *V) {
2597 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2600 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2601 virtual unsigned getNumSuccessorsV() const;
2602 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2605 //===----------------------------------------------------------------------===//
2607 //===----------------------------------------------------------------------===//
2609 /// @brief This class represents a truncation of integer types.
2610 class TruncInst : public CastInst {
2611 /// Private copy constructor
2612 TruncInst(const TruncInst &CI)
2613 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2616 /// @brief Constructor with insert-before-instruction semantics
2618 Value *S, ///< The value to be truncated
2619 const Type *Ty, ///< The (smaller) type to truncate to
2620 const std::string &NameStr = "", ///< A name for the new instruction
2621 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2624 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2632 /// @brief Clone an identical TruncInst
2633 virtual CastInst *clone(LLVMContext &Context) const;
2635 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2636 static inline bool classof(const TruncInst *) { return true; }
2637 static inline bool classof(const Instruction *I) {
2638 return I->getOpcode() == Trunc;
2640 static inline bool classof(const Value *V) {
2641 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2645 //===----------------------------------------------------------------------===//
2647 //===----------------------------------------------------------------------===//
2649 /// @brief This class represents zero extension of integer types.
2650 class ZExtInst : public CastInst {
2651 /// @brief Private copy constructor
2652 ZExtInst(const ZExtInst &CI)
2653 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2656 /// @brief Constructor with insert-before-instruction semantics
2658 Value *S, ///< The value to be zero extended
2659 const Type *Ty, ///< The type to zero extend to
2660 const std::string &NameStr = "", ///< A name for the new instruction
2661 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2664 /// @brief Constructor with insert-at-end 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2672 /// @brief Clone an identical ZExtInst
2673 virtual CastInst *clone(LLVMContext &Context) const;
2675 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2676 static inline bool classof(const ZExtInst *) { return true; }
2677 static inline bool classof(const Instruction *I) {
2678 return I->getOpcode() == ZExt;
2680 static inline bool classof(const Value *V) {
2681 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2685 //===----------------------------------------------------------------------===//
2687 //===----------------------------------------------------------------------===//
2689 /// @brief This class represents a sign extension of integer types.
2690 class SExtInst : public CastInst {
2691 /// @brief Private copy constructor
2692 SExtInst(const SExtInst &CI)
2693 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2696 /// @brief Constructor with insert-before-instruction semantics
2698 Value *S, ///< The value to be sign extended
2699 const Type *Ty, ///< The type to sign extend to
2700 const std::string &NameStr = "", ///< A name for the new instruction
2701 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2704 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2712 /// @brief Clone an identical SExtInst
2713 virtual CastInst *clone(LLVMContext &Context) const;
2715 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2716 static inline bool classof(const SExtInst *) { return true; }
2717 static inline bool classof(const Instruction *I) {
2718 return I->getOpcode() == SExt;
2720 static inline bool classof(const Value *V) {
2721 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2725 //===----------------------------------------------------------------------===//
2726 // FPTruncInst Class
2727 //===----------------------------------------------------------------------===//
2729 /// @brief This class represents a truncation of floating point types.
2730 class FPTruncInst : public CastInst {
2731 FPTruncInst(const FPTruncInst &CI)
2732 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2735 /// @brief Constructor with insert-before-instruction semantics
2737 Value *S, ///< The value to be truncated
2738 const Type *Ty, ///< The type to truncate to
2739 const std::string &NameStr = "", ///< A name for the new instruction
2740 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2751 /// @brief Clone an identical FPTruncInst
2752 virtual CastInst *clone(LLVMContext &Context) const;
2754 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2755 static inline bool classof(const FPTruncInst *) { return true; }
2756 static inline bool classof(const Instruction *I) {
2757 return I->getOpcode() == FPTrunc;
2759 static inline bool classof(const Value *V) {
2760 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2764 //===----------------------------------------------------------------------===//
2766 //===----------------------------------------------------------------------===//
2768 /// @brief This class represents an extension of floating point types.
2769 class FPExtInst : public CastInst {
2770 FPExtInst(const FPExtInst &CI)
2771 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2774 /// @brief Constructor with insert-before-instruction semantics
2776 Value *S, ///< The value to be extended
2777 const Type *Ty, ///< The type to extend to
2778 const std::string &NameStr = "", ///< A name for the new instruction
2779 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2782 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2790 /// @brief Clone an identical FPExtInst
2791 virtual CastInst *clone(LLVMContext &Context) const;
2793 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2794 static inline bool classof(const FPExtInst *) { return true; }
2795 static inline bool classof(const Instruction *I) {
2796 return I->getOpcode() == FPExt;
2798 static inline bool classof(const Value *V) {
2799 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2803 //===----------------------------------------------------------------------===//
2805 //===----------------------------------------------------------------------===//
2807 /// @brief This class represents a cast unsigned integer to floating point.
2808 class UIToFPInst : public CastInst {
2809 UIToFPInst(const UIToFPInst &CI)
2810 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2813 /// @brief Constructor with insert-before-instruction semantics
2815 Value *S, ///< The value to be converted
2816 const Type *Ty, ///< The type to convert to
2817 const std::string &NameStr = "", ///< A name for the new instruction
2818 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2821 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2829 /// @brief Clone an identical UIToFPInst
2830 virtual CastInst *clone(LLVMContext &Context) const;
2832 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2833 static inline bool classof(const UIToFPInst *) { return true; }
2834 static inline bool classof(const Instruction *I) {
2835 return I->getOpcode() == UIToFP;
2837 static inline bool classof(const Value *V) {
2838 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2842 //===----------------------------------------------------------------------===//
2844 //===----------------------------------------------------------------------===//
2846 /// @brief This class represents a cast from signed integer to floating point.
2847 class SIToFPInst : public CastInst {
2848 SIToFPInst(const SIToFPInst &CI)
2849 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2852 /// @brief Constructor with insert-before-instruction semantics
2854 Value *S, ///< The value to be converted
2855 const Type *Ty, ///< The type to convert to
2856 const std::string &NameStr = "", ///< A name for the new instruction
2857 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2860 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2868 /// @brief Clone an identical SIToFPInst
2869 virtual CastInst *clone(LLVMContext &Context) const;
2871 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2872 static inline bool classof(const SIToFPInst *) { return true; }
2873 static inline bool classof(const Instruction *I) {
2874 return I->getOpcode() == SIToFP;
2876 static inline bool classof(const Value *V) {
2877 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2881 //===----------------------------------------------------------------------===//
2883 //===----------------------------------------------------------------------===//
2885 /// @brief This class represents a cast from floating point to unsigned integer
2886 class FPToUIInst : public CastInst {
2887 FPToUIInst(const FPToUIInst &CI)
2888 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2891 /// @brief Constructor with insert-before-instruction semantics
2893 Value *S, ///< The value to be converted
2894 const Type *Ty, ///< The type to convert to
2895 const std::string &NameStr = "", ///< A name for the new instruction
2896 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2899 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2907 /// @brief Clone an identical FPToUIInst
2908 virtual CastInst *clone(LLVMContext &Context) const;
2910 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2911 static inline bool classof(const FPToUIInst *) { return true; }
2912 static inline bool classof(const Instruction *I) {
2913 return I->getOpcode() == FPToUI;
2915 static inline bool classof(const Value *V) {
2916 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2920 //===----------------------------------------------------------------------===//
2922 //===----------------------------------------------------------------------===//
2924 /// @brief This class represents a cast from floating point to signed integer.
2925 class FPToSIInst : public CastInst {
2926 FPToSIInst(const FPToSIInst &CI)
2927 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2930 /// @brief Constructor with insert-before-instruction semantics
2932 Value *S, ///< The value to be converted
2933 const Type *Ty, ///< The type to convert to
2934 const std::string &NameStr = "", ///< A name for the new instruction
2935 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2938 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2946 /// @brief Clone an identical FPToSIInst
2947 virtual CastInst *clone(LLVMContext &Context) const;
2949 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2950 static inline bool classof(const FPToSIInst *) { return true; }
2951 static inline bool classof(const Instruction *I) {
2952 return I->getOpcode() == FPToSI;
2954 static inline bool classof(const Value *V) {
2955 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2959 //===----------------------------------------------------------------------===//
2960 // IntToPtrInst Class
2961 //===----------------------------------------------------------------------===//
2963 /// @brief This class represents a cast from an integer to a pointer.
2964 class IntToPtrInst : public CastInst {
2965 IntToPtrInst(const IntToPtrInst &CI)
2966 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2969 /// @brief Constructor with insert-before-instruction semantics
2971 Value *S, ///< The value to be converted
2972 const Type *Ty, ///< The type to convert to
2973 const std::string &NameStr = "", ///< A name for the new instruction
2974 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2977 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2985 /// @brief Clone an identical IntToPtrInst
2986 virtual CastInst *clone(LLVMContext &Context) const;
2988 // Methods for support type inquiry through isa, cast, and dyn_cast:
2989 static inline bool classof(const IntToPtrInst *) { return true; }
2990 static inline bool classof(const Instruction *I) {
2991 return I->getOpcode() == IntToPtr;
2993 static inline bool classof(const Value *V) {
2994 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2998 //===----------------------------------------------------------------------===//
2999 // PtrToIntInst Class
3000 //===----------------------------------------------------------------------===//
3002 /// @brief This class represents a cast from a pointer to an integer
3003 class PtrToIntInst : public CastInst {
3004 PtrToIntInst(const PtrToIntInst &CI)
3005 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3008 /// @brief Constructor with insert-before-instruction semantics
3010 Value *S, ///< The value to be converted
3011 const Type *Ty, ///< The type to convert to
3012 const std::string &NameStr = "", ///< A name for the new instruction
3013 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3016 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3024 /// @brief Clone an identical PtrToIntInst
3025 virtual CastInst *clone(LLVMContext &Context) const;
3027 // Methods for support type inquiry through isa, cast, and dyn_cast:
3028 static inline bool classof(const PtrToIntInst *) { return true; }
3029 static inline bool classof(const Instruction *I) {
3030 return I->getOpcode() == PtrToInt;
3032 static inline bool classof(const Value *V) {
3033 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3037 //===----------------------------------------------------------------------===//
3038 // BitCastInst Class
3039 //===----------------------------------------------------------------------===//
3041 /// @brief This class represents a no-op cast from one type to another.
3042 class BitCastInst : public CastInst {
3043 BitCastInst(const BitCastInst &CI)
3044 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3047 /// @brief Constructor with insert-before-instruction semantics
3049 Value *S, ///< The value to be casted
3050 const Type *Ty, ///< The type to casted to
3051 const std::string &NameStr = "", ///< A name for the new instruction
3052 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3055 /// @brief Constructor with insert-at-end-of-block 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 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3063 /// @brief Clone an identical BitCastInst
3064 virtual CastInst *clone(LLVMContext &Context) const;
3066 // Methods for support type inquiry through isa, cast, and dyn_cast:
3067 static inline bool classof(const BitCastInst *) { return true; }
3068 static inline bool classof(const Instruction *I) {
3069 return I->getOpcode() == BitCast;
3071 static inline bool classof(const Value *V) {
3072 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3076 } // End llvm namespace