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"
18 /// deleteInstructionFromProgram - This method clones the current Program and
19 /// deletes the specified instruction from the cloned module. It then runs a
20 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
21 /// depends on the value. The modified module is then returned.
23 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
24 unsigned Simplification) const {
25 Module *Result = CloneModule(Program);
27 BasicBlock *PBB = I->getParent();
28 Function *PF = PBB->getParent();
30 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
31 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
33 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
34 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
36 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
37 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
38 I = RI; // Got the corresponding instruction!
40 // If this instruction produces a value, replace any users with null values
41 if (I->getType() != Type::VoidTy)
42 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
44 // Remove the instruction from the program.
45 I->getParent()->getInstList().erase(I);
47 // Spiff up the output a little bit.
49 if (Simplification > 2)
50 Passes.add(createAggressiveDCEPass()); // Remove dead code...
51 //Passes.add(createInstructionCombiningPass());
52 if (Simplification > 1)
53 Passes.add(createDeadCodeEliminationPass());
55 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
57 Passes.add(createVerifierPass());
62 /// performFinalCleanups - This method clones the current Program and performs
63 /// a series of cleanups intended to get rid of extra cruft on the module
64 /// before handing it to the user...
66 Module *BugDriver::performFinalCleanups() const {
67 Module *M = CloneModule(Program);
68 PassManager CleanupPasses;
69 CleanupPasses.add(createFunctionResolvingPass());
70 CleanupPasses.add(createGlobalDCEPass());
71 CleanupPasses.add(createDeadTypeEliminationPass());
72 CleanupPasses.add(createVerifierPass());
73 CleanupPasses.run(*M);