Refactor a bit of code into a function, no functionality changes.
[oota-llvm.git] / tools / gccld / GenerateCode.cpp
index 22db5af9a46f062901728cf939e78897cf2314e8..a844ee751a0c0aea4cbf787df451c2f4a2e4e50f 100644 (file)
@@ -25,7 +25,6 @@
 #include "llvm/Transforms/Utils/Linker.h"
 #include "Support/SystemUtils.h"
 #include "Support/CommandLine.h"
-
 using namespace llvm;
 
 namespace {
@@ -40,8 +39,6 @@ namespace {
                        cl::desc("Do not run any optimization passes"));
 }
 
-namespace llvm {
-
 static inline void addPass(PassManager &PM, Pass *P) {
   // Add the pass to the pass manager...
   PM.add(P);
@@ -58,13 +55,10 @@ static inline void addPass(PassManager &PM, Pass *P) {
 ///  Internalize - Flags whether all symbols should be marked internal.
 ///  Out         - Pointer to file stream to which to write the output.
 ///
-/// Outputs:
-///  None.
-///
 /// Returns non-zero value on error.
 ///
-int
-GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) {
+int llvm::GenerateBytecode(Module *M, bool Strip, bool Internalize,
+                           std::ostream *Out) {
   // In addition to just linking the input from GCC, we also want to spiff it up
   // a little bit.  Do this now.
   PassManager Passes;
@@ -72,61 +66,79 @@ GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) {
   if (Verify) Passes.add(createVerifierPass());
 
   // Add an appropriate TargetData instance for this module...
-  addPass (Passes, new TargetData("gccld", M));
+  addPass(Passes, new TargetData("gccld", M));
+
+  // Often if the programmer does not specify proper prototypes for the
+  // functions they are calling, they end up calling a vararg version of the
+  // function that does not get a body filled in (the real function has typed
+  // arguments).  This pass merges the two functions.
+  addPass(Passes, createFunctionResolvingPass());
 
   if (!DisableOptimizations) {
+    if (Internalize) {
+      // Now that composite has been compiled, scan through the module, looking
+      // for a main function.  If main is defined, mark all other functions
+      // internal.
+      addPass(Passes, createInternalizePass());
+    }
+
+    // Now that we internalized some globals, see if we can mark any globals as
+    // being constant!
+    addPass(Passes, createGlobalConstifierPass());
+
     // Linking modules together can lead to duplicated global constants, only
     // keep one copy of each constant...
-    addPass (Passes, createConstantMergePass());
+    addPass(Passes, createConstantMergePass());
 
     // If the -s command line option was specified, strip the symbols out of the
     // resulting program to make it smaller.  -s is a GCC option that we are
     // supporting.
     if (Strip)
-      addPass (Passes, createSymbolStrippingPass());
-
-    // Often if the programmer does not specify proper prototypes for the
-    // functions they are calling, they end up calling a vararg version of the
-    // function that does not get a body filled in (the real function has typed
-    // arguments).  This pass merges the two functions.
-    addPass (Passes, createFunctionResolvingPass());
-
-    if (Internalize) {
-      // Now that composite has been compiled, scan through the module, looking
-      // for a main function.  If main is defined, mark all other functions
-      // internal.
-      addPass (Passes, createInternalizePass());
-    }
+      addPass(Passes, createSymbolStrippingPass());
 
     // Propagate constants at call sites into the functions they call.
-    addPass (Passes, createIPConstantPropagationPass());
+    addPass(Passes, createIPConstantPropagationPass());
 
     // Remove unused arguments from functions...
-    addPass (Passes, createDeadArgEliminationPass());
+    addPass(Passes, createDeadArgEliminationPass());
 
     if (!DisableInline)
-      addPass (Passes, createFunctionInliningPass()); // Inline small functions
+      addPass(Passes, createFunctionInliningPass()); // Inline small functions
+
+    addPass(Passes, createPruneEHPass());            // Remove dead EH info
+    addPass(Passes, createGlobalDCEPass());          // Remove dead functions
+
+    // If we didn't decide to inline a function, check to see if we can
+    // transform it to pass arguments by value instead of by reference.
+    addPass(Passes, createArgumentPromotionPass());
+
+    // The IPO passes may leave cruft around.  Clean up after them.
+    addPass(Passes, createInstructionCombiningPass());
+
+    addPass(Passes, createScalarReplAggregatesPass()); // Break up allocas
 
     // Run a few AA driven optimizations here and now, to cleanup the code.
     // Eventually we should put an IP AA in place here.
 
-    addPass (Passes, createLICMPass());               // Hoist loop invariants
-    addPass (Passes, createLoadValueNumberingPass()); // GVN for load instrs
-    addPass (Passes, createGCSEPass());               // Remove common subexprs
+    addPass(Passes, createLICMPass());               // Hoist loop invariants
+    addPass(Passes, createLoadValueNumberingPass()); // GVN for load instrs
+    addPass(Passes, createGCSEPass());               // Remove common subexprs
 
-    // The FuncResolve pass may leave cruft around if functions were prototyped
-    // differently than they were defined.  Remove this cruft.
-    addPass (Passes, createInstructionCombiningPass());
+    // Cleanup and simplify the code after the scalar optimizations.
+    addPass(Passes, createInstructionCombiningPass());
 
     // Delete basic blocks, which optimization passes may have killed...
-    addPass (Passes, createCFGSimplificationPass());
+    addPass(Passes, createCFGSimplificationPass());
 
     // Now that we have optimized the program, discard unreachable functions...
-    addPass (Passes, createGlobalDCEPass());
+    addPass(Passes, createGlobalDCEPass());
   }
 
+  // Make sure everything is still good.
+  Passes.add(createVerifierPass());
+
   // Add the pass that writes bytecode to the output file...
-  addPass (Passes, new WriteBytecodePass(Out));
+  addPass(Passes, new WriteBytecodePass(Out));
 
   // Run our queue of passes all at once now, efficiently.
   Passes.run(*M);
@@ -143,30 +155,42 @@ GenerateBytecode (Module *M, bool Strip, bool Internalize, std::ostream *Out) {
 ///  llc            - The pathname to use for LLC.
 ///  envp           - The environment to use when running LLC.
 ///
-/// Outputs:
-///  None.
-///
 /// Return non-zero value on error.
 ///
-int
-GenerateAssembly(const std::string &OutputFilename,
-                 const std::string &InputFilename,
-                 const std::string &llc,
-                 char ** const envp)
-{
+int llvm::GenerateAssembly(const std::string &OutputFilename,
+                           const std::string &InputFilename,
+                           const std::string &llc,
+                           char ** const envp) {
   // Run LLC to convert the bytecode file into assembly code.
-  const char *cmd[8];
-
+  const char *cmd[6];
   cmd[0] = llc.c_str();
   cmd[1] = "-f";
   cmd[2] = "-o";
   cmd[3] = OutputFilename.c_str();
   cmd[4] = InputFilename.c_str();
-  cmd[5] = NULL;
+  cmd[5] = 0;
 
   return ExecWait(cmd, envp);
 }
 
+/// GenerateAssembly - generates a native assembly language source file from the
+/// specified bytecode file.
+int llvm::GenerateCFile(const std::string &OutputFile,
+                        const std::string &InputFile,
+                        const std::string &llc, char ** const envp) {
+  // Run LLC to convert the bytecode file into C.
+  const char *cmd[7];
+
+  cmd[0] = llc.c_str();
+  cmd[1] = "-march=c";
+  cmd[2] = "-f";
+  cmd[3] = "-o";
+  cmd[4] = OutputFile.c_str();
+  cmd[5] = InputFile.c_str();
+  cmd[6] = 0;
+  return ExecWait(cmd, envp);
+}
+
 /// GenerateNative - generates a native assembly language source file from the
 /// specified assembly source file.
 ///
@@ -183,13 +207,11 @@ GenerateAssembly(const std::string &OutputFilename,
 ///
 /// Returns non-zero value on error.
 ///
-int
-GenerateNative(const std::string &OutputFilename,
-               const std::string &InputFilename,
-               const std::vector<std::string> &Libraries,
-               const std::vector<std::string> &LibPaths,
-               const std::string &gcc,
-               char ** const envp) {
+int llvm::GenerateNative(const std::string &OutputFilename,
+                         const std::string &InputFilename,
+                         const std::vector<std::string> &Libraries,
+                         const std::vector<std::string> &LibPaths,
+                         const std::string &gcc, char ** const envp) {
   // Remove these environment variables from the environment of the
   // programs that we will execute.  It appears that GCC sets these
   // environment variables so that the programs it uses can configure
@@ -215,6 +237,8 @@ GenerateNative(const std::string &OutputFilename,
   //  and linker because we don't know where to put the _start symbol.
   //  GCC mysteriously knows how to do it.
   cmd.push_back(gcc.c_str());
+  cmd.push_back("-fno-strict-aliasing");
+  cmd.push_back("-O3");
   cmd.push_back("-o");
   cmd.push_back(OutputFilename.c_str());
   cmd.push_back(InputFilename.c_str());
@@ -236,13 +260,13 @@ GenerateNative(const std::string &OutputFilename,
   // Add in the libraries to link.
   std::vector<std::string> Libs(Libraries);
   for (unsigned index = 0; index < Libs.size(); index++) {
-    Libs[index] = "-l" + Libs[index];
-    cmd.push_back(Libs[index].c_str());
+    if (Libs[index] != "crtend") {
+      Libs[index] = "-l" + Libs[index];
+      cmd.push_back(Libs[index].c_str());
+    }
   }
   cmd.push_back(NULL);
 
   // Run the compiler to assembly and link together the program.
   return ExecWait(&(cmd[0]), clean_env);
 }
-
-} // End llvm namespace