block_merge_util.cpp 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168
  1. // Copyright (c) 2017 The Khronos Group Inc.
  2. // Copyright (c) 2017 Valve Corporation
  3. // Copyright (c) 2017 LunarG Inc.
  4. // Copyright (c) 2019 Google LLC
  5. //
  6. // Licensed under the Apache License, Version 2.0 (the "License");
  7. // you may not use this file except in compliance with the License.
  8. // You may obtain a copy of the License at
  9. //
  10. // http://www.apache.org/licenses/LICENSE-2.0
  11. //
  12. // Unless required by applicable law or agreed to in writing, software
  13. // distributed under the License is distributed on an "AS IS" BASIS,
  14. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  15. // See the License for the specific language governing permissions and
  16. // limitations under the License.
  17. #include "block_merge_util.h"
  18. namespace spvtools {
  19. namespace opt {
  20. namespace blockmergeutil {
  21. namespace {
  22. // Returns true if |block| contains a merge instruction.
  23. bool IsHeader(BasicBlock* block) { return block->GetMergeInst() != nullptr; }
  24. // Returns true if |id| contains a merge instruction.
  25. bool IsHeader(IRContext* context, uint32_t id) {
  26. return IsHeader(
  27. context->get_instr_block(context->get_def_use_mgr()->GetDef(id)));
  28. }
  29. // Returns true if |id| is the merge target of a merge instruction.
  30. bool IsMerge(IRContext* context, uint32_t id) {
  31. return !context->get_def_use_mgr()->WhileEachUse(id, [](Instruction* user,
  32. uint32_t index) {
  33. SpvOp op = user->opcode();
  34. if ((op == SpvOpLoopMerge || op == SpvOpSelectionMerge) && index == 0u) {
  35. return false;
  36. }
  37. return true;
  38. });
  39. }
  40. // Returns true if |block| is the merge target of a merge instruction.
  41. bool IsMerge(IRContext* context, BasicBlock* block) {
  42. return IsMerge(context, block->id());
  43. }
  44. // Removes any OpPhi instructions in |block|, which should have exactly one
  45. // predecessor, replacing uses of OpPhi ids with the ids associated with the
  46. // predecessor.
  47. void EliminateOpPhiInstructions(IRContext* context, BasicBlock* block) {
  48. block->ForEachPhiInst([context](Instruction* phi) {
  49. assert(2 == phi->NumInOperands() &&
  50. "A block can only have one predecessor for block merging to make "
  51. "sense.");
  52. context->ReplaceAllUsesWith(phi->result_id(),
  53. phi->GetSingleWordInOperand(0));
  54. context->KillInst(phi);
  55. });
  56. }
  57. } // Anonymous namespace
  58. bool CanMergeWithSuccessor(IRContext* context, BasicBlock* block) {
  59. // Find block with single successor which has no other predecessors.
  60. auto ii = block->end();
  61. --ii;
  62. Instruction* br = &*ii;
  63. if (br->opcode() != SpvOpBranch) {
  64. return false;
  65. }
  66. const uint32_t lab_id = br->GetSingleWordInOperand(0);
  67. if (context->cfg()->preds(lab_id).size() != 1) {
  68. return false;
  69. }
  70. bool pred_is_merge = IsMerge(context, block);
  71. bool succ_is_merge = IsMerge(context, lab_id);
  72. if (pred_is_merge && succ_is_merge) {
  73. // Cannot merge two merges together.
  74. return false;
  75. }
  76. // Don't bother trying to merge unreachable blocks.
  77. if (auto dominators = context->GetDominatorAnalysis(block->GetParent())) {
  78. if (!dominators->IsReachable(block)) return false;
  79. }
  80. Instruction* merge_inst = block->GetMergeInst();
  81. const bool pred_is_header = IsHeader(block);
  82. if (pred_is_header && lab_id != merge_inst->GetSingleWordInOperand(0u)) {
  83. bool succ_is_header = IsHeader(context, lab_id);
  84. if (pred_is_header && succ_is_header) {
  85. // Cannot merge two headers together when the successor is not the merge
  86. // block of the predecessor.
  87. return false;
  88. }
  89. // If this is a header block and the successor is not its merge, we must
  90. // be careful about which blocks we are willing to merge together.
  91. // OpLoopMerge must be followed by a conditional or unconditional branch.
  92. // The merge must be a loop merge because a selection merge cannot be
  93. // followed by an unconditional branch.
  94. BasicBlock* succ_block = context->get_instr_block(lab_id);
  95. SpvOp succ_term_op = succ_block->terminator()->opcode();
  96. assert(merge_inst->opcode() == SpvOpLoopMerge);
  97. if (succ_term_op != SpvOpBranch && succ_term_op != SpvOpBranchConditional) {
  98. return false;
  99. }
  100. }
  101. return true;
  102. }
  103. void MergeWithSuccessor(IRContext* context, Function* func,
  104. Function::iterator bi) {
  105. assert(CanMergeWithSuccessor(context, &*bi) &&
  106. "Precondition failure for MergeWithSuccessor: it must be legal to "
  107. "merge the block and its successor.");
  108. auto ii = bi->end();
  109. --ii;
  110. Instruction* br = &*ii;
  111. const uint32_t lab_id = br->GetSingleWordInOperand(0);
  112. Instruction* merge_inst = bi->GetMergeInst();
  113. bool pred_is_header = IsHeader(&*bi);
  114. // Merge blocks.
  115. context->KillInst(br);
  116. auto sbi = bi;
  117. for (; sbi != func->end(); ++sbi)
  118. if (sbi->id() == lab_id) break;
  119. // If bi is sbi's only predecessor, it dominates sbi and thus
  120. // sbi must follow bi in func's ordering.
  121. assert(sbi != func->end());
  122. // Update the inst-to-block mapping for the instructions in sbi.
  123. for (auto& inst : *sbi) {
  124. context->set_instr_block(&inst, &*bi);
  125. }
  126. EliminateOpPhiInstructions(context, &*sbi);
  127. // Now actually move the instructions.
  128. bi->AddInstructions(&*sbi);
  129. if (merge_inst) {
  130. if (pred_is_header && lab_id == merge_inst->GetSingleWordInOperand(0u)) {
  131. // Merging the header and merge blocks, so remove the structured control
  132. // flow declaration.
  133. context->KillInst(merge_inst);
  134. } else {
  135. // Move the merge instruction to just before the terminator.
  136. merge_inst->InsertBefore(bi->terminator());
  137. }
  138. }
  139. context->ReplaceAllUsesWith(lab_id, bi->id());
  140. context->KillInst(sbi->GetLabelInst());
  141. (void)sbi.Erase();
  142. }
  143. } // namespace blockmergeutil
  144. } // namespace opt
  145. } // namespace spvtools