From 7d65a311b1473a60a031a842a4ee5cbb0ad1d5e9 Mon Sep 17 00:00:00 2001 From: kai Date: Sun, 17 Mar 2013 22:05:04 +0100 Subject: [PATCH] More changes to std::vector usage. Replace with std::vector with static array, llvm::SmallVector or add code to reserve space. --- gen/irstate.cpp | 19 ++++--------------- gen/llvmhelpers.cpp | 30 ++++++++++++++++-------------- gen/module.cpp | 9 +++++---- gen/rttibuilder.cpp | 4 +++- gen/statements.cpp | 27 +++++++++++---------------- gen/structs.cpp | 1 + gen/tocall.cpp | 11 +++++++---- gen/todebug.cpp | 22 +++++++++------------- gen/toir.cpp | 23 ++++++++++++++--------- 9 files changed, 70 insertions(+), 76 deletions(-) diff --git a/gen/irstate.cpp b/gen/irstate.cpp index b81c9e1b..442f1292 100644 --- a/gen/irstate.cpp +++ b/gen/irstate.cpp @@ -143,39 +143,28 @@ LLCallSite IRState::CreateCallOrInvoke(LLValue* Callee, const char* Name) LLCallSite IRState::CreateCallOrInvoke(LLValue* Callee, LLValue* Arg1, const char* Name) { - LLSmallVector args; - args.push_back(Arg1); + LLValue* args[] = { Arg1 }; return CreateCallOrInvoke(Callee, args, Name); } LLCallSite IRState::CreateCallOrInvoke2(LLValue* Callee, LLValue* Arg1, LLValue* Arg2, const char* Name) { - LLSmallVector args; - args.push_back(Arg1); - args.push_back(Arg2); + LLValue* args[] = { Arg1, Arg2 }; return CreateCallOrInvoke(Callee, args, Name); } LLCallSite IRState::CreateCallOrInvoke3(LLValue* Callee, LLValue* Arg1, LLValue* Arg2, LLValue* Arg3, const char* Name) { - LLSmallVector args; - args.push_back(Arg1); - args.push_back(Arg2); - args.push_back(Arg3); + LLValue* args[] = { Arg1, Arg2, Arg3 }; return CreateCallOrInvoke(Callee, args, Name); } LLCallSite IRState::CreateCallOrInvoke4(LLValue* Callee, LLValue* Arg1, LLValue* Arg2, LLValue* Arg3, LLValue* Arg4, const char* Name) { - LLSmallVector args; - args.push_back(Arg1); - args.push_back(Arg2); - args.push_back(Arg3); - args.push_back(Arg4); + LLValue* args[] = { Arg1, Arg2, Arg3, Arg4 }; return CreateCallOrInvoke(Callee, args, Name); } - ////////////////////////////////////////////////////////////////////////////////////////// IRBuilder<>* IRBuilderHelper::operator->() diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index 8ad26c94..7d410fb4 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -59,8 +59,7 @@ void DtoDeleteMemory(LLValue* ptr) // get runtime function llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delmemory"); // build args - LLSmallVector arg; - arg.push_back(DtoBitCast(ptr, getVoidPtrType(), ".tmp")); + LLValue* arg[] = { DtoBitCast(ptr, getVoidPtrType(), ".tmp") }; // call gIR->CreateCallOrInvoke(fn, arg); } @@ -69,13 +68,14 @@ void DtoDeleteClass(LLValue* inst) { // get runtime function llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delclass"); - // build args - LLSmallVector arg; // druntime wants a pointer to object LLValue *ptr = DtoRawAlloca(inst->getType(), 0, "objectPtr"); DtoStore(inst, ptr); inst = ptr; - arg.push_back(DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")); + // build args + LLValue* arg[] = { + DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp") + }; // call gIR->CreateCallOrInvoke(fn, arg); } @@ -85,8 +85,9 @@ void DtoDeleteInterface(LLValue* inst) // get runtime function llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delinterface"); // build args - LLSmallVector arg; - arg.push_back(DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp")); + LLValue* arg[] = { + DtoBitCast(inst, fn->getFunctionType()->getParamType(0), ".tmp") + }; // call gIR->CreateCallOrInvoke(fn, arg); } @@ -97,9 +98,10 @@ void DtoDeleteArray(DValue* arr) llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_delarray_t"); // build args - LLSmallVector arg; - arg.push_back(DtoBitCast(arr->getLVal(), fn->getFunctionType()->getParamType(0))); - arg.push_back(DtoBitCast(DtoTypeInfoOf(arr->type->nextOf()), fn->getFunctionType()->getParamType(1))); + LLValue* arg[] = { + DtoBitCast(arr->getLVal(), fn->getFunctionType()->getParamType(0)), + DtoBitCast(DtoTypeInfoOf(arr->type->nextOf()), fn->getFunctionType()->getParamType(1)) + }; // call gIR->CreateCallOrInvoke(fn, arg); @@ -155,12 +157,13 @@ LLValue* DtoGcMalloc(LLType* lltype, const char* name) void DtoAssert(Module* M, Loc loc, DValue* msg) { - std::vector args; - // func const char* fname = msg ? "_d_assert_msg" : "_d_assert"; llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, fname); + // Arguments + llvm::SmallVector args; + // msg param if (msg) { @@ -182,8 +185,7 @@ void DtoAssert(Module* M, Loc loc, DValue* msg) } // line param - LLConstant* c = DtoConstUint(loc.linnum); - args.push_back(c); + args.push_back(DtoConstUint(loc.linnum)); // call gIR->CreateCallOrInvoke(fn, args); diff --git a/gen/module.cpp b/gen/module.cpp index 6788b448..fbbc4204 100644 --- a/gen/module.cpp +++ b/gen/module.cpp @@ -172,10 +172,11 @@ static LLFunction* build_module_reference_and_ctor(LLConstant* moduleinfo) // provide the default initializer LLStructType* modulerefTy = DtoModuleReferenceType(); - std::vector mrefvalues; - mrefvalues.push_back(LLConstant::getNullValue(modulerefTy->getContainedType(0))); - mrefvalues.push_back(llvm::ConstantExpr::getBitCast(moduleinfo, modulerefTy->getContainedType(1))); - LLConstant* thismrefinit = LLConstantStruct::get(modulerefTy, mrefvalues); + LLConstant* mrefvalues[] = { + LLConstant::getNullValue(modulerefTy->getContainedType(0)), + llvm::ConstantExpr::getBitCast(moduleinfo, modulerefTy->getContainedType(1)) + }; + LLConstant* thismrefinit = LLConstantStruct::get(modulerefTy, llvm::ArrayRef(mrefvalues)); // create the ModuleReference node for this module std::string thismrefname = "_D"; diff --git a/gen/rttibuilder.cpp b/gen/rttibuilder.cpp index 7e327645..cd12c7cb 100644 --- a/gen/rttibuilder.cpp +++ b/gen/rttibuilder.cpp @@ -165,8 +165,10 @@ void RTTIBuilder::finalize(LLType* type, LLValue* value) // set struct body if (st->isOpaque()) { + const int n = inits.size(); std::vector types; - for (int i = 0, n = inits.size(); i < n; ++i) + types.reserve(n); + for (int i = 0; i < n; ++i) types.push_back(inits[i]->getType()); st->setBody(types); } diff --git a/gen/statements.cpp b/gen/statements.cpp index f6f61355..96f7620e 100644 --- a/gen/statements.cpp +++ b/gen/statements.cpp @@ -938,7 +938,7 @@ void SwitchStatement::toIR(IRState* p) // first sort it caseArray.sort(); // iterate and add indices to cases - std::vector inits(caseArray.dim); + std::vector inits(caseArray.dim, 0); for (size_t i=0; i(caseArray.data[i]); @@ -956,14 +956,10 @@ void SwitchStatement::toIR(IRState* p) LLConstant* arrPtr = llvm::ConstantExpr::getBitCast(arr, elemPtrTy); // build the static table - std::vector types; - types.push_back(DtoSize_t()); - types.push_back(elemPtrTy); - LLStructType* sTy = llvm::StructType::get(gIR->context(), types); - std::vector sinits; - sinits.push_back(DtoConstSize_t(inits.size())); - sinits.push_back(arrPtr); - switchTable = llvm::ConstantStruct::get(sTy, sinits); + LLType* types[] = { DtoSize_t(), elemPtrTy }; + LLStructType* sTy = llvm::StructType::get(gIR->context(), types, false); + LLConstant* sinits[] = { DtoConstSize_t(inits.size()), arrPtr }; + switchTable = llvm::ConstantStruct::get(sTy, llvm::ArrayRef(sinits)); } // condition var @@ -1609,16 +1605,15 @@ void SwitchErrorStatement::toIR(IRState* p) llvm::Function* fn = LLVM_D_GetRuntimeFunction(gIR->module, "_d_switch_error"); - std::vector args; - - // module param LLValue *moduleInfoSymbol = gIR->func()->decl->getModule()->moduleInfoSymbol(); LLType *moduleInfoType = DtoType(Module::moduleinfo->type); - args.push_back(DtoBitCast(moduleInfoSymbol, getPtrToType(moduleInfoType))); - // line param - LLConstant* c = DtoConstUint(loc.linnum); - args.push_back(c); + LLValue* args[] = { + // module param + DtoBitCast(moduleInfoSymbol, getPtrToType(moduleInfoType)), + // line param + DtoConstUint(loc.linnum) + }; // call LLCallSite call = gIR->CreateCallOrInvoke(fn, args); diff --git a/gen/structs.cpp b/gen/structs.cpp index efa17310..3835022f 100644 --- a/gen/structs.cpp +++ b/gen/structs.cpp @@ -328,6 +328,7 @@ LLType* DtoUnpaddedStructType(Type* dty) { Array& fields = sty->sym->fields; std::vector types; + types.reserve(fields.dim); for (unsigned i = 0; i < fields.dim; i++) { VarDeclaration* vd = static_cast(fields.data[i]); diff --git a/gen/tocall.cpp b/gen/tocall.cpp index c6e2ea82..9ade207f 100644 --- a/gen/tocall.cpp +++ b/gen/tocall.cpp @@ -259,6 +259,7 @@ void DtoBuildDVarArgList(std::vector& args, Logger::cout() << "_arguments storage: " << *typeinfomem << '\n'; std::vector vtypeinfos; + vtypeinfos.reserve(n_arguments); for (size_t i=begin; i(arguments->data[i]); @@ -270,11 +271,12 @@ void DtoBuildDVarArgList(std::vector& args, typeinfomem->setInitializer(tiinits); // put data in d-array - std::vector pinits; - pinits.push_back(DtoConstSize_t(vtype->getNumElements())); - pinits.push_back(llvm::ConstantExpr::getBitCast(typeinfomem, getPtrToType(typeinfotype))); + LLConstant* pinits[] = { + DtoConstSize_t(vtype->getNumElements()), + llvm::ConstantExpr::getBitCast(typeinfomem, getPtrToType(typeinfotype)) + }; LLType* tiarrty = DtoType(Type::typeinfo->type->arrayOf()); - tiinits = LLConstantStruct::get(isaStruct(tiarrty), pinits); + tiinits = LLConstantStruct::get(isaStruct(tiarrty), llvm::ArrayRef(pinits)); LLValue* typeinfoarrayparam = new llvm::GlobalVariable(*gIR->module, tiarrty, true, llvm::GlobalValue::InternalLinkage, tiinits, "._arguments.array"); @@ -507,6 +509,7 @@ DValue* DtoCallFunction(Loc& loc, Type* resulttype, DValue* fnval, Expressions* size_t n = Parameter::dim(tf->parameters); std::vector argvals; + argvals.reserve(n); if (dfnval && dfnval->func->isArrayOp) { // slightly different approach for array operators for (int i=n-1; i>=0; --i) { diff --git a/gen/todebug.cpp b/gen/todebug.cpp index 9898ec7e..f0d6a33c 100644 --- a/gen/todebug.cpp +++ b/gen/todebug.cpp @@ -216,16 +216,6 @@ static llvm::DIType dwarfCompositeType(Type* type) LLType* T = DtoType(type); Type* t = type->toBasetype(); - // defaults - llvm::StringRef name; - unsigned linnum = 0; - llvm::DIFile file; - - // elements - std::vector elems; - - llvm::DIType derivedFrom; - assert((t->ty == Tstruct || t->ty == Tclass) && "unsupported type for dwarfCompositeType"); AggregateDeclaration* sd; @@ -254,9 +244,15 @@ static llvm::DIType dwarfCompositeType(Type* type) if (static_cast(ir->diCompositeType) != 0) return ir->diCompositeType; - name = sd->toChars(); - linnum = sd->loc.linnum; - file = DtoDwarfFile(sd->loc); + // elements + std::vector elems; + + // defaults + llvm::StringRef name = sd->toChars(); + unsigned linnum = sd->loc.linnum; + llvm::DIFile file = DtoDwarfFile(sd->loc); + llvm::DIType derivedFrom; + // set diCompositeType to handle recursive types properly if (!ir->diCompositeType) ir->diCompositeType = gIR->dibuilder.createTemporaryType(); diff --git a/gen/toir.cpp b/gen/toir.cpp index d922e906..7170453e 100644 --- a/gen/toir.cpp +++ b/gen/toir.cpp @@ -1950,9 +1950,10 @@ DValue* NewExp::toElem(IRState* p) else { size_t ndims = arguments->dim; - std::vector dims(ndims); + std::vector dims; + dims.reserve(ndims); for (size_t i=0; i(arguments->data[i])->toElem(p); + dims.push_back(static_cast(arguments->data[i])->toElem(p)); return DtoNewMulDimDynArray(loc, newtype, &dims[0], ndims, true); } } @@ -2798,11 +2799,12 @@ LLConstant* ArrayLiteralExp::toConstElem(IRState* p) bool dyn = (bt->ty != Tsarray); // build the initializer - std::vector vals(elements->dim, NULL); + std::vector vals; + vals.reserve(elements->dim); for (unsigned i=0; idim; ++i) { Expression* expr = static_cast(elements->data[i]); - vals[i] = expr->toConstElem(p); + vals.push_back(expr->toConstElem(p)); } // build the constant array initialize @@ -2974,7 +2976,7 @@ LLConstant* StructLiteralExp::toConstElem(IRState* p) sd->codegen(Type::sir); // get inits - std::vector inits(sd->fields.dim, NULL); + std::vector inits(sd->fields.dim, 0); size_t nexprs = elements->dim;; Expression** exprs = (Expression**)elements->data; @@ -2987,8 +2989,8 @@ LLConstant* StructLiteralExp::toConstElem(IRState* p) std::vector values = DtoStructLiteralValues(sd, inits); // we know those values are constants.. cast them - std::vector constvals(values.size(), NULL); - std::vector types(values.size(), NULL); + std::vector constvals(values.size(), 0); + std::vector types(values.size(), 0); for (size_t i = 0; i < values.size(); ++i) { constvals[i] = llvm::cast(values[i]); types[i] = values[i]->getType(); @@ -3059,6 +3061,8 @@ DValue* AssocArrayLiteralExp::toElem(IRState* p) { std::vector keysInits, valuesInits; + keysInits.reserve(keys->dim); + valuesInits.reserve(keys->dim); for (size_t i = 0, n = keys->dim; i < n; ++i) { Expression* ekey = keys->tdata()[i]; @@ -3178,11 +3182,12 @@ DValue* TypeExp::toElem(IRState *p) DValue* TupleExp::toElem(IRState *p) { Logger::print("TupleExp::toElem() %s\n", toChars()); - std::vector types(exps->dim, NULL); + std::vector types; + types.reserve(exps->dim); for (size_t i = 0; i < exps->dim; i++) { Expression *el = static_cast(exps->data[i]); - types[i] = DtoTypeNotVoid(el->type); + types.push_back(DtoTypeNotVoid(el->type)); } LLValue *val = DtoRawAlloca(LLStructType::get(gIR->context(), types),0, "tuple"); for (size_t i = 0; i < exps->dim; i++)