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