licm_pass.cpp 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. // Copyright (c) 2018 Google LLC.
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. #include "source/opt/licm_pass.h"
  15. #include <queue>
  16. #include <utility>
  17. #include "source/opt/module.h"
  18. #include "source/opt/pass.h"
  19. namespace spvtools {
  20. namespace opt {
  21. Pass::Status LICMPass::Process() { return ProcessIRContext(); }
  22. Pass::Status LICMPass::ProcessIRContext() {
  23. Status status = Status::SuccessWithoutChange;
  24. Module* module = get_module();
  25. // Process each function in the module
  26. for (auto func = module->begin();
  27. func != module->end() && status != Status::Failure; ++func) {
  28. status = CombineStatus(status, ProcessFunction(&*func));
  29. }
  30. return status;
  31. }
  32. Pass::Status LICMPass::ProcessFunction(Function* f) {
  33. Status status = Status::SuccessWithoutChange;
  34. LoopDescriptor* loop_descriptor = context()->GetLoopDescriptor(f);
  35. // Process each loop in the function
  36. for (auto it = loop_descriptor->begin();
  37. it != loop_descriptor->end() && status != Status::Failure; ++it) {
  38. Loop& loop = *it;
  39. // Ignore nested loops, as we will process them in order in ProcessLoop
  40. if (loop.IsNested()) {
  41. continue;
  42. }
  43. status = CombineStatus(status, ProcessLoop(&loop, f));
  44. }
  45. return status;
  46. }
  47. Pass::Status LICMPass::ProcessLoop(Loop* loop, Function* f) {
  48. Status status = Status::SuccessWithoutChange;
  49. // Process all nested loops first
  50. for (auto nl = loop->begin(); nl != loop->end() && status != Status::Failure;
  51. ++nl) {
  52. Loop* nested_loop = *nl;
  53. status = CombineStatus(status, ProcessLoop(nested_loop, f));
  54. }
  55. std::vector<BasicBlock*> loop_bbs{};
  56. status = CombineStatus(
  57. status,
  58. AnalyseAndHoistFromBB(loop, f, loop->GetHeaderBlock(), &loop_bbs));
  59. for (size_t i = 0; i < loop_bbs.size() && status != Status::Failure; ++i) {
  60. BasicBlock* bb = loop_bbs[i];
  61. // do not delete the element
  62. status =
  63. CombineStatus(status, AnalyseAndHoistFromBB(loop, f, bb, &loop_bbs));
  64. }
  65. return status;
  66. }
  67. Pass::Status LICMPass::AnalyseAndHoistFromBB(
  68. Loop* loop, Function* f, BasicBlock* bb,
  69. std::vector<BasicBlock*>* loop_bbs) {
  70. bool modified = false;
  71. std::function<bool(Instruction*)> hoist_inst =
  72. [this, &loop, &modified](Instruction* inst) {
  73. if (loop->ShouldHoistInstruction(this->context(), inst)) {
  74. if (!HoistInstruction(loop, inst)) {
  75. return false;
  76. }
  77. modified = true;
  78. }
  79. return true;
  80. };
  81. if (IsImmediatelyContainedInLoop(loop, f, bb)) {
  82. if (!bb->WhileEachInst(hoist_inst, false)) {
  83. return Status::Failure;
  84. }
  85. }
  86. DominatorAnalysis* dom_analysis = context()->GetDominatorAnalysis(f);
  87. DominatorTree& dom_tree = dom_analysis->GetDomTree();
  88. for (DominatorTreeNode* child_dom_tree_node : *dom_tree.GetTreeNode(bb)) {
  89. if (loop->IsInsideLoop(child_dom_tree_node->bb_)) {
  90. loop_bbs->push_back(child_dom_tree_node->bb_);
  91. }
  92. }
  93. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  94. }
  95. bool LICMPass::IsImmediatelyContainedInLoop(Loop* loop, Function* f,
  96. BasicBlock* bb) {
  97. LoopDescriptor* loop_descriptor = context()->GetLoopDescriptor(f);
  98. return loop == (*loop_descriptor)[bb->id()];
  99. }
  100. bool LICMPass::HoistInstruction(Loop* loop, Instruction* inst) {
  101. // TODO(1841): Handle failure to create pre-header.
  102. BasicBlock* pre_header_bb = loop->GetOrCreatePreHeaderBlock();
  103. if (!pre_header_bb) {
  104. return false;
  105. }
  106. Instruction* insertion_point = &*pre_header_bb->tail();
  107. Instruction* previous_node = insertion_point->PreviousNode();
  108. if (previous_node && (previous_node->opcode() == SpvOpLoopMerge ||
  109. previous_node->opcode() == SpvOpSelectionMerge)) {
  110. insertion_point = previous_node;
  111. }
  112. inst->InsertBefore(insertion_point);
  113. context()->set_instr_block(inst, pre_header_bb);
  114. return true;
  115. }
  116. } // namespace opt
  117. } // namespace spvtools