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(LLVMContext &C, Value *retVal = 0,
2029 Instruction *InsertBefore = 0);
2030 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2031 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2033 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2034 Instruction *InsertBefore = 0) {
2035 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2037 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2038 BasicBlock *InsertAtEnd) {
2039 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2041 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2042 return new(0) ReturnInst(C, InsertAtEnd);
2044 virtual ~ReturnInst();
2046 virtual ReturnInst *clone(LLVMContext &Context) const;
2048 /// Provide fast operand accessors
2049 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2051 /// Convenience accessor
2052 Value *getReturnValue(unsigned n = 0) const {
2053 return n < getNumOperands()
2058 unsigned getNumSuccessors() const { return 0; }
2060 // Methods for support type inquiry through isa, cast, and dyn_cast:
2061 static inline bool classof(const ReturnInst *) { return true; }
2062 static inline bool classof(const Instruction *I) {
2063 return (I->getOpcode() == Instruction::Ret);
2065 static inline bool classof(const Value *V) {
2066 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2069 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2070 virtual unsigned getNumSuccessorsV() const;
2071 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2075 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2078 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2080 //===----------------------------------------------------------------------===//
2082 //===----------------------------------------------------------------------===//
2084 //===---------------------------------------------------------------------------
2085 /// BranchInst - Conditional or Unconditional Branch instruction.
2087 class BranchInst : public TerminatorInst {
2088 /// Ops list - Branches are strange. The operands are ordered:
2089 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2090 /// they don't have to check for cond/uncond branchness. These are mostly
2091 /// accessed relative from op_end().
2092 BranchInst(const BranchInst &BI);
2094 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2095 // BranchInst(BB *B) - 'br B'
2096 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2097 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2098 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2099 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2100 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2101 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2102 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2103 Instruction *InsertBefore = 0);
2104 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2105 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2106 BasicBlock *InsertAtEnd);
2108 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2109 return new(1, true) BranchInst(IfTrue, InsertBefore);
2111 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2112 Value *Cond, Instruction *InsertBefore = 0) {
2113 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2115 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2116 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2118 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2119 Value *Cond, BasicBlock *InsertAtEnd) {
2120 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2125 /// Transparently provide more efficient getOperand methods.
2126 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2128 virtual BranchInst *clone(LLVMContext &Context) const;
2130 bool isUnconditional() const { return getNumOperands() == 1; }
2131 bool isConditional() const { return getNumOperands() == 3; }
2133 Value *getCondition() const {
2134 assert(isConditional() && "Cannot get condition of an uncond branch!");
2138 void setCondition(Value *V) {
2139 assert(isConditional() && "Cannot set condition of unconditional branch!");
2143 // setUnconditionalDest - Change the current branch to an unconditional branch
2144 // targeting the specified block.
2145 // FIXME: Eliminate this ugly method.
2146 void setUnconditionalDest(BasicBlock *Dest) {
2148 if (isConditional()) { // Convert this to an uncond branch.
2152 OperandList = op_begin();
2156 unsigned getNumSuccessors() const { return 1+isConditional(); }
2158 BasicBlock *getSuccessor(unsigned i) const {
2159 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2160 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2163 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2164 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2165 *(&Op<-1>() - idx) = NewSucc;
2168 // Methods for support type inquiry through isa, cast, and dyn_cast:
2169 static inline bool classof(const BranchInst *) { return true; }
2170 static inline bool classof(const Instruction *I) {
2171 return (I->getOpcode() == Instruction::Br);
2173 static inline bool classof(const Value *V) {
2174 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2177 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2178 virtual unsigned getNumSuccessorsV() const;
2179 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2183 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2185 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2187 //===----------------------------------------------------------------------===//
2189 //===----------------------------------------------------------------------===//
2191 //===---------------------------------------------------------------------------
2192 /// SwitchInst - Multiway switch
2194 class SwitchInst : public TerminatorInst {
2195 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2196 unsigned ReservedSpace;
2197 // Operand[0] = Value to switch on
2198 // Operand[1] = Default basic block destination
2199 // Operand[2n ] = Value to match
2200 // Operand[2n+1] = BasicBlock to go to on match
2201 SwitchInst(const SwitchInst &RI);
2202 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2203 void resizeOperands(unsigned No);
2204 // allocate space for exactly zero operands
2205 void *operator new(size_t s) {
2206 return User::operator new(s, 0);
2208 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2209 /// switch on and a default destination. The number of additional cases can
2210 /// be specified here to make memory allocation more efficient. This
2211 /// constructor can also autoinsert before another instruction.
2212 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2213 Instruction *InsertBefore = 0);
2215 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2216 /// switch on and a default destination. The number of additional cases can
2217 /// be specified here to make memory allocation more efficient. This
2218 /// constructor also autoinserts at the end of the specified BasicBlock.
2219 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2220 BasicBlock *InsertAtEnd);
2222 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2223 unsigned NumCases, Instruction *InsertBefore = 0) {
2224 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2226 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2227 unsigned NumCases, BasicBlock *InsertAtEnd) {
2228 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2232 /// Provide fast operand accessors
2233 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2235 // Accessor Methods for Switch stmt
2236 Value *getCondition() const { return getOperand(0); }
2237 void setCondition(Value *V) { setOperand(0, V); }
2239 BasicBlock *getDefaultDest() const {
2240 return cast<BasicBlock>(getOperand(1));
2243 /// getNumCases - return the number of 'cases' in this switch instruction.
2244 /// Note that case #0 is always the default case.
2245 unsigned getNumCases() const {
2246 return getNumOperands()/2;
2249 /// getCaseValue - Return the specified case value. Note that case #0, the
2250 /// default destination, does not have a case value.
2251 ConstantInt *getCaseValue(unsigned i) {
2252 assert(i && i < getNumCases() && "Illegal case value to get!");
2253 return getSuccessorValue(i);
2256 /// getCaseValue - Return the specified case value. Note that case #0, the
2257 /// default destination, does not have a case value.
2258 const ConstantInt *getCaseValue(unsigned i) const {
2259 assert(i && i < getNumCases() && "Illegal case value to get!");
2260 return getSuccessorValue(i);
2263 /// findCaseValue - Search all of the case values for the specified constant.
2264 /// If it is explicitly handled, return the case number of it, otherwise
2265 /// return 0 to indicate that it is handled by the default handler.
2266 unsigned findCaseValue(const ConstantInt *C) const {
2267 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2268 if (getCaseValue(i) == C)
2273 /// findCaseDest - Finds the unique case value for a given successor. Returns
2274 /// null if the successor is not found, not unique, or is the default case.
2275 ConstantInt *findCaseDest(BasicBlock *BB) {
2276 if (BB == getDefaultDest()) return NULL;
2278 ConstantInt *CI = NULL;
2279 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2280 if (getSuccessor(i) == BB) {
2281 if (CI) return NULL; // Multiple cases lead to BB.
2282 else CI = getCaseValue(i);
2288 /// addCase - Add an entry to the switch instruction...
2290 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2292 /// removeCase - This method removes the specified successor from the switch
2293 /// instruction. Note that this cannot be used to remove the default
2294 /// destination (successor #0).
2296 void removeCase(unsigned idx);
2298 virtual SwitchInst *clone(LLVMContext &Context) const;
2300 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2301 BasicBlock *getSuccessor(unsigned idx) const {
2302 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2303 return cast<BasicBlock>(getOperand(idx*2+1));
2305 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2306 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2307 setOperand(idx*2+1, NewSucc);
2310 // getSuccessorValue - Return the value associated with the specified
2312 ConstantInt *getSuccessorValue(unsigned idx) const {
2313 assert(idx < getNumSuccessors() && "Successor # out of range!");
2314 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2317 // Methods for support type inquiry through isa, cast, and dyn_cast:
2318 static inline bool classof(const SwitchInst *) { return true; }
2319 static inline bool classof(const Instruction *I) {
2320 return I->getOpcode() == Instruction::Switch;
2322 static inline bool classof(const Value *V) {
2323 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2326 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2327 virtual unsigned getNumSuccessorsV() const;
2328 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2332 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2335 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2338 //===----------------------------------------------------------------------===//
2340 //===----------------------------------------------------------------------===//
2342 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2343 /// calling convention of the call.
2345 class InvokeInst : public TerminatorInst {
2346 AttrListPtr AttributeList;
2347 InvokeInst(const InvokeInst &BI);
2348 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2349 Value* const *Args, unsigned NumArgs);
2351 template<typename InputIterator>
2352 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2353 InputIterator ArgBegin, InputIterator ArgEnd,
2354 const Twine &NameStr,
2355 // This argument ensures that we have an iterator we can
2356 // do arithmetic on in constant time
2357 std::random_access_iterator_tag) {
2358 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2360 // This requires that the iterator points to contiguous memory.
2361 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2365 /// Construct an InvokeInst given a range of arguments.
2366 /// InputIterator must be a random-access iterator pointing to
2367 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2368 /// made for random-accessness but not for contiguous storage as
2369 /// that would incur runtime overhead.
2371 /// @brief Construct an InvokeInst from a range of arguments
2372 template<typename InputIterator>
2373 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2374 InputIterator ArgBegin, InputIterator ArgEnd,
2376 const Twine &NameStr, Instruction *InsertBefore);
2378 /// Construct an InvokeInst given a range of arguments.
2379 /// InputIterator must be a random-access iterator pointing to
2380 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2381 /// made for random-accessness but not for contiguous storage as
2382 /// that would incur runtime overhead.
2384 /// @brief Construct an InvokeInst from a range of arguments
2385 template<typename InputIterator>
2386 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2387 InputIterator ArgBegin, InputIterator ArgEnd,
2389 const Twine &NameStr, BasicBlock *InsertAtEnd);
2391 template<typename InputIterator>
2392 static InvokeInst *Create(Value *Func,
2393 BasicBlock *IfNormal, BasicBlock *IfException,
2394 InputIterator ArgBegin, InputIterator ArgEnd,
2395 const Twine &NameStr = "",
2396 Instruction *InsertBefore = 0) {
2397 unsigned Values(ArgEnd - ArgBegin + 3);
2398 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2399 Values, NameStr, InsertBefore);
2401 template<typename InputIterator>
2402 static InvokeInst *Create(Value *Func,
2403 BasicBlock *IfNormal, BasicBlock *IfException,
2404 InputIterator ArgBegin, InputIterator ArgEnd,
2405 const Twine &NameStr,
2406 BasicBlock *InsertAtEnd) {
2407 unsigned Values(ArgEnd - ArgBegin + 3);
2408 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2409 Values, NameStr, InsertAtEnd);
2412 virtual InvokeInst *clone(LLVMContext &Context) const;
2414 /// Provide fast operand accessors
2415 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2417 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2419 unsigned getCallingConv() const { return SubclassData; }
2420 void setCallingConv(unsigned CC) {
2424 /// getAttributes - Return the parameter attributes for this invoke.
2426 const AttrListPtr &getAttributes() const { return AttributeList; }
2428 /// setAttributes - Set the parameter attributes for this invoke.
2430 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2432 /// addAttribute - adds the attribute to the list of attributes.
2433 void addAttribute(unsigned i, Attributes attr);
2435 /// removeAttribute - removes the attribute from the list of attributes.
2436 void removeAttribute(unsigned i, Attributes attr);
2438 /// @brief Determine whether the call or the callee has the given attribute.
2439 bool paramHasAttr(unsigned i, Attributes attr) const;
2441 /// @brief Extract the alignment for a call or parameter (0=unknown).
2442 unsigned getParamAlignment(unsigned i) const {
2443 return AttributeList.getParamAlignment(i);
2446 /// @brief Determine if the call does not access memory.
2447 bool doesNotAccessMemory() const {
2448 return paramHasAttr(~0, Attribute::ReadNone);
2450 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2451 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2452 else removeAttribute(~0, Attribute::ReadNone);
2455 /// @brief Determine if the call does not access or only reads memory.
2456 bool onlyReadsMemory() const {
2457 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2459 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2460 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2461 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2464 /// @brief Determine if the call cannot return.
2465 bool doesNotReturn() const {
2466 return paramHasAttr(~0, Attribute::NoReturn);
2468 void setDoesNotReturn(bool DoesNotReturn = true) {
2469 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2470 else removeAttribute(~0, Attribute::NoReturn);
2473 /// @brief Determine if the call cannot unwind.
2474 bool doesNotThrow() const {
2475 return paramHasAttr(~0, Attribute::NoUnwind);
2477 void setDoesNotThrow(bool DoesNotThrow = true) {
2478 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2479 else removeAttribute(~0, Attribute::NoUnwind);
2482 /// @brief Determine if the call returns a structure through first
2483 /// pointer argument.
2484 bool hasStructRetAttr() const {
2485 // Be friendly and also check the callee.
2486 return paramHasAttr(1, Attribute::StructRet);
2489 /// @brief Determine if any call argument is an aggregate passed by value.
2490 bool hasByValArgument() const {
2491 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2494 /// getCalledFunction - Return the function called, or null if this is an
2495 /// indirect function invocation.
2497 Function *getCalledFunction() const {
2498 return dyn_cast<Function>(getOperand(0));
2501 /// getCalledValue - Get a pointer to the function that is invoked by this
2503 const Value *getCalledValue() const { return getOperand(0); }
2504 Value *getCalledValue() { return getOperand(0); }
2506 // get*Dest - Return the destination basic blocks...
2507 BasicBlock *getNormalDest() const {
2508 return cast<BasicBlock>(getOperand(1));
2510 BasicBlock *getUnwindDest() const {
2511 return cast<BasicBlock>(getOperand(2));
2513 void setNormalDest(BasicBlock *B) {
2517 void setUnwindDest(BasicBlock *B) {
2521 BasicBlock *getSuccessor(unsigned i) const {
2522 assert(i < 2 && "Successor # out of range for invoke!");
2523 return i == 0 ? getNormalDest() : getUnwindDest();
2526 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2527 assert(idx < 2 && "Successor # out of range for invoke!");
2528 setOperand(idx+1, NewSucc);
2531 unsigned getNumSuccessors() const { return 2; }
2533 // Methods for support type inquiry through isa, cast, and dyn_cast:
2534 static inline bool classof(const InvokeInst *) { return true; }
2535 static inline bool classof(const Instruction *I) {
2536 return (I->getOpcode() == Instruction::Invoke);
2538 static inline bool classof(const Value *V) {
2539 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2542 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2543 virtual unsigned getNumSuccessorsV() const;
2544 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2548 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2551 template<typename InputIterator>
2552 InvokeInst::InvokeInst(Value *Func,
2553 BasicBlock *IfNormal, BasicBlock *IfException,
2554 InputIterator ArgBegin, InputIterator ArgEnd,
2556 const Twine &NameStr, Instruction *InsertBefore)
2557 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2558 ->getElementType())->getReturnType(),
2559 Instruction::Invoke,
2560 OperandTraits<InvokeInst>::op_end(this) - Values,
2561 Values, InsertBefore) {
2562 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2563 typename std::iterator_traits<InputIterator>::iterator_category());
2565 template<typename InputIterator>
2566 InvokeInst::InvokeInst(Value *Func,
2567 BasicBlock *IfNormal, BasicBlock *IfException,
2568 InputIterator ArgBegin, InputIterator ArgEnd,
2570 const Twine &NameStr, BasicBlock *InsertAtEnd)
2571 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2572 ->getElementType())->getReturnType(),
2573 Instruction::Invoke,
2574 OperandTraits<InvokeInst>::op_end(this) - Values,
2575 Values, InsertAtEnd) {
2576 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2577 typename std::iterator_traits<InputIterator>::iterator_category());
2580 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2582 //===----------------------------------------------------------------------===//
2584 //===----------------------------------------------------------------------===//
2586 //===---------------------------------------------------------------------------
2587 /// UnwindInst - Immediately exit the current function, unwinding the stack
2588 /// until an invoke instruction is found.
2590 class UnwindInst : public TerminatorInst {
2591 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2593 // allocate space for exactly zero operands
2594 void *operator new(size_t s) {
2595 return User::operator new(s, 0);
2597 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2598 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2600 virtual UnwindInst *clone(LLVMContext &Context) const;
2602 unsigned getNumSuccessors() const { return 0; }
2604 // Methods for support type inquiry through isa, cast, and dyn_cast:
2605 static inline bool classof(const UnwindInst *) { return true; }
2606 static inline bool classof(const Instruction *I) {
2607 return I->getOpcode() == Instruction::Unwind;
2609 static inline bool classof(const Value *V) {
2610 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2613 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2614 virtual unsigned getNumSuccessorsV() const;
2615 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2618 //===----------------------------------------------------------------------===//
2619 // UnreachableInst Class
2620 //===----------------------------------------------------------------------===//
2622 //===---------------------------------------------------------------------------
2623 /// UnreachableInst - This function has undefined behavior. In particular, the
2624 /// presence of this instruction indicates some higher level knowledge that the
2625 /// end of the block cannot be reached.
2627 class UnreachableInst : public TerminatorInst {
2628 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2630 // allocate space for exactly zero operands
2631 void *operator new(size_t s) {
2632 return User::operator new(s, 0);
2634 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2635 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2637 virtual UnreachableInst *clone(LLVMContext &Context) const;
2639 unsigned getNumSuccessors() const { return 0; }
2641 // Methods for support type inquiry through isa, cast, and dyn_cast:
2642 static inline bool classof(const UnreachableInst *) { return true; }
2643 static inline bool classof(const Instruction *I) {
2644 return I->getOpcode() == Instruction::Unreachable;
2646 static inline bool classof(const Value *V) {
2647 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2650 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2651 virtual unsigned getNumSuccessorsV() const;
2652 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2655 //===----------------------------------------------------------------------===//
2657 //===----------------------------------------------------------------------===//
2659 /// @brief This class represents a truncation of integer types.
2660 class TruncInst : public CastInst {
2661 /// Private copy constructor
2662 TruncInst(const TruncInst &CI)
2663 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2666 /// @brief Constructor with insert-before-instruction semantics
2668 Value *S, ///< The value to be truncated
2669 const Type *Ty, ///< The (smaller) type to truncate to
2670 const Twine &NameStr = "", ///< A name for the new instruction
2671 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2674 /// @brief Constructor with insert-at-end-of-block semantics
2676 Value *S, ///< The value to be truncated
2677 const Type *Ty, ///< The (smaller) type to truncate to
2678 const Twine &NameStr, ///< A name for the new instruction
2679 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2682 /// @brief Clone an identical TruncInst
2683 virtual CastInst *clone(LLVMContext &Context) const;
2685 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2686 static inline bool classof(const TruncInst *) { return true; }
2687 static inline bool classof(const Instruction *I) {
2688 return I->getOpcode() == Trunc;
2690 static inline bool classof(const Value *V) {
2691 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2695 //===----------------------------------------------------------------------===//
2697 //===----------------------------------------------------------------------===//
2699 /// @brief This class represents zero extension of integer types.
2700 class ZExtInst : public CastInst {
2701 /// @brief Private copy constructor
2702 ZExtInst(const ZExtInst &CI)
2703 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2706 /// @brief Constructor with insert-before-instruction semantics
2708 Value *S, ///< The value to be zero extended
2709 const Type *Ty, ///< The type to zero extend to
2710 const Twine &NameStr = "", ///< A name for the new instruction
2711 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2714 /// @brief Constructor with insert-at-end semantics.
2716 Value *S, ///< The value to be zero extended
2717 const Type *Ty, ///< The type to zero extend to
2718 const Twine &NameStr, ///< A name for the new instruction
2719 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2722 /// @brief Clone an identical ZExtInst
2723 virtual CastInst *clone(LLVMContext &Context) const;
2725 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2726 static inline bool classof(const ZExtInst *) { return true; }
2727 static inline bool classof(const Instruction *I) {
2728 return I->getOpcode() == ZExt;
2730 static inline bool classof(const Value *V) {
2731 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2735 //===----------------------------------------------------------------------===//
2737 //===----------------------------------------------------------------------===//
2739 /// @brief This class represents a sign extension of integer types.
2740 class SExtInst : public CastInst {
2741 /// @brief Private copy constructor
2742 SExtInst(const SExtInst &CI)
2743 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2746 /// @brief Constructor with insert-before-instruction semantics
2748 Value *S, ///< The value to be sign extended
2749 const Type *Ty, ///< The type to sign extend to
2750 const Twine &NameStr = "", ///< A name for the new instruction
2751 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2754 /// @brief Constructor with insert-at-end-of-block semantics
2756 Value *S, ///< The value to be sign extended
2757 const Type *Ty, ///< The type to sign extend to
2758 const Twine &NameStr, ///< A name for the new instruction
2759 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2762 /// @brief Clone an identical SExtInst
2763 virtual CastInst *clone(LLVMContext &Context) const;
2765 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2766 static inline bool classof(const SExtInst *) { return true; }
2767 static inline bool classof(const Instruction *I) {
2768 return I->getOpcode() == SExt;
2770 static inline bool classof(const Value *V) {
2771 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2775 //===----------------------------------------------------------------------===//
2776 // FPTruncInst Class
2777 //===----------------------------------------------------------------------===//
2779 /// @brief This class represents a truncation of floating point types.
2780 class FPTruncInst : public CastInst {
2781 FPTruncInst(const FPTruncInst &CI)
2782 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2785 /// @brief Constructor with insert-before-instruction semantics
2787 Value *S, ///< The value to be truncated
2788 const Type *Ty, ///< The type to truncate to
2789 const Twine &NameStr = "", ///< A name for the new instruction
2790 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2793 /// @brief Constructor with insert-before-instruction semantics
2795 Value *S, ///< The value to be truncated
2796 const Type *Ty, ///< The type to truncate to
2797 const Twine &NameStr, ///< A name for the new instruction
2798 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2801 /// @brief Clone an identical FPTruncInst
2802 virtual CastInst *clone(LLVMContext &Context) const;
2804 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2805 static inline bool classof(const FPTruncInst *) { return true; }
2806 static inline bool classof(const Instruction *I) {
2807 return I->getOpcode() == FPTrunc;
2809 static inline bool classof(const Value *V) {
2810 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2814 //===----------------------------------------------------------------------===//
2816 //===----------------------------------------------------------------------===//
2818 /// @brief This class represents an extension of floating point types.
2819 class FPExtInst : public CastInst {
2820 FPExtInst(const FPExtInst &CI)
2821 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2824 /// @brief Constructor with insert-before-instruction semantics
2826 Value *S, ///< The value to be extended
2827 const Type *Ty, ///< The type to extend to
2828 const Twine &NameStr = "", ///< A name for the new instruction
2829 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2832 /// @brief Constructor with insert-at-end-of-block semantics
2834 Value *S, ///< The value to be extended
2835 const Type *Ty, ///< The type to extend to
2836 const Twine &NameStr, ///< A name for the new instruction
2837 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2840 /// @brief Clone an identical FPExtInst
2841 virtual CastInst *clone(LLVMContext &Context) const;
2843 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2844 static inline bool classof(const FPExtInst *) { return true; }
2845 static inline bool classof(const Instruction *I) {
2846 return I->getOpcode() == FPExt;
2848 static inline bool classof(const Value *V) {
2849 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2853 //===----------------------------------------------------------------------===//
2855 //===----------------------------------------------------------------------===//
2857 /// @brief This class represents a cast unsigned integer to floating point.
2858 class UIToFPInst : public CastInst {
2859 UIToFPInst(const UIToFPInst &CI)
2860 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2863 /// @brief Constructor with insert-before-instruction semantics
2865 Value *S, ///< The value to be converted
2866 const Type *Ty, ///< The type to convert to
2867 const Twine &NameStr = "", ///< A name for the new instruction
2868 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2871 /// @brief Constructor with insert-at-end-of-block semantics
2873 Value *S, ///< The value to be converted
2874 const Type *Ty, ///< The type to convert to
2875 const Twine &NameStr, ///< A name for the new instruction
2876 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2879 /// @brief Clone an identical UIToFPInst
2880 virtual CastInst *clone(LLVMContext &Context) const;
2882 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2883 static inline bool classof(const UIToFPInst *) { return true; }
2884 static inline bool classof(const Instruction *I) {
2885 return I->getOpcode() == UIToFP;
2887 static inline bool classof(const Value *V) {
2888 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2892 //===----------------------------------------------------------------------===//
2894 //===----------------------------------------------------------------------===//
2896 /// @brief This class represents a cast from signed integer to floating point.
2897 class SIToFPInst : public CastInst {
2898 SIToFPInst(const SIToFPInst &CI)
2899 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2902 /// @brief Constructor with insert-before-instruction semantics
2904 Value *S, ///< The value to be converted
2905 const Type *Ty, ///< The type to convert to
2906 const Twine &NameStr = "", ///< A name for the new instruction
2907 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2910 /// @brief Constructor with insert-at-end-of-block semantics
2912 Value *S, ///< The value to be converted
2913 const Type *Ty, ///< The type to convert to
2914 const Twine &NameStr, ///< A name for the new instruction
2915 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2918 /// @brief Clone an identical SIToFPInst
2919 virtual CastInst *clone(LLVMContext &Context) const;
2921 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2922 static inline bool classof(const SIToFPInst *) { return true; }
2923 static inline bool classof(const Instruction *I) {
2924 return I->getOpcode() == SIToFP;
2926 static inline bool classof(const Value *V) {
2927 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2931 //===----------------------------------------------------------------------===//
2933 //===----------------------------------------------------------------------===//
2935 /// @brief This class represents a cast from floating point to unsigned integer
2936 class FPToUIInst : public CastInst {
2937 FPToUIInst(const FPToUIInst &CI)
2938 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2941 /// @brief Constructor with insert-before-instruction semantics
2943 Value *S, ///< The value to be converted
2944 const Type *Ty, ///< The type to convert to
2945 const Twine &NameStr = "", ///< A name for the new instruction
2946 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2949 /// @brief Constructor with insert-at-end-of-block semantics
2951 Value *S, ///< The value to be converted
2952 const Type *Ty, ///< The type to convert to
2953 const Twine &NameStr, ///< A name for the new instruction
2954 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2957 /// @brief Clone an identical FPToUIInst
2958 virtual CastInst *clone(LLVMContext &Context) const;
2960 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2961 static inline bool classof(const FPToUIInst *) { return true; }
2962 static inline bool classof(const Instruction *I) {
2963 return I->getOpcode() == FPToUI;
2965 static inline bool classof(const Value *V) {
2966 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2970 //===----------------------------------------------------------------------===//
2972 //===----------------------------------------------------------------------===//
2974 /// @brief This class represents a cast from floating point to signed integer.
2975 class FPToSIInst : public CastInst {
2976 FPToSIInst(const FPToSIInst &CI)
2977 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2980 /// @brief Constructor with insert-before-instruction semantics
2982 Value *S, ///< The value to be converted
2983 const Type *Ty, ///< The type to convert to
2984 const Twine &NameStr = "", ///< A name for the new instruction
2985 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2988 /// @brief Constructor with insert-at-end-of-block semantics
2990 Value *S, ///< The value to be converted
2991 const Type *Ty, ///< The type to convert to
2992 const Twine &NameStr, ///< A name for the new instruction
2993 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2996 /// @brief Clone an identical FPToSIInst
2997 virtual CastInst *clone(LLVMContext &Context) const;
2999 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3000 static inline bool classof(const FPToSIInst *) { return true; }
3001 static inline bool classof(const Instruction *I) {
3002 return I->getOpcode() == FPToSI;
3004 static inline bool classof(const Value *V) {
3005 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3009 //===----------------------------------------------------------------------===//
3010 // IntToPtrInst Class
3011 //===----------------------------------------------------------------------===//
3013 /// @brief This class represents a cast from an integer to a pointer.
3014 class IntToPtrInst : public CastInst {
3015 IntToPtrInst(const IntToPtrInst &CI)
3016 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3019 /// @brief Constructor with insert-before-instruction semantics
3021 Value *S, ///< The value to be converted
3022 const Type *Ty, ///< The type to convert to
3023 const Twine &NameStr = "", ///< A name for the new instruction
3024 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3027 /// @brief Constructor with insert-at-end-of-block semantics
3029 Value *S, ///< The value to be converted
3030 const Type *Ty, ///< The type to convert to
3031 const Twine &NameStr, ///< A name for the new instruction
3032 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3035 /// @brief Clone an identical IntToPtrInst
3036 virtual CastInst *clone(LLVMContext &Context) const;
3038 // Methods for support type inquiry through isa, cast, and dyn_cast:
3039 static inline bool classof(const IntToPtrInst *) { return true; }
3040 static inline bool classof(const Instruction *I) {
3041 return I->getOpcode() == IntToPtr;
3043 static inline bool classof(const Value *V) {
3044 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3048 //===----------------------------------------------------------------------===//
3049 // PtrToIntInst Class
3050 //===----------------------------------------------------------------------===//
3052 /// @brief This class represents a cast from a pointer to an integer
3053 class PtrToIntInst : public CastInst {
3054 PtrToIntInst(const PtrToIntInst &CI)
3055 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3058 /// @brief Constructor with insert-before-instruction semantics
3060 Value *S, ///< The value to be converted
3061 const Type *Ty, ///< The type to convert to
3062 const Twine &NameStr = "", ///< A name for the new instruction
3063 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3066 /// @brief Constructor with insert-at-end-of-block semantics
3068 Value *S, ///< The value to be converted
3069 const Type *Ty, ///< The type to convert to
3070 const Twine &NameStr, ///< A name for the new instruction
3071 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3074 /// @brief Clone an identical PtrToIntInst
3075 virtual CastInst *clone(LLVMContext &Context) const;
3077 // Methods for support type inquiry through isa, cast, and dyn_cast:
3078 static inline bool classof(const PtrToIntInst *) { return true; }
3079 static inline bool classof(const Instruction *I) {
3080 return I->getOpcode() == PtrToInt;
3082 static inline bool classof(const Value *V) {
3083 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3087 //===----------------------------------------------------------------------===//
3088 // BitCastInst Class
3089 //===----------------------------------------------------------------------===//
3091 /// @brief This class represents a no-op cast from one type to another.
3092 class BitCastInst : public CastInst {
3093 BitCastInst(const BitCastInst &CI)
3094 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3097 /// @brief Constructor with insert-before-instruction semantics
3099 Value *S, ///< The value to be casted
3100 const Type *Ty, ///< The type to casted to
3101 const Twine &NameStr = "", ///< A name for the new instruction
3102 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3105 /// @brief Constructor with insert-at-end-of-block semantics
3107 Value *S, ///< The value to be casted
3108 const Type *Ty, ///< The type to casted to
3109 const Twine &NameStr, ///< A name for the new instruction
3110 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3113 /// @brief Clone an identical BitCastInst
3114 virtual CastInst *clone(LLVMContext &Context) const;
3116 // Methods for support type inquiry through isa, cast, and dyn_cast:
3117 static inline bool classof(const BitCastInst *) { return true; }
3118 static inline bool classof(const Instruction *I) {
3119 return I->getOpcode() == BitCast;
3121 static inline bool classof(const Value *V) {
3122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3126 } // End llvm namespace