Home | History | Annotate | Download | only in libSPIRV
      1 //===- SPIRVOpCode.h - Class to represent SPIR-V Operation Codes -*- 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 Operation Code class for SPIR-V.
     37 ///
     38 //===----------------------------------------------------------------------===//
     39 
     40 #ifndef SPIRVOPCODE_HPP_
     41 #define SPIRVOPCODE_HPP_
     42 
     43 #include "SPIRVUtil.h"
     44 #include "spirv.hpp"
     45 #include <string>
     46 
     47 using namespace spv;
     48 namespace SPIRV{
     49 
     50 template<> inline void
     51 SPIRVMap<Op, std::string>::init() {
     52 #define _SPIRV_OP(x, ...) add(Op##x, #x);
     53 #include "SPIRVOpCodeEnum.h"
     54 #undef _SPIRV_OP
     55 }
     56 SPIRV_DEF_NAMEMAP(Op, OpCodeNameMap)
     57 
     58 inline bool isAtomicOpCode(Op OpCode) {
     59   assert(OpAtomicLoad < OpAtomicXor);
     60   return ((unsigned)OpCode >= OpAtomicLoad
     61       && (unsigned)OpCode <= OpAtomicXor)
     62       || OpCode == OpAtomicFlagTestAndSet
     63       || OpCode == OpAtomicFlagClear;
     64 }
     65 inline bool isBinaryOpCode(Op OpCode) {
     66   return ((unsigned)OpCode >= OpIAdd &&
     67       (unsigned)OpCode <= OpFMod) ||
     68       OpCode == OpDot;
     69 }
     70 
     71 inline bool isShiftOpCode(Op OpCode) {
     72   return (unsigned)OpCode >= OpShiftRightLogical &&
     73       (unsigned)OpCode <= OpShiftLeftLogical;
     74 }
     75 
     76 inline bool isLogicalOpCode(Op OpCode) {
     77   return (unsigned)OpCode >= OpLogicalEqual &&
     78       (unsigned)OpCode <= OpLogicalNot;
     79 }
     80 
     81 inline bool isBitwiseOpCode(Op OpCode) {
     82   return (unsigned)OpCode >= OpBitwiseOr &&
     83       (unsigned)OpCode <= OpBitwiseAnd;
     84 }
     85 
     86 inline bool isBinaryShiftLogicalBitwiseOpCode(Op OpCode) {
     87   return (((unsigned)OpCode >= OpShiftRightLogical &&
     88       (unsigned)OpCode <= OpBitwiseAnd) ||
     89       isBinaryOpCode(OpCode));
     90 }
     91 
     92 inline bool isCmpOpCode(Op OpCode) {
     93   return ((unsigned)OpCode >= OpIEqual &&
     94       (unsigned)OpCode <= OpFUnordGreaterThanEqual) ||
     95       (OpCode >= OpLessOrGreater && OpCode <= OpLogicalNotEqual);
     96 }
     97 
     98 inline bool isCvtOpCode(Op OpCode) {
     99   return ((unsigned)OpCode >= OpConvertFToU &&
    100       (unsigned)OpCode <= OpBitcast) ||
    101       OpCode == OpSatConvertSToU ||
    102       OpCode == OpSatConvertUToS;
    103 }
    104 
    105 inline bool isCvtToUnsignedOpCode(Op OpCode) {
    106   return OpCode == OpConvertFToU ||
    107       OpCode == OpUConvert ||
    108       OpCode == OpSatConvertSToU;
    109 }
    110 
    111 inline bool isCvtFromUnsignedOpCode(Op OpCode) {
    112   return OpCode == OpConvertUToF ||
    113       OpCode == OpUConvert ||
    114       OpCode == OpSatConvertUToS;
    115 }
    116 
    117 inline bool isOpaqueGenericTypeOpCode(Op OpCode) {
    118   return (unsigned)OpCode >= OpTypeEvent &&
    119       (unsigned)OpCode <= OpTypeQueue;
    120 }
    121 
    122 inline bool isGenericNegateOpCode(Op OpCode) {
    123   return (unsigned)OpCode == OpSNegate ||
    124       (unsigned)OpCode == OpFNegate ||
    125       (unsigned)OpCode == OpNot;
    126 }
    127 
    128 inline bool isAccessChainOpCode(Op OpCode) {
    129   return OpCode == OpAccessChain ||
    130       OpCode == OpInBoundsAccessChain;
    131 }
    132 
    133 inline bool hasExecScope(Op OpCode) {
    134   unsigned OC = OpCode;
    135   return (OpGroupWaitEvents <= OC &&
    136             OC <= OpGroupSMax) ||
    137       (OpGroupReserveReadPipePackets <= OC &&
    138           OC <= OpGroupCommitWritePipe);
    139 }
    140 
    141 inline bool hasGroupOperation(Op OpCode) {
    142   unsigned OC = OpCode;
    143   return OpGroupIAdd <= OC && OC <= OpGroupSMax;
    144 }
    145 
    146 inline bool isGroupOpCode(Op OpCode) {
    147   unsigned OC = OpCode;
    148   return OpGroupAll <= OC && OC <= OpGroupSMax;
    149 }
    150 
    151 inline bool isPipeOpCode(Op OpCode) {
    152   unsigned OC = OpCode;
    153   return OpReadPipe <= OC && OC <= OpGroupCommitWritePipe;
    154 }
    155 inline bool isTypeOpCode(Op OpCode) {
    156   unsigned OC = OpCode;
    157   return (OpTypeVoid <= OC && OC <= OpTypePipe) || OC == OpTypePipeStorage;
    158 }
    159 
    160 inline bool isConstantOpCode(Op OpCode) {
    161   unsigned OC = OpCode;
    162   return (OpConstantTrue <= OC
    163       && OC <= OpSpecConstantOp)
    164       || OC == OpUndef || OC == OpConstantPipeStorage;
    165 }
    166 
    167 inline bool isModuleScopeAllowedOpCode(Op OpCode) {
    168   return OpCode == OpVariable ||
    169       isConstantOpCode(OpCode);
    170 }
    171 
    172 }
    173 
    174 #endif /* SPIRVOPCODE_HPP_ */
    175