1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- ExtractFunction.cpp - Extract a function from Program --------------===//
23da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//                     The LLVM Compiler Infrastructure
47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
521c62da287237d39d0d95004881ea4baae3be6daChris Lattner// This file is distributed under the University of Illinois Open Source
621c62da287237d39d0d95004881ea4baae3be6daChris Lattner// License. See LICENSE.TXT for details.
73da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman//
87c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//===----------------------------------------------------------------------===//
9afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
10efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner// This file implements several methods that are used to extract functions,
11efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner// loops, or portions of a module from the rest of the module.
12afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
13afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===----------------------------------------------------------------------===//
14afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
15afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "BugDriver.h"
160b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Constants.h"
170b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DataLayout.h"
180b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/DerivedTypes.h"
190b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/LLVMContext.h"
20ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines#include "llvm/IR/LegacyPassManager.h"
210b8c9a80f20772c3793201ab5b251d3520b9cea3Chandler Carruth#include "llvm/IR/Module.h"
2236b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines#include "llvm/IR/Verifier.h"
23d1a85a744cc1001c2b7fc37cf37aca266964f519Brian Gaeke#include "llvm/Pass.h"
24551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
25551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
26551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/FileUtilities.h"
271f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Path.h"
281f6efa3996dd1929fbc129203ce5009b620e6969Michael J. Spencer#include "llvm/Support/Signals.h"
29f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Support/ToolOutputFile.h"
30f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Transforms/IPO.h"
31f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Transforms/Scalar.h"
32f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Transforms/Utils/Cloning.h"
33f010c464a11444733ec67e31aace8bcebeaf2588Chandler Carruth#include "llvm/Transforms/Utils/CodeExtractor.h"
34fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner#include <set>
35c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattnerusing namespace llvm;
36d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
37dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines#define DEBUG_TYPE "bugpoint"
38dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines
39d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
40c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  bool DisableSimplifyCFG = false;
4168ccdaa84909108c42417a8091c771598e26456eDaniel Dunbar  extern cl::opt<std::string> OutputPrefix;
42d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
43d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
446db70ef879916e6115ac97eb76e4fea973652e2cChris Lattnernamespace {
456db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  cl::opt<bool>
466db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  NoDCE ("disable-dce",
476db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -dce pass to reduce testcases"));
4847ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  cl::opt<bool, true>
4947ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
506db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
51967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman
52967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman  Function* globalInitUsesExternalBA(GlobalVariable* GV) {
53967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    if (!GV->hasInitializer())
54dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines      return nullptr;
55967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman
56967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    Constant *I = GV->getInitializer();
57967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman
58967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    // walk the values used by the initializer
59967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    // (and recurse into things like ConstantExpr)
60967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    std::vector<Constant*> Todo;
61967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    std::set<Constant*> Done;
62967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    Todo.push_back(I);
63967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman
64967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    while (!Todo.empty()) {
65967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      Constant* V = Todo.back();
66967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      Todo.pop_back();
67967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      Done.insert(V);
68967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman
69967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      if (BlockAddress *BA = dyn_cast<BlockAddress>(V)) {
70967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman        Function *F = BA->getFunction();
71967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman        if (F->isDeclaration())
72967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman          return F;
73967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      }
74967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman
75967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      for (User::op_iterator i = V->op_begin(), e = V->op_end(); i != e; ++i) {
76967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman        Constant *C = dyn_cast<Constant>(*i);
77967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman        if (C && !isa<GlobalValue>(C) && !Done.count(C))
78967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman          Todo.push_back(C);
79967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      }
80967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    }
81dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr;
82967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman  }
83967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman}  // end anonymous namespace
84afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
8537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstd::unique_ptr<Module>
8637ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesBugDriver::deleteInstructionFromProgram(const Instruction *I,
8737ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                        unsigned Simplification) {
88866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  // FIXME, use vmap?
89cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  Module *Clone = CloneModule(Program).release();
906520785dcd22012535934098942d57c07c7631c2Chris Lattner
910cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  const BasicBlock *PBB = I->getParent();
920cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  const Function *PF = PBB->getParent();
936520785dcd22012535934098942d57c07c7631c2Chris Lattner
94866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn
950cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RFI, std::distance(PF->getParent()->begin(),
960cc8807029f577996a442b96d24c3346ed6de091Chris Lattner                                  Module::const_iterator(PF)));
976520785dcd22012535934098942d57c07c7631c2Chris Lattner
986520785dcd22012535934098942d57c07c7631c2Chris Lattner  Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
990cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
1006520785dcd22012535934098942d57c07c7631c2Chris Lattner
1016520785dcd22012535934098942d57c07c7631c2Chris Lattner  BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
1020cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
103cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  Instruction *TheInst = &*RI; // Got the corresponding instruction!
1046520785dcd22012535934098942d57c07c7631c2Chris Lattner
1056520785dcd22012535934098942d57c07c7631c2Chris Lattner  // If this instruction produces a value, replace any users with null values
106e49a13e7260b83ce56d01446f2a165cc9f35da7fDan Gohman  if (!TheInst->getType()->isVoidTy())
107a7235ea7245028a0723e8ab7fd011386b3900777Owen Anderson    TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
1086520785dcd22012535934098942d57c07c7631c2Chris Lattner
1096520785dcd22012535934098942d57c07c7631c2Chris Lattner  // Remove the instruction from the program.
1100cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  TheInst->getParent()->getInstList().erase(TheInst);
1116520785dcd22012535934098942d57c07c7631c2Chris Lattner
11244be25716628941b4cccccf56a28ee0ba2606850Chris Lattner  // Spiff up the output a little bit.
113866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  std::vector<std::string> Passes;
1145da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner
115866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  /// Can we get rid of the -disable-* options?
1166db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  if (Simplification > 1 && !NoDCE)
117866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola    Passes.push_back("dce");
11847ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  if (Simplification && !DisableSimplifyCFG)
119866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola    Passes.push_back("simplifycfg");      // Delete dead control flow
120866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola
121866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  Passes.push_back("verify");
12237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> New = runPassesOn(Clone, Passes);
123866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  delete Clone;
124866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  if (!New) {
125866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola    errs() << "Instruction removal failed.  Sorry. :(  Please report a bug!\n";
126866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola    exit(1);
127866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  }
128866aa0d742b7bc9811fd1b45507af999c605205aRafael Espindola  return New;
1296520785dcd22012535934098942d57c07c7631c2Chris Lattner}
130ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner
13137ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstd::unique_ptr<Module>
13237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesBugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
13328b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  // Make all functions external, so GlobalDCE doesn't delete them...
13428b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
13528b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
1363da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
1378261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  std::vector<std::string> CleanupPasses;
1388261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  CleanupPasses.push_back("globaldce");
139fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
140c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  if (MayModifySemantics)
1418261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola    CleanupPasses.push_back("deadarghaX0r");
142c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  else
1438261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola    CleanupPasses.push_back("deadargelim");
144fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
14537ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> New = runPassesOn(M, CleanupPasses);
146dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!New) {
14765f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    errs() << "Final cleanups failed.  Sorry. :(  Please report a bug!\n";
14837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines    return nullptr;
149fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  }
150a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  delete M;
151a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  return New;
152ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner}
153be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
15437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstd::unique_ptr<Module> BugDriver::extractLoop(Module *M) {
1558261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  std::vector<std::string> LoopExtractPasses;
1568261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  LoopExtractPasses.push_back("loop-extract-single");
1577546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
15837ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> NewM = runPassesOn(M, LoopExtractPasses);
159dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!NewM) {
160ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman    outs() << "*** Loop extraction failed: ";
161bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola    EmitProgressBitcode(M, "loopextraction", true);
162ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman    outs() << "*** Sorry. :(  Please report a bug!\n";
163dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr;
1647546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  }
165a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner
166a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  // Check to see if we created any new functions.  If not, no loops were
167a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  // extracted and we should return null.  Limit the number of loops we extract
168a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  // to avoid taking forever.
169a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  static unsigned NumExtracted = 32;
17090c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  if (M->size() == NewM->size() || --NumExtracted == 0) {
171dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr;
17290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  } else {
17390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    assert(M->size() < NewM->size() && "Loop extract removed functions?");
17490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    Module::iterator MI = NewM->begin();
17590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    for (unsigned i = 0, e = M->size(); i != e; ++i)
17690c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner      ++MI;
177a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  }
1783da94aec4d429b2ba0f65fa040c33650cade196bMisha Brukman
179a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  return NewM;
1807546c3884a400b72d10fc19f120c6798b294a39dChris Lattner}
1817546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
182cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarstatic void eliminateAliases(GlobalValue *GV) {
183cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  // First, check whether a GlobalAlias references this definition.
184cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  // GlobalAlias MAY NOT reference declarations.
185cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  for (;;) {
186cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    // 1. Find aliases
187cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    SmallVector<GlobalAlias*,1> aliases;
188cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    Module *M = GV->getParent();
189cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    for (Module::alias_iterator I=M->alias_begin(), E=M->alias_end(); I!=E; ++I)
190cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      if (I->getAliasee()->stripPointerCasts() == GV)
191cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar        aliases.push_back(&*I);
192cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    if (aliases.empty())
193cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      break;
194cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    // 2. Resolve aliases
195cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    for (unsigned i=0, e=aliases.size(); i<e; ++i) {
196cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      aliases[i]->replaceAllUsesWith(aliases[i]->getAliasee());
197cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      aliases[i]->eraseFromParent();
198cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    }
199cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    // 3. Repeat until no more aliases found; there might
200cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    // be an alias to an alias...
201cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  }
202cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar}
203cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
204cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar//
205cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar// DeleteGlobalInitializer - "Remove" the global variable by deleting its initializer,
206cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar// making it external.
207cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar//
208cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarvoid llvm::DeleteGlobalInitializer(GlobalVariable *GV) {
209cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  eliminateAliases(GV);
210cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  GV->setInitializer(nullptr);
211cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar}
2127546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
213be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of its basic
214be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// blocks, making it external.
215be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner//
216be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnervoid llvm::DeleteFunctionBody(Function *F) {
217cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  eliminateAliases(F);
218cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
219be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // delete the body of the function...
220be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  F->deleteBody();
2215cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer  assert(F->isDeclaration() && "This didn't make the function external!");
222be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner}
223be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
2245a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// GetTorInit - Given a list of entries for static ctors/dtors, return them
2255a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// as a constant array.
2265a7a9e53861188ad84cb0e08e34562da744a907aChris Lattnerstatic Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) {
2275a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  assert(!TorList.empty() && "Don't create empty tor list!");
2285a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  std::vector<Constant*> ArrayElts;
2295fdd6c8793462549e3593890ec61573da06e3346Jay Foad  Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext());
230ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines
231db125cfaf57cc83e7dd7453de2d509bc8efd0e5eChris Lattner  StructType *STy =
232ebe69fe11e48d322045d5949c83283927a0d790bStephen Hines      StructType::get(Int32Ty, TorList[0].first->getType(), nullptr);
2335a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  for (unsigned i = 0, e = TorList.size(); i != e; ++i) {
234b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner    Constant *Elts[] = {
235b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner      ConstantInt::get(Int32Ty, TorList[i].second),
236b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner      TorList[i].first
237b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner    };
238b065b06c12dba6001b8140df2744d0c856ef6ea1Chris Lattner    ArrayElts.push_back(ConstantStruct::get(STy, Elts));
2395a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
240debcb01b0f0a15f568ca69e8f288fade4bfc7297Owen Anderson  return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
2415a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner                                           ArrayElts.size()),
2425a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner                            ArrayElts);
2435a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner}
2445a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2455a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and
2465a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// M1 has all of the global variables.  If M2 contains any functions that are
2475a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and
2485a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner/// prune appropriate entries out of M1s list.
249d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohmanstatic void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2,
2501ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola                                ValueToValueMapTy &VMap) {
2515a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GlobalVariable *GV = M1->getNamedGlobal(GlobalName);
252bb46f52027416598a662dc1c58f48d9d56b1a65bRafael Espindola  if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() ||
2535a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      !GV->use_empty()) return;
2545a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2555a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  std::vector<std::pair<Function*, int> > M1Tors, M2Tors;
2565a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
2575a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  if (!InitList) return;
2585a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2595a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
2605a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
2615a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (CS->getNumOperands() != 2) return;  // Not array of 2-element structs.
2625a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2635a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (CS->getOperand(1)->isNullValue())
2645a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner        break;  // Found a null terminator, stop here.
2655a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
266b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer      ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
267b83eb6447ba155342598f0fabe1f08f5baa9164aReid Spencer      int Priority = CI ? CI->getSExtValue() : 0;
2685a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2695a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      Constant *FP = CS->getOperand(1);
2705a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
2713da59db637a887474c1b1346c1f3ccf53b6c4663Reid Spencer        if (CE->isCast())
2725a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          FP = CE->getOperand(0);
2735a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      if (Function *F = dyn_cast<Function>(FP)) {
2745cbf985dcbc89fba3208e7baf8b6f488b06d3ec9Reid Spencer        if (!F->isDeclaration())
2755a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          M1Tors.push_back(std::make_pair(F, Priority));
2765a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner        else {
2775a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          // Map to M2's version of the function.
278e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel          F = cast<Function>(VMap[F]);
2795a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner          M2Tors.push_back(std::make_pair(F, Priority));
2805a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner        }
2815a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner      }
2825a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    }
2835a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
2845a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2855a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GV->eraseFromParent();
2865a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  if (!M1Tors.empty()) {
2875a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    Constant *M1Init = GetTorInit(M1Tors);
288e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson    new GlobalVariable(*M1, M1Init->getType(), false,
2893d29df3e8a203b167d8071ea6f805b21db18a5afOwen Anderson                       GlobalValue::AppendingLinkage,
290e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson                       M1Init, GlobalName);
2915a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
2925a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2935a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GV = M2->getNamedGlobal(GlobalName);
2945a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  assert(GV && "Not a clone of M1?");
2955a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  assert(GV->use_empty() && "llvm.ctors shouldn't have uses!");
2965a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
2975a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  GV->eraseFromParent();
2985a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  if (!M2Tors.empty()) {
2995a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner    Constant *M2Init = GetTorInit(M2Tors);
300e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson    new GlobalVariable(*M2, M2Init->getType(), false,
3013d29df3e8a203b167d8071ea6f805b21db18a5afOwen Anderson                       GlobalValue::AppendingLinkage,
302e9b11b431308f4766b73cda93e38ec930c912122Owen Anderson                       M2Init, GlobalName);
3035a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  }
3045a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner}
3055a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
306cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarstd::unique_ptr<Module>
307cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainarllvm::SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
3081ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola                                ValueToValueMapTy &VMap) {
309be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Make sure functions & globals are all external so that linkage
310be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // between the two modules will work.
311be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
312be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
3135a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  for (Module::global_iterator I = M->global_begin(), E = M->global_end();
3147220b814ed6f432d1d31d444b65e0c3b120942dfOwen Anderson       I != E; ++I) {
3153f53fa9a51c4ce7ba81170ca7ab2e49bd37281b0Daniel Dunbar    if (I->hasName() && I->getName()[0] == '\01')
3163f53fa9a51c4ce7ba81170ca7ab2e49bd37281b0Daniel Dunbar      I->setName(I->getName().substr(1));
317be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
3187220b814ed6f432d1d31d444b65e0c3b120942dfOwen Anderson  }
319be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
3201ed219a9d2279ce5a5bbcf16d9b7ccc05cce638cRafael Espindola  ValueToValueMapTy NewVMap;
321cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  std::unique_ptr<Module> New = CloneModule(M, NewVMap);
322be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
323fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  // Remove the Test functions from the Safe module
324d50330cd02b00c8e3de40e8544c45701b9891d87Dan Gohman  std::set<Function *> TestFunctions;
325be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (unsigned i = 0, e = F.size(); i != e; ++i) {
326e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel    Function *TNOF = cast<Function>(VMap[F[i]]);
32765f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    DEBUG(errs() << "Removing function ");
32836b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines    DEBUG(TNOF->printAsOperand(errs(), false));
32965f57c233cd4499e2e8b52a503201e64edfd6a9eDan Gohman    DEBUG(errs() << "\n");
330e9916a302f1bacad234d7dafc1df3dc968a6ba0fDevang Patel    TestFunctions.insert(cast<Function>(NewVMap[TNOF]));
331fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner    DeleteFunctionBody(TNOF);       // Function is now external in this module!
332e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins  }
333e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins
3345a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner
335fef02427cda8dc0b3e6aedd6d78c4ba3e1e48069Chris Lattner  // Remove the Safe functions from the Test module
336cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  for (Function &I : *New)
337cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    if (!TestFunctions.count(&I))
338cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      DeleteFunctionBody(&I);
339e47863e212e0af6239f6f40b2496ede29847bbcePatrick Jenkins
340967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman  // Try to split the global initializers evenly
341cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  for (GlobalVariable &I : M->globals()) {
342cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    GlobalVariable *GV = cast<GlobalVariable>(NewVMap[&I]);
343cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar    if (Function *TestFn = globalInitUsesExternalBA(&I)) {
344967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      if (Function *SafeFn = globalInitUsesExternalBA(GV)) {
345967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman        errs() << "*** Error: when reducing functions, encountered "
346967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman                  "the global '";
34736b56886974eae4f9c5ebc96befd3e7bfe5de338Stephen Hines        GV->printAsOperand(errs(), false);
348967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman        errs() << "' with an initializer that references blockaddresses "
349967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman                  "from safe function '" << SafeFn->getName()
350967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman               << "' and from test function '" << TestFn->getName() << "'.\n";
351967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman        exit(1);
352967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      }
353cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      DeleteGlobalInitializer(&I); // Delete the initializer to make it external
354967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    } else {
355967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman      // If we keep it in the safe module, then delete it in the test module
356cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar      DeleteGlobalInitializer(GV);
357967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman    }
358967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman  }
359967570f2ecdbedeae80bcc19744cf111c3e112e1Eli Friedman
3605a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  // Make sure that there is a global ctor/dtor array in both halves of the
3615a7a9e53861188ad84cb0e08e34562da744a907aChris Lattner  // module if they both have static ctor/dtor functions.
362cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  SplitStaticCtorDtor("llvm.global_ctors", M, New.get(), NewVMap);
363cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar  SplitStaticCtorDtor("llvm.global_dtors", M, New.get(), NewVMap);
364cddc3e03e4ec99c0268c03a126195173e519ed58Pirama Arumuga Nainar
365be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  return New;
366be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner}
3675e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
3685e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===//
3695e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner// Basic Block Extraction Code
3705e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===//
3715e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
37237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hinesstd::unique_ptr<Module>
37337ed9c199ca639565f6ce88105f9e39e898d82d0Stephen HinesBugDriver::extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
37437ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines                                         Module *M) {
375b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  SmallString<128> Filename;
376b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  int FD;
377c6a4f5e819217e1e12c458aed8e7b122e23a3a58Stephen Hines  std::error_code EC = sys::fs::createUniqueFile(
378200c748a8643cd127271f4d6849da1e147d4442fRafael Espindola      OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);
379b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  if (EC) {
380ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman    outs() << "*** Basic Block extraction failed!\n";
381b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola    errs() << "Error creating temporary file: " << EC.message() << "\n";
382bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola    EmitProgressBitcode(M, "basicblockextractfail", true);
383dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr;
3846fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  }
385b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  sys::RemoveFileOnSignal(Filename);
3866fa98b13206583e6eb90b8304758b35548914944Nick Lewycky
387b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  tool_output_file BlocksToNotExtractFile(Filename.c_str(), FD);
3886fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
3896fa98b13206583e6eb90b8304758b35548914944Nick Lewycky       I != E; ++I) {
3906fa98b13206583e6eb90b8304758b35548914944Nick Lewycky    BasicBlock *BB = *I;
3914a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner    // If the BB doesn't have a name, give it one so we have something to key
3924a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner    // off of.
3934a6a6f21f3342428e5dcf28b0ddfbe0039a040e5Chris Lattner    if (!BB->hasName()) BB->setName("tmpbb");
394a7b0cb759433c715065440ee2a963a04db7f2b0bBenjamin Kramer    BlocksToNotExtractFile.os() << BB->getParent()->getName() << " "
395d4c454317a38d65957edebe62bfc69fc8d9885e8Dan Gohman                                << BB->getName() << "\n";
3966fa98b13206583e6eb90b8304758b35548914944Nick Lewycky  }
397d4c454317a38d65957edebe62bfc69fc8d9885e8Dan Gohman  BlocksToNotExtractFile.os().close();
398d4c454317a38d65957edebe62bfc69fc8d9885e8Dan Gohman  if (BlocksToNotExtractFile.os().has_error()) {
399b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola    errs() << "Error writing list of blocks to not extract\n";
400f29140106f74d15ba357aa0a7f109adc939c3104Dan Gohman    EmitProgressBitcode(M, "basicblockextractfail", true);
401d4c454317a38d65957edebe62bfc69fc8d9885e8Dan Gohman    BlocksToNotExtractFile.os().clear_error();
402dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines    return nullptr;
403f29140106f74d15ba357aa0a7f109adc939c3104Dan Gohman  }
404f29140106f74d15ba357aa0a7f109adc939c3104Dan Gohman  BlocksToNotExtractFile.keep();
4056fa98b13206583e6eb90b8304758b35548914944Nick Lewycky
406b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  std::string uniqueFN = "--extract-blocks-file=";
407b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  uniqueFN += Filename.str();
40812ea66a7277240c5b045ed14c140f94d453eea0eBenjamin Kramer  const char *ExtraArg = uniqueFN.c_str();
4096fa98b13206583e6eb90b8304758b35548914944Nick Lewycky
4108261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  std::vector<std::string> PI;
4118261dfed05e32302469ef707cc881fed2c31f85fRafael Espindola  PI.push_back("extract-blocks");
41237ed9c199ca639565f6ce88105f9e39e898d82d0Stephen Hines  std::unique_ptr<Module> Ret = runPassesOn(M, PI, false, 1, &ExtraArg);
4136fa98b13206583e6eb90b8304758b35548914944Nick Lewycky
414b81784f0fe8aac20a6ed320851ca1f642175ebc3Rafael Espindola  sys::fs::remove(Filename.c_str());
4156fa98b13206583e6eb90b8304758b35548914944Nick Lewycky
416dce4a407a24b04eebc6a376f8e62b41aaa7b071fStephen Hines  if (!Ret) {
417ac95cc79ac0b899d566cc29c0f646f39c2fa35c0Dan Gohman    outs() << "*** Basic Block extraction failed, please report a bug!\n";
418bae1b71cbb930e419df03db209ebc547a0e4ec72Rafael Espindola    EmitProgressBitcode(M, "basicblockextractfail", true);
419891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner  }
4205e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  return Ret;
4215e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
422