1 //===-- InstrTypes.cpp - Implement Instruction subclasses --------*- C++ -*--=//
3 // This file implements
5 //===----------------------------------------------------------------------===//
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
15 //===----------------------------------------------------------------------===//
16 // TerminatorInst Class
17 //===----------------------------------------------------------------------===//
19 TerminatorInst::TerminatorInst(Instruction::TermOps iType)
20 : Instruction(Type::VoidTy, iType, "") {
23 TerminatorInst::TerminatorInst(const Type *Ty, Instruction::TermOps iType,
24 const std::string &Name = "")
25 : Instruction(Ty, iType, Name) {
29 //===----------------------------------------------------------------------===//
31 //===----------------------------------------------------------------------===//
33 PHINode::PHINode(const Type *Ty, const std::string &name)
34 : Instruction(Ty, Instruction::PHINode, name) {
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));
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));
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