blob: 6c8e6d467c8e47c22d24dade3226f2114f4d131d [file] [log] [blame]
dan sinclair53f646c2022-11-30 16:36:46 +00001// Copyright 2022 The Tint Authors.
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 SRC_TINT_IR_BINARY_H_
16#define SRC_TINT_IR_BINARY_H_
17
James Priceae8a9f92023-06-06 19:37:51 +000018#include "src/tint/ir/operand_instruction.h"
dan sinclair12fa3032023-04-19 23:52:33 +000019#include "src/tint/utils/castable.h"
dan sinclair53f646c2022-11-30 16:36:46 +000020
21namespace tint::ir {
22
James Priceae8a9f92023-06-06 19:37:51 +000023/// A binary instruction in the IR.
24class Binary : public utils::Castable<Binary, OperandInstruction<2>> {
dan sinclair53f646c2022-11-30 16:36:46 +000025 public:
26 /// The kind of instruction.
27 enum class Kind {
28 kAdd,
29 kSubtract,
30 kMultiply,
31 kDivide,
32 kModulo,
33
34 kAnd,
35 kOr,
36 kXor,
37
dan sinclair53f646c2022-11-30 16:36:46 +000038 kEqual,
39 kNotEqual,
40 kLessThan,
41 kGreaterThan,
42 kLessThanEqual,
43 kGreaterThanEqual,
44
45 kShiftLeft,
46 kShiftRight
47 };
48
49 /// Constructor
50 /// @param kind the kind of binary instruction
dan sinclairf00679f2023-04-27 14:00:06 +000051 /// @param type the result type
dan sinclair53f646c2022-11-30 16:36:46 +000052 /// @param lhs the lhs of the instruction
53 /// @param rhs the rhs of the instruction
dan sinclair24cb8112023-05-18 22:16:08 +000054 Binary(enum Kind kind, const type::Type* type, Value* lhs, Value* rhs);
dan sinclair53f646c2022-11-30 16:36:46 +000055 ~Binary() override;
56
dan sinclair24cb8112023-05-18 22:16:08 +000057 /// @returns the kind of the binary instruction
dan sinclaira5268df2023-06-08 21:39:53 +000058 enum Kind Kind() { return kind_; }
dan sinclair24cb8112023-05-18 22:16:08 +000059
Ben Clayton9f83fa12023-05-10 17:56:14 +000060 /// @returns the type of the value
dan sinclaira5268df2023-06-08 21:39:53 +000061 const type::Type* Type() override { return result_type_; }
dan sinclair53f646c2022-11-30 16:36:46 +000062
dan sinclair53f646c2022-11-30 16:36:46 +000063 /// @returns the left-hand-side value for the instruction
dan sinclaira5268df2023-06-08 21:39:53 +000064 Value* LHS() { return operands_[0]; }
dan sinclair53f646c2022-11-30 16:36:46 +000065
66 /// @returns the right-hand-side value for the instruction
dan sinclaira5268df2023-06-08 21:39:53 +000067 Value* RHS() { return operands_[1]; }
dan sinclair53f646c2022-11-30 16:36:46 +000068
dan sinclair53f646c2022-11-30 16:36:46 +000069 private:
dan sinclair24cb8112023-05-18 22:16:08 +000070 enum Kind kind_;
dan sinclaireefa7fe2023-06-05 15:16:53 +000071 const type::Type* result_type_ = nullptr;
dan sinclair53f646c2022-11-30 16:36:46 +000072};
73
dan sinclair53f646c2022-11-30 16:36:46 +000074} // namespace tint::ir
75
76#endif // SRC_TINT_IR_BINARY_H_