• 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 //
5 // Licensed under the Apache License, Version 2.0 (the "License");
6 // you may not use this file except in compliance with the License.
7 // You may obtain a copy of the License at
8 //
9 //     http://www.apache.org/licenses/LICENSE-2.0
10 //
11 // Unless required by applicable law or agreed to in writing, software
12 // distributed under the License is distributed on an "AS IS" BASIS,
13 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14 // See the License for the specific language governing permissions and
15 // limitations under the License.
16 
17 #ifndef SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_
18 #define SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_
19 
20 #include <algorithm>
21 #include <map>
22 #include <queue>
23 #include <unordered_map>
24 #include <unordered_set>
25 #include <utility>
26 #include <vector>
27 
28 #include "source/opt/basic_block.h"
29 #include "source/opt/def_use_manager.h"
30 #include "source/opt/mem_pass.h"
31 #include "source/opt/module.h"
32 
33 namespace spvtools {
34 namespace opt {
35 
36 // See optimizer.hpp for documentation.
37 class DeadBranchElimPass : public MemPass {
38   using cbb_ptr = const BasicBlock*;
39 
40  public:
41   DeadBranchElimPass() = default;
42 
name()43   const char* name() const override { return "eliminate-dead-branches"; }
44   Status Process() override;
45 
GetPreservedAnalyses()46   IRContext::Analysis GetPreservedAnalyses() override {
47     return IRContext::kAnalysisDefUse | IRContext::kAnalysisInstrToBlockMapping;
48   }
49 
50  private:
51   // If |condId| is boolean constant, return conditional value in |condVal| and
52   // return true, otherwise return false.
53   bool GetConstCondition(uint32_t condId, bool* condVal);
54 
55   // If |valId| is a 32-bit integer constant, return value via |value| and
56   // return true, otherwise return false.
57   bool GetConstInteger(uint32_t valId, uint32_t* value);
58 
59   // Add branch to |labelId| to end of block |bp|.
60   void AddBranch(uint32_t labelId, BasicBlock* bp);
61 
62   // For function |func|, look for BranchConditionals with constant condition
63   // and convert to a Branch to the indicated label. Delete resulting dead
64   // blocks. Note some such branches and blocks may be left to avoid creating
65   // invalid control flow.
66   // TODO(greg-lunarg): Remove remaining constant conditional branches and dead
67   // blocks.
68   bool EliminateDeadBranches(Function* func);
69 
70   // Returns the basic block containing |id|.
71   // Note: this pass only requires correct instruction block mappings for the
72   // input. This pass does not preserve the block mapping, so it is not kept
73   // up-to-date during processing.
74   BasicBlock* GetParentBlock(uint32_t id);
75 
76   // Marks live blocks reachable from the entry of |func|. Simplifies constant
77   // branches and switches as it proceeds, to limit the number of live blocks.
78   // It is careful not to eliminate backedges even if they are dead, but the
79   // header is live. Likewise, unreachable merge blocks named in live merge
80   // instruction must be retained (though they may be clobbered).
81   bool MarkLiveBlocks(Function* func,
82                       std::unordered_set<BasicBlock*>* live_blocks);
83 
84   // Checks for unreachable merge and continue blocks with live headers; those
85   // blocks must be retained. Continues are tracked separately so that a live
86   // phi can be updated to take an undef value from any of its predecessors
87   // that are unreachable continues.
88   //
89   // |unreachable_continues| maps the id of an unreachable continue target to
90   // the header block that declares it.
91   void MarkUnreachableStructuredTargets(
92       const std::unordered_set<BasicBlock*>& live_blocks,
93       std::unordered_set<BasicBlock*>* unreachable_merges,
94       std::unordered_map<BasicBlock*, BasicBlock*>* unreachable_continues);
95 
96   // Fix phis in reachable blocks so that only live (or unremovable) incoming
97   // edges are present. If the block now only has a single live incoming edge,
98   // remove the phi and replace its uses with its data input. If the single
99   // remaining incoming edge is from the phi itself, the the phi is in an
100   // unreachable single block loop. Either the block is dead and will be
101   // removed, or it's reachable from an unreachable continue target. In the
102   // latter case that continue target block will be collapsed into a block that
103   // only branches back to its header and we'll eliminate the block with the
104   // phi.
105   //
106   // |unreachable_continues| maps continue targets that cannot be reached to
107   // merge instruction that declares them.
108   bool FixPhiNodesInLiveBlocks(
109       Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
110       const std::unordered_map<BasicBlock*, BasicBlock*>&
111           unreachable_continues);
112 
113   // Erases dead blocks. Any block captured in |unreachable_merges| or
114   // |unreachable_continues| is a dead block that is required to remain due to
115   // a live merge instruction in the corresponding header. These blocks will
116   // have their instructions clobbered and will become a label and terminator.
117   // Unreachable merge blocks are terminated by OpUnreachable, while
118   // unreachable continue blocks are terminated by an unconditional branch to
119   // the header. Otherwise, blocks are dead if not explicitly captured in
120   // |live_blocks| and are totally removed.
121   //
122   // |unreachable_continues| maps continue targets that cannot be reached to
123   // corresponding header block that declares them.
124   bool EraseDeadBlocks(
125       Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
126       const std::unordered_set<BasicBlock*>& unreachable_merges,
127       const std::unordered_map<BasicBlock*, BasicBlock*>&
128           unreachable_continues);
129 
130   // Reorders blocks in reachable functions so that they satisfy dominator
131   // block ordering rules.
132   void FixBlockOrder();
133 
134   // Return the first branch instruction that is a conditional branch to
135   // |merge_block_id|. Returns |nullptr| if no such branch exists. If there are
136   // multiple such branches, the first one is the one that would be executed
137   // first when running the code.  That is, the one that dominates all of the
138   // others.
139   //
140   // |start_block_id| must be a block whose innermost containing merge construct
141   // has |merge_block_id| as the merge block.
142   //
143   // |loop_merge_id| and |loop_continue_id| are the merge and continue block ids
144   // of the innermost loop containing |start_block_id|.
145   Instruction* FindFirstExitFromSelectionMerge(uint32_t start_block_id,
146                                                uint32_t merge_block_id,
147                                                uint32_t loop_merge_id,
148                                                uint32_t loop_continue_id);
149 };
150 
151 }  // namespace opt
152 }  // namespace spvtools
153 
154 #endif  // SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_
155