WebAssemblyFixIrreducibleControlFlow.cpp 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560
  1. //=- WebAssemblyFixIrreducibleControlFlow.cpp - Fix irreducible control flow -//
  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. /// \file
  10. /// This file implements a pass that removes irreducible control flow.
  11. /// Irreducible control flow means multiple-entry loops, which this pass
  12. /// transforms to have a single entry.
  13. ///
  14. /// Note that LLVM has a generic pass that lowers irreducible control flow, but
  15. /// it linearizes control flow, turning diamonds into two triangles, which is
  16. /// both unnecessary and undesirable for WebAssembly.
  17. ///
  18. /// The big picture: We recursively process each "region", defined as a group
  19. /// of blocks with a single entry and no branches back to that entry. A region
  20. /// may be the entire function body, or the inner part of a loop, i.e., the
  21. /// loop's body without branches back to the loop entry. In each region we fix
  22. /// up multi-entry loops by adding a new block that can dispatch to each of the
  23. /// loop entries, based on the value of a label "helper" variable, and we
  24. /// replace direct branches to the entries with assignments to the label
  25. /// variable and a branch to the dispatch block. Then the dispatch block is the
  26. /// single entry in the loop containing the previous multiple entries. After
  27. /// ensuring all the loops in a region are reducible, we recurse into them. The
  28. /// total time complexity of this pass is:
  29. ///
  30. /// O(NumBlocks * NumNestedLoops * NumIrreducibleLoops +
  31. /// NumLoops * NumLoops)
  32. ///
  33. /// This pass is similar to what the Relooper [1] does. Both identify looping
  34. /// code that requires multiple entries, and resolve it in a similar way (in
  35. /// Relooper terminology, we implement a Multiple shape in a Loop shape). Note
  36. /// also that like the Relooper, we implement a "minimal" intervention: we only
  37. /// use the "label" helper for the blocks we absolutely must and no others. We
  38. /// also prioritize code size and do not duplicate code in order to resolve
  39. /// irreducibility. The graph algorithms for finding loops and entries and so
  40. /// forth are also similar to the Relooper. The main differences between this
  41. /// pass and the Relooper are:
  42. ///
  43. /// * We just care about irreducibility, so we just look at loops.
  44. /// * The Relooper emits structured control flow (with ifs etc.), while we
  45. /// emit a CFG.
  46. ///
  47. /// [1] Alon Zakai. 2011. Emscripten: an LLVM-to-JavaScript compiler. In
  48. /// Proceedings of the ACM international conference companion on Object oriented
  49. /// programming systems languages and applications companion (SPLASH '11). ACM,
  50. /// New York, NY, USA, 301-312. DOI=10.1145/2048147.2048224
  51. /// http://doi.acm.org/10.1145/2048147.2048224
  52. ///
  53. //===----------------------------------------------------------------------===//
  54. #include "MCTargetDesc/WebAssemblyMCTargetDesc.h"
  55. #include "WebAssembly.h"
  56. #include "WebAssemblySubtarget.h"
  57. #include "llvm/CodeGen/MachineFunctionPass.h"
  58. #include "llvm/CodeGen/MachineInstrBuilder.h"
  59. #include "llvm/Support/Debug.h"
  60. using namespace llvm;
  61. #define DEBUG_TYPE "wasm-fix-irreducible-control-flow"
  62. namespace {
  63. using BlockVector = SmallVector<MachineBasicBlock *, 4>;
  64. using BlockSet = SmallPtrSet<MachineBasicBlock *, 4>;
  65. static BlockVector getSortedEntries(const BlockSet &Entries) {
  66. BlockVector SortedEntries(Entries.begin(), Entries.end());
  67. llvm::sort(SortedEntries,
  68. [](const MachineBasicBlock *A, const MachineBasicBlock *B) {
  69. auto ANum = A->getNumber();
  70. auto BNum = B->getNumber();
  71. return ANum < BNum;
  72. });
  73. return SortedEntries;
  74. }
  75. // Calculates reachability in a region. Ignores branches to blocks outside of
  76. // the region, and ignores branches to the region entry (for the case where
  77. // the region is the inner part of a loop).
  78. class ReachabilityGraph {
  79. public:
  80. ReachabilityGraph(MachineBasicBlock *Entry, const BlockSet &Blocks)
  81. : Entry(Entry), Blocks(Blocks) {
  82. #ifndef NDEBUG
  83. // The region must have a single entry.
  84. for (auto *MBB : Blocks) {
  85. if (MBB != Entry) {
  86. for (auto *Pred : MBB->predecessors()) {
  87. assert(inRegion(Pred));
  88. }
  89. }
  90. }
  91. #endif
  92. calculate();
  93. }
  94. bool canReach(MachineBasicBlock *From, MachineBasicBlock *To) const {
  95. assert(inRegion(From) && inRegion(To));
  96. auto I = Reachable.find(From);
  97. if (I == Reachable.end())
  98. return false;
  99. return I->second.count(To);
  100. }
  101. // "Loopers" are blocks that are in a loop. We detect these by finding blocks
  102. // that can reach themselves.
  103. const BlockSet &getLoopers() const { return Loopers; }
  104. // Get all blocks that are loop entries.
  105. const BlockSet &getLoopEntries() const { return LoopEntries; }
  106. // Get all blocks that enter a particular loop from outside.
  107. const BlockSet &getLoopEnterers(MachineBasicBlock *LoopEntry) const {
  108. assert(inRegion(LoopEntry));
  109. auto I = LoopEnterers.find(LoopEntry);
  110. assert(I != LoopEnterers.end());
  111. return I->second;
  112. }
  113. private:
  114. MachineBasicBlock *Entry;
  115. const BlockSet &Blocks;
  116. BlockSet Loopers, LoopEntries;
  117. DenseMap<MachineBasicBlock *, BlockSet> LoopEnterers;
  118. bool inRegion(MachineBasicBlock *MBB) const { return Blocks.count(MBB); }
  119. // Maps a block to all the other blocks it can reach.
  120. DenseMap<MachineBasicBlock *, BlockSet> Reachable;
  121. void calculate() {
  122. // Reachability computation work list. Contains pairs of recent additions
  123. // (A, B) where we just added a link A => B.
  124. using BlockPair = std::pair<MachineBasicBlock *, MachineBasicBlock *>;
  125. SmallVector<BlockPair, 4> WorkList;
  126. // Add all relevant direct branches.
  127. for (auto *MBB : Blocks) {
  128. for (auto *Succ : MBB->successors()) {
  129. if (Succ != Entry && inRegion(Succ)) {
  130. Reachable[MBB].insert(Succ);
  131. WorkList.emplace_back(MBB, Succ);
  132. }
  133. }
  134. }
  135. while (!WorkList.empty()) {
  136. MachineBasicBlock *MBB, *Succ;
  137. std::tie(MBB, Succ) = WorkList.pop_back_val();
  138. assert(inRegion(MBB) && Succ != Entry && inRegion(Succ));
  139. if (MBB != Entry) {
  140. // We recently added MBB => Succ, and that means we may have enabled
  141. // Pred => MBB => Succ.
  142. for (auto *Pred : MBB->predecessors()) {
  143. if (Reachable[Pred].insert(Succ).second) {
  144. WorkList.emplace_back(Pred, Succ);
  145. }
  146. }
  147. }
  148. }
  149. // Blocks that can return to themselves are in a loop.
  150. for (auto *MBB : Blocks) {
  151. if (canReach(MBB, MBB)) {
  152. Loopers.insert(MBB);
  153. }
  154. }
  155. assert(!Loopers.count(Entry));
  156. // Find the loop entries - loopers reachable from blocks not in that loop -
  157. // and those outside blocks that reach them, the "loop enterers".
  158. for (auto *Looper : Loopers) {
  159. for (auto *Pred : Looper->predecessors()) {
  160. // Pred can reach Looper. If Looper can reach Pred, it is in the loop;
  161. // otherwise, it is a block that enters into the loop.
  162. if (!canReach(Looper, Pred)) {
  163. LoopEntries.insert(Looper);
  164. LoopEnterers[Looper].insert(Pred);
  165. }
  166. }
  167. }
  168. }
  169. };
  170. // Finds the blocks in a single-entry loop, given the loop entry and the
  171. // list of blocks that enter the loop.
  172. class LoopBlocks {
  173. public:
  174. LoopBlocks(MachineBasicBlock *Entry, const BlockSet &Enterers)
  175. : Entry(Entry), Enterers(Enterers) {
  176. calculate();
  177. }
  178. BlockSet &getBlocks() { return Blocks; }
  179. private:
  180. MachineBasicBlock *Entry;
  181. const BlockSet &Enterers;
  182. BlockSet Blocks;
  183. void calculate() {
  184. // Going backwards from the loop entry, if we ignore the blocks entering
  185. // from outside, we will traverse all the blocks in the loop.
  186. BlockVector WorkList;
  187. BlockSet AddedToWorkList;
  188. Blocks.insert(Entry);
  189. for (auto *Pred : Entry->predecessors()) {
  190. if (!Enterers.count(Pred)) {
  191. WorkList.push_back(Pred);
  192. AddedToWorkList.insert(Pred);
  193. }
  194. }
  195. while (!WorkList.empty()) {
  196. auto *MBB = WorkList.pop_back_val();
  197. assert(!Enterers.count(MBB));
  198. if (Blocks.insert(MBB).second) {
  199. for (auto *Pred : MBB->predecessors()) {
  200. if (AddedToWorkList.insert(Pred).second)
  201. WorkList.push_back(Pred);
  202. }
  203. }
  204. }
  205. }
  206. };
  207. class WebAssemblyFixIrreducibleControlFlow final : public MachineFunctionPass {
  208. StringRef getPassName() const override {
  209. return "WebAssembly Fix Irreducible Control Flow";
  210. }
  211. bool runOnMachineFunction(MachineFunction &MF) override;
  212. bool processRegion(MachineBasicBlock *Entry, BlockSet &Blocks,
  213. MachineFunction &MF);
  214. void makeSingleEntryLoop(BlockSet &Entries, BlockSet &Blocks,
  215. MachineFunction &MF, const ReachabilityGraph &Graph);
  216. public:
  217. static char ID; // Pass identification, replacement for typeid
  218. WebAssemblyFixIrreducibleControlFlow() : MachineFunctionPass(ID) {}
  219. };
  220. bool WebAssemblyFixIrreducibleControlFlow::processRegion(
  221. MachineBasicBlock *Entry, BlockSet &Blocks, MachineFunction &MF) {
  222. bool Changed = false;
  223. // Remove irreducibility before processing child loops, which may take
  224. // multiple iterations.
  225. while (true) {
  226. ReachabilityGraph Graph(Entry, Blocks);
  227. bool FoundIrreducibility = false;
  228. for (auto *LoopEntry : getSortedEntries(Graph.getLoopEntries())) {
  229. // Find mutual entries - all entries which can reach this one, and
  230. // are reached by it (that always includes LoopEntry itself). All mutual
  231. // entries must be in the same loop, so if we have more than one, then we
  232. // have irreducible control flow.
  233. //
  234. // (Note that we need to sort the entries here, as otherwise the order can
  235. // matter: being mutual is a symmetric relationship, and each set of
  236. // mutuals will be handled properly no matter which we see first. However,
  237. // there can be multiple disjoint sets of mutuals, and which we process
  238. // first changes the output.)
  239. //
  240. // Note that irreducibility may involve inner loops, e.g. imagine A
  241. // starts one loop, and it has B inside it which starts an inner loop.
  242. // If we add a branch from all the way on the outside to B, then in a
  243. // sense B is no longer an "inner" loop, semantically speaking. We will
  244. // fix that irreducibility by adding a block that dispatches to either
  245. // either A or B, so B will no longer be an inner loop in our output.
  246. // (A fancier approach might try to keep it as such.)
  247. //
  248. // Note that we still need to recurse into inner loops later, to handle
  249. // the case where the irreducibility is entirely nested - we would not
  250. // be able to identify that at this point, since the enclosing loop is
  251. // a group of blocks all of whom can reach each other. (We'll see the
  252. // irreducibility after removing branches to the top of that enclosing
  253. // loop.)
  254. BlockSet MutualLoopEntries;
  255. MutualLoopEntries.insert(LoopEntry);
  256. for (auto *OtherLoopEntry : Graph.getLoopEntries()) {
  257. if (OtherLoopEntry != LoopEntry &&
  258. Graph.canReach(LoopEntry, OtherLoopEntry) &&
  259. Graph.canReach(OtherLoopEntry, LoopEntry)) {
  260. MutualLoopEntries.insert(OtherLoopEntry);
  261. }
  262. }
  263. if (MutualLoopEntries.size() > 1) {
  264. makeSingleEntryLoop(MutualLoopEntries, Blocks, MF, Graph);
  265. FoundIrreducibility = true;
  266. Changed = true;
  267. break;
  268. }
  269. }
  270. // Only go on to actually process the inner loops when we are done
  271. // removing irreducible control flow and changing the graph. Modifying
  272. // the graph as we go is possible, and that might let us avoid looking at
  273. // the already-fixed loops again if we are careful, but all that is
  274. // complex and bug-prone. Since irreducible loops are rare, just starting
  275. // another iteration is best.
  276. if (FoundIrreducibility) {
  277. continue;
  278. }
  279. for (auto *LoopEntry : Graph.getLoopEntries()) {
  280. LoopBlocks InnerBlocks(LoopEntry, Graph.getLoopEnterers(LoopEntry));
  281. // Each of these calls to processRegion may change the graph, but are
  282. // guaranteed not to interfere with each other. The only changes we make
  283. // to the graph are to add blocks on the way to a loop entry. As the
  284. // loops are disjoint, that means we may only alter branches that exit
  285. // another loop, which are ignored when recursing into that other loop
  286. // anyhow.
  287. if (processRegion(LoopEntry, InnerBlocks.getBlocks(), MF)) {
  288. Changed = true;
  289. }
  290. }
  291. return Changed;
  292. }
  293. }
  294. // Given a set of entries to a single loop, create a single entry for that
  295. // loop by creating a dispatch block for them, routing control flow using
  296. // a helper variable. Also updates Blocks with any new blocks created, so
  297. // that we properly track all the blocks in the region. But this does not update
  298. // ReachabilityGraph; this will be updated in the caller of this function as
  299. // needed.
  300. void WebAssemblyFixIrreducibleControlFlow::makeSingleEntryLoop(
  301. BlockSet &Entries, BlockSet &Blocks, MachineFunction &MF,
  302. const ReachabilityGraph &Graph) {
  303. assert(Entries.size() >= 2);
  304. // Sort the entries to ensure a deterministic build.
  305. BlockVector SortedEntries = getSortedEntries(Entries);
  306. #ifndef NDEBUG
  307. for (auto *Block : SortedEntries)
  308. assert(Block->getNumber() != -1);
  309. if (SortedEntries.size() > 1) {
  310. for (auto I = SortedEntries.begin(), E = SortedEntries.end() - 1; I != E;
  311. ++I) {
  312. auto ANum = (*I)->getNumber();
  313. auto BNum = (*(std::next(I)))->getNumber();
  314. assert(ANum != BNum);
  315. }
  316. }
  317. #endif
  318. // Create a dispatch block which will contain a jump table to the entries.
  319. MachineBasicBlock *Dispatch = MF.CreateMachineBasicBlock();
  320. MF.insert(MF.end(), Dispatch);
  321. Blocks.insert(Dispatch);
  322. // Add the jump table.
  323. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  324. MachineInstrBuilder MIB =
  325. BuildMI(Dispatch, DebugLoc(), TII.get(WebAssembly::BR_TABLE_I32));
  326. // Add the register which will be used to tell the jump table which block to
  327. // jump to.
  328. MachineRegisterInfo &MRI = MF.getRegInfo();
  329. Register Reg = MRI.createVirtualRegister(&WebAssembly::I32RegClass);
  330. MIB.addReg(Reg);
  331. // Compute the indices in the superheader, one for each bad block, and
  332. // add them as successors.
  333. DenseMap<MachineBasicBlock *, unsigned> Indices;
  334. for (auto *Entry : SortedEntries) {
  335. auto Pair = Indices.insert(std::make_pair(Entry, 0));
  336. assert(Pair.second);
  337. unsigned Index = MIB.getInstr()->getNumExplicitOperands() - 1;
  338. Pair.first->second = Index;
  339. MIB.addMBB(Entry);
  340. Dispatch->addSuccessor(Entry);
  341. }
  342. // Rewrite the problematic successors for every block that wants to reach
  343. // the bad blocks. For simplicity, we just introduce a new block for every
  344. // edge we need to rewrite. (Fancier things are possible.)
  345. BlockVector AllPreds;
  346. for (auto *Entry : SortedEntries) {
  347. for (auto *Pred : Entry->predecessors()) {
  348. if (Pred != Dispatch) {
  349. AllPreds.push_back(Pred);
  350. }
  351. }
  352. }
  353. // This set stores predecessors within this loop.
  354. DenseSet<MachineBasicBlock *> InLoop;
  355. for (auto *Pred : AllPreds) {
  356. for (auto *Entry : Pred->successors()) {
  357. if (!Entries.count(Entry))
  358. continue;
  359. if (Graph.canReach(Entry, Pred)) {
  360. InLoop.insert(Pred);
  361. break;
  362. }
  363. }
  364. }
  365. // Record if each entry has a layout predecessor. This map stores
  366. // <<loop entry, Predecessor is within the loop?>, layout predecessor>
  367. DenseMap<PointerIntPair<MachineBasicBlock *, 1, bool>, MachineBasicBlock *>
  368. EntryToLayoutPred;
  369. for (auto *Pred : AllPreds) {
  370. bool PredInLoop = InLoop.count(Pred);
  371. for (auto *Entry : Pred->successors())
  372. if (Entries.count(Entry) && Pred->isLayoutSuccessor(Entry))
  373. EntryToLayoutPred[{Entry, PredInLoop}] = Pred;
  374. }
  375. // We need to create at most two routing blocks per entry: one for
  376. // predecessors outside the loop and one for predecessors inside the loop.
  377. // This map stores
  378. // <<loop entry, Predecessor is within the loop?>, routing block>
  379. DenseMap<PointerIntPair<MachineBasicBlock *, 1, bool>, MachineBasicBlock *>
  380. Map;
  381. for (auto *Pred : AllPreds) {
  382. bool PredInLoop = InLoop.count(Pred);
  383. for (auto *Entry : Pred->successors()) {
  384. if (!Entries.count(Entry) || Map.count({Entry, PredInLoop}))
  385. continue;
  386. // If there exists a layout predecessor of this entry and this predecessor
  387. // is not that, we rather create a routing block after that layout
  388. // predecessor to save a branch.
  389. if (auto *OtherPred = EntryToLayoutPred.lookup({Entry, PredInLoop}))
  390. if (OtherPred != Pred)
  391. continue;
  392. // This is a successor we need to rewrite.
  393. MachineBasicBlock *Routing = MF.CreateMachineBasicBlock();
  394. MF.insert(Pred->isLayoutSuccessor(Entry)
  395. ? MachineFunction::iterator(Entry)
  396. : MF.end(),
  397. Routing);
  398. Blocks.insert(Routing);
  399. // Set the jump table's register of the index of the block we wish to
  400. // jump to, and jump to the jump table.
  401. BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::CONST_I32), Reg)
  402. .addImm(Indices[Entry]);
  403. BuildMI(Routing, DebugLoc(), TII.get(WebAssembly::BR)).addMBB(Dispatch);
  404. Routing->addSuccessor(Dispatch);
  405. Map[{Entry, PredInLoop}] = Routing;
  406. }
  407. }
  408. for (auto *Pred : AllPreds) {
  409. bool PredInLoop = InLoop.count(Pred);
  410. // Remap the terminator operands and the successor list.
  411. for (MachineInstr &Term : Pred->terminators())
  412. for (auto &Op : Term.explicit_uses())
  413. if (Op.isMBB() && Indices.count(Op.getMBB()))
  414. Op.setMBB(Map[{Op.getMBB(), PredInLoop}]);
  415. for (auto *Succ : Pred->successors()) {
  416. if (!Entries.count(Succ))
  417. continue;
  418. auto *Routing = Map[{Succ, PredInLoop}];
  419. Pred->replaceSuccessor(Succ, Routing);
  420. }
  421. }
  422. // Create a fake default label, because br_table requires one.
  423. MIB.addMBB(MIB.getInstr()
  424. ->getOperand(MIB.getInstr()->getNumExplicitOperands() - 1)
  425. .getMBB());
  426. }
  427. } // end anonymous namespace
  428. char WebAssemblyFixIrreducibleControlFlow::ID = 0;
  429. INITIALIZE_PASS(WebAssemblyFixIrreducibleControlFlow, DEBUG_TYPE,
  430. "Removes irreducible control flow", false, false)
  431. FunctionPass *llvm::createWebAssemblyFixIrreducibleControlFlow() {
  432. return new WebAssemblyFixIrreducibleControlFlow();
  433. }
  434. // Test whether the given register has an ARGUMENT def.
  435. static bool hasArgumentDef(unsigned Reg, const MachineRegisterInfo &MRI) {
  436. for (const auto &Def : MRI.def_instructions(Reg))
  437. if (WebAssembly::isArgument(Def.getOpcode()))
  438. return true;
  439. return false;
  440. }
  441. // Add a register definition with IMPLICIT_DEFs for every register to cover for
  442. // register uses that don't have defs in every possible path.
  443. // TODO: This is fairly heavy-handed; find a better approach.
  444. static void addImplicitDefs(MachineFunction &MF) {
  445. const MachineRegisterInfo &MRI = MF.getRegInfo();
  446. const auto &TII = *MF.getSubtarget<WebAssemblySubtarget>().getInstrInfo();
  447. MachineBasicBlock &Entry = *MF.begin();
  448. for (unsigned I = 0, E = MRI.getNumVirtRegs(); I < E; ++I) {
  449. Register Reg = Register::index2VirtReg(I);
  450. // Skip unused registers.
  451. if (MRI.use_nodbg_empty(Reg))
  452. continue;
  453. // Skip registers that have an ARGUMENT definition.
  454. if (hasArgumentDef(Reg, MRI))
  455. continue;
  456. BuildMI(Entry, Entry.begin(), DebugLoc(),
  457. TII.get(WebAssembly::IMPLICIT_DEF), Reg);
  458. }
  459. // Move ARGUMENT_* instructions to the top of the entry block, so that their
  460. // liveness reflects the fact that these really are live-in values.
  461. for (MachineInstr &MI : llvm::make_early_inc_range(Entry)) {
  462. if (WebAssembly::isArgument(MI.getOpcode())) {
  463. MI.removeFromParent();
  464. Entry.insert(Entry.begin(), &MI);
  465. }
  466. }
  467. }
  468. bool WebAssemblyFixIrreducibleControlFlow::runOnMachineFunction(
  469. MachineFunction &MF) {
  470. LLVM_DEBUG(dbgs() << "********** Fixing Irreducible Control Flow **********\n"
  471. "********** Function: "
  472. << MF.getName() << '\n');
  473. // Start the recursive process on the entire function body.
  474. BlockSet AllBlocks;
  475. for (auto &MBB : MF) {
  476. AllBlocks.insert(&MBB);
  477. }
  478. if (LLVM_UNLIKELY(processRegion(&*MF.begin(), AllBlocks, MF))) {
  479. // We rewrote part of the function; recompute relevant things.
  480. MF.RenumberBlocks();
  481. // Now we've inserted dispatch blocks, some register uses can have incoming
  482. // paths without a def. For example, before this pass register %a was
  483. // defined in BB1 and used in BB2, and there was only one path from BB1 and
  484. // BB2. But if this pass inserts a dispatch block having multiple
  485. // predecessors between the two BBs, now there are paths to BB2 without
  486. // visiting BB1, and %a's use in BB2 is not dominated by its def. Adding
  487. // IMPLICIT_DEFs to all regs is one simple way to fix it.
  488. addImplicitDefs(MF);
  489. return true;
  490. }
  491. return false;
  492. }