Added the name of the public CVS repository.
[oota-llvm.git] / include / llvm / iPHINode.h
1 //===-- llvm/iPHINode.h - PHI instruction definition ------------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
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.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file defines the PHINode class.
11 //
12 //===----------------------------------------------------------------------===//
13
14 #ifndef LLVM_IPHINODE_H
15 #define LLVM_IPHINODE_H
16
17 #include "llvm/Instruction.h"
18 class BasicBlock;
19
20 //===----------------------------------------------------------------------===//
21 //                               PHINode Class
22 //===----------------------------------------------------------------------===//
23
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.
27 //
28 class PHINode : public Instruction {
29   PHINode(const PHINode &PN);
30 public:
31   PHINode(const Type *Ty, const std::string &Name = "",
32           Instruction *InsertBefore = 0)
33     : Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
34   }
35
36   virtual Instruction *clone() const { return new PHINode(*this); }
37
38   /// getNumIncomingValues - Return the number of incoming edges the PHI node
39   /// has
40   unsigned getNumIncomingValues() const { return Operands.size()/2; }
41
42   /// getIncomingValue - Return incoming value #x
43   Value *getIncomingValue(unsigned i) const {
44     assert(i*2 < Operands.size() && "Invalid value number!");
45     return Operands[i*2];
46   }
47   void setIncomingValue(unsigned i, Value *V) {
48     assert(i*2 < Operands.size() && "Invalid value number!");
49     Operands[i*2] = V;
50   }
51   inline unsigned getOperandNumForIncomingValue(unsigned i) {
52     return i*2;
53   }
54
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();
59   }
60   void setIncomingBlock(unsigned i, BasicBlock *BB) {
61     assert(i*2+1 < Operands.size() && "Invalid value number!");
62     Operands[i*2+1] = (Value*)BB;
63   }
64   unsigned getOperandNumForIncomingBlock(unsigned i) {
65     return i*2+1;
66   }
67
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));
74   }
75   
76   /// removeIncomingValue - Remove an incoming value.  This is useful if a
77   /// predecessor basic block is deleted.  The value removed is returned.
78   ///
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.
83   ///
84   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
85
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);
90   }
91
92   /// getBasicBlockIndex - Return the first index of the specified basic 
93   /// block in the value list for this PHI.  Returns -1 if no instance.
94   ///
95   int getBasicBlockIndex(const BasicBlock *BB) const {
96     for (unsigned i = 0; i < Operands.size()/2; ++i) 
97       if (getIncomingBlock(i) == BB) return i;
98     return -1;
99   }
100
101   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
102     return getIncomingValue(getBasicBlockIndex(BB));
103   }
104
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; 
109   }
110   static inline bool classof(const Value *V) {
111     return isa<Instruction>(V) && classof(cast<Instruction>(V));
112   }
113 };
114
115 #endif