1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/ADT/SmallVector.h"
34 //===----------------------------------------------------------------------===//
35 // AllocationInst Class
36 //===----------------------------------------------------------------------===//
38 /// AllocationInst - This class is the common base class of MallocInst and
41 class AllocationInst : public UnaryInstruction {
43 AllocationInst(const Type *Ty, Value *ArraySize,
44 unsigned iTy, unsigned Align, const Twine &Name = "",
45 Instruction *InsertBefore = 0);
46 AllocationInst(const Type *Ty, Value *ArraySize,
47 unsigned iTy, unsigned Align, const Twine &Name,
48 BasicBlock *InsertAtEnd);
50 // Out of line virtual method, so the vtable, etc. has a home.
51 virtual ~AllocationInst();
53 /// isArrayAllocation - Return true if there is an allocation size parameter
54 /// to the allocation instruction that is not 1.
56 bool isArrayAllocation() const;
58 /// getArraySize - Get the number of elements allocated. For a simple
59 /// allocation of a single element, this will return a constant 1 value.
61 const Value *getArraySize() const { return getOperand(0); }
62 Value *getArraySize() { return getOperand(0); }
64 /// getType - Overload to return most specific pointer type
66 const PointerType *getType() const {
67 return reinterpret_cast<const PointerType*>(Instruction::getType());
70 /// getAllocatedType - Return the type that is being allocated by the
73 const Type *getAllocatedType() const;
75 /// getAlignment - Return the alignment of the memory that is being allocated
76 /// by the instruction.
78 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
79 void setAlignment(unsigned Align);
81 virtual Instruction *clone(LLVMContext &Context) const = 0;
83 // Methods for support type inquiry through isa, cast, and dyn_cast:
84 static inline bool classof(const AllocationInst *) { return true; }
85 static inline bool classof(const Instruction *I) {
86 return I->getOpcode() == Instruction::Alloca ||
87 I->getOpcode() == Instruction::Malloc;
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) && classof(cast<Instruction>(V));
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 /// MallocInst - an instruction to allocated memory on the heap
101 class MallocInst : public AllocationInst {
102 MallocInst(const MallocInst &MI);
104 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
105 const Twine &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Malloc,
108 0, NameStr, InsertBefore) {}
109 MallocInst(const Type *Ty, Value *ArraySize,
110 const Twine &NameStr, BasicBlock *InsertAtEnd)
111 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
113 MallocInst(const Type *Ty, const Twine &NameStr,
114 Instruction *InsertBefore = 0)
115 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
116 MallocInst(const Type *Ty, const Twine &NameStr,
117 BasicBlock *InsertAtEnd)
118 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
120 MallocInst(const Type *Ty, Value *ArraySize,
121 unsigned Align, const Twine &NameStr,
122 BasicBlock *InsertAtEnd)
123 : AllocationInst(Ty, ArraySize, Malloc,
124 Align, NameStr, InsertAtEnd) {}
125 MallocInst(const Type *Ty, Value *ArraySize,
126 unsigned Align, const Twine &NameStr = "",
127 Instruction *InsertBefore = 0)
128 : AllocationInst(Ty, ArraySize,
129 Malloc, Align, NameStr, InsertBefore) {}
131 virtual MallocInst *clone(LLVMContext &Context) const;
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const MallocInst *) { return true; }
135 static inline bool classof(const Instruction *I) {
136 return (I->getOpcode() == Instruction::Malloc);
138 static inline bool classof(const Value *V) {
139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
144 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
148 /// AllocaInst - an instruction to allocate memory on the stack
150 class AllocaInst : public AllocationInst {
151 AllocaInst(const AllocaInst &);
153 explicit AllocaInst(const Type *Ty,
154 Value *ArraySize = 0,
155 const Twine &NameStr = "",
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, ArraySize, Alloca,
158 0, NameStr, InsertBefore) {}
159 AllocaInst(const Type *Ty,
160 Value *ArraySize, const Twine &NameStr,
161 BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, const Twine &NameStr,
165 Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167 AllocaInst(const Type *Ty, const Twine &NameStr,
168 BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
171 AllocaInst(const Type *Ty, Value *ArraySize,
172 unsigned Align, const Twine &NameStr = "",
173 Instruction *InsertBefore = 0)
174 : AllocationInst(Ty, ArraySize, Alloca,
175 Align, NameStr, InsertBefore) {}
176 AllocaInst(const Type *Ty, Value *ArraySize,
177 unsigned Align, const Twine &NameStr,
178 BasicBlock *InsertAtEnd)
179 : AllocationInst(Ty, ArraySize, Alloca,
180 Align, NameStr, InsertAtEnd) {}
182 virtual AllocaInst *clone(LLVMContext &Context) const;
184 /// isStaticAlloca - Return true if this alloca is in the entry block of the
185 /// function and is a constant size. If so, the code generator will fold it
186 /// into the prolog/epilog code, so it is basically free.
187 bool isStaticAlloca() const;
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const AllocaInst *) { return true; }
191 static inline bool classof(const Instruction *I) {
192 return (I->getOpcode() == Instruction::Alloca);
194 static inline bool classof(const Value *V) {
195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
200 //===----------------------------------------------------------------------===//
202 //===----------------------------------------------------------------------===//
204 /// FreeInst - an instruction to deallocate memory
206 class FreeInst : public UnaryInstruction {
209 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
210 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
212 virtual FreeInst *clone(LLVMContext &Context) const;
214 // Accessor methods for consistency with other memory operations
215 Value *getPointerOperand() { return getOperand(0); }
216 const Value *getPointerOperand() const { return getOperand(0); }
218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const FreeInst *) { return true; }
220 static inline bool classof(const Instruction *I) {
221 return (I->getOpcode() == Instruction::Free);
223 static inline bool classof(const Value *V) {
224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
229 //===----------------------------------------------------------------------===//
231 //===----------------------------------------------------------------------===//
233 /// LoadInst - an instruction for reading from memory. This uses the
234 /// SubclassData field in Value to store whether or not the load is volatile.
236 class LoadInst : public UnaryInstruction {
238 LoadInst(const LoadInst &LI)
239 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
240 setVolatile(LI.isVolatile());
241 setAlignment(LI.getAlignment());
249 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
250 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
251 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
252 Instruction *InsertBefore = 0);
253 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
254 unsigned Align, Instruction *InsertBefore = 0);
255 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
256 BasicBlock *InsertAtEnd);
257 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
258 unsigned Align, BasicBlock *InsertAtEnd);
260 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 Twine &NameStr);
403 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
405 template<typename InputIterator>
406 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
407 const Twine &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 Twine &NameStr,
455 Instruction *InsertBefore);
456 template<typename InputIterator>
457 inline GetElementPtrInst(Value *Ptr,
458 InputIterator IdxBegin, InputIterator IdxEnd,
460 const Twine &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 Twine &NameStr = "",
465 Instruction *InsertBefore = 0);
466 GetElementPtrInst(Value *Ptr, Value *Idx,
467 const Twine &NameStr, BasicBlock *InsertAtEnd);
469 template<typename InputIterator>
470 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
471 InputIterator IdxEnd,
472 const Twine &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 Twine &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 Twine &NameStr = "",
494 Instruction *InsertBefore = 0) {
495 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
497 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
498 const Twine &NameStr,
499 BasicBlock *InsertAtEnd) {
500 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
503 /// Create an "inbounds" getelementptr. See the documentation for the
504 /// "inbounds" flag in LangRef.html for details.
505 template<typename InputIterator>
506 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
507 InputIterator IdxEnd,
508 const Twine &NameStr = "",
509 Instruction *InsertBefore = 0) {
510 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
511 NameStr, InsertBefore);
512 cast<GEPOperator>(GEP)->setIsInBounds(true);
515 template<typename InputIterator>
516 static GetElementPtrInst *CreateInBounds(Value *Ptr,
517 InputIterator IdxBegin,
518 InputIterator IdxEnd,
519 const Twine &NameStr,
520 BasicBlock *InsertAtEnd) {
521 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
522 NameStr, InsertAtEnd);
523 cast<GEPOperator>(GEP)->setIsInBounds(true);
526 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
527 const Twine &NameStr = "",
528 Instruction *InsertBefore = 0) {
529 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
530 cast<GEPOperator>(GEP)->setIsInBounds(true);
533 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
534 const Twine &NameStr,
535 BasicBlock *InsertAtEnd) {
536 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
537 cast<GEPOperator>(GEP)->setIsInBounds(true);
541 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
543 /// Transparently provide more efficient getOperand methods.
544 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
546 // getType - Overload to return most specific pointer type...
547 const PointerType *getType() const {
548 return reinterpret_cast<const PointerType*>(Instruction::getType());
551 /// getIndexedType - Returns the type of the element that would be loaded with
552 /// a load instruction with the specified parameters.
554 /// Null is returned if the indices are invalid for the specified
557 template<typename InputIterator>
558 static const Type *getIndexedType(const Type *Ptr,
559 InputIterator IdxBegin,
560 InputIterator IdxEnd) {
561 return getIndexedType(Ptr, IdxBegin, IdxEnd,
562 typename std::iterator_traits<InputIterator>::
563 iterator_category());
566 static const Type *getIndexedType(const Type *Ptr,
567 Value* const *Idx, unsigned NumIdx);
569 static const Type *getIndexedType(const Type *Ptr,
570 uint64_t const *Idx, unsigned NumIdx);
572 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
574 inline op_iterator idx_begin() { return op_begin()+1; }
575 inline const_op_iterator idx_begin() const { return op_begin()+1; }
576 inline op_iterator idx_end() { return op_end(); }
577 inline const_op_iterator idx_end() const { return op_end(); }
579 Value *getPointerOperand() {
580 return getOperand(0);
582 const Value *getPointerOperand() const {
583 return getOperand(0);
585 static unsigned getPointerOperandIndex() {
586 return 0U; // get index for modifying correct operand
589 /// getPointerOperandType - Method to return the pointer operand as a
591 const PointerType *getPointerOperandType() const {
592 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
596 unsigned getNumIndices() const { // Note: always non-negative
597 return getNumOperands() - 1;
600 bool hasIndices() const {
601 return getNumOperands() > 1;
604 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
605 /// zeros. If so, the result pointer and the first operand have the same
606 /// value, just potentially different types.
607 bool hasAllZeroIndices() const;
609 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
610 /// constant integers. If so, the result pointer and the first operand have
611 /// a constant offset between them.
612 bool hasAllConstantIndices() const;
614 // Methods for support type inquiry through isa, cast, and dyn_cast:
615 static inline bool classof(const GetElementPtrInst *) { return true; }
616 static inline bool classof(const Instruction *I) {
617 return (I->getOpcode() == Instruction::GetElementPtr);
619 static inline bool classof(const Value *V) {
620 return isa<Instruction>(V) && classof(cast<Instruction>(V));
625 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
628 template<typename InputIterator>
629 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
630 InputIterator IdxBegin,
631 InputIterator IdxEnd,
633 const Twine &NameStr,
634 Instruction *InsertBefore)
635 : Instruction(PointerType::get(checkType(
636 getIndexedType(Ptr->getType(),
638 cast<PointerType>(Ptr->getType())
639 ->getAddressSpace()),
641 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
642 Values, InsertBefore) {
643 init(Ptr, IdxBegin, IdxEnd, NameStr,
644 typename std::iterator_traits<InputIterator>::iterator_category());
646 template<typename InputIterator>
647 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
648 InputIterator IdxBegin,
649 InputIterator IdxEnd,
651 const Twine &NameStr,
652 BasicBlock *InsertAtEnd)
653 : Instruction(PointerType::get(checkType(
654 getIndexedType(Ptr->getType(),
656 cast<PointerType>(Ptr->getType())
657 ->getAddressSpace()),
659 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
660 Values, InsertAtEnd) {
661 init(Ptr, IdxBegin, IdxEnd, NameStr,
662 typename std::iterator_traits<InputIterator>::iterator_category());
666 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
669 //===----------------------------------------------------------------------===//
671 //===----------------------------------------------------------------------===//
673 /// This instruction compares its operands according to the predicate given
674 /// to the constructor. It only operates on integers or pointers. The operands
675 /// must be identical types.
676 /// @brief Represent an integer comparison operator.
677 class ICmpInst: public CmpInst {
679 /// @brief Constructor with insert-before-instruction semantics.
681 Instruction *InsertBefore, ///< Where to insert
682 Predicate pred, ///< The predicate to use for the comparison
683 Value *LHS, ///< The left-hand-side of the expression
684 Value *RHS, ///< The right-hand-side of the expression
685 const Twine &NameStr = "" ///< Name of the instruction
686 ) : CmpInst(makeCmpResultType(LHS->getType()),
687 Instruction::ICmp, pred, LHS, RHS, NameStr,
689 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
690 pred <= CmpInst::LAST_ICMP_PREDICATE &&
691 "Invalid ICmp predicate value");
692 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
693 "Both operands to ICmp instruction are not of the same type!");
694 // Check that the operands are the right type
695 assert((getOperand(0)->getType()->isIntOrIntVector() ||
696 isa<PointerType>(getOperand(0)->getType())) &&
697 "Invalid operand types for ICmp instruction");
700 /// @brief Constructor with insert-at-end semantics.
702 BasicBlock &InsertAtEnd, ///< Block to insert into.
703 Predicate pred, ///< The predicate to use for the comparison
704 Value *LHS, ///< The left-hand-side of the expression
705 Value *RHS, ///< The right-hand-side of the expression
706 const Twine &NameStr = "" ///< Name of the instruction
707 ) : CmpInst(makeCmpResultType(LHS->getType()),
708 Instruction::ICmp, pred, LHS, RHS, NameStr,
710 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
711 pred <= CmpInst::LAST_ICMP_PREDICATE &&
712 "Invalid ICmp predicate value");
713 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
714 "Both operands to ICmp instruction are not of the same type!");
715 // Check that the operands are the right type
716 assert((getOperand(0)->getType()->isIntOrIntVector() ||
717 isa<PointerType>(getOperand(0)->getType())) &&
718 "Invalid operand types for ICmp instruction");
721 /// @brief Constructor with no-insertion semantics
723 LLVMContext &Context, ///< Context to construct within
724 Predicate pred, ///< The predicate to use for the comparison
725 Value *LHS, ///< The left-hand-side of the expression
726 Value *RHS, ///< The right-hand-side of the expression
727 const Twine &NameStr = "" ///< Name of the instruction
728 ) : CmpInst(makeCmpResultType(LHS->getType()),
729 Instruction::ICmp, pred, LHS, RHS, NameStr) {
730 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
731 pred <= CmpInst::LAST_ICMP_PREDICATE &&
732 "Invalid ICmp predicate value");
733 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
734 "Both operands to ICmp instruction are not of the same type!");
735 // Check that the operands are the right type
736 assert((getOperand(0)->getType()->isIntOrIntVector() ||
737 isa<PointerType>(getOperand(0)->getType())) &&
738 "Invalid operand types for ICmp instruction");
741 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
742 /// @returns the predicate that would be the result if the operand were
743 /// regarded as signed.
744 /// @brief Return the signed version of the predicate
745 Predicate getSignedPredicate() const {
746 return getSignedPredicate(getPredicate());
749 /// This is a static version that you can use without an instruction.
750 /// @brief Return the signed version of the predicate.
751 static Predicate getSignedPredicate(Predicate pred);
753 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
754 /// @returns the predicate that would be the result if the operand were
755 /// regarded as unsigned.
756 /// @brief Return the unsigned version of the predicate
757 Predicate getUnsignedPredicate() const {
758 return getUnsignedPredicate(getPredicate());
761 /// This is a static version that you can use without an instruction.
762 /// @brief Return the unsigned version of the predicate.
763 static Predicate getUnsignedPredicate(Predicate pred);
765 /// isEquality - Return true if this predicate is either EQ or NE. This also
766 /// tests for commutativity.
767 static bool isEquality(Predicate P) {
768 return P == ICMP_EQ || P == ICMP_NE;
771 /// isEquality - Return true if this predicate is either EQ or NE. This also
772 /// tests for commutativity.
773 bool isEquality() const {
774 return isEquality(getPredicate());
777 /// @returns true if the predicate of this ICmpInst is commutative
778 /// @brief Determine if this relation is commutative.
779 bool isCommutative() const { return isEquality(); }
781 /// isRelational - Return true if the predicate is relational (not EQ or NE).
783 bool isRelational() const {
784 return !isEquality();
787 /// isRelational - Return true if the predicate is relational (not EQ or NE).
789 static bool isRelational(Predicate P) {
790 return !isEquality(P);
793 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
794 /// @brief Determine if this instruction's predicate is signed.
795 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
797 /// @returns true if the predicate provided is signed, false otherwise
798 /// @brief Determine if the predicate is signed.
799 static bool isSignedPredicate(Predicate pred);
801 /// @returns true if the specified compare predicate is
802 /// true when both operands are equal...
803 /// @brief Determine if the icmp is true when both operands are equal
804 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
805 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
806 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
807 pred == ICmpInst::ICMP_SLE;
810 /// @returns true if the specified compare instruction is
811 /// true when both operands are equal...
812 /// @brief Determine if the ICmpInst returns true when both operands are equal
813 bool isTrueWhenEqual() {
814 return isTrueWhenEqual(getPredicate());
817 /// Initialize a set of values that all satisfy the predicate with C.
818 /// @brief Make a ConstantRange for a relation with a constant value.
819 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
821 /// Exchange the two operands to this instruction in such a way that it does
822 /// not modify the semantics of the instruction. The predicate value may be
823 /// changed to retain the same result if the predicate is order dependent
825 /// @brief Swap operands and adjust predicate.
826 void swapOperands() {
827 SubclassData = getSwappedPredicate();
828 Op<0>().swap(Op<1>());
831 virtual ICmpInst *clone(LLVMContext &Context) const;
833 // Methods for support type inquiry through isa, cast, and dyn_cast:
834 static inline bool classof(const ICmpInst *) { return true; }
835 static inline bool classof(const Instruction *I) {
836 return I->getOpcode() == Instruction::ICmp;
838 static inline bool classof(const Value *V) {
839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
844 //===----------------------------------------------------------------------===//
846 //===----------------------------------------------------------------------===//
848 /// This instruction compares its operands according to the predicate given
849 /// to the constructor. It only operates on floating point values or packed
850 /// vectors of floating point values. The operands must be identical types.
851 /// @brief Represents a floating point comparison operator.
852 class FCmpInst: public CmpInst {
854 /// @brief Constructor with insert-before-instruction semantics.
856 Instruction *InsertBefore, ///< Where to insert
857 Predicate pred, ///< The predicate to use for the comparison
858 Value *LHS, ///< The left-hand-side of the expression
859 Value *RHS, ///< The right-hand-side of the expression
860 const Twine &NameStr = "" ///< Name of the instruction
861 ) : CmpInst(makeCmpResultType(LHS->getType()),
862 Instruction::FCmp, pred, LHS, RHS, NameStr,
864 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
865 "Invalid FCmp predicate value");
866 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
867 "Both operands to FCmp instruction are not of the same type!");
868 // Check that the operands are the right type
869 assert(getOperand(0)->getType()->isFPOrFPVector() &&
870 "Invalid operand types for FCmp instruction");
873 /// @brief Constructor with insert-at-end semantics.
875 BasicBlock &InsertAtEnd, ///< Block to insert into.
876 Predicate pred, ///< The predicate to use for the comparison
877 Value *LHS, ///< The left-hand-side of the expression
878 Value *RHS, ///< The right-hand-side of the expression
879 const Twine &NameStr = "" ///< Name of the instruction
880 ) : CmpInst(makeCmpResultType(LHS->getType()),
881 Instruction::FCmp, pred, LHS, RHS, NameStr,
883 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
884 "Invalid FCmp predicate value");
885 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
886 "Both operands to FCmp instruction are not of the same type!");
887 // Check that the operands are the right type
888 assert(getOperand(0)->getType()->isFPOrFPVector() &&
889 "Invalid operand types for FCmp instruction");
892 /// @brief Constructor with no-insertion semantics
894 LLVMContext &Context, ///< Context to build in
895 Predicate pred, ///< The predicate to use for the comparison
896 Value *LHS, ///< The left-hand-side of the expression
897 Value *RHS, ///< The right-hand-side of the expression
898 const Twine &NameStr = "" ///< Name of the instruction
899 ) : CmpInst(makeCmpResultType(LHS->getType()),
900 Instruction::FCmp, pred, LHS, RHS, NameStr) {
901 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
902 "Invalid FCmp predicate value");
903 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
904 "Both operands to FCmp instruction are not of the same type!");
905 // Check that the operands are the right type
906 assert(getOperand(0)->getType()->isFPOrFPVector() &&
907 "Invalid operand types for FCmp instruction");
910 /// @returns true if the predicate of this instruction is EQ or NE.
911 /// @brief Determine if this is an equality predicate.
912 bool isEquality() const {
913 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
914 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
917 /// @returns true if the predicate of this instruction is commutative.
918 /// @brief Determine if this is a commutative predicate.
919 bool isCommutative() const {
920 return isEquality() ||
921 SubclassData == FCMP_FALSE ||
922 SubclassData == FCMP_TRUE ||
923 SubclassData == FCMP_ORD ||
924 SubclassData == FCMP_UNO;
927 /// @returns true if the predicate is relational (not EQ or NE).
928 /// @brief Determine if this a relational predicate.
929 bool isRelational() const { return !isEquality(); }
931 /// Exchange the two operands to this instruction in such a way that it does
932 /// not modify the semantics of the instruction. The predicate value may be
933 /// changed to retain the same result if the predicate is order dependent
935 /// @brief Swap operands and adjust predicate.
936 void swapOperands() {
937 SubclassData = getSwappedPredicate();
938 Op<0>().swap(Op<1>());
941 virtual FCmpInst *clone(LLVMContext &Context) const;
943 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
944 static inline bool classof(const FCmpInst *) { return true; }
945 static inline bool classof(const Instruction *I) {
946 return I->getOpcode() == Instruction::FCmp;
948 static inline bool classof(const Value *V) {
949 return isa<Instruction>(V) && classof(cast<Instruction>(V));
953 //===----------------------------------------------------------------------===//
955 //===----------------------------------------------------------------------===//
956 /// CallInst - This class represents a function call, abstracting a target
957 /// machine's calling convention. This class uses low bit of the SubClassData
958 /// field to indicate whether or not this is a tail call. The rest of the bits
959 /// hold the calling convention of the call.
962 class CallInst : public Instruction {
963 AttrListPtr AttributeList; ///< parameter attributes for call
964 CallInst(const CallInst &CI);
965 void init(Value *Func, Value* const *Params, unsigned NumParams);
966 void init(Value *Func, Value *Actual1, Value *Actual2);
967 void init(Value *Func, Value *Actual);
968 void init(Value *Func);
970 template<typename InputIterator>
971 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
972 const Twine &NameStr,
973 // This argument ensures that we have an iterator we can
974 // do arithmetic on in constant time
975 std::random_access_iterator_tag) {
976 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
978 // This requires that the iterator points to contiguous memory.
979 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
983 /// Construct a CallInst given a range of arguments. InputIterator
984 /// must be a random-access iterator pointing to contiguous storage
985 /// (e.g. a std::vector<>::iterator). Checks are made for
986 /// random-accessness but not for contiguous storage as that would
987 /// incur runtime overhead.
988 /// @brief Construct a CallInst from a range of arguments
989 template<typename InputIterator>
990 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
991 const Twine &NameStr, Instruction *InsertBefore);
993 /// Construct a CallInst given a range of arguments. InputIterator
994 /// must be a random-access iterator pointing to contiguous storage
995 /// (e.g. a std::vector<>::iterator). Checks are made for
996 /// random-accessness but not for contiguous storage as that would
997 /// incur runtime overhead.
998 /// @brief Construct a CallInst from a range of arguments
999 template<typename InputIterator>
1000 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1001 const Twine &NameStr, BasicBlock *InsertAtEnd);
1003 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1004 Instruction *InsertBefore);
1005 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1006 BasicBlock *InsertAtEnd);
1007 explicit CallInst(Value *F, const Twine &NameStr,
1008 Instruction *InsertBefore);
1009 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1011 template<typename InputIterator>
1012 static CallInst *Create(Value *Func,
1013 InputIterator ArgBegin, InputIterator ArgEnd,
1014 const Twine &NameStr = "",
1015 Instruction *InsertBefore = 0) {
1016 return new((unsigned)(ArgEnd - ArgBegin + 1))
1017 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1019 template<typename InputIterator>
1020 static CallInst *Create(Value *Func,
1021 InputIterator ArgBegin, InputIterator ArgEnd,
1022 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1023 return new((unsigned)(ArgEnd - ArgBegin + 1))
1024 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1026 static CallInst *Create(Value *F, Value *Actual,
1027 const Twine &NameStr = "",
1028 Instruction *InsertBefore = 0) {
1029 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1031 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1032 BasicBlock *InsertAtEnd) {
1033 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1035 static CallInst *Create(Value *F, const Twine &NameStr = "",
1036 Instruction *InsertBefore = 0) {
1037 return new(1) CallInst(F, NameStr, InsertBefore);
1039 static CallInst *Create(Value *F, const Twine &NameStr,
1040 BasicBlock *InsertAtEnd) {
1041 return new(1) CallInst(F, NameStr, InsertAtEnd);
1046 bool isTailCall() const { return SubclassData & 1; }
1047 void setTailCall(bool isTC = true) {
1048 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1051 virtual CallInst *clone(LLVMContext &Context) const;
1053 /// Provide fast operand accessors
1054 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1056 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1058 unsigned getCallingConv() const { return SubclassData >> 1; }
1059 void setCallingConv(unsigned CC) {
1060 SubclassData = (SubclassData & 1) | (CC << 1);
1063 /// getAttributes - Return the parameter attributes for this call.
1065 const AttrListPtr &getAttributes() const { return AttributeList; }
1067 /// setAttributes - Set the parameter attributes for this call.
1069 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1071 /// addAttribute - adds the attribute to the list of attributes.
1072 void addAttribute(unsigned i, Attributes attr);
1074 /// removeAttribute - removes the attribute from the list of attributes.
1075 void removeAttribute(unsigned i, Attributes attr);
1077 /// @brief Determine whether the call or the callee has the given attribute.
1078 bool paramHasAttr(unsigned i, Attributes attr) const;
1080 /// @brief Extract the alignment for a call or parameter (0=unknown).
1081 unsigned getParamAlignment(unsigned i) const {
1082 return AttributeList.getParamAlignment(i);
1085 /// @brief Determine if the call does not access memory.
1086 bool doesNotAccessMemory() const {
1087 return paramHasAttr(~0, Attribute::ReadNone);
1089 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1090 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1091 else removeAttribute(~0, Attribute::ReadNone);
1094 /// @brief Determine if the call does not access or only reads memory.
1095 bool onlyReadsMemory() const {
1096 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1098 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1099 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1100 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1103 /// @brief Determine if the call cannot return.
1104 bool doesNotReturn() const {
1105 return paramHasAttr(~0, Attribute::NoReturn);
1107 void setDoesNotReturn(bool DoesNotReturn = true) {
1108 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1109 else removeAttribute(~0, Attribute::NoReturn);
1112 /// @brief Determine if the call cannot unwind.
1113 bool doesNotThrow() const {
1114 return paramHasAttr(~0, Attribute::NoUnwind);
1116 void setDoesNotThrow(bool DoesNotThrow = true) {
1117 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1118 else removeAttribute(~0, Attribute::NoUnwind);
1121 /// @brief Determine if the call returns a structure through first
1122 /// pointer argument.
1123 bool hasStructRetAttr() const {
1124 // Be friendly and also check the callee.
1125 return paramHasAttr(1, Attribute::StructRet);
1128 /// @brief Determine if any call argument is an aggregate passed by value.
1129 bool hasByValArgument() const {
1130 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1133 /// getCalledFunction - Return the function called, or null if this is an
1134 /// indirect function invocation.
1136 Function *getCalledFunction() const {
1137 return dyn_cast<Function>(Op<0>());
1140 /// getCalledValue - Get a pointer to the function that is invoked by this
1142 const Value *getCalledValue() const { return Op<0>(); }
1143 Value *getCalledValue() { return Op<0>(); }
1145 // Methods for support type inquiry through isa, cast, and dyn_cast:
1146 static inline bool classof(const CallInst *) { return true; }
1147 static inline bool classof(const Instruction *I) {
1148 return I->getOpcode() == Instruction::Call;
1150 static inline bool classof(const Value *V) {
1151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1156 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1159 template<typename InputIterator>
1160 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1161 const Twine &NameStr, BasicBlock *InsertAtEnd)
1162 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1163 ->getElementType())->getReturnType(),
1165 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1166 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1167 init(Func, ArgBegin, ArgEnd, NameStr,
1168 typename std::iterator_traits<InputIterator>::iterator_category());
1171 template<typename InputIterator>
1172 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1173 const Twine &NameStr, Instruction *InsertBefore)
1174 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1175 ->getElementType())->getReturnType(),
1177 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1178 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1179 init(Func, ArgBegin, ArgEnd, NameStr,
1180 typename std::iterator_traits<InputIterator>::iterator_category());
1183 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1185 //===----------------------------------------------------------------------===//
1187 //===----------------------------------------------------------------------===//
1189 /// SelectInst - This class represents the LLVM 'select' instruction.
1191 class SelectInst : public Instruction {
1192 void init(Value *C, Value *S1, Value *S2) {
1193 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1199 SelectInst(const SelectInst &SI)
1200 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1201 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1203 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1204 Instruction *InsertBefore)
1205 : Instruction(S1->getType(), Instruction::Select,
1206 &Op<0>(), 3, InsertBefore) {
1210 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1211 BasicBlock *InsertAtEnd)
1212 : Instruction(S1->getType(), Instruction::Select,
1213 &Op<0>(), 3, InsertAtEnd) {
1218 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1219 const Twine &NameStr = "",
1220 Instruction *InsertBefore = 0) {
1221 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1223 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1224 const Twine &NameStr,
1225 BasicBlock *InsertAtEnd) {
1226 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1229 Value *getCondition() const { return Op<0>(); }
1230 Value *getTrueValue() const { return Op<1>(); }
1231 Value *getFalseValue() const { return Op<2>(); }
1233 /// areInvalidOperands - Return a string if the specified operands are invalid
1234 /// for a select operation, otherwise return null.
1235 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1237 /// Transparently provide more efficient getOperand methods.
1238 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1240 OtherOps getOpcode() const {
1241 return static_cast<OtherOps>(Instruction::getOpcode());
1244 virtual SelectInst *clone(LLVMContext &Context) const;
1246 // Methods for support type inquiry through isa, cast, and dyn_cast:
1247 static inline bool classof(const SelectInst *) { return true; }
1248 static inline bool classof(const Instruction *I) {
1249 return I->getOpcode() == Instruction::Select;
1251 static inline bool classof(const Value *V) {
1252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1257 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1260 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1262 //===----------------------------------------------------------------------===//
1264 //===----------------------------------------------------------------------===//
1266 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1267 /// an argument of the specified type given a va_list and increments that list
1269 class VAArgInst : public UnaryInstruction {
1270 VAArgInst(const VAArgInst &VAA)
1271 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1273 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1274 Instruction *InsertBefore = 0)
1275 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1278 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1279 BasicBlock *InsertAtEnd)
1280 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1284 virtual VAArgInst *clone(LLVMContext &Context) const;
1286 // Methods for support type inquiry through isa, cast, and dyn_cast:
1287 static inline bool classof(const VAArgInst *) { return true; }
1288 static inline bool classof(const Instruction *I) {
1289 return I->getOpcode() == VAArg;
1291 static inline bool classof(const Value *V) {
1292 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1296 //===----------------------------------------------------------------------===//
1297 // ExtractElementInst Class
1298 //===----------------------------------------------------------------------===//
1300 /// ExtractElementInst - This instruction extracts a single (scalar)
1301 /// element from a VectorType value
1303 class ExtractElementInst : public Instruction {
1304 ExtractElementInst(const ExtractElementInst &EE) :
1305 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1306 Op<0>() = EE.Op<0>();
1307 Op<1>() = EE.Op<1>();
1310 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1311 Instruction *InsertBefore = 0);
1312 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1313 BasicBlock *InsertAtEnd);
1315 static ExtractElementInst *Create(const ExtractElementInst &EE) {
1316 return new(EE.getNumOperands()) ExtractElementInst(EE);
1319 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1320 const Twine &NameStr = "",
1321 Instruction *InsertBefore = 0) {
1322 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1324 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1325 const Twine &NameStr,
1326 BasicBlock *InsertAtEnd) {
1327 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1330 /// isValidOperands - Return true if an extractelement instruction can be
1331 /// formed with the specified operands.
1332 static bool isValidOperands(const Value *Vec, const Value *Idx);
1334 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1336 /// Transparently provide more efficient getOperand methods.
1337 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1339 // Methods for support type inquiry through isa, cast, and dyn_cast:
1340 static inline bool classof(const ExtractElementInst *) { return true; }
1341 static inline bool classof(const Instruction *I) {
1342 return I->getOpcode() == Instruction::ExtractElement;
1344 static inline bool classof(const Value *V) {
1345 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1350 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1353 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1355 //===----------------------------------------------------------------------===//
1356 // InsertElementInst Class
1357 //===----------------------------------------------------------------------===//
1359 /// InsertElementInst - This instruction inserts a single (scalar)
1360 /// element into a VectorType value
1362 class InsertElementInst : public Instruction {
1363 InsertElementInst(const InsertElementInst &IE);
1364 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1365 const Twine &NameStr = "",
1366 Instruction *InsertBefore = 0);
1367 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1368 const Twine &NameStr, BasicBlock *InsertAtEnd);
1370 static InsertElementInst *Create(const InsertElementInst &IE) {
1371 return new(IE.getNumOperands()) InsertElementInst(IE);
1373 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1374 const Twine &NameStr = "",
1375 Instruction *InsertBefore = 0) {
1376 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1378 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1379 const Twine &NameStr,
1380 BasicBlock *InsertAtEnd) {
1381 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1384 /// isValidOperands - Return true if an insertelement instruction can be
1385 /// formed with the specified operands.
1386 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1389 virtual InsertElementInst *clone(LLVMContext &Context) const;
1391 /// getType - Overload to return most specific vector type.
1393 const VectorType *getType() const {
1394 return reinterpret_cast<const VectorType*>(Instruction::getType());
1397 /// Transparently provide more efficient getOperand methods.
1398 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1400 // Methods for support type inquiry through isa, cast, and dyn_cast:
1401 static inline bool classof(const InsertElementInst *) { return true; }
1402 static inline bool classof(const Instruction *I) {
1403 return I->getOpcode() == Instruction::InsertElement;
1405 static inline bool classof(const Value *V) {
1406 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1411 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1414 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1416 //===----------------------------------------------------------------------===//
1417 // ShuffleVectorInst Class
1418 //===----------------------------------------------------------------------===//
1420 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1423 class ShuffleVectorInst : public Instruction {
1424 ShuffleVectorInst(const ShuffleVectorInst &IE);
1426 // allocate space for exactly three operands
1427 void *operator new(size_t s) {
1428 return User::operator new(s, 3);
1430 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1431 const Twine &NameStr = "",
1432 Instruction *InsertBefor = 0);
1433 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1434 const Twine &NameStr, BasicBlock *InsertAtEnd);
1436 /// isValidOperands - Return true if a shufflevector instruction can be
1437 /// formed with the specified operands.
1438 static bool isValidOperands(const Value *V1, const Value *V2,
1441 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1443 /// getType - Overload to return most specific vector type.
1445 const VectorType *getType() const {
1446 return reinterpret_cast<const VectorType*>(Instruction::getType());
1449 /// Transparently provide more efficient getOperand methods.
1450 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1452 /// getMaskValue - Return the index from the shuffle mask for the specified
1453 /// output result. This is either -1 if the element is undef or a number less
1454 /// than 2*numelements.
1455 int getMaskValue(unsigned i) const;
1457 // Methods for support type inquiry through isa, cast, and dyn_cast:
1458 static inline bool classof(const ShuffleVectorInst *) { return true; }
1459 static inline bool classof(const Instruction *I) {
1460 return I->getOpcode() == Instruction::ShuffleVector;
1462 static inline bool classof(const Value *V) {
1463 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1468 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1471 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1473 //===----------------------------------------------------------------------===//
1474 // ExtractValueInst Class
1475 //===----------------------------------------------------------------------===//
1477 /// ExtractValueInst - This instruction extracts a struct member or array
1478 /// element value from an aggregate value.
1480 class ExtractValueInst : public UnaryInstruction {
1481 SmallVector<unsigned, 4> Indices;
1483 ExtractValueInst(const ExtractValueInst &EVI);
1484 void init(const unsigned *Idx, unsigned NumIdx,
1485 const Twine &NameStr);
1486 void init(unsigned Idx, const Twine &NameStr);
1488 template<typename InputIterator>
1489 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1490 const Twine &NameStr,
1491 // This argument ensures that we have an iterator we can
1492 // do arithmetic on in constant time
1493 std::random_access_iterator_tag) {
1494 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1496 // There's no fundamental reason why we require at least one index
1497 // (other than weirdness with &*IdxBegin being invalid; see
1498 // getelementptr's init routine for example). But there's no
1499 // present need to support it.
1500 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1502 // This requires that the iterator points to contiguous memory.
1503 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1504 // we have to build an array here
1507 /// getIndexedType - Returns the type of the element that would be extracted
1508 /// with an extractvalue instruction with the specified parameters.
1510 /// Null is returned if the indices are invalid for the specified
1513 static const Type *getIndexedType(const Type *Agg,
1514 const unsigned *Idx, unsigned NumIdx);
1516 template<typename InputIterator>
1517 static const Type *getIndexedType(const Type *Ptr,
1518 InputIterator IdxBegin,
1519 InputIterator IdxEnd,
1520 // This argument ensures that we
1521 // have an iterator we can do
1522 // arithmetic on in constant time
1523 std::random_access_iterator_tag) {
1524 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1527 // This requires that the iterator points to contiguous memory.
1528 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1530 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1533 /// Constructors - Create a extractvalue instruction with a base aggregate
1534 /// value and a list of indices. The first ctor can optionally insert before
1535 /// an existing instruction, the second appends the new instruction to the
1536 /// specified BasicBlock.
1537 template<typename InputIterator>
1538 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1539 InputIterator IdxEnd,
1540 const Twine &NameStr,
1541 Instruction *InsertBefore);
1542 template<typename InputIterator>
1543 inline ExtractValueInst(Value *Agg,
1544 InputIterator IdxBegin, InputIterator IdxEnd,
1545 const Twine &NameStr, BasicBlock *InsertAtEnd);
1547 // allocate space for exactly one operand
1548 void *operator new(size_t s) {
1549 return User::operator new(s, 1);
1553 template<typename InputIterator>
1554 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1555 InputIterator IdxEnd,
1556 const Twine &NameStr = "",
1557 Instruction *InsertBefore = 0) {
1559 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1561 template<typename InputIterator>
1562 static ExtractValueInst *Create(Value *Agg,
1563 InputIterator IdxBegin, InputIterator IdxEnd,
1564 const Twine &NameStr,
1565 BasicBlock *InsertAtEnd) {
1566 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1569 /// Constructors - These two creators are convenience methods because one
1570 /// index extractvalue instructions are much more common than those with
1572 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1573 const Twine &NameStr = "",
1574 Instruction *InsertBefore = 0) {
1575 unsigned Idxs[1] = { Idx };
1576 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1578 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1579 const Twine &NameStr,
1580 BasicBlock *InsertAtEnd) {
1581 unsigned Idxs[1] = { Idx };
1582 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1585 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1587 /// getIndexedType - Returns the type of the element that would be extracted
1588 /// with an extractvalue instruction with the specified parameters.
1590 /// Null is returned if the indices are invalid for the specified
1593 template<typename InputIterator>
1594 static const Type *getIndexedType(const Type *Ptr,
1595 InputIterator IdxBegin,
1596 InputIterator IdxEnd) {
1597 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1598 typename std::iterator_traits<InputIterator>::
1599 iterator_category());
1601 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1603 typedef const unsigned* idx_iterator;
1604 inline idx_iterator idx_begin() const { return Indices.begin(); }
1605 inline idx_iterator idx_end() const { return Indices.end(); }
1607 Value *getAggregateOperand() {
1608 return getOperand(0);
1610 const Value *getAggregateOperand() const {
1611 return getOperand(0);
1613 static unsigned getAggregateOperandIndex() {
1614 return 0U; // get index for modifying correct operand
1617 unsigned getNumIndices() const { // Note: always non-negative
1618 return (unsigned)Indices.size();
1621 bool hasIndices() const {
1625 // Methods for support type inquiry through isa, cast, and dyn_cast:
1626 static inline bool classof(const ExtractValueInst *) { return true; }
1627 static inline bool classof(const Instruction *I) {
1628 return I->getOpcode() == Instruction::ExtractValue;
1630 static inline bool classof(const Value *V) {
1631 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1635 template<typename InputIterator>
1636 ExtractValueInst::ExtractValueInst(Value *Agg,
1637 InputIterator IdxBegin,
1638 InputIterator IdxEnd,
1639 const Twine &NameStr,
1640 Instruction *InsertBefore)
1641 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1643 ExtractValue, Agg, InsertBefore) {
1644 init(IdxBegin, IdxEnd, NameStr,
1645 typename std::iterator_traits<InputIterator>::iterator_category());
1647 template<typename InputIterator>
1648 ExtractValueInst::ExtractValueInst(Value *Agg,
1649 InputIterator IdxBegin,
1650 InputIterator IdxEnd,
1651 const Twine &NameStr,
1652 BasicBlock *InsertAtEnd)
1653 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1655 ExtractValue, Agg, InsertAtEnd) {
1656 init(IdxBegin, IdxEnd, NameStr,
1657 typename std::iterator_traits<InputIterator>::iterator_category());
1661 //===----------------------------------------------------------------------===//
1662 // InsertValueInst Class
1663 //===----------------------------------------------------------------------===//
1665 /// InsertValueInst - This instruction inserts a struct field of array element
1666 /// value into an aggregate value.
1668 class InsertValueInst : public Instruction {
1669 SmallVector<unsigned, 4> Indices;
1671 void *operator new(size_t, unsigned); // Do not implement
1672 InsertValueInst(const InsertValueInst &IVI);
1673 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1674 const Twine &NameStr);
1675 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1677 template<typename InputIterator>
1678 void init(Value *Agg, Value *Val,
1679 InputIterator IdxBegin, InputIterator IdxEnd,
1680 const Twine &NameStr,
1681 // This argument ensures that we have an iterator we can
1682 // do arithmetic on in constant time
1683 std::random_access_iterator_tag) {
1684 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1686 // There's no fundamental reason why we require at least one index
1687 // (other than weirdness with &*IdxBegin being invalid; see
1688 // getelementptr's init routine for example). But there's no
1689 // present need to support it.
1690 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1692 // This requires that the iterator points to contiguous memory.
1693 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1694 // we have to build an array here
1697 /// Constructors - Create a insertvalue instruction with a base aggregate
1698 /// value, a value to insert, and a list of indices. The first ctor can
1699 /// optionally insert before an existing instruction, the second appends
1700 /// the new instruction to the specified BasicBlock.
1701 template<typename InputIterator>
1702 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1703 InputIterator IdxEnd,
1704 const Twine &NameStr,
1705 Instruction *InsertBefore);
1706 template<typename InputIterator>
1707 inline InsertValueInst(Value *Agg, Value *Val,
1708 InputIterator IdxBegin, InputIterator IdxEnd,
1709 const Twine &NameStr, BasicBlock *InsertAtEnd);
1711 /// Constructors - These two constructors are convenience methods because one
1712 /// and two index insertvalue instructions are so common.
1713 InsertValueInst(Value *Agg, Value *Val,
1714 unsigned Idx, const Twine &NameStr = "",
1715 Instruction *InsertBefore = 0);
1716 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1717 const Twine &NameStr, BasicBlock *InsertAtEnd);
1719 // allocate space for exactly two operands
1720 void *operator new(size_t s) {
1721 return User::operator new(s, 2);
1724 template<typename InputIterator>
1725 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1726 InputIterator IdxEnd,
1727 const Twine &NameStr = "",
1728 Instruction *InsertBefore = 0) {
1729 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1730 NameStr, InsertBefore);
1732 template<typename InputIterator>
1733 static InsertValueInst *Create(Value *Agg, Value *Val,
1734 InputIterator IdxBegin, InputIterator IdxEnd,
1735 const Twine &NameStr,
1736 BasicBlock *InsertAtEnd) {
1737 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1738 NameStr, InsertAtEnd);
1741 /// Constructors - These two creators are convenience methods because one
1742 /// index insertvalue instructions are much more common than those with
1744 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1745 const Twine &NameStr = "",
1746 Instruction *InsertBefore = 0) {
1747 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1749 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1750 const Twine &NameStr,
1751 BasicBlock *InsertAtEnd) {
1752 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1755 virtual InsertValueInst *clone(LLVMContext &Context) const;
1757 /// Transparently provide more efficient getOperand methods.
1758 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1760 typedef const unsigned* idx_iterator;
1761 inline idx_iterator idx_begin() const { return Indices.begin(); }
1762 inline idx_iterator idx_end() const { return Indices.end(); }
1764 Value *getAggregateOperand() {
1765 return getOperand(0);
1767 const Value *getAggregateOperand() const {
1768 return getOperand(0);
1770 static unsigned getAggregateOperandIndex() {
1771 return 0U; // get index for modifying correct operand
1774 Value *getInsertedValueOperand() {
1775 return getOperand(1);
1777 const Value *getInsertedValueOperand() const {
1778 return getOperand(1);
1780 static unsigned getInsertedValueOperandIndex() {
1781 return 1U; // get index for modifying correct operand
1784 unsigned getNumIndices() const { // Note: always non-negative
1785 return (unsigned)Indices.size();
1788 bool hasIndices() const {
1792 // Methods for support type inquiry through isa, cast, and dyn_cast:
1793 static inline bool classof(const InsertValueInst *) { return true; }
1794 static inline bool classof(const Instruction *I) {
1795 return I->getOpcode() == Instruction::InsertValue;
1797 static inline bool classof(const Value *V) {
1798 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1803 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1806 template<typename InputIterator>
1807 InsertValueInst::InsertValueInst(Value *Agg,
1809 InputIterator IdxBegin,
1810 InputIterator IdxEnd,
1811 const Twine &NameStr,
1812 Instruction *InsertBefore)
1813 : Instruction(Agg->getType(), InsertValue,
1814 OperandTraits<InsertValueInst>::op_begin(this),
1816 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1817 typename std::iterator_traits<InputIterator>::iterator_category());
1819 template<typename InputIterator>
1820 InsertValueInst::InsertValueInst(Value *Agg,
1822 InputIterator IdxBegin,
1823 InputIterator IdxEnd,
1824 const Twine &NameStr,
1825 BasicBlock *InsertAtEnd)
1826 : Instruction(Agg->getType(), InsertValue,
1827 OperandTraits<InsertValueInst>::op_begin(this),
1829 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1830 typename std::iterator_traits<InputIterator>::iterator_category());
1833 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1835 //===----------------------------------------------------------------------===//
1837 //===----------------------------------------------------------------------===//
1839 // PHINode - The PHINode class is used to represent the magical mystical PHI
1840 // node, that can not exist in nature, but can be synthesized in a computer
1841 // scientist's overactive imagination.
1843 class PHINode : public Instruction {
1844 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1845 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1846 /// the number actually in use.
1847 unsigned ReservedSpace;
1848 PHINode(const PHINode &PN);
1849 // allocate space for exactly zero operands
1850 void *operator new(size_t s) {
1851 return User::operator new(s, 0);
1853 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1854 Instruction *InsertBefore = 0)
1855 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1860 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1861 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1866 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1867 Instruction *InsertBefore = 0) {
1868 return new PHINode(Ty, NameStr, InsertBefore);
1870 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1871 BasicBlock *InsertAtEnd) {
1872 return new PHINode(Ty, NameStr, InsertAtEnd);
1876 /// reserveOperandSpace - This method can be used to avoid repeated
1877 /// reallocation of PHI operand lists by reserving space for the correct
1878 /// number of operands before adding them. Unlike normal vector reserves,
1879 /// this method can also be used to trim the operand space.
1880 void reserveOperandSpace(unsigned NumValues) {
1881 resizeOperands(NumValues*2);
1884 virtual PHINode *clone(LLVMContext &Context) const;
1886 /// Provide fast operand accessors
1887 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1889 /// getNumIncomingValues - Return the number of incoming edges
1891 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1893 /// getIncomingValue - Return incoming value number x
1895 Value *getIncomingValue(unsigned i) const {
1896 assert(i*2 < getNumOperands() && "Invalid value number!");
1897 return getOperand(i*2);
1899 void setIncomingValue(unsigned i, Value *V) {
1900 assert(i*2 < getNumOperands() && "Invalid value number!");
1903 static unsigned getOperandNumForIncomingValue(unsigned i) {
1906 static unsigned getIncomingValueNumForOperand(unsigned i) {
1907 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1911 /// getIncomingBlock - Return incoming basic block corresponding
1912 /// to value use iterator
1914 template <typename U>
1915 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1916 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1917 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1919 /// getIncomingBlock - Return incoming basic block number x
1921 BasicBlock *getIncomingBlock(unsigned i) const {
1922 return static_cast<BasicBlock*>(getOperand(i*2+1));
1924 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1925 setOperand(i*2+1, BB);
1927 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1930 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1931 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1935 /// addIncoming - Add an incoming value to the end of the PHI list
1937 void addIncoming(Value *V, BasicBlock *BB) {
1938 assert(V && "PHI node got a null value!");
1939 assert(BB && "PHI node got a null basic block!");
1940 assert(getType() == V->getType() &&
1941 "All operands to PHI node must be the same type as the PHI node!");
1942 unsigned OpNo = NumOperands;
1943 if (OpNo+2 > ReservedSpace)
1944 resizeOperands(0); // Get more space!
1945 // Initialize some new operands.
1946 NumOperands = OpNo+2;
1947 OperandList[OpNo] = V;
1948 OperandList[OpNo+1] = BB;
1951 /// removeIncomingValue - Remove an incoming value. This is useful if a
1952 /// predecessor basic block is deleted. The value removed is returned.
1954 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1955 /// is true), the PHI node is destroyed and any uses of it are replaced with
1956 /// dummy values. The only time there should be zero incoming values to a PHI
1957 /// node is when the block is dead, so this strategy is sound.
1959 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1961 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1962 int Idx = getBasicBlockIndex(BB);
1963 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1964 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1967 /// getBasicBlockIndex - Return the first index of the specified basic
1968 /// block in the value list for this PHI. Returns -1 if no instance.
1970 int getBasicBlockIndex(const BasicBlock *BB) const {
1971 Use *OL = OperandList;
1972 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1973 if (OL[i+1].get() == BB) return i/2;
1977 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1978 return getIncomingValue(getBasicBlockIndex(BB));
1981 /// hasConstantValue - If the specified PHI node always merges together the
1982 /// same value, return the value, otherwise return null.
1984 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1986 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1987 static inline bool classof(const PHINode *) { return true; }
1988 static inline bool classof(const Instruction *I) {
1989 return I->getOpcode() == Instruction::PHI;
1991 static inline bool classof(const Value *V) {
1992 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1995 void resizeOperands(unsigned NumOperands);
1999 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
2002 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2005 //===----------------------------------------------------------------------===//
2007 //===----------------------------------------------------------------------===//
2009 //===---------------------------------------------------------------------------
2010 /// ReturnInst - Return a value (possibly void), from a function. Execution
2011 /// does not continue in this function any longer.
2013 class ReturnInst : public TerminatorInst {
2014 ReturnInst(const ReturnInst &RI);
2017 // ReturnInst constructors:
2018 // ReturnInst() - 'ret void' instruction
2019 // ReturnInst( null) - 'ret void' instruction
2020 // ReturnInst(Value* X) - 'ret X' instruction
2021 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2022 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2023 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2024 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2026 // NOTE: If the Value* passed is of type void then the constructor behaves as
2027 // if it was passed NULL.
2028 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2029 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2030 explicit ReturnInst(BasicBlock *InsertAtEnd);
2032 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2033 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2035 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2036 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2038 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2039 return new(0) ReturnInst(InsertAtEnd);
2041 virtual ~ReturnInst();
2043 virtual ReturnInst *clone(LLVMContext &Context) const;
2045 /// Provide fast operand accessors
2046 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2048 /// Convenience accessor
2049 Value *getReturnValue(unsigned n = 0) const {
2050 return n < getNumOperands()
2055 unsigned getNumSuccessors() const { return 0; }
2057 // Methods for support type inquiry through isa, cast, and dyn_cast:
2058 static inline bool classof(const ReturnInst *) { return true; }
2059 static inline bool classof(const Instruction *I) {
2060 return (I->getOpcode() == Instruction::Ret);
2062 static inline bool classof(const Value *V) {
2063 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2066 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2067 virtual unsigned getNumSuccessorsV() const;
2068 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2072 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2075 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2077 //===----------------------------------------------------------------------===//
2079 //===----------------------------------------------------------------------===//
2081 //===---------------------------------------------------------------------------
2082 /// BranchInst - Conditional or Unconditional Branch instruction.
2084 class BranchInst : public TerminatorInst {
2085 /// Ops list - Branches are strange. The operands are ordered:
2086 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2087 /// they don't have to check for cond/uncond branchness. These are mostly
2088 /// accessed relative from op_end().
2089 BranchInst(const BranchInst &BI);
2091 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2092 // BranchInst(BB *B) - 'br B'
2093 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2094 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2095 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2096 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2097 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2098 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2099 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2100 Instruction *InsertBefore = 0);
2101 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2102 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2103 BasicBlock *InsertAtEnd);
2105 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2106 return new(1, true) BranchInst(IfTrue, InsertBefore);
2108 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2109 Value *Cond, Instruction *InsertBefore = 0) {
2110 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2112 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2113 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2115 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2116 Value *Cond, BasicBlock *InsertAtEnd) {
2117 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2122 /// Transparently provide more efficient getOperand methods.
2123 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2125 virtual BranchInst *clone(LLVMContext &Context) const;
2127 bool isUnconditional() const { return getNumOperands() == 1; }
2128 bool isConditional() const { return getNumOperands() == 3; }
2130 Value *getCondition() const {
2131 assert(isConditional() && "Cannot get condition of an uncond branch!");
2135 void setCondition(Value *V) {
2136 assert(isConditional() && "Cannot set condition of unconditional branch!");
2140 // setUnconditionalDest - Change the current branch to an unconditional branch
2141 // targeting the specified block.
2142 // FIXME: Eliminate this ugly method.
2143 void setUnconditionalDest(BasicBlock *Dest) {
2145 if (isConditional()) { // Convert this to an uncond branch.
2149 OperandList = op_begin();
2153 unsigned getNumSuccessors() const { return 1+isConditional(); }
2155 BasicBlock *getSuccessor(unsigned i) const {
2156 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2157 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2160 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2161 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2162 *(&Op<-1>() - idx) = NewSucc;
2165 // Methods for support type inquiry through isa, cast, and dyn_cast:
2166 static inline bool classof(const BranchInst *) { return true; }
2167 static inline bool classof(const Instruction *I) {
2168 return (I->getOpcode() == Instruction::Br);
2170 static inline bool classof(const Value *V) {
2171 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2174 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2175 virtual unsigned getNumSuccessorsV() const;
2176 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2180 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2182 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2184 //===----------------------------------------------------------------------===//
2186 //===----------------------------------------------------------------------===//
2188 //===---------------------------------------------------------------------------
2189 /// SwitchInst - Multiway switch
2191 class SwitchInst : public TerminatorInst {
2192 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2193 unsigned ReservedSpace;
2194 // Operand[0] = Value to switch on
2195 // Operand[1] = Default basic block destination
2196 // Operand[2n ] = Value to match
2197 // Operand[2n+1] = BasicBlock to go to on match
2198 SwitchInst(const SwitchInst &RI);
2199 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2200 void resizeOperands(unsigned No);
2201 // allocate space for exactly zero operands
2202 void *operator new(size_t s) {
2203 return User::operator new(s, 0);
2205 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2206 /// switch on and a default destination. The number of additional cases can
2207 /// be specified here to make memory allocation more efficient. This
2208 /// constructor can also autoinsert before another instruction.
2209 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2210 Instruction *InsertBefore = 0);
2212 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2213 /// switch on and a default destination. The number of additional cases can
2214 /// be specified here to make memory allocation more efficient. This
2215 /// constructor also autoinserts at the end of the specified BasicBlock.
2216 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2217 BasicBlock *InsertAtEnd);
2219 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2220 unsigned NumCases, Instruction *InsertBefore = 0) {
2221 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2223 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2224 unsigned NumCases, BasicBlock *InsertAtEnd) {
2225 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2229 /// Provide fast operand accessors
2230 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2232 // Accessor Methods for Switch stmt
2233 Value *getCondition() const { return getOperand(0); }
2234 void setCondition(Value *V) { setOperand(0, V); }
2236 BasicBlock *getDefaultDest() const {
2237 return cast<BasicBlock>(getOperand(1));
2240 /// getNumCases - return the number of 'cases' in this switch instruction.
2241 /// Note that case #0 is always the default case.
2242 unsigned getNumCases() const {
2243 return getNumOperands()/2;
2246 /// getCaseValue - Return the specified case value. Note that case #0, the
2247 /// default destination, does not have a case value.
2248 ConstantInt *getCaseValue(unsigned i) {
2249 assert(i && i < getNumCases() && "Illegal case value to get!");
2250 return getSuccessorValue(i);
2253 /// getCaseValue - Return the specified case value. Note that case #0, the
2254 /// default destination, does not have a case value.
2255 const ConstantInt *getCaseValue(unsigned i) const {
2256 assert(i && i < getNumCases() && "Illegal case value to get!");
2257 return getSuccessorValue(i);
2260 /// findCaseValue - Search all of the case values for the specified constant.
2261 /// If it is explicitly handled, return the case number of it, otherwise
2262 /// return 0 to indicate that it is handled by the default handler.
2263 unsigned findCaseValue(const ConstantInt *C) const {
2264 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2265 if (getCaseValue(i) == C)
2270 /// findCaseDest - Finds the unique case value for a given successor. Returns
2271 /// null if the successor is not found, not unique, or is the default case.
2272 ConstantInt *findCaseDest(BasicBlock *BB) {
2273 if (BB == getDefaultDest()) return NULL;
2275 ConstantInt *CI = NULL;
2276 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2277 if (getSuccessor(i) == BB) {
2278 if (CI) return NULL; // Multiple cases lead to BB.
2279 else CI = getCaseValue(i);
2285 /// addCase - Add an entry to the switch instruction...
2287 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2289 /// removeCase - This method removes the specified successor from the switch
2290 /// instruction. Note that this cannot be used to remove the default
2291 /// destination (successor #0).
2293 void removeCase(unsigned idx);
2295 virtual SwitchInst *clone(LLVMContext &Context) const;
2297 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2298 BasicBlock *getSuccessor(unsigned idx) const {
2299 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2300 return cast<BasicBlock>(getOperand(idx*2+1));
2302 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2303 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2304 setOperand(idx*2+1, NewSucc);
2307 // getSuccessorValue - Return the value associated with the specified
2309 ConstantInt *getSuccessorValue(unsigned idx) const {
2310 assert(idx < getNumSuccessors() && "Successor # out of range!");
2311 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2314 // Methods for support type inquiry through isa, cast, and dyn_cast:
2315 static inline bool classof(const SwitchInst *) { return true; }
2316 static inline bool classof(const Instruction *I) {
2317 return I->getOpcode() == Instruction::Switch;
2319 static inline bool classof(const Value *V) {
2320 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2323 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2324 virtual unsigned getNumSuccessorsV() const;
2325 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2329 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2332 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2335 //===----------------------------------------------------------------------===//
2337 //===----------------------------------------------------------------------===//
2339 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2340 /// calling convention of the call.
2342 class InvokeInst : public TerminatorInst {
2343 AttrListPtr AttributeList;
2344 InvokeInst(const InvokeInst &BI);
2345 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2346 Value* const *Args, unsigned NumArgs);
2348 template<typename InputIterator>
2349 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2350 InputIterator ArgBegin, InputIterator ArgEnd,
2351 const Twine &NameStr,
2352 // This argument ensures that we have an iterator we can
2353 // do arithmetic on in constant time
2354 std::random_access_iterator_tag) {
2355 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2357 // This requires that the iterator points to contiguous memory.
2358 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2362 /// Construct an InvokeInst given a range of arguments.
2363 /// InputIterator must be a random-access iterator pointing to
2364 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2365 /// made for random-accessness but not for contiguous storage as
2366 /// that would incur runtime overhead.
2368 /// @brief Construct an InvokeInst from a range of arguments
2369 template<typename InputIterator>
2370 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2371 InputIterator ArgBegin, InputIterator ArgEnd,
2373 const Twine &NameStr, Instruction *InsertBefore);
2375 /// Construct an InvokeInst given a range of arguments.
2376 /// InputIterator must be a random-access iterator pointing to
2377 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2378 /// made for random-accessness but not for contiguous storage as
2379 /// that would incur runtime overhead.
2381 /// @brief Construct an InvokeInst from a range of arguments
2382 template<typename InputIterator>
2383 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2384 InputIterator ArgBegin, InputIterator ArgEnd,
2386 const Twine &NameStr, BasicBlock *InsertAtEnd);
2388 template<typename InputIterator>
2389 static InvokeInst *Create(Value *Func,
2390 BasicBlock *IfNormal, BasicBlock *IfException,
2391 InputIterator ArgBegin, InputIterator ArgEnd,
2392 const Twine &NameStr = "",
2393 Instruction *InsertBefore = 0) {
2394 unsigned Values(ArgEnd - ArgBegin + 3);
2395 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2396 Values, NameStr, InsertBefore);
2398 template<typename InputIterator>
2399 static InvokeInst *Create(Value *Func,
2400 BasicBlock *IfNormal, BasicBlock *IfException,
2401 InputIterator ArgBegin, InputIterator ArgEnd,
2402 const Twine &NameStr,
2403 BasicBlock *InsertAtEnd) {
2404 unsigned Values(ArgEnd - ArgBegin + 3);
2405 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2406 Values, NameStr, InsertAtEnd);
2409 virtual InvokeInst *clone(LLVMContext &Context) const;
2411 /// Provide fast operand accessors
2412 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2414 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2416 unsigned getCallingConv() const { return SubclassData; }
2417 void setCallingConv(unsigned CC) {
2421 /// getAttributes - Return the parameter attributes for this invoke.
2423 const AttrListPtr &getAttributes() const { return AttributeList; }
2425 /// setAttributes - Set the parameter attributes for this invoke.
2427 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2429 /// addAttribute - adds the attribute to the list of attributes.
2430 void addAttribute(unsigned i, Attributes attr);
2432 /// removeAttribute - removes the attribute from the list of attributes.
2433 void removeAttribute(unsigned i, Attributes attr);
2435 /// @brief Determine whether the call or the callee has the given attribute.
2436 bool paramHasAttr(unsigned i, Attributes attr) const;
2438 /// @brief Extract the alignment for a call or parameter (0=unknown).
2439 unsigned getParamAlignment(unsigned i) const {
2440 return AttributeList.getParamAlignment(i);
2443 /// @brief Determine if the call does not access memory.
2444 bool doesNotAccessMemory() const {
2445 return paramHasAttr(~0, Attribute::ReadNone);
2447 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2448 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2449 else removeAttribute(~0, Attribute::ReadNone);
2452 /// @brief Determine if the call does not access or only reads memory.
2453 bool onlyReadsMemory() const {
2454 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2456 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2457 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2458 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2461 /// @brief Determine if the call cannot return.
2462 bool doesNotReturn() const {
2463 return paramHasAttr(~0, Attribute::NoReturn);
2465 void setDoesNotReturn(bool DoesNotReturn = true) {
2466 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2467 else removeAttribute(~0, Attribute::NoReturn);
2470 /// @brief Determine if the call cannot unwind.
2471 bool doesNotThrow() const {
2472 return paramHasAttr(~0, Attribute::NoUnwind);
2474 void setDoesNotThrow(bool DoesNotThrow = true) {
2475 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2476 else removeAttribute(~0, Attribute::NoUnwind);
2479 /// @brief Determine if the call returns a structure through first
2480 /// pointer argument.
2481 bool hasStructRetAttr() const {
2482 // Be friendly and also check the callee.
2483 return paramHasAttr(1, Attribute::StructRet);
2486 /// @brief Determine if any call argument is an aggregate passed by value.
2487 bool hasByValArgument() const {
2488 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2491 /// getCalledFunction - Return the function called, or null if this is an
2492 /// indirect function invocation.
2494 Function *getCalledFunction() const {
2495 return dyn_cast<Function>(getOperand(0));
2498 /// getCalledValue - Get a pointer to the function that is invoked by this
2500 const Value *getCalledValue() const { return getOperand(0); }
2501 Value *getCalledValue() { return getOperand(0); }
2503 // get*Dest - Return the destination basic blocks...
2504 BasicBlock *getNormalDest() const {
2505 return cast<BasicBlock>(getOperand(1));
2507 BasicBlock *getUnwindDest() const {
2508 return cast<BasicBlock>(getOperand(2));
2510 void setNormalDest(BasicBlock *B) {
2514 void setUnwindDest(BasicBlock *B) {
2518 BasicBlock *getSuccessor(unsigned i) const {
2519 assert(i < 2 && "Successor # out of range for invoke!");
2520 return i == 0 ? getNormalDest() : getUnwindDest();
2523 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2524 assert(idx < 2 && "Successor # out of range for invoke!");
2525 setOperand(idx+1, NewSucc);
2528 unsigned getNumSuccessors() const { return 2; }
2530 // Methods for support type inquiry through isa, cast, and dyn_cast:
2531 static inline bool classof(const InvokeInst *) { return true; }
2532 static inline bool classof(const Instruction *I) {
2533 return (I->getOpcode() == Instruction::Invoke);
2535 static inline bool classof(const Value *V) {
2536 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2539 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2540 virtual unsigned getNumSuccessorsV() const;
2541 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2545 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2548 template<typename InputIterator>
2549 InvokeInst::InvokeInst(Value *Func,
2550 BasicBlock *IfNormal, BasicBlock *IfException,
2551 InputIterator ArgBegin, InputIterator ArgEnd,
2553 const Twine &NameStr, Instruction *InsertBefore)
2554 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2555 ->getElementType())->getReturnType(),
2556 Instruction::Invoke,
2557 OperandTraits<InvokeInst>::op_end(this) - Values,
2558 Values, InsertBefore) {
2559 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2560 typename std::iterator_traits<InputIterator>::iterator_category());
2562 template<typename InputIterator>
2563 InvokeInst::InvokeInst(Value *Func,
2564 BasicBlock *IfNormal, BasicBlock *IfException,
2565 InputIterator ArgBegin, InputIterator ArgEnd,
2567 const Twine &NameStr, BasicBlock *InsertAtEnd)
2568 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2569 ->getElementType())->getReturnType(),
2570 Instruction::Invoke,
2571 OperandTraits<InvokeInst>::op_end(this) - Values,
2572 Values, InsertAtEnd) {
2573 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2574 typename std::iterator_traits<InputIterator>::iterator_category());
2577 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2579 //===----------------------------------------------------------------------===//
2581 //===----------------------------------------------------------------------===//
2583 //===---------------------------------------------------------------------------
2584 /// UnwindInst - Immediately exit the current function, unwinding the stack
2585 /// until an invoke instruction is found.
2587 class UnwindInst : public TerminatorInst {
2588 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2590 // allocate space for exactly zero operands
2591 void *operator new(size_t s) {
2592 return User::operator new(s, 0);
2594 explicit UnwindInst(Instruction *InsertBefore = 0);
2595 explicit UnwindInst(BasicBlock *InsertAtEnd);
2597 virtual UnwindInst *clone(LLVMContext &Context) const;
2599 unsigned getNumSuccessors() const { return 0; }
2601 // Methods for support type inquiry through isa, cast, and dyn_cast:
2602 static inline bool classof(const UnwindInst *) { return true; }
2603 static inline bool classof(const Instruction *I) {
2604 return I->getOpcode() == Instruction::Unwind;
2606 static inline bool classof(const Value *V) {
2607 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2610 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2611 virtual unsigned getNumSuccessorsV() const;
2612 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2615 //===----------------------------------------------------------------------===//
2616 // UnreachableInst Class
2617 //===----------------------------------------------------------------------===//
2619 //===---------------------------------------------------------------------------
2620 /// UnreachableInst - This function has undefined behavior. In particular, the
2621 /// presence of this instruction indicates some higher level knowledge that the
2622 /// end of the block cannot be reached.
2624 class UnreachableInst : public TerminatorInst {
2625 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2627 // allocate space for exactly zero operands
2628 void *operator new(size_t s) {
2629 return User::operator new(s, 0);
2631 explicit UnreachableInst(Instruction *InsertBefore = 0);
2632 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2634 virtual UnreachableInst *clone(LLVMContext &Context) const;
2636 unsigned getNumSuccessors() const { return 0; }
2638 // Methods for support type inquiry through isa, cast, and dyn_cast:
2639 static inline bool classof(const UnreachableInst *) { return true; }
2640 static inline bool classof(const Instruction *I) {
2641 return I->getOpcode() == Instruction::Unreachable;
2643 static inline bool classof(const Value *V) {
2644 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2647 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2648 virtual unsigned getNumSuccessorsV() const;
2649 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2652 //===----------------------------------------------------------------------===//
2654 //===----------------------------------------------------------------------===//
2656 /// @brief This class represents a truncation of integer types.
2657 class TruncInst : public CastInst {
2658 /// Private copy constructor
2659 TruncInst(const TruncInst &CI)
2660 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2663 /// @brief Constructor with insert-before-instruction semantics
2665 Value *S, ///< The value to be truncated
2666 const Type *Ty, ///< The (smaller) type to truncate to
2667 const Twine &NameStr = "", ///< A name for the new instruction
2668 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2671 /// @brief Constructor with insert-at-end-of-block semantics
2673 Value *S, ///< The value to be truncated
2674 const Type *Ty, ///< The (smaller) type to truncate to
2675 const Twine &NameStr, ///< A name for the new instruction
2676 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2679 /// @brief Clone an identical TruncInst
2680 virtual CastInst *clone(LLVMContext &Context) const;
2682 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2683 static inline bool classof(const TruncInst *) { return true; }
2684 static inline bool classof(const Instruction *I) {
2685 return I->getOpcode() == Trunc;
2687 static inline bool classof(const Value *V) {
2688 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2692 //===----------------------------------------------------------------------===//
2694 //===----------------------------------------------------------------------===//
2696 /// @brief This class represents zero extension of integer types.
2697 class ZExtInst : public CastInst {
2698 /// @brief Private copy constructor
2699 ZExtInst(const ZExtInst &CI)
2700 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2703 /// @brief Constructor with insert-before-instruction semantics
2705 Value *S, ///< The value to be zero extended
2706 const Type *Ty, ///< The type to zero extend to
2707 const Twine &NameStr = "", ///< A name for the new instruction
2708 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2711 /// @brief Constructor with insert-at-end semantics.
2713 Value *S, ///< The value to be zero extended
2714 const Type *Ty, ///< The type to zero extend to
2715 const Twine &NameStr, ///< A name for the new instruction
2716 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2719 /// @brief Clone an identical ZExtInst
2720 virtual CastInst *clone(LLVMContext &Context) const;
2722 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2723 static inline bool classof(const ZExtInst *) { return true; }
2724 static inline bool classof(const Instruction *I) {
2725 return I->getOpcode() == ZExt;
2727 static inline bool classof(const Value *V) {
2728 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2732 //===----------------------------------------------------------------------===//
2734 //===----------------------------------------------------------------------===//
2736 /// @brief This class represents a sign extension of integer types.
2737 class SExtInst : public CastInst {
2738 /// @brief Private copy constructor
2739 SExtInst(const SExtInst &CI)
2740 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2743 /// @brief Constructor with insert-before-instruction semantics
2745 Value *S, ///< The value to be sign extended
2746 const Type *Ty, ///< The type to sign extend to
2747 const Twine &NameStr = "", ///< A name for the new instruction
2748 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2751 /// @brief Constructor with insert-at-end-of-block semantics
2753 Value *S, ///< The value to be sign extended
2754 const Type *Ty, ///< The type to sign extend to
2755 const Twine &NameStr, ///< A name for the new instruction
2756 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2759 /// @brief Clone an identical SExtInst
2760 virtual CastInst *clone(LLVMContext &Context) const;
2762 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2763 static inline bool classof(const SExtInst *) { return true; }
2764 static inline bool classof(const Instruction *I) {
2765 return I->getOpcode() == SExt;
2767 static inline bool classof(const Value *V) {
2768 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2772 //===----------------------------------------------------------------------===//
2773 // FPTruncInst Class
2774 //===----------------------------------------------------------------------===//
2776 /// @brief This class represents a truncation of floating point types.
2777 class FPTruncInst : public CastInst {
2778 FPTruncInst(const FPTruncInst &CI)
2779 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2782 /// @brief Constructor with insert-before-instruction semantics
2784 Value *S, ///< The value to be truncated
2785 const Type *Ty, ///< The type to truncate to
2786 const Twine &NameStr = "", ///< A name for the new instruction
2787 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2790 /// @brief Constructor with insert-before-instruction semantics
2792 Value *S, ///< The value to be truncated
2793 const Type *Ty, ///< The type to truncate to
2794 const Twine &NameStr, ///< A name for the new instruction
2795 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2798 /// @brief Clone an identical FPTruncInst
2799 virtual CastInst *clone(LLVMContext &Context) const;
2801 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2802 static inline bool classof(const FPTruncInst *) { return true; }
2803 static inline bool classof(const Instruction *I) {
2804 return I->getOpcode() == FPTrunc;
2806 static inline bool classof(const Value *V) {
2807 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2811 //===----------------------------------------------------------------------===//
2813 //===----------------------------------------------------------------------===//
2815 /// @brief This class represents an extension of floating point types.
2816 class FPExtInst : public CastInst {
2817 FPExtInst(const FPExtInst &CI)
2818 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2821 /// @brief Constructor with insert-before-instruction semantics
2823 Value *S, ///< The value to be extended
2824 const Type *Ty, ///< The type to extend to
2825 const Twine &NameStr = "", ///< A name for the new instruction
2826 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2829 /// @brief Constructor with insert-at-end-of-block semantics
2831 Value *S, ///< The value to be extended
2832 const Type *Ty, ///< The type to extend to
2833 const Twine &NameStr, ///< A name for the new instruction
2834 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2837 /// @brief Clone an identical FPExtInst
2838 virtual CastInst *clone(LLVMContext &Context) const;
2840 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2841 static inline bool classof(const FPExtInst *) { return true; }
2842 static inline bool classof(const Instruction *I) {
2843 return I->getOpcode() == FPExt;
2845 static inline bool classof(const Value *V) {
2846 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2850 //===----------------------------------------------------------------------===//
2852 //===----------------------------------------------------------------------===//
2854 /// @brief This class represents a cast unsigned integer to floating point.
2855 class UIToFPInst : public CastInst {
2856 UIToFPInst(const UIToFPInst &CI)
2857 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2860 /// @brief Constructor with insert-before-instruction semantics
2862 Value *S, ///< The value to be converted
2863 const Type *Ty, ///< The type to convert to
2864 const Twine &NameStr = "", ///< A name for the new instruction
2865 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2868 /// @brief Constructor with insert-at-end-of-block semantics
2870 Value *S, ///< The value to be converted
2871 const Type *Ty, ///< The type to convert to
2872 const Twine &NameStr, ///< A name for the new instruction
2873 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2876 /// @brief Clone an identical UIToFPInst
2877 virtual CastInst *clone(LLVMContext &Context) const;
2879 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2880 static inline bool classof(const UIToFPInst *) { return true; }
2881 static inline bool classof(const Instruction *I) {
2882 return I->getOpcode() == UIToFP;
2884 static inline bool classof(const Value *V) {
2885 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2889 //===----------------------------------------------------------------------===//
2891 //===----------------------------------------------------------------------===//
2893 /// @brief This class represents a cast from signed integer to floating point.
2894 class SIToFPInst : public CastInst {
2895 SIToFPInst(const SIToFPInst &CI)
2896 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2899 /// @brief Constructor with insert-before-instruction semantics
2901 Value *S, ///< The value to be converted
2902 const Type *Ty, ///< The type to convert to
2903 const Twine &NameStr = "", ///< A name for the new instruction
2904 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2907 /// @brief Constructor with insert-at-end-of-block semantics
2909 Value *S, ///< The value to be converted
2910 const Type *Ty, ///< The type to convert to
2911 const Twine &NameStr, ///< A name for the new instruction
2912 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2915 /// @brief Clone an identical SIToFPInst
2916 virtual CastInst *clone(LLVMContext &Context) const;
2918 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2919 static inline bool classof(const SIToFPInst *) { return true; }
2920 static inline bool classof(const Instruction *I) {
2921 return I->getOpcode() == SIToFP;
2923 static inline bool classof(const Value *V) {
2924 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2928 //===----------------------------------------------------------------------===//
2930 //===----------------------------------------------------------------------===//
2932 /// @brief This class represents a cast from floating point to unsigned integer
2933 class FPToUIInst : public CastInst {
2934 FPToUIInst(const FPToUIInst &CI)
2935 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2938 /// @brief Constructor with insert-before-instruction semantics
2940 Value *S, ///< The value to be converted
2941 const Type *Ty, ///< The type to convert to
2942 const Twine &NameStr = "", ///< A name for the new instruction
2943 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2946 /// @brief Constructor with insert-at-end-of-block semantics
2948 Value *S, ///< The value to be converted
2949 const Type *Ty, ///< The type to convert to
2950 const Twine &NameStr, ///< A name for the new instruction
2951 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2954 /// @brief Clone an identical FPToUIInst
2955 virtual CastInst *clone(LLVMContext &Context) const;
2957 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2958 static inline bool classof(const FPToUIInst *) { return true; }
2959 static inline bool classof(const Instruction *I) {
2960 return I->getOpcode() == FPToUI;
2962 static inline bool classof(const Value *V) {
2963 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2967 //===----------------------------------------------------------------------===//
2969 //===----------------------------------------------------------------------===//
2971 /// @brief This class represents a cast from floating point to signed integer.
2972 class FPToSIInst : public CastInst {
2973 FPToSIInst(const FPToSIInst &CI)
2974 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2977 /// @brief Constructor with insert-before-instruction semantics
2979 Value *S, ///< The value to be converted
2980 const Type *Ty, ///< The type to convert to
2981 const Twine &NameStr = "", ///< A name for the new instruction
2982 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2985 /// @brief Constructor with insert-at-end-of-block semantics
2987 Value *S, ///< The value to be converted
2988 const Type *Ty, ///< The type to convert to
2989 const Twine &NameStr, ///< A name for the new instruction
2990 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2993 /// @brief Clone an identical FPToSIInst
2994 virtual CastInst *clone(LLVMContext &Context) const;
2996 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2997 static inline bool classof(const FPToSIInst *) { return true; }
2998 static inline bool classof(const Instruction *I) {
2999 return I->getOpcode() == FPToSI;
3001 static inline bool classof(const Value *V) {
3002 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3006 //===----------------------------------------------------------------------===//
3007 // IntToPtrInst Class
3008 //===----------------------------------------------------------------------===//
3010 /// @brief This class represents a cast from an integer to a pointer.
3011 class IntToPtrInst : public CastInst {
3012 IntToPtrInst(const IntToPtrInst &CI)
3013 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3016 /// @brief Constructor with insert-before-instruction semantics
3018 Value *S, ///< The value to be converted
3019 const Type *Ty, ///< The type to convert to
3020 const Twine &NameStr = "", ///< A name for the new instruction
3021 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3024 /// @brief Constructor with insert-at-end-of-block semantics
3026 Value *S, ///< The value to be converted
3027 const Type *Ty, ///< The type to convert to
3028 const Twine &NameStr, ///< A name for the new instruction
3029 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3032 /// @brief Clone an identical IntToPtrInst
3033 virtual CastInst *clone(LLVMContext &Context) const;
3035 // Methods for support type inquiry through isa, cast, and dyn_cast:
3036 static inline bool classof(const IntToPtrInst *) { return true; }
3037 static inline bool classof(const Instruction *I) {
3038 return I->getOpcode() == IntToPtr;
3040 static inline bool classof(const Value *V) {
3041 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3045 //===----------------------------------------------------------------------===//
3046 // PtrToIntInst Class
3047 //===----------------------------------------------------------------------===//
3049 /// @brief This class represents a cast from a pointer to an integer
3050 class PtrToIntInst : public CastInst {
3051 PtrToIntInst(const PtrToIntInst &CI)
3052 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3055 /// @brief Constructor with insert-before-instruction semantics
3057 Value *S, ///< The value to be converted
3058 const Type *Ty, ///< The type to convert to
3059 const Twine &NameStr = "", ///< A name for the new instruction
3060 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3063 /// @brief Constructor with insert-at-end-of-block semantics
3065 Value *S, ///< The value to be converted
3066 const Type *Ty, ///< The type to convert to
3067 const Twine &NameStr, ///< A name for the new instruction
3068 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3071 /// @brief Clone an identical PtrToIntInst
3072 virtual CastInst *clone(LLVMContext &Context) const;
3074 // Methods for support type inquiry through isa, cast, and dyn_cast:
3075 static inline bool classof(const PtrToIntInst *) { return true; }
3076 static inline bool classof(const Instruction *I) {
3077 return I->getOpcode() == PtrToInt;
3079 static inline bool classof(const Value *V) {
3080 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3084 //===----------------------------------------------------------------------===//
3085 // BitCastInst Class
3086 //===----------------------------------------------------------------------===//
3088 /// @brief This class represents a no-op cast from one type to another.
3089 class BitCastInst : public CastInst {
3090 BitCastInst(const BitCastInst &CI)
3091 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3094 /// @brief Constructor with insert-before-instruction semantics
3096 Value *S, ///< The value to be casted
3097 const Type *Ty, ///< The type to casted to
3098 const Twine &NameStr = "", ///< A name for the new instruction
3099 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3102 /// @brief Constructor with insert-at-end-of-block semantics
3104 Value *S, ///< The value to be casted
3105 const Type *Ty, ///< The type to casted to
3106 const Twine &NameStr, ///< A name for the new instruction
3107 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3110 /// @brief Clone an identical BitCastInst
3111 virtual CastInst *clone(LLVMContext &Context) const;
3113 // Methods for support type inquiry through isa, cast, and dyn_cast:
3114 static inline bool classof(const BitCastInst *) { return true; }
3115 static inline bool classof(const Instruction *I) {
3116 return I->getOpcode() == BitCast;
3118 static inline bool classof(const Value *V) {
3119 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3123 } // End llvm namespace