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 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
43 const Twine &Name = "", Instruction *InsertBefore = 0);
44 AllocaInst(const Type *Ty, Value *ArraySize,
45 const Twine &Name, BasicBlock *InsertAtEnd);
47 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
48 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
50 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
51 const Twine &Name = "", Instruction *InsertBefore = 0);
52 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
53 const Twine &Name, BasicBlock *InsertAtEnd);
55 // Out of line virtual method, so the vtable, etc. has a home.
56 virtual ~AllocaInst();
58 /// isArrayAllocation - Return true if there is an allocation size parameter
59 /// to the allocation instruction that is not 1.
61 bool isArrayAllocation() const;
63 /// getArraySize - Get the number of elements allocated. For a simple
64 /// allocation of a single element, this will return a constant 1 value.
66 const Value *getArraySize() const { return getOperand(0); }
67 Value *getArraySize() { return getOperand(0); }
69 /// getType - Overload to return most specific pointer type
71 const PointerType *getType() const {
72 return reinterpret_cast<const PointerType*>(Instruction::getType());
75 /// getAllocatedType - Return the type that is being allocated by the
78 const Type *getAllocatedType() const;
80 /// getAlignment - Return the alignment of the memory that is being allocated
81 /// by the instruction.
83 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
84 void setAlignment(unsigned Align);
86 /// isStaticAlloca - Return true if this alloca is in the entry block of the
87 /// function and is a constant size. If so, the code generator will fold it
88 /// into the prolog/epilog code, so it is basically free.
89 bool isStaticAlloca() const;
91 virtual AllocaInst *clone() 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 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
115 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
116 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
117 Instruction *InsertBefore = 0);
118 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
119 unsigned Align, Instruction *InsertBefore = 0);
120 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
121 BasicBlock *InsertAtEnd);
122 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
123 unsigned Align, BasicBlock *InsertAtEnd);
125 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
126 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
127 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
128 bool isVolatile = false, Instruction *InsertBefore = 0);
129 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
130 BasicBlock *InsertAtEnd);
132 /// isVolatile - Return true if this is a load from a volatile memory
135 bool isVolatile() const { return SubclassData & 1; }
137 /// setVolatile - Specify whether this is a volatile load or not.
139 void setVolatile(bool V) {
140 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
143 virtual LoadInst *clone() const;
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 // allocate space for exactly two operands
184 void *operator new(size_t s) {
185 return User::operator new(s, 2);
187 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
188 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
189 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
190 Instruction *InsertBefore = 0);
191 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
192 unsigned Align, Instruction *InsertBefore = 0);
193 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
194 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
195 unsigned Align, BasicBlock *InsertAtEnd);
198 /// isVolatile - Return true if this is a load from a volatile memory
201 bool isVolatile() const { return SubclassData & 1; }
203 /// setVolatile - Specify whether this is a volatile load or not.
205 void setVolatile(bool V) {
206 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
209 /// Transparently provide more efficient getOperand methods.
210 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
212 /// getAlignment - Return the alignment of the access that is being performed
214 unsigned getAlignment() const {
215 return (1 << (SubclassData>>1)) >> 1;
218 void setAlignment(unsigned Align);
220 virtual StoreInst *clone() const;
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 template<typename InputIterator>
332 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
333 InputIterator IdxEnd,
334 const Twine &NameStr = "",
335 Instruction *InsertBefore = 0) {
336 typename std::iterator_traits<InputIterator>::difference_type Values =
337 1 + std::distance(IdxBegin, IdxEnd);
339 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
341 template<typename InputIterator>
342 static GetElementPtrInst *Create(Value *Ptr,
343 InputIterator IdxBegin, InputIterator IdxEnd,
344 const Twine &NameStr,
345 BasicBlock *InsertAtEnd) {
346 typename std::iterator_traits<InputIterator>::difference_type Values =
347 1 + std::distance(IdxBegin, IdxEnd);
349 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
352 /// Constructors - These two creators are convenience methods because one
353 /// index getelementptr instructions are so common.
354 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
355 const Twine &NameStr = "",
356 Instruction *InsertBefore = 0) {
357 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
359 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
360 const Twine &NameStr,
361 BasicBlock *InsertAtEnd) {
362 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
365 /// Create an "inbounds" getelementptr. See the documentation for the
366 /// "inbounds" flag in LangRef.html for details.
367 template<typename InputIterator>
368 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
369 InputIterator IdxEnd,
370 const Twine &NameStr = "",
371 Instruction *InsertBefore = 0) {
372 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
373 NameStr, InsertBefore);
374 GEP->setIsInBounds(true);
377 template<typename InputIterator>
378 static GetElementPtrInst *CreateInBounds(Value *Ptr,
379 InputIterator IdxBegin,
380 InputIterator IdxEnd,
381 const Twine &NameStr,
382 BasicBlock *InsertAtEnd) {
383 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
384 NameStr, InsertAtEnd);
385 GEP->setIsInBounds(true);
388 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
389 const Twine &NameStr = "",
390 Instruction *InsertBefore = 0) {
391 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
392 GEP->setIsInBounds(true);
395 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
396 const Twine &NameStr,
397 BasicBlock *InsertAtEnd) {
398 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
399 GEP->setIsInBounds(true);
403 virtual GetElementPtrInst *clone() const;
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 Constructor with insert-before-instruction semantics.
554 Instruction *InsertBefore, ///< Where to insert
555 Predicate pred, ///< The predicate to use for the comparison
556 Value *LHS, ///< The left-hand-side of the expression
557 Value *RHS, ///< The right-hand-side of the expression
558 const Twine &NameStr = "" ///< Name of the instruction
559 ) : CmpInst(makeCmpResultType(LHS->getType()),
560 Instruction::ICmp, pred, LHS, RHS, NameStr,
562 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
563 pred <= CmpInst::LAST_ICMP_PREDICATE &&
564 "Invalid ICmp predicate value");
565 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
566 "Both operands to ICmp instruction are not of the same type!");
567 // Check that the operands are the right type
568 assert((getOperand(0)->getType()->isIntOrIntVector() ||
569 isa<PointerType>(getOperand(0)->getType())) &&
570 "Invalid operand types for ICmp instruction");
573 /// @brief Constructor with insert-at-end semantics.
575 BasicBlock &InsertAtEnd, ///< Block to insert into.
576 Predicate pred, ///< The predicate to use for the comparison
577 Value *LHS, ///< The left-hand-side of the expression
578 Value *RHS, ///< The right-hand-side of the expression
579 const Twine &NameStr = "" ///< Name of the instruction
580 ) : CmpInst(makeCmpResultType(LHS->getType()),
581 Instruction::ICmp, pred, LHS, RHS, NameStr,
583 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
584 pred <= CmpInst::LAST_ICMP_PREDICATE &&
585 "Invalid ICmp predicate value");
586 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
587 "Both operands to ICmp instruction are not of the same type!");
588 // Check that the operands are the right type
589 assert((getOperand(0)->getType()->isIntOrIntVector() ||
590 isa<PointerType>(getOperand(0)->getType())) &&
591 "Invalid operand types for ICmp instruction");
594 /// @brief Constructor with no-insertion semantics
596 Predicate pred, ///< The predicate to use for the comparison
597 Value *LHS, ///< The left-hand-side of the expression
598 Value *RHS, ///< The right-hand-side of the expression
599 const Twine &NameStr = "" ///< Name of the instruction
600 ) : CmpInst(makeCmpResultType(LHS->getType()),
601 Instruction::ICmp, pred, LHS, RHS, NameStr) {
602 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
603 pred <= CmpInst::LAST_ICMP_PREDICATE &&
604 "Invalid ICmp predicate value");
605 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
606 "Both operands to ICmp instruction are not of the same type!");
607 // Check that the operands are the right type
608 assert((getOperand(0)->getType()->isIntOrIntVector() ||
609 isa<PointerType>(getOperand(0)->getType())) &&
610 "Invalid operand types for ICmp instruction");
613 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
614 /// @returns the predicate that would be the result if the operand were
615 /// regarded as signed.
616 /// @brief Return the signed version of the predicate
617 Predicate getSignedPredicate() const {
618 return getSignedPredicate(getPredicate());
621 /// This is a static version that you can use without an instruction.
622 /// @brief Return the signed version of the predicate.
623 static Predicate getSignedPredicate(Predicate pred);
625 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
626 /// @returns the predicate that would be the result if the operand were
627 /// regarded as unsigned.
628 /// @brief Return the unsigned version of the predicate
629 Predicate getUnsignedPredicate() const {
630 return getUnsignedPredicate(getPredicate());
633 /// This is a static version that you can use without an instruction.
634 /// @brief Return the unsigned version of the predicate.
635 static Predicate getUnsignedPredicate(Predicate pred);
637 /// isEquality - Return true if this predicate is either EQ or NE. This also
638 /// tests for commutativity.
639 static bool isEquality(Predicate P) {
640 return P == ICMP_EQ || P == ICMP_NE;
643 /// isEquality - Return true if this predicate is either EQ or NE. This also
644 /// tests for commutativity.
645 bool isEquality() const {
646 return isEquality(getPredicate());
649 /// @returns true if the predicate of this ICmpInst is commutative
650 /// @brief Determine if this relation is commutative.
651 bool isCommutative() const { return isEquality(); }
653 /// isRelational - Return true if the predicate is relational (not EQ or NE).
655 bool isRelational() const {
656 return !isEquality();
659 /// isRelational - Return true if the predicate is relational (not EQ or NE).
661 static bool isRelational(Predicate P) {
662 return !isEquality(P);
665 /// Initialize a set of values that all satisfy the predicate with C.
666 /// @brief Make a ConstantRange for a relation with a constant value.
667 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
669 /// Exchange the two operands to this instruction in such a way that it does
670 /// not modify the semantics of the instruction. The predicate value may be
671 /// changed to retain the same result if the predicate is order dependent
673 /// @brief Swap operands and adjust predicate.
674 void swapOperands() {
675 SubclassData = getSwappedPredicate();
676 Op<0>().swap(Op<1>());
679 virtual ICmpInst *clone() const;
681 // Methods for support type inquiry through isa, cast, and dyn_cast:
682 static inline bool classof(const ICmpInst *) { return true; }
683 static inline bool classof(const Instruction *I) {
684 return I->getOpcode() == Instruction::ICmp;
686 static inline bool classof(const Value *V) {
687 return isa<Instruction>(V) && classof(cast<Instruction>(V));
692 //===----------------------------------------------------------------------===//
694 //===----------------------------------------------------------------------===//
696 /// This instruction compares its operands according to the predicate given
697 /// to the constructor. It only operates on floating point values or packed
698 /// vectors of floating point values. The operands must be identical types.
699 /// @brief Represents a floating point comparison operator.
700 class FCmpInst: public CmpInst {
702 /// @brief Constructor with insert-before-instruction semantics.
704 Instruction *InsertBefore, ///< Where to insert
705 Predicate pred, ///< The predicate to use for the comparison
706 Value *LHS, ///< The left-hand-side of the expression
707 Value *RHS, ///< The right-hand-side of the expression
708 const Twine &NameStr = "" ///< Name of the instruction
709 ) : CmpInst(makeCmpResultType(LHS->getType()),
710 Instruction::FCmp, pred, LHS, RHS, NameStr,
712 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
713 "Invalid FCmp predicate value");
714 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
715 "Both operands to FCmp instruction are not of the same type!");
716 // Check that the operands are the right type
717 assert(getOperand(0)->getType()->isFPOrFPVector() &&
718 "Invalid operand types for FCmp instruction");
721 /// @brief Constructor with insert-at-end semantics.
723 BasicBlock &InsertAtEnd, ///< Block to insert into.
724 Predicate pred, ///< The predicate to use for the comparison
725 Value *LHS, ///< The left-hand-side of the expression
726 Value *RHS, ///< The right-hand-side of the expression
727 const Twine &NameStr = "" ///< Name of the instruction
728 ) : CmpInst(makeCmpResultType(LHS->getType()),
729 Instruction::FCmp, pred, LHS, RHS, NameStr,
731 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
732 "Invalid FCmp predicate value");
733 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
734 "Both operands to FCmp instruction are not of the same type!");
735 // Check that the operands are the right type
736 assert(getOperand(0)->getType()->isFPOrFPVector() &&
737 "Invalid operand types for FCmp instruction");
740 /// @brief Constructor with no-insertion semantics
742 Predicate pred, ///< The predicate to use for the comparison
743 Value *LHS, ///< The left-hand-side of the expression
744 Value *RHS, ///< The right-hand-side of the expression
745 const Twine &NameStr = "" ///< Name of the instruction
746 ) : CmpInst(makeCmpResultType(LHS->getType()),
747 Instruction::FCmp, pred, LHS, RHS, NameStr) {
748 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
749 "Invalid FCmp predicate value");
750 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
751 "Both operands to FCmp instruction are not of the same type!");
752 // Check that the operands are the right type
753 assert(getOperand(0)->getType()->isFPOrFPVector() &&
754 "Invalid operand types for FCmp instruction");
757 /// @returns true if the predicate of this instruction is EQ or NE.
758 /// @brief Determine if this is an equality predicate.
759 bool isEquality() const {
760 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
761 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
764 /// @returns true if the predicate of this instruction is commutative.
765 /// @brief Determine if this is a commutative predicate.
766 bool isCommutative() const {
767 return isEquality() ||
768 SubclassData == FCMP_FALSE ||
769 SubclassData == FCMP_TRUE ||
770 SubclassData == FCMP_ORD ||
771 SubclassData == FCMP_UNO;
774 /// @returns true if the predicate is relational (not EQ or NE).
775 /// @brief Determine if this a relational predicate.
776 bool isRelational() const { return !isEquality(); }
778 /// Exchange the two operands to this instruction in such a way that it does
779 /// not modify the semantics of the instruction. The predicate value may be
780 /// changed to retain the same result if the predicate is order dependent
782 /// @brief Swap operands and adjust predicate.
783 void swapOperands() {
784 SubclassData = getSwappedPredicate();
785 Op<0>().swap(Op<1>());
788 virtual FCmpInst *clone() const;
790 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
791 static inline bool classof(const FCmpInst *) { return true; }
792 static inline bool classof(const Instruction *I) {
793 return I->getOpcode() == Instruction::FCmp;
795 static inline bool classof(const Value *V) {
796 return isa<Instruction>(V) && classof(cast<Instruction>(V));
800 //===----------------------------------------------------------------------===//
802 //===----------------------------------------------------------------------===//
803 /// CallInst - This class represents a function call, abstracting a target
804 /// machine's calling convention. This class uses low bit of the SubClassData
805 /// field to indicate whether or not this is a tail call. The rest of the bits
806 /// hold the calling convention of the call.
809 class CallInst : public Instruction {
810 AttrListPtr AttributeList; ///< parameter attributes for call
811 CallInst(const CallInst &CI);
812 void init(Value *Func, Value* const *Params, unsigned NumParams);
813 void init(Value *Func, Value *Actual1, Value *Actual2);
814 void init(Value *Func, Value *Actual);
815 void init(Value *Func);
817 template<typename InputIterator>
818 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
819 const Twine &NameStr,
820 // This argument ensures that we have an iterator we can
821 // do arithmetic on in constant time
822 std::random_access_iterator_tag) {
823 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
825 // This requires that the iterator points to contiguous memory.
826 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
830 /// Construct a CallInst given a range of arguments. InputIterator
831 /// must be a random-access iterator pointing to contiguous storage
832 /// (e.g. a std::vector<>::iterator). Checks are made for
833 /// random-accessness but not for contiguous storage as that would
834 /// incur runtime overhead.
835 /// @brief Construct a CallInst from a range of arguments
836 template<typename InputIterator>
837 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
838 const Twine &NameStr, Instruction *InsertBefore);
840 /// Construct a CallInst given a range of arguments. InputIterator
841 /// must be a random-access iterator pointing to contiguous storage
842 /// (e.g. a std::vector<>::iterator). Checks are made for
843 /// random-accessness but not for contiguous storage as that would
844 /// incur runtime overhead.
845 /// @brief Construct a CallInst from a range of arguments
846 template<typename InputIterator>
847 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
848 const Twine &NameStr, BasicBlock *InsertAtEnd);
850 CallInst(Value *F, Value *Actual, const Twine &NameStr,
851 Instruction *InsertBefore);
852 CallInst(Value *F, Value *Actual, const Twine &NameStr,
853 BasicBlock *InsertAtEnd);
854 explicit CallInst(Value *F, const Twine &NameStr,
855 Instruction *InsertBefore);
856 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
858 template<typename InputIterator>
859 static CallInst *Create(Value *Func,
860 InputIterator ArgBegin, InputIterator ArgEnd,
861 const Twine &NameStr = "",
862 Instruction *InsertBefore = 0) {
863 return new((unsigned)(ArgEnd - ArgBegin + 1))
864 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
866 template<typename InputIterator>
867 static CallInst *Create(Value *Func,
868 InputIterator ArgBegin, InputIterator ArgEnd,
869 const Twine &NameStr, BasicBlock *InsertAtEnd) {
870 return new((unsigned)(ArgEnd - ArgBegin + 1))
871 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
873 static CallInst *Create(Value *F, Value *Actual,
874 const Twine &NameStr = "",
875 Instruction *InsertBefore = 0) {
876 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
878 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
879 BasicBlock *InsertAtEnd) {
880 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
882 static CallInst *Create(Value *F, const Twine &NameStr = "",
883 Instruction *InsertBefore = 0) {
884 return new(1) CallInst(F, NameStr, InsertBefore);
886 static CallInst *Create(Value *F, const Twine &NameStr,
887 BasicBlock *InsertAtEnd) {
888 return new(1) CallInst(F, NameStr, InsertAtEnd);
890 /// CreateMalloc - Generate the IR for a call to malloc:
891 /// 1. Compute the malloc call's argument as the specified type's size,
892 /// possibly multiplied by the array size if the array size is not
894 /// 2. Call malloc with that argument.
895 /// 3. Bitcast the result of the malloc call to the specified type.
896 static Instruction *CreateMalloc(Instruction *InsertBefore,
897 const Type *IntPtrTy, const Type *AllocTy,
898 Value *ArraySize = 0,
899 const Twine &Name = "");
900 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
901 const Type *IntPtrTy, const Type *AllocTy,
902 Value *ArraySize = 0, Function* MallocF = 0,
903 const Twine &Name = "");
904 /// CreateFree - Generate the IR for a call to the builtin free function.
905 static void CreateFree(Value* Source, Instruction *InsertBefore);
906 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
910 bool isTailCall() const { return SubclassData & 1; }
911 void setTailCall(bool isTC = true) {
912 SubclassData = (SubclassData & ~1) | unsigned(isTC);
915 virtual CallInst *clone() const;
917 /// Provide fast operand accessors
918 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
920 /// getCallingConv/setCallingConv - Get or set the calling convention of this
922 CallingConv::ID getCallingConv() const {
923 return static_cast<CallingConv::ID>(SubclassData >> 1);
925 void setCallingConv(CallingConv::ID CC) {
926 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
929 /// getAttributes - Return the parameter attributes for this call.
931 const AttrListPtr &getAttributes() const { return AttributeList; }
933 /// setAttributes - Set the parameter attributes for this call.
935 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
937 /// addAttribute - adds the attribute to the list of attributes.
938 void addAttribute(unsigned i, Attributes attr);
940 /// removeAttribute - removes the attribute from the list of attributes.
941 void removeAttribute(unsigned i, Attributes attr);
943 /// @brief Determine whether the call or the callee has the given attribute.
944 bool paramHasAttr(unsigned i, Attributes attr) const;
946 /// @brief Extract the alignment for a call or parameter (0=unknown).
947 unsigned getParamAlignment(unsigned i) const {
948 return AttributeList.getParamAlignment(i);
951 /// @brief Determine if the call does not access memory.
952 bool doesNotAccessMemory() const {
953 return paramHasAttr(~0, Attribute::ReadNone);
955 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
956 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
957 else removeAttribute(~0, Attribute::ReadNone);
960 /// @brief Determine if the call does not access or only reads memory.
961 bool onlyReadsMemory() const {
962 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
964 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
965 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
966 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
969 /// @brief Determine if the call cannot return.
970 bool doesNotReturn() const {
971 return paramHasAttr(~0, Attribute::NoReturn);
973 void setDoesNotReturn(bool DoesNotReturn = true) {
974 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
975 else removeAttribute(~0, Attribute::NoReturn);
978 /// @brief Determine if the call cannot unwind.
979 bool doesNotThrow() const {
980 return paramHasAttr(~0, Attribute::NoUnwind);
982 void setDoesNotThrow(bool DoesNotThrow = true) {
983 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
984 else removeAttribute(~0, Attribute::NoUnwind);
987 /// @brief Determine if the call returns a structure through first
988 /// pointer argument.
989 bool hasStructRetAttr() const {
990 // Be friendly and also check the callee.
991 return paramHasAttr(1, Attribute::StructRet);
994 /// @brief Determine if any call argument is an aggregate passed by value.
995 bool hasByValArgument() const {
996 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
999 /// getCalledFunction - Return the function called, or null if this is an
1000 /// indirect function invocation.
1002 Function *getCalledFunction() const {
1003 return dyn_cast<Function>(Op<0>());
1006 /// getCalledValue - Get a pointer to the function that is invoked by this
1008 const Value *getCalledValue() const { return Op<0>(); }
1009 Value *getCalledValue() { return Op<0>(); }
1011 /// setCalledFunction - Set the function called.
1012 void setCalledFunction(Value* Fn) {
1016 // Methods for support type inquiry through isa, cast, and dyn_cast:
1017 static inline bool classof(const CallInst *) { return true; }
1018 static inline bool classof(const Instruction *I) {
1019 return I->getOpcode() == Instruction::Call;
1021 static inline bool classof(const Value *V) {
1022 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1027 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1030 template<typename InputIterator>
1031 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1032 const Twine &NameStr, BasicBlock *InsertAtEnd)
1033 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1034 ->getElementType())->getReturnType(),
1036 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1037 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1038 init(Func, ArgBegin, ArgEnd, NameStr,
1039 typename std::iterator_traits<InputIterator>::iterator_category());
1042 template<typename InputIterator>
1043 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1044 const Twine &NameStr, Instruction *InsertBefore)
1045 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1046 ->getElementType())->getReturnType(),
1048 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1049 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1050 init(Func, ArgBegin, ArgEnd, NameStr,
1051 typename std::iterator_traits<InputIterator>::iterator_category());
1054 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1056 //===----------------------------------------------------------------------===//
1058 //===----------------------------------------------------------------------===//
1060 /// SelectInst - This class represents the LLVM 'select' instruction.
1062 class SelectInst : public Instruction {
1063 void init(Value *C, Value *S1, Value *S2) {
1064 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1070 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1071 Instruction *InsertBefore)
1072 : Instruction(S1->getType(), Instruction::Select,
1073 &Op<0>(), 3, InsertBefore) {
1077 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1078 BasicBlock *InsertAtEnd)
1079 : Instruction(S1->getType(), Instruction::Select,
1080 &Op<0>(), 3, InsertAtEnd) {
1085 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1086 const Twine &NameStr = "",
1087 Instruction *InsertBefore = 0) {
1088 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1090 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1091 const Twine &NameStr,
1092 BasicBlock *InsertAtEnd) {
1093 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1096 const Value *getCondition() const { return Op<0>(); }
1097 const Value *getTrueValue() const { return Op<1>(); }
1098 const Value *getFalseValue() const { return Op<2>(); }
1099 Value *getCondition() { return Op<0>(); }
1100 Value *getTrueValue() { return Op<1>(); }
1101 Value *getFalseValue() { return Op<2>(); }
1103 /// areInvalidOperands - Return a string if the specified operands are invalid
1104 /// for a select operation, otherwise return null.
1105 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1107 /// Transparently provide more efficient getOperand methods.
1108 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1110 OtherOps getOpcode() const {
1111 return static_cast<OtherOps>(Instruction::getOpcode());
1114 virtual SelectInst *clone() const;
1116 // Methods for support type inquiry through isa, cast, and dyn_cast:
1117 static inline bool classof(const SelectInst *) { return true; }
1118 static inline bool classof(const Instruction *I) {
1119 return I->getOpcode() == Instruction::Select;
1121 static inline bool classof(const Value *V) {
1122 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1127 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1130 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1132 //===----------------------------------------------------------------------===//
1134 //===----------------------------------------------------------------------===//
1136 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1137 /// an argument of the specified type given a va_list and increments that list
1139 class VAArgInst : public UnaryInstruction {
1141 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1142 Instruction *InsertBefore = 0)
1143 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1146 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1147 BasicBlock *InsertAtEnd)
1148 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1152 virtual VAArgInst *clone() const;
1154 // Methods for support type inquiry through isa, cast, and dyn_cast:
1155 static inline bool classof(const VAArgInst *) { return true; }
1156 static inline bool classof(const Instruction *I) {
1157 return I->getOpcode() == VAArg;
1159 static inline bool classof(const Value *V) {
1160 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1164 //===----------------------------------------------------------------------===//
1165 // ExtractElementInst Class
1166 //===----------------------------------------------------------------------===//
1168 /// ExtractElementInst - This instruction extracts a single (scalar)
1169 /// element from a VectorType value
1171 class ExtractElementInst : public Instruction {
1172 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1173 Instruction *InsertBefore = 0);
1174 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1175 BasicBlock *InsertAtEnd);
1177 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1178 const Twine &NameStr = "",
1179 Instruction *InsertBefore = 0) {
1180 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1182 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1183 const Twine &NameStr,
1184 BasicBlock *InsertAtEnd) {
1185 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1188 /// isValidOperands - Return true if an extractelement instruction can be
1189 /// formed with the specified operands.
1190 static bool isValidOperands(const Value *Vec, const Value *Idx);
1192 virtual ExtractElementInst *clone() const;
1194 Value *getVectorOperand() { return Op<0>(); }
1195 Value *getIndexOperand() { return Op<1>(); }
1196 const Value *getVectorOperand() const { return Op<0>(); }
1197 const Value *getIndexOperand() const { return Op<1>(); }
1199 const VectorType *getVectorOperandType() const {
1200 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1204 /// Transparently provide more efficient getOperand methods.
1205 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1207 // Methods for support type inquiry through isa, cast, and dyn_cast:
1208 static inline bool classof(const ExtractElementInst *) { return true; }
1209 static inline bool classof(const Instruction *I) {
1210 return I->getOpcode() == Instruction::ExtractElement;
1212 static inline bool classof(const Value *V) {
1213 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1218 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1221 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1223 //===----------------------------------------------------------------------===//
1224 // InsertElementInst Class
1225 //===----------------------------------------------------------------------===//
1227 /// InsertElementInst - This instruction inserts a single (scalar)
1228 /// element into a VectorType value
1230 class InsertElementInst : public Instruction {
1231 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1232 const Twine &NameStr = "",
1233 Instruction *InsertBefore = 0);
1234 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1235 const Twine &NameStr, BasicBlock *InsertAtEnd);
1237 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1238 const Twine &NameStr = "",
1239 Instruction *InsertBefore = 0) {
1240 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1242 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1243 const Twine &NameStr,
1244 BasicBlock *InsertAtEnd) {
1245 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1248 /// isValidOperands - Return true if an insertelement instruction can be
1249 /// formed with the specified operands.
1250 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1253 virtual InsertElementInst *clone() const;
1255 /// getType - Overload to return most specific vector type.
1257 const VectorType *getType() const {
1258 return reinterpret_cast<const VectorType*>(Instruction::getType());
1261 /// Transparently provide more efficient getOperand methods.
1262 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1264 // Methods for support type inquiry through isa, cast, and dyn_cast:
1265 static inline bool classof(const InsertElementInst *) { return true; }
1266 static inline bool classof(const Instruction *I) {
1267 return I->getOpcode() == Instruction::InsertElement;
1269 static inline bool classof(const Value *V) {
1270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1275 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1278 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1280 //===----------------------------------------------------------------------===//
1281 // ShuffleVectorInst Class
1282 //===----------------------------------------------------------------------===//
1284 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1287 class ShuffleVectorInst : public Instruction {
1289 // allocate space for exactly three operands
1290 void *operator new(size_t s) {
1291 return User::operator new(s, 3);
1293 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1294 const Twine &NameStr = "",
1295 Instruction *InsertBefor = 0);
1296 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1297 const Twine &NameStr, BasicBlock *InsertAtEnd);
1299 /// isValidOperands - Return true if a shufflevector instruction can be
1300 /// formed with the specified operands.
1301 static bool isValidOperands(const Value *V1, const Value *V2,
1304 virtual ShuffleVectorInst *clone() const;
1306 /// getType - Overload to return most specific vector type.
1308 const VectorType *getType() const {
1309 return reinterpret_cast<const VectorType*>(Instruction::getType());
1312 /// Transparently provide more efficient getOperand methods.
1313 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1315 /// getMaskValue - Return the index from the shuffle mask for the specified
1316 /// output result. This is either -1 if the element is undef or a number less
1317 /// than 2*numelements.
1318 int getMaskValue(unsigned i) const;
1320 // Methods for support type inquiry through isa, cast, and dyn_cast:
1321 static inline bool classof(const ShuffleVectorInst *) { return true; }
1322 static inline bool classof(const Instruction *I) {
1323 return I->getOpcode() == Instruction::ShuffleVector;
1325 static inline bool classof(const Value *V) {
1326 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1331 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1334 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1336 //===----------------------------------------------------------------------===//
1337 // ExtractValueInst Class
1338 //===----------------------------------------------------------------------===//
1340 /// ExtractValueInst - This instruction extracts a struct member or array
1341 /// element value from an aggregate value.
1343 class ExtractValueInst : public UnaryInstruction {
1344 SmallVector<unsigned, 4> Indices;
1346 ExtractValueInst(const ExtractValueInst &EVI);
1347 void init(const unsigned *Idx, unsigned NumIdx,
1348 const Twine &NameStr);
1349 void init(unsigned Idx, const Twine &NameStr);
1351 template<typename InputIterator>
1352 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1353 const Twine &NameStr,
1354 // This argument ensures that we have an iterator we can
1355 // do arithmetic on in constant time
1356 std::random_access_iterator_tag) {
1357 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1359 // There's no fundamental reason why we require at least one index
1360 // (other than weirdness with &*IdxBegin being invalid; see
1361 // getelementptr's init routine for example). But there's no
1362 // present need to support it.
1363 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1365 // This requires that the iterator points to contiguous memory.
1366 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1367 // we have to build an array here
1370 /// getIndexedType - Returns the type of the element that would be extracted
1371 /// with an extractvalue instruction with the specified parameters.
1373 /// Null is returned if the indices are invalid for the specified
1376 static const Type *getIndexedType(const Type *Agg,
1377 const unsigned *Idx, unsigned NumIdx);
1379 template<typename InputIterator>
1380 static const Type *getIndexedType(const Type *Ptr,
1381 InputIterator IdxBegin,
1382 InputIterator IdxEnd,
1383 // This argument ensures that we
1384 // have an iterator we can do
1385 // arithmetic on in constant time
1386 std::random_access_iterator_tag) {
1387 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1390 // This requires that the iterator points to contiguous memory.
1391 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1393 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1396 /// Constructors - Create a extractvalue instruction with a base aggregate
1397 /// value and a list of indices. The first ctor can optionally insert before
1398 /// an existing instruction, the second appends the new instruction to the
1399 /// specified BasicBlock.
1400 template<typename InputIterator>
1401 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1402 InputIterator IdxEnd,
1403 const Twine &NameStr,
1404 Instruction *InsertBefore);
1405 template<typename InputIterator>
1406 inline ExtractValueInst(Value *Agg,
1407 InputIterator IdxBegin, InputIterator IdxEnd,
1408 const Twine &NameStr, BasicBlock *InsertAtEnd);
1410 // allocate space for exactly one operand
1411 void *operator new(size_t s) {
1412 return User::operator new(s, 1);
1416 template<typename InputIterator>
1417 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1418 InputIterator IdxEnd,
1419 const Twine &NameStr = "",
1420 Instruction *InsertBefore = 0) {
1422 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1424 template<typename InputIterator>
1425 static ExtractValueInst *Create(Value *Agg,
1426 InputIterator IdxBegin, InputIterator IdxEnd,
1427 const Twine &NameStr,
1428 BasicBlock *InsertAtEnd) {
1429 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1432 /// Constructors - These two creators are convenience methods because one
1433 /// index extractvalue instructions are much more common than those with
1435 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1436 const Twine &NameStr = "",
1437 Instruction *InsertBefore = 0) {
1438 unsigned Idxs[1] = { Idx };
1439 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1441 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1442 const Twine &NameStr,
1443 BasicBlock *InsertAtEnd) {
1444 unsigned Idxs[1] = { Idx };
1445 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1448 virtual ExtractValueInst *clone() const;
1450 /// getIndexedType - Returns the type of the element that would be extracted
1451 /// with an extractvalue instruction with the specified parameters.
1453 /// Null is returned if the indices are invalid for the specified
1456 template<typename InputIterator>
1457 static const Type *getIndexedType(const Type *Ptr,
1458 InputIterator IdxBegin,
1459 InputIterator IdxEnd) {
1460 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1461 typename std::iterator_traits<InputIterator>::
1462 iterator_category());
1464 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1466 typedef const unsigned* idx_iterator;
1467 inline idx_iterator idx_begin() const { return Indices.begin(); }
1468 inline idx_iterator idx_end() const { return Indices.end(); }
1470 Value *getAggregateOperand() {
1471 return getOperand(0);
1473 const Value *getAggregateOperand() const {
1474 return getOperand(0);
1476 static unsigned getAggregateOperandIndex() {
1477 return 0U; // get index for modifying correct operand
1480 unsigned getNumIndices() const { // Note: always non-negative
1481 return (unsigned)Indices.size();
1484 bool hasIndices() const {
1488 // Methods for support type inquiry through isa, cast, and dyn_cast:
1489 static inline bool classof(const ExtractValueInst *) { return true; }
1490 static inline bool classof(const Instruction *I) {
1491 return I->getOpcode() == Instruction::ExtractValue;
1493 static inline bool classof(const Value *V) {
1494 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1498 template<typename InputIterator>
1499 ExtractValueInst::ExtractValueInst(Value *Agg,
1500 InputIterator IdxBegin,
1501 InputIterator IdxEnd,
1502 const Twine &NameStr,
1503 Instruction *InsertBefore)
1504 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1506 ExtractValue, Agg, InsertBefore) {
1507 init(IdxBegin, IdxEnd, NameStr,
1508 typename std::iterator_traits<InputIterator>::iterator_category());
1510 template<typename InputIterator>
1511 ExtractValueInst::ExtractValueInst(Value *Agg,
1512 InputIterator IdxBegin,
1513 InputIterator IdxEnd,
1514 const Twine &NameStr,
1515 BasicBlock *InsertAtEnd)
1516 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1518 ExtractValue, Agg, InsertAtEnd) {
1519 init(IdxBegin, IdxEnd, NameStr,
1520 typename std::iterator_traits<InputIterator>::iterator_category());
1524 //===----------------------------------------------------------------------===//
1525 // InsertValueInst Class
1526 //===----------------------------------------------------------------------===//
1528 /// InsertValueInst - This instruction inserts a struct field of array element
1529 /// value into an aggregate value.
1531 class InsertValueInst : public Instruction {
1532 SmallVector<unsigned, 4> Indices;
1534 void *operator new(size_t, unsigned); // Do not implement
1535 InsertValueInst(const InsertValueInst &IVI);
1536 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1537 const Twine &NameStr);
1538 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1540 template<typename InputIterator>
1541 void init(Value *Agg, Value *Val,
1542 InputIterator IdxBegin, InputIterator IdxEnd,
1543 const Twine &NameStr,
1544 // This argument ensures that we have an iterator we can
1545 // do arithmetic on in constant time
1546 std::random_access_iterator_tag) {
1547 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1549 // There's no fundamental reason why we require at least one index
1550 // (other than weirdness with &*IdxBegin being invalid; see
1551 // getelementptr's init routine for example). But there's no
1552 // present need to support it.
1553 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1555 // This requires that the iterator points to contiguous memory.
1556 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1557 // we have to build an array here
1560 /// Constructors - Create a insertvalue instruction with a base aggregate
1561 /// value, a value to insert, and a list of indices. The first ctor can
1562 /// optionally insert before an existing instruction, the second appends
1563 /// the new instruction to the specified BasicBlock.
1564 template<typename InputIterator>
1565 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1566 InputIterator IdxEnd,
1567 const Twine &NameStr,
1568 Instruction *InsertBefore);
1569 template<typename InputIterator>
1570 inline InsertValueInst(Value *Agg, Value *Val,
1571 InputIterator IdxBegin, InputIterator IdxEnd,
1572 const Twine &NameStr, BasicBlock *InsertAtEnd);
1574 /// Constructors - These two constructors are convenience methods because one
1575 /// and two index insertvalue instructions are so common.
1576 InsertValueInst(Value *Agg, Value *Val,
1577 unsigned Idx, const Twine &NameStr = "",
1578 Instruction *InsertBefore = 0);
1579 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1580 const Twine &NameStr, BasicBlock *InsertAtEnd);
1582 // allocate space for exactly two operands
1583 void *operator new(size_t s) {
1584 return User::operator new(s, 2);
1587 template<typename InputIterator>
1588 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1589 InputIterator IdxEnd,
1590 const Twine &NameStr = "",
1591 Instruction *InsertBefore = 0) {
1592 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1593 NameStr, InsertBefore);
1595 template<typename InputIterator>
1596 static InsertValueInst *Create(Value *Agg, Value *Val,
1597 InputIterator IdxBegin, InputIterator IdxEnd,
1598 const Twine &NameStr,
1599 BasicBlock *InsertAtEnd) {
1600 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1601 NameStr, InsertAtEnd);
1604 /// Constructors - These two creators are convenience methods because one
1605 /// index insertvalue instructions are much more common than those with
1607 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1608 const Twine &NameStr = "",
1609 Instruction *InsertBefore = 0) {
1610 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1612 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1613 const Twine &NameStr,
1614 BasicBlock *InsertAtEnd) {
1615 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1618 virtual InsertValueInst *clone() const;
1620 /// Transparently provide more efficient getOperand methods.
1621 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1623 typedef const unsigned* idx_iterator;
1624 inline idx_iterator idx_begin() const { return Indices.begin(); }
1625 inline idx_iterator idx_end() const { return Indices.end(); }
1627 Value *getAggregateOperand() {
1628 return getOperand(0);
1630 const Value *getAggregateOperand() const {
1631 return getOperand(0);
1633 static unsigned getAggregateOperandIndex() {
1634 return 0U; // get index for modifying correct operand
1637 Value *getInsertedValueOperand() {
1638 return getOperand(1);
1640 const Value *getInsertedValueOperand() const {
1641 return getOperand(1);
1643 static unsigned getInsertedValueOperandIndex() {
1644 return 1U; // get index for modifying correct operand
1647 unsigned getNumIndices() const { // Note: always non-negative
1648 return (unsigned)Indices.size();
1651 bool hasIndices() const {
1655 // Methods for support type inquiry through isa, cast, and dyn_cast:
1656 static inline bool classof(const InsertValueInst *) { return true; }
1657 static inline bool classof(const Instruction *I) {
1658 return I->getOpcode() == Instruction::InsertValue;
1660 static inline bool classof(const Value *V) {
1661 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1666 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1669 template<typename InputIterator>
1670 InsertValueInst::InsertValueInst(Value *Agg,
1672 InputIterator IdxBegin,
1673 InputIterator IdxEnd,
1674 const Twine &NameStr,
1675 Instruction *InsertBefore)
1676 : Instruction(Agg->getType(), InsertValue,
1677 OperandTraits<InsertValueInst>::op_begin(this),
1679 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1680 typename std::iterator_traits<InputIterator>::iterator_category());
1682 template<typename InputIterator>
1683 InsertValueInst::InsertValueInst(Value *Agg,
1685 InputIterator IdxBegin,
1686 InputIterator IdxEnd,
1687 const Twine &NameStr,
1688 BasicBlock *InsertAtEnd)
1689 : Instruction(Agg->getType(), InsertValue,
1690 OperandTraits<InsertValueInst>::op_begin(this),
1692 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1693 typename std::iterator_traits<InputIterator>::iterator_category());
1696 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1698 //===----------------------------------------------------------------------===//
1700 //===----------------------------------------------------------------------===//
1702 // PHINode - The PHINode class is used to represent the magical mystical PHI
1703 // node, that can not exist in nature, but can be synthesized in a computer
1704 // scientist's overactive imagination.
1706 class PHINode : public Instruction {
1707 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1708 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1709 /// the number actually in use.
1710 unsigned ReservedSpace;
1711 PHINode(const PHINode &PN);
1712 // allocate space for exactly zero operands
1713 void *operator new(size_t s) {
1714 return User::operator new(s, 0);
1716 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1717 Instruction *InsertBefore = 0)
1718 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1723 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1724 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1729 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1730 Instruction *InsertBefore = 0) {
1731 return new PHINode(Ty, NameStr, InsertBefore);
1733 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1734 BasicBlock *InsertAtEnd) {
1735 return new PHINode(Ty, NameStr, InsertAtEnd);
1739 /// reserveOperandSpace - This method can be used to avoid repeated
1740 /// reallocation of PHI operand lists by reserving space for the correct
1741 /// number of operands before adding them. Unlike normal vector reserves,
1742 /// this method can also be used to trim the operand space.
1743 void reserveOperandSpace(unsigned NumValues) {
1744 resizeOperands(NumValues*2);
1747 virtual PHINode *clone() const;
1749 /// Provide fast operand accessors
1750 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1752 /// getNumIncomingValues - Return the number of incoming edges
1754 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1756 /// getIncomingValue - Return incoming value number x
1758 Value *getIncomingValue(unsigned i) const {
1759 assert(i*2 < getNumOperands() && "Invalid value number!");
1760 return getOperand(i*2);
1762 void setIncomingValue(unsigned i, Value *V) {
1763 assert(i*2 < getNumOperands() && "Invalid value number!");
1766 static unsigned getOperandNumForIncomingValue(unsigned i) {
1769 static unsigned getIncomingValueNumForOperand(unsigned i) {
1770 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1774 /// getIncomingBlock - Return incoming basic block #i.
1776 BasicBlock *getIncomingBlock(unsigned i) const {
1777 return cast<BasicBlock>(getOperand(i*2+1));
1780 /// getIncomingBlock - Return incoming basic block corresponding
1781 /// to an operand of the PHI.
1783 BasicBlock *getIncomingBlock(const Use &U) const {
1784 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1785 return cast<BasicBlock>((&U + 1)->get());
1788 /// getIncomingBlock - Return incoming basic block corresponding
1789 /// to value use iterator.
1791 template <typename U>
1792 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1793 return getIncomingBlock(I.getUse());
1797 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1798 setOperand(i*2+1, (Value*)BB);
1800 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1803 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1804 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1808 /// addIncoming - Add an incoming value to the end of the PHI list
1810 void addIncoming(Value *V, BasicBlock *BB) {
1811 assert(V && "PHI node got a null value!");
1812 assert(BB && "PHI node got a null basic block!");
1813 assert(getType() == V->getType() &&
1814 "All operands to PHI node must be the same type as the PHI node!");
1815 unsigned OpNo = NumOperands;
1816 if (OpNo+2 > ReservedSpace)
1817 resizeOperands(0); // Get more space!
1818 // Initialize some new operands.
1819 NumOperands = OpNo+2;
1820 OperandList[OpNo] = V;
1821 OperandList[OpNo+1] = (Value*)BB;
1824 /// removeIncomingValue - Remove an incoming value. This is useful if a
1825 /// predecessor basic block is deleted. The value removed is returned.
1827 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1828 /// is true), the PHI node is destroyed and any uses of it are replaced with
1829 /// dummy values. The only time there should be zero incoming values to a PHI
1830 /// node is when the block is dead, so this strategy is sound.
1832 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1834 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1835 int Idx = getBasicBlockIndex(BB);
1836 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1837 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1840 /// getBasicBlockIndex - Return the first index of the specified basic
1841 /// block in the value list for this PHI. Returns -1 if no instance.
1843 int getBasicBlockIndex(const BasicBlock *BB) const {
1844 Use *OL = OperandList;
1845 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1846 if (OL[i+1].get() == (const Value*)BB) return i/2;
1850 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1851 return getIncomingValue(getBasicBlockIndex(BB));
1854 /// hasConstantValue - If the specified PHI node always merges together the
1855 /// same value, return the value, otherwise return null.
1857 /// If the PHI has undef operands, but all the rest of the operands are
1858 /// some unique value, return that value if it can be proved that the
1859 /// value dominates the PHI. If DT is null, use a conservative check,
1860 /// otherwise use DT to test for dominance.
1862 Value *hasConstantValue(DominatorTree *DT = 0) const;
1864 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1865 static inline bool classof(const PHINode *) { return true; }
1866 static inline bool classof(const Instruction *I) {
1867 return I->getOpcode() == Instruction::PHI;
1869 static inline bool classof(const Value *V) {
1870 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1873 void resizeOperands(unsigned NumOperands);
1877 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1880 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1883 //===----------------------------------------------------------------------===//
1885 //===----------------------------------------------------------------------===//
1887 //===---------------------------------------------------------------------------
1888 /// ReturnInst - Return a value (possibly void), from a function. Execution
1889 /// does not continue in this function any longer.
1891 class ReturnInst : public TerminatorInst {
1892 ReturnInst(const ReturnInst &RI);
1895 // ReturnInst constructors:
1896 // ReturnInst() - 'ret void' instruction
1897 // ReturnInst( null) - 'ret void' instruction
1898 // ReturnInst(Value* X) - 'ret X' instruction
1899 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1900 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1901 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1902 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1904 // NOTE: If the Value* passed is of type void then the constructor behaves as
1905 // if it was passed NULL.
1906 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1907 Instruction *InsertBefore = 0);
1908 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1909 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1911 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1912 Instruction *InsertBefore = 0) {
1913 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1915 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1916 BasicBlock *InsertAtEnd) {
1917 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1919 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1920 return new(0) ReturnInst(C, InsertAtEnd);
1922 virtual ~ReturnInst();
1924 virtual ReturnInst *clone() const;
1926 /// Provide fast operand accessors
1927 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1929 /// Convenience accessor
1930 Value *getReturnValue(unsigned n = 0) const {
1931 return n < getNumOperands()
1936 unsigned getNumSuccessors() const { return 0; }
1938 // Methods for support type inquiry through isa, cast, and dyn_cast:
1939 static inline bool classof(const ReturnInst *) { return true; }
1940 static inline bool classof(const Instruction *I) {
1941 return (I->getOpcode() == Instruction::Ret);
1943 static inline bool classof(const Value *V) {
1944 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1947 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1948 virtual unsigned getNumSuccessorsV() const;
1949 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1953 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
1956 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1958 //===----------------------------------------------------------------------===//
1960 //===----------------------------------------------------------------------===//
1962 //===---------------------------------------------------------------------------
1963 /// BranchInst - Conditional or Unconditional Branch instruction.
1965 class BranchInst : public TerminatorInst {
1966 /// Ops list - Branches are strange. The operands are ordered:
1967 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
1968 /// they don't have to check for cond/uncond branchness. These are mostly
1969 /// accessed relative from op_end().
1970 BranchInst(const BranchInst &BI);
1972 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1973 // BranchInst(BB *B) - 'br B'
1974 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1975 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1976 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1977 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1978 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1979 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1980 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1981 Instruction *InsertBefore = 0);
1982 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1983 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1984 BasicBlock *InsertAtEnd);
1986 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1987 return new(1, true) BranchInst(IfTrue, InsertBefore);
1989 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1990 Value *Cond, Instruction *InsertBefore = 0) {
1991 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
1993 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
1994 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
1996 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1997 Value *Cond, BasicBlock *InsertAtEnd) {
1998 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2003 /// Transparently provide more efficient getOperand methods.
2004 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2006 virtual BranchInst *clone() const;
2008 bool isUnconditional() const { return getNumOperands() == 1; }
2009 bool isConditional() const { return getNumOperands() == 3; }
2011 Value *getCondition() const {
2012 assert(isConditional() && "Cannot get condition of an uncond branch!");
2016 void setCondition(Value *V) {
2017 assert(isConditional() && "Cannot set condition of unconditional branch!");
2021 // setUnconditionalDest - Change the current branch to an unconditional branch
2022 // targeting the specified block.
2023 // FIXME: Eliminate this ugly method.
2024 void setUnconditionalDest(BasicBlock *Dest) {
2025 Op<-1>() = (Value*)Dest;
2026 if (isConditional()) { // Convert this to an uncond branch.
2030 OperandList = op_begin();
2034 unsigned getNumSuccessors() const { return 1+isConditional(); }
2036 BasicBlock *getSuccessor(unsigned i) const {
2037 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2038 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2041 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2042 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2043 *(&Op<-1>() - idx) = (Value*)NewSucc;
2046 // Methods for support type inquiry through isa, cast, and dyn_cast:
2047 static inline bool classof(const BranchInst *) { return true; }
2048 static inline bool classof(const Instruction *I) {
2049 return (I->getOpcode() == Instruction::Br);
2051 static inline bool classof(const Value *V) {
2052 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2055 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2056 virtual unsigned getNumSuccessorsV() const;
2057 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2061 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2063 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2065 //===----------------------------------------------------------------------===//
2067 //===----------------------------------------------------------------------===//
2069 //===---------------------------------------------------------------------------
2070 /// SwitchInst - Multiway switch
2072 class SwitchInst : public TerminatorInst {
2073 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2074 unsigned ReservedSpace;
2075 // Operand[0] = Value to switch on
2076 // Operand[1] = Default basic block destination
2077 // Operand[2n ] = Value to match
2078 // Operand[2n+1] = BasicBlock to go to on match
2079 SwitchInst(const SwitchInst &SI);
2080 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2081 void resizeOperands(unsigned No);
2082 // allocate space for exactly zero operands
2083 void *operator new(size_t s) {
2084 return User::operator new(s, 0);
2086 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2087 /// switch on and a default destination. The number of additional cases can
2088 /// be specified here to make memory allocation more efficient. This
2089 /// constructor can also autoinsert before another instruction.
2090 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2091 Instruction *InsertBefore);
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 also autoinserts at the end of the specified BasicBlock.
2097 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2098 BasicBlock *InsertAtEnd);
2100 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2101 unsigned NumCases, Instruction *InsertBefore = 0) {
2102 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2104 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2105 unsigned NumCases, BasicBlock *InsertAtEnd) {
2106 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2110 /// Provide fast operand accessors
2111 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2113 // Accessor Methods for Switch stmt
2114 Value *getCondition() const { return getOperand(0); }
2115 void setCondition(Value *V) { setOperand(0, V); }
2117 BasicBlock *getDefaultDest() const {
2118 return cast<BasicBlock>(getOperand(1));
2121 /// getNumCases - return the number of 'cases' in this switch instruction.
2122 /// Note that case #0 is always the default case.
2123 unsigned getNumCases() const {
2124 return getNumOperands()/2;
2127 /// getCaseValue - Return the specified case value. Note that case #0, the
2128 /// default destination, does not have a case value.
2129 ConstantInt *getCaseValue(unsigned i) {
2130 assert(i && i < getNumCases() && "Illegal case value to get!");
2131 return getSuccessorValue(i);
2134 /// getCaseValue - Return the specified case value. Note that case #0, the
2135 /// default destination, does not have a case value.
2136 const ConstantInt *getCaseValue(unsigned i) const {
2137 assert(i && i < getNumCases() && "Illegal case value to get!");
2138 return getSuccessorValue(i);
2141 /// findCaseValue - Search all of the case values for the specified constant.
2142 /// If it is explicitly handled, return the case number of it, otherwise
2143 /// return 0 to indicate that it is handled by the default handler.
2144 unsigned findCaseValue(const ConstantInt *C) const {
2145 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2146 if (getCaseValue(i) == C)
2151 /// findCaseDest - Finds the unique case value for a given successor. Returns
2152 /// null if the successor is not found, not unique, or is the default case.
2153 ConstantInt *findCaseDest(BasicBlock *BB) {
2154 if (BB == getDefaultDest()) return NULL;
2156 ConstantInt *CI = NULL;
2157 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2158 if (getSuccessor(i) == BB) {
2159 if (CI) return NULL; // Multiple cases lead to BB.
2160 else CI = getCaseValue(i);
2166 /// addCase - Add an entry to the switch instruction...
2168 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2170 /// removeCase - This method removes the specified successor from the switch
2171 /// instruction. Note that this cannot be used to remove the default
2172 /// destination (successor #0).
2174 void removeCase(unsigned idx);
2176 virtual SwitchInst *clone() const;
2178 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2179 BasicBlock *getSuccessor(unsigned idx) const {
2180 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2181 return cast<BasicBlock>(getOperand(idx*2+1));
2183 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2184 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2185 setOperand(idx*2+1, (Value*)NewSucc);
2188 // getSuccessorValue - Return the value associated with the specified
2190 ConstantInt *getSuccessorValue(unsigned idx) const {
2191 assert(idx < getNumSuccessors() && "Successor # out of range!");
2192 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2195 // Methods for support type inquiry through isa, cast, and dyn_cast:
2196 static inline bool classof(const SwitchInst *) { return true; }
2197 static inline bool classof(const Instruction *I) {
2198 return I->getOpcode() == Instruction::Switch;
2200 static inline bool classof(const Value *V) {
2201 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2204 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2205 virtual unsigned getNumSuccessorsV() const;
2206 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2210 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2213 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2216 //===----------------------------------------------------------------------===//
2218 //===----------------------------------------------------------------------===//
2220 //===---------------------------------------------------------------------------
2221 /// IndBrInst - Indirect Branch Instruction.
2223 class IndBrInst : public TerminatorInst {
2224 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2225 unsigned ReservedSpace;
2226 // Operand[0] = Value to switch on
2227 // Operand[1] = Default basic block destination
2228 // Operand[2n ] = Value to match
2229 // Operand[2n+1] = BasicBlock to go to on match
2230 IndBrInst(const IndBrInst &IBI);
2231 void init(Value *Address, unsigned NumDests);
2232 void resizeOperands(unsigned No);
2233 // allocate space for exactly zero operands
2234 void *operator new(size_t s) {
2235 return User::operator new(s, 0);
2237 /// IndBrInst ctor - Create a new indbr instruction, specifying an Address to
2238 /// jump to. The number of expected destinations can be specified here to
2239 /// make memory allocation more efficient. This constructor can also
2240 /// autoinsert before another instruction.
2241 IndBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2243 /// IndBrInst ctor - Create a new indbr instruction, specifying an Address to
2244 /// jump to. The number of expected destinations can be specified here to
2245 /// make memory allocation more efficient. This constructor also autoinserts
2246 /// at the end of the specified BasicBlock.
2247 IndBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2249 static IndBrInst *Create(Value *Address, unsigned NumDests,
2250 Instruction *InsertBefore = 0) {
2251 return new IndBrInst(Address, NumDests, InsertBefore);
2253 static IndBrInst *Create(Value *Address, unsigned NumDests,
2254 BasicBlock *InsertAtEnd) {
2255 return new IndBrInst(Address, NumDests, InsertAtEnd);
2259 /// Provide fast operand accessors.
2260 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2262 // Accessor Methods for IndBr instruction.
2263 Value *getAddress() { return getOperand(0); }
2264 const Value *getAddress() const { return getOperand(0); }
2265 void setAddress(Value *V) { setOperand(0, V); }
2268 /// getNumDestinations - return the number of possible destinations in this
2269 /// indbr instruction.
2270 unsigned getNumDestinations() const { return getNumOperands()-1; }
2272 /// getDestination - Return the specified destination.
2273 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2274 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2276 /// addDestination - Add a destination.
2278 void addDestination(BasicBlock *Dest);
2280 /// removeDestination - This method removes the specified successor from the
2281 /// indbr instruction.
2282 void removeDestination(unsigned i);
2284 virtual IndBrInst *clone() const;
2286 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2287 BasicBlock *getSuccessor(unsigned i) const {
2288 return cast<BasicBlock>(getOperand(i+1));
2290 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2291 setOperand(i+1, (Value*)NewSucc);
2294 // Methods for support type inquiry through isa, cast, and dyn_cast:
2295 static inline bool classof(const IndBrInst *) { return true; }
2296 static inline bool classof(const Instruction *I) {
2297 return I->getOpcode() == Instruction::IndBr;
2299 static inline bool classof(const Value *V) {
2300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2303 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2304 virtual unsigned getNumSuccessorsV() const;
2305 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2309 struct OperandTraits<IndBrInst> : public HungoffOperandTraits<1> {
2312 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndBrInst, Value)
2315 //===----------------------------------------------------------------------===//
2317 //===----------------------------------------------------------------------===//
2319 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2320 /// calling convention of the call.
2322 class InvokeInst : public TerminatorInst {
2323 AttrListPtr AttributeList;
2324 InvokeInst(const InvokeInst &BI);
2325 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2326 Value* const *Args, unsigned NumArgs);
2328 template<typename InputIterator>
2329 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2330 InputIterator ArgBegin, InputIterator ArgEnd,
2331 const Twine &NameStr,
2332 // This argument ensures that we have an iterator we can
2333 // do arithmetic on in constant time
2334 std::random_access_iterator_tag) {
2335 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2337 // This requires that the iterator points to contiguous memory.
2338 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2342 /// Construct an InvokeInst given a range of arguments.
2343 /// InputIterator must be a random-access iterator pointing to
2344 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2345 /// made for random-accessness but not for contiguous storage as
2346 /// that would incur runtime overhead.
2348 /// @brief Construct an InvokeInst from a range of arguments
2349 template<typename InputIterator>
2350 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2351 InputIterator ArgBegin, InputIterator ArgEnd,
2353 const Twine &NameStr, Instruction *InsertBefore);
2355 /// Construct an InvokeInst given a range of arguments.
2356 /// InputIterator must be a random-access iterator pointing to
2357 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2358 /// made for random-accessness but not for contiguous storage as
2359 /// that would incur runtime overhead.
2361 /// @brief Construct an InvokeInst from a range of arguments
2362 template<typename InputIterator>
2363 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2364 InputIterator ArgBegin, InputIterator ArgEnd,
2366 const Twine &NameStr, BasicBlock *InsertAtEnd);
2368 template<typename InputIterator>
2369 static InvokeInst *Create(Value *Func,
2370 BasicBlock *IfNormal, BasicBlock *IfException,
2371 InputIterator ArgBegin, InputIterator ArgEnd,
2372 const Twine &NameStr = "",
2373 Instruction *InsertBefore = 0) {
2374 unsigned Values(ArgEnd - ArgBegin + 3);
2375 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2376 Values, NameStr, InsertBefore);
2378 template<typename InputIterator>
2379 static InvokeInst *Create(Value *Func,
2380 BasicBlock *IfNormal, BasicBlock *IfException,
2381 InputIterator ArgBegin, InputIterator ArgEnd,
2382 const Twine &NameStr,
2383 BasicBlock *InsertAtEnd) {
2384 unsigned Values(ArgEnd - ArgBegin + 3);
2385 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2386 Values, NameStr, InsertAtEnd);
2389 virtual InvokeInst *clone() const;
2391 /// Provide fast operand accessors
2392 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2394 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2396 CallingConv::ID getCallingConv() const {
2397 return static_cast<CallingConv::ID>(SubclassData);
2399 void setCallingConv(CallingConv::ID CC) {
2400 SubclassData = static_cast<unsigned>(CC);
2403 /// getAttributes - Return the parameter attributes for this invoke.
2405 const AttrListPtr &getAttributes() const { return AttributeList; }
2407 /// setAttributes - Set the parameter attributes for this invoke.
2409 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2411 /// addAttribute - adds the attribute to the list of attributes.
2412 void addAttribute(unsigned i, Attributes attr);
2414 /// removeAttribute - removes the attribute from the list of attributes.
2415 void removeAttribute(unsigned i, Attributes attr);
2417 /// @brief Determine whether the call or the callee has the given attribute.
2418 bool paramHasAttr(unsigned i, Attributes attr) const;
2420 /// @brief Extract the alignment for a call or parameter (0=unknown).
2421 unsigned getParamAlignment(unsigned i) const {
2422 return AttributeList.getParamAlignment(i);
2425 /// @brief Determine if the call does not access memory.
2426 bool doesNotAccessMemory() const {
2427 return paramHasAttr(~0, Attribute::ReadNone);
2429 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2430 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2431 else removeAttribute(~0, Attribute::ReadNone);
2434 /// @brief Determine if the call does not access or only reads memory.
2435 bool onlyReadsMemory() const {
2436 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2438 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2439 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2440 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2443 /// @brief Determine if the call cannot return.
2444 bool doesNotReturn() const {
2445 return paramHasAttr(~0, Attribute::NoReturn);
2447 void setDoesNotReturn(bool DoesNotReturn = true) {
2448 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2449 else removeAttribute(~0, Attribute::NoReturn);
2452 /// @brief Determine if the call cannot unwind.
2453 bool doesNotThrow() const {
2454 return paramHasAttr(~0, Attribute::NoUnwind);
2456 void setDoesNotThrow(bool DoesNotThrow = true) {
2457 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2458 else removeAttribute(~0, Attribute::NoUnwind);
2461 /// @brief Determine if the call returns a structure through first
2462 /// pointer argument.
2463 bool hasStructRetAttr() const {
2464 // Be friendly and also check the callee.
2465 return paramHasAttr(1, Attribute::StructRet);
2468 /// @brief Determine if any call argument is an aggregate passed by value.
2469 bool hasByValArgument() const {
2470 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2473 /// getCalledFunction - Return the function called, or null if this is an
2474 /// indirect function invocation.
2476 Function *getCalledFunction() const {
2477 return dyn_cast<Function>(getOperand(0));
2480 /// getCalledValue - Get a pointer to the function that is invoked by this
2482 const Value *getCalledValue() const { return getOperand(0); }
2483 Value *getCalledValue() { return getOperand(0); }
2485 // get*Dest - Return the destination basic blocks...
2486 BasicBlock *getNormalDest() const {
2487 return cast<BasicBlock>(getOperand(1));
2489 BasicBlock *getUnwindDest() const {
2490 return cast<BasicBlock>(getOperand(2));
2492 void setNormalDest(BasicBlock *B) {
2493 setOperand(1, (Value*)B);
2496 void setUnwindDest(BasicBlock *B) {
2497 setOperand(2, (Value*)B);
2500 BasicBlock *getSuccessor(unsigned i) const {
2501 assert(i < 2 && "Successor # out of range for invoke!");
2502 return i == 0 ? getNormalDest() : getUnwindDest();
2505 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2506 assert(idx < 2 && "Successor # out of range for invoke!");
2507 setOperand(idx+1, (Value*)NewSucc);
2510 unsigned getNumSuccessors() const { return 2; }
2512 // Methods for support type inquiry through isa, cast, and dyn_cast:
2513 static inline bool classof(const InvokeInst *) { return true; }
2514 static inline bool classof(const Instruction *I) {
2515 return (I->getOpcode() == Instruction::Invoke);
2517 static inline bool classof(const Value *V) {
2518 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2521 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2522 virtual unsigned getNumSuccessorsV() const;
2523 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2527 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2530 template<typename InputIterator>
2531 InvokeInst::InvokeInst(Value *Func,
2532 BasicBlock *IfNormal, BasicBlock *IfException,
2533 InputIterator ArgBegin, InputIterator ArgEnd,
2535 const Twine &NameStr, Instruction *InsertBefore)
2536 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2537 ->getElementType())->getReturnType(),
2538 Instruction::Invoke,
2539 OperandTraits<InvokeInst>::op_end(this) - Values,
2540 Values, InsertBefore) {
2541 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2542 typename std::iterator_traits<InputIterator>::iterator_category());
2544 template<typename InputIterator>
2545 InvokeInst::InvokeInst(Value *Func,
2546 BasicBlock *IfNormal, BasicBlock *IfException,
2547 InputIterator ArgBegin, InputIterator ArgEnd,
2549 const Twine &NameStr, BasicBlock *InsertAtEnd)
2550 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2551 ->getElementType())->getReturnType(),
2552 Instruction::Invoke,
2553 OperandTraits<InvokeInst>::op_end(this) - Values,
2554 Values, InsertAtEnd) {
2555 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2556 typename std::iterator_traits<InputIterator>::iterator_category());
2559 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2561 //===----------------------------------------------------------------------===//
2563 //===----------------------------------------------------------------------===//
2565 //===---------------------------------------------------------------------------
2566 /// UnwindInst - Immediately exit the current function, unwinding the stack
2567 /// until an invoke instruction is found.
2569 class UnwindInst : public TerminatorInst {
2570 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2572 // allocate space for exactly zero operands
2573 void *operator new(size_t s) {
2574 return User::operator new(s, 0);
2576 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2577 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2579 virtual UnwindInst *clone() const;
2581 unsigned getNumSuccessors() const { return 0; }
2583 // Methods for support type inquiry through isa, cast, and dyn_cast:
2584 static inline bool classof(const UnwindInst *) { return true; }
2585 static inline bool classof(const Instruction *I) {
2586 return I->getOpcode() == Instruction::Unwind;
2588 static inline bool classof(const Value *V) {
2589 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2592 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2593 virtual unsigned getNumSuccessorsV() const;
2594 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2597 //===----------------------------------------------------------------------===//
2598 // UnreachableInst Class
2599 //===----------------------------------------------------------------------===//
2601 //===---------------------------------------------------------------------------
2602 /// UnreachableInst - This function has undefined behavior. In particular, the
2603 /// presence of this instruction indicates some higher level knowledge that the
2604 /// end of the block cannot be reached.
2606 class UnreachableInst : public TerminatorInst {
2607 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2609 // allocate space for exactly zero operands
2610 void *operator new(size_t s) {
2611 return User::operator new(s, 0);
2613 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2614 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2616 virtual UnreachableInst *clone() const;
2618 unsigned getNumSuccessors() const { return 0; }
2620 // Methods for support type inquiry through isa, cast, and dyn_cast:
2621 static inline bool classof(const UnreachableInst *) { return true; }
2622 static inline bool classof(const Instruction *I) {
2623 return I->getOpcode() == Instruction::Unreachable;
2625 static inline bool classof(const Value *V) {
2626 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2629 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2630 virtual unsigned getNumSuccessorsV() const;
2631 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2634 //===----------------------------------------------------------------------===//
2636 //===----------------------------------------------------------------------===//
2638 /// @brief This class represents a truncation of integer types.
2639 class TruncInst : public CastInst {
2641 /// @brief Constructor with insert-before-instruction semantics
2643 Value *S, ///< The value to be truncated
2644 const Type *Ty, ///< The (smaller) type to truncate to
2645 const Twine &NameStr = "", ///< A name for the new instruction
2646 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2649 /// @brief Constructor with insert-at-end-of-block semantics
2651 Value *S, ///< The value to be truncated
2652 const Type *Ty, ///< The (smaller) type to truncate to
2653 const Twine &NameStr, ///< A name for the new instruction
2654 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2657 /// @brief Clone an identical TruncInst
2658 virtual TruncInst *clone() const;
2660 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2661 static inline bool classof(const TruncInst *) { return true; }
2662 static inline bool classof(const Instruction *I) {
2663 return I->getOpcode() == Trunc;
2665 static inline bool classof(const Value *V) {
2666 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2670 //===----------------------------------------------------------------------===//
2672 //===----------------------------------------------------------------------===//
2674 /// @brief This class represents zero extension of integer types.
2675 class ZExtInst : public CastInst {
2677 /// @brief Constructor with insert-before-instruction semantics
2679 Value *S, ///< The value to be zero extended
2680 const Type *Ty, ///< The type to zero extend to
2681 const Twine &NameStr = "", ///< A name for the new instruction
2682 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2685 /// @brief Constructor with insert-at-end semantics.
2687 Value *S, ///< The value to be zero extended
2688 const Type *Ty, ///< The type to zero extend to
2689 const Twine &NameStr, ///< A name for the new instruction
2690 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2693 /// @brief Clone an identical ZExtInst
2694 virtual ZExtInst *clone() const;
2696 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2697 static inline bool classof(const ZExtInst *) { return true; }
2698 static inline bool classof(const Instruction *I) {
2699 return I->getOpcode() == ZExt;
2701 static inline bool classof(const Value *V) {
2702 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2706 //===----------------------------------------------------------------------===//
2708 //===----------------------------------------------------------------------===//
2710 /// @brief This class represents a sign extension of integer types.
2711 class SExtInst : public CastInst {
2713 /// @brief Constructor with insert-before-instruction semantics
2715 Value *S, ///< The value to be sign extended
2716 const Type *Ty, ///< The type to sign extend to
2717 const Twine &NameStr = "", ///< A name for the new instruction
2718 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2721 /// @brief Constructor with insert-at-end-of-block semantics
2723 Value *S, ///< The value to be sign extended
2724 const Type *Ty, ///< The type to sign extend to
2725 const Twine &NameStr, ///< A name for the new instruction
2726 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2729 /// @brief Clone an identical SExtInst
2730 virtual SExtInst *clone() const;
2732 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2733 static inline bool classof(const SExtInst *) { return true; }
2734 static inline bool classof(const Instruction *I) {
2735 return I->getOpcode() == SExt;
2737 static inline bool classof(const Value *V) {
2738 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2742 //===----------------------------------------------------------------------===//
2743 // FPTruncInst Class
2744 //===----------------------------------------------------------------------===//
2746 /// @brief This class represents a truncation of floating point types.
2747 class FPTruncInst : public CastInst {
2749 /// @brief Constructor with insert-before-instruction semantics
2751 Value *S, ///< The value to be truncated
2752 const Type *Ty, ///< The type to truncate to
2753 const Twine &NameStr = "", ///< A name for the new instruction
2754 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2757 /// @brief Constructor with insert-before-instruction semantics
2759 Value *S, ///< The value to be truncated
2760 const Type *Ty, ///< The type to truncate to
2761 const Twine &NameStr, ///< A name for the new instruction
2762 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2765 /// @brief Clone an identical FPTruncInst
2766 virtual FPTruncInst *clone() const;
2768 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2769 static inline bool classof(const FPTruncInst *) { return true; }
2770 static inline bool classof(const Instruction *I) {
2771 return I->getOpcode() == FPTrunc;
2773 static inline bool classof(const Value *V) {
2774 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2778 //===----------------------------------------------------------------------===//
2780 //===----------------------------------------------------------------------===//
2782 /// @brief This class represents an extension of floating point types.
2783 class FPExtInst : public CastInst {
2785 /// @brief Constructor with insert-before-instruction semantics
2787 Value *S, ///< The value to be extended
2788 const Type *Ty, ///< The type to extend to
2789 const Twine &NameStr = "", ///< A name for the new instruction
2790 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2793 /// @brief Constructor with insert-at-end-of-block semantics
2795 Value *S, ///< The value to be extended
2796 const Type *Ty, ///< The type to extend to
2797 const Twine &NameStr, ///< A name for the new instruction
2798 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2801 /// @brief Clone an identical FPExtInst
2802 virtual FPExtInst *clone() const;
2804 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2805 static inline bool classof(const FPExtInst *) { return true; }
2806 static inline bool classof(const Instruction *I) {
2807 return I->getOpcode() == FPExt;
2809 static inline bool classof(const Value *V) {
2810 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2814 //===----------------------------------------------------------------------===//
2816 //===----------------------------------------------------------------------===//
2818 /// @brief This class represents a cast unsigned integer to floating point.
2819 class UIToFPInst : public CastInst {
2821 /// @brief Constructor with insert-before-instruction semantics
2823 Value *S, ///< The value to be converted
2824 const Type *Ty, ///< The type to convert to
2825 const Twine &NameStr = "", ///< A name for the new instruction
2826 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2829 /// @brief Constructor with insert-at-end-of-block semantics
2831 Value *S, ///< The value to be converted
2832 const Type *Ty, ///< The type to convert to
2833 const Twine &NameStr, ///< A name for the new instruction
2834 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2837 /// @brief Clone an identical UIToFPInst
2838 virtual UIToFPInst *clone() const;
2840 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2841 static inline bool classof(const UIToFPInst *) { return true; }
2842 static inline bool classof(const Instruction *I) {
2843 return I->getOpcode() == UIToFP;
2845 static inline bool classof(const Value *V) {
2846 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2850 //===----------------------------------------------------------------------===//
2852 //===----------------------------------------------------------------------===//
2854 /// @brief This class represents a cast from signed integer to floating point.
2855 class SIToFPInst : public CastInst {
2857 /// @brief Constructor with insert-before-instruction semantics
2859 Value *S, ///< The value to be converted
2860 const Type *Ty, ///< The type to convert to
2861 const Twine &NameStr = "", ///< A name for the new instruction
2862 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2865 /// @brief Constructor with insert-at-end-of-block semantics
2867 Value *S, ///< The value to be converted
2868 const Type *Ty, ///< The type to convert to
2869 const Twine &NameStr, ///< A name for the new instruction
2870 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2873 /// @brief Clone an identical SIToFPInst
2874 virtual SIToFPInst *clone() const;
2876 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2877 static inline bool classof(const SIToFPInst *) { return true; }
2878 static inline bool classof(const Instruction *I) {
2879 return I->getOpcode() == SIToFP;
2881 static inline bool classof(const Value *V) {
2882 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2886 //===----------------------------------------------------------------------===//
2888 //===----------------------------------------------------------------------===//
2890 /// @brief This class represents a cast from floating point to unsigned integer
2891 class FPToUIInst : public CastInst {
2893 /// @brief Constructor with insert-before-instruction semantics
2895 Value *S, ///< The value to be converted
2896 const Type *Ty, ///< The type to convert to
2897 const Twine &NameStr = "", ///< A name for the new instruction
2898 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2901 /// @brief Constructor with insert-at-end-of-block semantics
2903 Value *S, ///< The value to be converted
2904 const Type *Ty, ///< The type to convert to
2905 const Twine &NameStr, ///< A name for the new instruction
2906 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2909 /// @brief Clone an identical FPToUIInst
2910 virtual FPToUIInst *clone() const;
2912 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2913 static inline bool classof(const FPToUIInst *) { return true; }
2914 static inline bool classof(const Instruction *I) {
2915 return I->getOpcode() == FPToUI;
2917 static inline bool classof(const Value *V) {
2918 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2922 //===----------------------------------------------------------------------===//
2924 //===----------------------------------------------------------------------===//
2926 /// @brief This class represents a cast from floating point to signed integer.
2927 class FPToSIInst : public CastInst {
2929 /// @brief Constructor with insert-before-instruction semantics
2931 Value *S, ///< The value to be converted
2932 const Type *Ty, ///< The type to convert to
2933 const Twine &NameStr = "", ///< A name for the new instruction
2934 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2937 /// @brief Constructor with insert-at-end-of-block semantics
2939 Value *S, ///< The value to be converted
2940 const Type *Ty, ///< The type to convert to
2941 const Twine &NameStr, ///< A name for the new instruction
2942 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2945 /// @brief Clone an identical FPToSIInst
2946 virtual FPToSIInst *clone() const;
2948 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2949 static inline bool classof(const FPToSIInst *) { return true; }
2950 static inline bool classof(const Instruction *I) {
2951 return I->getOpcode() == FPToSI;
2953 static inline bool classof(const Value *V) {
2954 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2958 //===----------------------------------------------------------------------===//
2959 // IntToPtrInst Class
2960 //===----------------------------------------------------------------------===//
2962 /// @brief This class represents a cast from an integer to a pointer.
2963 class IntToPtrInst : public CastInst {
2965 /// @brief Constructor with insert-before-instruction semantics
2967 Value *S, ///< The value to be converted
2968 const Type *Ty, ///< The type to convert to
2969 const Twine &NameStr = "", ///< A name for the new instruction
2970 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2973 /// @brief Constructor with insert-at-end-of-block semantics
2975 Value *S, ///< The value to be converted
2976 const Type *Ty, ///< The type to convert to
2977 const Twine &NameStr, ///< A name for the new instruction
2978 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2981 /// @brief Clone an identical IntToPtrInst
2982 virtual IntToPtrInst *clone() const;
2984 // Methods for support type inquiry through isa, cast, and dyn_cast:
2985 static inline bool classof(const IntToPtrInst *) { return true; }
2986 static inline bool classof(const Instruction *I) {
2987 return I->getOpcode() == IntToPtr;
2989 static inline bool classof(const Value *V) {
2990 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2994 //===----------------------------------------------------------------------===//
2995 // PtrToIntInst Class
2996 //===----------------------------------------------------------------------===//
2998 /// @brief This class represents a cast from a pointer to an integer
2999 class PtrToIntInst : public CastInst {
3001 /// @brief Constructor with insert-before-instruction semantics
3003 Value *S, ///< The value to be converted
3004 const Type *Ty, ///< The type to convert to
3005 const Twine &NameStr = "", ///< A name for the new instruction
3006 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3009 /// @brief Constructor with insert-at-end-of-block semantics
3011 Value *S, ///< The value to be converted
3012 const Type *Ty, ///< The type to convert to
3013 const Twine &NameStr, ///< A name for the new instruction
3014 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3017 /// @brief Clone an identical PtrToIntInst
3018 virtual PtrToIntInst *clone() const;
3020 // Methods for support type inquiry through isa, cast, and dyn_cast:
3021 static inline bool classof(const PtrToIntInst *) { return true; }
3022 static inline bool classof(const Instruction *I) {
3023 return I->getOpcode() == PtrToInt;
3025 static inline bool classof(const Value *V) {
3026 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3030 //===----------------------------------------------------------------------===//
3031 // BitCastInst Class
3032 //===----------------------------------------------------------------------===//
3034 /// @brief This class represents a no-op cast from one type to another.
3035 class BitCastInst : public CastInst {
3037 /// @brief Constructor with insert-before-instruction semantics
3039 Value *S, ///< The value to be casted
3040 const Type *Ty, ///< The type to casted to
3041 const Twine &NameStr = "", ///< A name for the new instruction
3042 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3045 /// @brief Constructor with insert-at-end-of-block semantics
3047 Value *S, ///< The value to be casted
3048 const Type *Ty, ///< The type to casted to
3049 const Twine &NameStr, ///< A name for the new instruction
3050 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3053 /// @brief Clone an identical BitCastInst
3054 virtual BitCastInst *clone() const;
3056 // Methods for support type inquiry through isa, cast, and dyn_cast:
3057 static inline bool classof(const BitCastInst *) { return true; }
3058 static inline bool classof(const Instruction *I) {
3059 return I->getOpcode() == BitCast;
3061 static inline bool classof(const Value *V) {
3062 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3066 } // End llvm namespace