1// Copyright (c) 2015-2016 The Khronos Group Inc.
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#ifndef SOURCE_OPCODE_H_
16#define SOURCE_OPCODE_H_
17
18#include "source/instruction.h"
19#include "source/latest_version_spirv_header.h"
20#include "source/table.h"
21#include "spirv-tools/libspirv.h"
22
23// Returns the name of a registered SPIR-V generator as a null-terminated
24// string. If the generator is not known, then returns the string "Unknown".
25// The generator parameter should be most significant 16-bits of the generator
26// word in the SPIR-V module header.
27//
28// See the registry at https://www.khronos.org/registry/spir-v/api/spir-v.xml.
29const char* spvGeneratorStr(uint32_t generator);
30
31// Combines word_count and opcode enumerant in single word.
32uint32_t spvOpcodeMake(uint16_t word_count, SpvOp opcode);
33
34// Splits word into into two constituent parts: word_count and opcode.
35void spvOpcodeSplit(const uint32_t word, uint16_t* word_count,
36 uint16_t* opcode);
37
38// Finds the named opcode in the given opcode table. On success, returns
39// SPV_SUCCESS and writes a handle of the table entry into *entry.
40spv_result_t spvOpcodeTableNameLookup(spv_target_env,
41 const spv_opcode_table table,
42 const char* name, spv_opcode_desc* entry);
43
44// Finds the opcode by enumerant in the given opcode table. On success, returns
45// SPV_SUCCESS and writes a handle of the table entry into *entry.
46spv_result_t spvOpcodeTableValueLookup(spv_target_env,
47 const spv_opcode_table table,
48 const SpvOp opcode,
49 spv_opcode_desc* entry);
50
51// Copies an instruction's word and fixes the endianness to host native. The
52// source instruction's stream/opcode/endianness is in the words/opcode/endian
53// parameter. The word_count parameter specifies the number of words to copy.
54// Writes copied instruction into *inst.
55void spvInstructionCopy(const uint32_t* words, const SpvOp opcode,
56 const uint16_t word_count,
57 const spv_endianness_t endian, spv_instruction_t* inst);
58
59// Determine if the given opcode is a scalar type. Returns zero if false,
60// non-zero otherwise.
61int32_t spvOpcodeIsScalarType(const SpvOp opcode);
62
63// Determines if the given opcode is a specialization constant. Returns zero if
64// false, non-zero otherwise.
65int32_t spvOpcodeIsSpecConstant(const SpvOp opcode);
66
67// Determines if the given opcode is a constant. Returns zero if false, non-zero
68// otherwise.
69int32_t spvOpcodeIsConstant(const SpvOp opcode);
70
71// Returns true if the given opcode is a constant or undef.
72bool spvOpcodeIsConstantOrUndef(const SpvOp opcode);
73
74// Returns true if the given opcode is a scalar specialization constant.
75bool spvOpcodeIsScalarSpecConstant(const SpvOp opcode);
76
77// Determines if the given opcode is a composite type. Returns zero if false,
78// non-zero otherwise.
79int32_t spvOpcodeIsComposite(const SpvOp opcode);
80
81// Determines if the given opcode results in a pointer when using the logical
82// addressing model. Returns zero if false, non-zero otherwise.
83int32_t spvOpcodeReturnsLogicalPointer(const SpvOp opcode);
84
85// Returns whether the given opcode could result in a pointer or a variable
86// pointer when using the logical addressing model.
87bool spvOpcodeReturnsLogicalVariablePointer(const SpvOp opcode);
88
89// Determines if the given opcode generates a type. Returns zero if false,
90// non-zero otherwise.
91int32_t spvOpcodeGeneratesType(SpvOp opcode);
92
93// Returns true if the opcode adds a decoration to an id.
94bool spvOpcodeIsDecoration(const SpvOp opcode);
95
96// Returns true if the opcode is a load from memory into a result id. This
97// function only considers core instructions.
98bool spvOpcodeIsLoad(const SpvOp opcode);
99
100// Returns true if the opcode is an atomic operation that uses the original
101// value.
102bool spvOpcodeIsAtomicWithLoad(const SpvOp opcode);
103
104// Returns true if the opcode is an atomic operation.
105bool spvOpcodeIsAtomicOp(const SpvOp opcode);
106
107// Returns true if the given opcode is a branch instruction.
108bool spvOpcodeIsBranch(SpvOp opcode);
109
110// Returns true if the given opcode is a return instruction.
111bool spvOpcodeIsReturn(SpvOp opcode);
112
113// Returns true if the given opcode is a return instruction or it aborts
114// execution.
115bool spvOpcodeIsReturnOrAbort(SpvOp opcode);
116
117// Returns true if the given opcode is a basic block terminator.
118bool spvOpcodeIsBlockTerminator(SpvOp opcode);
119
120// Returns true if the given opcode always defines an opaque type.
121bool spvOpcodeIsBaseOpaqueType(SpvOp opcode);
122
123// Returns true if the given opcode is a non-uniform group operation.
124bool spvOpcodeIsNonUniformGroupOperation(SpvOp opcode);
125
126// Returns true if the opcode with vector inputs could be divided into a series
127// of independent scalar operations that would give the same result.
128bool spvOpcodeIsScalarizable(SpvOp opcode);
129
130// Returns true if the given opcode is a debug instruction.
131bool spvOpcodeIsDebug(SpvOp opcode);
132
133// Returns true for opcodes that are binary operators,
134// where the order of the operands is irrelevant.
135bool spvOpcodeIsCommutativeBinaryOperator(SpvOp opcode);
136
137// Returns a vector containing the indices of the memory semantics <id>
138// operands for |opcode|.
139std::vector<uint32_t> spvOpcodeMemorySemanticsOperandIndices(SpvOp opcode);
140
141#endif // SOURCE_OPCODE_H_
142