dead_branch_elim_pass.h 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. //
  5. // Licensed under the Apache License, Version 2.0 (the "License");
  6. // you may not use this file except in compliance with the License.
  7. // You may obtain a copy of the License at
  8. //
  9. // http://www.apache.org/licenses/LICENSE-2.0
  10. //
  11. // Unless required by applicable law or agreed to in writing, software
  12. // distributed under the License is distributed on an "AS IS" BASIS,
  13. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14. // See the License for the specific language governing permissions and
  15. // limitations under the License.
  16. #ifndef SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_
  17. #define SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_
  18. #include <algorithm>
  19. #include <map>
  20. #include <queue>
  21. #include <unordered_map>
  22. #include <unordered_set>
  23. #include <utility>
  24. #include <vector>
  25. #include "source/opt/basic_block.h"
  26. #include "source/opt/def_use_manager.h"
  27. #include "source/opt/mem_pass.h"
  28. #include "source/opt/module.h"
  29. namespace spvtools {
  30. namespace opt {
  31. // See optimizer.hpp for documentation.
  32. class DeadBranchElimPass : public MemPass {
  33. using cbb_ptr = const BasicBlock*;
  34. public:
  35. DeadBranchElimPass() = default;
  36. const char* name() const override { return "eliminate-dead-branches"; }
  37. Status Process() override;
  38. IRContext::Analysis GetPreservedAnalyses() override {
  39. return IRContext::kAnalysisDefUse |
  40. IRContext::kAnalysisInstrToBlockMapping |
  41. IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
  42. }
  43. private:
  44. // If |condId| is boolean constant, return conditional value in |condVal| and
  45. // return true, otherwise return false.
  46. bool GetConstCondition(uint32_t condId, bool* condVal);
  47. // If |valId| is a 32-bit integer constant, return value via |value| and
  48. // return true, otherwise return false.
  49. bool GetConstInteger(uint32_t valId, uint32_t* value);
  50. // Add branch to |labelId| to end of block |bp|.
  51. void AddBranch(uint32_t labelId, BasicBlock* bp);
  52. // For function |func|, look for BranchConditionals with constant condition
  53. // and convert to a Branch to the indicated label. Delete resulting dead
  54. // blocks. Note some such branches and blocks may be left to avoid creating
  55. // invalid control flow.
  56. // TODO(greg-lunarg): Remove remaining constant conditional branches and dead
  57. // blocks.
  58. bool EliminateDeadBranches(Function* func);
  59. // Returns the basic block containing |id|.
  60. // Note: this pass only requires correct instruction block mappings for the
  61. // input. This pass does not preserve the block mapping, so it is not kept
  62. // up-to-date during processing.
  63. BasicBlock* GetParentBlock(uint32_t id);
  64. // Marks live blocks reachable from the entry of |func|. Simplifies constant
  65. // branches and switches as it proceeds, to limit the number of live blocks.
  66. // It is careful not to eliminate backedges even if they are dead, but the
  67. // header is live. Likewise, unreachable merge blocks named in live merge
  68. // instruction must be retained (though they may be clobbered).
  69. bool MarkLiveBlocks(Function* func,
  70. std::unordered_set<BasicBlock*>* live_blocks);
  71. // Checks for unreachable merge and continue blocks with live headers; those
  72. // blocks must be retained. Continues are tracked separately so that a live
  73. // phi can be updated to take an undef value from any of its predecessors
  74. // that are unreachable continues.
  75. //
  76. // |unreachable_continues| maps the id of an unreachable continue target to
  77. // the header block that declares it.
  78. void MarkUnreachableStructuredTargets(
  79. const std::unordered_set<BasicBlock*>& live_blocks,
  80. std::unordered_set<BasicBlock*>* unreachable_merges,
  81. std::unordered_map<BasicBlock*, BasicBlock*>* unreachable_continues);
  82. // Fix phis in reachable blocks so that only live (or unremovable) incoming
  83. // edges are present. If the block now only has a single live incoming edge,
  84. // remove the phi and replace its uses with its data input. If the single
  85. // remaining incoming edge is from the phi itself, the phi is in an
  86. // unreachable single block loop. Either the block is dead and will be
  87. // removed, or it's reachable from an unreachable continue target. In the
  88. // latter case that continue target block will be collapsed into a block that
  89. // only branches back to its header and we'll eliminate the block with the
  90. // phi.
  91. //
  92. // |unreachable_continues| maps continue targets that cannot be reached to
  93. // merge instruction that declares them.
  94. bool FixPhiNodesInLiveBlocks(
  95. Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
  96. const std::unordered_map<BasicBlock*, BasicBlock*>&
  97. unreachable_continues);
  98. // Erases dead blocks. Any block captured in |unreachable_merges| or
  99. // |unreachable_continues| is a dead block that is required to remain due to
  100. // a live merge instruction in the corresponding header. These blocks will
  101. // have their instructions clobbered and will become a label and terminator.
  102. // Unreachable merge blocks are terminated by OpUnreachable, while
  103. // unreachable continue blocks are terminated by an unconditional branch to
  104. // the header. Otherwise, blocks are dead if not explicitly captured in
  105. // |live_blocks| and are totally removed.
  106. //
  107. // |unreachable_continues| maps continue targets that cannot be reached to
  108. // corresponding header block that declares them.
  109. bool EraseDeadBlocks(
  110. Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
  111. const std::unordered_set<BasicBlock*>& unreachable_merges,
  112. const std::unordered_map<BasicBlock*, BasicBlock*>&
  113. unreachable_continues);
  114. // Reorders blocks in reachable functions so that they satisfy dominator
  115. // block ordering rules.
  116. void FixBlockOrder();
  117. // Return the first branch instruction that is a conditional branch to
  118. // |merge_block_id|. Returns |nullptr| if no such branch exists. If there are
  119. // multiple such branches, the first one is the one that would be executed
  120. // first when running the code. That is, the one that dominates all of the
  121. // others.
  122. //
  123. // |start_block_id| must be a block whose innermost containing merge construct
  124. // has |merge_block_id| as the merge block.
  125. //
  126. // |loop_merge_id| and |loop_continue_id| are the merge and continue block ids
  127. // of the innermost loop containing |start_block_id|.
  128. Instruction* FindFirstExitFromSelectionMerge(uint32_t start_block_id,
  129. uint32_t merge_block_id,
  130. uint32_t loop_merge_id,
  131. uint32_t loop_continue_id,
  132. uint32_t switch_merge_id);
  133. // Adds to |blocks_with_back_edges| all of the blocks on the path from the
  134. // basic block |cont_id| to |header_id| and |merge_id|. The intention is that
  135. // |cond_id| is a the continue target of a loop, |header_id| is the header of
  136. // the loop, and |merge_id| is the merge block of the loop.
  137. void AddBlocksWithBackEdge(
  138. uint32_t cont_id, uint32_t header_id, uint32_t merge_id,
  139. std::unordered_set<BasicBlock*>* blocks_with_back_edges);
  140. // Returns true if there is a branch to the merge node of the selection
  141. // construct |switch_header_id| that is inside a nested selection construct or
  142. // in the header of the nested selection construct.
  143. bool SwitchHasNestedBreak(uint32_t switch_header_id);
  144. // Return true of the terminator of |block| is successfully replaced with a
  145. // branch to |live_lab_id|. The merge instruction is deleted or moved as
  146. // needed to maintain structured control flow. Assumes that the
  147. // StructuredCFGAnalysis is valid for the constructs containing |block|.
  148. bool SimplifyBranch(BasicBlock* block, uint32_t live_lab_id);
  149. };
  150. } // namespace opt
  151. } // namespace spvtools
  152. #endif // SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_