BasicBlockUtils.h 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315
  1. //===-- Transform/Utils/BasicBlockUtils.h - BasicBlock Utils ----*- C++ -*-===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is distributed under the University of Illinois Open Source
  6. // License. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. //
  10. // This family of functions perform manipulations on basic blocks, and
  11. // instructions contained within basic blocks.
  12. //
  13. //===----------------------------------------------------------------------===//
  14. #ifndef LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
  15. #define LLVM_TRANSFORMS_UTILS_BASICBLOCKUTILS_H
  16. // FIXME: Move to this file: BasicBlock::removePredecessor, BB::splitBasicBlock
  17. #include "llvm/IR/BasicBlock.h"
  18. #include "llvm/IR/CFG.h"
  19. namespace llvm {
  20. class AliasAnalysis;
  21. class MemoryDependenceAnalysis;
  22. class DominatorTree;
  23. class LoopInfo;
  24. class Instruction;
  25. class MDNode;
  26. class ReturnInst;
  27. class TargetLibraryInfo;
  28. class TerminatorInst;
  29. /// DeleteDeadBlock - Delete the specified block, which must have no
  30. /// predecessors.
  31. void DeleteDeadBlock(BasicBlock *BB);
  32. /// FoldSingleEntryPHINodes - We know that BB has one predecessor. If there are
  33. /// any single-entry PHI nodes in it, fold them away. This handles the case
  34. /// when all entries to the PHI nodes in a block are guaranteed equal, such as
  35. /// when the block has exactly one predecessor.
  36. void FoldSingleEntryPHINodes(BasicBlock *BB, AliasAnalysis *AA = nullptr,
  37. MemoryDependenceAnalysis *MemDep = nullptr);
  38. /// DeleteDeadPHIs - Examine each PHI in the given block and delete it if it
  39. /// is dead. Also recursively delete any operands that become dead as
  40. /// a result. This includes tracing the def-use list from the PHI to see if
  41. /// it is ultimately unused or if it reaches an unused cycle. Return true
  42. /// if any PHIs were deleted.
  43. bool DeleteDeadPHIs(BasicBlock *BB, const TargetLibraryInfo *TLI = nullptr);
  44. /// MergeBlockIntoPredecessor - Attempts to merge a block into its predecessor,
  45. /// if possible. The return value indicates success or failure.
  46. bool MergeBlockIntoPredecessor(BasicBlock *BB, DominatorTree *DT = nullptr,
  47. LoopInfo *LI = nullptr,
  48. AliasAnalysis *AA = nullptr,
  49. MemoryDependenceAnalysis *MemDep = nullptr);
  50. // ReplaceInstWithValue - Replace all uses of an instruction (specified by BI)
  51. // with a value, then remove and delete the original instruction.
  52. //
  53. void ReplaceInstWithValue(BasicBlock::InstListType &BIL,
  54. BasicBlock::iterator &BI, Value *V);
  55. // ReplaceInstWithInst - Replace the instruction specified by BI with the
  56. // instruction specified by I. Copies DebugLoc from BI to I, if I doesn't
  57. // already have a DebugLoc. The original instruction is deleted and BI is
  58. // updated to point to the new instruction.
  59. //
  60. void ReplaceInstWithInst(BasicBlock::InstListType &BIL,
  61. BasicBlock::iterator &BI, Instruction *I);
  62. // ReplaceInstWithInst - Replace the instruction specified by From with the
  63. // instruction specified by To. Copies DebugLoc from BI to I, if I doesn't
  64. // already have a DebugLoc.
  65. //
  66. void ReplaceInstWithInst(Instruction *From, Instruction *To);
  67. /// \brief Option class for critical edge splitting.
  68. ///
  69. /// This provides a builder interface for overriding the default options used
  70. /// during critical edge splitting.
  71. struct CriticalEdgeSplittingOptions {
  72. AliasAnalysis *AA;
  73. DominatorTree *DT;
  74. LoopInfo *LI;
  75. bool MergeIdenticalEdges;
  76. bool DontDeleteUselessPHIs;
  77. bool PreserveLCSSA;
  78. CriticalEdgeSplittingOptions()
  79. : AA(nullptr), DT(nullptr), LI(nullptr), MergeIdenticalEdges(false),
  80. DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
  81. /// \brief Basic case of setting up all the analysis.
  82. CriticalEdgeSplittingOptions(AliasAnalysis *AA, DominatorTree *DT = nullptr,
  83. LoopInfo *LI = nullptr)
  84. : AA(AA), DT(DT), LI(LI), MergeIdenticalEdges(false),
  85. DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
  86. /// \brief A common pattern is to preserve the dominator tree and loop
  87. /// info but not care about AA.
  88. CriticalEdgeSplittingOptions(DominatorTree *DT, LoopInfo *LI)
  89. : AA(nullptr), DT(DT), LI(LI), MergeIdenticalEdges(false),
  90. DontDeleteUselessPHIs(false), PreserveLCSSA(false) {}
  91. CriticalEdgeSplittingOptions &setMergeIdenticalEdges() {
  92. MergeIdenticalEdges = true;
  93. return *this;
  94. }
  95. CriticalEdgeSplittingOptions &setDontDeleteUselessPHIs() {
  96. DontDeleteUselessPHIs = true;
  97. return *this;
  98. }
  99. CriticalEdgeSplittingOptions &setPreserveLCSSA() {
  100. PreserveLCSSA = true;
  101. return *this;
  102. }
  103. };
  104. /// SplitCriticalEdge - If this edge is a critical edge, insert a new node to
  105. /// split the critical edge. This will update the analyses passed in through
  106. /// the option struct. This returns the new block if the edge was split, null
  107. /// otherwise.
  108. ///
  109. /// If MergeIdenticalEdges in the options struct is true (not the default),
  110. /// *all* edges from TI to the specified successor will be merged into the same
  111. /// critical edge block. This is most commonly interesting with switch
  112. /// instructions, which may have many edges to any one destination. This
  113. /// ensures that all edges to that dest go to one block instead of each going
  114. /// to a different block, but isn't the standard definition of a "critical
  115. /// edge".
  116. ///
  117. /// It is invalid to call this function on a critical edge that starts at an
  118. /// IndirectBrInst. Splitting these edges will almost always create an invalid
  119. /// program because the address of the new block won't be the one that is jumped
  120. /// to.
  121. ///
  122. BasicBlock *SplitCriticalEdge(TerminatorInst *TI, unsigned SuccNum,
  123. const CriticalEdgeSplittingOptions &Options =
  124. CriticalEdgeSplittingOptions());
  125. inline BasicBlock *
  126. SplitCriticalEdge(BasicBlock *BB, succ_iterator SI,
  127. const CriticalEdgeSplittingOptions &Options =
  128. CriticalEdgeSplittingOptions()) {
  129. return SplitCriticalEdge(BB->getTerminator(), SI.getSuccessorIndex(),
  130. Options);
  131. }
  132. /// SplitCriticalEdge - If the edge from *PI to BB is not critical, return
  133. /// false. Otherwise, split all edges between the two blocks and return true.
  134. /// This updates all of the same analyses as the other SplitCriticalEdge
  135. /// function. If P is specified, it updates the analyses
  136. /// described above.
  137. inline bool SplitCriticalEdge(BasicBlock *Succ, pred_iterator PI,
  138. const CriticalEdgeSplittingOptions &Options =
  139. CriticalEdgeSplittingOptions()) {
  140. bool MadeChange = false;
  141. TerminatorInst *TI = (*PI)->getTerminator();
  142. for (unsigned i = 0, e = TI->getNumSuccessors(); i != e; ++i)
  143. if (TI->getSuccessor(i) == Succ)
  144. MadeChange |= !!SplitCriticalEdge(TI, i, Options);
  145. return MadeChange;
  146. }
  147. /// SplitCriticalEdge - If an edge from Src to Dst is critical, split the edge
  148. /// and return true, otherwise return false. This method requires that there be
  149. /// an edge between the two blocks. It updates the analyses
  150. /// passed in the options struct
  151. inline BasicBlock *
  152. SplitCriticalEdge(BasicBlock *Src, BasicBlock *Dst,
  153. const CriticalEdgeSplittingOptions &Options =
  154. CriticalEdgeSplittingOptions()) {
  155. TerminatorInst *TI = Src->getTerminator();
  156. unsigned i = 0;
  157. while (1) {
  158. assert(i != TI->getNumSuccessors() && "Edge doesn't exist!");
  159. if (TI->getSuccessor(i) == Dst)
  160. return SplitCriticalEdge(TI, i, Options);
  161. ++i;
  162. }
  163. }
  164. // SplitAllCriticalEdges - Loop over all of the edges in the CFG,
  165. // breaking critical edges as they are found.
  166. // Returns the number of broken edges.
  167. unsigned SplitAllCriticalEdges(Function &F,
  168. const CriticalEdgeSplittingOptions &Options =
  169. CriticalEdgeSplittingOptions());
  170. /// SplitEdge - Split the edge connecting specified block.
  171. BasicBlock *SplitEdge(BasicBlock *From, BasicBlock *To,
  172. DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
  173. /// SplitBlock - Split the specified block at the specified instruction - every
  174. /// thing before SplitPt stays in Old and everything starting with SplitPt moves
  175. /// to a new block. The two blocks are joined by an unconditional branch and
  176. /// the loop info is updated.
  177. ///
  178. BasicBlock *SplitBlock(BasicBlock *Old, Instruction *SplitPt,
  179. DominatorTree *DT = nullptr, LoopInfo *LI = nullptr);
  180. /// SplitBlockPredecessors - This method introduces at least one new basic block
  181. /// into the function and moves some of the predecessors of BB to be
  182. /// predecessors of the new block. The new predecessors are indicated by the
  183. /// Preds array. The new block is given a suffix of 'Suffix'. Returns new basic
  184. /// block to which predecessors from Preds are now pointing.
  185. ///
  186. /// If BB is a landingpad block then additional basicblock might be introduced.
  187. /// It will have Suffix+".split_lp". See SplitLandingPadPredecessors for more
  188. /// details on this case.
  189. ///
  190. /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
  191. /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses.
  192. /// In particular, it does not preserve LoopSimplify (because it's
  193. /// complicated to handle the case where one of the edges being split
  194. /// is an exit of a loop with other exits).
  195. ///
  196. BasicBlock *SplitBlockPredecessors(BasicBlock *BB, ArrayRef<BasicBlock *> Preds,
  197. const char *Suffix,
  198. AliasAnalysis *AA = nullptr,
  199. DominatorTree *DT = nullptr,
  200. LoopInfo *LI = nullptr,
  201. bool PreserveLCSSA = false);
  202. /// SplitLandingPadPredecessors - This method transforms the landing pad,
  203. /// OrigBB, by introducing two new basic blocks into the function. One of those
  204. /// new basic blocks gets the predecessors listed in Preds. The other basic
  205. /// block gets the remaining predecessors of OrigBB. The landingpad instruction
  206. /// OrigBB is clone into both of the new basic blocks. The new blocks are given
  207. /// the suffixes 'Suffix1' and 'Suffix2', and are returned in the NewBBs vector.
  208. ///
  209. /// This currently updates the LLVM IR, AliasAnalysis, DominatorTree,
  210. /// DominanceFrontier, LoopInfo, and LCCSA but no other analyses. In particular,
  211. /// it does not preserve LoopSimplify (because it's complicated to handle the
  212. /// case where one of the edges being split is an exit of a loop with other
  213. /// exits).
  214. ///
  215. void SplitLandingPadPredecessors(BasicBlock *OrigBB,
  216. ArrayRef<BasicBlock *> Preds,
  217. const char *Suffix, const char *Suffix2,
  218. SmallVectorImpl<BasicBlock *> &NewBBs,
  219. AliasAnalysis *AA = nullptr,
  220. DominatorTree *DT = nullptr,
  221. LoopInfo *LI = nullptr,
  222. bool PreserveLCSSA = false);
  223. /// FoldReturnIntoUncondBranch - This method duplicates the specified return
  224. /// instruction into a predecessor which ends in an unconditional branch. If
  225. /// the return instruction returns a value defined by a PHI, propagate the
  226. /// right value into the return. It returns the new return instruction in the
  227. /// predecessor.
  228. ReturnInst *FoldReturnIntoUncondBranch(ReturnInst *RI, BasicBlock *BB,
  229. BasicBlock *Pred);
  230. /// SplitBlockAndInsertIfThen - Split the containing block at the
  231. /// specified instruction - everything before and including SplitBefore stays
  232. /// in the old basic block, and everything after SplitBefore is moved to a
  233. /// new block. The two blocks are connected by a conditional branch
  234. /// (with value of Cmp being the condition).
  235. /// Before:
  236. /// Head
  237. /// SplitBefore
  238. /// Tail
  239. /// After:
  240. /// Head
  241. /// if (Cond)
  242. /// ThenBlock
  243. /// SplitBefore
  244. /// Tail
  245. ///
  246. /// If Unreachable is true, then ThenBlock ends with
  247. /// UnreachableInst, otherwise it branches to Tail.
  248. /// Returns the NewBasicBlock's terminator.
  249. ///
  250. /// Updates DT if given.
  251. TerminatorInst *SplitBlockAndInsertIfThen(Value *Cond, Instruction *SplitBefore,
  252. bool Unreachable,
  253. MDNode *BranchWeights = nullptr,
  254. DominatorTree *DT = nullptr);
  255. /// SplitBlockAndInsertIfThenElse is similar to SplitBlockAndInsertIfThen,
  256. /// but also creates the ElseBlock.
  257. /// Before:
  258. /// Head
  259. /// SplitBefore
  260. /// Tail
  261. /// After:
  262. /// Head
  263. /// if (Cond)
  264. /// ThenBlock
  265. /// else
  266. /// ElseBlock
  267. /// SplitBefore
  268. /// Tail
  269. void SplitBlockAndInsertIfThenElse(Value *Cond, Instruction *SplitBefore,
  270. TerminatorInst **ThenTerm,
  271. TerminatorInst **ElseTerm,
  272. MDNode *BranchWeights = nullptr);
  273. ///
  274. /// GetIfCondition - Check whether BB is the merge point of a if-region.
  275. /// If so, return the boolean condition that determines which entry into
  276. /// BB will be taken. Also, return by references the block that will be
  277. /// entered from if the condition is true, and the block that will be
  278. /// entered if the condition is false.
  279. Value *GetIfCondition(BasicBlock *BB, BasicBlock *&IfTrue,
  280. BasicBlock *&IfFalse);
  281. } // End llvm namespace
  282. #endif