File: structured_loop_to_selection_reduction_opportunity.h

package info (click to toggle)
spirv-tools 2023.1-2
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 25,608 kB
  • sloc: cpp: 408,882; javascript: 5,890; python: 2,847; ansic: 403; sh: 385; ruby: 88; makefile: 17; lisp: 9
file content (93 lines) | stat: -rw-r--r-- 3,970 bytes parent folder | download | duplicates (23)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
// Copyright (c) 2018 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#ifndef SOURCE_REDUCE_STRUCTURED_LOOP_TO_SELECTION_REDUCTION_OPPORTUNITY_H_
#define SOURCE_REDUCE_STRUCTURED_LOOP_TO_SELECTION_REDUCTION_OPPORTUNITY_H_

#include "source/opt/def_use_manager.h"
#include "source/opt/dominator_analysis.h"
#include "source/opt/function.h"
#include "source/reduce/reduction_opportunity.h"

namespace spvtools {
namespace reduce {

// An opportunity to replace a structured loop with a selection.
class StructuredLoopToSelectionReductionOpportunity
    : public ReductionOpportunity {
 public:
  // Constructs an opportunity from a loop header block and the function that
  // encloses it.
  explicit StructuredLoopToSelectionReductionOpportunity(
      opt::IRContext* context, opt::BasicBlock* loop_construct_header)
      : context_(context), loop_construct_header_(loop_construct_header) {}

  // Returns true if the loop header is reachable.  A structured loop might
  // become unreachable as a result of turning another structured loop into
  // a selection.
  bool PreconditionHolds() override;

 protected:
  void Apply() override;

 private:
  // Parameter |original_target_id| is the id of the loop's merge block or
  // continue target.  This method considers each edge of the form
  // b->original_target_id and transforms it into an edge of the form b->c,
  // where c is the merge block of the structured control flow construct that
  // most tightly contains b.
  void RedirectToClosestMergeBlock(uint32_t original_target_id);

  // |source_id|, |original_target_id| and |new_target_id| are required to all
  // be distinct, with a CFG edge existing from |source_id| to
  // |original_target_id|, and |original_target_id| being either the merge block
  // or continue target for the loop being operated on.
  // The method removes this edge and adds an edge from
  // |source_id| to |new_target_id|.  It takes care of fixing up any OpPhi
  // instructions associated with |original_target_id| and |new_target_id|.
  void RedirectEdge(uint32_t source_id, uint32_t original_target_id,
                    uint32_t new_target_id);

  // Adds components to |to_block|'s phi instructions to account for a new
  // incoming edge from |from_id|.
  void AdaptPhiInstructionsForAddedEdge(uint32_t from_id,
                                        opt::BasicBlock* to_block);

  // Turns the OpLoopMerge for the loop into OpSelectionMerge, and adapts the
  // following branch instruction accordingly.
  void ChangeLoopToSelection();

  // Fixes any scenarios where, due to CFG changes, ids have uses not dominated
  // by their definitions, by changing such uses to uses of OpUndef or of
  // placeholder variables.
  void FixNonDominatedIdUses();

  // Returns true if and only if at least one of the following holds:
  // 1) |def| dominates |use|
  // 2) |def| is an OpVariable
  // 3) |use| is part of an OpPhi, with associated incoming block b, and |def|
  // dominates b.
  bool DefinitionSufficientlyDominatesUse(opt::Instruction* def,
                                          opt::Instruction* use,
                                          uint32_t use_index,
                                          opt::BasicBlock& def_block);

  opt::IRContext* context_;
  opt::BasicBlock* loop_construct_header_;
};

}  // namespace reduce
}  // namespace spvtools

#endif  // SOURCE_REDUCE_STRUCTURED_LOOP_TO_SELECTION_REDUCTION_OPPORTUNITY_H_