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 PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
40 : Instruction(Ty, Instruction::PHI, Name, InsertAtEnd) {
43 virtual Instruction *clone() const { return new PHINode(*this); }
45 /// getNumIncomingValues - Return the number of incoming edges
47 unsigned getNumIncomingValues() const { return Operands.size()/2; }
49 /// getIncomingValue - Return incoming value #x
51 Value *getIncomingValue(unsigned i) const {
52 assert(i*2 < Operands.size() && "Invalid value number!");
55 void setIncomingValue(unsigned i, Value *V) {
56 assert(i*2 < Operands.size() && "Invalid value number!");
59 inline unsigned getOperandNumForIncomingValue(unsigned i) {
63 /// getIncomingBlock - Return incoming basic block #x
65 BasicBlock *getIncomingBlock(unsigned i) const {
66 assert(i*2+1 < Operands.size() && "Invalid value number!");
67 return reinterpret_cast<BasicBlock*>(Operands[i*2+1].get());
69 void setIncomingBlock(unsigned i, BasicBlock *BB) {
70 assert(i*2+1 < Operands.size() && "Invalid value number!");
71 Operands[i*2+1] = reinterpret_cast<Value*>(BB);
73 unsigned getOperandNumForIncomingBlock(unsigned i) {
77 /// addIncoming - Add an incoming value to the end of the PHI list
79 void addIncoming(Value *V, BasicBlock *BB) {
80 assert(getType() == V->getType() &&
81 "All operands to PHI node must be the same type as the PHI node!");
82 Operands.push_back(Use(V, this));
83 Operands.push_back(Use(reinterpret_cast<Value*>(BB), this));
86 /// removeIncomingValue - Remove an incoming value. This is useful if a
87 /// predecessor basic block is deleted. The value removed is returned.
89 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
90 /// is true), the PHI node is destroyed and any uses of it are replaced with
91 /// dummy values. The only time there should be zero incoming values to a PHI
92 /// node is when the block is dead, so this strategy is sound.
94 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
96 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
97 int Idx = getBasicBlockIndex(BB);
98 assert(Idx >= 0 && "Invalid basic block argument to remove!");
99 return removeIncomingValue(Idx, DeletePHIIfEmpty);
102 /// getBasicBlockIndex - Return the first index of the specified basic
103 /// block in the value list for this PHI. Returns -1 if no instance.
105 int getBasicBlockIndex(const BasicBlock *BB) const {
106 for (unsigned i = 0; i < Operands.size()/2; ++i)
107 if (getIncomingBlock(i) == BB) return i;
111 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
112 return getIncomingValue(getBasicBlockIndex(BB));
115 /// Methods for support type inquiry through isa, cast, and dyn_cast:
116 static inline bool classof(const PHINode *) { return true; }
117 static inline bool classof(const Instruction *I) {
118 return I->getOpcode() == Instruction::PHI;
120 static inline bool classof(const Value *V) {
121 return isa<Instruction>(V) && classof(cast<Instruction>(V));
125 } // End llvm namespace