954719aa95070aae6b73a402213c899122b10670
[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/Type.h"
12 #include <algorithm>  // find
13
14 //===----------------------------------------------------------------------===//
15 //                            TerminatorInst Class
16 //===----------------------------------------------------------------------===//
17
18 TerminatorInst::TerminatorInst(Instruction::TermOps iType) 
19   : Instruction(Type::VoidTy, iType, "") {
20 }
21
22 TerminatorInst::TerminatorInst(const Type *Ty, Instruction::TermOps iType,
23                                const std::string &Name)
24   : Instruction(Ty, iType, Name) {
25 }
26
27
28 //===----------------------------------------------------------------------===//
29 //                               PHINode Class
30 //===----------------------------------------------------------------------===//
31
32 PHINode::PHINode(const Type *Ty, const std::string &name) 
33   : Instruction(Ty, Instruction::PHINode, name) {
34 }
35
36 PHINode::PHINode(const PHINode &PN) 
37   : Instruction(PN.getType(), Instruction::PHINode) {
38   Operands.reserve(PN.Operands.size());
39   for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
40     Operands.push_back(Use(PN.Operands[i], this));
41     Operands.push_back(Use(PN.Operands[i+1], this));
42   }
43 }
44
45 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
46   assert(getType() == D->getType() &&
47          "All operands to PHI node must be the same type as the PHI node!");
48   Operands.push_back(Use(D, this));
49   Operands.push_back(Use(BB, this));
50 }
51
52 // removeIncomingValue - Remove an incoming value.  This is useful if a
53 // predecessor basic block is deleted.
54 Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
55   op_iterator Idx = find(Operands.begin(), Operands.end(), (const Value*)BB);
56   assert(Idx != Operands.end() && "BB not in PHI node!");
57   --Idx;  // Back up to value prior to Basic block
58   Value *Removed = *Idx;
59   Operands.erase(Idx, Idx+2);  // Erase Value and BasicBlock
60   return Removed;
61 }