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"));
30 NoFinalCleanup("disable-final-cleanup",
31 cl::desc("Disable the final cleanup phase of narrowing"));
34 /// deleteInstructionFromProgram - This method clones the current Program and
35 /// deletes the specified instruction from the cloned module. It then runs a
36 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
37 /// depends on the value. The modified module is then returned.
39 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
40 unsigned Simplification) const {
41 Module *Result = CloneModule(Program);
43 BasicBlock *PBB = I->getParent();
44 Function *PF = PBB->getParent();
46 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
47 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
49 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
50 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
52 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
53 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
54 I = RI; // Got the corresponding instruction!
56 // If this instruction produces a value, replace any users with null values
57 if (I->getType() != Type::VoidTy)
58 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
60 // Remove the instruction from the program.
61 I->getParent()->getInstList().erase(I);
63 // Spiff up the output a little bit.
65 if (Simplification > 2 && !NoADCE)
66 Passes.add(createAggressiveDCEPass()); // Remove dead code...
67 //Passes.add(createInstructionCombiningPass());
68 if (Simplification > 1 && !NoDCE)
69 Passes.add(createDeadCodeEliminationPass());
70 if (Simplification && !NoSCFG)
71 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
73 Passes.add(createVerifierPass());
78 /// performFinalCleanups - This method clones the current Program and performs
79 /// a series of cleanups intended to get rid of extra cruft on the module
80 /// before handing it to the user...
82 Module *BugDriver::performFinalCleanups(Module *InM) const {
83 Module *M = InM ? InM : CloneModule(Program);
85 // Allow disabling these passes if they crash bugpoint.
87 // FIXME: This should eventually run these passes in a pass list to prevent
88 // them from being able to crash bugpoint at all!
90 if (NoFinalCleanup) return M;
92 // Make all functions external, so GlobalDCE doesn't delete them...
93 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
94 I->setLinkage(GlobalValue::ExternalLinkage);
96 PassManager CleanupPasses;
97 CleanupPasses.add(createFunctionResolvingPass());
98 CleanupPasses.add(createGlobalDCEPass());
99 CleanupPasses.add(createDeadTypeEliminationPass());
100 CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
101 CleanupPasses.add(createVerifierPass());
102 CleanupPasses.run(*M);