Fix bug: Assembler/2003-03-03-DuplicateConstant.ll
[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 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
37   assert(getType() == D->getType() &&
38          "All operands to PHI node must be the same type as the PHI node!");
39   Operands.push_back(Use(D, this));
40   Operands.push_back(Use(BB, this));
41 }
42
43 // removeIncomingValue - Remove an incoming value.  This is useful if a
44 // predecessor basic block is deleted.
45 Value *PHINode::removeIncomingValue(const BasicBlock *BB,
46                                     bool DeletePHIIfEmpty) {
47   op_iterator Idx = find(Operands.begin(), Operands.end(), (const Value*)BB);
48   assert(Idx != Operands.end() && "BB not in PHI node!");
49   --Idx;  // Back up to value prior to Basic block
50   Value *Removed = *Idx;
51   Operands.erase(Idx, Idx+2);  // Erase Value and BasicBlock
52
53   // If the PHI node is dead, because it has zero entries, nuke it now.
54   if (getNumOperands() == 0 && DeletePHIIfEmpty) {
55     // If anyone is using this PHI, make them use a dummy value instead...
56     replaceAllUsesWith(Constant::getNullValue(getType()));
57     getParent()->getInstList().erase(this);
58   }
59   return Removed;
60 }