2017-10-30 15:13:24 +00:00
|
|
|
// Copyright (c) 2017 Google 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_IR_CONTEXT_H_
|
|
|
|
#define SOURCE_OPT_IR_CONTEXT_H_
|
2017-10-30 15:13:24 +00:00
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
#include <algorithm>
|
2017-10-30 15:13:24 +00:00
|
|
|
#include <iostream>
|
2017-11-14 19:11:50 +00:00
|
|
|
#include <limits>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <map>
|
|
|
|
#include <memory>
|
2018-11-29 19:24:58 +00:00
|
|
|
#include <queue>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <unordered_map>
|
2017-11-11 01:26:55 +00:00
|
|
|
#include <unordered_set>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <utility>
|
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "source/assembly_grammar.h"
|
|
|
|
#include "source/opt/cfg.h"
|
|
|
|
#include "source/opt/constants.h"
|
|
|
|
#include "source/opt/decoration_manager.h"
|
|
|
|
#include "source/opt/def_use_manager.h"
|
|
|
|
#include "source/opt/dominator_analysis.h"
|
|
|
|
#include "source/opt/feature_manager.h"
|
|
|
|
#include "source/opt/fold.h"
|
|
|
|
#include "source/opt/loop_descriptor.h"
|
|
|
|
#include "source/opt/module.h"
|
|
|
|
#include "source/opt/register_pressure.h"
|
|
|
|
#include "source/opt/scalar_analysis.h"
|
2018-10-10 19:18:15 +00:00
|
|
|
#include "source/opt/struct_cfg_analysis.h"
|
2018-08-03 19:06:09 +00:00
|
|
|
#include "source/opt/type_manager.h"
|
|
|
|
#include "source/opt/value_number_table.h"
|
2018-08-14 19:01:50 +00:00
|
|
|
#include "source/util/make_unique.h"
|
2017-10-30 15:13:24 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2017-10-30 15:13:24 +00:00
|
|
|
|
|
|
|
class IRContext {
|
|
|
|
public:
|
2017-11-10 14:39:00 +00:00
|
|
|
// Available analyses.
|
|
|
|
//
|
|
|
|
// When adding a new analysis:
|
|
|
|
//
|
|
|
|
// 1. Enum values should be powers of 2. These are cast into uint32_t
|
|
|
|
// bitmasks, so we can have at most 31 analyses represented.
|
|
|
|
//
|
|
|
|
// 2. Make sure it gets invalidated or preserved by IRContext methods that add
|
|
|
|
// or remove IR elements (e.g., KillDef, KillInst, ReplaceAllUsesWith).
|
|
|
|
//
|
2017-11-27 19:20:32 +00:00
|
|
|
// 3. Add handling code in BuildInvalidAnalyses and InvalidateAnalyses
|
2017-11-02 18:25:48 +00:00
|
|
|
enum Analysis {
|
2017-11-10 14:39:00 +00:00
|
|
|
kAnalysisNone = 0 << 0,
|
|
|
|
kAnalysisBegin = 1 << 0,
|
2017-11-02 18:25:48 +00:00
|
|
|
kAnalysisDefUse = kAnalysisBegin,
|
2017-11-10 14:39:00 +00:00
|
|
|
kAnalysisInstrToBlockMapping = 1 << 1,
|
2017-11-13 20:31:43 +00:00
|
|
|
kAnalysisDecorations = 1 << 2,
|
2017-11-11 01:26:55 +00:00
|
|
|
kAnalysisCombinators = 1 << 3,
|
2017-11-27 19:20:32 +00:00
|
|
|
kAnalysisCFG = 1 << 4,
|
2017-11-27 21:21:26 +00:00
|
|
|
kAnalysisDominatorAnalysis = 1 << 5,
|
2018-01-25 10:33:06 +00:00
|
|
|
kAnalysisLoopAnalysis = 1 << 6,
|
2018-02-13 04:28:38 +00:00
|
|
|
kAnalysisNameMap = 1 << 7,
|
2018-03-28 13:19:55 +00:00
|
|
|
kAnalysisScalarEvolution = 1 << 8,
|
2018-04-17 14:31:09 +00:00
|
|
|
kAnalysisRegisterPressure = 1 << 9,
|
2018-04-26 19:07:48 +00:00
|
|
|
kAnalysisValueNumberTable = 1 << 10,
|
2018-10-10 19:18:15 +00:00
|
|
|
kAnalysisStructuredCFG = 1 << 11,
|
2018-11-08 18:54:54 +00:00
|
|
|
kAnalysisBuiltinVarId = 1 << 12,
|
2018-11-29 19:24:58 +00:00
|
|
|
kAnalysisIdToFuncMapping = 1 << 13,
|
|
|
|
kAnalysisEnd = 1 << 14
|
2017-11-02 18:25:48 +00:00
|
|
|
};
|
|
|
|
|
2018-11-29 19:24:58 +00:00
|
|
|
using ProcessFunction = std::function<bool(Function*)>;
|
|
|
|
|
2018-01-30 21:43:55 +00:00
|
|
|
friend inline Analysis operator|(Analysis lhs, Analysis rhs);
|
2017-11-02 18:25:48 +00:00
|
|
|
friend inline Analysis& operator|=(Analysis& lhs, Analysis rhs);
|
2018-01-30 21:43:55 +00:00
|
|
|
friend inline Analysis operator<<(Analysis a, int shift);
|
2017-11-02 18:25:48 +00:00
|
|
|
friend inline Analysis& operator<<=(Analysis& a, int shift);
|
|
|
|
|
2017-12-07 21:42:27 +00:00
|
|
|
// Creates an |IRContext| that contains an owned |Module|
|
2018-07-07 13:38:00 +00:00
|
|
|
IRContext(spv_target_env env, MessageConsumer c)
|
2017-12-19 19:18:13 +00:00
|
|
|
: syntax_context_(spvContextCreate(env)),
|
|
|
|
grammar_(syntax_context_),
|
|
|
|
unique_id_(0),
|
2017-11-14 19:11:50 +00:00
|
|
|
module_(new Module()),
|
|
|
|
consumer_(std::move(c)),
|
|
|
|
def_use_mgr_(nullptr),
|
2017-12-07 21:42:27 +00:00
|
|
|
valid_analyses_(kAnalysisNone),
|
|
|
|
constant_mgr_(nullptr),
|
2018-02-13 04:28:38 +00:00
|
|
|
type_mgr_(nullptr),
|
2018-09-10 15:49:41 +00:00
|
|
|
id_to_name_(nullptr),
|
|
|
|
max_id_bound_(kDefaultMaxIdBound) {
|
2018-07-07 13:38:00 +00:00
|
|
|
SetContextMessageConsumer(syntax_context_, consumer_);
|
2017-11-14 19:11:50 +00:00
|
|
|
module_->SetContext(this);
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
IRContext(spv_target_env env, std::unique_ptr<Module>&& m, MessageConsumer c)
|
2017-12-19 19:18:13 +00:00
|
|
|
: syntax_context_(spvContextCreate(env)),
|
|
|
|
grammar_(syntax_context_),
|
|
|
|
unique_id_(0),
|
2017-11-14 19:11:50 +00:00
|
|
|
module_(std::move(m)),
|
2017-11-02 18:25:48 +00:00
|
|
|
consumer_(std::move(c)),
|
|
|
|
def_use_mgr_(nullptr),
|
2017-12-07 21:42:27 +00:00
|
|
|
valid_analyses_(kAnalysisNone),
|
2018-02-13 04:28:38 +00:00
|
|
|
type_mgr_(nullptr),
|
2018-09-10 15:49:41 +00:00
|
|
|
id_to_name_(nullptr),
|
|
|
|
max_id_bound_(kDefaultMaxIdBound) {
|
2018-07-07 13:38:00 +00:00
|
|
|
SetContextMessageConsumer(syntax_context_, consumer_);
|
2017-11-14 19:11:50 +00:00
|
|
|
module_->SetContext(this);
|
2017-11-11 01:26:55 +00:00
|
|
|
InitializeCombinators();
|
2017-11-14 19:11:50 +00:00
|
|
|
}
|
2017-12-19 19:18:13 +00:00
|
|
|
|
|
|
|
~IRContext() { spvContextDestroy(syntax_context_); }
|
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
Module* module() const { return module_.get(); }
|
|
|
|
|
|
|
|
// Returns a vector of pointers to constant-creation instructions in this
|
|
|
|
// context.
|
|
|
|
inline std::vector<Instruction*> GetConstants();
|
|
|
|
inline std::vector<const Instruction*> GetConstants() const;
|
|
|
|
|
|
|
|
// Iterators for annotation instructions contained in this context.
|
|
|
|
inline Module::inst_iterator annotation_begin();
|
|
|
|
inline Module::inst_iterator annotation_end();
|
|
|
|
inline IteratorRange<Module::inst_iterator> annotations();
|
|
|
|
inline IteratorRange<Module::const_inst_iterator> annotations() const;
|
|
|
|
|
|
|
|
// Iterators for capabilities instructions contained in this module.
|
|
|
|
inline Module::inst_iterator capability_begin();
|
|
|
|
inline Module::inst_iterator capability_end();
|
|
|
|
inline IteratorRange<Module::inst_iterator> capabilities();
|
|
|
|
inline IteratorRange<Module::const_inst_iterator> capabilities() const;
|
|
|
|
|
|
|
|
// Iterators for types, constants and global variables instructions.
|
2018-07-12 19:14:43 +00:00
|
|
|
inline Module::inst_iterator types_values_begin();
|
|
|
|
inline Module::inst_iterator types_values_end();
|
2017-10-30 15:13:24 +00:00
|
|
|
inline IteratorRange<Module::inst_iterator> types_values();
|
|
|
|
inline IteratorRange<Module::const_inst_iterator> types_values() const;
|
|
|
|
|
|
|
|
// Iterators for extension instructions contained in this module.
|
|
|
|
inline Module::inst_iterator ext_inst_import_begin();
|
|
|
|
inline Module::inst_iterator ext_inst_import_end();
|
2017-11-21 19:47:46 +00:00
|
|
|
inline IteratorRange<Module::inst_iterator> ext_inst_imports();
|
|
|
|
inline IteratorRange<Module::const_inst_iterator> ext_inst_imports() const;
|
2017-10-30 15:13:24 +00:00
|
|
|
|
|
|
|
// There are several kinds of debug instructions, according to where they can
|
|
|
|
// appear in the logical layout of a module:
|
|
|
|
// - Section 7a: OpString, OpSourceExtension, OpSource, OpSourceContinued
|
|
|
|
// - Section 7b: OpName, OpMemberName
|
|
|
|
// - Section 7c: OpModuleProcessed
|
|
|
|
// - Mostly anywhere: OpLine and OpNoLine
|
|
|
|
//
|
|
|
|
|
|
|
|
// Iterators for debug 1 instructions (excluding OpLine & OpNoLine) contained
|
|
|
|
// in this module. These are for layout section 7a.
|
|
|
|
inline Module::inst_iterator debug1_begin();
|
|
|
|
inline Module::inst_iterator debug1_end();
|
|
|
|
inline IteratorRange<Module::inst_iterator> debugs1();
|
|
|
|
inline IteratorRange<Module::const_inst_iterator> debugs1() const;
|
|
|
|
|
|
|
|
// Iterators for debug 2 instructions (excluding OpLine & OpNoLine) contained
|
|
|
|
// in this module. These are for layout section 7b.
|
|
|
|
inline Module::inst_iterator debug2_begin();
|
|
|
|
inline Module::inst_iterator debug2_end();
|
|
|
|
inline IteratorRange<Module::inst_iterator> debugs2();
|
|
|
|
inline IteratorRange<Module::const_inst_iterator> debugs2() const;
|
|
|
|
|
|
|
|
// Iterators for debug 3 instructions (excluding OpLine & OpNoLine) contained
|
|
|
|
// in this module. These are for layout section 7c.
|
|
|
|
inline Module::inst_iterator debug3_begin();
|
|
|
|
inline Module::inst_iterator debug3_end();
|
|
|
|
inline IteratorRange<Module::inst_iterator> debugs3();
|
|
|
|
inline IteratorRange<Module::const_inst_iterator> debugs3() const;
|
|
|
|
|
|
|
|
// Clears all debug instructions (excluding OpLine & OpNoLine).
|
|
|
|
inline void debug_clear();
|
|
|
|
|
|
|
|
// Appends a capability instruction to this module.
|
|
|
|
inline void AddCapability(std::unique_ptr<Instruction>&& c);
|
|
|
|
// Appends an extension instruction to this module.
|
|
|
|
inline void AddExtension(std::unique_ptr<Instruction>&& e);
|
|
|
|
// Appends an extended instruction set instruction to this module.
|
|
|
|
inline void AddExtInstImport(std::unique_ptr<Instruction>&& e);
|
|
|
|
// Set the memory model for this module.
|
|
|
|
inline void SetMemoryModel(std::unique_ptr<Instruction>&& m);
|
|
|
|
// Appends an entry point instruction to this module.
|
|
|
|
inline void AddEntryPoint(std::unique_ptr<Instruction>&& e);
|
|
|
|
// Appends an execution mode instruction to this module.
|
|
|
|
inline void AddExecutionMode(std::unique_ptr<Instruction>&& e);
|
|
|
|
// Appends a debug 1 instruction (excluding OpLine & OpNoLine) to this module.
|
|
|
|
// "debug 1" instructions are the ones in layout section 7.a), see section
|
|
|
|
// 2.4 Logical Layout of a Module from the SPIR-V specification.
|
|
|
|
inline void AddDebug1Inst(std::unique_ptr<Instruction>&& d);
|
|
|
|
// Appends a debug 2 instruction (excluding OpLine & OpNoLine) to this module.
|
|
|
|
// "debug 2" instructions are the ones in layout section 7.b), see section
|
|
|
|
// 2.4 Logical Layout of a Module from the SPIR-V specification.
|
|
|
|
inline void AddDebug2Inst(std::unique_ptr<Instruction>&& d);
|
|
|
|
// Appends a debug 3 instruction (OpModuleProcessed) to this module.
|
|
|
|
// This is due to decision by the SPIR Working Group, pending publication.
|
|
|
|
inline void AddDebug3Inst(std::unique_ptr<Instruction>&& d);
|
|
|
|
// Appends an annotation instruction to this module.
|
|
|
|
inline void AddAnnotationInst(std::unique_ptr<Instruction>&& a);
|
|
|
|
// Appends a type-declaration instruction to this module.
|
|
|
|
inline void AddType(std::unique_ptr<Instruction>&& t);
|
|
|
|
// Appends a constant, global variable, or OpUndef instruction to this module.
|
|
|
|
inline void AddGlobalValue(std::unique_ptr<Instruction>&& v);
|
|
|
|
// Appends a function to this module.
|
|
|
|
inline void AddFunction(std::unique_ptr<Function>&& f);
|
|
|
|
|
2017-11-02 18:25:48 +00:00
|
|
|
// Returns a pointer to a def-use manager. If the def-use manager is
|
|
|
|
// invalid, it is rebuilt first.
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::DefUseManager* get_def_use_mgr() {
|
2017-11-02 18:25:48 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisDefUse)) {
|
|
|
|
BuildDefUseManager();
|
|
|
|
}
|
|
|
|
return def_use_mgr_.get();
|
|
|
|
}
|
|
|
|
|
2018-04-26 19:07:48 +00:00
|
|
|
// Returns a pointer to a value number table. If the liveness analysis is
|
|
|
|
// invalid, it is rebuilt first.
|
2018-07-12 19:14:43 +00:00
|
|
|
ValueNumberTable* GetValueNumberTable() {
|
2018-04-26 19:07:48 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisValueNumberTable)) {
|
|
|
|
BuildValueNumberTable();
|
|
|
|
}
|
|
|
|
return vn_table_.get();
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:18:15 +00:00
|
|
|
// Returns a pointer to a StructuredCFGAnalysis. If the analysis is invalid,
|
|
|
|
// it is rebuilt first.
|
2018-11-08 14:11:20 +00:00
|
|
|
StructuredCFGAnalysis* GetStructuredCFGAnalysis() {
|
2018-10-10 19:18:15 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisStructuredCFG)) {
|
|
|
|
BuildStructuredCFGAnalysis();
|
|
|
|
}
|
|
|
|
return struct_cfg_analysis_.get();
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:31:09 +00:00
|
|
|
// Returns a pointer to a liveness analysis. If the liveness analysis is
|
|
|
|
// invalid, it is rebuilt first.
|
2018-07-12 19:14:43 +00:00
|
|
|
LivenessAnalysis* GetLivenessAnalysis() {
|
2018-04-17 14:31:09 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisRegisterPressure)) {
|
|
|
|
BuildRegPressureAnalysis();
|
|
|
|
}
|
|
|
|
return reg_pressure_.get();
|
|
|
|
}
|
|
|
|
|
2017-11-10 14:39:00 +00:00
|
|
|
// Returns the basic block for instruction |instr|. Re-builds the instruction
|
|
|
|
// block map, if needed.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* get_instr_block(Instruction* instr) {
|
2017-11-10 14:39:00 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisInstrToBlockMapping)) {
|
|
|
|
BuildInstrToBlockMapping();
|
|
|
|
}
|
|
|
|
auto entry = instr_to_block_.find(instr);
|
|
|
|
return (entry != instr_to_block_.end()) ? entry->second : nullptr;
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
// Returns the basic block for |id|. Re-builds the instruction block map, if
|
|
|
|
// needed.
|
|
|
|
//
|
|
|
|
// |id| must be a registered definition.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* get_instr_block(uint32_t id) {
|
|
|
|
Instruction* def = get_def_use_mgr()->GetDef(id);
|
2017-11-30 22:03:06 +00:00
|
|
|
return get_instr_block(def);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Sets the basic block for |inst|. Re-builds the mapping if it has become
|
|
|
|
// invalid.
|
2018-07-12 19:14:43 +00:00
|
|
|
void set_instr_block(Instruction* inst, BasicBlock* block) {
|
2017-11-30 22:03:06 +00:00
|
|
|
if (AreAnalysesValid(kAnalysisInstrToBlockMapping)) {
|
|
|
|
instr_to_block_[inst] = block;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
// Returns a pointer the decoration manager. If the decoration manger is
|
|
|
|
// invalid, it is rebuilt first.
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::DecorationManager* get_decoration_mgr() {
|
2017-11-13 20:31:43 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisDecorations)) {
|
|
|
|
BuildDecorationManager();
|
|
|
|
}
|
|
|
|
return decoration_mgr_.get();
|
2018-08-07 13:09:47 +00:00
|
|
|
}
|
2017-11-13 20:31:43 +00:00
|
|
|
|
2017-12-07 21:42:27 +00:00
|
|
|
// Returns a pointer to the constant manager. If no constant manager has been
|
|
|
|
// created yet, it creates one. NOTE: Once created, the constant manager
|
|
|
|
// remains active and it is never re-built.
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::ConstantManager* get_constant_mgr() {
|
2017-12-07 21:42:27 +00:00
|
|
|
if (!constant_mgr_)
|
2018-08-14 19:01:50 +00:00
|
|
|
constant_mgr_ = MakeUnique<analysis::ConstantManager>(this);
|
2017-12-07 21:42:27 +00:00
|
|
|
return constant_mgr_.get();
|
|
|
|
}
|
|
|
|
|
|
|
|
// Returns a pointer to the type manager. If no type manager has been created
|
|
|
|
// yet, it creates one. NOTE: Once created, the type manager remains active it
|
|
|
|
// is never re-built.
|
2018-07-12 19:14:43 +00:00
|
|
|
analysis::TypeManager* get_type_mgr() {
|
2017-12-07 21:42:27 +00:00
|
|
|
if (!type_mgr_)
|
2018-08-14 19:01:50 +00:00
|
|
|
type_mgr_ = MakeUnique<analysis::TypeManager>(consumer(), this);
|
2017-12-07 21:42:27 +00:00
|
|
|
return type_mgr_.get();
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:19:55 +00:00
|
|
|
// Returns a pointer to the scalar evolution analysis. If it is invalid it
|
|
|
|
// will be rebuilt first.
|
2018-07-12 19:14:43 +00:00
|
|
|
ScalarEvolutionAnalysis* GetScalarEvolutionAnalysis() {
|
2018-03-28 13:19:55 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisScalarEvolution)) {
|
|
|
|
BuildScalarEvolutionAnalysis();
|
|
|
|
}
|
|
|
|
return scalar_evolution_analysis_.get();
|
|
|
|
}
|
|
|
|
|
2018-02-13 04:28:38 +00:00
|
|
|
// Build the map from the ids to the OpName and OpMemberName instruction
|
|
|
|
// associated with it.
|
|
|
|
inline void BuildIdToNameMap();
|
|
|
|
|
|
|
|
// Returns a range of instrucions that contain all of the OpName and
|
|
|
|
// OpMemberNames associated with the given id.
|
|
|
|
inline IteratorRange<std::multimap<uint32_t, Instruction*>::iterator>
|
|
|
|
GetNames(uint32_t id);
|
|
|
|
|
2017-11-02 18:25:48 +00:00
|
|
|
// Sets the message consumer to the given |consumer|. |consumer| which will be
|
|
|
|
// invoked every time there is a message to be communicated to the outside.
|
2018-07-07 13:38:00 +00:00
|
|
|
void SetMessageConsumer(MessageConsumer c) { consumer_ = std::move(c); }
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
// Returns the reference to the message consumer for this pass.
|
2018-07-07 13:38:00 +00:00
|
|
|
const MessageConsumer& consumer() const { return consumer_; }
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
// Rebuilds the analyses in |set| that are invalid.
|
|
|
|
void BuildInvalidAnalyses(Analysis set);
|
|
|
|
|
|
|
|
// Invalidates all of the analyses except for those in |preserved_analyses|.
|
|
|
|
void InvalidateAnalysesExceptFor(Analysis preserved_analyses);
|
|
|
|
|
2017-11-10 14:39:00 +00:00
|
|
|
// Invalidates the analyses marked in |analyses_to_invalidate|.
|
|
|
|
void InvalidateAnalyses(Analysis analyses_to_invalidate);
|
|
|
|
|
2017-11-21 19:47:46 +00:00
|
|
|
// Deletes the instruction defining the given |id|. Returns true on
|
2017-11-02 18:25:48 +00:00
|
|
|
// success, false if the given |id| is not defined at all. This method also
|
2017-11-21 19:47:46 +00:00
|
|
|
// erases the name, decorations, and defintion of |id|.
|
|
|
|
//
|
|
|
|
// Pointers and iterators pointing to the deleted instructions become invalid.
|
|
|
|
// However other pointers and iterators are still valid.
|
2017-11-02 18:25:48 +00:00
|
|
|
bool KillDef(uint32_t id);
|
|
|
|
|
2017-11-21 19:47:46 +00:00
|
|
|
// Deletes the given instruction |inst|. This method erases the
|
2017-11-02 18:25:48 +00:00
|
|
|
// information of the given instruction's uses of its operands. If |inst|
|
2017-11-21 19:47:46 +00:00
|
|
|
// defines a result id, its name and decorations will also be deleted.
|
|
|
|
//
|
|
|
|
// Pointer and iterator pointing to the deleted instructions become invalid.
|
|
|
|
// However other pointers and iterators are still valid.
|
|
|
|
//
|
|
|
|
// Note that if an instruction is not in an instruction list, the memory may
|
|
|
|
// not be safe to delete, so the instruction is turned into a OpNop instead.
|
|
|
|
// This can happen with OpLabel.
|
|
|
|
//
|
|
|
|
// Returns a pointer to the instruction after |inst| or |nullptr| if no such
|
|
|
|
// instruction exists.
|
2018-07-12 19:14:43 +00:00
|
|
|
Instruction* KillInst(Instruction* inst);
|
2017-11-02 18:25:48 +00:00
|
|
|
|
|
|
|
// Returns true if all of the given analyses are valid.
|
|
|
|
bool AreAnalysesValid(Analysis set) { return (set & valid_analyses_) == set; }
|
|
|
|
|
|
|
|
// Replaces all uses of |before| id with |after| id. Returns true if any
|
|
|
|
// replacement happens. This method does not kill the definition of the
|
|
|
|
// |before| id. If |after| is the same as |before|, does nothing and returns
|
|
|
|
// false.
|
2017-11-14 19:11:50 +00:00
|
|
|
//
|
|
|
|
// |before| and |after| must be registered definitions in the DefUseManager.
|
2017-11-02 18:25:48 +00:00
|
|
|
bool ReplaceAllUsesWith(uint32_t before, uint32_t after);
|
|
|
|
|
2017-11-09 16:24:41 +00:00
|
|
|
// Returns true if all of the analyses that are suppose to be valid are
|
|
|
|
// actually valid.
|
|
|
|
bool IsConsistent();
|
|
|
|
|
2017-12-11 18:10:24 +00:00
|
|
|
// The IRContext will look at the def and uses of |inst| and update any valid
|
|
|
|
// analyses will be updated accordingly.
|
|
|
|
inline void AnalyzeDefUse(Instruction* inst);
|
|
|
|
|
2017-11-09 16:24:41 +00:00
|
|
|
// Informs the IRContext that the uses of |inst| are going to change, and that
|
|
|
|
// is should forget everything it know about the current uses. Any valid
|
|
|
|
// analyses will be updated accordingly.
|
|
|
|
void ForgetUses(Instruction* inst);
|
|
|
|
|
|
|
|
// The IRContext will look at the uses of |inst| and update any valid analyses
|
|
|
|
// will be updated accordingly.
|
|
|
|
void AnalyzeUses(Instruction* inst);
|
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
// Kill all name and decorate ops targeting |id|.
|
|
|
|
void KillNamesAndDecorates(uint32_t id);
|
|
|
|
|
|
|
|
// Kill all name and decorate ops targeting the result id of |inst|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void KillNamesAndDecorates(Instruction* inst);
|
2017-11-13 20:31:43 +00:00
|
|
|
|
2017-11-14 19:11:50 +00:00
|
|
|
// Returns the next unique id for use by an instruction.
|
|
|
|
inline uint32_t TakeNextUniqueId() {
|
|
|
|
assert(unique_id_ != std::numeric_limits<uint32_t>::max());
|
|
|
|
|
|
|
|
// Skip zero.
|
|
|
|
return ++unique_id_;
|
|
|
|
}
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
// Returns true if |inst| is a combinator in the current context.
|
|
|
|
// |combinator_ops_| is built if it has not been already.
|
2018-04-26 19:07:48 +00:00
|
|
|
inline bool IsCombinatorInstruction(const Instruction* inst) {
|
2017-11-11 01:26:55 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisCombinators)) {
|
|
|
|
InitializeCombinators();
|
|
|
|
}
|
|
|
|
const uint32_t kExtInstSetIdInIndx = 0;
|
|
|
|
const uint32_t kExtInstInstructionInIndx = 1;
|
|
|
|
|
|
|
|
if (inst->opcode() != SpvOpExtInst) {
|
|
|
|
return combinator_ops_[0].count(inst->opcode()) != 0;
|
|
|
|
} else {
|
|
|
|
uint32_t set = inst->GetSingleWordInOperand(kExtInstSetIdInIndx);
|
|
|
|
uint32_t op = inst->GetSingleWordInOperand(kExtInstInstructionInIndx);
|
|
|
|
return combinator_ops_[set].count(op) != 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:20:32 +00:00
|
|
|
// Returns a pointer to the CFG for all the functions in |module_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
CFG* cfg() {
|
2017-11-27 19:20:32 +00:00
|
|
|
if (!AreAnalysesValid(kAnalysisCFG)) {
|
|
|
|
BuildCFG();
|
|
|
|
}
|
|
|
|
return cfg_.get();
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:33:06 +00:00
|
|
|
// Gets the loop descriptor for function |f|.
|
2018-07-12 19:14:43 +00:00
|
|
|
LoopDescriptor* GetLoopDescriptor(const Function* f);
|
2018-01-25 10:33:06 +00:00
|
|
|
|
2017-11-27 21:21:26 +00:00
|
|
|
// Gets the dominator analysis for function |f|.
|
2018-07-12 19:14:43 +00:00
|
|
|
DominatorAnalysis* GetDominatorAnalysis(const Function* f);
|
2018-04-17 14:31:09 +00:00
|
|
|
|
2017-11-27 21:21:26 +00:00
|
|
|
// Gets the postdominator analysis for function |f|.
|
2018-07-12 19:14:43 +00:00
|
|
|
PostDominatorAnalysis* GetPostDominatorAnalysis(const Function* f);
|
2018-04-17 14:31:09 +00:00
|
|
|
|
2017-11-27 21:21:26 +00:00
|
|
|
// Remove the dominator tree of |f| from the cache.
|
2018-07-12 19:14:43 +00:00
|
|
|
inline void RemoveDominatorAnalysis(const Function* f) {
|
2017-11-27 21:21:26 +00:00
|
|
|
dominator_trees_.erase(f);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove the postdominator tree of |f| from the cache.
|
2018-07-12 19:14:43 +00:00
|
|
|
inline void RemovePostDominatorAnalysis(const Function* f) {
|
2017-11-27 21:21:26 +00:00
|
|
|
post_dominator_trees_.erase(f);
|
|
|
|
}
|
|
|
|
|
2018-12-06 14:07:00 +00:00
|
|
|
// Return the next available SSA id and increment it. Returns 0 if the
|
|
|
|
// maximum SSA id has been reached.
|
2017-12-07 21:42:27 +00:00
|
|
|
inline uint32_t TakeNextId() { return module()->TakeNextIdBound(); }
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
FeatureManager* get_feature_mgr() {
|
2017-11-30 22:03:06 +00:00
|
|
|
if (!feature_mgr_.get()) {
|
|
|
|
AnalyzeFeatures();
|
|
|
|
}
|
|
|
|
return feature_mgr_.get();
|
|
|
|
}
|
|
|
|
|
2018-01-10 19:23:47 +00:00
|
|
|
// Returns the grammar for this context.
|
2018-07-07 13:38:00 +00:00
|
|
|
const AssemblyGrammar& grammar() const { return grammar_; }
|
2018-01-10 19:23:47 +00:00
|
|
|
|
2018-02-08 15:59:03 +00:00
|
|
|
// If |inst| has not yet been analysed by the def-use manager, then analyse
|
|
|
|
// its definitions and uses.
|
|
|
|
inline void UpdateDefUse(Instruction* inst);
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
const InstructionFolder& get_instruction_folder() {
|
2018-07-05 18:19:50 +00:00
|
|
|
if (!inst_folder_) {
|
2018-08-14 19:01:50 +00:00
|
|
|
inst_folder_ = MakeUnique<InstructionFolder>(this);
|
2018-07-05 18:19:50 +00:00
|
|
|
}
|
|
|
|
return *inst_folder_;
|
|
|
|
}
|
|
|
|
|
2018-09-10 15:49:41 +00:00
|
|
|
uint32_t max_id_bound() const { return max_id_bound_; }
|
|
|
|
void set_max_id_bound(uint32_t new_bound) { max_id_bound_ = new_bound; }
|
|
|
|
|
2018-11-08 18:54:54 +00:00
|
|
|
// Return id of variable only decorated with |builtin|, if in module.
|
|
|
|
// Create variable and return its id otherwise. If builtin not currently
|
|
|
|
// supported, return 0.
|
|
|
|
uint32_t GetBuiltinVarId(uint32_t builtin);
|
|
|
|
|
2018-11-29 19:24:58 +00:00
|
|
|
// Returns the function whose id is |id|, if one exists. Returns |nullptr|
|
|
|
|
// otherwise.
|
|
|
|
Function* GetFunction(uint32_t id) {
|
|
|
|
if (!AreAnalysesValid(kAnalysisIdToFuncMapping)) {
|
|
|
|
BuildIdToFuncMapping();
|
|
|
|
}
|
|
|
|
auto entry = id_to_func_.find(id);
|
|
|
|
return (entry != id_to_func_.end()) ? entry->second : nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
Function* GetFunction(Instruction* inst) {
|
|
|
|
if (inst->opcode() != SpvOpFunction) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
return GetFunction(inst->result_id());
|
|
|
|
}
|
|
|
|
|
|
|
|
// Add to |todo| all ids of functions called in |func|.
|
|
|
|
void AddCalls(const Function* func, std::queue<uint32_t>* todo);
|
|
|
|
|
|
|
|
// Applies |pfn| to every function in the call trees that are rooted at the
|
|
|
|
// entry points. Returns true if any call |pfn| returns true. By convention
|
|
|
|
// |pfn| should return true if it modified the module.
|
|
|
|
bool ProcessEntryPointCallTree(ProcessFunction& pfn);
|
|
|
|
|
|
|
|
// Applies |pfn| to every function in the call trees rooted at the entry
|
|
|
|
// points and exported functions. Returns true if any call |pfn| returns
|
|
|
|
// true. By convention |pfn| should return true if it modified the module.
|
|
|
|
bool ProcessReachableCallTree(ProcessFunction& pfn);
|
|
|
|
|
|
|
|
// Applies |pfn| to every function in the call trees rooted at the elements of
|
|
|
|
// |roots|. Returns true if any call to |pfn| returns true. By convention
|
|
|
|
// |pfn| should return true if it modified the module. After returning
|
|
|
|
// |roots| will be empty.
|
|
|
|
bool ProcessCallTreeFromRoots(ProcessFunction& pfn,
|
|
|
|
std::queue<uint32_t>* roots);
|
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
private:
|
2017-11-10 14:39:00 +00:00
|
|
|
// Builds the def-use manager from scratch, even if it was already valid.
|
|
|
|
void BuildDefUseManager() {
|
2018-08-14 19:01:50 +00:00
|
|
|
def_use_mgr_ = MakeUnique<analysis::DefUseManager>(module());
|
2017-11-10 14:39:00 +00:00
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisDefUse;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Builds the instruction-block map for the whole module.
|
|
|
|
void BuildInstrToBlockMapping() {
|
|
|
|
instr_to_block_.clear();
|
|
|
|
for (auto& fn : *module_) {
|
|
|
|
for (auto& block : fn) {
|
2018-07-12 19:14:43 +00:00
|
|
|
block.ForEachInst([this, &block](Instruction* inst) {
|
2017-11-10 14:39:00 +00:00
|
|
|
instr_to_block_[inst] = █
|
|
|
|
});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisInstrToBlockMapping;
|
|
|
|
}
|
|
|
|
|
2018-11-29 19:24:58 +00:00
|
|
|
// Builds the instruction-function map for the whole module.
|
|
|
|
void BuildIdToFuncMapping() {
|
|
|
|
id_to_func_.clear();
|
|
|
|
for (auto& fn : *module_) {
|
|
|
|
id_to_func_[fn.result_id()] = &fn;
|
|
|
|
}
|
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisIdToFuncMapping;
|
|
|
|
}
|
|
|
|
|
2017-11-13 20:31:43 +00:00
|
|
|
void BuildDecorationManager() {
|
2018-08-14 19:01:50 +00:00
|
|
|
decoration_mgr_ = MakeUnique<analysis::DecorationManager>(module());
|
2017-11-13 20:31:43 +00:00
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisDecorations;
|
|
|
|
}
|
|
|
|
|
2017-11-27 19:20:32 +00:00
|
|
|
void BuildCFG() {
|
2018-08-14 19:01:50 +00:00
|
|
|
cfg_ = MakeUnique<CFG>(module());
|
2017-11-27 19:20:32 +00:00
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisCFG;
|
|
|
|
}
|
|
|
|
|
2018-03-28 13:19:55 +00:00
|
|
|
void BuildScalarEvolutionAnalysis() {
|
2018-08-14 19:01:50 +00:00
|
|
|
scalar_evolution_analysis_ = MakeUnique<ScalarEvolutionAnalysis>(this);
|
2018-03-28 13:19:55 +00:00
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisScalarEvolution;
|
|
|
|
}
|
|
|
|
|
2018-04-17 14:31:09 +00:00
|
|
|
// Builds the liveness analysis from scratch, even if it was already valid.
|
|
|
|
void BuildRegPressureAnalysis() {
|
2018-08-14 19:01:50 +00:00
|
|
|
reg_pressure_ = MakeUnique<LivenessAnalysis>(this);
|
2018-04-17 14:31:09 +00:00
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisRegisterPressure;
|
|
|
|
}
|
|
|
|
|
2018-04-26 19:07:48 +00:00
|
|
|
// Builds the value number table analysis from scratch, even if it was already
|
|
|
|
// valid.
|
|
|
|
void BuildValueNumberTable() {
|
2018-08-14 19:01:50 +00:00
|
|
|
vn_table_ = MakeUnique<ValueNumberTable>(this);
|
2018-04-26 19:07:48 +00:00
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisValueNumberTable;
|
|
|
|
}
|
|
|
|
|
2018-10-10 19:18:15 +00:00
|
|
|
// Builds the structured CFG analysis from scratch, even if it was already
|
|
|
|
// valid.
|
|
|
|
void BuildStructuredCFGAnalysis() {
|
|
|
|
struct_cfg_analysis_ = MakeUnique<StructuredCFGAnalysis>(this);
|
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisStructuredCFG;
|
|
|
|
}
|
|
|
|
|
2017-12-21 14:47:25 +00:00
|
|
|
// Removes all computed dominator and post-dominator trees. This will force
|
|
|
|
// the context to rebuild the trees on demand.
|
|
|
|
void ResetDominatorAnalysis() {
|
|
|
|
// Clear the cache.
|
|
|
|
dominator_trees_.clear();
|
|
|
|
post_dominator_trees_.clear();
|
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisDominatorAnalysis;
|
|
|
|
}
|
|
|
|
|
2018-01-25 10:33:06 +00:00
|
|
|
// Removes all computed loop descriptors.
|
|
|
|
void ResetLoopAnalysis() {
|
|
|
|
// Clear the cache.
|
|
|
|
loop_descriptors_.clear();
|
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisLoopAnalysis;
|
|
|
|
}
|
|
|
|
|
2018-11-08 18:54:54 +00:00
|
|
|
// Removes all computed loop descriptors.
|
|
|
|
void ResetBuiltinAnalysis() {
|
|
|
|
// Clear the cache.
|
|
|
|
builtin_var_id_map_.clear();
|
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisBuiltinVarId;
|
|
|
|
}
|
|
|
|
|
2017-11-30 22:03:06 +00:00
|
|
|
// Analyzes the features in the owned module. Builds the manager if required.
|
|
|
|
void AnalyzeFeatures() {
|
2018-08-14 19:01:50 +00:00
|
|
|
feature_mgr_ = MakeUnique<FeatureManager>(grammar_);
|
2017-11-30 22:03:06 +00:00
|
|
|
feature_mgr_->Analyze(module());
|
|
|
|
}
|
|
|
|
|
2017-11-11 01:26:55 +00:00
|
|
|
// Scans a module looking for it capabilities, and initializes combinator_ops_
|
|
|
|
// accordingly.
|
|
|
|
void InitializeCombinators();
|
|
|
|
|
|
|
|
// Add the combinator opcode for the given capability to combinator_ops_.
|
|
|
|
void AddCombinatorsForCapability(uint32_t capability);
|
|
|
|
|
|
|
|
// Add the combinator opcode for the given extension to combinator_ops_.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AddCombinatorsForExtension(Instruction* extension);
|
2017-11-11 01:26:55 +00:00
|
|
|
|
2018-02-13 04:28:38 +00:00
|
|
|
// Remove |inst| from |id_to_name_| if it is in map.
|
|
|
|
void RemoveFromIdToName(const Instruction* inst);
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
// Returns true if it is suppose to be valid but it is incorrect. Returns
|
|
|
|
// true if the cfg is invalidated.
|
|
|
|
bool CheckCFG();
|
|
|
|
|
2018-11-08 18:54:54 +00:00
|
|
|
// Return id of variable only decorated with |builtin|, if in module.
|
|
|
|
// Return 0 otherwise.
|
|
|
|
uint32_t FindBuiltinVar(uint32_t builtin);
|
|
|
|
|
|
|
|
// Add |var_id| to all entry points in module.
|
|
|
|
void AddVarToEntryPoints(uint32_t var_id);
|
|
|
|
|
2017-12-19 19:18:13 +00:00
|
|
|
// The SPIR-V syntax context containing grammar tables for opcodes and
|
|
|
|
// operands.
|
|
|
|
spv_context syntax_context_;
|
|
|
|
|
|
|
|
// Auxiliary object for querying SPIR-V grammar facts.
|
2018-07-07 13:38:00 +00:00
|
|
|
AssemblyGrammar grammar_;
|
2017-12-19 19:18:13 +00:00
|
|
|
|
2017-12-07 21:42:27 +00:00
|
|
|
// An unique identifier for instructions in |module_|. Can be used to order
|
2017-11-14 19:11:50 +00:00
|
|
|
// instructions in a container.
|
|
|
|
//
|
|
|
|
// This member is initialized to 0, but always issues this value plus one.
|
|
|
|
// Therefore, 0 is not a valid unique id for an instruction.
|
|
|
|
uint32_t unique_id_;
|
|
|
|
|
2017-12-07 21:42:27 +00:00
|
|
|
// The module being processed within this IR context.
|
2017-10-30 15:13:24 +00:00
|
|
|
std::unique_ptr<Module> module_;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// A message consumer for diagnostics.
|
2018-07-07 13:38:00 +00:00
|
|
|
MessageConsumer consumer_;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// The def-use manager for |module_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<analysis::DefUseManager> def_use_mgr_;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// The instruction decoration manager for |module_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<analysis::DecorationManager> decoration_mgr_;
|
|
|
|
std::unique_ptr<FeatureManager> feature_mgr_;
|
2017-11-02 18:25:48 +00:00
|
|
|
|
2018-11-29 19:24:58 +00:00
|
|
|
// A map from instructions to the basic block they belong to. This mapping is
|
2017-11-10 14:39:00 +00:00
|
|
|
// built on-demand when get_instr_block() is called.
|
|
|
|
//
|
|
|
|
// NOTE: Do not traverse this map. Ever. Use the function and basic block
|
|
|
|
// iterators to traverse instructions.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<Instruction*, BasicBlock*> instr_to_block_;
|
2017-11-10 14:39:00 +00:00
|
|
|
|
2018-11-29 19:24:58 +00:00
|
|
|
// A map from ids to the function they define. This mapping is
|
|
|
|
// built on-demand when GetFunction() is called.
|
|
|
|
//
|
|
|
|
// NOTE: Do not traverse this map. Ever. Use the function and basic block
|
|
|
|
// iterators to traverse instructions.
|
|
|
|
std::unordered_map<uint32_t, Function*> id_to_func_;
|
|
|
|
|
2017-11-02 18:25:48 +00:00
|
|
|
// A bitset indicating which analyes are currently valid.
|
|
|
|
Analysis valid_analyses_;
|
2017-11-11 01:26:55 +00:00
|
|
|
|
|
|
|
// Opcodes of shader capability core executable instructions
|
|
|
|
// without side-effect.
|
2017-11-27 15:16:41 +00:00
|
|
|
std::unordered_map<uint32_t, std::unordered_set<uint32_t>> combinator_ops_;
|
2017-11-27 19:20:32 +00:00
|
|
|
|
2018-11-08 18:54:54 +00:00
|
|
|
// Opcodes of shader capability core executable instructions
|
|
|
|
// without side-effect.
|
|
|
|
std::unordered_map<uint32_t, uint32_t> builtin_var_id_map_;
|
|
|
|
|
2017-11-27 19:20:32 +00:00
|
|
|
// The CFG for all the functions in |module_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<CFG> cfg_;
|
2017-11-27 21:21:26 +00:00
|
|
|
|
|
|
|
// Each function in the module will create its own dominator tree. We cache
|
|
|
|
// the result so it doesn't need to be rebuilt each time.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::map<const Function*, DominatorAnalysis> dominator_trees_;
|
|
|
|
std::map<const Function*, PostDominatorAnalysis> post_dominator_trees_;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
2018-01-25 10:33:06 +00:00
|
|
|
// Cache of loop descriptors for each function.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<const Function*, LoopDescriptor> loop_descriptors_;
|
2018-01-25 10:33:06 +00:00
|
|
|
|
2017-12-07 21:42:27 +00:00
|
|
|
// Constant manager for |module_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<analysis::ConstantManager> constant_mgr_;
|
2017-12-07 21:42:27 +00:00
|
|
|
|
|
|
|
// Type manager for |module_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<analysis::TypeManager> type_mgr_;
|
2018-02-13 04:28:38 +00:00
|
|
|
|
|
|
|
// A map from an id to its corresponding OpName and OpMemberName instructions.
|
|
|
|
std::unique_ptr<std::multimap<uint32_t, Instruction*>> id_to_name_;
|
2018-03-28 13:19:55 +00:00
|
|
|
|
|
|
|
// The cache scalar evolution analysis node.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<ScalarEvolutionAnalysis> scalar_evolution_analysis_;
|
2018-04-17 14:31:09 +00:00
|
|
|
|
|
|
|
// The liveness analysis |module_|.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<LivenessAnalysis> reg_pressure_;
|
2018-04-26 19:07:48 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<ValueNumberTable> vn_table_;
|
2018-07-05 18:19:50 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unique_ptr<InstructionFolder> inst_folder_;
|
2018-09-10 15:49:41 +00:00
|
|
|
|
2018-10-10 19:18:15 +00:00
|
|
|
std::unique_ptr<StructuredCFGAnalysis> struct_cfg_analysis_;
|
|
|
|
|
2018-09-10 15:49:41 +00:00
|
|
|
// The maximum legal value for the id bound.
|
|
|
|
uint32_t max_id_bound_;
|
2017-10-30 15:13:24 +00:00
|
|
|
};
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
inline IRContext::Analysis operator|(IRContext::Analysis lhs,
|
|
|
|
IRContext::Analysis rhs) {
|
|
|
|
return static_cast<IRContext::Analysis>(static_cast<int>(lhs) |
|
|
|
|
static_cast<int>(rhs));
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
inline IRContext::Analysis& operator|=(IRContext::Analysis& lhs,
|
|
|
|
IRContext::Analysis rhs) {
|
|
|
|
lhs = static_cast<IRContext::Analysis>(static_cast<int>(lhs) |
|
|
|
|
static_cast<int>(rhs));
|
2017-11-02 18:25:48 +00:00
|
|
|
return lhs;
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
inline IRContext::Analysis operator<<(IRContext::Analysis a, int shift) {
|
|
|
|
return static_cast<IRContext::Analysis>(static_cast<int>(a) << shift);
|
2017-11-02 18:25:48 +00:00
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
inline IRContext::Analysis& operator<<=(IRContext::Analysis& a, int shift) {
|
|
|
|
a = static_cast<IRContext::Analysis>(static_cast<int>(a) << shift);
|
2017-11-02 18:25:48 +00:00
|
|
|
return a;
|
|
|
|
}
|
|
|
|
|
2018-07-07 13:38:00 +00:00
|
|
|
std::vector<Instruction*> IRContext::GetConstants() {
|
2017-10-30 15:13:24 +00:00
|
|
|
return module()->GetConstants();
|
|
|
|
}
|
|
|
|
|
|
|
|
std::vector<const Instruction*> IRContext::GetConstants() const {
|
|
|
|
return ((const Module*)module())->GetConstants();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::annotation_begin() {
|
|
|
|
return module()->annotation_begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::annotation_end() {
|
|
|
|
return module()->annotation_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::inst_iterator> IRContext::annotations() {
|
|
|
|
return module_->annotations();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::const_inst_iterator> IRContext::annotations() const {
|
|
|
|
return ((const Module*)module_.get())->annotations();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::capability_begin() {
|
|
|
|
return module()->capability_begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::capability_end() {
|
|
|
|
return module()->capability_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::inst_iterator> IRContext::capabilities() {
|
|
|
|
return module()->capabilities();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::const_inst_iterator> IRContext::capabilities() const {
|
|
|
|
return ((const Module*)module())->capabilities();
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Module::inst_iterator IRContext::types_values_begin() {
|
2017-10-30 15:13:24 +00:00
|
|
|
return module()->types_values_begin();
|
|
|
|
}
|
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
Module::inst_iterator IRContext::types_values_end() {
|
2017-10-30 15:13:24 +00:00
|
|
|
return module()->types_values_end();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::inst_iterator> IRContext::types_values() {
|
|
|
|
return module()->types_values();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::const_inst_iterator> IRContext::types_values() const {
|
|
|
|
return ((const Module*)module_.get())->types_values();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::ext_inst_import_begin() {
|
|
|
|
return module()->ext_inst_import_begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::ext_inst_import_end() {
|
|
|
|
return module()->ext_inst_import_end();
|
|
|
|
}
|
|
|
|
|
2017-11-21 19:47:46 +00:00
|
|
|
IteratorRange<Module::inst_iterator> IRContext::ext_inst_imports() {
|
|
|
|
return module()->ext_inst_imports();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::const_inst_iterator> IRContext::ext_inst_imports() const {
|
|
|
|
return ((const Module*)module_.get())->ext_inst_imports();
|
|
|
|
}
|
|
|
|
|
2017-10-30 15:13:24 +00:00
|
|
|
Module::inst_iterator IRContext::debug1_begin() {
|
|
|
|
return module()->debug1_begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::debug1_end() { return module()->debug1_end(); }
|
|
|
|
|
|
|
|
IteratorRange<Module::inst_iterator> IRContext::debugs1() {
|
|
|
|
return module()->debugs1();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::const_inst_iterator> IRContext::debugs1() const {
|
|
|
|
return ((const Module*)module_.get())->debugs1();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::debug2_begin() {
|
|
|
|
return module()->debug2_begin();
|
|
|
|
}
|
|
|
|
Module::inst_iterator IRContext::debug2_end() { return module()->debug2_end(); }
|
|
|
|
|
|
|
|
IteratorRange<Module::inst_iterator> IRContext::debugs2() {
|
|
|
|
return module()->debugs2();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::const_inst_iterator> IRContext::debugs2() const {
|
|
|
|
return ((const Module*)module_.get())->debugs2();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::debug3_begin() {
|
|
|
|
return module()->debug3_begin();
|
|
|
|
}
|
|
|
|
|
|
|
|
Module::inst_iterator IRContext::debug3_end() { return module()->debug3_end(); }
|
|
|
|
|
|
|
|
IteratorRange<Module::inst_iterator> IRContext::debugs3() {
|
|
|
|
return module()->debugs3();
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<Module::const_inst_iterator> IRContext::debugs3() const {
|
|
|
|
return ((const Module*)module_.get())->debugs3();
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::debug_clear() { module_->debug_clear(); }
|
|
|
|
|
|
|
|
void IRContext::AddCapability(std::unique_ptr<Instruction>&& c) {
|
2017-11-11 01:26:55 +00:00
|
|
|
AddCombinatorsForCapability(c->GetSingleWordInOperand(0));
|
2017-10-30 15:13:24 +00:00
|
|
|
module()->AddCapability(std::move(c));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddExtension(std::unique_ptr<Instruction>&& e) {
|
2018-11-08 18:54:54 +00:00
|
|
|
if (AreAnalysesValid(kAnalysisDefUse)) {
|
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(e.get());
|
|
|
|
}
|
2017-10-30 15:13:24 +00:00
|
|
|
module()->AddExtension(std::move(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddExtInstImport(std::unique_ptr<Instruction>&& e) {
|
2017-11-11 01:26:55 +00:00
|
|
|
AddCombinatorsForExtension(e.get());
|
2017-10-30 15:13:24 +00:00
|
|
|
module()->AddExtInstImport(std::move(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::SetMemoryModel(std::unique_ptr<Instruction>&& m) {
|
|
|
|
module()->SetMemoryModel(std::move(m));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddEntryPoint(std::unique_ptr<Instruction>&& e) {
|
|
|
|
module()->AddEntryPoint(std::move(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddExecutionMode(std::unique_ptr<Instruction>&& e) {
|
|
|
|
module()->AddExecutionMode(std::move(e));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddDebug1Inst(std::unique_ptr<Instruction>&& d) {
|
|
|
|
module()->AddDebug1Inst(std::move(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddDebug2Inst(std::unique_ptr<Instruction>&& d) {
|
2018-02-13 04:28:38 +00:00
|
|
|
if (AreAnalysesValid(kAnalysisNameMap)) {
|
|
|
|
if (d->opcode() == SpvOpName || d->opcode() == SpvOpMemberName) {
|
|
|
|
id_to_name_->insert({d->result_id(), d.get()});
|
|
|
|
}
|
|
|
|
}
|
2017-10-30 15:13:24 +00:00
|
|
|
module()->AddDebug2Inst(std::move(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddDebug3Inst(std::unique_ptr<Instruction>&& d) {
|
|
|
|
module()->AddDebug3Inst(std::move(d));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddAnnotationInst(std::unique_ptr<Instruction>&& a) {
|
2017-11-13 20:31:43 +00:00
|
|
|
if (AreAnalysesValid(kAnalysisDecorations)) {
|
|
|
|
get_decoration_mgr()->AddDecoration(a.get());
|
|
|
|
}
|
2018-11-08 18:54:54 +00:00
|
|
|
if (AreAnalysesValid(kAnalysisDefUse)) {
|
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(a.get());
|
|
|
|
}
|
2017-10-30 15:13:24 +00:00
|
|
|
module()->AddAnnotationInst(std::move(a));
|
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddType(std::unique_ptr<Instruction>&& t) {
|
|
|
|
module()->AddType(std::move(t));
|
2018-03-06 16:20:28 +00:00
|
|
|
if (AreAnalysesValid(kAnalysisDefUse)) {
|
2018-07-04 17:17:30 +00:00
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(&*(--types_values_end()));
|
2018-03-06 16:20:28 +00:00
|
|
|
}
|
2017-10-30 15:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddGlobalValue(std::unique_ptr<Instruction>&& v) {
|
2018-03-06 16:20:28 +00:00
|
|
|
if (AreAnalysesValid(kAnalysisDefUse)) {
|
2018-11-08 18:54:54 +00:00
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(&*v);
|
2018-03-06 16:20:28 +00:00
|
|
|
}
|
2018-11-08 18:54:54 +00:00
|
|
|
module()->AddGlobalValue(std::move(v));
|
2017-10-30 15:13:24 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void IRContext::AddFunction(std::unique_ptr<Function>&& f) {
|
|
|
|
module()->AddFunction(std::move(f));
|
|
|
|
}
|
2017-11-11 01:26:55 +00:00
|
|
|
|
2017-12-11 18:10:24 +00:00
|
|
|
void IRContext::AnalyzeDefUse(Instruction* inst) {
|
|
|
|
if (AreAnalysesValid(kAnalysisDefUse)) {
|
|
|
|
get_def_use_mgr()->AnalyzeInstDefUse(inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-08 15:59:03 +00:00
|
|
|
void IRContext::UpdateDefUse(Instruction* inst) {
|
|
|
|
if (AreAnalysesValid(kAnalysisDefUse)) {
|
|
|
|
get_def_use_mgr()->UpdateDefUse(inst);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-02-13 04:28:38 +00:00
|
|
|
void IRContext::BuildIdToNameMap() {
|
2018-08-14 19:01:50 +00:00
|
|
|
id_to_name_ = MakeUnique<std::multimap<uint32_t, Instruction*>>();
|
2018-02-13 04:28:38 +00:00
|
|
|
for (Instruction& debug_inst : debugs2()) {
|
|
|
|
if (debug_inst.opcode() == SpvOpMemberName ||
|
|
|
|
debug_inst.opcode() == SpvOpName) {
|
|
|
|
id_to_name_->insert({debug_inst.GetSingleWordInOperand(0), &debug_inst});
|
|
|
|
}
|
|
|
|
}
|
|
|
|
valid_analyses_ = valid_analyses_ | kAnalysisNameMap;
|
|
|
|
}
|
|
|
|
|
|
|
|
IteratorRange<std::multimap<uint32_t, Instruction*>::iterator>
|
|
|
|
IRContext::GetNames(uint32_t id) {
|
|
|
|
if (!AreAnalysesValid(kAnalysisNameMap)) {
|
|
|
|
BuildIdToNameMap();
|
|
|
|
}
|
|
|
|
auto result = id_to_name_->equal_range(id);
|
|
|
|
return make_range(std::move(result.first), std::move(result.second));
|
|
|
|
}
|
|
|
|
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2017-10-30 15:13:24 +00:00
|
|
|
} // namespace spvtools
|
2018-07-07 13:38:00 +00:00
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_IR_CONTEXT_H_
|