1 //===- ExtractFunction.cpp - Extract a function from Program --------------===//
3 // This file implements a method that extracts a function from program, cleans
4 // it up, and returns it as a new module.
6 //===----------------------------------------------------------------------===//
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"
21 NoADCE("disable-adce",
22 cl::desc("Do not use the -adce pass to reduce testcases"));
25 cl::desc("Do not use the -dce pass to reduce testcases"));
27 NoSCFG("disable-simplifycfg",
28 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
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.
36 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
37 unsigned Simplification) const {
38 Module *Result = CloneModule(Program);
40 BasicBlock *PBB = I->getParent();
41 Function *PF = PBB->getParent();
43 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
44 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
46 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
47 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
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!
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()));
57 // Remove the instruction from the program.
58 I->getParent()->getInstList().erase(I);
60 // Spiff up the output a little bit.
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
70 Passes.add(createVerifierPass());
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...
79 Module *BugDriver::performFinalCleanups() const {
80 Module *M = CloneModule(Program);
81 PassManager CleanupPasses;
82 CleanupPasses.add(createFunctionResolvingPass());
83 CleanupPasses.add(createGlobalDCEPass());
84 CleanupPasses.add(createDeadTypeEliminationPass());
85 CleanupPasses.add(createVerifierPass());
86 CleanupPasses.run(*M);