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 Instruction *CreateMalloc(Instruction *InsertBefore,
996 const Type *IntPtrTy, const Type *AllocTy,
997 Value *ArraySize = 0,
998 const Twine &Name = "");
999 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
1000 const Type *IntPtrTy, const Type *AllocTy,
1001 Value *ArraySize = 0, Function* MallocF = 0,
1002 const Twine &Name = "");
1006 bool isTailCall() const { return SubclassData & 1; }
1007 void setTailCall(bool isTC = true) {
1008 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1011 virtual CallInst *clone() const;
1013 /// Provide fast operand accessors
1014 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1016 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1018 CallingConv::ID getCallingConv() const {
1019 return static_cast<CallingConv::ID>(SubclassData >> 1);
1021 void setCallingConv(CallingConv::ID CC) {
1022 SubclassData = (SubclassData & 1) | (static_cast<unsigned>(CC) << 1);
1025 /// getAttributes - Return the parameter attributes for this call.
1027 const AttrListPtr &getAttributes() const { return AttributeList; }
1029 /// setAttributes - Set the parameter attributes for this call.
1031 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1033 /// addAttribute - adds the attribute to the list of attributes.
1034 void addAttribute(unsigned i, Attributes attr);
1036 /// removeAttribute - removes the attribute from the list of attributes.
1037 void removeAttribute(unsigned i, Attributes attr);
1039 /// @brief Determine whether the call or the callee has the given attribute.
1040 bool paramHasAttr(unsigned i, Attributes attr) const;
1042 /// @brief Extract the alignment for a call or parameter (0=unknown).
1043 unsigned getParamAlignment(unsigned i) const {
1044 return AttributeList.getParamAlignment(i);
1047 /// @brief Determine if the call does not access memory.
1048 bool doesNotAccessMemory() const {
1049 return paramHasAttr(~0, Attribute::ReadNone);
1051 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1052 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1053 else removeAttribute(~0, Attribute::ReadNone);
1056 /// @brief Determine if the call does not access or only reads memory.
1057 bool onlyReadsMemory() const {
1058 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1060 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1061 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1062 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1065 /// @brief Determine if the call cannot return.
1066 bool doesNotReturn() const {
1067 return paramHasAttr(~0, Attribute::NoReturn);
1069 void setDoesNotReturn(bool DoesNotReturn = true) {
1070 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1071 else removeAttribute(~0, Attribute::NoReturn);
1074 /// @brief Determine if the call cannot unwind.
1075 bool doesNotThrow() const {
1076 return paramHasAttr(~0, Attribute::NoUnwind);
1078 void setDoesNotThrow(bool DoesNotThrow = true) {
1079 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1080 else removeAttribute(~0, Attribute::NoUnwind);
1083 /// @brief Determine if the call returns a structure through first
1084 /// pointer argument.
1085 bool hasStructRetAttr() const {
1086 // Be friendly and also check the callee.
1087 return paramHasAttr(1, Attribute::StructRet);
1090 /// @brief Determine if any call argument is an aggregate passed by value.
1091 bool hasByValArgument() const {
1092 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1095 /// getCalledFunction - Return the function called, or null if this is an
1096 /// indirect function invocation.
1098 Function *getCalledFunction() const {
1099 return dyn_cast<Function>(Op<0>());
1102 /// getCalledValue - Get a pointer to the function that is invoked by this
1104 const Value *getCalledValue() const { return Op<0>(); }
1105 Value *getCalledValue() { return Op<0>(); }
1107 /// setCalledFunction - Set the function called.
1108 void setCalledFunction(Value* Fn) {
1112 // Methods for support type inquiry through isa, cast, and dyn_cast:
1113 static inline bool classof(const CallInst *) { return true; }
1114 static inline bool classof(const Instruction *I) {
1115 return I->getOpcode() == Instruction::Call;
1117 static inline bool classof(const Value *V) {
1118 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1123 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1126 template<typename InputIterator>
1127 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1128 const Twine &NameStr, BasicBlock *InsertAtEnd)
1129 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1130 ->getElementType())->getReturnType(),
1132 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1133 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1134 init(Func, ArgBegin, ArgEnd, NameStr,
1135 typename std::iterator_traits<InputIterator>::iterator_category());
1138 template<typename InputIterator>
1139 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1140 const Twine &NameStr, Instruction *InsertBefore)
1141 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1142 ->getElementType())->getReturnType(),
1144 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1145 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1146 init(Func, ArgBegin, ArgEnd, NameStr,
1147 typename std::iterator_traits<InputIterator>::iterator_category());
1150 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1152 //===----------------------------------------------------------------------===//
1154 //===----------------------------------------------------------------------===//
1156 /// SelectInst - This class represents the LLVM 'select' instruction.
1158 class SelectInst : public Instruction {
1159 void init(Value *C, Value *S1, Value *S2) {
1160 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1166 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1167 Instruction *InsertBefore)
1168 : Instruction(S1->getType(), Instruction::Select,
1169 &Op<0>(), 3, InsertBefore) {
1173 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1174 BasicBlock *InsertAtEnd)
1175 : Instruction(S1->getType(), Instruction::Select,
1176 &Op<0>(), 3, InsertAtEnd) {
1181 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1182 const Twine &NameStr = "",
1183 Instruction *InsertBefore = 0) {
1184 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1186 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1187 const Twine &NameStr,
1188 BasicBlock *InsertAtEnd) {
1189 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1192 const Value *getCondition() const { return Op<0>(); }
1193 const Value *getTrueValue() const { return Op<1>(); }
1194 const Value *getFalseValue() const { return Op<2>(); }
1195 Value *getCondition() { return Op<0>(); }
1196 Value *getTrueValue() { return Op<1>(); }
1197 Value *getFalseValue() { return Op<2>(); }
1199 /// areInvalidOperands - Return a string if the specified operands are invalid
1200 /// for a select operation, otherwise return null.
1201 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1203 /// Transparently provide more efficient getOperand methods.
1204 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1206 OtherOps getOpcode() const {
1207 return static_cast<OtherOps>(Instruction::getOpcode());
1210 virtual SelectInst *clone() const;
1212 // Methods for support type inquiry through isa, cast, and dyn_cast:
1213 static inline bool classof(const SelectInst *) { return true; }
1214 static inline bool classof(const Instruction *I) {
1215 return I->getOpcode() == Instruction::Select;
1217 static inline bool classof(const Value *V) {
1218 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1223 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1226 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1228 //===----------------------------------------------------------------------===//
1230 //===----------------------------------------------------------------------===//
1232 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1233 /// an argument of the specified type given a va_list and increments that list
1235 class VAArgInst : public UnaryInstruction {
1237 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1238 Instruction *InsertBefore = 0)
1239 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1242 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1243 BasicBlock *InsertAtEnd)
1244 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1248 virtual VAArgInst *clone() const;
1250 // Methods for support type inquiry through isa, cast, and dyn_cast:
1251 static inline bool classof(const VAArgInst *) { return true; }
1252 static inline bool classof(const Instruction *I) {
1253 return I->getOpcode() == VAArg;
1255 static inline bool classof(const Value *V) {
1256 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1260 //===----------------------------------------------------------------------===//
1261 // ExtractElementInst Class
1262 //===----------------------------------------------------------------------===//
1264 /// ExtractElementInst - This instruction extracts a single (scalar)
1265 /// element from a VectorType value
1267 class ExtractElementInst : public Instruction {
1268 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1269 Instruction *InsertBefore = 0);
1270 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1271 BasicBlock *InsertAtEnd);
1273 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1274 const Twine &NameStr = "",
1275 Instruction *InsertBefore = 0) {
1276 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1278 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1279 const Twine &NameStr,
1280 BasicBlock *InsertAtEnd) {
1281 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1284 /// isValidOperands - Return true if an extractelement instruction can be
1285 /// formed with the specified operands.
1286 static bool isValidOperands(const Value *Vec, const Value *Idx);
1288 virtual ExtractElementInst *clone() const;
1290 Value *getVectorOperand() { return Op<0>(); }
1291 Value *getIndexOperand() { return Op<1>(); }
1292 const Value *getVectorOperand() const { return Op<0>(); }
1293 const Value *getIndexOperand() const { return Op<1>(); }
1295 const VectorType *getVectorOperandType() const {
1296 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1300 /// Transparently provide more efficient getOperand methods.
1301 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1303 // Methods for support type inquiry through isa, cast, and dyn_cast:
1304 static inline bool classof(const ExtractElementInst *) { return true; }
1305 static inline bool classof(const Instruction *I) {
1306 return I->getOpcode() == Instruction::ExtractElement;
1308 static inline bool classof(const Value *V) {
1309 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1314 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1317 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1319 //===----------------------------------------------------------------------===//
1320 // InsertElementInst Class
1321 //===----------------------------------------------------------------------===//
1323 /// InsertElementInst - This instruction inserts a single (scalar)
1324 /// element into a VectorType value
1326 class InsertElementInst : public Instruction {
1327 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1328 const Twine &NameStr = "",
1329 Instruction *InsertBefore = 0);
1330 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1331 const Twine &NameStr, BasicBlock *InsertAtEnd);
1333 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1334 const Twine &NameStr = "",
1335 Instruction *InsertBefore = 0) {
1336 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1338 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1339 const Twine &NameStr,
1340 BasicBlock *InsertAtEnd) {
1341 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1344 /// isValidOperands - Return true if an insertelement instruction can be
1345 /// formed with the specified operands.
1346 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1349 virtual InsertElementInst *clone() const;
1351 /// getType - Overload to return most specific vector type.
1353 const VectorType *getType() const {
1354 return reinterpret_cast<const VectorType*>(Instruction::getType());
1357 /// Transparently provide more efficient getOperand methods.
1358 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1360 // Methods for support type inquiry through isa, cast, and dyn_cast:
1361 static inline bool classof(const InsertElementInst *) { return true; }
1362 static inline bool classof(const Instruction *I) {
1363 return I->getOpcode() == Instruction::InsertElement;
1365 static inline bool classof(const Value *V) {
1366 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1371 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1374 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1376 //===----------------------------------------------------------------------===//
1377 // ShuffleVectorInst Class
1378 //===----------------------------------------------------------------------===//
1380 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1383 class ShuffleVectorInst : public Instruction {
1385 // allocate space for exactly three operands
1386 void *operator new(size_t s) {
1387 return User::operator new(s, 3);
1389 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1390 const Twine &NameStr = "",
1391 Instruction *InsertBefor = 0);
1392 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1393 const Twine &NameStr, BasicBlock *InsertAtEnd);
1395 /// isValidOperands - Return true if a shufflevector instruction can be
1396 /// formed with the specified operands.
1397 static bool isValidOperands(const Value *V1, const Value *V2,
1400 virtual ShuffleVectorInst *clone() const;
1402 /// getType - Overload to return most specific vector type.
1404 const VectorType *getType() const {
1405 return reinterpret_cast<const VectorType*>(Instruction::getType());
1408 /// Transparently provide more efficient getOperand methods.
1409 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1411 /// getMaskValue - Return the index from the shuffle mask for the specified
1412 /// output result. This is either -1 if the element is undef or a number less
1413 /// than 2*numelements.
1414 int getMaskValue(unsigned i) const;
1416 // Methods for support type inquiry through isa, cast, and dyn_cast:
1417 static inline bool classof(const ShuffleVectorInst *) { return true; }
1418 static inline bool classof(const Instruction *I) {
1419 return I->getOpcode() == Instruction::ShuffleVector;
1421 static inline bool classof(const Value *V) {
1422 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1427 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1430 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1432 //===----------------------------------------------------------------------===//
1433 // ExtractValueInst Class
1434 //===----------------------------------------------------------------------===//
1436 /// ExtractValueInst - This instruction extracts a struct member or array
1437 /// element value from an aggregate value.
1439 class ExtractValueInst : public UnaryInstruction {
1440 SmallVector<unsigned, 4> Indices;
1442 ExtractValueInst(const ExtractValueInst &EVI);
1443 void init(const unsigned *Idx, unsigned NumIdx,
1444 const Twine &NameStr);
1445 void init(unsigned Idx, const Twine &NameStr);
1447 template<typename InputIterator>
1448 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1449 const Twine &NameStr,
1450 // This argument ensures that we have an iterator we can
1451 // do arithmetic on in constant time
1452 std::random_access_iterator_tag) {
1453 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1455 // There's no fundamental reason why we require at least one index
1456 // (other than weirdness with &*IdxBegin being invalid; see
1457 // getelementptr's init routine for example). But there's no
1458 // present need to support it.
1459 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1461 // This requires that the iterator points to contiguous memory.
1462 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1463 // we have to build an array here
1466 /// getIndexedType - Returns the type of the element that would be extracted
1467 /// with an extractvalue instruction with the specified parameters.
1469 /// Null is returned if the indices are invalid for the specified
1472 static const Type *getIndexedType(const Type *Agg,
1473 const unsigned *Idx, unsigned NumIdx);
1475 template<typename InputIterator>
1476 static const Type *getIndexedType(const Type *Ptr,
1477 InputIterator IdxBegin,
1478 InputIterator IdxEnd,
1479 // This argument ensures that we
1480 // have an iterator we can do
1481 // arithmetic on in constant time
1482 std::random_access_iterator_tag) {
1483 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1486 // This requires that the iterator points to contiguous memory.
1487 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1489 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1492 /// Constructors - Create a extractvalue instruction with a base aggregate
1493 /// value and a list of indices. The first ctor can optionally insert before
1494 /// an existing instruction, the second appends the new instruction to the
1495 /// specified BasicBlock.
1496 template<typename InputIterator>
1497 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1498 InputIterator IdxEnd,
1499 const Twine &NameStr,
1500 Instruction *InsertBefore);
1501 template<typename InputIterator>
1502 inline ExtractValueInst(Value *Agg,
1503 InputIterator IdxBegin, InputIterator IdxEnd,
1504 const Twine &NameStr, BasicBlock *InsertAtEnd);
1506 // allocate space for exactly one operand
1507 void *operator new(size_t s) {
1508 return User::operator new(s, 1);
1512 template<typename InputIterator>
1513 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1514 InputIterator IdxEnd,
1515 const Twine &NameStr = "",
1516 Instruction *InsertBefore = 0) {
1518 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1520 template<typename InputIterator>
1521 static ExtractValueInst *Create(Value *Agg,
1522 InputIterator IdxBegin, InputIterator IdxEnd,
1523 const Twine &NameStr,
1524 BasicBlock *InsertAtEnd) {
1525 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1528 /// Constructors - These two creators are convenience methods because one
1529 /// index extractvalue instructions are much more common than those with
1531 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1532 const Twine &NameStr = "",
1533 Instruction *InsertBefore = 0) {
1534 unsigned Idxs[1] = { Idx };
1535 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1537 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1538 const Twine &NameStr,
1539 BasicBlock *InsertAtEnd) {
1540 unsigned Idxs[1] = { Idx };
1541 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1544 virtual ExtractValueInst *clone() const;
1546 /// getIndexedType - Returns the type of the element that would be extracted
1547 /// with an extractvalue instruction with the specified parameters.
1549 /// Null is returned if the indices are invalid for the specified
1552 template<typename InputIterator>
1553 static const Type *getIndexedType(const Type *Ptr,
1554 InputIterator IdxBegin,
1555 InputIterator IdxEnd) {
1556 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1557 typename std::iterator_traits<InputIterator>::
1558 iterator_category());
1560 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1562 typedef const unsigned* idx_iterator;
1563 inline idx_iterator idx_begin() const { return Indices.begin(); }
1564 inline idx_iterator idx_end() const { return Indices.end(); }
1566 Value *getAggregateOperand() {
1567 return getOperand(0);
1569 const Value *getAggregateOperand() const {
1570 return getOperand(0);
1572 static unsigned getAggregateOperandIndex() {
1573 return 0U; // get index for modifying correct operand
1576 unsigned getNumIndices() const { // Note: always non-negative
1577 return (unsigned)Indices.size();
1580 bool hasIndices() const {
1584 // Methods for support type inquiry through isa, cast, and dyn_cast:
1585 static inline bool classof(const ExtractValueInst *) { return true; }
1586 static inline bool classof(const Instruction *I) {
1587 return I->getOpcode() == Instruction::ExtractValue;
1589 static inline bool classof(const Value *V) {
1590 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1594 template<typename InputIterator>
1595 ExtractValueInst::ExtractValueInst(Value *Agg,
1596 InputIterator IdxBegin,
1597 InputIterator IdxEnd,
1598 const Twine &NameStr,
1599 Instruction *InsertBefore)
1600 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1602 ExtractValue, Agg, InsertBefore) {
1603 init(IdxBegin, IdxEnd, NameStr,
1604 typename std::iterator_traits<InputIterator>::iterator_category());
1606 template<typename InputIterator>
1607 ExtractValueInst::ExtractValueInst(Value *Agg,
1608 InputIterator IdxBegin,
1609 InputIterator IdxEnd,
1610 const Twine &NameStr,
1611 BasicBlock *InsertAtEnd)
1612 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1614 ExtractValue, Agg, InsertAtEnd) {
1615 init(IdxBegin, IdxEnd, NameStr,
1616 typename std::iterator_traits<InputIterator>::iterator_category());
1620 //===----------------------------------------------------------------------===//
1621 // InsertValueInst Class
1622 //===----------------------------------------------------------------------===//
1624 /// InsertValueInst - This instruction inserts a struct field of array element
1625 /// value into an aggregate value.
1627 class InsertValueInst : public Instruction {
1628 SmallVector<unsigned, 4> Indices;
1630 void *operator new(size_t, unsigned); // Do not implement
1631 InsertValueInst(const InsertValueInst &IVI);
1632 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1633 const Twine &NameStr);
1634 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1636 template<typename InputIterator>
1637 void init(Value *Agg, Value *Val,
1638 InputIterator IdxBegin, InputIterator IdxEnd,
1639 const Twine &NameStr,
1640 // This argument ensures that we have an iterator we can
1641 // do arithmetic on in constant time
1642 std::random_access_iterator_tag) {
1643 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1645 // There's no fundamental reason why we require at least one index
1646 // (other than weirdness with &*IdxBegin being invalid; see
1647 // getelementptr's init routine for example). But there's no
1648 // present need to support it.
1649 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1651 // This requires that the iterator points to contiguous memory.
1652 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1653 // we have to build an array here
1656 /// Constructors - Create a insertvalue instruction with a base aggregate
1657 /// value, a value to insert, and a list of indices. The first ctor can
1658 /// optionally insert before an existing instruction, the second appends
1659 /// the new instruction to the specified BasicBlock.
1660 template<typename InputIterator>
1661 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1662 InputIterator IdxEnd,
1663 const Twine &NameStr,
1664 Instruction *InsertBefore);
1665 template<typename InputIterator>
1666 inline InsertValueInst(Value *Agg, Value *Val,
1667 InputIterator IdxBegin, InputIterator IdxEnd,
1668 const Twine &NameStr, BasicBlock *InsertAtEnd);
1670 /// Constructors - These two constructors are convenience methods because one
1671 /// and two index insertvalue instructions are so common.
1672 InsertValueInst(Value *Agg, Value *Val,
1673 unsigned Idx, const Twine &NameStr = "",
1674 Instruction *InsertBefore = 0);
1675 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1676 const Twine &NameStr, BasicBlock *InsertAtEnd);
1678 // allocate space for exactly two operands
1679 void *operator new(size_t s) {
1680 return User::operator new(s, 2);
1683 template<typename InputIterator>
1684 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1685 InputIterator IdxEnd,
1686 const Twine &NameStr = "",
1687 Instruction *InsertBefore = 0) {
1688 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1689 NameStr, InsertBefore);
1691 template<typename InputIterator>
1692 static InsertValueInst *Create(Value *Agg, Value *Val,
1693 InputIterator IdxBegin, InputIterator IdxEnd,
1694 const Twine &NameStr,
1695 BasicBlock *InsertAtEnd) {
1696 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1697 NameStr, InsertAtEnd);
1700 /// Constructors - These two creators are convenience methods because one
1701 /// index insertvalue instructions are much more common than those with
1703 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1704 const Twine &NameStr = "",
1705 Instruction *InsertBefore = 0) {
1706 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1708 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1709 const Twine &NameStr,
1710 BasicBlock *InsertAtEnd) {
1711 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1714 virtual InsertValueInst *clone() const;
1716 /// Transparently provide more efficient getOperand methods.
1717 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1719 typedef const unsigned* idx_iterator;
1720 inline idx_iterator idx_begin() const { return Indices.begin(); }
1721 inline idx_iterator idx_end() const { return Indices.end(); }
1723 Value *getAggregateOperand() {
1724 return getOperand(0);
1726 const Value *getAggregateOperand() const {
1727 return getOperand(0);
1729 static unsigned getAggregateOperandIndex() {
1730 return 0U; // get index for modifying correct operand
1733 Value *getInsertedValueOperand() {
1734 return getOperand(1);
1736 const Value *getInsertedValueOperand() const {
1737 return getOperand(1);
1739 static unsigned getInsertedValueOperandIndex() {
1740 return 1U; // get index for modifying correct operand
1743 unsigned getNumIndices() const { // Note: always non-negative
1744 return (unsigned)Indices.size();
1747 bool hasIndices() const {
1751 // Methods for support type inquiry through isa, cast, and dyn_cast:
1752 static inline bool classof(const InsertValueInst *) { return true; }
1753 static inline bool classof(const Instruction *I) {
1754 return I->getOpcode() == Instruction::InsertValue;
1756 static inline bool classof(const Value *V) {
1757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1762 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1765 template<typename InputIterator>
1766 InsertValueInst::InsertValueInst(Value *Agg,
1768 InputIterator IdxBegin,
1769 InputIterator IdxEnd,
1770 const Twine &NameStr,
1771 Instruction *InsertBefore)
1772 : Instruction(Agg->getType(), InsertValue,
1773 OperandTraits<InsertValueInst>::op_begin(this),
1775 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1776 typename std::iterator_traits<InputIterator>::iterator_category());
1778 template<typename InputIterator>
1779 InsertValueInst::InsertValueInst(Value *Agg,
1781 InputIterator IdxBegin,
1782 InputIterator IdxEnd,
1783 const Twine &NameStr,
1784 BasicBlock *InsertAtEnd)
1785 : Instruction(Agg->getType(), InsertValue,
1786 OperandTraits<InsertValueInst>::op_begin(this),
1788 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1789 typename std::iterator_traits<InputIterator>::iterator_category());
1792 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1794 //===----------------------------------------------------------------------===//
1796 //===----------------------------------------------------------------------===//
1798 // PHINode - The PHINode class is used to represent the magical mystical PHI
1799 // node, that can not exist in nature, but can be synthesized in a computer
1800 // scientist's overactive imagination.
1802 class PHINode : public Instruction {
1803 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1804 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1805 /// the number actually in use.
1806 unsigned ReservedSpace;
1807 PHINode(const PHINode &PN);
1808 // allocate space for exactly zero operands
1809 void *operator new(size_t s) {
1810 return User::operator new(s, 0);
1812 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1813 Instruction *InsertBefore = 0)
1814 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1819 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1820 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1825 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1826 Instruction *InsertBefore = 0) {
1827 return new PHINode(Ty, NameStr, InsertBefore);
1829 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1830 BasicBlock *InsertAtEnd) {
1831 return new PHINode(Ty, NameStr, InsertAtEnd);
1835 /// reserveOperandSpace - This method can be used to avoid repeated
1836 /// reallocation of PHI operand lists by reserving space for the correct
1837 /// number of operands before adding them. Unlike normal vector reserves,
1838 /// this method can also be used to trim the operand space.
1839 void reserveOperandSpace(unsigned NumValues) {
1840 resizeOperands(NumValues*2);
1843 virtual PHINode *clone() const;
1845 /// Provide fast operand accessors
1846 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1848 /// getNumIncomingValues - Return the number of incoming edges
1850 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1852 /// getIncomingValue - Return incoming value number x
1854 Value *getIncomingValue(unsigned i) const {
1855 assert(i*2 < getNumOperands() && "Invalid value number!");
1856 return getOperand(i*2);
1858 void setIncomingValue(unsigned i, Value *V) {
1859 assert(i*2 < getNumOperands() && "Invalid value number!");
1862 static unsigned getOperandNumForIncomingValue(unsigned i) {
1865 static unsigned getIncomingValueNumForOperand(unsigned i) {
1866 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1870 /// getIncomingBlock - Return incoming basic block #i.
1872 BasicBlock *getIncomingBlock(unsigned i) const {
1873 return cast<BasicBlock>(getOperand(i*2+1));
1876 /// getIncomingBlock - Return incoming basic block corresponding
1877 /// to an operand of the PHI.
1879 BasicBlock *getIncomingBlock(const Use &U) const {
1880 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1881 return cast<BasicBlock>((&U + 1)->get());
1884 /// getIncomingBlock - Return incoming basic block corresponding
1885 /// to value use iterator.
1887 template <typename U>
1888 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1889 return getIncomingBlock(I.getUse());
1893 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1894 setOperand(i*2+1, BB);
1896 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1899 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1900 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1904 /// addIncoming - Add an incoming value to the end of the PHI list
1906 void addIncoming(Value *V, BasicBlock *BB) {
1907 assert(V && "PHI node got a null value!");
1908 assert(BB && "PHI node got a null basic block!");
1909 assert(getType() == V->getType() &&
1910 "All operands to PHI node must be the same type as the PHI node!");
1911 unsigned OpNo = NumOperands;
1912 if (OpNo+2 > ReservedSpace)
1913 resizeOperands(0); // Get more space!
1914 // Initialize some new operands.
1915 NumOperands = OpNo+2;
1916 OperandList[OpNo] = V;
1917 OperandList[OpNo+1] = BB;
1920 /// removeIncomingValue - Remove an incoming value. This is useful if a
1921 /// predecessor basic block is deleted. The value removed is returned.
1923 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1924 /// is true), the PHI node is destroyed and any uses of it are replaced with
1925 /// dummy values. The only time there should be zero incoming values to a PHI
1926 /// node is when the block is dead, so this strategy is sound.
1928 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1930 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1931 int Idx = getBasicBlockIndex(BB);
1932 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1933 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1936 /// getBasicBlockIndex - Return the first index of the specified basic
1937 /// block in the value list for this PHI. Returns -1 if no instance.
1939 int getBasicBlockIndex(const BasicBlock *BB) const {
1940 Use *OL = OperandList;
1941 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1942 if (OL[i+1].get() == BB) return i/2;
1946 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1947 return getIncomingValue(getBasicBlockIndex(BB));
1950 /// hasConstantValue - If the specified PHI node always merges together the
1951 /// same value, return the value, otherwise return null.
1953 /// If the PHI has undef operands, but all the rest of the operands are
1954 /// some unique value, return that value if it can be proved that the
1955 /// value dominates the PHI. If DT is null, use a conservative check,
1956 /// otherwise use DT to test for dominance.
1958 Value *hasConstantValue(DominatorTree *DT = 0) const;
1960 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1961 static inline bool classof(const PHINode *) { return true; }
1962 static inline bool classof(const Instruction *I) {
1963 return I->getOpcode() == Instruction::PHI;
1965 static inline bool classof(const Value *V) {
1966 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1969 void resizeOperands(unsigned NumOperands);
1973 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1976 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1979 //===----------------------------------------------------------------------===//
1981 //===----------------------------------------------------------------------===//
1983 //===---------------------------------------------------------------------------
1984 /// ReturnInst - Return a value (possibly void), from a function. Execution
1985 /// does not continue in this function any longer.
1987 class ReturnInst : public TerminatorInst {
1988 ReturnInst(const ReturnInst &RI);
1991 // ReturnInst constructors:
1992 // ReturnInst() - 'ret void' instruction
1993 // ReturnInst( null) - 'ret void' instruction
1994 // ReturnInst(Value* X) - 'ret X' instruction
1995 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1996 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1997 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1998 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
2000 // NOTE: If the Value* passed is of type void then the constructor behaves as
2001 // if it was passed NULL.
2002 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
2003 Instruction *InsertBefore = 0);
2004 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
2005 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2007 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
2008 Instruction *InsertBefore = 0) {
2009 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
2011 static ReturnInst* Create(LLVMContext &C, Value *retVal,
2012 BasicBlock *InsertAtEnd) {
2013 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
2015 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
2016 return new(0) ReturnInst(C, InsertAtEnd);
2018 virtual ~ReturnInst();
2020 virtual ReturnInst *clone() const;
2022 /// Provide fast operand accessors
2023 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2025 /// Convenience accessor
2026 Value *getReturnValue(unsigned n = 0) const {
2027 return n < getNumOperands()
2032 unsigned getNumSuccessors() const { return 0; }
2034 // Methods for support type inquiry through isa, cast, and dyn_cast:
2035 static inline bool classof(const ReturnInst *) { return true; }
2036 static inline bool classof(const Instruction *I) {
2037 return (I->getOpcode() == Instruction::Ret);
2039 static inline bool classof(const Value *V) {
2040 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2043 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2044 virtual unsigned getNumSuccessorsV() const;
2045 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2049 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
2052 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2054 //===----------------------------------------------------------------------===//
2056 //===----------------------------------------------------------------------===//
2058 //===---------------------------------------------------------------------------
2059 /// BranchInst - Conditional or Unconditional Branch instruction.
2061 class BranchInst : public TerminatorInst {
2062 /// Ops list - Branches are strange. The operands are ordered:
2063 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2064 /// they don't have to check for cond/uncond branchness. These are mostly
2065 /// accessed relative from op_end().
2066 BranchInst(const BranchInst &BI);
2068 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2069 // BranchInst(BB *B) - 'br B'
2070 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2071 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2072 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2073 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2074 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2075 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2076 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2077 Instruction *InsertBefore = 0);
2078 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2079 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2080 BasicBlock *InsertAtEnd);
2082 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2083 return new(1, true) BranchInst(IfTrue, InsertBefore);
2085 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2086 Value *Cond, Instruction *InsertBefore = 0) {
2087 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2089 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2090 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2092 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2093 Value *Cond, BasicBlock *InsertAtEnd) {
2094 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2099 /// Transparently provide more efficient getOperand methods.
2100 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2102 virtual BranchInst *clone() const;
2104 bool isUnconditional() const { return getNumOperands() == 1; }
2105 bool isConditional() const { return getNumOperands() == 3; }
2107 Value *getCondition() const {
2108 assert(isConditional() && "Cannot get condition of an uncond branch!");
2112 void setCondition(Value *V) {
2113 assert(isConditional() && "Cannot set condition of unconditional branch!");
2117 // setUnconditionalDest - Change the current branch to an unconditional branch
2118 // targeting the specified block.
2119 // FIXME: Eliminate this ugly method.
2120 void setUnconditionalDest(BasicBlock *Dest) {
2122 if (isConditional()) { // Convert this to an uncond branch.
2126 OperandList = op_begin();
2130 unsigned getNumSuccessors() const { return 1+isConditional(); }
2132 BasicBlock *getSuccessor(unsigned i) const {
2133 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2134 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2137 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2138 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2139 *(&Op<-1>() - idx) = NewSucc;
2142 // Methods for support type inquiry through isa, cast, and dyn_cast:
2143 static inline bool classof(const BranchInst *) { return true; }
2144 static inline bool classof(const Instruction *I) {
2145 return (I->getOpcode() == Instruction::Br);
2147 static inline bool classof(const Value *V) {
2148 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2151 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2152 virtual unsigned getNumSuccessorsV() const;
2153 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2157 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2159 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2161 //===----------------------------------------------------------------------===//
2163 //===----------------------------------------------------------------------===//
2165 //===---------------------------------------------------------------------------
2166 /// SwitchInst - Multiway switch
2168 class SwitchInst : public TerminatorInst {
2169 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2170 unsigned ReservedSpace;
2171 // Operand[0] = Value to switch on
2172 // Operand[1] = Default basic block destination
2173 // Operand[2n ] = Value to match
2174 // Operand[2n+1] = BasicBlock to go to on match
2175 SwitchInst(const SwitchInst &RI);
2176 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2177 void resizeOperands(unsigned No);
2178 // allocate space for exactly zero operands
2179 void *operator new(size_t s) {
2180 return User::operator new(s, 0);
2182 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2183 /// switch on and a default destination. The number of additional cases can
2184 /// be specified here to make memory allocation more efficient. This
2185 /// constructor can also autoinsert before another instruction.
2186 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2187 Instruction *InsertBefore = 0);
2189 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2190 /// switch on and a default destination. The number of additional cases can
2191 /// be specified here to make memory allocation more efficient. This
2192 /// constructor also autoinserts at the end of the specified BasicBlock.
2193 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2194 BasicBlock *InsertAtEnd);
2196 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2197 unsigned NumCases, Instruction *InsertBefore = 0) {
2198 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2200 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2201 unsigned NumCases, BasicBlock *InsertAtEnd) {
2202 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2206 /// Provide fast operand accessors
2207 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2209 // Accessor Methods for Switch stmt
2210 Value *getCondition() const { return getOperand(0); }
2211 void setCondition(Value *V) { setOperand(0, V); }
2213 BasicBlock *getDefaultDest() const {
2214 return cast<BasicBlock>(getOperand(1));
2217 /// getNumCases - return the number of 'cases' in this switch instruction.
2218 /// Note that case #0 is always the default case.
2219 unsigned getNumCases() const {
2220 return getNumOperands()/2;
2223 /// getCaseValue - Return the specified case value. Note that case #0, the
2224 /// default destination, does not have a case value.
2225 ConstantInt *getCaseValue(unsigned i) {
2226 assert(i && i < getNumCases() && "Illegal case value to get!");
2227 return getSuccessorValue(i);
2230 /// getCaseValue - Return the specified case value. Note that case #0, the
2231 /// default destination, does not have a case value.
2232 const ConstantInt *getCaseValue(unsigned i) const {
2233 assert(i && i < getNumCases() && "Illegal case value to get!");
2234 return getSuccessorValue(i);
2237 /// findCaseValue - Search all of the case values for the specified constant.
2238 /// If it is explicitly handled, return the case number of it, otherwise
2239 /// return 0 to indicate that it is handled by the default handler.
2240 unsigned findCaseValue(const ConstantInt *C) const {
2241 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2242 if (getCaseValue(i) == C)
2247 /// findCaseDest - Finds the unique case value for a given successor. Returns
2248 /// null if the successor is not found, not unique, or is the default case.
2249 ConstantInt *findCaseDest(BasicBlock *BB) {
2250 if (BB == getDefaultDest()) return NULL;
2252 ConstantInt *CI = NULL;
2253 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2254 if (getSuccessor(i) == BB) {
2255 if (CI) return NULL; // Multiple cases lead to BB.
2256 else CI = getCaseValue(i);
2262 /// addCase - Add an entry to the switch instruction...
2264 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2266 /// removeCase - This method removes the specified successor from the switch
2267 /// instruction. Note that this cannot be used to remove the default
2268 /// destination (successor #0).
2270 void removeCase(unsigned idx);
2272 virtual SwitchInst *clone() const;
2274 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2275 BasicBlock *getSuccessor(unsigned idx) const {
2276 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2277 return cast<BasicBlock>(getOperand(idx*2+1));
2279 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2280 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2281 setOperand(idx*2+1, NewSucc);
2284 // getSuccessorValue - Return the value associated with the specified
2286 ConstantInt *getSuccessorValue(unsigned idx) const {
2287 assert(idx < getNumSuccessors() && "Successor # out of range!");
2288 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2291 // Methods for support type inquiry through isa, cast, and dyn_cast:
2292 static inline bool classof(const SwitchInst *) { return true; }
2293 static inline bool classof(const Instruction *I) {
2294 return I->getOpcode() == Instruction::Switch;
2296 static inline bool classof(const Value *V) {
2297 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2300 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2301 virtual unsigned getNumSuccessorsV() const;
2302 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2306 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2309 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2312 //===----------------------------------------------------------------------===//
2314 //===----------------------------------------------------------------------===//
2316 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2317 /// calling convention of the call.
2319 class InvokeInst : public TerminatorInst {
2320 AttrListPtr AttributeList;
2321 InvokeInst(const InvokeInst &BI);
2322 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2323 Value* const *Args, unsigned NumArgs);
2325 template<typename InputIterator>
2326 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2327 InputIterator ArgBegin, InputIterator ArgEnd,
2328 const Twine &NameStr,
2329 // This argument ensures that we have an iterator we can
2330 // do arithmetic on in constant time
2331 std::random_access_iterator_tag) {
2332 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2334 // This requires that the iterator points to contiguous memory.
2335 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2339 /// Construct an InvokeInst given a range of arguments.
2340 /// InputIterator must be a random-access iterator pointing to
2341 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2342 /// made for random-accessness but not for contiguous storage as
2343 /// that would incur runtime overhead.
2345 /// @brief Construct an InvokeInst from a range of arguments
2346 template<typename InputIterator>
2347 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2348 InputIterator ArgBegin, InputIterator ArgEnd,
2350 const Twine &NameStr, Instruction *InsertBefore);
2352 /// Construct an InvokeInst given a range of arguments.
2353 /// InputIterator must be a random-access iterator pointing to
2354 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2355 /// made for random-accessness but not for contiguous storage as
2356 /// that would incur runtime overhead.
2358 /// @brief Construct an InvokeInst from a range of arguments
2359 template<typename InputIterator>
2360 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2361 InputIterator ArgBegin, InputIterator ArgEnd,
2363 const Twine &NameStr, BasicBlock *InsertAtEnd);
2365 template<typename InputIterator>
2366 static InvokeInst *Create(Value *Func,
2367 BasicBlock *IfNormal, BasicBlock *IfException,
2368 InputIterator ArgBegin, InputIterator ArgEnd,
2369 const Twine &NameStr = "",
2370 Instruction *InsertBefore = 0) {
2371 unsigned Values(ArgEnd - ArgBegin + 3);
2372 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2373 Values, NameStr, InsertBefore);
2375 template<typename InputIterator>
2376 static InvokeInst *Create(Value *Func,
2377 BasicBlock *IfNormal, BasicBlock *IfException,
2378 InputIterator ArgBegin, InputIterator ArgEnd,
2379 const Twine &NameStr,
2380 BasicBlock *InsertAtEnd) {
2381 unsigned Values(ArgEnd - ArgBegin + 3);
2382 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2383 Values, NameStr, InsertAtEnd);
2386 virtual InvokeInst *clone() const;
2388 /// Provide fast operand accessors
2389 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2391 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2393 CallingConv::ID getCallingConv() const {
2394 return static_cast<CallingConv::ID>(SubclassData);
2396 void setCallingConv(CallingConv::ID CC) {
2397 SubclassData = static_cast<unsigned>(CC);
2400 /// getAttributes - Return the parameter attributes for this invoke.
2402 const AttrListPtr &getAttributes() const { return AttributeList; }
2404 /// setAttributes - Set the parameter attributes for this invoke.
2406 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2408 /// addAttribute - adds the attribute to the list of attributes.
2409 void addAttribute(unsigned i, Attributes attr);
2411 /// removeAttribute - removes the attribute from the list of attributes.
2412 void removeAttribute(unsigned i, Attributes attr);
2414 /// @brief Determine whether the call or the callee has the given attribute.
2415 bool paramHasAttr(unsigned i, Attributes attr) const;
2417 /// @brief Extract the alignment for a call or parameter (0=unknown).
2418 unsigned getParamAlignment(unsigned i) const {
2419 return AttributeList.getParamAlignment(i);
2422 /// @brief Determine if the call does not access memory.
2423 bool doesNotAccessMemory() const {
2424 return paramHasAttr(~0, Attribute::ReadNone);
2426 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2427 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2428 else removeAttribute(~0, Attribute::ReadNone);
2431 /// @brief Determine if the call does not access or only reads memory.
2432 bool onlyReadsMemory() const {
2433 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2435 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2436 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2437 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2440 /// @brief Determine if the call cannot return.
2441 bool doesNotReturn() const {
2442 return paramHasAttr(~0, Attribute::NoReturn);
2444 void setDoesNotReturn(bool DoesNotReturn = true) {
2445 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2446 else removeAttribute(~0, Attribute::NoReturn);
2449 /// @brief Determine if the call cannot unwind.
2450 bool doesNotThrow() const {
2451 return paramHasAttr(~0, Attribute::NoUnwind);
2453 void setDoesNotThrow(bool DoesNotThrow = true) {
2454 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2455 else removeAttribute(~0, Attribute::NoUnwind);
2458 /// @brief Determine if the call returns a structure through first
2459 /// pointer argument.
2460 bool hasStructRetAttr() const {
2461 // Be friendly and also check the callee.
2462 return paramHasAttr(1, Attribute::StructRet);
2465 /// @brief Determine if any call argument is an aggregate passed by value.
2466 bool hasByValArgument() const {
2467 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2470 /// getCalledFunction - Return the function called, or null if this is an
2471 /// indirect function invocation.
2473 Function *getCalledFunction() const {
2474 return dyn_cast<Function>(getOperand(0));
2477 /// getCalledValue - Get a pointer to the function that is invoked by this
2479 const Value *getCalledValue() const { return getOperand(0); }
2480 Value *getCalledValue() { return getOperand(0); }
2482 // get*Dest - Return the destination basic blocks...
2483 BasicBlock *getNormalDest() const {
2484 return cast<BasicBlock>(getOperand(1));
2486 BasicBlock *getUnwindDest() const {
2487 return cast<BasicBlock>(getOperand(2));
2489 void setNormalDest(BasicBlock *B) {
2493 void setUnwindDest(BasicBlock *B) {
2497 BasicBlock *getSuccessor(unsigned i) const {
2498 assert(i < 2 && "Successor # out of range for invoke!");
2499 return i == 0 ? getNormalDest() : getUnwindDest();
2502 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2503 assert(idx < 2 && "Successor # out of range for invoke!");
2504 setOperand(idx+1, NewSucc);
2507 unsigned getNumSuccessors() const { return 2; }
2509 // Methods for support type inquiry through isa, cast, and dyn_cast:
2510 static inline bool classof(const InvokeInst *) { return true; }
2511 static inline bool classof(const Instruction *I) {
2512 return (I->getOpcode() == Instruction::Invoke);
2514 static inline bool classof(const Value *V) {
2515 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2518 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2519 virtual unsigned getNumSuccessorsV() const;
2520 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2524 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2527 template<typename InputIterator>
2528 InvokeInst::InvokeInst(Value *Func,
2529 BasicBlock *IfNormal, BasicBlock *IfException,
2530 InputIterator ArgBegin, InputIterator ArgEnd,
2532 const Twine &NameStr, Instruction *InsertBefore)
2533 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2534 ->getElementType())->getReturnType(),
2535 Instruction::Invoke,
2536 OperandTraits<InvokeInst>::op_end(this) - Values,
2537 Values, InsertBefore) {
2538 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2539 typename std::iterator_traits<InputIterator>::iterator_category());
2541 template<typename InputIterator>
2542 InvokeInst::InvokeInst(Value *Func,
2543 BasicBlock *IfNormal, BasicBlock *IfException,
2544 InputIterator ArgBegin, InputIterator ArgEnd,
2546 const Twine &NameStr, BasicBlock *InsertAtEnd)
2547 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2548 ->getElementType())->getReturnType(),
2549 Instruction::Invoke,
2550 OperandTraits<InvokeInst>::op_end(this) - Values,
2551 Values, InsertAtEnd) {
2552 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2553 typename std::iterator_traits<InputIterator>::iterator_category());
2556 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2558 //===----------------------------------------------------------------------===//
2560 //===----------------------------------------------------------------------===//
2562 //===---------------------------------------------------------------------------
2563 /// UnwindInst - Immediately exit the current function, unwinding the stack
2564 /// until an invoke instruction is found.
2566 class UnwindInst : public TerminatorInst {
2567 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2569 // allocate space for exactly zero operands
2570 void *operator new(size_t s) {
2571 return User::operator new(s, 0);
2573 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2574 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2576 virtual UnwindInst *clone() const;
2578 unsigned getNumSuccessors() const { return 0; }
2580 // Methods for support type inquiry through isa, cast, and dyn_cast:
2581 static inline bool classof(const UnwindInst *) { return true; }
2582 static inline bool classof(const Instruction *I) {
2583 return I->getOpcode() == Instruction::Unwind;
2585 static inline bool classof(const Value *V) {
2586 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2589 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2590 virtual unsigned getNumSuccessorsV() const;
2591 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2594 //===----------------------------------------------------------------------===//
2595 // UnreachableInst Class
2596 //===----------------------------------------------------------------------===//
2598 //===---------------------------------------------------------------------------
2599 /// UnreachableInst - This function has undefined behavior. In particular, the
2600 /// presence of this instruction indicates some higher level knowledge that the
2601 /// end of the block cannot be reached.
2603 class UnreachableInst : public TerminatorInst {
2604 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2606 // allocate space for exactly zero operands
2607 void *operator new(size_t s) {
2608 return User::operator new(s, 0);
2610 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2611 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2613 virtual UnreachableInst *clone() const;
2615 unsigned getNumSuccessors() const { return 0; }
2617 // Methods for support type inquiry through isa, cast, and dyn_cast:
2618 static inline bool classof(const UnreachableInst *) { return true; }
2619 static inline bool classof(const Instruction *I) {
2620 return I->getOpcode() == Instruction::Unreachable;
2622 static inline bool classof(const Value *V) {
2623 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2626 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2627 virtual unsigned getNumSuccessorsV() const;
2628 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2631 //===----------------------------------------------------------------------===//
2633 //===----------------------------------------------------------------------===//
2635 /// @brief This class represents a truncation of integer types.
2636 class TruncInst : public CastInst {
2638 /// @brief Constructor with insert-before-instruction semantics
2640 Value *S, ///< The value to be truncated
2641 const Type *Ty, ///< The (smaller) type to truncate to
2642 const Twine &NameStr = "", ///< A name for the new instruction
2643 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2646 /// @brief Constructor with insert-at-end-of-block semantics
2648 Value *S, ///< The value to be truncated
2649 const Type *Ty, ///< The (smaller) type to truncate to
2650 const Twine &NameStr, ///< A name for the new instruction
2651 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2654 /// @brief Clone an identical TruncInst
2655 virtual TruncInst *clone() const;
2657 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2658 static inline bool classof(const TruncInst *) { return true; }
2659 static inline bool classof(const Instruction *I) {
2660 return I->getOpcode() == Trunc;
2662 static inline bool classof(const Value *V) {
2663 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2667 //===----------------------------------------------------------------------===//
2669 //===----------------------------------------------------------------------===//
2671 /// @brief This class represents zero extension of integer types.
2672 class ZExtInst : public CastInst {
2674 /// @brief Constructor with insert-before-instruction semantics
2676 Value *S, ///< The value to be zero extended
2677 const Type *Ty, ///< The type to zero extend to
2678 const Twine &NameStr = "", ///< A name for the new instruction
2679 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2682 /// @brief Constructor with insert-at-end semantics.
2684 Value *S, ///< The value to be zero extended
2685 const Type *Ty, ///< The type to zero extend to
2686 const Twine &NameStr, ///< A name for the new instruction
2687 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2690 /// @brief Clone an identical ZExtInst
2691 virtual ZExtInst *clone() const;
2693 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2694 static inline bool classof(const ZExtInst *) { return true; }
2695 static inline bool classof(const Instruction *I) {
2696 return I->getOpcode() == ZExt;
2698 static inline bool classof(const Value *V) {
2699 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2703 //===----------------------------------------------------------------------===//
2705 //===----------------------------------------------------------------------===//
2707 /// @brief This class represents a sign extension of integer types.
2708 class SExtInst : public CastInst {
2710 /// @brief Constructor with insert-before-instruction semantics
2712 Value *S, ///< The value to be sign extended
2713 const Type *Ty, ///< The type to sign extend to
2714 const Twine &NameStr = "", ///< A name for the new instruction
2715 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2718 /// @brief Constructor with insert-at-end-of-block semantics
2720 Value *S, ///< The value to be sign extended
2721 const Type *Ty, ///< The type to sign extend to
2722 const Twine &NameStr, ///< A name for the new instruction
2723 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2726 /// @brief Clone an identical SExtInst
2727 virtual SExtInst *clone() const;
2729 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2730 static inline bool classof(const SExtInst *) { return true; }
2731 static inline bool classof(const Instruction *I) {
2732 return I->getOpcode() == SExt;
2734 static inline bool classof(const Value *V) {
2735 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2739 //===----------------------------------------------------------------------===//
2740 // FPTruncInst Class
2741 //===----------------------------------------------------------------------===//
2743 /// @brief This class represents a truncation of floating point types.
2744 class FPTruncInst : public CastInst {
2746 /// @brief Constructor with insert-before-instruction semantics
2748 Value *S, ///< The value to be truncated
2749 const Type *Ty, ///< The type to truncate to
2750 const Twine &NameStr = "", ///< A name for the new instruction
2751 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2754 /// @brief Constructor with insert-before-instruction semantics
2756 Value *S, ///< The value to be truncated
2757 const Type *Ty, ///< The type to truncate to
2758 const Twine &NameStr, ///< A name for the new instruction
2759 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2762 /// @brief Clone an identical FPTruncInst
2763 virtual FPTruncInst *clone() const;
2765 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2766 static inline bool classof(const FPTruncInst *) { return true; }
2767 static inline bool classof(const Instruction *I) {
2768 return I->getOpcode() == FPTrunc;
2770 static inline bool classof(const Value *V) {
2771 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2775 //===----------------------------------------------------------------------===//
2777 //===----------------------------------------------------------------------===//
2779 /// @brief This class represents an extension of floating point types.
2780 class FPExtInst : public CastInst {
2782 /// @brief Constructor with insert-before-instruction semantics
2784 Value *S, ///< The value to be extended
2785 const Type *Ty, ///< The type to extend to
2786 const Twine &NameStr = "", ///< A name for the new instruction
2787 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2790 /// @brief Constructor with insert-at-end-of-block semantics
2792 Value *S, ///< The value to be extended
2793 const Type *Ty, ///< The type to extend to
2794 const Twine &NameStr, ///< A name for the new instruction
2795 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2798 /// @brief Clone an identical FPExtInst
2799 virtual FPExtInst *clone() const;
2801 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2802 static inline bool classof(const FPExtInst *) { return true; }
2803 static inline bool classof(const Instruction *I) {
2804 return I->getOpcode() == FPExt;
2806 static inline bool classof(const Value *V) {
2807 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2811 //===----------------------------------------------------------------------===//
2813 //===----------------------------------------------------------------------===//
2815 /// @brief This class represents a cast unsigned integer to floating point.
2816 class UIToFPInst : public CastInst {
2818 /// @brief Constructor with insert-before-instruction semantics
2820 Value *S, ///< The value to be converted
2821 const Type *Ty, ///< The type to convert to
2822 const Twine &NameStr = "", ///< A name for the new instruction
2823 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2826 /// @brief Constructor with insert-at-end-of-block semantics
2828 Value *S, ///< The value to be converted
2829 const Type *Ty, ///< The type to convert to
2830 const Twine &NameStr, ///< A name for the new instruction
2831 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2834 /// @brief Clone an identical UIToFPInst
2835 virtual UIToFPInst *clone() const;
2837 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2838 static inline bool classof(const UIToFPInst *) { return true; }
2839 static inline bool classof(const Instruction *I) {
2840 return I->getOpcode() == UIToFP;
2842 static inline bool classof(const Value *V) {
2843 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2847 //===----------------------------------------------------------------------===//
2849 //===----------------------------------------------------------------------===//
2851 /// @brief This class represents a cast from signed integer to floating point.
2852 class SIToFPInst : public CastInst {
2854 /// @brief Constructor with insert-before-instruction semantics
2856 Value *S, ///< The value to be converted
2857 const Type *Ty, ///< The type to convert to
2858 const Twine &NameStr = "", ///< A name for the new instruction
2859 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2862 /// @brief Constructor with insert-at-end-of-block semantics
2864 Value *S, ///< The value to be converted
2865 const Type *Ty, ///< The type to convert to
2866 const Twine &NameStr, ///< A name for the new instruction
2867 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2870 /// @brief Clone an identical SIToFPInst
2871 virtual SIToFPInst *clone() const;
2873 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2874 static inline bool classof(const SIToFPInst *) { return true; }
2875 static inline bool classof(const Instruction *I) {
2876 return I->getOpcode() == SIToFP;
2878 static inline bool classof(const Value *V) {
2879 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2883 //===----------------------------------------------------------------------===//
2885 //===----------------------------------------------------------------------===//
2887 /// @brief This class represents a cast from floating point to unsigned integer
2888 class FPToUIInst : public CastInst {
2890 /// @brief Constructor with insert-before-instruction semantics
2892 Value *S, ///< The value to be converted
2893 const Type *Ty, ///< The type to convert to
2894 const Twine &NameStr = "", ///< A name for the new instruction
2895 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2898 /// @brief Constructor with insert-at-end-of-block semantics
2900 Value *S, ///< The value to be converted
2901 const Type *Ty, ///< The type to convert to
2902 const Twine &NameStr, ///< A name for the new instruction
2903 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2906 /// @brief Clone an identical FPToUIInst
2907 virtual FPToUIInst *clone() const;
2909 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2910 static inline bool classof(const FPToUIInst *) { return true; }
2911 static inline bool classof(const Instruction *I) {
2912 return I->getOpcode() == FPToUI;
2914 static inline bool classof(const Value *V) {
2915 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2919 //===----------------------------------------------------------------------===//
2921 //===----------------------------------------------------------------------===//
2923 /// @brief This class represents a cast from floating point to signed integer.
2924 class FPToSIInst : public CastInst {
2926 /// @brief Constructor with insert-before-instruction semantics
2928 Value *S, ///< The value to be converted
2929 const Type *Ty, ///< The type to convert to
2930 const Twine &NameStr = "", ///< A name for the new instruction
2931 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2934 /// @brief Constructor with insert-at-end-of-block semantics
2936 Value *S, ///< The value to be converted
2937 const Type *Ty, ///< The type to convert to
2938 const Twine &NameStr, ///< A name for the new instruction
2939 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2942 /// @brief Clone an identical FPToSIInst
2943 virtual FPToSIInst *clone() const;
2945 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2946 static inline bool classof(const FPToSIInst *) { return true; }
2947 static inline bool classof(const Instruction *I) {
2948 return I->getOpcode() == FPToSI;
2950 static inline bool classof(const Value *V) {
2951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2955 //===----------------------------------------------------------------------===//
2956 // IntToPtrInst Class
2957 //===----------------------------------------------------------------------===//
2959 /// @brief This class represents a cast from an integer to a pointer.
2960 class IntToPtrInst : public CastInst {
2962 /// @brief Constructor with insert-before-instruction semantics
2964 Value *S, ///< The value to be converted
2965 const Type *Ty, ///< The type to convert to
2966 const Twine &NameStr = "", ///< A name for the new instruction
2967 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2970 /// @brief Constructor with insert-at-end-of-block semantics
2972 Value *S, ///< The value to be converted
2973 const Type *Ty, ///< The type to convert to
2974 const Twine &NameStr, ///< A name for the new instruction
2975 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2978 /// @brief Clone an identical IntToPtrInst
2979 virtual IntToPtrInst *clone() const;
2981 // Methods for support type inquiry through isa, cast, and dyn_cast:
2982 static inline bool classof(const IntToPtrInst *) { return true; }
2983 static inline bool classof(const Instruction *I) {
2984 return I->getOpcode() == IntToPtr;
2986 static inline bool classof(const Value *V) {
2987 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2991 //===----------------------------------------------------------------------===//
2992 // PtrToIntInst Class
2993 //===----------------------------------------------------------------------===//
2995 /// @brief This class represents a cast from a pointer to an integer
2996 class PtrToIntInst : public CastInst {
2998 /// @brief Constructor with insert-before-instruction semantics
3000 Value *S, ///< The value to be converted
3001 const Type *Ty, ///< The type to convert to
3002 const Twine &NameStr = "", ///< A name for the new instruction
3003 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3006 /// @brief Constructor with insert-at-end-of-block semantics
3008 Value *S, ///< The value to be converted
3009 const Type *Ty, ///< The type to convert to
3010 const Twine &NameStr, ///< A name for the new instruction
3011 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3014 /// @brief Clone an identical PtrToIntInst
3015 virtual PtrToIntInst *clone() const;
3017 // Methods for support type inquiry through isa, cast, and dyn_cast:
3018 static inline bool classof(const PtrToIntInst *) { return true; }
3019 static inline bool classof(const Instruction *I) {
3020 return I->getOpcode() == PtrToInt;
3022 static inline bool classof(const Value *V) {
3023 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3027 //===----------------------------------------------------------------------===//
3028 // BitCastInst Class
3029 //===----------------------------------------------------------------------===//
3031 /// @brief This class represents a no-op cast from one type to another.
3032 class BitCastInst : public CastInst {
3034 /// @brief Constructor with insert-before-instruction semantics
3036 Value *S, ///< The value to be casted
3037 const Type *Ty, ///< The type to casted to
3038 const Twine &NameStr = "", ///< A name for the new instruction
3039 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3042 /// @brief Constructor with insert-at-end-of-block semantics
3044 Value *S, ///< The value to be casted
3045 const Type *Ty, ///< The type to casted to
3046 const Twine &NameStr, ///< A name for the new instruction
3047 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3050 /// @brief Clone an identical BitCastInst
3051 virtual BitCastInst *clone() const;
3053 // Methods for support type inquiry through isa, cast, and dyn_cast:
3054 static inline bool classof(const BitCastInst *) { return true; }
3055 static inline bool classof(const Instruction *I) {
3056 return I->getOpcode() == BitCast;
3058 static inline bool classof(const Value *V) {
3059 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3063 } // End llvm namespace