If we're debugging the SimplifyCFG pass, we _REALLY_ don't want to use it for
[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 bool DisableSimplifyCFG = false;
20
21 namespace {
22   cl::opt<bool>
23   NoADCE("disable-adce",
24          cl::desc("Do not use the -adce pass to reduce testcases"));
25   cl::opt<bool>
26   NoDCE ("disable-dce",
27          cl::desc("Do not use the -dce pass to reduce testcases"));
28   cl::opt<bool, true>
29   NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
30          cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
31   cl::opt<bool>
32   NoFinalCleanup("disable-final-cleanup",
33                  cl::desc("Disable the final cleanup phase of narrowing"));
34 }
35
36 /// deleteInstructionFromProgram - This method clones the current Program and
37 /// deletes the specified instruction from the cloned module.  It then runs a
38 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
39 /// depends on the value.  The modified module is then returned.
40 ///
41 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
42                                                 unsigned Simplification) const {
43   Module *Result = CloneModule(Program);
44
45   BasicBlock *PBB = I->getParent();
46   Function *PF = PBB->getParent();
47
48   Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
49   std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
50
51   Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
52   std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
53
54   BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
55   std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
56   I = RI;                                 // Got the corresponding instruction!
57
58   // If this instruction produces a value, replace any users with null values
59   if (I->getType() != Type::VoidTy)
60     I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
61
62   // Remove the instruction from the program.
63   I->getParent()->getInstList().erase(I);
64
65   // Spiff up the output a little bit.
66   PassManager Passes;
67   if (Simplification > 2 && !NoADCE)
68     Passes.add(createAggressiveDCEPass());          // Remove dead code...
69   //Passes.add(createInstructionCombiningPass());
70   if (Simplification > 1 && !NoDCE)
71     Passes.add(createDeadCodeEliminationPass());
72   if (Simplification && !DisableSimplifyCFG)
73     Passes.add(createCFGSimplificationPass());      // Delete dead control flow
74
75   Passes.add(createVerifierPass());
76   Passes.run(*Result);
77   return Result;
78 }
79
80 /// performFinalCleanups - This method clones the current Program and performs
81 /// a series of cleanups intended to get rid of extra cruft on the module
82 /// before handing it to the user...
83 ///
84 Module *BugDriver::performFinalCleanups(Module *InM) const {
85   Module *M = InM ? InM : CloneModule(Program);
86
87   // Allow disabling these passes if they crash bugpoint.
88   //
89   // FIXME: This should eventually run these passes in a pass list to prevent
90   // them from being able to crash bugpoint at all!
91   //
92   if (NoFinalCleanup) return M;
93
94   // Make all functions external, so GlobalDCE doesn't delete them...
95   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
96     I->setLinkage(GlobalValue::ExternalLinkage);
97   
98   PassManager CleanupPasses;
99   CleanupPasses.add(createFunctionResolvingPass());
100   CleanupPasses.add(createGlobalDCEPass());
101   CleanupPasses.add(createDeadTypeEliminationPass());
102   CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
103   CleanupPasses.add(createVerifierPass());
104   CleanupPasses.run(*M);
105   return M;
106 }