1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/BasicBlock.h"
23 #include "llvm/CallingConv.h"
24 #include "llvm/LLVMContext.h"
25 #include "llvm/ADT/SmallVector.h"
36 //===----------------------------------------------------------------------===//
37 // AllocationInst Class
38 //===----------------------------------------------------------------------===//
40 /// AllocationInst - This class is the base class of AllocaInst.
42 class AllocationInst : public UnaryInstruction {
44 AllocationInst(const Type *Ty, Value *ArraySize,
45 unsigned iTy, unsigned Align, const Twine &Name = "",
46 Instruction *InsertBefore = 0);
47 AllocationInst(const Type *Ty, Value *ArraySize,
48 unsigned iTy, unsigned Align, const Twine &Name,
49 BasicBlock *InsertAtEnd);
51 // Out of line virtual method, so the vtable, etc. has a home.
52 virtual ~AllocationInst();
54 /// isArrayAllocation - Return true if there is an allocation size parameter
55 /// to the allocation instruction that is not 1.
57 bool isArrayAllocation() const;
59 /// getArraySize - Get the number of elements allocated. For a simple
60 /// allocation of a single element, this will return a constant 1 value.
62 const Value *getArraySize() const { return getOperand(0); }
63 Value *getArraySize() { return getOperand(0); }
65 /// getType - Overload to return most specific pointer type
67 const PointerType *getType() const {
68 return reinterpret_cast<const PointerType*>(Instruction::getType());
71 /// getAllocatedType - Return the type that is being allocated by the
74 const Type *getAllocatedType() const;
76 /// getAlignment - Return the alignment of the memory that is being allocated
77 /// by the instruction.
79 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
80 void setAlignment(unsigned Align);
82 virtual AllocationInst *clone() const = 0;
84 // Methods for support type inquiry through isa, cast, and dyn_cast:
85 static inline bool classof(const AllocationInst *) { return true; }
86 static inline bool classof(const Instruction *I) {
87 return I->getOpcode() == Instruction::Alloca;
89 static inline bool classof(const Value *V) {
90 return isa<Instruction>(V) && classof(cast<Instruction>(V));
95 //===----------------------------------------------------------------------===//
97 //===----------------------------------------------------------------------===//
99 /// AllocaInst - an instruction to allocate memory on the stack
101 class AllocaInst : public AllocationInst {
103 explicit AllocaInst(const Type *Ty,
104 Value *ArraySize = 0,
105 const Twine &NameStr = "",
106 Instruction *InsertBefore = 0)
107 : AllocationInst(Ty, ArraySize, Alloca,
108 0, NameStr, InsertBefore) {}
109 AllocaInst(const Type *Ty,
110 Value *ArraySize, const Twine &NameStr,
111 BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
114 AllocaInst(const Type *Ty, const Twine &NameStr,
115 Instruction *InsertBefore = 0)
116 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
117 AllocaInst(const Type *Ty, const Twine &NameStr,
118 BasicBlock *InsertAtEnd)
119 : AllocationInst(Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
121 AllocaInst(const Type *Ty, Value *ArraySize,
122 unsigned Align, const Twine &NameStr = "",
123 Instruction *InsertBefore = 0)
124 : AllocationInst(Ty, ArraySize, Alloca,
125 Align, NameStr, InsertBefore) {}
126 AllocaInst(const Type *Ty, Value *ArraySize,
127 unsigned Align, const Twine &NameStr,
128 BasicBlock *InsertAtEnd)
129 : AllocationInst(Ty, ArraySize, Alloca,
130 Align, NameStr, InsertAtEnd) {}
132 virtual AllocaInst *clone() const;
134 /// isStaticAlloca - Return true if this alloca is in the entry block of the
135 /// function and is a constant size. If so, the code generator will fold it
136 /// into the prolog/epilog code, so it is basically free.
137 bool isStaticAlloca() const;
139 // Methods for support type inquiry through isa, cast, and dyn_cast:
140 static inline bool classof(const AllocaInst *) { return true; }
141 static inline bool classof(const Instruction *I) {
142 return (I->getOpcode() == Instruction::Alloca);
144 static inline bool classof(const Value *V) {
145 return isa<Instruction>(V) && classof(cast<Instruction>(V));
150 //===----------------------------------------------------------------------===//
152 //===----------------------------------------------------------------------===//
154 /// FreeInst - an instruction to deallocate memory
156 class FreeInst : public UnaryInstruction {
159 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
160 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
162 virtual FreeInst *clone() const;
164 // Accessor methods for consistency with other memory operations
165 Value *getPointerOperand() { return getOperand(0); }
166 const Value *getPointerOperand() const { return getOperand(0); }
168 // Methods for support type inquiry through isa, cast, and dyn_cast:
169 static inline bool classof(const FreeInst *) { return true; }
170 static inline bool classof(const Instruction *I) {
171 return (I->getOpcode() == Instruction::Free);
173 static inline bool classof(const Value *V) {
174 return isa<Instruction>(V) && classof(cast<Instruction>(V));
179 //===----------------------------------------------------------------------===//
181 //===----------------------------------------------------------------------===//
183 /// LoadInst - an instruction for reading from memory. This uses the
184 /// SubclassData field in Value to store whether or not the load is volatile.
186 class LoadInst : public UnaryInstruction {
189 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
190 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
191 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
192 Instruction *InsertBefore = 0);
193 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
194 unsigned Align, Instruction *InsertBefore = 0);
195 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
196 BasicBlock *InsertAtEnd);
197 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
198 unsigned Align, BasicBlock *InsertAtEnd);
200 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
201 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
202 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
203 bool isVolatile = false, Instruction *InsertBefore = 0);
204 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
205 BasicBlock *InsertAtEnd);
207 /// isVolatile - Return true if this is a load from a volatile memory
210 bool isVolatile() const { return SubclassData & 1; }
212 /// setVolatile - Specify whether this is a volatile load or not.
214 void setVolatile(bool V) {
215 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
218 virtual LoadInst *clone() const;
220 /// getAlignment - Return the alignment of the access that is being performed
222 unsigned getAlignment() const {
223 return (1 << (SubclassData>>1)) >> 1;
226 void setAlignment(unsigned Align);
228 Value *getPointerOperand() { return getOperand(0); }
229 const Value *getPointerOperand() const { return getOperand(0); }
230 static unsigned getPointerOperandIndex() { return 0U; }
232 unsigned getPointerAddressSpace() const {
233 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
237 // Methods for support type inquiry through isa, cast, and dyn_cast:
238 static inline bool classof(const LoadInst *) { return true; }
239 static inline bool classof(const Instruction *I) {
240 return I->getOpcode() == Instruction::Load;
242 static inline bool classof(const Value *V) {
243 return isa<Instruction>(V) && classof(cast<Instruction>(V));
248 //===----------------------------------------------------------------------===//
250 //===----------------------------------------------------------------------===//
252 /// StoreInst - an instruction for storing to memory
254 class StoreInst : public Instruction {
255 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
258 // allocate space for exactly two operands
259 void *operator new(size_t s) {
260 return User::operator new(s, 2);
262 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
263 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
264 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
265 Instruction *InsertBefore = 0);
266 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
267 unsigned Align, Instruction *InsertBefore = 0);
268 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
269 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
270 unsigned Align, BasicBlock *InsertAtEnd);
273 /// isVolatile - Return true if this is a load from a volatile memory
276 bool isVolatile() const { return SubclassData & 1; }
278 /// setVolatile - Specify whether this is a volatile load or not.
280 void setVolatile(bool V) {
281 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
284 /// Transparently provide more efficient getOperand methods.
285 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
287 /// getAlignment - Return the alignment of the access that is being performed
289 unsigned getAlignment() const {
290 return (1 << (SubclassData>>1)) >> 1;
293 void setAlignment(unsigned Align);
295 virtual StoreInst *clone() const;
297 Value *getPointerOperand() { return getOperand(1); }
298 const Value *getPointerOperand() const { return getOperand(1); }
299 static unsigned getPointerOperandIndex() { return 1U; }
301 unsigned getPointerAddressSpace() const {
302 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
305 // Methods for support type inquiry through isa, cast, and dyn_cast:
306 static inline bool classof(const StoreInst *) { return true; }
307 static inline bool classof(const Instruction *I) {
308 return I->getOpcode() == Instruction::Store;
310 static inline bool classof(const Value *V) {
311 return isa<Instruction>(V) && classof(cast<Instruction>(V));
316 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
319 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
321 //===----------------------------------------------------------------------===//
322 // GetElementPtrInst Class
323 //===----------------------------------------------------------------------===//
325 // checkType - Simple wrapper function to give a better assertion failure
326 // message on bad indexes for a gep instruction.
328 static inline const Type *checkType(const Type *Ty) {
329 assert(Ty && "Invalid GetElementPtrInst indices for type!");
333 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
334 /// access elements of arrays and structs
336 class GetElementPtrInst : public Instruction {
337 GetElementPtrInst(const GetElementPtrInst &GEPI);
338 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
339 const Twine &NameStr);
340 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
342 template<typename InputIterator>
343 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
344 const Twine &NameStr,
345 // This argument ensures that we have an iterator we can
346 // do arithmetic on in constant time
347 std::random_access_iterator_tag) {
348 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
351 // This requires that the iterator points to contiguous memory.
352 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
353 // we have to build an array here
356 init(Ptr, 0, NumIdx, NameStr);
360 /// getIndexedType - Returns the type of the element that would be loaded with
361 /// a load instruction with the specified parameters.
363 /// Null is returned if the indices are invalid for the specified
366 template<typename InputIterator>
367 static const Type *getIndexedType(const Type *Ptr,
368 InputIterator IdxBegin,
369 InputIterator IdxEnd,
370 // This argument ensures that we
371 // have an iterator we can do
372 // arithmetic on in constant time
373 std::random_access_iterator_tag) {
374 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
377 // This requires that the iterator points to contiguous memory.
378 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
380 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
383 /// Constructors - Create a getelementptr instruction with a base pointer an
384 /// list of indices. The first ctor can optionally insert before an existing
385 /// instruction, the second appends the new instruction to the specified
387 template<typename InputIterator>
388 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
389 InputIterator IdxEnd,
391 const Twine &NameStr,
392 Instruction *InsertBefore);
393 template<typename InputIterator>
394 inline GetElementPtrInst(Value *Ptr,
395 InputIterator IdxBegin, InputIterator IdxEnd,
397 const Twine &NameStr, BasicBlock *InsertAtEnd);
399 /// Constructors - These two constructors are convenience methods because one
400 /// and two index getelementptr instructions are so common.
401 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
402 Instruction *InsertBefore = 0);
403 GetElementPtrInst(Value *Ptr, Value *Idx,
404 const Twine &NameStr, BasicBlock *InsertAtEnd);
406 template<typename InputIterator>
407 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
408 InputIterator IdxEnd,
409 const Twine &NameStr = "",
410 Instruction *InsertBefore = 0) {
411 typename std::iterator_traits<InputIterator>::difference_type Values =
412 1 + std::distance(IdxBegin, IdxEnd);
414 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
416 template<typename InputIterator>
417 static GetElementPtrInst *Create(Value *Ptr,
418 InputIterator IdxBegin, InputIterator IdxEnd,
419 const Twine &NameStr,
420 BasicBlock *InsertAtEnd) {
421 typename std::iterator_traits<InputIterator>::difference_type Values =
422 1 + std::distance(IdxBegin, IdxEnd);
424 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
427 /// Constructors - These two creators are convenience methods because one
428 /// index getelementptr instructions are so common.
429 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
430 const Twine &NameStr = "",
431 Instruction *InsertBefore = 0) {
432 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
434 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
435 const Twine &NameStr,
436 BasicBlock *InsertAtEnd) {
437 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
440 /// Create an "inbounds" getelementptr. See the documentation for the
441 /// "inbounds" flag in LangRef.html for details.
442 template<typename InputIterator>
443 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
444 InputIterator IdxEnd,
445 const Twine &NameStr = "",
446 Instruction *InsertBefore = 0) {
447 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
448 NameStr, InsertBefore);
449 GEP->setIsInBounds(true);
452 template<typename InputIterator>
453 static GetElementPtrInst *CreateInBounds(Value *Ptr,
454 InputIterator IdxBegin,
455 InputIterator IdxEnd,
456 const Twine &NameStr,
457 BasicBlock *InsertAtEnd) {
458 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
459 NameStr, InsertAtEnd);
460 GEP->setIsInBounds(true);
463 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
464 const Twine &NameStr = "",
465 Instruction *InsertBefore = 0) {
466 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
467 GEP->setIsInBounds(true);
470 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
471 const Twine &NameStr,
472 BasicBlock *InsertAtEnd) {
473 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
474 GEP->setIsInBounds(true);
478 virtual GetElementPtrInst *clone() const;
480 /// Transparently provide more efficient getOperand methods.
481 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
483 // getType - Overload to return most specific pointer type...
484 const PointerType *getType() const {
485 return reinterpret_cast<const PointerType*>(Instruction::getType());
488 /// getIndexedType - Returns the type of the element that would be loaded with
489 /// a load instruction with the specified parameters.
491 /// Null is returned if the indices are invalid for the specified
494 template<typename InputIterator>
495 static const Type *getIndexedType(const Type *Ptr,
496 InputIterator IdxBegin,
497 InputIterator IdxEnd) {
498 return getIndexedType(Ptr, IdxBegin, IdxEnd,
499 typename std::iterator_traits<InputIterator>::
500 iterator_category());
503 static const Type *getIndexedType(const Type *Ptr,
504 Value* const *Idx, unsigned NumIdx);
506 static const Type *getIndexedType(const Type *Ptr,
507 uint64_t const *Idx, unsigned NumIdx);
509 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
511 inline op_iterator idx_begin() { return op_begin()+1; }
512 inline const_op_iterator idx_begin() const { return op_begin()+1; }
513 inline op_iterator idx_end() { return op_end(); }
514 inline const_op_iterator idx_end() const { return op_end(); }
516 Value *getPointerOperand() {
517 return getOperand(0);
519 const Value *getPointerOperand() const {
520 return getOperand(0);
522 static unsigned getPointerOperandIndex() {
523 return 0U; // get index for modifying correct operand
526 unsigned getPointerAddressSpace() const {
527 return cast<PointerType>(getType())->getAddressSpace();
530 /// getPointerOperandType - Method to return the pointer operand as a
532 const PointerType *getPointerOperandType() const {
533 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
537 unsigned getNumIndices() const { // Note: always non-negative
538 return getNumOperands() - 1;
541 bool hasIndices() const {
542 return getNumOperands() > 1;
545 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
546 /// zeros. If so, the result pointer and the first operand have the same
547 /// value, just potentially different types.
548 bool hasAllZeroIndices() const;
550 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
551 /// constant integers. If so, the result pointer and the first operand have
552 /// a constant offset between them.
553 bool hasAllConstantIndices() const;
555 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
556 /// See LangRef.html for the meaning of inbounds on a getelementptr.
557 void setIsInBounds(bool b = true);
559 /// isInBounds - Determine whether the GEP has the inbounds flag.
560 bool isInBounds() const;
562 // Methods for support type inquiry through isa, cast, and dyn_cast:
563 static inline bool classof(const GetElementPtrInst *) { return true; }
564 static inline bool classof(const Instruction *I) {
565 return (I->getOpcode() == Instruction::GetElementPtr);
567 static inline bool classof(const Value *V) {
568 return isa<Instruction>(V) && classof(cast<Instruction>(V));
573 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
576 template<typename InputIterator>
577 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
578 InputIterator IdxBegin,
579 InputIterator IdxEnd,
581 const Twine &NameStr,
582 Instruction *InsertBefore)
583 : Instruction(PointerType::get(checkType(
584 getIndexedType(Ptr->getType(),
586 cast<PointerType>(Ptr->getType())
587 ->getAddressSpace()),
589 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
590 Values, InsertBefore) {
591 init(Ptr, IdxBegin, IdxEnd, NameStr,
592 typename std::iterator_traits<InputIterator>::iterator_category());
594 template<typename InputIterator>
595 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
596 InputIterator IdxBegin,
597 InputIterator IdxEnd,
599 const Twine &NameStr,
600 BasicBlock *InsertAtEnd)
601 : Instruction(PointerType::get(checkType(
602 getIndexedType(Ptr->getType(),
604 cast<PointerType>(Ptr->getType())
605 ->getAddressSpace()),
607 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
608 Values, InsertAtEnd) {
609 init(Ptr, IdxBegin, IdxEnd, NameStr,
610 typename std::iterator_traits<InputIterator>::iterator_category());
614 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
617 //===----------------------------------------------------------------------===//
619 //===----------------------------------------------------------------------===//
621 /// This instruction compares its operands according to the predicate given
622 /// to the constructor. It only operates on integers or pointers. The operands
623 /// must be identical types.
624 /// @brief Represent an integer comparison operator.
625 class ICmpInst: public CmpInst {
627 /// @brief Constructor with insert-before-instruction semantics.
629 Instruction *InsertBefore, ///< Where to insert
630 Predicate pred, ///< The predicate to use for the comparison
631 Value *LHS, ///< The left-hand-side of the expression
632 Value *RHS, ///< The right-hand-side of the expression
633 const Twine &NameStr = "" ///< Name of the instruction
634 ) : CmpInst(makeCmpResultType(LHS->getType()),
635 Instruction::ICmp, pred, LHS, RHS, NameStr,
637 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
638 pred <= CmpInst::LAST_ICMP_PREDICATE &&
639 "Invalid ICmp predicate value");
640 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
641 "Both operands to ICmp instruction are not of the same type!");
642 // Check that the operands are the right type
643 assert((getOperand(0)->getType()->isIntOrIntVector() ||
644 isa<PointerType>(getOperand(0)->getType())) &&
645 "Invalid operand types for ICmp instruction");
648 /// @brief Constructor with insert-at-end semantics.
650 BasicBlock &InsertAtEnd, ///< Block to insert into.
651 Predicate pred, ///< The predicate to use for the comparison
652 Value *LHS, ///< The left-hand-side of the expression
653 Value *RHS, ///< The right-hand-side of the expression
654 const Twine &NameStr = "" ///< Name of the instruction
655 ) : CmpInst(makeCmpResultType(LHS->getType()),
656 Instruction::ICmp, pred, LHS, RHS, NameStr,
658 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
659 pred <= CmpInst::LAST_ICMP_PREDICATE &&
660 "Invalid ICmp predicate value");
661 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
662 "Both operands to ICmp instruction are not of the same type!");
663 // Check that the operands are the right type
664 assert((getOperand(0)->getType()->isIntOrIntVector() ||
665 isa<PointerType>(getOperand(0)->getType())) &&
666 "Invalid operand types for ICmp instruction");
669 /// @brief Constructor with no-insertion semantics
671 Predicate pred, ///< The predicate to use for the comparison
672 Value *LHS, ///< The left-hand-side of the expression
673 Value *RHS, ///< The right-hand-side of the expression
674 const Twine &NameStr = "" ///< Name of the instruction
675 ) : CmpInst(makeCmpResultType(LHS->getType()),
676 Instruction::ICmp, pred, LHS, RHS, NameStr) {
677 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
678 pred <= CmpInst::LAST_ICMP_PREDICATE &&
679 "Invalid ICmp predicate value");
680 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
681 "Both operands to ICmp instruction are not of the same type!");
682 // Check that the operands are the right type
683 assert((getOperand(0)->getType()->isIntOrIntVector() ||
684 isa<PointerType>(getOperand(0)->getType())) &&
685 "Invalid operand types for ICmp instruction");
688 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
689 /// @returns the predicate that would be the result if the operand were
690 /// regarded as signed.
691 /// @brief Return the signed version of the predicate
692 Predicate getSignedPredicate() const {
693 return getSignedPredicate(getPredicate());
696 /// This is a static version that you can use without an instruction.
697 /// @brief Return the signed version of the predicate.
698 static Predicate getSignedPredicate(Predicate pred);
700 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
701 /// @returns the predicate that would be the result if the operand were
702 /// regarded as unsigned.
703 /// @brief Return the unsigned version of the predicate
704 Predicate getUnsignedPredicate() const {
705 return getUnsignedPredicate(getPredicate());
708 /// This is a static version that you can use without an instruction.
709 /// @brief Return the unsigned version of the predicate.
710 static Predicate getUnsignedPredicate(Predicate pred);
712 /// isEquality - Return true if this predicate is either EQ or NE. This also
713 /// tests for commutativity.
714 static bool isEquality(Predicate P) {
715 return P == ICMP_EQ || P == ICMP_NE;
718 /// isEquality - Return true if this predicate is either EQ or NE. This also
719 /// tests for commutativity.
720 bool isEquality() const {
721 return isEquality(getPredicate());
724 /// @returns true if the predicate of this ICmpInst is commutative
725 /// @brief Determine if this relation is commutative.
726 bool isCommutative() const { return isEquality(); }
728 /// isRelational - Return true if the predicate is relational (not EQ or NE).
730 bool isRelational() const {
731 return !isEquality();
734 /// isRelational - Return true if the predicate is relational (not EQ or NE).
736 static bool isRelational(Predicate P) {
737 return !isEquality(P);
740 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
741 /// @brief Determine if this instruction's predicate is signed.
742 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
744 /// @returns true if the predicate provided is signed, false otherwise
745 /// @brief Determine if the predicate is signed.
746 static bool isSignedPredicate(Predicate pred);
748 /// @returns true if the specified compare predicate is
749 /// true when both operands are equal...
750 /// @brief Determine if the icmp is true when both operands are equal
751 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
752 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
753 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
754 pred == ICmpInst::ICMP_SLE;
757 /// @returns true if the specified compare instruction is
758 /// true when both operands are equal...
759 /// @brief Determine if the ICmpInst returns true when both operands are equal
760 bool isTrueWhenEqual() {
761 return isTrueWhenEqual(getPredicate());
764 /// Initialize a set of values that all satisfy the predicate with C.
765 /// @brief Make a ConstantRange for a relation with a constant value.
766 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
768 /// Exchange the two operands to this instruction in such a way that it does
769 /// not modify the semantics of the instruction. The predicate value may be
770 /// changed to retain the same result if the predicate is order dependent
772 /// @brief Swap operands and adjust predicate.
773 void swapOperands() {
774 SubclassData = getSwappedPredicate();
775 Op<0>().swap(Op<1>());
778 virtual ICmpInst *clone() const;
780 // Methods for support type inquiry through isa, cast, and dyn_cast:
781 static inline bool classof(const ICmpInst *) { return true; }
782 static inline bool classof(const Instruction *I) {
783 return I->getOpcode() == Instruction::ICmp;
785 static inline bool classof(const Value *V) {
786 return isa<Instruction>(V) && classof(cast<Instruction>(V));
791 //===----------------------------------------------------------------------===//
793 //===----------------------------------------------------------------------===//
795 /// This instruction compares its operands according to the predicate given
796 /// to the constructor. It only operates on floating point values or packed
797 /// vectors of floating point values. The operands must be identical types.
798 /// @brief Represents a floating point comparison operator.
799 class FCmpInst: public CmpInst {
801 /// @brief Constructor with insert-before-instruction semantics.
803 Instruction *InsertBefore, ///< Where to insert
804 Predicate pred, ///< The predicate to use for the comparison
805 Value *LHS, ///< The left-hand-side of the expression
806 Value *RHS, ///< The right-hand-side of the expression
807 const Twine &NameStr = "" ///< Name of the instruction
808 ) : CmpInst(makeCmpResultType(LHS->getType()),
809 Instruction::FCmp, pred, LHS, RHS, NameStr,
811 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
812 "Invalid FCmp predicate value");
813 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
814 "Both operands to FCmp instruction are not of the same type!");
815 // Check that the operands are the right type
816 assert(getOperand(0)->getType()->isFPOrFPVector() &&
817 "Invalid operand types for FCmp instruction");
820 /// @brief Constructor with insert-at-end semantics.
822 BasicBlock &InsertAtEnd, ///< Block to insert into.
823 Predicate pred, ///< The predicate to use for the comparison
824 Value *LHS, ///< The left-hand-side of the expression
825 Value *RHS, ///< The right-hand-side of the expression
826 const Twine &NameStr = "" ///< Name of the instruction
827 ) : CmpInst(makeCmpResultType(LHS->getType()),
828 Instruction::FCmp, pred, LHS, RHS, NameStr,
830 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
831 "Invalid FCmp predicate value");
832 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
833 "Both operands to FCmp instruction are not of the same type!");
834 // Check that the operands are the right type
835 assert(getOperand(0)->getType()->isFPOrFPVector() &&
836 "Invalid operand types for FCmp instruction");
839 /// @brief Constructor with no-insertion semantics
841 Predicate pred, ///< The predicate to use for the comparison
842 Value *LHS, ///< The left-hand-side of the expression
843 Value *RHS, ///< The right-hand-side of the expression
844 const Twine &NameStr = "" ///< Name of the instruction
845 ) : CmpInst(makeCmpResultType(LHS->getType()),
846 Instruction::FCmp, pred, LHS, RHS, NameStr) {
847 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
848 "Invalid FCmp predicate value");
849 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
850 "Both operands to FCmp instruction are not of the same type!");
851 // Check that the operands are the right type
852 assert(getOperand(0)->getType()->isFPOrFPVector() &&
853 "Invalid operand types for FCmp instruction");
856 /// @returns true if the predicate of this instruction is EQ or NE.
857 /// @brief Determine if this is an equality predicate.
858 bool isEquality() const {
859 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
860 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
863 /// @returns true if the predicate of this instruction is commutative.
864 /// @brief Determine if this is a commutative predicate.
865 bool isCommutative() const {
866 return isEquality() ||
867 SubclassData == FCMP_FALSE ||
868 SubclassData == FCMP_TRUE ||
869 SubclassData == FCMP_ORD ||
870 SubclassData == FCMP_UNO;
873 /// @returns true if the predicate is relational (not EQ or NE).
874 /// @brief Determine if this a relational predicate.
875 bool isRelational() const { return !isEquality(); }
877 /// Exchange the two operands to this instruction in such a way that it does
878 /// not modify the semantics of the instruction. The predicate value may be
879 /// changed to retain the same result if the predicate is order dependent
881 /// @brief Swap operands and adjust predicate.
882 void swapOperands() {
883 SubclassData = getSwappedPredicate();
884 Op<0>().swap(Op<1>());
887 virtual FCmpInst *clone() const;
889 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
890 static inline bool classof(const FCmpInst *) { return true; }
891 static inline bool classof(const Instruction *I) {
892 return I->getOpcode() == Instruction::FCmp;
894 static inline bool classof(const Value *V) {
895 return isa<Instruction>(V) && classof(cast<Instruction>(V));
899 //===----------------------------------------------------------------------===//
901 //===----------------------------------------------------------------------===//
902 /// CallInst - This class represents a function call, abstracting a target
903 /// machine's calling convention. This class uses low bit of the SubClassData
904 /// field to indicate whether or not this is a tail call. The rest of the bits
905 /// hold the calling convention of the call.
908 class CallInst : public Instruction {
909 AttrListPtr AttributeList; ///< parameter attributes for call
910 CallInst(const CallInst &CI);
911 void init(Value *Func, Value* const *Params, unsigned NumParams);
912 void init(Value *Func, Value *Actual1, Value *Actual2);
913 void init(Value *Func, Value *Actual);
914 void init(Value *Func);
916 template<typename InputIterator>
917 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
918 const Twine &NameStr,
919 // This argument ensures that we have an iterator we can
920 // do arithmetic on in constant time
921 std::random_access_iterator_tag) {
922 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
924 // This requires that the iterator points to contiguous memory.
925 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
929 /// Construct a CallInst given a range of arguments. InputIterator
930 /// must be a random-access iterator pointing to contiguous storage
931 /// (e.g. a std::vector<>::iterator). Checks are made for
932 /// random-accessness but not for contiguous storage as that would
933 /// incur runtime overhead.
934 /// @brief Construct a CallInst from a range of arguments
935 template<typename InputIterator>
936 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
937 const Twine &NameStr, Instruction *InsertBefore);
939 /// Construct a CallInst given a range of arguments. InputIterator
940 /// must be a random-access iterator pointing to contiguous storage
941 /// (e.g. a std::vector<>::iterator). Checks are made for
942 /// random-accessness but not for contiguous storage as that would
943 /// incur runtime overhead.
944 /// @brief Construct a CallInst from a range of arguments
945 template<typename InputIterator>
946 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
947 const Twine &NameStr, BasicBlock *InsertAtEnd);
949 CallInst(Value *F, Value *Actual, const Twine &NameStr,
950 Instruction *InsertBefore);
951 CallInst(Value *F, Value *Actual, const Twine &NameStr,
952 BasicBlock *InsertAtEnd);
953 explicit CallInst(Value *F, const Twine &NameStr,
954 Instruction *InsertBefore);
955 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
957 template<typename InputIterator>
958 static CallInst *Create(Value *Func,
959 InputIterator ArgBegin, InputIterator ArgEnd,
960 const Twine &NameStr = "",
961 Instruction *InsertBefore = 0) {
962 return new((unsigned)(ArgEnd - ArgBegin + 1))
963 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
965 template<typename InputIterator>
966 static CallInst *Create(Value *Func,
967 InputIterator ArgBegin, InputIterator ArgEnd,
968 const Twine &NameStr, BasicBlock *InsertAtEnd) {
969 return new((unsigned)(ArgEnd - ArgBegin + 1))
970 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
972 static CallInst *Create(Value *F, Value *Actual,
973 const Twine &NameStr = "",
974 Instruction *InsertBefore = 0) {
975 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
977 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
978 BasicBlock *InsertAtEnd) {
979 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
981 static CallInst *Create(Value *F, const Twine &NameStr = "",
982 Instruction *InsertBefore = 0) {
983 return new(1) CallInst(F, NameStr, InsertBefore);
985 static CallInst *Create(Value *F, const Twine &NameStr,
986 BasicBlock *InsertAtEnd) {
987 return new(1) CallInst(F, NameStr, InsertAtEnd);
989 /// CreateMalloc - Generate the IR for a call to malloc:
990 /// 1. Compute the malloc call's argument as the specified type's size,
991 /// possibly multiplied by the array size if the array size is not
993 /// 2. Call malloc with that argument.
994 /// 3. Bitcast the result of the malloc call to the specified type.
995 static Value *CreateMalloc(Instruction *InsertBefore, const Type *IntPtrTy,
996 const Type *AllocTy, Value *ArraySize = 0,
997 const Twine &Name = "");
998 static Value *CreateMalloc(BasicBlock *InsertAtEnd, const Type *IntPtrTy,
999 const Type *AllocTy, Value *ArraySize = 0,
1000 Function* MallocF = 0, const Twine &Name = "");
1004 bool isTailCall() const { return SubclassData & 1; }
1005 void setTailCall(bool isTC = true) {
1006 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1009 virtual CallInst *clone() const;
1011 /// Provide fast operand accessors
1012 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1014 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1016 CallingConv::ID getCallingConv() const {
1017 return static_cast<CallingConv::ID>(SubclassData >> 1);
1019 void setCallingConv(CallingConv::ID CC) {
1020 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
1023 /// getAttributes - Return the parameter attributes for this call.
1025 const AttrListPtr &getAttributes() const { return AttributeList; }
1027 /// setAttributes - Set the parameter attributes for this call.
1029 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1031 /// addAttribute - adds the attribute to the list of attributes.
1032 void addAttribute(unsigned i, Attributes attr);
1034 /// removeAttribute - removes the attribute from the list of attributes.
1035 void removeAttribute(unsigned i, Attributes attr);
1037 /// @brief Determine whether the call or the callee has the given attribute.
1038 bool paramHasAttr(unsigned i, Attributes attr) const;
1040 /// @brief Extract the alignment for a call or parameter (0=unknown).
1041 unsigned getParamAlignment(unsigned i) const {
1042 return AttributeList.getParamAlignment(i);
1045 /// @brief Determine if the call does not access memory.
1046 bool doesNotAccessMemory() const {
1047 return paramHasAttr(~0, Attribute::ReadNone);
1049 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1050 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1051 else removeAttribute(~0, Attribute::ReadNone);
1054 /// @brief Determine if the call does not access or only reads memory.
1055 bool onlyReadsMemory() const {
1056 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1058 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1059 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1060 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1063 /// @brief Determine if the call cannot return.
1064 bool doesNotReturn() const {
1065 return paramHasAttr(~0, Attribute::NoReturn);
1067 void setDoesNotReturn(bool DoesNotReturn = true) {
1068 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1069 else removeAttribute(~0, Attribute::NoReturn);
1072 /// @brief Determine if the call cannot unwind.
1073 bool doesNotThrow() const {
1074 return paramHasAttr(~0, Attribute::NoUnwind);
1076 void setDoesNotThrow(bool DoesNotThrow = true) {
1077 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1078 else removeAttribute(~0, Attribute::NoUnwind);
1081 /// @brief Determine if the call returns a structure through first
1082 /// pointer argument.
1083 bool hasStructRetAttr() const {
1084 // Be friendly and also check the callee.
1085 return paramHasAttr(1, Attribute::StructRet);
1088 /// @brief Determine if any call argument is an aggregate passed by value.
1089 bool hasByValArgument() const {
1090 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1093 /// getCalledFunction - Return the function called, or null if this is an
1094 /// indirect function invocation.
1096 Function *getCalledFunction() const {
1097 return dyn_cast<Function>(Op<0>());
1100 /// getCalledValue - Get a pointer to the function that is invoked by this
1102 const Value *getCalledValue() const { return Op<0>(); }
1103 Value *getCalledValue() { return Op<0>(); }
1105 /// setCalledFunction - Set the function called
1106 void setCalledFunction(Value* Fn) {
1110 // Methods for support type inquiry through isa, cast, and dyn_cast:
1111 static inline bool classof(const CallInst *) { return true; }
1112 static inline bool classof(const Instruction *I) {
1113 return I->getOpcode() == Instruction::Call;
1115 static inline bool classof(const Value *V) {
1116 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1121 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1124 template<typename InputIterator>
1125 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1126 const Twine &NameStr, BasicBlock *InsertAtEnd)
1127 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1128 ->getElementType())->getReturnType(),
1130 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1131 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1132 init(Func, ArgBegin, ArgEnd, NameStr,
1133 typename std::iterator_traits<InputIterator>::iterator_category());
1136 template<typename InputIterator>
1137 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1138 const Twine &NameStr, Instruction *InsertBefore)
1139 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1140 ->getElementType())->getReturnType(),
1142 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1143 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1144 init(Func, ArgBegin, ArgEnd, NameStr,
1145 typename std::iterator_traits<InputIterator>::iterator_category());
1148 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1150 //===----------------------------------------------------------------------===//
1152 //===----------------------------------------------------------------------===//
1154 /// SelectInst - This class represents the LLVM 'select' instruction.
1156 class SelectInst : public Instruction {
1157 void init(Value *C, Value *S1, Value *S2) {
1158 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1164 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1165 Instruction *InsertBefore)
1166 : Instruction(S1->getType(), Instruction::Select,
1167 &Op<0>(), 3, InsertBefore) {
1171 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1172 BasicBlock *InsertAtEnd)
1173 : Instruction(S1->getType(), Instruction::Select,
1174 &Op<0>(), 3, InsertAtEnd) {
1179 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1180 const Twine &NameStr = "",
1181 Instruction *InsertBefore = 0) {
1182 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1184 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1185 const Twine &NameStr,
1186 BasicBlock *InsertAtEnd) {
1187 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1190 const Value *getCondition() const { return Op<0>(); }
1191 const Value *getTrueValue() const { return Op<1>(); }
1192 const Value *getFalseValue() const { return Op<2>(); }
1193 Value *getCondition() { return Op<0>(); }
1194 Value *getTrueValue() { return Op<1>(); }
1195 Value *getFalseValue() { return Op<2>(); }
1197 /// areInvalidOperands - Return a string if the specified operands are invalid
1198 /// for a select operation, otherwise return null.
1199 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1201 /// Transparently provide more efficient getOperand methods.
1202 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1204 OtherOps getOpcode() const {
1205 return static_cast<OtherOps>(Instruction::getOpcode());
1208 virtual SelectInst *clone() const;
1210 // Methods for support type inquiry through isa, cast, and dyn_cast:
1211 static inline bool classof(const SelectInst *) { return true; }
1212 static inline bool classof(const Instruction *I) {
1213 return I->getOpcode() == Instruction::Select;
1215 static inline bool classof(const Value *V) {
1216 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1221 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1224 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1226 //===----------------------------------------------------------------------===//
1228 //===----------------------------------------------------------------------===//
1230 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1231 /// an argument of the specified type given a va_list and increments that list
1233 class VAArgInst : public UnaryInstruction {
1235 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1236 Instruction *InsertBefore = 0)
1237 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1240 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1241 BasicBlock *InsertAtEnd)
1242 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1246 virtual VAArgInst *clone() const;
1248 // Methods for support type inquiry through isa, cast, and dyn_cast:
1249 static inline bool classof(const VAArgInst *) { return true; }
1250 static inline bool classof(const Instruction *I) {
1251 return I->getOpcode() == VAArg;
1253 static inline bool classof(const Value *V) {
1254 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1258 //===----------------------------------------------------------------------===//
1259 // ExtractElementInst Class
1260 //===----------------------------------------------------------------------===//
1262 /// ExtractElementInst - This instruction extracts a single (scalar)
1263 /// element from a VectorType value
1265 class ExtractElementInst : public Instruction {
1266 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1267 Instruction *InsertBefore = 0);
1268 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1269 BasicBlock *InsertAtEnd);
1271 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1272 const Twine &NameStr = "",
1273 Instruction *InsertBefore = 0) {
1274 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1276 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1277 const Twine &NameStr,
1278 BasicBlock *InsertAtEnd) {
1279 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1282 /// isValidOperands - Return true if an extractelement instruction can be
1283 /// formed with the specified operands.
1284 static bool isValidOperands(const Value *Vec, const Value *Idx);
1286 virtual ExtractElementInst *clone() const;
1288 Value *getVectorOperand() { return Op<0>(); }
1289 Value *getIndexOperand() { return Op<1>(); }
1290 const Value *getVectorOperand() const { return Op<0>(); }
1291 const Value *getIndexOperand() const { return Op<1>(); }
1293 const VectorType *getVectorOperandType() const {
1294 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1298 /// Transparently provide more efficient getOperand methods.
1299 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1301 // Methods for support type inquiry through isa, cast, and dyn_cast:
1302 static inline bool classof(const ExtractElementInst *) { return true; }
1303 static inline bool classof(const Instruction *I) {
1304 return I->getOpcode() == Instruction::ExtractElement;
1306 static inline bool classof(const Value *V) {
1307 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1312 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1315 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1317 //===----------------------------------------------------------------------===//
1318 // InsertElementInst Class
1319 //===----------------------------------------------------------------------===//
1321 /// InsertElementInst - This instruction inserts a single (scalar)
1322 /// element into a VectorType value
1324 class InsertElementInst : public Instruction {
1325 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1326 const Twine &NameStr = "",
1327 Instruction *InsertBefore = 0);
1328 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1329 const Twine &NameStr, BasicBlock *InsertAtEnd);
1331 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1332 const Twine &NameStr = "",
1333 Instruction *InsertBefore = 0) {
1334 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1336 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1337 const Twine &NameStr,
1338 BasicBlock *InsertAtEnd) {
1339 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1342 /// isValidOperands - Return true if an insertelement instruction can be
1343 /// formed with the specified operands.
1344 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1347 virtual InsertElementInst *clone() const;
1349 /// getType - Overload to return most specific vector type.
1351 const VectorType *getType() const {
1352 return reinterpret_cast<const VectorType*>(Instruction::getType());
1355 /// Transparently provide more efficient getOperand methods.
1356 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1358 // Methods for support type inquiry through isa, cast, and dyn_cast:
1359 static inline bool classof(const InsertElementInst *) { return true; }
1360 static inline bool classof(const Instruction *I) {
1361 return I->getOpcode() == Instruction::InsertElement;
1363 static inline bool classof(const Value *V) {
1364 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1369 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1372 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1374 //===----------------------------------------------------------------------===//
1375 // ShuffleVectorInst Class
1376 //===----------------------------------------------------------------------===//
1378 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1381 class ShuffleVectorInst : public Instruction {
1383 // allocate space for exactly three operands
1384 void *operator new(size_t s) {
1385 return User::operator new(s, 3);
1387 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1388 const Twine &NameStr = "",
1389 Instruction *InsertBefor = 0);
1390 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1391 const Twine &NameStr, BasicBlock *InsertAtEnd);
1393 /// isValidOperands - Return true if a shufflevector instruction can be
1394 /// formed with the specified operands.
1395 static bool isValidOperands(const Value *V1, const Value *V2,
1398 virtual ShuffleVectorInst *clone() const;
1400 /// getType - Overload to return most specific vector type.
1402 const VectorType *getType() const {
1403 return reinterpret_cast<const VectorType*>(Instruction::getType());
1406 /// Transparently provide more efficient getOperand methods.
1407 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1409 /// getMaskValue - Return the index from the shuffle mask for the specified
1410 /// output result. This is either -1 if the element is undef or a number less
1411 /// than 2*numelements.
1412 int getMaskValue(unsigned i) const;
1414 // Methods for support type inquiry through isa, cast, and dyn_cast:
1415 static inline bool classof(const ShuffleVectorInst *) { return true; }
1416 static inline bool classof(const Instruction *I) {
1417 return I->getOpcode() == Instruction::ShuffleVector;
1419 static inline bool classof(const Value *V) {
1420 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1425 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1428 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1430 //===----------------------------------------------------------------------===//
1431 // ExtractValueInst Class
1432 //===----------------------------------------------------------------------===//
1434 /// ExtractValueInst - This instruction extracts a struct member or array
1435 /// element value from an aggregate value.
1437 class ExtractValueInst : public UnaryInstruction {
1438 SmallVector<unsigned, 4> Indices;
1440 ExtractValueInst(const ExtractValueInst &EVI);
1441 void init(const unsigned *Idx, unsigned NumIdx,
1442 const Twine &NameStr);
1443 void init(unsigned Idx, const Twine &NameStr);
1445 template<typename InputIterator>
1446 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1447 const Twine &NameStr,
1448 // This argument ensures that we have an iterator we can
1449 // do arithmetic on in constant time
1450 std::random_access_iterator_tag) {
1451 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1453 // There's no fundamental reason why we require at least one index
1454 // (other than weirdness with &*IdxBegin being invalid; see
1455 // getelementptr's init routine for example). But there's no
1456 // present need to support it.
1457 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1459 // This requires that the iterator points to contiguous memory.
1460 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1461 // we have to build an array here
1464 /// getIndexedType - Returns the type of the element that would be extracted
1465 /// with an extractvalue instruction with the specified parameters.
1467 /// Null is returned if the indices are invalid for the specified
1470 static const Type *getIndexedType(const Type *Agg,
1471 const unsigned *Idx, unsigned NumIdx);
1473 template<typename InputIterator>
1474 static const Type *getIndexedType(const Type *Ptr,
1475 InputIterator IdxBegin,
1476 InputIterator IdxEnd,
1477 // This argument ensures that we
1478 // have an iterator we can do
1479 // arithmetic on in constant time
1480 std::random_access_iterator_tag) {
1481 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1484 // This requires that the iterator points to contiguous memory.
1485 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1487 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1490 /// Constructors - Create a extractvalue instruction with a base aggregate
1491 /// value and a list of indices. The first ctor can optionally insert before
1492 /// an existing instruction, the second appends the new instruction to the
1493 /// specified BasicBlock.
1494 template<typename InputIterator>
1495 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1496 InputIterator IdxEnd,
1497 const Twine &NameStr,
1498 Instruction *InsertBefore);
1499 template<typename InputIterator>
1500 inline ExtractValueInst(Value *Agg,
1501 InputIterator IdxBegin, InputIterator IdxEnd,
1502 const Twine &NameStr, BasicBlock *InsertAtEnd);
1504 // allocate space for exactly one operand
1505 void *operator new(size_t s) {
1506 return User::operator new(s, 1);
1510 template<typename InputIterator>
1511 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1512 InputIterator IdxEnd,
1513 const Twine &NameStr = "",
1514 Instruction *InsertBefore = 0) {
1516 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1518 template<typename InputIterator>
1519 static ExtractValueInst *Create(Value *Agg,
1520 InputIterator IdxBegin, InputIterator IdxEnd,
1521 const Twine &NameStr,
1522 BasicBlock *InsertAtEnd) {
1523 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1526 /// Constructors - These two creators are convenience methods because one
1527 /// index extractvalue instructions are much more common than those with
1529 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1530 const Twine &NameStr = "",
1531 Instruction *InsertBefore = 0) {
1532 unsigned Idxs[1] = { Idx };
1533 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1535 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1536 const Twine &NameStr,
1537 BasicBlock *InsertAtEnd) {
1538 unsigned Idxs[1] = { Idx };
1539 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1542 virtual ExtractValueInst *clone() const;
1544 /// getIndexedType - Returns the type of the element that would be extracted
1545 /// with an extractvalue instruction with the specified parameters.
1547 /// Null is returned if the indices are invalid for the specified
1550 template<typename InputIterator>
1551 static const Type *getIndexedType(const Type *Ptr,
1552 InputIterator IdxBegin,
1553 InputIterator IdxEnd) {
1554 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1555 typename std::iterator_traits<InputIterator>::
1556 iterator_category());
1558 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1560 typedef const unsigned* idx_iterator;
1561 inline idx_iterator idx_begin() const { return Indices.begin(); }
1562 inline idx_iterator idx_end() const { return Indices.end(); }
1564 Value *getAggregateOperand() {
1565 return getOperand(0);
1567 const Value *getAggregateOperand() const {
1568 return getOperand(0);
1570 static unsigned getAggregateOperandIndex() {
1571 return 0U; // get index for modifying correct operand
1574 unsigned getNumIndices() const { // Note: always non-negative
1575 return (unsigned)Indices.size();
1578 bool hasIndices() const {
1582 // Methods for support type inquiry through isa, cast, and dyn_cast:
1583 static inline bool classof(const ExtractValueInst *) { return true; }
1584 static inline bool classof(const Instruction *I) {
1585 return I->getOpcode() == Instruction::ExtractValue;
1587 static inline bool classof(const Value *V) {
1588 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1592 template<typename InputIterator>
1593 ExtractValueInst::ExtractValueInst(Value *Agg,
1594 InputIterator IdxBegin,
1595 InputIterator IdxEnd,
1596 const Twine &NameStr,
1597 Instruction *InsertBefore)
1598 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1600 ExtractValue, Agg, InsertBefore) {
1601 init(IdxBegin, IdxEnd, NameStr,
1602 typename std::iterator_traits<InputIterator>::iterator_category());
1604 template<typename InputIterator>
1605 ExtractValueInst::ExtractValueInst(Value *Agg,
1606 InputIterator IdxBegin,
1607 InputIterator IdxEnd,
1608 const Twine &NameStr,
1609 BasicBlock *InsertAtEnd)
1610 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1612 ExtractValue, Agg, InsertAtEnd) {
1613 init(IdxBegin, IdxEnd, NameStr,
1614 typename std::iterator_traits<InputIterator>::iterator_category());
1618 //===----------------------------------------------------------------------===//
1619 // InsertValueInst Class
1620 //===----------------------------------------------------------------------===//
1622 /// InsertValueInst - This instruction inserts a struct field of array element
1623 /// value into an aggregate value.
1625 class InsertValueInst : public Instruction {
1626 SmallVector<unsigned, 4> Indices;
1628 void *operator new(size_t, unsigned); // Do not implement
1629 InsertValueInst(const InsertValueInst &IVI);
1630 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1631 const Twine &NameStr);
1632 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1634 template<typename InputIterator>
1635 void init(Value *Agg, Value *Val,
1636 InputIterator IdxBegin, InputIterator IdxEnd,
1637 const Twine &NameStr,
1638 // This argument ensures that we have an iterator we can
1639 // do arithmetic on in constant time
1640 std::random_access_iterator_tag) {
1641 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1643 // There's no fundamental reason why we require at least one index
1644 // (other than weirdness with &*IdxBegin being invalid; see
1645 // getelementptr's init routine for example). But there's no
1646 // present need to support it.
1647 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1649 // This requires that the iterator points to contiguous memory.
1650 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1651 // we have to build an array here
1654 /// Constructors - Create a insertvalue instruction with a base aggregate
1655 /// value, a value to insert, and a list of indices. The first ctor can
1656 /// optionally insert before an existing instruction, the second appends
1657 /// the new instruction to the specified BasicBlock.
1658 template<typename InputIterator>
1659 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1660 InputIterator IdxEnd,
1661 const Twine &NameStr,
1662 Instruction *InsertBefore);
1663 template<typename InputIterator>
1664 inline InsertValueInst(Value *Agg, Value *Val,
1665 InputIterator IdxBegin, InputIterator IdxEnd,
1666 const Twine &NameStr, BasicBlock *InsertAtEnd);
1668 /// Constructors - These two constructors are convenience methods because one
1669 /// and two index insertvalue instructions are so common.
1670 InsertValueInst(Value *Agg, Value *Val,
1671 unsigned Idx, const Twine &NameStr = "",
1672 Instruction *InsertBefore = 0);
1673 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1674 const Twine &NameStr, BasicBlock *InsertAtEnd);
1676 // allocate space for exactly two operands
1677 void *operator new(size_t s) {
1678 return User::operator new(s, 2);
1681 template<typename InputIterator>
1682 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1683 InputIterator IdxEnd,
1684 const Twine &NameStr = "",
1685 Instruction *InsertBefore = 0) {
1686 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1687 NameStr, InsertBefore);
1689 template<typename InputIterator>
1690 static InsertValueInst *Create(Value *Agg, Value *Val,
1691 InputIterator IdxBegin, InputIterator IdxEnd,
1692 const Twine &NameStr,
1693 BasicBlock *InsertAtEnd) {
1694 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1695 NameStr, InsertAtEnd);
1698 /// Constructors - These two creators are convenience methods because one
1699 /// index insertvalue instructions are much more common than those with
1701 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1702 const Twine &NameStr = "",
1703 Instruction *InsertBefore = 0) {
1704 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1706 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1707 const Twine &NameStr,
1708 BasicBlock *InsertAtEnd) {
1709 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1712 virtual InsertValueInst *clone() const;
1714 /// Transparently provide more efficient getOperand methods.
1715 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1717 typedef const unsigned* idx_iterator;
1718 inline idx_iterator idx_begin() const { return Indices.begin(); }
1719 inline idx_iterator idx_end() const { return Indices.end(); }
1721 Value *getAggregateOperand() {
1722 return getOperand(0);
1724 const Value *getAggregateOperand() const {
1725 return getOperand(0);
1727 static unsigned getAggregateOperandIndex() {
1728 return 0U; // get index for modifying correct operand
1731 Value *getInsertedValueOperand() {
1732 return getOperand(1);
1734 const Value *getInsertedValueOperand() const {
1735 return getOperand(1);
1737 static unsigned getInsertedValueOperandIndex() {
1738 return 1U; // get index for modifying correct operand
1741 unsigned getNumIndices() const { // Note: always non-negative
1742 return (unsigned)Indices.size();
1745 bool hasIndices() const {
1749 // Methods for support type inquiry through isa, cast, and dyn_cast:
1750 static inline bool classof(const InsertValueInst *) { return true; }
1751 static inline bool classof(const Instruction *I) {
1752 return I->getOpcode() == Instruction::InsertValue;
1754 static inline bool classof(const Value *V) {
1755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1760 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1763 template<typename InputIterator>
1764 InsertValueInst::InsertValueInst(Value *Agg,
1766 InputIterator IdxBegin,
1767 InputIterator IdxEnd,
1768 const Twine &NameStr,
1769 Instruction *InsertBefore)
1770 : Instruction(Agg->getType(), InsertValue,
1771 OperandTraits<InsertValueInst>::op_begin(this),
1773 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1774 typename std::iterator_traits<InputIterator>::iterator_category());
1776 template<typename InputIterator>
1777 InsertValueInst::InsertValueInst(Value *Agg,
1779 InputIterator IdxBegin,
1780 InputIterator IdxEnd,
1781 const Twine &NameStr,
1782 BasicBlock *InsertAtEnd)
1783 : Instruction(Agg->getType(), InsertValue,
1784 OperandTraits<InsertValueInst>::op_begin(this),
1786 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1787 typename std::iterator_traits<InputIterator>::iterator_category());
1790 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1792 //===----------------------------------------------------------------------===//
1794 //===----------------------------------------------------------------------===//
1796 // PHINode - The PHINode class is used to represent the magical mystical PHI
1797 // node, that can not exist in nature, but can be synthesized in a computer
1798 // scientist's overactive imagination.
1800 class PHINode : public Instruction {
1801 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1802 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1803 /// the number actually in use.
1804 unsigned ReservedSpace;
1805 PHINode(const PHINode &PN);
1806 // allocate space for exactly zero operands
1807 void *operator new(size_t s) {
1808 return User::operator new(s, 0);
1810 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1811 Instruction *InsertBefore = 0)
1812 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1817 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1818 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1823 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1824 Instruction *InsertBefore = 0) {
1825 return new PHINode(Ty, NameStr, InsertBefore);
1827 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1828 BasicBlock *InsertAtEnd) {
1829 return new PHINode(Ty, NameStr, InsertAtEnd);
1833 /// reserveOperandSpace - This method can be used to avoid repeated
1834 /// reallocation of PHI operand lists by reserving space for the correct
1835 /// number of operands before adding them. Unlike normal vector reserves,
1836 /// this method can also be used to trim the operand space.
1837 void reserveOperandSpace(unsigned NumValues) {
1838 resizeOperands(NumValues*2);
1841 virtual PHINode *clone() const;
1843 /// Provide fast operand accessors
1844 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1846 /// getNumIncomingValues - Return the number of incoming edges
1848 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1850 /// getIncomingValue - Return incoming value number x
1852 Value *getIncomingValue(unsigned i) const {
1853 assert(i*2 < getNumOperands() && "Invalid value number!");
1854 return getOperand(i*2);
1856 void setIncomingValue(unsigned i, Value *V) {
1857 assert(i*2 < getNumOperands() && "Invalid value number!");
1860 static unsigned getOperandNumForIncomingValue(unsigned i) {
1863 static unsigned getIncomingValueNumForOperand(unsigned i) {
1864 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1868 /// getIncomingBlock - Return incoming basic block #i.
1870 BasicBlock *getIncomingBlock(unsigned i) const {
1871 return cast<BasicBlock>(getOperand(i*2+1));
1874 /// getIncomingBlock - Return incoming basic block corresponding
1875 /// to an operand of the PHI.
1877 BasicBlock *getIncomingBlock(const Use &U) const {
1878 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1879 return cast<BasicBlock>((&U + 1)->get());
1882 /// getIncomingBlock - Return incoming basic block corresponding
1883 /// to value use iterator.
1885 template <typename U>
1886 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1887 return getIncomingBlock(I.getUse());
1891 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1892 setOperand(i*2+1, BB);
1894 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1897 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1898 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1902 /// addIncoming - Add an incoming value to the end of the PHI list
1904 void addIncoming(Value *V, BasicBlock *BB) {
1905 assert(V && "PHI node got a null value!");
1906 assert(BB && "PHI node got a null basic block!");
1907 assert(getType() == V->getType() &&
1908 "All operands to PHI node must be the same type as the PHI node!");
1909 unsigned OpNo = NumOperands;
1910 if (OpNo+2 > ReservedSpace)
1911 resizeOperands(0); // Get more space!
1912 // Initialize some new operands.
1913 NumOperands = OpNo+2;
1914 OperandList[OpNo] = V;
1915 OperandList[OpNo+1] = BB;
1918 /// removeIncomingValue - Remove an incoming value. This is useful if a
1919 /// predecessor basic block is deleted. The value removed is returned.
1921 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1922 /// is true), the PHI node is destroyed and any uses of it are replaced with
1923 /// dummy values. The only time there should be zero incoming values to a PHI
1924 /// node is when the block is dead, so this strategy is sound.
1926 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1928 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1929 int Idx = getBasicBlockIndex(BB);
1930 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1931 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1934 /// getBasicBlockIndex - Return the first index of the specified basic
1935 /// block in the value list for this PHI. Returns -1 if no instance.
1937 int getBasicBlockIndex(const BasicBlock *BB) const {
1938 Use *OL = OperandList;
1939 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1940 if (OL[i+1].get() == BB) return i/2;
1944 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1945 return getIncomingValue(getBasicBlockIndex(BB));
1948 /// hasConstantValue - If the specified PHI node always merges together the
1949 /// same value, return the value, otherwise return null.
1951 /// If the PHI has undef operands, but all the rest of the operands are
1952 /// some unique value, return that value if it can be proved that the
1953 /// value dominates the PHI. If DT is null, use a conservative check,
1954 /// otherwise use DT to test for dominance.
1956 Value *hasConstantValue(DominatorTree *DT = 0) const;
1958 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1959 static inline bool classof(const PHINode *) { return true; }
1960 static inline bool classof(const Instruction *I) {
1961 return I->getOpcode() == Instruction::PHI;
1963 static inline bool classof(const Value *V) {
1964 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1967 void resizeOperands(unsigned NumOperands);
1971 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1974 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1977 //===----------------------------------------------------------------------===//
1979 //===----------------------------------------------------------------------===//
1981 //===---------------------------------------------------------------------------
1982 /// ReturnInst - Return a value (possibly void), from a function. Execution
1983 /// does not continue in this function any longer.
1985 class ReturnInst : public TerminatorInst {
1986 ReturnInst(const ReturnInst &RI);
1989 // ReturnInst constructors:
1990 // ReturnInst() - 'ret void' instruction
1991 // ReturnInst( null) - 'ret void' instruction
1992 // ReturnInst(Value* X) - 'ret X' instruction
1993 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1994 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1995 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1996 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1998 // NOTE: If the Value* passed is of type void then the constructor behaves as
1999 // if it was passed NULL.
2000 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2001 Instruction *InsertBefore = 0);
2002 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2003 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2005 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2006 Instruction *InsertBefore = 0) {
2007 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2009 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2010 BasicBlock *InsertAtEnd) {
2011 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2013 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2014 return new(0) ReturnInst(C, InsertAtEnd);
2016 virtual ~ReturnInst();
2018 virtual ReturnInst *clone() const;
2020 /// Provide fast operand accessors
2021 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2023 /// Convenience accessor
2024 Value *getReturnValue(unsigned n = 0) const {
2025 return n < getNumOperands()
2030 unsigned getNumSuccessors() const { return 0; }
2032 // Methods for support type inquiry through isa, cast, and dyn_cast:
2033 static inline bool classof(const ReturnInst *) { return true; }
2034 static inline bool classof(const Instruction *I) {
2035 return (I->getOpcode() == Instruction::Ret);
2037 static inline bool classof(const Value *V) {
2038 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2041 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2042 virtual unsigned getNumSuccessorsV() const;
2043 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2047 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
2050 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2052 //===----------------------------------------------------------------------===//
2054 //===----------------------------------------------------------------------===//
2056 //===---------------------------------------------------------------------------
2057 /// BranchInst - Conditional or Unconditional Branch instruction.
2059 class BranchInst : public TerminatorInst {
2060 /// Ops list - Branches are strange. The operands are ordered:
2061 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2062 /// they don't have to check for cond/uncond branchness. These are mostly
2063 /// accessed relative from op_end().
2064 BranchInst(const BranchInst &BI);
2066 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2067 // BranchInst(BB *B) - 'br B'
2068 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2069 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2070 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2071 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2072 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2073 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2074 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2075 Instruction *InsertBefore = 0);
2076 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2077 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2078 BasicBlock *InsertAtEnd);
2080 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2081 return new(1, true) BranchInst(IfTrue, InsertBefore);
2083 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2084 Value *Cond, Instruction *InsertBefore = 0) {
2085 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2087 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2088 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2090 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2091 Value *Cond, BasicBlock *InsertAtEnd) {
2092 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2097 /// Transparently provide more efficient getOperand methods.
2098 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2100 virtual BranchInst *clone() const;
2102 bool isUnconditional() const { return getNumOperands() == 1; }
2103 bool isConditional() const { return getNumOperands() == 3; }
2105 Value *getCondition() const {
2106 assert(isConditional() && "Cannot get condition of an uncond branch!");
2110 void setCondition(Value *V) {
2111 assert(isConditional() && "Cannot set condition of unconditional branch!");
2115 // setUnconditionalDest - Change the current branch to an unconditional branch
2116 // targeting the specified block.
2117 // FIXME: Eliminate this ugly method.
2118 void setUnconditionalDest(BasicBlock *Dest) {
2120 if (isConditional()) { // Convert this to an uncond branch.
2124 OperandList = op_begin();
2128 unsigned getNumSuccessors() const { return 1+isConditional(); }
2130 BasicBlock *getSuccessor(unsigned i) const {
2131 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2132 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2135 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2136 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2137 *(&Op<-1>() - idx) = NewSucc;
2140 // Methods for support type inquiry through isa, cast, and dyn_cast:
2141 static inline bool classof(const BranchInst *) { return true; }
2142 static inline bool classof(const Instruction *I) {
2143 return (I->getOpcode() == Instruction::Br);
2145 static inline bool classof(const Value *V) {
2146 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2149 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2150 virtual unsigned getNumSuccessorsV() const;
2151 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2155 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2157 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2159 //===----------------------------------------------------------------------===//
2161 //===----------------------------------------------------------------------===//
2163 //===---------------------------------------------------------------------------
2164 /// SwitchInst - Multiway switch
2166 class SwitchInst : public TerminatorInst {
2167 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2168 unsigned ReservedSpace;
2169 // Operand[0] = Value to switch on
2170 // Operand[1] = Default basic block destination
2171 // Operand[2n ] = Value to match
2172 // Operand[2n+1] = BasicBlock to go to on match
2173 SwitchInst(const SwitchInst &RI);
2174 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2175 void resizeOperands(unsigned No);
2176 // allocate space for exactly zero operands
2177 void *operator new(size_t s) {
2178 return User::operator new(s, 0);
2180 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2181 /// switch on and a default destination. The number of additional cases can
2182 /// be specified here to make memory allocation more efficient. This
2183 /// constructor can also autoinsert before another instruction.
2184 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2185 Instruction *InsertBefore = 0);
2187 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2188 /// switch on and a default destination. The number of additional cases can
2189 /// be specified here to make memory allocation more efficient. This
2190 /// constructor also autoinserts at the end of the specified BasicBlock.
2191 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2192 BasicBlock *InsertAtEnd);
2194 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2195 unsigned NumCases, Instruction *InsertBefore = 0) {
2196 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2198 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2199 unsigned NumCases, BasicBlock *InsertAtEnd) {
2200 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2204 /// Provide fast operand accessors
2205 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2207 // Accessor Methods for Switch stmt
2208 Value *getCondition() const { return getOperand(0); }
2209 void setCondition(Value *V) { setOperand(0, V); }
2211 BasicBlock *getDefaultDest() const {
2212 return cast<BasicBlock>(getOperand(1));
2215 /// getNumCases - return the number of 'cases' in this switch instruction.
2216 /// Note that case #0 is always the default case.
2217 unsigned getNumCases() const {
2218 return getNumOperands()/2;
2221 /// getCaseValue - Return the specified case value. Note that case #0, the
2222 /// default destination, does not have a case value.
2223 ConstantInt *getCaseValue(unsigned i) {
2224 assert(i && i < getNumCases() && "Illegal case value to get!");
2225 return getSuccessorValue(i);
2228 /// getCaseValue - Return the specified case value. Note that case #0, the
2229 /// default destination, does not have a case value.
2230 const ConstantInt *getCaseValue(unsigned i) const {
2231 assert(i && i < getNumCases() && "Illegal case value to get!");
2232 return getSuccessorValue(i);
2235 /// findCaseValue - Search all of the case values for the specified constant.
2236 /// If it is explicitly handled, return the case number of it, otherwise
2237 /// return 0 to indicate that it is handled by the default handler.
2238 unsigned findCaseValue(const ConstantInt *C) const {
2239 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2240 if (getCaseValue(i) == C)
2245 /// findCaseDest - Finds the unique case value for a given successor. Returns
2246 /// null if the successor is not found, not unique, or is the default case.
2247 ConstantInt *findCaseDest(BasicBlock *BB) {
2248 if (BB == getDefaultDest()) return NULL;
2250 ConstantInt *CI = NULL;
2251 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2252 if (getSuccessor(i) == BB) {
2253 if (CI) return NULL; // Multiple cases lead to BB.
2254 else CI = getCaseValue(i);
2260 /// addCase - Add an entry to the switch instruction...
2262 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2264 /// removeCase - This method removes the specified successor from the switch
2265 /// instruction. Note that this cannot be used to remove the default
2266 /// destination (successor #0).
2268 void removeCase(unsigned idx);
2270 virtual SwitchInst *clone() const;
2272 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2273 BasicBlock *getSuccessor(unsigned idx) const {
2274 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2275 return cast<BasicBlock>(getOperand(idx*2+1));
2277 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2278 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2279 setOperand(idx*2+1, NewSucc);
2282 // getSuccessorValue - Return the value associated with the specified
2284 ConstantInt *getSuccessorValue(unsigned idx) const {
2285 assert(idx < getNumSuccessors() && "Successor # out of range!");
2286 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2289 // Methods for support type inquiry through isa, cast, and dyn_cast:
2290 static inline bool classof(const SwitchInst *) { return true; }
2291 static inline bool classof(const Instruction *I) {
2292 return I->getOpcode() == Instruction::Switch;
2294 static inline bool classof(const Value *V) {
2295 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2298 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2299 virtual unsigned getNumSuccessorsV() const;
2300 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2304 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2307 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2310 //===----------------------------------------------------------------------===//
2312 //===----------------------------------------------------------------------===//
2314 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2315 /// calling convention of the call.
2317 class InvokeInst : public TerminatorInst {
2318 AttrListPtr AttributeList;
2319 InvokeInst(const InvokeInst &BI);
2320 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2321 Value* const *Args, unsigned NumArgs);
2323 template<typename InputIterator>
2324 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2325 InputIterator ArgBegin, InputIterator ArgEnd,
2326 const Twine &NameStr,
2327 // This argument ensures that we have an iterator we can
2328 // do arithmetic on in constant time
2329 std::random_access_iterator_tag) {
2330 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2332 // This requires that the iterator points to contiguous memory.
2333 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2337 /// Construct an InvokeInst given a range of arguments.
2338 /// InputIterator must be a random-access iterator pointing to
2339 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2340 /// made for random-accessness but not for contiguous storage as
2341 /// that would incur runtime overhead.
2343 /// @brief Construct an InvokeInst from a range of arguments
2344 template<typename InputIterator>
2345 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2346 InputIterator ArgBegin, InputIterator ArgEnd,
2348 const Twine &NameStr, Instruction *InsertBefore);
2350 /// Construct an InvokeInst given a range of arguments.
2351 /// InputIterator must be a random-access iterator pointing to
2352 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2353 /// made for random-accessness but not for contiguous storage as
2354 /// that would incur runtime overhead.
2356 /// @brief Construct an InvokeInst from a range of arguments
2357 template<typename InputIterator>
2358 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2359 InputIterator ArgBegin, InputIterator ArgEnd,
2361 const Twine &NameStr, BasicBlock *InsertAtEnd);
2363 template<typename InputIterator>
2364 static InvokeInst *Create(Value *Func,
2365 BasicBlock *IfNormal, BasicBlock *IfException,
2366 InputIterator ArgBegin, InputIterator ArgEnd,
2367 const Twine &NameStr = "",
2368 Instruction *InsertBefore = 0) {
2369 unsigned Values(ArgEnd - ArgBegin + 3);
2370 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2371 Values, NameStr, InsertBefore);
2373 template<typename InputIterator>
2374 static InvokeInst *Create(Value *Func,
2375 BasicBlock *IfNormal, BasicBlock *IfException,
2376 InputIterator ArgBegin, InputIterator ArgEnd,
2377 const Twine &NameStr,
2378 BasicBlock *InsertAtEnd) {
2379 unsigned Values(ArgEnd - ArgBegin + 3);
2380 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2381 Values, NameStr, InsertAtEnd);
2384 virtual InvokeInst *clone() const;
2386 /// Provide fast operand accessors
2387 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2389 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2391 CallingConv::ID getCallingConv() const {
2392 return static_cast<CallingConv::ID>(SubclassData);
2394 void setCallingConv(CallingConv::ID CC) {
2395 SubclassData = static_cast<unsigned>(CC);
2398 /// getAttributes - Return the parameter attributes for this invoke.
2400 const AttrListPtr &getAttributes() const { return AttributeList; }
2402 /// setAttributes - Set the parameter attributes for this invoke.
2404 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2406 /// addAttribute - adds the attribute to the list of attributes.
2407 void addAttribute(unsigned i, Attributes attr);
2409 /// removeAttribute - removes the attribute from the list of attributes.
2410 void removeAttribute(unsigned i, Attributes attr);
2412 /// @brief Determine whether the call or the callee has the given attribute.
2413 bool paramHasAttr(unsigned i, Attributes attr) const;
2415 /// @brief Extract the alignment for a call or parameter (0=unknown).
2416 unsigned getParamAlignment(unsigned i) const {
2417 return AttributeList.getParamAlignment(i);
2420 /// @brief Determine if the call does not access memory.
2421 bool doesNotAccessMemory() const {
2422 return paramHasAttr(~0, Attribute::ReadNone);
2424 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2425 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2426 else removeAttribute(~0, Attribute::ReadNone);
2429 /// @brief Determine if the call does not access or only reads memory.
2430 bool onlyReadsMemory() const {
2431 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2433 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2434 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2435 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2438 /// @brief Determine if the call cannot return.
2439 bool doesNotReturn() const {
2440 return paramHasAttr(~0, Attribute::NoReturn);
2442 void setDoesNotReturn(bool DoesNotReturn = true) {
2443 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2444 else removeAttribute(~0, Attribute::NoReturn);
2447 /// @brief Determine if the call cannot unwind.
2448 bool doesNotThrow() const {
2449 return paramHasAttr(~0, Attribute::NoUnwind);
2451 void setDoesNotThrow(bool DoesNotThrow = true) {
2452 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2453 else removeAttribute(~0, Attribute::NoUnwind);
2456 /// @brief Determine if the call returns a structure through first
2457 /// pointer argument.
2458 bool hasStructRetAttr() const {
2459 // Be friendly and also check the callee.
2460 return paramHasAttr(1, Attribute::StructRet);
2463 /// @brief Determine if any call argument is an aggregate passed by value.
2464 bool hasByValArgument() const {
2465 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2468 /// getCalledFunction - Return the function called, or null if this is an
2469 /// indirect function invocation.
2471 Function *getCalledFunction() const {
2472 return dyn_cast<Function>(getOperand(0));
2475 /// getCalledValue - Get a pointer to the function that is invoked by this
2477 const Value *getCalledValue() const { return getOperand(0); }
2478 Value *getCalledValue() { return getOperand(0); }
2480 // get*Dest - Return the destination basic blocks...
2481 BasicBlock *getNormalDest() const {
2482 return cast<BasicBlock>(getOperand(1));
2484 BasicBlock *getUnwindDest() const {
2485 return cast<BasicBlock>(getOperand(2));
2487 void setNormalDest(BasicBlock *B) {
2491 void setUnwindDest(BasicBlock *B) {
2495 BasicBlock *getSuccessor(unsigned i) const {
2496 assert(i < 2 && "Successor # out of range for invoke!");
2497 return i == 0 ? getNormalDest() : getUnwindDest();
2500 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2501 assert(idx < 2 && "Successor # out of range for invoke!");
2502 setOperand(idx+1, NewSucc);
2505 unsigned getNumSuccessors() const { return 2; }
2507 // Methods for support type inquiry through isa, cast, and dyn_cast:
2508 static inline bool classof(const InvokeInst *) { return true; }
2509 static inline bool classof(const Instruction *I) {
2510 return (I->getOpcode() == Instruction::Invoke);
2512 static inline bool classof(const Value *V) {
2513 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2516 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2517 virtual unsigned getNumSuccessorsV() const;
2518 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2522 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2525 template<typename InputIterator>
2526 InvokeInst::InvokeInst(Value *Func,
2527 BasicBlock *IfNormal, BasicBlock *IfException,
2528 InputIterator ArgBegin, InputIterator ArgEnd,
2530 const Twine &NameStr, Instruction *InsertBefore)
2531 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2532 ->getElementType())->getReturnType(),
2533 Instruction::Invoke,
2534 OperandTraits<InvokeInst>::op_end(this) - Values,
2535 Values, InsertBefore) {
2536 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2537 typename std::iterator_traits<InputIterator>::iterator_category());
2539 template<typename InputIterator>
2540 InvokeInst::InvokeInst(Value *Func,
2541 BasicBlock *IfNormal, BasicBlock *IfException,
2542 InputIterator ArgBegin, InputIterator ArgEnd,
2544 const Twine &NameStr, BasicBlock *InsertAtEnd)
2545 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2546 ->getElementType())->getReturnType(),
2547 Instruction::Invoke,
2548 OperandTraits<InvokeInst>::op_end(this) - Values,
2549 Values, InsertAtEnd) {
2550 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2551 typename std::iterator_traits<InputIterator>::iterator_category());
2554 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2556 //===----------------------------------------------------------------------===//
2558 //===----------------------------------------------------------------------===//
2560 //===---------------------------------------------------------------------------
2561 /// UnwindInst - Immediately exit the current function, unwinding the stack
2562 /// until an invoke instruction is found.
2564 class UnwindInst : public TerminatorInst {
2565 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2567 // allocate space for exactly zero operands
2568 void *operator new(size_t s) {
2569 return User::operator new(s, 0);
2571 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2572 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2574 virtual UnwindInst *clone() const;
2576 unsigned getNumSuccessors() const { return 0; }
2578 // Methods for support type inquiry through isa, cast, and dyn_cast:
2579 static inline bool classof(const UnwindInst *) { return true; }
2580 static inline bool classof(const Instruction *I) {
2581 return I->getOpcode() == Instruction::Unwind;
2583 static inline bool classof(const Value *V) {
2584 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2587 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2588 virtual unsigned getNumSuccessorsV() const;
2589 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2592 //===----------------------------------------------------------------------===//
2593 // UnreachableInst Class
2594 //===----------------------------------------------------------------------===//
2596 //===---------------------------------------------------------------------------
2597 /// UnreachableInst - This function has undefined behavior. In particular, the
2598 /// presence of this instruction indicates some higher level knowledge that the
2599 /// end of the block cannot be reached.
2601 class UnreachableInst : public TerminatorInst {
2602 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2604 // allocate space for exactly zero operands
2605 void *operator new(size_t s) {
2606 return User::operator new(s, 0);
2608 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2609 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2611 virtual UnreachableInst *clone() const;
2613 unsigned getNumSuccessors() const { return 0; }
2615 // Methods for support type inquiry through isa, cast, and dyn_cast:
2616 static inline bool classof(const UnreachableInst *) { return true; }
2617 static inline bool classof(const Instruction *I) {
2618 return I->getOpcode() == Instruction::Unreachable;
2620 static inline bool classof(const Value *V) {
2621 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2624 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2625 virtual unsigned getNumSuccessorsV() const;
2626 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2629 //===----------------------------------------------------------------------===//
2631 //===----------------------------------------------------------------------===//
2633 /// @brief This class represents a truncation of integer types.
2634 class TruncInst : public CastInst {
2636 /// @brief Constructor with insert-before-instruction semantics
2638 Value *S, ///< The value to be truncated
2639 const Type *Ty, ///< The (smaller) type to truncate to
2640 const Twine &NameStr = "", ///< A name for the new instruction
2641 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2644 /// @brief Constructor with insert-at-end-of-block semantics
2646 Value *S, ///< The value to be truncated
2647 const Type *Ty, ///< The (smaller) type to truncate to
2648 const Twine &NameStr, ///< A name for the new instruction
2649 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2652 /// @brief Clone an identical TruncInst
2653 virtual TruncInst *clone() const;
2655 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2656 static inline bool classof(const TruncInst *) { return true; }
2657 static inline bool classof(const Instruction *I) {
2658 return I->getOpcode() == Trunc;
2660 static inline bool classof(const Value *V) {
2661 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2665 //===----------------------------------------------------------------------===//
2667 //===----------------------------------------------------------------------===//
2669 /// @brief This class represents zero extension of integer types.
2670 class ZExtInst : public CastInst {
2672 /// @brief Constructor with insert-before-instruction semantics
2674 Value *S, ///< The value to be zero extended
2675 const Type *Ty, ///< The type to zero extend to
2676 const Twine &NameStr = "", ///< A name for the new instruction
2677 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2680 /// @brief Constructor with insert-at-end semantics.
2682 Value *S, ///< The value to be zero extended
2683 const Type *Ty, ///< The type to zero extend to
2684 const Twine &NameStr, ///< A name for the new instruction
2685 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2688 /// @brief Clone an identical ZExtInst
2689 virtual ZExtInst *clone() const;
2691 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2692 static inline bool classof(const ZExtInst *) { return true; }
2693 static inline bool classof(const Instruction *I) {
2694 return I->getOpcode() == ZExt;
2696 static inline bool classof(const Value *V) {
2697 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2701 //===----------------------------------------------------------------------===//
2703 //===----------------------------------------------------------------------===//
2705 /// @brief This class represents a sign extension of integer types.
2706 class SExtInst : public CastInst {
2708 /// @brief Constructor with insert-before-instruction semantics
2710 Value *S, ///< The value to be sign extended
2711 const Type *Ty, ///< The type to sign extend to
2712 const Twine &NameStr = "", ///< A name for the new instruction
2713 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2716 /// @brief Constructor with insert-at-end-of-block semantics
2718 Value *S, ///< The value to be sign extended
2719 const Type *Ty, ///< The type to sign extend to
2720 const Twine &NameStr, ///< A name for the new instruction
2721 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2724 /// @brief Clone an identical SExtInst
2725 virtual SExtInst *clone() const;
2727 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2728 static inline bool classof(const SExtInst *) { return true; }
2729 static inline bool classof(const Instruction *I) {
2730 return I->getOpcode() == SExt;
2732 static inline bool classof(const Value *V) {
2733 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2737 //===----------------------------------------------------------------------===//
2738 // FPTruncInst Class
2739 //===----------------------------------------------------------------------===//
2741 /// @brief This class represents a truncation of floating point types.
2742 class FPTruncInst : public CastInst {
2744 /// @brief Constructor with insert-before-instruction semantics
2746 Value *S, ///< The value to be truncated
2747 const Type *Ty, ///< The type to truncate to
2748 const Twine &NameStr = "", ///< A name for the new instruction
2749 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2752 /// @brief Constructor with insert-before-instruction semantics
2754 Value *S, ///< The value to be truncated
2755 const Type *Ty, ///< The type to truncate to
2756 const Twine &NameStr, ///< A name for the new instruction
2757 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2760 /// @brief Clone an identical FPTruncInst
2761 virtual FPTruncInst *clone() const;
2763 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2764 static inline bool classof(const FPTruncInst *) { return true; }
2765 static inline bool classof(const Instruction *I) {
2766 return I->getOpcode() == FPTrunc;
2768 static inline bool classof(const Value *V) {
2769 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2773 //===----------------------------------------------------------------------===//
2775 //===----------------------------------------------------------------------===//
2777 /// @brief This class represents an extension of floating point types.
2778 class FPExtInst : public CastInst {
2780 /// @brief Constructor with insert-before-instruction semantics
2782 Value *S, ///< The value to be extended
2783 const Type *Ty, ///< The type to extend to
2784 const Twine &NameStr = "", ///< A name for the new instruction
2785 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2788 /// @brief Constructor with insert-at-end-of-block semantics
2790 Value *S, ///< The value to be extended
2791 const Type *Ty, ///< The type to extend to
2792 const Twine &NameStr, ///< A name for the new instruction
2793 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2796 /// @brief Clone an identical FPExtInst
2797 virtual FPExtInst *clone() const;
2799 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2800 static inline bool classof(const FPExtInst *) { return true; }
2801 static inline bool classof(const Instruction *I) {
2802 return I->getOpcode() == FPExt;
2804 static inline bool classof(const Value *V) {
2805 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2809 //===----------------------------------------------------------------------===//
2811 //===----------------------------------------------------------------------===//
2813 /// @brief This class represents a cast unsigned integer to floating point.
2814 class UIToFPInst : public CastInst {
2816 /// @brief Constructor with insert-before-instruction semantics
2818 Value *S, ///< The value to be converted
2819 const Type *Ty, ///< The type to convert to
2820 const Twine &NameStr = "", ///< A name for the new instruction
2821 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2824 /// @brief Constructor with insert-at-end-of-block semantics
2826 Value *S, ///< The value to be converted
2827 const Type *Ty, ///< The type to convert to
2828 const Twine &NameStr, ///< A name for the new instruction
2829 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2832 /// @brief Clone an identical UIToFPInst
2833 virtual UIToFPInst *clone() const;
2835 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2836 static inline bool classof(const UIToFPInst *) { return true; }
2837 static inline bool classof(const Instruction *I) {
2838 return I->getOpcode() == UIToFP;
2840 static inline bool classof(const Value *V) {
2841 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2845 //===----------------------------------------------------------------------===//
2847 //===----------------------------------------------------------------------===//
2849 /// @brief This class represents a cast from signed integer to floating point.
2850 class SIToFPInst : public CastInst {
2852 /// @brief Constructor with insert-before-instruction semantics
2854 Value *S, ///< The value to be converted
2855 const Type *Ty, ///< The type to convert to
2856 const Twine &NameStr = "", ///< A name for the new instruction
2857 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2860 /// @brief Constructor with insert-at-end-of-block semantics
2862 Value *S, ///< The value to be converted
2863 const Type *Ty, ///< The type to convert to
2864 const Twine &NameStr, ///< A name for the new instruction
2865 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2868 /// @brief Clone an identical SIToFPInst
2869 virtual SIToFPInst *clone() const;
2871 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2872 static inline bool classof(const SIToFPInst *) { return true; }
2873 static inline bool classof(const Instruction *I) {
2874 return I->getOpcode() == SIToFP;
2876 static inline bool classof(const Value *V) {
2877 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2881 //===----------------------------------------------------------------------===//
2883 //===----------------------------------------------------------------------===//
2885 /// @brief This class represents a cast from floating point to unsigned integer
2886 class FPToUIInst : public CastInst {
2888 /// @brief Constructor with insert-before-instruction semantics
2890 Value *S, ///< The value to be converted
2891 const Type *Ty, ///< The type to convert to
2892 const Twine &NameStr = "", ///< A name for the new instruction
2893 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2896 /// @brief Constructor with insert-at-end-of-block semantics
2898 Value *S, ///< The value to be converted
2899 const Type *Ty, ///< The type to convert to
2900 const Twine &NameStr, ///< A name for the new instruction
2901 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2904 /// @brief Clone an identical FPToUIInst
2905 virtual FPToUIInst *clone() const;
2907 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2908 static inline bool classof(const FPToUIInst *) { return true; }
2909 static inline bool classof(const Instruction *I) {
2910 return I->getOpcode() == FPToUI;
2912 static inline bool classof(const Value *V) {
2913 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2917 //===----------------------------------------------------------------------===//
2919 //===----------------------------------------------------------------------===//
2921 /// @brief This class represents a cast from floating point to signed integer.
2922 class FPToSIInst : public CastInst {
2924 /// @brief Constructor with insert-before-instruction semantics
2926 Value *S, ///< The value to be converted
2927 const Type *Ty, ///< The type to convert to
2928 const Twine &NameStr = "", ///< A name for the new instruction
2929 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2932 /// @brief Constructor with insert-at-end-of-block semantics
2934 Value *S, ///< The value to be converted
2935 const Type *Ty, ///< The type to convert to
2936 const Twine &NameStr, ///< A name for the new instruction
2937 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2940 /// @brief Clone an identical FPToSIInst
2941 virtual FPToSIInst *clone() const;
2943 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2944 static inline bool classof(const FPToSIInst *) { return true; }
2945 static inline bool classof(const Instruction *I) {
2946 return I->getOpcode() == FPToSI;
2948 static inline bool classof(const Value *V) {
2949 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2953 //===----------------------------------------------------------------------===//
2954 // IntToPtrInst Class
2955 //===----------------------------------------------------------------------===//
2957 /// @brief This class represents a cast from an integer to a pointer.
2958 class IntToPtrInst : public CastInst {
2960 /// @brief Constructor with insert-before-instruction semantics
2962 Value *S, ///< The value to be converted
2963 const Type *Ty, ///< The type to convert to
2964 const Twine &NameStr = "", ///< A name for the new instruction
2965 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2968 /// @brief Constructor with insert-at-end-of-block semantics
2970 Value *S, ///< The value to be converted
2971 const Type *Ty, ///< The type to convert to
2972 const Twine &NameStr, ///< A name for the new instruction
2973 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2976 /// @brief Clone an identical IntToPtrInst
2977 virtual IntToPtrInst *clone() const;
2979 // Methods for support type inquiry through isa, cast, and dyn_cast:
2980 static inline bool classof(const IntToPtrInst *) { return true; }
2981 static inline bool classof(const Instruction *I) {
2982 return I->getOpcode() == IntToPtr;
2984 static inline bool classof(const Value *V) {
2985 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2989 //===----------------------------------------------------------------------===//
2990 // PtrToIntInst Class
2991 //===----------------------------------------------------------------------===//
2993 /// @brief This class represents a cast from a pointer to an integer
2994 class PtrToIntInst : public CastInst {
2996 /// @brief Constructor with insert-before-instruction semantics
2998 Value *S, ///< The value to be converted
2999 const Type *Ty, ///< The type to convert to
3000 const Twine &NameStr = "", ///< A name for the new instruction
3001 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3004 /// @brief Constructor with insert-at-end-of-block semantics
3006 Value *S, ///< The value to be converted
3007 const Type *Ty, ///< The type to convert to
3008 const Twine &NameStr, ///< A name for the new instruction
3009 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3012 /// @brief Clone an identical PtrToIntInst
3013 virtual PtrToIntInst *clone() const;
3015 // Methods for support type inquiry through isa, cast, and dyn_cast:
3016 static inline bool classof(const PtrToIntInst *) { return true; }
3017 static inline bool classof(const Instruction *I) {
3018 return I->getOpcode() == PtrToInt;
3020 static inline bool classof(const Value *V) {
3021 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3025 //===----------------------------------------------------------------------===//
3026 // BitCastInst Class
3027 //===----------------------------------------------------------------------===//
3029 /// @brief This class represents a no-op cast from one type to another.
3030 class BitCastInst : public CastInst {
3032 /// @brief Constructor with insert-before-instruction semantics
3034 Value *S, ///< The value to be casted
3035 const Type *Ty, ///< The type to casted to
3036 const Twine &NameStr = "", ///< A name for the new instruction
3037 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3040 /// @brief Constructor with insert-at-end-of-block semantics
3042 Value *S, ///< The value to be casted
3043 const Type *Ty, ///< The type to casted to
3044 const Twine &NameStr, ///< A name for the new instruction
3045 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3048 /// @brief Clone an identical BitCastInst
3049 virtual BitCastInst *clone() const;
3051 // Methods for support type inquiry through isa, cast, and dyn_cast:
3052 static inline bool classof(const BitCastInst *) { return true; }
3053 static inline bool classof(const Instruction *I) {
3054 return I->getOpcode() == BitCast;
3056 static inline bool classof(const Value *V) {
3057 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3061 } // End llvm namespace