• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2017 The Khronos Group Inc.
2 // Copyright (c) 2017 Valve Corporation
3 // Copyright (c) 2017 LunarG Inc.
4 // Copyright (c) 2019 Google LLC
5 //
6 // Licensed under the Apache License, Version 2.0 (the "License");
7 // you may not use this file except in compliance with the License.
8 // You may obtain a copy of the License at
9 //
10 //     http://www.apache.org/licenses/LICENSE-2.0
11 //
12 // Unless required by applicable law or agreed to in writing, software
13 // distributed under the License is distributed on an "AS IS" BASIS,
14 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 // See the License for the specific language governing permissions and
16 // limitations under the License.
17 
18 #include "block_merge_util.h"
19 
20 namespace spvtools {
21 namespace opt {
22 namespace blockmergeutil {
23 namespace {
24 
25 // Returns true if |block| contains a merge instruction.
IsHeader(BasicBlock * block)26 bool IsHeader(BasicBlock* block) { return block->GetMergeInst() != nullptr; }
27 
28 // Returns true if |id| contains a merge instruction.
IsHeader(IRContext * context,uint32_t id)29 bool IsHeader(IRContext* context, uint32_t id) {
30   return IsHeader(
31       context->get_instr_block(context->get_def_use_mgr()->GetDef(id)));
32 }
33 
34 // Returns true if |id| is the merge target of a merge instruction.
IsMerge(IRContext * context,uint32_t id)35 bool IsMerge(IRContext* context, uint32_t id) {
36   return !context->get_def_use_mgr()->WhileEachUse(
37       id, [](Instruction* user, uint32_t index) {
38         spv::Op op = user->opcode();
39         if ((op == spv::Op::OpLoopMerge || op == spv::Op::OpSelectionMerge) &&
40             index == 0u) {
41           return false;
42         }
43         return true;
44       });
45 }
46 
47 // Returns true if |block| is the merge target of a merge instruction.
IsMerge(IRContext * context,BasicBlock * block)48 bool IsMerge(IRContext* context, BasicBlock* block) {
49   return IsMerge(context, block->id());
50 }
51 
52 // Returns true if |id| is the continue target of a merge instruction.
IsContinue(IRContext * context,uint32_t id)53 bool IsContinue(IRContext* context, uint32_t id) {
54   return !context->get_def_use_mgr()->WhileEachUse(
55       id, [](Instruction* user, uint32_t index) {
56         spv::Op op = user->opcode();
57         if (op == spv::Op::OpLoopMerge && index == 1u) {
58           return false;
59         }
60         return true;
61       });
62 }
63 
64 // Removes any OpPhi instructions in |block|, which should have exactly one
65 // predecessor, replacing uses of OpPhi ids with the ids associated with the
66 // predecessor.
EliminateOpPhiInstructions(IRContext * context,BasicBlock * block)67 void EliminateOpPhiInstructions(IRContext* context, BasicBlock* block) {
68   block->ForEachPhiInst([context](Instruction* phi) {
69     assert(2 == phi->NumInOperands() &&
70            "A block can only have one predecessor for block merging to make "
71            "sense.");
72     context->ReplaceAllUsesWith(phi->result_id(),
73                                 phi->GetSingleWordInOperand(0));
74     context->KillInst(phi);
75   });
76 }
77 
78 }  // Anonymous namespace
79 
CanMergeWithSuccessor(IRContext * context,BasicBlock * block)80 bool CanMergeWithSuccessor(IRContext* context, BasicBlock* block) {
81   // Find block with single successor which has no other predecessors.
82   auto ii = block->end();
83   --ii;
84   Instruction* br = &*ii;
85   if (br->opcode() != spv::Op::OpBranch) {
86     return false;
87   }
88 
89   const uint32_t lab_id = br->GetSingleWordInOperand(0);
90   if (context->cfg()->preds(lab_id).size() != 1) {
91     return false;
92   }
93 
94   bool pred_is_merge = IsMerge(context, block);
95   bool succ_is_merge = IsMerge(context, lab_id);
96   if (pred_is_merge && succ_is_merge) {
97     // Cannot merge two merges together.
98     return false;
99   }
100 
101   // Note: This means that the instructions in a break block will execute as if
102   // they were still diverged according to the loop iteration. This restricts
103   // potential transformations an implementation may perform on the IR to match
104   // shader author expectations. Similarly, instructions in the loop construct
105   // cannot be moved into the continue construct unless it can be proven that
106   // invocations are always converged.
107   if (succ_is_merge && context->get_feature_mgr()->HasExtension(
108                            kSPV_KHR_maximal_reconvergence)) {
109     return false;
110   }
111 
112   if (pred_is_merge && IsContinue(context, lab_id)) {
113     // Cannot merge a continue target with a merge block.
114     return false;
115   }
116 
117   Instruction* merge_inst = block->GetMergeInst();
118   const bool pred_is_header = IsHeader(block);
119   if (pred_is_header && lab_id != merge_inst->GetSingleWordInOperand(0u)) {
120     bool succ_is_header = IsHeader(context, lab_id);
121     if (pred_is_header && succ_is_header) {
122       // Cannot merge two headers together when the successor is not the merge
123       // block of the predecessor.
124       return false;
125     }
126 
127     // If this is a header block and the successor is not its merge, we must
128     // be careful about which blocks we are willing to merge together.
129     // OpLoopMerge must be followed by a conditional or unconditional branch.
130     // The merge must be a loop merge because a selection merge cannot be
131     // followed by an unconditional branch.
132     BasicBlock* succ_block = context->get_instr_block(lab_id);
133     spv::Op succ_term_op = succ_block->terminator()->opcode();
134     assert(merge_inst->opcode() == spv::Op::OpLoopMerge);
135     if (succ_term_op != spv::Op::OpBranch &&
136         succ_term_op != spv::Op::OpBranchConditional) {
137       return false;
138     }
139   }
140 
141   if (succ_is_merge || IsContinue(context, lab_id)) {
142     auto* struct_cfg = context->GetStructuredCFGAnalysis();
143     auto switch_block_id = struct_cfg->ContainingSwitch(block->id());
144     if (switch_block_id) {
145       auto switch_merge_id = struct_cfg->SwitchMergeBlock(switch_block_id);
146       const auto* switch_inst =
147           &*block->GetParent()->FindBlock(switch_block_id)->tail();
148       for (uint32_t i = 1; i < switch_inst->NumInOperands(); i += 2) {
149         auto target_id = switch_inst->GetSingleWordInOperand(i);
150         if (target_id == block->id() && target_id != switch_merge_id) {
151           // Case constructs must be structurally dominated by the OpSwitch.
152           // Since the successor is the merge/continue for another construct,
153           // merging the blocks would break that requirement.
154           return false;
155         }
156       }
157     }
158   }
159 
160   return true;
161 }
162 
MergeWithSuccessor(IRContext * context,Function * func,Function::iterator bi)163 void MergeWithSuccessor(IRContext* context, Function* func,
164                         Function::iterator bi) {
165   assert(CanMergeWithSuccessor(context, &*bi) &&
166          "Precondition failure for MergeWithSuccessor: it must be legal to "
167          "merge the block and its successor.");
168 
169   auto ii = bi->end();
170   --ii;
171   Instruction* br = &*ii;
172   const uint32_t lab_id = br->GetSingleWordInOperand(0);
173   Instruction* merge_inst = bi->GetMergeInst();
174   bool pred_is_header = IsHeader(&*bi);
175 
176   // Merge blocks.
177   context->KillInst(br);
178   auto sbi = bi;
179   for (; sbi != func->end(); ++sbi)
180     if (sbi->id() == lab_id) break;
181   // If bi is sbi's only predecessor, it dominates sbi and thus
182   // sbi must follow bi in func's ordering.
183   assert(sbi != func->end());
184 
185   if (sbi->tail()->opcode() == spv::Op::OpSwitch &&
186       sbi->MergeBlockIdIfAny() != 0) {
187     context->InvalidateAnalyses(IRContext::Analysis::kAnalysisStructuredCFG);
188   }
189 
190   // Update the inst-to-block mapping for the instructions in sbi.
191   for (auto& inst : *sbi) {
192     context->set_instr_block(&inst, &*bi);
193   }
194 
195   EliminateOpPhiInstructions(context, &*sbi);
196 
197   // Now actually move the instructions.
198   bi->AddInstructions(&*sbi);
199 
200   if (merge_inst) {
201     if (pred_is_header && lab_id == merge_inst->GetSingleWordInOperand(0u)) {
202       // Merging the header and merge blocks, so remove the structured control
203       // flow declaration.
204       context->KillInst(merge_inst);
205     } else {
206       // Move OpLine/OpNoLine information to merge_inst. This solves
207       // the validation error that OpLine is placed between OpLoopMerge
208       // and OpBranchConditional.
209       auto terminator = bi->terminator();
210       auto& vec = terminator->dbg_line_insts();
211       if (vec.size() > 0) {
212         merge_inst->ClearDbgLineInsts();
213         auto& new_vec = merge_inst->dbg_line_insts();
214         new_vec.insert(new_vec.end(), vec.begin(), vec.end());
215         terminator->ClearDbgLineInsts();
216         for (auto& l_inst : new_vec)
217           context->get_def_use_mgr()->AnalyzeInstDefUse(&l_inst);
218       }
219       // Clear debug scope of terminator to avoid DebugScope
220       // emitted between terminator and merge.
221       terminator->SetDebugScope(DebugScope(kNoDebugScope, kNoInlinedAt));
222       // Move the merge instruction to just before the terminator.
223       merge_inst->InsertBefore(terminator);
224     }
225   }
226   context->ReplaceAllUsesWith(lab_id, bi->id());
227   context->KillInst(sbi->GetLabelInst());
228   (void)sbi.Erase();
229 }
230 
231 }  // namespace blockmergeutil
232 }  // namespace opt
233 }  // namespace spvtools
234