ExtractFunction.cpp revision 47ae4a1cee5eec5767a11403c0fac7c91ec45461
1afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===- ExtractFunction.cpp - Extract a function from Program --------------===//
2afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
3afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// This file implements a method that extracts a function from program, cleans
4afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner// it up, and returns it as a new module.
5afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//
6afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner//===----------------------------------------------------------------------===//
7afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
8afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "BugDriver.h"
9afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Module.h"
10afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/PassManager.h"
11afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/IPO.h"
126520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Transforms/Scalar.h"
13afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner#include "llvm/Transforms/Utils/Cloning.h"
1410f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner#include "llvm/Analysis/Verifier.h"
156520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Type.h"
166520785dcd22012535934098942d57c07c7631c2Chris Lattner#include "llvm/Constant.h"
176db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner#include "Support/CommandLine.h"
186db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner
1947ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattnerbool DisableSimplifyCFG = false;
2047ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner
216db70ef879916e6115ac97eb76e4fea973652e2cChris Lattnernamespace {
226db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  cl::opt<bool>
236db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  NoADCE("disable-adce",
246db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -adce pass to reduce testcases"));
256db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  cl::opt<bool>
266db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  NoDCE ("disable-dce",
276db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -dce pass to reduce testcases"));
2847ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  cl::opt<bool, true>
2947ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
306db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner         cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
31dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  cl::opt<bool>
32dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  NoFinalCleanup("disable-final-cleanup",
33dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner                 cl::desc("Disable the final cleanup phase of narrowing"));
346db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner}
35afade9294af43c6b947b9aeaa1555883d5f853e3Chris Lattner
366520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deleteInstructionFromProgram - This method clones the current Program and
376520785dcd22012535934098942d57c07c7631c2Chris Lattner/// deletes the specified instruction from the cloned module.  It then runs a
386520785dcd22012535934098942d57c07c7631c2Chris Lattner/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
396520785dcd22012535934098942d57c07c7631c2Chris Lattner/// depends on the value.  The modified module is then returned.
406520785dcd22012535934098942d57c07c7631c2Chris Lattner///
416520785dcd22012535934098942d57c07c7631c2Chris LattnerModule *BugDriver::deleteInstructionFromProgram(Instruction *I,
426520785dcd22012535934098942d57c07c7631c2Chris Lattner                                                unsigned Simplification) const {
436520785dcd22012535934098942d57c07c7631c2Chris Lattner  Module *Result = CloneModule(Program);
446520785dcd22012535934098942d57c07c7631c2Chris Lattner
456520785dcd22012535934098942d57c07c7631c2Chris Lattner  BasicBlock *PBB = I->getParent();
466520785dcd22012535934098942d57c07c7631c2Chris Lattner  Function *PF = PBB->getParent();
476520785dcd22012535934098942d57c07c7631c2Chris Lattner
486520785dcd22012535934098942d57c07c7631c2Chris Lattner  Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
496520785dcd22012535934098942d57c07c7631c2Chris Lattner  std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
506520785dcd22012535934098942d57c07c7631c2Chris Lattner
516520785dcd22012535934098942d57c07c7631c2Chris Lattner  Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
526520785dcd22012535934098942d57c07c7631c2Chris Lattner  std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
536520785dcd22012535934098942d57c07c7631c2Chris Lattner
546520785dcd22012535934098942d57c07c7631c2Chris Lattner  BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
556520785dcd22012535934098942d57c07c7631c2Chris Lattner  std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
566520785dcd22012535934098942d57c07c7631c2Chris Lattner  I = RI;                                 // Got the corresponding instruction!
576520785dcd22012535934098942d57c07c7631c2Chris Lattner
586520785dcd22012535934098942d57c07c7631c2Chris Lattner  // If this instruction produces a value, replace any users with null values
596520785dcd22012535934098942d57c07c7631c2Chris Lattner  if (I->getType() != Type::VoidTy)
606520785dcd22012535934098942d57c07c7631c2Chris Lattner    I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
616520785dcd22012535934098942d57c07c7631c2Chris Lattner
626520785dcd22012535934098942d57c07c7631c2Chris Lattner  // Remove the instruction from the program.
636520785dcd22012535934098942d57c07c7631c2Chris Lattner  I->getParent()->getInstList().erase(I);
646520785dcd22012535934098942d57c07c7631c2Chris Lattner
6544be25716628941b4cccccf56a28ee0ba2606850Chris Lattner  // Spiff up the output a little bit.
666520785dcd22012535934098942d57c07c7631c2Chris Lattner  PassManager Passes;
676db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  if (Simplification > 2 && !NoADCE)
686520785dcd22012535934098942d57c07c7631c2Chris Lattner    Passes.add(createAggressiveDCEPass());          // Remove dead code...
696520785dcd22012535934098942d57c07c7631c2Chris Lattner  //Passes.add(createInstructionCombiningPass());
706db70ef879916e6115ac97eb76e4fea973652e2cChris Lattner  if (Simplification > 1 && !NoDCE)
716520785dcd22012535934098942d57c07c7631c2Chris Lattner    Passes.add(createDeadCodeEliminationPass());
7247ae4a1cee5eec5767a11403c0fac7c91ec45461Chris Lattner  if (Simplification && !DisableSimplifyCFG)
736520785dcd22012535934098942d57c07c7631c2Chris Lattner    Passes.add(createCFGSimplificationPass());      // Delete dead control flow
7410f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner
7510f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner  Passes.add(createVerifierPass());
766520785dcd22012535934098942d57c07c7631c2Chris Lattner  Passes.run(*Result);
776520785dcd22012535934098942d57c07c7631c2Chris Lattner  return Result;
786520785dcd22012535934098942d57c07c7631c2Chris Lattner}
79ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner
80ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// performFinalCleanups - This method clones the current Program and performs
81ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// a series of cleanups intended to get rid of extra cruft on the module
82ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner/// before handing it to the user...
83ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner///
84587a6ce4b99346b5e785d0a5c360c6c6687b036aChris LattnerModule *BugDriver::performFinalCleanups(Module *InM) const {
85587a6ce4b99346b5e785d0a5c360c6c6687b036aChris Lattner  Module *M = InM ? InM : CloneModule(Program);
8628b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner
87dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  // Allow disabling these passes if they crash bugpoint.
88dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  //
89dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  // FIXME: This should eventually run these passes in a pass list to prevent
90dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  // them from being able to crash bugpoint at all!
91dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  //
92dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner  if (NoFinalCleanup) return M;
93dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner
9428b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  // Make all functions external, so GlobalDCE doesn't delete them...
9528b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner  for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
9628b8ed90c75ce6c271500fa778fef252f267a5ffChris Lattner    I->setLinkage(GlobalValue::ExternalLinkage);
97dbe48dcaec69ff78e39e2d5faf4323ade6fffb04Chris Lattner
98ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  PassManager CleanupPasses;
99ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  CleanupPasses.add(createFunctionResolvingPass());
100ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  CleanupPasses.add(createGlobalDCEPass());
10116608b4c853ba883ac4bf5c642c4a9b4977a4fbaChris Lattner  CleanupPasses.add(createDeadTypeEliminationPass());
102587a6ce4b99346b5e785d0a5c360c6c6687b036aChris Lattner  CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
10310f22cb1a0f2755050218cd0e07221a0985c6b63Chris Lattner  CleanupPasses.add(createVerifierPass());
104ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  CleanupPasses.run(*M);
105ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner  return M;
106ba386d943f4a83095d9c625cb0d46c1afe45ed1fChris Lattner}
107