Start using the new function cloning header
[oota-llvm.git] / lib / VMCore / InstrTypes.cpp
index 6fb18693fe2a3dd3327dd6f6333257f4bd29caf7..ff9d4059d24329ddf57df6e32a7e56838132cfff 100644 (file)
@@ -1,4 +1,4 @@
-//===-- InstrTypes.cpp - Implement Instruction subclasses --------*- C++ -*--=//
+//===-- InstrTypes.cpp - Implement Instruction subclasses -------*- C++ -*-===//
 //
 // This file implements 
 //
@@ -6,9 +6,9 @@
 
 #include "llvm/iOther.h"
 #include "llvm/iPHINode.h"
-#include "llvm/BasicBlock.h"
 #include "llvm/Function.h"
 #include "llvm/SymbolTable.h"
+#include "llvm/Constant.h"
 #include "llvm/Type.h"
 #include <algorithm>  // find
 
 //                            TerminatorInst Class
 //===----------------------------------------------------------------------===//
 
-TerminatorInst::TerminatorInst(Instruction::TermOps iType) 
-  : Instruction(Type::VoidTy, iType, "") {
+TerminatorInst::TerminatorInst(Instruction::TermOps iType, Instruction *IB
+  : Instruction(Type::VoidTy, iType, "", IB) {
 }
 
-TerminatorInst::TerminatorInst(const Type *Ty, Instruction::TermOps iType,
-                              const std::string &Name = "")
-  : Instruction(Ty, iType, Name) {
-}
-
-
 //===----------------------------------------------------------------------===//
 //                               PHINode Class
 //===----------------------------------------------------------------------===//
 
-PHINode::PHINode(const Type *Ty, const std::string &name) 
-  : Instruction(Ty, Instruction::PHINode, name) {
-}
-
-PHINode::PHINode(const PHINode &PN) 
+PHINode::PHINode(const PHINode &PN)
   : Instruction(PN.getType(), Instruction::PHINode) {
   Operands.reserve(PN.Operands.size());
   for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
@@ -52,11 +42,19 @@ void PHINode::addIncoming(Value *D, BasicBlock *BB) {
 
 // removeIncomingValue - Remove an incoming value.  This is useful if a
 // predecessor basic block is deleted.
-Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
+Value *PHINode::removeIncomingValue(const BasicBlock *BB,
+                                    bool DeletePHIIfEmpty) {
   op_iterator Idx = find(Operands.begin(), Operands.end(), (const Value*)BB);
   assert(Idx != Operands.end() && "BB not in PHI node!");
   --Idx;  // Back up to value prior to Basic block
   Value *Removed = *Idx;
   Operands.erase(Idx, Idx+2);  // Erase Value and BasicBlock
+
+  // If the PHI node is dead, because it has zero entries, nuke it now.
+  if (getNumOperands() == 0 && DeletePHIIfEmpty) {
+    // If anyone is using this PHI, make them use a dummy value instead...
+    replaceAllUsesWith(Constant::getNullValue(getType()));
+    getParent()->getInstList().erase(this);
+  }
   return Removed;
 }