Merge branch 'master' into merge-2.068

This commit is contained in:
Martin 2015-10-22 22:39:46 +02:00
commit b63a5e3cf8
18 changed files with 88 additions and 49 deletions

View file

@ -876,7 +876,7 @@ void DtoDefineFunction(FuncDeclaration* fd)
// Create the param here and set it to a "dummy" alloca that // Create the param here and set it to a "dummy" alloca that
// we do not store to here. // we do not store to here.
irparam = getIrParameter(vd, true); irparam = getIrParameter(vd, true);
irparam->value = DtoAlloca(vd->type, vd->ident->toChars()); irparam->value = DtoAlloca(vd, vd->ident->toChars());
} }
else else
{ {

View file

@ -134,6 +134,24 @@ void DtoDeleteArray(Loc& loc, DValue* arr)
); );
} }
/****************************************************************************************/
/*////////////////////////////////////////////////////////////////////////////////////////
// ALIGNMENT HELPERS
////////////////////////////////////////////////////////////////////////////////////////*/
unsigned DtoAlignment(Type* type)
{
structalign_t alignment = type->alignment();
if (alignment == STRUCTALIGN_DEFAULT)
alignment = type->alignsize();
return (alignment == STRUCTALIGN_DEFAULT ? 0 : alignment);
}
unsigned DtoAlignment(VarDeclaration* vd)
{
return vd->alignment == STRUCTALIGN_DEFAULT ? DtoAlignment(vd->type) : vd->alignment;
}
/****************************************************************************************/ /****************************************************************************************/
/*//////////////////////////////////////////////////////////////////////////////////////// /*////////////////////////////////////////////////////////////////////////////////////////
// ALLOCA HELPERS // ALLOCA HELPERS
@ -141,7 +159,12 @@ void DtoDeleteArray(Loc& loc, DValue* arr)
llvm::AllocaInst* DtoAlloca(Type* type, const char* name) llvm::AllocaInst* DtoAlloca(Type* type, const char* name)
{ {
return DtoRawAlloca(DtoMemType(type), type->alignsize(), name); return DtoRawAlloca(DtoMemType(type), DtoAlignment(type), name);
}
llvm::AllocaInst* DtoAlloca(VarDeclaration* vd, const char* name)
{
return DtoRawAlloca(DtoMemType(vd->type), DtoAlignment(vd), name);
} }
llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* name) llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* name)
@ -149,7 +172,7 @@ llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* nam
LLType* lltype = DtoType(type); LLType* lltype = DtoType(type);
llvm::AllocaInst* ai = new llvm::AllocaInst( llvm::AllocaInst* ai = new llvm::AllocaInst(
lltype, DtoConstUint(arraysize), name, gIR->topallocapoint()); lltype, DtoConstUint(arraysize), name, gIR->topallocapoint());
ai->setAlignment(type->alignsize()); ai->setAlignment(DtoAlignment(type));
return ai; return ai;
} }
@ -195,7 +218,7 @@ LLValue* DtoAllocaDump(LLValue* val, int alignment, const char* name)
LLValue* DtoAllocaDump(LLValue* val, Type* asType, const char* name) LLValue* DtoAllocaDump(LLValue* val, Type* asType, const char* name)
{ {
return DtoAllocaDump(val, DtoType(asType), asType->alignsize(), name); return DtoAllocaDump(val, DtoType(asType), DtoAlignment(asType), name);
} }
LLValue* DtoAllocaDump(LLValue* val, LLType* asType, int alignment, const char* name) LLValue* DtoAllocaDump(LLValue* val, LLType* asType, int alignment, const char* name)
@ -869,11 +892,9 @@ void DtoResolveVariable(VarDeclaration* vd)
vd->isThreadlocal()); vd->isThreadlocal());
getIrGlobal(vd)->value = gvar; getIrGlobal(vd)->value = gvar;
// Set the alignment (it is important not to use type->alignsize because // Set the alignment and use the target pointer size as lower bound.
// VarDeclarations can have an align() attribute independent of the type unsigned alignment = std::max(DtoAlignment(vd), gDataLayout->getPointerSize());
// as well). gvar->setAlignment(alignment);
if (vd->alignment != STRUCTALIGN_DEFAULT)
gvar->setAlignment(vd->alignment);
IF_LOG Logger::cout() << *gvar << '\n'; IF_LOG Logger::cout() << *gvar << '\n';
} }
@ -928,8 +949,10 @@ void DtoVarDeclaration(VarDeclaration* vd)
LLType* lltype = DtoType(type); LLType* lltype = DtoType(type);
if(gDataLayout->getTypeSizeInBits(lltype) == 0) if(gDataLayout->getTypeSizeInBits(lltype) == 0)
allocainst = llvm::ConstantPointerNull::get(getPtrToType(lltype)); allocainst = llvm::ConstantPointerNull::get(getPtrToType(lltype));
else else if (type != vd->type)
allocainst = DtoAlloca(type, vd->toChars()); allocainst = DtoAlloca(type, vd->toChars());
else
allocainst = DtoAlloca(vd, vd->toChars());
irLocal->value = allocainst; irLocal->value = allocainst;
@ -1106,7 +1129,7 @@ LLValue* DtoRawVarDeclaration(VarDeclaration* var, LLValue* addr)
// alloca if necessary // alloca if necessary
if (!addr && (!irLocal || !irLocal->value)) if (!addr && (!irLocal || !irLocal->value))
{ {
addr = DtoAlloca(var->type, var->toChars()); addr = DtoAlloca(var, var->toChars());
// add debug info // add debug info
if (!irLocal) if (!irLocal)
irLocal = getIrLocal(var, true); irLocal = getIrLocal(var, true);

View file

@ -31,8 +31,12 @@ void DtoDeleteClass(Loc& loc, DValue* inst);
void DtoDeleteInterface(Loc& loc, DValue* inst); void DtoDeleteInterface(Loc& loc, DValue* inst);
void DtoDeleteArray(Loc& loc, DValue* arr); void DtoDeleteArray(Loc& loc, DValue* arr);
unsigned DtoAlignment(Type* type);
unsigned DtoAlignment(VarDeclaration* vd);
// emit an alloca // emit an alloca
llvm::AllocaInst* DtoAlloca(Type* type, const char* name = ""); llvm::AllocaInst* DtoAlloca(Type* type, const char* name = "");
llvm::AllocaInst* DtoAlloca(VarDeclaration* vd, const char* name = "");
llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* name = ""); llvm::AllocaInst* DtoArrayAlloca(Type* type, unsigned arraysize, const char* name = "");
llvm::AllocaInst* DtoRawAlloca(LLType* lltype, size_t alignment, const char* name = ""); llvm::AllocaInst* DtoRawAlloca(LLType* lltype, size_t alignment, const char* name = "");
LLValue* DtoGcMalloc(Loc& loc, LLType* lltype, const char* name = ""); LLValue* DtoGcMalloc(Loc& loc, LLType* lltype, const char* name = "");

View file

@ -16,6 +16,7 @@
#include "gen/logger.h" #include "gen/logger.h"
#include "gen/tollvm.h" #include "gen/tollvm.h"
#include "ir/irfunction.h" #include "ir/irfunction.h"
#include "ir/irtypeaggr.h"
#include "llvm/Analysis/ValueTracking.h" #include "llvm/Analysis/ValueTracking.h"
/****************************************************************************************/ /****************************************************************************************/
@ -361,24 +362,17 @@ static void DtoCreateNestedContextType(FuncDeclaration* fd)
IF_LOG Logger::cout() << "Function " << fd->toChars() << " has depth " << depth << '\n'; IF_LOG Logger::cout() << "Function " << fd->toChars() << " has depth " << depth << '\n';
typedef std::vector<LLType*> TypeVec; AggrTypeBuilder builder(false);
TypeVec types;
if (depth != 0) if (depth != 0)
{ {
assert(innerFrameType); assert(innerFrameType);
unsigned ptrSize = gDataLayout->getPointerSize();
// Add frame pointer types for all but last frame // Add frame pointer types for all but last frame
for (unsigned i = 0; i < (depth - 1); ++i) for (unsigned i = 0; i < (depth - 1); ++i)
types.push_back(innerFrameType->getElementType(i)); builder.addType(innerFrameType->getElementType(i), ptrSize);
// Add frame pointer type for last frame // Add frame pointer type for last frame
types.push_back(LLPointerType::getUnqual(innerFrameType)); builder.addType(LLPointerType::getUnqual(innerFrameType), ptrSize);
}
if (Logger::enabled() && depth != 0)
{
Logger::println("Frame types: ");
LOG_SCOPE;
for (TypeVec::iterator i = types.begin(); i != types.end(); ++i)
Logger::cout() << **i << '\n';
} }
// Add the direct nested variables of this function, and update their indices to match. // Add the direct nested variables of this function, and update their indices to match.
@ -388,10 +382,16 @@ static void DtoCreateNestedContextType(FuncDeclaration* fd)
I != E; ++I) I != E; ++I)
{ {
VarDeclaration* vd = *I; VarDeclaration* vd = *I;
unsigned alignment = DtoAlignment(vd);
if (alignment > 1)
builder.alignCurrentOffset(alignment);
IrLocal& irLocal = *getIrLocal(vd, true); IrLocal& irLocal = *getIrLocal(vd, true);
irLocal.nestedIndex = types.size(); irLocal.nestedIndex = builder.currentFieldIndex();
irLocal.nestedDepth = depth; irLocal.nestedDepth = depth;
LLType* t = NULL;
if (vd->isParameter() && getIrParameter(vd)->arg) if (vd->isParameter() && getIrParameter(vd)->arg)
{ {
// Parameters that are part of the LLVM signature will have // Parameters that are part of the LLVM signature will have
@ -407,29 +407,32 @@ static void DtoCreateNestedContextType(FuncDeclaration* fd)
{ {
// This will be copied to the nesting frame. // This will be copied to the nesting frame.
if (lazy) if (lazy)
types.push_back(irparam->value->getType()->getContainedType(0)); t = irparam->value->getType()->getContainedType(0);
else else
types.push_back(DtoMemType(vd->type)); t = DtoMemType(vd->type);
} }
else else
types.push_back(irparam->value->getType()); t = irparam->value->getType();
} }
else if (isSpecialRefVar(vd)) else if (isSpecialRefVar(vd))
types.push_back(DtoType(vd->type->pointerTo())); t = DtoType(vd->type->pointerTo());
else else
types.push_back(DtoMemType(vd->type)); t = DtoMemType(vd->type);
builder.addType(t, getTypeAllocSize(t));
IF_LOG Logger::cout() << "Nested var '" << vd->toChars() IF_LOG Logger::cout() << "Nested var '" << vd->toChars()
<< "' of type " << *types.back() << "\n"; << "' of type " << *t << "\n";
} }
LLStructType* frameType = LLStructType::create(gIR->context(), types, LLStructType* frameType = LLStructType::create(gIR->context(), builder.defaultTypes(),
std::string("nest.") + fd->toChars()); std::string("nest.") + fd->toChars());
IF_LOG Logger::cout() << "frameType = " << *frameType << '\n'; IF_LOG Logger::cout() << "frameType = " << *frameType << '\n';
// Store type in IrFunction // Store type in IrFunction
irFunc.frameType = frameType; irFunc.frameType = frameType;
irFunc.frameTypeAlignment = builder.overallAlignment();
} }
else // no captured variables else // no captured variables
{ {
@ -438,6 +441,7 @@ static void DtoCreateNestedContextType(FuncDeclaration* fd)
// Propagate context arg properties if the context arg is passed on unmodified. // Propagate context arg properties if the context arg is passed on unmodified.
IrFunction& parentIrFunc = *getIrFunc(parentFunc); IrFunction& parentIrFunc = *getIrFunc(parentFunc);
irFunc.frameType = parentIrFunc.frameType; irFunc.frameType = parentIrFunc.frameType;
irFunc.frameTypeAlignment = parentIrFunc.frameTypeAlignment;
irFunc.depth = parentIrFunc.depth; irFunc.depth = parentIrFunc.depth;
} }
} }
@ -457,13 +461,18 @@ void DtoCreateNestedContext(FuncDeclaration* fd) {
unsigned depth = irfunction->depth; unsigned depth = irfunction->depth;
LLStructType *frameType = irfunction->frameType; LLStructType *frameType = irfunction->frameType;
// Create frame for current function and append to frames list // Create frame for current function and append to frames list
// FIXME: alignment ?
LLValue* frame = 0; LLValue* frame = 0;
bool needsClosure = fd->needsClosure(); bool needsClosure = fd->needsClosure();
if (needsClosure) if (needsClosure)
{
// FIXME: alignment ?
frame = DtoGcMalloc(fd->loc, frameType, ".frame"); frame = DtoGcMalloc(fd->loc, frameType, ".frame");
}
else else
frame = DtoRawAlloca(frameType, 0, ".frame"); {
unsigned alignment = std::max(getABITypeAlign(frameType), irfunction->frameTypeAlignment);
frame = DtoRawAlloca(frameType, alignment, ".frame");
}
// copy parent frames into beginning // copy parent frames into beginning
if (depth != 0) { if (depth != 0) {

View file

@ -44,7 +44,7 @@ static std::string getProgram(const char* name, const cl::opt<std::string>* opt,
if (opt && opt->getNumOccurrences() > 0 && opt->length() > 0 && (prog = opt->c_str())) if (opt && opt->getNumOccurrences() > 0 && opt->length() > 0 && (prog = opt->c_str()))
path = findProgramByName(prog); path = findProgramByName(prog);
if (path.empty() && envVar && (prog = getenv(envVar))) if (path.empty() && envVar && (prog = getenv(envVar)) && prog[0] != '\0')
path = findProgramByName(prog); path = findProgramByName(prog);
if (path.empty()) if (path.empty())

View file

@ -103,7 +103,7 @@ void RTTIBuilder::push_void_array(llvm::Constant* CI, Type* valtype, Dsymbol* ma
LLGlobalVariable* G = new LLGlobalVariable( LLGlobalVariable* G = new LLGlobalVariable(
gIR->module, CI->getType(), true, TYPEINFO_LINKAGE_TYPE, CI, initname); gIR->module, CI->getType(), true, TYPEINFO_LINKAGE_TYPE, CI, initname);
SET_COMDAT(G, gIR->module); SET_COMDAT(G, gIR->module);
G->setAlignment(valtype->alignsize()); G->setAlignment(DtoAlignment(valtype));
push_void_array(getTypePaddedSize(CI->getType()), G); push_void_array(getTypePaddedSize(CI->getType()), G);
} }
@ -123,7 +123,7 @@ void RTTIBuilder::push_array(llvm::Constant * CI, uint64_t dim, Type* valtype, D
LLGlobalVariable* G = new LLGlobalVariable( LLGlobalVariable* G = new LLGlobalVariable(
gIR->module, CI->getType(), true, TYPEINFO_LINKAGE_TYPE, CI, initname); gIR->module, CI->getType(), true, TYPEINFO_LINKAGE_TYPE, CI, initname);
SET_COMDAT(G, gIR->module); SET_COMDAT(G, gIR->module);
G->setAlignment(valtype->alignsize()); G->setAlignment(DtoAlignment(valtype));
push_array(dim, DtoBitCast(G, DtoType(valtype->pointerTo()))); push_array(dim, DtoBitCast(G, DtoType(valtype->pointerTo())));
} }

View file

@ -10,6 +10,7 @@
#include "target.h" #include "target.h"
#include "gen/abi.h" #include "gen/abi.h"
#include "gen/irstate.h" #include "gen/irstate.h"
#include "gen/llvmhelpers.h"
#include "mars.h" #include "mars.h"
#include "mtype.h" #include "mtype.h"
#include <assert.h> #include <assert.h>
@ -59,8 +60,7 @@ unsigned Target::alignsize (Type* type)
unsigned Target::fieldalign (Type* type) unsigned Target::fieldalign (Type* type)
{ {
// LDC_FIXME: Verify this. return DtoAlignment(type);
return type->alignsize();
} }
// sizes based on those from tollvm.cpp:DtoMutexType() // sizes based on those from tollvm.cpp:DtoMutexType()

View file

@ -663,7 +663,7 @@ private:
LLValue* var = retvar; LLValue* var = retvar;
if (!var) if (!var)
var = DtoRawAlloca(llArgType->getContainedType(0), resulttype->alignsize(), ".rettmp"); var = DtoRawAlloca(llArgType->getContainedType(0), DtoAlignment(resulttype), ".rettmp");
args.push_back(var); args.push_back(var);
attrs.add(index + 1, irFty.arg_sret->attrs); attrs.add(index + 1, irFty.arg_sret->attrs);

View file

@ -504,7 +504,7 @@ public:
se->globalVar = finalGlobalVar; se->globalVar = finalGlobalVar;
} }
se->globalVar->setInitializer(constValue); se->globalVar->setInitializer(constValue);
se->globalVar->setAlignment(e->e1->type->alignsize()); se->globalVar->setAlignment(DtoAlignment(se->type));
result = se->globalVar; result = se->globalVar;
} }

View file

@ -2590,7 +2590,7 @@ public:
} }
else else
{ {
llvm::Value* storage = DtoRawAlloca(llStoType, e->type->alignsize(), "arrayliteral"); llvm::Value* storage = DtoRawAlloca(llStoType, DtoAlignment(e->type), "arrayliteral");
initializeArrayLiteral(p, e, storage); initializeArrayLiteral(p, e, storage);
result = new DImValue(e->type, storage); result = new DImValue(e->type, storage);
} }

View file

@ -723,7 +723,7 @@ size_t getTypeAllocSize(LLType* t)
return gDataLayout->getTypeAllocSize(t); return gDataLayout->getTypeAllocSize(t);
} }
unsigned char getABITypeAlign(LLType* t) unsigned int getABITypeAlign(LLType* t)
{ {
return gDataLayout->getABITypeAlignment(t); return gDataLayout->getABITypeAlignment(t);
} }

View file

@ -131,7 +131,7 @@ size_t getTypePaddedSize(LLType* t);
size_t getTypeAllocSize(LLType* t); size_t getTypeAllocSize(LLType* t);
// type alignments // type alignments
unsigned char getABITypeAlign(LLType* t); unsigned int getABITypeAlign(LLType* t);
// pair type helpers // pair type helpers
LLValue* DtoAggrPair(LLType* type, LLValue* V1, LLValue* V2, const char* name = ""); LLValue* DtoAggrPair(LLType* type, LLValue* V1, LLValue* V2, const char* name = "");

View file

@ -483,7 +483,7 @@ public:
b.push_funcptr(xpostblit); b.push_funcptr(xpostblit);
//uint m_align; //uint m_align;
b.push_uint(tc->alignsize()); b.push_uint(DtoAlignment(tc));
if (global.params.is64bit) if (global.params.is64bit)
{ {

View file

@ -58,10 +58,7 @@ LLGlobalVariable * IrAggr::getInitSymbol()
gIR->module, init_type, true, llvm::GlobalValue::ExternalLinkage, NULL, initname); gIR->module, init_type, true, llvm::GlobalValue::ExternalLinkage, NULL, initname);
// set alignment // set alignment
init->setAlignment(type->alignsize()); init->setAlignment(DtoAlignment(type));
StructDeclaration *sd = aggrdecl->isStructDeclaration();
if (sd && sd->alignment != STRUCTALIGN_DEFAULT)
init->setAlignment(sd->alignment);
return init; return init;
} }

View file

@ -423,6 +423,7 @@ IrFunction::IrFunction(FuncDeclaration* fd) {
nestedVar = NULL; nestedVar = NULL;
frameType = NULL; frameType = NULL;
frameTypeAlignment = 0;
depth = -1; depth = -1;
nestedContextCreated = false; nestedContextCreated = false;

View file

@ -411,6 +411,7 @@ struct IrFunction {
llvm::Value* nestedVar; // alloca for the nested context of this function llvm::Value* nestedVar; // alloca for the nested context of this function
llvm::StructType* frameType; // type of nested context llvm::StructType* frameType; // type of nested context
unsigned frameTypeAlignment; // its alignment
// number of enclosing functions with variables accessed by nested functions // number of enclosing functions with variables accessed by nested functions
// (-1 if neither this function nor any enclosing ones access variables from enclosing functions) // (-1 if neither this function nor any enclosing ones access variables from enclosing functions)
int depth; int depth;

View file

@ -50,7 +50,7 @@ bool var_offset_sort_cb(const VarDeclaration* v1, const VarDeclaration* v2)
} }
AggrTypeBuilder::AggrTypeBuilder(bool packed) : AggrTypeBuilder::AggrTypeBuilder(bool packed) :
m_offset(0), m_fieldIndex(0), m_packed(packed) m_offset(0), m_fieldIndex(0), m_overallAlignment(0), m_packed(packed)
{ {
m_defaultTypes.reserve(32); m_defaultTypes.reserve(32);
} }
@ -192,6 +192,8 @@ void AggrTypeBuilder::addAggregate(AggregateDeclaration *ad)
void AggrTypeBuilder::alignCurrentOffset(unsigned alignment) void AggrTypeBuilder::alignCurrentOffset(unsigned alignment)
{ {
m_overallAlignment = std::max(alignment, m_overallAlignment);
unsigned aligned = (m_offset + alignment - 1) & ~(alignment - 1); unsigned aligned = (m_offset + alignment - 1) & ~(alignment - 1);
if (m_offset < aligned) { if (m_offset < aligned) {
m_fieldIndex += add_zeros(m_defaultTypes, m_offset, aligned); m_fieldIndex += add_zeros(m_defaultTypes, m_offset, aligned);

View file

@ -43,11 +43,13 @@ public:
unsigned currentFieldIndex() const { return m_fieldIndex; } unsigned currentFieldIndex() const { return m_fieldIndex; }
std::vector<llvm::Type*> defaultTypes() const { return m_defaultTypes; } std::vector<llvm::Type*> defaultTypes() const { return m_defaultTypes; }
VarGEPIndices varGEPIndices() const { return m_varGEPIndices; } VarGEPIndices varGEPIndices() const { return m_varGEPIndices; }
unsigned overallAlignment() const { return m_overallAlignment; }
protected: protected:
std::vector<llvm::Type*> m_defaultTypes; std::vector<llvm::Type*> m_defaultTypes;
VarGEPIndices m_varGEPIndices; VarGEPIndices m_varGEPIndices;
unsigned m_offset; unsigned m_offset;
unsigned m_fieldIndex; unsigned m_fieldIndex;
unsigned m_overallAlignment;
bool m_packed; bool m_packed;
}; };