blob: 5451888c4f3816fa48ddcd2ff0b2b38822c9815a [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 Lattnerafade922002-11-20 22:28:10 +000017
Chris Lattner65207852003-01-23 02:48:33 +000018/// deleteInstructionFromProgram - This method clones the current Program and
19/// deletes the specified instruction from the cloned module. It then runs a
20/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
21/// depends on the value. The modified module is then returned.
22///
23Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
24 unsigned Simplification) const {
25 Module *Result = CloneModule(Program);
26
27 BasicBlock *PBB = I->getParent();
28 Function *PF = PBB->getParent();
29
30 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
31 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
32
33 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
34 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
35
36 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
37 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
38 I = RI; // Got the corresponding instruction!
39
40 // If this instruction produces a value, replace any users with null values
41 if (I->getType() != Type::VoidTy)
42 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
43
44 // Remove the instruction from the program.
45 I->getParent()->getInstList().erase(I);
46
Chris Lattner44be2572003-04-24 22:53:24 +000047 // Spiff up the output a little bit.
Chris Lattner65207852003-01-23 02:48:33 +000048 PassManager Passes;
49 if (Simplification > 2)
50 Passes.add(createAggressiveDCEPass()); // Remove dead code...
51 //Passes.add(createInstructionCombiningPass());
52 if (Simplification > 1)
53 Passes.add(createDeadCodeEliminationPass());
54 if (Simplification)
55 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
Chris Lattner10f22cb2003-03-07 18:17:13 +000056
57 Passes.add(createVerifierPass());
Chris Lattner65207852003-01-23 02:48:33 +000058 Passes.run(*Result);
59 return Result;
60}
Chris Lattnerba386d92003-02-28 16:13:20 +000061
62/// performFinalCleanups - This method clones the current Program and performs
63/// a series of cleanups intended to get rid of extra cruft on the module
64/// before handing it to the user...
65///
66Module *BugDriver::performFinalCleanups() const {
Chris Lattner16608b42003-04-25 00:52:30 +000067 Module *M = CloneModule(Program);
Chris Lattnerba386d92003-02-28 16:13:20 +000068 PassManager CleanupPasses;
69 CleanupPasses.add(createFunctionResolvingPass());
70 CleanupPasses.add(createGlobalDCEPass());
Chris Lattner16608b42003-04-25 00:52:30 +000071 CleanupPasses.add(createDeadTypeEliminationPass());
Chris Lattner10f22cb2003-03-07 18:17:13 +000072 CleanupPasses.add(createVerifierPass());
Chris Lattnerba386d92003-02-28 16:13:20 +000073 CleanupPasses.run(*M);
74 return M;
75}