bgfx/3rdparty/spirv-tools/source/opt/dead_branch_elim_pass.h

157 lines
6.7 KiB
C
Raw Normal View History

2018-04-11 05:44:28 +03:00
// Copyright (c) 2017 The Khronos Group Inc.
// Copyright (c) 2017 Valve Corporation
// Copyright (c) 2017 LunarG Inc.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
2018-09-03 07:14:20 +03:00
#ifndef SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_
#define SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_
2018-04-11 05:44:28 +03:00
#include <algorithm>
#include <map>
#include <queue>
#include <unordered_map>
#include <unordered_set>
#include <utility>
2018-09-03 07:14:20 +03:00
#include <vector>
2018-04-11 05:44:28 +03:00
2018-09-03 07:14:20 +03:00
#include "source/opt/basic_block.h"
#include "source/opt/def_use_manager.h"
#include "source/opt/mem_pass.h"
#include "source/opt/module.h"
2018-04-11 05:44:28 +03:00
namespace spvtools {
namespace opt {
// See optimizer.hpp for documentation.
class DeadBranchElimPass : public MemPass {
2018-09-03 07:14:20 +03:00
using cbb_ptr = const BasicBlock*;
2018-04-11 05:44:28 +03:00
public:
2018-09-03 07:14:20 +03:00
DeadBranchElimPass() = default;
2018-04-11 05:44:28 +03:00
const char* name() const override { return "eliminate-dead-branches"; }
2018-09-03 07:14:20 +03:00
Status Process() override;
2018-04-11 05:44:28 +03:00
2018-09-03 07:14:20 +03:00
IRContext::Analysis GetPreservedAnalyses() override {
2019-01-05 19:15:11 +03:00
return IRContext::kAnalysisDefUse |
IRContext::kAnalysisInstrToBlockMapping |
IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
2018-04-11 05:44:28 +03:00
}
private:
// If |condId| is boolean constant, return conditional value in |condVal| and
// return true, otherwise return false.
bool GetConstCondition(uint32_t condId, bool* condVal);
// If |valId| is a 32-bit integer constant, return value via |value| and
// return true, otherwise return false.
bool GetConstInteger(uint32_t valId, uint32_t* value);
// Add branch to |labelId| to end of block |bp|.
2018-09-03 07:14:20 +03:00
void AddBranch(uint32_t labelId, BasicBlock* bp);
2018-04-11 05:44:28 +03:00
// For function |func|, look for BranchConditionals with constant condition
// and convert to a Branch to the indicated label. Delete resulting dead
// blocks. Note some such branches and blocks may be left to avoid creating
// invalid control flow.
// TODO(greg-lunarg): Remove remaining constant conditional branches and dead
// blocks.
2018-09-03 07:14:20 +03:00
bool EliminateDeadBranches(Function* func);
2018-04-11 05:44:28 +03:00
// Returns the basic block containing |id|.
// Note: this pass only requires correct instruction block mappings for the
// input. This pass does not preserve the block mapping, so it is not kept
// up-to-date during processing.
2018-09-03 07:14:20 +03:00
BasicBlock* GetParentBlock(uint32_t id);
2018-04-11 05:44:28 +03:00
// Marks live blocks reachable from the entry of |func|. Simplifies constant
// branches and switches as it proceeds, to limit the number of live blocks.
// It is careful not to eliminate backedges even if they are dead, but the
// header is live. Likewise, unreachable merge blocks named in live merge
// instruction must be retained (though they may be clobbered).
2018-09-03 07:14:20 +03:00
bool MarkLiveBlocks(Function* func,
std::unordered_set<BasicBlock*>* live_blocks);
2018-04-11 05:44:28 +03:00
// Checks for unreachable merge and continue blocks with live headers; those
// blocks must be retained. Continues are tracked separately so that a live
// phi can be updated to take an undef value from any of its predecessors
// that are unreachable continues.
//
// |unreachable_continues| maps the id of an unreachable continue target to
// the header block that declares it.
void MarkUnreachableStructuredTargets(
2018-09-03 07:14:20 +03:00
const std::unordered_set<BasicBlock*>& live_blocks,
std::unordered_set<BasicBlock*>* unreachable_merges,
std::unordered_map<BasicBlock*, BasicBlock*>* unreachable_continues);
2018-04-11 05:44:28 +03:00
// Fix phis in reachable blocks so that only live (or unremovable) incoming
// edges are present. If the block now only has a single live incoming edge,
// remove the phi and replace its uses with its data input. If the single
// remaining incoming edge is from the phi itself, the the phi is in an
// unreachable single block loop. Either the block is dead and will be
// removed, or it's reachable from an unreachable continue target. In the
// latter case that continue target block will be collapsed into a block that
// only branches back to its header and we'll eliminate the block with the
// phi.
//
// |unreachable_continues| maps continue targets that cannot be reached to
// merge instruction that declares them.
bool FixPhiNodesInLiveBlocks(
2018-09-03 07:14:20 +03:00
Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
const std::unordered_map<BasicBlock*, BasicBlock*>&
2018-04-11 05:44:28 +03:00
unreachable_continues);
// Erases dead blocks. Any block captured in |unreachable_merges| or
// |unreachable_continues| is a dead block that is required to remain due to
// a live merge instruction in the corresponding header. These blocks will
// have their instructions clobbered and will become a label and terminator.
// Unreachable merge blocks are terminated by OpUnreachable, while
// unreachable continue blocks are terminated by an unconditional branch to
// the header. Otherwise, blocks are dead if not explicitly captured in
// |live_blocks| and are totally removed.
//
// |unreachable_continues| maps continue targets that cannot be reached to
// corresponding header block that declares them.
bool EraseDeadBlocks(
2018-09-03 07:14:20 +03:00
Function* func, const std::unordered_set<BasicBlock*>& live_blocks,
const std::unordered_set<BasicBlock*>& unreachable_merges,
const std::unordered_map<BasicBlock*, BasicBlock*>&
2018-04-11 05:44:28 +03:00
unreachable_continues);
2018-09-03 07:14:20 +03:00
// Reorders blocks in reachable functions so that they satisfy dominator
// block ordering rules.
void FixBlockOrder();
// Return the first branch instruction that is a conditional branch to
2019-01-05 19:15:11 +03:00
// |merge_block_id|. Returns |nullptr| if no such branch exists. If there are
2018-09-03 07:14:20 +03:00
// multiple such branches, the first one is the one that would be executed
// first when running the code. That is, the one that dominates all of the
// others.
//
// |start_block_id| must be a block whose innermost containing merge construct
// has |merge_block_id| as the merge block.
2019-01-05 19:15:11 +03:00
//
// |loop_merge_id| and |loop_continue_id| are the merge and continue block ids
// of the innermost loop containing |start_block_id|.
2018-09-03 07:14:20 +03:00
Instruction* FindFirstExitFromSelectionMerge(uint32_t start_block_id,
2019-01-05 19:15:11 +03:00
uint32_t merge_block_id,
uint32_t loop_merge_id,
uint32_t loop_continue_id);
2018-04-11 05:44:28 +03:00
};
} // namespace opt
} // namespace spvtools
2018-09-03 07:14:20 +03:00
#endif // SOURCE_OPT_DEAD_BRANCH_ELIM_PASS_H_