[llvm-commits] [dragonegg] r102899 - /dragonegg/trunk/llvm-backend.cpp

Duncan Sands baldrick at free.fr
Mon May 3 04:32:05 PDT 2010


Author: baldrick
Date: Mon May  3 06:32:05 2010
New Revision: 102899

URL: http://llvm.org/viewvc/llvm-project?rev=102899&view=rev
Log:
Reorganize initialization code in an attempt to make it
more logical.  No intended functionality change.

Modified:
    dragonegg/trunk/llvm-backend.cpp

Modified: dragonegg/trunk/llvm-backend.cpp
URL: http://llvm.org/viewvc/llvm-project/dragonegg/trunk/llvm-backend.cpp?rev=102899&r1=102898&r2=102899&view=diff
==============================================================================
--- dragonegg/trunk/llvm-backend.cpp (original)
+++ dragonegg/trunk/llvm-backend.cpp Mon May  3 06:32:05 2010
@@ -427,43 +427,15 @@
   Args.push_back(0);  // Null terminator.
   int pseudo_argc = Args.size()-1;
   llvm::cl::ParseCommandLineOptions(pseudo_argc, const_cast<char**>(&Args[0]));
-}
-
-/// LazilyInitializeModule - Create a module to output LLVM IR to, if it wasn't
-/// already created.
-static void LazilyInitializeModule(void) {
-  static bool Initialized = false;
-  if (Initialized)
-    return;
-
-  ConfigureLLVM();
-
-  TheModule = new Module("", getGlobalContext());
-
-  if (main_input_filename)
-    TheModule->setModuleIdentifier(main_input_filename);
-
-  // Insert a special .ident directive to identify the version of the plugin
-  // which compiled this code.  The format of the .ident string is patterned
-  // after the ones produced by GCC.
-#ifdef IDENT_ASM_OP
-  if (!flag_no_ident) {
-    const char *pkg_version = "(GNU) ";
 
-    if (strcmp ("(GCC) ", pkgversion_string))
-      pkg_version = pkgversion_string;
-
-    std::string IdentString = IDENT_ASM_OP;
-    IdentString += "\"GCC: ";
-    IdentString += pkg_version;
-    IdentString += version_string;
-    IdentString += " LLVM: ";
-    IdentString += REVISION;
-    IdentString += "\"";
-    TheModule->setModuleInlineAsm(IdentString);
-  }
-#endif
+  if (optimize)
+    RegisterRegAlloc::setDefault(createLinearScanRegisterAllocator);
+  else
+    RegisterRegAlloc::setDefault(createLocalRegisterAllocator);
+}
 
+/// ComputeTargetTriple - Determine the target triple to use.
+static std::string ComputeTargetTriple() {
   // If the target wants to override the architecture, e.g. turning
   // powerpc-darwin-... into powerpc64-darwin-... when -m64 is enabled, do so
   // now.
@@ -483,11 +455,11 @@
   if (OverRidden)
     TargetTriple = std::string(NewTriple);
 #endif
-  TheModule->setTargetTriple(TargetTriple);
-
-  TheTypeConverter = new TypeConverter();
+  return TargetTriple;
+}
 
-  // Create the TargetMachine we will be generating code with.
+/// CreateTargetMachine - Create the TargetMachine we will generate code with.
+static void CreateTargetMachine(const std::string &TargetTriple) {
   // FIXME: Figure out how to select the target and pass down subtarget info.
   std::string Err;
   const Target *TME =
@@ -506,18 +478,61 @@
 #endif
   TheTarget = TME->createTargetMachine(TargetTriple, FeatureStr);
   assert(TheTarget->getTargetData()->isBigEndian() == BYTES_BIG_ENDIAN);
+}
 
-  TheFolder = new TargetFolder(TheTarget->getTargetData());
+/// CreateModule - Create and initialize a module to output LLVM IR to.
+static void CreateModule(const std::string &TargetTriple) {
+  // Create the module itself.
+  StringRef ModuleID = main_input_filename ? main_input_filename : "";
+  TheModule = new Module(ModuleID, getGlobalContext());
+
+  // Insert a special .ident directive to identify the version of the plugin
+  // which compiled this code.  The format of the .ident string is patterned
+  // after the ones produced by GCC.
+#ifdef IDENT_ASM_OP
+  if (!flag_no_ident) {
+    const char *pkg_version = "(GNU) ";
+
+    if (strcmp ("(GCC) ", pkgversion_string))
+      pkg_version = pkgversion_string;
+
+    std::string IdentString = IDENT_ASM_OP;
+    IdentString += "\"GCC: ";
+    IdentString += pkg_version;
+    IdentString += version_string;
+    IdentString += " LLVM: ";
+    IdentString += REVISION;
+    IdentString += "\"";
+    TheModule->setModuleInlineAsm(IdentString);
+  }
+#endif
 
-  // Install information about target datalayout stuff into the module for
-  // optimizer use.
+  // Install information about the target triple and data layout into the module
+  // for optimizer use.
+  TheModule->setTargetTriple(TargetTriple);
   TheModule->setDataLayout(TheTarget->getTargetData()->
                            getStringRepresentation());
+}
 
-  if (optimize)
-    RegisterRegAlloc::setDefault(createLinearScanRegisterAllocator);
-  else
-    RegisterRegAlloc::setDefault(createLocalRegisterAllocator);
+/// InitializeBackend - Initialize the GCC to LLVM conversion machinery.
+/// Can safely be called multiple times.
+static void InitializeBackend(void) {
+  static bool Initialized = false;
+  if (Initialized)
+    return;
+
+  // Initialize and configure LLVM.
+  ConfigureLLVM();
+
+  // Create the target machine to generate code for.
+  const std::string TargetTriple = ComputeTargetTriple();
+  CreateTargetMachine(TargetTriple);
+
+  // Create a module to hold the generated LLVM IR.
+  CreateModule(TargetTriple);
+
+  TheTypeConverter = new TypeConverter();
+  TheFolder = new TargetFolder(TheTarget->getTargetData());
 
   if (debug_info_level > DINFO_LEVEL_NONE)
     TheDebugInfo = new DebugInfo(TheModule);
@@ -1837,7 +1852,7 @@
   if (errorcount || sorrycount)
     return; // Do not process broken code.
 
-  LazilyInitializeModule();
+  InitializeBackend();
 
   // Visit each function with a body, outputting it only once (the same function
   // can appear in multiple cgraph nodes due to cloning).
@@ -1900,7 +1915,7 @@
   if (errorcount || sorrycount)
     return; // Do not process broken code.
 
-  LazilyInitializeModule();
+  InitializeBackend();
 
   // Output all externally visible global variables, whether they are used in
   // this compilation unit or not, as well as any internal variables explicitly
@@ -1995,7 +2010,7 @@
   if (!quiet_flag)
     errs() << "Finishing compilation unit\n";
 
-  LazilyInitializeModule();
+  InitializeBackend();
 
 //TODO  timevar_push(TV_LLVM_PERFILE);
   LLVMContext &Context = getGlobalContext();





More information about the llvm-commits mailing list