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() 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() 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() 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() 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() 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() 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() 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 b = true);
609 /// isInBounds - Determine whether the GEP has the inbounds flag.
610 bool isInBounds() const;
612 // Methods for support type inquiry through isa, cast, and dyn_cast:
613 static inline bool classof(const GetElementPtrInst *) { return true; }
614 static inline bool classof(const Instruction *I) {
615 return (I->getOpcode() == Instruction::GetElementPtr);
617 static inline bool classof(const Value *V) {
618 return isa<Instruction>(V) && classof(cast<Instruction>(V));
623 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
626 template<typename InputIterator>
627 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
628 InputIterator IdxBegin,
629 InputIterator IdxEnd,
631 const Twine &NameStr,
632 Instruction *InsertBefore)
633 : Instruction(PointerType::get(checkType(
634 getIndexedType(Ptr->getType(),
636 cast<PointerType>(Ptr->getType())
637 ->getAddressSpace()),
639 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
640 Values, InsertBefore) {
641 init(Ptr, IdxBegin, IdxEnd, NameStr,
642 typename std::iterator_traits<InputIterator>::iterator_category());
644 template<typename InputIterator>
645 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
646 InputIterator IdxBegin,
647 InputIterator IdxEnd,
649 const Twine &NameStr,
650 BasicBlock *InsertAtEnd)
651 : Instruction(PointerType::get(checkType(
652 getIndexedType(Ptr->getType(),
654 cast<PointerType>(Ptr->getType())
655 ->getAddressSpace()),
657 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
658 Values, InsertAtEnd) {
659 init(Ptr, IdxBegin, IdxEnd, NameStr,
660 typename std::iterator_traits<InputIterator>::iterator_category());
664 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
667 //===----------------------------------------------------------------------===//
669 //===----------------------------------------------------------------------===//
671 /// This instruction compares its operands according to the predicate given
672 /// to the constructor. It only operates on integers or pointers. The operands
673 /// must be identical types.
674 /// @brief Represent an integer comparison operator.
675 class ICmpInst: public CmpInst {
677 /// @brief Constructor with insert-before-instruction semantics.
679 Instruction *InsertBefore, ///< Where to insert
680 Predicate pred, ///< The predicate to use for the comparison
681 Value *LHS, ///< The left-hand-side of the expression
682 Value *RHS, ///< The right-hand-side of the expression
683 const Twine &NameStr = "" ///< Name of the instruction
684 ) : CmpInst(makeCmpResultType(LHS->getType()),
685 Instruction::ICmp, pred, LHS, RHS, NameStr,
687 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
688 pred <= CmpInst::LAST_ICMP_PREDICATE &&
689 "Invalid ICmp predicate value");
690 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
691 "Both operands to ICmp instruction are not of the same type!");
692 // Check that the operands are the right type
693 assert((getOperand(0)->getType()->isIntOrIntVector() ||
694 isa<PointerType>(getOperand(0)->getType())) &&
695 "Invalid operand types for ICmp instruction");
698 /// @brief Constructor with insert-at-end semantics.
700 BasicBlock &InsertAtEnd, ///< Block to insert into.
701 Predicate pred, ///< The predicate to use for the comparison
702 Value *LHS, ///< The left-hand-side of the expression
703 Value *RHS, ///< The right-hand-side of the expression
704 const Twine &NameStr = "" ///< Name of the instruction
705 ) : CmpInst(makeCmpResultType(LHS->getType()),
706 Instruction::ICmp, pred, LHS, RHS, NameStr,
708 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
709 pred <= CmpInst::LAST_ICMP_PREDICATE &&
710 "Invalid ICmp predicate value");
711 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
712 "Both operands to ICmp instruction are not of the same type!");
713 // Check that the operands are the right type
714 assert((getOperand(0)->getType()->isIntOrIntVector() ||
715 isa<PointerType>(getOperand(0)->getType())) &&
716 "Invalid operand types for ICmp instruction");
719 /// @brief Constructor with no-insertion semantics
721 Predicate pred, ///< The predicate to use for the comparison
722 Value *LHS, ///< The left-hand-side of the expression
723 Value *RHS, ///< The right-hand-side of the expression
724 const Twine &NameStr = "" ///< Name of the instruction
725 ) : CmpInst(makeCmpResultType(LHS->getType()),
726 Instruction::ICmp, pred, LHS, RHS, NameStr) {
727 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
728 pred <= CmpInst::LAST_ICMP_PREDICATE &&
729 "Invalid ICmp predicate value");
730 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
731 "Both operands to ICmp instruction are not of the same type!");
732 // Check that the operands are the right type
733 assert((getOperand(0)->getType()->isIntOrIntVector() ||
734 isa<PointerType>(getOperand(0)->getType())) &&
735 "Invalid operand types for ICmp instruction");
738 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
739 /// @returns the predicate that would be the result if the operand were
740 /// regarded as signed.
741 /// @brief Return the signed version of the predicate
742 Predicate getSignedPredicate() const {
743 return getSignedPredicate(getPredicate());
746 /// This is a static version that you can use without an instruction.
747 /// @brief Return the signed version of the predicate.
748 static Predicate getSignedPredicate(Predicate pred);
750 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
751 /// @returns the predicate that would be the result if the operand were
752 /// regarded as unsigned.
753 /// @brief Return the unsigned version of the predicate
754 Predicate getUnsignedPredicate() const {
755 return getUnsignedPredicate(getPredicate());
758 /// This is a static version that you can use without an instruction.
759 /// @brief Return the unsigned version of the predicate.
760 static Predicate getUnsignedPredicate(Predicate pred);
762 /// isEquality - Return true if this predicate is either EQ or NE. This also
763 /// tests for commutativity.
764 static bool isEquality(Predicate P) {
765 return P == ICMP_EQ || P == ICMP_NE;
768 /// isEquality - Return true if this predicate is either EQ or NE. This also
769 /// tests for commutativity.
770 bool isEquality() const {
771 return isEquality(getPredicate());
774 /// @returns true if the predicate of this ICmpInst is commutative
775 /// @brief Determine if this relation is commutative.
776 bool isCommutative() const { return isEquality(); }
778 /// isRelational - Return true if the predicate is relational (not EQ or NE).
780 bool isRelational() const {
781 return !isEquality();
784 /// isRelational - Return true if the predicate is relational (not EQ or NE).
786 static bool isRelational(Predicate P) {
787 return !isEquality(P);
790 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
791 /// @brief Determine if this instruction's predicate is signed.
792 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
794 /// @returns true if the predicate provided is signed, false otherwise
795 /// @brief Determine if the predicate is signed.
796 static bool isSignedPredicate(Predicate pred);
798 /// @returns true if the specified compare predicate is
799 /// true when both operands are equal...
800 /// @brief Determine if the icmp is true when both operands are equal
801 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
802 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
803 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
804 pred == ICmpInst::ICMP_SLE;
807 /// @returns true if the specified compare instruction is
808 /// true when both operands are equal...
809 /// @brief Determine if the ICmpInst returns true when both operands are equal
810 bool isTrueWhenEqual() {
811 return isTrueWhenEqual(getPredicate());
814 /// Initialize a set of values that all satisfy the predicate with C.
815 /// @brief Make a ConstantRange for a relation with a constant value.
816 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
818 /// Exchange the two operands to this instruction in such a way that it does
819 /// not modify the semantics of the instruction. The predicate value may be
820 /// changed to retain the same result if the predicate is order dependent
822 /// @brief Swap operands and adjust predicate.
823 void swapOperands() {
824 SubclassData = getSwappedPredicate();
825 Op<0>().swap(Op<1>());
828 virtual ICmpInst *clone() const;
830 // Methods for support type inquiry through isa, cast, and dyn_cast:
831 static inline bool classof(const ICmpInst *) { return true; }
832 static inline bool classof(const Instruction *I) {
833 return I->getOpcode() == Instruction::ICmp;
835 static inline bool classof(const Value *V) {
836 return isa<Instruction>(V) && classof(cast<Instruction>(V));
841 //===----------------------------------------------------------------------===//
843 //===----------------------------------------------------------------------===//
845 /// This instruction compares its operands according to the predicate given
846 /// to the constructor. It only operates on floating point values or packed
847 /// vectors of floating point values. The operands must be identical types.
848 /// @brief Represents a floating point comparison operator.
849 class FCmpInst: public CmpInst {
851 /// @brief Constructor with insert-before-instruction semantics.
853 Instruction *InsertBefore, ///< Where to insert
854 Predicate pred, ///< The predicate to use for the comparison
855 Value *LHS, ///< The left-hand-side of the expression
856 Value *RHS, ///< The right-hand-side of the expression
857 const Twine &NameStr = "" ///< Name of the instruction
858 ) : CmpInst(makeCmpResultType(LHS->getType()),
859 Instruction::FCmp, pred, LHS, RHS, NameStr,
861 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
862 "Invalid FCmp predicate value");
863 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
864 "Both operands to FCmp instruction are not of the same type!");
865 // Check that the operands are the right type
866 assert(getOperand(0)->getType()->isFPOrFPVector() &&
867 "Invalid operand types for FCmp instruction");
870 /// @brief Constructor with insert-at-end semantics.
872 BasicBlock &InsertAtEnd, ///< Block to insert into.
873 Predicate pred, ///< The predicate to use for the comparison
874 Value *LHS, ///< The left-hand-side of the expression
875 Value *RHS, ///< The right-hand-side of the expression
876 const Twine &NameStr = "" ///< Name of the instruction
877 ) : CmpInst(makeCmpResultType(LHS->getType()),
878 Instruction::FCmp, pred, LHS, RHS, NameStr,
880 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
881 "Invalid FCmp predicate value");
882 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
883 "Both operands to FCmp instruction are not of the same type!");
884 // Check that the operands are the right type
885 assert(getOperand(0)->getType()->isFPOrFPVector() &&
886 "Invalid operand types for FCmp instruction");
889 /// @brief Constructor with no-insertion semantics
891 Predicate pred, ///< The predicate to use for the comparison
892 Value *LHS, ///< The left-hand-side of the expression
893 Value *RHS, ///< The right-hand-side of the expression
894 const Twine &NameStr = "" ///< Name of the instruction
895 ) : CmpInst(makeCmpResultType(LHS->getType()),
896 Instruction::FCmp, pred, LHS, RHS, NameStr) {
897 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
898 "Invalid FCmp predicate value");
899 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
900 "Both operands to FCmp instruction are not of the same type!");
901 // Check that the operands are the right type
902 assert(getOperand(0)->getType()->isFPOrFPVector() &&
903 "Invalid operand types for FCmp instruction");
906 /// @returns true if the predicate of this instruction is EQ or NE.
907 /// @brief Determine if this is an equality predicate.
908 bool isEquality() const {
909 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
910 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
913 /// @returns true if the predicate of this instruction is commutative.
914 /// @brief Determine if this is a commutative predicate.
915 bool isCommutative() const {
916 return isEquality() ||
917 SubclassData == FCMP_FALSE ||
918 SubclassData == FCMP_TRUE ||
919 SubclassData == FCMP_ORD ||
920 SubclassData == FCMP_UNO;
923 /// @returns true if the predicate is relational (not EQ or NE).
924 /// @brief Determine if this a relational predicate.
925 bool isRelational() const { return !isEquality(); }
927 /// Exchange the two operands to this instruction in such a way that it does
928 /// not modify the semantics of the instruction. The predicate value may be
929 /// changed to retain the same result if the predicate is order dependent
931 /// @brief Swap operands and adjust predicate.
932 void swapOperands() {
933 SubclassData = getSwappedPredicate();
934 Op<0>().swap(Op<1>());
937 virtual FCmpInst *clone() const;
939 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
940 static inline bool classof(const FCmpInst *) { return true; }
941 static inline bool classof(const Instruction *I) {
942 return I->getOpcode() == Instruction::FCmp;
944 static inline bool classof(const Value *V) {
945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
949 //===----------------------------------------------------------------------===//
951 //===----------------------------------------------------------------------===//
952 /// CallInst - This class represents a function call, abstracting a target
953 /// machine's calling convention. This class uses low bit of the SubClassData
954 /// field to indicate whether or not this is a tail call. The rest of the bits
955 /// hold the calling convention of the call.
958 class CallInst : public Instruction {
959 AttrListPtr AttributeList; ///< parameter attributes for call
960 CallInst(const CallInst &CI);
961 void init(Value *Func, Value* const *Params, unsigned NumParams);
962 void init(Value *Func, Value *Actual1, Value *Actual2);
963 void init(Value *Func, Value *Actual);
964 void init(Value *Func);
966 template<typename InputIterator>
967 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
968 const Twine &NameStr,
969 // This argument ensures that we have an iterator we can
970 // do arithmetic on in constant time
971 std::random_access_iterator_tag) {
972 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
974 // This requires that the iterator points to contiguous memory.
975 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
979 /// Construct a CallInst given a range of arguments. InputIterator
980 /// must be a random-access iterator pointing to contiguous storage
981 /// (e.g. a std::vector<>::iterator). Checks are made for
982 /// random-accessness but not for contiguous storage as that would
983 /// incur runtime overhead.
984 /// @brief Construct a CallInst from a range of arguments
985 template<typename InputIterator>
986 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
987 const Twine &NameStr, Instruction *InsertBefore);
989 /// Construct a CallInst given a range of arguments. InputIterator
990 /// must be a random-access iterator pointing to contiguous storage
991 /// (e.g. a std::vector<>::iterator). Checks are made for
992 /// random-accessness but not for contiguous storage as that would
993 /// incur runtime overhead.
994 /// @brief Construct a CallInst from a range of arguments
995 template<typename InputIterator>
996 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
997 const Twine &NameStr, BasicBlock *InsertAtEnd);
999 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1000 Instruction *InsertBefore);
1001 CallInst(Value *F, Value *Actual, const Twine &NameStr,
1002 BasicBlock *InsertAtEnd);
1003 explicit CallInst(Value *F, const Twine &NameStr,
1004 Instruction *InsertBefore);
1005 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
1007 template<typename InputIterator>
1008 static CallInst *Create(Value *Func,
1009 InputIterator ArgBegin, InputIterator ArgEnd,
1010 const Twine &NameStr = "",
1011 Instruction *InsertBefore = 0) {
1012 return new((unsigned)(ArgEnd - ArgBegin + 1))
1013 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1015 template<typename InputIterator>
1016 static CallInst *Create(Value *Func,
1017 InputIterator ArgBegin, InputIterator ArgEnd,
1018 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1019 return new((unsigned)(ArgEnd - ArgBegin + 1))
1020 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1022 static CallInst *Create(Value *F, Value *Actual,
1023 const Twine &NameStr = "",
1024 Instruction *InsertBefore = 0) {
1025 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1027 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
1028 BasicBlock *InsertAtEnd) {
1029 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1031 static CallInst *Create(Value *F, const Twine &NameStr = "",
1032 Instruction *InsertBefore = 0) {
1033 return new(1) CallInst(F, NameStr, InsertBefore);
1035 static CallInst *Create(Value *F, const Twine &NameStr,
1036 BasicBlock *InsertAtEnd) {
1037 return new(1) CallInst(F, NameStr, InsertAtEnd);
1039 /// CreateMalloc - Generate the IR for a call to malloc:
1040 /// 1. Compute the malloc call's argument as the specified type's size,
1041 /// possibly multiplied by the array size if the array size is not
1043 /// 2. Call malloc with that argument.
1044 /// 3. Bitcast the result of the malloc call to the specified type.
1045 static Value *CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
1046 const Type *AllocTy, Value *ArraySize = 0,
1047 const Twine &Name = "");
1048 static Value *CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
1049 const Type *AllocTy, Value *ArraySize = 0,
1050 const Twine &Name = "");
1054 bool isTailCall() const { return SubclassData & 1; }
1055 void setTailCall(bool isTC = true) {
1056 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1059 virtual CallInst *clone() const;
1061 /// Provide fast operand accessors
1062 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1064 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1066 CallingConv::ID getCallingConv() const {
1067 return static_cast<CallingConv::ID>(SubclassData >> 1);
1069 void setCallingConv(CallingConv::ID CC) {
1070 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
1073 /// getAttributes - Return the parameter attributes for this call.
1075 const AttrListPtr &getAttributes() const { return AttributeList; }
1077 /// setAttributes - Set the parameter attributes for this call.
1079 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1081 /// addAttribute - adds the attribute to the list of attributes.
1082 void addAttribute(unsigned i, Attributes attr);
1084 /// removeAttribute - removes the attribute from the list of attributes.
1085 void removeAttribute(unsigned i, Attributes attr);
1087 /// @brief Determine whether the call or the callee has the given attribute.
1088 bool paramHasAttr(unsigned i, Attributes attr) const;
1090 /// @brief Extract the alignment for a call or parameter (0=unknown).
1091 unsigned getParamAlignment(unsigned i) const {
1092 return AttributeList.getParamAlignment(i);
1095 /// @brief Determine if the call does not access memory.
1096 bool doesNotAccessMemory() const {
1097 return paramHasAttr(~0, Attribute::ReadNone);
1099 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1100 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1101 else removeAttribute(~0, Attribute::ReadNone);
1104 /// @brief Determine if the call does not access or only reads memory.
1105 bool onlyReadsMemory() const {
1106 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1108 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1109 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1110 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1113 /// @brief Determine if the call cannot return.
1114 bool doesNotReturn() const {
1115 return paramHasAttr(~0, Attribute::NoReturn);
1117 void setDoesNotReturn(bool DoesNotReturn = true) {
1118 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1119 else removeAttribute(~0, Attribute::NoReturn);
1122 /// @brief Determine if the call cannot unwind.
1123 bool doesNotThrow() const {
1124 return paramHasAttr(~0, Attribute::NoUnwind);
1126 void setDoesNotThrow(bool DoesNotThrow = true) {
1127 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1128 else removeAttribute(~0, Attribute::NoUnwind);
1131 /// @brief Determine if the call returns a structure through first
1132 /// pointer argument.
1133 bool hasStructRetAttr() const {
1134 // Be friendly and also check the callee.
1135 return paramHasAttr(1, Attribute::StructRet);
1138 /// @brief Determine if any call argument is an aggregate passed by value.
1139 bool hasByValArgument() const {
1140 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1143 /// getCalledFunction - Return the function called, or null if this is an
1144 /// indirect function invocation.
1146 Function *getCalledFunction() const {
1147 return dyn_cast<Function>(Op<0>());
1150 /// getCalledValue - Get a pointer to the function that is invoked by this
1152 const Value *getCalledValue() const { return Op<0>(); }
1153 Value *getCalledValue() { return Op<0>(); }
1155 // Methods for support type inquiry through isa, cast, and dyn_cast:
1156 static inline bool classof(const CallInst *) { return true; }
1157 static inline bool classof(const Instruction *I) {
1158 return I->getOpcode() == Instruction::Call;
1160 static inline bool classof(const Value *V) {
1161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1166 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1169 template<typename InputIterator>
1170 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1171 const Twine &NameStr, BasicBlock *InsertAtEnd)
1172 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1173 ->getElementType())->getReturnType(),
1175 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1176 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1177 init(Func, ArgBegin, ArgEnd, NameStr,
1178 typename std::iterator_traits<InputIterator>::iterator_category());
1181 template<typename InputIterator>
1182 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1183 const Twine &NameStr, Instruction *InsertBefore)
1184 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1185 ->getElementType())->getReturnType(),
1187 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1188 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1189 init(Func, ArgBegin, ArgEnd, NameStr,
1190 typename std::iterator_traits<InputIterator>::iterator_category());
1193 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1195 //===----------------------------------------------------------------------===//
1197 //===----------------------------------------------------------------------===//
1199 /// SelectInst - This class represents the LLVM 'select' instruction.
1201 class SelectInst : public Instruction {
1202 void init(Value *C, Value *S1, Value *S2) {
1203 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1209 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1210 Instruction *InsertBefore)
1211 : Instruction(S1->getType(), Instruction::Select,
1212 &Op<0>(), 3, InsertBefore) {
1216 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1217 BasicBlock *InsertAtEnd)
1218 : Instruction(S1->getType(), Instruction::Select,
1219 &Op<0>(), 3, InsertAtEnd) {
1224 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1225 const Twine &NameStr = "",
1226 Instruction *InsertBefore = 0) {
1227 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1229 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1230 const Twine &NameStr,
1231 BasicBlock *InsertAtEnd) {
1232 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1235 const Value *getCondition() const { return Op<0>(); }
1236 const Value *getTrueValue() const { return Op<1>(); }
1237 const Value *getFalseValue() const { return Op<2>(); }
1238 Value *getCondition() { return Op<0>(); }
1239 Value *getTrueValue() { return Op<1>(); }
1240 Value *getFalseValue() { return Op<2>(); }
1242 /// areInvalidOperands - Return a string if the specified operands are invalid
1243 /// for a select operation, otherwise return null.
1244 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1246 /// Transparently provide more efficient getOperand methods.
1247 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1249 OtherOps getOpcode() const {
1250 return static_cast<OtherOps>(Instruction::getOpcode());
1253 virtual SelectInst *clone() const;
1255 // Methods for support type inquiry through isa, cast, and dyn_cast:
1256 static inline bool classof(const SelectInst *) { return true; }
1257 static inline bool classof(const Instruction *I) {
1258 return I->getOpcode() == Instruction::Select;
1260 static inline bool classof(const Value *V) {
1261 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1266 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1271 //===----------------------------------------------------------------------===//
1273 //===----------------------------------------------------------------------===//
1275 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1276 /// an argument of the specified type given a va_list and increments that list
1278 class VAArgInst : public UnaryInstruction {
1280 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1281 Instruction *InsertBefore = 0)
1282 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1285 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1286 BasicBlock *InsertAtEnd)
1287 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1291 virtual VAArgInst *clone() const;
1293 // Methods for support type inquiry through isa, cast, and dyn_cast:
1294 static inline bool classof(const VAArgInst *) { return true; }
1295 static inline bool classof(const Instruction *I) {
1296 return I->getOpcode() == VAArg;
1298 static inline bool classof(const Value *V) {
1299 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1303 //===----------------------------------------------------------------------===//
1304 // ExtractElementInst Class
1305 //===----------------------------------------------------------------------===//
1307 /// ExtractElementInst - This instruction extracts a single (scalar)
1308 /// element from a VectorType value
1310 class ExtractElementInst : public Instruction {
1311 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1312 Instruction *InsertBefore = 0);
1313 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1314 BasicBlock *InsertAtEnd);
1316 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1317 const Twine &NameStr = "",
1318 Instruction *InsertBefore = 0) {
1319 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1321 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1322 const Twine &NameStr,
1323 BasicBlock *InsertAtEnd) {
1324 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1327 /// isValidOperands - Return true if an extractelement instruction can be
1328 /// formed with the specified operands.
1329 static bool isValidOperands(const Value *Vec, const Value *Idx);
1331 virtual ExtractElementInst *clone() const;
1333 Value *getVectorOperand() { return Op<0>(); }
1334 Value *getIndexOperand() { return Op<1>(); }
1335 const Value *getVectorOperand() const { return Op<0>(); }
1336 const Value *getIndexOperand() const { return Op<1>(); }
1338 const VectorType *getVectorOperandType() const {
1339 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1343 /// Transparently provide more efficient getOperand methods.
1344 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1346 // Methods for support type inquiry through isa, cast, and dyn_cast:
1347 static inline bool classof(const ExtractElementInst *) { return true; }
1348 static inline bool classof(const Instruction *I) {
1349 return I->getOpcode() == Instruction::ExtractElement;
1351 static inline bool classof(const Value *V) {
1352 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1357 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1360 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1362 //===----------------------------------------------------------------------===//
1363 // InsertElementInst Class
1364 //===----------------------------------------------------------------------===//
1366 /// InsertElementInst - This instruction inserts a single (scalar)
1367 /// element into a VectorType value
1369 class InsertElementInst : public Instruction {
1370 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1371 const Twine &NameStr = "",
1372 Instruction *InsertBefore = 0);
1373 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1374 const Twine &NameStr, BasicBlock *InsertAtEnd);
1376 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1377 const Twine &NameStr = "",
1378 Instruction *InsertBefore = 0) {
1379 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1381 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1382 const Twine &NameStr,
1383 BasicBlock *InsertAtEnd) {
1384 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1387 /// isValidOperands - Return true if an insertelement instruction can be
1388 /// formed with the specified operands.
1389 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1392 virtual InsertElementInst *clone() const;
1394 /// getType - Overload to return most specific vector type.
1396 const VectorType *getType() const {
1397 return reinterpret_cast<const VectorType*>(Instruction::getType());
1400 /// Transparently provide more efficient getOperand methods.
1401 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1403 // Methods for support type inquiry through isa, cast, and dyn_cast:
1404 static inline bool classof(const InsertElementInst *) { return true; }
1405 static inline bool classof(const Instruction *I) {
1406 return I->getOpcode() == Instruction::InsertElement;
1408 static inline bool classof(const Value *V) {
1409 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1414 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1417 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1419 //===----------------------------------------------------------------------===//
1420 // ShuffleVectorInst Class
1421 //===----------------------------------------------------------------------===//
1423 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1426 class ShuffleVectorInst : public Instruction {
1428 // allocate space for exactly three operands
1429 void *operator new(size_t s) {
1430 return User::operator new(s, 3);
1432 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1433 const Twine &NameStr = "",
1434 Instruction *InsertBefor = 0);
1435 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1436 const Twine &NameStr, BasicBlock *InsertAtEnd);
1438 /// isValidOperands - Return true if a shufflevector instruction can be
1439 /// formed with the specified operands.
1440 static bool isValidOperands(const Value *V1, const Value *V2,
1443 virtual ShuffleVectorInst *clone() const;
1445 /// getType - Overload to return most specific vector type.
1447 const VectorType *getType() const {
1448 return reinterpret_cast<const VectorType*>(Instruction::getType());
1451 /// Transparently provide more efficient getOperand methods.
1452 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1454 /// getMaskValue - Return the index from the shuffle mask for the specified
1455 /// output result. This is either -1 if the element is undef or a number less
1456 /// than 2*numelements.
1457 int getMaskValue(unsigned i) const;
1459 // Methods for support type inquiry through isa, cast, and dyn_cast:
1460 static inline bool classof(const ShuffleVectorInst *) { return true; }
1461 static inline bool classof(const Instruction *I) {
1462 return I->getOpcode() == Instruction::ShuffleVector;
1464 static inline bool classof(const Value *V) {
1465 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1470 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1473 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1475 //===----------------------------------------------------------------------===//
1476 // ExtractValueInst Class
1477 //===----------------------------------------------------------------------===//
1479 /// ExtractValueInst - This instruction extracts a struct member or array
1480 /// element value from an aggregate value.
1482 class ExtractValueInst : public UnaryInstruction {
1483 SmallVector<unsigned, 4> Indices;
1485 ExtractValueInst(const ExtractValueInst &EVI);
1486 void init(const unsigned *Idx, unsigned NumIdx,
1487 const Twine &NameStr);
1488 void init(unsigned Idx, const Twine &NameStr);
1490 template<typename InputIterator>
1491 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1492 const Twine &NameStr,
1493 // This argument ensures that we have an iterator we can
1494 // do arithmetic on in constant time
1495 std::random_access_iterator_tag) {
1496 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1498 // There's no fundamental reason why we require at least one index
1499 // (other than weirdness with &*IdxBegin being invalid; see
1500 // getelementptr's init routine for example). But there's no
1501 // present need to support it.
1502 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1504 // This requires that the iterator points to contiguous memory.
1505 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1506 // we have to build an array here
1509 /// getIndexedType - Returns the type of the element that would be extracted
1510 /// with an extractvalue instruction with the specified parameters.
1512 /// Null is returned if the indices are invalid for the specified
1515 static const Type *getIndexedType(const Type *Agg,
1516 const unsigned *Idx, unsigned NumIdx);
1518 template<typename InputIterator>
1519 static const Type *getIndexedType(const Type *Ptr,
1520 InputIterator IdxBegin,
1521 InputIterator IdxEnd,
1522 // This argument ensures that we
1523 // have an iterator we can do
1524 // arithmetic on in constant time
1525 std::random_access_iterator_tag) {
1526 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1529 // This requires that the iterator points to contiguous memory.
1530 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1532 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1535 /// Constructors - Create a extractvalue instruction with a base aggregate
1536 /// value and a list of indices. The first ctor can optionally insert before
1537 /// an existing instruction, the second appends the new instruction to the
1538 /// specified BasicBlock.
1539 template<typename InputIterator>
1540 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1541 InputIterator IdxEnd,
1542 const Twine &NameStr,
1543 Instruction *InsertBefore);
1544 template<typename InputIterator>
1545 inline ExtractValueInst(Value *Agg,
1546 InputIterator IdxBegin, InputIterator IdxEnd,
1547 const Twine &NameStr, BasicBlock *InsertAtEnd);
1549 // allocate space for exactly one operand
1550 void *operator new(size_t s) {
1551 return User::operator new(s, 1);
1555 template<typename InputIterator>
1556 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1557 InputIterator IdxEnd,
1558 const Twine &NameStr = "",
1559 Instruction *InsertBefore = 0) {
1561 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1563 template<typename InputIterator>
1564 static ExtractValueInst *Create(Value *Agg,
1565 InputIterator IdxBegin, InputIterator IdxEnd,
1566 const Twine &NameStr,
1567 BasicBlock *InsertAtEnd) {
1568 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1571 /// Constructors - These two creators are convenience methods because one
1572 /// index extractvalue instructions are much more common than those with
1574 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1575 const Twine &NameStr = "",
1576 Instruction *InsertBefore = 0) {
1577 unsigned Idxs[1] = { Idx };
1578 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1580 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1581 const Twine &NameStr,
1582 BasicBlock *InsertAtEnd) {
1583 unsigned Idxs[1] = { Idx };
1584 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1587 virtual ExtractValueInst *clone() const;
1589 /// getIndexedType - Returns the type of the element that would be extracted
1590 /// with an extractvalue instruction with the specified parameters.
1592 /// Null is returned if the indices are invalid for the specified
1595 template<typename InputIterator>
1596 static const Type *getIndexedType(const Type *Ptr,
1597 InputIterator IdxBegin,
1598 InputIterator IdxEnd) {
1599 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1600 typename std::iterator_traits<InputIterator>::
1601 iterator_category());
1603 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1605 typedef const unsigned* idx_iterator;
1606 inline idx_iterator idx_begin() const { return Indices.begin(); }
1607 inline idx_iterator idx_end() const { return Indices.end(); }
1609 Value *getAggregateOperand() {
1610 return getOperand(0);
1612 const Value *getAggregateOperand() const {
1613 return getOperand(0);
1615 static unsigned getAggregateOperandIndex() {
1616 return 0U; // get index for modifying correct operand
1619 unsigned getNumIndices() const { // Note: always non-negative
1620 return (unsigned)Indices.size();
1623 bool hasIndices() const {
1627 // Methods for support type inquiry through isa, cast, and dyn_cast:
1628 static inline bool classof(const ExtractValueInst *) { return true; }
1629 static inline bool classof(const Instruction *I) {
1630 return I->getOpcode() == Instruction::ExtractValue;
1632 static inline bool classof(const Value *V) {
1633 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1637 template<typename InputIterator>
1638 ExtractValueInst::ExtractValueInst(Value *Agg,
1639 InputIterator IdxBegin,
1640 InputIterator IdxEnd,
1641 const Twine &NameStr,
1642 Instruction *InsertBefore)
1643 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1645 ExtractValue, Agg, InsertBefore) {
1646 init(IdxBegin, IdxEnd, NameStr,
1647 typename std::iterator_traits<InputIterator>::iterator_category());
1649 template<typename InputIterator>
1650 ExtractValueInst::ExtractValueInst(Value *Agg,
1651 InputIterator IdxBegin,
1652 InputIterator IdxEnd,
1653 const Twine &NameStr,
1654 BasicBlock *InsertAtEnd)
1655 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1657 ExtractValue, Agg, InsertAtEnd) {
1658 init(IdxBegin, IdxEnd, NameStr,
1659 typename std::iterator_traits<InputIterator>::iterator_category());
1663 //===----------------------------------------------------------------------===//
1664 // InsertValueInst Class
1665 //===----------------------------------------------------------------------===//
1667 /// InsertValueInst - This instruction inserts a struct field of array element
1668 /// value into an aggregate value.
1670 class InsertValueInst : public Instruction {
1671 SmallVector<unsigned, 4> Indices;
1673 void *operator new(size_t, unsigned); // Do not implement
1674 InsertValueInst(const InsertValueInst &IVI);
1675 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1676 const Twine &NameStr);
1677 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1679 template<typename InputIterator>
1680 void init(Value *Agg, Value *Val,
1681 InputIterator IdxBegin, InputIterator IdxEnd,
1682 const Twine &NameStr,
1683 // This argument ensures that we have an iterator we can
1684 // do arithmetic on in constant time
1685 std::random_access_iterator_tag) {
1686 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1688 // There's no fundamental reason why we require at least one index
1689 // (other than weirdness with &*IdxBegin being invalid; see
1690 // getelementptr's init routine for example). But there's no
1691 // present need to support it.
1692 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1694 // This requires that the iterator points to contiguous memory.
1695 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1696 // we have to build an array here
1699 /// Constructors - Create a insertvalue instruction with a base aggregate
1700 /// value, a value to insert, and a list of indices. The first ctor can
1701 /// optionally insert before an existing instruction, the second appends
1702 /// the new instruction to the specified BasicBlock.
1703 template<typename InputIterator>
1704 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1705 InputIterator IdxEnd,
1706 const Twine &NameStr,
1707 Instruction *InsertBefore);
1708 template<typename InputIterator>
1709 inline InsertValueInst(Value *Agg, Value *Val,
1710 InputIterator IdxBegin, InputIterator IdxEnd,
1711 const Twine &NameStr, BasicBlock *InsertAtEnd);
1713 /// Constructors - These two constructors are convenience methods because one
1714 /// and two index insertvalue instructions are so common.
1715 InsertValueInst(Value *Agg, Value *Val,
1716 unsigned Idx, const Twine &NameStr = "",
1717 Instruction *InsertBefore = 0);
1718 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1719 const Twine &NameStr, BasicBlock *InsertAtEnd);
1721 // allocate space for exactly two operands
1722 void *operator new(size_t s) {
1723 return User::operator new(s, 2);
1726 template<typename InputIterator>
1727 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1728 InputIterator IdxEnd,
1729 const Twine &NameStr = "",
1730 Instruction *InsertBefore = 0) {
1731 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1732 NameStr, InsertBefore);
1734 template<typename InputIterator>
1735 static InsertValueInst *Create(Value *Agg, Value *Val,
1736 InputIterator IdxBegin, InputIterator IdxEnd,
1737 const Twine &NameStr,
1738 BasicBlock *InsertAtEnd) {
1739 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1740 NameStr, InsertAtEnd);
1743 /// Constructors - These two creators are convenience methods because one
1744 /// index insertvalue instructions are much more common than those with
1746 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1747 const Twine &NameStr = "",
1748 Instruction *InsertBefore = 0) {
1749 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1751 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1752 const Twine &NameStr,
1753 BasicBlock *InsertAtEnd) {
1754 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1757 virtual InsertValueInst *clone() const;
1759 /// Transparently provide more efficient getOperand methods.
1760 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1762 typedef const unsigned* idx_iterator;
1763 inline idx_iterator idx_begin() const { return Indices.begin(); }
1764 inline idx_iterator idx_end() const { return Indices.end(); }
1766 Value *getAggregateOperand() {
1767 return getOperand(0);
1769 const Value *getAggregateOperand() const {
1770 return getOperand(0);
1772 static unsigned getAggregateOperandIndex() {
1773 return 0U; // get index for modifying correct operand
1776 Value *getInsertedValueOperand() {
1777 return getOperand(1);
1779 const Value *getInsertedValueOperand() const {
1780 return getOperand(1);
1782 static unsigned getInsertedValueOperandIndex() {
1783 return 1U; // get index for modifying correct operand
1786 unsigned getNumIndices() const { // Note: always non-negative
1787 return (unsigned)Indices.size();
1790 bool hasIndices() const {
1794 // Methods for support type inquiry through isa, cast, and dyn_cast:
1795 static inline bool classof(const InsertValueInst *) { return true; }
1796 static inline bool classof(const Instruction *I) {
1797 return I->getOpcode() == Instruction::InsertValue;
1799 static inline bool classof(const Value *V) {
1800 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1805 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1808 template<typename InputIterator>
1809 InsertValueInst::InsertValueInst(Value *Agg,
1811 InputIterator IdxBegin,
1812 InputIterator IdxEnd,
1813 const Twine &NameStr,
1814 Instruction *InsertBefore)
1815 : Instruction(Agg->getType(), InsertValue,
1816 OperandTraits<InsertValueInst>::op_begin(this),
1818 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1819 typename std::iterator_traits<InputIterator>::iterator_category());
1821 template<typename InputIterator>
1822 InsertValueInst::InsertValueInst(Value *Agg,
1824 InputIterator IdxBegin,
1825 InputIterator IdxEnd,
1826 const Twine &NameStr,
1827 BasicBlock *InsertAtEnd)
1828 : Instruction(Agg->getType(), InsertValue,
1829 OperandTraits<InsertValueInst>::op_begin(this),
1831 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1832 typename std::iterator_traits<InputIterator>::iterator_category());
1835 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1837 //===----------------------------------------------------------------------===//
1839 //===----------------------------------------------------------------------===//
1841 // PHINode - The PHINode class is used to represent the magical mystical PHI
1842 // node, that can not exist in nature, but can be synthesized in a computer
1843 // scientist's overactive imagination.
1845 class PHINode : public Instruction {
1846 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1847 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1848 /// the number actually in use.
1849 unsigned ReservedSpace;
1850 PHINode(const PHINode &PN);
1851 // allocate space for exactly zero operands
1852 void *operator new(size_t s) {
1853 return User::operator new(s, 0);
1855 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1856 Instruction *InsertBefore = 0)
1857 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1862 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1863 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1868 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1869 Instruction *InsertBefore = 0) {
1870 return new PHINode(Ty, NameStr, InsertBefore);
1872 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1873 BasicBlock *InsertAtEnd) {
1874 return new PHINode(Ty, NameStr, InsertAtEnd);
1878 /// reserveOperandSpace - This method can be used to avoid repeated
1879 /// reallocation of PHI operand lists by reserving space for the correct
1880 /// number of operands before adding them. Unlike normal vector reserves,
1881 /// this method can also be used to trim the operand space.
1882 void reserveOperandSpace(unsigned NumValues) {
1883 resizeOperands(NumValues*2);
1886 virtual PHINode *clone() const;
1888 /// Provide fast operand accessors
1889 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1891 /// getNumIncomingValues - Return the number of incoming edges
1893 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1895 /// getIncomingValue - Return incoming value number x
1897 Value *getIncomingValue(unsigned i) const {
1898 assert(i*2 < getNumOperands() && "Invalid value number!");
1899 return getOperand(i*2);
1901 void setIncomingValue(unsigned i, Value *V) {
1902 assert(i*2 < getNumOperands() && "Invalid value number!");
1905 static unsigned getOperandNumForIncomingValue(unsigned i) {
1908 static unsigned getIncomingValueNumForOperand(unsigned i) {
1909 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1913 /// getIncomingBlock - Return incoming basic block #i.
1915 BasicBlock *getIncomingBlock(unsigned i) const {
1916 return static_cast<BasicBlock*>(getOperand(i*2+1));
1919 /// getIncomingBlock - Return incoming basic block corresponding
1920 /// to an operand of the PHI.
1922 BasicBlock *getIncomingBlock(const Use &U) const {
1923 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1924 return static_cast<BasicBlock*>((&U + 1)->get());
1927 /// getIncomingBlock - Return incoming basic block corresponding
1928 /// to value use iterator.
1930 template <typename U>
1931 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1932 return getIncomingBlock(I.getUse());
1936 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1937 setOperand(i*2+1, BB);
1939 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1942 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1943 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1947 /// addIncoming - Add an incoming value to the end of the PHI list
1949 void addIncoming(Value *V, BasicBlock *BB) {
1950 assert(V && "PHI node got a null value!");
1951 assert(BB && "PHI node got a null basic block!");
1952 assert(getType() == V->getType() &&
1953 "All operands to PHI node must be the same type as the PHI node!");
1954 unsigned OpNo = NumOperands;
1955 if (OpNo+2 > ReservedSpace)
1956 resizeOperands(0); // Get more space!
1957 // Initialize some new operands.
1958 NumOperands = OpNo+2;
1959 OperandList[OpNo] = V;
1960 OperandList[OpNo+1] = BB;
1963 /// removeIncomingValue - Remove an incoming value. This is useful if a
1964 /// predecessor basic block is deleted. The value removed is returned.
1966 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1967 /// is true), the PHI node is destroyed and any uses of it are replaced with
1968 /// dummy values. The only time there should be zero incoming values to a PHI
1969 /// node is when the block is dead, so this strategy is sound.
1971 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1973 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1974 int Idx = getBasicBlockIndex(BB);
1975 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1976 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1979 /// getBasicBlockIndex - Return the first index of the specified basic
1980 /// block in the value list for this PHI. Returns -1 if no instance.
1982 int getBasicBlockIndex(const BasicBlock *BB) const {
1983 Use *OL = OperandList;
1984 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1985 if (OL[i+1].get() == BB) return i/2;
1989 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1990 return getIncomingValue(getBasicBlockIndex(BB));
1993 /// hasConstantValue - If the specified PHI node always merges together the
1994 /// same value, return the value, otherwise return null.
1996 /// If the PHI has undef operands, but all the rest of the operands are
1997 /// some unique value, return that value if it can be proved that the
1998 /// value dominates the PHI. If DT is null, use a conservative check,
1999 /// otherwise use DT to test for dominance.
2001 Value *hasConstantValue(DominatorTree *DT = 0) const;
2003 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2004 static inline bool classof(const PHINode *) { return true; }
2005 static inline bool classof(const Instruction *I) {
2006 return I->getOpcode() == Instruction::PHI;
2008 static inline bool classof(const Value *V) {
2009 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2012 void resizeOperands(unsigned NumOperands);
2016 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
2019 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2022 //===----------------------------------------------------------------------===//
2024 //===----------------------------------------------------------------------===//
2026 //===---------------------------------------------------------------------------
2027 /// ReturnInst - Return a value (possibly void), from a function. Execution
2028 /// does not continue in this function any longer.
2030 class ReturnInst : public TerminatorInst {
2031 ReturnInst(const ReturnInst &RI);
2034 // ReturnInst constructors:
2035 // ReturnInst() - 'ret void' instruction
2036 // ReturnInst( null) - 'ret void' instruction
2037 // ReturnInst(Value* X) - 'ret X' instruction
2038 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2039 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2040 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2041 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2043 // NOTE: If the Value* passed is of type void then the constructor behaves as
2044 // if it was passed NULL.
2045 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2046 Instruction *InsertBefore = 0);
2047 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2048 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2050 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2051 Instruction *InsertBefore = 0) {
2052 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2054 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2055 BasicBlock *InsertAtEnd) {
2056 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2058 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2059 return new(0) ReturnInst(C, InsertAtEnd);
2061 virtual ~ReturnInst();
2063 virtual ReturnInst *clone() const;
2065 /// Provide fast operand accessors
2066 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2068 /// Convenience accessor
2069 Value *getReturnValue(unsigned n = 0) const {
2070 return n < getNumOperands()
2075 unsigned getNumSuccessors() const { return 0; }
2077 // Methods for support type inquiry through isa, cast, and dyn_cast:
2078 static inline bool classof(const ReturnInst *) { return true; }
2079 static inline bool classof(const Instruction *I) {
2080 return (I->getOpcode() == Instruction::Ret);
2082 static inline bool classof(const Value *V) {
2083 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2086 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2087 virtual unsigned getNumSuccessorsV() const;
2088 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2092 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
2095 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2097 //===----------------------------------------------------------------------===//
2099 //===----------------------------------------------------------------------===//
2101 //===---------------------------------------------------------------------------
2102 /// BranchInst - Conditional or Unconditional Branch instruction.
2104 class BranchInst : public TerminatorInst {
2105 /// Ops list - Branches are strange. The operands are ordered:
2106 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2107 /// they don't have to check for cond/uncond branchness. These are mostly
2108 /// accessed relative from op_end().
2109 BranchInst(const BranchInst &BI);
2111 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2112 // BranchInst(BB *B) - 'br B'
2113 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2114 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2115 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2116 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2117 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2118 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2119 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2120 Instruction *InsertBefore = 0);
2121 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2122 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2123 BasicBlock *InsertAtEnd);
2125 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2126 return new(1, true) BranchInst(IfTrue, InsertBefore);
2128 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2129 Value *Cond, Instruction *InsertBefore = 0) {
2130 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2132 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2133 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2135 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2136 Value *Cond, BasicBlock *InsertAtEnd) {
2137 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2142 /// Transparently provide more efficient getOperand methods.
2143 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2145 virtual BranchInst *clone() const;
2147 bool isUnconditional() const { return getNumOperands() == 1; }
2148 bool isConditional() const { return getNumOperands() == 3; }
2150 Value *getCondition() const {
2151 assert(isConditional() && "Cannot get condition of an uncond branch!");
2155 void setCondition(Value *V) {
2156 assert(isConditional() && "Cannot set condition of unconditional branch!");
2160 // setUnconditionalDest - Change the current branch to an unconditional branch
2161 // targeting the specified block.
2162 // FIXME: Eliminate this ugly method.
2163 void setUnconditionalDest(BasicBlock *Dest) {
2165 if (isConditional()) { // Convert this to an uncond branch.
2169 OperandList = op_begin();
2173 unsigned getNumSuccessors() const { return 1+isConditional(); }
2175 BasicBlock *getSuccessor(unsigned i) const {
2176 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2177 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2180 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2181 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2182 *(&Op<-1>() - idx) = NewSucc;
2185 // Methods for support type inquiry through isa, cast, and dyn_cast:
2186 static inline bool classof(const BranchInst *) { return true; }
2187 static inline bool classof(const Instruction *I) {
2188 return (I->getOpcode() == Instruction::Br);
2190 static inline bool classof(const Value *V) {
2191 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2194 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2195 virtual unsigned getNumSuccessorsV() const;
2196 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2200 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2202 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2204 //===----------------------------------------------------------------------===//
2206 //===----------------------------------------------------------------------===//
2208 //===---------------------------------------------------------------------------
2209 /// SwitchInst - Multiway switch
2211 class SwitchInst : public TerminatorInst {
2212 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2213 unsigned ReservedSpace;
2214 // Operand[0] = Value to switch on
2215 // Operand[1] = Default basic block destination
2216 // Operand[2n ] = Value to match
2217 // Operand[2n+1] = BasicBlock to go to on match
2218 SwitchInst(const SwitchInst &RI);
2219 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2220 void resizeOperands(unsigned No);
2221 // allocate space for exactly zero operands
2222 void *operator new(size_t s) {
2223 return User::operator new(s, 0);
2225 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2226 /// switch on and a default destination. The number of additional cases can
2227 /// be specified here to make memory allocation more efficient. This
2228 /// constructor can also autoinsert before another instruction.
2229 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2230 Instruction *InsertBefore = 0);
2232 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2233 /// switch on and a default destination. The number of additional cases can
2234 /// be specified here to make memory allocation more efficient. This
2235 /// constructor also autoinserts at the end of the specified BasicBlock.
2236 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2237 BasicBlock *InsertAtEnd);
2239 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2240 unsigned NumCases, Instruction *InsertBefore = 0) {
2241 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2243 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2244 unsigned NumCases, BasicBlock *InsertAtEnd) {
2245 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2249 /// Provide fast operand accessors
2250 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2252 // Accessor Methods for Switch stmt
2253 Value *getCondition() const { return getOperand(0); }
2254 void setCondition(Value *V) { setOperand(0, V); }
2256 BasicBlock *getDefaultDest() const {
2257 return cast<BasicBlock>(getOperand(1));
2260 /// getNumCases - return the number of 'cases' in this switch instruction.
2261 /// Note that case #0 is always the default case.
2262 unsigned getNumCases() const {
2263 return getNumOperands()/2;
2266 /// getCaseValue - Return the specified case value. Note that case #0, the
2267 /// default destination, does not have a case value.
2268 ConstantInt *getCaseValue(unsigned i) {
2269 assert(i && i < getNumCases() && "Illegal case value to get!");
2270 return getSuccessorValue(i);
2273 /// getCaseValue - Return the specified case value. Note that case #0, the
2274 /// default destination, does not have a case value.
2275 const ConstantInt *getCaseValue(unsigned i) const {
2276 assert(i && i < getNumCases() && "Illegal case value to get!");
2277 return getSuccessorValue(i);
2280 /// findCaseValue - Search all of the case values for the specified constant.
2281 /// If it is explicitly handled, return the case number of it, otherwise
2282 /// return 0 to indicate that it is handled by the default handler.
2283 unsigned findCaseValue(const ConstantInt *C) const {
2284 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2285 if (getCaseValue(i) == C)
2290 /// findCaseDest - Finds the unique case value for a given successor. Returns
2291 /// null if the successor is not found, not unique, or is the default case.
2292 ConstantInt *findCaseDest(BasicBlock *BB) {
2293 if (BB == getDefaultDest()) return NULL;
2295 ConstantInt *CI = NULL;
2296 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2297 if (getSuccessor(i) == BB) {
2298 if (CI) return NULL; // Multiple cases lead to BB.
2299 else CI = getCaseValue(i);
2305 /// addCase - Add an entry to the switch instruction...
2307 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2309 /// removeCase - This method removes the specified successor from the switch
2310 /// instruction. Note that this cannot be used to remove the default
2311 /// destination (successor #0).
2313 void removeCase(unsigned idx);
2315 virtual SwitchInst *clone() const;
2317 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2318 BasicBlock *getSuccessor(unsigned idx) const {
2319 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2320 return cast<BasicBlock>(getOperand(idx*2+1));
2322 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2323 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2324 setOperand(idx*2+1, NewSucc);
2327 // getSuccessorValue - Return the value associated with the specified
2329 ConstantInt *getSuccessorValue(unsigned idx) const {
2330 assert(idx < getNumSuccessors() && "Successor # out of range!");
2331 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2334 // Methods for support type inquiry through isa, cast, and dyn_cast:
2335 static inline bool classof(const SwitchInst *) { return true; }
2336 static inline bool classof(const Instruction *I) {
2337 return I->getOpcode() == Instruction::Switch;
2339 static inline bool classof(const Value *V) {
2340 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2343 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2344 virtual unsigned getNumSuccessorsV() const;
2345 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2349 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2352 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2355 //===----------------------------------------------------------------------===//
2357 //===----------------------------------------------------------------------===//
2359 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2360 /// calling convention of the call.
2362 class InvokeInst : public TerminatorInst {
2363 AttrListPtr AttributeList;
2364 InvokeInst(const InvokeInst &BI);
2365 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2366 Value* const *Args, unsigned NumArgs);
2368 template<typename InputIterator>
2369 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2370 InputIterator ArgBegin, InputIterator ArgEnd,
2371 const Twine &NameStr,
2372 // This argument ensures that we have an iterator we can
2373 // do arithmetic on in constant time
2374 std::random_access_iterator_tag) {
2375 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2377 // This requires that the iterator points to contiguous memory.
2378 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2382 /// Construct an InvokeInst given a range of arguments.
2383 /// InputIterator must be a random-access iterator pointing to
2384 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2385 /// made for random-accessness but not for contiguous storage as
2386 /// that would incur runtime overhead.
2388 /// @brief Construct an InvokeInst from a range of arguments
2389 template<typename InputIterator>
2390 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2391 InputIterator ArgBegin, InputIterator ArgEnd,
2393 const Twine &NameStr, Instruction *InsertBefore);
2395 /// Construct an InvokeInst given a range of arguments.
2396 /// InputIterator must be a random-access iterator pointing to
2397 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2398 /// made for random-accessness but not for contiguous storage as
2399 /// that would incur runtime overhead.
2401 /// @brief Construct an InvokeInst from a range of arguments
2402 template<typename InputIterator>
2403 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2404 InputIterator ArgBegin, InputIterator ArgEnd,
2406 const Twine &NameStr, BasicBlock *InsertAtEnd);
2408 template<typename InputIterator>
2409 static InvokeInst *Create(Value *Func,
2410 BasicBlock *IfNormal, BasicBlock *IfException,
2411 InputIterator ArgBegin, InputIterator ArgEnd,
2412 const Twine &NameStr = "",
2413 Instruction *InsertBefore = 0) {
2414 unsigned Values(ArgEnd - ArgBegin + 3);
2415 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2416 Values, NameStr, InsertBefore);
2418 template<typename InputIterator>
2419 static InvokeInst *Create(Value *Func,
2420 BasicBlock *IfNormal, BasicBlock *IfException,
2421 InputIterator ArgBegin, InputIterator ArgEnd,
2422 const Twine &NameStr,
2423 BasicBlock *InsertAtEnd) {
2424 unsigned Values(ArgEnd - ArgBegin + 3);
2425 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2426 Values, NameStr, InsertAtEnd);
2429 virtual InvokeInst *clone() const;
2431 /// Provide fast operand accessors
2432 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2434 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2436 CallingConv::ID getCallingConv() const {
2437 return static_cast<CallingConv::ID>(SubclassData);
2439 void setCallingConv(CallingConv::ID CC) {
2440 SubclassData = static_cast<unsigned>(CC);
2443 /// getAttributes - Return the parameter attributes for this invoke.
2445 const AttrListPtr &getAttributes() const { return AttributeList; }
2447 /// setAttributes - Set the parameter attributes for this invoke.
2449 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2451 /// addAttribute - adds the attribute to the list of attributes.
2452 void addAttribute(unsigned i, Attributes attr);
2454 /// removeAttribute - removes the attribute from the list of attributes.
2455 void removeAttribute(unsigned i, Attributes attr);
2457 /// @brief Determine whether the call or the callee has the given attribute.
2458 bool paramHasAttr(unsigned i, Attributes attr) const;
2460 /// @brief Extract the alignment for a call or parameter (0=unknown).
2461 unsigned getParamAlignment(unsigned i) const {
2462 return AttributeList.getParamAlignment(i);
2465 /// @brief Determine if the call does not access memory.
2466 bool doesNotAccessMemory() const {
2467 return paramHasAttr(~0, Attribute::ReadNone);
2469 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2470 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2471 else removeAttribute(~0, Attribute::ReadNone);
2474 /// @brief Determine if the call does not access or only reads memory.
2475 bool onlyReadsMemory() const {
2476 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2478 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2479 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2480 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2483 /// @brief Determine if the call cannot return.
2484 bool doesNotReturn() const {
2485 return paramHasAttr(~0, Attribute::NoReturn);
2487 void setDoesNotReturn(bool DoesNotReturn = true) {
2488 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2489 else removeAttribute(~0, Attribute::NoReturn);
2492 /// @brief Determine if the call cannot unwind.
2493 bool doesNotThrow() const {
2494 return paramHasAttr(~0, Attribute::NoUnwind);
2496 void setDoesNotThrow(bool DoesNotThrow = true) {
2497 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2498 else removeAttribute(~0, Attribute::NoUnwind);
2501 /// @brief Determine if the call returns a structure through first
2502 /// pointer argument.
2503 bool hasStructRetAttr() const {
2504 // Be friendly and also check the callee.
2505 return paramHasAttr(1, Attribute::StructRet);
2508 /// @brief Determine if any call argument is an aggregate passed by value.
2509 bool hasByValArgument() const {
2510 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2513 /// getCalledFunction - Return the function called, or null if this is an
2514 /// indirect function invocation.
2516 Function *getCalledFunction() const {
2517 return dyn_cast<Function>(getOperand(0));
2520 /// getCalledValue - Get a pointer to the function that is invoked by this
2522 const Value *getCalledValue() const { return getOperand(0); }
2523 Value *getCalledValue() { return getOperand(0); }
2525 // get*Dest - Return the destination basic blocks...
2526 BasicBlock *getNormalDest() const {
2527 return cast<BasicBlock>(getOperand(1));
2529 BasicBlock *getUnwindDest() const {
2530 return cast<BasicBlock>(getOperand(2));
2532 void setNormalDest(BasicBlock *B) {
2536 void setUnwindDest(BasicBlock *B) {
2540 BasicBlock *getSuccessor(unsigned i) const {
2541 assert(i < 2 && "Successor # out of range for invoke!");
2542 return i == 0 ? getNormalDest() : getUnwindDest();
2545 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2546 assert(idx < 2 && "Successor # out of range for invoke!");
2547 setOperand(idx+1, NewSucc);
2550 unsigned getNumSuccessors() const { return 2; }
2552 // Methods for support type inquiry through isa, cast, and dyn_cast:
2553 static inline bool classof(const InvokeInst *) { return true; }
2554 static inline bool classof(const Instruction *I) {
2555 return (I->getOpcode() == Instruction::Invoke);
2557 static inline bool classof(const Value *V) {
2558 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2561 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2562 virtual unsigned getNumSuccessorsV() const;
2563 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2567 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2570 template<typename InputIterator>
2571 InvokeInst::InvokeInst(Value *Func,
2572 BasicBlock *IfNormal, BasicBlock *IfException,
2573 InputIterator ArgBegin, InputIterator ArgEnd,
2575 const Twine &NameStr, Instruction *InsertBefore)
2576 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2577 ->getElementType())->getReturnType(),
2578 Instruction::Invoke,
2579 OperandTraits<InvokeInst>::op_end(this) - Values,
2580 Values, InsertBefore) {
2581 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2582 typename std::iterator_traits<InputIterator>::iterator_category());
2584 template<typename InputIterator>
2585 InvokeInst::InvokeInst(Value *Func,
2586 BasicBlock *IfNormal, BasicBlock *IfException,
2587 InputIterator ArgBegin, InputIterator ArgEnd,
2589 const Twine &NameStr, BasicBlock *InsertAtEnd)
2590 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2591 ->getElementType())->getReturnType(),
2592 Instruction::Invoke,
2593 OperandTraits<InvokeInst>::op_end(this) - Values,
2594 Values, InsertAtEnd) {
2595 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2596 typename std::iterator_traits<InputIterator>::iterator_category());
2599 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2601 //===----------------------------------------------------------------------===//
2603 //===----------------------------------------------------------------------===//
2605 //===---------------------------------------------------------------------------
2606 /// UnwindInst - Immediately exit the current function, unwinding the stack
2607 /// until an invoke instruction is found.
2609 class UnwindInst : public TerminatorInst {
2610 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2612 // allocate space for exactly zero operands
2613 void *operator new(size_t s) {
2614 return User::operator new(s, 0);
2616 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2617 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2619 virtual UnwindInst *clone() const;
2621 unsigned getNumSuccessors() const { return 0; }
2623 // Methods for support type inquiry through isa, cast, and dyn_cast:
2624 static inline bool classof(const UnwindInst *) { return true; }
2625 static inline bool classof(const Instruction *I) {
2626 return I->getOpcode() == Instruction::Unwind;
2628 static inline bool classof(const Value *V) {
2629 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2632 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2633 virtual unsigned getNumSuccessorsV() const;
2634 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2637 //===----------------------------------------------------------------------===//
2638 // UnreachableInst Class
2639 //===----------------------------------------------------------------------===//
2641 //===---------------------------------------------------------------------------
2642 /// UnreachableInst - This function has undefined behavior. In particular, the
2643 /// presence of this instruction indicates some higher level knowledge that the
2644 /// end of the block cannot be reached.
2646 class UnreachableInst : public TerminatorInst {
2647 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2649 // allocate space for exactly zero operands
2650 void *operator new(size_t s) {
2651 return User::operator new(s, 0);
2653 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2654 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2656 virtual UnreachableInst *clone() const;
2658 unsigned getNumSuccessors() const { return 0; }
2660 // Methods for support type inquiry through isa, cast, and dyn_cast:
2661 static inline bool classof(const UnreachableInst *) { return true; }
2662 static inline bool classof(const Instruction *I) {
2663 return I->getOpcode() == Instruction::Unreachable;
2665 static inline bool classof(const Value *V) {
2666 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2669 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2670 virtual unsigned getNumSuccessorsV() const;
2671 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2674 //===----------------------------------------------------------------------===//
2676 //===----------------------------------------------------------------------===//
2678 /// @brief This class represents a truncation of integer types.
2679 class TruncInst : public CastInst {
2681 /// @brief Constructor with insert-before-instruction semantics
2683 Value *S, ///< The value to be truncated
2684 const Type *Ty, ///< The (smaller) type to truncate to
2685 const Twine &NameStr = "", ///< A name for the new instruction
2686 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2689 /// @brief Constructor with insert-at-end-of-block semantics
2691 Value *S, ///< The value to be truncated
2692 const Type *Ty, ///< The (smaller) type to truncate to
2693 const Twine &NameStr, ///< A name for the new instruction
2694 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2697 /// @brief Clone an identical TruncInst
2698 virtual TruncInst *clone() const;
2700 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2701 static inline bool classof(const TruncInst *) { return true; }
2702 static inline bool classof(const Instruction *I) {
2703 return I->getOpcode() == Trunc;
2705 static inline bool classof(const Value *V) {
2706 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2710 //===----------------------------------------------------------------------===//
2712 //===----------------------------------------------------------------------===//
2714 /// @brief This class represents zero extension of integer types.
2715 class ZExtInst : public CastInst {
2717 /// @brief Constructor with insert-before-instruction semantics
2719 Value *S, ///< The value to be zero extended
2720 const Type *Ty, ///< The type to zero extend to
2721 const Twine &NameStr = "", ///< A name for the new instruction
2722 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2725 /// @brief Constructor with insert-at-end semantics.
2727 Value *S, ///< The value to be zero extended
2728 const Type *Ty, ///< The type to zero extend to
2729 const Twine &NameStr, ///< A name for the new instruction
2730 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2733 /// @brief Clone an identical ZExtInst
2734 virtual ZExtInst *clone() const;
2736 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2737 static inline bool classof(const ZExtInst *) { return true; }
2738 static inline bool classof(const Instruction *I) {
2739 return I->getOpcode() == ZExt;
2741 static inline bool classof(const Value *V) {
2742 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2746 //===----------------------------------------------------------------------===//
2748 //===----------------------------------------------------------------------===//
2750 /// @brief This class represents a sign extension of integer types.
2751 class SExtInst : public CastInst {
2753 /// @brief Constructor with insert-before-instruction semantics
2755 Value *S, ///< The value to be sign extended
2756 const Type *Ty, ///< The type to sign extend to
2757 const Twine &NameStr = "", ///< A name for the new instruction
2758 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2761 /// @brief Constructor with insert-at-end-of-block semantics
2763 Value *S, ///< The value to be sign extended
2764 const Type *Ty, ///< The type to sign extend to
2765 const Twine &NameStr, ///< A name for the new instruction
2766 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2769 /// @brief Clone an identical SExtInst
2770 virtual SExtInst *clone() const;
2772 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2773 static inline bool classof(const SExtInst *) { return true; }
2774 static inline bool classof(const Instruction *I) {
2775 return I->getOpcode() == SExt;
2777 static inline bool classof(const Value *V) {
2778 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2782 //===----------------------------------------------------------------------===//
2783 // FPTruncInst Class
2784 //===----------------------------------------------------------------------===//
2786 /// @brief This class represents a truncation of floating point types.
2787 class FPTruncInst : public CastInst {
2789 /// @brief Constructor with insert-before-instruction semantics
2791 Value *S, ///< The value to be truncated
2792 const Type *Ty, ///< The type to truncate to
2793 const Twine &NameStr = "", ///< A name for the new instruction
2794 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2797 /// @brief Constructor with insert-before-instruction semantics
2799 Value *S, ///< The value to be truncated
2800 const Type *Ty, ///< The type to truncate to
2801 const Twine &NameStr, ///< A name for the new instruction
2802 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2805 /// @brief Clone an identical FPTruncInst
2806 virtual FPTruncInst *clone() const;
2808 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2809 static inline bool classof(const FPTruncInst *) { return true; }
2810 static inline bool classof(const Instruction *I) {
2811 return I->getOpcode() == FPTrunc;
2813 static inline bool classof(const Value *V) {
2814 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2818 //===----------------------------------------------------------------------===//
2820 //===----------------------------------------------------------------------===//
2822 /// @brief This class represents an extension of floating point types.
2823 class FPExtInst : public CastInst {
2825 /// @brief Constructor with insert-before-instruction semantics
2827 Value *S, ///< The value to be extended
2828 const Type *Ty, ///< The type to extend to
2829 const Twine &NameStr = "", ///< A name for the new instruction
2830 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2833 /// @brief Constructor with insert-at-end-of-block semantics
2835 Value *S, ///< The value to be extended
2836 const Type *Ty, ///< The type to extend to
2837 const Twine &NameStr, ///< A name for the new instruction
2838 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2841 /// @brief Clone an identical FPExtInst
2842 virtual FPExtInst *clone() const;
2844 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2845 static inline bool classof(const FPExtInst *) { return true; }
2846 static inline bool classof(const Instruction *I) {
2847 return I->getOpcode() == FPExt;
2849 static inline bool classof(const Value *V) {
2850 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2854 //===----------------------------------------------------------------------===//
2856 //===----------------------------------------------------------------------===//
2858 /// @brief This class represents a cast unsigned integer to floating point.
2859 class UIToFPInst : public CastInst {
2861 /// @brief Constructor with insert-before-instruction semantics
2863 Value *S, ///< The value to be converted
2864 const Type *Ty, ///< The type to convert to
2865 const Twine &NameStr = "", ///< A name for the new instruction
2866 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2869 /// @brief Constructor with insert-at-end-of-block semantics
2871 Value *S, ///< The value to be converted
2872 const Type *Ty, ///< The type to convert to
2873 const Twine &NameStr, ///< A name for the new instruction
2874 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2877 /// @brief Clone an identical UIToFPInst
2878 virtual UIToFPInst *clone() const;
2880 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2881 static inline bool classof(const UIToFPInst *) { return true; }
2882 static inline bool classof(const Instruction *I) {
2883 return I->getOpcode() == UIToFP;
2885 static inline bool classof(const Value *V) {
2886 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2890 //===----------------------------------------------------------------------===//
2892 //===----------------------------------------------------------------------===//
2894 /// @brief This class represents a cast from signed integer to floating point.
2895 class SIToFPInst : public CastInst {
2897 /// @brief Constructor with insert-before-instruction semantics
2899 Value *S, ///< The value to be converted
2900 const Type *Ty, ///< The type to convert to
2901 const Twine &NameStr = "", ///< A name for the new instruction
2902 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2905 /// @brief Constructor with insert-at-end-of-block semantics
2907 Value *S, ///< The value to be converted
2908 const Type *Ty, ///< The type to convert to
2909 const Twine &NameStr, ///< A name for the new instruction
2910 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2913 /// @brief Clone an identical SIToFPInst
2914 virtual SIToFPInst *clone() const;
2916 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2917 static inline bool classof(const SIToFPInst *) { return true; }
2918 static inline bool classof(const Instruction *I) {
2919 return I->getOpcode() == SIToFP;
2921 static inline bool classof(const Value *V) {
2922 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2926 //===----------------------------------------------------------------------===//
2928 //===----------------------------------------------------------------------===//
2930 /// @brief This class represents a cast from floating point to unsigned integer
2931 class FPToUIInst : public CastInst {
2933 /// @brief Constructor with insert-before-instruction semantics
2935 Value *S, ///< The value to be converted
2936 const Type *Ty, ///< The type to convert to
2937 const Twine &NameStr = "", ///< A name for the new instruction
2938 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2941 /// @brief Constructor with insert-at-end-of-block semantics
2943 Value *S, ///< The value to be converted
2944 const Type *Ty, ///< The type to convert to
2945 const Twine &NameStr, ///< A name for the new instruction
2946 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2949 /// @brief Clone an identical FPToUIInst
2950 virtual FPToUIInst *clone() const;
2952 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2953 static inline bool classof(const FPToUIInst *) { return true; }
2954 static inline bool classof(const Instruction *I) {
2955 return I->getOpcode() == FPToUI;
2957 static inline bool classof(const Value *V) {
2958 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2962 //===----------------------------------------------------------------------===//
2964 //===----------------------------------------------------------------------===//
2966 /// @brief This class represents a cast from floating point to signed integer.
2967 class FPToSIInst : public CastInst {
2969 /// @brief Constructor with insert-before-instruction semantics
2971 Value *S, ///< The value to be converted
2972 const Type *Ty, ///< The type to convert to
2973 const Twine &NameStr = "", ///< A name for the new instruction
2974 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2977 /// @brief Constructor with insert-at-end-of-block semantics
2979 Value *S, ///< The value to be converted
2980 const Type *Ty, ///< The type to convert to
2981 const Twine &NameStr, ///< A name for the new instruction
2982 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2985 /// @brief Clone an identical FPToSIInst
2986 virtual FPToSIInst *clone() const;
2988 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2989 static inline bool classof(const FPToSIInst *) { return true; }
2990 static inline bool classof(const Instruction *I) {
2991 return I->getOpcode() == FPToSI;
2993 static inline bool classof(const Value *V) {
2994 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2998 //===----------------------------------------------------------------------===//
2999 // IntToPtrInst Class
3000 //===----------------------------------------------------------------------===//
3002 /// @brief This class represents a cast from an integer to a pointer.
3003 class IntToPtrInst : public CastInst {
3005 /// @brief Constructor with insert-before-instruction semantics
3007 Value *S, ///< The value to be converted
3008 const Type *Ty, ///< The type to convert to
3009 const Twine &NameStr = "", ///< A name for the new instruction
3010 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3013 /// @brief Constructor with insert-at-end-of-block semantics
3015 Value *S, ///< The value to be converted
3016 const Type *Ty, ///< The type to convert to
3017 const Twine &NameStr, ///< A name for the new instruction
3018 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3021 /// @brief Clone an identical IntToPtrInst
3022 virtual IntToPtrInst *clone() const;
3024 // Methods for support type inquiry through isa, cast, and dyn_cast:
3025 static inline bool classof(const IntToPtrInst *) { return true; }
3026 static inline bool classof(const Instruction *I) {
3027 return I->getOpcode() == IntToPtr;
3029 static inline bool classof(const Value *V) {
3030 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3034 //===----------------------------------------------------------------------===//
3035 // PtrToIntInst Class
3036 //===----------------------------------------------------------------------===//
3038 /// @brief This class represents a cast from a pointer to an integer
3039 class PtrToIntInst : public CastInst {
3041 /// @brief Constructor with insert-before-instruction semantics
3043 Value *S, ///< The value to be converted
3044 const Type *Ty, ///< The type to convert to
3045 const Twine &NameStr = "", ///< A name for the new instruction
3046 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3049 /// @brief Constructor with insert-at-end-of-block semantics
3051 Value *S, ///< The value to be converted
3052 const Type *Ty, ///< The type to convert to
3053 const Twine &NameStr, ///< A name for the new instruction
3054 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3057 /// @brief Clone an identical PtrToIntInst
3058 virtual PtrToIntInst *clone() const;
3060 // Methods for support type inquiry through isa, cast, and dyn_cast:
3061 static inline bool classof(const PtrToIntInst *) { return true; }
3062 static inline bool classof(const Instruction *I) {
3063 return I->getOpcode() == PtrToInt;
3065 static inline bool classof(const Value *V) {
3066 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3070 //===----------------------------------------------------------------------===//
3071 // BitCastInst Class
3072 //===----------------------------------------------------------------------===//
3074 /// @brief This class represents a no-op cast from one type to another.
3075 class BitCastInst : public CastInst {
3077 /// @brief Constructor with insert-before-instruction semantics
3079 Value *S, ///< The value to be casted
3080 const Type *Ty, ///< The type to casted to
3081 const Twine &NameStr = "", ///< A name for the new instruction
3082 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3085 /// @brief Constructor with insert-at-end-of-block semantics
3087 Value *S, ///< The value to be casted
3088 const Type *Ty, ///< The type to casted to
3089 const Twine &NameStr, ///< A name for the new instruction
3090 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3093 /// @brief Clone an identical BitCastInst
3094 virtual BitCastInst *clone() const;
3096 // Methods for support type inquiry through isa, cast, and dyn_cast:
3097 static inline bool classof(const BitCastInst *) { return true; }
3098 static inline bool classof(const Instruction *I) {
3099 return I->getOpcode() == BitCast;
3101 static inline bool classof(const Value *V) {
3102 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3106 } // End llvm namespace