dataflow.cpp 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. // Copyright (c) 2021 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/dataflow.h"
  15. #include <algorithm>
  16. #include <cstdint>
  17. namespace spvtools {
  18. namespace opt {
  19. bool DataFlowAnalysis::Enqueue(Instruction* inst) {
  20. bool& is_enqueued = on_worklist_[inst];
  21. if (is_enqueued) return false;
  22. is_enqueued = true;
  23. worklist_.push(inst);
  24. return true;
  25. }
  26. DataFlowAnalysis::VisitResult DataFlowAnalysis::RunOnce(
  27. Function* function, bool is_first_iteration) {
  28. InitializeWorklist(function, is_first_iteration);
  29. VisitResult ret = VisitResult::kResultFixed;
  30. while (!worklist_.empty()) {
  31. Instruction* top = worklist_.front();
  32. worklist_.pop();
  33. on_worklist_[top] = false;
  34. VisitResult result = Visit(top);
  35. if (result == VisitResult::kResultChanged) {
  36. EnqueueSuccessors(top);
  37. ret = VisitResult::kResultChanged;
  38. }
  39. }
  40. return ret;
  41. }
  42. void DataFlowAnalysis::Run(Function* function) {
  43. VisitResult result = RunOnce(function, true);
  44. while (result == VisitResult::kResultChanged) {
  45. result = RunOnce(function, false);
  46. }
  47. }
  48. void ForwardDataFlowAnalysis::InitializeWorklist(Function* function,
  49. bool /*is_first_iteration*/) {
  50. context().cfg()->ForEachBlockInReversePostOrder(
  51. function->entry().get(), [this](BasicBlock* bb) {
  52. if (label_position_ == LabelPosition::kLabelsOnly) {
  53. Enqueue(bb->GetLabelInst());
  54. return;
  55. }
  56. if (label_position_ == LabelPosition::kLabelsAtBeginning) {
  57. Enqueue(bb->GetLabelInst());
  58. }
  59. for (Instruction& inst : *bb) {
  60. Enqueue(&inst);
  61. }
  62. if (label_position_ == LabelPosition::kLabelsAtEnd) {
  63. Enqueue(bb->GetLabelInst());
  64. }
  65. });
  66. }
  67. void ForwardDataFlowAnalysis::EnqueueUsers(Instruction* inst) {
  68. context().get_def_use_mgr()->ForEachUser(
  69. inst, [this](Instruction* user) { Enqueue(user); });
  70. }
  71. void ForwardDataFlowAnalysis::EnqueueBlockSuccessors(Instruction* inst) {
  72. if (inst->opcode() != SpvOpLabel) return;
  73. context()
  74. .cfg()
  75. ->block(inst->result_id())
  76. ->ForEachSuccessorLabel([this](uint32_t* label) {
  77. Enqueue(context().cfg()->block(*label)->GetLabelInst());
  78. });
  79. }
  80. } // namespace opt
  81. } // namespace spvtools