1*9880d681SAndroid Build Coastguard Worker //===- ExtractFunction.cpp - Extract a function from Program --------------===//
2*9880d681SAndroid Build Coastguard Worker //
3*9880d681SAndroid Build Coastguard Worker // The LLVM Compiler Infrastructure
4*9880d681SAndroid Build Coastguard Worker //
5*9880d681SAndroid Build Coastguard Worker // This file is distributed under the University of Illinois Open Source
6*9880d681SAndroid Build Coastguard Worker // License. See LICENSE.TXT for details.
7*9880d681SAndroid Build Coastguard Worker //
8*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
9*9880d681SAndroid Build Coastguard Worker //
10*9880d681SAndroid Build Coastguard Worker // This file implements several methods that are used to extract functions,
11*9880d681SAndroid Build Coastguard Worker // loops, or portions of a module from the rest of the module.
12*9880d681SAndroid Build Coastguard Worker //
13*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
14*9880d681SAndroid Build Coastguard Worker
15*9880d681SAndroid Build Coastguard Worker #include "BugDriver.h"
16*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Constants.h"
17*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DataLayout.h"
18*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/DerivedTypes.h"
19*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LLVMContext.h"
20*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/LegacyPassManager.h"
21*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Module.h"
22*9880d681SAndroid Build Coastguard Worker #include "llvm/IR/Verifier.h"
23*9880d681SAndroid Build Coastguard Worker #include "llvm/Pass.h"
24*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/CommandLine.h"
25*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Debug.h"
26*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/FileUtilities.h"
27*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Path.h"
28*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/Signals.h"
29*9880d681SAndroid Build Coastguard Worker #include "llvm/Support/ToolOutputFile.h"
30*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/IPO.h"
31*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Scalar.h"
32*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/Cloning.h"
33*9880d681SAndroid Build Coastguard Worker #include "llvm/Transforms/Utils/CodeExtractor.h"
34*9880d681SAndroid Build Coastguard Worker #include <set>
35*9880d681SAndroid Build Coastguard Worker using namespace llvm;
36*9880d681SAndroid Build Coastguard Worker
37*9880d681SAndroid Build Coastguard Worker #define DEBUG_TYPE "bugpoint"
38*9880d681SAndroid Build Coastguard Worker
39*9880d681SAndroid Build Coastguard Worker namespace llvm {
40*9880d681SAndroid Build Coastguard Worker bool DisableSimplifyCFG = false;
41*9880d681SAndroid Build Coastguard Worker extern cl::opt<std::string> OutputPrefix;
42*9880d681SAndroid Build Coastguard Worker } // End llvm namespace
43*9880d681SAndroid Build Coastguard Worker
44*9880d681SAndroid Build Coastguard Worker namespace {
45*9880d681SAndroid Build Coastguard Worker cl::opt<bool>
46*9880d681SAndroid Build Coastguard Worker NoDCE ("disable-dce",
47*9880d681SAndroid Build Coastguard Worker cl::desc("Do not use the -dce pass to reduce testcases"));
48*9880d681SAndroid Build Coastguard Worker cl::opt<bool, true>
49*9880d681SAndroid Build Coastguard Worker NoSCFG("disable-simplifycfg", cl::location(DisableSimplifyCFG),
50*9880d681SAndroid Build Coastguard Worker cl::desc("Do not use the -simplifycfg pass to reduce testcases"));
51*9880d681SAndroid Build Coastguard Worker
globalInitUsesExternalBA(GlobalVariable * GV)52*9880d681SAndroid Build Coastguard Worker Function* globalInitUsesExternalBA(GlobalVariable* GV) {
53*9880d681SAndroid Build Coastguard Worker if (!GV->hasInitializer())
54*9880d681SAndroid Build Coastguard Worker return nullptr;
55*9880d681SAndroid Build Coastguard Worker
56*9880d681SAndroid Build Coastguard Worker Constant *I = GV->getInitializer();
57*9880d681SAndroid Build Coastguard Worker
58*9880d681SAndroid Build Coastguard Worker // walk the values used by the initializer
59*9880d681SAndroid Build Coastguard Worker // (and recurse into things like ConstantExpr)
60*9880d681SAndroid Build Coastguard Worker std::vector<Constant*> Todo;
61*9880d681SAndroid Build Coastguard Worker std::set<Constant*> Done;
62*9880d681SAndroid Build Coastguard Worker Todo.push_back(I);
63*9880d681SAndroid Build Coastguard Worker
64*9880d681SAndroid Build Coastguard Worker while (!Todo.empty()) {
65*9880d681SAndroid Build Coastguard Worker Constant* V = Todo.back();
66*9880d681SAndroid Build Coastguard Worker Todo.pop_back();
67*9880d681SAndroid Build Coastguard Worker Done.insert(V);
68*9880d681SAndroid Build Coastguard Worker
69*9880d681SAndroid Build Coastguard Worker if (BlockAddress *BA = dyn_cast<BlockAddress>(V)) {
70*9880d681SAndroid Build Coastguard Worker Function *F = BA->getFunction();
71*9880d681SAndroid Build Coastguard Worker if (F->isDeclaration())
72*9880d681SAndroid Build Coastguard Worker return F;
73*9880d681SAndroid Build Coastguard Worker }
74*9880d681SAndroid Build Coastguard Worker
75*9880d681SAndroid Build Coastguard Worker for (User::op_iterator i = V->op_begin(), e = V->op_end(); i != e; ++i) {
76*9880d681SAndroid Build Coastguard Worker Constant *C = dyn_cast<Constant>(*i);
77*9880d681SAndroid Build Coastguard Worker if (C && !isa<GlobalValue>(C) && !Done.count(C))
78*9880d681SAndroid Build Coastguard Worker Todo.push_back(C);
79*9880d681SAndroid Build Coastguard Worker }
80*9880d681SAndroid Build Coastguard Worker }
81*9880d681SAndroid Build Coastguard Worker return nullptr;
82*9880d681SAndroid Build Coastguard Worker }
83*9880d681SAndroid Build Coastguard Worker } // end anonymous namespace
84*9880d681SAndroid Build Coastguard Worker
85*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module>
deleteInstructionFromProgram(const Instruction * I,unsigned Simplification)86*9880d681SAndroid Build Coastguard Worker BugDriver::deleteInstructionFromProgram(const Instruction *I,
87*9880d681SAndroid Build Coastguard Worker unsigned Simplification) {
88*9880d681SAndroid Build Coastguard Worker // FIXME, use vmap?
89*9880d681SAndroid Build Coastguard Worker Module *Clone = CloneModule(Program).release();
90*9880d681SAndroid Build Coastguard Worker
91*9880d681SAndroid Build Coastguard Worker const BasicBlock *PBB = I->getParent();
92*9880d681SAndroid Build Coastguard Worker const Function *PF = PBB->getParent();
93*9880d681SAndroid Build Coastguard Worker
94*9880d681SAndroid Build Coastguard Worker Module::iterator RFI = Clone->begin(); // Get iterator to corresponding fn
95*9880d681SAndroid Build Coastguard Worker std::advance(RFI, std::distance(PF->getParent()->begin(),
96*9880d681SAndroid Build Coastguard Worker Module::const_iterator(PF)));
97*9880d681SAndroid Build Coastguard Worker
98*9880d681SAndroid Build Coastguard Worker Function::iterator RBI = RFI->begin(); // Get iterator to corresponding BB
99*9880d681SAndroid Build Coastguard Worker std::advance(RBI, std::distance(PF->begin(), Function::const_iterator(PBB)));
100*9880d681SAndroid Build Coastguard Worker
101*9880d681SAndroid Build Coastguard Worker BasicBlock::iterator RI = RBI->begin(); // Get iterator to corresponding inst
102*9880d681SAndroid Build Coastguard Worker std::advance(RI, std::distance(PBB->begin(), BasicBlock::const_iterator(I)));
103*9880d681SAndroid Build Coastguard Worker Instruction *TheInst = &*RI; // Got the corresponding instruction!
104*9880d681SAndroid Build Coastguard Worker
105*9880d681SAndroid Build Coastguard Worker // If this instruction produces a value, replace any users with null values
106*9880d681SAndroid Build Coastguard Worker if (!TheInst->getType()->isVoidTy())
107*9880d681SAndroid Build Coastguard Worker TheInst->replaceAllUsesWith(Constant::getNullValue(TheInst->getType()));
108*9880d681SAndroid Build Coastguard Worker
109*9880d681SAndroid Build Coastguard Worker // Remove the instruction from the program.
110*9880d681SAndroid Build Coastguard Worker TheInst->getParent()->getInstList().erase(TheInst);
111*9880d681SAndroid Build Coastguard Worker
112*9880d681SAndroid Build Coastguard Worker // Spiff up the output a little bit.
113*9880d681SAndroid Build Coastguard Worker std::vector<std::string> Passes;
114*9880d681SAndroid Build Coastguard Worker
115*9880d681SAndroid Build Coastguard Worker /// Can we get rid of the -disable-* options?
116*9880d681SAndroid Build Coastguard Worker if (Simplification > 1 && !NoDCE)
117*9880d681SAndroid Build Coastguard Worker Passes.push_back("dce");
118*9880d681SAndroid Build Coastguard Worker if (Simplification && !DisableSimplifyCFG)
119*9880d681SAndroid Build Coastguard Worker Passes.push_back("simplifycfg"); // Delete dead control flow
120*9880d681SAndroid Build Coastguard Worker
121*9880d681SAndroid Build Coastguard Worker Passes.push_back("verify");
122*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> New = runPassesOn(Clone, Passes);
123*9880d681SAndroid Build Coastguard Worker delete Clone;
124*9880d681SAndroid Build Coastguard Worker if (!New) {
125*9880d681SAndroid Build Coastguard Worker errs() << "Instruction removal failed. Sorry. :( Please report a bug!\n";
126*9880d681SAndroid Build Coastguard Worker exit(1);
127*9880d681SAndroid Build Coastguard Worker }
128*9880d681SAndroid Build Coastguard Worker return New;
129*9880d681SAndroid Build Coastguard Worker }
130*9880d681SAndroid Build Coastguard Worker
131*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module>
performFinalCleanups(Module * M,bool MayModifySemantics)132*9880d681SAndroid Build Coastguard Worker BugDriver::performFinalCleanups(Module *M, bool MayModifySemantics) {
133*9880d681SAndroid Build Coastguard Worker // Make all functions external, so GlobalDCE doesn't delete them...
134*9880d681SAndroid Build Coastguard Worker for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
135*9880d681SAndroid Build Coastguard Worker I->setLinkage(GlobalValue::ExternalLinkage);
136*9880d681SAndroid Build Coastguard Worker
137*9880d681SAndroid Build Coastguard Worker std::vector<std::string> CleanupPasses;
138*9880d681SAndroid Build Coastguard Worker CleanupPasses.push_back("globaldce");
139*9880d681SAndroid Build Coastguard Worker
140*9880d681SAndroid Build Coastguard Worker if (MayModifySemantics)
141*9880d681SAndroid Build Coastguard Worker CleanupPasses.push_back("deadarghaX0r");
142*9880d681SAndroid Build Coastguard Worker else
143*9880d681SAndroid Build Coastguard Worker CleanupPasses.push_back("deadargelim");
144*9880d681SAndroid Build Coastguard Worker
145*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> New = runPassesOn(M, CleanupPasses);
146*9880d681SAndroid Build Coastguard Worker if (!New) {
147*9880d681SAndroid Build Coastguard Worker errs() << "Final cleanups failed. Sorry. :( Please report a bug!\n";
148*9880d681SAndroid Build Coastguard Worker return nullptr;
149*9880d681SAndroid Build Coastguard Worker }
150*9880d681SAndroid Build Coastguard Worker delete M;
151*9880d681SAndroid Build Coastguard Worker return New;
152*9880d681SAndroid Build Coastguard Worker }
153*9880d681SAndroid Build Coastguard Worker
extractLoop(Module * M)154*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> BugDriver::extractLoop(Module *M) {
155*9880d681SAndroid Build Coastguard Worker std::vector<std::string> LoopExtractPasses;
156*9880d681SAndroid Build Coastguard Worker LoopExtractPasses.push_back("loop-extract-single");
157*9880d681SAndroid Build Coastguard Worker
158*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> NewM = runPassesOn(M, LoopExtractPasses);
159*9880d681SAndroid Build Coastguard Worker if (!NewM) {
160*9880d681SAndroid Build Coastguard Worker outs() << "*** Loop extraction failed: ";
161*9880d681SAndroid Build Coastguard Worker EmitProgressBitcode(M, "loopextraction", true);
162*9880d681SAndroid Build Coastguard Worker outs() << "*** Sorry. :( Please report a bug!\n";
163*9880d681SAndroid Build Coastguard Worker return nullptr;
164*9880d681SAndroid Build Coastguard Worker }
165*9880d681SAndroid Build Coastguard Worker
166*9880d681SAndroid Build Coastguard Worker // Check to see if we created any new functions. If not, no loops were
167*9880d681SAndroid Build Coastguard Worker // extracted and we should return null. Limit the number of loops we extract
168*9880d681SAndroid Build Coastguard Worker // to avoid taking forever.
169*9880d681SAndroid Build Coastguard Worker static unsigned NumExtracted = 32;
170*9880d681SAndroid Build Coastguard Worker if (M->size() == NewM->size() || --NumExtracted == 0) {
171*9880d681SAndroid Build Coastguard Worker return nullptr;
172*9880d681SAndroid Build Coastguard Worker } else {
173*9880d681SAndroid Build Coastguard Worker assert(M->size() < NewM->size() && "Loop extract removed functions?");
174*9880d681SAndroid Build Coastguard Worker Module::iterator MI = NewM->begin();
175*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = M->size(); i != e; ++i)
176*9880d681SAndroid Build Coastguard Worker ++MI;
177*9880d681SAndroid Build Coastguard Worker }
178*9880d681SAndroid Build Coastguard Worker
179*9880d681SAndroid Build Coastguard Worker return NewM;
180*9880d681SAndroid Build Coastguard Worker }
181*9880d681SAndroid Build Coastguard Worker
eliminateAliases(GlobalValue * GV)182*9880d681SAndroid Build Coastguard Worker static void eliminateAliases(GlobalValue *GV) {
183*9880d681SAndroid Build Coastguard Worker // First, check whether a GlobalAlias references this definition.
184*9880d681SAndroid Build Coastguard Worker // GlobalAlias MAY NOT reference declarations.
185*9880d681SAndroid Build Coastguard Worker for (;;) {
186*9880d681SAndroid Build Coastguard Worker // 1. Find aliases
187*9880d681SAndroid Build Coastguard Worker SmallVector<GlobalAlias*,1> aliases;
188*9880d681SAndroid Build Coastguard Worker Module *M = GV->getParent();
189*9880d681SAndroid Build Coastguard Worker for (Module::alias_iterator I=M->alias_begin(), E=M->alias_end(); I!=E; ++I)
190*9880d681SAndroid Build Coastguard Worker if (I->getAliasee()->stripPointerCasts() == GV)
191*9880d681SAndroid Build Coastguard Worker aliases.push_back(&*I);
192*9880d681SAndroid Build Coastguard Worker if (aliases.empty())
193*9880d681SAndroid Build Coastguard Worker break;
194*9880d681SAndroid Build Coastguard Worker // 2. Resolve aliases
195*9880d681SAndroid Build Coastguard Worker for (unsigned i=0, e=aliases.size(); i<e; ++i) {
196*9880d681SAndroid Build Coastguard Worker aliases[i]->replaceAllUsesWith(aliases[i]->getAliasee());
197*9880d681SAndroid Build Coastguard Worker aliases[i]->eraseFromParent();
198*9880d681SAndroid Build Coastguard Worker }
199*9880d681SAndroid Build Coastguard Worker // 3. Repeat until no more aliases found; there might
200*9880d681SAndroid Build Coastguard Worker // be an alias to an alias...
201*9880d681SAndroid Build Coastguard Worker }
202*9880d681SAndroid Build Coastguard Worker }
203*9880d681SAndroid Build Coastguard Worker
204*9880d681SAndroid Build Coastguard Worker //
205*9880d681SAndroid Build Coastguard Worker // DeleteGlobalInitializer - "Remove" the global variable by deleting its initializer,
206*9880d681SAndroid Build Coastguard Worker // making it external.
207*9880d681SAndroid Build Coastguard Worker //
DeleteGlobalInitializer(GlobalVariable * GV)208*9880d681SAndroid Build Coastguard Worker void llvm::DeleteGlobalInitializer(GlobalVariable *GV) {
209*9880d681SAndroid Build Coastguard Worker eliminateAliases(GV);
210*9880d681SAndroid Build Coastguard Worker GV->setInitializer(nullptr);
211*9880d681SAndroid Build Coastguard Worker }
212*9880d681SAndroid Build Coastguard Worker
213*9880d681SAndroid Build Coastguard Worker // DeleteFunctionBody - "Remove" the function by deleting all of its basic
214*9880d681SAndroid Build Coastguard Worker // blocks, making it external.
215*9880d681SAndroid Build Coastguard Worker //
DeleteFunctionBody(Function * F)216*9880d681SAndroid Build Coastguard Worker void llvm::DeleteFunctionBody(Function *F) {
217*9880d681SAndroid Build Coastguard Worker eliminateAliases(F);
218*9880d681SAndroid Build Coastguard Worker // Function declarations can't have comdats.
219*9880d681SAndroid Build Coastguard Worker F->setComdat(nullptr);
220*9880d681SAndroid Build Coastguard Worker
221*9880d681SAndroid Build Coastguard Worker // delete the body of the function...
222*9880d681SAndroid Build Coastguard Worker F->deleteBody();
223*9880d681SAndroid Build Coastguard Worker assert(F->isDeclaration() && "This didn't make the function external!");
224*9880d681SAndroid Build Coastguard Worker }
225*9880d681SAndroid Build Coastguard Worker
226*9880d681SAndroid Build Coastguard Worker /// GetTorInit - Given a list of entries for static ctors/dtors, return them
227*9880d681SAndroid Build Coastguard Worker /// as a constant array.
GetTorInit(std::vector<std::pair<Function *,int>> & TorList)228*9880d681SAndroid Build Coastguard Worker static Constant *GetTorInit(std::vector<std::pair<Function*, int> > &TorList) {
229*9880d681SAndroid Build Coastguard Worker assert(!TorList.empty() && "Don't create empty tor list!");
230*9880d681SAndroid Build Coastguard Worker std::vector<Constant*> ArrayElts;
231*9880d681SAndroid Build Coastguard Worker Type *Int32Ty = Type::getInt32Ty(TorList[0].first->getContext());
232*9880d681SAndroid Build Coastguard Worker
233*9880d681SAndroid Build Coastguard Worker StructType *STy =
234*9880d681SAndroid Build Coastguard Worker StructType::get(Int32Ty, TorList[0].first->getType(), nullptr);
235*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = TorList.size(); i != e; ++i) {
236*9880d681SAndroid Build Coastguard Worker Constant *Elts[] = {
237*9880d681SAndroid Build Coastguard Worker ConstantInt::get(Int32Ty, TorList[i].second),
238*9880d681SAndroid Build Coastguard Worker TorList[i].first
239*9880d681SAndroid Build Coastguard Worker };
240*9880d681SAndroid Build Coastguard Worker ArrayElts.push_back(ConstantStruct::get(STy, Elts));
241*9880d681SAndroid Build Coastguard Worker }
242*9880d681SAndroid Build Coastguard Worker return ConstantArray::get(ArrayType::get(ArrayElts[0]->getType(),
243*9880d681SAndroid Build Coastguard Worker ArrayElts.size()),
244*9880d681SAndroid Build Coastguard Worker ArrayElts);
245*9880d681SAndroid Build Coastguard Worker }
246*9880d681SAndroid Build Coastguard Worker
247*9880d681SAndroid Build Coastguard Worker /// SplitStaticCtorDtor - A module was recently split into two parts, M1/M2, and
248*9880d681SAndroid Build Coastguard Worker /// M1 has all of the global variables. If M2 contains any functions that are
249*9880d681SAndroid Build Coastguard Worker /// static ctors/dtors, we need to add an llvm.global_[cd]tors global to M2, and
250*9880d681SAndroid Build Coastguard Worker /// prune appropriate entries out of M1s list.
SplitStaticCtorDtor(const char * GlobalName,Module * M1,Module * M2,ValueToValueMapTy & VMap)251*9880d681SAndroid Build Coastguard Worker static void SplitStaticCtorDtor(const char *GlobalName, Module *M1, Module *M2,
252*9880d681SAndroid Build Coastguard Worker ValueToValueMapTy &VMap) {
253*9880d681SAndroid Build Coastguard Worker GlobalVariable *GV = M1->getNamedGlobal(GlobalName);
254*9880d681SAndroid Build Coastguard Worker if (!GV || GV->isDeclaration() || GV->hasLocalLinkage() ||
255*9880d681SAndroid Build Coastguard Worker !GV->use_empty()) return;
256*9880d681SAndroid Build Coastguard Worker
257*9880d681SAndroid Build Coastguard Worker std::vector<std::pair<Function*, int> > M1Tors, M2Tors;
258*9880d681SAndroid Build Coastguard Worker ConstantArray *InitList = dyn_cast<ConstantArray>(GV->getInitializer());
259*9880d681SAndroid Build Coastguard Worker if (!InitList) return;
260*9880d681SAndroid Build Coastguard Worker
261*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = InitList->getNumOperands(); i != e; ++i) {
262*9880d681SAndroid Build Coastguard Worker if (ConstantStruct *CS = dyn_cast<ConstantStruct>(InitList->getOperand(i))){
263*9880d681SAndroid Build Coastguard Worker if (CS->getNumOperands() != 2) return; // Not array of 2-element structs.
264*9880d681SAndroid Build Coastguard Worker
265*9880d681SAndroid Build Coastguard Worker if (CS->getOperand(1)->isNullValue())
266*9880d681SAndroid Build Coastguard Worker break; // Found a null terminator, stop here.
267*9880d681SAndroid Build Coastguard Worker
268*9880d681SAndroid Build Coastguard Worker ConstantInt *CI = dyn_cast<ConstantInt>(CS->getOperand(0));
269*9880d681SAndroid Build Coastguard Worker int Priority = CI ? CI->getSExtValue() : 0;
270*9880d681SAndroid Build Coastguard Worker
271*9880d681SAndroid Build Coastguard Worker Constant *FP = CS->getOperand(1);
272*9880d681SAndroid Build Coastguard Worker if (ConstantExpr *CE = dyn_cast<ConstantExpr>(FP))
273*9880d681SAndroid Build Coastguard Worker if (CE->isCast())
274*9880d681SAndroid Build Coastguard Worker FP = CE->getOperand(0);
275*9880d681SAndroid Build Coastguard Worker if (Function *F = dyn_cast<Function>(FP)) {
276*9880d681SAndroid Build Coastguard Worker if (!F->isDeclaration())
277*9880d681SAndroid Build Coastguard Worker M1Tors.push_back(std::make_pair(F, Priority));
278*9880d681SAndroid Build Coastguard Worker else {
279*9880d681SAndroid Build Coastguard Worker // Map to M2's version of the function.
280*9880d681SAndroid Build Coastguard Worker F = cast<Function>(VMap[F]);
281*9880d681SAndroid Build Coastguard Worker M2Tors.push_back(std::make_pair(F, Priority));
282*9880d681SAndroid Build Coastguard Worker }
283*9880d681SAndroid Build Coastguard Worker }
284*9880d681SAndroid Build Coastguard Worker }
285*9880d681SAndroid Build Coastguard Worker }
286*9880d681SAndroid Build Coastguard Worker
287*9880d681SAndroid Build Coastguard Worker GV->eraseFromParent();
288*9880d681SAndroid Build Coastguard Worker if (!M1Tors.empty()) {
289*9880d681SAndroid Build Coastguard Worker Constant *M1Init = GetTorInit(M1Tors);
290*9880d681SAndroid Build Coastguard Worker new GlobalVariable(*M1, M1Init->getType(), false,
291*9880d681SAndroid Build Coastguard Worker GlobalValue::AppendingLinkage,
292*9880d681SAndroid Build Coastguard Worker M1Init, GlobalName);
293*9880d681SAndroid Build Coastguard Worker }
294*9880d681SAndroid Build Coastguard Worker
295*9880d681SAndroid Build Coastguard Worker GV = M2->getNamedGlobal(GlobalName);
296*9880d681SAndroid Build Coastguard Worker assert(GV && "Not a clone of M1?");
297*9880d681SAndroid Build Coastguard Worker assert(GV->use_empty() && "llvm.ctors shouldn't have uses!");
298*9880d681SAndroid Build Coastguard Worker
299*9880d681SAndroid Build Coastguard Worker GV->eraseFromParent();
300*9880d681SAndroid Build Coastguard Worker if (!M2Tors.empty()) {
301*9880d681SAndroid Build Coastguard Worker Constant *M2Init = GetTorInit(M2Tors);
302*9880d681SAndroid Build Coastguard Worker new GlobalVariable(*M2, M2Init->getType(), false,
303*9880d681SAndroid Build Coastguard Worker GlobalValue::AppendingLinkage,
304*9880d681SAndroid Build Coastguard Worker M2Init, GlobalName);
305*9880d681SAndroid Build Coastguard Worker }
306*9880d681SAndroid Build Coastguard Worker }
307*9880d681SAndroid Build Coastguard Worker
308*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module>
SplitFunctionsOutOfModule(Module * M,const std::vector<Function * > & F,ValueToValueMapTy & VMap)309*9880d681SAndroid Build Coastguard Worker llvm::SplitFunctionsOutOfModule(Module *M, const std::vector<Function *> &F,
310*9880d681SAndroid Build Coastguard Worker ValueToValueMapTy &VMap) {
311*9880d681SAndroid Build Coastguard Worker // Make sure functions & globals are all external so that linkage
312*9880d681SAndroid Build Coastguard Worker // between the two modules will work.
313*9880d681SAndroid Build Coastguard Worker for (Module::iterator I = M->begin(), E = M->end(); I != E; ++I)
314*9880d681SAndroid Build Coastguard Worker I->setLinkage(GlobalValue::ExternalLinkage);
315*9880d681SAndroid Build Coastguard Worker for (Module::global_iterator I = M->global_begin(), E = M->global_end();
316*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
317*9880d681SAndroid Build Coastguard Worker if (I->hasName() && I->getName()[0] == '\01')
318*9880d681SAndroid Build Coastguard Worker I->setName(I->getName().substr(1));
319*9880d681SAndroid Build Coastguard Worker I->setLinkage(GlobalValue::ExternalLinkage);
320*9880d681SAndroid Build Coastguard Worker }
321*9880d681SAndroid Build Coastguard Worker
322*9880d681SAndroid Build Coastguard Worker ValueToValueMapTy NewVMap;
323*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> New = CloneModule(M, NewVMap);
324*9880d681SAndroid Build Coastguard Worker
325*9880d681SAndroid Build Coastguard Worker // Remove the Test functions from the Safe module
326*9880d681SAndroid Build Coastguard Worker std::set<Function *> TestFunctions;
327*9880d681SAndroid Build Coastguard Worker for (unsigned i = 0, e = F.size(); i != e; ++i) {
328*9880d681SAndroid Build Coastguard Worker Function *TNOF = cast<Function>(VMap[F[i]]);
329*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "Removing function ");
330*9880d681SAndroid Build Coastguard Worker DEBUG(TNOF->printAsOperand(errs(), false));
331*9880d681SAndroid Build Coastguard Worker DEBUG(errs() << "\n");
332*9880d681SAndroid Build Coastguard Worker TestFunctions.insert(cast<Function>(NewVMap[TNOF]));
333*9880d681SAndroid Build Coastguard Worker DeleteFunctionBody(TNOF); // Function is now external in this module!
334*9880d681SAndroid Build Coastguard Worker }
335*9880d681SAndroid Build Coastguard Worker
336*9880d681SAndroid Build Coastguard Worker
337*9880d681SAndroid Build Coastguard Worker // Remove the Safe functions from the Test module
338*9880d681SAndroid Build Coastguard Worker for (Function &I : *New)
339*9880d681SAndroid Build Coastguard Worker if (!TestFunctions.count(&I))
340*9880d681SAndroid Build Coastguard Worker DeleteFunctionBody(&I);
341*9880d681SAndroid Build Coastguard Worker
342*9880d681SAndroid Build Coastguard Worker // Try to split the global initializers evenly
343*9880d681SAndroid Build Coastguard Worker for (GlobalVariable &I : M->globals()) {
344*9880d681SAndroid Build Coastguard Worker GlobalVariable *GV = cast<GlobalVariable>(NewVMap[&I]);
345*9880d681SAndroid Build Coastguard Worker if (Function *TestFn = globalInitUsesExternalBA(&I)) {
346*9880d681SAndroid Build Coastguard Worker if (Function *SafeFn = globalInitUsesExternalBA(GV)) {
347*9880d681SAndroid Build Coastguard Worker errs() << "*** Error: when reducing functions, encountered "
348*9880d681SAndroid Build Coastguard Worker "the global '";
349*9880d681SAndroid Build Coastguard Worker GV->printAsOperand(errs(), false);
350*9880d681SAndroid Build Coastguard Worker errs() << "' with an initializer that references blockaddresses "
351*9880d681SAndroid Build Coastguard Worker "from safe function '" << SafeFn->getName()
352*9880d681SAndroid Build Coastguard Worker << "' and from test function '" << TestFn->getName() << "'.\n";
353*9880d681SAndroid Build Coastguard Worker exit(1);
354*9880d681SAndroid Build Coastguard Worker }
355*9880d681SAndroid Build Coastguard Worker DeleteGlobalInitializer(&I); // Delete the initializer to make it external
356*9880d681SAndroid Build Coastguard Worker } else {
357*9880d681SAndroid Build Coastguard Worker // If we keep it in the safe module, then delete it in the test module
358*9880d681SAndroid Build Coastguard Worker DeleteGlobalInitializer(GV);
359*9880d681SAndroid Build Coastguard Worker }
360*9880d681SAndroid Build Coastguard Worker }
361*9880d681SAndroid Build Coastguard Worker
362*9880d681SAndroid Build Coastguard Worker // Make sure that there is a global ctor/dtor array in both halves of the
363*9880d681SAndroid Build Coastguard Worker // module if they both have static ctor/dtor functions.
364*9880d681SAndroid Build Coastguard Worker SplitStaticCtorDtor("llvm.global_ctors", M, New.get(), NewVMap);
365*9880d681SAndroid Build Coastguard Worker SplitStaticCtorDtor("llvm.global_dtors", M, New.get(), NewVMap);
366*9880d681SAndroid Build Coastguard Worker
367*9880d681SAndroid Build Coastguard Worker return New;
368*9880d681SAndroid Build Coastguard Worker }
369*9880d681SAndroid Build Coastguard Worker
370*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
371*9880d681SAndroid Build Coastguard Worker // Basic Block Extraction Code
372*9880d681SAndroid Build Coastguard Worker //===----------------------------------------------------------------------===//
373*9880d681SAndroid Build Coastguard Worker
374*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module>
extractMappedBlocksFromModule(const std::vector<BasicBlock * > & BBs,Module * M)375*9880d681SAndroid Build Coastguard Worker BugDriver::extractMappedBlocksFromModule(const std::vector<BasicBlock *> &BBs,
376*9880d681SAndroid Build Coastguard Worker Module *M) {
377*9880d681SAndroid Build Coastguard Worker SmallString<128> Filename;
378*9880d681SAndroid Build Coastguard Worker int FD;
379*9880d681SAndroid Build Coastguard Worker std::error_code EC = sys::fs::createUniqueFile(
380*9880d681SAndroid Build Coastguard Worker OutputPrefix + "-extractblocks%%%%%%%", FD, Filename);
381*9880d681SAndroid Build Coastguard Worker if (EC) {
382*9880d681SAndroid Build Coastguard Worker outs() << "*** Basic Block extraction failed!\n";
383*9880d681SAndroid Build Coastguard Worker errs() << "Error creating temporary file: " << EC.message() << "\n";
384*9880d681SAndroid Build Coastguard Worker EmitProgressBitcode(M, "basicblockextractfail", true);
385*9880d681SAndroid Build Coastguard Worker return nullptr;
386*9880d681SAndroid Build Coastguard Worker }
387*9880d681SAndroid Build Coastguard Worker sys::RemoveFileOnSignal(Filename);
388*9880d681SAndroid Build Coastguard Worker
389*9880d681SAndroid Build Coastguard Worker tool_output_file BlocksToNotExtractFile(Filename.c_str(), FD);
390*9880d681SAndroid Build Coastguard Worker for (std::vector<BasicBlock*>::const_iterator I = BBs.begin(), E = BBs.end();
391*9880d681SAndroid Build Coastguard Worker I != E; ++I) {
392*9880d681SAndroid Build Coastguard Worker BasicBlock *BB = *I;
393*9880d681SAndroid Build Coastguard Worker // If the BB doesn't have a name, give it one so we have something to key
394*9880d681SAndroid Build Coastguard Worker // off of.
395*9880d681SAndroid Build Coastguard Worker if (!BB->hasName()) BB->setName("tmpbb");
396*9880d681SAndroid Build Coastguard Worker BlocksToNotExtractFile.os() << BB->getParent()->getName() << " "
397*9880d681SAndroid Build Coastguard Worker << BB->getName() << "\n";
398*9880d681SAndroid Build Coastguard Worker }
399*9880d681SAndroid Build Coastguard Worker BlocksToNotExtractFile.os().close();
400*9880d681SAndroid Build Coastguard Worker if (BlocksToNotExtractFile.os().has_error()) {
401*9880d681SAndroid Build Coastguard Worker errs() << "Error writing list of blocks to not extract\n";
402*9880d681SAndroid Build Coastguard Worker EmitProgressBitcode(M, "basicblockextractfail", true);
403*9880d681SAndroid Build Coastguard Worker BlocksToNotExtractFile.os().clear_error();
404*9880d681SAndroid Build Coastguard Worker return nullptr;
405*9880d681SAndroid Build Coastguard Worker }
406*9880d681SAndroid Build Coastguard Worker BlocksToNotExtractFile.keep();
407*9880d681SAndroid Build Coastguard Worker
408*9880d681SAndroid Build Coastguard Worker std::string uniqueFN = "--extract-blocks-file=";
409*9880d681SAndroid Build Coastguard Worker uniqueFN += Filename.str();
410*9880d681SAndroid Build Coastguard Worker const char *ExtraArg = uniqueFN.c_str();
411*9880d681SAndroid Build Coastguard Worker
412*9880d681SAndroid Build Coastguard Worker std::vector<std::string> PI;
413*9880d681SAndroid Build Coastguard Worker PI.push_back("extract-blocks");
414*9880d681SAndroid Build Coastguard Worker std::unique_ptr<Module> Ret = runPassesOn(M, PI, 1, &ExtraArg);
415*9880d681SAndroid Build Coastguard Worker
416*9880d681SAndroid Build Coastguard Worker sys::fs::remove(Filename.c_str());
417*9880d681SAndroid Build Coastguard Worker
418*9880d681SAndroid Build Coastguard Worker if (!Ret) {
419*9880d681SAndroid Build Coastguard Worker outs() << "*** Basic Block extraction failed, please report a bug!\n";
420*9880d681SAndroid Build Coastguard Worker EmitProgressBitcode(M, "basicblockextractfail", true);
421*9880d681SAndroid Build Coastguard Worker }
422*9880d681SAndroid Build Coastguard Worker return Ret;
423*9880d681SAndroid Build Coastguard Worker }
424