blob: b9298d3db35f7b8dae33a07acbac56b007741422 [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 Lattner5da69c72003-10-23 15:42:55 +000025#include "llvm/Target/TargetData.h"
Chris Lattner6db70ef2003-04-25 22:08:12 +000026#include "Support/CommandLine.h"
Chris Lattner1b747162003-12-07 02:31:03 +000027#include "Support/FileUtilities.h"
Chris Lattnerc6b519d2003-11-23 04:51:05 +000028using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000029
30namespace llvm {
Chris Lattnerc6b519d2003-11-23 04:51:05 +000031 bool DisableSimplifyCFG = false;
Brian Gaeked0fde302003-11-11 22:41:34 +000032} // End llvm namespace
33
Chris Lattner6db70ef2003-04-25 22:08:12 +000034namespace {
35 cl::opt<bool>
Chris Lattner6db70ef2003-04-25 22:08:12 +000036 NoDCE ("disable-dce",
37 cl::desc("Do not use the -dce pass to reduce testcases"));
Chris Lattner47ae4a12003-08-05 15:51:05 +000038 cl::opt<bool, true>
39 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
Chris Lattner6db70ef2003-04-25 22:08:12 +000040 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
41}
Chris Lattnerafade922002-11-20 22:28:10 +000042
Chris Lattner65207852003-01-23 02:48:33 +000043/// deleteInstructionFromProgram - This method clones the current Program and
44/// deletes the specified instruction from the cloned module. It then runs a
45/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
46/// depends on the value. The modified module is then returned.
47///
Chris Lattner0cc88072004-02-18 21:50:26 +000048Module *BugDriver::deleteInstructionFromProgram(const Instruction *I,
Chris Lattner65207852003-01-23 02:48:33 +000049 unsigned Simplification) const {
50 Module *Result = CloneModule(Program);
51
Chris Lattner0cc88072004-02-18 21:50:26 +000052 const BasicBlock *PBB = I->getParent();
53 const Function *PF = PBB->getParent();
Chris Lattner65207852003-01-23 02:48:33 +000054
55 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
Chris Lattner0cc88072004-02-18 21:50:26 +000056 std::advance(RFI, std::distance(PF->getParent()->begin(),
57 Module::const_iterator(PF)));
Chris Lattner65207852003-01-23 02:48:33 +000058
59 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
Chris Lattner0cc88072004-02-18 21:50:26 +000060 std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
Chris Lattner65207852003-01-23 02:48:33 +000061
62 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
Chris Lattner0cc88072004-02-18 21:50:26 +000063 std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
64 Instruction *TheInst = RI; // Got the corresponding instruction!
Chris Lattner65207852003-01-23 02:48:33 +000065
66 // If this instruction produces a value, replace any users with null values
Chris Lattner0cc88072004-02-18 21:50:26 +000067 if (TheInst->getType() != Type::VoidTy)
68 TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
Chris Lattner65207852003-01-23 02:48:33 +000069
70 // Remove the instruction from the program.
Chris Lattner0cc88072004-02-18 21:50:26 +000071 TheInst->getParent()->getInstList().erase(TheInst);
Chris Lattner65207852003-01-23 02:48:33 +000072
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 Lattner5da69c72003-10-23 15:42:55 +000075 // Make sure that the appropriate target data is always used...
76 Passes.add(new TargetData("bugpoint", Result));
77
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
Chris Lattnerfcb6ec02003-11-05 21:45:35 +000088static const PassInfo *getPI(Pass *P) {
89 const PassInfo *PI = P->getPassInfo();
90 delete P;
91 return PI;
92}
93
Chris Lattnerba386d92003-02-28 16:13:20 +000094/// performFinalCleanups - This method clones the current Program and performs
95/// a series of cleanups intended to get rid of extra cruft on the module
96/// before handing it to the user...
97///
Chris Lattnerfcb6ec02003-11-05 21:45:35 +000098Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
Chris Lattner28b8ed92003-05-21 19:41:31 +000099 // Make all functions external, so GlobalDCE doesn't delete them...
100 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
101 I->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattnerdbe48dc2003-05-21 20:38:59 +0000102
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000103 std::vector<const PassInfo*> CleanupPasses;
104 CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
105 CleanupPasses.push_back(getPI(createGlobalDCEPass()));
106 CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000107
Chris Lattnerc6b519d2003-11-23 04:51:05 +0000108 if (MayModifySemantics)
109 CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
110 else
111 CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
112
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000113 std::swap(Program, M);
114 std::string Filename;
115 bool Failed = runPasses(CleanupPasses, Filename);
116 std::swap(Program, M);
117
118 if (Failed) {
119 std::cerr << "Final cleanups failed. Sorry. :(\n";
120 } else {
121 delete M;
122 M = ParseInputFile(Filename);
123 if (M == 0) {
124 std::cerr << getToolName() << ": Error reading bytecode file '"
125 << Filename << "'!\n";
126 exit(1);
127 }
Chris Lattner1b747162003-12-07 02:31:03 +0000128 removeFile(Filename);
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000129 }
130 return M;
Chris Lattnerba386d92003-02-28 16:13:20 +0000131}