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> : public 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 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 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 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 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 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
606 /// See LangRef.html for the meaning of inbounds on a getelementptr.
607 void setIsInBounds(bool);
609 // Methods for support type inquiry through isa, cast, and dyn_cast:
610 static inline bool classof(const GetElementPtrInst *) { return true; }
611 static inline bool classof(const Instruction *I) {
612 return (I->getOpcode() == Instruction::GetElementPtr);
614 static inline bool classof(const Value *V) {
615 return isa<Instruction>(V) && classof(cast<Instruction>(V));
620 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
623 template<typename InputIterator>
624 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
625 InputIterator IdxBegin,
626 InputIterator IdxEnd,
628 const Twine &NameStr,
629 Instruction *InsertBefore)
630 : Instruction(PointerType::get(checkType(
631 getIndexedType(Ptr->getType(),
633 cast<PointerType>(Ptr->getType())
634 ->getAddressSpace()),
636 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
637 Values, InsertBefore) {
638 init(Ptr, IdxBegin, IdxEnd, NameStr,
639 typename std::iterator_traits<InputIterator>::iterator_category());
641 template<typename InputIterator>
642 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
643 InputIterator IdxBegin,
644 InputIterator IdxEnd,
646 const Twine &NameStr,
647 BasicBlock *InsertAtEnd)
648 : Instruction(PointerType::get(checkType(
649 getIndexedType(Ptr->getType(),
651 cast<PointerType>(Ptr->getType())
652 ->getAddressSpace()),
654 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
655 Values, InsertAtEnd) {
656 init(Ptr, IdxBegin, IdxEnd, NameStr,
657 typename std::iterator_traits<InputIterator>::iterator_category());
661 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
664 //===----------------------------------------------------------------------===//
666 //===----------------------------------------------------------------------===//
668 /// This instruction compares its operands according to the predicate given
669 /// to the constructor. It only operates on integers or pointers. The operands
670 /// must be identical types.
671 /// @brief Represent an integer comparison operator.
672 class ICmpInst: public CmpInst {
674 /// @brief Constructor with insert-before-instruction semantics.
676 Instruction *InsertBefore, ///< Where to insert
677 Predicate pred, ///< The predicate to use for the comparison
678 Value *LHS, ///< The left-hand-side of the expression
679 Value *RHS, ///< The right-hand-side of the expression
680 const Twine &NameStr = "" ///< Name of the instruction
681 ) : CmpInst(makeCmpResultType(LHS->getType()),
682 Instruction::ICmp, pred, LHS, RHS, NameStr,
684 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
685 pred <= CmpInst::LAST_ICMP_PREDICATE &&
686 "Invalid ICmp predicate value");
687 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
688 "Both operands to ICmp instruction are not of the same type!");
689 // Check that the operands are the right type
690 assert((getOperand(0)->getType()->isIntOrIntVector() ||
691 isa<PointerType>(getOperand(0)->getType())) &&
692 "Invalid operand types for ICmp instruction");
695 /// @brief Constructor with insert-at-end semantics.
697 BasicBlock &InsertAtEnd, ///< Block to insert into.
698 Predicate pred, ///< The predicate to use for the comparison
699 Value *LHS, ///< The left-hand-side of the expression
700 Value *RHS, ///< The right-hand-side of the expression
701 const Twine &NameStr = "" ///< Name of the instruction
702 ) : CmpInst(makeCmpResultType(LHS->getType()),
703 Instruction::ICmp, pred, LHS, RHS, NameStr,
705 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
706 pred <= CmpInst::LAST_ICMP_PREDICATE &&
707 "Invalid ICmp predicate value");
708 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
709 "Both operands to ICmp instruction are not of the same type!");
710 // Check that the operands are the right type
711 assert((getOperand(0)->getType()->isIntOrIntVector() ||
712 isa<PointerType>(getOperand(0)->getType())) &&
713 "Invalid operand types for ICmp instruction");
716 /// @brief Constructor with no-insertion semantics
718 Predicate pred, ///< The predicate to use for the comparison
719 Value *LHS, ///< The left-hand-side of the expression
720 Value *RHS, ///< The right-hand-side of the expression
721 const Twine &NameStr = "" ///< Name of the instruction
722 ) : CmpInst(makeCmpResultType(LHS->getType()),
723 Instruction::ICmp, pred, LHS, RHS, NameStr) {
724 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
725 pred <= CmpInst::LAST_ICMP_PREDICATE &&
726 "Invalid ICmp predicate value");
727 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
728 "Both operands to ICmp instruction are not of the same type!");
729 // Check that the operands are the right type
730 assert((getOperand(0)->getType()->isIntOrIntVector() ||
731 isa<PointerType>(getOperand(0)->getType())) &&
732 "Invalid operand types for ICmp instruction");
735 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
736 /// @returns the predicate that would be the result if the operand were
737 /// regarded as signed.
738 /// @brief Return the signed version of the predicate
739 Predicate getSignedPredicate() const {
740 return getSignedPredicate(getPredicate());
743 /// This is a static version that you can use without an instruction.
744 /// @brief Return the signed version of the predicate.
745 static Predicate getSignedPredicate(Predicate pred);
747 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
748 /// @returns the predicate that would be the result if the operand were
749 /// regarded as unsigned.
750 /// @brief Return the unsigned version of the predicate
751 Predicate getUnsignedPredicate() const {
752 return getUnsignedPredicate(getPredicate());
755 /// This is a static version that you can use without an instruction.
756 /// @brief Return the unsigned version of the predicate.
757 static Predicate getUnsignedPredicate(Predicate pred);
759 /// isEquality - Return true if this predicate is either EQ or NE. This also
760 /// tests for commutativity.
761 static bool isEquality(Predicate P) {
762 return P == ICMP_EQ || P == ICMP_NE;
765 /// isEquality - Return true if this predicate is either EQ or NE. This also
766 /// tests for commutativity.
767 bool isEquality() const {
768 return isEquality(getPredicate());
771 /// @returns true if the predicate of this ICmpInst is commutative
772 /// @brief Determine if this relation is commutative.
773 bool isCommutative() const { return isEquality(); }
775 /// isRelational - Return true if the predicate is relational (not EQ or NE).
777 bool isRelational() const {
778 return !isEquality();
781 /// isRelational - Return true if the predicate is relational (not EQ or NE).
783 static bool isRelational(Predicate P) {
784 return !isEquality(P);
787 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
788 /// @brief Determine if this instruction's predicate is signed.
789 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
791 /// @returns true if the predicate provided is signed, false otherwise
792 /// @brief Determine if the predicate is signed.
793 static bool isSignedPredicate(Predicate pred);
795 /// @returns true if the specified compare predicate is
796 /// true when both operands are equal...
797 /// @brief Determine if the icmp is true when both operands are equal
798 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
799 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
800 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
801 pred == ICmpInst::ICMP_SLE;
804 /// @returns true if the specified compare instruction is
805 /// true when both operands are equal...
806 /// @brief Determine if the ICmpInst returns true when both operands are equal
807 bool isTrueWhenEqual() {
808 return isTrueWhenEqual(getPredicate());
811 /// Initialize a set of values that all satisfy the predicate with C.
812 /// @brief Make a ConstantRange for a relation with a constant value.
813 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
815 /// Exchange the two operands to this instruction in such a way that it does
816 /// not modify the semantics of the instruction. The predicate value may be
817 /// changed to retain the same result if the predicate is order dependent
819 /// @brief Swap operands and adjust predicate.
820 void swapOperands() {
821 SubclassData = getSwappedPredicate();
822 Op<0>().swap(Op<1>());
825 virtual ICmpInst *clone(LLVMContext &Context) const;
827 // Methods for support type inquiry through isa, cast, and dyn_cast:
828 static inline bool classof(const ICmpInst *) { return true; }
829 static inline bool classof(const Instruction *I) {
830 return I->getOpcode() == Instruction::ICmp;
832 static inline bool classof(const Value *V) {
833 return isa<Instruction>(V) && classof(cast<Instruction>(V));
838 //===----------------------------------------------------------------------===//
840 //===----------------------------------------------------------------------===//
842 /// This instruction compares its operands according to the predicate given
843 /// to the constructor. It only operates on floating point values or packed
844 /// vectors of floating point values. The operands must be identical types.
845 /// @brief Represents a floating point comparison operator.
846 class FCmpInst: public CmpInst {
848 /// @brief Constructor with insert-before-instruction semantics.
850 Instruction *InsertBefore, ///< Where to insert
851 Predicate pred, ///< The predicate to use for the comparison
852 Value *LHS, ///< The left-hand-side of the expression
853 Value *RHS, ///< The right-hand-side of the expression
854 const Twine &NameStr = "" ///< Name of the instruction
855 ) : CmpInst(makeCmpResultType(LHS->getType()),
856 Instruction::FCmp, pred, LHS, RHS, NameStr,
858 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
859 "Invalid FCmp predicate value");
860 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
861 "Both operands to FCmp instruction are not of the same type!");
862 // Check that the operands are the right type
863 assert(getOperand(0)->getType()->isFPOrFPVector() &&
864 "Invalid operand types for FCmp instruction");
867 /// @brief Constructor with insert-at-end semantics.
869 BasicBlock &InsertAtEnd, ///< Block to insert into.
870 Predicate pred, ///< The predicate to use for the comparison
871 Value *LHS, ///< The left-hand-side of the expression
872 Value *RHS, ///< The right-hand-side of the expression
873 const Twine &NameStr = "" ///< Name of the instruction
874 ) : CmpInst(makeCmpResultType(LHS->getType()),
875 Instruction::FCmp, pred, LHS, RHS, NameStr,
877 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
878 "Invalid FCmp predicate value");
879 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
880 "Both operands to FCmp instruction are not of the same type!");
881 // Check that the operands are the right type
882 assert(getOperand(0)->getType()->isFPOrFPVector() &&
883 "Invalid operand types for FCmp instruction");
886 /// @brief Constructor with no-insertion semantics
888 Predicate pred, ///< The predicate to use for the comparison
889 Value *LHS, ///< The left-hand-side of the expression
890 Value *RHS, ///< The right-hand-side of the expression
891 const Twine &NameStr = "" ///< Name of the instruction
892 ) : CmpInst(makeCmpResultType(LHS->getType()),
893 Instruction::FCmp, pred, LHS, RHS, NameStr) {
894 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
895 "Invalid FCmp predicate value");
896 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
897 "Both operands to FCmp instruction are not of the same type!");
898 // Check that the operands are the right type
899 assert(getOperand(0)->getType()->isFPOrFPVector() &&
900 "Invalid operand types for FCmp instruction");
903 /// @returns true if the predicate of this instruction is EQ or NE.
904 /// @brief Determine if this is an equality predicate.
905 bool isEquality() const {
906 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
907 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
910 /// @returns true if the predicate of this instruction is commutative.
911 /// @brief Determine if this is a commutative predicate.
912 bool isCommutative() const {
913 return isEquality() ||
914 SubclassData == FCMP_FALSE ||
915 SubclassData == FCMP_TRUE ||
916 SubclassData == FCMP_ORD ||
917 SubclassData == FCMP_UNO;
920 /// @returns true if the predicate is relational (not EQ or NE).
921 /// @brief Determine if this a relational predicate.
922 bool isRelational() const { return !isEquality(); }
924 /// Exchange the two operands to this instruction in such a way that it does
925 /// not modify the semantics of the instruction. The predicate value may be
926 /// changed to retain the same result if the predicate is order dependent
928 /// @brief Swap operands and adjust predicate.
929 void swapOperands() {
930 SubclassData = getSwappedPredicate();
931 Op<0>().swap(Op<1>());
934 virtual FCmpInst *clone(LLVMContext &Context) const;
936 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
937 static inline bool classof(const FCmpInst *) { return true; }
938 static inline bool classof(const Instruction *I) {
939 return I->getOpcode() == Instruction::FCmp;
941 static inline bool classof(const Value *V) {
942 return isa<Instruction>(V) && classof(cast<Instruction>(V));
946 //===----------------------------------------------------------------------===//
948 //===----------------------------------------------------------------------===//
949 /// CallInst - This class represents a function call, abstracting a target
950 /// machine's calling convention. This class uses low bit of the SubClassData
951 /// field to indicate whether or not this is a tail call. The rest of the bits
952 /// hold the calling convention of the call.
955 class CallInst : public Instruction {
956 AttrListPtr AttributeList; ///< parameter attributes for call
957 CallInst(const CallInst &CI);
958 void init(Value *Func, Value* const *Params, unsigned NumParams);
959 void init(Value *Func, Value *Actual1, Value *Actual2);
960 void init(Value *Func, Value *Actual);
961 void init(Value *Func);
963 template<typename InputIterator>
964 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
965 const Twine &NameStr,
966 // This argument ensures that we have an iterator we can
967 // do arithmetic on in constant time
968 std::random_access_iterator_tag) {
969 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
971 // This requires that the iterator points to contiguous memory.
972 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
976 /// Construct a CallInst given a range of arguments. InputIterator
977 /// must be a random-access iterator pointing to contiguous storage
978 /// (e.g. a std::vector<>::iterator). Checks are made for
979 /// random-accessness but not for contiguous storage as that would
980 /// incur runtime overhead.
981 /// @brief Construct a CallInst from a range of arguments
982 template<typename InputIterator>
983 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
984 const Twine &NameStr, Instruction *InsertBefore);
986 /// Construct a CallInst given a range of arguments. InputIterator
987 /// must be a random-access iterator pointing to contiguous storage
988 /// (e.g. a std::vector<>::iterator). Checks are made for
989 /// random-accessness but not for contiguous storage as that would
990 /// incur runtime overhead.
991 /// @brief Construct a CallInst from a range of arguments
992 template<typename InputIterator>
993 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
994 const Twine &NameStr, BasicBlock *InsertAtEnd);
996 CallInst(Value *F, Value *Actual, const Twine &NameStr,
997 Instruction *InsertBefore);
998 CallInst(Value *F, Value *Actual, const Twine &NameStr,
999 BasicBlock *InsertAtEnd);
1000 explicit CallInst(Value *F, const Twine &NameStr,
1001 Instruction *InsertBefore);
1002 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1004 template<typename InputIterator>
1005 static CallInst *Create(Value *Func,
1006 InputIterator ArgBegin, InputIterator ArgEnd,
1007 const Twine &NameStr = "",
1008 Instruction *InsertBefore = 0) {
1009 return new((unsigned)(ArgEnd - ArgBegin + 1))
1010 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1012 template<typename InputIterator>
1013 static CallInst *Create(Value *Func,
1014 InputIterator ArgBegin, InputIterator ArgEnd,
1015 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1016 return new((unsigned)(ArgEnd - ArgBegin + 1))
1017 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1019 static CallInst *Create(Value *F, Value *Actual,
1020 const Twine &NameStr = "",
1021 Instruction *InsertBefore = 0) {
1022 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1024 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1025 BasicBlock *InsertAtEnd) {
1026 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1028 static CallInst *Create(Value *F, const Twine &NameStr = "",
1029 Instruction *InsertBefore = 0) {
1030 return new(1) CallInst(F, NameStr, InsertBefore);
1032 static CallInst *Create(Value *F, const Twine &NameStr,
1033 BasicBlock *InsertAtEnd) {
1034 return new(1) CallInst(F, NameStr, InsertAtEnd);
1039 bool isTailCall() const { return SubclassData & 1; }
1040 void setTailCall(bool isTC = true) {
1041 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1044 virtual CallInst *clone(LLVMContext &Context) const;
1046 /// Provide fast operand accessors
1047 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1049 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1051 CallingConv::ID getCallingConv() const {
1052 return static_cast<CallingConv::ID>(SubclassData >> 1);
1054 void setCallingConv(CallingConv::ID CC) {
1055 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
1058 /// getAttributes - Return the parameter attributes for this call.
1060 const AttrListPtr &getAttributes() const { return AttributeList; }
1062 /// setAttributes - Set the parameter attributes for this call.
1064 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1066 /// addAttribute - adds the attribute to the list of attributes.
1067 void addAttribute(unsigned i, Attributes attr);
1069 /// removeAttribute - removes the attribute from the list of attributes.
1070 void removeAttribute(unsigned i, Attributes attr);
1072 /// @brief Determine whether the call or the callee has the given attribute.
1073 bool paramHasAttr(unsigned i, Attributes attr) const;
1075 /// @brief Extract the alignment for a call or parameter (0=unknown).
1076 unsigned getParamAlignment(unsigned i) const {
1077 return AttributeList.getParamAlignment(i);
1080 /// @brief Determine if the call does not access memory.
1081 bool doesNotAccessMemory() const {
1082 return paramHasAttr(~0, Attribute::ReadNone);
1084 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1085 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1086 else removeAttribute(~0, Attribute::ReadNone);
1089 /// @brief Determine if the call does not access or only reads memory.
1090 bool onlyReadsMemory() const {
1091 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1093 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1094 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1095 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1098 /// @brief Determine if the call cannot return.
1099 bool doesNotReturn() const {
1100 return paramHasAttr(~0, Attribute::NoReturn);
1102 void setDoesNotReturn(bool DoesNotReturn = true) {
1103 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1104 else removeAttribute(~0, Attribute::NoReturn);
1107 /// @brief Determine if the call cannot unwind.
1108 bool doesNotThrow() const {
1109 return paramHasAttr(~0, Attribute::NoUnwind);
1111 void setDoesNotThrow(bool DoesNotThrow = true) {
1112 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1113 else removeAttribute(~0, Attribute::NoUnwind);
1116 /// @brief Determine if the call returns a structure through first
1117 /// pointer argument.
1118 bool hasStructRetAttr() const {
1119 // Be friendly and also check the callee.
1120 return paramHasAttr(1, Attribute::StructRet);
1123 /// @brief Determine if any call argument is an aggregate passed by value.
1124 bool hasByValArgument() const {
1125 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1128 /// getCalledFunction - Return the function called, or null if this is an
1129 /// indirect function invocation.
1131 Function *getCalledFunction() const {
1132 return dyn_cast<Function>(Op<0>());
1135 /// getCalledValue - Get a pointer to the function that is invoked by this
1137 const Value *getCalledValue() const { return Op<0>(); }
1138 Value *getCalledValue() { return Op<0>(); }
1140 // Methods for support type inquiry through isa, cast, and dyn_cast:
1141 static inline bool classof(const CallInst *) { return true; }
1142 static inline bool classof(const Instruction *I) {
1143 return I->getOpcode() == Instruction::Call;
1145 static inline bool classof(const Value *V) {
1146 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1151 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1154 template<typename InputIterator>
1155 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1156 const Twine &NameStr, BasicBlock *InsertAtEnd)
1157 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1158 ->getElementType())->getReturnType(),
1160 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1161 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1162 init(Func, ArgBegin, ArgEnd, NameStr,
1163 typename std::iterator_traits<InputIterator>::iterator_category());
1166 template<typename InputIterator>
1167 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1168 const Twine &NameStr, Instruction *InsertBefore)
1169 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1170 ->getElementType())->getReturnType(),
1172 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1173 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1174 init(Func, ArgBegin, ArgEnd, NameStr,
1175 typename std::iterator_traits<InputIterator>::iterator_category());
1178 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1180 //===----------------------------------------------------------------------===//
1182 //===----------------------------------------------------------------------===//
1184 /// SelectInst - This class represents the LLVM 'select' instruction.
1186 class SelectInst : public Instruction {
1187 void init(Value *C, Value *S1, Value *S2) {
1188 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1194 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1195 Instruction *InsertBefore)
1196 : Instruction(S1->getType(), Instruction::Select,
1197 &Op<0>(), 3, InsertBefore) {
1201 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1202 BasicBlock *InsertAtEnd)
1203 : Instruction(S1->getType(), Instruction::Select,
1204 &Op<0>(), 3, InsertAtEnd) {
1209 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1210 const Twine &NameStr = "",
1211 Instruction *InsertBefore = 0) {
1212 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1214 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1215 const Twine &NameStr,
1216 BasicBlock *InsertAtEnd) {
1217 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1220 Value *getCondition() const { return Op<0>(); }
1221 Value *getTrueValue() const { return Op<1>(); }
1222 Value *getFalseValue() const { return Op<2>(); }
1224 /// areInvalidOperands - Return a string if the specified operands are invalid
1225 /// for a select operation, otherwise return null.
1226 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1228 /// Transparently provide more efficient getOperand methods.
1229 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1231 OtherOps getOpcode() const {
1232 return static_cast<OtherOps>(Instruction::getOpcode());
1235 virtual SelectInst *clone(LLVMContext &Context) const;
1237 // Methods for support type inquiry through isa, cast, and dyn_cast:
1238 static inline bool classof(const SelectInst *) { return true; }
1239 static inline bool classof(const Instruction *I) {
1240 return I->getOpcode() == Instruction::Select;
1242 static inline bool classof(const Value *V) {
1243 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1248 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1251 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1253 //===----------------------------------------------------------------------===//
1255 //===----------------------------------------------------------------------===//
1257 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1258 /// an argument of the specified type given a va_list and increments that list
1260 class VAArgInst : public UnaryInstruction {
1262 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1263 Instruction *InsertBefore = 0)
1264 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1267 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1268 BasicBlock *InsertAtEnd)
1269 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1273 virtual VAArgInst *clone(LLVMContext &Context) const;
1275 // Methods for support type inquiry through isa, cast, and dyn_cast:
1276 static inline bool classof(const VAArgInst *) { return true; }
1277 static inline bool classof(const Instruction *I) {
1278 return I->getOpcode() == VAArg;
1280 static inline bool classof(const Value *V) {
1281 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1285 //===----------------------------------------------------------------------===//
1286 // ExtractElementInst Class
1287 //===----------------------------------------------------------------------===//
1289 /// ExtractElementInst - This instruction extracts a single (scalar)
1290 /// element from a VectorType value
1292 class ExtractElementInst : public Instruction {
1293 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1294 Instruction *InsertBefore = 0);
1295 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1296 BasicBlock *InsertAtEnd);
1298 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1299 const Twine &NameStr = "",
1300 Instruction *InsertBefore = 0) {
1301 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1303 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1304 const Twine &NameStr,
1305 BasicBlock *InsertAtEnd) {
1306 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1309 /// isValidOperands - Return true if an extractelement instruction can be
1310 /// formed with the specified operands.
1311 static bool isValidOperands(const Value *Vec, const Value *Idx);
1313 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1315 /// Transparently provide more efficient getOperand methods.
1316 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1318 // Methods for support type inquiry through isa, cast, and dyn_cast:
1319 static inline bool classof(const ExtractElementInst *) { return true; }
1320 static inline bool classof(const Instruction *I) {
1321 return I->getOpcode() == Instruction::ExtractElement;
1323 static inline bool classof(const Value *V) {
1324 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1329 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1332 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1334 //===----------------------------------------------------------------------===//
1335 // InsertElementInst Class
1336 //===----------------------------------------------------------------------===//
1338 /// InsertElementInst - This instruction inserts a single (scalar)
1339 /// element into a VectorType value
1341 class InsertElementInst : public Instruction {
1342 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1343 const Twine &NameStr = "",
1344 Instruction *InsertBefore = 0);
1345 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1346 const Twine &NameStr, BasicBlock *InsertAtEnd);
1348 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1349 const Twine &NameStr = "",
1350 Instruction *InsertBefore = 0) {
1351 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1353 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1354 const Twine &NameStr,
1355 BasicBlock *InsertAtEnd) {
1356 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1359 /// isValidOperands - Return true if an insertelement instruction can be
1360 /// formed with the specified operands.
1361 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1364 virtual InsertElementInst *clone(LLVMContext &Context) const;
1366 /// getType - Overload to return most specific vector type.
1368 const VectorType *getType() const {
1369 return reinterpret_cast<const VectorType*>(Instruction::getType());
1372 /// Transparently provide more efficient getOperand methods.
1373 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1375 // Methods for support type inquiry through isa, cast, and dyn_cast:
1376 static inline bool classof(const InsertElementInst *) { return true; }
1377 static inline bool classof(const Instruction *I) {
1378 return I->getOpcode() == Instruction::InsertElement;
1380 static inline bool classof(const Value *V) {
1381 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1386 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1389 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1391 //===----------------------------------------------------------------------===//
1392 // ShuffleVectorInst Class
1393 //===----------------------------------------------------------------------===//
1395 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1398 class ShuffleVectorInst : public Instruction {
1400 // allocate space for exactly three operands
1401 void *operator new(size_t s) {
1402 return User::operator new(s, 3);
1404 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1405 const Twine &NameStr = "",
1406 Instruction *InsertBefor = 0);
1407 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1408 const Twine &NameStr, BasicBlock *InsertAtEnd);
1410 /// isValidOperands - Return true if a shufflevector instruction can be
1411 /// formed with the specified operands.
1412 static bool isValidOperands(const Value *V1, const Value *V2,
1415 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1417 /// getType - Overload to return most specific vector type.
1419 const VectorType *getType() const {
1420 return reinterpret_cast<const VectorType*>(Instruction::getType());
1423 /// Transparently provide more efficient getOperand methods.
1424 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1426 /// getMaskValue - Return the index from the shuffle mask for the specified
1427 /// output result. This is either -1 if the element is undef or a number less
1428 /// than 2*numelements.
1429 int getMaskValue(unsigned i) const;
1431 // Methods for support type inquiry through isa, cast, and dyn_cast:
1432 static inline bool classof(const ShuffleVectorInst *) { return true; }
1433 static inline bool classof(const Instruction *I) {
1434 return I->getOpcode() == Instruction::ShuffleVector;
1436 static inline bool classof(const Value *V) {
1437 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1442 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1445 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1447 //===----------------------------------------------------------------------===//
1448 // ExtractValueInst Class
1449 //===----------------------------------------------------------------------===//
1451 /// ExtractValueInst - This instruction extracts a struct member or array
1452 /// element value from an aggregate value.
1454 class ExtractValueInst : public UnaryInstruction {
1455 SmallVector<unsigned, 4> Indices;
1457 ExtractValueInst(const ExtractValueInst &EVI);
1458 void init(const unsigned *Idx, unsigned NumIdx,
1459 const Twine &NameStr);
1460 void init(unsigned Idx, const Twine &NameStr);
1462 template<typename InputIterator>
1463 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1464 const Twine &NameStr,
1465 // This argument ensures that we have an iterator we can
1466 // do arithmetic on in constant time
1467 std::random_access_iterator_tag) {
1468 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1470 // There's no fundamental reason why we require at least one index
1471 // (other than weirdness with &*IdxBegin being invalid; see
1472 // getelementptr's init routine for example). But there's no
1473 // present need to support it.
1474 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1476 // This requires that the iterator points to contiguous memory.
1477 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1478 // we have to build an array here
1481 /// getIndexedType - Returns the type of the element that would be extracted
1482 /// with an extractvalue instruction with the specified parameters.
1484 /// Null is returned if the indices are invalid for the specified
1487 static const Type *getIndexedType(const Type *Agg,
1488 const unsigned *Idx, unsigned NumIdx);
1490 template<typename InputIterator>
1491 static const Type *getIndexedType(const Type *Ptr,
1492 InputIterator IdxBegin,
1493 InputIterator IdxEnd,
1494 // This argument ensures that we
1495 // have an iterator we can do
1496 // arithmetic on in constant time
1497 std::random_access_iterator_tag) {
1498 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1501 // This requires that the iterator points to contiguous memory.
1502 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1504 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1507 /// Constructors - Create a extractvalue instruction with a base aggregate
1508 /// value and a list of indices. The first ctor can optionally insert before
1509 /// an existing instruction, the second appends the new instruction to the
1510 /// specified BasicBlock.
1511 template<typename InputIterator>
1512 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1513 InputIterator IdxEnd,
1514 const Twine &NameStr,
1515 Instruction *InsertBefore);
1516 template<typename InputIterator>
1517 inline ExtractValueInst(Value *Agg,
1518 InputIterator IdxBegin, InputIterator IdxEnd,
1519 const Twine &NameStr, BasicBlock *InsertAtEnd);
1521 // allocate space for exactly one operand
1522 void *operator new(size_t s) {
1523 return User::operator new(s, 1);
1527 template<typename InputIterator>
1528 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1529 InputIterator IdxEnd,
1530 const Twine &NameStr = "",
1531 Instruction *InsertBefore = 0) {
1533 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1535 template<typename InputIterator>
1536 static ExtractValueInst *Create(Value *Agg,
1537 InputIterator IdxBegin, InputIterator IdxEnd,
1538 const Twine &NameStr,
1539 BasicBlock *InsertAtEnd) {
1540 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1543 /// Constructors - These two creators are convenience methods because one
1544 /// index extractvalue instructions are much more common than those with
1546 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1547 const Twine &NameStr = "",
1548 Instruction *InsertBefore = 0) {
1549 unsigned Idxs[1] = { Idx };
1550 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1552 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1553 const Twine &NameStr,
1554 BasicBlock *InsertAtEnd) {
1555 unsigned Idxs[1] = { Idx };
1556 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1559 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1561 /// getIndexedType - Returns the type of the element that would be extracted
1562 /// with an extractvalue instruction with the specified parameters.
1564 /// Null is returned if the indices are invalid for the specified
1567 template<typename InputIterator>
1568 static const Type *getIndexedType(const Type *Ptr,
1569 InputIterator IdxBegin,
1570 InputIterator IdxEnd) {
1571 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1572 typename std::iterator_traits<InputIterator>::
1573 iterator_category());
1575 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1577 typedef const unsigned* idx_iterator;
1578 inline idx_iterator idx_begin() const { return Indices.begin(); }
1579 inline idx_iterator idx_end() const { return Indices.end(); }
1581 Value *getAggregateOperand() {
1582 return getOperand(0);
1584 const Value *getAggregateOperand() const {
1585 return getOperand(0);
1587 static unsigned getAggregateOperandIndex() {
1588 return 0U; // get index for modifying correct operand
1591 unsigned getNumIndices() const { // Note: always non-negative
1592 return (unsigned)Indices.size();
1595 bool hasIndices() const {
1599 // Methods for support type inquiry through isa, cast, and dyn_cast:
1600 static inline bool classof(const ExtractValueInst *) { return true; }
1601 static inline bool classof(const Instruction *I) {
1602 return I->getOpcode() == Instruction::ExtractValue;
1604 static inline bool classof(const Value *V) {
1605 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1609 template<typename InputIterator>
1610 ExtractValueInst::ExtractValueInst(Value *Agg,
1611 InputIterator IdxBegin,
1612 InputIterator IdxEnd,
1613 const Twine &NameStr,
1614 Instruction *InsertBefore)
1615 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1617 ExtractValue, Agg, InsertBefore) {
1618 init(IdxBegin, IdxEnd, NameStr,
1619 typename std::iterator_traits<InputIterator>::iterator_category());
1621 template<typename InputIterator>
1622 ExtractValueInst::ExtractValueInst(Value *Agg,
1623 InputIterator IdxBegin,
1624 InputIterator IdxEnd,
1625 const Twine &NameStr,
1626 BasicBlock *InsertAtEnd)
1627 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1629 ExtractValue, Agg, InsertAtEnd) {
1630 init(IdxBegin, IdxEnd, NameStr,
1631 typename std::iterator_traits<InputIterator>::iterator_category());
1635 //===----------------------------------------------------------------------===//
1636 // InsertValueInst Class
1637 //===----------------------------------------------------------------------===//
1639 /// InsertValueInst - This instruction inserts a struct field of array element
1640 /// value into an aggregate value.
1642 class InsertValueInst : public Instruction {
1643 SmallVector<unsigned, 4> Indices;
1645 void *operator new(size_t, unsigned); // Do not implement
1646 InsertValueInst(const InsertValueInst &IVI);
1647 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1648 const Twine &NameStr);
1649 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1651 template<typename InputIterator>
1652 void init(Value *Agg, Value *Val,
1653 InputIterator IdxBegin, InputIterator IdxEnd,
1654 const Twine &NameStr,
1655 // This argument ensures that we have an iterator we can
1656 // do arithmetic on in constant time
1657 std::random_access_iterator_tag) {
1658 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1660 // There's no fundamental reason why we require at least one index
1661 // (other than weirdness with &*IdxBegin being invalid; see
1662 // getelementptr's init routine for example). But there's no
1663 // present need to support it.
1664 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1666 // This requires that the iterator points to contiguous memory.
1667 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1668 // we have to build an array here
1671 /// Constructors - Create a insertvalue instruction with a base aggregate
1672 /// value, a value to insert, and a list of indices. The first ctor can
1673 /// optionally insert before an existing instruction, the second appends
1674 /// the new instruction to the specified BasicBlock.
1675 template<typename InputIterator>
1676 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1677 InputIterator IdxEnd,
1678 const Twine &NameStr,
1679 Instruction *InsertBefore);
1680 template<typename InputIterator>
1681 inline InsertValueInst(Value *Agg, Value *Val,
1682 InputIterator IdxBegin, InputIterator IdxEnd,
1683 const Twine &NameStr, BasicBlock *InsertAtEnd);
1685 /// Constructors - These two constructors are convenience methods because one
1686 /// and two index insertvalue instructions are so common.
1687 InsertValueInst(Value *Agg, Value *Val,
1688 unsigned Idx, const Twine &NameStr = "",
1689 Instruction *InsertBefore = 0);
1690 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1691 const Twine &NameStr, BasicBlock *InsertAtEnd);
1693 // allocate space for exactly two operands
1694 void *operator new(size_t s) {
1695 return User::operator new(s, 2);
1698 template<typename InputIterator>
1699 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1700 InputIterator IdxEnd,
1701 const Twine &NameStr = "",
1702 Instruction *InsertBefore = 0) {
1703 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1704 NameStr, InsertBefore);
1706 template<typename InputIterator>
1707 static InsertValueInst *Create(Value *Agg, Value *Val,
1708 InputIterator IdxBegin, InputIterator IdxEnd,
1709 const Twine &NameStr,
1710 BasicBlock *InsertAtEnd) {
1711 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1712 NameStr, InsertAtEnd);
1715 /// Constructors - These two creators are convenience methods because one
1716 /// index insertvalue instructions are much more common than those with
1718 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1719 const Twine &NameStr = "",
1720 Instruction *InsertBefore = 0) {
1721 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1723 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1724 const Twine &NameStr,
1725 BasicBlock *InsertAtEnd) {
1726 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1729 virtual InsertValueInst *clone(LLVMContext &Context) const;
1731 /// Transparently provide more efficient getOperand methods.
1732 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1734 typedef const unsigned* idx_iterator;
1735 inline idx_iterator idx_begin() const { return Indices.begin(); }
1736 inline idx_iterator idx_end() const { return Indices.end(); }
1738 Value *getAggregateOperand() {
1739 return getOperand(0);
1741 const Value *getAggregateOperand() const {
1742 return getOperand(0);
1744 static unsigned getAggregateOperandIndex() {
1745 return 0U; // get index for modifying correct operand
1748 Value *getInsertedValueOperand() {
1749 return getOperand(1);
1751 const Value *getInsertedValueOperand() const {
1752 return getOperand(1);
1754 static unsigned getInsertedValueOperandIndex() {
1755 return 1U; // get index for modifying correct operand
1758 unsigned getNumIndices() const { // Note: always non-negative
1759 return (unsigned)Indices.size();
1762 bool hasIndices() const {
1766 // Methods for support type inquiry through isa, cast, and dyn_cast:
1767 static inline bool classof(const InsertValueInst *) { return true; }
1768 static inline bool classof(const Instruction *I) {
1769 return I->getOpcode() == Instruction::InsertValue;
1771 static inline bool classof(const Value *V) {
1772 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1777 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1780 template<typename InputIterator>
1781 InsertValueInst::InsertValueInst(Value *Agg,
1783 InputIterator IdxBegin,
1784 InputIterator IdxEnd,
1785 const Twine &NameStr,
1786 Instruction *InsertBefore)
1787 : Instruction(Agg->getType(), InsertValue,
1788 OperandTraits<InsertValueInst>::op_begin(this),
1790 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1791 typename std::iterator_traits<InputIterator>::iterator_category());
1793 template<typename InputIterator>
1794 InsertValueInst::InsertValueInst(Value *Agg,
1796 InputIterator IdxBegin,
1797 InputIterator IdxEnd,
1798 const Twine &NameStr,
1799 BasicBlock *InsertAtEnd)
1800 : Instruction(Agg->getType(), InsertValue,
1801 OperandTraits<InsertValueInst>::op_begin(this),
1803 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1804 typename std::iterator_traits<InputIterator>::iterator_category());
1807 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1809 //===----------------------------------------------------------------------===//
1811 //===----------------------------------------------------------------------===//
1813 // PHINode - The PHINode class is used to represent the magical mystical PHI
1814 // node, that can not exist in nature, but can be synthesized in a computer
1815 // scientist's overactive imagination.
1817 class PHINode : public Instruction {
1818 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1819 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1820 /// the number actually in use.
1821 unsigned ReservedSpace;
1822 PHINode(const PHINode &PN);
1823 // allocate space for exactly zero operands
1824 void *operator new(size_t s) {
1825 return User::operator new(s, 0);
1827 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1828 Instruction *InsertBefore = 0)
1829 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1834 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1835 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1840 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1841 Instruction *InsertBefore = 0) {
1842 return new PHINode(Ty, NameStr, InsertBefore);
1844 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1845 BasicBlock *InsertAtEnd) {
1846 return new PHINode(Ty, NameStr, InsertAtEnd);
1850 /// reserveOperandSpace - This method can be used to avoid repeated
1851 /// reallocation of PHI operand lists by reserving space for the correct
1852 /// number of operands before adding them. Unlike normal vector reserves,
1853 /// this method can also be used to trim the operand space.
1854 void reserveOperandSpace(unsigned NumValues) {
1855 resizeOperands(NumValues*2);
1858 virtual PHINode *clone(LLVMContext &Context) const;
1860 /// Provide fast operand accessors
1861 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1863 /// getNumIncomingValues - Return the number of incoming edges
1865 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1867 /// getIncomingValue - Return incoming value number x
1869 Value *getIncomingValue(unsigned i) const {
1870 assert(i*2 < getNumOperands() && "Invalid value number!");
1871 return getOperand(i*2);
1873 void setIncomingValue(unsigned i, Value *V) {
1874 assert(i*2 < getNumOperands() && "Invalid value number!");
1877 static unsigned getOperandNumForIncomingValue(unsigned i) {
1880 static unsigned getIncomingValueNumForOperand(unsigned i) {
1881 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1885 /// getIncomingBlock - Return incoming basic block corresponding
1886 /// to value use iterator
1888 template <typename U>
1889 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1890 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1891 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1893 /// getIncomingBlock - Return incoming basic block number x
1895 BasicBlock *getIncomingBlock(unsigned i) const {
1896 return static_cast<BasicBlock*>(getOperand(i*2+1));
1898 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1899 setOperand(i*2+1, BB);
1901 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1904 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1905 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1909 /// addIncoming - Add an incoming value to the end of the PHI list
1911 void addIncoming(Value *V, BasicBlock *BB) {
1912 assert(V && "PHI node got a null value!");
1913 assert(BB && "PHI node got a null basic block!");
1914 assert(getType() == V->getType() &&
1915 "All operands to PHI node must be the same type as the PHI node!");
1916 unsigned OpNo = NumOperands;
1917 if (OpNo+2 > ReservedSpace)
1918 resizeOperands(0); // Get more space!
1919 // Initialize some new operands.
1920 NumOperands = OpNo+2;
1921 OperandList[OpNo] = V;
1922 OperandList[OpNo+1] = BB;
1925 /// removeIncomingValue - Remove an incoming value. This is useful if a
1926 /// predecessor basic block is deleted. The value removed is returned.
1928 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1929 /// is true), the PHI node is destroyed and any uses of it are replaced with
1930 /// dummy values. The only time there should be zero incoming values to a PHI
1931 /// node is when the block is dead, so this strategy is sound.
1933 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1935 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1936 int Idx = getBasicBlockIndex(BB);
1937 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1938 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1941 /// getBasicBlockIndex - Return the first index of the specified basic
1942 /// block in the value list for this PHI. Returns -1 if no instance.
1944 int getBasicBlockIndex(const BasicBlock *BB) const {
1945 Use *OL = OperandList;
1946 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1947 if (OL[i+1].get() == BB) return i/2;
1951 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1952 return getIncomingValue(getBasicBlockIndex(BB));
1955 /// hasConstantValue - If the specified PHI node always merges together the
1956 /// same value, return the value, otherwise return null.
1958 /// If the PHI has undef operands, but all the rest of the operands are
1959 /// some unique value, return that value if it can be proved that the
1960 /// value dominates the PHI. If DT is null, use a conservative check,
1961 /// otherwise use DT to test for dominance.
1963 Value *hasConstantValue(DominatorTree *DT = 0) const;
1965 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1966 static inline bool classof(const PHINode *) { return true; }
1967 static inline bool classof(const Instruction *I) {
1968 return I->getOpcode() == Instruction::PHI;
1970 static inline bool classof(const Value *V) {
1971 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1974 void resizeOperands(unsigned NumOperands);
1978 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1981 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1984 //===----------------------------------------------------------------------===//
1986 //===----------------------------------------------------------------------===//
1988 //===---------------------------------------------------------------------------
1989 /// ReturnInst - Return a value (possibly void), from a function. Execution
1990 /// does not continue in this function any longer.
1992 class ReturnInst : public TerminatorInst {
1993 ReturnInst(const ReturnInst &RI);
1996 // ReturnInst constructors:
1997 // ReturnInst() - 'ret void' instruction
1998 // ReturnInst( null) - 'ret void' instruction
1999 // ReturnInst(Value* X) - 'ret X' instruction
2000 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2001 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2002 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2003 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2005 // NOTE: If the Value* passed is of type void then the constructor behaves as
2006 // if it was passed NULL.
2007 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2008 Instruction *InsertBefore = 0);
2009 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2010 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2012 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2013 Instruction *InsertBefore = 0) {
2014 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2016 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2017 BasicBlock *InsertAtEnd) {
2018 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2020 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2021 return new(0) ReturnInst(C, InsertAtEnd);
2023 virtual ~ReturnInst();
2025 virtual ReturnInst *clone(LLVMContext &Context) const;
2027 /// Provide fast operand accessors
2028 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2030 /// Convenience accessor
2031 Value *getReturnValue(unsigned n = 0) const {
2032 return n < getNumOperands()
2037 unsigned getNumSuccessors() const { return 0; }
2039 // Methods for support type inquiry through isa, cast, and dyn_cast:
2040 static inline bool classof(const ReturnInst *) { return true; }
2041 static inline bool classof(const Instruction *I) {
2042 return (I->getOpcode() == Instruction::Ret);
2044 static inline bool classof(const Value *V) {
2045 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2048 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2049 virtual unsigned getNumSuccessorsV() const;
2050 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2054 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
2057 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2059 //===----------------------------------------------------------------------===//
2061 //===----------------------------------------------------------------------===//
2063 //===---------------------------------------------------------------------------
2064 /// BranchInst - Conditional or Unconditional Branch instruction.
2066 class BranchInst : public TerminatorInst {
2067 /// Ops list - Branches are strange. The operands are ordered:
2068 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2069 /// they don't have to check for cond/uncond branchness. These are mostly
2070 /// accessed relative from op_end().
2071 BranchInst(const BranchInst &BI);
2073 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2074 // BranchInst(BB *B) - 'br B'
2075 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2076 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2077 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2078 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2079 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2080 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2081 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2082 Instruction *InsertBefore = 0);
2083 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2084 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2085 BasicBlock *InsertAtEnd);
2087 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2088 return new(1, true) BranchInst(IfTrue, InsertBefore);
2090 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2091 Value *Cond, Instruction *InsertBefore = 0) {
2092 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2094 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2095 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2097 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2098 Value *Cond, BasicBlock *InsertAtEnd) {
2099 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2104 /// Transparently provide more efficient getOperand methods.
2105 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2107 virtual BranchInst *clone(LLVMContext &Context) const;
2109 bool isUnconditional() const { return getNumOperands() == 1; }
2110 bool isConditional() const { return getNumOperands() == 3; }
2112 Value *getCondition() const {
2113 assert(isConditional() && "Cannot get condition of an uncond branch!");
2117 void setCondition(Value *V) {
2118 assert(isConditional() && "Cannot set condition of unconditional branch!");
2122 // setUnconditionalDest - Change the current branch to an unconditional branch
2123 // targeting the specified block.
2124 // FIXME: Eliminate this ugly method.
2125 void setUnconditionalDest(BasicBlock *Dest) {
2127 if (isConditional()) { // Convert this to an uncond branch.
2131 OperandList = op_begin();
2135 unsigned getNumSuccessors() const { return 1+isConditional(); }
2137 BasicBlock *getSuccessor(unsigned i) const {
2138 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2139 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2142 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2143 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2144 *(&Op<-1>() - idx) = NewSucc;
2147 // Methods for support type inquiry through isa, cast, and dyn_cast:
2148 static inline bool classof(const BranchInst *) { return true; }
2149 static inline bool classof(const Instruction *I) {
2150 return (I->getOpcode() == Instruction::Br);
2152 static inline bool classof(const Value *V) {
2153 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2156 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2157 virtual unsigned getNumSuccessorsV() const;
2158 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2162 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2164 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2166 //===----------------------------------------------------------------------===//
2168 //===----------------------------------------------------------------------===//
2170 //===---------------------------------------------------------------------------
2171 /// SwitchInst - Multiway switch
2173 class SwitchInst : public TerminatorInst {
2174 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2175 unsigned ReservedSpace;
2176 // Operand[0] = Value to switch on
2177 // Operand[1] = Default basic block destination
2178 // Operand[2n ] = Value to match
2179 // Operand[2n+1] = BasicBlock to go to on match
2180 SwitchInst(const SwitchInst &RI);
2181 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2182 void resizeOperands(unsigned No);
2183 // allocate space for exactly zero operands
2184 void *operator new(size_t s) {
2185 return User::operator new(s, 0);
2187 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2188 /// switch on and a default destination. The number of additional cases can
2189 /// be specified here to make memory allocation more efficient. This
2190 /// constructor can also autoinsert before another instruction.
2191 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2192 Instruction *InsertBefore = 0);
2194 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2195 /// switch on and a default destination. The number of additional cases can
2196 /// be specified here to make memory allocation more efficient. This
2197 /// constructor also autoinserts at the end of the specified BasicBlock.
2198 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2199 BasicBlock *InsertAtEnd);
2201 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2202 unsigned NumCases, Instruction *InsertBefore = 0) {
2203 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2205 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2206 unsigned NumCases, BasicBlock *InsertAtEnd) {
2207 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2211 /// Provide fast operand accessors
2212 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2214 // Accessor Methods for Switch stmt
2215 Value *getCondition() const { return getOperand(0); }
2216 void setCondition(Value *V) { setOperand(0, V); }
2218 BasicBlock *getDefaultDest() const {
2219 return cast<BasicBlock>(getOperand(1));
2222 /// getNumCases - return the number of 'cases' in this switch instruction.
2223 /// Note that case #0 is always the default case.
2224 unsigned getNumCases() const {
2225 return getNumOperands()/2;
2228 /// getCaseValue - Return the specified case value. Note that case #0, the
2229 /// default destination, does not have a case value.
2230 ConstantInt *getCaseValue(unsigned i) {
2231 assert(i && i < getNumCases() && "Illegal case value to get!");
2232 return getSuccessorValue(i);
2235 /// getCaseValue - Return the specified case value. Note that case #0, the
2236 /// default destination, does not have a case value.
2237 const ConstantInt *getCaseValue(unsigned i) const {
2238 assert(i && i < getNumCases() && "Illegal case value to get!");
2239 return getSuccessorValue(i);
2242 /// findCaseValue - Search all of the case values for the specified constant.
2243 /// If it is explicitly handled, return the case number of it, otherwise
2244 /// return 0 to indicate that it is handled by the default handler.
2245 unsigned findCaseValue(const ConstantInt *C) const {
2246 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2247 if (getCaseValue(i) == C)
2252 /// findCaseDest - Finds the unique case value for a given successor. Returns
2253 /// null if the successor is not found, not unique, or is the default case.
2254 ConstantInt *findCaseDest(BasicBlock *BB) {
2255 if (BB == getDefaultDest()) return NULL;
2257 ConstantInt *CI = NULL;
2258 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2259 if (getSuccessor(i) == BB) {
2260 if (CI) return NULL; // Multiple cases lead to BB.
2261 else CI = getCaseValue(i);
2267 /// addCase - Add an entry to the switch instruction...
2269 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2271 /// removeCase - This method removes the specified successor from the switch
2272 /// instruction. Note that this cannot be used to remove the default
2273 /// destination (successor #0).
2275 void removeCase(unsigned idx);
2277 virtual SwitchInst *clone(LLVMContext &Context) const;
2279 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2280 BasicBlock *getSuccessor(unsigned idx) const {
2281 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2282 return cast<BasicBlock>(getOperand(idx*2+1));
2284 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2285 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2286 setOperand(idx*2+1, NewSucc);
2289 // getSuccessorValue - Return the value associated with the specified
2291 ConstantInt *getSuccessorValue(unsigned idx) const {
2292 assert(idx < getNumSuccessors() && "Successor # out of range!");
2293 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2296 // Methods for support type inquiry through isa, cast, and dyn_cast:
2297 static inline bool classof(const SwitchInst *) { return true; }
2298 static inline bool classof(const Instruction *I) {
2299 return I->getOpcode() == Instruction::Switch;
2301 static inline bool classof(const Value *V) {
2302 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2305 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2306 virtual unsigned getNumSuccessorsV() const;
2307 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2311 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2314 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2317 //===----------------------------------------------------------------------===//
2319 //===----------------------------------------------------------------------===//
2321 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2322 /// calling convention of the call.
2324 class InvokeInst : public TerminatorInst {
2325 AttrListPtr AttributeList;
2326 InvokeInst(const InvokeInst &BI);
2327 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2328 Value* const *Args, unsigned NumArgs);
2330 template<typename InputIterator>
2331 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2332 InputIterator ArgBegin, InputIterator ArgEnd,
2333 const Twine &NameStr,
2334 // This argument ensures that we have an iterator we can
2335 // do arithmetic on in constant time
2336 std::random_access_iterator_tag) {
2337 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2339 // This requires that the iterator points to contiguous memory.
2340 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2344 /// Construct an InvokeInst given a range of arguments.
2345 /// InputIterator must be a random-access iterator pointing to
2346 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2347 /// made for random-accessness but not for contiguous storage as
2348 /// that would incur runtime overhead.
2350 /// @brief Construct an InvokeInst from a range of arguments
2351 template<typename InputIterator>
2352 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2353 InputIterator ArgBegin, InputIterator ArgEnd,
2355 const Twine &NameStr, Instruction *InsertBefore);
2357 /// Construct an InvokeInst given a range of arguments.
2358 /// InputIterator must be a random-access iterator pointing to
2359 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2360 /// made for random-accessness but not for contiguous storage as
2361 /// that would incur runtime overhead.
2363 /// @brief Construct an InvokeInst from a range of arguments
2364 template<typename InputIterator>
2365 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2366 InputIterator ArgBegin, InputIterator ArgEnd,
2368 const Twine &NameStr, BasicBlock *InsertAtEnd);
2370 template<typename InputIterator>
2371 static InvokeInst *Create(Value *Func,
2372 BasicBlock *IfNormal, BasicBlock *IfException,
2373 InputIterator ArgBegin, InputIterator ArgEnd,
2374 const Twine &NameStr = "",
2375 Instruction *InsertBefore = 0) {
2376 unsigned Values(ArgEnd - ArgBegin + 3);
2377 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2378 Values, NameStr, InsertBefore);
2380 template<typename InputIterator>
2381 static InvokeInst *Create(Value *Func,
2382 BasicBlock *IfNormal, BasicBlock *IfException,
2383 InputIterator ArgBegin, InputIterator ArgEnd,
2384 const Twine &NameStr,
2385 BasicBlock *InsertAtEnd) {
2386 unsigned Values(ArgEnd - ArgBegin + 3);
2387 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2388 Values, NameStr, InsertAtEnd);
2391 virtual InvokeInst *clone(LLVMContext &Context) const;
2393 /// Provide fast operand accessors
2394 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2396 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2398 CallingConv::ID getCallingConv() const {
2399 return static_cast<CallingConv::ID>(SubclassData);
2401 void setCallingConv(CallingConv::ID CC) {
2402 SubclassData = static_cast<unsigned>(CC);
2405 /// getAttributes - Return the parameter attributes for this invoke.
2407 const AttrListPtr &getAttributes() const { return AttributeList; }
2409 /// setAttributes - Set the parameter attributes for this invoke.
2411 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2413 /// addAttribute - adds the attribute to the list of attributes.
2414 void addAttribute(unsigned i, Attributes attr);
2416 /// removeAttribute - removes the attribute from the list of attributes.
2417 void removeAttribute(unsigned i, Attributes attr);
2419 /// @brief Determine whether the call or the callee has the given attribute.
2420 bool paramHasAttr(unsigned i, Attributes attr) const;
2422 /// @brief Extract the alignment for a call or parameter (0=unknown).
2423 unsigned getParamAlignment(unsigned i) const {
2424 return AttributeList.getParamAlignment(i);
2427 /// @brief Determine if the call does not access memory.
2428 bool doesNotAccessMemory() const {
2429 return paramHasAttr(~0, Attribute::ReadNone);
2431 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2432 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2433 else removeAttribute(~0, Attribute::ReadNone);
2436 /// @brief Determine if the call does not access or only reads memory.
2437 bool onlyReadsMemory() const {
2438 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2440 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2441 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2442 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2445 /// @brief Determine if the call cannot return.
2446 bool doesNotReturn() const {
2447 return paramHasAttr(~0, Attribute::NoReturn);
2449 void setDoesNotReturn(bool DoesNotReturn = true) {
2450 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2451 else removeAttribute(~0, Attribute::NoReturn);
2454 /// @brief Determine if the call cannot unwind.
2455 bool doesNotThrow() const {
2456 return paramHasAttr(~0, Attribute::NoUnwind);
2458 void setDoesNotThrow(bool DoesNotThrow = true) {
2459 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2460 else removeAttribute(~0, Attribute::NoUnwind);
2463 /// @brief Determine if the call returns a structure through first
2464 /// pointer argument.
2465 bool hasStructRetAttr() const {
2466 // Be friendly and also check the callee.
2467 return paramHasAttr(1, Attribute::StructRet);
2470 /// @brief Determine if any call argument is an aggregate passed by value.
2471 bool hasByValArgument() const {
2472 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2475 /// getCalledFunction - Return the function called, or null if this is an
2476 /// indirect function invocation.
2478 Function *getCalledFunction() const {
2479 return dyn_cast<Function>(getOperand(0));
2482 /// getCalledValue - Get a pointer to the function that is invoked by this
2484 const Value *getCalledValue() const { return getOperand(0); }
2485 Value *getCalledValue() { return getOperand(0); }
2487 // get*Dest - Return the destination basic blocks...
2488 BasicBlock *getNormalDest() const {
2489 return cast<BasicBlock>(getOperand(1));
2491 BasicBlock *getUnwindDest() const {
2492 return cast<BasicBlock>(getOperand(2));
2494 void setNormalDest(BasicBlock *B) {
2498 void setUnwindDest(BasicBlock *B) {
2502 BasicBlock *getSuccessor(unsigned i) const {
2503 assert(i < 2 && "Successor # out of range for invoke!");
2504 return i == 0 ? getNormalDest() : getUnwindDest();
2507 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2508 assert(idx < 2 && "Successor # out of range for invoke!");
2509 setOperand(idx+1, NewSucc);
2512 unsigned getNumSuccessors() const { return 2; }
2514 // Methods for support type inquiry through isa, cast, and dyn_cast:
2515 static inline bool classof(const InvokeInst *) { return true; }
2516 static inline bool classof(const Instruction *I) {
2517 return (I->getOpcode() == Instruction::Invoke);
2519 static inline bool classof(const Value *V) {
2520 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2523 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2524 virtual unsigned getNumSuccessorsV() const;
2525 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2529 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2532 template<typename InputIterator>
2533 InvokeInst::InvokeInst(Value *Func,
2534 BasicBlock *IfNormal, BasicBlock *IfException,
2535 InputIterator ArgBegin, InputIterator ArgEnd,
2537 const Twine &NameStr, Instruction *InsertBefore)
2538 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2539 ->getElementType())->getReturnType(),
2540 Instruction::Invoke,
2541 OperandTraits<InvokeInst>::op_end(this) - Values,
2542 Values, InsertBefore) {
2543 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2544 typename std::iterator_traits<InputIterator>::iterator_category());
2546 template<typename InputIterator>
2547 InvokeInst::InvokeInst(Value *Func,
2548 BasicBlock *IfNormal, BasicBlock *IfException,
2549 InputIterator ArgBegin, InputIterator ArgEnd,
2551 const Twine &NameStr, BasicBlock *InsertAtEnd)
2552 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2553 ->getElementType())->getReturnType(),
2554 Instruction::Invoke,
2555 OperandTraits<InvokeInst>::op_end(this) - Values,
2556 Values, InsertAtEnd) {
2557 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2558 typename std::iterator_traits<InputIterator>::iterator_category());
2561 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2563 //===----------------------------------------------------------------------===//
2565 //===----------------------------------------------------------------------===//
2567 //===---------------------------------------------------------------------------
2568 /// UnwindInst - Immediately exit the current function, unwinding the stack
2569 /// until an invoke instruction is found.
2571 class UnwindInst : public TerminatorInst {
2572 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2574 // allocate space for exactly zero operands
2575 void *operator new(size_t s) {
2576 return User::operator new(s, 0);
2578 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2579 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2581 virtual UnwindInst *clone(LLVMContext &Context) const;
2583 unsigned getNumSuccessors() const { return 0; }
2585 // Methods for support type inquiry through isa, cast, and dyn_cast:
2586 static inline bool classof(const UnwindInst *) { return true; }
2587 static inline bool classof(const Instruction *I) {
2588 return I->getOpcode() == Instruction::Unwind;
2590 static inline bool classof(const Value *V) {
2591 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2594 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2595 virtual unsigned getNumSuccessorsV() const;
2596 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2599 //===----------------------------------------------------------------------===//
2600 // UnreachableInst Class
2601 //===----------------------------------------------------------------------===//
2603 //===---------------------------------------------------------------------------
2604 /// UnreachableInst - This function has undefined behavior. In particular, the
2605 /// presence of this instruction indicates some higher level knowledge that the
2606 /// end of the block cannot be reached.
2608 class UnreachableInst : public TerminatorInst {
2609 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2611 // allocate space for exactly zero operands
2612 void *operator new(size_t s) {
2613 return User::operator new(s, 0);
2615 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2616 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2618 virtual UnreachableInst *clone(LLVMContext &Context) const;
2620 unsigned getNumSuccessors() const { return 0; }
2622 // Methods for support type inquiry through isa, cast, and dyn_cast:
2623 static inline bool classof(const UnreachableInst *) { return true; }
2624 static inline bool classof(const Instruction *I) {
2625 return I->getOpcode() == Instruction::Unreachable;
2627 static inline bool classof(const Value *V) {
2628 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2631 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2632 virtual unsigned getNumSuccessorsV() const;
2633 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2636 //===----------------------------------------------------------------------===//
2638 //===----------------------------------------------------------------------===//
2640 /// @brief This class represents a truncation of integer types.
2641 class TruncInst : public CastInst {
2643 /// @brief Constructor with insert-before-instruction semantics
2645 Value *S, ///< The value to be truncated
2646 const Type *Ty, ///< The (smaller) type to truncate to
2647 const Twine &NameStr = "", ///< A name for the new instruction
2648 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2651 /// @brief Constructor with insert-at-end-of-block semantics
2653 Value *S, ///< The value to be truncated
2654 const Type *Ty, ///< The (smaller) type to truncate to
2655 const Twine &NameStr, ///< A name for the new instruction
2656 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2659 /// @brief Clone an identical TruncInst
2660 virtual TruncInst *clone(LLVMContext &Context) const;
2662 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2663 static inline bool classof(const TruncInst *) { return true; }
2664 static inline bool classof(const Instruction *I) {
2665 return I->getOpcode() == Trunc;
2667 static inline bool classof(const Value *V) {
2668 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2672 //===----------------------------------------------------------------------===//
2674 //===----------------------------------------------------------------------===//
2676 /// @brief This class represents zero extension of integer types.
2677 class ZExtInst : public CastInst {
2679 /// @brief Constructor with insert-before-instruction semantics
2681 Value *S, ///< The value to be zero extended
2682 const Type *Ty, ///< The type to zero extend to
2683 const Twine &NameStr = "", ///< A name for the new instruction
2684 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2687 /// @brief Constructor with insert-at-end semantics.
2689 Value *S, ///< The value to be zero extended
2690 const Type *Ty, ///< The type to zero extend to
2691 const Twine &NameStr, ///< A name for the new instruction
2692 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2695 /// @brief Clone an identical ZExtInst
2696 virtual ZExtInst *clone(LLVMContext &Context) const;
2698 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2699 static inline bool classof(const ZExtInst *) { return true; }
2700 static inline bool classof(const Instruction *I) {
2701 return I->getOpcode() == ZExt;
2703 static inline bool classof(const Value *V) {
2704 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2708 //===----------------------------------------------------------------------===//
2710 //===----------------------------------------------------------------------===//
2712 /// @brief This class represents a sign extension of integer types.
2713 class SExtInst : public CastInst {
2715 /// @brief Constructor with insert-before-instruction semantics
2717 Value *S, ///< The value to be sign extended
2718 const Type *Ty, ///< The type to sign extend to
2719 const Twine &NameStr = "", ///< A name for the new instruction
2720 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2723 /// @brief Constructor with insert-at-end-of-block semantics
2725 Value *S, ///< The value to be sign extended
2726 const Type *Ty, ///< The type to sign extend to
2727 const Twine &NameStr, ///< A name for the new instruction
2728 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2731 /// @brief Clone an identical SExtInst
2732 virtual SExtInst *clone(LLVMContext &Context) const;
2734 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2735 static inline bool classof(const SExtInst *) { return true; }
2736 static inline bool classof(const Instruction *I) {
2737 return I->getOpcode() == SExt;
2739 static inline bool classof(const Value *V) {
2740 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2744 //===----------------------------------------------------------------------===//
2745 // FPTruncInst Class
2746 //===----------------------------------------------------------------------===//
2748 /// @brief This class represents a truncation of floating point types.
2749 class FPTruncInst : public CastInst {
2751 /// @brief Constructor with insert-before-instruction semantics
2753 Value *S, ///< The value to be truncated
2754 const Type *Ty, ///< The type to truncate to
2755 const Twine &NameStr = "", ///< A name for the new instruction
2756 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2759 /// @brief Constructor with insert-before-instruction semantics
2761 Value *S, ///< The value to be truncated
2762 const Type *Ty, ///< The type to truncate to
2763 const Twine &NameStr, ///< A name for the new instruction
2764 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2767 /// @brief Clone an identical FPTruncInst
2768 virtual FPTruncInst *clone(LLVMContext &Context) const;
2770 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2771 static inline bool classof(const FPTruncInst *) { return true; }
2772 static inline bool classof(const Instruction *I) {
2773 return I->getOpcode() == FPTrunc;
2775 static inline bool classof(const Value *V) {
2776 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2780 //===----------------------------------------------------------------------===//
2782 //===----------------------------------------------------------------------===//
2784 /// @brief This class represents an extension of floating point types.
2785 class FPExtInst : public CastInst {
2787 /// @brief Constructor with insert-before-instruction semantics
2789 Value *S, ///< The value to be extended
2790 const Type *Ty, ///< The type to extend to
2791 const Twine &NameStr = "", ///< A name for the new instruction
2792 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2795 /// @brief Constructor with insert-at-end-of-block semantics
2797 Value *S, ///< The value to be extended
2798 const Type *Ty, ///< The type to extend to
2799 const Twine &NameStr, ///< A name for the new instruction
2800 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2803 /// @brief Clone an identical FPExtInst
2804 virtual FPExtInst *clone(LLVMContext &Context) const;
2806 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2807 static inline bool classof(const FPExtInst *) { return true; }
2808 static inline bool classof(const Instruction *I) {
2809 return I->getOpcode() == FPExt;
2811 static inline bool classof(const Value *V) {
2812 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2816 //===----------------------------------------------------------------------===//
2818 //===----------------------------------------------------------------------===//
2820 /// @brief This class represents a cast unsigned integer to floating point.
2821 class UIToFPInst : public CastInst {
2823 /// @brief Constructor with insert-before-instruction semantics
2825 Value *S, ///< The value to be converted
2826 const Type *Ty, ///< The type to convert to
2827 const Twine &NameStr = "", ///< A name for the new instruction
2828 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2831 /// @brief Constructor with insert-at-end-of-block semantics
2833 Value *S, ///< The value to be converted
2834 const Type *Ty, ///< The type to convert to
2835 const Twine &NameStr, ///< A name for the new instruction
2836 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2839 /// @brief Clone an identical UIToFPInst
2840 virtual UIToFPInst *clone(LLVMContext &Context) const;
2842 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2843 static inline bool classof(const UIToFPInst *) { return true; }
2844 static inline bool classof(const Instruction *I) {
2845 return I->getOpcode() == UIToFP;
2847 static inline bool classof(const Value *V) {
2848 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2852 //===----------------------------------------------------------------------===//
2854 //===----------------------------------------------------------------------===//
2856 /// @brief This class represents a cast from signed integer to floating point.
2857 class SIToFPInst : public CastInst {
2859 /// @brief Constructor with insert-before-instruction semantics
2861 Value *S, ///< The value to be converted
2862 const Type *Ty, ///< The type to convert to
2863 const Twine &NameStr = "", ///< A name for the new instruction
2864 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2867 /// @brief Constructor with insert-at-end-of-block semantics
2869 Value *S, ///< The value to be converted
2870 const Type *Ty, ///< The type to convert to
2871 const Twine &NameStr, ///< A name for the new instruction
2872 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2875 /// @brief Clone an identical SIToFPInst
2876 virtual SIToFPInst *clone(LLVMContext &Context) const;
2878 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2879 static inline bool classof(const SIToFPInst *) { return true; }
2880 static inline bool classof(const Instruction *I) {
2881 return I->getOpcode() == SIToFP;
2883 static inline bool classof(const Value *V) {
2884 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2888 //===----------------------------------------------------------------------===//
2890 //===----------------------------------------------------------------------===//
2892 /// @brief This class represents a cast from floating point to unsigned integer
2893 class FPToUIInst : public CastInst {
2895 /// @brief Constructor with insert-before-instruction semantics
2897 Value *S, ///< The value to be converted
2898 const Type *Ty, ///< The type to convert to
2899 const Twine &NameStr = "", ///< A name for the new instruction
2900 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2903 /// @brief Constructor with insert-at-end-of-block semantics
2905 Value *S, ///< The value to be converted
2906 const Type *Ty, ///< The type to convert to
2907 const Twine &NameStr, ///< A name for the new instruction
2908 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2911 /// @brief Clone an identical FPToUIInst
2912 virtual FPToUIInst *clone(LLVMContext &Context) const;
2914 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2915 static inline bool classof(const FPToUIInst *) { return true; }
2916 static inline bool classof(const Instruction *I) {
2917 return I->getOpcode() == FPToUI;
2919 static inline bool classof(const Value *V) {
2920 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2924 //===----------------------------------------------------------------------===//
2926 //===----------------------------------------------------------------------===//
2928 /// @brief This class represents a cast from floating point to signed integer.
2929 class FPToSIInst : public CastInst {
2931 /// @brief Constructor with insert-before-instruction semantics
2933 Value *S, ///< The value to be converted
2934 const Type *Ty, ///< The type to convert to
2935 const Twine &NameStr = "", ///< A name for the new instruction
2936 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2939 /// @brief Constructor with insert-at-end-of-block semantics
2941 Value *S, ///< The value to be converted
2942 const Type *Ty, ///< The type to convert to
2943 const Twine &NameStr, ///< A name for the new instruction
2944 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2947 /// @brief Clone an identical FPToSIInst
2948 virtual FPToSIInst *clone(LLVMContext &Context) const;
2950 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2951 static inline bool classof(const FPToSIInst *) { return true; }
2952 static inline bool classof(const Instruction *I) {
2953 return I->getOpcode() == FPToSI;
2955 static inline bool classof(const Value *V) {
2956 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2960 //===----------------------------------------------------------------------===//
2961 // IntToPtrInst Class
2962 //===----------------------------------------------------------------------===//
2964 /// @brief This class represents a cast from an integer to a pointer.
2965 class IntToPtrInst : public CastInst {
2967 /// @brief Constructor with insert-before-instruction semantics
2969 Value *S, ///< The value to be converted
2970 const Type *Ty, ///< The type to convert to
2971 const Twine &NameStr = "", ///< A name for the new instruction
2972 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2975 /// @brief Constructor with insert-at-end-of-block semantics
2977 Value *S, ///< The value to be converted
2978 const Type *Ty, ///< The type to convert to
2979 const Twine &NameStr, ///< A name for the new instruction
2980 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2983 /// @brief Clone an identical IntToPtrInst
2984 virtual IntToPtrInst *clone(LLVMContext &Context) const;
2986 // Methods for support type inquiry through isa, cast, and dyn_cast:
2987 static inline bool classof(const IntToPtrInst *) { return true; }
2988 static inline bool classof(const Instruction *I) {
2989 return I->getOpcode() == IntToPtr;
2991 static inline bool classof(const Value *V) {
2992 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2996 //===----------------------------------------------------------------------===//
2997 // PtrToIntInst Class
2998 //===----------------------------------------------------------------------===//
3000 /// @brief This class represents a cast from a pointer to an integer
3001 class PtrToIntInst : public CastInst {
3003 /// @brief Constructor with insert-before-instruction semantics
3005 Value *S, ///< The value to be converted
3006 const Type *Ty, ///< The type to convert to
3007 const Twine &NameStr = "", ///< A name for the new instruction
3008 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3011 /// @brief Constructor with insert-at-end-of-block semantics
3013 Value *S, ///< The value to be converted
3014 const Type *Ty, ///< The type to convert to
3015 const Twine &NameStr, ///< A name for the new instruction
3016 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3019 /// @brief Clone an identical PtrToIntInst
3020 virtual PtrToIntInst *clone(LLVMContext &Context) const;
3022 // Methods for support type inquiry through isa, cast, and dyn_cast:
3023 static inline bool classof(const PtrToIntInst *) { return true; }
3024 static inline bool classof(const Instruction *I) {
3025 return I->getOpcode() == PtrToInt;
3027 static inline bool classof(const Value *V) {
3028 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3032 //===----------------------------------------------------------------------===//
3033 // BitCastInst Class
3034 //===----------------------------------------------------------------------===//
3036 /// @brief This class represents a no-op cast from one type to another.
3037 class BitCastInst : public CastInst {
3039 /// @brief Constructor with insert-before-instruction semantics
3041 Value *S, ///< The value to be casted
3042 const Type *Ty, ///< The type to casted to
3043 const Twine &NameStr = "", ///< A name for the new instruction
3044 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3047 /// @brief Constructor with insert-at-end-of-block semantics
3049 Value *S, ///< The value to be casted
3050 const Type *Ty, ///< The type to casted to
3051 const Twine &NameStr, ///< A name for the new instruction
3052 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3055 /// @brief Clone an identical BitCastInst
3056 virtual BitCastInst *clone(LLVMContext &Context) const;
3058 // Methods for support type inquiry through isa, cast, and dyn_cast:
3059 static inline bool classof(const BitCastInst *) { return true; }
3060 static inline bool classof(const Instruction *I) {
3061 return I->getOpcode() == BitCast;
3063 static inline bool classof(const Value *V) {
3064 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3068 } // End llvm namespace