blob: 214833894b1258c652029666ef86dc4bf3d79887 [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"
Misha Brukmane49603d2003-08-07 21:19:30 +000012#include "llvm/Type.h"
13#include "llvm/Analysis/Verifier.h"
Chris Lattnerafade922002-11-20 22:28:10 +000014#include "llvm/Transforms/IPO.h"
Chris Lattner65207852003-01-23 02:48:33 +000015#include "llvm/Transforms/Scalar.h"
Chris Lattnerafade922002-11-20 22:28:10 +000016#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner6db70ef2003-04-25 22:08:12 +000017#include "Support/CommandLine.h"
18
Chris Lattner47ae4a12003-08-05 15:51:05 +000019bool DisableSimplifyCFG = false;
20
Chris Lattner6db70ef2003-04-25 22:08:12 +000021namespace {
22 cl::opt<bool>
23 NoADCE("disable-adce",
24 cl::desc("Do not use the -adce pass to reduce testcases"));
25 cl::opt<bool>
26 NoDCE ("disable-dce",
27 cl::desc("Do not use the -dce pass to reduce testcases"));
Chris Lattner47ae4a12003-08-05 15:51:05 +000028 cl::opt<bool, true>
29 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
Chris Lattner6db70ef2003-04-25 22:08:12 +000030 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000031 cl::opt<bool>
32 NoFinalCleanup("disable-final-cleanup",
33 cl::desc("Disable the final cleanup phase of narrowing"));
Chris Lattner6db70ef2003-04-25 22:08:12 +000034}
Chris Lattnerafade922002-11-20 22:28:10 +000035
Chris Lattner65207852003-01-23 02:48:33 +000036/// 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.
40///
41Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
42 unsigned Simplification) const {
43 Module *Result = CloneModule(Program);
44
45 BasicBlock *PBB = I->getParent();
46 Function *PF = PBB->getParent();
47
48 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
49 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
50
51 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
52 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
53
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!
57
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()));
61
62 // Remove the instruction from the program.
63 I->getParent()->getInstList().erase(I);
64
Chris Lattner44be2572003-04-24 22:53:24 +000065 // Spiff up the output a little bit.
Chris Lattner65207852003-01-23 02:48:33 +000066 PassManager Passes;
Chris Lattner6db70ef2003-04-25 22:08:12 +000067 if (Simplification > 2 && !NoADCE)
Chris Lattner65207852003-01-23 02:48:33 +000068 Passes.add(createAggressiveDCEPass()); // Remove dead code...
69 //Passes.add(createInstructionCombiningPass());
Chris Lattner6db70ef2003-04-25 22:08:12 +000070 if (Simplification > 1 && !NoDCE)
Chris Lattner65207852003-01-23 02:48:33 +000071 Passes.add(createDeadCodeEliminationPass());
Chris Lattner47ae4a12003-08-05 15:51:05 +000072 if (Simplification && !DisableSimplifyCFG)
Chris Lattner65207852003-01-23 02:48:33 +000073 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
Chris Lattner10f22cb2003-03-07 18:17:13 +000074
75 Passes.add(createVerifierPass());
Chris Lattner65207852003-01-23 02:48:33 +000076 Passes.run(*Result);
77 return Result;
78}
Chris Lattnerba386d92003-02-28 16:13:20 +000079
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...
83///
Chris Lattner587a6ce2003-08-01 16:13:49 +000084Module *BugDriver::performFinalCleanups(Module *InM) const {
85 Module *M = InM ? InM : CloneModule(Program);
Chris Lattner28b8ed92003-05-21 19:41:31 +000086
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000087 // Allow disabling these passes if they crash bugpoint.
88 //
89 // FIXME: This should eventually run these passes in a pass list to prevent
90 // them from being able to crash bugpoint at all!
91 //
92 if (NoFinalCleanup) return M;
93
Chris Lattner28b8ed92003-05-21 19:41:31 +000094 // 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);
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000097
Chris Lattnerba386d92003-02-28 16:13:20 +000098 PassManager CleanupPasses;
99 CleanupPasses.add(createFunctionResolvingPass());
100 CleanupPasses.add(createGlobalDCEPass());
Chris Lattner16608b42003-04-25 00:52:30 +0000101 CleanupPasses.add(createDeadTypeEliminationPass());
Chris Lattner587a6ce2003-08-01 16:13:49 +0000102 CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
Chris Lattner10f22cb2003-03-07 18:17:13 +0000103 CleanupPasses.add(createVerifierPass());
Chris Lattnerba386d92003-02-28 16:13:20 +0000104 CleanupPasses.run(*M);
105 return M;
106}