licm_pass.cpp 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139
  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 "source/opt/module.h"
  17. #include "source/opt/pass.h"
  18. namespace spvtools {
  19. namespace opt {
  20. Pass::Status LICMPass::Process() { return ProcessIRContext(); }
  21. Pass::Status LICMPass::ProcessIRContext() {
  22. Status status = Status::SuccessWithoutChange;
  23. Module* module = get_module();
  24. // Process each function in the module
  25. for (auto func = module->begin();
  26. func != module->end() && status != Status::Failure; ++func) {
  27. status = CombineStatus(status, ProcessFunction(&*func));
  28. }
  29. return status;
  30. }
  31. Pass::Status LICMPass::ProcessFunction(Function* f) {
  32. Status status = Status::SuccessWithoutChange;
  33. LoopDescriptor* loop_descriptor = context()->GetLoopDescriptor(f);
  34. // Process each loop in the function
  35. for (auto it = loop_descriptor->begin();
  36. it != loop_descriptor->end() && status != Status::Failure; ++it) {
  37. Loop& loop = *it;
  38. // Ignore nested loops, as we will process them in order in ProcessLoop
  39. if (loop.IsNested()) {
  40. continue;
  41. }
  42. status = CombineStatus(status, ProcessLoop(&loop, f));
  43. }
  44. return status;
  45. }
  46. Pass::Status LICMPass::ProcessLoop(Loop* loop, Function* f) {
  47. Status status = Status::SuccessWithoutChange;
  48. // Process all nested loops first
  49. for (auto nl = loop->begin(); nl != loop->end() && status != Status::Failure;
  50. ++nl) {
  51. Loop* nested_loop = *nl;
  52. status = CombineStatus(status, ProcessLoop(nested_loop, f));
  53. }
  54. std::vector<BasicBlock*> loop_bbs{};
  55. status = CombineStatus(
  56. status,
  57. AnalyseAndHoistFromBB(loop, f, loop->GetHeaderBlock(), &loop_bbs));
  58. for (size_t i = 0; i < loop_bbs.size() && status != Status::Failure; ++i) {
  59. BasicBlock* bb = loop_bbs[i];
  60. // do not delete the element
  61. status =
  62. CombineStatus(status, AnalyseAndHoistFromBB(loop, f, bb, &loop_bbs));
  63. }
  64. return status;
  65. }
  66. Pass::Status LICMPass::AnalyseAndHoistFromBB(
  67. Loop* loop, Function* f, BasicBlock* bb,
  68. std::vector<BasicBlock*>* loop_bbs) {
  69. bool modified = false;
  70. std::function<bool(Instruction*)> hoist_inst =
  71. [this, &loop, &modified](Instruction* inst) {
  72. if (loop->ShouldHoistInstruction(*inst)) {
  73. if (!HoistInstruction(loop, inst)) {
  74. return false;
  75. }
  76. modified = true;
  77. }
  78. return true;
  79. };
  80. if (IsImmediatelyContainedInLoop(loop, f, bb)) {
  81. if (!bb->WhileEachInst(hoist_inst, false)) {
  82. return Status::Failure;
  83. }
  84. }
  85. DominatorAnalysis* dom_analysis = context()->GetDominatorAnalysis(f);
  86. DominatorTree& dom_tree = dom_analysis->GetDomTree();
  87. for (DominatorTreeNode* child_dom_tree_node : *dom_tree.GetTreeNode(bb)) {
  88. if (loop->IsInsideLoop(child_dom_tree_node->bb_)) {
  89. loop_bbs->push_back(child_dom_tree_node->bb_);
  90. }
  91. }
  92. return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange);
  93. }
  94. bool LICMPass::IsImmediatelyContainedInLoop(Loop* loop, Function* f,
  95. BasicBlock* bb) {
  96. LoopDescriptor* loop_descriptor = context()->GetLoopDescriptor(f);
  97. return loop == (*loop_descriptor)[bb->id()];
  98. }
  99. bool LICMPass::HoistInstruction(Loop* loop, Instruction* inst) {
  100. // TODO(1841): Handle failure to create pre-header.
  101. BasicBlock* pre_header_bb = loop->GetOrCreatePreHeaderBlock();
  102. if (!pre_header_bb) {
  103. return false;
  104. }
  105. Instruction* insertion_point = &*pre_header_bb->tail();
  106. Instruction* previous_node = insertion_point->PreviousNode();
  107. if (previous_node && (previous_node->opcode() == spv::Op::OpLoopMerge ||
  108. previous_node->opcode() == spv::Op::OpSelectionMerge)) {
  109. insertion_point = previous_node;
  110. }
  111. inst->InsertBefore(insertion_point);
  112. context()->set_instr_block(inst, pre_header_bb);
  113. return true;
  114. }
  115. } // namespace opt
  116. } // namespace spvtools