DataLayout is changed to be managed from single place. v4-128-128 is added to generic DataLayout

This commit is contained in:
Dmitry Babokin
2013-03-23 14:38:51 +04:00
parent 7f0c92eb4d
commit 0af2a13349
4 changed files with 61 additions and 45 deletions

View File

@@ -146,6 +146,11 @@ static const char *supportedCPUs[] = {
Target::Target(const char *arch, const char *cpu, const char *isa, bool pic) : Target::Target(const char *arch, const char *cpu, const char *isa, bool pic) :
m_target(NULL), m_target(NULL),
m_targetMachine(NULL), m_targetMachine(NULL),
#if defined(LLVM_3_1)
m_targetData(NULL),
#else
m_dataLayout(NULL),
#endif
m_valid(false), m_valid(false),
m_isa(SSE2), m_isa(SSE2),
m_arch(""), m_arch(""),
@@ -443,16 +448,37 @@ Target::Target(const char *arch, const char *cpu, const char *isa, bool pic) :
m_targetMachine->setAsmVerbosityDefault(true); m_targetMachine->setAsmVerbosityDefault(true);
// Set is32Bit // Initialize TargetData/DataLayout in 3 steps.
// 1. Get default data layout first
std::string dl_string;
#if defined(LLVM_3_1) #if defined(LLVM_3_1)
const llvm::TargetData *targetData = m_targetMachine->getTargetData(); dl_string = m_targetMachine->getTargetData()->getStringRepresentation();
this->m_is32Bit = (targetData->getPointerSize() == 4);
#else #else
int addressSpace = 0; dl_string = m_targetMachine->getDataLayout()->getStringRepresentation();
const llvm::DataLayout *dataLayout = m_targetMachine->getDataLayout();
this->m_is32Bit = (dataLayout->getPointerSize(addressSpace) == 4);
#endif #endif
// 2. Adjust for generic
if (m_isa == Target::GENERIC) {
// <16 x i1> vectors only need 16 bit / 2 byte alignment, so add
// that to the regular datalayout string for IA..
// For generic-4 target we need to treat <4 x i1> as 128 bit value
// in terms of required memory storage and alignment, as this is
// translated to __m128 type.
dl_string = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
"i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-"
"f80:128:128-n8:16:32:64-S128-v16:16:16-v32:32:32-v4:128:128";
}
// 3. Finally set member data
#if defined(LLVM_3_1)
m_targetData = new llvm::TargetData(dl_string);
#else
m_dataLayout = new llvm::DataLayout(dl_string);
#endif
// Set is32Bit
this->m_is32Bit = (getDataLayout()->getPointerSize() == 4);
#if !defined(LLVM_3_1) && !defined(LLVM_3_2) #if !defined(LLVM_3_1) && !defined(LLVM_3_2)
// This is LLVM 3.3+ feature. // This is LLVM 3.3+ feature.
// Initialize target-specific "target-feature" attribute. // Initialize target-specific "target-feature" attribute.
@@ -603,15 +629,7 @@ Target::SizeOf(llvm::Type *type,
"sizeof_int", insertAtEnd); "sizeof_int", insertAtEnd);
} }
#if defined(LLVM_3_1) uint64_t bitSize = getDataLayout()->getTypeSizeInBits(type);
const llvm::TargetData *td = GetTargetMachine()->getTargetData();
Assert(td != NULL);
uint64_t bitSize = td->getTypeSizeInBits(type);
#else
const llvm::DataLayout *dl = GetTargetMachine()->getDataLayout();
Assert(dl != NULL);
uint64_t bitSize = dl->getTypeSizeInBits(type);
#endif
Assert((bitSize % 8) == 0); Assert((bitSize % 8) == 0);
uint64_t byteSize = bitSize / 8; uint64_t byteSize = bitSize / 8;
@@ -650,15 +668,7 @@ Target::StructOffset(llvm::Type *type, int element,
return NULL; return NULL;
} }
#if defined(LLVM_3_1) const llvm::StructLayout *sl = getDataLayout()->getStructLayout(structType);
const llvm::TargetData *td = GetTargetMachine()->getTargetData();
Assert(td != NULL);
const llvm::StructLayout *sl = td->getStructLayout(structType);
#else
const llvm::DataLayout *dl = GetTargetMachine()->getDataLayout();
Assert(dl != NULL);
const llvm::StructLayout *sl = dl->getStructLayout(structType);
#endif
Assert(sl != NULL); Assert(sl != NULL);
uint64_t offset = sl->getElementOffset(element); uint64_t offset = sl->getElementOffset(element);

19
ispc.h
View File

@@ -72,6 +72,11 @@ namespace llvm {
class BasicBlock; class BasicBlock;
class Constant; class Constant;
class ConstantValue; class ConstantValue;
#if defined(LLVM_3_1)
class TargetData;
#else
class DataLayout;
#endif
class DIBuilder; class DIBuilder;
class DIDescriptor; class DIDescriptor;
class DIFile; class DIFile;
@@ -215,6 +220,14 @@ public:
const llvm::Target *getTarget() const {return m_target;} const llvm::Target *getTarget() const {return m_target;}
// Note the same name of method for 3.1 and 3.2+, this allows
// to reduce number ifdefs on client side.
#if defined(LLVM_3_1)
llvm::TargetData *getDataLayout() const {return m_targetData;}
#else
llvm::DataLayout *getDataLayout() const {return m_dataLayout;}
#endif
/** Reports if Target object has valid state. */ /** Reports if Target object has valid state. */
bool isValid() const {return m_valid;} bool isValid() const {return m_valid;}
@@ -261,6 +274,12 @@ private:
*/ */
llvm::TargetMachine *m_targetMachine; llvm::TargetMachine *m_targetMachine;
#if defined(LLVM_3_1)
llvm::TargetData *m_targetData;
#else
llvm::DataLayout *m_dataLayout;
#endif
/** flag to report invalid state after construction /** flag to report invalid state after construction
(due to bad parameters passed to constructor). */ (due to bad parameters passed to constructor). */
bool m_valid; bool m_valid;

View File

@@ -257,14 +257,8 @@ Module::Module(const char *fn) {
module = new llvm::Module(filename ? filename : "<stdin>", *g->ctx); module = new llvm::Module(filename ? filename : "<stdin>", *g->ctx);
module->setTargetTriple(g->target->GetTripleString()); module->setTargetTriple(g->target->GetTripleString());
if (g->target->getISA() == Target::GENERIC) { // DataLayout information supposed to be managed in single place in Target class.
// <16 x i1> vectors only need 16 bit / 2 byte alignment, so add module->setDataLayout(g->target->getDataLayout()->getStringRepresentation());
// that to the regular datalayout string for IA..
std::string datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-"
"i64:64:64-f32:32:32-f64:64:64-v64:64:64-v128:128:128-a0:0:64-s0:64:64-"
"f80:128:128-n8:16:32:64-S128-v16:16:16-v32:32:32";
module->setDataLayout(datalayout);
}
if (g->generateDebuggingSymbols) { if (g->generateDebuggingSymbols) {
diBuilder = new llvm::DIBuilder(*module); diBuilder = new llvm::DIBuilder(*module);
@@ -1060,15 +1054,9 @@ Module::writeObjectFileOrAssembly(llvm::TargetMachine *targetMachine,
llvm::PassManager pm; llvm::PassManager pm;
#if defined(LLVM_3_1) #if defined(LLVM_3_1)
if (const llvm::TargetData *td = targetMachine->getTargetData()) pm.add(new llvm::TargetData(*g->target->getDataLayout()));
pm.add(new llvm::TargetData(*td));
else
pm.add(new llvm::TargetData(module));
#else #else
if (const llvm::DataLayout *dl = targetMachine->getDataLayout()) pm.add(new llvm::DataLayout(*g->target->getDataLayout()));
pm.add(new llvm::DataLayout(*dl));
else
pm.add(new llvm::DataLayout(module));
#endif #endif
llvm::formatted_raw_ostream fos(of->os()); llvm::formatted_raw_ostream fos(of->os());

View File

@@ -414,14 +414,13 @@ Optimize(llvm::Module *module, int optLevel) {
new llvm::TargetLibraryInfo(llvm::Triple(module->getTargetTriple())); new llvm::TargetLibraryInfo(llvm::Triple(module->getTargetTriple()));
optPM.add(targetLibraryInfo); optPM.add(targetLibraryInfo);
#if defined(LLVM_3_1) #if defined(LLVM_3_1)
optPM.add(new llvm::TargetData(module)); optPM.add(new llvm::TargetData(*g->target->getDataLayout()));
#else #else
optPM.add(new llvm::DataLayout(*g->target->getDataLayout()));
llvm::TargetMachine *targetMachine = g->target->GetTargetMachine(); llvm::TargetMachine *targetMachine = g->target->GetTargetMachine();
if (const llvm::DataLayout *dl = targetMachine->getDataLayout())
optPM.add(new llvm::DataLayout(*dl));
else
optPM.add(new llvm::DataLayout(module));
#ifdef LLVM_3_2 #ifdef LLVM_3_2
optPM.add(new llvm::TargetTransformInfo(targetMachine->getScalarTargetTransformInfo(), optPM.add(new llvm::TargetTransformInfo(targetMachine->getScalarTargetTransformInfo(),
targetMachine->getVectorTargetTransformInfo())); targetMachine->getVectorTargetTransformInfo()));