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"
27 //===----------------------------------------------------------------------===//
28 // AllocationInst Class
29 //===----------------------------------------------------------------------===//
31 /// AllocationInst - This class is the common base class of MallocInst and
34 class AllocationInst : public UnaryInstruction {
36 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
37 const std::string &Name = "", Instruction *InsertBefore = 0);
38 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
39 const std::string &Name, BasicBlock *InsertAtEnd);
43 /// isArrayAllocation - Return true if there is an allocation size parameter
44 /// to the allocation instruction that is not 1.
46 bool isArrayAllocation() const;
48 /// getArraySize - Get the number of element allocated, for a simple
49 /// allocation of a single element, this will return a constant 1 value.
51 inline const Value *getArraySize() const { return getOperand(0); }
52 inline Value *getArraySize() { return getOperand(0); }
54 /// getType - Overload to return most specific pointer type
56 inline const PointerType *getType() const {
57 return reinterpret_cast<const PointerType*>(Instruction::getType());
60 /// getAllocatedType - Return the type that is being allocated by the
63 const Type *getAllocatedType() const;
65 virtual Instruction *clone() const = 0;
67 // Methods for support type inquiry through isa, cast, and dyn_cast:
68 static inline bool classof(const AllocationInst *) { return true; }
69 static inline bool classof(const Instruction *I) {
70 return I->getOpcode() == Instruction::Alloca ||
71 I->getOpcode() == Instruction::Malloc;
73 static inline bool classof(const Value *V) {
74 return isa<Instruction>(V) && classof(cast<Instruction>(V));
79 //===----------------------------------------------------------------------===//
81 //===----------------------------------------------------------------------===//
83 /// MallocInst - an instruction to allocated memory on the heap
85 class MallocInst : public AllocationInst {
86 MallocInst(const MallocInst &MI);
88 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
89 const std::string &Name = "",
90 Instruction *InsertBefore = 0)
91 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertBefore) {}
92 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
93 BasicBlock *InsertAtEnd)
94 : AllocationInst(Ty, ArraySize, Malloc, Name, InsertAtEnd) {}
96 virtual MallocInst *clone() const;
98 // Methods for support type inquiry through isa, cast, and dyn_cast:
99 static inline bool classof(const MallocInst *) { return true; }
100 static inline bool classof(const Instruction *I) {
101 return (I->getOpcode() == Instruction::Malloc);
103 static inline bool classof(const Value *V) {
104 return isa<Instruction>(V) && classof(cast<Instruction>(V));
109 //===----------------------------------------------------------------------===//
111 //===----------------------------------------------------------------------===//
113 /// AllocaInst - an instruction to allocate memory on the stack
115 class AllocaInst : public AllocationInst {
116 AllocaInst(const AllocaInst &);
118 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
119 const std::string &Name = "",
120 Instruction *InsertBefore = 0)
121 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
122 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
123 BasicBlock *InsertAtEnd)
124 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {}
126 virtual AllocaInst *clone() const;
128 // Methods for support type inquiry through isa, cast, and dyn_cast:
129 static inline bool classof(const AllocaInst *) { return true; }
130 static inline bool classof(const Instruction *I) {
131 return (I->getOpcode() == Instruction::Alloca);
133 static inline bool classof(const Value *V) {
134 return isa<Instruction>(V) && classof(cast<Instruction>(V));
139 //===----------------------------------------------------------------------===//
141 //===----------------------------------------------------------------------===//
143 /// FreeInst - an instruction to deallocate memory
145 class FreeInst : public UnaryInstruction {
148 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
149 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
151 virtual FreeInst *clone() const;
153 virtual bool mayWriteToMemory() const { return true; }
155 // Methods for support type inquiry through isa, cast, and dyn_cast:
156 static inline bool classof(const FreeInst *) { return true; }
157 static inline bool classof(const Instruction *I) {
158 return (I->getOpcode() == Instruction::Free);
160 static inline bool classof(const Value *V) {
161 return isa<Instruction>(V) && classof(cast<Instruction>(V));
166 //===----------------------------------------------------------------------===//
168 //===----------------------------------------------------------------------===//
170 /// LoadInst - an instruction for reading from memory. This uses the
171 /// SubclassData field in Value to store whether or not the load is volatile.
173 class LoadInst : public UnaryInstruction {
174 LoadInst(const LoadInst &LI)
175 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
176 setVolatile(LI.isVolatile());
184 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
185 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
186 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
187 Instruction *InsertBefore = 0);
188 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
189 BasicBlock *InsertAtEnd);
191 /// isVolatile - Return true if this is a load from a volatile memory
194 bool isVolatile() const { return SubclassData; }
196 /// setVolatile - Specify whether this is a volatile load or not.
198 void setVolatile(bool V) { SubclassData = V; }
200 virtual LoadInst *clone() const;
202 virtual bool mayWriteToMemory() const { return isVolatile(); }
204 Value *getPointerOperand() { return getOperand(0); }
205 const Value *getPointerOperand() const { return getOperand(0); }
206 static unsigned getPointerOperandIndex() { return 0U; }
208 // Methods for support type inquiry through isa, cast, and dyn_cast:
209 static inline bool classof(const LoadInst *) { return true; }
210 static inline bool classof(const Instruction *I) {
211 return I->getOpcode() == Instruction::Load;
213 static inline bool classof(const Value *V) {
214 return isa<Instruction>(V) && classof(cast<Instruction>(V));
219 //===----------------------------------------------------------------------===//
221 //===----------------------------------------------------------------------===//
223 /// StoreInst - an instruction for storing to memory
225 class StoreInst : public Instruction {
227 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
228 Ops[0].init(SI.Ops[0], this);
229 Ops[1].init(SI.Ops[1], this);
230 setVolatile(SI.isVolatile());
237 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
238 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
239 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
240 Instruction *InsertBefore = 0);
241 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
244 /// isVolatile - Return true if this is a load from a volatile memory
247 bool isVolatile() const { return SubclassData; }
249 /// setVolatile - Specify whether this is a volatile load or not.
251 void setVolatile(bool V) { SubclassData = V; }
253 /// Transparently provide more efficient getOperand methods.
254 Value *getOperand(unsigned i) const {
255 assert(i < 2 && "getOperand() out of range!");
258 void setOperand(unsigned i, Value *Val) {
259 assert(i < 2 && "setOperand() out of range!");
262 unsigned getNumOperands() const { return 2; }
265 virtual StoreInst *clone() const;
267 virtual bool mayWriteToMemory() const { return true; }
269 Value *getPointerOperand() { return getOperand(1); }
270 const Value *getPointerOperand() const { return getOperand(1); }
271 static unsigned getPointerOperandIndex() { return 1U; }
273 // Methods for support type inquiry through isa, cast, and dyn_cast:
274 static inline bool classof(const StoreInst *) { return true; }
275 static inline bool classof(const Instruction *I) {
276 return I->getOpcode() == Instruction::Store;
278 static inline bool classof(const Value *V) {
279 return isa<Instruction>(V) && classof(cast<Instruction>(V));
284 //===----------------------------------------------------------------------===//
285 // GetElementPtrInst Class
286 //===----------------------------------------------------------------------===//
288 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
289 /// access elements of arrays and structs
291 class GetElementPtrInst : public Instruction {
292 GetElementPtrInst(const GetElementPtrInst &GEPI)
293 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
294 0, GEPI.getNumOperands()) {
295 Use *OL = OperandList = new Use[NumOperands];
296 Use *GEPIOL = GEPI.OperandList;
297 for (unsigned i = 0, E = NumOperands; i != E; ++i)
298 OL[i].init(GEPIOL[i], this);
300 void init(Value *Ptr, const std::vector<Value*> &Idx);
301 void init(Value *Ptr, Value *Idx0, Value *Idx1);
303 /// Constructors - Create a getelementptr instruction with a base pointer an
304 /// list of indices. The first ctor can optionally insert before an existing
305 /// instruction, the second appends the new instruction to the specified
307 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
308 const std::string &Name = "", Instruction *InsertBefore =0);
309 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
310 const std::string &Name, BasicBlock *InsertAtEnd);
312 /// Constructors - These two constructors are convenience methods because two
313 /// index getelementptr instructions are so common.
314 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
315 const std::string &Name = "", Instruction *InsertBefore =0);
316 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
317 const std::string &Name, BasicBlock *InsertAtEnd);
318 ~GetElementPtrInst();
320 virtual GetElementPtrInst *clone() const;
322 // getType - Overload to return most specific pointer type...
323 inline const PointerType *getType() const {
324 return reinterpret_cast<const PointerType*>(Instruction::getType());
327 /// getIndexedType - Returns the type of the element that would be loaded with
328 /// a load instruction with the specified parameters.
330 /// A null type is returned if the indices are invalid for the specified
333 static const Type *getIndexedType(const Type *Ptr,
334 const std::vector<Value*> &Indices,
335 bool AllowStructLeaf = false);
336 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
337 bool AllowStructLeaf = false);
339 inline op_iterator idx_begin() { return op_begin()+1; }
340 inline const_op_iterator idx_begin() const { return op_begin()+1; }
341 inline op_iterator idx_end() { return op_end(); }
342 inline const_op_iterator idx_end() const { return op_end(); }
344 Value *getPointerOperand() {
345 return getOperand(0);
347 const Value *getPointerOperand() const {
348 return getOperand(0);
350 static unsigned getPointerOperandIndex() {
351 return 0U; // get index for modifying correct operand
354 inline unsigned getNumIndices() const { // Note: always non-negative
355 return getNumOperands() - 1;
358 inline bool hasIndices() const {
359 return getNumOperands() > 1;
362 // Methods for support type inquiry through isa, cast, and dyn_cast:
363 static inline bool classof(const GetElementPtrInst *) { return true; }
364 static inline bool classof(const Instruction *I) {
365 return (I->getOpcode() == Instruction::GetElementPtr);
367 static inline bool classof(const Value *V) {
368 return isa<Instruction>(V) && classof(cast<Instruction>(V));
372 //===----------------------------------------------------------------------===//
374 //===----------------------------------------------------------------------===//
376 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
379 class SetCondInst : public BinaryOperator {
381 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
382 const std::string &Name = "", Instruction *InsertBefore = 0);
383 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
384 const std::string &Name, BasicBlock *InsertAtEnd);
386 /// getInverseCondition - Return the inverse of the current condition opcode.
387 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
389 BinaryOps getInverseCondition() const {
390 return getInverseCondition(getOpcode());
393 /// getInverseCondition - Static version that you can use without an
394 /// instruction available.
396 static BinaryOps getInverseCondition(BinaryOps Opcode);
398 /// getSwappedCondition - Return the condition opcode that would be the result
399 /// of exchanging the two operands of the setcc instruction without changing
400 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
402 BinaryOps getSwappedCondition() const {
403 return getSwappedCondition(getOpcode());
406 /// getSwappedCondition - Static version that you can use without an
407 /// instruction available.
409 static BinaryOps getSwappedCondition(BinaryOps Opcode);
412 // Methods for support type inquiry through isa, cast, and dyn_cast:
413 static inline bool classof(const SetCondInst *) { return true; }
414 static inline bool classof(const Instruction *I) {
415 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
416 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
417 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
419 static inline bool classof(const Value *V) {
420 return isa<Instruction>(V) && classof(cast<Instruction>(V));
424 //===----------------------------------------------------------------------===//
426 //===----------------------------------------------------------------------===//
428 /// CastInst - This class represents a cast from Operand[0] to the type of
429 /// the instruction (i->getType()).
431 class CastInst : public UnaryInstruction {
432 CastInst(const CastInst &CI)
433 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
436 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
437 Instruction *InsertBefore = 0)
438 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
440 CastInst(Value *S, const Type *Ty, const std::string &Name,
441 BasicBlock *InsertAtEnd)
442 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
445 virtual CastInst *clone() const;
447 // Methods for support type inquiry through isa, cast, and dyn_cast:
448 static inline bool classof(const CastInst *) { return true; }
449 static inline bool classof(const Instruction *I) {
450 return I->getOpcode() == Cast;
452 static inline bool classof(const Value *V) {
453 return isa<Instruction>(V) && classof(cast<Instruction>(V));
458 //===----------------------------------------------------------------------===//
460 //===----------------------------------------------------------------------===//
462 /// CallInst - This class represents a function call, abstracting a target
463 /// machine's calling convention.
465 class CallInst : public Instruction {
466 CallInst(const CallInst &CI);
467 void init(Value *Func, const std::vector<Value*> &Params);
468 void init(Value *Func, Value *Actual1, Value *Actual2);
469 void init(Value *Func, Value *Actual);
470 void init(Value *Func);
473 CallInst(Value *F, const std::vector<Value*> &Par,
474 const std::string &Name = "", Instruction *InsertBefore = 0);
475 CallInst(Value *F, const std::vector<Value*> &Par,
476 const std::string &Name, BasicBlock *InsertAtEnd);
478 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
479 // actuals, respectively.
480 CallInst(Value *F, Value *Actual1, Value *Actual2,
481 const std::string& Name = "", Instruction *InsertBefore = 0);
482 CallInst(Value *F, Value *Actual1, Value *Actual2,
483 const std::string& Name, BasicBlock *InsertAtEnd);
484 CallInst(Value *F, Value *Actual, const std::string& Name = "",
485 Instruction *InsertBefore = 0);
486 CallInst(Value *F, Value *Actual, const std::string& Name,
487 BasicBlock *InsertAtEnd);
488 explicit CallInst(Value *F, const std::string &Name = "",
489 Instruction *InsertBefore = 0);
490 explicit CallInst(Value *F, const std::string &Name,
491 BasicBlock *InsertAtEnd);
494 virtual CallInst *clone() const;
495 bool mayWriteToMemory() const { return true; }
497 /// getCalledFunction - Return the function being called by this instruction
498 /// if it is a direct call. If it is a call through a function pointer,
500 Function *getCalledFunction() const {
501 return (Function*)dyn_cast<Function>(getOperand(0));
504 // getCalledValue - Get a pointer to a method that is invoked by this inst.
505 inline const Value *getCalledValue() const { return getOperand(0); }
506 inline Value *getCalledValue() { return getOperand(0); }
508 // Methods for support type inquiry through isa, cast, and dyn_cast:
509 static inline bool classof(const CallInst *) { return true; }
510 static inline bool classof(const Instruction *I) {
511 return I->getOpcode() == Instruction::Call;
513 static inline bool classof(const Value *V) {
514 return isa<Instruction>(V) && classof(cast<Instruction>(V));
519 //===----------------------------------------------------------------------===//
521 //===----------------------------------------------------------------------===//
523 /// ShiftInst - This class represents left and right shift instructions.
525 class ShiftInst : public Instruction {
527 ShiftInst(const ShiftInst &SI)
528 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
529 Ops[0].init(SI.Ops[0], this);
530 Ops[1].init(SI.Ops[1], this);
532 void init(OtherOps Opcode, Value *S, Value *SA) {
533 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
534 Ops[0].init(S, this);
535 Ops[1].init(SA, this);
539 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
540 Instruction *InsertBefore = 0)
541 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
544 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
545 BasicBlock *InsertAtEnd)
546 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
550 OtherOps getOpcode() const {
551 return static_cast<OtherOps>(Instruction::getOpcode());
554 /// Transparently provide more efficient getOperand methods.
555 Value *getOperand(unsigned i) const {
556 assert(i < 2 && "getOperand() out of range!");
559 void setOperand(unsigned i, Value *Val) {
560 assert(i < 2 && "setOperand() out of range!");
563 unsigned getNumOperands() const { return 2; }
565 virtual ShiftInst *clone() const;
567 // Methods for support type inquiry through isa, cast, and dyn_cast:
568 static inline bool classof(const ShiftInst *) { return true; }
569 static inline bool classof(const Instruction *I) {
570 return (I->getOpcode() == Instruction::Shr) |
571 (I->getOpcode() == Instruction::Shl);
573 static inline bool classof(const Value *V) {
574 return isa<Instruction>(V) && classof(cast<Instruction>(V));
578 //===----------------------------------------------------------------------===//
580 //===----------------------------------------------------------------------===//
582 /// SelectInst - This class represents the LLVM 'select' instruction.
584 class SelectInst : public Instruction {
587 void init(Value *C, Value *S1, Value *S2) {
588 Ops[0].init(C, this);
589 Ops[1].init(S1, this);
590 Ops[2].init(S2, this);
593 SelectInst(const SelectInst &SI)
594 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
595 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
598 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
599 Instruction *InsertBefore = 0)
600 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
601 Name, InsertBefore) {
604 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
605 BasicBlock *InsertAtEnd)
606 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
611 Value *getCondition() const { return Ops[0]; }
612 Value *getTrueValue() const { return Ops[1]; }
613 Value *getFalseValue() const { return Ops[2]; }
615 /// Transparently provide more efficient getOperand methods.
616 Value *getOperand(unsigned i) const {
617 assert(i < 3 && "getOperand() out of range!");
620 void setOperand(unsigned i, Value *Val) {
621 assert(i < 3 && "setOperand() out of range!");
624 unsigned getNumOperands() const { return 3; }
626 OtherOps getOpcode() const {
627 return static_cast<OtherOps>(Instruction::getOpcode());
630 virtual SelectInst *clone() const;
632 // Methods for support type inquiry through isa, cast, and dyn_cast:
633 static inline bool classof(const SelectInst *) { return true; }
634 static inline bool classof(const Instruction *I) {
635 return I->getOpcode() == Instruction::Select;
637 static inline bool classof(const Value *V) {
638 return isa<Instruction>(V) && classof(cast<Instruction>(V));
643 //===----------------------------------------------------------------------===//
645 //===----------------------------------------------------------------------===//
647 /// VANextInst - This class represents the va_next llvm instruction, which
648 /// advances a vararg list passed an argument of the specified type, returning
649 /// the resultant list.
651 class VANextInst : public UnaryInstruction {
653 VANextInst(const VANextInst &VAN)
654 : UnaryInstruction(VAN.getType(), VANext, VAN.getOperand(0)),
655 ArgTy(VAN.getArgType()) {
659 VANextInst(Value *List, const Type *Ty, const std::string &Name = "",
660 Instruction *InsertBefore = 0)
661 : UnaryInstruction(List->getType(), VANext, List, Name, InsertBefore),
664 VANextInst(Value *List, const Type *Ty, const std::string &Name,
665 BasicBlock *InsertAtEnd)
666 : UnaryInstruction(List->getType(), VANext, List, Name, InsertAtEnd),
670 const Type *getArgType() const { return ArgTy; }
672 virtual VANextInst *clone() const;
674 // Methods for support type inquiry through isa, cast, and dyn_cast:
675 static inline bool classof(const VANextInst *) { return true; }
676 static inline bool classof(const Instruction *I) {
677 return I->getOpcode() == VANext;
679 static inline bool classof(const Value *V) {
680 return isa<Instruction>(V) && classof(cast<Instruction>(V));
685 //===----------------------------------------------------------------------===//
687 //===----------------------------------------------------------------------===//
689 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
690 /// an argument of the specified type given a va_list.
692 class VAArgInst : public UnaryInstruction {
693 VAArgInst(const VAArgInst &VAA)
694 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
696 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
697 Instruction *InsertBefore = 0)
698 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
700 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
701 BasicBlock *InsertAtEnd)
702 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
705 virtual VAArgInst *clone() const;
707 // Methods for support type inquiry through isa, cast, and dyn_cast:
708 static inline bool classof(const VAArgInst *) { return true; }
709 static inline bool classof(const Instruction *I) {
710 return I->getOpcode() == VAArg;
712 static inline bool classof(const Value *V) {
713 return isa<Instruction>(V) && classof(cast<Instruction>(V));
717 //===----------------------------------------------------------------------===//
719 //===----------------------------------------------------------------------===//
721 // PHINode - The PHINode class is used to represent the magical mystical PHI
722 // node, that can not exist in nature, but can be synthesized in a computer
723 // scientist's overactive imagination.
725 class PHINode : public Instruction {
726 /// ReservedSpace - The number of operands actually allocated. NumOperands is
727 /// the number actually in use.
728 unsigned ReservedSpace;
729 PHINode(const PHINode &PN);
731 PHINode(const Type *Ty, const std::string &Name = "",
732 Instruction *InsertBefore = 0)
733 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
737 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
738 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
744 /// reserveOperandSpace - This method can be used to avoid repeated
745 /// reallocation of PHI operand lists by reserving space for the correct
746 /// number of operands before adding them. Unlike normal vector reserves,
747 /// this method can also be used to trim the operand space.
748 void reserveOperandSpace(unsigned NumValues) {
749 resizeOperands(NumValues*2);
752 virtual PHINode *clone() const;
754 /// getNumIncomingValues - Return the number of incoming edges
756 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
758 /// getIncomingValue - Return incoming value #x
760 Value *getIncomingValue(unsigned i) const {
761 assert(i*2 < getNumOperands() && "Invalid value number!");
762 return getOperand(i*2);
764 void setIncomingValue(unsigned i, Value *V) {
765 assert(i*2 < getNumOperands() && "Invalid value number!");
768 unsigned getOperandNumForIncomingValue(unsigned i) {
772 /// getIncomingBlock - Return incoming basic block #x
774 BasicBlock *getIncomingBlock(unsigned i) const {
775 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
777 void setIncomingBlock(unsigned i, BasicBlock *BB) {
778 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
780 unsigned getOperandNumForIncomingBlock(unsigned i) {
784 /// addIncoming - Add an incoming value to the end of the PHI list
786 void addIncoming(Value *V, BasicBlock *BB) {
787 assert(getType() == V->getType() &&
788 "All operands to PHI node must be the same type as the PHI node!");
789 unsigned OpNo = NumOperands;
790 if (OpNo+2 > ReservedSpace)
791 resizeOperands(0); // Get more space!
792 // Initialize some new operands.
793 NumOperands = OpNo+2;
794 OperandList[OpNo].init(V, this);
795 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
798 /// removeIncomingValue - Remove an incoming value. This is useful if a
799 /// predecessor basic block is deleted. The value removed is returned.
801 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
802 /// is true), the PHI node is destroyed and any uses of it are replaced with
803 /// dummy values. The only time there should be zero incoming values to a PHI
804 /// node is when the block is dead, so this strategy is sound.
806 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
808 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
809 int Idx = getBasicBlockIndex(BB);
810 assert(Idx >= 0 && "Invalid basic block argument to remove!");
811 return removeIncomingValue(Idx, DeletePHIIfEmpty);
814 /// getBasicBlockIndex - Return the first index of the specified basic
815 /// block in the value list for this PHI. Returns -1 if no instance.
817 int getBasicBlockIndex(const BasicBlock *BB) const {
818 Use *OL = OperandList;
819 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
820 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
824 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
825 return getIncomingValue(getBasicBlockIndex(BB));
828 /// Methods for support type inquiry through isa, cast, and dyn_cast:
829 static inline bool classof(const PHINode *) { return true; }
830 static inline bool classof(const Instruction *I) {
831 return I->getOpcode() == Instruction::PHI;
833 static inline bool classof(const Value *V) {
834 return isa<Instruction>(V) && classof(cast<Instruction>(V));
837 void resizeOperands(unsigned NumOperands);
840 //===----------------------------------------------------------------------===//
842 //===----------------------------------------------------------------------===//
844 //===---------------------------------------------------------------------------
845 /// ReturnInst - Return a value (possibly void), from a function. Execution
846 /// does not continue in this function any longer.
848 class ReturnInst : public TerminatorInst {
849 Use RetVal; // Possibly null retval.
850 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
851 RI.getNumOperands()) {
852 if (RI.getNumOperands())
853 RetVal.init(RI.RetVal, this);
856 void init(Value *RetVal);
859 // ReturnInst constructors:
860 // ReturnInst() - 'ret void' instruction
861 // ReturnInst( null) - 'ret void' instruction
862 // ReturnInst(Value* X) - 'ret X' instruction
863 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
864 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
865 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
866 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
868 // NOTE: If the Value* passed is of type void then the constructor behaves as
869 // if it was passed NULL.
870 ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
871 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
874 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
875 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
878 ReturnInst(BasicBlock *InsertAtEnd)
879 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
882 virtual ReturnInst *clone() const;
884 // Transparently provide more efficient getOperand methods.
885 Value *getOperand(unsigned i) const {
886 assert(i < getNumOperands() && "getOperand() out of range!");
889 void setOperand(unsigned i, Value *Val) {
890 assert(i < getNumOperands() && "setOperand() out of range!");
894 Value *getReturnValue() const { return RetVal; }
896 unsigned getNumSuccessors() const { return 0; }
898 // Methods for support type inquiry through isa, cast, and dyn_cast:
899 static inline bool classof(const ReturnInst *) { return true; }
900 static inline bool classof(const Instruction *I) {
901 return (I->getOpcode() == Instruction::Ret);
903 static inline bool classof(const Value *V) {
904 return isa<Instruction>(V) && classof(cast<Instruction>(V));
907 virtual BasicBlock *getSuccessorV(unsigned idx) const;
908 virtual unsigned getNumSuccessorsV() const;
909 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
912 //===----------------------------------------------------------------------===//
914 //===----------------------------------------------------------------------===//
916 //===---------------------------------------------------------------------------
917 /// BranchInst - Conditional or Unconditional Branch instruction.
919 class BranchInst : public TerminatorInst {
920 /// Ops list - Branches are strange. The operands are ordered:
921 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
922 /// they don't have to check for cond/uncond branchness.
924 BranchInst(const BranchInst &BI);
927 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
928 // BranchInst(BB *B) - 'br B'
929 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
930 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
931 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
932 // BranchInst(BB* B, BB *I) - 'br B' insert at end
933 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
934 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
935 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
936 assert(IfTrue != 0 && "Branch destination may not be null!");
937 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
939 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
940 Instruction *InsertBefore = 0)
941 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
942 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
943 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
944 Ops[2].init(Cond, this);
950 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
951 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
952 assert(IfTrue != 0 && "Branch destination may not be null!");
953 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
956 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
957 BasicBlock *InsertAtEnd)
958 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
959 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
960 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
961 Ops[2].init(Cond, this);
968 /// Transparently provide more efficient getOperand methods.
969 Value *getOperand(unsigned i) const {
970 assert(i < getNumOperands() && "getOperand() out of range!");
973 void setOperand(unsigned i, Value *Val) {
974 assert(i < getNumOperands() && "setOperand() out of range!");
978 virtual BranchInst *clone() const;
980 inline bool isUnconditional() const { return getNumOperands() == 1; }
981 inline bool isConditional() const { return getNumOperands() == 3; }
983 inline Value *getCondition() const {
984 assert(isConditional() && "Cannot get condition of an uncond branch!");
985 return getOperand(2);
988 void setCondition(Value *V) {
989 assert(isConditional() && "Cannot set condition of unconditional branch!");
993 // setUnconditionalDest - Change the current branch to an unconditional branch
994 // targeting the specified block.
995 // FIXME: Eliminate this ugly method.
996 void setUnconditionalDest(BasicBlock *Dest) {
997 if (isConditional()) { // Convert this to an uncond branch.
1002 setOperand(0, reinterpret_cast<Value*>(Dest));
1005 unsigned getNumSuccessors() const { return 1+isConditional(); }
1007 BasicBlock *getSuccessor(unsigned i) const {
1008 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1009 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1010 cast<BasicBlock>(getOperand(1));
1013 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1014 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1015 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1018 // Methods for support type inquiry through isa, cast, and dyn_cast:
1019 static inline bool classof(const BranchInst *) { return true; }
1020 static inline bool classof(const Instruction *I) {
1021 return (I->getOpcode() == Instruction::Br);
1023 static inline bool classof(const Value *V) {
1024 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1027 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1028 virtual unsigned getNumSuccessorsV() const;
1029 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1032 //===----------------------------------------------------------------------===//
1034 //===----------------------------------------------------------------------===//
1036 //===---------------------------------------------------------------------------
1037 /// SwitchInst - Multiway switch
1039 class SwitchInst : public TerminatorInst {
1040 unsigned ReservedSpace;
1041 // Operand[0] = Value to switch on
1042 // Operand[1] = Default basic block destination
1043 // Operand[2n ] = Value to match
1044 // Operand[2n+1] = BasicBlock to go to on match
1045 SwitchInst(const SwitchInst &RI);
1046 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1047 void resizeOperands(unsigned No);
1049 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1050 /// switch on and a default destination. The number of additional cases can
1051 /// be specified here to make memory allocation more efficient. This
1052 /// constructor can also autoinsert before another instruction.
1053 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1054 Instruction *InsertBefore = 0)
1055 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1056 init(Value, Default, NumCases);
1059 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1060 /// switch on and a default destination. The number of additional cases can
1061 /// be specified here to make memory allocation more efficient. This
1062 /// constructor also autoinserts at the end of the specified BasicBlock.
1063 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1064 BasicBlock *InsertAtEnd)
1065 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1066 init(Value, Default, NumCases);
1071 // Accessor Methods for Switch stmt
1072 inline Value *getCondition() const { return getOperand(0); }
1073 void setCondition(Value *V) { setOperand(0, V); }
1075 inline BasicBlock *getDefaultDest() const {
1076 return cast<BasicBlock>(getOperand(1));
1079 /// getNumCases - return the number of 'cases' in this switch instruction.
1080 /// Note that case #0 is always the default case.
1081 unsigned getNumCases() const {
1082 return getNumOperands()/2;
1085 /// getCaseValue - Return the specified case value. Note that case #0, the
1086 /// default destination, does not have a case value.
1087 Constant *getCaseValue(unsigned i) {
1088 assert(i && i < getNumCases() && "Illegal case value to get!");
1089 return getSuccessorValue(i);
1092 /// getCaseValue - Return the specified case value. Note that case #0, the
1093 /// default destination, does not have a case value.
1094 const Constant *getCaseValue(unsigned i) const {
1095 assert(i && i < getNumCases() && "Illegal case value to get!");
1096 return getSuccessorValue(i);
1099 /// findCaseValue - Search all of the case values for the specified constant.
1100 /// If it is explicitly handled, return the case number of it, otherwise
1101 /// return 0 to indicate that it is handled by the default handler.
1102 unsigned findCaseValue(const Constant *C) const {
1103 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1104 if (getCaseValue(i) == C)
1109 /// addCase - Add an entry to the switch instruction...
1111 void addCase(Constant *OnVal, BasicBlock *Dest);
1113 /// removeCase - This method removes the specified successor from the switch
1114 /// instruction. Note that this cannot be used to remove the default
1115 /// destination (successor #0).
1117 void removeCase(unsigned idx);
1119 virtual SwitchInst *clone() const;
1121 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1122 BasicBlock *getSuccessor(unsigned idx) const {
1123 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1124 return cast<BasicBlock>(getOperand(idx*2+1));
1126 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1127 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1128 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1131 // getSuccessorValue - Return the value associated with the specified
1133 inline Constant *getSuccessorValue(unsigned idx) const {
1134 assert(idx < getNumSuccessors() && "Successor # out of range!");
1135 return cast<Constant>(getOperand(idx*2));
1138 // Methods for support type inquiry through isa, cast, and dyn_cast:
1139 static inline bool classof(const SwitchInst *) { return true; }
1140 static inline bool classof(const Instruction *I) {
1141 return (I->getOpcode() == Instruction::Switch);
1143 static inline bool classof(const Value *V) {
1144 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1147 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1148 virtual unsigned getNumSuccessorsV() const;
1149 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1152 //===----------------------------------------------------------------------===//
1154 //===----------------------------------------------------------------------===//
1156 //===---------------------------------------------------------------------------
1157 /// InvokeInst - Invoke instruction
1159 class InvokeInst : public TerminatorInst {
1160 InvokeInst(const InvokeInst &BI);
1161 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1162 const std::vector<Value*> &Params);
1164 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1165 const std::vector<Value*> &Params, const std::string &Name = "",
1166 Instruction *InsertBefore = 0);
1167 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1168 const std::vector<Value*> &Params, const std::string &Name,
1169 BasicBlock *InsertAtEnd);
1172 virtual InvokeInst *clone() const;
1174 bool mayWriteToMemory() const { return true; }
1176 /// getCalledFunction - Return the function called, or null if this is an
1177 /// indirect function invocation.
1179 Function *getCalledFunction() const {
1180 return dyn_cast<Function>(getOperand(0));
1183 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1184 inline Value *getCalledValue() const { return getOperand(0); }
1186 // get*Dest - Return the destination basic blocks...
1187 BasicBlock *getNormalDest() const {
1188 return cast<BasicBlock>(getOperand(1));
1190 BasicBlock *getUnwindDest() const {
1191 return cast<BasicBlock>(getOperand(2));
1193 void setNormalDest(BasicBlock *B) {
1194 setOperand(1, reinterpret_cast<Value*>(B));
1197 void setUnwindDest(BasicBlock *B) {
1198 setOperand(2, reinterpret_cast<Value*>(B));
1201 inline BasicBlock *getSuccessor(unsigned i) const {
1202 assert(i < 2 && "Successor # out of range for invoke!");
1203 return i == 0 ? getNormalDest() : getUnwindDest();
1206 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1207 assert(idx < 2 && "Successor # out of range for invoke!");
1208 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1211 unsigned getNumSuccessors() const { return 2; }
1213 // Methods for support type inquiry through isa, cast, and dyn_cast:
1214 static inline bool classof(const InvokeInst *) { return true; }
1215 static inline bool classof(const Instruction *I) {
1216 return (I->getOpcode() == Instruction::Invoke);
1218 static inline bool classof(const Value *V) {
1219 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1222 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1223 virtual unsigned getNumSuccessorsV() const;
1224 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1228 //===----------------------------------------------------------------------===//
1230 //===----------------------------------------------------------------------===//
1232 //===---------------------------------------------------------------------------
1233 /// UnwindInst - Immediately exit the current function, unwinding the stack
1234 /// until an invoke instruction is found.
1236 class UnwindInst : public TerminatorInst {
1238 UnwindInst(Instruction *InsertBefore = 0)
1239 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1241 UnwindInst(BasicBlock *InsertAtEnd)
1242 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1245 virtual UnwindInst *clone() const;
1247 unsigned getNumSuccessors() const { return 0; }
1249 // Methods for support type inquiry through isa, cast, and dyn_cast:
1250 static inline bool classof(const UnwindInst *) { return true; }
1251 static inline bool classof(const Instruction *I) {
1252 return I->getOpcode() == Instruction::Unwind;
1254 static inline bool classof(const Value *V) {
1255 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1258 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1259 virtual unsigned getNumSuccessorsV() const;
1260 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1263 //===----------------------------------------------------------------------===//
1264 // UnreachableInst Class
1265 //===----------------------------------------------------------------------===//
1267 //===---------------------------------------------------------------------------
1268 /// UnreachableInst - This function has undefined behavior. In particular, the
1269 /// presence of this instruction indicates some higher level knowledge that the
1270 /// end of the block cannot be reached.
1272 class UnreachableInst : public TerminatorInst {
1274 UnreachableInst(Instruction *InsertBefore = 0)
1275 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1277 UnreachableInst(BasicBlock *InsertAtEnd)
1278 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1281 virtual UnreachableInst *clone() const;
1283 unsigned getNumSuccessors() const { return 0; }
1285 // Methods for support type inquiry through isa, cast, and dyn_cast:
1286 static inline bool classof(const UnreachableInst *) { return true; }
1287 static inline bool classof(const Instruction *I) {
1288 return I->getOpcode() == Instruction::Unreachable;
1290 static inline bool classof(const Value *V) {
1291 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1294 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1295 virtual unsigned getNumSuccessorsV() const;
1296 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1299 } // End llvm namespace