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/ADT/SmallVector.h"
32 //===----------------------------------------------------------------------===//
33 // AllocationInst Class
34 //===----------------------------------------------------------------------===//
36 /// AllocationInst - This class is the common base class of MallocInst and
39 class AllocationInst : public UnaryInstruction {
41 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
42 const std::string &Name = "", Instruction *InsertBefore = 0);
43 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
44 const std::string &Name, BasicBlock *InsertAtEnd);
46 // Out of line virtual method, so the vtable, etc. has a home.
47 virtual ~AllocationInst();
49 /// isArrayAllocation - Return true if there is an allocation size parameter
50 /// to the allocation instruction that is not 1.
52 bool isArrayAllocation() const;
54 /// getArraySize - Get the number of element allocated, for a simple
55 /// allocation of a single element, this will return a constant 1 value.
57 const Value *getArraySize() const { return getOperand(0); }
58 Value *getArraySize() { return getOperand(0); }
60 /// getType - Overload to return most specific pointer type
62 const PointerType *getType() const {
63 return reinterpret_cast<const PointerType*>(Instruction::getType());
66 /// getAllocatedType - Return the type that is being allocated by the
69 const Type *getAllocatedType() const;
71 /// getAlignment - Return the alignment of the memory that is being allocated
72 /// by the instruction.
74 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
75 void setAlignment(unsigned Align);
77 virtual Instruction *clone() const = 0;
79 // Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const AllocationInst *) { return true; }
81 static inline bool classof(const Instruction *I) {
82 return I->getOpcode() == Instruction::Alloca ||
83 I->getOpcode() == Instruction::Malloc;
85 static inline bool classof(const Value *V) {
86 return isa<Instruction>(V) && classof(cast<Instruction>(V));
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
95 /// MallocInst - an instruction to allocated memory on the heap
97 class MallocInst : public AllocationInst {
98 MallocInst(const MallocInst &MI);
100 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
101 const std::string &NameStr = "",
102 Instruction *InsertBefore = 0)
103 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertBefore) {}
104 MallocInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
105 BasicBlock *InsertAtEnd)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
108 MallocInst(const Type *Ty, const std::string &NameStr,
109 Instruction *InsertBefore = 0)
110 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
111 MallocInst(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
114 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
115 const std::string &NameStr, BasicBlock *InsertAtEnd)
116 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertAtEnd) {}
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
118 const std::string &NameStr = "",
119 Instruction *InsertBefore = 0)
120 : AllocationInst(Ty, ArraySize, Malloc, Align, NameStr, InsertBefore) {}
122 virtual MallocInst *clone() const;
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const MallocInst *) { return true; }
126 static inline bool classof(const Instruction *I) {
127 return (I->getOpcode() == Instruction::Malloc);
129 static inline bool classof(const Value *V) {
130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
135 //===----------------------------------------------------------------------===//
137 //===----------------------------------------------------------------------===//
139 /// AllocaInst - an instruction to allocate memory on the stack
141 class AllocaInst : public AllocationInst {
142 AllocaInst(const AllocaInst &);
144 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
145 const std::string &NameStr = "",
146 Instruction *InsertBefore = 0)
147 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertBefore) {}
148 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &NameStr,
149 BasicBlock *InsertAtEnd)
150 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
152 AllocaInst(const Type *Ty, const std::string &NameStr,
153 Instruction *InsertBefore = 0)
154 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
155 AllocaInst(const Type *Ty, const std::string &NameStr,
156 BasicBlock *InsertAtEnd)
157 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
159 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
160 const std::string &NameStr = "", Instruction *InsertBefore = 0)
161 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertBefore) {}
162 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
163 const std::string &NameStr, BasicBlock *InsertAtEnd)
164 : AllocationInst(Ty, ArraySize, Alloca, Align, NameStr, InsertAtEnd) {}
166 virtual AllocaInst *clone() const;
168 /// isStaticAlloca - Return true if this alloca is in the entry block of the
169 /// function and is a constant size. If so, the code generator will fold it
170 /// into the prolog/epilog code, so it is basically free.
171 bool isStaticAlloca() const;
173 // Methods for support type inquiry through isa, cast, and dyn_cast:
174 static inline bool classof(const AllocaInst *) { return true; }
175 static inline bool classof(const Instruction *I) {
176 return (I->getOpcode() == Instruction::Alloca);
178 static inline bool classof(const Value *V) {
179 return isa<Instruction>(V) && classof(cast<Instruction>(V));
184 //===----------------------------------------------------------------------===//
186 //===----------------------------------------------------------------------===//
188 /// FreeInst - an instruction to deallocate memory
190 class FreeInst : public UnaryInstruction {
193 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
194 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
196 virtual FreeInst *clone() const;
198 // Accessor methods for consistency with other memory operations
199 Value *getPointerOperand() { return getOperand(0); }
200 const Value *getPointerOperand() const { return getOperand(0); }
202 // Methods for support type inquiry through isa, cast, and dyn_cast:
203 static inline bool classof(const FreeInst *) { return true; }
204 static inline bool classof(const Instruction *I) {
205 return (I->getOpcode() == Instruction::Free);
207 static inline bool classof(const Value *V) {
208 return isa<Instruction>(V) && classof(cast<Instruction>(V));
213 //===----------------------------------------------------------------------===//
215 //===----------------------------------------------------------------------===//
217 /// LoadInst - an instruction for reading from memory. This uses the
218 /// SubclassData field in Value to store whether or not the load is volatile.
220 class LoadInst : public UnaryInstruction {
222 LoadInst(const LoadInst &LI)
223 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
224 setVolatile(LI.isVolatile());
225 setAlignment(LI.getAlignment());
233 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
234 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
235 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
236 Instruction *InsertBefore = 0);
237 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
238 unsigned Align, Instruction *InsertBefore = 0);
239 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
240 BasicBlock *InsertAtEnd);
241 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
242 unsigned Align, BasicBlock *InsertAtEnd);
244 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
245 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
246 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
247 bool isVolatile = false, Instruction *InsertBefore = 0);
248 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
249 BasicBlock *InsertAtEnd);
251 /// isVolatile - Return true if this is a load from a volatile memory
254 bool isVolatile() const { return SubclassData & 1; }
256 /// setVolatile - Specify whether this is a volatile load or not.
258 void setVolatile(bool V) {
259 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
262 virtual LoadInst *clone() const;
264 /// getAlignment - Return the alignment of the access that is being performed
266 unsigned getAlignment() const {
267 return (1 << (SubclassData>>1)) >> 1;
270 void setAlignment(unsigned Align);
272 Value *getPointerOperand() { return getOperand(0); }
273 const Value *getPointerOperand() const { return getOperand(0); }
274 static unsigned getPointerOperandIndex() { return 0U; }
276 // Methods for support type inquiry through isa, cast, and dyn_cast:
277 static inline bool classof(const LoadInst *) { return true; }
278 static inline bool classof(const Instruction *I) {
279 return I->getOpcode() == Instruction::Load;
281 static inline bool classof(const Value *V) {
282 return isa<Instruction>(V) && classof(cast<Instruction>(V));
287 //===----------------------------------------------------------------------===//
289 //===----------------------------------------------------------------------===//
291 /// StoreInst - an instruction for storing to memory
293 class StoreInst : public Instruction {
294 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
296 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
298 Op<0>() = SI.Op<0>();
299 Op<1>() = SI.Op<1>();
300 setVolatile(SI.isVolatile());
301 setAlignment(SI.getAlignment());
309 // allocate space for exactly two operands
310 void *operator new(size_t s) {
311 return User::operator new(s, 2);
313 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
314 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
315 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
316 Instruction *InsertBefore = 0);
317 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
318 unsigned Align, Instruction *InsertBefore = 0);
319 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
320 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
321 unsigned Align, BasicBlock *InsertAtEnd);
324 /// isVolatile - Return true if this is a load from a volatile memory
327 bool isVolatile() const { return SubclassData & 1; }
329 /// setVolatile - Specify whether this is a volatile load or not.
331 void setVolatile(bool V) {
332 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
335 /// Transparently provide more efficient getOperand methods.
336 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
338 /// getAlignment - Return the alignment of the access that is being performed
340 unsigned getAlignment() const {
341 return (1 << (SubclassData>>1)) >> 1;
344 void setAlignment(unsigned Align);
346 virtual StoreInst *clone() const;
348 Value *getPointerOperand() { return getOperand(1); }
349 const Value *getPointerOperand() const { return getOperand(1); }
350 static unsigned getPointerOperandIndex() { return 1U; }
352 // Methods for support type inquiry through isa, cast, and dyn_cast:
353 static inline bool classof(const StoreInst *) { return true; }
354 static inline bool classof(const Instruction *I) {
355 return I->getOpcode() == Instruction::Store;
357 static inline bool classof(const Value *V) {
358 return isa<Instruction>(V) && classof(cast<Instruction>(V));
363 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
366 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
368 //===----------------------------------------------------------------------===//
369 // GetElementPtrInst Class
370 //===----------------------------------------------------------------------===//
372 // checkType - Simple wrapper function to give a better assertion failure
373 // message on bad indexes for a gep instruction.
375 static inline const Type *checkType(const Type *Ty) {
376 assert(Ty && "Invalid GetElementPtrInst indices for type!");
380 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
381 /// access elements of arrays and structs
383 class GetElementPtrInst : public Instruction {
384 GetElementPtrInst(const GetElementPtrInst &GEPI);
385 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
386 const std::string &NameStr);
387 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
389 template<typename InputIterator>
390 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
391 const std::string &NameStr,
392 // This argument ensures that we have an iterator we can
393 // do arithmetic on in constant time
394 std::random_access_iterator_tag) {
395 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
398 // This requires that the iterator points to contiguous memory.
399 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
400 // we have to build an array here
403 init(Ptr, 0, NumIdx, NameStr);
407 /// getIndexedType - Returns the type of the element that would be loaded with
408 /// a load instruction with the specified parameters.
410 /// Null is returned if the indices are invalid for the specified
413 template<typename InputIterator>
414 static const Type *getIndexedType(const Type *Ptr,
415 InputIterator IdxBegin,
416 InputIterator IdxEnd,
417 // This argument ensures that we
418 // have an iterator we can do
419 // arithmetic on in constant time
420 std::random_access_iterator_tag) {
421 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
424 // This requires that the iterator points to contiguous memory.
425 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
427 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
430 /// Constructors - Create a getelementptr instruction with a base pointer an
431 /// list of indices. The first ctor can optionally insert before an existing
432 /// instruction, the second appends the new instruction to the specified
434 template<typename InputIterator>
435 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
436 InputIterator IdxEnd,
438 const std::string &NameStr,
439 Instruction *InsertBefore);
440 template<typename InputIterator>
441 inline GetElementPtrInst(Value *Ptr,
442 InputIterator IdxBegin, InputIterator IdxEnd,
444 const std::string &NameStr, BasicBlock *InsertAtEnd);
446 /// Constructors - These two constructors are convenience methods because one
447 /// and two index getelementptr instructions are so common.
448 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
449 Instruction *InsertBefore = 0);
450 GetElementPtrInst(Value *Ptr, Value *Idx,
451 const std::string &NameStr, BasicBlock *InsertAtEnd);
453 template<typename InputIterator>
454 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
455 InputIterator IdxEnd,
456 const std::string &NameStr = "",
457 Instruction *InsertBefore = 0) {
458 typename std::iterator_traits<InputIterator>::difference_type Values =
459 1 + std::distance(IdxBegin, IdxEnd);
461 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
463 template<typename InputIterator>
464 static GetElementPtrInst *Create(Value *Ptr,
465 InputIterator IdxBegin, InputIterator IdxEnd,
466 const std::string &NameStr,
467 BasicBlock *InsertAtEnd) {
468 typename std::iterator_traits<InputIterator>::difference_type Values =
469 1 + std::distance(IdxBegin, IdxEnd);
471 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
474 /// Constructors - These two creators are convenience methods because one
475 /// index getelementptr instructions are so common.
476 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
477 const std::string &NameStr = "",
478 Instruction *InsertBefore = 0) {
479 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
481 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
482 const std::string &NameStr,
483 BasicBlock *InsertAtEnd) {
484 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
487 virtual GetElementPtrInst *clone() const;
489 /// Transparently provide more efficient getOperand methods.
490 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
492 // getType - Overload to return most specific pointer type...
493 const PointerType *getType() const {
494 return reinterpret_cast<const PointerType*>(Instruction::getType());
497 /// getIndexedType - Returns the type of the element that would be loaded with
498 /// a load instruction with the specified parameters.
500 /// Null is returned if the indices are invalid for the specified
503 template<typename InputIterator>
504 static const Type *getIndexedType(const Type *Ptr,
505 InputIterator IdxBegin,
506 InputIterator IdxEnd) {
507 return getIndexedType(Ptr, IdxBegin, IdxEnd,
508 typename std::iterator_traits<InputIterator>::
509 iterator_category());
512 static const Type *getIndexedType(const Type *Ptr,
513 Value* const *Idx, unsigned NumIdx);
515 static const Type *getIndexedType(const Type *Ptr,
516 uint64_t const *Idx, unsigned NumIdx);
518 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
520 inline op_iterator idx_begin() { return op_begin()+1; }
521 inline const_op_iterator idx_begin() const { return op_begin()+1; }
522 inline op_iterator idx_end() { return op_end(); }
523 inline const_op_iterator idx_end() const { return op_end(); }
525 Value *getPointerOperand() {
526 return getOperand(0);
528 const Value *getPointerOperand() const {
529 return getOperand(0);
531 static unsigned getPointerOperandIndex() {
532 return 0U; // get index for modifying correct operand
535 unsigned getNumIndices() const { // Note: always non-negative
536 return getNumOperands() - 1;
539 bool hasIndices() const {
540 return getNumOperands() > 1;
543 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
544 /// zeros. If so, the result pointer and the first operand have the same
545 /// value, just potentially different types.
546 bool hasAllZeroIndices() const;
548 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
549 /// constant integers. If so, the result pointer and the first operand have
550 /// a constant offset between them.
551 bool hasAllConstantIndices() const;
554 // Methods for support type inquiry through isa, cast, and dyn_cast:
555 static inline bool classof(const GetElementPtrInst *) { return true; }
556 static inline bool classof(const Instruction *I) {
557 return (I->getOpcode() == Instruction::GetElementPtr);
559 static inline bool classof(const Value *V) {
560 return isa<Instruction>(V) && classof(cast<Instruction>(V));
565 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
568 template<typename InputIterator>
569 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
570 InputIterator IdxBegin,
571 InputIterator IdxEnd,
573 const std::string &NameStr,
574 Instruction *InsertBefore)
575 : Instruction(PointerType::get(checkType(
576 getIndexedType(Ptr->getType(),
578 cast<PointerType>(Ptr->getType())
579 ->getAddressSpace()),
581 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
582 Values, InsertBefore) {
583 init(Ptr, IdxBegin, IdxEnd, NameStr,
584 typename std::iterator_traits<InputIterator>::iterator_category());
586 template<typename InputIterator>
587 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
588 InputIterator IdxBegin,
589 InputIterator IdxEnd,
591 const std::string &NameStr,
592 BasicBlock *InsertAtEnd)
593 : Instruction(PointerType::get(checkType(
594 getIndexedType(Ptr->getType(),
596 cast<PointerType>(Ptr->getType())
597 ->getAddressSpace()),
599 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
600 Values, InsertAtEnd) {
601 init(Ptr, IdxBegin, IdxEnd, NameStr,
602 typename std::iterator_traits<InputIterator>::iterator_category());
606 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
609 //===----------------------------------------------------------------------===//
611 //===----------------------------------------------------------------------===//
613 /// This instruction compares its operands according to the predicate given
614 /// to the constructor. It only operates on integers or pointers. The operands
615 /// must be identical types.
616 /// @brief Represent an integer comparison operator.
617 class ICmpInst: public CmpInst {
619 /// @brief Constructor with insert-before-instruction semantics.
621 Predicate pred, ///< The predicate to use for the comparison
622 Value *LHS, ///< The left-hand-side of the expression
623 Value *RHS, ///< The right-hand-side of the expression
624 const std::string &NameStr = "", ///< Name of the instruction
625 Instruction *InsertBefore = 0 ///< Where to insert
626 ) : CmpInst(makeCmpResultType(LHS->getType()),
627 Instruction::ICmp, pred, LHS, RHS, NameStr,
629 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
630 pred <= CmpInst::LAST_ICMP_PREDICATE &&
631 "Invalid ICmp predicate value");
632 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
633 "Both operands to ICmp instruction are not of the same type!");
634 // Check that the operands are the right type
635 assert((getOperand(0)->getType()->isIntOrIntVector() ||
636 isa<PointerType>(getOperand(0)->getType())) &&
637 "Invalid operand types for ICmp instruction");
640 /// @brief Constructor with insert-at-block-end semantics.
642 Predicate pred, ///< The predicate to use for the comparison
643 Value *LHS, ///< The left-hand-side of the expression
644 Value *RHS, ///< The right-hand-side of the expression
645 const std::string &NameStr, ///< Name of the instruction
646 BasicBlock *InsertAtEnd ///< Block to insert into.
647 ) : CmpInst(makeCmpResultType(LHS->getType()),
648 Instruction::ICmp, pred, LHS, RHS, NameStr,
650 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
651 pred <= CmpInst::LAST_ICMP_PREDICATE &&
652 "Invalid ICmp predicate value");
653 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
654 "Both operands to ICmp instruction are not of the same type!");
655 // Check that the operands are the right type
656 assert((getOperand(0)->getType()->isIntOrIntVector() ||
657 isa<PointerType>(getOperand(0)->getType())) &&
658 "Invalid operand types for ICmp instruction");
661 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
662 /// @returns the predicate that would be the result if the operand were
663 /// regarded as signed.
664 /// @brief Return the signed version of the predicate
665 Predicate getSignedPredicate() const {
666 return getSignedPredicate(getPredicate());
669 /// This is a static version that you can use without an instruction.
670 /// @brief Return the signed version of the predicate.
671 static Predicate getSignedPredicate(Predicate pred);
673 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
674 /// @returns the predicate that would be the result if the operand were
675 /// regarded as unsigned.
676 /// @brief Return the unsigned version of the predicate
677 Predicate getUnsignedPredicate() const {
678 return getUnsignedPredicate(getPredicate());
681 /// This is a static version that you can use without an instruction.
682 /// @brief Return the unsigned version of the predicate.
683 static Predicate getUnsignedPredicate(Predicate pred);
685 /// isEquality - Return true if this predicate is either EQ or NE. This also
686 /// tests for commutativity.
687 static bool isEquality(Predicate P) {
688 return P == ICMP_EQ || P == ICMP_NE;
691 /// isEquality - Return true if this predicate is either EQ or NE. This also
692 /// tests for commutativity.
693 bool isEquality() const {
694 return isEquality(getPredicate());
697 /// @returns true if the predicate of this ICmpInst is commutative
698 /// @brief Determine if this relation is commutative.
699 bool isCommutative() const { return isEquality(); }
701 /// isRelational - Return true if the predicate is relational (not EQ or NE).
703 bool isRelational() const {
704 return !isEquality();
707 /// isRelational - Return true if the predicate is relational (not EQ or NE).
709 static bool isRelational(Predicate P) {
710 return !isEquality(P);
713 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
714 /// @brief Determine if this instruction's predicate is signed.
715 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
717 /// @returns true if the predicate provided is signed, false otherwise
718 /// @brief Determine if the predicate is signed.
719 static bool isSignedPredicate(Predicate pred);
721 /// @returns true if the specified compare predicate is
722 /// true when both operands are equal...
723 /// @brief Determine if the icmp is true when both operands are equal
724 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
725 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
726 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
727 pred == ICmpInst::ICMP_SLE;
730 /// @returns true if the specified compare instruction is
731 /// true when both operands are equal...
732 /// @brief Determine if the ICmpInst returns true when both operands are equal
733 bool isTrueWhenEqual() {
734 return isTrueWhenEqual(getPredicate());
737 /// Initialize a set of values that all satisfy the predicate with C.
738 /// @brief Make a ConstantRange for a relation with a constant value.
739 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
741 /// Exchange the two operands to this instruction in such a way that it does
742 /// not modify the semantics of the instruction. The predicate value may be
743 /// changed to retain the same result if the predicate is order dependent
745 /// @brief Swap operands and adjust predicate.
746 void swapOperands() {
747 SubclassData = getSwappedPredicate();
748 Op<0>().swap(Op<1>());
751 virtual ICmpInst *clone() const;
753 // Methods for support type inquiry through isa, cast, and dyn_cast:
754 static inline bool classof(const ICmpInst *) { return true; }
755 static inline bool classof(const Instruction *I) {
756 return I->getOpcode() == Instruction::ICmp;
758 static inline bool classof(const Value *V) {
759 return isa<Instruction>(V) && classof(cast<Instruction>(V));
764 //===----------------------------------------------------------------------===//
766 //===----------------------------------------------------------------------===//
768 /// This instruction compares its operands according to the predicate given
769 /// to the constructor. It only operates on floating point values or packed
770 /// vectors of floating point values. The operands must be identical types.
771 /// @brief Represents a floating point comparison operator.
772 class FCmpInst: public CmpInst {
774 /// @brief Constructor with insert-before-instruction semantics.
776 Predicate pred, ///< The predicate to use for the comparison
777 Value *LHS, ///< The left-hand-side of the expression
778 Value *RHS, ///< The right-hand-side of the expression
779 const std::string &NameStr = "", ///< Name of the instruction
780 Instruction *InsertBefore = 0 ///< Where to insert
781 ) : CmpInst(makeCmpResultType(LHS->getType()),
782 Instruction::FCmp, pred, LHS, RHS, NameStr,
784 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
785 "Invalid FCmp predicate value");
786 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
787 "Both operands to FCmp instruction are not of the same type!");
788 // Check that the operands are the right type
789 assert(getOperand(0)->getType()->isFPOrFPVector() &&
790 "Invalid operand types for FCmp instruction");
793 /// @brief Constructor with insert-at-block-end semantics.
795 Predicate pred, ///< The predicate to use for the comparison
796 Value *LHS, ///< The left-hand-side of the expression
797 Value *RHS, ///< The right-hand-side of the expression
798 const std::string &NameStr, ///< Name of the instruction
799 BasicBlock *InsertAtEnd ///< Block to insert into.
800 ) : CmpInst(makeCmpResultType(LHS->getType()),
801 Instruction::FCmp, pred, LHS, RHS, NameStr,
803 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
804 "Invalid FCmp predicate value");
805 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
806 "Both operands to FCmp instruction are not of the same type!");
807 // Check that the operands are the right type
808 assert(getOperand(0)->getType()->isFPOrFPVector() &&
809 "Invalid operand types for FCmp instruction");
812 /// @returns true if the predicate of this instruction is EQ or NE.
813 /// @brief Determine if this is an equality predicate.
814 bool isEquality() const {
815 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
816 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
819 /// @returns true if the predicate of this instruction is commutative.
820 /// @brief Determine if this is a commutative predicate.
821 bool isCommutative() const {
822 return isEquality() ||
823 SubclassData == FCMP_FALSE ||
824 SubclassData == FCMP_TRUE ||
825 SubclassData == FCMP_ORD ||
826 SubclassData == FCMP_UNO;
829 /// @returns true if the predicate is relational (not EQ or NE).
830 /// @brief Determine if this a relational predicate.
831 bool isRelational() const { return !isEquality(); }
833 /// Exchange the two operands to this instruction in such a way that it does
834 /// not modify the semantics of the instruction. The predicate value may be
835 /// changed to retain the same result if the predicate is order dependent
837 /// @brief Swap operands and adjust predicate.
838 void swapOperands() {
839 SubclassData = getSwappedPredicate();
840 Op<0>().swap(Op<1>());
843 virtual FCmpInst *clone() const;
845 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
846 static inline bool classof(const FCmpInst *) { return true; }
847 static inline bool classof(const Instruction *I) {
848 return I->getOpcode() == Instruction::FCmp;
850 static inline bool classof(const Value *V) {
851 return isa<Instruction>(V) && classof(cast<Instruction>(V));
856 //===----------------------------------------------------------------------===//
858 //===----------------------------------------------------------------------===//
860 /// This instruction compares its operands according to the predicate given
861 /// to the constructor. It only operates on vectors of integers.
862 /// The operands must be identical types.
863 /// @brief Represents a vector integer comparison operator.
864 class VICmpInst: public CmpInst {
866 /// @brief Constructor with insert-before-instruction semantics.
868 Predicate pred, ///< The predicate to use for the comparison
869 Value *LHS, ///< The left-hand-side of the expression
870 Value *RHS, ///< The right-hand-side of the expression
871 const std::string &NameStr = "", ///< Name of the instruction
872 Instruction *InsertBefore = 0 ///< Where to insert
873 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
875 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
876 pred <= CmpInst::LAST_ICMP_PREDICATE &&
877 "Invalid VICmp predicate value");
878 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
879 "Both operands to VICmp instruction are not of the same type!");
882 /// @brief Constructor with insert-at-block-end semantics.
884 Predicate pred, ///< The predicate to use for the comparison
885 Value *LHS, ///< The left-hand-side of the expression
886 Value *RHS, ///< The right-hand-side of the expression
887 const std::string &NameStr, ///< Name of the instruction
888 BasicBlock *InsertAtEnd ///< Block to insert into.
889 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
891 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
892 pred <= CmpInst::LAST_ICMP_PREDICATE &&
893 "Invalid VICmp predicate value");
894 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
895 "Both operands to VICmp instruction are not of the same type!");
898 /// @brief Return the predicate for this instruction.
899 Predicate getPredicate() const { return Predicate(SubclassData); }
901 virtual VICmpInst *clone() const;
903 // Methods for support type inquiry through isa, cast, and dyn_cast:
904 static inline bool classof(const VICmpInst *) { return true; }
905 static inline bool classof(const Instruction *I) {
906 return I->getOpcode() == Instruction::VICmp;
908 static inline bool classof(const Value *V) {
909 return isa<Instruction>(V) && classof(cast<Instruction>(V));
913 //===----------------------------------------------------------------------===//
915 //===----------------------------------------------------------------------===//
917 /// This instruction compares its operands according to the predicate given
918 /// to the constructor. It only operates on vectors of floating point values.
919 /// The operands must be identical types.
920 /// @brief Represents a vector floating point comparison operator.
921 class VFCmpInst: public CmpInst {
923 /// @brief Constructor with insert-before-instruction semantics.
925 Predicate pred, ///< The predicate to use for the comparison
926 Value *LHS, ///< The left-hand-side of the expression
927 Value *RHS, ///< The right-hand-side of the expression
928 const std::string &NameStr = "", ///< Name of the instruction
929 Instruction *InsertBefore = 0 ///< Where to insert
930 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
931 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertBefore) {
932 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
933 "Invalid VFCmp predicate value");
934 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
935 "Both operands to VFCmp instruction are not of the same type!");
938 /// @brief Constructor with insert-at-block-end semantics.
940 Predicate pred, ///< The predicate to use for the comparison
941 Value *LHS, ///< The left-hand-side of the expression
942 Value *RHS, ///< The right-hand-side of the expression
943 const std::string &NameStr, ///< Name of the instruction
944 BasicBlock *InsertAtEnd ///< Block to insert into.
945 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
946 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertAtEnd) {
947 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
948 "Invalid VFCmp predicate value");
949 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
950 "Both operands to VFCmp instruction are not of the same type!");
953 /// @brief Return the predicate for this instruction.
954 Predicate getPredicate() const { return Predicate(SubclassData); }
956 virtual VFCmpInst *clone() const;
958 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
959 static inline bool classof(const VFCmpInst *) { return true; }
960 static inline bool classof(const Instruction *I) {
961 return I->getOpcode() == Instruction::VFCmp;
963 static inline bool classof(const Value *V) {
964 return isa<Instruction>(V) && classof(cast<Instruction>(V));
968 //===----------------------------------------------------------------------===//
970 //===----------------------------------------------------------------------===//
971 /// CallInst - This class represents a function call, abstracting a target
972 /// machine's calling convention. This class uses low bit of the SubClassData
973 /// field to indicate whether or not this is a tail call. The rest of the bits
974 /// hold the calling convention of the call.
977 class CallInst : public Instruction {
978 AttrListPtr AttributeList; ///< parameter attributes for call
979 CallInst(const CallInst &CI);
980 void init(Value *Func, Value* const *Params, unsigned NumParams);
981 void init(Value *Func, Value *Actual1, Value *Actual2);
982 void init(Value *Func, Value *Actual);
983 void init(Value *Func);
985 template<typename InputIterator>
986 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
987 const std::string &NameStr,
988 // This argument ensures that we have an iterator we can
989 // do arithmetic on in constant time
990 std::random_access_iterator_tag) {
991 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
993 // This requires that the iterator points to contiguous memory.
994 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
998 /// Construct a CallInst given a range of arguments. InputIterator
999 /// must be a random-access iterator pointing to contiguous storage
1000 /// (e.g. a std::vector<>::iterator). Checks are made for
1001 /// random-accessness but not for contiguous storage as that would
1002 /// incur runtime overhead.
1003 /// @brief Construct a CallInst from a range of arguments
1004 template<typename InputIterator>
1005 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1006 const std::string &NameStr, Instruction *InsertBefore);
1008 /// Construct a CallInst given a range of arguments. InputIterator
1009 /// must be a random-access iterator pointing to contiguous storage
1010 /// (e.g. a std::vector<>::iterator). Checks are made for
1011 /// random-accessness but not for contiguous storage as that would
1012 /// incur runtime overhead.
1013 /// @brief Construct a CallInst from a range of arguments
1014 template<typename InputIterator>
1015 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1016 const std::string &NameStr, BasicBlock *InsertAtEnd);
1018 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1019 Instruction *InsertBefore);
1020 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1021 BasicBlock *InsertAtEnd);
1022 explicit CallInst(Value *F, const std::string &NameStr,
1023 Instruction *InsertBefore);
1024 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
1026 template<typename InputIterator>
1027 static CallInst *Create(Value *Func,
1028 InputIterator ArgBegin, InputIterator ArgEnd,
1029 const std::string &NameStr = "",
1030 Instruction *InsertBefore = 0) {
1031 return new((unsigned)(ArgEnd - ArgBegin + 1))
1032 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1034 template<typename InputIterator>
1035 static CallInst *Create(Value *Func,
1036 InputIterator ArgBegin, InputIterator ArgEnd,
1037 const std::string &NameStr, BasicBlock *InsertAtEnd) {
1038 return new((unsigned)(ArgEnd - ArgBegin + 1))
1039 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1041 static CallInst *Create(Value *F, Value *Actual,
1042 const std::string& NameStr = "",
1043 Instruction *InsertBefore = 0) {
1044 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1046 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
1047 BasicBlock *InsertAtEnd) {
1048 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1050 static CallInst *Create(Value *F, const std::string &NameStr = "",
1051 Instruction *InsertBefore = 0) {
1052 return new(1) CallInst(F, NameStr, InsertBefore);
1054 static CallInst *Create(Value *F, const std::string &NameStr,
1055 BasicBlock *InsertAtEnd) {
1056 return new(1) CallInst(F, NameStr, InsertAtEnd);
1061 bool isTailCall() const { return SubclassData & 1; }
1062 void setTailCall(bool isTC = true) {
1063 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1066 virtual CallInst *clone() const;
1068 /// Provide fast operand accessors
1069 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1071 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1073 unsigned getCallingConv() const { return SubclassData >> 1; }
1074 void setCallingConv(unsigned CC) {
1075 SubclassData = (SubclassData & 1) | (CC << 1);
1078 /// getAttributes - Return the parameter attributes for this call.
1080 const AttrListPtr &getAttributes() const { return AttributeList; }
1082 /// setAttributes - Set the parameter attributes for this call.
1084 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1086 /// addAttribute - adds the attribute to the list of attributes.
1087 void addAttribute(unsigned i, Attributes attr);
1089 /// removeAttribute - removes the attribute from the list of attributes.
1090 void removeAttribute(unsigned i, Attributes attr);
1092 /// @brief Determine whether the call or the callee has the given attribute.
1093 bool paramHasAttr(unsigned i, Attributes attr) const;
1095 /// @brief Extract the alignment for a call or parameter (0=unknown).
1096 unsigned getParamAlignment(unsigned i) const {
1097 return AttributeList.getParamAlignment(i);
1100 /// @brief Determine if the call does not access memory.
1101 bool doesNotAccessMemory() const {
1102 return paramHasAttr(~0, Attribute::ReadNone);
1104 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1105 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1106 else removeAttribute(~0, Attribute::ReadNone);
1109 /// @brief Determine if the call does not access or only reads memory.
1110 bool onlyReadsMemory() const {
1111 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1113 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1114 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1115 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1118 /// @brief Determine if the call cannot return.
1119 bool doesNotReturn() const {
1120 return paramHasAttr(~0, Attribute::NoReturn);
1122 void setDoesNotReturn(bool DoesNotReturn = true) {
1123 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1124 else removeAttribute(~0, Attribute::NoReturn);
1127 /// @brief Determine if the call cannot unwind.
1128 bool doesNotThrow() const {
1129 return paramHasAttr(~0, Attribute::NoUnwind);
1131 void setDoesNotThrow(bool DoesNotThrow = true) {
1132 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1133 else removeAttribute(~0, Attribute::NoUnwind);
1136 /// @brief Determine if the call returns a structure through first
1137 /// pointer argument.
1138 bool hasStructRetAttr() const {
1139 // Be friendly and also check the callee.
1140 return paramHasAttr(1, Attribute::StructRet);
1143 /// @brief Determine if any call argument is an aggregate passed by value.
1144 bool hasByValArgument() const {
1145 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1148 /// getCalledFunction - Return the function called, or null if this is an
1149 /// indirect function invocation.
1151 Function *getCalledFunction() const {
1152 return dyn_cast<Function>(getOperand(0));
1155 /// getCalledValue - Get a pointer to the function that is invoked by this
1157 const Value *getCalledValue() const { return getOperand(0); }
1158 Value *getCalledValue() { return getOperand(0); }
1160 // Methods for support type inquiry through isa, cast, and dyn_cast:
1161 static inline bool classof(const CallInst *) { return true; }
1162 static inline bool classof(const Instruction *I) {
1163 return I->getOpcode() == Instruction::Call;
1165 static inline bool classof(const Value *V) {
1166 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1171 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1174 template<typename InputIterator>
1175 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1176 const std::string &NameStr, BasicBlock *InsertAtEnd)
1177 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1178 ->getElementType())->getReturnType(),
1180 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1181 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1182 init(Func, ArgBegin, ArgEnd, NameStr,
1183 typename std::iterator_traits<InputIterator>::iterator_category());
1186 template<typename InputIterator>
1187 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1188 const std::string &NameStr, Instruction *InsertBefore)
1189 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1190 ->getElementType())->getReturnType(),
1192 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1193 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1194 init(Func, ArgBegin, ArgEnd, NameStr,
1195 typename std::iterator_traits<InputIterator>::iterator_category());
1198 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1200 //===----------------------------------------------------------------------===//
1202 //===----------------------------------------------------------------------===//
1204 /// SelectInst - This class represents the LLVM 'select' instruction.
1206 class SelectInst : public Instruction {
1207 void init(Value *C, Value *S1, Value *S2) {
1208 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1214 SelectInst(const SelectInst &SI)
1215 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1216 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1218 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1219 Instruction *InsertBefore)
1220 : Instruction(S1->getType(), Instruction::Select,
1221 &Op<0>(), 3, InsertBefore) {
1225 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1226 BasicBlock *InsertAtEnd)
1227 : Instruction(S1->getType(), Instruction::Select,
1228 &Op<0>(), 3, InsertAtEnd) {
1233 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1234 const std::string &NameStr = "",
1235 Instruction *InsertBefore = 0) {
1236 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1238 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1239 const std::string &NameStr,
1240 BasicBlock *InsertAtEnd) {
1241 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1244 Value *getCondition() const { return Op<0>(); }
1245 Value *getTrueValue() const { return Op<1>(); }
1246 Value *getFalseValue() const { return Op<2>(); }
1248 /// areInvalidOperands - Return a string if the specified operands are invalid
1249 /// for a select operation, otherwise return null.
1250 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1252 /// Transparently provide more efficient getOperand methods.
1253 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1255 OtherOps getOpcode() const {
1256 return static_cast<OtherOps>(Instruction::getOpcode());
1259 virtual SelectInst *clone() const;
1261 // Methods for support type inquiry through isa, cast, and dyn_cast:
1262 static inline bool classof(const SelectInst *) { return true; }
1263 static inline bool classof(const Instruction *I) {
1264 return I->getOpcode() == Instruction::Select;
1266 static inline bool classof(const Value *V) {
1267 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1272 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1275 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1277 //===----------------------------------------------------------------------===//
1279 //===----------------------------------------------------------------------===//
1281 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1282 /// an argument of the specified type given a va_list and increments that list
1284 class VAArgInst : public UnaryInstruction {
1285 VAArgInst(const VAArgInst &VAA)
1286 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1288 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1289 Instruction *InsertBefore = 0)
1290 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1293 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1294 BasicBlock *InsertAtEnd)
1295 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1299 virtual VAArgInst *clone() const;
1301 // Methods for support type inquiry through isa, cast, and dyn_cast:
1302 static inline bool classof(const VAArgInst *) { return true; }
1303 static inline bool classof(const Instruction *I) {
1304 return I->getOpcode() == VAArg;
1306 static inline bool classof(const Value *V) {
1307 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1311 //===----------------------------------------------------------------------===//
1312 // ExtractElementInst Class
1313 //===----------------------------------------------------------------------===//
1315 /// ExtractElementInst - This instruction extracts a single (scalar)
1316 /// element from a VectorType value
1318 class ExtractElementInst : public Instruction {
1319 ExtractElementInst(const ExtractElementInst &EE) :
1320 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1321 Op<0>() = EE.Op<0>();
1322 Op<1>() = EE.Op<1>();
1326 // allocate space for exactly two operands
1327 void *operator new(size_t s) {
1328 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1330 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1331 Instruction *InsertBefore = 0);
1332 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
1333 Instruction *InsertBefore = 0);
1334 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1335 BasicBlock *InsertAtEnd);
1336 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
1337 BasicBlock *InsertAtEnd);
1339 /// isValidOperands - Return true if an extractelement instruction can be
1340 /// formed with the specified operands.
1341 static bool isValidOperands(const Value *Vec, const Value *Idx);
1343 virtual ExtractElementInst *clone() const;
1345 /// Transparently provide more efficient getOperand methods.
1346 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1348 // Methods for support type inquiry through isa, cast, and dyn_cast:
1349 static inline bool classof(const ExtractElementInst *) { return true; }
1350 static inline bool classof(const Instruction *I) {
1351 return I->getOpcode() == Instruction::ExtractElement;
1353 static inline bool classof(const Value *V) {
1354 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1359 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1362 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1364 //===----------------------------------------------------------------------===//
1365 // InsertElementInst Class
1366 //===----------------------------------------------------------------------===//
1368 /// InsertElementInst - This instruction inserts a single (scalar)
1369 /// element into a VectorType value
1371 class InsertElementInst : public Instruction {
1372 InsertElementInst(const InsertElementInst &IE);
1373 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1374 const std::string &NameStr = "",Instruction *InsertBefore = 0);
1375 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1376 const std::string &NameStr = "",Instruction *InsertBefore = 0);
1377 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1378 const std::string &NameStr, BasicBlock *InsertAtEnd);
1379 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1380 const std::string &NameStr, BasicBlock *InsertAtEnd);
1382 static InsertElementInst *Create(const InsertElementInst &IE) {
1383 return new(IE.getNumOperands()) InsertElementInst(IE);
1385 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1386 const std::string &NameStr = "",
1387 Instruction *InsertBefore = 0) {
1388 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1390 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1391 const std::string &NameStr = "",
1392 Instruction *InsertBefore = 0) {
1393 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1395 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1396 const std::string &NameStr,
1397 BasicBlock *InsertAtEnd) {
1398 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1400 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1401 const std::string &NameStr,
1402 BasicBlock *InsertAtEnd) {
1403 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1406 /// isValidOperands - Return true if an insertelement instruction can be
1407 /// formed with the specified operands.
1408 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1411 virtual InsertElementInst *clone() const;
1413 /// getType - Overload to return most specific vector type.
1415 const VectorType *getType() const {
1416 return reinterpret_cast<const VectorType*>(Instruction::getType());
1419 /// Transparently provide more efficient getOperand methods.
1420 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1422 // Methods for support type inquiry through isa, cast, and dyn_cast:
1423 static inline bool classof(const InsertElementInst *) { return true; }
1424 static inline bool classof(const Instruction *I) {
1425 return I->getOpcode() == Instruction::InsertElement;
1427 static inline bool classof(const Value *V) {
1428 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1433 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1436 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1438 //===----------------------------------------------------------------------===//
1439 // ShuffleVectorInst Class
1440 //===----------------------------------------------------------------------===//
1442 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1445 class ShuffleVectorInst : public Instruction {
1446 ShuffleVectorInst(const ShuffleVectorInst &IE);
1448 // allocate space for exactly three operands
1449 void *operator new(size_t s) {
1450 return User::operator new(s, 3);
1452 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1453 const std::string &NameStr = "",
1454 Instruction *InsertBefor = 0);
1455 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1456 const std::string &NameStr, BasicBlock *InsertAtEnd);
1458 /// isValidOperands - Return true if a shufflevector instruction can be
1459 /// formed with the specified operands.
1460 static bool isValidOperands(const Value *V1, const Value *V2,
1463 virtual ShuffleVectorInst *clone() const;
1465 /// getType - Overload to return most specific vector type.
1467 const VectorType *getType() const {
1468 return reinterpret_cast<const VectorType*>(Instruction::getType());
1471 /// Transparently provide more efficient getOperand methods.
1472 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1474 /// getMaskValue - Return the index from the shuffle mask for the specified
1475 /// output result. This is either -1 if the element is undef or a number less
1476 /// than 2*numelements.
1477 int getMaskValue(unsigned i) const;
1479 // Methods for support type inquiry through isa, cast, and dyn_cast:
1480 static inline bool classof(const ShuffleVectorInst *) { return true; }
1481 static inline bool classof(const Instruction *I) {
1482 return I->getOpcode() == Instruction::ShuffleVector;
1484 static inline bool classof(const Value *V) {
1485 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1490 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1493 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1495 //===----------------------------------------------------------------------===//
1496 // ExtractValueInst Class
1497 //===----------------------------------------------------------------------===//
1499 /// ExtractValueInst - This instruction extracts a struct member or array
1500 /// element value from an aggregate value.
1502 class ExtractValueInst : public UnaryInstruction {
1503 SmallVector<unsigned, 4> Indices;
1505 ExtractValueInst(const ExtractValueInst &EVI);
1506 void init(const unsigned *Idx, unsigned NumIdx,
1507 const std::string &NameStr);
1508 void init(unsigned Idx, const std::string &NameStr);
1510 template<typename InputIterator>
1511 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1512 const std::string &NameStr,
1513 // This argument ensures that we have an iterator we can
1514 // do arithmetic on in constant time
1515 std::random_access_iterator_tag) {
1516 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1518 // There's no fundamental reason why we require at least one index
1519 // (other than weirdness with &*IdxBegin being invalid; see
1520 // getelementptr's init routine for example). But there's no
1521 // present need to support it.
1522 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1524 // This requires that the iterator points to contiguous memory.
1525 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1526 // we have to build an array here
1529 /// getIndexedType - Returns the type of the element that would be extracted
1530 /// with an extractvalue instruction with the specified parameters.
1532 /// Null is returned if the indices are invalid for the specified
1535 static const Type *getIndexedType(const Type *Agg,
1536 const unsigned *Idx, unsigned NumIdx);
1538 template<typename InputIterator>
1539 static const Type *getIndexedType(const Type *Ptr,
1540 InputIterator IdxBegin,
1541 InputIterator IdxEnd,
1542 // This argument ensures that we
1543 // have an iterator we can do
1544 // arithmetic on in constant time
1545 std::random_access_iterator_tag) {
1546 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1549 // This requires that the iterator points to contiguous memory.
1550 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1552 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1555 /// Constructors - Create a extractvalue instruction with a base aggregate
1556 /// value and a list of indices. The first ctor can optionally insert before
1557 /// an existing instruction, the second appends the new instruction to the
1558 /// specified BasicBlock.
1559 template<typename InputIterator>
1560 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1561 InputIterator IdxEnd,
1562 const std::string &NameStr,
1563 Instruction *InsertBefore);
1564 template<typename InputIterator>
1565 inline ExtractValueInst(Value *Agg,
1566 InputIterator IdxBegin, InputIterator IdxEnd,
1567 const std::string &NameStr, BasicBlock *InsertAtEnd);
1569 // allocate space for exactly one operand
1570 void *operator new(size_t s) {
1571 return User::operator new(s, 1);
1575 template<typename InputIterator>
1576 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1577 InputIterator IdxEnd,
1578 const std::string &NameStr = "",
1579 Instruction *InsertBefore = 0) {
1581 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1583 template<typename InputIterator>
1584 static ExtractValueInst *Create(Value *Agg,
1585 InputIterator IdxBegin, InputIterator IdxEnd,
1586 const std::string &NameStr,
1587 BasicBlock *InsertAtEnd) {
1588 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1591 /// Constructors - These two creators are convenience methods because one
1592 /// index extractvalue instructions are much more common than those with
1594 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1595 const std::string &NameStr = "",
1596 Instruction *InsertBefore = 0) {
1597 unsigned Idxs[1] = { Idx };
1598 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1600 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1601 const std::string &NameStr,
1602 BasicBlock *InsertAtEnd) {
1603 unsigned Idxs[1] = { Idx };
1604 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1607 virtual ExtractValueInst *clone() const;
1609 // getType - Overload to return most specific pointer type...
1610 const PointerType *getType() const {
1611 return reinterpret_cast<const PointerType*>(Instruction::getType());
1614 /// getIndexedType - Returns the type of the element that would be extracted
1615 /// with an extractvalue instruction with the specified parameters.
1617 /// Null is returned if the indices are invalid for the specified
1620 template<typename InputIterator>
1621 static const Type *getIndexedType(const Type *Ptr,
1622 InputIterator IdxBegin,
1623 InputIterator IdxEnd) {
1624 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1625 typename std::iterator_traits<InputIterator>::
1626 iterator_category());
1628 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1630 typedef const unsigned* idx_iterator;
1631 inline idx_iterator idx_begin() const { return Indices.begin(); }
1632 inline idx_iterator idx_end() const { return Indices.end(); }
1634 Value *getAggregateOperand() {
1635 return getOperand(0);
1637 const Value *getAggregateOperand() const {
1638 return getOperand(0);
1640 static unsigned getAggregateOperandIndex() {
1641 return 0U; // get index for modifying correct operand
1644 unsigned getNumIndices() const { // Note: always non-negative
1645 return (unsigned)Indices.size();
1648 bool hasIndices() const {
1652 // Methods for support type inquiry through isa, cast, and dyn_cast:
1653 static inline bool classof(const ExtractValueInst *) { return true; }
1654 static inline bool classof(const Instruction *I) {
1655 return I->getOpcode() == Instruction::ExtractValue;
1657 static inline bool classof(const Value *V) {
1658 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1662 template<typename InputIterator>
1663 ExtractValueInst::ExtractValueInst(Value *Agg,
1664 InputIterator IdxBegin,
1665 InputIterator IdxEnd,
1666 const std::string &NameStr,
1667 Instruction *InsertBefore)
1668 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1670 ExtractValue, Agg, InsertBefore) {
1671 init(IdxBegin, IdxEnd, NameStr,
1672 typename std::iterator_traits<InputIterator>::iterator_category());
1674 template<typename InputIterator>
1675 ExtractValueInst::ExtractValueInst(Value *Agg,
1676 InputIterator IdxBegin,
1677 InputIterator IdxEnd,
1678 const std::string &NameStr,
1679 BasicBlock *InsertAtEnd)
1680 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1682 ExtractValue, Agg, InsertAtEnd) {
1683 init(IdxBegin, IdxEnd, NameStr,
1684 typename std::iterator_traits<InputIterator>::iterator_category());
1688 //===----------------------------------------------------------------------===//
1689 // InsertValueInst Class
1690 //===----------------------------------------------------------------------===//
1692 /// InsertValueInst - This instruction inserts a struct field of array element
1693 /// value into an aggregate value.
1695 class InsertValueInst : public Instruction {
1696 SmallVector<unsigned, 4> Indices;
1698 void *operator new(size_t, unsigned); // Do not implement
1699 InsertValueInst(const InsertValueInst &IVI);
1700 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1701 const std::string &NameStr);
1702 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1704 template<typename InputIterator>
1705 void init(Value *Agg, Value *Val,
1706 InputIterator IdxBegin, InputIterator IdxEnd,
1707 const std::string &NameStr,
1708 // This argument ensures that we have an iterator we can
1709 // do arithmetic on in constant time
1710 std::random_access_iterator_tag) {
1711 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1713 // There's no fundamental reason why we require at least one index
1714 // (other than weirdness with &*IdxBegin being invalid; see
1715 // getelementptr's init routine for example). But there's no
1716 // present need to support it.
1717 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1719 // This requires that the iterator points to contiguous memory.
1720 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1721 // we have to build an array here
1724 /// Constructors - Create a insertvalue instruction with a base aggregate
1725 /// value, a value to insert, and a list of indices. The first ctor can
1726 /// optionally insert before an existing instruction, the second appends
1727 /// the new instruction to the specified BasicBlock.
1728 template<typename InputIterator>
1729 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1730 InputIterator IdxEnd,
1731 const std::string &NameStr,
1732 Instruction *InsertBefore);
1733 template<typename InputIterator>
1734 inline InsertValueInst(Value *Agg, Value *Val,
1735 InputIterator IdxBegin, InputIterator IdxEnd,
1736 const std::string &NameStr, BasicBlock *InsertAtEnd);
1738 /// Constructors - These two constructors are convenience methods because one
1739 /// and two index insertvalue instructions are so common.
1740 InsertValueInst(Value *Agg, Value *Val,
1741 unsigned Idx, const std::string &NameStr = "",
1742 Instruction *InsertBefore = 0);
1743 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1744 const std::string &NameStr, BasicBlock *InsertAtEnd);
1746 // allocate space for exactly two operands
1747 void *operator new(size_t s) {
1748 return User::operator new(s, 2);
1751 template<typename InputIterator>
1752 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1753 InputIterator IdxEnd,
1754 const std::string &NameStr = "",
1755 Instruction *InsertBefore = 0) {
1756 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1757 NameStr, InsertBefore);
1759 template<typename InputIterator>
1760 static InsertValueInst *Create(Value *Agg, Value *Val,
1761 InputIterator IdxBegin, InputIterator IdxEnd,
1762 const std::string &NameStr,
1763 BasicBlock *InsertAtEnd) {
1764 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1765 NameStr, InsertAtEnd);
1768 /// Constructors - These two creators are convenience methods because one
1769 /// index insertvalue instructions are much more common than those with
1771 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1772 const std::string &NameStr = "",
1773 Instruction *InsertBefore = 0) {
1774 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1776 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1777 const std::string &NameStr,
1778 BasicBlock *InsertAtEnd) {
1779 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1782 virtual InsertValueInst *clone() const;
1784 /// Transparently provide more efficient getOperand methods.
1785 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1787 // getType - Overload to return most specific pointer type...
1788 const PointerType *getType() const {
1789 return reinterpret_cast<const PointerType*>(Instruction::getType());
1792 typedef const unsigned* idx_iterator;
1793 inline idx_iterator idx_begin() const { return Indices.begin(); }
1794 inline idx_iterator idx_end() const { return Indices.end(); }
1796 Value *getAggregateOperand() {
1797 return getOperand(0);
1799 const Value *getAggregateOperand() const {
1800 return getOperand(0);
1802 static unsigned getAggregateOperandIndex() {
1803 return 0U; // get index for modifying correct operand
1806 Value *getInsertedValueOperand() {
1807 return getOperand(1);
1809 const Value *getInsertedValueOperand() const {
1810 return getOperand(1);
1812 static unsigned getInsertedValueOperandIndex() {
1813 return 1U; // get index for modifying correct operand
1816 unsigned getNumIndices() const { // Note: always non-negative
1817 return (unsigned)Indices.size();
1820 bool hasIndices() const {
1824 // Methods for support type inquiry through isa, cast, and dyn_cast:
1825 static inline bool classof(const InsertValueInst *) { return true; }
1826 static inline bool classof(const Instruction *I) {
1827 return I->getOpcode() == Instruction::InsertValue;
1829 static inline bool classof(const Value *V) {
1830 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1835 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1838 template<typename InputIterator>
1839 InsertValueInst::InsertValueInst(Value *Agg,
1841 InputIterator IdxBegin,
1842 InputIterator IdxEnd,
1843 const std::string &NameStr,
1844 Instruction *InsertBefore)
1845 : Instruction(Agg->getType(), InsertValue,
1846 OperandTraits<InsertValueInst>::op_begin(this),
1848 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1849 typename std::iterator_traits<InputIterator>::iterator_category());
1851 template<typename InputIterator>
1852 InsertValueInst::InsertValueInst(Value *Agg,
1854 InputIterator IdxBegin,
1855 InputIterator IdxEnd,
1856 const std::string &NameStr,
1857 BasicBlock *InsertAtEnd)
1858 : Instruction(Agg->getType(), InsertValue,
1859 OperandTraits<InsertValueInst>::op_begin(this),
1861 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1862 typename std::iterator_traits<InputIterator>::iterator_category());
1865 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1867 //===----------------------------------------------------------------------===//
1869 //===----------------------------------------------------------------------===//
1871 // PHINode - The PHINode class is used to represent the magical mystical PHI
1872 // node, that can not exist in nature, but can be synthesized in a computer
1873 // scientist's overactive imagination.
1875 class PHINode : public Instruction {
1876 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1877 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1878 /// the number actually in use.
1879 unsigned ReservedSpace;
1880 PHINode(const PHINode &PN);
1881 // allocate space for exactly zero operands
1882 void *operator new(size_t s) {
1883 return User::operator new(s, 0);
1885 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1886 Instruction *InsertBefore = 0)
1887 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1892 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1893 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1898 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1899 Instruction *InsertBefore = 0) {
1900 return new PHINode(Ty, NameStr, InsertBefore);
1902 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1903 BasicBlock *InsertAtEnd) {
1904 return new PHINode(Ty, NameStr, InsertAtEnd);
1908 /// reserveOperandSpace - This method can be used to avoid repeated
1909 /// reallocation of PHI operand lists by reserving space for the correct
1910 /// number of operands before adding them. Unlike normal vector reserves,
1911 /// this method can also be used to trim the operand space.
1912 void reserveOperandSpace(unsigned NumValues) {
1913 resizeOperands(NumValues*2);
1916 virtual PHINode *clone() const;
1918 /// Provide fast operand accessors
1919 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1921 /// getNumIncomingValues - Return the number of incoming edges
1923 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1925 /// getIncomingValue - Return incoming value number x
1927 Value *getIncomingValue(unsigned i) const {
1928 assert(i*2 < getNumOperands() && "Invalid value number!");
1929 return getOperand(i*2);
1931 void setIncomingValue(unsigned i, Value *V) {
1932 assert(i*2 < getNumOperands() && "Invalid value number!");
1935 unsigned getOperandNumForIncomingValue(unsigned i) {
1939 /// getIncomingBlock - Return incoming basic block number x
1941 BasicBlock *getIncomingBlock(unsigned i) const {
1942 return static_cast<BasicBlock*>(getOperand(i*2+1));
1944 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1945 setOperand(i*2+1, BB);
1947 unsigned getOperandNumForIncomingBlock(unsigned i) {
1951 /// addIncoming - Add an incoming value to the end of the PHI list
1953 void addIncoming(Value *V, BasicBlock *BB) {
1954 assert(V && "PHI node got a null value!");
1955 assert(BB && "PHI node got a null basic block!");
1956 assert(getType() == V->getType() &&
1957 "All operands to PHI node must be the same type as the PHI node!");
1958 unsigned OpNo = NumOperands;
1959 if (OpNo+2 > ReservedSpace)
1960 resizeOperands(0); // Get more space!
1961 // Initialize some new operands.
1962 NumOperands = OpNo+2;
1963 OperandList[OpNo] = V;
1964 OperandList[OpNo+1] = BB;
1967 /// removeIncomingValue - Remove an incoming value. This is useful if a
1968 /// predecessor basic block is deleted. The value removed is returned.
1970 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1971 /// is true), the PHI node is destroyed and any uses of it are replaced with
1972 /// dummy values. The only time there should be zero incoming values to a PHI
1973 /// node is when the block is dead, so this strategy is sound.
1975 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1977 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1978 int Idx = getBasicBlockIndex(BB);
1979 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1980 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1983 /// getBasicBlockIndex - Return the first index of the specified basic
1984 /// block in the value list for this PHI. Returns -1 if no instance.
1986 int getBasicBlockIndex(const BasicBlock *BB) const {
1987 Use *OL = OperandList;
1988 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1989 if (OL[i+1].get() == BB) return i/2;
1993 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1994 return getIncomingValue(getBasicBlockIndex(BB));
1997 /// hasConstantValue - If the specified PHI node always merges together the
1998 /// same value, return the value, otherwise return null.
2000 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
2002 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2003 static inline bool classof(const PHINode *) { return true; }
2004 static inline bool classof(const Instruction *I) {
2005 return I->getOpcode() == Instruction::PHI;
2007 static inline bool classof(const Value *V) {
2008 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2011 void resizeOperands(unsigned NumOperands);
2015 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
2018 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2021 //===----------------------------------------------------------------------===//
2023 //===----------------------------------------------------------------------===//
2025 //===---------------------------------------------------------------------------
2026 /// ReturnInst - Return a value (possibly void), from a function. Execution
2027 /// does not continue in this function any longer.
2029 class ReturnInst : public TerminatorInst {
2030 ReturnInst(const ReturnInst &RI);
2033 // ReturnInst constructors:
2034 // ReturnInst() - 'ret void' instruction
2035 // ReturnInst( null) - 'ret void' instruction
2036 // ReturnInst(Value* X) - 'ret X' instruction
2037 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2038 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2039 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2040 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2042 // NOTE: If the Value* passed is of type void then the constructor behaves as
2043 // if it was passed NULL.
2044 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2045 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2046 explicit ReturnInst(BasicBlock *InsertAtEnd);
2048 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2049 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2051 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2052 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2054 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2055 return new(0) ReturnInst(InsertAtEnd);
2057 virtual ~ReturnInst();
2059 virtual ReturnInst *clone() const;
2061 /// Provide fast operand accessors
2062 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2064 /// Convenience accessor
2065 Value *getReturnValue(unsigned n = 0) const {
2066 return n < getNumOperands()
2071 unsigned getNumSuccessors() const { return 0; }
2073 // Methods for support type inquiry through isa, cast, and dyn_cast:
2074 static inline bool classof(const ReturnInst *) { return true; }
2075 static inline bool classof(const Instruction *I) {
2076 return (I->getOpcode() == Instruction::Ret);
2078 static inline bool classof(const Value *V) {
2079 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2082 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2083 virtual unsigned getNumSuccessorsV() const;
2084 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2088 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2091 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2093 //===----------------------------------------------------------------------===//
2095 //===----------------------------------------------------------------------===//
2097 //===---------------------------------------------------------------------------
2098 /// BranchInst - Conditional or Unconditional Branch instruction.
2100 class BranchInst : public TerminatorInst {
2101 /// Ops list - Branches are strange. The operands are ordered:
2102 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2103 /// they don't have to check for cond/uncond branchness.
2104 BranchInst(const BranchInst &BI);
2106 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2107 // BranchInst(BB *B) - 'br B'
2108 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2109 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2110 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2111 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2112 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2113 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2114 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2115 Instruction *InsertBefore = 0);
2116 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2117 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2118 BasicBlock *InsertAtEnd);
2120 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2121 return new(1) BranchInst(IfTrue, InsertBefore);
2123 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2124 Value *Cond, Instruction *InsertBefore = 0) {
2125 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2127 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2128 return new(1) BranchInst(IfTrue, InsertAtEnd);
2130 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2131 Value *Cond, BasicBlock *InsertAtEnd) {
2132 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2136 if (NumOperands == 1)
2137 NumOperands = (unsigned)((Use*)this - OperandList);
2140 /// Transparently provide more efficient getOperand methods.
2141 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2143 virtual BranchInst *clone() const;
2145 bool isUnconditional() const { return getNumOperands() == 1; }
2146 bool isConditional() const { return getNumOperands() == 3; }
2148 Value *getCondition() const {
2149 assert(isConditional() && "Cannot get condition of an uncond branch!");
2150 return getOperand(2);
2153 void setCondition(Value *V) {
2154 assert(isConditional() && "Cannot set condition of unconditional branch!");
2158 // setUnconditionalDest - Change the current branch to an unconditional branch
2159 // targeting the specified block.
2160 // FIXME: Eliminate this ugly method.
2161 void setUnconditionalDest(BasicBlock *Dest) {
2163 if (isConditional()) { // Convert this to an uncond branch.
2170 unsigned getNumSuccessors() const { return 1+isConditional(); }
2172 BasicBlock *getSuccessor(unsigned i) const {
2173 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2174 return cast<BasicBlock>(getOperand(i));
2177 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2178 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2179 setOperand(idx, NewSucc);
2182 // Methods for support type inquiry through isa, cast, and dyn_cast:
2183 static inline bool classof(const BranchInst *) { return true; }
2184 static inline bool classof(const Instruction *I) {
2185 return (I->getOpcode() == Instruction::Br);
2187 static inline bool classof(const Value *V) {
2188 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2191 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2192 virtual unsigned getNumSuccessorsV() const;
2193 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2197 struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2198 // we need to access operands via OperandList, since
2199 // the NumOperands may change from 3 to 1
2200 static inline void *allocate(unsigned); // FIXME
2203 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2205 //===----------------------------------------------------------------------===//
2207 //===----------------------------------------------------------------------===//
2209 //===---------------------------------------------------------------------------
2210 /// SwitchInst - Multiway switch
2212 class SwitchInst : public TerminatorInst {
2213 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2214 unsigned ReservedSpace;
2215 // Operand[0] = Value to switch on
2216 // Operand[1] = Default basic block destination
2217 // Operand[2n ] = Value to match
2218 // Operand[2n+1] = BasicBlock to go to on match
2219 SwitchInst(const SwitchInst &RI);
2220 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2221 void resizeOperands(unsigned No);
2222 // allocate space for exactly zero operands
2223 void *operator new(size_t s) {
2224 return User::operator new(s, 0);
2226 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2227 /// switch on and a default destination. The number of additional cases can
2228 /// be specified here to make memory allocation more efficient. This
2229 /// constructor can also autoinsert before another instruction.
2230 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2231 Instruction *InsertBefore = 0);
2233 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2234 /// switch on and a default destination. The number of additional cases can
2235 /// be specified here to make memory allocation more efficient. This
2236 /// constructor also autoinserts at the end of the specified BasicBlock.
2237 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2238 BasicBlock *InsertAtEnd);
2240 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2241 unsigned NumCases, Instruction *InsertBefore = 0) {
2242 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2244 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2245 unsigned NumCases, BasicBlock *InsertAtEnd) {
2246 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2250 /// Provide fast operand accessors
2251 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2253 // Accessor Methods for Switch stmt
2254 Value *getCondition() const { return getOperand(0); }
2255 void setCondition(Value *V) { setOperand(0, V); }
2257 BasicBlock *getDefaultDest() const {
2258 return cast<BasicBlock>(getOperand(1));
2261 /// getNumCases - return the number of 'cases' in this switch instruction.
2262 /// Note that case #0 is always the default case.
2263 unsigned getNumCases() const {
2264 return getNumOperands()/2;
2267 /// getCaseValue - Return the specified case value. Note that case #0, the
2268 /// default destination, does not have a case value.
2269 ConstantInt *getCaseValue(unsigned i) {
2270 assert(i && i < getNumCases() && "Illegal case value to get!");
2271 return getSuccessorValue(i);
2274 /// getCaseValue - Return the specified case value. Note that case #0, the
2275 /// default destination, does not have a case value.
2276 const ConstantInt *getCaseValue(unsigned i) const {
2277 assert(i && i < getNumCases() && "Illegal case value to get!");
2278 return getSuccessorValue(i);
2281 /// findCaseValue - Search all of the case values for the specified constant.
2282 /// If it is explicitly handled, return the case number of it, otherwise
2283 /// return 0 to indicate that it is handled by the default handler.
2284 unsigned findCaseValue(const ConstantInt *C) const {
2285 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2286 if (getCaseValue(i) == C)
2291 /// findCaseDest - Finds the unique case value for a given successor. Returns
2292 /// null if the successor is not found, not unique, or is the default case.
2293 ConstantInt *findCaseDest(BasicBlock *BB) {
2294 if (BB == getDefaultDest()) return NULL;
2296 ConstantInt *CI = NULL;
2297 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2298 if (getSuccessor(i) == BB) {
2299 if (CI) return NULL; // Multiple cases lead to BB.
2300 else CI = getCaseValue(i);
2306 /// addCase - Add an entry to the switch instruction...
2308 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2310 /// removeCase - This method removes the specified successor from the switch
2311 /// instruction. Note that this cannot be used to remove the default
2312 /// destination (successor #0).
2314 void removeCase(unsigned idx);
2316 virtual SwitchInst *clone() const;
2318 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2319 BasicBlock *getSuccessor(unsigned idx) const {
2320 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2321 return cast<BasicBlock>(getOperand(idx*2+1));
2323 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2324 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2325 setOperand(idx*2+1, NewSucc);
2328 // getSuccessorValue - Return the value associated with the specified
2330 ConstantInt *getSuccessorValue(unsigned idx) const {
2331 assert(idx < getNumSuccessors() && "Successor # out of range!");
2332 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2335 // Methods for support type inquiry through isa, cast, and dyn_cast:
2336 static inline bool classof(const SwitchInst *) { return true; }
2337 static inline bool classof(const Instruction *I) {
2338 return I->getOpcode() == Instruction::Switch;
2340 static inline bool classof(const Value *V) {
2341 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2344 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2345 virtual unsigned getNumSuccessorsV() const;
2346 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2350 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2353 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2356 //===----------------------------------------------------------------------===//
2358 //===----------------------------------------------------------------------===//
2360 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2361 /// calling convention of the call.
2363 class InvokeInst : public TerminatorInst {
2364 AttrListPtr AttributeList;
2365 InvokeInst(const InvokeInst &BI);
2366 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2367 Value* const *Args, unsigned NumArgs);
2369 template<typename InputIterator>
2370 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2371 InputIterator ArgBegin, InputIterator ArgEnd,
2372 const std::string &NameStr,
2373 // This argument ensures that we have an iterator we can
2374 // do arithmetic on in constant time
2375 std::random_access_iterator_tag) {
2376 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2378 // This requires that the iterator points to contiguous memory.
2379 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2383 /// Construct an InvokeInst given a range of arguments.
2384 /// InputIterator must be a random-access iterator pointing to
2385 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2386 /// made for random-accessness but not for contiguous storage as
2387 /// that would incur runtime overhead.
2389 /// @brief Construct an InvokeInst from a range of arguments
2390 template<typename InputIterator>
2391 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2392 InputIterator ArgBegin, InputIterator ArgEnd,
2394 const std::string &NameStr, Instruction *InsertBefore);
2396 /// Construct an InvokeInst given a range of arguments.
2397 /// InputIterator must be a random-access iterator pointing to
2398 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2399 /// made for random-accessness but not for contiguous storage as
2400 /// that would incur runtime overhead.
2402 /// @brief Construct an InvokeInst from a range of arguments
2403 template<typename InputIterator>
2404 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2405 InputIterator ArgBegin, InputIterator ArgEnd,
2407 const std::string &NameStr, BasicBlock *InsertAtEnd);
2409 template<typename InputIterator>
2410 static InvokeInst *Create(Value *Func,
2411 BasicBlock *IfNormal, BasicBlock *IfException,
2412 InputIterator ArgBegin, InputIterator ArgEnd,
2413 const std::string &NameStr = "",
2414 Instruction *InsertBefore = 0) {
2415 unsigned Values(ArgEnd - ArgBegin + 3);
2416 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2417 Values, NameStr, InsertBefore);
2419 template<typename InputIterator>
2420 static InvokeInst *Create(Value *Func,
2421 BasicBlock *IfNormal, BasicBlock *IfException,
2422 InputIterator ArgBegin, InputIterator ArgEnd,
2423 const std::string &NameStr,
2424 BasicBlock *InsertAtEnd) {
2425 unsigned Values(ArgEnd - ArgBegin + 3);
2426 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2427 Values, NameStr, InsertAtEnd);
2430 virtual InvokeInst *clone() const;
2432 /// Provide fast operand accessors
2433 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2435 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2437 unsigned getCallingConv() const { return SubclassData; }
2438 void setCallingConv(unsigned CC) {
2442 /// getAttributes - Return the parameter attributes for this invoke.
2444 const AttrListPtr &getAttributes() const { return AttributeList; }
2446 /// setAttributes - Set the parameter attributes for this invoke.
2448 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2450 /// addAttribute - adds the attribute to the list of attributes.
2451 void addAttribute(unsigned i, Attributes attr);
2453 /// removeAttribute - removes the attribute from the list of attributes.
2454 void removeAttribute(unsigned i, Attributes attr);
2456 /// @brief Determine whether the call or the callee has the given attribute.
2457 bool paramHasAttr(unsigned i, Attributes attr) const;
2459 /// @brief Extract the alignment for a call or parameter (0=unknown).
2460 unsigned getParamAlignment(unsigned i) const {
2461 return AttributeList.getParamAlignment(i);
2464 /// @brief Determine if the call does not access memory.
2465 bool doesNotAccessMemory() const {
2466 return paramHasAttr(0, Attribute::ReadNone);
2468 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2469 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2470 else removeAttribute(~0, Attribute::ReadNone);
2473 /// @brief Determine if the call does not access or only reads memory.
2474 bool onlyReadsMemory() const {
2475 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2477 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2478 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2479 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2482 /// @brief Determine if the call cannot return.
2483 bool doesNotReturn() const {
2484 return paramHasAttr(~0, Attribute::NoReturn);
2486 void setDoesNotReturn(bool DoesNotReturn = true) {
2487 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2488 else removeAttribute(~0, Attribute::NoReturn);
2491 /// @brief Determine if the call cannot unwind.
2492 bool doesNotThrow() const {
2493 return paramHasAttr(~0, Attribute::NoUnwind);
2495 void setDoesNotThrow(bool DoesNotThrow = true) {
2496 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2497 else removeAttribute(~0, Attribute::NoUnwind);
2500 /// @brief Determine if the call returns a structure through first
2501 /// pointer argument.
2502 bool hasStructRetAttr() const {
2503 // Be friendly and also check the callee.
2504 return paramHasAttr(1, Attribute::StructRet);
2507 /// @brief Determine if any call argument is an aggregate passed by value.
2508 bool hasByValArgument() const {
2509 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2512 /// getCalledFunction - Return the function called, or null if this is an
2513 /// indirect function invocation.
2515 Function *getCalledFunction() const {
2516 return dyn_cast<Function>(getOperand(0));
2519 /// getCalledValue - Get a pointer to the function that is invoked by this
2521 const Value *getCalledValue() const { return getOperand(0); }
2522 Value *getCalledValue() { return getOperand(0); }
2524 // get*Dest - Return the destination basic blocks...
2525 BasicBlock *getNormalDest() const {
2526 return cast<BasicBlock>(getOperand(1));
2528 BasicBlock *getUnwindDest() const {
2529 return cast<BasicBlock>(getOperand(2));
2531 void setNormalDest(BasicBlock *B) {
2535 void setUnwindDest(BasicBlock *B) {
2539 BasicBlock *getSuccessor(unsigned i) const {
2540 assert(i < 2 && "Successor # out of range for invoke!");
2541 return i == 0 ? getNormalDest() : getUnwindDest();
2544 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2545 assert(idx < 2 && "Successor # out of range for invoke!");
2546 setOperand(idx+1, NewSucc);
2549 unsigned getNumSuccessors() const { return 2; }
2551 // Methods for support type inquiry through isa, cast, and dyn_cast:
2552 static inline bool classof(const InvokeInst *) { return true; }
2553 static inline bool classof(const Instruction *I) {
2554 return (I->getOpcode() == Instruction::Invoke);
2556 static inline bool classof(const Value *V) {
2557 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2560 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2561 virtual unsigned getNumSuccessorsV() const;
2562 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2566 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2569 template<typename InputIterator>
2570 InvokeInst::InvokeInst(Value *Func,
2571 BasicBlock *IfNormal, BasicBlock *IfException,
2572 InputIterator ArgBegin, InputIterator ArgEnd,
2574 const std::string &NameStr, Instruction *InsertBefore)
2575 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2576 ->getElementType())->getReturnType(),
2577 Instruction::Invoke,
2578 OperandTraits<InvokeInst>::op_end(this) - Values,
2579 Values, InsertBefore) {
2580 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2581 typename std::iterator_traits<InputIterator>::iterator_category());
2583 template<typename InputIterator>
2584 InvokeInst::InvokeInst(Value *Func,
2585 BasicBlock *IfNormal, BasicBlock *IfException,
2586 InputIterator ArgBegin, InputIterator ArgEnd,
2588 const std::string &NameStr, BasicBlock *InsertAtEnd)
2589 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2590 ->getElementType())->getReturnType(),
2591 Instruction::Invoke,
2592 OperandTraits<InvokeInst>::op_end(this) - Values,
2593 Values, InsertAtEnd) {
2594 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2595 typename std::iterator_traits<InputIterator>::iterator_category());
2598 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2600 //===----------------------------------------------------------------------===//
2602 //===----------------------------------------------------------------------===//
2604 //===---------------------------------------------------------------------------
2605 /// UnwindInst - Immediately exit the current function, unwinding the stack
2606 /// until an invoke instruction is found.
2608 class UnwindInst : public TerminatorInst {
2609 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2611 // allocate space for exactly zero operands
2612 void *operator new(size_t s) {
2613 return User::operator new(s, 0);
2615 explicit UnwindInst(Instruction *InsertBefore = 0);
2616 explicit UnwindInst(BasicBlock *InsertAtEnd);
2618 virtual UnwindInst *clone() const;
2620 unsigned getNumSuccessors() const { return 0; }
2622 // Methods for support type inquiry through isa, cast, and dyn_cast:
2623 static inline bool classof(const UnwindInst *) { return true; }
2624 static inline bool classof(const Instruction *I) {
2625 return I->getOpcode() == Instruction::Unwind;
2627 static inline bool classof(const Value *V) {
2628 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2631 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2632 virtual unsigned getNumSuccessorsV() const;
2633 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2636 //===----------------------------------------------------------------------===//
2637 // UnreachableInst Class
2638 //===----------------------------------------------------------------------===//
2640 //===---------------------------------------------------------------------------
2641 /// UnreachableInst - This function has undefined behavior. In particular, the
2642 /// presence of this instruction indicates some higher level knowledge that the
2643 /// end of the block cannot be reached.
2645 class UnreachableInst : public TerminatorInst {
2646 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2648 // allocate space for exactly zero operands
2649 void *operator new(size_t s) {
2650 return User::operator new(s, 0);
2652 explicit UnreachableInst(Instruction *InsertBefore = 0);
2653 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2655 virtual UnreachableInst *clone() const;
2657 unsigned getNumSuccessors() const { return 0; }
2659 // Methods for support type inquiry through isa, cast, and dyn_cast:
2660 static inline bool classof(const UnreachableInst *) { return true; }
2661 static inline bool classof(const Instruction *I) {
2662 return I->getOpcode() == Instruction::Unreachable;
2664 static inline bool classof(const Value *V) {
2665 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2668 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2669 virtual unsigned getNumSuccessorsV() const;
2670 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2673 //===----------------------------------------------------------------------===//
2675 //===----------------------------------------------------------------------===//
2677 /// @brief This class represents a truncation of integer types.
2678 class TruncInst : public CastInst {
2679 /// Private copy constructor
2680 TruncInst(const TruncInst &CI)
2681 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2684 /// @brief Constructor with insert-before-instruction semantics
2686 Value *S, ///< The value to be truncated
2687 const Type *Ty, ///< The (smaller) type to truncate to
2688 const std::string &NameStr = "", ///< A name for the new instruction
2689 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2692 /// @brief Constructor with insert-at-end-of-block semantics
2694 Value *S, ///< The value to be truncated
2695 const Type *Ty, ///< The (smaller) type to truncate to
2696 const std::string &NameStr, ///< A name for the new instruction
2697 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2700 /// @brief Clone an identical TruncInst
2701 virtual CastInst *clone() const;
2703 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2704 static inline bool classof(const TruncInst *) { return true; }
2705 static inline bool classof(const Instruction *I) {
2706 return I->getOpcode() == Trunc;
2708 static inline bool classof(const Value *V) {
2709 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2713 //===----------------------------------------------------------------------===//
2715 //===----------------------------------------------------------------------===//
2717 /// @brief This class represents zero extension of integer types.
2718 class ZExtInst : public CastInst {
2719 /// @brief Private copy constructor
2720 ZExtInst(const ZExtInst &CI)
2721 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2724 /// @brief Constructor with insert-before-instruction semantics
2726 Value *S, ///< The value to be zero extended
2727 const Type *Ty, ///< The type to zero extend to
2728 const std::string &NameStr = "", ///< A name for the new instruction
2729 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2732 /// @brief Constructor with insert-at-end semantics.
2734 Value *S, ///< The value to be zero extended
2735 const Type *Ty, ///< The type to zero extend to
2736 const std::string &NameStr, ///< A name for the new instruction
2737 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2740 /// @brief Clone an identical ZExtInst
2741 virtual CastInst *clone() const;
2743 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2744 static inline bool classof(const ZExtInst *) { return true; }
2745 static inline bool classof(const Instruction *I) {
2746 return I->getOpcode() == ZExt;
2748 static inline bool classof(const Value *V) {
2749 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2753 //===----------------------------------------------------------------------===//
2755 //===----------------------------------------------------------------------===//
2757 /// @brief This class represents a sign extension of integer types.
2758 class SExtInst : public CastInst {
2759 /// @brief Private copy constructor
2760 SExtInst(const SExtInst &CI)
2761 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2764 /// @brief Constructor with insert-before-instruction semantics
2766 Value *S, ///< The value to be sign extended
2767 const Type *Ty, ///< The type to sign extend to
2768 const std::string &NameStr = "", ///< A name for the new instruction
2769 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2772 /// @brief Constructor with insert-at-end-of-block semantics
2774 Value *S, ///< The value to be sign extended
2775 const Type *Ty, ///< The type to sign extend to
2776 const std::string &NameStr, ///< A name for the new instruction
2777 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2780 /// @brief Clone an identical SExtInst
2781 virtual CastInst *clone() const;
2783 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2784 static inline bool classof(const SExtInst *) { return true; }
2785 static inline bool classof(const Instruction *I) {
2786 return I->getOpcode() == SExt;
2788 static inline bool classof(const Value *V) {
2789 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2793 //===----------------------------------------------------------------------===//
2794 // FPTruncInst Class
2795 //===----------------------------------------------------------------------===//
2797 /// @brief This class represents a truncation of floating point types.
2798 class FPTruncInst : public CastInst {
2799 FPTruncInst(const FPTruncInst &CI)
2800 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2803 /// @brief Constructor with insert-before-instruction semantics
2805 Value *S, ///< The value to be truncated
2806 const Type *Ty, ///< The type to truncate to
2807 const std::string &NameStr = "", ///< A name for the new instruction
2808 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2811 /// @brief Constructor with insert-before-instruction semantics
2813 Value *S, ///< The value to be truncated
2814 const Type *Ty, ///< The type to truncate to
2815 const std::string &NameStr, ///< A name for the new instruction
2816 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2819 /// @brief Clone an identical FPTruncInst
2820 virtual CastInst *clone() const;
2822 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2823 static inline bool classof(const FPTruncInst *) { return true; }
2824 static inline bool classof(const Instruction *I) {
2825 return I->getOpcode() == FPTrunc;
2827 static inline bool classof(const Value *V) {
2828 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2832 //===----------------------------------------------------------------------===//
2834 //===----------------------------------------------------------------------===//
2836 /// @brief This class represents an extension of floating point types.
2837 class FPExtInst : public CastInst {
2838 FPExtInst(const FPExtInst &CI)
2839 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2842 /// @brief Constructor with insert-before-instruction semantics
2844 Value *S, ///< The value to be extended
2845 const Type *Ty, ///< The type to extend to
2846 const std::string &NameStr = "", ///< A name for the new instruction
2847 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2850 /// @brief Constructor with insert-at-end-of-block semantics
2852 Value *S, ///< The value to be extended
2853 const Type *Ty, ///< The type to extend to
2854 const std::string &NameStr, ///< A name for the new instruction
2855 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2858 /// @brief Clone an identical FPExtInst
2859 virtual CastInst *clone() const;
2861 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2862 static inline bool classof(const FPExtInst *) { return true; }
2863 static inline bool classof(const Instruction *I) {
2864 return I->getOpcode() == FPExt;
2866 static inline bool classof(const Value *V) {
2867 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2871 //===----------------------------------------------------------------------===//
2873 //===----------------------------------------------------------------------===//
2875 /// @brief This class represents a cast unsigned integer to floating point.
2876 class UIToFPInst : public CastInst {
2877 UIToFPInst(const UIToFPInst &CI)
2878 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2881 /// @brief Constructor with insert-before-instruction semantics
2883 Value *S, ///< The value to be converted
2884 const Type *Ty, ///< The type to convert to
2885 const std::string &NameStr = "", ///< A name for the new instruction
2886 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2889 /// @brief Constructor with insert-at-end-of-block semantics
2891 Value *S, ///< The value to be converted
2892 const Type *Ty, ///< The type to convert to
2893 const std::string &NameStr, ///< A name for the new instruction
2894 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2897 /// @brief Clone an identical UIToFPInst
2898 virtual CastInst *clone() const;
2900 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2901 static inline bool classof(const UIToFPInst *) { return true; }
2902 static inline bool classof(const Instruction *I) {
2903 return I->getOpcode() == UIToFP;
2905 static inline bool classof(const Value *V) {
2906 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2910 //===----------------------------------------------------------------------===//
2912 //===----------------------------------------------------------------------===//
2914 /// @brief This class represents a cast from signed integer to floating point.
2915 class SIToFPInst : public CastInst {
2916 SIToFPInst(const SIToFPInst &CI)
2917 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2920 /// @brief Constructor with insert-before-instruction semantics
2922 Value *S, ///< The value to be converted
2923 const Type *Ty, ///< The type to convert to
2924 const std::string &NameStr = "", ///< A name for the new instruction
2925 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2928 /// @brief Constructor with insert-at-end-of-block semantics
2930 Value *S, ///< The value to be converted
2931 const Type *Ty, ///< The type to convert to
2932 const std::string &NameStr, ///< A name for the new instruction
2933 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2936 /// @brief Clone an identical SIToFPInst
2937 virtual CastInst *clone() const;
2939 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2940 static inline bool classof(const SIToFPInst *) { return true; }
2941 static inline bool classof(const Instruction *I) {
2942 return I->getOpcode() == SIToFP;
2944 static inline bool classof(const Value *V) {
2945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2949 //===----------------------------------------------------------------------===//
2951 //===----------------------------------------------------------------------===//
2953 /// @brief This class represents a cast from floating point to unsigned integer
2954 class FPToUIInst : public CastInst {
2955 FPToUIInst(const FPToUIInst &CI)
2956 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2959 /// @brief Constructor with insert-before-instruction semantics
2961 Value *S, ///< The value to be converted
2962 const Type *Ty, ///< The type to convert to
2963 const std::string &NameStr = "", ///< A name for the new instruction
2964 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2967 /// @brief Constructor with insert-at-end-of-block semantics
2969 Value *S, ///< The value to be converted
2970 const Type *Ty, ///< The type to convert to
2971 const std::string &NameStr, ///< A name for the new instruction
2972 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2975 /// @brief Clone an identical FPToUIInst
2976 virtual CastInst *clone() const;
2978 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2979 static inline bool classof(const FPToUIInst *) { return true; }
2980 static inline bool classof(const Instruction *I) {
2981 return I->getOpcode() == FPToUI;
2983 static inline bool classof(const Value *V) {
2984 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2988 //===----------------------------------------------------------------------===//
2990 //===----------------------------------------------------------------------===//
2992 /// @brief This class represents a cast from floating point to signed integer.
2993 class FPToSIInst : public CastInst {
2994 FPToSIInst(const FPToSIInst &CI)
2995 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2998 /// @brief Constructor with insert-before-instruction semantics
3000 Value *S, ///< The value to be converted
3001 const Type *Ty, ///< The type to convert to
3002 const std::string &NameStr = "", ///< A name for the new instruction
3003 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3006 /// @brief Constructor with insert-at-end-of-block semantics
3008 Value *S, ///< The value to be converted
3009 const Type *Ty, ///< The type to convert to
3010 const std::string &NameStr, ///< A name for the new instruction
3011 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3014 /// @brief Clone an identical FPToSIInst
3015 virtual CastInst *clone() const;
3017 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3018 static inline bool classof(const FPToSIInst *) { return true; }
3019 static inline bool classof(const Instruction *I) {
3020 return I->getOpcode() == FPToSI;
3022 static inline bool classof(const Value *V) {
3023 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3027 //===----------------------------------------------------------------------===//
3028 // IntToPtrInst Class
3029 //===----------------------------------------------------------------------===//
3031 /// @brief This class represents a cast from an integer to a pointer.
3032 class IntToPtrInst : public CastInst {
3033 IntToPtrInst(const IntToPtrInst &CI)
3034 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3037 /// @brief Constructor with insert-before-instruction semantics
3039 Value *S, ///< The value to be converted
3040 const Type *Ty, ///< The type to convert to
3041 const std::string &NameStr = "", ///< A name for the new instruction
3042 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3045 /// @brief Constructor with insert-at-end-of-block semantics
3047 Value *S, ///< The value to be converted
3048 const Type *Ty, ///< The type to convert to
3049 const std::string &NameStr, ///< A name for the new instruction
3050 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3053 /// @brief Clone an identical IntToPtrInst
3054 virtual CastInst *clone() const;
3056 // Methods for support type inquiry through isa, cast, and dyn_cast:
3057 static inline bool classof(const IntToPtrInst *) { return true; }
3058 static inline bool classof(const Instruction *I) {
3059 return I->getOpcode() == IntToPtr;
3061 static inline bool classof(const Value *V) {
3062 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3066 //===----------------------------------------------------------------------===//
3067 // PtrToIntInst Class
3068 //===----------------------------------------------------------------------===//
3070 /// @brief This class represents a cast from a pointer to an integer
3071 class PtrToIntInst : public CastInst {
3072 PtrToIntInst(const PtrToIntInst &CI)
3073 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3076 /// @brief Constructor with insert-before-instruction semantics
3078 Value *S, ///< The value to be converted
3079 const Type *Ty, ///< The type to convert to
3080 const std::string &NameStr = "", ///< A name for the new instruction
3081 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3084 /// @brief Constructor with insert-at-end-of-block semantics
3086 Value *S, ///< The value to be converted
3087 const Type *Ty, ///< The type to convert to
3088 const std::string &NameStr, ///< A name for the new instruction
3089 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3092 /// @brief Clone an identical PtrToIntInst
3093 virtual CastInst *clone() const;
3095 // Methods for support type inquiry through isa, cast, and dyn_cast:
3096 static inline bool classof(const PtrToIntInst *) { return true; }
3097 static inline bool classof(const Instruction *I) {
3098 return I->getOpcode() == PtrToInt;
3100 static inline bool classof(const Value *V) {
3101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3105 //===----------------------------------------------------------------------===//
3106 // BitCastInst Class
3107 //===----------------------------------------------------------------------===//
3109 /// @brief This class represents a no-op cast from one type to another.
3110 class BitCastInst : public CastInst {
3111 BitCastInst(const BitCastInst &CI)
3112 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3115 /// @brief Constructor with insert-before-instruction semantics
3117 Value *S, ///< The value to be casted
3118 const Type *Ty, ///< The type to casted to
3119 const std::string &NameStr = "", ///< A name for the new instruction
3120 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3123 /// @brief Constructor with insert-at-end-of-block semantics
3125 Value *S, ///< The value to be casted
3126 const Type *Ty, ///< The type to casted to
3127 const std::string &NameStr, ///< A name for the new instruction
3128 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3131 /// @brief Clone an identical BitCastInst
3132 virtual CastInst *clone() const;
3134 // Methods for support type inquiry through isa, cast, and dyn_cast:
3135 static inline bool classof(const BitCastInst *) { return true; }
3136 static inline bool classof(const Instruction *I) {
3137 return I->getOpcode() == BitCast;
3139 static inline bool classof(const Value *V) {
3140 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3144 } // End llvm namespace