blob: ac893629fb5427d0601d0eaf134df8365c3140bc [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"
9#include "llvm/Module.h"
10#include "llvm/PassManager.h"
11#include "llvm/Transforms/IPO.h"
Chris Lattner65207852003-01-23 02:48:33 +000012#include "llvm/Transforms/Scalar.h"
Chris Lattnerafade922002-11-20 22:28:10 +000013#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner10f22cb2003-03-07 18:17:13 +000014#include "llvm/Analysis/Verifier.h"
Chris Lattner65207852003-01-23 02:48:33 +000015#include "llvm/Type.h"
16#include "llvm/Constant.h"
Chris Lattner6db70ef2003-04-25 22:08:12 +000017#include "Support/CommandLine.h"
18
19namespace {
20 cl::opt<bool>
21 NoADCE("disable-adce",
22 cl::desc("Do not use the -adce pass to reduce testcases"));
23 cl::opt<bool>
24 NoDCE ("disable-dce",
25 cl::desc("Do not use the -dce pass to reduce testcases"));
26 cl::opt<bool>
27 NoSCFG("disable-simplifycfg",
28 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
29}
Chris Lattnerafade922002-11-20 22:28:10 +000030
Chris Lattner65207852003-01-23 02:48:33 +000031/// deleteInstructionFromProgram - This method clones the current Program and
32/// deletes the specified instruction from the cloned module. It then runs a
33/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
34/// depends on the value. The modified module is then returned.
35///
36Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
37 unsigned Simplification) const {
38 Module *Result = CloneModule(Program);
39
40 BasicBlock *PBB = I->getParent();
41 Function *PF = PBB->getParent();
42
43 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
44 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
45
46 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
47 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
48
49 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
50 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
51 I = RI; // Got the corresponding instruction!
52
53 // If this instruction produces a value, replace any users with null values
54 if (I->getType() != Type::VoidTy)
55 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
56
57 // Remove the instruction from the program.
58 I->getParent()->getInstList().erase(I);
59
Chris Lattner44be2572003-04-24 22:53:24 +000060 // Spiff up the output a little bit.
Chris Lattner65207852003-01-23 02:48:33 +000061 PassManager Passes;
Chris Lattner6db70ef2003-04-25 22:08:12 +000062 if (Simplification > 2 && !NoADCE)
Chris Lattner65207852003-01-23 02:48:33 +000063 Passes.add(createAggressiveDCEPass()); // Remove dead code...
64 //Passes.add(createInstructionCombiningPass());
Chris Lattner6db70ef2003-04-25 22:08:12 +000065 if (Simplification > 1 && !NoDCE)
Chris Lattner65207852003-01-23 02:48:33 +000066 Passes.add(createDeadCodeEliminationPass());
Chris Lattner6db70ef2003-04-25 22:08:12 +000067 if (Simplification && !NoSCFG)
Chris Lattner65207852003-01-23 02:48:33 +000068 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
Chris Lattner10f22cb2003-03-07 18:17:13 +000069
70 Passes.add(createVerifierPass());
Chris Lattner65207852003-01-23 02:48:33 +000071 Passes.run(*Result);
72 return Result;
73}
Chris Lattnerba386d92003-02-28 16:13:20 +000074
75/// performFinalCleanups - This method clones the current Program and performs
76/// a series of cleanups intended to get rid of extra cruft on the module
77/// before handing it to the user...
78///
79Module *BugDriver::performFinalCleanups() const {
Chris Lattner16608b42003-04-25 00:52:30 +000080 Module *M = CloneModule(Program);
Chris Lattner28b8ed92003-05-21 19:41:31 +000081
82 // Make all functions external, so GlobalDCE doesn't delete them...
83 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
84 I->setLinkage(GlobalValue::ExternalLinkage);
85
Chris Lattnerba386d92003-02-28 16:13:20 +000086 PassManager CleanupPasses;
87 CleanupPasses.add(createFunctionResolvingPass());
88 CleanupPasses.add(createGlobalDCEPass());
Chris Lattner16608b42003-04-25 00:52:30 +000089 CleanupPasses.add(createDeadTypeEliminationPass());
Chris Lattner10f22cb2003-03-07 18:17:13 +000090 CleanupPasses.add(createVerifierPass());
Chris Lattnerba386d92003-02-28 16:13:20 +000091 CleanupPasses.run(*M);
92 return M;
93}