Increase odds that this won't bork things
[oota-llvm.git] / tools / bugpoint / ExtractFunction.cpp
1 //===- ExtractFunction.cpp - Extract a function from Program --------------===//
2 //
3 // This file implements a method that extracts a function from program, cleans
4 // it up, and returns it as a new module.
5 //
6 //===----------------------------------------------------------------------===//
7
8 #include "BugDriver.h"
9 #include "llvm/Module.h"
10 #include "llvm/PassManager.h"
11 #include "llvm/Transforms/IPO.h"
12 #include "llvm/Transforms/Scalar.h"
13 #include "llvm/Transforms/Utils/Cloning.h"
14 #include "llvm/Analysis/Verifier.h"
15 #include "llvm/Type.h"
16 #include "llvm/Constant.h"
17 #include "Support/CommandLine.h"
18
19 namespace {
20   cl::opt<bool>
21   NoADCE("disable-adce",
22          cl::desc("Do not use the -adce pass to reduce testcases"));
23   cl::opt<bool>
24   NoDCE ("disable-dce",
25          cl::desc("Do not use the -dce pass to reduce testcases"));
26   cl::opt<bool>
27   NoSCFG("disable-simplifycfg",
28          cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
29 }
30
31 /// deleteInstructionFromProgram - This method clones the current Program and
32 /// deletes the specified instruction from the cloned module.  It then runs a
33 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
34 /// depends on the value.  The modified module is then returned.
35 ///
36 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
37                                                 unsigned Simplification) const {
38   Module *Result = CloneModule(Program);
39
40   BasicBlock *PBB = I->getParent();
41   Function *PF = PBB->getParent();
42
43   Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
44   std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
45
46   Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
47   std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
48
49   BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
50   std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
51   I = RI;                                 // Got the corresponding instruction!
52
53   // If this instruction produces a value, replace any users with null values
54   if (I->getType() != Type::VoidTy)
55     I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
56
57   // Remove the instruction from the program.
58   I->getParent()->getInstList().erase(I);
59
60   // Spiff up the output a little bit.
61   PassManager Passes;
62   if (Simplification > 2 && !NoADCE)
63     Passes.add(createAggressiveDCEPass());          // Remove dead code...
64   //Passes.add(createInstructionCombiningPass());
65   if (Simplification > 1 && !NoDCE)
66     Passes.add(createDeadCodeEliminationPass());
67   if (Simplification && !NoSCFG)
68     Passes.add(createCFGSimplificationPass());      // Delete dead control flow
69
70   Passes.add(createVerifierPass());
71   Passes.run(*Result);
72   return Result;
73 }
74
75 /// performFinalCleanups - This method clones the current Program and performs
76 /// a series of cleanups intended to get rid of extra cruft on the module
77 /// before handing it to the user...
78 ///
79 Module *BugDriver::performFinalCleanups() const {
80   Module *M = CloneModule(Program);
81
82   // Make all functions external, so GlobalDCE doesn't delete them...
83   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
84     I->setLinkage(GlobalValue::ExternalLinkage);
85
86   PassManager CleanupPasses;
87   CleanupPasses.add(createFunctionResolvingPass());
88   CleanupPasses.add(createGlobalDCEPass());
89   CleanupPasses.add(createDeadTypeEliminationPass());
90   CleanupPasses.add(createVerifierPass());
91   CleanupPasses.run(*M);
92   return M;
93 }