diff --git a/include/llvm/IR/Module.h b/include/llvm/IR/Module.h index b965c9ee565..d489bd6c0cf 100644 --- a/include/llvm/IR/Module.h +++ b/include/llvm/IR/Module.h @@ -16,6 +16,7 @@ #define LLVM_IR_MODULE_H #include "llvm/ADT/OwningPtr.h" +#include "llvm/IR/DataLayout.h" #include "llvm/IR/Function.h" #include "llvm/IR/GlobalAlias.h" #include "llvm/IR/GlobalVariable.h" @@ -199,9 +200,16 @@ private: OwningPtr Materializer; ///< Used to materialize GlobalValues std::string ModuleID; ///< Human readable identifier for the module std::string TargetTriple; ///< Platform target triple Module compiled on - std::string DataLayout; ///< Target data description void *NamedMDSymTab; ///< NamedMDNode names. + // We need to keep the string because the C API expects us to own the string + // representation. + // Since we have it, we also use an empty string to represent a module without + // a DataLayout. If it has a DataLayout, these variables are in sync and the + // string is just a cache of getDataLayout()->getStringRepresentation(). + std::string DataLayoutStr; + DataLayout DL; + friend class Constant; /// @} @@ -222,10 +230,12 @@ public: /// @returns the module identifier as a string const std::string &getModuleIdentifier() const { return ModuleID; } - /// Get the data layout string for the module's target platform. This encodes - /// the type sizes and alignments expected by this module. - /// @returns the data layout as a string - const std::string &getDataLayout() const { return DataLayout; } + /// Get the data layout string for the module's target platform. This is + /// equivalent to getDataLayout()->getStringRepresentation(). + const std::string &getDataLayoutStr() const { return DataLayoutStr; } + + /// Get the data layout for the module's target platform. + const DataLayout *getDataLayout() const; /// Get the target triple which is a string describing the target host. /// @returns a string containing the target triple. @@ -247,7 +257,8 @@ public: void setModuleIdentifier(StringRef ID) { ModuleID = ID; } /// Set the data layout - void setDataLayout(StringRef DL) { DataLayout = DL; } + void setDataLayout(StringRef Desc); + void setDataLayout(const DataLayout *Other); /// Set the target triple. void setTargetTriple(StringRef T) { TargetTriple = T; } diff --git a/lib/Bitcode/Writer/BitcodeWriter.cpp b/lib/Bitcode/Writer/BitcodeWriter.cpp index 1d763d66ddc..718cd123898 100644 --- a/lib/Bitcode/Writer/BitcodeWriter.cpp +++ b/lib/Bitcode/Writer/BitcodeWriter.cpp @@ -530,9 +530,9 @@ static void WriteModuleInfo(const Module *M, const ValueEnumerator &VE, if (!M->getTargetTriple().empty()) WriteStringRecord(bitc::MODULE_CODE_TRIPLE, M->getTargetTriple(), 0/*TODO*/, Stream); - if (!M->getDataLayout().empty()) - WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, M->getDataLayout(), - 0/*TODO*/, Stream); + const std::string &DL = M->getDataLayoutStr(); + if (!DL.empty()) + WriteStringRecord(bitc::MODULE_CODE_DATALAYOUT, DL, 0 /*TODO*/, Stream); if (!M->getModuleInlineAsm().empty()) WriteStringRecord(bitc::MODULE_CODE_ASM, M->getModuleInlineAsm(), 0/*TODO*/, Stream); diff --git a/lib/IR/AsmWriter.cpp b/lib/IR/AsmWriter.cpp index c48214cc2fa..d414f764d33 100644 --- a/lib/IR/AsmWriter.cpp +++ b/lib/IR/AsmWriter.cpp @@ -1252,8 +1252,9 @@ void AssemblyWriter::printModule(const Module *M) { M->getModuleIdentifier().find('\n') == std::string::npos) Out << "; ModuleID = '" << M->getModuleIdentifier() << "'\n"; - if (!M->getDataLayout().empty()) - Out << "target datalayout = \"" << M->getDataLayout() << "\"\n"; + const std::string &DL = M->getDataLayoutStr(); + if (!DL.empty()) + Out << "target datalayout = \"" << DL << "\"\n"; if (!M->getTargetTriple().empty()) Out << "target triple = \"" << M->getTargetTriple() << "\"\n"; diff --git a/lib/IR/Core.cpp b/lib/IR/Core.cpp index 68bc5c5810e..b19fbe3fda0 100644 --- a/lib/IR/Core.cpp +++ b/lib/IR/Core.cpp @@ -107,7 +107,7 @@ void LLVMDisposeModule(LLVMModuleRef M) { /*--.. Data layout .........................................................--*/ const char * LLVMGetDataLayout(LLVMModuleRef M) { - return unwrap(M)->getDataLayout().c_str(); + return unwrap(M)->getDataLayoutStr().c_str(); } void LLVMSetDataLayout(LLVMModuleRef M, const char *Triple) { diff --git a/lib/IR/DataLayout.cpp b/lib/IR/DataLayout.cpp index 44410ceb684..d60c79f52e8 100644 --- a/lib/IR/DataLayout.cpp +++ b/lib/IR/DataLayout.cpp @@ -344,7 +344,13 @@ void DataLayout::parseSpecifier(StringRef Desc) { } } -DataLayout::DataLayout(const Module *M) { init(M->getDataLayout()); } +DataLayout::DataLayout(const Module *M) { + const DataLayout *Other = M->getDataLayout(); + if (Other) + *this = *Other; + else + init(""); +} void DataLayout::setAlignment(AlignTypeEnum align_type, unsigned abi_align, diff --git a/lib/IR/Module.cpp b/lib/IR/Module.cpp index d911c7e2b5b..739f8888f96 100644 --- a/lib/IR/Module.cpp +++ b/lib/IR/Module.cpp @@ -42,8 +42,8 @@ template class llvm::SymbolTableListTraits; // Primitive Module methods. // -Module::Module(StringRef MID, LLVMContext& C) - : Context(C), Materializer(NULL), ModuleID(MID) { +Module::Module(StringRef MID, LLVMContext &C) + : Context(C), Materializer(NULL), ModuleID(MID), DL("") { ValSymTab = new ValueSymbolTable(); NamedMDSymTab = new StringMap(); Context.addModule(this); @@ -338,6 +338,30 @@ void Module::addModuleFlag(MDNode *Node) { getOrInsertModuleFlagsMetadata()->addOperand(Node); } +void Module::setDataLayout(StringRef Desc) { + if (Desc.empty()) { + DataLayoutStr = ""; + } else { + DL.init(Desc); + DataLayoutStr = DL.getStringRepresentation(); + } +} + +void Module::setDataLayout(const DataLayout *Other) { + if (!Other) { + DataLayoutStr = ""; + } else { + DL = *Other; + DataLayoutStr = DL.getStringRepresentation(); + } +} + +const DataLayout *Module::getDataLayout() const { + if (DataLayoutStr.empty()) + return 0; + return &DL; +} + //===----------------------------------------------------------------------===// // Methods to control the materialization of GlobalValues in the Module. // diff --git a/lib/Linker/LinkModules.cpp b/lib/Linker/LinkModules.cpp index 1bfc8284b81..f1b8cb76106 100644 --- a/lib/Linker/LinkModules.cpp +++ b/lib/Linker/LinkModules.cpp @@ -1200,14 +1200,14 @@ bool ModuleLinker::run() { // Inherit the target data from the source module if the destination module // doesn't have one already. - if (DstM->getDataLayout().empty() && !SrcM->getDataLayout().empty()) + if (!DstM->getDataLayout() && SrcM->getDataLayout()) DstM->setDataLayout(SrcM->getDataLayout()); // Copy the target triple from the source to dest if the dest's is empty. if (DstM->getTargetTriple().empty() && !SrcM->getTargetTriple().empty()) DstM->setTargetTriple(SrcM->getTargetTriple()); - if (!SrcM->getDataLayout().empty() && !DstM->getDataLayout().empty() && + if (SrcM->getDataLayout() && DstM->getDataLayout() && SrcM->getDataLayout() != DstM->getDataLayout()) { if (!SuppressWarnings) { errs() << "WARNING: Linking two modules of different data layouts!\n"; diff --git a/test/CodeGen/X86/pr1462.ll b/test/CodeGen/X86/pr1462.ll index 62549a50356..3aa18609d46 100644 --- a/test/CodeGen/X86/pr1462.ll +++ b/test/CodeGen/X86/pr1462.ll @@ -1,8 +1,7 @@ ; RUN: llc < %s ; PR1462 -target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64- -v64:64:64-v128:128:128-a0:0:64" +target datalayout = "e-p:64:64:64-i1:8:8-i8:8:8-i16:16:16-i32:32:32-i64:32:64-f32:32:32-f64:32:64-v64:64:64-v128:128:128-a0:0:64" target triple = "x86_64-unknown-linux-gnu" define hidden i128 @__addvti3(i128 %a1, i128 %b2) { diff --git a/tools/opt/opt.cpp b/tools/opt/opt.cpp index 19adf78cd34..2b209d99c74 100644 --- a/tools/opt/opt.cpp +++ b/tools/opt/opt.cpp @@ -429,11 +429,8 @@ int main(int argc, char **argv) { Passes.add(TLI); // Add an appropriate DataLayout instance for this module. - DataLayout *DL = 0; - const std::string &ModuleDataLayout = M.get()->getDataLayout(); - if (!ModuleDataLayout.empty()) - DL = new DataLayout(ModuleDataLayout); - else if (!DefaultDataLayout.empty()) + const DataLayout *DL = M.get()->getDataLayout(); + if (!DL && !DefaultDataLayout.empty()) DL = new DataLayout(DefaultDataLayout); if (DL)