Coverage Report

Created: 2026-09-14 06:31

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/shaderc/third_party/spirv-tools/source/opt/function.cpp
Line
Count
Source
1
// Copyright (c) 2016 Google Inc.
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/function.h"
16
17
#include <ostream>
18
19
#include "ir_context.h"
20
#include "source/util/bit_vector.h"
21
22
namespace spvtools {
23
namespace opt {
24
25
0
Function* Function::Clone(IRContext* ctx) const {
26
0
  Function* clone =
27
0
      new Function(std::unique_ptr<Instruction>(DefInst().Clone(ctx)));
28
0
  clone->params_.reserve(params_.size());
29
0
  ForEachParam(
30
0
      [clone, ctx](const Instruction* inst) {
31
0
        clone->AddParameter(std::unique_ptr<Instruction>(inst->Clone(ctx)));
32
0
      },
33
0
      true);
34
35
0
  for (const auto& i : debug_insts_in_header_) {
36
0
    clone->AddDebugInstructionInHeader(
37
0
        std::unique_ptr<Instruction>(i.Clone(ctx)));
38
0
  }
39
40
0
  clone->blocks_.reserve(blocks_.size());
41
0
  for (const auto& b : blocks_) {
42
0
    std::unique_ptr<BasicBlock> bb(b->Clone(ctx));
43
0
    if (!bb) {
44
0
      delete clone;
45
0
      return nullptr;
46
0
    }
47
0
    clone->AddBasicBlock(std::move(bb));
48
0
  }
49
50
0
  clone->SetFunctionEnd(std::unique_ptr<Instruction>(EndInst()->Clone(ctx)));
51
52
0
  clone->non_semantic_.reserve(non_semantic_.size());
53
0
  for (auto& non_semantic : non_semantic_) {
54
0
    clone->AddNonSemanticInstruction(
55
0
        std::unique_ptr<Instruction>(non_semantic->Clone(ctx)));
56
0
  }
57
0
  return clone;
58
0
}
59
60
void Function::ForEachInst(const std::function<void(Instruction*)>& f,
61
                           bool run_on_debug_line_insts,
62
1.25k
                           bool run_on_non_semantic_insts) {
63
1.25k
  WhileEachInst(
64
270k
      [&f](Instruction* inst) {
65
270k
        f(inst);
66
270k
        return true;
67
270k
      },
68
1.25k
      run_on_debug_line_insts, run_on_non_semantic_insts);
69
1.25k
}
70
71
void Function::ForEachInst(const std::function<void(const Instruction*)>& f,
72
                           bool run_on_debug_line_insts,
73
48
                           bool run_on_non_semantic_insts) const {
74
48
  WhileEachInst(
75
4.63k
      [&f](const Instruction* inst) {
76
4.63k
        f(inst);
77
4.63k
        return true;
78
4.63k
      },
79
48
      run_on_debug_line_insts, run_on_non_semantic_insts);
80
48
}
81
82
bool Function::WhileEachInst(const std::function<bool(Instruction*)>& f,
83
                             bool run_on_debug_line_insts,
84
7.38k
                             bool run_on_non_semantic_insts) {
85
7.38k
  if (def_inst_) {
86
7.38k
    if (!def_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
87
0
      return false;
88
0
    }
89
7.38k
  }
90
91
7.38k
  for (auto& param : params_) {
92
7.26k
    if (!param->WhileEachInst(f, run_on_debug_line_insts)) {
93
0
      return false;
94
0
    }
95
7.26k
  }
96
97
7.38k
  if (!debug_insts_in_header_.empty()) {
98
0
    Instruction* di = &debug_insts_in_header_.front();
99
0
    while (di != nullptr) {
100
0
      Instruction* next_instruction = di->NextNode();
101
0
      if (!di->WhileEachInst(f, run_on_debug_line_insts)) return false;
102
0
      di = next_instruction;
103
0
    }
104
0
  }
105
106
148k
  for (auto& bb : blocks_) {
107
148k
    if (!bb->WhileEachInst(f, run_on_debug_line_insts)) {
108
0
      return false;
109
0
    }
110
148k
  }
111
112
7.38k
  if (end_inst_) {
113
7.38k
    if (!end_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
114
0
      return false;
115
0
    }
116
7.38k
  }
117
118
7.38k
  if (run_on_non_semantic_insts) {
119
5.13k
    for (auto& non_semantic : non_semantic_) {
120
0
      if (!non_semantic->WhileEachInst(f, run_on_debug_line_insts)) {
121
0
        return false;
122
0
      }
123
0
    }
124
5.13k
  }
125
126
7.38k
  return true;
127
7.38k
}
128
129
bool Function::WhileEachInst(const std::function<bool(const Instruction*)>& f,
130
                             bool run_on_debug_line_insts,
131
544
                             bool run_on_non_semantic_insts) const {
132
544
  if (def_inst_) {
133
544
    if (!static_cast<const Instruction*>(def_inst_.get())
134
544
             ->WhileEachInst(f, run_on_debug_line_insts)) {
135
0
      return false;
136
0
    }
137
544
  }
138
139
544
  for (const auto& param : params_) {
140
0
    if (!static_cast<const Instruction*>(param.get())
141
0
             ->WhileEachInst(f, run_on_debug_line_insts)) {
142
0
      return false;
143
0
    }
144
0
  }
145
146
544
  for (const auto& di : debug_insts_in_header_) {
147
0
    if (!static_cast<const Instruction*>(&di)->WhileEachInst(
148
0
            f, run_on_debug_line_insts))
149
0
      return false;
150
0
  }
151
152
13.6k
  for (const auto& bb : blocks_) {
153
13.6k
    if (!static_cast<const BasicBlock*>(bb.get())->WhileEachInst(
154
13.6k
            f, run_on_debug_line_insts)) {
155
0
      return false;
156
0
    }
157
13.6k
  }
158
159
544
  if (end_inst_) {
160
544
    if (!static_cast<const Instruction*>(end_inst_.get())
161
544
             ->WhileEachInst(f, run_on_debug_line_insts)) {
162
0
      return false;
163
0
    }
164
544
  }
165
166
544
  if (run_on_non_semantic_insts) {
167
496
    for (auto& non_semantic : non_semantic_) {
168
0
      if (!static_cast<const Instruction*>(non_semantic.get())
169
0
               ->WhileEachInst(f, run_on_debug_line_insts)) {
170
0
        return false;
171
0
      }
172
0
    }
173
496
  }
174
175
544
  return true;
176
544
}
177
178
void Function::ForEachParam(const std::function<void(Instruction*)>& f,
179
1.54k
                            bool run_on_debug_line_insts) {
180
1.54k
  for (auto& param : params_)
181
1.60k
    static_cast<Instruction*>(param.get())
182
1.60k
        ->ForEachInst(f, run_on_debug_line_insts);
183
1.54k
}
184
185
void Function::ForEachParam(const std::function<void(const Instruction*)>& f,
186
1.66k
                            bool run_on_debug_line_insts) const {
187
1.66k
  for (const auto& param : params_)
188
0
    static_cast<const Instruction*>(param.get())
189
0
        ->ForEachInst(f, run_on_debug_line_insts);
190
1.66k
}
191
192
void Function::ForEachDebugInstructionsInHeader(
193
1.29k
    const std::function<void(Instruction*)>& f) {
194
1.29k
  if (debug_insts_in_header_.empty()) return;
195
196
0
  Instruction* di = &debug_insts_in_header_.front();
197
0
  while (di != nullptr) {
198
0
    Instruction* next_instruction = di->NextNode();
199
0
    di->ForEachInst(f);
200
0
    di = next_instruction;
201
0
  }
202
0
}
203
204
BasicBlock* Function::InsertBasicBlockAfter(
205
112
    std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
206
112
  for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
207
112
    if (&*bb_iter == position) {
208
112
      new_block->SetParent(this);
209
112
      ++bb_iter;
210
112
      bb_iter = bb_iter.InsertBefore(std::move(new_block));
211
112
      return &*bb_iter;
212
112
    }
213
112
  }
214
112
  assert(false && "Could not find insertion point.");
215
0
  return nullptr;
216
112
}
217
218
BasicBlock* Function::InsertBasicBlockBefore(
219
0
    std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
220
0
  for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
221
0
    if (&*bb_iter == position) {
222
0
      new_block->SetParent(this);
223
0
      bb_iter = bb_iter.InsertBefore(std::move(new_block));
224
0
      return &*bb_iter;
225
0
    }
226
0
  }
227
0
  assert(false && "Could not find insertion point.");
228
0
  return nullptr;
229
0
}
230
231
0
bool Function::HasEarlyReturn() const {
232
0
  auto post_dominator_analysis =
233
0
      blocks_.front()->GetLabel()->context()->GetPostDominatorAnalysis(this);
234
0
  for (auto& block : blocks_) {
235
0
    if (spvOpcodeIsReturn(block->tail()->opcode()) &&
236
0
        !post_dominator_analysis->Dominates(block.get(), entry().get())) {
237
0
      return true;
238
0
    }
239
0
  }
240
0
  return false;
241
0
}
242
243
652
bool Function::IsRecursive() const {
244
652
  IRContext* ctx = blocks_.front()->GetLabel()->context();
245
706
  IRContext::ProcessFunction mark_visited = [this](Function* fp) {
246
706
    return fp == this;
247
706
  };
248
249
  // Process the call tree from all of the function called by |this|.  If it get
250
  // back to |this|, then we have a recursive function.
251
652
  std::queue<uint32_t> roots;
252
652
  ctx->AddCalls(this, &roots);
253
652
  return ctx->ProcessCallTreeFromRoots(mark_visited, &roots);
254
652
}
255
256
0
std::ostream& operator<<(std::ostream& str, const Function& func) {
257
0
  str << func.PrettyPrint();
258
0
  return str;
259
0
}
260
261
0
void Function::Dump() const {
262
0
  std::cerr << "Function #" << result_id() << "\n" << *this << "\n";
263
0
}
264
265
0
std::string Function::PrettyPrint(uint32_t options) const {
266
0
  std::ostringstream str;
267
0
  ForEachInst([&str, options](const Instruction* inst) {
268
0
    str << inst->PrettyPrint(options);
269
0
    if (inst->opcode() != spv::Op::OpFunctionEnd) {
270
0
      str << std::endl;
271
0
    }
272
0
  });
273
0
  return str.str();
274
0
}
275
276
8
void Function::ReorderBasicBlocksInStructuredOrder() {
277
8
  std::list<BasicBlock*> order;
278
8
  IRContext* context = this->def_inst_->context();
279
8
  context->cfg()->ComputeStructuredOrder(this, blocks_[0].get(), &order);
280
8
  ReorderBasicBlocks(order.begin(), order.end());
281
8
}
282
283
}  // namespace opt
284
}  // namespace spvtools