Add the Instruction::Select enum
[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
19 namespace llvm {
20
21 class BasicBlock;
22
23 //===----------------------------------------------------------------------===//
24 //                               PHINode Class
25 //===----------------------------------------------------------------------===//
26
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.
30 //
31 class PHINode : public Instruction {
32   PHINode(const PHINode &PN);
33 public:
34   PHINode(const Type *Ty, const std::string &Name = "",
35           Instruction *InsertBefore = 0)
36     : Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
37   }
38
39   virtual Instruction *clone() const { return new PHINode(*this); }
40
41   /// getNumIncomingValues - Return the number of incoming edges
42   ///
43   unsigned getNumIncomingValues() const { return Operands.size()/2; }
44
45   /// getIncomingValue - Return incoming value #x
46   ///
47   Value *getIncomingValue(unsigned i) const {
48     assert(i*2 < Operands.size() && "Invalid value number!");
49     return Operands[i*2];
50   }
51   void setIncomingValue(unsigned i, Value *V) {
52     assert(i*2 < Operands.size() && "Invalid value number!");
53     Operands[i*2] = V;
54   }
55   inline unsigned getOperandNumForIncomingValue(unsigned i) {
56     return i*2;
57   }
58
59   /// getIncomingBlock - Return incoming basic block #x
60   ///
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());
64   }
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);
68   }
69   unsigned getOperandNumForIncomingBlock(unsigned i) {
70     return i*2+1;
71   }
72
73   /// addIncoming - Add an incoming value to the end of the PHI list
74   ///
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));
80   }
81   
82   /// removeIncomingValue - Remove an incoming value.  This is useful if a
83   /// predecessor basic block is deleted.  The value removed is returned.
84   ///
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.
89   ///
90   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
91
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);
96   }
97
98   /// getBasicBlockIndex - Return the first index of the specified basic 
99   /// block in the value list for this PHI.  Returns -1 if no instance.
100   ///
101   int getBasicBlockIndex(const BasicBlock *BB) const {
102     for (unsigned i = 0; i < Operands.size()/2; ++i) 
103       if (getIncomingBlock(i) == BB) return i;
104     return -1;
105   }
106
107   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
108     return getIncomingValue(getBasicBlockIndex(BB));
109   }
110
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; 
115   }
116   static inline bool classof(const Value *V) {
117     return isa<Instruction>(V) && classof(cast<Instruction>(V));
118   }
119 };
120
121 } // End llvm namespace
122
123 #endif