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 predecessors 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, merge blocks come after all blocks that are in the control 64 // constructs of their header, and continue blocks come after all of the 65 // blocks in the body of their loop. 66 void ComputeStructuredOrder(Function* func, BasicBlock* root, 67 std::list<BasicBlock*>* order); 68 69 // Compute structured block order into |order| for |func| starting at |root| 70 // and ending at |end|. This order has the property that dominators come 71 // before all blocks they dominate, merge blocks come after all blocks that 72 // are in the control constructs of their header, and continue blocks come 73 // after all the blocks in the body of their loop. 74 void ComputeStructuredOrder(Function* func, BasicBlock* root, BasicBlock* end, 75 std::list<BasicBlock*>* order); 76 77 // Applies |f| to all blocks that can be reach from |bb| in post order. 78 void ForEachBlockInPostOrder(BasicBlock* bb, 79 const std::function<void(BasicBlock*)>& f); 80 81 // Applies |f| to all blocks that can be reach from |bb| in reverse post 82 // order. 83 void ForEachBlockInReversePostOrder( 84 BasicBlock* bb, const std::function<void(BasicBlock*)>& f); 85 86 // Applies |f| to all blocks that can be reach from |bb| in reverse post 87 // order. Return false if |f| return false on any basic block, and stops 88 // processing. 89 bool WhileEachBlockInReversePostOrder( 90 BasicBlock* bb, const std::function<bool(BasicBlock*)>& f); 91 92 // Registers |blk| as a basic block in the cfg, this also updates the 93 // predecessor lists of each successor of |blk|. |blk| must have a terminator 94 // instruction at the end of the block. RegisterBlock(BasicBlock * blk)95 void RegisterBlock(BasicBlock* blk) { 96 assert(blk->begin() != blk->end() && 97 "Basic blocks must have a terminator before registering."); 98 assert(blk->tail()->IsBlockTerminator() && 99 "Basic blocks must have a terminator before registering."); 100 uint32_t blk_id = blk->id(); 101 id2block_[blk_id] = blk; 102 AddEdges(blk); 103 } 104 105 // Removes from the CFG any mapping for the basic block id |blk_id|. ForgetBlock(const BasicBlock * blk)106 void ForgetBlock(const BasicBlock* blk) { 107 id2block_.erase(blk->id()); 108 label2preds_.erase(blk->id()); 109 RemoveSuccessorEdges(blk); 110 } 111 RemoveEdge(uint32_t pred_blk_id,uint32_t succ_blk_id)112 void RemoveEdge(uint32_t pred_blk_id, uint32_t succ_blk_id) { 113 auto pred_it = label2preds_.find(succ_blk_id); 114 if (pred_it == label2preds_.end()) return; 115 auto& preds_list = pred_it->second; 116 auto it = std::find(preds_list.begin(), preds_list.end(), pred_blk_id); 117 if (it != preds_list.end()) preds_list.erase(it); 118 } 119 120 // Registers |blk| to all of its successors. 121 void AddEdges(BasicBlock* blk); 122 123 // Registers the basic block id |pred_blk_id| as being a predecessor of the 124 // basic block id |succ_blk_id|. AddEdge(uint32_t pred_blk_id,uint32_t succ_blk_id)125 void AddEdge(uint32_t pred_blk_id, uint32_t succ_blk_id) { 126 label2preds_[succ_blk_id].push_back(pred_blk_id); 127 } 128 129 // Removes any edges that no longer exist from the predecessor mapping for 130 // the basic block id |blk_id|. 131 void RemoveNonExistingEdges(uint32_t blk_id); 132 133 // Remove all edges that leave |bb|. RemoveSuccessorEdges(const BasicBlock * bb)134 void RemoveSuccessorEdges(const BasicBlock* bb) { 135 bb->ForEachSuccessorLabel( 136 [bb, this](uint32_t succ_id) { RemoveEdge(bb->id(), succ_id); }); 137 } 138 139 // Divides |block| into two basic blocks. The first block will have the same 140 // id as |block| and will become a preheader for the loop. The other block 141 // is a new block that will be the new loop header. 142 // 143 // Returns a pointer to the new loop header. Returns |nullptr| if the new 144 // loop pointer could not be created. 145 BasicBlock* SplitLoopHeader(BasicBlock* bb); 146 147 private: 148 // Compute structured successors for function |func|. A block's structured 149 // successors are the blocks it branches to together with its declared merge 150 // block and continue block if it has them. When order matters, the merge 151 // block and continue block always appear first. This assures correct depth 152 // first search in the presence of early returns and kills. If the successor 153 // vector contain duplicates of the merge or continue blocks, they are safely 154 // ignored by DFS. 155 void ComputeStructuredSuccessors(Function* func); 156 157 // Computes the post-order traversal of the cfg starting at |bb| skipping 158 // nodes in |seen|. The order of the traversal is appended to |order|, and 159 // all nodes in the traversal are added to |seen|. 160 void ComputePostOrderTraversal(BasicBlock* bb, 161 std::vector<BasicBlock*>* order, 162 std::unordered_set<BasicBlock*>* seen); 163 164 // Module for this CFG. 165 Module* module_; 166 167 // Map from block to its structured successor blocks. See 168 // ComputeStructuredSuccessors() for definition. 169 std::unordered_map<const BasicBlock*, std::vector<BasicBlock*>> 170 block2structured_succs_; 171 172 // Extra block whose successors are all blocks with no predecessors 173 // in function. 174 BasicBlock pseudo_entry_block_; 175 176 // Augmented CFG Exit Block. 177 BasicBlock pseudo_exit_block_; 178 179 // Map from block's label id to its predecessor blocks ids 180 std::unordered_map<uint32_t, std::vector<uint32_t>> label2preds_; 181 182 // Map from block's label id to block. 183 std::unordered_map<uint32_t, BasicBlock*> id2block_; 184 }; 185 186 } // namespace opt 187 } // namespace spvtools 188 189 #endif // SOURCE_OPT_CFG_H_ 190