1 //===- ExtractFunction.cpp - Extract a function from Program --------------===//
3 // The LLVM Compiler Infrastructure
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.
8 //===----------------------------------------------------------------------===//
10 // This file implements a method that extracts a function from program, cleans
11 // it up, and returns it as a new module.
13 //===----------------------------------------------------------------------===//
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"
31 bool DisableSimplifyCFG = false;
32 } // End llvm namespace
36 NoADCE("disable-adce",
37 cl::desc("Do not use the -adce pass to reduce testcases"));
40 cl::desc("Do not use the -dce pass to reduce testcases"));
42 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
43 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
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.
51 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
52 unsigned Simplification) const {
53 Module *Result = CloneModule(Program);
55 BasicBlock *PBB = I->getParent();
56 Function *PF = PBB->getParent();
58 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
59 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
61 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
62 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
64 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
65 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
66 I = RI; // Got the corresponding instruction!
68 // If this instruction produces a value, replace any users with null values
69 if (I->getType() != Type::VoidTy)
70 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
72 // Remove the instruction from the program.
73 I->getParent()->getInstList().erase(I);
75 // Spiff up the output a little bit.
77 // Make sure that the appropriate target data is always used...
78 Passes.add(new TargetData("bugpoint", Result));
80 if (Simplification > 2 && !NoADCE)
81 Passes.add(createAggressiveDCEPass()); // Remove dead code...
82 //Passes.add(createInstructionCombiningPass());
83 if (Simplification > 1 && !NoDCE)
84 Passes.add(createDeadCodeEliminationPass());
85 if (Simplification && !DisableSimplifyCFG)
86 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
88 Passes.add(createVerifierPass());
93 static const PassInfo *getPI(Pass *P) {
94 const PassInfo *PI = P->getPassInfo();
99 /// performFinalCleanups - This method clones the current Program and performs
100 /// a series of cleanups intended to get rid of extra cruft on the module
101 /// before handing it to the user...
103 Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
104 // Make all functions external, so GlobalDCE doesn't delete them...
105 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
106 I->setLinkage(GlobalValue::ExternalLinkage);
108 std::vector<const PassInfo*> CleanupPasses;
109 CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
110 CleanupPasses.push_back(getPI(createGlobalDCEPass()));
111 CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
113 if (MayModifySemantics)
114 CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
116 CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
118 std::swap(Program, M);
119 std::string Filename;
120 bool Failed = runPasses(CleanupPasses, Filename);
121 std::swap(Program, M);
124 std::cerr << "Final cleanups failed. Sorry. :(\n";
127 M = ParseInputFile(Filename);
129 std::cerr << getToolName() << ": Error reading bytecode file '"
130 << Filename << "'!\n";
133 removeFile(Filename);