| 1 |
/*========================== begin_copyright_notice ============================ |
| 2 |
|
| 3 |
Copyright (C) 2017-2021 Intel Corporation |
| 4 |
|
| 5 |
SPDX-License-Identifier: MIT |
| 6 |
|
| 7 |
============================= end_copyright_notice ===========================*/ |
| 8 |
|
| 9 |
#include "Traversals.hpp" |
| 10 |
#include "../asserts.hpp" |
| 11 |
|
| 12 |
#include <algorithm> |
| 13 |
#include <cstring> |
| 14 |
#include <sstream> |
| 15 |
|
| 16 |
using namespace iga; |
| 17 |
|
| 18 |
void BranchIterator::findNext() { |
| 19 |
while (m_currSrcIx < m_srcCount) { |
| 20 |
const auto &op = m_inst->getSource(m_currSrcIx); |
| 21 |
if (op.getKind() == Operand::Kind::LABEL) { |
| 22 |
auto b = m_inst->getSource(m_currSrcIx).getTargetBlock(); |
| 23 |
// FIXME!! What if it's an empty empty?? |
| 24 |
// jmpi FOO |
| 25 |
// FOO: // oops, foo is empty! |
| 26 |
// BAR: |
| 27 |
// op |
| 28 |
// |
| 29 |
// we must either: |
| 30 |
// A. define an IR normal form with coalesced blocks |
| 31 |
// B. keep underlying block iterator copies around |
| 32 |
// so we can walk forward searching for a non-empty block |
| 33 |
// C. add a pointer links to Block (seems easiest) |
| 34 |
m_nextTarget = b->getInstList().front(); |
| 35 |
break; |
| 36 |
} |
| 37 |
m_currSrcIx++; |
| 38 |
} |
| 39 |
if (m_currSrcIx == m_srcCount) { |
| 40 |
m_nextTarget = nullptr; |
| 41 |
} |
| 42 |
} |
| 43 |
|
| 44 |
BranchIterator::BranchIterator(Instruction *_inst, size_t _nextIx) |
| 45 |
: m_inst(_inst), m_currSrcIx(_nextIx), m_srcCount(_inst->getSourceCount()), |
| 46 |
m_nextTarget(nullptr) { |
| 47 |
if (!m_inst->isBranching()) { // early out |
| 48 |
m_currSrcIx = m_srcCount; |
| 49 |
} else { |
| 50 |
findNext(); |
| 51 |
} |
| 52 |
} |
| 53 |
|
| 54 |
KernelIterator::KernelIterator(const BlockList::iterator &_bi, |
| 55 |
const BlockList::iterator &_be) |
| 56 |
: bi(_bi), be(_be) { |
| 57 |
nextBlock(); |
| 58 |
// invariants established |
| 59 |
IGA_ASSERT(bi == be || (ii != ie && m_currInst && m_currBlock), |
| 60 |
"KernelIterator construction invariants failed"); |
| 61 |
} |
| 62 |
|
| 63 |
Instruction *KernelIterator::currInst() { |
| 64 |
IGA_ASSERT(m_currInst, "KernelIterator at end"); |
| 65 |
return m_currInst; |
| 66 |
} |
| 67 |
Block &KernelIterator::currBlock() { |
| 68 |
IGA_ASSERT(m_currBlock, "KernelIterator at end"); |
| 69 |
return *m_currBlock; |
| 70 |
} |
| 71 |
|
| 72 |
// ii and ie are equal or invalid |
| 73 |
void KernelIterator::nextBlock() { |
| 74 |
while (bi != be) { // while -> skip empty blocks |
| 75 |
m_currBlock = *bi; |
| 76 |
ii = m_currBlock->getInstList().begin(); |
| 77 |
ie = m_currBlock->getInstList().end(); |
| 78 |
if (ii != ie) { |
| 79 |
m_currInst = *ii; |
| 80 |
return; |
| 81 |
} |
| 82 |
// next block |
| 83 |
bi++; |
| 84 |
} |
| 85 |
m_currInst = nullptr; |
| 86 |
m_currBlock = nullptr; |
| 87 |
return; |
| 88 |
} |
| 89 |
|
| 90 |
// walks to the next logical instruction |
| 91 |
// ii and ie must be valid |
| 92 |
void KernelIterator::nextInst() { |
| 93 |
IGA_ASSERT(bi != be, "KernelIterator at end"); |
| 94 |
if (ii != ie) { |
| 95 |
ii++; // step |
| 96 |
if (ii != ie) { |
| 97 |
m_currInst = *ii; |
| 98 |
return; // same block |
| 99 |
} // else { fallthrough and find next block } |
| 100 |
} |
| 101 |
nextBlock(); |
| 102 |
bi++; |
| 103 |
} |
| 104 |
|
| 105 |
// post-increment |
| 106 |
KernelIterator KernelIterator::operator++(int) { |
| 107 |
auto a = *this; |
| 108 |
IGA_ASSERT(bi != be, "KernelIterator at end"); |
| 109 |
nextInst(); |
| 110 |
return a; |
| 111 |
} |
| 112 |
|
| 113 |
KernelWalker::KernelWalker(BlockList &_blocks) |
| 114 |
: blocks(_blocks), end_itr(_blocks.end(), _blocks.end()) {} |