Coverage Report

Created: 2026-01-17 07:12

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
2.25k
                           bool run_on_non_semantic_insts) {
63
2.25k
  WhileEachInst(
64
370k
      [&f](Instruction* inst) {
65
370k
        f(inst);
66
370k
        return true;
67
370k
      },
68
2.25k
      run_on_debug_line_insts, run_on_non_semantic_insts);
69
2.25k
}
70
71
void Function::ForEachInst(const std::function<void(const Instruction*)>& f,
72
                           bool run_on_debug_line_insts,
73
296
                           bool run_on_non_semantic_insts) const {
74
296
  WhileEachInst(
75
39.6k
      [&f](const Instruction* inst) {
76
39.6k
        f(inst);
77
39.6k
        return true;
78
39.6k
      },
79
296
      run_on_debug_line_insts, run_on_non_semantic_insts);
80
296
}
81
82
bool Function::WhileEachInst(const std::function<bool(Instruction*)>& f,
83
                             bool run_on_debug_line_insts,
84
2.99k
                             bool run_on_non_semantic_insts) {
85
2.99k
  if (def_inst_) {
86
2.99k
    if (!def_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
87
0
      return false;
88
0
    }
89
2.99k
  }
90
91
2.99k
  for (auto& param : params_) {
92
1.71k
    if (!param->WhileEachInst(f, run_on_debug_line_insts)) {
93
0
      return false;
94
0
    }
95
1.71k
  }
96
97
2.99k
  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
45.0k
  for (auto& bb : blocks_) {
107
45.0k
    if (!bb->WhileEachInst(f, run_on_debug_line_insts)) {
108
0
      return false;
109
0
    }
110
45.0k
  }
111
112
2.99k
  if (end_inst_) {
113
2.99k
    if (!end_inst_->WhileEachInst(f, run_on_debug_line_insts)) {
114
0
      return false;
115
0
    }
116
2.99k
  }
117
118
2.99k
  if (run_on_non_semantic_insts) {
119
1.85k
    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
1.85k
  }
125
126
2.99k
  return true;
127
2.99k
}
128
129
bool Function::WhileEachInst(const std::function<bool(const Instruction*)>& f,
130
                             bool run_on_debug_line_insts,
131
296
                             bool run_on_non_semantic_insts) const {
132
296
  if (def_inst_) {
133
296
    if (!static_cast<const Instruction*>(def_inst_.get())
134
296
             ->WhileEachInst(f, run_on_debug_line_insts)) {
135
0
      return false;
136
0
    }
137
296
  }
138
139
296
  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
296
  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
4.83k
  for (const auto& bb : blocks_) {
153
4.83k
    if (!static_cast<const BasicBlock*>(bb.get())->WhileEachInst(
154
4.83k
            f, run_on_debug_line_insts)) {
155
0
      return false;
156
0
    }
157
4.83k
  }
158
159
296
  if (end_inst_) {
160
296
    if (!static_cast<const Instruction*>(end_inst_.get())
161
296
             ->WhileEachInst(f, run_on_debug_line_insts)) {
162
0
      return false;
163
0
    }
164
296
  }
165
166
296
  if (run_on_non_semantic_insts) {
167
256
    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
256
  }
174
175
296
  return true;
176
296
}
177
178
void Function::ForEachParam(const std::function<void(Instruction*)>& f,
179
846
                            bool run_on_debug_line_insts) {
180
846
  for (auto& param : params_)
181
380
    static_cast<Instruction*>(param.get())
182
380
        ->ForEachInst(f, run_on_debug_line_insts);
183
846
}
184
185
void Function::ForEachParam(const std::function<void(const Instruction*)>& f,
186
764
                            bool run_on_debug_line_insts) const {
187
764
  for (const auto& param : params_)
188
0
    static_cast<const Instruction*>(param.get())
189
0
        ->ForEachInst(f, run_on_debug_line_insts);
190
764
}
191
192
void Function::ForEachDebugInstructionsInHeader(
193
718
    const std::function<void(Instruction*)>& f) {
194
718
  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
18
    std::unique_ptr<BasicBlock>&& new_block, BasicBlock* position) {
206
18
  for (auto bb_iter = begin(); bb_iter != end(); ++bb_iter) {
207
18
    if (&*bb_iter == position) {
208
18
      new_block->SetParent(this);
209
18
      ++bb_iter;
210
18
      bb_iter = bb_iter.InsertBefore(std::move(new_block));
211
18
      return &*bb_iter;
212
18
    }
213
18
  }
214
18
  assert(false && "Could not find insertion point.");
215
0
  return nullptr;
216
18
}
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
262
bool Function::IsRecursive() const {
244
262
  IRContext* ctx = blocks_.front()->GetLabel()->context();
245
262
  IRContext::ProcessFunction mark_visited = [this](Function* fp) {
246
200
    return fp == this;
247
200
  };
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
262
  std::queue<uint32_t> roots;
252
262
  ctx->AddCalls(this, &roots);
253
262
  return ctx->ProcessCallTreeFromRoots(mark_visited, &roots);
254
262
}
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
4
void Function::ReorderBasicBlocksInStructuredOrder() {
277
4
  std::list<BasicBlock*> order;
278
4
  IRContext* context = this->def_inst_->context();
279
4
  context->cfg()->ComputeStructuredOrder(this, blocks_[0].get(), &order);
280
4
  ReorderBasicBlocks(order.begin(), order.end());
281
4
}
282
283
}  // namespace opt
284
}  // namespace spvtools