1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/ADT/SmallVector.h"
34 //===----------------------------------------------------------------------===//
35 // AllocationInst Class
36 //===----------------------------------------------------------------------===//
38 /// AllocationInst - This class is the common base class of MallocInst and
41 class AllocationInst : public UnaryInstruction {
43 AllocationInst(const Type *Ty, Value *ArraySize,
44 unsigned iTy, unsigned Align, const std::string &Name = "",
45 Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize,
47 unsigned iTy, unsigned Align, const std::string &Name,
48 BasicBlock *InsertAtEnd);
50 // Out of line virtual method, so the vtable, etc. has a home.
51 virtual ~AllocationInst();
53 /// isArrayAllocation - Return true if there is an allocation size parameter
54 /// to the allocation instruction that is not 1.
56 bool isArrayAllocation() const;
58 /// getArraySize - Get the number of elements allocated. For a simple
59 /// allocation of a single element, this will return a constant 1 value.
61 const Value *getArraySize() const { return getOperand(0); }
62 Value *getArraySize() { return getOperand(0); }
64 /// getType - Overload to return most specific pointer type
66 const PointerType *getType() const {
67 return reinterpret_cast<const PointerType*>(Instruction::getType());
70 /// getAllocatedType - Return the type that is being allocated by the
73 const Type *getAllocatedType() const;
75 /// getAlignment - Return the alignment of the memory that is being allocated
76 /// by the instruction.
78 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
79 void setAlignment(unsigned Align);
81 virtual Instruction *clone(LLVMContext &Context) const = 0;
83 // Methods for support type inquiry through isa, cast, and dyn_cast:
84 static inline bool classof(const AllocationInst *) { return true; }
85 static inline bool classof(const Instruction *I) {
86 return I->getOpcode() == Instruction::Alloca ||
87 I->getOpcode() == Instruction::Malloc;
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) && classof(cast<Instruction>(V));
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 /// MallocInst - an instruction to allocated memory on the heap
101 class MallocInst : public AllocationInst {
102 MallocInst(const MallocInst &MI);
104 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
105 const std::string &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc,
108 0, NameStr, InsertBefore) {}
109 MallocInst(const Type *Ty, Value *ArraySize,
110 const std::string &NameStr, BasicBlock *InsertAtEnd)
111 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
113 MallocInst(const Type *Ty, const std::string &NameStr,
114 Instruction *InsertBefore = 0)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
116 MallocInst(const Type *Ty, const std::string &NameStr,
117 BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize,
121 unsigned Align, const std::string &NameStr,
122 BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc,
124 Align, NameStr, InsertAtEnd) {}
125 MallocInst(const Type *Ty, Value *ArraySize,
126 unsigned Align, const std::string &NameStr = "",
127 Instruction *InsertBefore = 0)
128 : AllocationInst(Ty, ArraySize,
129 Malloc, Align, NameStr, InsertBefore) {}
131 virtual MallocInst *clone(LLVMContext &Context) const;
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const MallocInst *) { return true; }
135 static inline bool classof(const Instruction *I) {
136 return (I->getOpcode() == Instruction::Malloc);
138 static inline bool classof(const Value *V) {
139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
144 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
148 /// AllocaInst - an instruction to allocate memory on the stack
150 class AllocaInst : public AllocationInst {
151 AllocaInst(const AllocaInst &);
153 explicit AllocaInst(const Type *Ty,
154 Value *ArraySize = 0,
155 const std::string &NameStr = "",
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, ArraySize, Alloca,
158 0, NameStr, InsertBefore) {}
159 AllocaInst(const Type *Ty,
160 Value *ArraySize, const std::string &NameStr,
161 BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, const std::string &NameStr,
165 Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167 AllocaInst(const Type *Ty, const std::string &NameStr,
168 BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
171 AllocaInst(const Type *Ty, Value *ArraySize,
172 unsigned Align, const std::string &NameStr = "",
173 Instruction *InsertBefore = 0)
174 : AllocationInst(Ty, ArraySize, Alloca,
175 Align, NameStr, InsertBefore) {}
176 AllocaInst(const Type *Ty, Value *ArraySize,
177 unsigned Align, const std::string &NameStr,
178 BasicBlock *InsertAtEnd)
179 : AllocationInst(Ty, ArraySize, Alloca,
180 Align, NameStr, InsertAtEnd) {}
182 virtual AllocaInst *clone(LLVMContext &Context) const;
184 /// isStaticAlloca - Return true if this alloca is in the entry block of the
185 /// function and is a constant size. If so, the code generator will fold it
186 /// into the prolog/epilog code, so it is basically free.
187 bool isStaticAlloca() const;
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const AllocaInst *) { return true; }
191 static inline bool classof(const Instruction *I) {
192 return (I->getOpcode() == Instruction::Alloca);
194 static inline bool classof(const Value *V) {
195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
200 //===----------------------------------------------------------------------===//
202 //===----------------------------------------------------------------------===//
204 /// FreeInst - an instruction to deallocate memory
206 class FreeInst : public UnaryInstruction {
209 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
210 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
212 virtual FreeInst *clone(LLVMContext &Context) const;
214 // Accessor methods for consistency with other memory operations
215 Value *getPointerOperand() { return getOperand(0); }
216 const Value *getPointerOperand() const { return getOperand(0); }
218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const FreeInst *) { return true; }
220 static inline bool classof(const Instruction *I) {
221 return (I->getOpcode() == Instruction::Free);
223 static inline bool classof(const Value *V) {
224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
229 //===----------------------------------------------------------------------===//
231 //===----------------------------------------------------------------------===//
233 /// LoadInst - an instruction for reading from memory. This uses the
234 /// SubclassData field in Value to store whether or not the load is volatile.
236 class LoadInst : public UnaryInstruction {
238 LoadInst(const LoadInst &LI)
239 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
240 setVolatile(LI.isVolatile());
241 setAlignment(LI.getAlignment());
249 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
250 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
251 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
252 Instruction *InsertBefore = 0);
253 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
254 unsigned Align, Instruction *InsertBefore = 0);
255 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
256 BasicBlock *InsertAtEnd);
257 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
258 unsigned Align, BasicBlock *InsertAtEnd);
260 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
261 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
262 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
263 bool isVolatile = false, Instruction *InsertBefore = 0);
264 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
265 BasicBlock *InsertAtEnd);
267 /// isVolatile - Return true if this is a load from a volatile memory
270 bool isVolatile() const { return SubclassData & 1; }
272 /// setVolatile - Specify whether this is a volatile load or not.
274 void setVolatile(bool V) {
275 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
278 virtual LoadInst *clone(LLVMContext &Context) const;
280 /// getAlignment - Return the alignment of the access that is being performed
282 unsigned getAlignment() const {
283 return (1 << (SubclassData>>1)) >> 1;
286 void setAlignment(unsigned Align);
288 Value *getPointerOperand() { return getOperand(0); }
289 const Value *getPointerOperand() const { return getOperand(0); }
290 static unsigned getPointerOperandIndex() { return 0U; }
292 // Methods for support type inquiry through isa, cast, and dyn_cast:
293 static inline bool classof(const LoadInst *) { return true; }
294 static inline bool classof(const Instruction *I) {
295 return I->getOpcode() == Instruction::Load;
297 static inline bool classof(const Value *V) {
298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
303 //===----------------------------------------------------------------------===//
305 //===----------------------------------------------------------------------===//
307 /// StoreInst - an instruction for storing to memory
309 class StoreInst : public Instruction {
310 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
312 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
314 Op<0>() = SI.Op<0>();
315 Op<1>() = SI.Op<1>();
316 setVolatile(SI.isVolatile());
317 setAlignment(SI.getAlignment());
325 // allocate space for exactly two operands
326 void *operator new(size_t s) {
327 return User::operator new(s, 2);
329 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
330 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
331 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
332 Instruction *InsertBefore = 0);
333 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
334 unsigned Align, Instruction *InsertBefore = 0);
335 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
336 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
337 unsigned Align, BasicBlock *InsertAtEnd);
340 /// isVolatile - Return true if this is a load from a volatile memory
343 bool isVolatile() const { return SubclassData & 1; }
345 /// setVolatile - Specify whether this is a volatile load or not.
347 void setVolatile(bool V) {
348 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
351 /// Transparently provide more efficient getOperand methods.
352 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
354 /// getAlignment - Return the alignment of the access that is being performed
356 unsigned getAlignment() const {
357 return (1 << (SubclassData>>1)) >> 1;
360 void setAlignment(unsigned Align);
362 virtual StoreInst *clone(LLVMContext &Context) const;
364 Value *getPointerOperand() { return getOperand(1); }
365 const Value *getPointerOperand() const { return getOperand(1); }
366 static unsigned getPointerOperandIndex() { return 1U; }
368 // Methods for support type inquiry through isa, cast, and dyn_cast:
369 static inline bool classof(const StoreInst *) { return true; }
370 static inline bool classof(const Instruction *I) {
371 return I->getOpcode() == Instruction::Store;
373 static inline bool classof(const Value *V) {
374 return isa<Instruction>(V) && classof(cast<Instruction>(V));
379 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
382 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
384 //===----------------------------------------------------------------------===//
385 // GetElementPtrInst Class
386 //===----------------------------------------------------------------------===//
388 // checkType - Simple wrapper function to give a better assertion failure
389 // message on bad indexes for a gep instruction.
391 static inline const Type *checkType(const Type *Ty) {
392 assert(Ty && "Invalid GetElementPtrInst indices for type!");
396 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
397 /// access elements of arrays and structs
399 class GetElementPtrInst : public Instruction {
400 GetElementPtrInst(const GetElementPtrInst &GEPI);
401 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
402 const std::string &NameStr);
403 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
405 template<typename InputIterator>
406 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
407 const std::string &NameStr,
408 // This argument ensures that we have an iterator we can
409 // do arithmetic on in constant time
410 std::random_access_iterator_tag) {
411 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
414 // This requires that the iterator points to contiguous memory.
415 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
416 // we have to build an array here
419 init(Ptr, 0, NumIdx, NameStr);
423 /// getIndexedType - Returns the type of the element that would be loaded with
424 /// a load instruction with the specified parameters.
426 /// Null is returned if the indices are invalid for the specified
429 template<typename InputIterator>
430 static const Type *getIndexedType(const Type *Ptr,
431 InputIterator IdxBegin,
432 InputIterator IdxEnd,
433 // This argument ensures that we
434 // have an iterator we can do
435 // arithmetic on in constant time
436 std::random_access_iterator_tag) {
437 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
440 // This requires that the iterator points to contiguous memory.
441 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
443 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
446 /// Constructors - Create a getelementptr instruction with a base pointer an
447 /// list of indices. The first ctor can optionally insert before an existing
448 /// instruction, the second appends the new instruction to the specified
450 template<typename InputIterator>
451 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
452 InputIterator IdxEnd,
454 const std::string &NameStr,
455 Instruction *InsertBefore);
456 template<typename InputIterator>
457 inline GetElementPtrInst(Value *Ptr,
458 InputIterator IdxBegin, InputIterator IdxEnd,
460 const std::string &NameStr, BasicBlock *InsertAtEnd);
462 /// Constructors - These two constructors are convenience methods because one
463 /// and two index getelementptr instructions are so common.
464 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
465 Instruction *InsertBefore = 0);
466 GetElementPtrInst(Value *Ptr, Value *Idx,
467 const std::string &NameStr, BasicBlock *InsertAtEnd);
469 template<typename InputIterator>
470 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
471 InputIterator IdxEnd,
472 const std::string &NameStr = "",
473 Instruction *InsertBefore = 0) {
474 typename std::iterator_traits<InputIterator>::difference_type Values =
475 1 + std::distance(IdxBegin, IdxEnd);
477 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
479 template<typename InputIterator>
480 static GetElementPtrInst *Create(Value *Ptr,
481 InputIterator IdxBegin, InputIterator IdxEnd,
482 const std::string &NameStr,
483 BasicBlock *InsertAtEnd) {
484 typename std::iterator_traits<InputIterator>::difference_type Values =
485 1 + std::distance(IdxBegin, IdxEnd);
487 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
490 /// Constructors - These two creators are convenience methods because one
491 /// index getelementptr instructions are so common.
492 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
493 const std::string &NameStr = "",
494 Instruction *InsertBefore = 0) {
495 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
497 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
498 const std::string &NameStr,
499 BasicBlock *InsertAtEnd) {
500 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
503 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
505 /// Transparently provide more efficient getOperand methods.
506 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
508 // getType - Overload to return most specific pointer type...
509 const PointerType *getType() const {
510 return reinterpret_cast<const PointerType*>(Instruction::getType());
513 /// getIndexedType - Returns the type of the element that would be loaded with
514 /// a load instruction with the specified parameters.
516 /// Null is returned if the indices are invalid for the specified
519 template<typename InputIterator>
520 static const Type *getIndexedType(const Type *Ptr,
521 InputIterator IdxBegin,
522 InputIterator IdxEnd) {
523 return getIndexedType(Ptr, IdxBegin, IdxEnd,
524 typename std::iterator_traits<InputIterator>::
525 iterator_category());
528 static const Type *getIndexedType(const Type *Ptr,
529 Value* const *Idx, unsigned NumIdx);
531 static const Type *getIndexedType(const Type *Ptr,
532 uint64_t const *Idx, unsigned NumIdx);
534 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
536 inline op_iterator idx_begin() { return op_begin()+1; }
537 inline const_op_iterator idx_begin() const { return op_begin()+1; }
538 inline op_iterator idx_end() { return op_end(); }
539 inline const_op_iterator idx_end() const { return op_end(); }
541 Value *getPointerOperand() {
542 return getOperand(0);
544 const Value *getPointerOperand() const {
545 return getOperand(0);
547 static unsigned getPointerOperandIndex() {
548 return 0U; // get index for modifying correct operand
551 /// getPointerOperandType - Method to return the pointer operand as a
553 const PointerType *getPointerOperandType() const {
554 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
558 unsigned getNumIndices() const { // Note: always non-negative
559 return getNumOperands() - 1;
562 bool hasIndices() const {
563 return getNumOperands() > 1;
566 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
567 /// zeros. If so, the result pointer and the first operand have the same
568 /// value, just potentially different types.
569 bool hasAllZeroIndices() const;
571 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
572 /// constant integers. If so, the result pointer and the first operand have
573 /// a constant offset between them.
574 bool hasAllConstantIndices() const;
577 // Methods for support type inquiry through isa, cast, and dyn_cast:
578 static inline bool classof(const GetElementPtrInst *) { return true; }
579 static inline bool classof(const Instruction *I) {
580 return (I->getOpcode() == Instruction::GetElementPtr);
582 static inline bool classof(const Value *V) {
583 return isa<Instruction>(V) && classof(cast<Instruction>(V));
588 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
591 template<typename InputIterator>
592 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
593 InputIterator IdxBegin,
594 InputIterator IdxEnd,
596 const std::string &NameStr,
597 Instruction *InsertBefore)
598 : Instruction(PointerType::get(checkType(
599 getIndexedType(Ptr->getType(),
601 cast<PointerType>(Ptr->getType())
602 ->getAddressSpace()),
604 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
605 Values, InsertBefore) {
606 init(Ptr, IdxBegin, IdxEnd, NameStr,
607 typename std::iterator_traits<InputIterator>::iterator_category());
609 template<typename InputIterator>
610 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
611 InputIterator IdxBegin,
612 InputIterator IdxEnd,
614 const std::string &NameStr,
615 BasicBlock *InsertAtEnd)
616 : Instruction(PointerType::get(checkType(
617 getIndexedType(Ptr->getType(),
619 cast<PointerType>(Ptr->getType())
620 ->getAddressSpace()),
622 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
623 Values, InsertAtEnd) {
624 init(Ptr, IdxBegin, IdxEnd, NameStr,
625 typename std::iterator_traits<InputIterator>::iterator_category());
629 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
632 //===----------------------------------------------------------------------===//
634 //===----------------------------------------------------------------------===//
636 /// This instruction compares its operands according to the predicate given
637 /// to the constructor. It only operates on integers or pointers. The operands
638 /// must be identical types.
639 /// @brief Represent an integer comparison operator.
640 class ICmpInst: public CmpInst {
642 /// @brief Constructor with insert-before-instruction semantics.
644 Instruction *InsertBefore, ///< Where to insert
645 Predicate pred, ///< The predicate to use for the comparison
646 Value *LHS, ///< The left-hand-side of the expression
647 Value *RHS, ///< The right-hand-side of the expression
648 const std::string &NameStr = "" ///< Name of the instruction
649 ) : CmpInst(InsertBefore->getParent()->getContext()->
650 makeCmpResultType(LHS->getType()),
651 Instruction::ICmp, pred, LHS, RHS, NameStr,
653 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
654 pred <= CmpInst::LAST_ICMP_PREDICATE &&
655 "Invalid ICmp predicate value");
656 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
657 "Both operands to ICmp instruction are not of the same type!");
658 // Check that the operands are the right type
659 assert((getOperand(0)->getType()->isIntOrIntVector() ||
660 isa<PointerType>(getOperand(0)->getType())) &&
661 "Invalid operand types for ICmp instruction");
664 /// @brief Constructor with insert-at-end semantics.
666 BasicBlock &InsertAtEnd, ///< Block to insert into.
667 Predicate pred, ///< The predicate to use for the comparison
668 Value *LHS, ///< The left-hand-side of the expression
669 Value *RHS, ///< The right-hand-side of the expression
670 const std::string &NameStr = "" ///< Name of the instruction
671 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
672 Instruction::ICmp, pred, LHS, RHS, NameStr,
674 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
675 pred <= CmpInst::LAST_ICMP_PREDICATE &&
676 "Invalid ICmp predicate value");
677 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
678 "Both operands to ICmp instruction are not of the same type!");
679 // Check that the operands are the right type
680 assert((getOperand(0)->getType()->isIntOrIntVector() ||
681 isa<PointerType>(getOperand(0)->getType())) &&
682 "Invalid operand types for ICmp instruction");
685 /// @brief Constructor with no-insertion semantics
687 LLVMContext &Context, ///< Context to construct within
688 Predicate pred, ///< The predicate to use for the comparison
689 Value *LHS, ///< The left-hand-side of the expression
690 Value *RHS, ///< The right-hand-side of the expression
691 const std::string &NameStr = "" ///< Name of the instruction
692 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
693 Instruction::ICmp, pred, LHS, RHS, NameStr) {
694 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
695 pred <= CmpInst::LAST_ICMP_PREDICATE &&
696 "Invalid ICmp predicate value");
697 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
698 "Both operands to ICmp instruction are not of the same type!");
699 // Check that the operands are the right type
700 assert((getOperand(0)->getType()->isIntOrIntVector() ||
701 isa<PointerType>(getOperand(0)->getType())) &&
702 "Invalid operand types for ICmp instruction");
705 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
706 /// @returns the predicate that would be the result if the operand were
707 /// regarded as signed.
708 /// @brief Return the signed version of the predicate
709 Predicate getSignedPredicate() const {
710 return getSignedPredicate(getPredicate());
713 /// This is a static version that you can use without an instruction.
714 /// @brief Return the signed version of the predicate.
715 static Predicate getSignedPredicate(Predicate pred);
717 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
718 /// @returns the predicate that would be the result if the operand were
719 /// regarded as unsigned.
720 /// @brief Return the unsigned version of the predicate
721 Predicate getUnsignedPredicate() const {
722 return getUnsignedPredicate(getPredicate());
725 /// This is a static version that you can use without an instruction.
726 /// @brief Return the unsigned version of the predicate.
727 static Predicate getUnsignedPredicate(Predicate pred);
729 /// isEquality - Return true if this predicate is either EQ or NE. This also
730 /// tests for commutativity.
731 static bool isEquality(Predicate P) {
732 return P == ICMP_EQ || P == ICMP_NE;
735 /// isEquality - Return true if this predicate is either EQ or NE. This also
736 /// tests for commutativity.
737 bool isEquality() const {
738 return isEquality(getPredicate());
741 /// @returns true if the predicate of this ICmpInst is commutative
742 /// @brief Determine if this relation is commutative.
743 bool isCommutative() const { return isEquality(); }
745 /// isRelational - Return true if the predicate is relational (not EQ or NE).
747 bool isRelational() const {
748 return !isEquality();
751 /// isRelational - Return true if the predicate is relational (not EQ or NE).
753 static bool isRelational(Predicate P) {
754 return !isEquality(P);
757 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
758 /// @brief Determine if this instruction's predicate is signed.
759 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
761 /// @returns true if the predicate provided is signed, false otherwise
762 /// @brief Determine if the predicate is signed.
763 static bool isSignedPredicate(Predicate pred);
765 /// @returns true if the specified compare predicate is
766 /// true when both operands are equal...
767 /// @brief Determine if the icmp is true when both operands are equal
768 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
769 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
770 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
771 pred == ICmpInst::ICMP_SLE;
774 /// @returns true if the specified compare instruction is
775 /// true when both operands are equal...
776 /// @brief Determine if the ICmpInst returns true when both operands are equal
777 bool isTrueWhenEqual() {
778 return isTrueWhenEqual(getPredicate());
781 /// Initialize a set of values that all satisfy the predicate with C.
782 /// @brief Make a ConstantRange for a relation with a constant value.
783 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
785 /// Exchange the two operands to this instruction in such a way that it does
786 /// not modify the semantics of the instruction. The predicate value may be
787 /// changed to retain the same result if the predicate is order dependent
789 /// @brief Swap operands and adjust predicate.
790 void swapOperands() {
791 SubclassData = getSwappedPredicate();
792 Op<0>().swap(Op<1>());
795 virtual ICmpInst *clone(LLVMContext &Context) const;
797 // Methods for support type inquiry through isa, cast, and dyn_cast:
798 static inline bool classof(const ICmpInst *) { return true; }
799 static inline bool classof(const Instruction *I) {
800 return I->getOpcode() == Instruction::ICmp;
802 static inline bool classof(const Value *V) {
803 return isa<Instruction>(V) && classof(cast<Instruction>(V));
808 //===----------------------------------------------------------------------===//
810 //===----------------------------------------------------------------------===//
812 /// This instruction compares its operands according to the predicate given
813 /// to the constructor. It only operates on floating point values or packed
814 /// vectors of floating point values. The operands must be identical types.
815 /// @brief Represents a floating point comparison operator.
816 class FCmpInst: public CmpInst {
818 /// @brief Constructor with insert-before-instruction semantics.
820 Instruction *InsertBefore, ///< Where to insert
821 Predicate pred, ///< The predicate to use for the comparison
822 Value *LHS, ///< The left-hand-side of the expression
823 Value *RHS, ///< The right-hand-side of the expression
824 const std::string &NameStr = "" ///< Name of the instruction
825 ) : CmpInst(InsertBefore->getParent()->getContext()->
826 makeCmpResultType(LHS->getType()),
827 Instruction::FCmp, pred, LHS, RHS, NameStr,
829 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
830 "Invalid FCmp predicate value");
831 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
832 "Both operands to FCmp instruction are not of the same type!");
833 // Check that the operands are the right type
834 assert(getOperand(0)->getType()->isFPOrFPVector() &&
835 "Invalid operand types for FCmp instruction");
838 /// @brief Constructor with insert-at-end semantics.
840 BasicBlock &InsertAtEnd, ///< Block to insert into.
841 Predicate pred, ///< The predicate to use for the comparison
842 Value *LHS, ///< The left-hand-side of the expression
843 Value *RHS, ///< The right-hand-side of the expression
844 const std::string &NameStr = "" ///< Name of the instruction
845 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
846 Instruction::FCmp, pred, LHS, RHS, NameStr,
848 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
849 "Invalid FCmp predicate value");
850 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
851 "Both operands to FCmp instruction are not of the same type!");
852 // Check that the operands are the right type
853 assert(getOperand(0)->getType()->isFPOrFPVector() &&
854 "Invalid operand types for FCmp instruction");
857 /// @brief Constructor with no-insertion semantics
859 LLVMContext &Context, ///< Context to build in
860 Predicate pred, ///< The predicate to use for the comparison
861 Value *LHS, ///< The left-hand-side of the expression
862 Value *RHS, ///< The right-hand-side of the expression
863 const std::string &NameStr = "" ///< Name of the instruction
864 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
865 Instruction::FCmp, pred, LHS, RHS, NameStr) {
866 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
867 "Invalid FCmp predicate value");
868 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
869 "Both operands to FCmp instruction are not of the same type!");
870 // Check that the operands are the right type
871 assert(getOperand(0)->getType()->isFPOrFPVector() &&
872 "Invalid operand types for FCmp instruction");
875 /// @returns true if the predicate of this instruction is EQ or NE.
876 /// @brief Determine if this is an equality predicate.
877 bool isEquality() const {
878 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
879 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
882 /// @returns true if the predicate of this instruction is commutative.
883 /// @brief Determine if this is a commutative predicate.
884 bool isCommutative() const {
885 return isEquality() ||
886 SubclassData == FCMP_FALSE ||
887 SubclassData == FCMP_TRUE ||
888 SubclassData == FCMP_ORD ||
889 SubclassData == FCMP_UNO;
892 /// @returns true if the predicate is relational (not EQ or NE).
893 /// @brief Determine if this a relational predicate.
894 bool isRelational() const { return !isEquality(); }
896 /// Exchange the two operands to this instruction in such a way that it does
897 /// not modify the semantics of the instruction. The predicate value may be
898 /// changed to retain the same result if the predicate is order dependent
900 /// @brief Swap operands and adjust predicate.
901 void swapOperands() {
902 SubclassData = getSwappedPredicate();
903 Op<0>().swap(Op<1>());
906 virtual FCmpInst *clone(LLVMContext &Context) const;
908 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
909 static inline bool classof(const FCmpInst *) { return true; }
910 static inline bool classof(const Instruction *I) {
911 return I->getOpcode() == Instruction::FCmp;
913 static inline bool classof(const Value *V) {
914 return isa<Instruction>(V) && classof(cast<Instruction>(V));
919 //===----------------------------------------------------------------------===//
921 //===----------------------------------------------------------------------===//
922 /// CallInst - This class represents a function call, abstracting a target
923 /// machine's calling convention. This class uses low bit of the SubClassData
924 /// field to indicate whether or not this is a tail call. The rest of the bits
925 /// hold the calling convention of the call.
928 class CallInst : public Instruction {
929 AttrListPtr AttributeList; ///< parameter attributes for call
930 CallInst(const CallInst &CI);
931 void init(Value *Func, Value* const *Params, unsigned NumParams);
932 void init(Value *Func, Value *Actual1, Value *Actual2);
933 void init(Value *Func, Value *Actual);
934 void init(Value *Func);
936 template<typename InputIterator>
937 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
938 const std::string &NameStr,
939 // This argument ensures that we have an iterator we can
940 // do arithmetic on in constant time
941 std::random_access_iterator_tag) {
942 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
944 // This requires that the iterator points to contiguous memory.
945 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
949 /// Construct a CallInst given a range of arguments. InputIterator
950 /// must be a random-access iterator pointing to contiguous storage
951 /// (e.g. a std::vector<>::iterator). Checks are made for
952 /// random-accessness but not for contiguous storage as that would
953 /// incur runtime overhead.
954 /// @brief Construct a CallInst from a range of arguments
955 template<typename InputIterator>
956 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
957 const std::string &NameStr, Instruction *InsertBefore);
959 /// Construct a CallInst given a range of arguments. InputIterator
960 /// must be a random-access iterator pointing to contiguous storage
961 /// (e.g. a std::vector<>::iterator). Checks are made for
962 /// random-accessness but not for contiguous storage as that would
963 /// incur runtime overhead.
964 /// @brief Construct a CallInst from a range of arguments
965 template<typename InputIterator>
966 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
967 const std::string &NameStr, BasicBlock *InsertAtEnd);
969 CallInst(Value *F, Value *Actual, const std::string& NameStr,
970 Instruction *InsertBefore);
971 CallInst(Value *F, Value *Actual, const std::string& NameStr,
972 BasicBlock *InsertAtEnd);
973 explicit CallInst(Value *F, const std::string &NameStr,
974 Instruction *InsertBefore);
975 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
977 template<typename InputIterator>
978 static CallInst *Create(Value *Func,
979 InputIterator ArgBegin, InputIterator ArgEnd,
980 const std::string &NameStr = "",
981 Instruction *InsertBefore = 0) {
982 return new((unsigned)(ArgEnd - ArgBegin + 1))
983 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
985 template<typename InputIterator>
986 static CallInst *Create(Value *Func,
987 InputIterator ArgBegin, InputIterator ArgEnd,
988 const std::string &NameStr, BasicBlock *InsertAtEnd) {
989 return new((unsigned)(ArgEnd - ArgBegin + 1))
990 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
992 static CallInst *Create(Value *F, Value *Actual,
993 const std::string& NameStr = "",
994 Instruction *InsertBefore = 0) {
995 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
997 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
998 BasicBlock *InsertAtEnd) {
999 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1001 static CallInst *Create(Value *F, const std::string &NameStr = "",
1002 Instruction *InsertBefore = 0) {
1003 return new(1) CallInst(F, NameStr, InsertBefore);
1005 static CallInst *Create(Value *F, const std::string &NameStr,
1006 BasicBlock *InsertAtEnd) {
1007 return new(1) CallInst(F, NameStr, InsertAtEnd);
1012 bool isTailCall() const { return SubclassData & 1; }
1013 void setTailCall(bool isTC = true) {
1014 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1017 virtual CallInst *clone(LLVMContext &Context) const;
1019 /// Provide fast operand accessors
1020 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1022 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1024 unsigned getCallingConv() const { return SubclassData >> 1; }
1025 void setCallingConv(unsigned CC) {
1026 SubclassData = (SubclassData & 1) | (CC << 1);
1029 /// getAttributes - Return the parameter attributes for this call.
1031 const AttrListPtr &getAttributes() const { return AttributeList; }
1033 /// setAttributes - Set the parameter attributes for this call.
1035 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1037 /// addAttribute - adds the attribute to the list of attributes.
1038 void addAttribute(unsigned i, Attributes attr);
1040 /// removeAttribute - removes the attribute from the list of attributes.
1041 void removeAttribute(unsigned i, Attributes attr);
1043 /// @brief Determine whether the call or the callee has the given attribute.
1044 bool paramHasAttr(unsigned i, Attributes attr) const;
1046 /// @brief Extract the alignment for a call or parameter (0=unknown).
1047 unsigned getParamAlignment(unsigned i) const {
1048 return AttributeList.getParamAlignment(i);
1051 /// @brief Determine if the call does not access memory.
1052 bool doesNotAccessMemory() const {
1053 return paramHasAttr(~0, Attribute::ReadNone);
1055 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1056 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1057 else removeAttribute(~0, Attribute::ReadNone);
1060 /// @brief Determine if the call does not access or only reads memory.
1061 bool onlyReadsMemory() const {
1062 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1064 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1065 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1066 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1069 /// @brief Determine if the call cannot return.
1070 bool doesNotReturn() const {
1071 return paramHasAttr(~0, Attribute::NoReturn);
1073 void setDoesNotReturn(bool DoesNotReturn = true) {
1074 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1075 else removeAttribute(~0, Attribute::NoReturn);
1078 /// @brief Determine if the call cannot unwind.
1079 bool doesNotThrow() const {
1080 return paramHasAttr(~0, Attribute::NoUnwind);
1082 void setDoesNotThrow(bool DoesNotThrow = true) {
1083 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1084 else removeAttribute(~0, Attribute::NoUnwind);
1087 /// @brief Determine if the call returns a structure through first
1088 /// pointer argument.
1089 bool hasStructRetAttr() const {
1090 // Be friendly and also check the callee.
1091 return paramHasAttr(1, Attribute::StructRet);
1094 /// @brief Determine if any call argument is an aggregate passed by value.
1095 bool hasByValArgument() const {
1096 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1099 /// getCalledFunction - Return the function called, or null if this is an
1100 /// indirect function invocation.
1102 Function *getCalledFunction() const {
1103 return dyn_cast<Function>(Op<0>());
1106 /// getCalledValue - Get a pointer to the function that is invoked by this
1108 const Value *getCalledValue() const { return Op<0>(); }
1109 Value *getCalledValue() { return Op<0>(); }
1111 // Methods for support type inquiry through isa, cast, and dyn_cast:
1112 static inline bool classof(const CallInst *) { return true; }
1113 static inline bool classof(const Instruction *I) {
1114 return I->getOpcode() == Instruction::Call;
1116 static inline bool classof(const Value *V) {
1117 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1122 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1125 template<typename InputIterator>
1126 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1127 const std::string &NameStr, BasicBlock *InsertAtEnd)
1128 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1129 ->getElementType())->getReturnType(),
1131 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1132 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1133 init(Func, ArgBegin, ArgEnd, NameStr,
1134 typename std::iterator_traits<InputIterator>::iterator_category());
1137 template<typename InputIterator>
1138 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1139 const std::string &NameStr, Instruction *InsertBefore)
1140 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1141 ->getElementType())->getReturnType(),
1143 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1144 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1145 init(Func, ArgBegin, ArgEnd, NameStr,
1146 typename std::iterator_traits<InputIterator>::iterator_category());
1149 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1151 //===----------------------------------------------------------------------===//
1153 //===----------------------------------------------------------------------===//
1155 /// SelectInst - This class represents the LLVM 'select' instruction.
1157 class SelectInst : public Instruction {
1158 void init(Value *C, Value *S1, Value *S2) {
1159 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1165 SelectInst(const SelectInst &SI)
1166 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1167 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1169 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1170 Instruction *InsertBefore)
1171 : Instruction(S1->getType(), Instruction::Select,
1172 &Op<0>(), 3, InsertBefore) {
1176 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1177 BasicBlock *InsertAtEnd)
1178 : Instruction(S1->getType(), Instruction::Select,
1179 &Op<0>(), 3, InsertAtEnd) {
1184 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1185 const std::string &NameStr = "",
1186 Instruction *InsertBefore = 0) {
1187 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1189 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1190 const std::string &NameStr,
1191 BasicBlock *InsertAtEnd) {
1192 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1195 Value *getCondition() const { return Op<0>(); }
1196 Value *getTrueValue() const { return Op<1>(); }
1197 Value *getFalseValue() const { return Op<2>(); }
1199 /// areInvalidOperands - Return a string if the specified operands are invalid
1200 /// for a select operation, otherwise return null.
1201 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1203 /// Transparently provide more efficient getOperand methods.
1204 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1206 OtherOps getOpcode() const {
1207 return static_cast<OtherOps>(Instruction::getOpcode());
1210 virtual SelectInst *clone(LLVMContext &Context) const;
1212 // Methods for support type inquiry through isa, cast, and dyn_cast:
1213 static inline bool classof(const SelectInst *) { return true; }
1214 static inline bool classof(const Instruction *I) {
1215 return I->getOpcode() == Instruction::Select;
1217 static inline bool classof(const Value *V) {
1218 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1223 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1226 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1228 //===----------------------------------------------------------------------===//
1230 //===----------------------------------------------------------------------===//
1232 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1233 /// an argument of the specified type given a va_list and increments that list
1235 class VAArgInst : public UnaryInstruction {
1236 VAArgInst(const VAArgInst &VAA)
1237 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1239 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1240 Instruction *InsertBefore = 0)
1241 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1244 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1245 BasicBlock *InsertAtEnd)
1246 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1250 virtual VAArgInst *clone(LLVMContext &Context) const;
1252 // Methods for support type inquiry through isa, cast, and dyn_cast:
1253 static inline bool classof(const VAArgInst *) { return true; }
1254 static inline bool classof(const Instruction *I) {
1255 return I->getOpcode() == VAArg;
1257 static inline bool classof(const Value *V) {
1258 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1262 //===----------------------------------------------------------------------===//
1263 // ExtractElementInst Class
1264 //===----------------------------------------------------------------------===//
1266 /// ExtractElementInst - This instruction extracts a single (scalar)
1267 /// element from a VectorType value
1269 class ExtractElementInst : public Instruction {
1270 ExtractElementInst(const ExtractElementInst &EE) :
1271 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1272 Op<0>() = EE.Op<0>();
1273 Op<1>() = EE.Op<1>();
1277 // allocate space for exactly two operands
1278 void *operator new(size_t s) {
1279 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1281 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1282 Instruction *InsertBefore = 0);
1283 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1284 BasicBlock *InsertAtEnd);
1286 /// isValidOperands - Return true if an extractelement instruction can be
1287 /// formed with the specified operands.
1288 static bool isValidOperands(const Value *Vec, const Value *Idx);
1290 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1292 /// Transparently provide more efficient getOperand methods.
1293 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1295 // Methods for support type inquiry through isa, cast, and dyn_cast:
1296 static inline bool classof(const ExtractElementInst *) { return true; }
1297 static inline bool classof(const Instruction *I) {
1298 return I->getOpcode() == Instruction::ExtractElement;
1300 static inline bool classof(const Value *V) {
1301 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1306 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1309 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1311 //===----------------------------------------------------------------------===//
1312 // InsertElementInst Class
1313 //===----------------------------------------------------------------------===//
1315 /// InsertElementInst - This instruction inserts a single (scalar)
1316 /// element into a VectorType value
1318 class InsertElementInst : public Instruction {
1319 InsertElementInst(const InsertElementInst &IE);
1320 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1321 const std::string &NameStr = "",
1322 Instruction *InsertBefore = 0);
1323 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1324 const std::string &NameStr, BasicBlock *InsertAtEnd);
1326 static InsertElementInst *Create(const InsertElementInst &IE) {
1327 return new(IE.getNumOperands()) InsertElementInst(IE);
1329 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1330 const std::string &NameStr = "",
1331 Instruction *InsertBefore = 0) {
1332 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1334 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1335 const std::string &NameStr,
1336 BasicBlock *InsertAtEnd) {
1337 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1340 /// isValidOperands - Return true if an insertelement instruction can be
1341 /// formed with the specified operands.
1342 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1345 virtual InsertElementInst *clone(LLVMContext &Context) const;
1347 /// getType - Overload to return most specific vector type.
1349 const VectorType *getType() const {
1350 return reinterpret_cast<const VectorType*>(Instruction::getType());
1353 /// Transparently provide more efficient getOperand methods.
1354 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1356 // Methods for support type inquiry through isa, cast, and dyn_cast:
1357 static inline bool classof(const InsertElementInst *) { return true; }
1358 static inline bool classof(const Instruction *I) {
1359 return I->getOpcode() == Instruction::InsertElement;
1361 static inline bool classof(const Value *V) {
1362 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1367 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1370 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1372 //===----------------------------------------------------------------------===//
1373 // ShuffleVectorInst Class
1374 //===----------------------------------------------------------------------===//
1376 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1379 class ShuffleVectorInst : public Instruction {
1380 ShuffleVectorInst(const ShuffleVectorInst &IE);
1382 // allocate space for exactly three operands
1383 void *operator new(size_t s) {
1384 return User::operator new(s, 3);
1386 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1387 const std::string &NameStr = "",
1388 Instruction *InsertBefor = 0);
1389 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1390 const std::string &NameStr, BasicBlock *InsertAtEnd);
1392 /// isValidOperands - Return true if a shufflevector instruction can be
1393 /// formed with the specified operands.
1394 static bool isValidOperands(const Value *V1, const Value *V2,
1397 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1399 /// getType - Overload to return most specific vector type.
1401 const VectorType *getType() const {
1402 return reinterpret_cast<const VectorType*>(Instruction::getType());
1405 /// Transparently provide more efficient getOperand methods.
1406 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1408 /// getMaskValue - Return the index from the shuffle mask for the specified
1409 /// output result. This is either -1 if the element is undef or a number less
1410 /// than 2*numelements.
1411 int getMaskValue(unsigned i) const;
1413 // Methods for support type inquiry through isa, cast, and dyn_cast:
1414 static inline bool classof(const ShuffleVectorInst *) { return true; }
1415 static inline bool classof(const Instruction *I) {
1416 return I->getOpcode() == Instruction::ShuffleVector;
1418 static inline bool classof(const Value *V) {
1419 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1424 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1427 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1429 //===----------------------------------------------------------------------===//
1430 // ExtractValueInst Class
1431 //===----------------------------------------------------------------------===//
1433 /// ExtractValueInst - This instruction extracts a struct member or array
1434 /// element value from an aggregate value.
1436 class ExtractValueInst : public UnaryInstruction {
1437 SmallVector<unsigned, 4> Indices;
1439 ExtractValueInst(const ExtractValueInst &EVI);
1440 void init(const unsigned *Idx, unsigned NumIdx,
1441 const std::string &NameStr);
1442 void init(unsigned Idx, const std::string &NameStr);
1444 template<typename InputIterator>
1445 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1446 const std::string &NameStr,
1447 // This argument ensures that we have an iterator we can
1448 // do arithmetic on in constant time
1449 std::random_access_iterator_tag) {
1450 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1452 // There's no fundamental reason why we require at least one index
1453 // (other than weirdness with &*IdxBegin being invalid; see
1454 // getelementptr's init routine for example). But there's no
1455 // present need to support it.
1456 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1458 // This requires that the iterator points to contiguous memory.
1459 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1460 // we have to build an array here
1463 /// getIndexedType - Returns the type of the element that would be extracted
1464 /// with an extractvalue instruction with the specified parameters.
1466 /// Null is returned if the indices are invalid for the specified
1469 static const Type *getIndexedType(const Type *Agg,
1470 const unsigned *Idx, unsigned NumIdx);
1472 template<typename InputIterator>
1473 static const Type *getIndexedType(const Type *Ptr,
1474 InputIterator IdxBegin,
1475 InputIterator IdxEnd,
1476 // This argument ensures that we
1477 // have an iterator we can do
1478 // arithmetic on in constant time
1479 std::random_access_iterator_tag) {
1480 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1483 // This requires that the iterator points to contiguous memory.
1484 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1486 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1489 /// Constructors - Create a extractvalue instruction with a base aggregate
1490 /// value and a list of indices. The first ctor can optionally insert before
1491 /// an existing instruction, the second appends the new instruction to the
1492 /// specified BasicBlock.
1493 template<typename InputIterator>
1494 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1495 InputIterator IdxEnd,
1496 const std::string &NameStr,
1497 Instruction *InsertBefore);
1498 template<typename InputIterator>
1499 inline ExtractValueInst(Value *Agg,
1500 InputIterator IdxBegin, InputIterator IdxEnd,
1501 const std::string &NameStr, BasicBlock *InsertAtEnd);
1503 // allocate space for exactly one operand
1504 void *operator new(size_t s) {
1505 return User::operator new(s, 1);
1509 template<typename InputIterator>
1510 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1511 InputIterator IdxEnd,
1512 const std::string &NameStr = "",
1513 Instruction *InsertBefore = 0) {
1515 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1517 template<typename InputIterator>
1518 static ExtractValueInst *Create(Value *Agg,
1519 InputIterator IdxBegin, InputIterator IdxEnd,
1520 const std::string &NameStr,
1521 BasicBlock *InsertAtEnd) {
1522 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1525 /// Constructors - These two creators are convenience methods because one
1526 /// index extractvalue instructions are much more common than those with
1528 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1529 const std::string &NameStr = "",
1530 Instruction *InsertBefore = 0) {
1531 unsigned Idxs[1] = { Idx };
1532 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1534 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1535 const std::string &NameStr,
1536 BasicBlock *InsertAtEnd) {
1537 unsigned Idxs[1] = { Idx };
1538 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1541 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1543 /// getIndexedType - Returns the type of the element that would be extracted
1544 /// with an extractvalue instruction with the specified parameters.
1546 /// Null is returned if the indices are invalid for the specified
1549 template<typename InputIterator>
1550 static const Type *getIndexedType(const Type *Ptr,
1551 InputIterator IdxBegin,
1552 InputIterator IdxEnd) {
1553 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1554 typename std::iterator_traits<InputIterator>::
1555 iterator_category());
1557 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1559 typedef const unsigned* idx_iterator;
1560 inline idx_iterator idx_begin() const { return Indices.begin(); }
1561 inline idx_iterator idx_end() const { return Indices.end(); }
1563 Value *getAggregateOperand() {
1564 return getOperand(0);
1566 const Value *getAggregateOperand() const {
1567 return getOperand(0);
1569 static unsigned getAggregateOperandIndex() {
1570 return 0U; // get index for modifying correct operand
1573 unsigned getNumIndices() const { // Note: always non-negative
1574 return (unsigned)Indices.size();
1577 bool hasIndices() const {
1581 // Methods for support type inquiry through isa, cast, and dyn_cast:
1582 static inline bool classof(const ExtractValueInst *) { return true; }
1583 static inline bool classof(const Instruction *I) {
1584 return I->getOpcode() == Instruction::ExtractValue;
1586 static inline bool classof(const Value *V) {
1587 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1591 template<typename InputIterator>
1592 ExtractValueInst::ExtractValueInst(Value *Agg,
1593 InputIterator IdxBegin,
1594 InputIterator IdxEnd,
1595 const std::string &NameStr,
1596 Instruction *InsertBefore)
1597 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1599 ExtractValue, Agg, InsertBefore) {
1600 init(IdxBegin, IdxEnd, NameStr,
1601 typename std::iterator_traits<InputIterator>::iterator_category());
1603 template<typename InputIterator>
1604 ExtractValueInst::ExtractValueInst(Value *Agg,
1605 InputIterator IdxBegin,
1606 InputIterator IdxEnd,
1607 const std::string &NameStr,
1608 BasicBlock *InsertAtEnd)
1609 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1611 ExtractValue, Agg, InsertAtEnd) {
1612 init(IdxBegin, IdxEnd, NameStr,
1613 typename std::iterator_traits<InputIterator>::iterator_category());
1617 //===----------------------------------------------------------------------===//
1618 // InsertValueInst Class
1619 //===----------------------------------------------------------------------===//
1621 /// InsertValueInst - This instruction inserts a struct field of array element
1622 /// value into an aggregate value.
1624 class InsertValueInst : public Instruction {
1625 SmallVector<unsigned, 4> Indices;
1627 void *operator new(size_t, unsigned); // Do not implement
1628 InsertValueInst(const InsertValueInst &IVI);
1629 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1630 const std::string &NameStr);
1631 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1633 template<typename InputIterator>
1634 void init(Value *Agg, Value *Val,
1635 InputIterator IdxBegin, InputIterator IdxEnd,
1636 const std::string &NameStr,
1637 // This argument ensures that we have an iterator we can
1638 // do arithmetic on in constant time
1639 std::random_access_iterator_tag) {
1640 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1642 // There's no fundamental reason why we require at least one index
1643 // (other than weirdness with &*IdxBegin being invalid; see
1644 // getelementptr's init routine for example). But there's no
1645 // present need to support it.
1646 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1648 // This requires that the iterator points to contiguous memory.
1649 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1650 // we have to build an array here
1653 /// Constructors - Create a insertvalue instruction with a base aggregate
1654 /// value, a value to insert, and a list of indices. The first ctor can
1655 /// optionally insert before an existing instruction, the second appends
1656 /// the new instruction to the specified BasicBlock.
1657 template<typename InputIterator>
1658 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1659 InputIterator IdxEnd,
1660 const std::string &NameStr,
1661 Instruction *InsertBefore);
1662 template<typename InputIterator>
1663 inline InsertValueInst(Value *Agg, Value *Val,
1664 InputIterator IdxBegin, InputIterator IdxEnd,
1665 const std::string &NameStr, BasicBlock *InsertAtEnd);
1667 /// Constructors - These two constructors are convenience methods because one
1668 /// and two index insertvalue instructions are so common.
1669 InsertValueInst(Value *Agg, Value *Val,
1670 unsigned Idx, const std::string &NameStr = "",
1671 Instruction *InsertBefore = 0);
1672 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1673 const std::string &NameStr, BasicBlock *InsertAtEnd);
1675 // allocate space for exactly two operands
1676 void *operator new(size_t s) {
1677 return User::operator new(s, 2);
1680 template<typename InputIterator>
1681 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1682 InputIterator IdxEnd,
1683 const std::string &NameStr = "",
1684 Instruction *InsertBefore = 0) {
1685 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1686 NameStr, InsertBefore);
1688 template<typename InputIterator>
1689 static InsertValueInst *Create(Value *Agg, Value *Val,
1690 InputIterator IdxBegin, InputIterator IdxEnd,
1691 const std::string &NameStr,
1692 BasicBlock *InsertAtEnd) {
1693 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1694 NameStr, InsertAtEnd);
1697 /// Constructors - These two creators are convenience methods because one
1698 /// index insertvalue instructions are much more common than those with
1700 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1701 const std::string &NameStr = "",
1702 Instruction *InsertBefore = 0) {
1703 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1705 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1706 const std::string &NameStr,
1707 BasicBlock *InsertAtEnd) {
1708 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1711 virtual InsertValueInst *clone(LLVMContext &Context) const;
1713 /// Transparently provide more efficient getOperand methods.
1714 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1716 typedef const unsigned* idx_iterator;
1717 inline idx_iterator idx_begin() const { return Indices.begin(); }
1718 inline idx_iterator idx_end() const { return Indices.end(); }
1720 Value *getAggregateOperand() {
1721 return getOperand(0);
1723 const Value *getAggregateOperand() const {
1724 return getOperand(0);
1726 static unsigned getAggregateOperandIndex() {
1727 return 0U; // get index for modifying correct operand
1730 Value *getInsertedValueOperand() {
1731 return getOperand(1);
1733 const Value *getInsertedValueOperand() const {
1734 return getOperand(1);
1736 static unsigned getInsertedValueOperandIndex() {
1737 return 1U; // get index for modifying correct operand
1740 unsigned getNumIndices() const { // Note: always non-negative
1741 return (unsigned)Indices.size();
1744 bool hasIndices() const {
1748 // Methods for support type inquiry through isa, cast, and dyn_cast:
1749 static inline bool classof(const InsertValueInst *) { return true; }
1750 static inline bool classof(const Instruction *I) {
1751 return I->getOpcode() == Instruction::InsertValue;
1753 static inline bool classof(const Value *V) {
1754 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1759 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1762 template<typename InputIterator>
1763 InsertValueInst::InsertValueInst(Value *Agg,
1765 InputIterator IdxBegin,
1766 InputIterator IdxEnd,
1767 const std::string &NameStr,
1768 Instruction *InsertBefore)
1769 : Instruction(Agg->getType(), InsertValue,
1770 OperandTraits<InsertValueInst>::op_begin(this),
1772 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1773 typename std::iterator_traits<InputIterator>::iterator_category());
1775 template<typename InputIterator>
1776 InsertValueInst::InsertValueInst(Value *Agg,
1778 InputIterator IdxBegin,
1779 InputIterator IdxEnd,
1780 const std::string &NameStr,
1781 BasicBlock *InsertAtEnd)
1782 : Instruction(Agg->getType(), InsertValue,
1783 OperandTraits<InsertValueInst>::op_begin(this),
1785 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1786 typename std::iterator_traits<InputIterator>::iterator_category());
1789 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1791 //===----------------------------------------------------------------------===//
1793 //===----------------------------------------------------------------------===//
1795 // PHINode - The PHINode class is used to represent the magical mystical PHI
1796 // node, that can not exist in nature, but can be synthesized in a computer
1797 // scientist's overactive imagination.
1799 class PHINode : public Instruction {
1800 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1801 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1802 /// the number actually in use.
1803 unsigned ReservedSpace;
1804 PHINode(const PHINode &PN);
1805 // allocate space for exactly zero operands
1806 void *operator new(size_t s) {
1807 return User::operator new(s, 0);
1809 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1810 Instruction *InsertBefore = 0)
1811 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1816 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1817 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1822 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1823 Instruction *InsertBefore = 0) {
1824 return new PHINode(Ty, NameStr, InsertBefore);
1826 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1827 BasicBlock *InsertAtEnd) {
1828 return new PHINode(Ty, NameStr, InsertAtEnd);
1832 /// reserveOperandSpace - This method can be used to avoid repeated
1833 /// reallocation of PHI operand lists by reserving space for the correct
1834 /// number of operands before adding them. Unlike normal vector reserves,
1835 /// this method can also be used to trim the operand space.
1836 void reserveOperandSpace(unsigned NumValues) {
1837 resizeOperands(NumValues*2);
1840 virtual PHINode *clone(LLVMContext &Context) const;
1842 /// Provide fast operand accessors
1843 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1845 /// getNumIncomingValues - Return the number of incoming edges
1847 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1849 /// getIncomingValue - Return incoming value number x
1851 Value *getIncomingValue(unsigned i) const {
1852 assert(i*2 < getNumOperands() && "Invalid value number!");
1853 return getOperand(i*2);
1855 void setIncomingValue(unsigned i, Value *V) {
1856 assert(i*2 < getNumOperands() && "Invalid value number!");
1859 static unsigned getOperandNumForIncomingValue(unsigned i) {
1862 static unsigned getIncomingValueNumForOperand(unsigned i) {
1863 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1867 /// getIncomingBlock - Return incoming basic block corresponding
1868 /// to value use iterator
1870 template <typename U>
1871 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1872 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1873 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1875 /// getIncomingBlock - Return incoming basic block number x
1877 BasicBlock *getIncomingBlock(unsigned i) const {
1878 return static_cast<BasicBlock*>(getOperand(i*2+1));
1880 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1881 setOperand(i*2+1, BB);
1883 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1886 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1887 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1891 /// addIncoming - Add an incoming value to the end of the PHI list
1893 void addIncoming(Value *V, BasicBlock *BB) {
1894 assert(V && "PHI node got a null value!");
1895 assert(BB && "PHI node got a null basic block!");
1896 assert(getType() == V->getType() &&
1897 "All operands to PHI node must be the same type as the PHI node!");
1898 unsigned OpNo = NumOperands;
1899 if (OpNo+2 > ReservedSpace)
1900 resizeOperands(0); // Get more space!
1901 // Initialize some new operands.
1902 NumOperands = OpNo+2;
1903 OperandList[OpNo] = V;
1904 OperandList[OpNo+1] = BB;
1907 /// removeIncomingValue - Remove an incoming value. This is useful if a
1908 /// predecessor basic block is deleted. The value removed is returned.
1910 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1911 /// is true), the PHI node is destroyed and any uses of it are replaced with
1912 /// dummy values. The only time there should be zero incoming values to a PHI
1913 /// node is when the block is dead, so this strategy is sound.
1915 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1917 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1918 int Idx = getBasicBlockIndex(BB);
1919 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1920 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1923 /// getBasicBlockIndex - Return the first index of the specified basic
1924 /// block in the value list for this PHI. Returns -1 if no instance.
1926 int getBasicBlockIndex(const BasicBlock *BB) const {
1927 Use *OL = OperandList;
1928 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1929 if (OL[i+1].get() == BB) return i/2;
1933 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1934 return getIncomingValue(getBasicBlockIndex(BB));
1937 /// hasConstantValue - If the specified PHI node always merges together the
1938 /// same value, return the value, otherwise return null.
1940 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1942 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1943 static inline bool classof(const PHINode *) { return true; }
1944 static inline bool classof(const Instruction *I) {
1945 return I->getOpcode() == Instruction::PHI;
1947 static inline bool classof(const Value *V) {
1948 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1951 void resizeOperands(unsigned NumOperands);
1955 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1958 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1961 //===----------------------------------------------------------------------===//
1963 //===----------------------------------------------------------------------===//
1965 //===---------------------------------------------------------------------------
1966 /// ReturnInst - Return a value (possibly void), from a function. Execution
1967 /// does not continue in this function any longer.
1969 class ReturnInst : public TerminatorInst {
1970 ReturnInst(const ReturnInst &RI);
1973 // ReturnInst constructors:
1974 // ReturnInst() - 'ret void' instruction
1975 // ReturnInst( null) - 'ret void' instruction
1976 // ReturnInst(Value* X) - 'ret X' instruction
1977 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1978 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1979 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1980 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1982 // NOTE: If the Value* passed is of type void then the constructor behaves as
1983 // if it was passed NULL.
1984 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1985 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1986 explicit ReturnInst(BasicBlock *InsertAtEnd);
1988 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1989 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1991 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1992 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1994 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1995 return new(0) ReturnInst(InsertAtEnd);
1997 virtual ~ReturnInst();
1999 virtual ReturnInst *clone(LLVMContext &Context) const;
2001 /// Provide fast operand accessors
2002 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2004 /// Convenience accessor
2005 Value *getReturnValue(unsigned n = 0) const {
2006 return n < getNumOperands()
2011 unsigned getNumSuccessors() const { return 0; }
2013 // Methods for support type inquiry through isa, cast, and dyn_cast:
2014 static inline bool classof(const ReturnInst *) { return true; }
2015 static inline bool classof(const Instruction *I) {
2016 return (I->getOpcode() == Instruction::Ret);
2018 static inline bool classof(const Value *V) {
2019 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2022 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2023 virtual unsigned getNumSuccessorsV() const;
2024 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2028 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2031 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2033 //===----------------------------------------------------------------------===//
2035 //===----------------------------------------------------------------------===//
2037 //===---------------------------------------------------------------------------
2038 /// BranchInst - Conditional or Unconditional Branch instruction.
2040 class BranchInst : public TerminatorInst {
2041 /// Ops list - Branches are strange. The operands are ordered:
2042 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2043 /// they don't have to check for cond/uncond branchness. These are mostly
2044 /// accessed relative from op_end().
2045 BranchInst(const BranchInst &BI);
2047 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2048 // BranchInst(BB *B) - 'br B'
2049 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2050 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2051 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2052 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2053 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2054 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2055 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2056 Instruction *InsertBefore = 0);
2057 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2058 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2059 BasicBlock *InsertAtEnd);
2061 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2062 return new(1, true) BranchInst(IfTrue, InsertBefore);
2064 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2065 Value *Cond, Instruction *InsertBefore = 0) {
2066 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2068 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2069 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2071 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2072 Value *Cond, BasicBlock *InsertAtEnd) {
2073 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2078 /// Transparently provide more efficient getOperand methods.
2079 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2081 virtual BranchInst *clone(LLVMContext &Context) const;
2083 bool isUnconditional() const { return getNumOperands() == 1; }
2084 bool isConditional() const { return getNumOperands() == 3; }
2086 Value *getCondition() const {
2087 assert(isConditional() && "Cannot get condition of an uncond branch!");
2091 void setCondition(Value *V) {
2092 assert(isConditional() && "Cannot set condition of unconditional branch!");
2096 // setUnconditionalDest - Change the current branch to an unconditional branch
2097 // targeting the specified block.
2098 // FIXME: Eliminate this ugly method.
2099 void setUnconditionalDest(BasicBlock *Dest) {
2101 if (isConditional()) { // Convert this to an uncond branch.
2105 OperandList = op_begin();
2109 unsigned getNumSuccessors() const { return 1+isConditional(); }
2111 BasicBlock *getSuccessor(unsigned i) const {
2112 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2113 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2116 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2117 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2118 *(&Op<-1>() - idx) = NewSucc;
2121 // Methods for support type inquiry through isa, cast, and dyn_cast:
2122 static inline bool classof(const BranchInst *) { return true; }
2123 static inline bool classof(const Instruction *I) {
2124 return (I->getOpcode() == Instruction::Br);
2126 static inline bool classof(const Value *V) {
2127 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2130 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2131 virtual unsigned getNumSuccessorsV() const;
2132 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2136 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2138 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2140 //===----------------------------------------------------------------------===//
2142 //===----------------------------------------------------------------------===//
2144 //===---------------------------------------------------------------------------
2145 /// SwitchInst - Multiway switch
2147 class SwitchInst : public TerminatorInst {
2148 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2149 unsigned ReservedSpace;
2150 // Operand[0] = Value to switch on
2151 // Operand[1] = Default basic block destination
2152 // Operand[2n ] = Value to match
2153 // Operand[2n+1] = BasicBlock to go to on match
2154 SwitchInst(const SwitchInst &RI);
2155 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2156 void resizeOperands(unsigned No);
2157 // allocate space for exactly zero operands
2158 void *operator new(size_t s) {
2159 return User::operator new(s, 0);
2161 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2162 /// switch on and a default destination. The number of additional cases can
2163 /// be specified here to make memory allocation more efficient. This
2164 /// constructor can also autoinsert before another instruction.
2165 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2166 Instruction *InsertBefore = 0);
2168 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2169 /// switch on and a default destination. The number of additional cases can
2170 /// be specified here to make memory allocation more efficient. This
2171 /// constructor also autoinserts at the end of the specified BasicBlock.
2172 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2173 BasicBlock *InsertAtEnd);
2175 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2176 unsigned NumCases, Instruction *InsertBefore = 0) {
2177 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2179 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2180 unsigned NumCases, BasicBlock *InsertAtEnd) {
2181 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2185 /// Provide fast operand accessors
2186 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2188 // Accessor Methods for Switch stmt
2189 Value *getCondition() const { return getOperand(0); }
2190 void setCondition(Value *V) { setOperand(0, V); }
2192 BasicBlock *getDefaultDest() const {
2193 return cast<BasicBlock>(getOperand(1));
2196 /// getNumCases - return the number of 'cases' in this switch instruction.
2197 /// Note that case #0 is always the default case.
2198 unsigned getNumCases() const {
2199 return getNumOperands()/2;
2202 /// getCaseValue - Return the specified case value. Note that case #0, the
2203 /// default destination, does not have a case value.
2204 ConstantInt *getCaseValue(unsigned i) {
2205 assert(i && i < getNumCases() && "Illegal case value to get!");
2206 return getSuccessorValue(i);
2209 /// getCaseValue - Return the specified case value. Note that case #0, the
2210 /// default destination, does not have a case value.
2211 const ConstantInt *getCaseValue(unsigned i) const {
2212 assert(i && i < getNumCases() && "Illegal case value to get!");
2213 return getSuccessorValue(i);
2216 /// findCaseValue - Search all of the case values for the specified constant.
2217 /// If it is explicitly handled, return the case number of it, otherwise
2218 /// return 0 to indicate that it is handled by the default handler.
2219 unsigned findCaseValue(const ConstantInt *C) const {
2220 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2221 if (getCaseValue(i) == C)
2226 /// findCaseDest - Finds the unique case value for a given successor. Returns
2227 /// null if the successor is not found, not unique, or is the default case.
2228 ConstantInt *findCaseDest(BasicBlock *BB) {
2229 if (BB == getDefaultDest()) return NULL;
2231 ConstantInt *CI = NULL;
2232 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2233 if (getSuccessor(i) == BB) {
2234 if (CI) return NULL; // Multiple cases lead to BB.
2235 else CI = getCaseValue(i);
2241 /// addCase - Add an entry to the switch instruction...
2243 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2245 /// removeCase - This method removes the specified successor from the switch
2246 /// instruction. Note that this cannot be used to remove the default
2247 /// destination (successor #0).
2249 void removeCase(unsigned idx);
2251 virtual SwitchInst *clone(LLVMContext &Context) const;
2253 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2254 BasicBlock *getSuccessor(unsigned idx) const {
2255 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2256 return cast<BasicBlock>(getOperand(idx*2+1));
2258 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2259 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2260 setOperand(idx*2+1, NewSucc);
2263 // getSuccessorValue - Return the value associated with the specified
2265 ConstantInt *getSuccessorValue(unsigned idx) const {
2266 assert(idx < getNumSuccessors() && "Successor # out of range!");
2267 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2270 // Methods for support type inquiry through isa, cast, and dyn_cast:
2271 static inline bool classof(const SwitchInst *) { return true; }
2272 static inline bool classof(const Instruction *I) {
2273 return I->getOpcode() == Instruction::Switch;
2275 static inline bool classof(const Value *V) {
2276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2279 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2280 virtual unsigned getNumSuccessorsV() const;
2281 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2285 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2288 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2291 //===----------------------------------------------------------------------===//
2293 //===----------------------------------------------------------------------===//
2295 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2296 /// calling convention of the call.
2298 class InvokeInst : public TerminatorInst {
2299 AttrListPtr AttributeList;
2300 InvokeInst(const InvokeInst &BI);
2301 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2302 Value* const *Args, unsigned NumArgs);
2304 template<typename InputIterator>
2305 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2306 InputIterator ArgBegin, InputIterator ArgEnd,
2307 const std::string &NameStr,
2308 // This argument ensures that we have an iterator we can
2309 // do arithmetic on in constant time
2310 std::random_access_iterator_tag) {
2311 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2313 // This requires that the iterator points to contiguous memory.
2314 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2318 /// Construct an InvokeInst given a range of arguments.
2319 /// InputIterator must be a random-access iterator pointing to
2320 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2321 /// made for random-accessness but not for contiguous storage as
2322 /// that would incur runtime overhead.
2324 /// @brief Construct an InvokeInst from a range of arguments
2325 template<typename InputIterator>
2326 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2327 InputIterator ArgBegin, InputIterator ArgEnd,
2329 const std::string &NameStr, Instruction *InsertBefore);
2331 /// Construct an InvokeInst given a range of arguments.
2332 /// InputIterator must be a random-access iterator pointing to
2333 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2334 /// made for random-accessness but not for contiguous storage as
2335 /// that would incur runtime overhead.
2337 /// @brief Construct an InvokeInst from a range of arguments
2338 template<typename InputIterator>
2339 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2340 InputIterator ArgBegin, InputIterator ArgEnd,
2342 const std::string &NameStr, BasicBlock *InsertAtEnd);
2344 template<typename InputIterator>
2345 static InvokeInst *Create(Value *Func,
2346 BasicBlock *IfNormal, BasicBlock *IfException,
2347 InputIterator ArgBegin, InputIterator ArgEnd,
2348 const std::string &NameStr = "",
2349 Instruction *InsertBefore = 0) {
2350 unsigned Values(ArgEnd - ArgBegin + 3);
2351 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2352 Values, NameStr, InsertBefore);
2354 template<typename InputIterator>
2355 static InvokeInst *Create(Value *Func,
2356 BasicBlock *IfNormal, BasicBlock *IfException,
2357 InputIterator ArgBegin, InputIterator ArgEnd,
2358 const std::string &NameStr,
2359 BasicBlock *InsertAtEnd) {
2360 unsigned Values(ArgEnd - ArgBegin + 3);
2361 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2362 Values, NameStr, InsertAtEnd);
2365 virtual InvokeInst *clone(LLVMContext &Context) const;
2367 /// Provide fast operand accessors
2368 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2370 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2372 unsigned getCallingConv() const { return SubclassData; }
2373 void setCallingConv(unsigned CC) {
2377 /// getAttributes - Return the parameter attributes for this invoke.
2379 const AttrListPtr &getAttributes() const { return AttributeList; }
2381 /// setAttributes - Set the parameter attributes for this invoke.
2383 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2385 /// addAttribute - adds the attribute to the list of attributes.
2386 void addAttribute(unsigned i, Attributes attr);
2388 /// removeAttribute - removes the attribute from the list of attributes.
2389 void removeAttribute(unsigned i, Attributes attr);
2391 /// @brief Determine whether the call or the callee has the given attribute.
2392 bool paramHasAttr(unsigned i, Attributes attr) const;
2394 /// @brief Extract the alignment for a call or parameter (0=unknown).
2395 unsigned getParamAlignment(unsigned i) const {
2396 return AttributeList.getParamAlignment(i);
2399 /// @brief Determine if the call does not access memory.
2400 bool doesNotAccessMemory() const {
2401 return paramHasAttr(0, Attribute::ReadNone);
2403 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2404 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2405 else removeAttribute(~0, Attribute::ReadNone);
2408 /// @brief Determine if the call does not access or only reads memory.
2409 bool onlyReadsMemory() const {
2410 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2412 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2413 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2414 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2417 /// @brief Determine if the call cannot return.
2418 bool doesNotReturn() const {
2419 return paramHasAttr(~0, Attribute::NoReturn);
2421 void setDoesNotReturn(bool DoesNotReturn = true) {
2422 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2423 else removeAttribute(~0, Attribute::NoReturn);
2426 /// @brief Determine if the call cannot unwind.
2427 bool doesNotThrow() const {
2428 return paramHasAttr(~0, Attribute::NoUnwind);
2430 void setDoesNotThrow(bool DoesNotThrow = true) {
2431 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2432 else removeAttribute(~0, Attribute::NoUnwind);
2435 /// @brief Determine if the call returns a structure through first
2436 /// pointer argument.
2437 bool hasStructRetAttr() const {
2438 // Be friendly and also check the callee.
2439 return paramHasAttr(1, Attribute::StructRet);
2442 /// @brief Determine if any call argument is an aggregate passed by value.
2443 bool hasByValArgument() const {
2444 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2447 /// getCalledFunction - Return the function called, or null if this is an
2448 /// indirect function invocation.
2450 Function *getCalledFunction() const {
2451 return dyn_cast<Function>(getOperand(0));
2454 /// getCalledValue - Get a pointer to the function that is invoked by this
2456 const Value *getCalledValue() const { return getOperand(0); }
2457 Value *getCalledValue() { return getOperand(0); }
2459 // get*Dest - Return the destination basic blocks...
2460 BasicBlock *getNormalDest() const {
2461 return cast<BasicBlock>(getOperand(1));
2463 BasicBlock *getUnwindDest() const {
2464 return cast<BasicBlock>(getOperand(2));
2466 void setNormalDest(BasicBlock *B) {
2470 void setUnwindDest(BasicBlock *B) {
2474 BasicBlock *getSuccessor(unsigned i) const {
2475 assert(i < 2 && "Successor # out of range for invoke!");
2476 return i == 0 ? getNormalDest() : getUnwindDest();
2479 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2480 assert(idx < 2 && "Successor # out of range for invoke!");
2481 setOperand(idx+1, NewSucc);
2484 unsigned getNumSuccessors() const { return 2; }
2486 // Methods for support type inquiry through isa, cast, and dyn_cast:
2487 static inline bool classof(const InvokeInst *) { return true; }
2488 static inline bool classof(const Instruction *I) {
2489 return (I->getOpcode() == Instruction::Invoke);
2491 static inline bool classof(const Value *V) {
2492 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2495 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2496 virtual unsigned getNumSuccessorsV() const;
2497 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2501 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2504 template<typename InputIterator>
2505 InvokeInst::InvokeInst(Value *Func,
2506 BasicBlock *IfNormal, BasicBlock *IfException,
2507 InputIterator ArgBegin, InputIterator ArgEnd,
2509 const std::string &NameStr, Instruction *InsertBefore)
2510 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2511 ->getElementType())->getReturnType(),
2512 Instruction::Invoke,
2513 OperandTraits<InvokeInst>::op_end(this) - Values,
2514 Values, InsertBefore) {
2515 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2516 typename std::iterator_traits<InputIterator>::iterator_category());
2518 template<typename InputIterator>
2519 InvokeInst::InvokeInst(Value *Func,
2520 BasicBlock *IfNormal, BasicBlock *IfException,
2521 InputIterator ArgBegin, InputIterator ArgEnd,
2523 const std::string &NameStr, BasicBlock *InsertAtEnd)
2524 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2525 ->getElementType())->getReturnType(),
2526 Instruction::Invoke,
2527 OperandTraits<InvokeInst>::op_end(this) - Values,
2528 Values, InsertAtEnd) {
2529 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2530 typename std::iterator_traits<InputIterator>::iterator_category());
2533 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2535 //===----------------------------------------------------------------------===//
2537 //===----------------------------------------------------------------------===//
2539 //===---------------------------------------------------------------------------
2540 /// UnwindInst - Immediately exit the current function, unwinding the stack
2541 /// until an invoke instruction is found.
2543 class UnwindInst : public TerminatorInst {
2544 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2546 // allocate space for exactly zero operands
2547 void *operator new(size_t s) {
2548 return User::operator new(s, 0);
2550 explicit UnwindInst(Instruction *InsertBefore = 0);
2551 explicit UnwindInst(BasicBlock *InsertAtEnd);
2553 virtual UnwindInst *clone(LLVMContext &Context) const;
2555 unsigned getNumSuccessors() const { return 0; }
2557 // Methods for support type inquiry through isa, cast, and dyn_cast:
2558 static inline bool classof(const UnwindInst *) { return true; }
2559 static inline bool classof(const Instruction *I) {
2560 return I->getOpcode() == Instruction::Unwind;
2562 static inline bool classof(const Value *V) {
2563 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2566 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2567 virtual unsigned getNumSuccessorsV() const;
2568 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2571 //===----------------------------------------------------------------------===//
2572 // UnreachableInst Class
2573 //===----------------------------------------------------------------------===//
2575 //===---------------------------------------------------------------------------
2576 /// UnreachableInst - This function has undefined behavior. In particular, the
2577 /// presence of this instruction indicates some higher level knowledge that the
2578 /// end of the block cannot be reached.
2580 class UnreachableInst : public TerminatorInst {
2581 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2583 // allocate space for exactly zero operands
2584 void *operator new(size_t s) {
2585 return User::operator new(s, 0);
2587 explicit UnreachableInst(Instruction *InsertBefore = 0);
2588 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2590 virtual UnreachableInst *clone(LLVMContext &Context) const;
2592 unsigned getNumSuccessors() const { return 0; }
2594 // Methods for support type inquiry through isa, cast, and dyn_cast:
2595 static inline bool classof(const UnreachableInst *) { return true; }
2596 static inline bool classof(const Instruction *I) {
2597 return I->getOpcode() == Instruction::Unreachable;
2599 static inline bool classof(const Value *V) {
2600 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2603 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2604 virtual unsigned getNumSuccessorsV() const;
2605 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2608 //===----------------------------------------------------------------------===//
2610 //===----------------------------------------------------------------------===//
2612 /// @brief This class represents a truncation of integer types.
2613 class TruncInst : public CastInst {
2614 /// Private copy constructor
2615 TruncInst(const TruncInst &CI)
2616 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2619 /// @brief Constructor with insert-before-instruction semantics
2621 Value *S, ///< The value to be truncated
2622 const Type *Ty, ///< The (smaller) type to truncate to
2623 const std::string &NameStr = "", ///< A name for the new instruction
2624 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2627 /// @brief Constructor with insert-at-end-of-block semantics
2629 Value *S, ///< The value to be truncated
2630 const Type *Ty, ///< The (smaller) type to truncate to
2631 const std::string &NameStr, ///< A name for the new instruction
2632 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2635 /// @brief Clone an identical TruncInst
2636 virtual CastInst *clone(LLVMContext &Context) const;
2638 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2639 static inline bool classof(const TruncInst *) { return true; }
2640 static inline bool classof(const Instruction *I) {
2641 return I->getOpcode() == Trunc;
2643 static inline bool classof(const Value *V) {
2644 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2648 //===----------------------------------------------------------------------===//
2650 //===----------------------------------------------------------------------===//
2652 /// @brief This class represents zero extension of integer types.
2653 class ZExtInst : public CastInst {
2654 /// @brief Private copy constructor
2655 ZExtInst(const ZExtInst &CI)
2656 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2659 /// @brief Constructor with insert-before-instruction semantics
2661 Value *S, ///< The value to be zero extended
2662 const Type *Ty, ///< The type to zero extend to
2663 const std::string &NameStr = "", ///< A name for the new instruction
2664 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2667 /// @brief Constructor with insert-at-end semantics.
2669 Value *S, ///< The value to be zero extended
2670 const Type *Ty, ///< The type to zero extend to
2671 const std::string &NameStr, ///< A name for the new instruction
2672 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2675 /// @brief Clone an identical ZExtInst
2676 virtual CastInst *clone(LLVMContext &Context) const;
2678 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2679 static inline bool classof(const ZExtInst *) { return true; }
2680 static inline bool classof(const Instruction *I) {
2681 return I->getOpcode() == ZExt;
2683 static inline bool classof(const Value *V) {
2684 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2688 //===----------------------------------------------------------------------===//
2690 //===----------------------------------------------------------------------===//
2692 /// @brief This class represents a sign extension of integer types.
2693 class SExtInst : public CastInst {
2694 /// @brief Private copy constructor
2695 SExtInst(const SExtInst &CI)
2696 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2699 /// @brief Constructor with insert-before-instruction semantics
2701 Value *S, ///< The value to be sign extended
2702 const Type *Ty, ///< The type to sign extend to
2703 const std::string &NameStr = "", ///< A name for the new instruction
2704 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2707 /// @brief Constructor with insert-at-end-of-block semantics
2709 Value *S, ///< The value to be sign extended
2710 const Type *Ty, ///< The type to sign extend to
2711 const std::string &NameStr, ///< A name for the new instruction
2712 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2715 /// @brief Clone an identical SExtInst
2716 virtual CastInst *clone(LLVMContext &Context) const;
2718 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2719 static inline bool classof(const SExtInst *) { return true; }
2720 static inline bool classof(const Instruction *I) {
2721 return I->getOpcode() == SExt;
2723 static inline bool classof(const Value *V) {
2724 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2728 //===----------------------------------------------------------------------===//
2729 // FPTruncInst Class
2730 //===----------------------------------------------------------------------===//
2732 /// @brief This class represents a truncation of floating point types.
2733 class FPTruncInst : public CastInst {
2734 FPTruncInst(const FPTruncInst &CI)
2735 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2738 /// @brief Constructor with insert-before-instruction semantics
2740 Value *S, ///< The value to be truncated
2741 const Type *Ty, ///< The type to truncate to
2742 const std::string &NameStr = "", ///< A name for the new instruction
2743 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2746 /// @brief Constructor with insert-before-instruction semantics
2748 Value *S, ///< The value to be truncated
2749 const Type *Ty, ///< The type to truncate to
2750 const std::string &NameStr, ///< A name for the new instruction
2751 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2754 /// @brief Clone an identical FPTruncInst
2755 virtual CastInst *clone(LLVMContext &Context) const;
2757 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2758 static inline bool classof(const FPTruncInst *) { return true; }
2759 static inline bool classof(const Instruction *I) {
2760 return I->getOpcode() == FPTrunc;
2762 static inline bool classof(const Value *V) {
2763 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2767 //===----------------------------------------------------------------------===//
2769 //===----------------------------------------------------------------------===//
2771 /// @brief This class represents an extension of floating point types.
2772 class FPExtInst : public CastInst {
2773 FPExtInst(const FPExtInst &CI)
2774 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2777 /// @brief Constructor with insert-before-instruction semantics
2779 Value *S, ///< The value to be extended
2780 const Type *Ty, ///< The type to extend to
2781 const std::string &NameStr = "", ///< A name for the new instruction
2782 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2785 /// @brief Constructor with insert-at-end-of-block semantics
2787 Value *S, ///< The value to be extended
2788 const Type *Ty, ///< The type to extend to
2789 const std::string &NameStr, ///< A name for the new instruction
2790 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2793 /// @brief Clone an identical FPExtInst
2794 virtual CastInst *clone(LLVMContext &Context) const;
2796 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2797 static inline bool classof(const FPExtInst *) { return true; }
2798 static inline bool classof(const Instruction *I) {
2799 return I->getOpcode() == FPExt;
2801 static inline bool classof(const Value *V) {
2802 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2806 //===----------------------------------------------------------------------===//
2808 //===----------------------------------------------------------------------===//
2810 /// @brief This class represents a cast unsigned integer to floating point.
2811 class UIToFPInst : public CastInst {
2812 UIToFPInst(const UIToFPInst &CI)
2813 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2816 /// @brief Constructor with insert-before-instruction semantics
2818 Value *S, ///< The value to be converted
2819 const Type *Ty, ///< The type to convert to
2820 const std::string &NameStr = "", ///< A name for the new instruction
2821 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2824 /// @brief Constructor with insert-at-end-of-block semantics
2826 Value *S, ///< The value to be converted
2827 const Type *Ty, ///< The type to convert to
2828 const std::string &NameStr, ///< A name for the new instruction
2829 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2832 /// @brief Clone an identical UIToFPInst
2833 virtual CastInst *clone(LLVMContext &Context) const;
2835 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2836 static inline bool classof(const UIToFPInst *) { return true; }
2837 static inline bool classof(const Instruction *I) {
2838 return I->getOpcode() == UIToFP;
2840 static inline bool classof(const Value *V) {
2841 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2845 //===----------------------------------------------------------------------===//
2847 //===----------------------------------------------------------------------===//
2849 /// @brief This class represents a cast from signed integer to floating point.
2850 class SIToFPInst : public CastInst {
2851 SIToFPInst(const SIToFPInst &CI)
2852 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2855 /// @brief Constructor with insert-before-instruction semantics
2857 Value *S, ///< The value to be converted
2858 const Type *Ty, ///< The type to convert to
2859 const std::string &NameStr = "", ///< A name for the new instruction
2860 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2863 /// @brief Constructor with insert-at-end-of-block semantics
2865 Value *S, ///< The value to be converted
2866 const Type *Ty, ///< The type to convert to
2867 const std::string &NameStr, ///< A name for the new instruction
2868 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2871 /// @brief Clone an identical SIToFPInst
2872 virtual CastInst *clone(LLVMContext &Context) const;
2874 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2875 static inline bool classof(const SIToFPInst *) { return true; }
2876 static inline bool classof(const Instruction *I) {
2877 return I->getOpcode() == SIToFP;
2879 static inline bool classof(const Value *V) {
2880 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2884 //===----------------------------------------------------------------------===//
2886 //===----------------------------------------------------------------------===//
2888 /// @brief This class represents a cast from floating point to unsigned integer
2889 class FPToUIInst : public CastInst {
2890 FPToUIInst(const FPToUIInst &CI)
2891 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2894 /// @brief Constructor with insert-before-instruction semantics
2896 Value *S, ///< The value to be converted
2897 const Type *Ty, ///< The type to convert to
2898 const std::string &NameStr = "", ///< A name for the new instruction
2899 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2902 /// @brief Constructor with insert-at-end-of-block semantics
2904 Value *S, ///< The value to be converted
2905 const Type *Ty, ///< The type to convert to
2906 const std::string &NameStr, ///< A name for the new instruction
2907 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2910 /// @brief Clone an identical FPToUIInst
2911 virtual CastInst *clone(LLVMContext &Context) const;
2913 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2914 static inline bool classof(const FPToUIInst *) { return true; }
2915 static inline bool classof(const Instruction *I) {
2916 return I->getOpcode() == FPToUI;
2918 static inline bool classof(const Value *V) {
2919 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2923 //===----------------------------------------------------------------------===//
2925 //===----------------------------------------------------------------------===//
2927 /// @brief This class represents a cast from floating point to signed integer.
2928 class FPToSIInst : public CastInst {
2929 FPToSIInst(const FPToSIInst &CI)
2930 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2933 /// @brief Constructor with insert-before-instruction semantics
2935 Value *S, ///< The value to be converted
2936 const Type *Ty, ///< The type to convert to
2937 const std::string &NameStr = "", ///< A name for the new instruction
2938 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2941 /// @brief Constructor with insert-at-end-of-block semantics
2943 Value *S, ///< The value to be converted
2944 const Type *Ty, ///< The type to convert to
2945 const std::string &NameStr, ///< A name for the new instruction
2946 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2949 /// @brief Clone an identical FPToSIInst
2950 virtual CastInst *clone(LLVMContext &Context) const;
2952 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2953 static inline bool classof(const FPToSIInst *) { return true; }
2954 static inline bool classof(const Instruction *I) {
2955 return I->getOpcode() == FPToSI;
2957 static inline bool classof(const Value *V) {
2958 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2962 //===----------------------------------------------------------------------===//
2963 // IntToPtrInst Class
2964 //===----------------------------------------------------------------------===//
2966 /// @brief This class represents a cast from an integer to a pointer.
2967 class IntToPtrInst : public CastInst {
2968 IntToPtrInst(const IntToPtrInst &CI)
2969 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2972 /// @brief Constructor with insert-before-instruction semantics
2974 Value *S, ///< The value to be converted
2975 const Type *Ty, ///< The type to convert to
2976 const std::string &NameStr = "", ///< A name for the new instruction
2977 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2980 /// @brief Constructor with insert-at-end-of-block semantics
2982 Value *S, ///< The value to be converted
2983 const Type *Ty, ///< The type to convert to
2984 const std::string &NameStr, ///< A name for the new instruction
2985 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2988 /// @brief Clone an identical IntToPtrInst
2989 virtual CastInst *clone(LLVMContext &Context) const;
2991 // Methods for support type inquiry through isa, cast, and dyn_cast:
2992 static inline bool classof(const IntToPtrInst *) { return true; }
2993 static inline bool classof(const Instruction *I) {
2994 return I->getOpcode() == IntToPtr;
2996 static inline bool classof(const Value *V) {
2997 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3001 //===----------------------------------------------------------------------===//
3002 // PtrToIntInst Class
3003 //===----------------------------------------------------------------------===//
3005 /// @brief This class represents a cast from a pointer to an integer
3006 class PtrToIntInst : public CastInst {
3007 PtrToIntInst(const PtrToIntInst &CI)
3008 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3011 /// @brief Constructor with insert-before-instruction semantics
3013 Value *S, ///< The value to be converted
3014 const Type *Ty, ///< The type to convert to
3015 const std::string &NameStr = "", ///< A name for the new instruction
3016 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3019 /// @brief Constructor with insert-at-end-of-block semantics
3021 Value *S, ///< The value to be converted
3022 const Type *Ty, ///< The type to convert to
3023 const std::string &NameStr, ///< A name for the new instruction
3024 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3027 /// @brief Clone an identical PtrToIntInst
3028 virtual CastInst *clone(LLVMContext &Context) const;
3030 // Methods for support type inquiry through isa, cast, and dyn_cast:
3031 static inline bool classof(const PtrToIntInst *) { return true; }
3032 static inline bool classof(const Instruction *I) {
3033 return I->getOpcode() == PtrToInt;
3035 static inline bool classof(const Value *V) {
3036 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3040 //===----------------------------------------------------------------------===//
3041 // BitCastInst Class
3042 //===----------------------------------------------------------------------===//
3044 /// @brief This class represents a no-op cast from one type to another.
3045 class BitCastInst : public CastInst {
3046 BitCastInst(const BitCastInst &CI)
3047 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3050 /// @brief Constructor with insert-before-instruction semantics
3052 Value *S, ///< The value to be casted
3053 const Type *Ty, ///< The type to casted to
3054 const std::string &NameStr = "", ///< A name for the new instruction
3055 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3058 /// @brief Constructor with insert-at-end-of-block semantics
3060 Value *S, ///< The value to be casted
3061 const Type *Ty, ///< The type to casted to
3062 const std::string &NameStr, ///< A name for the new instruction
3063 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3066 /// @brief Clone an identical BitCastInst
3067 virtual CastInst *clone(LLVMContext &Context) const;
3069 // Methods for support type inquiry through isa, cast, and dyn_cast:
3070 static inline bool classof(const BitCastInst *) { return true; }
3071 static inline bool classof(const Instruction *I) {
3072 return I->getOpcode() == BitCast;
3074 static inline bool classof(const Value *V) {
3075 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3079 } // End llvm namespace