Adjust to changes in the User class.
[oota-llvm.git] / lib / VMCore / InstrTypes.cpp
1 //===-- InstrTypes.cpp - Implement Instruction subclasses -------*- C++ -*-===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements 
11 //
12 //===----------------------------------------------------------------------===//
13
14 #include "llvm/Instructions.h"
15 #include "llvm/Function.h"
16 #include "llvm/SymbolTable.h"
17 #include "llvm/Constant.h"
18 #include "llvm/Type.h"
19 #include <algorithm>  // find
20 using namespace llvm;
21
22 //===----------------------------------------------------------------------===//
23 //                            TerminatorInst Class
24 //===----------------------------------------------------------------------===//
25
26 TerminatorInst::TerminatorInst(Instruction::TermOps iType, Instruction *IB) 
27   : Instruction(Type::VoidTy, iType, "", IB) {
28 }
29
30 TerminatorInst::TerminatorInst(Instruction::TermOps iType, BasicBlock *IAE)
31   : Instruction(Type::VoidTy, iType, "", IAE) {
32 }
33
34
35
36 //===----------------------------------------------------------------------===//
37 //                               PHINode Class
38 //===----------------------------------------------------------------------===//
39
40 PHINode::PHINode(const PHINode &PN)
41   : Instruction(PN.getType(), Instruction::PHI) {
42   Operands.reserve(PN.Operands.size());
43   for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
44     Operands.push_back(Use(PN.Operands[i], this));
45     Operands.push_back(Use(PN.Operands[i+1], this));
46   }
47 }
48
49 // removeIncomingValue - Remove an incoming value.  This is useful if a
50 // predecessor basic block is deleted.
51 Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
52   assert(Idx*2 < Operands.size() && "BB not in PHI node!");
53   Value *Removed = Operands[Idx*2];
54   Operands.erase(Operands.begin()+Idx*2,     // Erase Value and BasicBlock
55                  Operands.begin()+Idx*2+2);
56
57   // If the PHI node is dead, because it has zero entries, nuke it now.
58   if (getNumOperands() == 0 && DeletePHIIfEmpty) {
59     // If anyone is using this PHI, make them use a dummy value instead...
60     replaceAllUsesWith(Constant::getNullValue(getType()));
61     getParent()->getInstList().erase(this);
62   }
63   return Removed;
64 }