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"
20 //===----------------------------------------------------------------------===//
22 //===----------------------------------------------------------------------===//
24 // PHINode - The PHINode class is used to represent the magical mystical PHI
25 // node, that can not exist in nature, but can be synthesized in a computer
26 // scientist's overactive imagination.
28 class PHINode : public Instruction {
29 PHINode(const PHINode &PN);
31 PHINode(const Type *Ty, const std::string &Name = "",
32 Instruction *InsertBefore = 0)
33 : Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
36 virtual Instruction *clone() const { return new PHINode(*this); }
38 /// getNumIncomingValues - Return the number of incoming edges the PHI node
40 unsigned getNumIncomingValues() const { return Operands.size()/2; }
42 /// getIncomingValue - Return incoming value #x
43 Value *getIncomingValue(unsigned i) const {
44 assert(i*2 < Operands.size() && "Invalid value number!");
47 void setIncomingValue(unsigned i, Value *V) {
48 assert(i*2 < Operands.size() && "Invalid value number!");
51 inline unsigned getOperandNumForIncomingValue(unsigned i) {
55 /// getIncomingBlock - Return incoming basic block #x
56 BasicBlock *getIncomingBlock(unsigned i) const {
57 assert(i*2+1 < Operands.size() && "Invalid value number!");
58 return (BasicBlock*)Operands[i*2+1].get();
60 void setIncomingBlock(unsigned i, BasicBlock *BB) {
61 assert(i*2+1 < Operands.size() && "Invalid value number!");
62 Operands[i*2+1] = (Value*)BB;
64 unsigned getOperandNumForIncomingBlock(unsigned i) {
68 /// addIncoming - Add an incoming value to the end of the PHI list
69 void addIncoming(Value *D, BasicBlock *BB) {
70 assert(getType() == D->getType() &&
71 "All operands to PHI node must be the same type as the PHI node!");
72 Operands.push_back(Use(D, this));
73 Operands.push_back(Use((Value*)BB, this));
76 /// removeIncomingValue - Remove an incoming value. This is useful if a
77 /// predecessor basic block is deleted. The value removed is returned.
79 /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
80 /// is true), the PHI node is destroyed and any uses of it are replaced with
81 /// dummy values. The only time there should be zero incoming values to a PHI
82 /// node is when the block is dead, so this strategy is sound.
84 Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
86 Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
87 int Idx = getBasicBlockIndex(BB);
88 assert(Idx >= 0 && "Invalid basic block argument to remove!");
89 return removeIncomingValue(Idx, DeletePHIIfEmpty);
92 /// getBasicBlockIndex - Return the first index of the specified basic
93 /// block in the value list for this PHI. Returns -1 if no instance.
95 int getBasicBlockIndex(const BasicBlock *BB) const {
96 for (unsigned i = 0; i < Operands.size()/2; ++i)
97 if (getIncomingBlock(i) == BB) return i;
101 Value *getIncomingValueForBlock(const BasicBlock *BB) const {
102 return getIncomingValue(getBasicBlockIndex(BB));
105 /// Methods for support type inquiry through isa, cast, and dyn_cast:
106 static inline bool classof(const PHINode *) { return true; }
107 static inline bool classof(const Instruction *I) {
108 return I->getOpcode() == Instruction::PHI;
110 static inline bool classof(const Value *V) {
111 return isa<Instruction>(V) && classof(cast<Instruction>(V));