diff --git a/gen/aa.cpp b/gen/aa.cpp index bb31940a..45dbb4d6 100644 --- a/gen/aa.cpp +++ b/gen/aa.cpp @@ -33,7 +33,7 @@ static LLValue* to_pkey(Loc& loc, DValue* key) pkey = key->getRVal(); } else { - LLValue* tmp = DtoAlloca(DtoType(keytype), "aatmpkeystorage"); + LLValue* tmp = DtoAlloca(keytype, "aatmpkeystorage"); DVarValue var(keytype, tmp); DtoAssign(loc, &var, key); return tmp; @@ -41,7 +41,7 @@ static LLValue* to_pkey(Loc& loc, DValue* key) // give memory if (needmem) { - LLValue* tmp = DtoAlloca(DtoType(keytype), "aatmpkeystorage"); + LLValue* tmp = DtoAlloca(keytype, "aatmpkeystorage"); DtoStore(pkey, tmp); pkey = tmp; } diff --git a/gen/abi-x86-64.cpp b/gen/abi-x86-64.cpp index 7fdbb5d0..92a304eb 100644 --- a/gen/abi-x86-64.cpp +++ b/gen/abi-x86-64.cpp @@ -455,7 +455,7 @@ struct X86_64_C_struct_rewrite : ABIRewrite { } else { // No memory location, create one. LLValue* rval = v->getRVal(); - lval = DtoAlloca(rval->getType()); + lval = DtoRawAlloca(rval->getType(), 0); DtoStore(rval, lval); } @@ -479,7 +479,7 @@ struct X86_64_C_struct_rewrite : ABIRewrite { } else { // No memory location, create one. LLValue* rval = v->getRVal(); - lval = DtoAlloca(rval->getType()); + lval = DtoRawAlloca(rval->getType(), 0); DtoStore(rval, lval); } diff --git a/gen/abi.cpp b/gen/abi.cpp index a0bcef98..b7a6c6df 100644 --- a/gen/abi.cpp +++ b/gen/abi.cpp @@ -112,7 +112,7 @@ struct X86_struct_to_register : ABIRewrite LLValue* get(Type* dty, DValue* dv) { Logger::println("rewriting int -> struct"); - LLValue* mem = DtoAlloca(DtoType(dty), ".int_to_struct"); + LLValue* mem = DtoAlloca(dty, ".int_to_struct"); LLValue* v = dv->getRVal(); DtoStore(v, DtoBitCast(mem, getPtrToType(v->getType()))); return DtoLoad(mem); diff --git a/gen/arrays.cpp b/gen/arrays.cpp index 27c75aac..41a9ee0e 100644 --- a/gen/arrays.cpp +++ b/gen/arrays.cpp @@ -77,7 +77,7 @@ void DtoArrayInit(Loc& loc, DValue* array, DValue* value) // give slices and complex values storage (and thus an address to pass) if (value->isSlice()) { - val = DtoAlloca(DtoType(value->getType()), ".tmpparam"); + val = DtoAlloca(value->getType(), ".tmpparam"); DVarValue lval(value->getType(), val); DtoAssign(loc, &lval, value); } @@ -448,7 +448,7 @@ DSliceValue* DtoNewMulDimDynArray(Loc& loc, Type* arrayType, DValue** dims, size LLFunction* fn = LLVM_D_GetRuntimeFunction(gIR->module, fnname); // build dims - LLValue* dimsArg = DtoAlloca(DtoSize_t(), DtoConstUint(ndims), ".newdims"); + LLValue* dimsArg = DtoArrayAlloca(Type::tsize_t, ndims, ".newdims"); LLValue* firstDim = NULL; for (size_t i=0; icode += asmGotoEndLabel.str()+":\n"; // create storage for and initialize the temporary - jump_target = DtoAlloca(LLType::Int32Ty, "__llvm_jump_target"); + jump_target = DtoAlloca(Type::tint32, "__llvm_jump_target"); gIR->ir->CreateStore(DtoConstUint(0), jump_target); // setup variable for output from asm outSetterStmt->out_c = "=*m,"; diff --git a/gen/classes.cpp b/gen/classes.cpp index f492af73..d68c7384 100644 --- a/gen/classes.cpp +++ b/gen/classes.cpp @@ -129,7 +129,8 @@ DValue* DtoNewClass(Loc loc, TypeClass* tc, NewExp* newexp) LLValue* mem; if (newexp->onstack) { - mem = DtoAlloca(DtoType(tc)->getContainedType(0), ".newclass_alloca"); + // FIXME align scope class to its largest member + mem = DtoRawAlloca(DtoType(tc)->getContainedType(0), 0, ".newclass_alloca"); } // custom allocator else if (newexp->allocator) diff --git a/gen/functions.cpp b/gen/functions.cpp index c52b3f12..198b9875 100644 --- a/gen/functions.cpp +++ b/gen/functions.cpp @@ -683,6 +683,7 @@ void DtoDefineFunction(FuncDeclaration* fd) gIR->scopes.push_back(IRScope(beginbb, endbb)); // create alloca point + // this gets erased when the function is complete, so alignment etc does not matter at all llvm::Instruction* allocaPoint = new llvm::AllocaInst(LLType::Int32Ty, "alloca point", beginbb); irfunction->allocapoint = allocaPoint; @@ -704,7 +705,7 @@ void DtoDefineFunction(FuncDeclaration* fd) LLValue* thisvar = irfunction->thisArg; assert(thisvar); - LLValue* thismem = DtoAlloca(thisvar->getType(), "this"); + LLValue* thismem = DtoRawAlloca(thisvar->getType(), 0, "this"); // FIXME: align? DtoStore(thisvar, thismem); irfunction->thisArg = thismem; @@ -760,7 +761,7 @@ void DtoDefineFunction(FuncDeclaration* fd) argt = irloc->value->getType(); else argt = DtoType(vd->type); - LLValue* mem = DtoAlloca(argt, vd->ident->toChars()); + LLValue* mem = DtoRawAlloca(argt, 0, vd->ident->toChars()); // let the abi transform the argument back first DImValue arg_dval(vd->type, irloc->value); @@ -796,19 +797,19 @@ void DtoDefineFunction(FuncDeclaration* fd) DtoNestedInit(fd->vresult); } else if (fd->vresult) { fd->vresult->ir.irLocal = new IrLocal(fd->vresult); - fd->vresult->ir.irLocal->value = DtoAlloca(DtoType(fd->vresult->type), fd->vresult->toChars()); + fd->vresult->ir.irLocal->value = DtoAlloca(fd->vresult->type, fd->vresult->toChars()); } // copy _argptr and _arguments to a memory location if (f->linkage == LINKd && f->varargs == 1) { // _argptr - LLValue* argptrmem = DtoAlloca(fd->ir.irFunc->_argptr->getType(), "_argptr_mem"); + LLValue* argptrmem = DtoRawAlloca(fd->ir.irFunc->_argptr->getType(), 0, "_argptr_mem"); new llvm::StoreInst(fd->ir.irFunc->_argptr, argptrmem, gIR->scopebb()); fd->ir.irFunc->_argptr = argptrmem; // _arguments - LLValue* argumentsmem = DtoAlloca(fd->ir.irFunc->_arguments->getType(), "_arguments_mem"); + LLValue* argumentsmem = DtoRawAlloca(fd->ir.irFunc->_arguments->getType(), 0, "_arguments_mem"); new llvm::StoreInst(fd->ir.irFunc->_arguments, argumentsmem, gIR->scopebb()); fd->ir.irFunc->_arguments = argumentsmem; } @@ -918,7 +919,7 @@ DValue* DtoArgument(Argument* fnarg, Expression* argexp) // byval arg, but expr has no storage yet else if (DtoIsPassedByRef(argexp->type) && (arg->isSlice() || arg->isNull())) { - LLValue* alloc = DtoAlloca(DtoType(argexp->type), ".tmp_arg"); + LLValue* alloc = DtoAlloca(argexp->type, ".tmp_arg"); DVarValue* vv = new DVarValue(argexp->type, alloc); DtoAssign(argexp->loc, vv, arg); arg = vv; diff --git a/gen/llvmhelpers.cpp b/gen/llvmhelpers.cpp index d641e981..72f06f92 100644 --- a/gen/llvmhelpers.cpp +++ b/gen/llvmhelpers.cpp @@ -95,14 +95,29 @@ void DtoDeleteArray(DValue* arr) ////////////////////////////////////////////////////////////////////////////////////////*/ -llvm::AllocaInst* DtoAlloca(const LLType* lltype, const std::string& name) +llvm::AllocaInst* DtoAlloca(Type* type, const char* name) { - return new llvm::AllocaInst(lltype, name, gIR->topallocapoint()); + const llvm::Type* lltype = DtoType(type); + llvm::AllocaInst* ai = new llvm::AllocaInst(lltype, name, gIR->topallocapoint()); + ai->setAlignment(type->alignsize()); + return ai; } -llvm::AllocaInst* DtoAlloca(const LLType* lltype, LLValue* arraysize, const std::string& name) +llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* name) { - return new llvm::AllocaInst(lltype, arraysize, name, gIR->topallocapoint()); + const llvm::Type* lltype = DtoType(type); + llvm::AllocaInst* ai = new llvm::AllocaInst( + lltype, DtoConstUint(arraysize), name, gIR->topallocapoint()); + ai->setAlignment(type->alignsize()); + return ai; +} + +llvm::AllocaInst* DtoRawAlloca(const llvm::Type* lltype, size_t alignment, const char* name) +{ + llvm::AllocaInst* ai = new llvm::AllocaInst(lltype, name, gIR->topallocapoint()); + if (alignment) + ai->setAlignment(alignment); + return ai; } @@ -894,7 +909,7 @@ DValue* DtoDeclarationExp(Dsymbol* declaration) if(gTargetData->getTypeSizeInBits(lltype) == 0) allocainst = llvm::ConstantPointerNull::get(getPtrToType(lltype)); else - allocainst = DtoAlloca(lltype, vd->toChars()); + allocainst = DtoAlloca(vd->type, vd->toChars()); //allocainst->setAlignment(vd->type->alignsize()); // TODO vd->ir.irLocal = new IrLocal(vd); @@ -1010,7 +1025,7 @@ LLValue* DtoRawVarDeclaration(VarDeclaration* var, LLValue* addr) LLValue* allocaval = NULL; if (!addr && (!var->ir.irLocal || !var->ir.irLocal->value)) { - addr = DtoAlloca(DtoType(var->type), var->toChars()); + addr = DtoAlloca(var->type, var->toChars()); // add debug info if (global.params.symdebug) diff --git a/gen/llvmhelpers.h b/gen/llvmhelpers.h index 2d49e75c..8cc4e38c 100644 --- a/gen/llvmhelpers.h +++ b/gen/llvmhelpers.h @@ -40,8 +40,9 @@ void DtoDeleteInterface(LLValue* inst); void DtoDeleteArray(DValue* arr); // emit an alloca -llvm::AllocaInst* DtoAlloca(const LLType* lltype, const std::string& name = ""); -llvm::AllocaInst* DtoAlloca(const LLType* lltype, LLValue* arraysize, const std::string& name = ""); +llvm::AllocaInst* DtoAlloca(Type* type, const char* name = ""); +llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* name = ""); +llvm::AllocaInst* DtoRawAlloca(const llvm::Type* lltype, size_t alignment, const char* name = ""); // assertion generator void DtoAssert(Module* M, Loc loc, DValue* msg); diff --git a/gen/naked.cpp b/gen/naked.cpp index 4b72d974..9302bac6 100644 --- a/gen/naked.cpp +++ b/gen/naked.cpp @@ -258,7 +258,7 @@ void emitABIReturnAsmStmt(IRAsmBlock* asmblock, Loc loc, FuncDeclaration* fdecl) // generate asm as->out_c = "=*m,=*m,"; - LLValue* tmp = DtoAlloca(llretTy, ".tmp_asm_ret"); + LLValue* tmp = DtoRawAlloca(llretTy, 0, ".tmp_asm_ret"); as->out.push_back( tmp ); as->out.push_back( DtoGEPi(tmp, 0,1) ); as->code = "movd %eax, $<>" "\n\t" "mov %edx, $<>"; @@ -413,7 +413,7 @@ DValue * DtoInlineAsmExpr(Loc loc, FuncDeclaration * fd, Expressions * arguments if (type->ty == Tstruct) { // make a copy - llvm::Value* mem = DtoAlloca(ret_type, ".__asm_tuple_ret"); + llvm::Value* mem = DtoAlloca(type, ".__asm_tuple_ret"); TypeStruct* ts = (TypeStruct*)type; size_t n = ts->sym->fields.dim; diff --git a/gen/nested.cpp b/gen/nested.cpp index aab892fd..54550d07 100644 --- a/gen/nested.cpp +++ b/gen/nested.cpp @@ -167,7 +167,7 @@ void DtoNestedInit(VarDeclaration* vd) if (nestedCtx == NCArray) { // alloca as usual if no value already if (!vd->ir.irLocal->value) - vd->ir.irLocal->value = DtoAlloca(DtoType(vd->type), vd->toChars()); + vd->ir.irLocal->value = DtoAlloca(vd->type, vd->toChars()); // store the address into the nested vars array assert(vd->ir.irLocal->nestedIndex >= 0); @@ -324,7 +324,8 @@ void DtoCreateNestedContext(FuncDeclaration* fd) { const LLType* nestedVarsTy = LLArrayType::get(getVoidPtrType(), nelems); // alloca it - LLValue* nestedVars = DtoAlloca(nestedVarsTy, ".nested_vars"); + // FIXME align ? + LLValue* nestedVars = DtoRawAlloca(nestedVarsTy, 0, ".nested_vars"); IrFunction* irfunction = fd->ir.irFunc; @@ -459,7 +460,8 @@ void DtoCreateNestedContext(FuncDeclaration* fd) { // FIXME: For D2, this should be a gc_malloc (or similar) call, not alloca // (Note that it'd also require more aggressive copying of // by-value parameters instead of just alloca'd ones) - LLValue* frame = DtoAlloca(frameType, ".frame"); + // FIXME: alignment ? + LLValue* frame = DtoRawAlloca(frameType, 0, ".frame"); // copy parent frames into beginning if (depth != 0) { diff --git a/gen/passes/GarbageCollect2Stack.cpp b/gen/passes/GarbageCollect2Stack.cpp index a2940775..4906b3a9 100644 --- a/gen/passes/GarbageCollect2Stack.cpp +++ b/gen/passes/GarbageCollect2Stack.cpp @@ -98,7 +98,7 @@ namespace { NumGcToStack++; Instruction* Begin = CS.getCaller()->getEntryBlock().begin(); - return new AllocaInst(Ty, ".nongc_mem", Begin); + return new AllocaInst(Ty, ".nongc_mem", Begin); // FIXME: align? } FunctionInfo(unsigned typeInfoArgNr, bool safeToDelete) @@ -165,7 +165,7 @@ namespace { // Convert array size to 32 bits if necessary Value* count = Builder.CreateIntCast(arrSize, Type::Int32Ty, false); - AllocaInst* alloca = Builder.CreateAlloca(Ty, count, ".nongc_mem"); + AllocaInst* alloca = Builder.CreateAlloca(Ty, count, ".nongc_mem"); // FIXME: align? if (Initialized) { // For now, only zero-init is supported. diff --git a/gen/statements.cpp b/gen/statements.cpp index 6986501b..31bf8a73 100644 --- a/gen/statements.cpp +++ b/gen/statements.cpp @@ -1025,7 +1025,7 @@ void ForeachStatement::toIR(IRState* p) if (key) keyvar = DtoRawVarDeclaration(key); else - keyvar = DtoAlloca(keytype, "foreachkey"); + keyvar = DtoRawAlloca(keytype, 0, "foreachkey"); // FIXME: align? LLValue* zerokey = llvm::ConstantInt::get(keytype,0,false); // value diff --git a/gen/tocall.cpp b/gen/tocall.cpp index 2e52a725..222d5c47 100644 --- a/gen/tocall.cpp +++ b/gen/tocall.cpp @@ -169,7 +169,7 @@ void DtoBuildDVarArgList(std::vector& args, std::vector