1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320
|
// Copyright (c) 2015-2016 The Khronos Group 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.
#ifndef SOURCE_VAL_BASIC_BLOCK_H_
#define SOURCE_VAL_BASIC_BLOCK_H_
#include <bitset>
#include <cstdint>
#include <functional>
#include <memory>
#include <vector>
#include "source/latest_version_spirv_header.h"
namespace spvtools {
namespace val {
enum BlockType : uint32_t {
kBlockTypeUndefined,
kBlockTypeSelection,
kBlockTypeLoop,
kBlockTypeMerge,
kBlockTypeBreak,
kBlockTypeContinue,
kBlockTypeReturn,
kBlockTypeCOUNT ///< Total number of block types. (must be the last element)
};
class Instruction;
// This class represents a basic block in a SPIR-V module
class BasicBlock {
public:
/// Constructor for a BasicBlock
///
/// @param[in] id The ID of the basic block
explicit BasicBlock(uint32_t id);
/// Returns the id of the BasicBlock
uint32_t id() const { return id_; }
/// Returns the predecessors of the BasicBlock
const std::vector<BasicBlock*>* predecessors() const {
return &predecessors_;
}
/// Returns the predecessors of the BasicBlock
std::vector<BasicBlock*>* predecessors() { return &predecessors_; }
/// Returns the successors of the BasicBlock
const std::vector<BasicBlock*>* successors() const { return &successors_; }
/// Returns the successors of the BasicBlock
std::vector<BasicBlock*>* successors() { return &successors_; }
/// Returns the structural successors of the BasicBlock
std::vector<BasicBlock*>* structural_predecessors() {
return &structural_predecessors_;
}
/// Returns the structural predecessors of the BasicBlock
const std::vector<BasicBlock*>* structural_predecessors() const {
return &structural_predecessors_;
}
/// Returns the structural successors of the BasicBlock
std::vector<BasicBlock*>* structural_successors() {
return &structural_successors_;
}
/// Returns the structural predecessors of the BasicBlock
const std::vector<BasicBlock*>* structural_successors() const {
return &structural_successors_;
}
/// Returns true if the block is reachable in the CFG.
bool reachable() const { return reachable_; }
/// Returns true if the block is structurally reachable in the CFG.
bool structurally_reachable() const { return structurally_reachable_; }
/// Returns true if BasicBlock is of the given type
bool is_type(BlockType type) const {
if (type == kBlockTypeUndefined) return type_.none();
return type_.test(type);
}
/// Sets the reachability of the basic block in the CFG
void set_reachable(bool reachability) { reachable_ = reachability; }
/// Sets the structural reachability of the basic block in the CFG
void set_structurally_reachable(bool reachability) {
structurally_reachable_ = reachability;
}
/// Sets the type of the BasicBlock
void set_type(BlockType type) {
if (type == kBlockTypeUndefined)
type_.reset();
else
type_.set(type);
}
/// Sets the immediate dominator of this basic block
///
/// @param[in] dom_block The dominator block
void SetImmediateDominator(BasicBlock* dom_block);
/// Sets the immediate dominator of this basic block
///
/// @param[in] dom_block The dominator block
void SetImmediateStructuralDominator(BasicBlock* dom_block);
/// Sets the immediate post dominator of this basic block
///
/// @param[in] pdom_block The post dominator block
void SetImmediateStructuralPostDominator(BasicBlock* pdom_block);
/// Returns the immediate dominator of this basic block
BasicBlock* immediate_dominator();
/// Returns the immediate dominator of this basic block
const BasicBlock* immediate_dominator() const;
/// Returns the immediate dominator of this basic block
BasicBlock* immediate_structural_dominator();
/// Returns the immediate dominator of this basic block
const BasicBlock* immediate_structural_dominator() const;
/// Returns the immediate post dominator of this basic block
BasicBlock* immediate_structural_post_dominator();
/// Returns the immediate post dominator of this basic block
const BasicBlock* immediate_structural_post_dominator() const;
/// Returns the label instruction for the block, or nullptr if not set.
const Instruction* label() const { return label_; }
//// Registers the label instruction for the block.
void set_label(const Instruction* t) { label_ = t; }
/// Registers the terminator instruction for the block.
void set_terminator(const Instruction* t) { terminator_ = t; }
/// Returns the terminator instruction for the block.
const Instruction* terminator() const { return terminator_; }
/// Adds @p next BasicBlocks as successors of this BasicBlock
void RegisterSuccessors(
const std::vector<BasicBlock*>& next = std::vector<BasicBlock*>());
/// Returns true if the id of the BasicBlock matches
bool operator==(const BasicBlock& other) const { return other.id_ == id_; }
/// Returns true if the id of the BasicBlock matches
bool operator==(const uint32_t& other_id) const { return other_id == id_; }
/// Returns true if this block dominates the other block.
/// Assumes dominators have been computed.
bool dominates(const BasicBlock& other) const;
/// Returns true if this block structurally dominates the other block.
/// Assumes structural dominators have been computed.
bool structurally_dominates(const BasicBlock& other) const;
/// Returns true if this block structurally postdominates the other block.
/// Assumes structural dominators have been computed.
bool structurally_postdominates(const BasicBlock& other) const;
void RegisterStructuralSuccessor(BasicBlock* block) {
block->structural_predecessors_.push_back(this);
structural_successors_.push_back(block);
}
/// @brief A BasicBlock dominator iterator class
///
/// This iterator will iterate over the (post)dominators of the block
class DominatorIterator {
public:
using iterator_category = std::forward_iterator_tag;
using value_type = BasicBlock*;
using pointer = value_type*;
using reference = value_type&;
using difference_type = std::ptrdiff_t;
/// @brief Constructs the end of dominator iterator
///
/// This will create an iterator which will represent the element
/// before the root node of the dominator tree
DominatorIterator();
/// @brief Constructs an iterator for the given block which points to
/// @p block
///
/// @param block The block which is referenced by the iterator
/// @param dominator_func This function will be called to get the immediate
/// (post)dominator of the current block
DominatorIterator(
const BasicBlock* block,
std::function<const BasicBlock*(const BasicBlock*)> dominator_func);
/// @brief Advances the iterator
DominatorIterator& operator++();
/// @brief Returns the current element
const BasicBlock*& operator*();
friend bool operator==(const DominatorIterator& lhs,
const DominatorIterator& rhs);
private:
const BasicBlock* current_;
std::function<const BasicBlock*(const BasicBlock*)> dom_func_;
};
/// Returns a dominator iterator which points to the current block
const DominatorIterator dom_begin() const;
/// Returns a dominator iterator which points to the current block
DominatorIterator dom_begin();
/// Returns a dominator iterator which points to one element past the first
/// block
const DominatorIterator dom_end() const;
/// Returns a dominator iterator which points to one element past the first
/// block
DominatorIterator dom_end();
/// Returns a dominator iterator which points to the current block
const DominatorIterator structural_dom_begin() const;
/// Returns a dominator iterator which points to the current block
DominatorIterator structural_dom_begin();
/// Returns a dominator iterator which points to one element past the first
/// block
const DominatorIterator structural_dom_end() const;
/// Returns a dominator iterator which points to one element past the first
/// block
DominatorIterator structural_dom_end();
/// Returns a post dominator iterator which points to the current block
const DominatorIterator structural_pdom_begin() const;
/// Returns a post dominator iterator which points to the current block
DominatorIterator structural_pdom_begin();
/// Returns a post dominator iterator which points to one element past the
/// last block
const DominatorIterator structural_pdom_end() const;
/// Returns a post dominator iterator which points to one element past the
/// last block
DominatorIterator structural_pdom_end();
private:
/// Id of the BasicBlock
const uint32_t id_;
/// Pointer to the immediate dominator of the BasicBlock
BasicBlock* immediate_dominator_;
/// Pointer to the immediate structural dominator of the BasicBlock
BasicBlock* immediate_structural_dominator_;
/// Pointer to the immediate structural post dominator of the BasicBlock
BasicBlock* immediate_structural_post_dominator_;
/// The set of predecessors of the BasicBlock
std::vector<BasicBlock*> predecessors_;
/// The set of successors of the BasicBlock
std::vector<BasicBlock*> successors_;
/// The type of the block
std::bitset<kBlockTypeCOUNT> type_;
/// True if the block is reachable in the CFG
bool reachable_;
/// True if the block is structurally reachable in the CFG
bool structurally_reachable_;
/// label of this block, if any.
const Instruction* label_;
/// Terminator of this block.
const Instruction* terminator_;
std::vector<BasicBlock*> structural_predecessors_;
std::vector<BasicBlock*> structural_successors_;
};
/// @brief Returns true if the iterators point to the same element or if both
/// iterators point to the @p dom_end block
bool operator==(const BasicBlock::DominatorIterator& lhs,
const BasicBlock::DominatorIterator& rhs);
/// @brief Returns true if the iterators point to different elements and they
/// do not both point to the @p dom_end block
bool operator!=(const BasicBlock::DominatorIterator& lhs,
const BasicBlock::DominatorIterator& rhs);
} // namespace val
} // namespace spvtools
#endif // SOURCE_VAL_BASIC_BLOCK_H_
|