CodeExtractor.cpp 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620
  1. //===- CodeExtractor.cpp - Pull code region into a new function -----------===//
  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 the interface to tear out a code region, such as an
  10. // individual loop or a parallel section, into a new function, replacing it with
  11. // a call to the new function.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #include "llvm/Transforms/Utils/CodeExtractor.h"
  15. #include "llvm/ADT/ArrayRef.h"
  16. #include "llvm/ADT/DenseMap.h"
  17. #include "llvm/ADT/Optional.h"
  18. #include "llvm/ADT/STLExtras.h"
  19. #include "llvm/ADT/SetVector.h"
  20. #include "llvm/ADT/SmallPtrSet.h"
  21. #include "llvm/ADT/SmallVector.h"
  22. #include "llvm/Analysis/AssumptionCache.h"
  23. #include "llvm/Analysis/BlockFrequencyInfo.h"
  24. #include "llvm/Analysis/BlockFrequencyInfoImpl.h"
  25. #include "llvm/Analysis/BranchProbabilityInfo.h"
  26. #include "llvm/Analysis/LoopInfo.h"
  27. #include "llvm/IR/Argument.h"
  28. #include "llvm/IR/Attributes.h"
  29. #include "llvm/IR/BasicBlock.h"
  30. #include "llvm/IR/CFG.h"
  31. #include "llvm/IR/Constant.h"
  32. #include "llvm/IR/Constants.h"
  33. #include "llvm/IR/DataLayout.h"
  34. #include "llvm/IR/DerivedTypes.h"
  35. #include "llvm/IR/Dominators.h"
  36. #include "llvm/IR/Function.h"
  37. #include "llvm/IR/GlobalValue.h"
  38. #include "llvm/IR/InstrTypes.h"
  39. #include "llvm/IR/Instruction.h"
  40. #include "llvm/IR/Instructions.h"
  41. #include "llvm/IR/IntrinsicInst.h"
  42. #include "llvm/IR/Intrinsics.h"
  43. #include "llvm/IR/LLVMContext.h"
  44. #include "llvm/IR/MDBuilder.h"
  45. #include "llvm/IR/Module.h"
  46. #include "llvm/IR/PatternMatch.h"
  47. #include "llvm/IR/Type.h"
  48. #include "llvm/IR/User.h"
  49. #include "llvm/IR/Value.h"
  50. #include "llvm/IR/Verifier.h"
  51. #include "llvm/Pass.h"
  52. #include "llvm/Support/BlockFrequency.h"
  53. #include "llvm/Support/BranchProbability.h"
  54. #include "llvm/Support/Casting.h"
  55. #include "llvm/Support/CommandLine.h"
  56. #include "llvm/Support/Debug.h"
  57. #include "llvm/Support/ErrorHandling.h"
  58. #include "llvm/Support/raw_ostream.h"
  59. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  60. #include "llvm/Transforms/Utils/Local.h"
  61. #include <cassert>
  62. #include <cstdint>
  63. #include <iterator>
  64. #include <map>
  65. #include <set>
  66. #include <utility>
  67. #include <vector>
  68. using namespace llvm;
  69. using namespace llvm::PatternMatch;
  70. using ProfileCount = Function::ProfileCount;
  71. #define DEBUG_TYPE "code-extractor"
  72. // Provide a command-line option to aggregate function arguments into a struct
  73. // for functions produced by the code extractor. This is useful when converting
  74. // extracted functions to pthread-based code, as only one argument (void*) can
  75. // be passed in to pthread_create().
  76. static cl::opt<bool>
  77. AggregateArgsOpt("aggregate-extracted-args", cl::Hidden,
  78. cl::desc("Aggregate arguments to code-extracted functions"));
  79. /// Test whether a block is valid for extraction.
  80. static bool isBlockValidForExtraction(const BasicBlock &BB,
  81. const SetVector<BasicBlock *> &Result,
  82. bool AllowVarArgs, bool AllowAlloca) {
  83. // taking the address of a basic block moved to another function is illegal
  84. if (BB.hasAddressTaken())
  85. return false;
  86. // don't hoist code that uses another basicblock address, as it's likely to
  87. // lead to unexpected behavior, like cross-function jumps
  88. SmallPtrSet<User const *, 16> Visited;
  89. SmallVector<User const *, 16> ToVisit;
  90. for (Instruction const &Inst : BB)
  91. ToVisit.push_back(&Inst);
  92. while (!ToVisit.empty()) {
  93. User const *Curr = ToVisit.pop_back_val();
  94. if (!Visited.insert(Curr).second)
  95. continue;
  96. if (isa<BlockAddress const>(Curr))
  97. return false; // even a reference to self is likely to be not compatible
  98. if (isa<Instruction>(Curr) && cast<Instruction>(Curr)->getParent() != &BB)
  99. continue;
  100. for (auto const &U : Curr->operands()) {
  101. if (auto *UU = dyn_cast<User>(U))
  102. ToVisit.push_back(UU);
  103. }
  104. }
  105. // If explicitly requested, allow vastart and alloca. For invoke instructions
  106. // verify that extraction is valid.
  107. for (BasicBlock::const_iterator I = BB.begin(), E = BB.end(); I != E; ++I) {
  108. if (isa<AllocaInst>(I)) {
  109. if (!AllowAlloca)
  110. return false;
  111. continue;
  112. }
  113. if (const auto *II = dyn_cast<InvokeInst>(I)) {
  114. // Unwind destination (either a landingpad, catchswitch, or cleanuppad)
  115. // must be a part of the subgraph which is being extracted.
  116. if (auto *UBB = II->getUnwindDest())
  117. if (!Result.count(UBB))
  118. return false;
  119. continue;
  120. }
  121. // All catch handlers of a catchswitch instruction as well as the unwind
  122. // destination must be in the subgraph.
  123. if (const auto *CSI = dyn_cast<CatchSwitchInst>(I)) {
  124. if (auto *UBB = CSI->getUnwindDest())
  125. if (!Result.count(UBB))
  126. return false;
  127. for (auto *HBB : CSI->handlers())
  128. if (!Result.count(const_cast<BasicBlock*>(HBB)))
  129. return false;
  130. continue;
  131. }
  132. // Make sure that entire catch handler is within subgraph. It is sufficient
  133. // to check that catch return's block is in the list.
  134. if (const auto *CPI = dyn_cast<CatchPadInst>(I)) {
  135. for (const auto *U : CPI->users())
  136. if (const auto *CRI = dyn_cast<CatchReturnInst>(U))
  137. if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
  138. return false;
  139. continue;
  140. }
  141. // And do similar checks for cleanup handler - the entire handler must be
  142. // in subgraph which is going to be extracted. For cleanup return should
  143. // additionally check that the unwind destination is also in the subgraph.
  144. if (const auto *CPI = dyn_cast<CleanupPadInst>(I)) {
  145. for (const auto *U : CPI->users())
  146. if (const auto *CRI = dyn_cast<CleanupReturnInst>(U))
  147. if (!Result.count(const_cast<BasicBlock*>(CRI->getParent())))
  148. return false;
  149. continue;
  150. }
  151. if (const auto *CRI = dyn_cast<CleanupReturnInst>(I)) {
  152. if (auto *UBB = CRI->getUnwindDest())
  153. if (!Result.count(UBB))
  154. return false;
  155. continue;
  156. }
  157. if (const CallInst *CI = dyn_cast<CallInst>(I)) {
  158. if (const Function *F = CI->getCalledFunction()) {
  159. auto IID = F->getIntrinsicID();
  160. if (IID == Intrinsic::vastart) {
  161. if (AllowVarArgs)
  162. continue;
  163. else
  164. return false;
  165. }
  166. // Currently, we miscompile outlined copies of eh_typid_for. There are
  167. // proposals for fixing this in llvm.org/PR39545.
  168. if (IID == Intrinsic::eh_typeid_for)
  169. return false;
  170. }
  171. }
  172. }
  173. return true;
  174. }
  175. /// Build a set of blocks to extract if the input blocks are viable.
  176. static SetVector<BasicBlock *>
  177. buildExtractionBlockSet(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
  178. bool AllowVarArgs, bool AllowAlloca) {
  179. assert(!BBs.empty() && "The set of blocks to extract must be non-empty");
  180. SetVector<BasicBlock *> Result;
  181. // Loop over the blocks, adding them to our set-vector, and aborting with an
  182. // empty set if we encounter invalid blocks.
  183. for (BasicBlock *BB : BBs) {
  184. // If this block is dead, don't process it.
  185. if (DT && !DT->isReachableFromEntry(BB))
  186. continue;
  187. if (!Result.insert(BB))
  188. llvm_unreachable("Repeated basic blocks in extraction input");
  189. }
  190. LLVM_DEBUG(dbgs() << "Region front block: " << Result.front()->getName()
  191. << '\n');
  192. for (auto *BB : Result) {
  193. if (!isBlockValidForExtraction(*BB, Result, AllowVarArgs, AllowAlloca))
  194. return {};
  195. // Make sure that the first block is not a landing pad.
  196. if (BB == Result.front()) {
  197. if (BB->isEHPad()) {
  198. LLVM_DEBUG(dbgs() << "The first block cannot be an unwind block\n");
  199. return {};
  200. }
  201. continue;
  202. }
  203. // All blocks other than the first must not have predecessors outside of
  204. // the subgraph which is being extracted.
  205. for (auto *PBB : predecessors(BB))
  206. if (!Result.count(PBB)) {
  207. LLVM_DEBUG(dbgs() << "No blocks in this region may have entries from "
  208. "outside the region except for the first block!\n"
  209. << "Problematic source BB: " << BB->getName() << "\n"
  210. << "Problematic destination BB: " << PBB->getName()
  211. << "\n");
  212. return {};
  213. }
  214. }
  215. return Result;
  216. }
  217. CodeExtractor::CodeExtractor(ArrayRef<BasicBlock *> BBs, DominatorTree *DT,
  218. bool AggregateArgs, BlockFrequencyInfo *BFI,
  219. BranchProbabilityInfo *BPI, AssumptionCache *AC,
  220. bool AllowVarArgs, bool AllowAlloca,
  221. std::string Suffix)
  222. : DT(DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
  223. BPI(BPI), AC(AC), AllowVarArgs(AllowVarArgs),
  224. Blocks(buildExtractionBlockSet(BBs, DT, AllowVarArgs, AllowAlloca)),
  225. Suffix(Suffix) {}
  226. CodeExtractor::CodeExtractor(DominatorTree &DT, Loop &L, bool AggregateArgs,
  227. BlockFrequencyInfo *BFI,
  228. BranchProbabilityInfo *BPI, AssumptionCache *AC,
  229. std::string Suffix)
  230. : DT(&DT), AggregateArgs(AggregateArgs || AggregateArgsOpt), BFI(BFI),
  231. BPI(BPI), AC(AC), AllowVarArgs(false),
  232. Blocks(buildExtractionBlockSet(L.getBlocks(), &DT,
  233. /* AllowVarArgs */ false,
  234. /* AllowAlloca */ false)),
  235. Suffix(Suffix) {}
  236. /// definedInRegion - Return true if the specified value is defined in the
  237. /// extracted region.
  238. static bool definedInRegion(const SetVector<BasicBlock *> &Blocks, Value *V) {
  239. if (Instruction *I = dyn_cast<Instruction>(V))
  240. if (Blocks.count(I->getParent()))
  241. return true;
  242. return false;
  243. }
  244. /// definedInCaller - Return true if the specified value is defined in the
  245. /// function being code extracted, but not in the region being extracted.
  246. /// These values must be passed in as live-ins to the function.
  247. static bool definedInCaller(const SetVector<BasicBlock *> &Blocks, Value *V) {
  248. if (isa<Argument>(V)) return true;
  249. if (Instruction *I = dyn_cast<Instruction>(V))
  250. if (!Blocks.count(I->getParent()))
  251. return true;
  252. return false;
  253. }
  254. static BasicBlock *getCommonExitBlock(const SetVector<BasicBlock *> &Blocks) {
  255. BasicBlock *CommonExitBlock = nullptr;
  256. auto hasNonCommonExitSucc = [&](BasicBlock *Block) {
  257. for (auto *Succ : successors(Block)) {
  258. // Internal edges, ok.
  259. if (Blocks.count(Succ))
  260. continue;
  261. if (!CommonExitBlock) {
  262. CommonExitBlock = Succ;
  263. continue;
  264. }
  265. if (CommonExitBlock != Succ)
  266. return true;
  267. }
  268. return false;
  269. };
  270. if (any_of(Blocks, hasNonCommonExitSucc))
  271. return nullptr;
  272. return CommonExitBlock;
  273. }
  274. CodeExtractorAnalysisCache::CodeExtractorAnalysisCache(Function &F) {
  275. for (BasicBlock &BB : F) {
  276. for (Instruction &II : BB.instructionsWithoutDebug())
  277. if (auto *AI = dyn_cast<AllocaInst>(&II))
  278. Allocas.push_back(AI);
  279. findSideEffectInfoForBlock(BB);
  280. }
  281. }
  282. void CodeExtractorAnalysisCache::findSideEffectInfoForBlock(BasicBlock &BB) {
  283. for (Instruction &II : BB.instructionsWithoutDebug()) {
  284. unsigned Opcode = II.getOpcode();
  285. Value *MemAddr = nullptr;
  286. switch (Opcode) {
  287. case Instruction::Store:
  288. case Instruction::Load: {
  289. if (Opcode == Instruction::Store) {
  290. StoreInst *SI = cast<StoreInst>(&II);
  291. MemAddr = SI->getPointerOperand();
  292. } else {
  293. LoadInst *LI = cast<LoadInst>(&II);
  294. MemAddr = LI->getPointerOperand();
  295. }
  296. // Global variable can not be aliased with locals.
  297. if (dyn_cast<Constant>(MemAddr))
  298. break;
  299. Value *Base = MemAddr->stripInBoundsConstantOffsets();
  300. if (!isa<AllocaInst>(Base)) {
  301. SideEffectingBlocks.insert(&BB);
  302. return;
  303. }
  304. BaseMemAddrs[&BB].insert(Base);
  305. break;
  306. }
  307. default: {
  308. IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(&II);
  309. if (IntrInst) {
  310. if (IntrInst->isLifetimeStartOrEnd())
  311. break;
  312. SideEffectingBlocks.insert(&BB);
  313. return;
  314. }
  315. // Treat all the other cases conservatively if it has side effects.
  316. if (II.mayHaveSideEffects()) {
  317. SideEffectingBlocks.insert(&BB);
  318. return;
  319. }
  320. }
  321. }
  322. }
  323. }
  324. bool CodeExtractorAnalysisCache::doesBlockContainClobberOfAddr(
  325. BasicBlock &BB, AllocaInst *Addr) const {
  326. if (SideEffectingBlocks.count(&BB))
  327. return true;
  328. auto It = BaseMemAddrs.find(&BB);
  329. if (It != BaseMemAddrs.end())
  330. return It->second.count(Addr);
  331. return false;
  332. }
  333. bool CodeExtractor::isLegalToShrinkwrapLifetimeMarkers(
  334. const CodeExtractorAnalysisCache &CEAC, Instruction *Addr) const {
  335. AllocaInst *AI = cast<AllocaInst>(Addr->stripInBoundsConstantOffsets());
  336. Function *Func = (*Blocks.begin())->getParent();
  337. for (BasicBlock &BB : *Func) {
  338. if (Blocks.count(&BB))
  339. continue;
  340. if (CEAC.doesBlockContainClobberOfAddr(BB, AI))
  341. return false;
  342. }
  343. return true;
  344. }
  345. BasicBlock *
  346. CodeExtractor::findOrCreateBlockForHoisting(BasicBlock *CommonExitBlock) {
  347. BasicBlock *SinglePredFromOutlineRegion = nullptr;
  348. assert(!Blocks.count(CommonExitBlock) &&
  349. "Expect a block outside the region!");
  350. for (auto *Pred : predecessors(CommonExitBlock)) {
  351. if (!Blocks.count(Pred))
  352. continue;
  353. if (!SinglePredFromOutlineRegion) {
  354. SinglePredFromOutlineRegion = Pred;
  355. } else if (SinglePredFromOutlineRegion != Pred) {
  356. SinglePredFromOutlineRegion = nullptr;
  357. break;
  358. }
  359. }
  360. if (SinglePredFromOutlineRegion)
  361. return SinglePredFromOutlineRegion;
  362. #ifndef NDEBUG
  363. auto getFirstPHI = [](BasicBlock *BB) {
  364. BasicBlock::iterator I = BB->begin();
  365. PHINode *FirstPhi = nullptr;
  366. while (I != BB->end()) {
  367. PHINode *Phi = dyn_cast<PHINode>(I);
  368. if (!Phi)
  369. break;
  370. if (!FirstPhi) {
  371. FirstPhi = Phi;
  372. break;
  373. }
  374. }
  375. return FirstPhi;
  376. };
  377. // If there are any phi nodes, the single pred either exists or has already
  378. // be created before code extraction.
  379. assert(!getFirstPHI(CommonExitBlock) && "Phi not expected");
  380. #endif
  381. BasicBlock *NewExitBlock = CommonExitBlock->splitBasicBlock(
  382. CommonExitBlock->getFirstNonPHI()->getIterator());
  383. for (auto PI = pred_begin(CommonExitBlock), PE = pred_end(CommonExitBlock);
  384. PI != PE;) {
  385. BasicBlock *Pred = *PI++;
  386. if (Blocks.count(Pred))
  387. continue;
  388. Pred->getTerminator()->replaceUsesOfWith(CommonExitBlock, NewExitBlock);
  389. }
  390. // Now add the old exit block to the outline region.
  391. Blocks.insert(CommonExitBlock);
  392. return CommonExitBlock;
  393. }
  394. // Find the pair of life time markers for address 'Addr' that are either
  395. // defined inside the outline region or can legally be shrinkwrapped into the
  396. // outline region. If there are not other untracked uses of the address, return
  397. // the pair of markers if found; otherwise return a pair of nullptr.
  398. CodeExtractor::LifetimeMarkerInfo
  399. CodeExtractor::getLifetimeMarkers(const CodeExtractorAnalysisCache &CEAC,
  400. Instruction *Addr,
  401. BasicBlock *ExitBlock) const {
  402. LifetimeMarkerInfo Info;
  403. for (User *U : Addr->users()) {
  404. IntrinsicInst *IntrInst = dyn_cast<IntrinsicInst>(U);
  405. if (IntrInst) {
  406. if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_start) {
  407. // Do not handle the case where Addr has multiple start markers.
  408. if (Info.LifeStart)
  409. return {};
  410. Info.LifeStart = IntrInst;
  411. }
  412. if (IntrInst->getIntrinsicID() == Intrinsic::lifetime_end) {
  413. if (Info.LifeEnd)
  414. return {};
  415. Info.LifeEnd = IntrInst;
  416. }
  417. continue;
  418. }
  419. // Find untracked uses of the address, bail.
  420. if (!definedInRegion(Blocks, U))
  421. return {};
  422. }
  423. if (!Info.LifeStart || !Info.LifeEnd)
  424. return {};
  425. Info.SinkLifeStart = !definedInRegion(Blocks, Info.LifeStart);
  426. Info.HoistLifeEnd = !definedInRegion(Blocks, Info.LifeEnd);
  427. // Do legality check.
  428. if ((Info.SinkLifeStart || Info.HoistLifeEnd) &&
  429. !isLegalToShrinkwrapLifetimeMarkers(CEAC, Addr))
  430. return {};
  431. // Check to see if we have a place to do hoisting, if not, bail.
  432. if (Info.HoistLifeEnd && !ExitBlock)
  433. return {};
  434. return Info;
  435. }
  436. void CodeExtractor::findAllocas(const CodeExtractorAnalysisCache &CEAC,
  437. ValueSet &SinkCands, ValueSet &HoistCands,
  438. BasicBlock *&ExitBlock) const {
  439. Function *Func = (*Blocks.begin())->getParent();
  440. ExitBlock = getCommonExitBlock(Blocks);
  441. auto moveOrIgnoreLifetimeMarkers =
  442. [&](const LifetimeMarkerInfo &LMI) -> bool {
  443. if (!LMI.LifeStart)
  444. return false;
  445. if (LMI.SinkLifeStart) {
  446. LLVM_DEBUG(dbgs() << "Sinking lifetime.start: " << *LMI.LifeStart
  447. << "\n");
  448. SinkCands.insert(LMI.LifeStart);
  449. }
  450. if (LMI.HoistLifeEnd) {
  451. LLVM_DEBUG(dbgs() << "Hoisting lifetime.end: " << *LMI.LifeEnd << "\n");
  452. HoistCands.insert(LMI.LifeEnd);
  453. }
  454. return true;
  455. };
  456. // Look up allocas in the original function in CodeExtractorAnalysisCache, as
  457. // this is much faster than walking all the instructions.
  458. for (AllocaInst *AI : CEAC.getAllocas()) {
  459. BasicBlock *BB = AI->getParent();
  460. if (Blocks.count(BB))
  461. continue;
  462. // As a prior call to extractCodeRegion() may have shrinkwrapped the alloca,
  463. // check whether it is actually still in the original function.
  464. Function *AIFunc = BB->getParent();
  465. if (AIFunc != Func)
  466. continue;
  467. LifetimeMarkerInfo MarkerInfo = getLifetimeMarkers(CEAC, AI, ExitBlock);
  468. bool Moved = moveOrIgnoreLifetimeMarkers(MarkerInfo);
  469. if (Moved) {
  470. LLVM_DEBUG(dbgs() << "Sinking alloca: " << *AI << "\n");
  471. SinkCands.insert(AI);
  472. continue;
  473. }
  474. // Follow any bitcasts.
  475. SmallVector<Instruction *, 2> Bitcasts;
  476. SmallVector<LifetimeMarkerInfo, 2> BitcastLifetimeInfo;
  477. for (User *U : AI->users()) {
  478. if (U->stripInBoundsConstantOffsets() == AI) {
  479. Instruction *Bitcast = cast<Instruction>(U);
  480. LifetimeMarkerInfo LMI = getLifetimeMarkers(CEAC, Bitcast, ExitBlock);
  481. if (LMI.LifeStart) {
  482. Bitcasts.push_back(Bitcast);
  483. BitcastLifetimeInfo.push_back(LMI);
  484. continue;
  485. }
  486. }
  487. // Found unknown use of AI.
  488. if (!definedInRegion(Blocks, U)) {
  489. Bitcasts.clear();
  490. break;
  491. }
  492. }
  493. // Either no bitcasts reference the alloca or there are unknown uses.
  494. if (Bitcasts.empty())
  495. continue;
  496. LLVM_DEBUG(dbgs() << "Sinking alloca (via bitcast): " << *AI << "\n");
  497. SinkCands.insert(AI);
  498. for (unsigned I = 0, E = Bitcasts.size(); I != E; ++I) {
  499. Instruction *BitcastAddr = Bitcasts[I];
  500. const LifetimeMarkerInfo &LMI = BitcastLifetimeInfo[I];
  501. assert(LMI.LifeStart &&
  502. "Unsafe to sink bitcast without lifetime markers");
  503. moveOrIgnoreLifetimeMarkers(LMI);
  504. if (!definedInRegion(Blocks, BitcastAddr)) {
  505. LLVM_DEBUG(dbgs() << "Sinking bitcast-of-alloca: " << *BitcastAddr
  506. << "\n");
  507. SinkCands.insert(BitcastAddr);
  508. }
  509. }
  510. }
  511. }
  512. bool CodeExtractor::isEligible() const {
  513. if (Blocks.empty())
  514. return false;
  515. BasicBlock *Header = *Blocks.begin();
  516. Function *F = Header->getParent();
  517. // For functions with varargs, check that varargs handling is only done in the
  518. // outlined function, i.e vastart and vaend are only used in outlined blocks.
  519. if (AllowVarArgs && F->getFunctionType()->isVarArg()) {
  520. auto containsVarArgIntrinsic = [](const Instruction &I) {
  521. if (const CallInst *CI = dyn_cast<CallInst>(&I))
  522. if (const Function *Callee = CI->getCalledFunction())
  523. return Callee->getIntrinsicID() == Intrinsic::vastart ||
  524. Callee->getIntrinsicID() == Intrinsic::vaend;
  525. return false;
  526. };
  527. for (auto &BB : *F) {
  528. if (Blocks.count(&BB))
  529. continue;
  530. if (llvm::any_of(BB, containsVarArgIntrinsic))
  531. return false;
  532. }
  533. }
  534. return true;
  535. }
  536. void CodeExtractor::findInputsOutputs(ValueSet &Inputs, ValueSet &Outputs,
  537. const ValueSet &SinkCands) const {
  538. for (BasicBlock *BB : Blocks) {
  539. // If a used value is defined outside the region, it's an input. If an
  540. // instruction is used outside the region, it's an output.
  541. for (Instruction &II : *BB) {
  542. for (auto &OI : II.operands()) {
  543. Value *V = OI;
  544. if (!SinkCands.count(V) && definedInCaller(Blocks, V))
  545. Inputs.insert(V);
  546. }
  547. for (User *U : II.users())
  548. if (!definedInRegion(Blocks, U)) {
  549. Outputs.insert(&II);
  550. break;
  551. }
  552. }
  553. }
  554. }
  555. /// severSplitPHINodesOfEntry - If a PHI node has multiple inputs from outside
  556. /// of the region, we need to split the entry block of the region so that the
  557. /// PHI node is easier to deal with.
  558. void CodeExtractor::severSplitPHINodesOfEntry(BasicBlock *&Header) {
  559. unsigned NumPredsFromRegion = 0;
  560. unsigned NumPredsOutsideRegion = 0;
  561. if (Header != &Header->getParent()->getEntryBlock()) {
  562. PHINode *PN = dyn_cast<PHINode>(Header->begin());
  563. if (!PN) return; // No PHI nodes.
  564. // If the header node contains any PHI nodes, check to see if there is more
  565. // than one entry from outside the region. If so, we need to sever the
  566. // header block into two.
  567. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  568. if (Blocks.count(PN->getIncomingBlock(i)))
  569. ++NumPredsFromRegion;
  570. else
  571. ++NumPredsOutsideRegion;
  572. // If there is one (or fewer) predecessor from outside the region, we don't
  573. // need to do anything special.
  574. if (NumPredsOutsideRegion <= 1) return;
  575. }
  576. // Otherwise, we need to split the header block into two pieces: one
  577. // containing PHI nodes merging values from outside of the region, and a
  578. // second that contains all of the code for the block and merges back any
  579. // incoming values from inside of the region.
  580. BasicBlock *NewBB = SplitBlock(Header, Header->getFirstNonPHI(), DT);
  581. // We only want to code extract the second block now, and it becomes the new
  582. // header of the region.
  583. BasicBlock *OldPred = Header;
  584. Blocks.remove(OldPred);
  585. Blocks.insert(NewBB);
  586. Header = NewBB;
  587. // Okay, now we need to adjust the PHI nodes and any branches from within the
  588. // region to go to the new header block instead of the old header block.
  589. if (NumPredsFromRegion) {
  590. PHINode *PN = cast<PHINode>(OldPred->begin());
  591. // Loop over all of the predecessors of OldPred that are in the region,
  592. // changing them to branch to NewBB instead.
  593. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  594. if (Blocks.count(PN->getIncomingBlock(i))) {
  595. Instruction *TI = PN->getIncomingBlock(i)->getTerminator();
  596. TI->replaceUsesOfWith(OldPred, NewBB);
  597. }
  598. // Okay, everything within the region is now branching to the right block, we
  599. // just have to update the PHI nodes now, inserting PHI nodes into NewBB.
  600. BasicBlock::iterator AfterPHIs;
  601. for (AfterPHIs = OldPred->begin(); isa<PHINode>(AfterPHIs); ++AfterPHIs) {
  602. PHINode *PN = cast<PHINode>(AfterPHIs);
  603. // Create a new PHI node in the new region, which has an incoming value
  604. // from OldPred of PN.
  605. PHINode *NewPN = PHINode::Create(PN->getType(), 1 + NumPredsFromRegion,
  606. PN->getName() + ".ce", &NewBB->front());
  607. PN->replaceAllUsesWith(NewPN);
  608. NewPN->addIncoming(PN, OldPred);
  609. // Loop over all of the incoming value in PN, moving them to NewPN if they
  610. // are from the extracted region.
  611. for (unsigned i = 0; i != PN->getNumIncomingValues(); ++i) {
  612. if (Blocks.count(PN->getIncomingBlock(i))) {
  613. NewPN->addIncoming(PN->getIncomingValue(i), PN->getIncomingBlock(i));
  614. PN->removeIncomingValue(i);
  615. --i;
  616. }
  617. }
  618. }
  619. }
  620. }
  621. /// severSplitPHINodesOfExits - if PHI nodes in exit blocks have inputs from
  622. /// outlined region, we split these PHIs on two: one with inputs from region
  623. /// and other with remaining incoming blocks; then first PHIs are placed in
  624. /// outlined region.
  625. void CodeExtractor::severSplitPHINodesOfExits(
  626. const SmallPtrSetImpl<BasicBlock *> &Exits) {
  627. for (BasicBlock *ExitBB : Exits) {
  628. BasicBlock *NewBB = nullptr;
  629. for (PHINode &PN : ExitBB->phis()) {
  630. // Find all incoming values from the outlining region.
  631. SmallVector<unsigned, 2> IncomingVals;
  632. for (unsigned i = 0; i < PN.getNumIncomingValues(); ++i)
  633. if (Blocks.count(PN.getIncomingBlock(i)))
  634. IncomingVals.push_back(i);
  635. // Do not process PHI if there is one (or fewer) predecessor from region.
  636. // If PHI has exactly one predecessor from region, only this one incoming
  637. // will be replaced on codeRepl block, so it should be safe to skip PHI.
  638. if (IncomingVals.size() <= 1)
  639. continue;
  640. // Create block for new PHIs and add it to the list of outlined if it
  641. // wasn't done before.
  642. if (!NewBB) {
  643. NewBB = BasicBlock::Create(ExitBB->getContext(),
  644. ExitBB->getName() + ".split",
  645. ExitBB->getParent(), ExitBB);
  646. SmallVector<BasicBlock *, 4> Preds(pred_begin(ExitBB),
  647. pred_end(ExitBB));
  648. for (BasicBlock *PredBB : Preds)
  649. if (Blocks.count(PredBB))
  650. PredBB->getTerminator()->replaceUsesOfWith(ExitBB, NewBB);
  651. BranchInst::Create(ExitBB, NewBB);
  652. Blocks.insert(NewBB);
  653. }
  654. // Split this PHI.
  655. PHINode *NewPN =
  656. PHINode::Create(PN.getType(), IncomingVals.size(),
  657. PN.getName() + ".ce", NewBB->getFirstNonPHI());
  658. for (unsigned i : IncomingVals)
  659. NewPN->addIncoming(PN.getIncomingValue(i), PN.getIncomingBlock(i));
  660. for (unsigned i : reverse(IncomingVals))
  661. PN.removeIncomingValue(i, false);
  662. PN.addIncoming(NewPN, NewBB);
  663. }
  664. }
  665. }
  666. void CodeExtractor::splitReturnBlocks() {
  667. for (BasicBlock *Block : Blocks)
  668. if (ReturnInst *RI = dyn_cast<ReturnInst>(Block->getTerminator())) {
  669. BasicBlock *New =
  670. Block->splitBasicBlock(RI->getIterator(), Block->getName() + ".ret");
  671. if (DT) {
  672. // Old dominates New. New node dominates all other nodes dominated
  673. // by Old.
  674. DomTreeNode *OldNode = DT->getNode(Block);
  675. SmallVector<DomTreeNode *, 8> Children(OldNode->begin(),
  676. OldNode->end());
  677. DomTreeNode *NewNode = DT->addNewBlock(New, Block);
  678. for (DomTreeNode *I : Children)
  679. DT->changeImmediateDominator(I, NewNode);
  680. }
  681. }
  682. }
  683. /// constructFunction - make a function based on inputs and outputs, as follows:
  684. /// f(in0, ..., inN, out0, ..., outN)
  685. Function *CodeExtractor::constructFunction(const ValueSet &inputs,
  686. const ValueSet &outputs,
  687. BasicBlock *header,
  688. BasicBlock *newRootNode,
  689. BasicBlock *newHeader,
  690. Function *oldFunction,
  691. Module *M) {
  692. LLVM_DEBUG(dbgs() << "inputs: " << inputs.size() << "\n");
  693. LLVM_DEBUG(dbgs() << "outputs: " << outputs.size() << "\n");
  694. // This function returns unsigned, outputs will go back by reference.
  695. switch (NumExitBlocks) {
  696. case 0:
  697. case 1: RetTy = Type::getVoidTy(header->getContext()); break;
  698. case 2: RetTy = Type::getInt1Ty(header->getContext()); break;
  699. default: RetTy = Type::getInt16Ty(header->getContext()); break;
  700. }
  701. std::vector<Type *> paramTy;
  702. // Add the types of the input values to the function's argument list
  703. for (Value *value : inputs) {
  704. LLVM_DEBUG(dbgs() << "value used in func: " << *value << "\n");
  705. paramTy.push_back(value->getType());
  706. }
  707. // Add the types of the output values to the function's argument list.
  708. for (Value *output : outputs) {
  709. LLVM_DEBUG(dbgs() << "instr used in func: " << *output << "\n");
  710. if (AggregateArgs)
  711. paramTy.push_back(output->getType());
  712. else
  713. paramTy.push_back(PointerType::getUnqual(output->getType()));
  714. }
  715. LLVM_DEBUG({
  716. dbgs() << "Function type: " << *RetTy << " f(";
  717. for (Type *i : paramTy)
  718. dbgs() << *i << ", ";
  719. dbgs() << ")\n";
  720. });
  721. StructType *StructTy;
  722. if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
  723. StructTy = StructType::get(M->getContext(), paramTy);
  724. paramTy.clear();
  725. paramTy.push_back(PointerType::getUnqual(StructTy));
  726. }
  727. FunctionType *funcType =
  728. FunctionType::get(RetTy, paramTy,
  729. AllowVarArgs && oldFunction->isVarArg());
  730. std::string SuffixToUse =
  731. Suffix.empty()
  732. ? (header->getName().empty() ? "extracted" : header->getName().str())
  733. : Suffix;
  734. // Create the new function
  735. Function *newFunction = Function::Create(
  736. funcType, GlobalValue::InternalLinkage, oldFunction->getAddressSpace(),
  737. oldFunction->getName() + "." + SuffixToUse, M);
  738. // If the old function is no-throw, so is the new one.
  739. if (oldFunction->doesNotThrow())
  740. newFunction->setDoesNotThrow();
  741. // Inherit the uwtable attribute if we need to.
  742. if (oldFunction->hasUWTable())
  743. newFunction->setHasUWTable();
  744. // Inherit all of the target dependent attributes and white-listed
  745. // target independent attributes.
  746. // (e.g. If the extracted region contains a call to an x86.sse
  747. // instruction we need to make sure that the extracted region has the
  748. // "target-features" attribute allowing it to be lowered.
  749. // FIXME: This should be changed to check to see if a specific
  750. // attribute can not be inherited.
  751. for (const auto &Attr : oldFunction->getAttributes().getFnAttributes()) {
  752. if (Attr.isStringAttribute()) {
  753. if (Attr.getKindAsString() == "thunk")
  754. continue;
  755. } else
  756. switch (Attr.getKindAsEnum()) {
  757. // Those attributes cannot be propagated safely. Explicitly list them
  758. // here so we get a warning if new attributes are added. This list also
  759. // includes non-function attributes.
  760. case Attribute::Alignment:
  761. case Attribute::AllocSize:
  762. case Attribute::ArgMemOnly:
  763. case Attribute::Builtin:
  764. case Attribute::ByVal:
  765. case Attribute::Convergent:
  766. case Attribute::Dereferenceable:
  767. case Attribute::DereferenceableOrNull:
  768. case Attribute::InAlloca:
  769. case Attribute::InReg:
  770. case Attribute::InaccessibleMemOnly:
  771. case Attribute::InaccessibleMemOrArgMemOnly:
  772. case Attribute::JumpTable:
  773. case Attribute::Naked:
  774. case Attribute::Nest:
  775. case Attribute::NoAlias:
  776. case Attribute::NoBuiltin:
  777. case Attribute::NoCapture:
  778. case Attribute::NoReturn:
  779. case Attribute::NoSync:
  780. case Attribute::None:
  781. case Attribute::NonNull:
  782. case Attribute::ReadNone:
  783. case Attribute::ReadOnly:
  784. case Attribute::Returned:
  785. case Attribute::ReturnsTwice:
  786. case Attribute::SExt:
  787. case Attribute::Speculatable:
  788. case Attribute::StackAlignment:
  789. case Attribute::StructRet:
  790. case Attribute::SwiftError:
  791. case Attribute::SwiftSelf:
  792. case Attribute::WillReturn:
  793. case Attribute::WriteOnly:
  794. case Attribute::ZExt:
  795. case Attribute::ImmArg:
  796. case Attribute::EndAttrKinds:
  797. continue;
  798. // Those attributes should be safe to propagate to the extracted function.
  799. case Attribute::AlwaysInline:
  800. case Attribute::Cold:
  801. case Attribute::NoRecurse:
  802. case Attribute::InlineHint:
  803. case Attribute::MinSize:
  804. case Attribute::NoDuplicate:
  805. case Attribute::NoFree:
  806. case Attribute::NoImplicitFloat:
  807. case Attribute::NoInline:
  808. case Attribute::NonLazyBind:
  809. case Attribute::NoRedZone:
  810. case Attribute::NoUnwind:
  811. case Attribute::OptForFuzzing:
  812. case Attribute::OptimizeNone:
  813. case Attribute::OptimizeForSize:
  814. case Attribute::SafeStack:
  815. case Attribute::ShadowCallStack:
  816. case Attribute::SanitizeAddress:
  817. case Attribute::SanitizeMemory:
  818. case Attribute::SanitizeThread:
  819. case Attribute::SanitizeHWAddress:
  820. case Attribute::SanitizeMemTag:
  821. case Attribute::SpeculativeLoadHardening:
  822. case Attribute::StackProtect:
  823. case Attribute::StackProtectReq:
  824. case Attribute::StackProtectStrong:
  825. case Attribute::StrictFP:
  826. case Attribute::UWTable:
  827. case Attribute::NoCfCheck:
  828. break;
  829. }
  830. newFunction->addFnAttr(Attr);
  831. }
  832. newFunction->getBasicBlockList().push_back(newRootNode);
  833. // Create an iterator to name all of the arguments we inserted.
  834. Function::arg_iterator AI = newFunction->arg_begin();
  835. // Rewrite all users of the inputs in the extracted region to use the
  836. // arguments (or appropriate addressing into struct) instead.
  837. for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
  838. Value *RewriteVal;
  839. if (AggregateArgs) {
  840. Value *Idx[2];
  841. Idx[0] = Constant::getNullValue(Type::getInt32Ty(header->getContext()));
  842. Idx[1] = ConstantInt::get(Type::getInt32Ty(header->getContext()), i);
  843. Instruction *TI = newFunction->begin()->getTerminator();
  844. GetElementPtrInst *GEP = GetElementPtrInst::Create(
  845. StructTy, &*AI, Idx, "gep_" + inputs[i]->getName(), TI);
  846. RewriteVal = new LoadInst(StructTy->getElementType(i), GEP,
  847. "loadgep_" + inputs[i]->getName(), TI);
  848. } else
  849. RewriteVal = &*AI++;
  850. std::vector<User *> Users(inputs[i]->user_begin(), inputs[i]->user_end());
  851. for (User *use : Users)
  852. if (Instruction *inst = dyn_cast<Instruction>(use))
  853. if (Blocks.count(inst->getParent()))
  854. inst->replaceUsesOfWith(inputs[i], RewriteVal);
  855. }
  856. // Set names for input and output arguments.
  857. if (!AggregateArgs) {
  858. AI = newFunction->arg_begin();
  859. for (unsigned i = 0, e = inputs.size(); i != e; ++i, ++AI)
  860. AI->setName(inputs[i]->getName());
  861. for (unsigned i = 0, e = outputs.size(); i != e; ++i, ++AI)
  862. AI->setName(outputs[i]->getName()+".out");
  863. }
  864. // Rewrite branches to basic blocks outside of the loop to new dummy blocks
  865. // within the new function. This must be done before we lose track of which
  866. // blocks were originally in the code region.
  867. std::vector<User *> Users(header->user_begin(), header->user_end());
  868. for (unsigned i = 0, e = Users.size(); i != e; ++i)
  869. // The BasicBlock which contains the branch is not in the region
  870. // modify the branch target to a new block
  871. if (Instruction *I = dyn_cast<Instruction>(Users[i]))
  872. if (I->isTerminator() && !Blocks.count(I->getParent()) &&
  873. I->getParent()->getParent() == oldFunction)
  874. I->replaceUsesOfWith(header, newHeader);
  875. return newFunction;
  876. }
  877. /// Erase lifetime.start markers which reference inputs to the extraction
  878. /// region, and insert the referenced memory into \p LifetimesStart.
  879. ///
  880. /// The extraction region is defined by a set of blocks (\p Blocks), and a set
  881. /// of allocas which will be moved from the caller function into the extracted
  882. /// function (\p SunkAllocas).
  883. static void eraseLifetimeMarkersOnInputs(const SetVector<BasicBlock *> &Blocks,
  884. const SetVector<Value *> &SunkAllocas,
  885. SetVector<Value *> &LifetimesStart) {
  886. for (BasicBlock *BB : Blocks) {
  887. for (auto It = BB->begin(), End = BB->end(); It != End;) {
  888. auto *II = dyn_cast<IntrinsicInst>(&*It);
  889. ++It;
  890. if (!II || !II->isLifetimeStartOrEnd())
  891. continue;
  892. // Get the memory operand of the lifetime marker. If the underlying
  893. // object is a sunk alloca, or is otherwise defined in the extraction
  894. // region, the lifetime marker must not be erased.
  895. Value *Mem = II->getOperand(1)->stripInBoundsOffsets();
  896. if (SunkAllocas.count(Mem) || definedInRegion(Blocks, Mem))
  897. continue;
  898. if (II->getIntrinsicID() == Intrinsic::lifetime_start)
  899. LifetimesStart.insert(Mem);
  900. II->eraseFromParent();
  901. }
  902. }
  903. }
  904. /// Insert lifetime start/end markers surrounding the call to the new function
  905. /// for objects defined in the caller.
  906. static void insertLifetimeMarkersSurroundingCall(
  907. Module *M, ArrayRef<Value *> LifetimesStart, ArrayRef<Value *> LifetimesEnd,
  908. CallInst *TheCall) {
  909. LLVMContext &Ctx = M->getContext();
  910. auto Int8PtrTy = Type::getInt8PtrTy(Ctx);
  911. auto NegativeOne = ConstantInt::getSigned(Type::getInt64Ty(Ctx), -1);
  912. Instruction *Term = TheCall->getParent()->getTerminator();
  913. // The memory argument to a lifetime marker must be a i8*. Cache any bitcasts
  914. // needed to satisfy this requirement so they may be reused.
  915. DenseMap<Value *, Value *> Bitcasts;
  916. // Emit lifetime markers for the pointers given in \p Objects. Insert the
  917. // markers before the call if \p InsertBefore, and after the call otherwise.
  918. auto insertMarkers = [&](Function *MarkerFunc, ArrayRef<Value *> Objects,
  919. bool InsertBefore) {
  920. for (Value *Mem : Objects) {
  921. assert((!isa<Instruction>(Mem) || cast<Instruction>(Mem)->getFunction() ==
  922. TheCall->getFunction()) &&
  923. "Input memory not defined in original function");
  924. Value *&MemAsI8Ptr = Bitcasts[Mem];
  925. if (!MemAsI8Ptr) {
  926. if (Mem->getType() == Int8PtrTy)
  927. MemAsI8Ptr = Mem;
  928. else
  929. MemAsI8Ptr =
  930. CastInst::CreatePointerCast(Mem, Int8PtrTy, "lt.cast", TheCall);
  931. }
  932. auto Marker = CallInst::Create(MarkerFunc, {NegativeOne, MemAsI8Ptr});
  933. if (InsertBefore)
  934. Marker->insertBefore(TheCall);
  935. else
  936. Marker->insertBefore(Term);
  937. }
  938. };
  939. if (!LifetimesStart.empty()) {
  940. auto StartFn = llvm::Intrinsic::getDeclaration(
  941. M, llvm::Intrinsic::lifetime_start, Int8PtrTy);
  942. insertMarkers(StartFn, LifetimesStart, /*InsertBefore=*/true);
  943. }
  944. if (!LifetimesEnd.empty()) {
  945. auto EndFn = llvm::Intrinsic::getDeclaration(
  946. M, llvm::Intrinsic::lifetime_end, Int8PtrTy);
  947. insertMarkers(EndFn, LifetimesEnd, /*InsertBefore=*/false);
  948. }
  949. }
  950. /// emitCallAndSwitchStatement - This method sets up the caller side by adding
  951. /// the call instruction, splitting any PHI nodes in the header block as
  952. /// necessary.
  953. CallInst *CodeExtractor::emitCallAndSwitchStatement(Function *newFunction,
  954. BasicBlock *codeReplacer,
  955. ValueSet &inputs,
  956. ValueSet &outputs) {
  957. // Emit a call to the new function, passing in: *pointer to struct (if
  958. // aggregating parameters), or plan inputs and allocated memory for outputs
  959. std::vector<Value *> params, StructValues, ReloadOutputs, Reloads;
  960. Module *M = newFunction->getParent();
  961. LLVMContext &Context = M->getContext();
  962. const DataLayout &DL = M->getDataLayout();
  963. CallInst *call = nullptr;
  964. // Add inputs as params, or to be filled into the struct
  965. unsigned ArgNo = 0;
  966. SmallVector<unsigned, 1> SwiftErrorArgs;
  967. for (Value *input : inputs) {
  968. if (AggregateArgs)
  969. StructValues.push_back(input);
  970. else {
  971. params.push_back(input);
  972. if (input->isSwiftError())
  973. SwiftErrorArgs.push_back(ArgNo);
  974. }
  975. ++ArgNo;
  976. }
  977. // Create allocas for the outputs
  978. for (Value *output : outputs) {
  979. if (AggregateArgs) {
  980. StructValues.push_back(output);
  981. } else {
  982. AllocaInst *alloca =
  983. new AllocaInst(output->getType(), DL.getAllocaAddrSpace(),
  984. nullptr, output->getName() + ".loc",
  985. &codeReplacer->getParent()->front().front());
  986. ReloadOutputs.push_back(alloca);
  987. params.push_back(alloca);
  988. }
  989. }
  990. StructType *StructArgTy = nullptr;
  991. AllocaInst *Struct = nullptr;
  992. if (AggregateArgs && (inputs.size() + outputs.size() > 0)) {
  993. std::vector<Type *> ArgTypes;
  994. for (ValueSet::iterator v = StructValues.begin(),
  995. ve = StructValues.end(); v != ve; ++v)
  996. ArgTypes.push_back((*v)->getType());
  997. // Allocate a struct at the beginning of this function
  998. StructArgTy = StructType::get(newFunction->getContext(), ArgTypes);
  999. Struct = new AllocaInst(StructArgTy, DL.getAllocaAddrSpace(), nullptr,
  1000. "structArg",
  1001. &codeReplacer->getParent()->front().front());
  1002. params.push_back(Struct);
  1003. for (unsigned i = 0, e = inputs.size(); i != e; ++i) {
  1004. Value *Idx[2];
  1005. Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
  1006. Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), i);
  1007. GetElementPtrInst *GEP = GetElementPtrInst::Create(
  1008. StructArgTy, Struct, Idx, "gep_" + StructValues[i]->getName());
  1009. codeReplacer->getInstList().push_back(GEP);
  1010. StoreInst *SI = new StoreInst(StructValues[i], GEP);
  1011. codeReplacer->getInstList().push_back(SI);
  1012. }
  1013. }
  1014. // Emit the call to the function
  1015. call = CallInst::Create(newFunction, params,
  1016. NumExitBlocks > 1 ? "targetBlock" : "");
  1017. // Add debug location to the new call, if the original function has debug
  1018. // info. In that case, the terminator of the entry block of the extracted
  1019. // function contains the first debug location of the extracted function,
  1020. // set in extractCodeRegion.
  1021. if (codeReplacer->getParent()->getSubprogram()) {
  1022. if (auto DL = newFunction->getEntryBlock().getTerminator()->getDebugLoc())
  1023. call->setDebugLoc(DL);
  1024. }
  1025. codeReplacer->getInstList().push_back(call);
  1026. // Set swifterror parameter attributes.
  1027. for (unsigned SwiftErrArgNo : SwiftErrorArgs) {
  1028. call->addParamAttr(SwiftErrArgNo, Attribute::SwiftError);
  1029. newFunction->addParamAttr(SwiftErrArgNo, Attribute::SwiftError);
  1030. }
  1031. Function::arg_iterator OutputArgBegin = newFunction->arg_begin();
  1032. unsigned FirstOut = inputs.size();
  1033. if (!AggregateArgs)
  1034. std::advance(OutputArgBegin, inputs.size());
  1035. // Reload the outputs passed in by reference.
  1036. for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
  1037. Value *Output = nullptr;
  1038. if (AggregateArgs) {
  1039. Value *Idx[2];
  1040. Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
  1041. Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
  1042. GetElementPtrInst *GEP = GetElementPtrInst::Create(
  1043. StructArgTy, Struct, Idx, "gep_reload_" + outputs[i]->getName());
  1044. codeReplacer->getInstList().push_back(GEP);
  1045. Output = GEP;
  1046. } else {
  1047. Output = ReloadOutputs[i];
  1048. }
  1049. LoadInst *load = new LoadInst(outputs[i]->getType(), Output,
  1050. outputs[i]->getName() + ".reload");
  1051. Reloads.push_back(load);
  1052. codeReplacer->getInstList().push_back(load);
  1053. std::vector<User *> Users(outputs[i]->user_begin(), outputs[i]->user_end());
  1054. for (unsigned u = 0, e = Users.size(); u != e; ++u) {
  1055. Instruction *inst = cast<Instruction>(Users[u]);
  1056. if (!Blocks.count(inst->getParent()))
  1057. inst->replaceUsesOfWith(outputs[i], load);
  1058. }
  1059. }
  1060. // Now we can emit a switch statement using the call as a value.
  1061. SwitchInst *TheSwitch =
  1062. SwitchInst::Create(Constant::getNullValue(Type::getInt16Ty(Context)),
  1063. codeReplacer, 0, codeReplacer);
  1064. // Since there may be multiple exits from the original region, make the new
  1065. // function return an unsigned, switch on that number. This loop iterates
  1066. // over all of the blocks in the extracted region, updating any terminator
  1067. // instructions in the to-be-extracted region that branch to blocks that are
  1068. // not in the region to be extracted.
  1069. std::map<BasicBlock *, BasicBlock *> ExitBlockMap;
  1070. unsigned switchVal = 0;
  1071. for (BasicBlock *Block : Blocks) {
  1072. Instruction *TI = Block->getTerminator();
  1073. for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
  1074. if (!Blocks.count(TI->getSuccessor(i))) {
  1075. BasicBlock *OldTarget = TI->getSuccessor(i);
  1076. // add a new basic block which returns the appropriate value
  1077. BasicBlock *&NewTarget = ExitBlockMap[OldTarget];
  1078. if (!NewTarget) {
  1079. // If we don't already have an exit stub for this non-extracted
  1080. // destination, create one now!
  1081. NewTarget = BasicBlock::Create(Context,
  1082. OldTarget->getName() + ".exitStub",
  1083. newFunction);
  1084. unsigned SuccNum = switchVal++;
  1085. Value *brVal = nullptr;
  1086. switch (NumExitBlocks) {
  1087. case 0:
  1088. case 1: break; // No value needed.
  1089. case 2: // Conditional branch, return a bool
  1090. brVal = ConstantInt::get(Type::getInt1Ty(Context), !SuccNum);
  1091. break;
  1092. default:
  1093. brVal = ConstantInt::get(Type::getInt16Ty(Context), SuccNum);
  1094. break;
  1095. }
  1096. ReturnInst::Create(Context, brVal, NewTarget);
  1097. // Update the switch instruction.
  1098. TheSwitch->addCase(ConstantInt::get(Type::getInt16Ty(Context),
  1099. SuccNum),
  1100. OldTarget);
  1101. }
  1102. // rewrite the original branch instruction with this new target
  1103. TI->setSuccessor(i, NewTarget);
  1104. }
  1105. }
  1106. // Store the arguments right after the definition of output value.
  1107. // This should be proceeded after creating exit stubs to be ensure that invoke
  1108. // result restore will be placed in the outlined function.
  1109. Function::arg_iterator OAI = OutputArgBegin;
  1110. for (unsigned i = 0, e = outputs.size(); i != e; ++i) {
  1111. auto *OutI = dyn_cast<Instruction>(outputs[i]);
  1112. if (!OutI)
  1113. continue;
  1114. // Find proper insertion point.
  1115. BasicBlock::iterator InsertPt;
  1116. // In case OutI is an invoke, we insert the store at the beginning in the
  1117. // 'normal destination' BB. Otherwise we insert the store right after OutI.
  1118. if (auto *InvokeI = dyn_cast<InvokeInst>(OutI))
  1119. InsertPt = InvokeI->getNormalDest()->getFirstInsertionPt();
  1120. else if (auto *Phi = dyn_cast<PHINode>(OutI))
  1121. InsertPt = Phi->getParent()->getFirstInsertionPt();
  1122. else
  1123. InsertPt = std::next(OutI->getIterator());
  1124. Instruction *InsertBefore = &*InsertPt;
  1125. assert((InsertBefore->getFunction() == newFunction ||
  1126. Blocks.count(InsertBefore->getParent())) &&
  1127. "InsertPt should be in new function");
  1128. assert(OAI != newFunction->arg_end() &&
  1129. "Number of output arguments should match "
  1130. "the amount of defined values");
  1131. if (AggregateArgs) {
  1132. Value *Idx[2];
  1133. Idx[0] = Constant::getNullValue(Type::getInt32Ty(Context));
  1134. Idx[1] = ConstantInt::get(Type::getInt32Ty(Context), FirstOut + i);
  1135. GetElementPtrInst *GEP = GetElementPtrInst::Create(
  1136. StructArgTy, &*OAI, Idx, "gep_" + outputs[i]->getName(),
  1137. InsertBefore);
  1138. new StoreInst(outputs[i], GEP, InsertBefore);
  1139. // Since there should be only one struct argument aggregating
  1140. // all the output values, we shouldn't increment OAI, which always
  1141. // points to the struct argument, in this case.
  1142. } else {
  1143. new StoreInst(outputs[i], &*OAI, InsertBefore);
  1144. ++OAI;
  1145. }
  1146. }
  1147. // Now that we've done the deed, simplify the switch instruction.
  1148. Type *OldFnRetTy = TheSwitch->getParent()->getParent()->getReturnType();
  1149. switch (NumExitBlocks) {
  1150. case 0:
  1151. // There are no successors (the block containing the switch itself), which
  1152. // means that previously this was the last part of the function, and hence
  1153. // this should be rewritten as a `ret'
  1154. // Check if the function should return a value
  1155. if (OldFnRetTy->isVoidTy()) {
  1156. ReturnInst::Create(Context, nullptr, TheSwitch); // Return void
  1157. } else if (OldFnRetTy == TheSwitch->getCondition()->getType()) {
  1158. // return what we have
  1159. ReturnInst::Create(Context, TheSwitch->getCondition(), TheSwitch);
  1160. } else {
  1161. // Otherwise we must have code extracted an unwind or something, just
  1162. // return whatever we want.
  1163. ReturnInst::Create(Context,
  1164. Constant::getNullValue(OldFnRetTy), TheSwitch);
  1165. }
  1166. TheSwitch->eraseFromParent();
  1167. break;
  1168. case 1:
  1169. // Only a single destination, change the switch into an unconditional
  1170. // branch.
  1171. BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch);
  1172. TheSwitch->eraseFromParent();
  1173. break;
  1174. case 2:
  1175. BranchInst::Create(TheSwitch->getSuccessor(1), TheSwitch->getSuccessor(2),
  1176. call, TheSwitch);
  1177. TheSwitch->eraseFromParent();
  1178. break;
  1179. default:
  1180. // Otherwise, make the default destination of the switch instruction be one
  1181. // of the other successors.
  1182. TheSwitch->setCondition(call);
  1183. TheSwitch->setDefaultDest(TheSwitch->getSuccessor(NumExitBlocks));
  1184. // Remove redundant case
  1185. TheSwitch->removeCase(SwitchInst::CaseIt(TheSwitch, NumExitBlocks-1));
  1186. break;
  1187. }
  1188. // Insert lifetime markers around the reloads of any output values. The
  1189. // allocas output values are stored in are only in-use in the codeRepl block.
  1190. insertLifetimeMarkersSurroundingCall(M, ReloadOutputs, ReloadOutputs, call);
  1191. return call;
  1192. }
  1193. void CodeExtractor::moveCodeToFunction(Function *newFunction) {
  1194. Function *oldFunc = (*Blocks.begin())->getParent();
  1195. Function::BasicBlockListType &oldBlocks = oldFunc->getBasicBlockList();
  1196. Function::BasicBlockListType &newBlocks = newFunction->getBasicBlockList();
  1197. for (BasicBlock *Block : Blocks) {
  1198. // Delete the basic block from the old function, and the list of blocks
  1199. oldBlocks.remove(Block);
  1200. // Insert this basic block into the new function
  1201. newBlocks.push_back(Block);
  1202. }
  1203. }
  1204. void CodeExtractor::calculateNewCallTerminatorWeights(
  1205. BasicBlock *CodeReplacer,
  1206. DenseMap<BasicBlock *, BlockFrequency> &ExitWeights,
  1207. BranchProbabilityInfo *BPI) {
  1208. using Distribution = BlockFrequencyInfoImplBase::Distribution;
  1209. using BlockNode = BlockFrequencyInfoImplBase::BlockNode;
  1210. // Update the branch weights for the exit block.
  1211. Instruction *TI = CodeReplacer->getTerminator();
  1212. SmallVector<unsigned, 8> BranchWeights(TI->getNumSuccessors(), 0);
  1213. // Block Frequency distribution with dummy node.
  1214. Distribution BranchDist;
  1215. // Add each of the frequencies of the successors.
  1216. for (unsigned i = 0, e = TI->getNumSuccessors(); i < e; ++i) {
  1217. BlockNode ExitNode(i);
  1218. uint64_t ExitFreq = ExitWeights[TI->getSuccessor(i)].getFrequency();
  1219. if (ExitFreq != 0)
  1220. BranchDist.addExit(ExitNode, ExitFreq);
  1221. else
  1222. BPI->setEdgeProbability(CodeReplacer, i, BranchProbability::getZero());
  1223. }
  1224. // Check for no total weight.
  1225. if (BranchDist.Total == 0)
  1226. return;
  1227. // Normalize the distribution so that they can fit in unsigned.
  1228. BranchDist.normalize();
  1229. // Create normalized branch weights and set the metadata.
  1230. for (unsigned I = 0, E = BranchDist.Weights.size(); I < E; ++I) {
  1231. const auto &Weight = BranchDist.Weights[I];
  1232. // Get the weight and update the current BFI.
  1233. BranchWeights[Weight.TargetNode.Index] = Weight.Amount;
  1234. BranchProbability BP(Weight.Amount, BranchDist.Total);
  1235. BPI->setEdgeProbability(CodeReplacer, Weight.TargetNode.Index, BP);
  1236. }
  1237. TI->setMetadata(
  1238. LLVMContext::MD_prof,
  1239. MDBuilder(TI->getContext()).createBranchWeights(BranchWeights));
  1240. }
  1241. Function *
  1242. CodeExtractor::extractCodeRegion(const CodeExtractorAnalysisCache &CEAC) {
  1243. if (!isEligible())
  1244. return nullptr;
  1245. // Assumption: this is a single-entry code region, and the header is the first
  1246. // block in the region.
  1247. BasicBlock *header = *Blocks.begin();
  1248. Function *oldFunction = header->getParent();
  1249. // Calculate the entry frequency of the new function before we change the root
  1250. // block.
  1251. BlockFrequency EntryFreq;
  1252. if (BFI) {
  1253. assert(BPI && "Both BPI and BFI are required to preserve profile info");
  1254. for (BasicBlock *Pred : predecessors(header)) {
  1255. if (Blocks.count(Pred))
  1256. continue;
  1257. EntryFreq +=
  1258. BFI->getBlockFreq(Pred) * BPI->getEdgeProbability(Pred, header);
  1259. }
  1260. }
  1261. if (AC) {
  1262. // Remove @llvm.assume calls that were moved to the new function from the
  1263. // old function's assumption cache.
  1264. for (BasicBlock *Block : Blocks)
  1265. for (auto &I : *Block)
  1266. if (match(&I, m_Intrinsic<Intrinsic::assume>()))
  1267. AC->unregisterAssumption(cast<CallInst>(&I));
  1268. }
  1269. // If we have any return instructions in the region, split those blocks so
  1270. // that the return is not in the region.
  1271. splitReturnBlocks();
  1272. // Calculate the exit blocks for the extracted region and the total exit
  1273. // weights for each of those blocks.
  1274. DenseMap<BasicBlock *, BlockFrequency> ExitWeights;
  1275. SmallPtrSet<BasicBlock *, 1> ExitBlocks;
  1276. for (BasicBlock *Block : Blocks) {
  1277. for (succ_iterator SI = succ_begin(Block), SE = succ_end(Block); SI != SE;
  1278. ++SI) {
  1279. if (!Blocks.count(*SI)) {
  1280. // Update the branch weight for this successor.
  1281. if (BFI) {
  1282. BlockFrequency &BF = ExitWeights[*SI];
  1283. BF += BFI->getBlockFreq(Block) * BPI->getEdgeProbability(Block, *SI);
  1284. }
  1285. ExitBlocks.insert(*SI);
  1286. }
  1287. }
  1288. }
  1289. NumExitBlocks = ExitBlocks.size();
  1290. // If we have to split PHI nodes of the entry or exit blocks, do so now.
  1291. severSplitPHINodesOfEntry(header);
  1292. severSplitPHINodesOfExits(ExitBlocks);
  1293. // This takes place of the original loop
  1294. BasicBlock *codeReplacer = BasicBlock::Create(header->getContext(),
  1295. "codeRepl", oldFunction,
  1296. header);
  1297. // The new function needs a root node because other nodes can branch to the
  1298. // head of the region, but the entry node of a function cannot have preds.
  1299. BasicBlock *newFuncRoot = BasicBlock::Create(header->getContext(),
  1300. "newFuncRoot");
  1301. auto *BranchI = BranchInst::Create(header);
  1302. // If the original function has debug info, we have to add a debug location
  1303. // to the new branch instruction from the artificial entry block.
  1304. // We use the debug location of the first instruction in the extracted
  1305. // blocks, as there is no other equivalent line in the source code.
  1306. if (oldFunction->getSubprogram()) {
  1307. any_of(Blocks, [&BranchI](const BasicBlock *BB) {
  1308. return any_of(*BB, [&BranchI](const Instruction &I) {
  1309. if (!I.getDebugLoc())
  1310. return false;
  1311. BranchI->setDebugLoc(I.getDebugLoc());
  1312. return true;
  1313. });
  1314. });
  1315. }
  1316. newFuncRoot->getInstList().push_back(BranchI);
  1317. ValueSet inputs, outputs, SinkingCands, HoistingCands;
  1318. BasicBlock *CommonExit = nullptr;
  1319. findAllocas(CEAC, SinkingCands, HoistingCands, CommonExit);
  1320. assert(HoistingCands.empty() || CommonExit);
  1321. // Find inputs to, outputs from the code region.
  1322. findInputsOutputs(inputs, outputs, SinkingCands);
  1323. // Now sink all instructions which only have non-phi uses inside the region.
  1324. // Group the allocas at the start of the block, so that any bitcast uses of
  1325. // the allocas are well-defined.
  1326. AllocaInst *FirstSunkAlloca = nullptr;
  1327. for (auto *II : SinkingCands) {
  1328. if (auto *AI = dyn_cast<AllocaInst>(II)) {
  1329. AI->moveBefore(*newFuncRoot, newFuncRoot->getFirstInsertionPt());
  1330. if (!FirstSunkAlloca)
  1331. FirstSunkAlloca = AI;
  1332. }
  1333. }
  1334. assert((SinkingCands.empty() || FirstSunkAlloca) &&
  1335. "Did not expect a sink candidate without any allocas");
  1336. for (auto *II : SinkingCands) {
  1337. if (!isa<AllocaInst>(II)) {
  1338. cast<Instruction>(II)->moveAfter(FirstSunkAlloca);
  1339. }
  1340. }
  1341. if (!HoistingCands.empty()) {
  1342. auto *HoistToBlock = findOrCreateBlockForHoisting(CommonExit);
  1343. Instruction *TI = HoistToBlock->getTerminator();
  1344. for (auto *II : HoistingCands)
  1345. cast<Instruction>(II)->moveBefore(TI);
  1346. }
  1347. // Collect objects which are inputs to the extraction region and also
  1348. // referenced by lifetime start markers within it. The effects of these
  1349. // markers must be replicated in the calling function to prevent the stack
  1350. // coloring pass from merging slots which store input objects.
  1351. ValueSet LifetimesStart;
  1352. eraseLifetimeMarkersOnInputs(Blocks, SinkingCands, LifetimesStart);
  1353. // Construct new function based on inputs/outputs & add allocas for all defs.
  1354. Function *newFunction =
  1355. constructFunction(inputs, outputs, header, newFuncRoot, codeReplacer,
  1356. oldFunction, oldFunction->getParent());
  1357. // Update the entry count of the function.
  1358. if (BFI) {
  1359. auto Count = BFI->getProfileCountFromFreq(EntryFreq.getFrequency());
  1360. if (Count.hasValue())
  1361. newFunction->setEntryCount(
  1362. ProfileCount(Count.getValue(), Function::PCT_Real)); // FIXME
  1363. BFI->setBlockFreq(codeReplacer, EntryFreq.getFrequency());
  1364. }
  1365. CallInst *TheCall =
  1366. emitCallAndSwitchStatement(newFunction, codeReplacer, inputs, outputs);
  1367. moveCodeToFunction(newFunction);
  1368. // Replicate the effects of any lifetime start/end markers which referenced
  1369. // input objects in the extraction region by placing markers around the call.
  1370. insertLifetimeMarkersSurroundingCall(
  1371. oldFunction->getParent(), LifetimesStart.getArrayRef(), {}, TheCall);
  1372. // Propagate personality info to the new function if there is one.
  1373. if (oldFunction->hasPersonalityFn())
  1374. newFunction->setPersonalityFn(oldFunction->getPersonalityFn());
  1375. // Update the branch weights for the exit block.
  1376. if (BFI && NumExitBlocks > 1)
  1377. calculateNewCallTerminatorWeights(codeReplacer, ExitWeights, BPI);
  1378. // Loop over all of the PHI nodes in the header and exit blocks, and change
  1379. // any references to the old incoming edge to be the new incoming edge.
  1380. for (BasicBlock::iterator I = header->begin(); isa<PHINode>(I); ++I) {
  1381. PHINode *PN = cast<PHINode>(I);
  1382. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i)
  1383. if (!Blocks.count(PN->getIncomingBlock(i)))
  1384. PN->setIncomingBlock(i, newFuncRoot);
  1385. }
  1386. for (BasicBlock *ExitBB : ExitBlocks)
  1387. for (PHINode &PN : ExitBB->phis()) {
  1388. Value *IncomingCodeReplacerVal = nullptr;
  1389. for (unsigned i = 0, e = PN.getNumIncomingValues(); i != e; ++i) {
  1390. // Ignore incoming values from outside of the extracted region.
  1391. if (!Blocks.count(PN.getIncomingBlock(i)))
  1392. continue;
  1393. // Ensure that there is only one incoming value from codeReplacer.
  1394. if (!IncomingCodeReplacerVal) {
  1395. PN.setIncomingBlock(i, codeReplacer);
  1396. IncomingCodeReplacerVal = PN.getIncomingValue(i);
  1397. } else
  1398. assert(IncomingCodeReplacerVal == PN.getIncomingValue(i) &&
  1399. "PHI has two incompatbile incoming values from codeRepl");
  1400. }
  1401. }
  1402. // Erase debug info intrinsics. Variable updates within the new function are
  1403. // invisible to debuggers. This could be improved by defining a DISubprogram
  1404. // for the new function.
  1405. for (BasicBlock &BB : *newFunction) {
  1406. auto BlockIt = BB.begin();
  1407. // Remove debug info intrinsics from the new function.
  1408. while (BlockIt != BB.end()) {
  1409. Instruction *Inst = &*BlockIt;
  1410. ++BlockIt;
  1411. if (isa<DbgInfoIntrinsic>(Inst))
  1412. Inst->eraseFromParent();
  1413. }
  1414. // Remove debug info intrinsics which refer to values in the new function
  1415. // from the old function.
  1416. SmallVector<DbgVariableIntrinsic *, 4> DbgUsers;
  1417. for (Instruction &I : BB)
  1418. findDbgUsers(DbgUsers, &I);
  1419. for (DbgVariableIntrinsic *DVI : DbgUsers)
  1420. DVI->eraseFromParent();
  1421. }
  1422. // Mark the new function `noreturn` if applicable. Terminators which resume
  1423. // exception propagation are treated as returning instructions. This is to
  1424. // avoid inserting traps after calls to outlined functions which unwind.
  1425. bool doesNotReturn = none_of(*newFunction, [](const BasicBlock &BB) {
  1426. const Instruction *Term = BB.getTerminator();
  1427. return isa<ReturnInst>(Term) || isa<ResumeInst>(Term);
  1428. });
  1429. if (doesNotReturn)
  1430. newFunction->setDoesNotReturn();
  1431. LLVM_DEBUG(if (verifyFunction(*newFunction, &errs())) {
  1432. newFunction->dump();
  1433. report_fatal_error("verification of newFunction failed!");
  1434. });
  1435. LLVM_DEBUG(if (verifyFunction(*oldFunction))
  1436. report_fatal_error("verification of oldFunction failed!"));
  1437. LLVM_DEBUG(if (AC && verifyAssumptionCache(*oldFunction, AC))
  1438. report_fatal_error("Stale Asumption cache for old Function!"));
  1439. return newFunction;
  1440. }
  1441. bool CodeExtractor::verifyAssumptionCache(const Function& F,
  1442. AssumptionCache *AC) {
  1443. for (auto AssumeVH : AC->assumptions()) {
  1444. CallInst *I = cast<CallInst>(AssumeVH);
  1445. if (I->getFunction() != &F)
  1446. return true;
  1447. }
  1448. return false;
  1449. }