blob: 68dc3c913f9b3e1d748731416b7bc8a681accc6a [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- ExtractFunction.cpp - Extract a function from Program --------------===//
2//
3// This file implements a method that extracts a function from program, cleans
4// it up, and returns it as a new module.
5//
6//===----------------------------------------------------------------------===//
7
8#include "BugDriver.h"
Misha Brukmane49603d2003-08-07 21:19:30 +00009#include "llvm/Constant.h"
Chris Lattnerafade922002-11-20 22:28:10 +000010#include "llvm/Module.h"
11#include "llvm/PassManager.h"
Brian Gaeked1a85a72003-09-10 21:11:42 +000012#include "llvm/Pass.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000013#include "llvm/Type.h"
14#include "llvm/Analysis/Verifier.h"
Chris Lattnerafade922002-11-20 22:28:10 +000015#include "llvm/Transforms/IPO.h"
Chris Lattner65207852003-01-23 02:48:33 +000016#include "llvm/Transforms/Scalar.h"
Chris Lattnerafade922002-11-20 22:28:10 +000017#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner6db70ef2003-04-25 22:08:12 +000018#include "Support/CommandLine.h"
19
Chris Lattner47ae4a12003-08-05 15:51:05 +000020bool DisableSimplifyCFG = false;
21
Chris Lattner6db70ef2003-04-25 22:08:12 +000022namespace {
23 cl::opt<bool>
24 NoADCE("disable-adce",
25 cl::desc("Do not use the -adce pass to reduce testcases"));
26 cl::opt<bool>
27 NoDCE ("disable-dce",
28 cl::desc("Do not use the -dce pass to reduce testcases"));
Chris Lattner47ae4a12003-08-05 15:51:05 +000029 cl::opt<bool, true>
30 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
Chris Lattner6db70ef2003-04-25 22:08:12 +000031 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000032 cl::opt<bool>
33 NoFinalCleanup("disable-final-cleanup",
34 cl::desc("Disable the final cleanup phase of narrowing"));
Chris Lattner6db70ef2003-04-25 22:08:12 +000035}
Chris Lattnerafade922002-11-20 22:28:10 +000036
Chris Lattner65207852003-01-23 02:48:33 +000037/// 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.
41///
42Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
43 unsigned Simplification) const {
44 Module *Result = CloneModule(Program);
45
46 BasicBlock *PBB = I->getParent();
47 Function *PF = PBB->getParent();
48
49 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
50 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
51
52 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
53 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
54
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!
58
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()));
62
63 // Remove the instruction from the program.
64 I->getParent()->getInstList().erase(I);
65
Chris Lattner44be2572003-04-24 22:53:24 +000066 // Spiff up the output a little bit.
Chris Lattner65207852003-01-23 02:48:33 +000067 PassManager Passes;
Chris Lattner6db70ef2003-04-25 22:08:12 +000068 if (Simplification > 2 && !NoADCE)
Chris Lattner65207852003-01-23 02:48:33 +000069 Passes.add(createAggressiveDCEPass()); // Remove dead code...
70 //Passes.add(createInstructionCombiningPass());
Chris Lattner6db70ef2003-04-25 22:08:12 +000071 if (Simplification > 1 && !NoDCE)
Chris Lattner65207852003-01-23 02:48:33 +000072 Passes.add(createDeadCodeEliminationPass());
Chris Lattner47ae4a12003-08-05 15:51:05 +000073 if (Simplification && !DisableSimplifyCFG)
Chris Lattner65207852003-01-23 02:48:33 +000074 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
Chris Lattner10f22cb2003-03-07 18:17:13 +000075
76 Passes.add(createVerifierPass());
Chris Lattner65207852003-01-23 02:48:33 +000077 Passes.run(*Result);
78 return Result;
79}
Chris Lattnerba386d92003-02-28 16:13:20 +000080
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...
84///
Chris Lattner587a6ce2003-08-01 16:13:49 +000085Module *BugDriver::performFinalCleanups(Module *InM) const {
86 Module *M = InM ? InM : CloneModule(Program);
Chris Lattner28b8ed92003-05-21 19:41:31 +000087
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000088 // Allow disabling these passes if they crash bugpoint.
89 //
90 // FIXME: This should eventually run these passes in a pass list to prevent
91 // them from being able to crash bugpoint at all!
92 //
93 if (NoFinalCleanup) return M;
94
Chris Lattner28b8ed92003-05-21 19:41:31 +000095 // 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);
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000098
Chris Lattnerba386d92003-02-28 16:13:20 +000099 PassManager CleanupPasses;
100 CleanupPasses.add(createFunctionResolvingPass());
101 CleanupPasses.add(createGlobalDCEPass());
Chris Lattner16608b42003-04-25 00:52:30 +0000102 CleanupPasses.add(createDeadTypeEliminationPass());
Chris Lattner587a6ce2003-08-01 16:13:49 +0000103 CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
Chris Lattner10f22cb2003-03-07 18:17:13 +0000104 CleanupPasses.add(createVerifierPass());
Chris Lattnerba386d92003-02-28 16:13:20 +0000105 CleanupPasses.run(*M);
106 return M;
107}