Put all LLVM code into the llvm namespace, as per bug 109.
[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
28
29 namespace llvm {
30
31 bool DisableSimplifyCFG = false;
32
33 } // End llvm namespace
34
35 using namespace llvm;
36
37 namespace {
38   cl::opt<bool>
39   NoADCE("disable-adce",
40          cl::desc("Do not use the -adce pass to reduce testcases"));
41   cl::opt<bool>
42   NoDCE ("disable-dce",
43          cl::desc("Do not use the -dce pass to reduce testcases"));
44   cl::opt<bool, true>
45   NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
46          cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
47 }
48
49 namespace llvm {
50
51 /// deleteInstructionFromProgram - This method clones the current Program and
52 /// deletes the specified instruction from the cloned module.  It then runs a
53 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
54 /// depends on the value.  The modified module is then returned.
55 ///
56 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
57                                                 unsigned Simplification) const {
58   Module *Result = CloneModule(Program);
59
60   BasicBlock *PBB = I->getParent();
61   Function *PF = PBB->getParent();
62
63   Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
64   std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
65
66   Function::iterator RBI = RFI->begin();  // Get iterator to corresponding BB
67   std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
68
69   BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
70   std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
71   I = RI;                                 // Got the corresponding instruction!
72
73   // If this instruction produces a value, replace any users with null values
74   if (I->getType() != Type::VoidTy)
75     I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
76
77   // Remove the instruction from the program.
78   I->getParent()->getInstList().erase(I);
79
80   // Spiff up the output a little bit.
81   PassManager Passes;
82   // Make sure that the appropriate target data is always used...
83   Passes.add(new TargetData("bugpoint", Result));
84
85   if (Simplification > 2 && !NoADCE)
86     Passes.add(createAggressiveDCEPass());          // Remove dead code...
87   //Passes.add(createInstructionCombiningPass());
88   if (Simplification > 1 && !NoDCE)
89     Passes.add(createDeadCodeEliminationPass());
90   if (Simplification && !DisableSimplifyCFG)
91     Passes.add(createCFGSimplificationPass());      // Delete dead control flow
92
93   Passes.add(createVerifierPass());
94   Passes.run(*Result);
95   return Result;
96 }
97
98 static const PassInfo *getPI(Pass *P) {
99   const PassInfo *PI = P->getPassInfo();
100   delete P;
101   return PI;
102 }
103
104 /// performFinalCleanups - This method clones the current Program and performs
105 /// a series of cleanups intended to get rid of extra cruft on the module
106 /// before handing it to the user...
107 ///
108 Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
109   // Make all functions external, so GlobalDCE doesn't delete them...
110   for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
111     I->setLinkage(GlobalValue::ExternalLinkage);
112   
113   std::vector<const PassInfo*> CleanupPasses;
114   CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
115   CleanupPasses.push_back(getPI(createGlobalDCEPass()));
116   CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
117   CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
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   }
135   return M;
136 }
137
138 } // End llvm namespace