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