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 // This file implements the SSA rewriting algorithm proposed in
16 //
17 // Simple and Efficient Construction of Static Single Assignment Form.
18 // Braun M., Buchwald S., Hack S., Leißa R., Mallon C., Zwinkau A. (2013)
19 // In: Jhala R., De Bosschere K. (eds)
20 // Compiler Construction. CC 2013.
21 // Lecture Notes in Computer Science, vol 7791.
22 // Springer, Berlin, Heidelberg
23 //
24 // https://link.springer.com/chapter/10.1007/978-3-642-37051-9_6
25 //
26 // In contrast to common eager algorithms based on dominance and dominance
27 // frontier information, this algorithm works backwards from load operations.
28 //
29 // When a target variable is loaded, it queries the variable's reaching
30 // definition. If the reaching definition is unknown at the current location,
31 // it searches backwards in the CFG, inserting Phi instructions at join points
32 // in the CFG along the way until it finds the desired store instruction.
33 //
34 // The algorithm avoids repeated lookups using memoization.
35 //
36 // For reducible CFGs, which are a superset of the structured CFGs in SPIRV,
37 // this algorithm is proven to produce minimal SSA. That is, it inserts the
38 // minimal number of Phi instructions required to ensure the SSA property, but
39 // some Phi instructions may be dead
40 // (https://en.wikipedia.org/wiki/Static_single_assignment_form).
41
42 #include "source/opt/ssa_rewrite_pass.h"
43
44 #include <memory>
45 #include <sstream>
46
47 #include "source/opcode.h"
48 #include "source/opt/cfg.h"
49 #include "source/opt/mem_pass.h"
50 #include "source/util/make_unique.h"
51
52 // Debug logging (0: Off, 1-N: Verbosity level). Replace this with the
53 // implementation done for
54 // https://github.com/KhronosGroup/SPIRV-Tools/issues/1351
55 // #define SSA_REWRITE_DEBUGGING_LEVEL 3
56
57 #ifdef SSA_REWRITE_DEBUGGING_LEVEL
58 #include <ostream>
59 #else
60 #define SSA_REWRITE_DEBUGGING_LEVEL 0
61 #endif
62
63 namespace spvtools {
64 namespace opt {
65
66 namespace {
67 const uint32_t kStoreValIdInIdx = 1;
68 const uint32_t kVariableInitIdInIdx = 1;
69 } // namespace
70
PrettyPrint(const CFG * cfg) const71 std::string SSARewriter::PhiCandidate::PrettyPrint(const CFG* cfg) const {
72 std::ostringstream str;
73 str << "%" << result_id_ << " = Phi[%" << var_id_ << ", BB %" << bb_->id()
74 << "](";
75 if (phi_args_.size() > 0) {
76 uint32_t arg_ix = 0;
77 for (uint32_t pred_label : cfg->preds(bb_->id())) {
78 uint32_t arg_id = phi_args_[arg_ix++];
79 str << "[%" << arg_id << ", bb(%" << pred_label << ")] ";
80 }
81 }
82 str << ")";
83 if (copy_of_ != 0) {
84 str << " [COPY OF " << copy_of_ << "]";
85 }
86 str << ((is_complete_) ? " [COMPLETE]" : " [INCOMPLETE]");
87
88 return str.str();
89 }
90
CreatePhiCandidate(uint32_t var_id,BasicBlock * bb)91 SSARewriter::PhiCandidate& SSARewriter::CreatePhiCandidate(uint32_t var_id,
92 BasicBlock* bb) {
93 // TODO(1841): Handle id overflow.
94 uint32_t phi_result_id = pass_->context()->TakeNextId();
95 auto result = phi_candidates_.emplace(
96 phi_result_id, PhiCandidate(var_id, phi_result_id, bb));
97 PhiCandidate& phi_candidate = result.first->second;
98 return phi_candidate;
99 }
100
ReplacePhiUsersWith(const PhiCandidate & phi_to_remove,uint32_t repl_id)101 void SSARewriter::ReplacePhiUsersWith(const PhiCandidate& phi_to_remove,
102 uint32_t repl_id) {
103 for (uint32_t user_id : phi_to_remove.users()) {
104 PhiCandidate* user_phi = GetPhiCandidate(user_id);
105 if (user_phi) {
106 // If the user is a Phi candidate, replace all arguments that refer to
107 // |phi_to_remove.result_id()| with |repl_id|.
108 for (uint32_t& arg : user_phi->phi_args()) {
109 if (arg == phi_to_remove.result_id()) {
110 arg = repl_id;
111 }
112 }
113 } else {
114 // For regular loads, traverse the |load_replacement_| table looking for
115 // instances of |phi_to_remove|.
116 for (auto& it : load_replacement_) {
117 if (it.second == phi_to_remove.result_id()) {
118 it.second = repl_id;
119 }
120 }
121 }
122 }
123 }
124
TryRemoveTrivialPhi(PhiCandidate * phi_candidate)125 uint32_t SSARewriter::TryRemoveTrivialPhi(PhiCandidate* phi_candidate) {
126 uint32_t same_id = 0;
127 for (uint32_t arg_id : phi_candidate->phi_args()) {
128 if (arg_id == same_id || arg_id == phi_candidate->result_id()) {
129 // This is a self-reference operand or a reference to the same value ID.
130 continue;
131 }
132 if (same_id != 0) {
133 // This Phi candidate merges at least two values. Therefore, it is not
134 // trivial.
135 assert(phi_candidate->copy_of() == 0 &&
136 "Phi candidate transitioning from copy to non-copy.");
137 return phi_candidate->result_id();
138 }
139 same_id = arg_id;
140 }
141
142 // The previous logic has determined that this Phi candidate |phi_candidate|
143 // is trivial. It is essentially the copy operation phi_candidate->phi_result
144 // = Phi(same, same, same, ...). Since it is not necessary, we can re-route
145 // all the users of |phi_candidate->phi_result| to all its users, and remove
146 // |phi_candidate|.
147
148 // Mark the Phi candidate as a trivial copy of |same_id|, so it won't be
149 // generated.
150 phi_candidate->MarkCopyOf(same_id);
151
152 assert(same_id != 0 && "Completed Phis cannot have %0 in their arguments");
153
154 // Since |phi_candidate| always produces |same_id|, replace all the users of
155 // |phi_candidate| with |same_id|.
156 ReplacePhiUsersWith(*phi_candidate, same_id);
157
158 return same_id;
159 }
160
AddPhiOperands(PhiCandidate * phi_candidate)161 uint32_t SSARewriter::AddPhiOperands(PhiCandidate* phi_candidate) {
162 assert(phi_candidate->phi_args().size() == 0 &&
163 "Phi candidate already has arguments");
164
165 bool found_0_arg = false;
166 for (uint32_t pred : pass_->cfg()->preds(phi_candidate->bb()->id())) {
167 BasicBlock* pred_bb = pass_->cfg()->block(pred);
168
169 // If |pred_bb| is not sealed, use %0 to indicate that
170 // |phi_candidate| needs to be completed after the whole CFG has
171 // been processed.
172 //
173 // Note that we cannot call GetReachingDef() in these cases
174 // because this would generate an empty Phi candidate in
175 // |pred_bb|. When |pred_bb| is later processed, a new definition
176 // for |phi_candidate->var_id_| will be lost because
177 // |phi_candidate| will still be reached by the empty Phi.
178 //
179 // Consider:
180 //
181 // BB %23:
182 // %38 = Phi[%i](%int_0[%1], %39[%25])
183 //
184 // ...
185 //
186 // BB %25: [Starts unsealed]
187 // %39 = Phi[%i]()
188 // %34 = ...
189 // OpStore %i %34 -> Currdef(%i) at %25 is %34
190 // OpBranch %23
191 //
192 // When we first create the Phi in %38, we add an operandless Phi in
193 // %39 to hold the unknown reaching def for %i.
194 //
195 // But then, when we go to complete %39 at the end. The reaching def
196 // for %i in %25's predecessor is %38 itself. So we miss the fact
197 // that %25 has a def for %i that should be used.
198 //
199 // By making the argument %0, we make |phi_candidate| incomplete,
200 // which will cause it to be completed after the whole CFG has
201 // been scanned.
202 uint32_t arg_id = IsBlockSealed(pred_bb)
203 ? GetReachingDef(phi_candidate->var_id(), pred_bb)
204 : 0;
205 phi_candidate->phi_args().push_back(arg_id);
206
207 if (arg_id == 0) {
208 found_0_arg = true;
209 } else {
210 // If this argument is another Phi candidate, add |phi_candidate| to the
211 // list of users for the defining Phi.
212 PhiCandidate* defining_phi = GetPhiCandidate(arg_id);
213 if (defining_phi && defining_phi != phi_candidate) {
214 defining_phi->AddUser(phi_candidate->result_id());
215 }
216 }
217 }
218
219 // If we could not fill-in all the arguments of this Phi, mark it incomplete
220 // so it gets completed after the whole CFG has been processed.
221 if (found_0_arg) {
222 phi_candidate->MarkIncomplete();
223 incomplete_phis_.push(phi_candidate);
224 return phi_candidate->result_id();
225 }
226
227 // Try to remove |phi_candidate|, if it's trivial.
228 uint32_t repl_id = TryRemoveTrivialPhi(phi_candidate);
229 if (repl_id == phi_candidate->result_id()) {
230 // |phi_candidate| is complete and not trivial. Add it to the
231 // list of Phi candidates to generate.
232 phi_candidate->MarkComplete();
233 phis_to_generate_.push_back(phi_candidate);
234 }
235
236 return repl_id;
237 }
238
GetReachingDef(uint32_t var_id,BasicBlock * bb)239 uint32_t SSARewriter::GetReachingDef(uint32_t var_id, BasicBlock* bb) {
240 // If |var_id| has a definition in |bb|, return it.
241 const auto& bb_it = defs_at_block_.find(bb);
242 if (bb_it != defs_at_block_.end()) {
243 const auto& current_defs = bb_it->second;
244 const auto& var_it = current_defs.find(var_id);
245 if (var_it != current_defs.end()) {
246 return var_it->second;
247 }
248 }
249
250 // Otherwise, look up the value for |var_id| in |bb|'s predecessors.
251 uint32_t val_id = 0;
252 auto& predecessors = pass_->cfg()->preds(bb->id());
253 if (predecessors.size() == 1) {
254 // If |bb| has exactly one predecessor, we look for |var_id|'s definition
255 // there.
256 val_id = GetReachingDef(var_id, pass_->cfg()->block(predecessors[0]));
257 } else if (predecessors.size() > 1) {
258 // If there is more than one predecessor, this is a join block which may
259 // require a Phi instruction. This will act as |var_id|'s current
260 // definition to break potential cycles.
261 PhiCandidate& phi_candidate = CreatePhiCandidate(var_id, bb);
262 WriteVariable(var_id, bb, phi_candidate.result_id());
263 val_id = AddPhiOperands(&phi_candidate);
264 }
265
266 // If we could not find a store for this variable in the path from the root
267 // of the CFG, the variable is not defined, so we use undef.
268 if (val_id == 0) {
269 val_id = pass_->GetUndefVal(var_id);
270 }
271
272 WriteVariable(var_id, bb, val_id);
273
274 return val_id;
275 }
276
SealBlock(BasicBlock * bb)277 void SSARewriter::SealBlock(BasicBlock* bb) {
278 auto result = sealed_blocks_.insert(bb);
279 (void)result;
280 assert(result.second == true &&
281 "Tried to seal the same basic block more than once.");
282 }
283
ProcessStore(Instruction * inst,BasicBlock * bb)284 void SSARewriter::ProcessStore(Instruction* inst, BasicBlock* bb) {
285 auto opcode = inst->opcode();
286 assert((opcode == SpvOpStore || opcode == SpvOpVariable) &&
287 "Expecting a store or a variable definition instruction.");
288
289 uint32_t var_id = 0;
290 uint32_t val_id = 0;
291 if (opcode == SpvOpStore) {
292 (void)pass_->GetPtr(inst, &var_id);
293 val_id = inst->GetSingleWordInOperand(kStoreValIdInIdx);
294 } else if (inst->NumInOperands() >= 2) {
295 var_id = inst->result_id();
296 val_id = inst->GetSingleWordInOperand(kVariableInitIdInIdx);
297 }
298 if (pass_->IsTargetVar(var_id)) {
299 WriteVariable(var_id, bb, val_id);
300
301 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
302 std::cerr << "\tFound store '%" << var_id << " = %" << val_id << "': "
303 << inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
304 << "\n";
305 #endif
306 }
307 }
308
ProcessLoad(Instruction * inst,BasicBlock * bb)309 void SSARewriter::ProcessLoad(Instruction* inst, BasicBlock* bb) {
310 uint32_t var_id = 0;
311 (void)pass_->GetPtr(inst, &var_id);
312 if (pass_->IsTargetVar(var_id)) {
313 // Get the immediate reaching definition for |var_id|.
314 uint32_t val_id = GetReachingDef(var_id, bb);
315
316 // Schedule a replacement for the result of this load instruction with
317 // |val_id|. After all the rewriting decisions are made, every use of
318 // this load will be replaced with |val_id|.
319 const uint32_t load_id = inst->result_id();
320 assert(load_replacement_.count(load_id) == 0);
321 load_replacement_[load_id] = val_id;
322 PhiCandidate* defining_phi = GetPhiCandidate(val_id);
323 if (defining_phi) {
324 defining_phi->AddUser(load_id);
325 }
326
327 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
328 std::cerr << "\tFound load: "
329 << inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
330 << " (replacement for %" << load_id << " is %" << val_id << ")\n";
331 #endif
332 }
333 }
334
PrintPhiCandidates() const335 void SSARewriter::PrintPhiCandidates() const {
336 std::cerr << "\nPhi candidates:\n";
337 for (const auto& phi_it : phi_candidates_) {
338 std::cerr << "\tBB %" << phi_it.second.bb()->id() << ": "
339 << phi_it.second.PrettyPrint(pass_->cfg()) << "\n";
340 }
341 std::cerr << "\n";
342 }
343
PrintReplacementTable() const344 void SSARewriter::PrintReplacementTable() const {
345 std::cerr << "\nLoad replacement table\n";
346 for (const auto& it : load_replacement_) {
347 std::cerr << "\t%" << it.first << " -> %" << it.second << "\n";
348 }
349 std::cerr << "\n";
350 }
351
GenerateSSAReplacements(BasicBlock * bb)352 void SSARewriter::GenerateSSAReplacements(BasicBlock* bb) {
353 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
354 std::cerr << "Generating SSA replacements for block: " << bb->id() << "\n";
355 std::cerr << bb->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
356 << "\n";
357 #endif
358
359 for (auto& inst : *bb) {
360 auto opcode = inst.opcode();
361 if (opcode == SpvOpStore || opcode == SpvOpVariable) {
362 ProcessStore(&inst, bb);
363 } else if (inst.opcode() == SpvOpLoad) {
364 ProcessLoad(&inst, bb);
365 }
366 }
367
368 // Seal |bb|. This means that all the stores in it have been scanned and it's
369 // ready to feed them into its successors.
370 SealBlock(bb);
371
372 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
373 PrintPhiCandidates();
374 PrintReplacementTable();
375 std::cerr << "\n\n";
376 #endif
377 }
378
GetReplacement(std::pair<uint32_t,uint32_t> repl)379 uint32_t SSARewriter::GetReplacement(std::pair<uint32_t, uint32_t> repl) {
380 uint32_t val_id = repl.second;
381 auto it = load_replacement_.find(val_id);
382 while (it != load_replacement_.end()) {
383 val_id = it->second;
384 it = load_replacement_.find(val_id);
385 }
386 return val_id;
387 }
388
GetPhiArgument(const PhiCandidate * phi_candidate,uint32_t ix)389 uint32_t SSARewriter::GetPhiArgument(const PhiCandidate* phi_candidate,
390 uint32_t ix) {
391 assert(phi_candidate->IsReady() &&
392 "Tried to get the final argument from an incomplete/trivial Phi");
393
394 uint32_t arg_id = phi_candidate->phi_args()[ix];
395 while (arg_id != 0) {
396 PhiCandidate* phi_user = GetPhiCandidate(arg_id);
397 if (phi_user == nullptr || phi_user->IsReady()) {
398 // If the argument is not a Phi or it's a Phi candidate ready to be
399 // emitted, return it.
400 return arg_id;
401 }
402 arg_id = phi_user->copy_of();
403 }
404
405 assert(false &&
406 "No Phi candidates in the copy-of chain are ready to be generated");
407
408 return 0;
409 }
410
ApplyReplacements()411 bool SSARewriter::ApplyReplacements() {
412 bool modified = false;
413
414 #if SSA_REWRITE_DEBUGGING_LEVEL > 2
415 std::cerr << "\n\nApplying replacement decisions to IR\n\n";
416 PrintPhiCandidates();
417 PrintReplacementTable();
418 std::cerr << "\n\n";
419 #endif
420
421 // Add Phi instructions from completed Phi candidates.
422 std::vector<Instruction*> generated_phis;
423 for (const PhiCandidate* phi_candidate : phis_to_generate_) {
424 #if SSA_REWRITE_DEBUGGING_LEVEL > 2
425 std::cerr << "Phi candidate: " << phi_candidate->PrettyPrint(pass_->cfg())
426 << "\n";
427 #endif
428
429 assert(phi_candidate->is_complete() &&
430 "Tried to instantiate a Phi instruction from an incomplete Phi "
431 "candidate");
432
433 // Build the vector of operands for the new OpPhi instruction.
434 uint32_t type_id = pass_->GetPointeeTypeId(
435 pass_->get_def_use_mgr()->GetDef(phi_candidate->var_id()));
436 std::vector<Operand> phi_operands;
437 uint32_t arg_ix = 0;
438 std::unordered_map<uint32_t, uint32_t> already_seen;
439 for (uint32_t pred_label : pass_->cfg()->preds(phi_candidate->bb()->id())) {
440 uint32_t op_val_id = GetPhiArgument(phi_candidate, arg_ix++);
441 if (already_seen.count(pred_label) == 0) {
442 phi_operands.push_back(
443 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {op_val_id}});
444 phi_operands.push_back(
445 {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {pred_label}});
446 already_seen[pred_label] = op_val_id;
447 } else {
448 // It is possible that there are two edges from the same parent block.
449 // Since the OpPhi can have only one entry for each parent, we have to
450 // make sure the two edges are consistent with each other.
451 assert(already_seen[pred_label] == op_val_id &&
452 "Inconsistent value for duplicate edges.");
453 }
454 }
455
456 // Generate a new OpPhi instruction and insert it in its basic
457 // block.
458 std::unique_ptr<Instruction> phi_inst(
459 new Instruction(pass_->context(), SpvOpPhi, type_id,
460 phi_candidate->result_id(), phi_operands));
461 generated_phis.push_back(phi_inst.get());
462 pass_->get_def_use_mgr()->AnalyzeInstDef(&*phi_inst);
463 pass_->context()->set_instr_block(&*phi_inst, phi_candidate->bb());
464 auto insert_it = phi_candidate->bb()->begin();
465 insert_it.InsertBefore(std::move(phi_inst));
466 pass_->context()->get_decoration_mgr()->CloneDecorations(
467 phi_candidate->var_id(), phi_candidate->result_id(),
468 {SpvDecorationRelaxedPrecision});
469
470 modified = true;
471 }
472
473 // Scan uses for all inserted Phi instructions. Do this separately from the
474 // registration of the Phi instruction itself to avoid trying to analyze uses
475 // of Phi instructions that have not been registered yet.
476 for (Instruction* phi_inst : generated_phis) {
477 pass_->get_def_use_mgr()->AnalyzeInstUse(&*phi_inst);
478 }
479
480 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
481 std::cerr << "\n\nReplacing the result of load instructions with the "
482 "corresponding SSA id\n\n";
483 #endif
484
485 // Apply replacements from the load replacement table.
486 for (auto& repl : load_replacement_) {
487 uint32_t load_id = repl.first;
488 uint32_t val_id = GetReplacement(repl);
489 Instruction* load_inst =
490 pass_->context()->get_def_use_mgr()->GetDef(load_id);
491
492 #if SSA_REWRITE_DEBUGGING_LEVEL > 2
493 std::cerr << "\t"
494 << load_inst->PrettyPrint(
495 SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
496 << " (%" << load_id << " -> %" << val_id << ")\n";
497 #endif
498
499 // Remove the load instruction and replace all the uses of this load's
500 // result with |val_id|. Kill any names or decorates using the load's
501 // result before replacing to prevent incorrect replacement in those
502 // instructions.
503 pass_->context()->KillNamesAndDecorates(load_id);
504 pass_->context()->ReplaceAllUsesWith(load_id, val_id);
505 pass_->context()->KillInst(load_inst);
506 modified = true;
507 }
508
509 return modified;
510 }
511
FinalizePhiCandidate(PhiCandidate * phi_candidate)512 void SSARewriter::FinalizePhiCandidate(PhiCandidate* phi_candidate) {
513 assert(phi_candidate->phi_args().size() > 0 &&
514 "Phi candidate should have arguments");
515
516 uint32_t ix = 0;
517 for (uint32_t pred : pass_->cfg()->preds(phi_candidate->bb()->id())) {
518 BasicBlock* pred_bb = pass_->cfg()->block(pred);
519 uint32_t& arg_id = phi_candidate->phi_args()[ix++];
520 if (arg_id == 0) {
521 // If |pred_bb| is still not sealed, it means it's unreachable. In this
522 // case, we just use Undef as an argument.
523 arg_id = IsBlockSealed(pred_bb)
524 ? GetReachingDef(phi_candidate->var_id(), pred_bb)
525 : pass_->GetUndefVal(phi_candidate->var_id());
526 }
527 }
528
529 // This candidate is now completed.
530 phi_candidate->MarkComplete();
531
532 // If |phi_candidate| is not trivial, add it to the list of Phis to generate.
533 if (TryRemoveTrivialPhi(phi_candidate) == phi_candidate->result_id()) {
534 // If we could not remove |phi_candidate|, it means that it is complete
535 // and not trivial. Add it to the list of Phis to generate.
536 assert(!phi_candidate->copy_of() && "A completed Phi cannot be trivial.");
537 phis_to_generate_.push_back(phi_candidate);
538 }
539 }
540
FinalizePhiCandidates()541 void SSARewriter::FinalizePhiCandidates() {
542 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
543 std::cerr << "Finalizing Phi candidates:\n\n";
544 PrintPhiCandidates();
545 std::cerr << "\n";
546 #endif
547
548 // Now, complete the collected candidates.
549 while (incomplete_phis_.size() > 0) {
550 PhiCandidate* phi_candidate = incomplete_phis_.front();
551 incomplete_phis_.pop();
552 FinalizePhiCandidate(phi_candidate);
553 }
554 }
555
RewriteFunctionIntoSSA(Function * fp)556 bool SSARewriter::RewriteFunctionIntoSSA(Function* fp) {
557 #if SSA_REWRITE_DEBUGGING_LEVEL > 0
558 std::cerr << "Function before SSA rewrite:\n"
559 << fp->PrettyPrint(0) << "\n\n\n";
560 #endif
561
562 // Collect variables that can be converted into SSA IDs.
563 pass_->CollectTargetVars(fp);
564
565 // Generate all the SSA replacements and Phi candidates. This will
566 // generate incomplete and trivial Phis.
567 pass_->cfg()->ForEachBlockInReversePostOrder(
568 fp->entry().get(),
569 [this](BasicBlock* bb) { GenerateSSAReplacements(bb); });
570
571 // Remove trivial Phis and add arguments to incomplete Phis.
572 FinalizePhiCandidates();
573
574 // Finally, apply all the replacements in the IR.
575 bool modified = ApplyReplacements();
576
577 #if SSA_REWRITE_DEBUGGING_LEVEL > 0
578 std::cerr << "\n\n\nFunction after SSA rewrite:\n"
579 << fp->PrettyPrint(0) << "\n";
580 #endif
581
582 return modified;
583 }
584
Process()585 Pass::Status SSARewritePass::Process() {
586 bool modified = false;
587 for (auto& fn : *get_module()) {
588 modified |= SSARewriter(this).RewriteFunctionIntoSSA(&fn);
589 }
590 return modified ? Pass::Status::SuccessWithChange
591 : Pass::Status::SuccessWithoutChange;
592 }
593
594 } // namespace opt
595 } // namespace spvtools
596