From aab87fe0ec98072580586850ed00bac3d00df6c0 Mon Sep 17 00:00:00 2001 From: Rafael Espindola Date: Tue, 25 Feb 2014 20:01:08 +0000 Subject: Store a DataLayout in Module. Now that DataLayout is not a pass, store one in Module. Since the C API expects to be able to get a char* to the datalayout description, we have to keep a std::string somewhere. This patch keeps it in Module and also uses it to represent modules without a DataLayout. Once DataLayout is mandatory, we should probably move the string to DataLayout itself since it won't be necessary anymore to represent the special case of a module without a DataLayout. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@202190 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/IR/Module.h | 23 +++++++++++++++++------ lib/Bitcode/Writer/BitcodeWriter.cpp | 6 +++--- lib/IR/AsmWriter.cpp | 5 +++-- lib/IR/Core.cpp | 2 +- lib/IR/DataLayout.cpp | 8 +++++++- lib/IR/Module.cpp | 28 ++++++++++++++++++++++++++-- lib/Linker/LinkModules.cpp | 4 ++-- test/CodeGen/X86/pr1462.ll | 3 +-- tools/opt/opt.cpp | 7 ++----- 9 files changed, 62 insertions(+), 24 deletions(-) diff --git a/include/llvm/IR/Module.h b/include/llvm/IR/Module.h index b965c9ee56..d489bd6c0c 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 1d763d66dd..718cd12389 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 c48214cc2f..d414f764d3 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 68bc5c5810..b19fbe3fda 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 44410ceb68..d60c79f52e 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 d911c7e2b5..739f8888f9 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 1bfc8284b8..f1b8cb7610 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 62549a5035..3aa18609d4 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 19adf78cd3..2b209d99c7 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) -- cgit v1.2.3