1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/InstrTypes.h"
20 #include "llvm/DerivedTypes.h"
21 #include "llvm/Attributes.h"
22 #include "llvm/CallingConv.h"
23 #include "llvm/ADT/SmallVector.h"
34 //===----------------------------------------------------------------------===//
36 //===----------------------------------------------------------------------===//
38 /// AllocaInst - an instruction to allocate memory on the stack
40 class AllocaInst : public UnaryInstruction {
42 virtual AllocaInst *clone_impl() const;
44 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
45 const Twine &Name = "", Instruction *InsertBefore = 0);
46 AllocaInst(const Type *Ty, Value *ArraySize,
47 const Twine &Name, BasicBlock *InsertAtEnd);
49 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
50 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
52 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
53 const Twine &Name = "", Instruction *InsertBefore = 0);
54 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
55 const Twine &Name, BasicBlock *InsertAtEnd);
57 // Out of line virtual method, so the vtable, etc. has a home.
58 virtual ~AllocaInst();
60 /// isArrayAllocation - Return true if there is an allocation size parameter
61 /// to the allocation instruction that is not 1.
63 bool isArrayAllocation() const;
65 /// getArraySize - Get the number of elements allocated. For a simple
66 /// allocation of a single element, this will return a constant 1 value.
68 const Value *getArraySize() const { return getOperand(0); }
69 Value *getArraySize() { return getOperand(0); }
71 /// getType - Overload to return most specific pointer type
73 const PointerType *getType() const {
74 return reinterpret_cast<const PointerType*>(Instruction::getType());
77 /// getAllocatedType - Return the type that is being allocated by the
80 const Type *getAllocatedType() const;
82 /// getAlignment - Return the alignment of the memory that is being allocated
83 /// by the instruction.
85 unsigned getAlignment() const {
86 return (1u << getSubclassDataFromInstruction()) >> 1;
88 void setAlignment(unsigned Align);
90 /// isStaticAlloca - Return true if this alloca is in the entry block of the
91 /// function and is a constant size. If so, the code generator will fold it
92 /// into the prolog/epilog code, so it is basically free.
93 bool isStaticAlloca() const;
95 // Methods for support type inquiry through isa, cast, and dyn_cast:
96 static inline bool classof(const AllocaInst *) { return true; }
97 static inline bool classof(const Instruction *I) {
98 return (I->getOpcode() == Instruction::Alloca);
100 static inline bool classof(const Value *V) {
101 return isa<Instruction>(V) && classof(cast<Instruction>(V));
104 // Shadow Instruction::setInstructionSubclassData with a private forwarding
105 // method so that subclasses cannot accidentally use it.
106 void setInstructionSubclassData(unsigned short D) {
107 Instruction::setInstructionSubclassData(D);
112 //===----------------------------------------------------------------------===//
114 //===----------------------------------------------------------------------===//
116 /// LoadInst - an instruction for reading from memory. This uses the
117 /// SubclassData field in Value to store whether or not the load is volatile.
119 class LoadInst : public UnaryInstruction {
122 virtual LoadInst *clone_impl() const;
124 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
125 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
126 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
127 Instruction *InsertBefore = 0);
128 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
129 unsigned Align, Instruction *InsertBefore = 0);
130 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
131 BasicBlock *InsertAtEnd);
132 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
133 unsigned Align, BasicBlock *InsertAtEnd);
135 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
136 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
137 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
138 bool isVolatile = false, Instruction *InsertBefore = 0);
139 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
140 BasicBlock *InsertAtEnd);
142 /// isVolatile - Return true if this is a load from a volatile memory
145 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
147 /// setVolatile - Specify whether this is a volatile load or not.
149 void setVolatile(bool V) {
150 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
154 /// getAlignment - Return the alignment of the access that is being performed
156 unsigned getAlignment() const {
157 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
160 void setAlignment(unsigned Align);
162 Value *getPointerOperand() { return getOperand(0); }
163 const Value *getPointerOperand() const { return getOperand(0); }
164 static unsigned getPointerOperandIndex() { return 0U; }
166 unsigned getPointerAddressSpace() const {
167 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
171 // Methods for support type inquiry through isa, cast, and dyn_cast:
172 static inline bool classof(const LoadInst *) { return true; }
173 static inline bool classof(const Instruction *I) {
174 return I->getOpcode() == Instruction::Load;
176 static inline bool classof(const Value *V) {
177 return isa<Instruction>(V) && classof(cast<Instruction>(V));
180 // Shadow Instruction::setInstructionSubclassData with a private forwarding
181 // method so that subclasses cannot accidentally use it.
182 void setInstructionSubclassData(unsigned short D) {
183 Instruction::setInstructionSubclassData(D);
188 //===----------------------------------------------------------------------===//
190 //===----------------------------------------------------------------------===//
192 /// StoreInst - an instruction for storing to memory
194 class StoreInst : public Instruction {
195 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
198 virtual StoreInst *clone_impl() const;
200 // allocate space for exactly two operands
201 void *operator new(size_t s) {
202 return User::operator new(s, 2);
204 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
205 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
206 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
207 Instruction *InsertBefore = 0);
208 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
209 unsigned Align, Instruction *InsertBefore = 0);
210 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
211 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
212 unsigned Align, BasicBlock *InsertAtEnd);
215 /// isVolatile - Return true if this is a load from a volatile memory
218 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
220 /// setVolatile - Specify whether this is a volatile load or not.
222 void setVolatile(bool V) {
223 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
227 /// Transparently provide more efficient getOperand methods.
228 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
230 /// getAlignment - Return the alignment of the access that is being performed
232 unsigned getAlignment() const {
233 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
236 void setAlignment(unsigned Align);
238 Value *getPointerOperand() { return getOperand(1); }
239 const Value *getPointerOperand() const { return getOperand(1); }
240 static unsigned getPointerOperandIndex() { return 1U; }
242 unsigned getPointerAddressSpace() const {
243 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
246 // Methods for support type inquiry through isa, cast, and dyn_cast:
247 static inline bool classof(const StoreInst *) { return true; }
248 static inline bool classof(const Instruction *I) {
249 return I->getOpcode() == Instruction::Store;
251 static inline bool classof(const Value *V) {
252 return isa<Instruction>(V) && classof(cast<Instruction>(V));
255 // Shadow Instruction::setInstructionSubclassData with a private forwarding
256 // method so that subclasses cannot accidentally use it.
257 void setInstructionSubclassData(unsigned short D) {
258 Instruction::setInstructionSubclassData(D);
263 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
266 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
268 //===----------------------------------------------------------------------===//
269 // GetElementPtrInst Class
270 //===----------------------------------------------------------------------===//
272 // checkType - Simple wrapper function to give a better assertion failure
273 // message on bad indexes for a gep instruction.
275 static inline const Type *checkType(const Type *Ty) {
276 assert(Ty && "Invalid GetElementPtrInst indices for type!");
280 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
281 /// access elements of arrays and structs
283 class GetElementPtrInst : public Instruction {
284 GetElementPtrInst(const GetElementPtrInst &GEPI);
285 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
286 const Twine &NameStr);
287 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
289 template<typename InputIterator>
290 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
291 const Twine &NameStr,
292 // This argument ensures that we have an iterator we can
293 // do arithmetic on in constant time
294 std::random_access_iterator_tag) {
295 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
298 // This requires that the iterator points to contiguous memory.
299 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
300 // we have to build an array here
303 init(Ptr, 0, NumIdx, NameStr);
307 /// getIndexedType - Returns the type of the element that would be loaded with
308 /// a load instruction with the specified parameters.
310 /// Null is returned if the indices are invalid for the specified
313 template<typename InputIterator>
314 static const Type *getIndexedType(const Type *Ptr,
315 InputIterator IdxBegin,
316 InputIterator IdxEnd,
317 // This argument ensures that we
318 // have an iterator we can do
319 // arithmetic on in constant time
320 std::random_access_iterator_tag) {
321 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
324 // This requires that the iterator points to contiguous memory.
325 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
327 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
330 /// Constructors - Create a getelementptr instruction with a base pointer an
331 /// list of indices. The first ctor can optionally insert before an existing
332 /// instruction, the second appends the new instruction to the specified
334 template<typename InputIterator>
335 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
336 InputIterator IdxEnd,
338 const Twine &NameStr,
339 Instruction *InsertBefore);
340 template<typename InputIterator>
341 inline GetElementPtrInst(Value *Ptr,
342 InputIterator IdxBegin, InputIterator IdxEnd,
344 const Twine &NameStr, BasicBlock *InsertAtEnd);
346 /// Constructors - These two constructors are convenience methods because one
347 /// and two index getelementptr instructions are so common.
348 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
349 Instruction *InsertBefore = 0);
350 GetElementPtrInst(Value *Ptr, Value *Idx,
351 const Twine &NameStr, BasicBlock *InsertAtEnd);
353 virtual GetElementPtrInst *clone_impl() const;
355 template<typename InputIterator>
356 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
357 InputIterator IdxEnd,
358 const Twine &NameStr = "",
359 Instruction *InsertBefore = 0) {
360 typename std::iterator_traits<InputIterator>::difference_type Values =
361 1 + std::distance(IdxBegin, IdxEnd);
363 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
365 template<typename InputIterator>
366 static GetElementPtrInst *Create(Value *Ptr,
367 InputIterator IdxBegin, InputIterator IdxEnd,
368 const Twine &NameStr,
369 BasicBlock *InsertAtEnd) {
370 typename std::iterator_traits<InputIterator>::difference_type Values =
371 1 + std::distance(IdxBegin, IdxEnd);
373 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
376 /// Constructors - These two creators are convenience methods because one
377 /// index getelementptr instructions are so common.
378 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
379 const Twine &NameStr = "",
380 Instruction *InsertBefore = 0) {
381 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
383 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
384 const Twine &NameStr,
385 BasicBlock *InsertAtEnd) {
386 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
389 /// Create an "inbounds" getelementptr. See the documentation for the
390 /// "inbounds" flag in LangRef.html for details.
391 template<typename InputIterator>
392 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
393 InputIterator IdxEnd,
394 const Twine &NameStr = "",
395 Instruction *InsertBefore = 0) {
396 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
397 NameStr, InsertBefore);
398 GEP->setIsInBounds(true);
401 template<typename InputIterator>
402 static GetElementPtrInst *CreateInBounds(Value *Ptr,
403 InputIterator IdxBegin,
404 InputIterator IdxEnd,
405 const Twine &NameStr,
406 BasicBlock *InsertAtEnd) {
407 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
408 NameStr, InsertAtEnd);
409 GEP->setIsInBounds(true);
412 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
413 const Twine &NameStr = "",
414 Instruction *InsertBefore = 0) {
415 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
416 GEP->setIsInBounds(true);
419 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
420 const Twine &NameStr,
421 BasicBlock *InsertAtEnd) {
422 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
423 GEP->setIsInBounds(true);
427 /// Transparently provide more efficient getOperand methods.
428 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
430 // getType - Overload to return most specific pointer type...
431 const PointerType *getType() const {
432 return reinterpret_cast<const PointerType*>(Instruction::getType());
435 /// getIndexedType - Returns the type of the element that would be loaded with
436 /// a load instruction with the specified parameters.
438 /// Null is returned if the indices are invalid for the specified
441 template<typename InputIterator>
442 static const Type *getIndexedType(const Type *Ptr,
443 InputIterator IdxBegin,
444 InputIterator IdxEnd) {
445 return getIndexedType(Ptr, IdxBegin, IdxEnd,
446 typename std::iterator_traits<InputIterator>::
447 iterator_category());
450 static const Type *getIndexedType(const Type *Ptr,
451 Value* const *Idx, unsigned NumIdx);
453 static const Type *getIndexedType(const Type *Ptr,
454 uint64_t const *Idx, unsigned NumIdx);
456 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
458 inline op_iterator idx_begin() { return op_begin()+1; }
459 inline const_op_iterator idx_begin() const { return op_begin()+1; }
460 inline op_iterator idx_end() { return op_end(); }
461 inline const_op_iterator idx_end() const { return op_end(); }
463 Value *getPointerOperand() {
464 return getOperand(0);
466 const Value *getPointerOperand() const {
467 return getOperand(0);
469 static unsigned getPointerOperandIndex() {
470 return 0U; // get index for modifying correct operand
473 unsigned getPointerAddressSpace() const {
474 return cast<PointerType>(getType())->getAddressSpace();
477 /// getPointerOperandType - Method to return the pointer operand as a
479 const PointerType *getPointerOperandType() const {
480 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
484 unsigned getNumIndices() const { // Note: always non-negative
485 return getNumOperands() - 1;
488 bool hasIndices() const {
489 return getNumOperands() > 1;
492 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
493 /// zeros. If so, the result pointer and the first operand have the same
494 /// value, just potentially different types.
495 bool hasAllZeroIndices() const;
497 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
498 /// constant integers. If so, the result pointer and the first operand have
499 /// a constant offset between them.
500 bool hasAllConstantIndices() const;
502 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
503 /// See LangRef.html for the meaning of inbounds on a getelementptr.
504 void setIsInBounds(bool b = true);
506 /// isInBounds - Determine whether the GEP has the inbounds flag.
507 bool isInBounds() const;
509 // Methods for support type inquiry through isa, cast, and dyn_cast:
510 static inline bool classof(const GetElementPtrInst *) { return true; }
511 static inline bool classof(const Instruction *I) {
512 return (I->getOpcode() == Instruction::GetElementPtr);
514 static inline bool classof(const Value *V) {
515 return isa<Instruction>(V) && classof(cast<Instruction>(V));
520 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
523 template<typename InputIterator>
524 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
525 InputIterator IdxBegin,
526 InputIterator IdxEnd,
528 const Twine &NameStr,
529 Instruction *InsertBefore)
530 : Instruction(PointerType::get(checkType(
531 getIndexedType(Ptr->getType(),
533 cast<PointerType>(Ptr->getType())
534 ->getAddressSpace()),
536 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
537 Values, InsertBefore) {
538 init(Ptr, IdxBegin, IdxEnd, NameStr,
539 typename std::iterator_traits<InputIterator>::iterator_category());
541 template<typename InputIterator>
542 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
543 InputIterator IdxBegin,
544 InputIterator IdxEnd,
546 const Twine &NameStr,
547 BasicBlock *InsertAtEnd)
548 : Instruction(PointerType::get(checkType(
549 getIndexedType(Ptr->getType(),
551 cast<PointerType>(Ptr->getType())
552 ->getAddressSpace()),
554 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
555 Values, InsertAtEnd) {
556 init(Ptr, IdxBegin, IdxEnd, NameStr,
557 typename std::iterator_traits<InputIterator>::iterator_category());
561 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
564 //===----------------------------------------------------------------------===//
566 //===----------------------------------------------------------------------===//
568 /// This instruction compares its operands according to the predicate given
569 /// to the constructor. It only operates on integers or pointers. The operands
570 /// must be identical types.
571 /// @brief Represent an integer comparison operator.
572 class ICmpInst: public CmpInst {
574 /// @brief Clone an indentical ICmpInst
575 virtual ICmpInst *clone_impl() const;
577 /// @brief Constructor with insert-before-instruction semantics.
579 Instruction *InsertBefore, ///< Where to insert
580 Predicate pred, ///< The predicate to use for the comparison
581 Value *LHS, ///< The left-hand-side of the expression
582 Value *RHS, ///< The right-hand-side of the expression
583 const Twine &NameStr = "" ///< Name of the instruction
584 ) : CmpInst(makeCmpResultType(LHS->getType()),
585 Instruction::ICmp, pred, LHS, RHS, NameStr,
587 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
588 pred <= CmpInst::LAST_ICMP_PREDICATE &&
589 "Invalid ICmp predicate value");
590 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
591 "Both operands to ICmp instruction are not of the same type!");
592 // Check that the operands are the right type
593 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
594 getOperand(0)->getType()->isPointerTy()) &&
595 "Invalid operand types for ICmp instruction");
598 /// @brief Constructor with insert-at-end semantics.
600 BasicBlock &InsertAtEnd, ///< Block to insert into.
601 Predicate pred, ///< The predicate to use for the comparison
602 Value *LHS, ///< The left-hand-side of the expression
603 Value *RHS, ///< The right-hand-side of the expression
604 const Twine &NameStr = "" ///< Name of the instruction
605 ) : CmpInst(makeCmpResultType(LHS->getType()),
606 Instruction::ICmp, pred, LHS, RHS, NameStr,
608 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
609 pred <= CmpInst::LAST_ICMP_PREDICATE &&
610 "Invalid ICmp predicate value");
611 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
612 "Both operands to ICmp instruction are not of the same type!");
613 // Check that the operands are the right type
614 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
615 getOperand(0)->getType()->isPointerTy()) &&
616 "Invalid operand types for ICmp instruction");
619 /// @brief Constructor with no-insertion semantics
621 Predicate pred, ///< The predicate to use for the comparison
622 Value *LHS, ///< The left-hand-side of the expression
623 Value *RHS, ///< The right-hand-side of the expression
624 const Twine &NameStr = "" ///< Name of the instruction
625 ) : CmpInst(makeCmpResultType(LHS->getType()),
626 Instruction::ICmp, pred, LHS, RHS, NameStr) {
627 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
628 pred <= CmpInst::LAST_ICMP_PREDICATE &&
629 "Invalid ICmp predicate value");
630 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
631 "Both operands to ICmp instruction are not of the same type!");
632 // Check that the operands are the right type
633 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
634 getOperand(0)->getType()->isPointerTy()) &&
635 "Invalid operand types for ICmp instruction");
638 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
639 /// @returns the predicate that would be the result if the operand were
640 /// regarded as signed.
641 /// @brief Return the signed version of the predicate
642 Predicate getSignedPredicate() const {
643 return getSignedPredicate(getPredicate());
646 /// This is a static version that you can use without an instruction.
647 /// @brief Return the signed version of the predicate.
648 static Predicate getSignedPredicate(Predicate pred);
650 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
651 /// @returns the predicate that would be the result if the operand were
652 /// regarded as unsigned.
653 /// @brief Return the unsigned version of the predicate
654 Predicate getUnsignedPredicate() const {
655 return getUnsignedPredicate(getPredicate());
658 /// This is a static version that you can use without an instruction.
659 /// @brief Return the unsigned version of the predicate.
660 static Predicate getUnsignedPredicate(Predicate pred);
662 /// isEquality - Return true if this predicate is either EQ or NE. This also
663 /// tests for commutativity.
664 static bool isEquality(Predicate P) {
665 return P == ICMP_EQ || P == ICMP_NE;
668 /// isEquality - Return true if this predicate is either EQ or NE. This also
669 /// tests for commutativity.
670 bool isEquality() const {
671 return isEquality(getPredicate());
674 /// @returns true if the predicate of this ICmpInst is commutative
675 /// @brief Determine if this relation is commutative.
676 bool isCommutative() const { return isEquality(); }
678 /// isRelational - Return true if the predicate is relational (not EQ or NE).
680 bool isRelational() const {
681 return !isEquality();
684 /// isRelational - Return true if the predicate is relational (not EQ or NE).
686 static bool isRelational(Predicate P) {
687 return !isEquality(P);
690 /// Initialize a set of values that all satisfy the predicate with C.
691 /// @brief Make a ConstantRange for a relation with a constant value.
692 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
694 /// Exchange the two operands to this instruction in such a way that it does
695 /// not modify the semantics of the instruction. The predicate value may be
696 /// changed to retain the same result if the predicate is order dependent
698 /// @brief Swap operands and adjust predicate.
699 void swapOperands() {
700 setPredicate(getSwappedPredicate());
701 Op<0>().swap(Op<1>());
704 // Methods for support type inquiry through isa, cast, and dyn_cast:
705 static inline bool classof(const ICmpInst *) { return true; }
706 static inline bool classof(const Instruction *I) {
707 return I->getOpcode() == Instruction::ICmp;
709 static inline bool classof(const Value *V) {
710 return isa<Instruction>(V) && classof(cast<Instruction>(V));
715 //===----------------------------------------------------------------------===//
717 //===----------------------------------------------------------------------===//
719 /// This instruction compares its operands according to the predicate given
720 /// to the constructor. It only operates on floating point values or packed
721 /// vectors of floating point values. The operands must be identical types.
722 /// @brief Represents a floating point comparison operator.
723 class FCmpInst: public CmpInst {
725 /// @brief Clone an indentical FCmpInst
726 virtual FCmpInst *clone_impl() const;
728 /// @brief Constructor with insert-before-instruction semantics.
730 Instruction *InsertBefore, ///< Where to insert
731 Predicate pred, ///< The predicate to use for the comparison
732 Value *LHS, ///< The left-hand-side of the expression
733 Value *RHS, ///< The right-hand-side of the expression
734 const Twine &NameStr = "" ///< Name of the instruction
735 ) : CmpInst(makeCmpResultType(LHS->getType()),
736 Instruction::FCmp, pred, LHS, RHS, NameStr,
738 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
739 "Invalid FCmp predicate value");
740 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
741 "Both operands to FCmp instruction are not of the same type!");
742 // Check that the operands are the right type
743 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
744 "Invalid operand types for FCmp instruction");
747 /// @brief Constructor with insert-at-end semantics.
749 BasicBlock &InsertAtEnd, ///< Block to insert into.
750 Predicate pred, ///< The predicate to use for the comparison
751 Value *LHS, ///< The left-hand-side of the expression
752 Value *RHS, ///< The right-hand-side of the expression
753 const Twine &NameStr = "" ///< Name of the instruction
754 ) : CmpInst(makeCmpResultType(LHS->getType()),
755 Instruction::FCmp, pred, LHS, RHS, NameStr,
757 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
758 "Invalid FCmp predicate value");
759 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
760 "Both operands to FCmp instruction are not of the same type!");
761 // Check that the operands are the right type
762 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
763 "Invalid operand types for FCmp instruction");
766 /// @brief Constructor with no-insertion semantics
768 Predicate pred, ///< The predicate to use for the comparison
769 Value *LHS, ///< The left-hand-side of the expression
770 Value *RHS, ///< The right-hand-side of the expression
771 const Twine &NameStr = "" ///< Name of the instruction
772 ) : CmpInst(makeCmpResultType(LHS->getType()),
773 Instruction::FCmp, pred, LHS, RHS, NameStr) {
774 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
775 "Invalid FCmp predicate value");
776 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
777 "Both operands to FCmp instruction are not of the same type!");
778 // Check that the operands are the right type
779 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
780 "Invalid operand types for FCmp instruction");
783 /// @returns true if the predicate of this instruction is EQ or NE.
784 /// @brief Determine if this is an equality predicate.
785 bool isEquality() const {
786 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
787 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
790 /// @returns true if the predicate of this instruction is commutative.
791 /// @brief Determine if this is a commutative predicate.
792 bool isCommutative() const {
793 return isEquality() ||
794 getPredicate() == FCMP_FALSE ||
795 getPredicate() == FCMP_TRUE ||
796 getPredicate() == FCMP_ORD ||
797 getPredicate() == FCMP_UNO;
800 /// @returns true if the predicate is relational (not EQ or NE).
801 /// @brief Determine if this a relational predicate.
802 bool isRelational() const { return !isEquality(); }
804 /// Exchange the two operands to this instruction in such a way that it does
805 /// not modify the semantics of the instruction. The predicate value may be
806 /// changed to retain the same result if the predicate is order dependent
808 /// @brief Swap operands and adjust predicate.
809 void swapOperands() {
810 setPredicate(getSwappedPredicate());
811 Op<0>().swap(Op<1>());
814 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
815 static inline bool classof(const FCmpInst *) { return true; }
816 static inline bool classof(const Instruction *I) {
817 return I->getOpcode() == Instruction::FCmp;
819 static inline bool classof(const Value *V) {
820 return isa<Instruction>(V) && classof(cast<Instruction>(V));
824 //===----------------------------------------------------------------------===//
825 /// CallInst - This class represents a function call, abstracting a target
826 /// machine's calling convention. This class uses low bit of the SubClassData
827 /// field to indicate whether or not this is a tail call. The rest of the bits
828 /// hold the calling convention of the call.
830 class CallInst : public Instruction {
831 AttrListPtr AttributeList; ///< parameter attributes for call
832 CallInst(const CallInst &CI);
833 void init(Value *Func, Value* const *Params, unsigned NumParams);
834 void init(Value *Func, Value *Actual1, Value *Actual2);
835 void init(Value *Func, Value *Actual);
836 void init(Value *Func);
838 template<typename InputIterator>
839 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
840 const Twine &NameStr,
841 // This argument ensures that we have an iterator we can
842 // do arithmetic on in constant time
843 std::random_access_iterator_tag) {
844 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
846 // This requires that the iterator points to contiguous memory.
847 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
851 /// Construct a CallInst given a range of arguments. InputIterator
852 /// must be a random-access iterator pointing to contiguous storage
853 /// (e.g. a std::vector<>::iterator). Checks are made for
854 /// random-accessness but not for contiguous storage as that would
855 /// incur runtime overhead.
856 /// @brief Construct a CallInst from a range of arguments
857 template<typename InputIterator>
858 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
859 const Twine &NameStr, Instruction *InsertBefore);
861 /// Construct a CallInst given a range of arguments. InputIterator
862 /// must be a random-access iterator pointing to contiguous storage
863 /// (e.g. a std::vector<>::iterator). Checks are made for
864 /// random-accessness but not for contiguous storage as that would
865 /// incur runtime overhead.
866 /// @brief Construct a CallInst from a range of arguments
867 template<typename InputIterator>
868 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
869 const Twine &NameStr, BasicBlock *InsertAtEnd);
871 CallInst(Value *F, Value *Actual, const Twine &NameStr,
872 Instruction *InsertBefore);
873 CallInst(Value *F, Value *Actual, const Twine &NameStr,
874 BasicBlock *InsertAtEnd);
875 explicit CallInst(Value *F, const Twine &NameStr,
876 Instruction *InsertBefore);
877 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
879 virtual CallInst *clone_impl() const;
881 template<typename InputIterator>
882 static CallInst *Create(Value *Func,
883 InputIterator ArgBegin, InputIterator ArgEnd,
884 const Twine &NameStr = "",
885 Instruction *InsertBefore = 0) {
886 return new((unsigned)(ArgEnd - ArgBegin + 1))
887 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
889 template<typename InputIterator>
890 static CallInst *Create(Value *Func,
891 InputIterator ArgBegin, InputIterator ArgEnd,
892 const Twine &NameStr, BasicBlock *InsertAtEnd) {
893 return new((unsigned)(ArgEnd - ArgBegin + 1))
894 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
896 static CallInst *Create(Value *F, Value *Actual,
897 const Twine &NameStr = "",
898 Instruction *InsertBefore = 0) {
899 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
901 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
902 BasicBlock *InsertAtEnd) {
903 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
905 static CallInst *Create(Value *F, const Twine &NameStr = "",
906 Instruction *InsertBefore = 0) {
907 return new(1) CallInst(F, NameStr, InsertBefore);
909 static CallInst *Create(Value *F, const Twine &NameStr,
910 BasicBlock *InsertAtEnd) {
911 return new(1) CallInst(F, NameStr, InsertAtEnd);
913 /// CreateMalloc - Generate the IR for a call to malloc:
914 /// 1. Compute the malloc call's argument as the specified type's size,
915 /// possibly multiplied by the array size if the array size is not
917 /// 2. Call malloc with that argument.
918 /// 3. Bitcast the result of the malloc call to the specified type.
919 static Instruction *CreateMalloc(Instruction *InsertBefore,
920 const Type *IntPtrTy, const Type *AllocTy,
921 Value *AllocSize, Value *ArraySize = 0,
922 const Twine &Name = "");
923 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
924 const Type *IntPtrTy, const Type *AllocTy,
925 Value *AllocSize, Value *ArraySize = 0,
926 Function* MallocF = 0,
927 const Twine &Name = "");
928 /// CreateFree - Generate the IR for a call to the builtin free function.
929 static void CreateFree(Value* Source, Instruction *InsertBefore);
930 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
934 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
935 void setTailCall(bool isTC = true) {
936 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
940 /// Provide fast operand accessors
941 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
943 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
944 Value *getArgOperand(unsigned i) const { return getOperand(i + 1); }
946 /// getCallingConv/setCallingConv - Get or set the calling convention of this
948 CallingConv::ID getCallingConv() const {
949 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
951 void setCallingConv(CallingConv::ID CC) {
952 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
953 (static_cast<unsigned>(CC) << 1));
956 /// getAttributes - Return the parameter attributes for this call.
958 const AttrListPtr &getAttributes() const { return AttributeList; }
960 /// setAttributes - Set the parameter attributes for this call.
962 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
964 /// addAttribute - adds the attribute to the list of attributes.
965 void addAttribute(unsigned i, Attributes attr);
967 /// removeAttribute - removes the attribute from the list of attributes.
968 void removeAttribute(unsigned i, Attributes attr);
970 /// @brief Determine whether the call or the callee has the given attribute.
971 bool paramHasAttr(unsigned i, Attributes attr) const;
973 /// @brief Extract the alignment for a call or parameter (0=unknown).
974 unsigned getParamAlignment(unsigned i) const {
975 return AttributeList.getParamAlignment(i);
978 /// @brief Return true if the call should not be inlined.
979 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
980 void setIsNoInline(bool Value) {
981 if (Value) addAttribute(~0, Attribute::NoInline);
982 else removeAttribute(~0, Attribute::NoInline);
985 /// @brief Determine if the call does not access memory.
986 bool doesNotAccessMemory() const {
987 return paramHasAttr(~0, Attribute::ReadNone);
989 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
990 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
991 else removeAttribute(~0, Attribute::ReadNone);
994 /// @brief Determine if the call does not access or only reads memory.
995 bool onlyReadsMemory() const {
996 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
998 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
999 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1000 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1003 /// @brief Determine if the call cannot return.
1004 bool doesNotReturn() const {
1005 return paramHasAttr(~0, Attribute::NoReturn);
1007 void setDoesNotReturn(bool DoesNotReturn = true) {
1008 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1009 else removeAttribute(~0, Attribute::NoReturn);
1012 /// @brief Determine if the call cannot unwind.
1013 bool doesNotThrow() const {
1014 return paramHasAttr(~0, Attribute::NoUnwind);
1016 void setDoesNotThrow(bool DoesNotThrow = true) {
1017 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1018 else removeAttribute(~0, Attribute::NoUnwind);
1021 /// @brief Determine if the call returns a structure through first
1022 /// pointer argument.
1023 bool hasStructRetAttr() const {
1024 // Be friendly and also check the callee.
1025 return paramHasAttr(1, Attribute::StructRet);
1028 /// @brief Determine if any call argument is an aggregate passed by value.
1029 bool hasByValArgument() const {
1030 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1033 /// getCalledFunction - Return the function called, or null if this is an
1034 /// indirect function invocation.
1036 Function *getCalledFunction() const {
1037 return dyn_cast<Function>(Op<0>());
1040 /// getCalledValue - Get a pointer to the function that is invoked by this
1042 const Value *getCalledValue() const { return Op<0>(); }
1043 Value *getCalledValue() { return Op<0>(); }
1045 /// setCalledFunction - Set the function called.
1046 void setCalledFunction(Value* Fn) {
1050 // Methods for support type inquiry through isa, cast, and dyn_cast:
1051 static inline bool classof(const CallInst *) { return true; }
1052 static inline bool classof(const Instruction *I) {
1053 return I->getOpcode() == Instruction::Call;
1055 static inline bool classof(const Value *V) {
1056 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1059 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1060 // method so that subclasses cannot accidentally use it.
1061 void setInstructionSubclassData(unsigned short D) {
1062 Instruction::setInstructionSubclassData(D);
1067 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1070 template<typename InputIterator>
1071 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1072 const Twine &NameStr, BasicBlock *InsertAtEnd)
1073 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1074 ->getElementType())->getReturnType(),
1076 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1077 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1078 init(Func, ArgBegin, ArgEnd, NameStr,
1079 typename std::iterator_traits<InputIterator>::iterator_category());
1082 template<typename InputIterator>
1083 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1084 const Twine &NameStr, Instruction *InsertBefore)
1085 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1086 ->getElementType())->getReturnType(),
1088 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1089 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1090 init(Func, ArgBegin, ArgEnd, NameStr,
1091 typename std::iterator_traits<InputIterator>::iterator_category());
1094 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1096 //===----------------------------------------------------------------------===//
1098 //===----------------------------------------------------------------------===//
1100 /// SelectInst - This class represents the LLVM 'select' instruction.
1102 class SelectInst : public Instruction {
1103 void init(Value *C, Value *S1, Value *S2) {
1104 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1110 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1111 Instruction *InsertBefore)
1112 : Instruction(S1->getType(), Instruction::Select,
1113 &Op<0>(), 3, InsertBefore) {
1117 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1118 BasicBlock *InsertAtEnd)
1119 : Instruction(S1->getType(), Instruction::Select,
1120 &Op<0>(), 3, InsertAtEnd) {
1125 virtual SelectInst *clone_impl() const;
1127 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1128 const Twine &NameStr = "",
1129 Instruction *InsertBefore = 0) {
1130 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1132 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1133 const Twine &NameStr,
1134 BasicBlock *InsertAtEnd) {
1135 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1138 const Value *getCondition() const { return Op<0>(); }
1139 const Value *getTrueValue() const { return Op<1>(); }
1140 const Value *getFalseValue() const { return Op<2>(); }
1141 Value *getCondition() { return Op<0>(); }
1142 Value *getTrueValue() { return Op<1>(); }
1143 Value *getFalseValue() { return Op<2>(); }
1145 /// areInvalidOperands - Return a string if the specified operands are invalid
1146 /// for a select operation, otherwise return null.
1147 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1149 /// Transparently provide more efficient getOperand methods.
1150 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1152 OtherOps getOpcode() const {
1153 return static_cast<OtherOps>(Instruction::getOpcode());
1156 // Methods for support type inquiry through isa, cast, and dyn_cast:
1157 static inline bool classof(const SelectInst *) { return true; }
1158 static inline bool classof(const Instruction *I) {
1159 return I->getOpcode() == Instruction::Select;
1161 static inline bool classof(const Value *V) {
1162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1167 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1170 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1172 //===----------------------------------------------------------------------===//
1174 //===----------------------------------------------------------------------===//
1176 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1177 /// an argument of the specified type given a va_list and increments that list
1179 class VAArgInst : public UnaryInstruction {
1181 virtual VAArgInst *clone_impl() const;
1184 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1185 Instruction *InsertBefore = 0)
1186 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1189 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1190 BasicBlock *InsertAtEnd)
1191 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1195 // Methods for support type inquiry through isa, cast, and dyn_cast:
1196 static inline bool classof(const VAArgInst *) { return true; }
1197 static inline bool classof(const Instruction *I) {
1198 return I->getOpcode() == VAArg;
1200 static inline bool classof(const Value *V) {
1201 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1205 //===----------------------------------------------------------------------===//
1206 // ExtractElementInst Class
1207 //===----------------------------------------------------------------------===//
1209 /// ExtractElementInst - This instruction extracts a single (scalar)
1210 /// element from a VectorType value
1212 class ExtractElementInst : public Instruction {
1213 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1214 Instruction *InsertBefore = 0);
1215 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1216 BasicBlock *InsertAtEnd);
1218 virtual ExtractElementInst *clone_impl() const;
1221 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1222 const Twine &NameStr = "",
1223 Instruction *InsertBefore = 0) {
1224 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1226 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1227 const Twine &NameStr,
1228 BasicBlock *InsertAtEnd) {
1229 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1232 /// isValidOperands - Return true if an extractelement instruction can be
1233 /// formed with the specified operands.
1234 static bool isValidOperands(const Value *Vec, const Value *Idx);
1236 Value *getVectorOperand() { return Op<0>(); }
1237 Value *getIndexOperand() { return Op<1>(); }
1238 const Value *getVectorOperand() const { return Op<0>(); }
1239 const Value *getIndexOperand() const { return Op<1>(); }
1241 const VectorType *getVectorOperandType() const {
1242 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1246 /// Transparently provide more efficient getOperand methods.
1247 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1249 // Methods for support type inquiry through isa, cast, and dyn_cast:
1250 static inline bool classof(const ExtractElementInst *) { return true; }
1251 static inline bool classof(const Instruction *I) {
1252 return I->getOpcode() == Instruction::ExtractElement;
1254 static inline bool classof(const Value *V) {
1255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1260 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1263 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1265 //===----------------------------------------------------------------------===//
1266 // InsertElementInst Class
1267 //===----------------------------------------------------------------------===//
1269 /// InsertElementInst - This instruction inserts a single (scalar)
1270 /// element into a VectorType value
1272 class InsertElementInst : public Instruction {
1273 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1274 const Twine &NameStr = "",
1275 Instruction *InsertBefore = 0);
1276 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1277 const Twine &NameStr, BasicBlock *InsertAtEnd);
1279 virtual InsertElementInst *clone_impl() const;
1282 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1283 const Twine &NameStr = "",
1284 Instruction *InsertBefore = 0) {
1285 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1287 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1288 const Twine &NameStr,
1289 BasicBlock *InsertAtEnd) {
1290 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1293 /// isValidOperands - Return true if an insertelement instruction can be
1294 /// formed with the specified operands.
1295 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1298 /// getType - Overload to return most specific vector type.
1300 const VectorType *getType() const {
1301 return reinterpret_cast<const VectorType*>(Instruction::getType());
1304 /// Transparently provide more efficient getOperand methods.
1305 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1307 // Methods for support type inquiry through isa, cast, and dyn_cast:
1308 static inline bool classof(const InsertElementInst *) { return true; }
1309 static inline bool classof(const Instruction *I) {
1310 return I->getOpcode() == Instruction::InsertElement;
1312 static inline bool classof(const Value *V) {
1313 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1318 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1321 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1323 //===----------------------------------------------------------------------===//
1324 // ShuffleVectorInst Class
1325 //===----------------------------------------------------------------------===//
1327 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1330 class ShuffleVectorInst : public Instruction {
1332 virtual ShuffleVectorInst *clone_impl() const;
1335 // allocate space for exactly three operands
1336 void *operator new(size_t s) {
1337 return User::operator new(s, 3);
1339 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1340 const Twine &NameStr = "",
1341 Instruction *InsertBefor = 0);
1342 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1343 const Twine &NameStr, BasicBlock *InsertAtEnd);
1345 /// isValidOperands - Return true if a shufflevector instruction can be
1346 /// formed with the specified operands.
1347 static bool isValidOperands(const Value *V1, const Value *V2,
1350 /// getType - Overload to return most specific vector type.
1352 const VectorType *getType() const {
1353 return reinterpret_cast<const VectorType*>(Instruction::getType());
1356 /// Transparently provide more efficient getOperand methods.
1357 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1359 /// getMaskValue - Return the index from the shuffle mask for the specified
1360 /// output result. This is either -1 if the element is undef or a number less
1361 /// than 2*numelements.
1362 int getMaskValue(unsigned i) const;
1364 // Methods for support type inquiry through isa, cast, and dyn_cast:
1365 static inline bool classof(const ShuffleVectorInst *) { return true; }
1366 static inline bool classof(const Instruction *I) {
1367 return I->getOpcode() == Instruction::ShuffleVector;
1369 static inline bool classof(const Value *V) {
1370 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1375 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1378 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1380 //===----------------------------------------------------------------------===//
1381 // ExtractValueInst Class
1382 //===----------------------------------------------------------------------===//
1384 /// ExtractValueInst - This instruction extracts a struct member or array
1385 /// element value from an aggregate value.
1387 class ExtractValueInst : public UnaryInstruction {
1388 SmallVector<unsigned, 4> Indices;
1390 ExtractValueInst(const ExtractValueInst &EVI);
1391 void init(const unsigned *Idx, unsigned NumIdx,
1392 const Twine &NameStr);
1393 void init(unsigned Idx, const Twine &NameStr);
1395 template<typename InputIterator>
1396 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1397 const Twine &NameStr,
1398 // This argument ensures that we have an iterator we can
1399 // do arithmetic on in constant time
1400 std::random_access_iterator_tag) {
1401 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1403 // There's no fundamental reason why we require at least one index
1404 // (other than weirdness with &*IdxBegin being invalid; see
1405 // getelementptr's init routine for example). But there's no
1406 // present need to support it.
1407 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1409 // This requires that the iterator points to contiguous memory.
1410 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1411 // we have to build an array here
1414 /// getIndexedType - Returns the type of the element that would be extracted
1415 /// with an extractvalue instruction with the specified parameters.
1417 /// Null is returned if the indices are invalid for the specified
1420 static const Type *getIndexedType(const Type *Agg,
1421 const unsigned *Idx, unsigned NumIdx);
1423 template<typename InputIterator>
1424 static const Type *getIndexedType(const Type *Ptr,
1425 InputIterator IdxBegin,
1426 InputIterator IdxEnd,
1427 // This argument ensures that we
1428 // have an iterator we can do
1429 // arithmetic on in constant time
1430 std::random_access_iterator_tag) {
1431 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1434 // This requires that the iterator points to contiguous memory.
1435 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1437 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1440 /// Constructors - Create a extractvalue instruction with a base aggregate
1441 /// value and a list of indices. The first ctor can optionally insert before
1442 /// an existing instruction, the second appends the new instruction to the
1443 /// specified BasicBlock.
1444 template<typename InputIterator>
1445 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1446 InputIterator IdxEnd,
1447 const Twine &NameStr,
1448 Instruction *InsertBefore);
1449 template<typename InputIterator>
1450 inline ExtractValueInst(Value *Agg,
1451 InputIterator IdxBegin, InputIterator IdxEnd,
1452 const Twine &NameStr, BasicBlock *InsertAtEnd);
1454 // allocate space for exactly one operand
1455 void *operator new(size_t s) {
1456 return User::operator new(s, 1);
1459 virtual ExtractValueInst *clone_impl() const;
1462 template<typename InputIterator>
1463 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1464 InputIterator IdxEnd,
1465 const Twine &NameStr = "",
1466 Instruction *InsertBefore = 0) {
1468 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1470 template<typename InputIterator>
1471 static ExtractValueInst *Create(Value *Agg,
1472 InputIterator IdxBegin, InputIterator IdxEnd,
1473 const Twine &NameStr,
1474 BasicBlock *InsertAtEnd) {
1475 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1478 /// Constructors - These two creators are convenience methods because one
1479 /// index extractvalue instructions are much more common than those with
1481 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1482 const Twine &NameStr = "",
1483 Instruction *InsertBefore = 0) {
1484 unsigned Idxs[1] = { Idx };
1485 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1487 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1488 const Twine &NameStr,
1489 BasicBlock *InsertAtEnd) {
1490 unsigned Idxs[1] = { Idx };
1491 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1494 /// getIndexedType - Returns the type of the element that would be extracted
1495 /// with an extractvalue instruction with the specified parameters.
1497 /// Null is returned if the indices are invalid for the specified
1500 template<typename InputIterator>
1501 static const Type *getIndexedType(const Type *Ptr,
1502 InputIterator IdxBegin,
1503 InputIterator IdxEnd) {
1504 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1505 typename std::iterator_traits<InputIterator>::
1506 iterator_category());
1508 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1510 typedef const unsigned* idx_iterator;
1511 inline idx_iterator idx_begin() const { return Indices.begin(); }
1512 inline idx_iterator idx_end() const { return Indices.end(); }
1514 Value *getAggregateOperand() {
1515 return getOperand(0);
1517 const Value *getAggregateOperand() const {
1518 return getOperand(0);
1520 static unsigned getAggregateOperandIndex() {
1521 return 0U; // get index for modifying correct operand
1524 unsigned getNumIndices() const { // Note: always non-negative
1525 return (unsigned)Indices.size();
1528 bool hasIndices() const {
1532 // Methods for support type inquiry through isa, cast, and dyn_cast:
1533 static inline bool classof(const ExtractValueInst *) { return true; }
1534 static inline bool classof(const Instruction *I) {
1535 return I->getOpcode() == Instruction::ExtractValue;
1537 static inline bool classof(const Value *V) {
1538 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1542 template<typename InputIterator>
1543 ExtractValueInst::ExtractValueInst(Value *Agg,
1544 InputIterator IdxBegin,
1545 InputIterator IdxEnd,
1546 const Twine &NameStr,
1547 Instruction *InsertBefore)
1548 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1550 ExtractValue, Agg, InsertBefore) {
1551 init(IdxBegin, IdxEnd, NameStr,
1552 typename std::iterator_traits<InputIterator>::iterator_category());
1554 template<typename InputIterator>
1555 ExtractValueInst::ExtractValueInst(Value *Agg,
1556 InputIterator IdxBegin,
1557 InputIterator IdxEnd,
1558 const Twine &NameStr,
1559 BasicBlock *InsertAtEnd)
1560 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1562 ExtractValue, Agg, InsertAtEnd) {
1563 init(IdxBegin, IdxEnd, NameStr,
1564 typename std::iterator_traits<InputIterator>::iterator_category());
1568 //===----------------------------------------------------------------------===//
1569 // InsertValueInst Class
1570 //===----------------------------------------------------------------------===//
1572 /// InsertValueInst - This instruction inserts a struct field of array element
1573 /// value into an aggregate value.
1575 class InsertValueInst : public Instruction {
1576 SmallVector<unsigned, 4> Indices;
1578 void *operator new(size_t, unsigned); // Do not implement
1579 InsertValueInst(const InsertValueInst &IVI);
1580 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1581 const Twine &NameStr);
1582 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1584 template<typename InputIterator>
1585 void init(Value *Agg, Value *Val,
1586 InputIterator IdxBegin, InputIterator IdxEnd,
1587 const Twine &NameStr,
1588 // This argument ensures that we have an iterator we can
1589 // do arithmetic on in constant time
1590 std::random_access_iterator_tag) {
1591 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1593 // There's no fundamental reason why we require at least one index
1594 // (other than weirdness with &*IdxBegin being invalid; see
1595 // getelementptr's init routine for example). But there's no
1596 // present need to support it.
1597 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1599 // This requires that the iterator points to contiguous memory.
1600 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1601 // we have to build an array here
1604 /// Constructors - Create a insertvalue instruction with a base aggregate
1605 /// value, a value to insert, and a list of indices. The first ctor can
1606 /// optionally insert before an existing instruction, the second appends
1607 /// the new instruction to the specified BasicBlock.
1608 template<typename InputIterator>
1609 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1610 InputIterator IdxEnd,
1611 const Twine &NameStr,
1612 Instruction *InsertBefore);
1613 template<typename InputIterator>
1614 inline InsertValueInst(Value *Agg, Value *Val,
1615 InputIterator IdxBegin, InputIterator IdxEnd,
1616 const Twine &NameStr, BasicBlock *InsertAtEnd);
1618 /// Constructors - These two constructors are convenience methods because one
1619 /// and two index insertvalue instructions are so common.
1620 InsertValueInst(Value *Agg, Value *Val,
1621 unsigned Idx, const Twine &NameStr = "",
1622 Instruction *InsertBefore = 0);
1623 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1624 const Twine &NameStr, BasicBlock *InsertAtEnd);
1626 virtual InsertValueInst *clone_impl() const;
1628 // allocate space for exactly two operands
1629 void *operator new(size_t s) {
1630 return User::operator new(s, 2);
1633 template<typename InputIterator>
1634 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1635 InputIterator IdxEnd,
1636 const Twine &NameStr = "",
1637 Instruction *InsertBefore = 0) {
1638 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1639 NameStr, InsertBefore);
1641 template<typename InputIterator>
1642 static InsertValueInst *Create(Value *Agg, Value *Val,
1643 InputIterator IdxBegin, InputIterator IdxEnd,
1644 const Twine &NameStr,
1645 BasicBlock *InsertAtEnd) {
1646 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1647 NameStr, InsertAtEnd);
1650 /// Constructors - These two creators are convenience methods because one
1651 /// index insertvalue instructions are much more common than those with
1653 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1654 const Twine &NameStr = "",
1655 Instruction *InsertBefore = 0) {
1656 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1658 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1659 const Twine &NameStr,
1660 BasicBlock *InsertAtEnd) {
1661 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1664 /// Transparently provide more efficient getOperand methods.
1665 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1667 typedef const unsigned* idx_iterator;
1668 inline idx_iterator idx_begin() const { return Indices.begin(); }
1669 inline idx_iterator idx_end() const { return Indices.end(); }
1671 Value *getAggregateOperand() {
1672 return getOperand(0);
1674 const Value *getAggregateOperand() const {
1675 return getOperand(0);
1677 static unsigned getAggregateOperandIndex() {
1678 return 0U; // get index for modifying correct operand
1681 Value *getInsertedValueOperand() {
1682 return getOperand(1);
1684 const Value *getInsertedValueOperand() const {
1685 return getOperand(1);
1687 static unsigned getInsertedValueOperandIndex() {
1688 return 1U; // get index for modifying correct operand
1691 unsigned getNumIndices() const { // Note: always non-negative
1692 return (unsigned)Indices.size();
1695 bool hasIndices() const {
1699 // Methods for support type inquiry through isa, cast, and dyn_cast:
1700 static inline bool classof(const InsertValueInst *) { return true; }
1701 static inline bool classof(const Instruction *I) {
1702 return I->getOpcode() == Instruction::InsertValue;
1704 static inline bool classof(const Value *V) {
1705 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1710 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1713 template<typename InputIterator>
1714 InsertValueInst::InsertValueInst(Value *Agg,
1716 InputIterator IdxBegin,
1717 InputIterator IdxEnd,
1718 const Twine &NameStr,
1719 Instruction *InsertBefore)
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());
1726 template<typename InputIterator>
1727 InsertValueInst::InsertValueInst(Value *Agg,
1729 InputIterator IdxBegin,
1730 InputIterator IdxEnd,
1731 const Twine &NameStr,
1732 BasicBlock *InsertAtEnd)
1733 : Instruction(Agg->getType(), InsertValue,
1734 OperandTraits<InsertValueInst>::op_begin(this),
1736 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1737 typename std::iterator_traits<InputIterator>::iterator_category());
1740 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1742 //===----------------------------------------------------------------------===//
1744 //===----------------------------------------------------------------------===//
1746 // PHINode - The PHINode class is used to represent the magical mystical PHI
1747 // node, that can not exist in nature, but can be synthesized in a computer
1748 // scientist's overactive imagination.
1750 class PHINode : public Instruction {
1751 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1752 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1753 /// the number actually in use.
1754 unsigned ReservedSpace;
1755 PHINode(const PHINode &PN);
1756 // allocate space for exactly zero operands
1757 void *operator new(size_t s) {
1758 return User::operator new(s, 0);
1760 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1761 Instruction *InsertBefore = 0)
1762 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1767 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1768 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1773 virtual PHINode *clone_impl() const;
1775 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1776 Instruction *InsertBefore = 0) {
1777 return new PHINode(Ty, NameStr, InsertBefore);
1779 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1780 BasicBlock *InsertAtEnd) {
1781 return new PHINode(Ty, NameStr, InsertAtEnd);
1785 /// reserveOperandSpace - This method can be used to avoid repeated
1786 /// reallocation of PHI operand lists by reserving space for the correct
1787 /// number of operands before adding them. Unlike normal vector reserves,
1788 /// this method can also be used to trim the operand space.
1789 void reserveOperandSpace(unsigned NumValues) {
1790 resizeOperands(NumValues*2);
1793 /// Provide fast operand accessors
1794 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1796 /// getNumIncomingValues - Return the number of incoming edges
1798 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1800 /// getIncomingValue - Return incoming value number x
1802 Value *getIncomingValue(unsigned i) const {
1803 assert(i*2 < getNumOperands() && "Invalid value number!");
1804 return getOperand(i*2);
1806 void setIncomingValue(unsigned i, Value *V) {
1807 assert(i*2 < getNumOperands() && "Invalid value number!");
1810 static unsigned getOperandNumForIncomingValue(unsigned i) {
1813 static unsigned getIncomingValueNumForOperand(unsigned i) {
1814 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1818 /// getIncomingBlock - Return incoming basic block number @p i.
1820 BasicBlock *getIncomingBlock(unsigned i) const {
1821 return cast<BasicBlock>(getOperand(i*2+1));
1824 /// getIncomingBlock - Return incoming basic block corresponding
1825 /// to an operand of the PHI.
1827 BasicBlock *getIncomingBlock(const Use &U) const {
1828 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1829 return cast<BasicBlock>((&U + 1)->get());
1832 /// getIncomingBlock - Return incoming basic block corresponding
1833 /// to value use iterator.
1835 template <typename U>
1836 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1837 return getIncomingBlock(I.getUse());
1841 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1842 setOperand(i*2+1, (Value*)BB);
1844 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1847 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1848 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1852 /// addIncoming - Add an incoming value to the end of the PHI list
1854 void addIncoming(Value *V, BasicBlock *BB) {
1855 assert(V && "PHI node got a null value!");
1856 assert(BB && "PHI node got a null basic block!");
1857 assert(getType() == V->getType() &&
1858 "All operands to PHI node must be the same type as the PHI node!");
1859 unsigned OpNo = NumOperands;
1860 if (OpNo+2 > ReservedSpace)
1861 resizeOperands(0); // Get more space!
1862 // Initialize some new operands.
1863 NumOperands = OpNo+2;
1864 OperandList[OpNo] = V;
1865 OperandList[OpNo+1] = (Value*)BB;
1868 /// removeIncomingValue - Remove an incoming value. This is useful if a
1869 /// predecessor basic block is deleted. The value removed is returned.
1871 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1872 /// is true), the PHI node is destroyed and any uses of it are replaced with
1873 /// dummy values. The only time there should be zero incoming values to a PHI
1874 /// node is when the block is dead, so this strategy is sound.
1876 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1878 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1879 int Idx = getBasicBlockIndex(BB);
1880 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1881 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1884 /// getBasicBlockIndex - Return the first index of the specified basic
1885 /// block in the value list for this PHI. Returns -1 if no instance.
1887 int getBasicBlockIndex(const BasicBlock *BB) const {
1888 Use *OL = OperandList;
1889 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1890 if (OL[i+1].get() == (const Value*)BB) return i/2;
1894 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1895 return getIncomingValue(getBasicBlockIndex(BB));
1898 /// hasConstantValue - If the specified PHI node always merges together the
1899 /// same value, return the value, otherwise return null.
1901 /// If the PHI has undef operands, but all the rest of the operands are
1902 /// some unique value, return that value if it can be proved that the
1903 /// value dominates the PHI. If DT is null, use a conservative check,
1904 /// otherwise use DT to test for dominance.
1906 Value *hasConstantValue(DominatorTree *DT = 0) const;
1908 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1909 static inline bool classof(const PHINode *) { return true; }
1910 static inline bool classof(const Instruction *I) {
1911 return I->getOpcode() == Instruction::PHI;
1913 static inline bool classof(const Value *V) {
1914 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1917 void resizeOperands(unsigned NumOperands);
1921 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1924 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1927 //===----------------------------------------------------------------------===//
1929 //===----------------------------------------------------------------------===//
1931 //===---------------------------------------------------------------------------
1932 /// ReturnInst - Return a value (possibly void), from a function. Execution
1933 /// does not continue in this function any longer.
1935 class ReturnInst : public TerminatorInst {
1936 ReturnInst(const ReturnInst &RI);
1939 // ReturnInst constructors:
1940 // ReturnInst() - 'ret void' instruction
1941 // ReturnInst( null) - 'ret void' instruction
1942 // ReturnInst(Value* X) - 'ret X' instruction
1943 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1944 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1945 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1946 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1948 // NOTE: If the Value* passed is of type void then the constructor behaves as
1949 // if it was passed NULL.
1950 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1951 Instruction *InsertBefore = 0);
1952 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1953 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1955 virtual ReturnInst *clone_impl() const;
1957 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1958 Instruction *InsertBefore = 0) {
1959 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1961 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1962 BasicBlock *InsertAtEnd) {
1963 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1965 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1966 return new(0) ReturnInst(C, InsertAtEnd);
1968 virtual ~ReturnInst();
1970 /// Provide fast operand accessors
1971 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1973 /// Convenience accessor
1974 Value *getReturnValue(unsigned n = 0) const {
1975 return n < getNumOperands()
1980 unsigned getNumSuccessors() const { return 0; }
1982 // Methods for support type inquiry through isa, cast, and dyn_cast:
1983 static inline bool classof(const ReturnInst *) { return true; }
1984 static inline bool classof(const Instruction *I) {
1985 return (I->getOpcode() == Instruction::Ret);
1987 static inline bool classof(const Value *V) {
1988 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1991 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1992 virtual unsigned getNumSuccessorsV() const;
1993 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1997 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<> {
2000 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2002 //===----------------------------------------------------------------------===//
2004 //===----------------------------------------------------------------------===//
2006 //===---------------------------------------------------------------------------
2007 /// BranchInst - Conditional or Unconditional Branch instruction.
2009 class BranchInst : public TerminatorInst {
2010 /// Ops list - Branches are strange. The operands are ordered:
2011 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2012 /// they don't have to check for cond/uncond branchness. These are mostly
2013 /// accessed relative from op_end().
2014 BranchInst(const BranchInst &BI);
2016 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2017 // BranchInst(BB *B) - 'br B'
2018 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2019 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2020 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2021 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2022 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2023 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2024 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2025 Instruction *InsertBefore = 0);
2026 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2027 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2028 BasicBlock *InsertAtEnd);
2030 virtual BranchInst *clone_impl() const;
2032 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2033 return new(1, true) BranchInst(IfTrue, InsertBefore);
2035 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2036 Value *Cond, Instruction *InsertBefore = 0) {
2037 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2039 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2040 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2042 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2043 Value *Cond, BasicBlock *InsertAtEnd) {
2044 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2049 /// Transparently provide more efficient getOperand methods.
2050 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2052 bool isUnconditional() const { return getNumOperands() == 1; }
2053 bool isConditional() const { return getNumOperands() == 3; }
2055 Value *getCondition() const {
2056 assert(isConditional() && "Cannot get condition of an uncond branch!");
2060 void setCondition(Value *V) {
2061 assert(isConditional() && "Cannot set condition of unconditional branch!");
2065 // setUnconditionalDest - Change the current branch to an unconditional branch
2066 // targeting the specified block.
2067 // FIXME: Eliminate this ugly method.
2068 void setUnconditionalDest(BasicBlock *Dest) {
2069 Op<-1>() = (Value*)Dest;
2070 if (isConditional()) { // Convert this to an uncond branch.
2074 OperandList = op_begin();
2078 unsigned getNumSuccessors() const { return 1+isConditional(); }
2080 BasicBlock *getSuccessor(unsigned i) const {
2081 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2082 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2085 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2086 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2087 *(&Op<-1>() - idx) = (Value*)NewSucc;
2090 // Methods for support type inquiry through isa, cast, and dyn_cast:
2091 static inline bool classof(const BranchInst *) { return true; }
2092 static inline bool classof(const Instruction *I) {
2093 return (I->getOpcode() == Instruction::Br);
2095 static inline bool classof(const Value *V) {
2096 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2099 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2100 virtual unsigned getNumSuccessorsV() const;
2101 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2105 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2107 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2109 //===----------------------------------------------------------------------===//
2111 //===----------------------------------------------------------------------===//
2113 //===---------------------------------------------------------------------------
2114 /// SwitchInst - Multiway switch
2116 class SwitchInst : public TerminatorInst {
2117 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2118 unsigned ReservedSpace;
2119 // Operand[0] = Value to switch on
2120 // Operand[1] = Default basic block destination
2121 // Operand[2n ] = Value to match
2122 // Operand[2n+1] = BasicBlock to go to on match
2123 SwitchInst(const SwitchInst &SI);
2124 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2125 void resizeOperands(unsigned No);
2126 // allocate space for exactly zero operands
2127 void *operator new(size_t s) {
2128 return User::operator new(s, 0);
2130 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2131 /// switch on and a default destination. The number of additional cases can
2132 /// be specified here to make memory allocation more efficient. This
2133 /// constructor can also autoinsert before another instruction.
2134 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2135 Instruction *InsertBefore);
2137 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2138 /// switch on and a default destination. The number of additional cases can
2139 /// be specified here to make memory allocation more efficient. This
2140 /// constructor also autoinserts at the end of the specified BasicBlock.
2141 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2142 BasicBlock *InsertAtEnd);
2144 virtual SwitchInst *clone_impl() const;
2146 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2147 unsigned NumCases, Instruction *InsertBefore = 0) {
2148 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2150 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2151 unsigned NumCases, BasicBlock *InsertAtEnd) {
2152 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2156 /// Provide fast operand accessors
2157 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2159 // Accessor Methods for Switch stmt
2160 Value *getCondition() const { return getOperand(0); }
2161 void setCondition(Value *V) { setOperand(0, V); }
2163 BasicBlock *getDefaultDest() const {
2164 return cast<BasicBlock>(getOperand(1));
2167 /// getNumCases - return the number of 'cases' in this switch instruction.
2168 /// Note that case #0 is always the default case.
2169 unsigned getNumCases() const {
2170 return getNumOperands()/2;
2173 /// getCaseValue - Return the specified case value. Note that case #0, the
2174 /// default destination, does not have a case value.
2175 ConstantInt *getCaseValue(unsigned i) {
2176 assert(i && i < getNumCases() && "Illegal case value to get!");
2177 return getSuccessorValue(i);
2180 /// getCaseValue - Return the specified case value. Note that case #0, the
2181 /// default destination, does not have a case value.
2182 const ConstantInt *getCaseValue(unsigned i) const {
2183 assert(i && i < getNumCases() && "Illegal case value to get!");
2184 return getSuccessorValue(i);
2187 /// findCaseValue - Search all of the case values for the specified constant.
2188 /// If it is explicitly handled, return the case number of it, otherwise
2189 /// return 0 to indicate that it is handled by the default handler.
2190 unsigned findCaseValue(const ConstantInt *C) const {
2191 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2192 if (getCaseValue(i) == C)
2197 /// findCaseDest - Finds the unique case value for a given successor. Returns
2198 /// null if the successor is not found, not unique, or is the default case.
2199 ConstantInt *findCaseDest(BasicBlock *BB) {
2200 if (BB == getDefaultDest()) return NULL;
2202 ConstantInt *CI = NULL;
2203 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2204 if (getSuccessor(i) == BB) {
2205 if (CI) return NULL; // Multiple cases lead to BB.
2206 else CI = getCaseValue(i);
2212 /// addCase - Add an entry to the switch instruction...
2214 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2216 /// removeCase - This method removes the specified successor from the switch
2217 /// instruction. Note that this cannot be used to remove the default
2218 /// destination (successor #0).
2220 void removeCase(unsigned idx);
2222 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2223 BasicBlock *getSuccessor(unsigned idx) const {
2224 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2225 return cast<BasicBlock>(getOperand(idx*2+1));
2227 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2228 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2229 setOperand(idx*2+1, (Value*)NewSucc);
2232 // getSuccessorValue - Return the value associated with the specified
2234 ConstantInt *getSuccessorValue(unsigned idx) const {
2235 assert(idx < getNumSuccessors() && "Successor # out of range!");
2236 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2239 // Methods for support type inquiry through isa, cast, and dyn_cast:
2240 static inline bool classof(const SwitchInst *) { return true; }
2241 static inline bool classof(const Instruction *I) {
2242 return I->getOpcode() == Instruction::Switch;
2244 static inline bool classof(const Value *V) {
2245 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2248 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2249 virtual unsigned getNumSuccessorsV() const;
2250 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2254 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2257 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2260 //===----------------------------------------------------------------------===//
2261 // IndirectBrInst Class
2262 //===----------------------------------------------------------------------===//
2264 //===---------------------------------------------------------------------------
2265 /// IndirectBrInst - Indirect Branch Instruction.
2267 class IndirectBrInst : public TerminatorInst {
2268 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2269 unsigned ReservedSpace;
2270 // Operand[0] = Value to switch on
2271 // Operand[1] = Default basic block destination
2272 // Operand[2n ] = Value to match
2273 // Operand[2n+1] = BasicBlock to go to on match
2274 IndirectBrInst(const IndirectBrInst &IBI);
2275 void init(Value *Address, unsigned NumDests);
2276 void resizeOperands(unsigned No);
2277 // allocate space for exactly zero operands
2278 void *operator new(size_t s) {
2279 return User::operator new(s, 0);
2281 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2282 /// Address to jump to. The number of expected destinations can be specified
2283 /// here to make memory allocation more efficient. This constructor can also
2284 /// autoinsert before another instruction.
2285 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2287 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2288 /// Address to jump to. The number of expected destinations can be specified
2289 /// here to make memory allocation more efficient. This constructor also
2290 /// autoinserts at the end of the specified BasicBlock.
2291 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2293 virtual IndirectBrInst *clone_impl() const;
2295 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2296 Instruction *InsertBefore = 0) {
2297 return new IndirectBrInst(Address, NumDests, InsertBefore);
2299 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2300 BasicBlock *InsertAtEnd) {
2301 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2305 /// Provide fast operand accessors.
2306 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2308 // Accessor Methods for IndirectBrInst instruction.
2309 Value *getAddress() { return getOperand(0); }
2310 const Value *getAddress() const { return getOperand(0); }
2311 void setAddress(Value *V) { setOperand(0, V); }
2314 /// getNumDestinations - return the number of possible destinations in this
2315 /// indirectbr instruction.
2316 unsigned getNumDestinations() const { return getNumOperands()-1; }
2318 /// getDestination - Return the specified destination.
2319 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2320 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2322 /// addDestination - Add a destination.
2324 void addDestination(BasicBlock *Dest);
2326 /// removeDestination - This method removes the specified successor from the
2327 /// indirectbr instruction.
2328 void removeDestination(unsigned i);
2330 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2331 BasicBlock *getSuccessor(unsigned i) const {
2332 return cast<BasicBlock>(getOperand(i+1));
2334 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2335 setOperand(i+1, (Value*)NewSucc);
2338 // Methods for support type inquiry through isa, cast, and dyn_cast:
2339 static inline bool classof(const IndirectBrInst *) { return true; }
2340 static inline bool classof(const Instruction *I) {
2341 return I->getOpcode() == Instruction::IndirectBr;
2343 static inline bool classof(const Value *V) {
2344 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2347 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2348 virtual unsigned getNumSuccessorsV() const;
2349 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2353 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2356 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2359 //===----------------------------------------------------------------------===//
2361 //===----------------------------------------------------------------------===//
2363 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2364 /// calling convention of the call.
2366 class InvokeInst : public TerminatorInst {
2367 AttrListPtr AttributeList;
2368 InvokeInst(const InvokeInst &BI);
2369 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2370 Value* const *Args, unsigned NumArgs);
2372 template<typename InputIterator>
2373 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2374 InputIterator ArgBegin, InputIterator ArgEnd,
2375 const Twine &NameStr,
2376 // This argument ensures that we have an iterator we can
2377 // do arithmetic on in constant time
2378 std::random_access_iterator_tag) {
2379 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2381 // This requires that the iterator points to contiguous memory.
2382 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
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, Instruction *InsertBefore);
2399 /// Construct an InvokeInst given a range of arguments.
2400 /// InputIterator must be a random-access iterator pointing to
2401 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2402 /// made for random-accessness but not for contiguous storage as
2403 /// that would incur runtime overhead.
2405 /// @brief Construct an InvokeInst from a range of arguments
2406 template<typename InputIterator>
2407 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2408 InputIterator ArgBegin, InputIterator ArgEnd,
2410 const Twine &NameStr, BasicBlock *InsertAtEnd);
2412 virtual InvokeInst *clone_impl() const;
2414 template<typename InputIterator>
2415 static InvokeInst *Create(Value *Func,
2416 BasicBlock *IfNormal, BasicBlock *IfException,
2417 InputIterator ArgBegin, InputIterator ArgEnd,
2418 const Twine &NameStr = "",
2419 Instruction *InsertBefore = 0) {
2420 unsigned Values(ArgEnd - ArgBegin + 3);
2421 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2422 Values, NameStr, InsertBefore);
2424 template<typename InputIterator>
2425 static InvokeInst *Create(Value *Func,
2426 BasicBlock *IfNormal, BasicBlock *IfException,
2427 InputIterator ArgBegin, InputIterator ArgEnd,
2428 const Twine &NameStr,
2429 BasicBlock *InsertAtEnd) {
2430 unsigned Values(ArgEnd - ArgBegin + 3);
2431 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2432 Values, NameStr, InsertAtEnd);
2435 /// Provide fast operand accessors
2436 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2438 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2439 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2441 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2443 CallingConv::ID getCallingConv() const {
2444 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2446 void setCallingConv(CallingConv::ID CC) {
2447 setInstructionSubclassData(static_cast<unsigned>(CC));
2450 /// getAttributes - Return the parameter attributes for this invoke.
2452 const AttrListPtr &getAttributes() const { return AttributeList; }
2454 /// setAttributes - Set the parameter attributes for this invoke.
2456 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2458 /// addAttribute - adds the attribute to the list of attributes.
2459 void addAttribute(unsigned i, Attributes attr);
2461 /// removeAttribute - removes the attribute from the list of attributes.
2462 void removeAttribute(unsigned i, Attributes attr);
2464 /// @brief Determine whether the call or the callee has the given attribute.
2465 bool paramHasAttr(unsigned i, Attributes attr) const;
2467 /// @brief Extract the alignment for a call or parameter (0=unknown).
2468 unsigned getParamAlignment(unsigned i) const {
2469 return AttributeList.getParamAlignment(i);
2472 /// @brief Return true if the call should not be inlined.
2473 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
2474 void setIsNoInline(bool Value) {
2475 if (Value) addAttribute(~0, Attribute::NoInline);
2476 else removeAttribute(~0, Attribute::NoInline);
2479 /// @brief Determine if the call does not access memory.
2480 bool doesNotAccessMemory() const {
2481 return paramHasAttr(~0, Attribute::ReadNone);
2483 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2484 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2485 else removeAttribute(~0, Attribute::ReadNone);
2488 /// @brief Determine if the call does not access or only reads memory.
2489 bool onlyReadsMemory() const {
2490 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2492 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2493 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2494 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2497 /// @brief Determine if the call cannot return.
2498 bool doesNotReturn() const {
2499 return paramHasAttr(~0, Attribute::NoReturn);
2501 void setDoesNotReturn(bool DoesNotReturn = true) {
2502 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2503 else removeAttribute(~0, Attribute::NoReturn);
2506 /// @brief Determine if the call cannot unwind.
2507 bool doesNotThrow() const {
2508 return paramHasAttr(~0, Attribute::NoUnwind);
2510 void setDoesNotThrow(bool DoesNotThrow = true) {
2511 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2512 else removeAttribute(~0, Attribute::NoUnwind);
2515 /// @brief Determine if the call returns a structure through first
2516 /// pointer argument.
2517 bool hasStructRetAttr() const {
2518 // Be friendly and also check the callee.
2519 return paramHasAttr(1, Attribute::StructRet);
2522 /// @brief Determine if any call argument is an aggregate passed by value.
2523 bool hasByValArgument() const {
2524 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2527 /// getCalledFunction - Return the function called, or null if this is an
2528 /// indirect function invocation.
2530 Function *getCalledFunction() const {
2531 return dyn_cast<Function>(Op<-3>());
2534 /// getCalledValue - Get a pointer to the function that is invoked by this
2536 const Value *getCalledValue() const { return Op<-3>(); }
2537 Value *getCalledValue() { return Op<-3>(); }
2539 /// setCalledFunction - Set the function called.
2540 void setCalledFunction(Value* Fn) {
2544 // get*Dest - Return the destination basic blocks...
2545 BasicBlock *getNormalDest() const {
2546 return cast<BasicBlock>(Op<-2>());
2548 BasicBlock *getUnwindDest() const {
2549 return cast<BasicBlock>(Op<-1>());
2551 void setNormalDest(BasicBlock *B) {
2552 Op<-2>() = reinterpret_cast<Value*>(B);
2554 void setUnwindDest(BasicBlock *B) {
2555 Op<-1>() = reinterpret_cast<Value*>(B);
2558 BasicBlock *getSuccessor(unsigned i) const {
2559 assert(i < 2 && "Successor # out of range for invoke!");
2560 return i == 0 ? getNormalDest() : getUnwindDest();
2563 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2564 assert(idx < 2 && "Successor # out of range for invoke!");
2565 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
2568 unsigned getNumSuccessors() const { return 2; }
2570 // Methods for support type inquiry through isa, cast, and dyn_cast:
2571 static inline bool classof(const InvokeInst *) { return true; }
2572 static inline bool classof(const Instruction *I) {
2573 return (I->getOpcode() == Instruction::Invoke);
2575 static inline bool classof(const Value *V) {
2576 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2580 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2581 virtual unsigned getNumSuccessorsV() const;
2582 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2584 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2585 // method so that subclasses cannot accidentally use it.
2586 void setInstructionSubclassData(unsigned short D) {
2587 Instruction::setInstructionSubclassData(D);
2592 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2595 template<typename InputIterator>
2596 InvokeInst::InvokeInst(Value *Func,
2597 BasicBlock *IfNormal, BasicBlock *IfException,
2598 InputIterator ArgBegin, InputIterator ArgEnd,
2600 const Twine &NameStr, Instruction *InsertBefore)
2601 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2602 ->getElementType())->getReturnType(),
2603 Instruction::Invoke,
2604 OperandTraits<InvokeInst>::op_end(this) - Values,
2605 Values, InsertBefore) {
2606 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2607 typename std::iterator_traits<InputIterator>::iterator_category());
2609 template<typename InputIterator>
2610 InvokeInst::InvokeInst(Value *Func,
2611 BasicBlock *IfNormal, BasicBlock *IfException,
2612 InputIterator ArgBegin, InputIterator ArgEnd,
2614 const Twine &NameStr, BasicBlock *InsertAtEnd)
2615 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2616 ->getElementType())->getReturnType(),
2617 Instruction::Invoke,
2618 OperandTraits<InvokeInst>::op_end(this) - Values,
2619 Values, InsertAtEnd) {
2620 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2621 typename std::iterator_traits<InputIterator>::iterator_category());
2624 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2626 //===----------------------------------------------------------------------===//
2628 //===----------------------------------------------------------------------===//
2630 //===---------------------------------------------------------------------------
2631 /// UnwindInst - Immediately exit the current function, unwinding the stack
2632 /// until an invoke instruction is found.
2634 class UnwindInst : public TerminatorInst {
2635 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2637 virtual UnwindInst *clone_impl() const;
2639 // allocate space for exactly zero operands
2640 void *operator new(size_t s) {
2641 return User::operator new(s, 0);
2643 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2644 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2646 unsigned getNumSuccessors() const { return 0; }
2648 // Methods for support type inquiry through isa, cast, and dyn_cast:
2649 static inline bool classof(const UnwindInst *) { return true; }
2650 static inline bool classof(const Instruction *I) {
2651 return I->getOpcode() == Instruction::Unwind;
2653 static inline bool classof(const Value *V) {
2654 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2657 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2658 virtual unsigned getNumSuccessorsV() const;
2659 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2662 //===----------------------------------------------------------------------===//
2663 // UnreachableInst Class
2664 //===----------------------------------------------------------------------===//
2666 //===---------------------------------------------------------------------------
2667 /// UnreachableInst - This function has undefined behavior. In particular, the
2668 /// presence of this instruction indicates some higher level knowledge that the
2669 /// end of the block cannot be reached.
2671 class UnreachableInst : public TerminatorInst {
2672 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2674 virtual UnreachableInst *clone_impl() const;
2677 // allocate space for exactly zero operands
2678 void *operator new(size_t s) {
2679 return User::operator new(s, 0);
2681 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2682 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2684 unsigned getNumSuccessors() const { return 0; }
2686 // Methods for support type inquiry through isa, cast, and dyn_cast:
2687 static inline bool classof(const UnreachableInst *) { return true; }
2688 static inline bool classof(const Instruction *I) {
2689 return I->getOpcode() == Instruction::Unreachable;
2691 static inline bool classof(const Value *V) {
2692 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2695 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2696 virtual unsigned getNumSuccessorsV() const;
2697 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2700 //===----------------------------------------------------------------------===//
2702 //===----------------------------------------------------------------------===//
2704 /// @brief This class represents a truncation of integer types.
2705 class TruncInst : public CastInst {
2707 /// @brief Clone an identical TruncInst
2708 virtual TruncInst *clone_impl() const;
2711 /// @brief Constructor with insert-before-instruction semantics
2713 Value *S, ///< The value to be truncated
2714 const Type *Ty, ///< The (smaller) type to truncate to
2715 const Twine &NameStr = "", ///< A name for the new instruction
2716 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2719 /// @brief Constructor with insert-at-end-of-block semantics
2721 Value *S, ///< The value to be truncated
2722 const Type *Ty, ///< The (smaller) type to truncate to
2723 const Twine &NameStr, ///< A name for the new instruction
2724 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2727 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2728 static inline bool classof(const TruncInst *) { return true; }
2729 static inline bool classof(const Instruction *I) {
2730 return I->getOpcode() == Trunc;
2732 static inline bool classof(const Value *V) {
2733 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2737 //===----------------------------------------------------------------------===//
2739 //===----------------------------------------------------------------------===//
2741 /// @brief This class represents zero extension of integer types.
2742 class ZExtInst : public CastInst {
2744 /// @brief Clone an identical ZExtInst
2745 virtual ZExtInst *clone_impl() const;
2748 /// @brief Constructor with insert-before-instruction semantics
2750 Value *S, ///< The value to be zero extended
2751 const Type *Ty, ///< The type to zero extend to
2752 const Twine &NameStr = "", ///< A name for the new instruction
2753 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2756 /// @brief Constructor with insert-at-end semantics.
2758 Value *S, ///< The value to be zero extended
2759 const Type *Ty, ///< The type to zero extend to
2760 const Twine &NameStr, ///< A name for the new instruction
2761 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2764 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2765 static inline bool classof(const ZExtInst *) { return true; }
2766 static inline bool classof(const Instruction *I) {
2767 return I->getOpcode() == ZExt;
2769 static inline bool classof(const Value *V) {
2770 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2774 //===----------------------------------------------------------------------===//
2776 //===----------------------------------------------------------------------===//
2778 /// @brief This class represents a sign extension of integer types.
2779 class SExtInst : public CastInst {
2781 /// @brief Clone an identical SExtInst
2782 virtual SExtInst *clone_impl() const;
2785 /// @brief Constructor with insert-before-instruction semantics
2787 Value *S, ///< The value to be sign extended
2788 const Type *Ty, ///< The type to sign extend to
2789 const Twine &NameStr = "", ///< A name for the new instruction
2790 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2793 /// @brief Constructor with insert-at-end-of-block semantics
2795 Value *S, ///< The value to be sign extended
2796 const Type *Ty, ///< The type to sign extend to
2797 const Twine &NameStr, ///< A name for the new instruction
2798 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2801 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2802 static inline bool classof(const SExtInst *) { return true; }
2803 static inline bool classof(const Instruction *I) {
2804 return I->getOpcode() == SExt;
2806 static inline bool classof(const Value *V) {
2807 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2811 //===----------------------------------------------------------------------===//
2812 // FPTruncInst Class
2813 //===----------------------------------------------------------------------===//
2815 /// @brief This class represents a truncation of floating point types.
2816 class FPTruncInst : public CastInst {
2818 /// @brief Clone an identical FPTruncInst
2819 virtual FPTruncInst *clone_impl() const;
2822 /// @brief Constructor with insert-before-instruction semantics
2824 Value *S, ///< The value to be truncated
2825 const Type *Ty, ///< The type to truncate to
2826 const Twine &NameStr = "", ///< A name for the new instruction
2827 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2830 /// @brief Constructor with insert-before-instruction semantics
2832 Value *S, ///< The value to be truncated
2833 const Type *Ty, ///< The type to truncate to
2834 const Twine &NameStr, ///< A name for the new instruction
2835 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2838 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2839 static inline bool classof(const FPTruncInst *) { return true; }
2840 static inline bool classof(const Instruction *I) {
2841 return I->getOpcode() == FPTrunc;
2843 static inline bool classof(const Value *V) {
2844 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2848 //===----------------------------------------------------------------------===//
2850 //===----------------------------------------------------------------------===//
2852 /// @brief This class represents an extension of floating point types.
2853 class FPExtInst : public CastInst {
2855 /// @brief Clone an identical FPExtInst
2856 virtual FPExtInst *clone_impl() const;
2859 /// @brief Constructor with insert-before-instruction semantics
2861 Value *S, ///< The value to be extended
2862 const Type *Ty, ///< The type to extend to
2863 const Twine &NameStr = "", ///< A name for the new instruction
2864 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2867 /// @brief Constructor with insert-at-end-of-block semantics
2869 Value *S, ///< The value to be extended
2870 const Type *Ty, ///< The type to extend to
2871 const Twine &NameStr, ///< A name for the new instruction
2872 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2875 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2876 static inline bool classof(const FPExtInst *) { return true; }
2877 static inline bool classof(const Instruction *I) {
2878 return I->getOpcode() == FPExt;
2880 static inline bool classof(const Value *V) {
2881 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2885 //===----------------------------------------------------------------------===//
2887 //===----------------------------------------------------------------------===//
2889 /// @brief This class represents a cast unsigned integer to floating point.
2890 class UIToFPInst : public CastInst {
2892 /// @brief Clone an identical UIToFPInst
2893 virtual UIToFPInst *clone_impl() const;
2896 /// @brief Constructor with insert-before-instruction semantics
2898 Value *S, ///< The value to be converted
2899 const Type *Ty, ///< The type to convert to
2900 const Twine &NameStr = "", ///< A name for the new instruction
2901 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2904 /// @brief Constructor with insert-at-end-of-block semantics
2906 Value *S, ///< The value to be converted
2907 const Type *Ty, ///< The type to convert to
2908 const Twine &NameStr, ///< A name for the new instruction
2909 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2912 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2913 static inline bool classof(const UIToFPInst *) { return true; }
2914 static inline bool classof(const Instruction *I) {
2915 return I->getOpcode() == UIToFP;
2917 static inline bool classof(const Value *V) {
2918 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2922 //===----------------------------------------------------------------------===//
2924 //===----------------------------------------------------------------------===//
2926 /// @brief This class represents a cast from signed integer to floating point.
2927 class SIToFPInst : public CastInst {
2929 /// @brief Clone an identical SIToFPInst
2930 virtual SIToFPInst *clone_impl() const;
2933 /// @brief Constructor with insert-before-instruction semantics
2935 Value *S, ///< The value to be converted
2936 const Type *Ty, ///< The type to convert to
2937 const Twine &NameStr = "", ///< A name for the new instruction
2938 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2941 /// @brief Constructor with insert-at-end-of-block semantics
2943 Value *S, ///< The value to be converted
2944 const Type *Ty, ///< The type to convert to
2945 const Twine &NameStr, ///< A name for the new instruction
2946 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2949 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2950 static inline bool classof(const SIToFPInst *) { return true; }
2951 static inline bool classof(const Instruction *I) {
2952 return I->getOpcode() == SIToFP;
2954 static inline bool classof(const Value *V) {
2955 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2959 //===----------------------------------------------------------------------===//
2961 //===----------------------------------------------------------------------===//
2963 /// @brief This class represents a cast from floating point to unsigned integer
2964 class FPToUIInst : public CastInst {
2966 /// @brief Clone an identical FPToUIInst
2967 virtual FPToUIInst *clone_impl() const;
2970 /// @brief Constructor with insert-before-instruction semantics
2972 Value *S, ///< The value to be converted
2973 const Type *Ty, ///< The type to convert to
2974 const Twine &NameStr = "", ///< A name for the new instruction
2975 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2978 /// @brief Constructor with insert-at-end-of-block semantics
2980 Value *S, ///< The value to be converted
2981 const Type *Ty, ///< The type to convert to
2982 const Twine &NameStr, ///< A name for the new instruction
2983 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2986 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2987 static inline bool classof(const FPToUIInst *) { return true; }
2988 static inline bool classof(const Instruction *I) {
2989 return I->getOpcode() == FPToUI;
2991 static inline bool classof(const Value *V) {
2992 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2996 //===----------------------------------------------------------------------===//
2998 //===----------------------------------------------------------------------===//
3000 /// @brief This class represents a cast from floating point to signed integer.
3001 class FPToSIInst : public CastInst {
3003 /// @brief Clone an identical FPToSIInst
3004 virtual FPToSIInst *clone_impl() const;
3007 /// @brief Constructor with insert-before-instruction semantics
3009 Value *S, ///< The value to be converted
3010 const Type *Ty, ///< The type to convert to
3011 const Twine &NameStr = "", ///< A name for the new instruction
3012 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3015 /// @brief Constructor with insert-at-end-of-block semantics
3017 Value *S, ///< The value to be converted
3018 const Type *Ty, ///< The type to convert to
3019 const Twine &NameStr, ///< A name for the new instruction
3020 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3023 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3024 static inline bool classof(const FPToSIInst *) { return true; }
3025 static inline bool classof(const Instruction *I) {
3026 return I->getOpcode() == FPToSI;
3028 static inline bool classof(const Value *V) {
3029 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3033 //===----------------------------------------------------------------------===//
3034 // IntToPtrInst Class
3035 //===----------------------------------------------------------------------===//
3037 /// @brief This class represents a cast from an integer to a pointer.
3038 class IntToPtrInst : public CastInst {
3040 /// @brief Constructor with insert-before-instruction semantics
3042 Value *S, ///< The value to be converted
3043 const Type *Ty, ///< The type to convert to
3044 const Twine &NameStr = "", ///< A name for the new instruction
3045 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3048 /// @brief Constructor with insert-at-end-of-block semantics
3050 Value *S, ///< The value to be converted
3051 const Type *Ty, ///< The type to convert to
3052 const Twine &NameStr, ///< A name for the new instruction
3053 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3056 /// @brief Clone an identical IntToPtrInst
3057 virtual IntToPtrInst *clone_impl() const;
3059 // Methods for support type inquiry through isa, cast, and dyn_cast:
3060 static inline bool classof(const IntToPtrInst *) { return true; }
3061 static inline bool classof(const Instruction *I) {
3062 return I->getOpcode() == IntToPtr;
3064 static inline bool classof(const Value *V) {
3065 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3069 //===----------------------------------------------------------------------===//
3070 // PtrToIntInst Class
3071 //===----------------------------------------------------------------------===//
3073 /// @brief This class represents a cast from a pointer to an integer
3074 class PtrToIntInst : public CastInst {
3076 /// @brief Clone an identical PtrToIntInst
3077 virtual PtrToIntInst *clone_impl() const;
3080 /// @brief Constructor with insert-before-instruction semantics
3082 Value *S, ///< The value to be converted
3083 const Type *Ty, ///< The type to convert to
3084 const Twine &NameStr = "", ///< A name for the new instruction
3085 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3088 /// @brief Constructor with insert-at-end-of-block semantics
3090 Value *S, ///< The value to be converted
3091 const Type *Ty, ///< The type to convert to
3092 const Twine &NameStr, ///< A name for the new instruction
3093 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3096 // Methods for support type inquiry through isa, cast, and dyn_cast:
3097 static inline bool classof(const PtrToIntInst *) { return true; }
3098 static inline bool classof(const Instruction *I) {
3099 return I->getOpcode() == PtrToInt;
3101 static inline bool classof(const Value *V) {
3102 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3106 //===----------------------------------------------------------------------===//
3107 // BitCastInst Class
3108 //===----------------------------------------------------------------------===//
3110 /// @brief This class represents a no-op cast from one type to another.
3111 class BitCastInst : public CastInst {
3113 /// @brief Clone an identical BitCastInst
3114 virtual BitCastInst *clone_impl() const;
3117 /// @brief Constructor with insert-before-instruction semantics
3119 Value *S, ///< The value to be casted
3120 const Type *Ty, ///< The type to casted to
3121 const Twine &NameStr = "", ///< A name for the new instruction
3122 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3125 /// @brief Constructor with insert-at-end-of-block semantics
3127 Value *S, ///< The value to be casted
3128 const Type *Ty, ///< The type to casted to
3129 const Twine &NameStr, ///< A name for the new instruction
3130 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3133 // Methods for support type inquiry through isa, cast, and dyn_cast:
3134 static inline bool classof(const BitCastInst *) { return true; }
3135 static inline bool classof(const Instruction *I) {
3136 return I->getOpcode() == BitCast;
3138 static inline bool classof(const Value *V) {
3139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3143 } // End llvm namespace