1 //===-- llvm/iPHINode.h - PHI instruction definition ------------*- 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 defines the PHINode class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_IPHINODE_H
15 #define LLVM_IPHINODE_H
17 #include "llvm/Instruction.h"
23 //===----------------------------------------------------------------------===//
25 //===----------------------------------------------------------------------===//
27 // PHINode - The PHINode class is used to represent the magical mystical PHI
28 // node, that can not exist in nature, but can be synthesized in a computer
29 // scientist's overactive imagination.
31 class PHINode : public Instruction {
32 PHINode(const PHINode &PN);
34 PHINode(const Type *Ty, const std::string &Name = "",
35 Instruction *InsertBefore = 0)
36 : Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
39 virtual Instruction *clone() const { return new PHINode(*this); }
41 /// getNumIncomingValues - Return the number of incoming edges the PHI node
43 unsigned getNumIncomingValues() const { return Operands.size()/2; }
45 /// getIncomingValue - Return incoming value #x
46 Value *getIncomingValue(unsigned i) const {
47 assert(i*2 < Operands.size() && "Invalid value number!");
50 void setIncomingValue(unsigned i, Value *V) {
51 assert(i*2 < Operands.size() && "Invalid value number!");
54 inline unsigned getOperandNumForIncomingValue(unsigned i) {
58 /// getIncomingBlock - Return incoming basic block #x
59 BasicBlock *getIncomingBlock(unsigned i) const {
60 assert(i*2+1 < Operands.size() && "Invalid value number!");
61 return reinterpret_cast<BasicBlock*>(Operands[i*2+1].get());
63 void setIncomingBlock(unsigned i, BasicBlock *BB) {
64 assert(i*2+1 < Operands.size() && "Invalid value number!");
65 Operands[i*2+1] = reinterpret_cast<Value*>(BB);
67 unsigned getOperandNumForIncomingBlock(unsigned i) {
71 /// addIncoming - Add an incoming value to the end of the PHI list
72 void addIncoming(Value *D, BasicBlock *BB) {
73 assert(getType() == D->getType() &&
74 "All operands to PHI node must be the same type as the PHI node!");
75 Operands.push_back(Use(D, this));
76 Operands.push_back(Use(reinterpret_cast<Value*>(BB), this));
79 /// removeIncomingValue - Remove an incoming value. This is useful if a
80 /// predecessor basic block is deleted. The value removed is returned.
82 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
83 /// is true), the PHI node is destroyed and any uses of it are replaced with
84 /// dummy values. The only time there should be zero incoming values to a PHI
85 /// node is when the block is dead, so this strategy is sound.
87 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
89 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
90 int Idx = getBasicBlockIndex(BB);
91 assert(Idx >= 0 && "Invalid basic block argument to remove!");
92 return removeIncomingValue(Idx, DeletePHIIfEmpty);
95 /// getBasicBlockIndex - Return the first index of the specified basic
96 /// block in the value list for this PHI. Returns -1 if no instance.
98 int getBasicBlockIndex(const BasicBlock *BB) const {
99 for (unsigned i = 0; i < Operands.size()/2; ++i)
100 if (getIncomingBlock(i) == BB) return i;
104 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
105 return getIncomingValue(getBasicBlockIndex(BB));
108 /// Methods for support type inquiry through isa, cast, and dyn_cast:
109 static inline bool classof(const PHINode *) { return true; }
110 static inline bool classof(const Instruction *I) {
111 return I->getOpcode() == Instruction::PHI;
113 static inline bool classof(const Value *V) {
114 return isa<Instruction>(V) && classof(cast<Instruction>(V));
118 } // End llvm namespace