diff options
Diffstat (limited to 'lib')
-rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.cpp | 51 | ||||
-rw-r--r-- | lib/Bitcode/Reader/BitcodeReader.h | 50 | ||||
-rw-r--r-- | lib/VMCore/ConstantFold.cpp | 16 | ||||
-rw-r--r-- | lib/VMCore/Constants.cpp | 182 | ||||
-rw-r--r-- | lib/VMCore/Globals.cpp | 18 | ||||
-rw-r--r-- | lib/VMCore/Instructions.cpp | 412 | ||||
-rw-r--r-- | lib/VMCore/Use.cpp | 131 |
7 files changed, 598 insertions, 262 deletions
diff --git a/lib/Bitcode/Reader/BitcodeReader.cpp b/lib/Bitcode/Reader/BitcodeReader.cpp index ec25f52b1b..78cd1de205 100644 --- a/lib/Bitcode/Reader/BitcodeReader.cpp +++ b/lib/Bitcode/Reader/BitcodeReader.cpp @@ -23,6 +23,7 @@ #include "llvm/ADT/SmallVector.h" #include "llvm/Support/MathExtras.h" #include "llvm/Support/MemoryBuffer.h" +#include "llvm/OperandTraits.h" using namespace llvm; void BitcodeReader::FreeState() { @@ -115,55 +116,81 @@ static int GetDecodedBinaryOpcode(unsigned Val, const Type *Ty) { } } - +namespace llvm { namespace { /// @brief A class for maintaining the slot number definition /// as a placeholder for the actual definition for forward constants defs. class ConstantPlaceHolder : public ConstantExpr { ConstantPlaceHolder(); // DO NOT IMPLEMENT void operator=(const ConstantPlaceHolder &); // DO NOT IMPLEMENT - Use Op; public: // allocate space for exactly one operand void *operator new(size_t s) { return User::operator new(s, 1); } explicit ConstantPlaceHolder(const Type *Ty) - : ConstantExpr(Ty, Instruction::UserOp1, &Op, 1), - Op(UndefValue::get(Type::Int32Ty), this) { + : ConstantExpr(Ty, Instruction::UserOp1, &Op<0>(), 1) { + Op<0>() = UndefValue::get(Type::Int32Ty); } + /// Provide fast operand accessors + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; } + + // FIXME: can we inherit this from ConstantExpr? +template <> +struct OperandTraits<ConstantPlaceHolder> : FixedNumOperandTraits<1> { +}; + +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ConstantPlaceHolder, Value) +} + +void BitcodeReaderValueList::resize(unsigned Desired) { + if (Desired > Capacity) { + // Since we expect many values to come from the bitcode file we better + // allocate the double amount, so that the array size grows exponentially + // at each reallocation. Also, add a small amount of 100 extra elements + // each time, to reallocate less frequently when the array is still small. + // + Capacity = Desired * 2 + 100; + Use *New = allocHungoffUses(Capacity); + Use *Old = OperandList; + unsigned Ops = getNumOperands(); + for (int i(Ops - 1); i >= 0; --i) + New[i] = Old[i].get(); + OperandList = New; + if (Old) Use::zap(Old, Old + Ops, true); + } +} + Constant *BitcodeReaderValueList::getConstantFwdRef(unsigned Idx, const Type *Ty) { if (Idx >= size()) { // Insert a bunch of null values. - Uses.resize(Idx+1); - OperandList = &Uses[0]; + resize(Idx + 1); NumOperands = Idx+1; } - if (Value *V = Uses[Idx]) { + if (Value *V = OperandList[Idx]) { assert(Ty == V->getType() && "Type mismatch in constant table!"); return cast<Constant>(V); } // Create and return a placeholder, which will later be RAUW'd. Constant *C = new ConstantPlaceHolder(Ty); - Uses[Idx].init(C, this); + OperandList[Idx].init(C, this); return C; } Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) { if (Idx >= size()) { // Insert a bunch of null values. - Uses.resize(Idx+1); - OperandList = &Uses[0]; + resize(Idx + 1); NumOperands = Idx+1; } - if (Value *V = Uses[Idx]) { + if (Value *V = OperandList[Idx]) { assert((Ty == 0 || Ty == V->getType()) && "Type mismatch in value table!"); return V; } @@ -173,7 +200,7 @@ Value *BitcodeReaderValueList::getValueFwdRef(unsigned Idx, const Type *Ty) { // Create and return a placeholder, which will later be RAUW'd. Value *V = new Argument(Ty); - Uses[Idx].init(V, this); + OperandList[Idx].init(V, this); return V; } diff --git a/lib/Bitcode/Reader/BitcodeReader.h b/lib/Bitcode/Reader/BitcodeReader.h index 86b00a5ef1..c35846f402 100644 --- a/lib/Bitcode/Reader/BitcodeReader.h +++ b/lib/Bitcode/Reader/BitcodeReader.h @@ -17,7 +17,7 @@ #include "llvm/ModuleProvider.h" #include "llvm/ParameterAttributes.h" #include "llvm/Type.h" -#include "llvm/User.h" +#include "llvm/OperandTraits.h" #include "llvm/Bitcode/BitstreamReader.h" #include "llvm/Bitcode/LLVMBitCodes.h" #include "llvm/ADT/DenseMap.h" @@ -26,32 +26,43 @@ namespace llvm { class MemoryBuffer; +//===----------------------------------------------------------------------===// +// BitcodeReaderValueList Class +//===----------------------------------------------------------------------===// + class BitcodeReaderValueList : public User { - std::vector<Use> Uses; + unsigned Capacity; public: - BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) {} - + BitcodeReaderValueList() : User(Type::VoidTy, Value::ArgumentVal, 0, 0) + , Capacity(0) {} + + /// Provide fast operand accessors + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); + // vector compatibility methods unsigned size() const { return getNumOperands(); } + void resize(unsigned); void push_back(Value *V) { - Uses.push_back(Use(V, this)); - OperandList = &Uses[0]; - ++NumOperands; + unsigned OldOps(NumOperands), NewOps(NumOperands + 1); + resize(NewOps); + NumOperands = NewOps; + OperandList[OldOps] = V; } void clear() { - std::vector<Use>().swap(Uses); + if (OperandList) dropHungoffUses(OperandList); + Capacity = 0; } Value *operator[](unsigned i) const { return getOperand(i); } - Value *back() const { return Uses.back(); } - void pop_back() { Uses.pop_back(); --NumOperands; } + Value *back() const { return getOperand(size() - 1); } + void pop_back() { setOperand(size() - 1, 0); --NumOperands; } bool empty() const { return NumOperands == 0; } void shrinkTo(unsigned N) { assert(N <= NumOperands && "Invalid shrinkTo request!"); - Uses.resize(N); - NumOperands = N; + while (NumOperands > N) + pop_back(); } virtual void print(std::ostream&) const {} @@ -73,11 +84,20 @@ public: private: void initVal(unsigned Idx, Value *V) { - assert(Uses[Idx] == 0 && "Cannot init an already init'd Use!"); - Uses[Idx].init(V, this); + if (Idx >= size()) { + // Insert a bunch of null values. + resize(Idx * 2 + 1); + } + assert(getOperand(Idx) == 0 && "Cannot init an already init'd Use!"); + OperandList[Idx].init(V, this); } }; - + +template <> +struct OperandTraits<BitcodeReaderValueList> : HungoffOperandTraits</*16 FIXME*/> { +}; + +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BitcodeReaderValueList, Value) class BitcodeReader : public ModuleProvider { MemoryBuffer *Buffer; diff --git a/lib/VMCore/ConstantFold.cpp b/lib/VMCore/ConstantFold.cpp index 9f31bcdb75..5138031da9 100644 --- a/lib/VMCore/ConstantFold.cpp +++ b/lib/VMCore/ConstantFold.cpp @@ -332,10 +332,10 @@ Constant *llvm::ConstantFoldExtractElementInstruction(const Constant *Val, if (const ConstantVector *CVal = dyn_cast<ConstantVector>(Val)) { if (const ConstantInt *CIdx = dyn_cast<ConstantInt>(Idx)) { - return const_cast<Constant*>(CVal->getOperand(CIdx->getZExtValue())); + return CVal->getOperand(CIdx->getZExtValue()); } else if (isa<UndefValue>(Idx)) { // ee({w,x,y,z}, undef) -> w (an arbitrary value). - return const_cast<Constant*>(CVal->getOperand(0)); + return CVal->getOperand(0); } } return 0; @@ -401,7 +401,7 @@ Constant *llvm::ConstantFoldInsertElementInstruction(const Constant *Val, /// return the specified element value. Otherwise return null. static Constant *GetVectorElement(const Constant *C, unsigned EltNo) { if (const ConstantVector *CV = dyn_cast<ConstantVector>(C)) - return const_cast<Constant*>(CV->getOperand(EltNo)); + return CV->getOperand(EltNo); const Type *EltTy = cast<VectorType>(C->getType())->getElementType(); if (isa<ConstantAggregateZero>(C)) @@ -1222,9 +1222,9 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, if (const ConstantVector *CP2 = dyn_cast<ConstantVector>(C2)) { if (pred == FCmpInst::FCMP_OEQ || pred == FCmpInst::FCMP_UEQ) { for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) { - Constant *C= ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, - const_cast<Constant*>(CP1->getOperand(i)), - const_cast<Constant*>(CP2->getOperand(i))); + Constant *C = ConstantExpr::getFCmp(FCmpInst::FCMP_OEQ, + CP1->getOperand(i), + CP2->getOperand(i)); if (ConstantInt *CB = dyn_cast<ConstantInt>(C)) return CB; } @@ -1233,8 +1233,8 @@ Constant *llvm::ConstantFoldCompareInstruction(unsigned short pred, } else if (pred == ICmpInst::ICMP_EQ) { for (unsigned i = 0, e = CP1->getNumOperands(); i != e; ++i) { Constant *C = ConstantExpr::getICmp(ICmpInst::ICMP_EQ, - const_cast<Constant*>(CP1->getOperand(i)), - const_cast<Constant*>(CP2->getOperand(i))); + CP1->getOperand(i), + CP2->getOperand(i)); if (ConstantInt *CB = dyn_cast<ConstantInt>(C)) return CB; } diff --git a/lib/VMCore/Constants.cpp b/lib/VMCore/Constants.cpp index 0b8fcc4325..d0c837335d 100644 --- a/lib/VMCore/Constants.cpp +++ b/lib/VMCore/Constants.cpp @@ -354,7 +354,9 @@ ConstantFP *ConstantFP::get(const Type *Ty, double V) { ConstantArray::ConstantArray(const ArrayType *T, const std::vector<Constant*> &V) - : Constant(T, ConstantArrayVal, new Use[V.size()], V.size()) { + : Constant(T, ConstantArrayVal, + OperandTraits<ConstantArray>::op_end(this) - V.size(), + V.size()) { assert(V.size() == T->getNumElements() && "Invalid initializer vector for constant array"); Use *OL = OperandList; @@ -369,13 +371,12 @@ ConstantArray::ConstantArray(const ArrayType *T, } } -ConstantArray::~ConstantArray() { - delete [] OperandList; -} ConstantStruct::ConstantStruct(const StructType *T, const std::vector<Constant*> &V) - : Constant(T, ConstantStructVal, new Use[V.size()], V.size()) { + : Constant(T, ConstantStructVal, + OperandTraits<ConstantStruct>::op_end(this) - V.size(), + V.size()) { assert(V.size() == T->getNumElements() && "Invalid initializer vector for constant structure"); Use *OL = OperandList; @@ -392,14 +393,12 @@ ConstantStruct::ConstantStruct(const StructType *T, } } -ConstantStruct::~ConstantStruct() { - delete [] OperandList; -} - ConstantVector::ConstantVector(const VectorType *T, const std::vector<Constant*> &V) - : Constant(T, ConstantVectorVal, new Use[V.size()], V.size()) { + : Constant(T, ConstantVectorVal, + OperandTraits<ConstantVector>::op_end(this) - V.size(), + V.size()) { Use *OL = OperandList; for (std::vector<Constant*>::const_iterator I = V.begin(), E = V.end(); I != E; ++I, ++OL) { @@ -412,10 +411,8 @@ ConstantVector::ConstantVector(const VectorType *T, } } -ConstantVector::~ConstantVector() { - delete [] OperandList; -} +namespace llvm { // We declare several classes private to this file, so use an anonymous // namespace namespace { @@ -424,49 +421,54 @@ namespace { /// behind the scenes to implement unary constant exprs. class VISIBILITY_HIDDEN UnaryConstantExpr : public ConstantExpr { void *operator new(size_t, unsigned); // DO NOT IMPLEMENT - Use Op; public: // allocate space for exactly one operand void *operator new(size_t s) { return User::operator new(s, 1); } UnaryConstantExpr(unsigned Opcode, Constant *C, const Type *Ty) - : ConstantExpr(Ty, Opcode, &Op, 1), Op(C, this) {} + : ConstantExpr(Ty, Opcode, &Op<0>(), 1) { + Op<0>() = C; + } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; /// BinaryConstantExpr - This class is private to Constants.cpp, and is used /// behind the scenes to implement binary constant exprs. class VISIBILITY_HIDDEN BinaryConstantExpr : public ConstantExpr { void *operator new(size_t, unsigned); // DO NOT IMPLEMENT - Use Ops[2]; public: // allocate space for exactly two operands void *operator new(size_t s) { return User::operator new(s, 2); } BinaryConstantExpr(unsigned Opcode, Constant *C1, Constant *C2) - : ConstantExpr(C1->getType(), Opcode, Ops, 2) { - Ops[0].init(C1, this); - Ops[1].init(C2, this); + : ConstantExpr(C1->getType(), Opcode, &Op<0>(), 2) { + Op<0>().init(C1, this); + Op<1>().init(C2, this); } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; /// SelectConstantExpr - This class is private to Constants.cpp, and is used /// behind the scenes to implement select constant exprs. class VISIBILITY_HIDDEN SelectConstantExpr : public ConstantExpr { void *operator new(size_t, unsigned); // DO NOT IMPLEMENT - Use Ops[3]; public: // allocate space for exactly three operands void *operator new(size_t s) { return User::operator new(s, 3); } SelectConstantExpr(Constant *C1, Constant *C2, Constant *C3) - : ConstantExpr(C2->getType(), Instruction::Select, Ops, 3) { - Ops[0].init(C1, this); - Ops[1].init(C2, this); - Ops[2].init(C3, this); + : ConstantExpr(C2->getType(), Instruction::Select, &Op<0>(), 3) { + Op<0>().init(C1, this); + Op<1>().init(C2, this); + Op<2>().init(C3, this); } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; /// ExtractElementConstantExpr - This class is private to @@ -474,7 +476,6 @@ public: /// extractelement constant exprs. class VISIBILITY_HIDDEN ExtractElementConstantExpr : public ConstantExpr { void *operator new(size_t, unsigned); // DO NOT IMPLEMENT - Use Ops[2]; public: // allocate space for exactly two operands void *operator new(size_t s) { @@ -482,10 +483,12 @@ public: } ExtractElementConstantExpr(Constant *C1, Constant *C2) : ConstantExpr(cast<VectorType>(C1->getType())->getElementType(), - Instruction::ExtractElement, Ops, 2) { - Ops[0].init(C1, this); - Ops[1].init(C2, this); + Instruction::ExtractElement, &Op<0>(), 2) { + Op<0>().init(C1, this); + Op<1>().init(C2, this); } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; /// InsertElementConstantExpr - This class is private to @@ -493,7 +496,6 @@ public: /// insertelement constant exprs. class VISIBILITY_HIDDEN InsertElementConstantExpr : public ConstantExpr { void *operator new(size_t, unsigned); // DO NOT IMPLEMENT - Use Ops[3]; public: // allocate space for exactly three operands void *operator new(size_t s) { @@ -501,11 +503,13 @@ public: } InsertElementConstantExpr(Constant *C1, Constant *C2, Constant *C3) : ConstantExpr(C1->getType(), Instruction::InsertElement, - Ops, 3) { - Ops[0].init(C1, this); - Ops[1].init(C2, this); - Ops[2].init(C3, this); + &Op<0>(), 3) { + Op<0>().init(C1, this); + Op<1>().init(C2, this); + Op<2>().init(C3, this); } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; /// ShuffleVectorConstantExpr - This class is private to @@ -513,7 +517,6 @@ public: /// shufflevector constant exprs. class VISIBILITY_HIDDEN ShuffleVectorConstantExpr : public ConstantExpr { void *operator new(size_t, unsigned); // DO NOT IMPLEMENT - Use Ops[3]; public: // allocate space for exactly three operands void *operator new(size_t s) { @@ -521,32 +524,27 @@ public: } ShuffleVectorConstantExpr(Constant *C1, Constant *C2, Constant *C3) : ConstantExpr(C1->getType(), Instruction::ShuffleVector, - Ops, 3) { - Ops[0].init(C1, this); - Ops[1].init(C2, this); - Ops[2].init(C3, this); + &Op<0>(), 3) { + Op<0>().init(C1, this); + Op<1>().init(C2, this); + Op<2>().init(C3, this); } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; /// GetElementPtrConstantExpr - This class is private to Constants.cpp, and is /// used behind the scenes to implement getelementpr constant exprs. class VISIBILITY_HIDDEN GetElementPtrConstantExpr : public ConstantExpr { GetElementPtrConstantExpr(Constant *C, const std::vector<Constant*> &IdxList, - const Type *DestTy) - : ConstantExpr(DestTy, Instruction::GetElementPtr, - new Use[IdxList.size()+1], IdxList.size()+1) { - OperandList[0].init(C, this); - for (unsigned i = 0, E = IdxList.size(); i != E; ++i) - OperandList[i+1].init(IdxList[i], this); - } + const Type *DestTy); public: static GetElementPtrConstantExpr *Create(Constant *C, const std::vector<Constant*> &IdxList, - const Type *DestTy) { - return new(IdxList.size() + 1/*FIXME*/) GetElementPtrConstantExpr(C, IdxList, DestTy); - } - ~GetElementPtrConstantExpr() { - delete [] OperandList; + const Type *DestTy) { + return new(IdxList.size() + 1) GetElementPtrConstantExpr(C, IdxList, DestTy); } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; // CompareConstantExpr - This class is private to Constants.cpp, and is used @@ -559,17 +557,77 @@ struct VISIBILITY_HIDDEN CompareConstantExpr : public ConstantExpr { return User::operator new(s, 2); } unsigned short predicate; - Use Ops[2]; CompareConstantExpr(Instruction::OtherOps opc, unsigned short pred, Constant* LHS, Constant* RHS) - : ConstantExpr(Type::Int1Ty, opc, Ops, 2), predicate(pred) { - OperandList[0].init(LHS, this); - OperandList[1].init(RHS, this); + : ConstantExpr(Type::Int1Ty, opc, &Op<0>(), 2), predicate(pred) { + Op<0>().init(LHS, this); + Op<1>().init(RHS, this); } + /// Transparently provide more efficient getOperand methods. + DECLARE_TRANSPARENT_OPERAND_ACCESSORS(Value); }; } // end anonymous namespace +template <> +struct OperandTraits<UnaryConstantExpr> : FixedNumOperandTraits<1> { +}; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(UnaryConstantExpr, Value) + +template <> +struct OperandTraits<BinaryConstantExpr> : FixedNumOperandTraits<2> { +}; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(BinaryConstantExpr, Value) + +template <> +struct OperandTraits<SelectConstantExpr> : FixedNumOperandTraits<3> { +}; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(SelectConstantExpr, Value) + +template <> +struct OperandTraits<ExtractElementConstantExpr> : FixedNumOperandTraits<2> { +}; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ExtractElementConstantExpr, Value) + +template <> +struct OperandTraits<InsertElementConstantExpr> : FixedNumOperandTraits<3> { +}; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(InsertElementConstantExpr, Value) + +template <> +struct OperandTraits<ShuffleVectorConstantExpr> : FixedNumOperandTraits<3> { +}; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(ShuffleVectorConstantExpr, Value) + + +template <> +struct OperandTraits<GetElementPtrConstantExpr> : VariadicOperandTraits<1> { +}; + +GetElementPtrConstantExpr::GetElementPtrConstantExpr + (Constant *C, + const std::vector<Constant*> &IdxList, + const Type *DestTy) + : ConstantExpr(DestTy, Instruction::GetElementPtr, + OperandTraits<GetElementPtrConstantExpr>::op_end(this) + - (IdxList.size()+1), + IdxList.size()+1) { + OperandList[0].init(C, this); + for (unsigned i = 0, E = IdxList.size(); i != E; ++i) + OperandList[i+1].init(IdxList[i], this); +} + +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(GetElementPtrConstantExpr, Value) + + +template <> +struct OperandTraits<CompareConstantExpr> : FixedNumOperandTraits<2> { +}; +DEFINE_TRANSPARENT_OPERAND_ACCESSORS(CompareConstantExpr, Value) + + +} // End llvm namespace + // Utility function for determining if a ConstantExpr is a CastOp or not. This // can't be inline because we don't want to #include Instruction.h into @@ -815,17 +873,29 @@ bool ConstantFP::isValueValidForType(const Type *Ty, const APFloat& Val) { //===----------------------------------------------------------------------===// // Factory Function Implementation + +// The number of operands for each ConstantCreator::create method is +// determined by the ConstantTraits template. // ConstantCreator - A class that is used to create constants by // ValueMap*. This class should be partially specialized if there is // something strange that needs to be done to interface to the ctor for the // constant. // namespace llvm { + template<class ValType> + struct ConstantTraits; + + template<typename T, typename Alloc> + struct VISIBILITY_HIDDEN ConstantTraits< std::vector<T, Alloc> > { + static unsigned uses(const std::vector<T, Alloc>& v) { + return v.size(); + } + }; + template<class ConstantClass, class TypeClass, class ValType> struct VISIBILITY_HIDDEN ConstantCreator { static ConstantClass *create(const TypeClass *Ty, const ValType &V) { - unsigned FIXME = 0; // = traits<ValType>::uses(V) - return new(FIXME) ConstantClass(Ty, V); + return new(ConstantTraits<ValType>::uses(V)) ConstantClass(Ty, V); } }; diff --git a/lib/VMCore/Globals.cpp b/lib/VMCore/Globals.cpp index e75b1867a9..1a328d80f6 100644 --- a/lib/VMCore/Globals.cpp +++ b/lib/VMCore/Globals.cpp @@ -89,14 +89,13 @@ GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, Module *ParentModule, bool ThreadLocal, unsigned AddressSpace) : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, - &Initializer, InitVal != 0, Link, Name), + OperandTraits<GlobalVariable>::op_begin(this), + InitVal != 0, Link, Name), isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { if (InitVal) { assert(InitVal->getType() == Ty && "Initializer should be the same type as the GlobalVariable!"); - Initializer.init(InitVal, this); - } else { - Initializer.init(0, this); + Op<0>().init(InitVal, this); } LeakDetector::addGarbageObject(this); @@ -110,14 +109,13 @@ GlobalVariable::GlobalVariable(const Type *Ty, bool constant, LinkageTypes Link, GlobalVariable *Before, bool ThreadLocal, unsigned AddressSpace) : GlobalValue(PointerType::get(Ty, AddressSpace), Value::GlobalVariableVal, - &Initializer, InitVal != 0, Link, Name), + OperandTraits<GlobalVariable>::op_begin(this), + InitVal != 0, Link, Name), isConstantGlobal(constant), isThreadLocalSymbol(ThreadLocal) { if (InitVal) { assert(InitVal->getType() == Ty && "Initializer should be the same type as the GlobalVariable!"); - Initializer.init(InitVal, this); - } else { - Initializer.init(0, this); + Op<0>().init(InitVal, this); } LeakDetector::addGarbageObject(this); @@ -169,12 +167,12 @@ void GlobalVariable::replaceUsesOfWithOnConstant(Value *From, Value *To, GlobalAlias::GlobalAlias(const Type *Ty, LinkageTypes Link, const std::string &Name, Constant* aliasee, Module *ParentModule) - : GlobalValue(Ty, Value::GlobalAliasVal, &Aliasee, 1, Link, Name) { + : GlobalValue(Ty, Value::GlobalAliasVal, &Op<0>(), 1, Link, Name) { LeakDetector::addGarbageObject(this); if (aliasee) assert(aliasee->getType() == Ty && "Alias and aliasee types should match!"); - Aliasee.init(aliasee, this); + Op<0>().init(aliasee, this); if (ParentModule) ParentModule->getAliasList().push_back(this); diff --git a/lib/VMCore/Instructions.cpp b/lib/VMCore/Instructions.cpp index 56bc8167c3..78e7b17a66 100644 --- a/lib/VMCore/Instructions.cpp +++ b/lib/VMCore/Instructions.cpp @@ -100,18 +100,21 @@ void CallSite::setDoesNotThrow(bool doesNotThrow) { TerminatorInst::~TerminatorInst() { } +//===----------------------------------------------------------------------===// +// UnaryInstruction Class +//===----------------------------------------------------------------------===// + // Out of line virtual method, so the vtable, etc has a home. UnaryInstruction::~UnaryInstruction() { } - //===----------------------------------------------------------------------===// // PHINode Class //===----------------------------------------------------------------------===// PHINode::PHINode(const PHINode &PN) : Instruction(PN.getType(), Instruction::PHI, - new Use[PN.getNumOperands()], PN.getNumOperands()), + allocHungoffUses(PN.getNumOperands()), PN.getNumOperands()), ReservedSpace(PN.getNumOperands()) { Use *OL = OperandList; for (unsigned i = 0, e = PN.getNumOperands(); i != e; i+=2) { @@ -121,7 +124,7 @@ PHINode::PHINode(const PHINode &PN) } PHINode::~PHINode() { - delete [] OperandList; + dropHungoffUses(OperandList); } // removeIncomingValue - Remove an incoming value. This is useful if a @@ -164,8 +167,9 @@ Value *PHINode::removeIncomingValue(unsigned Idx, bool DeletePHIIfEmpty) { /// 3. If NumOps == NumOperands, trim the reserved space. /// void PHINode::resizeOperands(unsigned NumOps) { + unsigned e = getNumOperands(); if (NumOps == 0) { - NumOps = (getNumOperands())*3/2; + NumOps = e*3/2; if (NumOps < 4) NumOps = 4; // 4 op PHI nodes are VERY common. } else if (NumOps*2 > NumOperands) { // No resize needed. @@ -177,14 +181,13 @@ void PHINode::resizeOperands(unsigned NumOps) { } ReservedSpace = NumOps; - Use *NewOps = new Use[NumOps]; Use *OldOps = OperandList; - for (unsigned i = 0, e = getNumOperands(); i != e; ++i) { + Use *NewOps = allocHungoffUses(NumOps); + for (unsigned i = 0; i != e; ++i) { NewOps[i].init(OldOps[i], this); - OldOps[i].set(0); } - delete [] OldOps; OperandList = NewOps; + if (OldOps) Use::zap(OldOps, OldOps + e, true); } /// hasConstantValue - If the specified PHI node always merges together the same @@ -241,12 +244,11 @@ Value *PHINode::hasConstantValue(bool AllowNonDominatingInstruction) const { //===----------------------------------------------------------------------===// CallInst::~CallInst() { - delete [] OperandList; } void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { - NumOperands = NumParams+1; - Use *OL = OperandList = new Use[NumParams+1]; + assert(NumOperands == NumParams+1 && "NumOperands not set up?"); + Use *OL = OperandList; OL[0].init(Func, this); const FunctionType *FTy = @@ -265,8 +267,8 @@ void CallInst::init(Value *Func, Value* const *Params, unsigned NumParams) { } void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { - NumOperands = 3; - Use *OL = OperandList = new Use[3]; + assert(NumOperands == 3 && "NumOperands not set up?"); + Use *OL = OperandList; OL[0].init(Func, this); OL[1].init(Actual1, this); OL[2].init(Actual2, this); @@ -287,8 +289,8 @@ void CallInst::init(Value *Func, Value *Actual1, Value *Actual2) { } void CallInst::init(Value *Func, Value *Actual) { - NumOperands = 2; - Use *OL = OperandList = new Use[2]; + assert(NumOperands == 2 && "NumOperands not set up?"); + Use *OL = OperandList; OL[0].init(Func, this); OL[1].init(Actual, this); @@ -305,8 +307,8 @@ void CallInst::init(Value *Func, Value *Actual) { } void CallInst::init(Value *Func) { - NumOperands = 1; - Use *OL = OperandList = new Use[1]; + assert(NumOperands == 1 && "NumOperands not set up?"); + Use *OL = OperandList; OL[0].init(Func, this); const FunctionType *FTy = @@ -320,7 +322,9 @@ CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, Instruction *InsertBefore) : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertBefore) { + Instruction::Call, + OperandTraits<CallInst>::op_end(this) - 2, + 2, InsertBefore) { init(Func, Actual); setName(Name); } @@ -329,7 +333,9 @@ CallInst::CallInst(Value *Func, Value* Actual, const std::string &Name, BasicBlock *InsertAtEnd) : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertAtEnd) { + Instruction::Call, + OperandTraits<CallInst>::op_end(this) - 2, + 2, InsertAtEnd) { init(Func, Actual); setName(Name); } @@ -337,7 +343,9 @@ CallInst::CallInst(Value *Func, const std::string &Name, Instruction *InsertBefore) : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertBefore) { + Instruction::Call, + OperandTraits<CallInst>::op_end(this) - 1, + 1, InsertBefore) { init(Func); setName(Name); } @@ -346,13 +354,16 @@ CallInst::CallInst(Value *Func, const std::string &Name, BasicBlock *InsertAtEnd) : Instruction(cast<FunctionType>(cast<PointerType>(Func->getType()) ->getElementType())->getReturnType(), - Instruction::Call, 0, 0, InsertAtEnd) { + Instruction::Call, + OperandTraits<CallInst>::op_end(this) - 1, + 1, InsertAtEnd) { init(Func); setName(Name); } CallInst::CallInst(const CallInst &CI) - : Instruction(CI.getType(), Instruction::Call, new Use[CI.getNumOperands()], + : Instruction(CI.getType(), Instruction::Call, + OperandTraits<CallInst>::op_end(this) - CI.getNumOperands(), CI.getNumOperands()) { setParamAttrs(CI.getParamAttrs()); SubclassData = CI.SubclassData; @@ -384,14 +395,10 @@ void CallInst::setDoesNotThrow(bool doesNotThrow) { // InvokeInst Implementation //===----------------------------------------------------------------------===// -InvokeInst::~InvokeInst() { - delete [] OperandList; -} - void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, Value* const *Args, unsigned NumArgs) { - NumOperands = 3+NumArgs; - Use *OL = OperandList = new Use[3+NumArgs]; + assert(NumOperands == 3+NumArgs && "NumOperands not set up?"); + Use *OL = OperandList; OL[0].init(Fn, this); OL[1].init(IfNormal, this); OL[2].init(IfException, this); @@ -414,7 +421,8 @@ void InvokeInst::init(Value *Fn, BasicBlock *IfNormal, BasicBlock *IfException, InvokeInst::InvokeInst(const InvokeInst &II) : TerminatorInst(II.getType(), Instruction::Invoke, - new Use[II.getNumOperands()], II.getNumOperands()) { + OperandTraits<InvokeInst>::op_end(this) - II.getNumOperands(), + II.getNumOperands()) { setParamAttrs(II.getParamAttrs()); SubclassData = II.SubclassData; Use *OL = OperandList, *InOL = II.OperandList; @@ -456,45 +464,51 @@ void InvokeInst::setDoesNotThrow(bool doesNotThrow) { ReturnInst::ReturnInst(const ReturnInst &RI) : TerminatorInst(Type::VoidTy, Instruction::Ret, - &RetVal, RI.getNumOperands()) { + OperandTraits<ReturnInst>::op_end(this) - RI.getNumOperands(), + RI.getNumOperands()) { unsigned N = RI.getNumOperands(); - if (N == 1) - RetVal.init(RI.RetVal, this); + if (N == 1) + Op<0>().init(RI.Op<0>(), this); else if (N) { - Use *OL = OperandList = new Use[N]; + Use *OL = OperandList; for (unsigned i = 0; i < N; ++i) OL[i].init(RI.getOperand(i), this); } } ReturnInst::ReturnInst(Value *retVal, Instruction *InsertBefore) - : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertBefore) { + : TerminatorInst(Type::VoidTy, Instruction::Ret, + OperandTraits<ReturnInst>::op_end(this) - (retVal != 0), + retVal != 0, InsertBefore) { if (retVal) init(&retVal, 1); } ReturnInst::ReturnInst(Value *retVal, BasicBlock *InsertAtEnd) - : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { + : TerminatorInst(Type::VoidTy, Instruction::Ret, + OperandTraits<ReturnInst>::op_end(this) - (retVal != 0), + retVal != 0, InsertAtEnd) { if (retVal) init(&retVal, 1); } ReturnInst::ReturnInst(BasicBlock *InsertAtEnd) - : TerminatorInst(Type::VoidTy, Instruction::Ret, &RetVal, 0, InsertAtEnd) { + : TerminatorInst(Type::VoidTy, Instruction::Ret, + OperandTraits<ReturnInst>::op_end(this), + 0, InsertAtEnd) { } ReturnInst::ReturnInst(Value * con |