Use DataLayout from the module when easily available.
authorRafael Espindola <rafael.espindola@gmail.com>
Tue, 25 Feb 2014 23:25:17 +0000 (23:25 +0000)
committerRafael Espindola <rafael.espindola@gmail.com>
Tue, 25 Feb 2014 23:25:17 +0000 (23:25 +0000)
Eventually DataLayoutPass should go away, but for now that is the only easy
way to get a DataLayout in some APIs. This patch only changes the ones that
have easy access to a Module.

One interesting issue with sometimes using DataLayoutPass and sometimes
fetching it from the Module is that we have to make sure they are equivalent.
We can get most of the way there by always constructing the pass with a Module.
In fact, the pass could be changed to point to an external DataLayout instead
of owning one to make this stricter.

Unfortunately, the C api passes a DataLayout, so it has to be up to the caller
to make sure the pass and the module are in sync.

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

22 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/Analysis/InlineCost.h
include/llvm/IR/BasicBlock.h
include/llvm/IR/DataLayout.h
include/llvm/IR/GlobalValue.h
include/llvm/IR/Instruction.h
lib/Analysis/IPA/InlineCost.cpp
lib/ExecutionEngine/JIT/JIT.cpp
lib/ExecutionEngine/MCJIT/MCJIT.cpp
lib/IR/BasicBlock.cpp
lib/IR/DataLayout.cpp
lib/IR/Globals.cpp
lib/IR/Instruction.cpp
lib/LTO/LTOCodeGenerator.cpp
lib/Target/Target.cpp
lib/Target/TargetMachineC.cpp
tools/llc/llc.cpp
tools/opt/opt.cpp

index 4b7023faef262e9246ab7794f908bf98a5312750..24e538cacf203edfecdbdee902551b1b10d7717e 100644 (file)
@@ -1976,7 +1976,8 @@ int main(int argc, char *argv[]) {
     // Set up the optimizer pipeline.
     // Start with registering info about how the
     // target lays out data structures.
-    fpm.add(new llvm::DataLayoutPass(*executionEngine->getDataLayout()));
+    module->setDataLayout(executionEngine->getDataLayout());
+    fpm.add(new llvm::DataLayoutPass(module));
 
     // Optimizations turned on
 #ifdef ADD_OPT_PASSES
index fd94592513ad6119e894515d1285242011167f13..a8f59428c0da02945b3b061916f1f0912a00e6be 100644 (file)
@@ -586,7 +586,8 @@ int main() {
 
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
-  OurFPM.add(new DataLayoutPass(*TheExecutionEngine->getDataLayout()));
+  TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
+  OurFPM.add(new DataLayoutPass(TheModule));
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Do simple "peephole" optimizations and bit-twiddling optzns.
index 3f3f0a019c6ff180606e2b211726eb85e6a93030..a31b5b4792afb1978d04e269a04dab176a29a486 100644 (file)
@@ -831,7 +831,8 @@ int main() {
 
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
-  OurFPM.add(new DataLayoutPass(*TheExecutionEngine->getDataLayout()));
+  TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
+  OurFPM.add(new DataLayoutPass(TheModule));
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Do simple "peephole" optimizations and bit-twiddling optzns.
index c45292251d3bbb6f370077333a65a73c104d585e..5a3bd2e314749a2ee8686a1c6354b61bcc5d4cee 100644 (file)
@@ -949,7 +949,8 @@ int main() {
 
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
-  OurFPM.add(new DataLayoutPass(*TheExecutionEngine->getDataLayout()));
+  TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
+  OurFPM.add(new DataLayoutPass(TheModule));
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Do simple "peephole" optimizations and bit-twiddling optzns.
index 48d24c0508d28572e4d867d1c9272a3853bcb3a6..c2c337c900880565748a7ae97951ef1337e1f1b6 100644 (file)
@@ -1113,7 +1113,8 @@ int main() {
 
   // Set up the optimizer pipeline.  Start with registering info about how the
   // target lays out data structures.
-  OurFPM.add(new DataLayoutPass(*TheExecutionEngine->getDataLayout()));
+  TheModule->setDataLayout(TheExecutionEngine->getDataLayout());
+  OurFPM.add(new DataLayoutPass(TheModule));
   // Provide basic AliasAnalysis support for GVN.
   OurFPM.add(createBasicAliasAnalysisPass());
   // Promote allocas to registers.
index ab1d500b7323f495edca5317f8bf46db84560a3c..a9d8d313e45ca43fbf64cd805f59b44a4ef2201c 100644 (file)
@@ -99,7 +99,6 @@ public:
 
 /// \brief Cost analyzer used by inliner.
 class InlineCostAnalysis : public CallGraphSCCPass {
-  const DataLayout *DL;
   const TargetTransformInfo *TTI;
 
 public:
index 3bdc95d556f36b571f2572c5bc391e41acafd0ce..1adc254a61ef7db85d57aa0333a3528ce8ec5fe5 100644 (file)
@@ -116,6 +116,8 @@ public:
   const Function *getParent() const { return Parent; }
         Function *getParent()       { return Parent; }
 
+  const DataLayout *getDataLayout() const;
+
   /// \brief Returns the terminator instruction if the block is well formed or
   /// null if the block is not well formed.
   TerminatorInst *getTerminator();
index 046553dc1a27c2b9060ee1ecc8df66b12a3578f7..5bdfc4987514df545948ddf0069567bfa084bf32 100644 (file)
@@ -460,10 +460,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(StringRef LayoutDescription);
-
   explicit DataLayoutPass(const Module *M);
 
   static char ID; // Pass identification, replacement for typeid
index f0c80673be167f4cd0a993ff34493f435455465e..32108b153cd8bda4323a18655e7c2410a49a1f10 100644 (file)
@@ -300,6 +300,8 @@ public:
   inline Module *getParent() { return Parent; }
   inline const Module *getParent() const { return Parent; }
 
+  const DataLayout *getDataLayout() const;
+
   // Methods for support type inquiry through isa, cast, and dyn_cast:
   static inline bool classof(const Value *V) {
     return V->getValueID() == Value::FunctionVal ||
index 0a245483ff762e3458acfaf14c8a49e88c6be17f..8ba8b9c4f1afb74828cb3dbe894769a3c1e9d412 100644 (file)
@@ -53,6 +53,8 @@ public:
   inline const BasicBlock *getParent() const { return Parent; }
   inline       BasicBlock *getParent()       { return Parent; }
 
+  const DataLayout *getDataLayout() const;
+
   /// removeFromParent - This method unlinks 'this' from the containing basic
   /// block, but does not delete it.
   ///
index 53faae5c6711dee5193f596043c5c03d7a4dc7e9..6d700b9db8b7f7cfe17669749e78284709ac7033 100644 (file)
@@ -399,6 +399,7 @@ bool CallAnalyzer::visitBitCast(BitCastInst &I) {
 }
 
 bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
+  const DataLayout *DL = I.getDataLayout();
   // Propagate constants through ptrtoint.
   Constant *COp = dyn_cast<Constant>(I.getOperand(0));
   if (!COp)
@@ -435,6 +436,7 @@ bool CallAnalyzer::visitPtrToInt(PtrToIntInst &I) {
 }
 
 bool CallAnalyzer::visitIntToPtr(IntToPtrInst &I) {
+  const DataLayout *DL = I.getDataLayout();
   // Propagate constants through ptrtoint.
   Constant *COp = dyn_cast<Constant>(I.getOperand(0));
   if (!COp)
@@ -1203,7 +1205,7 @@ INITIALIZE_PASS_END(InlineCostAnalysis, "inline-cost", "Inline Cost Analysis",
 
 char InlineCostAnalysis::ID = 0;
 
-InlineCostAnalysis::InlineCostAnalysis() : CallGraphSCCPass(ID), DL(0) {}
+InlineCostAnalysis::InlineCostAnalysis() : CallGraphSCCPass(ID) {}
 
 InlineCostAnalysis::~InlineCostAnalysis() {}
 
@@ -1214,8 +1216,6 @@ void InlineCostAnalysis::getAnalysisUsage(AnalysisUsage &AU) const {
 }
 
 bool InlineCostAnalysis::runOnSCC(CallGraphSCC &SCC) {
-  DataLayoutPass *DLP = getAnalysisIfAvailable<DataLayoutPass>();
-  DL = DLP ? &DLP->getDataLayout() : 0;
   TTI = &getAnalysis<TargetTransformInfo>();
   return false;
 }
@@ -1273,7 +1273,7 @@ InlineCost InlineCostAnalysis::getInlineCost(CallSite CS, Function *Callee,
   DEBUG(llvm::dbgs() << "      Analyzing call of " << Callee->getName()
         << "...\n");
 
-  CallAnalyzer CA(DL, *TTI, *Callee, Threshold);
+  CallAnalyzer CA(Callee->getDataLayout(), *TTI, *Callee, Threshold);
   bool ShouldInline = CA.analyzeCall(CS);
 
   DEBUG(CA.dump());
index 2836218d4a4da2fc43a00381b3b0dc116bb9b258..b271618a5ffaf1680a17b732d878436d4bd637a2 100644 (file)
@@ -26,6 +26,7 @@
 #include "llvm/IR/Function.h"
 #include "llvm/IR/GlobalVariable.h"
 #include "llvm/IR/Instructions.h"
+#include "llvm/IR/Module.h"
 #include "llvm/Support/Dwarf.h"
 #include "llvm/Support/DynamicLibrary.h"
 #include "llvm/Support/ErrorHandling.h"
@@ -151,7 +152,8 @@ JIT::JIT(Module *M, TargetMachine &tm, TargetJITInfo &tji,
   // Add target data
   MutexGuard locked(lock);
   FunctionPassManager &PM = jitstate->getPM(locked);
-  PM.add(new DataLayoutPass(*TM.getDataLayout()));
+  M->setDataLayout(TM.getDataLayout());
+  PM.add(new DataLayoutPass(M));
 
   // Turn the machine code intermediate representation into bytes in memory that
   // may be executed.
@@ -183,7 +185,8 @@ void JIT::addModule(Module *M) {
     jitstate = new JITState(M);
 
     FunctionPassManager &PM = jitstate->getPM(locked);
-    PM.add(new DataLayoutPass(*TM.getDataLayout()));
+    M->setDataLayout(TM.getDataLayout());
+    PM.add(new DataLayoutPass(M));
 
     // Turn the machine code intermediate representation into bytes in memory
     // that may be executed.
@@ -214,7 +217,8 @@ bool JIT::removeModule(Module *M) {
     jitstate = new JITState(Modules[0]);
 
     FunctionPassManager &PM = jitstate->getPM(locked);
-    PM.add(new DataLayoutPass(*TM.getDataLayout()));
+    M->setDataLayout(TM.getDataLayout());
+    PM.add(new DataLayoutPass(M));
 
     // Turn the machine code intermediate representation into bytes in memory
     // that may be executed.
index ad11a839f0dae6469cc60de20bd5b66d8755206a..a0dfbef51f3302aa8c8a6958d22d12392eba6843 100644 (file)
@@ -142,7 +142,8 @@ ObjectBufferStream* MCJIT::emitObject(Module *M) {
 
   PassManager PM;
 
-  PM.add(new DataLayoutPass(*TM->getDataLayout()));
+  M->setDataLayout(TM->getDataLayout());
+  PM.add(new DataLayoutPass(M));
 
   // The RuntimeDyld will take ownership of this shortly
   OwningPtr<ObjectBufferStream> CompiledObject(new ObjectBufferStream());
index 41e58ec5da2d93b09cdcf25008beb6493d0cdce8..1d7db917915289497dcee1cfda5dc55ead545305 100644 (file)
@@ -30,6 +30,10 @@ ValueSymbolTable *BasicBlock::getValueSymbolTable() {
   return 0;
 }
 
+const DataLayout *BasicBlock::getDataLayout() const {
+  return getParent()->getDataLayout();
+}
+
 LLVMContext &BasicBlock::getContext() const {
   return getType()->getContext();
 }
index ccdaec5e554f465acd806ccd39afe18f027a1e3b..bddb6807512b84dcfc745129d07c912b4e4b5218 100644 (file)
@@ -786,10 +786,6 @@ DataLayoutPass::DataLayoutPass(const DataLayout &DL)
   initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
 }
 
-DataLayoutPass::DataLayoutPass(StringRef Str) : ImmutablePass(ID), DL(Str) {
-  initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
-}
-
 DataLayoutPass::DataLayoutPass(const Module *M) : ImmutablePass(ID), DL(M) {
   initializeDataLayoutPassPass(*PassRegistry::getPassRegistry());
 }
index 5ad96b2ce3af93db7f2dae20da0d5fed477e8fe8..ee882c3eace6df37fe7bfef19d7fc2328b950b96 100644 (file)
@@ -40,6 +40,10 @@ void GlobalValue::Dematerialize() {
   getParent()->Dematerialize(this);
 }
 
+const DataLayout *GlobalValue::getDataLayout() const {
+  return getParent()->getDataLayout();
+}
+
 /// Override destroyConstant to make sure it doesn't get called on
 /// GlobalValue's because they shouldn't be treated like other constants.
 void GlobalValue::destroyConstant() {
index a7773c47168ee849f32895f93aef15940ae464be..fd5bcc904fb47f79a5a1efbd2b28fd06e5a76dff 100644 (file)
@@ -35,6 +35,10 @@ Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
   }
 }
 
+const DataLayout *Instruction::getDataLayout() const {
+  return getParent()->getDataLayout();
+}
+
 Instruction::Instruction(Type *ty, unsigned it, Use *Ops, unsigned NumOps,
                          BasicBlock *InsertAtEnd)
   : User(ty, Value::InstructionVal + it, Ops, NumOps), Parent(0) {
index b18726b98dff797b4d17d2307d7d5b22e6eb9b3a..c5f98736c00fb182cdc4ec0e124c42bd34568097 100644 (file)
@@ -482,7 +482,8 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
   passes.add(createVerifierPass());
 
   // Add an appropriate DataLayout instance for this module...
-  passes.add(new DataLayoutPass(*TargetMach->getDataLayout()));
+  mergedModule->setDataLayout(TargetMach->getDataLayout());
+  passes.add(new DataLayoutPass(mergedModule));
 
   // Add appropriate TargetLibraryInfo for this module.
   passes.add(new TargetLibraryInfo(Triple(TargetMach->getTargetTriple())));
@@ -503,7 +504,7 @@ bool LTOCodeGenerator::generateObjectFile(raw_ostream &out,
 
   PassManager codeGenPasses;
 
-  codeGenPasses.add(new DataLayoutPass(*TargetMach->getDataLayout()));
+  codeGenPasses.add(new DataLayoutPass(mergedModule));
 
   formatted_raw_ostream Out(out);
 
index ee5178184fb9ff03d2a969f2691a787da5af8a3a..627786dfb4968f10c6562ddd282be2e5fbce4a69 100644 (file)
@@ -55,6 +55,8 @@ 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)));
 }
 
index e939b88e0a06f1fd9a3136dbd1bffff89c952d82..a2829d4c027fd9969a87518866118763750ec426 100644 (file)
@@ -212,7 +212,8 @@ static LLVMBool LLVMTargetMachineEmit(LLVMTargetMachineRef T, LLVMModuleRef M,
     *ErrorMessage = strdup(error.c_str());
     return true;
   }
-  pass.add(new DataLayoutPass(*td));
+  Mod->setDataLayout(td);
+  pass.add(new DataLayoutPass(Mod));
 
   TargetMachine::CodeGenFileType ft;
   switch (codegen) {
index 5b311cb6173fa3bae763926e61bef84e86b5a989..bb72f252fe459508bee7b70f753389a3b7c572bd 100644 (file)
@@ -299,9 +299,8 @@ 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.getDataLayout())
-    PM.add(new DataLayoutPass(*DL));
-  else
-    PM.add(new DataLayoutPass(mod));
+    mod->setDataLayout(DL);
+  PM.add(new DataLayoutPass(mod));
 
   // Override default to generate verbose assembly.
   Target.setAsmVerbosityDefault(true);
index 2b209d99c741294683c843e47899a56900d23940..169e648e2ebca8e6679226901c3dd0b3b84e5ce1 100644 (file)
@@ -430,11 +430,13 @@ int main(int argc, char **argv) {
 
   // Add an appropriate DataLayout instance for this module.
   const DataLayout *DL = M.get()->getDataLayout();
-  if (!DL && !DefaultDataLayout.empty())
-    DL = new DataLayout(DefaultDataLayout);
+  if (!DL && !DefaultDataLayout.empty()) {
+    M->setDataLayout(DefaultDataLayout);
+    DL = M.get()->getDataLayout();
+  }
 
   if (DL)
-    Passes.add(new DataLayoutPass(*DL));
+    Passes.add(new DataLayoutPass(M.get()));
 
   Triple ModuleTriple(M->getTargetTriple());
   TargetMachine *Machine = 0;
@@ -450,7 +452,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(*DL));
+      FPasses->add(new DataLayoutPass(M.get()));
     if (TM.get())
       TM->addAnalysisPasses(*FPasses);