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