/src/spirv-tools/source/opt/licm_pass.cpp
Line | Count | Source (jump to first uncovered line) |
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/licm_pass.h" |
16 | | |
17 | | #include <queue> |
18 | | #include <utility> |
19 | | |
20 | | #include "source/opt/module.h" |
21 | | #include "source/opt/pass.h" |
22 | | |
23 | | namespace spvtools { |
24 | | namespace opt { |
25 | | |
26 | 0 | Pass::Status LICMPass::Process() { return ProcessIRContext(); } |
27 | | |
28 | 0 | Pass::Status LICMPass::ProcessIRContext() { |
29 | 0 | Status status = Status::SuccessWithoutChange; |
30 | 0 | Module* module = get_module(); |
31 | | |
32 | | // Process each function in the module |
33 | 0 | for (auto func = module->begin(); |
34 | 0 | func != module->end() && status != Status::Failure; ++func) { |
35 | 0 | status = CombineStatus(status, ProcessFunction(&*func)); |
36 | 0 | } |
37 | 0 | return status; |
38 | 0 | } |
39 | | |
40 | 0 | Pass::Status LICMPass::ProcessFunction(Function* f) { |
41 | 0 | Status status = Status::SuccessWithoutChange; |
42 | 0 | LoopDescriptor* loop_descriptor = context()->GetLoopDescriptor(f); |
43 | | |
44 | | // Process each loop in the function |
45 | 0 | for (auto it = loop_descriptor->begin(); |
46 | 0 | it != loop_descriptor->end() && status != Status::Failure; ++it) { |
47 | 0 | Loop& loop = *it; |
48 | | // Ignore nested loops, as we will process them in order in ProcessLoop |
49 | 0 | if (loop.IsNested()) { |
50 | 0 | continue; |
51 | 0 | } |
52 | 0 | status = CombineStatus(status, ProcessLoop(&loop, f)); |
53 | 0 | } |
54 | 0 | return status; |
55 | 0 | } |
56 | | |
57 | 0 | Pass::Status LICMPass::ProcessLoop(Loop* loop, Function* f) { |
58 | 0 | Status status = Status::SuccessWithoutChange; |
59 | | |
60 | | // Process all nested loops first |
61 | 0 | for (auto nl = loop->begin(); nl != loop->end() && status != Status::Failure; |
62 | 0 | ++nl) { |
63 | 0 | Loop* nested_loop = *nl; |
64 | 0 | status = CombineStatus(status, ProcessLoop(nested_loop, f)); |
65 | 0 | } |
66 | |
|
67 | 0 | std::vector<BasicBlock*> loop_bbs{}; |
68 | 0 | status = CombineStatus( |
69 | 0 | status, |
70 | 0 | AnalyseAndHoistFromBB(loop, f, loop->GetHeaderBlock(), &loop_bbs)); |
71 | |
|
72 | 0 | for (size_t i = 0; i < loop_bbs.size() && status != Status::Failure; ++i) { |
73 | 0 | BasicBlock* bb = loop_bbs[i]; |
74 | | // do not delete the element |
75 | 0 | status = |
76 | 0 | CombineStatus(status, AnalyseAndHoistFromBB(loop, f, bb, &loop_bbs)); |
77 | 0 | } |
78 | |
|
79 | 0 | return status; |
80 | 0 | } |
81 | | |
82 | | Pass::Status LICMPass::AnalyseAndHoistFromBB( |
83 | | Loop* loop, Function* f, BasicBlock* bb, |
84 | 0 | std::vector<BasicBlock*>* loop_bbs) { |
85 | 0 | bool modified = false; |
86 | 0 | std::function<bool(Instruction*)> hoist_inst = |
87 | 0 | [this, &loop, &modified](Instruction* inst) { |
88 | 0 | if (loop->ShouldHoistInstruction(this->context(), inst)) { |
89 | 0 | if (!HoistInstruction(loop, inst)) { |
90 | 0 | return false; |
91 | 0 | } |
92 | 0 | modified = true; |
93 | 0 | } |
94 | 0 | return true; |
95 | 0 | }; |
96 | |
|
97 | 0 | if (IsImmediatelyContainedInLoop(loop, f, bb)) { |
98 | 0 | if (!bb->WhileEachInst(hoist_inst, false)) { |
99 | 0 | return Status::Failure; |
100 | 0 | } |
101 | 0 | } |
102 | | |
103 | 0 | DominatorAnalysis* dom_analysis = context()->GetDominatorAnalysis(f); |
104 | 0 | DominatorTree& dom_tree = dom_analysis->GetDomTree(); |
105 | |
|
106 | 0 | for (DominatorTreeNode* child_dom_tree_node : *dom_tree.GetTreeNode(bb)) { |
107 | 0 | if (loop->IsInsideLoop(child_dom_tree_node->bb_)) { |
108 | 0 | loop_bbs->push_back(child_dom_tree_node->bb_); |
109 | 0 | } |
110 | 0 | } |
111 | |
|
112 | 0 | return (modified ? Status::SuccessWithChange : Status::SuccessWithoutChange); |
113 | 0 | } |
114 | | |
115 | | bool LICMPass::IsImmediatelyContainedInLoop(Loop* loop, Function* f, |
116 | 0 | BasicBlock* bb) { |
117 | 0 | LoopDescriptor* loop_descriptor = context()->GetLoopDescriptor(f); |
118 | 0 | return loop == (*loop_descriptor)[bb->id()]; |
119 | 0 | } |
120 | | |
121 | 0 | bool LICMPass::HoistInstruction(Loop* loop, Instruction* inst) { |
122 | | // TODO(1841): Handle failure to create pre-header. |
123 | 0 | BasicBlock* pre_header_bb = loop->GetOrCreatePreHeaderBlock(); |
124 | 0 | if (!pre_header_bb) { |
125 | 0 | return false; |
126 | 0 | } |
127 | 0 | Instruction* insertion_point = &*pre_header_bb->tail(); |
128 | 0 | Instruction* previous_node = insertion_point->PreviousNode(); |
129 | 0 | if (previous_node && (previous_node->opcode() == spv::Op::OpLoopMerge || |
130 | 0 | previous_node->opcode() == spv::Op::OpSelectionMerge)) { |
131 | 0 | insertion_point = previous_node; |
132 | 0 | } |
133 | |
|
134 | 0 | inst->InsertBefore(insertion_point); |
135 | 0 | context()->set_instr_block(inst, pre_header_bb); |
136 | 0 | return true; |
137 | 0 | } |
138 | | |
139 | | } // namespace opt |
140 | | } // namespace spvtools |