Included assert.h so that the code compiles under newer versions of GCC.
[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 <assert.h>
11
12 #include "llvm/Instruction.h"
13 class BasicBlock;
14
15 //===----------------------------------------------------------------------===//
16 //                               PHINode Class
17 //===----------------------------------------------------------------------===//
18
19 // PHINode - The PHINode class is used to represent the magical mystical PHI
20 // node, that can not exist in nature, but can be synthesized in a computer
21 // scientist's overactive imagination.
22 //
23 class PHINode : public Instruction {
24   PHINode(const PHINode &PN);
25 public:
26   PHINode(const Type *Ty, const std::string &Name = "",
27           Instruction *InsertBefore = 0)
28     : Instruction(Ty, Instruction::PHINode, Name, InsertBefore) {
29   }
30
31   virtual Instruction *clone() const { return new PHINode(*this); }
32
33   /// getNumIncomingValues - Return the number of incoming edges the PHI node
34   /// has
35   unsigned getNumIncomingValues() const { return Operands.size()/2; }
36
37   /// getIncomingValue - Return incoming value #x
38   Value *getIncomingValue(unsigned i) const {
39     assert(i*2 < Operands.size() && "Invalid value number!");
40     return Operands[i*2];
41   }
42   void setIncomingValue(unsigned i, Value *V) {
43     assert(i*2 < Operands.size() && "Invalid value number!");
44     Operands[i*2] = V;
45   }
46   inline unsigned getOperandNumForIncomingValue(unsigned i) {
47     return i*2;
48   }
49
50   /// getIncomingBlock - Return incoming basic block #x
51   BasicBlock *getIncomingBlock(unsigned i) const { 
52     assert(i*2+1 < Operands.size() && "Invalid value number!");
53     return (BasicBlock*)Operands[i*2+1].get();
54   }
55   void setIncomingBlock(unsigned i, BasicBlock *BB) {
56     assert(i*2+1 < Operands.size() && "Invalid value number!");
57     Operands[i*2+1] = (Value*)BB;
58   }
59   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     assert(getType() == D->getType() &&
66            "All operands to PHI node must be the same type as the PHI node!");
67     Operands.push_back(Use(D, this));
68     Operands.push_back(Use((Value*)BB, this));
69   }
70   
71   /// removeIncomingValue - Remove an incoming value.  This is useful if a
72   /// predecessor basic block is deleted.  The value removed is returned.
73   ///
74   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
75   /// is true), the PHI node is destroyed and any uses of it are replaced with
76   /// dummy values.  The only time there should be zero incoming values to a PHI
77   /// node is when the block is dead, so this strategy is sound.
78   ///
79   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
80
81   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
82     int Idx = getBasicBlockIndex(BB);
83     assert(Idx >= 0 && "Invalid basic block argument to remove!");
84     return removeIncomingValue(Idx, DeletePHIIfEmpty);
85   }
86
87   /// getBasicBlockIndex - Return the first index of the specified basic 
88   /// block in the value list for this PHI.  Returns -1 if no instance.
89   ///
90   int getBasicBlockIndex(const BasicBlock *BB) const {
91     for (unsigned i = 0; i < Operands.size()/2; ++i) 
92       if (getIncomingBlock(i) == BB) return i;
93     return -1;
94   }
95
96   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
97     return getIncomingValue(getBasicBlockIndex(BB));
98   }
99
100   /// Methods for support type inquiry through isa, cast, and dyn_cast:
101   static inline bool classof(const PHINode *) { return true; }
102   static inline bool classof(const Instruction *I) {
103     return I->getOpcode() == Instruction::PHINode; 
104   }
105   static inline bool classof(const Value *V) {
106     return isa<Instruction>(V) && classof(cast<Instruction>(V));
107   }
108 };
109
110 #endif