• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/opt/types.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 namespace {
66 constexpr uint32_t kStoreValIdInIdx = 1;
67 constexpr uint32_t kVariableInitIdInIdx = 1;
68 }  // namespace
69 
PrettyPrint(const CFG * cfg) const70 std::string SSARewriter::PhiCandidate::PrettyPrint(const CFG* cfg) const {
71   std::ostringstream str;
72   str << "%" << result_id_ << " = Phi[%" << var_id_ << ", BB %" << bb_->id()
73       << "](";
74   if (phi_args_.size() > 0) {
75     uint32_t arg_ix = 0;
76     for (uint32_t pred_label : cfg->preds(bb_->id())) {
77       uint32_t arg_id = phi_args_[arg_ix++];
78       str << "[%" << arg_id << ", bb(%" << pred_label << ")] ";
79     }
80   }
81   str << ")";
82   if (copy_of_ != 0) {
83     str << "  [COPY OF " << copy_of_ << "]";
84   }
85   str << ((is_complete_) ? "  [COMPLETE]" : "  [INCOMPLETE]");
86 
87   return str.str();
88 }
89 
CreatePhiCandidate(uint32_t var_id,BasicBlock * bb)90 SSARewriter::PhiCandidate& SSARewriter::CreatePhiCandidate(uint32_t var_id,
91                                                            BasicBlock* bb) {
92   // TODO(1841): Handle id overflow.
93   uint32_t phi_result_id = pass_->context()->TakeNextId();
94   auto result = phi_candidates_.emplace(
95       phi_result_id, PhiCandidate(var_id, phi_result_id, bb));
96   PhiCandidate& phi_candidate = result.first->second;
97   return phi_candidate;
98 }
99 
ReplacePhiUsersWith(const PhiCandidate & phi_to_remove,uint32_t repl_id)100 void SSARewriter::ReplacePhiUsersWith(const PhiCandidate& phi_to_remove,
101                                       uint32_t repl_id) {
102   for (uint32_t user_id : phi_to_remove.users()) {
103     PhiCandidate* user_phi = GetPhiCandidate(user_id);
104     BasicBlock* bb = pass_->context()->get_instr_block(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 if (bb->id() == user_id) {
114       // The phi candidate is the definition of the variable at basic block
115       // |bb|.  We must change this to the replacement.
116       WriteVariable(phi_to_remove.var_id(), bb, repl_id);
117     } else {
118       // For regular loads, traverse the |load_replacement_| table looking for
119       // instances of |phi_to_remove|.
120       for (auto& it : load_replacement_) {
121         if (it.second == phi_to_remove.result_id()) {
122           it.second = repl_id;
123         }
124       }
125     }
126   }
127 }
128 
TryRemoveTrivialPhi(PhiCandidate * phi_candidate)129 uint32_t SSARewriter::TryRemoveTrivialPhi(PhiCandidate* phi_candidate) {
130   uint32_t same_id = 0;
131   for (uint32_t arg_id : phi_candidate->phi_args()) {
132     if (arg_id == same_id || arg_id == phi_candidate->result_id()) {
133       // This is a self-reference operand or a reference to the same value ID.
134       continue;
135     }
136     if (same_id != 0) {
137       // This Phi candidate merges at least two values.  Therefore, it is not
138       // trivial.
139       assert(phi_candidate->copy_of() == 0 &&
140              "Phi candidate transitioning from copy to non-copy.");
141       return phi_candidate->result_id();
142     }
143     same_id = arg_id;
144   }
145 
146   // The previous logic has determined that this Phi candidate |phi_candidate|
147   // is trivial.  It is essentially the copy operation phi_candidate->phi_result
148   // = Phi(same, same, same, ...).  Since it is not necessary, we can re-route
149   // all the users of |phi_candidate->phi_result| to all its users, and remove
150   // |phi_candidate|.
151 
152   // Mark the Phi candidate as a trivial copy of |same_id|, so it won't be
153   // generated.
154   phi_candidate->MarkCopyOf(same_id);
155 
156   assert(same_id != 0 && "Completed Phis cannot have %0 in their arguments");
157 
158   // Since |phi_candidate| always produces |same_id|, replace all the users of
159   // |phi_candidate| with |same_id|.
160   ReplacePhiUsersWith(*phi_candidate, same_id);
161 
162   return same_id;
163 }
164 
AddPhiOperands(PhiCandidate * phi_candidate)165 uint32_t SSARewriter::AddPhiOperands(PhiCandidate* phi_candidate) {
166   assert(phi_candidate->phi_args().size() == 0 &&
167          "Phi candidate already has arguments");
168 
169   bool found_0_arg = false;
170   for (uint32_t pred : pass_->cfg()->preds(phi_candidate->bb()->id())) {
171     BasicBlock* pred_bb = pass_->cfg()->block(pred);
172 
173     // If |pred_bb| is not sealed, use %0 to indicate that
174     // |phi_candidate| needs to be completed after the whole CFG has
175     // been processed.
176     //
177     // Note that we cannot call GetReachingDef() in these cases
178     // because this would generate an empty Phi candidate in
179     // |pred_bb|.  When |pred_bb| is later processed, a new definition
180     // for |phi_candidate->var_id_| will be lost because
181     // |phi_candidate| will still be reached by the empty Phi.
182     //
183     // Consider:
184     //
185     //       BB %23:
186     //           %38 = Phi[%i](%int_0[%1], %39[%25])
187     //
188     //           ...
189     //
190     //       BB %25: [Starts unsealed]
191     //       %39 = Phi[%i]()
192     //       %34 = ...
193     //       OpStore %i %34    -> Currdef(%i) at %25 is %34
194     //       OpBranch %23
195     //
196     // When we first create the Phi in %38, we add an operandless Phi in
197     // %39 to hold the unknown reaching def for %i.
198     //
199     // But then, when we go to complete %39 at the end.  The reaching def
200     // for %i in %25's predecessor is %38 itself.  So we miss the fact
201     // that %25 has a def for %i that should be used.
202     //
203     // By making the argument %0, we make |phi_candidate| incomplete,
204     // which will cause it to be completed after the whole CFG has
205     // been scanned.
206     uint32_t arg_id = IsBlockSealed(pred_bb)
207                           ? GetReachingDef(phi_candidate->var_id(), pred_bb)
208                           : 0;
209     phi_candidate->phi_args().push_back(arg_id);
210 
211     if (arg_id == 0) {
212       found_0_arg = true;
213     } else {
214       // If this argument is another Phi candidate, add |phi_candidate| to the
215       // list of users for the defining Phi.
216       PhiCandidate* defining_phi = GetPhiCandidate(arg_id);
217       if (defining_phi && defining_phi != phi_candidate) {
218         defining_phi->AddUser(phi_candidate->result_id());
219       }
220     }
221   }
222 
223   // If we could not fill-in all the arguments of this Phi, mark it incomplete
224   // so it gets completed after the whole CFG has been processed.
225   if (found_0_arg) {
226     phi_candidate->MarkIncomplete();
227     incomplete_phis_.push(phi_candidate);
228     return phi_candidate->result_id();
229   }
230 
231   // Try to remove |phi_candidate|, if it's trivial.
232   uint32_t repl_id = TryRemoveTrivialPhi(phi_candidate);
233   if (repl_id == phi_candidate->result_id()) {
234     // |phi_candidate| is complete and not trivial.  Add it to the
235     // list of Phi candidates to generate.
236     phi_candidate->MarkComplete();
237     phis_to_generate_.push_back(phi_candidate);
238   }
239 
240   return repl_id;
241 }
242 
GetValueAtBlock(uint32_t var_id,BasicBlock * bb)243 uint32_t SSARewriter::GetValueAtBlock(uint32_t var_id, BasicBlock* bb) {
244   assert(bb != nullptr);
245   const auto& bb_it = defs_at_block_.find(bb);
246   if (bb_it != defs_at_block_.end()) {
247     const auto& current_defs = bb_it->second;
248     const auto& var_it = current_defs.find(var_id);
249     if (var_it != current_defs.end()) {
250       return var_it->second;
251     }
252   }
253   return 0;
254 }
255 
GetReachingDef(uint32_t var_id,BasicBlock * bb)256 uint32_t SSARewriter::GetReachingDef(uint32_t var_id, BasicBlock* bb) {
257   // If |var_id| has a definition in |bb|, return it.
258   uint32_t val_id = GetValueAtBlock(var_id, bb);
259   if (val_id != 0) return val_id;
260 
261   // Otherwise, look up the value for |var_id| in |bb|'s predecessors.
262   auto& predecessors = pass_->cfg()->preds(bb->id());
263   if (predecessors.size() == 1) {
264     // If |bb| has exactly one predecessor, we look for |var_id|'s definition
265     // there.
266     val_id = GetReachingDef(var_id, pass_->cfg()->block(predecessors[0]));
267   } else if (predecessors.size() > 1) {
268     // If there is more than one predecessor, this is a join block which may
269     // require a Phi instruction.  This will act as |var_id|'s current
270     // definition to break potential cycles.
271     PhiCandidate& phi_candidate = CreatePhiCandidate(var_id, bb);
272 
273     // Set the value for |bb| to avoid an infinite recursion.
274     WriteVariable(var_id, bb, phi_candidate.result_id());
275     val_id = AddPhiOperands(&phi_candidate);
276   }
277 
278   // If we could not find a store for this variable in the path from the root
279   // of the CFG, the variable is not defined, so we use undef.
280   if (val_id == 0) {
281     val_id = pass_->GetUndefVal(var_id);
282     if (val_id == 0) {
283       return 0;
284     }
285   }
286 
287   WriteVariable(var_id, bb, val_id);
288 
289   return val_id;
290 }
291 
SealBlock(BasicBlock * bb)292 void SSARewriter::SealBlock(BasicBlock* bb) {
293   auto result = sealed_blocks_.insert(bb);
294   (void)result;
295   assert(result.second == true &&
296          "Tried to seal the same basic block more than once.");
297 }
298 
ProcessStore(Instruction * inst,BasicBlock * bb)299 void SSARewriter::ProcessStore(Instruction* inst, BasicBlock* bb) {
300   auto opcode = inst->opcode();
301   assert((opcode == spv::Op::OpStore || opcode == spv::Op::OpVariable) &&
302          "Expecting a store or a variable definition instruction.");
303 
304   uint32_t var_id = 0;
305   uint32_t val_id = 0;
306   if (opcode == spv::Op::OpStore) {
307     (void)pass_->GetPtr(inst, &var_id);
308     val_id = inst->GetSingleWordInOperand(kStoreValIdInIdx);
309   } else if (inst->NumInOperands() >= 2) {
310     var_id = inst->result_id();
311     val_id = inst->GetSingleWordInOperand(kVariableInitIdInIdx);
312   }
313   if (pass_->IsTargetVar(var_id)) {
314     WriteVariable(var_id, bb, val_id);
315     pass_->context()->get_debug_info_mgr()->AddDebugValueForVariable(
316         inst, var_id, val_id, inst);
317 
318 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
319     std::cerr << "\tFound store '%" << var_id << " = %" << val_id << "': "
320               << inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
321               << "\n";
322 #endif
323   }
324 }
325 
ProcessLoad(Instruction * inst,BasicBlock * bb)326 bool SSARewriter::ProcessLoad(Instruction* inst, BasicBlock* bb) {
327   // Get the pointer that we are using to load from.
328   uint32_t var_id = 0;
329   (void)pass_->GetPtr(inst, &var_id);
330 
331   // Get the immediate reaching definition for |var_id|.
332   //
333   // In the presence of variable pointers, the reaching definition may be
334   // another pointer.  For example, the following fragment:
335   //
336   //  %2 = OpVariable %_ptr_Input_float Input
337   // %11 = OpVariable %_ptr_Function__ptr_Input_float Function
338   //       OpStore %11 %2
339   // %12 = OpLoad %_ptr_Input_float %11
340   // %13 = OpLoad %float %12
341   //
342   // corresponds to the pseudo-code:
343   //
344   // layout(location = 0) in flat float *%2
345   // float %13;
346   // float *%12;
347   // float **%11;
348   // *%11 = %2;
349   // %12 = *%11;
350   // %13 = *%12;
351   //
352   // which ultimately, should correspond to:
353   //
354   // %13 = *%2;
355   //
356   // During rewriting, the pointer %12 is found to be replaceable by %2 (i.e.,
357   // load_replacement_[12] is 2). However, when processing the load
358   // %13 = *%12, the type of %12's reaching definition is another float
359   // pointer (%2), instead of a float value.
360   //
361   // When this happens, we need to continue looking up the reaching definition
362   // chain until we get to a float value or a non-target var (i.e. a variable
363   // that cannot be SSA replaced, like %2 in this case since it is a function
364   // argument).
365   analysis::DefUseManager* def_use_mgr = pass_->context()->get_def_use_mgr();
366   analysis::TypeManager* type_mgr = pass_->context()->get_type_mgr();
367   analysis::Type* load_type = type_mgr->GetType(inst->type_id());
368   uint32_t val_id = 0;
369   bool found_reaching_def = false;
370   while (!found_reaching_def) {
371     if (!pass_->IsTargetVar(var_id)) {
372       // If the variable we are loading from is not an SSA target (globals,
373       // function parameters), do nothing.
374       return true;
375     }
376 
377     val_id = GetReachingDef(var_id, bb);
378     if (val_id == 0) {
379       return false;
380     }
381 
382     // If the reaching definition is a pointer type different than the type of
383     // the instruction we are analyzing, then it must be a reference to another
384     // pointer (otherwise, this would be invalid SPIRV).  We continue
385     // de-referencing it by making |val_id| be |var_id|.
386     //
387     // NOTE: if there is no reaching definition instruction, it means |val_id|
388     // is an undef.
389     Instruction* reaching_def_inst = def_use_mgr->GetDef(val_id);
390     if (reaching_def_inst &&
391         !type_mgr->GetType(reaching_def_inst->type_id())->IsSame(load_type)) {
392       var_id = val_id;
393     } else {
394       found_reaching_def = true;
395     }
396   }
397 
398   // Schedule a replacement for the result of this load instruction with
399   // |val_id|. After all the rewriting decisions are made, every use of
400   // this load will be replaced with |val_id|.
401   uint32_t load_id = inst->result_id();
402   assert(load_replacement_.count(load_id) == 0);
403   load_replacement_[load_id] = val_id;
404   PhiCandidate* defining_phi = GetPhiCandidate(val_id);
405   if (defining_phi) {
406     defining_phi->AddUser(load_id);
407   }
408 
409 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
410   std::cerr << "\tFound load: "
411             << inst->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
412             << " (replacement for %" << load_id << " is %" << val_id << ")\n";
413 #endif
414 
415   return true;
416 }
417 
PrintPhiCandidates() const418 void SSARewriter::PrintPhiCandidates() const {
419   std::cerr << "\nPhi candidates:\n";
420   for (const auto& phi_it : phi_candidates_) {
421     std::cerr << "\tBB %" << phi_it.second.bb()->id() << ": "
422               << phi_it.second.PrettyPrint(pass_->cfg()) << "\n";
423   }
424   std::cerr << "\n";
425 }
426 
PrintReplacementTable() const427 void SSARewriter::PrintReplacementTable() const {
428   std::cerr << "\nLoad replacement table\n";
429   for (const auto& it : load_replacement_) {
430     std::cerr << "\t%" << it.first << " -> %" << it.second << "\n";
431   }
432   std::cerr << "\n";
433 }
434 
GenerateSSAReplacements(BasicBlock * bb)435 bool SSARewriter::GenerateSSAReplacements(BasicBlock* bb) {
436 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
437   std::cerr << "Generating SSA replacements for block: " << bb->id() << "\n";
438   std::cerr << bb->PrettyPrint(SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
439             << "\n";
440 #endif
441 
442   for (auto& inst : *bb) {
443     auto opcode = inst.opcode();
444     if (opcode == spv::Op::OpStore || opcode == spv::Op::OpVariable) {
445       ProcessStore(&inst, bb);
446     } else if (inst.opcode() == spv::Op::OpLoad) {
447       if (!ProcessLoad(&inst, bb)) {
448         return false;
449       }
450     }
451   }
452 
453   // Seal |bb|. This means that all the stores in it have been scanned and
454   // it's ready to feed them into its successors.
455   SealBlock(bb);
456 
457 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
458   PrintPhiCandidates();
459   PrintReplacementTable();
460   std::cerr << "\n\n";
461 #endif
462   return true;
463 }
464 
GetReplacement(std::pair<uint32_t,uint32_t> repl)465 uint32_t SSARewriter::GetReplacement(std::pair<uint32_t, uint32_t> repl) {
466   uint32_t val_id = repl.second;
467   auto it = load_replacement_.find(val_id);
468   while (it != load_replacement_.end()) {
469     val_id = it->second;
470     it = load_replacement_.find(val_id);
471   }
472   return val_id;
473 }
474 
GetPhiArgument(const PhiCandidate * phi_candidate,uint32_t ix)475 uint32_t SSARewriter::GetPhiArgument(const PhiCandidate* phi_candidate,
476                                      uint32_t ix) {
477   assert(phi_candidate->IsReady() &&
478          "Tried to get the final argument from an incomplete/trivial Phi");
479 
480   uint32_t arg_id = phi_candidate->phi_args()[ix];
481   while (arg_id != 0) {
482     PhiCandidate* phi_user = GetPhiCandidate(arg_id);
483     if (phi_user == nullptr || phi_user->IsReady()) {
484       // If the argument is not a Phi or it's a Phi candidate ready to be
485       // emitted, return it.
486       return arg_id;
487     }
488     arg_id = phi_user->copy_of();
489   }
490 
491   assert(false &&
492          "No Phi candidates in the copy-of chain are ready to be generated");
493 
494   return 0;
495 }
496 
ApplyReplacements()497 bool SSARewriter::ApplyReplacements() {
498   bool modified = false;
499 
500 #if SSA_REWRITE_DEBUGGING_LEVEL > 2
501   std::cerr << "\n\nApplying replacement decisions to IR\n\n";
502   PrintPhiCandidates();
503   PrintReplacementTable();
504   std::cerr << "\n\n";
505 #endif
506 
507   // Add Phi instructions from completed Phi candidates.
508   std::vector<Instruction*> generated_phis;
509   for (const PhiCandidate* phi_candidate : phis_to_generate_) {
510 #if SSA_REWRITE_DEBUGGING_LEVEL > 2
511     std::cerr << "Phi candidate: " << phi_candidate->PrettyPrint(pass_->cfg())
512               << "\n";
513 #endif
514 
515     assert(phi_candidate->is_complete() &&
516            "Tried to instantiate a Phi instruction from an incomplete Phi "
517            "candidate");
518 
519     auto* local_var = pass_->get_def_use_mgr()->GetDef(phi_candidate->var_id());
520 
521     // Build the vector of operands for the new OpPhi instruction.
522     uint32_t type_id = pass_->GetPointeeTypeId(local_var);
523     std::vector<Operand> phi_operands;
524     uint32_t arg_ix = 0;
525     std::unordered_map<uint32_t, uint32_t> already_seen;
526     for (uint32_t pred_label : pass_->cfg()->preds(phi_candidate->bb()->id())) {
527       uint32_t op_val_id = GetPhiArgument(phi_candidate, arg_ix++);
528       if (already_seen.count(pred_label) == 0) {
529         phi_operands.push_back(
530             {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {op_val_id}});
531         phi_operands.push_back(
532             {spv_operand_type_t::SPV_OPERAND_TYPE_ID, {pred_label}});
533         already_seen[pred_label] = op_val_id;
534       } else {
535         // It is possible that there are two edges from the same parent block.
536         // Since the OpPhi can have only one entry for each parent, we have to
537         // make sure the two edges are consistent with each other.
538         assert(already_seen[pred_label] == op_val_id &&
539                "Inconsistent value for duplicate edges.");
540       }
541     }
542 
543     // Generate a new OpPhi instruction and insert it in its basic
544     // block.
545     std::unique_ptr<Instruction> phi_inst(
546         new Instruction(pass_->context(), spv::Op::OpPhi, type_id,
547                         phi_candidate->result_id(), phi_operands));
548     generated_phis.push_back(phi_inst.get());
549     pass_->get_def_use_mgr()->AnalyzeInstDef(&*phi_inst);
550     pass_->context()->set_instr_block(&*phi_inst, phi_candidate->bb());
551     auto insert_it = phi_candidate->bb()->begin();
552     insert_it = insert_it.InsertBefore(std::move(phi_inst));
553     pass_->context()->get_decoration_mgr()->CloneDecorations(
554         phi_candidate->var_id(), phi_candidate->result_id(),
555         {spv::Decoration::RelaxedPrecision});
556 
557     // Add DebugValue for the new OpPhi instruction.
558     insert_it->SetDebugScope(local_var->GetDebugScope());
559     pass_->context()->get_debug_info_mgr()->AddDebugValueForVariable(
560         &*insert_it, phi_candidate->var_id(), phi_candidate->result_id(),
561         &*insert_it);
562 
563     modified = true;
564   }
565 
566   // Scan uses for all inserted Phi instructions. Do this separately from the
567   // registration of the Phi instruction itself to avoid trying to analyze
568   // uses of Phi instructions that have not been registered yet.
569   for (Instruction* phi_inst : generated_phis) {
570     pass_->get_def_use_mgr()->AnalyzeInstUse(&*phi_inst);
571   }
572 
573 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
574   std::cerr << "\n\nReplacing the result of load instructions with the "
575                "corresponding SSA id\n\n";
576 #endif
577 
578   // Apply replacements from the load replacement table.
579   for (auto& repl : load_replacement_) {
580     uint32_t load_id = repl.first;
581     uint32_t val_id = GetReplacement(repl);
582     Instruction* load_inst =
583         pass_->context()->get_def_use_mgr()->GetDef(load_id);
584 
585 #if SSA_REWRITE_DEBUGGING_LEVEL > 2
586     std::cerr << "\t"
587               << load_inst->PrettyPrint(
588                      SPV_BINARY_TO_TEXT_OPTION_FRIENDLY_NAMES)
589               << "  (%" << load_id << " -> %" << val_id << ")\n";
590 #endif
591 
592     // Remove the load instruction and replace all the uses of this load's
593     // result with |val_id|.  Kill any names or decorates using the load's
594     // result before replacing to prevent incorrect replacement in those
595     // instructions.
596     pass_->context()->KillNamesAndDecorates(load_id);
597     pass_->context()->ReplaceAllUsesWith(load_id, val_id);
598     pass_->context()->KillInst(load_inst);
599     modified = true;
600   }
601 
602   return modified;
603 }
604 
FinalizePhiCandidate(PhiCandidate * phi_candidate)605 void SSARewriter::FinalizePhiCandidate(PhiCandidate* phi_candidate) {
606   assert(phi_candidate->phi_args().size() > 0 &&
607          "Phi candidate should have arguments");
608 
609   uint32_t ix = 0;
610   for (uint32_t pred : pass_->cfg()->preds(phi_candidate->bb()->id())) {
611     BasicBlock* pred_bb = pass_->cfg()->block(pred);
612     uint32_t& arg_id = phi_candidate->phi_args()[ix++];
613     if (arg_id == 0) {
614       // If |pred_bb| is still not sealed, it means it's unreachable. In this
615       // case, we just use Undef as an argument.
616       arg_id = IsBlockSealed(pred_bb)
617                    ? GetReachingDef(phi_candidate->var_id(), pred_bb)
618                    : pass_->GetUndefVal(phi_candidate->var_id());
619     }
620   }
621 
622   // This candidate is now completed.
623   phi_candidate->MarkComplete();
624 
625   // If |phi_candidate| is not trivial, add it to the list of Phis to
626   // generate.
627   if (TryRemoveTrivialPhi(phi_candidate) == phi_candidate->result_id()) {
628     // If we could not remove |phi_candidate|, it means that it is complete
629     // and not trivial. Add it to the list of Phis to generate.
630     assert(!phi_candidate->copy_of() && "A completed Phi cannot be trivial.");
631     phis_to_generate_.push_back(phi_candidate);
632   }
633 }
634 
FinalizePhiCandidates()635 void SSARewriter::FinalizePhiCandidates() {
636 #if SSA_REWRITE_DEBUGGING_LEVEL > 1
637   std::cerr << "Finalizing Phi candidates:\n\n";
638   PrintPhiCandidates();
639   std::cerr << "\n";
640 #endif
641 
642   // Now, complete the collected candidates.
643   while (incomplete_phis_.size() > 0) {
644     PhiCandidate* phi_candidate = incomplete_phis_.front();
645     incomplete_phis_.pop();
646     FinalizePhiCandidate(phi_candidate);
647   }
648 }
649 
RewriteFunctionIntoSSA(Function * fp)650 Pass::Status SSARewriter::RewriteFunctionIntoSSA(Function* fp) {
651 #if SSA_REWRITE_DEBUGGING_LEVEL > 0
652   std::cerr << "Function before SSA rewrite:\n"
653             << fp->PrettyPrint(0) << "\n\n\n";
654 #endif
655 
656   // Collect variables that can be converted into SSA IDs.
657   pass_->CollectTargetVars(fp);
658 
659   // Generate all the SSA replacements and Phi candidates. This will
660   // generate incomplete and trivial Phis.
661   bool succeeded = pass_->cfg()->WhileEachBlockInReversePostOrder(
662       fp->entry().get(), [this](BasicBlock* bb) {
663         if (!GenerateSSAReplacements(bb)) {
664           return false;
665         }
666         return true;
667       });
668 
669   if (!succeeded) {
670     return Pass::Status::Failure;
671   }
672 
673   // Remove trivial Phis and add arguments to incomplete Phis.
674   FinalizePhiCandidates();
675 
676   // Finally, apply all the replacements in the IR.
677   bool modified = ApplyReplacements();
678 
679 #if SSA_REWRITE_DEBUGGING_LEVEL > 0
680   std::cerr << "\n\n\nFunction after SSA rewrite:\n"
681             << fp->PrettyPrint(0) << "\n";
682 #endif
683 
684   return modified ? Pass::Status::SuccessWithChange
685                   : Pass::Status::SuccessWithoutChange;
686 }
687 
Process()688 Pass::Status SSARewritePass::Process() {
689   Status status = Status::SuccessWithoutChange;
690   for (auto& fn : *get_module()) {
691     if (fn.IsDeclaration()) {
692       continue;
693     }
694     status =
695         CombineStatus(status, SSARewriter(this).RewriteFunctionIntoSSA(&fn));
696     // Kill DebugDeclares for target variables.
697     for (auto var_id : seen_target_vars_) {
698       context()->get_debug_info_mgr()->KillDebugDeclares(var_id);
699     }
700     if (status == Status::Failure) {
701       break;
702     }
703   }
704   return status;
705 }
706 
707 }  // namespace opt
708 }  // namespace spvtools
709