blob: f031d34dec2c86536944888e5ef478ea724885d2 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- ExtractFunction.cpp - Extract a function from Program --------------===//
John Criswell7c0e0222003-10-20 17:47:21 +00002//
3// The LLVM Compiler Infrastructure
4//
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.
7//
8//===----------------------------------------------------------------------===//
Chris Lattnerafade922002-11-20 22:28:10 +00009//
10// This file implements a method that extracts a function from program, cleans
11// it up, and returns it as a new module.
12//
13//===----------------------------------------------------------------------===//
14
15#include "BugDriver.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000016#include "llvm/Constant.h"
Chris Lattnerafade922002-11-20 22:28:10 +000017#include "llvm/Module.h"
18#include "llvm/PassManager.h"
Brian Gaeked1a85a72003-09-10 21:11:42 +000019#include "llvm/Pass.h"
Misha Brukmane49603d2003-08-07 21:19:30 +000020#include "llvm/Type.h"
21#include "llvm/Analysis/Verifier.h"
Chris Lattnerafade922002-11-20 22:28:10 +000022#include "llvm/Transforms/IPO.h"
Chris Lattner65207852003-01-23 02:48:33 +000023#include "llvm/Transforms/Scalar.h"
Chris Lattnerafade922002-11-20 22:28:10 +000024#include "llvm/Transforms/Utils/Cloning.h"
Chris Lattner6db70ef2003-04-25 22:08:12 +000025#include "Support/CommandLine.h"
26
Chris Lattner47ae4a12003-08-05 15:51:05 +000027bool DisableSimplifyCFG = false;
28
Chris Lattner6db70ef2003-04-25 22:08:12 +000029namespace {
30 cl::opt<bool>
31 NoADCE("disable-adce",
32 cl::desc("Do not use the -adce pass to reduce testcases"));
33 cl::opt<bool>
34 NoDCE ("disable-dce",
35 cl::desc("Do not use the -dce pass to reduce testcases"));
Chris Lattner47ae4a12003-08-05 15:51:05 +000036 cl::opt<bool, true>
37 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
Chris Lattner6db70ef2003-04-25 22:08:12 +000038 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000039 cl::opt<bool>
40 NoFinalCleanup("disable-final-cleanup",
41 cl::desc("Disable the final cleanup phase of narrowing"));
Chris Lattner6db70ef2003-04-25 22:08:12 +000042}
Chris Lattnerafade922002-11-20 22:28:10 +000043
Chris Lattner65207852003-01-23 02:48:33 +000044/// deleteInstructionFromProgram - This method clones the current Program and
45/// deletes the specified instruction from the cloned module. It then runs a
46/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
47/// depends on the value. The modified module is then returned.
48///
49Module *BugDriver::deleteInstructionFromProgram(Instruction *I,
50 unsigned Simplification) const {
51 Module *Result = CloneModule(Program);
52
53 BasicBlock *PBB = I->getParent();
54 Function *PF = PBB->getParent();
55
56 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
57 std::advance(RFI, std::distance(Program->begin(), Module::iterator(PF)));
58
59 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
60 std::advance(RBI, std::distance(PF->begin(), Function::iterator(PBB)));
61
62 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
63 std::advance(RI, std::distance(PBB->begin(), BasicBlock::iterator(I)));
64 I = RI; // Got the corresponding instruction!
65
66 // If this instruction produces a value, replace any users with null values
67 if (I->getType() != Type::VoidTy)
68 I->replaceAllUsesWith(Constant::getNullValue(I->getType()));
69
70 // Remove the instruction from the program.
71 I->getParent()->getInstList().erase(I);
72
Chris Lattner44be2572003-04-24 22:53:24 +000073 // Spiff up the output a little bit.
Chris Lattner65207852003-01-23 02:48:33 +000074 PassManager Passes;
Chris Lattner6db70ef2003-04-25 22:08:12 +000075 if (Simplification > 2 && !NoADCE)
Chris Lattner65207852003-01-23 02:48:33 +000076 Passes.add(createAggressiveDCEPass()); // Remove dead code...
77 //Passes.add(createInstructionCombiningPass());
Chris Lattner6db70ef2003-04-25 22:08:12 +000078 if (Simplification > 1 && !NoDCE)
Chris Lattner65207852003-01-23 02:48:33 +000079 Passes.add(createDeadCodeEliminationPass());
Chris Lattner47ae4a12003-08-05 15:51:05 +000080 if (Simplification && !DisableSimplifyCFG)
Chris Lattner65207852003-01-23 02:48:33 +000081 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
Chris Lattner10f22cb2003-03-07 18:17:13 +000082
83 Passes.add(createVerifierPass());
Chris Lattner65207852003-01-23 02:48:33 +000084 Passes.run(*Result);
85 return Result;
86}
Chris Lattnerba386d92003-02-28 16:13:20 +000087
88/// performFinalCleanups - This method clones the current Program and performs
89/// a series of cleanups intended to get rid of extra cruft on the module
90/// before handing it to the user...
91///
Chris Lattner587a6ce2003-08-01 16:13:49 +000092Module *BugDriver::performFinalCleanups(Module *InM) const {
93 Module *M = InM ? InM : CloneModule(Program);
Chris Lattner28b8ed92003-05-21 19:41:31 +000094
Chris Lattnerdbe48dc2003-05-21 20:38:59 +000095 // Allow disabling these passes if they crash bugpoint.
96 //
97 // FIXME: This should eventually run these passes in a pass list to prevent
98 // them from being able to crash bugpoint at all!
99 //
100 if (NoFinalCleanup) return M;
101
Chris Lattner28b8ed92003-05-21 19:41:31 +0000102 // Make all functions external, so GlobalDCE doesn't delete them...
103 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
104 I->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattnerdbe48dc2003-05-21 20:38:59 +0000105
Chris Lattnerba386d92003-02-28 16:13:20 +0000106 PassManager CleanupPasses;
107 CleanupPasses.add(createFunctionResolvingPass());
108 CleanupPasses.add(createGlobalDCEPass());
Chris Lattner16608b42003-04-25 00:52:30 +0000109 CleanupPasses.add(createDeadTypeEliminationPass());
Chris Lattner587a6ce2003-08-01 16:13:49 +0000110 CleanupPasses.add(createDeadArgEliminationPass(InM == 0));
Chris Lattner10f22cb2003-03-07 18:17:13 +0000111 CleanupPasses.add(createVerifierPass());
Chris Lattnerba386d92003-02-28 16:13:20 +0000112 CleanupPasses.run(*M);
113 return M;
114}