1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/ADT/SmallVector.h"
35 //===----------------------------------------------------------------------===//
37 //===----------------------------------------------------------------------===//
39 /// AllocaInst - an instruction to allocate memory on the stack
41 class AllocaInst : public UnaryInstruction {
43 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
44 const Twine &Name = "", Instruction *InsertBefore = 0);
45 AllocaInst(const Type *Ty, Value *ArraySize,
46 const Twine &Name, BasicBlock *InsertAtEnd);
48 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
49 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
51 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
52 const Twine &Name = "", Instruction *InsertBefore = 0);
53 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
54 const Twine &Name, BasicBlock *InsertAtEnd);
56 // Out of line virtual method, so the vtable, etc. has a home.
57 virtual ~AllocaInst();
59 /// isArrayAllocation - Return true if there is an allocation size parameter
60 /// to the allocation instruction that is not 1.
62 bool isArrayAllocation() const;
64 /// getArraySize - Get the number of elements allocated. For a simple
65 /// allocation of a single element, this will return a constant 1 value.
67 const Value *getArraySize() const { return getOperand(0); }
68 Value *getArraySize() { return getOperand(0); }
70 /// getType - Overload to return most specific pointer type
72 const PointerType *getType() const {
73 return reinterpret_cast<const PointerType*>(Instruction::getType());
76 /// getAllocatedType - Return the type that is being allocated by the
79 const Type *getAllocatedType() const;
81 /// getAlignment - Return the alignment of the memory that is being allocated
82 /// by the instruction.
84 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
85 void setAlignment(unsigned Align);
87 /// isStaticAlloca - Return true if this alloca is in the entry block of the
88 /// function and is a constant size. If so, the code generator will fold it
89 /// into the prolog/epilog code, so it is basically free.
90 bool isStaticAlloca() const;
92 virtual AllocaInst *clone() const;
94 // Methods for support type inquiry through isa, cast, and dyn_cast:
95 static inline bool classof(const AllocaInst *) { return true; }
96 static inline bool classof(const Instruction *I) {
97 return (I->getOpcode() == Instruction::Alloca);
99 static inline bool classof(const Value *V) {
100 return isa<Instruction>(V) && classof(cast<Instruction>(V));
105 //===----------------------------------------------------------------------===//
107 //===----------------------------------------------------------------------===//
109 /// LoadInst - an instruction for reading from memory. This uses the
110 /// SubclassData field in Value to store whether or not the load is volatile.
112 class LoadInst : public UnaryInstruction {
115 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
116 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
117 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
118 Instruction *InsertBefore = 0);
119 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
120 unsigned Align, Instruction *InsertBefore = 0);
121 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
122 BasicBlock *InsertAtEnd);
123 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
124 unsigned Align, BasicBlock *InsertAtEnd);
126 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
127 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
128 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
129 bool isVolatile = false, Instruction *InsertBefore = 0);
130 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
131 BasicBlock *InsertAtEnd);
133 /// isVolatile - Return true if this is a load from a volatile memory
136 bool isVolatile() const { return SubclassData & 1; }
138 /// setVolatile - Specify whether this is a volatile load or not.
140 void setVolatile(bool V) {
141 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
144 virtual LoadInst *clone() const;
146 /// getAlignment - Return the alignment of the access that is being performed
148 unsigned getAlignment() const {
149 return (1 << (SubclassData>>1)) >> 1;
152 void setAlignment(unsigned Align);
154 Value *getPointerOperand() { return getOperand(0); }
155 const Value *getPointerOperand() const { return getOperand(0); }
156 static unsigned getPointerOperandIndex() { return 0U; }
158 unsigned getPointerAddressSpace() const {
159 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
163 // Methods for support type inquiry through isa, cast, and dyn_cast:
164 static inline bool classof(const LoadInst *) { return true; }
165 static inline bool classof(const Instruction *I) {
166 return I->getOpcode() == Instruction::Load;
168 static inline bool classof(const Value *V) {
169 return isa<Instruction>(V) && classof(cast<Instruction>(V));
174 //===----------------------------------------------------------------------===//
176 //===----------------------------------------------------------------------===//
178 /// StoreInst - an instruction for storing to memory
180 class StoreInst : public Instruction {
181 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
184 // allocate space for exactly two operands
185 void *operator new(size_t s) {
186 return User::operator new(s, 2);
188 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
189 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
190 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
191 Instruction *InsertBefore = 0);
192 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
193 unsigned Align, Instruction *InsertBefore = 0);
194 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
195 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
196 unsigned Align, BasicBlock *InsertAtEnd);
199 /// isVolatile - Return true if this is a load from a volatile memory
202 bool isVolatile() const { return SubclassData & 1; }
204 /// setVolatile - Specify whether this is a volatile load or not.
206 void setVolatile(bool V) {
207 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
210 /// Transparently provide more efficient getOperand methods.
211 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
213 /// getAlignment - Return the alignment of the access that is being performed
215 unsigned getAlignment() const {
216 return (1 << (SubclassData>>1)) >> 1;
219 void setAlignment(unsigned Align);
221 virtual StoreInst *clone() const;
223 Value *getPointerOperand() { return getOperand(1); }
224 const Value *getPointerOperand() const { return getOperand(1); }
225 static unsigned getPointerOperandIndex() { return 1U; }
227 unsigned getPointerAddressSpace() const {
228 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
231 // Methods for support type inquiry through isa, cast, and dyn_cast:
232 static inline bool classof(const StoreInst *) { return true; }
233 static inline bool classof(const Instruction *I) {
234 return I->getOpcode() == Instruction::Store;
236 static inline bool classof(const Value *V) {
237 return isa<Instruction>(V) && classof(cast<Instruction>(V));
242 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
245 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
247 //===----------------------------------------------------------------------===//
248 // GetElementPtrInst Class
249 //===----------------------------------------------------------------------===//
251 // checkType - Simple wrapper function to give a better assertion failure
252 // message on bad indexes for a gep instruction.
254 static inline const Type *checkType(const Type *Ty) {
255 assert(Ty && "Invalid GetElementPtrInst indices for type!");
259 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
260 /// access elements of arrays and structs
262 class GetElementPtrInst : public Instruction {
263 GetElementPtrInst(const GetElementPtrInst &GEPI);
264 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
265 const Twine &NameStr);
266 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
268 template<typename InputIterator>
269 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
270 const Twine &NameStr,
271 // This argument ensures that we have an iterator we can
272 // do arithmetic on in constant time
273 std::random_access_iterator_tag) {
274 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
277 // This requires that the iterator points to contiguous memory.
278 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
279 // we have to build an array here
282 init(Ptr, 0, NumIdx, NameStr);
286 /// getIndexedType - Returns the type of the element that would be loaded with
287 /// a load instruction with the specified parameters.
289 /// Null is returned if the indices are invalid for the specified
292 template<typename InputIterator>
293 static const Type *getIndexedType(const Type *Ptr,
294 InputIterator IdxBegin,
295 InputIterator IdxEnd,
296 // This argument ensures that we
297 // have an iterator we can do
298 // arithmetic on in constant time
299 std::random_access_iterator_tag) {
300 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
303 // This requires that the iterator points to contiguous memory.
304 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
306 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
309 /// Constructors - Create a getelementptr instruction with a base pointer an
310 /// list of indices. The first ctor can optionally insert before an existing
311 /// instruction, the second appends the new instruction to the specified
313 template<typename InputIterator>
314 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
315 InputIterator IdxEnd,
317 const Twine &NameStr,
318 Instruction *InsertBefore);
319 template<typename InputIterator>
320 inline GetElementPtrInst(Value *Ptr,
321 InputIterator IdxBegin, InputIterator IdxEnd,
323 const Twine &NameStr, BasicBlock *InsertAtEnd);
325 /// Constructors - These two constructors are convenience methods because one
326 /// and two index getelementptr instructions are so common.
327 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
328 Instruction *InsertBefore = 0);
329 GetElementPtrInst(Value *Ptr, Value *Idx,
330 const Twine &NameStr, BasicBlock *InsertAtEnd);
332 template<typename InputIterator>
333 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
334 InputIterator IdxEnd,
335 const Twine &NameStr = "",
336 Instruction *InsertBefore = 0) {
337 typename std::iterator_traits<InputIterator>::difference_type Values =
338 1 + std::distance(IdxBegin, IdxEnd);
340 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
342 template<typename InputIterator>
343 static GetElementPtrInst *Create(Value *Ptr,
344 InputIterator IdxBegin, InputIterator IdxEnd,
345 const Twine &NameStr,
346 BasicBlock *InsertAtEnd) {
347 typename std::iterator_traits<InputIterator>::difference_type Values =
348 1 + std::distance(IdxBegin, IdxEnd);
350 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
353 /// Constructors - These two creators are convenience methods because one
354 /// index getelementptr instructions are so common.
355 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
356 const Twine &NameStr = "",
357 Instruction *InsertBefore = 0) {
358 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
360 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
361 const Twine &NameStr,
362 BasicBlock *InsertAtEnd) {
363 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
366 /// Create an "inbounds" getelementptr. See the documentation for the
367 /// "inbounds" flag in LangRef.html for details.
368 template<typename InputIterator>
369 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
370 InputIterator IdxEnd,
371 const Twine &NameStr = "",
372 Instruction *InsertBefore = 0) {
373 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
374 NameStr, InsertBefore);
375 GEP->setIsInBounds(true);
378 template<typename InputIterator>
379 static GetElementPtrInst *CreateInBounds(Value *Ptr,
380 InputIterator IdxBegin,
381 InputIterator IdxEnd,
382 const Twine &NameStr,
383 BasicBlock *InsertAtEnd) {
384 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
385 NameStr, InsertAtEnd);
386 GEP->setIsInBounds(true);
389 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
390 const Twine &NameStr = "",
391 Instruction *InsertBefore = 0) {
392 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
393 GEP->setIsInBounds(true);
396 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
397 const Twine &NameStr,
398 BasicBlock *InsertAtEnd) {
399 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
400 GEP->setIsInBounds(true);
404 virtual GetElementPtrInst *clone() const;
406 /// Transparently provide more efficient getOperand methods.
407 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
409 // getType - Overload to return most specific pointer type...
410 const PointerType *getType() const {
411 return reinterpret_cast<const PointerType*>(Instruction::getType());
414 /// getIndexedType - Returns the type of the element that would be loaded with
415 /// a load instruction with the specified parameters.
417 /// Null is returned if the indices are invalid for the specified
420 template<typename InputIterator>
421 static const Type *getIndexedType(const Type *Ptr,
422 InputIterator IdxBegin,
423 InputIterator IdxEnd) {
424 return getIndexedType(Ptr, IdxBegin, IdxEnd,
425 typename std::iterator_traits<InputIterator>::
426 iterator_category());
429 static const Type *getIndexedType(const Type *Ptr,
430 Value* const *Idx, unsigned NumIdx);
432 static const Type *getIndexedType(const Type *Ptr,
433 uint64_t const *Idx, unsigned NumIdx);
435 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
437 inline op_iterator idx_begin() { return op_begin()+1; }
438 inline const_op_iterator idx_begin() const { return op_begin()+1; }
439 inline op_iterator idx_end() { return op_end(); }
440 inline const_op_iterator idx_end() const { return op_end(); }
442 Value *getPointerOperand() {
443 return getOperand(0);
445 const Value *getPointerOperand() const {
446 return getOperand(0);
448 static unsigned getPointerOperandIndex() {
449 return 0U; // get index for modifying correct operand
452 unsigned getPointerAddressSpace() const {
453 return cast<PointerType>(getType())->getAddressSpace();
456 /// getPointerOperandType - Method to return the pointer operand as a
458 const PointerType *getPointerOperandType() const {
459 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
463 unsigned getNumIndices() const { // Note: always non-negative
464 return getNumOperands() - 1;
467 bool hasIndices() const {
468 return getNumOperands() > 1;
471 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
472 /// zeros. If so, the result pointer and the first operand have the same
473 /// value, just potentially different types.
474 bool hasAllZeroIndices() const;
476 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
477 /// constant integers. If so, the result pointer and the first operand have
478 /// a constant offset between them.
479 bool hasAllConstantIndices() const;
481 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
482 /// See LangRef.html for the meaning of inbounds on a getelementptr.
483 void setIsInBounds(bool b = true);
485 /// isInBounds - Determine whether the GEP has the inbounds flag.
486 bool isInBounds() const;
488 // Methods for support type inquiry through isa, cast, and dyn_cast:
489 static inline bool classof(const GetElementPtrInst *) { return true; }
490 static inline bool classof(const Instruction *I) {
491 return (I->getOpcode() == Instruction::GetElementPtr);
493 static inline bool classof(const Value *V) {
494 return isa<Instruction>(V) && classof(cast<Instruction>(V));
499 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
502 template<typename InputIterator>
503 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
504 InputIterator IdxBegin,
505 InputIterator IdxEnd,
507 const Twine &NameStr,
508 Instruction *InsertBefore)
509 : Instruction(PointerType::get(checkType(
510 getIndexedType(Ptr->getType(),
512 cast<PointerType>(Ptr->getType())
513 ->getAddressSpace()),
515 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
516 Values, InsertBefore) {
517 init(Ptr, IdxBegin, IdxEnd, NameStr,
518 typename std::iterator_traits<InputIterator>::iterator_category());
520 template<typename InputIterator>
521 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
522 InputIterator IdxBegin,
523 InputIterator IdxEnd,
525 const Twine &NameStr,
526 BasicBlock *InsertAtEnd)
527 : Instruction(PointerType::get(checkType(
528 getIndexedType(Ptr->getType(),
530 cast<PointerType>(Ptr->getType())
531 ->getAddressSpace()),
533 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
534 Values, InsertAtEnd) {
535 init(Ptr, IdxBegin, IdxEnd, NameStr,
536 typename std::iterator_traits<InputIterator>::iterator_category());
540 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
543 //===----------------------------------------------------------------------===//
545 //===----------------------------------------------------------------------===//
547 /// This instruction compares its operands according to the predicate given
548 /// to the constructor. It only operates on integers or pointers. The operands
549 /// must be identical types.
550 /// @brief Represent an integer comparison operator.
551 class ICmpInst: public CmpInst {
553 /// @brief Constructor with insert-before-instruction semantics.
555 Instruction *InsertBefore, ///< Where to insert
556 Predicate pred, ///< The predicate to use for the comparison
557 Value *LHS, ///< The left-hand-side of the expression
558 Value *RHS, ///< The right-hand-side of the expression
559 const Twine &NameStr = "" ///< Name of the instruction
560 ) : CmpInst(makeCmpResultType(LHS->getType()),
561 Instruction::ICmp, pred, LHS, RHS, NameStr,
563 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
564 pred <= CmpInst::LAST_ICMP_PREDICATE &&
565 "Invalid ICmp predicate value");
566 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
567 "Both operands to ICmp instruction are not of the same type!");
568 // Check that the operands are the right type
569 assert((getOperand(0)->getType()->isIntOrIntVector() ||
570 isa<PointerType>(getOperand(0)->getType())) &&
571 "Invalid operand types for ICmp instruction");
574 /// @brief Constructor with insert-at-end semantics.
576 BasicBlock &InsertAtEnd, ///< Block to insert into.
577 Predicate pred, ///< The predicate to use for the comparison
578 Value *LHS, ///< The left-hand-side of the expression
579 Value *RHS, ///< The right-hand-side of the expression
580 const Twine &NameStr = "" ///< Name of the instruction
581 ) : CmpInst(makeCmpResultType(LHS->getType()),
582 Instruction::ICmp, pred, LHS, RHS, NameStr,
584 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
585 pred <= CmpInst::LAST_ICMP_PREDICATE &&
586 "Invalid ICmp predicate value");
587 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
588 "Both operands to ICmp instruction are not of the same type!");
589 // Check that the operands are the right type
590 assert((getOperand(0)->getType()->isIntOrIntVector() ||
591 isa<PointerType>(getOperand(0)->getType())) &&
592 "Invalid operand types for ICmp instruction");
595 /// @brief Constructor with no-insertion semantics
597 Predicate pred, ///< The predicate to use for the comparison
598 Value *LHS, ///< The left-hand-side of the expression
599 Value *RHS, ///< The right-hand-side of the expression
600 const Twine &NameStr = "" ///< Name of the instruction
601 ) : CmpInst(makeCmpResultType(LHS->getType()),
602 Instruction::ICmp, pred, LHS, RHS, NameStr) {
603 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
604 pred <= CmpInst::LAST_ICMP_PREDICATE &&
605 "Invalid ICmp predicate value");
606 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
607 "Both operands to ICmp instruction are not of the same type!");
608 // Check that the operands are the right type
609 assert((getOperand(0)->getType()->isIntOrIntVector() ||
610 isa<PointerType>(getOperand(0)->getType())) &&
611 "Invalid operand types for ICmp instruction");
614 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
615 /// @returns the predicate that would be the result if the operand were
616 /// regarded as signed.
617 /// @brief Return the signed version of the predicate
618 Predicate getSignedPredicate() const {
619 return getSignedPredicate(getPredicate());
622 /// This is a static version that you can use without an instruction.
623 /// @brief Return the signed version of the predicate.
624 static Predicate getSignedPredicate(Predicate pred);
626 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
627 /// @returns the predicate that would be the result if the operand were
628 /// regarded as unsigned.
629 /// @brief Return the unsigned version of the predicate
630 Predicate getUnsignedPredicate() const {
631 return getUnsignedPredicate(getPredicate());
634 /// This is a static version that you can use without an instruction.
635 /// @brief Return the unsigned version of the predicate.
636 static Predicate getUnsignedPredicate(Predicate pred);
638 /// isEquality - Return true if this predicate is either EQ or NE. This also
639 /// tests for commutativity.
640 static bool isEquality(Predicate P) {
641 return P == ICMP_EQ || P == ICMP_NE;
644 /// isEquality - Return true if this predicate is either EQ or NE. This also
645 /// tests for commutativity.
646 bool isEquality() const {
647 return isEquality(getPredicate());
650 /// @returns true if the predicate of this ICmpInst is commutative
651 /// @brief Determine if this relation is commutative.
652 bool isCommutative() const { return isEquality(); }
654 /// isRelational - Return true if the predicate is relational (not EQ or NE).
656 bool isRelational() const {
657 return !isEquality();
660 /// isRelational - Return true if the predicate is relational (not EQ or NE).
662 static bool isRelational(Predicate P) {
663 return !isEquality(P);
666 /// Initialize a set of values that all satisfy the predicate with C.
667 /// @brief Make a ConstantRange for a relation with a constant value.
668 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
670 /// Exchange the two operands to this instruction in such a way that it does
671 /// not modify the semantics of the instruction. The predicate value may be
672 /// changed to retain the same result if the predicate is order dependent
674 /// @brief Swap operands and adjust predicate.
675 void swapOperands() {
676 SubclassData = getSwappedPredicate();
677 Op<0>().swap(Op<1>());
680 virtual ICmpInst *clone() const;
682 // Methods for support type inquiry through isa, cast, and dyn_cast:
683 static inline bool classof(const ICmpInst *) { return true; }
684 static inline bool classof(const Instruction *I) {
685 return I->getOpcode() == Instruction::ICmp;
687 static inline bool classof(const Value *V) {
688 return isa<Instruction>(V) && classof(cast<Instruction>(V));
693 //===----------------------------------------------------------------------===//
695 //===----------------------------------------------------------------------===//
697 /// This instruction compares its operands according to the predicate given
698 /// to the constructor. It only operates on floating point values or packed
699 /// vectors of floating point values. The operands must be identical types.
700 /// @brief Represents a floating point comparison operator.
701 class FCmpInst: public CmpInst {
703 /// @brief Constructor with insert-before-instruction semantics.
705 Instruction *InsertBefore, ///< Where to insert
706 Predicate pred, ///< The predicate to use for the comparison
707 Value *LHS, ///< The left-hand-side of the expression
708 Value *RHS, ///< The right-hand-side of the expression
709 const Twine &NameStr = "" ///< Name of the instruction
710 ) : CmpInst(makeCmpResultType(LHS->getType()),
711 Instruction::FCmp, pred, LHS, RHS, NameStr,
713 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
714 "Invalid FCmp predicate value");
715 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
716 "Both operands to FCmp instruction are not of the same type!");
717 // Check that the operands are the right type
718 assert(getOperand(0)->getType()->isFPOrFPVector() &&
719 "Invalid operand types for FCmp instruction");
722 /// @brief Constructor with insert-at-end semantics.
724 BasicBlock &InsertAtEnd, ///< Block to insert into.
725 Predicate pred, ///< The predicate to use for the comparison
726 Value *LHS, ///< The left-hand-side of the expression
727 Value *RHS, ///< The right-hand-side of the expression
728 const Twine &NameStr = "" ///< Name of the instruction
729 ) : CmpInst(makeCmpResultType(LHS->getType()),
730 Instruction::FCmp, pred, LHS, RHS, NameStr,
732 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
733 "Invalid FCmp predicate value");
734 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
735 "Both operands to FCmp instruction are not of the same type!");
736 // Check that the operands are the right type
737 assert(getOperand(0)->getType()->isFPOrFPVector() &&
738 "Invalid operand types for FCmp instruction");
741 /// @brief Constructor with no-insertion semantics
743 Predicate pred, ///< The predicate to use for the comparison
744 Value *LHS, ///< The left-hand-side of the expression
745 Value *RHS, ///< The right-hand-side of the expression
746 const Twine &NameStr = "" ///< Name of the instruction
747 ) : CmpInst(makeCmpResultType(LHS->getType()),
748 Instruction::FCmp, pred, LHS, RHS, NameStr) {
749 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
750 "Invalid FCmp predicate value");
751 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
752 "Both operands to FCmp instruction are not of the same type!");
753 // Check that the operands are the right type
754 assert(getOperand(0)->getType()->isFPOrFPVector() &&
755 "Invalid operand types for FCmp instruction");
758 /// @returns true if the predicate of this instruction is EQ or NE.
759 /// @brief Determine if this is an equality predicate.
760 bool isEquality() const {
761 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
762 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
765 /// @returns true if the predicate of this instruction is commutative.
766 /// @brief Determine if this is a commutative predicate.
767 bool isCommutative() const {
768 return isEquality() ||
769 SubclassData == FCMP_FALSE ||
770 SubclassData == FCMP_TRUE ||
771 SubclassData == FCMP_ORD ||
772 SubclassData == FCMP_UNO;
775 /// @returns true if the predicate is relational (not EQ or NE).
776 /// @brief Determine if this a relational predicate.
777 bool isRelational() const { return !isEquality(); }
779 /// Exchange the two operands to this instruction in such a way that it does
780 /// not modify the semantics of the instruction. The predicate value may be
781 /// changed to retain the same result if the predicate is order dependent
783 /// @brief Swap operands and adjust predicate.
784 void swapOperands() {
785 SubclassData = getSwappedPredicate();
786 Op<0>().swap(Op<1>());
789 virtual FCmpInst *clone() const;
791 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
792 static inline bool classof(const FCmpInst *) { return true; }
793 static inline bool classof(const Instruction *I) {
794 return I->getOpcode() == Instruction::FCmp;
796 static inline bool classof(const Value *V) {
797 return isa<Instruction>(V) && classof(cast<Instruction>(V));
801 //===----------------------------------------------------------------------===//
803 //===----------------------------------------------------------------------===//
804 /// CallInst - This class represents a function call, abstracting a target
805 /// machine's calling convention. This class uses low bit of the SubClassData
806 /// field to indicate whether or not this is a tail call. The rest of the bits
807 /// hold the calling convention of the call.
810 class CallInst : public Instruction {
811 AttrListPtr AttributeList; ///< parameter attributes for call
812 CallInst(const CallInst &CI);
813 void init(Value *Func, Value* const *Params, unsigned NumParams);
814 void init(Value *Func, Value *Actual1, Value *Actual2);
815 void init(Value *Func, Value *Actual);
816 void init(Value *Func);
818 template<typename InputIterator>
819 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
820 const Twine &NameStr,
821 // This argument ensures that we have an iterator we can
822 // do arithmetic on in constant time
823 std::random_access_iterator_tag) {
824 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
826 // This requires that the iterator points to contiguous memory.
827 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
831 /// Construct a CallInst given a range of arguments. InputIterator
832 /// must be a random-access iterator pointing to contiguous storage
833 /// (e.g. a std::vector<>::iterator). Checks are made for
834 /// random-accessness but not for contiguous storage as that would
835 /// incur runtime overhead.
836 /// @brief Construct a CallInst from a range of arguments
837 template<typename InputIterator>
838 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
839 const Twine &NameStr, Instruction *InsertBefore);
841 /// Construct a CallInst given a range of arguments. InputIterator
842 /// must be a random-access iterator pointing to contiguous storage
843 /// (e.g. a std::vector<>::iterator). Checks are made for
844 /// random-accessness but not for contiguous storage as that would
845 /// incur runtime overhead.
846 /// @brief Construct a CallInst from a range of arguments
847 template<typename InputIterator>
848 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
849 const Twine &NameStr, BasicBlock *InsertAtEnd);
851 CallInst(Value *F, Value *Actual, const Twine &NameStr,
852 Instruction *InsertBefore);
853 CallInst(Value *F, Value *Actual, const Twine &NameStr,
854 BasicBlock *InsertAtEnd);
855 explicit CallInst(Value *F, const Twine &NameStr,
856 Instruction *InsertBefore);
857 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
859 template<typename InputIterator>
860 static CallInst *Create(Value *Func,
861 InputIterator ArgBegin, InputIterator ArgEnd,
862 const Twine &NameStr = "",
863 Instruction *InsertBefore = 0) {
864 return new((unsigned)(ArgEnd - ArgBegin + 1))
865 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
867 template<typename InputIterator>
868 static CallInst *Create(Value *Func,
869 InputIterator ArgBegin, InputIterator ArgEnd,
870 const Twine &NameStr, BasicBlock *InsertAtEnd) {
871 return new((unsigned)(ArgEnd - ArgBegin + 1))
872 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
874 static CallInst *Create(Value *F, Value *Actual,
875 const Twine &NameStr = "",
876 Instruction *InsertBefore = 0) {
877 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
879 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
880 BasicBlock *InsertAtEnd) {
881 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
883 static CallInst *Create(Value *F, const Twine &NameStr = "",
884 Instruction *InsertBefore = 0) {
885 return new(1) CallInst(F, NameStr, InsertBefore);
887 static CallInst *Create(Value *F, const Twine &NameStr,
888 BasicBlock *InsertAtEnd) {
889 return new(1) CallInst(F, NameStr, InsertAtEnd);
891 /// CreateMalloc - Generate the IR for a call to malloc:
892 /// 1. Compute the malloc call's argument as the specified type's size,
893 /// possibly multiplied by the array size if the array size is not
895 /// 2. Call malloc with that argument.
896 /// 3. Bitcast the result of the malloc call to the specified type.
897 static Instruction *CreateMalloc(Instruction *InsertBefore,
898 const Type *IntPtrTy, const Type *AllocTy,
899 Value *ArraySize = 0,
900 const Twine &Name = "");
901 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
902 const Type *IntPtrTy, const Type *AllocTy,
903 Value *ArraySize = 0, Function* MallocF = 0,
904 const Twine &Name = "");
905 /// CreateFree - Generate the IR for a call to the builtin free function.
906 static void CreateFree(Value* Source, Instruction *InsertBefore);
907 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
911 bool isTailCall() const { return SubclassData & 1; }
912 void setTailCall(bool isTC = true) {
913 SubclassData = (SubclassData & ~1) | unsigned(isTC);
916 virtual CallInst *clone() const;
918 /// Provide fast operand accessors
919 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
921 /// getCallingConv/setCallingConv - Get or set the calling convention of this
923 CallingConv::ID getCallingConv() const {
924 return static_cast<CallingConv::ID>(SubclassData >> 1);
926 void setCallingConv(CallingConv::ID CC) {
927 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
930 /// getAttributes - Return the parameter attributes for this call.
932 const AttrListPtr &getAttributes() const { return AttributeList; }
934 /// setAttributes - Set the parameter attributes for this call.
936 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
938 /// addAttribute - adds the attribute to the list of attributes.
939 void addAttribute(unsigned i, Attributes attr);
941 /// removeAttribute - removes the attribute from the list of attributes.
942 void removeAttribute(unsigned i, Attributes attr);
944 /// @brief Determine whether the call or the callee has the given attribute.
945 bool paramHasAttr(unsigned i, Attributes attr) const;
947 /// @brief Extract the alignment for a call or parameter (0=unknown).
948 unsigned getParamAlignment(unsigned i) const {
949 return AttributeList.getParamAlignment(i);
952 /// @brief Determine if the call does not access memory.
953 bool doesNotAccessMemory() const {
954 return paramHasAttr(~0, Attribute::ReadNone);
956 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
957 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
958 else removeAttribute(~0, Attribute::ReadNone);
961 /// @brief Determine if the call does not access or only reads memory.
962 bool onlyReadsMemory() const {
963 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
965 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
966 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
967 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
970 /// @brief Determine if the call cannot return.
971 bool doesNotReturn() const {
972 return paramHasAttr(~0, Attribute::NoReturn);
974 void setDoesNotReturn(bool DoesNotReturn = true) {
975 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
976 else removeAttribute(~0, Attribute::NoReturn);
979 /// @brief Determine if the call cannot unwind.
980 bool doesNotThrow() const {
981 return paramHasAttr(~0, Attribute::NoUnwind);
983 void setDoesNotThrow(bool DoesNotThrow = true) {
984 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
985 else removeAttribute(~0, Attribute::NoUnwind);
988 /// @brief Determine if the call returns a structure through first
989 /// pointer argument.
990 bool hasStructRetAttr() const {
991 // Be friendly and also check the callee.
992 return paramHasAttr(1, Attribute::StructRet);
995 /// @brief Determine if any call argument is an aggregate passed by value.
996 bool hasByValArgument() const {
997 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1000 /// getCalledFunction - Return the function called, or null if this is an
1001 /// indirect function invocation.
1003 Function *getCalledFunction() const {
1004 return dyn_cast<Function>(Op<0>());
1007 /// getCalledValue - Get a pointer to the function that is invoked by this
1009 const Value *getCalledValue() const { return Op<0>(); }
1010 Value *getCalledValue() { return Op<0>(); }
1012 /// setCalledFunction - Set the function called.
1013 void setCalledFunction(Value* Fn) {
1017 // Methods for support type inquiry through isa, cast, and dyn_cast:
1018 static inline bool classof(const CallInst *) { return true; }
1019 static inline bool classof(const Instruction *I) {
1020 return I->getOpcode() == Instruction::Call;
1022 static inline bool classof(const Value *V) {
1023 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1028 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1031 template<typename InputIterator>
1032 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1033 const Twine &NameStr, BasicBlock *InsertAtEnd)
1034 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1035 ->getElementType())->getReturnType(),
1037 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1038 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1039 init(Func, ArgBegin, ArgEnd, NameStr,
1040 typename std::iterator_traits<InputIterator>::iterator_category());
1043 template<typename InputIterator>
1044 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1045 const Twine &NameStr, Instruction *InsertBefore)
1046 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1047 ->getElementType())->getReturnType(),
1049 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1050 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1051 init(Func, ArgBegin, ArgEnd, NameStr,
1052 typename std::iterator_traits<InputIterator>::iterator_category());
1055 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1057 //===----------------------------------------------------------------------===//
1059 //===----------------------------------------------------------------------===//
1061 /// SelectInst - This class represents the LLVM 'select' instruction.
1063 class SelectInst : public Instruction {
1064 void init(Value *C, Value *S1, Value *S2) {
1065 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1071 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1072 Instruction *InsertBefore)
1073 : Instruction(S1->getType(), Instruction::Select,
1074 &Op<0>(), 3, InsertBefore) {
1078 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1079 BasicBlock *InsertAtEnd)
1080 : Instruction(S1->getType(), Instruction::Select,
1081 &Op<0>(), 3, InsertAtEnd) {
1086 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1087 const Twine &NameStr = "",
1088 Instruction *InsertBefore = 0) {
1089 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1091 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1092 const Twine &NameStr,
1093 BasicBlock *InsertAtEnd) {
1094 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1097 const Value *getCondition() const { return Op<0>(); }
1098 const Value *getTrueValue() const { return Op<1>(); }
1099 const Value *getFalseValue() const { return Op<2>(); }
1100 Value *getCondition() { return Op<0>(); }
1101 Value *getTrueValue() { return Op<1>(); }
1102 Value *getFalseValue() { return Op<2>(); }
1104 /// areInvalidOperands - Return a string if the specified operands are invalid
1105 /// for a select operation, otherwise return null.
1106 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1108 /// Transparently provide more efficient getOperand methods.
1109 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1111 OtherOps getOpcode() const {
1112 return static_cast<OtherOps>(Instruction::getOpcode());
1115 virtual SelectInst *clone() const;
1117 // Methods for support type inquiry through isa, cast, and dyn_cast:
1118 static inline bool classof(const SelectInst *) { return true; }
1119 static inline bool classof(const Instruction *I) {
1120 return I->getOpcode() == Instruction::Select;
1122 static inline bool classof(const Value *V) {
1123 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1128 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1131 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1133 //===----------------------------------------------------------------------===//
1135 //===----------------------------------------------------------------------===//
1137 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1138 /// an argument of the specified type given a va_list and increments that list
1140 class VAArgInst : public UnaryInstruction {
1142 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1143 Instruction *InsertBefore = 0)
1144 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1147 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1148 BasicBlock *InsertAtEnd)
1149 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1153 virtual VAArgInst *clone() const;
1155 // Methods for support type inquiry through isa, cast, and dyn_cast:
1156 static inline bool classof(const VAArgInst *) { return true; }
1157 static inline bool classof(const Instruction *I) {
1158 return I->getOpcode() == VAArg;
1160 static inline bool classof(const Value *V) {
1161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1165 //===----------------------------------------------------------------------===//
1166 // ExtractElementInst Class
1167 //===----------------------------------------------------------------------===//
1169 /// ExtractElementInst - This instruction extracts a single (scalar)
1170 /// element from a VectorType value
1172 class ExtractElementInst : public Instruction {
1173 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1174 Instruction *InsertBefore = 0);
1175 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1176 BasicBlock *InsertAtEnd);
1178 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1179 const Twine &NameStr = "",
1180 Instruction *InsertBefore = 0) {
1181 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1183 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1184 const Twine &NameStr,
1185 BasicBlock *InsertAtEnd) {
1186 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1189 /// isValidOperands - Return true if an extractelement instruction can be
1190 /// formed with the specified operands.
1191 static bool isValidOperands(const Value *Vec, const Value *Idx);
1193 virtual ExtractElementInst *clone() const;
1195 Value *getVectorOperand() { return Op<0>(); }
1196 Value *getIndexOperand() { return Op<1>(); }
1197 const Value *getVectorOperand() const { return Op<0>(); }
1198 const Value *getIndexOperand() const { return Op<1>(); }
1200 const VectorType *getVectorOperandType() const {
1201 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1205 /// Transparently provide more efficient getOperand methods.
1206 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1208 // Methods for support type inquiry through isa, cast, and dyn_cast:
1209 static inline bool classof(const ExtractElementInst *) { return true; }
1210 static inline bool classof(const Instruction *I) {
1211 return I->getOpcode() == Instruction::ExtractElement;
1213 static inline bool classof(const Value *V) {
1214 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1219 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1222 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1224 //===----------------------------------------------------------------------===//
1225 // InsertElementInst Class
1226 //===----------------------------------------------------------------------===//
1228 /// InsertElementInst - This instruction inserts a single (scalar)
1229 /// element into a VectorType value
1231 class InsertElementInst : public Instruction {
1232 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1233 const Twine &NameStr = "",
1234 Instruction *InsertBefore = 0);
1235 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1236 const Twine &NameStr, BasicBlock *InsertAtEnd);
1238 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1239 const Twine &NameStr = "",
1240 Instruction *InsertBefore = 0) {
1241 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1243 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1244 const Twine &NameStr,
1245 BasicBlock *InsertAtEnd) {
1246 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1249 /// isValidOperands - Return true if an insertelement instruction can be
1250 /// formed with the specified operands.
1251 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1254 virtual InsertElementInst *clone() const;
1256 /// getType - Overload to return most specific vector type.
1258 const VectorType *getType() const {
1259 return reinterpret_cast<const VectorType*>(Instruction::getType());
1262 /// Transparently provide more efficient getOperand methods.
1263 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1265 // Methods for support type inquiry through isa, cast, and dyn_cast:
1266 static inline bool classof(const InsertElementInst *) { return true; }
1267 static inline bool classof(const Instruction *I) {
1268 return I->getOpcode() == Instruction::InsertElement;
1270 static inline bool classof(const Value *V) {
1271 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1276 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1279 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1281 //===----------------------------------------------------------------------===//
1282 // ShuffleVectorInst Class
1283 //===----------------------------------------------------------------------===//
1285 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1288 class ShuffleVectorInst : public Instruction {
1290 // allocate space for exactly three operands
1291 void *operator new(size_t s) {
1292 return User::operator new(s, 3);
1294 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1295 const Twine &NameStr = "",
1296 Instruction *InsertBefor = 0);
1297 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1298 const Twine &NameStr, BasicBlock *InsertAtEnd);
1300 /// isValidOperands - Return true if a shufflevector instruction can be
1301 /// formed with the specified operands.
1302 static bool isValidOperands(const Value *V1, const Value *V2,
1305 virtual ShuffleVectorInst *clone() const;
1307 /// getType - Overload to return most specific vector type.
1309 const VectorType *getType() const {
1310 return reinterpret_cast<const VectorType*>(Instruction::getType());
1313 /// Transparently provide more efficient getOperand methods.
1314 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1316 /// getMaskValue - Return the index from the shuffle mask for the specified
1317 /// output result. This is either -1 if the element is undef or a number less
1318 /// than 2*numelements.
1319 int getMaskValue(unsigned i) const;
1321 // Methods for support type inquiry through isa, cast, and dyn_cast:
1322 static inline bool classof(const ShuffleVectorInst *) { return true; }
1323 static inline bool classof(const Instruction *I) {
1324 return I->getOpcode() == Instruction::ShuffleVector;
1326 static inline bool classof(const Value *V) {
1327 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1332 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1335 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1337 //===----------------------------------------------------------------------===//
1338 // ExtractValueInst Class
1339 //===----------------------------------------------------------------------===//
1341 /// ExtractValueInst - This instruction extracts a struct member or array
1342 /// element value from an aggregate value.
1344 class ExtractValueInst : public UnaryInstruction {
1345 SmallVector<unsigned, 4> Indices;
1347 ExtractValueInst(const ExtractValueInst &EVI);
1348 void init(const unsigned *Idx, unsigned NumIdx,
1349 const Twine &NameStr);
1350 void init(unsigned Idx, const Twine &NameStr);
1352 template<typename InputIterator>
1353 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1354 const Twine &NameStr,
1355 // This argument ensures that we have an iterator we can
1356 // do arithmetic on in constant time
1357 std::random_access_iterator_tag) {
1358 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1360 // There's no fundamental reason why we require at least one index
1361 // (other than weirdness with &*IdxBegin being invalid; see
1362 // getelementptr's init routine for example). But there's no
1363 // present need to support it.
1364 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1366 // This requires that the iterator points to contiguous memory.
1367 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1368 // we have to build an array here
1371 /// getIndexedType - Returns the type of the element that would be extracted
1372 /// with an extractvalue instruction with the specified parameters.
1374 /// Null is returned if the indices are invalid for the specified
1377 static const Type *getIndexedType(const Type *Agg,
1378 const unsigned *Idx, unsigned NumIdx);
1380 template<typename InputIterator>
1381 static const Type *getIndexedType(const Type *Ptr,
1382 InputIterator IdxBegin,
1383 InputIterator IdxEnd,
1384 // This argument ensures that we
1385 // have an iterator we can do
1386 // arithmetic on in constant time
1387 std::random_access_iterator_tag) {
1388 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1391 // This requires that the iterator points to contiguous memory.
1392 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1394 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1397 /// Constructors - Create a extractvalue instruction with a base aggregate
1398 /// value and a list of indices. The first ctor can optionally insert before
1399 /// an existing instruction, the second appends the new instruction to the
1400 /// specified BasicBlock.
1401 template<typename InputIterator>
1402 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1403 InputIterator IdxEnd,
1404 const Twine &NameStr,
1405 Instruction *InsertBefore);
1406 template<typename InputIterator>
1407 inline ExtractValueInst(Value *Agg,
1408 InputIterator IdxBegin, InputIterator IdxEnd,
1409 const Twine &NameStr, BasicBlock *InsertAtEnd);
1411 // allocate space for exactly one operand
1412 void *operator new(size_t s) {
1413 return User::operator new(s, 1);
1417 template<typename InputIterator>
1418 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1419 InputIterator IdxEnd,
1420 const Twine &NameStr = "",
1421 Instruction *InsertBefore = 0) {
1423 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1425 template<typename InputIterator>
1426 static ExtractValueInst *Create(Value *Agg,
1427 InputIterator IdxBegin, InputIterator IdxEnd,
1428 const Twine &NameStr,
1429 BasicBlock *InsertAtEnd) {
1430 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1433 /// Constructors - These two creators are convenience methods because one
1434 /// index extractvalue instructions are much more common than those with
1436 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1437 const Twine &NameStr = "",
1438 Instruction *InsertBefore = 0) {
1439 unsigned Idxs[1] = { Idx };
1440 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1442 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1443 const Twine &NameStr,
1444 BasicBlock *InsertAtEnd) {
1445 unsigned Idxs[1] = { Idx };
1446 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1449 virtual ExtractValueInst *clone() const;
1451 /// getIndexedType - Returns the type of the element that would be extracted
1452 /// with an extractvalue instruction with the specified parameters.
1454 /// Null is returned if the indices are invalid for the specified
1457 template<typename InputIterator>
1458 static const Type *getIndexedType(const Type *Ptr,
1459 InputIterator IdxBegin,
1460 InputIterator IdxEnd) {
1461 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1462 typename std::iterator_traits<InputIterator>::
1463 iterator_category());
1465 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1467 typedef const unsigned* idx_iterator;
1468 inline idx_iterator idx_begin() const { return Indices.begin(); }
1469 inline idx_iterator idx_end() const { return Indices.end(); }
1471 Value *getAggregateOperand() {
1472 return getOperand(0);
1474 const Value *getAggregateOperand() const {
1475 return getOperand(0);
1477 static unsigned getAggregateOperandIndex() {
1478 return 0U; // get index for modifying correct operand
1481 unsigned getNumIndices() const { // Note: always non-negative
1482 return (unsigned)Indices.size();
1485 bool hasIndices() const {
1489 // Methods for support type inquiry through isa, cast, and dyn_cast:
1490 static inline bool classof(const ExtractValueInst *) { return true; }
1491 static inline bool classof(const Instruction *I) {
1492 return I->getOpcode() == Instruction::ExtractValue;
1494 static inline bool classof(const Value *V) {
1495 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1499 template<typename InputIterator>
1500 ExtractValueInst::ExtractValueInst(Value *Agg,
1501 InputIterator IdxBegin,
1502 InputIterator IdxEnd,
1503 const Twine &NameStr,
1504 Instruction *InsertBefore)
1505 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1507 ExtractValue, Agg, InsertBefore) {
1508 init(IdxBegin, IdxEnd, NameStr,
1509 typename std::iterator_traits<InputIterator>::iterator_category());
1511 template<typename InputIterator>
1512 ExtractValueInst::ExtractValueInst(Value *Agg,
1513 InputIterator IdxBegin,
1514 InputIterator IdxEnd,
1515 const Twine &NameStr,
1516 BasicBlock *InsertAtEnd)
1517 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1519 ExtractValue, Agg, InsertAtEnd) {
1520 init(IdxBegin, IdxEnd, NameStr,
1521 typename std::iterator_traits<InputIterator>::iterator_category());
1525 //===----------------------------------------------------------------------===//
1526 // InsertValueInst Class
1527 //===----------------------------------------------------------------------===//
1529 /// InsertValueInst - This instruction inserts a struct field of array element
1530 /// value into an aggregate value.
1532 class InsertValueInst : public Instruction {
1533 SmallVector<unsigned, 4> Indices;
1535 void *operator new(size_t, unsigned); // Do not implement
1536 InsertValueInst(const InsertValueInst &IVI);
1537 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1538 const Twine &NameStr);
1539 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1541 template<typename InputIterator>
1542 void init(Value *Agg, Value *Val,
1543 InputIterator IdxBegin, InputIterator IdxEnd,
1544 const Twine &NameStr,
1545 // This argument ensures that we have an iterator we can
1546 // do arithmetic on in constant time
1547 std::random_access_iterator_tag) {
1548 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1550 // There's no fundamental reason why we require at least one index
1551 // (other than weirdness with &*IdxBegin being invalid; see
1552 // getelementptr's init routine for example). But there's no
1553 // present need to support it.
1554 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1556 // This requires that the iterator points to contiguous memory.
1557 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1558 // we have to build an array here
1561 /// Constructors - Create a insertvalue instruction with a base aggregate
1562 /// value, a value to insert, and a list of indices. The first ctor can
1563 /// optionally insert before an existing instruction, the second appends
1564 /// the new instruction to the specified BasicBlock.
1565 template<typename InputIterator>
1566 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1567 InputIterator IdxEnd,
1568 const Twine &NameStr,
1569 Instruction *InsertBefore);
1570 template<typename InputIterator>
1571 inline InsertValueInst(Value *Agg, Value *Val,
1572 InputIterator IdxBegin, InputIterator IdxEnd,
1573 const Twine &NameStr, BasicBlock *InsertAtEnd);
1575 /// Constructors - These two constructors are convenience methods because one
1576 /// and two index insertvalue instructions are so common.
1577 InsertValueInst(Value *Agg, Value *Val,
1578 unsigned Idx, const Twine &NameStr = "",
1579 Instruction *InsertBefore = 0);
1580 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1581 const Twine &NameStr, BasicBlock *InsertAtEnd);
1583 // allocate space for exactly two operands
1584 void *operator new(size_t s) {
1585 return User::operator new(s, 2);
1588 template<typename InputIterator>
1589 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1590 InputIterator IdxEnd,
1591 const Twine &NameStr = "",
1592 Instruction *InsertBefore = 0) {
1593 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1594 NameStr, InsertBefore);
1596 template<typename InputIterator>
1597 static InsertValueInst *Create(Value *Agg, Value *Val,
1598 InputIterator IdxBegin, InputIterator IdxEnd,
1599 const Twine &NameStr,
1600 BasicBlock *InsertAtEnd) {
1601 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1602 NameStr, InsertAtEnd);
1605 /// Constructors - These two creators are convenience methods because one
1606 /// index insertvalue instructions are much more common than those with
1608 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1609 const Twine &NameStr = "",
1610 Instruction *InsertBefore = 0) {
1611 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1613 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1614 const Twine &NameStr,
1615 BasicBlock *InsertAtEnd) {
1616 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1619 virtual InsertValueInst *clone() const;
1621 /// Transparently provide more efficient getOperand methods.
1622 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1624 typedef const unsigned* idx_iterator;
1625 inline idx_iterator idx_begin() const { return Indices.begin(); }
1626 inline idx_iterator idx_end() const { return Indices.end(); }
1628 Value *getAggregateOperand() {
1629 return getOperand(0);
1631 const Value *getAggregateOperand() const {
1632 return getOperand(0);
1634 static unsigned getAggregateOperandIndex() {
1635 return 0U; // get index for modifying correct operand
1638 Value *getInsertedValueOperand() {
1639 return getOperand(1);
1641 const Value *getInsertedValueOperand() const {
1642 return getOperand(1);
1644 static unsigned getInsertedValueOperandIndex() {
1645 return 1U; // get index for modifying correct operand
1648 unsigned getNumIndices() const { // Note: always non-negative
1649 return (unsigned)Indices.size();
1652 bool hasIndices() const {
1656 // Methods for support type inquiry through isa, cast, and dyn_cast:
1657 static inline bool classof(const InsertValueInst *) { return true; }
1658 static inline bool classof(const Instruction *I) {
1659 return I->getOpcode() == Instruction::InsertValue;
1661 static inline bool classof(const Value *V) {
1662 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1667 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1670 template<typename InputIterator>
1671 InsertValueInst::InsertValueInst(Value *Agg,
1673 InputIterator IdxBegin,
1674 InputIterator IdxEnd,
1675 const Twine &NameStr,
1676 Instruction *InsertBefore)
1677 : Instruction(Agg->getType(), InsertValue,
1678 OperandTraits<InsertValueInst>::op_begin(this),
1680 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1681 typename std::iterator_traits<InputIterator>::iterator_category());
1683 template<typename InputIterator>
1684 InsertValueInst::InsertValueInst(Value *Agg,
1686 InputIterator IdxBegin,
1687 InputIterator IdxEnd,
1688 const Twine &NameStr,
1689 BasicBlock *InsertAtEnd)
1690 : Instruction(Agg->getType(), InsertValue,
1691 OperandTraits<InsertValueInst>::op_begin(this),
1693 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1694 typename std::iterator_traits<InputIterator>::iterator_category());
1697 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1699 //===----------------------------------------------------------------------===//
1701 //===----------------------------------------------------------------------===//
1703 // PHINode - The PHINode class is used to represent the magical mystical PHI
1704 // node, that can not exist in nature, but can be synthesized in a computer
1705 // scientist's overactive imagination.
1707 class PHINode : public Instruction {
1708 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1709 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1710 /// the number actually in use.
1711 unsigned ReservedSpace;
1712 PHINode(const PHINode &PN);
1713 // allocate space for exactly zero operands
1714 void *operator new(size_t s) {
1715 return User::operator new(s, 0);
1717 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1718 Instruction *InsertBefore = 0)
1719 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1724 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1725 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1730 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1731 Instruction *InsertBefore = 0) {
1732 return new PHINode(Ty, NameStr, InsertBefore);
1734 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1735 BasicBlock *InsertAtEnd) {
1736 return new PHINode(Ty, NameStr, InsertAtEnd);
1740 /// reserveOperandSpace - This method can be used to avoid repeated
1741 /// reallocation of PHI operand lists by reserving space for the correct
1742 /// number of operands before adding them. Unlike normal vector reserves,
1743 /// this method can also be used to trim the operand space.
1744 void reserveOperandSpace(unsigned NumValues) {
1745 resizeOperands(NumValues*2);
1748 virtual PHINode *clone() const;
1750 /// Provide fast operand accessors
1751 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1753 /// getNumIncomingValues - Return the number of incoming edges
1755 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1757 /// getIncomingValue - Return incoming value number x
1759 Value *getIncomingValue(unsigned i) const {
1760 assert(i*2 < getNumOperands() && "Invalid value number!");
1761 return getOperand(i*2);
1763 void setIncomingValue(unsigned i, Value *V) {
1764 assert(i*2 < getNumOperands() && "Invalid value number!");
1767 static unsigned getOperandNumForIncomingValue(unsigned i) {
1770 static unsigned getIncomingValueNumForOperand(unsigned i) {
1771 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1775 /// getIncomingBlock - Return incoming basic block #i.
1777 BasicBlock *getIncomingBlock(unsigned i) const {
1778 return cast<BasicBlock>(getOperand(i*2+1));
1781 /// getIncomingBlock - Return incoming basic block corresponding
1782 /// to an operand of the PHI.
1784 BasicBlock *getIncomingBlock(const Use &U) const {
1785 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1786 return cast<BasicBlock>((&U + 1)->get());
1789 /// getIncomingBlock - Return incoming basic block corresponding
1790 /// to value use iterator.
1792 template <typename U>
1793 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1794 return getIncomingBlock(I.getUse());
1798 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1799 setOperand(i*2+1, (Value*)BB);
1801 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1804 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1805 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1809 /// addIncoming - Add an incoming value to the end of the PHI list
1811 void addIncoming(Value *V, BasicBlock *BB) {
1812 assert(V && "PHI node got a null value!");
1813 assert(BB && "PHI node got a null basic block!");
1814 assert(getType() == V->getType() &&
1815 "All operands to PHI node must be the same type as the PHI node!");
1816 unsigned OpNo = NumOperands;
1817 if (OpNo+2 > ReservedSpace)
1818 resizeOperands(0); // Get more space!
1819 // Initialize some new operands.
1820 NumOperands = OpNo+2;
1821 OperandList[OpNo] = V;
1822 OperandList[OpNo+1] = (Value*)BB;
1825 /// removeIncomingValue - Remove an incoming value. This is useful if a
1826 /// predecessor basic block is deleted. The value removed is returned.
1828 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1829 /// is true), the PHI node is destroyed and any uses of it are replaced with
1830 /// dummy values. The only time there should be zero incoming values to a PHI
1831 /// node is when the block is dead, so this strategy is sound.
1833 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1835 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1836 int Idx = getBasicBlockIndex(BB);
1837 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1838 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1841 /// getBasicBlockIndex - Return the first index of the specified basic
1842 /// block in the value list for this PHI. Returns -1 if no instance.
1844 int getBasicBlockIndex(const BasicBlock *BB) const {
1845 Use *OL = OperandList;
1846 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1847 if (OL[i+1].get() == (const Value*)BB) return i/2;
1851 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1852 return getIncomingValue(getBasicBlockIndex(BB));
1855 /// hasConstantValue - If the specified PHI node always merges together the
1856 /// same value, return the value, otherwise return null.
1858 /// If the PHI has undef operands, but all the rest of the operands are
1859 /// some unique value, return that value if it can be proved that the
1860 /// value dominates the PHI. If DT is null, use a conservative check,
1861 /// otherwise use DT to test for dominance.
1863 Value *hasConstantValue(DominatorTree *DT = 0) const;
1865 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1866 static inline bool classof(const PHINode *) { return true; }
1867 static inline bool classof(const Instruction *I) {
1868 return I->getOpcode() == Instruction::PHI;
1870 static inline bool classof(const Value *V) {
1871 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1874 void resizeOperands(unsigned NumOperands);
1878 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1881 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1884 //===----------------------------------------------------------------------===//
1886 //===----------------------------------------------------------------------===//
1888 //===---------------------------------------------------------------------------
1889 /// ReturnInst - Return a value (possibly void), from a function. Execution
1890 /// does not continue in this function any longer.
1892 class ReturnInst : public TerminatorInst {
1893 ReturnInst(const ReturnInst &RI);
1896 // ReturnInst constructors:
1897 // ReturnInst() - 'ret void' instruction
1898 // ReturnInst( null) - 'ret void' instruction
1899 // ReturnInst(Value* X) - 'ret X' instruction
1900 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1901 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1902 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1903 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1905 // NOTE: If the Value* passed is of type void then the constructor behaves as
1906 // if it was passed NULL.
1907 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1908 Instruction *InsertBefore = 0);
1909 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1910 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1912 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1913 Instruction *InsertBefore = 0) {
1914 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1916 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1917 BasicBlock *InsertAtEnd) {
1918 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1920 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1921 return new(0) ReturnInst(C, InsertAtEnd);
1923 virtual ~ReturnInst();
1925 virtual ReturnInst *clone() const;
1927 /// Provide fast operand accessors
1928 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1930 /// Convenience accessor
1931 Value *getReturnValue(unsigned n = 0) const {
1932 return n < getNumOperands()
1937 unsigned getNumSuccessors() const { return 0; }
1939 // Methods for support type inquiry through isa, cast, and dyn_cast:
1940 static inline bool classof(const ReturnInst *) { return true; }
1941 static inline bool classof(const Instruction *I) {
1942 return (I->getOpcode() == Instruction::Ret);
1944 static inline bool classof(const Value *V) {
1945 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1948 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1949 virtual unsigned getNumSuccessorsV() const;
1950 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1954 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
1957 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1959 //===----------------------------------------------------------------------===//
1961 //===----------------------------------------------------------------------===//
1963 //===---------------------------------------------------------------------------
1964 /// BranchInst - Conditional or Unconditional Branch instruction.
1966 class BranchInst : public TerminatorInst {
1967 /// Ops list - Branches are strange. The operands are ordered:
1968 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
1969 /// they don't have to check for cond/uncond branchness. These are mostly
1970 /// accessed relative from op_end().
1971 BranchInst(const BranchInst &BI);
1973 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1974 // BranchInst(BB *B) - 'br B'
1975 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1976 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1977 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1978 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1979 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1980 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1981 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1982 Instruction *InsertBefore = 0);
1983 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1984 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1985 BasicBlock *InsertAtEnd);
1987 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1988 return new(1, true) BranchInst(IfTrue, InsertBefore);
1990 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1991 Value *Cond, Instruction *InsertBefore = 0) {
1992 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
1994 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
1995 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
1997 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1998 Value *Cond, BasicBlock *InsertAtEnd) {
1999 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2004 /// Transparently provide more efficient getOperand methods.
2005 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2007 virtual BranchInst *clone() const;
2009 bool isUnconditional() const { return getNumOperands() == 1; }
2010 bool isConditional() const { return getNumOperands() == 3; }
2012 Value *getCondition() const {
2013 assert(isConditional() && "Cannot get condition of an uncond branch!");
2017 void setCondition(Value *V) {
2018 assert(isConditional() && "Cannot set condition of unconditional branch!");
2022 // setUnconditionalDest - Change the current branch to an unconditional branch
2023 // targeting the specified block.
2024 // FIXME: Eliminate this ugly method.
2025 void setUnconditionalDest(BasicBlock *Dest) {
2026 Op<-1>() = (Value*)Dest;
2027 if (isConditional()) { // Convert this to an uncond branch.
2031 OperandList = op_begin();
2035 unsigned getNumSuccessors() const { return 1+isConditional(); }
2037 BasicBlock *getSuccessor(unsigned i) const {
2038 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2039 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2042 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2043 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2044 *(&Op<-1>() - idx) = (Value*)NewSucc;
2047 // Methods for support type inquiry through isa, cast, and dyn_cast:
2048 static inline bool classof(const BranchInst *) { return true; }
2049 static inline bool classof(const Instruction *I) {
2050 return (I->getOpcode() == Instruction::Br);
2052 static inline bool classof(const Value *V) {
2053 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2056 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2057 virtual unsigned getNumSuccessorsV() const;
2058 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2062 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2064 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2066 //===----------------------------------------------------------------------===//
2068 //===----------------------------------------------------------------------===//
2070 //===---------------------------------------------------------------------------
2071 /// SwitchInst - Multiway switch
2073 class SwitchInst : public TerminatorInst {
2074 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2075 unsigned ReservedSpace;
2076 // Operand[0] = Value to switch on
2077 // Operand[1] = Default basic block destination
2078 // Operand[2n ] = Value to match
2079 // Operand[2n+1] = BasicBlock to go to on match
2080 SwitchInst(const SwitchInst &RI);
2081 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2082 void resizeOperands(unsigned No);
2083 // allocate space for exactly zero operands
2084 void *operator new(size_t s) {
2085 return User::operator new(s, 0);
2087 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2088 /// switch on and a default destination. The number of additional cases can
2089 /// be specified here to make memory allocation more efficient. This
2090 /// constructor can also autoinsert before another instruction.
2091 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2092 Instruction *InsertBefore = 0);
2094 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2095 /// switch on and a default destination. The number of additional cases can
2096 /// be specified here to make memory allocation more efficient. This
2097 /// constructor also autoinserts at the end of the specified BasicBlock.
2098 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2099 BasicBlock *InsertAtEnd);
2101 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2102 unsigned NumCases, Instruction *InsertBefore = 0) {
2103 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2105 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2106 unsigned NumCases, BasicBlock *InsertAtEnd) {
2107 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2111 /// Provide fast operand accessors
2112 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2114 // Accessor Methods for Switch stmt
2115 Value *getCondition() const { return getOperand(0); }
2116 void setCondition(Value *V) { setOperand(0, V); }
2118 BasicBlock *getDefaultDest() const {
2119 return cast<BasicBlock>(getOperand(1));
2122 /// getNumCases - return the number of 'cases' in this switch instruction.
2123 /// Note that case #0 is always the default case.
2124 unsigned getNumCases() const {
2125 return getNumOperands()/2;
2128 /// getCaseValue - Return the specified case value. Note that case #0, the
2129 /// default destination, does not have a case value.
2130 ConstantInt *getCaseValue(unsigned i) {
2131 assert(i && i < getNumCases() && "Illegal case value to get!");
2132 return getSuccessorValue(i);
2135 /// getCaseValue - Return the specified case value. Note that case #0, the
2136 /// default destination, does not have a case value.
2137 const ConstantInt *getCaseValue(unsigned i) const {
2138 assert(i && i < getNumCases() && "Illegal case value to get!");
2139 return getSuccessorValue(i);
2142 /// findCaseValue - Search all of the case values for the specified constant.
2143 /// If it is explicitly handled, return the case number of it, otherwise
2144 /// return 0 to indicate that it is handled by the default handler.
2145 unsigned findCaseValue(const ConstantInt *C) const {
2146 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2147 if (getCaseValue(i) == C)
2152 /// findCaseDest - Finds the unique case value for a given successor. Returns
2153 /// null if the successor is not found, not unique, or is the default case.
2154 ConstantInt *findCaseDest(BasicBlock *BB) {
2155 if (BB == getDefaultDest()) return NULL;
2157 ConstantInt *CI = NULL;
2158 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2159 if (getSuccessor(i) == BB) {
2160 if (CI) return NULL; // Multiple cases lead to BB.
2161 else CI = getCaseValue(i);
2167 /// addCase - Add an entry to the switch instruction...
2169 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2171 /// removeCase - This method removes the specified successor from the switch
2172 /// instruction. Note that this cannot be used to remove the default
2173 /// destination (successor #0).
2175 void removeCase(unsigned idx);
2177 virtual SwitchInst *clone() const;
2179 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2180 BasicBlock *getSuccessor(unsigned idx) const {
2181 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2182 return cast<BasicBlock>(getOperand(idx*2+1));
2184 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2185 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2186 setOperand(idx*2+1, (Value*)NewSucc);
2189 // getSuccessorValue - Return the value associated with the specified
2191 ConstantInt *getSuccessorValue(unsigned idx) const {
2192 assert(idx < getNumSuccessors() && "Successor # out of range!");
2193 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2196 // Methods for support type inquiry through isa, cast, and dyn_cast:
2197 static inline bool classof(const SwitchInst *) { return true; }
2198 static inline bool classof(const Instruction *I) {
2199 return I->getOpcode() == Instruction::Switch;
2201 static inline bool classof(const Value *V) {
2202 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2205 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2206 virtual unsigned getNumSuccessorsV() const;
2207 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2211 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2214 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2217 //===----------------------------------------------------------------------===//
2219 //===----------------------------------------------------------------------===//
2221 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2222 /// calling convention of the call.
2224 class InvokeInst : public TerminatorInst {
2225 AttrListPtr AttributeList;
2226 InvokeInst(const InvokeInst &BI);
2227 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2228 Value* const *Args, unsigned NumArgs);
2230 template<typename InputIterator>
2231 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2232 InputIterator ArgBegin, InputIterator ArgEnd,
2233 const Twine &NameStr,
2234 // This argument ensures that we have an iterator we can
2235 // do arithmetic on in constant time
2236 std::random_access_iterator_tag) {
2237 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2239 // This requires that the iterator points to contiguous memory.
2240 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2244 /// Construct an InvokeInst given a range of arguments.
2245 /// InputIterator must be a random-access iterator pointing to
2246 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2247 /// made for random-accessness but not for contiguous storage as
2248 /// that would incur runtime overhead.
2250 /// @brief Construct an InvokeInst from a range of arguments
2251 template<typename InputIterator>
2252 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2253 InputIterator ArgBegin, InputIterator ArgEnd,
2255 const Twine &NameStr, Instruction *InsertBefore);
2257 /// Construct an InvokeInst given a range of arguments.
2258 /// InputIterator must be a random-access iterator pointing to
2259 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2260 /// made for random-accessness but not for contiguous storage as
2261 /// that would incur runtime overhead.
2263 /// @brief Construct an InvokeInst from a range of arguments
2264 template<typename InputIterator>
2265 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2266 InputIterator ArgBegin, InputIterator ArgEnd,
2268 const Twine &NameStr, BasicBlock *InsertAtEnd);
2270 template<typename InputIterator>
2271 static InvokeInst *Create(Value *Func,
2272 BasicBlock *IfNormal, BasicBlock *IfException,
2273 InputIterator ArgBegin, InputIterator ArgEnd,
2274 const Twine &NameStr = "",
2275 Instruction *InsertBefore = 0) {
2276 unsigned Values(ArgEnd - ArgBegin + 3);
2277 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2278 Values, NameStr, InsertBefore);
2280 template<typename InputIterator>
2281 static InvokeInst *Create(Value *Func,
2282 BasicBlock *IfNormal, BasicBlock *IfException,
2283 InputIterator ArgBegin, InputIterator ArgEnd,
2284 const Twine &NameStr,
2285 BasicBlock *InsertAtEnd) {
2286 unsigned Values(ArgEnd - ArgBegin + 3);
2287 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2288 Values, NameStr, InsertAtEnd);
2291 virtual InvokeInst *clone() const;
2293 /// Provide fast operand accessors
2294 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2296 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2298 CallingConv::ID getCallingConv() const {
2299 return static_cast<CallingConv::ID>(SubclassData);
2301 void setCallingConv(CallingConv::ID CC) {
2302 SubclassData = static_cast<unsigned>(CC);
2305 /// getAttributes - Return the parameter attributes for this invoke.
2307 const AttrListPtr &getAttributes() const { return AttributeList; }
2309 /// setAttributes - Set the parameter attributes for this invoke.
2311 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2313 /// addAttribute - adds the attribute to the list of attributes.
2314 void addAttribute(unsigned i, Attributes attr);
2316 /// removeAttribute - removes the attribute from the list of attributes.
2317 void removeAttribute(unsigned i, Attributes attr);
2319 /// @brief Determine whether the call or the callee has the given attribute.
2320 bool paramHasAttr(unsigned i, Attributes attr) const;
2322 /// @brief Extract the alignment for a call or parameter (0=unknown).
2323 unsigned getParamAlignment(unsigned i) const {
2324 return AttributeList.getParamAlignment(i);
2327 /// @brief Determine if the call does not access memory.
2328 bool doesNotAccessMemory() const {
2329 return paramHasAttr(~0, Attribute::ReadNone);
2331 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2332 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2333 else removeAttribute(~0, Attribute::ReadNone);
2336 /// @brief Determine if the call does not access or only reads memory.
2337 bool onlyReadsMemory() const {
2338 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2340 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2341 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2342 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2345 /// @brief Determine if the call cannot return.
2346 bool doesNotReturn() const {
2347 return paramHasAttr(~0, Attribute::NoReturn);
2349 void setDoesNotReturn(bool DoesNotReturn = true) {
2350 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2351 else removeAttribute(~0, Attribute::NoReturn);
2354 /// @brief Determine if the call cannot unwind.
2355 bool doesNotThrow() const {
2356 return paramHasAttr(~0, Attribute::NoUnwind);
2358 void setDoesNotThrow(bool DoesNotThrow = true) {
2359 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2360 else removeAttribute(~0, Attribute::NoUnwind);
2363 /// @brief Determine if the call returns a structure through first
2364 /// pointer argument.
2365 bool hasStructRetAttr() const {
2366 // Be friendly and also check the callee.
2367 return paramHasAttr(1, Attribute::StructRet);
2370 /// @brief Determine if any call argument is an aggregate passed by value.
2371 bool hasByValArgument() const {
2372 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2375 /// getCalledFunction - Return the function called, or null if this is an
2376 /// indirect function invocation.
2378 Function *getCalledFunction() const {
2379 return dyn_cast<Function>(getOperand(0));
2382 /// getCalledValue - Get a pointer to the function that is invoked by this
2384 const Value *getCalledValue() const { return getOperand(0); }
2385 Value *getCalledValue() { return getOperand(0); }
2387 // get*Dest - Return the destination basic blocks...
2388 BasicBlock *getNormalDest() const {
2389 return cast<BasicBlock>(getOperand(1));
2391 BasicBlock *getUnwindDest() const {
2392 return cast<BasicBlock>(getOperand(2));
2394 void setNormalDest(BasicBlock *B) {
2395 setOperand(1, (Value*)B);
2398 void setUnwindDest(BasicBlock *B) {
2399 setOperand(2, (Value*)B);
2402 BasicBlock *getSuccessor(unsigned i) const {
2403 assert(i < 2 && "Successor # out of range for invoke!");
2404 return i == 0 ? getNormalDest() : getUnwindDest();
2407 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2408 assert(idx < 2 && "Successor # out of range for invoke!");
2409 setOperand(idx+1, (Value*)NewSucc);
2412 unsigned getNumSuccessors() const { return 2; }
2414 // Methods for support type inquiry through isa, cast, and dyn_cast:
2415 static inline bool classof(const InvokeInst *) { return true; }
2416 static inline bool classof(const Instruction *I) {
2417 return (I->getOpcode() == Instruction::Invoke);
2419 static inline bool classof(const Value *V) {
2420 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2423 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2424 virtual unsigned getNumSuccessorsV() const;
2425 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2429 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2432 template<typename InputIterator>
2433 InvokeInst::InvokeInst(Value *Func,
2434 BasicBlock *IfNormal, BasicBlock *IfException,
2435 InputIterator ArgBegin, InputIterator ArgEnd,
2437 const Twine &NameStr, Instruction *InsertBefore)
2438 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2439 ->getElementType())->getReturnType(),
2440 Instruction::Invoke,
2441 OperandTraits<InvokeInst>::op_end(this) - Values,
2442 Values, InsertBefore) {
2443 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2444 typename std::iterator_traits<InputIterator>::iterator_category());
2446 template<typename InputIterator>
2447 InvokeInst::InvokeInst(Value *Func,
2448 BasicBlock *IfNormal, BasicBlock *IfException,
2449 InputIterator ArgBegin, InputIterator ArgEnd,
2451 const Twine &NameStr, BasicBlock *InsertAtEnd)
2452 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2453 ->getElementType())->getReturnType(),
2454 Instruction::Invoke,
2455 OperandTraits<InvokeInst>::op_end(this) - Values,
2456 Values, InsertAtEnd) {
2457 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2458 typename std::iterator_traits<InputIterator>::iterator_category());
2461 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2463 //===----------------------------------------------------------------------===//
2465 //===----------------------------------------------------------------------===//
2467 //===---------------------------------------------------------------------------
2468 /// UnwindInst - Immediately exit the current function, unwinding the stack
2469 /// until an invoke instruction is found.
2471 class UnwindInst : public TerminatorInst {
2472 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2474 // allocate space for exactly zero operands
2475 void *operator new(size_t s) {
2476 return User::operator new(s, 0);
2478 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2479 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2481 virtual UnwindInst *clone() const;
2483 unsigned getNumSuccessors() const { return 0; }
2485 // Methods for support type inquiry through isa, cast, and dyn_cast:
2486 static inline bool classof(const UnwindInst *) { return true; }
2487 static inline bool classof(const Instruction *I) {
2488 return I->getOpcode() == Instruction::Unwind;
2490 static inline bool classof(const Value *V) {
2491 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2494 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2495 virtual unsigned getNumSuccessorsV() const;
2496 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2499 //===----------------------------------------------------------------------===//
2500 // UnreachableInst Class
2501 //===----------------------------------------------------------------------===//
2503 //===---------------------------------------------------------------------------
2504 /// UnreachableInst - This function has undefined behavior. In particular, the
2505 /// presence of this instruction indicates some higher level knowledge that the
2506 /// end of the block cannot be reached.
2508 class UnreachableInst : public TerminatorInst {
2509 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2511 // allocate space for exactly zero operands
2512 void *operator new(size_t s) {
2513 return User::operator new(s, 0);
2515 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2516 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2518 virtual UnreachableInst *clone() const;
2520 unsigned getNumSuccessors() const { return 0; }
2522 // Methods for support type inquiry through isa, cast, and dyn_cast:
2523 static inline bool classof(const UnreachableInst *) { return true; }
2524 static inline bool classof(const Instruction *I) {
2525 return I->getOpcode() == Instruction::Unreachable;
2527 static inline bool classof(const Value *V) {
2528 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2531 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2532 virtual unsigned getNumSuccessorsV() const;
2533 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2536 //===----------------------------------------------------------------------===//
2538 //===----------------------------------------------------------------------===//
2540 /// @brief This class represents a truncation of integer types.
2541 class TruncInst : public CastInst {
2543 /// @brief Constructor with insert-before-instruction semantics
2545 Value *S, ///< The value to be truncated
2546 const Type *Ty, ///< The (smaller) type to truncate to
2547 const Twine &NameStr = "", ///< A name for the new instruction
2548 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2551 /// @brief Constructor with insert-at-end-of-block semantics
2553 Value *S, ///< The value to be truncated
2554 const Type *Ty, ///< The (smaller) type to truncate to
2555 const Twine &NameStr, ///< A name for the new instruction
2556 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2559 /// @brief Clone an identical TruncInst
2560 virtual TruncInst *clone() const;
2562 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2563 static inline bool classof(const TruncInst *) { return true; }
2564 static inline bool classof(const Instruction *I) {
2565 return I->getOpcode() == Trunc;
2567 static inline bool classof(const Value *V) {
2568 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2572 //===----------------------------------------------------------------------===//
2574 //===----------------------------------------------------------------------===//
2576 /// @brief This class represents zero extension of integer types.
2577 class ZExtInst : public CastInst {
2579 /// @brief Constructor with insert-before-instruction semantics
2581 Value *S, ///< The value to be zero extended
2582 const Type *Ty, ///< The type to zero extend to
2583 const Twine &NameStr = "", ///< A name for the new instruction
2584 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2587 /// @brief Constructor with insert-at-end semantics.
2589 Value *S, ///< The value to be zero extended
2590 const Type *Ty, ///< The type to zero extend to
2591 const Twine &NameStr, ///< A name for the new instruction
2592 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2595 /// @brief Clone an identical ZExtInst
2596 virtual ZExtInst *clone() const;
2598 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2599 static inline bool classof(const ZExtInst *) { return true; }
2600 static inline bool classof(const Instruction *I) {
2601 return I->getOpcode() == ZExt;
2603 static inline bool classof(const Value *V) {
2604 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2608 //===----------------------------------------------------------------------===//
2610 //===----------------------------------------------------------------------===//
2612 /// @brief This class represents a sign extension of integer types.
2613 class SExtInst : public CastInst {
2615 /// @brief Constructor with insert-before-instruction semantics
2617 Value *S, ///< The value to be sign extended
2618 const Type *Ty, ///< The type to sign extend to
2619 const Twine &NameStr = "", ///< A name for the new instruction
2620 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2623 /// @brief Constructor with insert-at-end-of-block semantics
2625 Value *S, ///< The value to be sign extended
2626 const Type *Ty, ///< The type to sign extend to
2627 const Twine &NameStr, ///< A name for the new instruction
2628 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2631 /// @brief Clone an identical SExtInst
2632 virtual SExtInst *clone() const;
2634 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2635 static inline bool classof(const SExtInst *) { return true; }
2636 static inline bool classof(const Instruction *I) {
2637 return I->getOpcode() == SExt;
2639 static inline bool classof(const Value *V) {
2640 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2644 //===----------------------------------------------------------------------===//
2645 // FPTruncInst Class
2646 //===----------------------------------------------------------------------===//
2648 /// @brief This class represents a truncation of floating point types.
2649 class FPTruncInst : public CastInst {
2651 /// @brief Constructor with insert-before-instruction semantics
2653 Value *S, ///< The value to be truncated
2654 const Type *Ty, ///< The type to truncate to
2655 const Twine &NameStr = "", ///< A name for the new instruction
2656 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2659 /// @brief Constructor with insert-before-instruction semantics
2661 Value *S, ///< The value to be truncated
2662 const Type *Ty, ///< The type to truncate to
2663 const Twine &NameStr, ///< A name for the new instruction
2664 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2667 /// @brief Clone an identical FPTruncInst
2668 virtual FPTruncInst *clone() const;
2670 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2671 static inline bool classof(const FPTruncInst *) { return true; }
2672 static inline bool classof(const Instruction *I) {
2673 return I->getOpcode() == FPTrunc;
2675 static inline bool classof(const Value *V) {
2676 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2680 //===----------------------------------------------------------------------===//
2682 //===----------------------------------------------------------------------===//
2684 /// @brief This class represents an extension of floating point types.
2685 class FPExtInst : public CastInst {
2687 /// @brief Constructor with insert-before-instruction semantics
2689 Value *S, ///< The value to be extended
2690 const Type *Ty, ///< The type to extend to
2691 const Twine &NameStr = "", ///< A name for the new instruction
2692 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2695 /// @brief Constructor with insert-at-end-of-block semantics
2697 Value *S, ///< The value to be extended
2698 const Type *Ty, ///< The type to extend to
2699 const Twine &NameStr, ///< A name for the new instruction
2700 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2703 /// @brief Clone an identical FPExtInst
2704 virtual FPExtInst *clone() const;
2706 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2707 static inline bool classof(const FPExtInst *) { return true; }
2708 static inline bool classof(const Instruction *I) {
2709 return I->getOpcode() == FPExt;
2711 static inline bool classof(const Value *V) {
2712 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2716 //===----------------------------------------------------------------------===//
2718 //===----------------------------------------------------------------------===//
2720 /// @brief This class represents a cast unsigned integer to floating point.
2721 class UIToFPInst : public CastInst {
2723 /// @brief Constructor with insert-before-instruction semantics
2725 Value *S, ///< The value to be converted
2726 const Type *Ty, ///< The type to convert to
2727 const Twine &NameStr = "", ///< A name for the new instruction
2728 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2731 /// @brief Constructor with insert-at-end-of-block semantics
2733 Value *S, ///< The value to be converted
2734 const Type *Ty, ///< The type to convert to
2735 const Twine &NameStr, ///< A name for the new instruction
2736 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2739 /// @brief Clone an identical UIToFPInst
2740 virtual UIToFPInst *clone() const;
2742 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2743 static inline bool classof(const UIToFPInst *) { return true; }
2744 static inline bool classof(const Instruction *I) {
2745 return I->getOpcode() == UIToFP;
2747 static inline bool classof(const Value *V) {
2748 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2752 //===----------------------------------------------------------------------===//
2754 //===----------------------------------------------------------------------===//
2756 /// @brief This class represents a cast from signed integer to floating point.
2757 class SIToFPInst : public CastInst {
2759 /// @brief Constructor with insert-before-instruction semantics
2761 Value *S, ///< The value to be converted
2762 const Type *Ty, ///< The type to convert to
2763 const Twine &NameStr = "", ///< A name for the new instruction
2764 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2767 /// @brief Constructor with insert-at-end-of-block semantics
2769 Value *S, ///< The value to be converted
2770 const Type *Ty, ///< The type to convert to
2771 const Twine &NameStr, ///< A name for the new instruction
2772 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2775 /// @brief Clone an identical SIToFPInst
2776 virtual SIToFPInst *clone() const;
2778 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2779 static inline bool classof(const SIToFPInst *) { return true; }
2780 static inline bool classof(const Instruction *I) {
2781 return I->getOpcode() == SIToFP;
2783 static inline bool classof(const Value *V) {
2784 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2788 //===----------------------------------------------------------------------===//
2790 //===----------------------------------------------------------------------===//
2792 /// @brief This class represents a cast from floating point to unsigned integer
2793 class FPToUIInst : public CastInst {
2795 /// @brief Constructor with insert-before-instruction semantics
2797 Value *S, ///< The value to be converted
2798 const Type *Ty, ///< The type to convert to
2799 const Twine &NameStr = "", ///< A name for the new instruction
2800 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2803 /// @brief Constructor with insert-at-end-of-block semantics
2805 Value *S, ///< The value to be converted
2806 const Type *Ty, ///< The type to convert to
2807 const Twine &NameStr, ///< A name for the new instruction
2808 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2811 /// @brief Clone an identical FPToUIInst
2812 virtual FPToUIInst *clone() const;
2814 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2815 static inline bool classof(const FPToUIInst *) { return true; }
2816 static inline bool classof(const Instruction *I) {
2817 return I->getOpcode() == FPToUI;
2819 static inline bool classof(const Value *V) {
2820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2824 //===----------------------------------------------------------------------===//
2826 //===----------------------------------------------------------------------===//
2828 /// @brief This class represents a cast from floating point to signed integer.
2829 class FPToSIInst : public CastInst {
2831 /// @brief Constructor with insert-before-instruction semantics
2833 Value *S, ///< The value to be converted
2834 const Type *Ty, ///< The type to convert to
2835 const Twine &NameStr = "", ///< A name for the new instruction
2836 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2839 /// @brief Constructor with insert-at-end-of-block semantics
2841 Value *S, ///< The value to be converted
2842 const Type *Ty, ///< The type to convert to
2843 const Twine &NameStr, ///< A name for the new instruction
2844 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2847 /// @brief Clone an identical FPToSIInst
2848 virtual FPToSIInst *clone() const;
2850 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2851 static inline bool classof(const FPToSIInst *) { return true; }
2852 static inline bool classof(const Instruction *I) {
2853 return I->getOpcode() == FPToSI;
2855 static inline bool classof(const Value *V) {
2856 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2860 //===----------------------------------------------------------------------===//
2861 // IntToPtrInst Class
2862 //===----------------------------------------------------------------------===//
2864 /// @brief This class represents a cast from an integer to a pointer.
2865 class IntToPtrInst : public CastInst {
2867 /// @brief Constructor with insert-before-instruction semantics
2869 Value *S, ///< The value to be converted
2870 const Type *Ty, ///< The type to convert to
2871 const Twine &NameStr = "", ///< A name for the new instruction
2872 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2875 /// @brief Constructor with insert-at-end-of-block semantics
2877 Value *S, ///< The value to be converted
2878 const Type *Ty, ///< The type to convert to
2879 const Twine &NameStr, ///< A name for the new instruction
2880 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2883 /// @brief Clone an identical IntToPtrInst
2884 virtual IntToPtrInst *clone() const;
2886 // Methods for support type inquiry through isa, cast, and dyn_cast:
2887 static inline bool classof(const IntToPtrInst *) { return true; }
2888 static inline bool classof(const Instruction *I) {
2889 return I->getOpcode() == IntToPtr;
2891 static inline bool classof(const Value *V) {
2892 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2896 //===----------------------------------------------------------------------===//
2897 // PtrToIntInst Class
2898 //===----------------------------------------------------------------------===//
2900 /// @brief This class represents a cast from a pointer to an integer
2901 class PtrToIntInst : public CastInst {
2903 /// @brief Constructor with insert-before-instruction semantics
2905 Value *S, ///< The value to be converted
2906 const Type *Ty, ///< The type to convert to
2907 const Twine &NameStr = "", ///< A name for the new instruction
2908 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2911 /// @brief Constructor with insert-at-end-of-block semantics
2913 Value *S, ///< The value to be converted
2914 const Type *Ty, ///< The type to convert to
2915 const Twine &NameStr, ///< A name for the new instruction
2916 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2919 /// @brief Clone an identical PtrToIntInst
2920 virtual PtrToIntInst *clone() const;
2922 // Methods for support type inquiry through isa, cast, and dyn_cast:
2923 static inline bool classof(const PtrToIntInst *) { return true; }
2924 static inline bool classof(const Instruction *I) {
2925 return I->getOpcode() == PtrToInt;
2927 static inline bool classof(const Value *V) {
2928 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2932 //===----------------------------------------------------------------------===//
2933 // BitCastInst Class
2934 //===----------------------------------------------------------------------===//
2936 /// @brief This class represents a no-op cast from one type to another.
2937 class BitCastInst : public CastInst {
2939 /// @brief Constructor with insert-before-instruction semantics
2941 Value *S, ///< The value to be casted
2942 const Type *Ty, ///< The type to casted to
2943 const Twine &NameStr = "", ///< A name for the new instruction
2944 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2947 /// @brief Constructor with insert-at-end-of-block semantics
2949 Value *S, ///< The value to be casted
2950 const Type *Ty, ///< The type to casted to
2951 const Twine &NameStr, ///< A name for the new instruction
2952 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2955 /// @brief Clone an identical BitCastInst
2956 virtual BitCastInst *clone() const;
2958 // Methods for support type inquiry through isa, cast, and dyn_cast:
2959 static inline bool classof(const BitCastInst *) { return true; }
2960 static inline bool classof(const Instruction *I) {
2961 return I->getOpcode() == BitCast;
2963 static inline bool classof(const Value *V) {
2964 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2968 } // End llvm namespace