BasicBlockUtils.cpp 70 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819
  1. //===- BasicBlockUtils.cpp - BasicBlock Utilities --------------------------==//
  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 family of functions perform manipulations on basic blocks, and
  10. // instructions contained within basic blocks.
  11. //
  12. //===----------------------------------------------------------------------===//
  13. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  14. #include "llvm/ADT/ArrayRef.h"
  15. #include "llvm/ADT/SmallPtrSet.h"
  16. #include "llvm/ADT/SmallVector.h"
  17. #include "llvm/ADT/Twine.h"
  18. #include "llvm/Analysis/CFG.h"
  19. #include "llvm/Analysis/DomTreeUpdater.h"
  20. #include "llvm/Analysis/LoopInfo.h"
  21. #include "llvm/Analysis/MemoryDependenceAnalysis.h"
  22. #include "llvm/Analysis/MemorySSAUpdater.h"
  23. #include "llvm/Analysis/PostDominators.h"
  24. #include "llvm/IR/BasicBlock.h"
  25. #include "llvm/IR/CFG.h"
  26. #include "llvm/IR/Constants.h"
  27. #include "llvm/IR/DebugInfoMetadata.h"
  28. #include "llvm/IR/Dominators.h"
  29. #include "llvm/IR/Function.h"
  30. #include "llvm/IR/InstrTypes.h"
  31. #include "llvm/IR/Instruction.h"
  32. #include "llvm/IR/Instructions.h"
  33. #include "llvm/IR/IntrinsicInst.h"
  34. #include "llvm/IR/LLVMContext.h"
  35. #include "llvm/IR/PseudoProbe.h"
  36. #include "llvm/IR/Type.h"
  37. #include "llvm/IR/User.h"
  38. #include "llvm/IR/Value.h"
  39. #include "llvm/IR/ValueHandle.h"
  40. #include "llvm/Support/Casting.h"
  41. #include "llvm/Support/CommandLine.h"
  42. #include "llvm/Support/Debug.h"
  43. #include "llvm/Support/raw_ostream.h"
  44. #include "llvm/Transforms/Utils/Local.h"
  45. #include <cassert>
  46. #include <cstdint>
  47. #include <string>
  48. #include <utility>
  49. #include <vector>
  50. using namespace llvm;
  51. #define DEBUG_TYPE "basicblock-utils"
  52. static cl::opt<unsigned> MaxDeoptOrUnreachableSuccessorCheckDepth(
  53. "max-deopt-or-unreachable-succ-check-depth", cl::init(8), cl::Hidden,
  54. cl::desc("Set the maximum path length when checking whether a basic block "
  55. "is followed by a block that either has a terminating "
  56. "deoptimizing call or is terminated with an unreachable"));
  57. void llvm::detachDeadBlocks(
  58. ArrayRef<BasicBlock *> BBs,
  59. SmallVectorImpl<DominatorTree::UpdateType> *Updates,
  60. bool KeepOneInputPHIs) {
  61. for (auto *BB : BBs) {
  62. // Loop through all of our successors and make sure they know that one
  63. // of their predecessors is going away.
  64. SmallPtrSet<BasicBlock *, 4> UniqueSuccessors;
  65. for (BasicBlock *Succ : successors(BB)) {
  66. Succ->removePredecessor(BB, KeepOneInputPHIs);
  67. if (Updates && UniqueSuccessors.insert(Succ).second)
  68. Updates->push_back({DominatorTree::Delete, BB, Succ});
  69. }
  70. // Zap all the instructions in the block.
  71. while (!BB->empty()) {
  72. Instruction &I = BB->back();
  73. // If this instruction is used, replace uses with an arbitrary value.
  74. // Because control flow can't get here, we don't care what we replace the
  75. // value with. Note that since this block is unreachable, and all values
  76. // contained within it must dominate their uses, that all uses will
  77. // eventually be removed (they are themselves dead).
  78. if (!I.use_empty())
  79. I.replaceAllUsesWith(UndefValue::get(I.getType()));
  80. BB->getInstList().pop_back();
  81. }
  82. new UnreachableInst(BB->getContext(), BB);
  83. assert(BB->getInstList().size() == 1 &&
  84. isa<UnreachableInst>(BB->getTerminator()) &&
  85. "The successor list of BB isn't empty before "
  86. "applying corresponding DTU updates.");
  87. }
  88. }
  89. void llvm::DeleteDeadBlock(BasicBlock *BB, DomTreeUpdater *DTU,
  90. bool KeepOneInputPHIs) {
  91. DeleteDeadBlocks({BB}, DTU, KeepOneInputPHIs);
  92. }
  93. void llvm::DeleteDeadBlocks(ArrayRef <BasicBlock *> BBs, DomTreeUpdater *DTU,
  94. bool KeepOneInputPHIs) {
  95. #ifndef NDEBUG
  96. // Make sure that all predecessors of each dead block is also dead.
  97. SmallPtrSet<BasicBlock *, 4> Dead(BBs.begin(), BBs.end());
  98. assert(Dead.size() == BBs.size() && "Duplicating blocks?");
  99. for (auto *BB : Dead)
  100. for (BasicBlock *Pred : predecessors(BB))
  101. assert(Dead.count(Pred) && "All predecessors must be dead!");
  102. #endif
  103. SmallVector<DominatorTree::UpdateType, 4> Updates;
  104. detachDeadBlocks(BBs, DTU ? &Updates : nullptr, KeepOneInputPHIs);
  105. if (DTU)
  106. DTU->applyUpdates(Updates);
  107. for (BasicBlock *BB : BBs)
  108. if (DTU)
  109. DTU->deleteBB(BB);
  110. else
  111. BB->eraseFromParent();
  112. }
  113. bool llvm::EliminateUnreachableBlocks(Function &F, DomTreeUpdater *DTU,
  114. bool KeepOneInputPHIs) {
  115. df_iterator_default_set<BasicBlock*> Reachable;
  116. // Mark all reachable blocks.
  117. for (BasicBlock *BB : depth_first_ext(&F, Reachable))
  118. (void)BB/* Mark all reachable blocks */;
  119. // Collect all dead blocks.
  120. std::vector<BasicBlock*> DeadBlocks;
  121. for (BasicBlock &BB : F)
  122. if (!Reachable.count(&BB))
  123. DeadBlocks.push_back(&BB);
  124. // Delete the dead blocks.
  125. DeleteDeadBlocks(DeadBlocks, DTU, KeepOneInputPHIs);
  126. return !DeadBlocks.empty();
  127. }
  128. bool llvm::FoldSingleEntryPHINodes(BasicBlock *BB,
  129. MemoryDependenceResults *MemDep) {
  130. if (!isa<PHINode>(BB->begin()))
  131. return false;
  132. while (PHINode *PN = dyn_cast<PHINode>(BB->begin())) {
  133. if (PN->getIncomingValue(0) != PN)
  134. PN->replaceAllUsesWith(PN->getIncomingValue(0));
  135. else
  136. PN->replaceAllUsesWith(UndefValue::get(PN->getType()));
  137. if (MemDep)
  138. MemDep->removeInstruction(PN); // Memdep updates AA itself.
  139. PN->eraseFromParent();
  140. }
  141. return true;
  142. }
  143. bool llvm::DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI,
  144. MemorySSAUpdater *MSSAU) {
  145. // Recursively deleting a PHI may cause multiple PHIs to be deleted
  146. // or RAUW'd undef, so use an array of WeakTrackingVH for the PHIs to delete.
  147. SmallVector<WeakTrackingVH, 8> PHIs;
  148. for (PHINode &PN : BB->phis())
  149. PHIs.push_back(&PN);
  150. bool Changed = false;
  151. for (unsigned i = 0, e = PHIs.size(); i != e; ++i)
  152. if (PHINode *PN = dyn_cast_or_null<PHINode>(PHIs[i].operator Value*()))
  153. Changed |= RecursivelyDeleteDeadPHINode(PN, TLI, MSSAU);
  154. return Changed;
  155. }
  156. bool llvm::MergeBlockIntoPredecessor(BasicBlock *BB, DomTreeUpdater *DTU,
  157. LoopInfo *LI, MemorySSAUpdater *MSSAU,
  158. MemoryDependenceResults *MemDep,
  159. bool PredecessorWithTwoSuccessors) {
  160. if (BB->hasAddressTaken())
  161. return false;
  162. // Can't merge if there are multiple predecessors, or no predecessors.
  163. BasicBlock *PredBB = BB->getUniquePredecessor();
  164. if (!PredBB) return false;
  165. // Don't break self-loops.
  166. if (PredBB == BB) return false;
  167. // Don't break unwinding instructions.
  168. if (PredBB->getTerminator()->isExceptionalTerminator())
  169. return false;
  170. // Can't merge if there are multiple distinct successors.
  171. if (!PredecessorWithTwoSuccessors && PredBB->getUniqueSuccessor() != BB)
  172. return false;
  173. // Currently only allow PredBB to have two predecessors, one being BB.
  174. // Update BI to branch to BB's only successor instead of BB.
  175. BranchInst *PredBB_BI;
  176. BasicBlock *NewSucc = nullptr;
  177. unsigned FallThruPath;
  178. if (PredecessorWithTwoSuccessors) {
  179. if (!(PredBB_BI = dyn_cast<BranchInst>(PredBB->getTerminator())))
  180. return false;
  181. BranchInst *BB_JmpI = dyn_cast<BranchInst>(BB->getTerminator());
  182. if (!BB_JmpI || !BB_JmpI->isUnconditional())
  183. return false;
  184. NewSucc = BB_JmpI->getSuccessor(0);
  185. FallThruPath = PredBB_BI->getSuccessor(0) == BB ? 0 : 1;
  186. }
  187. // Can't merge if there is PHI loop.
  188. for (PHINode &PN : BB->phis())
  189. if (llvm::is_contained(PN.incoming_values(), &PN))
  190. return false;
  191. LLVM_DEBUG(dbgs() << "Merging: " << BB->getName() << " into "
  192. << PredBB->getName() << "\n");
  193. // Begin by getting rid of unneeded PHIs.
  194. SmallVector<AssertingVH<Value>, 4> IncomingValues;
  195. if (isa<PHINode>(BB->front())) {
  196. for (PHINode &PN : BB->phis())
  197. if (!isa<PHINode>(PN.getIncomingValue(0)) ||
  198. cast<PHINode>(PN.getIncomingValue(0))->getParent() != BB)
  199. IncomingValues.push_back(PN.getIncomingValue(0));
  200. FoldSingleEntryPHINodes(BB, MemDep);
  201. }
  202. // DTU update: Collect all the edges that exit BB.
  203. // These dominator edges will be redirected from Pred.
  204. std::vector<DominatorTree::UpdateType> Updates;
  205. if (DTU) {
  206. // To avoid processing the same predecessor more than once.
  207. SmallPtrSet<BasicBlock *, 8> SeenSuccs;
  208. SmallPtrSet<BasicBlock *, 2> SuccsOfPredBB(succ_begin(PredBB),
  209. succ_end(PredBB));
  210. Updates.reserve(Updates.size() + 2 * succ_size(BB) + 1);
  211. // Add insert edges first. Experimentally, for the particular case of two
  212. // blocks that can be merged, with a single successor and single predecessor
  213. // respectively, it is beneficial to have all insert updates first. Deleting
  214. // edges first may lead to unreachable blocks, followed by inserting edges
  215. // making the blocks reachable again. Such DT updates lead to high compile
  216. // times. We add inserts before deletes here to reduce compile time.
  217. for (BasicBlock *SuccOfBB : successors(BB))
  218. // This successor of BB may already be a PredBB's successor.
  219. if (!SuccsOfPredBB.contains(SuccOfBB))
  220. if (SeenSuccs.insert(SuccOfBB).second)
  221. Updates.push_back({DominatorTree::Insert, PredBB, SuccOfBB});
  222. SeenSuccs.clear();
  223. for (BasicBlock *SuccOfBB : successors(BB))
  224. if (SeenSuccs.insert(SuccOfBB).second)
  225. Updates.push_back({DominatorTree::Delete, BB, SuccOfBB});
  226. Updates.push_back({DominatorTree::Delete, PredBB, BB});
  227. }
  228. Instruction *PTI = PredBB->getTerminator();
  229. Instruction *STI = BB->getTerminator();
  230. Instruction *Start = &*BB->begin();
  231. // If there's nothing to move, mark the starting instruction as the last
  232. // instruction in the block. Terminator instruction is handled separately.
  233. if (Start == STI)
  234. Start = PTI;
  235. // Move all definitions in the successor to the predecessor...
  236. PredBB->getInstList().splice(PTI->getIterator(), BB->getInstList(),
  237. BB->begin(), STI->getIterator());
  238. if (MSSAU)
  239. MSSAU->moveAllAfterMergeBlocks(BB, PredBB, Start);
  240. // Make all PHI nodes that referred to BB now refer to Pred as their
  241. // source...
  242. BB->replaceAllUsesWith(PredBB);
  243. if (PredecessorWithTwoSuccessors) {
  244. // Delete the unconditional branch from BB.
  245. BB->getInstList().pop_back();
  246. // Update branch in the predecessor.
  247. PredBB_BI->setSuccessor(FallThruPath, NewSucc);
  248. } else {
  249. // Delete the unconditional branch from the predecessor.
  250. PredBB->getInstList().pop_back();
  251. // Move terminator instruction.
  252. PredBB->getInstList().splice(PredBB->end(), BB->getInstList());
  253. // Terminator may be a memory accessing instruction too.
  254. if (MSSAU)
  255. if (MemoryUseOrDef *MUD = cast_or_null<MemoryUseOrDef>(
  256. MSSAU->getMemorySSA()->getMemoryAccess(PredBB->getTerminator())))
  257. MSSAU->moveToPlace(MUD, PredBB, MemorySSA::End);
  258. }
  259. // Add unreachable to now empty BB.
  260. new UnreachableInst(BB->getContext(), BB);
  261. // Inherit predecessors name if it exists.
  262. if (!PredBB->hasName())
  263. PredBB->takeName(BB);
  264. if (LI)
  265. LI->removeBlock(BB);
  266. if (MemDep)
  267. MemDep->invalidateCachedPredecessors();
  268. if (DTU)
  269. DTU->applyUpdates(Updates);
  270. // Finally, erase the old block and update dominator info.
  271. DeleteDeadBlock(BB, DTU);
  272. return true;
  273. }
  274. bool llvm::MergeBlockSuccessorsIntoGivenBlocks(
  275. SmallPtrSetImpl<BasicBlock *> &MergeBlocks, Loop *L, DomTreeUpdater *DTU,
  276. LoopInfo *LI) {
  277. assert(!MergeBlocks.empty() && "MergeBlocks should not be empty");
  278. bool BlocksHaveBeenMerged = false;
  279. while (!MergeBlocks.empty()) {
  280. BasicBlock *BB = *MergeBlocks.begin();
  281. BasicBlock *Dest = BB->getSingleSuccessor();
  282. if (Dest && (!L || L->contains(Dest))) {
  283. BasicBlock *Fold = Dest->getUniquePredecessor();
  284. (void)Fold;
  285. if (MergeBlockIntoPredecessor(Dest, DTU, LI)) {
  286. assert(Fold == BB &&
  287. "Expecting BB to be unique predecessor of the Dest block");
  288. MergeBlocks.erase(Dest);
  289. BlocksHaveBeenMerged = true;
  290. } else
  291. MergeBlocks.erase(BB);
  292. } else
  293. MergeBlocks.erase(BB);
  294. }
  295. return BlocksHaveBeenMerged;
  296. }
  297. /// Remove redundant instructions within sequences of consecutive dbg.value
  298. /// instructions. This is done using a backward scan to keep the last dbg.value
  299. /// describing a specific variable/fragment.
  300. ///
  301. /// BackwardScan strategy:
  302. /// ----------------------
  303. /// Given a sequence of consecutive DbgValueInst like this
  304. ///
  305. /// dbg.value ..., "x", FragmentX1 (*)
  306. /// dbg.value ..., "y", FragmentY1
  307. /// dbg.value ..., "x", FragmentX2
  308. /// dbg.value ..., "x", FragmentX1 (**)
  309. ///
  310. /// then the instruction marked with (*) can be removed (it is guaranteed to be
  311. /// obsoleted by the instruction marked with (**) as the latter instruction is
  312. /// describing the same variable using the same fragment info).
  313. ///
  314. /// Possible improvements:
  315. /// - Check fully overlapping fragments and not only identical fragments.
  316. /// - Support dbg.addr, dbg.declare. dbg.label, and possibly other meta
  317. /// instructions being part of the sequence of consecutive instructions.
  318. static bool removeRedundantDbgInstrsUsingBackwardScan(BasicBlock *BB) {
  319. SmallVector<DbgValueInst *, 8> ToBeRemoved;
  320. SmallDenseSet<DebugVariable> VariableSet;
  321. for (auto &I : reverse(*BB)) {
  322. if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
  323. DebugVariable Key(DVI->getVariable(),
  324. DVI->getExpression(),
  325. DVI->getDebugLoc()->getInlinedAt());
  326. auto R = VariableSet.insert(Key);
  327. // If the same variable fragment is described more than once it is enough
  328. // to keep the last one (i.e. the first found since we for reverse
  329. // iteration).
  330. if (!R.second)
  331. ToBeRemoved.push_back(DVI);
  332. continue;
  333. }
  334. // Sequence with consecutive dbg.value instrs ended. Clear the map to
  335. // restart identifying redundant instructions if case we find another
  336. // dbg.value sequence.
  337. VariableSet.clear();
  338. }
  339. for (auto &Instr : ToBeRemoved)
  340. Instr->eraseFromParent();
  341. return !ToBeRemoved.empty();
  342. }
  343. /// Remove redundant dbg.value instructions using a forward scan. This can
  344. /// remove a dbg.value instruction that is redundant due to indicating that a
  345. /// variable has the same value as already being indicated by an earlier
  346. /// dbg.value.
  347. ///
  348. /// ForwardScan strategy:
  349. /// ---------------------
  350. /// Given two identical dbg.value instructions, separated by a block of
  351. /// instructions that isn't describing the same variable, like this
  352. ///
  353. /// dbg.value X1, "x", FragmentX1 (**)
  354. /// <block of instructions, none being "dbg.value ..., "x", ...">
  355. /// dbg.value X1, "x", FragmentX1 (*)
  356. ///
  357. /// then the instruction marked with (*) can be removed. Variable "x" is already
  358. /// described as being mapped to the SSA value X1.
  359. ///
  360. /// Possible improvements:
  361. /// - Keep track of non-overlapping fragments.
  362. static bool removeRedundantDbgInstrsUsingForwardScan(BasicBlock *BB) {
  363. SmallVector<DbgValueInst *, 8> ToBeRemoved;
  364. DenseMap<DebugVariable, std::pair<SmallVector<Value *, 4>, DIExpression *>>
  365. VariableMap;
  366. for (auto &I : *BB) {
  367. if (DbgValueInst *DVI = dyn_cast<DbgValueInst>(&I)) {
  368. DebugVariable Key(DVI->getVariable(),
  369. NoneType(),
  370. DVI->getDebugLoc()->getInlinedAt());
  371. auto VMI = VariableMap.find(Key);
  372. // Update the map if we found a new value/expression describing the
  373. // variable, or if the variable wasn't mapped already.
  374. SmallVector<Value *, 4> Values(DVI->getValues());
  375. if (VMI == VariableMap.end() || VMI->second.first != Values ||
  376. VMI->second.second != DVI->getExpression()) {
  377. VariableMap[Key] = {Values, DVI->getExpression()};
  378. continue;
  379. }
  380. // Found an identical mapping. Remember the instruction for later removal.
  381. ToBeRemoved.push_back(DVI);
  382. }
  383. }
  384. for (auto &Instr : ToBeRemoved)
  385. Instr->eraseFromParent();
  386. return !ToBeRemoved.empty();
  387. }
  388. bool llvm::RemoveRedundantDbgInstrs(BasicBlock *BB) {
  389. bool MadeChanges = false;
  390. // By using the "backward scan" strategy before the "forward scan" strategy we
  391. // can remove both dbg.value (2) and (3) in a situation like this:
  392. //
  393. // (1) dbg.value V1, "x", DIExpression()
  394. // ...
  395. // (2) dbg.value V2, "x", DIExpression()
  396. // (3) dbg.value V1, "x", DIExpression()
  397. //
  398. // The backward scan will remove (2), it is made obsolete by (3). After
  399. // getting (2) out of the way, the foward scan will remove (3) since "x"
  400. // already is described as having the value V1 at (1).
  401. MadeChanges |= removeRedundantDbgInstrsUsingBackwardScan(BB);
  402. MadeChanges |= removeRedundantDbgInstrsUsingForwardScan(BB);
  403. if (MadeChanges)
  404. LLVM_DEBUG(dbgs() << "Removed redundant dbg instrs from: "
  405. << BB->getName() << "\n");
  406. return MadeChanges;
  407. }
  408. void llvm::ReplaceInstWithValue(BasicBlock::InstListType &BIL,
  409. BasicBlock::iterator &BI, Value *V) {
  410. Instruction &I = *BI;
  411. // Replaces all of the uses of the instruction with uses of the value
  412. I.replaceAllUsesWith(V);
  413. // Make sure to propagate a name if there is one already.
  414. if (I.hasName() && !V->hasName())
  415. V->takeName(&I);
  416. // Delete the unnecessary instruction now...
  417. BI = BIL.erase(BI);
  418. }
  419. void llvm::ReplaceInstWithInst(BasicBlock::InstListType &BIL,
  420. BasicBlock::iterator &BI, Instruction *I) {
  421. assert(I->getParent() == nullptr &&
  422. "ReplaceInstWithInst: Instruction already inserted into basic block!");
  423. // Copy debug location to newly added instruction, if it wasn't already set
  424. // by the caller.
  425. if (!I->getDebugLoc())
  426. I->setDebugLoc(BI->getDebugLoc());
  427. // Insert the new instruction into the basic block...
  428. BasicBlock::iterator New = BIL.insert(BI, I);
  429. // Replace all uses of the old instruction, and delete it.
  430. ReplaceInstWithValue(BIL, BI, I);
  431. // Move BI back to point to the newly inserted instruction
  432. BI = New;
  433. }
  434. bool llvm::IsBlockFollowedByDeoptOrUnreachable(const BasicBlock *BB) {
  435. // Remember visited blocks to avoid infinite loop
  436. SmallPtrSet<const BasicBlock *, 8> VisitedBlocks;
  437. unsigned Depth = 0;
  438. while (BB && Depth++ < MaxDeoptOrUnreachableSuccessorCheckDepth &&
  439. VisitedBlocks.insert(BB).second) {
  440. if (BB->getTerminatingDeoptimizeCall() ||
  441. isa<UnreachableInst>(BB->getTerminator()))
  442. return true;
  443. BB = BB->getUniqueSuccessor();
  444. }
  445. return false;
  446. }
  447. void llvm::ReplaceInstWithInst(Instruction *From, Instruction *To) {
  448. BasicBlock::iterator BI(From);
  449. ReplaceInstWithInst(From->getParent()->getInstList(), BI, To);
  450. }
  451. BasicBlock *llvm::SplitEdge(BasicBlock *BB, BasicBlock *Succ, DominatorTree *DT,
  452. LoopInfo *LI, MemorySSAUpdater *MSSAU,
  453. const Twine &BBName) {
  454. unsigned SuccNum = GetSuccessorNumber(BB, Succ);
  455. Instruction *LatchTerm = BB->getTerminator();
  456. CriticalEdgeSplittingOptions Options =
  457. CriticalEdgeSplittingOptions(DT, LI, MSSAU).setPreserveLCSSA();
  458. if ((isCriticalEdge(LatchTerm, SuccNum, Options.MergeIdenticalEdges))) {
  459. // If it is a critical edge, and the succesor is an exception block, handle
  460. // the split edge logic in this specific function
  461. if (Succ->isEHPad())
  462. return ehAwareSplitEdge(BB, Succ, nullptr, nullptr, Options, BBName);
  463. // If this is a critical edge, let SplitKnownCriticalEdge do it.
  464. return SplitKnownCriticalEdge(LatchTerm, SuccNum, Options, BBName);
  465. }
  466. // If the edge isn't critical, then BB has a single successor or Succ has a
  467. // single pred. Split the block.
  468. if (BasicBlock *SP = Succ->getSinglePredecessor()) {
  469. // If the successor only has a single pred, split the top of the successor
  470. // block.
  471. assert(SP == BB && "CFG broken");
  472. SP = nullptr;
  473. return SplitBlock(Succ, &Succ->front(), DT, LI, MSSAU, BBName,
  474. /*Before=*/true);
  475. }
  476. // Otherwise, if BB has a single successor, split it at the bottom of the
  477. // block.
  478. assert(BB->getTerminator()->getNumSuccessors() == 1 &&
  479. "Should have a single succ!");
  480. return SplitBlock(BB, BB->getTerminator(), DT, LI, MSSAU, BBName);
  481. }
  482. void llvm::setUnwindEdgeTo(Instruction *TI, BasicBlock *Succ) {
  483. if (auto *II = dyn_cast<InvokeInst>(TI))
  484. II->setUnwindDest(Succ);
  485. else if (auto *CS = dyn_cast<CatchSwitchInst>(TI))
  486. CS->setUnwindDest(Succ);
  487. else if (auto *CR = dyn_cast<CleanupReturnInst>(TI))
  488. CR->setUnwindDest(Succ);
  489. else
  490. llvm_unreachable("unexpected terminator instruction");
  491. }
  492. void llvm::updatePhiNodes(BasicBlock *DestBB, BasicBlock *OldPred,
  493. BasicBlock *NewPred, PHINode *Until) {
  494. int BBIdx = 0;
  495. for (PHINode &PN : DestBB->phis()) {
  496. // We manually update the LandingPadReplacement PHINode and it is the last
  497. // PHI Node. So, if we find it, we are done.
  498. if (Until == &PN)
  499. break;
  500. // Reuse the previous value of BBIdx if it lines up. In cases where we
  501. // have multiple phi nodes with *lots* of predecessors, this is a speed
  502. // win because we don't have to scan the PHI looking for TIBB. This
  503. // happens because the BB list of PHI nodes are usually in the same
  504. // order.
  505. if (PN.getIncomingBlock(BBIdx) != OldPred)
  506. BBIdx = PN.getBasicBlockIndex(OldPred);
  507. assert(BBIdx != -1 && "Invalid PHI Index!");
  508. PN.setIncomingBlock(BBIdx, NewPred);
  509. }
  510. }
  511. BasicBlock *llvm::ehAwareSplitEdge(BasicBlock *BB, BasicBlock *Succ,
  512. LandingPadInst *OriginalPad,
  513. PHINode *LandingPadReplacement,
  514. const CriticalEdgeSplittingOptions &Options,
  515. const Twine &BBName) {
  516. auto *PadInst = Succ->getFirstNonPHI();
  517. if (!LandingPadReplacement && !PadInst->isEHPad())
  518. return SplitEdge(BB, Succ, Options.DT, Options.LI, Options.MSSAU, BBName);
  519. auto *LI = Options.LI;
  520. SmallVector<BasicBlock *, 4> LoopPreds;
  521. // Check if extra modifications will be required to preserve loop-simplify
  522. // form after splitting. If it would require splitting blocks with IndirectBr
  523. // terminators, bail out if preserving loop-simplify form is requested.
  524. if (Options.PreserveLoopSimplify && LI) {
  525. if (Loop *BBLoop = LI->getLoopFor(BB)) {
  526. // The only way that we can break LoopSimplify form by splitting a
  527. // critical edge is when there exists some edge from BBLoop to Succ *and*
  528. // the only edge into Succ from outside of BBLoop is that of NewBB after
  529. // the split. If the first isn't true, then LoopSimplify still holds,
  530. // NewBB is the new exit block and it has no non-loop predecessors. If the
  531. // second isn't true, then Succ was not in LoopSimplify form prior to
  532. // the split as it had a non-loop predecessor. In both of these cases,
  533. // the predecessor must be directly in BBLoop, not in a subloop, or again
  534. // LoopSimplify doesn't hold.
  535. for (BasicBlock *P : predecessors(Succ)) {
  536. if (P == BB)
  537. continue; // The new block is known.
  538. if (LI->getLoopFor(P) != BBLoop) {
  539. // Loop is not in LoopSimplify form, no need to re simplify after
  540. // splitting edge.
  541. LoopPreds.clear();
  542. break;
  543. }
  544. LoopPreds.push_back(P);
  545. }
  546. // Loop-simplify form can be preserved, if we can split all in-loop
  547. // predecessors.
  548. if (any_of(LoopPreds, [](BasicBlock *Pred) {
  549. return isa<IndirectBrInst>(Pred->getTerminator());
  550. })) {
  551. return nullptr;
  552. }
  553. }
  554. }
  555. auto *NewBB =
  556. BasicBlock::Create(BB->getContext(), BBName, BB->getParent(), Succ);
  557. setUnwindEdgeTo(BB->getTerminator(), NewBB);
  558. updatePhiNodes(Succ, BB, NewBB, LandingPadReplacement);
  559. if (LandingPadReplacement) {
  560. auto *NewLP = OriginalPad->clone();
  561. auto *Terminator = BranchInst::Create(Succ, NewBB);
  562. NewLP->insertBefore(Terminator);
  563. LandingPadReplacement->addIncoming(NewLP, NewBB);
  564. } else {
  565. Value *ParentPad = nullptr;
  566. if (auto *FuncletPad = dyn_cast<FuncletPadInst>(PadInst))
  567. ParentPad = FuncletPad->getParentPad();
  568. else if (auto *CatchSwitch = dyn_cast<CatchSwitchInst>(PadInst))
  569. ParentPad = CatchSwitch->getParentPad();
  570. else if (auto *CleanupPad = dyn_cast<CleanupPadInst>(PadInst))
  571. ParentPad = CleanupPad->getParentPad();
  572. else if (auto *LandingPad = dyn_cast<LandingPadInst>(PadInst))
  573. ParentPad = LandingPad->getParent();
  574. else
  575. llvm_unreachable("handling for other EHPads not implemented yet");
  576. auto *NewCleanupPad = CleanupPadInst::Create(ParentPad, {}, BBName, NewBB);
  577. CleanupReturnInst::Create(NewCleanupPad, Succ, NewBB);
  578. }
  579. auto *DT = Options.DT;
  580. auto *MSSAU = Options.MSSAU;
  581. if (!DT && !LI)
  582. return NewBB;
  583. if (DT) {
  584. DomTreeUpdater DTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
  585. SmallVector<DominatorTree::UpdateType, 3> Updates;
  586. Updates.push_back({DominatorTree::Insert, BB, NewBB});
  587. Updates.push_back({DominatorTree::Insert, NewBB, Succ});
  588. Updates.push_back({DominatorTree::Delete, BB, Succ});
  589. DTU.applyUpdates(Updates);
  590. DTU.flush();
  591. if (MSSAU) {
  592. MSSAU->applyUpdates(Updates, *DT);
  593. if (VerifyMemorySSA)
  594. MSSAU->getMemorySSA()->verifyMemorySSA();
  595. }
  596. }
  597. if (LI) {
  598. if (Loop *BBLoop = LI->getLoopFor(BB)) {
  599. // If one or the other blocks were not in a loop, the new block is not
  600. // either, and thus LI doesn't need to be updated.
  601. if (Loop *SuccLoop = LI->getLoopFor(Succ)) {
  602. if (BBLoop == SuccLoop) {
  603. // Both in the same loop, the NewBB joins loop.
  604. SuccLoop->addBasicBlockToLoop(NewBB, *LI);
  605. } else if (BBLoop->contains(SuccLoop)) {
  606. // Edge from an outer loop to an inner loop. Add to the outer loop.
  607. BBLoop->addBasicBlockToLoop(NewBB, *LI);
  608. } else if (SuccLoop->contains(BBLoop)) {
  609. // Edge from an inner loop to an outer loop. Add to the outer loop.
  610. SuccLoop->addBasicBlockToLoop(NewBB, *LI);
  611. } else {
  612. // Edge from two loops with no containment relation. Because these
  613. // are natural loops, we know that the destination block must be the
  614. // header of its loop (adding a branch into a loop elsewhere would
  615. // create an irreducible loop).
  616. assert(SuccLoop->getHeader() == Succ &&
  617. "Should not create irreducible loops!");
  618. if (Loop *P = SuccLoop->getParentLoop())
  619. P->addBasicBlockToLoop(NewBB, *LI);
  620. }
  621. }
  622. // If BB is in a loop and Succ is outside of that loop, we may need to
  623. // update LoopSimplify form and LCSSA form.
  624. if (!BBLoop->contains(Succ)) {
  625. assert(!BBLoop->contains(NewBB) &&
  626. "Split point for loop exit is contained in loop!");
  627. // Update LCSSA form in the newly created exit block.
  628. if (Options.PreserveLCSSA) {
  629. createPHIsForSplitLoopExit(BB, NewBB, Succ);
  630. }
  631. if (!LoopPreds.empty()) {
  632. BasicBlock *NewExitBB = SplitBlockPredecessors(
  633. Succ, LoopPreds, "split", DT, LI, MSSAU, Options.PreserveLCSSA);
  634. if (Options.PreserveLCSSA)
  635. createPHIsForSplitLoopExit(LoopPreds, NewExitBB, Succ);
  636. }
  637. }
  638. }
  639. }
  640. return NewBB;
  641. }
  642. void llvm::createPHIsForSplitLoopExit(ArrayRef<BasicBlock *> Preds,
  643. BasicBlock *SplitBB, BasicBlock *DestBB) {
  644. // SplitBB shouldn't have anything non-trivial in it yet.
  645. assert((SplitBB->getFirstNonPHI() == SplitBB->getTerminator() ||
  646. SplitBB->isLandingPad()) &&
  647. "SplitBB has non-PHI nodes!");
  648. // For each PHI in the destination block.
  649. for (PHINode &PN : DestBB->phis()) {
  650. int Idx = PN.getBasicBlockIndex(SplitBB);
  651. assert(Idx >= 0 && "Invalid Block Index");
  652. Value *V = PN.getIncomingValue(Idx);
  653. // If the input is a PHI which already satisfies LCSSA, don't create
  654. // a new one.
  655. if (const PHINode *VP = dyn_cast<PHINode>(V))
  656. if (VP->getParent() == SplitBB)
  657. continue;
  658. // Otherwise a new PHI is needed. Create one and populate it.
  659. PHINode *NewPN = PHINode::Create(
  660. PN.getType(), Preds.size(), "split",
  661. SplitBB->isLandingPad() ? &SplitBB->front() : SplitBB->getTerminator());
  662. for (BasicBlock *BB : Preds)
  663. NewPN->addIncoming(V, BB);
  664. // Update the original PHI.
  665. PN.setIncomingValue(Idx, NewPN);
  666. }
  667. }
  668. unsigned
  669. llvm::SplitAllCriticalEdges(Function &F,
  670. const CriticalEdgeSplittingOptions &Options) {
  671. unsigned NumBroken = 0;
  672. for (BasicBlock &BB : F) {
  673. Instruction *TI = BB.getTerminator();
  674. if (TI->getNumSuccessors() > 1 && !isa<IndirectBrInst>(TI) &&
  675. !isa<CallBrInst>(TI))
  676. for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
  677. if (SplitCriticalEdge(TI, i, Options))
  678. ++NumBroken;
  679. }
  680. return NumBroken;
  681. }
  682. static BasicBlock *SplitBlockImpl(BasicBlock *Old, Instruction *SplitPt,
  683. DomTreeUpdater *DTU, DominatorTree *DT,
  684. LoopInfo *LI, MemorySSAUpdater *MSSAU,
  685. const Twine &BBName, bool Before) {
  686. if (Before) {
  687. DomTreeUpdater LocalDTU(DT, DomTreeUpdater::UpdateStrategy::Lazy);
  688. return splitBlockBefore(Old, SplitPt,
  689. DTU ? DTU : (DT ? &LocalDTU : nullptr), LI, MSSAU,
  690. BBName);
  691. }
  692. BasicBlock::iterator SplitIt = SplitPt->getIterator();
  693. while (isa<PHINode>(SplitIt) || SplitIt->isEHPad()) {
  694. ++SplitIt;
  695. assert(SplitIt != SplitPt->getParent()->end());
  696. }
  697. std::string Name = BBName.str();
  698. BasicBlock *New = Old->splitBasicBlock(
  699. SplitIt, Name.empty() ? Old->getName() + ".split" : Name);
  700. // The new block lives in whichever loop the old one did. This preserves
  701. // LCSSA as well, because we force the split point to be after any PHI nodes.
  702. if (LI)
  703. if (Loop *L = LI->getLoopFor(Old))
  704. L->addBasicBlockToLoop(New, *LI);
  705. if (DTU) {
  706. SmallVector<DominatorTree::UpdateType, 8> Updates;
  707. // Old dominates New. New node dominates all other nodes dominated by Old.
  708. SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfOld;
  709. Updates.push_back({DominatorTree::Insert, Old, New});
  710. Updates.reserve(Updates.size() + 2 * succ_size(New));
  711. for (BasicBlock *SuccessorOfOld : successors(New))
  712. if (UniqueSuccessorsOfOld.insert(SuccessorOfOld).second) {
  713. Updates.push_back({DominatorTree::Insert, New, SuccessorOfOld});
  714. Updates.push_back({DominatorTree::Delete, Old, SuccessorOfOld});
  715. }
  716. DTU->applyUpdates(Updates);
  717. } else if (DT)
  718. // Old dominates New. New node dominates all other nodes dominated by Old.
  719. if (DomTreeNode *OldNode = DT->getNode(Old)) {
  720. std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
  721. DomTreeNode *NewNode = DT->addNewBlock(New, Old);
  722. for (DomTreeNode *I : Children)
  723. DT->changeImmediateDominator(I, NewNode);
  724. }
  725. // Move MemoryAccesses still tracked in Old, but part of New now.
  726. // Update accesses in successor blocks accordingly.
  727. if (MSSAU)
  728. MSSAU->moveAllAfterSpliceBlocks(Old, New, &*(New->begin()));
  729. return New;
  730. }
  731. BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
  732. DominatorTree *DT, LoopInfo *LI,
  733. MemorySSAUpdater *MSSAU, const Twine &BBName,
  734. bool Before) {
  735. return SplitBlockImpl(Old, SplitPt, /*DTU=*/nullptr, DT, LI, MSSAU, BBName,
  736. Before);
  737. }
  738. BasicBlock *llvm::SplitBlock(BasicBlock *Old, Instruction *SplitPt,
  739. DomTreeUpdater *DTU, LoopInfo *LI,
  740. MemorySSAUpdater *MSSAU, const Twine &BBName,
  741. bool Before) {
  742. return SplitBlockImpl(Old, SplitPt, DTU, /*DT=*/nullptr, LI, MSSAU, BBName,
  743. Before);
  744. }
  745. BasicBlock *llvm::splitBlockBefore(BasicBlock *Old, Instruction *SplitPt,
  746. DomTreeUpdater *DTU, LoopInfo *LI,
  747. MemorySSAUpdater *MSSAU,
  748. const Twine &BBName) {
  749. BasicBlock::iterator SplitIt = SplitPt->getIterator();
  750. while (isa<PHINode>(SplitIt) || SplitIt->isEHPad())
  751. ++SplitIt;
  752. std::string Name = BBName.str();
  753. BasicBlock *New = Old->splitBasicBlock(
  754. SplitIt, Name.empty() ? Old->getName() + ".split" : Name,
  755. /* Before=*/true);
  756. // The new block lives in whichever loop the old one did. This preserves
  757. // LCSSA as well, because we force the split point to be after any PHI nodes.
  758. if (LI)
  759. if (Loop *L = LI->getLoopFor(Old))
  760. L->addBasicBlockToLoop(New, *LI);
  761. if (DTU) {
  762. SmallVector<DominatorTree::UpdateType, 8> DTUpdates;
  763. // New dominates Old. The predecessor nodes of the Old node dominate
  764. // New node.
  765. SmallPtrSet<BasicBlock *, 8> UniquePredecessorsOfOld;
  766. DTUpdates.push_back({DominatorTree::Insert, New, Old});
  767. DTUpdates.reserve(DTUpdates.size() + 2 * pred_size(New));
  768. for (BasicBlock *PredecessorOfOld : predecessors(New))
  769. if (UniquePredecessorsOfOld.insert(PredecessorOfOld).second) {
  770. DTUpdates.push_back({DominatorTree::Insert, PredecessorOfOld, New});
  771. DTUpdates.push_back({DominatorTree::Delete, PredecessorOfOld, Old});
  772. }
  773. DTU->applyUpdates(DTUpdates);
  774. // Move MemoryAccesses still tracked in Old, but part of New now.
  775. // Update accesses in successor blocks accordingly.
  776. if (MSSAU) {
  777. MSSAU->applyUpdates(DTUpdates, DTU->getDomTree());
  778. if (VerifyMemorySSA)
  779. MSSAU->getMemorySSA()->verifyMemorySSA();
  780. }
  781. }
  782. return New;
  783. }
  784. /// Update DominatorTree, LoopInfo, and LCCSA analysis information.
  785. static void UpdateAnalysisInformation(BasicBlock *OldBB, BasicBlock *NewBB,
  786. ArrayRef<BasicBlock *> Preds,
  787. DomTreeUpdater *DTU, DominatorTree *DT,
  788. LoopInfo *LI, MemorySSAUpdater *MSSAU,
  789. bool PreserveLCSSA, bool &HasLoopExit) {
  790. // Update dominator tree if available.
  791. if (DTU) {
  792. // Recalculation of DomTree is needed when updating a forward DomTree and
  793. // the Entry BB is replaced.
  794. if (NewBB->isEntryBlock() && DTU->hasDomTree()) {
  795. // The entry block was removed and there is no external interface for
  796. // the dominator tree to be notified of this change. In this corner-case
  797. // we recalculate the entire tree.
  798. DTU->recalculate(*NewBB->getParent());
  799. } else {
  800. // Split block expects NewBB to have a non-empty set of predecessors.
  801. SmallVector<DominatorTree::UpdateType, 8> Updates;
  802. SmallPtrSet<BasicBlock *, 8> UniquePreds;
  803. Updates.push_back({DominatorTree::Insert, NewBB, OldBB});
  804. Updates.reserve(Updates.size() + 2 * Preds.size());
  805. for (auto *Pred : Preds)
  806. if (UniquePreds.insert(Pred).second) {
  807. Updates.push_back({DominatorTree::Insert, Pred, NewBB});
  808. Updates.push_back({DominatorTree::Delete, Pred, OldBB});
  809. }
  810. DTU->applyUpdates(Updates);
  811. }
  812. } else if (DT) {
  813. if (OldBB == DT->getRootNode()->getBlock()) {
  814. assert(NewBB->isEntryBlock());
  815. DT->setNewRoot(NewBB);
  816. } else {
  817. // Split block expects NewBB to have a non-empty set of predecessors.
  818. DT->splitBlock(NewBB);
  819. }
  820. }
  821. // Update MemoryPhis after split if MemorySSA is available
  822. if (MSSAU)
  823. MSSAU->wireOldPredecessorsToNewImmediatePredecessor(OldBB, NewBB, Preds);
  824. // The rest of the logic is only relevant for updating the loop structures.
  825. if (!LI)
  826. return;
  827. if (DTU && DTU->hasDomTree())
  828. DT = &DTU->getDomTree();
  829. assert(DT && "DT should be available to update LoopInfo!");
  830. Loop *L = LI->getLoopFor(OldBB);
  831. // If we need to preserve loop analyses, collect some information about how
  832. // this split will affect loops.
  833. bool IsLoopEntry = !!L;
  834. bool SplitMakesNewLoopHeader = false;
  835. for (BasicBlock *Pred : Preds) {
  836. // Preds that are not reachable from entry should not be used to identify if
  837. // OldBB is a loop entry or if SplitMakesNewLoopHeader. Unreachable blocks
  838. // are not within any loops, so we incorrectly mark SplitMakesNewLoopHeader
  839. // as true and make the NewBB the header of some loop. This breaks LI.
  840. if (!DT->isReachableFromEntry(Pred))
  841. continue;
  842. // If we need to preserve LCSSA, determine if any of the preds is a loop
  843. // exit.
  844. if (PreserveLCSSA)
  845. if (Loop *PL = LI->getLoopFor(Pred))
  846. if (!PL->contains(OldBB))
  847. HasLoopExit = true;
  848. // If we need to preserve LoopInfo, note whether any of the preds crosses
  849. // an interesting loop boundary.
  850. if (!L)
  851. continue;
  852. if (L->contains(Pred))
  853. IsLoopEntry = false;
  854. else
  855. SplitMakesNewLoopHeader = true;
  856. }
  857. // Unless we have a loop for OldBB, nothing else to do here.
  858. if (!L)
  859. return;
  860. if (IsLoopEntry) {
  861. // Add the new block to the nearest enclosing loop (and not an adjacent
  862. // loop). To find this, examine each of the predecessors and determine which
  863. // loops enclose them, and select the most-nested loop which contains the
  864. // loop containing the block being split.
  865. Loop *InnermostPredLoop = nullptr;
  866. for (BasicBlock *Pred : Preds) {
  867. if (Loop *PredLoop = LI->getLoopFor(Pred)) {
  868. // Seek a loop which actually contains the block being split (to avoid
  869. // adjacent loops).
  870. while (PredLoop && !PredLoop->contains(OldBB))
  871. PredLoop = PredLoop->getParentLoop();
  872. // Select the most-nested of these loops which contains the block.
  873. if (PredLoop && PredLoop->contains(OldBB) &&
  874. (!InnermostPredLoop ||
  875. InnermostPredLoop->getLoopDepth() < PredLoop->getLoopDepth()))
  876. InnermostPredLoop = PredLoop;
  877. }
  878. }
  879. if (InnermostPredLoop)
  880. InnermostPredLoop->addBasicBlockToLoop(NewBB, *LI);
  881. } else {
  882. L->addBasicBlockToLoop(NewBB, *LI);
  883. if (SplitMakesNewLoopHeader)
  884. L->moveToHeader(NewBB);
  885. }
  886. }
  887. /// Update the PHI nodes in OrigBB to include the values coming from NewBB.
  888. /// This also updates AliasAnalysis, if available.
  889. static void UpdatePHINodes(BasicBlock *OrigBB, BasicBlock *NewBB,
  890. ArrayRef<BasicBlock *> Preds, BranchInst *BI,
  891. bool HasLoopExit) {
  892. // Otherwise, create a new PHI node in NewBB for each PHI node in OrigBB.
  893. SmallPtrSet<BasicBlock *, 16> PredSet(Preds.begin(), Preds.end());
  894. for (BasicBlock::iterator I = OrigBB->begin(); isa<PHINode>(I); ) {
  895. PHINode *PN = cast<PHINode>(I++);
  896. // Check to see if all of the values coming in are the same. If so, we
  897. // don't need to create a new PHI node, unless it's needed for LCSSA.
  898. Value *InVal = nullptr;
  899. if (!HasLoopExit) {
  900. InVal = PN->getIncomingValueForBlock(Preds[0]);
  901. for (unsigned i = 0, e = PN->getNumIncomingValues(); i != e; ++i) {
  902. if (!PredSet.count(PN->getIncomingBlock(i)))
  903. continue;
  904. if (!InVal)
  905. InVal = PN->getIncomingValue(i);
  906. else if (InVal != PN->getIncomingValue(i)) {
  907. InVal = nullptr;
  908. break;
  909. }
  910. }
  911. }
  912. if (InVal) {
  913. // If all incoming values for the new PHI would be the same, just don't
  914. // make a new PHI. Instead, just remove the incoming values from the old
  915. // PHI.
  916. // NOTE! This loop walks backwards for a reason! First off, this minimizes
  917. // the cost of removal if we end up removing a large number of values, and
  918. // second off, this ensures that the indices for the incoming values
  919. // aren't invalidated when we remove one.
  920. for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i)
  921. if (PredSet.count(PN->getIncomingBlock(i)))
  922. PN->removeIncomingValue(i, false);
  923. // Add an incoming value to the PHI node in the loop for the preheader
  924. // edge.
  925. PN->addIncoming(InVal, NewBB);
  926. continue;
  927. }
  928. // If the values coming into the block are not the same, we need a new
  929. // PHI.
  930. // Create the new PHI node, insert it into NewBB at the end of the block
  931. PHINode *NewPHI =
  932. PHINode::Create(PN->getType(), Preds.size(), PN->getName() + ".ph", BI);
  933. // NOTE! This loop walks backwards for a reason! First off, this minimizes
  934. // the cost of removal if we end up removing a large number of values, and
  935. // second off, this ensures that the indices for the incoming values aren't
  936. // invalidated when we remove one.
  937. for (int64_t i = PN->getNumIncomingValues() - 1; i >= 0; --i) {
  938. BasicBlock *IncomingBB = PN->getIncomingBlock(i);
  939. if (PredSet.count(IncomingBB)) {
  940. Value *V = PN->removeIncomingValue(i, false);
  941. NewPHI->addIncoming(V, IncomingBB);
  942. }
  943. }
  944. PN->addIncoming(NewPHI, NewBB);
  945. }
  946. }
  947. static void SplitLandingPadPredecessorsImpl(
  948. BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
  949. const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
  950. DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
  951. MemorySSAUpdater *MSSAU, bool PreserveLCSSA);
  952. static BasicBlock *
  953. SplitBlockPredecessorsImpl(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
  954. const char *Suffix, DomTreeUpdater *DTU,
  955. DominatorTree *DT, LoopInfo *LI,
  956. MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
  957. // Do not attempt to split that which cannot be split.
  958. if (!BB->canSplitPredecessors())
  959. return nullptr;
  960. // For the landingpads we need to act a bit differently.
  961. // Delegate this work to the SplitLandingPadPredecessors.
  962. if (BB->isLandingPad()) {
  963. SmallVector<BasicBlock*, 2> NewBBs;
  964. std::string NewName = std::string(Suffix) + ".split-lp";
  965. SplitLandingPadPredecessorsImpl(BB, Preds, Suffix, NewName.c_str(), NewBBs,
  966. DTU, DT, LI, MSSAU, PreserveLCSSA);
  967. return NewBBs[0];
  968. }
  969. // Create new basic block, insert right before the original block.
  970. BasicBlock *NewBB = BasicBlock::Create(
  971. BB->getContext(), BB->getName() + Suffix, BB->getParent(), BB);
  972. // The new block unconditionally branches to the old block.
  973. BranchInst *BI = BranchInst::Create(BB, NewBB);
  974. Loop *L = nullptr;
  975. BasicBlock *OldLatch = nullptr;
  976. // Splitting the predecessors of a loop header creates a preheader block.
  977. if (LI && LI->isLoopHeader(BB)) {
  978. L = LI->getLoopFor(BB);
  979. // Using the loop start line number prevents debuggers stepping into the
  980. // loop body for this instruction.
  981. BI->setDebugLoc(L->getStartLoc());
  982. // If BB is the header of the Loop, it is possible that the loop is
  983. // modified, such that the current latch does not remain the latch of the
  984. // loop. If that is the case, the loop metadata from the current latch needs
  985. // to be applied to the new latch.
  986. OldLatch = L->getLoopLatch();
  987. } else
  988. BI->setDebugLoc(BB->getFirstNonPHIOrDbg()->getDebugLoc());
  989. // Move the edges from Preds to point to NewBB instead of BB.
  990. for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
  991. // This is slightly more strict than necessary; the minimum requirement
  992. // is that there be no more than one indirectbr branching to BB. And
  993. // all BlockAddress uses would need to be updated.
  994. assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
  995. "Cannot split an edge from an IndirectBrInst");
  996. assert(!isa<CallBrInst>(Preds[i]->getTerminator()) &&
  997. "Cannot split an edge from a CallBrInst");
  998. Preds[i]->getTerminator()->replaceUsesOfWith(BB, NewBB);
  999. }
  1000. // Insert a new PHI node into NewBB for every PHI node in BB and that new PHI
  1001. // node becomes an incoming value for BB's phi node. However, if the Preds
  1002. // list is empty, we need to insert dummy entries into the PHI nodes in BB to
  1003. // account for the newly created predecessor.
  1004. if (Preds.empty()) {
  1005. // Insert dummy values as the incoming value.
  1006. for (BasicBlock::iterator I = BB->begin(); isa<PHINode>(I); ++I)
  1007. cast<PHINode>(I)->addIncoming(UndefValue::get(I->getType()), NewBB);
  1008. }
  1009. // Update DominatorTree, LoopInfo, and LCCSA analysis information.
  1010. bool HasLoopExit = false;
  1011. UpdateAnalysisInformation(BB, NewBB, Preds, DTU, DT, LI, MSSAU, PreserveLCSSA,
  1012. HasLoopExit);
  1013. if (!Preds.empty()) {
  1014. // Update the PHI nodes in BB with the values coming from NewBB.
  1015. UpdatePHINodes(BB, NewBB, Preds, BI, HasLoopExit);
  1016. }
  1017. if (OldLatch) {
  1018. BasicBlock *NewLatch = L->getLoopLatch();
  1019. if (NewLatch != OldLatch) {
  1020. MDNode *MD = OldLatch->getTerminator()->getMetadata("llvm.loop");
  1021. NewLatch->getTerminator()->setMetadata("llvm.loop", MD);
  1022. OldLatch->getTerminator()->setMetadata("llvm.loop", nullptr);
  1023. }
  1024. }
  1025. return NewBB;
  1026. }
  1027. BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
  1028. ArrayRef<BasicBlock *> Preds,
  1029. const char *Suffix, DominatorTree *DT,
  1030. LoopInfo *LI, MemorySSAUpdater *MSSAU,
  1031. bool PreserveLCSSA) {
  1032. return SplitBlockPredecessorsImpl(BB, Preds, Suffix, /*DTU=*/nullptr, DT, LI,
  1033. MSSAU, PreserveLCSSA);
  1034. }
  1035. BasicBlock *llvm::SplitBlockPredecessors(BasicBlock *BB,
  1036. ArrayRef<BasicBlock *> Preds,
  1037. const char *Suffix,
  1038. DomTreeUpdater *DTU, LoopInfo *LI,
  1039. MemorySSAUpdater *MSSAU,
  1040. bool PreserveLCSSA) {
  1041. return SplitBlockPredecessorsImpl(BB, Preds, Suffix, DTU,
  1042. /*DT=*/nullptr, LI, MSSAU, PreserveLCSSA);
  1043. }
  1044. static void SplitLandingPadPredecessorsImpl(
  1045. BasicBlock *OrigBB, ArrayRef<BasicBlock *> Preds, const char *Suffix1,
  1046. const char *Suffix2, SmallVectorImpl<BasicBlock *> &NewBBs,
  1047. DomTreeUpdater *DTU, DominatorTree *DT, LoopInfo *LI,
  1048. MemorySSAUpdater *MSSAU, bool PreserveLCSSA) {
  1049. assert(OrigBB->isLandingPad() && "Trying to split a non-landing pad!");
  1050. // Create a new basic block for OrigBB's predecessors listed in Preds. Insert
  1051. // it right before the original block.
  1052. BasicBlock *NewBB1 = BasicBlock::Create(OrigBB->getContext(),
  1053. OrigBB->getName() + Suffix1,
  1054. OrigBB->getParent(), OrigBB);
  1055. NewBBs.push_back(NewBB1);
  1056. // The new block unconditionally branches to the old block.
  1057. BranchInst *BI1 = BranchInst::Create(OrigBB, NewBB1);
  1058. BI1->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
  1059. // Move the edges from Preds to point to NewBB1 instead of OrigBB.
  1060. for (unsigned i = 0, e = Preds.size(); i != e; ++i) {
  1061. // This is slightly more strict than necessary; the minimum requirement
  1062. // is that there be no more than one indirectbr branching to BB. And
  1063. // all BlockAddress uses would need to be updated.
  1064. assert(!isa<IndirectBrInst>(Preds[i]->getTerminator()) &&
  1065. "Cannot split an edge from an IndirectBrInst");
  1066. Preds[i]->getTerminator()->replaceUsesOfWith(OrigBB, NewBB1);
  1067. }
  1068. bool HasLoopExit = false;
  1069. UpdateAnalysisInformation(OrigBB, NewBB1, Preds, DTU, DT, LI, MSSAU,
  1070. PreserveLCSSA, HasLoopExit);
  1071. // Update the PHI nodes in OrigBB with the values coming from NewBB1.
  1072. UpdatePHINodes(OrigBB, NewBB1, Preds, BI1, HasLoopExit);
  1073. // Move the remaining edges from OrigBB to point to NewBB2.
  1074. SmallVector<BasicBlock*, 8> NewBB2Preds;
  1075. for (pred_iterator i = pred_begin(OrigBB), e = pred_end(OrigBB);
  1076. i != e; ) {
  1077. BasicBlock *Pred = *i++;
  1078. if (Pred == NewBB1) continue;
  1079. assert(!isa<IndirectBrInst>(Pred->getTerminator()) &&
  1080. "Cannot split an edge from an IndirectBrInst");
  1081. NewBB2Preds.push_back(Pred);
  1082. e = pred_end(OrigBB);
  1083. }
  1084. BasicBlock *NewBB2 = nullptr;
  1085. if (!NewBB2Preds.empty()) {
  1086. // Create another basic block for the rest of OrigBB's predecessors.
  1087. NewBB2 = BasicBlock::Create(OrigBB->getContext(),
  1088. OrigBB->getName() + Suffix2,
  1089. OrigBB->getParent(), OrigBB);
  1090. NewBBs.push_back(NewBB2);
  1091. // The new block unconditionally branches to the old block.
  1092. BranchInst *BI2 = BranchInst::Create(OrigBB, NewBB2);
  1093. BI2->setDebugLoc(OrigBB->getFirstNonPHI()->getDebugLoc());
  1094. // Move the remaining edges from OrigBB to point to NewBB2.
  1095. for (BasicBlock *NewBB2Pred : NewBB2Preds)
  1096. NewBB2Pred->getTerminator()->replaceUsesOfWith(OrigBB, NewBB2);
  1097. // Update DominatorTree, LoopInfo, and LCCSA analysis information.
  1098. HasLoopExit = false;
  1099. UpdateAnalysisInformation(OrigBB, NewBB2, NewBB2Preds, DTU, DT, LI, MSSAU,
  1100. PreserveLCSSA, HasLoopExit);
  1101. // Update the PHI nodes in OrigBB with the values coming from NewBB2.
  1102. UpdatePHINodes(OrigBB, NewBB2, NewBB2Preds, BI2, HasLoopExit);
  1103. }
  1104. LandingPadInst *LPad = OrigBB->getLandingPadInst();
  1105. Instruction *Clone1 = LPad->clone();
  1106. Clone1->setName(Twine("lpad") + Suffix1);
  1107. NewBB1->getInstList().insert(NewBB1->getFirstInsertionPt(), Clone1);
  1108. if (NewBB2) {
  1109. Instruction *Clone2 = LPad->clone();
  1110. Clone2->setName(Twine("lpad") + Suffix2);
  1111. NewBB2->getInstList().insert(NewBB2->getFirstInsertionPt(), Clone2);
  1112. // Create a PHI node for the two cloned landingpad instructions only
  1113. // if the original landingpad instruction has some uses.
  1114. if (!LPad->use_empty()) {
  1115. assert(!LPad->getType()->isTokenTy() &&
  1116. "Split cannot be applied if LPad is token type. Otherwise an "
  1117. "invalid PHINode of token type would be created.");
  1118. PHINode *PN = PHINode::Create(LPad->getType(), 2, "lpad.phi", LPad);
  1119. PN->addIncoming(Clone1, NewBB1);
  1120. PN->addIncoming(Clone2, NewBB2);
  1121. LPad->replaceAllUsesWith(PN);
  1122. }
  1123. LPad->eraseFromParent();
  1124. } else {
  1125. // There is no second clone. Just replace the landing pad with the first
  1126. // clone.
  1127. LPad->replaceAllUsesWith(Clone1);
  1128. LPad->eraseFromParent();
  1129. }
  1130. }
  1131. void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
  1132. ArrayRef<BasicBlock *> Preds,
  1133. const char *Suffix1, const char *Suffix2,
  1134. SmallVectorImpl<BasicBlock *> &NewBBs,
  1135. DominatorTree *DT, LoopInfo *LI,
  1136. MemorySSAUpdater *MSSAU,
  1137. bool PreserveLCSSA) {
  1138. return SplitLandingPadPredecessorsImpl(
  1139. OrigBB, Preds, Suffix1, Suffix2, NewBBs,
  1140. /*DTU=*/nullptr, DT, LI, MSSAU, PreserveLCSSA);
  1141. }
  1142. void llvm::SplitLandingPadPredecessors(BasicBlock *OrigBB,
  1143. ArrayRef<BasicBlock *> Preds,
  1144. const char *Suffix1, const char *Suffix2,
  1145. SmallVectorImpl<BasicBlock *> &NewBBs,
  1146. DomTreeUpdater *DTU, LoopInfo *LI,
  1147. MemorySSAUpdater *MSSAU,
  1148. bool PreserveLCSSA) {
  1149. return SplitLandingPadPredecessorsImpl(OrigBB, Preds, Suffix1, Suffix2,
  1150. NewBBs, DTU, /*DT=*/nullptr, LI, MSSAU,
  1151. PreserveLCSSA);
  1152. }
  1153. ReturnInst *llvm::FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
  1154. BasicBlock *Pred,
  1155. DomTreeUpdater *DTU) {
  1156. Instruction *UncondBranch = Pred->getTerminator();
  1157. // Clone the return and add it to the end of the predecessor.
  1158. Instruction *NewRet = RI->clone();
  1159. Pred->getInstList().push_back(NewRet);
  1160. // If the return instruction returns a value, and if the value was a
  1161. // PHI node in "BB", propagate the right value into the return.
  1162. for (Use &Op : NewRet->operands()) {
  1163. Value *V = Op;
  1164. Instruction *NewBC = nullptr;
  1165. if (BitCastInst *BCI = dyn_cast<BitCastInst>(V)) {
  1166. // Return value might be bitcasted. Clone and insert it before the
  1167. // return instruction.
  1168. V = BCI->getOperand(0);
  1169. NewBC = BCI->clone();
  1170. Pred->getInstList().insert(NewRet->getIterator(), NewBC);
  1171. Op = NewBC;
  1172. }
  1173. Instruction *NewEV = nullptr;
  1174. if (ExtractValueInst *EVI = dyn_cast<ExtractValueInst>(V)) {
  1175. V = EVI->getOperand(0);
  1176. NewEV = EVI->clone();
  1177. if (NewBC) {
  1178. NewBC->setOperand(0, NewEV);
  1179. Pred->getInstList().insert(NewBC->getIterator(), NewEV);
  1180. } else {
  1181. Pred->getInstList().insert(NewRet->getIterator(), NewEV);
  1182. Op = NewEV;
  1183. }
  1184. }
  1185. if (PHINode *PN = dyn_cast<PHINode>(V)) {
  1186. if (PN->getParent() == BB) {
  1187. if (NewEV) {
  1188. NewEV->setOperand(0, PN->getIncomingValueForBlock(Pred));
  1189. } else if (NewBC)
  1190. NewBC->setOperand(0, PN->getIncomingValueForBlock(Pred));
  1191. else
  1192. Op = PN->getIncomingValueForBlock(Pred);
  1193. }
  1194. }
  1195. }
  1196. // Update any PHI nodes in the returning block to realize that we no
  1197. // longer branch to them.
  1198. BB->removePredecessor(Pred);
  1199. UncondBranch->eraseFromParent();
  1200. if (DTU)
  1201. DTU->applyUpdates({{DominatorTree::Delete, Pred, BB}});
  1202. return cast<ReturnInst>(NewRet);
  1203. }
  1204. static Instruction *
  1205. SplitBlockAndInsertIfThenImpl(Value *Cond, Instruction *SplitBefore,
  1206. bool Unreachable, MDNode *BranchWeights,
  1207. DomTreeUpdater *DTU, DominatorTree *DT,
  1208. LoopInfo *LI, BasicBlock *ThenBlock) {
  1209. SmallVector<DominatorTree::UpdateType, 8> Updates;
  1210. BasicBlock *Head = SplitBefore->getParent();
  1211. BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
  1212. if (DTU) {
  1213. SmallPtrSet<BasicBlock *, 8> UniqueSuccessorsOfHead;
  1214. Updates.push_back({DominatorTree::Insert, Head, Tail});
  1215. Updates.reserve(Updates.size() + 2 * succ_size(Tail));
  1216. for (BasicBlock *SuccessorOfHead : successors(Tail))
  1217. if (UniqueSuccessorsOfHead.insert(SuccessorOfHead).second) {
  1218. Updates.push_back({DominatorTree::Insert, Tail, SuccessorOfHead});
  1219. Updates.push_back({DominatorTree::Delete, Head, SuccessorOfHead});
  1220. }
  1221. }
  1222. Instruction *HeadOldTerm = Head->getTerminator();
  1223. LLVMContext &C = Head->getContext();
  1224. Instruction *CheckTerm;
  1225. bool CreateThenBlock = (ThenBlock == nullptr);
  1226. if (CreateThenBlock) {
  1227. ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
  1228. if (Unreachable)
  1229. CheckTerm = new UnreachableInst(C, ThenBlock);
  1230. else {
  1231. CheckTerm = BranchInst::Create(Tail, ThenBlock);
  1232. if (DTU)
  1233. Updates.push_back({DominatorTree::Insert, ThenBlock, Tail});
  1234. }
  1235. CheckTerm->setDebugLoc(SplitBefore->getDebugLoc());
  1236. } else
  1237. CheckTerm = ThenBlock->getTerminator();
  1238. BranchInst *HeadNewTerm =
  1239. BranchInst::Create(/*ifTrue*/ ThenBlock, /*ifFalse*/ Tail, Cond);
  1240. if (DTU)
  1241. Updates.push_back({DominatorTree::Insert, Head, ThenBlock});
  1242. HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
  1243. ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
  1244. if (DTU)
  1245. DTU->applyUpdates(Updates);
  1246. else if (DT) {
  1247. if (DomTreeNode *OldNode = DT->getNode(Head)) {
  1248. std::vector<DomTreeNode *> Children(OldNode->begin(), OldNode->end());
  1249. DomTreeNode *NewNode = DT->addNewBlock(Tail, Head);
  1250. for (DomTreeNode *Child : Children)
  1251. DT->changeImmediateDominator(Child, NewNode);
  1252. // Head dominates ThenBlock.
  1253. if (CreateThenBlock)
  1254. DT->addNewBlock(ThenBlock, Head);
  1255. else
  1256. DT->changeImmediateDominator(ThenBlock, Head);
  1257. }
  1258. }
  1259. if (LI) {
  1260. if (Loop *L = LI->getLoopFor(Head)) {
  1261. L->addBasicBlockToLoop(ThenBlock, *LI);
  1262. L->addBasicBlockToLoop(Tail, *LI);
  1263. }
  1264. }
  1265. return CheckTerm;
  1266. }
  1267. Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
  1268. Instruction *SplitBefore,
  1269. bool Unreachable,
  1270. MDNode *BranchWeights,
  1271. DominatorTree *DT, LoopInfo *LI,
  1272. BasicBlock *ThenBlock) {
  1273. return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable,
  1274. BranchWeights,
  1275. /*DTU=*/nullptr, DT, LI, ThenBlock);
  1276. }
  1277. Instruction *llvm::SplitBlockAndInsertIfThen(Value *Cond,
  1278. Instruction *SplitBefore,
  1279. bool Unreachable,
  1280. MDNode *BranchWeights,
  1281. DomTreeUpdater *DTU, LoopInfo *LI,
  1282. BasicBlock *ThenBlock) {
  1283. return SplitBlockAndInsertIfThenImpl(Cond, SplitBefore, Unreachable,
  1284. BranchWeights, DTU, /*DT=*/nullptr, LI,
  1285. ThenBlock);
  1286. }
  1287. void llvm::SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
  1288. Instruction **ThenTerm,
  1289. Instruction **ElseTerm,
  1290. MDNode *BranchWeights) {
  1291. BasicBlock *Head = SplitBefore->getParent();
  1292. BasicBlock *Tail = Head->splitBasicBlock(SplitBefore->getIterator());
  1293. Instruction *HeadOldTerm = Head->getTerminator();
  1294. LLVMContext &C = Head->getContext();
  1295. BasicBlock *ThenBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
  1296. BasicBlock *ElseBlock = BasicBlock::Create(C, "", Head->getParent(), Tail);
  1297. *ThenTerm = BranchInst::Create(Tail, ThenBlock);
  1298. (*ThenTerm)->setDebugLoc(SplitBefore->getDebugLoc());
  1299. *ElseTerm = BranchInst::Create(Tail, ElseBlock);
  1300. (*ElseTerm)->setDebugLoc(SplitBefore->getDebugLoc());
  1301. BranchInst *HeadNewTerm =
  1302. BranchInst::Create(/*ifTrue*/ThenBlock, /*ifFalse*/ElseBlock, Cond);
  1303. HeadNewTerm->setMetadata(LLVMContext::MD_prof, BranchWeights);
  1304. ReplaceInstWithInst(HeadOldTerm, HeadNewTerm);
  1305. }
  1306. BranchInst *llvm::GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
  1307. BasicBlock *&IfFalse) {
  1308. PHINode *SomePHI = dyn_cast<PHINode>(BB->begin());
  1309. BasicBlock *Pred1 = nullptr;
  1310. BasicBlock *Pred2 = nullptr;
  1311. if (SomePHI) {
  1312. if (SomePHI->getNumIncomingValues() != 2)
  1313. return nullptr;
  1314. Pred1 = SomePHI->getIncomingBlock(0);
  1315. Pred2 = SomePHI->getIncomingBlock(1);
  1316. } else {
  1317. pred_iterator PI = pred_begin(BB), PE = pred_end(BB);
  1318. if (PI == PE) // No predecessor
  1319. return nullptr;
  1320. Pred1 = *PI++;
  1321. if (PI == PE) // Only one predecessor
  1322. return nullptr;
  1323. Pred2 = *PI++;
  1324. if (PI != PE) // More than two predecessors
  1325. return nullptr;
  1326. }
  1327. // We can only handle branches. Other control flow will be lowered to
  1328. // branches if possible anyway.
  1329. BranchInst *Pred1Br = dyn_cast<BranchInst>(Pred1->getTerminator());
  1330. BranchInst *Pred2Br = dyn_cast<BranchInst>(Pred2->getTerminator());
  1331. if (!Pred1Br || !Pred2Br)
  1332. return nullptr;
  1333. // Eliminate code duplication by ensuring that Pred1Br is conditional if
  1334. // either are.
  1335. if (Pred2Br->isConditional()) {
  1336. // If both branches are conditional, we don't have an "if statement". In
  1337. // reality, we could transform this case, but since the condition will be
  1338. // required anyway, we stand no chance of eliminating it, so the xform is
  1339. // probably not profitable.
  1340. if (Pred1Br->isConditional())
  1341. return nullptr;
  1342. std::swap(Pred1, Pred2);
  1343. std::swap(Pred1Br, Pred2Br);
  1344. }
  1345. if (Pred1Br->isConditional()) {
  1346. // The only thing we have to watch out for here is to make sure that Pred2
  1347. // doesn't have incoming edges from other blocks. If it does, the condition
  1348. // doesn't dominate BB.
  1349. if (!Pred2->getSinglePredecessor())
  1350. return nullptr;
  1351. // If we found a conditional branch predecessor, make sure that it branches
  1352. // to BB and Pred2Br. If it doesn't, this isn't an "if statement".
  1353. if (Pred1Br->getSuccessor(0) == BB &&
  1354. Pred1Br->getSuccessor(1) == Pred2) {
  1355. IfTrue = Pred1;
  1356. IfFalse = Pred2;
  1357. } else if (Pred1Br->getSuccessor(0) == Pred2 &&
  1358. Pred1Br->getSuccessor(1) == BB) {
  1359. IfTrue = Pred2;
  1360. IfFalse = Pred1;
  1361. } else {
  1362. // We know that one arm of the conditional goes to BB, so the other must
  1363. // go somewhere unrelated, and this must not be an "if statement".
  1364. return nullptr;
  1365. }
  1366. return Pred1Br;
  1367. }
  1368. // Ok, if we got here, both predecessors end with an unconditional branch to
  1369. // BB. Don't panic! If both blocks only have a single (identical)
  1370. // predecessor, and THAT is a conditional branch, then we're all ok!
  1371. BasicBlock *CommonPred = Pred1->getSinglePredecessor();
  1372. if (CommonPred == nullptr || CommonPred != Pred2->getSinglePredecessor())
  1373. return nullptr;
  1374. // Otherwise, if this is a conditional branch, then we can use it!
  1375. BranchInst *BI = dyn_cast<BranchInst>(CommonPred->getTerminator());
  1376. if (!BI) return nullptr;
  1377. assert(BI->isConditional() && "Two successors but not conditional?");
  1378. if (BI->getSuccessor(0) == Pred1) {
  1379. IfTrue = Pred1;
  1380. IfFalse = Pred2;
  1381. } else {
  1382. IfTrue = Pred2;
  1383. IfFalse = Pred1;
  1384. }
  1385. return BI;
  1386. }
  1387. // After creating a control flow hub, the operands of PHINodes in an outgoing
  1388. // block Out no longer match the predecessors of that block. Predecessors of Out
  1389. // that are incoming blocks to the hub are now replaced by just one edge from
  1390. // the hub. To match this new control flow, the corresponding values from each
  1391. // PHINode must now be moved a new PHINode in the first guard block of the hub.
  1392. //
  1393. // This operation cannot be performed with SSAUpdater, because it involves one
  1394. // new use: If the block Out is in the list of Incoming blocks, then the newly
  1395. // created PHI in the Hub will use itself along that edge from Out to Hub.
  1396. static void reconnectPhis(BasicBlock *Out, BasicBlock *GuardBlock,
  1397. const SetVector<BasicBlock *> &Incoming,
  1398. BasicBlock *FirstGuardBlock) {
  1399. auto I = Out->begin();
  1400. while (I != Out->end() && isa<PHINode>(I)) {
  1401. auto Phi = cast<PHINode>(I);
  1402. auto NewPhi =
  1403. PHINode::Create(Phi->getType(), Incoming.size(),
  1404. Phi->getName() + ".moved", &FirstGuardBlock->back());
  1405. for (auto In : Incoming) {
  1406. Value *V = UndefValue::get(Phi->getType());
  1407. if (In == Out) {
  1408. V = NewPhi;
  1409. } else if (Phi->getBasicBlockIndex(In) != -1) {
  1410. V = Phi->removeIncomingValue(In, false);
  1411. }
  1412. NewPhi->addIncoming(V, In);
  1413. }
  1414. assert(NewPhi->getNumIncomingValues() == Incoming.size());
  1415. if (Phi->getNumOperands() == 0) {
  1416. Phi->replaceAllUsesWith(NewPhi);
  1417. I = Phi->eraseFromParent();
  1418. continue;
  1419. }
  1420. Phi->addIncoming(NewPhi, GuardBlock);
  1421. ++I;
  1422. }
  1423. }
  1424. using BBPredicates = DenseMap<BasicBlock *, PHINode *>;
  1425. using BBSetVector = SetVector<BasicBlock *>;
  1426. // Redirects the terminator of the incoming block to the first guard
  1427. // block in the hub. The condition of the original terminator (if it
  1428. // was conditional) and its original successors are returned as a
  1429. // tuple <condition, succ0, succ1>. The function additionally filters
  1430. // out successors that are not in the set of outgoing blocks.
  1431. //
  1432. // - condition is non-null iff the branch is conditional.
  1433. // - Succ1 is non-null iff the sole/taken target is an outgoing block.
  1434. // - Succ2 is non-null iff condition is non-null and the fallthrough
  1435. // target is an outgoing block.
  1436. static std::tuple<Value *, BasicBlock *, BasicBlock *>
  1437. redirectToHub(BasicBlock *BB, BasicBlock *FirstGuardBlock,
  1438. const BBSetVector &Outgoing) {
  1439. auto Branch = cast<BranchInst>(BB->getTerminator());
  1440. auto Condition = Branch->isConditional() ? Branch->getCondition() : nullptr;
  1441. BasicBlock *Succ0 = Branch->getSuccessor(0);
  1442. BasicBlock *Succ1 = nullptr;
  1443. Succ0 = Outgoing.count(Succ0) ? Succ0 : nullptr;
  1444. if (Branch->isUnconditional()) {
  1445. Branch->setSuccessor(0, FirstGuardBlock);
  1446. assert(Succ0);
  1447. } else {
  1448. Succ1 = Branch->getSuccessor(1);
  1449. Succ1 = Outgoing.count(Succ1) ? Succ1 : nullptr;
  1450. assert(Succ0 || Succ1);
  1451. if (Succ0 && !Succ1) {
  1452. Branch->setSuccessor(0, FirstGuardBlock);
  1453. } else if (Succ1 && !Succ0) {
  1454. Branch->setSuccessor(1, FirstGuardBlock);
  1455. } else {
  1456. Branch->eraseFromParent();
  1457. BranchInst::Create(FirstGuardBlock, BB);
  1458. }
  1459. }
  1460. assert(Succ0 || Succ1);
  1461. return std::make_tuple(Condition, Succ0, Succ1);
  1462. }
  1463. // Capture the existing control flow as guard predicates, and redirect
  1464. // control flow from every incoming block to the first guard block in
  1465. // the hub.
  1466. //
  1467. // There is one guard predicate for each outgoing block OutBB. The
  1468. // predicate is a PHINode with one input for each InBB which
  1469. // represents whether the hub should transfer control flow to OutBB if
  1470. // it arrived from InBB. These predicates are NOT ORTHOGONAL. The Hub
  1471. // evaluates them in the same order as the Outgoing set-vector, and
  1472. // control branches to the first outgoing block whose predicate
  1473. // evaluates to true.
  1474. static void convertToGuardPredicates(
  1475. BasicBlock *FirstGuardBlock, BBPredicates &GuardPredicates,
  1476. SmallVectorImpl<WeakVH> &DeletionCandidates, const BBSetVector &Incoming,
  1477. const BBSetVector &Outgoing) {
  1478. auto &Context = Incoming.front()->getContext();
  1479. auto BoolTrue = ConstantInt::getTrue(Context);
  1480. auto BoolFalse = ConstantInt::getFalse(Context);
  1481. // The predicate for the last outgoing is trivially true, and so we
  1482. // process only the first N-1 successors.
  1483. for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
  1484. auto Out = Outgoing[i];
  1485. LLVM_DEBUG(dbgs() << "Creating guard for " << Out->getName() << "\n");
  1486. auto Phi =
  1487. PHINode::Create(Type::getInt1Ty(Context), Incoming.size(),
  1488. StringRef("Guard.") + Out->getName(), FirstGuardBlock);
  1489. GuardPredicates[Out] = Phi;
  1490. }
  1491. for (auto In : Incoming) {
  1492. Value *Condition;
  1493. BasicBlock *Succ0;
  1494. BasicBlock *Succ1;
  1495. std::tie(Condition, Succ0, Succ1) =
  1496. redirectToHub(In, FirstGuardBlock, Outgoing);
  1497. // Optimization: Consider an incoming block A with both successors
  1498. // Succ0 and Succ1 in the set of outgoing blocks. The predicates
  1499. // for Succ0 and Succ1 complement each other. If Succ0 is visited
  1500. // first in the loop below, control will branch to Succ0 using the
  1501. // corresponding predicate. But if that branch is not taken, then
  1502. // control must reach Succ1, which means that the predicate for
  1503. // Succ1 is always true.
  1504. bool OneSuccessorDone = false;
  1505. for (int i = 0, e = Outgoing.size() - 1; i != e; ++i) {
  1506. auto Out = Outgoing[i];
  1507. auto Phi = GuardPredicates[Out];
  1508. if (Out != Succ0 && Out != Succ1) {
  1509. Phi->addIncoming(BoolFalse, In);
  1510. continue;
  1511. }
  1512. // Optimization: When only one successor is an outgoing block,
  1513. // the predicate is always true.
  1514. if (!Succ0 || !Succ1 || OneSuccessorDone) {
  1515. Phi->addIncoming(BoolTrue, In);
  1516. continue;
  1517. }
  1518. assert(Succ0 && Succ1);
  1519. OneSuccessorDone = true;
  1520. if (Out == Succ0) {
  1521. Phi->addIncoming(Condition, In);
  1522. continue;
  1523. }
  1524. auto Inverted = invertCondition(Condition);
  1525. DeletionCandidates.push_back(Condition);
  1526. Phi->addIncoming(Inverted, In);
  1527. }
  1528. }
  1529. }
  1530. // For each outgoing block OutBB, create a guard block in the Hub. The
  1531. // first guard block was already created outside, and available as the
  1532. // first element in the vector of guard blocks.
  1533. //
  1534. // Each guard block terminates in a conditional branch that transfers
  1535. // control to the corresponding outgoing block or the next guard
  1536. // block. The last guard block has two outgoing blocks as successors
  1537. // since the condition for the final outgoing block is trivially
  1538. // true. So we create one less block (including the first guard block)
  1539. // than the number of outgoing blocks.
  1540. static void createGuardBlocks(SmallVectorImpl<BasicBlock *> &GuardBlocks,
  1541. Function *F, const BBSetVector &Outgoing,
  1542. BBPredicates &GuardPredicates, StringRef Prefix) {
  1543. for (int i = 0, e = Outgoing.size() - 2; i != e; ++i) {
  1544. GuardBlocks.push_back(
  1545. BasicBlock::Create(F->getContext(), Prefix + ".guard", F));
  1546. }
  1547. assert(GuardBlocks.size() == GuardPredicates.size());
  1548. // To help keep the loop simple, temporarily append the last
  1549. // outgoing block to the list of guard blocks.
  1550. GuardBlocks.push_back(Outgoing.back());
  1551. for (int i = 0, e = GuardBlocks.size() - 1; i != e; ++i) {
  1552. auto Out = Outgoing[i];
  1553. assert(GuardPredicates.count(Out));
  1554. BranchInst::Create(Out, GuardBlocks[i + 1], GuardPredicates[Out],
  1555. GuardBlocks[i]);
  1556. }
  1557. // Remove the last block from the guard list.
  1558. GuardBlocks.pop_back();
  1559. }
  1560. BasicBlock *llvm::CreateControlFlowHub(
  1561. DomTreeUpdater *DTU, SmallVectorImpl<BasicBlock *> &GuardBlocks,
  1562. const BBSetVector &Incoming, const BBSetVector &Outgoing,
  1563. const StringRef Prefix) {
  1564. auto F = Incoming.front()->getParent();
  1565. auto FirstGuardBlock =
  1566. BasicBlock::Create(F->getContext(), Prefix + ".guard", F);
  1567. SmallVector<DominatorTree::UpdateType, 16> Updates;
  1568. if (DTU) {
  1569. for (auto In : Incoming) {
  1570. Updates.push_back({DominatorTree::Insert, In, FirstGuardBlock});
  1571. for (auto Succ : successors(In)) {
  1572. if (Outgoing.count(Succ))
  1573. Updates.push_back({DominatorTree::Delete, In, Succ});
  1574. }
  1575. }
  1576. }
  1577. BBPredicates GuardPredicates;
  1578. SmallVector<WeakVH, 8> DeletionCandidates;
  1579. convertToGuardPredicates(FirstGuardBlock, GuardPredicates, DeletionCandidates,
  1580. Incoming, Outgoing);
  1581. GuardBlocks.push_back(FirstGuardBlock);
  1582. createGuardBlocks(GuardBlocks, F, Outgoing, GuardPredicates, Prefix);
  1583. // Update the PHINodes in each outgoing block to match the new control flow.
  1584. for (int i = 0, e = GuardBlocks.size(); i != e; ++i) {
  1585. reconnectPhis(Outgoing[i], GuardBlocks[i], Incoming, FirstGuardBlock);
  1586. }
  1587. reconnectPhis(Outgoing.back(), GuardBlocks.back(), Incoming, FirstGuardBlock);
  1588. if (DTU) {
  1589. int NumGuards = GuardBlocks.size();
  1590. assert((int)Outgoing.size() == NumGuards + 1);
  1591. for (int i = 0; i != NumGuards - 1; ++i) {
  1592. Updates.push_back({DominatorTree::Insert, GuardBlocks[i], Outgoing[i]});
  1593. Updates.push_back(
  1594. {DominatorTree::Insert, GuardBlocks[i], GuardBlocks[i + 1]});
  1595. }
  1596. Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
  1597. Outgoing[NumGuards - 1]});
  1598. Updates.push_back({DominatorTree::Insert, GuardBlocks[NumGuards - 1],
  1599. Outgoing[NumGuards]});
  1600. DTU->applyUpdates(Updates);
  1601. }
  1602. for (auto I : DeletionCandidates) {
  1603. if (I->use_empty())
  1604. if (auto Inst = dyn_cast_or_null<Instruction>(I))
  1605. Inst->eraseFromParent();
  1606. }
  1607. return FirstGuardBlock;
  1608. }