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 Instruction {
36 void init(const Type *Ty, Value *ArraySize, unsigned iTy);
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 Operands[0]; }
53 inline Value *getArraySize() { return Operands[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 Instruction *clone() const {
98 return new MallocInst(*this);
101 // Methods for support type inquiry through isa, cast, and dyn_cast:
102 static inline bool classof(const MallocInst *) { return true; }
103 static inline bool classof(const Instruction *I) {
104 return (I->getOpcode() == Instruction::Malloc);
106 static inline bool classof(const Value *V) {
107 return isa<Instruction>(V) && classof(cast<Instruction>(V));
112 //===----------------------------------------------------------------------===//
114 //===----------------------------------------------------------------------===//
116 /// AllocaInst - an instruction to allocate memory on the stack
118 class AllocaInst : public AllocationInst {
119 AllocaInst(const AllocaInst &);
121 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
122 const std::string &Name = "",
123 Instruction *InsertBefore = 0)
124 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertBefore) {}
125 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
126 BasicBlock *InsertAtEnd)
127 : AllocationInst(Ty, ArraySize, Alloca, Name, InsertAtEnd) {}
129 virtual Instruction *clone() const {
130 return new AllocaInst(*this);
133 // Methods for support type inquiry through isa, cast, and dyn_cast:
134 static inline bool classof(const AllocaInst *) { return true; }
135 static inline bool classof(const Instruction *I) {
136 return (I->getOpcode() == Instruction::Alloca);
138 static inline bool classof(const Value *V) {
139 return isa<Instruction>(V) && classof(cast<Instruction>(V));
144 //===----------------------------------------------------------------------===//
146 //===----------------------------------------------------------------------===//
148 /// FreeInst - an instruction to deallocate memory
150 class FreeInst : public Instruction {
151 void init(Value *Ptr);
154 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
155 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
157 virtual Instruction *clone() const { return new FreeInst(Operands[0]); }
159 virtual bool mayWriteToMemory() const { return true; }
161 // Methods for support type inquiry through isa, cast, and dyn_cast:
162 static inline bool classof(const FreeInst *) { return true; }
163 static inline bool classof(const Instruction *I) {
164 return (I->getOpcode() == Instruction::Free);
166 static inline bool classof(const Value *V) {
167 return isa<Instruction>(V) && classof(cast<Instruction>(V));
172 //===----------------------------------------------------------------------===//
174 //===----------------------------------------------------------------------===//
176 /// LoadInst - an instruction for reading from memory
178 class LoadInst : public Instruction {
179 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
180 Volatile = LI.isVolatile();
181 init(LI.Operands[0]);
183 bool Volatile; // True if this is a volatile load
184 void init(Value *Ptr);
186 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
187 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
188 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
189 Instruction *InsertBefore = 0);
190 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
191 BasicBlock *InsertAtEnd);
193 /// isVolatile - Return true if this is a load from a volatile memory
196 bool isVolatile() const { return Volatile; }
198 /// setVolatile - Specify whether this is a volatile load or not.
200 void setVolatile(bool V) { Volatile = V; }
202 virtual Instruction *clone() const { return new LoadInst(*this); }
204 virtual bool mayWriteToMemory() const { return isVolatile(); }
206 Value *getPointerOperand() { return getOperand(0); }
207 const Value *getPointerOperand() const { return getOperand(0); }
208 static unsigned getPointerOperandIndex() { return 0U; }
210 // Methods for support type inquiry through isa, cast, and dyn_cast:
211 static inline bool classof(const LoadInst *) { return true; }
212 static inline bool classof(const Instruction *I) {
213 return I->getOpcode() == Instruction::Load;
215 static inline bool classof(const Value *V) {
216 return isa<Instruction>(V) && classof(cast<Instruction>(V));
221 //===----------------------------------------------------------------------===//
223 //===----------------------------------------------------------------------===//
225 /// StoreInst - an instruction for storing to memory
227 class StoreInst : public Instruction {
228 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
229 Volatile = SI.isVolatile();
230 init(SI.Operands[0], SI.Operands[1]);
232 bool Volatile; // True if this is a volatile store
233 void init(Value *Val, Value *Ptr);
235 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
236 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
237 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
238 Instruction *InsertBefore = 0);
239 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
242 /// isVolatile - Return true if this is a load from a volatile memory
245 bool isVolatile() const { return Volatile; }
247 /// setVolatile - Specify whether this is a volatile load or not.
249 void setVolatile(bool V) { Volatile = V; }
251 virtual Instruction *clone() const { return new StoreInst(*this); }
253 virtual bool mayWriteToMemory() const { return true; }
255 Value *getPointerOperand() { return getOperand(1); }
256 const Value *getPointerOperand() const { return getOperand(1); }
257 static unsigned getPointerOperandIndex() { return 1U; }
259 // Methods for support type inquiry through isa, cast, and dyn_cast:
260 static inline bool classof(const StoreInst *) { return true; }
261 static inline bool classof(const Instruction *I) {
262 return I->getOpcode() == Instruction::Store;
264 static inline bool classof(const Value *V) {
265 return isa<Instruction>(V) && classof(cast<Instruction>(V));
270 //===----------------------------------------------------------------------===//
271 // GetElementPtrInst Class
272 //===----------------------------------------------------------------------===//
274 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
275 /// access elements of arrays and structs
277 class GetElementPtrInst : public Instruction {
278 GetElementPtrInst(const GetElementPtrInst &EPI)
279 : Instruction((static_cast<const Instruction*>(&EPI)->getType()),
281 Operands.reserve(EPI.Operands.size());
282 for (unsigned i = 0, E = EPI.Operands.size(); i != E; ++i)
283 Operands.push_back(Use(EPI.Operands[i], this));
285 void init(Value *Ptr, const std::vector<Value*> &Idx);
286 void init(Value *Ptr, Value *Idx0, Value *Idx1);
288 /// Constructors - Create a getelementptr instruction with a base pointer an
289 /// list of indices. The first ctor can optionally insert before an existing
290 /// instruction, the second appends the new instruction to the specified
292 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
293 const std::string &Name = "", Instruction *InsertBefore =0);
294 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
295 const std::string &Name, BasicBlock *InsertAtEnd);
297 /// Constructors - These two constructors are convenience methods because two
298 /// index getelementptr instructions are so common.
299 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
300 const std::string &Name = "", Instruction *InsertBefore =0);
301 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
302 const std::string &Name, BasicBlock *InsertAtEnd);
304 virtual Instruction *clone() const { return new GetElementPtrInst(*this); }
306 // getType - Overload to return most specific pointer type...
307 inline const PointerType *getType() const {
308 return reinterpret_cast<const PointerType*>(Instruction::getType());
311 /// getIndexedType - Returns the type of the element that would be loaded with
312 /// a load instruction with the specified parameters.
314 /// A null type is returned if the indices are invalid for the specified
317 static const Type *getIndexedType(const Type *Ptr,
318 const std::vector<Value*> &Indices,
319 bool AllowStructLeaf = false);
320 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
321 bool AllowStructLeaf = false);
323 inline op_iterator idx_begin() { return op_begin()+1; }
324 inline const_op_iterator idx_begin() const { return op_begin()+1; }
325 inline op_iterator idx_end() { return op_end(); }
326 inline const_op_iterator idx_end() const { return op_end(); }
328 Value *getPointerOperand() {
329 return getOperand(0);
331 const Value *getPointerOperand() const {
332 return getOperand(0);
334 static unsigned getPointerOperandIndex() {
335 return 0U; // get index for modifying correct operand
338 inline unsigned getNumIndices() const { // Note: always non-negative
339 return getNumOperands() - 1;
342 inline bool hasIndices() const {
343 return getNumOperands() > 1;
346 // Methods for support type inquiry through isa, cast, and dyn_cast:
347 static inline bool classof(const GetElementPtrInst *) { return true; }
348 static inline bool classof(const Instruction *I) {
349 return (I->getOpcode() == Instruction::GetElementPtr);
351 static inline bool classof(const Value *V) {
352 return isa<Instruction>(V) && classof(cast<Instruction>(V));
356 //===----------------------------------------------------------------------===//
358 //===----------------------------------------------------------------------===//
360 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
363 class SetCondInst : public BinaryOperator {
366 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
367 const std::string &Name = "", Instruction *InsertBefore = 0);
368 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
369 const std::string &Name, BasicBlock *InsertAtEnd);
371 /// getInverseCondition - Return the inverse of the current condition opcode.
372 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
374 BinaryOps getInverseCondition() const {
375 return getInverseCondition(getOpcode());
378 /// getInverseCondition - Static version that you can use without an
379 /// instruction available.
381 static BinaryOps getInverseCondition(BinaryOps Opcode);
383 /// getSwappedCondition - Return the condition opcode that would be the result
384 /// of exchanging the two operands of the setcc instruction without changing
385 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
387 BinaryOps getSwappedCondition() const {
388 return getSwappedCondition(getOpcode());
391 /// getSwappedCondition - Static version that you can use without an
392 /// instruction available.
394 static BinaryOps getSwappedCondition(BinaryOps Opcode);
397 // Methods for support type inquiry through isa, cast, and dyn_cast:
398 static inline bool classof(const SetCondInst *) { return true; }
399 static inline bool classof(const Instruction *I) {
400 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
401 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
402 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
404 static inline bool classof(const Value *V) {
405 return isa<Instruction>(V) && classof(cast<Instruction>(V));
409 //===----------------------------------------------------------------------===//
411 //===----------------------------------------------------------------------===//
413 /// CastInst - This class represents a cast from Operand[0] to the type of
414 /// the instruction (i->getType()).
416 class CastInst : public Instruction {
417 CastInst(const CastInst &CI) : Instruction(CI.getType(), Cast) {
419 Operands.push_back(Use(CI.Operands[0], this));
421 void init(Value *S) {
423 Operands.push_back(Use(S, this));
426 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
427 Instruction *InsertBefore = 0)
428 : Instruction(Ty, Cast, Name, InsertBefore) {
431 CastInst(Value *S, const Type *Ty, const std::string &Name,
432 BasicBlock *InsertAtEnd)
433 : Instruction(Ty, Cast, Name, InsertAtEnd) {
437 virtual Instruction *clone() const { return new CastInst(*this); }
439 // Methods for support type inquiry through isa, cast, and dyn_cast:
440 static inline bool classof(const CastInst *) { return true; }
441 static inline bool classof(const Instruction *I) {
442 return I->getOpcode() == Cast;
444 static inline bool classof(const Value *V) {
445 return isa<Instruction>(V) && classof(cast<Instruction>(V));
450 //===----------------------------------------------------------------------===//
452 //===----------------------------------------------------------------------===//
454 /// CallInst - This class represents a function call, abstracting a target
455 /// machine's calling convention.
457 class CallInst : public Instruction {
458 CallInst(const CallInst &CI);
459 void init(Value *Func, const std::vector<Value*> &Params);
460 void init(Value *Func, Value *Actual1, Value *Actual2);
461 void init(Value *Func, Value *Actual);
462 void init(Value *Func);
465 CallInst(Value *F, const std::vector<Value*> &Par,
466 const std::string &Name = "", Instruction *InsertBefore = 0);
467 CallInst(Value *F, const std::vector<Value*> &Par,
468 const std::string &Name, BasicBlock *InsertAtEnd);
470 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
471 // actuals, respectively.
472 CallInst(Value *F, Value *Actual1, Value *Actual2,
473 const std::string& Name = "", Instruction *InsertBefore = 0);
474 CallInst(Value *F, Value *Actual1, Value *Actual2,
475 const std::string& Name, BasicBlock *InsertAtEnd);
476 CallInst(Value *F, Value *Actual, const std::string& Name = "",
477 Instruction *InsertBefore = 0);
478 CallInst(Value *F, Value *Actual, const std::string& Name,
479 BasicBlock *InsertAtEnd);
480 explicit CallInst(Value *F, const std::string &Name = "",
481 Instruction *InsertBefore = 0);
482 explicit CallInst(Value *F, const std::string &Name,
483 BasicBlock *InsertAtEnd);
485 virtual Instruction *clone() const { return new CallInst(*this); }
486 bool mayWriteToMemory() const { return true; }
488 // FIXME: These methods should be inline once we eliminate
489 // ConstantPointerRefs!
490 const Function *getCalledFunction() const;
491 Function *getCalledFunction();
493 // getCalledValue - Get a pointer to a method that is invoked by this inst.
494 inline const Value *getCalledValue() const { return Operands[0]; }
495 inline Value *getCalledValue() { return Operands[0]; }
497 // Methods for support type inquiry through isa, cast, and dyn_cast:
498 static inline bool classof(const CallInst *) { return true; }
499 static inline bool classof(const Instruction *I) {
500 return I->getOpcode() == Instruction::Call;
502 static inline bool classof(const Value *V) {
503 return isa<Instruction>(V) && classof(cast<Instruction>(V));
508 //===----------------------------------------------------------------------===//
510 //===----------------------------------------------------------------------===//
512 /// ShiftInst - This class represents left and right shift instructions.
514 class ShiftInst : public Instruction {
515 ShiftInst(const ShiftInst &SI) : Instruction(SI.getType(), SI.getOpcode()) {
517 Operands.push_back(Use(SI.Operands[0], this));
518 Operands.push_back(Use(SI.Operands[1], this));
520 void init(OtherOps Opcode, Value *S, Value *SA) {
521 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
523 Operands.push_back(Use(S, this));
524 Operands.push_back(Use(SA, this));
528 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
529 Instruction *InsertBefore = 0)
530 : Instruction(S->getType(), Opcode, Name, InsertBefore) {
533 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
534 BasicBlock *InsertAtEnd)
535 : Instruction(S->getType(), Opcode, Name, InsertAtEnd) {
539 OtherOps getOpcode() const {
540 return static_cast<OtherOps>(Instruction::getOpcode());
543 virtual Instruction *clone() const { return new ShiftInst(*this); }
545 // Methods for support type inquiry through isa, cast, and dyn_cast:
546 static inline bool classof(const ShiftInst *) { return true; }
547 static inline bool classof(const Instruction *I) {
548 return (I->getOpcode() == Instruction::Shr) |
549 (I->getOpcode() == Instruction::Shl);
551 static inline bool classof(const Value *V) {
552 return isa<Instruction>(V) && classof(cast<Instruction>(V));
556 //===----------------------------------------------------------------------===//
558 //===----------------------------------------------------------------------===//
560 /// SelectInst - This class represents the LLVM 'select' instruction.
562 class SelectInst : public Instruction {
563 SelectInst(const SelectInst &SI) : Instruction(SI.getType(), SI.getOpcode()) {
565 Operands.push_back(Use(SI.Operands[0], this));
566 Operands.push_back(Use(SI.Operands[1], this));
567 Operands.push_back(Use(SI.Operands[2], this));
569 void init(Value *C, Value *S1, Value *S2) {
571 Operands.push_back(Use(C, this));
572 Operands.push_back(Use(S1, this));
573 Operands.push_back(Use(S2, this));
577 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
578 Instruction *InsertBefore = 0)
579 : Instruction(S1->getType(), Instruction::Select, Name, InsertBefore) {
582 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
583 BasicBlock *InsertAtEnd)
584 : Instruction(S1->getType(), Instruction::Select, Name, InsertAtEnd) {
588 Value *getCondition() const { return Operands[0]; }
589 Value *getTrueValue() const { return Operands[1]; }
590 Value *getFalseValue() const { return Operands[2]; }
592 OtherOps getOpcode() const {
593 return static_cast<OtherOps>(Instruction::getOpcode());
596 virtual Instruction *clone() const { return new SelectInst(*this); }
598 // Methods for support type inquiry through isa, cast, and dyn_cast:
599 static inline bool classof(const SelectInst *) { return true; }
600 static inline bool classof(const Instruction *I) {
601 return I->getOpcode() == Instruction::Select;
603 static inline bool classof(const Value *V) {
604 return isa<Instruction>(V) && classof(cast<Instruction>(V));
609 //===----------------------------------------------------------------------===//
611 //===----------------------------------------------------------------------===//
613 /// VANextInst - This class represents the va_next llvm instruction, which
614 /// advances a vararg list passed an argument of the specified type, returning
615 /// the resultant list.
617 class VANextInst : public Instruction {
619 void init(Value *List) {
621 Operands.push_back(Use(List, this));
623 VANextInst(const VANextInst &VAN)
624 : Instruction(VAN.getType(), VANext), ArgTy(VAN.getArgType()) {
625 init(VAN.Operands[0]);
629 VANextInst(Value *List, const Type *Ty, const std::string &Name = "",
630 Instruction *InsertBefore = 0)
631 : Instruction(List->getType(), VANext, Name, InsertBefore), ArgTy(Ty) {
634 VANextInst(Value *List, const Type *Ty, const std::string &Name,
635 BasicBlock *InsertAtEnd)
636 : Instruction(List->getType(), VANext, Name, InsertAtEnd), ArgTy(Ty) {
640 const Type *getArgType() const { return ArgTy; }
642 virtual Instruction *clone() const { return new VANextInst(*this); }
644 // Methods for support type inquiry through isa, cast, and dyn_cast:
645 static inline bool classof(const VANextInst *) { return true; }
646 static inline bool classof(const Instruction *I) {
647 return I->getOpcode() == VANext;
649 static inline bool classof(const Value *V) {
650 return isa<Instruction>(V) && classof(cast<Instruction>(V));
655 //===----------------------------------------------------------------------===//
657 //===----------------------------------------------------------------------===//
659 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
660 /// an argument of the specified type given a va_list.
662 class VAArgInst : public Instruction {
663 void init(Value* List) {
665 Operands.push_back(Use(List, this));
667 VAArgInst(const VAArgInst &VAA)
668 : Instruction(VAA.getType(), VAArg) {
669 init(VAA.Operands[0]);
672 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
673 Instruction *InsertBefore = 0)
674 : Instruction(Ty, VAArg, Name, InsertBefore) {
677 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
678 BasicBlock *InsertAtEnd)
679 : Instruction(Ty, VAArg, Name, InsertAtEnd) {
683 virtual Instruction *clone() const { return new VAArgInst(*this); }
685 // Methods for support type inquiry through isa, cast, and dyn_cast:
686 static inline bool classof(const VAArgInst *) { return true; }
687 static inline bool classof(const Instruction *I) {
688 return I->getOpcode() == VAArg;
690 static inline bool classof(const Value *V) {
691 return isa<Instruction>(V) && classof(cast<Instruction>(V));
695 //===----------------------------------------------------------------------===//
697 //===----------------------------------------------------------------------===//
699 // PHINode - The PHINode class is used to represent the magical mystical PHI
700 // node, that can not exist in nature, but can be synthesized in a computer
701 // scientist's overactive imagination.
703 class PHINode : public Instruction {
704 PHINode(const PHINode &PN);
706 PHINode(const Type *Ty, const std::string &Name = "",
707 Instruction *InsertBefore = 0)
708 : Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
711 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
712 : Instruction(Ty, Instruction::PHI, Name, InsertAtEnd) {
715 virtual Instruction *clone() const { return new PHINode(*this); }
717 /// getNumIncomingValues - Return the number of incoming edges
719 unsigned getNumIncomingValues() const { return Operands.size()/2; }
721 /// getIncomingValue - Return incoming value #x
723 Value *getIncomingValue(unsigned i) const {
724 assert(i*2 < Operands.size() && "Invalid value number!");
725 return Operands[i*2];
727 void setIncomingValue(unsigned i, Value *V) {
728 assert(i*2 < Operands.size() && "Invalid value number!");
731 inline unsigned getOperandNumForIncomingValue(unsigned i) {
735 /// getIncomingBlock - Return incoming basic block #x
737 BasicBlock *getIncomingBlock(unsigned i) const {
738 assert(i*2+1 < Operands.size() && "Invalid value number!");
739 return reinterpret_cast<BasicBlock*>(Operands[i*2+1].get());
741 void setIncomingBlock(unsigned i, BasicBlock *BB) {
742 assert(i*2+1 < Operands.size() && "Invalid value number!");
743 Operands[i*2+1] = reinterpret_cast<Value*>(BB);
745 unsigned getOperandNumForIncomingBlock(unsigned i) {
749 /// addIncoming - Add an incoming value to the end of the PHI list
751 void addIncoming(Value *V, BasicBlock *BB) {
752 assert(getType() == V->getType() &&
753 "All operands to PHI node must be the same type as the PHI node!");
754 Operands.push_back(Use(V, this));
755 Operands.push_back(Use(reinterpret_cast<Value*>(BB), this));
758 /// removeIncomingValue - Remove an incoming value. This is useful if a
759 /// predecessor basic block is deleted. The value removed is returned.
761 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
762 /// is true), the PHI node is destroyed and any uses of it are replaced with
763 /// dummy values. The only time there should be zero incoming values to a PHI
764 /// node is when the block is dead, so this strategy is sound.
766 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
768 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
769 int Idx = getBasicBlockIndex(BB);
770 assert(Idx >= 0 && "Invalid basic block argument to remove!");
771 return removeIncomingValue(Idx, DeletePHIIfEmpty);
774 /// getBasicBlockIndex - Return the first index of the specified basic
775 /// block in the value list for this PHI. Returns -1 if no instance.
777 int getBasicBlockIndex(const BasicBlock *BB) const {
778 for (unsigned i = 0; i < Operands.size()/2; ++i)
779 if (getIncomingBlock(i) == BB) return i;
783 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
784 return getIncomingValue(getBasicBlockIndex(BB));
787 /// Methods for support type inquiry through isa, cast, and dyn_cast:
788 static inline bool classof(const PHINode *) { return true; }
789 static inline bool classof(const Instruction *I) {
790 return I->getOpcode() == Instruction::PHI;
792 static inline bool classof(const Value *V) {
793 return isa<Instruction>(V) && classof(cast<Instruction>(V));
797 //===----------------------------------------------------------------------===//
799 //===----------------------------------------------------------------------===//
801 //===---------------------------------------------------------------------------
802 /// ReturnInst - Return a value (possibly void), from a function. Execution
803 /// does not continue in this function any longer.
805 class ReturnInst : public TerminatorInst {
806 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
807 if (RI.Operands.size()) {
808 assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
810 Operands.push_back(Use(RI.Operands[0], this));
814 void init(Value *RetVal) {
816 assert(!isa<BasicBlock>(RetVal) &&
817 "Cannot return basic block. Probably using the incorrect ctor");
819 Operands.push_back(Use(RetVal, this));
824 // ReturnInst constructors:
825 // ReturnInst() - 'ret void' instruction
826 // ReturnInst(Value* X) - 'ret X' instruction
827 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
828 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
829 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
830 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
831 ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
832 : TerminatorInst(Instruction::Ret, InsertBefore) {
835 ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
836 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
839 ReturnInst(BasicBlock *InsertAtEnd)
840 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
843 virtual Instruction *clone() const { return new ReturnInst(*this); }
845 inline const Value *getReturnValue() const {
846 return Operands.size() ? Operands[0].get() : 0;
848 inline Value *getReturnValue() {
849 return Operands.size() ? Operands[0].get() : 0;
852 virtual const BasicBlock *getSuccessor(unsigned idx) const {
853 assert(0 && "ReturnInst has no successors!");
857 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
858 virtual unsigned getNumSuccessors() const { return 0; }
860 // Methods for support type inquiry through isa, cast, and dyn_cast:
861 static inline bool classof(const ReturnInst *) { return true; }
862 static inline bool classof(const Instruction *I) {
863 return (I->getOpcode() == Instruction::Ret);
865 static inline bool classof(const Value *V) {
866 return isa<Instruction>(V) && classof(cast<Instruction>(V));
870 //===----------------------------------------------------------------------===//
872 //===----------------------------------------------------------------------===//
874 //===---------------------------------------------------------------------------
875 /// BranchInst - Conditional or Unconditional Branch instruction.
877 class BranchInst : public TerminatorInst {
878 BranchInst(const BranchInst &BI);
879 void init(BasicBlock *IfTrue);
880 void init(BasicBlock *True, BasicBlock *False, Value *Cond);
882 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
883 // BranchInst(BB *B) - 'br B'
884 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
885 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
886 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
887 // BranchInst(BB* B, BB *I) - 'br B' insert at end
888 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
889 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
890 : TerminatorInst(Instruction::Br, InsertBefore) {
893 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
894 Instruction *InsertBefore = 0)
895 : TerminatorInst(Instruction::Br, InsertBefore) {
896 init(IfTrue, IfFalse, Cond);
899 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
900 : TerminatorInst(Instruction::Br, InsertAtEnd) {
904 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
905 BasicBlock *InsertAtEnd)
906 : TerminatorInst(Instruction::Br, InsertAtEnd) {
907 init(IfTrue, IfFalse, Cond);
910 virtual Instruction *clone() const { return new BranchInst(*this); }
912 inline bool isUnconditional() const { return Operands.size() == 1; }
913 inline bool isConditional() const { return Operands.size() == 3; }
915 inline Value *getCondition() const {
916 assert(isConditional() && "Cannot get condition of an uncond branch!");
917 return Operands[2].get();
920 void setCondition(Value *V) {
921 assert(isConditional() && "Cannot set condition of unconditional branch!");
925 // setUnconditionalDest - Change the current branch to an unconditional branch
926 // targeting the specified block.
928 void setUnconditionalDest(BasicBlock *Dest) {
929 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
930 Operands[0] = reinterpret_cast<Value*>(Dest);
933 virtual const BasicBlock *getSuccessor(unsigned i) const {
934 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
935 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
936 cast<BasicBlock>(Operands[1].get());
938 inline BasicBlock *getSuccessor(unsigned idx) {
939 const BranchInst *BI = this;
940 return const_cast<BasicBlock*>(BI->getSuccessor(idx));
943 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
944 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
945 Operands[idx] = reinterpret_cast<Value*>(NewSucc);
948 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
950 // Methods for support type inquiry through isa, cast, and dyn_cast:
951 static inline bool classof(const BranchInst *) { return true; }
952 static inline bool classof(const Instruction *I) {
953 return (I->getOpcode() == Instruction::Br);
955 static inline bool classof(const Value *V) {
956 return isa<Instruction>(V) && classof(cast<Instruction>(V));
960 //===----------------------------------------------------------------------===//
962 //===----------------------------------------------------------------------===//
964 //===---------------------------------------------------------------------------
965 /// SwitchInst - Multiway switch
967 class SwitchInst : public TerminatorInst {
968 // Operand[0] = Value to switch on
969 // Operand[1] = Default basic block destination
970 // Operand[2n ] = Value to match
971 // Operand[2n+1] = BasicBlock to go to on match
972 SwitchInst(const SwitchInst &RI);
973 void init(Value *Value, BasicBlock *Default);
976 SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0)
977 : TerminatorInst(Instruction::Switch, InsertBefore) {
978 init(Value, Default);
980 SwitchInst(Value *Value, BasicBlock *Default, BasicBlock *InsertAtEnd)
981 : TerminatorInst(Instruction::Switch, InsertAtEnd) {
982 init(Value, Default);
985 virtual Instruction *clone() const { return new SwitchInst(*this); }
987 // Accessor Methods for Switch stmt
989 inline const Value *getCondition() const { return Operands[0]; }
990 inline Value *getCondition() { return Operands[0]; }
991 inline const BasicBlock *getDefaultDest() const {
992 return cast<BasicBlock>(Operands[1].get());
994 inline BasicBlock *getDefaultDest() {
995 return cast<BasicBlock>(Operands[1].get());
998 /// getNumCases - return the number of 'cases' in this switch instruction.
999 /// Note that case #0 is always the default case.
1000 unsigned getNumCases() const {
1001 return Operands.size()/2;
1004 /// getCaseValue - Return the specified case value. Note that case #0, the
1005 /// default destination, does not have a case value.
1006 Constant *getCaseValue(unsigned i) {
1007 assert(i && i < getNumCases() && "Illegal case value to get!");
1008 return getSuccessorValue(i);
1011 /// getCaseValue - Return the specified case value. Note that case #0, the
1012 /// default destination, does not have a case value.
1013 const Constant *getCaseValue(unsigned i) const {
1014 assert(i && i < getNumCases() && "Illegal case value to get!");
1015 return getSuccessorValue(i);
1018 /// findCaseValue - Search all of the case values for the specified constant.
1019 /// If it is explicitly handled, return the case number of it, otherwise
1020 /// return 0 to indicate that it is handled by the default handler.
1021 unsigned findCaseValue(const Constant *C) const {
1022 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1023 if (getCaseValue(i) == C)
1028 /// addCase - Add an entry to the switch instruction...
1030 void addCase(Constant *OnVal, BasicBlock *Dest);
1032 /// removeCase - This method removes the specified successor from the switch
1033 /// instruction. Note that this cannot be used to remove the default
1034 /// destination (successor #0).
1036 void removeCase(unsigned idx);
1038 virtual const BasicBlock *getSuccessor(unsigned idx) const {
1039 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1040 return cast<BasicBlock>(Operands[idx*2+1].get());
1042 inline BasicBlock *getSuccessor(unsigned idx) {
1043 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1044 return cast<BasicBlock>(Operands[idx*2+1].get());
1047 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1048 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1049 Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
1052 // getSuccessorValue - Return the value associated with the specified
1054 inline const Constant *getSuccessorValue(unsigned idx) const {
1055 assert(idx < getNumSuccessors() && "Successor # out of range!");
1056 return cast<Constant>(Operands[idx*2].get());
1058 inline Constant *getSuccessorValue(unsigned idx) {
1059 assert(idx < getNumSuccessors() && "Successor # out of range!");
1060 return cast<Constant>(Operands[idx*2].get());
1062 virtual unsigned getNumSuccessors() const { return Operands.size()/2; }
1064 // Methods for support type inquiry through isa, cast, and dyn_cast:
1065 static inline bool classof(const SwitchInst *) { return true; }
1066 static inline bool classof(const Instruction *I) {
1067 return (I->getOpcode() == Instruction::Switch);
1069 static inline bool classof(const Value *V) {
1070 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1074 //===----------------------------------------------------------------------===//
1076 //===----------------------------------------------------------------------===//
1078 //===---------------------------------------------------------------------------
1079 /// InvokeInst - Invoke instruction
1081 class InvokeInst : public TerminatorInst {
1082 InvokeInst(const InvokeInst &BI);
1083 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1084 const std::vector<Value*> &Params);
1086 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1087 const std::vector<Value*> &Params, const std::string &Name = "",
1088 Instruction *InsertBefore = 0);
1089 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1090 const std::vector<Value*> &Params, const std::string &Name,
1091 BasicBlock *InsertAtEnd);
1093 virtual Instruction *clone() const { return new InvokeInst(*this); }
1095 bool mayWriteToMemory() const { return true; }
1097 /// getCalledFunction - Return the function called, or null if this is an
1098 /// indirect function invocation...
1100 /// FIXME: These should be inlined once we get rid of ConstantPointerRefs!
1102 const Function *getCalledFunction() const;
1103 Function *getCalledFunction();
1105 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1106 inline const Value *getCalledValue() const { return Operands[0]; }
1107 inline Value *getCalledValue() { return Operands[0]; }
1109 // get*Dest - Return the destination basic blocks...
1110 inline const BasicBlock *getNormalDest() const {
1111 return cast<BasicBlock>(Operands[1].get());
1113 inline BasicBlock *getNormalDest() {
1114 return cast<BasicBlock>(Operands[1].get());
1116 inline const BasicBlock *getUnwindDest() const {
1117 return cast<BasicBlock>(Operands[2].get());
1119 inline BasicBlock *getUnwindDest() {
1120 return cast<BasicBlock>(Operands[2].get());
1123 inline void setNormalDest(BasicBlock *B){
1124 Operands[1] = reinterpret_cast<Value*>(B);
1127 inline void setUnwindDest(BasicBlock *B){
1128 Operands[2] = reinterpret_cast<Value*>(B);
1131 virtual const BasicBlock *getSuccessor(unsigned i) const {
1132 assert(i < 2 && "Successor # out of range for invoke!");
1133 return i == 0 ? getNormalDest() : getUnwindDest();
1135 inline BasicBlock *getSuccessor(unsigned i) {
1136 assert(i < 2 && "Successor # out of range for invoke!");
1137 return i == 0 ? getNormalDest() : getUnwindDest();
1140 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1141 assert(idx < 2 && "Successor # out of range for invoke!");
1142 Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
1145 virtual unsigned getNumSuccessors() const { return 2; }
1147 // Methods for support type inquiry through isa, cast, and dyn_cast:
1148 static inline bool classof(const InvokeInst *) { return true; }
1149 static inline bool classof(const Instruction *I) {
1150 return (I->getOpcode() == Instruction::Invoke);
1152 static inline bool classof(const Value *V) {
1153 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1158 //===----------------------------------------------------------------------===//
1160 //===----------------------------------------------------------------------===//
1162 //===---------------------------------------------------------------------------
1163 /// UnwindInst - Immediately exit the current function, unwinding the stack
1164 /// until an invoke instruction is found.
1166 struct UnwindInst : public TerminatorInst {
1167 UnwindInst(Instruction *InsertBefore = 0)
1168 : TerminatorInst(Instruction::Unwind, InsertBefore) {
1170 UnwindInst(BasicBlock *InsertAtEnd)
1171 : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
1174 virtual Instruction *clone() const { return new UnwindInst(); }
1176 virtual const BasicBlock *getSuccessor(unsigned idx) const {
1177 assert(0 && "UnwindInst has no successors!");
1181 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
1182 virtual unsigned getNumSuccessors() const { return 0; }
1184 // Methods for support type inquiry through isa, cast, and dyn_cast:
1185 static inline bool classof(const UnwindInst *) { return true; }
1186 static inline bool classof(const Instruction *I) {
1187 return I->getOpcode() == Instruction::Unwind;
1189 static inline bool classof(const Value *V) {
1190 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1194 } // End llvm namespace