2017-06-08 16:37:21 +00: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-08-03 12:05:33 +00:00
|
|
|
#ifndef SOURCE_OPT_AGGRESSIVE_DEAD_CODE_ELIM_PASS_H_
|
|
|
|
#define SOURCE_OPT_AGGRESSIVE_DEAD_CODE_ELIM_PASS_H_
|
2017-06-08 16:37:21 +00:00
|
|
|
|
|
|
|
#include <algorithm>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <list>
|
2017-06-08 16:37:21 +00:00
|
|
|
#include <map>
|
|
|
|
#include <queue>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <string>
|
2017-06-08 16:37:21 +00:00
|
|
|
#include <unordered_map>
|
|
|
|
#include <unordered_set>
|
|
|
|
#include <utility>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <vector>
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2018-08-03 19:06:09 +00: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"
|
|
|
|
#include "source/util/bit_vector.h"
|
2017-06-08 16:37:21 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
|
|
|
namespace opt {
|
|
|
|
|
|
|
|
// See optimizer.hpp for documentation.
|
2017-07-27 19:30:12 +00:00
|
|
|
class AggressiveDCEPass : public MemPass {
|
2018-07-12 19:14:43 +00:00
|
|
|
using cbb_ptr = const BasicBlock*;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
|
|
|
public:
|
2017-11-08 17:40:02 +00:00
|
|
|
using GetBlocksFunction =
|
2018-07-12 19:14:43 +00:00
|
|
|
std::function<std::vector<BasicBlock*>*(const BasicBlock*)>;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
|
|
|
AggressiveDCEPass();
|
2017-08-30 18:19:22 +00:00
|
|
|
const char* name() const override { return "eliminate-dead-code-aggressive"; }
|
2018-07-12 13:08:45 +00:00
|
|
|
Status Process() override;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
IRContext::Analysis GetPreservedAnalyses() override {
|
2018-12-20 18:00:05 +00:00
|
|
|
return IRContext::kAnalysisDefUse |
|
|
|
|
IRContext::kAnalysisInstrToBlockMapping |
|
|
|
|
IRContext::kAnalysisConstants | IRContext::kAnalysisTypes;
|
2017-11-09 16:24:41 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
private:
|
2017-12-11 18:10:24 +00:00
|
|
|
// Return true if |varId| is a variable of |storageClass|. |varId| must either
|
|
|
|
// be 0 or the result of an instruction.
|
2017-10-10 20:35:53 +00:00
|
|
|
bool IsVarOfStorage(uint32_t varId, uint32_t storageClass);
|
|
|
|
|
|
|
|
// Return true if |varId| is variable of function storage class or is
|
|
|
|
// private variable and privates can be optimized like locals (see
|
2017-12-18 17:13:10 +00:00
|
|
|
// privates_like_local_).
|
2017-10-10 20:35:53 +00:00
|
|
|
bool IsLocalVar(uint32_t varId);
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Return true if |inst| is marked live.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsLive(const Instruction* inst) const {
|
2018-04-12 15:00:58 +00:00
|
|
|
return live_insts_.Get(inst->unique_id());
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Returns true if |inst| is dead.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsDead(Instruction* inst);
|
2017-12-18 17:13:10 +00:00
|
|
|
|
2018-03-13 17:48:48 +00:00
|
|
|
// Adds entry points, execution modes and workgroup size decorations to the
|
|
|
|
// worklist for processing with the first function.
|
2017-12-18 17:13:10 +00:00
|
|
|
void InitializeModuleScopeLiveInstructions();
|
|
|
|
|
2017-10-17 22:33:43 +00:00
|
|
|
// Add |inst| to worklist_ and live_insts_.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AddToWorklist(Instruction* inst) {
|
2018-04-12 15:00:58 +00:00
|
|
|
if (!live_insts_.Set(inst->unique_id())) {
|
|
|
|
worklist_.push(inst);
|
|
|
|
}
|
2017-10-17 22:33:43 +00:00
|
|
|
}
|
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
// Add all store instruction which use |ptrId|, directly or indirectly,
|
|
|
|
// to the live instruction worklist.
|
|
|
|
void AddStores(uint32_t ptrId);
|
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
// Initialize extensions whitelist
|
|
|
|
void InitExtensions();
|
|
|
|
|
2017-07-10 15:45:59 +00:00
|
|
|
// Return true if all extensions in this module are supported by this pass.
|
2017-07-19 00:57:26 +00:00
|
|
|
bool AllExtensionsSupported() const;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Returns true if the target of |inst| is dead. An instruction is dead if
|
|
|
|
// its result id is used in decoration or debug instructions only. |inst| is
|
|
|
|
// assumed to be OpName, OpMemberName or an annotation instruction.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsTargetDead(Instruction* inst);
|
2017-08-04 21:04:37 +00:00
|
|
|
|
|
|
|
// If |varId| is local, mark all stores of varId as live.
|
|
|
|
void ProcessLoad(uint32_t varId);
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2018-01-15 18:25:45 +00:00
|
|
|
// If |bp| is structured header block, returns true and sets |mergeInst| to
|
|
|
|
// the merge instruction, |branchInst| to the branch and |mergeBlockId| to the
|
|
|
|
// merge block if they are not nullptr. Any of |mergeInst|, |branchInst| or
|
|
|
|
// |mergeBlockId| may be a null pointer. Returns false if |bp| is a null
|
|
|
|
// pointer.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsStructuredHeader(BasicBlock* bp, Instruction** mergeInst,
|
|
|
|
Instruction** branchInst, uint32_t* mergeBlockId);
|
2017-10-17 22:33:43 +00:00
|
|
|
|
2018-10-16 12:00:07 +00:00
|
|
|
// Initialize block2headerBranch_, header2nextHeaderBranch_, and
|
|
|
|
// branch2merge_ using |structuredOrder| to order blocks.
|
2018-07-12 19:14:43 +00:00
|
|
|
void ComputeBlock2HeaderMaps(std::list<BasicBlock*>& structuredOrder);
|
2017-10-17 22:33:43 +00:00
|
|
|
|
|
|
|
// Add branch to |labelId| to end of block |bp|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AddBranch(uint32_t labelId, BasicBlock* bp);
|
2017-10-17 22:33:43 +00:00
|
|
|
|
2018-08-21 15:54:44 +00:00
|
|
|
// Add all break and continue branches in the construct associated with
|
2017-12-12 21:27:46 +00:00
|
|
|
// |mergeInst| to worklist if not already live
|
2018-07-12 19:14:43 +00:00
|
|
|
void AddBreaksAndContinuesToWorklist(Instruction* mergeInst);
|
2017-11-28 23:18:05 +00:00
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// Eliminates dead debug2 and annotation instructions. Marks dead globals for
|
|
|
|
// removal (e.g. types, constants and variables).
|
|
|
|
bool ProcessGlobalValues();
|
|
|
|
|
|
|
|
// Erases functions that are unreachable from the entry points of the module.
|
|
|
|
bool EliminateDeadFunctions();
|
|
|
|
|
|
|
|
// Removes |func| from the module and deletes all its instructions.
|
2018-07-12 19:14:43 +00:00
|
|
|
void EliminateFunction(Function* func);
|
2017-12-18 17:13:10 +00:00
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
// For function |func|, mark all Stores to non-function-scope variables
|
|
|
|
// and block terminating instructions as live. Recursively mark the values
|
2017-12-18 17:13:10 +00:00
|
|
|
// they use. When complete, mark any non-live instructions to be deleted.
|
|
|
|
// Returns true if the function has been modified.
|
2017-11-08 17:40:02 +00:00
|
|
|
//
|
2017-06-08 16:37:21 +00:00
|
|
|
// Note: This function does not delete useless control structures. All
|
|
|
|
// existing control structures will remain. This can leave not-insignificant
|
|
|
|
// sequences of ultimately useless code.
|
|
|
|
// TODO(): Remove useless control constructs.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool AggressiveDCE(Function* func);
|
2017-06-08 16:37:21 +00:00
|
|
|
|
|
|
|
Pass::Status ProcessImpl();
|
|
|
|
|
2017-10-10 20:35:53 +00:00
|
|
|
// True if current function has a call instruction contained in it
|
|
|
|
bool call_in_func_;
|
|
|
|
|
|
|
|
// True if current function is an entry point
|
|
|
|
bool func_is_entry_point_;
|
|
|
|
|
|
|
|
// True if current function is entry point and has no function calls.
|
|
|
|
bool private_like_local_;
|
|
|
|
|
2017-07-10 15:45:59 +00:00
|
|
|
// Live Instruction Worklist. An instruction is added to this list
|
|
|
|
// if it might have a side effect, either directly or indirectly.
|
|
|
|
// If we don't know, then add it to this list. Instructions are
|
|
|
|
// removed from this list as the algorithm traces side effects,
|
|
|
|
// building up the live instructions set |live_insts_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::queue<Instruction*> worklist_;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-10-17 22:33:43 +00:00
|
|
|
// Map from block to the branch instruction in the header of the most
|
2017-12-12 21:27:46 +00:00
|
|
|
// immediate controlling structured if or loop. A loop header block points
|
|
|
|
// to its own branch instruction. An if-selection block points to the branch
|
|
|
|
// of an enclosing construct's header, if one exists.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<BasicBlock*, Instruction*> block2headerBranch_;
|
2017-10-17 22:33:43 +00:00
|
|
|
|
2018-10-16 12:00:07 +00:00
|
|
|
// Map from header block to the branch instruction in the header of the
|
|
|
|
// structured construct enclosing it.
|
|
|
|
// The liveness algorithm is designed to iteratively mark as live all
|
|
|
|
// structured constructs enclosing a live instruction.
|
|
|
|
std::unordered_map<BasicBlock*, Instruction*> header2nextHeaderBranch_;
|
|
|
|
|
2018-01-18 20:44:00 +00:00
|
|
|
// Maps basic block to their index in the structured order traversal.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<BasicBlock*, uint32_t> structured_order_index_;
|
2018-01-18 20:44:00 +00:00
|
|
|
|
2017-12-12 21:27:46 +00:00
|
|
|
// Map from branch to its associated merge instruction, if any
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<Instruction*, Instruction*> branch2merge_;
|
2017-10-17 22:33:43 +00:00
|
|
|
|
2017-10-10 20:35:53 +00:00
|
|
|
// Store instructions to variables of private storage
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> private_stores_;
|
2017-10-10 20:35:53 +00:00
|
|
|
|
2017-06-08 16:37:21 +00:00
|
|
|
// Live Instructions
|
2018-04-12 15:00:58 +00:00
|
|
|
utils::BitVector live_insts_;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
|
|
|
// Live Local Variables
|
|
|
|
std::unordered_set<uint32_t> live_local_vars_;
|
|
|
|
|
2017-12-18 17:13:10 +00:00
|
|
|
// List of instructions to delete. Deletion is delayed until debug and
|
|
|
|
// annotation instructions are processed.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::vector<Instruction*> to_kill_;
|
2017-06-08 16:37:21 +00:00
|
|
|
|
2017-07-19 00:57:26 +00:00
|
|
|
// Extensions supported by this pass.
|
|
|
|
std::unordered_set<std::string> extensions_whitelist_;
|
2017-06-08 16:37:21 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
} // namespace opt
|
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_AGGRESSIVE_DEAD_CODE_ELIM_PASS_H_
|