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/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 const 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 *getPointerOperand() { return getOperand(1); }
239 const Value *getPointerOperand() const { return getOperand(1); }
240 static unsigned getPointerOperandIndex() { return 1U; }
242 unsigned getPointerAddressSpace() const {
243 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
246 // Methods for support type inquiry through isa, cast, and dyn_cast:
247 static inline bool classof(const StoreInst *) { return true; }
248 static inline bool classof(const Instruction *I) {
249 return I->getOpcode() == Instruction::Store;
251 static inline bool classof(const Value *V) {
252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
255 // Shadow Instruction::setInstructionSubclassData with a private forwarding
256 // method so that subclasses cannot accidentally use it.
257 void setInstructionSubclassData(unsigned short D) {
258 Instruction::setInstructionSubclassData(D);
263 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
266 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
268 //===----------------------------------------------------------------------===//
269 // GetElementPtrInst Class
270 //===----------------------------------------------------------------------===//
272 // checkType - Simple wrapper function to give a better assertion failure
273 // message on bad indexes for a gep instruction.
275 static inline const Type *checkType(const Type *Ty) {
276 assert(Ty && "Invalid GetElementPtrInst indices for type!");
280 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
281 /// access elements of arrays and structs
283 class GetElementPtrInst : public Instruction {
284 GetElementPtrInst(const GetElementPtrInst &GEPI);
285 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
286 const Twine &NameStr);
287 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
289 template<typename InputIterator>
290 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
291 const Twine &NameStr,
292 // This argument ensures that we have an iterator we can
293 // do arithmetic on in constant time
294 std::random_access_iterator_tag) {
295 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
298 // This requires that the iterator points to contiguous memory.
299 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
300 // we have to build an array here
303 init(Ptr, 0, NumIdx, NameStr);
307 /// getIndexedType - Returns the type of the element that would be loaded with
308 /// a load instruction with the specified parameters.
310 /// Null is returned if the indices are invalid for the specified
313 template<typename InputIterator>
314 static const Type *getIndexedType(const Type *Ptr,
315 InputIterator IdxBegin,
316 InputIterator IdxEnd,
317 // This argument ensures that we
318 // have an iterator we can do
319 // arithmetic on in constant time
320 std::random_access_iterator_tag) {
321 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
324 // This requires that the iterator points to contiguous memory.
325 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
327 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
330 /// Constructors - Create a getelementptr instruction with a base pointer an
331 /// list of indices. The first ctor can optionally insert before an existing
332 /// instruction, the second appends the new instruction to the specified
334 template<typename InputIterator>
335 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
336 InputIterator IdxEnd,
338 const Twine &NameStr,
339 Instruction *InsertBefore);
340 template<typename InputIterator>
341 inline GetElementPtrInst(Value *Ptr,
342 InputIterator IdxBegin, InputIterator IdxEnd,
344 const Twine &NameStr, BasicBlock *InsertAtEnd);
346 /// Constructors - These two constructors are convenience methods because one
347 /// and two index getelementptr instructions are so common.
348 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
349 Instruction *InsertBefore = 0);
350 GetElementPtrInst(Value *Ptr, Value *Idx,
351 const Twine &NameStr, BasicBlock *InsertAtEnd);
353 virtual GetElementPtrInst *clone_impl() const;
355 template<typename InputIterator>
356 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
357 InputIterator IdxEnd,
358 const Twine &NameStr = "",
359 Instruction *InsertBefore = 0) {
360 typename std::iterator_traits<InputIterator>::difference_type Values =
361 1 + std::distance(IdxBegin, IdxEnd);
363 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
365 template<typename InputIterator>
366 static GetElementPtrInst *Create(Value *Ptr,
367 InputIterator IdxBegin, InputIterator IdxEnd,
368 const Twine &NameStr,
369 BasicBlock *InsertAtEnd) {
370 typename std::iterator_traits<InputIterator>::difference_type Values =
371 1 + std::distance(IdxBegin, IdxEnd);
373 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
376 /// Constructors - These two creators are convenience methods because one
377 /// index getelementptr instructions are so common.
378 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
379 const Twine &NameStr = "",
380 Instruction *InsertBefore = 0) {
381 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
383 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
384 const Twine &NameStr,
385 BasicBlock *InsertAtEnd) {
386 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
389 /// Create an "inbounds" getelementptr. See the documentation for the
390 /// "inbounds" flag in LangRef.html for details.
391 template<typename InputIterator>
392 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
393 InputIterator IdxEnd,
394 const Twine &NameStr = "",
395 Instruction *InsertBefore = 0) {
396 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
397 NameStr, InsertBefore);
398 GEP->setIsInBounds(true);
401 template<typename InputIterator>
402 static GetElementPtrInst *CreateInBounds(Value *Ptr,
403 InputIterator IdxBegin,
404 InputIterator IdxEnd,
405 const Twine &NameStr,
406 BasicBlock *InsertAtEnd) {
407 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
408 NameStr, InsertAtEnd);
409 GEP->setIsInBounds(true);
412 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
413 const Twine &NameStr = "",
414 Instruction *InsertBefore = 0) {
415 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
416 GEP->setIsInBounds(true);
419 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
420 const Twine &NameStr,
421 BasicBlock *InsertAtEnd) {
422 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
423 GEP->setIsInBounds(true);
427 /// Transparently provide more efficient getOperand methods.
428 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
430 // getType - Overload to return most specific pointer type...
431 const PointerType *getType() const {
432 return reinterpret_cast<const PointerType*>(Instruction::getType());
435 /// getIndexedType - Returns the type of the element that would be loaded with
436 /// a load instruction with the specified parameters.
438 /// Null is returned if the indices are invalid for the specified
441 template<typename InputIterator>
442 static const Type *getIndexedType(const Type *Ptr,
443 InputIterator IdxBegin,
444 InputIterator IdxEnd) {
445 return getIndexedType(Ptr, IdxBegin, IdxEnd,
446 typename std::iterator_traits<InputIterator>::
447 iterator_category());
450 static const Type *getIndexedType(const Type *Ptr,
451 Value* const *Idx, unsigned NumIdx);
453 static const Type *getIndexedType(const Type *Ptr,
454 uint64_t const *Idx, unsigned NumIdx);
456 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
458 inline op_iterator idx_begin() { return op_begin()+1; }
459 inline const_op_iterator idx_begin() const { return op_begin()+1; }
460 inline op_iterator idx_end() { return op_end(); }
461 inline const_op_iterator idx_end() const { return op_end(); }
463 Value *getPointerOperand() {
464 return getOperand(0);
466 const Value *getPointerOperand() const {
467 return getOperand(0);
469 static unsigned getPointerOperandIndex() {
470 return 0U; // get index for modifying correct operand
473 unsigned getPointerAddressSpace() const {
474 return cast<PointerType>(getType())->getAddressSpace();
477 /// getPointerOperandType - Method to return the pointer operand as a
479 const PointerType *getPointerOperandType() const {
480 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
484 unsigned getNumIndices() const { // Note: always non-negative
485 return getNumOperands() - 1;
488 bool hasIndices() const {
489 return getNumOperands() > 1;
492 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
493 /// zeros. If so, the result pointer and the first operand have the same
494 /// value, just potentially different types.
495 bool hasAllZeroIndices() const;
497 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
498 /// constant integers. If so, the result pointer and the first operand have
499 /// a constant offset between them.
500 bool hasAllConstantIndices() const;
502 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
503 /// See LangRef.html for the meaning of inbounds on a getelementptr.
504 void setIsInBounds(bool b = true);
506 /// isInBounds - Determine whether the GEP has the inbounds flag.
507 bool isInBounds() const;
509 // Methods for support type inquiry through isa, cast, and dyn_cast:
510 static inline bool classof(const GetElementPtrInst *) { return true; }
511 static inline bool classof(const Instruction *I) {
512 return (I->getOpcode() == Instruction::GetElementPtr);
514 static inline bool classof(const Value *V) {
515 return isa<Instruction>(V) && classof(cast<Instruction>(V));
520 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
523 template<typename InputIterator>
524 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
525 InputIterator IdxBegin,
526 InputIterator IdxEnd,
528 const Twine &NameStr,
529 Instruction *InsertBefore)
530 : Instruction(PointerType::get(checkType(
531 getIndexedType(Ptr->getType(),
533 cast<PointerType>(Ptr->getType())
534 ->getAddressSpace()),
536 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
537 Values, InsertBefore) {
538 init(Ptr, IdxBegin, IdxEnd, NameStr,
539 typename std::iterator_traits<InputIterator>::iterator_category());
541 template<typename InputIterator>
542 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
543 InputIterator IdxBegin,
544 InputIterator IdxEnd,
546 const Twine &NameStr,
547 BasicBlock *InsertAtEnd)
548 : Instruction(PointerType::get(checkType(
549 getIndexedType(Ptr->getType(),
551 cast<PointerType>(Ptr->getType())
552 ->getAddressSpace()),
554 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
555 Values, InsertAtEnd) {
556 init(Ptr, IdxBegin, IdxEnd, NameStr,
557 typename std::iterator_traits<InputIterator>::iterator_category());
561 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
564 //===----------------------------------------------------------------------===//
566 //===----------------------------------------------------------------------===//
568 /// This instruction compares its operands according to the predicate given
569 /// to the constructor. It only operates on integers or pointers. The operands
570 /// must be identical types.
571 /// @brief Represent an integer comparison operator.
572 class ICmpInst: public CmpInst {
574 /// @brief Clone an indentical ICmpInst
575 virtual ICmpInst *clone_impl() const;
577 /// @brief Constructor with insert-before-instruction semantics.
579 Instruction *InsertBefore, ///< Where to insert
580 Predicate pred, ///< The predicate to use for the comparison
581 Value *LHS, ///< The left-hand-side of the expression
582 Value *RHS, ///< The right-hand-side of the expression
583 const Twine &NameStr = "" ///< Name of the instruction
584 ) : CmpInst(makeCmpResultType(LHS->getType()),
585 Instruction::ICmp, pred, LHS, RHS, NameStr,
587 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
588 pred <= CmpInst::LAST_ICMP_PREDICATE &&
589 "Invalid ICmp predicate value");
590 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
591 "Both operands to ICmp instruction are not of the same type!");
592 // Check that the operands are the right type
593 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
594 getOperand(0)->getType()->isPointerTy()) &&
595 "Invalid operand types for ICmp instruction");
598 /// @brief Constructor with insert-at-end semantics.
600 BasicBlock &InsertAtEnd, ///< Block to insert into.
601 Predicate pred, ///< The predicate to use for the comparison
602 Value *LHS, ///< The left-hand-side of the expression
603 Value *RHS, ///< The right-hand-side of the expression
604 const Twine &NameStr = "" ///< Name of the instruction
605 ) : CmpInst(makeCmpResultType(LHS->getType()),
606 Instruction::ICmp, pred, LHS, RHS, NameStr,
608 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
609 pred <= CmpInst::LAST_ICMP_PREDICATE &&
610 "Invalid ICmp predicate value");
611 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
612 "Both operands to ICmp instruction are not of the same type!");
613 // Check that the operands are the right type
614 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
615 getOperand(0)->getType()->isPointerTy()) &&
616 "Invalid operand types for ICmp instruction");
619 /// @brief Constructor with no-insertion semantics
621 Predicate pred, ///< The predicate to use for the comparison
622 Value *LHS, ///< The left-hand-side of the expression
623 Value *RHS, ///< The right-hand-side of the expression
624 const Twine &NameStr = "" ///< Name of the instruction
625 ) : CmpInst(makeCmpResultType(LHS->getType()),
626 Instruction::ICmp, pred, LHS, RHS, NameStr) {
627 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
628 pred <= CmpInst::LAST_ICMP_PREDICATE &&
629 "Invalid ICmp predicate value");
630 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
631 "Both operands to ICmp instruction are not of the same type!");
632 // Check that the operands are the right type
633 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
634 getOperand(0)->getType()->isPointerTy()) &&
635 "Invalid operand types for ICmp instruction");
638 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
639 /// @returns the predicate that would be the result if the operand were
640 /// regarded as signed.
641 /// @brief Return the signed version of the predicate
642 Predicate getSignedPredicate() const {
643 return getSignedPredicate(getPredicate());
646 /// This is a static version that you can use without an instruction.
647 /// @brief Return the signed version of the predicate.
648 static Predicate getSignedPredicate(Predicate pred);
650 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
651 /// @returns the predicate that would be the result if the operand were
652 /// regarded as unsigned.
653 /// @brief Return the unsigned version of the predicate
654 Predicate getUnsignedPredicate() const {
655 return getUnsignedPredicate(getPredicate());
658 /// This is a static version that you can use without an instruction.
659 /// @brief Return the unsigned version of the predicate.
660 static Predicate getUnsignedPredicate(Predicate pred);
662 /// isEquality - Return true if this predicate is either EQ or NE. This also
663 /// tests for commutativity.
664 static bool isEquality(Predicate P) {
665 return P == ICMP_EQ || P == ICMP_NE;
668 /// isEquality - Return true if this predicate is either EQ or NE. This also
669 /// tests for commutativity.
670 bool isEquality() const {
671 return isEquality(getPredicate());
674 /// @returns true if the predicate of this ICmpInst is commutative
675 /// @brief Determine if this relation is commutative.
676 bool isCommutative() const { return isEquality(); }
678 /// isRelational - Return true if the predicate is relational (not EQ or NE).
680 bool isRelational() const {
681 return !isEquality();
684 /// isRelational - Return true if the predicate is relational (not EQ or NE).
686 static bool isRelational(Predicate P) {
687 return !isEquality(P);
690 /// Initialize a set of values that all satisfy the predicate with C.
691 /// @brief Make a ConstantRange for a relation with a constant value.
692 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
694 /// Exchange the two operands to this instruction in such a way that it does
695 /// not modify the semantics of the instruction. The predicate value may be
696 /// changed to retain the same result if the predicate is order dependent
698 /// @brief Swap operands and adjust predicate.
699 void swapOperands() {
700 setPredicate(getSwappedPredicate());
701 Op<0>().swap(Op<1>());
704 // Methods for support type inquiry through isa, cast, and dyn_cast:
705 static inline bool classof(const ICmpInst *) { return true; }
706 static inline bool classof(const Instruction *I) {
707 return I->getOpcode() == Instruction::ICmp;
709 static inline bool classof(const Value *V) {
710 return isa<Instruction>(V) && classof(cast<Instruction>(V));
715 //===----------------------------------------------------------------------===//
717 //===----------------------------------------------------------------------===//
719 /// This instruction compares its operands according to the predicate given
720 /// to the constructor. It only operates on floating point values or packed
721 /// vectors of floating point values. The operands must be identical types.
722 /// @brief Represents a floating point comparison operator.
723 class FCmpInst: public CmpInst {
725 /// @brief Clone an indentical FCmpInst
726 virtual FCmpInst *clone_impl() const;
728 /// @brief Constructor with insert-before-instruction semantics.
730 Instruction *InsertBefore, ///< Where to insert
731 Predicate pred, ///< The predicate to use for the comparison
732 Value *LHS, ///< The left-hand-side of the expression
733 Value *RHS, ///< The right-hand-side of the expression
734 const Twine &NameStr = "" ///< Name of the instruction
735 ) : CmpInst(makeCmpResultType(LHS->getType()),
736 Instruction::FCmp, pred, LHS, RHS, NameStr,
738 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
739 "Invalid FCmp predicate value");
740 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
741 "Both operands to FCmp instruction are not of the same type!");
742 // Check that the operands are the right type
743 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
744 "Invalid operand types for FCmp instruction");
747 /// @brief Constructor with insert-at-end semantics.
749 BasicBlock &InsertAtEnd, ///< Block to insert into.
750 Predicate pred, ///< The predicate to use for the comparison
751 Value *LHS, ///< The left-hand-side of the expression
752 Value *RHS, ///< The right-hand-side of the expression
753 const Twine &NameStr = "" ///< Name of the instruction
754 ) : CmpInst(makeCmpResultType(LHS->getType()),
755 Instruction::FCmp, pred, LHS, RHS, NameStr,
757 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
758 "Invalid FCmp predicate value");
759 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
760 "Both operands to FCmp instruction are not of the same type!");
761 // Check that the operands are the right type
762 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
763 "Invalid operand types for FCmp instruction");
766 /// @brief Constructor with no-insertion semantics
768 Predicate pred, ///< The predicate to use for the comparison
769 Value *LHS, ///< The left-hand-side of the expression
770 Value *RHS, ///< The right-hand-side of the expression
771 const Twine &NameStr = "" ///< Name of the instruction
772 ) : CmpInst(makeCmpResultType(LHS->getType()),
773 Instruction::FCmp, pred, LHS, RHS, NameStr) {
774 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
775 "Invalid FCmp predicate value");
776 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
777 "Both operands to FCmp instruction are not of the same type!");
778 // Check that the operands are the right type
779 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
780 "Invalid operand types for FCmp instruction");
783 /// @returns true if the predicate of this instruction is EQ or NE.
784 /// @brief Determine if this is an equality predicate.
785 bool isEquality() const {
786 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
787 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
790 /// @returns true if the predicate of this instruction is commutative.
791 /// @brief Determine if this is a commutative predicate.
792 bool isCommutative() const {
793 return isEquality() ||
794 getPredicate() == FCMP_FALSE ||
795 getPredicate() == FCMP_TRUE ||
796 getPredicate() == FCMP_ORD ||
797 getPredicate() == FCMP_UNO;
800 /// @returns true if the predicate is relational (not EQ or NE).
801 /// @brief Determine if this a relational predicate.
802 bool isRelational() const { return !isEquality(); }
804 /// Exchange the two operands to this instruction in such a way that it does
805 /// not modify the semantics of the instruction. The predicate value may be
806 /// changed to retain the same result if the predicate is order dependent
808 /// @brief Swap operands and adjust predicate.
809 void swapOperands() {
810 setPredicate(getSwappedPredicate());
811 Op<0>().swap(Op<1>());
814 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
815 static inline bool classof(const FCmpInst *) { return true; }
816 static inline bool classof(const Instruction *I) {
817 return I->getOpcode() == Instruction::FCmp;
819 static inline bool classof(const Value *V) {
820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
824 //===----------------------------------------------------------------------===//
825 /// CallInst - This class represents a function call, abstracting a target
826 /// machine's calling convention. This class uses low bit of the SubClassData
827 /// field to indicate whether or not this is a tail call. The rest of the bits
828 /// hold the calling convention of the call.
830 class CallInst : public Instruction {
831 AttrListPtr AttributeList; ///< parameter attributes for call
832 CallInst(const CallInst &CI);
833 void init(Value *Func, Value* const *Params, unsigned NumParams);
834 void init(Value *Func, Value *Actual1, Value *Actual2);
835 void init(Value *Func, Value *Actual);
836 void init(Value *Func);
838 template<typename InputIterator>
839 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
840 const Twine &NameStr,
841 // This argument ensures that we have an iterator we can
842 // do arithmetic on in constant time
843 std::random_access_iterator_tag) {
844 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
846 // This requires that the iterator points to contiguous memory.
847 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
851 /// Construct a CallInst given a range of arguments. InputIterator
852 /// must be a random-access iterator pointing to contiguous storage
853 /// (e.g. a std::vector<>::iterator). Checks are made for
854 /// random-accessness but not for contiguous storage as that would
855 /// incur runtime overhead.
856 /// @brief Construct a CallInst from a range of arguments
857 template<typename InputIterator>
858 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
859 const Twine &NameStr, Instruction *InsertBefore);
861 /// Construct a CallInst given a range of arguments. InputIterator
862 /// must be a random-access iterator pointing to contiguous storage
863 /// (e.g. a std::vector<>::iterator). Checks are made for
864 /// random-accessness but not for contiguous storage as that would
865 /// incur runtime overhead.
866 /// @brief Construct a CallInst from a range of arguments
867 template<typename InputIterator>
868 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
869 const Twine &NameStr, BasicBlock *InsertAtEnd);
871 CallInst(Value *F, Value *Actual, const Twine &NameStr,
872 Instruction *InsertBefore);
873 CallInst(Value *F, Value *Actual, const Twine &NameStr,
874 BasicBlock *InsertAtEnd);
875 explicit CallInst(Value *F, const Twine &NameStr,
876 Instruction *InsertBefore);
877 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
879 virtual CallInst *clone_impl() const;
881 template<typename InputIterator>
882 static CallInst *Create(Value *Func,
883 InputIterator ArgBegin, InputIterator ArgEnd,
884 const Twine &NameStr = "",
885 Instruction *InsertBefore = 0) {
886 return new((unsigned)(ArgEnd - ArgBegin + 1))
887 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
889 template<typename InputIterator>
890 static CallInst *Create(Value *Func,
891 InputIterator ArgBegin, InputIterator ArgEnd,
892 const Twine &NameStr, BasicBlock *InsertAtEnd) {
893 return new((unsigned)(ArgEnd - ArgBegin + 1))
894 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
896 static CallInst *Create(Value *F, Value *Actual,
897 const Twine &NameStr = "",
898 Instruction *InsertBefore = 0) {
899 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
901 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
902 BasicBlock *InsertAtEnd) {
903 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
905 static CallInst *Create(Value *F, const Twine &NameStr = "",
906 Instruction *InsertBefore = 0) {
907 return new(1) CallInst(F, NameStr, InsertBefore);
909 static CallInst *Create(Value *F, const Twine &NameStr,
910 BasicBlock *InsertAtEnd) {
911 return new(1) CallInst(F, NameStr, InsertAtEnd);
913 /// CreateMalloc - Generate the IR for a call to malloc:
914 /// 1. Compute the malloc call's argument as the specified type's size,
915 /// possibly multiplied by the array size if the array size is not
917 /// 2. Call malloc with that argument.
918 /// 3. Bitcast the result of the malloc call to the specified type.
919 static Instruction *CreateMalloc(Instruction *InsertBefore,
920 const Type *IntPtrTy, const Type *AllocTy,
921 Value *AllocSize, Value *ArraySize = 0,
922 const Twine &Name = "");
923 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
924 const Type *IntPtrTy, const Type *AllocTy,
925 Value *AllocSize, Value *ArraySize = 0,
926 Function* MallocF = 0,
927 const Twine &Name = "");
928 /// CreateFree - Generate the IR for a call to the builtin free function.
929 static void CreateFree(Value* Source, Instruction *InsertBefore);
930 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
934 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
935 void setTailCall(bool isTC = true) {
936 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
940 /// Provide fast operand accessors
941 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
943 /// getCallingConv/setCallingConv - Get or set the calling convention of this
945 CallingConv::ID getCallingConv() const {
946 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
948 void setCallingConv(CallingConv::ID CC) {
949 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
950 (static_cast<unsigned>(CC) << 1));
953 /// getAttributes - Return the parameter attributes for this call.
955 const AttrListPtr &getAttributes() const { return AttributeList; }
957 /// setAttributes - Set the parameter attributes for this call.
959 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
961 /// addAttribute - adds the attribute to the list of attributes.
962 void addAttribute(unsigned i, Attributes attr);
964 /// removeAttribute - removes the attribute from the list of attributes.
965 void removeAttribute(unsigned i, Attributes attr);
967 /// @brief Determine whether the call or the callee has the given attribute.
968 bool paramHasAttr(unsigned i, Attributes attr) const;
970 /// @brief Extract the alignment for a call or parameter (0=unknown).
971 unsigned getParamAlignment(unsigned i) const {
972 return AttributeList.getParamAlignment(i);
975 /// @brief Determine if the call does not access memory.
976 bool doesNotAccessMemory() const {
977 return paramHasAttr(~0, Attribute::ReadNone);
979 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
980 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
981 else removeAttribute(~0, Attribute::ReadNone);
984 /// @brief Determine if the call does not access or only reads memory.
985 bool onlyReadsMemory() const {
986 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
988 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
989 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
990 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
993 /// @brief Determine if the call cannot return.
994 bool doesNotReturn() const {
995 return paramHasAttr(~0, Attribute::NoReturn);
997 void setDoesNotReturn(bool DoesNotReturn = true) {
998 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
999 else removeAttribute(~0, Attribute::NoReturn);
1002 /// @brief Determine if the call cannot unwind.
1003 bool doesNotThrow() const {
1004 return paramHasAttr(~0, Attribute::NoUnwind);
1006 void setDoesNotThrow(bool DoesNotThrow = true) {
1007 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1008 else removeAttribute(~0, Attribute::NoUnwind);
1011 /// @brief Determine if the call returns a structure through first
1012 /// pointer argument.
1013 bool hasStructRetAttr() const {
1014 // Be friendly and also check the callee.
1015 return paramHasAttr(1, Attribute::StructRet);
1018 /// @brief Determine if any call argument is an aggregate passed by value.
1019 bool hasByValArgument() const {
1020 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1023 /// getCalledFunction - Return the function called, or null if this is an
1024 /// indirect function invocation.
1026 Function *getCalledFunction() const {
1027 return dyn_cast<Function>(Op<0>());
1030 /// getCalledValue - Get a pointer to the function that is invoked by this
1032 const Value *getCalledValue() const { return Op<0>(); }
1033 Value *getCalledValue() { return Op<0>(); }
1035 /// setCalledFunction - Set the function called.
1036 void setCalledFunction(Value* Fn) {
1040 // Methods for support type inquiry through isa, cast, and dyn_cast:
1041 static inline bool classof(const CallInst *) { return true; }
1042 static inline bool classof(const Instruction *I) {
1043 return I->getOpcode() == Instruction::Call;
1045 static inline bool classof(const Value *V) {
1046 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1049 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1050 // method so that subclasses cannot accidentally use it.
1051 void setInstructionSubclassData(unsigned short D) {
1052 Instruction::setInstructionSubclassData(D);
1057 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1060 template<typename InputIterator>
1061 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1062 const Twine &NameStr, BasicBlock *InsertAtEnd)
1063 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1064 ->getElementType())->getReturnType(),
1066 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1067 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1068 init(Func, ArgBegin, ArgEnd, NameStr,
1069 typename std::iterator_traits<InputIterator>::iterator_category());
1072 template<typename InputIterator>
1073 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1074 const Twine &NameStr, Instruction *InsertBefore)
1075 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1076 ->getElementType())->getReturnType(),
1078 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1079 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1080 init(Func, ArgBegin, ArgEnd, NameStr,
1081 typename std::iterator_traits<InputIterator>::iterator_category());
1084 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1086 //===----------------------------------------------------------------------===//
1088 //===----------------------------------------------------------------------===//
1090 /// SelectInst - This class represents the LLVM 'select' instruction.
1092 class SelectInst : public Instruction {
1093 void init(Value *C, Value *S1, Value *S2) {
1094 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1100 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1101 Instruction *InsertBefore)
1102 : Instruction(S1->getType(), Instruction::Select,
1103 &Op<0>(), 3, InsertBefore) {
1107 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1108 BasicBlock *InsertAtEnd)
1109 : Instruction(S1->getType(), Instruction::Select,
1110 &Op<0>(), 3, InsertAtEnd) {
1115 virtual SelectInst *clone_impl() const;
1117 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1118 const Twine &NameStr = "",
1119 Instruction *InsertBefore = 0) {
1120 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1122 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1123 const Twine &NameStr,
1124 BasicBlock *InsertAtEnd) {
1125 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1128 const Value *getCondition() const { return Op<0>(); }
1129 const Value *getTrueValue() const { return Op<1>(); }
1130 const Value *getFalseValue() const { return Op<2>(); }
1131 Value *getCondition() { return Op<0>(); }
1132 Value *getTrueValue() { return Op<1>(); }
1133 Value *getFalseValue() { return Op<2>(); }
1135 /// areInvalidOperands - Return a string if the specified operands are invalid
1136 /// for a select operation, otherwise return null.
1137 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1139 /// Transparently provide more efficient getOperand methods.
1140 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1142 OtherOps getOpcode() const {
1143 return static_cast<OtherOps>(Instruction::getOpcode());
1146 // Methods for support type inquiry through isa, cast, and dyn_cast:
1147 static inline bool classof(const SelectInst *) { return true; }
1148 static inline bool classof(const Instruction *I) {
1149 return I->getOpcode() == Instruction::Select;
1151 static inline bool classof(const Value *V) {
1152 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1157 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1160 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1162 //===----------------------------------------------------------------------===//
1164 //===----------------------------------------------------------------------===//
1166 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1167 /// an argument of the specified type given a va_list and increments that list
1169 class VAArgInst : public UnaryInstruction {
1171 virtual VAArgInst *clone_impl() const;
1174 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1175 Instruction *InsertBefore = 0)
1176 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1179 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1180 BasicBlock *InsertAtEnd)
1181 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1185 // Methods for support type inquiry through isa, cast, and dyn_cast:
1186 static inline bool classof(const VAArgInst *) { return true; }
1187 static inline bool classof(const Instruction *I) {
1188 return I->getOpcode() == VAArg;
1190 static inline bool classof(const Value *V) {
1191 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1195 //===----------------------------------------------------------------------===//
1196 // ExtractElementInst Class
1197 //===----------------------------------------------------------------------===//
1199 /// ExtractElementInst - This instruction extracts a single (scalar)
1200 /// element from a VectorType value
1202 class ExtractElementInst : public Instruction {
1203 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1204 Instruction *InsertBefore = 0);
1205 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1206 BasicBlock *InsertAtEnd);
1208 virtual ExtractElementInst *clone_impl() const;
1211 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1212 const Twine &NameStr = "",
1213 Instruction *InsertBefore = 0) {
1214 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1216 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1217 const Twine &NameStr,
1218 BasicBlock *InsertAtEnd) {
1219 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1222 /// isValidOperands - Return true if an extractelement instruction can be
1223 /// formed with the specified operands.
1224 static bool isValidOperands(const Value *Vec, const Value *Idx);
1226 Value *getVectorOperand() { return Op<0>(); }
1227 Value *getIndexOperand() { return Op<1>(); }
1228 const Value *getVectorOperand() const { return Op<0>(); }
1229 const Value *getIndexOperand() const { return Op<1>(); }
1231 const VectorType *getVectorOperandType() const {
1232 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1236 /// Transparently provide more efficient getOperand methods.
1237 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1239 // Methods for support type inquiry through isa, cast, and dyn_cast:
1240 static inline bool classof(const ExtractElementInst *) { return true; }
1241 static inline bool classof(const Instruction *I) {
1242 return I->getOpcode() == Instruction::ExtractElement;
1244 static inline bool classof(const Value *V) {
1245 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1250 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1253 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1255 //===----------------------------------------------------------------------===//
1256 // InsertElementInst Class
1257 //===----------------------------------------------------------------------===//
1259 /// InsertElementInst - This instruction inserts a single (scalar)
1260 /// element into a VectorType value
1262 class InsertElementInst : public Instruction {
1263 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1264 const Twine &NameStr = "",
1265 Instruction *InsertBefore = 0);
1266 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1267 const Twine &NameStr, BasicBlock *InsertAtEnd);
1269 virtual InsertElementInst *clone_impl() const;
1272 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1273 const Twine &NameStr = "",
1274 Instruction *InsertBefore = 0) {
1275 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1277 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1278 const Twine &NameStr,
1279 BasicBlock *InsertAtEnd) {
1280 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1283 /// isValidOperands - Return true if an insertelement instruction can be
1284 /// formed with the specified operands.
1285 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1288 /// getType - Overload to return most specific vector type.
1290 const VectorType *getType() const {
1291 return reinterpret_cast<const VectorType*>(Instruction::getType());
1294 /// Transparently provide more efficient getOperand methods.
1295 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1297 // Methods for support type inquiry through isa, cast, and dyn_cast:
1298 static inline bool classof(const InsertElementInst *) { return true; }
1299 static inline bool classof(const Instruction *I) {
1300 return I->getOpcode() == Instruction::InsertElement;
1302 static inline bool classof(const Value *V) {
1303 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1308 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1311 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1313 //===----------------------------------------------------------------------===//
1314 // ShuffleVectorInst Class
1315 //===----------------------------------------------------------------------===//
1317 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1320 class ShuffleVectorInst : public Instruction {
1322 virtual ShuffleVectorInst *clone_impl() const;
1325 // allocate space for exactly three operands
1326 void *operator new(size_t s) {
1327 return User::operator new(s, 3);
1329 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1330 const Twine &NameStr = "",
1331 Instruction *InsertBefor = 0);
1332 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1333 const Twine &NameStr, BasicBlock *InsertAtEnd);
1335 /// isValidOperands - Return true if a shufflevector instruction can be
1336 /// formed with the specified operands.
1337 static bool isValidOperands(const Value *V1, const Value *V2,
1340 /// getType - Overload to return most specific vector type.
1342 const VectorType *getType() const {
1343 return reinterpret_cast<const VectorType*>(Instruction::getType());
1346 /// Transparently provide more efficient getOperand methods.
1347 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1349 /// getMaskValue - Return the index from the shuffle mask for the specified
1350 /// output result. This is either -1 if the element is undef or a number less
1351 /// than 2*numelements.
1352 int getMaskValue(unsigned i) const;
1354 // Methods for support type inquiry through isa, cast, and dyn_cast:
1355 static inline bool classof(const ShuffleVectorInst *) { return true; }
1356 static inline bool classof(const Instruction *I) {
1357 return I->getOpcode() == Instruction::ShuffleVector;
1359 static inline bool classof(const Value *V) {
1360 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1365 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1368 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1370 //===----------------------------------------------------------------------===//
1371 // ExtractValueInst Class
1372 //===----------------------------------------------------------------------===//
1374 /// ExtractValueInst - This instruction extracts a struct member or array
1375 /// element value from an aggregate value.
1377 class ExtractValueInst : public UnaryInstruction {
1378 SmallVector<unsigned, 4> Indices;
1380 ExtractValueInst(const ExtractValueInst &EVI);
1381 void init(const unsigned *Idx, unsigned NumIdx,
1382 const Twine &NameStr);
1383 void init(unsigned Idx, const Twine &NameStr);
1385 template<typename InputIterator>
1386 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1387 const Twine &NameStr,
1388 // This argument ensures that we have an iterator we can
1389 // do arithmetic on in constant time
1390 std::random_access_iterator_tag) {
1391 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1393 // There's no fundamental reason why we require at least one index
1394 // (other than weirdness with &*IdxBegin being invalid; see
1395 // getelementptr's init routine for example). But there's no
1396 // present need to support it.
1397 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1399 // This requires that the iterator points to contiguous memory.
1400 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1401 // we have to build an array here
1404 /// getIndexedType - Returns the type of the element that would be extracted
1405 /// with an extractvalue instruction with the specified parameters.
1407 /// Null is returned if the indices are invalid for the specified
1410 static const Type *getIndexedType(const Type *Agg,
1411 const unsigned *Idx, unsigned NumIdx);
1413 template<typename InputIterator>
1414 static const Type *getIndexedType(const Type *Ptr,
1415 InputIterator IdxBegin,
1416 InputIterator IdxEnd,
1417 // This argument ensures that we
1418 // have an iterator we can do
1419 // arithmetic on in constant time
1420 std::random_access_iterator_tag) {
1421 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1424 // This requires that the iterator points to contiguous memory.
1425 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1427 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1430 /// Constructors - Create a extractvalue instruction with a base aggregate
1431 /// value and a list of indices. The first ctor can optionally insert before
1432 /// an existing instruction, the second appends the new instruction to the
1433 /// specified BasicBlock.
1434 template<typename InputIterator>
1435 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1436 InputIterator IdxEnd,
1437 const Twine &NameStr,
1438 Instruction *InsertBefore);
1439 template<typename InputIterator>
1440 inline ExtractValueInst(Value *Agg,
1441 InputIterator IdxBegin, InputIterator IdxEnd,
1442 const Twine &NameStr, BasicBlock *InsertAtEnd);
1444 // allocate space for exactly one operand
1445 void *operator new(size_t s) {
1446 return User::operator new(s, 1);
1449 virtual ExtractValueInst *clone_impl() const;
1452 template<typename InputIterator>
1453 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1454 InputIterator IdxEnd,
1455 const Twine &NameStr = "",
1456 Instruction *InsertBefore = 0) {
1458 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1460 template<typename InputIterator>
1461 static ExtractValueInst *Create(Value *Agg,
1462 InputIterator IdxBegin, InputIterator IdxEnd,
1463 const Twine &NameStr,
1464 BasicBlock *InsertAtEnd) {
1465 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1468 /// Constructors - These two creators are convenience methods because one
1469 /// index extractvalue instructions are much more common than those with
1471 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1472 const Twine &NameStr = "",
1473 Instruction *InsertBefore = 0) {
1474 unsigned Idxs[1] = { Idx };
1475 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1477 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1478 const Twine &NameStr,
1479 BasicBlock *InsertAtEnd) {
1480 unsigned Idxs[1] = { Idx };
1481 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1484 /// getIndexedType - Returns the type of the element that would be extracted
1485 /// with an extractvalue instruction with the specified parameters.
1487 /// Null is returned if the indices are invalid for the specified
1490 template<typename InputIterator>
1491 static const Type *getIndexedType(const Type *Ptr,
1492 InputIterator IdxBegin,
1493 InputIterator IdxEnd) {
1494 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1495 typename std::iterator_traits<InputIterator>::
1496 iterator_category());
1498 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1500 typedef const unsigned* idx_iterator;
1501 inline idx_iterator idx_begin() const { return Indices.begin(); }
1502 inline idx_iterator idx_end() const { return Indices.end(); }
1504 Value *getAggregateOperand() {
1505 return getOperand(0);
1507 const Value *getAggregateOperand() const {
1508 return getOperand(0);
1510 static unsigned getAggregateOperandIndex() {
1511 return 0U; // get index for modifying correct operand
1514 unsigned getNumIndices() const { // Note: always non-negative
1515 return (unsigned)Indices.size();
1518 bool hasIndices() const {
1522 // Methods for support type inquiry through isa, cast, and dyn_cast:
1523 static inline bool classof(const ExtractValueInst *) { return true; }
1524 static inline bool classof(const Instruction *I) {
1525 return I->getOpcode() == Instruction::ExtractValue;
1527 static inline bool classof(const Value *V) {
1528 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1532 template<typename InputIterator>
1533 ExtractValueInst::ExtractValueInst(Value *Agg,
1534 InputIterator IdxBegin,
1535 InputIterator IdxEnd,
1536 const Twine &NameStr,
1537 Instruction *InsertBefore)
1538 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1540 ExtractValue, Agg, InsertBefore) {
1541 init(IdxBegin, IdxEnd, NameStr,
1542 typename std::iterator_traits<InputIterator>::iterator_category());
1544 template<typename InputIterator>
1545 ExtractValueInst::ExtractValueInst(Value *Agg,
1546 InputIterator IdxBegin,
1547 InputIterator IdxEnd,
1548 const Twine &NameStr,
1549 BasicBlock *InsertAtEnd)
1550 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1552 ExtractValue, Agg, InsertAtEnd) {
1553 init(IdxBegin, IdxEnd, NameStr,
1554 typename std::iterator_traits<InputIterator>::iterator_category());
1558 //===----------------------------------------------------------------------===//
1559 // InsertValueInst Class
1560 //===----------------------------------------------------------------------===//
1562 /// InsertValueInst - This instruction inserts a struct field of array element
1563 /// value into an aggregate value.
1565 class InsertValueInst : public Instruction {
1566 SmallVector<unsigned, 4> Indices;
1568 void *operator new(size_t, unsigned); // Do not implement
1569 InsertValueInst(const InsertValueInst &IVI);
1570 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1571 const Twine &NameStr);
1572 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1574 template<typename InputIterator>
1575 void init(Value *Agg, Value *Val,
1576 InputIterator IdxBegin, InputIterator IdxEnd,
1577 const Twine &NameStr,
1578 // This argument ensures that we have an iterator we can
1579 // do arithmetic on in constant time
1580 std::random_access_iterator_tag) {
1581 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1583 // There's no fundamental reason why we require at least one index
1584 // (other than weirdness with &*IdxBegin being invalid; see
1585 // getelementptr's init routine for example). But there's no
1586 // present need to support it.
1587 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1589 // This requires that the iterator points to contiguous memory.
1590 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1591 // we have to build an array here
1594 /// Constructors - Create a insertvalue instruction with a base aggregate
1595 /// value, a value to insert, and a list of indices. The first ctor can
1596 /// optionally insert before an existing instruction, the second appends
1597 /// the new instruction to the specified BasicBlock.
1598 template<typename InputIterator>
1599 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1600 InputIterator IdxEnd,
1601 const Twine &NameStr,
1602 Instruction *InsertBefore);
1603 template<typename InputIterator>
1604 inline InsertValueInst(Value *Agg, Value *Val,
1605 InputIterator IdxBegin, InputIterator IdxEnd,
1606 const Twine &NameStr, BasicBlock *InsertAtEnd);
1608 /// Constructors - These two constructors are convenience methods because one
1609 /// and two index insertvalue instructions are so common.
1610 InsertValueInst(Value *Agg, Value *Val,
1611 unsigned Idx, const Twine &NameStr = "",
1612 Instruction *InsertBefore = 0);
1613 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1614 const Twine &NameStr, BasicBlock *InsertAtEnd);
1616 virtual InsertValueInst *clone_impl() const;
1618 // allocate space for exactly two operands
1619 void *operator new(size_t s) {
1620 return User::operator new(s, 2);
1623 template<typename InputIterator>
1624 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1625 InputIterator IdxEnd,
1626 const Twine &NameStr = "",
1627 Instruction *InsertBefore = 0) {
1628 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1629 NameStr, InsertBefore);
1631 template<typename InputIterator>
1632 static InsertValueInst *Create(Value *Agg, Value *Val,
1633 InputIterator IdxBegin, InputIterator IdxEnd,
1634 const Twine &NameStr,
1635 BasicBlock *InsertAtEnd) {
1636 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1637 NameStr, InsertAtEnd);
1640 /// Constructors - These two creators are convenience methods because one
1641 /// index insertvalue instructions are much more common than those with
1643 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1644 const Twine &NameStr = "",
1645 Instruction *InsertBefore = 0) {
1646 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1648 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1649 const Twine &NameStr,
1650 BasicBlock *InsertAtEnd) {
1651 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1654 /// Transparently provide more efficient getOperand methods.
1655 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1657 typedef const unsigned* idx_iterator;
1658 inline idx_iterator idx_begin() const { return Indices.begin(); }
1659 inline idx_iterator idx_end() const { return Indices.end(); }
1661 Value *getAggregateOperand() {
1662 return getOperand(0);
1664 const Value *getAggregateOperand() const {
1665 return getOperand(0);
1667 static unsigned getAggregateOperandIndex() {
1668 return 0U; // get index for modifying correct operand
1671 Value *getInsertedValueOperand() {
1672 return getOperand(1);
1674 const Value *getInsertedValueOperand() const {
1675 return getOperand(1);
1677 static unsigned getInsertedValueOperandIndex() {
1678 return 1U; // get index for modifying correct operand
1681 unsigned getNumIndices() const { // Note: always non-negative
1682 return (unsigned)Indices.size();
1685 bool hasIndices() const {
1689 // Methods for support type inquiry through isa, cast, and dyn_cast:
1690 static inline bool classof(const InsertValueInst *) { return true; }
1691 static inline bool classof(const Instruction *I) {
1692 return I->getOpcode() == Instruction::InsertValue;
1694 static inline bool classof(const Value *V) {
1695 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1700 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1703 template<typename InputIterator>
1704 InsertValueInst::InsertValueInst(Value *Agg,
1706 InputIterator IdxBegin,
1707 InputIterator IdxEnd,
1708 const Twine &NameStr,
1709 Instruction *InsertBefore)
1710 : Instruction(Agg->getType(), InsertValue,
1711 OperandTraits<InsertValueInst>::op_begin(this),
1713 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1714 typename std::iterator_traits<InputIterator>::iterator_category());
1716 template<typename InputIterator>
1717 InsertValueInst::InsertValueInst(Value *Agg,
1719 InputIterator IdxBegin,
1720 InputIterator IdxEnd,
1721 const Twine &NameStr,
1722 BasicBlock *InsertAtEnd)
1723 : Instruction(Agg->getType(), InsertValue,
1724 OperandTraits<InsertValueInst>::op_begin(this),
1726 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1727 typename std::iterator_traits<InputIterator>::iterator_category());
1730 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1732 //===----------------------------------------------------------------------===//
1734 //===----------------------------------------------------------------------===//
1736 // PHINode - The PHINode class is used to represent the magical mystical PHI
1737 // node, that can not exist in nature, but can be synthesized in a computer
1738 // scientist's overactive imagination.
1740 class PHINode : public Instruction {
1741 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1742 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1743 /// the number actually in use.
1744 unsigned ReservedSpace;
1745 PHINode(const PHINode &PN);
1746 // allocate space for exactly zero operands
1747 void *operator new(size_t s) {
1748 return User::operator new(s, 0);
1750 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1751 Instruction *InsertBefore = 0)
1752 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1757 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1758 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1763 virtual PHINode *clone_impl() const;
1765 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1766 Instruction *InsertBefore = 0) {
1767 return new PHINode(Ty, NameStr, InsertBefore);
1769 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1770 BasicBlock *InsertAtEnd) {
1771 return new PHINode(Ty, NameStr, InsertAtEnd);
1775 /// reserveOperandSpace - This method can be used to avoid repeated
1776 /// reallocation of PHI operand lists by reserving space for the correct
1777 /// number of operands before adding them. Unlike normal vector reserves,
1778 /// this method can also be used to trim the operand space.
1779 void reserveOperandSpace(unsigned NumValues) {
1780 resizeOperands(NumValues*2);
1783 /// Provide fast operand accessors
1784 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1786 /// getNumIncomingValues - Return the number of incoming edges
1788 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1790 /// getIncomingValue - Return incoming value number x
1792 Value *getIncomingValue(unsigned i) const {
1793 assert(i*2 < getNumOperands() && "Invalid value number!");
1794 return getOperand(i*2);
1796 void setIncomingValue(unsigned i, Value *V) {
1797 assert(i*2 < getNumOperands() && "Invalid value number!");
1800 static unsigned getOperandNumForIncomingValue(unsigned i) {
1803 static unsigned getIncomingValueNumForOperand(unsigned i) {
1804 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1808 /// getIncomingBlock - Return incoming basic block number @p i.
1810 BasicBlock *getIncomingBlock(unsigned i) const {
1811 return cast<BasicBlock>(getOperand(i*2+1));
1814 /// getIncomingBlock - Return incoming basic block corresponding
1815 /// to an operand of the PHI.
1817 BasicBlock *getIncomingBlock(const Use &U) const {
1818 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1819 return cast<BasicBlock>((&U + 1)->get());
1822 /// getIncomingBlock - Return incoming basic block corresponding
1823 /// to value use iterator.
1825 template <typename U>
1826 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1827 return getIncomingBlock(I.getUse());
1831 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1832 setOperand(i*2+1, (Value*)BB);
1834 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1837 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1838 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1842 /// addIncoming - Add an incoming value to the end of the PHI list
1844 void addIncoming(Value *V, BasicBlock *BB) {
1845 assert(V && "PHI node got a null value!");
1846 assert(BB && "PHI node got a null basic block!");
1847 assert(getType() == V->getType() &&
1848 "All operands to PHI node must be the same type as the PHI node!");
1849 unsigned OpNo = NumOperands;
1850 if (OpNo+2 > ReservedSpace)
1851 resizeOperands(0); // Get more space!
1852 // Initialize some new operands.
1853 NumOperands = OpNo+2;
1854 OperandList[OpNo] = V;
1855 OperandList[OpNo+1] = (Value*)BB;
1858 /// removeIncomingValue - Remove an incoming value. This is useful if a
1859 /// predecessor basic block is deleted. The value removed is returned.
1861 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1862 /// is true), the PHI node is destroyed and any uses of it are replaced with
1863 /// dummy values. The only time there should be zero incoming values to a PHI
1864 /// node is when the block is dead, so this strategy is sound.
1866 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1868 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1869 int Idx = getBasicBlockIndex(BB);
1870 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1871 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1874 /// getBasicBlockIndex - Return the first index of the specified basic
1875 /// block in the value list for this PHI. Returns -1 if no instance.
1877 int getBasicBlockIndex(const BasicBlock *BB) const {
1878 Use *OL = OperandList;
1879 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1880 if (OL[i+1].get() == (const Value*)BB) return i/2;
1884 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1885 return getIncomingValue(getBasicBlockIndex(BB));
1888 /// hasConstantValue - If the specified PHI node always merges together the
1889 /// same value, return the value, otherwise return null.
1891 /// If the PHI has undef operands, but all the rest of the operands are
1892 /// some unique value, return that value if it can be proved that the
1893 /// value dominates the PHI. If DT is null, use a conservative check,
1894 /// otherwise use DT to test for dominance.
1896 Value *hasConstantValue(DominatorTree *DT = 0) const;
1898 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1899 static inline bool classof(const PHINode *) { return true; }
1900 static inline bool classof(const Instruction *I) {
1901 return I->getOpcode() == Instruction::PHI;
1903 static inline bool classof(const Value *V) {
1904 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1907 void resizeOperands(unsigned NumOperands);
1911 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1914 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1917 //===----------------------------------------------------------------------===//
1919 //===----------------------------------------------------------------------===//
1921 //===---------------------------------------------------------------------------
1922 /// ReturnInst - Return a value (possibly void), from a function. Execution
1923 /// does not continue in this function any longer.
1925 class ReturnInst : public TerminatorInst {
1926 ReturnInst(const ReturnInst &RI);
1929 // ReturnInst constructors:
1930 // ReturnInst() - 'ret void' instruction
1931 // ReturnInst( null) - 'ret void' instruction
1932 // ReturnInst(Value* X) - 'ret X' instruction
1933 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1934 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1935 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1936 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1938 // NOTE: If the Value* passed is of type void then the constructor behaves as
1939 // if it was passed NULL.
1940 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1941 Instruction *InsertBefore = 0);
1942 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1943 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1945 virtual ReturnInst *clone_impl() const;
1947 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1948 Instruction *InsertBefore = 0) {
1949 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1951 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1952 BasicBlock *InsertAtEnd) {
1953 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1955 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1956 return new(0) ReturnInst(C, InsertAtEnd);
1958 virtual ~ReturnInst();
1960 /// Provide fast operand accessors
1961 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1963 /// Convenience accessor
1964 Value *getReturnValue(unsigned n = 0) const {
1965 return n < getNumOperands()
1970 unsigned getNumSuccessors() const { return 0; }
1972 // Methods for support type inquiry through isa, cast, and dyn_cast:
1973 static inline bool classof(const ReturnInst *) { return true; }
1974 static inline bool classof(const Instruction *I) {
1975 return (I->getOpcode() == Instruction::Ret);
1977 static inline bool classof(const Value *V) {
1978 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1981 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1982 virtual unsigned getNumSuccessorsV() const;
1983 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1987 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<> {
1990 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1992 //===----------------------------------------------------------------------===//
1994 //===----------------------------------------------------------------------===//
1996 //===---------------------------------------------------------------------------
1997 /// BranchInst - Conditional or Unconditional Branch instruction.
1999 class BranchInst : public TerminatorInst {
2000 /// Ops list - Branches are strange. The operands are ordered:
2001 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2002 /// they don't have to check for cond/uncond branchness. These are mostly
2003 /// accessed relative from op_end().
2004 BranchInst(const BranchInst &BI);
2006 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2007 // BranchInst(BB *B) - 'br B'
2008 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2009 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2010 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2011 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2012 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2013 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2014 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2015 Instruction *InsertBefore = 0);
2016 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2017 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2018 BasicBlock *InsertAtEnd);
2020 virtual BranchInst *clone_impl() const;
2022 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2023 return new(1, true) BranchInst(IfTrue, InsertBefore);
2025 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2026 Value *Cond, Instruction *InsertBefore = 0) {
2027 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2029 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2030 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2032 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2033 Value *Cond, BasicBlock *InsertAtEnd) {
2034 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2039 /// Transparently provide more efficient getOperand methods.
2040 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2042 bool isUnconditional() const { return getNumOperands() == 1; }
2043 bool isConditional() const { return getNumOperands() == 3; }
2045 Value *getCondition() const {
2046 assert(isConditional() && "Cannot get condition of an uncond branch!");
2050 void setCondition(Value *V) {
2051 assert(isConditional() && "Cannot set condition of unconditional branch!");
2055 // setUnconditionalDest - Change the current branch to an unconditional branch
2056 // targeting the specified block.
2057 // FIXME: Eliminate this ugly method.
2058 void setUnconditionalDest(BasicBlock *Dest) {
2059 Op<-1>() = (Value*)Dest;
2060 if (isConditional()) { // Convert this to an uncond branch.
2064 OperandList = op_begin();
2068 unsigned getNumSuccessors() const { return 1+isConditional(); }
2070 BasicBlock *getSuccessor(unsigned i) const {
2071 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2072 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2075 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2076 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2077 *(&Op<-1>() - idx) = (Value*)NewSucc;
2080 // Methods for support type inquiry through isa, cast, and dyn_cast:
2081 static inline bool classof(const BranchInst *) { return true; }
2082 static inline bool classof(const Instruction *I) {
2083 return (I->getOpcode() == Instruction::Br);
2085 static inline bool classof(const Value *V) {
2086 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2089 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2090 virtual unsigned getNumSuccessorsV() const;
2091 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2095 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2097 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2099 //===----------------------------------------------------------------------===//
2101 //===----------------------------------------------------------------------===//
2103 //===---------------------------------------------------------------------------
2104 /// SwitchInst - Multiway switch
2106 class SwitchInst : public TerminatorInst {
2107 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2108 unsigned ReservedSpace;
2109 // Operand[0] = Value to switch on
2110 // Operand[1] = Default basic block destination
2111 // Operand[2n ] = Value to match
2112 // Operand[2n+1] = BasicBlock to go to on match
2113 SwitchInst(const SwitchInst &SI);
2114 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2115 void resizeOperands(unsigned No);
2116 // allocate space for exactly zero operands
2117 void *operator new(size_t s) {
2118 return User::operator new(s, 0);
2120 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2121 /// switch on and a default destination. The number of additional cases can
2122 /// be specified here to make memory allocation more efficient. This
2123 /// constructor can also autoinsert before another instruction.
2124 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2125 Instruction *InsertBefore);
2127 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2128 /// switch on and a default destination. The number of additional cases can
2129 /// be specified here to make memory allocation more efficient. This
2130 /// constructor also autoinserts at the end of the specified BasicBlock.
2131 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2132 BasicBlock *InsertAtEnd);
2134 virtual SwitchInst *clone_impl() const;
2136 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2137 unsigned NumCases, Instruction *InsertBefore = 0) {
2138 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2140 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2141 unsigned NumCases, BasicBlock *InsertAtEnd) {
2142 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2146 /// Provide fast operand accessors
2147 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2149 // Accessor Methods for Switch stmt
2150 Value *getCondition() const { return getOperand(0); }
2151 void setCondition(Value *V) { setOperand(0, V); }
2153 BasicBlock *getDefaultDest() const {
2154 return cast<BasicBlock>(getOperand(1));
2157 /// getNumCases - return the number of 'cases' in this switch instruction.
2158 /// Note that case #0 is always the default case.
2159 unsigned getNumCases() const {
2160 return getNumOperands()/2;
2163 /// getCaseValue - Return the specified case value. Note that case #0, the
2164 /// default destination, does not have a case value.
2165 ConstantInt *getCaseValue(unsigned i) {
2166 assert(i && i < getNumCases() && "Illegal case value to get!");
2167 return getSuccessorValue(i);
2170 /// getCaseValue - Return the specified case value. Note that case #0, the
2171 /// default destination, does not have a case value.
2172 const ConstantInt *getCaseValue(unsigned i) const {
2173 assert(i && i < getNumCases() && "Illegal case value to get!");
2174 return getSuccessorValue(i);
2177 /// findCaseValue - Search all of the case values for the specified constant.
2178 /// If it is explicitly handled, return the case number of it, otherwise
2179 /// return 0 to indicate that it is handled by the default handler.
2180 unsigned findCaseValue(const ConstantInt *C) const {
2181 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2182 if (getCaseValue(i) == C)
2187 /// findCaseDest - Finds the unique case value for a given successor. Returns
2188 /// null if the successor is not found, not unique, or is the default case.
2189 ConstantInt *findCaseDest(BasicBlock *BB) {
2190 if (BB == getDefaultDest()) return NULL;
2192 ConstantInt *CI = NULL;
2193 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2194 if (getSuccessor(i) == BB) {
2195 if (CI) return NULL; // Multiple cases lead to BB.
2196 else CI = getCaseValue(i);
2202 /// addCase - Add an entry to the switch instruction...
2204 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2206 /// removeCase - This method removes the specified successor from the switch
2207 /// instruction. Note that this cannot be used to remove the default
2208 /// destination (successor #0).
2210 void removeCase(unsigned idx);
2212 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2213 BasicBlock *getSuccessor(unsigned idx) const {
2214 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2215 return cast<BasicBlock>(getOperand(idx*2+1));
2217 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2218 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2219 setOperand(idx*2+1, (Value*)NewSucc);
2222 // getSuccessorValue - Return the value associated with the specified
2224 ConstantInt *getSuccessorValue(unsigned idx) const {
2225 assert(idx < getNumSuccessors() && "Successor # out of range!");
2226 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2229 // Methods for support type inquiry through isa, cast, and dyn_cast:
2230 static inline bool classof(const SwitchInst *) { return true; }
2231 static inline bool classof(const Instruction *I) {
2232 return I->getOpcode() == Instruction::Switch;
2234 static inline bool classof(const Value *V) {
2235 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2238 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2239 virtual unsigned getNumSuccessorsV() const;
2240 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2244 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2247 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2250 //===----------------------------------------------------------------------===//
2251 // IndirectBrInst Class
2252 //===----------------------------------------------------------------------===//
2254 //===---------------------------------------------------------------------------
2255 /// IndirectBrInst - Indirect Branch Instruction.
2257 class IndirectBrInst : public TerminatorInst {
2258 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2259 unsigned ReservedSpace;
2260 // Operand[0] = Value to switch on
2261 // Operand[1] = Default basic block destination
2262 // Operand[2n ] = Value to match
2263 // Operand[2n+1] = BasicBlock to go to on match
2264 IndirectBrInst(const IndirectBrInst &IBI);
2265 void init(Value *Address, unsigned NumDests);
2266 void resizeOperands(unsigned No);
2267 // allocate space for exactly zero operands
2268 void *operator new(size_t s) {
2269 return User::operator new(s, 0);
2271 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2272 /// Address to jump to. The number of expected destinations can be specified
2273 /// here to make memory allocation more efficient. This constructor can also
2274 /// autoinsert before another instruction.
2275 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2277 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2278 /// Address to jump to. The number of expected destinations can be specified
2279 /// here to make memory allocation more efficient. This constructor also
2280 /// autoinserts at the end of the specified BasicBlock.
2281 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2283 virtual IndirectBrInst *clone_impl() const;
2285 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2286 Instruction *InsertBefore = 0) {
2287 return new IndirectBrInst(Address, NumDests, InsertBefore);
2289 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2290 BasicBlock *InsertAtEnd) {
2291 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2295 /// Provide fast operand accessors.
2296 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2298 // Accessor Methods for IndirectBrInst instruction.
2299 Value *getAddress() { return getOperand(0); }
2300 const Value *getAddress() const { return getOperand(0); }
2301 void setAddress(Value *V) { setOperand(0, V); }
2304 /// getNumDestinations - return the number of possible destinations in this
2305 /// indirectbr instruction.
2306 unsigned getNumDestinations() const { return getNumOperands()-1; }
2308 /// getDestination - Return the specified destination.
2309 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2310 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2312 /// addDestination - Add a destination.
2314 void addDestination(BasicBlock *Dest);
2316 /// removeDestination - This method removes the specified successor from the
2317 /// indirectbr instruction.
2318 void removeDestination(unsigned i);
2320 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2321 BasicBlock *getSuccessor(unsigned i) const {
2322 return cast<BasicBlock>(getOperand(i+1));
2324 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2325 setOperand(i+1, (Value*)NewSucc);
2328 // Methods for support type inquiry through isa, cast, and dyn_cast:
2329 static inline bool classof(const IndirectBrInst *) { return true; }
2330 static inline bool classof(const Instruction *I) {
2331 return I->getOpcode() == Instruction::IndirectBr;
2333 static inline bool classof(const Value *V) {
2334 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2337 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2338 virtual unsigned getNumSuccessorsV() const;
2339 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2343 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2346 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2349 //===----------------------------------------------------------------------===//
2351 //===----------------------------------------------------------------------===//
2353 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2354 /// calling convention of the call.
2356 class InvokeInst : public TerminatorInst {
2357 AttrListPtr AttributeList;
2358 InvokeInst(const InvokeInst &BI);
2359 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2360 Value* const *Args, unsigned NumArgs);
2362 template<typename InputIterator>
2363 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2364 InputIterator ArgBegin, InputIterator ArgEnd,
2365 const Twine &NameStr,
2366 // This argument ensures that we have an iterator we can
2367 // do arithmetic on in constant time
2368 std::random_access_iterator_tag) {
2369 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2371 // This requires that the iterator points to contiguous memory.
2372 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2376 /// Construct an InvokeInst given a range of arguments.
2377 /// InputIterator must be a random-access iterator pointing to
2378 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2379 /// made for random-accessness but not for contiguous storage as
2380 /// that would incur runtime overhead.
2382 /// @brief Construct an InvokeInst from a range of arguments
2383 template<typename InputIterator>
2384 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2385 InputIterator ArgBegin, InputIterator ArgEnd,
2387 const Twine &NameStr, Instruction *InsertBefore);
2389 /// Construct an InvokeInst given a range of arguments.
2390 /// InputIterator must be a random-access iterator pointing to
2391 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2392 /// made for random-accessness but not for contiguous storage as
2393 /// that would incur runtime overhead.
2395 /// @brief Construct an InvokeInst from a range of arguments
2396 template<typename InputIterator>
2397 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2398 InputIterator ArgBegin, InputIterator ArgEnd,
2400 const Twine &NameStr, BasicBlock *InsertAtEnd);
2402 virtual InvokeInst *clone_impl() const;
2404 template<typename InputIterator>
2405 static InvokeInst *Create(Value *Func,
2406 BasicBlock *IfNormal, BasicBlock *IfException,
2407 InputIterator ArgBegin, InputIterator ArgEnd,
2408 const Twine &NameStr = "",
2409 Instruction *InsertBefore = 0) {
2410 unsigned Values(ArgEnd - ArgBegin + 3);
2411 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2412 Values, NameStr, InsertBefore);
2414 template<typename InputIterator>
2415 static InvokeInst *Create(Value *Func,
2416 BasicBlock *IfNormal, BasicBlock *IfException,
2417 InputIterator ArgBegin, InputIterator ArgEnd,
2418 const Twine &NameStr,
2419 BasicBlock *InsertAtEnd) {
2420 unsigned Values(ArgEnd - ArgBegin + 3);
2421 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2422 Values, NameStr, InsertAtEnd);
2425 /// Provide fast operand accessors
2426 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2428 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2430 CallingConv::ID getCallingConv() const {
2431 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2433 void setCallingConv(CallingConv::ID CC) {
2434 setInstructionSubclassData(static_cast<unsigned>(CC));
2437 /// getAttributes - Return the parameter attributes for this invoke.
2439 const AttrListPtr &getAttributes() const { return AttributeList; }
2441 /// setAttributes - Set the parameter attributes for this invoke.
2443 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2445 /// addAttribute - adds the attribute to the list of attributes.
2446 void addAttribute(unsigned i, Attributes attr);
2448 /// removeAttribute - removes the attribute from the list of attributes.
2449 void removeAttribute(unsigned i, Attributes attr);
2451 /// @brief Determine whether the call or the callee has the given attribute.
2452 bool paramHasAttr(unsigned i, Attributes attr) const;
2454 /// @brief Extract the alignment for a call or parameter (0=unknown).
2455 unsigned getParamAlignment(unsigned i) const {
2456 return AttributeList.getParamAlignment(i);
2459 /// @brief Determine if the call does not access memory.
2460 bool doesNotAccessMemory() const {
2461 return paramHasAttr(~0, Attribute::ReadNone);
2463 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2464 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2465 else removeAttribute(~0, Attribute::ReadNone);
2468 /// @brief Determine if the call does not access or only reads memory.
2469 bool onlyReadsMemory() const {
2470 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2472 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2473 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2474 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2477 /// @brief Determine if the call cannot return.
2478 bool doesNotReturn() const {
2479 return paramHasAttr(~0, Attribute::NoReturn);
2481 void setDoesNotReturn(bool DoesNotReturn = true) {
2482 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2483 else removeAttribute(~0, Attribute::NoReturn);
2486 /// @brief Determine if the call cannot unwind.
2487 bool doesNotThrow() const {
2488 return paramHasAttr(~0, Attribute::NoUnwind);
2490 void setDoesNotThrow(bool DoesNotThrow = true) {
2491 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2492 else removeAttribute(~0, Attribute::NoUnwind);
2495 /// @brief Determine if the call returns a structure through first
2496 /// pointer argument.
2497 bool hasStructRetAttr() const {
2498 // Be friendly and also check the callee.
2499 return paramHasAttr(1, Attribute::StructRet);
2502 /// @brief Determine if any call argument is an aggregate passed by value.
2503 bool hasByValArgument() const {
2504 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2507 /// getCalledFunction - Return the function called, or null if this is an
2508 /// indirect function invocation.
2510 Function *getCalledFunction() const {
2511 return dyn_cast<Function>(getOperand(0));
2514 /// getCalledValue - Get a pointer to the function that is invoked by this
2516 const Value *getCalledValue() const { return getOperand(0); }
2517 Value *getCalledValue() { return getOperand(0); }
2519 // get*Dest - Return the destination basic blocks...
2520 BasicBlock *getNormalDest() const {
2521 return cast<BasicBlock>(getOperand(1));
2523 BasicBlock *getUnwindDest() const {
2524 return cast<BasicBlock>(getOperand(2));
2526 void setNormalDest(BasicBlock *B) {
2527 setOperand(1, (Value*)B);
2530 void setUnwindDest(BasicBlock *B) {
2531 setOperand(2, (Value*)B);
2534 BasicBlock *getSuccessor(unsigned i) const {
2535 assert(i < 2 && "Successor # out of range for invoke!");
2536 return i == 0 ? getNormalDest() : getUnwindDest();
2539 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2540 assert(idx < 2 && "Successor # out of range for invoke!");
2541 setOperand(idx+1, (Value*)NewSucc);
2544 unsigned getNumSuccessors() const { return 2; }
2546 // Methods for support type inquiry through isa, cast, and dyn_cast:
2547 static inline bool classof(const InvokeInst *) { return true; }
2548 static inline bool classof(const Instruction *I) {
2549 return (I->getOpcode() == Instruction::Invoke);
2551 static inline bool classof(const Value *V) {
2552 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2555 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2556 virtual unsigned getNumSuccessorsV() const;
2557 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2559 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2560 // method so that subclasses cannot accidentally use it.
2561 void setInstructionSubclassData(unsigned short D) {
2562 Instruction::setInstructionSubclassData(D);
2567 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2570 template<typename InputIterator>
2571 InvokeInst::InvokeInst(Value *Func,
2572 BasicBlock *IfNormal, BasicBlock *IfException,
2573 InputIterator ArgBegin, InputIterator ArgEnd,
2575 const Twine &NameStr, Instruction *InsertBefore)
2576 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2577 ->getElementType())->getReturnType(),
2578 Instruction::Invoke,
2579 OperandTraits<InvokeInst>::op_end(this) - Values,
2580 Values, InsertBefore) {
2581 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2582 typename std::iterator_traits<InputIterator>::iterator_category());
2584 template<typename InputIterator>
2585 InvokeInst::InvokeInst(Value *Func,
2586 BasicBlock *IfNormal, BasicBlock *IfException,
2587 InputIterator ArgBegin, InputIterator ArgEnd,
2589 const Twine &NameStr, BasicBlock *InsertAtEnd)
2590 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2591 ->getElementType())->getReturnType(),
2592 Instruction::Invoke,
2593 OperandTraits<InvokeInst>::op_end(this) - Values,
2594 Values, InsertAtEnd) {
2595 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2596 typename std::iterator_traits<InputIterator>::iterator_category());
2599 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2601 //===----------------------------------------------------------------------===//
2603 //===----------------------------------------------------------------------===//
2605 //===---------------------------------------------------------------------------
2606 /// UnwindInst - Immediately exit the current function, unwinding the stack
2607 /// until an invoke instruction is found.
2609 class UnwindInst : public TerminatorInst {
2610 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2612 virtual UnwindInst *clone_impl() const;
2614 // allocate space for exactly zero operands
2615 void *operator new(size_t s) {
2616 return User::operator new(s, 0);
2618 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2619 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2621 unsigned getNumSuccessors() const { return 0; }
2623 // Methods for support type inquiry through isa, cast, and dyn_cast:
2624 static inline bool classof(const UnwindInst *) { return true; }
2625 static inline bool classof(const Instruction *I) {
2626 return I->getOpcode() == Instruction::Unwind;
2628 static inline bool classof(const Value *V) {
2629 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2632 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2633 virtual unsigned getNumSuccessorsV() const;
2634 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2637 //===----------------------------------------------------------------------===//
2638 // UnreachableInst Class
2639 //===----------------------------------------------------------------------===//
2641 //===---------------------------------------------------------------------------
2642 /// UnreachableInst - This function has undefined behavior. In particular, the
2643 /// presence of this instruction indicates some higher level knowledge that the
2644 /// end of the block cannot be reached.
2646 class UnreachableInst : public TerminatorInst {
2647 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2649 virtual UnreachableInst *clone_impl() const;
2652 // allocate space for exactly zero operands
2653 void *operator new(size_t s) {
2654 return User::operator new(s, 0);
2656 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2657 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2659 unsigned getNumSuccessors() const { return 0; }
2661 // Methods for support type inquiry through isa, cast, and dyn_cast:
2662 static inline bool classof(const UnreachableInst *) { return true; }
2663 static inline bool classof(const Instruction *I) {
2664 return I->getOpcode() == Instruction::Unreachable;
2666 static inline bool classof(const Value *V) {
2667 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2670 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2671 virtual unsigned getNumSuccessorsV() const;
2672 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2675 //===----------------------------------------------------------------------===//
2677 //===----------------------------------------------------------------------===//
2679 /// @brief This class represents a truncation of integer types.
2680 class TruncInst : public CastInst {
2682 /// @brief Clone an identical TruncInst
2683 virtual TruncInst *clone_impl() const;
2686 /// @brief Constructor with insert-before-instruction semantics
2688 Value *S, ///< The value to be truncated
2689 const Type *Ty, ///< The (smaller) type to truncate to
2690 const Twine &NameStr = "", ///< A name for the new instruction
2691 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2694 /// @brief Constructor with insert-at-end-of-block semantics
2696 Value *S, ///< The value to be truncated
2697 const Type *Ty, ///< The (smaller) type to truncate to
2698 const Twine &NameStr, ///< A name for the new instruction
2699 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2702 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2703 static inline bool classof(const TruncInst *) { return true; }
2704 static inline bool classof(const Instruction *I) {
2705 return I->getOpcode() == Trunc;
2707 static inline bool classof(const Value *V) {
2708 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2712 //===----------------------------------------------------------------------===//
2714 //===----------------------------------------------------------------------===//
2716 /// @brief This class represents zero extension of integer types.
2717 class ZExtInst : public CastInst {
2719 /// @brief Clone an identical ZExtInst
2720 virtual ZExtInst *clone_impl() const;
2723 /// @brief Constructor with insert-before-instruction semantics
2725 Value *S, ///< The value to be zero extended
2726 const Type *Ty, ///< The type to zero extend to
2727 const Twine &NameStr = "", ///< A name for the new instruction
2728 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2731 /// @brief Constructor with insert-at-end semantics.
2733 Value *S, ///< The value to be zero extended
2734 const Type *Ty, ///< The type to zero extend to
2735 const Twine &NameStr, ///< A name for the new instruction
2736 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2739 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2740 static inline bool classof(const ZExtInst *) { return true; }
2741 static inline bool classof(const Instruction *I) {
2742 return I->getOpcode() == ZExt;
2744 static inline bool classof(const Value *V) {
2745 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2749 //===----------------------------------------------------------------------===//
2751 //===----------------------------------------------------------------------===//
2753 /// @brief This class represents a sign extension of integer types.
2754 class SExtInst : public CastInst {
2756 /// @brief Clone an identical SExtInst
2757 virtual SExtInst *clone_impl() const;
2760 /// @brief Constructor with insert-before-instruction semantics
2762 Value *S, ///< The value to be sign extended
2763 const Type *Ty, ///< The type to sign extend to
2764 const Twine &NameStr = "", ///< A name for the new instruction
2765 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2768 /// @brief Constructor with insert-at-end-of-block semantics
2770 Value *S, ///< The value to be sign extended
2771 const Type *Ty, ///< The type to sign extend to
2772 const Twine &NameStr, ///< A name for the new instruction
2773 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2776 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2777 static inline bool classof(const SExtInst *) { return true; }
2778 static inline bool classof(const Instruction *I) {
2779 return I->getOpcode() == SExt;
2781 static inline bool classof(const Value *V) {
2782 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2786 //===----------------------------------------------------------------------===//
2787 // FPTruncInst Class
2788 //===----------------------------------------------------------------------===//
2790 /// @brief This class represents a truncation of floating point types.
2791 class FPTruncInst : public CastInst {
2793 /// @brief Clone an identical FPTruncInst
2794 virtual FPTruncInst *clone_impl() const;
2797 /// @brief Constructor with insert-before-instruction semantics
2799 Value *S, ///< The value to be truncated
2800 const Type *Ty, ///< The type to truncate to
2801 const Twine &NameStr = "", ///< A name for the new instruction
2802 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2805 /// @brief Constructor with insert-before-instruction semantics
2807 Value *S, ///< The value to be truncated
2808 const Type *Ty, ///< The type to truncate to
2809 const Twine &NameStr, ///< A name for the new instruction
2810 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2813 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2814 static inline bool classof(const FPTruncInst *) { return true; }
2815 static inline bool classof(const Instruction *I) {
2816 return I->getOpcode() == FPTrunc;
2818 static inline bool classof(const Value *V) {
2819 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2823 //===----------------------------------------------------------------------===//
2825 //===----------------------------------------------------------------------===//
2827 /// @brief This class represents an extension of floating point types.
2828 class FPExtInst : public CastInst {
2830 /// @brief Clone an identical FPExtInst
2831 virtual FPExtInst *clone_impl() const;
2834 /// @brief Constructor with insert-before-instruction semantics
2836 Value *S, ///< The value to be extended
2837 const Type *Ty, ///< The type to extend to
2838 const Twine &NameStr = "", ///< A name for the new instruction
2839 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2842 /// @brief Constructor with insert-at-end-of-block semantics
2844 Value *S, ///< The value to be extended
2845 const Type *Ty, ///< The type to extend to
2846 const Twine &NameStr, ///< A name for the new instruction
2847 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2850 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2851 static inline bool classof(const FPExtInst *) { return true; }
2852 static inline bool classof(const Instruction *I) {
2853 return I->getOpcode() == FPExt;
2855 static inline bool classof(const Value *V) {
2856 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2860 //===----------------------------------------------------------------------===//
2862 //===----------------------------------------------------------------------===//
2864 /// @brief This class represents a cast unsigned integer to floating point.
2865 class UIToFPInst : public CastInst {
2867 /// @brief Clone an identical UIToFPInst
2868 virtual UIToFPInst *clone_impl() const;
2871 /// @brief Constructor with insert-before-instruction semantics
2873 Value *S, ///< The value to be converted
2874 const Type *Ty, ///< The type to convert to
2875 const Twine &NameStr = "", ///< A name for the new instruction
2876 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2879 /// @brief Constructor with insert-at-end-of-block semantics
2881 Value *S, ///< The value to be converted
2882 const Type *Ty, ///< The type to convert to
2883 const Twine &NameStr, ///< A name for the new instruction
2884 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2887 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2888 static inline bool classof(const UIToFPInst *) { return true; }
2889 static inline bool classof(const Instruction *I) {
2890 return I->getOpcode() == UIToFP;
2892 static inline bool classof(const Value *V) {
2893 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2897 //===----------------------------------------------------------------------===//
2899 //===----------------------------------------------------------------------===//
2901 /// @brief This class represents a cast from signed integer to floating point.
2902 class SIToFPInst : public CastInst {
2904 /// @brief Clone an identical SIToFPInst
2905 virtual SIToFPInst *clone_impl() const;
2908 /// @brief Constructor with insert-before-instruction semantics
2910 Value *S, ///< The value to be converted
2911 const Type *Ty, ///< The type to convert to
2912 const Twine &NameStr = "", ///< A name for the new instruction
2913 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2916 /// @brief Constructor with insert-at-end-of-block semantics
2918 Value *S, ///< The value to be converted
2919 const Type *Ty, ///< The type to convert to
2920 const Twine &NameStr, ///< A name for the new instruction
2921 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2924 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2925 static inline bool classof(const SIToFPInst *) { return true; }
2926 static inline bool classof(const Instruction *I) {
2927 return I->getOpcode() == SIToFP;
2929 static inline bool classof(const Value *V) {
2930 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2934 //===----------------------------------------------------------------------===//
2936 //===----------------------------------------------------------------------===//
2938 /// @brief This class represents a cast from floating point to unsigned integer
2939 class FPToUIInst : public CastInst {
2941 /// @brief Clone an identical FPToUIInst
2942 virtual FPToUIInst *clone_impl() const;
2945 /// @brief Constructor with insert-before-instruction semantics
2947 Value *S, ///< The value to be converted
2948 const Type *Ty, ///< The type to convert to
2949 const Twine &NameStr = "", ///< A name for the new instruction
2950 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2953 /// @brief Constructor with insert-at-end-of-block semantics
2955 Value *S, ///< The value to be converted
2956 const Type *Ty, ///< The type to convert to
2957 const Twine &NameStr, ///< A name for the new instruction
2958 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2961 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2962 static inline bool classof(const FPToUIInst *) { return true; }
2963 static inline bool classof(const Instruction *I) {
2964 return I->getOpcode() == FPToUI;
2966 static inline bool classof(const Value *V) {
2967 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2971 //===----------------------------------------------------------------------===//
2973 //===----------------------------------------------------------------------===//
2975 /// @brief This class represents a cast from floating point to signed integer.
2976 class FPToSIInst : public CastInst {
2978 /// @brief Clone an identical FPToSIInst
2979 virtual FPToSIInst *clone_impl() const;
2982 /// @brief Constructor with insert-before-instruction semantics
2984 Value *S, ///< The value to be converted
2985 const Type *Ty, ///< The type to convert to
2986 const Twine &NameStr = "", ///< A name for the new instruction
2987 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2990 /// @brief Constructor with insert-at-end-of-block semantics
2992 Value *S, ///< The value to be converted
2993 const Type *Ty, ///< The type to convert to
2994 const Twine &NameStr, ///< A name for the new instruction
2995 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2998 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2999 static inline bool classof(const FPToSIInst *) { return true; }
3000 static inline bool classof(const Instruction *I) {
3001 return I->getOpcode() == FPToSI;
3003 static inline bool classof(const Value *V) {
3004 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3008 //===----------------------------------------------------------------------===//
3009 // IntToPtrInst Class
3010 //===----------------------------------------------------------------------===//
3012 /// @brief This class represents a cast from an integer to a pointer.
3013 class IntToPtrInst : public CastInst {
3015 /// @brief Constructor with insert-before-instruction semantics
3017 Value *S, ///< The value to be converted
3018 const Type *Ty, ///< The type to convert to
3019 const Twine &NameStr = "", ///< A name for the new instruction
3020 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3023 /// @brief Constructor with insert-at-end-of-block semantics
3025 Value *S, ///< The value to be converted
3026 const Type *Ty, ///< The type to convert to
3027 const Twine &NameStr, ///< A name for the new instruction
3028 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3031 /// @brief Clone an identical IntToPtrInst
3032 virtual IntToPtrInst *clone_impl() const;
3034 // Methods for support type inquiry through isa, cast, and dyn_cast:
3035 static inline bool classof(const IntToPtrInst *) { return true; }
3036 static inline bool classof(const Instruction *I) {
3037 return I->getOpcode() == IntToPtr;
3039 static inline bool classof(const Value *V) {
3040 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3044 //===----------------------------------------------------------------------===//
3045 // PtrToIntInst Class
3046 //===----------------------------------------------------------------------===//
3048 /// @brief This class represents a cast from a pointer to an integer
3049 class PtrToIntInst : public CastInst {
3051 /// @brief Clone an identical PtrToIntInst
3052 virtual PtrToIntInst *clone_impl() const;
3055 /// @brief Constructor with insert-before-instruction semantics
3057 Value *S, ///< The value to be converted
3058 const Type *Ty, ///< The type to convert to
3059 const Twine &NameStr = "", ///< A name for the new instruction
3060 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3063 /// @brief Constructor with insert-at-end-of-block semantics
3065 Value *S, ///< The value to be converted
3066 const Type *Ty, ///< The type to convert to
3067 const Twine &NameStr, ///< A name for the new instruction
3068 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3071 // Methods for support type inquiry through isa, cast, and dyn_cast:
3072 static inline bool classof(const PtrToIntInst *) { return true; }
3073 static inline bool classof(const Instruction *I) {
3074 return I->getOpcode() == PtrToInt;
3076 static inline bool classof(const Value *V) {
3077 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3081 //===----------------------------------------------------------------------===//
3082 // BitCastInst Class
3083 //===----------------------------------------------------------------------===//
3085 /// @brief This class represents a no-op cast from one type to another.
3086 class BitCastInst : public CastInst {
3088 /// @brief Clone an identical BitCastInst
3089 virtual BitCastInst *clone_impl() const;
3092 /// @brief Constructor with insert-before-instruction semantics
3094 Value *S, ///< The value to be casted
3095 const Type *Ty, ///< The type to casted to
3096 const Twine &NameStr = "", ///< A name for the new instruction
3097 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3100 /// @brief Constructor with insert-at-end-of-block semantics
3102 Value *S, ///< The value to be casted
3103 const Type *Ty, ///< The type to casted to
3104 const Twine &NameStr, ///< A name for the new instruction
3105 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3108 // Methods for support type inquiry through isa, cast, and dyn_cast:
3109 static inline bool classof(const BitCastInst *) { return true; }
3110 static inline bool classof(const Instruction *I) {
3111 return I->getOpcode() == BitCast;
3113 static inline bool classof(const Value *V) {
3114 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3118 } // End llvm namespace