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 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 Instruction {
147 void init(Value *Ptr);
150 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
151 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
153 virtual FreeInst *clone() const;
155 virtual bool mayWriteToMemory() const { return true; }
157 // Methods for support type inquiry through isa, cast, and dyn_cast:
158 static inline bool classof(const FreeInst *) { return true; }
159 static inline bool classof(const Instruction *I) {
160 return (I->getOpcode() == Instruction::Free);
162 static inline bool classof(const Value *V) {
163 return isa<Instruction>(V) && classof(cast<Instruction>(V));
168 //===----------------------------------------------------------------------===//
170 //===----------------------------------------------------------------------===//
172 /// LoadInst - an instruction for reading from memory
174 class LoadInst : public Instruction {
175 LoadInst(const LoadInst &LI) : Instruction(LI.getType(), Load) {
176 Volatile = LI.isVolatile();
177 init(LI.Operands[0]);
179 bool Volatile; // True if this is a volatile load
180 void init(Value *Ptr);
182 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
183 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
184 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
185 Instruction *InsertBefore = 0);
186 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
187 BasicBlock *InsertAtEnd);
189 /// isVolatile - Return true if this is a load from a volatile memory
192 bool isVolatile() const { return Volatile; }
194 /// setVolatile - Specify whether this is a volatile load or not.
196 void setVolatile(bool V) { Volatile = V; }
198 virtual LoadInst *clone() const;
200 virtual bool mayWriteToMemory() const { return isVolatile(); }
202 Value *getPointerOperand() { return getOperand(0); }
203 const Value *getPointerOperand() const { return getOperand(0); }
204 static unsigned getPointerOperandIndex() { return 0U; }
206 // Methods for support type inquiry through isa, cast, and dyn_cast:
207 static inline bool classof(const LoadInst *) { return true; }
208 static inline bool classof(const Instruction *I) {
209 return I->getOpcode() == Instruction::Load;
211 static inline bool classof(const Value *V) {
212 return isa<Instruction>(V) && classof(cast<Instruction>(V));
217 //===----------------------------------------------------------------------===//
219 //===----------------------------------------------------------------------===//
221 /// StoreInst - an instruction for storing to memory
223 class StoreInst : public Instruction {
224 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store) {
225 Volatile = SI.isVolatile();
226 init(SI.Operands[0], SI.Operands[1]);
228 bool Volatile; // True if this is a volatile store
229 void init(Value *Val, Value *Ptr);
231 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
232 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
233 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
234 Instruction *InsertBefore = 0);
235 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
238 /// isVolatile - Return true if this is a load from a volatile memory
241 bool isVolatile() const { return Volatile; }
243 /// setVolatile - Specify whether this is a volatile load or not.
245 void setVolatile(bool V) { Volatile = V; }
247 virtual StoreInst *clone() const;
249 virtual bool mayWriteToMemory() const { return true; }
251 Value *getPointerOperand() { return getOperand(1); }
252 const Value *getPointerOperand() const { return getOperand(1); }
253 static unsigned getPointerOperandIndex() { return 1U; }
255 // Methods for support type inquiry through isa, cast, and dyn_cast:
256 static inline bool classof(const StoreInst *) { return true; }
257 static inline bool classof(const Instruction *I) {
258 return I->getOpcode() == Instruction::Store;
260 static inline bool classof(const Value *V) {
261 return isa<Instruction>(V) && classof(cast<Instruction>(V));
266 //===----------------------------------------------------------------------===//
267 // GetElementPtrInst Class
268 //===----------------------------------------------------------------------===//
270 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
271 /// access elements of arrays and structs
273 class GetElementPtrInst : public Instruction {
274 GetElementPtrInst(const GetElementPtrInst &EPI)
275 : Instruction((static_cast<const Instruction*>(&EPI)->getType()),
277 Operands.reserve(EPI.Operands.size());
278 for (unsigned i = 0, E = (unsigned)EPI.Operands.size(); i != E; ++i)
279 Operands.push_back(Use(EPI.Operands[i], this));
281 void init(Value *Ptr, const std::vector<Value*> &Idx);
282 void init(Value *Ptr, Value *Idx0, Value *Idx1);
284 /// Constructors - Create a getelementptr instruction with a base pointer an
285 /// list of indices. The first ctor can optionally insert before an existing
286 /// instruction, the second appends the new instruction to the specified
288 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
289 const std::string &Name = "", Instruction *InsertBefore =0);
290 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
291 const std::string &Name, BasicBlock *InsertAtEnd);
293 /// Constructors - These two constructors are convenience methods because two
294 /// index getelementptr instructions are so common.
295 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
296 const std::string &Name = "", Instruction *InsertBefore =0);
297 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
298 const std::string &Name, BasicBlock *InsertAtEnd);
300 virtual GetElementPtrInst *clone() const;
302 // getType - Overload to return most specific pointer type...
303 inline const PointerType *getType() const {
304 return reinterpret_cast<const PointerType*>(Instruction::getType());
307 /// getIndexedType - Returns the type of the element that would be loaded with
308 /// a load instruction with the specified parameters.
310 /// A null type is returned if the indices are invalid for the specified
313 static const Type *getIndexedType(const Type *Ptr,
314 const std::vector<Value*> &Indices,
315 bool AllowStructLeaf = false);
316 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
317 bool AllowStructLeaf = false);
319 inline op_iterator idx_begin() { return op_begin()+1; }
320 inline const_op_iterator idx_begin() const { return op_begin()+1; }
321 inline op_iterator idx_end() { return op_end(); }
322 inline const_op_iterator idx_end() const { return op_end(); }
324 Value *getPointerOperand() {
325 return getOperand(0);
327 const Value *getPointerOperand() const {
328 return getOperand(0);
330 static unsigned getPointerOperandIndex() {
331 return 0U; // get index for modifying correct operand
334 inline unsigned getNumIndices() const { // Note: always non-negative
335 return getNumOperands() - 1;
338 inline bool hasIndices() const {
339 return getNumOperands() > 1;
342 // Methods for support type inquiry through isa, cast, and dyn_cast:
343 static inline bool classof(const GetElementPtrInst *) { return true; }
344 static inline bool classof(const Instruction *I) {
345 return (I->getOpcode() == Instruction::GetElementPtr);
347 static inline bool classof(const Value *V) {
348 return isa<Instruction>(V) && classof(cast<Instruction>(V));
352 //===----------------------------------------------------------------------===//
354 //===----------------------------------------------------------------------===//
356 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
359 class SetCondInst : public BinaryOperator {
362 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
363 const std::string &Name = "", Instruction *InsertBefore = 0);
364 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
365 const std::string &Name, BasicBlock *InsertAtEnd);
367 /// getInverseCondition - Return the inverse of the current condition opcode.
368 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
370 BinaryOps getInverseCondition() const {
371 return getInverseCondition(getOpcode());
374 /// getInverseCondition - Static version that you can use without an
375 /// instruction available.
377 static BinaryOps getInverseCondition(BinaryOps Opcode);
379 /// getSwappedCondition - Return the condition opcode that would be the result
380 /// of exchanging the two operands of the setcc instruction without changing
381 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
383 BinaryOps getSwappedCondition() const {
384 return getSwappedCondition(getOpcode());
387 /// getSwappedCondition - Static version that you can use without an
388 /// instruction available.
390 static BinaryOps getSwappedCondition(BinaryOps Opcode);
393 // Methods for support type inquiry through isa, cast, and dyn_cast:
394 static inline bool classof(const SetCondInst *) { return true; }
395 static inline bool classof(const Instruction *I) {
396 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
397 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
398 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
400 static inline bool classof(const Value *V) {
401 return isa<Instruction>(V) && classof(cast<Instruction>(V));
405 //===----------------------------------------------------------------------===//
407 //===----------------------------------------------------------------------===//
409 /// CastInst - This class represents a cast from Operand[0] to the type of
410 /// the instruction (i->getType()).
412 class CastInst : public Instruction {
413 CastInst(const CastInst &CI) : Instruction(CI.getType(), Cast) {
415 Operands.push_back(Use(CI.Operands[0], this));
417 void init(Value *S) {
419 Operands.push_back(Use(S, this));
422 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
423 Instruction *InsertBefore = 0)
424 : Instruction(Ty, Cast, Name, InsertBefore) {
427 CastInst(Value *S, const Type *Ty, const std::string &Name,
428 BasicBlock *InsertAtEnd)
429 : Instruction(Ty, Cast, Name, InsertAtEnd) {
433 virtual CastInst *clone() const;
435 // Methods for support type inquiry through isa, cast, and dyn_cast:
436 static inline bool classof(const CastInst *) { return true; }
437 static inline bool classof(const Instruction *I) {
438 return I->getOpcode() == Cast;
440 static inline bool classof(const Value *V) {
441 return isa<Instruction>(V) && classof(cast<Instruction>(V));
446 //===----------------------------------------------------------------------===//
448 //===----------------------------------------------------------------------===//
450 /// CallInst - This class represents a function call, abstracting a target
451 /// machine's calling convention.
453 class CallInst : public Instruction {
454 CallInst(const CallInst &CI);
455 void init(Value *Func, const std::vector<Value*> &Params);
456 void init(Value *Func, Value *Actual1, Value *Actual2);
457 void init(Value *Func, Value *Actual);
458 void init(Value *Func);
461 CallInst(Value *F, const std::vector<Value*> &Par,
462 const std::string &Name = "", Instruction *InsertBefore = 0);
463 CallInst(Value *F, const std::vector<Value*> &Par,
464 const std::string &Name, BasicBlock *InsertAtEnd);
466 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
467 // actuals, respectively.
468 CallInst(Value *F, Value *Actual1, Value *Actual2,
469 const std::string& Name = "", Instruction *InsertBefore = 0);
470 CallInst(Value *F, Value *Actual1, Value *Actual2,
471 const std::string& Name, BasicBlock *InsertAtEnd);
472 CallInst(Value *F, Value *Actual, const std::string& Name = "",
473 Instruction *InsertBefore = 0);
474 CallInst(Value *F, Value *Actual, const std::string& Name,
475 BasicBlock *InsertAtEnd);
476 explicit CallInst(Value *F, const std::string &Name = "",
477 Instruction *InsertBefore = 0);
478 explicit CallInst(Value *F, const std::string &Name,
479 BasicBlock *InsertAtEnd);
481 virtual CallInst *clone() const;
482 bool mayWriteToMemory() const { return true; }
484 /// getCalledFunction - Return the function being called by this instruction
485 /// if it is a direct call. If it is a call through a function pointer,
487 Function *getCalledFunction() const {
488 return dyn_cast<Function>(Operands[0]);
491 // getCalledValue - Get a pointer to a method that is invoked by this inst.
492 inline const Value *getCalledValue() const { return Operands[0]; }
493 inline Value *getCalledValue() { return Operands[0]; }
495 // Methods for support type inquiry through isa, cast, and dyn_cast:
496 static inline bool classof(const CallInst *) { return true; }
497 static inline bool classof(const Instruction *I) {
498 return I->getOpcode() == Instruction::Call;
500 static inline bool classof(const Value *V) {
501 return isa<Instruction>(V) && classof(cast<Instruction>(V));
506 //===----------------------------------------------------------------------===//
508 //===----------------------------------------------------------------------===//
510 /// ShiftInst - This class represents left and right shift instructions.
512 class ShiftInst : public Instruction {
513 ShiftInst(const ShiftInst &SI) : Instruction(SI.getType(), SI.getOpcode()) {
515 Operands.push_back(Use(SI.Operands[0], this));
516 Operands.push_back(Use(SI.Operands[1], this));
518 void init(OtherOps Opcode, Value *S, Value *SA) {
519 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
521 Operands.push_back(Use(S, this));
522 Operands.push_back(Use(SA, this));
526 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
527 Instruction *InsertBefore = 0)
528 : Instruction(S->getType(), Opcode, Name, InsertBefore) {
531 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
532 BasicBlock *InsertAtEnd)
533 : Instruction(S->getType(), Opcode, Name, InsertAtEnd) {
537 OtherOps getOpcode() const {
538 return static_cast<OtherOps>(Instruction::getOpcode());
541 virtual ShiftInst *clone() const;
543 // Methods for support type inquiry through isa, cast, and dyn_cast:
544 static inline bool classof(const ShiftInst *) { return true; }
545 static inline bool classof(const Instruction *I) {
546 return (I->getOpcode() == Instruction::Shr) |
547 (I->getOpcode() == Instruction::Shl);
549 static inline bool classof(const Value *V) {
550 return isa<Instruction>(V) && classof(cast<Instruction>(V));
554 //===----------------------------------------------------------------------===//
556 //===----------------------------------------------------------------------===//
558 /// SelectInst - This class represents the LLVM 'select' instruction.
560 class SelectInst : public Instruction {
561 SelectInst(const SelectInst &SI) : Instruction(SI.getType(), SI.getOpcode()) {
563 Operands.push_back(Use(SI.Operands[0], this));
564 Operands.push_back(Use(SI.Operands[1], this));
565 Operands.push_back(Use(SI.Operands[2], this));
567 void init(Value *C, Value *S1, Value *S2) {
569 Operands.push_back(Use(C, this));
570 Operands.push_back(Use(S1, this));
571 Operands.push_back(Use(S2, this));
575 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
576 Instruction *InsertBefore = 0)
577 : Instruction(S1->getType(), Instruction::Select, Name, InsertBefore) {
580 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
581 BasicBlock *InsertAtEnd)
582 : Instruction(S1->getType(), Instruction::Select, Name, InsertAtEnd) {
586 Value *getCondition() const { return Operands[0]; }
587 Value *getTrueValue() const { return Operands[1]; }
588 Value *getFalseValue() const { return Operands[2]; }
590 OtherOps getOpcode() const {
591 return static_cast<OtherOps>(Instruction::getOpcode());
594 virtual SelectInst *clone() const;
596 // Methods for support type inquiry through isa, cast, and dyn_cast:
597 static inline bool classof(const SelectInst *) { return true; }
598 static inline bool classof(const Instruction *I) {
599 return I->getOpcode() == Instruction::Select;
601 static inline bool classof(const Value *V) {
602 return isa<Instruction>(V) && classof(cast<Instruction>(V));
607 //===----------------------------------------------------------------------===//
609 //===----------------------------------------------------------------------===//
611 /// VANextInst - This class represents the va_next llvm instruction, which
612 /// advances a vararg list passed an argument of the specified type, returning
613 /// the resultant list.
615 class VANextInst : public Instruction {
617 void init(Value *List) {
619 Operands.push_back(Use(List, this));
621 VANextInst(const VANextInst &VAN)
622 : Instruction(VAN.getType(), VANext), ArgTy(VAN.getArgType()) {
623 init(VAN.Operands[0]);
627 VANextInst(Value *List, const Type *Ty, const std::string &Name = "",
628 Instruction *InsertBefore = 0)
629 : Instruction(List->getType(), VANext, Name, InsertBefore), ArgTy(Ty) {
632 VANextInst(Value *List, const Type *Ty, const std::string &Name,
633 BasicBlock *InsertAtEnd)
634 : Instruction(List->getType(), VANext, Name, InsertAtEnd), ArgTy(Ty) {
638 const Type *getArgType() const { return ArgTy; }
640 virtual VANextInst *clone() const;
642 // Methods for support type inquiry through isa, cast, and dyn_cast:
643 static inline bool classof(const VANextInst *) { return true; }
644 static inline bool classof(const Instruction *I) {
645 return I->getOpcode() == VANext;
647 static inline bool classof(const Value *V) {
648 return isa<Instruction>(V) && classof(cast<Instruction>(V));
653 //===----------------------------------------------------------------------===//
655 //===----------------------------------------------------------------------===//
657 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
658 /// an argument of the specified type given a va_list.
660 class VAArgInst : public Instruction {
661 void init(Value* List) {
663 Operands.push_back(Use(List, this));
665 VAArgInst(const VAArgInst &VAA)
666 : Instruction(VAA.getType(), VAArg) {
667 init(VAA.Operands[0]);
670 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
671 Instruction *InsertBefore = 0)
672 : Instruction(Ty, VAArg, Name, InsertBefore) {
675 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
676 BasicBlock *InsertAtEnd)
677 : Instruction(Ty, VAArg, Name, InsertAtEnd) {
681 virtual VAArgInst *clone() const;
683 // Methods for support type inquiry through isa, cast, and dyn_cast:
684 static inline bool classof(const VAArgInst *) { return true; }
685 static inline bool classof(const Instruction *I) {
686 return I->getOpcode() == VAArg;
688 static inline bool classof(const Value *V) {
689 return isa<Instruction>(V) && classof(cast<Instruction>(V));
693 //===----------------------------------------------------------------------===//
695 //===----------------------------------------------------------------------===//
697 // PHINode - The PHINode class is used to represent the magical mystical PHI
698 // node, that can not exist in nature, but can be synthesized in a computer
699 // scientist's overactive imagination.
701 class PHINode : public Instruction {
702 PHINode(const PHINode &PN);
704 PHINode(const Type *Ty, const std::string &Name = "",
705 Instruction *InsertBefore = 0)
706 : Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
709 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
710 : Instruction(Ty, Instruction::PHI, Name, InsertAtEnd) {
713 virtual PHINode *clone() const;
715 /// getNumIncomingValues - Return the number of incoming edges
717 unsigned getNumIncomingValues() const { return (unsigned)Operands.size()/2; }
719 /// getIncomingValue - Return incoming value #x
721 Value *getIncomingValue(unsigned i) const {
722 assert(i*2 < Operands.size() && "Invalid value number!");
723 return Operands[i*2];
725 void setIncomingValue(unsigned i, Value *V) {
726 assert(i*2 < Operands.size() && "Invalid value number!");
729 inline unsigned getOperandNumForIncomingValue(unsigned i) {
733 /// getIncomingBlock - Return incoming basic block #x
735 BasicBlock *getIncomingBlock(unsigned i) const {
736 assert(i*2+1 < Operands.size() && "Invalid value number!");
737 return reinterpret_cast<BasicBlock*>(Operands[i*2+1].get());
739 void setIncomingBlock(unsigned i, BasicBlock *BB) {
740 assert(i*2+1 < Operands.size() && "Invalid value number!");
741 Operands[i*2+1] = reinterpret_cast<Value*>(BB);
743 unsigned getOperandNumForIncomingBlock(unsigned i) {
747 /// addIncoming - Add an incoming value to the end of the PHI list
749 void addIncoming(Value *V, BasicBlock *BB) {
750 assert(getType() == V->getType() &&
751 "All operands to PHI node must be the same type as the PHI node!");
752 Operands.push_back(Use(V, this));
753 Operands.push_back(Use(reinterpret_cast<Value*>(BB), this));
756 /// removeIncomingValue - Remove an incoming value. This is useful if a
757 /// predecessor basic block is deleted. The value removed is returned.
759 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
760 /// is true), the PHI node is destroyed and any uses of it are replaced with
761 /// dummy values. The only time there should be zero incoming values to a PHI
762 /// node is when the block is dead, so this strategy is sound.
764 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
766 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
767 int Idx = getBasicBlockIndex(BB);
768 assert(Idx >= 0 && "Invalid basic block argument to remove!");
769 return removeIncomingValue(Idx, DeletePHIIfEmpty);
772 /// getBasicBlockIndex - Return the first index of the specified basic
773 /// block in the value list for this PHI. Returns -1 if no instance.
775 int getBasicBlockIndex(const BasicBlock *BB) const {
776 for (unsigned i = 0; i < Operands.size()/2; ++i)
777 if (getIncomingBlock(i) == BB) return i;
781 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
782 return getIncomingValue(getBasicBlockIndex(BB));
785 /// Methods for support type inquiry through isa, cast, and dyn_cast:
786 static inline bool classof(const PHINode *) { return true; }
787 static inline bool classof(const Instruction *I) {
788 return I->getOpcode() == Instruction::PHI;
790 static inline bool classof(const Value *V) {
791 return isa<Instruction>(V) && classof(cast<Instruction>(V));
795 //===----------------------------------------------------------------------===//
797 //===----------------------------------------------------------------------===//
799 //===---------------------------------------------------------------------------
800 /// ReturnInst - Return a value (possibly void), from a function. Execution
801 /// does not continue in this function any longer.
803 class ReturnInst : public TerminatorInst {
804 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret) {
805 if (RI.Operands.size()) {
806 assert(RI.Operands.size() == 1 && "Return insn can only have 1 operand!");
808 Operands.push_back(Use(RI.Operands[0], this));
812 void init(Value *RetVal);
815 // ReturnInst constructors:
816 // ReturnInst() - 'ret void' instruction
817 // ReturnInst( null) - 'ret void' instruction
818 // ReturnInst(Value* X) - 'ret X' instruction
819 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
820 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
821 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
822 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
824 // NOTE: If the Value* passed is of type void then the constructor behaves as
825 // if it was passed NULL.
826 ReturnInst(Value *RetVal = 0, Instruction *InsertBefore = 0)
827 : TerminatorInst(Instruction::Ret, InsertBefore) {
830 ReturnInst(Value *RetVal, BasicBlock *InsertAtEnd)
831 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
834 ReturnInst(BasicBlock *InsertAtEnd)
835 : TerminatorInst(Instruction::Ret, InsertAtEnd) {
838 virtual ReturnInst *clone() const;
840 inline const Value *getReturnValue() const {
841 return Operands.size() ? Operands[0].get() : 0;
843 inline Value *getReturnValue() {
844 return Operands.size() ? Operands[0].get() : 0;
847 virtual const BasicBlock *getSuccessor(unsigned idx) const {
848 assert(0 && "ReturnInst has no successors!");
852 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
853 virtual unsigned getNumSuccessors() const { return 0; }
855 // Methods for support type inquiry through isa, cast, and dyn_cast:
856 static inline bool classof(const ReturnInst *) { return true; }
857 static inline bool classof(const Instruction *I) {
858 return (I->getOpcode() == Instruction::Ret);
860 static inline bool classof(const Value *V) {
861 return isa<Instruction>(V) && classof(cast<Instruction>(V));
865 //===----------------------------------------------------------------------===//
867 //===----------------------------------------------------------------------===//
869 //===---------------------------------------------------------------------------
870 /// BranchInst - Conditional or Unconditional Branch instruction.
872 class BranchInst : public TerminatorInst {
873 BranchInst(const BranchInst &BI);
874 void init(BasicBlock *IfTrue);
875 void init(BasicBlock *True, BasicBlock *False, Value *Cond);
877 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
878 // BranchInst(BB *B) - 'br B'
879 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
880 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
881 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
882 // BranchInst(BB* B, BB *I) - 'br B' insert at end
883 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
884 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
885 : TerminatorInst(Instruction::Br, InsertBefore) {
888 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
889 Instruction *InsertBefore = 0)
890 : TerminatorInst(Instruction::Br, InsertBefore) {
891 init(IfTrue, IfFalse, Cond);
894 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
895 : TerminatorInst(Instruction::Br, InsertAtEnd) {
899 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
900 BasicBlock *InsertAtEnd)
901 : TerminatorInst(Instruction::Br, InsertAtEnd) {
902 init(IfTrue, IfFalse, Cond);
905 virtual BranchInst *clone() const;
907 inline bool isUnconditional() const { return Operands.size() == 1; }
908 inline bool isConditional() const { return Operands.size() == 3; }
910 inline Value *getCondition() const {
911 assert(isConditional() && "Cannot get condition of an uncond branch!");
912 return Operands[2].get();
915 void setCondition(Value *V) {
916 assert(isConditional() && "Cannot set condition of unconditional branch!");
920 // setUnconditionalDest - Change the current branch to an unconditional branch
921 // targeting the specified block.
923 void setUnconditionalDest(BasicBlock *Dest) {
924 if (isConditional()) Operands.erase(Operands.begin()+1, Operands.end());
925 Operands[0] = reinterpret_cast<Value*>(Dest);
928 virtual const BasicBlock *getSuccessor(unsigned i) const {
929 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
930 return (i == 0) ? cast<BasicBlock>(Operands[0].get()) :
931 cast<BasicBlock>(Operands[1].get());
933 inline BasicBlock *getSuccessor(unsigned idx) {
934 const BranchInst *BI = this;
935 return const_cast<BasicBlock*>(BI->getSuccessor(idx));
938 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
939 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
940 Operands[idx] = reinterpret_cast<Value*>(NewSucc);
943 virtual unsigned getNumSuccessors() const { return 1+isConditional(); }
945 // Methods for support type inquiry through isa, cast, and dyn_cast:
946 static inline bool classof(const BranchInst *) { return true; }
947 static inline bool classof(const Instruction *I) {
948 return (I->getOpcode() == Instruction::Br);
950 static inline bool classof(const Value *V) {
951 return isa<Instruction>(V) && classof(cast<Instruction>(V));
955 //===----------------------------------------------------------------------===//
957 //===----------------------------------------------------------------------===//
959 //===---------------------------------------------------------------------------
960 /// SwitchInst - Multiway switch
962 class SwitchInst : public TerminatorInst {
963 // Operand[0] = Value to switch on
964 // Operand[1] = Default basic block destination
965 // Operand[2n ] = Value to match
966 // Operand[2n+1] = BasicBlock to go to on match
967 SwitchInst(const SwitchInst &RI);
968 void init(Value *Value, BasicBlock *Default);
971 SwitchInst(Value *Value, BasicBlock *Default, Instruction *InsertBefore = 0)
972 : TerminatorInst(Instruction::Switch, InsertBefore) {
973 init(Value, Default);
975 SwitchInst(Value *Value, BasicBlock *Default, BasicBlock *InsertAtEnd)
976 : TerminatorInst(Instruction::Switch, InsertAtEnd) {
977 init(Value, Default);
980 virtual SwitchInst *clone() const;
982 // Accessor Methods for Switch stmt
984 inline const Value *getCondition() const { return Operands[0]; }
985 inline Value *getCondition() { return Operands[0]; }
986 inline const BasicBlock *getDefaultDest() const {
987 return cast<BasicBlock>(Operands[1].get());
989 inline BasicBlock *getDefaultDest() {
990 return cast<BasicBlock>(Operands[1].get());
993 /// getNumCases - return the number of 'cases' in this switch instruction.
994 /// Note that case #0 is always the default case.
995 unsigned getNumCases() const {
996 return (unsigned)Operands.size()/2;
999 /// getCaseValue - Return the specified case value. Note that case #0, the
1000 /// default destination, does not have a case value.
1001 Constant *getCaseValue(unsigned i) {
1002 assert(i && i < getNumCases() && "Illegal case value to get!");
1003 return getSuccessorValue(i);
1006 /// getCaseValue - Return the specified case value. Note that case #0, the
1007 /// default destination, does not have a case value.
1008 const Constant *getCaseValue(unsigned i) const {
1009 assert(i && i < getNumCases() && "Illegal case value to get!");
1010 return getSuccessorValue(i);
1013 /// findCaseValue - Search all of the case values for the specified constant.
1014 /// If it is explicitly handled, return the case number of it, otherwise
1015 /// return 0 to indicate that it is handled by the default handler.
1016 unsigned findCaseValue(const Constant *C) const {
1017 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1018 if (getCaseValue(i) == C)
1023 /// addCase - Add an entry to the switch instruction...
1025 void addCase(Constant *OnVal, BasicBlock *Dest);
1027 /// removeCase - This method removes the specified successor from the switch
1028 /// instruction. Note that this cannot be used to remove the default
1029 /// destination (successor #0).
1031 void removeCase(unsigned idx);
1033 virtual const BasicBlock *getSuccessor(unsigned idx) const {
1034 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1035 return cast<BasicBlock>(Operands[idx*2+1].get());
1037 inline BasicBlock *getSuccessor(unsigned idx) {
1038 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1039 return cast<BasicBlock>(Operands[idx*2+1].get());
1042 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1043 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1044 Operands[idx*2+1] = reinterpret_cast<Value*>(NewSucc);
1047 // getSuccessorValue - Return the value associated with the specified
1049 inline const Constant *getSuccessorValue(unsigned idx) const {
1050 assert(idx < getNumSuccessors() && "Successor # out of range!");
1051 return cast<Constant>(Operands[idx*2].get());
1053 inline Constant *getSuccessorValue(unsigned idx) {
1054 assert(idx < getNumSuccessors() && "Successor # out of range!");
1055 return cast<Constant>(Operands[idx*2].get());
1057 virtual unsigned getNumSuccessors() const { return (unsigned)Operands.size()/2; }
1059 // Methods for support type inquiry through isa, cast, and dyn_cast:
1060 static inline bool classof(const SwitchInst *) { return true; }
1061 static inline bool classof(const Instruction *I) {
1062 return (I->getOpcode() == Instruction::Switch);
1064 static inline bool classof(const Value *V) {
1065 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1069 //===----------------------------------------------------------------------===//
1071 //===----------------------------------------------------------------------===//
1073 //===---------------------------------------------------------------------------
1074 /// InvokeInst - Invoke instruction
1076 class InvokeInst : public TerminatorInst {
1077 InvokeInst(const InvokeInst &BI);
1078 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1079 const std::vector<Value*> &Params);
1081 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1082 const std::vector<Value*> &Params, const std::string &Name = "",
1083 Instruction *InsertBefore = 0);
1084 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1085 const std::vector<Value*> &Params, const std::string &Name,
1086 BasicBlock *InsertAtEnd);
1088 virtual InvokeInst *clone() const;
1090 bool mayWriteToMemory() const { return true; }
1092 /// getCalledFunction - Return the function called, or null if this is an
1093 /// indirect function invocation.
1095 Function *getCalledFunction() const {
1096 return dyn_cast<Function>(Operands[0]);
1099 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1100 inline const Value *getCalledValue() const { return Operands[0]; }
1101 inline Value *getCalledValue() { return Operands[0]; }
1103 // get*Dest - Return the destination basic blocks...
1104 inline const BasicBlock *getNormalDest() const {
1105 return cast<BasicBlock>(Operands[1].get());
1107 inline BasicBlock *getNormalDest() {
1108 return cast<BasicBlock>(Operands[1].get());
1110 inline const BasicBlock *getUnwindDest() const {
1111 return cast<BasicBlock>(Operands[2].get());
1113 inline BasicBlock *getUnwindDest() {
1114 return cast<BasicBlock>(Operands[2].get());
1117 inline void setNormalDest(BasicBlock *B){
1118 Operands[1] = reinterpret_cast<Value*>(B);
1121 inline void setUnwindDest(BasicBlock *B){
1122 Operands[2] = reinterpret_cast<Value*>(B);
1125 virtual const BasicBlock *getSuccessor(unsigned i) const {
1126 assert(i < 2 && "Successor # out of range for invoke!");
1127 return i == 0 ? getNormalDest() : getUnwindDest();
1129 inline BasicBlock *getSuccessor(unsigned i) {
1130 assert(i < 2 && "Successor # out of range for invoke!");
1131 return i == 0 ? getNormalDest() : getUnwindDest();
1134 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1135 assert(idx < 2 && "Successor # out of range for invoke!");
1136 Operands[idx+1] = reinterpret_cast<Value*>(NewSucc);
1139 virtual unsigned getNumSuccessors() const { return 2; }
1141 // Methods for support type inquiry through isa, cast, and dyn_cast:
1142 static inline bool classof(const InvokeInst *) { return true; }
1143 static inline bool classof(const Instruction *I) {
1144 return (I->getOpcode() == Instruction::Invoke);
1146 static inline bool classof(const Value *V) {
1147 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1152 //===----------------------------------------------------------------------===//
1154 //===----------------------------------------------------------------------===//
1156 //===---------------------------------------------------------------------------
1157 /// UnwindInst - Immediately exit the current function, unwinding the stack
1158 /// until an invoke instruction is found.
1160 class UnwindInst : public TerminatorInst {
1162 UnwindInst(Instruction *InsertBefore = 0)
1163 : TerminatorInst(Instruction::Unwind, InsertBefore) {
1165 UnwindInst(BasicBlock *InsertAtEnd)
1166 : TerminatorInst(Instruction::Unwind, InsertAtEnd) {
1169 virtual UnwindInst *clone() const;
1171 virtual const BasicBlock *getSuccessor(unsigned idx) const {
1172 assert(0 && "UnwindInst has no successors!");
1176 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
1177 virtual unsigned getNumSuccessors() const { return 0; }
1179 // Methods for support type inquiry through isa, cast, and dyn_cast:
1180 static inline bool classof(const UnwindInst *) { return true; }
1181 static inline bool classof(const Instruction *I) {
1182 return I->getOpcode() == Instruction::Unwind;
1184 static inline bool classof(const Value *V) {
1185 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1189 //===----------------------------------------------------------------------===//
1190 // UnreachableInst Class
1191 //===----------------------------------------------------------------------===//
1193 //===---------------------------------------------------------------------------
1194 /// UnreachableInst - This function has undefined behavior. In particular, the
1195 /// presence of this instruction indicates some higher level knowledge that the
1196 /// end of the block cannot be reached.
1198 class UnreachableInst : public TerminatorInst {
1200 UnreachableInst(Instruction *InsertBefore = 0)
1201 : TerminatorInst(Instruction::Unreachable, InsertBefore) {
1203 UnreachableInst(BasicBlock *InsertAtEnd)
1204 : TerminatorInst(Instruction::Unreachable, InsertAtEnd) {
1207 virtual UnreachableInst *clone() const;
1209 virtual const BasicBlock *getSuccessor(unsigned idx) const {
1210 assert(0 && "UnreachableInst has no successors!");
1214 virtual void setSuccessor(unsigned idx, BasicBlock *NewSucc);
1215 virtual unsigned getNumSuccessors() const { return 0; }
1217 // Methods for support type inquiry through isa, cast, and dyn_cast:
1218 static inline bool classof(const UnreachableInst *) { return true; }
1219 static inline bool classof(const Instruction *I) {
1220 return I->getOpcode() == Instruction::Unreachable;
1222 static inline bool classof(const Value *V) {
1223 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1227 } // End llvm namespace