Make more stuff public. Make the instruction argument to
[oota-llvm.git] / tools / bugpoint / ExtractFunction.cpp
1 //===- ExtractFunction.cpp - Extract a function from Program --------------===//
2 // 
3 //                     The LLVM Compiler Infrastructure
4 //
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
7 // 
8 //===----------------------------------------------------------------------===//
9 //
10 // This file implements a method that extracts a function from program, cleans
11 // it up, and returns it as a new module.
12 //
13 //===----------------------------------------------------------------------===//
14
15 #include "BugDriver.h"
16 #include "llvm/Constant.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Type.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "llvm/Transforms/IPO.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Transforms/Utils/Cloning.h"
25 #include "llvm/Target/TargetData.h"
26 #include "Support/CommandLine.h"
27 #include "Support/FileUtilities.h"
28 using namespace llvm;
29
30 namespace llvm {
31   bool DisableSimplifyCFG = false;
32 } // End llvm namespace
33
34 namespace {
35   cl::opt<bool>
36   NoADCE("disable-adce",
37          cl::desc("Do not use the -adce pass to reduce testcases"));
38   cl::opt<bool>
39   NoDCE ("disable-dce",
40          cl::desc("Do not use the -dce pass to reduce testcases"));
41   cl::opt<bool, true>
42   NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
43          cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
44 }
45
46 /// deleteInstructionFromProgram - This method clones the current Program and
47 /// deletes the specified instruction from the cloned module.  It then runs a
48 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
49 /// depends on the value.  The modified module is then returned.
50 ///
51 Module *BugDriver::deleteInstructionFromProgram(const Instruction *I,
52                                                 unsigned Simplification) const {
53   Module *Result = CloneModule(Program);
54
55   const BasicBlock *PBB = I->getParent();
56   const Function *PF = PBB->getParent();
57
58   Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
59   std::advance(RFI, std::distance(PF->getParent()->begin(),
60                                   Module::const_iterator(PF)));
61
62   Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
63   std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
64
65   BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
66   std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
67   Instruction *TheInst = RI;              // Got the corresponding instruction!
68
69   // If this instruction produces a value, replace any users with null values
70   if (TheInst->getType() != Type::VoidTy)
71     TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
72
73   // Remove the instruction from the program.
74   TheInst->getParent()->getInstList().erase(TheInst);
75
76   // Spiff up the output a little bit.
77   PassManager Passes;
78   // Make sure that the appropriate target data is always used...
79   Passes.add(new TargetData("bugpoint", Result));
80
81   if (Simplification > 2 && !NoADCE)
82     Passes.add(createAggressiveDCEPass());          // Remove dead code...
83   //Passes.add(createInstructionCombiningPass());
84   if (Simplification > 1 && !NoDCE)
85     Passes.add(createDeadCodeEliminationPass());
86   if (Simplification && !DisableSimplifyCFG)
87     Passes.add(createCFGSimplificationPass());      // Delete dead control flow
88
89   Passes.add(createVerifierPass());
90   Passes.run(*Result);
91   return Result;
92 }
93
94 static const PassInfo *getPI(Pass *P) {
95   const PassInfo *PI = P->getPassInfo();
96   delete P;
97   return PI;
98 }
99
100 /// performFinalCleanups - This method clones the current Program and performs
101 /// a series of cleanups intended to get rid of extra cruft on the module
102 /// before handing it to the user...
103 ///
104 Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
105   // Make all functions external, so GlobalDCE doesn't delete them...
106   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
107     I->setLinkage(GlobalValue::ExternalLinkage);
108   
109   std::vector<const PassInfo*> CleanupPasses;
110   CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
111   CleanupPasses.push_back(getPI(createGlobalDCEPass()));
112   CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
113
114   if (MayModifySemantics)
115     CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
116   else
117     CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
118   
119   std::swap(Program, M);
120   std::string Filename;
121   bool Failed = runPasses(CleanupPasses, Filename);
122   std::swap(Program, M);
123
124   if (Failed) {
125     std::cerr << "Final cleanups failed.  Sorry.  :(\n";
126   } else {
127     delete M;
128     M = ParseInputFile(Filename);
129     if (M == 0) {
130       std::cerr << getToolName() << ": Error reading bytecode file '"
131                 << Filename << "'!\n";
132       exit(1);
133     }
134     removeFile(Filename);
135   }
136   return M;
137 }