Use use_empty() instead of getNumUses(), avoiding a use list traversal.
[oota-llvm.git] / tools / lli / lli.cpp
index e3809538a57323e0ad3eda497586360c04260908..67b1424d839fa1a5315e0932dded192f84f0d2d4 100644 (file)
@@ -2,8 +2,8 @@
 //
 //                     The LLVM Compiler Infrastructure
 //
-// This file was developed by the LLVM research group and is distributed under
-// the University of Illinois Open Source License. See LICENSE.TXT for details.
+// This file is distributed under the University of Illinois Open Source
+// License. See LICENSE.TXT for details.
 //
 //===----------------------------------------------------------------------===//
 //
 #include "llvm/ModuleProvider.h"
 #include "llvm/Type.h"
 #include "llvm/Bitcode/ReaderWriter.h"
-#include "llvm/Bytecode/Reader.h"
 #include "llvm/CodeGen/LinkAllCodegenComponents.h"
 #include "llvm/ExecutionEngine/JIT.h"
 #include "llvm/ExecutionEngine/Interpreter.h"
 #include "llvm/ExecutionEngine/GenericValue.h"
 #include "llvm/Support/CommandLine.h"
-#include "llvm/Support/Compressor.h"
 #include "llvm/Support/ManagedStatic.h"
 #include "llvm/Support/MemoryBuffer.h"
 #include "llvm/Support/PluginLoader.h"
 using namespace llvm;
 
 namespace {
-  cl::opt<bool> Bitcode("bitcode");
-
   cl::opt<std::string>
-  InputFile(cl::desc("<input bytecode>"), cl::Positional, cl::init("-"));
+  InputFile(cl::desc("<input bitcode>"), cl::Positional, cl::init("-"));
 
   cl::list<std::string>
   InputArgv(cl::ConsumeAfter, cl::desc("<program arguments>..."));
@@ -56,6 +52,11 @@ namespace {
   cl::opt<bool>
   DisableCoreFiles("disable-core-files", cl::Hidden,
                    cl::desc("Disable emission of core files if possible"));
+
+  cl::opt<bool>
+  NoLazyCompilation("disable-lazy-compilation",
+                  cl::desc("Disable JIT lazy compilation"),
+                  cl::init(false));
 }
 
 static ExecutionEngine *EE = 0;
@@ -71,29 +72,19 @@ static void do_shutdown() {
 int main(int argc, char **argv, char * const *envp) {
   atexit(do_shutdown);  // Call llvm_shutdown() on exit.
   cl::ParseCommandLineOptions(argc, argv,
-                              " llvm interpreter & dynamic compiler\n");
+                              "llvm interpreter & dynamic compiler\n");
   sys::PrintStackTraceOnErrorSignal();
 
   // If the user doesn't want core files, disable them.
   if (DisableCoreFiles)
     sys::Process::PreventCoreFiles();
   
-  // Load the bytecode...
+  // Load the bitcode...
   std::string ErrorMsg;
-  ModuleProvider *MP = 0;
-  if (Bitcode) {
-    MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(&InputFile[0],
-                                                        InputFile.size());
-    if (Buffer == 0)
-      ErrorMsg = "Error reading file '" + InputFile + "'";
-    else {
-      MP = getBitcodeModuleProvider(Buffer, &ErrorMsg);
-      if (!MP) delete Buffer;
-    }
-  } else {
-    MP = getBytecodeModuleProvider(InputFile, 
-                                   Compressor::decompressToNewBuffer,
-                                   &ErrorMsg);
+  ModuleProvider *MP = NULL;
+  if (MemoryBuffer *Buffer = MemoryBuffer::getFileOrSTDIN(InputFile,&ErrorMsg)) {
+    MP = getBitcodeModuleProvider(Buffer, &ErrorMsg);
+    if (!MP) delete Buffer;
   }
   
   if (!MP) {
@@ -103,18 +94,27 @@ int main(int argc, char **argv, char * const *envp) {
   }
 
   // Get the module as the MP could go away once EE takes over.
-  Module *Mod = MP->getModule();
+  Module *Mod = NoLazyCompilation
+    ? MP->materializeModule(&ErrorMsg) : MP->getModule();
+  if (!Mod) {
+    std::cerr << argv[0] << ": bitcode didn't read correctly.\n";
+    std::cerr << "Reason: " << ErrorMsg << "\n";
+    exit(1);
+  }
 
   // If we are supposed to override the target triple, do so now.
   if (!TargetTriple.empty())
     Mod->setTargetTriple(TargetTriple);
-  
+
   EE = ExecutionEngine::create(MP, ForceInterpreter, &ErrorMsg);
   if (!EE && !ErrorMsg.empty()) {
     std::cerr << argv[0] << ":error creating EE: " << ErrorMsg << "\n";
     exit(1);
   }
 
+  if (NoLazyCompilation)
+    EE->DisableLazyCompilation();
+
   // If the user specifically requested an argv[0] to pass into the program,
   // do it now.
   if (!FakeArgv0.empty()) {
@@ -134,8 +134,8 @@ int main(int argc, char **argv, char * const *envp) {
   // using the contents of Args to determine argc & argv, and the contents of
   // EnvVars to determine envp.
   //
-  Function *Fn = Mod->getFunction("main");
-  if (!Fn) {
+  Function *MainFn = Mod->getFunction("main");
+  if (!MainFn) {
     std::cerr << "'main' function not found in module.\n";
     return -1;
   }
@@ -150,9 +150,17 @@ int main(int argc, char **argv, char * const *envp) {
  
   // Run static constructors.
   EE->runStaticConstructorsDestructors(false);
-  
+
+  if (NoLazyCompilation) {
+    for (Module::iterator I = Mod->begin(), E = Mod->end(); I != E; ++I) {
+      Function *Fn = &*I;
+      if (Fn != MainFn && !Fn->isDeclaration())
+        EE->getPointerToFunction(Fn);
+    }
+  }
+
   // Run main.
-  int Result = EE->runFunctionAsMain(Fn, InputArgv, envp);
+  int Result = EE->runFunctionAsMain(MainFn, InputArgv, envp);
 
   // Run static destructors.
   EE->runStaticConstructorsDestructors(true);