9d3262cbda7209a70a66be854ce93ce75f63d56e
[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 // TODO: Move to getUnaryOperator iUnary.cpp when and if it exists!
15 UnaryOperator *UnaryOperator::create(UnaryOps Op, Value *Source) {
16   switch (Op) {
17   default:
18     cerr << "Don't know how to GetUnaryOperator " << Op << endl;
19     return 0;
20   }
21 }
22
23 //===----------------------------------------------------------------------===//
24 //                            TerminatorInst Class
25 //===----------------------------------------------------------------------===//
26
27 TerminatorInst::TerminatorInst(unsigned iType) 
28   : Instruction(Type::VoidTy, iType, "") {
29 }
30
31
32 //===----------------------------------------------------------------------===//
33 //                            MethodArgument Class
34 //===----------------------------------------------------------------------===//
35
36 // Specialize setName to take care of symbol table majik
37 void MethodArgument::setName(const string &name) {
38   Method *P;
39   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
40   Value::setName(name);
41   if (P && hasName()) P->getSymbolTable()->insert(this);
42 }
43
44
45 //===----------------------------------------------------------------------===//
46 //                               PHINode Class
47 //===----------------------------------------------------------------------===//
48
49 PHINode::PHINode(const Type *Ty, const string &name) 
50   : Instruction(Ty, Instruction::PHINode, name) {
51 }
52
53 PHINode::PHINode(const PHINode &PN) 
54   : Instruction(PN.getType(), Instruction::PHINode) {
55   Operands.reserve(PN.Operands.size());
56   for (unsigned i = 0; i < PN.Operands.size(); i+=2) {
57     Operands.push_back(Use(PN.Operands[i], this));
58     Operands.push_back(Use(PN.Operands[i+1], this));
59   }
60 }
61
62 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
63   Operands.push_back(Use(D, this));
64   Operands.push_back(Use(BB, this));
65 }
66
67 // removeIncomingValue - Remove an incoming value.  This is useful if a
68 // predecessor basic block is deleted.
69 Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
70   op_iterator Idx = find(Operands.begin(), Operands.end(), (const Value*)BB);
71   assert(Idx != Operands.end() && "BB not in PHI node!");
72   --Idx;  // Back up to value prior to Basic block
73   Value *Removed = *Idx;
74   Operands.erase(Idx, Idx+2);  // Erase Value and BasicBlock
75   return Removed;
76 }