[RuntimeDyld] Make RuntimeDyldImpl::resolveExternalSymbols preserve the
[oota-llvm.git] / tools / bugpoint / ExtractFunction.cpp
index b40b4f10db9915a512a4e09be5c655220bb5264e..38cdf241ce767394bd3cdb31ccbdb6f5603bb6cb 100644 (file)
 //===----------------------------------------------------------------------===//
 
 #include "BugDriver.h"
-#include "llvm/Constants.h"
-#include "llvm/DataLayout.h"
-#include "llvm/DerivedTypes.h"
-#include "llvm/LLVMContext.h"
-#include "llvm/Module.h"
-#include "llvm/PassManager.h"
+#include "llvm/IR/Constants.h"
+#include "llvm/IR/DataLayout.h"
+#include "llvm/IR/DerivedTypes.h"
+#include "llvm/IR/LLVMContext.h"
+#include "llvm/IR/Module.h"
+#include "llvm/IR/Verifier.h"
 #include "llvm/Pass.h"
-#include "llvm/Analysis/Verifier.h"
-#include "llvm/Assembly/Writer.h"
-#include "llvm/Transforms/IPO.h"
-#include "llvm/Transforms/Scalar.h"
-#include "llvm/Transforms/Utils/Cloning.h"
-#include "llvm/Transforms/Utils/CodeExtractor.h"
+#include "llvm/PassManager.h"
 #include "llvm/Support/CommandLine.h"
 #include "llvm/Support/Debug.h"
 #include "llvm/Support/FileUtilities.h"
-#include "llvm/Support/ToolOutputFile.h"
 #include "llvm/Support/Path.h"
 #include "llvm/Support/Signals.h"
+#include "llvm/Support/ToolOutputFile.h"
+#include "llvm/Transforms/IPO.h"
+#include "llvm/Transforms/Scalar.h"
+#include "llvm/Transforms/Utils/Cloning.h"
+#include "llvm/Transforms/Utils/CodeExtractor.h"
 #include <set>
 using namespace llvm;
 
+#define DEBUG_TYPE "bugpoint"
+
 namespace llvm {
   bool DisableSimplifyCFG = false;
   extern cl::opt<std::string> OutputPrefix;
@@ -50,7 +51,7 @@ namespace {
 
   Function* globalInitUsesExternalBA(GlobalVariable* GV) {
     if (!GV->hasInitializer())
-      return 0;
+      return nullptr;
 
     Constant *I = GV->getInitializer();
 
@@ -77,7 +78,7 @@ namespace {
           Todo.push_back(C);
       }
     }
-    return 0;
+    return nullptr;
   }
 }  // end anonymous namespace
 
@@ -149,7 +150,7 @@ Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
     CleanupPasses.push_back("deadargelim");
 
   Module *New = runPassesOn(M, CleanupPasses);
-  if (New == 0) {
+  if (!New) {
     errs() << "Final cleanups failed.  Sorry. :(  Please report a bug!\n";
     return M;
   }
@@ -166,11 +167,11 @@ Module *BugDriver::ExtractLoop(Module *M) {
   LoopExtractPasses.push_back("loop-extract-single");
 
   Module *NewM = runPassesOn(M, LoopExtractPasses);
-  if (NewM == 0) {
+  if (!NewM) {
     outs() << "*** Loop extraction failed: ";
     EmitProgressBitcode(M, "loopextraction", true);
     outs() << "*** Sorry. :(  Please report a bug!\n";
-    return 0;
+    return nullptr;
   }
 
   // Check to see if we created any new functions.  If not, no loops were
@@ -179,7 +180,7 @@ Module *BugDriver::ExtractLoop(Module *M) {
   static unsigned NumExtracted = 32;
   if (M->size() == NewM->size() || --NumExtracted == 0) {
     delete NewM;
-    return 0;
+    return nullptr;
   } else {
     assert(M->size() < NewM->size() && "Loop extract removed functions?");
     Module::iterator MI = NewM->begin();
@@ -309,7 +310,7 @@ llvm::SplitFunctionsOutOfModule(Module *M,
   for (unsigned i = 0, e = F.size(); i != e; ++i) {
     Function *TNOF = cast<Function>(VMap[F[i]]);
     DEBUG(errs() << "Removing function ");
-    DEBUG(WriteAsOperand(errs(), TNOF, false));
+    DEBUG(TNOF->printAsOperand(errs(), false));
     DEBUG(errs() << "\n");
     TestFunctions.insert(cast<Function>(NewVMap[TNOF]));
     DeleteFunctionBody(TNOF);       // Function is now external in this module!
@@ -330,16 +331,16 @@ llvm::SplitFunctionsOutOfModule(Module *M,
       if (Function *SafeFn = globalInitUsesExternalBA(GV)) {
         errs() << "*** Error: when reducing functions, encountered "
                   "the global '";
-        WriteAsOperand(errs(), GV, false);
+        GV->printAsOperand(errs(), false);
         errs() << "' with an initializer that references blockaddresses "
                   "from safe function '" << SafeFn->getName()
                << "' and from test function '" << TestFn->getName() << "'.\n";
         exit(1);
       }
-      I->setInitializer(0);  // Delete the initializer to make it external
+      I->setInitializer(nullptr);  // Delete the initializer to make it external
     } else {
       // If we keep it in the safe module, then delete it in the test module
-      GV->setInitializer(0);
+      GV->setInitializer(nullptr);
     }
   }
 
@@ -363,25 +364,19 @@ llvm::SplitFunctionsOutOfModule(Module *M,
 Module *BugDriver::ExtractMappedBlocksFromModule(const
                                                  std::vector<BasicBlock*> &BBs,
                                                  Module *M) {
-  sys::Path uniqueFilename(OutputPrefix + "-extractblocks");
-  std::string ErrMsg;
-  if (uniqueFilename.createTemporaryFileOnDisk(true, &ErrMsg)) {
+  SmallString<128> Filename;
+  int FD;
+  error_code EC = sys::fs::createUniqueFile(
+      OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);
+  if (EC) {
     outs() << "*** Basic Block extraction failed!\n";
-    errs() << "Error creating temporary file: " << ErrMsg << "\n";
+    errs() << "Error creating temporary file: " << EC.message() << "\n";
     EmitProgressBitcode(M, "basicblockextractfail", true);
-    return 0;
+    return nullptr;
   }
-  sys::RemoveFileOnSignal(uniqueFilename);
+  sys::RemoveFileOnSignal(Filename);
 
-  std::string ErrorInfo;
-  tool_output_file BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo);
-  if (!ErrorInfo.empty()) {
-    outs() << "*** Basic Block extraction failed!\n";
-    errs() << "Error writing list of blocks to not extract: " << ErrorInfo
-           << "\n";
-    EmitProgressBitcode(M, "basicblockextractfail", true);
-    return 0;
-  }
+  tool_output_file BlocksToNotExtractFile(Filename.c_str(), FD);
   for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
        I != E; ++I) {
     BasicBlock *BB = *I;
@@ -393,24 +388,24 @@ Module *BugDriver::ExtractMappedBlocksFromModule(const
   }
   BlocksToNotExtractFile.os().close();
   if (BlocksToNotExtractFile.os().has_error()) {
-    errs() << "Error writing list of blocks to not extract: " << ErrorInfo
-           << "\n";
+    errs() << "Error writing list of blocks to not extract\n";
     EmitProgressBitcode(M, "basicblockextractfail", true);
     BlocksToNotExtractFile.os().clear_error();
-    return 0;
+    return nullptr;
   }
   BlocksToNotExtractFile.keep();
 
-  std::string uniqueFN = "--extract-blocks-file=" + uniqueFilename.str();
+  std::string uniqueFN = "--extract-blocks-file=";
+  uniqueFN += Filename.str();
   const char *ExtraArg = uniqueFN.c_str();
 
   std::vector<std::string> PI;
   PI.push_back("extract-blocks");
   Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg);
 
-  uniqueFilename.eraseFromDisk(); // Free disk space
+  sys::fs::remove(Filename.c_str());
 
-  if (Ret == 0) {
+  if (!Ret) {
     outs() << "*** Basic Block extraction failed, please report a bug!\n";
     EmitProgressBitcode(M, "basicblockextractfail", true);
   }