ExtractFunction.cpp revision a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90
1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- ExtractFunction.cpp - Extract a function from Program --------------===//
27c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
37c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//                     The LLVM Compiler Infrastructure
47c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
57c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// This file was developed by the LLVM research group and is distributed under
67c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell// the University of Illinois Open Source License. See LICENSE.TXT for details.
77c0e022c5c4be4b11e199a53f73bbdd84e34aa80John Criswell//
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"
16e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Constant.h"
17afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Module.h"
18afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/PassManager.h"
19d1a85a744cc1001c2b7fc37cf37aca266964f519Brian Gaeke#include "llvm/Pass.h"
20e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Type.h"
21e49603d79d220a795bd50684c8b1f503ee40f97fMisha Brukman#include "llvm/Analysis/Verifier.h"
22afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/IPO.h"
236520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Transforms/Scalar.h"
24afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/Utils/Cloning.h"
255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner#include "llvm/Transforms/Utils/FunctionUtils.h"
265da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner#include "llvm/Target/TargetData.h"
27551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/CommandLine.h"
28551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/Debug.h"
29551ccae044b0ff658fe629dd67edd5ffe75d10e8Reid Spencer#include "llvm/Support/FileUtilities.h"
30fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner#include <set>
31c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattnerusing namespace llvm;
32d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
33d0fde30ce850b78371fd1386338350591f9ff494Brian Gaekenamespace llvm {
34c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  bool DisableSimplifyCFG = false;
35d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke} // End llvm namespace
36d0fde30ce850b78371fd1386338350591f9ff494Brian Gaeke
376db70ef879916e6115ac97eb76e4fea973652e2cChris Lattnernamespace {
386db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  cl::opt<bool>
396db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  NoDCE ("disable-dce",
406db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -dce pass to reduce testcases"));
4147ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  cl::opt<bool, true>
4247ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
436db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
446db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner}
45afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
466520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deleteInstructionFromProgram - This method clones the current Program and
476520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deletes the specified instruction from the cloned module.  It then runs a
486520785dcd22012535934098942d57c07c7631c2Chris Lattner/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
496520785dcd22012535934098942d57c07c7631c2Chris Lattner/// depends on the value.  The modified module is then returned.
506520785dcd22012535934098942d57c07c7631c2Chris Lattner///
510cc8807029f577996a442b96d24c3346ed6de091Chris LattnerModule *BugDriver::deleteInstructionFromProgram(const Instruction *I,
526520785dcd22012535934098942d57c07c7631c2Chris Lattner                                                unsigned Simplification) const {
536520785dcd22012535934098942d57c07c7631c2Chris Lattner  Module *Result = CloneModule(Program);
546520785dcd22012535934098942d57c07c7631c2Chris Lattner
550cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  const BasicBlock *PBB = I->getParent();
560cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  const Function *PF = PBB->getParent();
576520785dcd22012535934098942d57c07c7631c2Chris Lattner
586520785dcd22012535934098942d57c07c7631c2Chris Lattner  Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
590cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RFI, std::distance(PF->getParent()->begin(),
600cc8807029f577996a442b96d24c3346ed6de091Chris Lattner                                  Module::const_iterator(PF)));
616520785dcd22012535934098942d57c07c7631c2Chris Lattner
626520785dcd22012535934098942d57c07c7631c2Chris Lattner  Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
630cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
646520785dcd22012535934098942d57c07c7631c2Chris Lattner
656520785dcd22012535934098942d57c07c7631c2Chris Lattner  BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
660cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
670cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  Instruction *TheInst = RI;              // Got the corresponding instruction!
686520785dcd22012535934098942d57c07c7631c2Chris Lattner
696520785dcd22012535934098942d57c07c7631c2Chris Lattner  // If this instruction produces a value, replace any users with null values
700cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  if (TheInst->getType() != Type::VoidTy)
710cc8807029f577996a442b96d24c3346ed6de091Chris Lattner    TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
726520785dcd22012535934098942d57c07c7631c2Chris Lattner
736520785dcd22012535934098942d57c07c7631c2Chris Lattner  // Remove the instruction from the program.
740cc8807029f577996a442b96d24c3346ed6de091Chris Lattner  TheInst->getParent()->getInstList().erase(TheInst);
756520785dcd22012535934098942d57c07c7631c2Chris Lattner
7644be25716628941b4cccccf56a28ee0ba2606850Chris Lattner  // Spiff up the output a little bit.
776520785dcd22012535934098942d57c07c7631c2Chris Lattner  PassManager Passes;
785da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner  // Make sure that the appropriate target data is always used...
795da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner  Passes.add(new TargetData("bugpoint", Result));
805da69c79f9c4490c6657c207430dfeb1060fc4ddChris Lattner
81efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// FIXME: If this used runPasses() like the methods below, we could get rid
82efdc0b505712d1ca4460def27e51c430f033d58dChris Lattner  /// of the -disable-* options!
836db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  if (Simplification > 1 && !NoDCE)
846520785dcd22012535934098942d57c07c7631c2Chris Lattner    Passes.add(createDeadCodeEliminationPass());
8547ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  if (Simplification && !DisableSimplifyCFG)
866520785dcd22012535934098942d57c07c7631c2Chris Lattner    Passes.add(createCFGSimplificationPass());      // Delete dead control flow
8710f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner
8810f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner  Passes.add(createVerifierPass());
896520785dcd22012535934098942d57c07c7631c2Chris Lattner  Passes.run(*Result);
906520785dcd22012535934098942d57c07c7631c2Chris Lattner  return Result;
916520785dcd22012535934098942d57c07c7631c2Chris Lattner}
92ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner
93fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattnerstatic const PassInfo *getPI(Pass *P) {
94fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  const PassInfo *PI = P->getPassInfo();
95fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  delete P;
96fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  return PI;
97fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner}
98fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
99ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// performFinalCleanups - This method clones the current Program and performs
100ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// a series of cleanups intended to get rid of extra cruft on the module
101ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// before handing it to the user...
102ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner///
103fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris LattnerModule *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
10428b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  // Make all functions external, so GlobalDCE doesn't delete them...
10528b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
10628b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
107dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner
108fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  std::vector<const PassInfo*> CleanupPasses;
109fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
110fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  CleanupPasses.push_back(getPI(createGlobalDCEPass()));
111fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
112fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
113c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  if (MayModifySemantics)
114c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner    CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
115c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner  else
116c6b519d64ef55d39e66a49510d4703a49bf228ccChris Lattner    CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
117fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner
118a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  Module *New = runPassesOn(M, CleanupPasses);
119a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  if (New == 0) {
1207546c3884a400b72d10fc19f120c6798b294a39dChris Lattner    std::cerr << "Final cleanups failed.  Sorry. :(  Please report a bug!\n";
121fcb6ec0c7e37c2d15ddb04878f05cbd69d1da036Chris Lattner  }
122a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  delete M;
123a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  return New;
124ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner}
125be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
126be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
1277546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// ExtractLoop - Given a module, extract up to one loop from it into a new
1287546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// function.  This returns null if there are no extractable loops in the
1297546c3884a400b72d10fc19f120c6798b294a39dChris Lattner/// program or if the loop extractor crashes.
1307546c3884a400b72d10fc19f120c6798b294a39dChris LattnerModule *BugDriver::ExtractLoop(Module *M) {
1317546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  std::vector<const PassInfo*> LoopExtractPasses;
1327546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass()));
1337546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
134a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  Module *NewM = runPassesOn(M, LoopExtractPasses);
135a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  if (NewM == 0) {
136a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    Module *Old = swapProgramIn(M);
137a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "*** Loop extraction failed: ";
138a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    EmitProgressBytecode("loopextraction", true);
139a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    std::cout << "*** Sorry. :(  Please report a bug!\n";
140a1cf1c8c87f10f12343ff6ae75f332390e7205abChris Lattner    swapProgramIn(Old);
1417546c3884a400b72d10fc19f120c6798b294a39dChris Lattner    return 0;
1427546c3884a400b72d10fc19f120c6798b294a39dChris Lattner  }
143a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner
144a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  // Check to see if we created any new functions.  If not, no loops were
145a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  // extracted and we should return null.  Limit the number of loops we extract
146a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  // to avoid taking forever.
147a269ec7b0a83d3b20730fd2d9f7c3ed5a552da90Chris Lattner  static unsigned NumExtracted = 32;
14890c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  if (M->size() == NewM->size() || --NumExtracted == 0) {
149a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner    delete NewM;
150a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner    return 0;
15190c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner  } else {
15290c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    assert(M->size() < NewM->size() && "Loop extract removed functions?");
15390c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    Module::iterator MI = NewM->begin();
15490c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner    for (unsigned i = 0, e = M->size(); i != e; ++i)
15590c18c5c69d9c451e5fdca1e4b4b95e8ed13291aChris Lattner      ++MI;
156a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  }
157a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner
158a75766a6c14b364b74b30546802e26d4b4b36a9bChris Lattner  return NewM;
1597546c3884a400b72d10fc19f120c6798b294a39dChris Lattner}
1607546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
1617546c3884a400b72d10fc19f120c6798b294a39dChris Lattner
162be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// DeleteFunctionBody - "Remove" the function by deleting all of its basic
163be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner// blocks, making it external.
164be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner//
165be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattnervoid llvm::DeleteFunctionBody(Function *F) {
166be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // delete the body of the function...
167be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  F->deleteBody();
168be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  assert(F->isExternal() && "This didn't make the function external!");
169be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner}
170be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
171be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
172be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// module, split the functions OUT of the specified module, and place them in
173be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner/// the new module.
1745eda1f2f65941a24ef9a5a85addf4d90eb13fa9dChris Lattner///
1755eda1f2f65941a24ef9a5a85addf4d90eb13fa9dChris Lattner/// FIXME: this could be made DRAMATICALLY more efficient for large programs if
1765eda1f2f65941a24ef9a5a85addf4d90eb13fa9dChris Lattner/// we just MOVED functions from one module to the other, instead of cloning the
1775eda1f2f65941a24ef9a5a85addf4d90eb13fa9dChris Lattner/// whole module, then proceeding to delete an entire module's worth of stuff.
1785eda1f2f65941a24ef9a5a85addf4d90eb13fa9dChris Lattner///
179be21ca54e08339ede5dd4bbb882182d22e274988Chris LattnerModule *llvm::SplitFunctionsOutOfModule(Module *M,
180be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner                                        const std::vector<Function*> &F) {
181be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Make sure functions & globals are all external so that linkage
182be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // between the two modules will work.
183be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
184be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
185be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
186be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
187be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
188be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  Module *New = CloneModule(M);
189be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
190be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Make sure global initializers exist only in the safe module (CBE->.so)
191be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (Module::giterator I = New->gbegin(), E = New->gend(); I != E; ++I)
192be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    I->setInitializer(0);  // Delete the initializer to make it external
193be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
194be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Remove the Test functions from the Safe module
195fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner  std::set<std::pair<std::string, const PointerType*> > TestFunctions;
196be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  for (unsigned i = 0, e = F.size(); i != e; ++i) {
197fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner    TestFunctions.insert(std::make_pair(F[i]->getName(), F[i]->getType()));
198be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    Function *TNOF = M->getFunction(F[i]->getName(), F[i]->getFunctionType());
199be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    DEBUG(std::cerr << "Removing function " << F[i]->getName() << "\n");
200be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    assert(TNOF && "Function doesn't exist in module!");
201be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner    DeleteFunctionBody(TNOF);       // Function is now external in this module!
202be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  }
203be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner
204be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  // Remove the Safe functions from the Test module
205fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner  for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I)
206fb4b96e77e2930bf3d0c148f1c3685b6a4434666Chris Lattner    if (!TestFunctions.count(std::make_pair(I->getName(), I->getType())))
207be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner      DeleteFunctionBody(I);
208be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner  return New;
209be21ca54e08339ede5dd4bbb882182d22e274988Chris Lattner}
2105e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2115e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===//
2125e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner// Basic Block Extraction Code
2135e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner//===----------------------------------------------------------------------===//
2145e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2155e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattnernamespace {
2165e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<BasicBlock*> BlocksToNotExtract;
2175e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2185e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// BlockExtractorPass - This pass is used by bugpoint to extract all blocks
2195e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// from the module into their own functions except for those specified by the
2205e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  /// BlocksToNotExtract list.
221b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner  class BlockExtractorPass : public ModulePass {
222b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattner    bool runOnModule(Module &M);
2235e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  };
2245e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  RegisterOpt<BlockExtractorPass>
2255e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  XX("extract-bbs", "Extract Basic Blocks From Module (for bugpoint use)");
2265e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
2275e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
228b12914bfc0f76a7a48357162d5f4c39a1343e69bChris Lattnerbool BlockExtractorPass::runOnModule(Module &M) {
2295e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::set<BasicBlock*> TranslatedBlocksToNotExtract;
2305e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (unsigned i = 0, e = BlocksToNotExtract.size(); i != e; ++i) {
2315e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    BasicBlock *BB = BlocksToNotExtract[i];
2325e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    Function *F = BB->getParent();
2335e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2345e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Map the corresponding function in this module.
2355e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    Function *MF = M.getFunction(F->getName(), F->getFunctionType());
2365e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2375e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    // Figure out which index the basic block is in its function.
2385e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    Function::iterator BBI = MF->begin();
2395e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::advance(BBI, std::distance(F->begin(), Function::iterator(BB)));
2405e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    TranslatedBlocksToNotExtract.insert(BBI);
2415e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  }
2425e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2435e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Now that we know which blocks to not extract, figure out which ones we WANT
2445e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // to extract.
2455e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<BasicBlock*> BlocksToExtract;
2465e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (Module::iterator F = M.begin(), E = M.end(); F != E; ++F)
2475e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    for (Function::iterator BB = F->begin(), E = F->end(); BB != E; ++BB)
2485e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner      if (!TranslatedBlocksToNotExtract.count(BB))
2495e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner        BlocksToExtract.push_back(BB);
2505e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2515e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  for (unsigned i = 0, e = BlocksToExtract.size(); i != e; ++i)
2525e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    ExtractBasicBlock(BlocksToExtract[i]);
2535e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2545e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  return !BlocksToExtract.empty();
2555e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
2565e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2575e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
2585e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// into their own functions.  The only detail is that M is actually a module
2595e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// cloned from the one the BBs are in, so some mapping needs to be performed.
2605e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// If this operation fails for some reason (ie the implementation is buggy),
2615e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner/// this function should return null, otherwise it returns a new Module.
2625e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris LattnerModule *BugDriver::ExtractMappedBlocksFromModule(const
2635e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                                 std::vector<BasicBlock*> &BBs,
2645e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner                                                 Module *M) {
2655e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  // Set the global list so that pass will be able to access it.
2665e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  BlocksToNotExtract = BBs;
2675e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner
2685e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  std::vector<const PassInfo*> PI;
2695e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  PI.push_back(getPI(new BlockExtractorPass()));
2705e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  Module *Ret = runPassesOn(M, PI);
2715e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  BlocksToNotExtract.clear();
272891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner  if (Ret == 0) {
2735e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner    std::cout << "*** Basic Block extraction failed, please report a bug!\n";
274891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner    M = swapProgramIn(M);
275891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner    EmitProgressBytecode("basicblockextractfail", true);
276891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner    M = swapProgramIn(M);
277891150f0b2a124a75c9f31516182ce864ae69ef8Chris Lattner  }
2785e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner  return Ret;
2795e783ab0b5fc3407ec59f1a598fdb9ef3b96b287Chris Lattner}
280