1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/ADT/ArrayRef.h"
24 #include "llvm/ADT/SmallVector.h"
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 /// AllocaInst - an instruction to allocate memory on the stack
40 class AllocaInst : public UnaryInstruction {
42 virtual AllocaInst *clone_impl() const;
44 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
45 const Twine &Name = "", Instruction *InsertBefore = 0);
46 AllocaInst(const Type *Ty, Value *ArraySize,
47 const Twine &Name, BasicBlock *InsertAtEnd);
49 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
50 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
52 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
53 const Twine &Name = "", Instruction *InsertBefore = 0);
54 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
55 const Twine &Name, BasicBlock *InsertAtEnd);
57 // Out of line virtual method, so the vtable, etc. has a home.
58 virtual ~AllocaInst();
60 /// isArrayAllocation - Return true if there is an allocation size parameter
61 /// to the allocation instruction that is not 1.
63 bool isArrayAllocation() const;
65 /// getArraySize - Get the number of elements allocated. For a simple
66 /// allocation of a single element, this will return a constant 1 value.
68 const Value *getArraySize() const { return getOperand(0); }
69 Value *getArraySize() { return getOperand(0); }
71 /// getType - Overload to return most specific pointer type
73 const PointerType *getType() const {
74 return reinterpret_cast<const PointerType*>(Instruction::getType());
77 /// getAllocatedType - Return the type that is being allocated by the
80 Type *getAllocatedType() const;
82 /// getAlignment - Return the alignment of the memory that is being allocated
83 /// by the instruction.
85 unsigned getAlignment() const {
86 return (1u << getSubclassDataFromInstruction()) >> 1;
88 void setAlignment(unsigned Align);
90 /// isStaticAlloca - Return true if this alloca is in the entry block of the
91 /// function and is a constant size. If so, the code generator will fold it
92 /// into the prolog/epilog code, so it is basically free.
93 bool isStaticAlloca() const;
95 // Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool classof(const AllocaInst *) { return true; }
97 static inline bool classof(const Instruction *I) {
98 return (I->getOpcode() == Instruction::Alloca);
100 static inline bool classof(const Value *V) {
101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
104 // Shadow Instruction::setInstructionSubclassData with a private forwarding
105 // method so that subclasses cannot accidentally use it.
106 void setInstructionSubclassData(unsigned short D) {
107 Instruction::setInstructionSubclassData(D);
112 //===----------------------------------------------------------------------===//
114 //===----------------------------------------------------------------------===//
116 /// LoadInst - an instruction for reading from memory. This uses the
117 /// SubclassData field in Value to store whether or not the load is volatile.
119 class LoadInst : public UnaryInstruction {
122 virtual LoadInst *clone_impl() const;
124 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
125 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
126 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
127 Instruction *InsertBefore = 0);
128 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
129 unsigned Align, Instruction *InsertBefore = 0);
130 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
131 BasicBlock *InsertAtEnd);
132 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
133 unsigned Align, BasicBlock *InsertAtEnd);
135 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
136 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
137 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
138 bool isVolatile = false, Instruction *InsertBefore = 0);
139 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
140 BasicBlock *InsertAtEnd);
142 /// isVolatile - Return true if this is a load from a volatile memory
145 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
147 /// setVolatile - Specify whether this is a volatile load or not.
149 void setVolatile(bool V) {
150 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
154 /// getAlignment - Return the alignment of the access that is being performed
156 unsigned getAlignment() const {
157 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
160 void setAlignment(unsigned Align);
162 Value *getPointerOperand() { return getOperand(0); }
163 const Value *getPointerOperand() const { return getOperand(0); }
164 static unsigned getPointerOperandIndex() { return 0U; }
166 unsigned getPointerAddressSpace() const {
167 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const LoadInst *) { return true; }
173 static inline bool classof(const Instruction *I) {
174 return I->getOpcode() == Instruction::Load;
176 static inline bool classof(const Value *V) {
177 return isa<Instruction>(V) && classof(cast<Instruction>(V));
180 // Shadow Instruction::setInstructionSubclassData with a private forwarding
181 // method so that subclasses cannot accidentally use it.
182 void setInstructionSubclassData(unsigned short D) {
183 Instruction::setInstructionSubclassData(D);
188 //===----------------------------------------------------------------------===//
190 //===----------------------------------------------------------------------===//
192 /// StoreInst - an instruction for storing to memory
194 class StoreInst : public Instruction {
195 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
198 virtual StoreInst *clone_impl() const;
200 // allocate space for exactly two operands
201 void *operator new(size_t s) {
202 return User::operator new(s, 2);
204 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
205 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
206 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
207 Instruction *InsertBefore = 0);
208 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
209 unsigned Align, Instruction *InsertBefore = 0);
210 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
211 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
212 unsigned Align, BasicBlock *InsertAtEnd);
215 /// isVolatile - Return true if this is a load from a volatile memory
218 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
220 /// setVolatile - Specify whether this is a volatile load or not.
222 void setVolatile(bool V) {
223 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
227 /// Transparently provide more efficient getOperand methods.
228 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
230 /// getAlignment - Return the alignment of the access that is being performed
232 unsigned getAlignment() const {
233 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
236 void setAlignment(unsigned Align);
238 Value *getValueOperand() { return getOperand(0); }
239 const Value *getValueOperand() const { return getOperand(0); }
241 Value *getPointerOperand() { return getOperand(1); }
242 const Value *getPointerOperand() const { return getOperand(1); }
243 static unsigned getPointerOperandIndex() { return 1U; }
245 unsigned getPointerAddressSpace() const {
246 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
249 // Methods for support type inquiry through isa, cast, and dyn_cast:
250 static inline bool classof(const StoreInst *) { return true; }
251 static inline bool classof(const Instruction *I) {
252 return I->getOpcode() == Instruction::Store;
254 static inline bool classof(const Value *V) {
255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
258 // Shadow Instruction::setInstructionSubclassData with a private forwarding
259 // method so that subclasses cannot accidentally use it.
260 void setInstructionSubclassData(unsigned short D) {
261 Instruction::setInstructionSubclassData(D);
266 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<StoreInst, 2> {
269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
271 //===----------------------------------------------------------------------===//
272 // GetElementPtrInst Class
273 //===----------------------------------------------------------------------===//
275 // checkGEPType - Simple wrapper function to give a better assertion failure
276 // message on bad indexes for a gep instruction.
278 static inline const Type *checkGEPType(const Type *Ty) {
279 assert(Ty && "Invalid GetElementPtrInst indices for type!");
283 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
284 /// access elements of arrays and structs
286 class GetElementPtrInst : public Instruction {
287 GetElementPtrInst(const GetElementPtrInst &GEPI);
288 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
289 const Twine &NameStr);
290 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
292 template<typename RandomAccessIterator>
293 void init(Value *Ptr,
294 RandomAccessIterator IdxBegin,
295 RandomAccessIterator IdxEnd,
296 const Twine &NameStr,
297 // This argument ensures that we have an iterator we can
298 // do arithmetic on in constant time
299 std::random_access_iterator_tag) {
300 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
303 // This requires that the iterator points to contiguous memory.
304 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
305 // we have to build an array here
308 init(Ptr, 0, NumIdx, NameStr);
312 /// getIndexedType - Returns the type of the element that would be loaded with
313 /// a load instruction with the specified parameters.
315 /// Null is returned if the indices are invalid for the specified
318 template<typename RandomAccessIterator>
319 static Type *getIndexedType(const Type *Ptr,
320 RandomAccessIterator IdxBegin,
321 RandomAccessIterator IdxEnd,
322 // This argument ensures that we
323 // have an iterator we can do
324 // arithmetic on in constant time
325 std::random_access_iterator_tag) {
326 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
329 // This requires that the iterator points to contiguous memory.
330 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
332 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
335 /// Constructors - Create a getelementptr instruction with a base pointer an
336 /// list of indices. The first ctor can optionally insert before an existing
337 /// instruction, the second appends the new instruction to the specified
339 template<typename RandomAccessIterator>
340 inline GetElementPtrInst(Value *Ptr, RandomAccessIterator IdxBegin,
341 RandomAccessIterator IdxEnd,
343 const Twine &NameStr,
344 Instruction *InsertBefore);
345 template<typename RandomAccessIterator>
346 inline GetElementPtrInst(Value *Ptr,
347 RandomAccessIterator IdxBegin,
348 RandomAccessIterator IdxEnd,
350 const Twine &NameStr, BasicBlock *InsertAtEnd);
352 /// Constructors - These two constructors are convenience methods because one
353 /// and two index getelementptr instructions are so common.
354 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
355 Instruction *InsertBefore = 0);
356 GetElementPtrInst(Value *Ptr, Value *Idx,
357 const Twine &NameStr, BasicBlock *InsertAtEnd);
359 virtual GetElementPtrInst *clone_impl() const;
361 template<typename RandomAccessIterator>
362 static GetElementPtrInst *Create(Value *Ptr, RandomAccessIterator IdxBegin,
363 RandomAccessIterator IdxEnd,
364 const Twine &NameStr = "",
365 Instruction *InsertBefore = 0) {
366 typename std::iterator_traits<RandomAccessIterator>::difference_type
367 Values = 1 + std::distance(IdxBegin, IdxEnd);
369 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
371 template<typename RandomAccessIterator>
372 static GetElementPtrInst *Create(Value *Ptr,
373 RandomAccessIterator IdxBegin,
374 RandomAccessIterator IdxEnd,
375 const Twine &NameStr,
376 BasicBlock *InsertAtEnd) {
377 typename std::iterator_traits<RandomAccessIterator>::difference_type
378 Values = 1 + std::distance(IdxBegin, IdxEnd);
380 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
383 /// Constructors - These two creators are convenience methods because one
384 /// index getelementptr instructions are so common.
385 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
386 const Twine &NameStr = "",
387 Instruction *InsertBefore = 0) {
388 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
390 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
391 const Twine &NameStr,
392 BasicBlock *InsertAtEnd) {
393 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
396 /// Create an "inbounds" getelementptr. See the documentation for the
397 /// "inbounds" flag in LangRef.html for details.
398 template<typename RandomAccessIterator>
399 static GetElementPtrInst *CreateInBounds(Value *Ptr,
400 RandomAccessIterator IdxBegin,
401 RandomAccessIterator IdxEnd,
402 const Twine &NameStr = "",
403 Instruction *InsertBefore = 0) {
404 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
405 NameStr, InsertBefore);
406 GEP->setIsInBounds(true);
409 template<typename RandomAccessIterator>
410 static GetElementPtrInst *CreateInBounds(Value *Ptr,
411 RandomAccessIterator IdxBegin,
412 RandomAccessIterator IdxEnd,
413 const Twine &NameStr,
414 BasicBlock *InsertAtEnd) {
415 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
416 NameStr, InsertAtEnd);
417 GEP->setIsInBounds(true);
420 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
421 const Twine &NameStr = "",
422 Instruction *InsertBefore = 0) {
423 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
424 GEP->setIsInBounds(true);
427 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
428 const Twine &NameStr,
429 BasicBlock *InsertAtEnd) {
430 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
431 GEP->setIsInBounds(true);
435 /// Transparently provide more efficient getOperand methods.
436 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
438 // getType - Overload to return most specific pointer type...
439 const PointerType *getType() const {
440 return reinterpret_cast<const PointerType*>(Instruction::getType());
443 /// getIndexedType - Returns the type of the element that would be loaded with
444 /// a load instruction with the specified parameters.
446 /// Null is returned if the indices are invalid for the specified
449 template<typename RandomAccessIterator>
450 static Type *getIndexedType(const Type *Ptr, RandomAccessIterator IdxBegin,
451 RandomAccessIterator IdxEnd) {
452 return getIndexedType(Ptr, IdxBegin, IdxEnd,
453 typename std::iterator_traits<RandomAccessIterator>::
454 iterator_category());
457 // FIXME: Use ArrayRef
458 static Type *getIndexedType(const Type *Ptr,
459 Value* const *Idx, unsigned NumIdx);
460 static Type *getIndexedType(const Type *Ptr,
461 Constant* const *Idx, unsigned NumIdx);
463 static Type *getIndexedType(const Type *Ptr,
464 uint64_t const *Idx, unsigned NumIdx);
465 static Type *getIndexedType(const Type *Ptr, Value *Idx);
467 inline op_iterator idx_begin() { return op_begin()+1; }
468 inline const_op_iterator idx_begin() const { return op_begin()+1; }
469 inline op_iterator idx_end() { return op_end(); }
470 inline const_op_iterator idx_end() const { return op_end(); }
472 Value *getPointerOperand() {
473 return getOperand(0);
475 const Value *getPointerOperand() const {
476 return getOperand(0);
478 static unsigned getPointerOperandIndex() {
479 return 0U; // get index for modifying correct operand
482 unsigned getPointerAddressSpace() const {
483 return cast<PointerType>(getType())->getAddressSpace();
486 /// getPointerOperandType - Method to return the pointer operand as a
488 const PointerType *getPointerOperandType() const {
489 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
493 unsigned getNumIndices() const { // Note: always non-negative
494 return getNumOperands() - 1;
497 bool hasIndices() const {
498 return getNumOperands() > 1;
501 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
502 /// zeros. If so, the result pointer and the first operand have the same
503 /// value, just potentially different types.
504 bool hasAllZeroIndices() const;
506 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
507 /// constant integers. If so, the result pointer and the first operand have
508 /// a constant offset between them.
509 bool hasAllConstantIndices() const;
511 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
512 /// See LangRef.html for the meaning of inbounds on a getelementptr.
513 void setIsInBounds(bool b = true);
515 /// isInBounds - Determine whether the GEP has the inbounds flag.
516 bool isInBounds() const;
518 // Methods for support type inquiry through isa, cast, and dyn_cast:
519 static inline bool classof(const GetElementPtrInst *) { return true; }
520 static inline bool classof(const Instruction *I) {
521 return (I->getOpcode() == Instruction::GetElementPtr);
523 static inline bool classof(const Value *V) {
524 return isa<Instruction>(V) && classof(cast<Instruction>(V));
529 struct OperandTraits<GetElementPtrInst> :
530 public VariadicOperandTraits<GetElementPtrInst, 1> {
533 template<typename RandomAccessIterator>
534 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
535 RandomAccessIterator IdxBegin,
536 RandomAccessIterator IdxEnd,
538 const Twine &NameStr,
539 Instruction *InsertBefore)
540 : Instruction(PointerType::get(checkGEPType(
541 getIndexedType(Ptr->getType(),
543 cast<PointerType>(Ptr->getType())
544 ->getAddressSpace()),
546 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
547 Values, InsertBefore) {
548 init(Ptr, IdxBegin, IdxEnd, NameStr,
549 typename std::iterator_traits<RandomAccessIterator>
550 ::iterator_category());
552 template<typename RandomAccessIterator>
553 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
554 RandomAccessIterator IdxBegin,
555 RandomAccessIterator IdxEnd,
557 const Twine &NameStr,
558 BasicBlock *InsertAtEnd)
559 : Instruction(PointerType::get(checkGEPType(
560 getIndexedType(Ptr->getType(),
562 cast<PointerType>(Ptr->getType())
563 ->getAddressSpace()),
565 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
566 Values, InsertAtEnd) {
567 init(Ptr, IdxBegin, IdxEnd, NameStr,
568 typename std::iterator_traits<RandomAccessIterator>
569 ::iterator_category());
573 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
576 //===----------------------------------------------------------------------===//
578 //===----------------------------------------------------------------------===//
580 /// This instruction compares its operands according to the predicate given
581 /// to the constructor. It only operates on integers or pointers. The operands
582 /// must be identical types.
583 /// @brief Represent an integer comparison operator.
584 class ICmpInst: public CmpInst {
586 /// @brief Clone an identical ICmpInst
587 virtual ICmpInst *clone_impl() const;
589 /// @brief Constructor with insert-before-instruction semantics.
591 Instruction *InsertBefore, ///< Where to insert
592 Predicate pred, ///< The predicate to use for the comparison
593 Value *LHS, ///< The left-hand-side of the expression
594 Value *RHS, ///< The right-hand-side of the expression
595 const Twine &NameStr = "" ///< Name of the instruction
596 ) : CmpInst(makeCmpResultType(LHS->getType()),
597 Instruction::ICmp, pred, LHS, RHS, NameStr,
599 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
600 pred <= CmpInst::LAST_ICMP_PREDICATE &&
601 "Invalid ICmp predicate value");
602 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
603 "Both operands to ICmp instruction are not of the same type!");
604 // Check that the operands are the right type
605 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
606 getOperand(0)->getType()->isPointerTy()) &&
607 "Invalid operand types for ICmp instruction");
610 /// @brief Constructor with insert-at-end semantics.
612 BasicBlock &InsertAtEnd, ///< Block to insert into.
613 Predicate pred, ///< The predicate to use for the comparison
614 Value *LHS, ///< The left-hand-side of the expression
615 Value *RHS, ///< The right-hand-side of the expression
616 const Twine &NameStr = "" ///< Name of the instruction
617 ) : CmpInst(makeCmpResultType(LHS->getType()),
618 Instruction::ICmp, pred, LHS, RHS, NameStr,
620 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
621 pred <= CmpInst::LAST_ICMP_PREDICATE &&
622 "Invalid ICmp predicate value");
623 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
624 "Both operands to ICmp instruction are not of the same type!");
625 // Check that the operands are the right type
626 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
627 getOperand(0)->getType()->isPointerTy()) &&
628 "Invalid operand types for ICmp instruction");
631 /// @brief Constructor with no-insertion semantics
633 Predicate pred, ///< The predicate to use for the comparison
634 Value *LHS, ///< The left-hand-side of the expression
635 Value *RHS, ///< The right-hand-side of the expression
636 const Twine &NameStr = "" ///< Name of the instruction
637 ) : CmpInst(makeCmpResultType(LHS->getType()),
638 Instruction::ICmp, pred, LHS, RHS, NameStr) {
639 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
640 pred <= CmpInst::LAST_ICMP_PREDICATE &&
641 "Invalid ICmp predicate value");
642 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
643 "Both operands to ICmp instruction are not of the same type!");
644 // Check that the operands are the right type
645 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
646 getOperand(0)->getType()->isPointerTy()) &&
647 "Invalid operand types for ICmp instruction");
650 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
651 /// @returns the predicate that would be the result if the operand were
652 /// regarded as signed.
653 /// @brief Return the signed version of the predicate
654 Predicate getSignedPredicate() const {
655 return getSignedPredicate(getPredicate());
658 /// This is a static version that you can use without an instruction.
659 /// @brief Return the signed version of the predicate.
660 static Predicate getSignedPredicate(Predicate pred);
662 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
663 /// @returns the predicate that would be the result if the operand were
664 /// regarded as unsigned.
665 /// @brief Return the unsigned version of the predicate
666 Predicate getUnsignedPredicate() const {
667 return getUnsignedPredicate(getPredicate());
670 /// This is a static version that you can use without an instruction.
671 /// @brief Return the unsigned version of the predicate.
672 static Predicate getUnsignedPredicate(Predicate pred);
674 /// isEquality - Return true if this predicate is either EQ or NE. This also
675 /// tests for commutativity.
676 static bool isEquality(Predicate P) {
677 return P == ICMP_EQ || P == ICMP_NE;
680 /// isEquality - Return true if this predicate is either EQ or NE. This also
681 /// tests for commutativity.
682 bool isEquality() const {
683 return isEquality(getPredicate());
686 /// @returns true if the predicate of this ICmpInst is commutative
687 /// @brief Determine if this relation is commutative.
688 bool isCommutative() const { return isEquality(); }
690 /// isRelational - Return true if the predicate is relational (not EQ or NE).
692 bool isRelational() const {
693 return !isEquality();
696 /// isRelational - Return true if the predicate is relational (not EQ or NE).
698 static bool isRelational(Predicate P) {
699 return !isEquality(P);
702 /// Initialize a set of values that all satisfy the predicate with C.
703 /// @brief Make a ConstantRange for a relation with a constant value.
704 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
706 /// Exchange the two operands to this instruction in such a way that it does
707 /// not modify the semantics of the instruction. The predicate value may be
708 /// changed to retain the same result if the predicate is order dependent
710 /// @brief Swap operands and adjust predicate.
711 void swapOperands() {
712 setPredicate(getSwappedPredicate());
713 Op<0>().swap(Op<1>());
716 // Methods for support type inquiry through isa, cast, and dyn_cast:
717 static inline bool classof(const ICmpInst *) { return true; }
718 static inline bool classof(const Instruction *I) {
719 return I->getOpcode() == Instruction::ICmp;
721 static inline bool classof(const Value *V) {
722 return isa<Instruction>(V) && classof(cast<Instruction>(V));
727 //===----------------------------------------------------------------------===//
729 //===----------------------------------------------------------------------===//
731 /// This instruction compares its operands according to the predicate given
732 /// to the constructor. It only operates on floating point values or packed
733 /// vectors of floating point values. The operands must be identical types.
734 /// @brief Represents a floating point comparison operator.
735 class FCmpInst: public CmpInst {
737 /// @brief Clone an identical FCmpInst
738 virtual FCmpInst *clone_impl() const;
740 /// @brief Constructor with insert-before-instruction semantics.
742 Instruction *InsertBefore, ///< Where to insert
743 Predicate pred, ///< The predicate to use for the comparison
744 Value *LHS, ///< The left-hand-side of the expression
745 Value *RHS, ///< The right-hand-side of the expression
746 const Twine &NameStr = "" ///< Name of the instruction
747 ) : CmpInst(makeCmpResultType(LHS->getType()),
748 Instruction::FCmp, pred, LHS, RHS, NameStr,
750 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
751 "Invalid FCmp predicate value");
752 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
753 "Both operands to FCmp instruction are not of the same type!");
754 // Check that the operands are the right type
755 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
756 "Invalid operand types for FCmp instruction");
759 /// @brief Constructor with insert-at-end semantics.
761 BasicBlock &InsertAtEnd, ///< Block to insert into.
762 Predicate pred, ///< The predicate to use for the comparison
763 Value *LHS, ///< The left-hand-side of the expression
764 Value *RHS, ///< The right-hand-side of the expression
765 const Twine &NameStr = "" ///< Name of the instruction
766 ) : CmpInst(makeCmpResultType(LHS->getType()),
767 Instruction::FCmp, pred, LHS, RHS, NameStr,
769 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
770 "Invalid FCmp predicate value");
771 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
772 "Both operands to FCmp instruction are not of the same type!");
773 // Check that the operands are the right type
774 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
775 "Invalid operand types for FCmp instruction");
778 /// @brief Constructor with no-insertion semantics
780 Predicate pred, ///< The predicate to use for the comparison
781 Value *LHS, ///< The left-hand-side of the expression
782 Value *RHS, ///< The right-hand-side of the expression
783 const Twine &NameStr = "" ///< Name of the instruction
784 ) : CmpInst(makeCmpResultType(LHS->getType()),
785 Instruction::FCmp, pred, LHS, RHS, NameStr) {
786 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
787 "Invalid FCmp predicate value");
788 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
789 "Both operands to FCmp instruction are not of the same type!");
790 // Check that the operands are the right type
791 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
792 "Invalid operand types for FCmp instruction");
795 /// @returns true if the predicate of this instruction is EQ or NE.
796 /// @brief Determine if this is an equality predicate.
797 bool isEquality() const {
798 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
799 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
802 /// @returns true if the predicate of this instruction is commutative.
803 /// @brief Determine if this is a commutative predicate.
804 bool isCommutative() const {
805 return isEquality() ||
806 getPredicate() == FCMP_FALSE ||
807 getPredicate() == FCMP_TRUE ||
808 getPredicate() == FCMP_ORD ||
809 getPredicate() == FCMP_UNO;
812 /// @returns true if the predicate is relational (not EQ or NE).
813 /// @brief Determine if this a relational predicate.
814 bool isRelational() const { return !isEquality(); }
816 /// Exchange the two operands to this instruction in such a way that it does
817 /// not modify the semantics of the instruction. The predicate value may be
818 /// changed to retain the same result if the predicate is order dependent
820 /// @brief Swap operands and adjust predicate.
821 void swapOperands() {
822 setPredicate(getSwappedPredicate());
823 Op<0>().swap(Op<1>());
826 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
827 static inline bool classof(const FCmpInst *) { return true; }
828 static inline bool classof(const Instruction *I) {
829 return I->getOpcode() == Instruction::FCmp;
831 static inline bool classof(const Value *V) {
832 return isa<Instruction>(V) && classof(cast<Instruction>(V));
836 //===----------------------------------------------------------------------===//
837 /// CallInst - This class represents a function call, abstracting a target
838 /// machine's calling convention. This class uses low bit of the SubClassData
839 /// field to indicate whether or not this is a tail call. The rest of the bits
840 /// hold the calling convention of the call.
842 class CallInst : public Instruction {
843 AttrListPtr AttributeList; ///< parameter attributes for call
844 CallInst(const CallInst &CI);
845 void init(Value *Func, Value* const *Params, unsigned NumParams);
846 void init(Value *Func, Value *Actual1, Value *Actual2);
847 void init(Value *Func, Value *Actual);
848 void init(Value *Func);
850 template<typename RandomAccessIterator>
851 void init(Value *Func,
852 RandomAccessIterator ArgBegin,
853 RandomAccessIterator ArgEnd,
854 const Twine &NameStr,
855 // This argument ensures that we have an iterator we can
856 // do arithmetic on in constant time
857 std::random_access_iterator_tag) {
858 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
860 // This requires that the iterator points to contiguous memory.
861 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
865 /// Construct a CallInst given a range of arguments. RandomAccessIterator
866 /// must be a random-access iterator pointing to contiguous storage
867 /// (e.g. a std::vector<>::iterator). Checks are made for
868 /// random-accessness but not for contiguous storage as that would
869 /// incur runtime overhead.
870 /// @brief Construct a CallInst from a range of arguments
871 template<typename RandomAccessIterator>
872 CallInst(Value *Func,
873 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
874 const Twine &NameStr, Instruction *InsertBefore);
876 /// Construct a CallInst given a range of arguments. RandomAccessIterator
877 /// must be a random-access iterator pointing to contiguous storage
878 /// (e.g. a std::vector<>::iterator). Checks are made for
879 /// random-accessness but not for contiguous storage as that would
880 /// incur runtime overhead.
881 /// @brief Construct a CallInst from a range of arguments
882 template<typename RandomAccessIterator>
883 inline CallInst(Value *Func,
884 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
885 const Twine &NameStr, BasicBlock *InsertAtEnd);
887 CallInst(Value *F, Value *Actual, const Twine &NameStr,
888 Instruction *InsertBefore);
889 CallInst(Value *F, Value *Actual, const Twine &NameStr,
890 BasicBlock *InsertAtEnd);
891 explicit CallInst(Value *F, const Twine &NameStr,
892 Instruction *InsertBefore);
893 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
895 virtual CallInst *clone_impl() const;
897 template<typename RandomAccessIterator>
898 static CallInst *Create(Value *Func,
899 RandomAccessIterator ArgBegin,
900 RandomAccessIterator ArgEnd,
901 const Twine &NameStr = "",
902 Instruction *InsertBefore = 0) {
903 return new(unsigned(ArgEnd - ArgBegin + 1))
904 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
906 template<typename RandomAccessIterator>
907 static CallInst *Create(Value *Func,
908 RandomAccessIterator ArgBegin,
909 RandomAccessIterator ArgEnd,
910 const Twine &NameStr, BasicBlock *InsertAtEnd) {
911 return new(unsigned(ArgEnd - ArgBegin + 1))
912 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
914 static CallInst *Create(Value *F, Value *Actual,
915 const Twine &NameStr = "",
916 Instruction *InsertBefore = 0) {
917 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
919 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
920 BasicBlock *InsertAtEnd) {
921 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
923 static CallInst *Create(Value *F, const Twine &NameStr = "",
924 Instruction *InsertBefore = 0) {
925 return new(1) CallInst(F, NameStr, InsertBefore);
927 static CallInst *Create(Value *F, const Twine &NameStr,
928 BasicBlock *InsertAtEnd) {
929 return new(1) CallInst(F, NameStr, InsertAtEnd);
931 /// CreateMalloc - Generate the IR for a call to malloc:
932 /// 1. Compute the malloc call's argument as the specified type's size,
933 /// possibly multiplied by the array size if the array size is not
935 /// 2. Call malloc with that argument.
936 /// 3. Bitcast the result of the malloc call to the specified type.
937 static Instruction *CreateMalloc(Instruction *InsertBefore,
938 const Type *IntPtrTy, const Type *AllocTy,
939 Value *AllocSize, Value *ArraySize = 0,
940 Function* MallocF = 0,
941 const Twine &Name = "");
942 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
943 const Type *IntPtrTy, const Type *AllocTy,
944 Value *AllocSize, Value *ArraySize = 0,
945 Function* MallocF = 0,
946 const Twine &Name = "");
947 /// CreateFree - Generate the IR for a call to the builtin free function.
948 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
949 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
953 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
954 void setTailCall(bool isTC = true) {
955 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
959 /// Provide fast operand accessors
960 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
962 /// getNumArgOperands - Return the number of call arguments.
964 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
966 /// getArgOperand/setArgOperand - Return/set the i-th call argument.
968 Value *getArgOperand(unsigned i) const { return getOperand(i); }
969 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
971 /// getCallingConv/setCallingConv - Get or set the calling convention of this
973 CallingConv::ID getCallingConv() const {
974 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
976 void setCallingConv(CallingConv::ID CC) {
977 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
978 (static_cast<unsigned>(CC) << 1));
981 /// getAttributes - Return the parameter attributes for this call.
983 const AttrListPtr &getAttributes() const { return AttributeList; }
985 /// setAttributes - Set the parameter attributes for this call.
987 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
989 /// addAttribute - adds the attribute to the list of attributes.
990 void addAttribute(unsigned i, Attributes attr);
992 /// removeAttribute - removes the attribute from the list of attributes.
993 void removeAttribute(unsigned i, Attributes attr);
995 /// @brief Determine whether the call or the callee has the given attribute.
996 bool paramHasAttr(unsigned i, Attributes attr) const;
998 /// @brief Extract the alignment for a call or parameter (0=unknown).
999 unsigned getParamAlignment(unsigned i) const {
1000 return AttributeList.getParamAlignment(i);
1003 /// @brief Return true if the call should not be inlined.
1004 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
1005 void setIsNoInline(bool Value = true) {
1006 if (Value) addAttribute(~0, Attribute::NoInline);
1007 else removeAttribute(~0, Attribute::NoInline);
1010 /// @brief Determine if the call does not access memory.
1011 bool doesNotAccessMemory() const {
1012 return paramHasAttr(~0, Attribute::ReadNone);
1014 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1015 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1016 else removeAttribute(~0, Attribute::ReadNone);
1019 /// @brief Determine if the call does not access or only reads memory.
1020 bool onlyReadsMemory() const {
1021 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1023 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1024 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1025 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1028 /// @brief Determine if the call cannot return.
1029 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
1030 void setDoesNotReturn(bool DoesNotReturn = true) {
1031 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1032 else removeAttribute(~0, Attribute::NoReturn);
1035 /// @brief Determine if the call cannot unwind.
1036 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
1037 void setDoesNotThrow(bool DoesNotThrow = true) {
1038 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1039 else removeAttribute(~0, Attribute::NoUnwind);
1042 /// @brief Determine if the call returns a structure through first
1043 /// pointer argument.
1044 bool hasStructRetAttr() const {
1045 // Be friendly and also check the callee.
1046 return paramHasAttr(1, Attribute::StructRet);
1049 /// @brief Determine if any call argument is an aggregate passed by value.
1050 bool hasByValArgument() const {
1051 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1054 /// getCalledFunction - Return the function called, or null if this is an
1055 /// indirect function invocation.
1057 Function *getCalledFunction() const {
1058 return dyn_cast<Function>(Op<-1>());
1061 /// getCalledValue - Get a pointer to the function that is invoked by this
1063 const Value *getCalledValue() const { return Op<-1>(); }
1064 Value *getCalledValue() { return Op<-1>(); }
1066 /// setCalledFunction - Set the function called.
1067 void setCalledFunction(Value* Fn) {
1071 /// isInlineAsm - Check if this call is an inline asm statement.
1072 bool isInlineAsm() const {
1073 return isa<InlineAsm>(Op<-1>());
1076 // Methods for support type inquiry through isa, cast, and dyn_cast:
1077 static inline bool classof(const CallInst *) { return true; }
1078 static inline bool classof(const Instruction *I) {
1079 return I->getOpcode() == Instruction::Call;
1081 static inline bool classof(const Value *V) {
1082 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1085 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1086 // method so that subclasses cannot accidentally use it.
1087 void setInstructionSubclassData(unsigned short D) {
1088 Instruction::setInstructionSubclassData(D);
1093 struct OperandTraits<CallInst> : public VariadicOperandTraits<CallInst, 1> {
1096 template<typename RandomAccessIterator>
1097 CallInst::CallInst(Value *Func,
1098 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
1099 const Twine &NameStr, BasicBlock *InsertAtEnd)
1100 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1101 ->getElementType())->getReturnType(),
1103 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1104 unsigned(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1105 init(Func, ArgBegin, ArgEnd, NameStr,
1106 typename std::iterator_traits<RandomAccessIterator>
1107 ::iterator_category());
1110 template<typename RandomAccessIterator>
1111 CallInst::CallInst(Value *Func,
1112 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
1113 const Twine &NameStr, Instruction *InsertBefore)
1114 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1115 ->getElementType())->getReturnType(),
1117 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1118 unsigned(ArgEnd - ArgBegin + 1), InsertBefore) {
1119 init(Func, ArgBegin, ArgEnd, NameStr,
1120 typename std::iterator_traits<RandomAccessIterator>
1121 ::iterator_category());
1125 // Note: if you get compile errors about private methods then
1126 // please update your code to use the high-level operand
1127 // interfaces. See line 943 above.
1128 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1130 //===----------------------------------------------------------------------===//
1132 //===----------------------------------------------------------------------===//
1134 /// SelectInst - This class represents the LLVM 'select' instruction.
1136 class SelectInst : public Instruction {
1137 void init(Value *C, Value *S1, Value *S2) {
1138 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1144 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1145 Instruction *InsertBefore)
1146 : Instruction(S1->getType(), Instruction::Select,
1147 &Op<0>(), 3, InsertBefore) {
1151 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1152 BasicBlock *InsertAtEnd)
1153 : Instruction(S1->getType(), Instruction::Select,
1154 &Op<0>(), 3, InsertAtEnd) {
1159 virtual SelectInst *clone_impl() const;
1161 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1162 const Twine &NameStr = "",
1163 Instruction *InsertBefore = 0) {
1164 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1166 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1167 const Twine &NameStr,
1168 BasicBlock *InsertAtEnd) {
1169 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1172 const Value *getCondition() const { return Op<0>(); }
1173 const Value *getTrueValue() const { return Op<1>(); }
1174 const Value *getFalseValue() const { return Op<2>(); }
1175 Value *getCondition() { return Op<0>(); }
1176 Value *getTrueValue() { return Op<1>(); }
1177 Value *getFalseValue() { return Op<2>(); }
1179 /// areInvalidOperands - Return a string if the specified operands are invalid
1180 /// for a select operation, otherwise return null.
1181 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1183 /// Transparently provide more efficient getOperand methods.
1184 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1186 OtherOps getOpcode() const {
1187 return static_cast<OtherOps>(Instruction::getOpcode());
1190 // Methods for support type inquiry through isa, cast, and dyn_cast:
1191 static inline bool classof(const SelectInst *) { return true; }
1192 static inline bool classof(const Instruction *I) {
1193 return I->getOpcode() == Instruction::Select;
1195 static inline bool classof(const Value *V) {
1196 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1201 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<SelectInst, 3> {
1204 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1206 //===----------------------------------------------------------------------===//
1208 //===----------------------------------------------------------------------===//
1210 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1211 /// an argument of the specified type given a va_list and increments that list
1213 class VAArgInst : public UnaryInstruction {
1215 virtual VAArgInst *clone_impl() const;
1218 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1219 Instruction *InsertBefore = 0)
1220 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1223 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1224 BasicBlock *InsertAtEnd)
1225 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1229 Value *getPointerOperand() { return getOperand(0); }
1230 const Value *getPointerOperand() const { return getOperand(0); }
1231 static unsigned getPointerOperandIndex() { return 0U; }
1233 // Methods for support type inquiry through isa, cast, and dyn_cast:
1234 static inline bool classof(const VAArgInst *) { return true; }
1235 static inline bool classof(const Instruction *I) {
1236 return I->getOpcode() == VAArg;
1238 static inline bool classof(const Value *V) {
1239 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1243 //===----------------------------------------------------------------------===//
1244 // ExtractElementInst Class
1245 //===----------------------------------------------------------------------===//
1247 /// ExtractElementInst - This instruction extracts a single (scalar)
1248 /// element from a VectorType value
1250 class ExtractElementInst : public Instruction {
1251 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1252 Instruction *InsertBefore = 0);
1253 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1254 BasicBlock *InsertAtEnd);
1256 virtual ExtractElementInst *clone_impl() const;
1259 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1260 const Twine &NameStr = "",
1261 Instruction *InsertBefore = 0) {
1262 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1264 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1265 const Twine &NameStr,
1266 BasicBlock *InsertAtEnd) {
1267 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1270 /// isValidOperands - Return true if an extractelement instruction can be
1271 /// formed with the specified operands.
1272 static bool isValidOperands(const Value *Vec, const Value *Idx);
1274 Value *getVectorOperand() { return Op<0>(); }
1275 Value *getIndexOperand() { return Op<1>(); }
1276 const Value *getVectorOperand() const { return Op<0>(); }
1277 const Value *getIndexOperand() const { return Op<1>(); }
1279 const VectorType *getVectorOperandType() const {
1280 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1284 /// Transparently provide more efficient getOperand methods.
1285 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1287 // Methods for support type inquiry through isa, cast, and dyn_cast:
1288 static inline bool classof(const ExtractElementInst *) { return true; }
1289 static inline bool classof(const Instruction *I) {
1290 return I->getOpcode() == Instruction::ExtractElement;
1292 static inline bool classof(const Value *V) {
1293 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1298 struct OperandTraits<ExtractElementInst> :
1299 public FixedNumOperandTraits<ExtractElementInst, 2> {
1302 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1304 //===----------------------------------------------------------------------===//
1305 // InsertElementInst Class
1306 //===----------------------------------------------------------------------===//
1308 /// InsertElementInst - This instruction inserts a single (scalar)
1309 /// element into a VectorType value
1311 class InsertElementInst : public Instruction {
1312 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1313 const Twine &NameStr = "",
1314 Instruction *InsertBefore = 0);
1315 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1316 const Twine &NameStr, BasicBlock *InsertAtEnd);
1318 virtual InsertElementInst *clone_impl() const;
1321 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1322 const Twine &NameStr = "",
1323 Instruction *InsertBefore = 0) {
1324 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1326 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1327 const Twine &NameStr,
1328 BasicBlock *InsertAtEnd) {
1329 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1332 /// isValidOperands - Return true if an insertelement instruction can be
1333 /// formed with the specified operands.
1334 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1337 /// getType - Overload to return most specific vector type.
1339 const VectorType *getType() const {
1340 return reinterpret_cast<const VectorType*>(Instruction::getType());
1343 /// Transparently provide more efficient getOperand methods.
1344 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1346 // Methods for support type inquiry through isa, cast, and dyn_cast:
1347 static inline bool classof(const InsertElementInst *) { return true; }
1348 static inline bool classof(const Instruction *I) {
1349 return I->getOpcode() == Instruction::InsertElement;
1351 static inline bool classof(const Value *V) {
1352 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1357 struct OperandTraits<InsertElementInst> :
1358 public FixedNumOperandTraits<InsertElementInst, 3> {
1361 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1363 //===----------------------------------------------------------------------===//
1364 // ShuffleVectorInst Class
1365 //===----------------------------------------------------------------------===//
1367 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1370 class ShuffleVectorInst : public Instruction {
1372 virtual ShuffleVectorInst *clone_impl() const;
1375 // allocate space for exactly three operands
1376 void *operator new(size_t s) {
1377 return User::operator new(s, 3);
1379 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1380 const Twine &NameStr = "",
1381 Instruction *InsertBefor = 0);
1382 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1383 const Twine &NameStr, BasicBlock *InsertAtEnd);
1385 /// isValidOperands - Return true if a shufflevector instruction can be
1386 /// formed with the specified operands.
1387 static bool isValidOperands(const Value *V1, const Value *V2,
1390 /// getType - Overload to return most specific vector type.
1392 const VectorType *getType() const {
1393 return reinterpret_cast<const VectorType*>(Instruction::getType());
1396 /// Transparently provide more efficient getOperand methods.
1397 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1399 /// getMaskValue - Return the index from the shuffle mask for the specified
1400 /// output result. This is either -1 if the element is undef or a number less
1401 /// than 2*numelements.
1402 int getMaskValue(unsigned i) const;
1404 // Methods for support type inquiry through isa, cast, and dyn_cast:
1405 static inline bool classof(const ShuffleVectorInst *) { return true; }
1406 static inline bool classof(const Instruction *I) {
1407 return I->getOpcode() == Instruction::ShuffleVector;
1409 static inline bool classof(const Value *V) {
1410 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1415 struct OperandTraits<ShuffleVectorInst> :
1416 public FixedNumOperandTraits<ShuffleVectorInst, 3> {
1419 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1421 //===----------------------------------------------------------------------===//
1422 // ExtractValueInst Class
1423 //===----------------------------------------------------------------------===//
1425 /// ExtractValueInst - This instruction extracts a struct member or array
1426 /// element value from an aggregate value.
1428 class ExtractValueInst : public UnaryInstruction {
1429 SmallVector<unsigned, 4> Indices;
1431 ExtractValueInst(const ExtractValueInst &EVI);
1432 void init(ArrayRef<unsigned> Idxs, const Twine &NameStr);
1434 /// Constructors - Create a extractvalue instruction with a base aggregate
1435 /// value and a list of indices. The first ctor can optionally insert before
1436 /// an existing instruction, the second appends the new instruction to the
1437 /// specified BasicBlock.
1438 inline ExtractValueInst(Value *Agg,
1439 ArrayRef<unsigned> Idxs,
1440 const Twine &NameStr,
1441 Instruction *InsertBefore);
1442 inline ExtractValueInst(Value *Agg,
1443 ArrayRef<unsigned> Idxs,
1444 const Twine &NameStr, BasicBlock *InsertAtEnd);
1446 // allocate space for exactly one operand
1447 void *operator new(size_t s) {
1448 return User::operator new(s, 1);
1451 virtual ExtractValueInst *clone_impl() const;
1454 static ExtractValueInst *Create(Value *Agg,
1455 ArrayRef<unsigned> Idxs,
1456 const Twine &NameStr = "",
1457 Instruction *InsertBefore = 0) {
1459 ExtractValueInst(Agg, Idxs, NameStr, InsertBefore);
1461 static ExtractValueInst *Create(Value *Agg,
1462 ArrayRef<unsigned> Idxs,
1463 const Twine &NameStr,
1464 BasicBlock *InsertAtEnd) {
1465 return new ExtractValueInst(Agg, Idxs, NameStr, InsertAtEnd);
1468 /// getIndexedType - Returns the type of the element that would be extracted
1469 /// with an extractvalue instruction with the specified parameters.
1471 /// Null is returned if the indices are invalid for the specified type.
1472 static Type *getIndexedType(const Type *Agg, ArrayRef<unsigned> Idxs);
1474 typedef const unsigned* idx_iterator;
1475 inline idx_iterator idx_begin() const { return Indices.begin(); }
1476 inline idx_iterator idx_end() const { return Indices.end(); }
1478 Value *getAggregateOperand() {
1479 return getOperand(0);
1481 const Value *getAggregateOperand() const {
1482 return getOperand(0);
1484 static unsigned getAggregateOperandIndex() {
1485 return 0U; // get index for modifying correct operand
1488 ArrayRef<unsigned> getIndices() const {
1492 unsigned getNumIndices() const {
1493 return (unsigned)Indices.size();
1496 bool hasIndices() const {
1500 // Methods for support type inquiry through isa, cast, and dyn_cast:
1501 static inline bool classof(const ExtractValueInst *) { return true; }
1502 static inline bool classof(const Instruction *I) {
1503 return I->getOpcode() == Instruction::ExtractValue;
1505 static inline bool classof(const Value *V) {
1506 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1510 ExtractValueInst::ExtractValueInst(Value *Agg,
1511 ArrayRef<unsigned> Idxs,
1512 const Twine &NameStr,
1513 Instruction *InsertBefore)
1514 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1515 ExtractValue, Agg, InsertBefore) {
1516 init(Idxs, NameStr);
1518 ExtractValueInst::ExtractValueInst(Value *Agg,
1519 ArrayRef<unsigned> Idxs,
1520 const Twine &NameStr,
1521 BasicBlock *InsertAtEnd)
1522 : UnaryInstruction(checkGEPType(getIndexedType(Agg->getType(), Idxs)),
1523 ExtractValue, Agg, InsertAtEnd) {
1524 init(Idxs, NameStr);
1528 //===----------------------------------------------------------------------===//
1529 // InsertValueInst Class
1530 //===----------------------------------------------------------------------===//
1532 /// InsertValueInst - This instruction inserts a struct field of array element
1533 /// value into an aggregate value.
1535 class InsertValueInst : public Instruction {
1536 SmallVector<unsigned, 4> Indices;
1538 void *operator new(size_t, unsigned); // Do not implement
1539 InsertValueInst(const InsertValueInst &IVI);
1540 void init(Value *Agg, Value *Val, ArrayRef<unsigned> Idxs,
1541 const Twine &NameStr);
1543 /// Constructors - Create a insertvalue instruction with a base aggregate
1544 /// value, a value to insert, and a list of indices. The first ctor can
1545 /// optionally insert before an existing instruction, the second appends
1546 /// the new instruction to the specified BasicBlock.
1547 inline InsertValueInst(Value *Agg, Value *Val,
1548 ArrayRef<unsigned> Idxs,
1549 const Twine &NameStr,
1550 Instruction *InsertBefore);
1551 inline InsertValueInst(Value *Agg, Value *Val,
1552 ArrayRef<unsigned> Idxs,
1553 const Twine &NameStr, BasicBlock *InsertAtEnd);
1555 /// Constructors - These two constructors are convenience methods because one
1556 /// and two index insertvalue instructions are so common.
1557 InsertValueInst(Value *Agg, Value *Val,
1558 unsigned Idx, const Twine &NameStr = "",
1559 Instruction *InsertBefore = 0);
1560 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1561 const Twine &NameStr, BasicBlock *InsertAtEnd);
1563 virtual InsertValueInst *clone_impl() const;
1565 // allocate space for exactly two operands
1566 void *operator new(size_t s) {
1567 return User::operator new(s, 2);
1570 static InsertValueInst *Create(Value *Agg, Value *Val,
1571 ArrayRef<unsigned> Idxs,
1572 const Twine &NameStr = "",
1573 Instruction *InsertBefore = 0) {
1574 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertBefore);
1576 static InsertValueInst *Create(Value *Agg, Value *Val,
1577 ArrayRef<unsigned> Idxs,
1578 const Twine &NameStr,
1579 BasicBlock *InsertAtEnd) {
1580 return new InsertValueInst(Agg, Val, Idxs, NameStr, InsertAtEnd);
1583 /// Transparently provide more efficient getOperand methods.
1584 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1586 typedef const unsigned* idx_iterator;
1587 inline idx_iterator idx_begin() const { return Indices.begin(); }
1588 inline idx_iterator idx_end() const { return Indices.end(); }
1590 Value *getAggregateOperand() {
1591 return getOperand(0);
1593 const Value *getAggregateOperand() const {
1594 return getOperand(0);
1596 static unsigned getAggregateOperandIndex() {
1597 return 0U; // get index for modifying correct operand
1600 Value *getInsertedValueOperand() {
1601 return getOperand(1);
1603 const Value *getInsertedValueOperand() const {
1604 return getOperand(1);
1606 static unsigned getInsertedValueOperandIndex() {
1607 return 1U; // get index for modifying correct operand
1610 ArrayRef<unsigned> getIndices() const {
1614 unsigned getNumIndices() const {
1615 return (unsigned)Indices.size();
1618 bool hasIndices() const {
1622 // Methods for support type inquiry through isa, cast, and dyn_cast:
1623 static inline bool classof(const InsertValueInst *) { return true; }
1624 static inline bool classof(const Instruction *I) {
1625 return I->getOpcode() == Instruction::InsertValue;
1627 static inline bool classof(const Value *V) {
1628 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1633 struct OperandTraits<InsertValueInst> :
1634 public FixedNumOperandTraits<InsertValueInst, 2> {
1637 InsertValueInst::InsertValueInst(Value *Agg,
1639 ArrayRef<unsigned> Idxs,
1640 const Twine &NameStr,
1641 Instruction *InsertBefore)
1642 : Instruction(Agg->getType(), InsertValue,
1643 OperandTraits<InsertValueInst>::op_begin(this),
1645 init(Agg, Val, Idxs, NameStr);
1647 InsertValueInst::InsertValueInst(Value *Agg,
1649 ArrayRef<unsigned> Idxs,
1650 const Twine &NameStr,
1651 BasicBlock *InsertAtEnd)
1652 : Instruction(Agg->getType(), InsertValue,
1653 OperandTraits<InsertValueInst>::op_begin(this),
1655 init(Agg, Val, Idxs, NameStr);
1658 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1660 //===----------------------------------------------------------------------===//
1662 //===----------------------------------------------------------------------===//
1664 // PHINode - The PHINode class is used to represent the magical mystical PHI
1665 // node, that can not exist in nature, but can be synthesized in a computer
1666 // scientist's overactive imagination.
1668 class PHINode : public Instruction {
1669 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1670 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1671 /// the number actually in use.
1672 unsigned ReservedSpace;
1673 PHINode(const PHINode &PN);
1674 // allocate space for exactly zero operands
1675 void *operator new(size_t s) {
1676 return User::operator new(s, 0);
1678 explicit PHINode(const Type *Ty, unsigned NumReservedValues,
1679 const Twine &NameStr = "", Instruction *InsertBefore = 0)
1680 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1681 ReservedSpace(NumReservedValues) {
1683 OperandList = allocHungoffUses(ReservedSpace);
1686 PHINode(const Type *Ty, unsigned NumReservedValues, const Twine &NameStr,
1687 BasicBlock *InsertAtEnd)
1688 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1689 ReservedSpace(NumReservedValues) {
1691 OperandList = allocHungoffUses(ReservedSpace);
1694 // allocHungoffUses - this is more complicated than the generic
1695 // User::allocHungoffUses, because we have to allocate Uses for the incoming
1696 // values and pointers to the incoming blocks, all in one allocation.
1697 Use *allocHungoffUses(unsigned) const;
1699 virtual PHINode *clone_impl() const;
1701 /// Constructors - NumReservedValues is a hint for the number of incoming
1702 /// edges that this phi node will have (use 0 if you really have no idea).
1703 static PHINode *Create(const Type *Ty, unsigned NumReservedValues,
1704 const Twine &NameStr = "",
1705 Instruction *InsertBefore = 0) {
1706 return new PHINode(Ty, NumReservedValues, NameStr, InsertBefore);
1708 static PHINode *Create(const Type *Ty, unsigned NumReservedValues,
1709 const Twine &NameStr, BasicBlock *InsertAtEnd) {
1710 return new PHINode(Ty, NumReservedValues, NameStr, InsertAtEnd);
1714 /// Provide fast operand accessors
1715 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1717 // Block iterator interface. This provides access to the list of incoming
1718 // basic blocks, which parallels the list of incoming values.
1720 typedef BasicBlock **block_iterator;
1721 typedef BasicBlock * const *const_block_iterator;
1723 block_iterator block_begin() {
1725 reinterpret_cast<Use::UserRef*>(op_begin() + ReservedSpace);
1726 return reinterpret_cast<block_iterator>(ref + 1);
1729 const_block_iterator block_begin() const {
1730 const Use::UserRef *ref =
1731 reinterpret_cast<const Use::UserRef*>(op_begin() + ReservedSpace);
1732 return reinterpret_cast<const_block_iterator>(ref + 1);
1735 block_iterator block_end() {
1736 return block_begin() + getNumOperands();
1739 const_block_iterator block_end() const {
1740 return block_begin() + getNumOperands();
1743 /// getNumIncomingValues - Return the number of incoming edges
1745 unsigned getNumIncomingValues() const { return getNumOperands(); }
1747 /// getIncomingValue - Return incoming value number x
1749 Value *getIncomingValue(unsigned i) const {
1750 return getOperand(i);
1752 void setIncomingValue(unsigned i, Value *V) {
1755 static unsigned getOperandNumForIncomingValue(unsigned i) {
1758 static unsigned getIncomingValueNumForOperand(unsigned i) {
1762 /// getIncomingBlock - Return incoming basic block number @p i.
1764 BasicBlock *getIncomingBlock(unsigned i) const {
1765 return block_begin()[i];
1768 /// getIncomingBlock - Return incoming basic block corresponding
1769 /// to an operand of the PHI.
1771 BasicBlock *getIncomingBlock(const Use &U) const {
1772 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1773 return getIncomingBlock(unsigned(&U - op_begin()));
1776 /// getIncomingBlock - Return incoming basic block corresponding
1777 /// to value use iterator.
1779 template <typename U>
1780 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1781 return getIncomingBlock(I.getUse());
1784 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1785 block_begin()[i] = BB;
1788 /// addIncoming - Add an incoming value to the end of the PHI list
1790 void addIncoming(Value *V, BasicBlock *BB) {
1791 assert(V && "PHI node got a null value!");
1792 assert(BB && "PHI node got a null basic block!");
1793 assert(getType() == V->getType() &&
1794 "All operands to PHI node must be the same type as the PHI node!");
1795 if (NumOperands == ReservedSpace)
1796 growOperands(); // Get more space!
1797 // Initialize some new operands.
1799 setIncomingValue(NumOperands - 1, V);
1800 setIncomingBlock(NumOperands - 1, BB);
1803 /// removeIncomingValue - Remove an incoming value. This is useful if a
1804 /// predecessor basic block is deleted. The value removed is returned.
1806 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1807 /// is true), the PHI node is destroyed and any uses of it are replaced with
1808 /// dummy values. The only time there should be zero incoming values to a PHI
1809 /// node is when the block is dead, so this strategy is sound.
1811 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1813 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1814 int Idx = getBasicBlockIndex(BB);
1815 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1816 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1819 /// getBasicBlockIndex - Return the first index of the specified basic
1820 /// block in the value list for this PHI. Returns -1 if no instance.
1822 int getBasicBlockIndex(const BasicBlock *BB) const {
1823 for (unsigned i = 0, e = getNumOperands(); i != e; ++i)
1824 if (block_begin()[i] == BB)
1829 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1830 int Idx = getBasicBlockIndex(BB);
1831 assert(Idx >= 0 && "Invalid basic block argument!");
1832 return getIncomingValue(Idx);
1835 /// hasConstantValue - If the specified PHI node always merges together the
1836 /// same value, return the value, otherwise return null.
1837 Value *hasConstantValue() const;
1839 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1840 static inline bool classof(const PHINode *) { return true; }
1841 static inline bool classof(const Instruction *I) {
1842 return I->getOpcode() == Instruction::PHI;
1844 static inline bool classof(const Value *V) {
1845 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1848 void growOperands();
1852 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1855 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1858 //===----------------------------------------------------------------------===//
1860 //===----------------------------------------------------------------------===//
1862 //===---------------------------------------------------------------------------
1863 /// ReturnInst - Return a value (possibly void), from a function. Execution
1864 /// does not continue in this function any longer.
1866 class ReturnInst : public TerminatorInst {
1867 ReturnInst(const ReturnInst &RI);
1870 // ReturnInst constructors:
1871 // ReturnInst() - 'ret void' instruction
1872 // ReturnInst( null) - 'ret void' instruction
1873 // ReturnInst(Value* X) - 'ret X' instruction
1874 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1875 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1876 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1877 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1879 // NOTE: If the Value* passed is of type void then the constructor behaves as
1880 // if it was passed NULL.
1881 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1882 Instruction *InsertBefore = 0);
1883 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1884 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1886 virtual ReturnInst *clone_impl() const;
1888 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1889 Instruction *InsertBefore = 0) {
1890 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1892 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1893 BasicBlock *InsertAtEnd) {
1894 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1896 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1897 return new(0) ReturnInst(C, InsertAtEnd);
1899 virtual ~ReturnInst();
1901 /// Provide fast operand accessors
1902 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1904 /// Convenience accessor. Returns null if there is no return value.
1905 Value *getReturnValue() const {
1906 return getNumOperands() != 0 ? getOperand(0) : 0;
1909 unsigned getNumSuccessors() const { return 0; }
1911 // Methods for support type inquiry through isa, cast, and dyn_cast:
1912 static inline bool classof(const ReturnInst *) { return true; }
1913 static inline bool classof(const Instruction *I) {
1914 return (I->getOpcode() == Instruction::Ret);
1916 static inline bool classof(const Value *V) {
1917 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1920 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1921 virtual unsigned getNumSuccessorsV() const;
1922 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1926 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<ReturnInst> {
1929 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1931 //===----------------------------------------------------------------------===//
1933 //===----------------------------------------------------------------------===//
1935 //===---------------------------------------------------------------------------
1936 /// BranchInst - Conditional or Unconditional Branch instruction.
1938 class BranchInst : public TerminatorInst {
1939 /// Ops list - Branches are strange. The operands are ordered:
1940 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
1941 /// they don't have to check for cond/uncond branchness. These are mostly
1942 /// accessed relative from op_end().
1943 BranchInst(const BranchInst &BI);
1945 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1946 // BranchInst(BB *B) - 'br B'
1947 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1948 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1949 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1950 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1951 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1952 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
1953 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1954 Instruction *InsertBefore = 0);
1955 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
1956 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1957 BasicBlock *InsertAtEnd);
1959 virtual BranchInst *clone_impl() const;
1961 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
1962 return new(1) BranchInst(IfTrue, InsertBefore);
1964 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1965 Value *Cond, Instruction *InsertBefore = 0) {
1966 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
1968 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
1969 return new(1) BranchInst(IfTrue, InsertAtEnd);
1971 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
1972 Value *Cond, BasicBlock *InsertAtEnd) {
1973 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
1976 /// Transparently provide more efficient getOperand methods.
1977 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1979 bool isUnconditional() const { return getNumOperands() == 1; }
1980 bool isConditional() const { return getNumOperands() == 3; }
1982 Value *getCondition() const {
1983 assert(isConditional() && "Cannot get condition of an uncond branch!");
1987 void setCondition(Value *V) {
1988 assert(isConditional() && "Cannot set condition of unconditional branch!");
1992 unsigned getNumSuccessors() const { return 1+isConditional(); }
1994 BasicBlock *getSuccessor(unsigned i) const {
1995 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1996 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
1999 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2000 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2001 *(&Op<-1>() - idx) = (Value*)NewSucc;
2004 // Methods for support type inquiry through isa, cast, and dyn_cast:
2005 static inline bool classof(const BranchInst *) { return true; }
2006 static inline bool classof(const Instruction *I) {
2007 return (I->getOpcode() == Instruction::Br);
2009 static inline bool classof(const Value *V) {
2010 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2013 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2014 virtual unsigned getNumSuccessorsV() const;
2015 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2019 struct OperandTraits<BranchInst> : public VariadicOperandTraits<BranchInst, 1> {
2022 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2024 //===----------------------------------------------------------------------===//
2026 //===----------------------------------------------------------------------===//
2028 //===---------------------------------------------------------------------------
2029 /// SwitchInst - Multiway switch
2031 class SwitchInst : public TerminatorInst {
2032 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2033 unsigned ReservedSpace;
2034 // Operand[0] = Value to switch on
2035 // Operand[1] = Default basic block destination
2036 // Operand[2n ] = Value to match
2037 // Operand[2n+1] = BasicBlock to go to on match
2038 SwitchInst(const SwitchInst &SI);
2039 void init(Value *Value, BasicBlock *Default, unsigned NumReserved);
2040 void growOperands();
2041 // allocate space for exactly zero operands
2042 void *operator new(size_t s) {
2043 return User::operator new(s, 0);
2045 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2046 /// switch on and a default destination. The number of additional cases can
2047 /// be specified here to make memory allocation more efficient. This
2048 /// constructor can also autoinsert before another instruction.
2049 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2050 Instruction *InsertBefore);
2052 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2053 /// switch on and a default destination. The number of additional cases can
2054 /// be specified here to make memory allocation more efficient. This
2055 /// constructor also autoinserts at the end of the specified BasicBlock.
2056 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2057 BasicBlock *InsertAtEnd);
2059 virtual SwitchInst *clone_impl() const;
2061 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2062 unsigned NumCases, Instruction *InsertBefore = 0) {
2063 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2065 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2066 unsigned NumCases, BasicBlock *InsertAtEnd) {
2067 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2071 /// Provide fast operand accessors
2072 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2074 // Accessor Methods for Switch stmt
2075 Value *getCondition() const { return getOperand(0); }
2076 void setCondition(Value *V) { setOperand(0, V); }
2078 BasicBlock *getDefaultDest() const {
2079 return cast<BasicBlock>(getOperand(1));
2082 /// getNumCases - return the number of 'cases' in this switch instruction.
2083 /// Note that case #0 is always the default case.
2084 unsigned getNumCases() const {
2085 return getNumOperands()/2;
2088 /// getCaseValue - Return the specified case value. Note that case #0, the
2089 /// default destination, does not have a case value.
2090 ConstantInt *getCaseValue(unsigned i) {
2091 assert(i && i < getNumCases() && "Illegal case value to get!");
2092 return getSuccessorValue(i);
2095 /// getCaseValue - Return the specified case value. Note that case #0, the
2096 /// default destination, does not have a case value.
2097 const ConstantInt *getCaseValue(unsigned i) const {
2098 assert(i && i < getNumCases() && "Illegal case value to get!");
2099 return getSuccessorValue(i);
2102 /// findCaseValue - Search all of the case values for the specified constant.
2103 /// If it is explicitly handled, return the case number of it, otherwise
2104 /// return 0 to indicate that it is handled by the default handler.
2105 unsigned findCaseValue(const ConstantInt *C) const {
2106 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2107 if (getCaseValue(i) == C)
2112 /// findCaseDest - Finds the unique case value for a given successor. Returns
2113 /// null if the successor is not found, not unique, or is the default case.
2114 ConstantInt *findCaseDest(BasicBlock *BB) {
2115 if (BB == getDefaultDest()) return NULL;
2117 ConstantInt *CI = NULL;
2118 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2119 if (getSuccessor(i) == BB) {
2120 if (CI) return NULL; // Multiple cases lead to BB.
2121 else CI = getCaseValue(i);
2127 /// addCase - Add an entry to the switch instruction...
2129 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2131 /// removeCase - This method removes the specified successor from the switch
2132 /// instruction. Note that this cannot be used to remove the default
2133 /// destination (successor #0). Also note that this operation may reorder the
2134 /// remaining cases at index idx and above.
2136 void removeCase(unsigned idx);
2138 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2139 BasicBlock *getSuccessor(unsigned idx) const {
2140 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2141 return cast<BasicBlock>(getOperand(idx*2+1));
2143 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2144 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2145 setOperand(idx*2+1, (Value*)NewSucc);
2148 // getSuccessorValue - Return the value associated with the specified
2150 ConstantInt *getSuccessorValue(unsigned idx) const {
2151 assert(idx < getNumSuccessors() && "Successor # out of range!");
2152 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2155 // Methods for support type inquiry through isa, cast, and dyn_cast:
2156 static inline bool classof(const SwitchInst *) { return true; }
2157 static inline bool classof(const Instruction *I) {
2158 return I->getOpcode() == Instruction::Switch;
2160 static inline bool classof(const Value *V) {
2161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2164 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2165 virtual unsigned getNumSuccessorsV() const;
2166 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2170 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2173 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2176 //===----------------------------------------------------------------------===//
2177 // IndirectBrInst Class
2178 //===----------------------------------------------------------------------===//
2180 //===---------------------------------------------------------------------------
2181 /// IndirectBrInst - Indirect Branch Instruction.
2183 class IndirectBrInst : public TerminatorInst {
2184 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2185 unsigned ReservedSpace;
2186 // Operand[0] = Value to switch on
2187 // Operand[1] = Default basic block destination
2188 // Operand[2n ] = Value to match
2189 // Operand[2n+1] = BasicBlock to go to on match
2190 IndirectBrInst(const IndirectBrInst &IBI);
2191 void init(Value *Address, unsigned NumDests);
2192 void growOperands();
2193 // allocate space for exactly zero operands
2194 void *operator new(size_t s) {
2195 return User::operator new(s, 0);
2197 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2198 /// Address to jump to. The number of expected destinations can be specified
2199 /// here to make memory allocation more efficient. This constructor can also
2200 /// autoinsert before another instruction.
2201 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2203 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2204 /// Address to jump to. The number of expected destinations can be specified
2205 /// here to make memory allocation more efficient. This constructor also
2206 /// autoinserts at the end of the specified BasicBlock.
2207 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2209 virtual IndirectBrInst *clone_impl() const;
2211 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2212 Instruction *InsertBefore = 0) {
2213 return new IndirectBrInst(Address, NumDests, InsertBefore);
2215 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2216 BasicBlock *InsertAtEnd) {
2217 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2221 /// Provide fast operand accessors.
2222 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2224 // Accessor Methods for IndirectBrInst instruction.
2225 Value *getAddress() { return getOperand(0); }
2226 const Value *getAddress() const { return getOperand(0); }
2227 void setAddress(Value *V) { setOperand(0, V); }
2230 /// getNumDestinations - return the number of possible destinations in this
2231 /// indirectbr instruction.
2232 unsigned getNumDestinations() const { return getNumOperands()-1; }
2234 /// getDestination - Return the specified destination.
2235 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2236 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2238 /// addDestination - Add a destination.
2240 void addDestination(BasicBlock *Dest);
2242 /// removeDestination - This method removes the specified successor from the
2243 /// indirectbr instruction.
2244 void removeDestination(unsigned i);
2246 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2247 BasicBlock *getSuccessor(unsigned i) const {
2248 return cast<BasicBlock>(getOperand(i+1));
2250 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2251 setOperand(i+1, (Value*)NewSucc);
2254 // Methods for support type inquiry through isa, cast, and dyn_cast:
2255 static inline bool classof(const IndirectBrInst *) { return true; }
2256 static inline bool classof(const Instruction *I) {
2257 return I->getOpcode() == Instruction::IndirectBr;
2259 static inline bool classof(const Value *V) {
2260 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2263 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2264 virtual unsigned getNumSuccessorsV() const;
2265 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2269 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2272 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2275 //===----------------------------------------------------------------------===//
2277 //===----------------------------------------------------------------------===//
2279 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2280 /// calling convention of the call.
2282 class InvokeInst : public TerminatorInst {
2283 AttrListPtr AttributeList;
2284 InvokeInst(const InvokeInst &BI);
2285 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2286 Value* const *Args, unsigned NumArgs);
2288 template<typename RandomAccessIterator>
2289 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2290 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
2291 const Twine &NameStr,
2292 // This argument ensures that we have an iterator we can
2293 // do arithmetic on in constant time
2294 std::random_access_iterator_tag) {
2295 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2297 // This requires that the iterator points to contiguous memory.
2298 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2302 /// Construct an InvokeInst given a range of arguments.
2303 /// RandomAccessIterator must be a random-access iterator pointing to
2304 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2305 /// made for random-accessness but not for contiguous storage as
2306 /// that would incur runtime overhead.
2308 /// @brief Construct an InvokeInst from a range of arguments
2309 template<typename RandomAccessIterator>
2310 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2311 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
2313 const Twine &NameStr, Instruction *InsertBefore);
2315 /// Construct an InvokeInst given a range of arguments.
2316 /// RandomAccessIterator must be a random-access iterator pointing to
2317 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2318 /// made for random-accessness but not for contiguous storage as
2319 /// that would incur runtime overhead.
2321 /// @brief Construct an InvokeInst from a range of arguments
2322 template<typename RandomAccessIterator>
2323 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2324 RandomAccessIterator ArgBegin, RandomAccessIterator ArgEnd,
2326 const Twine &NameStr, BasicBlock *InsertAtEnd);
2328 virtual InvokeInst *clone_impl() const;
2330 template<typename RandomAccessIterator>
2331 static InvokeInst *Create(Value *Func,
2332 BasicBlock *IfNormal, BasicBlock *IfException,
2333 RandomAccessIterator ArgBegin,
2334 RandomAccessIterator ArgEnd,
2335 const Twine &NameStr = "",
2336 Instruction *InsertBefore = 0) {
2337 unsigned Values(ArgEnd - ArgBegin + 3);
2338 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2339 Values, NameStr, InsertBefore);
2341 template<typename RandomAccessIterator>
2342 static InvokeInst *Create(Value *Func,
2343 BasicBlock *IfNormal, BasicBlock *IfException,
2344 RandomAccessIterator ArgBegin,
2345 RandomAccessIterator ArgEnd,
2346 const Twine &NameStr,
2347 BasicBlock *InsertAtEnd) {
2348 unsigned Values(ArgEnd - ArgBegin + 3);
2349 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2350 Values, NameStr, InsertAtEnd);
2353 /// Provide fast operand accessors
2354 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2356 /// getNumArgOperands - Return the number of invoke arguments.
2358 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2360 /// getArgOperand/setArgOperand - Return/set the i-th invoke argument.
2362 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2363 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2365 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2367 CallingConv::ID getCallingConv() const {
2368 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2370 void setCallingConv(CallingConv::ID CC) {
2371 setInstructionSubclassData(static_cast<unsigned>(CC));
2374 /// getAttributes - Return the parameter attributes for this invoke.
2376 const AttrListPtr &getAttributes() const { return AttributeList; }
2378 /// setAttributes - Set the parameter attributes for this invoke.
2380 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2382 /// addAttribute - adds the attribute to the list of attributes.
2383 void addAttribute(unsigned i, Attributes attr);
2385 /// removeAttribute - removes the attribute from the list of attributes.
2386 void removeAttribute(unsigned i, Attributes attr);
2388 /// @brief Determine whether the call or the callee has the given attribute.
2389 bool paramHasAttr(unsigned i, Attributes attr) const;
2391 /// @brief Extract the alignment for a call or parameter (0=unknown).
2392 unsigned getParamAlignment(unsigned i) const {
2393 return AttributeList.getParamAlignment(i);
2396 /// @brief Return true if the call should not be inlined.
2397 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
2398 void setIsNoInline(bool Value = true) {
2399 if (Value) addAttribute(~0, Attribute::NoInline);
2400 else removeAttribute(~0, Attribute::NoInline);
2403 /// @brief Determine if the call does not access memory.
2404 bool doesNotAccessMemory() const {
2405 return paramHasAttr(~0, Attribute::ReadNone);
2407 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2408 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2409 else removeAttribute(~0, Attribute::ReadNone);
2412 /// @brief Determine if the call does not access or only reads memory.
2413 bool onlyReadsMemory() const {
2414 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2416 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2417 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2418 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2421 /// @brief Determine if the call cannot return.
2422 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
2423 void setDoesNotReturn(bool DoesNotReturn = true) {
2424 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2425 else removeAttribute(~0, Attribute::NoReturn);
2428 /// @brief Determine if the call cannot unwind.
2429 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
2430 void setDoesNotThrow(bool DoesNotThrow = true) {
2431 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2432 else removeAttribute(~0, Attribute::NoUnwind);
2435 /// @brief Determine if the call returns a structure through first
2436 /// pointer argument.
2437 bool hasStructRetAttr() const {
2438 // Be friendly and also check the callee.
2439 return paramHasAttr(1, Attribute::StructRet);
2442 /// @brief Determine if any call argument is an aggregate passed by value.
2443 bool hasByValArgument() const {
2444 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2447 /// getCalledFunction - Return the function called, or null if this is an
2448 /// indirect function invocation.
2450 Function *getCalledFunction() const {
2451 return dyn_cast<Function>(Op<-3>());
2454 /// getCalledValue - Get a pointer to the function that is invoked by this
2456 const Value *getCalledValue() const { return Op<-3>(); }
2457 Value *getCalledValue() { return Op<-3>(); }
2459 /// setCalledFunction - Set the function called.
2460 void setCalledFunction(Value* Fn) {
2464 // get*Dest - Return the destination basic blocks...
2465 BasicBlock *getNormalDest() const {
2466 return cast<BasicBlock>(Op<-2>());
2468 BasicBlock *getUnwindDest() const {
2469 return cast<BasicBlock>(Op<-1>());
2471 void setNormalDest(BasicBlock *B) {
2472 Op<-2>() = reinterpret_cast<Value*>(B);
2474 void setUnwindDest(BasicBlock *B) {
2475 Op<-1>() = reinterpret_cast<Value*>(B);
2478 BasicBlock *getSuccessor(unsigned i) const {
2479 assert(i < 2 && "Successor # out of range for invoke!");
2480 return i == 0 ? getNormalDest() : getUnwindDest();
2483 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2484 assert(idx < 2 && "Successor # out of range for invoke!");
2485 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
2488 unsigned getNumSuccessors() const { return 2; }
2490 // Methods for support type inquiry through isa, cast, and dyn_cast:
2491 static inline bool classof(const InvokeInst *) { return true; }
2492 static inline bool classof(const Instruction *I) {
2493 return (I->getOpcode() == Instruction::Invoke);
2495 static inline bool classof(const Value *V) {
2496 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2500 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2501 virtual unsigned getNumSuccessorsV() const;
2502 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2504 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2505 // method so that subclasses cannot accidentally use it.
2506 void setInstructionSubclassData(unsigned short D) {
2507 Instruction::setInstructionSubclassData(D);
2512 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<InvokeInst, 3> {
2515 template<typename RandomAccessIterator>
2516 InvokeInst::InvokeInst(Value *Func,
2517 BasicBlock *IfNormal, BasicBlock *IfException,
2518 RandomAccessIterator ArgBegin,
2519 RandomAccessIterator ArgEnd,
2521 const Twine &NameStr, Instruction *InsertBefore)
2522 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2523 ->getElementType())->getReturnType(),
2524 Instruction::Invoke,
2525 OperandTraits<InvokeInst>::op_end(this) - Values,
2526 Values, InsertBefore) {
2527 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2528 typename std::iterator_traits<RandomAccessIterator>
2529 ::iterator_category());
2531 template<typename RandomAccessIterator>
2532 InvokeInst::InvokeInst(Value *Func,
2533 BasicBlock *IfNormal, BasicBlock *IfException,
2534 RandomAccessIterator ArgBegin,
2535 RandomAccessIterator ArgEnd,
2537 const Twine &NameStr, BasicBlock *InsertAtEnd)
2538 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2539 ->getElementType())->getReturnType(),
2540 Instruction::Invoke,
2541 OperandTraits<InvokeInst>::op_end(this) - Values,
2542 Values, InsertAtEnd) {
2543 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2544 typename std::iterator_traits<RandomAccessIterator>
2545 ::iterator_category());
2548 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2550 //===----------------------------------------------------------------------===//
2552 //===----------------------------------------------------------------------===//
2554 //===---------------------------------------------------------------------------
2555 /// UnwindInst - Immediately exit the current function, unwinding the stack
2556 /// until an invoke instruction is found.
2558 class UnwindInst : public TerminatorInst {
2559 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2561 virtual UnwindInst *clone_impl() const;
2563 // allocate space for exactly zero operands
2564 void *operator new(size_t s) {
2565 return User::operator new(s, 0);
2567 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2568 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2570 unsigned getNumSuccessors() const { return 0; }
2572 // Methods for support type inquiry through isa, cast, and dyn_cast:
2573 static inline bool classof(const UnwindInst *) { return true; }
2574 static inline bool classof(const Instruction *I) {
2575 return I->getOpcode() == Instruction::Unwind;
2577 static inline bool classof(const Value *V) {
2578 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2581 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2582 virtual unsigned getNumSuccessorsV() const;
2583 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2586 //===----------------------------------------------------------------------===//
2587 // UnreachableInst Class
2588 //===----------------------------------------------------------------------===//
2590 //===---------------------------------------------------------------------------
2591 /// UnreachableInst - This function has undefined behavior. In particular, the
2592 /// presence of this instruction indicates some higher level knowledge that the
2593 /// end of the block cannot be reached.
2595 class UnreachableInst : public TerminatorInst {
2596 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2598 virtual UnreachableInst *clone_impl() const;
2601 // allocate space for exactly zero operands
2602 void *operator new(size_t s) {
2603 return User::operator new(s, 0);
2605 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2606 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2608 unsigned getNumSuccessors() const { return 0; }
2610 // Methods for support type inquiry through isa, cast, and dyn_cast:
2611 static inline bool classof(const UnreachableInst *) { return true; }
2612 static inline bool classof(const Instruction *I) {
2613 return I->getOpcode() == Instruction::Unreachable;
2615 static inline bool classof(const Value *V) {
2616 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2619 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2620 virtual unsigned getNumSuccessorsV() const;
2621 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2624 //===----------------------------------------------------------------------===//
2626 //===----------------------------------------------------------------------===//
2628 /// @brief This class represents a truncation of integer types.
2629 class TruncInst : public CastInst {
2631 /// @brief Clone an identical TruncInst
2632 virtual TruncInst *clone_impl() const;
2635 /// @brief Constructor with insert-before-instruction semantics
2637 Value *S, ///< The value to be truncated
2638 const Type *Ty, ///< The (smaller) type to truncate to
2639 const Twine &NameStr = "", ///< A name for the new instruction
2640 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2643 /// @brief Constructor with insert-at-end-of-block semantics
2645 Value *S, ///< The value to be truncated
2646 const Type *Ty, ///< The (smaller) type to truncate to
2647 const Twine &NameStr, ///< A name for the new instruction
2648 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2651 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2652 static inline bool classof(const TruncInst *) { return true; }
2653 static inline bool classof(const Instruction *I) {
2654 return I->getOpcode() == Trunc;
2656 static inline bool classof(const Value *V) {
2657 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2661 //===----------------------------------------------------------------------===//
2663 //===----------------------------------------------------------------------===//
2665 /// @brief This class represents zero extension of integer types.
2666 class ZExtInst : public CastInst {
2668 /// @brief Clone an identical ZExtInst
2669 virtual ZExtInst *clone_impl() const;
2672 /// @brief Constructor with insert-before-instruction semantics
2674 Value *S, ///< The value to be zero extended
2675 const Type *Ty, ///< The type to zero extend to
2676 const Twine &NameStr = "", ///< A name for the new instruction
2677 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2680 /// @brief Constructor with insert-at-end semantics.
2682 Value *S, ///< The value to be zero extended
2683 const Type *Ty, ///< The type to zero extend to
2684 const Twine &NameStr, ///< A name for the new instruction
2685 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2688 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2689 static inline bool classof(const ZExtInst *) { return true; }
2690 static inline bool classof(const Instruction *I) {
2691 return I->getOpcode() == ZExt;
2693 static inline bool classof(const Value *V) {
2694 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2698 //===----------------------------------------------------------------------===//
2700 //===----------------------------------------------------------------------===//
2702 /// @brief This class represents a sign extension of integer types.
2703 class SExtInst : public CastInst {
2705 /// @brief Clone an identical SExtInst
2706 virtual SExtInst *clone_impl() const;
2709 /// @brief Constructor with insert-before-instruction semantics
2711 Value *S, ///< The value to be sign extended
2712 const Type *Ty, ///< The type to sign extend to
2713 const Twine &NameStr = "", ///< A name for the new instruction
2714 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2717 /// @brief Constructor with insert-at-end-of-block semantics
2719 Value *S, ///< The value to be sign extended
2720 const Type *Ty, ///< The type to sign extend to
2721 const Twine &NameStr, ///< A name for the new instruction
2722 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2725 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2726 static inline bool classof(const SExtInst *) { return true; }
2727 static inline bool classof(const Instruction *I) {
2728 return I->getOpcode() == SExt;
2730 static inline bool classof(const Value *V) {
2731 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2735 //===----------------------------------------------------------------------===//
2736 // FPTruncInst Class
2737 //===----------------------------------------------------------------------===//
2739 /// @brief This class represents a truncation of floating point types.
2740 class FPTruncInst : public CastInst {
2742 /// @brief Clone an identical FPTruncInst
2743 virtual FPTruncInst *clone_impl() const;
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 Methods for support type inquiry through isa, cast, and dyn_cast:
2763 static inline bool classof(const FPTruncInst *) { return true; }
2764 static inline bool classof(const Instruction *I) {
2765 return I->getOpcode() == FPTrunc;
2767 static inline bool classof(const Value *V) {
2768 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2772 //===----------------------------------------------------------------------===//
2774 //===----------------------------------------------------------------------===//
2776 /// @brief This class represents an extension of floating point types.
2777 class FPExtInst : public CastInst {
2779 /// @brief Clone an identical FPExtInst
2780 virtual FPExtInst *clone_impl() const;
2783 /// @brief Constructor with insert-before-instruction semantics
2785 Value *S, ///< The value to be extended
2786 const Type *Ty, ///< The type to extend to
2787 const Twine &NameStr = "", ///< A name for the new instruction
2788 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2791 /// @brief Constructor with insert-at-end-of-block semantics
2793 Value *S, ///< The value to be extended
2794 const Type *Ty, ///< The type to extend to
2795 const Twine &NameStr, ///< A name for the new instruction
2796 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2799 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2800 static inline bool classof(const FPExtInst *) { return true; }
2801 static inline bool classof(const Instruction *I) {
2802 return I->getOpcode() == FPExt;
2804 static inline bool classof(const Value *V) {
2805 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2809 //===----------------------------------------------------------------------===//
2811 //===----------------------------------------------------------------------===//
2813 /// @brief This class represents a cast unsigned integer to floating point.
2814 class UIToFPInst : public CastInst {
2816 /// @brief Clone an identical UIToFPInst
2817 virtual UIToFPInst *clone_impl() const;
2820 /// @brief Constructor with insert-before-instruction semantics
2822 Value *S, ///< The value to be converted
2823 const Type *Ty, ///< The type to convert to
2824 const Twine &NameStr = "", ///< A name for the new instruction
2825 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2828 /// @brief Constructor with insert-at-end-of-block semantics
2830 Value *S, ///< The value to be converted
2831 const Type *Ty, ///< The type to convert to
2832 const Twine &NameStr, ///< A name for the new instruction
2833 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2836 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2837 static inline bool classof(const UIToFPInst *) { return true; }
2838 static inline bool classof(const Instruction *I) {
2839 return I->getOpcode() == UIToFP;
2841 static inline bool classof(const Value *V) {
2842 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2846 //===----------------------------------------------------------------------===//
2848 //===----------------------------------------------------------------------===//
2850 /// @brief This class represents a cast from signed integer to floating point.
2851 class SIToFPInst : public CastInst {
2853 /// @brief Clone an identical SIToFPInst
2854 virtual SIToFPInst *clone_impl() const;
2857 /// @brief Constructor with insert-before-instruction semantics
2859 Value *S, ///< The value to be converted
2860 const Type *Ty, ///< The type to convert to
2861 const Twine &NameStr = "", ///< A name for the new instruction
2862 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2865 /// @brief Constructor with insert-at-end-of-block semantics
2867 Value *S, ///< The value to be converted
2868 const Type *Ty, ///< The type to convert to
2869 const Twine &NameStr, ///< A name for the new instruction
2870 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2873 /// @brief 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 Clone an identical FPToUIInst
2891 virtual FPToUIInst *clone_impl() const;
2894 /// @brief Constructor with insert-before-instruction semantics
2896 Value *S, ///< The value to be converted
2897 const Type *Ty, ///< The type to convert to
2898 const Twine &NameStr = "", ///< A name for the new instruction
2899 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2902 /// @brief Constructor with insert-at-end-of-block semantics
2904 Value *S, ///< The value to be converted
2905 const Type *Ty, ///< The type to convert to
2906 const Twine &NameStr, ///< A name for the new instruction
2907 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2910 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2911 static inline bool classof(const FPToUIInst *) { return true; }
2912 static inline bool classof(const Instruction *I) {
2913 return I->getOpcode() == FPToUI;
2915 static inline bool classof(const Value *V) {
2916 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2920 //===----------------------------------------------------------------------===//
2922 //===----------------------------------------------------------------------===//
2924 /// @brief This class represents a cast from floating point to signed integer.
2925 class FPToSIInst : public CastInst {
2927 /// @brief Clone an identical FPToSIInst
2928 virtual FPToSIInst *clone_impl() const;
2931 /// @brief Constructor with insert-before-instruction semantics
2933 Value *S, ///< The value to be converted
2934 const Type *Ty, ///< The type to convert to
2935 const Twine &NameStr = "", ///< A name for the new instruction
2936 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2939 /// @brief Constructor with insert-at-end-of-block semantics
2941 Value *S, ///< The value to be converted
2942 const Type *Ty, ///< The type to convert to
2943 const Twine &NameStr, ///< A name for the new instruction
2944 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2947 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2948 static inline bool classof(const FPToSIInst *) { return true; }
2949 static inline bool classof(const Instruction *I) {
2950 return I->getOpcode() == FPToSI;
2952 static inline bool classof(const Value *V) {
2953 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2957 //===----------------------------------------------------------------------===//
2958 // IntToPtrInst Class
2959 //===----------------------------------------------------------------------===//
2961 /// @brief This class represents a cast from an integer to a pointer.
2962 class IntToPtrInst : public CastInst {
2964 /// @brief Constructor with insert-before-instruction semantics
2966 Value *S, ///< The value to be converted
2967 const Type *Ty, ///< The type to convert to
2968 const Twine &NameStr = "", ///< A name for the new instruction
2969 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2972 /// @brief Constructor with insert-at-end-of-block semantics
2974 Value *S, ///< The value to be converted
2975 const Type *Ty, ///< The type to convert to
2976 const Twine &NameStr, ///< A name for the new instruction
2977 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2980 /// @brief Clone an identical IntToPtrInst
2981 virtual IntToPtrInst *clone_impl() const;
2983 // Methods for support type inquiry through isa, cast, and dyn_cast:
2984 static inline bool classof(const IntToPtrInst *) { return true; }
2985 static inline bool classof(const Instruction *I) {
2986 return I->getOpcode() == IntToPtr;
2988 static inline bool classof(const Value *V) {
2989 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2993 //===----------------------------------------------------------------------===//
2994 // PtrToIntInst Class
2995 //===----------------------------------------------------------------------===//
2997 /// @brief This class represents a cast from a pointer to an integer
2998 class PtrToIntInst : public CastInst {
3000 /// @brief Clone an identical PtrToIntInst
3001 virtual PtrToIntInst *clone_impl() const;
3004 /// @brief Constructor with insert-before-instruction semantics
3006 Value *S, ///< The value to be converted
3007 const Type *Ty, ///< The type to convert to
3008 const Twine &NameStr = "", ///< A name for the new instruction
3009 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3012 /// @brief Constructor with insert-at-end-of-block semantics
3014 Value *S, ///< The value to be converted
3015 const Type *Ty, ///< The type to convert to
3016 const Twine &NameStr, ///< A name for the new instruction
3017 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3020 // Methods for support type inquiry through isa, cast, and dyn_cast:
3021 static inline bool classof(const PtrToIntInst *) { return true; }
3022 static inline bool classof(const Instruction *I) {
3023 return I->getOpcode() == PtrToInt;
3025 static inline bool classof(const Value *V) {
3026 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3030 //===----------------------------------------------------------------------===//
3031 // BitCastInst Class
3032 //===----------------------------------------------------------------------===//
3034 /// @brief This class represents a no-op cast from one type to another.
3035 class BitCastInst : public CastInst {
3037 /// @brief Clone an identical BitCastInst
3038 virtual BitCastInst *clone_impl() const;
3041 /// @brief Constructor with insert-before-instruction semantics
3043 Value *S, ///< The value to be casted
3044 const Type *Ty, ///< The type to casted to
3045 const Twine &NameStr = "", ///< A name for the new instruction
3046 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3049 /// @brief Constructor with insert-at-end-of-block semantics
3051 Value *S, ///< The value to be casted
3052 const Type *Ty, ///< The type to casted to
3053 const Twine &NameStr, ///< A name for the new instruction
3054 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3057 // Methods for support type inquiry through isa, cast, and dyn_cast:
3058 static inline bool classof(const BitCastInst *) { return true; }
3059 static inline bool classof(const Instruction *I) {
3060 return I->getOpcode() == BitCast;
3062 static inline bool classof(const Value *V) {
3063 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3067 } // End llvm namespace