Utils.cpp 7.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219
  1. //===--- Utils.cpp - Utility functions for the code generation --*- C++ -*-===//
  2. //
  3. // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
  4. // See https://llvm.org/LICENSE.txt for license information.
  5. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
  6. //
  7. //===----------------------------------------------------------------------===//
  8. //
  9. // This file contains utility functions for the code generation.
  10. //
  11. //===----------------------------------------------------------------------===//
  12. #include "polly/CodeGen/Utils.h"
  13. #include "polly/CodeGen/IRBuilder.h"
  14. #include "polly/ScopInfo.h"
  15. #include "llvm/Analysis/LoopInfo.h"
  16. #include "llvm/Analysis/RegionInfo.h"
  17. #include "llvm/Transforms/Utils/BasicBlockUtils.h"
  18. using namespace llvm;
  19. // Alternative to llvm::SplitCriticalEdge.
  20. //
  21. // Creates a new block which branches to Succ. The edge to split is redirected
  22. // to the new block.
  23. //
  24. // The issue with llvm::SplitCriticalEdge is that it does nothing if the edge is
  25. // not critical.
  26. // The issue with llvm::SplitEdge is that it does not always create the middle
  27. // block, but reuses Prev/Succ if it can. We always want a new middle block.
  28. static BasicBlock *splitEdge(BasicBlock *Prev, BasicBlock *Succ,
  29. const char *Suffix, DominatorTree *DT,
  30. LoopInfo *LI, RegionInfo *RI) {
  31. assert(Prev && Succ);
  32. // Before:
  33. // \ / / //
  34. // Prev / //
  35. // | \___/ //
  36. // | ___ //
  37. // | / \ //
  38. // Succ \ //
  39. // / \ \ //
  40. // The algorithm to update DominatorTree and LoopInfo of
  41. // llvm::SplitCriticalEdge is more efficient than
  42. // llvm::SplitBlockPredecessors, which is more general. In the future we might
  43. // either modify llvm::SplitCriticalEdge to allow skipping the critical edge
  44. // check; or Copy&Pase it here.
  45. BasicBlock *MiddleBlock = SplitBlockPredecessors(
  46. Succ, ArrayRef<BasicBlock *>(Prev), Suffix, DT, LI);
  47. if (RI) {
  48. Region *PrevRegion = RI->getRegionFor(Prev);
  49. Region *SuccRegion = RI->getRegionFor(Succ);
  50. if (PrevRegion->contains(MiddleBlock)) {
  51. RI->setRegionFor(MiddleBlock, PrevRegion);
  52. } else {
  53. RI->setRegionFor(MiddleBlock, SuccRegion);
  54. }
  55. }
  56. // After:
  57. // \ / / //
  58. // Prev / //
  59. // | \___/ //
  60. // | //
  61. // MiddleBlock //
  62. // | ___ //
  63. // | / \ //
  64. // Succ \ //
  65. // / \ \ //
  66. return MiddleBlock;
  67. }
  68. std::pair<polly::BBPair, BranchInst *>
  69. polly::executeScopConditionally(Scop &S, Value *RTC, DominatorTree &DT,
  70. RegionInfo &RI, LoopInfo &LI) {
  71. Region &R = S.getRegion();
  72. PollyIRBuilder Builder(S.getEntry());
  73. // Before:
  74. //
  75. // \ / //
  76. // EnteringBB //
  77. // _____|_____ //
  78. // / EntryBB \ //
  79. // | (region) | //
  80. // \_ExitingBB_/ //
  81. // | //
  82. // ExitBB //
  83. // / \ //
  84. // Create a fork block.
  85. BasicBlock *EnteringBB = S.getEnteringBlock();
  86. BasicBlock *EntryBB = S.getEntry();
  87. assert(EnteringBB && "Must be a simple region");
  88. BasicBlock *SplitBlock =
  89. splitEdge(EnteringBB, EntryBB, ".split_new_and_old", &DT, &LI, &RI);
  90. SplitBlock->setName("polly.split_new_and_old");
  91. // If EntryBB is the exit block of the region that includes Prev, exclude
  92. // SplitBlock from that region by making it itself the exit block. This is
  93. // trivially possible because there is just one edge to EnteringBB.
  94. // This is necessary because we will add an outgoing edge from SplitBlock,
  95. // which would violate the single exit block requirement of PrevRegion.
  96. Region *PrevRegion = RI.getRegionFor(EnteringBB);
  97. while (PrevRegion->getExit() == EntryBB) {
  98. PrevRegion->replaceExit(SplitBlock);
  99. PrevRegion = PrevRegion->getParent();
  100. }
  101. RI.setRegionFor(SplitBlock, PrevRegion);
  102. // Create a join block
  103. BasicBlock *ExitingBB = S.getExitingBlock();
  104. BasicBlock *ExitBB = S.getExit();
  105. assert(ExitingBB && "Must be a simple region");
  106. BasicBlock *MergeBlock =
  107. splitEdge(ExitingBB, ExitBB, ".merge_new_and_old", &DT, &LI, &RI);
  108. MergeBlock->setName("polly.merge_new_and_old");
  109. // Exclude the join block from the region.
  110. R.replaceExitRecursive(MergeBlock);
  111. RI.setRegionFor(MergeBlock, R.getParent());
  112. // \ / //
  113. // EnteringBB //
  114. // | //
  115. // SplitBlock //
  116. // _____|_____ //
  117. // / EntryBB \ //
  118. // | (region) | //
  119. // \_ExitingBB_/ //
  120. // | //
  121. // MergeBlock //
  122. // | //
  123. // ExitBB //
  124. // / \ //
  125. // Create the start and exiting block.
  126. Function *F = SplitBlock->getParent();
  127. BasicBlock *StartBlock =
  128. BasicBlock::Create(F->getContext(), "polly.start", F);
  129. BasicBlock *ExitingBlock =
  130. BasicBlock::Create(F->getContext(), "polly.exiting", F);
  131. SplitBlock->getTerminator()->eraseFromParent();
  132. Builder.SetInsertPoint(SplitBlock);
  133. BranchInst *CondBr = Builder.CreateCondBr(RTC, StartBlock, S.getEntry());
  134. if (Loop *L = LI.getLoopFor(SplitBlock)) {
  135. L->addBasicBlockToLoop(StartBlock, LI);
  136. L->addBasicBlockToLoop(ExitingBlock, LI);
  137. }
  138. DT.addNewBlock(StartBlock, SplitBlock);
  139. DT.addNewBlock(ExitingBlock, StartBlock);
  140. RI.setRegionFor(StartBlock, RI.getRegionFor(SplitBlock));
  141. RI.setRegionFor(ExitingBlock, RI.getRegionFor(SplitBlock));
  142. // \ / //
  143. // EnteringBB //
  144. // | //
  145. // SplitBlock---------\ //
  146. // _____|_____ | //
  147. // / EntryBB \ StartBlock //
  148. // | (region) | | //
  149. // \_ExitingBB_/ ExitingBlock //
  150. // | //
  151. // MergeBlock //
  152. // | //
  153. // ExitBB //
  154. // / \ //
  155. // Connect start block to exiting block.
  156. Builder.SetInsertPoint(StartBlock);
  157. Builder.CreateBr(ExitingBlock);
  158. DT.changeImmediateDominator(ExitingBlock, StartBlock);
  159. // Connect exiting block to join block.
  160. Builder.SetInsertPoint(ExitingBlock);
  161. Builder.CreateBr(MergeBlock);
  162. DT.changeImmediateDominator(MergeBlock, SplitBlock);
  163. // \ / //
  164. // EnteringBB //
  165. // | //
  166. // SplitBlock---------\ //
  167. // _____|_____ | //
  168. // / EntryBB \ StartBlock //
  169. // | (region) | | //
  170. // \_ExitingBB_/ ExitingBlock //
  171. // | | //
  172. // MergeBlock---------/ //
  173. // | //
  174. // ExitBB //
  175. // / \ //
  176. //
  177. // Split the edge between SplitBlock and EntryBB, to avoid a critical edge.
  178. splitEdge(SplitBlock, EntryBB, ".pre_entry_bb", &DT, &LI, &RI);
  179. // \ / //
  180. // EnteringBB //
  181. // | //
  182. // SplitBlock---------\ //
  183. // | | //
  184. // PreEntryBB | //
  185. // _____|_____ | //
  186. // / EntryBB \ StartBlock //
  187. // | (region) | | //
  188. // \_ExitingBB_/ ExitingBlock //
  189. // | | //
  190. // MergeBlock---------/ //
  191. // | //
  192. // ExitBB //
  193. // / \ //
  194. return std::make_pair(std::make_pair(StartBlock, ExitingBlock), CondBr);
  195. }