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"
36 //===----------------------------------------------------------------------===//
37 // AllocationInst Class
38 //===----------------------------------------------------------------------===//
40 /// AllocationInst - This class is the common base class of MallocInst and
43 class AllocationInst : public UnaryInstruction {
45 AllocationInst(const Type *Ty, Value *ArraySize,
46 unsigned iTy, unsigned Align, const Twine &Name = "",
47 Instruction *InsertBefore = 0);
48 AllocationInst(const Type *Ty, Value *ArraySize,
49 unsigned iTy, unsigned Align, const Twine &Name,
50 BasicBlock *InsertAtEnd);
52 // Out of line virtual method, so the vtable, etc. has a home.
53 virtual ~AllocationInst();
55 /// isArrayAllocation - Return true if there is an allocation size parameter
56 /// to the allocation instruction that is not 1.
58 bool isArrayAllocation() const;
60 /// getArraySize - Get the number of elements allocated. For a simple
61 /// allocation of a single element, this will return a constant 1 value.
63 const Value *getArraySize() const { return getOperand(0); }
64 Value *getArraySize() { return getOperand(0); }
66 /// getType - Overload to return most specific pointer type
68 const PointerType *getType() const {
69 return reinterpret_cast<const PointerType*>(Instruction::getType());
72 /// getAllocatedType - Return the type that is being allocated by the
75 const Type *getAllocatedType() const;
77 /// getAlignment - Return the alignment of the memory that is being allocated
78 /// by the instruction.
80 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
81 void setAlignment(unsigned Align);
83 virtual AllocationInst *clone(LLVMContext &Context) const = 0;
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const AllocationInst *) { return true; }
87 static inline bool classof(const Instruction *I) {
88 return I->getOpcode() == Instruction::Alloca ||
89 I->getOpcode() == Instruction::Malloc;
91 static inline bool classof(const Value *V) {
92 return isa<Instruction>(V) && classof(cast<Instruction>(V));
97 //===----------------------------------------------------------------------===//
99 //===----------------------------------------------------------------------===//
101 /// MallocInst - an instruction to allocated memory on the heap
103 class MallocInst : public AllocationInst {
105 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
106 const Twine &NameStr = "",
107 Instruction *InsertBefore = 0)
108 : AllocationInst(Ty, ArraySize, Malloc,
109 0, NameStr, InsertBefore) {}
110 MallocInst(const Type *Ty, Value *ArraySize,
111 const Twine &NameStr, BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
114 MallocInst(const Type *Ty, const Twine &NameStr,
115 Instruction *InsertBefore = 0)
116 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
117 MallocInst(const Type *Ty, const Twine &NameStr,
118 BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
121 MallocInst(const Type *Ty, Value *ArraySize,
122 unsigned Align, const Twine &NameStr,
123 BasicBlock *InsertAtEnd)
124 : AllocationInst(Ty, ArraySize, Malloc,
125 Align, NameStr, InsertAtEnd) {}
126 MallocInst(const Type *Ty, Value *ArraySize,
127 unsigned Align, const Twine &NameStr = "",
128 Instruction *InsertBefore = 0)
129 : AllocationInst(Ty, ArraySize,
130 Malloc, Align, NameStr, InsertBefore) {}
132 virtual MallocInst *clone(LLVMContext &Context) const;
134 // Methods for support type inquiry through isa, cast, and dyn_cast:
135 static inline bool classof(const MallocInst *) { return true; }
136 static inline bool classof(const Instruction *I) {
137 return (I->getOpcode() == Instruction::Malloc);
139 static inline bool classof(const Value *V) {
140 return isa<Instruction>(V) && classof(cast<Instruction>(V));
145 //===----------------------------------------------------------------------===//
147 //===----------------------------------------------------------------------===//
149 /// AllocaInst - an instruction to allocate memory on the stack
151 class AllocaInst : public AllocationInst {
153 explicit AllocaInst(const Type *Ty,
154 Value *ArraySize = 0,
155 const Twine &NameStr = "",
156 Instruction *InsertBefore = 0)
157 : AllocationInst(Ty, ArraySize, Alloca,
158 0, NameStr, InsertBefore) {}
159 AllocaInst(const Type *Ty,
160 Value *ArraySize, const Twine &NameStr,
161 BasicBlock *InsertAtEnd)
162 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
164 AllocaInst(const Type *Ty, const Twine &NameStr,
165 Instruction *InsertBefore = 0)
166 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
167 AllocaInst(const Type *Ty, const Twine &NameStr,
168 BasicBlock *InsertAtEnd)
169 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
171 AllocaInst(const Type *Ty, Value *ArraySize,
172 unsigned Align, const Twine &NameStr = "",
173 Instruction *InsertBefore = 0)
174 : AllocationInst(Ty, ArraySize, Alloca,
175 Align, NameStr, InsertBefore) {}
176 AllocaInst(const Type *Ty, Value *ArraySize,
177 unsigned Align, const Twine &NameStr,
178 BasicBlock *InsertAtEnd)
179 : AllocationInst(Ty, ArraySize, Alloca,
180 Align, NameStr, InsertAtEnd) {}
182 virtual AllocaInst *clone(LLVMContext &Context) const;
184 /// isStaticAlloca - Return true if this alloca is in the entry block of the
185 /// function and is a constant size. If so, the code generator will fold it
186 /// into the prolog/epilog code, so it is basically free.
187 bool isStaticAlloca() const;
189 // Methods for support type inquiry through isa, cast, and dyn_cast:
190 static inline bool classof(const AllocaInst *) { return true; }
191 static inline bool classof(const Instruction *I) {
192 return (I->getOpcode() == Instruction::Alloca);
194 static inline bool classof(const Value *V) {
195 return isa<Instruction>(V) && classof(cast<Instruction>(V));
200 //===----------------------------------------------------------------------===//
202 //===----------------------------------------------------------------------===//
204 /// FreeInst - an instruction to deallocate memory
206 class FreeInst : public UnaryInstruction {
209 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
210 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
212 virtual FreeInst *clone(LLVMContext &Context) const;
214 // Accessor methods for consistency with other memory operations
215 Value *getPointerOperand() { return getOperand(0); }
216 const Value *getPointerOperand() const { return getOperand(0); }
218 // Methods for support type inquiry through isa, cast, and dyn_cast:
219 static inline bool classof(const FreeInst *) { return true; }
220 static inline bool classof(const Instruction *I) {
221 return (I->getOpcode() == Instruction::Free);
223 static inline bool classof(const Value *V) {
224 return isa<Instruction>(V) && classof(cast<Instruction>(V));
229 //===----------------------------------------------------------------------===//
231 //===----------------------------------------------------------------------===//
233 /// LoadInst - an instruction for reading from memory. This uses the
234 /// SubclassData field in Value to store whether or not the load is volatile.
236 class LoadInst : public UnaryInstruction {
239 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
240 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
241 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
242 Instruction *InsertBefore = 0);
243 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
244 unsigned Align, Instruction *InsertBefore = 0);
245 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
246 BasicBlock *InsertAtEnd);
247 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
248 unsigned Align, BasicBlock *InsertAtEnd);
250 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
251 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
252 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
253 bool isVolatile = false, Instruction *InsertBefore = 0);
254 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
255 BasicBlock *InsertAtEnd);
257 /// isVolatile - Return true if this is a load from a volatile memory
260 bool isVolatile() const { return SubclassData & 1; }
262 /// setVolatile - Specify whether this is a volatile load or not.
264 void setVolatile(bool V) {
265 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
268 virtual LoadInst *clone(LLVMContext &Context) const;
270 /// getAlignment - Return the alignment of the access that is being performed
272 unsigned getAlignment() const {
273 return (1 << (SubclassData>>1)) >> 1;
276 void setAlignment(unsigned Align);
278 Value *getPointerOperand() { return getOperand(0); }
279 const Value *getPointerOperand() const { return getOperand(0); }
280 static unsigned getPointerOperandIndex() { return 0U; }
282 unsigned getPointerAddressSpace() const {
283 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
287 // Methods for support type inquiry through isa, cast, and dyn_cast:
288 static inline bool classof(const LoadInst *) { return true; }
289 static inline bool classof(const Instruction *I) {
290 return I->getOpcode() == Instruction::Load;
292 static inline bool classof(const Value *V) {
293 return isa<Instruction>(V) && classof(cast<Instruction>(V));
298 //===----------------------------------------------------------------------===//
300 //===----------------------------------------------------------------------===//
302 /// StoreInst - an instruction for storing to memory
304 class StoreInst : public Instruction {
305 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
308 // allocate space for exactly two operands
309 void *operator new(size_t s) {
310 return User::operator new(s, 2);
312 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
313 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
314 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
315 Instruction *InsertBefore = 0);
316 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
317 unsigned Align, Instruction *InsertBefore = 0);
318 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
319 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
320 unsigned Align, BasicBlock *InsertAtEnd);
323 /// isVolatile - Return true if this is a load from a volatile memory
326 bool isVolatile() const { return SubclassData & 1; }
328 /// setVolatile - Specify whether this is a volatile load or not.
330 void setVolatile(bool V) {
331 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
334 /// Transparently provide more efficient getOperand methods.
335 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
337 /// getAlignment - Return the alignment of the access that is being performed
339 unsigned getAlignment() const {
340 return (1 << (SubclassData>>1)) >> 1;
343 void setAlignment(unsigned Align);
345 virtual StoreInst *clone(LLVMContext &Context) const;
347 Value *getPointerOperand() { return getOperand(1); }
348 const Value *getPointerOperand() const { return getOperand(1); }
349 static unsigned getPointerOperandIndex() { return 1U; }
351 unsigned getPointerAddressSpace() const {
352 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
355 // Methods for support type inquiry through isa, cast, and dyn_cast:
356 static inline bool classof(const StoreInst *) { return true; }
357 static inline bool classof(const Instruction *I) {
358 return I->getOpcode() == Instruction::Store;
360 static inline bool classof(const Value *V) {
361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
366 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
369 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
371 //===----------------------------------------------------------------------===//
372 // GetElementPtrInst Class
373 //===----------------------------------------------------------------------===//
375 // checkType - Simple wrapper function to give a better assertion failure
376 // message on bad indexes for a gep instruction.
378 static inline const Type *checkType(const Type *Ty) {
379 assert(Ty && "Invalid GetElementPtrInst indices for type!");
383 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
384 /// access elements of arrays and structs
386 class GetElementPtrInst : public Instruction {
387 GetElementPtrInst(const GetElementPtrInst &GEPI);
388 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
389 const Twine &NameStr);
390 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
392 template<typename InputIterator>
393 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
394 const Twine &NameStr,
395 // This argument ensures that we have an iterator we can
396 // do arithmetic on in constant time
397 std::random_access_iterator_tag) {
398 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
401 // This requires that the iterator points to contiguous memory.
402 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
403 // we have to build an array here
406 init(Ptr, 0, NumIdx, NameStr);
410 /// getIndexedType - Returns the type of the element that would be loaded with
411 /// a load instruction with the specified parameters.
413 /// Null is returned if the indices are invalid for the specified
416 template<typename InputIterator>
417 static const Type *getIndexedType(const Type *Ptr,
418 InputIterator IdxBegin,
419 InputIterator IdxEnd,
420 // This argument ensures that we
421 // have an iterator we can do
422 // arithmetic on in constant time
423 std::random_access_iterator_tag) {
424 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
427 // This requires that the iterator points to contiguous memory.
428 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
430 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
433 /// Constructors - Create a getelementptr instruction with a base pointer an
434 /// list of indices. The first ctor can optionally insert before an existing
435 /// instruction, the second appends the new instruction to the specified
437 template<typename InputIterator>
438 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
439 InputIterator IdxEnd,
441 const Twine &NameStr,
442 Instruction *InsertBefore);
443 template<typename InputIterator>
444 inline GetElementPtrInst(Value *Ptr,
445 InputIterator IdxBegin, InputIterator IdxEnd,
447 const Twine &NameStr, BasicBlock *InsertAtEnd);
449 /// Constructors - These two constructors are convenience methods because one
450 /// and two index getelementptr instructions are so common.
451 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
452 Instruction *InsertBefore = 0);
453 GetElementPtrInst(Value *Ptr, Value *Idx,
454 const Twine &NameStr, BasicBlock *InsertAtEnd);
456 template<typename InputIterator>
457 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
458 InputIterator IdxEnd,
459 const Twine &NameStr = "",
460 Instruction *InsertBefore = 0) {
461 typename std::iterator_traits<InputIterator>::difference_type Values =
462 1 + std::distance(IdxBegin, IdxEnd);
464 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
466 template<typename InputIterator>
467 static GetElementPtrInst *Create(Value *Ptr,
468 InputIterator IdxBegin, InputIterator IdxEnd,
469 const Twine &NameStr,
470 BasicBlock *InsertAtEnd) {
471 typename std::iterator_traits<InputIterator>::difference_type Values =
472 1 + std::distance(IdxBegin, IdxEnd);
474 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
477 /// Constructors - These two creators are convenience methods because one
478 /// index getelementptr instructions are so common.
479 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
480 const Twine &NameStr = "",
481 Instruction *InsertBefore = 0) {
482 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
484 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
485 const Twine &NameStr,
486 BasicBlock *InsertAtEnd) {
487 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
490 /// Create an "inbounds" getelementptr. See the documentation for the
491 /// "inbounds" flag in LangRef.html for details.
492 template<typename InputIterator>
493 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
494 InputIterator IdxEnd,
495 const Twine &NameStr = "",
496 Instruction *InsertBefore = 0) {
497 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
498 NameStr, InsertBefore);
499 cast<GEPOperator>(GEP)->setIsInBounds(true);
502 template<typename InputIterator>
503 static GetElementPtrInst *CreateInBounds(Value *Ptr,
504 InputIterator IdxBegin,
505 InputIterator IdxEnd,
506 const Twine &NameStr,
507 BasicBlock *InsertAtEnd) {
508 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
509 NameStr, InsertAtEnd);
510 cast<GEPOperator>(GEP)->setIsInBounds(true);
513 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
514 const Twine &NameStr = "",
515 Instruction *InsertBefore = 0) {
516 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
517 cast<GEPOperator>(GEP)->setIsInBounds(true);
520 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
521 const Twine &NameStr,
522 BasicBlock *InsertAtEnd) {
523 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
524 cast<GEPOperator>(GEP)->setIsInBounds(true);
528 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
530 /// Transparently provide more efficient getOperand methods.
531 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
533 // getType - Overload to return most specific pointer type...
534 const PointerType *getType() const {
535 return reinterpret_cast<const PointerType*>(Instruction::getType());
538 /// getIndexedType - Returns the type of the element that would be loaded with
539 /// a load instruction with the specified parameters.
541 /// Null is returned if the indices are invalid for the specified
544 template<typename InputIterator>
545 static const Type *getIndexedType(const Type *Ptr,
546 InputIterator IdxBegin,
547 InputIterator IdxEnd) {
548 return getIndexedType(Ptr, IdxBegin, IdxEnd,
549 typename std::iterator_traits<InputIterator>::
550 iterator_category());
553 static const Type *getIndexedType(const Type *Ptr,
554 Value* const *Idx, unsigned NumIdx);
556 static const Type *getIndexedType(const Type *Ptr,
557 uint64_t const *Idx, unsigned NumIdx);
559 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
561 inline op_iterator idx_begin() { return op_begin()+1; }
562 inline const_op_iterator idx_begin() const { return op_begin()+1; }
563 inline op_iterator idx_end() { return op_end(); }
564 inline const_op_iterator idx_end() const { return op_end(); }
566 Value *getPointerOperand() {
567 return getOperand(0);
569 const Value *getPointerOperand() const {
570 return getOperand(0);
572 static unsigned getPointerOperandIndex() {
573 return 0U; // get index for modifying correct operand
576 unsigned getPointerAddressSpace() const {
577 return cast<PointerType>(getType())->getAddressSpace();
580 /// getPointerOperandType - Method to return the pointer operand as a
582 const PointerType *getPointerOperandType() const {
583 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
587 unsigned getNumIndices() const { // Note: always non-negative
588 return getNumOperands() - 1;
591 bool hasIndices() const {
592 return getNumOperands() > 1;
595 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
596 /// zeros. If so, the result pointer and the first operand have the same
597 /// value, just potentially different types.
598 bool hasAllZeroIndices() const;
600 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
601 /// constant integers. If so, the result pointer and the first operand have
602 /// a constant offset between them.
603 bool hasAllConstantIndices() const;
605 // Methods for support type inquiry through isa, cast, and dyn_cast:
606 static inline bool classof(const GetElementPtrInst *) { return true; }
607 static inline bool classof(const Instruction *I) {
608 return (I->getOpcode() == Instruction::GetElementPtr);
610 static inline bool classof(const Value *V) {
611 return isa<Instruction>(V) && classof(cast<Instruction>(V));
616 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
619 template<typename InputIterator>
620 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
621 InputIterator IdxBegin,
622 InputIterator IdxEnd,
624 const Twine &NameStr,
625 Instruction *InsertBefore)
626 : Instruction(PointerType::get(checkType(
627 getIndexedType(Ptr->getType(),
629 cast<PointerType>(Ptr->getType())
630 ->getAddressSpace()),
632 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
633 Values, InsertBefore) {
634 init(Ptr, IdxBegin, IdxEnd, NameStr,
635 typename std::iterator_traits<InputIterator>::iterator_category());
637 template<typename InputIterator>
638 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
639 InputIterator IdxBegin,
640 InputIterator IdxEnd,
642 const Twine &NameStr,
643 BasicBlock *InsertAtEnd)
644 : Instruction(PointerType::get(checkType(
645 getIndexedType(Ptr->getType(),
647 cast<PointerType>(Ptr->getType())
648 ->getAddressSpace()),
650 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
651 Values, InsertAtEnd) {
652 init(Ptr, IdxBegin, IdxEnd, NameStr,
653 typename std::iterator_traits<InputIterator>::iterator_category());
657 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
660 //===----------------------------------------------------------------------===//
662 //===----------------------------------------------------------------------===//
664 /// This instruction compares its operands according to the predicate given
665 /// to the constructor. It only operates on integers or pointers. The operands
666 /// must be identical types.
667 /// @brief Represent an integer comparison operator.
668 class ICmpInst: public CmpInst {
670 /// @brief Constructor with insert-before-instruction semantics.
672 Instruction *InsertBefore, ///< Where to insert
673 Predicate pred, ///< The predicate to use for the comparison
674 Value *LHS, ///< The left-hand-side of the expression
675 Value *RHS, ///< The right-hand-side of the expression
676 const Twine &NameStr = "" ///< Name of the instruction
677 ) : CmpInst(makeCmpResultType(LHS->getType()),
678 Instruction::ICmp, pred, LHS, RHS, NameStr,
680 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
681 pred <= CmpInst::LAST_ICMP_PREDICATE &&
682 "Invalid ICmp predicate value");
683 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
684 "Both operands to ICmp instruction are not of the same type!");
685 // Check that the operands are the right type
686 assert((getOperand(0)->getType()->isIntOrIntVector() ||
687 isa<PointerType>(getOperand(0)->getType())) &&
688 "Invalid operand types for ICmp instruction");
691 /// @brief Constructor with insert-at-end semantics.
693 BasicBlock &InsertAtEnd, ///< Block to insert into.
694 Predicate pred, ///< The predicate to use for the comparison
695 Value *LHS, ///< The left-hand-side of the expression
696 Value *RHS, ///< The right-hand-side of the expression
697 const Twine &NameStr = "" ///< Name of the instruction
698 ) : CmpInst(makeCmpResultType(LHS->getType()),
699 Instruction::ICmp, pred, LHS, RHS, NameStr,
701 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
702 pred <= CmpInst::LAST_ICMP_PREDICATE &&
703 "Invalid ICmp predicate value");
704 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
705 "Both operands to ICmp instruction are not of the same type!");
706 // Check that the operands are the right type
707 assert((getOperand(0)->getType()->isIntOrIntVector() ||
708 isa<PointerType>(getOperand(0)->getType())) &&
709 "Invalid operand types for ICmp instruction");
712 /// @brief Constructor with no-insertion semantics
714 Predicate pred, ///< The predicate to use for the comparison
715 Value *LHS, ///< The left-hand-side of the expression
716 Value *RHS, ///< The right-hand-side of the expression
717 const Twine &NameStr = "" ///< Name of the instruction
718 ) : CmpInst(makeCmpResultType(LHS->getType()),
719 Instruction::ICmp, pred, LHS, RHS, NameStr) {
720 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
721 pred <= CmpInst::LAST_ICMP_PREDICATE &&
722 "Invalid ICmp predicate value");
723 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
724 "Both operands to ICmp instruction are not of the same type!");
725 // Check that the operands are the right type
726 assert((getOperand(0)->getType()->isIntOrIntVector() ||
727 isa<PointerType>(getOperand(0)->getType())) &&
728 "Invalid operand types for ICmp instruction");
731 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
732 /// @returns the predicate that would be the result if the operand were
733 /// regarded as signed.
734 /// @brief Return the signed version of the predicate
735 Predicate getSignedPredicate() const {
736 return getSignedPredicate(getPredicate());
739 /// This is a static version that you can use without an instruction.
740 /// @brief Return the signed version of the predicate.
741 static Predicate getSignedPredicate(Predicate pred);
743 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
744 /// @returns the predicate that would be the result if the operand were
745 /// regarded as unsigned.
746 /// @brief Return the unsigned version of the predicate
747 Predicate getUnsignedPredicate() const {
748 return getUnsignedPredicate(getPredicate());
751 /// This is a static version that you can use without an instruction.
752 /// @brief Return the unsigned version of the predicate.
753 static Predicate getUnsignedPredicate(Predicate pred);
755 /// isEquality - Return true if this predicate is either EQ or NE. This also
756 /// tests for commutativity.
757 static bool isEquality(Predicate P) {
758 return P == ICMP_EQ || P == ICMP_NE;
761 /// isEquality - Return true if this predicate is either EQ or NE. This also
762 /// tests for commutativity.
763 bool isEquality() const {
764 return isEquality(getPredicate());
767 /// @returns true if the predicate of this ICmpInst is commutative
768 /// @brief Determine if this relation is commutative.
769 bool isCommutative() const { return isEquality(); }
771 /// isRelational - Return true if the predicate is relational (not EQ or NE).
773 bool isRelational() const {
774 return !isEquality();
777 /// isRelational - Return true if the predicate is relational (not EQ or NE).
779 static bool isRelational(Predicate P) {
780 return !isEquality(P);
783 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
784 /// @brief Determine if this instruction's predicate is signed.
785 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
787 /// @returns true if the predicate provided is signed, false otherwise
788 /// @brief Determine if the predicate is signed.
789 static bool isSignedPredicate(Predicate pred);
791 /// @returns true if the specified compare predicate is
792 /// true when both operands are equal...
793 /// @brief Determine if the icmp is true when both operands are equal
794 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
795 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
796 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
797 pred == ICmpInst::ICMP_SLE;
800 /// @returns true if the specified compare instruction is
801 /// true when both operands are equal...
802 /// @brief Determine if the ICmpInst returns true when both operands are equal
803 bool isTrueWhenEqual() {
804 return isTrueWhenEqual(getPredicate());
807 /// Initialize a set of values that all satisfy the predicate with C.
808 /// @brief Make a ConstantRange for a relation with a constant value.
809 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
811 /// Exchange the two operands to this instruction in such a way that it does
812 /// not modify the semantics of the instruction. The predicate value may be
813 /// changed to retain the same result if the predicate is order dependent
815 /// @brief Swap operands and adjust predicate.
816 void swapOperands() {
817 SubclassData = getSwappedPredicate();
818 Op<0>().swap(Op<1>());
821 virtual ICmpInst *clone(LLVMContext &Context) const;
823 // Methods for support type inquiry through isa, cast, and dyn_cast:
824 static inline bool classof(const ICmpInst *) { return true; }
825 static inline bool classof(const Instruction *I) {
826 return I->getOpcode() == Instruction::ICmp;
828 static inline bool classof(const Value *V) {
829 return isa<Instruction>(V) && classof(cast<Instruction>(V));
834 //===----------------------------------------------------------------------===//
836 //===----------------------------------------------------------------------===//
838 /// This instruction compares its operands according to the predicate given
839 /// to the constructor. It only operates on floating point values or packed
840 /// vectors of floating point values. The operands must be identical types.
841 /// @brief Represents a floating point comparison operator.
842 class FCmpInst: public CmpInst {
844 /// @brief Constructor with insert-before-instruction semantics.
846 Instruction *InsertBefore, ///< Where to insert
847 Predicate pred, ///< The predicate to use for the comparison
848 Value *LHS, ///< The left-hand-side of the expression
849 Value *RHS, ///< The right-hand-side of the expression
850 const Twine &NameStr = "" ///< Name of the instruction
851 ) : CmpInst(makeCmpResultType(LHS->getType()),
852 Instruction::FCmp, pred, LHS, RHS, NameStr,
854 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
855 "Invalid FCmp predicate value");
856 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
857 "Both operands to FCmp instruction are not of the same type!");
858 // Check that the operands are the right type
859 assert(getOperand(0)->getType()->isFPOrFPVector() &&
860 "Invalid operand types for FCmp instruction");
863 /// @brief Constructor with insert-at-end semantics.
865 BasicBlock &InsertAtEnd, ///< Block to insert into.
866 Predicate pred, ///< The predicate to use for the comparison
867 Value *LHS, ///< The left-hand-side of the expression
868 Value *RHS, ///< The right-hand-side of the expression
869 const Twine &NameStr = "" ///< Name of the instruction
870 ) : CmpInst(makeCmpResultType(LHS->getType()),
871 Instruction::FCmp, pred, LHS, RHS, NameStr,
873 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
874 "Invalid FCmp predicate value");
875 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
876 "Both operands to FCmp instruction are not of the same type!");
877 // Check that the operands are the right type
878 assert(getOperand(0)->getType()->isFPOrFPVector() &&
879 "Invalid operand types for FCmp instruction");
882 /// @brief Constructor with no-insertion semantics
884 Predicate pred, ///< The predicate to use for the comparison
885 Value *LHS, ///< The left-hand-side of the expression
886 Value *RHS, ///< The right-hand-side of the expression
887 const Twine &NameStr = "" ///< Name of the instruction
888 ) : CmpInst(makeCmpResultType(LHS->getType()),
889 Instruction::FCmp, pred, LHS, RHS, NameStr) {
890 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
891 "Invalid FCmp predicate value");
892 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
893 "Both operands to FCmp instruction are not of the same type!");
894 // Check that the operands are the right type
895 assert(getOperand(0)->getType()->isFPOrFPVector() &&
896 "Invalid operand types for FCmp instruction");
899 /// @returns true if the predicate of this instruction is EQ or NE.
900 /// @brief Determine if this is an equality predicate.
901 bool isEquality() const {
902 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
903 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
906 /// @returns true if the predicate of this instruction is commutative.
907 /// @brief Determine if this is a commutative predicate.
908 bool isCommutative() const {
909 return isEquality() ||
910 SubclassData == FCMP_FALSE ||
911 SubclassData == FCMP_TRUE ||
912 SubclassData == FCMP_ORD ||
913 SubclassData == FCMP_UNO;
916 /// @returns true if the predicate is relational (not EQ or NE).
917 /// @brief Determine if this a relational predicate.
918 bool isRelational() const { return !isEquality(); }
920 /// Exchange the two operands to this instruction in such a way that it does
921 /// not modify the semantics of the instruction. The predicate value may be
922 /// changed to retain the same result if the predicate is order dependent
924 /// @brief Swap operands and adjust predicate.
925 void swapOperands() {
926 SubclassData = getSwappedPredicate();
927 Op<0>().swap(Op<1>());
930 virtual FCmpInst *clone(LLVMContext &Context) const;
932 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
933 static inline bool classof(const FCmpInst *) { return true; }
934 static inline bool classof(const Instruction *I) {
935 return I->getOpcode() == Instruction::FCmp;
937 static inline bool classof(const Value *V) {
938 return isa<Instruction>(V) && classof(cast<Instruction>(V));
942 //===----------------------------------------------------------------------===//
944 //===----------------------------------------------------------------------===//
945 /// CallInst - This class represents a function call, abstracting a target
946 /// machine's calling convention. This class uses low bit of the SubClassData
947 /// field to indicate whether or not this is a tail call. The rest of the bits
948 /// hold the calling convention of the call.
951 class CallInst : public Instruction {
952 AttrListPtr AttributeList; ///< parameter attributes for call
953 CallInst(const CallInst &CI);
954 void init(Value *Func, Value* const *Params, unsigned NumParams);
955 void init(Value *Func, Value *Actual1, Value *Actual2);
956 void init(Value *Func, Value *Actual);
957 void init(Value *Func);
959 template<typename InputIterator>
960 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
961 const Twine &NameStr,
962 // This argument ensures that we have an iterator we can
963 // do arithmetic on in constant time
964 std::random_access_iterator_tag) {
965 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
967 // This requires that the iterator points to contiguous memory.
968 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
972 /// Construct a CallInst given a range of arguments. InputIterator
973 /// must be a random-access iterator pointing to contiguous storage
974 /// (e.g. a std::vector<>::iterator). Checks are made for
975 /// random-accessness but not for contiguous storage as that would
976 /// incur runtime overhead.
977 /// @brief Construct a CallInst from a range of arguments
978 template<typename InputIterator>
979 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
980 const Twine &NameStr, Instruction *InsertBefore);
982 /// Construct a CallInst given a range of arguments. InputIterator
983 /// must be a random-access iterator pointing to contiguous storage
984 /// (e.g. a std::vector<>::iterator). Checks are made for
985 /// random-accessness but not for contiguous storage as that would
986 /// incur runtime overhead.
987 /// @brief Construct a CallInst from a range of arguments
988 template<typename InputIterator>
989 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
990 const Twine &NameStr, BasicBlock *InsertAtEnd);
992 CallInst(Value *F, Value *Actual, const Twine &NameStr,
993 Instruction *InsertBefore);
994 CallInst(Value *F, Value *Actual, const Twine &NameStr,
995 BasicBlock *InsertAtEnd);
996 explicit CallInst(Value *F, const Twine &NameStr,
997 Instruction *InsertBefore);
998 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1000 template<typename InputIterator>
1001 static CallInst *Create(Value *Func,
1002 InputIterator ArgBegin, InputIterator ArgEnd,
1003 const Twine &NameStr = "",
1004 Instruction *InsertBefore = 0) {
1005 return new((unsigned)(ArgEnd - ArgBegin + 1))
1006 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1008 template<typename InputIterator>
1009 static CallInst *Create(Value *Func,
1010 InputIterator ArgBegin, InputIterator ArgEnd,
1011 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1012 return new((unsigned)(ArgEnd - ArgBegin + 1))
1013 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1015 static CallInst *Create(Value *F, Value *Actual,
1016 const Twine &NameStr = "",
1017 Instruction *InsertBefore = 0) {
1018 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1020 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1021 BasicBlock *InsertAtEnd) {
1022 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1024 static CallInst *Create(Value *F, const Twine &NameStr = "",
1025 Instruction *InsertBefore = 0) {
1026 return new(1) CallInst(F, NameStr, InsertBefore);
1028 static CallInst *Create(Value *F, const Twine &NameStr,
1029 BasicBlock *InsertAtEnd) {
1030 return new(1) CallInst(F, NameStr, InsertAtEnd);
1035 bool isTailCall() const { return SubclassData & 1; }
1036 void setTailCall(bool isTC = true) {
1037 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1040 virtual CallInst *clone(LLVMContext &Context) const;
1042 /// Provide fast operand accessors
1043 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1045 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1047 CallingConv::ID getCallingConv() const {
1048 return static_cast<CallingConv::ID>(SubclassData >> 1);
1050 void setCallingConv(CallingConv::ID CC) {
1051 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
1054 /// getAttributes - Return the parameter attributes for this call.
1056 const AttrListPtr &getAttributes() const { return AttributeList; }
1058 /// setAttributes - Set the parameter attributes for this call.
1060 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1062 /// addAttribute - adds the attribute to the list of attributes.
1063 void addAttribute(unsigned i, Attributes attr);
1065 /// removeAttribute - removes the attribute from the list of attributes.
1066 void removeAttribute(unsigned i, Attributes attr);
1068 /// @brief Determine whether the call or the callee has the given attribute.
1069 bool paramHasAttr(unsigned i, Attributes attr) const;
1071 /// @brief Extract the alignment for a call or parameter (0=unknown).
1072 unsigned getParamAlignment(unsigned i) const {
1073 return AttributeList.getParamAlignment(i);
1076 /// @brief Determine if the call does not access memory.
1077 bool doesNotAccessMemory() const {
1078 return paramHasAttr(~0, Attribute::ReadNone);
1080 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1081 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1082 else removeAttribute(~0, Attribute::ReadNone);
1085 /// @brief Determine if the call does not access or only reads memory.
1086 bool onlyReadsMemory() const {
1087 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1089 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1090 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1091 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1094 /// @brief Determine if the call cannot return.
1095 bool doesNotReturn() const {
1096 return paramHasAttr(~0, Attribute::NoReturn);
1098 void setDoesNotReturn(bool DoesNotReturn = true) {
1099 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1100 else removeAttribute(~0, Attribute::NoReturn);
1103 /// @brief Determine if the call cannot unwind.
1104 bool doesNotThrow() const {
1105 return paramHasAttr(~0, Attribute::NoUnwind);
1107 void setDoesNotThrow(bool DoesNotThrow = true) {
1108 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1109 else removeAttribute(~0, Attribute::NoUnwind);
1112 /// @brief Determine if the call returns a structure through first
1113 /// pointer argument.
1114 bool hasStructRetAttr() const {
1115 // Be friendly and also check the callee.
1116 return paramHasAttr(1, Attribute::StructRet);
1119 /// @brief Determine if any call argument is an aggregate passed by value.
1120 bool hasByValArgument() const {
1121 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1124 /// getCalledFunction - Return the function called, or null if this is an
1125 /// indirect function invocation.
1127 Function *getCalledFunction() const {
1128 return dyn_cast<Function>(Op<0>());
1131 /// getCalledValue - Get a pointer to the function that is invoked by this
1133 const Value *getCalledValue() const { return Op<0>(); }
1134 Value *getCalledValue() { return Op<0>(); }
1136 // Methods for support type inquiry through isa, cast, and dyn_cast:
1137 static inline bool classof(const CallInst *) { return true; }
1138 static inline bool classof(const Instruction *I) {
1139 return I->getOpcode() == Instruction::Call;
1141 static inline bool classof(const Value *V) {
1142 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1147 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1150 template<typename InputIterator>
1151 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1152 const Twine &NameStr, BasicBlock *InsertAtEnd)
1153 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1154 ->getElementType())->getReturnType(),
1156 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1157 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1158 init(Func, ArgBegin, ArgEnd, NameStr,
1159 typename std::iterator_traits<InputIterator>::iterator_category());
1162 template<typename InputIterator>
1163 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1164 const Twine &NameStr, Instruction *InsertBefore)
1165 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1166 ->getElementType())->getReturnType(),
1168 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1169 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1170 init(Func, ArgBegin, ArgEnd, NameStr,
1171 typename std::iterator_traits<InputIterator>::iterator_category());
1174 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1176 //===----------------------------------------------------------------------===//
1178 //===----------------------------------------------------------------------===//
1180 /// SelectInst - This class represents the LLVM 'select' instruction.
1182 class SelectInst : public Instruction {
1183 void init(Value *C, Value *S1, Value *S2) {
1184 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1190 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1191 Instruction *InsertBefore)
1192 : Instruction(S1->getType(), Instruction::Select,
1193 &Op<0>(), 3, InsertBefore) {
1197 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1198 BasicBlock *InsertAtEnd)
1199 : Instruction(S1->getType(), Instruction::Select,
1200 &Op<0>(), 3, InsertAtEnd) {
1205 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1206 const Twine &NameStr = "",
1207 Instruction *InsertBefore = 0) {
1208 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1210 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1211 const Twine &NameStr,
1212 BasicBlock *InsertAtEnd) {
1213 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1216 Value *getCondition() const { return Op<0>(); }
1217 Value *getTrueValue() const { return Op<1>(); }
1218 Value *getFalseValue() const { return Op<2>(); }
1220 /// areInvalidOperands - Return a string if the specified operands are invalid
1221 /// for a select operation, otherwise return null.
1222 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1224 /// Transparently provide more efficient getOperand methods.
1225 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1227 OtherOps getOpcode() const {
1228 return static_cast<OtherOps>(Instruction::getOpcode());
1231 virtual SelectInst *clone(LLVMContext &Context) const;
1233 // Methods for support type inquiry through isa, cast, and dyn_cast:
1234 static inline bool classof(const SelectInst *) { return true; }
1235 static inline bool classof(const Instruction *I) {
1236 return I->getOpcode() == Instruction::Select;
1238 static inline bool classof(const Value *V) {
1239 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1244 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1247 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1249 //===----------------------------------------------------------------------===//
1251 //===----------------------------------------------------------------------===//
1253 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1254 /// an argument of the specified type given a va_list and increments that list
1256 class VAArgInst : public UnaryInstruction {
1258 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1259 Instruction *InsertBefore = 0)
1260 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1263 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1264 BasicBlock *InsertAtEnd)
1265 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1269 virtual VAArgInst *clone(LLVMContext &Context) const;
1271 // Methods for support type inquiry through isa, cast, and dyn_cast:
1272 static inline bool classof(const VAArgInst *) { return true; }
1273 static inline bool classof(const Instruction *I) {
1274 return I->getOpcode() == VAArg;
1276 static inline bool classof(const Value *V) {
1277 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1281 //===----------------------------------------------------------------------===//
1282 // ExtractElementInst Class
1283 //===----------------------------------------------------------------------===//
1285 /// ExtractElementInst - This instruction extracts a single (scalar)
1286 /// element from a VectorType value
1288 class ExtractElementInst : public Instruction {
1289 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1290 Instruction *InsertBefore = 0);
1291 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1292 BasicBlock *InsertAtEnd);
1294 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1295 const Twine &NameStr = "",
1296 Instruction *InsertBefore = 0) {
1297 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1299 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1300 const Twine &NameStr,
1301 BasicBlock *InsertAtEnd) {
1302 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1305 /// isValidOperands - Return true if an extractelement instruction can be
1306 /// formed with the specified operands.
1307 static bool isValidOperands(const Value *Vec, const Value *Idx);
1309 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1311 /// Transparently provide more efficient getOperand methods.
1312 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1314 // Methods for support type inquiry through isa, cast, and dyn_cast:
1315 static inline bool classof(const ExtractElementInst *) { return true; }
1316 static inline bool classof(const Instruction *I) {
1317 return I->getOpcode() == Instruction::ExtractElement;
1319 static inline bool classof(const Value *V) {
1320 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1325 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1328 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1330 //===----------------------------------------------------------------------===//
1331 // InsertElementInst Class
1332 //===----------------------------------------------------------------------===//
1334 /// InsertElementInst - This instruction inserts a single (scalar)
1335 /// element into a VectorType value
1337 class InsertElementInst : public Instruction {
1338 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1339 const Twine &NameStr = "",
1340 Instruction *InsertBefore = 0);
1341 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1342 const Twine &NameStr, BasicBlock *InsertAtEnd);
1344 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1345 const Twine &NameStr = "",
1346 Instruction *InsertBefore = 0) {
1347 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1349 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1350 const Twine &NameStr,
1351 BasicBlock *InsertAtEnd) {
1352 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1355 /// isValidOperands - Return true if an insertelement instruction can be
1356 /// formed with the specified operands.
1357 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1360 virtual InsertElementInst *clone(LLVMContext &Context) const;
1362 /// getType - Overload to return most specific vector type.
1364 const VectorType *getType() const {
1365 return reinterpret_cast<const VectorType*>(Instruction::getType());
1368 /// Transparently provide more efficient getOperand methods.
1369 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1371 // Methods for support type inquiry through isa, cast, and dyn_cast:
1372 static inline bool classof(const InsertElementInst *) { return true; }
1373 static inline bool classof(const Instruction *I) {
1374 return I->getOpcode() == Instruction::InsertElement;
1376 static inline bool classof(const Value *V) {
1377 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1382 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1385 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1387 //===----------------------------------------------------------------------===//
1388 // ShuffleVectorInst Class
1389 //===----------------------------------------------------------------------===//
1391 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1394 class ShuffleVectorInst : public Instruction {
1396 // allocate space for exactly three operands
1397 void *operator new(size_t s) {
1398 return User::operator new(s, 3);
1400 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1401 const Twine &NameStr = "",
1402 Instruction *InsertBefor = 0);
1403 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1404 const Twine &NameStr, BasicBlock *InsertAtEnd);
1406 /// isValidOperands - Return true if a shufflevector instruction can be
1407 /// formed with the specified operands.
1408 static bool isValidOperands(const Value *V1, const Value *V2,
1411 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1413 /// getType - Overload to return most specific vector type.
1415 const VectorType *getType() const {
1416 return reinterpret_cast<const VectorType*>(Instruction::getType());
1419 /// Transparently provide more efficient getOperand methods.
1420 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1422 /// getMaskValue - Return the index from the shuffle mask for the specified
1423 /// output result. This is either -1 if the element is undef or a number less
1424 /// than 2*numelements.
1425 int getMaskValue(unsigned i) const;
1427 // Methods for support type inquiry through isa, cast, and dyn_cast:
1428 static inline bool classof(const ShuffleVectorInst *) { return true; }
1429 static inline bool classof(const Instruction *I) {
1430 return I->getOpcode() == Instruction::ShuffleVector;
1432 static inline bool classof(const Value *V) {
1433 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1438 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1441 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1443 //===----------------------------------------------------------------------===//
1444 // ExtractValueInst Class
1445 //===----------------------------------------------------------------------===//
1447 /// ExtractValueInst - This instruction extracts a struct member or array
1448 /// element value from an aggregate value.
1450 class ExtractValueInst : public UnaryInstruction {
1451 SmallVector<unsigned, 4> Indices;
1453 ExtractValueInst(const ExtractValueInst &EVI);
1454 void init(const unsigned *Idx, unsigned NumIdx,
1455 const Twine &NameStr);
1456 void init(unsigned Idx, const Twine &NameStr);
1458 template<typename InputIterator>
1459 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1460 const Twine &NameStr,
1461 // This argument ensures that we have an iterator we can
1462 // do arithmetic on in constant time
1463 std::random_access_iterator_tag) {
1464 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1466 // There's no fundamental reason why we require at least one index
1467 // (other than weirdness with &*IdxBegin being invalid; see
1468 // getelementptr's init routine for example). But there's no
1469 // present need to support it.
1470 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1472 // This requires that the iterator points to contiguous memory.
1473 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1474 // we have to build an array here
1477 /// getIndexedType - Returns the type of the element that would be extracted
1478 /// with an extractvalue instruction with the specified parameters.
1480 /// Null is returned if the indices are invalid for the specified
1483 static const Type *getIndexedType(const Type *Agg,
1484 const unsigned *Idx, unsigned NumIdx);
1486 template<typename InputIterator>
1487 static const Type *getIndexedType(const Type *Ptr,
1488 InputIterator IdxBegin,
1489 InputIterator IdxEnd,
1490 // This argument ensures that we
1491 // have an iterator we can do
1492 // arithmetic on in constant time
1493 std::random_access_iterator_tag) {
1494 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1497 // This requires that the iterator points to contiguous memory.
1498 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1500 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1503 /// Constructors - Create a extractvalue instruction with a base aggregate
1504 /// value and a list of indices. The first ctor can optionally insert before
1505 /// an existing instruction, the second appends the new instruction to the
1506 /// specified BasicBlock.
1507 template<typename InputIterator>
1508 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1509 InputIterator IdxEnd,
1510 const Twine &NameStr,
1511 Instruction *InsertBefore);
1512 template<typename InputIterator>
1513 inline ExtractValueInst(Value *Agg,
1514 InputIterator IdxBegin, InputIterator IdxEnd,
1515 const Twine &NameStr, BasicBlock *InsertAtEnd);
1517 // allocate space for exactly one operand
1518 void *operator new(size_t s) {
1519 return User::operator new(s, 1);
1523 template<typename InputIterator>
1524 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1525 InputIterator IdxEnd,
1526 const Twine &NameStr = "",
1527 Instruction *InsertBefore = 0) {
1529 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1531 template<typename InputIterator>
1532 static ExtractValueInst *Create(Value *Agg,
1533 InputIterator IdxBegin, InputIterator IdxEnd,
1534 const Twine &NameStr,
1535 BasicBlock *InsertAtEnd) {
1536 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1539 /// Constructors - These two creators are convenience methods because one
1540 /// index extractvalue instructions are much more common than those with
1542 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1543 const Twine &NameStr = "",
1544 Instruction *InsertBefore = 0) {
1545 unsigned Idxs[1] = { Idx };
1546 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1548 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1549 const Twine &NameStr,
1550 BasicBlock *InsertAtEnd) {
1551 unsigned Idxs[1] = { Idx };
1552 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1555 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1557 /// getIndexedType - Returns the type of the element that would be extracted
1558 /// with an extractvalue instruction with the specified parameters.
1560 /// Null is returned if the indices are invalid for the specified
1563 template<typename InputIterator>
1564 static const Type *getIndexedType(const Type *Ptr,
1565 InputIterator IdxBegin,
1566 InputIterator IdxEnd) {
1567 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1568 typename std::iterator_traits<InputIterator>::
1569 iterator_category());
1571 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1573 typedef const unsigned* idx_iterator;
1574 inline idx_iterator idx_begin() const { return Indices.begin(); }
1575 inline idx_iterator idx_end() const { return Indices.end(); }
1577 Value *getAggregateOperand() {
1578 return getOperand(0);
1580 const Value *getAggregateOperand() const {
1581 return getOperand(0);
1583 static unsigned getAggregateOperandIndex() {
1584 return 0U; // get index for modifying correct operand
1587 unsigned getNumIndices() const { // Note: always non-negative
1588 return (unsigned)Indices.size();
1591 bool hasIndices() const {
1595 // Methods for support type inquiry through isa, cast, and dyn_cast:
1596 static inline bool classof(const ExtractValueInst *) { return true; }
1597 static inline bool classof(const Instruction *I) {
1598 return I->getOpcode() == Instruction::ExtractValue;
1600 static inline bool classof(const Value *V) {
1601 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1605 template<typename InputIterator>
1606 ExtractValueInst::ExtractValueInst(Value *Agg,
1607 InputIterator IdxBegin,
1608 InputIterator IdxEnd,
1609 const Twine &NameStr,
1610 Instruction *InsertBefore)
1611 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1613 ExtractValue, Agg, InsertBefore) {
1614 init(IdxBegin, IdxEnd, NameStr,
1615 typename std::iterator_traits<InputIterator>::iterator_category());
1617 template<typename InputIterator>
1618 ExtractValueInst::ExtractValueInst(Value *Agg,
1619 InputIterator IdxBegin,
1620 InputIterator IdxEnd,
1621 const Twine &NameStr,
1622 BasicBlock *InsertAtEnd)
1623 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1625 ExtractValue, Agg, InsertAtEnd) {
1626 init(IdxBegin, IdxEnd, NameStr,
1627 typename std::iterator_traits<InputIterator>::iterator_category());
1631 //===----------------------------------------------------------------------===//
1632 // InsertValueInst Class
1633 //===----------------------------------------------------------------------===//
1635 /// InsertValueInst - This instruction inserts a struct field of array element
1636 /// value into an aggregate value.
1638 class InsertValueInst : public Instruction {
1639 SmallVector<unsigned, 4> Indices;
1641 void *operator new(size_t, unsigned); // Do not implement
1642 InsertValueInst(const InsertValueInst &IVI);
1643 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1644 const Twine &NameStr);
1645 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1647 template<typename InputIterator>
1648 void init(Value *Agg, Value *Val,
1649 InputIterator IdxBegin, InputIterator IdxEnd,
1650 const Twine &NameStr,
1651 // This argument ensures that we have an iterator we can
1652 // do arithmetic on in constant time
1653 std::random_access_iterator_tag) {
1654 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1656 // There's no fundamental reason why we require at least one index
1657 // (other than weirdness with &*IdxBegin being invalid; see
1658 // getelementptr's init routine for example). But there's no
1659 // present need to support it.
1660 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1662 // This requires that the iterator points to contiguous memory.
1663 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1664 // we have to build an array here
1667 /// Constructors - Create a insertvalue instruction with a base aggregate
1668 /// value, a value to insert, and a list of indices. The first ctor can
1669 /// optionally insert before an existing instruction, the second appends
1670 /// the new instruction to the specified BasicBlock.
1671 template<typename InputIterator>
1672 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1673 InputIterator IdxEnd,
1674 const Twine &NameStr,
1675 Instruction *InsertBefore);
1676 template<typename InputIterator>
1677 inline InsertValueInst(Value *Agg, Value *Val,
1678 InputIterator IdxBegin, InputIterator IdxEnd,
1679 const Twine &NameStr, BasicBlock *InsertAtEnd);
1681 /// Constructors - These two constructors are convenience methods because one
1682 /// and two index insertvalue instructions are so common.
1683 InsertValueInst(Value *Agg, Value *Val,
1684 unsigned Idx, const Twine &NameStr = "",
1685 Instruction *InsertBefore = 0);
1686 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1687 const Twine &NameStr, BasicBlock *InsertAtEnd);
1689 // allocate space for exactly two operands
1690 void *operator new(size_t s) {
1691 return User::operator new(s, 2);
1694 template<typename InputIterator>
1695 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1696 InputIterator IdxEnd,
1697 const Twine &NameStr = "",
1698 Instruction *InsertBefore = 0) {
1699 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1700 NameStr, InsertBefore);
1702 template<typename InputIterator>
1703 static InsertValueInst *Create(Value *Agg, Value *Val,
1704 InputIterator IdxBegin, InputIterator IdxEnd,
1705 const Twine &NameStr,
1706 BasicBlock *InsertAtEnd) {
1707 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1708 NameStr, InsertAtEnd);
1711 /// Constructors - These two creators are convenience methods because one
1712 /// index insertvalue instructions are much more common than those with
1714 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1715 const Twine &NameStr = "",
1716 Instruction *InsertBefore = 0) {
1717 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1719 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1720 const Twine &NameStr,
1721 BasicBlock *InsertAtEnd) {
1722 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1725 virtual InsertValueInst *clone(LLVMContext &Context) const;
1727 /// Transparently provide more efficient getOperand methods.
1728 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1730 typedef const unsigned* idx_iterator;
1731 inline idx_iterator idx_begin() const { return Indices.begin(); }
1732 inline idx_iterator idx_end() const { return Indices.end(); }
1734 Value *getAggregateOperand() {
1735 return getOperand(0);
1737 const Value *getAggregateOperand() const {
1738 return getOperand(0);
1740 static unsigned getAggregateOperandIndex() {
1741 return 0U; // get index for modifying correct operand
1744 Value *getInsertedValueOperand() {
1745 return getOperand(1);
1747 const Value *getInsertedValueOperand() const {
1748 return getOperand(1);
1750 static unsigned getInsertedValueOperandIndex() {
1751 return 1U; // get index for modifying correct operand
1754 unsigned getNumIndices() const { // Note: always non-negative
1755 return (unsigned)Indices.size();
1758 bool hasIndices() const {
1762 // Methods for support type inquiry through isa, cast, and dyn_cast:
1763 static inline bool classof(const InsertValueInst *) { return true; }
1764 static inline bool classof(const Instruction *I) {
1765 return I->getOpcode() == Instruction::InsertValue;
1767 static inline bool classof(const Value *V) {
1768 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1773 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1776 template<typename InputIterator>
1777 InsertValueInst::InsertValueInst(Value *Agg,
1779 InputIterator IdxBegin,
1780 InputIterator IdxEnd,
1781 const Twine &NameStr,
1782 Instruction *InsertBefore)
1783 : Instruction(Agg->getType(), InsertValue,
1784 OperandTraits<InsertValueInst>::op_begin(this),
1786 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1787 typename std::iterator_traits<InputIterator>::iterator_category());
1789 template<typename InputIterator>
1790 InsertValueInst::InsertValueInst(Value *Agg,
1792 InputIterator IdxBegin,
1793 InputIterator IdxEnd,
1794 const Twine &NameStr,
1795 BasicBlock *InsertAtEnd)
1796 : Instruction(Agg->getType(), InsertValue,
1797 OperandTraits<InsertValueInst>::op_begin(this),
1799 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1800 typename std::iterator_traits<InputIterator>::iterator_category());
1803 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1805 //===----------------------------------------------------------------------===//
1807 //===----------------------------------------------------------------------===//
1809 // PHINode - The PHINode class is used to represent the magical mystical PHI
1810 // node, that can not exist in nature, but can be synthesized in a computer
1811 // scientist's overactive imagination.
1813 class PHINode : public Instruction {
1814 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1815 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1816 /// the number actually in use.
1817 unsigned ReservedSpace;
1818 PHINode(const PHINode &PN);
1819 // allocate space for exactly zero operands
1820 void *operator new(size_t s) {
1821 return User::operator new(s, 0);
1823 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1824 Instruction *InsertBefore = 0)
1825 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1830 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1831 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1836 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1837 Instruction *InsertBefore = 0) {
1838 return new PHINode(Ty, NameStr, InsertBefore);
1840 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1841 BasicBlock *InsertAtEnd) {
1842 return new PHINode(Ty, NameStr, InsertAtEnd);
1846 /// reserveOperandSpace - This method can be used to avoid repeated
1847 /// reallocation of PHI operand lists by reserving space for the correct
1848 /// number of operands before adding them. Unlike normal vector reserves,
1849 /// this method can also be used to trim the operand space.
1850 void reserveOperandSpace(unsigned NumValues) {
1851 resizeOperands(NumValues*2);
1854 virtual PHINode *clone(LLVMContext &Context) const;
1856 /// Provide fast operand accessors
1857 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1859 /// getNumIncomingValues - Return the number of incoming edges
1861 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1863 /// getIncomingValue - Return incoming value number x
1865 Value *getIncomingValue(unsigned i) const {
1866 assert(i*2 < getNumOperands() && "Invalid value number!");
1867 return getOperand(i*2);
1869 void setIncomingValue(unsigned i, Value *V) {
1870 assert(i*2 < getNumOperands() && "Invalid value number!");
1873 static unsigned getOperandNumForIncomingValue(unsigned i) {
1876 static unsigned getIncomingValueNumForOperand(unsigned i) {
1877 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1881 /// getIncomingBlock - Return incoming basic block corresponding
1882 /// to value use iterator
1884 template <typename U>
1885 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1886 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1887 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1889 /// getIncomingBlock - Return incoming basic block number x
1891 BasicBlock *getIncomingBlock(unsigned i) const {
1892 return static_cast<BasicBlock*>(getOperand(i*2+1));
1894 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1895 setOperand(i*2+1, BB);
1897 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1900 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1901 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1905 /// addIncoming - Add an incoming value to the end of the PHI list
1907 void addIncoming(Value *V, BasicBlock *BB) {
1908 assert(V && "PHI node got a null value!");
1909 assert(BB && "PHI node got a null basic block!");
1910 assert(getType() == V->getType() &&
1911 "All operands to PHI node must be the same type as the PHI node!");
1912 unsigned OpNo = NumOperands;
1913 if (OpNo+2 > ReservedSpace)
1914 resizeOperands(0); // Get more space!
1915 // Initialize some new operands.
1916 NumOperands = OpNo+2;
1917 OperandList[OpNo] = V;
1918 OperandList[OpNo+1] = BB;
1921 /// removeIncomingValue - Remove an incoming value. This is useful if a
1922 /// predecessor basic block is deleted. The value removed is returned.
1924 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1925 /// is true), the PHI node is destroyed and any uses of it are replaced with
1926 /// dummy values. The only time there should be zero incoming values to a PHI
1927 /// node is when the block is dead, so this strategy is sound.
1929 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1931 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1932 int Idx = getBasicBlockIndex(BB);
1933 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1934 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1937 /// getBasicBlockIndex - Return the first index of the specified basic
1938 /// block in the value list for this PHI. Returns -1 if no instance.
1940 int getBasicBlockIndex(const BasicBlock *BB) const {
1941 Use *OL = OperandList;
1942 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1943 if (OL[i+1].get() == BB) return i/2;
1947 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1948 return getIncomingValue(getBasicBlockIndex(BB));
1951 /// hasConstantValue - If the specified PHI node always merges together the
1952 /// same value, return the value, otherwise return null.
1954 /// If the PHI has undef operands, but all the rest of the operands are
1955 /// some unique value, return that value if it can be proved that the
1956 /// value dominates the PHI. If DT is null, use a conservative check,
1957 /// otherwise use DT to test for dominance.
1959 Value *hasConstantValue(DominatorTree *DT = 0) const;
1961 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1962 static inline bool classof(const PHINode *) { return true; }
1963 static inline bool classof(const Instruction *I) {
1964 return I->getOpcode() == Instruction::PHI;
1966 static inline bool classof(const Value *V) {
1967 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1970 void resizeOperands(unsigned NumOperands);
1974 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1977 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1980 //===----------------------------------------------------------------------===//
1982 //===----------------------------------------------------------------------===//
1984 //===---------------------------------------------------------------------------
1985 /// ReturnInst - Return a value (possibly void), from a function. Execution
1986 /// does not continue in this function any longer.
1988 class ReturnInst : public TerminatorInst {
1989 ReturnInst(const ReturnInst &RI);
1992 // ReturnInst constructors:
1993 // ReturnInst() - 'ret void' instruction
1994 // ReturnInst( null) - 'ret void' instruction
1995 // ReturnInst(Value* X) - 'ret X' instruction
1996 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1997 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1998 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1999 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2001 // NOTE: If the Value* passed is of type void then the constructor behaves as
2002 // if it was passed NULL.
2003 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2004 Instruction *InsertBefore = 0);
2005 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2006 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2008 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2009 Instruction *InsertBefore = 0) {
2010 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2012 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2013 BasicBlock *InsertAtEnd) {
2014 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2016 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2017 return new(0) ReturnInst(C, InsertAtEnd);
2019 virtual ~ReturnInst();
2021 virtual ReturnInst *clone(LLVMContext &Context) const;
2023 /// Provide fast operand accessors
2024 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2026 /// Convenience accessor
2027 Value *getReturnValue(unsigned n = 0) const {
2028 return n < getNumOperands()
2033 unsigned getNumSuccessors() const { return 0; }
2035 // Methods for support type inquiry through isa, cast, and dyn_cast:
2036 static inline bool classof(const ReturnInst *) { return true; }
2037 static inline bool classof(const Instruction *I) {
2038 return (I->getOpcode() == Instruction::Ret);
2040 static inline bool classof(const Value *V) {
2041 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2044 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2045 virtual unsigned getNumSuccessorsV() const;
2046 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2050 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2053 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2055 //===----------------------------------------------------------------------===//
2057 //===----------------------------------------------------------------------===//
2059 //===---------------------------------------------------------------------------
2060 /// BranchInst - Conditional or Unconditional Branch instruction.
2062 class BranchInst : public TerminatorInst {
2063 /// Ops list - Branches are strange. The operands are ordered:
2064 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2065 /// they don't have to check for cond/uncond branchness. These are mostly
2066 /// accessed relative from op_end().
2067 BranchInst(const BranchInst &BI);
2069 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2070 // BranchInst(BB *B) - 'br B'
2071 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2072 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2073 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2074 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2075 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2076 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2077 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2078 Instruction *InsertBefore = 0);
2079 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2080 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2081 BasicBlock *InsertAtEnd);
2083 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2084 return new(1, true) BranchInst(IfTrue, InsertBefore);
2086 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2087 Value *Cond, Instruction *InsertBefore = 0) {
2088 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2090 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2091 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2093 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2094 Value *Cond, BasicBlock *InsertAtEnd) {
2095 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2100 /// Transparently provide more efficient getOperand methods.
2101 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2103 virtual BranchInst *clone(LLVMContext &Context) const;
2105 bool isUnconditional() const { return getNumOperands() == 1; }
2106 bool isConditional() const { return getNumOperands() == 3; }
2108 Value *getCondition() const {
2109 assert(isConditional() && "Cannot get condition of an uncond branch!");
2113 void setCondition(Value *V) {
2114 assert(isConditional() && "Cannot set condition of unconditional branch!");
2118 // setUnconditionalDest - Change the current branch to an unconditional branch
2119 // targeting the specified block.
2120 // FIXME: Eliminate this ugly method.
2121 void setUnconditionalDest(BasicBlock *Dest) {
2123 if (isConditional()) { // Convert this to an uncond branch.
2127 OperandList = op_begin();
2131 unsigned getNumSuccessors() const { return 1+isConditional(); }
2133 BasicBlock *getSuccessor(unsigned i) const {
2134 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2135 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2138 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2139 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2140 *(&Op<-1>() - idx) = NewSucc;
2143 // Methods for support type inquiry through isa, cast, and dyn_cast:
2144 static inline bool classof(const BranchInst *) { return true; }
2145 static inline bool classof(const Instruction *I) {
2146 return (I->getOpcode() == Instruction::Br);
2148 static inline bool classof(const Value *V) {
2149 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2152 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2153 virtual unsigned getNumSuccessorsV() const;
2154 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2158 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2160 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2162 //===----------------------------------------------------------------------===//
2164 //===----------------------------------------------------------------------===//
2166 //===---------------------------------------------------------------------------
2167 /// SwitchInst - Multiway switch
2169 class SwitchInst : public TerminatorInst {
2170 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2171 unsigned ReservedSpace;
2172 // Operand[0] = Value to switch on
2173 // Operand[1] = Default basic block destination
2174 // Operand[2n ] = Value to match
2175 // Operand[2n+1] = BasicBlock to go to on match
2176 SwitchInst(const SwitchInst &RI);
2177 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2178 void resizeOperands(unsigned No);
2179 // allocate space for exactly zero operands
2180 void *operator new(size_t s) {
2181 return User::operator new(s, 0);
2183 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2184 /// switch on and a default destination. The number of additional cases can
2185 /// be specified here to make memory allocation more efficient. This
2186 /// constructor can also autoinsert before another instruction.
2187 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2188 Instruction *InsertBefore = 0);
2190 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2191 /// switch on and a default destination. The number of additional cases can
2192 /// be specified here to make memory allocation more efficient. This
2193 /// constructor also autoinserts at the end of the specified BasicBlock.
2194 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2195 BasicBlock *InsertAtEnd);
2197 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2198 unsigned NumCases, Instruction *InsertBefore = 0) {
2199 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2201 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2202 unsigned NumCases, BasicBlock *InsertAtEnd) {
2203 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2207 /// Provide fast operand accessors
2208 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2210 // Accessor Methods for Switch stmt
2211 Value *getCondition() const { return getOperand(0); }
2212 void setCondition(Value *V) { setOperand(0, V); }
2214 BasicBlock *getDefaultDest() const {
2215 return cast<BasicBlock>(getOperand(1));
2218 /// getNumCases - return the number of 'cases' in this switch instruction.
2219 /// Note that case #0 is always the default case.
2220 unsigned getNumCases() const {
2221 return getNumOperands()/2;
2224 /// getCaseValue - Return the specified case value. Note that case #0, the
2225 /// default destination, does not have a case value.
2226 ConstantInt *getCaseValue(unsigned i) {
2227 assert(i && i < getNumCases() && "Illegal case value to get!");
2228 return getSuccessorValue(i);
2231 /// getCaseValue - Return the specified case value. Note that case #0, the
2232 /// default destination, does not have a case value.
2233 const ConstantInt *getCaseValue(unsigned i) const {
2234 assert(i && i < getNumCases() && "Illegal case value to get!");
2235 return getSuccessorValue(i);
2238 /// findCaseValue - Search all of the case values for the specified constant.
2239 /// If it is explicitly handled, return the case number of it, otherwise
2240 /// return 0 to indicate that it is handled by the default handler.
2241 unsigned findCaseValue(const ConstantInt *C) const {
2242 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2243 if (getCaseValue(i) == C)
2248 /// findCaseDest - Finds the unique case value for a given successor. Returns
2249 /// null if the successor is not found, not unique, or is the default case.
2250 ConstantInt *findCaseDest(BasicBlock *BB) {
2251 if (BB == getDefaultDest()) return NULL;
2253 ConstantInt *CI = NULL;
2254 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2255 if (getSuccessor(i) == BB) {
2256 if (CI) return NULL; // Multiple cases lead to BB.
2257 else CI = getCaseValue(i);
2263 /// addCase - Add an entry to the switch instruction...
2265 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2267 /// removeCase - This method removes the specified successor from the switch
2268 /// instruction. Note that this cannot be used to remove the default
2269 /// destination (successor #0).
2271 void removeCase(unsigned idx);
2273 virtual SwitchInst *clone(LLVMContext &Context) const;
2275 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2276 BasicBlock *getSuccessor(unsigned idx) const {
2277 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2278 return cast<BasicBlock>(getOperand(idx*2+1));
2280 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2281 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2282 setOperand(idx*2+1, NewSucc);
2285 // getSuccessorValue - Return the value associated with the specified
2287 ConstantInt *getSuccessorValue(unsigned idx) const {
2288 assert(idx < getNumSuccessors() && "Successor # out of range!");
2289 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2292 // Methods for support type inquiry through isa, cast, and dyn_cast:
2293 static inline bool classof(const SwitchInst *) { return true; }
2294 static inline bool classof(const Instruction *I) {
2295 return I->getOpcode() == Instruction::Switch;
2297 static inline bool classof(const Value *V) {
2298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2301 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2302 virtual unsigned getNumSuccessorsV() const;
2303 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2307 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2310 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2313 //===----------------------------------------------------------------------===//
2315 //===----------------------------------------------------------------------===//
2317 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2318 /// calling convention of the call.
2320 class InvokeInst : public TerminatorInst {
2321 AttrListPtr AttributeList;
2322 InvokeInst(const InvokeInst &BI);
2323 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2324 Value* const *Args, unsigned NumArgs);
2326 template<typename InputIterator>
2327 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2328 InputIterator ArgBegin, InputIterator ArgEnd,
2329 const Twine &NameStr,
2330 // This argument ensures that we have an iterator we can
2331 // do arithmetic on in constant time
2332 std::random_access_iterator_tag) {
2333 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2335 // This requires that the iterator points to contiguous memory.
2336 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2340 /// Construct an InvokeInst given a range of arguments.
2341 /// InputIterator must be a random-access iterator pointing to
2342 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2343 /// made for random-accessness but not for contiguous storage as
2344 /// that would incur runtime overhead.
2346 /// @brief Construct an InvokeInst from a range of arguments
2347 template<typename InputIterator>
2348 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2349 InputIterator ArgBegin, InputIterator ArgEnd,
2351 const Twine &NameStr, Instruction *InsertBefore);
2353 /// Construct an InvokeInst given a range of arguments.
2354 /// InputIterator must be a random-access iterator pointing to
2355 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2356 /// made for random-accessness but not for contiguous storage as
2357 /// that would incur runtime overhead.
2359 /// @brief Construct an InvokeInst from a range of arguments
2360 template<typename InputIterator>
2361 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2362 InputIterator ArgBegin, InputIterator ArgEnd,
2364 const Twine &NameStr, BasicBlock *InsertAtEnd);
2366 template<typename InputIterator>
2367 static InvokeInst *Create(Value *Func,
2368 BasicBlock *IfNormal, BasicBlock *IfException,
2369 InputIterator ArgBegin, InputIterator ArgEnd,
2370 const Twine &NameStr = "",
2371 Instruction *InsertBefore = 0) {
2372 unsigned Values(ArgEnd - ArgBegin + 3);
2373 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2374 Values, NameStr, InsertBefore);
2376 template<typename InputIterator>
2377 static InvokeInst *Create(Value *Func,
2378 BasicBlock *IfNormal, BasicBlock *IfException,
2379 InputIterator ArgBegin, InputIterator ArgEnd,
2380 const Twine &NameStr,
2381 BasicBlock *InsertAtEnd) {
2382 unsigned Values(ArgEnd - ArgBegin + 3);
2383 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2384 Values, NameStr, InsertAtEnd);
2387 virtual InvokeInst *clone(LLVMContext &Context) const;
2389 /// Provide fast operand accessors
2390 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2392 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2394 CallingConv::ID getCallingConv() const {
2395 return static_cast<CallingConv::ID>(SubclassData);
2397 void setCallingConv(CallingConv::ID CC) {
2398 SubclassData = static_cast<unsigned>(CC);
2401 /// getAttributes - Return the parameter attributes for this invoke.
2403 const AttrListPtr &getAttributes() const { return AttributeList; }
2405 /// setAttributes - Set the parameter attributes for this invoke.
2407 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2409 /// addAttribute - adds the attribute to the list of attributes.
2410 void addAttribute(unsigned i, Attributes attr);
2412 /// removeAttribute - removes the attribute from the list of attributes.
2413 void removeAttribute(unsigned i, Attributes attr);
2415 /// @brief Determine whether the call or the callee has the given attribute.
2416 bool paramHasAttr(unsigned i, Attributes attr) const;
2418 /// @brief Extract the alignment for a call or parameter (0=unknown).
2419 unsigned getParamAlignment(unsigned i) const {
2420 return AttributeList.getParamAlignment(i);
2423 /// @brief Determine if the call does not access memory.
2424 bool doesNotAccessMemory() const {
2425 return paramHasAttr(~0, Attribute::ReadNone);
2427 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2428 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2429 else removeAttribute(~0, Attribute::ReadNone);
2432 /// @brief Determine if the call does not access or only reads memory.
2433 bool onlyReadsMemory() const {
2434 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2436 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2437 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2438 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2441 /// @brief Determine if the call cannot return.
2442 bool doesNotReturn() const {
2443 return paramHasAttr(~0, Attribute::NoReturn);
2445 void setDoesNotReturn(bool DoesNotReturn = true) {
2446 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2447 else removeAttribute(~0, Attribute::NoReturn);
2450 /// @brief Determine if the call cannot unwind.
2451 bool doesNotThrow() const {
2452 return paramHasAttr(~0, Attribute::NoUnwind);
2454 void setDoesNotThrow(bool DoesNotThrow = true) {
2455 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2456 else removeAttribute(~0, Attribute::NoUnwind);
2459 /// @brief Determine if the call returns a structure through first
2460 /// pointer argument.
2461 bool hasStructRetAttr() const {
2462 // Be friendly and also check the callee.
2463 return paramHasAttr(1, Attribute::StructRet);
2466 /// @brief Determine if any call argument is an aggregate passed by value.
2467 bool hasByValArgument() const {
2468 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2471 /// getCalledFunction - Return the function called, or null if this is an
2472 /// indirect function invocation.
2474 Function *getCalledFunction() const {
2475 return dyn_cast<Function>(getOperand(0));
2478 /// getCalledValue - Get a pointer to the function that is invoked by this
2480 const Value *getCalledValue() const { return getOperand(0); }
2481 Value *getCalledValue() { return getOperand(0); }
2483 // get*Dest - Return the destination basic blocks...
2484 BasicBlock *getNormalDest() const {
2485 return cast<BasicBlock>(getOperand(1));
2487 BasicBlock *getUnwindDest() const {
2488 return cast<BasicBlock>(getOperand(2));
2490 void setNormalDest(BasicBlock *B) {
2494 void setUnwindDest(BasicBlock *B) {
2498 BasicBlock *getSuccessor(unsigned i) const {
2499 assert(i < 2 && "Successor # out of range for invoke!");
2500 return i == 0 ? getNormalDest() : getUnwindDest();
2503 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2504 assert(idx < 2 && "Successor # out of range for invoke!");
2505 setOperand(idx+1, NewSucc);
2508 unsigned getNumSuccessors() const { return 2; }
2510 // Methods for support type inquiry through isa, cast, and dyn_cast:
2511 static inline bool classof(const InvokeInst *) { return true; }
2512 static inline bool classof(const Instruction *I) {
2513 return (I->getOpcode() == Instruction::Invoke);
2515 static inline bool classof(const Value *V) {
2516 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2519 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2520 virtual unsigned getNumSuccessorsV() const;
2521 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2525 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2528 template<typename InputIterator>
2529 InvokeInst::InvokeInst(Value *Func,
2530 BasicBlock *IfNormal, BasicBlock *IfException,
2531 InputIterator ArgBegin, InputIterator ArgEnd,
2533 const Twine &NameStr, Instruction *InsertBefore)
2534 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2535 ->getElementType())->getReturnType(),
2536 Instruction::Invoke,
2537 OperandTraits<InvokeInst>::op_end(this) - Values,
2538 Values, InsertBefore) {
2539 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2540 typename std::iterator_traits<InputIterator>::iterator_category());
2542 template<typename InputIterator>
2543 InvokeInst::InvokeInst(Value *Func,
2544 BasicBlock *IfNormal, BasicBlock *IfException,
2545 InputIterator ArgBegin, InputIterator ArgEnd,
2547 const Twine &NameStr, BasicBlock *InsertAtEnd)
2548 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2549 ->getElementType())->getReturnType(),
2550 Instruction::Invoke,
2551 OperandTraits<InvokeInst>::op_end(this) - Values,
2552 Values, InsertAtEnd) {
2553 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2554 typename std::iterator_traits<InputIterator>::iterator_category());
2557 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2559 //===----------------------------------------------------------------------===//
2561 //===----------------------------------------------------------------------===//
2563 //===---------------------------------------------------------------------------
2564 /// UnwindInst - Immediately exit the current function, unwinding the stack
2565 /// until an invoke instruction is found.
2567 class UnwindInst : public TerminatorInst {
2568 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2570 // allocate space for exactly zero operands
2571 void *operator new(size_t s) {
2572 return User::operator new(s, 0);
2574 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2575 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2577 virtual UnwindInst *clone(LLVMContext &Context) const;
2579 unsigned getNumSuccessors() const { return 0; }
2581 // Methods for support type inquiry through isa, cast, and dyn_cast:
2582 static inline bool classof(const UnwindInst *) { return true; }
2583 static inline bool classof(const Instruction *I) {
2584 return I->getOpcode() == Instruction::Unwind;
2586 static inline bool classof(const Value *V) {
2587 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2590 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2591 virtual unsigned getNumSuccessorsV() const;
2592 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2595 //===----------------------------------------------------------------------===//
2596 // UnreachableInst Class
2597 //===----------------------------------------------------------------------===//
2599 //===---------------------------------------------------------------------------
2600 /// UnreachableInst - This function has undefined behavior. In particular, the
2601 /// presence of this instruction indicates some higher level knowledge that the
2602 /// end of the block cannot be reached.
2604 class UnreachableInst : public TerminatorInst {
2605 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2607 // allocate space for exactly zero operands
2608 void *operator new(size_t s) {
2609 return User::operator new(s, 0);
2611 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2612 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2614 virtual UnreachableInst *clone(LLVMContext &Context) const;
2616 unsigned getNumSuccessors() const { return 0; }
2618 // Methods for support type inquiry through isa, cast, and dyn_cast:
2619 static inline bool classof(const UnreachableInst *) { return true; }
2620 static inline bool classof(const Instruction *I) {
2621 return I->getOpcode() == Instruction::Unreachable;
2623 static inline bool classof(const Value *V) {
2624 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2627 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2628 virtual unsigned getNumSuccessorsV() const;
2629 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2632 //===----------------------------------------------------------------------===//
2634 //===----------------------------------------------------------------------===//
2636 /// @brief This class represents a truncation of integer types.
2637 class TruncInst : public CastInst {
2639 /// @brief Constructor with insert-before-instruction semantics
2641 Value *S, ///< The value to be truncated
2642 const Type *Ty, ///< The (smaller) type to truncate to
2643 const Twine &NameStr = "", ///< A name for the new instruction
2644 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2647 /// @brief Constructor with insert-at-end-of-block semantics
2649 Value *S, ///< The value to be truncated
2650 const Type *Ty, ///< The (smaller) type to truncate to
2651 const Twine &NameStr, ///< A name for the new instruction
2652 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2655 /// @brief Clone an identical TruncInst
2656 virtual TruncInst *clone(LLVMContext &Context) const;
2658 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2659 static inline bool classof(const TruncInst *) { return true; }
2660 static inline bool classof(const Instruction *I) {
2661 return I->getOpcode() == Trunc;
2663 static inline bool classof(const Value *V) {
2664 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2668 //===----------------------------------------------------------------------===//
2670 //===----------------------------------------------------------------------===//
2672 /// @brief This class represents zero extension of integer types.
2673 class ZExtInst : public CastInst {
2675 /// @brief Constructor with insert-before-instruction semantics
2677 Value *S, ///< The value to be zero extended
2678 const Type *Ty, ///< The type to zero extend to
2679 const Twine &NameStr = "", ///< A name for the new instruction
2680 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2683 /// @brief Constructor with insert-at-end semantics.
2685 Value *S, ///< The value to be zero extended
2686 const Type *Ty, ///< The type to zero extend to
2687 const Twine &NameStr, ///< A name for the new instruction
2688 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2691 /// @brief Clone an identical ZExtInst
2692 virtual ZExtInst *clone(LLVMContext &Context) const;
2694 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2695 static inline bool classof(const ZExtInst *) { return true; }
2696 static inline bool classof(const Instruction *I) {
2697 return I->getOpcode() == ZExt;
2699 static inline bool classof(const Value *V) {
2700 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2704 //===----------------------------------------------------------------------===//
2706 //===----------------------------------------------------------------------===//
2708 /// @brief This class represents a sign extension of integer types.
2709 class SExtInst : public CastInst {
2711 /// @brief Constructor with insert-before-instruction semantics
2713 Value *S, ///< The value to be sign extended
2714 const Type *Ty, ///< The type to sign extend to
2715 const Twine &NameStr = "", ///< A name for the new instruction
2716 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2719 /// @brief Constructor with insert-at-end-of-block semantics
2721 Value *S, ///< The value to be sign extended
2722 const Type *Ty, ///< The type to sign extend to
2723 const Twine &NameStr, ///< A name for the new instruction
2724 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2727 /// @brief Clone an identical SExtInst
2728 virtual SExtInst *clone(LLVMContext &Context) const;
2730 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2731 static inline bool classof(const SExtInst *) { return true; }
2732 static inline bool classof(const Instruction *I) {
2733 return I->getOpcode() == SExt;
2735 static inline bool classof(const Value *V) {
2736 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2740 //===----------------------------------------------------------------------===//
2741 // FPTruncInst Class
2742 //===----------------------------------------------------------------------===//
2744 /// @brief This class represents a truncation of floating point types.
2745 class FPTruncInst : public CastInst {
2747 /// @brief Constructor with insert-before-instruction semantics
2749 Value *S, ///< The value to be truncated
2750 const Type *Ty, ///< The type to truncate to
2751 const Twine &NameStr = "", ///< A name for the new instruction
2752 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2755 /// @brief Constructor with insert-before-instruction semantics
2757 Value *S, ///< The value to be truncated
2758 const Type *Ty, ///< The type to truncate to
2759 const Twine &NameStr, ///< A name for the new instruction
2760 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2763 /// @brief Clone an identical FPTruncInst
2764 virtual FPTruncInst *clone(LLVMContext &Context) const;
2766 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2767 static inline bool classof(const FPTruncInst *) { return true; }
2768 static inline bool classof(const Instruction *I) {
2769 return I->getOpcode() == FPTrunc;
2771 static inline bool classof(const Value *V) {
2772 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2776 //===----------------------------------------------------------------------===//
2778 //===----------------------------------------------------------------------===//
2780 /// @brief This class represents an extension of floating point types.
2781 class FPExtInst : public CastInst {
2783 /// @brief Constructor with insert-before-instruction semantics
2785 Value *S, ///< The value to be extended
2786 const Type *Ty, ///< The type to extend to
2787 const Twine &NameStr = "", ///< A name for the new instruction
2788 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2791 /// @brief Constructor with insert-at-end-of-block semantics
2793 Value *S, ///< The value to be extended
2794 const Type *Ty, ///< The type to extend to
2795 const Twine &NameStr, ///< A name for the new instruction
2796 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2799 /// @brief Clone an identical FPExtInst
2800 virtual FPExtInst *clone(LLVMContext &Context) const;
2802 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2803 static inline bool classof(const FPExtInst *) { return true; }
2804 static inline bool classof(const Instruction *I) {
2805 return I->getOpcode() == FPExt;
2807 static inline bool classof(const Value *V) {
2808 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2812 //===----------------------------------------------------------------------===//
2814 //===----------------------------------------------------------------------===//
2816 /// @brief This class represents a cast unsigned integer to floating point.
2817 class UIToFPInst : public CastInst {
2819 /// @brief Constructor with insert-before-instruction semantics
2821 Value *S, ///< The value to be converted
2822 const Type *Ty, ///< The type to convert to
2823 const Twine &NameStr = "", ///< A name for the new instruction
2824 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2827 /// @brief Constructor with insert-at-end-of-block semantics
2829 Value *S, ///< The value to be converted
2830 const Type *Ty, ///< The type to convert to
2831 const Twine &NameStr, ///< A name for the new instruction
2832 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2835 /// @brief Clone an identical UIToFPInst
2836 virtual UIToFPInst *clone(LLVMContext &Context) const;
2838 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2839 static inline bool classof(const UIToFPInst *) { return true; }
2840 static inline bool classof(const Instruction *I) {
2841 return I->getOpcode() == UIToFP;
2843 static inline bool classof(const Value *V) {
2844 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2848 //===----------------------------------------------------------------------===//
2850 //===----------------------------------------------------------------------===//
2852 /// @brief This class represents a cast from signed integer to floating point.
2853 class SIToFPInst : public CastInst {
2855 /// @brief Constructor with insert-before-instruction semantics
2857 Value *S, ///< The value to be converted
2858 const Type *Ty, ///< The type to convert to
2859 const Twine &NameStr = "", ///< A name for the new instruction
2860 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2863 /// @brief Constructor with insert-at-end-of-block semantics
2865 Value *S, ///< The value to be converted
2866 const Type *Ty, ///< The type to convert to
2867 const Twine &NameStr, ///< A name for the new instruction
2868 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2871 /// @brief Clone an identical SIToFPInst
2872 virtual SIToFPInst *clone(LLVMContext &Context) const;
2874 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2875 static inline bool classof(const SIToFPInst *) { return true; }
2876 static inline bool classof(const Instruction *I) {
2877 return I->getOpcode() == SIToFP;
2879 static inline bool classof(const Value *V) {
2880 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2884 //===----------------------------------------------------------------------===//
2886 //===----------------------------------------------------------------------===//
2888 /// @brief This class represents a cast from floating point to unsigned integer
2889 class FPToUIInst : public CastInst {
2891 /// @brief Constructor with insert-before-instruction semantics
2893 Value *S, ///< The value to be converted
2894 const Type *Ty, ///< The type to convert to
2895 const Twine &NameStr = "", ///< A name for the new instruction
2896 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2899 /// @brief Constructor with insert-at-end-of-block semantics
2901 Value *S, ///< The value to be converted
2902 const Type *Ty, ///< The type to convert to
2903 const Twine &NameStr, ///< A name for the new instruction
2904 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2907 /// @brief Clone an identical FPToUIInst
2908 virtual FPToUIInst *clone(LLVMContext &Context) const;
2910 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2911 static inline bool classof(const FPToUIInst *) { return true; }
2912 static inline bool classof(const Instruction *I) {
2913 return I->getOpcode() == FPToUI;
2915 static inline bool classof(const Value *V) {
2916 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2920 //===----------------------------------------------------------------------===//
2922 //===----------------------------------------------------------------------===//
2924 /// @brief This class represents a cast from floating point to signed integer.
2925 class FPToSIInst : public CastInst {
2927 /// @brief Constructor with insert-before-instruction semantics
2929 Value *S, ///< The value to be converted
2930 const Type *Ty, ///< The type to convert to
2931 const Twine &NameStr = "", ///< A name for the new instruction
2932 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2935 /// @brief Constructor with insert-at-end-of-block semantics
2937 Value *S, ///< The value to be converted
2938 const Type *Ty, ///< The type to convert to
2939 const Twine &NameStr, ///< A name for the new instruction
2940 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2943 /// @brief Clone an identical FPToSIInst
2944 virtual FPToSIInst *clone(LLVMContext &Context) const;
2946 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2947 static inline bool classof(const FPToSIInst *) { return true; }
2948 static inline bool classof(const Instruction *I) {
2949 return I->getOpcode() == FPToSI;
2951 static inline bool classof(const Value *V) {
2952 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2956 //===----------------------------------------------------------------------===//
2957 // IntToPtrInst Class
2958 //===----------------------------------------------------------------------===//
2960 /// @brief This class represents a cast from an integer to a pointer.
2961 class IntToPtrInst : public CastInst {
2963 /// @brief Constructor with insert-before-instruction semantics
2965 Value *S, ///< The value to be converted
2966 const Type *Ty, ///< The type to convert to
2967 const Twine &NameStr = "", ///< A name for the new instruction
2968 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2971 /// @brief Constructor with insert-at-end-of-block semantics
2973 Value *S, ///< The value to be converted
2974 const Type *Ty, ///< The type to convert to
2975 const Twine &NameStr, ///< A name for the new instruction
2976 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2979 /// @brief Clone an identical IntToPtrInst
2980 virtual IntToPtrInst *clone(LLVMContext &Context) const;
2982 // Methods for support type inquiry through isa, cast, and dyn_cast:
2983 static inline bool classof(const IntToPtrInst *) { return true; }
2984 static inline bool classof(const Instruction *I) {
2985 return I->getOpcode() == IntToPtr;
2987 static inline bool classof(const Value *V) {
2988 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2992 //===----------------------------------------------------------------------===//
2993 // PtrToIntInst Class
2994 //===----------------------------------------------------------------------===//
2996 /// @brief This class represents a cast from a pointer to an integer
2997 class PtrToIntInst : public CastInst {
2999 /// @brief Constructor with insert-before-instruction semantics
3001 Value *S, ///< The value to be converted
3002 const Type *Ty, ///< The type to convert to
3003 const Twine &NameStr = "", ///< A name for the new instruction
3004 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3007 /// @brief Constructor with insert-at-end-of-block semantics
3009 Value *S, ///< The value to be converted
3010 const Type *Ty, ///< The type to convert to
3011 const Twine &NameStr, ///< A name for the new instruction
3012 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3015 /// @brief Clone an identical PtrToIntInst
3016 virtual PtrToIntInst *clone(LLVMContext &Context) const;
3018 // Methods for support type inquiry through isa, cast, and dyn_cast:
3019 static inline bool classof(const PtrToIntInst *) { return true; }
3020 static inline bool classof(const Instruction *I) {
3021 return I->getOpcode() == PtrToInt;
3023 static inline bool classof(const Value *V) {
3024 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3028 //===----------------------------------------------------------------------===//
3029 // BitCastInst Class
3030 //===----------------------------------------------------------------------===//
3032 /// @brief This class represents a no-op cast from one type to another.
3033 class BitCastInst : public CastInst {
3035 /// @brief Constructor with insert-before-instruction semantics
3037 Value *S, ///< The value to be casted
3038 const Type *Ty, ///< The type to casted to
3039 const Twine &NameStr = "", ///< A name for the new instruction
3040 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3043 /// @brief Constructor with insert-at-end-of-block semantics
3045 Value *S, ///< The value to be casted
3046 const Type *Ty, ///< The type to casted to
3047 const Twine &NameStr, ///< A name for the new instruction
3048 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3051 /// @brief Clone an identical BitCastInst
3052 virtual BitCastInst *clone(LLVMContext &Context) const;
3054 // Methods for support type inquiry through isa, cast, and dyn_cast:
3055 static inline bool classof(const BitCastInst *) { return true; }
3056 static inline bool classof(const Instruction *I) {
3057 return I->getOpcode() == BitCast;
3059 static inline bool classof(const Value *V) {
3060 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3064 } // End llvm namespace