Support abstract types
[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/BasicBlock.h"
9 #include "llvm/Method.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(unsigned iType) 
19   : Instruction(Type::VoidTy, iType, "") {
20 }
21
22
23 //===----------------------------------------------------------------------===//
24 //                            MethodArgument Class
25 //===----------------------------------------------------------------------===//
26
27 // Specialize setName to take care of symbol table majik
28 void MethodArgument::setName(const string &name, SymbolTable *ST) {
29   Method *P;
30   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
31          "Invalid symtab argument!");
32   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
33   Value::setName(name);
34   if (P && hasName()) P->getSymbolTable()->insert(this);
35 }
36
37
38 //===----------------------------------------------------------------------===//
39 //                               PHINode Class
40 //===----------------------------------------------------------------------===//
41
42 PHINode::PHINode(const Type *Ty, const string &name) 
43   : Instruction(Ty, Instruction::PHINode, name) {
44 }
45
46 PHINode::PHINode(const PHINode &PN) 
47   : Instruction(PN.getType(), Instruction::PHINode) {
48   Operands.reserve(PN.Operands.size());
49   for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
50     Operands.push_back(Use(PN.Operands[i], this));
51     Operands.push_back(Use(PN.Operands[i+1], this));
52   }
53 }
54
55 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
56   Operands.push_back(Use(D, this));
57   Operands.push_back(Use(BB, this));
58 }
59
60 // removeIncomingValue - Remove an incoming value.  This is useful if a
61 // predecessor basic block is deleted.
62 Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
63   op_iterator Idx = find(Operands.begin(), Operands.end(), (const Value*)BB);
64   assert(Idx != Operands.end() && "BB not in PHI node!");
65   --Idx;  // Back up to value prior to Basic block
66   Value *Removed = *Idx;
67   Operands.erase(Idx, Idx+2);  // Erase Value and BasicBlock
68   return Removed;
69 }