2001-06-06 20:29:01 +00:00
|
|
|
//===-- InstrTypes.cpp - Implement Instruction subclasses --------*- C++ -*--=//
|
|
|
|
//
|
|
|
|
// This file implements
|
|
|
|
//
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
#include "llvm/iOther.h"
|
|
|
|
#include "llvm/BasicBlock.h"
|
|
|
|
#include "llvm/Method.h"
|
|
|
|
#include "llvm/SymbolTable.h"
|
|
|
|
#include "llvm/Type.h"
|
|
|
|
#include <algorithm>
|
|
|
|
|
2001-06-25 07:33:13 +00:00
|
|
|
// TODO: Move to getUnaryOperator iUnary.cpp when and if it exists!
|
|
|
|
UnaryOperator *UnaryOperator::create(unsigned Op, Value *Source) {
|
|
|
|
switch (Op) {
|
|
|
|
default:
|
|
|
|
cerr << "Don't know how to GetUnaryOperator " << Op << endl;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2001-06-06 20:29:01 +00:00
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// TerminatorInst Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
TerminatorInst::TerminatorInst(unsigned iType)
|
|
|
|
: Instruction(Type::VoidTy, iType, "") {
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// MethodArgument Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
// Specialize setName to take care of symbol table majik
|
|
|
|
void MethodArgument::setName(const string &name) {
|
|
|
|
Method *P;
|
|
|
|
if ((P = getParent()) && hasName()) P->getSymbolTable()->remove(this);
|
|
|
|
Value::setName(name);
|
|
|
|
if (P && hasName()) P->getSymbolTable()->insert(this);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
// PHINode Class
|
|
|
|
//===----------------------------------------------------------------------===//
|
|
|
|
|
|
|
|
PHINode::PHINode(const Type *Ty, const string &name)
|
|
|
|
: Instruction(Ty, Instruction::PHINode, name) {
|
|
|
|
}
|
|
|
|
|
|
|
|
PHINode::PHINode(const PHINode &PN)
|
|
|
|
: Instruction(PN.getType(), Instruction::PHINode) {
|
|
|
|
|
|
|
|
for (unsigned i = 0; i < PN.IncomingValues.size(); i++)
|
2001-06-11 15:04:40 +00:00
|
|
|
IncomingValues.push_back(
|
|
|
|
make_pair(Use(PN.IncomingValues[i].first, this),
|
|
|
|
BasicBlockUse(PN.IncomingValues[i].second, this)));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void PHINode::dropAllReferences() {
|
|
|
|
IncomingValues.clear();
|
|
|
|
}
|
|
|
|
|
|
|
|
bool PHINode::setOperand(unsigned i, Value *Val) {
|
|
|
|
assert(Val && "PHI node must only reference nonnull definitions!");
|
2001-06-11 15:04:40 +00:00
|
|
|
if (i >= IncomingValues.size()*2) return false;
|
2001-06-06 20:29:01 +00:00
|
|
|
|
2001-06-11 15:04:40 +00:00
|
|
|
if (i & 1) {
|
|
|
|
assert(Val->getValueType() == BasicBlockVal && "Not a BB!");
|
|
|
|
IncomingValues[i/2].second = (BasicBlock*)Val;
|
|
|
|
} else {
|
|
|
|
IncomingValues[i/2].first = Val;
|
|
|
|
}
|
2001-06-06 20:29:01 +00:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2001-06-11 15:04:40 +00:00
|
|
|
void PHINode::addIncoming(Value *D, BasicBlock *BB) {
|
|
|
|
IncomingValues.push_back(make_pair(Use(D, this), BasicBlockUse(BB, this)));
|
2001-06-06 20:29:01 +00:00
|
|
|
}
|
|
|
|
|
2001-06-11 15:04:40 +00:00
|
|
|
struct FindBBEntry {
|
|
|
|
const BasicBlock *BB;
|
|
|
|
inline FindBBEntry(const BasicBlock *bb) : BB(bb) {}
|
|
|
|
inline bool operator()(const pair<Use,BasicBlockUse> &Entry) {
|
|
|
|
return Entry.second == BB;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
|
2001-06-07 16:59:37 +00:00
|
|
|
// removeIncomingValue - Remove an incoming value. This is useful if a
|
|
|
|
// predecessor basic block is deleted.
|
2001-06-11 15:04:40 +00:00
|
|
|
Value *PHINode::removeIncomingValue(const BasicBlock *BB) {
|
|
|
|
vector<PairTy>::iterator Idx = find_if(IncomingValues.begin(),
|
|
|
|
IncomingValues.end(), FindBBEntry(BB));
|
|
|
|
assert(Idx != IncomingValues.end() && "BB not in PHI node!");
|
|
|
|
Value *Removed = Idx->first;
|
|
|
|
IncomingValues.erase(Idx);
|
2001-06-07 16:59:37 +00:00
|
|
|
return Removed;
|
|
|
|
}
|