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 /// getPointerOperandType - Method to return the pointer operand as a
537 const PointerType *getPointerOperandType() const {
538 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
542 unsigned getNumIndices() const { // Note: always non-negative
543 return getNumOperands() - 1;
546 bool hasIndices() const {
547 return getNumOperands() > 1;
550 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
551 /// zeros. If so, the result pointer and the first operand have the same
552 /// value, just potentially different types.
553 bool hasAllZeroIndices() const;
555 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
556 /// constant integers. If so, the result pointer and the first operand have
557 /// a constant offset between them.
558 bool hasAllConstantIndices() const;
561 // Methods for support type inquiry through isa, cast, and dyn_cast:
562 static inline bool classof(const GetElementPtrInst *) { return true; }
563 static inline bool classof(const Instruction *I) {
564 return (I->getOpcode() == Instruction::GetElementPtr);
566 static inline bool classof(const Value *V) {
567 return isa<Instruction>(V) && classof(cast<Instruction>(V));
572 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
575 template<typename InputIterator>
576 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
577 InputIterator IdxBegin,
578 InputIterator IdxEnd,
580 const std::string &NameStr,
581 Instruction *InsertBefore)
582 : Instruction(PointerType::get(checkType(
583 getIndexedType(Ptr->getType(),
585 cast<PointerType>(Ptr->getType())
586 ->getAddressSpace()),
588 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
589 Values, InsertBefore) {
590 init(Ptr, IdxBegin, IdxEnd, NameStr,
591 typename std::iterator_traits<InputIterator>::iterator_category());
593 template<typename InputIterator>
594 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
595 InputIterator IdxBegin,
596 InputIterator IdxEnd,
598 const std::string &NameStr,
599 BasicBlock *InsertAtEnd)
600 : Instruction(PointerType::get(checkType(
601 getIndexedType(Ptr->getType(),
603 cast<PointerType>(Ptr->getType())
604 ->getAddressSpace()),
606 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
607 Values, InsertAtEnd) {
608 init(Ptr, IdxBegin, IdxEnd, NameStr,
609 typename std::iterator_traits<InputIterator>::iterator_category());
613 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
616 //===----------------------------------------------------------------------===//
618 //===----------------------------------------------------------------------===//
620 /// This instruction compares its operands according to the predicate given
621 /// to the constructor. It only operates on integers or pointers. The operands
622 /// must be identical types.
623 /// @brief Represent an integer comparison operator.
624 class ICmpInst: public CmpInst {
626 /// @brief Constructor with insert-before-instruction semantics.
628 Predicate pred, ///< The predicate to use for the comparison
629 Value *LHS, ///< The left-hand-side of the expression
630 Value *RHS, ///< The right-hand-side of the expression
631 const std::string &NameStr = "", ///< Name of the instruction
632 Instruction *InsertBefore = 0 ///< Where to insert
633 ) : CmpInst(makeCmpResultType(LHS->getType()),
634 Instruction::ICmp, pred, LHS, RHS, NameStr,
636 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
637 pred <= CmpInst::LAST_ICMP_PREDICATE &&
638 "Invalid ICmp predicate value");
639 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
640 "Both operands to ICmp instruction are not of the same type!");
641 // Check that the operands are the right type
642 assert((getOperand(0)->getType()->isIntOrIntVector() ||
643 isa<PointerType>(getOperand(0)->getType())) &&
644 "Invalid operand types for ICmp instruction");
647 /// @brief Constructor with insert-at-block-end semantics.
649 Predicate pred, ///< The predicate to use for the comparison
650 Value *LHS, ///< The left-hand-side of the expression
651 Value *RHS, ///< The right-hand-side of the expression
652 const std::string &NameStr, ///< Name of the instruction
653 BasicBlock *InsertAtEnd ///< Block to insert into.
654 ) : CmpInst(makeCmpResultType(LHS->getType()),
655 Instruction::ICmp, pred, LHS, RHS, NameStr,
657 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
658 pred <= CmpInst::LAST_ICMP_PREDICATE &&
659 "Invalid ICmp predicate value");
660 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
661 "Both operands to ICmp instruction are not of the same type!");
662 // Check that the operands are the right type
663 assert((getOperand(0)->getType()->isIntOrIntVector() ||
664 isa<PointerType>(getOperand(0)->getType())) &&
665 "Invalid operand types for ICmp instruction");
668 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
669 /// @returns the predicate that would be the result if the operand were
670 /// regarded as signed.
671 /// @brief Return the signed version of the predicate
672 Predicate getSignedPredicate() const {
673 return getSignedPredicate(getPredicate());
676 /// This is a static version that you can use without an instruction.
677 /// @brief Return the signed version of the predicate.
678 static Predicate getSignedPredicate(Predicate pred);
680 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
681 /// @returns the predicate that would be the result if the operand were
682 /// regarded as unsigned.
683 /// @brief Return the unsigned version of the predicate
684 Predicate getUnsignedPredicate() const {
685 return getUnsignedPredicate(getPredicate());
688 /// This is a static version that you can use without an instruction.
689 /// @brief Return the unsigned version of the predicate.
690 static Predicate getUnsignedPredicate(Predicate pred);
692 /// isEquality - Return true if this predicate is either EQ or NE. This also
693 /// tests for commutativity.
694 static bool isEquality(Predicate P) {
695 return P == ICMP_EQ || P == ICMP_NE;
698 /// isEquality - Return true if this predicate is either EQ or NE. This also
699 /// tests for commutativity.
700 bool isEquality() const {
701 return isEquality(getPredicate());
704 /// @returns true if the predicate of this ICmpInst is commutative
705 /// @brief Determine if this relation is commutative.
706 bool isCommutative() const { return isEquality(); }
708 /// isRelational - Return true if the predicate is relational (not EQ or NE).
710 bool isRelational() const {
711 return !isEquality();
714 /// isRelational - Return true if the predicate is relational (not EQ or NE).
716 static bool isRelational(Predicate P) {
717 return !isEquality(P);
720 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
721 /// @brief Determine if this instruction's predicate is signed.
722 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
724 /// @returns true if the predicate provided is signed, false otherwise
725 /// @brief Determine if the predicate is signed.
726 static bool isSignedPredicate(Predicate pred);
728 /// @returns true if the specified compare predicate is
729 /// true when both operands are equal...
730 /// @brief Determine if the icmp is true when both operands are equal
731 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
732 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
733 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
734 pred == ICmpInst::ICMP_SLE;
737 /// @returns true if the specified compare instruction is
738 /// true when both operands are equal...
739 /// @brief Determine if the ICmpInst returns true when both operands are equal
740 bool isTrueWhenEqual() {
741 return isTrueWhenEqual(getPredicate());
744 /// Initialize a set of values that all satisfy the predicate with C.
745 /// @brief Make a ConstantRange for a relation with a constant value.
746 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
748 /// Exchange the two operands to this instruction in such a way that it does
749 /// not modify the semantics of the instruction. The predicate value may be
750 /// changed to retain the same result if the predicate is order dependent
752 /// @brief Swap operands and adjust predicate.
753 void swapOperands() {
754 SubclassData = getSwappedPredicate();
755 Op<0>().swap(Op<1>());
758 virtual ICmpInst *clone() const;
760 // Methods for support type inquiry through isa, cast, and dyn_cast:
761 static inline bool classof(const ICmpInst *) { return true; }
762 static inline bool classof(const Instruction *I) {
763 return I->getOpcode() == Instruction::ICmp;
765 static inline bool classof(const Value *V) {
766 return isa<Instruction>(V) && classof(cast<Instruction>(V));
771 //===----------------------------------------------------------------------===//
773 //===----------------------------------------------------------------------===//
775 /// This instruction compares its operands according to the predicate given
776 /// to the constructor. It only operates on floating point values or packed
777 /// vectors of floating point values. The operands must be identical types.
778 /// @brief Represents a floating point comparison operator.
779 class FCmpInst: public CmpInst {
781 /// @brief Constructor with insert-before-instruction semantics.
783 Predicate pred, ///< The predicate to use for the comparison
784 Value *LHS, ///< The left-hand-side of the expression
785 Value *RHS, ///< The right-hand-side of the expression
786 const std::string &NameStr = "", ///< Name of the instruction
787 Instruction *InsertBefore = 0 ///< Where to insert
788 ) : CmpInst(makeCmpResultType(LHS->getType()),
789 Instruction::FCmp, pred, LHS, RHS, NameStr,
791 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
792 "Invalid FCmp predicate value");
793 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
794 "Both operands to FCmp instruction are not of the same type!");
795 // Check that the operands are the right type
796 assert(getOperand(0)->getType()->isFPOrFPVector() &&
797 "Invalid operand types for FCmp instruction");
800 /// @brief Constructor with insert-at-block-end semantics.
802 Predicate pred, ///< The predicate to use for the comparison
803 Value *LHS, ///< The left-hand-side of the expression
804 Value *RHS, ///< The right-hand-side of the expression
805 const std::string &NameStr, ///< Name of the instruction
806 BasicBlock *InsertAtEnd ///< Block to insert into.
807 ) : CmpInst(makeCmpResultType(LHS->getType()),
808 Instruction::FCmp, pred, LHS, RHS, NameStr,
810 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
811 "Invalid FCmp predicate value");
812 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
813 "Both operands to FCmp instruction are not of the same type!");
814 // Check that the operands are the right type
815 assert(getOperand(0)->getType()->isFPOrFPVector() &&
816 "Invalid operand types for FCmp instruction");
819 /// @returns true if the predicate of this instruction is EQ or NE.
820 /// @brief Determine if this is an equality predicate.
821 bool isEquality() const {
822 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
823 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
826 /// @returns true if the predicate of this instruction is commutative.
827 /// @brief Determine if this is a commutative predicate.
828 bool isCommutative() const {
829 return isEquality() ||
830 SubclassData == FCMP_FALSE ||
831 SubclassData == FCMP_TRUE ||
832 SubclassData == FCMP_ORD ||
833 SubclassData == FCMP_UNO;
836 /// @returns true if the predicate is relational (not EQ or NE).
837 /// @brief Determine if this a relational predicate.
838 bool isRelational() const { return !isEquality(); }
840 /// Exchange the two operands to this instruction in such a way that it does
841 /// not modify the semantics of the instruction. The predicate value may be
842 /// changed to retain the same result if the predicate is order dependent
844 /// @brief Swap operands and adjust predicate.
845 void swapOperands() {
846 SubclassData = getSwappedPredicate();
847 Op<0>().swap(Op<1>());
850 virtual FCmpInst *clone() const;
852 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
853 static inline bool classof(const FCmpInst *) { return true; }
854 static inline bool classof(const Instruction *I) {
855 return I->getOpcode() == Instruction::FCmp;
857 static inline bool classof(const Value *V) {
858 return isa<Instruction>(V) && classof(cast<Instruction>(V));
863 //===----------------------------------------------------------------------===//
865 //===----------------------------------------------------------------------===//
867 /// This instruction compares its operands according to the predicate given
868 /// to the constructor. It only operates on vectors of integers.
869 /// The operands must be identical types.
870 /// @brief Represents a vector integer comparison operator.
871 class VICmpInst: public CmpInst {
873 /// @brief Constructor with insert-before-instruction semantics.
875 Predicate pred, ///< The predicate to use for the comparison
876 Value *LHS, ///< The left-hand-side of the expression
877 Value *RHS, ///< The right-hand-side of the expression
878 const std::string &NameStr = "", ///< Name of the instruction
879 Instruction *InsertBefore = 0 ///< Where to insert
880 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
882 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
883 pred <= CmpInst::LAST_ICMP_PREDICATE &&
884 "Invalid VICmp predicate value");
885 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
886 "Both operands to VICmp instruction are not of the same type!");
889 /// @brief Constructor with insert-at-block-end semantics.
891 Predicate pred, ///< The predicate to use for the comparison
892 Value *LHS, ///< The left-hand-side of the expression
893 Value *RHS, ///< The right-hand-side of the expression
894 const std::string &NameStr, ///< Name of the instruction
895 BasicBlock *InsertAtEnd ///< Block to insert into.
896 ) : CmpInst(LHS->getType(), Instruction::VICmp, pred, LHS, RHS, NameStr,
898 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
899 pred <= CmpInst::LAST_ICMP_PREDICATE &&
900 "Invalid VICmp predicate value");
901 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
902 "Both operands to VICmp instruction are not of the same type!");
905 /// @brief Return the predicate for this instruction.
906 Predicate getPredicate() const { return Predicate(SubclassData); }
908 virtual VICmpInst *clone() const;
910 // Methods for support type inquiry through isa, cast, and dyn_cast:
911 static inline bool classof(const VICmpInst *) { return true; }
912 static inline bool classof(const Instruction *I) {
913 return I->getOpcode() == Instruction::VICmp;
915 static inline bool classof(const Value *V) {
916 return isa<Instruction>(V) && classof(cast<Instruction>(V));
920 //===----------------------------------------------------------------------===//
922 //===----------------------------------------------------------------------===//
924 /// This instruction compares its operands according to the predicate given
925 /// to the constructor. It only operates on vectors of floating point values.
926 /// The operands must be identical types.
927 /// @brief Represents a vector floating point comparison operator.
928 class VFCmpInst: public CmpInst {
930 /// @brief Constructor with insert-before-instruction semantics.
932 Predicate pred, ///< The predicate to use for the comparison
933 Value *LHS, ///< The left-hand-side of the expression
934 Value *RHS, ///< The right-hand-side of the expression
935 const std::string &NameStr = "", ///< Name of the instruction
936 Instruction *InsertBefore = 0 ///< Where to insert
937 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
938 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertBefore) {
939 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
940 "Invalid VFCmp predicate value");
941 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
942 "Both operands to VFCmp instruction are not of the same type!");
945 /// @brief Constructor with insert-at-block-end semantics.
947 Predicate pred, ///< The predicate to use for the comparison
948 Value *LHS, ///< The left-hand-side of the expression
949 Value *RHS, ///< The right-hand-side of the expression
950 const std::string &NameStr, ///< Name of the instruction
951 BasicBlock *InsertAtEnd ///< Block to insert into.
952 ) : CmpInst(VectorType::getInteger(cast<VectorType>(LHS->getType())),
953 Instruction::VFCmp, pred, LHS, RHS, NameStr, InsertAtEnd) {
954 assert(pred <= CmpInst::LAST_FCMP_PREDICATE &&
955 "Invalid VFCmp predicate value");
956 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
957 "Both operands to VFCmp instruction are not of the same type!");
960 /// @brief Return the predicate for this instruction.
961 Predicate getPredicate() const { return Predicate(SubclassData); }
963 virtual VFCmpInst *clone() const;
965 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
966 static inline bool classof(const VFCmpInst *) { return true; }
967 static inline bool classof(const Instruction *I) {
968 return I->getOpcode() == Instruction::VFCmp;
970 static inline bool classof(const Value *V) {
971 return isa<Instruction>(V) && classof(cast<Instruction>(V));
975 //===----------------------------------------------------------------------===//
977 //===----------------------------------------------------------------------===//
978 /// CallInst - This class represents a function call, abstracting a target
979 /// machine's calling convention. This class uses low bit of the SubClassData
980 /// field to indicate whether or not this is a tail call. The rest of the bits
981 /// hold the calling convention of the call.
984 class CallInst : public Instruction {
985 AttrListPtr AttributeList; ///< parameter attributes for call
986 CallInst(const CallInst &CI);
987 void init(Value *Func, Value* const *Params, unsigned NumParams);
988 void init(Value *Func, Value *Actual1, Value *Actual2);
989 void init(Value *Func, Value *Actual);
990 void init(Value *Func);
992 template<typename InputIterator>
993 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
994 const std::string &NameStr,
995 // This argument ensures that we have an iterator we can
996 // do arithmetic on in constant time
997 std::random_access_iterator_tag) {
998 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
1000 // This requires that the iterator points to contiguous memory.
1001 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
1005 /// Construct a CallInst given a range of arguments. InputIterator
1006 /// must be a random-access iterator pointing to contiguous storage
1007 /// (e.g. a std::vector<>::iterator). Checks are made for
1008 /// random-accessness but not for contiguous storage as that would
1009 /// incur runtime overhead.
1010 /// @brief Construct a CallInst from a range of arguments
1011 template<typename InputIterator>
1012 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1013 const std::string &NameStr, Instruction *InsertBefore);
1015 /// Construct a CallInst given a range of arguments. InputIterator
1016 /// must be a random-access iterator pointing to contiguous storage
1017 /// (e.g. a std::vector<>::iterator). Checks are made for
1018 /// random-accessness but not for contiguous storage as that would
1019 /// incur runtime overhead.
1020 /// @brief Construct a CallInst from a range of arguments
1021 template<typename InputIterator>
1022 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1023 const std::string &NameStr, BasicBlock *InsertAtEnd);
1025 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1026 Instruction *InsertBefore);
1027 CallInst(Value *F, Value *Actual, const std::string& NameStr,
1028 BasicBlock *InsertAtEnd);
1029 explicit CallInst(Value *F, const std::string &NameStr,
1030 Instruction *InsertBefore);
1031 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
1033 template<typename InputIterator>
1034 static CallInst *Create(Value *Func,
1035 InputIterator ArgBegin, InputIterator ArgEnd,
1036 const std::string &NameStr = "",
1037 Instruction *InsertBefore = 0) {
1038 return new((unsigned)(ArgEnd - ArgBegin + 1))
1039 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
1041 template<typename InputIterator>
1042 static CallInst *Create(Value *Func,
1043 InputIterator ArgBegin, InputIterator ArgEnd,
1044 const std::string &NameStr, BasicBlock *InsertAtEnd) {
1045 return new((unsigned)(ArgEnd - ArgBegin + 1))
1046 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
1048 static CallInst *Create(Value *F, Value *Actual,
1049 const std::string& NameStr = "",
1050 Instruction *InsertBefore = 0) {
1051 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1053 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
1054 BasicBlock *InsertAtEnd) {
1055 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1057 static CallInst *Create(Value *F, const std::string &NameStr = "",
1058 Instruction *InsertBefore = 0) {
1059 return new(1) CallInst(F, NameStr, InsertBefore);
1061 static CallInst *Create(Value *F, const std::string &NameStr,
1062 BasicBlock *InsertAtEnd) {
1063 return new(1) CallInst(F, NameStr, InsertAtEnd);
1068 bool isTailCall() const { return SubclassData & 1; }
1069 void setTailCall(bool isTC = true) {
1070 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1073 virtual CallInst *clone() const;
1075 /// Provide fast operand accessors
1076 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1078 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1080 unsigned getCallingConv() const { return SubclassData >> 1; }
1081 void setCallingConv(unsigned CC) {
1082 SubclassData = (SubclassData & 1) | (CC << 1);
1085 /// getAttributes - Return the parameter attributes for this call.
1087 const AttrListPtr &getAttributes() const { return AttributeList; }
1089 /// setAttributes - Set the parameter attributes for this call.
1091 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1093 /// addAttribute - adds the attribute to the list of attributes.
1094 void addAttribute(unsigned i, Attributes attr);
1096 /// removeAttribute - removes the attribute from the list of attributes.
1097 void removeAttribute(unsigned i, Attributes attr);
1099 /// @brief Determine whether the call or the callee has the given attribute.
1100 bool paramHasAttr(unsigned i, Attributes attr) const;
1102 /// @brief Extract the alignment for a call or parameter (0=unknown).
1103 unsigned getParamAlignment(unsigned i) const {
1104 return AttributeList.getParamAlignment(i);
1107 /// @brief Determine if the call does not access memory.
1108 bool doesNotAccessMemory() const {
1109 return paramHasAttr(~0, Attribute::ReadNone);
1111 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1112 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1113 else removeAttribute(~0, Attribute::ReadNone);
1116 /// @brief Determine if the call does not access or only reads memory.
1117 bool onlyReadsMemory() const {
1118 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1120 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1121 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1122 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1125 /// @brief Determine if the call cannot return.
1126 bool doesNotReturn() const {
1127 return paramHasAttr(~0, Attribute::NoReturn);
1129 void setDoesNotReturn(bool DoesNotReturn = true) {
1130 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1131 else removeAttribute(~0, Attribute::NoReturn);
1134 /// @brief Determine if the call cannot unwind.
1135 bool doesNotThrow() const {
1136 return paramHasAttr(~0, Attribute::NoUnwind);
1138 void setDoesNotThrow(bool DoesNotThrow = true) {
1139 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1140 else removeAttribute(~0, Attribute::NoUnwind);
1143 /// @brief Determine if the call returns a structure through first
1144 /// pointer argument.
1145 bool hasStructRetAttr() const {
1146 // Be friendly and also check the callee.
1147 return paramHasAttr(1, Attribute::StructRet);
1150 /// @brief Determine if any call argument is an aggregate passed by value.
1151 bool hasByValArgument() const {
1152 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1155 /// getCalledFunction - Return the function called, or null if this is an
1156 /// indirect function invocation.
1158 Function *getCalledFunction() const {
1159 return dyn_cast<Function>(getOperand(0));
1162 /// getCalledValue - Get a pointer to the function that is invoked by this
1164 const Value *getCalledValue() const { return getOperand(0); }
1165 Value *getCalledValue() { return getOperand(0); }
1167 // Methods for support type inquiry through isa, cast, and dyn_cast:
1168 static inline bool classof(const CallInst *) { return true; }
1169 static inline bool classof(const Instruction *I) {
1170 return I->getOpcode() == Instruction::Call;
1172 static inline bool classof(const Value *V) {
1173 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1178 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1181 template<typename InputIterator>
1182 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1183 const std::string &NameStr, BasicBlock *InsertAtEnd)
1184 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1185 ->getElementType())->getReturnType(),
1187 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1188 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1189 init(Func, ArgBegin, ArgEnd, NameStr,
1190 typename std::iterator_traits<InputIterator>::iterator_category());
1193 template<typename InputIterator>
1194 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1195 const std::string &NameStr, Instruction *InsertBefore)
1196 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1197 ->getElementType())->getReturnType(),
1199 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1200 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1201 init(Func, ArgBegin, ArgEnd, NameStr,
1202 typename std::iterator_traits<InputIterator>::iterator_category());
1205 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1207 //===----------------------------------------------------------------------===//
1209 //===----------------------------------------------------------------------===//
1211 /// SelectInst - This class represents the LLVM 'select' instruction.
1213 class SelectInst : public Instruction {
1214 void init(Value *C, Value *S1, Value *S2) {
1215 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1221 SelectInst(const SelectInst &SI)
1222 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1223 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1225 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1226 Instruction *InsertBefore)
1227 : Instruction(S1->getType(), Instruction::Select,
1228 &Op<0>(), 3, InsertBefore) {
1232 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1233 BasicBlock *InsertAtEnd)
1234 : Instruction(S1->getType(), Instruction::Select,
1235 &Op<0>(), 3, InsertAtEnd) {
1240 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1241 const std::string &NameStr = "",
1242 Instruction *InsertBefore = 0) {
1243 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1245 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1246 const std::string &NameStr,
1247 BasicBlock *InsertAtEnd) {
1248 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1251 Value *getCondition() const { return Op<0>(); }
1252 Value *getTrueValue() const { return Op<1>(); }
1253 Value *getFalseValue() const { return Op<2>(); }
1255 /// areInvalidOperands - Return a string if the specified operands are invalid
1256 /// for a select operation, otherwise return null.
1257 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1259 /// Transparently provide more efficient getOperand methods.
1260 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1262 OtherOps getOpcode() const {
1263 return static_cast<OtherOps>(Instruction::getOpcode());
1266 virtual SelectInst *clone() const;
1268 // Methods for support type inquiry through isa, cast, and dyn_cast:
1269 static inline bool classof(const SelectInst *) { return true; }
1270 static inline bool classof(const Instruction *I) {
1271 return I->getOpcode() == Instruction::Select;
1273 static inline bool classof(const Value *V) {
1274 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1279 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1284 //===----------------------------------------------------------------------===//
1286 //===----------------------------------------------------------------------===//
1288 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1289 /// an argument of the specified type given a va_list and increments that list
1291 class VAArgInst : public UnaryInstruction {
1292 VAArgInst(const VAArgInst &VAA)
1293 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1295 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1296 Instruction *InsertBefore = 0)
1297 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1300 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1301 BasicBlock *InsertAtEnd)
1302 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1306 virtual VAArgInst *clone() const;
1308 // Methods for support type inquiry through isa, cast, and dyn_cast:
1309 static inline bool classof(const VAArgInst *) { return true; }
1310 static inline bool classof(const Instruction *I) {
1311 return I->getOpcode() == VAArg;
1313 static inline bool classof(const Value *V) {
1314 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1318 //===----------------------------------------------------------------------===//
1319 // ExtractElementInst Class
1320 //===----------------------------------------------------------------------===//
1322 /// ExtractElementInst - This instruction extracts a single (scalar)
1323 /// element from a VectorType value
1325 class ExtractElementInst : public Instruction {
1326 ExtractElementInst(const ExtractElementInst &EE) :
1327 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1328 Op<0>() = EE.Op<0>();
1329 Op<1>() = EE.Op<1>();
1333 // allocate space for exactly two operands
1334 void *operator new(size_t s) {
1335 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1337 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1338 Instruction *InsertBefore = 0);
1339 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr = "",
1340 Instruction *InsertBefore = 0);
1341 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1342 BasicBlock *InsertAtEnd);
1343 ExtractElementInst(Value *Vec, unsigned Idx, const std::string &NameStr,
1344 BasicBlock *InsertAtEnd);
1346 /// isValidOperands - Return true if an extractelement instruction can be
1347 /// formed with the specified operands.
1348 static bool isValidOperands(const Value *Vec, const Value *Idx);
1350 virtual ExtractElementInst *clone() const;
1352 /// Transparently provide more efficient getOperand methods.
1353 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1355 // Methods for support type inquiry through isa, cast, and dyn_cast:
1356 static inline bool classof(const ExtractElementInst *) { return true; }
1357 static inline bool classof(const Instruction *I) {
1358 return I->getOpcode() == Instruction::ExtractElement;
1360 static inline bool classof(const Value *V) {
1361 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1366 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1369 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1371 //===----------------------------------------------------------------------===//
1372 // InsertElementInst Class
1373 //===----------------------------------------------------------------------===//
1375 /// InsertElementInst - This instruction inserts a single (scalar)
1376 /// element into a VectorType value
1378 class InsertElementInst : public Instruction {
1379 InsertElementInst(const InsertElementInst &IE);
1380 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1381 const std::string &NameStr = "",
1382 Instruction *InsertBefore = 0);
1383 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1384 const std::string &NameStr = "",
1385 Instruction *InsertBefore = 0);
1386 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1387 const std::string &NameStr, BasicBlock *InsertAtEnd);
1388 InsertElementInst(Value *Vec, Value *NewElt, unsigned Idx,
1389 const std::string &NameStr, BasicBlock *InsertAtEnd);
1391 static InsertElementInst *Create(const InsertElementInst &IE) {
1392 return new(IE.getNumOperands()) InsertElementInst(IE);
1394 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1395 const std::string &NameStr = "",
1396 Instruction *InsertBefore = 0) {
1397 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1399 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1400 const std::string &NameStr = "",
1401 Instruction *InsertBefore = 0) {
1402 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1404 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1405 const std::string &NameStr,
1406 BasicBlock *InsertAtEnd) {
1407 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1409 static InsertElementInst *Create(Value *Vec, Value *NewElt, unsigned Idx,
1410 const std::string &NameStr,
1411 BasicBlock *InsertAtEnd) {
1412 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1415 /// isValidOperands - Return true if an insertelement instruction can be
1416 /// formed with the specified operands.
1417 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1420 virtual InsertElementInst *clone() const;
1422 /// getType - Overload to return most specific vector type.
1424 const VectorType *getType() const {
1425 return reinterpret_cast<const VectorType*>(Instruction::getType());
1428 /// Transparently provide more efficient getOperand methods.
1429 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1431 // Methods for support type inquiry through isa, cast, and dyn_cast:
1432 static inline bool classof(const InsertElementInst *) { return true; }
1433 static inline bool classof(const Instruction *I) {
1434 return I->getOpcode() == Instruction::InsertElement;
1436 static inline bool classof(const Value *V) {
1437 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1442 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1445 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1447 //===----------------------------------------------------------------------===//
1448 // ShuffleVectorInst Class
1449 //===----------------------------------------------------------------------===//
1451 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1454 class ShuffleVectorInst : public Instruction {
1455 ShuffleVectorInst(const ShuffleVectorInst &IE);
1457 // allocate space for exactly three operands
1458 void *operator new(size_t s) {
1459 return User::operator new(s, 3);
1461 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1462 const std::string &NameStr = "",
1463 Instruction *InsertBefor = 0);
1464 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1465 const std::string &NameStr, BasicBlock *InsertAtEnd);
1467 /// isValidOperands - Return true if a shufflevector instruction can be
1468 /// formed with the specified operands.
1469 static bool isValidOperands(const Value *V1, const Value *V2,
1472 virtual ShuffleVectorInst *clone() const;
1474 /// getType - Overload to return most specific vector type.
1476 const VectorType *getType() const {
1477 return reinterpret_cast<const VectorType*>(Instruction::getType());
1480 /// Transparently provide more efficient getOperand methods.
1481 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1483 /// getMaskValue - Return the index from the shuffle mask for the specified
1484 /// output result. This is either -1 if the element is undef or a number less
1485 /// than 2*numelements.
1486 int getMaskValue(unsigned i) const;
1488 // Methods for support type inquiry through isa, cast, and dyn_cast:
1489 static inline bool classof(const ShuffleVectorInst *) { return true; }
1490 static inline bool classof(const Instruction *I) {
1491 return I->getOpcode() == Instruction::ShuffleVector;
1493 static inline bool classof(const Value *V) {
1494 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1499 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1502 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1504 //===----------------------------------------------------------------------===//
1505 // ExtractValueInst Class
1506 //===----------------------------------------------------------------------===//
1508 /// ExtractValueInst - This instruction extracts a struct member or array
1509 /// element value from an aggregate value.
1511 class ExtractValueInst : public UnaryInstruction {
1512 SmallVector<unsigned, 4> Indices;
1514 ExtractValueInst(const ExtractValueInst &EVI);
1515 void init(const unsigned *Idx, unsigned NumIdx,
1516 const std::string &NameStr);
1517 void init(unsigned Idx, const std::string &NameStr);
1519 template<typename InputIterator>
1520 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1521 const std::string &NameStr,
1522 // This argument ensures that we have an iterator we can
1523 // do arithmetic on in constant time
1524 std::random_access_iterator_tag) {
1525 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1527 // There's no fundamental reason why we require at least one index
1528 // (other than weirdness with &*IdxBegin being invalid; see
1529 // getelementptr's init routine for example). But there's no
1530 // present need to support it.
1531 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1533 // This requires that the iterator points to contiguous memory.
1534 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1535 // we have to build an array here
1538 /// getIndexedType - Returns the type of the element that would be extracted
1539 /// with an extractvalue instruction with the specified parameters.
1541 /// Null is returned if the indices are invalid for the specified
1544 static const Type *getIndexedType(const Type *Agg,
1545 const unsigned *Idx, unsigned NumIdx);
1547 template<typename InputIterator>
1548 static const Type *getIndexedType(const Type *Ptr,
1549 InputIterator IdxBegin,
1550 InputIterator IdxEnd,
1551 // This argument ensures that we
1552 // have an iterator we can do
1553 // arithmetic on in constant time
1554 std::random_access_iterator_tag) {
1555 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1558 // This requires that the iterator points to contiguous memory.
1559 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1561 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1564 /// Constructors - Create a extractvalue instruction with a base aggregate
1565 /// value and a list of indices. The first ctor can optionally insert before
1566 /// an existing instruction, the second appends the new instruction to the
1567 /// specified BasicBlock.
1568 template<typename InputIterator>
1569 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1570 InputIterator IdxEnd,
1571 const std::string &NameStr,
1572 Instruction *InsertBefore);
1573 template<typename InputIterator>
1574 inline ExtractValueInst(Value *Agg,
1575 InputIterator IdxBegin, InputIterator IdxEnd,
1576 const std::string &NameStr, BasicBlock *InsertAtEnd);
1578 // allocate space for exactly one operand
1579 void *operator new(size_t s) {
1580 return User::operator new(s, 1);
1584 template<typename InputIterator>
1585 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1586 InputIterator IdxEnd,
1587 const std::string &NameStr = "",
1588 Instruction *InsertBefore = 0) {
1590 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1592 template<typename InputIterator>
1593 static ExtractValueInst *Create(Value *Agg,
1594 InputIterator IdxBegin, InputIterator IdxEnd,
1595 const std::string &NameStr,
1596 BasicBlock *InsertAtEnd) {
1597 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1600 /// Constructors - These two creators are convenience methods because one
1601 /// index extractvalue instructions are much more common than those with
1603 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1604 const std::string &NameStr = "",
1605 Instruction *InsertBefore = 0) {
1606 unsigned Idxs[1] = { Idx };
1607 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1609 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1610 const std::string &NameStr,
1611 BasicBlock *InsertAtEnd) {
1612 unsigned Idxs[1] = { Idx };
1613 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1616 virtual ExtractValueInst *clone() const;
1618 // getType - Overload to return most specific pointer type...
1619 const PointerType *getType() const {
1620 return reinterpret_cast<const PointerType*>(Instruction::getType());
1623 /// getIndexedType - Returns the type of the element that would be extracted
1624 /// with an extractvalue instruction with the specified parameters.
1626 /// Null is returned if the indices are invalid for the specified
1629 template<typename InputIterator>
1630 static const Type *getIndexedType(const Type *Ptr,
1631 InputIterator IdxBegin,
1632 InputIterator IdxEnd) {
1633 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1634 typename std::iterator_traits<InputIterator>::
1635 iterator_category());
1637 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1639 typedef const unsigned* idx_iterator;
1640 inline idx_iterator idx_begin() const { return Indices.begin(); }
1641 inline idx_iterator idx_end() const { return Indices.end(); }
1643 Value *getAggregateOperand() {
1644 return getOperand(0);
1646 const Value *getAggregateOperand() const {
1647 return getOperand(0);
1649 static unsigned getAggregateOperandIndex() {
1650 return 0U; // get index for modifying correct operand
1653 unsigned getNumIndices() const { // Note: always non-negative
1654 return (unsigned)Indices.size();
1657 bool hasIndices() const {
1661 // Methods for support type inquiry through isa, cast, and dyn_cast:
1662 static inline bool classof(const ExtractValueInst *) { return true; }
1663 static inline bool classof(const Instruction *I) {
1664 return I->getOpcode() == Instruction::ExtractValue;
1666 static inline bool classof(const Value *V) {
1667 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1671 template<typename InputIterator>
1672 ExtractValueInst::ExtractValueInst(Value *Agg,
1673 InputIterator IdxBegin,
1674 InputIterator IdxEnd,
1675 const std::string &NameStr,
1676 Instruction *InsertBefore)
1677 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1679 ExtractValue, Agg, InsertBefore) {
1680 init(IdxBegin, IdxEnd, NameStr,
1681 typename std::iterator_traits<InputIterator>::iterator_category());
1683 template<typename InputIterator>
1684 ExtractValueInst::ExtractValueInst(Value *Agg,
1685 InputIterator IdxBegin,
1686 InputIterator IdxEnd,
1687 const std::string &NameStr,
1688 BasicBlock *InsertAtEnd)
1689 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1691 ExtractValue, Agg, InsertAtEnd) {
1692 init(IdxBegin, IdxEnd, NameStr,
1693 typename std::iterator_traits<InputIterator>::iterator_category());
1697 //===----------------------------------------------------------------------===//
1698 // InsertValueInst Class
1699 //===----------------------------------------------------------------------===//
1701 /// InsertValueInst - This instruction inserts a struct field of array element
1702 /// value into an aggregate value.
1704 class InsertValueInst : public Instruction {
1705 SmallVector<unsigned, 4> Indices;
1707 void *operator new(size_t, unsigned); // Do not implement
1708 InsertValueInst(const InsertValueInst &IVI);
1709 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1710 const std::string &NameStr);
1711 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1713 template<typename InputIterator>
1714 void init(Value *Agg, Value *Val,
1715 InputIterator IdxBegin, InputIterator IdxEnd,
1716 const std::string &NameStr,
1717 // This argument ensures that we have an iterator we can
1718 // do arithmetic on in constant time
1719 std::random_access_iterator_tag) {
1720 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1722 // There's no fundamental reason why we require at least one index
1723 // (other than weirdness with &*IdxBegin being invalid; see
1724 // getelementptr's init routine for example). But there's no
1725 // present need to support it.
1726 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1728 // This requires that the iterator points to contiguous memory.
1729 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1730 // we have to build an array here
1733 /// Constructors - Create a insertvalue instruction with a base aggregate
1734 /// value, a value to insert, and a list of indices. The first ctor can
1735 /// optionally insert before an existing instruction, the second appends
1736 /// the new instruction to the specified BasicBlock.
1737 template<typename InputIterator>
1738 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1739 InputIterator IdxEnd,
1740 const std::string &NameStr,
1741 Instruction *InsertBefore);
1742 template<typename InputIterator>
1743 inline InsertValueInst(Value *Agg, Value *Val,
1744 InputIterator IdxBegin, InputIterator IdxEnd,
1745 const std::string &NameStr, BasicBlock *InsertAtEnd);
1747 /// Constructors - These two constructors are convenience methods because one
1748 /// and two index insertvalue instructions are so common.
1749 InsertValueInst(Value *Agg, Value *Val,
1750 unsigned Idx, const std::string &NameStr = "",
1751 Instruction *InsertBefore = 0);
1752 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1753 const std::string &NameStr, BasicBlock *InsertAtEnd);
1755 // allocate space for exactly two operands
1756 void *operator new(size_t s) {
1757 return User::operator new(s, 2);
1760 template<typename InputIterator>
1761 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1762 InputIterator IdxEnd,
1763 const std::string &NameStr = "",
1764 Instruction *InsertBefore = 0) {
1765 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1766 NameStr, InsertBefore);
1768 template<typename InputIterator>
1769 static InsertValueInst *Create(Value *Agg, Value *Val,
1770 InputIterator IdxBegin, InputIterator IdxEnd,
1771 const std::string &NameStr,
1772 BasicBlock *InsertAtEnd) {
1773 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1774 NameStr, InsertAtEnd);
1777 /// Constructors - These two creators are convenience methods because one
1778 /// index insertvalue instructions are much more common than those with
1780 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1781 const std::string &NameStr = "",
1782 Instruction *InsertBefore = 0) {
1783 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1785 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1786 const std::string &NameStr,
1787 BasicBlock *InsertAtEnd) {
1788 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1791 virtual InsertValueInst *clone() const;
1793 /// Transparently provide more efficient getOperand methods.
1794 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1796 // getType - Overload to return most specific pointer type...
1797 const PointerType *getType() const {
1798 return reinterpret_cast<const PointerType*>(Instruction::getType());
1801 typedef const unsigned* idx_iterator;
1802 inline idx_iterator idx_begin() const { return Indices.begin(); }
1803 inline idx_iterator idx_end() const { return Indices.end(); }
1805 Value *getAggregateOperand() {
1806 return getOperand(0);
1808 const Value *getAggregateOperand() const {
1809 return getOperand(0);
1811 static unsigned getAggregateOperandIndex() {
1812 return 0U; // get index for modifying correct operand
1815 Value *getInsertedValueOperand() {
1816 return getOperand(1);
1818 const Value *getInsertedValueOperand() const {
1819 return getOperand(1);
1821 static unsigned getInsertedValueOperandIndex() {
1822 return 1U; // get index for modifying correct operand
1825 unsigned getNumIndices() const { // Note: always non-negative
1826 return (unsigned)Indices.size();
1829 bool hasIndices() const {
1833 // Methods for support type inquiry through isa, cast, and dyn_cast:
1834 static inline bool classof(const InsertValueInst *) { return true; }
1835 static inline bool classof(const Instruction *I) {
1836 return I->getOpcode() == Instruction::InsertValue;
1838 static inline bool classof(const Value *V) {
1839 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1844 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1847 template<typename InputIterator>
1848 InsertValueInst::InsertValueInst(Value *Agg,
1850 InputIterator IdxBegin,
1851 InputIterator IdxEnd,
1852 const std::string &NameStr,
1853 Instruction *InsertBefore)
1854 : Instruction(Agg->getType(), InsertValue,
1855 OperandTraits<InsertValueInst>::op_begin(this),
1857 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1858 typename std::iterator_traits<InputIterator>::iterator_category());
1860 template<typename InputIterator>
1861 InsertValueInst::InsertValueInst(Value *Agg,
1863 InputIterator IdxBegin,
1864 InputIterator IdxEnd,
1865 const std::string &NameStr,
1866 BasicBlock *InsertAtEnd)
1867 : Instruction(Agg->getType(), InsertValue,
1868 OperandTraits<InsertValueInst>::op_begin(this),
1870 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1871 typename std::iterator_traits<InputIterator>::iterator_category());
1874 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1876 //===----------------------------------------------------------------------===//
1878 //===----------------------------------------------------------------------===//
1880 // PHINode - The PHINode class is used to represent the magical mystical PHI
1881 // node, that can not exist in nature, but can be synthesized in a computer
1882 // scientist's overactive imagination.
1884 class PHINode : public Instruction {
1885 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1886 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1887 /// the number actually in use.
1888 unsigned ReservedSpace;
1889 PHINode(const PHINode &PN);
1890 // allocate space for exactly zero operands
1891 void *operator new(size_t s) {
1892 return User::operator new(s, 0);
1894 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1895 Instruction *InsertBefore = 0)
1896 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1901 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1902 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1907 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1908 Instruction *InsertBefore = 0) {
1909 return new PHINode(Ty, NameStr, InsertBefore);
1911 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1912 BasicBlock *InsertAtEnd) {
1913 return new PHINode(Ty, NameStr, InsertAtEnd);
1917 /// reserveOperandSpace - This method can be used to avoid repeated
1918 /// reallocation of PHI operand lists by reserving space for the correct
1919 /// number of operands before adding them. Unlike normal vector reserves,
1920 /// this method can also be used to trim the operand space.
1921 void reserveOperandSpace(unsigned NumValues) {
1922 resizeOperands(NumValues*2);
1925 virtual PHINode *clone() const;
1927 /// Provide fast operand accessors
1928 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1930 /// getNumIncomingValues - Return the number of incoming edges
1932 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1934 /// getIncomingValue - Return incoming value number x
1936 Value *getIncomingValue(unsigned i) const {
1937 assert(i*2 < getNumOperands() && "Invalid value number!");
1938 return getOperand(i*2);
1940 void setIncomingValue(unsigned i, Value *V) {
1941 assert(i*2 < getNumOperands() && "Invalid value number!");
1944 unsigned getOperandNumForIncomingValue(unsigned i) {
1948 /// getIncomingBlock - Return incoming basic block corresponding
1949 /// to value use iterator
1951 template <typename U>
1952 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1953 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1954 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1956 /// getIncomingBlock - Return incoming basic block number x
1958 BasicBlock *getIncomingBlock(unsigned i) const {
1959 return static_cast<BasicBlock*>(getOperand(i*2+1));
1961 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1962 setOperand(i*2+1, BB);
1964 unsigned getOperandNumForIncomingBlock(unsigned i) {
1968 /// addIncoming - Add an incoming value to the end of the PHI list
1970 void addIncoming(Value *V, BasicBlock *BB) {
1971 assert(V && "PHI node got a null value!");
1972 assert(BB && "PHI node got a null basic block!");
1973 assert(getType() == V->getType() &&
1974 "All operands to PHI node must be the same type as the PHI node!");
1975 unsigned OpNo = NumOperands;
1976 if (OpNo+2 > ReservedSpace)
1977 resizeOperands(0); // Get more space!
1978 // Initialize some new operands.
1979 NumOperands = OpNo+2;
1980 OperandList[OpNo] = V;
1981 OperandList[OpNo+1] = BB;
1984 /// removeIncomingValue - Remove an incoming value. This is useful if a
1985 /// predecessor basic block is deleted. The value removed is returned.
1987 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1988 /// is true), the PHI node is destroyed and any uses of it are replaced with
1989 /// dummy values. The only time there should be zero incoming values to a PHI
1990 /// node is when the block is dead, so this strategy is sound.
1992 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1994 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1995 int Idx = getBasicBlockIndex(BB);
1996 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1997 return removeIncomingValue(Idx, DeletePHIIfEmpty);
2000 /// getBasicBlockIndex - Return the first index of the specified basic
2001 /// block in the value list for this PHI. Returns -1 if no instance.
2003 int getBasicBlockIndex(const BasicBlock *BB) const {
2004 Use *OL = OperandList;
2005 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
2006 if (OL[i+1].get() == BB) return i/2;
2010 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
2011 return getIncomingValue(getBasicBlockIndex(BB));
2014 /// hasConstantValue - If the specified PHI node always merges together the
2015 /// same value, return the value, otherwise return null.
2017 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
2019 /// Methods for support type inquiry through isa, cast, and dyn_cast:
2020 static inline bool classof(const PHINode *) { return true; }
2021 static inline bool classof(const Instruction *I) {
2022 return I->getOpcode() == Instruction::PHI;
2024 static inline bool classof(const Value *V) {
2025 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2028 void resizeOperands(unsigned NumOperands);
2032 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
2035 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
2038 //===----------------------------------------------------------------------===//
2040 //===----------------------------------------------------------------------===//
2042 //===---------------------------------------------------------------------------
2043 /// ReturnInst - Return a value (possibly void), from a function. Execution
2044 /// does not continue in this function any longer.
2046 class ReturnInst : public TerminatorInst {
2047 ReturnInst(const ReturnInst &RI);
2050 // ReturnInst constructors:
2051 // ReturnInst() - 'ret void' instruction
2052 // ReturnInst( null) - 'ret void' instruction
2053 // ReturnInst(Value* X) - 'ret X' instruction
2054 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
2055 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
2056 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
2057 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2059 // NOTE: If the Value* passed is of type void then the constructor behaves as
2060 // if it was passed NULL.
2061 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
2062 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
2063 explicit ReturnInst(BasicBlock *InsertAtEnd);
2065 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
2066 return new(!!retVal) ReturnInst(retVal, InsertBefore);
2068 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
2069 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
2071 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
2072 return new(0) ReturnInst(InsertAtEnd);
2074 virtual ~ReturnInst();
2076 virtual ReturnInst *clone() const;
2078 /// Provide fast operand accessors
2079 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2081 /// Convenience accessor
2082 Value *getReturnValue(unsigned n = 0) const {
2083 return n < getNumOperands()
2088 unsigned getNumSuccessors() const { return 0; }
2090 // Methods for support type inquiry through isa, cast, and dyn_cast:
2091 static inline bool classof(const ReturnInst *) { return true; }
2092 static inline bool classof(const Instruction *I) {
2093 return (I->getOpcode() == Instruction::Ret);
2095 static inline bool classof(const Value *V) {
2096 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2099 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2100 virtual unsigned getNumSuccessorsV() const;
2101 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2105 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2108 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2110 //===----------------------------------------------------------------------===//
2112 //===----------------------------------------------------------------------===//
2114 //===---------------------------------------------------------------------------
2115 /// BranchInst - Conditional or Unconditional Branch instruction.
2117 class BranchInst : public TerminatorInst {
2118 /// Ops list - Branches are strange. The operands are ordered:
2119 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
2120 /// they don't have to check for cond/uncond branchness.
2121 BranchInst(const BranchInst &BI);
2123 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2124 // BranchInst(BB *B) - 'br B'
2125 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2126 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2127 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2128 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2129 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2130 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2131 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2132 Instruction *InsertBefore = 0);
2133 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2134 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2135 BasicBlock *InsertAtEnd);
2137 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2138 return new(1) BranchInst(IfTrue, InsertBefore);
2140 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2141 Value *Cond, Instruction *InsertBefore = 0) {
2142 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2144 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2145 return new(1) BranchInst(IfTrue, InsertAtEnd);
2147 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2148 Value *Cond, BasicBlock *InsertAtEnd) {
2149 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2153 if (NumOperands == 1)
2154 NumOperands = (unsigned)((Use*)this - OperandList);
2157 /// Transparently provide more efficient getOperand methods.
2158 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2160 virtual BranchInst *clone() const;
2162 bool isUnconditional() const { return getNumOperands() == 1; }
2163 bool isConditional() const { return getNumOperands() == 3; }
2165 Value *getCondition() const {
2166 assert(isConditional() && "Cannot get condition of an uncond branch!");
2167 return getOperand(2);
2170 void setCondition(Value *V) {
2171 assert(isConditional() && "Cannot set condition of unconditional branch!");
2175 // setUnconditionalDest - Change the current branch to an unconditional branch
2176 // targeting the specified block.
2177 // FIXME: Eliminate this ugly method.
2178 void setUnconditionalDest(BasicBlock *Dest) {
2180 if (isConditional()) { // Convert this to an uncond branch.
2187 unsigned getNumSuccessors() const { return 1+isConditional(); }
2189 BasicBlock *getSuccessor(unsigned i) const {
2190 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2191 if (Value *V = getOperand(i))
2192 return cast<BasicBlock>(V);
2196 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2197 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2198 setOperand(idx, NewSucc);
2201 // Methods for support type inquiry through isa, cast, and dyn_cast:
2202 static inline bool classof(const BranchInst *) { return true; }
2203 static inline bool classof(const Instruction *I) {
2204 return (I->getOpcode() == Instruction::Br);
2206 static inline bool classof(const Value *V) {
2207 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2210 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2211 virtual unsigned getNumSuccessorsV() const;
2212 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2216 struct OperandTraits<BranchInst> : HungoffOperandTraits<> {
2217 // we need to access operands via OperandList, since
2218 // the NumOperands may change from 3 to 1
2219 static inline void *allocate(unsigned); // FIXME
2222 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2224 //===----------------------------------------------------------------------===//
2226 //===----------------------------------------------------------------------===//
2228 //===---------------------------------------------------------------------------
2229 /// SwitchInst - Multiway switch
2231 class SwitchInst : public TerminatorInst {
2232 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2233 unsigned ReservedSpace;
2234 // Operand[0] = Value to switch on
2235 // Operand[1] = Default basic block destination
2236 // Operand[2n ] = Value to match
2237 // Operand[2n+1] = BasicBlock to go to on match
2238 SwitchInst(const SwitchInst &RI);
2239 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2240 void resizeOperands(unsigned No);
2241 // allocate space for exactly zero operands
2242 void *operator new(size_t s) {
2243 return User::operator new(s, 0);
2245 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2246 /// switch on and a default destination. The number of additional cases can
2247 /// be specified here to make memory allocation more efficient. This
2248 /// constructor can also autoinsert before another instruction.
2249 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2250 Instruction *InsertBefore = 0);
2252 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2253 /// switch on and a default destination. The number of additional cases can
2254 /// be specified here to make memory allocation more efficient. This
2255 /// constructor also autoinserts at the end of the specified BasicBlock.
2256 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2257 BasicBlock *InsertAtEnd);
2259 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2260 unsigned NumCases, Instruction *InsertBefore = 0) {
2261 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2263 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2264 unsigned NumCases, BasicBlock *InsertAtEnd) {
2265 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2269 /// Provide fast operand accessors
2270 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2272 // Accessor Methods for Switch stmt
2273 Value *getCondition() const { return getOperand(0); }
2274 void setCondition(Value *V) { setOperand(0, V); }
2276 BasicBlock *getDefaultDest() const {
2277 return cast<BasicBlock>(getOperand(1));
2280 /// getNumCases - return the number of 'cases' in this switch instruction.
2281 /// Note that case #0 is always the default case.
2282 unsigned getNumCases() const {
2283 return getNumOperands()/2;
2286 /// getCaseValue - Return the specified case value. Note that case #0, the
2287 /// default destination, does not have a case value.
2288 ConstantInt *getCaseValue(unsigned i) {
2289 assert(i && i < getNumCases() && "Illegal case value to get!");
2290 return getSuccessorValue(i);
2293 /// getCaseValue - Return the specified case value. Note that case #0, the
2294 /// default destination, does not have a case value.
2295 const ConstantInt *getCaseValue(unsigned i) const {
2296 assert(i && i < getNumCases() && "Illegal case value to get!");
2297 return getSuccessorValue(i);
2300 /// findCaseValue - Search all of the case values for the specified constant.
2301 /// If it is explicitly handled, return the case number of it, otherwise
2302 /// return 0 to indicate that it is handled by the default handler.
2303 unsigned findCaseValue(const ConstantInt *C) const {
2304 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2305 if (getCaseValue(i) == C)
2310 /// findCaseDest - Finds the unique case value for a given successor. Returns
2311 /// null if the successor is not found, not unique, or is the default case.
2312 ConstantInt *findCaseDest(BasicBlock *BB) {
2313 if (BB == getDefaultDest()) return NULL;
2315 ConstantInt *CI = NULL;
2316 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2317 if (getSuccessor(i) == BB) {
2318 if (CI) return NULL; // Multiple cases lead to BB.
2319 else CI = getCaseValue(i);
2325 /// addCase - Add an entry to the switch instruction...
2327 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2329 /// removeCase - This method removes the specified successor from the switch
2330 /// instruction. Note that this cannot be used to remove the default
2331 /// destination (successor #0).
2333 void removeCase(unsigned idx);
2335 virtual SwitchInst *clone() const;
2337 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2338 BasicBlock *getSuccessor(unsigned idx) const {
2339 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2340 return cast<BasicBlock>(getOperand(idx*2+1));
2342 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2343 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2344 setOperand(idx*2+1, NewSucc);
2347 // getSuccessorValue - Return the value associated with the specified
2349 ConstantInt *getSuccessorValue(unsigned idx) const {
2350 assert(idx < getNumSuccessors() && "Successor # out of range!");
2351 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2354 // Methods for support type inquiry through isa, cast, and dyn_cast:
2355 static inline bool classof(const SwitchInst *) { return true; }
2356 static inline bool classof(const Instruction *I) {
2357 return I->getOpcode() == Instruction::Switch;
2359 static inline bool classof(const Value *V) {
2360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2363 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2364 virtual unsigned getNumSuccessorsV() const;
2365 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2369 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2372 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2375 //===----------------------------------------------------------------------===//
2377 //===----------------------------------------------------------------------===//
2379 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2380 /// calling convention of the call.
2382 class InvokeInst : public TerminatorInst {
2383 AttrListPtr AttributeList;
2384 InvokeInst(const InvokeInst &BI);
2385 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2386 Value* const *Args, unsigned NumArgs);
2388 template<typename InputIterator>
2389 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2390 InputIterator ArgBegin, InputIterator ArgEnd,
2391 const std::string &NameStr,
2392 // This argument ensures that we have an iterator we can
2393 // do arithmetic on in constant time
2394 std::random_access_iterator_tag) {
2395 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2397 // This requires that the iterator points to contiguous memory.
2398 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2402 /// Construct an InvokeInst given a range of arguments.
2403 /// InputIterator must be a random-access iterator pointing to
2404 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2405 /// made for random-accessness but not for contiguous storage as
2406 /// that would incur runtime overhead.
2408 /// @brief Construct an InvokeInst from a range of arguments
2409 template<typename InputIterator>
2410 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2411 InputIterator ArgBegin, InputIterator ArgEnd,
2413 const std::string &NameStr, Instruction *InsertBefore);
2415 /// Construct an InvokeInst given a range of arguments.
2416 /// InputIterator must be a random-access iterator pointing to
2417 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2418 /// made for random-accessness but not for contiguous storage as
2419 /// that would incur runtime overhead.
2421 /// @brief Construct an InvokeInst from a range of arguments
2422 template<typename InputIterator>
2423 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2424 InputIterator ArgBegin, InputIterator ArgEnd,
2426 const std::string &NameStr, BasicBlock *InsertAtEnd);
2428 template<typename InputIterator>
2429 static InvokeInst *Create(Value *Func,
2430 BasicBlock *IfNormal, BasicBlock *IfException,
2431 InputIterator ArgBegin, InputIterator ArgEnd,
2432 const std::string &NameStr = "",
2433 Instruction *InsertBefore = 0) {
2434 unsigned Values(ArgEnd - ArgBegin + 3);
2435 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2436 Values, NameStr, InsertBefore);
2438 template<typename InputIterator>
2439 static InvokeInst *Create(Value *Func,
2440 BasicBlock *IfNormal, BasicBlock *IfException,
2441 InputIterator ArgBegin, InputIterator ArgEnd,
2442 const std::string &NameStr,
2443 BasicBlock *InsertAtEnd) {
2444 unsigned Values(ArgEnd - ArgBegin + 3);
2445 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2446 Values, NameStr, InsertAtEnd);
2449 virtual InvokeInst *clone() const;
2451 /// Provide fast operand accessors
2452 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2454 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2456 unsigned getCallingConv() const { return SubclassData; }
2457 void setCallingConv(unsigned CC) {
2461 /// getAttributes - Return the parameter attributes for this invoke.
2463 const AttrListPtr &getAttributes() const { return AttributeList; }
2465 /// setAttributes - Set the parameter attributes for this invoke.
2467 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2469 /// addAttribute - adds the attribute to the list of attributes.
2470 void addAttribute(unsigned i, Attributes attr);
2472 /// removeAttribute - removes the attribute from the list of attributes.
2473 void removeAttribute(unsigned i, Attributes attr);
2475 /// @brief Determine whether the call or the callee has the given attribute.
2476 bool paramHasAttr(unsigned i, Attributes attr) const;
2478 /// @brief Extract the alignment for a call or parameter (0=unknown).
2479 unsigned getParamAlignment(unsigned i) const {
2480 return AttributeList.getParamAlignment(i);
2483 /// @brief Determine if the call does not access memory.
2484 bool doesNotAccessMemory() const {
2485 return paramHasAttr(0, Attribute::ReadNone);
2487 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2488 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2489 else removeAttribute(~0, Attribute::ReadNone);
2492 /// @brief Determine if the call does not access or only reads memory.
2493 bool onlyReadsMemory() const {
2494 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2496 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2497 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2498 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2501 /// @brief Determine if the call cannot return.
2502 bool doesNotReturn() const {
2503 return paramHasAttr(~0, Attribute::NoReturn);
2505 void setDoesNotReturn(bool DoesNotReturn = true) {
2506 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2507 else removeAttribute(~0, Attribute::NoReturn);
2510 /// @brief Determine if the call cannot unwind.
2511 bool doesNotThrow() const {
2512 return paramHasAttr(~0, Attribute::NoUnwind);
2514 void setDoesNotThrow(bool DoesNotThrow = true) {
2515 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2516 else removeAttribute(~0, Attribute::NoUnwind);
2519 /// @brief Determine if the call returns a structure through first
2520 /// pointer argument.
2521 bool hasStructRetAttr() const {
2522 // Be friendly and also check the callee.
2523 return paramHasAttr(1, Attribute::StructRet);
2526 /// @brief Determine if any call argument is an aggregate passed by value.
2527 bool hasByValArgument() const {
2528 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2531 /// getCalledFunction - Return the function called, or null if this is an
2532 /// indirect function invocation.
2534 Function *getCalledFunction() const {
2535 return dyn_cast<Function>(getOperand(0));
2538 /// getCalledValue - Get a pointer to the function that is invoked by this
2540 const Value *getCalledValue() const { return getOperand(0); }
2541 Value *getCalledValue() { return getOperand(0); }
2543 // get*Dest - Return the destination basic blocks...
2544 BasicBlock *getNormalDest() const {
2545 return cast<BasicBlock>(getOperand(1));
2547 BasicBlock *getUnwindDest() const {
2548 return cast<BasicBlock>(getOperand(2));
2550 void setNormalDest(BasicBlock *B) {
2554 void setUnwindDest(BasicBlock *B) {
2558 BasicBlock *getSuccessor(unsigned i) const {
2559 assert(i < 2 && "Successor # out of range for invoke!");
2560 return i == 0 ? getNormalDest() : getUnwindDest();
2563 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2564 assert(idx < 2 && "Successor # out of range for invoke!");
2565 setOperand(idx+1, NewSucc);
2568 unsigned getNumSuccessors() const { return 2; }
2570 // Methods for support type inquiry through isa, cast, and dyn_cast:
2571 static inline bool classof(const InvokeInst *) { return true; }
2572 static inline bool classof(const Instruction *I) {
2573 return (I->getOpcode() == Instruction::Invoke);
2575 static inline bool classof(const Value *V) {
2576 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2579 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2580 virtual unsigned getNumSuccessorsV() const;
2581 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2585 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2588 template<typename InputIterator>
2589 InvokeInst::InvokeInst(Value *Func,
2590 BasicBlock *IfNormal, BasicBlock *IfException,
2591 InputIterator ArgBegin, InputIterator ArgEnd,
2593 const std::string &NameStr, Instruction *InsertBefore)
2594 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2595 ->getElementType())->getReturnType(),
2596 Instruction::Invoke,
2597 OperandTraits<InvokeInst>::op_end(this) - Values,
2598 Values, InsertBefore) {
2599 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2600 typename std::iterator_traits<InputIterator>::iterator_category());
2602 template<typename InputIterator>
2603 InvokeInst::InvokeInst(Value *Func,
2604 BasicBlock *IfNormal, BasicBlock *IfException,
2605 InputIterator ArgBegin, InputIterator ArgEnd,
2607 const std::string &NameStr, BasicBlock *InsertAtEnd)
2608 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2609 ->getElementType())->getReturnType(),
2610 Instruction::Invoke,
2611 OperandTraits<InvokeInst>::op_end(this) - Values,
2612 Values, InsertAtEnd) {
2613 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2614 typename std::iterator_traits<InputIterator>::iterator_category());
2617 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2619 //===----------------------------------------------------------------------===//
2621 //===----------------------------------------------------------------------===//
2623 //===---------------------------------------------------------------------------
2624 /// UnwindInst - Immediately exit the current function, unwinding the stack
2625 /// until an invoke instruction is found.
2627 class UnwindInst : public TerminatorInst {
2628 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2630 // allocate space for exactly zero operands
2631 void *operator new(size_t s) {
2632 return User::operator new(s, 0);
2634 explicit UnwindInst(Instruction *InsertBefore = 0);
2635 explicit UnwindInst(BasicBlock *InsertAtEnd);
2637 virtual UnwindInst *clone() const;
2639 unsigned getNumSuccessors() const { return 0; }
2641 // Methods for support type inquiry through isa, cast, and dyn_cast:
2642 static inline bool classof(const UnwindInst *) { return true; }
2643 static inline bool classof(const Instruction *I) {
2644 return I->getOpcode() == Instruction::Unwind;
2646 static inline bool classof(const Value *V) {
2647 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2650 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2651 virtual unsigned getNumSuccessorsV() const;
2652 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2655 //===----------------------------------------------------------------------===//
2656 // UnreachableInst Class
2657 //===----------------------------------------------------------------------===//
2659 //===---------------------------------------------------------------------------
2660 /// UnreachableInst - This function has undefined behavior. In particular, the
2661 /// presence of this instruction indicates some higher level knowledge that the
2662 /// end of the block cannot be reached.
2664 class UnreachableInst : public TerminatorInst {
2665 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2667 // allocate space for exactly zero operands
2668 void *operator new(size_t s) {
2669 return User::operator new(s, 0);
2671 explicit UnreachableInst(Instruction *InsertBefore = 0);
2672 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2674 virtual UnreachableInst *clone() const;
2676 unsigned getNumSuccessors() const { return 0; }
2678 // Methods for support type inquiry through isa, cast, and dyn_cast:
2679 static inline bool classof(const UnreachableInst *) { return true; }
2680 static inline bool classof(const Instruction *I) {
2681 return I->getOpcode() == Instruction::Unreachable;
2683 static inline bool classof(const Value *V) {
2684 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2687 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2688 virtual unsigned getNumSuccessorsV() const;
2689 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2692 //===----------------------------------------------------------------------===//
2694 //===----------------------------------------------------------------------===//
2696 /// @brief This class represents a truncation of integer types.
2697 class TruncInst : public CastInst {
2698 /// Private copy constructor
2699 TruncInst(const TruncInst &CI)
2700 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2703 /// @brief Constructor with insert-before-instruction semantics
2705 Value *S, ///< The value to be truncated
2706 const Type *Ty, ///< The (smaller) type to truncate to
2707 const std::string &NameStr = "", ///< A name for the new instruction
2708 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2711 /// @brief Constructor with insert-at-end-of-block semantics
2713 Value *S, ///< The value to be truncated
2714 const Type *Ty, ///< The (smaller) type to truncate to
2715 const std::string &NameStr, ///< A name for the new instruction
2716 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2719 /// @brief Clone an identical TruncInst
2720 virtual CastInst *clone() const;
2722 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2723 static inline bool classof(const TruncInst *) { return true; }
2724 static inline bool classof(const Instruction *I) {
2725 return I->getOpcode() == Trunc;
2727 static inline bool classof(const Value *V) {
2728 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2732 //===----------------------------------------------------------------------===//
2734 //===----------------------------------------------------------------------===//
2736 /// @brief This class represents zero extension of integer types.
2737 class ZExtInst : public CastInst {
2738 /// @brief Private copy constructor
2739 ZExtInst(const ZExtInst &CI)
2740 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2743 /// @brief Constructor with insert-before-instruction semantics
2745 Value *S, ///< The value to be zero extended
2746 const Type *Ty, ///< The type to zero extend to
2747 const std::string &NameStr = "", ///< A name for the new instruction
2748 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2751 /// @brief Constructor with insert-at-end semantics.
2753 Value *S, ///< The value to be zero extended
2754 const Type *Ty, ///< The type to zero extend to
2755 const std::string &NameStr, ///< A name for the new instruction
2756 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2759 /// @brief Clone an identical ZExtInst
2760 virtual CastInst *clone() const;
2762 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2763 static inline bool classof(const ZExtInst *) { return true; }
2764 static inline bool classof(const Instruction *I) {
2765 return I->getOpcode() == ZExt;
2767 static inline bool classof(const Value *V) {
2768 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2772 //===----------------------------------------------------------------------===//
2774 //===----------------------------------------------------------------------===//
2776 /// @brief This class represents a sign extension of integer types.
2777 class SExtInst : public CastInst {
2778 /// @brief Private copy constructor
2779 SExtInst(const SExtInst &CI)
2780 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2783 /// @brief Constructor with insert-before-instruction semantics
2785 Value *S, ///< The value to be sign extended
2786 const Type *Ty, ///< The type to sign extend to
2787 const std::string &NameStr = "", ///< A name for the new instruction
2788 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2791 /// @brief Constructor with insert-at-end-of-block semantics
2793 Value *S, ///< The value to be sign extended
2794 const Type *Ty, ///< The type to sign extend to
2795 const std::string &NameStr, ///< A name for the new instruction
2796 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2799 /// @brief Clone an identical SExtInst
2800 virtual CastInst *clone() const;
2802 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2803 static inline bool classof(const SExtInst *) { return true; }
2804 static inline bool classof(const Instruction *I) {
2805 return I->getOpcode() == SExt;
2807 static inline bool classof(const Value *V) {
2808 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2812 //===----------------------------------------------------------------------===//
2813 // FPTruncInst Class
2814 //===----------------------------------------------------------------------===//
2816 /// @brief This class represents a truncation of floating point types.
2817 class FPTruncInst : public CastInst {
2818 FPTruncInst(const FPTruncInst &CI)
2819 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2822 /// @brief Constructor with insert-before-instruction semantics
2824 Value *S, ///< The value to be truncated
2825 const Type *Ty, ///< The type to truncate to
2826 const std::string &NameStr = "", ///< A name for the new instruction
2827 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2830 /// @brief Constructor with insert-before-instruction semantics
2832 Value *S, ///< The value to be truncated
2833 const Type *Ty, ///< The type to truncate to
2834 const std::string &NameStr, ///< A name for the new instruction
2835 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2838 /// @brief Clone an identical FPTruncInst
2839 virtual CastInst *clone() const;
2841 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2842 static inline bool classof(const FPTruncInst *) { return true; }
2843 static inline bool classof(const Instruction *I) {
2844 return I->getOpcode() == FPTrunc;
2846 static inline bool classof(const Value *V) {
2847 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2851 //===----------------------------------------------------------------------===//
2853 //===----------------------------------------------------------------------===//
2855 /// @brief This class represents an extension of floating point types.
2856 class FPExtInst : public CastInst {
2857 FPExtInst(const FPExtInst &CI)
2858 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2861 /// @brief Constructor with insert-before-instruction semantics
2863 Value *S, ///< The value to be extended
2864 const Type *Ty, ///< The type to extend to
2865 const std::string &NameStr = "", ///< A name for the new instruction
2866 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2869 /// @brief Constructor with insert-at-end-of-block semantics
2871 Value *S, ///< The value to be extended
2872 const Type *Ty, ///< The type to extend to
2873 const std::string &NameStr, ///< A name for the new instruction
2874 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2877 /// @brief Clone an identical FPExtInst
2878 virtual CastInst *clone() const;
2880 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2881 static inline bool classof(const FPExtInst *) { return true; }
2882 static inline bool classof(const Instruction *I) {
2883 return I->getOpcode() == FPExt;
2885 static inline bool classof(const Value *V) {
2886 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2890 //===----------------------------------------------------------------------===//
2892 //===----------------------------------------------------------------------===//
2894 /// @brief This class represents a cast unsigned integer to floating point.
2895 class UIToFPInst : public CastInst {
2896 UIToFPInst(const UIToFPInst &CI)
2897 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2900 /// @brief Constructor with insert-before-instruction semantics
2902 Value *S, ///< The value to be converted
2903 const Type *Ty, ///< The type to convert to
2904 const std::string &NameStr = "", ///< A name for the new instruction
2905 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2908 /// @brief Constructor with insert-at-end-of-block semantics
2910 Value *S, ///< The value to be converted
2911 const Type *Ty, ///< The type to convert to
2912 const std::string &NameStr, ///< A name for the new instruction
2913 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2916 /// @brief Clone an identical UIToFPInst
2917 virtual CastInst *clone() const;
2919 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2920 static inline bool classof(const UIToFPInst *) { return true; }
2921 static inline bool classof(const Instruction *I) {
2922 return I->getOpcode() == UIToFP;
2924 static inline bool classof(const Value *V) {
2925 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2929 //===----------------------------------------------------------------------===//
2931 //===----------------------------------------------------------------------===//
2933 /// @brief This class represents a cast from signed integer to floating point.
2934 class SIToFPInst : public CastInst {
2935 SIToFPInst(const SIToFPInst &CI)
2936 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2939 /// @brief Constructor with insert-before-instruction semantics
2941 Value *S, ///< The value to be converted
2942 const Type *Ty, ///< The type to convert to
2943 const std::string &NameStr = "", ///< A name for the new instruction
2944 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2947 /// @brief Constructor with insert-at-end-of-block semantics
2949 Value *S, ///< The value to be converted
2950 const Type *Ty, ///< The type to convert to
2951 const std::string &NameStr, ///< A name for the new instruction
2952 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2955 /// @brief Clone an identical SIToFPInst
2956 virtual CastInst *clone() const;
2958 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2959 static inline bool classof(const SIToFPInst *) { return true; }
2960 static inline bool classof(const Instruction *I) {
2961 return I->getOpcode() == SIToFP;
2963 static inline bool classof(const Value *V) {
2964 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2968 //===----------------------------------------------------------------------===//
2970 //===----------------------------------------------------------------------===//
2972 /// @brief This class represents a cast from floating point to unsigned integer
2973 class FPToUIInst : public CastInst {
2974 FPToUIInst(const FPToUIInst &CI)
2975 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2978 /// @brief Constructor with insert-before-instruction semantics
2980 Value *S, ///< The value to be converted
2981 const Type *Ty, ///< The type to convert to
2982 const std::string &NameStr = "", ///< A name for the new instruction
2983 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2986 /// @brief Constructor with insert-at-end-of-block semantics
2988 Value *S, ///< The value to be converted
2989 const Type *Ty, ///< The type to convert to
2990 const std::string &NameStr, ///< A name for the new instruction
2991 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2994 /// @brief Clone an identical FPToUIInst
2995 virtual CastInst *clone() const;
2997 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2998 static inline bool classof(const FPToUIInst *) { return true; }
2999 static inline bool classof(const Instruction *I) {
3000 return I->getOpcode() == FPToUI;
3002 static inline bool classof(const Value *V) {
3003 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3007 //===----------------------------------------------------------------------===//
3009 //===----------------------------------------------------------------------===//
3011 /// @brief This class represents a cast from floating point to signed integer.
3012 class FPToSIInst : public CastInst {
3013 FPToSIInst(const FPToSIInst &CI)
3014 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
3017 /// @brief Constructor with insert-before-instruction semantics
3019 Value *S, ///< The value to be converted
3020 const Type *Ty, ///< The type to convert to
3021 const std::string &NameStr = "", ///< A name for the new instruction
3022 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3025 /// @brief Constructor with insert-at-end-of-block semantics
3027 Value *S, ///< The value to be converted
3028 const Type *Ty, ///< The type to convert to
3029 const std::string &NameStr, ///< A name for the new instruction
3030 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3033 /// @brief Clone an identical FPToSIInst
3034 virtual CastInst *clone() const;
3036 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3037 static inline bool classof(const FPToSIInst *) { return true; }
3038 static inline bool classof(const Instruction *I) {
3039 return I->getOpcode() == FPToSI;
3041 static inline bool classof(const Value *V) {
3042 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3046 //===----------------------------------------------------------------------===//
3047 // IntToPtrInst Class
3048 //===----------------------------------------------------------------------===//
3050 /// @brief This class represents a cast from an integer to a pointer.
3051 class IntToPtrInst : public CastInst {
3052 IntToPtrInst(const IntToPtrInst &CI)
3053 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
3056 /// @brief Constructor with insert-before-instruction semantics
3058 Value *S, ///< The value to be converted
3059 const Type *Ty, ///< The type to convert to
3060 const std::string &NameStr = "", ///< A name for the new instruction
3061 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3064 /// @brief Constructor with insert-at-end-of-block semantics
3066 Value *S, ///< The value to be converted
3067 const Type *Ty, ///< The type to convert to
3068 const std::string &NameStr, ///< A name for the new instruction
3069 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3072 /// @brief Clone an identical IntToPtrInst
3073 virtual CastInst *clone() const;
3075 // Methods for support type inquiry through isa, cast, and dyn_cast:
3076 static inline bool classof(const IntToPtrInst *) { return true; }
3077 static inline bool classof(const Instruction *I) {
3078 return I->getOpcode() == IntToPtr;
3080 static inline bool classof(const Value *V) {
3081 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3085 //===----------------------------------------------------------------------===//
3086 // PtrToIntInst Class
3087 //===----------------------------------------------------------------------===//
3089 /// @brief This class represents a cast from a pointer to an integer
3090 class PtrToIntInst : public CastInst {
3091 PtrToIntInst(const PtrToIntInst &CI)
3092 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3095 /// @brief Constructor with insert-before-instruction semantics
3097 Value *S, ///< The value to be converted
3098 const Type *Ty, ///< The type to convert to
3099 const std::string &NameStr = "", ///< A name for the new instruction
3100 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3103 /// @brief Constructor with insert-at-end-of-block semantics
3105 Value *S, ///< The value to be converted
3106 const Type *Ty, ///< The type to convert to
3107 const std::string &NameStr, ///< A name for the new instruction
3108 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3111 /// @brief Clone an identical PtrToIntInst
3112 virtual CastInst *clone() const;
3114 // Methods for support type inquiry through isa, cast, and dyn_cast:
3115 static inline bool classof(const PtrToIntInst *) { return true; }
3116 static inline bool classof(const Instruction *I) {
3117 return I->getOpcode() == PtrToInt;
3119 static inline bool classof(const Value *V) {
3120 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3124 //===----------------------------------------------------------------------===//
3125 // BitCastInst Class
3126 //===----------------------------------------------------------------------===//
3128 /// @brief This class represents a no-op cast from one type to another.
3129 class BitCastInst : public CastInst {
3130 BitCastInst(const BitCastInst &CI)
3131 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3134 /// @brief Constructor with insert-before-instruction semantics
3136 Value *S, ///< The value to be casted
3137 const Type *Ty, ///< The type to casted to
3138 const std::string &NameStr = "", ///< A name for the new instruction
3139 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3142 /// @brief Constructor with insert-at-end-of-block semantics
3144 Value *S, ///< The value to be casted
3145 const Type *Ty, ///< The type to casted to
3146 const std::string &NameStr, ///< A name for the new instruction
3147 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3150 /// @brief Clone an identical BitCastInst
3151 virtual CastInst *clone() const;
3153 // Methods for support type inquiry through isa, cast, and dyn_cast:
3154 static inline bool classof(const BitCastInst *) { return true; }
3155 static inline bool classof(const Instruction *I) {
3156 return I->getOpcode() == BitCast;
3158 static inline bool classof(const Value *V) {
3159 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3163 } // End llvm namespace