1 //===-- InstrTypes.cpp - Implement Instruction subclasses --------*- C++ -*--=//
3 // This file implements
5 //===----------------------------------------------------------------------===//
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"
14 // TODO: Move to getUnaryOperator iUnary.cpp when and if it exists!
15 UnaryOperator *UnaryOperator::create(unsigned Op, Value *Source) {
18 cerr << "Don't know how to GetUnaryOperator " << Op << endl;
23 //===----------------------------------------------------------------------===//
24 // TerminatorInst Class
25 //===----------------------------------------------------------------------===//
27 TerminatorInst::TerminatorInst(unsigned iType)
28 : Instruction(Type::VoidTy, iType, "") {
32 //===----------------------------------------------------------------------===//
33 // MethodArgument Class
34 //===----------------------------------------------------------------------===//
36 // Specialize setName to take care of symbol table majik
37 void MethodArgument::setName(const string &name) {
39 if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
41 if (P && hasName()) P->getSymbolTable()->insert(this);
45 //===----------------------------------------------------------------------===//
47 //===----------------------------------------------------------------------===//
49 PHINode::PHINode(const Type *Ty, const string &name)
50 : Instruction(Ty, Instruction::PHINode, name) {
53 PHINode::PHINode(const PHINode &PN)
54 : Instruction(PN.getType(), Instruction::PHINode) {
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)));
62 void PHINode::dropAllReferences() {
63 IncomingValues.clear();
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;
71 IncomingValues[i/2].second = Val->castBasicBlockAsserting();
73 IncomingValues[i/2].first = Val;
78 void PHINode::addIncoming(Value *D, BasicBlock *BB) {
79 IncomingValues.push_back(make_pair(Use(D, this), BasicBlockUse(BB, this)));
84 inline FindBBEntry(const BasicBlock *bb) : BB(bb) {}
85 inline bool operator()(const pair<Use,BasicBlockUse> &Entry) {
86 return Entry.second == BB;
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);