block_merge_util.cpp 6.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196
  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. // Returns true if |id| is the continue target of a merge instruction.
  45. bool IsContinue(IRContext* context, uint32_t id) {
  46. return !context->get_def_use_mgr()->WhileEachUse(
  47. id, [](Instruction* user, uint32_t index) {
  48. SpvOp op = user->opcode();
  49. if (op == SpvOpLoopMerge && index == 1u) {
  50. return false;
  51. }
  52. return true;
  53. });
  54. }
  55. // Removes any OpPhi instructions in |block|, which should have exactly one
  56. // predecessor, replacing uses of OpPhi ids with the ids associated with the
  57. // predecessor.
  58. void EliminateOpPhiInstructions(IRContext* context, BasicBlock* block) {
  59. block->ForEachPhiInst([context](Instruction* phi) {
  60. assert(2 == phi->NumInOperands() &&
  61. "A block can only have one predecessor for block merging to make "
  62. "sense.");
  63. context->ReplaceAllUsesWith(phi->result_id(),
  64. phi->GetSingleWordInOperand(0));
  65. context->KillInst(phi);
  66. });
  67. }
  68. } // Anonymous namespace
  69. bool CanMergeWithSuccessor(IRContext* context, BasicBlock* block) {
  70. // Find block with single successor which has no other predecessors.
  71. auto ii = block->end();
  72. --ii;
  73. Instruction* br = &*ii;
  74. if (br->opcode() != SpvOpBranch) {
  75. return false;
  76. }
  77. const uint32_t lab_id = br->GetSingleWordInOperand(0);
  78. if (context->cfg()->preds(lab_id).size() != 1) {
  79. return false;
  80. }
  81. bool pred_is_merge = IsMerge(context, block);
  82. bool succ_is_merge = IsMerge(context, lab_id);
  83. if (pred_is_merge && succ_is_merge) {
  84. // Cannot merge two merges together.
  85. return false;
  86. }
  87. if (pred_is_merge && IsContinue(context, lab_id)) {
  88. // Cannot merge a continue target with a merge block.
  89. return false;
  90. }
  91. Instruction* merge_inst = block->GetMergeInst();
  92. const bool pred_is_header = IsHeader(block);
  93. if (pred_is_header && lab_id != merge_inst->GetSingleWordInOperand(0u)) {
  94. bool succ_is_header = IsHeader(context, lab_id);
  95. if (pred_is_header && succ_is_header) {
  96. // Cannot merge two headers together when the successor is not the merge
  97. // block of the predecessor.
  98. return false;
  99. }
  100. // If this is a header block and the successor is not its merge, we must
  101. // be careful about which blocks we are willing to merge together.
  102. // OpLoopMerge must be followed by a conditional or unconditional branch.
  103. // The merge must be a loop merge because a selection merge cannot be
  104. // followed by an unconditional branch.
  105. BasicBlock* succ_block = context->get_instr_block(lab_id);
  106. SpvOp succ_term_op = succ_block->terminator()->opcode();
  107. assert(merge_inst->opcode() == SpvOpLoopMerge);
  108. if (succ_term_op != SpvOpBranch && succ_term_op != SpvOpBranchConditional) {
  109. return false;
  110. }
  111. }
  112. return true;
  113. }
  114. void MergeWithSuccessor(IRContext* context, Function* func,
  115. Function::iterator bi) {
  116. assert(CanMergeWithSuccessor(context, &*bi) &&
  117. "Precondition failure for MergeWithSuccessor: it must be legal to "
  118. "merge the block and its successor.");
  119. auto ii = bi->end();
  120. --ii;
  121. Instruction* br = &*ii;
  122. const uint32_t lab_id = br->GetSingleWordInOperand(0);
  123. Instruction* merge_inst = bi->GetMergeInst();
  124. bool pred_is_header = IsHeader(&*bi);
  125. // Merge blocks.
  126. context->KillInst(br);
  127. auto sbi = bi;
  128. for (; sbi != func->end(); ++sbi)
  129. if (sbi->id() == lab_id) break;
  130. // If bi is sbi's only predecessor, it dominates sbi and thus
  131. // sbi must follow bi in func's ordering.
  132. assert(sbi != func->end());
  133. // Update the inst-to-block mapping for the instructions in sbi.
  134. for (auto& inst : *sbi) {
  135. context->set_instr_block(&inst, &*bi);
  136. }
  137. EliminateOpPhiInstructions(context, &*sbi);
  138. // Now actually move the instructions.
  139. bi->AddInstructions(&*sbi);
  140. if (merge_inst) {
  141. if (pred_is_header && lab_id == merge_inst->GetSingleWordInOperand(0u)) {
  142. // Merging the header and merge blocks, so remove the structured control
  143. // flow declaration.
  144. context->KillInst(merge_inst);
  145. } else {
  146. // Move OpLine/OpNoLine information to merge_inst. This solves
  147. // the validation error that OpLine is placed between OpLoopMerge
  148. // and OpBranchConditional.
  149. auto terminator = bi->terminator();
  150. auto& vec = terminator->dbg_line_insts();
  151. if (vec.size() > 0) {
  152. merge_inst->ClearDbgLineInsts();
  153. auto& new_vec = merge_inst->dbg_line_insts();
  154. new_vec.insert(new_vec.end(), vec.begin(), vec.end());
  155. terminator->ClearDbgLineInsts();
  156. for (auto& l_inst : new_vec)
  157. context->get_def_use_mgr()->AnalyzeInstDefUse(&l_inst);
  158. }
  159. // Clear debug scope of terminator to avoid DebugScope
  160. // emitted between terminator and merge.
  161. terminator->SetDebugScope(DebugScope(kNoDebugScope, kNoInlinedAt));
  162. // Move the merge instruction to just before the terminator.
  163. merge_inst->InsertBefore(terminator);
  164. }
  165. }
  166. context->ReplaceAllUsesWith(lab_id, bi->id());
  167. context->KillInst(sbi->GetLabelInst());
  168. (void)sbi.Erase();
  169. }
  170. } // namespace blockmergeutil
  171. } // namespace opt
  172. } // namespace spvtools