baafe0bc13108692e83cb4dddb3c958903dbd58b
[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>
13
14 // TODO: Move to getUnaryOperator iUnary.cpp when and if it exists!
15 UnaryOperator *UnaryOperator::create(unsigned 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   
56   for (unsigned i = 0; i < PN.IncomingValues.size(); i++)
57     IncomingValues.push_back(
58         make_pair(Use(PN.IncomingValues[i].first, this),
59                   BasicBlockUse(PN.IncomingValues[i].second, this)));
60 }
61
62 void PHINode::dropAllReferences() {
63   IncomingValues.clear();
64 }
65
66 bool PHINode::setOperand(unsigned i, Value *Val) {
67   assert(Val && "PHI node must only reference nonnull definitions!");
68   if (i >= IncomingValues.size()*2) return false;
69
70   if (i & 1) {
71     IncomingValues[i/2].second = Val->castBasicBlockAsserting();
72   } else {
73     IncomingValues[i/2].first  = Val;
74   }
75   return true;
76 }
77
78 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
79   IncomingValues.push_back(make_pair(Use(D, this), BasicBlockUse(BB, this)));
80 }
81
82 struct FindBBEntry {
83   const BasicBlock *BB;
84   inline FindBBEntry(const BasicBlock *bb) : BB(bb) {}
85   inline bool operator()(const pair<Use,BasicBlockUse> &Entry) {
86     return Entry.second == BB;
87   }
88 };
89
90
91 // removeIncomingValue - Remove an incoming value.  This is useful if a
92 // predecessor basic block is deleted.
93 Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
94   vector<PairTy>::iterator Idx = find_if(IncomingValues.begin(), 
95                                          IncomingValues.end(), FindBBEntry(BB));
96   assert(Idx != IncomingValues.end() && "BB not in PHI node!");
97   Value *Removed = Idx->first;
98   IncomingValues.erase(Idx);
99   return Removed;
100 }