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 << getSubclassDataFromValue()) >> 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 Value::setValueSubclassData with a private forwarding method so that
105 // subclasses cannot accidentally use it.
106 void setValueSubclassData(unsigned short D) {
107 Value::setValueSubclassData(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 getSubclassDataFromValue() & 1; }
147 /// setVolatile - Specify whether this is a volatile load or not.
149 void setVolatile(bool V) {
150 setValueSubclassData((getSubclassDataFromValue() & ~1) | (V ? 1 : 0));
153 /// getAlignment - Return the alignment of the access that is being performed
155 unsigned getAlignment() const {
156 return (1 << (getSubclassDataFromValue() >> 1)) >> 1;
159 void setAlignment(unsigned Align);
161 Value *getPointerOperand() { return getOperand(0); }
162 const Value *getPointerOperand() const { return getOperand(0); }
163 static unsigned getPointerOperandIndex() { return 0U; }
165 unsigned getPointerAddressSpace() const {
166 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
170 // Methods for support type inquiry through isa, cast, and dyn_cast:
171 static inline bool classof(const LoadInst *) { return true; }
172 static inline bool classof(const Instruction *I) {
173 return I->getOpcode() == Instruction::Load;
175 static inline bool classof(const Value *V) {
176 return isa<Instruction>(V) && classof(cast<Instruction>(V));
179 // Shadow Value::setValueSubclassData with a private forwarding method so that
180 // subclasses cannot accidentally use it.
181 void setValueSubclassData(unsigned short D) {
182 Value::setValueSubclassData(D);
187 //===----------------------------------------------------------------------===//
189 //===----------------------------------------------------------------------===//
191 /// StoreInst - an instruction for storing to memory
193 class StoreInst : public Instruction {
194 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
197 virtual StoreInst *clone_impl() const;
199 // allocate space for exactly two operands
200 void *operator new(size_t s) {
201 return User::operator new(s, 2);
203 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
204 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
205 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
206 Instruction *InsertBefore = 0);
207 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
208 unsigned Align, Instruction *InsertBefore = 0);
209 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
210 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
211 unsigned Align, BasicBlock *InsertAtEnd);
214 /// isVolatile - Return true if this is a load from a volatile memory
217 bool isVolatile() const { return getSubclassDataFromValue() & 1; }
219 /// setVolatile - Specify whether this is a volatile load or not.
221 void setVolatile(bool V) {
222 setValueSubclassData((getSubclassDataFromValue() & ~1) | (V ? 1 : 0));
225 /// Transparently provide more efficient getOperand methods.
226 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
228 /// getAlignment - Return the alignment of the access that is being performed
230 unsigned getAlignment() const {
231 return (1 << (getSubclassDataFromValue() >> 1)) >> 1;
234 void setAlignment(unsigned Align);
236 Value *getPointerOperand() { return getOperand(1); }
237 const Value *getPointerOperand() const { return getOperand(1); }
238 static unsigned getPointerOperandIndex() { return 1U; }
240 unsigned getPointerAddressSpace() const {
241 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
244 // Methods for support type inquiry through isa, cast, and dyn_cast:
245 static inline bool classof(const StoreInst *) { return true; }
246 static inline bool classof(const Instruction *I) {
247 return I->getOpcode() == Instruction::Store;
249 static inline bool classof(const Value *V) {
250 return isa<Instruction>(V) && classof(cast<Instruction>(V));
253 // Shadow Value::setValueSubclassData with a private forwarding method so that
254 // subclasses cannot accidentally use it.
255 void setValueSubclassData(unsigned short D) {
256 Value::setValueSubclassData(D);
261 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
264 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
266 //===----------------------------------------------------------------------===//
267 // GetElementPtrInst Class
268 //===----------------------------------------------------------------------===//
270 // checkType - Simple wrapper function to give a better assertion failure
271 // message on bad indexes for a gep instruction.
273 static inline const Type *checkType(const Type *Ty) {
274 assert(Ty && "Invalid GetElementPtrInst indices for type!");
278 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
279 /// access elements of arrays and structs
281 class GetElementPtrInst : public Instruction {
282 GetElementPtrInst(const GetElementPtrInst &GEPI);
283 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
284 const Twine &NameStr);
285 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
287 template<typename InputIterator>
288 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
289 const Twine &NameStr,
290 // This argument ensures that we have an iterator we can
291 // do arithmetic on in constant time
292 std::random_access_iterator_tag) {
293 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
296 // This requires that the iterator points to contiguous memory.
297 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
298 // we have to build an array here
301 init(Ptr, 0, NumIdx, NameStr);
305 /// getIndexedType - Returns the type of the element that would be loaded with
306 /// a load instruction with the specified parameters.
308 /// Null is returned if the indices are invalid for the specified
311 template<typename InputIterator>
312 static const Type *getIndexedType(const Type *Ptr,
313 InputIterator IdxBegin,
314 InputIterator IdxEnd,
315 // This argument ensures that we
316 // have an iterator we can do
317 // arithmetic on in constant time
318 std::random_access_iterator_tag) {
319 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
322 // This requires that the iterator points to contiguous memory.
323 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
325 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
328 /// Constructors - Create a getelementptr instruction with a base pointer an
329 /// list of indices. The first ctor can optionally insert before an existing
330 /// instruction, the second appends the new instruction to the specified
332 template<typename InputIterator>
333 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
334 InputIterator IdxEnd,
336 const Twine &NameStr,
337 Instruction *InsertBefore);
338 template<typename InputIterator>
339 inline GetElementPtrInst(Value *Ptr,
340 InputIterator IdxBegin, InputIterator IdxEnd,
342 const Twine &NameStr, BasicBlock *InsertAtEnd);
344 /// Constructors - These two constructors are convenience methods because one
345 /// and two index getelementptr instructions are so common.
346 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
347 Instruction *InsertBefore = 0);
348 GetElementPtrInst(Value *Ptr, Value *Idx,
349 const Twine &NameStr, BasicBlock *InsertAtEnd);
351 virtual GetElementPtrInst *clone_impl() const;
353 template<typename InputIterator>
354 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
355 InputIterator IdxEnd,
356 const Twine &NameStr = "",
357 Instruction *InsertBefore = 0) {
358 typename std::iterator_traits<InputIterator>::difference_type Values =
359 1 + std::distance(IdxBegin, IdxEnd);
361 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
363 template<typename InputIterator>
364 static GetElementPtrInst *Create(Value *Ptr,
365 InputIterator IdxBegin, InputIterator IdxEnd,
366 const Twine &NameStr,
367 BasicBlock *InsertAtEnd) {
368 typename std::iterator_traits<InputIterator>::difference_type Values =
369 1 + std::distance(IdxBegin, IdxEnd);
371 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
374 /// Constructors - These two creators are convenience methods because one
375 /// index getelementptr instructions are so common.
376 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
377 const Twine &NameStr = "",
378 Instruction *InsertBefore = 0) {
379 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
381 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
382 const Twine &NameStr,
383 BasicBlock *InsertAtEnd) {
384 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
387 /// Create an "inbounds" getelementptr. See the documentation for the
388 /// "inbounds" flag in LangRef.html for details.
389 template<typename InputIterator>
390 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
391 InputIterator IdxEnd,
392 const Twine &NameStr = "",
393 Instruction *InsertBefore = 0) {
394 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
395 NameStr, InsertBefore);
396 GEP->setIsInBounds(true);
399 template<typename InputIterator>
400 static GetElementPtrInst *CreateInBounds(Value *Ptr,
401 InputIterator IdxBegin,
402 InputIterator IdxEnd,
403 const Twine &NameStr,
404 BasicBlock *InsertAtEnd) {
405 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
406 NameStr, InsertAtEnd);
407 GEP->setIsInBounds(true);
410 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
411 const Twine &NameStr = "",
412 Instruction *InsertBefore = 0) {
413 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
414 GEP->setIsInBounds(true);
417 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
418 const Twine &NameStr,
419 BasicBlock *InsertAtEnd) {
420 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
421 GEP->setIsInBounds(true);
425 /// Transparently provide more efficient getOperand methods.
426 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
428 // getType - Overload to return most specific pointer type...
429 const PointerType *getType() const {
430 return reinterpret_cast<const PointerType*>(Instruction::getType());
433 /// getIndexedType - Returns the type of the element that would be loaded with
434 /// a load instruction with the specified parameters.
436 /// Null is returned if the indices are invalid for the specified
439 template<typename InputIterator>
440 static const Type *getIndexedType(const Type *Ptr,
441 InputIterator IdxBegin,
442 InputIterator IdxEnd) {
443 return getIndexedType(Ptr, IdxBegin, IdxEnd,
444 typename std::iterator_traits<InputIterator>::
445 iterator_category());
448 static const Type *getIndexedType(const Type *Ptr,
449 Value* const *Idx, unsigned NumIdx);
451 static const Type *getIndexedType(const Type *Ptr,
452 uint64_t const *Idx, unsigned NumIdx);
454 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
456 inline op_iterator idx_begin() { return op_begin()+1; }
457 inline const_op_iterator idx_begin() const { return op_begin()+1; }
458 inline op_iterator idx_end() { return op_end(); }
459 inline const_op_iterator idx_end() const { return op_end(); }
461 Value *getPointerOperand() {
462 return getOperand(0);
464 const Value *getPointerOperand() const {
465 return getOperand(0);
467 static unsigned getPointerOperandIndex() {
468 return 0U; // get index for modifying correct operand
471 unsigned getPointerAddressSpace() const {
472 return cast<PointerType>(getType())->getAddressSpace();
475 /// getPointerOperandType - Method to return the pointer operand as a
477 const PointerType *getPointerOperandType() const {
478 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
482 unsigned getNumIndices() const { // Note: always non-negative
483 return getNumOperands() - 1;
486 bool hasIndices() const {
487 return getNumOperands() > 1;
490 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
491 /// zeros. If so, the result pointer and the first operand have the same
492 /// value, just potentially different types.
493 bool hasAllZeroIndices() const;
495 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
496 /// constant integers. If so, the result pointer and the first operand have
497 /// a constant offset between them.
498 bool hasAllConstantIndices() const;
500 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
501 /// See LangRef.html for the meaning of inbounds on a getelementptr.
502 void setIsInBounds(bool b = true);
504 /// isInBounds - Determine whether the GEP has the inbounds flag.
505 bool isInBounds() const;
507 // Methods for support type inquiry through isa, cast, and dyn_cast:
508 static inline bool classof(const GetElementPtrInst *) { return true; }
509 static inline bool classof(const Instruction *I) {
510 return (I->getOpcode() == Instruction::GetElementPtr);
512 static inline bool classof(const Value *V) {
513 return isa<Instruction>(V) && classof(cast<Instruction>(V));
518 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
521 template<typename InputIterator>
522 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
523 InputIterator IdxBegin,
524 InputIterator IdxEnd,
526 const Twine &NameStr,
527 Instruction *InsertBefore)
528 : Instruction(PointerType::get(checkType(
529 getIndexedType(Ptr->getType(),
531 cast<PointerType>(Ptr->getType())
532 ->getAddressSpace()),
534 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
535 Values, InsertBefore) {
536 init(Ptr, IdxBegin, IdxEnd, NameStr,
537 typename std::iterator_traits<InputIterator>::iterator_category());
539 template<typename InputIterator>
540 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
541 InputIterator IdxBegin,
542 InputIterator IdxEnd,
544 const Twine &NameStr,
545 BasicBlock *InsertAtEnd)
546 : Instruction(PointerType::get(checkType(
547 getIndexedType(Ptr->getType(),
549 cast<PointerType>(Ptr->getType())
550 ->getAddressSpace()),
552 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
553 Values, InsertAtEnd) {
554 init(Ptr, IdxBegin, IdxEnd, NameStr,
555 typename std::iterator_traits<InputIterator>::iterator_category());
559 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
562 //===----------------------------------------------------------------------===//
564 //===----------------------------------------------------------------------===//
566 /// This instruction compares its operands according to the predicate given
567 /// to the constructor. It only operates on integers or pointers. The operands
568 /// must be identical types.
569 /// @brief Represent an integer comparison operator.
570 class ICmpInst: public CmpInst {
572 /// @brief Clone an indentical ICmpInst
573 virtual ICmpInst *clone_impl() const;
575 /// @brief Constructor with insert-before-instruction semantics.
577 Instruction *InsertBefore, ///< Where to insert
578 Predicate pred, ///< The predicate to use for the comparison
579 Value *LHS, ///< The left-hand-side of the expression
580 Value *RHS, ///< The right-hand-side of the expression
581 const Twine &NameStr = "" ///< Name of the instruction
582 ) : CmpInst(makeCmpResultType(LHS->getType()),
583 Instruction::ICmp, pred, LHS, RHS, NameStr,
585 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
586 pred <= CmpInst::LAST_ICMP_PREDICATE &&
587 "Invalid ICmp predicate value");
588 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
589 "Both operands to ICmp instruction are not of the same type!");
590 // Check that the operands are the right type
591 assert((getOperand(0)->getType()->isIntOrIntVector() ||
592 isa<PointerType>(getOperand(0)->getType())) &&
593 "Invalid operand types for ICmp instruction");
596 /// @brief Constructor with insert-at-end semantics.
598 BasicBlock &InsertAtEnd, ///< Block to insert into.
599 Predicate pred, ///< The predicate to use for the comparison
600 Value *LHS, ///< The left-hand-side of the expression
601 Value *RHS, ///< The right-hand-side of the expression
602 const Twine &NameStr = "" ///< Name of the instruction
603 ) : CmpInst(makeCmpResultType(LHS->getType()),
604 Instruction::ICmp, pred, LHS, RHS, NameStr,
606 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
607 pred <= CmpInst::LAST_ICMP_PREDICATE &&
608 "Invalid ICmp predicate value");
609 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
610 "Both operands to ICmp instruction are not of the same type!");
611 // Check that the operands are the right type
612 assert((getOperand(0)->getType()->isIntOrIntVector() ||
613 isa<PointerType>(getOperand(0)->getType())) &&
614 "Invalid operand types for ICmp instruction");
617 /// @brief Constructor with no-insertion semantics
619 Predicate pred, ///< The predicate to use for the comparison
620 Value *LHS, ///< The left-hand-side of the expression
621 Value *RHS, ///< The right-hand-side of the expression
622 const Twine &NameStr = "" ///< Name of the instruction
623 ) : CmpInst(makeCmpResultType(LHS->getType()),
624 Instruction::ICmp, pred, LHS, RHS, NameStr) {
625 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
626 pred <= CmpInst::LAST_ICMP_PREDICATE &&
627 "Invalid ICmp predicate value");
628 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
629 "Both operands to ICmp instruction are not of the same type!");
630 // Check that the operands are the right type
631 assert((getOperand(0)->getType()->isIntOrIntVector() ||
632 isa<PointerType>(getOperand(0)->getType())) &&
633 "Invalid operand types for ICmp instruction");
636 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
637 /// @returns the predicate that would be the result if the operand were
638 /// regarded as signed.
639 /// @brief Return the signed version of the predicate
640 Predicate getSignedPredicate() const {
641 return getSignedPredicate(getPredicate());
644 /// This is a static version that you can use without an instruction.
645 /// @brief Return the signed version of the predicate.
646 static Predicate getSignedPredicate(Predicate pred);
648 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
649 /// @returns the predicate that would be the result if the operand were
650 /// regarded as unsigned.
651 /// @brief Return the unsigned version of the predicate
652 Predicate getUnsignedPredicate() const {
653 return getUnsignedPredicate(getPredicate());
656 /// This is a static version that you can use without an instruction.
657 /// @brief Return the unsigned version of the predicate.
658 static Predicate getUnsignedPredicate(Predicate pred);
660 /// isEquality - Return true if this predicate is either EQ or NE. This also
661 /// tests for commutativity.
662 static bool isEquality(Predicate P) {
663 return P == ICMP_EQ || P == ICMP_NE;
666 /// isEquality - Return true if this predicate is either EQ or NE. This also
667 /// tests for commutativity.
668 bool isEquality() const {
669 return isEquality(getPredicate());
672 /// @returns true if the predicate of this ICmpInst is commutative
673 /// @brief Determine if this relation is commutative.
674 bool isCommutative() const { return isEquality(); }
676 /// isRelational - Return true if the predicate is relational (not EQ or NE).
678 bool isRelational() const {
679 return !isEquality();
682 /// isRelational - Return true if the predicate is relational (not EQ or NE).
684 static bool isRelational(Predicate P) {
685 return !isEquality(P);
688 /// Initialize a set of values that all satisfy the predicate with C.
689 /// @brief Make a ConstantRange for a relation with a constant value.
690 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
692 /// Exchange the two operands to this instruction in such a way that it does
693 /// not modify the semantics of the instruction. The predicate value may be
694 /// changed to retain the same result if the predicate is order dependent
696 /// @brief Swap operands and adjust predicate.
697 void swapOperands() {
698 setPredicate(getSwappedPredicate());
699 Op<0>().swap(Op<1>());
702 // Methods for support type inquiry through isa, cast, and dyn_cast:
703 static inline bool classof(const ICmpInst *) { return true; }
704 static inline bool classof(const Instruction *I) {
705 return I->getOpcode() == Instruction::ICmp;
707 static inline bool classof(const Value *V) {
708 return isa<Instruction>(V) && classof(cast<Instruction>(V));
713 //===----------------------------------------------------------------------===//
715 //===----------------------------------------------------------------------===//
717 /// This instruction compares its operands according to the predicate given
718 /// to the constructor. It only operates on floating point values or packed
719 /// vectors of floating point values. The operands must be identical types.
720 /// @brief Represents a floating point comparison operator.
721 class FCmpInst: public CmpInst {
723 /// @brief Clone an indentical FCmpInst
724 virtual FCmpInst *clone_impl() const;
726 /// @brief Constructor with insert-before-instruction semantics.
728 Instruction *InsertBefore, ///< Where to insert
729 Predicate pred, ///< The predicate to use for the comparison
730 Value *LHS, ///< The left-hand-side of the expression
731 Value *RHS, ///< The right-hand-side of the expression
732 const Twine &NameStr = "" ///< Name of the instruction
733 ) : CmpInst(makeCmpResultType(LHS->getType()),
734 Instruction::FCmp, pred, LHS, RHS, NameStr,
736 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
737 "Invalid FCmp predicate value");
738 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
739 "Both operands to FCmp instruction are not of the same type!");
740 // Check that the operands are the right type
741 assert(getOperand(0)->getType()->isFPOrFPVector() &&
742 "Invalid operand types for FCmp instruction");
745 /// @brief Constructor with insert-at-end semantics.
747 BasicBlock &InsertAtEnd, ///< Block to insert into.
748 Predicate pred, ///< The predicate to use for the comparison
749 Value *LHS, ///< The left-hand-side of the expression
750 Value *RHS, ///< The right-hand-side of the expression
751 const Twine &NameStr = "" ///< Name of the instruction
752 ) : CmpInst(makeCmpResultType(LHS->getType()),
753 Instruction::FCmp, pred, LHS, RHS, NameStr,
755 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
756 "Invalid FCmp predicate value");
757 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
758 "Both operands to FCmp instruction are not of the same type!");
759 // Check that the operands are the right type
760 assert(getOperand(0)->getType()->isFPOrFPVector() &&
761 "Invalid operand types for FCmp instruction");
764 /// @brief Constructor with no-insertion semantics
766 Predicate pred, ///< The predicate to use for the comparison
767 Value *LHS, ///< The left-hand-side of the expression
768 Value *RHS, ///< The right-hand-side of the expression
769 const Twine &NameStr = "" ///< Name of the instruction
770 ) : CmpInst(makeCmpResultType(LHS->getType()),
771 Instruction::FCmp, pred, LHS, RHS, NameStr) {
772 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
773 "Invalid FCmp predicate value");
774 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
775 "Both operands to FCmp instruction are not of the same type!");
776 // Check that the operands are the right type
777 assert(getOperand(0)->getType()->isFPOrFPVector() &&
778 "Invalid operand types for FCmp instruction");
781 /// @returns true if the predicate of this instruction is EQ or NE.
782 /// @brief Determine if this is an equality predicate.
783 bool isEquality() const {
784 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
785 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
788 /// @returns true if the predicate of this instruction is commutative.
789 /// @brief Determine if this is a commutative predicate.
790 bool isCommutative() const {
791 return isEquality() ||
792 getPredicate() == FCMP_FALSE ||
793 getPredicate() == FCMP_TRUE ||
794 getPredicate() == FCMP_ORD ||
795 getPredicate() == FCMP_UNO;
798 /// @returns true if the predicate is relational (not EQ or NE).
799 /// @brief Determine if this a relational predicate.
800 bool isRelational() const { return !isEquality(); }
802 /// Exchange the two operands to this instruction in such a way that it does
803 /// not modify the semantics of the instruction. The predicate value may be
804 /// changed to retain the same result if the predicate is order dependent
806 /// @brief Swap operands and adjust predicate.
807 void swapOperands() {
808 setPredicate(getSwappedPredicate());
809 Op<0>().swap(Op<1>());
812 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
813 static inline bool classof(const FCmpInst *) { return true; }
814 static inline bool classof(const Instruction *I) {
815 return I->getOpcode() == Instruction::FCmp;
817 static inline bool classof(const Value *V) {
818 return isa<Instruction>(V) && classof(cast<Instruction>(V));
822 //===----------------------------------------------------------------------===//
823 /// CallInst - This class represents a function call, abstracting a target
824 /// machine's calling convention. This class uses low bit of the SubClassData
825 /// field to indicate whether or not this is a tail call. The rest of the bits
826 /// hold the calling convention of the call.
828 class CallInst : public Instruction {
829 AttrListPtr AttributeList; ///< parameter attributes for call
830 CallInst(const CallInst &CI);
831 void init(Value *Func, Value* const *Params, unsigned NumParams);
832 void init(Value *Func, Value *Actual1, Value *Actual2);
833 void init(Value *Func, Value *Actual);
834 void init(Value *Func);
836 template<typename InputIterator>
837 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
838 const Twine &NameStr,
839 // This argument ensures that we have an iterator we can
840 // do arithmetic on in constant time
841 std::random_access_iterator_tag) {
842 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
844 // This requires that the iterator points to contiguous memory.
845 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
849 /// Construct a CallInst given a range of arguments. InputIterator
850 /// must be a random-access iterator pointing to contiguous storage
851 /// (e.g. a std::vector<>::iterator). Checks are made for
852 /// random-accessness but not for contiguous storage as that would
853 /// incur runtime overhead.
854 /// @brief Construct a CallInst from a range of arguments
855 template<typename InputIterator>
856 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
857 const Twine &NameStr, Instruction *InsertBefore);
859 /// Construct a CallInst given a range of arguments. InputIterator
860 /// must be a random-access iterator pointing to contiguous storage
861 /// (e.g. a std::vector<>::iterator). Checks are made for
862 /// random-accessness but not for contiguous storage as that would
863 /// incur runtime overhead.
864 /// @brief Construct a CallInst from a range of arguments
865 template<typename InputIterator>
866 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
867 const Twine &NameStr, BasicBlock *InsertAtEnd);
869 CallInst(Value *F, Value *Actual, const Twine &NameStr,
870 Instruction *InsertBefore);
871 CallInst(Value *F, Value *Actual, const Twine &NameStr,
872 BasicBlock *InsertAtEnd);
873 explicit CallInst(Value *F, const Twine &NameStr,
874 Instruction *InsertBefore);
875 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
877 virtual CallInst *clone_impl() const;
879 template<typename InputIterator>
880 static CallInst *Create(Value *Func,
881 InputIterator ArgBegin, InputIterator ArgEnd,
882 const Twine &NameStr = "",
883 Instruction *InsertBefore = 0) {
884 return new((unsigned)(ArgEnd - ArgBegin + 1))
885 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
887 template<typename InputIterator>
888 static CallInst *Create(Value *Func,
889 InputIterator ArgBegin, InputIterator ArgEnd,
890 const Twine &NameStr, BasicBlock *InsertAtEnd) {
891 return new((unsigned)(ArgEnd - ArgBegin + 1))
892 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
894 static CallInst *Create(Value *F, Value *Actual,
895 const Twine &NameStr = "",
896 Instruction *InsertBefore = 0) {
897 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
899 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
900 BasicBlock *InsertAtEnd) {
901 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
903 static CallInst *Create(Value *F, const Twine &NameStr = "",
904 Instruction *InsertBefore = 0) {
905 return new(1) CallInst(F, NameStr, InsertBefore);
907 static CallInst *Create(Value *F, const Twine &NameStr,
908 BasicBlock *InsertAtEnd) {
909 return new(1) CallInst(F, NameStr, InsertAtEnd);
911 /// CreateMalloc - Generate the IR for a call to malloc:
912 /// 1. Compute the malloc call's argument as the specified type's size,
913 /// possibly multiplied by the array size if the array size is not
915 /// 2. Call malloc with that argument.
916 /// 3. Bitcast the result of the malloc call to the specified type.
917 static Instruction *CreateMalloc(Instruction *InsertBefore,
918 const Type *IntPtrTy, const Type *AllocTy,
919 Value *AllocSize, Value *ArraySize = 0,
920 const Twine &Name = "");
921 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
922 const Type *IntPtrTy, const Type *AllocTy,
923 Value *AllocSize, Value *ArraySize = 0,
924 Function* MallocF = 0,
925 const Twine &Name = "");
926 /// CreateFree - Generate the IR for a call to the builtin free function.
927 static void CreateFree(Value* Source, Instruction *InsertBefore);
928 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
932 bool isTailCall() const { return getSubclassDataFromValue() & 1; }
933 void setTailCall(bool isTC = true) {
934 setValueSubclassData((getSubclassDataFromValue() & ~1) | unsigned(isTC));
937 /// Provide fast operand accessors
938 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
940 /// getCallingConv/setCallingConv - Get or set the calling convention of this
942 CallingConv::ID getCallingConv() const {
943 return static_cast<CallingConv::ID>(getSubclassDataFromValue() >> 1);
945 void setCallingConv(CallingConv::ID CC) {
946 setValueSubclassData((getSubclassDataFromValue() & 1) |
947 (static_cast<unsigned>(CC) << 1));
950 /// getAttributes - Return the parameter attributes for this call.
952 const AttrListPtr &getAttributes() const { return AttributeList; }
954 /// setAttributes - Set the parameter attributes for this call.
956 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
958 /// addAttribute - adds the attribute to the list of attributes.
959 void addAttribute(unsigned i, Attributes attr);
961 /// removeAttribute - removes the attribute from the list of attributes.
962 void removeAttribute(unsigned i, Attributes attr);
964 /// @brief Determine whether the call or the callee has the given attribute.
965 bool paramHasAttr(unsigned i, Attributes attr) const;
967 /// @brief Extract the alignment for a call or parameter (0=unknown).
968 unsigned getParamAlignment(unsigned i) const {
969 return AttributeList.getParamAlignment(i);
972 /// @brief Determine if the call does not access memory.
973 bool doesNotAccessMemory() const {
974 return paramHasAttr(~0, Attribute::ReadNone);
976 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
977 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
978 else removeAttribute(~0, Attribute::ReadNone);
981 /// @brief Determine if the call does not access or only reads memory.
982 bool onlyReadsMemory() const {
983 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
985 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
986 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
987 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
990 /// @brief Determine if the call cannot return.
991 bool doesNotReturn() const {
992 return paramHasAttr(~0, Attribute::NoReturn);
994 void setDoesNotReturn(bool DoesNotReturn = true) {
995 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
996 else removeAttribute(~0, Attribute::NoReturn);
999 /// @brief Determine if the call cannot unwind.
1000 bool doesNotThrow() const {
1001 return paramHasAttr(~0, Attribute::NoUnwind);
1003 void setDoesNotThrow(bool DoesNotThrow = true) {
1004 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1005 else removeAttribute(~0, Attribute::NoUnwind);
1008 /// @brief Determine if the call returns a structure through first
1009 /// pointer argument.
1010 bool hasStructRetAttr() const {
1011 // Be friendly and also check the callee.
1012 return paramHasAttr(1, Attribute::StructRet);
1015 /// @brief Determine if any call argument is an aggregate passed by value.
1016 bool hasByValArgument() const {
1017 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1020 /// getCalledFunction - Return the function called, or null if this is an
1021 /// indirect function invocation.
1023 Function *getCalledFunction() const {
1024 return dyn_cast<Function>(Op<0>());
1027 /// getCalledValue - Get a pointer to the function that is invoked by this
1029 const Value *getCalledValue() const { return Op<0>(); }
1030 Value *getCalledValue() { return Op<0>(); }
1032 /// setCalledFunction - Set the function called.
1033 void setCalledFunction(Value* Fn) {
1037 // Methods for support type inquiry through isa, cast, and dyn_cast:
1038 static inline bool classof(const CallInst *) { return true; }
1039 static inline bool classof(const Instruction *I) {
1040 return I->getOpcode() == Instruction::Call;
1042 static inline bool classof(const Value *V) {
1043 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1046 // Shadow Value::setValueSubclassData with a private forwarding method so that
1047 // subclasses cannot accidentally use it.
1048 void setValueSubclassData(unsigned short D) {
1049 Value::setValueSubclassData(D);
1054 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1057 template<typename InputIterator>
1058 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1059 const Twine &NameStr, BasicBlock *InsertAtEnd)
1060 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1061 ->getElementType())->getReturnType(),
1063 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1064 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1065 init(Func, ArgBegin, ArgEnd, NameStr,
1066 typename std::iterator_traits<InputIterator>::iterator_category());
1069 template<typename InputIterator>
1070 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1071 const Twine &NameStr, Instruction *InsertBefore)
1072 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1073 ->getElementType())->getReturnType(),
1075 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1076 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1077 init(Func, ArgBegin, ArgEnd, NameStr,
1078 typename std::iterator_traits<InputIterator>::iterator_category());
1081 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1083 //===----------------------------------------------------------------------===//
1085 //===----------------------------------------------------------------------===//
1087 /// SelectInst - This class represents the LLVM 'select' instruction.
1089 class SelectInst : public Instruction {
1090 void init(Value *C, Value *S1, Value *S2) {
1091 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1097 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1098 Instruction *InsertBefore)
1099 : Instruction(S1->getType(), Instruction::Select,
1100 &Op<0>(), 3, InsertBefore) {
1104 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1105 BasicBlock *InsertAtEnd)
1106 : Instruction(S1->getType(), Instruction::Select,
1107 &Op<0>(), 3, InsertAtEnd) {
1112 virtual SelectInst *clone_impl() const;
1114 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1115 const Twine &NameStr = "",
1116 Instruction *InsertBefore = 0) {
1117 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1119 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1120 const Twine &NameStr,
1121 BasicBlock *InsertAtEnd) {
1122 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1125 const Value *getCondition() const { return Op<0>(); }
1126 const Value *getTrueValue() const { return Op<1>(); }
1127 const Value *getFalseValue() const { return Op<2>(); }
1128 Value *getCondition() { return Op<0>(); }
1129 Value *getTrueValue() { return Op<1>(); }
1130 Value *getFalseValue() { return Op<2>(); }
1132 /// areInvalidOperands - Return a string if the specified operands are invalid
1133 /// for a select operation, otherwise return null.
1134 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1136 /// Transparently provide more efficient getOperand methods.
1137 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1139 OtherOps getOpcode() const {
1140 return static_cast<OtherOps>(Instruction::getOpcode());
1143 // Methods for support type inquiry through isa, cast, and dyn_cast:
1144 static inline bool classof(const SelectInst *) { return true; }
1145 static inline bool classof(const Instruction *I) {
1146 return I->getOpcode() == Instruction::Select;
1148 static inline bool classof(const Value *V) {
1149 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1154 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1157 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1159 //===----------------------------------------------------------------------===//
1161 //===----------------------------------------------------------------------===//
1163 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1164 /// an argument of the specified type given a va_list and increments that list
1166 class VAArgInst : public UnaryInstruction {
1168 virtual VAArgInst *clone_impl() const;
1171 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1172 Instruction *InsertBefore = 0)
1173 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1176 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1177 BasicBlock *InsertAtEnd)
1178 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1182 // Methods for support type inquiry through isa, cast, and dyn_cast:
1183 static inline bool classof(const VAArgInst *) { return true; }
1184 static inline bool classof(const Instruction *I) {
1185 return I->getOpcode() == VAArg;
1187 static inline bool classof(const Value *V) {
1188 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1192 //===----------------------------------------------------------------------===//
1193 // ExtractElementInst Class
1194 //===----------------------------------------------------------------------===//
1196 /// ExtractElementInst - This instruction extracts a single (scalar)
1197 /// element from a VectorType value
1199 class ExtractElementInst : public Instruction {
1200 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1201 Instruction *InsertBefore = 0);
1202 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1203 BasicBlock *InsertAtEnd);
1205 virtual ExtractElementInst *clone_impl() const;
1208 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1209 const Twine &NameStr = "",
1210 Instruction *InsertBefore = 0) {
1211 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1213 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1214 const Twine &NameStr,
1215 BasicBlock *InsertAtEnd) {
1216 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1219 /// isValidOperands - Return true if an extractelement instruction can be
1220 /// formed with the specified operands.
1221 static bool isValidOperands(const Value *Vec, const Value *Idx);
1223 Value *getVectorOperand() { return Op<0>(); }
1224 Value *getIndexOperand() { return Op<1>(); }
1225 const Value *getVectorOperand() const { return Op<0>(); }
1226 const Value *getIndexOperand() const { return Op<1>(); }
1228 const VectorType *getVectorOperandType() const {
1229 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1233 /// Transparently provide more efficient getOperand methods.
1234 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1236 // Methods for support type inquiry through isa, cast, and dyn_cast:
1237 static inline bool classof(const ExtractElementInst *) { return true; }
1238 static inline bool classof(const Instruction *I) {
1239 return I->getOpcode() == Instruction::ExtractElement;
1241 static inline bool classof(const Value *V) {
1242 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1247 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1250 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1252 //===----------------------------------------------------------------------===//
1253 // InsertElementInst Class
1254 //===----------------------------------------------------------------------===//
1256 /// InsertElementInst - This instruction inserts a single (scalar)
1257 /// element into a VectorType value
1259 class InsertElementInst : public Instruction {
1260 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1261 const Twine &NameStr = "",
1262 Instruction *InsertBefore = 0);
1263 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1264 const Twine &NameStr, BasicBlock *InsertAtEnd);
1266 virtual InsertElementInst *clone_impl() const;
1269 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1270 const Twine &NameStr = "",
1271 Instruction *InsertBefore = 0) {
1272 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1274 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1275 const Twine &NameStr,
1276 BasicBlock *InsertAtEnd) {
1277 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1280 /// isValidOperands - Return true if an insertelement instruction can be
1281 /// formed with the specified operands.
1282 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1285 /// getType - Overload to return most specific vector type.
1287 const VectorType *getType() const {
1288 return reinterpret_cast<const VectorType*>(Instruction::getType());
1291 /// Transparently provide more efficient getOperand methods.
1292 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1294 // Methods for support type inquiry through isa, cast, and dyn_cast:
1295 static inline bool classof(const InsertElementInst *) { return true; }
1296 static inline bool classof(const Instruction *I) {
1297 return I->getOpcode() == Instruction::InsertElement;
1299 static inline bool classof(const Value *V) {
1300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1305 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1308 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1310 //===----------------------------------------------------------------------===//
1311 // ShuffleVectorInst Class
1312 //===----------------------------------------------------------------------===//
1314 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1317 class ShuffleVectorInst : public Instruction {
1319 virtual ShuffleVectorInst *clone_impl() const;
1322 // allocate space for exactly three operands
1323 void *operator new(size_t s) {
1324 return User::operator new(s, 3);
1326 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1327 const Twine &NameStr = "",
1328 Instruction *InsertBefor = 0);
1329 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1330 const Twine &NameStr, BasicBlock *InsertAtEnd);
1332 /// isValidOperands - Return true if a shufflevector instruction can be
1333 /// formed with the specified operands.
1334 static bool isValidOperands(const Value *V1, const Value *V2,
1337 /// getType - Overload to return most specific vector type.
1339 const VectorType *getType() const {
1340 return reinterpret_cast<const VectorType*>(Instruction::getType());
1343 /// Transparently provide more efficient getOperand methods.
1344 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1346 /// getMaskValue - Return the index from the shuffle mask for the specified
1347 /// output result. This is either -1 if the element is undef or a number less
1348 /// than 2*numelements.
1349 int getMaskValue(unsigned i) const;
1351 // Methods for support type inquiry through isa, cast, and dyn_cast:
1352 static inline bool classof(const ShuffleVectorInst *) { return true; }
1353 static inline bool classof(const Instruction *I) {
1354 return I->getOpcode() == Instruction::ShuffleVector;
1356 static inline bool classof(const Value *V) {
1357 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1362 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1365 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1367 //===----------------------------------------------------------------------===//
1368 // ExtractValueInst Class
1369 //===----------------------------------------------------------------------===//
1371 /// ExtractValueInst - This instruction extracts a struct member or array
1372 /// element value from an aggregate value.
1374 class ExtractValueInst : public UnaryInstruction {
1375 SmallVector<unsigned, 4> Indices;
1377 ExtractValueInst(const ExtractValueInst &EVI);
1378 void init(const unsigned *Idx, unsigned NumIdx,
1379 const Twine &NameStr);
1380 void init(unsigned Idx, const Twine &NameStr);
1382 template<typename InputIterator>
1383 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1384 const Twine &NameStr,
1385 // This argument ensures that we have an iterator we can
1386 // do arithmetic on in constant time
1387 std::random_access_iterator_tag) {
1388 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1390 // There's no fundamental reason why we require at least one index
1391 // (other than weirdness with &*IdxBegin being invalid; see
1392 // getelementptr's init routine for example). But there's no
1393 // present need to support it.
1394 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1396 // This requires that the iterator points to contiguous memory.
1397 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1398 // we have to build an array here
1401 /// getIndexedType - Returns the type of the element that would be extracted
1402 /// with an extractvalue instruction with the specified parameters.
1404 /// Null is returned if the indices are invalid for the specified
1407 static const Type *getIndexedType(const Type *Agg,
1408 const unsigned *Idx, unsigned NumIdx);
1410 template<typename InputIterator>
1411 static const Type *getIndexedType(const Type *Ptr,
1412 InputIterator IdxBegin,
1413 InputIterator IdxEnd,
1414 // This argument ensures that we
1415 // have an iterator we can do
1416 // arithmetic on in constant time
1417 std::random_access_iterator_tag) {
1418 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1421 // This requires that the iterator points to contiguous memory.
1422 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1424 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1427 /// Constructors - Create a extractvalue instruction with a base aggregate
1428 /// value and a list of indices. The first ctor can optionally insert before
1429 /// an existing instruction, the second appends the new instruction to the
1430 /// specified BasicBlock.
1431 template<typename InputIterator>
1432 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1433 InputIterator IdxEnd,
1434 const Twine &NameStr,
1435 Instruction *InsertBefore);
1436 template<typename InputIterator>
1437 inline ExtractValueInst(Value *Agg,
1438 InputIterator IdxBegin, InputIterator IdxEnd,
1439 const Twine &NameStr, BasicBlock *InsertAtEnd);
1441 // allocate space for exactly one operand
1442 void *operator new(size_t s) {
1443 return User::operator new(s, 1);
1446 virtual ExtractValueInst *clone_impl() const;
1449 template<typename InputIterator>
1450 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1451 InputIterator IdxEnd,
1452 const Twine &NameStr = "",
1453 Instruction *InsertBefore = 0) {
1455 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1457 template<typename InputIterator>
1458 static ExtractValueInst *Create(Value *Agg,
1459 InputIterator IdxBegin, InputIterator IdxEnd,
1460 const Twine &NameStr,
1461 BasicBlock *InsertAtEnd) {
1462 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1465 /// Constructors - These two creators are convenience methods because one
1466 /// index extractvalue instructions are much more common than those with
1468 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1469 const Twine &NameStr = "",
1470 Instruction *InsertBefore = 0) {
1471 unsigned Idxs[1] = { Idx };
1472 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1474 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1475 const Twine &NameStr,
1476 BasicBlock *InsertAtEnd) {
1477 unsigned Idxs[1] = { Idx };
1478 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1481 /// getIndexedType - Returns the type of the element that would be extracted
1482 /// with an extractvalue instruction with the specified parameters.
1484 /// Null is returned if the indices are invalid for the specified
1487 template<typename InputIterator>
1488 static const Type *getIndexedType(const Type *Ptr,
1489 InputIterator IdxBegin,
1490 InputIterator IdxEnd) {
1491 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1492 typename std::iterator_traits<InputIterator>::
1493 iterator_category());
1495 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1497 typedef const unsigned* idx_iterator;
1498 inline idx_iterator idx_begin() const { return Indices.begin(); }
1499 inline idx_iterator idx_end() const { return Indices.end(); }
1501 Value *getAggregateOperand() {
1502 return getOperand(0);
1504 const Value *getAggregateOperand() const {
1505 return getOperand(0);
1507 static unsigned getAggregateOperandIndex() {
1508 return 0U; // get index for modifying correct operand
1511 unsigned getNumIndices() const { // Note: always non-negative
1512 return (unsigned)Indices.size();
1515 bool hasIndices() const {
1519 // Methods for support type inquiry through isa, cast, and dyn_cast:
1520 static inline bool classof(const ExtractValueInst *) { return true; }
1521 static inline bool classof(const Instruction *I) {
1522 return I->getOpcode() == Instruction::ExtractValue;
1524 static inline bool classof(const Value *V) {
1525 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1529 template<typename InputIterator>
1530 ExtractValueInst::ExtractValueInst(Value *Agg,
1531 InputIterator IdxBegin,
1532 InputIterator IdxEnd,
1533 const Twine &NameStr,
1534 Instruction *InsertBefore)
1535 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1537 ExtractValue, Agg, InsertBefore) {
1538 init(IdxBegin, IdxEnd, NameStr,
1539 typename std::iterator_traits<InputIterator>::iterator_category());
1541 template<typename InputIterator>
1542 ExtractValueInst::ExtractValueInst(Value *Agg,
1543 InputIterator IdxBegin,
1544 InputIterator IdxEnd,
1545 const Twine &NameStr,
1546 BasicBlock *InsertAtEnd)
1547 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1549 ExtractValue, Agg, InsertAtEnd) {
1550 init(IdxBegin, IdxEnd, NameStr,
1551 typename std::iterator_traits<InputIterator>::iterator_category());
1555 //===----------------------------------------------------------------------===//
1556 // InsertValueInst Class
1557 //===----------------------------------------------------------------------===//
1559 /// InsertValueInst - This instruction inserts a struct field of array element
1560 /// value into an aggregate value.
1562 class InsertValueInst : public Instruction {
1563 SmallVector<unsigned, 4> Indices;
1565 void *operator new(size_t, unsigned); // Do not implement
1566 InsertValueInst(const InsertValueInst &IVI);
1567 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1568 const Twine &NameStr);
1569 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1571 template<typename InputIterator>
1572 void init(Value *Agg, Value *Val,
1573 InputIterator IdxBegin, InputIterator IdxEnd,
1574 const Twine &NameStr,
1575 // This argument ensures that we have an iterator we can
1576 // do arithmetic on in constant time
1577 std::random_access_iterator_tag) {
1578 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1580 // There's no fundamental reason why we require at least one index
1581 // (other than weirdness with &*IdxBegin being invalid; see
1582 // getelementptr's init routine for example). But there's no
1583 // present need to support it.
1584 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1586 // This requires that the iterator points to contiguous memory.
1587 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1588 // we have to build an array here
1591 /// Constructors - Create a insertvalue instruction with a base aggregate
1592 /// value, a value to insert, and a list of indices. The first ctor can
1593 /// optionally insert before an existing instruction, the second appends
1594 /// the new instruction to the specified BasicBlock.
1595 template<typename InputIterator>
1596 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1597 InputIterator IdxEnd,
1598 const Twine &NameStr,
1599 Instruction *InsertBefore);
1600 template<typename InputIterator>
1601 inline InsertValueInst(Value *Agg, Value *Val,
1602 InputIterator IdxBegin, InputIterator IdxEnd,
1603 const Twine &NameStr, BasicBlock *InsertAtEnd);
1605 /// Constructors - These two constructors are convenience methods because one
1606 /// and two index insertvalue instructions are so common.
1607 InsertValueInst(Value *Agg, Value *Val,
1608 unsigned Idx, const Twine &NameStr = "",
1609 Instruction *InsertBefore = 0);
1610 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1611 const Twine &NameStr, BasicBlock *InsertAtEnd);
1613 virtual InsertValueInst *clone_impl() const;
1615 // allocate space for exactly two operands
1616 void *operator new(size_t s) {
1617 return User::operator new(s, 2);
1620 template<typename InputIterator>
1621 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1622 InputIterator IdxEnd,
1623 const Twine &NameStr = "",
1624 Instruction *InsertBefore = 0) {
1625 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1626 NameStr, InsertBefore);
1628 template<typename InputIterator>
1629 static InsertValueInst *Create(Value *Agg, Value *Val,
1630 InputIterator IdxBegin, InputIterator IdxEnd,
1631 const Twine &NameStr,
1632 BasicBlock *InsertAtEnd) {
1633 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1634 NameStr, InsertAtEnd);
1637 /// Constructors - These two creators are convenience methods because one
1638 /// index insertvalue instructions are much more common than those with
1640 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1641 const Twine &NameStr = "",
1642 Instruction *InsertBefore = 0) {
1643 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1645 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1646 const Twine &NameStr,
1647 BasicBlock *InsertAtEnd) {
1648 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1651 /// Transparently provide more efficient getOperand methods.
1652 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1654 typedef const unsigned* idx_iterator;
1655 inline idx_iterator idx_begin() const { return Indices.begin(); }
1656 inline idx_iterator idx_end() const { return Indices.end(); }
1658 Value *getAggregateOperand() {
1659 return getOperand(0);
1661 const Value *getAggregateOperand() const {
1662 return getOperand(0);
1664 static unsigned getAggregateOperandIndex() {
1665 return 0U; // get index for modifying correct operand
1668 Value *getInsertedValueOperand() {
1669 return getOperand(1);
1671 const Value *getInsertedValueOperand() const {
1672 return getOperand(1);
1674 static unsigned getInsertedValueOperandIndex() {
1675 return 1U; // get index for modifying correct operand
1678 unsigned getNumIndices() const { // Note: always non-negative
1679 return (unsigned)Indices.size();
1682 bool hasIndices() const {
1686 // Methods for support type inquiry through isa, cast, and dyn_cast:
1687 static inline bool classof(const InsertValueInst *) { return true; }
1688 static inline bool classof(const Instruction *I) {
1689 return I->getOpcode() == Instruction::InsertValue;
1691 static inline bool classof(const Value *V) {
1692 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1697 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1700 template<typename InputIterator>
1701 InsertValueInst::InsertValueInst(Value *Agg,
1703 InputIterator IdxBegin,
1704 InputIterator IdxEnd,
1705 const Twine &NameStr,
1706 Instruction *InsertBefore)
1707 : Instruction(Agg->getType(), InsertValue,
1708 OperandTraits<InsertValueInst>::op_begin(this),
1710 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1711 typename std::iterator_traits<InputIterator>::iterator_category());
1713 template<typename InputIterator>
1714 InsertValueInst::InsertValueInst(Value *Agg,
1716 InputIterator IdxBegin,
1717 InputIterator IdxEnd,
1718 const Twine &NameStr,
1719 BasicBlock *InsertAtEnd)
1720 : Instruction(Agg->getType(), InsertValue,
1721 OperandTraits<InsertValueInst>::op_begin(this),
1723 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1724 typename std::iterator_traits<InputIterator>::iterator_category());
1727 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1729 //===----------------------------------------------------------------------===//
1731 //===----------------------------------------------------------------------===//
1733 // PHINode - The PHINode class is used to represent the magical mystical PHI
1734 // node, that can not exist in nature, but can be synthesized in a computer
1735 // scientist's overactive imagination.
1737 class PHINode : public Instruction {
1738 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1739 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1740 /// the number actually in use.
1741 unsigned ReservedSpace;
1742 PHINode(const PHINode &PN);
1743 // allocate space for exactly zero operands
1744 void *operator new(size_t s) {
1745 return User::operator new(s, 0);
1747 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1748 Instruction *InsertBefore = 0)
1749 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1754 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1755 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1760 virtual PHINode *clone_impl() const;
1762 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1763 Instruction *InsertBefore = 0) {
1764 return new PHINode(Ty, NameStr, InsertBefore);
1766 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1767 BasicBlock *InsertAtEnd) {
1768 return new PHINode(Ty, NameStr, InsertAtEnd);
1772 /// reserveOperandSpace - This method can be used to avoid repeated
1773 /// reallocation of PHI operand lists by reserving space for the correct
1774 /// number of operands before adding them. Unlike normal vector reserves,
1775 /// this method can also be used to trim the operand space.
1776 void reserveOperandSpace(unsigned NumValues) {
1777 resizeOperands(NumValues*2);
1780 /// Provide fast operand accessors
1781 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1783 /// getNumIncomingValues - Return the number of incoming edges
1785 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1787 /// getIncomingValue - Return incoming value number x
1789 Value *getIncomingValue(unsigned i) const {
1790 assert(i*2 < getNumOperands() && "Invalid value number!");
1791 return getOperand(i*2);
1793 void setIncomingValue(unsigned i, Value *V) {
1794 assert(i*2 < getNumOperands() && "Invalid value number!");
1797 static unsigned getOperandNumForIncomingValue(unsigned i) {
1800 static unsigned getIncomingValueNumForOperand(unsigned i) {
1801 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1805 /// getIncomingBlock - Return incoming basic block #i.
1807 BasicBlock *getIncomingBlock(unsigned i) const {
1808 return cast<BasicBlock>(getOperand(i*2+1));
1811 /// getIncomingBlock - Return incoming basic block corresponding
1812 /// to an operand of the PHI.
1814 BasicBlock *getIncomingBlock(const Use &U) const {
1815 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1816 return cast<BasicBlock>((&U + 1)->get());
1819 /// getIncomingBlock - Return incoming basic block corresponding
1820 /// to value use iterator.
1822 template <typename U>
1823 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1824 return getIncomingBlock(I.getUse());
1828 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1829 setOperand(i*2+1, (Value*)BB);
1831 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1834 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1835 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1839 /// addIncoming - Add an incoming value to the end of the PHI list
1841 void addIncoming(Value *V, BasicBlock *BB) {
1842 assert(V && "PHI node got a null value!");
1843 assert(BB && "PHI node got a null basic block!");
1844 assert(getType() == V->getType() &&
1845 "All operands to PHI node must be the same type as the PHI node!");
1846 unsigned OpNo = NumOperands;
1847 if (OpNo+2 > ReservedSpace)
1848 resizeOperands(0); // Get more space!
1849 // Initialize some new operands.
1850 NumOperands = OpNo+2;
1851 OperandList[OpNo] = V;
1852 OperandList[OpNo+1] = (Value*)BB;
1855 /// removeIncomingValue - Remove an incoming value. This is useful if a
1856 /// predecessor basic block is deleted. The value removed is returned.
1858 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1859 /// is true), the PHI node is destroyed and any uses of it are replaced with
1860 /// dummy values. The only time there should be zero incoming values to a PHI
1861 /// node is when the block is dead, so this strategy is sound.
1863 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1865 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1866 int Idx = getBasicBlockIndex(BB);
1867 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1868 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1871 /// getBasicBlockIndex - Return the first index of the specified basic
1872 /// block in the value list for this PHI. Returns -1 if no instance.
1874 int getBasicBlockIndex(const BasicBlock *BB) const {
1875 Use *OL = OperandList;
1876 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1877 if (OL[i+1].get() == (const Value*)BB) return i/2;
1881 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1882 return getIncomingValue(getBasicBlockIndex(BB));
1885 /// hasConstantValue - If the specified PHI node always merges together the
1886 /// same value, return the value, otherwise return null.
1888 /// If the PHI has undef operands, but all the rest of the operands are
1889 /// some unique value, return that value if it can be proved that the
1890 /// value dominates the PHI. If DT is null, use a conservative check,
1891 /// otherwise use DT to test for dominance.
1893 Value *hasConstantValue(DominatorTree *DT = 0) const;
1895 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1896 static inline bool classof(const PHINode *) { return true; }
1897 static inline bool classof(const Instruction *I) {
1898 return I->getOpcode() == Instruction::PHI;
1900 static inline bool classof(const Value *V) {
1901 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1904 void resizeOperands(unsigned NumOperands);
1908 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1911 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1914 //===----------------------------------------------------------------------===//
1916 //===----------------------------------------------------------------------===//
1918 //===---------------------------------------------------------------------------
1919 /// ReturnInst - Return a value (possibly void), from a function. Execution
1920 /// does not continue in this function any longer.
1922 class ReturnInst : public TerminatorInst {
1923 ReturnInst(const ReturnInst &RI);
1926 // ReturnInst constructors:
1927 // ReturnInst() - 'ret void' instruction
1928 // ReturnInst( null) - 'ret void' instruction
1929 // ReturnInst(Value* X) - 'ret X' instruction
1930 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1931 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1932 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1933 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1935 // NOTE: If the Value* passed is of type void then the constructor behaves as
1936 // if it was passed NULL.
1937 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1938 Instruction *InsertBefore = 0);
1939 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1940 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1942 virtual ReturnInst *clone_impl() const;
1944 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1945 Instruction *InsertBefore = 0) {
1946 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1948 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1949 BasicBlock *InsertAtEnd) {
1950 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1952 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1953 return new(0) ReturnInst(C, InsertAtEnd);
1955 virtual ~ReturnInst();
1957 /// Provide fast operand accessors
1958 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1960 /// Convenience accessor
1961 Value *getReturnValue(unsigned n = 0) const {
1962 return n < getNumOperands()
1967 unsigned getNumSuccessors() const { return 0; }
1969 // Methods for support type inquiry through isa, cast, and dyn_cast:
1970 static inline bool classof(const ReturnInst *) { return true; }
1971 static inline bool classof(const Instruction *I) {
1972 return (I->getOpcode() == Instruction::Ret);
1974 static inline bool classof(const Value *V) {
1975 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1978 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1979 virtual unsigned getNumSuccessorsV() const;
1980 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1984 struct OperandTraits<ReturnInst> : public OptionalOperandTraits<> {
1987 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
1989 //===----------------------------------------------------------------------===//
1991 //===----------------------------------------------------------------------===//
1993 //===---------------------------------------------------------------------------
1994 /// BranchInst - Conditional or Unconditional Branch instruction.
1996 class BranchInst : public TerminatorInst {
1997 /// Ops list - Branches are strange. The operands are ordered:
1998 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
1999 /// they don't have to check for cond/uncond branchness. These are mostly
2000 /// accessed relative from op_end().
2001 BranchInst(const BranchInst &BI);
2003 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2004 // BranchInst(BB *B) - 'br B'
2005 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2006 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2007 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2008 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2009 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2010 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2011 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2012 Instruction *InsertBefore = 0);
2013 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2014 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2015 BasicBlock *InsertAtEnd);
2017 virtual BranchInst *clone_impl() const;
2019 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2020 return new(1, true) BranchInst(IfTrue, InsertBefore);
2022 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2023 Value *Cond, Instruction *InsertBefore = 0) {
2024 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2026 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2027 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2029 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2030 Value *Cond, BasicBlock *InsertAtEnd) {
2031 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2036 /// Transparently provide more efficient getOperand methods.
2037 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2039 bool isUnconditional() const { return getNumOperands() == 1; }
2040 bool isConditional() const { return getNumOperands() == 3; }
2042 Value *getCondition() const {
2043 assert(isConditional() && "Cannot get condition of an uncond branch!");
2047 void setCondition(Value *V) {
2048 assert(isConditional() && "Cannot set condition of unconditional branch!");
2052 // setUnconditionalDest - Change the current branch to an unconditional branch
2053 // targeting the specified block.
2054 // FIXME: Eliminate this ugly method.
2055 void setUnconditionalDest(BasicBlock *Dest) {
2056 Op<-1>() = (Value*)Dest;
2057 if (isConditional()) { // Convert this to an uncond branch.
2061 OperandList = op_begin();
2065 unsigned getNumSuccessors() const { return 1+isConditional(); }
2067 BasicBlock *getSuccessor(unsigned i) const {
2068 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2069 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2072 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2073 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2074 *(&Op<-1>() - idx) = (Value*)NewSucc;
2077 // Methods for support type inquiry through isa, cast, and dyn_cast:
2078 static inline bool classof(const BranchInst *) { return true; }
2079 static inline bool classof(const Instruction *I) {
2080 return (I->getOpcode() == Instruction::Br);
2082 static inline bool classof(const Value *V) {
2083 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2086 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2087 virtual unsigned getNumSuccessorsV() const;
2088 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2092 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2094 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2096 //===----------------------------------------------------------------------===//
2098 //===----------------------------------------------------------------------===//
2100 //===---------------------------------------------------------------------------
2101 /// SwitchInst - Multiway switch
2103 class SwitchInst : public TerminatorInst {
2104 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2105 unsigned ReservedSpace;
2106 // Operand[0] = Value to switch on
2107 // Operand[1] = Default basic block destination
2108 // Operand[2n ] = Value to match
2109 // Operand[2n+1] = BasicBlock to go to on match
2110 SwitchInst(const SwitchInst &SI);
2111 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2112 void resizeOperands(unsigned No);
2113 // allocate space for exactly zero operands
2114 void *operator new(size_t s) {
2115 return User::operator new(s, 0);
2117 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2118 /// switch on and a default destination. The number of additional cases can
2119 /// be specified here to make memory allocation more efficient. This
2120 /// constructor can also autoinsert before another instruction.
2121 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2122 Instruction *InsertBefore);
2124 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2125 /// switch on and a default destination. The number of additional cases can
2126 /// be specified here to make memory allocation more efficient. This
2127 /// constructor also autoinserts at the end of the specified BasicBlock.
2128 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2129 BasicBlock *InsertAtEnd);
2131 virtual SwitchInst *clone_impl() const;
2133 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2134 unsigned NumCases, Instruction *InsertBefore = 0) {
2135 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2137 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2138 unsigned NumCases, BasicBlock *InsertAtEnd) {
2139 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2143 /// Provide fast operand accessors
2144 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2146 // Accessor Methods for Switch stmt
2147 Value *getCondition() const { return getOperand(0); }
2148 void setCondition(Value *V) { setOperand(0, V); }
2150 BasicBlock *getDefaultDest() const {
2151 return cast<BasicBlock>(getOperand(1));
2154 /// getNumCases - return the number of 'cases' in this switch instruction.
2155 /// Note that case #0 is always the default case.
2156 unsigned getNumCases() const {
2157 return getNumOperands()/2;
2160 /// getCaseValue - Return the specified case value. Note that case #0, the
2161 /// default destination, does not have a case value.
2162 ConstantInt *getCaseValue(unsigned i) {
2163 assert(i && i < getNumCases() && "Illegal case value to get!");
2164 return getSuccessorValue(i);
2167 /// getCaseValue - Return the specified case value. Note that case #0, the
2168 /// default destination, does not have a case value.
2169 const ConstantInt *getCaseValue(unsigned i) const {
2170 assert(i && i < getNumCases() && "Illegal case value to get!");
2171 return getSuccessorValue(i);
2174 /// findCaseValue - Search all of the case values for the specified constant.
2175 /// If it is explicitly handled, return the case number of it, otherwise
2176 /// return 0 to indicate that it is handled by the default handler.
2177 unsigned findCaseValue(const ConstantInt *C) const {
2178 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2179 if (getCaseValue(i) == C)
2184 /// findCaseDest - Finds the unique case value for a given successor. Returns
2185 /// null if the successor is not found, not unique, or is the default case.
2186 ConstantInt *findCaseDest(BasicBlock *BB) {
2187 if (BB == getDefaultDest()) return NULL;
2189 ConstantInt *CI = NULL;
2190 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2191 if (getSuccessor(i) == BB) {
2192 if (CI) return NULL; // Multiple cases lead to BB.
2193 else CI = getCaseValue(i);
2199 /// addCase - Add an entry to the switch instruction...
2201 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2203 /// removeCase - This method removes the specified successor from the switch
2204 /// instruction. Note that this cannot be used to remove the default
2205 /// destination (successor #0).
2207 void removeCase(unsigned idx);
2209 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2210 BasicBlock *getSuccessor(unsigned idx) const {
2211 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2212 return cast<BasicBlock>(getOperand(idx*2+1));
2214 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2215 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2216 setOperand(idx*2+1, (Value*)NewSucc);
2219 // getSuccessorValue - Return the value associated with the specified
2221 ConstantInt *getSuccessorValue(unsigned idx) const {
2222 assert(idx < getNumSuccessors() && "Successor # out of range!");
2223 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2226 // Methods for support type inquiry through isa, cast, and dyn_cast:
2227 static inline bool classof(const SwitchInst *) { return true; }
2228 static inline bool classof(const Instruction *I) {
2229 return I->getOpcode() == Instruction::Switch;
2231 static inline bool classof(const Value *V) {
2232 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2235 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2236 virtual unsigned getNumSuccessorsV() const;
2237 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2241 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2244 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2247 //===----------------------------------------------------------------------===//
2248 // IndirectBrInst Class
2249 //===----------------------------------------------------------------------===//
2251 //===---------------------------------------------------------------------------
2252 /// IndirectBrInst - Indirect Branch Instruction.
2254 class IndirectBrInst : public TerminatorInst {
2255 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2256 unsigned ReservedSpace;
2257 // Operand[0] = Value to switch on
2258 // Operand[1] = Default basic block destination
2259 // Operand[2n ] = Value to match
2260 // Operand[2n+1] = BasicBlock to go to on match
2261 IndirectBrInst(const IndirectBrInst &IBI);
2262 void init(Value *Address, unsigned NumDests);
2263 void resizeOperands(unsigned No);
2264 // allocate space for exactly zero operands
2265 void *operator new(size_t s) {
2266 return User::operator new(s, 0);
2268 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2269 /// Address to jump to. The number of expected destinations can be specified
2270 /// here to make memory allocation more efficient. This constructor can also
2271 /// autoinsert before another instruction.
2272 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2274 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2275 /// Address to jump to. The number of expected destinations can be specified
2276 /// here to make memory allocation more efficient. This constructor also
2277 /// autoinserts at the end of the specified BasicBlock.
2278 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2280 virtual IndirectBrInst *clone_impl() const;
2282 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2283 Instruction *InsertBefore = 0) {
2284 return new IndirectBrInst(Address, NumDests, InsertBefore);
2286 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2287 BasicBlock *InsertAtEnd) {
2288 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2292 /// Provide fast operand accessors.
2293 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2295 // Accessor Methods for IndirectBrInst instruction.
2296 Value *getAddress() { return getOperand(0); }
2297 const Value *getAddress() const { return getOperand(0); }
2298 void setAddress(Value *V) { setOperand(0, V); }
2301 /// getNumDestinations - return the number of possible destinations in this
2302 /// indirectbr instruction.
2303 unsigned getNumDestinations() const { return getNumOperands()-1; }
2305 /// getDestination - Return the specified destination.
2306 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2307 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2309 /// addDestination - Add a destination.
2311 void addDestination(BasicBlock *Dest);
2313 /// removeDestination - This method removes the specified successor from the
2314 /// indirectbr instruction.
2315 void removeDestination(unsigned i);
2317 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2318 BasicBlock *getSuccessor(unsigned i) const {
2319 return cast<BasicBlock>(getOperand(i+1));
2321 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2322 setOperand(i+1, (Value*)NewSucc);
2325 // Methods for support type inquiry through isa, cast, and dyn_cast:
2326 static inline bool classof(const IndirectBrInst *) { return true; }
2327 static inline bool classof(const Instruction *I) {
2328 return I->getOpcode() == Instruction::IndirectBr;
2330 static inline bool classof(const Value *V) {
2331 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2334 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2335 virtual unsigned getNumSuccessorsV() const;
2336 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2340 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2343 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2346 //===----------------------------------------------------------------------===//
2348 //===----------------------------------------------------------------------===//
2350 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2351 /// calling convention of the call.
2353 class InvokeInst : public TerminatorInst {
2354 AttrListPtr AttributeList;
2355 InvokeInst(const InvokeInst &BI);
2356 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2357 Value* const *Args, unsigned NumArgs);
2359 template<typename InputIterator>
2360 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2361 InputIterator ArgBegin, InputIterator ArgEnd,
2362 const Twine &NameStr,
2363 // This argument ensures that we have an iterator we can
2364 // do arithmetic on in constant time
2365 std::random_access_iterator_tag) {
2366 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2368 // This requires that the iterator points to contiguous memory.
2369 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2373 /// Construct an InvokeInst given a range of arguments.
2374 /// InputIterator must be a random-access iterator pointing to
2375 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2376 /// made for random-accessness but not for contiguous storage as
2377 /// that would incur runtime overhead.
2379 /// @brief Construct an InvokeInst from a range of arguments
2380 template<typename InputIterator>
2381 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2382 InputIterator ArgBegin, InputIterator ArgEnd,
2384 const Twine &NameStr, Instruction *InsertBefore);
2386 /// Construct an InvokeInst given a range of arguments.
2387 /// InputIterator must be a random-access iterator pointing to
2388 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2389 /// made for random-accessness but not for contiguous storage as
2390 /// that would incur runtime overhead.
2392 /// @brief Construct an InvokeInst from a range of arguments
2393 template<typename InputIterator>
2394 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2395 InputIterator ArgBegin, InputIterator ArgEnd,
2397 const Twine &NameStr, BasicBlock *InsertAtEnd);
2399 virtual InvokeInst *clone_impl() const;
2401 template<typename InputIterator>
2402 static InvokeInst *Create(Value *Func,
2403 BasicBlock *IfNormal, BasicBlock *IfException,
2404 InputIterator ArgBegin, InputIterator ArgEnd,
2405 const Twine &NameStr = "",
2406 Instruction *InsertBefore = 0) {
2407 unsigned Values(ArgEnd - ArgBegin + 3);
2408 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2409 Values, NameStr, InsertBefore);
2411 template<typename InputIterator>
2412 static InvokeInst *Create(Value *Func,
2413 BasicBlock *IfNormal, BasicBlock *IfException,
2414 InputIterator ArgBegin, InputIterator ArgEnd,
2415 const Twine &NameStr,
2416 BasicBlock *InsertAtEnd) {
2417 unsigned Values(ArgEnd - ArgBegin + 3);
2418 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2419 Values, NameStr, InsertAtEnd);
2422 /// Provide fast operand accessors
2423 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2425 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2427 CallingConv::ID getCallingConv() const {
2428 return static_cast<CallingConv::ID>(getSubclassDataFromValue());
2430 void setCallingConv(CallingConv::ID CC) {
2431 setValueSubclassData(static_cast<unsigned>(CC));
2434 /// getAttributes - Return the parameter attributes for this invoke.
2436 const AttrListPtr &getAttributes() const { return AttributeList; }
2438 /// setAttributes - Set the parameter attributes for this invoke.
2440 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2442 /// addAttribute - adds the attribute to the list of attributes.
2443 void addAttribute(unsigned i, Attributes attr);
2445 /// removeAttribute - removes the attribute from the list of attributes.
2446 void removeAttribute(unsigned i, Attributes attr);
2448 /// @brief Determine whether the call or the callee has the given attribute.
2449 bool paramHasAttr(unsigned i, Attributes attr) const;
2451 /// @brief Extract the alignment for a call or parameter (0=unknown).
2452 unsigned getParamAlignment(unsigned i) const {
2453 return AttributeList.getParamAlignment(i);
2456 /// @brief Determine if the call does not access memory.
2457 bool doesNotAccessMemory() const {
2458 return paramHasAttr(~0, Attribute::ReadNone);
2460 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2461 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2462 else removeAttribute(~0, Attribute::ReadNone);
2465 /// @brief Determine if the call does not access or only reads memory.
2466 bool onlyReadsMemory() const {
2467 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2469 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2470 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2471 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2474 /// @brief Determine if the call cannot return.
2475 bool doesNotReturn() const {
2476 return paramHasAttr(~0, Attribute::NoReturn);
2478 void setDoesNotReturn(bool DoesNotReturn = true) {
2479 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2480 else removeAttribute(~0, Attribute::NoReturn);
2483 /// @brief Determine if the call cannot unwind.
2484 bool doesNotThrow() const {
2485 return paramHasAttr(~0, Attribute::NoUnwind);
2487 void setDoesNotThrow(bool DoesNotThrow = true) {
2488 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2489 else removeAttribute(~0, Attribute::NoUnwind);
2492 /// @brief Determine if the call returns a structure through first
2493 /// pointer argument.
2494 bool hasStructRetAttr() const {
2495 // Be friendly and also check the callee.
2496 return paramHasAttr(1, Attribute::StructRet);
2499 /// @brief Determine if any call argument is an aggregate passed by value.
2500 bool hasByValArgument() const {
2501 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2504 /// getCalledFunction - Return the function called, or null if this is an
2505 /// indirect function invocation.
2507 Function *getCalledFunction() const {
2508 return dyn_cast<Function>(getOperand(0));
2511 /// getCalledValue - Get a pointer to the function that is invoked by this
2513 const Value *getCalledValue() const { return getOperand(0); }
2514 Value *getCalledValue() { return getOperand(0); }
2516 // get*Dest - Return the destination basic blocks...
2517 BasicBlock *getNormalDest() const {
2518 return cast<BasicBlock>(getOperand(1));
2520 BasicBlock *getUnwindDest() const {
2521 return cast<BasicBlock>(getOperand(2));
2523 void setNormalDest(BasicBlock *B) {
2524 setOperand(1, (Value*)B);
2527 void setUnwindDest(BasicBlock *B) {
2528 setOperand(2, (Value*)B);
2531 BasicBlock *getSuccessor(unsigned i) const {
2532 assert(i < 2 && "Successor # out of range for invoke!");
2533 return i == 0 ? getNormalDest() : getUnwindDest();
2536 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2537 assert(idx < 2 && "Successor # out of range for invoke!");
2538 setOperand(idx+1, (Value*)NewSucc);
2541 unsigned getNumSuccessors() const { return 2; }
2543 // Methods for support type inquiry through isa, cast, and dyn_cast:
2544 static inline bool classof(const InvokeInst *) { return true; }
2545 static inline bool classof(const Instruction *I) {
2546 return (I->getOpcode() == Instruction::Invoke);
2548 static inline bool classof(const Value *V) {
2549 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2552 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2553 virtual unsigned getNumSuccessorsV() const;
2554 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2556 // Shadow Value::setValueSubclassData with a private forwarding method so that
2557 // subclasses cannot accidentally use it.
2558 void setValueSubclassData(unsigned short D) {
2559 Value::setValueSubclassData(D);
2564 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2567 template<typename InputIterator>
2568 InvokeInst::InvokeInst(Value *Func,
2569 BasicBlock *IfNormal, BasicBlock *IfException,
2570 InputIterator ArgBegin, InputIterator ArgEnd,
2572 const Twine &NameStr, Instruction *InsertBefore)
2573 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2574 ->getElementType())->getReturnType(),
2575 Instruction::Invoke,
2576 OperandTraits<InvokeInst>::op_end(this) - Values,
2577 Values, InsertBefore) {
2578 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2579 typename std::iterator_traits<InputIterator>::iterator_category());
2581 template<typename InputIterator>
2582 InvokeInst::InvokeInst(Value *Func,
2583 BasicBlock *IfNormal, BasicBlock *IfException,
2584 InputIterator ArgBegin, InputIterator ArgEnd,
2586 const Twine &NameStr, BasicBlock *InsertAtEnd)
2587 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2588 ->getElementType())->getReturnType(),
2589 Instruction::Invoke,
2590 OperandTraits<InvokeInst>::op_end(this) - Values,
2591 Values, InsertAtEnd) {
2592 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2593 typename std::iterator_traits<InputIterator>::iterator_category());
2596 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2598 //===----------------------------------------------------------------------===//
2600 //===----------------------------------------------------------------------===//
2602 //===---------------------------------------------------------------------------
2603 /// UnwindInst - Immediately exit the current function, unwinding the stack
2604 /// until an invoke instruction is found.
2606 class UnwindInst : public TerminatorInst {
2607 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2609 virtual UnwindInst *clone_impl() const;
2611 // allocate space for exactly zero operands
2612 void *operator new(size_t s) {
2613 return User::operator new(s, 0);
2615 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2616 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2618 unsigned getNumSuccessors() const { return 0; }
2620 // Methods for support type inquiry through isa, cast, and dyn_cast:
2621 static inline bool classof(const UnwindInst *) { return true; }
2622 static inline bool classof(const Instruction *I) {
2623 return I->getOpcode() == Instruction::Unwind;
2625 static inline bool classof(const Value *V) {
2626 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2629 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2630 virtual unsigned getNumSuccessorsV() const;
2631 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2634 //===----------------------------------------------------------------------===//
2635 // UnreachableInst Class
2636 //===----------------------------------------------------------------------===//
2638 //===---------------------------------------------------------------------------
2639 /// UnreachableInst - This function has undefined behavior. In particular, the
2640 /// presence of this instruction indicates some higher level knowledge that the
2641 /// end of the block cannot be reached.
2643 class UnreachableInst : public TerminatorInst {
2644 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2646 virtual UnreachableInst *clone_impl() const;
2649 // allocate space for exactly zero operands
2650 void *operator new(size_t s) {
2651 return User::operator new(s, 0);
2653 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2654 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2656 unsigned getNumSuccessors() const { return 0; }
2658 // Methods for support type inquiry through isa, cast, and dyn_cast:
2659 static inline bool classof(const UnreachableInst *) { return true; }
2660 static inline bool classof(const Instruction *I) {
2661 return I->getOpcode() == Instruction::Unreachable;
2663 static inline bool classof(const Value *V) {
2664 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2667 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2668 virtual unsigned getNumSuccessorsV() const;
2669 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2672 //===----------------------------------------------------------------------===//
2674 //===----------------------------------------------------------------------===//
2676 /// @brief This class represents a truncation of integer types.
2677 class TruncInst : public CastInst {
2679 /// @brief Clone an identical TruncInst
2680 virtual TruncInst *clone_impl() const;
2683 /// @brief Constructor with insert-before-instruction semantics
2685 Value *S, ///< The value to be truncated
2686 const Type *Ty, ///< The (smaller) type to truncate to
2687 const Twine &NameStr = "", ///< A name for the new instruction
2688 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2691 /// @brief Constructor with insert-at-end-of-block semantics
2693 Value *S, ///< The value to be truncated
2694 const Type *Ty, ///< The (smaller) type to truncate to
2695 const Twine &NameStr, ///< A name for the new instruction
2696 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2699 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2700 static inline bool classof(const TruncInst *) { return true; }
2701 static inline bool classof(const Instruction *I) {
2702 return I->getOpcode() == Trunc;
2704 static inline bool classof(const Value *V) {
2705 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2709 //===----------------------------------------------------------------------===//
2711 //===----------------------------------------------------------------------===//
2713 /// @brief This class represents zero extension of integer types.
2714 class ZExtInst : public CastInst {
2716 /// @brief Clone an identical ZExtInst
2717 virtual ZExtInst *clone_impl() const;
2720 /// @brief Constructor with insert-before-instruction semantics
2722 Value *S, ///< The value to be zero extended
2723 const Type *Ty, ///< The type to zero extend to
2724 const Twine &NameStr = "", ///< A name for the new instruction
2725 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2728 /// @brief Constructor with insert-at-end semantics.
2730 Value *S, ///< The value to be zero extended
2731 const Type *Ty, ///< The type to zero extend to
2732 const Twine &NameStr, ///< A name for the new instruction
2733 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2736 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2737 static inline bool classof(const ZExtInst *) { return true; }
2738 static inline bool classof(const Instruction *I) {
2739 return I->getOpcode() == ZExt;
2741 static inline bool classof(const Value *V) {
2742 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2746 //===----------------------------------------------------------------------===//
2748 //===----------------------------------------------------------------------===//
2750 /// @brief This class represents a sign extension of integer types.
2751 class SExtInst : public CastInst {
2753 /// @brief Clone an identical SExtInst
2754 virtual SExtInst *clone_impl() const;
2757 /// @brief Constructor with insert-before-instruction semantics
2759 Value *S, ///< The value to be sign extended
2760 const Type *Ty, ///< The type to sign extend to
2761 const Twine &NameStr = "", ///< A name for the new instruction
2762 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2765 /// @brief Constructor with insert-at-end-of-block semantics
2767 Value *S, ///< The value to be sign extended
2768 const Type *Ty, ///< The type to sign extend to
2769 const Twine &NameStr, ///< A name for the new instruction
2770 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2773 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2774 static inline bool classof(const SExtInst *) { return true; }
2775 static inline bool classof(const Instruction *I) {
2776 return I->getOpcode() == SExt;
2778 static inline bool classof(const Value *V) {
2779 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2783 //===----------------------------------------------------------------------===//
2784 // FPTruncInst Class
2785 //===----------------------------------------------------------------------===//
2787 /// @brief This class represents a truncation of floating point types.
2788 class FPTruncInst : public CastInst {
2790 /// @brief Clone an identical FPTruncInst
2791 virtual FPTruncInst *clone_impl() const;
2794 /// @brief Constructor with insert-before-instruction semantics
2796 Value *S, ///< The value to be truncated
2797 const Type *Ty, ///< The type to truncate to
2798 const Twine &NameStr = "", ///< A name for the new instruction
2799 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2802 /// @brief Constructor with insert-before-instruction semantics
2804 Value *S, ///< The value to be truncated
2805 const Type *Ty, ///< The type to truncate to
2806 const Twine &NameStr, ///< A name for the new instruction
2807 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2810 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2811 static inline bool classof(const FPTruncInst *) { return true; }
2812 static inline bool classof(const Instruction *I) {
2813 return I->getOpcode() == FPTrunc;
2815 static inline bool classof(const Value *V) {
2816 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2820 //===----------------------------------------------------------------------===//
2822 //===----------------------------------------------------------------------===//
2824 /// @brief This class represents an extension of floating point types.
2825 class FPExtInst : public CastInst {
2827 /// @brief Clone an identical FPExtInst
2828 virtual FPExtInst *clone_impl() const;
2831 /// @brief Constructor with insert-before-instruction semantics
2833 Value *S, ///< The value to be extended
2834 const Type *Ty, ///< The type to extend to
2835 const Twine &NameStr = "", ///< A name for the new instruction
2836 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2839 /// @brief Constructor with insert-at-end-of-block semantics
2841 Value *S, ///< The value to be extended
2842 const Type *Ty, ///< The type to extend to
2843 const Twine &NameStr, ///< A name for the new instruction
2844 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2847 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2848 static inline bool classof(const FPExtInst *) { return true; }
2849 static inline bool classof(const Instruction *I) {
2850 return I->getOpcode() == FPExt;
2852 static inline bool classof(const Value *V) {
2853 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2857 //===----------------------------------------------------------------------===//
2859 //===----------------------------------------------------------------------===//
2861 /// @brief This class represents a cast unsigned integer to floating point.
2862 class UIToFPInst : public CastInst {
2864 /// @brief Clone an identical UIToFPInst
2865 virtual UIToFPInst *clone_impl() const;
2868 /// @brief Constructor with insert-before-instruction semantics
2870 Value *S, ///< The value to be converted
2871 const Type *Ty, ///< The type to convert to
2872 const Twine &NameStr = "", ///< A name for the new instruction
2873 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2876 /// @brief Constructor with insert-at-end-of-block semantics
2878 Value *S, ///< The value to be converted
2879 const Type *Ty, ///< The type to convert to
2880 const Twine &NameStr, ///< A name for the new instruction
2881 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2884 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2885 static inline bool classof(const UIToFPInst *) { return true; }
2886 static inline bool classof(const Instruction *I) {
2887 return I->getOpcode() == UIToFP;
2889 static inline bool classof(const Value *V) {
2890 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2894 //===----------------------------------------------------------------------===//
2896 //===----------------------------------------------------------------------===//
2898 /// @brief This class represents a cast from signed integer to floating point.
2899 class SIToFPInst : public CastInst {
2901 /// @brief Clone an identical SIToFPInst
2902 virtual SIToFPInst *clone_impl() const;
2905 /// @brief Constructor with insert-before-instruction semantics
2907 Value *S, ///< The value to be converted
2908 const Type *Ty, ///< The type to convert to
2909 const Twine &NameStr = "", ///< A name for the new instruction
2910 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2913 /// @brief Constructor with insert-at-end-of-block semantics
2915 Value *S, ///< The value to be converted
2916 const Type *Ty, ///< The type to convert to
2917 const Twine &NameStr, ///< A name for the new instruction
2918 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2921 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2922 static inline bool classof(const SIToFPInst *) { return true; }
2923 static inline bool classof(const Instruction *I) {
2924 return I->getOpcode() == SIToFP;
2926 static inline bool classof(const Value *V) {
2927 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2931 //===----------------------------------------------------------------------===//
2933 //===----------------------------------------------------------------------===//
2935 /// @brief This class represents a cast from floating point to unsigned integer
2936 class FPToUIInst : public CastInst {
2938 /// @brief Clone an identical FPToUIInst
2939 virtual FPToUIInst *clone_impl() const;
2942 /// @brief Constructor with insert-before-instruction semantics
2944 Value *S, ///< The value to be converted
2945 const Type *Ty, ///< The type to convert to
2946 const Twine &NameStr = "", ///< A name for the new instruction
2947 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2950 /// @brief Constructor with insert-at-end-of-block semantics
2952 Value *S, ///< The value to be converted
2953 const Type *Ty, ///< The type to convert to
2954 const Twine &NameStr, ///< A name for the new instruction
2955 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2958 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2959 static inline bool classof(const FPToUIInst *) { return true; }
2960 static inline bool classof(const Instruction *I) {
2961 return I->getOpcode() == FPToUI;
2963 static inline bool classof(const Value *V) {
2964 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2968 //===----------------------------------------------------------------------===//
2970 //===----------------------------------------------------------------------===//
2972 /// @brief This class represents a cast from floating point to signed integer.
2973 class FPToSIInst : public CastInst {
2975 /// @brief Clone an identical FPToSIInst
2976 virtual FPToSIInst *clone_impl() const;
2979 /// @brief Constructor with insert-before-instruction semantics
2981 Value *S, ///< The value to be converted
2982 const Type *Ty, ///< The type to convert to
2983 const Twine &NameStr = "", ///< A name for the new instruction
2984 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2987 /// @brief Constructor with insert-at-end-of-block semantics
2989 Value *S, ///< The value to be converted
2990 const Type *Ty, ///< The type to convert to
2991 const Twine &NameStr, ///< A name for the new instruction
2992 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2995 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2996 static inline bool classof(const FPToSIInst *) { return true; }
2997 static inline bool classof(const Instruction *I) {
2998 return I->getOpcode() == FPToSI;
3000 static inline bool classof(const Value *V) {
3001 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3005 //===----------------------------------------------------------------------===//
3006 // IntToPtrInst Class
3007 //===----------------------------------------------------------------------===//
3009 /// @brief This class represents a cast from an integer to a pointer.
3010 class IntToPtrInst : public CastInst {
3012 /// @brief Constructor with insert-before-instruction semantics
3014 Value *S, ///< The value to be converted
3015 const Type *Ty, ///< The type to convert to
3016 const Twine &NameStr = "", ///< A name for the new instruction
3017 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3020 /// @brief Constructor with insert-at-end-of-block semantics
3022 Value *S, ///< The value to be converted
3023 const Type *Ty, ///< The type to convert to
3024 const Twine &NameStr, ///< A name for the new instruction
3025 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3028 /// @brief Clone an identical IntToPtrInst
3029 virtual IntToPtrInst *clone_impl() const;
3031 // Methods for support type inquiry through isa, cast, and dyn_cast:
3032 static inline bool classof(const IntToPtrInst *) { return true; }
3033 static inline bool classof(const Instruction *I) {
3034 return I->getOpcode() == IntToPtr;
3036 static inline bool classof(const Value *V) {
3037 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3041 //===----------------------------------------------------------------------===//
3042 // PtrToIntInst Class
3043 //===----------------------------------------------------------------------===//
3045 /// @brief This class represents a cast from a pointer to an integer
3046 class PtrToIntInst : public CastInst {
3048 /// @brief Clone an identical PtrToIntInst
3049 virtual PtrToIntInst *clone_impl() const;
3052 /// @brief Constructor with insert-before-instruction semantics
3054 Value *S, ///< The value to be converted
3055 const Type *Ty, ///< The type to convert to
3056 const Twine &NameStr = "", ///< A name for the new instruction
3057 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3060 /// @brief Constructor with insert-at-end-of-block semantics
3062 Value *S, ///< The value to be converted
3063 const Type *Ty, ///< The type to convert to
3064 const Twine &NameStr, ///< A name for the new instruction
3065 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3068 // Methods for support type inquiry through isa, cast, and dyn_cast:
3069 static inline bool classof(const PtrToIntInst *) { return true; }
3070 static inline bool classof(const Instruction *I) {
3071 return I->getOpcode() == PtrToInt;
3073 static inline bool classof(const Value *V) {
3074 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3078 //===----------------------------------------------------------------------===//
3079 // BitCastInst Class
3080 //===----------------------------------------------------------------------===//
3082 /// @brief This class represents a no-op cast from one type to another.
3083 class BitCastInst : public CastInst {
3085 /// @brief Clone an identical BitCastInst
3086 virtual BitCastInst *clone_impl() const;
3089 /// @brief Constructor with insert-before-instruction semantics
3091 Value *S, ///< The value to be casted
3092 const Type *Ty, ///< The type to casted to
3093 const Twine &NameStr = "", ///< A name for the new instruction
3094 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3097 /// @brief Constructor with insert-at-end-of-block semantics
3099 Value *S, ///< The value to be casted
3100 const Type *Ty, ///< The type to casted to
3101 const Twine &NameStr, ///< A name for the new instruction
3102 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3105 // Methods for support type inquiry through isa, cast, and dyn_cast:
3106 static inline bool classof(const BitCastInst *) { return true; }
3107 static inline bool classof(const Instruction *I) {
3108 return I->getOpcode() == BitCast;
3110 static inline bool classof(const Value *V) {
3111 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3115 } // End llvm namespace