blob: 8e392613c933e882a47056f6093c35204ce8d63b [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 Lattnerbe21ca52004-03-14 19:27:19 +000027#include "Support/Debug.h"
Chris Lattner1b747162003-12-07 02:31:03 +000028#include "Support/FileUtilities.h"
Chris Lattnerc6b519d2003-11-23 04:51:05 +000029using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000030
31namespace llvm {
Chris Lattnerc6b519d2003-11-23 04:51:05 +000032 bool DisableSimplifyCFG = false;
Brian Gaeked0fde302003-11-11 22:41:34 +000033} // End llvm namespace
34
Chris Lattner6db70ef2003-04-25 22:08:12 +000035namespace {
36 cl::opt<bool>
Chris Lattner6db70ef2003-04-25 22:08:12 +000037 NoDCE ("disable-dce",
38 cl::desc("Do not use the -dce pass to reduce testcases"));
Chris Lattner47ae4a12003-08-05 15:51:05 +000039 cl::opt<bool, true>
40 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
Chris Lattner6db70ef2003-04-25 22:08:12 +000041 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
42}
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///
Chris Lattner0cc88072004-02-18 21:50:26 +000049Module *BugDriver::deleteInstructionFromProgram(const Instruction *I,
Chris Lattner65207852003-01-23 02:48:33 +000050 unsigned Simplification) const {
51 Module *Result = CloneModule(Program);
52
Chris Lattner0cc88072004-02-18 21:50:26 +000053 const BasicBlock *PBB = I->getParent();
54 const Function *PF = PBB->getParent();
Chris Lattner65207852003-01-23 02:48:33 +000055
56 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
Chris Lattner0cc88072004-02-18 21:50:26 +000057 std::advance(RFI, std::distance(PF->getParent()->begin(),
58 Module::const_iterator(PF)));
Chris Lattner65207852003-01-23 02:48:33 +000059
60 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
Chris Lattner0cc88072004-02-18 21:50:26 +000061 std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
Chris Lattner65207852003-01-23 02:48:33 +000062
63 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
Chris Lattner0cc88072004-02-18 21:50:26 +000064 std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
65 Instruction *TheInst = RI; // Got the corresponding instruction!
Chris Lattner65207852003-01-23 02:48:33 +000066
67 // If this instruction produces a value, replace any users with null values
Chris Lattner0cc88072004-02-18 21:50:26 +000068 if (TheInst->getType() != Type::VoidTy)
69 TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
Chris Lattner65207852003-01-23 02:48:33 +000070
71 // Remove the instruction from the program.
Chris Lattner0cc88072004-02-18 21:50:26 +000072 TheInst->getParent()->getInstList().erase(TheInst);
Chris Lattner65207852003-01-23 02:48:33 +000073
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 Lattner5da69c72003-10-23 15:42:55 +000076 // Make sure that the appropriate target data is always used...
77 Passes.add(new TargetData("bugpoint", Result));
78
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
Chris Lattnerfcb6ec02003-11-05 21:45:35 +000089static const PassInfo *getPI(Pass *P) {
90 const PassInfo *PI = P->getPassInfo();
91 delete P;
92 return PI;
93}
94
Chris Lattnerba386d92003-02-28 16:13:20 +000095/// performFinalCleanups - This method clones the current Program and performs
96/// a series of cleanups intended to get rid of extra cruft on the module
97/// before handing it to the user...
98///
Chris Lattnerfcb6ec02003-11-05 21:45:35 +000099Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
Chris Lattner28b8ed92003-05-21 19:41:31 +0000100 // Make all functions external, so GlobalDCE doesn't delete them...
101 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
102 I->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattnerdbe48dc2003-05-21 20:38:59 +0000103
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000104 std::vector<const PassInfo*> CleanupPasses;
105 CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
106 CleanupPasses.push_back(getPI(createGlobalDCEPass()));
107 CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000108
Chris Lattnerc6b519d2003-11-23 04:51:05 +0000109 if (MayModifySemantics)
110 CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
111 else
112 CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
113
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000114 std::swap(Program, M);
115 std::string Filename;
116 bool Failed = runPasses(CleanupPasses, Filename);
117 std::swap(Program, M);
118
119 if (Failed) {
Chris Lattner7546c382004-03-14 20:02:07 +0000120 std::cerr << "Final cleanups failed. Sorry. :( Please report a bug!\n";
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000121 } else {
122 delete M;
123 M = ParseInputFile(Filename);
124 if (M == 0) {
125 std::cerr << getToolName() << ": Error reading bytecode file '"
126 << Filename << "'!\n";
127 exit(1);
128 }
Chris Lattner1b747162003-12-07 02:31:03 +0000129 removeFile(Filename);
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000130 }
131 return M;
Chris Lattnerba386d92003-02-28 16:13:20 +0000132}
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000133
134
Chris Lattner7546c382004-03-14 20:02:07 +0000135/// ExtractLoop - Given a module, extract up to one loop from it into a new
136/// function. This returns null if there are no extractable loops in the
137/// program or if the loop extractor crashes.
138Module *BugDriver::ExtractLoop(Module *M) {
139 std::vector<const PassInfo*> LoopExtractPasses;
140 LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass()));
141
142 std::swap(Program, M);
143 std::string Filename;
144 bool Failed = runPasses(LoopExtractPasses, Filename);
145 std::swap(Program, M);
146
147 if (Failed) {
148 std::cerr << "Loop extraction failed. Sorry. :( Please report a bug!\n";
149 return 0;
150 } else {
151 Module *NewM = ParseInputFile(Filename);
152 if (NewM == 0) {
153 std::cerr << getToolName() << ": Error reading bytecode file '"
154 << Filename << "'!\n";
155 exit(1);
156 }
157 removeFile(Filename);
158
159 // Check to see if we created any new functions. If not, no loops were
160 // extracted and we should return null.
161 if (M->size() != NewM->size()) {
162 delete NewM;
163 return 0;
164 }
165
166 return NewM;
167 }
168}
169
170
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000171// DeleteFunctionBody - "Remove" the function by deleting all of its basic
172// blocks, making it external.
173//
174void llvm::DeleteFunctionBody(Function *F) {
175 // delete the body of the function...
176 F->deleteBody();
177 assert(F->isExternal() && "This didn't make the function external!");
178}
179
180/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
181/// module, split the functions OUT of the specified module, and place them in
182/// the new module.
Chris Lattner5eda1f22004-03-14 19:31:00 +0000183///
184/// FIXME: this could be made DRAMATICALLY more efficient for large programs if
185/// we just MOVED functions from one module to the other, instead of cloning the
186/// whole module, then proceeding to delete an entire module's worth of stuff.
187///
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000188Module *llvm::SplitFunctionsOutOfModule(Module *M,
189 const std::vector<Function*> &F) {
190 // Make sure functions & globals are all external so that linkage
191 // between the two modules will work.
192 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
193 I->setLinkage(GlobalValue::ExternalLinkage);
194 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
195 I->setLinkage(GlobalValue::ExternalLinkage);
196
197 Module *New = CloneModule(M);
198
199 // Make sure global initializers exist only in the safe module (CBE->.so)
200 for (Module::giterator I = New->gbegin(), E = New->gend(); I != E; ++I)
201 I->setInitializer(0); // Delete the initializer to make it external
202
203 // Remove the Test functions from the Safe module
204 for (unsigned i = 0, e = F.size(); i != e; ++i) {
205 Function *TNOF = M->getFunction(F[i]->getName(), F[i]->getFunctionType());
206 DEBUG(std::cerr << "Removing function " << F[i]->getName() << "\n");
207 assert(TNOF && "Function doesn't exist in module!");
208 DeleteFunctionBody(TNOF); // Function is now external in this module!
209 }
210
211 // Remove the Safe functions from the Test module
212 for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I) {
213 bool funcFound = false;
214 for (std::vector<Function*>::const_iterator FI = F.begin(), Fe = F.end();
215 FI != Fe; ++FI)
216 if (I->getName() == (*FI)->getName()) funcFound = true;
217
218 if (!funcFound)
219 DeleteFunctionBody(I);
220 }
221 return New;
222}