ExtractFunction.cpp 14 KB

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