An "autoconf wrapper" for the infamous windows.h file
[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   PHINode(const Type *Ty, const std::string &Name, BasicBlock *InsertAtEnd)
40     : Instruction(Ty, Instruction::PHI, Name, InsertAtEnd) {
41   }
42
43   virtual Instruction *clone() const { return new PHINode(*this); }
44
45   /// getNumIncomingValues - Return the number of incoming edges
46   ///
47   unsigned getNumIncomingValues() const { return Operands.size()/2; }
48
49   /// getIncomingValue - Return incoming value #x
50   ///
51   Value *getIncomingValue(unsigned i) const {
52     assert(i*2 < Operands.size() && "Invalid value number!");
53     return Operands[i*2];
54   }
55   void setIncomingValue(unsigned i, Value *V) {
56     assert(i*2 < Operands.size() && "Invalid value number!");
57     Operands[i*2] = V;
58   }
59   inline unsigned getOperandNumForIncomingValue(unsigned i) {
60     return i*2;
61   }
62
63   /// getIncomingBlock - Return incoming basic block #x
64   ///
65   BasicBlock *getIncomingBlock(unsigned i) const { 
66     assert(i*2+1 < Operands.size() && "Invalid value number!");
67     return reinterpret_cast<BasicBlock*>(Operands[i*2+1].get());
68   }
69   void setIncomingBlock(unsigned i, BasicBlock *BB) {
70     assert(i*2+1 < Operands.size() && "Invalid value number!");
71     Operands[i*2+1] = reinterpret_cast<Value*>(BB);
72   }
73   unsigned getOperandNumForIncomingBlock(unsigned i) {
74     return i*2+1;
75   }
76
77   /// addIncoming - Add an incoming value to the end of the PHI list
78   ///
79   void addIncoming(Value *D, BasicBlock *BB) {
80     assert(getType() == D->getType() &&
81            "All operands to PHI node must be the same type as the PHI node!");
82     Operands.push_back(Use(D, this));
83     Operands.push_back(Use(reinterpret_cast<Value*>(BB), this));
84   }
85   
86   /// removeIncomingValue - Remove an incoming value.  This is useful if a
87   /// predecessor basic block is deleted.  The value removed is returned.
88   ///
89   /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
90   /// is true), the PHI node is destroyed and any uses of it are replaced with
91   /// dummy values.  The only time there should be zero incoming values to a PHI
92   /// node is when the block is dead, so this strategy is sound.
93   ///
94   Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
95
96   Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
97     int Idx = getBasicBlockIndex(BB);
98     assert(Idx >= 0 && "Invalid basic block argument to remove!");
99     return removeIncomingValue(Idx, DeletePHIIfEmpty);
100   }
101
102   /// getBasicBlockIndex - Return the first index of the specified basic 
103   /// block in the value list for this PHI.  Returns -1 if no instance.
104   ///
105   int getBasicBlockIndex(const BasicBlock *BB) const {
106     for (unsigned i = 0; i < Operands.size()/2; ++i) 
107       if (getIncomingBlock(i) == BB) return i;
108     return -1;
109   }
110
111   Value *getIncomingValueForBlock(const BasicBlock *BB) const {
112     return getIncomingValue(getBasicBlockIndex(BB));
113   }
114
115   /// Methods for support type inquiry through isa, cast, and dyn_cast:
116   static inline bool classof(const PHINode *) { return true; }
117   static inline bool classof(const Instruction *I) {
118     return I->getOpcode() == Instruction::PHI; 
119   }
120   static inline bool classof(const Value *V) {
121     return isa<Instruction>(V) && classof(cast<Instruction>(V));
122   }
123 };
124
125 } // End llvm namespace
126
127 #endif