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