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/Instruction.h"
20 #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 {
37 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
38 const std::string &Name = "", Instruction *InsertBefore = 0);
39 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
40 const std::string &Name, BasicBlock *InsertAtEnd);
44 /// isArrayAllocation - Return true if there is an allocation size parameter
45 /// to the allocation instruction that is not 1.
47 bool isArrayAllocation() const;
49 /// getArraySize - Get the number of element allocated, for a simple
50 /// allocation of a single element, this will return a constant 1 value.
52 inline const Value *getArraySize() const { return getOperand(0); }
53 inline Value *getArraySize() { return getOperand(0); }
55 /// getType - Overload to return most specific pointer type
57 inline const PointerType *getType() const {
58 return reinterpret_cast<const PointerType*>(Instruction::getType());
61 /// getAllocatedType - Return the type that is being allocated by the
64 const Type *getAllocatedType() const;
66 virtual Instruction *clone() const = 0;
68 // Methods for support type inquiry through isa, cast, and dyn_cast:
69 static inline bool classof(const AllocationInst *) { return true; }
70 static inline bool classof(const Instruction *I) {
71 return I->getOpcode() == Instruction::Alloca ||
72 I->getOpcode() == Instruction::Malloc;
74 static inline bool classof(const Value *V) {
75 return isa<Instruction>(V) && classof(cast<Instruction>(V));
80 //===----------------------------------------------------------------------===//
82 //===----------------------------------------------------------------------===//
84 /// MallocInst - an instruction to allocated memory on the heap
86 class MallocInst : public AllocationInst {
87 MallocInst(const MallocInst &MI);
89 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
90 const std::string &Name = "",
91 Instruction *InsertBefore = 0)
92 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
93 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
94 BasicBlock *InsertAtEnd)
95 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertAtEnd) {}
97 virtual MallocInst *clone() const;
99 // Methods for support type inquiry through isa, cast, and dyn_cast:
100 static inline bool classof(const MallocInst *) { return true; }
101 static inline bool classof(const Instruction *I) {
102 return (I->getOpcode() == Instruction::Malloc);
104 static inline bool classof(const Value *V) {
105 return isa<Instruction>(V) && classof(cast<Instruction>(V));
110 //===----------------------------------------------------------------------===//
112 //===----------------------------------------------------------------------===//
114 /// AllocaInst - an instruction to allocate memory on the stack
116 class AllocaInst : public AllocationInst {
117 AllocaInst(const AllocaInst &);
119 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
120 const std::string &Name = "",
121 Instruction *InsertBefore = 0)
122 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
123 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
124 BasicBlock *InsertAtEnd)
125 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {}
127 virtual AllocaInst *clone() const;
129 // Methods for support type inquiry through isa, cast, and dyn_cast:
130 static inline bool classof(const AllocaInst *) { return true; }
131 static inline bool classof(const Instruction *I) {
132 return (I->getOpcode() == Instruction::Alloca);
134 static inline bool classof(const Value *V) {
135 return isa<Instruction>(V) && classof(cast<Instruction>(V));
140 //===----------------------------------------------------------------------===//
142 //===----------------------------------------------------------------------===//
144 /// FreeInst - an instruction to deallocate memory
146 class FreeInst : public UnaryInstruction {
149 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
150 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
152 virtual FreeInst *clone() const;
154 virtual bool mayWriteToMemory() const { return true; }
156 // Methods for support type inquiry through isa, cast, and dyn_cast:
157 static inline bool classof(const FreeInst *) { return true; }
158 static inline bool classof(const Instruction *I) {
159 return (I->getOpcode() == Instruction::Free);
161 static inline bool classof(const Value *V) {
162 return isa<Instruction>(V) && classof(cast<Instruction>(V));
167 //===----------------------------------------------------------------------===//
169 //===----------------------------------------------------------------------===//
171 /// LoadInst - an instruction for reading from memory. This uses the
172 /// SubclassData field in Value to store whether or not the load is volatile.
174 class LoadInst : public UnaryInstruction {
175 LoadInst(const LoadInst &LI)
176 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
177 setVolatile(LI.isVolatile());
185 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
186 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
187 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
188 Instruction *InsertBefore = 0);
189 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
190 BasicBlock *InsertAtEnd);
192 /// isVolatile - Return true if this is a load from a volatile memory
195 bool isVolatile() const { return SubclassData; }
197 /// setVolatile - Specify whether this is a volatile load or not.
199 void setVolatile(bool V) { SubclassData = V; }
201 virtual LoadInst *clone() const;
203 virtual bool mayWriteToMemory() const { return isVolatile(); }
205 Value *getPointerOperand() { return getOperand(0); }
206 const Value *getPointerOperand() const { return getOperand(0); }
207 static unsigned getPointerOperandIndex() { return 0U; }
209 // Methods for support type inquiry through isa, cast, and dyn_cast:
210 static inline bool classof(const LoadInst *) { return true; }
211 static inline bool classof(const Instruction *I) {
212 return I->getOpcode() == Instruction::Load;
214 static inline bool classof(const Value *V) {
215 return isa<Instruction>(V) && classof(cast<Instruction>(V));
220 //===----------------------------------------------------------------------===//
222 //===----------------------------------------------------------------------===//
224 /// StoreInst - an instruction for storing to memory
226 class StoreInst : public Instruction {
228 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
229 Ops[0].init(SI.Ops[0], this);
230 Ops[1].init(SI.Ops[1], this);
231 setVolatile(SI.isVolatile());
238 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
239 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
240 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
241 Instruction *InsertBefore = 0);
242 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
245 /// isVolatile - Return true if this is a load from a volatile memory
248 bool isVolatile() const { return SubclassData; }
250 /// setVolatile - Specify whether this is a volatile load or not.
252 void setVolatile(bool V) { SubclassData = V; }
254 /// Transparently provide more efficient getOperand methods.
255 Value *getOperand(unsigned i) const {
256 assert(i < 2 && "getOperand() out of range!");
259 void setOperand(unsigned i, Value *Val) {
260 assert(i < 2 && "setOperand() out of range!");
263 unsigned getNumOperands() const { return 2; }
266 virtual StoreInst *clone() const;
268 virtual bool mayWriteToMemory() const { return true; }
270 Value *getPointerOperand() { return getOperand(1); }
271 const Value *getPointerOperand() const { return getOperand(1); }
272 static unsigned getPointerOperandIndex() { return 1U; }
274 // Methods for support type inquiry through isa, cast, and dyn_cast:
275 static inline bool classof(const StoreInst *) { return true; }
276 static inline bool classof(const Instruction *I) {
277 return I->getOpcode() == Instruction::Store;
279 static inline bool classof(const Value *V) {
280 return isa<Instruction>(V) && classof(cast<Instruction>(V));
285 //===----------------------------------------------------------------------===//
286 // GetElementPtrInst Class
287 //===----------------------------------------------------------------------===//
289 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
290 /// access elements of arrays and structs
292 class GetElementPtrInst : public Instruction {
293 GetElementPtrInst(const GetElementPtrInst &GEPI)
294 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
295 0, GEPI.getNumOperands()) {
296 Use *OL = OperandList = new Use[NumOperands];
297 Use *GEPIOL = GEPI.OperandList;
298 for (unsigned i = 0, E = NumOperands; i != E; ++i)
299 OL[i].init(GEPIOL[i], this);
301 void init(Value *Ptr, const std::vector<Value*> &Idx);
302 void init(Value *Ptr, Value *Idx0, Value *Idx1);
303 void init(Value *Ptr, Value *Idx);
305 /// Constructors - Create a getelementptr instruction with a base pointer an
306 /// list of indices. The first ctor can optionally insert before an existing
307 /// instruction, the second appends the new instruction to the specified
309 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
310 const std::string &Name = "", Instruction *InsertBefore =0);
311 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
312 const std::string &Name, BasicBlock *InsertAtEnd);
314 /// Constructors - These two constructors are convenience methods because one
315 /// and two index getelementptr instructions are so common.
316 GetElementPtrInst(Value *Ptr, Value *Idx,
317 const std::string &Name = "", Instruction *InsertBefore =0);
318 GetElementPtrInst(Value *Ptr, Value *Idx,
319 const std::string &Name, BasicBlock *InsertAtEnd);
320 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
321 const std::string &Name = "", Instruction *InsertBefore =0);
322 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
323 const std::string &Name, BasicBlock *InsertAtEnd);
324 ~GetElementPtrInst();
326 virtual GetElementPtrInst *clone() const;
328 // getType - Overload to return most specific pointer type...
329 inline const PointerType *getType() const {
330 return reinterpret_cast<const PointerType*>(Instruction::getType());
333 /// getIndexedType - Returns the type of the element that would be loaded with
334 /// a load instruction with the specified parameters.
336 /// A null type is returned if the indices are invalid for the specified
339 static const Type *getIndexedType(const Type *Ptr,
340 const std::vector<Value*> &Indices,
341 bool AllowStructLeaf = false);
342 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
343 bool AllowStructLeaf = false);
344 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
346 inline op_iterator idx_begin() { return op_begin()+1; }
347 inline const_op_iterator idx_begin() const { return op_begin()+1; }
348 inline op_iterator idx_end() { return op_end(); }
349 inline const_op_iterator idx_end() const { return op_end(); }
351 Value *getPointerOperand() {
352 return getOperand(0);
354 const Value *getPointerOperand() const {
355 return getOperand(0);
357 static unsigned getPointerOperandIndex() {
358 return 0U; // get index for modifying correct operand
361 inline unsigned getNumIndices() const { // Note: always non-negative
362 return getNumOperands() - 1;
365 inline bool hasIndices() const {
366 return getNumOperands() > 1;
369 // Methods for support type inquiry through isa, cast, and dyn_cast:
370 static inline bool classof(const GetElementPtrInst *) { return true; }
371 static inline bool classof(const Instruction *I) {
372 return (I->getOpcode() == Instruction::GetElementPtr);
374 static inline bool classof(const Value *V) {
375 return isa<Instruction>(V) && classof(cast<Instruction>(V));
379 //===----------------------------------------------------------------------===//
381 //===----------------------------------------------------------------------===//
383 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
386 class SetCondInst : public BinaryOperator {
388 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
389 const std::string &Name = "", Instruction *InsertBefore = 0);
390 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
391 const std::string &Name, BasicBlock *InsertAtEnd);
393 /// getInverseCondition - Return the inverse of the current condition opcode.
394 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
396 BinaryOps getInverseCondition() const {
397 return getInverseCondition(getOpcode());
400 /// getInverseCondition - Static version that you can use without an
401 /// instruction available.
403 static BinaryOps getInverseCondition(BinaryOps Opcode);
405 /// getSwappedCondition - Return the condition opcode that would be the result
406 /// of exchanging the two operands of the setcc instruction without changing
407 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
409 BinaryOps getSwappedCondition() const {
410 return getSwappedCondition(getOpcode());
413 /// getSwappedCondition - Static version that you can use without an
414 /// instruction available.
416 static BinaryOps getSwappedCondition(BinaryOps Opcode);
419 // Methods for support type inquiry through isa, cast, and dyn_cast:
420 static inline bool classof(const SetCondInst *) { return true; }
421 static inline bool classof(const Instruction *I) {
422 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
423 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
424 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
426 static inline bool classof(const Value *V) {
427 return isa<Instruction>(V) && classof(cast<Instruction>(V));
431 //===----------------------------------------------------------------------===//
433 //===----------------------------------------------------------------------===//
435 /// CastInst - This class represents a cast from Operand[0] to the type of
436 /// the instruction (i->getType()).
438 class CastInst : public UnaryInstruction {
439 CastInst(const CastInst &CI)
440 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
443 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
444 Instruction *InsertBefore = 0)
445 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
447 CastInst(Value *S, const Type *Ty, const std::string &Name,
448 BasicBlock *InsertAtEnd)
449 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
452 virtual CastInst *clone() const;
454 // Methods for support type inquiry through isa, cast, and dyn_cast:
455 static inline bool classof(const CastInst *) { return true; }
456 static inline bool classof(const Instruction *I) {
457 return I->getOpcode() == Cast;
459 static inline bool classof(const Value *V) {
460 return isa<Instruction>(V) && classof(cast<Instruction>(V));
465 //===----------------------------------------------------------------------===//
467 //===----------------------------------------------------------------------===//
469 /// CallInst - This class represents a function call, abstracting a target
470 /// machine's calling convention.
472 class CallInst : public Instruction {
473 CallInst(const CallInst &CI);
474 void init(Value *Func, const std::vector<Value*> &Params);
475 void init(Value *Func, Value *Actual1, Value *Actual2);
476 void init(Value *Func, Value *Actual);
477 void init(Value *Func);
480 CallInst(Value *F, const std::vector<Value*> &Par,
481 const std::string &Name = "", Instruction *InsertBefore = 0);
482 CallInst(Value *F, const std::vector<Value*> &Par,
483 const std::string &Name, BasicBlock *InsertAtEnd);
485 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
486 // actuals, respectively.
487 CallInst(Value *F, Value *Actual1, Value *Actual2,
488 const std::string& Name = "", Instruction *InsertBefore = 0);
489 CallInst(Value *F, Value *Actual1, Value *Actual2,
490 const std::string& Name, BasicBlock *InsertAtEnd);
491 CallInst(Value *F, Value *Actual, const std::string& Name = "",
492 Instruction *InsertBefore = 0);
493 CallInst(Value *F, Value *Actual, const std::string& Name,
494 BasicBlock *InsertAtEnd);
495 explicit CallInst(Value *F, const std::string &Name = "",
496 Instruction *InsertBefore = 0);
497 explicit CallInst(Value *F, const std::string &Name,
498 BasicBlock *InsertAtEnd);
501 virtual CallInst *clone() const;
502 bool mayWriteToMemory() const { return true; }
504 /// getCalledFunction - Return the function being called by this instruction
505 /// if it is a direct call. If it is a call through a function pointer,
507 Function *getCalledFunction() const {
508 return (Function*)dyn_cast<Function>(getOperand(0));
511 // getCalledValue - Get a pointer to a method that is invoked by this inst.
512 inline const Value *getCalledValue() const { return getOperand(0); }
513 inline Value *getCalledValue() { return getOperand(0); }
515 // Methods for support type inquiry through isa, cast, and dyn_cast:
516 static inline bool classof(const CallInst *) { return true; }
517 static inline bool classof(const Instruction *I) {
518 return I->getOpcode() == Instruction::Call;
520 static inline bool classof(const Value *V) {
521 return isa<Instruction>(V) && classof(cast<Instruction>(V));
526 //===----------------------------------------------------------------------===//
528 //===----------------------------------------------------------------------===//
530 /// ShiftInst - This class represents left and right shift instructions.
532 class ShiftInst : public Instruction {
534 ShiftInst(const ShiftInst &SI)
535 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
536 Ops[0].init(SI.Ops[0], this);
537 Ops[1].init(SI.Ops[1], this);
539 void init(OtherOps Opcode, Value *S, Value *SA) {
540 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
541 Ops[0].init(S, this);
542 Ops[1].init(SA, this);
546 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
547 Instruction *InsertBefore = 0)
548 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
551 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
552 BasicBlock *InsertAtEnd)
553 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
557 OtherOps getOpcode() const {
558 return static_cast<OtherOps>(Instruction::getOpcode());
561 /// Transparently provide more efficient getOperand methods.
562 Value *getOperand(unsigned i) const {
563 assert(i < 2 && "getOperand() out of range!");
566 void setOperand(unsigned i, Value *Val) {
567 assert(i < 2 && "setOperand() out of range!");
570 unsigned getNumOperands() const { return 2; }
572 virtual ShiftInst *clone() const;
574 // Methods for support type inquiry through isa, cast, and dyn_cast:
575 static inline bool classof(const ShiftInst *) { return true; }
576 static inline bool classof(const Instruction *I) {
577 return (I->getOpcode() == Instruction::Shr) |
578 (I->getOpcode() == Instruction::Shl);
580 static inline bool classof(const Value *V) {
581 return isa<Instruction>(V) && classof(cast<Instruction>(V));
585 //===----------------------------------------------------------------------===//
587 //===----------------------------------------------------------------------===//
589 /// SelectInst - This class represents the LLVM 'select' instruction.
591 class SelectInst : public Instruction {
594 void init(Value *C, Value *S1, Value *S2) {
595 Ops[0].init(C, this);
596 Ops[1].init(S1, this);
597 Ops[2].init(S2, this);
600 SelectInst(const SelectInst &SI)
601 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
602 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
605 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
606 Instruction *InsertBefore = 0)
607 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
608 Name, InsertBefore) {
611 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
612 BasicBlock *InsertAtEnd)
613 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
618 Value *getCondition() const { return Ops[0]; }
619 Value *getTrueValue() const { return Ops[1]; }
620 Value *getFalseValue() const { return Ops[2]; }
622 /// Transparently provide more efficient getOperand methods.
623 Value *getOperand(unsigned i) const {
624 assert(i < 3 && "getOperand() out of range!");
627 void setOperand(unsigned i, Value *Val) {
628 assert(i < 3 && "setOperand() out of range!");
631 unsigned getNumOperands() const { return 3; }
633 OtherOps getOpcode() const {
634 return static_cast<OtherOps>(Instruction::getOpcode());
637 virtual SelectInst *clone() const;
639 // Methods for support type inquiry through isa, cast, and dyn_cast:
640 static inline bool classof(const SelectInst *) { return true; }
641 static inline bool classof(const Instruction *I) {
642 return I->getOpcode() == Instruction::Select;
644 static inline bool classof(const Value *V) {
645 return isa<Instruction>(V) && classof(cast<Instruction>(V));
650 //===----------------------------------------------------------------------===//
652 //===----------------------------------------------------------------------===//
654 /// VANextInst - This class represents the va_next llvm instruction, which
655 /// advances a vararg list passed an argument of the specified type, returning
656 /// the resultant list.
658 class VANextInst : public UnaryInstruction {
660 VANextInst(const VANextInst &VAN)
661 : UnaryInstruction(VAN.getType(), VANext, VAN.getOperand(0)),
662 ArgTy(VAN.getArgType()) {
666 VANextInst(Value *List, const Type *Ty, const std::string &Name = "",
667 Instruction *InsertBefore = 0)
668 : UnaryInstruction(List->getType(), VANext, List, Name, InsertBefore),
671 VANextInst(Value *List, const Type *Ty, const std::string &Name,
672 BasicBlock *InsertAtEnd)
673 : UnaryInstruction(List->getType(), VANext, List, Name, InsertAtEnd),
677 const Type *getArgType() const { return ArgTy; }
679 virtual VANextInst *clone() const;
681 // Methods for support type inquiry through isa, cast, and dyn_cast:
682 static inline bool classof(const VANextInst *) { return true; }
683 static inline bool classof(const Instruction *I) {
684 return I->getOpcode() == VANext;
686 static inline bool classof(const Value *V) {
687 return isa<Instruction>(V) && classof(cast<Instruction>(V));
692 //===----------------------------------------------------------------------===//
694 //===----------------------------------------------------------------------===//
696 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
697 /// an argument of the specified type given a va_list.
699 class VAArgInst : public UnaryInstruction {
700 VAArgInst(const VAArgInst &VAA)
701 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
703 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
704 Instruction *InsertBefore = 0)
705 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
707 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
708 BasicBlock *InsertAtEnd)
709 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
712 virtual VAArgInst *clone() const;
714 // Methods for support type inquiry through isa, cast, and dyn_cast:
715 static inline bool classof(const VAArgInst *) { return true; }
716 static inline bool classof(const Instruction *I) {
717 return I->getOpcode() == VAArg;
719 static inline bool classof(const Value *V) {
720 return isa<Instruction>(V) && classof(cast<Instruction>(V));
724 //===----------------------------------------------------------------------===//
726 //===----------------------------------------------------------------------===//
728 // PHINode - The PHINode class is used to represent the magical mystical PHI
729 // node, that can not exist in nature, but can be synthesized in a computer
730 // scientist's overactive imagination.
732 class PHINode : public Instruction {
733 /// ReservedSpace - The number of operands actually allocated. NumOperands is
734 /// the number actually in use.
735 unsigned ReservedSpace;
736 PHINode(const PHINode &PN);
738 PHINode(const Type *Ty, const std::string &Name = "",
739 Instruction *InsertBefore = 0)
740 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
744 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
745 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
751 /// reserveOperandSpace - This method can be used to avoid repeated
752 /// reallocation of PHI operand lists by reserving space for the correct
753 /// number of operands before adding them. Unlike normal vector reserves,
754 /// this method can also be used to trim the operand space.
755 void reserveOperandSpace(unsigned NumValues) {
756 resizeOperands(NumValues*2);
759 virtual PHINode *clone() const;
761 /// getNumIncomingValues - Return the number of incoming edges
763 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
765 /// getIncomingValue - Return incoming value #x
767 Value *getIncomingValue(unsigned i) const {
768 assert(i*2 < getNumOperands() && "Invalid value number!");
769 return getOperand(i*2);
771 void setIncomingValue(unsigned i, Value *V) {
772 assert(i*2 < getNumOperands() && "Invalid value number!");
775 unsigned getOperandNumForIncomingValue(unsigned i) {
779 /// getIncomingBlock - Return incoming basic block #x
781 BasicBlock *getIncomingBlock(unsigned i) const {
782 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
784 void setIncomingBlock(unsigned i, BasicBlock *BB) {
785 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
787 unsigned getOperandNumForIncomingBlock(unsigned i) {
791 /// addIncoming - Add an incoming value to the end of the PHI list
793 void addIncoming(Value *V, BasicBlock *BB) {
794 assert(getType() == V->getType() &&
795 "All operands to PHI node must be the same type as the PHI node!");
796 unsigned OpNo = NumOperands;
797 if (OpNo+2 > ReservedSpace)
798 resizeOperands(0); // Get more space!
799 // Initialize some new operands.
800 NumOperands = OpNo+2;
801 OperandList[OpNo].init(V, this);
802 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
805 /// removeIncomingValue - Remove an incoming value. This is useful if a
806 /// predecessor basic block is deleted. The value removed is returned.
808 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
809 /// is true), the PHI node is destroyed and any uses of it are replaced with
810 /// dummy values. The only time there should be zero incoming values to a PHI
811 /// node is when the block is dead, so this strategy is sound.
813 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
815 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
816 int Idx = getBasicBlockIndex(BB);
817 assert(Idx >= 0 && "Invalid basic block argument to remove!");
818 return removeIncomingValue(Idx, DeletePHIIfEmpty);
821 /// getBasicBlockIndex - Return the first index of the specified basic
822 /// block in the value list for this PHI. Returns -1 if no instance.
824 int getBasicBlockIndex(const BasicBlock *BB) const {
825 Use *OL = OperandList;
826 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
827 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
831 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
832 return getIncomingValue(getBasicBlockIndex(BB));
835 /// Methods for support type inquiry through isa, cast, and dyn_cast:
836 static inline bool classof(const PHINode *) { return true; }
837 static inline bool classof(const Instruction *I) {
838 return I->getOpcode() == Instruction::PHI;
840 static inline bool classof(const Value *V) {
841 return isa<Instruction>(V) && classof(cast<Instruction>(V));
844 void resizeOperands(unsigned NumOperands);
847 //===----------------------------------------------------------------------===//
849 //===----------------------------------------------------------------------===//
851 //===---------------------------------------------------------------------------
852 /// ReturnInst - Return a value (possibly void), from a function. Execution
853 /// does not continue in this function any longer.
855 class ReturnInst : public TerminatorInst {
856 Use RetVal; // Possibly null retval.
857 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
858 RI.getNumOperands()) {
859 if (RI.getNumOperands())
860 RetVal.init(RI.RetVal, this);
863 void init(Value *RetVal);
866 // ReturnInst constructors:
867 // ReturnInst() - 'ret void' instruction
868 // ReturnInst( null) - 'ret void' instruction
869 // ReturnInst(Value* X) - 'ret X' instruction
870 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
871 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
872 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
873 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
875 // NOTE: If the Value* passed is of type void then the constructor behaves as
876 // if it was passed NULL.
877 ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
878 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
881 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
882 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
885 ReturnInst(BasicBlock *InsertAtEnd)
886 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
889 virtual ReturnInst *clone() const;
891 // Transparently provide more efficient getOperand methods.
892 Value *getOperand(unsigned i) const {
893 assert(i < getNumOperands() && "getOperand() out of range!");
896 void setOperand(unsigned i, Value *Val) {
897 assert(i < getNumOperands() && "setOperand() out of range!");
901 Value *getReturnValue() const { return RetVal; }
903 unsigned getNumSuccessors() const { return 0; }
905 // Methods for support type inquiry through isa, cast, and dyn_cast:
906 static inline bool classof(const ReturnInst *) { return true; }
907 static inline bool classof(const Instruction *I) {
908 return (I->getOpcode() == Instruction::Ret);
910 static inline bool classof(const Value *V) {
911 return isa<Instruction>(V) && classof(cast<Instruction>(V));
914 virtual BasicBlock *getSuccessorV(unsigned idx) const;
915 virtual unsigned getNumSuccessorsV() const;
916 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
919 //===----------------------------------------------------------------------===//
921 //===----------------------------------------------------------------------===//
923 //===---------------------------------------------------------------------------
924 /// BranchInst - Conditional or Unconditional Branch instruction.
926 class BranchInst : public TerminatorInst {
927 /// Ops list - Branches are strange. The operands are ordered:
928 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
929 /// they don't have to check for cond/uncond branchness.
931 BranchInst(const BranchInst &BI);
934 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
935 // BranchInst(BB *B) - 'br B'
936 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
937 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
938 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
939 // BranchInst(BB* B, BB *I) - 'br B' insert at end
940 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
941 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
942 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
943 assert(IfTrue != 0 && "Branch destination may not be null!");
944 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
946 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
947 Instruction *InsertBefore = 0)
948 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
949 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
950 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
951 Ops[2].init(Cond, this);
957 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
958 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
959 assert(IfTrue != 0 && "Branch destination may not be null!");
960 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
963 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
964 BasicBlock *InsertAtEnd)
965 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
966 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
967 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
968 Ops[2].init(Cond, this);
975 /// Transparently provide more efficient getOperand methods.
976 Value *getOperand(unsigned i) const {
977 assert(i < getNumOperands() && "getOperand() out of range!");
980 void setOperand(unsigned i, Value *Val) {
981 assert(i < getNumOperands() && "setOperand() out of range!");
985 virtual BranchInst *clone() const;
987 inline bool isUnconditional() const { return getNumOperands() == 1; }
988 inline bool isConditional() const { return getNumOperands() == 3; }
990 inline Value *getCondition() const {
991 assert(isConditional() && "Cannot get condition of an uncond branch!");
992 return getOperand(2);
995 void setCondition(Value *V) {
996 assert(isConditional() && "Cannot set condition of unconditional branch!");
1000 // setUnconditionalDest - Change the current branch to an unconditional branch
1001 // targeting the specified block.
1002 // FIXME: Eliminate this ugly method.
1003 void setUnconditionalDest(BasicBlock *Dest) {
1004 if (isConditional()) { // Convert this to an uncond branch.
1009 setOperand(0, reinterpret_cast<Value*>(Dest));
1012 unsigned getNumSuccessors() const { return 1+isConditional(); }
1014 BasicBlock *getSuccessor(unsigned i) const {
1015 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1016 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1017 cast<BasicBlock>(getOperand(1));
1020 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1021 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1022 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1025 // Methods for support type inquiry through isa, cast, and dyn_cast:
1026 static inline bool classof(const BranchInst *) { return true; }
1027 static inline bool classof(const Instruction *I) {
1028 return (I->getOpcode() == Instruction::Br);
1030 static inline bool classof(const Value *V) {
1031 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1034 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1035 virtual unsigned getNumSuccessorsV() const;
1036 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1039 //===----------------------------------------------------------------------===//
1041 //===----------------------------------------------------------------------===//
1043 //===---------------------------------------------------------------------------
1044 /// SwitchInst - Multiway switch
1046 class SwitchInst : public TerminatorInst {
1047 unsigned ReservedSpace;
1048 // Operand[0] = Value to switch on
1049 // Operand[1] = Default basic block destination
1050 // Operand[2n ] = Value to match
1051 // Operand[2n+1] = BasicBlock to go to on match
1052 SwitchInst(const SwitchInst &RI);
1053 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1054 void resizeOperands(unsigned No);
1056 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1057 /// switch on and a default destination. The number of additional cases can
1058 /// be specified here to make memory allocation more efficient. This
1059 /// constructor can also autoinsert before another instruction.
1060 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1061 Instruction *InsertBefore = 0)
1062 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1063 init(Value, Default, NumCases);
1066 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1067 /// switch on and a default destination. The number of additional cases can
1068 /// be specified here to make memory allocation more efficient. This
1069 /// constructor also autoinserts at the end of the specified BasicBlock.
1070 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1071 BasicBlock *InsertAtEnd)
1072 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1073 init(Value, Default, NumCases);
1078 // Accessor Methods for Switch stmt
1079 inline Value *getCondition() const { return getOperand(0); }
1080 void setCondition(Value *V) { setOperand(0, V); }
1082 inline BasicBlock *getDefaultDest() const {
1083 return cast<BasicBlock>(getOperand(1));
1086 /// getNumCases - return the number of 'cases' in this switch instruction.
1087 /// Note that case #0 is always the default case.
1088 unsigned getNumCases() const {
1089 return getNumOperands()/2;
1092 /// getCaseValue - Return the specified case value. Note that case #0, the
1093 /// default destination, does not have a case value.
1094 ConstantInt *getCaseValue(unsigned i) {
1095 assert(i && i < getNumCases() && "Illegal case value to get!");
1096 return getSuccessorValue(i);
1099 /// getCaseValue - Return the specified case value. Note that case #0, the
1100 /// default destination, does not have a case value.
1101 const ConstantInt *getCaseValue(unsigned i) const {
1102 assert(i && i < getNumCases() && "Illegal case value to get!");
1103 return getSuccessorValue(i);
1106 /// findCaseValue - Search all of the case values for the specified constant.
1107 /// If it is explicitly handled, return the case number of it, otherwise
1108 /// return 0 to indicate that it is handled by the default handler.
1109 unsigned findCaseValue(const ConstantInt *C) const {
1110 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1111 if (getCaseValue(i) == C)
1116 /// addCase - Add an entry to the switch instruction...
1118 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1120 /// removeCase - This method removes the specified successor from the switch
1121 /// instruction. Note that this cannot be used to remove the default
1122 /// destination (successor #0).
1124 void removeCase(unsigned idx);
1126 virtual SwitchInst *clone() const;
1128 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1129 BasicBlock *getSuccessor(unsigned idx) const {
1130 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1131 return cast<BasicBlock>(getOperand(idx*2+1));
1133 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1134 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1135 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1138 // getSuccessorValue - Return the value associated with the specified
1140 inline ConstantInt *getSuccessorValue(unsigned idx) const {
1141 assert(idx < getNumSuccessors() && "Successor # out of range!");
1142 return (ConstantInt*)getOperand(idx*2);
1145 // Methods for support type inquiry through isa, cast, and dyn_cast:
1146 static inline bool classof(const SwitchInst *) { return true; }
1147 static inline bool classof(const Instruction *I) {
1148 return I->getOpcode() == Instruction::Switch;
1150 static inline bool classof(const Value *V) {
1151 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1154 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1155 virtual unsigned getNumSuccessorsV() const;
1156 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1159 //===----------------------------------------------------------------------===//
1161 //===----------------------------------------------------------------------===//
1163 //===---------------------------------------------------------------------------
1164 /// InvokeInst - Invoke instruction
1166 class InvokeInst : public TerminatorInst {
1167 InvokeInst(const InvokeInst &BI);
1168 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1169 const std::vector<Value*> &Params);
1171 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1172 const std::vector<Value*> &Params, const std::string &Name = "",
1173 Instruction *InsertBefore = 0);
1174 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1175 const std::vector<Value*> &Params, const std::string &Name,
1176 BasicBlock *InsertAtEnd);
1179 virtual InvokeInst *clone() const;
1181 bool mayWriteToMemory() const { return true; }
1183 /// getCalledFunction - Return the function called, or null if this is an
1184 /// indirect function invocation.
1186 Function *getCalledFunction() const {
1187 return dyn_cast<Function>(getOperand(0));
1190 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1191 inline Value *getCalledValue() const { return getOperand(0); }
1193 // get*Dest - Return the destination basic blocks...
1194 BasicBlock *getNormalDest() const {
1195 return cast<BasicBlock>(getOperand(1));
1197 BasicBlock *getUnwindDest() const {
1198 return cast<BasicBlock>(getOperand(2));
1200 void setNormalDest(BasicBlock *B) {
1201 setOperand(1, reinterpret_cast<Value*>(B));
1204 void setUnwindDest(BasicBlock *B) {
1205 setOperand(2, reinterpret_cast<Value*>(B));
1208 inline BasicBlock *getSuccessor(unsigned i) const {
1209 assert(i < 2 && "Successor # out of range for invoke!");
1210 return i == 0 ? getNormalDest() : getUnwindDest();
1213 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1214 assert(idx < 2 && "Successor # out of range for invoke!");
1215 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1218 unsigned getNumSuccessors() const { return 2; }
1220 // Methods for support type inquiry through isa, cast, and dyn_cast:
1221 static inline bool classof(const InvokeInst *) { return true; }
1222 static inline bool classof(const Instruction *I) {
1223 return (I->getOpcode() == Instruction::Invoke);
1225 static inline bool classof(const Value *V) {
1226 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1229 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1230 virtual unsigned getNumSuccessorsV() const;
1231 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1235 //===----------------------------------------------------------------------===//
1237 //===----------------------------------------------------------------------===//
1239 //===---------------------------------------------------------------------------
1240 /// UnwindInst - Immediately exit the current function, unwinding the stack
1241 /// until an invoke instruction is found.
1243 class UnwindInst : public TerminatorInst {
1245 UnwindInst(Instruction *InsertBefore = 0)
1246 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1248 UnwindInst(BasicBlock *InsertAtEnd)
1249 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1252 virtual UnwindInst *clone() const;
1254 unsigned getNumSuccessors() const { return 0; }
1256 // Methods for support type inquiry through isa, cast, and dyn_cast:
1257 static inline bool classof(const UnwindInst *) { return true; }
1258 static inline bool classof(const Instruction *I) {
1259 return I->getOpcode() == Instruction::Unwind;
1261 static inline bool classof(const Value *V) {
1262 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1265 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1266 virtual unsigned getNumSuccessorsV() const;
1267 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1270 //===----------------------------------------------------------------------===//
1271 // UnreachableInst Class
1272 //===----------------------------------------------------------------------===//
1274 //===---------------------------------------------------------------------------
1275 /// UnreachableInst - This function has undefined behavior. In particular, the
1276 /// presence of this instruction indicates some higher level knowledge that the
1277 /// end of the block cannot be reached.
1279 class UnreachableInst : public TerminatorInst {
1281 UnreachableInst(Instruction *InsertBefore = 0)
1282 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1284 UnreachableInst(BasicBlock *InsertAtEnd)
1285 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1288 virtual UnreachableInst *clone() const;
1290 unsigned getNumSuccessors() const { return 0; }
1292 // Methods for support type inquiry through isa, cast, and dyn_cast:
1293 static inline bool classof(const UnreachableInst *) { return true; }
1294 static inline bool classof(const Instruction *I) {
1295 return I->getOpcode() == Instruction::Unreachable;
1297 static inline bool classof(const Value *V) {
1298 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1301 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1302 virtual unsigned getNumSuccessorsV() const;
1303 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1306 } // End llvm namespace