dataflow.h 5.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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. #ifndef SOURCE_OPT_DATAFLOW_H_
  15. #define SOURCE_OPT_DATAFLOW_H_
  16. #include <queue>
  17. #include <unordered_map>
  18. #include <vector>
  19. #include "source/opt/instruction.h"
  20. #include "source/opt/ir_context.h"
  21. namespace spvtools {
  22. namespace opt {
  23. // Generic data-flow analysis.
  24. // Maintains a worklist of instructions to process and processes them in a
  25. // specified order. See also ForwardDataFlowAnalysis, which is specialized for
  26. // forward data-flow analysis.
  27. class DataFlowAnalysis {
  28. public:
  29. // The result of a |Visit| operation on an instruction.
  30. // This is used to determine when analysis has reached a fixpoint.
  31. enum class VisitResult {
  32. // The analysis result for this instruction has changed.
  33. // This means that any instructions that depend on it (its successors) must
  34. // be recomputed.
  35. kResultChanged,
  36. // The analysis result for this instruction has not changed.
  37. // When all visit operations return |kResultFixed|, the analysis has reached
  38. // a fixpoint (converged).
  39. kResultFixed,
  40. };
  41. virtual ~DataFlowAnalysis() {}
  42. // Run this analysis on a given function.
  43. // For analyses which work interprocedurally, |function| may be ignored.
  44. void Run(Function* function);
  45. protected:
  46. DataFlowAnalysis(IRContext& context) : context_(context) {}
  47. // Initialize the worklist for a given function.
  48. // |is_first_iteration| is true on the first call to |Run| and false
  49. // afterwards. All subsequent runs are only necessary to check if the analysis
  50. // has converged; if |EnqueueSuccessors| is complete, |InitializeWorklist|
  51. // should do nothing after the first iteration.
  52. virtual void InitializeWorklist(Function* function,
  53. bool is_first_iteration) = 0;
  54. // Enqueues the successors (instructions which use the analysis result) of
  55. // |inst|. This is not required to be complete, but convergence is faster when
  56. // it is. This is called whenever |Visit| returns |kResultChanged|.
  57. virtual void EnqueueSuccessors(Instruction* inst) = 0;
  58. // Visits the given instruction, recomputing the analysis result. This is
  59. // called once per instruction queued in |InitializeWorklist| and afterward
  60. // when a predecessor is changed, through |EnqueueSuccessors|.
  61. virtual VisitResult Visit(Instruction* inst) = 0;
  62. // Enqueues the given instruction to be visited. Ignored if already in the
  63. // worklist.
  64. bool Enqueue(Instruction* inst);
  65. IRContext& context() { return context_; }
  66. private:
  67. // Runs one pass, calling |InitializeWorklist| and then iterating through the
  68. // worklist until all fixed.
  69. VisitResult RunOnce(Function* function, bool is_first_iteration);
  70. IRContext& context_;
  71. std::unordered_map<Instruction*, bool> on_worklist_;
  72. // The worklist, which contains the list of instructions to be visited.
  73. //
  74. // The choice of data structure was influenced by the data in "Iterative
  75. // Data-flow Analysis, Revisited" (Cooper et al, 2002).
  76. // https://citeseerx.ist.psu.edu/viewdoc/download?doi=10.1.1.125.1549&rep=rep1&type=pdf
  77. // The paper shows that the overall performance benefit of a priority queue
  78. // over a regular queue or stack is relatively small (or negative).
  79. //
  80. // A queue has the advantage that nodes are visited in the same order they are
  81. // enqueued, which relieves the analysis from inserting nodes "backwards", for
  82. // example in worklist initialization. Also, as the paper claims that sorting
  83. // successors does not improve runtime, we can use a single queue which is
  84. // modified during iteration.
  85. std::queue<Instruction*> worklist_;
  86. };
  87. // A generic data flow analysis, specialized for forward analysis.
  88. class ForwardDataFlowAnalysis : public DataFlowAnalysis {
  89. public:
  90. // Indicates where labels should be in the worklist RPO ordering.
  91. enum class LabelPosition {
  92. // Labels should be placed at the beginning of their blocks.
  93. kLabelsAtBeginning,
  94. // Labels should be placed at the end of their blocks.
  95. kLabelsAtEnd,
  96. // Labels should not be in the worklist.
  97. kNoLabels,
  98. // Only labels should be placed in the worklist.
  99. kLabelsOnly,
  100. };
  101. ForwardDataFlowAnalysis(IRContext& context, LabelPosition label_position)
  102. : DataFlowAnalysis(context), label_position_(label_position) {}
  103. protected:
  104. // Initializes the worklist in reverse postorder, regardless of
  105. // |is_first_iteration|. Labels are placed according to the label position
  106. // specified in the constructor.
  107. void InitializeWorklist(Function* function, bool is_first_iteration) override;
  108. // Enqueues the users and block successors of the given instruction.
  109. // See |EnqueueUsers| and |EnqueueBlockSuccessors|.
  110. void EnqueueSuccessors(Instruction* inst) override {
  111. EnqueueUsers(inst);
  112. EnqueueBlockSuccessors(inst);
  113. }
  114. // Enqueues the users of the given instruction.
  115. void EnqueueUsers(Instruction* inst);
  116. // Enqueues the labels of the successors of the block corresponding to the
  117. // given label instruction. Does nothing for other instructions.
  118. void EnqueueBlockSuccessors(Instruction* inst);
  119. private:
  120. LabelPosition label_position_;
  121. };
  122. } // namespace opt
  123. } // namespace spvtools
  124. #endif // SOURCE_OPT_DATAFLOW_H_