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/CallingConv.h"
23 #include "llvm/ADT/SmallVector.h"
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 /// AllocaInst - an instruction to allocate memory on the stack
40 class AllocaInst : public UnaryInstruction {
42 virtual AllocaInst *clone_impl() const;
44 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
45 const Twine &Name = "", Instruction *InsertBefore = 0);
46 AllocaInst(const Type *Ty, Value *ArraySize,
47 const Twine &Name, BasicBlock *InsertAtEnd);
49 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
50 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
52 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
53 const Twine &Name = "", Instruction *InsertBefore = 0);
54 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
55 const Twine &Name, BasicBlock *InsertAtEnd);
57 // Out of line virtual method, so the vtable, etc. has a home.
58 virtual ~AllocaInst();
60 /// isArrayAllocation - Return true if there is an allocation size parameter
61 /// to the allocation instruction that is not 1.
63 bool isArrayAllocation() const;
65 /// getArraySize - Get the number of elements allocated. For a simple
66 /// allocation of a single element, this will return a constant 1 value.
68 const Value *getArraySize() const { return getOperand(0); }
69 Value *getArraySize() { return getOperand(0); }
71 /// getType - Overload to return most specific pointer type
73 const PointerType *getType() const {
74 return reinterpret_cast<const PointerType*>(Instruction::getType());
77 /// getAllocatedType - Return the type that is being allocated by the
80 const Type *getAllocatedType() const;
82 /// getAlignment - Return the alignment of the memory that is being allocated
83 /// by the instruction.
85 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
86 void setAlignment(unsigned Align);
88 /// isStaticAlloca - Return true if this alloca is in the entry block of the
89 /// function and is a constant size. If so, the code generator will fold it
90 /// into the prolog/epilog code, so it is basically free.
91 bool isStaticAlloca() const;
93 // Methods for support type inquiry through isa, cast, and dyn_cast:
94 static inline bool classof(const AllocaInst *) { return true; }
95 static inline bool classof(const Instruction *I) {
96 return (I->getOpcode() == Instruction::Alloca);
98 static inline bool classof(const Value *V) {
99 return isa<Instruction>(V) && classof(cast<Instruction>(V));
104 //===----------------------------------------------------------------------===//
106 //===----------------------------------------------------------------------===//
108 /// LoadInst - an instruction for reading from memory. This uses the
109 /// SubclassData field in Value to store whether or not the load is volatile.
111 class LoadInst : public UnaryInstruction {
114 virtual LoadInst *clone_impl() const;
116 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
117 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
118 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
119 Instruction *InsertBefore = 0);
120 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
121 unsigned Align, Instruction *InsertBefore = 0);
122 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
123 BasicBlock *InsertAtEnd);
124 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
125 unsigned Align, BasicBlock *InsertAtEnd);
127 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
128 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
129 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
130 bool isVolatile = false, Instruction *InsertBefore = 0);
131 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
132 BasicBlock *InsertAtEnd);
134 /// isVolatile - Return true if this is a load from a volatile memory
137 bool isVolatile() const { return SubclassData & 1; }
139 /// setVolatile - Specify whether this is a volatile load or not.
141 void setVolatile(bool V) {
142 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
145 /// getAlignment - Return the alignment of the access that is being performed
147 unsigned getAlignment() const {
148 return (1 << (SubclassData>>1)) >> 1;
151 void setAlignment(unsigned Align);
153 Value *getPointerOperand() { return getOperand(0); }
154 const Value *getPointerOperand() const { return getOperand(0); }
155 static unsigned getPointerOperandIndex() { return 0U; }
157 unsigned getPointerAddressSpace() const {
158 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
162 // Methods for support type inquiry through isa, cast, and dyn_cast:
163 static inline bool classof(const LoadInst *) { return true; }
164 static inline bool classof(const Instruction *I) {
165 return I->getOpcode() == Instruction::Load;
167 static inline bool classof(const Value *V) {
168 return isa<Instruction>(V) && classof(cast<Instruction>(V));
173 //===----------------------------------------------------------------------===//
175 //===----------------------------------------------------------------------===//
177 /// StoreInst - an instruction for storing to memory
179 class StoreInst : public Instruction {
180 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
183 virtual StoreInst *clone_impl() const;
185 // allocate space for exactly two operands
186 void *operator new(size_t s) {
187 return User::operator new(s, 2);
189 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
190 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
191 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
192 Instruction *InsertBefore = 0);
193 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
194 unsigned Align, Instruction *InsertBefore = 0);
195 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
196 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
197 unsigned Align, BasicBlock *InsertAtEnd);
200 /// isVolatile - Return true if this is a load from a volatile memory
203 bool isVolatile() const { return SubclassData & 1; }
205 /// setVolatile - Specify whether this is a volatile load or not.
207 void setVolatile(bool V) {
208 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
211 /// Transparently provide more efficient getOperand methods.
212 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
214 /// getAlignment - Return the alignment of the access that is being performed
216 unsigned getAlignment() const {
217 return (1 << (SubclassData>>1)) >> 1;
220 void setAlignment(unsigned Align);
222 Value *getPointerOperand() { return getOperand(1); }
223 const Value *getPointerOperand() const { return getOperand(1); }
224 static unsigned getPointerOperandIndex() { return 1U; }
226 unsigned getPointerAddressSpace() const {
227 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
230 // Methods for support type inquiry through isa, cast, and dyn_cast:
231 static inline bool classof(const StoreInst *) { return true; }
232 static inline bool classof(const Instruction *I) {
233 return I->getOpcode() == Instruction::Store;
235 static inline bool classof(const Value *V) {
236 return isa<Instruction>(V) && classof(cast<Instruction>(V));
241 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
244 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
246 //===----------------------------------------------------------------------===//
247 // GetElementPtrInst Class
248 //===----------------------------------------------------------------------===//
250 // checkType - Simple wrapper function to give a better assertion failure
251 // message on bad indexes for a gep instruction.
253 static inline const Type *checkType(const Type *Ty) {
254 assert(Ty && "Invalid GetElementPtrInst indices for type!");
258 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
259 /// access elements of arrays and structs
261 class GetElementPtrInst : public Instruction {
262 GetElementPtrInst(const GetElementPtrInst &GEPI);
263 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
264 const Twine &NameStr);
265 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
267 template<typename InputIterator>
268 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
269 const Twine &NameStr,
270 // This argument ensures that we have an iterator we can
271 // do arithmetic on in constant time
272 std::random_access_iterator_tag) {
273 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
276 // This requires that the iterator points to contiguous memory.
277 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
278 // we have to build an array here
281 init(Ptr, 0, NumIdx, NameStr);
285 /// getIndexedType - Returns the type of the element that would be loaded with
286 /// a load instruction with the specified parameters.
288 /// Null is returned if the indices are invalid for the specified
291 template<typename InputIterator>
292 static const Type *getIndexedType(const Type *Ptr,
293 InputIterator IdxBegin,
294 InputIterator IdxEnd,
295 // This argument ensures that we
296 // have an iterator we can do
297 // arithmetic on in constant time
298 std::random_access_iterator_tag) {
299 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
302 // This requires that the iterator points to contiguous memory.
303 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
305 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
308 /// Constructors - Create a getelementptr instruction with a base pointer an
309 /// list of indices. The first ctor can optionally insert before an existing
310 /// instruction, the second appends the new instruction to the specified
312 template<typename InputIterator>
313 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
314 InputIterator IdxEnd,
316 const Twine &NameStr,
317 Instruction *InsertBefore);
318 template<typename InputIterator>
319 inline GetElementPtrInst(Value *Ptr,
320 InputIterator IdxBegin, InputIterator IdxEnd,
322 const Twine &NameStr, BasicBlock *InsertAtEnd);
324 /// Constructors - These two constructors are convenience methods because one
325 /// and two index getelementptr instructions are so common.
326 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
327 Instruction *InsertBefore = 0);
328 GetElementPtrInst(Value *Ptr, Value *Idx,
329 const Twine &NameStr, BasicBlock *InsertAtEnd);
331 virtual GetElementPtrInst *clone_impl() const;
333 template<typename InputIterator>
334 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
335 InputIterator IdxEnd,
336 const Twine &NameStr = "",
337 Instruction *InsertBefore = 0) {
338 typename std::iterator_traits<InputIterator>::difference_type Values =
339 1 + std::distance(IdxBegin, IdxEnd);
341 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
343 template<typename InputIterator>
344 static GetElementPtrInst *Create(Value *Ptr,
345 InputIterator IdxBegin, InputIterator IdxEnd,
346 const Twine &NameStr,
347 BasicBlock *InsertAtEnd) {
348 typename std::iterator_traits<InputIterator>::difference_type Values =
349 1 + std::distance(IdxBegin, IdxEnd);
351 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
354 /// Constructors - These two creators are convenience methods because one
355 /// index getelementptr instructions are so common.
356 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
357 const Twine &NameStr = "",
358 Instruction *InsertBefore = 0) {
359 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
361 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
362 const Twine &NameStr,
363 BasicBlock *InsertAtEnd) {
364 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
367 /// Create an "inbounds" getelementptr. See the documentation for the
368 /// "inbounds" flag in LangRef.html for details.
369 template<typename InputIterator>
370 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
371 InputIterator IdxEnd,
372 const Twine &NameStr = "",
373 Instruction *InsertBefore = 0) {
374 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
375 NameStr, InsertBefore);
376 GEP->setIsInBounds(true);
379 template<typename InputIterator>
380 static GetElementPtrInst *CreateInBounds(Value *Ptr,
381 InputIterator IdxBegin,
382 InputIterator IdxEnd,
383 const Twine &NameStr,
384 BasicBlock *InsertAtEnd) {
385 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
386 NameStr, InsertAtEnd);
387 GEP->setIsInBounds(true);
390 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
391 const Twine &NameStr = "",
392 Instruction *InsertBefore = 0) {
393 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
394 GEP->setIsInBounds(true);
397 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
398 const Twine &NameStr,
399 BasicBlock *InsertAtEnd) {
400 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
401 GEP->setIsInBounds(true);
405 /// Transparently provide more efficient getOperand methods.
406 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
408 // getType - Overload to return most specific pointer type...
409 const PointerType *getType() const {
410 return reinterpret_cast<const PointerType*>(Instruction::getType());
413 /// getIndexedType - Returns the type of the element that would be loaded with
414 /// a load instruction with the specified parameters.
416 /// Null is returned if the indices are invalid for the specified
419 template<typename InputIterator>
420 static const Type *getIndexedType(const Type *Ptr,
421 InputIterator IdxBegin,
422 InputIterator IdxEnd) {
423 return getIndexedType(Ptr, IdxBegin, IdxEnd,
424 typename std::iterator_traits<InputIterator>::
425 iterator_category());
428 static const Type *getIndexedType(const Type *Ptr,
429 Value* const *Idx, unsigned NumIdx);
431 static const Type *getIndexedType(const Type *Ptr,
432 uint64_t const *Idx, unsigned NumIdx);
434 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
436 inline op_iterator idx_begin() { return op_begin()+1; }
437 inline const_op_iterator idx_begin() const { return op_begin()+1; }
438 inline op_iterator idx_end() { return op_end(); }
439 inline const_op_iterator idx_end() const { return op_end(); }
441 Value *getPointerOperand() {
442 return getOperand(0);
444 const Value *getPointerOperand() const {
445 return getOperand(0);
447 static unsigned getPointerOperandIndex() {
448 return 0U; // get index for modifying correct operand
451 unsigned getPointerAddressSpace() const {
452 return cast<PointerType>(getType())->getAddressSpace();
455 /// getPointerOperandType - Method to return the pointer operand as a
457 const PointerType *getPointerOperandType() const {
458 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
462 unsigned getNumIndices() const { // Note: always non-negative
463 return getNumOperands() - 1;
466 bool hasIndices() const {
467 return getNumOperands() > 1;
470 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
471 /// zeros. If so, the result pointer and the first operand have the same
472 /// value, just potentially different types.
473 bool hasAllZeroIndices() const;
475 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
476 /// constant integers. If so, the result pointer and the first operand have
477 /// a constant offset between them.
478 bool hasAllConstantIndices() const;
480 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
481 /// See LangRef.html for the meaning of inbounds on a getelementptr.
482 void setIsInBounds(bool b = true);
484 /// isInBounds - Determine whether the GEP has the inbounds flag.
485 bool isInBounds() const;
487 // Methods for support type inquiry through isa, cast, and dyn_cast:
488 static inline bool classof(const GetElementPtrInst *) { return true; }
489 static inline bool classof(const Instruction *I) {
490 return (I->getOpcode() == Instruction::GetElementPtr);
492 static inline bool classof(const Value *V) {
493 return isa<Instruction>(V) && classof(cast<Instruction>(V));
498 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
501 template<typename InputIterator>
502 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
503 InputIterator IdxBegin,
504 InputIterator IdxEnd,
506 const Twine &NameStr,
507 Instruction *InsertBefore)
508 : Instruction(PointerType::get(checkType(
509 getIndexedType(Ptr->getType(),
511 cast<PointerType>(Ptr->getType())
512 ->getAddressSpace()),
514 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
515 Values, InsertBefore) {
516 init(Ptr, IdxBegin, IdxEnd, NameStr,
517 typename std::iterator_traits<InputIterator>::iterator_category());
519 template<typename InputIterator>
520 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
521 InputIterator IdxBegin,
522 InputIterator IdxEnd,
524 const Twine &NameStr,
525 BasicBlock *InsertAtEnd)
526 : Instruction(PointerType::get(checkType(
527 getIndexedType(Ptr->getType(),
529 cast<PointerType>(Ptr->getType())
530 ->getAddressSpace()),
532 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
533 Values, InsertAtEnd) {
534 init(Ptr, IdxBegin, IdxEnd, NameStr,
535 typename std::iterator_traits<InputIterator>::iterator_category());
539 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
542 //===----------------------------------------------------------------------===//
544 //===----------------------------------------------------------------------===//
546 /// This instruction compares its operands according to the predicate given
547 /// to the constructor. It only operates on integers or pointers. The operands
548 /// must be identical types.
549 /// @brief Represent an integer comparison operator.
550 class ICmpInst: public CmpInst {
552 /// @brief Clone an indentical ICmpInst
553 virtual ICmpInst *clone_impl() const;
555 /// @brief Constructor with insert-before-instruction semantics.
557 Instruction *InsertBefore, ///< Where to insert
558 Predicate pred, ///< The predicate to use for the comparison
559 Value *LHS, ///< The left-hand-side of the expression
560 Value *RHS, ///< The right-hand-side of the expression
561 const Twine &NameStr = "" ///< Name of the instruction
562 ) : CmpInst(makeCmpResultType(LHS->getType()),
563 Instruction::ICmp, pred, LHS, RHS, NameStr,
565 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
566 pred <= CmpInst::LAST_ICMP_PREDICATE &&
567 "Invalid ICmp predicate value");
568 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
569 "Both operands to ICmp instruction are not of the same type!");
570 // Check that the operands are the right type
571 assert((getOperand(0)->getType()->isIntOrIntVector() ||
572 isa<PointerType>(getOperand(0)->getType())) &&
573 "Invalid operand types for ICmp instruction");
576 /// @brief Constructor with insert-at-end semantics.
578 BasicBlock &InsertAtEnd, ///< Block to insert into.
579 Predicate pred, ///< The predicate to use for the comparison
580 Value *LHS, ///< The left-hand-side of the expression
581 Value *RHS, ///< The right-hand-side of the expression
582 const Twine &NameStr = "" ///< Name of the instruction
583 ) : CmpInst(makeCmpResultType(LHS->getType()),
584 Instruction::ICmp, pred, LHS, RHS, NameStr,
586 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
587 pred <= CmpInst::LAST_ICMP_PREDICATE &&
588 "Invalid ICmp predicate value");
589 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
590 "Both operands to ICmp instruction are not of the same type!");
591 // Check that the operands are the right type
592 assert((getOperand(0)->getType()->isIntOrIntVector() ||
593 isa<PointerType>(getOperand(0)->getType())) &&
594 "Invalid operand types for ICmp instruction");
597 /// @brief Constructor with no-insertion semantics
599 Predicate pred, ///< The predicate to use for the comparison
600 Value *LHS, ///< The left-hand-side of the expression
601 Value *RHS, ///< The right-hand-side of the expression
602 const Twine &NameStr = "" ///< Name of the instruction
603 ) : CmpInst(makeCmpResultType(LHS->getType()),
604 Instruction::ICmp, pred, LHS, RHS, NameStr) {
605 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
606 pred <= CmpInst::LAST_ICMP_PREDICATE &&
607 "Invalid ICmp predicate value");
608 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
609 "Both operands to ICmp instruction are not of the same type!");
610 // Check that the operands are the right type
611 assert((getOperand(0)->getType()->isIntOrIntVector() ||
612 isa<PointerType>(getOperand(0)->getType())) &&
613 "Invalid operand types for ICmp instruction");
616 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
617 /// @returns the predicate that would be the result if the operand were
618 /// regarded as signed.
619 /// @brief Return the signed version of the predicate
620 Predicate getSignedPredicate() const {
621 return getSignedPredicate(getPredicate());
624 /// This is a static version that you can use without an instruction.
625 /// @brief Return the signed version of the predicate.
626 static Predicate getSignedPredicate(Predicate pred);
628 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
629 /// @returns the predicate that would be the result if the operand were
630 /// regarded as unsigned.
631 /// @brief Return the unsigned version of the predicate
632 Predicate getUnsignedPredicate() const {
633 return getUnsignedPredicate(getPredicate());
636 /// This is a static version that you can use without an instruction.
637 /// @brief Return the unsigned version of the predicate.
638 static Predicate getUnsignedPredicate(Predicate pred);
640 /// isEquality - Return true if this predicate is either EQ or NE. This also
641 /// tests for commutativity.
642 static bool isEquality(Predicate P) {
643 return P == ICMP_EQ || P == ICMP_NE;
646 /// isEquality - Return true if this predicate is either EQ or NE. This also
647 /// tests for commutativity.
648 bool isEquality() const {
649 return isEquality(getPredicate());
652 /// @returns true if the predicate of this ICmpInst is commutative
653 /// @brief Determine if this relation is commutative.
654 bool isCommutative() const { return isEquality(); }
656 /// isRelational - Return true if the predicate is relational (not EQ or NE).
658 bool isRelational() const {
659 return !isEquality();
662 /// isRelational - Return true if the predicate is relational (not EQ or NE).
664 static bool isRelational(Predicate P) {
665 return !isEquality(P);
668 /// Initialize a set of values that all satisfy the predicate with C.
669 /// @brief Make a ConstantRange for a relation with a constant value.
670 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
672 /// Exchange the two operands to this instruction in such a way that it does
673 /// not modify the semantics of the instruction. The predicate value may be
674 /// changed to retain the same result if the predicate is order dependent
676 /// @brief Swap operands and adjust predicate.
677 void swapOperands() {
678 SubclassData = getSwappedPredicate();
679 Op<0>().swap(Op<1>());
682 // Methods for support type inquiry through isa, cast, and dyn_cast:
683 static inline bool classof(const ICmpInst *) { return true; }
684 static inline bool classof(const Instruction *I) {
685 return I->getOpcode() == Instruction::ICmp;
687 static inline bool classof(const Value *V) {
688 return isa<Instruction>(V) && classof(cast<Instruction>(V));
693 //===----------------------------------------------------------------------===//
695 //===----------------------------------------------------------------------===//
697 /// This instruction compares its operands according to the predicate given
698 /// to the constructor. It only operates on floating point values or packed
699 /// vectors of floating point values. The operands must be identical types.
700 /// @brief Represents a floating point comparison operator.
701 class FCmpInst: public CmpInst {
703 /// @brief Clone an indentical FCmpInst
704 virtual FCmpInst *clone_impl() const;
706 /// @brief Constructor with insert-before-instruction semantics.
708 Instruction *InsertBefore, ///< Where to insert
709 Predicate pred, ///< The predicate to use for the comparison
710 Value *LHS, ///< The left-hand-side of the expression
711 Value *RHS, ///< The right-hand-side of the expression
712 const Twine &NameStr = "" ///< Name of the instruction
713 ) : CmpInst(makeCmpResultType(LHS->getType()),
714 Instruction::FCmp, pred, LHS, RHS, NameStr,
716 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
717 "Invalid FCmp predicate value");
718 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
719 "Both operands to FCmp instruction are not of the same type!");
720 // Check that the operands are the right type
721 assert(getOperand(0)->getType()->isFPOrFPVector() &&
722 "Invalid operand types for FCmp instruction");
725 /// @brief Constructor with insert-at-end semantics.
727 BasicBlock &InsertAtEnd, ///< Block to insert into.
728 Predicate pred, ///< The predicate to use for the comparison
729 Value *LHS, ///< The left-hand-side of the expression
730 Value *RHS, ///< The right-hand-side of the expression
731 const Twine &NameStr = "" ///< Name of the instruction
732 ) : CmpInst(makeCmpResultType(LHS->getType()),
733 Instruction::FCmp, pred, LHS, RHS, NameStr,
735 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
736 "Invalid FCmp predicate value");
737 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
738 "Both operands to FCmp instruction are not of the same type!");
739 // Check that the operands are the right type
740 assert(getOperand(0)->getType()->isFPOrFPVector() &&
741 "Invalid operand types for FCmp instruction");
744 /// @brief Constructor with no-insertion semantics
746 Predicate pred, ///< The predicate to use for the comparison
747 Value *LHS, ///< The left-hand-side of the expression
748 Value *RHS, ///< The right-hand-side of the expression
749 const Twine &NameStr = "" ///< Name of the instruction
750 ) : CmpInst(makeCmpResultType(LHS->getType()),
751 Instruction::FCmp, pred, LHS, RHS, NameStr) {
752 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
753 "Invalid FCmp predicate value");
754 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
755 "Both operands to FCmp instruction are not of the same type!");
756 // Check that the operands are the right type
757 assert(getOperand(0)->getType()->isFPOrFPVector() &&
758 "Invalid operand types for FCmp instruction");
761 /// @returns true if the predicate of this instruction is EQ or NE.
762 /// @brief Determine if this is an equality predicate.
763 bool isEquality() const {
764 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
765 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
768 /// @returns true if the predicate of this instruction is commutative.
769 /// @brief Determine if this is a commutative predicate.
770 bool isCommutative() const {
771 return isEquality() ||
772 SubclassData == FCMP_FALSE ||
773 SubclassData == FCMP_TRUE ||
774 SubclassData == FCMP_ORD ||
775 SubclassData == FCMP_UNO;
778 /// @returns true if the predicate is relational (not EQ or NE).
779 /// @brief Determine if this a relational predicate.
780 bool isRelational() const { return !isEquality(); }
782 /// Exchange the two operands to this instruction in such a way that it does
783 /// not modify the semantics of the instruction. The predicate value may be
784 /// changed to retain the same result if the predicate is order dependent
786 /// @brief Swap operands and adjust predicate.
787 void swapOperands() {
788 SubclassData = getSwappedPredicate();
789 Op<0>().swap(Op<1>());
792 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
793 static inline bool classof(const FCmpInst *) { return true; }
794 static inline bool classof(const Instruction *I) {
795 return I->getOpcode() == Instruction::FCmp;
797 static inline bool classof(const Value *V) {
798 return isa<Instruction>(V) && classof(cast<Instruction>(V));
802 //===----------------------------------------------------------------------===//
804 //===----------------------------------------------------------------------===//
805 /// CallInst - This class represents a function call, abstracting a target
806 /// machine's calling convention. This class uses low bit of the SubClassData
807 /// field to indicate whether or not this is a tail call. The rest of the bits
808 /// hold the calling convention of the call.
811 class CallInst : public Instruction {
812 AttrListPtr AttributeList; ///< parameter attributes for call
813 CallInst(const CallInst &CI);
814 void init(Value *Func, Value* const *Params, unsigned NumParams);
815 void init(Value *Func, Value *Actual1, Value *Actual2);
816 void init(Value *Func, Value *Actual);
817 void init(Value *Func);
819 template<typename InputIterator>
820 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
821 const Twine &NameStr,
822 // This argument ensures that we have an iterator we can
823 // do arithmetic on in constant time
824 std::random_access_iterator_tag) {
825 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
827 // This requires that the iterator points to contiguous memory.
828 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
832 /// Construct a CallInst given a range of arguments. InputIterator
833 /// must be a random-access iterator pointing to contiguous storage
834 /// (e.g. a std::vector<>::iterator). Checks are made for
835 /// random-accessness but not for contiguous storage as that would
836 /// incur runtime overhead.
837 /// @brief Construct a CallInst from a range of arguments
838 template<typename InputIterator>
839 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
840 const Twine &NameStr, Instruction *InsertBefore);
842 /// Construct a CallInst given a range of arguments. InputIterator
843 /// must be a random-access iterator pointing to contiguous storage
844 /// (e.g. a std::vector<>::iterator). Checks are made for
845 /// random-accessness but not for contiguous storage as that would
846 /// incur runtime overhead.
847 /// @brief Construct a CallInst from a range of arguments
848 template<typename InputIterator>
849 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
850 const Twine &NameStr, BasicBlock *InsertAtEnd);
852 CallInst(Value *F, Value *Actual, const Twine &NameStr,
853 Instruction *InsertBefore);
854 CallInst(Value *F, Value *Actual, const Twine &NameStr,
855 BasicBlock *InsertAtEnd);
856 explicit CallInst(Value *F, const Twine &NameStr,
857 Instruction *InsertBefore);
858 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
860 virtual CallInst *clone_impl() const;
862 template<typename InputIterator>
863 static CallInst *Create(Value *Func,
864 InputIterator ArgBegin, InputIterator ArgEnd,
865 const Twine &NameStr = "",
866 Instruction *InsertBefore = 0) {
867 return new((unsigned)(ArgEnd - ArgBegin + 1))
868 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
870 template<typename InputIterator>
871 static CallInst *Create(Value *Func,
872 InputIterator ArgBegin, InputIterator ArgEnd,
873 const Twine &NameStr, BasicBlock *InsertAtEnd) {
874 return new((unsigned)(ArgEnd - ArgBegin + 1))
875 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
877 static CallInst *Create(Value *F, Value *Actual,
878 const Twine &NameStr = "",
879 Instruction *InsertBefore = 0) {
880 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
882 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
883 BasicBlock *InsertAtEnd) {
884 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
886 static CallInst *Create(Value *F, const Twine &NameStr = "",
887 Instruction *InsertBefore = 0) {
888 return new(1) CallInst(F, NameStr, InsertBefore);
890 static CallInst *Create(Value *F, const Twine &NameStr,
891 BasicBlock *InsertAtEnd) {
892 return new(1) CallInst(F, NameStr, InsertAtEnd);
894 /// CreateMalloc - Generate the IR for a call to malloc:
895 /// 1. Compute the malloc call's argument as the specified type's size,
896 /// possibly multiplied by the array size if the array size is not
898 /// 2. Call malloc with that argument.
899 /// 3. Bitcast the result of the malloc call to the specified type.
900 static Instruction *CreateMalloc(Instruction *InsertBefore,
901 const Type *IntPtrTy, const Type *AllocTy,
902 Value *ArraySize = 0,
903 const Twine &Name = "");
904 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
905 const Type *IntPtrTy, const Type *AllocTy,
906 Value *ArraySize = 0, Function* MallocF = 0,
907 const Twine &Name = "");
908 /// CreateFree - Generate the IR for a call to the builtin free function.
909 static void CreateFree(Value* Source, Instruction *InsertBefore);
910 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
914 bool isTailCall() const { return SubclassData & 1; }
915 void setTailCall(bool isTC = true) {
916 SubclassData = (SubclassData & ~1) | unsigned(isTC);
919 /// Provide fast operand accessors
920 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
922 /// getCallingConv/setCallingConv - Get or set the calling convention of this
924 CallingConv::ID getCallingConv() const {
925 return static_cast<CallingConv::ID>(SubclassData >> 1);
927 void setCallingConv(CallingConv::ID CC) {
928 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
931 /// getAttributes - Return the parameter attributes for this call.
933 const AttrListPtr &getAttributes() const { return AttributeList; }
935 /// setAttributes - Set the parameter attributes for this call.
937 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
939 /// addAttribute - adds the attribute to the list of attributes.
940 void addAttribute(unsigned i, Attributes attr);
942 /// removeAttribute - removes the attribute from the list of attributes.
943 void removeAttribute(unsigned i, Attributes attr);
945 /// @brief Determine whether the call or the callee has the given attribute.
946 bool paramHasAttr(unsigned i, Attributes attr) const;
948 /// @brief Extract the alignment for a call or parameter (0=unknown).
949 unsigned getParamAlignment(unsigned i) const {
950 return AttributeList.getParamAlignment(i);
953 /// @brief Determine if the call does not access memory.
954 bool doesNotAccessMemory() const {
955 return paramHasAttr(~0, Attribute::ReadNone);
957 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
958 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
959 else removeAttribute(~0, Attribute::ReadNone);
962 /// @brief Determine if the call does not access or only reads memory.
963 bool onlyReadsMemory() const {
964 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
966 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
967 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
968 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
971 /// @brief Determine if the call cannot return.
972 bool doesNotReturn() const {
973 return paramHasAttr(~0, Attribute::NoReturn);
975 void setDoesNotReturn(bool DoesNotReturn = true) {
976 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
977 else removeAttribute(~0, Attribute::NoReturn);
980 /// @brief Determine if the call cannot unwind.
981 bool doesNotThrow() const {
982 return paramHasAttr(~0, Attribute::NoUnwind);
984 void setDoesNotThrow(bool DoesNotThrow = true) {
985 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
986 else removeAttribute(~0, Attribute::NoUnwind);
989 /// @brief Determine if the call returns a structure through first
990 /// pointer argument.
991 bool hasStructRetAttr() const {
992 // Be friendly and also check the callee.
993 return paramHasAttr(1, Attribute::StructRet);
996 /// @brief Determine if any call argument is an aggregate passed by value.
997 bool hasByValArgument() const {
998 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1001 /// getCalledFunction - Return the function called, or null if this is an
1002 /// indirect function invocation.
1004 Function *getCalledFunction() const {
1005 return dyn_cast<Function>(Op<0>());
1008 /// getCalledValue - Get a pointer to the function that is invoked by this
1010 const Value *getCalledValue() const { return Op<0>(); }
1011 Value *getCalledValue() { return Op<0>(); }
1013 /// setCalledFunction - Set the function called.
1014 void setCalledFunction(Value* Fn) {
1018 // Methods for support type inquiry through isa, cast, and dyn_cast:
1019 static inline bool classof(const CallInst *) { return true; }
1020 static inline bool classof(const Instruction *I) {
1021 return I->getOpcode() == Instruction::Call;
1023 static inline bool classof(const Value *V) {
1024 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1029 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1032 template<typename InputIterator>
1033 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1034 const Twine &NameStr, BasicBlock *InsertAtEnd)
1035 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1036 ->getElementType())->getReturnType(),
1038 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1039 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1040 init(Func, ArgBegin, ArgEnd, NameStr,
1041 typename std::iterator_traits<InputIterator>::iterator_category());
1044 template<typename InputIterator>
1045 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1046 const Twine &NameStr, Instruction *InsertBefore)
1047 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1048 ->getElementType())->getReturnType(),
1050 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1051 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1052 init(Func, ArgBegin, ArgEnd, NameStr,
1053 typename std::iterator_traits<InputIterator>::iterator_category());
1056 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1058 //===----------------------------------------------------------------------===//
1060 //===----------------------------------------------------------------------===//
1062 /// SelectInst - This class represents the LLVM 'select' instruction.
1064 class SelectInst : public Instruction {
1065 void init(Value *C, Value *S1, Value *S2) {
1066 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1072 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1073 Instruction *InsertBefore)
1074 : Instruction(S1->getType(), Instruction::Select,
1075 &Op<0>(), 3, InsertBefore) {
1079 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1080 BasicBlock *InsertAtEnd)
1081 : Instruction(S1->getType(), Instruction::Select,
1082 &Op<0>(), 3, InsertAtEnd) {
1087 virtual SelectInst *clone_impl() const;
1089 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1090 const Twine &NameStr = "",
1091 Instruction *InsertBefore = 0) {
1092 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1094 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1095 const Twine &NameStr,
1096 BasicBlock *InsertAtEnd) {
1097 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1100 const Value *getCondition() const { return Op<0>(); }
1101 const Value *getTrueValue() const { return Op<1>(); }
1102 const Value *getFalseValue() const { return Op<2>(); }
1103 Value *getCondition() { return Op<0>(); }
1104 Value *getTrueValue() { return Op<1>(); }
1105 Value *getFalseValue() { return Op<2>(); }
1107 /// areInvalidOperands - Return a string if the specified operands are invalid
1108 /// for a select operation, otherwise return null.
1109 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1111 /// Transparently provide more efficient getOperand methods.
1112 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1114 OtherOps getOpcode() const {
1115 return static_cast<OtherOps>(Instruction::getOpcode());
1118 // Methods for support type inquiry through isa, cast, and dyn_cast:
1119 static inline bool classof(const SelectInst *) { return true; }
1120 static inline bool classof(const Instruction *I) {
1121 return I->getOpcode() == Instruction::Select;
1123 static inline bool classof(const Value *V) {
1124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1129 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1132 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1134 //===----------------------------------------------------------------------===//
1136 //===----------------------------------------------------------------------===//
1138 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1139 /// an argument of the specified type given a va_list and increments that list
1141 class VAArgInst : public UnaryInstruction {
1143 virtual VAArgInst *clone_impl() const;
1146 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1147 Instruction *InsertBefore = 0)
1148 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1151 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1152 BasicBlock *InsertAtEnd)
1153 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1157 // Methods for support type inquiry through isa, cast, and dyn_cast:
1158 static inline bool classof(const VAArgInst *) { return true; }
1159 static inline bool classof(const Instruction *I) {
1160 return I->getOpcode() == VAArg;
1162 static inline bool classof(const Value *V) {
1163 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1167 //===----------------------------------------------------------------------===//
1168 // ExtractElementInst Class
1169 //===----------------------------------------------------------------------===//
1171 /// ExtractElementInst - This instruction extracts a single (scalar)
1172 /// element from a VectorType value
1174 class ExtractElementInst : public Instruction {
1175 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1176 Instruction *InsertBefore = 0);
1177 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1178 BasicBlock *InsertAtEnd);
1180 virtual ExtractElementInst *clone_impl() const;
1183 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1184 const Twine &NameStr = "",
1185 Instruction *InsertBefore = 0) {
1186 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1188 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1189 const Twine &NameStr,
1190 BasicBlock *InsertAtEnd) {
1191 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1194 /// isValidOperands - Return true if an extractelement instruction can be
1195 /// formed with the specified operands.
1196 static bool isValidOperands(const Value *Vec, const Value *Idx);
1198 Value *getVectorOperand() { return Op<0>(); }
1199 Value *getIndexOperand() { return Op<1>(); }
1200 const Value *getVectorOperand() const { return Op<0>(); }
1201 const Value *getIndexOperand() const { return Op<1>(); }
1203 const VectorType *getVectorOperandType() const {
1204 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1208 /// Transparently provide more efficient getOperand methods.
1209 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1211 // Methods for support type inquiry through isa, cast, and dyn_cast:
1212 static inline bool classof(const ExtractElementInst *) { return true; }
1213 static inline bool classof(const Instruction *I) {
1214 return I->getOpcode() == Instruction::ExtractElement;
1216 static inline bool classof(const Value *V) {
1217 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1222 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1225 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1227 //===----------------------------------------------------------------------===//
1228 // InsertElementInst Class
1229 //===----------------------------------------------------------------------===//
1231 /// InsertElementInst - This instruction inserts a single (scalar)
1232 /// element into a VectorType value
1234 class InsertElementInst : public Instruction {
1235 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1236 const Twine &NameStr = "",
1237 Instruction *InsertBefore = 0);
1238 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1239 const Twine &NameStr, BasicBlock *InsertAtEnd);
1241 virtual InsertElementInst *clone_impl() const;
1244 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1245 const Twine &NameStr = "",
1246 Instruction *InsertBefore = 0) {
1247 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1249 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1250 const Twine &NameStr,
1251 BasicBlock *InsertAtEnd) {
1252 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1255 /// isValidOperands - Return true if an insertelement instruction can be
1256 /// formed with the specified operands.
1257 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1260 /// getType - Overload to return most specific vector type.
1262 const VectorType *getType() const {
1263 return reinterpret_cast<const VectorType*>(Instruction::getType());
1266 /// Transparently provide more efficient getOperand methods.
1267 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1269 // Methods for support type inquiry through isa, cast, and dyn_cast:
1270 static inline bool classof(const InsertElementInst *) { return true; }
1271 static inline bool classof(const Instruction *I) {
1272 return I->getOpcode() == Instruction::InsertElement;
1274 static inline bool classof(const Value *V) {
1275 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1280 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1283 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1285 //===----------------------------------------------------------------------===//
1286 // ShuffleVectorInst Class
1287 //===----------------------------------------------------------------------===//
1289 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1292 class ShuffleVectorInst : public Instruction {
1294 virtual ShuffleVectorInst *clone_impl() const;
1297 // allocate space for exactly three operands
1298 void *operator new(size_t s) {
1299 return User::operator new(s, 3);
1301 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1302 const Twine &NameStr = "",
1303 Instruction *InsertBefor = 0);
1304 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1305 const Twine &NameStr, BasicBlock *InsertAtEnd);
1307 /// isValidOperands - Return true if a shufflevector instruction can be
1308 /// formed with the specified operands.
1309 static bool isValidOperands(const Value *V1, const Value *V2,
1312 /// getType - Overload to return most specific vector type.
1314 const VectorType *getType() const {
1315 return reinterpret_cast<const VectorType*>(Instruction::getType());
1318 /// Transparently provide more efficient getOperand methods.
1319 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1321 /// getMaskValue - Return the index from the shuffle mask for the specified
1322 /// output result. This is either -1 if the element is undef or a number less
1323 /// than 2*numelements.
1324 int getMaskValue(unsigned i) const;
1326 // Methods for support type inquiry through isa, cast, and dyn_cast:
1327 static inline bool classof(const ShuffleVectorInst *) { return true; }
1328 static inline bool classof(const Instruction *I) {
1329 return I->getOpcode() == Instruction::ShuffleVector;
1331 static inline bool classof(const Value *V) {
1332 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1337 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1340 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1342 //===----------------------------------------------------------------------===//
1343 // ExtractValueInst Class
1344 //===----------------------------------------------------------------------===//
1346 /// ExtractValueInst - This instruction extracts a struct member or array
1347 /// element value from an aggregate value.
1349 class ExtractValueInst : public UnaryInstruction {
1350 SmallVector<unsigned, 4> Indices;
1352 ExtractValueInst(const ExtractValueInst &EVI);
1353 void init(const unsigned *Idx, unsigned NumIdx,
1354 const Twine &NameStr);
1355 void init(unsigned Idx, const Twine &NameStr);
1357 template<typename InputIterator>
1358 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1359 const Twine &NameStr,
1360 // This argument ensures that we have an iterator we can
1361 // do arithmetic on in constant time
1362 std::random_access_iterator_tag) {
1363 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1365 // There's no fundamental reason why we require at least one index
1366 // (other than weirdness with &*IdxBegin being invalid; see
1367 // getelementptr's init routine for example). But there's no
1368 // present need to support it.
1369 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1371 // This requires that the iterator points to contiguous memory.
1372 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1373 // we have to build an array here
1376 /// getIndexedType - Returns the type of the element that would be extracted
1377 /// with an extractvalue instruction with the specified parameters.
1379 /// Null is returned if the indices are invalid for the specified
1382 static const Type *getIndexedType(const Type *Agg,
1383 const unsigned *Idx, unsigned NumIdx);
1385 template<typename InputIterator>
1386 static const Type *getIndexedType(const Type *Ptr,
1387 InputIterator IdxBegin,
1388 InputIterator IdxEnd,
1389 // This argument ensures that we
1390 // have an iterator we can do
1391 // arithmetic on in constant time
1392 std::random_access_iterator_tag) {
1393 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1396 // This requires that the iterator points to contiguous memory.
1397 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1399 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1402 /// Constructors - Create a extractvalue instruction with a base aggregate
1403 /// value and a list of indices. The first ctor can optionally insert before
1404 /// an existing instruction, the second appends the new instruction to the
1405 /// specified BasicBlock.
1406 template<typename InputIterator>
1407 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1408 InputIterator IdxEnd,
1409 const Twine &NameStr,
1410 Instruction *InsertBefore);
1411 template<typename InputIterator>
1412 inline ExtractValueInst(Value *Agg,
1413 InputIterator IdxBegin, InputIterator IdxEnd,
1414 const Twine &NameStr, BasicBlock *InsertAtEnd);
1416 // allocate space for exactly one operand
1417 void *operator new(size_t s) {
1418 return User::operator new(s, 1);
1421 virtual ExtractValueInst *clone_impl() const;
1424 template<typename InputIterator>
1425 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1426 InputIterator IdxEnd,
1427 const Twine &NameStr = "",
1428 Instruction *InsertBefore = 0) {
1430 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1432 template<typename InputIterator>
1433 static ExtractValueInst *Create(Value *Agg,
1434 InputIterator IdxBegin, InputIterator IdxEnd,
1435 const Twine &NameStr,
1436 BasicBlock *InsertAtEnd) {
1437 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1440 /// Constructors - These two creators are convenience methods because one
1441 /// index extractvalue instructions are much more common than those with
1443 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1444 const Twine &NameStr = "",
1445 Instruction *InsertBefore = 0) {
1446 unsigned Idxs[1] = { Idx };
1447 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1449 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1450 const Twine &NameStr,
1451 BasicBlock *InsertAtEnd) {
1452 unsigned Idxs[1] = { Idx };
1453 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1456 /// getIndexedType - Returns the type of the element that would be extracted
1457 /// with an extractvalue instruction with the specified parameters.
1459 /// Null is returned if the indices are invalid for the specified
1462 template<typename InputIterator>
1463 static const Type *getIndexedType(const Type *Ptr,
1464 InputIterator IdxBegin,
1465 InputIterator IdxEnd) {
1466 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1467 typename std::iterator_traits<InputIterator>::
1468 iterator_category());
1470 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1472 typedef const unsigned* idx_iterator;
1473 inline idx_iterator idx_begin() const { return Indices.begin(); }
1474 inline idx_iterator idx_end() const { return Indices.end(); }
1476 Value *getAggregateOperand() {
1477 return getOperand(0);
1479 const Value *getAggregateOperand() const {
1480 return getOperand(0);
1482 static unsigned getAggregateOperandIndex() {
1483 return 0U; // get index for modifying correct operand
1486 unsigned getNumIndices() const { // Note: always non-negative
1487 return (unsigned)Indices.size();
1490 bool hasIndices() const {
1494 // Methods for support type inquiry through isa, cast, and dyn_cast:
1495 static inline bool classof(const ExtractValueInst *) { return true; }
1496 static inline bool classof(const Instruction *I) {
1497 return I->getOpcode() == Instruction::ExtractValue;
1499 static inline bool classof(const Value *V) {
1500 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1504 template<typename InputIterator>
1505 ExtractValueInst::ExtractValueInst(Value *Agg,
1506 InputIterator IdxBegin,
1507 InputIterator IdxEnd,
1508 const Twine &NameStr,
1509 Instruction *InsertBefore)
1510 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1512 ExtractValue, Agg, InsertBefore) {
1513 init(IdxBegin, IdxEnd, NameStr,
1514 typename std::iterator_traits<InputIterator>::iterator_category());
1516 template<typename InputIterator>
1517 ExtractValueInst::ExtractValueInst(Value *Agg,
1518 InputIterator IdxBegin,
1519 InputIterator IdxEnd,
1520 const Twine &NameStr,
1521 BasicBlock *InsertAtEnd)
1522 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1524 ExtractValue, Agg, InsertAtEnd) {
1525 init(IdxBegin, IdxEnd, NameStr,
1526 typename std::iterator_traits<InputIterator>::iterator_category());
1530 //===----------------------------------------------------------------------===//
1531 // InsertValueInst Class
1532 //===----------------------------------------------------------------------===//
1534 /// InsertValueInst - This instruction inserts a struct field of array element
1535 /// value into an aggregate value.
1537 class InsertValueInst : public Instruction {
1538 SmallVector<unsigned, 4> Indices;
1540 void *operator new(size_t, unsigned); // Do not implement
1541 InsertValueInst(const InsertValueInst &IVI);
1542 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1543 const Twine &NameStr);
1544 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1546 template<typename InputIterator>
1547 void init(Value *Agg, Value *Val,
1548 InputIterator IdxBegin, InputIterator IdxEnd,
1549 const Twine &NameStr,
1550 // This argument ensures that we have an iterator we can
1551 // do arithmetic on in constant time
1552 std::random_access_iterator_tag) {
1553 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1555 // There's no fundamental reason why we require at least one index
1556 // (other than weirdness with &*IdxBegin being invalid; see
1557 // getelementptr's init routine for example). But there's no
1558 // present need to support it.
1559 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1561 // This requires that the iterator points to contiguous memory.
1562 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1563 // we have to build an array here
1566 /// Constructors - Create a insertvalue instruction with a base aggregate
1567 /// value, a value to insert, and a list of indices. The first ctor can
1568 /// optionally insert before an existing instruction, the second appends
1569 /// the new instruction to the specified BasicBlock.
1570 template<typename InputIterator>
1571 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1572 InputIterator IdxEnd,
1573 const Twine &NameStr,
1574 Instruction *InsertBefore);
1575 template<typename InputIterator>
1576 inline InsertValueInst(Value *Agg, Value *Val,
1577 InputIterator IdxBegin, InputIterator IdxEnd,
1578 const Twine &NameStr, BasicBlock *InsertAtEnd);
1580 /// Constructors - These two constructors are convenience methods because one
1581 /// and two index insertvalue instructions are so common.
1582 InsertValueInst(Value *Agg, Value *Val,
1583 unsigned Idx, const Twine &NameStr = "",
1584 Instruction *InsertBefore = 0);
1585 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1586 const Twine &NameStr, BasicBlock *InsertAtEnd);
1588 virtual InsertValueInst *clone_impl() const;
1590 // allocate space for exactly two operands
1591 void *operator new(size_t s) {
1592 return User::operator new(s, 2);
1595 template<typename InputIterator>
1596 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1597 InputIterator IdxEnd,
1598 const Twine &NameStr = "",
1599 Instruction *InsertBefore = 0) {
1600 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1601 NameStr, InsertBefore);
1603 template<typename InputIterator>
1604 static InsertValueInst *Create(Value *Agg, Value *Val,
1605 InputIterator IdxBegin, InputIterator IdxEnd,
1606 const Twine &NameStr,
1607 BasicBlock *InsertAtEnd) {
1608 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1609 NameStr, InsertAtEnd);
1612 /// Constructors - These two creators are convenience methods because one
1613 /// index insertvalue instructions are much more common than those with
1615 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1616 const Twine &NameStr = "",
1617 Instruction *InsertBefore = 0) {
1618 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1620 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1621 const Twine &NameStr,
1622 BasicBlock *InsertAtEnd) {
1623 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1626 /// Transparently provide more efficient getOperand methods.
1627 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1629 typedef const unsigned* idx_iterator;
1630 inline idx_iterator idx_begin() const { return Indices.begin(); }
1631 inline idx_iterator idx_end() const { return Indices.end(); }
1633 Value *getAggregateOperand() {
1634 return getOperand(0);
1636 const Value *getAggregateOperand() const {
1637 return getOperand(0);
1639 static unsigned getAggregateOperandIndex() {
1640 return 0U; // get index for modifying correct operand
1643 Value *getInsertedValueOperand() {
1644 return getOperand(1);
1646 const Value *getInsertedValueOperand() const {
1647 return getOperand(1);
1649 static unsigned getInsertedValueOperandIndex() {
1650 return 1U; // 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 InsertValueInst *) { return true; }
1663 static inline bool classof(const Instruction *I) {
1664 return I->getOpcode() == Instruction::InsertValue;
1666 static inline bool classof(const Value *V) {
1667 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1672 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1675 template<typename InputIterator>
1676 InsertValueInst::InsertValueInst(Value *Agg,
1678 InputIterator IdxBegin,
1679 InputIterator IdxEnd,
1680 const Twine &NameStr,
1681 Instruction *InsertBefore)
1682 : Instruction(Agg->getType(), InsertValue,
1683 OperandTraits<InsertValueInst>::op_begin(this),
1685 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1686 typename std::iterator_traits<InputIterator>::iterator_category());
1688 template<typename InputIterator>
1689 InsertValueInst::InsertValueInst(Value *Agg,
1691 InputIterator IdxBegin,
1692 InputIterator IdxEnd,
1693 const Twine &NameStr,
1694 BasicBlock *InsertAtEnd)
1695 : Instruction(Agg->getType(), InsertValue,
1696 OperandTraits<InsertValueInst>::op_begin(this),
1698 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1699 typename std::iterator_traits<InputIterator>::iterator_category());
1702 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1704 //===----------------------------------------------------------------------===//
1706 //===----------------------------------------------------------------------===//
1708 // PHINode - The PHINode class is used to represent the magical mystical PHI
1709 // node, that can not exist in nature, but can be synthesized in a computer
1710 // scientist's overactive imagination.
1712 class PHINode : public Instruction {
1713 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1714 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1715 /// the number actually in use.
1716 unsigned ReservedSpace;
1717 PHINode(const PHINode &PN);
1718 // allocate space for exactly zero operands
1719 void *operator new(size_t s) {
1720 return User::operator new(s, 0);
1722 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1723 Instruction *InsertBefore = 0)
1724 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1729 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1730 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1735 virtual PHINode *clone_impl() const;
1737 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1738 Instruction *InsertBefore = 0) {
1739 return new PHINode(Ty, NameStr, InsertBefore);
1741 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1742 BasicBlock *InsertAtEnd) {
1743 return new PHINode(Ty, NameStr, InsertAtEnd);
1747 /// reserveOperandSpace - This method can be used to avoid repeated
1748 /// reallocation of PHI operand lists by reserving space for the correct
1749 /// number of operands before adding them. Unlike normal vector reserves,
1750 /// this method can also be used to trim the operand space.
1751 void reserveOperandSpace(unsigned NumValues) {
1752 resizeOperands(NumValues*2);
1755 /// Provide fast operand accessors
1756 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1758 /// getNumIncomingValues - Return the number of incoming edges
1760 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1762 /// getIncomingValue - Return incoming value number x
1764 Value *getIncomingValue(unsigned i) const {
1765 assert(i*2 < getNumOperands() && "Invalid value number!");
1766 return getOperand(i*2);
1768 void setIncomingValue(unsigned i, Value *V) {
1769 assert(i*2 < getNumOperands() && "Invalid value number!");
1772 static unsigned getOperandNumForIncomingValue(unsigned i) {
1775 static unsigned getIncomingValueNumForOperand(unsigned i) {
1776 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1780 /// getIncomingBlock - Return incoming basic block #i.
1782 BasicBlock *getIncomingBlock(unsigned i) const {
1783 return cast<BasicBlock>(getOperand(i*2+1));
1786 /// getIncomingBlock - Return incoming basic block corresponding
1787 /// to an operand of the PHI.
1789 BasicBlock *getIncomingBlock(const Use &U) const {
1790 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1791 return cast<BasicBlock>((&U + 1)->get());
1794 /// getIncomingBlock - Return incoming basic block corresponding
1795 /// to value use iterator.
1797 template <typename U>
1798 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1799 return getIncomingBlock(I.getUse());
1803 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1804 setOperand(i*2+1, (Value*)BB);
1806 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1809 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1810 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1814 /// addIncoming - Add an incoming value to the end of the PHI list
1816 void addIncoming(Value *V, BasicBlock *BB) {
1817 assert(V && "PHI node got a null value!");
1818 assert(BB && "PHI node got a null basic block!");
1819 assert(getType() == V->getType() &&
1820 "All operands to PHI node must be the same type as the PHI node!");
1821 unsigned OpNo = NumOperands;
1822 if (OpNo+2 > ReservedSpace)
1823 resizeOperands(0); // Get more space!
1824 // Initialize some new operands.
1825 NumOperands = OpNo+2;
1826 OperandList[OpNo] = V;
1827 OperandList[OpNo+1] = (Value*)BB;
1830 /// removeIncomingValue - Remove an incoming value. This is useful if a
1831 /// predecessor basic block is deleted. The value removed is returned.
1833 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1834 /// is true), the PHI node is destroyed and any uses of it are replaced with
1835 /// dummy values. The only time there should be zero incoming values to a PHI
1836 /// node is when the block is dead, so this strategy is sound.
1838 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1840 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1841 int Idx = getBasicBlockIndex(BB);
1842 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1843 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1846 /// getBasicBlockIndex - Return the first index of the specified basic
1847 /// block in the value list for this PHI. Returns -1 if no instance.
1849 int getBasicBlockIndex(const BasicBlock *BB) const {
1850 Use *OL = OperandList;
1851 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1852 if (OL[i+1].get() == (const Value*)BB) return i/2;
1856 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1857 return getIncomingValue(getBasicBlockIndex(BB));
1860 /// hasConstantValue - If the specified PHI node always merges together the
1861 /// same value, return the value, otherwise return null.
1863 /// If the PHI has undef operands, but all the rest of the operands are
1864 /// some unique value, return that value if it can be proved that the
1865 /// value dominates the PHI. If DT is null, use a conservative check,
1866 /// otherwise use DT to test for dominance.
1868 Value *hasConstantValue(DominatorTree *DT = 0) const;
1870 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1871 static inline bool classof(const PHINode *) { return true; }
1872 static inline bool classof(const Instruction *I) {
1873 return I->getOpcode() == Instruction::PHI;
1875 static inline bool classof(const Value *V) {
1876 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1879 void resizeOperands(unsigned NumOperands);
1883 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1886 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1889 //===----------------------------------------------------------------------===//
1891 //===----------------------------------------------------------------------===//
1893 //===---------------------------------------------------------------------------
1894 /// ReturnInst - Return a value (possibly void), from a function. Execution
1895 /// does not continue in this function any longer.
1897 class ReturnInst : public TerminatorInst {
1898 ReturnInst(const ReturnInst &RI);
1901 // ReturnInst constructors:
1902 // ReturnInst() - 'ret void' instruction
1903 // ReturnInst( null) - 'ret void' instruction
1904 // ReturnInst(Value* X) - 'ret X' instruction
1905 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1906 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1907 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1908 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1910 // NOTE: If the Value* passed is of type void then the constructor behaves as
1911 // if it was passed NULL.
1912 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1913 Instruction *InsertBefore = 0);
1914 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1915 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1917 virtual ReturnInst *clone_impl() const;
1919 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1920 Instruction *InsertBefore = 0) {
1921 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1923 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1924 BasicBlock *InsertAtEnd) {
1925 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1927 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1928 return new(0) ReturnInst(C, InsertAtEnd);
1930 virtual ~ReturnInst();
1932 /// Provide fast operand accessors
1933 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1935 /// Convenience accessor
1936 Value *getReturnValue(unsigned n = 0) const {
1937 return n < getNumOperands()
1942 unsigned getNumSuccessors() const { return 0; }
1944 // Methods for support type inquiry through isa, cast, and dyn_cast:
1945 static inline bool classof(const ReturnInst *) { return true; }
1946 static inline bool classof(const Instruction *I) {
1947 return (I->getOpcode() == Instruction::Ret);
1949 static inline bool classof(const Value *V) {
1950 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1953 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1954 virtual unsigned getNumSuccessorsV() const;
1955 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1959 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
1962 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1964 //===----------------------------------------------------------------------===//
1966 //===----------------------------------------------------------------------===//
1968 //===---------------------------------------------------------------------------
1969 /// BranchInst - Conditional or Unconditional Branch instruction.
1971 class BranchInst : public TerminatorInst {
1972 /// Ops list - Branches are strange. The operands are ordered:
1973 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
1974 /// they don't have to check for cond/uncond branchness. These are mostly
1975 /// accessed relative from op_end().
1976 BranchInst(const BranchInst &BI);
1978 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1979 // BranchInst(BB *B) - 'br B'
1980 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1981 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1982 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1983 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1984 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1985 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1986 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1987 Instruction *InsertBefore = 0);
1988 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1989 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1990 BasicBlock *InsertAtEnd);
1992 virtual BranchInst *clone_impl() const;
1994 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1995 return new(1, true) BranchInst(IfTrue, InsertBefore);
1997 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1998 Value *Cond, Instruction *InsertBefore = 0) {
1999 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2001 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2002 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2004 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2005 Value *Cond, BasicBlock *InsertAtEnd) {
2006 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2011 /// Transparently provide more efficient getOperand methods.
2012 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2014 bool isUnconditional() const { return getNumOperands() == 1; }
2015 bool isConditional() const { return getNumOperands() == 3; }
2017 Value *getCondition() const {
2018 assert(isConditional() && "Cannot get condition of an uncond branch!");
2022 void setCondition(Value *V) {
2023 assert(isConditional() && "Cannot set condition of unconditional branch!");
2027 // setUnconditionalDest - Change the current branch to an unconditional branch
2028 // targeting the specified block.
2029 // FIXME: Eliminate this ugly method.
2030 void setUnconditionalDest(BasicBlock *Dest) {
2031 Op<-1>() = (Value*)Dest;
2032 if (isConditional()) { // Convert this to an uncond branch.
2036 OperandList = op_begin();
2040 unsigned getNumSuccessors() const { return 1+isConditional(); }
2042 BasicBlock *getSuccessor(unsigned i) const {
2043 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2044 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2047 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2048 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2049 *(&Op<-1>() - idx) = (Value*)NewSucc;
2052 // Methods for support type inquiry through isa, cast, and dyn_cast:
2053 static inline bool classof(const BranchInst *) { return true; }
2054 static inline bool classof(const Instruction *I) {
2055 return (I->getOpcode() == Instruction::Br);
2057 static inline bool classof(const Value *V) {
2058 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2061 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2062 virtual unsigned getNumSuccessorsV() const;
2063 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2067 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2069 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2071 //===----------------------------------------------------------------------===//
2073 //===----------------------------------------------------------------------===//
2075 //===---------------------------------------------------------------------------
2076 /// SwitchInst - Multiway switch
2078 class SwitchInst : public TerminatorInst {
2079 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2080 unsigned ReservedSpace;
2081 // Operand[0] = Value to switch on
2082 // Operand[1] = Default basic block destination
2083 // Operand[2n ] = Value to match
2084 // Operand[2n+1] = BasicBlock to go to on match
2085 SwitchInst(const SwitchInst &SI);
2086 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2087 void resizeOperands(unsigned No);
2088 // allocate space for exactly zero operands
2089 void *operator new(size_t s) {
2090 return User::operator new(s, 0);
2092 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2093 /// switch on and a default destination. The number of additional cases can
2094 /// be specified here to make memory allocation more efficient. This
2095 /// constructor can also autoinsert before another instruction.
2096 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2097 Instruction *InsertBefore);
2099 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2100 /// switch on and a default destination. The number of additional cases can
2101 /// be specified here to make memory allocation more efficient. This
2102 /// constructor also autoinserts at the end of the specified BasicBlock.
2103 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2104 BasicBlock *InsertAtEnd);
2106 virtual SwitchInst *clone_impl() const;
2108 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2109 unsigned NumCases, Instruction *InsertBefore = 0) {
2110 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2112 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2113 unsigned NumCases, BasicBlock *InsertAtEnd) {
2114 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2118 /// Provide fast operand accessors
2119 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2121 // Accessor Methods for Switch stmt
2122 Value *getCondition() const { return getOperand(0); }
2123 void setCondition(Value *V) { setOperand(0, V); }
2125 BasicBlock *getDefaultDest() const {
2126 return cast<BasicBlock>(getOperand(1));
2129 /// getNumCases - return the number of 'cases' in this switch instruction.
2130 /// Note that case #0 is always the default case.
2131 unsigned getNumCases() const {
2132 return getNumOperands()/2;
2135 /// getCaseValue - Return the specified case value. Note that case #0, the
2136 /// default destination, does not have a case value.
2137 ConstantInt *getCaseValue(unsigned i) {
2138 assert(i && i < getNumCases() && "Illegal case value to get!");
2139 return getSuccessorValue(i);
2142 /// getCaseValue - Return the specified case value. Note that case #0, the
2143 /// default destination, does not have a case value.
2144 const ConstantInt *getCaseValue(unsigned i) const {
2145 assert(i && i < getNumCases() && "Illegal case value to get!");
2146 return getSuccessorValue(i);
2149 /// findCaseValue - Search all of the case values for the specified constant.
2150 /// If it is explicitly handled, return the case number of it, otherwise
2151 /// return 0 to indicate that it is handled by the default handler.
2152 unsigned findCaseValue(const ConstantInt *C) const {
2153 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2154 if (getCaseValue(i) == C)
2159 /// findCaseDest - Finds the unique case value for a given successor. Returns
2160 /// null if the successor is not found, not unique, or is the default case.
2161 ConstantInt *findCaseDest(BasicBlock *BB) {
2162 if (BB == getDefaultDest()) return NULL;
2164 ConstantInt *CI = NULL;
2165 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2166 if (getSuccessor(i) == BB) {
2167 if (CI) return NULL; // Multiple cases lead to BB.
2168 else CI = getCaseValue(i);
2174 /// addCase - Add an entry to the switch instruction...
2176 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2178 /// removeCase - This method removes the specified successor from the switch
2179 /// instruction. Note that this cannot be used to remove the default
2180 /// destination (successor #0).
2182 void removeCase(unsigned idx);
2184 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2185 BasicBlock *getSuccessor(unsigned idx) const {
2186 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2187 return cast<BasicBlock>(getOperand(idx*2+1));
2189 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2190 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2191 setOperand(idx*2+1, (Value*)NewSucc);
2194 // getSuccessorValue - Return the value associated with the specified
2196 ConstantInt *getSuccessorValue(unsigned idx) const {
2197 assert(idx < getNumSuccessors() && "Successor # out of range!");
2198 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2201 // Methods for support type inquiry through isa, cast, and dyn_cast:
2202 static inline bool classof(const SwitchInst *) { return true; }
2203 static inline bool classof(const Instruction *I) {
2204 return I->getOpcode() == Instruction::Switch;
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<SwitchInst> : public HungoffOperandTraits<2> {
2219 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2222 //===----------------------------------------------------------------------===//
2223 // IndirectBrInst Class
2224 //===----------------------------------------------------------------------===//
2226 //===---------------------------------------------------------------------------
2227 /// IndirectBrInst - Indirect Branch Instruction.
2229 class IndirectBrInst : public TerminatorInst {
2230 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2231 unsigned ReservedSpace;
2232 // Operand[0] = Value to switch on
2233 // Operand[1] = Default basic block destination
2234 // Operand[2n ] = Value to match
2235 // Operand[2n+1] = BasicBlock to go to on match
2236 IndirectBrInst(const IndirectBrInst &IBI);
2237 void init(Value *Address, unsigned NumDests);
2238 void resizeOperands(unsigned No);
2239 // allocate space for exactly zero operands
2240 void *operator new(size_t s) {
2241 return User::operator new(s, 0);
2243 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2244 /// Address to jump to. The number of expected destinations can be specified
2245 /// here to make memory allocation more efficient. This constructor can also
2246 /// autoinsert before another instruction.
2247 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2249 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2250 /// Address to jump to. The number of expected destinations can be specified
2251 /// here to make memory allocation more efficient. This constructor also
2252 /// autoinserts at the end of the specified BasicBlock.
2253 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2255 virtual IndirectBrInst *clone_impl() const;
2257 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2258 Instruction *InsertBefore = 0) {
2259 return new IndirectBrInst(Address, NumDests, InsertBefore);
2261 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2262 BasicBlock *InsertAtEnd) {
2263 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2267 /// Provide fast operand accessors.
2268 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2270 // Accessor Methods for IndirectBrInst instruction.
2271 Value *getAddress() { return getOperand(0); }
2272 const Value *getAddress() const { return getOperand(0); }
2273 void setAddress(Value *V) { setOperand(0, V); }
2276 /// getNumDestinations - return the number of possible destinations in this
2277 /// indirectbr instruction.
2278 unsigned getNumDestinations() const { return getNumOperands()-1; }
2280 /// getDestination - Return the specified destination.
2281 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2282 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2284 /// addDestination - Add a destination.
2286 void addDestination(BasicBlock *Dest);
2288 /// removeDestination - This method removes the specified successor from the
2289 /// indirectbr instruction.
2290 void removeDestination(unsigned i);
2292 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2293 BasicBlock *getSuccessor(unsigned i) const {
2294 return cast<BasicBlock>(getOperand(i+1));
2296 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2297 setOperand(i+1, (Value*)NewSucc);
2300 // Methods for support type inquiry through isa, cast, and dyn_cast:
2301 static inline bool classof(const IndirectBrInst *) { return true; }
2302 static inline bool classof(const Instruction *I) {
2303 return I->getOpcode() == Instruction::IndirectBr;
2305 static inline bool classof(const Value *V) {
2306 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2309 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2310 virtual unsigned getNumSuccessorsV() const;
2311 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2315 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2318 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2321 //===----------------------------------------------------------------------===//
2323 //===----------------------------------------------------------------------===//
2325 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2326 /// calling convention of the call.
2328 class InvokeInst : public TerminatorInst {
2329 AttrListPtr AttributeList;
2330 InvokeInst(const InvokeInst &BI);
2331 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2332 Value* const *Args, unsigned NumArgs);
2334 template<typename InputIterator>
2335 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2336 InputIterator ArgBegin, InputIterator ArgEnd,
2337 const Twine &NameStr,
2338 // This argument ensures that we have an iterator we can
2339 // do arithmetic on in constant time
2340 std::random_access_iterator_tag) {
2341 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2343 // This requires that the iterator points to contiguous memory.
2344 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2348 /// Construct an InvokeInst given a range of arguments.
2349 /// InputIterator must be a random-access iterator pointing to
2350 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2351 /// made for random-accessness but not for contiguous storage as
2352 /// that would incur runtime overhead.
2354 /// @brief Construct an InvokeInst from a range of arguments
2355 template<typename InputIterator>
2356 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2357 InputIterator ArgBegin, InputIterator ArgEnd,
2359 const Twine &NameStr, Instruction *InsertBefore);
2361 /// Construct an InvokeInst given a range of arguments.
2362 /// InputIterator must be a random-access iterator pointing to
2363 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2364 /// made for random-accessness but not for contiguous storage as
2365 /// that would incur runtime overhead.
2367 /// @brief Construct an InvokeInst from a range of arguments
2368 template<typename InputIterator>
2369 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2370 InputIterator ArgBegin, InputIterator ArgEnd,
2372 const Twine &NameStr, BasicBlock *InsertAtEnd);
2374 virtual InvokeInst *clone_impl() const;
2376 template<typename InputIterator>
2377 static InvokeInst *Create(Value *Func,
2378 BasicBlock *IfNormal, BasicBlock *IfException,
2379 InputIterator ArgBegin, InputIterator ArgEnd,
2380 const Twine &NameStr = "",
2381 Instruction *InsertBefore = 0) {
2382 unsigned Values(ArgEnd - ArgBegin + 3);
2383 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2384 Values, NameStr, InsertBefore);
2386 template<typename InputIterator>
2387 static InvokeInst *Create(Value *Func,
2388 BasicBlock *IfNormal, BasicBlock *IfException,
2389 InputIterator ArgBegin, InputIterator ArgEnd,
2390 const Twine &NameStr,
2391 BasicBlock *InsertAtEnd) {
2392 unsigned Values(ArgEnd - ArgBegin + 3);
2393 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2394 Values, NameStr, InsertAtEnd);
2397 /// Provide fast operand accessors
2398 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2400 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2402 CallingConv::ID getCallingConv() const {
2403 return static_cast<CallingConv::ID>(SubclassData);
2405 void setCallingConv(CallingConv::ID CC) {
2406 SubclassData = static_cast<unsigned>(CC);
2409 /// getAttributes - Return the parameter attributes for this invoke.
2411 const AttrListPtr &getAttributes() const { return AttributeList; }
2413 /// setAttributes - Set the parameter attributes for this invoke.
2415 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2417 /// addAttribute - adds the attribute to the list of attributes.
2418 void addAttribute(unsigned i, Attributes attr);
2420 /// removeAttribute - removes the attribute from the list of attributes.
2421 void removeAttribute(unsigned i, Attributes attr);
2423 /// @brief Determine whether the call or the callee has the given attribute.
2424 bool paramHasAttr(unsigned i, Attributes attr) const;
2426 /// @brief Extract the alignment for a call or parameter (0=unknown).
2427 unsigned getParamAlignment(unsigned i) const {
2428 return AttributeList.getParamAlignment(i);
2431 /// @brief Determine if the call does not access memory.
2432 bool doesNotAccessMemory() const {
2433 return paramHasAttr(~0, Attribute::ReadNone);
2435 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2436 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2437 else removeAttribute(~0, Attribute::ReadNone);
2440 /// @brief Determine if the call does not access or only reads memory.
2441 bool onlyReadsMemory() const {
2442 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2444 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2445 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2446 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2449 /// @brief Determine if the call cannot return.
2450 bool doesNotReturn() const {
2451 return paramHasAttr(~0, Attribute::NoReturn);
2453 void setDoesNotReturn(bool DoesNotReturn = true) {
2454 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2455 else removeAttribute(~0, Attribute::NoReturn);
2458 /// @brief Determine if the call cannot unwind.
2459 bool doesNotThrow() const {
2460 return paramHasAttr(~0, Attribute::NoUnwind);
2462 void setDoesNotThrow(bool DoesNotThrow = true) {
2463 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2464 else removeAttribute(~0, Attribute::NoUnwind);
2467 /// @brief Determine if the call returns a structure through first
2468 /// pointer argument.
2469 bool hasStructRetAttr() const {
2470 // Be friendly and also check the callee.
2471 return paramHasAttr(1, Attribute::StructRet);
2474 /// @brief Determine if any call argument is an aggregate passed by value.
2475 bool hasByValArgument() const {
2476 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2479 /// getCalledFunction - Return the function called, or null if this is an
2480 /// indirect function invocation.
2482 Function *getCalledFunction() const {
2483 return dyn_cast<Function>(getOperand(0));
2486 /// getCalledValue - Get a pointer to the function that is invoked by this
2488 const Value *getCalledValue() const { return getOperand(0); }
2489 Value *getCalledValue() { return getOperand(0); }
2491 // get*Dest - Return the destination basic blocks...
2492 BasicBlock *getNormalDest() const {
2493 return cast<BasicBlock>(getOperand(1));
2495 BasicBlock *getUnwindDest() const {
2496 return cast<BasicBlock>(getOperand(2));
2498 void setNormalDest(BasicBlock *B) {
2499 setOperand(1, (Value*)B);
2502 void setUnwindDest(BasicBlock *B) {
2503 setOperand(2, (Value*)B);
2506 BasicBlock *getSuccessor(unsigned i) const {
2507 assert(i < 2 && "Successor # out of range for invoke!");
2508 return i == 0 ? getNormalDest() : getUnwindDest();
2511 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2512 assert(idx < 2 && "Successor # out of range for invoke!");
2513 setOperand(idx+1, (Value*)NewSucc);
2516 unsigned getNumSuccessors() const { return 2; }
2518 // Methods for support type inquiry through isa, cast, and dyn_cast:
2519 static inline bool classof(const InvokeInst *) { return true; }
2520 static inline bool classof(const Instruction *I) {
2521 return (I->getOpcode() == Instruction::Invoke);
2523 static inline bool classof(const Value *V) {
2524 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2527 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2528 virtual unsigned getNumSuccessorsV() const;
2529 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2533 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2536 template<typename InputIterator>
2537 InvokeInst::InvokeInst(Value *Func,
2538 BasicBlock *IfNormal, BasicBlock *IfException,
2539 InputIterator ArgBegin, InputIterator ArgEnd,
2541 const Twine &NameStr, Instruction *InsertBefore)
2542 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2543 ->getElementType())->getReturnType(),
2544 Instruction::Invoke,
2545 OperandTraits<InvokeInst>::op_end(this) - Values,
2546 Values, InsertBefore) {
2547 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2548 typename std::iterator_traits<InputIterator>::iterator_category());
2550 template<typename InputIterator>
2551 InvokeInst::InvokeInst(Value *Func,
2552 BasicBlock *IfNormal, BasicBlock *IfException,
2553 InputIterator ArgBegin, InputIterator ArgEnd,
2555 const Twine &NameStr, BasicBlock *InsertAtEnd)
2556 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2557 ->getElementType())->getReturnType(),
2558 Instruction::Invoke,
2559 OperandTraits<InvokeInst>::op_end(this) - Values,
2560 Values, InsertAtEnd) {
2561 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2562 typename std::iterator_traits<InputIterator>::iterator_category());
2565 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2567 //===----------------------------------------------------------------------===//
2569 //===----------------------------------------------------------------------===//
2571 //===---------------------------------------------------------------------------
2572 /// UnwindInst - Immediately exit the current function, unwinding the stack
2573 /// until an invoke instruction is found.
2575 class UnwindInst : public TerminatorInst {
2576 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2578 virtual UnwindInst *clone_impl() const;
2580 // allocate space for exactly zero operands
2581 void *operator new(size_t s) {
2582 return User::operator new(s, 0);
2584 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2585 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2587 unsigned getNumSuccessors() const { return 0; }
2589 // Methods for support type inquiry through isa, cast, and dyn_cast:
2590 static inline bool classof(const UnwindInst *) { return true; }
2591 static inline bool classof(const Instruction *I) {
2592 return I->getOpcode() == Instruction::Unwind;
2594 static inline bool classof(const Value *V) {
2595 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2598 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2599 virtual unsigned getNumSuccessorsV() const;
2600 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2603 //===----------------------------------------------------------------------===//
2604 // UnreachableInst Class
2605 //===----------------------------------------------------------------------===//
2607 //===---------------------------------------------------------------------------
2608 /// UnreachableInst - This function has undefined behavior. In particular, the
2609 /// presence of this instruction indicates some higher level knowledge that the
2610 /// end of the block cannot be reached.
2612 class UnreachableInst : public TerminatorInst {
2613 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2615 virtual UnreachableInst *clone_impl() const;
2618 // allocate space for exactly zero operands
2619 void *operator new(size_t s) {
2620 return User::operator new(s, 0);
2622 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2623 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2625 unsigned getNumSuccessors() const { return 0; }
2627 // Methods for support type inquiry through isa, cast, and dyn_cast:
2628 static inline bool classof(const UnreachableInst *) { return true; }
2629 static inline bool classof(const Instruction *I) {
2630 return I->getOpcode() == Instruction::Unreachable;
2632 static inline bool classof(const Value *V) {
2633 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2636 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2637 virtual unsigned getNumSuccessorsV() const;
2638 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2641 //===----------------------------------------------------------------------===//
2643 //===----------------------------------------------------------------------===//
2645 /// @brief This class represents a truncation of integer types.
2646 class TruncInst : public CastInst {
2648 /// @brief Clone an identical TruncInst
2649 virtual TruncInst *clone_impl() const;
2652 /// @brief Constructor with insert-before-instruction semantics
2654 Value *S, ///< The value to be truncated
2655 const Type *Ty, ///< The (smaller) type to truncate to
2656 const Twine &NameStr = "", ///< A name for the new instruction
2657 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2660 /// @brief Constructor with insert-at-end-of-block semantics
2662 Value *S, ///< The value to be truncated
2663 const Type *Ty, ///< The (smaller) type to truncate to
2664 const Twine &NameStr, ///< A name for the new instruction
2665 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2668 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2669 static inline bool classof(const TruncInst *) { return true; }
2670 static inline bool classof(const Instruction *I) {
2671 return I->getOpcode() == Trunc;
2673 static inline bool classof(const Value *V) {
2674 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2678 //===----------------------------------------------------------------------===//
2680 //===----------------------------------------------------------------------===//
2682 /// @brief This class represents zero extension of integer types.
2683 class ZExtInst : public CastInst {
2685 /// @brief Clone an identical ZExtInst
2686 virtual ZExtInst *clone_impl() const;
2689 /// @brief Constructor with insert-before-instruction semantics
2691 Value *S, ///< The value to be zero extended
2692 const Type *Ty, ///< The type to zero extend to
2693 const Twine &NameStr = "", ///< A name for the new instruction
2694 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2697 /// @brief Constructor with insert-at-end semantics.
2699 Value *S, ///< The value to be zero extended
2700 const Type *Ty, ///< The type to zero extend to
2701 const Twine &NameStr, ///< A name for the new instruction
2702 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2705 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2706 static inline bool classof(const ZExtInst *) { return true; }
2707 static inline bool classof(const Instruction *I) {
2708 return I->getOpcode() == ZExt;
2710 static inline bool classof(const Value *V) {
2711 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2715 //===----------------------------------------------------------------------===//
2717 //===----------------------------------------------------------------------===//
2719 /// @brief This class represents a sign extension of integer types.
2720 class SExtInst : public CastInst {
2722 /// @brief Clone an identical SExtInst
2723 virtual SExtInst *clone_impl() const;
2726 /// @brief Constructor with insert-before-instruction semantics
2728 Value *S, ///< The value to be sign extended
2729 const Type *Ty, ///< The type to sign extend to
2730 const Twine &NameStr = "", ///< A name for the new instruction
2731 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2734 /// @brief Constructor with insert-at-end-of-block semantics
2736 Value *S, ///< The value to be sign extended
2737 const Type *Ty, ///< The type to sign extend to
2738 const Twine &NameStr, ///< A name for the new instruction
2739 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2742 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2743 static inline bool classof(const SExtInst *) { return true; }
2744 static inline bool classof(const Instruction *I) {
2745 return I->getOpcode() == SExt;
2747 static inline bool classof(const Value *V) {
2748 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2752 //===----------------------------------------------------------------------===//
2753 // FPTruncInst Class
2754 //===----------------------------------------------------------------------===//
2756 /// @brief This class represents a truncation of floating point types.
2757 class FPTruncInst : public CastInst {
2759 /// @brief Clone an identical FPTruncInst
2760 virtual FPTruncInst *clone_impl() const;
2763 /// @brief Constructor with insert-before-instruction semantics
2765 Value *S, ///< The value to be truncated
2766 const Type *Ty, ///< The type to truncate to
2767 const Twine &NameStr = "", ///< A name for the new instruction
2768 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2771 /// @brief Constructor with insert-before-instruction semantics
2773 Value *S, ///< The value to be truncated
2774 const Type *Ty, ///< The type to truncate to
2775 const Twine &NameStr, ///< A name for the new instruction
2776 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2779 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2780 static inline bool classof(const FPTruncInst *) { return true; }
2781 static inline bool classof(const Instruction *I) {
2782 return I->getOpcode() == FPTrunc;
2784 static inline bool classof(const Value *V) {
2785 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2789 //===----------------------------------------------------------------------===//
2791 //===----------------------------------------------------------------------===//
2793 /// @brief This class represents an extension of floating point types.
2794 class FPExtInst : public CastInst {
2796 /// @brief Clone an identical FPExtInst
2797 virtual FPExtInst *clone_impl() const;
2800 /// @brief Constructor with insert-before-instruction semantics
2802 Value *S, ///< The value to be extended
2803 const Type *Ty, ///< The type to extend to
2804 const Twine &NameStr = "", ///< A name for the new instruction
2805 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2808 /// @brief Constructor with insert-at-end-of-block semantics
2810 Value *S, ///< The value to be extended
2811 const Type *Ty, ///< The type to extend to
2812 const Twine &NameStr, ///< A name for the new instruction
2813 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2816 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2817 static inline bool classof(const FPExtInst *) { return true; }
2818 static inline bool classof(const Instruction *I) {
2819 return I->getOpcode() == FPExt;
2821 static inline bool classof(const Value *V) {
2822 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2826 //===----------------------------------------------------------------------===//
2828 //===----------------------------------------------------------------------===//
2830 /// @brief This class represents a cast unsigned integer to floating point.
2831 class UIToFPInst : public CastInst {
2833 /// @brief Clone an identical UIToFPInst
2834 virtual UIToFPInst *clone_impl() const;
2837 /// @brief Constructor with insert-before-instruction semantics
2839 Value *S, ///< The value to be converted
2840 const Type *Ty, ///< The type to convert to
2841 const Twine &NameStr = "", ///< A name for the new instruction
2842 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2845 /// @brief Constructor with insert-at-end-of-block semantics
2847 Value *S, ///< The value to be converted
2848 const Type *Ty, ///< The type to convert to
2849 const Twine &NameStr, ///< A name for the new instruction
2850 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2853 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2854 static inline bool classof(const UIToFPInst *) { return true; }
2855 static inline bool classof(const Instruction *I) {
2856 return I->getOpcode() == UIToFP;
2858 static inline bool classof(const Value *V) {
2859 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2863 //===----------------------------------------------------------------------===//
2865 //===----------------------------------------------------------------------===//
2867 /// @brief This class represents a cast from signed integer to floating point.
2868 class SIToFPInst : public CastInst {
2870 /// @brief Clone an identical SIToFPInst
2871 virtual SIToFPInst *clone_impl() const;
2874 /// @brief Constructor with insert-before-instruction semantics
2876 Value *S, ///< The value to be converted
2877 const Type *Ty, ///< The type to convert to
2878 const Twine &NameStr = "", ///< A name for the new instruction
2879 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2882 /// @brief Constructor with insert-at-end-of-block semantics
2884 Value *S, ///< The value to be converted
2885 const Type *Ty, ///< The type to convert to
2886 const Twine &NameStr, ///< A name for the new instruction
2887 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2890 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2891 static inline bool classof(const SIToFPInst *) { return true; }
2892 static inline bool classof(const Instruction *I) {
2893 return I->getOpcode() == SIToFP;
2895 static inline bool classof(const Value *V) {
2896 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2900 //===----------------------------------------------------------------------===//
2902 //===----------------------------------------------------------------------===//
2904 /// @brief This class represents a cast from floating point to unsigned integer
2905 class FPToUIInst : public CastInst {
2907 /// @brief Clone an identical FPToUIInst
2908 virtual FPToUIInst *clone_impl() const;
2911 /// @brief Constructor with insert-before-instruction semantics
2913 Value *S, ///< The value to be converted
2914 const Type *Ty, ///< The type to convert to
2915 const Twine &NameStr = "", ///< A name for the new instruction
2916 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2919 /// @brief Constructor with insert-at-end-of-block semantics
2921 Value *S, ///< The value to be converted
2922 const Type *Ty, ///< The type to convert to
2923 const Twine &NameStr, ///< A name for the new instruction
2924 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2927 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2928 static inline bool classof(const FPToUIInst *) { return true; }
2929 static inline bool classof(const Instruction *I) {
2930 return I->getOpcode() == FPToUI;
2932 static inline bool classof(const Value *V) {
2933 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2937 //===----------------------------------------------------------------------===//
2939 //===----------------------------------------------------------------------===//
2941 /// @brief This class represents a cast from floating point to signed integer.
2942 class FPToSIInst : public CastInst {
2944 /// @brief Clone an identical FPToSIInst
2945 virtual FPToSIInst *clone_impl() const;
2948 /// @brief Constructor with insert-before-instruction semantics
2950 Value *S, ///< The value to be converted
2951 const Type *Ty, ///< The type to convert to
2952 const Twine &NameStr = "", ///< A name for the new instruction
2953 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2956 /// @brief Constructor with insert-at-end-of-block semantics
2958 Value *S, ///< The value to be converted
2959 const Type *Ty, ///< The type to convert to
2960 const Twine &NameStr, ///< A name for the new instruction
2961 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2964 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2965 static inline bool classof(const FPToSIInst *) { return true; }
2966 static inline bool classof(const Instruction *I) {
2967 return I->getOpcode() == FPToSI;
2969 static inline bool classof(const Value *V) {
2970 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2974 //===----------------------------------------------------------------------===//
2975 // IntToPtrInst Class
2976 //===----------------------------------------------------------------------===//
2978 /// @brief This class represents a cast from an integer to a pointer.
2979 class IntToPtrInst : public CastInst {
2981 /// @brief Constructor with insert-before-instruction semantics
2983 Value *S, ///< The value to be converted
2984 const Type *Ty, ///< The type to convert to
2985 const Twine &NameStr = "", ///< A name for the new instruction
2986 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2989 /// @brief Constructor with insert-at-end-of-block semantics
2991 Value *S, ///< The value to be converted
2992 const Type *Ty, ///< The type to convert to
2993 const Twine &NameStr, ///< A name for the new instruction
2994 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2997 /// @brief Clone an identical IntToPtrInst
2998 virtual IntToPtrInst *clone_impl() const;
3000 // Methods for support type inquiry through isa, cast, and dyn_cast:
3001 static inline bool classof(const IntToPtrInst *) { return true; }
3002 static inline bool classof(const Instruction *I) {
3003 return I->getOpcode() == IntToPtr;
3005 static inline bool classof(const Value *V) {
3006 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3010 //===----------------------------------------------------------------------===//
3011 // PtrToIntInst Class
3012 //===----------------------------------------------------------------------===//
3014 /// @brief This class represents a cast from a pointer to an integer
3015 class PtrToIntInst : public CastInst {
3017 /// @brief Clone an identical PtrToIntInst
3018 virtual PtrToIntInst *clone_impl() const;
3021 /// @brief Constructor with insert-before-instruction semantics
3023 Value *S, ///< The value to be converted
3024 const Type *Ty, ///< The type to convert to
3025 const Twine &NameStr = "", ///< A name for the new instruction
3026 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3029 /// @brief Constructor with insert-at-end-of-block semantics
3031 Value *S, ///< The value to be converted
3032 const Type *Ty, ///< The type to convert to
3033 const Twine &NameStr, ///< A name for the new instruction
3034 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3037 // Methods for support type inquiry through isa, cast, and dyn_cast:
3038 static inline bool classof(const PtrToIntInst *) { return true; }
3039 static inline bool classof(const Instruction *I) {
3040 return I->getOpcode() == PtrToInt;
3042 static inline bool classof(const Value *V) {
3043 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3047 //===----------------------------------------------------------------------===//
3048 // BitCastInst Class
3049 //===----------------------------------------------------------------------===//
3051 /// @brief This class represents a no-op cast from one type to another.
3052 class BitCastInst : public CastInst {
3054 /// @brief Clone an identical BitCastInst
3055 virtual BitCastInst *clone_impl() const;
3058 /// @brief Constructor with insert-before-instruction semantics
3060 Value *S, ///< The value to be casted
3061 const Type *Ty, ///< The type to casted to
3062 const Twine &NameStr = "", ///< A name for the new instruction
3063 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3066 /// @brief Constructor with insert-at-end-of-block semantics
3068 Value *S, ///< The value to be casted
3069 const Type *Ty, ///< The type to casted to
3070 const Twine &NameStr, ///< A name for the new instruction
3071 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3074 // Methods for support type inquiry through isa, cast, and dyn_cast:
3075 static inline bool classof(const BitCastInst *) { return true; }
3076 static inline bool classof(const Instruction *I) {
3077 return I->getOpcode() == BitCast;
3079 static inline bool classof(const Value *V) {
3080 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3084 } // End llvm namespace