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 *getValueOperand() { return getOperand(0); }
239 const Value *getValueOperand() const { return getOperand(0); }
241 Value *getPointerOperand() { return getOperand(1); }
242 const Value *getPointerOperand() const { return getOperand(1); }
243 static unsigned getPointerOperandIndex() { return 1U; }
245 unsigned getPointerAddressSpace() const {
246 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
249 // Methods for support type inquiry through isa, cast, and dyn_cast:
250 static inline bool classof(const StoreInst *) { return true; }
251 static inline bool classof(const Instruction *I) {
252 return I->getOpcode() == Instruction::Store;
254 static inline bool classof(const Value *V) {
255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
258 // Shadow Instruction::setInstructionSubclassData with a private forwarding
259 // method so that subclasses cannot accidentally use it.
260 void setInstructionSubclassData(unsigned short D) {
261 Instruction::setInstructionSubclassData(D);
266 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
269 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
271 //===----------------------------------------------------------------------===//
272 // GetElementPtrInst Class
273 //===----------------------------------------------------------------------===//
275 // checkType - Simple wrapper function to give a better assertion failure
276 // message on bad indexes for a gep instruction.
278 static inline const Type *checkType(const Type *Ty) {
279 assert(Ty && "Invalid GetElementPtrInst indices for type!");
283 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
284 /// access elements of arrays and structs
286 class GetElementPtrInst : public Instruction {
287 GetElementPtrInst(const GetElementPtrInst &GEPI);
288 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
289 const Twine &NameStr);
290 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
292 template<typename InputIterator>
293 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
294 const Twine &NameStr,
295 // This argument ensures that we have an iterator we can
296 // do arithmetic on in constant time
297 std::random_access_iterator_tag) {
298 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
301 // This requires that the iterator points to contiguous memory.
302 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
303 // we have to build an array here
306 init(Ptr, 0, NumIdx, NameStr);
310 /// getIndexedType - Returns the type of the element that would be loaded with
311 /// a load instruction with the specified parameters.
313 /// Null is returned if the indices are invalid for the specified
316 template<typename InputIterator>
317 static const Type *getIndexedType(const Type *Ptr,
318 InputIterator IdxBegin,
319 InputIterator IdxEnd,
320 // This argument ensures that we
321 // have an iterator we can do
322 // arithmetic on in constant time
323 std::random_access_iterator_tag) {
324 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
327 // This requires that the iterator points to contiguous memory.
328 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
330 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
333 /// Constructors - Create a getelementptr instruction with a base pointer an
334 /// list of indices. The first ctor can optionally insert before an existing
335 /// instruction, the second appends the new instruction to the specified
337 template<typename InputIterator>
338 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
339 InputIterator IdxEnd,
341 const Twine &NameStr,
342 Instruction *InsertBefore);
343 template<typename InputIterator>
344 inline GetElementPtrInst(Value *Ptr,
345 InputIterator IdxBegin, InputIterator IdxEnd,
347 const Twine &NameStr, BasicBlock *InsertAtEnd);
349 /// Constructors - These two constructors are convenience methods because one
350 /// and two index getelementptr instructions are so common.
351 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
352 Instruction *InsertBefore = 0);
353 GetElementPtrInst(Value *Ptr, Value *Idx,
354 const Twine &NameStr, BasicBlock *InsertAtEnd);
356 virtual GetElementPtrInst *clone_impl() const;
358 template<typename InputIterator>
359 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
360 InputIterator IdxEnd,
361 const Twine &NameStr = "",
362 Instruction *InsertBefore = 0) {
363 typename std::iterator_traits<InputIterator>::difference_type Values =
364 1 + std::distance(IdxBegin, IdxEnd);
366 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
368 template<typename InputIterator>
369 static GetElementPtrInst *Create(Value *Ptr,
370 InputIterator IdxBegin, InputIterator IdxEnd,
371 const Twine &NameStr,
372 BasicBlock *InsertAtEnd) {
373 typename std::iterator_traits<InputIterator>::difference_type Values =
374 1 + std::distance(IdxBegin, IdxEnd);
376 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
379 /// Constructors - These two creators are convenience methods because one
380 /// index getelementptr instructions are so common.
381 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
382 const Twine &NameStr = "",
383 Instruction *InsertBefore = 0) {
384 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
386 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
387 const Twine &NameStr,
388 BasicBlock *InsertAtEnd) {
389 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
392 /// Create an "inbounds" getelementptr. See the documentation for the
393 /// "inbounds" flag in LangRef.html for details.
394 template<typename InputIterator>
395 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
396 InputIterator IdxEnd,
397 const Twine &NameStr = "",
398 Instruction *InsertBefore = 0) {
399 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
400 NameStr, InsertBefore);
401 GEP->setIsInBounds(true);
404 template<typename InputIterator>
405 static GetElementPtrInst *CreateInBounds(Value *Ptr,
406 InputIterator IdxBegin,
407 InputIterator IdxEnd,
408 const Twine &NameStr,
409 BasicBlock *InsertAtEnd) {
410 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
411 NameStr, InsertAtEnd);
412 GEP->setIsInBounds(true);
415 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
416 const Twine &NameStr = "",
417 Instruction *InsertBefore = 0) {
418 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
419 GEP->setIsInBounds(true);
422 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
423 const Twine &NameStr,
424 BasicBlock *InsertAtEnd) {
425 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
426 GEP->setIsInBounds(true);
430 /// Transparently provide more efficient getOperand methods.
431 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
433 // getType - Overload to return most specific pointer type...
434 const PointerType *getType() const {
435 return reinterpret_cast<const PointerType*>(Instruction::getType());
438 /// getIndexedType - Returns the type of the element that would be loaded with
439 /// a load instruction with the specified parameters.
441 /// Null is returned if the indices are invalid for the specified
444 template<typename InputIterator>
445 static const Type *getIndexedType(const Type *Ptr,
446 InputIterator IdxBegin,
447 InputIterator IdxEnd) {
448 return getIndexedType(Ptr, IdxBegin, IdxEnd,
449 typename std::iterator_traits<InputIterator>::
450 iterator_category());
453 static const Type *getIndexedType(const Type *Ptr,
454 Value* const *Idx, unsigned NumIdx);
456 static const Type *getIndexedType(const Type *Ptr,
457 uint64_t const *Idx, unsigned NumIdx);
459 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
461 inline op_iterator idx_begin() { return op_begin()+1; }
462 inline const_op_iterator idx_begin() const { return op_begin()+1; }
463 inline op_iterator idx_end() { return op_end(); }
464 inline const_op_iterator idx_end() const { return op_end(); }
466 Value *getPointerOperand() {
467 return getOperand(0);
469 const Value *getPointerOperand() const {
470 return getOperand(0);
472 static unsigned getPointerOperandIndex() {
473 return 0U; // get index for modifying correct operand
476 unsigned getPointerAddressSpace() const {
477 return cast<PointerType>(getType())->getAddressSpace();
480 /// getPointerOperandType - Method to return the pointer operand as a
482 const PointerType *getPointerOperandType() const {
483 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
487 unsigned getNumIndices() const { // Note: always non-negative
488 return getNumOperands() - 1;
491 bool hasIndices() const {
492 return getNumOperands() > 1;
495 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
496 /// zeros. If so, the result pointer and the first operand have the same
497 /// value, just potentially different types.
498 bool hasAllZeroIndices() const;
500 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
501 /// constant integers. If so, the result pointer and the first operand have
502 /// a constant offset between them.
503 bool hasAllConstantIndices() const;
505 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
506 /// See LangRef.html for the meaning of inbounds on a getelementptr.
507 void setIsInBounds(bool b = true);
509 /// isInBounds - Determine whether the GEP has the inbounds flag.
510 bool isInBounds() const;
512 // Methods for support type inquiry through isa, cast, and dyn_cast:
513 static inline bool classof(const GetElementPtrInst *) { return true; }
514 static inline bool classof(const Instruction *I) {
515 return (I->getOpcode() == Instruction::GetElementPtr);
517 static inline bool classof(const Value *V) {
518 return isa<Instruction>(V) && classof(cast<Instruction>(V));
523 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
526 template<typename InputIterator>
527 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
528 InputIterator IdxBegin,
529 InputIterator IdxEnd,
531 const Twine &NameStr,
532 Instruction *InsertBefore)
533 : Instruction(PointerType::get(checkType(
534 getIndexedType(Ptr->getType(),
536 cast<PointerType>(Ptr->getType())
537 ->getAddressSpace()),
539 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
540 Values, InsertBefore) {
541 init(Ptr, IdxBegin, IdxEnd, NameStr,
542 typename std::iterator_traits<InputIterator>::iterator_category());
544 template<typename InputIterator>
545 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
546 InputIterator IdxBegin,
547 InputIterator IdxEnd,
549 const Twine &NameStr,
550 BasicBlock *InsertAtEnd)
551 : Instruction(PointerType::get(checkType(
552 getIndexedType(Ptr->getType(),
554 cast<PointerType>(Ptr->getType())
555 ->getAddressSpace()),
557 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
558 Values, InsertAtEnd) {
559 init(Ptr, IdxBegin, IdxEnd, NameStr,
560 typename std::iterator_traits<InputIterator>::iterator_category());
564 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
567 //===----------------------------------------------------------------------===//
569 //===----------------------------------------------------------------------===//
571 /// This instruction compares its operands according to the predicate given
572 /// to the constructor. It only operates on integers or pointers. The operands
573 /// must be identical types.
574 /// @brief Represent an integer comparison operator.
575 class ICmpInst: public CmpInst {
577 /// @brief Clone an indentical ICmpInst
578 virtual ICmpInst *clone_impl() const;
580 /// @brief Constructor with insert-before-instruction semantics.
582 Instruction *InsertBefore, ///< Where to insert
583 Predicate pred, ///< The predicate to use for the comparison
584 Value *LHS, ///< The left-hand-side of the expression
585 Value *RHS, ///< The right-hand-side of the expression
586 const Twine &NameStr = "" ///< Name of the instruction
587 ) : CmpInst(makeCmpResultType(LHS->getType()),
588 Instruction::ICmp, pred, LHS, RHS, NameStr,
590 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
591 pred <= CmpInst::LAST_ICMP_PREDICATE &&
592 "Invalid ICmp predicate value");
593 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
594 "Both operands to ICmp instruction are not of the same type!");
595 // Check that the operands are the right type
596 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
597 getOperand(0)->getType()->isPointerTy()) &&
598 "Invalid operand types for ICmp instruction");
601 /// @brief Constructor with insert-at-end semantics.
603 BasicBlock &InsertAtEnd, ///< Block to insert into.
604 Predicate pred, ///< The predicate to use for the comparison
605 Value *LHS, ///< The left-hand-side of the expression
606 Value *RHS, ///< The right-hand-side of the expression
607 const Twine &NameStr = "" ///< Name of the instruction
608 ) : CmpInst(makeCmpResultType(LHS->getType()),
609 Instruction::ICmp, pred, LHS, RHS, NameStr,
611 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
612 pred <= CmpInst::LAST_ICMP_PREDICATE &&
613 "Invalid ICmp predicate value");
614 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
615 "Both operands to ICmp instruction are not of the same type!");
616 // Check that the operands are the right type
617 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
618 getOperand(0)->getType()->isPointerTy()) &&
619 "Invalid operand types for ICmp instruction");
622 /// @brief Constructor with no-insertion semantics
624 Predicate pred, ///< The predicate to use for the comparison
625 Value *LHS, ///< The left-hand-side of the expression
626 Value *RHS, ///< The right-hand-side of the expression
627 const Twine &NameStr = "" ///< Name of the instruction
628 ) : CmpInst(makeCmpResultType(LHS->getType()),
629 Instruction::ICmp, pred, LHS, RHS, NameStr) {
630 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
631 pred <= CmpInst::LAST_ICMP_PREDICATE &&
632 "Invalid ICmp predicate value");
633 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
634 "Both operands to ICmp instruction are not of the same type!");
635 // Check that the operands are the right type
636 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
637 getOperand(0)->getType()->isPointerTy()) &&
638 "Invalid operand types for ICmp instruction");
641 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
642 /// @returns the predicate that would be the result if the operand were
643 /// regarded as signed.
644 /// @brief Return the signed version of the predicate
645 Predicate getSignedPredicate() const {
646 return getSignedPredicate(getPredicate());
649 /// This is a static version that you can use without an instruction.
650 /// @brief Return the signed version of the predicate.
651 static Predicate getSignedPredicate(Predicate pred);
653 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
654 /// @returns the predicate that would be the result if the operand were
655 /// regarded as unsigned.
656 /// @brief Return the unsigned version of the predicate
657 Predicate getUnsignedPredicate() const {
658 return getUnsignedPredicate(getPredicate());
661 /// This is a static version that you can use without an instruction.
662 /// @brief Return the unsigned version of the predicate.
663 static Predicate getUnsignedPredicate(Predicate pred);
665 /// isEquality - Return true if this predicate is either EQ or NE. This also
666 /// tests for commutativity.
667 static bool isEquality(Predicate P) {
668 return P == ICMP_EQ || P == ICMP_NE;
671 /// isEquality - Return true if this predicate is either EQ or NE. This also
672 /// tests for commutativity.
673 bool isEquality() const {
674 return isEquality(getPredicate());
677 /// @returns true if the predicate of this ICmpInst is commutative
678 /// @brief Determine if this relation is commutative.
679 bool isCommutative() const { return isEquality(); }
681 /// isRelational - Return true if the predicate is relational (not EQ or NE).
683 bool isRelational() const {
684 return !isEquality();
687 /// isRelational - Return true if the predicate is relational (not EQ or NE).
689 static bool isRelational(Predicate P) {
690 return !isEquality(P);
693 /// Initialize a set of values that all satisfy the predicate with C.
694 /// @brief Make a ConstantRange for a relation with a constant value.
695 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
697 /// Exchange the two operands to this instruction in such a way that it does
698 /// not modify the semantics of the instruction. The predicate value may be
699 /// changed to retain the same result if the predicate is order dependent
701 /// @brief Swap operands and adjust predicate.
702 void swapOperands() {
703 setPredicate(getSwappedPredicate());
704 Op<0>().swap(Op<1>());
707 // Methods for support type inquiry through isa, cast, and dyn_cast:
708 static inline bool classof(const ICmpInst *) { return true; }
709 static inline bool classof(const Instruction *I) {
710 return I->getOpcode() == Instruction::ICmp;
712 static inline bool classof(const Value *V) {
713 return isa<Instruction>(V) && classof(cast<Instruction>(V));
718 //===----------------------------------------------------------------------===//
720 //===----------------------------------------------------------------------===//
722 /// This instruction compares its operands according to the predicate given
723 /// to the constructor. It only operates on floating point values or packed
724 /// vectors of floating point values. The operands must be identical types.
725 /// @brief Represents a floating point comparison operator.
726 class FCmpInst: public CmpInst {
728 /// @brief Clone an indentical FCmpInst
729 virtual FCmpInst *clone_impl() const;
731 /// @brief Constructor with insert-before-instruction semantics.
733 Instruction *InsertBefore, ///< Where to insert
734 Predicate pred, ///< The predicate to use for the comparison
735 Value *LHS, ///< The left-hand-side of the expression
736 Value *RHS, ///< The right-hand-side of the expression
737 const Twine &NameStr = "" ///< Name of the instruction
738 ) : CmpInst(makeCmpResultType(LHS->getType()),
739 Instruction::FCmp, pred, LHS, RHS, NameStr,
741 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
742 "Invalid FCmp predicate value");
743 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
744 "Both operands to FCmp instruction are not of the same type!");
745 // Check that the operands are the right type
746 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
747 "Invalid operand types for FCmp instruction");
750 /// @brief Constructor with insert-at-end semantics.
752 BasicBlock &InsertAtEnd, ///< Block to insert into.
753 Predicate pred, ///< The predicate to use for the comparison
754 Value *LHS, ///< The left-hand-side of the expression
755 Value *RHS, ///< The right-hand-side of the expression
756 const Twine &NameStr = "" ///< Name of the instruction
757 ) : CmpInst(makeCmpResultType(LHS->getType()),
758 Instruction::FCmp, pred, LHS, RHS, NameStr,
760 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
761 "Invalid FCmp predicate value");
762 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
763 "Both operands to FCmp instruction are not of the same type!");
764 // Check that the operands are the right type
765 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
766 "Invalid operand types for FCmp instruction");
769 /// @brief Constructor with no-insertion semantics
771 Predicate pred, ///< The predicate to use for the comparison
772 Value *LHS, ///< The left-hand-side of the expression
773 Value *RHS, ///< The right-hand-side of the expression
774 const Twine &NameStr = "" ///< Name of the instruction
775 ) : CmpInst(makeCmpResultType(LHS->getType()),
776 Instruction::FCmp, pred, LHS, RHS, NameStr) {
777 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
778 "Invalid FCmp predicate value");
779 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
780 "Both operands to FCmp instruction are not of the same type!");
781 // Check that the operands are the right type
782 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
783 "Invalid operand types for FCmp instruction");
786 /// @returns true if the predicate of this instruction is EQ or NE.
787 /// @brief Determine if this is an equality predicate.
788 bool isEquality() const {
789 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
790 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
793 /// @returns true if the predicate of this instruction is commutative.
794 /// @brief Determine if this is a commutative predicate.
795 bool isCommutative() const {
796 return isEquality() ||
797 getPredicate() == FCMP_FALSE ||
798 getPredicate() == FCMP_TRUE ||
799 getPredicate() == FCMP_ORD ||
800 getPredicate() == FCMP_UNO;
803 /// @returns true if the predicate is relational (not EQ or NE).
804 /// @brief Determine if this a relational predicate.
805 bool isRelational() const { return !isEquality(); }
807 /// Exchange the two operands to this instruction in such a way that it does
808 /// not modify the semantics of the instruction. The predicate value may be
809 /// changed to retain the same result if the predicate is order dependent
811 /// @brief Swap operands and adjust predicate.
812 void swapOperands() {
813 setPredicate(getSwappedPredicate());
814 Op<0>().swap(Op<1>());
817 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
818 static inline bool classof(const FCmpInst *) { return true; }
819 static inline bool classof(const Instruction *I) {
820 return I->getOpcode() == Instruction::FCmp;
822 static inline bool classof(const Value *V) {
823 return isa<Instruction>(V) && classof(cast<Instruction>(V));
827 //===----------------------------------------------------------------------===//
828 /// CallInst - This class represents a function call, abstracting a target
829 /// machine's calling convention. This class uses low bit of the SubClassData
830 /// field to indicate whether or not this is a tail call. The rest of the bits
831 /// hold the calling convention of the call.
833 class CallInst : public Instruction {
834 AttrListPtr AttributeList; ///< parameter attributes for call
835 CallInst(const CallInst &CI);
836 void init(Value *Func, Value* const *Params, unsigned NumParams);
837 void init(Value *Func, Value *Actual1, Value *Actual2);
838 void init(Value *Func, Value *Actual);
839 void init(Value *Func);
841 template<typename InputIterator>
842 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
843 const Twine &NameStr,
844 // This argument ensures that we have an iterator we can
845 // do arithmetic on in constant time
846 std::random_access_iterator_tag) {
847 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
849 // This requires that the iterator points to contiguous memory.
850 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
854 /// Construct a CallInst given a range of arguments. InputIterator
855 /// must be a random-access iterator pointing to contiguous storage
856 /// (e.g. a std::vector<>::iterator). Checks are made for
857 /// random-accessness but not for contiguous storage as that would
858 /// incur runtime overhead.
859 /// @brief Construct a CallInst from a range of arguments
860 template<typename InputIterator>
861 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
862 const Twine &NameStr, Instruction *InsertBefore);
864 /// Construct a CallInst given a range of arguments. InputIterator
865 /// must be a random-access iterator pointing to contiguous storage
866 /// (e.g. a std::vector<>::iterator). Checks are made for
867 /// random-accessness but not for contiguous storage as that would
868 /// incur runtime overhead.
869 /// @brief Construct a CallInst from a range of arguments
870 template<typename InputIterator>
871 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
872 const Twine &NameStr, BasicBlock *InsertAtEnd);
874 CallInst(Value *F, Value *Actual, const Twine &NameStr,
875 Instruction *InsertBefore);
876 CallInst(Value *F, Value *Actual, const Twine &NameStr,
877 BasicBlock *InsertAtEnd);
878 explicit CallInst(Value *F, const Twine &NameStr,
879 Instruction *InsertBefore);
880 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
882 virtual CallInst *clone_impl() const;
884 template<typename InputIterator>
885 static CallInst *Create(Value *Func,
886 InputIterator ArgBegin, InputIterator ArgEnd,
887 const Twine &NameStr = "",
888 Instruction *InsertBefore = 0) {
889 return new(unsigned(ArgEnd - ArgBegin + 1))
890 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
892 template<typename InputIterator>
893 static CallInst *Create(Value *Func,
894 InputIterator ArgBegin, InputIterator ArgEnd,
895 const Twine &NameStr, BasicBlock *InsertAtEnd) {
896 return new(unsigned(ArgEnd - ArgBegin + 1))
897 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
899 static CallInst *Create(Value *F, Value *Actual,
900 const Twine &NameStr = "",
901 Instruction *InsertBefore = 0) {
902 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
904 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
905 BasicBlock *InsertAtEnd) {
906 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
908 static CallInst *Create(Value *F, const Twine &NameStr = "",
909 Instruction *InsertBefore = 0) {
910 return new(1) CallInst(F, NameStr, InsertBefore);
912 static CallInst *Create(Value *F, const Twine &NameStr,
913 BasicBlock *InsertAtEnd) {
914 return new(1) CallInst(F, NameStr, InsertAtEnd);
916 /// CreateMalloc - Generate the IR for a call to malloc:
917 /// 1. Compute the malloc call's argument as the specified type's size,
918 /// possibly multiplied by the array size if the array size is not
920 /// 2. Call malloc with that argument.
921 /// 3. Bitcast the result of the malloc call to the specified type.
922 static Instruction *CreateMalloc(Instruction *InsertBefore,
923 const Type *IntPtrTy, const Type *AllocTy,
924 Value *AllocSize, Value *ArraySize = 0,
925 Function* MallocF = 0,
926 const Twine &Name = "");
927 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
928 const Type *IntPtrTy, const Type *AllocTy,
929 Value *AllocSize, Value *ArraySize = 0,
930 Function* MallocF = 0,
931 const Twine &Name = "");
932 /// CreateFree - Generate the IR for a call to the builtin free function.
933 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
934 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
938 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
939 void setTailCall(bool isTC = true) {
940 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
944 /// @deprecated these "define hacks" will go away soon
945 /// @brief coerce out-of-tree code to abandon the low-level interfaces
946 /// @detail see below comments and update your code to high-level interfaces
947 /// - getOperand(0) ---> getCalledValue(), or possibly getCalledFunction
948 /// - setOperand(0, V) ---> setCalledFunction(V)
950 /// in LLVM v2.8-only code
951 /// - getOperand(N+1) ---> getArgOperand(N)
952 /// - setOperand(N+1, V) ---> setArgOperand(N, V)
953 /// - getNumOperands() ---> getNumArgOperands()+1 // note the "+1"!
955 /// in backward compatible code please consult llvm/Support/CallSite.h,
956 /// you should create a callsite using the CallInst pointer and call its
959 # define public private
960 # define protected private
961 /// Provide fast operand accessors
962 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
967 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
968 Value *getArgOperand(unsigned i) const { return getOperand(i); }
969 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
971 /// getCallingConv/setCallingConv - Get or set the calling convention of this
973 CallingConv::ID getCallingConv() const {
974 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
976 void setCallingConv(CallingConv::ID CC) {
977 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
978 (static_cast<unsigned>(CC) << 1));
981 /// getAttributes - Return the parameter attributes for this call.
983 const AttrListPtr &getAttributes() const { return AttributeList; }
985 /// setAttributes - Set the parameter attributes for this call.
987 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
989 /// addAttribute - adds the attribute to the list of attributes.
990 void addAttribute(unsigned i, Attributes attr);
992 /// removeAttribute - removes the attribute from the list of attributes.
993 void removeAttribute(unsigned i, Attributes attr);
995 /// @brief Determine whether the call or the callee has the given attribute.
996 bool paramHasAttr(unsigned i, Attributes attr) const;
998 /// @brief Extract the alignment for a call or parameter (0=unknown).
999 unsigned getParamAlignment(unsigned i) const {
1000 return AttributeList.getParamAlignment(i);
1003 /// @brief Return true if the call should not be inlined.
1004 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
1005 void setIsNoInline(bool Value = true) {
1006 if (Value) addAttribute(~0, Attribute::NoInline);
1007 else removeAttribute(~0, Attribute::NoInline);
1010 /// @brief Determine if the call does not access memory.
1011 bool doesNotAccessMemory() const {
1012 return paramHasAttr(~0, Attribute::ReadNone);
1014 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1015 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1016 else removeAttribute(~0, Attribute::ReadNone);
1019 /// @brief Determine if the call does not access or only reads memory.
1020 bool onlyReadsMemory() const {
1021 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1023 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1024 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1025 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1028 /// @brief Determine if the call cannot return.
1029 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
1030 void setDoesNotReturn(bool DoesNotReturn = true) {
1031 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1032 else removeAttribute(~0, Attribute::NoReturn);
1035 /// @brief Determine if the call cannot unwind.
1036 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
1037 void setDoesNotThrow(bool DoesNotThrow = true) {
1038 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1039 else removeAttribute(~0, Attribute::NoUnwind);
1042 /// @brief Determine if the call returns a structure through first
1043 /// pointer argument.
1044 bool hasStructRetAttr() const {
1045 // Be friendly and also check the callee.
1046 return paramHasAttr(1, Attribute::StructRet);
1049 /// @brief Determine if any call argument is an aggregate passed by value.
1050 bool hasByValArgument() const {
1051 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1054 /// getCalledFunction - Return the function called, or null if this is an
1055 /// indirect function invocation.
1057 Function *getCalledFunction() const {
1058 return dyn_cast<Function>(Op<-1>());
1061 /// getCalledValue - Get a pointer to the function that is invoked by this
1063 const Value *getCalledValue() const { return Op<-1>(); }
1064 Value *getCalledValue() { return Op<-1>(); }
1066 /// setCalledFunction - Set the function called.
1067 void setCalledFunction(Value* Fn) {
1071 // Methods for support type inquiry through isa, cast, and dyn_cast:
1072 static inline bool classof(const CallInst *) { return true; }
1073 static inline bool classof(const Instruction *I) {
1074 return I->getOpcode() == Instruction::Call;
1076 static inline bool classof(const Value *V) {
1077 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1080 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1081 // method so that subclasses cannot accidentally use it.
1082 void setInstructionSubclassData(unsigned short D) {
1083 Instruction::setInstructionSubclassData(D);
1088 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1091 template<typename InputIterator>
1092 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1093 const Twine &NameStr, BasicBlock *InsertAtEnd)
1094 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1095 ->getElementType())->getReturnType(),
1097 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1098 unsigned(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1099 init(Func, ArgBegin, ArgEnd, NameStr,
1100 typename std::iterator_traits<InputIterator>::iterator_category());
1103 template<typename InputIterator>
1104 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1105 const Twine &NameStr, Instruction *InsertBefore)
1106 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1107 ->getElementType())->getReturnType(),
1109 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1110 unsigned(ArgEnd - ArgBegin + 1), InsertBefore) {
1111 init(Func, ArgBegin, ArgEnd, NameStr,
1112 typename std::iterator_traits<InputIterator>::iterator_category());
1116 // Note: if you get compile errors about private methods then
1117 // please update your code to use the high-level operand
1118 // interfaces. See line 943 above.
1119 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1121 //===----------------------------------------------------------------------===//
1123 //===----------------------------------------------------------------------===//
1125 /// SelectInst - This class represents the LLVM 'select' instruction.
1127 class SelectInst : public Instruction {
1128 void init(Value *C, Value *S1, Value *S2) {
1129 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1135 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1136 Instruction *InsertBefore)
1137 : Instruction(S1->getType(), Instruction::Select,
1138 &Op<0>(), 3, InsertBefore) {
1142 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1143 BasicBlock *InsertAtEnd)
1144 : Instruction(S1->getType(), Instruction::Select,
1145 &Op<0>(), 3, InsertAtEnd) {
1150 virtual SelectInst *clone_impl() const;
1152 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1153 const Twine &NameStr = "",
1154 Instruction *InsertBefore = 0) {
1155 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1157 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1158 const Twine &NameStr,
1159 BasicBlock *InsertAtEnd) {
1160 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1163 const Value *getCondition() const { return Op<0>(); }
1164 const Value *getTrueValue() const { return Op<1>(); }
1165 const Value *getFalseValue() const { return Op<2>(); }
1166 Value *getCondition() { return Op<0>(); }
1167 Value *getTrueValue() { return Op<1>(); }
1168 Value *getFalseValue() { return Op<2>(); }
1170 /// areInvalidOperands - Return a string if the specified operands are invalid
1171 /// for a select operation, otherwise return null.
1172 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1174 /// Transparently provide more efficient getOperand methods.
1175 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1177 OtherOps getOpcode() const {
1178 return static_cast<OtherOps>(Instruction::getOpcode());
1181 // Methods for support type inquiry through isa, cast, and dyn_cast:
1182 static inline bool classof(const SelectInst *) { return true; }
1183 static inline bool classof(const Instruction *I) {
1184 return I->getOpcode() == Instruction::Select;
1186 static inline bool classof(const Value *V) {
1187 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1192 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1195 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1197 //===----------------------------------------------------------------------===//
1199 //===----------------------------------------------------------------------===//
1201 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1202 /// an argument of the specified type given a va_list and increments that list
1204 class VAArgInst : public UnaryInstruction {
1206 virtual VAArgInst *clone_impl() const;
1209 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1210 Instruction *InsertBefore = 0)
1211 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1214 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1215 BasicBlock *InsertAtEnd)
1216 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1220 // Methods for support type inquiry through isa, cast, and dyn_cast:
1221 static inline bool classof(const VAArgInst *) { return true; }
1222 static inline bool classof(const Instruction *I) {
1223 return I->getOpcode() == VAArg;
1225 static inline bool classof(const Value *V) {
1226 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1230 //===----------------------------------------------------------------------===//
1231 // ExtractElementInst Class
1232 //===----------------------------------------------------------------------===//
1234 /// ExtractElementInst - This instruction extracts a single (scalar)
1235 /// element from a VectorType value
1237 class ExtractElementInst : public Instruction {
1238 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1239 Instruction *InsertBefore = 0);
1240 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1241 BasicBlock *InsertAtEnd);
1243 virtual ExtractElementInst *clone_impl() const;
1246 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1247 const Twine &NameStr = "",
1248 Instruction *InsertBefore = 0) {
1249 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1251 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1252 const Twine &NameStr,
1253 BasicBlock *InsertAtEnd) {
1254 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1257 /// isValidOperands - Return true if an extractelement instruction can be
1258 /// formed with the specified operands.
1259 static bool isValidOperands(const Value *Vec, const Value *Idx);
1261 Value *getVectorOperand() { return Op<0>(); }
1262 Value *getIndexOperand() { return Op<1>(); }
1263 const Value *getVectorOperand() const { return Op<0>(); }
1264 const Value *getIndexOperand() const { return Op<1>(); }
1266 const VectorType *getVectorOperandType() const {
1267 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1271 /// Transparently provide more efficient getOperand methods.
1272 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1274 // Methods for support type inquiry through isa, cast, and dyn_cast:
1275 static inline bool classof(const ExtractElementInst *) { return true; }
1276 static inline bool classof(const Instruction *I) {
1277 return I->getOpcode() == Instruction::ExtractElement;
1279 static inline bool classof(const Value *V) {
1280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1285 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1288 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1290 //===----------------------------------------------------------------------===//
1291 // InsertElementInst Class
1292 //===----------------------------------------------------------------------===//
1294 /// InsertElementInst - This instruction inserts a single (scalar)
1295 /// element into a VectorType value
1297 class InsertElementInst : public Instruction {
1298 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1299 const Twine &NameStr = "",
1300 Instruction *InsertBefore = 0);
1301 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1302 const Twine &NameStr, BasicBlock *InsertAtEnd);
1304 virtual InsertElementInst *clone_impl() const;
1307 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1308 const Twine &NameStr = "",
1309 Instruction *InsertBefore = 0) {
1310 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1312 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1313 const Twine &NameStr,
1314 BasicBlock *InsertAtEnd) {
1315 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1318 /// isValidOperands - Return true if an insertelement instruction can be
1319 /// formed with the specified operands.
1320 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1323 /// getType - Overload to return most specific vector type.
1325 const VectorType *getType() const {
1326 return reinterpret_cast<const VectorType*>(Instruction::getType());
1329 /// Transparently provide more efficient getOperand methods.
1330 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1332 // Methods for support type inquiry through isa, cast, and dyn_cast:
1333 static inline bool classof(const InsertElementInst *) { return true; }
1334 static inline bool classof(const Instruction *I) {
1335 return I->getOpcode() == Instruction::InsertElement;
1337 static inline bool classof(const Value *V) {
1338 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1343 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1346 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1348 //===----------------------------------------------------------------------===//
1349 // ShuffleVectorInst Class
1350 //===----------------------------------------------------------------------===//
1352 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1355 class ShuffleVectorInst : public Instruction {
1357 virtual ShuffleVectorInst *clone_impl() const;
1360 // allocate space for exactly three operands
1361 void *operator new(size_t s) {
1362 return User::operator new(s, 3);
1364 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1365 const Twine &NameStr = "",
1366 Instruction *InsertBefor = 0);
1367 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1368 const Twine &NameStr, BasicBlock *InsertAtEnd);
1370 /// isValidOperands - Return true if a shufflevector instruction can be
1371 /// formed with the specified operands.
1372 static bool isValidOperands(const Value *V1, const Value *V2,
1375 /// getType - Overload to return most specific vector type.
1377 const VectorType *getType() const {
1378 return reinterpret_cast<const VectorType*>(Instruction::getType());
1381 /// Transparently provide more efficient getOperand methods.
1382 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1384 /// getMaskValue - Return the index from the shuffle mask for the specified
1385 /// output result. This is either -1 if the element is undef or a number less
1386 /// than 2*numelements.
1387 int getMaskValue(unsigned i) const;
1389 // Methods for support type inquiry through isa, cast, and dyn_cast:
1390 static inline bool classof(const ShuffleVectorInst *) { return true; }
1391 static inline bool classof(const Instruction *I) {
1392 return I->getOpcode() == Instruction::ShuffleVector;
1394 static inline bool classof(const Value *V) {
1395 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1400 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1403 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1405 //===----------------------------------------------------------------------===//
1406 // ExtractValueInst Class
1407 //===----------------------------------------------------------------------===//
1409 /// ExtractValueInst - This instruction extracts a struct member or array
1410 /// element value from an aggregate value.
1412 class ExtractValueInst : public UnaryInstruction {
1413 SmallVector<unsigned, 4> Indices;
1415 ExtractValueInst(const ExtractValueInst &EVI);
1416 void init(const unsigned *Idx, unsigned NumIdx,
1417 const Twine &NameStr);
1418 void init(unsigned Idx, const Twine &NameStr);
1420 template<typename InputIterator>
1421 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1422 const Twine &NameStr,
1423 // This argument ensures that we have an iterator we can
1424 // do arithmetic on in constant time
1425 std::random_access_iterator_tag) {
1426 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1428 // There's no fundamental reason why we require at least one index
1429 // (other than weirdness with &*IdxBegin being invalid; see
1430 // getelementptr's init routine for example). But there's no
1431 // present need to support it.
1432 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1434 // This requires that the iterator points to contiguous memory.
1435 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1436 // we have to build an array here
1439 /// getIndexedType - Returns the type of the element that would be extracted
1440 /// with an extractvalue instruction with the specified parameters.
1442 /// Null is returned if the indices are invalid for the specified
1445 static const Type *getIndexedType(const Type *Agg,
1446 const unsigned *Idx, unsigned NumIdx);
1448 template<typename InputIterator>
1449 static const Type *getIndexedType(const Type *Ptr,
1450 InputIterator IdxBegin,
1451 InputIterator IdxEnd,
1452 // This argument ensures that we
1453 // have an iterator we can do
1454 // arithmetic on in constant time
1455 std::random_access_iterator_tag) {
1456 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1459 // This requires that the iterator points to contiguous memory.
1460 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1462 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1465 /// Constructors - Create a extractvalue instruction with a base aggregate
1466 /// value and a list of indices. The first ctor can optionally insert before
1467 /// an existing instruction, the second appends the new instruction to the
1468 /// specified BasicBlock.
1469 template<typename InputIterator>
1470 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1471 InputIterator IdxEnd,
1472 const Twine &NameStr,
1473 Instruction *InsertBefore);
1474 template<typename InputIterator>
1475 inline ExtractValueInst(Value *Agg,
1476 InputIterator IdxBegin, InputIterator IdxEnd,
1477 const Twine &NameStr, BasicBlock *InsertAtEnd);
1479 // allocate space for exactly one operand
1480 void *operator new(size_t s) {
1481 return User::operator new(s, 1);
1484 virtual ExtractValueInst *clone_impl() const;
1487 template<typename InputIterator>
1488 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1489 InputIterator IdxEnd,
1490 const Twine &NameStr = "",
1491 Instruction *InsertBefore = 0) {
1493 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1495 template<typename InputIterator>
1496 static ExtractValueInst *Create(Value *Agg,
1497 InputIterator IdxBegin, InputIterator IdxEnd,
1498 const Twine &NameStr,
1499 BasicBlock *InsertAtEnd) {
1500 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1503 /// Constructors - These two creators are convenience methods because one
1504 /// index extractvalue instructions are much more common than those with
1506 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1507 const Twine &NameStr = "",
1508 Instruction *InsertBefore = 0) {
1509 unsigned Idxs[1] = { Idx };
1510 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1512 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1513 const Twine &NameStr,
1514 BasicBlock *InsertAtEnd) {
1515 unsigned Idxs[1] = { Idx };
1516 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1519 /// getIndexedType - Returns the type of the element that would be extracted
1520 /// with an extractvalue instruction with the specified parameters.
1522 /// Null is returned if the indices are invalid for the specified
1525 template<typename InputIterator>
1526 static const Type *getIndexedType(const Type *Ptr,
1527 InputIterator IdxBegin,
1528 InputIterator IdxEnd) {
1529 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1530 typename std::iterator_traits<InputIterator>::
1531 iterator_category());
1533 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1535 typedef const unsigned* idx_iterator;
1536 inline idx_iterator idx_begin() const { return Indices.begin(); }
1537 inline idx_iterator idx_end() const { return Indices.end(); }
1539 Value *getAggregateOperand() {
1540 return getOperand(0);
1542 const Value *getAggregateOperand() const {
1543 return getOperand(0);
1545 static unsigned getAggregateOperandIndex() {
1546 return 0U; // get index for modifying correct operand
1549 unsigned getNumIndices() const { // Note: always non-negative
1550 return (unsigned)Indices.size();
1553 bool hasIndices() const {
1557 // Methods for support type inquiry through isa, cast, and dyn_cast:
1558 static inline bool classof(const ExtractValueInst *) { return true; }
1559 static inline bool classof(const Instruction *I) {
1560 return I->getOpcode() == Instruction::ExtractValue;
1562 static inline bool classof(const Value *V) {
1563 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1567 template<typename InputIterator>
1568 ExtractValueInst::ExtractValueInst(Value *Agg,
1569 InputIterator IdxBegin,
1570 InputIterator IdxEnd,
1571 const Twine &NameStr,
1572 Instruction *InsertBefore)
1573 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1575 ExtractValue, Agg, InsertBefore) {
1576 init(IdxBegin, IdxEnd, NameStr,
1577 typename std::iterator_traits<InputIterator>::iterator_category());
1579 template<typename InputIterator>
1580 ExtractValueInst::ExtractValueInst(Value *Agg,
1581 InputIterator IdxBegin,
1582 InputIterator IdxEnd,
1583 const Twine &NameStr,
1584 BasicBlock *InsertAtEnd)
1585 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1587 ExtractValue, Agg, InsertAtEnd) {
1588 init(IdxBegin, IdxEnd, NameStr,
1589 typename std::iterator_traits<InputIterator>::iterator_category());
1593 //===----------------------------------------------------------------------===//
1594 // InsertValueInst Class
1595 //===----------------------------------------------------------------------===//
1597 /// InsertValueInst - This instruction inserts a struct field of array element
1598 /// value into an aggregate value.
1600 class InsertValueInst : public Instruction {
1601 SmallVector<unsigned, 4> Indices;
1603 void *operator new(size_t, unsigned); // Do not implement
1604 InsertValueInst(const InsertValueInst &IVI);
1605 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1606 const Twine &NameStr);
1607 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1609 template<typename InputIterator>
1610 void init(Value *Agg, Value *Val,
1611 InputIterator IdxBegin, InputIterator IdxEnd,
1612 const Twine &NameStr,
1613 // This argument ensures that we have an iterator we can
1614 // do arithmetic on in constant time
1615 std::random_access_iterator_tag) {
1616 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1618 // There's no fundamental reason why we require at least one index
1619 // (other than weirdness with &*IdxBegin being invalid; see
1620 // getelementptr's init routine for example). But there's no
1621 // present need to support it.
1622 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1624 // This requires that the iterator points to contiguous memory.
1625 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1626 // we have to build an array here
1629 /// Constructors - Create a insertvalue instruction with a base aggregate
1630 /// value, a value to insert, and a list of indices. The first ctor can
1631 /// optionally insert before an existing instruction, the second appends
1632 /// the new instruction to the specified BasicBlock.
1633 template<typename InputIterator>
1634 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1635 InputIterator IdxEnd,
1636 const Twine &NameStr,
1637 Instruction *InsertBefore);
1638 template<typename InputIterator>
1639 inline InsertValueInst(Value *Agg, Value *Val,
1640 InputIterator IdxBegin, InputIterator IdxEnd,
1641 const Twine &NameStr, BasicBlock *InsertAtEnd);
1643 /// Constructors - These two constructors are convenience methods because one
1644 /// and two index insertvalue instructions are so common.
1645 InsertValueInst(Value *Agg, Value *Val,
1646 unsigned Idx, const Twine &NameStr = "",
1647 Instruction *InsertBefore = 0);
1648 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1649 const Twine &NameStr, BasicBlock *InsertAtEnd);
1651 virtual InsertValueInst *clone_impl() const;
1653 // allocate space for exactly two operands
1654 void *operator new(size_t s) {
1655 return User::operator new(s, 2);
1658 template<typename InputIterator>
1659 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1660 InputIterator IdxEnd,
1661 const Twine &NameStr = "",
1662 Instruction *InsertBefore = 0) {
1663 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1664 NameStr, InsertBefore);
1666 template<typename InputIterator>
1667 static InsertValueInst *Create(Value *Agg, Value *Val,
1668 InputIterator IdxBegin, InputIterator IdxEnd,
1669 const Twine &NameStr,
1670 BasicBlock *InsertAtEnd) {
1671 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1672 NameStr, InsertAtEnd);
1675 /// Constructors - These two creators are convenience methods because one
1676 /// index insertvalue instructions are much more common than those with
1678 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1679 const Twine &NameStr = "",
1680 Instruction *InsertBefore = 0) {
1681 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1683 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1684 const Twine &NameStr,
1685 BasicBlock *InsertAtEnd) {
1686 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1689 /// Transparently provide more efficient getOperand methods.
1690 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1692 typedef const unsigned* idx_iterator;
1693 inline idx_iterator idx_begin() const { return Indices.begin(); }
1694 inline idx_iterator idx_end() const { return Indices.end(); }
1696 Value *getAggregateOperand() {
1697 return getOperand(0);
1699 const Value *getAggregateOperand() const {
1700 return getOperand(0);
1702 static unsigned getAggregateOperandIndex() {
1703 return 0U; // get index for modifying correct operand
1706 Value *getInsertedValueOperand() {
1707 return getOperand(1);
1709 const Value *getInsertedValueOperand() const {
1710 return getOperand(1);
1712 static unsigned getInsertedValueOperandIndex() {
1713 return 1U; // get index for modifying correct operand
1716 unsigned getNumIndices() const { // Note: always non-negative
1717 return (unsigned)Indices.size();
1720 bool hasIndices() const {
1724 // Methods for support type inquiry through isa, cast, and dyn_cast:
1725 static inline bool classof(const InsertValueInst *) { return true; }
1726 static inline bool classof(const Instruction *I) {
1727 return I->getOpcode() == Instruction::InsertValue;
1729 static inline bool classof(const Value *V) {
1730 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1735 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1738 template<typename InputIterator>
1739 InsertValueInst::InsertValueInst(Value *Agg,
1741 InputIterator IdxBegin,
1742 InputIterator IdxEnd,
1743 const Twine &NameStr,
1744 Instruction *InsertBefore)
1745 : Instruction(Agg->getType(), InsertValue,
1746 OperandTraits<InsertValueInst>::op_begin(this),
1748 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1749 typename std::iterator_traits<InputIterator>::iterator_category());
1751 template<typename InputIterator>
1752 InsertValueInst::InsertValueInst(Value *Agg,
1754 InputIterator IdxBegin,
1755 InputIterator IdxEnd,
1756 const Twine &NameStr,
1757 BasicBlock *InsertAtEnd)
1758 : Instruction(Agg->getType(), InsertValue,
1759 OperandTraits<InsertValueInst>::op_begin(this),
1761 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1762 typename std::iterator_traits<InputIterator>::iterator_category());
1765 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1767 //===----------------------------------------------------------------------===//
1769 //===----------------------------------------------------------------------===//
1771 // PHINode - The PHINode class is used to represent the magical mystical PHI
1772 // node, that can not exist in nature, but can be synthesized in a computer
1773 // scientist's overactive imagination.
1775 class PHINode : public Instruction {
1776 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1777 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1778 /// the number actually in use.
1779 unsigned ReservedSpace;
1780 PHINode(const PHINode &PN);
1781 // allocate space for exactly zero operands
1782 void *operator new(size_t s) {
1783 return User::operator new(s, 0);
1785 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1786 Instruction *InsertBefore = 0)
1787 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1792 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1793 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1798 virtual PHINode *clone_impl() const;
1800 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1801 Instruction *InsertBefore = 0) {
1802 return new PHINode(Ty, NameStr, InsertBefore);
1804 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1805 BasicBlock *InsertAtEnd) {
1806 return new PHINode(Ty, NameStr, InsertAtEnd);
1810 /// reserveOperandSpace - This method can be used to avoid repeated
1811 /// reallocation of PHI operand lists by reserving space for the correct
1812 /// number of operands before adding them. Unlike normal vector reserves,
1813 /// this method can also be used to trim the operand space.
1814 void reserveOperandSpace(unsigned NumValues) {
1815 resizeOperands(NumValues*2);
1818 /// Provide fast operand accessors
1819 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1821 /// getNumIncomingValues - Return the number of incoming edges
1823 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1825 /// getIncomingValue - Return incoming value number x
1827 Value *getIncomingValue(unsigned i) const {
1828 assert(i*2 < getNumOperands() && "Invalid value number!");
1829 return getOperand(i*2);
1831 void setIncomingValue(unsigned i, Value *V) {
1832 assert(i*2 < getNumOperands() && "Invalid value number!");
1835 static unsigned getOperandNumForIncomingValue(unsigned i) {
1838 static unsigned getIncomingValueNumForOperand(unsigned i) {
1839 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1843 /// getIncomingBlock - Return incoming basic block number @p i.
1845 BasicBlock *getIncomingBlock(unsigned i) const {
1846 return cast<BasicBlock>(getOperand(i*2+1));
1849 /// getIncomingBlock - Return incoming basic block corresponding
1850 /// to an operand of the PHI.
1852 BasicBlock *getIncomingBlock(const Use &U) const {
1853 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1854 return cast<BasicBlock>((&U + 1)->get());
1857 /// getIncomingBlock - Return incoming basic block corresponding
1858 /// to value use iterator.
1860 template <typename U>
1861 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1862 return getIncomingBlock(I.getUse());
1866 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1867 setOperand(i*2+1, (Value*)BB);
1869 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1872 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1873 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1877 /// addIncoming - Add an incoming value to the end of the PHI list
1879 void addIncoming(Value *V, BasicBlock *BB) {
1880 assert(V && "PHI node got a null value!");
1881 assert(BB && "PHI node got a null basic block!");
1882 assert(getType() == V->getType() &&
1883 "All operands to PHI node must be the same type as the PHI node!");
1884 unsigned OpNo = NumOperands;
1885 if (OpNo+2 > ReservedSpace)
1886 resizeOperands(0); // Get more space!
1887 // Initialize some new operands.
1888 NumOperands = OpNo+2;
1889 OperandList[OpNo] = V;
1890 OperandList[OpNo+1] = (Value*)BB;
1893 /// removeIncomingValue - Remove an incoming value. This is useful if a
1894 /// predecessor basic block is deleted. The value removed is returned.
1896 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1897 /// is true), the PHI node is destroyed and any uses of it are replaced with
1898 /// dummy values. The only time there should be zero incoming values to a PHI
1899 /// node is when the block is dead, so this strategy is sound.
1901 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1903 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1904 int Idx = getBasicBlockIndex(BB);
1905 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1906 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1909 /// getBasicBlockIndex - Return the first index of the specified basic
1910 /// block in the value list for this PHI. Returns -1 if no instance.
1912 int getBasicBlockIndex(const BasicBlock *BB) const {
1913 Use *OL = OperandList;
1914 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1915 if (OL[i+1].get() == (const Value*)BB) return i/2;
1919 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1920 return getIncomingValue(getBasicBlockIndex(BB));
1923 /// hasConstantValue - If the specified PHI node always merges together the
1924 /// same value, return the value, otherwise return null.
1926 /// If the PHI has undef operands, but all the rest of the operands are
1927 /// some unique value, return that value if it can be proved that the
1928 /// value dominates the PHI. If DT is null, use a conservative check,
1929 /// otherwise use DT to test for dominance.
1931 Value *hasConstantValue(DominatorTree *DT = 0) const;
1933 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1934 static inline bool classof(const PHINode *) { return true; }
1935 static inline bool classof(const Instruction *I) {
1936 return I->getOpcode() == Instruction::PHI;
1938 static inline bool classof(const Value *V) {
1939 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1942 void resizeOperands(unsigned NumOperands);
1946 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1949 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1952 //===----------------------------------------------------------------------===//
1954 //===----------------------------------------------------------------------===//
1956 //===---------------------------------------------------------------------------
1957 /// ReturnInst - Return a value (possibly void), from a function. Execution
1958 /// does not continue in this function any longer.
1960 class ReturnInst : public TerminatorInst {
1961 ReturnInst(const ReturnInst &RI);
1964 // ReturnInst constructors:
1965 // ReturnInst() - 'ret void' instruction
1966 // ReturnInst( null) - 'ret void' instruction
1967 // ReturnInst(Value* X) - 'ret X' instruction
1968 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1969 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1970 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1971 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1973 // NOTE: If the Value* passed is of type void then the constructor behaves as
1974 // if it was passed NULL.
1975 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1976 Instruction *InsertBefore = 0);
1977 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1978 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1980 virtual ReturnInst *clone_impl() const;
1982 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1983 Instruction *InsertBefore = 0) {
1984 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1986 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1987 BasicBlock *InsertAtEnd) {
1988 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1990 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1991 return new(0) ReturnInst(C, InsertAtEnd);
1993 virtual ~ReturnInst();
1995 /// Provide fast operand accessors
1996 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1998 /// Convenience accessor
1999 Value *getReturnValue(unsigned n = 0) const {
2000 return n < getNumOperands()
2005 unsigned getNumSuccessors() const { return 0; }
2007 // Methods for support type inquiry through isa, cast, and dyn_cast:
2008 static inline bool classof(const ReturnInst *) { return true; }
2009 static inline bool classof(const Instruction *I) {
2010 return (I->getOpcode() == Instruction::Ret);
2012 static inline bool classof(const Value *V) {
2013 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2016 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2017 virtual unsigned getNumSuccessorsV() const;
2018 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2022 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<> {
2025 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2027 //===----------------------------------------------------------------------===//
2029 //===----------------------------------------------------------------------===//
2031 //===---------------------------------------------------------------------------
2032 /// BranchInst - Conditional or Unconditional Branch instruction.
2034 class BranchInst : public TerminatorInst {
2035 /// Ops list - Branches are strange. The operands are ordered:
2036 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2037 /// they don't have to check for cond/uncond branchness. These are mostly
2038 /// accessed relative from op_end().
2039 BranchInst(const BranchInst &BI);
2041 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2042 // BranchInst(BB *B) - 'br B'
2043 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2044 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2045 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2046 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2047 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2048 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2049 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2050 Instruction *InsertBefore = 0);
2051 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2052 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2053 BasicBlock *InsertAtEnd);
2055 virtual BranchInst *clone_impl() const;
2057 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2058 return new(1, true) BranchInst(IfTrue, InsertBefore);
2060 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2061 Value *Cond, Instruction *InsertBefore = 0) {
2062 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2064 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2065 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2067 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2068 Value *Cond, BasicBlock *InsertAtEnd) {
2069 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2074 /// Transparently provide more efficient getOperand methods.
2075 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2077 bool isUnconditional() const { return getNumOperands() == 1; }
2078 bool isConditional() const { return getNumOperands() == 3; }
2080 Value *getCondition() const {
2081 assert(isConditional() && "Cannot get condition of an uncond branch!");
2085 void setCondition(Value *V) {
2086 assert(isConditional() && "Cannot set condition of unconditional branch!");
2090 // setUnconditionalDest - Change the current branch to an unconditional branch
2091 // targeting the specified block.
2092 // FIXME: Eliminate this ugly method.
2093 void setUnconditionalDest(BasicBlock *Dest) {
2094 Op<-1>() = (Value*)Dest;
2095 if (isConditional()) { // Convert this to an uncond branch.
2099 OperandList = op_begin();
2103 unsigned getNumSuccessors() const { return 1+isConditional(); }
2105 BasicBlock *getSuccessor(unsigned i) const {
2106 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2107 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2110 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2111 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2112 *(&Op<-1>() - idx) = (Value*)NewSucc;
2115 // Methods for support type inquiry through isa, cast, and dyn_cast:
2116 static inline bool classof(const BranchInst *) { return true; }
2117 static inline bool classof(const Instruction *I) {
2118 return (I->getOpcode() == Instruction::Br);
2120 static inline bool classof(const Value *V) {
2121 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2124 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2125 virtual unsigned getNumSuccessorsV() const;
2126 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2130 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2132 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2134 //===----------------------------------------------------------------------===//
2136 //===----------------------------------------------------------------------===//
2138 //===---------------------------------------------------------------------------
2139 /// SwitchInst - Multiway switch
2141 class SwitchInst : public TerminatorInst {
2142 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2143 unsigned ReservedSpace;
2144 // Operand[0] = Value to switch on
2145 // Operand[1] = Default basic block destination
2146 // Operand[2n ] = Value to match
2147 // Operand[2n+1] = BasicBlock to go to on match
2148 SwitchInst(const SwitchInst &SI);
2149 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2150 void resizeOperands(unsigned No);
2151 // allocate space for exactly zero operands
2152 void *operator new(size_t s) {
2153 return User::operator new(s, 0);
2155 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2156 /// switch on and a default destination. The number of additional cases can
2157 /// be specified here to make memory allocation more efficient. This
2158 /// constructor can also autoinsert before another instruction.
2159 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2160 Instruction *InsertBefore);
2162 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2163 /// switch on and a default destination. The number of additional cases can
2164 /// be specified here to make memory allocation more efficient. This
2165 /// constructor also autoinserts at the end of the specified BasicBlock.
2166 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2167 BasicBlock *InsertAtEnd);
2169 virtual SwitchInst *clone_impl() const;
2171 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2172 unsigned NumCases, Instruction *InsertBefore = 0) {
2173 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2175 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2176 unsigned NumCases, BasicBlock *InsertAtEnd) {
2177 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2181 /// Provide fast operand accessors
2182 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2184 // Accessor Methods for Switch stmt
2185 Value *getCondition() const { return getOperand(0); }
2186 void setCondition(Value *V) { setOperand(0, V); }
2188 BasicBlock *getDefaultDest() const {
2189 return cast<BasicBlock>(getOperand(1));
2192 /// getNumCases - return the number of 'cases' in this switch instruction.
2193 /// Note that case #0 is always the default case.
2194 unsigned getNumCases() const {
2195 return getNumOperands()/2;
2198 /// getCaseValue - Return the specified case value. Note that case #0, the
2199 /// default destination, does not have a case value.
2200 ConstantInt *getCaseValue(unsigned i) {
2201 assert(i && i < getNumCases() && "Illegal case value to get!");
2202 return getSuccessorValue(i);
2205 /// getCaseValue - Return the specified case value. Note that case #0, the
2206 /// default destination, does not have a case value.
2207 const ConstantInt *getCaseValue(unsigned i) const {
2208 assert(i && i < getNumCases() && "Illegal case value to get!");
2209 return getSuccessorValue(i);
2212 /// findCaseValue - Search all of the case values for the specified constant.
2213 /// If it is explicitly handled, return the case number of it, otherwise
2214 /// return 0 to indicate that it is handled by the default handler.
2215 unsigned findCaseValue(const ConstantInt *C) const {
2216 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2217 if (getCaseValue(i) == C)
2222 /// findCaseDest - Finds the unique case value for a given successor. Returns
2223 /// null if the successor is not found, not unique, or is the default case.
2224 ConstantInt *findCaseDest(BasicBlock *BB) {
2225 if (BB == getDefaultDest()) return NULL;
2227 ConstantInt *CI = NULL;
2228 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2229 if (getSuccessor(i) == BB) {
2230 if (CI) return NULL; // Multiple cases lead to BB.
2231 else CI = getCaseValue(i);
2237 /// addCase - Add an entry to the switch instruction...
2239 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2241 /// removeCase - This method removes the specified successor from the switch
2242 /// instruction. Note that this cannot be used to remove the default
2243 /// destination (successor #0).
2245 void removeCase(unsigned idx);
2247 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2248 BasicBlock *getSuccessor(unsigned idx) const {
2249 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2250 return cast<BasicBlock>(getOperand(idx*2+1));
2252 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2253 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2254 setOperand(idx*2+1, (Value*)NewSucc);
2257 // getSuccessorValue - Return the value associated with the specified
2259 ConstantInt *getSuccessorValue(unsigned idx) const {
2260 assert(idx < getNumSuccessors() && "Successor # out of range!");
2261 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2264 // Methods for support type inquiry through isa, cast, and dyn_cast:
2265 static inline bool classof(const SwitchInst *) { return true; }
2266 static inline bool classof(const Instruction *I) {
2267 return I->getOpcode() == Instruction::Switch;
2269 static inline bool classof(const Value *V) {
2270 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2273 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2274 virtual unsigned getNumSuccessorsV() const;
2275 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2279 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2282 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2285 //===----------------------------------------------------------------------===//
2286 // IndirectBrInst Class
2287 //===----------------------------------------------------------------------===//
2289 //===---------------------------------------------------------------------------
2290 /// IndirectBrInst - Indirect Branch Instruction.
2292 class IndirectBrInst : public TerminatorInst {
2293 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2294 unsigned ReservedSpace;
2295 // Operand[0] = Value to switch on
2296 // Operand[1] = Default basic block destination
2297 // Operand[2n ] = Value to match
2298 // Operand[2n+1] = BasicBlock to go to on match
2299 IndirectBrInst(const IndirectBrInst &IBI);
2300 void init(Value *Address, unsigned NumDests);
2301 void resizeOperands(unsigned No);
2302 // allocate space for exactly zero operands
2303 void *operator new(size_t s) {
2304 return User::operator new(s, 0);
2306 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2307 /// Address to jump to. The number of expected destinations can be specified
2308 /// here to make memory allocation more efficient. This constructor can also
2309 /// autoinsert before another instruction.
2310 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2312 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2313 /// Address to jump to. The number of expected destinations can be specified
2314 /// here to make memory allocation more efficient. This constructor also
2315 /// autoinserts at the end of the specified BasicBlock.
2316 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2318 virtual IndirectBrInst *clone_impl() const;
2320 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2321 Instruction *InsertBefore = 0) {
2322 return new IndirectBrInst(Address, NumDests, InsertBefore);
2324 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2325 BasicBlock *InsertAtEnd) {
2326 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2330 /// Provide fast operand accessors.
2331 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2333 // Accessor Methods for IndirectBrInst instruction.
2334 Value *getAddress() { return getOperand(0); }
2335 const Value *getAddress() const { return getOperand(0); }
2336 void setAddress(Value *V) { setOperand(0, V); }
2339 /// getNumDestinations - return the number of possible destinations in this
2340 /// indirectbr instruction.
2341 unsigned getNumDestinations() const { return getNumOperands()-1; }
2343 /// getDestination - Return the specified destination.
2344 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2345 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2347 /// addDestination - Add a destination.
2349 void addDestination(BasicBlock *Dest);
2351 /// removeDestination - This method removes the specified successor from the
2352 /// indirectbr instruction.
2353 void removeDestination(unsigned i);
2355 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2356 BasicBlock *getSuccessor(unsigned i) const {
2357 return cast<BasicBlock>(getOperand(i+1));
2359 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2360 setOperand(i+1, (Value*)NewSucc);
2363 // Methods for support type inquiry through isa, cast, and dyn_cast:
2364 static inline bool classof(const IndirectBrInst *) { return true; }
2365 static inline bool classof(const Instruction *I) {
2366 return I->getOpcode() == Instruction::IndirectBr;
2368 static inline bool classof(const Value *V) {
2369 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2372 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2373 virtual unsigned getNumSuccessorsV() const;
2374 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2378 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2381 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2384 //===----------------------------------------------------------------------===//
2386 //===----------------------------------------------------------------------===//
2388 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2389 /// calling convention of the call.
2391 class InvokeInst : public TerminatorInst {
2392 AttrListPtr AttributeList;
2393 InvokeInst(const InvokeInst &BI);
2394 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2395 Value* const *Args, unsigned NumArgs);
2397 template<typename InputIterator>
2398 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2399 InputIterator ArgBegin, InputIterator ArgEnd,
2400 const Twine &NameStr,
2401 // This argument ensures that we have an iterator we can
2402 // do arithmetic on in constant time
2403 std::random_access_iterator_tag) {
2404 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2406 // This requires that the iterator points to contiguous memory.
2407 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2411 /// Construct an InvokeInst given a range of arguments.
2412 /// InputIterator must be a random-access iterator pointing to
2413 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2414 /// made for random-accessness but not for contiguous storage as
2415 /// that would incur runtime overhead.
2417 /// @brief Construct an InvokeInst from a range of arguments
2418 template<typename InputIterator>
2419 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2420 InputIterator ArgBegin, InputIterator ArgEnd,
2422 const Twine &NameStr, Instruction *InsertBefore);
2424 /// Construct an InvokeInst given a range of arguments.
2425 /// InputIterator must be a random-access iterator pointing to
2426 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2427 /// made for random-accessness but not for contiguous storage as
2428 /// that would incur runtime overhead.
2430 /// @brief Construct an InvokeInst from a range of arguments
2431 template<typename InputIterator>
2432 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2433 InputIterator ArgBegin, InputIterator ArgEnd,
2435 const Twine &NameStr, BasicBlock *InsertAtEnd);
2437 virtual InvokeInst *clone_impl() const;
2439 template<typename InputIterator>
2440 static InvokeInst *Create(Value *Func,
2441 BasicBlock *IfNormal, BasicBlock *IfException,
2442 InputIterator ArgBegin, InputIterator ArgEnd,
2443 const Twine &NameStr = "",
2444 Instruction *InsertBefore = 0) {
2445 unsigned Values(ArgEnd - ArgBegin + 3);
2446 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2447 Values, NameStr, InsertBefore);
2449 template<typename InputIterator>
2450 static InvokeInst *Create(Value *Func,
2451 BasicBlock *IfNormal, BasicBlock *IfException,
2452 InputIterator ArgBegin, InputIterator ArgEnd,
2453 const Twine &NameStr,
2454 BasicBlock *InsertAtEnd) {
2455 unsigned Values(ArgEnd - ArgBegin + 3);
2456 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2457 Values, NameStr, InsertAtEnd);
2460 /// Provide fast operand accessors
2461 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2463 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2464 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2465 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2467 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2469 CallingConv::ID getCallingConv() const {
2470 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2472 void setCallingConv(CallingConv::ID CC) {
2473 setInstructionSubclassData(static_cast<unsigned>(CC));
2476 /// getAttributes - Return the parameter attributes for this invoke.
2478 const AttrListPtr &getAttributes() const { return AttributeList; }
2480 /// setAttributes - Set the parameter attributes for this invoke.
2482 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2484 /// addAttribute - adds the attribute to the list of attributes.
2485 void addAttribute(unsigned i, Attributes attr);
2487 /// removeAttribute - removes the attribute from the list of attributes.
2488 void removeAttribute(unsigned i, Attributes attr);
2490 /// @brief Determine whether the call or the callee has the given attribute.
2491 bool paramHasAttr(unsigned i, Attributes attr) const;
2493 /// @brief Extract the alignment for a call or parameter (0=unknown).
2494 unsigned getParamAlignment(unsigned i) const {
2495 return AttributeList.getParamAlignment(i);
2498 /// @brief Return true if the call should not be inlined.
2499 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
2500 void setIsNoInline(bool Value = true) {
2501 if (Value) addAttribute(~0, Attribute::NoInline);
2502 else removeAttribute(~0, Attribute::NoInline);
2505 /// @brief Determine if the call does not access memory.
2506 bool doesNotAccessMemory() const {
2507 return paramHasAttr(~0, Attribute::ReadNone);
2509 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2510 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2511 else removeAttribute(~0, Attribute::ReadNone);
2514 /// @brief Determine if the call does not access or only reads memory.
2515 bool onlyReadsMemory() const {
2516 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2518 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2519 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2520 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2523 /// @brief Determine if the call cannot return.
2524 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
2525 void setDoesNotReturn(bool DoesNotReturn = true) {
2526 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2527 else removeAttribute(~0, Attribute::NoReturn);
2530 /// @brief Determine if the call cannot unwind.
2531 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
2532 void setDoesNotThrow(bool DoesNotThrow = true) {
2533 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2534 else removeAttribute(~0, Attribute::NoUnwind);
2537 /// @brief Determine if the call returns a structure through first
2538 /// pointer argument.
2539 bool hasStructRetAttr() const {
2540 // Be friendly and also check the callee.
2541 return paramHasAttr(1, Attribute::StructRet);
2544 /// @brief Determine if any call argument is an aggregate passed by value.
2545 bool hasByValArgument() const {
2546 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2549 /// getCalledFunction - Return the function called, or null if this is an
2550 /// indirect function invocation.
2552 Function *getCalledFunction() const {
2553 return dyn_cast<Function>(Op<-3>());
2556 /// getCalledValue - Get a pointer to the function that is invoked by this
2558 const Value *getCalledValue() const { return Op<-3>(); }
2559 Value *getCalledValue() { return Op<-3>(); }
2561 /// setCalledFunction - Set the function called.
2562 void setCalledFunction(Value* Fn) {
2566 // get*Dest - Return the destination basic blocks...
2567 BasicBlock *getNormalDest() const {
2568 return cast<BasicBlock>(Op<-2>());
2570 BasicBlock *getUnwindDest() const {
2571 return cast<BasicBlock>(Op<-1>());
2573 void setNormalDest(BasicBlock *B) {
2574 Op<-2>() = reinterpret_cast<Value*>(B);
2576 void setUnwindDest(BasicBlock *B) {
2577 Op<-1>() = reinterpret_cast<Value*>(B);
2580 BasicBlock *getSuccessor(unsigned i) const {
2581 assert(i < 2 && "Successor # out of range for invoke!");
2582 return i == 0 ? getNormalDest() : getUnwindDest();
2585 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2586 assert(idx < 2 && "Successor # out of range for invoke!");
2587 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
2590 unsigned getNumSuccessors() const { return 2; }
2592 // Methods for support type inquiry through isa, cast, and dyn_cast:
2593 static inline bool classof(const InvokeInst *) { return true; }
2594 static inline bool classof(const Instruction *I) {
2595 return (I->getOpcode() == Instruction::Invoke);
2597 static inline bool classof(const Value *V) {
2598 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2602 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2603 virtual unsigned getNumSuccessorsV() const;
2604 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2606 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2607 // method so that subclasses cannot accidentally use it.
2608 void setInstructionSubclassData(unsigned short D) {
2609 Instruction::setInstructionSubclassData(D);
2614 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2617 template<typename InputIterator>
2618 InvokeInst::InvokeInst(Value *Func,
2619 BasicBlock *IfNormal, BasicBlock *IfException,
2620 InputIterator ArgBegin, InputIterator ArgEnd,
2622 const Twine &NameStr, Instruction *InsertBefore)
2623 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2624 ->getElementType())->getReturnType(),
2625 Instruction::Invoke,
2626 OperandTraits<InvokeInst>::op_end(this) - Values,
2627 Values, InsertBefore) {
2628 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2629 typename std::iterator_traits<InputIterator>::iterator_category());
2631 template<typename InputIterator>
2632 InvokeInst::InvokeInst(Value *Func,
2633 BasicBlock *IfNormal, BasicBlock *IfException,
2634 InputIterator ArgBegin, InputIterator ArgEnd,
2636 const Twine &NameStr, BasicBlock *InsertAtEnd)
2637 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2638 ->getElementType())->getReturnType(),
2639 Instruction::Invoke,
2640 OperandTraits<InvokeInst>::op_end(this) - Values,
2641 Values, InsertAtEnd) {
2642 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2643 typename std::iterator_traits<InputIterator>::iterator_category());
2646 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2648 //===----------------------------------------------------------------------===//
2650 //===----------------------------------------------------------------------===//
2652 //===---------------------------------------------------------------------------
2653 /// UnwindInst - Immediately exit the current function, unwinding the stack
2654 /// until an invoke instruction is found.
2656 class UnwindInst : public TerminatorInst {
2657 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2659 virtual UnwindInst *clone_impl() const;
2661 // allocate space for exactly zero operands
2662 void *operator new(size_t s) {
2663 return User::operator new(s, 0);
2665 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2666 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2668 unsigned getNumSuccessors() const { return 0; }
2670 // Methods for support type inquiry through isa, cast, and dyn_cast:
2671 static inline bool classof(const UnwindInst *) { return true; }
2672 static inline bool classof(const Instruction *I) {
2673 return I->getOpcode() == Instruction::Unwind;
2675 static inline bool classof(const Value *V) {
2676 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2679 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2680 virtual unsigned getNumSuccessorsV() const;
2681 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2684 //===----------------------------------------------------------------------===//
2685 // UnreachableInst Class
2686 //===----------------------------------------------------------------------===//
2688 //===---------------------------------------------------------------------------
2689 /// UnreachableInst - This function has undefined behavior. In particular, the
2690 /// presence of this instruction indicates some higher level knowledge that the
2691 /// end of the block cannot be reached.
2693 class UnreachableInst : public TerminatorInst {
2694 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2696 virtual UnreachableInst *clone_impl() const;
2699 // allocate space for exactly zero operands
2700 void *operator new(size_t s) {
2701 return User::operator new(s, 0);
2703 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2704 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2706 unsigned getNumSuccessors() const { return 0; }
2708 // Methods for support type inquiry through isa, cast, and dyn_cast:
2709 static inline bool classof(const UnreachableInst *) { return true; }
2710 static inline bool classof(const Instruction *I) {
2711 return I->getOpcode() == Instruction::Unreachable;
2713 static inline bool classof(const Value *V) {
2714 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2717 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2718 virtual unsigned getNumSuccessorsV() const;
2719 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2722 //===----------------------------------------------------------------------===//
2724 //===----------------------------------------------------------------------===//
2726 /// @brief This class represents a truncation of integer types.
2727 class TruncInst : public CastInst {
2729 /// @brief Clone an identical TruncInst
2730 virtual TruncInst *clone_impl() const;
2733 /// @brief Constructor with insert-before-instruction semantics
2735 Value *S, ///< The value to be truncated
2736 const Type *Ty, ///< The (smaller) type to truncate to
2737 const Twine &NameStr = "", ///< A name for the new instruction
2738 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2741 /// @brief Constructor with insert-at-end-of-block semantics
2743 Value *S, ///< The value to be truncated
2744 const Type *Ty, ///< The (smaller) type to truncate to
2745 const Twine &NameStr, ///< A name for the new instruction
2746 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2749 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2750 static inline bool classof(const TruncInst *) { return true; }
2751 static inline bool classof(const Instruction *I) {
2752 return I->getOpcode() == Trunc;
2754 static inline bool classof(const Value *V) {
2755 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2759 //===----------------------------------------------------------------------===//
2761 //===----------------------------------------------------------------------===//
2763 /// @brief This class represents zero extension of integer types.
2764 class ZExtInst : public CastInst {
2766 /// @brief Clone an identical ZExtInst
2767 virtual ZExtInst *clone_impl() const;
2770 /// @brief Constructor with insert-before-instruction semantics
2772 Value *S, ///< The value to be zero extended
2773 const Type *Ty, ///< The type to zero extend to
2774 const Twine &NameStr = "", ///< A name for the new instruction
2775 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2778 /// @brief Constructor with insert-at-end semantics.
2780 Value *S, ///< The value to be zero extended
2781 const Type *Ty, ///< The type to zero extend to
2782 const Twine &NameStr, ///< A name for the new instruction
2783 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2786 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2787 static inline bool classof(const ZExtInst *) { return true; }
2788 static inline bool classof(const Instruction *I) {
2789 return I->getOpcode() == ZExt;
2791 static inline bool classof(const Value *V) {
2792 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2796 //===----------------------------------------------------------------------===//
2798 //===----------------------------------------------------------------------===//
2800 /// @brief This class represents a sign extension of integer types.
2801 class SExtInst : public CastInst {
2803 /// @brief Clone an identical SExtInst
2804 virtual SExtInst *clone_impl() const;
2807 /// @brief Constructor with insert-before-instruction semantics
2809 Value *S, ///< The value to be sign extended
2810 const Type *Ty, ///< The type to sign extend to
2811 const Twine &NameStr = "", ///< A name for the new instruction
2812 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2815 /// @brief Constructor with insert-at-end-of-block semantics
2817 Value *S, ///< The value to be sign extended
2818 const Type *Ty, ///< The type to sign extend to
2819 const Twine &NameStr, ///< A name for the new instruction
2820 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2823 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2824 static inline bool classof(const SExtInst *) { return true; }
2825 static inline bool classof(const Instruction *I) {
2826 return I->getOpcode() == SExt;
2828 static inline bool classof(const Value *V) {
2829 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2833 //===----------------------------------------------------------------------===//
2834 // FPTruncInst Class
2835 //===----------------------------------------------------------------------===//
2837 /// @brief This class represents a truncation of floating point types.
2838 class FPTruncInst : public CastInst {
2840 /// @brief Clone an identical FPTruncInst
2841 virtual FPTruncInst *clone_impl() const;
2844 /// @brief Constructor with insert-before-instruction semantics
2846 Value *S, ///< The value to be truncated
2847 const Type *Ty, ///< The type to truncate to
2848 const Twine &NameStr = "", ///< A name for the new instruction
2849 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2852 /// @brief Constructor with insert-before-instruction semantics
2854 Value *S, ///< The value to be truncated
2855 const Type *Ty, ///< The type to truncate to
2856 const Twine &NameStr, ///< A name for the new instruction
2857 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2860 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2861 static inline bool classof(const FPTruncInst *) { return true; }
2862 static inline bool classof(const Instruction *I) {
2863 return I->getOpcode() == FPTrunc;
2865 static inline bool classof(const Value *V) {
2866 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2870 //===----------------------------------------------------------------------===//
2872 //===----------------------------------------------------------------------===//
2874 /// @brief This class represents an extension of floating point types.
2875 class FPExtInst : public CastInst {
2877 /// @brief Clone an identical FPExtInst
2878 virtual FPExtInst *clone_impl() const;
2881 /// @brief Constructor with insert-before-instruction semantics
2883 Value *S, ///< The value to be extended
2884 const Type *Ty, ///< The type to extend to
2885 const Twine &NameStr = "", ///< A name for the new instruction
2886 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2889 /// @brief Constructor with insert-at-end-of-block semantics
2891 Value *S, ///< The value to be extended
2892 const Type *Ty, ///< The type to extend to
2893 const Twine &NameStr, ///< A name for the new instruction
2894 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2897 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2898 static inline bool classof(const FPExtInst *) { return true; }
2899 static inline bool classof(const Instruction *I) {
2900 return I->getOpcode() == FPExt;
2902 static inline bool classof(const Value *V) {
2903 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2907 //===----------------------------------------------------------------------===//
2909 //===----------------------------------------------------------------------===//
2911 /// @brief This class represents a cast unsigned integer to floating point.
2912 class UIToFPInst : public CastInst {
2914 /// @brief Clone an identical UIToFPInst
2915 virtual UIToFPInst *clone_impl() const;
2918 /// @brief Constructor with insert-before-instruction semantics
2920 Value *S, ///< The value to be converted
2921 const Type *Ty, ///< The type to convert to
2922 const Twine &NameStr = "", ///< A name for the new instruction
2923 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2926 /// @brief Constructor with insert-at-end-of-block semantics
2928 Value *S, ///< The value to be converted
2929 const Type *Ty, ///< The type to convert to
2930 const Twine &NameStr, ///< A name for the new instruction
2931 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2934 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2935 static inline bool classof(const UIToFPInst *) { return true; }
2936 static inline bool classof(const Instruction *I) {
2937 return I->getOpcode() == UIToFP;
2939 static inline bool classof(const Value *V) {
2940 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2944 //===----------------------------------------------------------------------===//
2946 //===----------------------------------------------------------------------===//
2948 /// @brief This class represents a cast from signed integer to floating point.
2949 class SIToFPInst : public CastInst {
2951 /// @brief Clone an identical SIToFPInst
2952 virtual SIToFPInst *clone_impl() const;
2955 /// @brief Constructor with insert-before-instruction semantics
2957 Value *S, ///< The value to be converted
2958 const Type *Ty, ///< The type to convert to
2959 const Twine &NameStr = "", ///< A name for the new instruction
2960 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2963 /// @brief Constructor with insert-at-end-of-block semantics
2965 Value *S, ///< The value to be converted
2966 const Type *Ty, ///< The type to convert to
2967 const Twine &NameStr, ///< A name for the new instruction
2968 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2971 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2972 static inline bool classof(const SIToFPInst *) { return true; }
2973 static inline bool classof(const Instruction *I) {
2974 return I->getOpcode() == SIToFP;
2976 static inline bool classof(const Value *V) {
2977 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2981 //===----------------------------------------------------------------------===//
2983 //===----------------------------------------------------------------------===//
2985 /// @brief This class represents a cast from floating point to unsigned integer
2986 class FPToUIInst : public CastInst {
2988 /// @brief Clone an identical FPToUIInst
2989 virtual FPToUIInst *clone_impl() const;
2992 /// @brief Constructor with insert-before-instruction semantics
2994 Value *S, ///< The value to be converted
2995 const Type *Ty, ///< The type to convert to
2996 const Twine &NameStr = "", ///< A name for the new instruction
2997 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3000 /// @brief Constructor with insert-at-end-of-block semantics
3002 Value *S, ///< The value to be converted
3003 const Type *Ty, ///< The type to convert to
3004 const Twine &NameStr, ///< A name for the new instruction
3005 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3008 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3009 static inline bool classof(const FPToUIInst *) { return true; }
3010 static inline bool classof(const Instruction *I) {
3011 return I->getOpcode() == FPToUI;
3013 static inline bool classof(const Value *V) {
3014 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3018 //===----------------------------------------------------------------------===//
3020 //===----------------------------------------------------------------------===//
3022 /// @brief This class represents a cast from floating point to signed integer.
3023 class FPToSIInst : public CastInst {
3025 /// @brief Clone an identical FPToSIInst
3026 virtual FPToSIInst *clone_impl() const;
3029 /// @brief Constructor with insert-before-instruction semantics
3031 Value *S, ///< The value to be converted
3032 const Type *Ty, ///< The type to convert to
3033 const Twine &NameStr = "", ///< A name for the new instruction
3034 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3037 /// @brief Constructor with insert-at-end-of-block semantics
3039 Value *S, ///< The value to be converted
3040 const Type *Ty, ///< The type to convert to
3041 const Twine &NameStr, ///< A name for the new instruction
3042 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3045 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3046 static inline bool classof(const FPToSIInst *) { return true; }
3047 static inline bool classof(const Instruction *I) {
3048 return I->getOpcode() == FPToSI;
3050 static inline bool classof(const Value *V) {
3051 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3055 //===----------------------------------------------------------------------===//
3056 // IntToPtrInst Class
3057 //===----------------------------------------------------------------------===//
3059 /// @brief This class represents a cast from an integer to a pointer.
3060 class IntToPtrInst : public CastInst {
3062 /// @brief Constructor with insert-before-instruction semantics
3064 Value *S, ///< The value to be converted
3065 const Type *Ty, ///< The type to convert to
3066 const Twine &NameStr = "", ///< A name for the new instruction
3067 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3070 /// @brief Constructor with insert-at-end-of-block semantics
3072 Value *S, ///< The value to be converted
3073 const Type *Ty, ///< The type to convert to
3074 const Twine &NameStr, ///< A name for the new instruction
3075 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3078 /// @brief Clone an identical IntToPtrInst
3079 virtual IntToPtrInst *clone_impl() const;
3081 // Methods for support type inquiry through isa, cast, and dyn_cast:
3082 static inline bool classof(const IntToPtrInst *) { return true; }
3083 static inline bool classof(const Instruction *I) {
3084 return I->getOpcode() == IntToPtr;
3086 static inline bool classof(const Value *V) {
3087 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3091 //===----------------------------------------------------------------------===//
3092 // PtrToIntInst Class
3093 //===----------------------------------------------------------------------===//
3095 /// @brief This class represents a cast from a pointer to an integer
3096 class PtrToIntInst : public CastInst {
3098 /// @brief Clone an identical PtrToIntInst
3099 virtual PtrToIntInst *clone_impl() const;
3102 /// @brief Constructor with insert-before-instruction semantics
3104 Value *S, ///< The value to be converted
3105 const Type *Ty, ///< The type to convert to
3106 const Twine &NameStr = "", ///< A name for the new instruction
3107 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3110 /// @brief Constructor with insert-at-end-of-block semantics
3112 Value *S, ///< The value to be converted
3113 const Type *Ty, ///< The type to convert to
3114 const Twine &NameStr, ///< A name for the new instruction
3115 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3118 // Methods for support type inquiry through isa, cast, and dyn_cast:
3119 static inline bool classof(const PtrToIntInst *) { return true; }
3120 static inline bool classof(const Instruction *I) {
3121 return I->getOpcode() == PtrToInt;
3123 static inline bool classof(const Value *V) {
3124 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3128 //===----------------------------------------------------------------------===//
3129 // BitCastInst Class
3130 //===----------------------------------------------------------------------===//
3132 /// @brief This class represents a no-op cast from one type to another.
3133 class BitCastInst : public CastInst {
3135 /// @brief Clone an identical BitCastInst
3136 virtual BitCastInst *clone_impl() const;
3139 /// @brief Constructor with insert-before-instruction semantics
3141 Value *S, ///< The value to be casted
3142 const Type *Ty, ///< The type to casted to
3143 const Twine &NameStr = "", ///< A name for the new instruction
3144 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3147 /// @brief Constructor with insert-at-end-of-block semantics
3149 Value *S, ///< The value to be casted
3150 const Type *Ty, ///< The type to casted to
3151 const Twine &NameStr, ///< A name for the new instruction
3152 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3155 // Methods for support type inquiry through isa, cast, and dyn_cast:
3156 static inline bool classof(const BitCastInst *) { return true; }
3157 static inline bool classof(const Instruction *I) {
3158 return I->getOpcode() == BitCast;
3160 static inline bool classof(const Value *V) {
3161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3165 } // End llvm namespace