Coverage Report

Created: 2026-06-08 06:54

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/spirv-tools/source/opt/if_conversion.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/if_conversion.h"
16
17
#include <memory>
18
#include <vector>
19
20
#include "source/opt/value_number_table.h"
21
22
namespace spvtools {
23
namespace opt {
24
25
11.3k
Pass::Status IfConversion::Process() {
26
11.3k
  if (!context()->get_feature_mgr()->HasCapability(spv::Capability::Shader)) {
27
70
    return Status::SuccessWithoutChange;
28
70
  }
29
30
11.3k
  const ValueNumberTable& vn_table = *context()->GetValueNumberTable();
31
11.3k
  bool modified = false;
32
11.3k
  std::vector<Instruction*> to_kill;
33
11.3k
  for (auto& func : *get_module()) {
34
10.8k
    DominatorAnalysis* dominators = context()->GetDominatorAnalysis(&func);
35
830k
    for (auto& block : func) {
36
      // Check if it is possible for |block| to have phis that can be
37
      // transformed.
38
830k
      BasicBlock* common = nullptr;
39
830k
      if (!CheckBlock(&block, dominators, &common)) continue;
40
41
      // Get an insertion point.
42
38.2k
      auto iter = block.begin();
43
64.7k
      while (iter != block.end() && iter->opcode() == spv::Op::OpPhi) {
44
26.5k
        ++iter;
45
26.5k
      }
46
47
38.2k
      InstructionBuilder builder(
48
38.2k
          context(), &*iter,
49
38.2k
          IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping);
50
38.2k
      block.ForEachPhiInst([this, &builder, &modified, &common, &to_kill,
51
38.2k
                            dominators, &block, &vn_table](Instruction* phi) {
52
        // This phi is not compatible, but subsequent phis might be.
53
26.5k
        if (!CheckType(phi->type_id())) return;
54
55
        // We cannot transform cases where the phi is used by another phi in the
56
        // same block due to instruction ordering restrictions.
57
        // TODO(alan-baker): If all inappropriate uses could also be
58
        // transformed, we could still remove this phi.
59
26.4k
        if (!CheckPhiUsers(phi, &block)) return;
60
61
        // Identify the incoming values associated with the true and false
62
        // branches. If |then_block| dominates |inc0| or if the true edge
63
        // branches straight to this block and |common| is |inc0|, then |inc0|
64
        // is on the true branch. Otherwise the |inc1| is on the true branch.
65
26.4k
        BasicBlock* inc0 = GetIncomingBlock(phi, 0u);
66
26.4k
        Instruction* branch = common->terminator();
67
26.4k
        uint32_t condition = branch->GetSingleWordInOperand(0u);
68
26.4k
        BasicBlock* then_block = GetBlock(branch->GetSingleWordInOperand(1u));
69
26.4k
        Instruction* true_value = nullptr;
70
26.4k
        Instruction* false_value = nullptr;
71
26.4k
        if ((then_block == &block && inc0 == common) ||
72
26.1k
            dominators->Dominates(then_block, inc0)) {
73
6.33k
          true_value = GetIncomingValue(phi, 0u);
74
6.33k
          false_value = GetIncomingValue(phi, 1u);
75
20.1k
        } else {
76
20.1k
          true_value = GetIncomingValue(phi, 1u);
77
20.1k
          false_value = GetIncomingValue(phi, 0u);
78
20.1k
        }
79
80
26.4k
        BasicBlock* true_def_block = context()->get_instr_block(true_value);
81
26.4k
        BasicBlock* false_def_block = context()->get_instr_block(false_value);
82
83
26.4k
        uint32_t true_vn = vn_table.GetValueNumber(true_value);
84
26.4k
        uint32_t false_vn = vn_table.GetValueNumber(false_value);
85
26.4k
        if (true_vn != 0 && true_vn == false_vn) {
86
576
          Instruction* inst_to_use = nullptr;
87
88
          // Try to pick an instruction that is not in a side node.  If we can't
89
          // pick either the true for false branch as long as they can be
90
          // legally moved.
91
576
          if (!true_def_block ||
92
434
              dominators->Dominates(true_def_block, &block)) {
93
142
            inst_to_use = true_value;
94
434
          } else if (!false_def_block ||
95
434
                     dominators->Dominates(false_def_block, &block)) {
96
371
            inst_to_use = false_value;
97
371
          } else if (CanHoistInstruction(true_value, common, dominators)) {
98
42
            inst_to_use = true_value;
99
42
          } else if (CanHoistInstruction(false_value, common, dominators)) {
100
0
            inst_to_use = false_value;
101
0
          }
102
103
576
          if (inst_to_use != nullptr) {
104
555
            modified = true;
105
555
            HoistInstruction(inst_to_use, common, dominators);
106
555
            context()->KillNamesAndDecorates(phi);
107
555
            context()->ReplaceAllUsesWith(phi->result_id(),
108
555
                                          inst_to_use->result_id());
109
555
          }
110
576
          return;
111
576
        }
112
113
        // If either incoming value is defined in a block that does not dominate
114
        // this phi, then we cannot eliminate the phi with a select.
115
        // TODO(alan-baker): Perform code motion where it makes sense to enable
116
        // the transform in this case.
117
25.8k
        if (true_def_block && !dominators->Dominates(true_def_block, &block))
118
14.5k
          return;
119
120
11.2k
        if (false_def_block && !dominators->Dominates(false_def_block, &block))
121
1.39k
          return;
122
123
9.87k
        analysis::Type* data_ty =
124
9.87k
            context()->get_type_mgr()->GetType(true_value->type_id());
125
9.87k
        if (analysis::Vector* vec_data_ty = data_ty->AsVector()) {
126
1.30k
          condition = SplatCondition(vec_data_ty, condition, &builder);
127
1.30k
        }
128
129
        // TODO(1841): Handle id overflow.
130
9.87k
        Instruction* select = builder.AddSelect(phi->type_id(), condition,
131
9.87k
                                                true_value->result_id(),
132
9.87k
                                                false_value->result_id());
133
9.87k
        context()->get_def_use_mgr()->AnalyzeInstDefUse(select);
134
9.87k
        select->UpdateDebugInfoFrom(phi);
135
9.87k
        context()->ReplaceAllUsesWith(phi->result_id(), select->result_id());
136
9.87k
        to_kill.push_back(phi);
137
9.87k
        modified = true;
138
139
9.87k
        return;
140
11.2k
      });
141
38.2k
    }
142
10.8k
  }
143
144
11.3k
  for (auto inst : to_kill) {
145
9.87k
    context()->KillInst(inst);
146
9.87k
  }
147
148
11.3k
  return modified ? Status::SuccessWithChange : Status::SuccessWithoutChange;
149
11.3k
}
150
151
bool IfConversion::CheckBlock(BasicBlock* block, DominatorAnalysis* dominators,
152
830k
                              BasicBlock** common) {
153
830k
  const std::vector<uint32_t>& preds = cfg()->preds(block->id());
154
155
  // TODO(alan-baker): Extend to more than two predecessors
156
830k
  if (preds.size() != 2) return false;
157
158
84.7k
  BasicBlock* inc0 = context()->get_instr_block(preds[0]);
159
84.7k
  if (dominators->Dominates(block, inc0)) return false;
160
161
84.7k
  BasicBlock* inc1 = context()->get_instr_block(preds[1]);
162
84.7k
  if (dominators->Dominates(block, inc1)) return false;
163
164
70.0k
  if (inc0 == inc1) {
165
    // If the predecessor blocks are the same, then there is only 1 value for
166
    // the OpPhi.  Other transformation should be able to simplify that.
167
3.83k
    return false;
168
3.83k
  }
169
  // All phis will have the same common dominator, so cache the result
170
  // for this block. If there is no common dominator, then we cannot transform
171
  // any phi in this basic block.
172
66.1k
  *common = dominators->CommonDominator(inc0, inc1);
173
66.1k
  if (!*common || cfg()->IsPseudoEntryBlock(*common)) return false;
174
60.6k
  Instruction* branch = (*common)->terminator();
175
60.6k
  if (branch->opcode() != spv::Op::OpBranchConditional) return false;
176
60.2k
  auto merge = (*common)->GetMergeInst();
177
60.2k
  if (!merge || merge->opcode() != spv::Op::OpSelectionMerge) return false;
178
54.9k
  if (spv::SelectionControlMask(merge->GetSingleWordInOperand(1)) ==
179
54.9k
      spv::SelectionControlMask::DontFlatten) {
180
4.61k
    return false;
181
4.61k
  }
182
50.3k
  if ((*common)->MergeBlockIdIfAny() != block->id()) return false;
183
184
38.2k
  return true;
185
50.3k
}
186
187
26.4k
bool IfConversion::CheckPhiUsers(Instruction* phi, BasicBlock* block) {
188
26.4k
  return get_def_use_mgr()->WhileEachUser(
189
56.1k
      phi, [block, this](Instruction* user) {
190
56.1k
        if (user->opcode() == spv::Op::OpPhi &&
191
13.8k
            context()->get_instr_block(user) == block)
192
0
          return false;
193
56.1k
        return true;
194
56.1k
      });
195
26.4k
}
196
197
uint32_t IfConversion::SplatCondition(analysis::Vector* vec_data_ty,
198
                                      uint32_t cond,
199
1.30k
                                      InstructionBuilder* builder) {
200
  // If the data inputs to OpSelect are vectors, the condition for
201
  // OpSelect must be a boolean vector with the same number of
202
  // components. So splat the condition for the branch into a vector
203
  // type.
204
1.30k
  analysis::Bool bool_ty;
205
1.30k
  analysis::Vector bool_vec_ty(&bool_ty, vec_data_ty->element_count());
206
1.30k
  uint32_t bool_vec_id =
207
1.30k
      context()->get_type_mgr()->GetTypeInstruction(&bool_vec_ty);
208
1.30k
  std::vector<uint32_t> ids(vec_data_ty->element_count(), cond);
209
  // TODO(1841): Handle id overflow.
210
1.30k
  return builder->AddCompositeConstruct(bool_vec_id, ids)->result_id();
211
1.30k
}
212
213
26.5k
bool IfConversion::CheckType(uint32_t id) {
214
26.5k
  Instruction* type = get_def_use_mgr()->GetDef(id);
215
26.5k
  spv::Op op = type->opcode();
216
26.5k
  if (spvOpcodeIsScalarType(op) || op == spv::Op::OpTypePointer ||
217
10.7k
      op == spv::Op::OpTypeVector)
218
26.4k
    return true;
219
57
  return false;
220
26.5k
}
221
222
52.8k
BasicBlock* IfConversion::GetBlock(uint32_t id) {
223
52.8k
  return context()->get_instr_block(get_def_use_mgr()->GetDef(id));
224
52.8k
}
225
226
BasicBlock* IfConversion::GetIncomingBlock(Instruction* phi,
227
26.4k
                                           uint32_t predecessor) {
228
26.4k
  uint32_t in_index = 2 * predecessor + 1;
229
26.4k
  return GetBlock(phi->GetSingleWordInOperand(in_index));
230
26.4k
}
231
232
Instruction* IfConversion::GetIncomingValue(Instruction* phi,
233
52.8k
                                            uint32_t predecessor) {
234
52.8k
  uint32_t in_index = 2 * predecessor;
235
52.8k
  return get_def_use_mgr()->GetDef(phi->GetSingleWordInOperand(in_index));
236
52.8k
}
237
238
void IfConversion::HoistInstruction(Instruction* inst, BasicBlock* target_block,
239
653
                                    DominatorAnalysis* dominators) {
240
653
  BasicBlock* inst_block = context()->get_instr_block(inst);
241
653
  if (!inst_block) {
242
    // This is in the header, and dominates everything.
243
189
    return;
244
189
  }
245
246
464
  if (dominators->Dominates(inst_block, target_block)) {
247
    // Already in position.  No work to do.
248
411
    return;
249
411
  }
250
251
464
  assert(inst->IsOpcodeCodeMotionSafe() &&
252
53
         "Trying to move an instruction that is not safe to move.");
253
254
  // First hoist all instructions it depends on.
255
53
  analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
256
53
  inst->ForEachInId(
257
98
      [this, target_block, def_use_mgr, dominators](uint32_t* id) {
258
98
        Instruction* operand_inst = def_use_mgr->GetDef(*id);
259
98
        HoistInstruction(operand_inst, target_block, dominators);
260
98
      });
261
262
53
  Instruction* insertion_pos = target_block->terminator();
263
53
  if ((insertion_pos)->PreviousNode()->opcode() == spv::Op::OpSelectionMerge) {
264
53
    insertion_pos = insertion_pos->PreviousNode();
265
53
  }
266
53
  inst->RemoveFromList();
267
53
  insertion_pos->InsertBefore(std::unique_ptr<Instruction>(inst));
268
53
  context()->set_instr_block(inst, target_block);
269
53
}
270
271
bool IfConversion::CanHoistInstruction(Instruction* inst,
272
                                       BasicBlock* target_block,
273
210
                                       DominatorAnalysis* dominators) {
274
210
  BasicBlock* inst_block = context()->get_instr_block(inst);
275
210
  if (!inst_block) {
276
    // This is in the header, and dominates everything.
277
61
    return true;
278
61
  }
279
280
149
  if (dominators->Dominates(inst_block, target_block)) {
281
    // Already in position.  No work to do.
282
40
    return true;
283
40
  }
284
285
109
  if (!inst->IsOpcodeCodeMotionSafe()) {
286
42
    return false;
287
42
  }
288
289
  // Check all instruction |inst| depends on.
290
67
  analysis::DefUseManager* def_use_mgr = context()->get_def_use_mgr();
291
67
  return inst->WhileEachInId(
292
126
      [this, target_block, def_use_mgr, dominators](uint32_t* id) {
293
126
        Instruction* operand_inst = def_use_mgr->GetDef(*id);
294
126
        return CanHoistInstruction(operand_inst, target_block, dominators);
295
126
      });
296
109
}
297
298
}  // namespace opt
299
}  // namespace spvtools