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 Return true if the call should not be inlined.
976 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
977 void setIsNoInline(bool Value) {
978 if (Value) addAttribute(~0, Attribute::NoInline);
979 else removeAttribute(~0, Attribute::NoInline);
982 /// @brief Determine if the call does not access memory.
983 bool doesNotAccessMemory() const {
984 return paramHasAttr(~0, Attribute::ReadNone);
986 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
987 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
988 else removeAttribute(~0, Attribute::ReadNone);
991 /// @brief Determine if the call does not access or only reads memory.
992 bool onlyReadsMemory() const {
993 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
995 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
996 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
997 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1000 /// @brief Determine if the call cannot return.
1001 bool doesNotReturn() const {
1002 return paramHasAttr(~0, Attribute::NoReturn);
1004 void setDoesNotReturn(bool DoesNotReturn = true) {
1005 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1006 else removeAttribute(~0, Attribute::NoReturn);
1009 /// @brief Determine if the call cannot unwind.
1010 bool doesNotThrow() const {
1011 return paramHasAttr(~0, Attribute::NoUnwind);
1013 void setDoesNotThrow(bool DoesNotThrow = true) {
1014 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1015 else removeAttribute(~0, Attribute::NoUnwind);
1018 /// @brief Determine if the call returns a structure through first
1019 /// pointer argument.
1020 bool hasStructRetAttr() const {
1021 // Be friendly and also check the callee.
1022 return paramHasAttr(1, Attribute::StructRet);
1025 /// @brief Determine if any call argument is an aggregate passed by value.
1026 bool hasByValArgument() const {
1027 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1030 /// getCalledFunction - Return the function called, or null if this is an
1031 /// indirect function invocation.
1033 Function *getCalledFunction() const {
1034 return dyn_cast<Function>(Op<0>());
1037 /// getCalledValue - Get a pointer to the function that is invoked by this
1039 const Value *getCalledValue() const { return Op<0>(); }
1040 Value *getCalledValue() { return Op<0>(); }
1042 /// setCalledFunction - Set the function called.
1043 void setCalledFunction(Value* Fn) {
1047 // Methods for support type inquiry through isa, cast, and dyn_cast:
1048 static inline bool classof(const CallInst *) { return true; }
1049 static inline bool classof(const Instruction *I) {
1050 return I->getOpcode() == Instruction::Call;
1052 static inline bool classof(const Value *V) {
1053 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1056 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1057 // method so that subclasses cannot accidentally use it.
1058 void setInstructionSubclassData(unsigned short D) {
1059 Instruction::setInstructionSubclassData(D);
1064 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1067 template<typename InputIterator>
1068 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1069 const Twine &NameStr, BasicBlock *InsertAtEnd)
1070 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1071 ->getElementType())->getReturnType(),
1073 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1074 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1075 init(Func, ArgBegin, ArgEnd, NameStr,
1076 typename std::iterator_traits<InputIterator>::iterator_category());
1079 template<typename InputIterator>
1080 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1081 const Twine &NameStr, Instruction *InsertBefore)
1082 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1083 ->getElementType())->getReturnType(),
1085 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1086 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1087 init(Func, ArgBegin, ArgEnd, NameStr,
1088 typename std::iterator_traits<InputIterator>::iterator_category());
1091 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1093 //===----------------------------------------------------------------------===//
1095 //===----------------------------------------------------------------------===//
1097 /// SelectInst - This class represents the LLVM 'select' instruction.
1099 class SelectInst : public Instruction {
1100 void init(Value *C, Value *S1, Value *S2) {
1101 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1107 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1108 Instruction *InsertBefore)
1109 : Instruction(S1->getType(), Instruction::Select,
1110 &Op<0>(), 3, InsertBefore) {
1114 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1115 BasicBlock *InsertAtEnd)
1116 : Instruction(S1->getType(), Instruction::Select,
1117 &Op<0>(), 3, InsertAtEnd) {
1122 virtual SelectInst *clone_impl() const;
1124 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1125 const Twine &NameStr = "",
1126 Instruction *InsertBefore = 0) {
1127 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1129 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1130 const Twine &NameStr,
1131 BasicBlock *InsertAtEnd) {
1132 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1135 const Value *getCondition() const { return Op<0>(); }
1136 const Value *getTrueValue() const { return Op<1>(); }
1137 const Value *getFalseValue() const { return Op<2>(); }
1138 Value *getCondition() { return Op<0>(); }
1139 Value *getTrueValue() { return Op<1>(); }
1140 Value *getFalseValue() { return Op<2>(); }
1142 /// areInvalidOperands - Return a string if the specified operands are invalid
1143 /// for a select operation, otherwise return null.
1144 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1146 /// Transparently provide more efficient getOperand methods.
1147 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1149 OtherOps getOpcode() const {
1150 return static_cast<OtherOps>(Instruction::getOpcode());
1153 // Methods for support type inquiry through isa, cast, and dyn_cast:
1154 static inline bool classof(const SelectInst *) { return true; }
1155 static inline bool classof(const Instruction *I) {
1156 return I->getOpcode() == Instruction::Select;
1158 static inline bool classof(const Value *V) {
1159 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1164 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1167 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1169 //===----------------------------------------------------------------------===//
1171 //===----------------------------------------------------------------------===//
1173 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1174 /// an argument of the specified type given a va_list and increments that list
1176 class VAArgInst : public UnaryInstruction {
1178 virtual VAArgInst *clone_impl() const;
1181 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1182 Instruction *InsertBefore = 0)
1183 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1186 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1187 BasicBlock *InsertAtEnd)
1188 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1192 // Methods for support type inquiry through isa, cast, and dyn_cast:
1193 static inline bool classof(const VAArgInst *) { return true; }
1194 static inline bool classof(const Instruction *I) {
1195 return I->getOpcode() == VAArg;
1197 static inline bool classof(const Value *V) {
1198 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1202 //===----------------------------------------------------------------------===//
1203 // ExtractElementInst Class
1204 //===----------------------------------------------------------------------===//
1206 /// ExtractElementInst - This instruction extracts a single (scalar)
1207 /// element from a VectorType value
1209 class ExtractElementInst : public Instruction {
1210 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1211 Instruction *InsertBefore = 0);
1212 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1213 BasicBlock *InsertAtEnd);
1215 virtual ExtractElementInst *clone_impl() const;
1218 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1219 const Twine &NameStr = "",
1220 Instruction *InsertBefore = 0) {
1221 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1223 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1224 const Twine &NameStr,
1225 BasicBlock *InsertAtEnd) {
1226 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1229 /// isValidOperands - Return true if an extractelement instruction can be
1230 /// formed with the specified operands.
1231 static bool isValidOperands(const Value *Vec, const Value *Idx);
1233 Value *getVectorOperand() { return Op<0>(); }
1234 Value *getIndexOperand() { return Op<1>(); }
1235 const Value *getVectorOperand() const { return Op<0>(); }
1236 const Value *getIndexOperand() const { return Op<1>(); }
1238 const VectorType *getVectorOperandType() const {
1239 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1243 /// Transparently provide more efficient getOperand methods.
1244 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1246 // Methods for support type inquiry through isa, cast, and dyn_cast:
1247 static inline bool classof(const ExtractElementInst *) { return true; }
1248 static inline bool classof(const Instruction *I) {
1249 return I->getOpcode() == Instruction::ExtractElement;
1251 static inline bool classof(const Value *V) {
1252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1257 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1260 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1262 //===----------------------------------------------------------------------===//
1263 // InsertElementInst Class
1264 //===----------------------------------------------------------------------===//
1266 /// InsertElementInst - This instruction inserts a single (scalar)
1267 /// element into a VectorType value
1269 class InsertElementInst : public Instruction {
1270 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1271 const Twine &NameStr = "",
1272 Instruction *InsertBefore = 0);
1273 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1274 const Twine &NameStr, BasicBlock *InsertAtEnd);
1276 virtual InsertElementInst *clone_impl() const;
1279 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1280 const Twine &NameStr = "",
1281 Instruction *InsertBefore = 0) {
1282 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1284 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1285 const Twine &NameStr,
1286 BasicBlock *InsertAtEnd) {
1287 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1290 /// isValidOperands - Return true if an insertelement instruction can be
1291 /// formed with the specified operands.
1292 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1295 /// getType - Overload to return most specific vector type.
1297 const VectorType *getType() const {
1298 return reinterpret_cast<const VectorType*>(Instruction::getType());
1301 /// Transparently provide more efficient getOperand methods.
1302 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1304 // Methods for support type inquiry through isa, cast, and dyn_cast:
1305 static inline bool classof(const InsertElementInst *) { return true; }
1306 static inline bool classof(const Instruction *I) {
1307 return I->getOpcode() == Instruction::InsertElement;
1309 static inline bool classof(const Value *V) {
1310 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1315 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1318 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1320 //===----------------------------------------------------------------------===//
1321 // ShuffleVectorInst Class
1322 //===----------------------------------------------------------------------===//
1324 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1327 class ShuffleVectorInst : public Instruction {
1329 virtual ShuffleVectorInst *clone_impl() const;
1332 // allocate space for exactly three operands
1333 void *operator new(size_t s) {
1334 return User::operator new(s, 3);
1336 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1337 const Twine &NameStr = "",
1338 Instruction *InsertBefor = 0);
1339 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1340 const Twine &NameStr, BasicBlock *InsertAtEnd);
1342 /// isValidOperands - Return true if a shufflevector instruction can be
1343 /// formed with the specified operands.
1344 static bool isValidOperands(const Value *V1, const Value *V2,
1347 /// getType - Overload to return most specific vector type.
1349 const VectorType *getType() const {
1350 return reinterpret_cast<const VectorType*>(Instruction::getType());
1353 /// Transparently provide more efficient getOperand methods.
1354 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1356 /// getMaskValue - Return the index from the shuffle mask for the specified
1357 /// output result. This is either -1 if the element is undef or a number less
1358 /// than 2*numelements.
1359 int getMaskValue(unsigned i) const;
1361 // Methods for support type inquiry through isa, cast, and dyn_cast:
1362 static inline bool classof(const ShuffleVectorInst *) { return true; }
1363 static inline bool classof(const Instruction *I) {
1364 return I->getOpcode() == Instruction::ShuffleVector;
1366 static inline bool classof(const Value *V) {
1367 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1372 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1375 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1377 //===----------------------------------------------------------------------===//
1378 // ExtractValueInst Class
1379 //===----------------------------------------------------------------------===//
1381 /// ExtractValueInst - This instruction extracts a struct member or array
1382 /// element value from an aggregate value.
1384 class ExtractValueInst : public UnaryInstruction {
1385 SmallVector<unsigned, 4> Indices;
1387 ExtractValueInst(const ExtractValueInst &EVI);
1388 void init(const unsigned *Idx, unsigned NumIdx,
1389 const Twine &NameStr);
1390 void init(unsigned Idx, const Twine &NameStr);
1392 template<typename InputIterator>
1393 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1394 const Twine &NameStr,
1395 // This argument ensures that we have an iterator we can
1396 // do arithmetic on in constant time
1397 std::random_access_iterator_tag) {
1398 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1400 // There's no fundamental reason why we require at least one index
1401 // (other than weirdness with &*IdxBegin being invalid; see
1402 // getelementptr's init routine for example). But there's no
1403 // present need to support it.
1404 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1406 // This requires that the iterator points to contiguous memory.
1407 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1408 // we have to build an array here
1411 /// getIndexedType - Returns the type of the element that would be extracted
1412 /// with an extractvalue instruction with the specified parameters.
1414 /// Null is returned if the indices are invalid for the specified
1417 static const Type *getIndexedType(const Type *Agg,
1418 const unsigned *Idx, unsigned NumIdx);
1420 template<typename InputIterator>
1421 static const Type *getIndexedType(const Type *Ptr,
1422 InputIterator IdxBegin,
1423 InputIterator IdxEnd,
1424 // This argument ensures that we
1425 // have an iterator we can do
1426 // arithmetic on in constant time
1427 std::random_access_iterator_tag) {
1428 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1431 // This requires that the iterator points to contiguous memory.
1432 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1434 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1437 /// Constructors - Create a extractvalue instruction with a base aggregate
1438 /// value and a list of indices. The first ctor can optionally insert before
1439 /// an existing instruction, the second appends the new instruction to the
1440 /// specified BasicBlock.
1441 template<typename InputIterator>
1442 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1443 InputIterator IdxEnd,
1444 const Twine &NameStr,
1445 Instruction *InsertBefore);
1446 template<typename InputIterator>
1447 inline ExtractValueInst(Value *Agg,
1448 InputIterator IdxBegin, InputIterator IdxEnd,
1449 const Twine &NameStr, BasicBlock *InsertAtEnd);
1451 // allocate space for exactly one operand
1452 void *operator new(size_t s) {
1453 return User::operator new(s, 1);
1456 virtual ExtractValueInst *clone_impl() const;
1459 template<typename InputIterator>
1460 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1461 InputIterator IdxEnd,
1462 const Twine &NameStr = "",
1463 Instruction *InsertBefore = 0) {
1465 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1467 template<typename InputIterator>
1468 static ExtractValueInst *Create(Value *Agg,
1469 InputIterator IdxBegin, InputIterator IdxEnd,
1470 const Twine &NameStr,
1471 BasicBlock *InsertAtEnd) {
1472 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1475 /// Constructors - These two creators are convenience methods because one
1476 /// index extractvalue instructions are much more common than those with
1478 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1479 const Twine &NameStr = "",
1480 Instruction *InsertBefore = 0) {
1481 unsigned Idxs[1] = { Idx };
1482 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1484 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1485 const Twine &NameStr,
1486 BasicBlock *InsertAtEnd) {
1487 unsigned Idxs[1] = { Idx };
1488 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1491 /// getIndexedType - Returns the type of the element that would be extracted
1492 /// with an extractvalue instruction with the specified parameters.
1494 /// Null is returned if the indices are invalid for the specified
1497 template<typename InputIterator>
1498 static const Type *getIndexedType(const Type *Ptr,
1499 InputIterator IdxBegin,
1500 InputIterator IdxEnd) {
1501 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1502 typename std::iterator_traits<InputIterator>::
1503 iterator_category());
1505 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1507 typedef const unsigned* idx_iterator;
1508 inline idx_iterator idx_begin() const { return Indices.begin(); }
1509 inline idx_iterator idx_end() const { return Indices.end(); }
1511 Value *getAggregateOperand() {
1512 return getOperand(0);
1514 const Value *getAggregateOperand() const {
1515 return getOperand(0);
1517 static unsigned getAggregateOperandIndex() {
1518 return 0U; // get index for modifying correct operand
1521 unsigned getNumIndices() const { // Note: always non-negative
1522 return (unsigned)Indices.size();
1525 bool hasIndices() const {
1529 // Methods for support type inquiry through isa, cast, and dyn_cast:
1530 static inline bool classof(const ExtractValueInst *) { return true; }
1531 static inline bool classof(const Instruction *I) {
1532 return I->getOpcode() == Instruction::ExtractValue;
1534 static inline bool classof(const Value *V) {
1535 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1539 template<typename InputIterator>
1540 ExtractValueInst::ExtractValueInst(Value *Agg,
1541 InputIterator IdxBegin,
1542 InputIterator IdxEnd,
1543 const Twine &NameStr,
1544 Instruction *InsertBefore)
1545 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1547 ExtractValue, Agg, InsertBefore) {
1548 init(IdxBegin, IdxEnd, NameStr,
1549 typename std::iterator_traits<InputIterator>::iterator_category());
1551 template<typename InputIterator>
1552 ExtractValueInst::ExtractValueInst(Value *Agg,
1553 InputIterator IdxBegin,
1554 InputIterator IdxEnd,
1555 const Twine &NameStr,
1556 BasicBlock *InsertAtEnd)
1557 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1559 ExtractValue, Agg, InsertAtEnd) {
1560 init(IdxBegin, IdxEnd, NameStr,
1561 typename std::iterator_traits<InputIterator>::iterator_category());
1565 //===----------------------------------------------------------------------===//
1566 // InsertValueInst Class
1567 //===----------------------------------------------------------------------===//
1569 /// InsertValueInst - This instruction inserts a struct field of array element
1570 /// value into an aggregate value.
1572 class InsertValueInst : public Instruction {
1573 SmallVector<unsigned, 4> Indices;
1575 void *operator new(size_t, unsigned); // Do not implement
1576 InsertValueInst(const InsertValueInst &IVI);
1577 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1578 const Twine &NameStr);
1579 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1581 template<typename InputIterator>
1582 void init(Value *Agg, Value *Val,
1583 InputIterator IdxBegin, InputIterator IdxEnd,
1584 const Twine &NameStr,
1585 // This argument ensures that we have an iterator we can
1586 // do arithmetic on in constant time
1587 std::random_access_iterator_tag) {
1588 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1590 // There's no fundamental reason why we require at least one index
1591 // (other than weirdness with &*IdxBegin being invalid; see
1592 // getelementptr's init routine for example). But there's no
1593 // present need to support it.
1594 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1596 // This requires that the iterator points to contiguous memory.
1597 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1598 // we have to build an array here
1601 /// Constructors - Create a insertvalue instruction with a base aggregate
1602 /// value, a value to insert, and a list of indices. The first ctor can
1603 /// optionally insert before an existing instruction, the second appends
1604 /// the new instruction to the specified BasicBlock.
1605 template<typename InputIterator>
1606 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1607 InputIterator IdxEnd,
1608 const Twine &NameStr,
1609 Instruction *InsertBefore);
1610 template<typename InputIterator>
1611 inline InsertValueInst(Value *Agg, Value *Val,
1612 InputIterator IdxBegin, InputIterator IdxEnd,
1613 const Twine &NameStr, BasicBlock *InsertAtEnd);
1615 /// Constructors - These two constructors are convenience methods because one
1616 /// and two index insertvalue instructions are so common.
1617 InsertValueInst(Value *Agg, Value *Val,
1618 unsigned Idx, const Twine &NameStr = "",
1619 Instruction *InsertBefore = 0);
1620 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1621 const Twine &NameStr, BasicBlock *InsertAtEnd);
1623 virtual InsertValueInst *clone_impl() const;
1625 // allocate space for exactly two operands
1626 void *operator new(size_t s) {
1627 return User::operator new(s, 2);
1630 template<typename InputIterator>
1631 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1632 InputIterator IdxEnd,
1633 const Twine &NameStr = "",
1634 Instruction *InsertBefore = 0) {
1635 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1636 NameStr, InsertBefore);
1638 template<typename InputIterator>
1639 static InsertValueInst *Create(Value *Agg, Value *Val,
1640 InputIterator IdxBegin, InputIterator IdxEnd,
1641 const Twine &NameStr,
1642 BasicBlock *InsertAtEnd) {
1643 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1644 NameStr, InsertAtEnd);
1647 /// Constructors - These two creators are convenience methods because one
1648 /// index insertvalue instructions are much more common than those with
1650 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1651 const Twine &NameStr = "",
1652 Instruction *InsertBefore = 0) {
1653 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1655 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1656 const Twine &NameStr,
1657 BasicBlock *InsertAtEnd) {
1658 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1661 /// Transparently provide more efficient getOperand methods.
1662 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1664 typedef const unsigned* idx_iterator;
1665 inline idx_iterator idx_begin() const { return Indices.begin(); }
1666 inline idx_iterator idx_end() const { return Indices.end(); }
1668 Value *getAggregateOperand() {
1669 return getOperand(0);
1671 const Value *getAggregateOperand() const {
1672 return getOperand(0);
1674 static unsigned getAggregateOperandIndex() {
1675 return 0U; // get index for modifying correct operand
1678 Value *getInsertedValueOperand() {
1679 return getOperand(1);
1681 const Value *getInsertedValueOperand() const {
1682 return getOperand(1);
1684 static unsigned getInsertedValueOperandIndex() {
1685 return 1U; // get index for modifying correct operand
1688 unsigned getNumIndices() const { // Note: always non-negative
1689 return (unsigned)Indices.size();
1692 bool hasIndices() const {
1696 // Methods for support type inquiry through isa, cast, and dyn_cast:
1697 static inline bool classof(const InsertValueInst *) { return true; }
1698 static inline bool classof(const Instruction *I) {
1699 return I->getOpcode() == Instruction::InsertValue;
1701 static inline bool classof(const Value *V) {
1702 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1707 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1710 template<typename InputIterator>
1711 InsertValueInst::InsertValueInst(Value *Agg,
1713 InputIterator IdxBegin,
1714 InputIterator IdxEnd,
1715 const Twine &NameStr,
1716 Instruction *InsertBefore)
1717 : Instruction(Agg->getType(), InsertValue,
1718 OperandTraits<InsertValueInst>::op_begin(this),
1720 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1721 typename std::iterator_traits<InputIterator>::iterator_category());
1723 template<typename InputIterator>
1724 InsertValueInst::InsertValueInst(Value *Agg,
1726 InputIterator IdxBegin,
1727 InputIterator IdxEnd,
1728 const Twine &NameStr,
1729 BasicBlock *InsertAtEnd)
1730 : Instruction(Agg->getType(), InsertValue,
1731 OperandTraits<InsertValueInst>::op_begin(this),
1733 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1734 typename std::iterator_traits<InputIterator>::iterator_category());
1737 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1739 //===----------------------------------------------------------------------===//
1741 //===----------------------------------------------------------------------===//
1743 // PHINode - The PHINode class is used to represent the magical mystical PHI
1744 // node, that can not exist in nature, but can be synthesized in a computer
1745 // scientist's overactive imagination.
1747 class PHINode : public Instruction {
1748 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1749 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1750 /// the number actually in use.
1751 unsigned ReservedSpace;
1752 PHINode(const PHINode &PN);
1753 // allocate space for exactly zero operands
1754 void *operator new(size_t s) {
1755 return User::operator new(s, 0);
1757 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1758 Instruction *InsertBefore = 0)
1759 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1764 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1765 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1770 virtual PHINode *clone_impl() const;
1772 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1773 Instruction *InsertBefore = 0) {
1774 return new PHINode(Ty, NameStr, InsertBefore);
1776 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1777 BasicBlock *InsertAtEnd) {
1778 return new PHINode(Ty, NameStr, InsertAtEnd);
1782 /// reserveOperandSpace - This method can be used to avoid repeated
1783 /// reallocation of PHI operand lists by reserving space for the correct
1784 /// number of operands before adding them. Unlike normal vector reserves,
1785 /// this method can also be used to trim the operand space.
1786 void reserveOperandSpace(unsigned NumValues) {
1787 resizeOperands(NumValues*2);
1790 /// Provide fast operand accessors
1791 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1793 /// getNumIncomingValues - Return the number of incoming edges
1795 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1797 /// getIncomingValue - Return incoming value number x
1799 Value *getIncomingValue(unsigned i) const {
1800 assert(i*2 < getNumOperands() && "Invalid value number!");
1801 return getOperand(i*2);
1803 void setIncomingValue(unsigned i, Value *V) {
1804 assert(i*2 < getNumOperands() && "Invalid value number!");
1807 static unsigned getOperandNumForIncomingValue(unsigned i) {
1810 static unsigned getIncomingValueNumForOperand(unsigned i) {
1811 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1815 /// getIncomingBlock - Return incoming basic block number @p i.
1817 BasicBlock *getIncomingBlock(unsigned i) const {
1818 return cast<BasicBlock>(getOperand(i*2+1));
1821 /// getIncomingBlock - Return incoming basic block corresponding
1822 /// to an operand of the PHI.
1824 BasicBlock *getIncomingBlock(const Use &U) const {
1825 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1826 return cast<BasicBlock>((&U + 1)->get());
1829 /// getIncomingBlock - Return incoming basic block corresponding
1830 /// to value use iterator.
1832 template <typename U>
1833 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1834 return getIncomingBlock(I.getUse());
1838 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1839 setOperand(i*2+1, (Value*)BB);
1841 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1844 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1845 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1849 /// addIncoming - Add an incoming value to the end of the PHI list
1851 void addIncoming(Value *V, BasicBlock *BB) {
1852 assert(V && "PHI node got a null value!");
1853 assert(BB && "PHI node got a null basic block!");
1854 assert(getType() == V->getType() &&
1855 "All operands to PHI node must be the same type as the PHI node!");
1856 unsigned OpNo = NumOperands;
1857 if (OpNo+2 > ReservedSpace)
1858 resizeOperands(0); // Get more space!
1859 // Initialize some new operands.
1860 NumOperands = OpNo+2;
1861 OperandList[OpNo] = V;
1862 OperandList[OpNo+1] = (Value*)BB;
1865 /// removeIncomingValue - Remove an incoming value. This is useful if a
1866 /// predecessor basic block is deleted. The value removed is returned.
1868 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1869 /// is true), the PHI node is destroyed and any uses of it are replaced with
1870 /// dummy values. The only time there should be zero incoming values to a PHI
1871 /// node is when the block is dead, so this strategy is sound.
1873 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1875 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1876 int Idx = getBasicBlockIndex(BB);
1877 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1878 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1881 /// getBasicBlockIndex - Return the first index of the specified basic
1882 /// block in the value list for this PHI. Returns -1 if no instance.
1884 int getBasicBlockIndex(const BasicBlock *BB) const {
1885 Use *OL = OperandList;
1886 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1887 if (OL[i+1].get() == (const Value*)BB) return i/2;
1891 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1892 return getIncomingValue(getBasicBlockIndex(BB));
1895 /// hasConstantValue - If the specified PHI node always merges together the
1896 /// same value, return the value, otherwise return null.
1898 /// If the PHI has undef operands, but all the rest of the operands are
1899 /// some unique value, return that value if it can be proved that the
1900 /// value dominates the PHI. If DT is null, use a conservative check,
1901 /// otherwise use DT to test for dominance.
1903 Value *hasConstantValue(DominatorTree *DT = 0) const;
1905 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1906 static inline bool classof(const PHINode *) { return true; }
1907 static inline bool classof(const Instruction *I) {
1908 return I->getOpcode() == Instruction::PHI;
1910 static inline bool classof(const Value *V) {
1911 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1914 void resizeOperands(unsigned NumOperands);
1918 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1921 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1924 //===----------------------------------------------------------------------===//
1926 //===----------------------------------------------------------------------===//
1928 //===---------------------------------------------------------------------------
1929 /// ReturnInst - Return a value (possibly void), from a function. Execution
1930 /// does not continue in this function any longer.
1932 class ReturnInst : public TerminatorInst {
1933 ReturnInst(const ReturnInst &RI);
1936 // ReturnInst constructors:
1937 // ReturnInst() - 'ret void' instruction
1938 // ReturnInst( null) - 'ret void' instruction
1939 // ReturnInst(Value* X) - 'ret X' instruction
1940 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1941 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1942 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1943 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1945 // NOTE: If the Value* passed is of type void then the constructor behaves as
1946 // if it was passed NULL.
1947 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1948 Instruction *InsertBefore = 0);
1949 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1950 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1952 virtual ReturnInst *clone_impl() const;
1954 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1955 Instruction *InsertBefore = 0) {
1956 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1958 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1959 BasicBlock *InsertAtEnd) {
1960 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1962 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1963 return new(0) ReturnInst(C, InsertAtEnd);
1965 virtual ~ReturnInst();
1967 /// Provide fast operand accessors
1968 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1970 /// Convenience accessor
1971 Value *getReturnValue(unsigned n = 0) const {
1972 return n < getNumOperands()
1977 unsigned getNumSuccessors() const { return 0; }
1979 // Methods for support type inquiry through isa, cast, and dyn_cast:
1980 static inline bool classof(const ReturnInst *) { return true; }
1981 static inline bool classof(const Instruction *I) {
1982 return (I->getOpcode() == Instruction::Ret);
1984 static inline bool classof(const Value *V) {
1985 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1988 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1989 virtual unsigned getNumSuccessorsV() const;
1990 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1994 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<> {
1997 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1999 //===----------------------------------------------------------------------===//
2001 //===----------------------------------------------------------------------===//
2003 //===---------------------------------------------------------------------------
2004 /// BranchInst - Conditional or Unconditional Branch instruction.
2006 class BranchInst : public TerminatorInst {
2007 /// Ops list - Branches are strange. The operands are ordered:
2008 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2009 /// they don't have to check for cond/uncond branchness. These are mostly
2010 /// accessed relative from op_end().
2011 BranchInst(const BranchInst &BI);
2013 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2014 // BranchInst(BB *B) - 'br B'
2015 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2016 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2017 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2018 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2019 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2020 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2021 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2022 Instruction *InsertBefore = 0);
2023 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2024 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2025 BasicBlock *InsertAtEnd);
2027 virtual BranchInst *clone_impl() const;
2029 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2030 return new(1, true) BranchInst(IfTrue, InsertBefore);
2032 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2033 Value *Cond, Instruction *InsertBefore = 0) {
2034 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2036 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2037 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2039 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2040 Value *Cond, BasicBlock *InsertAtEnd) {
2041 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2046 /// Transparently provide more efficient getOperand methods.
2047 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2049 bool isUnconditional() const { return getNumOperands() == 1; }
2050 bool isConditional() const { return getNumOperands() == 3; }
2052 Value *getCondition() const {
2053 assert(isConditional() && "Cannot get condition of an uncond branch!");
2057 void setCondition(Value *V) {
2058 assert(isConditional() && "Cannot set condition of unconditional branch!");
2062 // setUnconditionalDest - Change the current branch to an unconditional branch
2063 // targeting the specified block.
2064 // FIXME: Eliminate this ugly method.
2065 void setUnconditionalDest(BasicBlock *Dest) {
2066 Op<-1>() = (Value*)Dest;
2067 if (isConditional()) { // Convert this to an uncond branch.
2071 OperandList = op_begin();
2075 unsigned getNumSuccessors() const { return 1+isConditional(); }
2077 BasicBlock *getSuccessor(unsigned i) const {
2078 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2079 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2082 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2083 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2084 *(&Op<-1>() - idx) = (Value*)NewSucc;
2087 // Methods for support type inquiry through isa, cast, and dyn_cast:
2088 static inline bool classof(const BranchInst *) { return true; }
2089 static inline bool classof(const Instruction *I) {
2090 return (I->getOpcode() == Instruction::Br);
2092 static inline bool classof(const Value *V) {
2093 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2096 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2097 virtual unsigned getNumSuccessorsV() const;
2098 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2102 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2104 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2106 //===----------------------------------------------------------------------===//
2108 //===----------------------------------------------------------------------===//
2110 //===---------------------------------------------------------------------------
2111 /// SwitchInst - Multiway switch
2113 class SwitchInst : public TerminatorInst {
2114 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2115 unsigned ReservedSpace;
2116 // Operand[0] = Value to switch on
2117 // Operand[1] = Default basic block destination
2118 // Operand[2n ] = Value to match
2119 // Operand[2n+1] = BasicBlock to go to on match
2120 SwitchInst(const SwitchInst &SI);
2121 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2122 void resizeOperands(unsigned No);
2123 // allocate space for exactly zero operands
2124 void *operator new(size_t s) {
2125 return User::operator new(s, 0);
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 can also autoinsert before another instruction.
2131 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2132 Instruction *InsertBefore);
2134 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2135 /// switch on and a default destination. The number of additional cases can
2136 /// be specified here to make memory allocation more efficient. This
2137 /// constructor also autoinserts at the end of the specified BasicBlock.
2138 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2139 BasicBlock *InsertAtEnd);
2141 virtual SwitchInst *clone_impl() const;
2143 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2144 unsigned NumCases, Instruction *InsertBefore = 0) {
2145 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2147 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2148 unsigned NumCases, BasicBlock *InsertAtEnd) {
2149 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2153 /// Provide fast operand accessors
2154 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2156 // Accessor Methods for Switch stmt
2157 Value *getCondition() const { return getOperand(0); }
2158 void setCondition(Value *V) { setOperand(0, V); }
2160 BasicBlock *getDefaultDest() const {
2161 return cast<BasicBlock>(getOperand(1));
2164 /// getNumCases - return the number of 'cases' in this switch instruction.
2165 /// Note that case #0 is always the default case.
2166 unsigned getNumCases() const {
2167 return getNumOperands()/2;
2170 /// getCaseValue - Return the specified case value. Note that case #0, the
2171 /// default destination, does not have a case value.
2172 ConstantInt *getCaseValue(unsigned i) {
2173 assert(i && i < getNumCases() && "Illegal case value to get!");
2174 return getSuccessorValue(i);
2177 /// getCaseValue - Return the specified case value. Note that case #0, the
2178 /// default destination, does not have a case value.
2179 const ConstantInt *getCaseValue(unsigned i) const {
2180 assert(i && i < getNumCases() && "Illegal case value to get!");
2181 return getSuccessorValue(i);
2184 /// findCaseValue - Search all of the case values for the specified constant.
2185 /// If it is explicitly handled, return the case number of it, otherwise
2186 /// return 0 to indicate that it is handled by the default handler.
2187 unsigned findCaseValue(const ConstantInt *C) const {
2188 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2189 if (getCaseValue(i) == C)
2194 /// findCaseDest - Finds the unique case value for a given successor. Returns
2195 /// null if the successor is not found, not unique, or is the default case.
2196 ConstantInt *findCaseDest(BasicBlock *BB) {
2197 if (BB == getDefaultDest()) return NULL;
2199 ConstantInt *CI = NULL;
2200 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2201 if (getSuccessor(i) == BB) {
2202 if (CI) return NULL; // Multiple cases lead to BB.
2203 else CI = getCaseValue(i);
2209 /// addCase - Add an entry to the switch instruction...
2211 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2213 /// removeCase - This method removes the specified successor from the switch
2214 /// instruction. Note that this cannot be used to remove the default
2215 /// destination (successor #0).
2217 void removeCase(unsigned idx);
2219 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2220 BasicBlock *getSuccessor(unsigned idx) const {
2221 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2222 return cast<BasicBlock>(getOperand(idx*2+1));
2224 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2225 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2226 setOperand(idx*2+1, (Value*)NewSucc);
2229 // getSuccessorValue - Return the value associated with the specified
2231 ConstantInt *getSuccessorValue(unsigned idx) const {
2232 assert(idx < getNumSuccessors() && "Successor # out of range!");
2233 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2236 // Methods for support type inquiry through isa, cast, and dyn_cast:
2237 static inline bool classof(const SwitchInst *) { return true; }
2238 static inline bool classof(const Instruction *I) {
2239 return I->getOpcode() == Instruction::Switch;
2241 static inline bool classof(const Value *V) {
2242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2245 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2246 virtual unsigned getNumSuccessorsV() const;
2247 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2251 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2254 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2257 //===----------------------------------------------------------------------===//
2258 // IndirectBrInst Class
2259 //===----------------------------------------------------------------------===//
2261 //===---------------------------------------------------------------------------
2262 /// IndirectBrInst - Indirect Branch Instruction.
2264 class IndirectBrInst : public TerminatorInst {
2265 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2266 unsigned ReservedSpace;
2267 // Operand[0] = Value to switch on
2268 // Operand[1] = Default basic block destination
2269 // Operand[2n ] = Value to match
2270 // Operand[2n+1] = BasicBlock to go to on match
2271 IndirectBrInst(const IndirectBrInst &IBI);
2272 void init(Value *Address, unsigned NumDests);
2273 void resizeOperands(unsigned No);
2274 // allocate space for exactly zero operands
2275 void *operator new(size_t s) {
2276 return User::operator new(s, 0);
2278 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2279 /// Address to jump to. The number of expected destinations can be specified
2280 /// here to make memory allocation more efficient. This constructor can also
2281 /// autoinsert before another instruction.
2282 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2284 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2285 /// Address to jump to. The number of expected destinations can be specified
2286 /// here to make memory allocation more efficient. This constructor also
2287 /// autoinserts at the end of the specified BasicBlock.
2288 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2290 virtual IndirectBrInst *clone_impl() const;
2292 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2293 Instruction *InsertBefore = 0) {
2294 return new IndirectBrInst(Address, NumDests, InsertBefore);
2296 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2297 BasicBlock *InsertAtEnd) {
2298 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2302 /// Provide fast operand accessors.
2303 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2305 // Accessor Methods for IndirectBrInst instruction.
2306 Value *getAddress() { return getOperand(0); }
2307 const Value *getAddress() const { return getOperand(0); }
2308 void setAddress(Value *V) { setOperand(0, V); }
2311 /// getNumDestinations - return the number of possible destinations in this
2312 /// indirectbr instruction.
2313 unsigned getNumDestinations() const { return getNumOperands()-1; }
2315 /// getDestination - Return the specified destination.
2316 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2317 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2319 /// addDestination - Add a destination.
2321 void addDestination(BasicBlock *Dest);
2323 /// removeDestination - This method removes the specified successor from the
2324 /// indirectbr instruction.
2325 void removeDestination(unsigned i);
2327 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2328 BasicBlock *getSuccessor(unsigned i) const {
2329 return cast<BasicBlock>(getOperand(i+1));
2331 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2332 setOperand(i+1, (Value*)NewSucc);
2335 // Methods for support type inquiry through isa, cast, and dyn_cast:
2336 static inline bool classof(const IndirectBrInst *) { return true; }
2337 static inline bool classof(const Instruction *I) {
2338 return I->getOpcode() == Instruction::IndirectBr;
2340 static inline bool classof(const Value *V) {
2341 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2344 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2345 virtual unsigned getNumSuccessorsV() const;
2346 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2350 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2353 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2356 //===----------------------------------------------------------------------===//
2358 //===----------------------------------------------------------------------===//
2360 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2361 /// calling convention of the call.
2363 class InvokeInst : public TerminatorInst {
2364 AttrListPtr AttributeList;
2365 InvokeInst(const InvokeInst &BI);
2366 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2367 Value* const *Args, unsigned NumArgs);
2369 template<typename InputIterator>
2370 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2371 InputIterator ArgBegin, InputIterator ArgEnd,
2372 const Twine &NameStr,
2373 // This argument ensures that we have an iterator we can
2374 // do arithmetic on in constant time
2375 std::random_access_iterator_tag) {
2376 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2378 // This requires that the iterator points to contiguous memory.
2379 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2383 /// Construct an InvokeInst given a range of arguments.
2384 /// InputIterator must be a random-access iterator pointing to
2385 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2386 /// made for random-accessness but not for contiguous storage as
2387 /// that would incur runtime overhead.
2389 /// @brief Construct an InvokeInst from a range of arguments
2390 template<typename InputIterator>
2391 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2392 InputIterator ArgBegin, InputIterator ArgEnd,
2394 const Twine &NameStr, Instruction *InsertBefore);
2396 /// Construct an InvokeInst given a range of arguments.
2397 /// InputIterator must be a random-access iterator pointing to
2398 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2399 /// made for random-accessness but not for contiguous storage as
2400 /// that would incur runtime overhead.
2402 /// @brief Construct an InvokeInst from a range of arguments
2403 template<typename InputIterator>
2404 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2405 InputIterator ArgBegin, InputIterator ArgEnd,
2407 const Twine &NameStr, BasicBlock *InsertAtEnd);
2409 virtual InvokeInst *clone_impl() const;
2411 template<typename InputIterator>
2412 static InvokeInst *Create(Value *Func,
2413 BasicBlock *IfNormal, BasicBlock *IfException,
2414 InputIterator ArgBegin, InputIterator ArgEnd,
2415 const Twine &NameStr = "",
2416 Instruction *InsertBefore = 0) {
2417 unsigned Values(ArgEnd - ArgBegin + 3);
2418 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2419 Values, NameStr, InsertBefore);
2421 template<typename InputIterator>
2422 static InvokeInst *Create(Value *Func,
2423 BasicBlock *IfNormal, BasicBlock *IfException,
2424 InputIterator ArgBegin, InputIterator ArgEnd,
2425 const Twine &NameStr,
2426 BasicBlock *InsertAtEnd) {
2427 unsigned Values(ArgEnd - ArgBegin + 3);
2428 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2429 Values, NameStr, InsertAtEnd);
2432 /// Provide fast operand accessors
2433 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2435 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2437 CallingConv::ID getCallingConv() const {
2438 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2440 void setCallingConv(CallingConv::ID CC) {
2441 setInstructionSubclassData(static_cast<unsigned>(CC));
2444 /// getAttributes - Return the parameter attributes for this invoke.
2446 const AttrListPtr &getAttributes() const { return AttributeList; }
2448 /// setAttributes - Set the parameter attributes for this invoke.
2450 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2452 /// addAttribute - adds the attribute to the list of attributes.
2453 void addAttribute(unsigned i, Attributes attr);
2455 /// removeAttribute - removes the attribute from the list of attributes.
2456 void removeAttribute(unsigned i, Attributes attr);
2458 /// @brief Determine whether the call or the callee has the given attribute.
2459 bool paramHasAttr(unsigned i, Attributes attr) const;
2461 /// @brief Extract the alignment for a call or parameter (0=unknown).
2462 unsigned getParamAlignment(unsigned i) const {
2463 return AttributeList.getParamAlignment(i);
2466 /// @brief Return true if the call should not be inlined.
2467 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
2468 void setIsNoInline(bool Value) {
2469 if (Value) addAttribute(~0, Attribute::NoInline);
2470 else removeAttribute(~0, Attribute::NoInline);
2473 /// @brief Determine if the call does not access memory.
2474 bool doesNotAccessMemory() const {
2475 return paramHasAttr(~0, Attribute::ReadNone);
2477 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2478 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2479 else removeAttribute(~0, Attribute::ReadNone);
2482 /// @brief Determine if the call does not access or only reads memory.
2483 bool onlyReadsMemory() const {
2484 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2486 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2487 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2488 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2491 /// @brief Determine if the call cannot return.
2492 bool doesNotReturn() const {
2493 return paramHasAttr(~0, Attribute::NoReturn);
2495 void setDoesNotReturn(bool DoesNotReturn = true) {
2496 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2497 else removeAttribute(~0, Attribute::NoReturn);
2500 /// @brief Determine if the call cannot unwind.
2501 bool doesNotThrow() const {
2502 return paramHasAttr(~0, Attribute::NoUnwind);
2504 void setDoesNotThrow(bool DoesNotThrow = true) {
2505 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2506 else removeAttribute(~0, Attribute::NoUnwind);
2509 /// @brief Determine if the call returns a structure through first
2510 /// pointer argument.
2511 bool hasStructRetAttr() const {
2512 // Be friendly and also check the callee.
2513 return paramHasAttr(1, Attribute::StructRet);
2516 /// @brief Determine if any call argument is an aggregate passed by value.
2517 bool hasByValArgument() const {
2518 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2521 /// getCalledFunction - Return the function called, or null if this is an
2522 /// indirect function invocation.
2524 Function *getCalledFunction() const {
2525 return dyn_cast<Function>(getOperand(0));
2528 /// getCalledValue - Get a pointer to the function that is invoked by this
2530 const Value *getCalledValue() const { return getOperand(0); }
2531 Value *getCalledValue() { return getOperand(0); }
2533 /// setCalledFunction - Set the function called.
2534 void setCalledFunction(Value* Fn) {
2538 // get*Dest - Return the destination basic blocks...
2539 BasicBlock *getNormalDest() const {
2540 return cast<BasicBlock>(getOperand(1));
2542 BasicBlock *getUnwindDest() const {
2543 return cast<BasicBlock>(getOperand(2));
2545 void setNormalDest(BasicBlock *B) {
2546 setOperand(1, (Value*)B);
2549 void setUnwindDest(BasicBlock *B) {
2550 setOperand(2, (Value*)B);
2553 BasicBlock *getSuccessor(unsigned i) const {
2554 assert(i < 2 && "Successor # out of range for invoke!");
2555 return i == 0 ? getNormalDest() : getUnwindDest();
2558 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2559 assert(idx < 2 && "Successor # out of range for invoke!");
2560 setOperand(idx+1, (Value*)NewSucc);
2563 unsigned getNumSuccessors() const { return 2; }
2565 // Methods for support type inquiry through isa, cast, and dyn_cast:
2566 static inline bool classof(const InvokeInst *) { return true; }
2567 static inline bool classof(const Instruction *I) {
2568 return (I->getOpcode() == Instruction::Invoke);
2570 static inline bool classof(const Value *V) {
2571 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2574 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2575 virtual unsigned getNumSuccessorsV() const;
2576 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2578 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2579 // method so that subclasses cannot accidentally use it.
2580 void setInstructionSubclassData(unsigned short D) {
2581 Instruction::setInstructionSubclassData(D);
2586 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2589 template<typename InputIterator>
2590 InvokeInst::InvokeInst(Value *Func,
2591 BasicBlock *IfNormal, BasicBlock *IfException,
2592 InputIterator ArgBegin, InputIterator ArgEnd,
2594 const Twine &NameStr, Instruction *InsertBefore)
2595 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2596 ->getElementType())->getReturnType(),
2597 Instruction::Invoke,
2598 OperandTraits<InvokeInst>::op_end(this) - Values,
2599 Values, InsertBefore) {
2600 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2601 typename std::iterator_traits<InputIterator>::iterator_category());
2603 template<typename InputIterator>
2604 InvokeInst::InvokeInst(Value *Func,
2605 BasicBlock *IfNormal, BasicBlock *IfException,
2606 InputIterator ArgBegin, InputIterator ArgEnd,
2608 const Twine &NameStr, BasicBlock *InsertAtEnd)
2609 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2610 ->getElementType())->getReturnType(),
2611 Instruction::Invoke,
2612 OperandTraits<InvokeInst>::op_end(this) - Values,
2613 Values, InsertAtEnd) {
2614 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2615 typename std::iterator_traits<InputIterator>::iterator_category());
2618 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2620 //===----------------------------------------------------------------------===//
2622 //===----------------------------------------------------------------------===//
2624 //===---------------------------------------------------------------------------
2625 /// UnwindInst - Immediately exit the current function, unwinding the stack
2626 /// until an invoke instruction is found.
2628 class UnwindInst : public TerminatorInst {
2629 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2631 virtual UnwindInst *clone_impl() const;
2633 // allocate space for exactly zero operands
2634 void *operator new(size_t s) {
2635 return User::operator new(s, 0);
2637 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2638 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2640 unsigned getNumSuccessors() const { return 0; }
2642 // Methods for support type inquiry through isa, cast, and dyn_cast:
2643 static inline bool classof(const UnwindInst *) { return true; }
2644 static inline bool classof(const Instruction *I) {
2645 return I->getOpcode() == Instruction::Unwind;
2647 static inline bool classof(const Value *V) {
2648 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2651 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2652 virtual unsigned getNumSuccessorsV() const;
2653 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2656 //===----------------------------------------------------------------------===//
2657 // UnreachableInst Class
2658 //===----------------------------------------------------------------------===//
2660 //===---------------------------------------------------------------------------
2661 /// UnreachableInst - This function has undefined behavior. In particular, the
2662 /// presence of this instruction indicates some higher level knowledge that the
2663 /// end of the block cannot be reached.
2665 class UnreachableInst : public TerminatorInst {
2666 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2668 virtual UnreachableInst *clone_impl() const;
2671 // allocate space for exactly zero operands
2672 void *operator new(size_t s) {
2673 return User::operator new(s, 0);
2675 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2676 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2678 unsigned getNumSuccessors() const { return 0; }
2680 // Methods for support type inquiry through isa, cast, and dyn_cast:
2681 static inline bool classof(const UnreachableInst *) { return true; }
2682 static inline bool classof(const Instruction *I) {
2683 return I->getOpcode() == Instruction::Unreachable;
2685 static inline bool classof(const Value *V) {
2686 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2689 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2690 virtual unsigned getNumSuccessorsV() const;
2691 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2694 //===----------------------------------------------------------------------===//
2696 //===----------------------------------------------------------------------===//
2698 /// @brief This class represents a truncation of integer types.
2699 class TruncInst : public CastInst {
2701 /// @brief Clone an identical TruncInst
2702 virtual TruncInst *clone_impl() const;
2705 /// @brief Constructor with insert-before-instruction semantics
2707 Value *S, ///< The value to be truncated
2708 const Type *Ty, ///< The (smaller) type to truncate to
2709 const Twine &NameStr = "", ///< A name for the new instruction
2710 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2713 /// @brief Constructor with insert-at-end-of-block semantics
2715 Value *S, ///< The value to be truncated
2716 const Type *Ty, ///< The (smaller) type to truncate to
2717 const Twine &NameStr, ///< A name for the new instruction
2718 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2721 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2722 static inline bool classof(const TruncInst *) { return true; }
2723 static inline bool classof(const Instruction *I) {
2724 return I->getOpcode() == Trunc;
2726 static inline bool classof(const Value *V) {
2727 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2731 //===----------------------------------------------------------------------===//
2733 //===----------------------------------------------------------------------===//
2735 /// @brief This class represents zero extension of integer types.
2736 class ZExtInst : public CastInst {
2738 /// @brief Clone an identical ZExtInst
2739 virtual ZExtInst *clone_impl() const;
2742 /// @brief Constructor with insert-before-instruction semantics
2744 Value *S, ///< The value to be zero extended
2745 const Type *Ty, ///< The type to zero extend to
2746 const Twine &NameStr = "", ///< A name for the new instruction
2747 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2750 /// @brief Constructor with insert-at-end semantics.
2752 Value *S, ///< The value to be zero extended
2753 const Type *Ty, ///< The type to zero extend to
2754 const Twine &NameStr, ///< A name for the new instruction
2755 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2758 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2759 static inline bool classof(const ZExtInst *) { return true; }
2760 static inline bool classof(const Instruction *I) {
2761 return I->getOpcode() == ZExt;
2763 static inline bool classof(const Value *V) {
2764 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2768 //===----------------------------------------------------------------------===//
2770 //===----------------------------------------------------------------------===//
2772 /// @brief This class represents a sign extension of integer types.
2773 class SExtInst : public CastInst {
2775 /// @brief Clone an identical SExtInst
2776 virtual SExtInst *clone_impl() const;
2779 /// @brief Constructor with insert-before-instruction semantics
2781 Value *S, ///< The value to be sign extended
2782 const Type *Ty, ///< The type to sign extend to
2783 const Twine &NameStr = "", ///< A name for the new instruction
2784 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2787 /// @brief Constructor with insert-at-end-of-block semantics
2789 Value *S, ///< The value to be sign extended
2790 const Type *Ty, ///< The type to sign extend to
2791 const Twine &NameStr, ///< A name for the new instruction
2792 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2795 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2796 static inline bool classof(const SExtInst *) { return true; }
2797 static inline bool classof(const Instruction *I) {
2798 return I->getOpcode() == SExt;
2800 static inline bool classof(const Value *V) {
2801 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2805 //===----------------------------------------------------------------------===//
2806 // FPTruncInst Class
2807 //===----------------------------------------------------------------------===//
2809 /// @brief This class represents a truncation of floating point types.
2810 class FPTruncInst : public CastInst {
2812 /// @brief Clone an identical FPTruncInst
2813 virtual FPTruncInst *clone_impl() const;
2816 /// @brief Constructor with insert-before-instruction semantics
2818 Value *S, ///< The value to be truncated
2819 const Type *Ty, ///< The type to truncate to
2820 const Twine &NameStr = "", ///< A name for the new instruction
2821 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2824 /// @brief Constructor with insert-before-instruction semantics
2826 Value *S, ///< The value to be truncated
2827 const Type *Ty, ///< The type to truncate to
2828 const Twine &NameStr, ///< A name for the new instruction
2829 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2832 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2833 static inline bool classof(const FPTruncInst *) { return true; }
2834 static inline bool classof(const Instruction *I) {
2835 return I->getOpcode() == FPTrunc;
2837 static inline bool classof(const Value *V) {
2838 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2842 //===----------------------------------------------------------------------===//
2844 //===----------------------------------------------------------------------===//
2846 /// @brief This class represents an extension of floating point types.
2847 class FPExtInst : public CastInst {
2849 /// @brief Clone an identical FPExtInst
2850 virtual FPExtInst *clone_impl() const;
2853 /// @brief Constructor with insert-before-instruction semantics
2855 Value *S, ///< The value to be extended
2856 const Type *Ty, ///< The type to extend to
2857 const Twine &NameStr = "", ///< A name for the new instruction
2858 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2861 /// @brief Constructor with insert-at-end-of-block semantics
2863 Value *S, ///< The value to be extended
2864 const Type *Ty, ///< The type to extend to
2865 const Twine &NameStr, ///< A name for the new instruction
2866 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2869 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2870 static inline bool classof(const FPExtInst *) { return true; }
2871 static inline bool classof(const Instruction *I) {
2872 return I->getOpcode() == FPExt;
2874 static inline bool classof(const Value *V) {
2875 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2879 //===----------------------------------------------------------------------===//
2881 //===----------------------------------------------------------------------===//
2883 /// @brief This class represents a cast unsigned integer to floating point.
2884 class UIToFPInst : public CastInst {
2886 /// @brief Clone an identical UIToFPInst
2887 virtual UIToFPInst *clone_impl() const;
2890 /// @brief Constructor with insert-before-instruction semantics
2892 Value *S, ///< The value to be converted
2893 const Type *Ty, ///< The type to convert to
2894 const Twine &NameStr = "", ///< A name for the new instruction
2895 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2898 /// @brief Constructor with insert-at-end-of-block semantics
2900 Value *S, ///< The value to be converted
2901 const Type *Ty, ///< The type to convert to
2902 const Twine &NameStr, ///< A name for the new instruction
2903 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2906 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2907 static inline bool classof(const UIToFPInst *) { return true; }
2908 static inline bool classof(const Instruction *I) {
2909 return I->getOpcode() == UIToFP;
2911 static inline bool classof(const Value *V) {
2912 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2916 //===----------------------------------------------------------------------===//
2918 //===----------------------------------------------------------------------===//
2920 /// @brief This class represents a cast from signed integer to floating point.
2921 class SIToFPInst : public CastInst {
2923 /// @brief Clone an identical SIToFPInst
2924 virtual SIToFPInst *clone_impl() const;
2927 /// @brief Constructor with insert-before-instruction semantics
2929 Value *S, ///< The value to be converted
2930 const Type *Ty, ///< The type to convert to
2931 const Twine &NameStr = "", ///< A name for the new instruction
2932 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2935 /// @brief Constructor with insert-at-end-of-block semantics
2937 Value *S, ///< The value to be converted
2938 const Type *Ty, ///< The type to convert to
2939 const Twine &NameStr, ///< A name for the new instruction
2940 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2943 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2944 static inline bool classof(const SIToFPInst *) { return true; }
2945 static inline bool classof(const Instruction *I) {
2946 return I->getOpcode() == SIToFP;
2948 static inline bool classof(const Value *V) {
2949 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2953 //===----------------------------------------------------------------------===//
2955 //===----------------------------------------------------------------------===//
2957 /// @brief This class represents a cast from floating point to unsigned integer
2958 class FPToUIInst : public CastInst {
2960 /// @brief Clone an identical FPToUIInst
2961 virtual FPToUIInst *clone_impl() const;
2964 /// @brief Constructor with insert-before-instruction semantics
2966 Value *S, ///< The value to be converted
2967 const Type *Ty, ///< The type to convert to
2968 const Twine &NameStr = "", ///< A name for the new instruction
2969 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2972 /// @brief Constructor with insert-at-end-of-block semantics
2974 Value *S, ///< The value to be converted
2975 const Type *Ty, ///< The type to convert to
2976 const Twine &NameStr, ///< A name for the new instruction
2977 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2980 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2981 static inline bool classof(const FPToUIInst *) { return true; }
2982 static inline bool classof(const Instruction *I) {
2983 return I->getOpcode() == FPToUI;
2985 static inline bool classof(const Value *V) {
2986 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2990 //===----------------------------------------------------------------------===//
2992 //===----------------------------------------------------------------------===//
2994 /// @brief This class represents a cast from floating point to signed integer.
2995 class FPToSIInst : public CastInst {
2997 /// @brief Clone an identical FPToSIInst
2998 virtual FPToSIInst *clone_impl() const;
3001 /// @brief Constructor with insert-before-instruction semantics
3003 Value *S, ///< The value to be converted
3004 const Type *Ty, ///< The type to convert to
3005 const Twine &NameStr = "", ///< A name for the new instruction
3006 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3009 /// @brief Constructor with insert-at-end-of-block semantics
3011 Value *S, ///< The value to be converted
3012 const Type *Ty, ///< The type to convert to
3013 const Twine &NameStr, ///< A name for the new instruction
3014 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3017 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3018 static inline bool classof(const FPToSIInst *) { return true; }
3019 static inline bool classof(const Instruction *I) {
3020 return I->getOpcode() == FPToSI;
3022 static inline bool classof(const Value *V) {
3023 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3027 //===----------------------------------------------------------------------===//
3028 // IntToPtrInst Class
3029 //===----------------------------------------------------------------------===//
3031 /// @brief This class represents a cast from an integer to a pointer.
3032 class IntToPtrInst : public CastInst {
3034 /// @brief Constructor with insert-before-instruction semantics
3036 Value *S, ///< The value to be converted
3037 const Type *Ty, ///< The type to convert to
3038 const Twine &NameStr = "", ///< A name for the new instruction
3039 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3042 /// @brief Constructor with insert-at-end-of-block semantics
3044 Value *S, ///< The value to be converted
3045 const Type *Ty, ///< The type to convert to
3046 const Twine &NameStr, ///< A name for the new instruction
3047 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3050 /// @brief Clone an identical IntToPtrInst
3051 virtual IntToPtrInst *clone_impl() const;
3053 // Methods for support type inquiry through isa, cast, and dyn_cast:
3054 static inline bool classof(const IntToPtrInst *) { return true; }
3055 static inline bool classof(const Instruction *I) {
3056 return I->getOpcode() == IntToPtr;
3058 static inline bool classof(const Value *V) {
3059 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3063 //===----------------------------------------------------------------------===//
3064 // PtrToIntInst Class
3065 //===----------------------------------------------------------------------===//
3067 /// @brief This class represents a cast from a pointer to an integer
3068 class PtrToIntInst : public CastInst {
3070 /// @brief Clone an identical PtrToIntInst
3071 virtual PtrToIntInst *clone_impl() const;
3074 /// @brief Constructor with insert-before-instruction semantics
3076 Value *S, ///< The value to be converted
3077 const Type *Ty, ///< The type to convert to
3078 const Twine &NameStr = "", ///< A name for the new instruction
3079 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3082 /// @brief Constructor with insert-at-end-of-block semantics
3084 Value *S, ///< The value to be converted
3085 const Type *Ty, ///< The type to convert to
3086 const Twine &NameStr, ///< A name for the new instruction
3087 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3090 // Methods for support type inquiry through isa, cast, and dyn_cast:
3091 static inline bool classof(const PtrToIntInst *) { return true; }
3092 static inline bool classof(const Instruction *I) {
3093 return I->getOpcode() == PtrToInt;
3095 static inline bool classof(const Value *V) {
3096 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3100 //===----------------------------------------------------------------------===//
3101 // BitCastInst Class
3102 //===----------------------------------------------------------------------===//
3104 /// @brief This class represents a no-op cast from one type to another.
3105 class BitCastInst : public CastInst {
3107 /// @brief Clone an identical BitCastInst
3108 virtual BitCastInst *clone_impl() const;
3111 /// @brief Constructor with insert-before-instruction semantics
3113 Value *S, ///< The value to be casted
3114 const Type *Ty, ///< The type to casted to
3115 const Twine &NameStr = "", ///< A name for the new instruction
3116 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3119 /// @brief Constructor with insert-at-end-of-block semantics
3121 Value *S, ///< The value to be casted
3122 const Type *Ty, ///< The type to casted to
3123 const Twine &NameStr, ///< A name for the new instruction
3124 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3127 // Methods for support type inquiry through isa, cast, and dyn_cast:
3128 static inline bool classof(const BitCastInst *) { return true; }
3129 static inline bool classof(const Instruction *I) {
3130 return I->getOpcode() == BitCast;
3132 static inline bool classof(const Value *V) {
3133 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3137 } // End llvm namespace