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 Twine &Name = "",
45 Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize,
47 unsigned iTy, unsigned Align, const Twine &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 Twine &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc,
108 0, NameStr, InsertBefore) {}
109 MallocInst(const Type *Ty, Value *ArraySize,
110 const Twine &NameStr, BasicBlock *InsertAtEnd)
111 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
113 MallocInst(const Type *Ty, const Twine &NameStr,
114 Instruction *InsertBefore = 0)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
116 MallocInst(const Type *Ty, const Twine &NameStr,
117 BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize,
121 unsigned Align, const Twine &NameStr,
122 BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc,
124 Align, NameStr, InsertAtEnd) {}
125 MallocInst(const Type *Ty, Value *ArraySize,
126 unsigned Align, const Twine &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 Twine &NameStr = "",
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, ArraySize, Alloca,
158 0, NameStr, InsertBefore) {}
159 AllocaInst(const Type *Ty,
160 Value *ArraySize, const Twine &NameStr,
161 BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, const Twine &NameStr,
165 Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167 AllocaInst(const Type *Ty, const Twine &NameStr,
168 BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
171 AllocaInst(const Type *Ty, Value *ArraySize,
172 unsigned Align, const Twine &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 Twine &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 Twine &NameStr, Instruction *InsertBefore);
250 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
251 LoadInst(Value *Ptr, const Twine &NameStr = "", bool isVolatile = false,
252 Instruction *InsertBefore = 0);
253 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
254 unsigned Align, Instruction *InsertBefore = 0);
255 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
256 BasicBlock *InsertAtEnd);
257 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
258 unsigned Align, BasicBlock *InsertAtEnd);
260 /// isVolatile - Return true if this is a load from a volatile memory
263 bool isVolatile() const { return SubclassData & 1; }
265 /// setVolatile - Specify whether this is a volatile load or not.
267 void setVolatile(bool V) {
268 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
271 virtual LoadInst *clone(LLVMContext &Context) const;
273 /// getAlignment - Return the alignment of the access that is being performed
275 unsigned getAlignment() const {
276 return (1 << (SubclassData>>1)) >> 1;
279 void setAlignment(unsigned Align);
281 Value *getPointerOperand() { return getOperand(0); }
282 const Value *getPointerOperand() const { return getOperand(0); }
283 static unsigned getPointerOperandIndex() { return 0U; }
285 // Methods for support type inquiry through isa, cast, and dyn_cast:
286 static inline bool classof(const LoadInst *) { return true; }
287 static inline bool classof(const Instruction *I) {
288 return I->getOpcode() == Instruction::Load;
290 static inline bool classof(const Value *V) {
291 return isa<Instruction>(V) && classof(cast<Instruction>(V));
296 //===----------------------------------------------------------------------===//
298 //===----------------------------------------------------------------------===//
300 /// StoreInst - an instruction for storing to memory
302 class StoreInst : public Instruction {
303 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
305 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
307 Op<0>() = SI.Op<0>();
308 Op<1>() = SI.Op<1>();
309 setVolatile(SI.isVolatile());
310 setAlignment(SI.getAlignment());
318 // allocate space for exactly two operands
319 void *operator new(size_t s) {
320 return User::operator new(s, 2);
322 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
323 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
324 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
325 Instruction *InsertBefore = 0);
326 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
327 unsigned Align, Instruction *InsertBefore = 0);
328 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
329 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
330 unsigned Align, BasicBlock *InsertAtEnd);
333 /// isVolatile - Return true if this is a load from a volatile memory
336 bool isVolatile() const { return SubclassData & 1; }
338 /// setVolatile - Specify whether this is a volatile load or not.
340 void setVolatile(bool V) {
341 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
344 /// Transparently provide more efficient getOperand methods.
345 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
347 /// getAlignment - Return the alignment of the access that is being performed
349 unsigned getAlignment() const {
350 return (1 << (SubclassData>>1)) >> 1;
353 void setAlignment(unsigned Align);
355 virtual StoreInst *clone(LLVMContext &Context) const;
357 Value *getPointerOperand() { return getOperand(1); }
358 const Value *getPointerOperand() const { return getOperand(1); }
359 static unsigned getPointerOperandIndex() { return 1U; }
361 // Methods for support type inquiry through isa, cast, and dyn_cast:
362 static inline bool classof(const StoreInst *) { return true; }
363 static inline bool classof(const Instruction *I) {
364 return I->getOpcode() == Instruction::Store;
366 static inline bool classof(const Value *V) {
367 return isa<Instruction>(V) && classof(cast<Instruction>(V));
372 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
375 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
377 //===----------------------------------------------------------------------===//
378 // GetElementPtrInst Class
379 //===----------------------------------------------------------------------===//
381 // checkType - Simple wrapper function to give a better assertion failure
382 // message on bad indexes for a gep instruction.
384 static inline const Type *checkType(const Type *Ty) {
385 assert(Ty && "Invalid GetElementPtrInst indices for type!");
389 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
390 /// access elements of arrays and structs
392 class GetElementPtrInst : public Instruction {
393 GetElementPtrInst(const GetElementPtrInst &GEPI);
394 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
395 const Twine &NameStr);
396 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
398 template<typename InputIterator>
399 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
400 const Twine &NameStr,
401 // This argument ensures that we have an iterator we can
402 // do arithmetic on in constant time
403 std::random_access_iterator_tag) {
404 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
407 // This requires that the iterator points to contiguous memory.
408 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
409 // we have to build an array here
412 init(Ptr, 0, NumIdx, NameStr);
416 /// getIndexedType - Returns the type of the element that would be loaded with
417 /// a load instruction with the specified parameters.
419 /// Null is returned if the indices are invalid for the specified
422 template<typename InputIterator>
423 static const Type *getIndexedType(const Type *Ptr,
424 InputIterator IdxBegin,
425 InputIterator IdxEnd,
426 // This argument ensures that we
427 // have an iterator we can do
428 // arithmetic on in constant time
429 std::random_access_iterator_tag) {
430 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
433 // This requires that the iterator points to contiguous memory.
434 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
436 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
439 /// Constructors - Create a getelementptr instruction with a base pointer an
440 /// list of indices. The first ctor can optionally insert before an existing
441 /// instruction, the second appends the new instruction to the specified
443 template<typename InputIterator>
444 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
445 InputIterator IdxEnd,
447 const Twine &NameStr,
448 Instruction *InsertBefore);
449 template<typename InputIterator>
450 inline GetElementPtrInst(Value *Ptr,
451 InputIterator IdxBegin, InputIterator IdxEnd,
453 const Twine &NameStr, BasicBlock *InsertAtEnd);
455 /// Constructors - These two constructors are convenience methods because one
456 /// and two index getelementptr instructions are so common.
457 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
458 Instruction *InsertBefore = 0);
459 GetElementPtrInst(Value *Ptr, Value *Idx,
460 const Twine &NameStr, BasicBlock *InsertAtEnd);
462 template<typename InputIterator>
463 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
464 InputIterator IdxEnd,
465 const Twine &NameStr = "",
466 Instruction *InsertBefore = 0) {
467 typename std::iterator_traits<InputIterator>::difference_type Values =
468 1 + std::distance(IdxBegin, IdxEnd);
470 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
472 template<typename InputIterator>
473 static GetElementPtrInst *Create(Value *Ptr,
474 InputIterator IdxBegin, InputIterator IdxEnd,
475 const Twine &NameStr,
476 BasicBlock *InsertAtEnd) {
477 typename std::iterator_traits<InputIterator>::difference_type Values =
478 1 + std::distance(IdxBegin, IdxEnd);
480 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
483 /// Constructors - These two creators are convenience methods because one
484 /// index getelementptr instructions are so common.
485 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
486 const Twine &NameStr = "",
487 Instruction *InsertBefore = 0) {
488 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
490 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
491 const Twine &NameStr,
492 BasicBlock *InsertAtEnd) {
493 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
496 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
498 /// Transparently provide more efficient getOperand methods.
499 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
501 // getType - Overload to return most specific pointer type...
502 const PointerType *getType() const {
503 return reinterpret_cast<const PointerType*>(Instruction::getType());
506 /// getIndexedType - Returns the type of the element that would be loaded with
507 /// a load instruction with the specified parameters.
509 /// Null is returned if the indices are invalid for the specified
512 template<typename InputIterator>
513 static const Type *getIndexedType(const Type *Ptr,
514 InputIterator IdxBegin,
515 InputIterator IdxEnd) {
516 return getIndexedType(Ptr, IdxBegin, IdxEnd,
517 typename std::iterator_traits<InputIterator>::
518 iterator_category());
521 static const Type *getIndexedType(const Type *Ptr,
522 Value* const *Idx, unsigned NumIdx);
524 static const Type *getIndexedType(const Type *Ptr,
525 uint64_t const *Idx, unsigned NumIdx);
527 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
529 inline op_iterator idx_begin() { return op_begin()+1; }
530 inline const_op_iterator idx_begin() const { return op_begin()+1; }
531 inline op_iterator idx_end() { return op_end(); }
532 inline const_op_iterator idx_end() const { return op_end(); }
534 Value *getPointerOperand() {
535 return getOperand(0);
537 const Value *getPointerOperand() const {
538 return getOperand(0);
540 static unsigned getPointerOperandIndex() {
541 return 0U; // get index for modifying correct operand
544 /// getPointerOperandType - Method to return the pointer operand as a
546 const PointerType *getPointerOperandType() const {
547 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
551 unsigned getNumIndices() const { // Note: always non-negative
552 return getNumOperands() - 1;
555 bool hasIndices() const {
556 return getNumOperands() > 1;
559 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
560 /// zeros. If so, the result pointer and the first operand have the same
561 /// value, just potentially different types.
562 bool hasAllZeroIndices() const;
564 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
565 /// constant integers. If so, the result pointer and the first operand have
566 /// a constant offset between them.
567 bool hasAllConstantIndices() const;
569 // Methods for support type inquiry through isa, cast, and dyn_cast:
570 static inline bool classof(const GetElementPtrInst *) { return true; }
571 static inline bool classof(const Instruction *I) {
572 return (I->getOpcode() == Instruction::GetElementPtr);
574 static inline bool classof(const Value *V) {
575 return isa<Instruction>(V) && classof(cast<Instruction>(V));
580 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
583 template<typename InputIterator>
584 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
585 InputIterator IdxBegin,
586 InputIterator IdxEnd,
588 const Twine &NameStr,
589 Instruction *InsertBefore)
590 : Instruction(PointerType::get(checkType(
591 getIndexedType(Ptr->getType(),
593 cast<PointerType>(Ptr->getType())
594 ->getAddressSpace()),
596 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
597 Values, InsertBefore) {
598 init(Ptr, IdxBegin, IdxEnd, NameStr,
599 typename std::iterator_traits<InputIterator>::iterator_category());
601 template<typename InputIterator>
602 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
603 InputIterator IdxBegin,
604 InputIterator IdxEnd,
606 const Twine &NameStr,
607 BasicBlock *InsertAtEnd)
608 : Instruction(PointerType::get(checkType(
609 getIndexedType(Ptr->getType(),
611 cast<PointerType>(Ptr->getType())
612 ->getAddressSpace()),
614 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
615 Values, InsertAtEnd) {
616 init(Ptr, IdxBegin, IdxEnd, NameStr,
617 typename std::iterator_traits<InputIterator>::iterator_category());
621 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
624 //===----------------------------------------------------------------------===//
626 //===----------------------------------------------------------------------===//
628 /// This instruction compares its operands according to the predicate given
629 /// to the constructor. It only operates on integers or pointers. The operands
630 /// must be identical types.
631 /// @brief Represent an integer comparison operator.
632 class ICmpInst: public CmpInst {
634 /// @brief Constructor with insert-before-instruction semantics.
636 Instruction *InsertBefore, ///< Where to insert
637 Predicate pred, ///< The predicate to use for the comparison
638 Value *LHS, ///< The left-hand-side of the expression
639 Value *RHS, ///< The right-hand-side of the expression
640 const Twine &NameStr = "" ///< Name of the instruction
641 ) : CmpInst(makeCmpResultType(LHS->getType()),
642 Instruction::ICmp, pred, LHS, RHS, NameStr,
644 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
645 pred <= CmpInst::LAST_ICMP_PREDICATE &&
646 "Invalid ICmp predicate value");
647 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
648 "Both operands to ICmp instruction are not of the same type!");
649 // Check that the operands are the right type
650 assert((getOperand(0)->getType()->isIntOrIntVector() ||
651 isa<PointerType>(getOperand(0)->getType())) &&
652 "Invalid operand types for ICmp instruction");
655 /// @brief Constructor with insert-at-end semantics.
657 BasicBlock &InsertAtEnd, ///< Block to insert into.
658 Predicate pred, ///< The predicate to use for the comparison
659 Value *LHS, ///< The left-hand-side of the expression
660 Value *RHS, ///< The right-hand-side of the expression
661 const Twine &NameStr = "" ///< Name of the instruction
662 ) : CmpInst(makeCmpResultType(LHS->getType()),
663 Instruction::ICmp, pred, LHS, RHS, NameStr,
665 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
666 pred <= CmpInst::LAST_ICMP_PREDICATE &&
667 "Invalid ICmp predicate value");
668 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
669 "Both operands to ICmp instruction are not of the same type!");
670 // Check that the operands are the right type
671 assert((getOperand(0)->getType()->isIntOrIntVector() ||
672 isa<PointerType>(getOperand(0)->getType())) &&
673 "Invalid operand types for ICmp instruction");
676 /// @brief Constructor with no-insertion semantics
678 LLVMContext &Context, ///< Context to construct within
679 Predicate pred, ///< The predicate to use for the comparison
680 Value *LHS, ///< The left-hand-side of the expression
681 Value *RHS, ///< The right-hand-side of the expression
682 const Twine &NameStr = "" ///< Name of the instruction
683 ) : CmpInst(makeCmpResultType(LHS->getType()),
684 Instruction::ICmp, pred, LHS, RHS, NameStr) {
685 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
686 pred <= CmpInst::LAST_ICMP_PREDICATE &&
687 "Invalid ICmp predicate value");
688 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
689 "Both operands to ICmp instruction are not of the same type!");
690 // Check that the operands are the right type
691 assert((getOperand(0)->getType()->isIntOrIntVector() ||
692 isa<PointerType>(getOperand(0)->getType())) &&
693 "Invalid operand types for ICmp instruction");
696 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
697 /// @returns the predicate that would be the result if the operand were
698 /// regarded as signed.
699 /// @brief Return the signed version of the predicate
700 Predicate getSignedPredicate() const {
701 return getSignedPredicate(getPredicate());
704 /// This is a static version that you can use without an instruction.
705 /// @brief Return the signed version of the predicate.
706 static Predicate getSignedPredicate(Predicate pred);
708 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
709 /// @returns the predicate that would be the result if the operand were
710 /// regarded as unsigned.
711 /// @brief Return the unsigned version of the predicate
712 Predicate getUnsignedPredicate() const {
713 return getUnsignedPredicate(getPredicate());
716 /// This is a static version that you can use without an instruction.
717 /// @brief Return the unsigned version of the predicate.
718 static Predicate getUnsignedPredicate(Predicate pred);
720 /// isEquality - Return true if this predicate is either EQ or NE. This also
721 /// tests for commutativity.
722 static bool isEquality(Predicate P) {
723 return P == ICMP_EQ || P == ICMP_NE;
726 /// isEquality - Return true if this predicate is either EQ or NE. This also
727 /// tests for commutativity.
728 bool isEquality() const {
729 return isEquality(getPredicate());
732 /// @returns true if the predicate of this ICmpInst is commutative
733 /// @brief Determine if this relation is commutative.
734 bool isCommutative() const { return isEquality(); }
736 /// isRelational - Return true if the predicate is relational (not EQ or NE).
738 bool isRelational() const {
739 return !isEquality();
742 /// isRelational - Return true if the predicate is relational (not EQ or NE).
744 static bool isRelational(Predicate P) {
745 return !isEquality(P);
748 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
749 /// @brief Determine if this instruction's predicate is signed.
750 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
752 /// @returns true if the predicate provided is signed, false otherwise
753 /// @brief Determine if the predicate is signed.
754 static bool isSignedPredicate(Predicate pred);
756 /// @returns true if the specified compare predicate is
757 /// true when both operands are equal...
758 /// @brief Determine if the icmp is true when both operands are equal
759 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
760 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
761 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
762 pred == ICmpInst::ICMP_SLE;
765 /// @returns true if the specified compare instruction is
766 /// true when both operands are equal...
767 /// @brief Determine if the ICmpInst returns true when both operands are equal
768 bool isTrueWhenEqual() {
769 return isTrueWhenEqual(getPredicate());
772 /// Initialize a set of values that all satisfy the predicate with C.
773 /// @brief Make a ConstantRange for a relation with a constant value.
774 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
776 /// Exchange the two operands to this instruction in such a way that it does
777 /// not modify the semantics of the instruction. The predicate value may be
778 /// changed to retain the same result if the predicate is order dependent
780 /// @brief Swap operands and adjust predicate.
781 void swapOperands() {
782 SubclassData = getSwappedPredicate();
783 Op<0>().swap(Op<1>());
786 virtual ICmpInst *clone(LLVMContext &Context) const;
788 // Methods for support type inquiry through isa, cast, and dyn_cast:
789 static inline bool classof(const ICmpInst *) { return true; }
790 static inline bool classof(const Instruction *I) {
791 return I->getOpcode() == Instruction::ICmp;
793 static inline bool classof(const Value *V) {
794 return isa<Instruction>(V) && classof(cast<Instruction>(V));
799 //===----------------------------------------------------------------------===//
801 //===----------------------------------------------------------------------===//
803 /// This instruction compares its operands according to the predicate given
804 /// to the constructor. It only operates on floating point values or packed
805 /// vectors of floating point values. The operands must be identical types.
806 /// @brief Represents a floating point comparison operator.
807 class FCmpInst: public CmpInst {
809 /// @brief Constructor with insert-before-instruction semantics.
811 Instruction *InsertBefore, ///< Where to insert
812 Predicate pred, ///< The predicate to use for the comparison
813 Value *LHS, ///< The left-hand-side of the expression
814 Value *RHS, ///< The right-hand-side of the expression
815 const Twine &NameStr = "" ///< Name of the instruction
816 ) : CmpInst(makeCmpResultType(LHS->getType()),
817 Instruction::FCmp, pred, LHS, RHS, NameStr,
819 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
820 "Invalid FCmp predicate value");
821 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
822 "Both operands to FCmp instruction are not of the same type!");
823 // Check that the operands are the right type
824 assert(getOperand(0)->getType()->isFPOrFPVector() &&
825 "Invalid operand types for FCmp instruction");
828 /// @brief Constructor with insert-at-end semantics.
830 BasicBlock &InsertAtEnd, ///< Block to insert into.
831 Predicate pred, ///< The predicate to use for the comparison
832 Value *LHS, ///< The left-hand-side of the expression
833 Value *RHS, ///< The right-hand-side of the expression
834 const Twine &NameStr = "" ///< Name of the instruction
835 ) : CmpInst(makeCmpResultType(LHS->getType()),
836 Instruction::FCmp, pred, LHS, RHS, NameStr,
838 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
839 "Invalid FCmp predicate value");
840 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
841 "Both operands to FCmp instruction are not of the same type!");
842 // Check that the operands are the right type
843 assert(getOperand(0)->getType()->isFPOrFPVector() &&
844 "Invalid operand types for FCmp instruction");
847 /// @brief Constructor with no-insertion semantics
849 LLVMContext &Context, ///< Context to build in
850 Predicate pred, ///< The predicate to use for the comparison
851 Value *LHS, ///< The left-hand-side of the expression
852 Value *RHS, ///< The right-hand-side of the expression
853 const Twine &NameStr = "" ///< Name of the instruction
854 ) : CmpInst(makeCmpResultType(LHS->getType()),
855 Instruction::FCmp, pred, LHS, RHS, NameStr) {
856 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
857 "Invalid FCmp predicate value");
858 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
859 "Both operands to FCmp instruction are not of the same type!");
860 // Check that the operands are the right type
861 assert(getOperand(0)->getType()->isFPOrFPVector() &&
862 "Invalid operand types for FCmp instruction");
865 /// @returns true if the predicate of this instruction is EQ or NE.
866 /// @brief Determine if this is an equality predicate.
867 bool isEquality() const {
868 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
869 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
872 /// @returns true if the predicate of this instruction is commutative.
873 /// @brief Determine if this is a commutative predicate.
874 bool isCommutative() const {
875 return isEquality() ||
876 SubclassData == FCMP_FALSE ||
877 SubclassData == FCMP_TRUE ||
878 SubclassData == FCMP_ORD ||
879 SubclassData == FCMP_UNO;
882 /// @returns true if the predicate is relational (not EQ or NE).
883 /// @brief Determine if this a relational predicate.
884 bool isRelational() const { return !isEquality(); }
886 /// Exchange the two operands to this instruction in such a way that it does
887 /// not modify the semantics of the instruction. The predicate value may be
888 /// changed to retain the same result if the predicate is order dependent
890 /// @brief Swap operands and adjust predicate.
891 void swapOperands() {
892 SubclassData = getSwappedPredicate();
893 Op<0>().swap(Op<1>());
896 virtual FCmpInst *clone(LLVMContext &Context) const;
898 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
899 static inline bool classof(const FCmpInst *) { return true; }
900 static inline bool classof(const Instruction *I) {
901 return I->getOpcode() == Instruction::FCmp;
903 static inline bool classof(const Value *V) {
904 return isa<Instruction>(V) && classof(cast<Instruction>(V));
908 //===----------------------------------------------------------------------===//
910 //===----------------------------------------------------------------------===//
911 /// CallInst - This class represents a function call, abstracting a target
912 /// machine's calling convention. This class uses low bit of the SubClassData
913 /// field to indicate whether or not this is a tail call. The rest of the bits
914 /// hold the calling convention of the call.
917 class CallInst : public Instruction {
918 AttrListPtr AttributeList; ///< parameter attributes for call
919 CallInst(const CallInst &CI);
920 void init(Value *Func, Value* const *Params, unsigned NumParams);
921 void init(Value *Func, Value *Actual1, Value *Actual2);
922 void init(Value *Func, Value *Actual);
923 void init(Value *Func);
925 template<typename InputIterator>
926 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
927 const Twine &NameStr,
928 // This argument ensures that we have an iterator we can
929 // do arithmetic on in constant time
930 std::random_access_iterator_tag) {
931 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
933 // This requires that the iterator points to contiguous memory.
934 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
938 /// Construct a CallInst given a range of arguments. InputIterator
939 /// must be a random-access iterator pointing to contiguous storage
940 /// (e.g. a std::vector<>::iterator). Checks are made for
941 /// random-accessness but not for contiguous storage as that would
942 /// incur runtime overhead.
943 /// @brief Construct a CallInst from a range of arguments
944 template<typename InputIterator>
945 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
946 const Twine &NameStr, Instruction *InsertBefore);
948 /// Construct a CallInst given a range of arguments. InputIterator
949 /// must be a random-access iterator pointing to contiguous storage
950 /// (e.g. a std::vector<>::iterator). Checks are made for
951 /// random-accessness but not for contiguous storage as that would
952 /// incur runtime overhead.
953 /// @brief Construct a CallInst from a range of arguments
954 template<typename InputIterator>
955 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
956 const Twine &NameStr, BasicBlock *InsertAtEnd);
958 CallInst(Value *F, Value *Actual, const Twine &NameStr,
959 Instruction *InsertBefore);
960 CallInst(Value *F, Value *Actual, const Twine &NameStr,
961 BasicBlock *InsertAtEnd);
962 explicit CallInst(Value *F, const Twine &NameStr,
963 Instruction *InsertBefore);
964 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
966 template<typename InputIterator>
967 static CallInst *Create(Value *Func,
968 InputIterator ArgBegin, InputIterator ArgEnd,
969 const Twine &NameStr = "",
970 Instruction *InsertBefore = 0) {
971 return new((unsigned)(ArgEnd - ArgBegin + 1))
972 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
974 template<typename InputIterator>
975 static CallInst *Create(Value *Func,
976 InputIterator ArgBegin, InputIterator ArgEnd,
977 const Twine &NameStr, BasicBlock *InsertAtEnd) {
978 return new((unsigned)(ArgEnd - ArgBegin + 1))
979 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
981 static CallInst *Create(Value *F, Value *Actual,
982 const Twine &NameStr = "",
983 Instruction *InsertBefore = 0) {
984 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
986 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
987 BasicBlock *InsertAtEnd) {
988 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
990 static CallInst *Create(Value *F, const Twine &NameStr = "",
991 Instruction *InsertBefore = 0) {
992 return new(1) CallInst(F, NameStr, InsertBefore);
994 static CallInst *Create(Value *F, const Twine &NameStr,
995 BasicBlock *InsertAtEnd) {
996 return new(1) CallInst(F, NameStr, InsertAtEnd);
1001 bool isTailCall() const { return SubclassData & 1; }
1002 void setTailCall(bool isTC = true) {
1003 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1006 virtual CallInst *clone(LLVMContext &Context) const;
1008 /// Provide fast operand accessors
1009 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1011 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1013 unsigned getCallingConv() const { return SubclassData >> 1; }
1014 void setCallingConv(unsigned CC) {
1015 SubclassData = (SubclassData & 1) | (CC << 1);
1018 /// getAttributes - Return the parameter attributes for this call.
1020 const AttrListPtr &getAttributes() const { return AttributeList; }
1022 /// setAttributes - Set the parameter attributes for this call.
1024 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1026 /// addAttribute - adds the attribute to the list of attributes.
1027 void addAttribute(unsigned i, Attributes attr);
1029 /// removeAttribute - removes the attribute from the list of attributes.
1030 void removeAttribute(unsigned i, Attributes attr);
1032 /// @brief Determine whether the call or the callee has the given attribute.
1033 bool paramHasAttr(unsigned i, Attributes attr) const;
1035 /// @brief Extract the alignment for a call or parameter (0=unknown).
1036 unsigned getParamAlignment(unsigned i) const {
1037 return AttributeList.getParamAlignment(i);
1040 /// @brief Determine if the call does not access memory.
1041 bool doesNotAccessMemory() const {
1042 return paramHasAttr(~0, Attribute::ReadNone);
1044 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1045 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1046 else removeAttribute(~0, Attribute::ReadNone);
1049 /// @brief Determine if the call does not access or only reads memory.
1050 bool onlyReadsMemory() const {
1051 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1053 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1054 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1055 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1058 /// @brief Determine if the call cannot return.
1059 bool doesNotReturn() const {
1060 return paramHasAttr(~0, Attribute::NoReturn);
1062 void setDoesNotReturn(bool DoesNotReturn = true) {
1063 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1064 else removeAttribute(~0, Attribute::NoReturn);
1067 /// @brief Determine if the call cannot unwind.
1068 bool doesNotThrow() const {
1069 return paramHasAttr(~0, Attribute::NoUnwind);
1071 void setDoesNotThrow(bool DoesNotThrow = true) {
1072 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1073 else removeAttribute(~0, Attribute::NoUnwind);
1076 /// @brief Determine if the call returns a structure through first
1077 /// pointer argument.
1078 bool hasStructRetAttr() const {
1079 // Be friendly and also check the callee.
1080 return paramHasAttr(1, Attribute::StructRet);
1083 /// @brief Determine if any call argument is an aggregate passed by value.
1084 bool hasByValArgument() const {
1085 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1088 /// getCalledFunction - Return the function called, or null if this is an
1089 /// indirect function invocation.
1091 Function *getCalledFunction() const {
1092 return dyn_cast<Function>(Op<0>());
1095 /// getCalledValue - Get a pointer to the function that is invoked by this
1097 const Value *getCalledValue() const { return Op<0>(); }
1098 Value *getCalledValue() { return Op<0>(); }
1100 // Methods for support type inquiry through isa, cast, and dyn_cast:
1101 static inline bool classof(const CallInst *) { return true; }
1102 static inline bool classof(const Instruction *I) {
1103 return I->getOpcode() == Instruction::Call;
1105 static inline bool classof(const Value *V) {
1106 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1111 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1114 template<typename InputIterator>
1115 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1116 const Twine &NameStr, BasicBlock *InsertAtEnd)
1117 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1118 ->getElementType())->getReturnType(),
1120 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1121 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1122 init(Func, ArgBegin, ArgEnd, NameStr,
1123 typename std::iterator_traits<InputIterator>::iterator_category());
1126 template<typename InputIterator>
1127 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1128 const Twine &NameStr, Instruction *InsertBefore)
1129 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1130 ->getElementType())->getReturnType(),
1132 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1133 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1134 init(Func, ArgBegin, ArgEnd, NameStr,
1135 typename std::iterator_traits<InputIterator>::iterator_category());
1138 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1140 //===----------------------------------------------------------------------===//
1142 //===----------------------------------------------------------------------===//
1144 /// SelectInst - This class represents the LLVM 'select' instruction.
1146 class SelectInst : public Instruction {
1147 void init(Value *C, Value *S1, Value *S2) {
1148 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1154 SelectInst(const SelectInst &SI)
1155 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1156 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1158 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1159 Instruction *InsertBefore)
1160 : Instruction(S1->getType(), Instruction::Select,
1161 &Op<0>(), 3, InsertBefore) {
1165 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1166 BasicBlock *InsertAtEnd)
1167 : Instruction(S1->getType(), Instruction::Select,
1168 &Op<0>(), 3, InsertAtEnd) {
1173 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1174 const Twine &NameStr = "",
1175 Instruction *InsertBefore = 0) {
1176 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1178 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1179 const Twine &NameStr,
1180 BasicBlock *InsertAtEnd) {
1181 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1184 Value *getCondition() const { return Op<0>(); }
1185 Value *getTrueValue() const { return Op<1>(); }
1186 Value *getFalseValue() const { return Op<2>(); }
1188 /// areInvalidOperands - Return a string if the specified operands are invalid
1189 /// for a select operation, otherwise return null.
1190 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1192 /// Transparently provide more efficient getOperand methods.
1193 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1195 OtherOps getOpcode() const {
1196 return static_cast<OtherOps>(Instruction::getOpcode());
1199 virtual SelectInst *clone(LLVMContext &Context) const;
1201 // Methods for support type inquiry through isa, cast, and dyn_cast:
1202 static inline bool classof(const SelectInst *) { return true; }
1203 static inline bool classof(const Instruction *I) {
1204 return I->getOpcode() == Instruction::Select;
1206 static inline bool classof(const Value *V) {
1207 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1212 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1215 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1217 //===----------------------------------------------------------------------===//
1219 //===----------------------------------------------------------------------===//
1221 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1222 /// an argument of the specified type given a va_list and increments that list
1224 class VAArgInst : public UnaryInstruction {
1225 VAArgInst(const VAArgInst &VAA)
1226 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1228 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1229 Instruction *InsertBefore = 0)
1230 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1233 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1234 BasicBlock *InsertAtEnd)
1235 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1239 virtual VAArgInst *clone(LLVMContext &Context) const;
1241 // Methods for support type inquiry through isa, cast, and dyn_cast:
1242 static inline bool classof(const VAArgInst *) { return true; }
1243 static inline bool classof(const Instruction *I) {
1244 return I->getOpcode() == VAArg;
1246 static inline bool classof(const Value *V) {
1247 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1251 //===----------------------------------------------------------------------===//
1252 // ExtractElementInst Class
1253 //===----------------------------------------------------------------------===//
1255 /// ExtractElementInst - This instruction extracts a single (scalar)
1256 /// element from a VectorType value
1258 class ExtractElementInst : public Instruction {
1259 ExtractElementInst(const ExtractElementInst &EE) :
1260 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1261 Op<0>() = EE.Op<0>();
1262 Op<1>() = EE.Op<1>();
1265 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1266 Instruction *InsertBefore = 0);
1267 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1268 BasicBlock *InsertAtEnd);
1270 static ExtractElementInst *Create(const ExtractElementInst &EE) {
1271 return new(EE.getNumOperands()) ExtractElementInst(EE);
1274 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1275 const Twine &NameStr = "",
1276 Instruction *InsertBefore = 0) {
1277 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1279 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1280 const Twine &NameStr,
1281 BasicBlock *InsertAtEnd) {
1282 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1285 /// isValidOperands - Return true if an extractelement instruction can be
1286 /// formed with the specified operands.
1287 static bool isValidOperands(const Value *Vec, const Value *Idx);
1289 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1291 /// Transparently provide more efficient getOperand methods.
1292 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1294 // Methods for support type inquiry through isa, cast, and dyn_cast:
1295 static inline bool classof(const ExtractElementInst *) { return true; }
1296 static inline bool classof(const Instruction *I) {
1297 return I->getOpcode() == Instruction::ExtractElement;
1299 static inline bool classof(const Value *V) {
1300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1305 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1308 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1310 //===----------------------------------------------------------------------===//
1311 // InsertElementInst Class
1312 //===----------------------------------------------------------------------===//
1314 /// InsertElementInst - This instruction inserts a single (scalar)
1315 /// element into a VectorType value
1317 class InsertElementInst : public Instruction {
1318 InsertElementInst(const InsertElementInst &IE);
1319 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1320 const Twine &NameStr = "",
1321 Instruction *InsertBefore = 0);
1322 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1323 const Twine &NameStr, BasicBlock *InsertAtEnd);
1325 static InsertElementInst *Create(const InsertElementInst &IE) {
1326 return new(IE.getNumOperands()) InsertElementInst(IE);
1328 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1329 const Twine &NameStr = "",
1330 Instruction *InsertBefore = 0) {
1331 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1333 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1334 const Twine &NameStr,
1335 BasicBlock *InsertAtEnd) {
1336 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1339 /// isValidOperands - Return true if an insertelement instruction can be
1340 /// formed with the specified operands.
1341 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1344 virtual InsertElementInst *clone(LLVMContext &Context) const;
1346 /// getType - Overload to return most specific vector type.
1348 const VectorType *getType() const {
1349 return reinterpret_cast<const VectorType*>(Instruction::getType());
1352 /// Transparently provide more efficient getOperand methods.
1353 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1355 // Methods for support type inquiry through isa, cast, and dyn_cast:
1356 static inline bool classof(const InsertElementInst *) { return true; }
1357 static inline bool classof(const Instruction *I) {
1358 return I->getOpcode() == Instruction::InsertElement;
1360 static inline bool classof(const Value *V) {
1361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1366 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1369 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1371 //===----------------------------------------------------------------------===//
1372 // ShuffleVectorInst Class
1373 //===----------------------------------------------------------------------===//
1375 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1378 class ShuffleVectorInst : public Instruction {
1379 ShuffleVectorInst(const ShuffleVectorInst &IE);
1381 // allocate space for exactly three operands
1382 void *operator new(size_t s) {
1383 return User::operator new(s, 3);
1385 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1386 const Twine &NameStr = "",
1387 Instruction *InsertBefor = 0);
1388 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1389 const Twine &NameStr, BasicBlock *InsertAtEnd);
1391 /// isValidOperands - Return true if a shufflevector instruction can be
1392 /// formed with the specified operands.
1393 static bool isValidOperands(const Value *V1, const Value *V2,
1396 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1398 /// getType - Overload to return most specific vector type.
1400 const VectorType *getType() const {
1401 return reinterpret_cast<const VectorType*>(Instruction::getType());
1404 /// Transparently provide more efficient getOperand methods.
1405 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1407 /// getMaskValue - Return the index from the shuffle mask for the specified
1408 /// output result. This is either -1 if the element is undef or a number less
1409 /// than 2*numelements.
1410 int getMaskValue(unsigned i) const;
1412 // Methods for support type inquiry through isa, cast, and dyn_cast:
1413 static inline bool classof(const ShuffleVectorInst *) { return true; }
1414 static inline bool classof(const Instruction *I) {
1415 return I->getOpcode() == Instruction::ShuffleVector;
1417 static inline bool classof(const Value *V) {
1418 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1423 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1426 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1428 //===----------------------------------------------------------------------===//
1429 // ExtractValueInst Class
1430 //===----------------------------------------------------------------------===//
1432 /// ExtractValueInst - This instruction extracts a struct member or array
1433 /// element value from an aggregate value.
1435 class ExtractValueInst : public UnaryInstruction {
1436 SmallVector<unsigned, 4> Indices;
1438 ExtractValueInst(const ExtractValueInst &EVI);
1439 void init(const unsigned *Idx, unsigned NumIdx,
1440 const Twine &NameStr);
1441 void init(unsigned Idx, const Twine &NameStr);
1443 template<typename InputIterator>
1444 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1445 const Twine &NameStr,
1446 // This argument ensures that we have an iterator we can
1447 // do arithmetic on in constant time
1448 std::random_access_iterator_tag) {
1449 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1451 // There's no fundamental reason why we require at least one index
1452 // (other than weirdness with &*IdxBegin being invalid; see
1453 // getelementptr's init routine for example). But there's no
1454 // present need to support it.
1455 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1457 // This requires that the iterator points to contiguous memory.
1458 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1459 // we have to build an array here
1462 /// getIndexedType - Returns the type of the element that would be extracted
1463 /// with an extractvalue instruction with the specified parameters.
1465 /// Null is returned if the indices are invalid for the specified
1468 static const Type *getIndexedType(const Type *Agg,
1469 const unsigned *Idx, unsigned NumIdx);
1471 template<typename InputIterator>
1472 static const Type *getIndexedType(const Type *Ptr,
1473 InputIterator IdxBegin,
1474 InputIterator IdxEnd,
1475 // This argument ensures that we
1476 // have an iterator we can do
1477 // arithmetic on in constant time
1478 std::random_access_iterator_tag) {
1479 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1482 // This requires that the iterator points to contiguous memory.
1483 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1485 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1488 /// Constructors - Create a extractvalue instruction with a base aggregate
1489 /// value and a list of indices. The first ctor can optionally insert before
1490 /// an existing instruction, the second appends the new instruction to the
1491 /// specified BasicBlock.
1492 template<typename InputIterator>
1493 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1494 InputIterator IdxEnd,
1495 const Twine &NameStr,
1496 Instruction *InsertBefore);
1497 template<typename InputIterator>
1498 inline ExtractValueInst(Value *Agg,
1499 InputIterator IdxBegin, InputIterator IdxEnd,
1500 const Twine &NameStr, BasicBlock *InsertAtEnd);
1502 // allocate space for exactly one operand
1503 void *operator new(size_t s) {
1504 return User::operator new(s, 1);
1508 template<typename InputIterator>
1509 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1510 InputIterator IdxEnd,
1511 const Twine &NameStr = "",
1512 Instruction *InsertBefore = 0) {
1514 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1516 template<typename InputIterator>
1517 static ExtractValueInst *Create(Value *Agg,
1518 InputIterator IdxBegin, InputIterator IdxEnd,
1519 const Twine &NameStr,
1520 BasicBlock *InsertAtEnd) {
1521 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1524 /// Constructors - These two creators are convenience methods because one
1525 /// index extractvalue instructions are much more common than those with
1527 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1528 const Twine &NameStr = "",
1529 Instruction *InsertBefore = 0) {
1530 unsigned Idxs[1] = { Idx };
1531 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1533 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1534 const Twine &NameStr,
1535 BasicBlock *InsertAtEnd) {
1536 unsigned Idxs[1] = { Idx };
1537 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1540 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1542 /// getIndexedType - Returns the type of the element that would be extracted
1543 /// with an extractvalue instruction with the specified parameters.
1545 /// Null is returned if the indices are invalid for the specified
1548 template<typename InputIterator>
1549 static const Type *getIndexedType(const Type *Ptr,
1550 InputIterator IdxBegin,
1551 InputIterator IdxEnd) {
1552 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1553 typename std::iterator_traits<InputIterator>::
1554 iterator_category());
1556 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1558 typedef const unsigned* idx_iterator;
1559 inline idx_iterator idx_begin() const { return Indices.begin(); }
1560 inline idx_iterator idx_end() const { return Indices.end(); }
1562 Value *getAggregateOperand() {
1563 return getOperand(0);
1565 const Value *getAggregateOperand() const {
1566 return getOperand(0);
1568 static unsigned getAggregateOperandIndex() {
1569 return 0U; // get index for modifying correct operand
1572 unsigned getNumIndices() const { // Note: always non-negative
1573 return (unsigned)Indices.size();
1576 bool hasIndices() const {
1580 // Methods for support type inquiry through isa, cast, and dyn_cast:
1581 static inline bool classof(const ExtractValueInst *) { return true; }
1582 static inline bool classof(const Instruction *I) {
1583 return I->getOpcode() == Instruction::ExtractValue;
1585 static inline bool classof(const Value *V) {
1586 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1590 template<typename InputIterator>
1591 ExtractValueInst::ExtractValueInst(Value *Agg,
1592 InputIterator IdxBegin,
1593 InputIterator IdxEnd,
1594 const Twine &NameStr,
1595 Instruction *InsertBefore)
1596 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1598 ExtractValue, Agg, InsertBefore) {
1599 init(IdxBegin, IdxEnd, NameStr,
1600 typename std::iterator_traits<InputIterator>::iterator_category());
1602 template<typename InputIterator>
1603 ExtractValueInst::ExtractValueInst(Value *Agg,
1604 InputIterator IdxBegin,
1605 InputIterator IdxEnd,
1606 const Twine &NameStr,
1607 BasicBlock *InsertAtEnd)
1608 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1610 ExtractValue, Agg, InsertAtEnd) {
1611 init(IdxBegin, IdxEnd, NameStr,
1612 typename std::iterator_traits<InputIterator>::iterator_category());
1616 //===----------------------------------------------------------------------===//
1617 // InsertValueInst Class
1618 //===----------------------------------------------------------------------===//
1620 /// InsertValueInst - This instruction inserts a struct field of array element
1621 /// value into an aggregate value.
1623 class InsertValueInst : public Instruction {
1624 SmallVector<unsigned, 4> Indices;
1626 void *operator new(size_t, unsigned); // Do not implement
1627 InsertValueInst(const InsertValueInst &IVI);
1628 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1629 const Twine &NameStr);
1630 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1632 template<typename InputIterator>
1633 void init(Value *Agg, Value *Val,
1634 InputIterator IdxBegin, InputIterator IdxEnd,
1635 const Twine &NameStr,
1636 // This argument ensures that we have an iterator we can
1637 // do arithmetic on in constant time
1638 std::random_access_iterator_tag) {
1639 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1641 // There's no fundamental reason why we require at least one index
1642 // (other than weirdness with &*IdxBegin being invalid; see
1643 // getelementptr's init routine for example). But there's no
1644 // present need to support it.
1645 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1647 // This requires that the iterator points to contiguous memory.
1648 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1649 // we have to build an array here
1652 /// Constructors - Create a insertvalue instruction with a base aggregate
1653 /// value, a value to insert, and a list of indices. The first ctor can
1654 /// optionally insert before an existing instruction, the second appends
1655 /// the new instruction to the specified BasicBlock.
1656 template<typename InputIterator>
1657 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1658 InputIterator IdxEnd,
1659 const Twine &NameStr,
1660 Instruction *InsertBefore);
1661 template<typename InputIterator>
1662 inline InsertValueInst(Value *Agg, Value *Val,
1663 InputIterator IdxBegin, InputIterator IdxEnd,
1664 const Twine &NameStr, BasicBlock *InsertAtEnd);
1666 /// Constructors - These two constructors are convenience methods because one
1667 /// and two index insertvalue instructions are so common.
1668 InsertValueInst(Value *Agg, Value *Val,
1669 unsigned Idx, const Twine &NameStr = "",
1670 Instruction *InsertBefore = 0);
1671 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1672 const Twine &NameStr, BasicBlock *InsertAtEnd);
1674 // allocate space for exactly two operands
1675 void *operator new(size_t s) {
1676 return User::operator new(s, 2);
1679 template<typename InputIterator>
1680 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1681 InputIterator IdxEnd,
1682 const Twine &NameStr = "",
1683 Instruction *InsertBefore = 0) {
1684 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1685 NameStr, InsertBefore);
1687 template<typename InputIterator>
1688 static InsertValueInst *Create(Value *Agg, Value *Val,
1689 InputIterator IdxBegin, InputIterator IdxEnd,
1690 const Twine &NameStr,
1691 BasicBlock *InsertAtEnd) {
1692 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1693 NameStr, InsertAtEnd);
1696 /// Constructors - These two creators are convenience methods because one
1697 /// index insertvalue instructions are much more common than those with
1699 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1700 const Twine &NameStr = "",
1701 Instruction *InsertBefore = 0) {
1702 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1704 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1705 const Twine &NameStr,
1706 BasicBlock *InsertAtEnd) {
1707 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1710 virtual InsertValueInst *clone(LLVMContext &Context) const;
1712 /// Transparently provide more efficient getOperand methods.
1713 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1715 typedef const unsigned* idx_iterator;
1716 inline idx_iterator idx_begin() const { return Indices.begin(); }
1717 inline idx_iterator idx_end() const { return Indices.end(); }
1719 Value *getAggregateOperand() {
1720 return getOperand(0);
1722 const Value *getAggregateOperand() const {
1723 return getOperand(0);
1725 static unsigned getAggregateOperandIndex() {
1726 return 0U; // get index for modifying correct operand
1729 Value *getInsertedValueOperand() {
1730 return getOperand(1);
1732 const Value *getInsertedValueOperand() const {
1733 return getOperand(1);
1735 static unsigned getInsertedValueOperandIndex() {
1736 return 1U; // get index for modifying correct operand
1739 unsigned getNumIndices() const { // Note: always non-negative
1740 return (unsigned)Indices.size();
1743 bool hasIndices() const {
1747 // Methods for support type inquiry through isa, cast, and dyn_cast:
1748 static inline bool classof(const InsertValueInst *) { return true; }
1749 static inline bool classof(const Instruction *I) {
1750 return I->getOpcode() == Instruction::InsertValue;
1752 static inline bool classof(const Value *V) {
1753 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1758 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1761 template<typename InputIterator>
1762 InsertValueInst::InsertValueInst(Value *Agg,
1764 InputIterator IdxBegin,
1765 InputIterator IdxEnd,
1766 const Twine &NameStr,
1767 Instruction *InsertBefore)
1768 : Instruction(Agg->getType(), InsertValue,
1769 OperandTraits<InsertValueInst>::op_begin(this),
1771 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1772 typename std::iterator_traits<InputIterator>::iterator_category());
1774 template<typename InputIterator>
1775 InsertValueInst::InsertValueInst(Value *Agg,
1777 InputIterator IdxBegin,
1778 InputIterator IdxEnd,
1779 const Twine &NameStr,
1780 BasicBlock *InsertAtEnd)
1781 : Instruction(Agg->getType(), InsertValue,
1782 OperandTraits<InsertValueInst>::op_begin(this),
1784 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1785 typename std::iterator_traits<InputIterator>::iterator_category());
1788 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1790 //===----------------------------------------------------------------------===//
1792 //===----------------------------------------------------------------------===//
1794 // PHINode - The PHINode class is used to represent the magical mystical PHI
1795 // node, that can not exist in nature, but can be synthesized in a computer
1796 // scientist's overactive imagination.
1798 class PHINode : public Instruction {
1799 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1800 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1801 /// the number actually in use.
1802 unsigned ReservedSpace;
1803 PHINode(const PHINode &PN);
1804 // allocate space for exactly zero operands
1805 void *operator new(size_t s) {
1806 return User::operator new(s, 0);
1808 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1809 Instruction *InsertBefore = 0)
1810 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1815 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1816 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1821 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1822 Instruction *InsertBefore = 0) {
1823 return new PHINode(Ty, NameStr, InsertBefore);
1825 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1826 BasicBlock *InsertAtEnd) {
1827 return new PHINode(Ty, NameStr, InsertAtEnd);
1831 /// reserveOperandSpace - This method can be used to avoid repeated
1832 /// reallocation of PHI operand lists by reserving space for the correct
1833 /// number of operands before adding them. Unlike normal vector reserves,
1834 /// this method can also be used to trim the operand space.
1835 void reserveOperandSpace(unsigned NumValues) {
1836 resizeOperands(NumValues*2);
1839 virtual PHINode *clone(LLVMContext &Context) const;
1841 /// Provide fast operand accessors
1842 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1844 /// getNumIncomingValues - Return the number of incoming edges
1846 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1848 /// getIncomingValue - Return incoming value number x
1850 Value *getIncomingValue(unsigned i) const {
1851 assert(i*2 < getNumOperands() && "Invalid value number!");
1852 return getOperand(i*2);
1854 void setIncomingValue(unsigned i, Value *V) {
1855 assert(i*2 < getNumOperands() && "Invalid value number!");
1858 static unsigned getOperandNumForIncomingValue(unsigned i) {
1861 static unsigned getIncomingValueNumForOperand(unsigned i) {
1862 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1866 /// getIncomingBlock - Return incoming basic block corresponding
1867 /// to value use iterator
1869 template <typename U>
1870 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1871 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1872 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1874 /// getIncomingBlock - Return incoming basic block number x
1876 BasicBlock *getIncomingBlock(unsigned i) const {
1877 return static_cast<BasicBlock*>(getOperand(i*2+1));
1879 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1880 setOperand(i*2+1, BB);
1882 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1885 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1886 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1890 /// addIncoming - Add an incoming value to the end of the PHI list
1892 void addIncoming(Value *V, BasicBlock *BB) {
1893 assert(V && "PHI node got a null value!");
1894 assert(BB && "PHI node got a null basic block!");
1895 assert(getType() == V->getType() &&
1896 "All operands to PHI node must be the same type as the PHI node!");
1897 unsigned OpNo = NumOperands;
1898 if (OpNo+2 > ReservedSpace)
1899 resizeOperands(0); // Get more space!
1900 // Initialize some new operands.
1901 NumOperands = OpNo+2;
1902 OperandList[OpNo] = V;
1903 OperandList[OpNo+1] = BB;
1906 /// removeIncomingValue - Remove an incoming value. This is useful if a
1907 /// predecessor basic block is deleted. The value removed is returned.
1909 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1910 /// is true), the PHI node is destroyed and any uses of it are replaced with
1911 /// dummy values. The only time there should be zero incoming values to a PHI
1912 /// node is when the block is dead, so this strategy is sound.
1914 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1916 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1917 int Idx = getBasicBlockIndex(BB);
1918 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1919 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1922 /// getBasicBlockIndex - Return the first index of the specified basic
1923 /// block in the value list for this PHI. Returns -1 if no instance.
1925 int getBasicBlockIndex(const BasicBlock *BB) const {
1926 Use *OL = OperandList;
1927 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1928 if (OL[i+1].get() == BB) return i/2;
1932 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1933 return getIncomingValue(getBasicBlockIndex(BB));
1936 /// hasConstantValue - If the specified PHI node always merges together the
1937 /// same value, return the value, otherwise return null.
1939 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1941 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1942 static inline bool classof(const PHINode *) { return true; }
1943 static inline bool classof(const Instruction *I) {
1944 return I->getOpcode() == Instruction::PHI;
1946 static inline bool classof(const Value *V) {
1947 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1950 void resizeOperands(unsigned NumOperands);
1954 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1957 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1960 //===----------------------------------------------------------------------===//
1962 //===----------------------------------------------------------------------===//
1964 //===---------------------------------------------------------------------------
1965 /// ReturnInst - Return a value (possibly void), from a function. Execution
1966 /// does not continue in this function any longer.
1968 class ReturnInst : public TerminatorInst {
1969 ReturnInst(const ReturnInst &RI);
1972 // ReturnInst constructors:
1973 // ReturnInst() - 'ret void' instruction
1974 // ReturnInst( null) - 'ret void' instruction
1975 // ReturnInst(Value* X) - 'ret X' instruction
1976 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1977 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1978 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1979 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1981 // NOTE: If the Value* passed is of type void then the constructor behaves as
1982 // if it was passed NULL.
1983 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1984 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1985 explicit ReturnInst(BasicBlock *InsertAtEnd);
1987 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1988 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1990 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1991 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1993 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1994 return new(0) ReturnInst(InsertAtEnd);
1996 virtual ~ReturnInst();
1998 virtual ReturnInst *clone(LLVMContext &Context) const;
2000 /// Provide fast operand accessors
2001 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2003 /// Convenience accessor
2004 Value *getReturnValue(unsigned n = 0) const {
2005 return n < getNumOperands()
2010 unsigned getNumSuccessors() const { return 0; }
2012 // Methods for support type inquiry through isa, cast, and dyn_cast:
2013 static inline bool classof(const ReturnInst *) { return true; }
2014 static inline bool classof(const Instruction *I) {
2015 return (I->getOpcode() == Instruction::Ret);
2017 static inline bool classof(const Value *V) {
2018 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2021 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2022 virtual unsigned getNumSuccessorsV() const;
2023 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2027 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2030 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2032 //===----------------------------------------------------------------------===//
2034 //===----------------------------------------------------------------------===//
2036 //===---------------------------------------------------------------------------
2037 /// BranchInst - Conditional or Unconditional Branch instruction.
2039 class BranchInst : public TerminatorInst {
2040 /// Ops list - Branches are strange. The operands are ordered:
2041 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2042 /// they don't have to check for cond/uncond branchness. These are mostly
2043 /// accessed relative from op_end().
2044 BranchInst(const BranchInst &BI);
2046 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2047 // BranchInst(BB *B) - 'br B'
2048 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2049 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2050 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2051 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2052 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2053 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2054 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2055 Instruction *InsertBefore = 0);
2056 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2057 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2058 BasicBlock *InsertAtEnd);
2060 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2061 return new(1, true) BranchInst(IfTrue, InsertBefore);
2063 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2064 Value *Cond, Instruction *InsertBefore = 0) {
2065 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2067 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2068 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2070 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2071 Value *Cond, BasicBlock *InsertAtEnd) {
2072 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2077 /// Transparently provide more efficient getOperand methods.
2078 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2080 virtual BranchInst *clone(LLVMContext &Context) const;
2082 bool isUnconditional() const { return getNumOperands() == 1; }
2083 bool isConditional() const { return getNumOperands() == 3; }
2085 Value *getCondition() const {
2086 assert(isConditional() && "Cannot get condition of an uncond branch!");
2090 void setCondition(Value *V) {
2091 assert(isConditional() && "Cannot set condition of unconditional branch!");
2095 // setUnconditionalDest - Change the current branch to an unconditional branch
2096 // targeting the specified block.
2097 // FIXME: Eliminate this ugly method.
2098 void setUnconditionalDest(BasicBlock *Dest) {
2100 if (isConditional()) { // Convert this to an uncond branch.
2104 OperandList = op_begin();
2108 unsigned getNumSuccessors() const { return 1+isConditional(); }
2110 BasicBlock *getSuccessor(unsigned i) const {
2111 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2112 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2115 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2116 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2117 *(&Op<-1>() - idx) = NewSucc;
2120 // Methods for support type inquiry through isa, cast, and dyn_cast:
2121 static inline bool classof(const BranchInst *) { return true; }
2122 static inline bool classof(const Instruction *I) {
2123 return (I->getOpcode() == Instruction::Br);
2125 static inline bool classof(const Value *V) {
2126 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2129 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2130 virtual unsigned getNumSuccessorsV() const;
2131 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2135 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2137 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2139 //===----------------------------------------------------------------------===//
2141 //===----------------------------------------------------------------------===//
2143 //===---------------------------------------------------------------------------
2144 /// SwitchInst - Multiway switch
2146 class SwitchInst : public TerminatorInst {
2147 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2148 unsigned ReservedSpace;
2149 // Operand[0] = Value to switch on
2150 // Operand[1] = Default basic block destination
2151 // Operand[2n ] = Value to match
2152 // Operand[2n+1] = BasicBlock to go to on match
2153 SwitchInst(const SwitchInst &RI);
2154 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2155 void resizeOperands(unsigned No);
2156 // allocate space for exactly zero operands
2157 void *operator new(size_t s) {
2158 return User::operator new(s, 0);
2160 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2161 /// switch on and a default destination. The number of additional cases can
2162 /// be specified here to make memory allocation more efficient. This
2163 /// constructor can also autoinsert before another instruction.
2164 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2165 Instruction *InsertBefore = 0);
2167 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2168 /// switch on and a default destination. The number of additional cases can
2169 /// be specified here to make memory allocation more efficient. This
2170 /// constructor also autoinserts at the end of the specified BasicBlock.
2171 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2172 BasicBlock *InsertAtEnd);
2174 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2175 unsigned NumCases, Instruction *InsertBefore = 0) {
2176 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2178 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2179 unsigned NumCases, BasicBlock *InsertAtEnd) {
2180 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2184 /// Provide fast operand accessors
2185 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2187 // Accessor Methods for Switch stmt
2188 Value *getCondition() const { return getOperand(0); }
2189 void setCondition(Value *V) { setOperand(0, V); }
2191 BasicBlock *getDefaultDest() const {
2192 return cast<BasicBlock>(getOperand(1));
2195 /// getNumCases - return the number of 'cases' in this switch instruction.
2196 /// Note that case #0 is always the default case.
2197 unsigned getNumCases() const {
2198 return getNumOperands()/2;
2201 /// getCaseValue - Return the specified case value. Note that case #0, the
2202 /// default destination, does not have a case value.
2203 ConstantInt *getCaseValue(unsigned i) {
2204 assert(i && i < getNumCases() && "Illegal case value to get!");
2205 return getSuccessorValue(i);
2208 /// getCaseValue - Return the specified case value. Note that case #0, the
2209 /// default destination, does not have a case value.
2210 const ConstantInt *getCaseValue(unsigned i) const {
2211 assert(i && i < getNumCases() && "Illegal case value to get!");
2212 return getSuccessorValue(i);
2215 /// findCaseValue - Search all of the case values for the specified constant.
2216 /// If it is explicitly handled, return the case number of it, otherwise
2217 /// return 0 to indicate that it is handled by the default handler.
2218 unsigned findCaseValue(const ConstantInt *C) const {
2219 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2220 if (getCaseValue(i) == C)
2225 /// findCaseDest - Finds the unique case value for a given successor. Returns
2226 /// null if the successor is not found, not unique, or is the default case.
2227 ConstantInt *findCaseDest(BasicBlock *BB) {
2228 if (BB == getDefaultDest()) return NULL;
2230 ConstantInt *CI = NULL;
2231 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2232 if (getSuccessor(i) == BB) {
2233 if (CI) return NULL; // Multiple cases lead to BB.
2234 else CI = getCaseValue(i);
2240 /// addCase - Add an entry to the switch instruction...
2242 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2244 /// removeCase - This method removes the specified successor from the switch
2245 /// instruction. Note that this cannot be used to remove the default
2246 /// destination (successor #0).
2248 void removeCase(unsigned idx);
2250 virtual SwitchInst *clone(LLVMContext &Context) const;
2252 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2253 BasicBlock *getSuccessor(unsigned idx) const {
2254 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2255 return cast<BasicBlock>(getOperand(idx*2+1));
2257 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2258 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2259 setOperand(idx*2+1, NewSucc);
2262 // getSuccessorValue - Return the value associated with the specified
2264 ConstantInt *getSuccessorValue(unsigned idx) const {
2265 assert(idx < getNumSuccessors() && "Successor # out of range!");
2266 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2269 // Methods for support type inquiry through isa, cast, and dyn_cast:
2270 static inline bool classof(const SwitchInst *) { return true; }
2271 static inline bool classof(const Instruction *I) {
2272 return I->getOpcode() == Instruction::Switch;
2274 static inline bool classof(const Value *V) {
2275 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2278 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2279 virtual unsigned getNumSuccessorsV() const;
2280 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2284 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2287 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2290 //===----------------------------------------------------------------------===//
2292 //===----------------------------------------------------------------------===//
2294 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2295 /// calling convention of the call.
2297 class InvokeInst : public TerminatorInst {
2298 AttrListPtr AttributeList;
2299 InvokeInst(const InvokeInst &BI);
2300 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2301 Value* const *Args, unsigned NumArgs);
2303 template<typename InputIterator>
2304 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2305 InputIterator ArgBegin, InputIterator ArgEnd,
2306 const Twine &NameStr,
2307 // This argument ensures that we have an iterator we can
2308 // do arithmetic on in constant time
2309 std::random_access_iterator_tag) {
2310 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2312 // This requires that the iterator points to contiguous memory.
2313 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2317 /// Construct an InvokeInst given a range of arguments.
2318 /// InputIterator must be a random-access iterator pointing to
2319 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2320 /// made for random-accessness but not for contiguous storage as
2321 /// that would incur runtime overhead.
2323 /// @brief Construct an InvokeInst from a range of arguments
2324 template<typename InputIterator>
2325 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2326 InputIterator ArgBegin, InputIterator ArgEnd,
2328 const Twine &NameStr, Instruction *InsertBefore);
2330 /// Construct an InvokeInst given a range of arguments.
2331 /// InputIterator must be a random-access iterator pointing to
2332 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2333 /// made for random-accessness but not for contiguous storage as
2334 /// that would incur runtime overhead.
2336 /// @brief Construct an InvokeInst from a range of arguments
2337 template<typename InputIterator>
2338 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2339 InputIterator ArgBegin, InputIterator ArgEnd,
2341 const Twine &NameStr, BasicBlock *InsertAtEnd);
2343 template<typename InputIterator>
2344 static InvokeInst *Create(Value *Func,
2345 BasicBlock *IfNormal, BasicBlock *IfException,
2346 InputIterator ArgBegin, InputIterator ArgEnd,
2347 const Twine &NameStr = "",
2348 Instruction *InsertBefore = 0) {
2349 unsigned Values(ArgEnd - ArgBegin + 3);
2350 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2351 Values, NameStr, InsertBefore);
2353 template<typename InputIterator>
2354 static InvokeInst *Create(Value *Func,
2355 BasicBlock *IfNormal, BasicBlock *IfException,
2356 InputIterator ArgBegin, InputIterator ArgEnd,
2357 const Twine &NameStr,
2358 BasicBlock *InsertAtEnd) {
2359 unsigned Values(ArgEnd - ArgBegin + 3);
2360 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2361 Values, NameStr, InsertAtEnd);
2364 virtual InvokeInst *clone(LLVMContext &Context) const;
2366 /// Provide fast operand accessors
2367 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2369 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2371 unsigned getCallingConv() const { return SubclassData; }
2372 void setCallingConv(unsigned CC) {
2376 /// getAttributes - Return the parameter attributes for this invoke.
2378 const AttrListPtr &getAttributes() const { return AttributeList; }
2380 /// setAttributes - Set the parameter attributes for this invoke.
2382 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2384 /// addAttribute - adds the attribute to the list of attributes.
2385 void addAttribute(unsigned i, Attributes attr);
2387 /// removeAttribute - removes the attribute from the list of attributes.
2388 void removeAttribute(unsigned i, Attributes attr);
2390 /// @brief Determine whether the call or the callee has the given attribute.
2391 bool paramHasAttr(unsigned i, Attributes attr) const;
2393 /// @brief Extract the alignment for a call or parameter (0=unknown).
2394 unsigned getParamAlignment(unsigned i) const {
2395 return AttributeList.getParamAlignment(i);
2398 /// @brief Determine if the call does not access memory.
2399 bool doesNotAccessMemory() const {
2400 return paramHasAttr(~0, Attribute::ReadNone);
2402 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2403 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2404 else removeAttribute(~0, Attribute::ReadNone);
2407 /// @brief Determine if the call does not access or only reads memory.
2408 bool onlyReadsMemory() const {
2409 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2411 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2412 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2413 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2416 /// @brief Determine if the call cannot return.
2417 bool doesNotReturn() const {
2418 return paramHasAttr(~0, Attribute::NoReturn);
2420 void setDoesNotReturn(bool DoesNotReturn = true) {
2421 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2422 else removeAttribute(~0, Attribute::NoReturn);
2425 /// @brief Determine if the call cannot unwind.
2426 bool doesNotThrow() const {
2427 return paramHasAttr(~0, Attribute::NoUnwind);
2429 void setDoesNotThrow(bool DoesNotThrow = true) {
2430 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2431 else removeAttribute(~0, Attribute::NoUnwind);
2434 /// @brief Determine if the call returns a structure through first
2435 /// pointer argument.
2436 bool hasStructRetAttr() const {
2437 // Be friendly and also check the callee.
2438 return paramHasAttr(1, Attribute::StructRet);
2441 /// @brief Determine if any call argument is an aggregate passed by value.
2442 bool hasByValArgument() const {
2443 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2446 /// getCalledFunction - Return the function called, or null if this is an
2447 /// indirect function invocation.
2449 Function *getCalledFunction() const {
2450 return dyn_cast<Function>(getOperand(0));
2453 /// getCalledValue - Get a pointer to the function that is invoked by this
2455 const Value *getCalledValue() const { return getOperand(0); }
2456 Value *getCalledValue() { return getOperand(0); }
2458 // get*Dest - Return the destination basic blocks...
2459 BasicBlock *getNormalDest() const {
2460 return cast<BasicBlock>(getOperand(1));
2462 BasicBlock *getUnwindDest() const {
2463 return cast<BasicBlock>(getOperand(2));
2465 void setNormalDest(BasicBlock *B) {
2469 void setUnwindDest(BasicBlock *B) {
2473 BasicBlock *getSuccessor(unsigned i) const {
2474 assert(i < 2 && "Successor # out of range for invoke!");
2475 return i == 0 ? getNormalDest() : getUnwindDest();
2478 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2479 assert(idx < 2 && "Successor # out of range for invoke!");
2480 setOperand(idx+1, NewSucc);
2483 unsigned getNumSuccessors() const { return 2; }
2485 // Methods for support type inquiry through isa, cast, and dyn_cast:
2486 static inline bool classof(const InvokeInst *) { return true; }
2487 static inline bool classof(const Instruction *I) {
2488 return (I->getOpcode() == Instruction::Invoke);
2490 static inline bool classof(const Value *V) {
2491 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2494 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2495 virtual unsigned getNumSuccessorsV() const;
2496 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2500 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2503 template<typename InputIterator>
2504 InvokeInst::InvokeInst(Value *Func,
2505 BasicBlock *IfNormal, BasicBlock *IfException,
2506 InputIterator ArgBegin, InputIterator ArgEnd,
2508 const Twine &NameStr, Instruction *InsertBefore)
2509 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2510 ->getElementType())->getReturnType(),
2511 Instruction::Invoke,
2512 OperandTraits<InvokeInst>::op_end(this) - Values,
2513 Values, InsertBefore) {
2514 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2515 typename std::iterator_traits<InputIterator>::iterator_category());
2517 template<typename InputIterator>
2518 InvokeInst::InvokeInst(Value *Func,
2519 BasicBlock *IfNormal, BasicBlock *IfException,
2520 InputIterator ArgBegin, InputIterator ArgEnd,
2522 const Twine &NameStr, BasicBlock *InsertAtEnd)
2523 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2524 ->getElementType())->getReturnType(),
2525 Instruction::Invoke,
2526 OperandTraits<InvokeInst>::op_end(this) - Values,
2527 Values, InsertAtEnd) {
2528 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2529 typename std::iterator_traits<InputIterator>::iterator_category());
2532 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2534 //===----------------------------------------------------------------------===//
2536 //===----------------------------------------------------------------------===//
2538 //===---------------------------------------------------------------------------
2539 /// UnwindInst - Immediately exit the current function, unwinding the stack
2540 /// until an invoke instruction is found.
2542 class UnwindInst : public TerminatorInst {
2543 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2545 // allocate space for exactly zero operands
2546 void *operator new(size_t s) {
2547 return User::operator new(s, 0);
2549 explicit UnwindInst(Instruction *InsertBefore = 0);
2550 explicit UnwindInst(BasicBlock *InsertAtEnd);
2552 virtual UnwindInst *clone(LLVMContext &Context) const;
2554 unsigned getNumSuccessors() const { return 0; }
2556 // Methods for support type inquiry through isa, cast, and dyn_cast:
2557 static inline bool classof(const UnwindInst *) { return true; }
2558 static inline bool classof(const Instruction *I) {
2559 return I->getOpcode() == Instruction::Unwind;
2561 static inline bool classof(const Value *V) {
2562 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2565 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2566 virtual unsigned getNumSuccessorsV() const;
2567 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2570 //===----------------------------------------------------------------------===//
2571 // UnreachableInst Class
2572 //===----------------------------------------------------------------------===//
2574 //===---------------------------------------------------------------------------
2575 /// UnreachableInst - This function has undefined behavior. In particular, the
2576 /// presence of this instruction indicates some higher level knowledge that the
2577 /// end of the block cannot be reached.
2579 class UnreachableInst : public TerminatorInst {
2580 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2582 // allocate space for exactly zero operands
2583 void *operator new(size_t s) {
2584 return User::operator new(s, 0);
2586 explicit UnreachableInst(Instruction *InsertBefore = 0);
2587 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2589 virtual UnreachableInst *clone(LLVMContext &Context) const;
2591 unsigned getNumSuccessors() const { return 0; }
2593 // Methods for support type inquiry through isa, cast, and dyn_cast:
2594 static inline bool classof(const UnreachableInst *) { return true; }
2595 static inline bool classof(const Instruction *I) {
2596 return I->getOpcode() == Instruction::Unreachable;
2598 static inline bool classof(const Value *V) {
2599 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2602 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2603 virtual unsigned getNumSuccessorsV() const;
2604 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2607 //===----------------------------------------------------------------------===//
2609 //===----------------------------------------------------------------------===//
2611 /// @brief This class represents a truncation of integer types.
2612 class TruncInst : public CastInst {
2613 /// Private copy constructor
2614 TruncInst(const TruncInst &CI)
2615 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2618 /// @brief Constructor with insert-before-instruction semantics
2620 Value *S, ///< The value to be truncated
2621 const Type *Ty, ///< The (smaller) type to truncate to
2622 const Twine &NameStr = "", ///< A name for the new instruction
2623 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2626 /// @brief Constructor with insert-at-end-of-block semantics
2628 Value *S, ///< The value to be truncated
2629 const Type *Ty, ///< The (smaller) type to truncate to
2630 const Twine &NameStr, ///< A name for the new instruction
2631 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2634 /// @brief Clone an identical TruncInst
2635 virtual CastInst *clone(LLVMContext &Context) const;
2637 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2638 static inline bool classof(const TruncInst *) { return true; }
2639 static inline bool classof(const Instruction *I) {
2640 return I->getOpcode() == Trunc;
2642 static inline bool classof(const Value *V) {
2643 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2647 //===----------------------------------------------------------------------===//
2649 //===----------------------------------------------------------------------===//
2651 /// @brief This class represents zero extension of integer types.
2652 class ZExtInst : public CastInst {
2653 /// @brief Private copy constructor
2654 ZExtInst(const ZExtInst &CI)
2655 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2658 /// @brief Constructor with insert-before-instruction semantics
2660 Value *S, ///< The value to be zero extended
2661 const Type *Ty, ///< The type to zero extend to
2662 const Twine &NameStr = "", ///< A name for the new instruction
2663 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2666 /// @brief Constructor with insert-at-end semantics.
2668 Value *S, ///< The value to be zero extended
2669 const Type *Ty, ///< The type to zero extend to
2670 const Twine &NameStr, ///< A name for the new instruction
2671 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2674 /// @brief Clone an identical ZExtInst
2675 virtual CastInst *clone(LLVMContext &Context) const;
2677 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2678 static inline bool classof(const ZExtInst *) { return true; }
2679 static inline bool classof(const Instruction *I) {
2680 return I->getOpcode() == ZExt;
2682 static inline bool classof(const Value *V) {
2683 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2687 //===----------------------------------------------------------------------===//
2689 //===----------------------------------------------------------------------===//
2691 /// @brief This class represents a sign extension of integer types.
2692 class SExtInst : public CastInst {
2693 /// @brief Private copy constructor
2694 SExtInst(const SExtInst &CI)
2695 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2698 /// @brief Constructor with insert-before-instruction semantics
2700 Value *S, ///< The value to be sign extended
2701 const Type *Ty, ///< The type to sign extend to
2702 const Twine &NameStr = "", ///< A name for the new instruction
2703 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2706 /// @brief Constructor with insert-at-end-of-block semantics
2708 Value *S, ///< The value to be sign extended
2709 const Type *Ty, ///< The type to sign extend to
2710 const Twine &NameStr, ///< A name for the new instruction
2711 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2714 /// @brief Clone an identical SExtInst
2715 virtual CastInst *clone(LLVMContext &Context) const;
2717 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2718 static inline bool classof(const SExtInst *) { return true; }
2719 static inline bool classof(const Instruction *I) {
2720 return I->getOpcode() == SExt;
2722 static inline bool classof(const Value *V) {
2723 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2727 //===----------------------------------------------------------------------===//
2728 // FPTruncInst Class
2729 //===----------------------------------------------------------------------===//
2731 /// @brief This class represents a truncation of floating point types.
2732 class FPTruncInst : public CastInst {
2733 FPTruncInst(const FPTruncInst &CI)
2734 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2737 /// @brief Constructor with insert-before-instruction semantics
2739 Value *S, ///< The value to be truncated
2740 const Type *Ty, ///< The type to truncate to
2741 const Twine &NameStr = "", ///< A name for the new instruction
2742 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2745 /// @brief Constructor with insert-before-instruction semantics
2747 Value *S, ///< The value to be truncated
2748 const Type *Ty, ///< The type to truncate to
2749 const Twine &NameStr, ///< A name for the new instruction
2750 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2753 /// @brief Clone an identical FPTruncInst
2754 virtual CastInst *clone(LLVMContext &Context) const;
2756 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2757 static inline bool classof(const FPTruncInst *) { return true; }
2758 static inline bool classof(const Instruction *I) {
2759 return I->getOpcode() == FPTrunc;
2761 static inline bool classof(const Value *V) {
2762 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2766 //===----------------------------------------------------------------------===//
2768 //===----------------------------------------------------------------------===//
2770 /// @brief This class represents an extension of floating point types.
2771 class FPExtInst : public CastInst {
2772 FPExtInst(const FPExtInst &CI)
2773 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2776 /// @brief Constructor with insert-before-instruction semantics
2778 Value *S, ///< The value to be extended
2779 const Type *Ty, ///< The type to extend to
2780 const Twine &NameStr = "", ///< A name for the new instruction
2781 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2784 /// @brief Constructor with insert-at-end-of-block semantics
2786 Value *S, ///< The value to be extended
2787 const Type *Ty, ///< The type to extend to
2788 const Twine &NameStr, ///< A name for the new instruction
2789 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2792 /// @brief Clone an identical FPExtInst
2793 virtual CastInst *clone(LLVMContext &Context) const;
2795 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2796 static inline bool classof(const FPExtInst *) { return true; }
2797 static inline bool classof(const Instruction *I) {
2798 return I->getOpcode() == FPExt;
2800 static inline bool classof(const Value *V) {
2801 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2805 //===----------------------------------------------------------------------===//
2807 //===----------------------------------------------------------------------===//
2809 /// @brief This class represents a cast unsigned integer to floating point.
2810 class UIToFPInst : public CastInst {
2811 UIToFPInst(const UIToFPInst &CI)
2812 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2815 /// @brief Constructor with insert-before-instruction semantics
2817 Value *S, ///< The value to be converted
2818 const Type *Ty, ///< The type to convert to
2819 const Twine &NameStr = "", ///< A name for the new instruction
2820 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2823 /// @brief Constructor with insert-at-end-of-block semantics
2825 Value *S, ///< The value to be converted
2826 const Type *Ty, ///< The type to convert to
2827 const Twine &NameStr, ///< A name for the new instruction
2828 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2831 /// @brief Clone an identical UIToFPInst
2832 virtual CastInst *clone(LLVMContext &Context) const;
2834 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2835 static inline bool classof(const UIToFPInst *) { return true; }
2836 static inline bool classof(const Instruction *I) {
2837 return I->getOpcode() == UIToFP;
2839 static inline bool classof(const Value *V) {
2840 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2844 //===----------------------------------------------------------------------===//
2846 //===----------------------------------------------------------------------===//
2848 /// @brief This class represents a cast from signed integer to floating point.
2849 class SIToFPInst : public CastInst {
2850 SIToFPInst(const SIToFPInst &CI)
2851 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2854 /// @brief Constructor with insert-before-instruction semantics
2856 Value *S, ///< The value to be converted
2857 const Type *Ty, ///< The type to convert to
2858 const Twine &NameStr = "", ///< A name for the new instruction
2859 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2862 /// @brief Constructor with insert-at-end-of-block semantics
2864 Value *S, ///< The value to be converted
2865 const Type *Ty, ///< The type to convert to
2866 const Twine &NameStr, ///< A name for the new instruction
2867 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2870 /// @brief Clone an identical SIToFPInst
2871 virtual CastInst *clone(LLVMContext &Context) const;
2873 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2874 static inline bool classof(const SIToFPInst *) { return true; }
2875 static inline bool classof(const Instruction *I) {
2876 return I->getOpcode() == SIToFP;
2878 static inline bool classof(const Value *V) {
2879 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2883 //===----------------------------------------------------------------------===//
2885 //===----------------------------------------------------------------------===//
2887 /// @brief This class represents a cast from floating point to unsigned integer
2888 class FPToUIInst : public CastInst {
2889 FPToUIInst(const FPToUIInst &CI)
2890 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2893 /// @brief Constructor with insert-before-instruction semantics
2895 Value *S, ///< The value to be converted
2896 const Type *Ty, ///< The type to convert to
2897 const Twine &NameStr = "", ///< A name for the new instruction
2898 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2901 /// @brief Constructor with insert-at-end-of-block semantics
2903 Value *S, ///< The value to be converted
2904 const Type *Ty, ///< The type to convert to
2905 const Twine &NameStr, ///< A name for the new instruction
2906 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2909 /// @brief Clone an identical FPToUIInst
2910 virtual CastInst *clone(LLVMContext &Context) const;
2912 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2913 static inline bool classof(const FPToUIInst *) { return true; }
2914 static inline bool classof(const Instruction *I) {
2915 return I->getOpcode() == FPToUI;
2917 static inline bool classof(const Value *V) {
2918 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2922 //===----------------------------------------------------------------------===//
2924 //===----------------------------------------------------------------------===//
2926 /// @brief This class represents a cast from floating point to signed integer.
2927 class FPToSIInst : public CastInst {
2928 FPToSIInst(const FPToSIInst &CI)
2929 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2932 /// @brief Constructor with insert-before-instruction semantics
2934 Value *S, ///< The value to be converted
2935 const Type *Ty, ///< The type to convert to
2936 const Twine &NameStr = "", ///< A name for the new instruction
2937 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2940 /// @brief Constructor with insert-at-end-of-block semantics
2942 Value *S, ///< The value to be converted
2943 const Type *Ty, ///< The type to convert to
2944 const Twine &NameStr, ///< A name for the new instruction
2945 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2948 /// @brief Clone an identical FPToSIInst
2949 virtual CastInst *clone(LLVMContext &Context) const;
2951 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2952 static inline bool classof(const FPToSIInst *) { return true; }
2953 static inline bool classof(const Instruction *I) {
2954 return I->getOpcode() == FPToSI;
2956 static inline bool classof(const Value *V) {
2957 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2961 //===----------------------------------------------------------------------===//
2962 // IntToPtrInst Class
2963 //===----------------------------------------------------------------------===//
2965 /// @brief This class represents a cast from an integer to a pointer.
2966 class IntToPtrInst : public CastInst {
2967 IntToPtrInst(const IntToPtrInst &CI)
2968 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2971 /// @brief Constructor with insert-before-instruction semantics
2973 Value *S, ///< The value to be converted
2974 const Type *Ty, ///< The type to convert to
2975 const Twine &NameStr = "", ///< A name for the new instruction
2976 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2979 /// @brief Constructor with insert-at-end-of-block semantics
2981 Value *S, ///< The value to be converted
2982 const Type *Ty, ///< The type to convert to
2983 const Twine &NameStr, ///< A name for the new instruction
2984 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2987 /// @brief Clone an identical IntToPtrInst
2988 virtual CastInst *clone(LLVMContext &Context) const;
2990 // Methods for support type inquiry through isa, cast, and dyn_cast:
2991 static inline bool classof(const IntToPtrInst *) { return true; }
2992 static inline bool classof(const Instruction *I) {
2993 return I->getOpcode() == IntToPtr;
2995 static inline bool classof(const Value *V) {
2996 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3000 //===----------------------------------------------------------------------===//
3001 // PtrToIntInst Class
3002 //===----------------------------------------------------------------------===//
3004 /// @brief This class represents a cast from a pointer to an integer
3005 class PtrToIntInst : public CastInst {
3006 PtrToIntInst(const PtrToIntInst &CI)
3007 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3010 /// @brief Constructor with insert-before-instruction semantics
3012 Value *S, ///< The value to be converted
3013 const Type *Ty, ///< The type to convert to
3014 const Twine &NameStr = "", ///< A name for the new instruction
3015 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3018 /// @brief Constructor with insert-at-end-of-block semantics
3020 Value *S, ///< The value to be converted
3021 const Type *Ty, ///< The type to convert to
3022 const Twine &NameStr, ///< A name for the new instruction
3023 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3026 /// @brief Clone an identical PtrToIntInst
3027 virtual CastInst *clone(LLVMContext &Context) const;
3029 // Methods for support type inquiry through isa, cast, and dyn_cast:
3030 static inline bool classof(const PtrToIntInst *) { return true; }
3031 static inline bool classof(const Instruction *I) {
3032 return I->getOpcode() == PtrToInt;
3034 static inline bool classof(const Value *V) {
3035 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3039 //===----------------------------------------------------------------------===//
3040 // BitCastInst Class
3041 //===----------------------------------------------------------------------===//
3043 /// @brief This class represents a no-op cast from one type to another.
3044 class BitCastInst : public CastInst {
3045 BitCastInst(const BitCastInst &CI)
3046 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3049 /// @brief Constructor with insert-before-instruction semantics
3051 Value *S, ///< The value to be casted
3052 const Type *Ty, ///< The type to casted to
3053 const Twine &NameStr = "", ///< A name for the new instruction
3054 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3057 /// @brief Constructor with insert-at-end-of-block semantics
3059 Value *S, ///< The value to be casted
3060 const Type *Ty, ///< The type to casted to
3061 const Twine &NameStr, ///< A name for the new instruction
3062 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3065 /// @brief Clone an identical BitCastInst
3066 virtual CastInst *clone(LLVMContext &Context) const;
3068 // Methods for support type inquiry through isa, cast, and dyn_cast:
3069 static inline bool classof(const BitCastInst *) { return true; }
3070 static inline bool classof(const Instruction *I) {
3071 return I->getOpcode() == BitCast;
3073 static inline bool classof(const Value *V) {
3074 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3078 } // End llvm namespace