Coverage Report

Created: 2025-11-16 06:14

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