1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/ADT/SmallVector.h"
36 //===----------------------------------------------------------------------===//
38 //===----------------------------------------------------------------------===//
40 /// AllocaInst - an instruction to allocate memory on the stack
42 class AllocaInst : public UnaryInstruction {
44 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
45 const Twine &Name = "", Instruction *InsertBefore = 0);
46 AllocaInst(const Type *Ty, Value *ArraySize,
47 const Twine &Name, BasicBlock *InsertAtEnd);
49 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
50 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
52 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
53 const Twine &Name = "", Instruction *InsertBefore = 0);
54 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
55 const Twine &Name, BasicBlock *InsertAtEnd);
57 // Out of line virtual method, so the vtable, etc. has a home.
58 virtual ~AllocaInst();
60 /// isArrayAllocation - Return true if there is an allocation size parameter
61 /// to the allocation instruction that is not 1.
63 bool isArrayAllocation() const;
65 /// getArraySize - Get the number of elements allocated. For a simple
66 /// allocation of a single element, this will return a constant 1 value.
68 const Value *getArraySize() const { return getOperand(0); }
69 Value *getArraySize() { return getOperand(0); }
71 /// getType - Overload to return most specific pointer type
73 const PointerType *getType() const {
74 return reinterpret_cast<const PointerType*>(Instruction::getType());
77 /// getAllocatedType - Return the type that is being allocated by the
80 const Type *getAllocatedType() const;
82 /// getAlignment - Return the alignment of the memory that is being allocated
83 /// by the instruction.
85 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
86 void setAlignment(unsigned Align);
88 /// isStaticAlloca - Return true if this alloca is in the entry block of the
89 /// function and is a constant size. If so, the code generator will fold it
90 /// into the prolog/epilog code, so it is basically free.
91 bool isStaticAlloca() const;
93 virtual AllocaInst *clone() const;
95 // Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool classof(const AllocaInst *) { return true; }
97 static inline bool classof(const Instruction *I) {
98 return (I->getOpcode() == Instruction::Alloca);
100 static inline bool classof(const Value *V) {
101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
106 //===----------------------------------------------------------------------===//
108 //===----------------------------------------------------------------------===//
110 /// LoadInst - an instruction for reading from memory. This uses the
111 /// SubclassData field in Value to store whether or not the load is volatile.
113 class LoadInst : public UnaryInstruction {
116 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
117 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
118 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
119 Instruction *InsertBefore = 0);
120 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
121 unsigned Align, Instruction *InsertBefore = 0);
122 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
123 BasicBlock *InsertAtEnd);
124 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
125 unsigned Align, BasicBlock *InsertAtEnd);
127 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
128 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
129 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
130 bool isVolatile = false, Instruction *InsertBefore = 0);
131 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
132 BasicBlock *InsertAtEnd);
134 /// isVolatile - Return true if this is a load from a volatile memory
137 bool isVolatile() const { return SubclassData & 1; }
139 /// setVolatile - Specify whether this is a volatile load or not.
141 void setVolatile(bool V) {
142 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
145 virtual LoadInst *clone() const;
147 /// getAlignment - Return the alignment of the access that is being performed
149 unsigned getAlignment() const {
150 return (1 << (SubclassData>>1)) >> 1;
153 void setAlignment(unsigned Align);
155 Value *getPointerOperand() { return getOperand(0); }
156 const Value *getPointerOperand() const { return getOperand(0); }
157 static unsigned getPointerOperandIndex() { return 0U; }
159 unsigned getPointerAddressSpace() const {
160 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
164 // Methods for support type inquiry through isa, cast, and dyn_cast:
165 static inline bool classof(const LoadInst *) { return true; }
166 static inline bool classof(const Instruction *I) {
167 return I->getOpcode() == Instruction::Load;
169 static inline bool classof(const Value *V) {
170 return isa<Instruction>(V) && classof(cast<Instruction>(V));
175 //===----------------------------------------------------------------------===//
177 //===----------------------------------------------------------------------===//
179 /// StoreInst - an instruction for storing to memory
181 class StoreInst : public Instruction {
182 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
185 // allocate space for exactly two operands
186 void *operator new(size_t s) {
187 return User::operator new(s, 2);
189 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
190 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
191 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
192 Instruction *InsertBefore = 0);
193 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
194 unsigned Align, Instruction *InsertBefore = 0);
195 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
196 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
197 unsigned Align, BasicBlock *InsertAtEnd);
200 /// isVolatile - Return true if this is a load from a volatile memory
203 bool isVolatile() const { return SubclassData & 1; }
205 /// setVolatile - Specify whether this is a volatile load or not.
207 void setVolatile(bool V) {
208 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
211 /// Transparently provide more efficient getOperand methods.
212 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
214 /// getAlignment - Return the alignment of the access that is being performed
216 unsigned getAlignment() const {
217 return (1 << (SubclassData>>1)) >> 1;
220 void setAlignment(unsigned Align);
222 virtual StoreInst *clone() const;
224 Value *getPointerOperand() { return getOperand(1); }
225 const Value *getPointerOperand() const { return getOperand(1); }
226 static unsigned getPointerOperandIndex() { return 1U; }
228 unsigned getPointerAddressSpace() const {
229 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
232 // Methods for support type inquiry through isa, cast, and dyn_cast:
233 static inline bool classof(const StoreInst *) { return true; }
234 static inline bool classof(const Instruction *I) {
235 return I->getOpcode() == Instruction::Store;
237 static inline bool classof(const Value *V) {
238 return isa<Instruction>(V) && classof(cast<Instruction>(V));
243 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
246 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
248 //===----------------------------------------------------------------------===//
249 // GetElementPtrInst Class
250 //===----------------------------------------------------------------------===//
252 // checkType - Simple wrapper function to give a better assertion failure
253 // message on bad indexes for a gep instruction.
255 static inline const Type *checkType(const Type *Ty) {
256 assert(Ty && "Invalid GetElementPtrInst indices for type!");
260 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
261 /// access elements of arrays and structs
263 class GetElementPtrInst : public Instruction {
264 GetElementPtrInst(const GetElementPtrInst &GEPI);
265 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
266 const Twine &NameStr);
267 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
269 template<typename InputIterator>
270 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
271 const Twine &NameStr,
272 // This argument ensures that we have an iterator we can
273 // do arithmetic on in constant time
274 std::random_access_iterator_tag) {
275 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
278 // This requires that the iterator points to contiguous memory.
279 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
280 // we have to build an array here
283 init(Ptr, 0, NumIdx, NameStr);
287 /// getIndexedType - Returns the type of the element that would be loaded with
288 /// a load instruction with the specified parameters.
290 /// Null is returned if the indices are invalid for the specified
293 template<typename InputIterator>
294 static const Type *getIndexedType(const Type *Ptr,
295 InputIterator IdxBegin,
296 InputIterator IdxEnd,
297 // This argument ensures that we
298 // have an iterator we can do
299 // arithmetic on in constant time
300 std::random_access_iterator_tag) {
301 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
304 // This requires that the iterator points to contiguous memory.
305 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
307 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
310 /// Constructors - Create a getelementptr instruction with a base pointer an
311 /// list of indices. The first ctor can optionally insert before an existing
312 /// instruction, the second appends the new instruction to the specified
314 template<typename InputIterator>
315 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
316 InputIterator IdxEnd,
318 const Twine &NameStr,
319 Instruction *InsertBefore);
320 template<typename InputIterator>
321 inline GetElementPtrInst(Value *Ptr,
322 InputIterator IdxBegin, InputIterator IdxEnd,
324 const Twine &NameStr, BasicBlock *InsertAtEnd);
326 /// Constructors - These two constructors are convenience methods because one
327 /// and two index getelementptr instructions are so common.
328 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
329 Instruction *InsertBefore = 0);
330 GetElementPtrInst(Value *Ptr, Value *Idx,
331 const Twine &NameStr, BasicBlock *InsertAtEnd);
333 template<typename InputIterator>
334 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
335 InputIterator IdxEnd,
336 const Twine &NameStr = "",
337 Instruction *InsertBefore = 0) {
338 typename std::iterator_traits<InputIterator>::difference_type Values =
339 1 + std::distance(IdxBegin, IdxEnd);
341 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
343 template<typename InputIterator>
344 static GetElementPtrInst *Create(Value *Ptr,
345 InputIterator IdxBegin, InputIterator IdxEnd,
346 const Twine &NameStr,
347 BasicBlock *InsertAtEnd) {
348 typename std::iterator_traits<InputIterator>::difference_type Values =
349 1 + std::distance(IdxBegin, IdxEnd);
351 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
354 /// Constructors - These two creators are convenience methods because one
355 /// index getelementptr instructions are so common.
356 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
357 const Twine &NameStr = "",
358 Instruction *InsertBefore = 0) {
359 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
361 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
362 const Twine &NameStr,
363 BasicBlock *InsertAtEnd) {
364 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
367 /// Create an "inbounds" getelementptr. See the documentation for the
368 /// "inbounds" flag in LangRef.html for details.
369 template<typename InputIterator>
370 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
371 InputIterator IdxEnd,
372 const Twine &NameStr = "",
373 Instruction *InsertBefore = 0) {
374 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
375 NameStr, InsertBefore);
376 GEP->setIsInBounds(true);
379 template<typename InputIterator>
380 static GetElementPtrInst *CreateInBounds(Value *Ptr,
381 InputIterator IdxBegin,
382 InputIterator IdxEnd,
383 const Twine &NameStr,
384 BasicBlock *InsertAtEnd) {
385 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
386 NameStr, InsertAtEnd);
387 GEP->setIsInBounds(true);
390 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
391 const Twine &NameStr = "",
392 Instruction *InsertBefore = 0) {
393 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
394 GEP->setIsInBounds(true);
397 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
398 const Twine &NameStr,
399 BasicBlock *InsertAtEnd) {
400 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
401 GEP->setIsInBounds(true);
405 virtual GetElementPtrInst *clone() const;
407 /// Transparently provide more efficient getOperand methods.
408 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
410 // getType - Overload to return most specific pointer type...
411 const PointerType *getType() const {
412 return reinterpret_cast<const PointerType*>(Instruction::getType());
415 /// getIndexedType - Returns the type of the element that would be loaded with
416 /// a load instruction with the specified parameters.
418 /// Null is returned if the indices are invalid for the specified
421 template<typename InputIterator>
422 static const Type *getIndexedType(const Type *Ptr,
423 InputIterator IdxBegin,
424 InputIterator IdxEnd) {
425 return getIndexedType(Ptr, IdxBegin, IdxEnd,
426 typename std::iterator_traits<InputIterator>::
427 iterator_category());
430 static const Type *getIndexedType(const Type *Ptr,
431 Value* const *Idx, unsigned NumIdx);
433 static const Type *getIndexedType(const Type *Ptr,
434 uint64_t const *Idx, unsigned NumIdx);
436 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
438 inline op_iterator idx_begin() { return op_begin()+1; }
439 inline const_op_iterator idx_begin() const { return op_begin()+1; }
440 inline op_iterator idx_end() { return op_end(); }
441 inline const_op_iterator idx_end() const { return op_end(); }
443 Value *getPointerOperand() {
444 return getOperand(0);
446 const Value *getPointerOperand() const {
447 return getOperand(0);
449 static unsigned getPointerOperandIndex() {
450 return 0U; // get index for modifying correct operand
453 unsigned getPointerAddressSpace() const {
454 return cast<PointerType>(getType())->getAddressSpace();
457 /// getPointerOperandType - Method to return the pointer operand as a
459 const PointerType *getPointerOperandType() const {
460 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
464 unsigned getNumIndices() const { // Note: always non-negative
465 return getNumOperands() - 1;
468 bool hasIndices() const {
469 return getNumOperands() > 1;
472 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
473 /// zeros. If so, the result pointer and the first operand have the same
474 /// value, just potentially different types.
475 bool hasAllZeroIndices() const;
477 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
478 /// constant integers. If so, the result pointer and the first operand have
479 /// a constant offset between them.
480 bool hasAllConstantIndices() const;
482 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
483 /// See LangRef.html for the meaning of inbounds on a getelementptr.
484 void setIsInBounds(bool b = true);
486 /// isInBounds - Determine whether the GEP has the inbounds flag.
487 bool isInBounds() const;
489 // Methods for support type inquiry through isa, cast, and dyn_cast:
490 static inline bool classof(const GetElementPtrInst *) { return true; }
491 static inline bool classof(const Instruction *I) {
492 return (I->getOpcode() == Instruction::GetElementPtr);
494 static inline bool classof(const Value *V) {
495 return isa<Instruction>(V) && classof(cast<Instruction>(V));
500 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
503 template<typename InputIterator>
504 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
505 InputIterator IdxBegin,
506 InputIterator IdxEnd,
508 const Twine &NameStr,
509 Instruction *InsertBefore)
510 : Instruction(PointerType::get(checkType(
511 getIndexedType(Ptr->getType(),
513 cast<PointerType>(Ptr->getType())
514 ->getAddressSpace()),
516 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
517 Values, InsertBefore) {
518 init(Ptr, IdxBegin, IdxEnd, NameStr,
519 typename std::iterator_traits<InputIterator>::iterator_category());
521 template<typename InputIterator>
522 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
523 InputIterator IdxBegin,
524 InputIterator IdxEnd,
526 const Twine &NameStr,
527 BasicBlock *InsertAtEnd)
528 : Instruction(PointerType::get(checkType(
529 getIndexedType(Ptr->getType(),
531 cast<PointerType>(Ptr->getType())
532 ->getAddressSpace()),
534 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
535 Values, InsertAtEnd) {
536 init(Ptr, IdxBegin, IdxEnd, NameStr,
537 typename std::iterator_traits<InputIterator>::iterator_category());
541 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
544 //===----------------------------------------------------------------------===//
546 //===----------------------------------------------------------------------===//
548 /// This instruction compares its operands according to the predicate given
549 /// to the constructor. It only operates on integers or pointers. The operands
550 /// must be identical types.
551 /// @brief Represent an integer comparison operator.
552 class ICmpInst: public CmpInst {
554 /// @brief Constructor with insert-before-instruction semantics.
556 Instruction *InsertBefore, ///< Where to insert
557 Predicate pred, ///< The predicate to use for the comparison
558 Value *LHS, ///< The left-hand-side of the expression
559 Value *RHS, ///< The right-hand-side of the expression
560 const Twine &NameStr = "" ///< Name of the instruction
561 ) : CmpInst(makeCmpResultType(LHS->getType()),
562 Instruction::ICmp, pred, LHS, RHS, NameStr,
564 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
565 pred <= CmpInst::LAST_ICMP_PREDICATE &&
566 "Invalid ICmp predicate value");
567 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
568 "Both operands to ICmp instruction are not of the same type!");
569 // Check that the operands are the right type
570 assert((getOperand(0)->getType()->isIntOrIntVector() ||
571 isa<PointerType>(getOperand(0)->getType())) &&
572 "Invalid operand types for ICmp instruction");
575 /// @brief Constructor with insert-at-end semantics.
577 BasicBlock &InsertAtEnd, ///< Block to insert into.
578 Predicate pred, ///< The predicate to use for the comparison
579 Value *LHS, ///< The left-hand-side of the expression
580 Value *RHS, ///< The right-hand-side of the expression
581 const Twine &NameStr = "" ///< Name of the instruction
582 ) : CmpInst(makeCmpResultType(LHS->getType()),
583 Instruction::ICmp, pred, LHS, RHS, NameStr,
585 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
586 pred <= CmpInst::LAST_ICMP_PREDICATE &&
587 "Invalid ICmp predicate value");
588 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
589 "Both operands to ICmp instruction are not of the same type!");
590 // Check that the operands are the right type
591 assert((getOperand(0)->getType()->isIntOrIntVector() ||
592 isa<PointerType>(getOperand(0)->getType())) &&
593 "Invalid operand types for ICmp instruction");
596 /// @brief Constructor with no-insertion semantics
598 Predicate pred, ///< The predicate to use for the comparison
599 Value *LHS, ///< The left-hand-side of the expression
600 Value *RHS, ///< The right-hand-side of the expression
601 const Twine &NameStr = "" ///< Name of the instruction
602 ) : CmpInst(makeCmpResultType(LHS->getType()),
603 Instruction::ICmp, pred, LHS, RHS, NameStr) {
604 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
605 pred <= CmpInst::LAST_ICMP_PREDICATE &&
606 "Invalid ICmp predicate value");
607 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
608 "Both operands to ICmp instruction are not of the same type!");
609 // Check that the operands are the right type
610 assert((getOperand(0)->getType()->isIntOrIntVector() ||
611 isa<PointerType>(getOperand(0)->getType())) &&
612 "Invalid operand types for ICmp instruction");
615 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
616 /// @returns the predicate that would be the result if the operand were
617 /// regarded as signed.
618 /// @brief Return the signed version of the predicate
619 Predicate getSignedPredicate() const {
620 return getSignedPredicate(getPredicate());
623 /// This is a static version that you can use without an instruction.
624 /// @brief Return the signed version of the predicate.
625 static Predicate getSignedPredicate(Predicate pred);
627 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
628 /// @returns the predicate that would be the result if the operand were
629 /// regarded as unsigned.
630 /// @brief Return the unsigned version of the predicate
631 Predicate getUnsignedPredicate() const {
632 return getUnsignedPredicate(getPredicate());
635 /// This is a static version that you can use without an instruction.
636 /// @brief Return the unsigned version of the predicate.
637 static Predicate getUnsignedPredicate(Predicate pred);
639 /// isEquality - Return true if this predicate is either EQ or NE. This also
640 /// tests for commutativity.
641 static bool isEquality(Predicate P) {
642 return P == ICMP_EQ || P == ICMP_NE;
645 /// isEquality - Return true if this predicate is either EQ or NE. This also
646 /// tests for commutativity.
647 bool isEquality() const {
648 return isEquality(getPredicate());
651 /// @returns true if the predicate of this ICmpInst is commutative
652 /// @brief Determine if this relation is commutative.
653 bool isCommutative() const { return isEquality(); }
655 /// isRelational - Return true if the predicate is relational (not EQ or NE).
657 bool isRelational() const {
658 return !isEquality();
661 /// isRelational - Return true if the predicate is relational (not EQ or NE).
663 static bool isRelational(Predicate P) {
664 return !isEquality(P);
667 /// Initialize a set of values that all satisfy the predicate with C.
668 /// @brief Make a ConstantRange for a relation with a constant value.
669 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
671 /// Exchange the two operands to this instruction in such a way that it does
672 /// not modify the semantics of the instruction. The predicate value may be
673 /// changed to retain the same result if the predicate is order dependent
675 /// @brief Swap operands and adjust predicate.
676 void swapOperands() {
677 SubclassData = getSwappedPredicate();
678 Op<0>().swap(Op<1>());
681 virtual ICmpInst *clone() const;
683 // Methods for support type inquiry through isa, cast, and dyn_cast:
684 static inline bool classof(const ICmpInst *) { return true; }
685 static inline bool classof(const Instruction *I) {
686 return I->getOpcode() == Instruction::ICmp;
688 static inline bool classof(const Value *V) {
689 return isa<Instruction>(V) && classof(cast<Instruction>(V));
694 //===----------------------------------------------------------------------===//
696 //===----------------------------------------------------------------------===//
698 /// This instruction compares its operands according to the predicate given
699 /// to the constructor. It only operates on floating point values or packed
700 /// vectors of floating point values. The operands must be identical types.
701 /// @brief Represents a floating point comparison operator.
702 class FCmpInst: public CmpInst {
704 /// @brief Constructor with insert-before-instruction semantics.
706 Instruction *InsertBefore, ///< Where to insert
707 Predicate pred, ///< The predicate to use for the comparison
708 Value *LHS, ///< The left-hand-side of the expression
709 Value *RHS, ///< The right-hand-side of the expression
710 const Twine &NameStr = "" ///< Name of the instruction
711 ) : CmpInst(makeCmpResultType(LHS->getType()),
712 Instruction::FCmp, pred, LHS, RHS, NameStr,
714 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
715 "Invalid FCmp predicate value");
716 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
717 "Both operands to FCmp instruction are not of the same type!");
718 // Check that the operands are the right type
719 assert(getOperand(0)->getType()->isFPOrFPVector() &&
720 "Invalid operand types for FCmp instruction");
723 /// @brief Constructor with insert-at-end semantics.
725 BasicBlock &InsertAtEnd, ///< Block to insert into.
726 Predicate pred, ///< The predicate to use for the comparison
727 Value *LHS, ///< The left-hand-side of the expression
728 Value *RHS, ///< The right-hand-side of the expression
729 const Twine &NameStr = "" ///< Name of the instruction
730 ) : CmpInst(makeCmpResultType(LHS->getType()),
731 Instruction::FCmp, pred, LHS, RHS, NameStr,
733 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
734 "Invalid FCmp predicate value");
735 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
736 "Both operands to FCmp instruction are not of the same type!");
737 // Check that the operands are the right type
738 assert(getOperand(0)->getType()->isFPOrFPVector() &&
739 "Invalid operand types for FCmp instruction");
742 /// @brief Constructor with no-insertion semantics
744 Predicate pred, ///< The predicate to use for the comparison
745 Value *LHS, ///< The left-hand-side of the expression
746 Value *RHS, ///< The right-hand-side of the expression
747 const Twine &NameStr = "" ///< Name of the instruction
748 ) : CmpInst(makeCmpResultType(LHS->getType()),
749 Instruction::FCmp, pred, LHS, RHS, NameStr) {
750 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
751 "Invalid FCmp predicate value");
752 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
753 "Both operands to FCmp instruction are not of the same type!");
754 // Check that the operands are the right type
755 assert(getOperand(0)->getType()->isFPOrFPVector() &&
756 "Invalid operand types for FCmp instruction");
759 /// @returns true if the predicate of this instruction is EQ or NE.
760 /// @brief Determine if this is an equality predicate.
761 bool isEquality() const {
762 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
763 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
766 /// @returns true if the predicate of this instruction is commutative.
767 /// @brief Determine if this is a commutative predicate.
768 bool isCommutative() const {
769 return isEquality() ||
770 SubclassData == FCMP_FALSE ||
771 SubclassData == FCMP_TRUE ||
772 SubclassData == FCMP_ORD ||
773 SubclassData == FCMP_UNO;
776 /// @returns true if the predicate is relational (not EQ or NE).
777 /// @brief Determine if this a relational predicate.
778 bool isRelational() const { return !isEquality(); }
780 /// Exchange the two operands to this instruction in such a way that it does
781 /// not modify the semantics of the instruction. The predicate value may be
782 /// changed to retain the same result if the predicate is order dependent
784 /// @brief Swap operands and adjust predicate.
785 void swapOperands() {
786 SubclassData = getSwappedPredicate();
787 Op<0>().swap(Op<1>());
790 virtual FCmpInst *clone() const;
792 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
793 static inline bool classof(const FCmpInst *) { return true; }
794 static inline bool classof(const Instruction *I) {
795 return I->getOpcode() == Instruction::FCmp;
797 static inline bool classof(const Value *V) {
798 return isa<Instruction>(V) && classof(cast<Instruction>(V));
802 //===----------------------------------------------------------------------===//
804 //===----------------------------------------------------------------------===//
805 /// CallInst - This class represents a function call, abstracting a target
806 /// machine's calling convention. This class uses low bit of the SubClassData
807 /// field to indicate whether or not this is a tail call. The rest of the bits
808 /// hold the calling convention of the call.
811 class CallInst : public Instruction {
812 AttrListPtr AttributeList; ///< parameter attributes for call
813 CallInst(const CallInst &CI);
814 void init(Value *Func, Value* const *Params, unsigned NumParams);
815 void init(Value *Func, Value *Actual1, Value *Actual2);
816 void init(Value *Func, Value *Actual);
817 void init(Value *Func);
819 template<typename InputIterator>
820 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
821 const Twine &NameStr,
822 // This argument ensures that we have an iterator we can
823 // do arithmetic on in constant time
824 std::random_access_iterator_tag) {
825 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
827 // This requires that the iterator points to contiguous memory.
828 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
832 /// Construct a CallInst given a range of arguments. InputIterator
833 /// must be a random-access iterator pointing to contiguous storage
834 /// (e.g. a std::vector<>::iterator). Checks are made for
835 /// random-accessness but not for contiguous storage as that would
836 /// incur runtime overhead.
837 /// @brief Construct a CallInst from a range of arguments
838 template<typename InputIterator>
839 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
840 const Twine &NameStr, Instruction *InsertBefore);
842 /// Construct a CallInst given a range of arguments. InputIterator
843 /// must be a random-access iterator pointing to contiguous storage
844 /// (e.g. a std::vector<>::iterator). Checks are made for
845 /// random-accessness but not for contiguous storage as that would
846 /// incur runtime overhead.
847 /// @brief Construct a CallInst from a range of arguments
848 template<typename InputIterator>
849 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
850 const Twine &NameStr, BasicBlock *InsertAtEnd);
852 CallInst(Value *F, Value *Actual, const Twine &NameStr,
853 Instruction *InsertBefore);
854 CallInst(Value *F, Value *Actual, const Twine &NameStr,
855 BasicBlock *InsertAtEnd);
856 explicit CallInst(Value *F, const Twine &NameStr,
857 Instruction *InsertBefore);
858 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
860 template<typename InputIterator>
861 static CallInst *Create(Value *Func,
862 InputIterator ArgBegin, InputIterator ArgEnd,
863 const Twine &NameStr = "",
864 Instruction *InsertBefore = 0) {
865 return new((unsigned)(ArgEnd - ArgBegin + 1))
866 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
868 template<typename InputIterator>
869 static CallInst *Create(Value *Func,
870 InputIterator ArgBegin, InputIterator ArgEnd,
871 const Twine &NameStr, BasicBlock *InsertAtEnd) {
872 return new((unsigned)(ArgEnd - ArgBegin + 1))
873 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
875 static CallInst *Create(Value *F, Value *Actual,
876 const Twine &NameStr = "",
877 Instruction *InsertBefore = 0) {
878 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
880 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
881 BasicBlock *InsertAtEnd) {
882 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
884 static CallInst *Create(Value *F, const Twine &NameStr = "",
885 Instruction *InsertBefore = 0) {
886 return new(1) CallInst(F, NameStr, InsertBefore);
888 static CallInst *Create(Value *F, const Twine &NameStr,
889 BasicBlock *InsertAtEnd) {
890 return new(1) CallInst(F, NameStr, InsertAtEnd);
892 /// CreateMalloc - Generate the IR for a call to malloc:
893 /// 1. Compute the malloc call's argument as the specified type's size,
894 /// possibly multiplied by the array size if the array size is not
896 /// 2. Call malloc with that argument.
897 /// 3. Bitcast the result of the malloc call to the specified type.
898 static Instruction *CreateMalloc(Instruction *InsertBefore,
899 const Type *IntPtrTy, const Type *AllocTy,
900 Value *ArraySize = 0,
901 const Twine &Name = "");
902 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
903 const Type *IntPtrTy, const Type *AllocTy,
904 Value *ArraySize = 0, Function* MallocF = 0,
905 const Twine &Name = "");
906 /// CreateFree - Generate the IR for a call to the builtin free function.
907 static void CreateFree(Value* Source, Instruction *InsertBefore);
908 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
912 bool isTailCall() const { return SubclassData & 1; }
913 void setTailCall(bool isTC = true) {
914 SubclassData = (SubclassData & ~1) | unsigned(isTC);
917 virtual CallInst *clone() const;
919 /// Provide fast operand accessors
920 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
922 /// getCallingConv/setCallingConv - Get or set the calling convention of this
924 CallingConv::ID getCallingConv() const {
925 return static_cast<CallingConv::ID>(SubclassData >> 1);
927 void setCallingConv(CallingConv::ID CC) {
928 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
931 /// getAttributes - Return the parameter attributes for this call.
933 const AttrListPtr &getAttributes() const { return AttributeList; }
935 /// setAttributes - Set the parameter attributes for this call.
937 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
939 /// addAttribute - adds the attribute to the list of attributes.
940 void addAttribute(unsigned i, Attributes attr);
942 /// removeAttribute - removes the attribute from the list of attributes.
943 void removeAttribute(unsigned i, Attributes attr);
945 /// @brief Determine whether the call or the callee has the given attribute.
946 bool paramHasAttr(unsigned i, Attributes attr) const;
948 /// @brief Extract the alignment for a call or parameter (0=unknown).
949 unsigned getParamAlignment(unsigned i) const {
950 return AttributeList.getParamAlignment(i);
953 /// @brief Determine if the call does not access memory.
954 bool doesNotAccessMemory() const {
955 return paramHasAttr(~0, Attribute::ReadNone);
957 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
958 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
959 else removeAttribute(~0, Attribute::ReadNone);
962 /// @brief Determine if the call does not access or only reads memory.
963 bool onlyReadsMemory() const {
964 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
966 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
967 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
968 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
971 /// @brief Determine if the call cannot return.
972 bool doesNotReturn() const {
973 return paramHasAttr(~0, Attribute::NoReturn);
975 void setDoesNotReturn(bool DoesNotReturn = true) {
976 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
977 else removeAttribute(~0, Attribute::NoReturn);
980 /// @brief Determine if the call cannot unwind.
981 bool doesNotThrow() const {
982 return paramHasAttr(~0, Attribute::NoUnwind);
984 void setDoesNotThrow(bool DoesNotThrow = true) {
985 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
986 else removeAttribute(~0, Attribute::NoUnwind);
989 /// @brief Determine if the call returns a structure through first
990 /// pointer argument.
991 bool hasStructRetAttr() const {
992 // Be friendly and also check the callee.
993 return paramHasAttr(1, Attribute::StructRet);
996 /// @brief Determine if any call argument is an aggregate passed by value.
997 bool hasByValArgument() const {
998 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1001 /// getCalledFunction - Return the function called, or null if this is an
1002 /// indirect function invocation.
1004 Function *getCalledFunction() const {
1005 return dyn_cast<Function>(Op<0>());
1008 /// getCalledValue - Get a pointer to the function that is invoked by this
1010 const Value *getCalledValue() const { return Op<0>(); }
1011 Value *getCalledValue() { return Op<0>(); }
1013 /// setCalledFunction - Set the function called.
1014 void setCalledFunction(Value* Fn) {
1018 // Methods for support type inquiry through isa, cast, and dyn_cast:
1019 static inline bool classof(const CallInst *) { return true; }
1020 static inline bool classof(const Instruction *I) {
1021 return I->getOpcode() == Instruction::Call;
1023 static inline bool classof(const Value *V) {
1024 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1029 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1032 template<typename InputIterator>
1033 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1034 const Twine &NameStr, BasicBlock *InsertAtEnd)
1035 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1036 ->getElementType())->getReturnType(),
1038 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1039 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1040 init(Func, ArgBegin, ArgEnd, NameStr,
1041 typename std::iterator_traits<InputIterator>::iterator_category());
1044 template<typename InputIterator>
1045 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1046 const Twine &NameStr, Instruction *InsertBefore)
1047 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1048 ->getElementType())->getReturnType(),
1050 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1051 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1052 init(Func, ArgBegin, ArgEnd, NameStr,
1053 typename std::iterator_traits<InputIterator>::iterator_category());
1056 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1058 //===----------------------------------------------------------------------===//
1060 //===----------------------------------------------------------------------===//
1062 /// SelectInst - This class represents the LLVM 'select' instruction.
1064 class SelectInst : public Instruction {
1065 void init(Value *C, Value *S1, Value *S2) {
1066 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1072 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1073 Instruction *InsertBefore)
1074 : Instruction(S1->getType(), Instruction::Select,
1075 &Op<0>(), 3, InsertBefore) {
1079 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1080 BasicBlock *InsertAtEnd)
1081 : Instruction(S1->getType(), Instruction::Select,
1082 &Op<0>(), 3, InsertAtEnd) {
1087 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1088 const Twine &NameStr = "",
1089 Instruction *InsertBefore = 0) {
1090 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1092 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1093 const Twine &NameStr,
1094 BasicBlock *InsertAtEnd) {
1095 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1098 const Value *getCondition() const { return Op<0>(); }
1099 const Value *getTrueValue() const { return Op<1>(); }
1100 const Value *getFalseValue() const { return Op<2>(); }
1101 Value *getCondition() { return Op<0>(); }
1102 Value *getTrueValue() { return Op<1>(); }
1103 Value *getFalseValue() { return Op<2>(); }
1105 /// areInvalidOperands - Return a string if the specified operands are invalid
1106 /// for a select operation, otherwise return null.
1107 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1109 /// Transparently provide more efficient getOperand methods.
1110 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1112 OtherOps getOpcode() const {
1113 return static_cast<OtherOps>(Instruction::getOpcode());
1116 virtual SelectInst *clone() const;
1118 // Methods for support type inquiry through isa, cast, and dyn_cast:
1119 static inline bool classof(const SelectInst *) { return true; }
1120 static inline bool classof(const Instruction *I) {
1121 return I->getOpcode() == Instruction::Select;
1123 static inline bool classof(const Value *V) {
1124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1129 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1132 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1134 //===----------------------------------------------------------------------===//
1136 //===----------------------------------------------------------------------===//
1138 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1139 /// an argument of the specified type given a va_list and increments that list
1141 class VAArgInst : public UnaryInstruction {
1143 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1144 Instruction *InsertBefore = 0)
1145 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1148 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1149 BasicBlock *InsertAtEnd)
1150 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1154 virtual VAArgInst *clone() const;
1156 // Methods for support type inquiry through isa, cast, and dyn_cast:
1157 static inline bool classof(const VAArgInst *) { return true; }
1158 static inline bool classof(const Instruction *I) {
1159 return I->getOpcode() == VAArg;
1161 static inline bool classof(const Value *V) {
1162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1166 //===----------------------------------------------------------------------===//
1167 // ExtractElementInst Class
1168 //===----------------------------------------------------------------------===//
1170 /// ExtractElementInst - This instruction extracts a single (scalar)
1171 /// element from a VectorType value
1173 class ExtractElementInst : public Instruction {
1174 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1175 Instruction *InsertBefore = 0);
1176 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1177 BasicBlock *InsertAtEnd);
1179 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1180 const Twine &NameStr = "",
1181 Instruction *InsertBefore = 0) {
1182 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1184 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1185 const Twine &NameStr,
1186 BasicBlock *InsertAtEnd) {
1187 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1190 /// isValidOperands - Return true if an extractelement instruction can be
1191 /// formed with the specified operands.
1192 static bool isValidOperands(const Value *Vec, const Value *Idx);
1194 virtual ExtractElementInst *clone() const;
1196 Value *getVectorOperand() { return Op<0>(); }
1197 Value *getIndexOperand() { return Op<1>(); }
1198 const Value *getVectorOperand() const { return Op<0>(); }
1199 const Value *getIndexOperand() const { return Op<1>(); }
1201 const VectorType *getVectorOperandType() const {
1202 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1206 /// Transparently provide more efficient getOperand methods.
1207 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1209 // Methods for support type inquiry through isa, cast, and dyn_cast:
1210 static inline bool classof(const ExtractElementInst *) { return true; }
1211 static inline bool classof(const Instruction *I) {
1212 return I->getOpcode() == Instruction::ExtractElement;
1214 static inline bool classof(const Value *V) {
1215 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1220 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1223 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1225 //===----------------------------------------------------------------------===//
1226 // InsertElementInst Class
1227 //===----------------------------------------------------------------------===//
1229 /// InsertElementInst - This instruction inserts a single (scalar)
1230 /// element into a VectorType value
1232 class InsertElementInst : public Instruction {
1233 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1234 const Twine &NameStr = "",
1235 Instruction *InsertBefore = 0);
1236 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1237 const Twine &NameStr, BasicBlock *InsertAtEnd);
1239 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1240 const Twine &NameStr = "",
1241 Instruction *InsertBefore = 0) {
1242 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1244 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1245 const Twine &NameStr,
1246 BasicBlock *InsertAtEnd) {
1247 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1250 /// isValidOperands - Return true if an insertelement instruction can be
1251 /// formed with the specified operands.
1252 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1255 virtual InsertElementInst *clone() const;
1257 /// getType - Overload to return most specific vector type.
1259 const VectorType *getType() const {
1260 return reinterpret_cast<const VectorType*>(Instruction::getType());
1263 /// Transparently provide more efficient getOperand methods.
1264 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1266 // Methods for support type inquiry through isa, cast, and dyn_cast:
1267 static inline bool classof(const InsertElementInst *) { return true; }
1268 static inline bool classof(const Instruction *I) {
1269 return I->getOpcode() == Instruction::InsertElement;
1271 static inline bool classof(const Value *V) {
1272 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1277 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1280 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1282 //===----------------------------------------------------------------------===//
1283 // ShuffleVectorInst Class
1284 //===----------------------------------------------------------------------===//
1286 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1289 class ShuffleVectorInst : public Instruction {
1291 // allocate space for exactly three operands
1292 void *operator new(size_t s) {
1293 return User::operator new(s, 3);
1295 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1296 const Twine &NameStr = "",
1297 Instruction *InsertBefor = 0);
1298 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1299 const Twine &NameStr, BasicBlock *InsertAtEnd);
1301 /// isValidOperands - Return true if a shufflevector instruction can be
1302 /// formed with the specified operands.
1303 static bool isValidOperands(const Value *V1, const Value *V2,
1306 virtual ShuffleVectorInst *clone() const;
1308 /// getType - Overload to return most specific vector type.
1310 const VectorType *getType() const {
1311 return reinterpret_cast<const VectorType*>(Instruction::getType());
1314 /// Transparently provide more efficient getOperand methods.
1315 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1317 /// getMaskValue - Return the index from the shuffle mask for the specified
1318 /// output result. This is either -1 if the element is undef or a number less
1319 /// than 2*numelements.
1320 int getMaskValue(unsigned i) const;
1322 // Methods for support type inquiry through isa, cast, and dyn_cast:
1323 static inline bool classof(const ShuffleVectorInst *) { return true; }
1324 static inline bool classof(const Instruction *I) {
1325 return I->getOpcode() == Instruction::ShuffleVector;
1327 static inline bool classof(const Value *V) {
1328 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1333 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1336 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1338 //===----------------------------------------------------------------------===//
1339 // ExtractValueInst Class
1340 //===----------------------------------------------------------------------===//
1342 /// ExtractValueInst - This instruction extracts a struct member or array
1343 /// element value from an aggregate value.
1345 class ExtractValueInst : public UnaryInstruction {
1346 SmallVector<unsigned, 4> Indices;
1348 ExtractValueInst(const ExtractValueInst &EVI);
1349 void init(const unsigned *Idx, unsigned NumIdx,
1350 const Twine &NameStr);
1351 void init(unsigned Idx, const Twine &NameStr);
1353 template<typename InputIterator>
1354 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1355 const Twine &NameStr,
1356 // This argument ensures that we have an iterator we can
1357 // do arithmetic on in constant time
1358 std::random_access_iterator_tag) {
1359 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1361 // There's no fundamental reason why we require at least one index
1362 // (other than weirdness with &*IdxBegin being invalid; see
1363 // getelementptr's init routine for example). But there's no
1364 // present need to support it.
1365 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1367 // This requires that the iterator points to contiguous memory.
1368 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1369 // we have to build an array here
1372 /// getIndexedType - Returns the type of the element that would be extracted
1373 /// with an extractvalue instruction with the specified parameters.
1375 /// Null is returned if the indices are invalid for the specified
1378 static const Type *getIndexedType(const Type *Agg,
1379 const unsigned *Idx, unsigned NumIdx);
1381 template<typename InputIterator>
1382 static const Type *getIndexedType(const Type *Ptr,
1383 InputIterator IdxBegin,
1384 InputIterator IdxEnd,
1385 // This argument ensures that we
1386 // have an iterator we can do
1387 // arithmetic on in constant time
1388 std::random_access_iterator_tag) {
1389 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1392 // This requires that the iterator points to contiguous memory.
1393 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1395 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1398 /// Constructors - Create a extractvalue instruction with a base aggregate
1399 /// value and a list of indices. The first ctor can optionally insert before
1400 /// an existing instruction, the second appends the new instruction to the
1401 /// specified BasicBlock.
1402 template<typename InputIterator>
1403 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1404 InputIterator IdxEnd,
1405 const Twine &NameStr,
1406 Instruction *InsertBefore);
1407 template<typename InputIterator>
1408 inline ExtractValueInst(Value *Agg,
1409 InputIterator IdxBegin, InputIterator IdxEnd,
1410 const Twine &NameStr, BasicBlock *InsertAtEnd);
1412 // allocate space for exactly one operand
1413 void *operator new(size_t s) {
1414 return User::operator new(s, 1);
1418 template<typename InputIterator>
1419 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1420 InputIterator IdxEnd,
1421 const Twine &NameStr = "",
1422 Instruction *InsertBefore = 0) {
1424 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1426 template<typename InputIterator>
1427 static ExtractValueInst *Create(Value *Agg,
1428 InputIterator IdxBegin, InputIterator IdxEnd,
1429 const Twine &NameStr,
1430 BasicBlock *InsertAtEnd) {
1431 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1434 /// Constructors - These two creators are convenience methods because one
1435 /// index extractvalue instructions are much more common than those with
1437 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1438 const Twine &NameStr = "",
1439 Instruction *InsertBefore = 0) {
1440 unsigned Idxs[1] = { Idx };
1441 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1443 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1444 const Twine &NameStr,
1445 BasicBlock *InsertAtEnd) {
1446 unsigned Idxs[1] = { Idx };
1447 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1450 virtual ExtractValueInst *clone() const;
1452 /// getIndexedType - Returns the type of the element that would be extracted
1453 /// with an extractvalue instruction with the specified parameters.
1455 /// Null is returned if the indices are invalid for the specified
1458 template<typename InputIterator>
1459 static const Type *getIndexedType(const Type *Ptr,
1460 InputIterator IdxBegin,
1461 InputIterator IdxEnd) {
1462 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1463 typename std::iterator_traits<InputIterator>::
1464 iterator_category());
1466 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1468 typedef const unsigned* idx_iterator;
1469 inline idx_iterator idx_begin() const { return Indices.begin(); }
1470 inline idx_iterator idx_end() const { return Indices.end(); }
1472 Value *getAggregateOperand() {
1473 return getOperand(0);
1475 const Value *getAggregateOperand() const {
1476 return getOperand(0);
1478 static unsigned getAggregateOperandIndex() {
1479 return 0U; // get index for modifying correct operand
1482 unsigned getNumIndices() const { // Note: always non-negative
1483 return (unsigned)Indices.size();
1486 bool hasIndices() const {
1490 // Methods for support type inquiry through isa, cast, and dyn_cast:
1491 static inline bool classof(const ExtractValueInst *) { return true; }
1492 static inline bool classof(const Instruction *I) {
1493 return I->getOpcode() == Instruction::ExtractValue;
1495 static inline bool classof(const Value *V) {
1496 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1500 template<typename InputIterator>
1501 ExtractValueInst::ExtractValueInst(Value *Agg,
1502 InputIterator IdxBegin,
1503 InputIterator IdxEnd,
1504 const Twine &NameStr,
1505 Instruction *InsertBefore)
1506 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1508 ExtractValue, Agg, InsertBefore) {
1509 init(IdxBegin, IdxEnd, NameStr,
1510 typename std::iterator_traits<InputIterator>::iterator_category());
1512 template<typename InputIterator>
1513 ExtractValueInst::ExtractValueInst(Value *Agg,
1514 InputIterator IdxBegin,
1515 InputIterator IdxEnd,
1516 const Twine &NameStr,
1517 BasicBlock *InsertAtEnd)
1518 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1520 ExtractValue, Agg, InsertAtEnd) {
1521 init(IdxBegin, IdxEnd, NameStr,
1522 typename std::iterator_traits<InputIterator>::iterator_category());
1526 //===----------------------------------------------------------------------===//
1527 // InsertValueInst Class
1528 //===----------------------------------------------------------------------===//
1530 /// InsertValueInst - This instruction inserts a struct field of array element
1531 /// value into an aggregate value.
1533 class InsertValueInst : public Instruction {
1534 SmallVector<unsigned, 4> Indices;
1536 void *operator new(size_t, unsigned); // Do not implement
1537 InsertValueInst(const InsertValueInst &IVI);
1538 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1539 const Twine &NameStr);
1540 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1542 template<typename InputIterator>
1543 void init(Value *Agg, Value *Val,
1544 InputIterator IdxBegin, InputIterator IdxEnd,
1545 const Twine &NameStr,
1546 // This argument ensures that we have an iterator we can
1547 // do arithmetic on in constant time
1548 std::random_access_iterator_tag) {
1549 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1551 // There's no fundamental reason why we require at least one index
1552 // (other than weirdness with &*IdxBegin being invalid; see
1553 // getelementptr's init routine for example). But there's no
1554 // present need to support it.
1555 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1557 // This requires that the iterator points to contiguous memory.
1558 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1559 // we have to build an array here
1562 /// Constructors - Create a insertvalue instruction with a base aggregate
1563 /// value, a value to insert, and a list of indices. The first ctor can
1564 /// optionally insert before an existing instruction, the second appends
1565 /// the new instruction to the specified BasicBlock.
1566 template<typename InputIterator>
1567 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1568 InputIterator IdxEnd,
1569 const Twine &NameStr,
1570 Instruction *InsertBefore);
1571 template<typename InputIterator>
1572 inline InsertValueInst(Value *Agg, Value *Val,
1573 InputIterator IdxBegin, InputIterator IdxEnd,
1574 const Twine &NameStr, BasicBlock *InsertAtEnd);
1576 /// Constructors - These two constructors are convenience methods because one
1577 /// and two index insertvalue instructions are so common.
1578 InsertValueInst(Value *Agg, Value *Val,
1579 unsigned Idx, const Twine &NameStr = "",
1580 Instruction *InsertBefore = 0);
1581 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1582 const Twine &NameStr, BasicBlock *InsertAtEnd);
1584 // allocate space for exactly two operands
1585 void *operator new(size_t s) {
1586 return User::operator new(s, 2);
1589 template<typename InputIterator>
1590 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1591 InputIterator IdxEnd,
1592 const Twine &NameStr = "",
1593 Instruction *InsertBefore = 0) {
1594 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1595 NameStr, InsertBefore);
1597 template<typename InputIterator>
1598 static InsertValueInst *Create(Value *Agg, Value *Val,
1599 InputIterator IdxBegin, InputIterator IdxEnd,
1600 const Twine &NameStr,
1601 BasicBlock *InsertAtEnd) {
1602 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1603 NameStr, InsertAtEnd);
1606 /// Constructors - These two creators are convenience methods because one
1607 /// index insertvalue instructions are much more common than those with
1609 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1610 const Twine &NameStr = "",
1611 Instruction *InsertBefore = 0) {
1612 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1614 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1615 const Twine &NameStr,
1616 BasicBlock *InsertAtEnd) {
1617 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1620 virtual InsertValueInst *clone() const;
1622 /// Transparently provide more efficient getOperand methods.
1623 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1625 typedef const unsigned* idx_iterator;
1626 inline idx_iterator idx_begin() const { return Indices.begin(); }
1627 inline idx_iterator idx_end() const { return Indices.end(); }
1629 Value *getAggregateOperand() {
1630 return getOperand(0);
1632 const Value *getAggregateOperand() const {
1633 return getOperand(0);
1635 static unsigned getAggregateOperandIndex() {
1636 return 0U; // get index for modifying correct operand
1639 Value *getInsertedValueOperand() {
1640 return getOperand(1);
1642 const Value *getInsertedValueOperand() const {
1643 return getOperand(1);
1645 static unsigned getInsertedValueOperandIndex() {
1646 return 1U; // get index for modifying correct operand
1649 unsigned getNumIndices() const { // Note: always non-negative
1650 return (unsigned)Indices.size();
1653 bool hasIndices() const {
1657 // Methods for support type inquiry through isa, cast, and dyn_cast:
1658 static inline bool classof(const InsertValueInst *) { return true; }
1659 static inline bool classof(const Instruction *I) {
1660 return I->getOpcode() == Instruction::InsertValue;
1662 static inline bool classof(const Value *V) {
1663 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1668 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1671 template<typename InputIterator>
1672 InsertValueInst::InsertValueInst(Value *Agg,
1674 InputIterator IdxBegin,
1675 InputIterator IdxEnd,
1676 const Twine &NameStr,
1677 Instruction *InsertBefore)
1678 : Instruction(Agg->getType(), InsertValue,
1679 OperandTraits<InsertValueInst>::op_begin(this),
1681 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1682 typename std::iterator_traits<InputIterator>::iterator_category());
1684 template<typename InputIterator>
1685 InsertValueInst::InsertValueInst(Value *Agg,
1687 InputIterator IdxBegin,
1688 InputIterator IdxEnd,
1689 const Twine &NameStr,
1690 BasicBlock *InsertAtEnd)
1691 : Instruction(Agg->getType(), InsertValue,
1692 OperandTraits<InsertValueInst>::op_begin(this),
1694 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1695 typename std::iterator_traits<InputIterator>::iterator_category());
1698 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1700 //===----------------------------------------------------------------------===//
1702 //===----------------------------------------------------------------------===//
1704 // PHINode - The PHINode class is used to represent the magical mystical PHI
1705 // node, that can not exist in nature, but can be synthesized in a computer
1706 // scientist's overactive imagination.
1708 class PHINode : public Instruction {
1709 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1710 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1711 /// the number actually in use.
1712 unsigned ReservedSpace;
1713 PHINode(const PHINode &PN);
1714 // allocate space for exactly zero operands
1715 void *operator new(size_t s) {
1716 return User::operator new(s, 0);
1718 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1719 Instruction *InsertBefore = 0)
1720 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1725 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1726 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1731 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1732 Instruction *InsertBefore = 0) {
1733 return new PHINode(Ty, NameStr, InsertBefore);
1735 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1736 BasicBlock *InsertAtEnd) {
1737 return new PHINode(Ty, NameStr, InsertAtEnd);
1741 /// reserveOperandSpace - This method can be used to avoid repeated
1742 /// reallocation of PHI operand lists by reserving space for the correct
1743 /// number of operands before adding them. Unlike normal vector reserves,
1744 /// this method can also be used to trim the operand space.
1745 void reserveOperandSpace(unsigned NumValues) {
1746 resizeOperands(NumValues*2);
1749 virtual PHINode *clone() const;
1751 /// Provide fast operand accessors
1752 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1754 /// getNumIncomingValues - Return the number of incoming edges
1756 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1758 /// getIncomingValue - Return incoming value number x
1760 Value *getIncomingValue(unsigned i) const {
1761 assert(i*2 < getNumOperands() && "Invalid value number!");
1762 return getOperand(i*2);
1764 void setIncomingValue(unsigned i, Value *V) {
1765 assert(i*2 < getNumOperands() && "Invalid value number!");
1768 static unsigned getOperandNumForIncomingValue(unsigned i) {
1771 static unsigned getIncomingValueNumForOperand(unsigned i) {
1772 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1776 /// getIncomingBlock - Return incoming basic block #i.
1778 BasicBlock *getIncomingBlock(unsigned i) const {
1779 return cast<BasicBlock>(getOperand(i*2+1));
1782 /// getIncomingBlock - Return incoming basic block corresponding
1783 /// to an operand of the PHI.
1785 BasicBlock *getIncomingBlock(const Use &U) const {
1786 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1787 return cast<BasicBlock>((&U + 1)->get());
1790 /// getIncomingBlock - Return incoming basic block corresponding
1791 /// to value use iterator.
1793 template <typename U>
1794 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1795 return getIncomingBlock(I.getUse());
1799 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1800 setOperand(i*2+1, BB);
1802 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1805 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1806 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1810 /// addIncoming - Add an incoming value to the end of the PHI list
1812 void addIncoming(Value *V, BasicBlock *BB) {
1813 assert(V && "PHI node got a null value!");
1814 assert(BB && "PHI node got a null basic block!");
1815 assert(getType() == V->getType() &&
1816 "All operands to PHI node must be the same type as the PHI node!");
1817 unsigned OpNo = NumOperands;
1818 if (OpNo+2 > ReservedSpace)
1819 resizeOperands(0); // Get more space!
1820 // Initialize some new operands.
1821 NumOperands = OpNo+2;
1822 OperandList[OpNo] = V;
1823 OperandList[OpNo+1] = BB;
1826 /// removeIncomingValue - Remove an incoming value. This is useful if a
1827 /// predecessor basic block is deleted. The value removed is returned.
1829 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1830 /// is true), the PHI node is destroyed and any uses of it are replaced with
1831 /// dummy values. The only time there should be zero incoming values to a PHI
1832 /// node is when the block is dead, so this strategy is sound.
1834 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1836 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1837 int Idx = getBasicBlockIndex(BB);
1838 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1839 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1842 /// getBasicBlockIndex - Return the first index of the specified basic
1843 /// block in the value list for this PHI. Returns -1 if no instance.
1845 int getBasicBlockIndex(const BasicBlock *BB) const {
1846 Use *OL = OperandList;
1847 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1848 if (OL[i+1].get() == BB) return i/2;
1852 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1853 return getIncomingValue(getBasicBlockIndex(BB));
1856 /// hasConstantValue - If the specified PHI node always merges together the
1857 /// same value, return the value, otherwise return null.
1859 /// If the PHI has undef operands, but all the rest of the operands are
1860 /// some unique value, return that value if it can be proved that the
1861 /// value dominates the PHI. If DT is null, use a conservative check,
1862 /// otherwise use DT to test for dominance.
1864 Value *hasConstantValue(DominatorTree *DT = 0) const;
1866 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1867 static inline bool classof(const PHINode *) { return true; }
1868 static inline bool classof(const Instruction *I) {
1869 return I->getOpcode() == Instruction::PHI;
1871 static inline bool classof(const Value *V) {
1872 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1875 void resizeOperands(unsigned NumOperands);
1879 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1882 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1885 //===----------------------------------------------------------------------===//
1887 //===----------------------------------------------------------------------===//
1889 //===---------------------------------------------------------------------------
1890 /// ReturnInst - Return a value (possibly void), from a function. Execution
1891 /// does not continue in this function any longer.
1893 class ReturnInst : public TerminatorInst {
1894 ReturnInst(const ReturnInst &RI);
1897 // ReturnInst constructors:
1898 // ReturnInst() - 'ret void' instruction
1899 // ReturnInst( null) - 'ret void' instruction
1900 // ReturnInst(Value* X) - 'ret X' instruction
1901 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1902 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1903 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1904 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1906 // NOTE: If the Value* passed is of type void then the constructor behaves as
1907 // if it was passed NULL.
1908 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1909 Instruction *InsertBefore = 0);
1910 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1911 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1913 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1914 Instruction *InsertBefore = 0) {
1915 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1917 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1918 BasicBlock *InsertAtEnd) {
1919 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1921 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1922 return new(0) ReturnInst(C, InsertAtEnd);
1924 virtual ~ReturnInst();
1926 virtual ReturnInst *clone() const;
1928 /// Provide fast operand accessors
1929 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1931 /// Convenience accessor
1932 Value *getReturnValue(unsigned n = 0) const {
1933 return n < getNumOperands()
1938 unsigned getNumSuccessors() const { return 0; }
1940 // Methods for support type inquiry through isa, cast, and dyn_cast:
1941 static inline bool classof(const ReturnInst *) { return true; }
1942 static inline bool classof(const Instruction *I) {
1943 return (I->getOpcode() == Instruction::Ret);
1945 static inline bool classof(const Value *V) {
1946 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1949 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1950 virtual unsigned getNumSuccessorsV() const;
1951 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1955 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
1958 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1960 //===----------------------------------------------------------------------===//
1962 //===----------------------------------------------------------------------===//
1964 //===---------------------------------------------------------------------------
1965 /// BranchInst - Conditional or Unconditional Branch instruction.
1967 class BranchInst : public TerminatorInst {
1968 /// Ops list - Branches are strange. The operands are ordered:
1969 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
1970 /// they don't have to check for cond/uncond branchness. These are mostly
1971 /// accessed relative from op_end().
1972 BranchInst(const BranchInst &BI);
1974 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1975 // BranchInst(BB *B) - 'br B'
1976 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1977 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1978 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1979 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1980 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1981 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1982 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1983 Instruction *InsertBefore = 0);
1984 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1985 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1986 BasicBlock *InsertAtEnd);
1988 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1989 return new(1, true) BranchInst(IfTrue, InsertBefore);
1991 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1992 Value *Cond, Instruction *InsertBefore = 0) {
1993 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
1995 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
1996 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
1998 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1999 Value *Cond, BasicBlock *InsertAtEnd) {
2000 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2005 /// Transparently provide more efficient getOperand methods.
2006 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2008 virtual BranchInst *clone() const;
2010 bool isUnconditional() const { return getNumOperands() == 1; }
2011 bool isConditional() const { return getNumOperands() == 3; }
2013 Value *getCondition() const {
2014 assert(isConditional() && "Cannot get condition of an uncond branch!");
2018 void setCondition(Value *V) {
2019 assert(isConditional() && "Cannot set condition of unconditional branch!");
2023 // setUnconditionalDest - Change the current branch to an unconditional branch
2024 // targeting the specified block.
2025 // FIXME: Eliminate this ugly method.
2026 void setUnconditionalDest(BasicBlock *Dest) {
2028 if (isConditional()) { // Convert this to an uncond branch.
2032 OperandList = op_begin();
2036 unsigned getNumSuccessors() const { return 1+isConditional(); }
2038 BasicBlock *getSuccessor(unsigned i) const {
2039 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2040 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2043 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2044 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2045 *(&Op<-1>() - idx) = NewSucc;
2048 // Methods for support type inquiry through isa, cast, and dyn_cast:
2049 static inline bool classof(const BranchInst *) { return true; }
2050 static inline bool classof(const Instruction *I) {
2051 return (I->getOpcode() == Instruction::Br);
2053 static inline bool classof(const Value *V) {
2054 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2057 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2058 virtual unsigned getNumSuccessorsV() const;
2059 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2063 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2065 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2067 //===----------------------------------------------------------------------===//
2069 //===----------------------------------------------------------------------===//
2071 //===---------------------------------------------------------------------------
2072 /// SwitchInst - Multiway switch
2074 class SwitchInst : public TerminatorInst {
2075 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2076 unsigned ReservedSpace;
2077 // Operand[0] = Value to switch on
2078 // Operand[1] = Default basic block destination
2079 // Operand[2n ] = Value to match
2080 // Operand[2n+1] = BasicBlock to go to on match
2081 SwitchInst(const SwitchInst &RI);
2082 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2083 void resizeOperands(unsigned No);
2084 // allocate space for exactly zero operands
2085 void *operator new(size_t s) {
2086 return User::operator new(s, 0);
2088 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2089 /// switch on and a default destination. The number of additional cases can
2090 /// be specified here to make memory allocation more efficient. This
2091 /// constructor can also autoinsert before another instruction.
2092 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2093 Instruction *InsertBefore = 0);
2095 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2096 /// switch on and a default destination. The number of additional cases can
2097 /// be specified here to make memory allocation more efficient. This
2098 /// constructor also autoinserts at the end of the specified BasicBlock.
2099 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2100 BasicBlock *InsertAtEnd);
2102 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2103 unsigned NumCases, Instruction *InsertBefore = 0) {
2104 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2106 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2107 unsigned NumCases, BasicBlock *InsertAtEnd) {
2108 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2112 /// Provide fast operand accessors
2113 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2115 // Accessor Methods for Switch stmt
2116 Value *getCondition() const { return getOperand(0); }
2117 void setCondition(Value *V) { setOperand(0, V); }
2119 BasicBlock *getDefaultDest() const {
2120 return cast<BasicBlock>(getOperand(1));
2123 /// getNumCases - return the number of 'cases' in this switch instruction.
2124 /// Note that case #0 is always the default case.
2125 unsigned getNumCases() const {
2126 return getNumOperands()/2;
2129 /// getCaseValue - Return the specified case value. Note that case #0, the
2130 /// default destination, does not have a case value.
2131 ConstantInt *getCaseValue(unsigned i) {
2132 assert(i && i < getNumCases() && "Illegal case value to get!");
2133 return getSuccessorValue(i);
2136 /// getCaseValue - Return the specified case value. Note that case #0, the
2137 /// default destination, does not have a case value.
2138 const ConstantInt *getCaseValue(unsigned i) const {
2139 assert(i && i < getNumCases() && "Illegal case value to get!");
2140 return getSuccessorValue(i);
2143 /// findCaseValue - Search all of the case values for the specified constant.
2144 /// If it is explicitly handled, return the case number of it, otherwise
2145 /// return 0 to indicate that it is handled by the default handler.
2146 unsigned findCaseValue(const ConstantInt *C) const {
2147 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2148 if (getCaseValue(i) == C)
2153 /// findCaseDest - Finds the unique case value for a given successor. Returns
2154 /// null if the successor is not found, not unique, or is the default case.
2155 ConstantInt *findCaseDest(BasicBlock *BB) {
2156 if (BB == getDefaultDest()) return NULL;
2158 ConstantInt *CI = NULL;
2159 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2160 if (getSuccessor(i) == BB) {
2161 if (CI) return NULL; // Multiple cases lead to BB.
2162 else CI = getCaseValue(i);
2168 /// addCase - Add an entry to the switch instruction...
2170 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2172 /// removeCase - This method removes the specified successor from the switch
2173 /// instruction. Note that this cannot be used to remove the default
2174 /// destination (successor #0).
2176 void removeCase(unsigned idx);
2178 virtual SwitchInst *clone() const;
2180 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2181 BasicBlock *getSuccessor(unsigned idx) const {
2182 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2183 return cast<BasicBlock>(getOperand(idx*2+1));
2185 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2186 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2187 setOperand(idx*2+1, NewSucc);
2190 // getSuccessorValue - Return the value associated with the specified
2192 ConstantInt *getSuccessorValue(unsigned idx) const {
2193 assert(idx < getNumSuccessors() && "Successor # out of range!");
2194 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2197 // Methods for support type inquiry through isa, cast, and dyn_cast:
2198 static inline bool classof(const SwitchInst *) { return true; }
2199 static inline bool classof(const Instruction *I) {
2200 return I->getOpcode() == Instruction::Switch;
2202 static inline bool classof(const Value *V) {
2203 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2206 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2207 virtual unsigned getNumSuccessorsV() const;
2208 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2212 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2215 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2218 //===----------------------------------------------------------------------===//
2220 //===----------------------------------------------------------------------===//
2222 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2223 /// calling convention of the call.
2225 class InvokeInst : public TerminatorInst {
2226 AttrListPtr AttributeList;
2227 InvokeInst(const InvokeInst &BI);
2228 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2229 Value* const *Args, unsigned NumArgs);
2231 template<typename InputIterator>
2232 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2233 InputIterator ArgBegin, InputIterator ArgEnd,
2234 const Twine &NameStr,
2235 // This argument ensures that we have an iterator we can
2236 // do arithmetic on in constant time
2237 std::random_access_iterator_tag) {
2238 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2240 // This requires that the iterator points to contiguous memory.
2241 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2245 /// Construct an InvokeInst given a range of arguments.
2246 /// InputIterator must be a random-access iterator pointing to
2247 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2248 /// made for random-accessness but not for contiguous storage as
2249 /// that would incur runtime overhead.
2251 /// @brief Construct an InvokeInst from a range of arguments
2252 template<typename InputIterator>
2253 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2254 InputIterator ArgBegin, InputIterator ArgEnd,
2256 const Twine &NameStr, Instruction *InsertBefore);
2258 /// Construct an InvokeInst given a range of arguments.
2259 /// InputIterator must be a random-access iterator pointing to
2260 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2261 /// made for random-accessness but not for contiguous storage as
2262 /// that would incur runtime overhead.
2264 /// @brief Construct an InvokeInst from a range of arguments
2265 template<typename InputIterator>
2266 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2267 InputIterator ArgBegin, InputIterator ArgEnd,
2269 const Twine &NameStr, BasicBlock *InsertAtEnd);
2271 template<typename InputIterator>
2272 static InvokeInst *Create(Value *Func,
2273 BasicBlock *IfNormal, BasicBlock *IfException,
2274 InputIterator ArgBegin, InputIterator ArgEnd,
2275 const Twine &NameStr = "",
2276 Instruction *InsertBefore = 0) {
2277 unsigned Values(ArgEnd - ArgBegin + 3);
2278 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2279 Values, NameStr, InsertBefore);
2281 template<typename InputIterator>
2282 static InvokeInst *Create(Value *Func,
2283 BasicBlock *IfNormal, BasicBlock *IfException,
2284 InputIterator ArgBegin, InputIterator ArgEnd,
2285 const Twine &NameStr,
2286 BasicBlock *InsertAtEnd) {
2287 unsigned Values(ArgEnd - ArgBegin + 3);
2288 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2289 Values, NameStr, InsertAtEnd);
2292 virtual InvokeInst *clone() const;
2294 /// Provide fast operand accessors
2295 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2297 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2299 CallingConv::ID getCallingConv() const {
2300 return static_cast<CallingConv::ID>(SubclassData);
2302 void setCallingConv(CallingConv::ID CC) {
2303 SubclassData = static_cast<unsigned>(CC);
2306 /// getAttributes - Return the parameter attributes for this invoke.
2308 const AttrListPtr &getAttributes() const { return AttributeList; }
2310 /// setAttributes - Set the parameter attributes for this invoke.
2312 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2314 /// addAttribute - adds the attribute to the list of attributes.
2315 void addAttribute(unsigned i, Attributes attr);
2317 /// removeAttribute - removes the attribute from the list of attributes.
2318 void removeAttribute(unsigned i, Attributes attr);
2320 /// @brief Determine whether the call or the callee has the given attribute.
2321 bool paramHasAttr(unsigned i, Attributes attr) const;
2323 /// @brief Extract the alignment for a call or parameter (0=unknown).
2324 unsigned getParamAlignment(unsigned i) const {
2325 return AttributeList.getParamAlignment(i);
2328 /// @brief Determine if the call does not access memory.
2329 bool doesNotAccessMemory() const {
2330 return paramHasAttr(~0, Attribute::ReadNone);
2332 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2333 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2334 else removeAttribute(~0, Attribute::ReadNone);
2337 /// @brief Determine if the call does not access or only reads memory.
2338 bool onlyReadsMemory() const {
2339 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2341 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2342 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2343 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2346 /// @brief Determine if the call cannot return.
2347 bool doesNotReturn() const {
2348 return paramHasAttr(~0, Attribute::NoReturn);
2350 void setDoesNotReturn(bool DoesNotReturn = true) {
2351 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2352 else removeAttribute(~0, Attribute::NoReturn);
2355 /// @brief Determine if the call cannot unwind.
2356 bool doesNotThrow() const {
2357 return paramHasAttr(~0, Attribute::NoUnwind);
2359 void setDoesNotThrow(bool DoesNotThrow = true) {
2360 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2361 else removeAttribute(~0, Attribute::NoUnwind);
2364 /// @brief Determine if the call returns a structure through first
2365 /// pointer argument.
2366 bool hasStructRetAttr() const {
2367 // Be friendly and also check the callee.
2368 return paramHasAttr(1, Attribute::StructRet);
2371 /// @brief Determine if any call argument is an aggregate passed by value.
2372 bool hasByValArgument() const {
2373 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2376 /// getCalledFunction - Return the function called, or null if this is an
2377 /// indirect function invocation.
2379 Function *getCalledFunction() const {
2380 return dyn_cast<Function>(getOperand(0));
2383 /// getCalledValue - Get a pointer to the function that is invoked by this
2385 const Value *getCalledValue() const { return getOperand(0); }
2386 Value *getCalledValue() { return getOperand(0); }
2388 // get*Dest - Return the destination basic blocks...
2389 BasicBlock *getNormalDest() const {
2390 return cast<BasicBlock>(getOperand(1));
2392 BasicBlock *getUnwindDest() const {
2393 return cast<BasicBlock>(getOperand(2));
2395 void setNormalDest(BasicBlock *B) {
2399 void setUnwindDest(BasicBlock *B) {
2403 BasicBlock *getSuccessor(unsigned i) const {
2404 assert(i < 2 && "Successor # out of range for invoke!");
2405 return i == 0 ? getNormalDest() : getUnwindDest();
2408 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2409 assert(idx < 2 && "Successor # out of range for invoke!");
2410 setOperand(idx+1, NewSucc);
2413 unsigned getNumSuccessors() const { return 2; }
2415 // Methods for support type inquiry through isa, cast, and dyn_cast:
2416 static inline bool classof(const InvokeInst *) { return true; }
2417 static inline bool classof(const Instruction *I) {
2418 return (I->getOpcode() == Instruction::Invoke);
2420 static inline bool classof(const Value *V) {
2421 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2424 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2425 virtual unsigned getNumSuccessorsV() const;
2426 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2430 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2433 template<typename InputIterator>
2434 InvokeInst::InvokeInst(Value *Func,
2435 BasicBlock *IfNormal, BasicBlock *IfException,
2436 InputIterator ArgBegin, InputIterator ArgEnd,
2438 const Twine &NameStr, Instruction *InsertBefore)
2439 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2440 ->getElementType())->getReturnType(),
2441 Instruction::Invoke,
2442 OperandTraits<InvokeInst>::op_end(this) - Values,
2443 Values, InsertBefore) {
2444 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2445 typename std::iterator_traits<InputIterator>::iterator_category());
2447 template<typename InputIterator>
2448 InvokeInst::InvokeInst(Value *Func,
2449 BasicBlock *IfNormal, BasicBlock *IfException,
2450 InputIterator ArgBegin, InputIterator ArgEnd,
2452 const Twine &NameStr, BasicBlock *InsertAtEnd)
2453 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2454 ->getElementType())->getReturnType(),
2455 Instruction::Invoke,
2456 OperandTraits<InvokeInst>::op_end(this) - Values,
2457 Values, InsertAtEnd) {
2458 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2459 typename std::iterator_traits<InputIterator>::iterator_category());
2462 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2464 //===----------------------------------------------------------------------===//
2466 //===----------------------------------------------------------------------===//
2468 //===---------------------------------------------------------------------------
2469 /// UnwindInst - Immediately exit the current function, unwinding the stack
2470 /// until an invoke instruction is found.
2472 class UnwindInst : public TerminatorInst {
2473 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2475 // allocate space for exactly zero operands
2476 void *operator new(size_t s) {
2477 return User::operator new(s, 0);
2479 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2480 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2482 virtual UnwindInst *clone() const;
2484 unsigned getNumSuccessors() const { return 0; }
2486 // Methods for support type inquiry through isa, cast, and dyn_cast:
2487 static inline bool classof(const UnwindInst *) { return true; }
2488 static inline bool classof(const Instruction *I) {
2489 return I->getOpcode() == Instruction::Unwind;
2491 static inline bool classof(const Value *V) {
2492 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2495 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2496 virtual unsigned getNumSuccessorsV() const;
2497 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2500 //===----------------------------------------------------------------------===//
2501 // UnreachableInst Class
2502 //===----------------------------------------------------------------------===//
2504 //===---------------------------------------------------------------------------
2505 /// UnreachableInst - This function has undefined behavior. In particular, the
2506 /// presence of this instruction indicates some higher level knowledge that the
2507 /// end of the block cannot be reached.
2509 class UnreachableInst : public TerminatorInst {
2510 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2512 // allocate space for exactly zero operands
2513 void *operator new(size_t s) {
2514 return User::operator new(s, 0);
2516 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2517 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2519 virtual UnreachableInst *clone() const;
2521 unsigned getNumSuccessors() const { return 0; }
2523 // Methods for support type inquiry through isa, cast, and dyn_cast:
2524 static inline bool classof(const UnreachableInst *) { return true; }
2525 static inline bool classof(const Instruction *I) {
2526 return I->getOpcode() == Instruction::Unreachable;
2528 static inline bool classof(const Value *V) {
2529 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2532 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2533 virtual unsigned getNumSuccessorsV() const;
2534 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2537 //===----------------------------------------------------------------------===//
2539 //===----------------------------------------------------------------------===//
2541 /// @brief This class represents a truncation of integer types.
2542 class TruncInst : public CastInst {
2544 /// @brief Constructor with insert-before-instruction semantics
2546 Value *S, ///< The value to be truncated
2547 const Type *Ty, ///< The (smaller) type to truncate to
2548 const Twine &NameStr = "", ///< A name for the new instruction
2549 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2552 /// @brief Constructor with insert-at-end-of-block semantics
2554 Value *S, ///< The value to be truncated
2555 const Type *Ty, ///< The (smaller) type to truncate to
2556 const Twine &NameStr, ///< A name for the new instruction
2557 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2560 /// @brief Clone an identical TruncInst
2561 virtual TruncInst *clone() const;
2563 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2564 static inline bool classof(const TruncInst *) { return true; }
2565 static inline bool classof(const Instruction *I) {
2566 return I->getOpcode() == Trunc;
2568 static inline bool classof(const Value *V) {
2569 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2573 //===----------------------------------------------------------------------===//
2575 //===----------------------------------------------------------------------===//
2577 /// @brief This class represents zero extension of integer types.
2578 class ZExtInst : public CastInst {
2580 /// @brief Constructor with insert-before-instruction semantics
2582 Value *S, ///< The value to be zero extended
2583 const Type *Ty, ///< The type to zero extend to
2584 const Twine &NameStr = "", ///< A name for the new instruction
2585 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2588 /// @brief Constructor with insert-at-end semantics.
2590 Value *S, ///< The value to be zero extended
2591 const Type *Ty, ///< The type to zero extend to
2592 const Twine &NameStr, ///< A name for the new instruction
2593 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2596 /// @brief Clone an identical ZExtInst
2597 virtual ZExtInst *clone() const;
2599 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2600 static inline bool classof(const ZExtInst *) { return true; }
2601 static inline bool classof(const Instruction *I) {
2602 return I->getOpcode() == ZExt;
2604 static inline bool classof(const Value *V) {
2605 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2609 //===----------------------------------------------------------------------===//
2611 //===----------------------------------------------------------------------===//
2613 /// @brief This class represents a sign extension of integer types.
2614 class SExtInst : public CastInst {
2616 /// @brief Constructor with insert-before-instruction semantics
2618 Value *S, ///< The value to be sign extended
2619 const Type *Ty, ///< The type to sign extend to
2620 const Twine &NameStr = "", ///< A name for the new instruction
2621 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2624 /// @brief Constructor with insert-at-end-of-block semantics
2626 Value *S, ///< The value to be sign extended
2627 const Type *Ty, ///< The type to sign extend to
2628 const Twine &NameStr, ///< A name for the new instruction
2629 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2632 /// @brief Clone an identical SExtInst
2633 virtual SExtInst *clone() const;
2635 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2636 static inline bool classof(const SExtInst *) { return true; }
2637 static inline bool classof(const Instruction *I) {
2638 return I->getOpcode() == SExt;
2640 static inline bool classof(const Value *V) {
2641 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2645 //===----------------------------------------------------------------------===//
2646 // FPTruncInst Class
2647 //===----------------------------------------------------------------------===//
2649 /// @brief This class represents a truncation of floating point types.
2650 class FPTruncInst : public CastInst {
2652 /// @brief Constructor with insert-before-instruction semantics
2654 Value *S, ///< The value to be truncated
2655 const Type *Ty, ///< The type to truncate to
2656 const Twine &NameStr = "", ///< A name for the new instruction
2657 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2660 /// @brief Constructor with insert-before-instruction semantics
2662 Value *S, ///< The value to be truncated
2663 const Type *Ty, ///< The type to truncate to
2664 const Twine &NameStr, ///< A name for the new instruction
2665 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2668 /// @brief Clone an identical FPTruncInst
2669 virtual FPTruncInst *clone() const;
2671 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2672 static inline bool classof(const FPTruncInst *) { return true; }
2673 static inline bool classof(const Instruction *I) {
2674 return I->getOpcode() == FPTrunc;
2676 static inline bool classof(const Value *V) {
2677 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2681 //===----------------------------------------------------------------------===//
2683 //===----------------------------------------------------------------------===//
2685 /// @brief This class represents an extension of floating point types.
2686 class FPExtInst : public CastInst {
2688 /// @brief Constructor with insert-before-instruction semantics
2690 Value *S, ///< The value to be extended
2691 const Type *Ty, ///< The type to extend to
2692 const Twine &NameStr = "", ///< A name for the new instruction
2693 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2696 /// @brief Constructor with insert-at-end-of-block semantics
2698 Value *S, ///< The value to be extended
2699 const Type *Ty, ///< The type to extend to
2700 const Twine &NameStr, ///< A name for the new instruction
2701 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2704 /// @brief Clone an identical FPExtInst
2705 virtual FPExtInst *clone() const;
2707 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2708 static inline bool classof(const FPExtInst *) { return true; }
2709 static inline bool classof(const Instruction *I) {
2710 return I->getOpcode() == FPExt;
2712 static inline bool classof(const Value *V) {
2713 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2717 //===----------------------------------------------------------------------===//
2719 //===----------------------------------------------------------------------===//
2721 /// @brief This class represents a cast unsigned integer to floating point.
2722 class UIToFPInst : public CastInst {
2724 /// @brief Constructor with insert-before-instruction semantics
2726 Value *S, ///< The value to be converted
2727 const Type *Ty, ///< The type to convert to
2728 const Twine &NameStr = "", ///< A name for the new instruction
2729 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2732 /// @brief Constructor with insert-at-end-of-block semantics
2734 Value *S, ///< The value to be converted
2735 const Type *Ty, ///< The type to convert to
2736 const Twine &NameStr, ///< A name for the new instruction
2737 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2740 /// @brief Clone an identical UIToFPInst
2741 virtual UIToFPInst *clone() const;
2743 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2744 static inline bool classof(const UIToFPInst *) { return true; }
2745 static inline bool classof(const Instruction *I) {
2746 return I->getOpcode() == UIToFP;
2748 static inline bool classof(const Value *V) {
2749 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2753 //===----------------------------------------------------------------------===//
2755 //===----------------------------------------------------------------------===//
2757 /// @brief This class represents a cast from signed integer to floating point.
2758 class SIToFPInst : public CastInst {
2760 /// @brief Constructor with insert-before-instruction semantics
2762 Value *S, ///< The value to be converted
2763 const Type *Ty, ///< The type to convert to
2764 const Twine &NameStr = "", ///< A name for the new instruction
2765 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2768 /// @brief Constructor with insert-at-end-of-block semantics
2770 Value *S, ///< The value to be converted
2771 const Type *Ty, ///< The type to convert to
2772 const Twine &NameStr, ///< A name for the new instruction
2773 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2776 /// @brief Clone an identical SIToFPInst
2777 virtual SIToFPInst *clone() const;
2779 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2780 static inline bool classof(const SIToFPInst *) { return true; }
2781 static inline bool classof(const Instruction *I) {
2782 return I->getOpcode() == SIToFP;
2784 static inline bool classof(const Value *V) {
2785 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2789 //===----------------------------------------------------------------------===//
2791 //===----------------------------------------------------------------------===//
2793 /// @brief This class represents a cast from floating point to unsigned integer
2794 class FPToUIInst : public CastInst {
2796 /// @brief Constructor with insert-before-instruction semantics
2798 Value *S, ///< The value to be converted
2799 const Type *Ty, ///< The type to convert to
2800 const Twine &NameStr = "", ///< A name for the new instruction
2801 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2804 /// @brief Constructor with insert-at-end-of-block semantics
2806 Value *S, ///< The value to be converted
2807 const Type *Ty, ///< The type to convert to
2808 const Twine &NameStr, ///< A name for the new instruction
2809 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2812 /// @brief Clone an identical FPToUIInst
2813 virtual FPToUIInst *clone() const;
2815 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2816 static inline bool classof(const FPToUIInst *) { return true; }
2817 static inline bool classof(const Instruction *I) {
2818 return I->getOpcode() == FPToUI;
2820 static inline bool classof(const Value *V) {
2821 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2825 //===----------------------------------------------------------------------===//
2827 //===----------------------------------------------------------------------===//
2829 /// @brief This class represents a cast from floating point to signed integer.
2830 class FPToSIInst : public CastInst {
2832 /// @brief Constructor with insert-before-instruction semantics
2834 Value *S, ///< The value to be converted
2835 const Type *Ty, ///< The type to convert to
2836 const Twine &NameStr = "", ///< A name for the new instruction
2837 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2840 /// @brief Constructor with insert-at-end-of-block semantics
2842 Value *S, ///< The value to be converted
2843 const Type *Ty, ///< The type to convert to
2844 const Twine &NameStr, ///< A name for the new instruction
2845 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2848 /// @brief Clone an identical FPToSIInst
2849 virtual FPToSIInst *clone() const;
2851 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2852 static inline bool classof(const FPToSIInst *) { return true; }
2853 static inline bool classof(const Instruction *I) {
2854 return I->getOpcode() == FPToSI;
2856 static inline bool classof(const Value *V) {
2857 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2861 //===----------------------------------------------------------------------===//
2862 // IntToPtrInst Class
2863 //===----------------------------------------------------------------------===//
2865 /// @brief This class represents a cast from an integer to a pointer.
2866 class IntToPtrInst : public CastInst {
2868 /// @brief Constructor with insert-before-instruction semantics
2870 Value *S, ///< The value to be converted
2871 const Type *Ty, ///< The type to convert to
2872 const Twine &NameStr = "", ///< A name for the new instruction
2873 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2876 /// @brief Constructor with insert-at-end-of-block semantics
2878 Value *S, ///< The value to be converted
2879 const Type *Ty, ///< The type to convert to
2880 const Twine &NameStr, ///< A name for the new instruction
2881 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2884 /// @brief Clone an identical IntToPtrInst
2885 virtual IntToPtrInst *clone() const;
2887 // Methods for support type inquiry through isa, cast, and dyn_cast:
2888 static inline bool classof(const IntToPtrInst *) { return true; }
2889 static inline bool classof(const Instruction *I) {
2890 return I->getOpcode() == IntToPtr;
2892 static inline bool classof(const Value *V) {
2893 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2897 //===----------------------------------------------------------------------===//
2898 // PtrToIntInst Class
2899 //===----------------------------------------------------------------------===//
2901 /// @brief This class represents a cast from a pointer to an integer
2902 class PtrToIntInst : public CastInst {
2904 /// @brief Constructor with insert-before-instruction semantics
2906 Value *S, ///< The value to be converted
2907 const Type *Ty, ///< The type to convert to
2908 const Twine &NameStr = "", ///< A name for the new instruction
2909 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2912 /// @brief Constructor with insert-at-end-of-block semantics
2914 Value *S, ///< The value to be converted
2915 const Type *Ty, ///< The type to convert to
2916 const Twine &NameStr, ///< A name for the new instruction
2917 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2920 /// @brief Clone an identical PtrToIntInst
2921 virtual PtrToIntInst *clone() const;
2923 // Methods for support type inquiry through isa, cast, and dyn_cast:
2924 static inline bool classof(const PtrToIntInst *) { return true; }
2925 static inline bool classof(const Instruction *I) {
2926 return I->getOpcode() == PtrToInt;
2928 static inline bool classof(const Value *V) {
2929 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2933 //===----------------------------------------------------------------------===//
2934 // BitCastInst Class
2935 //===----------------------------------------------------------------------===//
2937 /// @brief This class represents a no-op cast from one type to another.
2938 class BitCastInst : public CastInst {
2940 /// @brief Constructor with insert-before-instruction semantics
2942 Value *S, ///< The value to be casted
2943 const Type *Ty, ///< The type to casted to
2944 const Twine &NameStr = "", ///< A name for the new instruction
2945 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2948 /// @brief Constructor with insert-at-end-of-block semantics
2950 Value *S, ///< The value to be casted
2951 const Type *Ty, ///< The type to casted to
2952 const Twine &NameStr, ///< A name for the new instruction
2953 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2956 /// @brief Clone an identical BitCastInst
2957 virtual BitCastInst *clone() const;
2959 // Methods for support type inquiry through isa, cast, and dyn_cast:
2960 static inline bool classof(const BitCastInst *) { return true; }
2961 static inline bool classof(const Instruction *I) {
2962 return I->getOpcode() == BitCast;
2964 static inline bool classof(const Value *V) {
2965 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2969 } // End llvm namespace