| 1 |
//===- SPIRVBasicBlock.cpp - SPIR-V Basic Block -----------------*- C++ -*-===// |
| 2 |
// |
| 3 |
// The LLVM/SPIRV Translator |
| 4 |
// |
| 5 |
// This file is distributed under the University of Illinois Open Source |
| 6 |
// License. See LICENSE.TXT for details. |
| 7 |
// |
| 8 |
// Copyright (c) 2014 Advanced Micro Devices, Inc. All rights reserved. |
| 9 |
// |
| 10 |
// Permission is hereby granted, free of charge, to any person obtaining a |
| 11 |
// copy of this software and associated documentation files (the "Software"), |
| 12 |
// to deal with the Software without restriction, including without limitation |
| 13 |
// the rights to use, copy, modify, merge, publish, distribute, sublicense, |
| 14 |
// and/or sell copies of the Software, and to permit persons to whom the |
| 15 |
// Software is furnished to do so, subject to the following conditions: |
| 16 |
// |
| 17 |
// Redistributions of source code must retain the above copyright notice, |
| 18 |
// this list of conditions and the following disclaimers. |
| 19 |
// Redistributions in binary form must reproduce the above copyright notice, |
| 20 |
// this list of conditions and the following disclaimers in the documentation |
| 21 |
// and/or other materials provided with the distribution. |
| 22 |
// Neither the names of Advanced Micro Devices, Inc., nor the names of its |
| 23 |
// contributors may be used to endorse or promote products derived from this |
| 24 |
// Software without specific prior written permission. |
| 25 |
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR |
| 26 |
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, |
| 27 |
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE |
| 28 |
// CONTRIBUTORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER |
| 29 |
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
| 30 |
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS WITH |
| 31 |
// THE SOFTWARE. |
| 32 |
// |
| 33 |
//===----------------------------------------------------------------------===// |
| 34 |
/// \file |
| 35 |
/// |
| 36 |
/// This file implements SPIRV basic block. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#include "SPIRVBasicBlock.h" |
| 41 |
#include "SPIRVEntry.h" |
| 42 |
#include "SPIRVFunction.h" |
| 43 |
#include "SPIRVInstruction.h" |
| 44 |
#include "SPIRVStream.h" |
| 45 |
#include "SPIRVValue.h" |
| 46 |
|
| 47 |
#include <iostream> |
| 48 |
|
| 49 |
using namespace SPIRV; |
| 50 |
|
| 51 |
SPIRVBasicBlock::SPIRVBasicBlock(SPIRVId TheId, SPIRVFunction *Func) |
| 52 |
: SPIRVValue(Func->getModule(), 2, OpLabel, TheId), ParentF(Func) { |
| 53 |
setAttr(); |
| 54 |
validate(); |
| 55 |
} |
| 56 |
|
| 57 |
SPIRVDecoder SPIRVBasicBlock::getDecoder(std::istream &IS) { |
| 58 |
return SPIRVDecoder(IS, *this); |
| 59 |
} |
| 60 |
|
| 61 |
/// Assume I contains valid Id. |
| 62 |
SPIRVInstruction * |
| 63 |
SPIRVBasicBlock::addInstruction(SPIRVInstruction *I, |
| 64 |
const SPIRVInstruction *InsertBefore) { |
| 65 |
assert(I && "Invalid instruction"); |
| 66 |
Module->add(I); |
| 67 |
I->setParent(this); |
| 68 |
if (InsertBefore) { |
| 69 |
auto Pos = find(InsertBefore); |
| 70 |
// If insertion of a new instruction before the one passed to the function |
| 71 |
// is illegal, insertion before the returned instruction is guaranteed |
| 72 |
// to retain correct instruction order in a block |
| 73 |
if (Pos != InstVec.begin() && (isa<OpLoopMerge>(*std::prev(Pos)) || |
| 74 |
isa<OpLoopControlINTEL>(*std::prev(Pos)))) |
| 75 |
--Pos; |
| 76 |
InstVec.insert(Pos, I); |
| 77 |
} else |
| 78 |
InstVec.push_back(I); |
| 79 |
return I; |
| 80 |
} |
| 81 |
|
| 82 |
void SPIRVBasicBlock::encodeChildren(spv_ostream &O) const { |
| 83 |
O << SPIRVNL(); |
| 84 |
for (size_t I = 0, E = InstVec.size(); I != E; ++I) |
| 85 |
O << *InstVec[I]; |
| 86 |
} |
| 87 |
|
| 88 |
_SPIRV_IMP_ENCDEC1(SPIRVBasicBlock, Id) |
| 89 |
|
| 90 |
void SPIRVBasicBlock::setScope(SPIRVEntry *Scope) { |
| 91 |
assert(Scope && Scope->getOpCode() == OpFunction && "Invalid scope"); |
| 92 |
setParent(static_cast<SPIRVFunction *>(Scope)); |
| 93 |
} |