Don't performance load/op/store transformation if op produces a floating point
[oota-llvm.git] / lib / Support / CommandLine.cpp
index 6aa6c091292c9c8e28c4ff27c334bbd29a4168d2..57620b50d6c6f8a3a8e81c194616a414a923e53c 100644 (file)
@@ -18,6 +18,7 @@
 
 #include "llvm/Config/config.h"
 #include "llvm/Support/CommandLine.h"
+#include "llvm/System/Path.h"
 #include <algorithm>
 #include <functional>
 #include <map>
 #include <cerrno>
 #include <cstring>
 using namespace llvm;
-
 using namespace cl;
 
+//===----------------------------------------------------------------------===//
+// Template instantiations and anchors.
+//
+TEMPLATE_INSTANTIATION(class basic_parser<bool>);
+TEMPLATE_INSTANTIATION(class basic_parser<int>);
+TEMPLATE_INSTANTIATION(class basic_parser<unsigned>);
+TEMPLATE_INSTANTIATION(class basic_parser<double>);
+TEMPLATE_INSTANTIATION(class basic_parser<float>);
+TEMPLATE_INSTANTIATION(class basic_parser<std::string>);
+
+TEMPLATE_INSTANTIATION(class opt<unsigned>);
+TEMPLATE_INSTANTIATION(class opt<int>);
+TEMPLATE_INSTANTIATION(class opt<std::string>);
+TEMPLATE_INSTANTIATION(class opt<bool>);
+
+void Option::anchor() {}
+void basic_parser_impl::anchor() {}
+void parser<bool>::anchor() {}
+void parser<int>::anchor() {}
+void parser<unsigned>::anchor() {}
+void parser<double>::anchor() {}
+void parser<float>::anchor() {}
+void parser<std::string>::anchor() {}
+
+//===----------------------------------------------------------------------===//
+
 // Globals for name and overview of program
-static const char *ProgramName = "<unknown>";
+static std::string ProgramName = "<premain>";
 static const char *ProgramOverview = 0;
 
 // This collects additional help to be printed.
@@ -46,7 +72,7 @@ extrahelp::extrahelp(const char* Help)
 }
 
 //===----------------------------------------------------------------------===//
-// Basic, shared command line option processing machinery...
+// Basic, shared command line option processing machinery.
 //
 
 // Return the global command line option vector.  Making it a function scoped
@@ -188,8 +214,8 @@ static bool EatsUnboundedNumberOfValues(const Option *O) {
 /// using strdup (), so it is the caller's responsibility to free ()
 /// them later.
 ///
-static void ParseCStringVector (std::vector<char *> &output,
-                                const char *input) {
+static void ParseCStringVector(std::vector<char *> &output,
+                               const char *input) {
   // Characters which will be treated as token separators:
   static const char *delims = " \v\f\t\r\n";
 
@@ -239,26 +265,25 @@ void cl::ParseEnvironmentOptions(const char *progName, const char *envVar,
   assert(envVar && "Environment variable name missing");
 
   // Get the environment variable they want us to parse options out of.
-  const char *envValue = getenv (envVar);
+  const char *envValue = getenv(envVar);
   if (!envValue)
     return;
 
   // Get program's "name", which we wouldn't know without the caller
   // telling us.
-  std::vector<char *> newArgv;
-  newArgv.push_back (strdup (progName));
+  std::vector<char*> newArgv;
+  newArgv.push_back(strdup(progName));
 
   // Parse the value of the environment variable into a "command line"
   // and hand it off to ParseCommandLineOptions().
-  ParseCStringVector (newArgv, envValue);
-  int newArgc = newArgv.size ();
-  ParseCommandLineOptions (newArgc, &newArgv[0], Overview);
+  ParseCStringVector(newArgv, envValue);
+  int newArgc = newArgv.size();
+  ParseCommandLineOptions(newArgc, &newArgv[0], Overview);
 
   // Free all the strdup()ed strings.
-  for (std::vector<char *>::iterator i = newArgv.begin (), e = newArgv.end ();
-       i != e; ++i) {
+  for (std::vector<char*>::iterator i = newArgv.begin(), e = newArgv.end();
+       i != e; ++i)
     free (*i);
-  }
 }
 
 /// LookupOption - Lookup the option specified by the specified option on the
@@ -289,7 +314,8 @@ void cl::ParseCommandLineOptions(int &argc, char **argv,
   assert((!getOpts().empty() || !getPositionalOpts().empty()) &&
          "No options specified, or ParseCommandLineOptions called more"
          " than once!");
-  ProgramName = argv[0];  // Save this away safe and snug
+  sys::Path progname(argv[0]);
+  ProgramName = sys::Path(argv[0]).getLast();
   ProgramOverview = Overview;
   bool ErrorParsing = false;
 
@@ -448,8 +474,8 @@ void cl::ParseCommandLineOptions(int &argc, char **argv,
     }
 
     if (Handler == 0) {
-      std::cerr << ProgramName << ": Unknown command line argument '" << argv[i]
-                << "'.  Try: '" << argv[0] << " --help'\n";
+      std::cerr << ProgramName << ": Unknown command line argument '"
+                  << argv[i] << "'.  Try: '" << argv[0] << " --help'\n";
       ErrorParsing = true;
       continue;
     }
@@ -489,6 +515,7 @@ void cl::ParseCommandLineOptions(int &argc, char **argv,
               << ": Not enough positional command line arguments specified!\n"
               << "Must specify at least " << NumPositionalRequired
               << " positional arguments: See: " << argv[0] << " --help\n";
+    
     ErrorParsing = true;
   } else if (!HasUnlimitedPositionals
              && PositionalVals.size() > PositionalOpts.size()) {
@@ -599,6 +626,7 @@ bool Option::error(std::string Message, const char *ArgName) {
     std::cerr << HelpStr;  // Be nice for positional arguments
   else
     std::cerr << ProgramName << ": for the -" << ArgName;
+  
   std::cerr << " option: " << Message << "\n";
   return true;
 }
@@ -676,10 +704,10 @@ unsigned alias::getOptionWidth() const {
   return std::strlen(ArgStr)+6;
 }
 
-// Print out the option for the alias...
+// Print out the option for the alias.
 void alias::printOptionInfo(unsigned GlobalWidth) const {
   unsigned L = std::strlen(ArgStr);
-  std::cerr << "  -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
+  std::cout << "  -" << ArgStr << std::string(GlobalWidth-L-6, ' ') << " - "
             << HelpStr << "\n";
 }
 
@@ -706,12 +734,12 @@ unsigned basic_parser_impl::getOptionWidth(const Option &O) const {
 //
 void basic_parser_impl::printOptionInfo(const Option &O,
                                         unsigned GlobalWidth) const {
-  std::cerr << "  -" << O.ArgStr;
+  std::cout << "  -" << O.ArgStr;
 
   if (const char *ValName = getValueName())
-    std::cerr << "=<" << getValueStr(O, ValName) << ">";
+    std::cout << "=<" << getValueStr(O, ValName) << ">";
 
-  std::cerr << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
+  std::cout << std::string(GlobalWidth-getOptionWidth(O), ' ') << " - "
             << O.HelpStr << "\n";
 }
 
@@ -828,20 +856,20 @@ void generic_parser_base::printOptionInfo(const Option &O,
                                           unsigned GlobalWidth) const {
   if (O.hasArgStr()) {
     unsigned L = std::strlen(O.ArgStr);
-    std::cerr << "  -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
+    std::cout << "  -" << O.ArgStr << std::string(GlobalWidth-L-6, ' ')
               << " - " << O.HelpStr << "\n";
 
     for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
       unsigned NumSpaces = GlobalWidth-strlen(getOption(i))-8;
-      std::cerr << "    =" << getOption(i) << std::string(NumSpaces, ' ')
+      std::cout << "    =" << getOption(i) << std::string(NumSpaces, ' ')
                 << " - " << getDescription(i) << "\n";
     }
   } else {
     if (O.HelpStr[0])
-      std::cerr << "  " << O.HelpStr << "\n";
+      std::cout << "  " << O.HelpStr << "\n";
     for (unsigned i = 0, e = getNumOptions(); i != e; ++i) {
       unsigned L = std::strlen(getOption(i));
-      std::cerr << "    -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
+      std::cout << "    -" << getOption(i) << std::string(GlobalWidth-L-8, ' ')
                 << " - " << getDescription(i) << "\n";
     }
   }
@@ -895,9 +923,9 @@ public:
     }
 
     if (ProgramOverview)
-      std::cerr << "OVERVIEW:" << ProgramOverview << "\n";
+      std::cout << "OVERVIEW:" << ProgramOverview << "\n";
 
-    std::cerr << "USAGE: " << ProgramName << " [options]";
+    std::cout << "USAGE: " << ProgramName << " [options]";
 
     // Print out the positional options...
     std::vector<Option*> &PosOpts = getPositionalOpts();
@@ -907,28 +935,28 @@ public:
 
     for (unsigned i = CAOpt != 0, e = PosOpts.size(); i != e; ++i) {
       if (PosOpts[i]->ArgStr[0])
-        std::cerr << " --" << PosOpts[i]->ArgStr;
-      std::cerr << " " << PosOpts[i]->HelpStr;
+        std::cout << " --" << PosOpts[i]->ArgStr;
+      std::cout << " " << PosOpts[i]->HelpStr;
     }
 
     // Print the consume after option info if it exists...
-    if (CAOpt) std::cerr << " " << CAOpt->HelpStr;
+    if (CAOpt) std::cout << " " << CAOpt->HelpStr;
 
-    std::cerr << "\n\n";
+    std::cout << "\n\n";
 
     // Compute the maximum argument length...
     MaxArgLen = 0;
     for (unsigned i = 0, e = Options.size(); i != e; ++i)
       MaxArgLen = std::max(MaxArgLen, Options[i].second->getOptionWidth());
 
-    std::cerr << "OPTIONS:\n";
+    std::cout << "OPTIONS:\n";
     for (unsigned i = 0, e = Options.size(); i != e; ++i)
       Options[i].second->printOptionInfo(MaxArgLen);
 
     // Print any extra help the user has declared.
     for (std::vector<const char *>::iterator I = MoreHelp().begin(),
           E = MoreHelp().end(); I != E; ++I)
-      std::cerr << *I;
+      std::cout << *I;
     MoreHelp().clear();
 
     // Halt the program since help information was printed
@@ -937,24 +965,6 @@ public:
   }
 };
 
-class VersionPrinter {
-public:
-  void operator=(bool OptionWasSpecified) {
-    if (OptionWasSpecified) {
-      std::cerr << "Low Level Virtual Machine (" << PACKAGE_NAME << ") "
-                << PACKAGE_VERSION << " (see http://llvm.org/)";
-#ifndef NDEBUG
-      std::cerr << " DEBUG BUILD\n";
-#else
-      std::cerr << "\n";
-#endif
-      getOpts().clear();  // Don't bother making option dtors remove from map.
-      exit(1);
-    }
-  }
-};
-
-
 // Define the two HelpPrinter instances that are used to print out help, or
 // help-hidden...
 //
@@ -969,6 +979,39 @@ cl::opt<HelpPrinter, true, parser<bool> >
 HHOp("help-hidden", cl::desc("Display all available options"),
      cl::location(HiddenPrinter), cl::Hidden, cl::ValueDisallowed);
 
+void (*OverrideVersionPrinter)() = 0;
+
+class VersionPrinter {
+public:
+  void operator=(bool OptionWasSpecified) {
+    if (OptionWasSpecified) {
+      if (OverrideVersionPrinter == 0) {
+        std::cout << "Low Level Virtual Machine (http://llvm.org/):\n";
+        std::cout << "  " << PACKAGE_NAME << " version " << PACKAGE_VERSION;
+#ifdef LLVM_VERSION_INFO
+        std::cout << LLVM_VERSION_INFO;
+#endif
+        std::cout << "\n  ";
+#ifndef __OPTIMIZE__
+        std::cout << "DEBUG build";
+#else
+        std::cout << "Optimized build";
+#endif
+#ifndef NDEBUG
+        std::cout << " with assertions";
+#endif
+        std::cout << ".\n";
+        getOpts().clear();  // Don't bother making option dtors remove from map.
+        exit(1);
+      } else {
+        (*OverrideVersionPrinter)();
+        exit(1);
+      }
+    }
+  }
+};
+
+
 // Define the --version option that prints out the LLVM version for the tool
 VersionPrinter VersionPrinterInstance;
 cl::opt<VersionPrinter, true, parser<bool> >
@@ -988,3 +1031,7 @@ void cl::PrintHelpMessage() {
   // to make it look like --help was given, so we assign true.
   NormalPrinter = true;
 }
+
+void cl::SetVersionPrinter(void (*func)()) {
+  OverrideVersionPrinter = func;
+}