alan-baker | 06cad65 | 2019-12-03 17:56:47 -0500 | [diff] [blame^] | 1 | // Copyright 2019 The Clspv Authors. All rights reserved. |
| 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 | #include "ComputeStructuredOrder.h" |
| 16 | |
| 17 | using namespace clspv; |
| 18 | using namespace llvm; |
| 19 | |
| 20 | void clspv::ComputeStructuredOrder(BasicBlock *block, DominatorTree *DT, |
| 21 | const LoopInfo &LI, |
| 22 | std::deque<BasicBlock *> *order, |
| 23 | DenseSet<BasicBlock *> *visited) { |
| 24 | if (!visited->insert(block).second) |
| 25 | return; |
| 26 | |
| 27 | auto F = block->getParent(); |
| 28 | |
| 29 | // Identify the merge and continue blocks for special treatment. |
| 30 | const auto *terminator = block->getTerminator(); |
| 31 | BasicBlock *continue_block = nullptr; |
| 32 | BasicBlock *merge_block = nullptr; |
| 33 | if (LI.isLoopHeader(block)) { |
| 34 | Loop *loop = LI.getLoopFor(block); |
| 35 | merge_block = loop->getExitBlock(); |
| 36 | |
| 37 | if (loop->isLoopLatch(block)) { |
| 38 | // The header is also the latch (i.e. a single block loop). |
| 39 | continue_block = block; |
| 40 | } else { |
| 41 | // The continue block satisfies the following conditions: |
| 42 | // 1. Is dominated by the header (true by construction at this point). |
| 43 | // 2. Dominates the latch block. |
| 44 | // We can assume the loop has multiple blocks since the single block loop |
| 45 | // is handled above. By construction the header always dominates the |
| 46 | // latch block, so we exclude that case specifically. |
| 47 | auto *header = loop->getHeader(); |
| 48 | auto *latch = loop->getLoopLatch(); |
| 49 | for (auto *bb : loop->blocks()) { |
| 50 | if (bb == header) |
| 51 | continue; |
| 52 | |
| 53 | // Several block might dominate the latch, we can pick any. |
| 54 | if (DT->dominates(bb, latch)) |
| 55 | continue_block = bb; |
| 56 | } |
| 57 | } |
| 58 | // At least the latch dominates itself so we will always find a continue |
| 59 | // block. |
| 60 | assert(continue_block && merge_block && "Bad loop"); |
| 61 | } else if (terminator->getNumSuccessors() > 1) { |
| 62 | // This is potentially a selection case, but it could also be a conditional |
| 63 | // branch with one arm back to the loop header, which would make this block |
| 64 | // the latch block. |
| 65 | bool has_back_edge = false; |
| 66 | |
| 67 | for (unsigned i = 0; i < terminator->getNumSuccessors(); ++i) { |
| 68 | if (LI.isLoopHeader(terminator->getSuccessor(i))) |
| 69 | has_back_edge = true; |
| 70 | } |
| 71 | |
| 72 | if (!has_back_edge) { |
| 73 | // The cfg structurizer generates a cfg where the true branch goes into |
| 74 | // the then/else region and the false branch skips the region. Therefore, |
| 75 | // we use the false branch here as the merge. |
| 76 | merge_block = terminator->getSuccessor(1); |
| 77 | } |
| 78 | } |
| 79 | |
| 80 | // Traverse merge and continue first. |
| 81 | if (merge_block) { |
| 82 | ComputeStructuredOrder(merge_block, DT, LI, order, visited); |
| 83 | if (continue_block) |
| 84 | ComputeStructuredOrder(continue_block, DT, LI, order, visited); |
| 85 | } |
| 86 | for (unsigned i = 0; i < terminator->getNumSuccessors(); ++i) { |
| 87 | auto *successor = terminator->getSuccessor(i); |
| 88 | if (successor != merge_block && successor != continue_block) |
| 89 | ComputeStructuredOrder(successor, DT, LI, order, visited); |
| 90 | } |
| 91 | |
| 92 | order->push_front(block); |
| 93 | } |