1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source 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"
28 //===----------------------------------------------------------------------===//
29 // AllocationInst Class
30 //===----------------------------------------------------------------------===//
32 /// AllocationInst - This class is the common base class of MallocInst and
35 class AllocationInst : public UnaryInstruction {
38 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
39 const std::string &Name = "", Instruction *InsertBefore = 0);
40 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
41 const std::string &Name, BasicBlock *InsertAtEnd);
43 // Out of line virtual method, so the vtable, etc has a home.
44 virtual ~AllocationInst();
46 /// isArrayAllocation - Return true if there is an allocation size parameter
47 /// to the allocation instruction that is not 1.
49 bool isArrayAllocation() const;
51 /// getArraySize - Get the number of element allocated, for a simple
52 /// allocation of a single element, this will return a constant 1 value.
54 inline const Value *getArraySize() const { return getOperand(0); }
55 inline Value *getArraySize() { return getOperand(0); }
57 /// getType - Overload to return most specific pointer type
59 inline const PointerType *getType() const {
60 return reinterpret_cast<const PointerType*>(Instruction::getType());
63 /// getAllocatedType - Return the type that is being allocated by the
66 const Type *getAllocatedType() const;
68 /// getAlignment - Return the alignment of the memory that is being allocated
69 /// by the instruction.
71 unsigned getAlignment() const { return Alignment; }
72 void setAlignment(unsigned Align) {
73 assert((Align & (Align-1)) == 0 && "Alignment is not a power of 2!");
77 virtual Instruction *clone() const = 0;
79 // Methods for support type inquiry through isa, cast, and dyn_cast:
80 static inline bool classof(const AllocationInst *) { return true; }
81 static inline bool classof(const Instruction *I) {
82 return I->getOpcode() == Instruction::Alloca ||
83 I->getOpcode() == Instruction::Malloc;
85 static inline bool classof(const Value *V) {
86 return isa<Instruction>(V) && classof(cast<Instruction>(V));
91 //===----------------------------------------------------------------------===//
93 //===----------------------------------------------------------------------===//
95 /// MallocInst - an instruction to allocated memory on the heap
97 class MallocInst : public AllocationInst {
98 MallocInst(const MallocInst &MI);
100 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
101 const std::string &Name = "",
102 Instruction *InsertBefore = 0)
103 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
104 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
105 BasicBlock *InsertAtEnd)
106 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
108 explicit MallocInst(const Type *Ty, const std::string &Name,
109 Instruction *InsertBefore = 0)
110 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertBefore) {}
111 MallocInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
112 : AllocationInst(Ty, 0, Malloc, 0, Name, InsertAtEnd) {}
114 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
115 const std::string &Name, BasicBlock *InsertAtEnd)
116 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
117 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
118 const std::string &Name = "",
119 Instruction *InsertBefore = 0)
120 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
122 virtual MallocInst *clone() const;
124 // Methods for support type inquiry through isa, cast, and dyn_cast:
125 static inline bool classof(const MallocInst *) { return true; }
126 static inline bool classof(const Instruction *I) {
127 return (I->getOpcode() == Instruction::Malloc);
129 static inline bool classof(const Value *V) {
130 return isa<Instruction>(V) && classof(cast<Instruction>(V));
135 //===----------------------------------------------------------------------===//
137 //===----------------------------------------------------------------------===//
139 /// AllocaInst - an instruction to allocate memory on the stack
141 class AllocaInst : public AllocationInst {
142 AllocaInst(const AllocaInst &);
144 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
145 const std::string &Name = "",
146 Instruction *InsertBefore = 0)
147 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
148 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
149 BasicBlock *InsertAtEnd)
150 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
152 AllocaInst(const Type *Ty, const std::string &Name,
153 Instruction *InsertBefore = 0)
154 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertBefore) {}
155 AllocaInst(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
156 : AllocationInst(Ty, 0, Alloca, 0, Name, InsertAtEnd) {}
158 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
159 const std::string &Name = "", Instruction *InsertBefore = 0)
160 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
161 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
162 const std::string &Name, BasicBlock *InsertAtEnd)
163 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
165 virtual AllocaInst *clone() const;
167 // Methods for support type inquiry through isa, cast, and dyn_cast:
168 static inline bool classof(const AllocaInst *) { return true; }
169 static inline bool classof(const Instruction *I) {
170 return (I->getOpcode() == Instruction::Alloca);
172 static inline bool classof(const Value *V) {
173 return isa<Instruction>(V) && classof(cast<Instruction>(V));
178 //===----------------------------------------------------------------------===//
180 //===----------------------------------------------------------------------===//
182 /// FreeInst - an instruction to deallocate memory
184 class FreeInst : public UnaryInstruction {
187 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
188 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
190 virtual FreeInst *clone() const;
192 virtual bool mayWriteToMemory() const { return true; }
194 // Methods for support type inquiry through isa, cast, and dyn_cast:
195 static inline bool classof(const FreeInst *) { return true; }
196 static inline bool classof(const Instruction *I) {
197 return (I->getOpcode() == Instruction::Free);
199 static inline bool classof(const Value *V) {
200 return isa<Instruction>(V) && classof(cast<Instruction>(V));
205 //===----------------------------------------------------------------------===//
207 //===----------------------------------------------------------------------===//
209 /// LoadInst - an instruction for reading from memory. This uses the
210 /// SubclassData field in Value to store whether or not the load is volatile.
212 class LoadInst : public UnaryInstruction {
213 LoadInst(const LoadInst &LI)
214 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
215 setVolatile(LI.isVolatile());
223 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
224 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
225 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
226 Instruction *InsertBefore = 0);
227 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
228 BasicBlock *InsertAtEnd);
230 /// isVolatile - Return true if this is a load from a volatile memory
233 bool isVolatile() const { return SubclassData; }
235 /// setVolatile - Specify whether this is a volatile load or not.
237 void setVolatile(bool V) { SubclassData = V; }
239 virtual LoadInst *clone() const;
241 virtual bool mayWriteToMemory() const { return isVolatile(); }
243 Value *getPointerOperand() { return getOperand(0); }
244 const Value *getPointerOperand() const { return getOperand(0); }
245 static unsigned getPointerOperandIndex() { return 0U; }
247 // Methods for support type inquiry through isa, cast, and dyn_cast:
248 static inline bool classof(const LoadInst *) { return true; }
249 static inline bool classof(const Instruction *I) {
250 return I->getOpcode() == Instruction::Load;
252 static inline bool classof(const Value *V) {
253 return isa<Instruction>(V) && classof(cast<Instruction>(V));
258 //===----------------------------------------------------------------------===//
260 //===----------------------------------------------------------------------===//
262 /// StoreInst - an instruction for storing to memory
264 class StoreInst : public Instruction {
266 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
267 Ops[0].init(SI.Ops[0], this);
268 Ops[1].init(SI.Ops[1], this);
269 setVolatile(SI.isVolatile());
276 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
277 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
278 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
279 Instruction *InsertBefore = 0);
280 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
283 /// isVolatile - Return true if this is a load from a volatile memory
286 bool isVolatile() const { return SubclassData; }
288 /// setVolatile - Specify whether this is a volatile load or not.
290 void setVolatile(bool V) { SubclassData = V; }
292 /// Transparently provide more efficient getOperand methods.
293 Value *getOperand(unsigned i) const {
294 assert(i < 2 && "getOperand() out of range!");
297 void setOperand(unsigned i, Value *Val) {
298 assert(i < 2 && "setOperand() out of range!");
301 unsigned getNumOperands() const { return 2; }
304 virtual StoreInst *clone() const;
306 virtual bool mayWriteToMemory() const { return true; }
308 Value *getPointerOperand() { return getOperand(1); }
309 const Value *getPointerOperand() const { return getOperand(1); }
310 static unsigned getPointerOperandIndex() { return 1U; }
312 // Methods for support type inquiry through isa, cast, and dyn_cast:
313 static inline bool classof(const StoreInst *) { return true; }
314 static inline bool classof(const Instruction *I) {
315 return I->getOpcode() == Instruction::Store;
317 static inline bool classof(const Value *V) {
318 return isa<Instruction>(V) && classof(cast<Instruction>(V));
323 //===----------------------------------------------------------------------===//
324 // GetElementPtrInst Class
325 //===----------------------------------------------------------------------===//
327 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
328 /// access elements of arrays and structs
330 class GetElementPtrInst : public Instruction {
331 GetElementPtrInst(const GetElementPtrInst &GEPI)
332 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
333 0, GEPI.getNumOperands()) {
334 Use *OL = OperandList = new Use[NumOperands];
335 Use *GEPIOL = GEPI.OperandList;
336 for (unsigned i = 0, E = NumOperands; i != E; ++i)
337 OL[i].init(GEPIOL[i], this);
339 void init(Value *Ptr, const std::vector<Value*> &Idx);
340 void init(Value *Ptr, Value *Idx0, Value *Idx1);
341 void init(Value *Ptr, Value *Idx);
343 /// Constructors - Create a getelementptr instruction with a base pointer an
344 /// list of indices. The first ctor can optionally insert before an existing
345 /// instruction, the second appends the new instruction to the specified
347 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
348 const std::string &Name = "", Instruction *InsertBefore =0);
349 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
350 const std::string &Name, BasicBlock *InsertAtEnd);
352 /// Constructors - These two constructors are convenience methods because one
353 /// and two index getelementptr instructions are so common.
354 GetElementPtrInst(Value *Ptr, Value *Idx,
355 const std::string &Name = "", Instruction *InsertBefore =0);
356 GetElementPtrInst(Value *Ptr, Value *Idx,
357 const std::string &Name, BasicBlock *InsertAtEnd);
358 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
359 const std::string &Name = "", Instruction *InsertBefore =0);
360 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
361 const std::string &Name, BasicBlock *InsertAtEnd);
362 ~GetElementPtrInst();
364 virtual GetElementPtrInst *clone() const;
366 // getType - Overload to return most specific pointer type...
367 inline const PointerType *getType() const {
368 return reinterpret_cast<const PointerType*>(Instruction::getType());
371 /// getIndexedType - Returns the type of the element that would be loaded with
372 /// a load instruction with the specified parameters.
374 /// A null type is returned if the indices are invalid for the specified
377 static const Type *getIndexedType(const Type *Ptr,
378 const std::vector<Value*> &Indices,
379 bool AllowStructLeaf = false);
380 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
381 bool AllowStructLeaf = false);
382 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
384 inline op_iterator idx_begin() { return op_begin()+1; }
385 inline const_op_iterator idx_begin() const { return op_begin()+1; }
386 inline op_iterator idx_end() { return op_end(); }
387 inline const_op_iterator idx_end() const { return op_end(); }
389 Value *getPointerOperand() {
390 return getOperand(0);
392 const Value *getPointerOperand() const {
393 return getOperand(0);
395 static unsigned getPointerOperandIndex() {
396 return 0U; // get index for modifying correct operand
399 inline unsigned getNumIndices() const { // Note: always non-negative
400 return getNumOperands() - 1;
403 inline bool hasIndices() const {
404 return getNumOperands() > 1;
407 // Methods for support type inquiry through isa, cast, and dyn_cast:
408 static inline bool classof(const GetElementPtrInst *) { return true; }
409 static inline bool classof(const Instruction *I) {
410 return (I->getOpcode() == Instruction::GetElementPtr);
412 static inline bool classof(const Value *V) {
413 return isa<Instruction>(V) && classof(cast<Instruction>(V));
417 //===----------------------------------------------------------------------===//
419 //===----------------------------------------------------------------------===//
421 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
424 class SetCondInst : public BinaryOperator {
426 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
427 const std::string &Name = "", Instruction *InsertBefore = 0);
428 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
429 const std::string &Name, BasicBlock *InsertAtEnd);
431 /// getInverseCondition - Return the inverse of the current condition opcode.
432 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
434 BinaryOps getInverseCondition() const {
435 return getInverseCondition(getOpcode());
438 /// getInverseCondition - Static version that you can use without an
439 /// instruction available.
441 static BinaryOps getInverseCondition(BinaryOps Opcode);
443 /// getSwappedCondition - Return the condition opcode that would be the result
444 /// of exchanging the two operands of the setcc instruction without changing
445 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
447 BinaryOps getSwappedCondition() const {
448 return getSwappedCondition(getOpcode());
451 /// getSwappedCondition - Static version that you can use without an
452 /// instruction available.
454 static BinaryOps getSwappedCondition(BinaryOps Opcode);
456 /// isEquality - Return true if this comparison is an ==/!= comparison.
458 bool isEquality() const {
459 return getOpcode() == SetEQ || getOpcode() == SetNE;
462 /// isRelational - Return true if this comparison is a </>/<=/>= comparison.
464 bool isRelational() const {
465 return !isEquality();
468 // Methods for support type inquiry through isa, cast, and dyn_cast:
469 static inline bool classof(const SetCondInst *) { return true; }
470 static inline bool classof(const Instruction *I) {
471 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
472 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
473 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
475 static inline bool classof(const Value *V) {
476 return isa<Instruction>(V) && classof(cast<Instruction>(V));
480 //===----------------------------------------------------------------------===//
482 //===----------------------------------------------------------------------===//
484 /// CastInst - This class represents a cast from Operand[0] to the type of
485 /// the instruction (i->getType()).
487 class CastInst : public UnaryInstruction {
488 CastInst(const CastInst &CI)
489 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
492 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
493 Instruction *InsertBefore = 0)
494 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
496 CastInst(Value *S, const Type *Ty, const std::string &Name,
497 BasicBlock *InsertAtEnd)
498 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
501 /// isTruncIntCast - Return true if this is a truncating integer cast
502 /// instruction, e.g. a cast from long to uint.
503 bool isTruncIntCast() const;
506 virtual CastInst *clone() const;
508 // Methods for support type inquiry through isa, cast, and dyn_cast:
509 static inline bool classof(const CastInst *) { return true; }
510 static inline bool classof(const Instruction *I) {
511 return I->getOpcode() == Cast;
513 static inline bool classof(const Value *V) {
514 return isa<Instruction>(V) && classof(cast<Instruction>(V));
519 //===----------------------------------------------------------------------===//
521 //===----------------------------------------------------------------------===//
523 /// CallInst - This class represents a function call, abstracting a target
524 /// machine's calling convention. This class uses low bit of the SubClassData
525 /// field to indicate whether or not this is a tail call. The rest of the bits
526 /// hold the calling convention of the call.
528 class CallInst : public Instruction {
529 CallInst(const CallInst &CI);
530 void init(Value *Func, const std::vector<Value*> &Params);
531 void init(Value *Func, Value *Actual1, Value *Actual2);
532 void init(Value *Func, Value *Actual);
533 void init(Value *Func);
536 CallInst(Value *F, const std::vector<Value*> &Par,
537 const std::string &Name = "", Instruction *InsertBefore = 0);
538 CallInst(Value *F, const std::vector<Value*> &Par,
539 const std::string &Name, BasicBlock *InsertAtEnd);
541 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
542 // actuals, respectively.
543 CallInst(Value *F, Value *Actual1, Value *Actual2,
544 const std::string& Name = "", Instruction *InsertBefore = 0);
545 CallInst(Value *F, Value *Actual1, Value *Actual2,
546 const std::string& Name, BasicBlock *InsertAtEnd);
547 CallInst(Value *F, Value *Actual, const std::string& Name = "",
548 Instruction *InsertBefore = 0);
549 CallInst(Value *F, Value *Actual, const std::string& Name,
550 BasicBlock *InsertAtEnd);
551 explicit CallInst(Value *F, const std::string &Name = "",
552 Instruction *InsertBefore = 0);
553 explicit CallInst(Value *F, const std::string &Name,
554 BasicBlock *InsertAtEnd);
557 virtual CallInst *clone() const;
558 bool mayWriteToMemory() const { return true; }
560 bool isTailCall() const { return SubclassData & 1; }
561 void setTailCall(bool isTailCall = true) {
562 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
565 /// getCallingConv/setCallingConv - Get or set the calling convention of this
567 unsigned getCallingConv() const { return SubclassData >> 1; }
568 void setCallingConv(unsigned CC) {
569 SubclassData = (SubclassData & 1) | (CC << 1);
572 /// getCalledFunction - Return the function being called by this instruction
573 /// if it is a direct call. If it is a call through a function pointer,
575 Function *getCalledFunction() const {
576 return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
579 // getCalledValue - Get a pointer to a method that is invoked by this inst.
580 inline const Value *getCalledValue() const { return getOperand(0); }
581 inline Value *getCalledValue() { return getOperand(0); }
583 // Methods for support type inquiry through isa, cast, and dyn_cast:
584 static inline bool classof(const CallInst *) { return true; }
585 static inline bool classof(const Instruction *I) {
586 return I->getOpcode() == Instruction::Call;
588 static inline bool classof(const Value *V) {
589 return isa<Instruction>(V) && classof(cast<Instruction>(V));
594 //===----------------------------------------------------------------------===//
596 //===----------------------------------------------------------------------===//
598 /// ShiftInst - This class represents left and right shift instructions.
600 class ShiftInst : public Instruction {
602 ShiftInst(const ShiftInst &SI)
603 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
604 Ops[0].init(SI.Ops[0], this);
605 Ops[1].init(SI.Ops[1], this);
607 void init(OtherOps Opcode, Value *S, Value *SA) {
608 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
609 Ops[0].init(S, this);
610 Ops[1].init(SA, this);
614 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
615 Instruction *InsertBefore = 0)
616 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
619 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
620 BasicBlock *InsertAtEnd)
621 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
625 OtherOps getOpcode() const {
626 return static_cast<OtherOps>(Instruction::getOpcode());
629 /// Transparently provide more efficient getOperand methods.
630 Value *getOperand(unsigned i) const {
631 assert(i < 2 && "getOperand() out of range!");
634 void setOperand(unsigned i, Value *Val) {
635 assert(i < 2 && "setOperand() out of range!");
638 unsigned getNumOperands() const { return 2; }
640 /// isLogicalShift - Return true if this is a logical shift left or a logical
642 bool isLogicalShift() const;
644 /// isArithmeticShift - Return true if this is a sign-extending shift right
646 bool isArithmeticShift() const {
647 return !isLogicalShift();
651 virtual ShiftInst *clone() const;
653 // Methods for support type inquiry through isa, cast, and dyn_cast:
654 static inline bool classof(const ShiftInst *) { return true; }
655 static inline bool classof(const Instruction *I) {
656 return (I->getOpcode() == Instruction::Shr) |
657 (I->getOpcode() == Instruction::Shl);
659 static inline bool classof(const Value *V) {
660 return isa<Instruction>(V) && classof(cast<Instruction>(V));
664 //===----------------------------------------------------------------------===//
666 //===----------------------------------------------------------------------===//
668 /// SelectInst - This class represents the LLVM 'select' instruction.
670 class SelectInst : public Instruction {
673 void init(Value *C, Value *S1, Value *S2) {
674 Ops[0].init(C, this);
675 Ops[1].init(S1, this);
676 Ops[2].init(S2, this);
679 SelectInst(const SelectInst &SI)
680 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
681 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
684 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
685 Instruction *InsertBefore = 0)
686 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
687 Name, InsertBefore) {
690 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
691 BasicBlock *InsertAtEnd)
692 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
697 Value *getCondition() const { return Ops[0]; }
698 Value *getTrueValue() const { return Ops[1]; }
699 Value *getFalseValue() const { return Ops[2]; }
701 /// Transparently provide more efficient getOperand methods.
702 Value *getOperand(unsigned i) const {
703 assert(i < 3 && "getOperand() out of range!");
706 void setOperand(unsigned i, Value *Val) {
707 assert(i < 3 && "setOperand() out of range!");
710 unsigned getNumOperands() const { return 3; }
712 OtherOps getOpcode() const {
713 return static_cast<OtherOps>(Instruction::getOpcode());
716 virtual SelectInst *clone() const;
718 // Methods for support type inquiry through isa, cast, and dyn_cast:
719 static inline bool classof(const SelectInst *) { return true; }
720 static inline bool classof(const Instruction *I) {
721 return I->getOpcode() == Instruction::Select;
723 static inline bool classof(const Value *V) {
724 return isa<Instruction>(V) && classof(cast<Instruction>(V));
728 //===----------------------------------------------------------------------===//
730 //===----------------------------------------------------------------------===//
732 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
733 /// an argument of the specified type given a va_list and increments that list
735 class VAArgInst : public UnaryInstruction {
736 VAArgInst(const VAArgInst &VAA)
737 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
739 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
740 Instruction *InsertBefore = 0)
741 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
743 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
744 BasicBlock *InsertAtEnd)
745 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
748 virtual VAArgInst *clone() const;
749 bool mayWriteToMemory() const { return true; }
751 // Methods for support type inquiry through isa, cast, and dyn_cast:
752 static inline bool classof(const VAArgInst *) { return true; }
753 static inline bool classof(const Instruction *I) {
754 return I->getOpcode() == VAArg;
756 static inline bool classof(const Value *V) {
757 return isa<Instruction>(V) && classof(cast<Instruction>(V));
761 //===----------------------------------------------------------------------===//
762 // ExtractElementInst Class
763 //===----------------------------------------------------------------------===//
765 /// ExtractElementInst - This instruction extracts a single (scalar)
766 /// element from a PackedType value
768 class ExtractElementInst : public Instruction {
770 ExtractElementInst(const ExtractElementInst &EE) :
771 Instruction(EE.getType(), ExtractElement, Ops, 2) {
772 Ops[0].init(EE.Ops[0], this);
773 Ops[1].init(EE.Ops[1], this);
777 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name = "",
778 Instruction *InsertBefore = 0);
779 ExtractElementInst(Value *Vec, Value *Idx, const std::string &Name,
780 BasicBlock *InsertAtEnd);
782 /// isValidOperands - Return true if an extractelement instruction can be
783 /// formed with the specified operands.
784 static bool isValidOperands(const Value *Vec, const Value *Idx);
786 virtual ExtractElementInst *clone() const;
788 virtual bool mayWriteToMemory() const { return false; }
790 /// Transparently provide more efficient getOperand methods.
791 Value *getOperand(unsigned i) const {
792 assert(i < 2 && "getOperand() out of range!");
795 void setOperand(unsigned i, Value *Val) {
796 assert(i < 2 && "setOperand() out of range!");
799 unsigned getNumOperands() const { return 2; }
801 // Methods for support type inquiry through isa, cast, and dyn_cast:
802 static inline bool classof(const ExtractElementInst *) { return true; }
803 static inline bool classof(const Instruction *I) {
804 return I->getOpcode() == Instruction::ExtractElement;
806 static inline bool classof(const Value *V) {
807 return isa<Instruction>(V) && classof(cast<Instruction>(V));
811 //===----------------------------------------------------------------------===//
812 // InsertElementInst Class
813 //===----------------------------------------------------------------------===//
815 /// InsertElementInst - This instruction inserts a single (scalar)
816 /// element into a PackedType value
818 class InsertElementInst : public Instruction {
820 InsertElementInst(const InsertElementInst &IE);
822 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
823 const std::string &Name = "",Instruction *InsertBefore = 0);
824 InsertElementInst(Value *Vec, Value *NewElt, Value *Idx,
825 const std::string &Name, BasicBlock *InsertAtEnd);
827 /// isValidOperands - Return true if an insertelement instruction can be
828 /// formed with the specified operands.
829 static bool isValidOperands(const Value *Vec, const Value *NewElt,
832 virtual InsertElementInst *clone() const;
834 virtual bool mayWriteToMemory() const { return false; }
836 /// getType - Overload to return most specific packed type.
838 inline const PackedType *getType() const {
839 return reinterpret_cast<const PackedType*>(Instruction::getType());
842 /// Transparently provide more efficient getOperand methods.
843 Value *getOperand(unsigned i) const {
844 assert(i < 3 && "getOperand() out of range!");
847 void setOperand(unsigned i, Value *Val) {
848 assert(i < 3 && "setOperand() out of range!");
851 unsigned getNumOperands() const { return 3; }
853 // Methods for support type inquiry through isa, cast, and dyn_cast:
854 static inline bool classof(const InsertElementInst *) { return true; }
855 static inline bool classof(const Instruction *I) {
856 return I->getOpcode() == Instruction::InsertElement;
858 static inline bool classof(const Value *V) {
859 return isa<Instruction>(V) && classof(cast<Instruction>(V));
863 //===----------------------------------------------------------------------===//
864 // ShuffleVectorInst Class
865 //===----------------------------------------------------------------------===//
867 /// ShuffleVectorInst - This instruction constructs a fixed permutation of two
870 class ShuffleVectorInst : public Instruction {
872 ShuffleVectorInst(const ShuffleVectorInst &IE);
874 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
875 const std::string &Name = "", Instruction *InsertBefor = 0);
876 ShuffleVectorInst(Value *V1, Value *V2, Value *Mask,
877 const std::string &Name, BasicBlock *InsertAtEnd);
879 /// isValidOperands - Return true if a shufflevector instruction can be
880 /// formed with the specified operands.
881 static bool isValidOperands(const Value *V1, const Value *V2,
884 virtual ShuffleVectorInst *clone() const;
886 virtual bool mayWriteToMemory() const { return false; }
888 /// getType - Overload to return most specific packed type.
890 inline const PackedType *getType() const {
891 return reinterpret_cast<const PackedType*>(Instruction::getType());
894 /// Transparently provide more efficient getOperand methods.
895 Value *getOperand(unsigned i) const {
896 assert(i < 3 && "getOperand() out of range!");
899 void setOperand(unsigned i, Value *Val) {
900 assert(i < 3 && "setOperand() out of range!");
903 unsigned getNumOperands() const { return 3; }
905 // Methods for support type inquiry through isa, cast, and dyn_cast:
906 static inline bool classof(const ShuffleVectorInst *) { return true; }
907 static inline bool classof(const Instruction *I) {
908 return I->getOpcode() == Instruction::ShuffleVector;
910 static inline bool classof(const Value *V) {
911 return isa<Instruction>(V) && classof(cast<Instruction>(V));
916 //===----------------------------------------------------------------------===//
918 //===----------------------------------------------------------------------===//
920 // PHINode - The PHINode class is used to represent the magical mystical PHI
921 // node, that can not exist in nature, but can be synthesized in a computer
922 // scientist's overactive imagination.
924 class PHINode : public Instruction {
925 /// ReservedSpace - The number of operands actually allocated. NumOperands is
926 /// the number actually in use.
927 unsigned ReservedSpace;
928 PHINode(const PHINode &PN);
930 PHINode(const Type *Ty, const std::string &Name = "",
931 Instruction *InsertBefore = 0)
932 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
936 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
937 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
943 /// reserveOperandSpace - This method can be used to avoid repeated
944 /// reallocation of PHI operand lists by reserving space for the correct
945 /// number of operands before adding them. Unlike normal vector reserves,
946 /// this method can also be used to trim the operand space.
947 void reserveOperandSpace(unsigned NumValues) {
948 resizeOperands(NumValues*2);
951 virtual PHINode *clone() const;
953 /// getNumIncomingValues - Return the number of incoming edges
955 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
957 /// getIncomingValue - Return incoming value number x
959 Value *getIncomingValue(unsigned i) const {
960 assert(i*2 < getNumOperands() && "Invalid value number!");
961 return getOperand(i*2);
963 void setIncomingValue(unsigned i, Value *V) {
964 assert(i*2 < getNumOperands() && "Invalid value number!");
967 unsigned getOperandNumForIncomingValue(unsigned i) {
971 /// getIncomingBlock - Return incoming basic block number x
973 BasicBlock *getIncomingBlock(unsigned i) const {
974 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
976 void setIncomingBlock(unsigned i, BasicBlock *BB) {
977 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
979 unsigned getOperandNumForIncomingBlock(unsigned i) {
983 /// addIncoming - Add an incoming value to the end of the PHI list
985 void addIncoming(Value *V, BasicBlock *BB) {
986 assert(getType() == V->getType() &&
987 "All operands to PHI node must be the same type as the PHI node!");
988 unsigned OpNo = NumOperands;
989 if (OpNo+2 > ReservedSpace)
990 resizeOperands(0); // Get more space!
991 // Initialize some new operands.
992 NumOperands = OpNo+2;
993 OperandList[OpNo].init(V, this);
994 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
997 /// removeIncomingValue - Remove an incoming value. This is useful if a
998 /// predecessor basic block is deleted. The value removed is returned.
1000 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
1001 /// is true), the PHI node is destroyed and any uses of it are replaced with
1002 /// dummy values. The only time there should be zero incoming values to a PHI
1003 /// node is when the block is dead, so this strategy is sound.
1005 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
1007 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
1008 int Idx = getBasicBlockIndex(BB);
1009 assert(Idx >= 0 && "Invalid basic block argument to remove!");
1010 return removeIncomingValue(Idx, DeletePHIIfEmpty);
1013 /// getBasicBlockIndex - Return the first index of the specified basic
1014 /// block in the value list for this PHI. Returns -1 if no instance.
1016 int getBasicBlockIndex(const BasicBlock *BB) const {
1017 Use *OL = OperandList;
1018 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
1019 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
1023 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
1024 return getIncomingValue(getBasicBlockIndex(BB));
1027 /// hasConstantValue - If the specified PHI node always merges together the
1028 /// same value, return the value, otherwise return null.
1030 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
1032 /// Methods for support type inquiry through isa, cast, and dyn_cast:
1033 static inline bool classof(const PHINode *) { return true; }
1034 static inline bool classof(const Instruction *I) {
1035 return I->getOpcode() == Instruction::PHI;
1037 static inline bool classof(const Value *V) {
1038 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1041 void resizeOperands(unsigned NumOperands);
1044 //===----------------------------------------------------------------------===//
1046 //===----------------------------------------------------------------------===//
1048 //===---------------------------------------------------------------------------
1049 /// ReturnInst - Return a value (possibly void), from a function. Execution
1050 /// does not continue in this function any longer.
1052 class ReturnInst : public TerminatorInst {
1053 Use RetVal; // Possibly null retval.
1054 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
1055 RI.getNumOperands()) {
1056 if (RI.getNumOperands())
1057 RetVal.init(RI.RetVal, this);
1060 void init(Value *RetVal);
1063 // ReturnInst constructors:
1064 // ReturnInst() - 'ret void' instruction
1065 // ReturnInst( null) - 'ret void' instruction
1066 // ReturnInst(Value* X) - 'ret X' instruction
1067 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
1068 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
1069 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
1070 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
1072 // NOTE: If the Value* passed is of type void then the constructor behaves as
1073 // if it was passed NULL.
1074 ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
1075 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
1078 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
1079 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1082 ReturnInst(BasicBlock *InsertAtEnd)
1083 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
1086 virtual ReturnInst *clone() const;
1088 // Transparently provide more efficient getOperand methods.
1089 Value *getOperand(unsigned i) const {
1090 assert(i < getNumOperands() && "getOperand() out of range!");
1093 void setOperand(unsigned i, Value *Val) {
1094 assert(i < getNumOperands() && "setOperand() out of range!");
1098 Value *getReturnValue() const { return RetVal; }
1100 unsigned getNumSuccessors() const { return 0; }
1102 // Methods for support type inquiry through isa, cast, and dyn_cast:
1103 static inline bool classof(const ReturnInst *) { return true; }
1104 static inline bool classof(const Instruction *I) {
1105 return (I->getOpcode() == Instruction::Ret);
1107 static inline bool classof(const Value *V) {
1108 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1111 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1112 virtual unsigned getNumSuccessorsV() const;
1113 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1116 //===----------------------------------------------------------------------===//
1118 //===----------------------------------------------------------------------===//
1120 //===---------------------------------------------------------------------------
1121 /// BranchInst - Conditional or Unconditional Branch instruction.
1123 class BranchInst : public TerminatorInst {
1124 /// Ops list - Branches are strange. The operands are ordered:
1125 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
1126 /// they don't have to check for cond/uncond branchness.
1128 BranchInst(const BranchInst &BI);
1131 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
1132 // BranchInst(BB *B) - 'br B'
1133 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
1134 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
1135 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
1136 // BranchInst(BB* B, BB *I) - 'br B' insert at end
1137 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
1138 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
1139 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
1140 assert(IfTrue != 0 && "Branch destination may not be null!");
1141 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1143 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1144 Instruction *InsertBefore = 0)
1145 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
1146 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1147 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1148 Ops[2].init(Cond, this);
1154 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
1155 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
1156 assert(IfTrue != 0 && "Branch destination may not be null!");
1157 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1160 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
1161 BasicBlock *InsertAtEnd)
1162 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
1163 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
1164 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
1165 Ops[2].init(Cond, this);
1172 /// Transparently provide more efficient getOperand methods.
1173 Value *getOperand(unsigned i) const {
1174 assert(i < getNumOperands() && "getOperand() out of range!");
1177 void setOperand(unsigned i, Value *Val) {
1178 assert(i < getNumOperands() && "setOperand() out of range!");
1182 virtual BranchInst *clone() const;
1184 inline bool isUnconditional() const { return getNumOperands() == 1; }
1185 inline bool isConditional() const { return getNumOperands() == 3; }
1187 inline Value *getCondition() const {
1188 assert(isConditional() && "Cannot get condition of an uncond branch!");
1189 return getOperand(2);
1192 void setCondition(Value *V) {
1193 assert(isConditional() && "Cannot set condition of unconditional branch!");
1197 // setUnconditionalDest - Change the current branch to an unconditional branch
1198 // targeting the specified block.
1199 // FIXME: Eliminate this ugly method.
1200 void setUnconditionalDest(BasicBlock *Dest) {
1201 if (isConditional()) { // Convert this to an uncond branch.
1206 setOperand(0, reinterpret_cast<Value*>(Dest));
1209 unsigned getNumSuccessors() const { return 1+isConditional(); }
1211 BasicBlock *getSuccessor(unsigned i) const {
1212 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1213 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1214 cast<BasicBlock>(getOperand(1));
1217 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1218 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1219 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1222 // Methods for support type inquiry through isa, cast, and dyn_cast:
1223 static inline bool classof(const BranchInst *) { return true; }
1224 static inline bool classof(const Instruction *I) {
1225 return (I->getOpcode() == Instruction::Br);
1227 static inline bool classof(const Value *V) {
1228 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1231 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1232 virtual unsigned getNumSuccessorsV() const;
1233 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1236 //===----------------------------------------------------------------------===//
1238 //===----------------------------------------------------------------------===//
1240 //===---------------------------------------------------------------------------
1241 /// SwitchInst - Multiway switch
1243 class SwitchInst : public TerminatorInst {
1244 unsigned ReservedSpace;
1245 // Operand[0] = Value to switch on
1246 // Operand[1] = Default basic block destination
1247 // Operand[2n ] = Value to match
1248 // Operand[2n+1] = BasicBlock to go to on match
1249 SwitchInst(const SwitchInst &RI);
1250 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1251 void resizeOperands(unsigned No);
1253 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1254 /// switch on and a default destination. The number of additional cases can
1255 /// be specified here to make memory allocation more efficient. This
1256 /// constructor can also autoinsert before another instruction.
1257 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1258 Instruction *InsertBefore = 0)
1259 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1260 init(Value, Default, NumCases);
1263 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1264 /// switch on and a default destination. The number of additional cases can
1265 /// be specified here to make memory allocation more efficient. This
1266 /// constructor also autoinserts at the end of the specified BasicBlock.
1267 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1268 BasicBlock *InsertAtEnd)
1269 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1270 init(Value, Default, NumCases);
1275 // Accessor Methods for Switch stmt
1276 inline Value *getCondition() const { return getOperand(0); }
1277 void setCondition(Value *V) { setOperand(0, V); }
1279 inline BasicBlock *getDefaultDest() const {
1280 return cast<BasicBlock>(getOperand(1));
1283 /// getNumCases - return the number of 'cases' in this switch instruction.
1284 /// Note that case #0 is always the default case.
1285 unsigned getNumCases() const {
1286 return getNumOperands()/2;
1289 /// getCaseValue - Return the specified case value. Note that case #0, the
1290 /// default destination, does not have a case value.
1291 ConstantInt *getCaseValue(unsigned i) {
1292 assert(i && i < getNumCases() && "Illegal case value to get!");
1293 return getSuccessorValue(i);
1296 /// getCaseValue - Return the specified case value. Note that case #0, the
1297 /// default destination, does not have a case value.
1298 const ConstantInt *getCaseValue(unsigned i) const {
1299 assert(i && i < getNumCases() && "Illegal case value to get!");
1300 return getSuccessorValue(i);
1303 /// findCaseValue - Search all of the case values for the specified constant.
1304 /// If it is explicitly handled, return the case number of it, otherwise
1305 /// return 0 to indicate that it is handled by the default handler.
1306 unsigned findCaseValue(const ConstantInt *C) const {
1307 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1308 if (getCaseValue(i) == C)
1313 /// addCase - Add an entry to the switch instruction...
1315 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1317 /// removeCase - This method removes the specified successor from the switch
1318 /// instruction. Note that this cannot be used to remove the default
1319 /// destination (successor #0).
1321 void removeCase(unsigned idx);
1323 virtual SwitchInst *clone() const;
1325 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1326 BasicBlock *getSuccessor(unsigned idx) const {
1327 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1328 return cast<BasicBlock>(getOperand(idx*2+1));
1330 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1331 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1332 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1335 // getSuccessorValue - Return the value associated with the specified
1337 inline ConstantInt *getSuccessorValue(unsigned idx) const {
1338 assert(idx < getNumSuccessors() && "Successor # out of range!");
1339 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1342 // Methods for support type inquiry through isa, cast, and dyn_cast:
1343 static inline bool classof(const SwitchInst *) { return true; }
1344 static inline bool classof(const Instruction *I) {
1345 return I->getOpcode() == Instruction::Switch;
1347 static inline bool classof(const Value *V) {
1348 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1351 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1352 virtual unsigned getNumSuccessorsV() const;
1353 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1356 //===----------------------------------------------------------------------===//
1358 //===----------------------------------------------------------------------===//
1360 //===---------------------------------------------------------------------------
1362 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1363 /// calling convention of the call.
1365 class InvokeInst : public TerminatorInst {
1366 InvokeInst(const InvokeInst &BI);
1367 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1368 const std::vector<Value*> &Params);
1370 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1371 const std::vector<Value*> &Params, const std::string &Name = "",
1372 Instruction *InsertBefore = 0);
1373 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1374 const std::vector<Value*> &Params, const std::string &Name,
1375 BasicBlock *InsertAtEnd);
1378 virtual InvokeInst *clone() const;
1380 bool mayWriteToMemory() const { return true; }
1382 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1384 unsigned getCallingConv() const { return SubclassData; }
1385 void setCallingConv(unsigned CC) {
1389 /// getCalledFunction - Return the function called, or null if this is an
1390 /// indirect function invocation.
1392 Function *getCalledFunction() const {
1393 return dyn_cast<Function>(getOperand(0));
1396 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1397 inline Value *getCalledValue() const { return getOperand(0); }
1399 // get*Dest - Return the destination basic blocks...
1400 BasicBlock *getNormalDest() const {
1401 return cast<BasicBlock>(getOperand(1));
1403 BasicBlock *getUnwindDest() const {
1404 return cast<BasicBlock>(getOperand(2));
1406 void setNormalDest(BasicBlock *B) {
1407 setOperand(1, reinterpret_cast<Value*>(B));
1410 void setUnwindDest(BasicBlock *B) {
1411 setOperand(2, reinterpret_cast<Value*>(B));
1414 inline BasicBlock *getSuccessor(unsigned i) const {
1415 assert(i < 2 && "Successor # out of range for invoke!");
1416 return i == 0 ? getNormalDest() : getUnwindDest();
1419 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1420 assert(idx < 2 && "Successor # out of range for invoke!");
1421 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1424 unsigned getNumSuccessors() const { return 2; }
1426 // Methods for support type inquiry through isa, cast, and dyn_cast:
1427 static inline bool classof(const InvokeInst *) { return true; }
1428 static inline bool classof(const Instruction *I) {
1429 return (I->getOpcode() == Instruction::Invoke);
1431 static inline bool classof(const Value *V) {
1432 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1435 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1436 virtual unsigned getNumSuccessorsV() const;
1437 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1441 //===----------------------------------------------------------------------===//
1443 //===----------------------------------------------------------------------===//
1445 //===---------------------------------------------------------------------------
1446 /// UnwindInst - Immediately exit the current function, unwinding the stack
1447 /// until an invoke instruction is found.
1449 class UnwindInst : public TerminatorInst {
1451 UnwindInst(Instruction *InsertBefore = 0)
1452 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1454 UnwindInst(BasicBlock *InsertAtEnd)
1455 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1458 virtual UnwindInst *clone() const;
1460 unsigned getNumSuccessors() const { return 0; }
1462 // Methods for support type inquiry through isa, cast, and dyn_cast:
1463 static inline bool classof(const UnwindInst *) { return true; }
1464 static inline bool classof(const Instruction *I) {
1465 return I->getOpcode() == Instruction::Unwind;
1467 static inline bool classof(const Value *V) {
1468 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1471 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1472 virtual unsigned getNumSuccessorsV() const;
1473 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1476 //===----------------------------------------------------------------------===//
1477 // UnreachableInst Class
1478 //===----------------------------------------------------------------------===//
1480 //===---------------------------------------------------------------------------
1481 /// UnreachableInst - This function has undefined behavior. In particular, the
1482 /// presence of this instruction indicates some higher level knowledge that the
1483 /// end of the block cannot be reached.
1485 class UnreachableInst : public TerminatorInst {
1487 UnreachableInst(Instruction *InsertBefore = 0)
1488 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1490 UnreachableInst(BasicBlock *InsertAtEnd)
1491 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1494 virtual UnreachableInst *clone() const;
1496 unsigned getNumSuccessors() const { return 0; }
1498 // Methods for support type inquiry through isa, cast, and dyn_cast:
1499 static inline bool classof(const UnreachableInst *) { return true; }
1500 static inline bool classof(const Instruction *I) {
1501 return I->getOpcode() == Instruction::Unreachable;
1503 static inline bool classof(const Value *V) {
1504 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1507 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1508 virtual unsigned getNumSuccessorsV() const;
1509 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1512 } // End llvm namespace