Refactor code to make it useful outside of Constants.cpp
[oota-llvm.git] / lib / VMCore / InstrTypes.cpp
1 //===-- InstrTypes.cpp - Implement Instruction subclasses -------*- C++ -*-===//
2 //
3 // This file implements 
4 //
5 //===----------------------------------------------------------------------===//
6
7 #include "llvm/iOther.h"
8 #include "llvm/iPHINode.h"
9 #include "llvm/Function.h"
10 #include "llvm/SymbolTable.h"
11 #include "llvm/Constant.h"
12 #include "llvm/Type.h"
13 #include <algorithm>  // find
14
15 //===----------------------------------------------------------------------===//
16 //                            TerminatorInst Class
17 //===----------------------------------------------------------------------===//
18
19 TerminatorInst::TerminatorInst(Instruction::TermOps iType, Instruction *IB) 
20   : Instruction(Type::VoidTy, iType, "", IB) {
21 }
22
23 //===----------------------------------------------------------------------===//
24 //                               PHINode Class
25 //===----------------------------------------------------------------------===//
26
27 PHINode::PHINode(const PHINode &PN)
28   : Instruction(PN.getType(), Instruction::PHINode) {
29   Operands.reserve(PN.Operands.size());
30   for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
31     Operands.push_back(Use(PN.Operands[i], this));
32     Operands.push_back(Use(PN.Operands[i+1], this));
33   }
34 }
35
36 // removeIncomingValue - Remove an incoming value.  This is useful if a
37 // predecessor basic block is deleted.
38 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
39   assert(Idx*2 < Operands.size() && "BB not in PHI node!");
40   Value *Removed = Operands[Idx*2];
41   Operands.erase(Operands.begin()+Idx*2,     // Erase Value and BasicBlock
42                  Operands.begin()+Idx*2+2);
43
44   // If the PHI node is dead, because it has zero entries, nuke it now.
45   if (getNumOperands() == 0 && DeletePHIIfEmpty) {
46     // If anyone is using this PHI, make them use a dummy value instead...
47     replaceAllUsesWith(Constant::getNullValue(getType()));
48     getParent()->getInstList().erase(this);
49   }
50   return Removed;
51 }