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/CallingConv.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/ADT/SmallVector.h"
35 //===----------------------------------------------------------------------===//
36 // AllocationInst Class
37 //===----------------------------------------------------------------------===//
39 /// AllocationInst - This class is the common base class of MallocInst and
42 class AllocationInst : public UnaryInstruction {
44 AllocationInst(const Type *Ty, Value *ArraySize,
45 unsigned iTy, unsigned Align, const Twine &Name = "",
46 Instruction *InsertBefore = 0);
47 AllocationInst(const Type *Ty, Value *ArraySize,
48 unsigned iTy, unsigned Align, const Twine &Name,
49 BasicBlock *InsertAtEnd);
51 // Out of line virtual method, so the vtable, etc. has a home.
52 virtual ~AllocationInst();
54 /// isArrayAllocation - Return true if there is an allocation size parameter
55 /// to the allocation instruction that is not 1.
57 bool isArrayAllocation() const;
59 /// getArraySize - Get the number of elements allocated. For a simple
60 /// allocation of a single element, this will return a constant 1 value.
62 const Value *getArraySize() const { return getOperand(0); }
63 Value *getArraySize() { return getOperand(0); }
65 /// getType - Overload to return most specific pointer type
67 const PointerType *getType() const {
68 return reinterpret_cast<const PointerType*>(Instruction::getType());
71 /// getAllocatedType - Return the type that is being allocated by the
74 const Type *getAllocatedType() const;
76 /// getAlignment - Return the alignment of the memory that is being allocated
77 /// by the instruction.
79 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
80 void setAlignment(unsigned Align);
82 virtual AllocationInst *clone(LLVMContext &Context) const = 0;
84 // Methods for support type inquiry through isa, cast, and dyn_cast:
85 static inline bool classof(const AllocationInst *) { return true; }
86 static inline bool classof(const Instruction *I) {
87 return I->getOpcode() == Instruction::Alloca ||
88 I->getOpcode() == Instruction::Malloc;
90 static inline bool classof(const Value *V) {
91 return isa<Instruction>(V) && classof(cast<Instruction>(V));
96 //===----------------------------------------------------------------------===//
98 //===----------------------------------------------------------------------===//
100 /// MallocInst - an instruction to allocated memory on the heap
102 class MallocInst : public AllocationInst {
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 {
152 explicit AllocaInst(const Type *Ty,
153 Value *ArraySize = 0,
154 const Twine &NameStr = "",
155 Instruction *InsertBefore = 0)
156 : AllocationInst(Ty, ArraySize, Alloca,
157 0, NameStr, InsertBefore) {}
158 AllocaInst(const Type *Ty,
159 Value *ArraySize, const Twine &NameStr,
160 BasicBlock *InsertAtEnd)
161 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
163 AllocaInst(const Type *Ty, const Twine &NameStr,
164 Instruction *InsertBefore = 0)
165 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
166 AllocaInst(const Type *Ty, const Twine &NameStr,
167 BasicBlock *InsertAtEnd)
168 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
170 AllocaInst(const Type *Ty, Value *ArraySize,
171 unsigned Align, const Twine &NameStr = "",
172 Instruction *InsertBefore = 0)
173 : AllocationInst(Ty, ArraySize, Alloca,
174 Align, NameStr, InsertBefore) {}
175 AllocaInst(const Type *Ty, Value *ArraySize,
176 unsigned Align, const Twine &NameStr,
177 BasicBlock *InsertAtEnd)
178 : AllocationInst(Ty, ArraySize, Alloca,
179 Align, NameStr, InsertAtEnd) {}
181 virtual AllocaInst *clone(LLVMContext &Context) const;
183 /// isStaticAlloca - Return true if this alloca is in the entry block of the
184 /// function and is a constant size. If so, the code generator will fold it
185 /// into the prolog/epilog code, so it is basically free.
186 bool isStaticAlloca() const;
188 // Methods for support type inquiry through isa, cast, and dyn_cast:
189 static inline bool classof(const AllocaInst *) { return true; }
190 static inline bool classof(const Instruction *I) {
191 return (I->getOpcode() == Instruction::Alloca);
193 static inline bool classof(const Value *V) {
194 return isa<Instruction>(V) && classof(cast<Instruction>(V));
199 //===----------------------------------------------------------------------===//
201 //===----------------------------------------------------------------------===//
203 /// FreeInst - an instruction to deallocate memory
205 class FreeInst : public UnaryInstruction {
208 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
209 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
211 virtual FreeInst *clone(LLVMContext &Context) const;
213 // Accessor methods for consistency with other memory operations
214 Value *getPointerOperand() { return getOperand(0); }
215 const Value *getPointerOperand() const { return getOperand(0); }
217 // Methods for support type inquiry through isa, cast, and dyn_cast:
218 static inline bool classof(const FreeInst *) { return true; }
219 static inline bool classof(const Instruction *I) {
220 return (I->getOpcode() == Instruction::Free);
222 static inline bool classof(const Value *V) {
223 return isa<Instruction>(V) && classof(cast<Instruction>(V));
228 //===----------------------------------------------------------------------===//
230 //===----------------------------------------------------------------------===//
232 /// LoadInst - an instruction for reading from memory. This uses the
233 /// SubclassData field in Value to store whether or not the load is volatile.
235 class LoadInst : public UnaryInstruction {
238 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
239 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
240 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
241 Instruction *InsertBefore = 0);
242 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
243 unsigned Align, Instruction *InsertBefore = 0);
244 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
245 BasicBlock *InsertAtEnd);
246 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
247 unsigned Align, BasicBlock *InsertAtEnd);
249 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
250 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
251 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
252 bool isVolatile = false, Instruction *InsertBefore = 0);
253 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
254 BasicBlock *InsertAtEnd);
256 /// isVolatile - Return true if this is a load from a volatile memory
259 bool isVolatile() const { return SubclassData & 1; }
261 /// setVolatile - Specify whether this is a volatile load or not.
263 void setVolatile(bool V) {
264 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
267 virtual LoadInst *clone(LLVMContext &Context) const;
269 /// getAlignment - Return the alignment of the access that is being performed
271 unsigned getAlignment() const {
272 return (1 << (SubclassData>>1)) >> 1;
275 void setAlignment(unsigned Align);
277 Value *getPointerOperand() { return getOperand(0); }
278 const Value *getPointerOperand() const { return getOperand(0); }
279 static unsigned getPointerOperandIndex() { return 0U; }
281 unsigned getPointerAddressSpace() const {
282 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
286 // Methods for support type inquiry through isa, cast, and dyn_cast:
287 static inline bool classof(const LoadInst *) { return true; }
288 static inline bool classof(const Instruction *I) {
289 return I->getOpcode() == Instruction::Load;
291 static inline bool classof(const Value *V) {
292 return isa<Instruction>(V) && classof(cast<Instruction>(V));
297 //===----------------------------------------------------------------------===//
299 //===----------------------------------------------------------------------===//
301 /// StoreInst - an instruction for storing to memory
303 class StoreInst : public Instruction {
304 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
307 // allocate space for exactly two operands
308 void *operator new(size_t s) {
309 return User::operator new(s, 2);
311 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
312 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
313 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
314 Instruction *InsertBefore = 0);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
316 unsigned Align, Instruction *InsertBefore = 0);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
318 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
319 unsigned Align, BasicBlock *InsertAtEnd);
322 /// isVolatile - Return true if this is a load from a volatile memory
325 bool isVolatile() const { return SubclassData & 1; }
327 /// setVolatile - Specify whether this is a volatile load or not.
329 void setVolatile(bool V) {
330 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
333 /// Transparently provide more efficient getOperand methods.
334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
336 /// getAlignment - Return the alignment of the access that is being performed
338 unsigned getAlignment() const {
339 return (1 << (SubclassData>>1)) >> 1;
342 void setAlignment(unsigned Align);
344 virtual StoreInst *clone(LLVMContext &Context) const;
346 Value *getPointerOperand() { return getOperand(1); }
347 const Value *getPointerOperand() const { return getOperand(1); }
348 static unsigned getPointerOperandIndex() { return 1U; }
350 unsigned getPointerAddressSpace() const {
351 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
354 // Methods for support type inquiry through isa, cast, and dyn_cast:
355 static inline bool classof(const StoreInst *) { return true; }
356 static inline bool classof(const Instruction *I) {
357 return I->getOpcode() == Instruction::Store;
359 static inline bool classof(const Value *V) {
360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
365 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
368 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
370 //===----------------------------------------------------------------------===//
371 // GetElementPtrInst Class
372 //===----------------------------------------------------------------------===//
374 // checkType - Simple wrapper function to give a better assertion failure
375 // message on bad indexes for a gep instruction.
377 static inline const Type *checkType(const Type *Ty) {
378 assert(Ty && "Invalid GetElementPtrInst indices for type!");
382 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
383 /// access elements of arrays and structs
385 class GetElementPtrInst : public Instruction {
386 GetElementPtrInst(const GetElementPtrInst &GEPI);
387 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
388 const Twine &NameStr);
389 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
391 template<typename InputIterator>
392 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
393 const Twine &NameStr,
394 // This argument ensures that we have an iterator we can
395 // do arithmetic on in constant time
396 std::random_access_iterator_tag) {
397 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
400 // This requires that the iterator points to contiguous memory.
401 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
402 // we have to build an array here
405 init(Ptr, 0, NumIdx, NameStr);
409 /// getIndexedType - Returns the type of the element that would be loaded with
410 /// a load instruction with the specified parameters.
412 /// Null is returned if the indices are invalid for the specified
415 template<typename InputIterator>
416 static const Type *getIndexedType(const Type *Ptr,
417 InputIterator IdxBegin,
418 InputIterator IdxEnd,
419 // This argument ensures that we
420 // have an iterator we can do
421 // arithmetic on in constant time
422 std::random_access_iterator_tag) {
423 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
426 // This requires that the iterator points to contiguous memory.
427 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
429 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
432 /// Constructors - Create a getelementptr instruction with a base pointer an
433 /// list of indices. The first ctor can optionally insert before an existing
434 /// instruction, the second appends the new instruction to the specified
436 template<typename InputIterator>
437 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
438 InputIterator IdxEnd,
440 const Twine &NameStr,
441 Instruction *InsertBefore);
442 template<typename InputIterator>
443 inline GetElementPtrInst(Value *Ptr,
444 InputIterator IdxBegin, InputIterator IdxEnd,
446 const Twine &NameStr, BasicBlock *InsertAtEnd);
448 /// Constructors - These two constructors are convenience methods because one
449 /// and two index getelementptr instructions are so common.
450 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
451 Instruction *InsertBefore = 0);
452 GetElementPtrInst(Value *Ptr, Value *Idx,
453 const Twine &NameStr, BasicBlock *InsertAtEnd);
455 template<typename InputIterator>
456 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
457 InputIterator IdxEnd,
458 const Twine &NameStr = "",
459 Instruction *InsertBefore = 0) {
460 typename std::iterator_traits<InputIterator>::difference_type Values =
461 1 + std::distance(IdxBegin, IdxEnd);
463 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
465 template<typename InputIterator>
466 static GetElementPtrInst *Create(Value *Ptr,
467 InputIterator IdxBegin, InputIterator IdxEnd,
468 const Twine &NameStr,
469 BasicBlock *InsertAtEnd) {
470 typename std::iterator_traits<InputIterator>::difference_type Values =
471 1 + std::distance(IdxBegin, IdxEnd);
473 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
476 /// Constructors - These two creators are convenience methods because one
477 /// index getelementptr instructions are so common.
478 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
479 const Twine &NameStr = "",
480 Instruction *InsertBefore = 0) {
481 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
483 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
484 const Twine &NameStr,
485 BasicBlock *InsertAtEnd) {
486 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
489 /// Create an "inbounds" getelementptr. See the documentation for the
490 /// "inbounds" flag in LangRef.html for details.
491 template<typename InputIterator>
492 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
493 InputIterator IdxEnd,
494 const Twine &NameStr = "",
495 Instruction *InsertBefore = 0) {
496 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
497 NameStr, InsertBefore);
498 cast<GEPOperator>(GEP)->setIsInBounds(true);
501 template<typename InputIterator>
502 static GetElementPtrInst *CreateInBounds(Value *Ptr,
503 InputIterator IdxBegin,
504 InputIterator IdxEnd,
505 const Twine &NameStr,
506 BasicBlock *InsertAtEnd) {
507 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
508 NameStr, InsertAtEnd);
509 cast<GEPOperator>(GEP)->setIsInBounds(true);
512 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
513 const Twine &NameStr = "",
514 Instruction *InsertBefore = 0) {
515 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
516 cast<GEPOperator>(GEP)->setIsInBounds(true);
519 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
520 const Twine &NameStr,
521 BasicBlock *InsertAtEnd) {
522 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
523 cast<GEPOperator>(GEP)->setIsInBounds(true);
527 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
529 /// Transparently provide more efficient getOperand methods.
530 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
532 // getType - Overload to return most specific pointer type...
533 const PointerType *getType() const {
534 return reinterpret_cast<const PointerType*>(Instruction::getType());
537 /// getIndexedType - Returns the type of the element that would be loaded with
538 /// a load instruction with the specified parameters.
540 /// Null is returned if the indices are invalid for the specified
543 template<typename InputIterator>
544 static const Type *getIndexedType(const Type *Ptr,
545 InputIterator IdxBegin,
546 InputIterator IdxEnd) {
547 return getIndexedType(Ptr, IdxBegin, IdxEnd,
548 typename std::iterator_traits<InputIterator>::
549 iterator_category());
552 static const Type *getIndexedType(const Type *Ptr,
553 Value* const *Idx, unsigned NumIdx);
555 static const Type *getIndexedType(const Type *Ptr,
556 uint64_t const *Idx, unsigned NumIdx);
558 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
560 inline op_iterator idx_begin() { return op_begin()+1; }
561 inline const_op_iterator idx_begin() const { return op_begin()+1; }
562 inline op_iterator idx_end() { return op_end(); }
563 inline const_op_iterator idx_end() const { return op_end(); }
565 Value *getPointerOperand() {
566 return getOperand(0);
568 const Value *getPointerOperand() const {
569 return getOperand(0);
571 static unsigned getPointerOperandIndex() {
572 return 0U; // get index for modifying correct operand
575 unsigned getPointerAddressSpace() const {
576 return cast<PointerType>(getType())->getAddressSpace();
579 /// getPointerOperandType - Method to return the pointer operand as a
581 const PointerType *getPointerOperandType() const {
582 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
586 unsigned getNumIndices() const { // Note: always non-negative
587 return getNumOperands() - 1;
590 bool hasIndices() const {
591 return getNumOperands() > 1;
594 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
595 /// zeros. If so, the result pointer and the first operand have the same
596 /// value, just potentially different types.
597 bool hasAllZeroIndices() const;
599 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
600 /// constant integers. If so, the result pointer and the first operand have
601 /// a constant offset between them.
602 bool hasAllConstantIndices() const;
604 // Methods for support type inquiry through isa, cast, and dyn_cast:
605 static inline bool classof(const GetElementPtrInst *) { return true; }
606 static inline bool classof(const Instruction *I) {
607 return (I->getOpcode() == Instruction::GetElementPtr);
609 static inline bool classof(const Value *V) {
610 return isa<Instruction>(V) && classof(cast<Instruction>(V));
615 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
618 template<typename InputIterator>
619 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
620 InputIterator IdxBegin,
621 InputIterator IdxEnd,
623 const Twine &NameStr,
624 Instruction *InsertBefore)
625 : Instruction(PointerType::get(checkType(
626 getIndexedType(Ptr->getType(),
628 cast<PointerType>(Ptr->getType())
629 ->getAddressSpace()),
631 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
632 Values, InsertBefore) {
633 init(Ptr, IdxBegin, IdxEnd, NameStr,
634 typename std::iterator_traits<InputIterator>::iterator_category());
636 template<typename InputIterator>
637 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
638 InputIterator IdxBegin,
639 InputIterator IdxEnd,
641 const Twine &NameStr,
642 BasicBlock *InsertAtEnd)
643 : Instruction(PointerType::get(checkType(
644 getIndexedType(Ptr->getType(),
646 cast<PointerType>(Ptr->getType())
647 ->getAddressSpace()),
649 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
650 Values, InsertAtEnd) {
651 init(Ptr, IdxBegin, IdxEnd, NameStr,
652 typename std::iterator_traits<InputIterator>::iterator_category());
656 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
659 //===----------------------------------------------------------------------===//
661 //===----------------------------------------------------------------------===//
663 /// This instruction compares its operands according to the predicate given
664 /// to the constructor. It only operates on integers or pointers. The operands
665 /// must be identical types.
666 /// @brief Represent an integer comparison operator.
667 class ICmpInst: public CmpInst {
669 /// @brief Constructor with insert-before-instruction semantics.
671 Instruction *InsertBefore, ///< Where to insert
672 Predicate pred, ///< The predicate to use for the comparison
673 Value *LHS, ///< The left-hand-side of the expression
674 Value *RHS, ///< The right-hand-side of the expression
675 const Twine &NameStr = "" ///< Name of the instruction
676 ) : CmpInst(makeCmpResultType(LHS->getType()),
677 Instruction::ICmp, pred, LHS, RHS, NameStr,
679 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
680 pred <= CmpInst::LAST_ICMP_PREDICATE &&
681 "Invalid ICmp predicate value");
682 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
683 "Both operands to ICmp instruction are not of the same type!");
684 // Check that the operands are the right type
685 assert((getOperand(0)->getType()->isIntOrIntVector() ||
686 isa<PointerType>(getOperand(0)->getType())) &&
687 "Invalid operand types for ICmp instruction");
690 /// @brief Constructor with insert-at-end semantics.
692 BasicBlock &InsertAtEnd, ///< Block to insert into.
693 Predicate pred, ///< The predicate to use for the comparison
694 Value *LHS, ///< The left-hand-side of the expression
695 Value *RHS, ///< The right-hand-side of the expression
696 const Twine &NameStr = "" ///< Name of the instruction
697 ) : CmpInst(makeCmpResultType(LHS->getType()),
698 Instruction::ICmp, pred, LHS, RHS, NameStr,
700 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
701 pred <= CmpInst::LAST_ICMP_PREDICATE &&
702 "Invalid ICmp predicate value");
703 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
704 "Both operands to ICmp instruction are not of the same type!");
705 // Check that the operands are the right type
706 assert((getOperand(0)->getType()->isIntOrIntVector() ||
707 isa<PointerType>(getOperand(0)->getType())) &&
708 "Invalid operand types for ICmp instruction");
711 /// @brief Constructor with no-insertion semantics
713 Predicate pred, ///< The predicate to use for the comparison
714 Value *LHS, ///< The left-hand-side of the expression
715 Value *RHS, ///< The right-hand-side of the expression
716 const Twine &NameStr = "" ///< Name of the instruction
717 ) : CmpInst(makeCmpResultType(LHS->getType()),
718 Instruction::ICmp, pred, LHS, RHS, NameStr) {
719 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
720 pred <= CmpInst::LAST_ICMP_PREDICATE &&
721 "Invalid ICmp predicate value");
722 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
723 "Both operands to ICmp instruction are not of the same type!");
724 // Check that the operands are the right type
725 assert((getOperand(0)->getType()->isIntOrIntVector() ||
726 isa<PointerType>(getOperand(0)->getType())) &&
727 "Invalid operand types for ICmp instruction");
730 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
731 /// @returns the predicate that would be the result if the operand were
732 /// regarded as signed.
733 /// @brief Return the signed version of the predicate
734 Predicate getSignedPredicate() const {
735 return getSignedPredicate(getPredicate());
738 /// This is a static version that you can use without an instruction.
739 /// @brief Return the signed version of the predicate.
740 static Predicate getSignedPredicate(Predicate pred);
742 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
743 /// @returns the predicate that would be the result if the operand were
744 /// regarded as unsigned.
745 /// @brief Return the unsigned version of the predicate
746 Predicate getUnsignedPredicate() const {
747 return getUnsignedPredicate(getPredicate());
750 /// This is a static version that you can use without an instruction.
751 /// @brief Return the unsigned version of the predicate.
752 static Predicate getUnsignedPredicate(Predicate pred);
754 /// isEquality - Return true if this predicate is either EQ or NE. This also
755 /// tests for commutativity.
756 static bool isEquality(Predicate P) {
757 return P == ICMP_EQ || P == ICMP_NE;
760 /// isEquality - Return true if this predicate is either EQ or NE. This also
761 /// tests for commutativity.
762 bool isEquality() const {
763 return isEquality(getPredicate());
766 /// @returns true if the predicate of this ICmpInst is commutative
767 /// @brief Determine if this relation is commutative.
768 bool isCommutative() const { return isEquality(); }
770 /// isRelational - Return true if the predicate is relational (not EQ or NE).
772 bool isRelational() const {
773 return !isEquality();
776 /// isRelational - Return true if the predicate is relational (not EQ or NE).
778 static bool isRelational(Predicate P) {
779 return !isEquality(P);
782 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
783 /// @brief Determine if this instruction's predicate is signed.
784 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
786 /// @returns true if the predicate provided is signed, false otherwise
787 /// @brief Determine if the predicate is signed.
788 static bool isSignedPredicate(Predicate pred);
790 /// @returns true if the specified compare predicate is
791 /// true when both operands are equal...
792 /// @brief Determine if the icmp is true when both operands are equal
793 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
794 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
795 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
796 pred == ICmpInst::ICMP_SLE;
799 /// @returns true if the specified compare instruction is
800 /// true when both operands are equal...
801 /// @brief Determine if the ICmpInst returns true when both operands are equal
802 bool isTrueWhenEqual() {
803 return isTrueWhenEqual(getPredicate());
806 /// Initialize a set of values that all satisfy the predicate with C.
807 /// @brief Make a ConstantRange for a relation with a constant value.
808 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
810 /// Exchange the two operands to this instruction in such a way that it does
811 /// not modify the semantics of the instruction. The predicate value may be
812 /// changed to retain the same result if the predicate is order dependent
814 /// @brief Swap operands and adjust predicate.
815 void swapOperands() {
816 SubclassData = getSwappedPredicate();
817 Op<0>().swap(Op<1>());
820 virtual ICmpInst *clone(LLVMContext &Context) const;
822 // Methods for support type inquiry through isa, cast, and dyn_cast:
823 static inline bool classof(const ICmpInst *) { return true; }
824 static inline bool classof(const Instruction *I) {
825 return I->getOpcode() == Instruction::ICmp;
827 static inline bool classof(const Value *V) {
828 return isa<Instruction>(V) && classof(cast<Instruction>(V));
833 //===----------------------------------------------------------------------===//
835 //===----------------------------------------------------------------------===//
837 /// This instruction compares its operands according to the predicate given
838 /// to the constructor. It only operates on floating point values or packed
839 /// vectors of floating point values. The operands must be identical types.
840 /// @brief Represents a floating point comparison operator.
841 class FCmpInst: public CmpInst {
843 /// @brief Constructor with insert-before-instruction semantics.
845 Instruction *InsertBefore, ///< Where to insert
846 Predicate pred, ///< The predicate to use for the comparison
847 Value *LHS, ///< The left-hand-side of the expression
848 Value *RHS, ///< The right-hand-side of the expression
849 const Twine &NameStr = "" ///< Name of the instruction
850 ) : CmpInst(makeCmpResultType(LHS->getType()),
851 Instruction::FCmp, pred, LHS, RHS, NameStr,
853 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
854 "Invalid FCmp predicate value");
855 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
856 "Both operands to FCmp instruction are not of the same type!");
857 // Check that the operands are the right type
858 assert(getOperand(0)->getType()->isFPOrFPVector() &&
859 "Invalid operand types for FCmp instruction");
862 /// @brief Constructor with insert-at-end semantics.
864 BasicBlock &InsertAtEnd, ///< Block to insert into.
865 Predicate pred, ///< The predicate to use for the comparison
866 Value *LHS, ///< The left-hand-side of the expression
867 Value *RHS, ///< The right-hand-side of the expression
868 const Twine &NameStr = "" ///< Name of the instruction
869 ) : CmpInst(makeCmpResultType(LHS->getType()),
870 Instruction::FCmp, pred, LHS, RHS, NameStr,
872 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
873 "Invalid FCmp predicate value");
874 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
875 "Both operands to FCmp instruction are not of the same type!");
876 // Check that the operands are the right type
877 assert(getOperand(0)->getType()->isFPOrFPVector() &&
878 "Invalid operand types for FCmp instruction");
881 /// @brief Constructor with no-insertion semantics
883 Predicate pred, ///< The predicate to use for the comparison
884 Value *LHS, ///< The left-hand-side of the expression
885 Value *RHS, ///< The right-hand-side of the expression
886 const Twine &NameStr = "" ///< Name of the instruction
887 ) : CmpInst(makeCmpResultType(LHS->getType()),
888 Instruction::FCmp, pred, LHS, RHS, NameStr) {
889 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
890 "Invalid FCmp predicate value");
891 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
892 "Both operands to FCmp instruction are not of the same type!");
893 // Check that the operands are the right type
894 assert(getOperand(0)->getType()->isFPOrFPVector() &&
895 "Invalid operand types for FCmp instruction");
898 /// @returns true if the predicate of this instruction is EQ or NE.
899 /// @brief Determine if this is an equality predicate.
900 bool isEquality() const {
901 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
902 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
905 /// @returns true if the predicate of this instruction is commutative.
906 /// @brief Determine if this is a commutative predicate.
907 bool isCommutative() const {
908 return isEquality() ||
909 SubclassData == FCMP_FALSE ||
910 SubclassData == FCMP_TRUE ||
911 SubclassData == FCMP_ORD ||
912 SubclassData == FCMP_UNO;
915 /// @returns true if the predicate is relational (not EQ or NE).
916 /// @brief Determine if this a relational predicate.
917 bool isRelational() const { return !isEquality(); }
919 /// Exchange the two operands to this instruction in such a way that it does
920 /// not modify the semantics of the instruction. The predicate value may be
921 /// changed to retain the same result if the predicate is order dependent
923 /// @brief Swap operands and adjust predicate.
924 void swapOperands() {
925 SubclassData = getSwappedPredicate();
926 Op<0>().swap(Op<1>());
929 virtual FCmpInst *clone(LLVMContext &Context) const;
931 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
932 static inline bool classof(const FCmpInst *) { return true; }
933 static inline bool classof(const Instruction *I) {
934 return I->getOpcode() == Instruction::FCmp;
936 static inline bool classof(const Value *V) {
937 return isa<Instruction>(V) && classof(cast<Instruction>(V));
941 //===----------------------------------------------------------------------===//
943 //===----------------------------------------------------------------------===//
944 /// CallInst - This class represents a function call, abstracting a target
945 /// machine's calling convention. This class uses low bit of the SubClassData
946 /// field to indicate whether or not this is a tail call. The rest of the bits
947 /// hold the calling convention of the call.
950 class CallInst : public Instruction {
951 AttrListPtr AttributeList; ///< parameter attributes for call
952 CallInst(const CallInst &CI);
953 void init(Value *Func, Value* const *Params, unsigned NumParams);
954 void init(Value *Func, Value *Actual1, Value *Actual2);
955 void init(Value *Func, Value *Actual);
956 void init(Value *Func);
958 template<typename InputIterator>
959 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
960 const Twine &NameStr,
961 // This argument ensures that we have an iterator we can
962 // do arithmetic on in constant time
963 std::random_access_iterator_tag) {
964 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
966 // This requires that the iterator points to contiguous memory.
967 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
971 /// Construct a CallInst given a range of arguments. InputIterator
972 /// must be a random-access iterator pointing to contiguous storage
973 /// (e.g. a std::vector<>::iterator). Checks are made for
974 /// random-accessness but not for contiguous storage as that would
975 /// incur runtime overhead.
976 /// @brief Construct a CallInst from a range of arguments
977 template<typename InputIterator>
978 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
979 const Twine &NameStr, Instruction *InsertBefore);
981 /// Construct a CallInst given a range of arguments. InputIterator
982 /// must be a random-access iterator pointing to contiguous storage
983 /// (e.g. a std::vector<>::iterator). Checks are made for
984 /// random-accessness but not for contiguous storage as that would
985 /// incur runtime overhead.
986 /// @brief Construct a CallInst from a range of arguments
987 template<typename InputIterator>
988 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
989 const Twine &NameStr, BasicBlock *InsertAtEnd);
991 CallInst(Value *F, Value *Actual, const Twine &NameStr,
992 Instruction *InsertBefore);
993 CallInst(Value *F, Value *Actual, const Twine &NameStr,
994 BasicBlock *InsertAtEnd);
995 explicit CallInst(Value *F, const Twine &NameStr,
996 Instruction *InsertBefore);
997 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
999 template<typename InputIterator>
1000 static CallInst *Create(Value *Func,
1001 InputIterator ArgBegin, InputIterator ArgEnd,
1002 const Twine &NameStr = "",
1003 Instruction *InsertBefore = 0) {
1004 return new((unsigned)(ArgEnd - ArgBegin + 1))
1005 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1007 template<typename InputIterator>
1008 static CallInst *Create(Value *Func,
1009 InputIterator ArgBegin, InputIterator ArgEnd,
1010 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1011 return new((unsigned)(ArgEnd - ArgBegin + 1))
1012 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1014 static CallInst *Create(Value *F, Value *Actual,
1015 const Twine &NameStr = "",
1016 Instruction *InsertBefore = 0) {
1017 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1019 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1020 BasicBlock *InsertAtEnd) {
1021 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1023 static CallInst *Create(Value *F, const Twine &NameStr = "",
1024 Instruction *InsertBefore = 0) {
1025 return new(1) CallInst(F, NameStr, InsertBefore);
1027 static CallInst *Create(Value *F, const Twine &NameStr,
1028 BasicBlock *InsertAtEnd) {
1029 return new(1) CallInst(F, NameStr, InsertAtEnd);
1034 bool isTailCall() const { return SubclassData & 1; }
1035 void setTailCall(bool isTC = true) {
1036 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1039 virtual CallInst *clone(LLVMContext &Context) const;
1041 /// Provide fast operand accessors
1042 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1044 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1046 CallingConv::ID getCallingConv() const {
1047 return static_cast<CallingConv::ID>(SubclassData >> 1);
1049 void setCallingConv(CallingConv::ID CC) {
1050 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
1053 /// getAttributes - Return the parameter attributes for this call.
1055 const AttrListPtr &getAttributes() const { return AttributeList; }
1057 /// setAttributes - Set the parameter attributes for this call.
1059 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1061 /// addAttribute - adds the attribute to the list of attributes.
1062 void addAttribute(unsigned i, Attributes attr);
1064 /// removeAttribute - removes the attribute from the list of attributes.
1065 void removeAttribute(unsigned i, Attributes attr);
1067 /// @brief Determine whether the call or the callee has the given attribute.
1068 bool paramHasAttr(unsigned i, Attributes attr) const;
1070 /// @brief Extract the alignment for a call or parameter (0=unknown).
1071 unsigned getParamAlignment(unsigned i) const {
1072 return AttributeList.getParamAlignment(i);
1075 /// @brief Determine if the call does not access memory.
1076 bool doesNotAccessMemory() const {
1077 return paramHasAttr(~0, Attribute::ReadNone);
1079 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1080 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1081 else removeAttribute(~0, Attribute::ReadNone);
1084 /// @brief Determine if the call does not access or only reads memory.
1085 bool onlyReadsMemory() const {
1086 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1088 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1089 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1090 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1093 /// @brief Determine if the call cannot return.
1094 bool doesNotReturn() const {
1095 return paramHasAttr(~0, Attribute::NoReturn);
1097 void setDoesNotReturn(bool DoesNotReturn = true) {
1098 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1099 else removeAttribute(~0, Attribute::NoReturn);
1102 /// @brief Determine if the call cannot unwind.
1103 bool doesNotThrow() const {
1104 return paramHasAttr(~0, Attribute::NoUnwind);
1106 void setDoesNotThrow(bool DoesNotThrow = true) {
1107 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1108 else removeAttribute(~0, Attribute::NoUnwind);
1111 /// @brief Determine if the call returns a structure through first
1112 /// pointer argument.
1113 bool hasStructRetAttr() const {
1114 // Be friendly and also check the callee.
1115 return paramHasAttr(1, Attribute::StructRet);
1118 /// @brief Determine if any call argument is an aggregate passed by value.
1119 bool hasByValArgument() const {
1120 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1123 /// getCalledFunction - Return the function called, or null if this is an
1124 /// indirect function invocation.
1126 Function *getCalledFunction() const {
1127 return dyn_cast<Function>(Op<0>());
1130 /// getCalledValue - Get a pointer to the function that is invoked by this
1132 const Value *getCalledValue() const { return Op<0>(); }
1133 Value *getCalledValue() { return Op<0>(); }
1135 // Methods for support type inquiry through isa, cast, and dyn_cast:
1136 static inline bool classof(const CallInst *) { return true; }
1137 static inline bool classof(const Instruction *I) {
1138 return I->getOpcode() == Instruction::Call;
1140 static inline bool classof(const Value *V) {
1141 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1146 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1149 template<typename InputIterator>
1150 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1151 const Twine &NameStr, BasicBlock *InsertAtEnd)
1152 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1153 ->getElementType())->getReturnType(),
1155 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1156 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1157 init(Func, ArgBegin, ArgEnd, NameStr,
1158 typename std::iterator_traits<InputIterator>::iterator_category());
1161 template<typename InputIterator>
1162 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1163 const Twine &NameStr, Instruction *InsertBefore)
1164 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1165 ->getElementType())->getReturnType(),
1167 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1168 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1169 init(Func, ArgBegin, ArgEnd, NameStr,
1170 typename std::iterator_traits<InputIterator>::iterator_category());
1173 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1175 //===----------------------------------------------------------------------===//
1177 //===----------------------------------------------------------------------===//
1179 /// SelectInst - This class represents the LLVM 'select' instruction.
1181 class SelectInst : public Instruction {
1182 void init(Value *C, Value *S1, Value *S2) {
1183 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1189 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1190 Instruction *InsertBefore)
1191 : Instruction(S1->getType(), Instruction::Select,
1192 &Op<0>(), 3, InsertBefore) {
1196 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1197 BasicBlock *InsertAtEnd)
1198 : Instruction(S1->getType(), Instruction::Select,
1199 &Op<0>(), 3, InsertAtEnd) {
1204 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1205 const Twine &NameStr = "",
1206 Instruction *InsertBefore = 0) {
1207 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1209 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1210 const Twine &NameStr,
1211 BasicBlock *InsertAtEnd) {
1212 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1215 Value *getCondition() const { return Op<0>(); }
1216 Value *getTrueValue() const { return Op<1>(); }
1217 Value *getFalseValue() const { return Op<2>(); }
1219 /// areInvalidOperands - Return a string if the specified operands are invalid
1220 /// for a select operation, otherwise return null.
1221 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1223 /// Transparently provide more efficient getOperand methods.
1224 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1226 OtherOps getOpcode() const {
1227 return static_cast<OtherOps>(Instruction::getOpcode());
1230 virtual SelectInst *clone(LLVMContext &Context) const;
1232 // Methods for support type inquiry through isa, cast, and dyn_cast:
1233 static inline bool classof(const SelectInst *) { return true; }
1234 static inline bool classof(const Instruction *I) {
1235 return I->getOpcode() == Instruction::Select;
1237 static inline bool classof(const Value *V) {
1238 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1243 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1246 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1248 //===----------------------------------------------------------------------===//
1250 //===----------------------------------------------------------------------===//
1252 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1253 /// an argument of the specified type given a va_list and increments that list
1255 class VAArgInst : public UnaryInstruction {
1257 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1258 Instruction *InsertBefore = 0)
1259 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1262 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1263 BasicBlock *InsertAtEnd)
1264 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1268 virtual VAArgInst *clone(LLVMContext &Context) const;
1270 // Methods for support type inquiry through isa, cast, and dyn_cast:
1271 static inline bool classof(const VAArgInst *) { return true; }
1272 static inline bool classof(const Instruction *I) {
1273 return I->getOpcode() == VAArg;
1275 static inline bool classof(const Value *V) {
1276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1280 //===----------------------------------------------------------------------===//
1281 // ExtractElementInst Class
1282 //===----------------------------------------------------------------------===//
1284 /// ExtractElementInst - This instruction extracts a single (scalar)
1285 /// element from a VectorType value
1287 class ExtractElementInst : public Instruction {
1288 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1289 Instruction *InsertBefore = 0);
1290 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1291 BasicBlock *InsertAtEnd);
1293 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1294 const Twine &NameStr = "",
1295 Instruction *InsertBefore = 0) {
1296 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1298 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1299 const Twine &NameStr,
1300 BasicBlock *InsertAtEnd) {
1301 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1304 /// isValidOperands - Return true if an extractelement instruction can be
1305 /// formed with the specified operands.
1306 static bool isValidOperands(const Value *Vec, const Value *Idx);
1308 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1310 /// Transparently provide more efficient getOperand methods.
1311 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1313 // Methods for support type inquiry through isa, cast, and dyn_cast:
1314 static inline bool classof(const ExtractElementInst *) { return true; }
1315 static inline bool classof(const Instruction *I) {
1316 return I->getOpcode() == Instruction::ExtractElement;
1318 static inline bool classof(const Value *V) {
1319 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1324 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1327 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1329 //===----------------------------------------------------------------------===//
1330 // InsertElementInst Class
1331 //===----------------------------------------------------------------------===//
1333 /// InsertElementInst - This instruction inserts a single (scalar)
1334 /// element into a VectorType value
1336 class InsertElementInst : public Instruction {
1337 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1338 const Twine &NameStr = "",
1339 Instruction *InsertBefore = 0);
1340 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1341 const Twine &NameStr, BasicBlock *InsertAtEnd);
1343 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1344 const Twine &NameStr = "",
1345 Instruction *InsertBefore = 0) {
1346 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1348 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1349 const Twine &NameStr,
1350 BasicBlock *InsertAtEnd) {
1351 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1354 /// isValidOperands - Return true if an insertelement instruction can be
1355 /// formed with the specified operands.
1356 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1359 virtual InsertElementInst *clone(LLVMContext &Context) const;
1361 /// getType - Overload to return most specific vector type.
1363 const VectorType *getType() const {
1364 return reinterpret_cast<const VectorType*>(Instruction::getType());
1367 /// Transparently provide more efficient getOperand methods.
1368 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1370 // Methods for support type inquiry through isa, cast, and dyn_cast:
1371 static inline bool classof(const InsertElementInst *) { return true; }
1372 static inline bool classof(const Instruction *I) {
1373 return I->getOpcode() == Instruction::InsertElement;
1375 static inline bool classof(const Value *V) {
1376 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1381 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1384 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1386 //===----------------------------------------------------------------------===//
1387 // ShuffleVectorInst Class
1388 //===----------------------------------------------------------------------===//
1390 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1393 class ShuffleVectorInst : public Instruction {
1395 // allocate space for exactly three operands
1396 void *operator new(size_t s) {
1397 return User::operator new(s, 3);
1399 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1400 const Twine &NameStr = "",
1401 Instruction *InsertBefor = 0);
1402 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1403 const Twine &NameStr, BasicBlock *InsertAtEnd);
1405 /// isValidOperands - Return true if a shufflevector instruction can be
1406 /// formed with the specified operands.
1407 static bool isValidOperands(const Value *V1, const Value *V2,
1410 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1412 /// getType - Overload to return most specific vector type.
1414 const VectorType *getType() const {
1415 return reinterpret_cast<const VectorType*>(Instruction::getType());
1418 /// Transparently provide more efficient getOperand methods.
1419 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1421 /// getMaskValue - Return the index from the shuffle mask for the specified
1422 /// output result. This is either -1 if the element is undef or a number less
1423 /// than 2*numelements.
1424 int getMaskValue(unsigned i) const;
1426 // Methods for support type inquiry through isa, cast, and dyn_cast:
1427 static inline bool classof(const ShuffleVectorInst *) { return true; }
1428 static inline bool classof(const Instruction *I) {
1429 return I->getOpcode() == Instruction::ShuffleVector;
1431 static inline bool classof(const Value *V) {
1432 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1437 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1440 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1442 //===----------------------------------------------------------------------===//
1443 // ExtractValueInst Class
1444 //===----------------------------------------------------------------------===//
1446 /// ExtractValueInst - This instruction extracts a struct member or array
1447 /// element value from an aggregate value.
1449 class ExtractValueInst : public UnaryInstruction {
1450 SmallVector<unsigned, 4> Indices;
1452 ExtractValueInst(const ExtractValueInst &EVI);
1453 void init(const unsigned *Idx, unsigned NumIdx,
1454 const Twine &NameStr);
1455 void init(unsigned Idx, const Twine &NameStr);
1457 template<typename InputIterator>
1458 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1459 const Twine &NameStr,
1460 // This argument ensures that we have an iterator we can
1461 // do arithmetic on in constant time
1462 std::random_access_iterator_tag) {
1463 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1465 // There's no fundamental reason why we require at least one index
1466 // (other than weirdness with &*IdxBegin being invalid; see
1467 // getelementptr's init routine for example). But there's no
1468 // present need to support it.
1469 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1471 // This requires that the iterator points to contiguous memory.
1472 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1473 // we have to build an array here
1476 /// getIndexedType - Returns the type of the element that would be extracted
1477 /// with an extractvalue instruction with the specified parameters.
1479 /// Null is returned if the indices are invalid for the specified
1482 static const Type *getIndexedType(const Type *Agg,
1483 const unsigned *Idx, unsigned NumIdx);
1485 template<typename InputIterator>
1486 static const Type *getIndexedType(const Type *Ptr,
1487 InputIterator IdxBegin,
1488 InputIterator IdxEnd,
1489 // This argument ensures that we
1490 // have an iterator we can do
1491 // arithmetic on in constant time
1492 std::random_access_iterator_tag) {
1493 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1496 // This requires that the iterator points to contiguous memory.
1497 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1499 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1502 /// Constructors - Create a extractvalue instruction with a base aggregate
1503 /// value and a list of indices. The first ctor can optionally insert before
1504 /// an existing instruction, the second appends the new instruction to the
1505 /// specified BasicBlock.
1506 template<typename InputIterator>
1507 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1508 InputIterator IdxEnd,
1509 const Twine &NameStr,
1510 Instruction *InsertBefore);
1511 template<typename InputIterator>
1512 inline ExtractValueInst(Value *Agg,
1513 InputIterator IdxBegin, InputIterator IdxEnd,
1514 const Twine &NameStr, BasicBlock *InsertAtEnd);
1516 // allocate space for exactly one operand
1517 void *operator new(size_t s) {
1518 return User::operator new(s, 1);
1522 template<typename InputIterator>
1523 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1524 InputIterator IdxEnd,
1525 const Twine &NameStr = "",
1526 Instruction *InsertBefore = 0) {
1528 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1530 template<typename InputIterator>
1531 static ExtractValueInst *Create(Value *Agg,
1532 InputIterator IdxBegin, InputIterator IdxEnd,
1533 const Twine &NameStr,
1534 BasicBlock *InsertAtEnd) {
1535 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1538 /// Constructors - These two creators are convenience methods because one
1539 /// index extractvalue instructions are much more common than those with
1541 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1542 const Twine &NameStr = "",
1543 Instruction *InsertBefore = 0) {
1544 unsigned Idxs[1] = { Idx };
1545 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1547 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1548 const Twine &NameStr,
1549 BasicBlock *InsertAtEnd) {
1550 unsigned Idxs[1] = { Idx };
1551 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1554 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1556 /// getIndexedType - Returns the type of the element that would be extracted
1557 /// with an extractvalue instruction with the specified parameters.
1559 /// Null is returned if the indices are invalid for the specified
1562 template<typename InputIterator>
1563 static const Type *getIndexedType(const Type *Ptr,
1564 InputIterator IdxBegin,
1565 InputIterator IdxEnd) {
1566 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1567 typename std::iterator_traits<InputIterator>::
1568 iterator_category());
1570 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1572 typedef const unsigned* idx_iterator;
1573 inline idx_iterator idx_begin() const { return Indices.begin(); }
1574 inline idx_iterator idx_end() const { return Indices.end(); }
1576 Value *getAggregateOperand() {
1577 return getOperand(0);
1579 const Value *getAggregateOperand() const {
1580 return getOperand(0);
1582 static unsigned getAggregateOperandIndex() {
1583 return 0U; // get index for modifying correct operand
1586 unsigned getNumIndices() const { // Note: always non-negative
1587 return (unsigned)Indices.size();
1590 bool hasIndices() const {
1594 // Methods for support type inquiry through isa, cast, and dyn_cast:
1595 static inline bool classof(const ExtractValueInst *) { return true; }
1596 static inline bool classof(const Instruction *I) {
1597 return I->getOpcode() == Instruction::ExtractValue;
1599 static inline bool classof(const Value *V) {
1600 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1604 template<typename InputIterator>
1605 ExtractValueInst::ExtractValueInst(Value *Agg,
1606 InputIterator IdxBegin,
1607 InputIterator IdxEnd,
1608 const Twine &NameStr,
1609 Instruction *InsertBefore)
1610 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1612 ExtractValue, Agg, InsertBefore) {
1613 init(IdxBegin, IdxEnd, NameStr,
1614 typename std::iterator_traits<InputIterator>::iterator_category());
1616 template<typename InputIterator>
1617 ExtractValueInst::ExtractValueInst(Value *Agg,
1618 InputIterator IdxBegin,
1619 InputIterator IdxEnd,
1620 const Twine &NameStr,
1621 BasicBlock *InsertAtEnd)
1622 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1624 ExtractValue, Agg, InsertAtEnd) {
1625 init(IdxBegin, IdxEnd, NameStr,
1626 typename std::iterator_traits<InputIterator>::iterator_category());
1630 //===----------------------------------------------------------------------===//
1631 // InsertValueInst Class
1632 //===----------------------------------------------------------------------===//
1634 /// InsertValueInst - This instruction inserts a struct field of array element
1635 /// value into an aggregate value.
1637 class InsertValueInst : public Instruction {
1638 SmallVector<unsigned, 4> Indices;
1640 void *operator new(size_t, unsigned); // Do not implement
1641 InsertValueInst(const InsertValueInst &IVI);
1642 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1643 const Twine &NameStr);
1644 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1646 template<typename InputIterator>
1647 void init(Value *Agg, Value *Val,
1648 InputIterator IdxBegin, InputIterator IdxEnd,
1649 const Twine &NameStr,
1650 // This argument ensures that we have an iterator we can
1651 // do arithmetic on in constant time
1652 std::random_access_iterator_tag) {
1653 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1655 // There's no fundamental reason why we require at least one index
1656 // (other than weirdness with &*IdxBegin being invalid; see
1657 // getelementptr's init routine for example). But there's no
1658 // present need to support it.
1659 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1661 // This requires that the iterator points to contiguous memory.
1662 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1663 // we have to build an array here
1666 /// Constructors - Create a insertvalue instruction with a base aggregate
1667 /// value, a value to insert, and a list of indices. The first ctor can
1668 /// optionally insert before an existing instruction, the second appends
1669 /// the new instruction to the specified BasicBlock.
1670 template<typename InputIterator>
1671 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1672 InputIterator IdxEnd,
1673 const Twine &NameStr,
1674 Instruction *InsertBefore);
1675 template<typename InputIterator>
1676 inline InsertValueInst(Value *Agg, Value *Val,
1677 InputIterator IdxBegin, InputIterator IdxEnd,
1678 const Twine &NameStr, BasicBlock *InsertAtEnd);
1680 /// Constructors - These two constructors are convenience methods because one
1681 /// and two index insertvalue instructions are so common.
1682 InsertValueInst(Value *Agg, Value *Val,
1683 unsigned Idx, const Twine &NameStr = "",
1684 Instruction *InsertBefore = 0);
1685 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1686 const Twine &NameStr, BasicBlock *InsertAtEnd);
1688 // allocate space for exactly two operands
1689 void *operator new(size_t s) {
1690 return User::operator new(s, 2);
1693 template<typename InputIterator>
1694 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1695 InputIterator IdxEnd,
1696 const Twine &NameStr = "",
1697 Instruction *InsertBefore = 0) {
1698 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1699 NameStr, InsertBefore);
1701 template<typename InputIterator>
1702 static InsertValueInst *Create(Value *Agg, Value *Val,
1703 InputIterator IdxBegin, InputIterator IdxEnd,
1704 const Twine &NameStr,
1705 BasicBlock *InsertAtEnd) {
1706 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1707 NameStr, InsertAtEnd);
1710 /// Constructors - These two creators are convenience methods because one
1711 /// index insertvalue instructions are much more common than those with
1713 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1714 const Twine &NameStr = "",
1715 Instruction *InsertBefore = 0) {
1716 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1718 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1719 const Twine &NameStr,
1720 BasicBlock *InsertAtEnd) {
1721 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1724 virtual InsertValueInst *clone(LLVMContext &Context) const;
1726 /// Transparently provide more efficient getOperand methods.
1727 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1729 typedef const unsigned* idx_iterator;
1730 inline idx_iterator idx_begin() const { return Indices.begin(); }
1731 inline idx_iterator idx_end() const { return Indices.end(); }
1733 Value *getAggregateOperand() {
1734 return getOperand(0);
1736 const Value *getAggregateOperand() const {
1737 return getOperand(0);
1739 static unsigned getAggregateOperandIndex() {
1740 return 0U; // get index for modifying correct operand
1743 Value *getInsertedValueOperand() {
1744 return getOperand(1);
1746 const Value *getInsertedValueOperand() const {
1747 return getOperand(1);
1749 static unsigned getInsertedValueOperandIndex() {
1750 return 1U; // get index for modifying correct operand
1753 unsigned getNumIndices() const { // Note: always non-negative
1754 return (unsigned)Indices.size();
1757 bool hasIndices() const {
1761 // Methods for support type inquiry through isa, cast, and dyn_cast:
1762 static inline bool classof(const InsertValueInst *) { return true; }
1763 static inline bool classof(const Instruction *I) {
1764 return I->getOpcode() == Instruction::InsertValue;
1766 static inline bool classof(const Value *V) {
1767 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1772 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1775 template<typename InputIterator>
1776 InsertValueInst::InsertValueInst(Value *Agg,
1778 InputIterator IdxBegin,
1779 InputIterator IdxEnd,
1780 const Twine &NameStr,
1781 Instruction *InsertBefore)
1782 : Instruction(Agg->getType(), InsertValue,
1783 OperandTraits<InsertValueInst>::op_begin(this),
1785 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1786 typename std::iterator_traits<InputIterator>::iterator_category());
1788 template<typename InputIterator>
1789 InsertValueInst::InsertValueInst(Value *Agg,
1791 InputIterator IdxBegin,
1792 InputIterator IdxEnd,
1793 const Twine &NameStr,
1794 BasicBlock *InsertAtEnd)
1795 : Instruction(Agg->getType(), InsertValue,
1796 OperandTraits<InsertValueInst>::op_begin(this),
1798 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1799 typename std::iterator_traits<InputIterator>::iterator_category());
1802 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1804 //===----------------------------------------------------------------------===//
1806 //===----------------------------------------------------------------------===//
1808 // PHINode - The PHINode class is used to represent the magical mystical PHI
1809 // node, that can not exist in nature, but can be synthesized in a computer
1810 // scientist's overactive imagination.
1812 class PHINode : public Instruction {
1813 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1814 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1815 /// the number actually in use.
1816 unsigned ReservedSpace;
1817 PHINode(const PHINode &PN);
1818 // allocate space for exactly zero operands
1819 void *operator new(size_t s) {
1820 return User::operator new(s, 0);
1822 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1823 Instruction *InsertBefore = 0)
1824 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1829 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1830 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1835 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1836 Instruction *InsertBefore = 0) {
1837 return new PHINode(Ty, NameStr, InsertBefore);
1839 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1840 BasicBlock *InsertAtEnd) {
1841 return new PHINode(Ty, NameStr, InsertAtEnd);
1845 /// reserveOperandSpace - This method can be used to avoid repeated
1846 /// reallocation of PHI operand lists by reserving space for the correct
1847 /// number of operands before adding them. Unlike normal vector reserves,
1848 /// this method can also be used to trim the operand space.
1849 void reserveOperandSpace(unsigned NumValues) {
1850 resizeOperands(NumValues*2);
1853 virtual PHINode *clone(LLVMContext &Context) const;
1855 /// Provide fast operand accessors
1856 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1858 /// getNumIncomingValues - Return the number of incoming edges
1860 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1862 /// getIncomingValue - Return incoming value number x
1864 Value *getIncomingValue(unsigned i) const {
1865 assert(i*2 < getNumOperands() && "Invalid value number!");
1866 return getOperand(i*2);
1868 void setIncomingValue(unsigned i, Value *V) {
1869 assert(i*2 < getNumOperands() && "Invalid value number!");
1872 static unsigned getOperandNumForIncomingValue(unsigned i) {
1875 static unsigned getIncomingValueNumForOperand(unsigned i) {
1876 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1880 /// getIncomingBlock - Return incoming basic block corresponding
1881 /// to value use iterator
1883 template <typename U>
1884 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1885 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1886 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1888 /// getIncomingBlock - Return incoming basic block number x
1890 BasicBlock *getIncomingBlock(unsigned i) const {
1891 return static_cast<BasicBlock*>(getOperand(i*2+1));
1893 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1894 setOperand(i*2+1, BB);
1896 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1899 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1900 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1904 /// addIncoming - Add an incoming value to the end of the PHI list
1906 void addIncoming(Value *V, BasicBlock *BB) {
1907 assert(V && "PHI node got a null value!");
1908 assert(BB && "PHI node got a null basic block!");
1909 assert(getType() == V->getType() &&
1910 "All operands to PHI node must be the same type as the PHI node!");
1911 unsigned OpNo = NumOperands;
1912 if (OpNo+2 > ReservedSpace)
1913 resizeOperands(0); // Get more space!
1914 // Initialize some new operands.
1915 NumOperands = OpNo+2;
1916 OperandList[OpNo] = V;
1917 OperandList[OpNo+1] = BB;
1920 /// removeIncomingValue - Remove an incoming value. This is useful if a
1921 /// predecessor basic block is deleted. The value removed is returned.
1923 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1924 /// is true), the PHI node is destroyed and any uses of it are replaced with
1925 /// dummy values. The only time there should be zero incoming values to a PHI
1926 /// node is when the block is dead, so this strategy is sound.
1928 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1930 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1931 int Idx = getBasicBlockIndex(BB);
1932 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1933 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1936 /// getBasicBlockIndex - Return the first index of the specified basic
1937 /// block in the value list for this PHI. Returns -1 if no instance.
1939 int getBasicBlockIndex(const BasicBlock *BB) const {
1940 Use *OL = OperandList;
1941 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1942 if (OL[i+1].get() == BB) return i/2;
1946 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1947 return getIncomingValue(getBasicBlockIndex(BB));
1950 /// hasConstantValue - If the specified PHI node always merges together the
1951 /// same value, return the value, otherwise return null.
1953 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1955 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1956 static inline bool classof(const PHINode *) { return true; }
1957 static inline bool classof(const Instruction *I) {
1958 return I->getOpcode() == Instruction::PHI;
1960 static inline bool classof(const Value *V) {
1961 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1964 void resizeOperands(unsigned NumOperands);
1968 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1971 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1974 //===----------------------------------------------------------------------===//
1976 //===----------------------------------------------------------------------===//
1978 //===---------------------------------------------------------------------------
1979 /// ReturnInst - Return a value (possibly void), from a function. Execution
1980 /// does not continue in this function any longer.
1982 class ReturnInst : public TerminatorInst {
1983 ReturnInst(const ReturnInst &RI);
1986 // ReturnInst constructors:
1987 // ReturnInst() - 'ret void' instruction
1988 // ReturnInst( null) - 'ret void' instruction
1989 // ReturnInst(Value* X) - 'ret X' instruction
1990 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1991 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1992 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1993 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1995 // NOTE: If the Value* passed is of type void then the constructor behaves as
1996 // if it was passed NULL.
1997 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1998 Instruction *InsertBefore = 0);
1999 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2000 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2002 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2003 Instruction *InsertBefore = 0) {
2004 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2006 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2007 BasicBlock *InsertAtEnd) {
2008 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2010 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2011 return new(0) ReturnInst(C, InsertAtEnd);
2013 virtual ~ReturnInst();
2015 virtual ReturnInst *clone(LLVMContext &Context) const;
2017 /// Provide fast operand accessors
2018 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2020 /// Convenience accessor
2021 Value *getReturnValue(unsigned n = 0) const {
2022 return n < getNumOperands()
2027 unsigned getNumSuccessors() const { return 0; }
2029 // Methods for support type inquiry through isa, cast, and dyn_cast:
2030 static inline bool classof(const ReturnInst *) { return true; }
2031 static inline bool classof(const Instruction *I) {
2032 return (I->getOpcode() == Instruction::Ret);
2034 static inline bool classof(const Value *V) {
2035 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2038 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2039 virtual unsigned getNumSuccessorsV() const;
2040 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2044 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2047 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2049 //===----------------------------------------------------------------------===//
2051 //===----------------------------------------------------------------------===//
2053 //===---------------------------------------------------------------------------
2054 /// BranchInst - Conditional or Unconditional Branch instruction.
2056 class BranchInst : public TerminatorInst {
2057 /// Ops list - Branches are strange. The operands are ordered:
2058 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2059 /// they don't have to check for cond/uncond branchness. These are mostly
2060 /// accessed relative from op_end().
2061 BranchInst(const BranchInst &BI);
2063 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2064 // BranchInst(BB *B) - 'br B'
2065 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2066 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2067 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2068 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2069 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2070 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2071 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2072 Instruction *InsertBefore = 0);
2073 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2074 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2075 BasicBlock *InsertAtEnd);
2077 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2078 return new(1, true) BranchInst(IfTrue, InsertBefore);
2080 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2081 Value *Cond, Instruction *InsertBefore = 0) {
2082 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2084 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2085 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2087 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2088 Value *Cond, BasicBlock *InsertAtEnd) {
2089 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2094 /// Transparently provide more efficient getOperand methods.
2095 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2097 virtual BranchInst *clone(LLVMContext &Context) const;
2099 bool isUnconditional() const { return getNumOperands() == 1; }
2100 bool isConditional() const { return getNumOperands() == 3; }
2102 Value *getCondition() const {
2103 assert(isConditional() && "Cannot get condition of an uncond branch!");
2107 void setCondition(Value *V) {
2108 assert(isConditional() && "Cannot set condition of unconditional branch!");
2112 // setUnconditionalDest - Change the current branch to an unconditional branch
2113 // targeting the specified block.
2114 // FIXME: Eliminate this ugly method.
2115 void setUnconditionalDest(BasicBlock *Dest) {
2117 if (isConditional()) { // Convert this to an uncond branch.
2121 OperandList = op_begin();
2125 unsigned getNumSuccessors() const { return 1+isConditional(); }
2127 BasicBlock *getSuccessor(unsigned i) const {
2128 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2129 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2132 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2133 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2134 *(&Op<-1>() - idx) = NewSucc;
2137 // Methods for support type inquiry through isa, cast, and dyn_cast:
2138 static inline bool classof(const BranchInst *) { return true; }
2139 static inline bool classof(const Instruction *I) {
2140 return (I->getOpcode() == Instruction::Br);
2142 static inline bool classof(const Value *V) {
2143 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2146 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2147 virtual unsigned getNumSuccessorsV() const;
2148 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2152 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2154 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2156 //===----------------------------------------------------------------------===//
2158 //===----------------------------------------------------------------------===//
2160 //===---------------------------------------------------------------------------
2161 /// SwitchInst - Multiway switch
2163 class SwitchInst : public TerminatorInst {
2164 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2165 unsigned ReservedSpace;
2166 // Operand[0] = Value to switch on
2167 // Operand[1] = Default basic block destination
2168 // Operand[2n ] = Value to match
2169 // Operand[2n+1] = BasicBlock to go to on match
2170 SwitchInst(const SwitchInst &RI);
2171 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2172 void resizeOperands(unsigned No);
2173 // allocate space for exactly zero operands
2174 void *operator new(size_t s) {
2175 return User::operator new(s, 0);
2177 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2178 /// switch on and a default destination. The number of additional cases can
2179 /// be specified here to make memory allocation more efficient. This
2180 /// constructor can also autoinsert before another instruction.
2181 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2182 Instruction *InsertBefore = 0);
2184 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2185 /// switch on and a default destination. The number of additional cases can
2186 /// be specified here to make memory allocation more efficient. This
2187 /// constructor also autoinserts at the end of the specified BasicBlock.
2188 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2189 BasicBlock *InsertAtEnd);
2191 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2192 unsigned NumCases, Instruction *InsertBefore = 0) {
2193 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2195 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2196 unsigned NumCases, BasicBlock *InsertAtEnd) {
2197 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2201 /// Provide fast operand accessors
2202 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2204 // Accessor Methods for Switch stmt
2205 Value *getCondition() const { return getOperand(0); }
2206 void setCondition(Value *V) { setOperand(0, V); }
2208 BasicBlock *getDefaultDest() const {
2209 return cast<BasicBlock>(getOperand(1));
2212 /// getNumCases - return the number of 'cases' in this switch instruction.
2213 /// Note that case #0 is always the default case.
2214 unsigned getNumCases() const {
2215 return getNumOperands()/2;
2218 /// getCaseValue - Return the specified case value. Note that case #0, the
2219 /// default destination, does not have a case value.
2220 ConstantInt *getCaseValue(unsigned i) {
2221 assert(i && i < getNumCases() && "Illegal case value to get!");
2222 return getSuccessorValue(i);
2225 /// getCaseValue - Return the specified case value. Note that case #0, the
2226 /// default destination, does not have a case value.
2227 const ConstantInt *getCaseValue(unsigned i) const {
2228 assert(i && i < getNumCases() && "Illegal case value to get!");
2229 return getSuccessorValue(i);
2232 /// findCaseValue - Search all of the case values for the specified constant.
2233 /// If it is explicitly handled, return the case number of it, otherwise
2234 /// return 0 to indicate that it is handled by the default handler.
2235 unsigned findCaseValue(const ConstantInt *C) const {
2236 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2237 if (getCaseValue(i) == C)
2242 /// findCaseDest - Finds the unique case value for a given successor. Returns
2243 /// null if the successor is not found, not unique, or is the default case.
2244 ConstantInt *findCaseDest(BasicBlock *BB) {
2245 if (BB == getDefaultDest()) return NULL;
2247 ConstantInt *CI = NULL;
2248 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2249 if (getSuccessor(i) == BB) {
2250 if (CI) return NULL; // Multiple cases lead to BB.
2251 else CI = getCaseValue(i);
2257 /// addCase - Add an entry to the switch instruction...
2259 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2261 /// removeCase - This method removes the specified successor from the switch
2262 /// instruction. Note that this cannot be used to remove the default
2263 /// destination (successor #0).
2265 void removeCase(unsigned idx);
2267 virtual SwitchInst *clone(LLVMContext &Context) const;
2269 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2270 BasicBlock *getSuccessor(unsigned idx) const {
2271 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2272 return cast<BasicBlock>(getOperand(idx*2+1));
2274 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2275 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2276 setOperand(idx*2+1, NewSucc);
2279 // getSuccessorValue - Return the value associated with the specified
2281 ConstantInt *getSuccessorValue(unsigned idx) const {
2282 assert(idx < getNumSuccessors() && "Successor # out of range!");
2283 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2286 // Methods for support type inquiry through isa, cast, and dyn_cast:
2287 static inline bool classof(const SwitchInst *) { return true; }
2288 static inline bool classof(const Instruction *I) {
2289 return I->getOpcode() == Instruction::Switch;
2291 static inline bool classof(const Value *V) {
2292 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2295 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2296 virtual unsigned getNumSuccessorsV() const;
2297 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2301 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2304 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2307 //===----------------------------------------------------------------------===//
2309 //===----------------------------------------------------------------------===//
2311 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2312 /// calling convention of the call.
2314 class InvokeInst : public TerminatorInst {
2315 AttrListPtr AttributeList;
2316 InvokeInst(const InvokeInst &BI);
2317 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2318 Value* const *Args, unsigned NumArgs);
2320 template<typename InputIterator>
2321 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2322 InputIterator ArgBegin, InputIterator ArgEnd,
2323 const Twine &NameStr,
2324 // This argument ensures that we have an iterator we can
2325 // do arithmetic on in constant time
2326 std::random_access_iterator_tag) {
2327 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2329 // This requires that the iterator points to contiguous memory.
2330 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2334 /// Construct an InvokeInst given a range of arguments.
2335 /// InputIterator must be a random-access iterator pointing to
2336 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2337 /// made for random-accessness but not for contiguous storage as
2338 /// that would incur runtime overhead.
2340 /// @brief Construct an InvokeInst from a range of arguments
2341 template<typename InputIterator>
2342 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2343 InputIterator ArgBegin, InputIterator ArgEnd,
2345 const Twine &NameStr, Instruction *InsertBefore);
2347 /// Construct an InvokeInst given a range of arguments.
2348 /// InputIterator must be a random-access iterator pointing to
2349 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2350 /// made for random-accessness but not for contiguous storage as
2351 /// that would incur runtime overhead.
2353 /// @brief Construct an InvokeInst from a range of arguments
2354 template<typename InputIterator>
2355 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2356 InputIterator ArgBegin, InputIterator ArgEnd,
2358 const Twine &NameStr, BasicBlock *InsertAtEnd);
2360 template<typename InputIterator>
2361 static InvokeInst *Create(Value *Func,
2362 BasicBlock *IfNormal, BasicBlock *IfException,
2363 InputIterator ArgBegin, InputIterator ArgEnd,
2364 const Twine &NameStr = "",
2365 Instruction *InsertBefore = 0) {
2366 unsigned Values(ArgEnd - ArgBegin + 3);
2367 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2368 Values, NameStr, InsertBefore);
2370 template<typename InputIterator>
2371 static InvokeInst *Create(Value *Func,
2372 BasicBlock *IfNormal, BasicBlock *IfException,
2373 InputIterator ArgBegin, InputIterator ArgEnd,
2374 const Twine &NameStr,
2375 BasicBlock *InsertAtEnd) {
2376 unsigned Values(ArgEnd - ArgBegin + 3);
2377 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2378 Values, NameStr, InsertAtEnd);
2381 virtual InvokeInst *clone(LLVMContext &Context) const;
2383 /// Provide fast operand accessors
2384 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2386 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2388 CallingConv::ID getCallingConv() const {
2389 return static_cast<CallingConv::ID>(SubclassData);
2391 void setCallingConv(CallingConv::ID CC) {
2392 SubclassData = static_cast<unsigned>(CC);
2395 /// getAttributes - Return the parameter attributes for this invoke.
2397 const AttrListPtr &getAttributes() const { return AttributeList; }
2399 /// setAttributes - Set the parameter attributes for this invoke.
2401 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2403 /// addAttribute - adds the attribute to the list of attributes.
2404 void addAttribute(unsigned i, Attributes attr);
2406 /// removeAttribute - removes the attribute from the list of attributes.
2407 void removeAttribute(unsigned i, Attributes attr);
2409 /// @brief Determine whether the call or the callee has the given attribute.
2410 bool paramHasAttr(unsigned i, Attributes attr) const;
2412 /// @brief Extract the alignment for a call or parameter (0=unknown).
2413 unsigned getParamAlignment(unsigned i) const {
2414 return AttributeList.getParamAlignment(i);
2417 /// @brief Determine if the call does not access memory.
2418 bool doesNotAccessMemory() const {
2419 return paramHasAttr(~0, Attribute::ReadNone);
2421 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2422 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2423 else removeAttribute(~0, Attribute::ReadNone);
2426 /// @brief Determine if the call does not access or only reads memory.
2427 bool onlyReadsMemory() const {
2428 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2430 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2431 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2432 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2435 /// @brief Determine if the call cannot return.
2436 bool doesNotReturn() const {
2437 return paramHasAttr(~0, Attribute::NoReturn);
2439 void setDoesNotReturn(bool DoesNotReturn = true) {
2440 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2441 else removeAttribute(~0, Attribute::NoReturn);
2444 /// @brief Determine if the call cannot unwind.
2445 bool doesNotThrow() const {
2446 return paramHasAttr(~0, Attribute::NoUnwind);
2448 void setDoesNotThrow(bool DoesNotThrow = true) {
2449 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2450 else removeAttribute(~0, Attribute::NoUnwind);
2453 /// @brief Determine if the call returns a structure through first
2454 /// pointer argument.
2455 bool hasStructRetAttr() const {
2456 // Be friendly and also check the callee.
2457 return paramHasAttr(1, Attribute::StructRet);
2460 /// @brief Determine if any call argument is an aggregate passed by value.
2461 bool hasByValArgument() const {
2462 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2465 /// getCalledFunction - Return the function called, or null if this is an
2466 /// indirect function invocation.
2468 Function *getCalledFunction() const {
2469 return dyn_cast<Function>(Op<-3>());
2472 /// getCalledValue - Get a pointer to the function that is invoked by this
2474 const Value *getCalledValue() const { return Op<-3>(); }
2475 Value *getCalledValue() { return Op<-3>(); }
2477 // get*Dest - Return the destination basic blocks...
2478 BasicBlock *getNormalDest() const {
2479 return cast<BasicBlock>(Op<-2>());
2481 BasicBlock *getUnwindDest() const {
2482 return cast<BasicBlock>(Op<-1>());
2484 void setNormalDest(BasicBlock *B) {
2487 void setUnwindDest(BasicBlock *B) {
2491 BasicBlock *getSuccessor(unsigned i) const {
2492 assert(i < 2 && "Successor # out of range for invoke!");
2493 return i == 0 ? getNormalDest() : getUnwindDest();
2496 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2497 assert(idx < 2 && "Successor # out of range for invoke!");
2498 *(&Op<-2>() + idx) = NewSucc;
2501 unsigned getNumSuccessors() const { return 2; }
2503 // Methods for support type inquiry through isa, cast, and dyn_cast:
2504 static inline bool classof(const InvokeInst *) { return true; }
2505 static inline bool classof(const Instruction *I) {
2506 return (I->getOpcode() == Instruction::Invoke);
2508 static inline bool classof(const Value *V) {
2509 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2513 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2514 virtual unsigned getNumSuccessorsV() const;
2515 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2519 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2522 template<typename InputIterator>
2523 InvokeInst::InvokeInst(Value *Func,
2524 BasicBlock *IfNormal, BasicBlock *IfException,
2525 InputIterator ArgBegin, InputIterator ArgEnd,
2527 const Twine &NameStr, Instruction *InsertBefore)
2528 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2529 ->getElementType())->getReturnType(),
2530 Instruction::Invoke,
2531 OperandTraits<InvokeInst>::op_end(this) - Values,
2532 Values, InsertBefore) {
2533 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2534 typename std::iterator_traits<InputIterator>::iterator_category());
2536 template<typename InputIterator>
2537 InvokeInst::InvokeInst(Value *Func,
2538 BasicBlock *IfNormal, BasicBlock *IfException,
2539 InputIterator ArgBegin, InputIterator ArgEnd,
2541 const Twine &NameStr, BasicBlock *InsertAtEnd)
2542 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2543 ->getElementType())->getReturnType(),
2544 Instruction::Invoke,
2545 OperandTraits<InvokeInst>::op_end(this) - Values,
2546 Values, InsertAtEnd) {
2547 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2548 typename std::iterator_traits<InputIterator>::iterator_category());
2551 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2553 //===----------------------------------------------------------------------===//
2555 //===----------------------------------------------------------------------===//
2557 //===---------------------------------------------------------------------------
2558 /// UnwindInst - Immediately exit the current function, unwinding the stack
2559 /// until an invoke instruction is found.
2561 class UnwindInst : public TerminatorInst {
2562 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2564 // allocate space for exactly zero operands
2565 void *operator new(size_t s) {
2566 return User::operator new(s, 0);
2568 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2569 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2571 virtual UnwindInst *clone(LLVMContext &Context) const;
2573 unsigned getNumSuccessors() const { return 0; }
2575 // Methods for support type inquiry through isa, cast, and dyn_cast:
2576 static inline bool classof(const UnwindInst *) { return true; }
2577 static inline bool classof(const Instruction *I) {
2578 return I->getOpcode() == Instruction::Unwind;
2580 static inline bool classof(const Value *V) {
2581 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2584 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2585 virtual unsigned getNumSuccessorsV() const;
2586 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2589 //===----------------------------------------------------------------------===//
2590 // UnreachableInst Class
2591 //===----------------------------------------------------------------------===//
2593 //===---------------------------------------------------------------------------
2594 /// UnreachableInst - This function has undefined behavior. In particular, the
2595 /// presence of this instruction indicates some higher level knowledge that the
2596 /// end of the block cannot be reached.
2598 class UnreachableInst : public TerminatorInst {
2599 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2601 // allocate space for exactly zero operands
2602 void *operator new(size_t s) {
2603 return User::operator new(s, 0);
2605 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2606 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2608 virtual UnreachableInst *clone(LLVMContext &Context) const;
2610 unsigned getNumSuccessors() const { return 0; }
2612 // Methods for support type inquiry through isa, cast, and dyn_cast:
2613 static inline bool classof(const UnreachableInst *) { return true; }
2614 static inline bool classof(const Instruction *I) {
2615 return I->getOpcode() == Instruction::Unreachable;
2617 static inline bool classof(const Value *V) {
2618 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2621 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2622 virtual unsigned getNumSuccessorsV() const;
2623 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2626 //===----------------------------------------------------------------------===//
2628 //===----------------------------------------------------------------------===//
2630 /// @brief This class represents a truncation of integer types.
2631 class TruncInst : public CastInst {
2633 /// @brief Constructor with insert-before-instruction semantics
2635 Value *S, ///< The value to be truncated
2636 const Type *Ty, ///< The (smaller) type to truncate to
2637 const Twine &NameStr = "", ///< A name for the new instruction
2638 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2641 /// @brief Constructor with insert-at-end-of-block semantics
2643 Value *S, ///< The value to be truncated
2644 const Type *Ty, ///< The (smaller) type to truncate to
2645 const Twine &NameStr, ///< A name for the new instruction
2646 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2649 /// @brief Clone an identical TruncInst
2650 virtual TruncInst *clone(LLVMContext &Context) const;
2652 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2653 static inline bool classof(const TruncInst *) { return true; }
2654 static inline bool classof(const Instruction *I) {
2655 return I->getOpcode() == Trunc;
2657 static inline bool classof(const Value *V) {
2658 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2662 //===----------------------------------------------------------------------===//
2664 //===----------------------------------------------------------------------===//
2666 /// @brief This class represents zero extension of integer types.
2667 class ZExtInst : public CastInst {
2669 /// @brief Constructor with insert-before-instruction semantics
2671 Value *S, ///< The value to be zero extended
2672 const Type *Ty, ///< The type to zero extend to
2673 const Twine &NameStr = "", ///< A name for the new instruction
2674 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2677 /// @brief Constructor with insert-at-end semantics.
2679 Value *S, ///< The value to be zero extended
2680 const Type *Ty, ///< The type to zero extend to
2681 const Twine &NameStr, ///< A name for the new instruction
2682 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2685 /// @brief Clone an identical ZExtInst
2686 virtual ZExtInst *clone(LLVMContext &Context) const;
2688 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2689 static inline bool classof(const ZExtInst *) { return true; }
2690 static inline bool classof(const Instruction *I) {
2691 return I->getOpcode() == ZExt;
2693 static inline bool classof(const Value *V) {
2694 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2698 //===----------------------------------------------------------------------===//
2700 //===----------------------------------------------------------------------===//
2702 /// @brief This class represents a sign extension of integer types.
2703 class SExtInst : public CastInst {
2705 /// @brief Constructor with insert-before-instruction semantics
2707 Value *S, ///< The value to be sign extended
2708 const Type *Ty, ///< The type to sign extend to
2709 const Twine &NameStr = "", ///< A name for the new instruction
2710 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2713 /// @brief Constructor with insert-at-end-of-block semantics
2715 Value *S, ///< The value to be sign extended
2716 const Type *Ty, ///< The type to sign extend to
2717 const Twine &NameStr, ///< A name for the new instruction
2718 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2721 /// @brief Clone an identical SExtInst
2722 virtual SExtInst *clone(LLVMContext &Context) const;
2724 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2725 static inline bool classof(const SExtInst *) { return true; }
2726 static inline bool classof(const Instruction *I) {
2727 return I->getOpcode() == SExt;
2729 static inline bool classof(const Value *V) {
2730 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2734 //===----------------------------------------------------------------------===//
2735 // FPTruncInst Class
2736 //===----------------------------------------------------------------------===//
2738 /// @brief This class represents a truncation of floating point types.
2739 class FPTruncInst : public CastInst {
2741 /// @brief Constructor with insert-before-instruction semantics
2743 Value *S, ///< The value to be truncated
2744 const Type *Ty, ///< The type to truncate to
2745 const Twine &NameStr = "", ///< A name for the new instruction
2746 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2749 /// @brief Constructor with insert-before-instruction semantics
2751 Value *S, ///< The value to be truncated
2752 const Type *Ty, ///< The type to truncate to
2753 const Twine &NameStr, ///< A name for the new instruction
2754 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2757 /// @brief Clone an identical FPTruncInst
2758 virtual FPTruncInst *clone(LLVMContext &Context) const;
2760 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2761 static inline bool classof(const FPTruncInst *) { return true; }
2762 static inline bool classof(const Instruction *I) {
2763 return I->getOpcode() == FPTrunc;
2765 static inline bool classof(const Value *V) {
2766 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2770 //===----------------------------------------------------------------------===//
2772 //===----------------------------------------------------------------------===//
2774 /// @brief This class represents an extension of floating point types.
2775 class FPExtInst : public CastInst {
2777 /// @brief Constructor with insert-before-instruction semantics
2779 Value *S, ///< The value to be extended
2780 const Type *Ty, ///< The type to extend to
2781 const Twine &NameStr = "", ///< A name for the new instruction
2782 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2785 /// @brief Constructor with insert-at-end-of-block semantics
2787 Value *S, ///< The value to be extended
2788 const Type *Ty, ///< The type to extend to
2789 const Twine &NameStr, ///< A name for the new instruction
2790 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2793 /// @brief Clone an identical FPExtInst
2794 virtual FPExtInst *clone(LLVMContext &Context) const;
2796 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2797 static inline bool classof(const FPExtInst *) { return true; }
2798 static inline bool classof(const Instruction *I) {
2799 return I->getOpcode() == FPExt;
2801 static inline bool classof(const Value *V) {
2802 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2806 //===----------------------------------------------------------------------===//
2808 //===----------------------------------------------------------------------===//
2810 /// @brief This class represents a cast unsigned integer to floating point.
2811 class UIToFPInst : public CastInst {
2813 /// @brief Constructor with insert-before-instruction semantics
2815 Value *S, ///< The value to be converted
2816 const Type *Ty, ///< The type to convert to
2817 const Twine &NameStr = "", ///< A name for the new instruction
2818 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2821 /// @brief Constructor with insert-at-end-of-block semantics
2823 Value *S, ///< The value to be converted
2824 const Type *Ty, ///< The type to convert to
2825 const Twine &NameStr, ///< A name for the new instruction
2826 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2829 /// @brief Clone an identical UIToFPInst
2830 virtual UIToFPInst *clone(LLVMContext &Context) const;
2832 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2833 static inline bool classof(const UIToFPInst *) { return true; }
2834 static inline bool classof(const Instruction *I) {
2835 return I->getOpcode() == UIToFP;
2837 static inline bool classof(const Value *V) {
2838 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2842 //===----------------------------------------------------------------------===//
2844 //===----------------------------------------------------------------------===//
2846 /// @brief This class represents a cast from signed integer to floating point.
2847 class SIToFPInst : public CastInst {
2849 /// @brief Constructor with insert-before-instruction semantics
2851 Value *S, ///< The value to be converted
2852 const Type *Ty, ///< The type to convert to
2853 const Twine &NameStr = "", ///< A name for the new instruction
2854 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2857 /// @brief Constructor with insert-at-end-of-block semantics
2859 Value *S, ///< The value to be converted
2860 const Type *Ty, ///< The type to convert to
2861 const Twine &NameStr, ///< A name for the new instruction
2862 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2865 /// @brief Clone an identical SIToFPInst
2866 virtual SIToFPInst *clone(LLVMContext &Context) const;
2868 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2869 static inline bool classof(const SIToFPInst *) { return true; }
2870 static inline bool classof(const Instruction *I) {
2871 return I->getOpcode() == SIToFP;
2873 static inline bool classof(const Value *V) {
2874 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2878 //===----------------------------------------------------------------------===//
2880 //===----------------------------------------------------------------------===//
2882 /// @brief This class represents a cast from floating point to unsigned integer
2883 class FPToUIInst : public CastInst {
2885 /// @brief Constructor with insert-before-instruction semantics
2887 Value *S, ///< The value to be converted
2888 const Type *Ty, ///< The type to convert to
2889 const Twine &NameStr = "", ///< A name for the new instruction
2890 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2893 /// @brief Constructor with insert-at-end-of-block semantics
2895 Value *S, ///< The value to be converted
2896 const Type *Ty, ///< The type to convert to
2897 const Twine &NameStr, ///< A name for the new instruction
2898 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2901 /// @brief Clone an identical FPToUIInst
2902 virtual FPToUIInst *clone(LLVMContext &Context) const;
2904 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2905 static inline bool classof(const FPToUIInst *) { return true; }
2906 static inline bool classof(const Instruction *I) {
2907 return I->getOpcode() == FPToUI;
2909 static inline bool classof(const Value *V) {
2910 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2914 //===----------------------------------------------------------------------===//
2916 //===----------------------------------------------------------------------===//
2918 /// @brief This class represents a cast from floating point to signed integer.
2919 class FPToSIInst : public CastInst {
2921 /// @brief Constructor with insert-before-instruction semantics
2923 Value *S, ///< The value to be converted
2924 const Type *Ty, ///< The type to convert to
2925 const Twine &NameStr = "", ///< A name for the new instruction
2926 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2929 /// @brief Constructor with insert-at-end-of-block semantics
2931 Value *S, ///< The value to be converted
2932 const Type *Ty, ///< The type to convert to
2933 const Twine &NameStr, ///< A name for the new instruction
2934 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2937 /// @brief Clone an identical FPToSIInst
2938 virtual FPToSIInst *clone(LLVMContext &Context) const;
2940 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2941 static inline bool classof(const FPToSIInst *) { return true; }
2942 static inline bool classof(const Instruction *I) {
2943 return I->getOpcode() == FPToSI;
2945 static inline bool classof(const Value *V) {
2946 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2950 //===----------------------------------------------------------------------===//
2951 // IntToPtrInst Class
2952 //===----------------------------------------------------------------------===//
2954 /// @brief This class represents a cast from an integer to a pointer.
2955 class IntToPtrInst : public CastInst {
2957 /// @brief Constructor with insert-before-instruction semantics
2959 Value *S, ///< The value to be converted
2960 const Type *Ty, ///< The type to convert to
2961 const Twine &NameStr = "", ///< A name for the new instruction
2962 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2965 /// @brief Constructor with insert-at-end-of-block semantics
2967 Value *S, ///< The value to be converted
2968 const Type *Ty, ///< The type to convert to
2969 const Twine &NameStr, ///< A name for the new instruction
2970 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2973 /// @brief Clone an identical IntToPtrInst
2974 virtual IntToPtrInst *clone(LLVMContext &Context) const;
2976 // Methods for support type inquiry through isa, cast, and dyn_cast:
2977 static inline bool classof(const IntToPtrInst *) { return true; }
2978 static inline bool classof(const Instruction *I) {
2979 return I->getOpcode() == IntToPtr;
2981 static inline bool classof(const Value *V) {
2982 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2986 //===----------------------------------------------------------------------===//
2987 // PtrToIntInst Class
2988 //===----------------------------------------------------------------------===//
2990 /// @brief This class represents a cast from a pointer to an integer
2991 class PtrToIntInst : public CastInst {
2993 /// @brief Constructor with insert-before-instruction semantics
2995 Value *S, ///< The value to be converted
2996 const Type *Ty, ///< The type to convert to
2997 const Twine &NameStr = "", ///< A name for the new instruction
2998 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3001 /// @brief Constructor with insert-at-end-of-block semantics
3003 Value *S, ///< The value to be converted
3004 const Type *Ty, ///< The type to convert to
3005 const Twine &NameStr, ///< A name for the new instruction
3006 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3009 /// @brief Clone an identical PtrToIntInst
3010 virtual PtrToIntInst *clone(LLVMContext &Context) const;
3012 // Methods for support type inquiry through isa, cast, and dyn_cast:
3013 static inline bool classof(const PtrToIntInst *) { return true; }
3014 static inline bool classof(const Instruction *I) {
3015 return I->getOpcode() == PtrToInt;
3017 static inline bool classof(const Value *V) {
3018 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3022 //===----------------------------------------------------------------------===//
3023 // BitCastInst Class
3024 //===----------------------------------------------------------------------===//
3026 /// @brief This class represents a no-op cast from one type to another.
3027 class BitCastInst : public CastInst {
3029 /// @brief Constructor with insert-before-instruction semantics
3031 Value *S, ///< The value to be casted
3032 const Type *Ty, ///< The type to casted to
3033 const Twine &NameStr = "", ///< A name for the new instruction
3034 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3037 /// @brief Constructor with insert-at-end-of-block semantics
3039 Value *S, ///< The value to be casted
3040 const Type *Ty, ///< The type to casted to
3041 const Twine &NameStr, ///< A name for the new instruction
3042 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3045 /// @brief Clone an identical BitCastInst
3046 virtual BitCastInst *clone(LLVMContext &Context) const;
3048 // Methods for support type inquiry through isa, cast, and dyn_cast:
3049 static inline bool classof(const BitCastInst *) { return true; }
3050 static inline bool classof(const Instruction *I) {
3051 return I->getOpcode() == BitCast;
3053 static inline bool classof(const Value *V) {
3054 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3058 } // End llvm namespace