By default PHINode::removeIncomingValue will delete the phi node if the last
[oota-llvm.git] / include / llvm / iPHINode.h
1 //===-- llvm/iPHINode.h - PHI instruction definition -------------*- C++ -*--=//
2 //
3 // This file defines the PHINode class.
4 //
5 //===----------------------------------------------------------------------===//
6
7 #ifndef LLVM_IPHINODE_H
8 #define LLVM_IPHINODE_H
9
10 #include "llvm/Instruction.h"
11 class BasicBlock;
12
13 //===----------------------------------------------------------------------===//
14 //                               PHINode Class
15 //===----------------------------------------------------------------------===//
16
17 // PHINode - The PHINode class is used to represent the magical mystical PHI
18 // node, that can not exist in nature, but can be synthesized in a computer
19 // scientist's overactive imagination.
20 //
21 class PHINode : public Instruction {
22   PHINode(const PHINode &PN);
23 public:
24   PHINode(const Type *Ty, const std::string &Name = "",
25           Instruction *InsertBefore = 0)
26     : Instruction(Ty, Instruction::PHINode, Name, InsertBefore) {
27   }
28
29   virtual Instruction *clone() const { return new PHINode(*this); }
30
31   /// getNumIncomingValues - Return the number of incoming edges the PHI node
32   /// has
33   unsigned getNumIncomingValues() const { return Operands.size()/2; }
34
35   /// getIncomingValue - Return incoming value #x
36   const Value *getIncomingValue(unsigned i) const {
37     return Operands[i*2];
38   }
39   Value *getIncomingValue(unsigned i) {
40     return Operands[i*2];
41   }
42   void setIncomingValue(unsigned i, Value *V) {
43     Operands[i*2] = V;
44   }
45   inline unsigned getOperandNumForIncomingValue(unsigned i) {
46     return i*2;
47   }
48
49   /// getIncomingBlock - Return incoming basic block #x
50   const BasicBlock *getIncomingBlock(unsigned i) const { 
51     return (const BasicBlock*)Operands[i*2+1].get();
52   }
53   inline BasicBlock *getIncomingBlock(unsigned i) { 
54     return (BasicBlock*)Operands[i*2+1].get();
55   }
56   inline void setIncomingBlock(unsigned i, BasicBlock *BB) {
57     Operands[i*2+1] = (Value*)BB;
58   }
59   inline unsigned getOperandNumForIncomingBlock(unsigned i) {
60     return i*2+1;
61   }
62
63   /// addIncoming - Add an incoming value to the end of the PHI list
64   void addIncoming(Value *D, BasicBlock *BB);
65   
66   /// removeIncomingValue - Remove an incoming value.  This is useful if a
67   /// predecessor basic block is deleted.  The value removed is returned.
68   ///
69   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
70   /// is true), the PHI node is destroyed and any uses of it are replaced with
71   /// dummy values.  The only time there should be zero incoming values to a PHI
72   /// node is when the block is dead, so this strategy is sound.
73   ///
74   Value *removeIncomingValue(const BasicBlock *BB,
75                              bool DeletePHIIfEmpty = true);
76
77   /// getBasicBlockIndex - Return the first index of the specified basic 
78   /// block in the value list for this PHI.  Returns -1 if no instance.
79   ///
80   int getBasicBlockIndex(const BasicBlock *BB) const {
81     for (unsigned i = 0; i < Operands.size()/2; ++i) 
82       if (getIncomingBlock(i) == BB) return i;
83     return -1;
84   }
85
86   /// Methods for support type inquiry through isa, cast, and dyn_cast:
87   static inline bool classof(const PHINode *) { return true; }
88   static inline bool classof(const Instruction *I) {
89     return I->getOpcode() == Instruction::PHINode; 
90   }
91   static inline bool classof(const Value *V) {
92     return isa<Instruction>(V) && classof(cast<Instruction>(V));
93   }
94 };
95
96 #endif