blob: 7f6432fbd7089a6cf977dd04cb43c997e2ebe573 [file] [log] [blame]
Chris Lattnerafade922002-11-20 22:28:10 +00001//===- ExtractFunction.cpp - Extract a function from Program --------------===//
Misha Brukman3da94ae2005-04-22 00:00:37 +00002//
John Criswell7c0e0222003-10-20 17:47:21 +00003// The LLVM Compiler Infrastructure
4//
Chris Lattner21c62da2007-12-29 20:44:31 +00005// This file is distributed under the University of Illinois Open Source
6// License. See LICENSE.TXT for details.
Misha Brukman3da94ae2005-04-22 00:00:37 +00007//
John Criswell7c0e0222003-10-20 17:47:21 +00008//===----------------------------------------------------------------------===//
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"
Chandler Carruthf010c462012-12-04 10:44:52 +000016#include "llvm/Analysis/Verifier.h"
17#include "llvm/Assembly/Writer.h"
Chandler Carruth0b8c9a82013-01-02 11:36:10 +000018#include "llvm/IR/Constants.h"
19#include "llvm/IR/DataLayout.h"
20#include "llvm/IR/DerivedTypes.h"
21#include "llvm/IR/LLVMContext.h"
22#include "llvm/IR/Module.h"
Brian Gaeked1a85a72003-09-10 21:11:42 +000023#include "llvm/Pass.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000024#include "llvm/PassManager.h"
25#include "llvm/Support/CommandLine.h"
26#include "llvm/Support/Debug.h"
27#include "llvm/Support/FileUtilities.h"
28#include "llvm/Support/Path.h"
Rafael Espindolab7e21882013-06-13 21:16:58 +000029#include "llvm/Support/PathV1.h"
Chandler Carruthf010c462012-12-04 10:44:52 +000030#include "llvm/Support/Signals.h"
31#include "llvm/Support/ToolOutputFile.h"
Chris Lattnerafade922002-11-20 22:28:10 +000032#include "llvm/Transforms/IPO.h"
Chris Lattner65207852003-01-23 02:48:33 +000033#include "llvm/Transforms/Scalar.h"
Chris Lattnerafade922002-11-20 22:28:10 +000034#include "llvm/Transforms/Utils/Cloning.h"
Chandler Carruth99650c92012-05-04 10:18:49 +000035#include "llvm/Transforms/Utils/CodeExtractor.h"
Chris Lattnerfb4b96e2004-04-02 16:28:32 +000036#include <set>
Chris Lattnerc6b519d2003-11-23 04:51:05 +000037using namespace llvm;
Brian Gaeked0fde302003-11-11 22:41:34 +000038
39namespace llvm {
Chris Lattnerc6b519d2003-11-23 04:51:05 +000040 bool DisableSimplifyCFG = false;
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +000041 extern cl::opt<std::string> OutputPrefix;
Brian Gaeked0fde302003-11-11 22:41:34 +000042} // End llvm namespace
43
Chris Lattner6db70ef2003-04-25 22:08:12 +000044namespace {
45 cl::opt<bool>
Chris Lattner6db70ef2003-04-25 22:08:12 +000046 NoDCE ("disable-dce",
47 cl::desc("Do not use the -dce pass to reduce testcases"));
Chris Lattner47ae4a12003-08-05 15:51:05 +000048 cl::opt<bool, true>
49 NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
Chris Lattner6db70ef2003-04-25 22:08:12 +000050 cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
Eli Friedman967570f2012-02-22 01:43:47 +000051
52 Function* globalInitUsesExternalBA(GlobalVariable* GV) {
53 if (!GV->hasInitializer())
54 return 0;
55
56 Constant *I = GV->getInitializer();
57
58 // walk the values used by the initializer
59 // (and recurse into things like ConstantExpr)
60 std::vector<Constant*> Todo;
61 std::set<Constant*> Done;
62 Todo.push_back(I);
63
64 while (!Todo.empty()) {
65 Constant* V = Todo.back();
66 Todo.pop_back();
67 Done.insert(V);
68
69 if (BlockAddress *BA = dyn_cast<BlockAddress>(V)) {
70 Function *F = BA->getFunction();
71 if (F->isDeclaration())
72 return F;
73 }
74
75 for (User::op_iterator i = V->op_begin(), e = V->op_end(); i != e; ++i) {
76 Constant *C = dyn_cast<Constant>(*i);
77 if (C && !isa<GlobalValue>(C) && !Done.count(C))
78 Todo.push_back(C);
79 }
80 }
81 return 0;
82 }
83} // end anonymous namespace
Chris Lattnerafade922002-11-20 22:28:10 +000084
Chris Lattner65207852003-01-23 02:48:33 +000085/// deleteInstructionFromProgram - This method clones the current Program and
86/// deletes the specified instruction from the cloned module. It then runs a
87/// series of cleanup passes (ADCE and SimplifyCFG) to eliminate any code which
88/// depends on the value. The modified module is then returned.
89///
Chris Lattner0cc88072004-02-18 21:50:26 +000090Module *BugDriver::deleteInstructionFromProgram(const Instruction *I,
Rafael Espindola866aa0d2010-08-10 15:46:11 +000091 unsigned Simplification) {
92 // FIXME, use vmap?
93 Module *Clone = CloneModule(Program);
Chris Lattner65207852003-01-23 02:48:33 +000094
Chris Lattner0cc88072004-02-18 21:50:26 +000095 const BasicBlock *PBB = I->getParent();
96 const Function *PF = PBB->getParent();
Chris Lattner65207852003-01-23 02:48:33 +000097
Rafael Espindola866aa0d2010-08-10 15:46:11 +000098 Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn
Chris Lattner0cc88072004-02-18 21:50:26 +000099 std::advance(RFI, std::distance(PF->getParent()->begin(),
100 Module::const_iterator(PF)));
Chris Lattner65207852003-01-23 02:48:33 +0000101
102 Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
Chris Lattner0cc88072004-02-18 21:50:26 +0000103 std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
Chris Lattner65207852003-01-23 02:48:33 +0000104
105 BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
Chris Lattner0cc88072004-02-18 21:50:26 +0000106 std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
107 Instruction *TheInst = RI; // Got the corresponding instruction!
Chris Lattner65207852003-01-23 02:48:33 +0000108
109 // If this instruction produces a value, replace any users with null values
Dan Gohmane49a13e2010-06-07 20:19:26 +0000110 if (!TheInst->getType()->isVoidTy())
Owen Andersona7235ea2009-07-31 20:28:14 +0000111 TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
Chris Lattner65207852003-01-23 02:48:33 +0000112
113 // Remove the instruction from the program.
Chris Lattner0cc88072004-02-18 21:50:26 +0000114 TheInst->getParent()->getInstList().erase(TheInst);
Chris Lattner65207852003-01-23 02:48:33 +0000115
Chris Lattner44be2572003-04-24 22:53:24 +0000116 // Spiff up the output a little bit.
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000117 std::vector<std::string> Passes;
Chris Lattner5da69c72003-10-23 15:42:55 +0000118
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000119 /// Can we get rid of the -disable-* options?
Chris Lattner6db70ef2003-04-25 22:08:12 +0000120 if (Simplification > 1 && !NoDCE)
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000121 Passes.push_back("dce");
Chris Lattner47ae4a12003-08-05 15:51:05 +0000122 if (Simplification && !DisableSimplifyCFG)
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000123 Passes.push_back("simplifycfg"); // Delete dead control flow
Chris Lattner10f22cb2003-03-07 18:17:13 +0000124
Rafael Espindola866aa0d2010-08-10 15:46:11 +0000125 Passes.push_back("verify");
126 Module *New = runPassesOn(Clone, Passes);
127 delete Clone;
128 if (!New) {
129 errs() << "Instruction removal failed. Sorry. :( Please report a bug!\n";
130 exit(1);
131 }
132 return New;
Chris Lattner65207852003-01-23 02:48:33 +0000133}
Chris Lattnerba386d92003-02-28 16:13:20 +0000134
135/// performFinalCleanups - This method clones the current Program and performs
136/// a series of cleanups intended to get rid of extra cruft on the module
Chris Lattner9b5b1902005-02-23 06:12:11 +0000137/// before handing it to the user.
Chris Lattnerba386d92003-02-28 16:13:20 +0000138///
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000139Module *BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
Chris Lattner28b8ed92003-05-21 19:41:31 +0000140 // Make all functions external, so GlobalDCE doesn't delete them...
141 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
142 I->setLinkage(GlobalValue::ExternalLinkage);
Misha Brukman3da94ae2005-04-22 00:00:37 +0000143
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000144 std::vector<std::string> CleanupPasses;
145 CleanupPasses.push_back("globaldce");
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000146
Chris Lattnerc6b519d2003-11-23 04:51:05 +0000147 if (MayModifySemantics)
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000148 CleanupPasses.push_back("deadarghaX0r");
Chris Lattnerc6b519d2003-11-23 04:51:05 +0000149 else
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000150 CleanupPasses.push_back("deadargelim");
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000151
Chris Lattnera75766a2004-03-14 21:17:22 +0000152 Module *New = runPassesOn(M, CleanupPasses);
153 if (New == 0) {
Dan Gohman65f57c22009-07-15 16:35:29 +0000154 errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n";
Chris Lattner9b5b1902005-02-23 06:12:11 +0000155 return M;
Chris Lattnerfcb6ec02003-11-05 21:45:35 +0000156 }
Chris Lattnera75766a2004-03-14 21:17:22 +0000157 delete M;
158 return New;
Chris Lattnerba386d92003-02-28 16:13:20 +0000159}
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000160
161
Chris Lattner7546c382004-03-14 20:02:07 +0000162/// ExtractLoop - Given a module, extract up to one loop from it into a new
163/// function. This returns null if there are no extractable loops in the
164/// program or if the loop extractor crashes.
165Module *BugDriver::ExtractLoop(Module *M) {
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000166 std::vector<std::string> LoopExtractPasses;
167 LoopExtractPasses.push_back("loop-extract-single");
Chris Lattner7546c382004-03-14 20:02:07 +0000168
Chris Lattnera75766a2004-03-14 21:17:22 +0000169 Module *NewM = runPassesOn(M, LoopExtractPasses);
170 if (NewM == 0) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000171 outs() << "*** Loop extraction failed: ";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000172 EmitProgressBitcode(M, "loopextraction", true);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000173 outs() << "*** Sorry. :( Please report a bug!\n";
Chris Lattner7546c382004-03-14 20:02:07 +0000174 return 0;
Chris Lattner7546c382004-03-14 20:02:07 +0000175 }
Chris Lattnera75766a2004-03-14 21:17:22 +0000176
177 // Check to see if we created any new functions. If not, no loops were
Chris Lattnera269ec72004-11-18 19:40:13 +0000178 // extracted and we should return null. Limit the number of loops we extract
179 // to avoid taking forever.
180 static unsigned NumExtracted = 32;
Chris Lattner90c18c52004-11-16 06:31:38 +0000181 if (M->size() == NewM->size() || --NumExtracted == 0) {
Chris Lattnera75766a2004-03-14 21:17:22 +0000182 delete NewM;
183 return 0;
Chris Lattner90c18c52004-11-16 06:31:38 +0000184 } else {
185 assert(M->size() < NewM->size() && "Loop extract removed functions?");
186 Module::iterator MI = NewM->begin();
187 for (unsigned i = 0, e = M->size(); i != e; ++i)
188 ++MI;
Chris Lattnera75766a2004-03-14 21:17:22 +0000189 }
Misha Brukman3da94ae2005-04-22 00:00:37 +0000190
Chris Lattnera75766a2004-03-14 21:17:22 +0000191 return NewM;
Chris Lattner7546c382004-03-14 20:02:07 +0000192}
193
194
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000195// DeleteFunctionBody - "Remove" the function by deleting all of its basic
196// blocks, making it external.
197//
198void llvm::DeleteFunctionBody(Function *F) {
199 // delete the body of the function...
200 F->deleteBody();
Reid Spencer5cbf9852007-01-30 20:08:39 +0000201 assert(F->isDeclaration() && "This didn't make the function external!");
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000202}
203
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000204/// GetTorInit - Given a list of entries for static ctors/dtors, return them
205/// as a constant array.
206static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) {
207 assert(!TorList.empty() && "Don't create empty tor list!");
208 std::vector<Constant*> ArrayElts;
Jay Foad5fdd6c82011-07-12 14:06:48 +0000209 Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext());
Chris Lattnerb065b062011-06-20 04:01:31 +0000210
Chris Lattnerdb125cf2011-07-18 04:54:35 +0000211 StructType *STy =
Chris Lattnerb065b062011-06-20 04:01:31 +0000212 StructType::get(Int32Ty, TorList[0].first->getType(), NULL);
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000213 for (unsigned i = 0, e = TorList.size(); i != e; ++i) {
Chris Lattnerb065b062011-06-20 04:01:31 +0000214 Constant *Elts[] = {
215 ConstantInt::get(Int32Ty, TorList[i].second),
216 TorList[i].first
217 };
218 ArrayElts.push_back(ConstantStruct::get(STy, Elts));
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000219 }
Owen Andersondebcb012009-07-29 22:17:13 +0000220 return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000221 ArrayElts.size()),
222 ArrayElts);
223}
224
225/// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and
226/// M1 has all of the global variables. If M2 contains any functions that are
227/// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and
228/// prune appropriate entries out of M1s list.
Dan Gohmand50330c2009-04-22 15:57:18 +0000229static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2,
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000230 ValueToValueMapTy &VMap) {
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000231 GlobalVariable *GV = M1->getNamedGlobal(GlobalName);
Rafael Espindolabb46f522009-01-15 20:18:42 +0000232 if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() ||
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000233 !GV->use_empty()) return;
234
235 std::vector<std::pair<Function*, int> > M1Tors, M2Tors;
236 ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
237 if (!InitList) return;
238
239 for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
240 if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
241 if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
242
243 if (CS->getOperand(1)->isNullValue())
244 break; // Found a null terminator, stop here.
245
Reid Spencerb83eb642006-10-20 07:07:24 +0000246 ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
247 int Priority = CI ? CI->getSExtValue() : 0;
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000248
249 Constant *FP = CS->getOperand(1);
250 if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
Reid Spencer3da59db2006-11-27 01:05:10 +0000251 if (CE->isCast())
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000252 FP = CE->getOperand(0);
253 if (Function *F = dyn_cast<Function>(FP)) {
Reid Spencer5cbf9852007-01-30 20:08:39 +0000254 if (!F->isDeclaration())
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000255 M1Tors.push_back(std::make_pair(F, Priority));
256 else {
257 // Map to M2's version of the function.
Devang Patele9916a32010-06-24 00:33:28 +0000258 F = cast<Function>(VMap[F]);
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000259 M2Tors.push_back(std::make_pair(F, Priority));
260 }
261 }
262 }
263 }
264
265 GV->eraseFromParent();
266 if (!M1Tors.empty()) {
267 Constant *M1Init = GetTorInit(M1Tors);
Owen Andersone9b11b42009-07-08 19:03:57 +0000268 new GlobalVariable(*M1, M1Init->getType(), false,
Owen Anderson3d29df32009-07-08 01:26:06 +0000269 GlobalValue::AppendingLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000270 M1Init, GlobalName);
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000271 }
272
273 GV = M2->getNamedGlobal(GlobalName);
274 assert(GV && "Not a clone of M1?");
275 assert(GV->use_empty() && "llvm.ctors shouldn't have uses!");
276
277 GV->eraseFromParent();
278 if (!M2Tors.empty()) {
279 Constant *M2Init = GetTorInit(M2Tors);
Owen Andersone9b11b42009-07-08 19:03:57 +0000280 new GlobalVariable(*M2, M2Init->getType(), false,
Owen Anderson3d29df32009-07-08 01:26:06 +0000281 GlobalValue::AppendingLinkage,
Owen Andersone9b11b42009-07-08 19:03:57 +0000282 M2Init, GlobalName);
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000283 }
284}
285
Patrick Jenkinse47863e2006-07-28 01:19:28 +0000286
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000287/// SplitFunctionsOutOfModule - Given a module and a list of functions in the
288/// module, split the functions OUT of the specified module, and place them in
289/// the new module.
Dan Gohmand50330c2009-04-22 15:57:18 +0000290Module *
291llvm::SplitFunctionsOutOfModule(Module *M,
292 const std::vector<Function*> &F,
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000293 ValueToValueMapTy &VMap) {
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000294 // Make sure functions & globals are all external so that linkage
295 // between the two modules will work.
296 for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
297 I->setLinkage(GlobalValue::ExternalLinkage);
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000298 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
Owen Anderson7220b812008-07-08 16:38:42 +0000299 I != E; ++I) {
Daniel Dunbar3f53fa92009-07-26 00:34:27 +0000300 if (I->hasName() && I->getName()[0] == '\01')
301 I->setName(I->getName().substr(1));
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000302 I->setLinkage(GlobalValue::ExternalLinkage);
Owen Anderson7220b812008-07-08 16:38:42 +0000303 }
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000304
Rafael Espindola1ed219a2010-10-13 01:36:30 +0000305 ValueToValueMapTy NewVMap;
Devang Patele9916a32010-06-24 00:33:28 +0000306 Module *New = CloneModule(M, NewVMap);
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000307
Chris Lattnerfef02422006-11-09 06:24:56 +0000308 // Remove the Test functions from the Safe module
Dan Gohmand50330c2009-04-22 15:57:18 +0000309 std::set<Function *> TestFunctions;
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000310 for (unsigned i = 0, e = F.size(); i != e; ++i) {
Devang Patele9916a32010-06-24 00:33:28 +0000311 Function *TNOF = cast<Function>(VMap[F[i]]);
Dan Gohman65f57c22009-07-15 16:35:29 +0000312 DEBUG(errs() << "Removing function ");
313 DEBUG(WriteAsOperand(errs(), TNOF, false));
314 DEBUG(errs() << "\n");
Devang Patele9916a32010-06-24 00:33:28 +0000315 TestFunctions.insert(cast<Function>(NewVMap[TNOF]));
Chris Lattnerfef02422006-11-09 06:24:56 +0000316 DeleteFunctionBody(TNOF); // Function is now external in this module!
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000317 }
318
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000319
Chris Lattnerfef02422006-11-09 06:24:56 +0000320 // Remove the Safe functions from the Test module
321 for (Module::iterator I = New->begin(), E = New->end(); I != E; ++I)
Dan Gohmand50330c2009-04-22 15:57:18 +0000322 if (!TestFunctions.count(I))
Chris Lattnerfef02422006-11-09 06:24:56 +0000323 DeleteFunctionBody(I);
324
Patrick Jenkinse47863e2006-07-28 01:19:28 +0000325
Eli Friedman967570f2012-02-22 01:43:47 +0000326 // Try to split the global initializers evenly
327 for (Module::global_iterator I = M->global_begin(), E = M->global_end();
328 I != E; ++I) {
329 GlobalVariable *GV = cast<GlobalVariable>(NewVMap[I]);
330 if (Function *TestFn = globalInitUsesExternalBA(I)) {
331 if (Function *SafeFn = globalInitUsesExternalBA(GV)) {
332 errs() << "*** Error: when reducing functions, encountered "
333 "the global '";
334 WriteAsOperand(errs(), GV, false);
335 errs() << "' with an initializer that references blockaddresses "
336 "from safe function '" << SafeFn->getName()
337 << "' and from test function '" << TestFn->getName() << "'.\n";
338 exit(1);
339 }
340 I->setInitializer(0); // Delete the initializer to make it external
341 } else {
342 // If we keep it in the safe module, then delete it in the test module
343 GV->setInitializer(0);
344 }
345 }
346
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000347 // Make sure that there is a global ctor/dtor array in both halves of the
348 // module if they both have static ctor/dtor functions.
Devang Patele9916a32010-06-24 00:33:28 +0000349 SplitStaticCtorDtor("llvm.global_ctors", M, New, NewVMap);
350 SplitStaticCtorDtor("llvm.global_dtors", M, New, NewVMap);
Chris Lattner5a7a9e52006-03-08 23:55:38 +0000351
Chris Lattnerbe21ca52004-03-14 19:27:19 +0000352 return New;
353}
Chris Lattner5e783ab2004-05-11 21:54:13 +0000354
355//===----------------------------------------------------------------------===//
356// Basic Block Extraction Code
357//===----------------------------------------------------------------------===//
358
Chris Lattner5e783ab2004-05-11 21:54:13 +0000359/// ExtractMappedBlocksFromModule - Extract all but the specified basic blocks
360/// into their own functions. The only detail is that M is actually a module
361/// cloned from the one the BBs are in, so some mapping needs to be performed.
362/// If this operation fails for some reason (ie the implementation is buggy),
363/// this function should return null, otherwise it returns a new Module.
364Module *BugDriver::ExtractMappedBlocksFromModule(const
365 std::vector<BasicBlock*> &BBs,
366 Module *M) {
Daniel Dunbar68ccdaa2009-09-07 19:26:11 +0000367 sys::Path uniqueFilename(OutputPrefix + "-extractblocks");
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000368 std::string ErrMsg;
369 if (uniqueFilename.createTemporaryFileOnDisk(true, &ErrMsg)) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000370 outs() << "*** Basic Block extraction failed!\n";
Dan Gohman65f57c22009-07-15 16:35:29 +0000371 errs() << "Error creating temporary file: " << ErrMsg << "\n";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000372 EmitProgressBitcode(M, "basicblockextractfail", true);
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000373 return 0;
374 }
Rafael Espindolab7e21882013-06-13 21:16:58 +0000375 sys::RemoveFileOnSignal(uniqueFilename.str());
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000376
Dan Gohmanac95cc72009-07-16 15:30:09 +0000377 std::string ErrorInfo;
Dan Gohmanf2914012010-08-20 16:59:15 +0000378 tool_output_file BlocksToNotExtractFile(uniqueFilename.c_str(), ErrorInfo);
Dan Gohmanac95cc72009-07-16 15:30:09 +0000379 if (!ErrorInfo.empty()) {
380 outs() << "*** Basic Block extraction failed!\n";
381 errs() << "Error writing list of blocks to not extract: " << ErrorInfo
Dan Gohman65f57c22009-07-15 16:35:29 +0000382 << "\n";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000383 EmitProgressBitcode(M, "basicblockextractfail", true);
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000384 return 0;
385 }
386 for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
387 I != E; ++I) {
388 BasicBlock *BB = *I;
Chris Lattner4a6a6f22008-01-08 04:26:20 +0000389 // If the BB doesn't have a name, give it one so we have something to key
390 // off of.
391 if (!BB->hasName()) BB->setName("tmpbb");
Benjamin Kramera7b0cb72011-11-15 16:27:03 +0000392 BlocksToNotExtractFile.os() << BB->getParent()->getName() << " "
Dan Gohmand4c45432010-09-01 14:20:41 +0000393 << BB->getName() << "\n";
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000394 }
Dan Gohmand4c45432010-09-01 14:20:41 +0000395 BlocksToNotExtractFile.os().close();
396 if (BlocksToNotExtractFile.os().has_error()) {
Dan Gohmanf2914012010-08-20 16:59:15 +0000397 errs() << "Error writing list of blocks to not extract: " << ErrorInfo
398 << "\n";
399 EmitProgressBitcode(M, "basicblockextractfail", true);
Dan Gohmand4c45432010-09-01 14:20:41 +0000400 BlocksToNotExtractFile.os().clear_error();
Dan Gohmanf2914012010-08-20 16:59:15 +0000401 return 0;
402 }
403 BlocksToNotExtractFile.keep();
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000404
Benjamin Kramer12ea66a2010-01-28 18:04:38 +0000405 std::string uniqueFN = "--extract-blocks-file=" + uniqueFilename.str();
406 const char *ExtraArg = uniqueFN.c_str();
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000407
Rafael Espindola8261dfe2010-08-08 03:55:08 +0000408 std::vector<std::string> PI;
409 PI.push_back("extract-blocks");
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000410 Module *Ret = runPassesOn(M, PI, false, 1, &ExtraArg);
411
Dan Gohmand27047f2010-05-27 20:51:54 +0000412 uniqueFilename.eraseFromDisk(); // Free disk space
Nick Lewycky6fa98b12007-11-14 06:47:06 +0000413
Chris Lattner891150f2004-08-12 02:36:50 +0000414 if (Ret == 0) {
Dan Gohmanac95cc72009-07-16 15:30:09 +0000415 outs() << "*** Basic Block extraction failed, please report a bug!\n";
Rafael Espindolabae1b712010-07-28 18:12:30 +0000416 EmitProgressBitcode(M, "basicblockextractfail", true);
Chris Lattner891150f2004-08-12 02:36:50 +0000417 }
Chris Lattner5e783ab2004-05-11 21:54:13 +0000418 return Ret;
419}