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 *AllocSize, Value *ArraySize = 0,
903 const Twine &Name = "");
904 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
905 const Type *IntPtrTy, const Type *AllocTy,
906 Value *AllocSize, Value *ArraySize = 0,
907 Function* MallocF = 0,
908 const Twine &Name = "");
909 /// CreateFree - Generate the IR for a call to the builtin free function.
910 static void CreateFree(Value* Source, Instruction *InsertBefore);
911 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
915 bool isTailCall() const { return SubclassData & 1; }
916 void setTailCall(bool isTC = true) {
917 SubclassData = (SubclassData & ~1) | unsigned(isTC);
920 /// Provide fast operand accessors
921 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
923 /// getCallingConv/setCallingConv - Get or set the calling convention of this
925 CallingConv::ID getCallingConv() const {
926 return static_cast<CallingConv::ID>(SubclassData >> 1);
928 void setCallingConv(CallingConv::ID CC) {
929 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
932 /// getAttributes - Return the parameter attributes for this call.
934 const AttrListPtr &getAttributes() const { return AttributeList; }
936 /// setAttributes - Set the parameter attributes for this call.
938 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
940 /// addAttribute - adds the attribute to the list of attributes.
941 void addAttribute(unsigned i, Attributes attr);
943 /// removeAttribute - removes the attribute from the list of attributes.
944 void removeAttribute(unsigned i, Attributes attr);
946 /// @brief Determine whether the call or the callee has the given attribute.
947 bool paramHasAttr(unsigned i, Attributes attr) const;
949 /// @brief Extract the alignment for a call or parameter (0=unknown).
950 unsigned getParamAlignment(unsigned i) const {
951 return AttributeList.getParamAlignment(i);
954 /// @brief Determine if the call does not access memory.
955 bool doesNotAccessMemory() const {
956 return paramHasAttr(~0, Attribute::ReadNone);
958 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
959 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
960 else removeAttribute(~0, Attribute::ReadNone);
963 /// @brief Determine if the call does not access or only reads memory.
964 bool onlyReadsMemory() const {
965 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
967 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
968 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
969 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
972 /// @brief Determine if the call cannot return.
973 bool doesNotReturn() const {
974 return paramHasAttr(~0, Attribute::NoReturn);
976 void setDoesNotReturn(bool DoesNotReturn = true) {
977 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
978 else removeAttribute(~0, Attribute::NoReturn);
981 /// @brief Determine if the call cannot unwind.
982 bool doesNotThrow() const {
983 return paramHasAttr(~0, Attribute::NoUnwind);
985 void setDoesNotThrow(bool DoesNotThrow = true) {
986 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
987 else removeAttribute(~0, Attribute::NoUnwind);
990 /// @brief Determine if the call returns a structure through first
991 /// pointer argument.
992 bool hasStructRetAttr() const {
993 // Be friendly and also check the callee.
994 return paramHasAttr(1, Attribute::StructRet);
997 /// @brief Determine if any call argument is an aggregate passed by value.
998 bool hasByValArgument() const {
999 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1002 /// getCalledFunction - Return the function called, or null if this is an
1003 /// indirect function invocation.
1005 Function *getCalledFunction() const {
1006 return dyn_cast<Function>(Op<0>());
1009 /// getCalledValue - Get a pointer to the function that is invoked by this
1011 const Value *getCalledValue() const { return Op<0>(); }
1012 Value *getCalledValue() { return Op<0>(); }
1014 /// setCalledFunction - Set the function called.
1015 void setCalledFunction(Value* Fn) {
1019 // Methods for support type inquiry through isa, cast, and dyn_cast:
1020 static inline bool classof(const CallInst *) { return true; }
1021 static inline bool classof(const Instruction *I) {
1022 return I->getOpcode() == Instruction::Call;
1024 static inline bool classof(const Value *V) {
1025 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1030 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1033 template<typename InputIterator>
1034 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1035 const Twine &NameStr, BasicBlock *InsertAtEnd)
1036 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1037 ->getElementType())->getReturnType(),
1039 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1040 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1041 init(Func, ArgBegin, ArgEnd, NameStr,
1042 typename std::iterator_traits<InputIterator>::iterator_category());
1045 template<typename InputIterator>
1046 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1047 const Twine &NameStr, Instruction *InsertBefore)
1048 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1049 ->getElementType())->getReturnType(),
1051 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1052 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1053 init(Func, ArgBegin, ArgEnd, NameStr,
1054 typename std::iterator_traits<InputIterator>::iterator_category());
1057 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1059 //===----------------------------------------------------------------------===//
1061 //===----------------------------------------------------------------------===//
1063 /// SelectInst - This class represents the LLVM 'select' instruction.
1065 class SelectInst : public Instruction {
1066 void init(Value *C, Value *S1, Value *S2) {
1067 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1073 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1074 Instruction *InsertBefore)
1075 : Instruction(S1->getType(), Instruction::Select,
1076 &Op<0>(), 3, InsertBefore) {
1080 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1081 BasicBlock *InsertAtEnd)
1082 : Instruction(S1->getType(), Instruction::Select,
1083 &Op<0>(), 3, InsertAtEnd) {
1088 virtual SelectInst *clone_impl() const;
1090 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1091 const Twine &NameStr = "",
1092 Instruction *InsertBefore = 0) {
1093 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1095 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1096 const Twine &NameStr,
1097 BasicBlock *InsertAtEnd) {
1098 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1101 const Value *getCondition() const { return Op<0>(); }
1102 const Value *getTrueValue() const { return Op<1>(); }
1103 const Value *getFalseValue() const { return Op<2>(); }
1104 Value *getCondition() { return Op<0>(); }
1105 Value *getTrueValue() { return Op<1>(); }
1106 Value *getFalseValue() { return Op<2>(); }
1108 /// areInvalidOperands - Return a string if the specified operands are invalid
1109 /// for a select operation, otherwise return null.
1110 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1112 /// Transparently provide more efficient getOperand methods.
1113 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1115 OtherOps getOpcode() const {
1116 return static_cast<OtherOps>(Instruction::getOpcode());
1119 // Methods for support type inquiry through isa, cast, and dyn_cast:
1120 static inline bool classof(const SelectInst *) { return true; }
1121 static inline bool classof(const Instruction *I) {
1122 return I->getOpcode() == Instruction::Select;
1124 static inline bool classof(const Value *V) {
1125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1130 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1133 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1135 //===----------------------------------------------------------------------===//
1137 //===----------------------------------------------------------------------===//
1139 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1140 /// an argument of the specified type given a va_list and increments that list
1142 class VAArgInst : public UnaryInstruction {
1144 virtual VAArgInst *clone_impl() const;
1147 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1148 Instruction *InsertBefore = 0)
1149 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1152 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1153 BasicBlock *InsertAtEnd)
1154 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1158 // Methods for support type inquiry through isa, cast, and dyn_cast:
1159 static inline bool classof(const VAArgInst *) { return true; }
1160 static inline bool classof(const Instruction *I) {
1161 return I->getOpcode() == VAArg;
1163 static inline bool classof(const Value *V) {
1164 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1168 //===----------------------------------------------------------------------===//
1169 // ExtractElementInst Class
1170 //===----------------------------------------------------------------------===//
1172 /// ExtractElementInst - This instruction extracts a single (scalar)
1173 /// element from a VectorType value
1175 class ExtractElementInst : public Instruction {
1176 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1177 Instruction *InsertBefore = 0);
1178 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1179 BasicBlock *InsertAtEnd);
1181 virtual ExtractElementInst *clone_impl() const;
1184 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1185 const Twine &NameStr = "",
1186 Instruction *InsertBefore = 0) {
1187 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1189 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1190 const Twine &NameStr,
1191 BasicBlock *InsertAtEnd) {
1192 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1195 /// isValidOperands - Return true if an extractelement instruction can be
1196 /// formed with the specified operands.
1197 static bool isValidOperands(const Value *Vec, const Value *Idx);
1199 Value *getVectorOperand() { return Op<0>(); }
1200 Value *getIndexOperand() { return Op<1>(); }
1201 const Value *getVectorOperand() const { return Op<0>(); }
1202 const Value *getIndexOperand() const { return Op<1>(); }
1204 const VectorType *getVectorOperandType() const {
1205 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1209 /// Transparently provide more efficient getOperand methods.
1210 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1212 // Methods for support type inquiry through isa, cast, and dyn_cast:
1213 static inline bool classof(const ExtractElementInst *) { return true; }
1214 static inline bool classof(const Instruction *I) {
1215 return I->getOpcode() == Instruction::ExtractElement;
1217 static inline bool classof(const Value *V) {
1218 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1223 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1226 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1228 //===----------------------------------------------------------------------===//
1229 // InsertElementInst Class
1230 //===----------------------------------------------------------------------===//
1232 /// InsertElementInst - This instruction inserts a single (scalar)
1233 /// element into a VectorType value
1235 class InsertElementInst : public Instruction {
1236 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1237 const Twine &NameStr = "",
1238 Instruction *InsertBefore = 0);
1239 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1240 const Twine &NameStr, BasicBlock *InsertAtEnd);
1242 virtual InsertElementInst *clone_impl() const;
1245 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1246 const Twine &NameStr = "",
1247 Instruction *InsertBefore = 0) {
1248 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1250 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1251 const Twine &NameStr,
1252 BasicBlock *InsertAtEnd) {
1253 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1256 /// isValidOperands - Return true if an insertelement instruction can be
1257 /// formed with the specified operands.
1258 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1261 /// getType - Overload to return most specific vector type.
1263 const VectorType *getType() const {
1264 return reinterpret_cast<const VectorType*>(Instruction::getType());
1267 /// Transparently provide more efficient getOperand methods.
1268 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1270 // Methods for support type inquiry through isa, cast, and dyn_cast:
1271 static inline bool classof(const InsertElementInst *) { return true; }
1272 static inline bool classof(const Instruction *I) {
1273 return I->getOpcode() == Instruction::InsertElement;
1275 static inline bool classof(const Value *V) {
1276 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1281 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1284 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1286 //===----------------------------------------------------------------------===//
1287 // ShuffleVectorInst Class
1288 //===----------------------------------------------------------------------===//
1290 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1293 class ShuffleVectorInst : public Instruction {
1295 virtual ShuffleVectorInst *clone_impl() const;
1298 // allocate space for exactly three operands
1299 void *operator new(size_t s) {
1300 return User::operator new(s, 3);
1302 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1303 const Twine &NameStr = "",
1304 Instruction *InsertBefor = 0);
1305 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1306 const Twine &NameStr, BasicBlock *InsertAtEnd);
1308 /// isValidOperands - Return true if a shufflevector instruction can be
1309 /// formed with the specified operands.
1310 static bool isValidOperands(const Value *V1, const Value *V2,
1313 /// getType - Overload to return most specific vector type.
1315 const VectorType *getType() const {
1316 return reinterpret_cast<const VectorType*>(Instruction::getType());
1319 /// Transparently provide more efficient getOperand methods.
1320 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1322 /// getMaskValue - Return the index from the shuffle mask for the specified
1323 /// output result. This is either -1 if the element is undef or a number less
1324 /// than 2*numelements.
1325 int getMaskValue(unsigned i) const;
1327 // Methods for support type inquiry through isa, cast, and dyn_cast:
1328 static inline bool classof(const ShuffleVectorInst *) { return true; }
1329 static inline bool classof(const Instruction *I) {
1330 return I->getOpcode() == Instruction::ShuffleVector;
1332 static inline bool classof(const Value *V) {
1333 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1338 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1341 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1343 //===----------------------------------------------------------------------===//
1344 // ExtractValueInst Class
1345 //===----------------------------------------------------------------------===//
1347 /// ExtractValueInst - This instruction extracts a struct member or array
1348 /// element value from an aggregate value.
1350 class ExtractValueInst : public UnaryInstruction {
1351 SmallVector<unsigned, 4> Indices;
1353 ExtractValueInst(const ExtractValueInst &EVI);
1354 void init(const unsigned *Idx, unsigned NumIdx,
1355 const Twine &NameStr);
1356 void init(unsigned Idx, const Twine &NameStr);
1358 template<typename InputIterator>
1359 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1360 const Twine &NameStr,
1361 // This argument ensures that we have an iterator we can
1362 // do arithmetic on in constant time
1363 std::random_access_iterator_tag) {
1364 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1366 // There's no fundamental reason why we require at least one index
1367 // (other than weirdness with &*IdxBegin being invalid; see
1368 // getelementptr's init routine for example). But there's no
1369 // present need to support it.
1370 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1372 // This requires that the iterator points to contiguous memory.
1373 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1374 // we have to build an array here
1377 /// getIndexedType - Returns the type of the element that would be extracted
1378 /// with an extractvalue instruction with the specified parameters.
1380 /// Null is returned if the indices are invalid for the specified
1383 static const Type *getIndexedType(const Type *Agg,
1384 const unsigned *Idx, unsigned NumIdx);
1386 template<typename InputIterator>
1387 static const Type *getIndexedType(const Type *Ptr,
1388 InputIterator IdxBegin,
1389 InputIterator IdxEnd,
1390 // This argument ensures that we
1391 // have an iterator we can do
1392 // arithmetic on in constant time
1393 std::random_access_iterator_tag) {
1394 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1397 // This requires that the iterator points to contiguous memory.
1398 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1400 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1403 /// Constructors - Create a extractvalue instruction with a base aggregate
1404 /// value and a list of indices. The first ctor can optionally insert before
1405 /// an existing instruction, the second appends the new instruction to the
1406 /// specified BasicBlock.
1407 template<typename InputIterator>
1408 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1409 InputIterator IdxEnd,
1410 const Twine &NameStr,
1411 Instruction *InsertBefore);
1412 template<typename InputIterator>
1413 inline ExtractValueInst(Value *Agg,
1414 InputIterator IdxBegin, InputIterator IdxEnd,
1415 const Twine &NameStr, BasicBlock *InsertAtEnd);
1417 // allocate space for exactly one operand
1418 void *operator new(size_t s) {
1419 return User::operator new(s, 1);
1422 virtual ExtractValueInst *clone_impl() const;
1425 template<typename InputIterator>
1426 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1427 InputIterator IdxEnd,
1428 const Twine &NameStr = "",
1429 Instruction *InsertBefore = 0) {
1431 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1433 template<typename InputIterator>
1434 static ExtractValueInst *Create(Value *Agg,
1435 InputIterator IdxBegin, InputIterator IdxEnd,
1436 const Twine &NameStr,
1437 BasicBlock *InsertAtEnd) {
1438 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1441 /// Constructors - These two creators are convenience methods because one
1442 /// index extractvalue instructions are much more common than those with
1444 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1445 const Twine &NameStr = "",
1446 Instruction *InsertBefore = 0) {
1447 unsigned Idxs[1] = { Idx };
1448 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1450 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1451 const Twine &NameStr,
1452 BasicBlock *InsertAtEnd) {
1453 unsigned Idxs[1] = { Idx };
1454 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1457 /// getIndexedType - Returns the type of the element that would be extracted
1458 /// with an extractvalue instruction with the specified parameters.
1460 /// Null is returned if the indices are invalid for the specified
1463 template<typename InputIterator>
1464 static const Type *getIndexedType(const Type *Ptr,
1465 InputIterator IdxBegin,
1466 InputIterator IdxEnd) {
1467 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1468 typename std::iterator_traits<InputIterator>::
1469 iterator_category());
1471 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1473 typedef const unsigned* idx_iterator;
1474 inline idx_iterator idx_begin() const { return Indices.begin(); }
1475 inline idx_iterator idx_end() const { return Indices.end(); }
1477 Value *getAggregateOperand() {
1478 return getOperand(0);
1480 const Value *getAggregateOperand() const {
1481 return getOperand(0);
1483 static unsigned getAggregateOperandIndex() {
1484 return 0U; // get index for modifying correct operand
1487 unsigned getNumIndices() const { // Note: always non-negative
1488 return (unsigned)Indices.size();
1491 bool hasIndices() const {
1495 // Methods for support type inquiry through isa, cast, and dyn_cast:
1496 static inline bool classof(const ExtractValueInst *) { return true; }
1497 static inline bool classof(const Instruction *I) {
1498 return I->getOpcode() == Instruction::ExtractValue;
1500 static inline bool classof(const Value *V) {
1501 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1505 template<typename InputIterator>
1506 ExtractValueInst::ExtractValueInst(Value *Agg,
1507 InputIterator IdxBegin,
1508 InputIterator IdxEnd,
1509 const Twine &NameStr,
1510 Instruction *InsertBefore)
1511 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1513 ExtractValue, Agg, InsertBefore) {
1514 init(IdxBegin, IdxEnd, NameStr,
1515 typename std::iterator_traits<InputIterator>::iterator_category());
1517 template<typename InputIterator>
1518 ExtractValueInst::ExtractValueInst(Value *Agg,
1519 InputIterator IdxBegin,
1520 InputIterator IdxEnd,
1521 const Twine &NameStr,
1522 BasicBlock *InsertAtEnd)
1523 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1525 ExtractValue, Agg, InsertAtEnd) {
1526 init(IdxBegin, IdxEnd, NameStr,
1527 typename std::iterator_traits<InputIterator>::iterator_category());
1531 //===----------------------------------------------------------------------===//
1532 // InsertValueInst Class
1533 //===----------------------------------------------------------------------===//
1535 /// InsertValueInst - This instruction inserts a struct field of array element
1536 /// value into an aggregate value.
1538 class InsertValueInst : public Instruction {
1539 SmallVector<unsigned, 4> Indices;
1541 void *operator new(size_t, unsigned); // Do not implement
1542 InsertValueInst(const InsertValueInst &IVI);
1543 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1544 const Twine &NameStr);
1545 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1547 template<typename InputIterator>
1548 void init(Value *Agg, Value *Val,
1549 InputIterator IdxBegin, InputIterator IdxEnd,
1550 const Twine &NameStr,
1551 // This argument ensures that we have an iterator we can
1552 // do arithmetic on in constant time
1553 std::random_access_iterator_tag) {
1554 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1556 // There's no fundamental reason why we require at least one index
1557 // (other than weirdness with &*IdxBegin being invalid; see
1558 // getelementptr's init routine for example). But there's no
1559 // present need to support it.
1560 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1562 // This requires that the iterator points to contiguous memory.
1563 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1564 // we have to build an array here
1567 /// Constructors - Create a insertvalue instruction with a base aggregate
1568 /// value, a value to insert, and a list of indices. The first ctor can
1569 /// optionally insert before an existing instruction, the second appends
1570 /// the new instruction to the specified BasicBlock.
1571 template<typename InputIterator>
1572 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1573 InputIterator IdxEnd,
1574 const Twine &NameStr,
1575 Instruction *InsertBefore);
1576 template<typename InputIterator>
1577 inline InsertValueInst(Value *Agg, Value *Val,
1578 InputIterator IdxBegin, InputIterator IdxEnd,
1579 const Twine &NameStr, BasicBlock *InsertAtEnd);
1581 /// Constructors - These two constructors are convenience methods because one
1582 /// and two index insertvalue instructions are so common.
1583 InsertValueInst(Value *Agg, Value *Val,
1584 unsigned Idx, const Twine &NameStr = "",
1585 Instruction *InsertBefore = 0);
1586 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1587 const Twine &NameStr, BasicBlock *InsertAtEnd);
1589 virtual InsertValueInst *clone_impl() const;
1591 // allocate space for exactly two operands
1592 void *operator new(size_t s) {
1593 return User::operator new(s, 2);
1596 template<typename InputIterator>
1597 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1598 InputIterator IdxEnd,
1599 const Twine &NameStr = "",
1600 Instruction *InsertBefore = 0) {
1601 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1602 NameStr, InsertBefore);
1604 template<typename InputIterator>
1605 static InsertValueInst *Create(Value *Agg, Value *Val,
1606 InputIterator IdxBegin, InputIterator IdxEnd,
1607 const Twine &NameStr,
1608 BasicBlock *InsertAtEnd) {
1609 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1610 NameStr, InsertAtEnd);
1613 /// Constructors - These two creators are convenience methods because one
1614 /// index insertvalue instructions are much more common than those with
1616 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1617 const Twine &NameStr = "",
1618 Instruction *InsertBefore = 0) {
1619 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1621 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1622 const Twine &NameStr,
1623 BasicBlock *InsertAtEnd) {
1624 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1627 /// Transparently provide more efficient getOperand methods.
1628 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1630 typedef const unsigned* idx_iterator;
1631 inline idx_iterator idx_begin() const { return Indices.begin(); }
1632 inline idx_iterator idx_end() const { return Indices.end(); }
1634 Value *getAggregateOperand() {
1635 return getOperand(0);
1637 const Value *getAggregateOperand() const {
1638 return getOperand(0);
1640 static unsigned getAggregateOperandIndex() {
1641 return 0U; // get index for modifying correct operand
1644 Value *getInsertedValueOperand() {
1645 return getOperand(1);
1647 const Value *getInsertedValueOperand() const {
1648 return getOperand(1);
1650 static unsigned getInsertedValueOperandIndex() {
1651 return 1U; // get index for modifying correct operand
1654 unsigned getNumIndices() const { // Note: always non-negative
1655 return (unsigned)Indices.size();
1658 bool hasIndices() const {
1662 // Methods for support type inquiry through isa, cast, and dyn_cast:
1663 static inline bool classof(const InsertValueInst *) { return true; }
1664 static inline bool classof(const Instruction *I) {
1665 return I->getOpcode() == Instruction::InsertValue;
1667 static inline bool classof(const Value *V) {
1668 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1673 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1676 template<typename InputIterator>
1677 InsertValueInst::InsertValueInst(Value *Agg,
1679 InputIterator IdxBegin,
1680 InputIterator IdxEnd,
1681 const Twine &NameStr,
1682 Instruction *InsertBefore)
1683 : Instruction(Agg->getType(), InsertValue,
1684 OperandTraits<InsertValueInst>::op_begin(this),
1686 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1687 typename std::iterator_traits<InputIterator>::iterator_category());
1689 template<typename InputIterator>
1690 InsertValueInst::InsertValueInst(Value *Agg,
1692 InputIterator IdxBegin,
1693 InputIterator IdxEnd,
1694 const Twine &NameStr,
1695 BasicBlock *InsertAtEnd)
1696 : Instruction(Agg->getType(), InsertValue,
1697 OperandTraits<InsertValueInst>::op_begin(this),
1699 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1700 typename std::iterator_traits<InputIterator>::iterator_category());
1703 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1705 //===----------------------------------------------------------------------===//
1707 //===----------------------------------------------------------------------===//
1709 // PHINode - The PHINode class is used to represent the magical mystical PHI
1710 // node, that can not exist in nature, but can be synthesized in a computer
1711 // scientist's overactive imagination.
1713 class PHINode : public Instruction {
1714 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1715 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1716 /// the number actually in use.
1717 unsigned ReservedSpace;
1718 PHINode(const PHINode &PN);
1719 // allocate space for exactly zero operands
1720 void *operator new(size_t s) {
1721 return User::operator new(s, 0);
1723 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1724 Instruction *InsertBefore = 0)
1725 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1730 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1731 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1736 virtual PHINode *clone_impl() const;
1738 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1739 Instruction *InsertBefore = 0) {
1740 return new PHINode(Ty, NameStr, InsertBefore);
1742 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1743 BasicBlock *InsertAtEnd) {
1744 return new PHINode(Ty, NameStr, InsertAtEnd);
1748 /// reserveOperandSpace - This method can be used to avoid repeated
1749 /// reallocation of PHI operand lists by reserving space for the correct
1750 /// number of operands before adding them. Unlike normal vector reserves,
1751 /// this method can also be used to trim the operand space.
1752 void reserveOperandSpace(unsigned NumValues) {
1753 resizeOperands(NumValues*2);
1756 /// Provide fast operand accessors
1757 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1759 /// getNumIncomingValues - Return the number of incoming edges
1761 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1763 /// getIncomingValue - Return incoming value number x
1765 Value *getIncomingValue(unsigned i) const {
1766 assert(i*2 < getNumOperands() && "Invalid value number!");
1767 return getOperand(i*2);
1769 void setIncomingValue(unsigned i, Value *V) {
1770 assert(i*2 < getNumOperands() && "Invalid value number!");
1773 static unsigned getOperandNumForIncomingValue(unsigned i) {
1776 static unsigned getIncomingValueNumForOperand(unsigned i) {
1777 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1781 /// getIncomingBlock - Return incoming basic block #i.
1783 BasicBlock *getIncomingBlock(unsigned i) const {
1784 return cast<BasicBlock>(getOperand(i*2+1));
1787 /// getIncomingBlock - Return incoming basic block corresponding
1788 /// to an operand of the PHI.
1790 BasicBlock *getIncomingBlock(const Use &U) const {
1791 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1792 return cast<BasicBlock>((&U + 1)->get());
1795 /// getIncomingBlock - Return incoming basic block corresponding
1796 /// to value use iterator.
1798 template <typename U>
1799 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1800 return getIncomingBlock(I.getUse());
1804 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1805 setOperand(i*2+1, (Value*)BB);
1807 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1810 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1811 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1815 /// addIncoming - Add an incoming value to the end of the PHI list
1817 void addIncoming(Value *V, BasicBlock *BB) {
1818 assert(V && "PHI node got a null value!");
1819 assert(BB && "PHI node got a null basic block!");
1820 assert(getType() == V->getType() &&
1821 "All operands to PHI node must be the same type as the PHI node!");
1822 unsigned OpNo = NumOperands;
1823 if (OpNo+2 > ReservedSpace)
1824 resizeOperands(0); // Get more space!
1825 // Initialize some new operands.
1826 NumOperands = OpNo+2;
1827 OperandList[OpNo] = V;
1828 OperandList[OpNo+1] = (Value*)BB;
1831 /// removeIncomingValue - Remove an incoming value. This is useful if a
1832 /// predecessor basic block is deleted. The value removed is returned.
1834 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1835 /// is true), the PHI node is destroyed and any uses of it are replaced with
1836 /// dummy values. The only time there should be zero incoming values to a PHI
1837 /// node is when the block is dead, so this strategy is sound.
1839 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1841 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1842 int Idx = getBasicBlockIndex(BB);
1843 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1844 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1847 /// getBasicBlockIndex - Return the first index of the specified basic
1848 /// block in the value list for this PHI. Returns -1 if no instance.
1850 int getBasicBlockIndex(const BasicBlock *BB) const {
1851 Use *OL = OperandList;
1852 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1853 if (OL[i+1].get() == (const Value*)BB) return i/2;
1857 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1858 return getIncomingValue(getBasicBlockIndex(BB));
1861 /// hasConstantValue - If the specified PHI node always merges together the
1862 /// same value, return the value, otherwise return null.
1864 /// If the PHI has undef operands, but all the rest of the operands are
1865 /// some unique value, return that value if it can be proved that the
1866 /// value dominates the PHI. If DT is null, use a conservative check,
1867 /// otherwise use DT to test for dominance.
1869 Value *hasConstantValue(DominatorTree *DT = 0) const;
1871 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1872 static inline bool classof(const PHINode *) { return true; }
1873 static inline bool classof(const Instruction *I) {
1874 return I->getOpcode() == Instruction::PHI;
1876 static inline bool classof(const Value *V) {
1877 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1880 void resizeOperands(unsigned NumOperands);
1884 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1887 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1890 //===----------------------------------------------------------------------===//
1892 //===----------------------------------------------------------------------===//
1894 //===---------------------------------------------------------------------------
1895 /// ReturnInst - Return a value (possibly void), from a function. Execution
1896 /// does not continue in this function any longer.
1898 class ReturnInst : public TerminatorInst {
1899 ReturnInst(const ReturnInst &RI);
1902 // ReturnInst constructors:
1903 // ReturnInst() - 'ret void' instruction
1904 // ReturnInst( null) - 'ret void' instruction
1905 // ReturnInst(Value* X) - 'ret X' instruction
1906 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1907 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1908 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1909 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1911 // NOTE: If the Value* passed is of type void then the constructor behaves as
1912 // if it was passed NULL.
1913 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1914 Instruction *InsertBefore = 0);
1915 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1916 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1918 virtual ReturnInst *clone_impl() const;
1920 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1921 Instruction *InsertBefore = 0) {
1922 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1924 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1925 BasicBlock *InsertAtEnd) {
1926 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1928 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1929 return new(0) ReturnInst(C, InsertAtEnd);
1931 virtual ~ReturnInst();
1933 /// Provide fast operand accessors
1934 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1936 /// Convenience accessor
1937 Value *getReturnValue(unsigned n = 0) const {
1938 return n < getNumOperands()
1943 unsigned getNumSuccessors() const { return 0; }
1945 // Methods for support type inquiry through isa, cast, and dyn_cast:
1946 static inline bool classof(const ReturnInst *) { return true; }
1947 static inline bool classof(const Instruction *I) {
1948 return (I->getOpcode() == Instruction::Ret);
1950 static inline bool classof(const Value *V) {
1951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1954 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1955 virtual unsigned getNumSuccessorsV() const;
1956 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1960 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
1963 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1965 //===----------------------------------------------------------------------===//
1967 //===----------------------------------------------------------------------===//
1969 //===---------------------------------------------------------------------------
1970 /// BranchInst - Conditional or Unconditional Branch instruction.
1972 class BranchInst : public TerminatorInst {
1973 /// Ops list - Branches are strange. The operands are ordered:
1974 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
1975 /// they don't have to check for cond/uncond branchness. These are mostly
1976 /// accessed relative from op_end().
1977 BranchInst(const BranchInst &BI);
1979 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1980 // BranchInst(BB *B) - 'br B'
1981 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1982 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1983 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1984 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1985 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1986 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1987 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1988 Instruction *InsertBefore = 0);
1989 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1990 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1991 BasicBlock *InsertAtEnd);
1993 virtual BranchInst *clone_impl() const;
1995 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1996 return new(1, true) BranchInst(IfTrue, InsertBefore);
1998 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1999 Value *Cond, Instruction *InsertBefore = 0) {
2000 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2002 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2003 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2005 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2006 Value *Cond, BasicBlock *InsertAtEnd) {
2007 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2012 /// Transparently provide more efficient getOperand methods.
2013 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2015 bool isUnconditional() const { return getNumOperands() == 1; }
2016 bool isConditional() const { return getNumOperands() == 3; }
2018 Value *getCondition() const {
2019 assert(isConditional() && "Cannot get condition of an uncond branch!");
2023 void setCondition(Value *V) {
2024 assert(isConditional() && "Cannot set condition of unconditional branch!");
2028 // setUnconditionalDest - Change the current branch to an unconditional branch
2029 // targeting the specified block.
2030 // FIXME: Eliminate this ugly method.
2031 void setUnconditionalDest(BasicBlock *Dest) {
2032 Op<-1>() = (Value*)Dest;
2033 if (isConditional()) { // Convert this to an uncond branch.
2037 OperandList = op_begin();
2041 unsigned getNumSuccessors() const { return 1+isConditional(); }
2043 BasicBlock *getSuccessor(unsigned i) const {
2044 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2045 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2048 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2049 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2050 *(&Op<-1>() - idx) = (Value*)NewSucc;
2053 // Methods for support type inquiry through isa, cast, and dyn_cast:
2054 static inline bool classof(const BranchInst *) { return true; }
2055 static inline bool classof(const Instruction *I) {
2056 return (I->getOpcode() == Instruction::Br);
2058 static inline bool classof(const Value *V) {
2059 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2062 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2063 virtual unsigned getNumSuccessorsV() const;
2064 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2068 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2070 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2072 //===----------------------------------------------------------------------===//
2074 //===----------------------------------------------------------------------===//
2076 //===---------------------------------------------------------------------------
2077 /// SwitchInst - Multiway switch
2079 class SwitchInst : public TerminatorInst {
2080 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2081 unsigned ReservedSpace;
2082 // Operand[0] = Value to switch on
2083 // Operand[1] = Default basic block destination
2084 // Operand[2n ] = Value to match
2085 // Operand[2n+1] = BasicBlock to go to on match
2086 SwitchInst(const SwitchInst &SI);
2087 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2088 void resizeOperands(unsigned No);
2089 // allocate space for exactly zero operands
2090 void *operator new(size_t s) {
2091 return User::operator new(s, 0);
2093 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2094 /// switch on and a default destination. The number of additional cases can
2095 /// be specified here to make memory allocation more efficient. This
2096 /// constructor can also autoinsert before another instruction.
2097 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2098 Instruction *InsertBefore);
2100 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2101 /// switch on and a default destination. The number of additional cases can
2102 /// be specified here to make memory allocation more efficient. This
2103 /// constructor also autoinserts at the end of the specified BasicBlock.
2104 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2105 BasicBlock *InsertAtEnd);
2107 virtual SwitchInst *clone_impl() const;
2109 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2110 unsigned NumCases, Instruction *InsertBefore = 0) {
2111 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2113 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2114 unsigned NumCases, BasicBlock *InsertAtEnd) {
2115 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2119 /// Provide fast operand accessors
2120 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2122 // Accessor Methods for Switch stmt
2123 Value *getCondition() const { return getOperand(0); }
2124 void setCondition(Value *V) { setOperand(0, V); }
2126 BasicBlock *getDefaultDest() const {
2127 return cast<BasicBlock>(getOperand(1));
2130 /// getNumCases - return the number of 'cases' in this switch instruction.
2131 /// Note that case #0 is always the default case.
2132 unsigned getNumCases() const {
2133 return getNumOperands()/2;
2136 /// getCaseValue - Return the specified case value. Note that case #0, the
2137 /// default destination, does not have a case value.
2138 ConstantInt *getCaseValue(unsigned i) {
2139 assert(i && i < getNumCases() && "Illegal case value to get!");
2140 return getSuccessorValue(i);
2143 /// getCaseValue - Return the specified case value. Note that case #0, the
2144 /// default destination, does not have a case value.
2145 const ConstantInt *getCaseValue(unsigned i) const {
2146 assert(i && i < getNumCases() && "Illegal case value to get!");
2147 return getSuccessorValue(i);
2150 /// findCaseValue - Search all of the case values for the specified constant.
2151 /// If it is explicitly handled, return the case number of it, otherwise
2152 /// return 0 to indicate that it is handled by the default handler.
2153 unsigned findCaseValue(const ConstantInt *C) const {
2154 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2155 if (getCaseValue(i) == C)
2160 /// findCaseDest - Finds the unique case value for a given successor. Returns
2161 /// null if the successor is not found, not unique, or is the default case.
2162 ConstantInt *findCaseDest(BasicBlock *BB) {
2163 if (BB == getDefaultDest()) return NULL;
2165 ConstantInt *CI = NULL;
2166 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2167 if (getSuccessor(i) == BB) {
2168 if (CI) return NULL; // Multiple cases lead to BB.
2169 else CI = getCaseValue(i);
2175 /// addCase - Add an entry to the switch instruction...
2177 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2179 /// removeCase - This method removes the specified successor from the switch
2180 /// instruction. Note that this cannot be used to remove the default
2181 /// destination (successor #0).
2183 void removeCase(unsigned idx);
2185 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2186 BasicBlock *getSuccessor(unsigned idx) const {
2187 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2188 return cast<BasicBlock>(getOperand(idx*2+1));
2190 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2191 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2192 setOperand(idx*2+1, (Value*)NewSucc);
2195 // getSuccessorValue - Return the value associated with the specified
2197 ConstantInt *getSuccessorValue(unsigned idx) const {
2198 assert(idx < getNumSuccessors() && "Successor # out of range!");
2199 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2202 // Methods for support type inquiry through isa, cast, and dyn_cast:
2203 static inline bool classof(const SwitchInst *) { return true; }
2204 static inline bool classof(const Instruction *I) {
2205 return I->getOpcode() == Instruction::Switch;
2207 static inline bool classof(const Value *V) {
2208 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2211 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2212 virtual unsigned getNumSuccessorsV() const;
2213 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2217 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2220 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2223 //===----------------------------------------------------------------------===//
2224 // IndirectBrInst Class
2225 //===----------------------------------------------------------------------===//
2227 //===---------------------------------------------------------------------------
2228 /// IndirectBrInst - Indirect Branch Instruction.
2230 class IndirectBrInst : public TerminatorInst {
2231 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2232 unsigned ReservedSpace;
2233 // Operand[0] = Value to switch on
2234 // Operand[1] = Default basic block destination
2235 // Operand[2n ] = Value to match
2236 // Operand[2n+1] = BasicBlock to go to on match
2237 IndirectBrInst(const IndirectBrInst &IBI);
2238 void init(Value *Address, unsigned NumDests);
2239 void resizeOperands(unsigned No);
2240 // allocate space for exactly zero operands
2241 void *operator new(size_t s) {
2242 return User::operator new(s, 0);
2244 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2245 /// Address to jump to. The number of expected destinations can be specified
2246 /// here to make memory allocation more efficient. This constructor can also
2247 /// autoinsert before another instruction.
2248 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2250 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2251 /// Address to jump to. The number of expected destinations can be specified
2252 /// here to make memory allocation more efficient. This constructor also
2253 /// autoinserts at the end of the specified BasicBlock.
2254 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2256 virtual IndirectBrInst *clone_impl() const;
2258 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2259 Instruction *InsertBefore = 0) {
2260 return new IndirectBrInst(Address, NumDests, InsertBefore);
2262 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2263 BasicBlock *InsertAtEnd) {
2264 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2268 /// Provide fast operand accessors.
2269 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2271 // Accessor Methods for IndirectBrInst instruction.
2272 Value *getAddress() { return getOperand(0); }
2273 const Value *getAddress() const { return getOperand(0); }
2274 void setAddress(Value *V) { setOperand(0, V); }
2277 /// getNumDestinations - return the number of possible destinations in this
2278 /// indirectbr instruction.
2279 unsigned getNumDestinations() const { return getNumOperands()-1; }
2281 /// getDestination - Return the specified destination.
2282 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2283 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2285 /// addDestination - Add a destination.
2287 void addDestination(BasicBlock *Dest);
2289 /// removeDestination - This method removes the specified successor from the
2290 /// indirectbr instruction.
2291 void removeDestination(unsigned i);
2293 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2294 BasicBlock *getSuccessor(unsigned i) const {
2295 return cast<BasicBlock>(getOperand(i+1));
2297 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2298 setOperand(i+1, (Value*)NewSucc);
2301 // Methods for support type inquiry through isa, cast, and dyn_cast:
2302 static inline bool classof(const IndirectBrInst *) { return true; }
2303 static inline bool classof(const Instruction *I) {
2304 return I->getOpcode() == Instruction::IndirectBr;
2306 static inline bool classof(const Value *V) {
2307 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2310 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2311 virtual unsigned getNumSuccessorsV() const;
2312 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2316 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2319 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2322 //===----------------------------------------------------------------------===//
2324 //===----------------------------------------------------------------------===//
2326 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2327 /// calling convention of the call.
2329 class InvokeInst : public TerminatorInst {
2330 AttrListPtr AttributeList;
2331 InvokeInst(const InvokeInst &BI);
2332 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2333 Value* const *Args, unsigned NumArgs);
2335 template<typename InputIterator>
2336 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2337 InputIterator ArgBegin, InputIterator ArgEnd,
2338 const Twine &NameStr,
2339 // This argument ensures that we have an iterator we can
2340 // do arithmetic on in constant time
2341 std::random_access_iterator_tag) {
2342 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2344 // This requires that the iterator points to contiguous memory.
2345 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2349 /// Construct an InvokeInst given a range of arguments.
2350 /// InputIterator must be a random-access iterator pointing to
2351 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2352 /// made for random-accessness but not for contiguous storage as
2353 /// that would incur runtime overhead.
2355 /// @brief Construct an InvokeInst from a range of arguments
2356 template<typename InputIterator>
2357 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2358 InputIterator ArgBegin, InputIterator ArgEnd,
2360 const Twine &NameStr, Instruction *InsertBefore);
2362 /// Construct an InvokeInst given a range of arguments.
2363 /// InputIterator must be a random-access iterator pointing to
2364 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2365 /// made for random-accessness but not for contiguous storage as
2366 /// that would incur runtime overhead.
2368 /// @brief Construct an InvokeInst from a range of arguments
2369 template<typename InputIterator>
2370 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2371 InputIterator ArgBegin, InputIterator ArgEnd,
2373 const Twine &NameStr, BasicBlock *InsertAtEnd);
2375 virtual InvokeInst *clone_impl() const;
2377 template<typename InputIterator>
2378 static InvokeInst *Create(Value *Func,
2379 BasicBlock *IfNormal, BasicBlock *IfException,
2380 InputIterator ArgBegin, InputIterator ArgEnd,
2381 const Twine &NameStr = "",
2382 Instruction *InsertBefore = 0) {
2383 unsigned Values(ArgEnd - ArgBegin + 3);
2384 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2385 Values, NameStr, InsertBefore);
2387 template<typename InputIterator>
2388 static InvokeInst *Create(Value *Func,
2389 BasicBlock *IfNormal, BasicBlock *IfException,
2390 InputIterator ArgBegin, InputIterator ArgEnd,
2391 const Twine &NameStr,
2392 BasicBlock *InsertAtEnd) {
2393 unsigned Values(ArgEnd - ArgBegin + 3);
2394 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2395 Values, NameStr, InsertAtEnd);
2398 /// Provide fast operand accessors
2399 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2401 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2403 CallingConv::ID getCallingConv() const {
2404 return static_cast<CallingConv::ID>(SubclassData);
2406 void setCallingConv(CallingConv::ID CC) {
2407 SubclassData = static_cast<unsigned>(CC);
2410 /// getAttributes - Return the parameter attributes for this invoke.
2412 const AttrListPtr &getAttributes() const { return AttributeList; }
2414 /// setAttributes - Set the parameter attributes for this invoke.
2416 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2418 /// addAttribute - adds the attribute to the list of attributes.
2419 void addAttribute(unsigned i, Attributes attr);
2421 /// removeAttribute - removes the attribute from the list of attributes.
2422 void removeAttribute(unsigned i, Attributes attr);
2424 /// @brief Determine whether the call or the callee has the given attribute.
2425 bool paramHasAttr(unsigned i, Attributes attr) const;
2427 /// @brief Extract the alignment for a call or parameter (0=unknown).
2428 unsigned getParamAlignment(unsigned i) const {
2429 return AttributeList.getParamAlignment(i);
2432 /// @brief Determine if the call does not access memory.
2433 bool doesNotAccessMemory() const {
2434 return paramHasAttr(~0, Attribute::ReadNone);
2436 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2437 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2438 else removeAttribute(~0, Attribute::ReadNone);
2441 /// @brief Determine if the call does not access or only reads memory.
2442 bool onlyReadsMemory() const {
2443 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2445 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2446 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2447 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2450 /// @brief Determine if the call cannot return.
2451 bool doesNotReturn() const {
2452 return paramHasAttr(~0, Attribute::NoReturn);
2454 void setDoesNotReturn(bool DoesNotReturn = true) {
2455 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2456 else removeAttribute(~0, Attribute::NoReturn);
2459 /// @brief Determine if the call cannot unwind.
2460 bool doesNotThrow() const {
2461 return paramHasAttr(~0, Attribute::NoUnwind);
2463 void setDoesNotThrow(bool DoesNotThrow = true) {
2464 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2465 else removeAttribute(~0, Attribute::NoUnwind);
2468 /// @brief Determine if the call returns a structure through first
2469 /// pointer argument.
2470 bool hasStructRetAttr() const {
2471 // Be friendly and also check the callee.
2472 return paramHasAttr(1, Attribute::StructRet);
2475 /// @brief Determine if any call argument is an aggregate passed by value.
2476 bool hasByValArgument() const {
2477 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2480 /// getCalledFunction - Return the function called, or null if this is an
2481 /// indirect function invocation.
2483 Function *getCalledFunction() const {
2484 return dyn_cast<Function>(getOperand(0));
2487 /// getCalledValue - Get a pointer to the function that is invoked by this
2489 const Value *getCalledValue() const { return getOperand(0); }
2490 Value *getCalledValue() { return getOperand(0); }
2492 // get*Dest - Return the destination basic blocks...
2493 BasicBlock *getNormalDest() const {
2494 return cast<BasicBlock>(getOperand(1));
2496 BasicBlock *getUnwindDest() const {
2497 return cast<BasicBlock>(getOperand(2));
2499 void setNormalDest(BasicBlock *B) {
2500 setOperand(1, (Value*)B);
2503 void setUnwindDest(BasicBlock *B) {
2504 setOperand(2, (Value*)B);
2507 BasicBlock *getSuccessor(unsigned i) const {
2508 assert(i < 2 && "Successor # out of range for invoke!");
2509 return i == 0 ? getNormalDest() : getUnwindDest();
2512 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2513 assert(idx < 2 && "Successor # out of range for invoke!");
2514 setOperand(idx+1, (Value*)NewSucc);
2517 unsigned getNumSuccessors() const { return 2; }
2519 // Methods for support type inquiry through isa, cast, and dyn_cast:
2520 static inline bool classof(const InvokeInst *) { return true; }
2521 static inline bool classof(const Instruction *I) {
2522 return (I->getOpcode() == Instruction::Invoke);
2524 static inline bool classof(const Value *V) {
2525 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2528 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2529 virtual unsigned getNumSuccessorsV() const;
2530 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2534 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2537 template<typename InputIterator>
2538 InvokeInst::InvokeInst(Value *Func,
2539 BasicBlock *IfNormal, BasicBlock *IfException,
2540 InputIterator ArgBegin, InputIterator ArgEnd,
2542 const Twine &NameStr, Instruction *InsertBefore)
2543 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2544 ->getElementType())->getReturnType(),
2545 Instruction::Invoke,
2546 OperandTraits<InvokeInst>::op_end(this) - Values,
2547 Values, InsertBefore) {
2548 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2549 typename std::iterator_traits<InputIterator>::iterator_category());
2551 template<typename InputIterator>
2552 InvokeInst::InvokeInst(Value *Func,
2553 BasicBlock *IfNormal, BasicBlock *IfException,
2554 InputIterator ArgBegin, InputIterator ArgEnd,
2556 const Twine &NameStr, BasicBlock *InsertAtEnd)
2557 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2558 ->getElementType())->getReturnType(),
2559 Instruction::Invoke,
2560 OperandTraits<InvokeInst>::op_end(this) - Values,
2561 Values, InsertAtEnd) {
2562 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2563 typename std::iterator_traits<InputIterator>::iterator_category());
2566 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2568 //===----------------------------------------------------------------------===//
2570 //===----------------------------------------------------------------------===//
2572 //===---------------------------------------------------------------------------
2573 /// UnwindInst - Immediately exit the current function, unwinding the stack
2574 /// until an invoke instruction is found.
2576 class UnwindInst : public TerminatorInst {
2577 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2579 virtual UnwindInst *clone_impl() const;
2581 // allocate space for exactly zero operands
2582 void *operator new(size_t s) {
2583 return User::operator new(s, 0);
2585 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2586 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2588 unsigned getNumSuccessors() const { return 0; }
2590 // Methods for support type inquiry through isa, cast, and dyn_cast:
2591 static inline bool classof(const UnwindInst *) { return true; }
2592 static inline bool classof(const Instruction *I) {
2593 return I->getOpcode() == Instruction::Unwind;
2595 static inline bool classof(const Value *V) {
2596 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2599 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2600 virtual unsigned getNumSuccessorsV() const;
2601 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2604 //===----------------------------------------------------------------------===//
2605 // UnreachableInst Class
2606 //===----------------------------------------------------------------------===//
2608 //===---------------------------------------------------------------------------
2609 /// UnreachableInst - This function has undefined behavior. In particular, the
2610 /// presence of this instruction indicates some higher level knowledge that the
2611 /// end of the block cannot be reached.
2613 class UnreachableInst : public TerminatorInst {
2614 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2616 virtual UnreachableInst *clone_impl() const;
2619 // allocate space for exactly zero operands
2620 void *operator new(size_t s) {
2621 return User::operator new(s, 0);
2623 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2624 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2626 unsigned getNumSuccessors() const { return 0; }
2628 // Methods for support type inquiry through isa, cast, and dyn_cast:
2629 static inline bool classof(const UnreachableInst *) { return true; }
2630 static inline bool classof(const Instruction *I) {
2631 return I->getOpcode() == Instruction::Unreachable;
2633 static inline bool classof(const Value *V) {
2634 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2637 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2638 virtual unsigned getNumSuccessorsV() const;
2639 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2642 //===----------------------------------------------------------------------===//
2644 //===----------------------------------------------------------------------===//
2646 /// @brief This class represents a truncation of integer types.
2647 class TruncInst : public CastInst {
2649 /// @brief Clone an identical TruncInst
2650 virtual TruncInst *clone_impl() const;
2653 /// @brief Constructor with insert-before-instruction semantics
2655 Value *S, ///< The value to be truncated
2656 const Type *Ty, ///< The (smaller) type to truncate to
2657 const Twine &NameStr = "", ///< A name for the new instruction
2658 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2661 /// @brief Constructor with insert-at-end-of-block semantics
2663 Value *S, ///< The value to be truncated
2664 const Type *Ty, ///< The (smaller) type to truncate to
2665 const Twine &NameStr, ///< A name for the new instruction
2666 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2669 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2670 static inline bool classof(const TruncInst *) { return true; }
2671 static inline bool classof(const Instruction *I) {
2672 return I->getOpcode() == Trunc;
2674 static inline bool classof(const Value *V) {
2675 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2679 //===----------------------------------------------------------------------===//
2681 //===----------------------------------------------------------------------===//
2683 /// @brief This class represents zero extension of integer types.
2684 class ZExtInst : public CastInst {
2686 /// @brief Clone an identical ZExtInst
2687 virtual ZExtInst *clone_impl() const;
2690 /// @brief Constructor with insert-before-instruction semantics
2692 Value *S, ///< The value to be zero extended
2693 const Type *Ty, ///< The type to zero extend to
2694 const Twine &NameStr = "", ///< A name for the new instruction
2695 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2698 /// @brief Constructor with insert-at-end semantics.
2700 Value *S, ///< The value to be zero extended
2701 const Type *Ty, ///< The type to zero extend to
2702 const Twine &NameStr, ///< A name for the new instruction
2703 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2706 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2707 static inline bool classof(const ZExtInst *) { return true; }
2708 static inline bool classof(const Instruction *I) {
2709 return I->getOpcode() == ZExt;
2711 static inline bool classof(const Value *V) {
2712 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2716 //===----------------------------------------------------------------------===//
2718 //===----------------------------------------------------------------------===//
2720 /// @brief This class represents a sign extension of integer types.
2721 class SExtInst : public CastInst {
2723 /// @brief Clone an identical SExtInst
2724 virtual SExtInst *clone_impl() const;
2727 /// @brief Constructor with insert-before-instruction semantics
2729 Value *S, ///< The value to be sign extended
2730 const Type *Ty, ///< The type to sign extend to
2731 const Twine &NameStr = "", ///< A name for the new instruction
2732 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2735 /// @brief Constructor with insert-at-end-of-block semantics
2737 Value *S, ///< The value to be sign extended
2738 const Type *Ty, ///< The type to sign extend to
2739 const Twine &NameStr, ///< A name for the new instruction
2740 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2743 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2744 static inline bool classof(const SExtInst *) { return true; }
2745 static inline bool classof(const Instruction *I) {
2746 return I->getOpcode() == SExt;
2748 static inline bool classof(const Value *V) {
2749 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2753 //===----------------------------------------------------------------------===//
2754 // FPTruncInst Class
2755 //===----------------------------------------------------------------------===//
2757 /// @brief This class represents a truncation of floating point types.
2758 class FPTruncInst : public CastInst {
2760 /// @brief Clone an identical FPTruncInst
2761 virtual FPTruncInst *clone_impl() const;
2764 /// @brief Constructor with insert-before-instruction semantics
2766 Value *S, ///< The value to be truncated
2767 const Type *Ty, ///< The type to truncate to
2768 const Twine &NameStr = "", ///< A name for the new instruction
2769 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2772 /// @brief Constructor with insert-before-instruction semantics
2774 Value *S, ///< The value to be truncated
2775 const Type *Ty, ///< The type to truncate to
2776 const Twine &NameStr, ///< A name for the new instruction
2777 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2780 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2781 static inline bool classof(const FPTruncInst *) { return true; }
2782 static inline bool classof(const Instruction *I) {
2783 return I->getOpcode() == FPTrunc;
2785 static inline bool classof(const Value *V) {
2786 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2790 //===----------------------------------------------------------------------===//
2792 //===----------------------------------------------------------------------===//
2794 /// @brief This class represents an extension of floating point types.
2795 class FPExtInst : public CastInst {
2797 /// @brief Clone an identical FPExtInst
2798 virtual FPExtInst *clone_impl() const;
2801 /// @brief Constructor with insert-before-instruction semantics
2803 Value *S, ///< The value to be extended
2804 const Type *Ty, ///< The type to extend to
2805 const Twine &NameStr = "", ///< A name for the new instruction
2806 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2809 /// @brief Constructor with insert-at-end-of-block semantics
2811 Value *S, ///< The value to be extended
2812 const Type *Ty, ///< The type to extend to
2813 const Twine &NameStr, ///< A name for the new instruction
2814 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2817 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2818 static inline bool classof(const FPExtInst *) { return true; }
2819 static inline bool classof(const Instruction *I) {
2820 return I->getOpcode() == FPExt;
2822 static inline bool classof(const Value *V) {
2823 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2827 //===----------------------------------------------------------------------===//
2829 //===----------------------------------------------------------------------===//
2831 /// @brief This class represents a cast unsigned integer to floating point.
2832 class UIToFPInst : public CastInst {
2834 /// @brief Clone an identical UIToFPInst
2835 virtual UIToFPInst *clone_impl() const;
2838 /// @brief Constructor with insert-before-instruction semantics
2840 Value *S, ///< The value to be converted
2841 const Type *Ty, ///< The type to convert to
2842 const Twine &NameStr = "", ///< A name for the new instruction
2843 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2846 /// @brief Constructor with insert-at-end-of-block semantics
2848 Value *S, ///< The value to be converted
2849 const Type *Ty, ///< The type to convert to
2850 const Twine &NameStr, ///< A name for the new instruction
2851 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2854 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2855 static inline bool classof(const UIToFPInst *) { return true; }
2856 static inline bool classof(const Instruction *I) {
2857 return I->getOpcode() == UIToFP;
2859 static inline bool classof(const Value *V) {
2860 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2864 //===----------------------------------------------------------------------===//
2866 //===----------------------------------------------------------------------===//
2868 /// @brief This class represents a cast from signed integer to floating point.
2869 class SIToFPInst : public CastInst {
2871 /// @brief Clone an identical SIToFPInst
2872 virtual SIToFPInst *clone_impl() const;
2875 /// @brief Constructor with insert-before-instruction semantics
2877 Value *S, ///< The value to be converted
2878 const Type *Ty, ///< The type to convert to
2879 const Twine &NameStr = "", ///< A name for the new instruction
2880 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2883 /// @brief Constructor with insert-at-end-of-block semantics
2885 Value *S, ///< The value to be converted
2886 const Type *Ty, ///< The type to convert to
2887 const Twine &NameStr, ///< A name for the new instruction
2888 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2891 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2892 static inline bool classof(const SIToFPInst *) { return true; }
2893 static inline bool classof(const Instruction *I) {
2894 return I->getOpcode() == SIToFP;
2896 static inline bool classof(const Value *V) {
2897 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2901 //===----------------------------------------------------------------------===//
2903 //===----------------------------------------------------------------------===//
2905 /// @brief This class represents a cast from floating point to unsigned integer
2906 class FPToUIInst : public CastInst {
2908 /// @brief Clone an identical FPToUIInst
2909 virtual FPToUIInst *clone_impl() const;
2912 /// @brief Constructor with insert-before-instruction semantics
2914 Value *S, ///< The value to be converted
2915 const Type *Ty, ///< The type to convert to
2916 const Twine &NameStr = "", ///< A name for the new instruction
2917 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2920 /// @brief Constructor with insert-at-end-of-block semantics
2922 Value *S, ///< The value to be converted
2923 const Type *Ty, ///< The type to convert to
2924 const Twine &NameStr, ///< A name for the new instruction
2925 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2928 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2929 static inline bool classof(const FPToUIInst *) { return true; }
2930 static inline bool classof(const Instruction *I) {
2931 return I->getOpcode() == FPToUI;
2933 static inline bool classof(const Value *V) {
2934 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2938 //===----------------------------------------------------------------------===//
2940 //===----------------------------------------------------------------------===//
2942 /// @brief This class represents a cast from floating point to signed integer.
2943 class FPToSIInst : public CastInst {
2945 /// @brief Clone an identical FPToSIInst
2946 virtual FPToSIInst *clone_impl() const;
2949 /// @brief Constructor with insert-before-instruction semantics
2951 Value *S, ///< The value to be converted
2952 const Type *Ty, ///< The type to convert to
2953 const Twine &NameStr = "", ///< A name for the new instruction
2954 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2957 /// @brief Constructor with insert-at-end-of-block semantics
2959 Value *S, ///< The value to be converted
2960 const Type *Ty, ///< The type to convert to
2961 const Twine &NameStr, ///< A name for the new instruction
2962 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2965 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2966 static inline bool classof(const FPToSIInst *) { return true; }
2967 static inline bool classof(const Instruction *I) {
2968 return I->getOpcode() == FPToSI;
2970 static inline bool classof(const Value *V) {
2971 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2975 //===----------------------------------------------------------------------===//
2976 // IntToPtrInst Class
2977 //===----------------------------------------------------------------------===//
2979 /// @brief This class represents a cast from an integer to a pointer.
2980 class IntToPtrInst : public CastInst {
2982 /// @brief Constructor with insert-before-instruction semantics
2984 Value *S, ///< The value to be converted
2985 const Type *Ty, ///< The type to convert to
2986 const Twine &NameStr = "", ///< A name for the new instruction
2987 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2990 /// @brief Constructor with insert-at-end-of-block semantics
2992 Value *S, ///< The value to be converted
2993 const Type *Ty, ///< The type to convert to
2994 const Twine &NameStr, ///< A name for the new instruction
2995 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2998 /// @brief Clone an identical IntToPtrInst
2999 virtual IntToPtrInst *clone_impl() const;
3001 // Methods for support type inquiry through isa, cast, and dyn_cast:
3002 static inline bool classof(const IntToPtrInst *) { return true; }
3003 static inline bool classof(const Instruction *I) {
3004 return I->getOpcode() == IntToPtr;
3006 static inline bool classof(const Value *V) {
3007 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3011 //===----------------------------------------------------------------------===//
3012 // PtrToIntInst Class
3013 //===----------------------------------------------------------------------===//
3015 /// @brief This class represents a cast from a pointer to an integer
3016 class PtrToIntInst : public CastInst {
3018 /// @brief Clone an identical PtrToIntInst
3019 virtual PtrToIntInst *clone_impl() const;
3022 /// @brief Constructor with insert-before-instruction semantics
3024 Value *S, ///< The value to be converted
3025 const Type *Ty, ///< The type to convert to
3026 const Twine &NameStr = "", ///< A name for the new instruction
3027 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3030 /// @brief Constructor with insert-at-end-of-block semantics
3032 Value *S, ///< The value to be converted
3033 const Type *Ty, ///< The type to convert to
3034 const Twine &NameStr, ///< A name for the new instruction
3035 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3038 // Methods for support type inquiry through isa, cast, and dyn_cast:
3039 static inline bool classof(const PtrToIntInst *) { return true; }
3040 static inline bool classof(const Instruction *I) {
3041 return I->getOpcode() == PtrToInt;
3043 static inline bool classof(const Value *V) {
3044 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3048 //===----------------------------------------------------------------------===//
3049 // BitCastInst Class
3050 //===----------------------------------------------------------------------===//
3052 /// @brief This class represents a no-op cast from one type to another.
3053 class BitCastInst : public CastInst {
3055 /// @brief Clone an identical BitCastInst
3056 virtual BitCastInst *clone_impl() const;
3059 /// @brief Constructor with insert-before-instruction semantics
3061 Value *S, ///< The value to be casted
3062 const Type *Ty, ///< The type to casted to
3063 const Twine &NameStr = "", ///< A name for the new instruction
3064 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3067 /// @brief Constructor with insert-at-end-of-block semantics
3069 Value *S, ///< The value to be casted
3070 const Type *Ty, ///< The type to casted to
3071 const Twine &NameStr, ///< A name for the new instruction
3072 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3075 // Methods for support type inquiry through isa, cast, and dyn_cast:
3076 static inline bool classof(const BitCastInst *) { return true; }
3077 static inline bool classof(const Instruction *I) {
3078 return I->getOpcode() == BitCast;
3080 static inline bool classof(const Value *V) {
3081 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3085 } // End llvm namespace