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