dataflow.cpp 2.8 KB

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