1 //===-- llvm/Instructions.h - Instruction subclass definitions --*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file exposes the class definitions of all of the subclasses of the
11 // Instruction class. This is meant to be an easy way to get access to all
12 // instruction subclasses.
14 //===----------------------------------------------------------------------===//
16 #ifndef LLVM_INSTRUCTIONS_H
17 #define LLVM_INSTRUCTIONS_H
19 #include "llvm/Instruction.h"
20 #include "llvm/InstrTypes.h"
28 //===----------------------------------------------------------------------===//
29 // AllocationInst Class
30 //===----------------------------------------------------------------------===//
32 /// AllocationInst - This class is the common base class of MallocInst and
35 class AllocationInst : public UnaryInstruction {
38 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
39 const std::string &Name = "", Instruction *InsertBefore = 0);
40 AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy, unsigned Align,
41 const std::string &Name, BasicBlock *InsertAtEnd);
45 /// isArrayAllocation - Return true if there is an allocation size parameter
46 /// to the allocation instruction that is not 1.
48 bool isArrayAllocation() const;
50 /// getArraySize - Get the number of element allocated, for a simple
51 /// allocation of a single element, this will return a constant 1 value.
53 inline const Value *getArraySize() const { return getOperand(0); }
54 inline Value *getArraySize() { return getOperand(0); }
56 /// getType - Overload to return most specific pointer type
58 inline const PointerType *getType() const {
59 return reinterpret_cast<const PointerType*>(Instruction::getType());
62 /// getAllocatedType - Return the type that is being allocated by the
65 const Type *getAllocatedType() const;
67 /// getAlignment - Return the alignment of the memory that is being allocated
68 /// by the instruction.
70 unsigned getAlignment() const { return Alignment; }
72 virtual Instruction *clone() const = 0;
74 // Methods for support type inquiry through isa, cast, and dyn_cast:
75 static inline bool classof(const AllocationInst *) { return true; }
76 static inline bool classof(const Instruction *I) {
77 return I->getOpcode() == Instruction::Alloca ||
78 I->getOpcode() == Instruction::Malloc;
80 static inline bool classof(const Value *V) {
81 return isa<Instruction>(V) && classof(cast<Instruction>(V));
86 //===----------------------------------------------------------------------===//
88 //===----------------------------------------------------------------------===//
90 /// MallocInst - an instruction to allocated memory on the heap
92 class MallocInst : public AllocationInst {
93 MallocInst(const MallocInst &MI);
95 explicit MallocInst(const Type *Ty, Value *ArraySize = 0,
96 const std::string &Name = "",
97 Instruction *InsertBefore = 0)
98 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertBefore) {}
99 MallocInst(const Type *Ty, Value *ArraySize, const std::string &Name,
100 BasicBlock *InsertAtEnd)
101 : AllocationInst(Ty, ArraySize, Malloc, 0, Name, InsertAtEnd) {}
102 MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
103 const std::string &Name, BasicBlock *InsertAtEnd)
104 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertAtEnd) {}
105 explicit MallocInst(const Type *Ty, Value *ArraySize, unsigned Align,
106 const std::string &Name = "",
107 Instruction *InsertBefore = 0)
108 : AllocationInst(Ty, ArraySize, Malloc, Align, Name, InsertBefore) {}
110 virtual MallocInst *clone() const;
112 // Methods for support type inquiry through isa, cast, and dyn_cast:
113 static inline bool classof(const MallocInst *) { return true; }
114 static inline bool classof(const Instruction *I) {
115 return (I->getOpcode() == Instruction::Malloc);
117 static inline bool classof(const Value *V) {
118 return isa<Instruction>(V) && classof(cast<Instruction>(V));
123 //===----------------------------------------------------------------------===//
125 //===----------------------------------------------------------------------===//
127 /// AllocaInst - an instruction to allocate memory on the stack
129 class AllocaInst : public AllocationInst {
130 AllocaInst(const AllocaInst &);
132 explicit AllocaInst(const Type *Ty, Value *ArraySize = 0,
133 const std::string &Name = "",
134 Instruction *InsertBefore = 0)
135 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertBefore) {}
136 AllocaInst(const Type *Ty, Value *ArraySize, const std::string &Name,
137 BasicBlock *InsertAtEnd)
138 : AllocationInst(Ty, ArraySize, Alloca, 0, Name, InsertAtEnd) {}
139 AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
140 const std::string &Name, BasicBlock *InsertAtEnd)
141 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertAtEnd) {}
142 explicit AllocaInst(const Type *Ty, Value *ArraySize, unsigned Align,
143 const std::string &Name = "",
144 Instruction *InsertBefore = 0)
145 : AllocationInst(Ty, ArraySize, Alloca, Align, Name, InsertBefore) {}
147 virtual AllocaInst *clone() const;
149 // Methods for support type inquiry through isa, cast, and dyn_cast:
150 static inline bool classof(const AllocaInst *) { return true; }
151 static inline bool classof(const Instruction *I) {
152 return (I->getOpcode() == Instruction::Alloca);
154 static inline bool classof(const Value *V) {
155 return isa<Instruction>(V) && classof(cast<Instruction>(V));
160 //===----------------------------------------------------------------------===//
162 //===----------------------------------------------------------------------===//
164 /// FreeInst - an instruction to deallocate memory
166 class FreeInst : public UnaryInstruction {
169 explicit FreeInst(Value *Ptr, Instruction *InsertBefore = 0);
170 FreeInst(Value *Ptr, BasicBlock *InsertAfter);
172 virtual FreeInst *clone() const;
174 virtual bool mayWriteToMemory() const { return true; }
176 // Methods for support type inquiry through isa, cast, and dyn_cast:
177 static inline bool classof(const FreeInst *) { return true; }
178 static inline bool classof(const Instruction *I) {
179 return (I->getOpcode() == Instruction::Free);
181 static inline bool classof(const Value *V) {
182 return isa<Instruction>(V) && classof(cast<Instruction>(V));
187 //===----------------------------------------------------------------------===//
189 //===----------------------------------------------------------------------===//
191 /// LoadInst - an instruction for reading from memory. This uses the
192 /// SubclassData field in Value to store whether or not the load is volatile.
194 class LoadInst : public UnaryInstruction {
195 LoadInst(const LoadInst &LI)
196 : UnaryInstruction(LI.getType(), Load, LI.getOperand(0)) {
197 setVolatile(LI.isVolatile());
205 LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBefore);
206 LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAtEnd);
207 LoadInst(Value *Ptr, const std::string &Name = "", bool isVolatile = false,
208 Instruction *InsertBefore = 0);
209 LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
210 BasicBlock *InsertAtEnd);
212 /// isVolatile - Return true if this is a load from a volatile memory
215 bool isVolatile() const { return SubclassData; }
217 /// setVolatile - Specify whether this is a volatile load or not.
219 void setVolatile(bool V) { SubclassData = V; }
221 virtual LoadInst *clone() const;
223 virtual bool mayWriteToMemory() const { return isVolatile(); }
225 Value *getPointerOperand() { return getOperand(0); }
226 const Value *getPointerOperand() const { return getOperand(0); }
227 static unsigned getPointerOperandIndex() { return 0U; }
229 // Methods for support type inquiry through isa, cast, and dyn_cast:
230 static inline bool classof(const LoadInst *) { return true; }
231 static inline bool classof(const Instruction *I) {
232 return I->getOpcode() == Instruction::Load;
234 static inline bool classof(const Value *V) {
235 return isa<Instruction>(V) && classof(cast<Instruction>(V));
240 //===----------------------------------------------------------------------===//
242 //===----------------------------------------------------------------------===//
244 /// StoreInst - an instruction for storing to memory
246 class StoreInst : public Instruction {
248 StoreInst(const StoreInst &SI) : Instruction(SI.getType(), Store, Ops, 2) {
249 Ops[0].init(SI.Ops[0], this);
250 Ops[1].init(SI.Ops[1], this);
251 setVolatile(SI.isVolatile());
258 StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore);
259 StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd);
260 StoreInst(Value *Val, Value *Ptr, bool isVolatile = false,
261 Instruction *InsertBefore = 0);
262 StoreInst(Value *Val, Value *Ptr, bool isVolatile, BasicBlock *InsertAtEnd);
265 /// isVolatile - Return true if this is a load from a volatile memory
268 bool isVolatile() const { return SubclassData; }
270 /// setVolatile - Specify whether this is a volatile load or not.
272 void setVolatile(bool V) { SubclassData = V; }
274 /// Transparently provide more efficient getOperand methods.
275 Value *getOperand(unsigned i) const {
276 assert(i < 2 && "getOperand() out of range!");
279 void setOperand(unsigned i, Value *Val) {
280 assert(i < 2 && "setOperand() out of range!");
283 unsigned getNumOperands() const { return 2; }
286 virtual StoreInst *clone() const;
288 virtual bool mayWriteToMemory() const { return true; }
290 Value *getPointerOperand() { return getOperand(1); }
291 const Value *getPointerOperand() const { return getOperand(1); }
292 static unsigned getPointerOperandIndex() { return 1U; }
294 // Methods for support type inquiry through isa, cast, and dyn_cast:
295 static inline bool classof(const StoreInst *) { return true; }
296 static inline bool classof(const Instruction *I) {
297 return I->getOpcode() == Instruction::Store;
299 static inline bool classof(const Value *V) {
300 return isa<Instruction>(V) && classof(cast<Instruction>(V));
305 //===----------------------------------------------------------------------===//
306 // GetElementPtrInst Class
307 //===----------------------------------------------------------------------===//
309 /// GetElementPtrInst - an instruction for type-safe pointer arithmetic to
310 /// access elements of arrays and structs
312 class GetElementPtrInst : public Instruction {
313 GetElementPtrInst(const GetElementPtrInst &GEPI)
314 : Instruction(reinterpret_cast<const Type*>(GEPI.getType()), GetElementPtr,
315 0, GEPI.getNumOperands()) {
316 Use *OL = OperandList = new Use[NumOperands];
317 Use *GEPIOL = GEPI.OperandList;
318 for (unsigned i = 0, E = NumOperands; i != E; ++i)
319 OL[i].init(GEPIOL[i], this);
321 void init(Value *Ptr, const std::vector<Value*> &Idx);
322 void init(Value *Ptr, Value *Idx0, Value *Idx1);
323 void init(Value *Ptr, Value *Idx);
325 /// Constructors - Create a getelementptr instruction with a base pointer an
326 /// list of indices. The first ctor can optionally insert before an existing
327 /// instruction, the second appends the new instruction to the specified
329 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
330 const std::string &Name = "", Instruction *InsertBefore =0);
331 GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
332 const std::string &Name, BasicBlock *InsertAtEnd);
334 /// Constructors - These two constructors are convenience methods because one
335 /// and two index getelementptr instructions are so common.
336 GetElementPtrInst(Value *Ptr, Value *Idx,
337 const std::string &Name = "", Instruction *InsertBefore =0);
338 GetElementPtrInst(Value *Ptr, Value *Idx,
339 const std::string &Name, BasicBlock *InsertAtEnd);
340 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
341 const std::string &Name = "", Instruction *InsertBefore =0);
342 GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
343 const std::string &Name, BasicBlock *InsertAtEnd);
344 ~GetElementPtrInst();
346 virtual GetElementPtrInst *clone() const;
348 // getType - Overload to return most specific pointer type...
349 inline const PointerType *getType() const {
350 return reinterpret_cast<const PointerType*>(Instruction::getType());
353 /// getIndexedType - Returns the type of the element that would be loaded with
354 /// a load instruction with the specified parameters.
356 /// A null type is returned if the indices are invalid for the specified
359 static const Type *getIndexedType(const Type *Ptr,
360 const std::vector<Value*> &Indices,
361 bool AllowStructLeaf = false);
362 static const Type *getIndexedType(const Type *Ptr, Value *Idx0, Value *Idx1,
363 bool AllowStructLeaf = false);
364 static const Type *getIndexedType(const Type *Ptr, Value *Idx);
366 inline op_iterator idx_begin() { return op_begin()+1; }
367 inline const_op_iterator idx_begin() const { return op_begin()+1; }
368 inline op_iterator idx_end() { return op_end(); }
369 inline const_op_iterator idx_end() const { return op_end(); }
371 Value *getPointerOperand() {
372 return getOperand(0);
374 const Value *getPointerOperand() const {
375 return getOperand(0);
377 static unsigned getPointerOperandIndex() {
378 return 0U; // get index for modifying correct operand
381 inline unsigned getNumIndices() const { // Note: always non-negative
382 return getNumOperands() - 1;
385 inline bool hasIndices() const {
386 return getNumOperands() > 1;
389 // Methods for support type inquiry through isa, cast, and dyn_cast:
390 static inline bool classof(const GetElementPtrInst *) { return true; }
391 static inline bool classof(const Instruction *I) {
392 return (I->getOpcode() == Instruction::GetElementPtr);
394 static inline bool classof(const Value *V) {
395 return isa<Instruction>(V) && classof(cast<Instruction>(V));
399 //===----------------------------------------------------------------------===//
401 //===----------------------------------------------------------------------===//
403 /// SetCondInst class - Represent a setCC operator, where CC is eq, ne, lt, gt,
406 class SetCondInst : public BinaryOperator {
408 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
409 const std::string &Name = "", Instruction *InsertBefore = 0);
410 SetCondInst(BinaryOps Opcode, Value *LHS, Value *RHS,
411 const std::string &Name, BasicBlock *InsertAtEnd);
413 /// getInverseCondition - Return the inverse of the current condition opcode.
414 /// For example seteq -> setne, setgt -> setle, setlt -> setge, etc...
416 BinaryOps getInverseCondition() const {
417 return getInverseCondition(getOpcode());
420 /// getInverseCondition - Static version that you can use without an
421 /// instruction available.
423 static BinaryOps getInverseCondition(BinaryOps Opcode);
425 /// getSwappedCondition - Return the condition opcode that would be the result
426 /// of exchanging the two operands of the setcc instruction without changing
427 /// the result produced. Thus, seteq->seteq, setle->setge, setlt->setgt, etc.
429 BinaryOps getSwappedCondition() const {
430 return getSwappedCondition(getOpcode());
433 /// getSwappedCondition - Static version that you can use without an
434 /// instruction available.
436 static BinaryOps getSwappedCondition(BinaryOps Opcode);
439 // Methods for support type inquiry through isa, cast, and dyn_cast:
440 static inline bool classof(const SetCondInst *) { return true; }
441 static inline bool classof(const Instruction *I) {
442 return I->getOpcode() == SetEQ || I->getOpcode() == SetNE ||
443 I->getOpcode() == SetLE || I->getOpcode() == SetGE ||
444 I->getOpcode() == SetLT || I->getOpcode() == SetGT;
446 static inline bool classof(const Value *V) {
447 return isa<Instruction>(V) && classof(cast<Instruction>(V));
451 //===----------------------------------------------------------------------===//
453 //===----------------------------------------------------------------------===//
455 /// CastInst - This class represents a cast from Operand[0] to the type of
456 /// the instruction (i->getType()).
458 class CastInst : public UnaryInstruction {
459 CastInst(const CastInst &CI)
460 : UnaryInstruction(CI.getType(), Cast, CI.getOperand(0)) {
463 CastInst(Value *S, const Type *Ty, const std::string &Name = "",
464 Instruction *InsertBefore = 0)
465 : UnaryInstruction(Ty, Cast, S, Name, InsertBefore) {
467 CastInst(Value *S, const Type *Ty, const std::string &Name,
468 BasicBlock *InsertAtEnd)
469 : UnaryInstruction(Ty, Cast, S, Name, InsertAtEnd) {
472 virtual CastInst *clone() const;
474 // Methods for support type inquiry through isa, cast, and dyn_cast:
475 static inline bool classof(const CastInst *) { return true; }
476 static inline bool classof(const Instruction *I) {
477 return I->getOpcode() == Cast;
479 static inline bool classof(const Value *V) {
480 return isa<Instruction>(V) && classof(cast<Instruction>(V));
485 //===----------------------------------------------------------------------===//
487 //===----------------------------------------------------------------------===//
489 /// CallInst - This class represents a function call, abstracting a target
490 /// machine's calling convention. This class uses low bit of the SubClassData
491 /// field to indicate whether or not this is a tail call. The rest of the bits
492 /// hold the calling convention of the call.
494 class CallInst : public Instruction {
495 CallInst(const CallInst &CI);
496 void init(Value *Func, const std::vector<Value*> &Params);
497 void init(Value *Func, Value *Actual1, Value *Actual2);
498 void init(Value *Func, Value *Actual);
499 void init(Value *Func);
502 CallInst(Value *F, const std::vector<Value*> &Par,
503 const std::string &Name = "", Instruction *InsertBefore = 0);
504 CallInst(Value *F, const std::vector<Value*> &Par,
505 const std::string &Name, BasicBlock *InsertAtEnd);
507 // Alternate CallInst ctors w/ two actuals, w/ one actual and no
508 // actuals, respectively.
509 CallInst(Value *F, Value *Actual1, Value *Actual2,
510 const std::string& Name = "", Instruction *InsertBefore = 0);
511 CallInst(Value *F, Value *Actual1, Value *Actual2,
512 const std::string& Name, BasicBlock *InsertAtEnd);
513 CallInst(Value *F, Value *Actual, const std::string& Name = "",
514 Instruction *InsertBefore = 0);
515 CallInst(Value *F, Value *Actual, const std::string& Name,
516 BasicBlock *InsertAtEnd);
517 explicit CallInst(Value *F, const std::string &Name = "",
518 Instruction *InsertBefore = 0);
519 explicit CallInst(Value *F, const std::string &Name,
520 BasicBlock *InsertAtEnd);
523 virtual CallInst *clone() const;
524 bool mayWriteToMemory() const { return true; }
526 bool isTailCall() const { return SubclassData & 1; }
527 void setTailCall(bool isTailCall = true) {
528 SubclassData = (SubclassData & ~1) | unsigned(isTailCall);
531 /// getCallingConv/setCallingConv - Get or set the calling convention of this
533 unsigned getCallingConv() const { return SubclassData >> 1; }
534 void setCallingConv(unsigned CC) {
535 SubclassData = (SubclassData & 1) | (CC << 1);
538 /// getCalledFunction - Return the function being called by this instruction
539 /// if it is a direct call. If it is a call through a function pointer,
541 Function *getCalledFunction() const {
542 return static_cast<Function*>(dyn_cast<Function>(getOperand(0)));
545 // getCalledValue - Get a pointer to a method that is invoked by this inst.
546 inline const Value *getCalledValue() const { return getOperand(0); }
547 inline Value *getCalledValue() { return getOperand(0); }
549 // Methods for support type inquiry through isa, cast, and dyn_cast:
550 static inline bool classof(const CallInst *) { return true; }
551 static inline bool classof(const Instruction *I) {
552 return I->getOpcode() == Instruction::Call;
554 static inline bool classof(const Value *V) {
555 return isa<Instruction>(V) && classof(cast<Instruction>(V));
560 //===----------------------------------------------------------------------===//
562 //===----------------------------------------------------------------------===//
564 /// ShiftInst - This class represents left and right shift instructions.
566 class ShiftInst : public Instruction {
568 ShiftInst(const ShiftInst &SI)
569 : Instruction(SI.getType(), SI.getOpcode(), Ops, 2) {
570 Ops[0].init(SI.Ops[0], this);
571 Ops[1].init(SI.Ops[1], this);
573 void init(OtherOps Opcode, Value *S, Value *SA) {
574 assert((Opcode == Shl || Opcode == Shr) && "ShiftInst Opcode invalid!");
575 Ops[0].init(S, this);
576 Ops[1].init(SA, this);
580 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name = "",
581 Instruction *InsertBefore = 0)
582 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertBefore) {
585 ShiftInst(OtherOps Opcode, Value *S, Value *SA, const std::string &Name,
586 BasicBlock *InsertAtEnd)
587 : Instruction(S->getType(), Opcode, Ops, 2, Name, InsertAtEnd) {
591 OtherOps getOpcode() const {
592 return static_cast<OtherOps>(Instruction::getOpcode());
595 /// Transparently provide more efficient getOperand methods.
596 Value *getOperand(unsigned i) const {
597 assert(i < 2 && "getOperand() out of range!");
600 void setOperand(unsigned i, Value *Val) {
601 assert(i < 2 && "setOperand() out of range!");
604 unsigned getNumOperands() const { return 2; }
606 virtual ShiftInst *clone() const;
608 // Methods for support type inquiry through isa, cast, and dyn_cast:
609 static inline bool classof(const ShiftInst *) { return true; }
610 static inline bool classof(const Instruction *I) {
611 return (I->getOpcode() == Instruction::Shr) |
612 (I->getOpcode() == Instruction::Shl);
614 static inline bool classof(const Value *V) {
615 return isa<Instruction>(V) && classof(cast<Instruction>(V));
619 //===----------------------------------------------------------------------===//
621 //===----------------------------------------------------------------------===//
623 /// SelectInst - This class represents the LLVM 'select' instruction.
625 class SelectInst : public Instruction {
628 void init(Value *C, Value *S1, Value *S2) {
629 Ops[0].init(C, this);
630 Ops[1].init(S1, this);
631 Ops[2].init(S2, this);
634 SelectInst(const SelectInst &SI)
635 : Instruction(SI.getType(), SI.getOpcode(), Ops, 3) {
636 init(SI.Ops[0], SI.Ops[1], SI.Ops[2]);
639 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name = "",
640 Instruction *InsertBefore = 0)
641 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
642 Name, InsertBefore) {
645 SelectInst(Value *C, Value *S1, Value *S2, const std::string &Name,
646 BasicBlock *InsertAtEnd)
647 : Instruction(S1->getType(), Instruction::Select, Ops, 3,
652 Value *getCondition() const { return Ops[0]; }
653 Value *getTrueValue() const { return Ops[1]; }
654 Value *getFalseValue() const { return Ops[2]; }
656 /// Transparently provide more efficient getOperand methods.
657 Value *getOperand(unsigned i) const {
658 assert(i < 3 && "getOperand() out of range!");
661 void setOperand(unsigned i, Value *Val) {
662 assert(i < 3 && "setOperand() out of range!");
665 unsigned getNumOperands() const { return 3; }
667 OtherOps getOpcode() const {
668 return static_cast<OtherOps>(Instruction::getOpcode());
671 virtual SelectInst *clone() const;
673 // Methods for support type inquiry through isa, cast, and dyn_cast:
674 static inline bool classof(const SelectInst *) { return true; }
675 static inline bool classof(const Instruction *I) {
676 return I->getOpcode() == Instruction::Select;
678 static inline bool classof(const Value *V) {
679 return isa<Instruction>(V) && classof(cast<Instruction>(V));
683 //===----------------------------------------------------------------------===//
685 //===----------------------------------------------------------------------===//
687 /// VAArgInst - This class represents the va_arg llvm instruction, which returns
688 /// an argument of the specified type given a va_list and increments that list
690 class VAArgInst : public UnaryInstruction {
691 VAArgInst(const VAArgInst &VAA)
692 : UnaryInstruction(VAA.getType(), VAArg, VAA.getOperand(0)) {}
694 VAArgInst(Value *List, const Type *Ty, const std::string &Name = "",
695 Instruction *InsertBefore = 0)
696 : UnaryInstruction(Ty, VAArg, List, Name, InsertBefore) {
698 VAArgInst(Value *List, const Type *Ty, const std::string &Name,
699 BasicBlock *InsertAtEnd)
700 : UnaryInstruction(Ty, VAArg, List, Name, InsertAtEnd) {
703 virtual VAArgInst *clone() const;
704 bool mayWriteToMemory() const { return true; }
706 // Methods for support type inquiry through isa, cast, and dyn_cast:
707 static inline bool classof(const VAArgInst *) { return true; }
708 static inline bool classof(const Instruction *I) {
709 return I->getOpcode() == VAArg;
711 static inline bool classof(const Value *V) {
712 return isa<Instruction>(V) && classof(cast<Instruction>(V));
716 //===----------------------------------------------------------------------===//
718 //===----------------------------------------------------------------------===//
720 // PHINode - The PHINode class is used to represent the magical mystical PHI
721 // node, that can not exist in nature, but can be synthesized in a computer
722 // scientist's overactive imagination.
724 class PHINode : public Instruction {
725 /// ReservedSpace - The number of operands actually allocated. NumOperands is
726 /// the number actually in use.
727 unsigned ReservedSpace;
728 PHINode(const PHINode &PN);
730 PHINode(const Type *Ty, const std::string &Name = "",
731 Instruction *InsertBefore = 0)
732 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertBefore),
736 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
737 : Instruction(Ty, Instruction::PHI, 0, 0, Name, InsertAtEnd),
743 /// reserveOperandSpace - This method can be used to avoid repeated
744 /// reallocation of PHI operand lists by reserving space for the correct
745 /// number of operands before adding them. Unlike normal vector reserves,
746 /// this method can also be used to trim the operand space.
747 void reserveOperandSpace(unsigned NumValues) {
748 resizeOperands(NumValues*2);
751 virtual PHINode *clone() const;
753 /// getNumIncomingValues - Return the number of incoming edges
755 unsigned getNumIncomingValues() const { return getNumOperands()/2; }
757 /// getIncomingValue - Return incoming value #x
759 Value *getIncomingValue(unsigned i) const {
760 assert(i*2 < getNumOperands() && "Invalid value number!");
761 return getOperand(i*2);
763 void setIncomingValue(unsigned i, Value *V) {
764 assert(i*2 < getNumOperands() && "Invalid value number!");
767 unsigned getOperandNumForIncomingValue(unsigned i) {
771 /// getIncomingBlock - Return incoming basic block #x
773 BasicBlock *getIncomingBlock(unsigned i) const {
774 return reinterpret_cast<BasicBlock*>(getOperand(i*2+1));
776 void setIncomingBlock(unsigned i, BasicBlock *BB) {
777 setOperand(i*2+1, reinterpret_cast<Value*>(BB));
779 unsigned getOperandNumForIncomingBlock(unsigned i) {
783 /// addIncoming - Add an incoming value to the end of the PHI list
785 void addIncoming(Value *V, BasicBlock *BB) {
786 assert(getType() == V->getType() &&
787 "All operands to PHI node must be the same type as the PHI node!");
788 unsigned OpNo = NumOperands;
789 if (OpNo+2 > ReservedSpace)
790 resizeOperands(0); // Get more space!
791 // Initialize some new operands.
792 NumOperands = OpNo+2;
793 OperandList[OpNo].init(V, this);
794 OperandList[OpNo+1].init(reinterpret_cast<Value*>(BB), this);
797 /// removeIncomingValue - Remove an incoming value. This is useful if a
798 /// predecessor basic block is deleted. The value removed is returned.
800 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
801 /// is true), the PHI node is destroyed and any uses of it are replaced with
802 /// dummy values. The only time there should be zero incoming values to a PHI
803 /// node is when the block is dead, so this strategy is sound.
805 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
807 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
808 int Idx = getBasicBlockIndex(BB);
809 assert(Idx >= 0 && "Invalid basic block argument to remove!");
810 return removeIncomingValue(Idx, DeletePHIIfEmpty);
813 /// getBasicBlockIndex - Return the first index of the specified basic
814 /// block in the value list for this PHI. Returns -1 if no instance.
816 int getBasicBlockIndex(const BasicBlock *BB) const {
817 Use *OL = OperandList;
818 for (unsigned i = 0, e = getNumOperands(); i != e; i += 2)
819 if (OL[i+1] == reinterpret_cast<const Value*>(BB)) return i/2;
823 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
824 return getIncomingValue(getBasicBlockIndex(BB));
827 /// hasConstantValue - If the specified PHI node always merges together the
828 /// same value, return the value, otherwise return null.
830 Value *hasConstantValue(bool AllowNonDominatingInstruction = false) const;
832 /// Methods for support type inquiry through isa, cast, and dyn_cast:
833 static inline bool classof(const PHINode *) { return true; }
834 static inline bool classof(const Instruction *I) {
835 return I->getOpcode() == Instruction::PHI;
837 static inline bool classof(const Value *V) {
838 return isa<Instruction>(V) && classof(cast<Instruction>(V));
841 void resizeOperands(unsigned NumOperands);
844 //===----------------------------------------------------------------------===//
846 //===----------------------------------------------------------------------===//
848 //===---------------------------------------------------------------------------
849 /// ReturnInst - Return a value (possibly void), from a function. Execution
850 /// does not continue in this function any longer.
852 class ReturnInst : public TerminatorInst {
853 Use RetVal; // Possibly null retval.
854 ReturnInst(const ReturnInst &RI) : TerminatorInst(Instruction::Ret, &RetVal,
855 RI.getNumOperands()) {
856 if (RI.getNumOperands())
857 RetVal.init(RI.RetVal, this);
860 void init(Value *RetVal);
863 // ReturnInst constructors:
864 // ReturnInst() - 'ret void' instruction
865 // ReturnInst( null) - 'ret void' instruction
866 // ReturnInst(Value* X) - 'ret X' instruction
867 // ReturnInst( null, Inst *) - 'ret void' instruction, insert before I
868 // ReturnInst(Value* X, Inst *I) - 'ret X' instruction, insert before I
869 // ReturnInst( null, BB *B) - 'ret void' instruction, insert @ end of BB
870 // ReturnInst(Value* X, BB *B) - 'ret X' instruction, insert @ end of BB
872 // NOTE: If the Value* passed is of type void then the constructor behaves as
873 // if it was passed NULL.
874 ReturnInst(Value *retVal = 0, Instruction *InsertBefore = 0)
875 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertBefore) {
878 ReturnInst(Value *retVal, BasicBlock *InsertAtEnd)
879 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
882 ReturnInst(BasicBlock *InsertAtEnd)
883 : TerminatorInst(Instruction::Ret, &RetVal, 0, InsertAtEnd) {
886 virtual ReturnInst *clone() const;
888 // Transparently provide more efficient getOperand methods.
889 Value *getOperand(unsigned i) const {
890 assert(i < getNumOperands() && "getOperand() out of range!");
893 void setOperand(unsigned i, Value *Val) {
894 assert(i < getNumOperands() && "setOperand() out of range!");
898 Value *getReturnValue() const { return RetVal; }
900 unsigned getNumSuccessors() const { return 0; }
902 // Methods for support type inquiry through isa, cast, and dyn_cast:
903 static inline bool classof(const ReturnInst *) { return true; }
904 static inline bool classof(const Instruction *I) {
905 return (I->getOpcode() == Instruction::Ret);
907 static inline bool classof(const Value *V) {
908 return isa<Instruction>(V) && classof(cast<Instruction>(V));
911 virtual BasicBlock *getSuccessorV(unsigned idx) const;
912 virtual unsigned getNumSuccessorsV() const;
913 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
916 //===----------------------------------------------------------------------===//
918 //===----------------------------------------------------------------------===//
920 //===---------------------------------------------------------------------------
921 /// BranchInst - Conditional or Unconditional Branch instruction.
923 class BranchInst : public TerminatorInst {
924 /// Ops list - Branches are strange. The operands are ordered:
925 /// TrueDest, FalseDest, Cond. This makes some accessors faster because
926 /// they don't have to check for cond/uncond branchness.
928 BranchInst(const BranchInst &BI);
931 // BranchInst constructors (where {B, T, F} are blocks, and C is a condition):
932 // BranchInst(BB *B) - 'br B'
933 // BranchInst(BB* T, BB *F, Value *C) - 'br C, T, F'
934 // BranchInst(BB* B, Inst *I) - 'br B' insert before I
935 // BranchInst(BB* T, BB *F, Value *C, Inst *I) - 'br C, T, F', insert before I
936 // BranchInst(BB* B, BB *I) - 'br B' insert at end
937 // BranchInst(BB* T, BB *F, Value *C, BB *I) - 'br C, T, F', insert at end
938 BranchInst(BasicBlock *IfTrue, Instruction *InsertBefore = 0)
939 : TerminatorInst(Instruction::Br, Ops, 1, InsertBefore) {
940 assert(IfTrue != 0 && "Branch destination may not be null!");
941 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
943 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
944 Instruction *InsertBefore = 0)
945 : TerminatorInst(Instruction::Br, Ops, 3, InsertBefore) {
946 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
947 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
948 Ops[2].init(Cond, this);
954 BranchInst(BasicBlock *IfTrue, BasicBlock *InsertAtEnd)
955 : TerminatorInst(Instruction::Br, Ops, 1, InsertAtEnd) {
956 assert(IfTrue != 0 && "Branch destination may not be null!");
957 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
960 BranchInst(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond,
961 BasicBlock *InsertAtEnd)
962 : TerminatorInst(Instruction::Br, Ops, 3, InsertAtEnd) {
963 Ops[0].init(reinterpret_cast<Value*>(IfTrue), this);
964 Ops[1].init(reinterpret_cast<Value*>(IfFalse), this);
965 Ops[2].init(Cond, this);
972 /// Transparently provide more efficient getOperand methods.
973 Value *getOperand(unsigned i) const {
974 assert(i < getNumOperands() && "getOperand() out of range!");
977 void setOperand(unsigned i, Value *Val) {
978 assert(i < getNumOperands() && "setOperand() out of range!");
982 virtual BranchInst *clone() const;
984 inline bool isUnconditional() const { return getNumOperands() == 1; }
985 inline bool isConditional() const { return getNumOperands() == 3; }
987 inline Value *getCondition() const {
988 assert(isConditional() && "Cannot get condition of an uncond branch!");
989 return getOperand(2);
992 void setCondition(Value *V) {
993 assert(isConditional() && "Cannot set condition of unconditional branch!");
997 // setUnconditionalDest - Change the current branch to an unconditional branch
998 // targeting the specified block.
999 // FIXME: Eliminate this ugly method.
1000 void setUnconditionalDest(BasicBlock *Dest) {
1001 if (isConditional()) { // Convert this to an uncond branch.
1006 setOperand(0, reinterpret_cast<Value*>(Dest));
1009 unsigned getNumSuccessors() const { return 1+isConditional(); }
1011 BasicBlock *getSuccessor(unsigned i) const {
1012 assert(i < getNumSuccessors() && "Successor # out of range for Branch!");
1013 return (i == 0) ? cast<BasicBlock>(getOperand(0)) :
1014 cast<BasicBlock>(getOperand(1));
1017 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1018 assert(idx < getNumSuccessors() && "Successor # out of range for Branch!");
1019 setOperand(idx, reinterpret_cast<Value*>(NewSucc));
1022 // Methods for support type inquiry through isa, cast, and dyn_cast:
1023 static inline bool classof(const BranchInst *) { return true; }
1024 static inline bool classof(const Instruction *I) {
1025 return (I->getOpcode() == Instruction::Br);
1027 static inline bool classof(const Value *V) {
1028 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1031 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1032 virtual unsigned getNumSuccessorsV() const;
1033 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1036 //===----------------------------------------------------------------------===//
1038 //===----------------------------------------------------------------------===//
1040 //===---------------------------------------------------------------------------
1041 /// SwitchInst - Multiway switch
1043 class SwitchInst : public TerminatorInst {
1044 unsigned ReservedSpace;
1045 // Operand[0] = Value to switch on
1046 // Operand[1] = Default basic block destination
1047 // Operand[2n ] = Value to match
1048 // Operand[2n+1] = BasicBlock to go to on match
1049 SwitchInst(const SwitchInst &RI);
1050 void init(Value *Value, BasicBlock *Default, unsigned NumCases);
1051 void resizeOperands(unsigned No);
1053 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1054 /// switch on and a default destination. The number of additional cases can
1055 /// be specified here to make memory allocation more efficient. This
1056 /// constructor can also autoinsert before another instruction.
1057 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1058 Instruction *InsertBefore = 0)
1059 : TerminatorInst(Instruction::Switch, 0, 0, InsertBefore) {
1060 init(Value, Default, NumCases);
1063 /// SwitchInst ctor - Create a new switch instruction, specifying a value to
1064 /// switch on and a default destination. The number of additional cases can
1065 /// be specified here to make memory allocation more efficient. This
1066 /// constructor also autoinserts at the end of the specified BasicBlock.
1067 SwitchInst(Value *Value, BasicBlock *Default, unsigned NumCases,
1068 BasicBlock *InsertAtEnd)
1069 : TerminatorInst(Instruction::Switch, 0, 0, InsertAtEnd) {
1070 init(Value, Default, NumCases);
1075 // Accessor Methods for Switch stmt
1076 inline Value *getCondition() const { return getOperand(0); }
1077 void setCondition(Value *V) { setOperand(0, V); }
1079 inline BasicBlock *getDefaultDest() const {
1080 return cast<BasicBlock>(getOperand(1));
1083 /// getNumCases - return the number of 'cases' in this switch instruction.
1084 /// Note that case #0 is always the default case.
1085 unsigned getNumCases() const {
1086 return getNumOperands()/2;
1089 /// getCaseValue - Return the specified case value. Note that case #0, the
1090 /// default destination, does not have a case value.
1091 ConstantInt *getCaseValue(unsigned i) {
1092 assert(i && i < getNumCases() && "Illegal case value to get!");
1093 return getSuccessorValue(i);
1096 /// getCaseValue - Return the specified case value. Note that case #0, the
1097 /// default destination, does not have a case value.
1098 const ConstantInt *getCaseValue(unsigned i) const {
1099 assert(i && i < getNumCases() && "Illegal case value to get!");
1100 return getSuccessorValue(i);
1103 /// findCaseValue - Search all of the case values for the specified constant.
1104 /// If it is explicitly handled, return the case number of it, otherwise
1105 /// return 0 to indicate that it is handled by the default handler.
1106 unsigned findCaseValue(const ConstantInt *C) const {
1107 for (unsigned i = 1, e = getNumCases(); i != e; ++i)
1108 if (getCaseValue(i) == C)
1113 /// addCase - Add an entry to the switch instruction...
1115 void addCase(ConstantInt *OnVal, BasicBlock *Dest);
1117 /// removeCase - This method removes the specified successor from the switch
1118 /// instruction. Note that this cannot be used to remove the default
1119 /// destination (successor #0).
1121 void removeCase(unsigned idx);
1123 virtual SwitchInst *clone() const;
1125 unsigned getNumSuccessors() const { return getNumOperands()/2; }
1126 BasicBlock *getSuccessor(unsigned idx) const {
1127 assert(idx < getNumSuccessors() &&"Successor idx out of range for switch!");
1128 return cast<BasicBlock>(getOperand(idx*2+1));
1130 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1131 assert(idx < getNumSuccessors() && "Successor # out of range for switch!");
1132 setOperand(idx*2+1, reinterpret_cast<Value*>(NewSucc));
1135 // getSuccessorValue - Return the value associated with the specified
1137 inline ConstantInt *getSuccessorValue(unsigned idx) const {
1138 assert(idx < getNumSuccessors() && "Successor # out of range!");
1139 return reinterpret_cast<ConstantInt*>(getOperand(idx*2));
1142 // Methods for support type inquiry through isa, cast, and dyn_cast:
1143 static inline bool classof(const SwitchInst *) { return true; }
1144 static inline bool classof(const Instruction *I) {
1145 return I->getOpcode() == Instruction::Switch;
1147 static inline bool classof(const Value *V) {
1148 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1151 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1152 virtual unsigned getNumSuccessorsV() const;
1153 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1156 //===----------------------------------------------------------------------===//
1158 //===----------------------------------------------------------------------===//
1160 //===---------------------------------------------------------------------------
1162 /// InvokeInst - Invoke instruction. The SubclassData field is used to hold the
1163 /// calling convention of the call.
1165 class InvokeInst : public TerminatorInst {
1166 InvokeInst(const InvokeInst &BI);
1167 void init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1168 const std::vector<Value*> &Params);
1170 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1171 const std::vector<Value*> &Params, const std::string &Name = "",
1172 Instruction *InsertBefore = 0);
1173 InvokeInst(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
1174 const std::vector<Value*> &Params, const std::string &Name,
1175 BasicBlock *InsertAtEnd);
1178 virtual InvokeInst *clone() const;
1180 bool mayWriteToMemory() const { return true; }
1182 /// getCallingConv/setCallingConv - Get or set the calling convention of this
1184 unsigned getCallingConv() const { return SubclassData; }
1185 void setCallingConv(unsigned CC) {
1189 /// getCalledFunction - Return the function called, or null if this is an
1190 /// indirect function invocation.
1192 Function *getCalledFunction() const {
1193 return dyn_cast<Function>(getOperand(0));
1196 // getCalledValue - Get a pointer to a function that is invoked by this inst.
1197 inline Value *getCalledValue() const { return getOperand(0); }
1199 // get*Dest - Return the destination basic blocks...
1200 BasicBlock *getNormalDest() const {
1201 return cast<BasicBlock>(getOperand(1));
1203 BasicBlock *getUnwindDest() const {
1204 return cast<BasicBlock>(getOperand(2));
1206 void setNormalDest(BasicBlock *B) {
1207 setOperand(1, reinterpret_cast<Value*>(B));
1210 void setUnwindDest(BasicBlock *B) {
1211 setOperand(2, reinterpret_cast<Value*>(B));
1214 inline BasicBlock *getSuccessor(unsigned i) const {
1215 assert(i < 2 && "Successor # out of range for invoke!");
1216 return i == 0 ? getNormalDest() : getUnwindDest();
1219 void setSuccessor(unsigned idx, BasicBlock *NewSucc) {
1220 assert(idx < 2 && "Successor # out of range for invoke!");
1221 setOperand(idx+1, reinterpret_cast<Value*>(NewSucc));
1224 unsigned getNumSuccessors() const { return 2; }
1226 // Methods for support type inquiry through isa, cast, and dyn_cast:
1227 static inline bool classof(const InvokeInst *) { return true; }
1228 static inline bool classof(const Instruction *I) {
1229 return (I->getOpcode() == Instruction::Invoke);
1231 static inline bool classof(const Value *V) {
1232 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1235 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1236 virtual unsigned getNumSuccessorsV() const;
1237 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1241 //===----------------------------------------------------------------------===//
1243 //===----------------------------------------------------------------------===//
1245 //===---------------------------------------------------------------------------
1246 /// UnwindInst - Immediately exit the current function, unwinding the stack
1247 /// until an invoke instruction is found.
1249 class UnwindInst : public TerminatorInst {
1251 UnwindInst(Instruction *InsertBefore = 0)
1252 : TerminatorInst(Instruction::Unwind, 0, 0, InsertBefore) {
1254 UnwindInst(BasicBlock *InsertAtEnd)
1255 : TerminatorInst(Instruction::Unwind, 0, 0, InsertAtEnd) {
1258 virtual UnwindInst *clone() const;
1260 unsigned getNumSuccessors() const { return 0; }
1262 // Methods for support type inquiry through isa, cast, and dyn_cast:
1263 static inline bool classof(const UnwindInst *) { return true; }
1264 static inline bool classof(const Instruction *I) {
1265 return I->getOpcode() == Instruction::Unwind;
1267 static inline bool classof(const Value *V) {
1268 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1271 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1272 virtual unsigned getNumSuccessorsV() const;
1273 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1276 //===----------------------------------------------------------------------===//
1277 // UnreachableInst Class
1278 //===----------------------------------------------------------------------===//
1280 //===---------------------------------------------------------------------------
1281 /// UnreachableInst - This function has undefined behavior. In particular, the
1282 /// presence of this instruction indicates some higher level knowledge that the
1283 /// end of the block cannot be reached.
1285 class UnreachableInst : public TerminatorInst {
1287 UnreachableInst(Instruction *InsertBefore = 0)
1288 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertBefore) {
1290 UnreachableInst(BasicBlock *InsertAtEnd)
1291 : TerminatorInst(Instruction::Unreachable, 0, 0, InsertAtEnd) {
1294 virtual UnreachableInst *clone() const;
1296 unsigned getNumSuccessors() const { return 0; }
1298 // Methods for support type inquiry through isa, cast, and dyn_cast:
1299 static inline bool classof(const UnreachableInst *) { return true; }
1300 static inline bool classof(const Instruction *I) {
1301 return I->getOpcode() == Instruction::Unreachable;
1303 static inline bool classof(const Value *V) {
1304 return isa<Instruction>(V) && classof(cast<Instruction>(V));
1307 virtual BasicBlock *getSuccessorV(unsigned idx) const;
1308 virtual unsigned getNumSuccessorsV() const;
1309 virtual void setSuccessorV(unsigned idx, BasicBlock *B);
1312 } // End llvm namespace