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 //===----------------------------------------------------------------------===//
30 // FunctionArgument Class
31 //===----------------------------------------------------------------------===//
33 // Specialize setName to take care of symbol table majik
34 void FunctionArgument::setName(const std::string &name, SymbolTable *ST) {
36 assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
37 "Invalid symtab argument!");
38 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
40 if (P && hasName()) P->getSymbolTable()->insert(this);
44 //===----------------------------------------------------------------------===//
46 //===----------------------------------------------------------------------===//
48 PHINode::PHINode(const Type *Ty, const std::string &name)
49 : Instruction(Ty, Instruction::PHINode, name) {
52 PHINode::PHINode(const PHINode &PN)
53 : Instruction(PN.getType(), Instruction::PHINode) {
54 Operands.reserve(PN.Operands.size());
55 for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
56 Operands.push_back(Use(PN.Operands[i], this));
57 Operands.push_back(Use(PN.Operands[i+1], this));
61 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
62 assert(getType() == D->getType() &&
63 "All operands to PHI node must be the same type as the PHI node!");
64 Operands.push_back(Use(D, this));
65 Operands.push_back(Use(BB, this));
68 // removeIncomingValue - Remove an incoming value. This is useful if a
69 // predecessor basic block is deleted.
70 Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
71 op_iterator Idx = find(Operands.begin(), Operands.end(), (const Value*)BB);
72 assert(Idx != Operands.end() && "BB not in PHI node!");
73 --Idx; // Back up to value prior to Basic block
74 Value *Removed = *Idx;
75 Operands.erase(Idx, Idx+2); // Erase Value and BasicBlock