//===-- Instructions.cpp - Implement the LLVM instructions ----------------===//
-//
+//
// The LLVM Compiler Infrastructure
//
// This file was developed by the LLVM research group and is distributed under
// the University of Illinois Open Source License. See LICENSE.TXT for details.
-//
+//
//===----------------------------------------------------------------------===//
//
-// This file implements the LLVM instructions...
+// This file implements all of the non-inline methods for the LLVM instruction
+// classes.
//
//===----------------------------------------------------------------------===//
#include "llvm/Support/CallSite.h"
using namespace llvm;
+unsigned CallSite::getCallingConv() const {
+ if (CallInst *CI = dyn_cast<CallInst>(I))
+ return CI->getCallingConv();
+ else
+ return cast<InvokeInst>(I)->getCallingConv();
+}
+void CallSite::setCallingConv(unsigned CC) {
+ if (CallInst *CI = dyn_cast<CallInst>(I))
+ CI->setCallingConv(CC);
+ else
+ cast<InvokeInst>(I)->setCallingConv(CC);
+}
+
+
+//===----------------------------------------------------------------------===//
+// TerminatorInst Class
+//===----------------------------------------------------------------------===//
+
+TerminatorInst::TerminatorInst(Instruction::TermOps iType,
+ Use *Ops, unsigned NumOps, Instruction *IB)
+ : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IB) {
+}
+
+TerminatorInst::TerminatorInst(Instruction::TermOps iType,
+ Use *Ops, unsigned NumOps, BasicBlock *IAE)
+ : Instruction(Type::VoidTy, iType, Ops, NumOps, "", IAE) {
+}
+
+
+
+//===----------------------------------------------------------------------===//
+// PHINode Class
+//===----------------------------------------------------------------------===//
+
+PHINode::PHINode(const PHINode &PN)
+ : Instruction(PN.getType(), Instruction::PHI,
+ new Use[PN.getNumOperands()], PN.getNumOperands()),
+ ReservedSpace(PN.getNumOperands()) {
+ Use *OL = OperandList;
+ for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) {
+ OL[i].init(PN.getOperand(i), this);
+ OL[i+1].init(PN.getOperand(i+1), this);
+ }
+}
+
+PHINode::~PHINode() {
+ delete [] OperandList;
+}
+
+// removeIncomingValue - Remove an incoming value. This is useful if a
+// predecessor basic block is deleted.
+Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) {
+ unsigned NumOps = getNumOperands();
+ Use *OL = OperandList;
+ assert(Idx*2 < NumOps && "BB not in PHI node!");
+ Value *Removed = OL[Idx*2];
+
+ // Move everything after this operand down.
+ //
+ // FIXME: we could just swap with the end of the list, then erase. However,
+ // client might not expect this to happen. The code as it is thrashes the
+ // use/def lists, which is kinda lame.
+ for (unsigned i = (Idx+1)*2; i != NumOps; i += 2) {
+ OL[i-2] = OL[i];
+ OL[i-2+1] = OL[i+1];
+ }
+
+ // Nuke the last value.
+ OL[NumOps-2].set(0);
+ OL[NumOps-2+1].set(0);
+ NumOperands = NumOps-2;
+
+ // If the PHI node is dead, because it has zero entries, nuke it now.
+ if (NumOps == 2 && DeletePHIIfEmpty) {
+ // If anyone is using this PHI, make them use a dummy value instead...
+ replaceAllUsesWith(UndefValue::get(getType()));
+ eraseFromParent();
+ }
+ return Removed;
+}
+
+/// resizeOperands - resize operands - This adjusts the length of the operands
+/// list according to the following behavior:
+/// 1. If NumOps == 0, grow the operand list in response to a push_back style
+/// of operation. This grows the number of ops by 1.5 times.
+/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
+/// 3. If NumOps == NumOperands, trim the reserved space.
+///
+void PHINode::resizeOperands(unsigned NumOps) {
+ if (NumOps == 0) {
+ NumOps = (getNumOperands())*3/2;
+ if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common.
+ } else if (NumOps*2 > NumOperands) {
+ // No resize needed.
+ if (ReservedSpace >= NumOps) return;
+ } else if (NumOps == NumOperands) {
+ if (ReservedSpace == NumOps) return;
+ } else {
+ return;
+ }
+
+ ReservedSpace = NumOps;
+ Use *NewOps = new Use[NumOps];
+ Use *OldOps = OperandList;
+ for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
+ NewOps[i].init(OldOps[i], this);
+ OldOps[i].set(0);
+ }
+ delete [] OldOps;
+ OperandList = NewOps;
+}
+
+/// hasConstantValue - If the specified PHI node always merges together the same
+/// value, return the value, otherwise return null.
+///
+Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const {
+ // If the PHI node only has one incoming value, eliminate the PHI node...
+ if (getNumIncomingValues() == 1)
+ return getIncomingValue(0);
+
+ // Otherwise if all of the incoming values are the same for the PHI, replace
+ // the PHI node with the incoming value.
+ //
+ Value *InVal = 0;
+ bool HasUndefInput = false;
+ for (unsigned i = 0, e = getNumIncomingValues(); i != e; ++i)
+ if (isa<UndefValue>(getIncomingValue(i)))
+ HasUndefInput = true;
+ else if (getIncomingValue(i) != this) // Not the PHI node itself...
+ if (InVal && getIncomingValue(i) != InVal)
+ return 0; // Not the same, bail out.
+ else
+ InVal = getIncomingValue(i);
+
+ // The only case that could cause InVal to be null is if we have a PHI node
+ // that only has entries for itself. In this case, there is no entry into the
+ // loop, so kill the PHI.
+ //
+ if (InVal == 0) InVal = UndefValue::get(getType());
+
+ // If we have a PHI node like phi(X, undef, X), where X is defined by some
+ // instruction, we cannot always return X as the result of the PHI node. Only
+ // do this if X is not an instruction (thus it must dominate the PHI block),
+ // or if the client is prepared to deal with this possibility.
+ if (HasUndefInput && !AllowNonDominatingInstruction)
+ if (Instruction *IV = dyn_cast<Instruction>(InVal))
+ // If it's in the entry block, it dominates everything.
+ if (IV->getParent() != &IV->getParent()->getParent()->front())
+ return 0; // Cannot guarantee that InVal dominates this PHINode.
+
+ // All of the incoming values are the same, return the value now.
+ return InVal;
+}
+
+
//===----------------------------------------------------------------------===//
// CallInst Implementation
//===----------------------------------------------------------------------===//
-void CallInst::init(Value *Func, const std::vector<Value*> &Params)
-{
- Operands.reserve(1+Params.size());
- Operands.push_back(Use(Func, this));
+CallInst::~CallInst() {
+ delete [] OperandList;
+}
+
+void CallInst::init(Value *Func, const std::vector<Value*> &Params) {
+ NumOperands = Params.size()+1;
+ Use *OL = OperandList = new Use[Params.size()+1];
+ OL[0].init(Func, this);
- const FunctionType *FTy =
+ const FunctionType *FTy =
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
- assert((Params.size() == FTy->getNumParams() ||
+ assert((Params.size() == FTy->getNumParams() ||
(FTy->isVarArg() && Params.size() > FTy->getNumParams())) &&
"Calling a function with bad signature");
- for (unsigned i = 0; i != Params.size(); i++)
- Operands.push_back(Use(Params[i], this));
+ for (unsigned i = 0, e = Params.size(); i != e; ++i)
+ OL[i+1].init(Params[i], this);
}
-void CallInst::init(Value *Func, Value *Actual1, Value *Actual2)
-{
- Operands.reserve(3);
- Operands.push_back(Use(Func, this));
-
- const FunctionType *MTy =
+void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) {
+ NumOperands = 3;
+ Use *OL = OperandList = new Use[3];
+ OL[0].init(Func, this);
+ OL[1].init(Actual1, this);
+ OL[2].init(Actual2, this);
+
+ const FunctionType *FTy =
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
- assert((MTy->getNumParams() == 2 ||
- (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
+ assert((FTy->getNumParams() == 2 ||
+ (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
"Calling a function with bad signature");
- Operands.push_back(Use(Actual1, this));
- Operands.push_back(Use(Actual2, this));
}
-void CallInst::init(Value *Func, Value *Actual)
-{
- Operands.reserve(2);
- Operands.push_back(Use(Func, this));
-
- const FunctionType *MTy =
+void CallInst::init(Value *Func, Value *Actual) {
+ NumOperands = 2;
+ Use *OL = OperandList = new Use[2];
+ OL[0].init(Func, this);
+ OL[1].init(Actual, this);
+
+ const FunctionType *FTy =
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
- assert((MTy->getNumParams() == 1 ||
- (MTy->isVarArg() && MTy->getNumParams() == 0)) &&
+ assert((FTy->getNumParams() == 1 ||
+ (FTy->isVarArg() && FTy->getNumParams() == 0)) &&
"Calling a function with bad signature");
- Operands.push_back(Use(Actual, this));
}
-void CallInst::init(Value *Func)
-{
- Operands.reserve(1);
- Operands.push_back(Use(Func, this));
-
- const FunctionType *MTy =
+void CallInst::init(Value *Func) {
+ NumOperands = 1;
+ Use *OL = OperandList = new Use[1];
+ OL[0].init(Func, this);
+
+ const FunctionType *MTy =
cast<FunctionType>(cast<PointerType>(Func->getType())->getElementType());
assert(MTy->getNumParams() == 0 && "Calling a function with bad signature");
}
-CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
- const std::string &Name, Instruction *InsertBefore)
+CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
+ const std::string &Name, Instruction *InsertBefore)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertBefore) {
+ Instruction::Call, 0, 0, Name, InsertBefore) {
init(Func, Params);
}
-CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
- const std::string &Name, BasicBlock *InsertAtEnd)
+CallInst::CallInst(Value *Func, const std::vector<Value*> &Params,
+ const std::string &Name, BasicBlock *InsertAtEnd)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertAtEnd) {
+ Instruction::Call, 0, 0, Name, InsertAtEnd) {
init(Func, Params);
}
const std::string &Name, Instruction *InsertBefore)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertBefore) {
+ Instruction::Call, 0, 0, Name, InsertBefore) {
init(Func, Actual1, Actual2);
}
const std::string &Name, BasicBlock *InsertAtEnd)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertAtEnd) {
+ Instruction::Call, 0, 0, Name, InsertAtEnd) {
init(Func, Actual1, Actual2);
}
Instruction *InsertBefore)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertBefore) {
+ Instruction::Call, 0, 0, Name, InsertBefore) {
init(Func, Actual);
}
BasicBlock *InsertAtEnd)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertAtEnd) {
+ Instruction::Call, 0, 0, Name, InsertAtEnd) {
init(Func, Actual);
}
Instruction *InsertBefore)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertBefore) {
+ Instruction::Call, 0, 0, Name, InsertBefore) {
init(Func);
}
BasicBlock *InsertAtEnd)
: Instruction(cast<FunctionType>(cast<PointerType>(Func->getType())
->getElementType())->getReturnType(),
- Instruction::Call, Name, InsertAtEnd) {
+ Instruction::Call, 0, 0, Name, InsertAtEnd) {
init(Func);
}
-CallInst::CallInst(const CallInst &CI)
- : Instruction(CI.getType(), Instruction::Call) {
- Operands.reserve(CI.Operands.size());
- for (unsigned i = 0; i < CI.Operands.size(); ++i)
- Operands.push_back(Use(CI.Operands[i], this));
-}
-
-const Function *CallInst::getCalledFunction() const {
- if (const Function *F = dyn_cast<Function>(Operands[0]))
- return F;
- return 0;
-}
-Function *CallInst::getCalledFunction() {
- if (Function *F = dyn_cast<Function>(Operands[0]))
- return F;
- return 0;
+CallInst::CallInst(const CallInst &CI)
+ : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()],
+ CI.getNumOperands()) {
+ SubclassData = CI.SubclassData;
+ Use *OL = OperandList;
+ Use *InOL = CI.OperandList;
+ for (unsigned i = 0, e = CI.getNumOperands(); i != e; ++i)
+ OL[i].init(InOL[i], this);
}
// InvokeInst Implementation
//===----------------------------------------------------------------------===//
+InvokeInst::~InvokeInst() {
+ delete [] OperandList;
+}
+
void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException,
- const std::vector<Value*> &Params)
-{
- Operands.reserve(3+Params.size());
- Operands.push_back(Use(Fn, this));
- Operands.push_back(Use((Value*)IfNormal, this));
- Operands.push_back(Use((Value*)IfException, this));
- const FunctionType *MTy =
+ const std::vector<Value*> &Params) {
+ NumOperands = 3+Params.size();
+ Use *OL = OperandList = new Use[3+Params.size()];
+ OL[0].init(Fn, this);
+ OL[1].init(IfNormal, this);
+ OL[2].init(IfException, this);
+ const FunctionType *FTy =
cast<FunctionType>(cast<PointerType>(Fn->getType())->getElementType());
-
- assert((Params.size() == MTy->getNumParams()) ||
- (MTy->isVarArg() && Params.size() > MTy->getNumParams()) &&
+
+ assert((Params.size() == FTy->getNumParams()) ||
+ (FTy->isVarArg() && Params.size() > FTy->getNumParams()) &&
"Calling a function with bad signature");
-
- for (unsigned i = 0; i < Params.size(); i++)
- Operands.push_back(Use(Params[i], this));
+
+ for (unsigned i = 0, e = Params.size(); i != e; i++)
+ OL[i+3].init(Params[i], this);
}
InvokeInst::InvokeInst(Value *Fn, BasicBlock *IfNormal,
const std::string &Name, Instruction *InsertBefore)
: TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
->getElementType())->getReturnType(),
- Instruction::Invoke, Name, InsertBefore) {
+ Instruction::Invoke, 0, 0, Name, InsertBefore) {
init(Fn, IfNormal, IfException, Params);
}
const std::string &Name, BasicBlock *InsertAtEnd)
: TerminatorInst(cast<FunctionType>(cast<PointerType>(Fn->getType())
->getElementType())->getReturnType(),
- Instruction::Invoke, Name, InsertAtEnd) {
+ Instruction::Invoke, 0, 0, Name, InsertAtEnd) {
init(Fn, IfNormal, IfException, Params);
}
-InvokeInst::InvokeInst(const InvokeInst &CI)
- : TerminatorInst(CI.getType(), Instruction::Invoke) {
- Operands.reserve(CI.Operands.size());
- for (unsigned i = 0; i < CI.Operands.size(); ++i)
- Operands.push_back(Use(CI.Operands[i], this));
+InvokeInst::InvokeInst(const InvokeInst &II)
+ : TerminatorInst(II.getType(), Instruction::Invoke,
+ new Use[II.getNumOperands()], II.getNumOperands()) {
+ SubclassData = II.SubclassData;
+ Use *OL = OperandList, *InOL = II.OperandList;
+ for (unsigned i = 0, e = II.getNumOperands(); i != e; ++i)
+ OL[i].init(InOL[i], this);
}
-const Function *InvokeInst::getCalledFunction() const {
- if (const Function *F = dyn_cast<Function>(Operands[0]))
- return F;
- return 0;
+BasicBlock *InvokeInst::getSuccessorV(unsigned idx) const {
+ return getSuccessor(idx);
}
-Function *InvokeInst::getCalledFunction() {
- if (Function *F = dyn_cast<Function>(Operands[0]))
- return F;
- return 0;
+unsigned InvokeInst::getNumSuccessorsV() const {
+ return getNumSuccessors();
}
-
-// FIXME: Is this supposed to be here?
-Function *CallSite::getCalledFunction() const {
- Value *Callee = getCalledValue();
- if (Function *F = dyn_cast<Function>(Callee))
- return F;
- return 0;
+void InvokeInst::setSuccessorV(unsigned idx, BasicBlock *B) {
+ return setSuccessor(idx, B);
}
+
//===----------------------------------------------------------------------===//
// ReturnInst Implementation
//===----------------------------------------------------------------------===//
+void ReturnInst::init(Value *retVal) {
+ if (retVal && retVal->getType() != Type::VoidTy) {
+ assert(!isa<BasicBlock>(retVal) &&
+ "Cannot return basic block. Probably using the incorrect ctor");
+ NumOperands = 1;
+ RetVal.init(retVal, this);
+ }
+}
+
+unsigned ReturnInst::getNumSuccessorsV() const {
+ return getNumSuccessors();
+}
+
// Out-of-line ReturnInst method, put here so the C++ compiler can choose to
// emit the vtable for the class in this translation unit.
-void ReturnInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
+void ReturnInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
assert(0 && "ReturnInst has no successors!");
}
+BasicBlock *ReturnInst::getSuccessorV(unsigned idx) const {
+ assert(0 && "ReturnInst has no successors!");
+ abort();
+ return 0;
+}
+
+
//===----------------------------------------------------------------------===//
// UnwindInst Implementation
//===----------------------------------------------------------------------===//
-// Likewise for UnwindInst
-void UnwindInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
+unsigned UnwindInst::getNumSuccessorsV() const {
+ return getNumSuccessors();
+}
+
+void UnwindInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
+ assert(0 && "UnwindInst has no successors!");
+}
+
+BasicBlock *UnwindInst::getSuccessorV(unsigned idx) const {
assert(0 && "UnwindInst has no successors!");
+ abort();
+ return 0;
}
//===----------------------------------------------------------------------===//
// UnreachableInst Implementation
//===----------------------------------------------------------------------===//
-void UnreachableInst::setSuccessor(unsigned idx, BasicBlock *NewSucc) {
- assert(0 && "UnreachableInst has no successors!");
+unsigned UnreachableInst::getNumSuccessorsV() const {
+ return getNumSuccessors();
+}
+
+void UnreachableInst::setSuccessorV(unsigned idx, BasicBlock *NewSucc) {
+ assert(0 && "UnwindInst has no successors!");
+}
+
+BasicBlock *UnreachableInst::getSuccessorV(unsigned idx) const {
+ assert(0 && "UnwindInst has no successors!");
+ abort();
+ return 0;
}
//===----------------------------------------------------------------------===//
// BranchInst Implementation
//===----------------------------------------------------------------------===//
-void BranchInst::init(BasicBlock *IfTrue)
-{
- assert(IfTrue != 0 && "Branch destination may not be null!");
- Operands.reserve(1);
- Operands.push_back(Use(IfTrue, this));
+void BranchInst::AssertOK() {
+ if (isConditional())
+ assert(getCondition()->getType() == Type::BoolTy &&
+ "May only branch on boolean predicates!");
}
-void BranchInst::init(BasicBlock *IfTrue, BasicBlock *IfFalse, Value *Cond)
-{
- assert(IfTrue && IfFalse && Cond &&
- "Branch destinations and condition may not be null!");
- assert(Cond && Cond->getType() == Type::BoolTy &&
- "May only branch on boolean predicates!");
- Operands.reserve(3);
- Operands.push_back(Use(IfTrue, this));
- Operands.push_back(Use(IfFalse, this));
- Operands.push_back(Use(Cond, this));
-}
-
-BranchInst::BranchInst(const BranchInst &BI) : TerminatorInst(Instruction::Br) {
- Operands.reserve(BI.Operands.size());
- Operands.push_back(Use(BI.Operands[0], this));
- if (BI.Operands.size() != 1) {
- assert(BI.Operands.size() == 3 && "BR can have 1 or 3 operands!");
- Operands.push_back(Use(BI.Operands[1], this));
- Operands.push_back(Use(BI.Operands[2], this));
+BranchInst::BranchInst(const BranchInst &BI) :
+ TerminatorInst(Instruction::Br, Ops, BI.getNumOperands()) {
+ OperandList[0].init(BI.getOperand(0), this);
+ if (BI.getNumOperands() != 1) {
+ assert(BI.getNumOperands() == 3 && "BR can have 1 or 3 operands!");
+ OperandList[1].init(BI.getOperand(1), this);
+ OperandList[2].init(BI.getOperand(2), this);
}
}
+BasicBlock *BranchInst::getSuccessorV(unsigned idx) const {
+ return getSuccessor(idx);
+}
+unsigned BranchInst::getNumSuccessorsV() const {
+ return getNumSuccessors();
+}
+void BranchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
+ setSuccessor(idx, B);
+}
+
+
//===----------------------------------------------------------------------===//
// AllocationInst Implementation
//===----------------------------------------------------------------------===//
-void AllocationInst::init(const Type *Ty, Value *ArraySize, unsigned iTy) {
- assert(Ty != Type::VoidTy && "Cannot allocate void elements!");
- // ArraySize defaults to 1.
- if (!ArraySize) ArraySize = ConstantUInt::get(Type::UIntTy, 1);
-
- Operands.reserve(1);
- assert(ArraySize->getType() == Type::UIntTy &&
- "Malloc/Allocation array size != UIntTy!");
-
- Operands.push_back(Use(ArraySize, this));
+static Value *getAISize(Value *Amt) {
+ if (!Amt)
+ Amt = ConstantUInt::get(Type::UIntTy, 1);
+ else
+ assert(Amt->getType() == Type::UIntTy &&
+ "Malloc/Allocation array size != UIntTy!");
+ return Amt;
}
-AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
+AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
const std::string &Name,
Instruction *InsertBefore)
- : Instruction(PointerType::get(Ty), iTy, Name, InsertBefore) {
- init(Ty, ArraySize, iTy);
+ : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
+ Name, InsertBefore) {
+ assert(Ty != Type::VoidTy && "Cannot allocate void!");
}
-AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
+AllocationInst::AllocationInst(const Type *Ty, Value *ArraySize, unsigned iTy,
const std::string &Name,
BasicBlock *InsertAtEnd)
- : Instruction(PointerType::get(Ty), iTy, Name, InsertAtEnd) {
- init(Ty, ArraySize, iTy);
+ : UnaryInstruction(PointerType::get(Ty), iTy, getAISize(ArraySize),
+ Name, InsertAtEnd) {
+ assert(Ty != Type::VoidTy && "Cannot allocate void!");
}
bool AllocationInst::isArrayAllocation() const {
- return getOperand(0) != ConstantUInt::get(Type::UIntTy, 1);
+ if (ConstantUInt *CUI = dyn_cast<ConstantUInt>(getOperand(0)))
+ return CUI->getValue() != 1;
+ return true;
}
const Type *AllocationInst::getAllocatedType() const {
// FreeInst Implementation
//===----------------------------------------------------------------------===//
-void FreeInst::init(Value *Ptr)
-{
- assert(Ptr && isa<PointerType>(Ptr->getType()) && "Can't free nonpointer!");
- Operands.reserve(1);
- Operands.push_back(Use(Ptr, this));
+void FreeInst::AssertOK() {
+ assert(isa<PointerType>(getOperand(0)->getType()) &&
+ "Can not free something of nonpointer type!");
}
FreeInst::FreeInst(Value *Ptr, Instruction *InsertBefore)
- : Instruction(Type::VoidTy, Free, "", InsertBefore) {
- init(Ptr);
+ : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertBefore) {
+ AssertOK();
}
FreeInst::FreeInst(Value *Ptr, BasicBlock *InsertAtEnd)
- : Instruction(Type::VoidTy, Free, "", InsertAtEnd) {
- init(Ptr);
+ : UnaryInstruction(Type::VoidTy, Free, Ptr, "", InsertAtEnd) {
+ AssertOK();
}
// LoadInst Implementation
//===----------------------------------------------------------------------===//
-void LoadInst::init(Value *Ptr) {
- assert(Ptr && isa<PointerType>(Ptr->getType()) &&
+void LoadInst::AssertOK() {
+ assert(isa<PointerType>(getOperand(0)->getType()) &&
"Ptr must have pointer type.");
- Operands.reserve(1);
- Operands.push_back(Use(Ptr, this));
}
LoadInst::LoadInst(Value *Ptr, const std::string &Name, Instruction *InsertBef)
- : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
- Load, Name, InsertBef), Volatile(false) {
- init(Ptr);
+ : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+ Load, Ptr, Name, InsertBef) {
+ setVolatile(false);
+ AssertOK();
}
LoadInst::LoadInst(Value *Ptr, const std::string &Name, BasicBlock *InsertAE)
- : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
- Load, Name, InsertAE), Volatile(false) {
- init(Ptr);
+ : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+ Load, Ptr, Name, InsertAE) {
+ setVolatile(false);
+ AssertOK();
}
LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
Instruction *InsertBef)
- : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
- Load, Name, InsertBef), Volatile(isVolatile) {
- init(Ptr);
+ : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+ Load, Ptr, Name, InsertBef) {
+ setVolatile(isVolatile);
+ AssertOK();
}
LoadInst::LoadInst(Value *Ptr, const std::string &Name, bool isVolatile,
BasicBlock *InsertAE)
- : Instruction(cast<PointerType>(Ptr->getType())->getElementType(),
- Load, Name, InsertAE), Volatile(isVolatile) {
- init(Ptr);
+ : UnaryInstruction(cast<PointerType>(Ptr->getType())->getElementType(),
+ Load, Ptr, Name, InsertAE) {
+ setVolatile(isVolatile);
+ AssertOK();
}
// StoreInst Implementation
//===----------------------------------------------------------------------===//
-StoreInst::StoreInst(Value *Val, Value *Ptr, Instruction *InsertBefore)
- : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(false) {
- init(Val, Ptr);
+void StoreInst::AssertOK() {
+ assert(isa<PointerType>(getOperand(1)->getType()) &&
+ "Ptr must have pointer type!");
+ assert(getOperand(0)->getType() ==
+ cast<PointerType>(getOperand(1)->getType())->getElementType()
+ && "Ptr must be a pointer to Val type!");
}
-StoreInst::StoreInst(Value *Val, Value *Ptr, BasicBlock *InsertAtEnd)
- : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(false) {
- init(Val, Ptr);
-}
-StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
- Instruction *InsertBefore)
- : Instruction(Type::VoidTy, Store, "", InsertBefore), Volatile(isVolatile) {
- init(Val, Ptr);
+StoreInst::StoreInst(Value *val, Value *addr, Instruction *InsertBefore)
+ : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
+ Ops[0].init(val, this);
+ Ops[1].init(addr, this);
+ setVolatile(false);
+ AssertOK();
}
-StoreInst::StoreInst(Value *Val, Value *Ptr, bool isVolatile,
- BasicBlock *InsertAtEnd)
- : Instruction(Type::VoidTy, Store, "", InsertAtEnd), Volatile(isVolatile) {
- init(Val, Ptr);
+StoreInst::StoreInst(Value *val, Value *addr, BasicBlock *InsertAtEnd)
+ : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
+ Ops[0].init(val, this);
+ Ops[1].init(addr, this);
+ setVolatile(false);
+ AssertOK();
}
-void StoreInst::init(Value *Val, Value *Ptr) {
- assert(isa<PointerType>(Ptr->getType()) && "Ptr must have pointer type!");
- assert(Val->getType() == cast<PointerType>(Ptr->getType())->getElementType()
- && "Ptr must be a pointer to Val type!");
+StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
+ Instruction *InsertBefore)
+ : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertBefore) {
+ Ops[0].init(val, this);
+ Ops[1].init(addr, this);
+ setVolatile(isVolatile);
+ AssertOK();
+}
- Operands.reserve(2);
- Operands.push_back(Use(Val, this));
- Operands.push_back(Use(Ptr, this));
+StoreInst::StoreInst(Value *val, Value *addr, bool isVolatile,
+ BasicBlock *InsertAtEnd)
+ : Instruction(Type::VoidTy, Store, Ops, 2, "", InsertAtEnd) {
+ Ops[0].init(val, this);
+ Ops[1].init(addr, this);
+ setVolatile(isVolatile);
+ AssertOK();
}
//===----------------------------------------------------------------------===//
return Ty;
}
-void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx)
-{
- Operands.reserve(1+Idx.size());
- Operands.push_back(Use(Ptr, this));
+void GetElementPtrInst::init(Value *Ptr, const std::vector<Value*> &Idx) {
+ NumOperands = 1+Idx.size();
+ Use *OL = OperandList = new Use[NumOperands];
+ OL[0].init(Ptr, this);
- for (unsigned i = 0, E = Idx.size(); i != E; ++i)
- Operands.push_back(Use(Idx[i], this));
+ for (unsigned i = 0, e = Idx.size(); i != e; ++i)
+ OL[i+1].init(Idx[i], this);
}
void GetElementPtrInst::init(Value *Ptr, Value *Idx0, Value *Idx1) {
- Operands.reserve(3);
- Operands.push_back(Use(Ptr, this));
- Operands.push_back(Use(Idx0, this));
- Operands.push_back(Use(Idx1, this));
+ NumOperands = 3;
+ Use *OL = OperandList = new Use[3];
+ OL[0].init(Ptr, this);
+ OL[1].init(Idx0, this);
+ OL[2].init(Idx1, this);
+}
+
+void GetElementPtrInst::init(Value *Ptr, Value *Idx) {
+ NumOperands = 2;
+ Use *OL = OperandList = new Use[2];
+ OL[0].init(Ptr, this);
+ OL[1].init(Idx, this);
}
GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
- const std::string &Name, Instruction *InBe)
+ const std::string &Name, Instruction *InBe)
: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Idx, true))),
- GetElementPtr, Name, InBe) {
+ GetElementPtr, 0, 0, Name, InBe) {
init(Ptr, Idx);
}
GetElementPtrInst::GetElementPtrInst(Value *Ptr, const std::vector<Value*> &Idx,
- const std::string &Name, BasicBlock *IAE)
+ const std::string &Name, BasicBlock *IAE)
: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Idx, true))),
- GetElementPtr, Name, IAE) {
+ GetElementPtr, 0, 0, Name, IAE) {
+ init(Ptr, Idx);
+}
+
+GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
+ const std::string &Name, Instruction *InBe)
+ : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
+ GetElementPtr, 0, 0, Name, InBe) {
+ init(Ptr, Idx);
+}
+
+GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx,
+ const std::string &Name, BasicBlock *IAE)
+ : Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),Idx))),
+ GetElementPtr, 0, 0, Name, IAE) {
init(Ptr, Idx);
}
const std::string &Name, Instruction *InBe)
: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Idx0, Idx1, true))),
- GetElementPtr, Name, InBe) {
+ GetElementPtr, 0, 0, Name, InBe) {
init(Ptr, Idx0, Idx1);
}
GetElementPtrInst::GetElementPtrInst(Value *Ptr, Value *Idx0, Value *Idx1,
- const std::string &Name, BasicBlock *IAE)
+ const std::string &Name, BasicBlock *IAE)
: Instruction(PointerType::get(checkType(getIndexedType(Ptr->getType(),
Idx0, Idx1, true))),
- GetElementPtr, Name, IAE) {
+ GetElementPtr, 0, 0, Name, IAE) {
init(Ptr, Idx0, Idx1);
}
+GetElementPtrInst::~GetElementPtrInst() {
+ delete[] OperandList;
+}
+
// getIndexedType - Returns the type of the element that would be loaded with
// a load instruction with the specified parameters.
//
-// A null type is returned if the indices are invalid for the specified
+// A null type is returned if the indices are invalid for the specified
// pointer type.
//
-const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
+const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
const std::vector<Value*> &Idx,
bool AllowCompositeLeaf) {
if (!isa<PointerType>(Ptr)) return 0; // Type isn't a pointer type!
return cast<PointerType>(Ptr)->getElementType();
else
return 0;
-
+
unsigned CurIdx = 0;
while (const CompositeType *CT = dyn_cast<CompositeType>(Ptr)) {
if (Idx.size() == CurIdx) {
return CurIdx == Idx.size() ? Ptr : 0;
}
-const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
+const Type* GetElementPtrInst::getIndexedType(const Type *Ptr,
Value *Idx0, Value *Idx1,
bool AllowCompositeLeaf) {
const PointerType *PTy = dyn_cast<PointerType>(Ptr);
return 0;
}
+const Type* GetElementPtrInst::getIndexedType(const Type *Ptr, Value *Idx) {
+ const PointerType *PTy = dyn_cast<PointerType>(Ptr);
+ if (!PTy) return 0; // Type isn't a pointer type!
+
+ // Check the pointer index.
+ if (!PTy->indexValid(Idx)) return 0;
+
+ return PTy->getElementType();
+}
+
//===----------------------------------------------------------------------===//
// BinaryOperator Class
//===----------------------------------------------------------------------===//
-void BinaryOperator::init(BinaryOps iType, Value *S1, Value *S2)
+void BinaryOperator::init(BinaryOps iType)
{
- Operands.reserve(2);
- Operands.push_back(Use(S1, this));
- Operands.push_back(Use(S2, this));
- assert(S1 && S2 && S1->getType() == S2->getType());
-
+ Value *LHS = getOperand(0), *RHS = getOperand(1);
+ assert(LHS->getType() == RHS->getType() &&
+ "Binary operator operand types must match!");
#ifndef NDEBUG
switch (iType) {
case Add: case Sub:
case Mul: case Div:
case Rem:
- assert(getType() == S1->getType() &&
+ assert(getType() == LHS->getType() &&
"Arithmetic operation should return same type as operands!");
- assert((getType()->isInteger() ||
- getType()->isFloatingPoint() ||
- isa<PackedType>(getType()) ) &&
+ assert((getType()->isInteger() ||
+ getType()->isFloatingPoint() ||
+ isa<PackedType>(getType()) ) &&
"Tried to create an arithmetic operation on a non-arithmetic type!");
break;
case And: case Or:
case Xor:
- assert(getType() == S1->getType() &&
+ assert(getType() == LHS->getType() &&
"Logical operation should return same type as operands!");
assert(getType()->isIntegral() &&
- "Tried to create an logical operation on a non-integral type!");
+ "Tried to create a logical operation on a non-integral type!");
break;
case SetLT: case SetGT: case SetLE:
case SetGE: case SetEQ: case SetNE:
}
BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
- const std::string &Name,
+ const std::string &Name,
Instruction *InsertBefore) {
assert(S1->getType() == S2->getType() &&
"Cannot create binary operator with two operands of differing type!");
}
BinaryOperator *BinaryOperator::create(BinaryOps Op, Value *S1, Value *S2,
- const std::string &Name,
+ const std::string &Name,
BasicBlock *InsertAtEnd) {
BinaryOperator *Res = create(Op, S1, S2, Name);
InsertAtEnd->getInstList().push_back(Res);
return false;
}
-Value *BinaryOperator::getNegArgument(BinaryOperator *Bop) {
- assert(isNeg(Bop) && "getNegArgument from non-'neg' instruction!");
- return Bop->getOperand(1);
+Value *BinaryOperator::getNegArgument(Value *BinOp) {
+ assert(isNeg(BinOp) && "getNegArgument from non-'neg' instruction!");
+ return cast<BinaryOperator>(BinOp)->getOperand(1);
}
-const Value *BinaryOperator::getNegArgument(const BinaryOperator *Bop) {
- return getNegArgument((BinaryOperator*)Bop);
+const Value *BinaryOperator::getNegArgument(const Value *BinOp) {
+ return getNegArgument(const_cast<Value*>(BinOp));
}
-Value *BinaryOperator::getNotArgument(BinaryOperator *Bop) {
- assert(isNot(Bop) && "getNotArgument on non-'not' instruction!");
- Value *Op0 = Bop->getOperand(0);
- Value *Op1 = Bop->getOperand(1);
+Value *BinaryOperator::getNotArgument(Value *BinOp) {
+ assert(isNot(BinOp) && "getNotArgument on non-'not' instruction!");
+ BinaryOperator *BO = cast<BinaryOperator>(BinOp);
+ Value *Op0 = BO->getOperand(0);
+ Value *Op1 = BO->getOperand(1);
if (isConstantAllOnes(Op0)) return Op1;
assert(isConstantAllOnes(Op1));
return Op0;
}
-const Value *BinaryOperator::getNotArgument(const BinaryOperator *Bop) {
- return getNotArgument((BinaryOperator*)Bop);
+const Value *BinaryOperator::getNotArgument(const Value *BinOp) {
+ return getNotArgument(const_cast<Value*>(BinOp));
}
else
return true; // Can't commute operands
- std::swap(Operands[0], Operands[1]);
+ std::swap(Ops[0], Ops[1]);
return false;
}
// SetCondInst Class
//===----------------------------------------------------------------------===//
-SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
+SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
const std::string &Name, Instruction *InsertBefore)
: BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertBefore) {
assert(getInverseCondition(Opcode));
}
-SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
+SetCondInst::SetCondInst(BinaryOps Opcode, Value *S1, Value *S2,
const std::string &Name, BasicBlock *InsertAtEnd)
: BinaryOperator(Opcode, S1, S2, Type::BoolTy, Name, InsertAtEnd) {
// SwitchInst Implementation
//===----------------------------------------------------------------------===//
-void SwitchInst::init(Value *Value, BasicBlock *Default)
-{
+void SwitchInst::init(Value *Value, BasicBlock *Default, unsigned NumCases) {
assert(Value && Default);
- Operands.push_back(Use(Value, this));
- Operands.push_back(Use(Default, this));
-}
+ ReservedSpace = 2+NumCases*2;
+ NumOperands = 2;
+ OperandList = new Use[ReservedSpace];
-SwitchInst::SwitchInst(const SwitchInst &SI)
- : TerminatorInst(Instruction::Switch) {
- Operands.reserve(SI.Operands.size());
+ OperandList[0].init(Value, this);
+ OperandList[1].init(Default, this);
+}
- for (unsigned i = 0, E = SI.Operands.size(); i != E; i+=2) {
- Operands.push_back(Use(SI.Operands[i], this));
- Operands.push_back(Use(SI.Operands[i+1], this));
+SwitchInst::SwitchInst(const SwitchInst &SI)
+ : TerminatorInst(Instruction::Switch, new Use[SI.getNumOperands()],
+ SI.getNumOperands()) {
+ Use *OL = OperandList, *InOL = SI.OperandList;
+ for (unsigned i = 0, E = SI.getNumOperands(); i != E; i+=2) {
+ OL[i].init(InOL[i], this);
+ OL[i+1].init(InOL[i+1], this);
}
}
+SwitchInst::~SwitchInst() {
+ delete [] OperandList;
+}
+
+
/// addCase - Add an entry to the switch instruction...
///
-void SwitchInst::addCase(Constant *OnVal, BasicBlock *Dest) {
- Operands.push_back(Use((Value*)OnVal, this));
- Operands.push_back(Use((Value*)Dest, this));
+void SwitchInst::addCase(ConstantInt *OnVal, BasicBlock *Dest) {
+ unsigned OpNo = NumOperands;
+ if (OpNo+2 > ReservedSpace)
+ resizeOperands(0); // Get more space!
+ // Initialize some new operands.
+ assert(OpNo+1 < ReservedSpace && "Growing didn't work!");
+ NumOperands = OpNo+2;
+ OperandList[OpNo].init(OnVal, this);
+ OperandList[OpNo+1].init(Dest, this);
}
/// removeCase - This method removes the specified successor from the switch
///
void SwitchInst::removeCase(unsigned idx) {
assert(idx != 0 && "Cannot remove the default case!");
- assert(idx*2 < Operands.size() && "Successor index out of range!!!");
- Operands.erase(Operands.begin()+idx*2, Operands.begin()+(idx+1)*2);
+ assert(idx*2 < getNumOperands() && "Successor index out of range!!!");
+
+ unsigned NumOps = getNumOperands();
+ Use *OL = OperandList;
+
+ // Move everything after this operand down.
+ //
+ // FIXME: we could just swap with the end of the list, then erase. However,
+ // client might not expect this to happen. The code as it is thrashes the
+ // use/def lists, which is kinda lame.
+ for (unsigned i = (idx+1)*2; i != NumOps; i += 2) {
+ OL[i-2] = OL[i];
+ OL[i-2+1] = OL[i+1];
+ }
+
+ // Nuke the last value.
+ OL[NumOps-2].set(0);
+ OL[NumOps-2+1].set(0);
+ NumOperands = NumOps-2;
+}
+
+/// resizeOperands - resize operands - This adjusts the length of the operands
+/// list according to the following behavior:
+/// 1. If NumOps == 0, grow the operand list in response to a push_back style
+/// of operation. This grows the number of ops by 1.5 times.
+/// 2. If NumOps > NumOperands, reserve space for NumOps operands.
+/// 3. If NumOps == NumOperands, trim the reserved space.
+///
+void SwitchInst::resizeOperands(unsigned NumOps) {
+ if (NumOps == 0) {
+ NumOps = getNumOperands()/2*6;
+ } else if (NumOps*2 > NumOperands) {
+ // No resize needed.
+ if (ReservedSpace >= NumOps) return;
+ } else if (NumOps == NumOperands) {
+ if (ReservedSpace == NumOps) return;
+ } else {
+ return;
+ }
+
+ ReservedSpace = NumOps;
+ Use *NewOps = new Use[NumOps];
+ Use *OldOps = OperandList;
+ for (unsigned i = 0, e = getNumOperands(); i != e; ++i) {
+ NewOps[i].init(OldOps[i], this);
+ OldOps[i].set(0);
+ }
+ delete [] OldOps;
+ OperandList = NewOps;
+}
+
+
+BasicBlock *SwitchInst::getSuccessorV(unsigned idx) const {
+ return getSuccessor(idx);
+}
+unsigned SwitchInst::getNumSuccessorsV() const {
+ return getNumSuccessors();
+}
+void SwitchInst::setSuccessorV(unsigned idx, BasicBlock *B) {
+ setSuccessor(idx, B);
}
}
BinaryOperator *BinaryOperator::clone() const {
- return create(getOpcode(), Operands[0], Operands[1]);
+ return create(getOpcode(), Ops[0], Ops[1]);
}
MallocInst *MallocInst::clone() const { return new MallocInst(*this); }
AllocaInst *AllocaInst::clone() const { return new AllocaInst(*this); }
-FreeInst *FreeInst::clone() const { return new FreeInst(Operands[0]); }
+FreeInst *FreeInst::clone() const { return new FreeInst(getOperand(0)); }
LoadInst *LoadInst::clone() const { return new LoadInst(*this); }
StoreInst *StoreInst::clone() const { return new StoreInst(*this); }
CastInst *CastInst::clone() const { return new CastInst(*this); }
CallInst *CallInst::clone() const { return new CallInst(*this); }
ShiftInst *ShiftInst::clone() const { return new ShiftInst(*this); }
SelectInst *SelectInst::clone() const { return new SelectInst(*this); }
-VANextInst *VANextInst::clone() const { return new VANextInst(*this); }
VAArgInst *VAArgInst::clone() const { return new VAArgInst(*this); }
PHINode *PHINode::clone() const { return new PHINode(*this); }
ReturnInst *ReturnInst::clone() const { return new ReturnInst(*this); }