Simplify conditional and fix LICM/2004-11-17-UndefIndexCrash.ll
[oota-llvm.git] / lib / VMCore / InstrTypes.cpp
index decb4ad386412ef4981b4d943f737f65ba790fad..5d0e727ccc611082a23b5d99f9ca22ebd605270a 100644 (file)
@@ -1,73 +1,64 @@
-//===-- InstrTypes.cpp - Implement Instruction subclasses --------*- C++ -*--=//
+//===-- InstrTypes.cpp - Implement Instruction subclasses -------*- 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 implements 
 //
 //===----------------------------------------------------------------------===//
 
-#include "llvm/iOther.h"
-#include "llvm/BasicBlock.h"
-#include "llvm/Method.h"
+#include "llvm/Instructions.h"
+#include "llvm/Function.h"
 #include "llvm/SymbolTable.h"
+#include "llvm/Constant.h"
 #include "llvm/Type.h"
-#include <algorithm>
+#include <algorithm>  // find
+using namespace llvm;
 
 //===----------------------------------------------------------------------===//
 //                            TerminatorInst Class
 //===----------------------------------------------------------------------===//
 
-TerminatorInst::TerminatorInst(unsigned iType
-  : Instruction(Type::VoidTy, iType, "") {
+TerminatorInst::TerminatorInst(Instruction::TermOps iType, Instruction *IB
+  : Instruction(Type::VoidTy, iType, "", IB) {
 }
 
-
-//===----------------------------------------------------------------------===//
-//                            MethodArgument Class
-//===----------------------------------------------------------------------===//
-
-// Specialize setName to take care of symbol table majik
-void MethodArgument::setName(const string &name) {
-  Method *P;
-  if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
-  Value::setName(name);
-  if (P && hasName()) P->getSymbolTable()->insert(this);
+TerminatorInst::TerminatorInst(Instruction::TermOps iType, BasicBlock *IAE)
+  : Instruction(Type::VoidTy, iType, "", IAE) {
 }
 
 
+
 //===----------------------------------------------------------------------===//
 //                               PHINode Class
 //===----------------------------------------------------------------------===//
 
-PHINode::PHINode(const Type *Ty, const string &name) 
-  : Instruction(Ty, Instruction::PHINode, name) {
-}
-
-PHINode::PHINode(const PHINode &PN) 
-  : Instruction(PN.getType(), Instruction::PHINode) {
-  
-  for (unsigned i = 0; i < PN.IncomingValues.size(); i++)
-    IncomingValues.push_back(Use(PN.IncomingValues[i], this));
-}
-
-void PHINode::dropAllReferences() {
-  IncomingValues.clear();
-}
-
-bool PHINode::setOperand(unsigned i, Value *Val) {
-  assert(Val && "PHI node must only reference nonnull definitions!");
-  if (i >= IncomingValues.size()) return false;
-
-  IncomingValues[i] = Val;
-  return true;
-}
-
-void PHINode::addIncoming(Value *D) {
-  IncomingValues.push_back(Use(D, this));
+PHINode::PHINode(const PHINode &PN)
+  : Instruction(PN.getType(), Instruction::PHI) {
+  Operands.reserve(PN.Operands.size());
+  for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
+    Operands.push_back(Use(PN.Operands[i], this));
+    Operands.push_back(Use(PN.Operands[i+1], this));
+  }
 }
 
 // removeIncomingValue - Remove an incoming value.  This is useful if a
 // predecessor basic block is deleted.
-Value *PHINode::removeIncomingValue(unsigned idx) {
-  Value *Removed = IncomingValues[idx];
-  IncomingValues.erase(IncomingValues.begin()+idx);
+Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
+  assert(Idx*2 < Operands.size() && "BB not in PHI node!");
+  Value *Removed = Operands[Idx*2];
+  Operands.erase(Operands.begin()+Idx*2,     // Erase Value and BasicBlock
+                 Operands.begin()+Idx*2+2);
+
+  // 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;
 }