Add doInitialization/doFinalization to DataLayoutPass.
authorRafael Espindola <rafael.espindola@gmail.com>
Wed, 10 Sep 2014 21:27:43 +0000 (21:27 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Wed, 10 Sep 2014 21:27:43 +0000 (21:27 +0000)
With this a DataLayoutPass can be reused for multiple modules.

Once we have doInitialization/doFinalization, it doesn't seem necessary to pass
a Module to the constructor.

Overall this change seems in line with the idea of making DataLayout a required
part of Module. With it the only way of having a DataLayout used is to add it
to the Module.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@217548 91177308-0d34-0410-b5e6-96231b3b80d8

17 files changed:
examples/ExceptionDemo/ExceptionDemo.cpp
examples/Kaleidoscope/Chapter4/toy.cpp
examples/Kaleidoscope/Chapter5/toy.cpp
examples/Kaleidoscope/Chapter6/toy.cpp
examples/Kaleidoscope/Chapter7/toy.cpp
include/llvm/IR/DataLayout.h
lib/ExecutionEngine/MCJIT/MCJIT.cpp
lib/IR/DataLayout.cpp
lib/LTO/LTOCodeGenerator.cpp
lib/Target/Target.cpp
lib/Target/TargetMachineC.cpp
lib/Transforms/IPO/PassManagerBuilder.cpp
tools/gold/gold-plugin.cpp
tools/llc/llc.cpp
tools/llvm-extract/llvm-extract.cpp
tools/opt/opt.cpp
unittests/IR/LegacyPassManagerTest.cpp

index e5e2c923d7310a8e7f379971423d67aa9a6f15ce..17076facbca980f819d37cef0af424f9455a09f0 100644 (file)
@@ -1977,7 +1977,7 @@ int main(int argc, char *argv[]) {
     // Start with registering info about how the
     // target lays out data structures.
     module->setDataLayout(executionEngine->getDataLayout());
-    fpm.add(new llvm::DataLayoutPass(module));
+    fpm.add(new llvm::DataLayoutPass());
 
     // Optimizations turned on
 #ifdef ADD_OPT_PASSES
index 9daaaa7466f444fe4618fda9e07bc9d569979bb1..3564d751e65f3e94a7acc0434ccf831cdd91afa9 100644 (file)
@@ -588,7 +588,7 @@ int main() {
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
   TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
-  OurFPM.add(new DataLayoutPass(TheModule));
+  OurFPM.add(new DataLayoutPass());
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Do simple "peephole" optimizations and bit-twiddling optzns.
index 5454b21bece009da93ceb931246d46edd78f7616..4929a20cd7e9a00587f29a21dfcd6fa6353e18a1 100644 (file)
@@ -833,7 +833,7 @@ int main() {
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
   TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
-  OurFPM.add(new DataLayoutPass(TheModule));
+  OurFPM.add(new DataLayoutPass());
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Do simple "peephole" optimizations and bit-twiddling optzns.
index 8977203a719c22a04efaa96b74c046d72e857339..06da9ac682d579536997e34cc935d2226803efc1 100644 (file)
@@ -951,7 +951,7 @@ int main() {
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
   TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
-  OurFPM.add(new DataLayoutPass(TheModule));
+  OurFPM.add(new DataLayoutPass());
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Do simple "peephole" optimizations and bit-twiddling optzns.
index 3550a0d8a5b29738203365beaeca6dcbcd5c1e94..56a6fa96e25c3edcd7d4ec7d034f08ddcee3b049 100644 (file)
@@ -1115,7 +1115,7 @@ int main() {
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
   TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
-  OurFPM.add(new DataLayoutPass(TheModule));
+  OurFPM.add(new DataLayoutPass());
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Promote allocas to registers.
index 877029f92f0b1d6de8b28ed9a9c864275f422225..4ed10e0be9a18b265201ec1df7d8a48fb47d5ff1 100644 (file)
@@ -184,6 +184,8 @@ public:
   /// Initialize target data from properties stored in the module.
   explicit DataLayout(const Module *M);
 
+  void init(const Module *M);
+
   DataLayout(const DataLayout &DL) : LayoutMap(nullptr) { *this = DL; }
 
   DataLayout &operator=(const DataLayout &DL) {
@@ -466,13 +468,10 @@ public:
 
   const DataLayout &getDataLayout() const { return DL; }
 
-  // For use with the C API. C++ code should always use the constructor that
-  // takes a module.
-  explicit DataLayoutPass(const DataLayout &DL);
-
-  explicit DataLayoutPass(const Module *M);
-
   static char ID; // Pass identification, replacement for typeid
+
+  bool doFinalization(Module &M) override;
+  bool doInitialization(Module &M) override;
 };
 
 /// StructLayout - used to lazily calculate structure layout information for a
index 8ff41ffd7d677f51af72dca49822c7ae5043f91a..e441ec8d1a33e3a7d1c966f804495cded640ebe9 100644 (file)
@@ -133,7 +133,7 @@ std::unique_ptr<ObjectBufferStream> MCJIT::emitObject(Module *M) {
   PassManager PM;
 
   M->setDataLayout(TM->getSubtargetImpl()->getDataLayout());
-  PM.add(new DataLayoutPass(M));
+  PM.add(new DataLayoutPass());
 
   // The RuntimeDyld will take ownership of this shortly
   std::unique_ptr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
index dea05fbef4ab426756b06805a643078341c6124c..388735386709cadf1dd107c01d1ca0c2ec36f8ba 100644 (file)
@@ -345,6 +345,10 @@ void DataLayout::parseSpecifier(StringRef Desc) {
 }
 
 DataLayout::DataLayout(const Module *M) : LayoutMap(nullptr) {
+  init(M);
+}
+
+void DataLayout::init(const Module *M) {
   const DataLayout *Other = M->getDataLayout();
   if (Other)
     *this = *Other;
@@ -796,17 +800,17 @@ unsigned DataLayout::getPreferredAlignmentLog(const GlobalVariable *GV) const {
 }
 
 DataLayoutPass::DataLayoutPass() : ImmutablePass(ID), DL("") {
-  report_fatal_error("Bad DataLayoutPass ctor used. Tool did not specify a "
-                     "DataLayout to use?");
+  initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
 }
 
 DataLayoutPass::~DataLayoutPass() {}
 
-DataLayoutPass::DataLayoutPass(const DataLayout &DL)
-    : ImmutablePass(ID), DL(DL) {
-  initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
+bool DataLayoutPass::doInitialization(Module &M) {
+  DL.init(&M);
+  return false;
 }
 
-DataLayoutPass::DataLayoutPass(const Module *M) : ImmutablePass(ID), DL(M) {
-  initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
+bool DataLayoutPass::doFinalization(Module &M) {
+  DL.reset("");
+  return false;
 }
index 491975e3db64bf03be902779fbb724ef8e358a74..1b6f905f0aca1df802cda3c4a82f679516516618 100644 (file)
@@ -471,7 +471,7 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
 
   PassManager codeGenPasses;
 
-  codeGenPasses.add(new DataLayoutPass(mergedModule));
+  codeGenPasses.add(new DataLayoutPass());
 
   formatted_raw_ostream Out(out);
 
index d277f82eb869bd2f983f9bca31a535e1085a4fe0..4b51b3f7eea5a24750f7233132a574f31fa5f00d 100644 (file)
@@ -49,7 +49,7 @@ LLVMTargetDataRef LLVMCreateTargetData(const char *StringRep) {
 void LLVMAddTargetData(LLVMTargetDataRef TD, LLVMPassManagerRef PM) {
   // The DataLayoutPass must now be in sync with the module. Unfortunatelly we
   // cannot enforce that from the C api.
-  unwrap(PM)->add(new DataLayoutPass(*unwrap(TD)));
+  unwrap(PM)->add(new DataLayoutPass());
 }
 
 void LLVMAddTargetLibraryInfo(LLVMTargetLibraryInfoRef TLI,
index 598783b1be11587c1f20f7a0976de6eecb6433cc..b3e07df7b5ba42da9759b8fe160d1527a2a49ceb 100644 (file)
@@ -198,7 +198,7 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
     return true;
   }
   Mod->setDataLayout(td);
-  pass.add(new DataLayoutPass(Mod));
+  pass.add(new DataLayoutPass());
 
   TargetMachine::CodeGenFileType ft;
   switch (codegen) {
index 1b229123233c497069690ad839f2b3f17e7c2bb0..1b9d8c16060a542fcc1de292d2e942973981dd35 100644 (file)
@@ -432,8 +432,7 @@ void PassManagerBuilder::addLTOOptimizationPasses(PassManagerBase &PM) {
 void PassManagerBuilder::populateLTOPassManager(PassManagerBase &PM,
                                                 TargetMachine *TM) {
   if (TM) {
-    const DataLayout *DL = TM->getSubtargetImpl()->getDataLayout();
-    PM.add(new DataLayoutPass(*DL));
+    PM.add(new DataLayoutPass());
     TM->addAnalysisPasses(PM);
   }
 
index e7888bea9e6ae964483210575ea3037cead15df1..abe6e4dd66bc00371a8c717dae11f0783885d7ab 100644 (file)
@@ -693,7 +693,7 @@ static void codegen(Module &M) {
   runLTOPasses(M, *TM);
 
   PassManager CodeGenPasses;
-  CodeGenPasses.add(new DataLayoutPass(&M));
+  CodeGenPasses.add(new DataLayoutPass());
 
   SmallString<128> Filename;
   int FD;
index 0dbe215cef7b430137718792cd09c77aa54e99ba..fe4d9ac4f195d4aff16d194249e69d7494965754 100644 (file)
@@ -307,7 +307,7 @@ static int compileModule(char **argv, LLVMContext &Context) {
   // Add the target data from the target machine, if it exists, or the module.
   if (const DataLayout *DL = Target.getSubtargetImpl()->getDataLayout())
     mod->setDataLayout(DL);
-  PM.add(new DataLayoutPass(mod));
+  PM.add(new DataLayoutPass());
 
   if (RelaxAll.getNumOccurrences() > 0 &&
       FileType != TargetMachine::CGFT_ObjectFile)
index 8da63fb1797ea7b212f042f26dfc44acfdb0b776..116b678e07e1252731c75d4ff73c411135d38754 100644 (file)
@@ -250,7 +250,7 @@ int main(int argc, char **argv) {
   // In addition to deleting all other functions, we also want to spiff it
   // up a little bit.  Do this now.
   PassManager Passes;
-  Passes.add(new DataLayoutPass(M.get())); // Use correct DataLayout
+  Passes.add(new DataLayoutPass()); // Use correct DataLayout
 
   std::vector<GlobalValue*> Gvs(GVs.begin(), GVs.end());
 
index f8172b51207dc42207457ae120cb5abbf123a269..f1a945a60008a1aa306f02382e56438530ff2cba 100644 (file)
@@ -440,7 +440,7 @@ int main(int argc, char **argv) {
   }
 
   if (DL)
-    Passes.add(new DataLayoutPass(M.get()));
+    Passes.add(new DataLayoutPass());
 
   Triple ModuleTriple(M->getTargetTriple());
   TargetMachine *Machine = nullptr;
@@ -456,7 +456,7 @@ int main(int argc, char **argv) {
   if (OptLevelO1 || OptLevelO2 || OptLevelOs || OptLevelOz || OptLevelO3) {
     FPasses.reset(new FunctionPassManager(M.get()));
     if (DL)
-      FPasses->add(new DataLayoutPass(M.get()));
+      FPasses->add(new DataLayoutPass());
     if (TM.get())
       TM->addAnalysisPasses(*FPasses);
 
index 7a2c47a59f937e0ddbca51adcf587e5731ec78ef..4efc2f5b80574d8ede690c263406670383215115 100644 (file)
@@ -303,7 +303,7 @@ namespace llvm {
       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
 
       PassManager Passes;
-      Passes.add(new DataLayoutPass(&M));
+      Passes.add(new DataLayoutPass());
       Passes.add(mNDM2);
       Passes.add(mNDM);
       Passes.add(mNDNM);
@@ -327,7 +327,7 @@ namespace llvm {
       mNDM->run = mNDNM->run = mDNM->run = mNDM2->run = 0;
 
       PassManager Passes;
-      Passes.add(new DataLayoutPass(&M));
+      Passes.add(new DataLayoutPass());
       Passes.add(mNDM);
       Passes.add(mNDNM);
       Passes.add(mNDM2);// invalidates mNDM needed by mDNM
@@ -349,7 +349,7 @@ namespace llvm {
       std::unique_ptr<Module> M(makeLLVMModule());
       T *P = new T();
       PassManager Passes;
-      Passes.add(new DataLayoutPass(M.get()));
+      Passes.add(new DataLayoutPass());
       Passes.add(P);
       Passes.run(*M);
       T::finishedOK(run);
@@ -360,7 +360,7 @@ namespace llvm {
       Module *M = makeLLVMModule();
       T *P = new T();
       PassManager Passes;
-      Passes.add(new DataLayoutPass(M));
+      Passes.add(new DataLayoutPass());
       Passes.add(P);
       Passes.run(*M);
       T::finishedOK(run, N);
@@ -398,7 +398,7 @@ namespace llvm {
         SCOPED_TRACE("Running OnTheFlyTest");
         struct OnTheFlyTest *O = new OnTheFlyTest();
         PassManager Passes;
-        Passes.add(new DataLayoutPass(M));
+        Passes.add(new DataLayoutPass());
         Passes.add(O);
         Passes.run(*M);