Fix failing test cases with joined live intervals. It turns out that
[oota-llvm.git] / include / llvm / iPHINode.h
index 93b6e7cc80e50cc3f870e1d09b739bfb52732308..399c761670b2125386a06c6397627e62dab676ba 100644 (file)
@@ -1,4 +1,11 @@
-//===-- llvm/iPHINode.h - PHI instruction definition -------------*- C++ -*--=//
+//===-- llvm/iPHINode.h - PHI instruction definition ------------*- C++ -*-===//
+// 
+//                     The LLVM Compiler Infrastructure
+//
+// This file was developed by the LLVM research group and is distributed under
+// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// 
+//===----------------------------------------------------------------------===//
 //
 // This file defines the PHINode class.
 //
@@ -8,6 +15,9 @@
 #define LLVM_IPHINODE_H
 
 #include "llvm/Instruction.h"
+
+namespace llvm {
+
 class BasicBlock;
 
 //===----------------------------------------------------------------------===//
@@ -23,7 +33,7 @@ class PHINode : public Instruction {
 public:
   PHINode(const Type *Ty, const std::string &Name = "",
           Instruction *InsertBefore = 0)
-    : Instruction(Ty, Instruction::PHINode, Name, InsertBefore) {
+    : Instruction(Ty, Instruction::PHI, Name, InsertBefore) {
   }
 
   virtual Instruction *clone() const { return new PHINode(*this); }
@@ -33,13 +43,12 @@ public:
   unsigned getNumIncomingValues() const { return Operands.size()/2; }
 
   /// getIncomingValue - Return incoming value #x
-  const Value *getIncomingValue(unsigned i) const {
-    return Operands[i*2];
-  }
-  Value *getIncomingValue(unsigned i) {
+  Value *getIncomingValue(unsigned i) const {
+    assert(i*2 < Operands.size() && "Invalid value number!");
     return Operands[i*2];
   }
   void setIncomingValue(unsigned i, Value *V) {
+    assert(i*2 < Operands.size() && "Invalid value number!");
     Operands[i*2] = V;
   }
   inline unsigned getOperandNumForIncomingValue(unsigned i) {
@@ -47,25 +56,41 @@ public:
   }
 
   /// getIncomingBlock - Return incoming basic block #x
-  const BasicBlock *getIncomingBlock(unsigned i) const { 
-    return (const BasicBlock*)Operands[i*2+1].get();
+  BasicBlock *getIncomingBlock(unsigned i) const { 
+    assert(i*2+1 < Operands.size() && "Invalid value number!");
+    return reinterpret_cast<BasicBlock*>(Operands[i*2+1].get());
   }
-  inline BasicBlock *getIncomingBlock(unsigned i) { 
-    return (BasicBlock*)Operands[i*2+1].get();
+  void setIncomingBlock(unsigned i, BasicBlock *BB) {
+    assert(i*2+1 < Operands.size() && "Invalid value number!");
+    Operands[i*2+1] = reinterpret_cast<Value*>(BB);
   }
-  inline void setIncomingBlock(unsigned i, BasicBlock *BB) {
-    Operands[i*2+1] = (Value*)BB;
-  }
-  inline unsigned getOperandNumForIncomingBlock(unsigned i) {
+  unsigned getOperandNumForIncomingBlock(unsigned i) {
     return i*2+1;
   }
 
   /// addIncoming - Add an incoming value to the end of the PHI list
-  void addIncoming(Value *D, BasicBlock *BB);
+  void addIncoming(Value *D, BasicBlock *BB) {
+    assert(getType() == D->getType() &&
+           "All operands to PHI node must be the same type as the PHI node!");
+    Operands.push_back(Use(D, this));
+    Operands.push_back(Use(reinterpret_cast<Value*>(BB), this));
+  }
   
   /// removeIncomingValue - Remove an incoming value.  This is useful if a
   /// predecessor basic block is deleted.  The value removed is returned.
-  Value *removeIncomingValue(const BasicBlock *BB);
+  ///
+  /// If the last incoming value for a PHI node is removed (and DeletePHIIfEmpty
+  /// is true), the PHI node is destroyed and any uses of it are replaced with
+  /// dummy values.  The only time there should be zero incoming values to a PHI
+  /// node is when the block is dead, so this strategy is sound.
+  ///
+  Value *removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty = true);
+
+  Value *removeIncomingValue(const BasicBlock *BB, bool DeletePHIIfEmpty =true){
+    int Idx = getBasicBlockIndex(BB);
+    assert(Idx >= 0 && "Invalid basic block argument to remove!");
+    return removeIncomingValue(Idx, DeletePHIIfEmpty);
+  }
 
   /// getBasicBlockIndex - Return the first index of the specified basic 
   /// block in the value list for this PHI.  Returns -1 if no instance.
@@ -76,14 +101,20 @@ public:
     return -1;
   }
 
+  Value *getIncomingValueForBlock(const BasicBlock *BB) const {
+    return getIncomingValue(getBasicBlockIndex(BB));
+  }
+
   /// Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const PHINode *) { return true; }
   static inline bool classof(const Instruction *I) {
-    return I->getOpcode() == Instruction::PHINode
+    return I->getOpcode() == Instruction::PHI; 
   }
   static inline bool classof(const Value *V) {
     return isa<Instruction>(V) && classof(cast<Instruction>(V));
   }
 };
 
+} // End llvm namespace
+
 #endif