2017-10-30 21:42:26 +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_CFG_H_
|
|
|
|
#define SOURCE_OPT_CFG_H_
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
#include <algorithm>
|
2017-10-30 21:42:26 +00:00
|
|
|
#include <list>
|
|
|
|
#include <unordered_map>
|
2018-02-02 16:55:05 +00:00
|
|
|
#include <unordered_set>
|
2018-08-03 19:06:09 +00:00
|
|
|
#include <vector>
|
|
|
|
|
|
|
|
#include "source/opt/basic_block.h"
|
2017-10-30 21:42:26 +00:00
|
|
|
|
|
|
|
namespace spvtools {
|
2018-07-09 15:32:29 +00:00
|
|
|
namespace opt {
|
2017-10-30 21:42:26 +00:00
|
|
|
|
|
|
|
class CFG {
|
|
|
|
public:
|
2018-07-12 19:14:43 +00:00
|
|
|
explicit CFG(Module* module);
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2022-01-26 20:13:08 +00:00
|
|
|
// Return the list of predecessors for basic block with label |blkid|.
|
2018-07-12 19:14:43 +00:00
|
|
|
// TODO(dnovillo): Move this to BasicBlock.
|
2017-10-30 21:42:26 +00:00
|
|
|
const std::vector<uint32_t>& preds(uint32_t blk_id) const {
|
2018-03-06 16:20:28 +00:00
|
|
|
assert(label2preds_.count(blk_id));
|
2017-10-30 21:42:26 +00:00
|
|
|
return label2preds_.at(blk_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return a pointer to the basic block instance corresponding to the label
|
|
|
|
// |blk_id|.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* block(uint32_t blk_id) const { return id2block_.at(blk_id); }
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2017-11-27 19:20:32 +00:00
|
|
|
// Return the pseudo entry and exit blocks.
|
2018-07-12 19:14:43 +00:00
|
|
|
const BasicBlock* pseudo_entry_block() const { return &pseudo_entry_block_; }
|
|
|
|
BasicBlock* pseudo_entry_block() { return &pseudo_entry_block_; }
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2018-07-12 19:14:43 +00:00
|
|
|
const BasicBlock* pseudo_exit_block() const { return &pseudo_exit_block_; }
|
|
|
|
BasicBlock* pseudo_exit_block() { return &pseudo_exit_block_; }
|
2017-10-30 21:42:26 +00:00
|
|
|
|
|
|
|
// Return true if |block_ptr| is the pseudo-entry block.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsPseudoEntryBlock(BasicBlock* block_ptr) const {
|
2017-10-30 21:42:26 +00:00
|
|
|
return block_ptr == &pseudo_entry_block_;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return true if |block_ptr| is the pseudo-exit block.
|
2018-07-12 19:14:43 +00:00
|
|
|
bool IsPseudoExitBlock(BasicBlock* block_ptr) const {
|
2017-10-30 21:42:26 +00:00
|
|
|
return block_ptr == &pseudo_exit_block_;
|
|
|
|
}
|
|
|
|
|
2017-11-17 13:59:25 +00:00
|
|
|
// Compute structured block order into |order| for |func| starting at |root|.
|
|
|
|
// This order has the property that dominators come before all blocks they
|
2019-10-01 14:27:09 +00:00
|
|
|
// dominate, merge blocks come after all blocks that are in the control
|
|
|
|
// constructs of their header, and continue blocks come after all of the
|
|
|
|
// blocks in the body of their loop.
|
2018-07-12 19:14:43 +00:00
|
|
|
void ComputeStructuredOrder(Function* func, BasicBlock* root,
|
|
|
|
std::list<BasicBlock*>* order);
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2022-06-29 13:53:26 +00:00
|
|
|
// Compute structured block order into |order| for |func| starting at |root|
|
|
|
|
// and ending at |end|. This order has the property that dominators come
|
|
|
|
// before all blocks they dominate, merge blocks come after all blocks that
|
|
|
|
// are in the control constructs of their header, and continue blocks come
|
|
|
|
// after all the blocks in the body of their loop.
|
|
|
|
void ComputeStructuredOrder(Function* func, BasicBlock* root, BasicBlock* end,
|
|
|
|
std::list<BasicBlock*>* order);
|
|
|
|
|
2019-09-10 13:38:23 +00:00
|
|
|
// Applies |f| to all blocks that can be reach from |bb| in post order.
|
2018-04-17 14:31:09 +00:00
|
|
|
void ForEachBlockInPostOrder(BasicBlock* bb,
|
|
|
|
const std::function<void(BasicBlock*)>& f);
|
|
|
|
|
2019-09-10 13:38:23 +00:00
|
|
|
// Applies |f| to all blocks that can be reach from |bb| in reverse post
|
|
|
|
// order.
|
2018-02-02 16:55:05 +00:00
|
|
|
void ForEachBlockInReversePostOrder(
|
|
|
|
BasicBlock* bb, const std::function<void(BasicBlock*)>& f);
|
|
|
|
|
2019-09-10 13:38:23 +00:00
|
|
|
// Applies |f| to all blocks that can be reach from |bb| in reverse post
|
|
|
|
// order. Return false if |f| return false on any basic block, and stops
|
|
|
|
// processing.
|
|
|
|
bool WhileEachBlockInReversePostOrder(
|
|
|
|
BasicBlock* bb, const std::function<bool(BasicBlock*)>& f);
|
|
|
|
|
2018-01-26 12:07:10 +00:00
|
|
|
// Registers |blk| as a basic block in the cfg, this also updates the
|
2018-09-18 12:52:47 +00:00
|
|
|
// predecessor lists of each successor of |blk|. |blk| must have a terminator
|
|
|
|
// instruction at the end of the block.
|
2018-07-12 19:14:43 +00:00
|
|
|
void RegisterBlock(BasicBlock* blk) {
|
2018-09-18 12:52:47 +00:00
|
|
|
assert(blk->begin() != blk->end() &&
|
|
|
|
"Basic blocks must have a terminator before registering.");
|
|
|
|
assert(blk->tail()->IsBlockTerminator() &&
|
|
|
|
"Basic blocks must have a terminator before registering.");
|
2018-01-26 12:07:10 +00:00
|
|
|
uint32_t blk_id = blk->id();
|
|
|
|
id2block_[blk_id] = blk;
|
|
|
|
AddEdges(blk);
|
|
|
|
}
|
|
|
|
|
2018-02-12 21:42:15 +00:00
|
|
|
// Removes from the CFG any mapping for the basic block id |blk_id|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void ForgetBlock(const BasicBlock* blk) {
|
2018-02-12 21:42:15 +00:00
|
|
|
id2block_.erase(blk->id());
|
|
|
|
label2preds_.erase(blk->id());
|
2018-03-06 16:20:28 +00:00
|
|
|
RemoveSuccessorEdges(blk);
|
2018-02-12 21:42:15 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
void RemoveEdge(uint32_t pred_blk_id, uint32_t succ_blk_id) {
|
|
|
|
auto pred_it = label2preds_.find(succ_blk_id);
|
|
|
|
if (pred_it == label2preds_.end()) return;
|
|
|
|
auto& preds_list = pred_it->second;
|
|
|
|
auto it = std::find(preds_list.begin(), preds_list.end(), pred_blk_id);
|
|
|
|
if (it != preds_list.end()) preds_list.erase(it);
|
|
|
|
}
|
|
|
|
|
2018-01-26 12:07:10 +00:00
|
|
|
// Registers |blk| to all of its successors.
|
2018-07-12 19:14:43 +00:00
|
|
|
void AddEdges(BasicBlock* blk);
|
2018-01-26 12:07:10 +00:00
|
|
|
|
|
|
|
// Registers the basic block id |pred_blk_id| as being a predecessor of the
|
|
|
|
// basic block id |succ_blk_id|.
|
|
|
|
void AddEdge(uint32_t pred_blk_id, uint32_t succ_blk_id) {
|
|
|
|
label2preds_[succ_blk_id].push_back(pred_blk_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
// Removes any edges that no longer exist from the predecessor mapping for
|
|
|
|
// the basic block id |blk_id|.
|
|
|
|
void RemoveNonExistingEdges(uint32_t blk_id);
|
|
|
|
|
2018-03-06 16:20:28 +00:00
|
|
|
// Remove all edges that leave |bb|.
|
2018-07-12 19:14:43 +00:00
|
|
|
void RemoveSuccessorEdges(const BasicBlock* bb) {
|
2018-03-06 16:20:28 +00:00
|
|
|
bb->ForEachSuccessorLabel(
|
|
|
|
[bb, this](uint32_t succ_id) { RemoveEdge(bb->id(), succ_id); });
|
|
|
|
}
|
|
|
|
|
|
|
|
// Divides |block| into two basic blocks. The first block will have the same
|
|
|
|
// id as |block| and will become a preheader for the loop. The other block
|
2018-03-21 16:52:13 +00:00
|
|
|
// is a new block that will be the new loop header.
|
2018-03-06 16:20:28 +00:00
|
|
|
//
|
2018-12-06 14:07:00 +00:00
|
|
|
// Returns a pointer to the new loop header. Returns |nullptr| if the new
|
|
|
|
// loop pointer could not be created.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock* SplitLoopHeader(BasicBlock* bb);
|
2018-03-06 16:20:28 +00:00
|
|
|
|
2017-10-30 21:42:26 +00:00
|
|
|
private:
|
|
|
|
// Compute structured successors for function |func|. A block's structured
|
|
|
|
// successors are the blocks it branches to together with its declared merge
|
|
|
|
// block and continue block if it has them. When order matters, the merge
|
|
|
|
// block and continue block always appear first. This assures correct depth
|
|
|
|
// first search in the presence of early returns and kills. If the successor
|
|
|
|
// vector contain duplicates of the merge or continue blocks, they are safely
|
|
|
|
// ignored by DFS.
|
2018-07-12 19:14:43 +00:00
|
|
|
void ComputeStructuredSuccessors(Function* func);
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2018-02-02 16:55:05 +00:00
|
|
|
// Computes the post-order traversal of the cfg starting at |bb| skipping
|
|
|
|
// nodes in |seen|. The order of the traversal is appended to |order|, and
|
|
|
|
// all nodes in the traversal are added to |seen|.
|
|
|
|
void ComputePostOrderTraversal(BasicBlock* bb,
|
|
|
|
std::vector<BasicBlock*>* order,
|
|
|
|
std::unordered_set<BasicBlock*>* seen);
|
|
|
|
|
2017-10-30 21:42:26 +00:00
|
|
|
// Module for this CFG.
|
2018-07-12 19:14:43 +00:00
|
|
|
Module* module_;
|
2017-10-30 21:42:26 +00:00
|
|
|
|
|
|
|
// Map from block to its structured successor blocks. See
|
|
|
|
// ComputeStructuredSuccessors() for definition.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<const BasicBlock*, std::vector<BasicBlock*>>
|
2017-10-30 21:42:26 +00:00
|
|
|
block2structured_succs_;
|
|
|
|
|
|
|
|
// Extra block whose successors are all blocks with no predecessors
|
2017-11-27 19:20:32 +00:00
|
|
|
// in function.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock pseudo_entry_block_;
|
2017-10-30 21:42:26 +00:00
|
|
|
|
2017-11-27 19:20:32 +00:00
|
|
|
// Augmented CFG Exit Block.
|
2018-07-12 19:14:43 +00:00
|
|
|
BasicBlock pseudo_exit_block_;
|
2017-10-30 21:42:26 +00:00
|
|
|
|
|
|
|
// Map from block's label id to its predecessor blocks ids
|
|
|
|
std::unordered_map<uint32_t, std::vector<uint32_t>> label2preds_;
|
|
|
|
|
|
|
|
// Map from block's label id to block.
|
2018-07-12 19:14:43 +00:00
|
|
|
std::unordered_map<uint32_t, BasicBlock*> id2block_;
|
2017-10-30 21:42:26 +00:00
|
|
|
};
|
|
|
|
|
2018-07-09 15:32:29 +00:00
|
|
|
} // namespace opt
|
2017-10-30 21:42:26 +00:00
|
|
|
} // namespace spvtools
|
|
|
|
|
2018-08-03 12:05:33 +00:00
|
|
|
#endif // SOURCE_OPT_CFG_H_
|