blob: 9f8dca8abd0e0e3a2a5b85d51c43be707c6941d6 [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//
Chris Lattnerefdc0b52004-03-14 20:50:42 +000010// This file implements several methods that are used to extract functions,
11// loops, or portions of a module from the rest of the module.
Chris Lattnerafade922002-11-20 22:28:10 +000012//
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 Lattnerfb4b96e2004-04-02 16:28:32 +000029#include <set>
Chris Lattnerc6b519d2003-11-23 04:51:05 +000030using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000031
32namespace llvm {
Chris Lattnerc6b519d2003-11-23 04:51:05 +000033 bool DisableSimplifyCFG = false;
Brian Gaeked0fde302003-11-11 22:41:34 +000034} // End llvm namespace
35
Chris Lattner6db70ef2003-04-25 22:08:12 +000036namespace {
37 cl::opt<bool>
Chris Lattner6db70ef2003-04-25 22:08:12 +000038 NoDCE ("disable-dce",
39 cl::desc("Do not use the -dce pass to reduce testcases"));
Chris Lattner47ae4a12003-08-05 15:51:05 +000040 cl::opt<bool, true>
41 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
Chris Lattner6db70ef2003-04-25 22:08:12 +000042 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
43}
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///
Chris Lattner0cc88072004-02-18 21:50:26 +000050Module *BugDriver::deleteInstructionFromProgram(const Instruction *I,
Chris Lattner65207852003-01-23 02:48:33 +000051 unsigned Simplification) const {
52 Module *Result = CloneModule(Program);
53
Chris Lattner0cc88072004-02-18 21:50:26 +000054 const BasicBlock *PBB = I->getParent();
55 const Function *PF = PBB->getParent();
Chris Lattner65207852003-01-23 02:48:33 +000056
57 Module::iterator RFI = Result->begin(); // Get iterator to corresponding fn
Chris Lattner0cc88072004-02-18 21:50:26 +000058 std::advance(RFI, std::distance(PF->getParent()->begin(),
59 Module::const_iterator(PF)));
Chris Lattner65207852003-01-23 02:48:33 +000060
61 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
Chris Lattner0cc88072004-02-18 21:50:26 +000062 std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
Chris Lattner65207852003-01-23 02:48:33 +000063
64 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
Chris Lattner0cc88072004-02-18 21:50:26 +000065 std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
66 Instruction *TheInst = RI; // Got the corresponding instruction!
Chris Lattner65207852003-01-23 02:48:33 +000067
68 // If this instruction produces a value, replace any users with null values
Chris Lattner0cc88072004-02-18 21:50:26 +000069 if (TheInst->getType() != Type::VoidTy)
70 TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
Chris Lattner65207852003-01-23 02:48:33 +000071
72 // Remove the instruction from the program.
Chris Lattner0cc88072004-02-18 21:50:26 +000073 TheInst->getParent()->getInstList().erase(TheInst);
Chris Lattner65207852003-01-23 02:48:33 +000074
Chris Lattner44be2572003-04-24 22:53:24 +000075 // Spiff up the output a little bit.
Chris Lattner65207852003-01-23 02:48:33 +000076 PassManager Passes;
Chris Lattner5da69c72003-10-23 15:42:55 +000077 // Make sure that the appropriate target data is always used...
78 Passes.add(new TargetData("bugpoint", Result));
79
Chris Lattnerefdc0b52004-03-14 20:50:42 +000080 /// FIXME: If this used runPasses() like the methods below, we could get rid
81 /// of the -disable-* options!
Chris Lattner6db70ef2003-04-25 22:08:12 +000082 if (Simplification > 1 && !NoDCE)
Chris Lattner65207852003-01-23 02:48:33 +000083 Passes.add(createDeadCodeEliminationPass());
Chris Lattner47ae4a12003-08-05 15:51:05 +000084 if (Simplification && !DisableSimplifyCFG)
Chris Lattner65207852003-01-23 02:48:33 +000085 Passes.add(createCFGSimplificationPass()); // Delete dead control flow
Chris Lattner10f22cb2003-03-07 18:17:13 +000086
87 Passes.add(createVerifierPass());
Chris Lattner65207852003-01-23 02:48:33 +000088 Passes.run(*Result);
89 return Result;
90}
Chris Lattnerba386d92003-02-28 16:13:20 +000091
Chris Lattnerfcb6ec02003-11-05 21:45:35 +000092static const PassInfo *getPI(Pass *P) {
93 const PassInfo *PI = P->getPassInfo();
94 delete P;
95 return PI;
96}
97
Chris Lattnerba386d92003-02-28 16:13:20 +000098/// performFinalCleanups - This method clones the current Program and performs
99/// a series of cleanups intended to get rid of extra cruft on the module
100/// before handing it to the user...
101///
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000102Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
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 Lattnerfcb6ec02003-11-05 21:45:35 +0000107 std::vector<const PassInfo*> CleanupPasses;
108 CleanupPasses.push_back(getPI(createFunctionResolvingPass()));
109 CleanupPasses.push_back(getPI(createGlobalDCEPass()));
110 CleanupPasses.push_back(getPI(createDeadTypeEliminationPass()));
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000111
Chris Lattnerc6b519d2003-11-23 04:51:05 +0000112 if (MayModifySemantics)
113 CleanupPasses.push_back(getPI(createDeadArgHackingPass()));
114 else
115 CleanupPasses.push_back(getPI(createDeadArgEliminationPass()));
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000116
Chris Lattnera75766a2004-03-14 21:17:22 +0000117 Module *New = runPassesOn(M, CleanupPasses);
118 if (New == 0) {
Chris Lattner7546c382004-03-14 20:02:07 +0000119 std::cerr << "Final cleanups failed. Sorry. :( Please report a bug!\n";
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000120 }
Chris Lattnera75766a2004-03-14 21:17:22 +0000121 delete M;
122 return New;
Chris Lattnerba386d92003-02-28 16:13:20 +0000123}
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000124
125
Chris Lattner7546c382004-03-14 20:02:07 +0000126/// ExtractLoop - Given a module, extract up to one loop from it into a new
127/// function. This returns null if there are no extractable loops in the
128/// program or if the loop extractor crashes.
129Module *BugDriver::ExtractLoop(Module *M) {
130 std::vector<const PassInfo*> LoopExtractPasses;
131 LoopExtractPasses.push_back(getPI(createSingleLoopExtractorPass()));
132
Chris Lattnera75766a2004-03-14 21:17:22 +0000133 Module *NewM = runPassesOn(M, LoopExtractPasses);
134 if (NewM == 0) {
Chris Lattnera1cf1c82004-03-14 22:08:00 +0000135 Module *Old = swapProgramIn(M);
136 std::cout << "*** Loop extraction failed: ";
137 EmitProgressBytecode("loopextraction", true);
138 std::cout << "*** Sorry. :( Please report a bug!\n";
139 swapProgramIn(Old);
Chris Lattner7546c382004-03-14 20:02:07 +0000140 return 0;
Chris Lattner7546c382004-03-14 20:02:07 +0000141 }
Chris Lattnera75766a2004-03-14 21:17:22 +0000142
143 // Check to see if we created any new functions. If not, no loops were
144 // extracted and we should return null.
Chris Lattner7fa44fc2004-03-17 17:37:18 +0000145 if (M->size() == NewM->size()) {
Chris Lattnera75766a2004-03-14 21:17:22 +0000146 delete NewM;
147 return 0;
148 }
149
150 return NewM;
Chris Lattner7546c382004-03-14 20:02:07 +0000151}
152
153
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000154// DeleteFunctionBody - "Remove" the function by deleting all of its basic
155// blocks, making it external.
156//
157void llvm::DeleteFunctionBody(Function *F) {
158 // delete the body of the function...
159 F->deleteBody();
160 assert(F->isExternal() && "This didn't make the function external!");
161}
162
163/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
164/// module, split the functions OUT of the specified module, and place them in
165/// the new module.
Chris Lattner5eda1f22004-03-14 19:31:00 +0000166///
167/// FIXME: this could be made DRAMATICALLY more efficient for large programs if
168/// we just MOVED functions from one module to the other, instead of cloning the
169/// whole module, then proceeding to delete an entire module's worth of stuff.
170///
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000171Module *llvm::SplitFunctionsOutOfModule(Module *M,
172 const std::vector<Function*> &F) {
173 // Make sure functions & globals are all external so that linkage
174 // between the two modules will work.
175 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
176 I->setLinkage(GlobalValue::ExternalLinkage);
177 for (Module::giterator I = M->gbegin(), E = M->gend(); I != E; ++I)
178 I->setLinkage(GlobalValue::ExternalLinkage);
179
180 Module *New = CloneModule(M);
181
182 // Make sure global initializers exist only in the safe module (CBE->.so)
183 for (Module::giterator I = New->gbegin(), E = New->gend(); I != E; ++I)
184 I->setInitializer(0); // Delete the initializer to make it external
185
186 // Remove the Test functions from the Safe module
Chris Lattnerfb4b96e2004-04-02 16:28:32 +0000187 std::set<std::pair<std::string, const PointerType*> > TestFunctions;
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000188 for (unsigned i = 0, e = F.size(); i != e; ++i) {
Chris Lattnerfb4b96e2004-04-02 16:28:32 +0000189 TestFunctions.insert(std::make_pair(F[i]->getName(), F[i]->getType()));
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000190 Function *TNOF = M->getFunction(F[i]->getName(), F[i]->getFunctionType());
191 DEBUG(std::cerr << "Removing function " << F[i]->getName() << "\n");
192 assert(TNOF && "Function doesn't exist in module!");
193 DeleteFunctionBody(TNOF); // Function is now external in this module!
194 }
195
196 // Remove the Safe functions from the Test module
Chris Lattnerfb4b96e2004-04-02 16:28:32 +0000197 for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I)
198 if (!TestFunctions.count(std::make_pair(I->getName(), I->getType())))
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000199 DeleteFunctionBody(I);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000200 return New;
201}