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 /// MaximumAlignment - This is the greatest alignment value supported by
35 /// load, store, and alloca instructions.
36 static const unsigned MaximumAlignment = 1u << 29;
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
42 /// AllocaInst - an instruction to allocate memory on the stack
44 class AllocaInst : public UnaryInstruction {
46 virtual AllocaInst *clone_impl() const;
48 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
49 const Twine &Name = "", Instruction *InsertBefore = 0);
50 AllocaInst(const Type *Ty, Value *ArraySize,
51 const Twine &Name, BasicBlock *InsertAtEnd);
53 AllocaInst(const Type *Ty, const Twine &Name, Instruction *InsertBefore = 0);
54 AllocaInst(const Type *Ty, const Twine &Name, BasicBlock *InsertAtEnd);
56 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
57 const Twine &Name = "", Instruction *InsertBefore = 0);
58 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
59 const Twine &Name, BasicBlock *InsertAtEnd);
61 // Out of line virtual method, so the vtable, etc. has a home.
62 virtual ~AllocaInst();
64 /// isArrayAllocation - Return true if there is an allocation size parameter
65 /// to the allocation instruction that is not 1.
67 bool isArrayAllocation() const;
69 /// getArraySize - Get the number of elements allocated. For a simple
70 /// allocation of a single element, this will return a constant 1 value.
72 const Value *getArraySize() const { return getOperand(0); }
73 Value *getArraySize() { return getOperand(0); }
75 /// getType - Overload to return most specific pointer type
77 const PointerType *getType() const {
78 return reinterpret_cast<const PointerType*>(Instruction::getType());
81 /// getAllocatedType - Return the type that is being allocated by the
84 const Type *getAllocatedType() const;
86 /// getAlignment - Return the alignment of the memory that is being allocated
87 /// by the instruction.
89 unsigned getAlignment() const {
90 return (1u << getSubclassDataFromInstruction()) >> 1;
92 void setAlignment(unsigned Align);
94 /// isStaticAlloca - Return true if this alloca is in the entry block of the
95 /// function and is a constant size. If so, the code generator will fold it
96 /// into the prolog/epilog code, so it is basically free.
97 bool isStaticAlloca() const;
99 // Methods for support type inquiry through isa, cast, and dyn_cast:
100 static inline bool classof(const AllocaInst *) { return true; }
101 static inline bool classof(const Instruction *I) {
102 return (I->getOpcode() == Instruction::Alloca);
104 static inline bool classof(const Value *V) {
105 return isa<Instruction>(V) && classof(cast<Instruction>(V));
108 // Shadow Instruction::setInstructionSubclassData with a private forwarding
109 // method so that subclasses cannot accidentally use it.
110 void setInstructionSubclassData(unsigned short D) {
111 Instruction::setInstructionSubclassData(D);
116 //===----------------------------------------------------------------------===//
118 //===----------------------------------------------------------------------===//
120 /// LoadInst - an instruction for reading from memory. This uses the
121 /// SubclassData field in Value to store whether or not the load is volatile.
123 class LoadInst : public UnaryInstruction {
126 virtual LoadInst *clone_impl() const;
128 LoadInst(Value *Ptr, const Twine &NameStr, Instruction *InsertBefore);
129 LoadInst(Value *Ptr, const Twine &NameStr, BasicBlock *InsertAtEnd);
130 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile = false,
131 Instruction *InsertBefore = 0);
132 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
133 unsigned Align, Instruction *InsertBefore = 0);
134 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
135 BasicBlock *InsertAtEnd);
136 LoadInst(Value *Ptr, const Twine &NameStr, bool isVolatile,
137 unsigned Align, BasicBlock *InsertAtEnd);
139 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
140 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
141 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
142 bool isVolatile = false, Instruction *InsertBefore = 0);
143 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
144 BasicBlock *InsertAtEnd);
146 /// isVolatile - Return true if this is a load from a volatile memory
149 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
151 /// setVolatile - Specify whether this is a volatile load or not.
153 void setVolatile(bool V) {
154 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
158 /// getAlignment - Return the alignment of the access that is being performed
160 unsigned getAlignment() const {
161 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
164 void setAlignment(unsigned Align);
166 Value *getPointerOperand() { return getOperand(0); }
167 const Value *getPointerOperand() const { return getOperand(0); }
168 static unsigned getPointerOperandIndex() { return 0U; }
170 unsigned getPointerAddressSpace() const {
171 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
175 // Methods for support type inquiry through isa, cast, and dyn_cast:
176 static inline bool classof(const LoadInst *) { return true; }
177 static inline bool classof(const Instruction *I) {
178 return I->getOpcode() == Instruction::Load;
180 static inline bool classof(const Value *V) {
181 return isa<Instruction>(V) && classof(cast<Instruction>(V));
184 // Shadow Instruction::setInstructionSubclassData with a private forwarding
185 // method so that subclasses cannot accidentally use it.
186 void setInstructionSubclassData(unsigned short D) {
187 Instruction::setInstructionSubclassData(D);
192 //===----------------------------------------------------------------------===//
194 //===----------------------------------------------------------------------===//
196 /// StoreInst - an instruction for storing to memory
198 class StoreInst : public Instruction {
199 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
202 virtual StoreInst *clone_impl() const;
204 // allocate space for exactly two operands
205 void *operator new(size_t s) {
206 return User::operator new(s, 2);
208 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
209 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
210 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
211 Instruction *InsertBefore = 0);
212 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
213 unsigned Align, Instruction *InsertBefore = 0);
214 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
215 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
216 unsigned Align, BasicBlock *InsertAtEnd);
219 /// isVolatile - Return true if this is a load from a volatile memory
222 bool isVolatile() const { return getSubclassDataFromInstruction() & 1; }
224 /// setVolatile - Specify whether this is a volatile load or not.
226 void setVolatile(bool V) {
227 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
231 /// Transparently provide more efficient getOperand methods.
232 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
234 /// getAlignment - Return the alignment of the access that is being performed
236 unsigned getAlignment() const {
237 return (1 << (getSubclassDataFromInstruction() >> 1)) >> 1;
240 void setAlignment(unsigned Align);
242 Value *getValueOperand() { return getOperand(0); }
243 const Value *getValueOperand() const { return getOperand(0); }
245 Value *getPointerOperand() { return getOperand(1); }
246 const Value *getPointerOperand() const { return getOperand(1); }
247 static unsigned getPointerOperandIndex() { return 1U; }
249 unsigned getPointerAddressSpace() const {
250 return cast<PointerType>(getPointerOperand()->getType())->getAddressSpace();
253 // Methods for support type inquiry through isa, cast, and dyn_cast:
254 static inline bool classof(const StoreInst *) { return true; }
255 static inline bool classof(const Instruction *I) {
256 return I->getOpcode() == Instruction::Store;
258 static inline bool classof(const Value *V) {
259 return isa<Instruction>(V) && classof(cast<Instruction>(V));
262 // Shadow Instruction::setInstructionSubclassData with a private forwarding
263 // method so that subclasses cannot accidentally use it.
264 void setInstructionSubclassData(unsigned short D) {
265 Instruction::setInstructionSubclassData(D);
270 struct OperandTraits<StoreInst> : public FixedNumOperandTraits<2> {
273 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
275 //===----------------------------------------------------------------------===//
276 // GetElementPtrInst Class
277 //===----------------------------------------------------------------------===//
279 // checkType - Simple wrapper function to give a better assertion failure
280 // message on bad indexes for a gep instruction.
282 static inline const Type *checkType(const Type *Ty) {
283 assert(Ty && "Invalid GetElementPtrInst indices for type!");
287 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
288 /// access elements of arrays and structs
290 class GetElementPtrInst : public Instruction {
291 GetElementPtrInst(const GetElementPtrInst &GEPI);
292 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
293 const Twine &NameStr);
294 void init(Value *Ptr, Value *Idx, const Twine &NameStr);
296 template<typename InputIterator>
297 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
298 const Twine &NameStr,
299 // This argument ensures that we have an iterator we can
300 // do arithmetic on in constant time
301 std::random_access_iterator_tag) {
302 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
305 // This requires that the iterator points to contiguous memory.
306 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
307 // we have to build an array here
310 init(Ptr, 0, NumIdx, NameStr);
314 /// getIndexedType - Returns the type of the element that would be loaded with
315 /// a load instruction with the specified parameters.
317 /// Null is returned if the indices are invalid for the specified
320 template<typename InputIterator>
321 static const Type *getIndexedType(const Type *Ptr,
322 InputIterator IdxBegin,
323 InputIterator IdxEnd,
324 // This argument ensures that we
325 // have an iterator we can do
326 // arithmetic on in constant time
327 std::random_access_iterator_tag) {
328 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
331 // This requires that the iterator points to contiguous memory.
332 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
334 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
337 /// Constructors - Create a getelementptr instruction with a base pointer an
338 /// list of indices. The first ctor can optionally insert before an existing
339 /// instruction, the second appends the new instruction to the specified
341 template<typename InputIterator>
342 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
343 InputIterator IdxEnd,
345 const Twine &NameStr,
346 Instruction *InsertBefore);
347 template<typename InputIterator>
348 inline GetElementPtrInst(Value *Ptr,
349 InputIterator IdxBegin, InputIterator IdxEnd,
351 const Twine &NameStr, BasicBlock *InsertAtEnd);
353 /// Constructors - These two constructors are convenience methods because one
354 /// and two index getelementptr instructions are so common.
355 GetElementPtrInst(Value *Ptr, Value *Idx, const Twine &NameStr = "",
356 Instruction *InsertBefore = 0);
357 GetElementPtrInst(Value *Ptr, Value *Idx,
358 const Twine &NameStr, BasicBlock *InsertAtEnd);
360 virtual GetElementPtrInst *clone_impl() const;
362 template<typename InputIterator>
363 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
364 InputIterator IdxEnd,
365 const Twine &NameStr = "",
366 Instruction *InsertBefore = 0) {
367 typename std::iterator_traits<InputIterator>::difference_type Values =
368 1 + std::distance(IdxBegin, IdxEnd);
370 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
372 template<typename InputIterator>
373 static GetElementPtrInst *Create(Value *Ptr,
374 InputIterator IdxBegin, InputIterator IdxEnd,
375 const Twine &NameStr,
376 BasicBlock *InsertAtEnd) {
377 typename std::iterator_traits<InputIterator>::difference_type Values =
378 1 + std::distance(IdxBegin, IdxEnd);
380 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
383 /// Constructors - These two creators are convenience methods because one
384 /// index getelementptr instructions are so common.
385 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
386 const Twine &NameStr = "",
387 Instruction *InsertBefore = 0) {
388 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
390 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
391 const Twine &NameStr,
392 BasicBlock *InsertAtEnd) {
393 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
396 /// Create an "inbounds" getelementptr. See the documentation for the
397 /// "inbounds" flag in LangRef.html for details.
398 template<typename InputIterator>
399 static GetElementPtrInst *CreateInBounds(Value *Ptr, InputIterator IdxBegin,
400 InputIterator IdxEnd,
401 const Twine &NameStr = "",
402 Instruction *InsertBefore = 0) {
403 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
404 NameStr, InsertBefore);
405 GEP->setIsInBounds(true);
408 template<typename InputIterator>
409 static GetElementPtrInst *CreateInBounds(Value *Ptr,
410 InputIterator IdxBegin,
411 InputIterator IdxEnd,
412 const Twine &NameStr,
413 BasicBlock *InsertAtEnd) {
414 GetElementPtrInst *GEP = Create(Ptr, IdxBegin, IdxEnd,
415 NameStr, InsertAtEnd);
416 GEP->setIsInBounds(true);
419 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
420 const Twine &NameStr = "",
421 Instruction *InsertBefore = 0) {
422 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertBefore);
423 GEP->setIsInBounds(true);
426 static GetElementPtrInst *CreateInBounds(Value *Ptr, Value *Idx,
427 const Twine &NameStr,
428 BasicBlock *InsertAtEnd) {
429 GetElementPtrInst *GEP = Create(Ptr, Idx, NameStr, InsertAtEnd);
430 GEP->setIsInBounds(true);
434 /// Transparently provide more efficient getOperand methods.
435 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
437 // getType - Overload to return most specific pointer type...
438 const PointerType *getType() const {
439 return reinterpret_cast<const PointerType*>(Instruction::getType());
442 /// getIndexedType - Returns the type of the element that would be loaded with
443 /// a load instruction with the specified parameters.
445 /// Null is returned if the indices are invalid for the specified
448 template<typename InputIterator>
449 static const Type *getIndexedType(const Type *Ptr,
450 InputIterator IdxBegin,
451 InputIterator IdxEnd) {
452 return getIndexedType(Ptr, IdxBegin, IdxEnd,
453 typename std::iterator_traits<InputIterator>::
454 iterator_category());
457 static const Type *getIndexedType(const Type *Ptr,
458 Value* const *Idx, unsigned NumIdx);
460 static const Type *getIndexedType(const Type *Ptr,
461 uint64_t const *Idx, unsigned NumIdx);
463 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
465 inline op_iterator idx_begin() { return op_begin()+1; }
466 inline const_op_iterator idx_begin() const { return op_begin()+1; }
467 inline op_iterator idx_end() { return op_end(); }
468 inline const_op_iterator idx_end() const { return op_end(); }
470 Value *getPointerOperand() {
471 return getOperand(0);
473 const Value *getPointerOperand() const {
474 return getOperand(0);
476 static unsigned getPointerOperandIndex() {
477 return 0U; // get index for modifying correct operand
480 unsigned getPointerAddressSpace() const {
481 return cast<PointerType>(getType())->getAddressSpace();
484 /// getPointerOperandType - Method to return the pointer operand as a
486 const PointerType *getPointerOperandType() const {
487 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
491 unsigned getNumIndices() const { // Note: always non-negative
492 return getNumOperands() - 1;
495 bool hasIndices() const {
496 return getNumOperands() > 1;
499 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
500 /// zeros. If so, the result pointer and the first operand have the same
501 /// value, just potentially different types.
502 bool hasAllZeroIndices() const;
504 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
505 /// constant integers. If so, the result pointer and the first operand have
506 /// a constant offset between them.
507 bool hasAllConstantIndices() const;
509 /// setIsInBounds - Set or clear the inbounds flag on this GEP instruction.
510 /// See LangRef.html for the meaning of inbounds on a getelementptr.
511 void setIsInBounds(bool b = true);
513 /// isInBounds - Determine whether the GEP has the inbounds flag.
514 bool isInBounds() const;
516 // Methods for support type inquiry through isa, cast, and dyn_cast:
517 static inline bool classof(const GetElementPtrInst *) { return true; }
518 static inline bool classof(const Instruction *I) {
519 return (I->getOpcode() == Instruction::GetElementPtr);
521 static inline bool classof(const Value *V) {
522 return isa<Instruction>(V) && classof(cast<Instruction>(V));
527 struct OperandTraits<GetElementPtrInst> : public VariadicOperandTraits<1> {
530 template<typename InputIterator>
531 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
532 InputIterator IdxBegin,
533 InputIterator IdxEnd,
535 const Twine &NameStr,
536 Instruction *InsertBefore)
537 : Instruction(PointerType::get(checkType(
538 getIndexedType(Ptr->getType(),
540 cast<PointerType>(Ptr->getType())
541 ->getAddressSpace()),
543 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
544 Values, InsertBefore) {
545 init(Ptr, IdxBegin, IdxEnd, NameStr,
546 typename std::iterator_traits<InputIterator>::iterator_category());
548 template<typename InputIterator>
549 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
550 InputIterator IdxBegin,
551 InputIterator IdxEnd,
553 const Twine &NameStr,
554 BasicBlock *InsertAtEnd)
555 : Instruction(PointerType::get(checkType(
556 getIndexedType(Ptr->getType(),
558 cast<PointerType>(Ptr->getType())
559 ->getAddressSpace()),
561 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
562 Values, InsertAtEnd) {
563 init(Ptr, IdxBegin, IdxEnd, NameStr,
564 typename std::iterator_traits<InputIterator>::iterator_category());
568 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
571 //===----------------------------------------------------------------------===//
573 //===----------------------------------------------------------------------===//
575 /// This instruction compares its operands according to the predicate given
576 /// to the constructor. It only operates on integers or pointers. The operands
577 /// must be identical types.
578 /// @brief Represent an integer comparison operator.
579 class ICmpInst: public CmpInst {
581 /// @brief Clone an indentical ICmpInst
582 virtual ICmpInst *clone_impl() const;
584 /// @brief Constructor with insert-before-instruction semantics.
586 Instruction *InsertBefore, ///< Where to insert
587 Predicate pred, ///< The predicate to use for the comparison
588 Value *LHS, ///< The left-hand-side of the expression
589 Value *RHS, ///< The right-hand-side of the expression
590 const Twine &NameStr = "" ///< Name of the instruction
591 ) : CmpInst(makeCmpResultType(LHS->getType()),
592 Instruction::ICmp, pred, LHS, RHS, NameStr,
594 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
595 pred <= CmpInst::LAST_ICMP_PREDICATE &&
596 "Invalid ICmp predicate value");
597 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
598 "Both operands to ICmp instruction are not of the same type!");
599 // Check that the operands are the right type
600 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
601 getOperand(0)->getType()->isPointerTy()) &&
602 "Invalid operand types for ICmp instruction");
605 /// @brief Constructor with insert-at-end semantics.
607 BasicBlock &InsertAtEnd, ///< Block to insert into.
608 Predicate pred, ///< The predicate to use for the comparison
609 Value *LHS, ///< The left-hand-side of the expression
610 Value *RHS, ///< The right-hand-side of the expression
611 const Twine &NameStr = "" ///< Name of the instruction
612 ) : CmpInst(makeCmpResultType(LHS->getType()),
613 Instruction::ICmp, pred, LHS, RHS, NameStr,
615 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
616 pred <= CmpInst::LAST_ICMP_PREDICATE &&
617 "Invalid ICmp predicate value");
618 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
619 "Both operands to ICmp instruction are not of the same type!");
620 // Check that the operands are the right type
621 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
622 getOperand(0)->getType()->isPointerTy()) &&
623 "Invalid operand types for ICmp instruction");
626 /// @brief Constructor with no-insertion semantics
628 Predicate pred, ///< The predicate to use for the comparison
629 Value *LHS, ///< The left-hand-side of the expression
630 Value *RHS, ///< The right-hand-side of the expression
631 const Twine &NameStr = "" ///< Name of the instruction
632 ) : CmpInst(makeCmpResultType(LHS->getType()),
633 Instruction::ICmp, pred, LHS, RHS, NameStr) {
634 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
635 pred <= CmpInst::LAST_ICMP_PREDICATE &&
636 "Invalid ICmp predicate value");
637 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
638 "Both operands to ICmp instruction are not of the same type!");
639 // Check that the operands are the right type
640 assert((getOperand(0)->getType()->isIntOrIntVectorTy() ||
641 getOperand(0)->getType()->isPointerTy()) &&
642 "Invalid operand types for ICmp instruction");
645 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
646 /// @returns the predicate that would be the result if the operand were
647 /// regarded as signed.
648 /// @brief Return the signed version of the predicate
649 Predicate getSignedPredicate() const {
650 return getSignedPredicate(getPredicate());
653 /// This is a static version that you can use without an instruction.
654 /// @brief Return the signed version of the predicate.
655 static Predicate getSignedPredicate(Predicate pred);
657 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
658 /// @returns the predicate that would be the result if the operand were
659 /// regarded as unsigned.
660 /// @brief Return the unsigned version of the predicate
661 Predicate getUnsignedPredicate() const {
662 return getUnsignedPredicate(getPredicate());
665 /// This is a static version that you can use without an instruction.
666 /// @brief Return the unsigned version of the predicate.
667 static Predicate getUnsignedPredicate(Predicate pred);
669 /// isEquality - Return true if this predicate is either EQ or NE. This also
670 /// tests for commutativity.
671 static bool isEquality(Predicate P) {
672 return P == ICMP_EQ || P == ICMP_NE;
675 /// isEquality - Return true if this predicate is either EQ or NE. This also
676 /// tests for commutativity.
677 bool isEquality() const {
678 return isEquality(getPredicate());
681 /// @returns true if the predicate of this ICmpInst is commutative
682 /// @brief Determine if this relation is commutative.
683 bool isCommutative() const { return isEquality(); }
685 /// isRelational - Return true if the predicate is relational (not EQ or NE).
687 bool isRelational() const {
688 return !isEquality();
691 /// isRelational - Return true if the predicate is relational (not EQ or NE).
693 static bool isRelational(Predicate P) {
694 return !isEquality(P);
697 /// Initialize a set of values that all satisfy the predicate with C.
698 /// @brief Make a ConstantRange for a relation with a constant value.
699 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
701 /// Exchange the two operands to this instruction in such a way that it does
702 /// not modify the semantics of the instruction. The predicate value may be
703 /// changed to retain the same result if the predicate is order dependent
705 /// @brief Swap operands and adjust predicate.
706 void swapOperands() {
707 setPredicate(getSwappedPredicate());
708 Op<0>().swap(Op<1>());
711 // Methods for support type inquiry through isa, cast, and dyn_cast:
712 static inline bool classof(const ICmpInst *) { return true; }
713 static inline bool classof(const Instruction *I) {
714 return I->getOpcode() == Instruction::ICmp;
716 static inline bool classof(const Value *V) {
717 return isa<Instruction>(V) && classof(cast<Instruction>(V));
722 //===----------------------------------------------------------------------===//
724 //===----------------------------------------------------------------------===//
726 /// This instruction compares its operands according to the predicate given
727 /// to the constructor. It only operates on floating point values or packed
728 /// vectors of floating point values. The operands must be identical types.
729 /// @brief Represents a floating point comparison operator.
730 class FCmpInst: public CmpInst {
732 /// @brief Clone an indentical FCmpInst
733 virtual FCmpInst *clone_impl() const;
735 /// @brief Constructor with insert-before-instruction semantics.
737 Instruction *InsertBefore, ///< Where to insert
738 Predicate pred, ///< The predicate to use for the comparison
739 Value *LHS, ///< The left-hand-side of the expression
740 Value *RHS, ///< The right-hand-side of the expression
741 const Twine &NameStr = "" ///< Name of the instruction
742 ) : CmpInst(makeCmpResultType(LHS->getType()),
743 Instruction::FCmp, pred, LHS, RHS, NameStr,
745 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
746 "Invalid FCmp predicate value");
747 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
748 "Both operands to FCmp instruction are not of the same type!");
749 // Check that the operands are the right type
750 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
751 "Invalid operand types for FCmp instruction");
754 /// @brief Constructor with insert-at-end semantics.
756 BasicBlock &InsertAtEnd, ///< Block to insert into.
757 Predicate pred, ///< The predicate to use for the comparison
758 Value *LHS, ///< The left-hand-side of the expression
759 Value *RHS, ///< The right-hand-side of the expression
760 const Twine &NameStr = "" ///< Name of the instruction
761 ) : CmpInst(makeCmpResultType(LHS->getType()),
762 Instruction::FCmp, pred, LHS, RHS, NameStr,
764 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
765 "Invalid FCmp predicate value");
766 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
767 "Both operands to FCmp instruction are not of the same type!");
768 // Check that the operands are the right type
769 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
770 "Invalid operand types for FCmp instruction");
773 /// @brief Constructor with no-insertion semantics
775 Predicate pred, ///< The predicate to use for the comparison
776 Value *LHS, ///< The left-hand-side of the expression
777 Value *RHS, ///< The right-hand-side of the expression
778 const Twine &NameStr = "" ///< Name of the instruction
779 ) : CmpInst(makeCmpResultType(LHS->getType()),
780 Instruction::FCmp, pred, LHS, RHS, NameStr) {
781 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
782 "Invalid FCmp predicate value");
783 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
784 "Both operands to FCmp instruction are not of the same type!");
785 // Check that the operands are the right type
786 assert(getOperand(0)->getType()->isFPOrFPVectorTy() &&
787 "Invalid operand types for FCmp instruction");
790 /// @returns true if the predicate of this instruction is EQ or NE.
791 /// @brief Determine if this is an equality predicate.
792 bool isEquality() const {
793 return getPredicate() == FCMP_OEQ || getPredicate() == FCMP_ONE ||
794 getPredicate() == FCMP_UEQ || getPredicate() == FCMP_UNE;
797 /// @returns true if the predicate of this instruction is commutative.
798 /// @brief Determine if this is a commutative predicate.
799 bool isCommutative() const {
800 return isEquality() ||
801 getPredicate() == FCMP_FALSE ||
802 getPredicate() == FCMP_TRUE ||
803 getPredicate() == FCMP_ORD ||
804 getPredicate() == FCMP_UNO;
807 /// @returns true if the predicate is relational (not EQ or NE).
808 /// @brief Determine if this a relational predicate.
809 bool isRelational() const { return !isEquality(); }
811 /// Exchange the two operands to this instruction in such a way that it does
812 /// not modify the semantics of the instruction. The predicate value may be
813 /// changed to retain the same result if the predicate is order dependent
815 /// @brief Swap operands and adjust predicate.
816 void swapOperands() {
817 setPredicate(getSwappedPredicate());
818 Op<0>().swap(Op<1>());
821 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
822 static inline bool classof(const FCmpInst *) { return true; }
823 static inline bool classof(const Instruction *I) {
824 return I->getOpcode() == Instruction::FCmp;
826 static inline bool classof(const Value *V) {
827 return isa<Instruction>(V) && classof(cast<Instruction>(V));
831 //===----------------------------------------------------------------------===//
832 /// CallInst - This class represents a function call, abstracting a target
833 /// machine's calling convention. This class uses low bit of the SubClassData
834 /// field to indicate whether or not this is a tail call. The rest of the bits
835 /// hold the calling convention of the call.
837 class CallInst : public Instruction {
838 AttrListPtr AttributeList; ///< parameter attributes for call
839 CallInst(const CallInst &CI);
840 void init(Value *Func, Value* const *Params, unsigned NumParams);
841 void init(Value *Func, Value *Actual1, Value *Actual2);
842 void init(Value *Func, Value *Actual);
843 void init(Value *Func);
845 template<typename InputIterator>
846 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
847 const Twine &NameStr,
848 // This argument ensures that we have an iterator we can
849 // do arithmetic on in constant time
850 std::random_access_iterator_tag) {
851 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
853 // This requires that the iterator points to contiguous memory.
854 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
858 /// Construct a CallInst given a range of arguments. InputIterator
859 /// must be a random-access iterator pointing to contiguous storage
860 /// (e.g. a std::vector<>::iterator). Checks are made for
861 /// random-accessness but not for contiguous storage as that would
862 /// incur runtime overhead.
863 /// @brief Construct a CallInst from a range of arguments
864 template<typename InputIterator>
865 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
866 const Twine &NameStr, Instruction *InsertBefore);
868 /// Construct a CallInst given a range of arguments. InputIterator
869 /// must be a random-access iterator pointing to contiguous storage
870 /// (e.g. a std::vector<>::iterator). Checks are made for
871 /// random-accessness but not for contiguous storage as that would
872 /// incur runtime overhead.
873 /// @brief Construct a CallInst from a range of arguments
874 template<typename InputIterator>
875 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
876 const Twine &NameStr, BasicBlock *InsertAtEnd);
878 CallInst(Value *F, Value *Actual, const Twine &NameStr,
879 Instruction *InsertBefore);
880 CallInst(Value *F, Value *Actual, const Twine &NameStr,
881 BasicBlock *InsertAtEnd);
882 explicit CallInst(Value *F, const Twine &NameStr,
883 Instruction *InsertBefore);
884 CallInst(Value *F, const Twine &NameStr, BasicBlock *InsertAtEnd);
886 virtual CallInst *clone_impl() const;
888 template<typename InputIterator>
889 static CallInst *Create(Value *Func,
890 InputIterator ArgBegin, InputIterator ArgEnd,
891 const Twine &NameStr = "",
892 Instruction *InsertBefore = 0) {
893 return new(unsigned(ArgEnd - ArgBegin + 1))
894 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
896 template<typename InputIterator>
897 static CallInst *Create(Value *Func,
898 InputIterator ArgBegin, InputIterator ArgEnd,
899 const Twine &NameStr, BasicBlock *InsertAtEnd) {
900 return new(unsigned(ArgEnd - ArgBegin + 1))
901 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
903 static CallInst *Create(Value *F, Value *Actual,
904 const Twine &NameStr = "",
905 Instruction *InsertBefore = 0) {
906 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
908 static CallInst *Create(Value *F, Value *Actual, const Twine &NameStr,
909 BasicBlock *InsertAtEnd) {
910 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
912 static CallInst *Create(Value *F, const Twine &NameStr = "",
913 Instruction *InsertBefore = 0) {
914 return new(1) CallInst(F, NameStr, InsertBefore);
916 static CallInst *Create(Value *F, const Twine &NameStr,
917 BasicBlock *InsertAtEnd) {
918 return new(1) CallInst(F, NameStr, InsertAtEnd);
920 /// CreateMalloc - Generate the IR for a call to malloc:
921 /// 1. Compute the malloc call's argument as the specified type's size,
922 /// possibly multiplied by the array size if the array size is not
924 /// 2. Call malloc with that argument.
925 /// 3. Bitcast the result of the malloc call to the specified type.
926 static Instruction *CreateMalloc(Instruction *InsertBefore,
927 const Type *IntPtrTy, const Type *AllocTy,
928 Value *AllocSize, Value *ArraySize = 0,
929 Function* MallocF = 0,
930 const Twine &Name = "");
931 static Instruction *CreateMalloc(BasicBlock *InsertAtEnd,
932 const Type *IntPtrTy, const Type *AllocTy,
933 Value *AllocSize, Value *ArraySize = 0,
934 Function* MallocF = 0,
935 const Twine &Name = "");
936 /// CreateFree - Generate the IR for a call to the builtin free function.
937 static Instruction* CreateFree(Value* Source, Instruction *InsertBefore);
938 static Instruction* CreateFree(Value* Source, BasicBlock *InsertAtEnd);
942 bool isTailCall() const { return getSubclassDataFromInstruction() & 1; }
943 void setTailCall(bool isTC = true) {
944 setInstructionSubclassData((getSubclassDataFromInstruction() & ~1) |
948 /// @deprecated these "define hacks" will go away soon
949 /// @brief coerce out-of-tree code to abandon the low-level interfaces
950 /// @detail see below comments and update your code to high-level interfaces
951 /// - getOperand(0) ---> getCalledValue(), or possibly getCalledFunction
952 /// - setOperand(0, V) ---> setCalledFunction(V)
954 /// in LLVM v2.8-only code
955 /// - getOperand(N+1) ---> getArgOperand(N)
956 /// - setOperand(N+1, V) ---> setArgOperand(N, V)
957 /// - getNumOperands() ---> getNumArgOperands()+1 // note the "+1"!
959 /// in backward compatible code please consult llvm/Support/CallSite.h,
960 /// you should create a callsite using the CallInst pointer and call its
963 # define public private
964 # define protected private
965 /// Provide fast operand accessors
966 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
971 unsigned getNumArgOperands() const { return getNumOperands() - 1; }
972 Value *getArgOperand(unsigned i) const { return getOperand(i); }
973 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
975 /// getCallingConv/setCallingConv - Get or set the calling convention of this
977 CallingConv::ID getCallingConv() const {
978 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction() >> 1);
980 void setCallingConv(CallingConv::ID CC) {
981 setInstructionSubclassData((getSubclassDataFromInstruction() & 1) |
982 (static_cast<unsigned>(CC) << 1));
985 /// getAttributes - Return the parameter attributes for this call.
987 const AttrListPtr &getAttributes() const { return AttributeList; }
989 /// setAttributes - Set the parameter attributes for this call.
991 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
993 /// addAttribute - adds the attribute to the list of attributes.
994 void addAttribute(unsigned i, Attributes attr);
996 /// removeAttribute - removes the attribute from the list of attributes.
997 void removeAttribute(unsigned i, Attributes attr);
999 /// @brief Determine whether the call or the callee has the given attribute.
1000 bool paramHasAttr(unsigned i, Attributes attr) const;
1002 /// @brief Extract the alignment for a call or parameter (0=unknown).
1003 unsigned getParamAlignment(unsigned i) const {
1004 return AttributeList.getParamAlignment(i);
1007 /// @brief Return true if the call should not be inlined.
1008 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
1009 void setIsNoInline(bool Value = true) {
1010 if (Value) addAttribute(~0, Attribute::NoInline);
1011 else removeAttribute(~0, Attribute::NoInline);
1014 /// @brief Determine if the call does not access memory.
1015 bool doesNotAccessMemory() const {
1016 return paramHasAttr(~0, Attribute::ReadNone);
1018 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1019 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1020 else removeAttribute(~0, Attribute::ReadNone);
1023 /// @brief Determine if the call does not access or only reads memory.
1024 bool onlyReadsMemory() const {
1025 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1027 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1028 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1029 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1032 /// @brief Determine if the call cannot return.
1033 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
1034 void setDoesNotReturn(bool DoesNotReturn = true) {
1035 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1036 else removeAttribute(~0, Attribute::NoReturn);
1039 /// @brief Determine if the call cannot unwind.
1040 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
1041 void setDoesNotThrow(bool DoesNotThrow = true) {
1042 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1043 else removeAttribute(~0, Attribute::NoUnwind);
1046 /// @brief Determine if the call returns a structure through first
1047 /// pointer argument.
1048 bool hasStructRetAttr() const {
1049 // Be friendly and also check the callee.
1050 return paramHasAttr(1, Attribute::StructRet);
1053 /// @brief Determine if any call argument is an aggregate passed by value.
1054 bool hasByValArgument() const {
1055 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1058 /// getCalledFunction - Return the function called, or null if this is an
1059 /// indirect function invocation.
1061 Function *getCalledFunction() const {
1062 return dyn_cast<Function>(Op<-1>());
1065 /// getCalledValue - Get a pointer to the function that is invoked by this
1067 const Value *getCalledValue() const { return Op<-1>(); }
1068 Value *getCalledValue() { return Op<-1>(); }
1070 /// setCalledFunction - Set the function called.
1071 void setCalledFunction(Value* Fn) {
1075 // Methods for support type inquiry through isa, cast, and dyn_cast:
1076 static inline bool classof(const CallInst *) { return true; }
1077 static inline bool classof(const Instruction *I) {
1078 return I->getOpcode() == Instruction::Call;
1080 static inline bool classof(const Value *V) {
1081 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1084 // Shadow Instruction::setInstructionSubclassData with a private forwarding
1085 // method so that subclasses cannot accidentally use it.
1086 void setInstructionSubclassData(unsigned short D) {
1087 Instruction::setInstructionSubclassData(D);
1092 struct OperandTraits<CallInst> : public VariadicOperandTraits<1> {
1095 template<typename InputIterator>
1096 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1097 const Twine &NameStr, BasicBlock *InsertAtEnd)
1098 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1099 ->getElementType())->getReturnType(),
1101 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1102 unsigned(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1103 init(Func, ArgBegin, ArgEnd, NameStr,
1104 typename std::iterator_traits<InputIterator>::iterator_category());
1107 template<typename InputIterator>
1108 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1109 const Twine &NameStr, Instruction *InsertBefore)
1110 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1111 ->getElementType())->getReturnType(),
1113 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1114 unsigned(ArgEnd - ArgBegin + 1), InsertBefore) {
1115 init(Func, ArgBegin, ArgEnd, NameStr,
1116 typename std::iterator_traits<InputIterator>::iterator_category());
1120 // Note: if you get compile errors about private methods then
1121 // please update your code to use the high-level operand
1122 // interfaces. See line 943 above.
1123 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1125 //===----------------------------------------------------------------------===//
1127 //===----------------------------------------------------------------------===//
1129 /// SelectInst - This class represents the LLVM 'select' instruction.
1131 class SelectInst : public Instruction {
1132 void init(Value *C, Value *S1, Value *S2) {
1133 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1139 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1140 Instruction *InsertBefore)
1141 : Instruction(S1->getType(), Instruction::Select,
1142 &Op<0>(), 3, InsertBefore) {
1146 SelectInst(Value *C, Value *S1, Value *S2, const Twine &NameStr,
1147 BasicBlock *InsertAtEnd)
1148 : Instruction(S1->getType(), Instruction::Select,
1149 &Op<0>(), 3, InsertAtEnd) {
1154 virtual SelectInst *clone_impl() const;
1156 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1157 const Twine &NameStr = "",
1158 Instruction *InsertBefore = 0) {
1159 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1161 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1162 const Twine &NameStr,
1163 BasicBlock *InsertAtEnd) {
1164 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1167 const Value *getCondition() const { return Op<0>(); }
1168 const Value *getTrueValue() const { return Op<1>(); }
1169 const Value *getFalseValue() const { return Op<2>(); }
1170 Value *getCondition() { return Op<0>(); }
1171 Value *getTrueValue() { return Op<1>(); }
1172 Value *getFalseValue() { return Op<2>(); }
1174 /// areInvalidOperands - Return a string if the specified operands are invalid
1175 /// for a select operation, otherwise return null.
1176 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1178 /// Transparently provide more efficient getOperand methods.
1179 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1181 OtherOps getOpcode() const {
1182 return static_cast<OtherOps>(Instruction::getOpcode());
1185 // Methods for support type inquiry through isa, cast, and dyn_cast:
1186 static inline bool classof(const SelectInst *) { return true; }
1187 static inline bool classof(const Instruction *I) {
1188 return I->getOpcode() == Instruction::Select;
1190 static inline bool classof(const Value *V) {
1191 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1196 struct OperandTraits<SelectInst> : public FixedNumOperandTraits<3> {
1199 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1201 //===----------------------------------------------------------------------===//
1203 //===----------------------------------------------------------------------===//
1205 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1206 /// an argument of the specified type given a va_list and increments that list
1208 class VAArgInst : public UnaryInstruction {
1210 virtual VAArgInst *clone_impl() const;
1213 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr = "",
1214 Instruction *InsertBefore = 0)
1215 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1218 VAArgInst(Value *List, const Type *Ty, const Twine &NameStr,
1219 BasicBlock *InsertAtEnd)
1220 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1224 // Methods for support type inquiry through isa, cast, and dyn_cast:
1225 static inline bool classof(const VAArgInst *) { return true; }
1226 static inline bool classof(const Instruction *I) {
1227 return I->getOpcode() == VAArg;
1229 static inline bool classof(const Value *V) {
1230 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1234 //===----------------------------------------------------------------------===//
1235 // ExtractElementInst Class
1236 //===----------------------------------------------------------------------===//
1238 /// ExtractElementInst - This instruction extracts a single (scalar)
1239 /// element from a VectorType value
1241 class ExtractElementInst : public Instruction {
1242 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr = "",
1243 Instruction *InsertBefore = 0);
1244 ExtractElementInst(Value *Vec, Value *Idx, const Twine &NameStr,
1245 BasicBlock *InsertAtEnd);
1247 virtual ExtractElementInst *clone_impl() const;
1250 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1251 const Twine &NameStr = "",
1252 Instruction *InsertBefore = 0) {
1253 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertBefore);
1255 static ExtractElementInst *Create(Value *Vec, Value *Idx,
1256 const Twine &NameStr,
1257 BasicBlock *InsertAtEnd) {
1258 return new(2) ExtractElementInst(Vec, Idx, NameStr, InsertAtEnd);
1261 /// isValidOperands - Return true if an extractelement instruction can be
1262 /// formed with the specified operands.
1263 static bool isValidOperands(const Value *Vec, const Value *Idx);
1265 Value *getVectorOperand() { return Op<0>(); }
1266 Value *getIndexOperand() { return Op<1>(); }
1267 const Value *getVectorOperand() const { return Op<0>(); }
1268 const Value *getIndexOperand() const { return Op<1>(); }
1270 const VectorType *getVectorOperandType() const {
1271 return reinterpret_cast<const VectorType*>(getVectorOperand()->getType());
1275 /// Transparently provide more efficient getOperand methods.
1276 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1278 // Methods for support type inquiry through isa, cast, and dyn_cast:
1279 static inline bool classof(const ExtractElementInst *) { return true; }
1280 static inline bool classof(const Instruction *I) {
1281 return I->getOpcode() == Instruction::ExtractElement;
1283 static inline bool classof(const Value *V) {
1284 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1289 struct OperandTraits<ExtractElementInst> : public FixedNumOperandTraits<2> {
1292 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1294 //===----------------------------------------------------------------------===//
1295 // InsertElementInst Class
1296 //===----------------------------------------------------------------------===//
1298 /// InsertElementInst - This instruction inserts a single (scalar)
1299 /// element into a VectorType value
1301 class InsertElementInst : public Instruction {
1302 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1303 const Twine &NameStr = "",
1304 Instruction *InsertBefore = 0);
1305 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1306 const Twine &NameStr, BasicBlock *InsertAtEnd);
1308 virtual InsertElementInst *clone_impl() const;
1311 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1312 const Twine &NameStr = "",
1313 Instruction *InsertBefore = 0) {
1314 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1316 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1317 const Twine &NameStr,
1318 BasicBlock *InsertAtEnd) {
1319 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1322 /// isValidOperands - Return true if an insertelement instruction can be
1323 /// formed with the specified operands.
1324 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1327 /// getType - Overload to return most specific vector type.
1329 const VectorType *getType() const {
1330 return reinterpret_cast<const VectorType*>(Instruction::getType());
1333 /// Transparently provide more efficient getOperand methods.
1334 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1336 // Methods for support type inquiry through isa, cast, and dyn_cast:
1337 static inline bool classof(const InsertElementInst *) { return true; }
1338 static inline bool classof(const Instruction *I) {
1339 return I->getOpcode() == Instruction::InsertElement;
1341 static inline bool classof(const Value *V) {
1342 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1347 struct OperandTraits<InsertElementInst> : public FixedNumOperandTraits<3> {
1350 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1352 //===----------------------------------------------------------------------===//
1353 // ShuffleVectorInst Class
1354 //===----------------------------------------------------------------------===//
1356 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1359 class ShuffleVectorInst : public Instruction {
1361 virtual ShuffleVectorInst *clone_impl() const;
1364 // allocate space for exactly three operands
1365 void *operator new(size_t s) {
1366 return User::operator new(s, 3);
1368 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1369 const Twine &NameStr = "",
1370 Instruction *InsertBefor = 0);
1371 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1372 const Twine &NameStr, BasicBlock *InsertAtEnd);
1374 /// isValidOperands - Return true if a shufflevector instruction can be
1375 /// formed with the specified operands.
1376 static bool isValidOperands(const Value *V1, const Value *V2,
1379 /// getType - Overload to return most specific vector type.
1381 const VectorType *getType() const {
1382 return reinterpret_cast<const VectorType*>(Instruction::getType());
1385 /// Transparently provide more efficient getOperand methods.
1386 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1388 /// getMaskValue - Return the index from the shuffle mask for the specified
1389 /// output result. This is either -1 if the element is undef or a number less
1390 /// than 2*numelements.
1391 int getMaskValue(unsigned i) const;
1393 // Methods for support type inquiry through isa, cast, and dyn_cast:
1394 static inline bool classof(const ShuffleVectorInst *) { return true; }
1395 static inline bool classof(const Instruction *I) {
1396 return I->getOpcode() == Instruction::ShuffleVector;
1398 static inline bool classof(const Value *V) {
1399 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1404 struct OperandTraits<ShuffleVectorInst> : public FixedNumOperandTraits<3> {
1407 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1409 //===----------------------------------------------------------------------===//
1410 // ExtractValueInst Class
1411 //===----------------------------------------------------------------------===//
1413 /// ExtractValueInst - This instruction extracts a struct member or array
1414 /// element value from an aggregate value.
1416 class ExtractValueInst : public UnaryInstruction {
1417 SmallVector<unsigned, 4> Indices;
1419 ExtractValueInst(const ExtractValueInst &EVI);
1420 void init(const unsigned *Idx, unsigned NumIdx,
1421 const Twine &NameStr);
1422 void init(unsigned Idx, const Twine &NameStr);
1424 template<typename InputIterator>
1425 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1426 const Twine &NameStr,
1427 // This argument ensures that we have an iterator we can
1428 // do arithmetic on in constant time
1429 std::random_access_iterator_tag) {
1430 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1432 // There's no fundamental reason why we require at least one index
1433 // (other than weirdness with &*IdxBegin being invalid; see
1434 // getelementptr's init routine for example). But there's no
1435 // present need to support it.
1436 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1438 // This requires that the iterator points to contiguous memory.
1439 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1440 // we have to build an array here
1443 /// getIndexedType - Returns the type of the element that would be extracted
1444 /// with an extractvalue instruction with the specified parameters.
1446 /// Null is returned if the indices are invalid for the specified
1449 static const Type *getIndexedType(const Type *Agg,
1450 const unsigned *Idx, unsigned NumIdx);
1452 template<typename InputIterator>
1453 static const Type *getIndexedType(const Type *Ptr,
1454 InputIterator IdxBegin,
1455 InputIterator IdxEnd,
1456 // This argument ensures that we
1457 // have an iterator we can do
1458 // arithmetic on in constant time
1459 std::random_access_iterator_tag) {
1460 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1463 // This requires that the iterator points to contiguous memory.
1464 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1466 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1469 /// Constructors - Create a extractvalue instruction with a base aggregate
1470 /// value and a list of indices. The first ctor can optionally insert before
1471 /// an existing instruction, the second appends the new instruction to the
1472 /// specified BasicBlock.
1473 template<typename InputIterator>
1474 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1475 InputIterator IdxEnd,
1476 const Twine &NameStr,
1477 Instruction *InsertBefore);
1478 template<typename InputIterator>
1479 inline ExtractValueInst(Value *Agg,
1480 InputIterator IdxBegin, InputIterator IdxEnd,
1481 const Twine &NameStr, BasicBlock *InsertAtEnd);
1483 // allocate space for exactly one operand
1484 void *operator new(size_t s) {
1485 return User::operator new(s, 1);
1488 virtual ExtractValueInst *clone_impl() const;
1491 template<typename InputIterator>
1492 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1493 InputIterator IdxEnd,
1494 const Twine &NameStr = "",
1495 Instruction *InsertBefore = 0) {
1497 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1499 template<typename InputIterator>
1500 static ExtractValueInst *Create(Value *Agg,
1501 InputIterator IdxBegin, InputIterator IdxEnd,
1502 const Twine &NameStr,
1503 BasicBlock *InsertAtEnd) {
1504 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1507 /// Constructors - These two creators are convenience methods because one
1508 /// index extractvalue instructions are much more common than those with
1510 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1511 const Twine &NameStr = "",
1512 Instruction *InsertBefore = 0) {
1513 unsigned Idxs[1] = { Idx };
1514 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1516 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1517 const Twine &NameStr,
1518 BasicBlock *InsertAtEnd) {
1519 unsigned Idxs[1] = { Idx };
1520 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1523 /// getIndexedType - Returns the type of the element that would be extracted
1524 /// with an extractvalue instruction with the specified parameters.
1526 /// Null is returned if the indices are invalid for the specified
1529 template<typename InputIterator>
1530 static const Type *getIndexedType(const Type *Ptr,
1531 InputIterator IdxBegin,
1532 InputIterator IdxEnd) {
1533 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1534 typename std::iterator_traits<InputIterator>::
1535 iterator_category());
1537 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1539 typedef const unsigned* idx_iterator;
1540 inline idx_iterator idx_begin() const { return Indices.begin(); }
1541 inline idx_iterator idx_end() const { return Indices.end(); }
1543 Value *getAggregateOperand() {
1544 return getOperand(0);
1546 const Value *getAggregateOperand() const {
1547 return getOperand(0);
1549 static unsigned getAggregateOperandIndex() {
1550 return 0U; // get index for modifying correct operand
1553 unsigned getNumIndices() const { // Note: always non-negative
1554 return (unsigned)Indices.size();
1557 bool hasIndices() const {
1561 // Methods for support type inquiry through isa, cast, and dyn_cast:
1562 static inline bool classof(const ExtractValueInst *) { return true; }
1563 static inline bool classof(const Instruction *I) {
1564 return I->getOpcode() == Instruction::ExtractValue;
1566 static inline bool classof(const Value *V) {
1567 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1571 template<typename InputIterator>
1572 ExtractValueInst::ExtractValueInst(Value *Agg,
1573 InputIterator IdxBegin,
1574 InputIterator IdxEnd,
1575 const Twine &NameStr,
1576 Instruction *InsertBefore)
1577 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1579 ExtractValue, Agg, InsertBefore) {
1580 init(IdxBegin, IdxEnd, NameStr,
1581 typename std::iterator_traits<InputIterator>::iterator_category());
1583 template<typename InputIterator>
1584 ExtractValueInst::ExtractValueInst(Value *Agg,
1585 InputIterator IdxBegin,
1586 InputIterator IdxEnd,
1587 const Twine &NameStr,
1588 BasicBlock *InsertAtEnd)
1589 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1591 ExtractValue, Agg, InsertAtEnd) {
1592 init(IdxBegin, IdxEnd, NameStr,
1593 typename std::iterator_traits<InputIterator>::iterator_category());
1597 //===----------------------------------------------------------------------===//
1598 // InsertValueInst Class
1599 //===----------------------------------------------------------------------===//
1601 /// InsertValueInst - This instruction inserts a struct field of array element
1602 /// value into an aggregate value.
1604 class InsertValueInst : public Instruction {
1605 SmallVector<unsigned, 4> Indices;
1607 void *operator new(size_t, unsigned); // Do not implement
1608 InsertValueInst(const InsertValueInst &IVI);
1609 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1610 const Twine &NameStr);
1611 void init(Value *Agg, Value *Val, unsigned Idx, const Twine &NameStr);
1613 template<typename InputIterator>
1614 void init(Value *Agg, Value *Val,
1615 InputIterator IdxBegin, InputIterator IdxEnd,
1616 const Twine &NameStr,
1617 // This argument ensures that we have an iterator we can
1618 // do arithmetic on in constant time
1619 std::random_access_iterator_tag) {
1620 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1622 // There's no fundamental reason why we require at least one index
1623 // (other than weirdness with &*IdxBegin being invalid; see
1624 // getelementptr's init routine for example). But there's no
1625 // present need to support it.
1626 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1628 // This requires that the iterator points to contiguous memory.
1629 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1630 // we have to build an array here
1633 /// Constructors - Create a insertvalue instruction with a base aggregate
1634 /// value, a value to insert, and a list of indices. The first ctor can
1635 /// optionally insert before an existing instruction, the second appends
1636 /// the new instruction to the specified BasicBlock.
1637 template<typename InputIterator>
1638 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1639 InputIterator IdxEnd,
1640 const Twine &NameStr,
1641 Instruction *InsertBefore);
1642 template<typename InputIterator>
1643 inline InsertValueInst(Value *Agg, Value *Val,
1644 InputIterator IdxBegin, InputIterator IdxEnd,
1645 const Twine &NameStr, BasicBlock *InsertAtEnd);
1647 /// Constructors - These two constructors are convenience methods because one
1648 /// and two index insertvalue instructions are so common.
1649 InsertValueInst(Value *Agg, Value *Val,
1650 unsigned Idx, const Twine &NameStr = "",
1651 Instruction *InsertBefore = 0);
1652 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1653 const Twine &NameStr, BasicBlock *InsertAtEnd);
1655 virtual InsertValueInst *clone_impl() const;
1657 // allocate space for exactly two operands
1658 void *operator new(size_t s) {
1659 return User::operator new(s, 2);
1662 template<typename InputIterator>
1663 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1664 InputIterator IdxEnd,
1665 const Twine &NameStr = "",
1666 Instruction *InsertBefore = 0) {
1667 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1668 NameStr, InsertBefore);
1670 template<typename InputIterator>
1671 static InsertValueInst *Create(Value *Agg, Value *Val,
1672 InputIterator IdxBegin, InputIterator IdxEnd,
1673 const Twine &NameStr,
1674 BasicBlock *InsertAtEnd) {
1675 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1676 NameStr, InsertAtEnd);
1679 /// Constructors - These two creators are convenience methods because one
1680 /// index insertvalue instructions are much more common than those with
1682 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1683 const Twine &NameStr = "",
1684 Instruction *InsertBefore = 0) {
1685 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1687 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1688 const Twine &NameStr,
1689 BasicBlock *InsertAtEnd) {
1690 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1693 /// Transparently provide more efficient getOperand methods.
1694 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1696 typedef const unsigned* idx_iterator;
1697 inline idx_iterator idx_begin() const { return Indices.begin(); }
1698 inline idx_iterator idx_end() const { return Indices.end(); }
1700 Value *getAggregateOperand() {
1701 return getOperand(0);
1703 const Value *getAggregateOperand() const {
1704 return getOperand(0);
1706 static unsigned getAggregateOperandIndex() {
1707 return 0U; // get index for modifying correct operand
1710 Value *getInsertedValueOperand() {
1711 return getOperand(1);
1713 const Value *getInsertedValueOperand() const {
1714 return getOperand(1);
1716 static unsigned getInsertedValueOperandIndex() {
1717 return 1U; // get index for modifying correct operand
1720 unsigned getNumIndices() const { // Note: always non-negative
1721 return (unsigned)Indices.size();
1724 bool hasIndices() const {
1728 // Methods for support type inquiry through isa, cast, and dyn_cast:
1729 static inline bool classof(const InsertValueInst *) { return true; }
1730 static inline bool classof(const Instruction *I) {
1731 return I->getOpcode() == Instruction::InsertValue;
1733 static inline bool classof(const Value *V) {
1734 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1739 struct OperandTraits<InsertValueInst> : public FixedNumOperandTraits<2> {
1742 template<typename InputIterator>
1743 InsertValueInst::InsertValueInst(Value *Agg,
1745 InputIterator IdxBegin,
1746 InputIterator IdxEnd,
1747 const Twine &NameStr,
1748 Instruction *InsertBefore)
1749 : Instruction(Agg->getType(), InsertValue,
1750 OperandTraits<InsertValueInst>::op_begin(this),
1752 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1753 typename std::iterator_traits<InputIterator>::iterator_category());
1755 template<typename InputIterator>
1756 InsertValueInst::InsertValueInst(Value *Agg,
1758 InputIterator IdxBegin,
1759 InputIterator IdxEnd,
1760 const Twine &NameStr,
1761 BasicBlock *InsertAtEnd)
1762 : Instruction(Agg->getType(), InsertValue,
1763 OperandTraits<InsertValueInst>::op_begin(this),
1765 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1766 typename std::iterator_traits<InputIterator>::iterator_category());
1769 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1771 //===----------------------------------------------------------------------===//
1773 //===----------------------------------------------------------------------===//
1775 // PHINode - The PHINode class is used to represent the magical mystical PHI
1776 // node, that can not exist in nature, but can be synthesized in a computer
1777 // scientist's overactive imagination.
1779 class PHINode : public Instruction {
1780 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1781 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1782 /// the number actually in use.
1783 unsigned ReservedSpace;
1784 PHINode(const PHINode &PN);
1785 // allocate space for exactly zero operands
1786 void *operator new(size_t s) {
1787 return User::operator new(s, 0);
1789 explicit PHINode(const Type *Ty, const Twine &NameStr = "",
1790 Instruction *InsertBefore = 0)
1791 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1796 PHINode(const Type *Ty, const Twine &NameStr, BasicBlock *InsertAtEnd)
1797 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1802 virtual PHINode *clone_impl() const;
1804 static PHINode *Create(const Type *Ty, const Twine &NameStr = "",
1805 Instruction *InsertBefore = 0) {
1806 return new PHINode(Ty, NameStr, InsertBefore);
1808 static PHINode *Create(const Type *Ty, const Twine &NameStr,
1809 BasicBlock *InsertAtEnd) {
1810 return new PHINode(Ty, NameStr, InsertAtEnd);
1814 /// reserveOperandSpace - This method can be used to avoid repeated
1815 /// reallocation of PHI operand lists by reserving space for the correct
1816 /// number of operands before adding them. Unlike normal vector reserves,
1817 /// this method can also be used to trim the operand space.
1818 void reserveOperandSpace(unsigned NumValues) {
1819 resizeOperands(NumValues*2);
1822 /// Provide fast operand accessors
1823 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1825 /// getNumIncomingValues - Return the number of incoming edges
1827 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1829 /// getIncomingValue - Return incoming value number x
1831 Value *getIncomingValue(unsigned i) const {
1832 assert(i*2 < getNumOperands() && "Invalid value number!");
1833 return getOperand(i*2);
1835 void setIncomingValue(unsigned i, Value *V) {
1836 assert(i*2 < getNumOperands() && "Invalid value number!");
1839 static unsigned getOperandNumForIncomingValue(unsigned i) {
1842 static unsigned getIncomingValueNumForOperand(unsigned i) {
1843 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1847 /// getIncomingBlock - Return incoming basic block number @p i.
1849 BasicBlock *getIncomingBlock(unsigned i) const {
1850 return cast<BasicBlock>(getOperand(i*2+1));
1853 /// getIncomingBlock - Return incoming basic block corresponding
1854 /// to an operand of the PHI.
1856 BasicBlock *getIncomingBlock(const Use &U) const {
1857 assert(this == U.getUser() && "Iterator doesn't point to PHI's Uses?");
1858 return cast<BasicBlock>((&U + 1)->get());
1861 /// getIncomingBlock - Return incoming basic block corresponding
1862 /// to value use iterator.
1864 template <typename U>
1865 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1866 return getIncomingBlock(I.getUse());
1870 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1871 setOperand(i*2+1, (Value*)BB);
1873 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1876 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1877 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1881 /// addIncoming - Add an incoming value to the end of the PHI list
1883 void addIncoming(Value *V, BasicBlock *BB) {
1884 assert(V && "PHI node got a null value!");
1885 assert(BB && "PHI node got a null basic block!");
1886 assert(getType() == V->getType() &&
1887 "All operands to PHI node must be the same type as the PHI node!");
1888 unsigned OpNo = NumOperands;
1889 if (OpNo+2 > ReservedSpace)
1890 resizeOperands(0); // Get more space!
1891 // Initialize some new operands.
1892 NumOperands = OpNo+2;
1893 OperandList[OpNo] = V;
1894 OperandList[OpNo+1] = (Value*)BB;
1897 /// removeIncomingValue - Remove an incoming value. This is useful if a
1898 /// predecessor basic block is deleted. The value removed is returned.
1900 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1901 /// is true), the PHI node is destroyed and any uses of it are replaced with
1902 /// dummy values. The only time there should be zero incoming values to a PHI
1903 /// node is when the block is dead, so this strategy is sound.
1905 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1907 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1908 int Idx = getBasicBlockIndex(BB);
1909 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1910 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1913 /// getBasicBlockIndex - Return the first index of the specified basic
1914 /// block in the value list for this PHI. Returns -1 if no instance.
1916 int getBasicBlockIndex(const BasicBlock *BB) const {
1917 Use *OL = OperandList;
1918 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1919 if (OL[i+1].get() == (const Value*)BB) return i/2;
1923 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1924 return getIncomingValue(getBasicBlockIndex(BB));
1927 /// hasConstantValue - If the specified PHI node always merges together the
1928 /// same value, return the value, otherwise return null.
1930 /// If the PHI has undef operands, but all the rest of the operands are
1931 /// some unique value, return that value if it can be proved that the
1932 /// value dominates the PHI. If DT is null, use a conservative check,
1933 /// otherwise use DT to test for dominance.
1935 Value *hasConstantValue(DominatorTree *DT = 0) const;
1937 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1938 static inline bool classof(const PHINode *) { return true; }
1939 static inline bool classof(const Instruction *I) {
1940 return I->getOpcode() == Instruction::PHI;
1942 static inline bool classof(const Value *V) {
1943 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1946 void resizeOperands(unsigned NumOperands);
1950 struct OperandTraits<PHINode> : public HungoffOperandTraits<2> {
1953 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1956 //===----------------------------------------------------------------------===//
1958 //===----------------------------------------------------------------------===//
1960 //===---------------------------------------------------------------------------
1961 /// ReturnInst - Return a value (possibly void), from a function. Execution
1962 /// does not continue in this function any longer.
1964 class ReturnInst : public TerminatorInst {
1965 ReturnInst(const ReturnInst &RI);
1968 // ReturnInst constructors:
1969 // ReturnInst() - 'ret void' instruction
1970 // ReturnInst( null) - 'ret void' instruction
1971 // ReturnInst(Value* X) - 'ret X' instruction
1972 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1973 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1974 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1975 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1977 // NOTE: If the Value* passed is of type void then the constructor behaves as
1978 // if it was passed NULL.
1979 explicit ReturnInst(LLVMContext &C, Value *retVal = 0,
1980 Instruction *InsertBefore = 0);
1981 ReturnInst(LLVMContext &C, Value *retVal, BasicBlock *InsertAtEnd);
1982 explicit ReturnInst(LLVMContext &C, BasicBlock *InsertAtEnd);
1984 virtual ReturnInst *clone_impl() const;
1986 static ReturnInst* Create(LLVMContext &C, Value *retVal = 0,
1987 Instruction *InsertBefore = 0) {
1988 return new(!!retVal) ReturnInst(C, retVal, InsertBefore);
1990 static ReturnInst* Create(LLVMContext &C, Value *retVal,
1991 BasicBlock *InsertAtEnd) {
1992 return new(!!retVal) ReturnInst(C, retVal, InsertAtEnd);
1994 static ReturnInst* Create(LLVMContext &C, BasicBlock *InsertAtEnd) {
1995 return new(0) ReturnInst(C, InsertAtEnd);
1997 virtual ~ReturnInst();
1999 /// Provide fast operand accessors
2000 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2002 /// Convenience accessor
2003 Value *getReturnValue(unsigned n = 0) const {
2004 return n < getNumOperands()
2009 unsigned getNumSuccessors() const { return 0; }
2011 // Methods for support type inquiry through isa, cast, and dyn_cast:
2012 static inline bool classof(const ReturnInst *) { return true; }
2013 static inline bool classof(const Instruction *I) {
2014 return (I->getOpcode() == Instruction::Ret);
2016 static inline bool classof(const Value *V) {
2017 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2020 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2021 virtual unsigned getNumSuccessorsV() const;
2022 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2026 struct OperandTraits<ReturnInst> : public VariadicOperandTraits<> {
2029 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2031 //===----------------------------------------------------------------------===//
2033 //===----------------------------------------------------------------------===//
2035 //===---------------------------------------------------------------------------
2036 /// BranchInst - Conditional or Unconditional Branch instruction.
2038 class BranchInst : public TerminatorInst {
2039 /// Ops list - Branches are strange. The operands are ordered:
2040 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2041 /// they don't have to check for cond/uncond branchness. These are mostly
2042 /// accessed relative from op_end().
2043 BranchInst(const BranchInst &BI);
2045 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2046 // BranchInst(BB *B) - 'br B'
2047 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2048 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2049 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2050 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2051 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2052 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2053 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2054 Instruction *InsertBefore = 0);
2055 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2056 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2057 BasicBlock *InsertAtEnd);
2059 virtual BranchInst *clone_impl() const;
2061 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2062 return new(1, true) BranchInst(IfTrue, InsertBefore);
2064 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2065 Value *Cond, Instruction *InsertBefore = 0) {
2066 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2068 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2069 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2071 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2072 Value *Cond, BasicBlock *InsertAtEnd) {
2073 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2078 /// Transparently provide more efficient getOperand methods.
2079 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2081 bool isUnconditional() const { return getNumOperands() == 1; }
2082 bool isConditional() const { return getNumOperands() == 3; }
2084 Value *getCondition() const {
2085 assert(isConditional() && "Cannot get condition of an uncond branch!");
2089 void setCondition(Value *V) {
2090 assert(isConditional() && "Cannot set condition of unconditional branch!");
2094 // setUnconditionalDest - Change the current branch to an unconditional branch
2095 // targeting the specified block.
2096 // FIXME: Eliminate this ugly method.
2097 void setUnconditionalDest(BasicBlock *Dest) {
2098 Op<-1>() = (Value*)Dest;
2099 if (isConditional()) { // Convert this to an uncond branch.
2103 OperandList = op_begin();
2107 unsigned getNumSuccessors() const { return 1+isConditional(); }
2109 BasicBlock *getSuccessor(unsigned i) const {
2110 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2111 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2114 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2115 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2116 *(&Op<-1>() - idx) = (Value*)NewSucc;
2119 // Methods for support type inquiry through isa, cast, and dyn_cast:
2120 static inline bool classof(const BranchInst *) { return true; }
2121 static inline bool classof(const Instruction *I) {
2122 return (I->getOpcode() == Instruction::Br);
2124 static inline bool classof(const Value *V) {
2125 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2128 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2129 virtual unsigned getNumSuccessorsV() const;
2130 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2134 struct OperandTraits<BranchInst> : public VariadicOperandTraits<1> {};
2136 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2138 //===----------------------------------------------------------------------===//
2140 //===----------------------------------------------------------------------===//
2142 //===---------------------------------------------------------------------------
2143 /// SwitchInst - Multiway switch
2145 class SwitchInst : public TerminatorInst {
2146 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2147 unsigned ReservedSpace;
2148 // Operand[0] = Value to switch on
2149 // Operand[1] = Default basic block destination
2150 // Operand[2n ] = Value to match
2151 // Operand[2n+1] = BasicBlock to go to on match
2152 SwitchInst(const SwitchInst &SI);
2153 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2154 void resizeOperands(unsigned No);
2155 // allocate space for exactly zero operands
2156 void *operator new(size_t s) {
2157 return User::operator new(s, 0);
2159 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2160 /// switch on and a default destination. The number of additional cases can
2161 /// be specified here to make memory allocation more efficient. This
2162 /// constructor can also autoinsert before another instruction.
2163 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2164 Instruction *InsertBefore);
2166 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2167 /// switch on and a default destination. The number of additional cases can
2168 /// be specified here to make memory allocation more efficient. This
2169 /// constructor also autoinserts at the end of the specified BasicBlock.
2170 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2171 BasicBlock *InsertAtEnd);
2173 virtual SwitchInst *clone_impl() const;
2175 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2176 unsigned NumCases, Instruction *InsertBefore = 0) {
2177 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2179 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2180 unsigned NumCases, BasicBlock *InsertAtEnd) {
2181 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2185 /// Provide fast operand accessors
2186 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2188 // Accessor Methods for Switch stmt
2189 Value *getCondition() const { return getOperand(0); }
2190 void setCondition(Value *V) { setOperand(0, V); }
2192 BasicBlock *getDefaultDest() const {
2193 return cast<BasicBlock>(getOperand(1));
2196 /// getNumCases - return the number of 'cases' in this switch instruction.
2197 /// Note that case #0 is always the default case.
2198 unsigned getNumCases() const {
2199 return getNumOperands()/2;
2202 /// getCaseValue - Return the specified case value. Note that case #0, the
2203 /// default destination, does not have a case value.
2204 ConstantInt *getCaseValue(unsigned i) {
2205 assert(i && i < getNumCases() && "Illegal case value to get!");
2206 return getSuccessorValue(i);
2209 /// getCaseValue - Return the specified case value. Note that case #0, the
2210 /// default destination, does not have a case value.
2211 const ConstantInt *getCaseValue(unsigned i) const {
2212 assert(i && i < getNumCases() && "Illegal case value to get!");
2213 return getSuccessorValue(i);
2216 /// findCaseValue - Search all of the case values for the specified constant.
2217 /// If it is explicitly handled, return the case number of it, otherwise
2218 /// return 0 to indicate that it is handled by the default handler.
2219 unsigned findCaseValue(const ConstantInt *C) const {
2220 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2221 if (getCaseValue(i) == C)
2226 /// findCaseDest - Finds the unique case value for a given successor. Returns
2227 /// null if the successor is not found, not unique, or is the default case.
2228 ConstantInt *findCaseDest(BasicBlock *BB) {
2229 if (BB == getDefaultDest()) return NULL;
2231 ConstantInt *CI = NULL;
2232 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2233 if (getSuccessor(i) == BB) {
2234 if (CI) return NULL; // Multiple cases lead to BB.
2235 else CI = getCaseValue(i);
2241 /// addCase - Add an entry to the switch instruction...
2243 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2245 /// removeCase - This method removes the specified successor from the switch
2246 /// instruction. Note that this cannot be used to remove the default
2247 /// destination (successor #0).
2249 void removeCase(unsigned idx);
2251 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2252 BasicBlock *getSuccessor(unsigned idx) const {
2253 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2254 return cast<BasicBlock>(getOperand(idx*2+1));
2256 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2257 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2258 setOperand(idx*2+1, (Value*)NewSucc);
2261 // getSuccessorValue - Return the value associated with the specified
2263 ConstantInt *getSuccessorValue(unsigned idx) const {
2264 assert(idx < getNumSuccessors() && "Successor # out of range!");
2265 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2268 // Methods for support type inquiry through isa, cast, and dyn_cast:
2269 static inline bool classof(const SwitchInst *) { return true; }
2270 static inline bool classof(const Instruction *I) {
2271 return I->getOpcode() == Instruction::Switch;
2273 static inline bool classof(const Value *V) {
2274 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2277 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2278 virtual unsigned getNumSuccessorsV() const;
2279 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2283 struct OperandTraits<SwitchInst> : public HungoffOperandTraits<2> {
2286 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2289 //===----------------------------------------------------------------------===//
2290 // IndirectBrInst Class
2291 //===----------------------------------------------------------------------===//
2293 //===---------------------------------------------------------------------------
2294 /// IndirectBrInst - Indirect Branch Instruction.
2296 class IndirectBrInst : public TerminatorInst {
2297 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2298 unsigned ReservedSpace;
2299 // Operand[0] = Value to switch on
2300 // Operand[1] = Default basic block destination
2301 // Operand[2n ] = Value to match
2302 // Operand[2n+1] = BasicBlock to go to on match
2303 IndirectBrInst(const IndirectBrInst &IBI);
2304 void init(Value *Address, unsigned NumDests);
2305 void resizeOperands(unsigned No);
2306 // allocate space for exactly zero operands
2307 void *operator new(size_t s) {
2308 return User::operator new(s, 0);
2310 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2311 /// Address to jump to. The number of expected destinations can be specified
2312 /// here to make memory allocation more efficient. This constructor can also
2313 /// autoinsert before another instruction.
2314 IndirectBrInst(Value *Address, unsigned NumDests, Instruction *InsertBefore);
2316 /// IndirectBrInst ctor - Create a new indirectbr instruction, specifying an
2317 /// Address to jump to. The number of expected destinations can be specified
2318 /// here to make memory allocation more efficient. This constructor also
2319 /// autoinserts at the end of the specified BasicBlock.
2320 IndirectBrInst(Value *Address, unsigned NumDests, BasicBlock *InsertAtEnd);
2322 virtual IndirectBrInst *clone_impl() const;
2324 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2325 Instruction *InsertBefore = 0) {
2326 return new IndirectBrInst(Address, NumDests, InsertBefore);
2328 static IndirectBrInst *Create(Value *Address, unsigned NumDests,
2329 BasicBlock *InsertAtEnd) {
2330 return new IndirectBrInst(Address, NumDests, InsertAtEnd);
2334 /// Provide fast operand accessors.
2335 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2337 // Accessor Methods for IndirectBrInst instruction.
2338 Value *getAddress() { return getOperand(0); }
2339 const Value *getAddress() const { return getOperand(0); }
2340 void setAddress(Value *V) { setOperand(0, V); }
2343 /// getNumDestinations - return the number of possible destinations in this
2344 /// indirectbr instruction.
2345 unsigned getNumDestinations() const { return getNumOperands()-1; }
2347 /// getDestination - Return the specified destination.
2348 BasicBlock *getDestination(unsigned i) { return getSuccessor(i); }
2349 const BasicBlock *getDestination(unsigned i) const { return getSuccessor(i); }
2351 /// addDestination - Add a destination.
2353 void addDestination(BasicBlock *Dest);
2355 /// removeDestination - This method removes the specified successor from the
2356 /// indirectbr instruction.
2357 void removeDestination(unsigned i);
2359 unsigned getNumSuccessors() const { return getNumOperands()-1; }
2360 BasicBlock *getSuccessor(unsigned i) const {
2361 return cast<BasicBlock>(getOperand(i+1));
2363 void setSuccessor(unsigned i, BasicBlock *NewSucc) {
2364 setOperand(i+1, (Value*)NewSucc);
2367 // Methods for support type inquiry through isa, cast, and dyn_cast:
2368 static inline bool classof(const IndirectBrInst *) { return true; }
2369 static inline bool classof(const Instruction *I) {
2370 return I->getOpcode() == Instruction::IndirectBr;
2372 static inline bool classof(const Value *V) {
2373 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2376 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2377 virtual unsigned getNumSuccessorsV() const;
2378 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2382 struct OperandTraits<IndirectBrInst> : public HungoffOperandTraits<1> {
2385 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(IndirectBrInst, Value)
2388 //===----------------------------------------------------------------------===//
2390 //===----------------------------------------------------------------------===//
2392 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2393 /// calling convention of the call.
2395 class InvokeInst : public TerminatorInst {
2396 AttrListPtr AttributeList;
2397 InvokeInst(const InvokeInst &BI);
2398 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2399 Value* const *Args, unsigned NumArgs);
2401 template<typename InputIterator>
2402 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2403 InputIterator ArgBegin, InputIterator ArgEnd,
2404 const Twine &NameStr,
2405 // This argument ensures that we have an iterator we can
2406 // do arithmetic on in constant time
2407 std::random_access_iterator_tag) {
2408 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2410 // This requires that the iterator points to contiguous memory.
2411 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2415 /// Construct an InvokeInst given a range of arguments.
2416 /// InputIterator must be a random-access iterator pointing to
2417 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2418 /// made for random-accessness but not for contiguous storage as
2419 /// that would incur runtime overhead.
2421 /// @brief Construct an InvokeInst from a range of arguments
2422 template<typename InputIterator>
2423 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2424 InputIterator ArgBegin, InputIterator ArgEnd,
2426 const Twine &NameStr, Instruction *InsertBefore);
2428 /// Construct an InvokeInst given a range of arguments.
2429 /// InputIterator must be a random-access iterator pointing to
2430 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2431 /// made for random-accessness but not for contiguous storage as
2432 /// that would incur runtime overhead.
2434 /// @brief Construct an InvokeInst from a range of arguments
2435 template<typename InputIterator>
2436 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2437 InputIterator ArgBegin, InputIterator ArgEnd,
2439 const Twine &NameStr, BasicBlock *InsertAtEnd);
2441 virtual InvokeInst *clone_impl() const;
2443 template<typename InputIterator>
2444 static InvokeInst *Create(Value *Func,
2445 BasicBlock *IfNormal, BasicBlock *IfException,
2446 InputIterator ArgBegin, InputIterator ArgEnd,
2447 const Twine &NameStr = "",
2448 Instruction *InsertBefore = 0) {
2449 unsigned Values(ArgEnd - ArgBegin + 3);
2450 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2451 Values, NameStr, InsertBefore);
2453 template<typename InputIterator>
2454 static InvokeInst *Create(Value *Func,
2455 BasicBlock *IfNormal, BasicBlock *IfException,
2456 InputIterator ArgBegin, InputIterator ArgEnd,
2457 const Twine &NameStr,
2458 BasicBlock *InsertAtEnd) {
2459 unsigned Values(ArgEnd - ArgBegin + 3);
2460 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2461 Values, NameStr, InsertAtEnd);
2464 /// Provide fast operand accessors
2465 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2467 unsigned getNumArgOperands() const { return getNumOperands() - 3; }
2468 Value *getArgOperand(unsigned i) const { return getOperand(i); }
2469 void setArgOperand(unsigned i, Value *v) { setOperand(i, v); }
2471 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2473 CallingConv::ID getCallingConv() const {
2474 return static_cast<CallingConv::ID>(getSubclassDataFromInstruction());
2476 void setCallingConv(CallingConv::ID CC) {
2477 setInstructionSubclassData(static_cast<unsigned>(CC));
2480 /// getAttributes - Return the parameter attributes for this invoke.
2482 const AttrListPtr &getAttributes() const { return AttributeList; }
2484 /// setAttributes - Set the parameter attributes for this invoke.
2486 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2488 /// addAttribute - adds the attribute to the list of attributes.
2489 void addAttribute(unsigned i, Attributes attr);
2491 /// removeAttribute - removes the attribute from the list of attributes.
2492 void removeAttribute(unsigned i, Attributes attr);
2494 /// @brief Determine whether the call or the callee has the given attribute.
2495 bool paramHasAttr(unsigned i, Attributes attr) const;
2497 /// @brief Extract the alignment for a call or parameter (0=unknown).
2498 unsigned getParamAlignment(unsigned i) const {
2499 return AttributeList.getParamAlignment(i);
2502 /// @brief Return true if the call should not be inlined.
2503 bool isNoInline() const { return paramHasAttr(~0, Attribute::NoInline); }
2504 void setIsNoInline(bool Value = true) {
2505 if (Value) addAttribute(~0, Attribute::NoInline);
2506 else removeAttribute(~0, Attribute::NoInline);
2509 /// @brief Determine if the call does not access memory.
2510 bool doesNotAccessMemory() const {
2511 return paramHasAttr(~0, Attribute::ReadNone);
2513 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2514 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2515 else removeAttribute(~0, Attribute::ReadNone);
2518 /// @brief Determine if the call does not access or only reads memory.
2519 bool onlyReadsMemory() const {
2520 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2522 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2523 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2524 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2527 /// @brief Determine if the call cannot return.
2528 bool doesNotReturn() const { return paramHasAttr(~0, Attribute::NoReturn); }
2529 void setDoesNotReturn(bool DoesNotReturn = true) {
2530 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2531 else removeAttribute(~0, Attribute::NoReturn);
2534 /// @brief Determine if the call cannot unwind.
2535 bool doesNotThrow() const { return paramHasAttr(~0, Attribute::NoUnwind); }
2536 void setDoesNotThrow(bool DoesNotThrow = true) {
2537 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2538 else removeAttribute(~0, Attribute::NoUnwind);
2541 /// @brief Determine if the call returns a structure through first
2542 /// pointer argument.
2543 bool hasStructRetAttr() const {
2544 // Be friendly and also check the callee.
2545 return paramHasAttr(1, Attribute::StructRet);
2548 /// @brief Determine if any call argument is an aggregate passed by value.
2549 bool hasByValArgument() const {
2550 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2553 /// getCalledFunction - Return the function called, or null if this is an
2554 /// indirect function invocation.
2556 Function *getCalledFunction() const {
2557 return dyn_cast<Function>(Op<-3>());
2560 /// getCalledValue - Get a pointer to the function that is invoked by this
2562 const Value *getCalledValue() const { return Op<-3>(); }
2563 Value *getCalledValue() { return Op<-3>(); }
2565 /// setCalledFunction - Set the function called.
2566 void setCalledFunction(Value* Fn) {
2570 // get*Dest - Return the destination basic blocks...
2571 BasicBlock *getNormalDest() const {
2572 return cast<BasicBlock>(Op<-2>());
2574 BasicBlock *getUnwindDest() const {
2575 return cast<BasicBlock>(Op<-1>());
2577 void setNormalDest(BasicBlock *B) {
2578 Op<-2>() = reinterpret_cast<Value*>(B);
2580 void setUnwindDest(BasicBlock *B) {
2581 Op<-1>() = reinterpret_cast<Value*>(B);
2584 BasicBlock *getSuccessor(unsigned i) const {
2585 assert(i < 2 && "Successor # out of range for invoke!");
2586 return i == 0 ? getNormalDest() : getUnwindDest();
2589 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2590 assert(idx < 2 && "Successor # out of range for invoke!");
2591 *(&Op<-2>() + idx) = reinterpret_cast<Value*>(NewSucc);
2594 unsigned getNumSuccessors() const { return 2; }
2596 // Methods for support type inquiry through isa, cast, and dyn_cast:
2597 static inline bool classof(const InvokeInst *) { return true; }
2598 static inline bool classof(const Instruction *I) {
2599 return (I->getOpcode() == Instruction::Invoke);
2601 static inline bool classof(const Value *V) {
2602 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2606 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2607 virtual unsigned getNumSuccessorsV() const;
2608 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2610 // Shadow Instruction::setInstructionSubclassData with a private forwarding
2611 // method so that subclasses cannot accidentally use it.
2612 void setInstructionSubclassData(unsigned short D) {
2613 Instruction::setInstructionSubclassData(D);
2618 struct OperandTraits<InvokeInst> : public VariadicOperandTraits<3> {
2621 template<typename InputIterator>
2622 InvokeInst::InvokeInst(Value *Func,
2623 BasicBlock *IfNormal, BasicBlock *IfException,
2624 InputIterator ArgBegin, InputIterator ArgEnd,
2626 const Twine &NameStr, Instruction *InsertBefore)
2627 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2628 ->getElementType())->getReturnType(),
2629 Instruction::Invoke,
2630 OperandTraits<InvokeInst>::op_end(this) - Values,
2631 Values, InsertBefore) {
2632 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2633 typename std::iterator_traits<InputIterator>::iterator_category());
2635 template<typename InputIterator>
2636 InvokeInst::InvokeInst(Value *Func,
2637 BasicBlock *IfNormal, BasicBlock *IfException,
2638 InputIterator ArgBegin, InputIterator ArgEnd,
2640 const Twine &NameStr, BasicBlock *InsertAtEnd)
2641 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2642 ->getElementType())->getReturnType(),
2643 Instruction::Invoke,
2644 OperandTraits<InvokeInst>::op_end(this) - Values,
2645 Values, InsertAtEnd) {
2646 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2647 typename std::iterator_traits<InputIterator>::iterator_category());
2650 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2652 //===----------------------------------------------------------------------===//
2654 //===----------------------------------------------------------------------===//
2656 //===---------------------------------------------------------------------------
2657 /// UnwindInst - Immediately exit the current function, unwinding the stack
2658 /// until an invoke instruction is found.
2660 class UnwindInst : public TerminatorInst {
2661 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2663 virtual UnwindInst *clone_impl() const;
2665 // allocate space for exactly zero operands
2666 void *operator new(size_t s) {
2667 return User::operator new(s, 0);
2669 explicit UnwindInst(LLVMContext &C, Instruction *InsertBefore = 0);
2670 explicit UnwindInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2672 unsigned getNumSuccessors() const { return 0; }
2674 // Methods for support type inquiry through isa, cast, and dyn_cast:
2675 static inline bool classof(const UnwindInst *) { return true; }
2676 static inline bool classof(const Instruction *I) {
2677 return I->getOpcode() == Instruction::Unwind;
2679 static inline bool classof(const Value *V) {
2680 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2683 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2684 virtual unsigned getNumSuccessorsV() const;
2685 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2688 //===----------------------------------------------------------------------===//
2689 // UnreachableInst Class
2690 //===----------------------------------------------------------------------===//
2692 //===---------------------------------------------------------------------------
2693 /// UnreachableInst - This function has undefined behavior. In particular, the
2694 /// presence of this instruction indicates some higher level knowledge that the
2695 /// end of the block cannot be reached.
2697 class UnreachableInst : public TerminatorInst {
2698 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2700 virtual UnreachableInst *clone_impl() const;
2703 // allocate space for exactly zero operands
2704 void *operator new(size_t s) {
2705 return User::operator new(s, 0);
2707 explicit UnreachableInst(LLVMContext &C, Instruction *InsertBefore = 0);
2708 explicit UnreachableInst(LLVMContext &C, BasicBlock *InsertAtEnd);
2710 unsigned getNumSuccessors() const { return 0; }
2712 // Methods for support type inquiry through isa, cast, and dyn_cast:
2713 static inline bool classof(const UnreachableInst *) { return true; }
2714 static inline bool classof(const Instruction *I) {
2715 return I->getOpcode() == Instruction::Unreachable;
2717 static inline bool classof(const Value *V) {
2718 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2721 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2722 virtual unsigned getNumSuccessorsV() const;
2723 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2726 //===----------------------------------------------------------------------===//
2728 //===----------------------------------------------------------------------===//
2730 /// @brief This class represents a truncation of integer types.
2731 class TruncInst : public CastInst {
2733 /// @brief Clone an identical TruncInst
2734 virtual TruncInst *clone_impl() const;
2737 /// @brief Constructor with insert-before-instruction semantics
2739 Value *S, ///< The value to be truncated
2740 const Type *Ty, ///< The (smaller) type to truncate to
2741 const Twine &NameStr = "", ///< A name for the new instruction
2742 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2745 /// @brief Constructor with insert-at-end-of-block semantics
2747 Value *S, ///< The value to be truncated
2748 const Type *Ty, ///< The (smaller) type to truncate to
2749 const Twine &NameStr, ///< A name for the new instruction
2750 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2753 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2754 static inline bool classof(const TruncInst *) { return true; }
2755 static inline bool classof(const Instruction *I) {
2756 return I->getOpcode() == Trunc;
2758 static inline bool classof(const Value *V) {
2759 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2763 //===----------------------------------------------------------------------===//
2765 //===----------------------------------------------------------------------===//
2767 /// @brief This class represents zero extension of integer types.
2768 class ZExtInst : public CastInst {
2770 /// @brief Clone an identical ZExtInst
2771 virtual ZExtInst *clone_impl() const;
2774 /// @brief Constructor with insert-before-instruction semantics
2776 Value *S, ///< The value to be zero extended
2777 const Type *Ty, ///< The type to zero extend to
2778 const Twine &NameStr = "", ///< A name for the new instruction
2779 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2782 /// @brief Constructor with insert-at-end semantics.
2784 Value *S, ///< The value to be zero extended
2785 const Type *Ty, ///< The type to zero extend to
2786 const Twine &NameStr, ///< A name for the new instruction
2787 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2790 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2791 static inline bool classof(const ZExtInst *) { return true; }
2792 static inline bool classof(const Instruction *I) {
2793 return I->getOpcode() == ZExt;
2795 static inline bool classof(const Value *V) {
2796 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2800 //===----------------------------------------------------------------------===//
2802 //===----------------------------------------------------------------------===//
2804 /// @brief This class represents a sign extension of integer types.
2805 class SExtInst : public CastInst {
2807 /// @brief Clone an identical SExtInst
2808 virtual SExtInst *clone_impl() const;
2811 /// @brief Constructor with insert-before-instruction semantics
2813 Value *S, ///< The value to be sign extended
2814 const Type *Ty, ///< The type to sign extend to
2815 const Twine &NameStr = "", ///< A name for the new instruction
2816 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2819 /// @brief Constructor with insert-at-end-of-block semantics
2821 Value *S, ///< The value to be sign extended
2822 const Type *Ty, ///< The type to sign extend to
2823 const Twine &NameStr, ///< A name for the new instruction
2824 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2827 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2828 static inline bool classof(const SExtInst *) { return true; }
2829 static inline bool classof(const Instruction *I) {
2830 return I->getOpcode() == SExt;
2832 static inline bool classof(const Value *V) {
2833 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2837 //===----------------------------------------------------------------------===//
2838 // FPTruncInst Class
2839 //===----------------------------------------------------------------------===//
2841 /// @brief This class represents a truncation of floating point types.
2842 class FPTruncInst : public CastInst {
2844 /// @brief Clone an identical FPTruncInst
2845 virtual FPTruncInst *clone_impl() const;
2848 /// @brief Constructor with insert-before-instruction semantics
2850 Value *S, ///< The value to be truncated
2851 const Type *Ty, ///< The type to truncate to
2852 const Twine &NameStr = "", ///< A name for the new instruction
2853 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2856 /// @brief Constructor with insert-before-instruction semantics
2858 Value *S, ///< The value to be truncated
2859 const Type *Ty, ///< The type to truncate to
2860 const Twine &NameStr, ///< A name for the new instruction
2861 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2864 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2865 static inline bool classof(const FPTruncInst *) { return true; }
2866 static inline bool classof(const Instruction *I) {
2867 return I->getOpcode() == FPTrunc;
2869 static inline bool classof(const Value *V) {
2870 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2874 //===----------------------------------------------------------------------===//
2876 //===----------------------------------------------------------------------===//
2878 /// @brief This class represents an extension of floating point types.
2879 class FPExtInst : public CastInst {
2881 /// @brief Clone an identical FPExtInst
2882 virtual FPExtInst *clone_impl() const;
2885 /// @brief Constructor with insert-before-instruction semantics
2887 Value *S, ///< The value to be extended
2888 const Type *Ty, ///< The type to extend to
2889 const Twine &NameStr = "", ///< A name for the new instruction
2890 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2893 /// @brief Constructor with insert-at-end-of-block semantics
2895 Value *S, ///< The value to be extended
2896 const Type *Ty, ///< The type to extend to
2897 const Twine &NameStr, ///< A name for the new instruction
2898 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2901 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2902 static inline bool classof(const FPExtInst *) { return true; }
2903 static inline bool classof(const Instruction *I) {
2904 return I->getOpcode() == FPExt;
2906 static inline bool classof(const Value *V) {
2907 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2911 //===----------------------------------------------------------------------===//
2913 //===----------------------------------------------------------------------===//
2915 /// @brief This class represents a cast unsigned integer to floating point.
2916 class UIToFPInst : public CastInst {
2918 /// @brief Clone an identical UIToFPInst
2919 virtual UIToFPInst *clone_impl() const;
2922 /// @brief Constructor with insert-before-instruction semantics
2924 Value *S, ///< The value to be converted
2925 const Type *Ty, ///< The type to convert to
2926 const Twine &NameStr = "", ///< A name for the new instruction
2927 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2930 /// @brief Constructor with insert-at-end-of-block semantics
2932 Value *S, ///< The value to be converted
2933 const Type *Ty, ///< The type to convert to
2934 const Twine &NameStr, ///< A name for the new instruction
2935 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2938 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2939 static inline bool classof(const UIToFPInst *) { return true; }
2940 static inline bool classof(const Instruction *I) {
2941 return I->getOpcode() == UIToFP;
2943 static inline bool classof(const Value *V) {
2944 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2948 //===----------------------------------------------------------------------===//
2950 //===----------------------------------------------------------------------===//
2952 /// @brief This class represents a cast from signed integer to floating point.
2953 class SIToFPInst : public CastInst {
2955 /// @brief Clone an identical SIToFPInst
2956 virtual SIToFPInst *clone_impl() const;
2959 /// @brief Constructor with insert-before-instruction semantics
2961 Value *S, ///< The value to be converted
2962 const Type *Ty, ///< The type to convert to
2963 const Twine &NameStr = "", ///< A name for the new instruction
2964 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2967 /// @brief Constructor with insert-at-end-of-block semantics
2969 Value *S, ///< The value to be converted
2970 const Type *Ty, ///< The type to convert to
2971 const Twine &NameStr, ///< A name for the new instruction
2972 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2975 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2976 static inline bool classof(const SIToFPInst *) { return true; }
2977 static inline bool classof(const Instruction *I) {
2978 return I->getOpcode() == SIToFP;
2980 static inline bool classof(const Value *V) {
2981 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2985 //===----------------------------------------------------------------------===//
2987 //===----------------------------------------------------------------------===//
2989 /// @brief This class represents a cast from floating point to unsigned integer
2990 class FPToUIInst : public CastInst {
2992 /// @brief Clone an identical FPToUIInst
2993 virtual FPToUIInst *clone_impl() const;
2996 /// @brief Constructor with insert-before-instruction semantics
2998 Value *S, ///< The value to be converted
2999 const Type *Ty, ///< The type to convert to
3000 const Twine &NameStr = "", ///< A name for the new instruction
3001 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3004 /// @brief Constructor with insert-at-end-of-block semantics
3006 Value *S, ///< The value to be converted
3007 const Type *Ty, ///< The type to convert to
3008 const Twine &NameStr, ///< A name for the new instruction
3009 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
3012 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3013 static inline bool classof(const FPToUIInst *) { return true; }
3014 static inline bool classof(const Instruction *I) {
3015 return I->getOpcode() == FPToUI;
3017 static inline bool classof(const Value *V) {
3018 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3022 //===----------------------------------------------------------------------===//
3024 //===----------------------------------------------------------------------===//
3026 /// @brief This class represents a cast from floating point to signed integer.
3027 class FPToSIInst : public CastInst {
3029 /// @brief Clone an identical FPToSIInst
3030 virtual FPToSIInst *clone_impl() const;
3033 /// @brief Constructor with insert-before-instruction semantics
3035 Value *S, ///< The value to be converted
3036 const Type *Ty, ///< The type to convert to
3037 const Twine &NameStr = "", ///< A name for the new instruction
3038 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3041 /// @brief Constructor with insert-at-end-of-block semantics
3043 Value *S, ///< The value to be converted
3044 const Type *Ty, ///< The type to convert to
3045 const Twine &NameStr, ///< A name for the new instruction
3046 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3049 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
3050 static inline bool classof(const FPToSIInst *) { return true; }
3051 static inline bool classof(const Instruction *I) {
3052 return I->getOpcode() == FPToSI;
3054 static inline bool classof(const Value *V) {
3055 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3059 //===----------------------------------------------------------------------===//
3060 // IntToPtrInst Class
3061 //===----------------------------------------------------------------------===//
3063 /// @brief This class represents a cast from an integer to a pointer.
3064 class IntToPtrInst : public CastInst {
3066 /// @brief Constructor with insert-before-instruction semantics
3068 Value *S, ///< The value to be converted
3069 const Type *Ty, ///< The type to convert to
3070 const Twine &NameStr = "", ///< A name for the new instruction
3071 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3074 /// @brief Constructor with insert-at-end-of-block semantics
3076 Value *S, ///< The value to be converted
3077 const Type *Ty, ///< The type to convert to
3078 const Twine &NameStr, ///< A name for the new instruction
3079 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3082 /// @brief Clone an identical IntToPtrInst
3083 virtual IntToPtrInst *clone_impl() const;
3085 // Methods for support type inquiry through isa, cast, and dyn_cast:
3086 static inline bool classof(const IntToPtrInst *) { return true; }
3087 static inline bool classof(const Instruction *I) {
3088 return I->getOpcode() == IntToPtr;
3090 static inline bool classof(const Value *V) {
3091 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3095 //===----------------------------------------------------------------------===//
3096 // PtrToIntInst Class
3097 //===----------------------------------------------------------------------===//
3099 /// @brief This class represents a cast from a pointer to an integer
3100 class PtrToIntInst : public CastInst {
3102 /// @brief Clone an identical PtrToIntInst
3103 virtual PtrToIntInst *clone_impl() const;
3106 /// @brief Constructor with insert-before-instruction semantics
3108 Value *S, ///< The value to be converted
3109 const Type *Ty, ///< The type to convert to
3110 const Twine &NameStr = "", ///< A name for the new instruction
3111 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3114 /// @brief Constructor with insert-at-end-of-block semantics
3116 Value *S, ///< The value to be converted
3117 const Type *Ty, ///< The type to convert to
3118 const Twine &NameStr, ///< A name for the new instruction
3119 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3122 // Methods for support type inquiry through isa, cast, and dyn_cast:
3123 static inline bool classof(const PtrToIntInst *) { return true; }
3124 static inline bool classof(const Instruction *I) {
3125 return I->getOpcode() == PtrToInt;
3127 static inline bool classof(const Value *V) {
3128 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3132 //===----------------------------------------------------------------------===//
3133 // BitCastInst Class
3134 //===----------------------------------------------------------------------===//
3136 /// @brief This class represents a no-op cast from one type to another.
3137 class BitCastInst : public CastInst {
3139 /// @brief Clone an identical BitCastInst
3140 virtual BitCastInst *clone_impl() const;
3143 /// @brief Constructor with insert-before-instruction semantics
3145 Value *S, ///< The value to be casted
3146 const Type *Ty, ///< The type to casted to
3147 const Twine &NameStr = "", ///< A name for the new instruction
3148 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3151 /// @brief Constructor with insert-at-end-of-block semantics
3153 Value *S, ///< The value to be casted
3154 const Type *Ty, ///< The type to casted to
3155 const Twine &NameStr, ///< A name for the new instruction
3156 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3159 // Methods for support type inquiry through isa, cast, and dyn_cast:
3160 static inline bool classof(const BitCastInst *) { return true; }
3161 static inline bool classof(const Instruction *I) {
3162 return I->getOpcode() == BitCast;
3164 static inline bool classof(const Value *V) {
3165 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3169 } // End llvm namespace