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/Constant.h"
10 #include "llvm/Module.h"
11 #include "llvm/PassManager.h"
12 #include "llvm/Pass.h"
13 #include "llvm/Type.h"
14 #include "llvm/Analysis/Verifier.h"
15 #include "llvm/Transforms/IPO.h"
16 #include "llvm/Transforms/Scalar.h"
17 #include "llvm/Transforms/Utils/Cloning.h"
18 #include "Support/CommandLine.h"
20 bool DisableSimplifyCFG = false;
24 NoADCE("disable-adce",
25 cl::desc("Do not use the -adce pass to reduce testcases"));
28 cl::desc("Do not use the -dce pass to reduce testcases"));
30 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
31 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
33 NoFinalCleanup("disable-final-cleanup",
34 cl::desc("Disable the final cleanup phase of narrowing"));
37 /// deleteInstructionFromProgram - This method clones the current Program and
38 /// deletes the specified instruction from the cloned module. It then runs a
39 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
40 /// depends on the value. The modified module is then returned.
42 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
43 unsigned Simplification) const {
44 Module *Result = CloneModule(Program);
46 BasicBlock *PBB = I->getParent();
47 Function *PF = PBB->getParent();
49 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
50 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
52 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
53 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
55 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
56 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
57 I = RI; // Got the corresponding instruction!
59 // If this instruction produces a value, replace any users with null values
60 if (I->getType() != Type::VoidTy)
61 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
63 // Remove the instruction from the program.
64 I->getParent()->getInstList().erase(I);
66 // Spiff up the output a little bit.
68 if (Simplification > 2 && !NoADCE)
69 Passes.add(createAggressiveDCEPass()); // Remove dead code...
70 //Passes.add(createInstructionCombiningPass());
71 if (Simplification > 1 && !NoDCE)
72 Passes.add(createDeadCodeEliminationPass());
73 if (Simplification && !DisableSimplifyCFG)
74 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
76 Passes.add(createVerifierPass());
81 /// performFinalCleanups - This method clones the current Program and performs
82 /// a series of cleanups intended to get rid of extra cruft on the module
83 /// before handing it to the user...
85 Module *BugDriver::performFinalCleanups(Module *InM) const {
86 Module *M = InM ? InM : CloneModule(Program);
88 // Allow disabling these passes if they crash bugpoint.
90 // FIXME: This should eventually run these passes in a pass list to prevent
91 // them from being able to crash bugpoint at all!
93 if (NoFinalCleanup) return M;
95 // Make all functions external, so GlobalDCE doesn't delete them...
96 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
97 I->setLinkage(GlobalValue::ExternalLinkage);
99 PassManager CleanupPasses;
100 CleanupPasses.add(createFunctionResolvingPass());
101 CleanupPasses.add(createGlobalDCEPass());
102 CleanupPasses.add(createDeadTypeEliminationPass());
103 CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
104 CleanupPasses.add(createVerifierPass());
105 CleanupPasses.run(*M);