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