1 // Copyright (c) 2017 Google Inc. 2 // 3 // Licensed under the Apache License, Version 2.0 (the "License"); 4 // you may not use this file except in compliance with the License. 5 // You may obtain a copy of the License at 6 // 7 // http://www.apache.org/licenses/LICENSE-2.0 8 // 9 // Unless required by applicable law or agreed to in writing, software 10 // distributed under the License is distributed on an "AS IS" BASIS, 11 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 // See the License for the specific language governing permissions and 13 // limitations under the License. 14 15 #ifndef SOURCE_OPT_CFG_H_ 16 #define SOURCE_OPT_CFG_H_ 17 18 #include <algorithm> 19 #include <list> 20 #include <unordered_map> 21 #include <unordered_set> 22 #include <vector> 23 24 #include "source/opt/basic_block.h" 25 26 namespace spvtools { 27 namespace opt { 28 29 class CFG { 30 public: 31 explicit CFG(Module* module); 32 33 // Return the list of predecesors for basic block with label |blkid|. 34 // TODO(dnovillo): Move this to BasicBlock. preds(uint32_t blk_id)35 const std::vector<uint32_t>& preds(uint32_t blk_id) const { 36 assert(label2preds_.count(blk_id)); 37 return label2preds_.at(blk_id); 38 } 39 40 // Return a pointer to the basic block instance corresponding to the label 41 // |blk_id|. block(uint32_t blk_id)42 BasicBlock* block(uint32_t blk_id) const { return id2block_.at(blk_id); } 43 44 // Return the pseudo entry and exit blocks. pseudo_entry_block()45 const BasicBlock* pseudo_entry_block() const { return &pseudo_entry_block_; } pseudo_entry_block()46 BasicBlock* pseudo_entry_block() { return &pseudo_entry_block_; } 47 pseudo_exit_block()48 const BasicBlock* pseudo_exit_block() const { return &pseudo_exit_block_; } pseudo_exit_block()49 BasicBlock* pseudo_exit_block() { return &pseudo_exit_block_; } 50 51 // Return true if |block_ptr| is the pseudo-entry block. IsPseudoEntryBlock(BasicBlock * block_ptr)52 bool IsPseudoEntryBlock(BasicBlock* block_ptr) const { 53 return block_ptr == &pseudo_entry_block_; 54 } 55 56 // Return true if |block_ptr| is the pseudo-exit block. IsPseudoExitBlock(BasicBlock * block_ptr)57 bool IsPseudoExitBlock(BasicBlock* block_ptr) const { 58 return block_ptr == &pseudo_exit_block_; 59 } 60 61 // Compute structured block order into |order| for |func| starting at |root|. 62 // This order has the property that dominators come before all blocks they 63 // dominate and merge blocks come after all blocks that are in the control 64 // constructs of their header. 65 void ComputeStructuredOrder(Function* func, BasicBlock* root, 66 std::list<BasicBlock*>* order); 67 68 // Applies |f| to the basic block in post order starting with |bb|. 69 // Note that basic blocks that cannot be reached from |bb| node will not be 70 // processed. 71 void ForEachBlockInPostOrder(BasicBlock* bb, 72 const std::function<void(BasicBlock*)>& f); 73 74 // Applies |f| to the basic block in reverse post order starting with |bb|. 75 // Note that basic blocks that cannot be reached from |bb| node will not be 76 // processed. 77 void ForEachBlockInReversePostOrder( 78 BasicBlock* bb, const std::function<void(BasicBlock*)>& f); 79 80 // Registers |blk| as a basic block in the cfg, this also updates the 81 // predecessor lists of each successor of |blk|. |blk| must have a terminator 82 // instruction at the end of the block. RegisterBlock(BasicBlock * blk)83 void RegisterBlock(BasicBlock* blk) { 84 assert(blk->begin() != blk->end() && 85 "Basic blocks must have a terminator before registering."); 86 assert(blk->tail()->IsBlockTerminator() && 87 "Basic blocks must have a terminator before registering."); 88 uint32_t blk_id = blk->id(); 89 id2block_[blk_id] = blk; 90 AddEdges(blk); 91 } 92 93 // Removes from the CFG any mapping for the basic block id |blk_id|. ForgetBlock(const BasicBlock * blk)94 void ForgetBlock(const BasicBlock* blk) { 95 id2block_.erase(blk->id()); 96 label2preds_.erase(blk->id()); 97 RemoveSuccessorEdges(blk); 98 } 99 RemoveEdge(uint32_t pred_blk_id,uint32_t succ_blk_id)100 void RemoveEdge(uint32_t pred_blk_id, uint32_t succ_blk_id) { 101 auto pred_it = label2preds_.find(succ_blk_id); 102 if (pred_it == label2preds_.end()) return; 103 auto& preds_list = pred_it->second; 104 auto it = std::find(preds_list.begin(), preds_list.end(), pred_blk_id); 105 if (it != preds_list.end()) preds_list.erase(it); 106 } 107 108 // Registers |blk| to all of its successors. 109 void AddEdges(BasicBlock* blk); 110 111 // Registers the basic block id |pred_blk_id| as being a predecessor of the 112 // basic block id |succ_blk_id|. AddEdge(uint32_t pred_blk_id,uint32_t succ_blk_id)113 void AddEdge(uint32_t pred_blk_id, uint32_t succ_blk_id) { 114 label2preds_[succ_blk_id].push_back(pred_blk_id); 115 } 116 117 // Removes any edges that no longer exist from the predecessor mapping for 118 // the basic block id |blk_id|. 119 void RemoveNonExistingEdges(uint32_t blk_id); 120 121 // Remove all edges that leave |bb|. RemoveSuccessorEdges(const BasicBlock * bb)122 void RemoveSuccessorEdges(const BasicBlock* bb) { 123 bb->ForEachSuccessorLabel( 124 [bb, this](uint32_t succ_id) { RemoveEdge(bb->id(), succ_id); }); 125 } 126 127 // Divides |block| into two basic blocks. The first block will have the same 128 // id as |block| and will become a preheader for the loop. The other block 129 // is a new block that will be the new loop header. 130 // 131 // Returns a pointer to the new loop header. Returns |nullptr| if the new 132 // loop pointer could not be created. 133 BasicBlock* SplitLoopHeader(BasicBlock* bb); 134 135 private: 136 // Compute structured successors for function |func|. A block's structured 137 // successors are the blocks it branches to together with its declared merge 138 // block and continue block if it has them. When order matters, the merge 139 // block and continue block always appear first. This assures correct depth 140 // first search in the presence of early returns and kills. If the successor 141 // vector contain duplicates of the merge or continue blocks, they are safely 142 // ignored by DFS. 143 void ComputeStructuredSuccessors(Function* func); 144 145 // Computes the post-order traversal of the cfg starting at |bb| skipping 146 // nodes in |seen|. The order of the traversal is appended to |order|, and 147 // all nodes in the traversal are added to |seen|. 148 void ComputePostOrderTraversal(BasicBlock* bb, 149 std::vector<BasicBlock*>* order, 150 std::unordered_set<BasicBlock*>* seen); 151 152 // Module for this CFG. 153 Module* module_; 154 155 // Map from block to its structured successor blocks. See 156 // ComputeStructuredSuccessors() for definition. 157 std::unordered_map<const BasicBlock*, std::vector<BasicBlock*>> 158 block2structured_succs_; 159 160 // Extra block whose successors are all blocks with no predecessors 161 // in function. 162 BasicBlock pseudo_entry_block_; 163 164 // Augmented CFG Exit Block. 165 BasicBlock pseudo_exit_block_; 166 167 // Map from block's label id to its predecessor blocks ids 168 std::unordered_map<uint32_t, std::vector<uint32_t>> label2preds_; 169 170 // Map from block's label id to block. 171 std::unordered_map<uint32_t, BasicBlock*> id2block_; 172 }; 173 174 } // namespace opt 175 } // namespace spvtools 176 177 #endif // SOURCE_OPT_CFG_H_ 178