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/BasicBlock.h"
23 #include "llvm/LLVMContext.h"
24 #include "llvm/ADT/SmallVector.h"
34 //===----------------------------------------------------------------------===//
35 // AllocationInst Class
36 //===----------------------------------------------------------------------===//
38 /// AllocationInst - This class is the common base class of MallocInst and
41 class AllocationInst : public UnaryInstruction {
45 AllocationInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
46 unsigned iTy, unsigned Align, const std::string &Name = "",
47 Instruction *InsertBefore = 0);
48 AllocationInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
49 unsigned iTy, unsigned Align, const std::string &Name,
50 BasicBlock *InsertAtEnd);
52 // Out of line virtual method, so the vtable, etc. has a home.
53 virtual ~AllocationInst();
55 /// isArrayAllocation - Return true if there is an allocation size parameter
56 /// to the allocation instruction that is not 1.
58 bool isArrayAllocation() const;
60 /// getArraySize - Get the number of elements allocated. For a simple
61 /// allocation of a single element, this will return a constant 1 value.
63 const Value *getArraySize() const { return getOperand(0); }
64 Value *getArraySize() { return getOperand(0); }
66 /// getType - Overload to return most specific pointer type
68 const PointerType *getType() const {
69 return reinterpret_cast<const PointerType*>(Instruction::getType());
72 /// getAllocatedType - Return the type that is being allocated by the
75 const Type *getAllocatedType() const;
77 /// getAlignment - Return the alignment of the memory that is being allocated
78 /// by the instruction.
80 unsigned getAlignment() const { return (1u << SubclassData) >> 1; }
81 void setAlignment(unsigned Align);
83 virtual Instruction *clone(LLVMContext &Context) const = 0;
85 // Methods for support type inquiry through isa, cast, and dyn_cast:
86 static inline bool classof(const AllocationInst *) { return true; }
87 static inline bool classof(const Instruction *I) {
88 return I->getOpcode() == Instruction::Alloca ||
89 I->getOpcode() == Instruction::Malloc;
91 static inline bool classof(const Value *V) {
92 return isa<Instruction>(V) && classof(cast<Instruction>(V));
97 //===----------------------------------------------------------------------===//
99 //===----------------------------------------------------------------------===//
101 /// MallocInst - an instruction to allocated memory on the heap
103 class MallocInst : public AllocationInst {
104 MallocInst(const MallocInst &MI);
106 explicit MallocInst(LLVMContext &Context,
107 const Type *Ty, Value *ArraySize = 0,
108 const std::string &NameStr = "",
109 Instruction *InsertBefore = 0)
110 : AllocationInst(Context, Ty, ArraySize, Malloc,
111 0, NameStr, InsertBefore) {}
112 MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
113 const std::string &NameStr, BasicBlock *InsertAtEnd)
114 : AllocationInst(Context, Ty, ArraySize, Malloc, 0, NameStr, InsertAtEnd) {}
116 MallocInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
117 Instruction *InsertBefore = 0)
118 : AllocationInst(Context, Ty, 0, Malloc, 0, NameStr, InsertBefore) {}
119 MallocInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
120 BasicBlock *InsertAtEnd)
121 : AllocationInst(Context, Ty, 0, Malloc, 0, NameStr, InsertAtEnd) {}
123 MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
124 unsigned Align, const std::string &NameStr,
125 BasicBlock *InsertAtEnd)
126 : AllocationInst(Context, Ty, ArraySize, Malloc,
127 Align, NameStr, InsertAtEnd) {}
128 MallocInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
129 unsigned Align, const std::string &NameStr = "",
130 Instruction *InsertBefore = 0)
131 : AllocationInst(Context, Ty, ArraySize,
132 Malloc, Align, NameStr, InsertBefore) {}
134 virtual MallocInst *clone(LLVMContext &Context) const;
136 // Methods for support type inquiry through isa, cast, and dyn_cast:
137 static inline bool classof(const MallocInst *) { return true; }
138 static inline bool classof(const Instruction *I) {
139 return (I->getOpcode() == Instruction::Malloc);
141 static inline bool classof(const Value *V) {
142 return isa<Instruction>(V) && classof(cast<Instruction>(V));
147 //===----------------------------------------------------------------------===//
149 //===----------------------------------------------------------------------===//
151 /// AllocaInst - an instruction to allocate memory on the stack
153 class AllocaInst : public AllocationInst {
154 AllocaInst(const AllocaInst &);
156 explicit AllocaInst(LLVMContext &Context, const Type *Ty,
157 Value *ArraySize = 0,
158 const std::string &NameStr = "",
159 Instruction *InsertBefore = 0)
160 : AllocationInst(Context, Ty, ArraySize, Alloca,
161 0, NameStr, InsertBefore) {}
162 AllocaInst(LLVMContext &Context, const Type *Ty,
163 Value *ArraySize, const std::string &NameStr,
164 BasicBlock *InsertAtEnd)
165 : AllocationInst(Context, Ty, ArraySize, Alloca, 0, NameStr, InsertAtEnd) {}
167 AllocaInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
168 Instruction *InsertBefore = 0)
169 : AllocationInst(Context, Ty, 0, Alloca, 0, NameStr, InsertBefore) {}
170 AllocaInst(LLVMContext &Context, const Type *Ty, const std::string &NameStr,
171 BasicBlock *InsertAtEnd)
172 : AllocationInst(Context, Ty, 0, Alloca, 0, NameStr, InsertAtEnd) {}
174 AllocaInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
175 unsigned Align, const std::string &NameStr = "",
176 Instruction *InsertBefore = 0)
177 : AllocationInst(Context, Ty, ArraySize, Alloca,
178 Align, NameStr, InsertBefore) {}
179 AllocaInst(LLVMContext &Context, const Type *Ty, Value *ArraySize,
180 unsigned Align, const std::string &NameStr,
181 BasicBlock *InsertAtEnd)
182 : AllocationInst(Context, Ty, ArraySize, Alloca,
183 Align, NameStr, InsertAtEnd) {}
185 virtual AllocaInst *clone(LLVMContext &Context) const;
187 /// isStaticAlloca - Return true if this alloca is in the entry block of the
188 /// function and is a constant size. If so, the code generator will fold it
189 /// into the prolog/epilog code, so it is basically free.
190 bool isStaticAlloca() const;
192 // Methods for support type inquiry through isa, cast, and dyn_cast:
193 static inline bool classof(const AllocaInst *) { return true; }
194 static inline bool classof(const Instruction *I) {
195 return (I->getOpcode() == Instruction::Alloca);
197 static inline bool classof(const Value *V) {
198 return isa<Instruction>(V) && classof(cast<Instruction>(V));
203 //===----------------------------------------------------------------------===//
205 //===----------------------------------------------------------------------===//
207 /// FreeInst - an instruction to deallocate memory
209 class FreeInst : public UnaryInstruction {
212 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
213 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
215 virtual FreeInst *clone(LLVMContext &Context) const;
217 // Accessor methods for consistency with other memory operations
218 Value *getPointerOperand() { return getOperand(0); }
219 const Value *getPointerOperand() const { return getOperand(0); }
221 // Methods for support type inquiry through isa, cast, and dyn_cast:
222 static inline bool classof(const FreeInst *) { return true; }
223 static inline bool classof(const Instruction *I) {
224 return (I->getOpcode() == Instruction::Free);
226 static inline bool classof(const Value *V) {
227 return isa<Instruction>(V) && classof(cast<Instruction>(V));
232 //===----------------------------------------------------------------------===//
234 //===----------------------------------------------------------------------===//
236 /// LoadInst - an instruction for reading from memory. This uses the
237 /// SubclassData field in Value to store whether or not the load is volatile.
239 class LoadInst : public UnaryInstruction {
241 LoadInst(const LoadInst &LI)
242 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
243 setVolatile(LI.isVolatile());
244 setAlignment(LI.getAlignment());
252 LoadInst(Value *Ptr, const std::string &NameStr, Instruction *InsertBefore);
253 LoadInst(Value *Ptr, const std::string &NameStr, BasicBlock *InsertAtEnd);
254 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile = false,
255 Instruction *InsertBefore = 0);
256 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
257 unsigned Align, Instruction *InsertBefore = 0);
258 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
259 BasicBlock *InsertAtEnd);
260 LoadInst(Value *Ptr, const std::string &NameStr, bool isVolatile,
261 unsigned Align, BasicBlock *InsertAtEnd);
263 LoadInst(Value *Ptr, const char *NameStr, Instruction *InsertBefore);
264 LoadInst(Value *Ptr, const char *NameStr, BasicBlock *InsertAtEnd);
265 explicit LoadInst(Value *Ptr, const char *NameStr = 0,
266 bool isVolatile = false, Instruction *InsertBefore = 0);
267 LoadInst(Value *Ptr, const char *NameStr, bool isVolatile,
268 BasicBlock *InsertAtEnd);
270 /// isVolatile - Return true if this is a load from a volatile memory
273 bool isVolatile() const { return SubclassData & 1; }
275 /// setVolatile - Specify whether this is a volatile load or not.
277 void setVolatile(bool V) {
278 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
281 virtual LoadInst *clone(LLVMContext &Context) const;
283 /// getAlignment - Return the alignment of the access that is being performed
285 unsigned getAlignment() const {
286 return (1 << (SubclassData>>1)) >> 1;
289 void setAlignment(unsigned Align);
291 Value *getPointerOperand() { return getOperand(0); }
292 const Value *getPointerOperand() const { return getOperand(0); }
293 static unsigned getPointerOperandIndex() { return 0U; }
295 // Methods for support type inquiry through isa, cast, and dyn_cast:
296 static inline bool classof(const LoadInst *) { return true; }
297 static inline bool classof(const Instruction *I) {
298 return I->getOpcode() == Instruction::Load;
300 static inline bool classof(const Value *V) {
301 return isa<Instruction>(V) && classof(cast<Instruction>(V));
306 //===----------------------------------------------------------------------===//
308 //===----------------------------------------------------------------------===//
310 /// StoreInst - an instruction for storing to memory
312 class StoreInst : public Instruction {
313 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
315 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store,
317 Op<0>() = SI.Op<0>();
318 Op<1>() = SI.Op<1>();
319 setVolatile(SI.isVolatile());
320 setAlignment(SI.getAlignment());
328 // allocate space for exactly two operands
329 void *operator new(size_t s) {
330 return User::operator new(s, 2);
332 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
333 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
334 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
335 Instruction *InsertBefore = 0);
336 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
337 unsigned Align, Instruction *InsertBefore = 0);
338 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
339 StoreInst(Value *Val, Value *Ptr, bool isVolatile,
340 unsigned Align, BasicBlock *InsertAtEnd);
343 /// isVolatile - Return true if this is a load from a volatile memory
346 bool isVolatile() const { return SubclassData & 1; }
348 /// setVolatile - Specify whether this is a volatile load or not.
350 void setVolatile(bool V) {
351 SubclassData = (SubclassData & ~1) | (V ? 1 : 0);
354 /// Transparently provide more efficient getOperand methods.
355 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
357 /// getAlignment - Return the alignment of the access that is being performed
359 unsigned getAlignment() const {
360 return (1 << (SubclassData>>1)) >> 1;
363 void setAlignment(unsigned Align);
365 virtual StoreInst *clone(LLVMContext &Context) const;
367 Value *getPointerOperand() { return getOperand(1); }
368 const Value *getPointerOperand() const { return getOperand(1); }
369 static unsigned getPointerOperandIndex() { return 1U; }
371 // Methods for support type inquiry through isa, cast, and dyn_cast:
372 static inline bool classof(const StoreInst *) { return true; }
373 static inline bool classof(const Instruction *I) {
374 return I->getOpcode() == Instruction::Store;
376 static inline bool classof(const Value *V) {
377 return isa<Instruction>(V) && classof(cast<Instruction>(V));
382 struct OperandTraits<StoreInst> : FixedNumOperandTraits<2> {
385 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(StoreInst, Value)
387 //===----------------------------------------------------------------------===//
388 // GetElementPtrInst Class
389 //===----------------------------------------------------------------------===//
391 // checkType - Simple wrapper function to give a better assertion failure
392 // message on bad indexes for a gep instruction.
394 static inline const Type *checkType(const Type *Ty) {
395 assert(Ty && "Invalid GetElementPtrInst indices for type!");
399 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
400 /// access elements of arrays and structs
402 class GetElementPtrInst : public Instruction {
403 GetElementPtrInst(const GetElementPtrInst &GEPI);
404 void init(Value *Ptr, Value* const *Idx, unsigned NumIdx,
405 const std::string &NameStr);
406 void init(Value *Ptr, Value *Idx, const std::string &NameStr);
408 template<typename InputIterator>
409 void init(Value *Ptr, InputIterator IdxBegin, InputIterator IdxEnd,
410 const std::string &NameStr,
411 // This argument ensures that we have an iterator we can
412 // do arithmetic on in constant time
413 std::random_access_iterator_tag) {
414 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
417 // This requires that the iterator points to contiguous memory.
418 init(Ptr, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
419 // we have to build an array here
422 init(Ptr, 0, NumIdx, NameStr);
426 /// getIndexedType - Returns the type of the element that would be loaded with
427 /// a load instruction with the specified parameters.
429 /// Null is returned if the indices are invalid for the specified
432 template<typename InputIterator>
433 static const Type *getIndexedType(const Type *Ptr,
434 InputIterator IdxBegin,
435 InputIterator IdxEnd,
436 // This argument ensures that we
437 // have an iterator we can do
438 // arithmetic on in constant time
439 std::random_access_iterator_tag) {
440 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
443 // This requires that the iterator points to contiguous memory.
444 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
446 return getIndexedType(Ptr, (Value *const*)0, NumIdx);
449 /// Constructors - Create a getelementptr instruction with a base pointer an
450 /// list of indices. The first ctor can optionally insert before an existing
451 /// instruction, the second appends the new instruction to the specified
453 template<typename InputIterator>
454 inline GetElementPtrInst(Value *Ptr, InputIterator IdxBegin,
455 InputIterator IdxEnd,
457 const std::string &NameStr,
458 Instruction *InsertBefore);
459 template<typename InputIterator>
460 inline GetElementPtrInst(Value *Ptr,
461 InputIterator IdxBegin, InputIterator IdxEnd,
463 const std::string &NameStr, BasicBlock *InsertAtEnd);
465 /// Constructors - These two constructors are convenience methods because one
466 /// and two index getelementptr instructions are so common.
467 GetElementPtrInst(Value *Ptr, Value *Idx, const std::string &NameStr = "",
468 Instruction *InsertBefore = 0);
469 GetElementPtrInst(Value *Ptr, Value *Idx,
470 const std::string &NameStr, BasicBlock *InsertAtEnd);
472 template<typename InputIterator>
473 static GetElementPtrInst *Create(Value *Ptr, InputIterator IdxBegin,
474 InputIterator IdxEnd,
475 const std::string &NameStr = "",
476 Instruction *InsertBefore = 0) {
477 typename std::iterator_traits<InputIterator>::difference_type Values =
478 1 + std::distance(IdxBegin, IdxEnd);
480 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertBefore);
482 template<typename InputIterator>
483 static GetElementPtrInst *Create(Value *Ptr,
484 InputIterator IdxBegin, InputIterator IdxEnd,
485 const std::string &NameStr,
486 BasicBlock *InsertAtEnd) {
487 typename std::iterator_traits<InputIterator>::difference_type Values =
488 1 + std::distance(IdxBegin, IdxEnd);
490 GetElementPtrInst(Ptr, IdxBegin, IdxEnd, Values, NameStr, InsertAtEnd);
493 /// Constructors - These two creators are convenience methods because one
494 /// index getelementptr instructions are so common.
495 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
496 const std::string &NameStr = "",
497 Instruction *InsertBefore = 0) {
498 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertBefore);
500 static GetElementPtrInst *Create(Value *Ptr, Value *Idx,
501 const std::string &NameStr,
502 BasicBlock *InsertAtEnd) {
503 return new(2) GetElementPtrInst(Ptr, Idx, NameStr, InsertAtEnd);
506 virtual GetElementPtrInst *clone(LLVMContext &Context) const;
508 /// Transparently provide more efficient getOperand methods.
509 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
511 // getType - Overload to return most specific pointer type...
512 const PointerType *getType() const {
513 return reinterpret_cast<const PointerType*>(Instruction::getType());
516 /// getIndexedType - Returns the type of the element that would be loaded with
517 /// a load instruction with the specified parameters.
519 /// Null is returned if the indices are invalid for the specified
522 template<typename InputIterator>
523 static const Type *getIndexedType(const Type *Ptr,
524 InputIterator IdxBegin,
525 InputIterator IdxEnd) {
526 return getIndexedType(Ptr, IdxBegin, IdxEnd,
527 typename std::iterator_traits<InputIterator>::
528 iterator_category());
531 static const Type *getIndexedType(const Type *Ptr,
532 Value* const *Idx, unsigned NumIdx);
534 static const Type *getIndexedType(const Type *Ptr,
535 uint64_t const *Idx, unsigned NumIdx);
537 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
539 inline op_iterator idx_begin() { return op_begin()+1; }
540 inline const_op_iterator idx_begin() const { return op_begin()+1; }
541 inline op_iterator idx_end() { return op_end(); }
542 inline const_op_iterator idx_end() const { return op_end(); }
544 Value *getPointerOperand() {
545 return getOperand(0);
547 const Value *getPointerOperand() const {
548 return getOperand(0);
550 static unsigned getPointerOperandIndex() {
551 return 0U; // get index for modifying correct operand
554 /// getPointerOperandType - Method to return the pointer operand as a
556 const PointerType *getPointerOperandType() const {
557 return reinterpret_cast<const PointerType*>(getPointerOperand()->getType());
561 unsigned getNumIndices() const { // Note: always non-negative
562 return getNumOperands() - 1;
565 bool hasIndices() const {
566 return getNumOperands() > 1;
569 /// hasAllZeroIndices - Return true if all of the indices of this GEP are
570 /// zeros. If so, the result pointer and the first operand have the same
571 /// value, just potentially different types.
572 bool hasAllZeroIndices() const;
574 /// hasAllConstantIndices - Return true if all of the indices of this GEP are
575 /// constant integers. If so, the result pointer and the first operand have
576 /// a constant offset between them.
577 bool hasAllConstantIndices() const;
580 // Methods for support type inquiry through isa, cast, and dyn_cast:
581 static inline bool classof(const GetElementPtrInst *) { return true; }
582 static inline bool classof(const Instruction *I) {
583 return (I->getOpcode() == Instruction::GetElementPtr);
585 static inline bool classof(const Value *V) {
586 return isa<Instruction>(V) && classof(cast<Instruction>(V));
591 struct OperandTraits<GetElementPtrInst> : VariadicOperandTraits<1> {
594 template<typename InputIterator>
595 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
596 InputIterator IdxBegin,
597 InputIterator IdxEnd,
599 const std::string &NameStr,
600 Instruction *InsertBefore)
601 : Instruction(PointerType::get(checkType(
602 getIndexedType(Ptr->getType(),
604 cast<PointerType>(Ptr->getType())
605 ->getAddressSpace()),
607 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
608 Values, InsertBefore) {
609 init(Ptr, IdxBegin, IdxEnd, NameStr,
610 typename std::iterator_traits<InputIterator>::iterator_category());
612 template<typename InputIterator>
613 GetElementPtrInst::GetElementPtrInst(Value *Ptr,
614 InputIterator IdxBegin,
615 InputIterator IdxEnd,
617 const std::string &NameStr,
618 BasicBlock *InsertAtEnd)
619 : Instruction(PointerType::get(checkType(
620 getIndexedType(Ptr->getType(),
622 cast<PointerType>(Ptr->getType())
623 ->getAddressSpace()),
625 OperandTraits<GetElementPtrInst>::op_end(this) - Values,
626 Values, InsertAtEnd) {
627 init(Ptr, IdxBegin, IdxEnd, NameStr,
628 typename std::iterator_traits<InputIterator>::iterator_category());
632 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrInst, Value)
635 //===----------------------------------------------------------------------===//
637 //===----------------------------------------------------------------------===//
639 /// This instruction compares its operands according to the predicate given
640 /// to the constructor. It only operates on integers or pointers. The operands
641 /// must be identical types.
642 /// @brief Represent an integer comparison operator.
643 class ICmpInst: public CmpInst {
645 /// @brief Constructor with insert-before-instruction semantics.
647 Instruction *InsertBefore, ///< Where to insert
648 Predicate pred, ///< The predicate to use for the comparison
649 Value *LHS, ///< The left-hand-side of the expression
650 Value *RHS, ///< The right-hand-side of the expression
651 const std::string &NameStr = "" ///< Name of the instruction
652 ) : CmpInst(InsertBefore->getParent()->getContext()->
653 makeCmpResultType(LHS->getType()),
654 Instruction::ICmp, pred, LHS, RHS, NameStr,
656 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
657 pred <= CmpInst::LAST_ICMP_PREDICATE &&
658 "Invalid ICmp predicate value");
659 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
660 "Both operands to ICmp instruction are not of the same type!");
661 // Check that the operands are the right type
662 assert((getOperand(0)->getType()->isIntOrIntVector() ||
663 isa<PointerType>(getOperand(0)->getType())) &&
664 "Invalid operand types for ICmp instruction");
667 /// @brief Constructor with insert-at-end semantics.
669 BasicBlock &InsertAtEnd, ///< Block to insert into.
670 Predicate pred, ///< The predicate to use for the comparison
671 Value *LHS, ///< The left-hand-side of the expression
672 Value *RHS, ///< The right-hand-side of the expression
673 const std::string &NameStr = "" ///< Name of the instruction
674 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
675 Instruction::ICmp, pred, LHS, RHS, NameStr,
677 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
678 pred <= CmpInst::LAST_ICMP_PREDICATE &&
679 "Invalid ICmp predicate value");
680 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
681 "Both operands to ICmp instruction are not of the same type!");
682 // Check that the operands are the right type
683 assert((getOperand(0)->getType()->isIntOrIntVector() ||
684 isa<PointerType>(getOperand(0)->getType())) &&
685 "Invalid operand types for ICmp instruction");
688 /// @brief Constructor with no-insertion semantics
690 LLVMContext &Context, ///< Context to construct within
691 Predicate pred, ///< The predicate to use for the comparison
692 Value *LHS, ///< The left-hand-side of the expression
693 Value *RHS, ///< The right-hand-side of the expression
694 const std::string &NameStr = "" ///< Name of the instruction
695 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
696 Instruction::ICmp, pred, LHS, RHS, NameStr) {
697 assert(pred >= CmpInst::FIRST_ICMP_PREDICATE &&
698 pred <= CmpInst::LAST_ICMP_PREDICATE &&
699 "Invalid ICmp predicate value");
700 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
701 "Both operands to ICmp instruction are not of the same type!");
702 // Check that the operands are the right type
703 assert((getOperand(0)->getType()->isIntOrIntVector() ||
704 isa<PointerType>(getOperand(0)->getType())) &&
705 "Invalid operand types for ICmp instruction");
708 /// For example, EQ->EQ, SLE->SLE, UGT->SGT, etc.
709 /// @returns the predicate that would be the result if the operand were
710 /// regarded as signed.
711 /// @brief Return the signed version of the predicate
712 Predicate getSignedPredicate() const {
713 return getSignedPredicate(getPredicate());
716 /// This is a static version that you can use without an instruction.
717 /// @brief Return the signed version of the predicate.
718 static Predicate getSignedPredicate(Predicate pred);
720 /// For example, EQ->EQ, SLE->ULE, UGT->UGT, etc.
721 /// @returns the predicate that would be the result if the operand were
722 /// regarded as unsigned.
723 /// @brief Return the unsigned version of the predicate
724 Predicate getUnsignedPredicate() const {
725 return getUnsignedPredicate(getPredicate());
728 /// This is a static version that you can use without an instruction.
729 /// @brief Return the unsigned version of the predicate.
730 static Predicate getUnsignedPredicate(Predicate pred);
732 /// isEquality - Return true if this predicate is either EQ or NE. This also
733 /// tests for commutativity.
734 static bool isEquality(Predicate P) {
735 return P == ICMP_EQ || P == ICMP_NE;
738 /// isEquality - Return true if this predicate is either EQ or NE. This also
739 /// tests for commutativity.
740 bool isEquality() const {
741 return isEquality(getPredicate());
744 /// @returns true if the predicate of this ICmpInst is commutative
745 /// @brief Determine if this relation is commutative.
746 bool isCommutative() const { return isEquality(); }
748 /// isRelational - Return true if the predicate is relational (not EQ or NE).
750 bool isRelational() const {
751 return !isEquality();
754 /// isRelational - Return true if the predicate is relational (not EQ or NE).
756 static bool isRelational(Predicate P) {
757 return !isEquality(P);
760 /// @returns true if the predicate of this ICmpInst is signed, false otherwise
761 /// @brief Determine if this instruction's predicate is signed.
762 bool isSignedPredicate() const { return isSignedPredicate(getPredicate()); }
764 /// @returns true if the predicate provided is signed, false otherwise
765 /// @brief Determine if the predicate is signed.
766 static bool isSignedPredicate(Predicate pred);
768 /// @returns true if the specified compare predicate is
769 /// true when both operands are equal...
770 /// @brief Determine if the icmp is true when both operands are equal
771 static bool isTrueWhenEqual(ICmpInst::Predicate pred) {
772 return pred == ICmpInst::ICMP_EQ || pred == ICmpInst::ICMP_UGE ||
773 pred == ICmpInst::ICMP_SGE || pred == ICmpInst::ICMP_ULE ||
774 pred == ICmpInst::ICMP_SLE;
777 /// @returns true if the specified compare instruction is
778 /// true when both operands are equal...
779 /// @brief Determine if the ICmpInst returns true when both operands are equal
780 bool isTrueWhenEqual() {
781 return isTrueWhenEqual(getPredicate());
784 /// Initialize a set of values that all satisfy the predicate with C.
785 /// @brief Make a ConstantRange for a relation with a constant value.
786 static ConstantRange makeConstantRange(Predicate pred, const APInt &C);
788 /// Exchange the two operands to this instruction in such a way that it does
789 /// not modify the semantics of the instruction. The predicate value may be
790 /// changed to retain the same result if the predicate is order dependent
792 /// @brief Swap operands and adjust predicate.
793 void swapOperands() {
794 SubclassData = getSwappedPredicate();
795 Op<0>().swap(Op<1>());
798 virtual ICmpInst *clone(LLVMContext &Context) const;
800 // Methods for support type inquiry through isa, cast, and dyn_cast:
801 static inline bool classof(const ICmpInst *) { return true; }
802 static inline bool classof(const Instruction *I) {
803 return I->getOpcode() == Instruction::ICmp;
805 static inline bool classof(const Value *V) {
806 return isa<Instruction>(V) && classof(cast<Instruction>(V));
811 //===----------------------------------------------------------------------===//
813 //===----------------------------------------------------------------------===//
815 /// This instruction compares its operands according to the predicate given
816 /// to the constructor. It only operates on floating point values or packed
817 /// vectors of floating point values. The operands must be identical types.
818 /// @brief Represents a floating point comparison operator.
819 class FCmpInst: public CmpInst {
821 /// @brief Constructor with insert-before-instruction semantics.
823 Instruction *InsertBefore, ///< Where to insert
824 Predicate pred, ///< The predicate to use for the comparison
825 Value *LHS, ///< The left-hand-side of the expression
826 Value *RHS, ///< The right-hand-side of the expression
827 const std::string &NameStr = "" ///< Name of the instruction
828 ) : CmpInst(InsertBefore->getParent()->getContext()->
829 makeCmpResultType(LHS->getType()),
830 Instruction::FCmp, pred, LHS, RHS, NameStr,
832 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
833 "Invalid FCmp predicate value");
834 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
835 "Both operands to FCmp instruction are not of the same type!");
836 // Check that the operands are the right type
837 assert(getOperand(0)->getType()->isFPOrFPVector() &&
838 "Invalid operand types for FCmp instruction");
841 /// @brief Constructor with insert-at-end semantics.
843 BasicBlock &InsertAtEnd, ///< Block to insert into.
844 Predicate pred, ///< The predicate to use for the comparison
845 Value *LHS, ///< The left-hand-side of the expression
846 Value *RHS, ///< The right-hand-side of the expression
847 const std::string &NameStr = "" ///< Name of the instruction
848 ) : CmpInst(InsertAtEnd.getContext()->makeCmpResultType(LHS->getType()),
849 Instruction::FCmp, pred, LHS, RHS, NameStr,
851 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
852 "Invalid FCmp predicate value");
853 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
854 "Both operands to FCmp instruction are not of the same type!");
855 // Check that the operands are the right type
856 assert(getOperand(0)->getType()->isFPOrFPVector() &&
857 "Invalid operand types for FCmp instruction");
860 /// @brief Constructor with no-insertion semantics
862 LLVMContext &Context, ///< Context to build in
863 Predicate pred, ///< The predicate to use for the comparison
864 Value *LHS, ///< The left-hand-side of the expression
865 Value *RHS, ///< The right-hand-side of the expression
866 const std::string &NameStr = "" ///< Name of the instruction
867 ) : CmpInst(Context.makeCmpResultType(LHS->getType()),
868 Instruction::FCmp, pred, LHS, RHS, NameStr) {
869 assert(pred <= FCmpInst::LAST_FCMP_PREDICATE &&
870 "Invalid FCmp predicate value");
871 assert(getOperand(0)->getType() == getOperand(1)->getType() &&
872 "Both operands to FCmp instruction are not of the same type!");
873 // Check that the operands are the right type
874 assert(getOperand(0)->getType()->isFPOrFPVector() &&
875 "Invalid operand types for FCmp instruction");
878 /// @returns true if the predicate of this instruction is EQ or NE.
879 /// @brief Determine if this is an equality predicate.
880 bool isEquality() const {
881 return SubclassData == FCMP_OEQ || SubclassData == FCMP_ONE ||
882 SubclassData == FCMP_UEQ || SubclassData == FCMP_UNE;
885 /// @returns true if the predicate of this instruction is commutative.
886 /// @brief Determine if this is a commutative predicate.
887 bool isCommutative() const {
888 return isEquality() ||
889 SubclassData == FCMP_FALSE ||
890 SubclassData == FCMP_TRUE ||
891 SubclassData == FCMP_ORD ||
892 SubclassData == FCMP_UNO;
895 /// @returns true if the predicate is relational (not EQ or NE).
896 /// @brief Determine if this a relational predicate.
897 bool isRelational() const { return !isEquality(); }
899 /// Exchange the two operands to this instruction in such a way that it does
900 /// not modify the semantics of the instruction. The predicate value may be
901 /// changed to retain the same result if the predicate is order dependent
903 /// @brief Swap operands and adjust predicate.
904 void swapOperands() {
905 SubclassData = getSwappedPredicate();
906 Op<0>().swap(Op<1>());
909 virtual FCmpInst *clone(LLVMContext &Context) const;
911 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
912 static inline bool classof(const FCmpInst *) { return true; }
913 static inline bool classof(const Instruction *I) {
914 return I->getOpcode() == Instruction::FCmp;
916 static inline bool classof(const Value *V) {
917 return isa<Instruction>(V) && classof(cast<Instruction>(V));
922 //===----------------------------------------------------------------------===//
924 //===----------------------------------------------------------------------===//
925 /// CallInst - This class represents a function call, abstracting a target
926 /// machine's calling convention. This class uses low bit of the SubClassData
927 /// field to indicate whether or not this is a tail call. The rest of the bits
928 /// hold the calling convention of the call.
931 class CallInst : public Instruction {
932 AttrListPtr AttributeList; ///< parameter attributes for call
933 CallInst(const CallInst &CI);
934 void init(Value *Func, Value* const *Params, unsigned NumParams);
935 void init(Value *Func, Value *Actual1, Value *Actual2);
936 void init(Value *Func, Value *Actual);
937 void init(Value *Func);
939 template<typename InputIterator>
940 void init(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
941 const std::string &NameStr,
942 // This argument ensures that we have an iterator we can
943 // do arithmetic on in constant time
944 std::random_access_iterator_tag) {
945 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
947 // This requires that the iterator points to contiguous memory.
948 init(Func, NumArgs ? &*ArgBegin : 0, NumArgs);
952 /// Construct a CallInst given a range of arguments. InputIterator
953 /// must be a random-access iterator pointing to contiguous storage
954 /// (e.g. a std::vector<>::iterator). Checks are made for
955 /// random-accessness but not for contiguous storage as that would
956 /// incur runtime overhead.
957 /// @brief Construct a CallInst from a range of arguments
958 template<typename InputIterator>
959 CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
960 const std::string &NameStr, Instruction *InsertBefore);
962 /// Construct a CallInst given a range of arguments. InputIterator
963 /// must be a random-access iterator pointing to contiguous storage
964 /// (e.g. a std::vector<>::iterator). Checks are made for
965 /// random-accessness but not for contiguous storage as that would
966 /// incur runtime overhead.
967 /// @brief Construct a CallInst from a range of arguments
968 template<typename InputIterator>
969 inline CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
970 const std::string &NameStr, BasicBlock *InsertAtEnd);
972 CallInst(Value *F, Value *Actual, const std::string& NameStr,
973 Instruction *InsertBefore);
974 CallInst(Value *F, Value *Actual, const std::string& NameStr,
975 BasicBlock *InsertAtEnd);
976 explicit CallInst(Value *F, const std::string &NameStr,
977 Instruction *InsertBefore);
978 CallInst(Value *F, const std::string &NameStr, BasicBlock *InsertAtEnd);
980 template<typename InputIterator>
981 static CallInst *Create(Value *Func,
982 InputIterator ArgBegin, InputIterator ArgEnd,
983 const std::string &NameStr = "",
984 Instruction *InsertBefore = 0) {
985 return new((unsigned)(ArgEnd - ArgBegin + 1))
986 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertBefore);
988 template<typename InputIterator>
989 static CallInst *Create(Value *Func,
990 InputIterator ArgBegin, InputIterator ArgEnd,
991 const std::string &NameStr, BasicBlock *InsertAtEnd) {
992 return new((unsigned)(ArgEnd - ArgBegin + 1))
993 CallInst(Func, ArgBegin, ArgEnd, NameStr, InsertAtEnd);
995 static CallInst *Create(Value *F, Value *Actual,
996 const std::string& NameStr = "",
997 Instruction *InsertBefore = 0) {
998 return new(2) CallInst(F, Actual, NameStr, InsertBefore);
1000 static CallInst *Create(Value *F, Value *Actual, const std::string& NameStr,
1001 BasicBlock *InsertAtEnd) {
1002 return new(2) CallInst(F, Actual, NameStr, InsertAtEnd);
1004 static CallInst *Create(Value *F, const std::string &NameStr = "",
1005 Instruction *InsertBefore = 0) {
1006 return new(1) CallInst(F, NameStr, InsertBefore);
1008 static CallInst *Create(Value *F, const std::string &NameStr,
1009 BasicBlock *InsertAtEnd) {
1010 return new(1) CallInst(F, NameStr, InsertAtEnd);
1015 bool isTailCall() const { return SubclassData & 1; }
1016 void setTailCall(bool isTC = true) {
1017 SubclassData = (SubclassData & ~1) | unsigned(isTC);
1020 virtual CallInst *clone(LLVMContext &Context) const;
1022 /// Provide fast operand accessors
1023 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1025 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1027 unsigned getCallingConv() const { return SubclassData >> 1; }
1028 void setCallingConv(unsigned CC) {
1029 SubclassData = (SubclassData & 1) | (CC << 1);
1032 /// getAttributes - Return the parameter attributes for this call.
1034 const AttrListPtr &getAttributes() const { return AttributeList; }
1036 /// setAttributes - Set the parameter attributes for this call.
1038 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
1040 /// addAttribute - adds the attribute to the list of attributes.
1041 void addAttribute(unsigned i, Attributes attr);
1043 /// removeAttribute - removes the attribute from the list of attributes.
1044 void removeAttribute(unsigned i, Attributes attr);
1046 /// @brief Determine whether the call or the callee has the given attribute.
1047 bool paramHasAttr(unsigned i, Attributes attr) const;
1049 /// @brief Extract the alignment for a call or parameter (0=unknown).
1050 unsigned getParamAlignment(unsigned i) const {
1051 return AttributeList.getParamAlignment(i);
1054 /// @brief Determine if the call does not access memory.
1055 bool doesNotAccessMemory() const {
1056 return paramHasAttr(~0, Attribute::ReadNone);
1058 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
1059 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
1060 else removeAttribute(~0, Attribute::ReadNone);
1063 /// @brief Determine if the call does not access or only reads memory.
1064 bool onlyReadsMemory() const {
1065 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
1067 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
1068 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
1069 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
1072 /// @brief Determine if the call cannot return.
1073 bool doesNotReturn() const {
1074 return paramHasAttr(~0, Attribute::NoReturn);
1076 void setDoesNotReturn(bool DoesNotReturn = true) {
1077 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
1078 else removeAttribute(~0, Attribute::NoReturn);
1081 /// @brief Determine if the call cannot unwind.
1082 bool doesNotThrow() const {
1083 return paramHasAttr(~0, Attribute::NoUnwind);
1085 void setDoesNotThrow(bool DoesNotThrow = true) {
1086 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
1087 else removeAttribute(~0, Attribute::NoUnwind);
1090 /// @brief Determine if the call returns a structure through first
1091 /// pointer argument.
1092 bool hasStructRetAttr() const {
1093 // Be friendly and also check the callee.
1094 return paramHasAttr(1, Attribute::StructRet);
1097 /// @brief Determine if any call argument is an aggregate passed by value.
1098 bool hasByValArgument() const {
1099 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
1102 /// getCalledFunction - Return the function called, or null if this is an
1103 /// indirect function invocation.
1105 Function *getCalledFunction() const {
1106 return dyn_cast<Function>(Op<0>());
1109 /// getCalledValue - Get a pointer to the function that is invoked by this
1111 const Value *getCalledValue() const { return Op<0>(); }
1112 Value *getCalledValue() { return Op<0>(); }
1114 // Methods for support type inquiry through isa, cast, and dyn_cast:
1115 static inline bool classof(const CallInst *) { return true; }
1116 static inline bool classof(const Instruction *I) {
1117 return I->getOpcode() == Instruction::Call;
1119 static inline bool classof(const Value *V) {
1120 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1125 struct OperandTraits<CallInst> : VariadicOperandTraits<1> {
1128 template<typename InputIterator>
1129 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1130 const std::string &NameStr, BasicBlock *InsertAtEnd)
1131 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1132 ->getElementType())->getReturnType(),
1134 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1135 (unsigned)(ArgEnd - ArgBegin + 1), InsertAtEnd) {
1136 init(Func, ArgBegin, ArgEnd, NameStr,
1137 typename std::iterator_traits<InputIterator>::iterator_category());
1140 template<typename InputIterator>
1141 CallInst::CallInst(Value *Func, InputIterator ArgBegin, InputIterator ArgEnd,
1142 const std::string &NameStr, Instruction *InsertBefore)
1143 : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
1144 ->getElementType())->getReturnType(),
1146 OperandTraits<CallInst>::op_end(this) - (ArgEnd - ArgBegin + 1),
1147 (unsigned)(ArgEnd - ArgBegin + 1), InsertBefore) {
1148 init(Func, ArgBegin, ArgEnd, NameStr,
1149 typename std::iterator_traits<InputIterator>::iterator_category());
1152 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CallInst, Value)
1154 //===----------------------------------------------------------------------===//
1156 //===----------------------------------------------------------------------===//
1158 /// SelectInst - This class represents the LLVM 'select' instruction.
1160 class SelectInst : public Instruction {
1161 void init(Value *C, Value *S1, Value *S2) {
1162 assert(!areInvalidOperands(C, S1, S2) && "Invalid operands for select");
1168 SelectInst(const SelectInst &SI)
1169 : Instruction(SI.getType(), SI.getOpcode(), &Op<0>(), 3) {
1170 init(SI.Op<0>(), SI.Op<1>(), SI.Op<2>());
1172 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1173 Instruction *InsertBefore)
1174 : Instruction(S1->getType(), Instruction::Select,
1175 &Op<0>(), 3, InsertBefore) {
1179 SelectInst(Value *C, Value *S1, Value *S2, const std::string &NameStr,
1180 BasicBlock *InsertAtEnd)
1181 : Instruction(S1->getType(), Instruction::Select,
1182 &Op<0>(), 3, InsertAtEnd) {
1187 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1188 const std::string &NameStr = "",
1189 Instruction *InsertBefore = 0) {
1190 return new(3) SelectInst(C, S1, S2, NameStr, InsertBefore);
1192 static SelectInst *Create(Value *C, Value *S1, Value *S2,
1193 const std::string &NameStr,
1194 BasicBlock *InsertAtEnd) {
1195 return new(3) SelectInst(C, S1, S2, NameStr, InsertAtEnd);
1198 Value *getCondition() const { return Op<0>(); }
1199 Value *getTrueValue() const { return Op<1>(); }
1200 Value *getFalseValue() const { return Op<2>(); }
1202 /// areInvalidOperands - Return a string if the specified operands are invalid
1203 /// for a select operation, otherwise return null.
1204 static const char *areInvalidOperands(Value *Cond, Value *True, Value *False);
1206 /// Transparently provide more efficient getOperand methods.
1207 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1209 OtherOps getOpcode() const {
1210 return static_cast<OtherOps>(Instruction::getOpcode());
1213 virtual SelectInst *clone(LLVMContext &Context) const;
1215 // Methods for support type inquiry through isa, cast, and dyn_cast:
1216 static inline bool classof(const SelectInst *) { return true; }
1217 static inline bool classof(const Instruction *I) {
1218 return I->getOpcode() == Instruction::Select;
1220 static inline bool classof(const Value *V) {
1221 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1226 struct OperandTraits<SelectInst> : FixedNumOperandTraits<3> {
1229 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectInst, Value)
1231 //===----------------------------------------------------------------------===//
1233 //===----------------------------------------------------------------------===//
1235 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
1236 /// an argument of the specified type given a va_list and increments that list
1238 class VAArgInst : public UnaryInstruction {
1239 VAArgInst(const VAArgInst &VAA)
1240 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
1242 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr = "",
1243 Instruction *InsertBefore = 0)
1244 : UnaryInstruction(Ty, VAArg, List, InsertBefore) {
1247 VAArgInst(Value *List, const Type *Ty, const std::string &NameStr,
1248 BasicBlock *InsertAtEnd)
1249 : UnaryInstruction(Ty, VAArg, List, InsertAtEnd) {
1253 virtual VAArgInst *clone(LLVMContext &Context) const;
1255 // Methods for support type inquiry through isa, cast, and dyn_cast:
1256 static inline bool classof(const VAArgInst *) { return true; }
1257 static inline bool classof(const Instruction *I) {
1258 return I->getOpcode() == VAArg;
1260 static inline bool classof(const Value *V) {
1261 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1265 //===----------------------------------------------------------------------===//
1266 // ExtractElementInst Class
1267 //===----------------------------------------------------------------------===//
1269 /// ExtractElementInst - This instruction extracts a single (scalar)
1270 /// element from a VectorType value
1272 class ExtractElementInst : public Instruction {
1273 ExtractElementInst(const ExtractElementInst &EE) :
1274 Instruction(EE.getType(), ExtractElement, &Op<0>(), 2) {
1275 Op<0>() = EE.Op<0>();
1276 Op<1>() = EE.Op<1>();
1280 // allocate space for exactly two operands
1281 void *operator new(size_t s) {
1282 return User::operator new(s, 2); // FIXME: "unsigned Idx" forms of ctor?
1284 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr = "",
1285 Instruction *InsertBefore = 0);
1286 ExtractElementInst(Value *Vec, Value *Idx, const std::string &NameStr,
1287 BasicBlock *InsertAtEnd);
1289 /// isValidOperands - Return true if an extractelement instruction can be
1290 /// formed with the specified operands.
1291 static bool isValidOperands(const Value *Vec, const Value *Idx);
1293 virtual ExtractElementInst *clone(LLVMContext &Context) const;
1295 /// Transparently provide more efficient getOperand methods.
1296 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1298 // Methods for support type inquiry through isa, cast, and dyn_cast:
1299 static inline bool classof(const ExtractElementInst *) { return true; }
1300 static inline bool classof(const Instruction *I) {
1301 return I->getOpcode() == Instruction::ExtractElement;
1303 static inline bool classof(const Value *V) {
1304 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1309 struct OperandTraits<ExtractElementInst> : FixedNumOperandTraits<2> {
1312 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementInst, Value)
1314 //===----------------------------------------------------------------------===//
1315 // InsertElementInst Class
1316 //===----------------------------------------------------------------------===//
1318 /// InsertElementInst - This instruction inserts a single (scalar)
1319 /// element into a VectorType value
1321 class InsertElementInst : public Instruction {
1322 InsertElementInst(const InsertElementInst &IE);
1323 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1324 const std::string &NameStr = "",
1325 Instruction *InsertBefore = 0);
1326 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
1327 const std::string &NameStr, BasicBlock *InsertAtEnd);
1329 static InsertElementInst *Create(const InsertElementInst &IE) {
1330 return new(IE.getNumOperands()) InsertElementInst(IE);
1332 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1333 const std::string &NameStr = "",
1334 Instruction *InsertBefore = 0) {
1335 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertBefore);
1337 static InsertElementInst *Create(Value *Vec, Value *NewElt, Value *Idx,
1338 const std::string &NameStr,
1339 BasicBlock *InsertAtEnd) {
1340 return new(3) InsertElementInst(Vec, NewElt, Idx, NameStr, InsertAtEnd);
1343 /// isValidOperands - Return true if an insertelement instruction can be
1344 /// formed with the specified operands.
1345 static bool isValidOperands(const Value *Vec, const Value *NewElt,
1348 virtual InsertElementInst *clone(LLVMContext &Context) const;
1350 /// getType - Overload to return most specific vector type.
1352 const VectorType *getType() const {
1353 return reinterpret_cast<const VectorType*>(Instruction::getType());
1356 /// Transparently provide more efficient getOperand methods.
1357 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1359 // Methods for support type inquiry through isa, cast, and dyn_cast:
1360 static inline bool classof(const InsertElementInst *) { return true; }
1361 static inline bool classof(const Instruction *I) {
1362 return I->getOpcode() == Instruction::InsertElement;
1364 static inline bool classof(const Value *V) {
1365 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1370 struct OperandTraits<InsertElementInst> : FixedNumOperandTraits<3> {
1373 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementInst, Value)
1375 //===----------------------------------------------------------------------===//
1376 // ShuffleVectorInst Class
1377 //===----------------------------------------------------------------------===//
1379 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
1382 class ShuffleVectorInst : public Instruction {
1383 ShuffleVectorInst(const ShuffleVectorInst &IE);
1385 // allocate space for exactly three operands
1386 void *operator new(size_t s) {
1387 return User::operator new(s, 3);
1389 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1390 const std::string &NameStr = "",
1391 Instruction *InsertBefor = 0);
1392 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
1393 const std::string &NameStr, BasicBlock *InsertAtEnd);
1395 /// isValidOperands - Return true if a shufflevector instruction can be
1396 /// formed with the specified operands.
1397 static bool isValidOperands(const Value *V1, const Value *V2,
1400 virtual ShuffleVectorInst *clone(LLVMContext &Context) const;
1402 /// getType - Overload to return most specific vector type.
1404 const VectorType *getType() const {
1405 return reinterpret_cast<const VectorType*>(Instruction::getType());
1408 /// Transparently provide more efficient getOperand methods.
1409 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1411 /// getMaskValue - Return the index from the shuffle mask for the specified
1412 /// output result. This is either -1 if the element is undef or a number less
1413 /// than 2*numelements.
1414 int getMaskValue(unsigned i) const;
1416 // Methods for support type inquiry through isa, cast, and dyn_cast:
1417 static inline bool classof(const ShuffleVectorInst *) { return true; }
1418 static inline bool classof(const Instruction *I) {
1419 return I->getOpcode() == Instruction::ShuffleVector;
1421 static inline bool classof(const Value *V) {
1422 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1427 struct OperandTraits<ShuffleVectorInst> : FixedNumOperandTraits<3> {
1430 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorInst, Value)
1432 //===----------------------------------------------------------------------===//
1433 // ExtractValueInst Class
1434 //===----------------------------------------------------------------------===//
1436 /// ExtractValueInst - This instruction extracts a struct member or array
1437 /// element value from an aggregate value.
1439 class ExtractValueInst : public UnaryInstruction {
1440 SmallVector<unsigned, 4> Indices;
1442 ExtractValueInst(const ExtractValueInst &EVI);
1443 void init(const unsigned *Idx, unsigned NumIdx,
1444 const std::string &NameStr);
1445 void init(unsigned Idx, const std::string &NameStr);
1447 template<typename InputIterator>
1448 void init(InputIterator IdxBegin, InputIterator IdxEnd,
1449 const std::string &NameStr,
1450 // This argument ensures that we have an iterator we can
1451 // do arithmetic on in constant time
1452 std::random_access_iterator_tag) {
1453 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1455 // There's no fundamental reason why we require at least one index
1456 // (other than weirdness with &*IdxBegin being invalid; see
1457 // getelementptr's init routine for example). But there's no
1458 // present need to support it.
1459 assert(NumIdx > 0 && "ExtractValueInst must have at least one index");
1461 // This requires that the iterator points to contiguous memory.
1462 init(&*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1463 // we have to build an array here
1466 /// getIndexedType - Returns the type of the element that would be extracted
1467 /// with an extractvalue instruction with the specified parameters.
1469 /// Null is returned if the indices are invalid for the specified
1472 static const Type *getIndexedType(const Type *Agg,
1473 const unsigned *Idx, unsigned NumIdx);
1475 template<typename InputIterator>
1476 static const Type *getIndexedType(const Type *Ptr,
1477 InputIterator IdxBegin,
1478 InputIterator IdxEnd,
1479 // This argument ensures that we
1480 // have an iterator we can do
1481 // arithmetic on in constant time
1482 std::random_access_iterator_tag) {
1483 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1486 // This requires that the iterator points to contiguous memory.
1487 return getIndexedType(Ptr, &*IdxBegin, NumIdx);
1489 return getIndexedType(Ptr, (const unsigned *)0, NumIdx);
1492 /// Constructors - Create a extractvalue instruction with a base aggregate
1493 /// value and a list of indices. The first ctor can optionally insert before
1494 /// an existing instruction, the second appends the new instruction to the
1495 /// specified BasicBlock.
1496 template<typename InputIterator>
1497 inline ExtractValueInst(Value *Agg, InputIterator IdxBegin,
1498 InputIterator IdxEnd,
1499 const std::string &NameStr,
1500 Instruction *InsertBefore);
1501 template<typename InputIterator>
1502 inline ExtractValueInst(Value *Agg,
1503 InputIterator IdxBegin, InputIterator IdxEnd,
1504 const std::string &NameStr, BasicBlock *InsertAtEnd);
1506 // allocate space for exactly one operand
1507 void *operator new(size_t s) {
1508 return User::operator new(s, 1);
1512 template<typename InputIterator>
1513 static ExtractValueInst *Create(Value *Agg, InputIterator IdxBegin,
1514 InputIterator IdxEnd,
1515 const std::string &NameStr = "",
1516 Instruction *InsertBefore = 0) {
1518 ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertBefore);
1520 template<typename InputIterator>
1521 static ExtractValueInst *Create(Value *Agg,
1522 InputIterator IdxBegin, InputIterator IdxEnd,
1523 const std::string &NameStr,
1524 BasicBlock *InsertAtEnd) {
1525 return new ExtractValueInst(Agg, IdxBegin, IdxEnd, NameStr, InsertAtEnd);
1528 /// Constructors - These two creators are convenience methods because one
1529 /// index extractvalue instructions are much more common than those with
1531 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1532 const std::string &NameStr = "",
1533 Instruction *InsertBefore = 0) {
1534 unsigned Idxs[1] = { Idx };
1535 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertBefore);
1537 static ExtractValueInst *Create(Value *Agg, unsigned Idx,
1538 const std::string &NameStr,
1539 BasicBlock *InsertAtEnd) {
1540 unsigned Idxs[1] = { Idx };
1541 return new ExtractValueInst(Agg, Idxs, Idxs + 1, NameStr, InsertAtEnd);
1544 virtual ExtractValueInst *clone(LLVMContext &Context) const;
1546 /// getIndexedType - Returns the type of the element that would be extracted
1547 /// with an extractvalue instruction with the specified parameters.
1549 /// Null is returned if the indices are invalid for the specified
1552 template<typename InputIterator>
1553 static const Type *getIndexedType(const Type *Ptr,
1554 InputIterator IdxBegin,
1555 InputIterator IdxEnd) {
1556 return getIndexedType(Ptr, IdxBegin, IdxEnd,
1557 typename std::iterator_traits<InputIterator>::
1558 iterator_category());
1560 static const Type *getIndexedType(const Type *Ptr, unsigned Idx);
1562 typedef const unsigned* idx_iterator;
1563 inline idx_iterator idx_begin() const { return Indices.begin(); }
1564 inline idx_iterator idx_end() const { return Indices.end(); }
1566 Value *getAggregateOperand() {
1567 return getOperand(0);
1569 const Value *getAggregateOperand() const {
1570 return getOperand(0);
1572 static unsigned getAggregateOperandIndex() {
1573 return 0U; // get index for modifying correct operand
1576 unsigned getNumIndices() const { // Note: always non-negative
1577 return (unsigned)Indices.size();
1580 bool hasIndices() const {
1584 // Methods for support type inquiry through isa, cast, and dyn_cast:
1585 static inline bool classof(const ExtractValueInst *) { return true; }
1586 static inline bool classof(const Instruction *I) {
1587 return I->getOpcode() == Instruction::ExtractValue;
1589 static inline bool classof(const Value *V) {
1590 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1594 template<typename InputIterator>
1595 ExtractValueInst::ExtractValueInst(Value *Agg,
1596 InputIterator IdxBegin,
1597 InputIterator IdxEnd,
1598 const std::string &NameStr,
1599 Instruction *InsertBefore)
1600 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1602 ExtractValue, Agg, InsertBefore) {
1603 init(IdxBegin, IdxEnd, NameStr,
1604 typename std::iterator_traits<InputIterator>::iterator_category());
1606 template<typename InputIterator>
1607 ExtractValueInst::ExtractValueInst(Value *Agg,
1608 InputIterator IdxBegin,
1609 InputIterator IdxEnd,
1610 const std::string &NameStr,
1611 BasicBlock *InsertAtEnd)
1612 : UnaryInstruction(checkType(getIndexedType(Agg->getType(),
1614 ExtractValue, Agg, InsertAtEnd) {
1615 init(IdxBegin, IdxEnd, NameStr,
1616 typename std::iterator_traits<InputIterator>::iterator_category());
1620 //===----------------------------------------------------------------------===//
1621 // InsertValueInst Class
1622 //===----------------------------------------------------------------------===//
1624 /// InsertValueInst - This instruction inserts a struct field of array element
1625 /// value into an aggregate value.
1627 class InsertValueInst : public Instruction {
1628 SmallVector<unsigned, 4> Indices;
1630 void *operator new(size_t, unsigned); // Do not implement
1631 InsertValueInst(const InsertValueInst &IVI);
1632 void init(Value *Agg, Value *Val, const unsigned *Idx, unsigned NumIdx,
1633 const std::string &NameStr);
1634 void init(Value *Agg, Value *Val, unsigned Idx, const std::string &NameStr);
1636 template<typename InputIterator>
1637 void init(Value *Agg, Value *Val,
1638 InputIterator IdxBegin, InputIterator IdxEnd,
1639 const std::string &NameStr,
1640 // This argument ensures that we have an iterator we can
1641 // do arithmetic on in constant time
1642 std::random_access_iterator_tag) {
1643 unsigned NumIdx = static_cast<unsigned>(std::distance(IdxBegin, IdxEnd));
1645 // There's no fundamental reason why we require at least one index
1646 // (other than weirdness with &*IdxBegin being invalid; see
1647 // getelementptr's init routine for example). But there's no
1648 // present need to support it.
1649 assert(NumIdx > 0 && "InsertValueInst must have at least one index");
1651 // This requires that the iterator points to contiguous memory.
1652 init(Agg, Val, &*IdxBegin, NumIdx, NameStr); // FIXME: for the general case
1653 // we have to build an array here
1656 /// Constructors - Create a insertvalue instruction with a base aggregate
1657 /// value, a value to insert, and a list of indices. The first ctor can
1658 /// optionally insert before an existing instruction, the second appends
1659 /// the new instruction to the specified BasicBlock.
1660 template<typename InputIterator>
1661 inline InsertValueInst(Value *Agg, Value *Val, InputIterator IdxBegin,
1662 InputIterator IdxEnd,
1663 const std::string &NameStr,
1664 Instruction *InsertBefore);
1665 template<typename InputIterator>
1666 inline InsertValueInst(Value *Agg, Value *Val,
1667 InputIterator IdxBegin, InputIterator IdxEnd,
1668 const std::string &NameStr, BasicBlock *InsertAtEnd);
1670 /// Constructors - These two constructors are convenience methods because one
1671 /// and two index insertvalue instructions are so common.
1672 InsertValueInst(Value *Agg, Value *Val,
1673 unsigned Idx, const std::string &NameStr = "",
1674 Instruction *InsertBefore = 0);
1675 InsertValueInst(Value *Agg, Value *Val, unsigned Idx,
1676 const std::string &NameStr, BasicBlock *InsertAtEnd);
1678 // allocate space for exactly two operands
1679 void *operator new(size_t s) {
1680 return User::operator new(s, 2);
1683 template<typename InputIterator>
1684 static InsertValueInst *Create(Value *Agg, Value *Val, InputIterator IdxBegin,
1685 InputIterator IdxEnd,
1686 const std::string &NameStr = "",
1687 Instruction *InsertBefore = 0) {
1688 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1689 NameStr, InsertBefore);
1691 template<typename InputIterator>
1692 static InsertValueInst *Create(Value *Agg, Value *Val,
1693 InputIterator IdxBegin, InputIterator IdxEnd,
1694 const std::string &NameStr,
1695 BasicBlock *InsertAtEnd) {
1696 return new InsertValueInst(Agg, Val, IdxBegin, IdxEnd,
1697 NameStr, InsertAtEnd);
1700 /// Constructors - These two creators are convenience methods because one
1701 /// index insertvalue instructions are much more common than those with
1703 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1704 const std::string &NameStr = "",
1705 Instruction *InsertBefore = 0) {
1706 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertBefore);
1708 static InsertValueInst *Create(Value *Agg, Value *Val, unsigned Idx,
1709 const std::string &NameStr,
1710 BasicBlock *InsertAtEnd) {
1711 return new InsertValueInst(Agg, Val, Idx, NameStr, InsertAtEnd);
1714 virtual InsertValueInst *clone(LLVMContext &Context) const;
1716 /// Transparently provide more efficient getOperand methods.
1717 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1719 typedef const unsigned* idx_iterator;
1720 inline idx_iterator idx_begin() const { return Indices.begin(); }
1721 inline idx_iterator idx_end() const { return Indices.end(); }
1723 Value *getAggregateOperand() {
1724 return getOperand(0);
1726 const Value *getAggregateOperand() const {
1727 return getOperand(0);
1729 static unsigned getAggregateOperandIndex() {
1730 return 0U; // get index for modifying correct operand
1733 Value *getInsertedValueOperand() {
1734 return getOperand(1);
1736 const Value *getInsertedValueOperand() const {
1737 return getOperand(1);
1739 static unsigned getInsertedValueOperandIndex() {
1740 return 1U; // get index for modifying correct operand
1743 unsigned getNumIndices() const { // Note: always non-negative
1744 return (unsigned)Indices.size();
1747 bool hasIndices() const {
1751 // Methods for support type inquiry through isa, cast, and dyn_cast:
1752 static inline bool classof(const InsertValueInst *) { return true; }
1753 static inline bool classof(const Instruction *I) {
1754 return I->getOpcode() == Instruction::InsertValue;
1756 static inline bool classof(const Value *V) {
1757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1762 struct OperandTraits<InsertValueInst> : FixedNumOperandTraits<2> {
1765 template<typename InputIterator>
1766 InsertValueInst::InsertValueInst(Value *Agg,
1768 InputIterator IdxBegin,
1769 InputIterator IdxEnd,
1770 const std::string &NameStr,
1771 Instruction *InsertBefore)
1772 : Instruction(Agg->getType(), InsertValue,
1773 OperandTraits<InsertValueInst>::op_begin(this),
1775 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1776 typename std::iterator_traits<InputIterator>::iterator_category());
1778 template<typename InputIterator>
1779 InsertValueInst::InsertValueInst(Value *Agg,
1781 InputIterator IdxBegin,
1782 InputIterator IdxEnd,
1783 const std::string &NameStr,
1784 BasicBlock *InsertAtEnd)
1785 : Instruction(Agg->getType(), InsertValue,
1786 OperandTraits<InsertValueInst>::op_begin(this),
1788 init(Agg, Val, IdxBegin, IdxEnd, NameStr,
1789 typename std::iterator_traits<InputIterator>::iterator_category());
1792 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertValueInst, Value)
1794 //===----------------------------------------------------------------------===//
1796 //===----------------------------------------------------------------------===//
1798 // PHINode - The PHINode class is used to represent the magical mystical PHI
1799 // node, that can not exist in nature, but can be synthesized in a computer
1800 // scientist's overactive imagination.
1802 class PHINode : public Instruction {
1803 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
1804 /// ReservedSpace - The number of operands actually allocated. NumOperands is
1805 /// the number actually in use.
1806 unsigned ReservedSpace;
1807 PHINode(const PHINode &PN);
1808 // allocate space for exactly zero operands
1809 void *operator new(size_t s) {
1810 return User::operator new(s, 0);
1812 explicit PHINode(const Type *Ty, const std::string &NameStr = "",
1813 Instruction *InsertBefore = 0)
1814 : Instruction(Ty, Instruction::PHI, 0, 0, InsertBefore),
1819 PHINode(const Type *Ty, const std::string &NameStr, BasicBlock *InsertAtEnd)
1820 : Instruction(Ty, Instruction::PHI, 0, 0, InsertAtEnd),
1825 static PHINode *Create(const Type *Ty, const std::string &NameStr = "",
1826 Instruction *InsertBefore = 0) {
1827 return new PHINode(Ty, NameStr, InsertBefore);
1829 static PHINode *Create(const Type *Ty, const std::string &NameStr,
1830 BasicBlock *InsertAtEnd) {
1831 return new PHINode(Ty, NameStr, InsertAtEnd);
1835 /// reserveOperandSpace - This method can be used to avoid repeated
1836 /// reallocation of PHI operand lists by reserving space for the correct
1837 /// number of operands before adding them. Unlike normal vector reserves,
1838 /// this method can also be used to trim the operand space.
1839 void reserveOperandSpace(unsigned NumValues) {
1840 resizeOperands(NumValues*2);
1843 virtual PHINode *clone(LLVMContext &Context) const;
1845 /// Provide fast operand accessors
1846 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
1848 /// getNumIncomingValues - Return the number of incoming edges
1850 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
1852 /// getIncomingValue - Return incoming value number x
1854 Value *getIncomingValue(unsigned i) const {
1855 assert(i*2 < getNumOperands() && "Invalid value number!");
1856 return getOperand(i*2);
1858 void setIncomingValue(unsigned i, Value *V) {
1859 assert(i*2 < getNumOperands() && "Invalid value number!");
1862 static unsigned getOperandNumForIncomingValue(unsigned i) {
1865 static unsigned getIncomingValueNumForOperand(unsigned i) {
1866 assert(i % 2 == 0 && "Invalid incoming-value operand index!");
1870 /// getIncomingBlock - Return incoming basic block corresponding
1871 /// to value use iterator
1873 template <typename U>
1874 BasicBlock *getIncomingBlock(value_use_iterator<U> I) const {
1875 assert(this == *I && "Iterator doesn't point to PHI's Uses?");
1876 return static_cast<BasicBlock*>((&I.getUse() + 1)->get());
1878 /// getIncomingBlock - Return incoming basic block number x
1880 BasicBlock *getIncomingBlock(unsigned i) const {
1881 return static_cast<BasicBlock*>(getOperand(i*2+1));
1883 void setIncomingBlock(unsigned i, BasicBlock *BB) {
1884 setOperand(i*2+1, BB);
1886 static unsigned getOperandNumForIncomingBlock(unsigned i) {
1889 static unsigned getIncomingBlockNumForOperand(unsigned i) {
1890 assert(i % 2 == 1 && "Invalid incoming-block operand index!");
1894 /// addIncoming - Add an incoming value to the end of the PHI list
1896 void addIncoming(Value *V, BasicBlock *BB) {
1897 assert(V && "PHI node got a null value!");
1898 assert(BB && "PHI node got a null basic block!");
1899 assert(getType() == V->getType() &&
1900 "All operands to PHI node must be the same type as the PHI node!");
1901 unsigned OpNo = NumOperands;
1902 if (OpNo+2 > ReservedSpace)
1903 resizeOperands(0); // Get more space!
1904 // Initialize some new operands.
1905 NumOperands = OpNo+2;
1906 OperandList[OpNo] = V;
1907 OperandList[OpNo+1] = BB;
1910 /// removeIncomingValue - Remove an incoming value. This is useful if a
1911 /// predecessor basic block is deleted. The value removed is returned.
1913 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1914 /// is true), the PHI node is destroyed and any uses of it are replaced with
1915 /// dummy values. The only time there should be zero incoming values to a PHI
1916 /// node is when the block is dead, so this strategy is sound.
1918 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1920 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty=true) {
1921 int Idx = getBasicBlockIndex(BB);
1922 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1923 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1926 /// getBasicBlockIndex - Return the first index of the specified basic
1927 /// block in the value list for this PHI. Returns -1 if no instance.
1929 int getBasicBlockIndex(const BasicBlock *BB) const {
1930 Use *OL = OperandList;
1931 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1932 if (OL[i+1].get() == BB) return i/2;
1936 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1937 return getIncomingValue(getBasicBlockIndex(BB));
1940 /// hasConstantValue - If the specified PHI node always merges together the
1941 /// same value, return the value, otherwise return null.
1943 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1945 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1946 static inline bool classof(const PHINode *) { return true; }
1947 static inline bool classof(const Instruction *I) {
1948 return I->getOpcode() == Instruction::PHI;
1950 static inline bool classof(const Value *V) {
1951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1954 void resizeOperands(unsigned NumOperands);
1958 struct OperandTraits<PHINode> : HungoffOperandTraits<2> {
1961 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(PHINode, Value)
1964 //===----------------------------------------------------------------------===//
1966 //===----------------------------------------------------------------------===//
1968 //===---------------------------------------------------------------------------
1969 /// ReturnInst - Return a value (possibly void), from a function. Execution
1970 /// does not continue in this function any longer.
1972 class ReturnInst : public TerminatorInst {
1973 ReturnInst(const ReturnInst &RI);
1976 // ReturnInst constructors:
1977 // ReturnInst() - 'ret void' instruction
1978 // ReturnInst( null) - 'ret void' instruction
1979 // ReturnInst(Value* X) - 'ret X' instruction
1980 // ReturnInst( null, Inst *I) - 'ret void' instruction, insert before I
1981 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1982 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of B
1983 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of B
1985 // NOTE: If the Value* passed is of type void then the constructor behaves as
1986 // if it was passed NULL.
1987 explicit ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0);
1988 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd);
1989 explicit ReturnInst(BasicBlock *InsertAtEnd);
1991 static ReturnInst* Create(Value *retVal = 0, Instruction *InsertBefore = 0) {
1992 return new(!!retVal) ReturnInst(retVal, InsertBefore);
1994 static ReturnInst* Create(Value *retVal, BasicBlock *InsertAtEnd) {
1995 return new(!!retVal) ReturnInst(retVal, InsertAtEnd);
1997 static ReturnInst* Create(BasicBlock *InsertAtEnd) {
1998 return new(0) ReturnInst(InsertAtEnd);
2000 virtual ~ReturnInst();
2002 virtual ReturnInst *clone(LLVMContext &Context) const;
2004 /// Provide fast operand accessors
2005 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2007 /// Convenience accessor
2008 Value *getReturnValue(unsigned n = 0) const {
2009 return n < getNumOperands()
2014 unsigned getNumSuccessors() const { return 0; }
2016 // Methods for support type inquiry through isa, cast, and dyn_cast:
2017 static inline bool classof(const ReturnInst *) { return true; }
2018 static inline bool classof(const Instruction *I) {
2019 return (I->getOpcode() == Instruction::Ret);
2021 static inline bool classof(const Value *V) {
2022 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2025 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2026 virtual unsigned getNumSuccessorsV() const;
2027 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2031 struct OperandTraits<ReturnInst> : OptionalOperandTraits<> {
2034 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ReturnInst, Value)
2036 //===----------------------------------------------------------------------===//
2038 //===----------------------------------------------------------------------===//
2040 //===---------------------------------------------------------------------------
2041 /// BranchInst - Conditional or Unconditional Branch instruction.
2043 class BranchInst : public TerminatorInst {
2044 /// Ops list - Branches are strange. The operands are ordered:
2045 /// [Cond, FalseDest,] TrueDest. This makes some accessors faster because
2046 /// they don't have to check for cond/uncond branchness. These are mostly
2047 /// accessed relative from op_end().
2048 BranchInst(const BranchInst &BI);
2050 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
2051 // BranchInst(BB *B) - 'br B'
2052 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
2053 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
2054 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
2055 // BranchInst(BB* B, BB *I) - 'br B' insert at end
2056 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
2057 explicit BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0);
2058 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2059 Instruction *InsertBefore = 0);
2060 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd);
2061 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
2062 BasicBlock *InsertAtEnd);
2064 static BranchInst *Create(BasicBlock *IfTrue, Instruction *InsertBefore = 0) {
2065 return new(1, true) BranchInst(IfTrue, InsertBefore);
2067 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2068 Value *Cond, Instruction *InsertBefore = 0) {
2069 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertBefore);
2071 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *InsertAtEnd) {
2072 return new(1, true) BranchInst(IfTrue, InsertAtEnd);
2074 static BranchInst *Create(BasicBlock *IfTrue, BasicBlock *IfFalse,
2075 Value *Cond, BasicBlock *InsertAtEnd) {
2076 return new(3) BranchInst(IfTrue, IfFalse, Cond, InsertAtEnd);
2081 /// Transparently provide more efficient getOperand methods.
2082 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2084 virtual BranchInst *clone(LLVMContext &Context) const;
2086 bool isUnconditional() const { return getNumOperands() == 1; }
2087 bool isConditional() const { return getNumOperands() == 3; }
2089 Value *getCondition() const {
2090 assert(isConditional() && "Cannot get condition of an uncond branch!");
2094 void setCondition(Value *V) {
2095 assert(isConditional() && "Cannot set condition of unconditional branch!");
2099 // setUnconditionalDest - Change the current branch to an unconditional branch
2100 // targeting the specified block.
2101 // FIXME: Eliminate this ugly method.
2102 void setUnconditionalDest(BasicBlock *Dest) {
2104 if (isConditional()) { // Convert this to an uncond branch.
2108 OperandList = op_begin();
2112 unsigned getNumSuccessors() const { return 1+isConditional(); }
2114 BasicBlock *getSuccessor(unsigned i) const {
2115 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
2116 return cast_or_null<BasicBlock>((&Op<-1>() - i)->get());
2119 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2120 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
2121 *(&Op<-1>() - idx) = NewSucc;
2124 // Methods for support type inquiry through isa, cast, and dyn_cast:
2125 static inline bool classof(const BranchInst *) { return true; }
2126 static inline bool classof(const Instruction *I) {
2127 return (I->getOpcode() == Instruction::Br);
2129 static inline bool classof(const Value *V) {
2130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2133 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2134 virtual unsigned getNumSuccessorsV() const;
2135 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2139 struct OperandTraits<BranchInst> : VariadicOperandTraits<1> {};
2141 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BranchInst, Value)
2143 //===----------------------------------------------------------------------===//
2145 //===----------------------------------------------------------------------===//
2147 //===---------------------------------------------------------------------------
2148 /// SwitchInst - Multiway switch
2150 class SwitchInst : public TerminatorInst {
2151 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2152 unsigned ReservedSpace;
2153 // Operand[0] = Value to switch on
2154 // Operand[1] = Default basic block destination
2155 // Operand[2n ] = Value to match
2156 // Operand[2n+1] = BasicBlock to go to on match
2157 SwitchInst(const SwitchInst &RI);
2158 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
2159 void resizeOperands(unsigned No);
2160 // allocate space for exactly zero operands
2161 void *operator new(size_t s) {
2162 return User::operator new(s, 0);
2164 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2165 /// switch on and a default destination. The number of additional cases can
2166 /// be specified here to make memory allocation more efficient. This
2167 /// constructor can also autoinsert before another instruction.
2168 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2169 Instruction *InsertBefore = 0);
2171 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
2172 /// switch on and a default destination. The number of additional cases can
2173 /// be specified here to make memory allocation more efficient. This
2174 /// constructor also autoinserts at the end of the specified BasicBlock.
2175 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
2176 BasicBlock *InsertAtEnd);
2178 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2179 unsigned NumCases, Instruction *InsertBefore = 0) {
2180 return new SwitchInst(Value, Default, NumCases, InsertBefore);
2182 static SwitchInst *Create(Value *Value, BasicBlock *Default,
2183 unsigned NumCases, BasicBlock *InsertAtEnd) {
2184 return new SwitchInst(Value, Default, NumCases, InsertAtEnd);
2188 /// Provide fast operand accessors
2189 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2191 // Accessor Methods for Switch stmt
2192 Value *getCondition() const { return getOperand(0); }
2193 void setCondition(Value *V) { setOperand(0, V); }
2195 BasicBlock *getDefaultDest() const {
2196 return cast<BasicBlock>(getOperand(1));
2199 /// getNumCases - return the number of 'cases' in this switch instruction.
2200 /// Note that case #0 is always the default case.
2201 unsigned getNumCases() const {
2202 return getNumOperands()/2;
2205 /// getCaseValue - Return the specified case value. Note that case #0, the
2206 /// default destination, does not have a case value.
2207 ConstantInt *getCaseValue(unsigned i) {
2208 assert(i && i < getNumCases() && "Illegal case value to get!");
2209 return getSuccessorValue(i);
2212 /// getCaseValue - Return the specified case value. Note that case #0, the
2213 /// default destination, does not have a case value.
2214 const ConstantInt *getCaseValue(unsigned i) const {
2215 assert(i && i < getNumCases() && "Illegal case value to get!");
2216 return getSuccessorValue(i);
2219 /// findCaseValue - Search all of the case values for the specified constant.
2220 /// If it is explicitly handled, return the case number of it, otherwise
2221 /// return 0 to indicate that it is handled by the default handler.
2222 unsigned findCaseValue(const ConstantInt *C) const {
2223 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
2224 if (getCaseValue(i) == C)
2229 /// findCaseDest - Finds the unique case value for a given successor. Returns
2230 /// null if the successor is not found, not unique, or is the default case.
2231 ConstantInt *findCaseDest(BasicBlock *BB) {
2232 if (BB == getDefaultDest()) return NULL;
2234 ConstantInt *CI = NULL;
2235 for (unsigned i = 1, e = getNumCases(); i != e; ++i) {
2236 if (getSuccessor(i) == BB) {
2237 if (CI) return NULL; // Multiple cases lead to BB.
2238 else CI = getCaseValue(i);
2244 /// addCase - Add an entry to the switch instruction...
2246 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
2248 /// removeCase - This method removes the specified successor from the switch
2249 /// instruction. Note that this cannot be used to remove the default
2250 /// destination (successor #0).
2252 void removeCase(unsigned idx);
2254 virtual SwitchInst *clone(LLVMContext &Context) const;
2256 unsigned getNumSuccessors() const { return getNumOperands()/2; }
2257 BasicBlock *getSuccessor(unsigned idx) const {
2258 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
2259 return cast<BasicBlock>(getOperand(idx*2+1));
2261 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2262 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
2263 setOperand(idx*2+1, NewSucc);
2266 // getSuccessorValue - Return the value associated with the specified
2268 ConstantInt *getSuccessorValue(unsigned idx) const {
2269 assert(idx < getNumSuccessors() && "Successor # out of range!");
2270 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
2273 // Methods for support type inquiry through isa, cast, and dyn_cast:
2274 static inline bool classof(const SwitchInst *) { return true; }
2275 static inline bool classof(const Instruction *I) {
2276 return I->getOpcode() == Instruction::Switch;
2278 static inline bool classof(const Value *V) {
2279 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2282 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2283 virtual unsigned getNumSuccessorsV() const;
2284 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2288 struct OperandTraits<SwitchInst> : HungoffOperandTraits<2> {
2291 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SwitchInst, Value)
2294 //===----------------------------------------------------------------------===//
2296 //===----------------------------------------------------------------------===//
2298 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
2299 /// calling convention of the call.
2301 class InvokeInst : public TerminatorInst {
2302 AttrListPtr AttributeList;
2303 InvokeInst(const InvokeInst &BI);
2304 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
2305 Value* const *Args, unsigned NumArgs);
2307 template<typename InputIterator>
2308 void init(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2309 InputIterator ArgBegin, InputIterator ArgEnd,
2310 const std::string &NameStr,
2311 // This argument ensures that we have an iterator we can
2312 // do arithmetic on in constant time
2313 std::random_access_iterator_tag) {
2314 unsigned NumArgs = (unsigned)std::distance(ArgBegin, ArgEnd);
2316 // This requires that the iterator points to contiguous memory.
2317 init(Func, IfNormal, IfException, NumArgs ? &*ArgBegin : 0, NumArgs);
2321 /// Construct an InvokeInst given a range of arguments.
2322 /// InputIterator must be a random-access iterator pointing to
2323 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2324 /// made for random-accessness but not for contiguous storage as
2325 /// that would incur runtime overhead.
2327 /// @brief Construct an InvokeInst from a range of arguments
2328 template<typename InputIterator>
2329 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2330 InputIterator ArgBegin, InputIterator ArgEnd,
2332 const std::string &NameStr, Instruction *InsertBefore);
2334 /// Construct an InvokeInst given a range of arguments.
2335 /// InputIterator must be a random-access iterator pointing to
2336 /// contiguous storage (e.g. a std::vector<>::iterator). Checks are
2337 /// made for random-accessness but not for contiguous storage as
2338 /// that would incur runtime overhead.
2340 /// @brief Construct an InvokeInst from a range of arguments
2341 template<typename InputIterator>
2342 inline InvokeInst(Value *Func, BasicBlock *IfNormal, BasicBlock *IfException,
2343 InputIterator ArgBegin, InputIterator ArgEnd,
2345 const std::string &NameStr, BasicBlock *InsertAtEnd);
2347 template<typename InputIterator>
2348 static InvokeInst *Create(Value *Func,
2349 BasicBlock *IfNormal, BasicBlock *IfException,
2350 InputIterator ArgBegin, InputIterator ArgEnd,
2351 const std::string &NameStr = "",
2352 Instruction *InsertBefore = 0) {
2353 unsigned Values(ArgEnd - ArgBegin + 3);
2354 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2355 Values, NameStr, InsertBefore);
2357 template<typename InputIterator>
2358 static InvokeInst *Create(Value *Func,
2359 BasicBlock *IfNormal, BasicBlock *IfException,
2360 InputIterator ArgBegin, InputIterator ArgEnd,
2361 const std::string &NameStr,
2362 BasicBlock *InsertAtEnd) {
2363 unsigned Values(ArgEnd - ArgBegin + 3);
2364 return new(Values) InvokeInst(Func, IfNormal, IfException, ArgBegin, ArgEnd,
2365 Values, NameStr, InsertAtEnd);
2368 virtual InvokeInst *clone(LLVMContext &Context) const;
2370 /// Provide fast operand accessors
2371 DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value);
2373 /// getCallingConv/setCallingConv - Get or set the calling convention of this
2375 unsigned getCallingConv() const { return SubclassData; }
2376 void setCallingConv(unsigned CC) {
2380 /// getAttributes - Return the parameter attributes for this invoke.
2382 const AttrListPtr &getAttributes() const { return AttributeList; }
2384 /// setAttributes - Set the parameter attributes for this invoke.
2386 void setAttributes(const AttrListPtr &Attrs) { AttributeList = Attrs; }
2388 /// addAttribute - adds the attribute to the list of attributes.
2389 void addAttribute(unsigned i, Attributes attr);
2391 /// removeAttribute - removes the attribute from the list of attributes.
2392 void removeAttribute(unsigned i, Attributes attr);
2394 /// @brief Determine whether the call or the callee has the given attribute.
2395 bool paramHasAttr(unsigned i, Attributes attr) const;
2397 /// @brief Extract the alignment for a call or parameter (0=unknown).
2398 unsigned getParamAlignment(unsigned i) const {
2399 return AttributeList.getParamAlignment(i);
2402 /// @brief Determine if the call does not access memory.
2403 bool doesNotAccessMemory() const {
2404 return paramHasAttr(0, Attribute::ReadNone);
2406 void setDoesNotAccessMemory(bool NotAccessMemory = true) {
2407 if (NotAccessMemory) addAttribute(~0, Attribute::ReadNone);
2408 else removeAttribute(~0, Attribute::ReadNone);
2411 /// @brief Determine if the call does not access or only reads memory.
2412 bool onlyReadsMemory() const {
2413 return doesNotAccessMemory() || paramHasAttr(~0, Attribute::ReadOnly);
2415 void setOnlyReadsMemory(bool OnlyReadsMemory = true) {
2416 if (OnlyReadsMemory) addAttribute(~0, Attribute::ReadOnly);
2417 else removeAttribute(~0, Attribute::ReadOnly | Attribute::ReadNone);
2420 /// @brief Determine if the call cannot return.
2421 bool doesNotReturn() const {
2422 return paramHasAttr(~0, Attribute::NoReturn);
2424 void setDoesNotReturn(bool DoesNotReturn = true) {
2425 if (DoesNotReturn) addAttribute(~0, Attribute::NoReturn);
2426 else removeAttribute(~0, Attribute::NoReturn);
2429 /// @brief Determine if the call cannot unwind.
2430 bool doesNotThrow() const {
2431 return paramHasAttr(~0, Attribute::NoUnwind);
2433 void setDoesNotThrow(bool DoesNotThrow = true) {
2434 if (DoesNotThrow) addAttribute(~0, Attribute::NoUnwind);
2435 else removeAttribute(~0, Attribute::NoUnwind);
2438 /// @brief Determine if the call returns a structure through first
2439 /// pointer argument.
2440 bool hasStructRetAttr() const {
2441 // Be friendly and also check the callee.
2442 return paramHasAttr(1, Attribute::StructRet);
2445 /// @brief Determine if any call argument is an aggregate passed by value.
2446 bool hasByValArgument() const {
2447 return AttributeList.hasAttrSomewhere(Attribute::ByVal);
2450 /// getCalledFunction - Return the function called, or null if this is an
2451 /// indirect function invocation.
2453 Function *getCalledFunction() const {
2454 return dyn_cast<Function>(getOperand(0));
2457 /// getCalledValue - Get a pointer to the function that is invoked by this
2459 const Value *getCalledValue() const { return getOperand(0); }
2460 Value *getCalledValue() { return getOperand(0); }
2462 // get*Dest - Return the destination basic blocks...
2463 BasicBlock *getNormalDest() const {
2464 return cast<BasicBlock>(getOperand(1));
2466 BasicBlock *getUnwindDest() const {
2467 return cast<BasicBlock>(getOperand(2));
2469 void setNormalDest(BasicBlock *B) {
2473 void setUnwindDest(BasicBlock *B) {
2477 BasicBlock *getSuccessor(unsigned i) const {
2478 assert(i < 2 && "Successor # out of range for invoke!");
2479 return i == 0 ? getNormalDest() : getUnwindDest();
2482 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
2483 assert(idx < 2 && "Successor # out of range for invoke!");
2484 setOperand(idx+1, NewSucc);
2487 unsigned getNumSuccessors() const { return 2; }
2489 // Methods for support type inquiry through isa, cast, and dyn_cast:
2490 static inline bool classof(const InvokeInst *) { return true; }
2491 static inline bool classof(const Instruction *I) {
2492 return (I->getOpcode() == Instruction::Invoke);
2494 static inline bool classof(const Value *V) {
2495 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2498 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2499 virtual unsigned getNumSuccessorsV() const;
2500 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2504 struct OperandTraits<InvokeInst> : VariadicOperandTraits<3> {
2507 template<typename InputIterator>
2508 InvokeInst::InvokeInst(Value *Func,
2509 BasicBlock *IfNormal, BasicBlock *IfException,
2510 InputIterator ArgBegin, InputIterator ArgEnd,
2512 const std::string &NameStr, Instruction *InsertBefore)
2513 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2514 ->getElementType())->getReturnType(),
2515 Instruction::Invoke,
2516 OperandTraits<InvokeInst>::op_end(this) - Values,
2517 Values, InsertBefore) {
2518 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2519 typename std::iterator_traits<InputIterator>::iterator_category());
2521 template<typename InputIterator>
2522 InvokeInst::InvokeInst(Value *Func,
2523 BasicBlock *IfNormal, BasicBlock *IfException,
2524 InputIterator ArgBegin, InputIterator ArgEnd,
2526 const std::string &NameStr, BasicBlock *InsertAtEnd)
2527 : TerminatorInst(cast<FunctionType>(cast<PointerType>(Func->getType())
2528 ->getElementType())->getReturnType(),
2529 Instruction::Invoke,
2530 OperandTraits<InvokeInst>::op_end(this) - Values,
2531 Values, InsertAtEnd) {
2532 init(Func, IfNormal, IfException, ArgBegin, ArgEnd, NameStr,
2533 typename std::iterator_traits<InputIterator>::iterator_category());
2536 DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InvokeInst, Value)
2538 //===----------------------------------------------------------------------===//
2540 //===----------------------------------------------------------------------===//
2542 //===---------------------------------------------------------------------------
2543 /// UnwindInst - Immediately exit the current function, unwinding the stack
2544 /// until an invoke instruction is found.
2546 class UnwindInst : public TerminatorInst {
2547 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2549 // allocate space for exactly zero operands
2550 void *operator new(size_t s) {
2551 return User::operator new(s, 0);
2553 explicit UnwindInst(Instruction *InsertBefore = 0);
2554 explicit UnwindInst(BasicBlock *InsertAtEnd);
2556 virtual UnwindInst *clone(LLVMContext &Context) const;
2558 unsigned getNumSuccessors() const { return 0; }
2560 // Methods for support type inquiry through isa, cast, and dyn_cast:
2561 static inline bool classof(const UnwindInst *) { return true; }
2562 static inline bool classof(const Instruction *I) {
2563 return I->getOpcode() == Instruction::Unwind;
2565 static inline bool classof(const Value *V) {
2566 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2569 virtual BasicBlock *getSuccessorV(unsigned idx) const;
2570 virtual unsigned getNumSuccessorsV() const;
2571 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
2574 //===----------------------------------------------------------------------===//
2575 // UnreachableInst Class
2576 //===----------------------------------------------------------------------===//
2578 //===---------------------------------------------------------------------------
2579 /// UnreachableInst - This function has undefined behavior. In particular, the
2580 /// presence of this instruction indicates some higher level knowledge that the
2581 /// end of the block cannot be reached.
2583 class UnreachableInst : public TerminatorInst {
2584 void *operator new(size_t, unsigned); // DO NOT IMPLEMENT
2586 // allocate space for exactly zero operands
2587 void *operator new(size_t s) {
2588 return User::operator new(s, 0);
2590 explicit UnreachableInst(Instruction *InsertBefore = 0);
2591 explicit UnreachableInst(BasicBlock *InsertAtEnd);
2593 virtual UnreachableInst *clone(LLVMContext &Context) const;
2595 unsigned getNumSuccessors() const { return 0; }
2597 // Methods for support type inquiry through isa, cast, and dyn_cast:
2598 static inline bool classof(const UnreachableInst *) { return true; }
2599 static inline bool classof(const Instruction *I) {
2600 return I->getOpcode() == Instruction::Unreachable;
2602 static inline bool classof(const Value *V) {
2603 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);
2611 //===----------------------------------------------------------------------===//
2613 //===----------------------------------------------------------------------===//
2615 /// @brief This class represents a truncation of integer types.
2616 class TruncInst : public CastInst {
2617 /// Private copy constructor
2618 TruncInst(const TruncInst &CI)
2619 : CastInst(CI.getType(), Trunc, CI.getOperand(0)) {
2622 /// @brief Constructor with insert-before-instruction semantics
2624 Value *S, ///< The value to be truncated
2625 const Type *Ty, ///< The (smaller) type to truncate to
2626 const std::string &NameStr = "", ///< A name for the new instruction
2627 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2630 /// @brief Constructor with insert-at-end-of-block semantics
2632 Value *S, ///< The value to be truncated
2633 const Type *Ty, ///< The (smaller) type to truncate to
2634 const std::string &NameStr, ///< A name for the new instruction
2635 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2638 /// @brief Clone an identical TruncInst
2639 virtual CastInst *clone(LLVMContext &Context) const;
2641 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2642 static inline bool classof(const TruncInst *) { return true; }
2643 static inline bool classof(const Instruction *I) {
2644 return I->getOpcode() == Trunc;
2646 static inline bool classof(const Value *V) {
2647 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2651 //===----------------------------------------------------------------------===//
2653 //===----------------------------------------------------------------------===//
2655 /// @brief This class represents zero extension of integer types.
2656 class ZExtInst : public CastInst {
2657 /// @brief Private copy constructor
2658 ZExtInst(const ZExtInst &CI)
2659 : CastInst(CI.getType(), ZExt, CI.getOperand(0)) {
2662 /// @brief Constructor with insert-before-instruction semantics
2664 Value *S, ///< The value to be zero extended
2665 const Type *Ty, ///< The type to zero extend to
2666 const std::string &NameStr = "", ///< A name for the new instruction
2667 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2670 /// @brief Constructor with insert-at-end semantics.
2672 Value *S, ///< The value to be zero extended
2673 const Type *Ty, ///< The type to zero extend to
2674 const std::string &NameStr, ///< A name for the new instruction
2675 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2678 /// @brief Clone an identical ZExtInst
2679 virtual CastInst *clone(LLVMContext &Context) const;
2681 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2682 static inline bool classof(const ZExtInst *) { return true; }
2683 static inline bool classof(const Instruction *I) {
2684 return I->getOpcode() == ZExt;
2686 static inline bool classof(const Value *V) {
2687 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2691 //===----------------------------------------------------------------------===//
2693 //===----------------------------------------------------------------------===//
2695 /// @brief This class represents a sign extension of integer types.
2696 class SExtInst : public CastInst {
2697 /// @brief Private copy constructor
2698 SExtInst(const SExtInst &CI)
2699 : CastInst(CI.getType(), SExt, CI.getOperand(0)) {
2702 /// @brief Constructor with insert-before-instruction semantics
2704 Value *S, ///< The value to be sign extended
2705 const Type *Ty, ///< The type to sign extend to
2706 const std::string &NameStr = "", ///< A name for the new instruction
2707 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2710 /// @brief Constructor with insert-at-end-of-block semantics
2712 Value *S, ///< The value to be sign extended
2713 const Type *Ty, ///< The type to sign extend to
2714 const std::string &NameStr, ///< A name for the new instruction
2715 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2718 /// @brief Clone an identical SExtInst
2719 virtual CastInst *clone(LLVMContext &Context) const;
2721 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2722 static inline bool classof(const SExtInst *) { return true; }
2723 static inline bool classof(const Instruction *I) {
2724 return I->getOpcode() == SExt;
2726 static inline bool classof(const Value *V) {
2727 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2731 //===----------------------------------------------------------------------===//
2732 // FPTruncInst Class
2733 //===----------------------------------------------------------------------===//
2735 /// @brief This class represents a truncation of floating point types.
2736 class FPTruncInst : public CastInst {
2737 FPTruncInst(const FPTruncInst &CI)
2738 : CastInst(CI.getType(), FPTrunc, CI.getOperand(0)) {
2741 /// @brief Constructor with insert-before-instruction semantics
2743 Value *S, ///< The value to be truncated
2744 const Type *Ty, ///< The type to truncate to
2745 const std::string &NameStr = "", ///< A name for the new instruction
2746 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2749 /// @brief Constructor with insert-before-instruction semantics
2751 Value *S, ///< The value to be truncated
2752 const Type *Ty, ///< The type to truncate to
2753 const std::string &NameStr, ///< A name for the new instruction
2754 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2757 /// @brief Clone an identical FPTruncInst
2758 virtual CastInst *clone(LLVMContext &Context) const;
2760 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2761 static inline bool classof(const FPTruncInst *) { return true; }
2762 static inline bool classof(const Instruction *I) {
2763 return I->getOpcode() == FPTrunc;
2765 static inline bool classof(const Value *V) {
2766 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2770 //===----------------------------------------------------------------------===//
2772 //===----------------------------------------------------------------------===//
2774 /// @brief This class represents an extension of floating point types.
2775 class FPExtInst : public CastInst {
2776 FPExtInst(const FPExtInst &CI)
2777 : CastInst(CI.getType(), FPExt, CI.getOperand(0)) {
2780 /// @brief Constructor with insert-before-instruction semantics
2782 Value *S, ///< The value to be extended
2783 const Type *Ty, ///< The type to extend to
2784 const std::string &NameStr = "", ///< A name for the new instruction
2785 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2788 /// @brief Constructor with insert-at-end-of-block semantics
2790 Value *S, ///< The value to be extended
2791 const Type *Ty, ///< The type to extend to
2792 const std::string &NameStr, ///< A name for the new instruction
2793 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2796 /// @brief Clone an identical FPExtInst
2797 virtual CastInst *clone(LLVMContext &Context) const;
2799 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2800 static inline bool classof(const FPExtInst *) { return true; }
2801 static inline bool classof(const Instruction *I) {
2802 return I->getOpcode() == FPExt;
2804 static inline bool classof(const Value *V) {
2805 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2809 //===----------------------------------------------------------------------===//
2811 //===----------------------------------------------------------------------===//
2813 /// @brief This class represents a cast unsigned integer to floating point.
2814 class UIToFPInst : public CastInst {
2815 UIToFPInst(const UIToFPInst &CI)
2816 : CastInst(CI.getType(), UIToFP, CI.getOperand(0)) {
2819 /// @brief Constructor with insert-before-instruction semantics
2821 Value *S, ///< The value to be converted
2822 const Type *Ty, ///< The type to convert to
2823 const std::string &NameStr = "", ///< A name for the new instruction
2824 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2827 /// @brief Constructor with insert-at-end-of-block semantics
2829 Value *S, ///< The value to be converted
2830 const Type *Ty, ///< The type to convert to
2831 const std::string &NameStr, ///< A name for the new instruction
2832 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2835 /// @brief Clone an identical UIToFPInst
2836 virtual CastInst *clone(LLVMContext &Context) const;
2838 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2839 static inline bool classof(const UIToFPInst *) { return true; }
2840 static inline bool classof(const Instruction *I) {
2841 return I->getOpcode() == UIToFP;
2843 static inline bool classof(const Value *V) {
2844 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2848 //===----------------------------------------------------------------------===//
2850 //===----------------------------------------------------------------------===//
2852 /// @brief This class represents a cast from signed integer to floating point.
2853 class SIToFPInst : public CastInst {
2854 SIToFPInst(const SIToFPInst &CI)
2855 : CastInst(CI.getType(), SIToFP, CI.getOperand(0)) {
2858 /// @brief Constructor with insert-before-instruction semantics
2860 Value *S, ///< The value to be converted
2861 const Type *Ty, ///< The type to convert to
2862 const std::string &NameStr = "", ///< A name for the new instruction
2863 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2866 /// @brief Constructor with insert-at-end-of-block semantics
2868 Value *S, ///< The value to be converted
2869 const Type *Ty, ///< The type to convert to
2870 const std::string &NameStr, ///< A name for the new instruction
2871 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2874 /// @brief Clone an identical SIToFPInst
2875 virtual CastInst *clone(LLVMContext &Context) const;
2877 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2878 static inline bool classof(const SIToFPInst *) { return true; }
2879 static inline bool classof(const Instruction *I) {
2880 return I->getOpcode() == SIToFP;
2882 static inline bool classof(const Value *V) {
2883 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2887 //===----------------------------------------------------------------------===//
2889 //===----------------------------------------------------------------------===//
2891 /// @brief This class represents a cast from floating point to unsigned integer
2892 class FPToUIInst : public CastInst {
2893 FPToUIInst(const FPToUIInst &CI)
2894 : CastInst(CI.getType(), FPToUI, CI.getOperand(0)) {
2897 /// @brief Constructor with insert-before-instruction semantics
2899 Value *S, ///< The value to be converted
2900 const Type *Ty, ///< The type to convert to
2901 const std::string &NameStr = "", ///< A name for the new instruction
2902 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2905 /// @brief Constructor with insert-at-end-of-block semantics
2907 Value *S, ///< The value to be converted
2908 const Type *Ty, ///< The type to convert to
2909 const std::string &NameStr, ///< A name for the new instruction
2910 BasicBlock *InsertAtEnd ///< Where to insert the new instruction
2913 /// @brief Clone an identical FPToUIInst
2914 virtual CastInst *clone(LLVMContext &Context) const;
2916 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2917 static inline bool classof(const FPToUIInst *) { return true; }
2918 static inline bool classof(const Instruction *I) {
2919 return I->getOpcode() == FPToUI;
2921 static inline bool classof(const Value *V) {
2922 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2926 //===----------------------------------------------------------------------===//
2928 //===----------------------------------------------------------------------===//
2930 /// @brief This class represents a cast from floating point to signed integer.
2931 class FPToSIInst : public CastInst {
2932 FPToSIInst(const FPToSIInst &CI)
2933 : CastInst(CI.getType(), FPToSI, CI.getOperand(0)) {
2936 /// @brief Constructor with insert-before-instruction semantics
2938 Value *S, ///< The value to be converted
2939 const Type *Ty, ///< The type to convert to
2940 const std::string &NameStr = "", ///< A name for the new instruction
2941 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2944 /// @brief Constructor with insert-at-end-of-block semantics
2946 Value *S, ///< The value to be converted
2947 const Type *Ty, ///< The type to convert to
2948 const std::string &NameStr, ///< A name for the new instruction
2949 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2952 /// @brief Clone an identical FPToSIInst
2953 virtual CastInst *clone(LLVMContext &Context) const;
2955 /// @brief Methods for support type inquiry through isa, cast, and dyn_cast:
2956 static inline bool classof(const FPToSIInst *) { return true; }
2957 static inline bool classof(const Instruction *I) {
2958 return I->getOpcode() == FPToSI;
2960 static inline bool classof(const Value *V) {
2961 return isa<Instruction>(V) && classof(cast<Instruction>(V));
2965 //===----------------------------------------------------------------------===//
2966 // IntToPtrInst Class
2967 //===----------------------------------------------------------------------===//
2969 /// @brief This class represents a cast from an integer to a pointer.
2970 class IntToPtrInst : public CastInst {
2971 IntToPtrInst(const IntToPtrInst &CI)
2972 : CastInst(CI.getType(), IntToPtr, CI.getOperand(0)) {
2975 /// @brief Constructor with insert-before-instruction semantics
2977 Value *S, ///< The value to be converted
2978 const Type *Ty, ///< The type to convert to
2979 const std::string &NameStr = "", ///< A name for the new instruction
2980 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
2983 /// @brief Constructor with insert-at-end-of-block semantics
2985 Value *S, ///< The value to be converted
2986 const Type *Ty, ///< The type to convert to
2987 const std::string &NameStr, ///< A name for the new instruction
2988 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
2991 /// @brief Clone an identical IntToPtrInst
2992 virtual CastInst *clone(LLVMContext &Context) const;
2994 // Methods for support type inquiry through isa, cast, and dyn_cast:
2995 static inline bool classof(const IntToPtrInst *) { return true; }
2996 static inline bool classof(const Instruction *I) {
2997 return I->getOpcode() == IntToPtr;
2999 static inline bool classof(const Value *V) {
3000 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3004 //===----------------------------------------------------------------------===//
3005 // PtrToIntInst Class
3006 //===----------------------------------------------------------------------===//
3008 /// @brief This class represents a cast from a pointer to an integer
3009 class PtrToIntInst : public CastInst {
3010 PtrToIntInst(const PtrToIntInst &CI)
3011 : CastInst(CI.getType(), PtrToInt, CI.getOperand(0)) {
3014 /// @brief Constructor with insert-before-instruction semantics
3016 Value *S, ///< The value to be converted
3017 const Type *Ty, ///< The type to convert to
3018 const std::string &NameStr = "", ///< A name for the new instruction
3019 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3022 /// @brief Constructor with insert-at-end-of-block semantics
3024 Value *S, ///< The value to be converted
3025 const Type *Ty, ///< The type to convert to
3026 const std::string &NameStr, ///< A name for the new instruction
3027 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3030 /// @brief Clone an identical PtrToIntInst
3031 virtual CastInst *clone(LLVMContext &Context) const;
3033 // Methods for support type inquiry through isa, cast, and dyn_cast:
3034 static inline bool classof(const PtrToIntInst *) { return true; }
3035 static inline bool classof(const Instruction *I) {
3036 return I->getOpcode() == PtrToInt;
3038 static inline bool classof(const Value *V) {
3039 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3043 //===----------------------------------------------------------------------===//
3044 // BitCastInst Class
3045 //===----------------------------------------------------------------------===//
3047 /// @brief This class represents a no-op cast from one type to another.
3048 class BitCastInst : public CastInst {
3049 BitCastInst(const BitCastInst &CI)
3050 : CastInst(CI.getType(), BitCast, CI.getOperand(0)) {
3053 /// @brief Constructor with insert-before-instruction semantics
3055 Value *S, ///< The value to be casted
3056 const Type *Ty, ///< The type to casted to
3057 const std::string &NameStr = "", ///< A name for the new instruction
3058 Instruction *InsertBefore = 0 ///< Where to insert the new instruction
3061 /// @brief Constructor with insert-at-end-of-block semantics
3063 Value *S, ///< The value to be casted
3064 const Type *Ty, ///< The type to casted to
3065 const std::string &NameStr, ///< A name for the new instruction
3066 BasicBlock *InsertAtEnd ///< The block to insert the instruction into
3069 /// @brief Clone an identical BitCastInst
3070 virtual CastInst *clone(LLVMContext &Context) const;
3072 // Methods for support type inquiry through isa, cast, and dyn_cast:
3073 static inline bool classof(const BitCastInst *) { return true; }
3074 static inline bool classof(const Instruction *I) {
3075 return I->getOpcode() == BitCast;
3077 static inline bool classof(const Value *V) {
3078 return isa<Instruction>(V) && classof(cast<Instruction>(V));
3082 } // End llvm namespace