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