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