| 1 |
//===- SPIRVBasicBlock.h - 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 defines Basic Block class for SPIR-V. |
| 37 |
/// |
| 38 |
//===----------------------------------------------------------------------===// |
| 39 |
|
| 40 |
#ifndef SPIRV_LIBSPIRV_SPIRVBASICBLOCK_H |
| 41 |
#define SPIRV_LIBSPIRV_SPIRVBASICBLOCK_H |
| 42 |
|
| 43 |
#include "SPIRVValue.h" |
| 44 |
#include <algorithm> |
| 45 |
|
| 46 |
namespace SPIRV { |
| 47 |
class SPIRVFunction; |
| 48 |
class SPIRVInstruction; |
| 49 |
class SPIRVDecoder; |
| 50 |
class SPIRVBasicBlock : public SPIRVValue { |
| 51 |
|
| 52 |
public: |
| 53 |
SPIRVBasicBlock(SPIRVId TheId, SPIRVFunction *Func); |
| 54 |
|
| 55 |
SPIRVBasicBlock() : SPIRVValue(OpLabel), ParentF(NULL) { setAttr(); } |
| 56 |
|
| 57 |
SPIRVDecoder getDecoder(std::istream &IS) override; |
| 58 |
SPIRVFunction *getParent() const { return ParentF; } |
| 59 |
size_t getNumInst() const { return InstVec.size(); } |
| 60 |
SPIRVInstruction *getInst(size_t I) const { return InstVec[I]; } |
| 61 |
SPIRVInstruction *getPrevious(const SPIRVInstruction *I) const { |
| 62 |
auto Loc = find(I); |
| 63 |
if (Loc == InstVec.end() || Loc == InstVec.begin()) |
| 64 |
return nullptr; |
| 65 |
return *(--Loc); |
| 66 |
} |
| 67 |
SPIRVInstruction *getNext(const SPIRVInstruction *I) const { |
| 68 |
auto Loc = find(I); |
| 69 |
if (Loc == InstVec.end()) |
| 70 |
return nullptr; |
| 71 |
++Loc; |
| 72 |
if (Loc == InstVec.end()) |
| 73 |
return nullptr; |
| 74 |
return *Loc; |
| 75 |
} |
| 76 |
// Return the last instruction in the BB or nullptr if the BB is empty. |
| 77 |
const SPIRVInstruction *getTerminateInstr() const { |
| 78 |
return InstVec.empty() ? nullptr : InstVec.back(); |
| 79 |
} |
| 80 |
|
| 81 |
void setScope(SPIRVEntry *Scope) override; |
| 82 |
void setParent(SPIRVFunction *F) { ParentF = F; } |
| 83 |
SPIRVInstruction * |
| 84 |
addInstruction(SPIRVInstruction *I, |
| 85 |
const SPIRVInstruction *InsertBefore = nullptr); |
| 86 |
void eraseInstruction(const SPIRVInstruction *I) { |
| 87 |
auto Loc = find(I); |
| 88 |
assert(Loc != InstVec.end()); |
| 89 |
InstVec.erase(Loc); |
| 90 |
} |
| 91 |
|
| 92 |
void setAttr() { setHasNoType(); } |
| 93 |
_SPIRV_DCL_ENCDEC |
| 94 |
void encodeChildren(spv_ostream &) const override; |
| 95 |
void validate() const override { |
| 96 |
SPIRVValue::validate(); |
| 97 |
assert(ParentF && "Invalid parent function"); |
| 98 |
} |
| 99 |
|
| 100 |
private: |
| 101 |
SPIRVFunction *ParentF; |
| 102 |
typedef std::vector<SPIRVInstruction *> SPIRVInstructionVector; |
| 103 |
SPIRVInstructionVector InstVec; |
| 104 |
|
| 105 |
SPIRVInstructionVector::const_iterator |
| 106 |
find(const SPIRVInstruction *Inst) const { |
| 107 |
return std::find(InstVec.begin(), InstVec.end(), Inst); |
| 108 |
} |
| 109 |
|
| 110 |
SPIRVInstructionVector::iterator find(const SPIRVInstruction *Inst) { |
| 111 |
return std::find(InstVec.begin(), InstVec.end(), Inst); |
| 112 |
} |
| 113 |
}; |
| 114 |
|
| 115 |
typedef SPIRVBasicBlock SPIRVLabel; |
| 116 |
} // namespace SPIRV |
| 117 |
|
| 118 |
#endif // SPIRV_LIBSPIRV_SPIRVBASICBLOCK_H |