Change references from Method to Function
[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/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
14
15 //===----------------------------------------------------------------------===//
16 //                            TerminatorInst Class
17 //===----------------------------------------------------------------------===//
18
19 TerminatorInst::TerminatorInst(Instruction::TermOps iType) 
20   : Instruction(Type::VoidTy, iType, "") {
21 }
22
23 TerminatorInst::TerminatorInst(const Type *Ty, Instruction::TermOps iType,
24                                const std::string &Name = "")
25   : Instruction(Ty, iType, Name) {
26 }
27
28
29 //===----------------------------------------------------------------------===//
30 //                            FunctionArgument Class
31 //===----------------------------------------------------------------------===//
32
33 // Specialize setName to take care of symbol table majik
34 void FunctionArgument::setName(const std::string &name, SymbolTable *ST) {
35   Function *P;
36   assert((ST == 0 || (!getParent() || ST == getParent()->getSymbolTable())) &&
37          "Invalid symtab argument!");
38   if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
39   Value::setName(name);
40   if (P && hasName()) P->getSymbolTable()->insert(this);
41 }
42
43
44 //===----------------------------------------------------------------------===//
45 //                               PHINode Class
46 //===----------------------------------------------------------------------===//
47
48 PHINode::PHINode(const Type *Ty, const std::string &name) 
49   : Instruction(Ty, Instruction::PHINode, name) {
50 }
51
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));
58   }
59 }
60
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));
66 }
67
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
76   return Removed;
77 }