1 //===- ExtractFunction.cpp - Extract a function from Program --------------===//
3 // The LLVM Compiler Infrastructure
5 // This file was developed by the LLVM research group and is distributed under
6 // the University of Illinois Open Source License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file implements a method that extracts a function from program, cleans
11 // it up, and returns it as a new module.
13 //===----------------------------------------------------------------------===//
15 #include "BugDriver.h"
16 #include "llvm/Constant.h"
17 #include "llvm/Module.h"
18 #include "llvm/PassManager.h"
19 #include "llvm/Pass.h"
20 #include "llvm/Type.h"
21 #include "llvm/Analysis/Verifier.h"
22 #include "llvm/Transforms/IPO.h"
23 #include "llvm/Transforms/Scalar.h"
24 #include "llvm/Transforms/Utils/Cloning.h"
25 #include "llvm/Target/TargetData.h"
26 #include "Support/CommandLine.h"
28 bool DisableSimplifyCFG = false;
32 NoADCE("disable-adce",
33 cl::desc("Do not use the -adce pass to reduce testcases"));
36 cl::desc("Do not use the -dce pass to reduce testcases"));
38 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
39 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
41 NoFinalCleanup("disable-final-cleanup",
42 cl::desc("Disable the final cleanup phase of narrowing"));
45 /// deleteInstructionFromProgram - This method clones the current Program and
46 /// deletes the specified instruction from the cloned module. It then runs a
47 /// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
48 /// depends on the value. The modified module is then returned.
50 Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
51 unsigned Simplification) const {
52 Module *Result = CloneModule(Program);
54 BasicBlock *PBB = I->getParent();
55 Function *PF = PBB->getParent();
57 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
58 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
60 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
61 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
63 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
64 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
65 I = RI; // Got the corresponding instruction!
67 // If this instruction produces a value, replace any users with null values
68 if (I->getType() != Type::VoidTy)
69 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
71 // Remove the instruction from the program.
72 I->getParent()->getInstList().erase(I);
74 // Spiff up the output a little bit.
76 // Make sure that the appropriate target data is always used...
77 Passes.add(new TargetData("bugpoint", Result));
79 if (Simplification > 2 && !NoADCE)
80 Passes.add(createAggressiveDCEPass()); // Remove dead code...
81 //Passes.add(createInstructionCombiningPass());
82 if (Simplification > 1 && !NoDCE)
83 Passes.add(createDeadCodeEliminationPass());
84 if (Simplification && !DisableSimplifyCFG)
85 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
87 Passes.add(createVerifierPass());
92 /// performFinalCleanups - This method clones the current Program and performs
93 /// a series of cleanups intended to get rid of extra cruft on the module
94 /// before handing it to the user...
96 Module *BugDriver::performFinalCleanups(Module *InM) const {
97 Module *M = InM ? InM : CloneModule(Program);
99 // Allow disabling these passes if they crash bugpoint.
101 // FIXME: This should eventually run these passes in a pass list to prevent
102 // them from being able to crash bugpoint at all!
104 if (NoFinalCleanup) return M;
106 // Make all functions external, so GlobalDCE doesn't delete them...
107 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
108 I->setLinkage(GlobalValue::ExternalLinkage);
110 PassManager CleanupPasses;
111 // Make sure that the appropriate target data is always used...
112 CleanupPasses.add(new TargetData("bugpoint", M));
113 CleanupPasses.add(createFunctionResolvingPass());
114 CleanupPasses.add(createGlobalDCEPass());
115 CleanupPasses.add(createDeadTypeEliminationPass());
116 CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
117 CleanupPasses.add(createVerifierPass());
118 CleanupPasses.run(*M);