blob: 27e610328f4e340ddcf83c4ab08c64054233823c [file] [log] [blame]
Artem Udovichenko4a0dad62016-01-26 12:28:31 +03001/*
2 * Copyright (C) 2015 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef ART_COMPILER_OPTIMIZING_NODES_SHARED_H_
18#define ART_COMPILER_OPTIMIZING_NODES_SHARED_H_
19
Alexandre Ramesebc32802016-09-19 13:56:18 +010020// This `#include` should never be used by compilation, as this file (`nodes_shared.h`) is included
21// in `nodes.h`. However it helps editing tools (e.g. YouCompleteMe) by giving them better context
22// (defining `HInstruction` and co).
23#include "nodes.h"
24
VladimĂ­r Marko434d9682022-11-04 14:04:17 +000025namespace art HIDDEN {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030026
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010027class HMultiplyAccumulate final : public HExpression<3> {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030028 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010029 HMultiplyAccumulate(DataType::Type type,
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030030 InstructionKind op,
31 HInstruction* accumulator,
32 HInstruction* mul_left,
33 HInstruction* mul_right,
34 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +053035 : HExpression(kMultiplyAccumulate, type, SideEffects::None(), dex_pc),
36 op_kind_(op) {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030037 SetRawInputAt(kInputAccumulatorIndex, accumulator);
38 SetRawInputAt(kInputMulLeftIndex, mul_left);
39 SetRawInputAt(kInputMulRightIndex, mul_right);
40 }
41
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010042 bool IsClonable() const override { return true; }
Artem Serovcced8ba2017-07-19 18:18:09 +010043
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030044 static constexpr int kInputAccumulatorIndex = 0;
45 static constexpr int kInputMulLeftIndex = 1;
46 static constexpr int kInputMulRightIndex = 2;
47
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010048 bool CanBeMoved() const override { return true; }
49 bool InstructionDataEquals(const HInstruction* other) const override {
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030050 return op_kind_ == other->AsMultiplyAccumulate()->op_kind_;
51 }
52
53 InstructionKind GetOpKind() const { return op_kind_; }
54
55 DECLARE_INSTRUCTION(MultiplyAccumulate);
56
Artem Serovcced8ba2017-07-19 18:18:09 +010057 protected:
58 DEFAULT_COPY_CONSTRUCTOR(MultiplyAccumulate);
59
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030060 private:
61 // Indicates if this is a MADD or MSUB.
62 const InstructionKind op_kind_;
Artem Udovichenko4a0dad62016-01-26 12:28:31 +030063};
64
Roland Levillainbbc6e7e2018-08-24 16:58:47 +010065class HBitwiseNegatedRight final : public HBinaryOperation {
Artem Serov7fc63502016-02-09 17:15:29 +000066 public:
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010067 HBitwiseNegatedRight(DataType::Type result_type,
68 InstructionKind op,
69 HInstruction* left,
70 HInstruction* right,
71 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +053072 : HBinaryOperation(kBitwiseNegatedRight,
73 result_type,
74 left,
75 right,
76 SideEffects::None(),
77 dex_pc),
Artem Serov7fc63502016-02-09 17:15:29 +000078 op_kind_(op) {
79 DCHECK(op == HInstruction::kAnd || op == HInstruction::kOr || op == HInstruction::kXor) << op;
80 }
81
82 template <typename T, typename U>
83 auto Compute(T x, U y) const -> decltype(x & ~y) {
84 static_assert(std::is_same<decltype(x & ~y), decltype(x | ~y)>::value &&
85 std::is_same<decltype(x & ~y), decltype(x ^ ~y)>::value,
86 "Inconsistent negated bitwise types");
87 switch (op_kind_) {
88 case HInstruction::kAnd:
89 return x & ~y;
90 case HInstruction::kOr:
91 return x | ~y;
92 case HInstruction::kXor:
93 return x ^ ~y;
94 default:
95 LOG(FATAL) << "Unreachable";
96 UNREACHABLE();
97 }
98 }
99
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100100 HConstant* Evaluate(HIntConstant* x, HIntConstant* y) const override {
Artem Serov7fc63502016-02-09 17:15:29 +0000101 return GetBlock()->GetGraph()->GetIntConstant(
102 Compute(x->GetValue(), y->GetValue()), GetDexPc());
103 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100104 HConstant* Evaluate(HLongConstant* x, HLongConstant* y) const override {
Artem Serov7fc63502016-02-09 17:15:29 +0000105 return GetBlock()->GetGraph()->GetLongConstant(
106 Compute(x->GetValue(), y->GetValue()), GetDexPc());
107 }
108 HConstant* Evaluate(HFloatConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100109 HFloatConstant* y ATTRIBUTE_UNUSED) const override {
Artem Serov7fc63502016-02-09 17:15:29 +0000110 LOG(FATAL) << DebugName() << " is not defined for float values";
111 UNREACHABLE();
112 }
113 HConstant* Evaluate(HDoubleConstant* x ATTRIBUTE_UNUSED,
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100114 HDoubleConstant* y ATTRIBUTE_UNUSED) const override {
Artem Serov7fc63502016-02-09 17:15:29 +0000115 LOG(FATAL) << DebugName() << " is not defined for double values";
116 UNREACHABLE();
117 }
118
119 InstructionKind GetOpKind() const { return op_kind_; }
120
121 DECLARE_INSTRUCTION(BitwiseNegatedRight);
122
Artem Serovcced8ba2017-07-19 18:18:09 +0100123 protected:
124 DEFAULT_COPY_CONSTRUCTOR(BitwiseNegatedRight);
125
Artem Serov7fc63502016-02-09 17:15:29 +0000126 private:
127 // Specifies the bitwise operation, which will be then negated.
128 const InstructionKind op_kind_;
Artem Serov7fc63502016-02-09 17:15:29 +0000129};
130
Artem Serove1811ed2017-04-27 16:50:47 +0100131// This instruction computes part of the array access offset (data and index offset).
132//
133// For array accesses the element address has the following structure:
134// Address = CONST_OFFSET + base_addr + index << ELEM_SHIFT. Taking into account LDR/STR addressing
135// modes address part (CONST_OFFSET + index << ELEM_SHIFT) can be shared across array access with
136// the same data type and index. For example, for the following loop 5 accesses can share address
137// computation:
138//
139// void foo(int[] a, int[] b, int[] c) {
140// for (i...) {
141// a[i] = a[i] + 5;
142// b[i] = b[i] + c[i];
143// }
144// }
145//
146// Note: as the instruction doesn't involve base array address into computations it has no side
147// effects (in comparison of HIntermediateAddress).
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100148class HIntermediateAddressIndex final : public HExpression<3> {
Artem Serove1811ed2017-04-27 16:50:47 +0100149 public:
150 HIntermediateAddressIndex(
151 HInstruction* index, HInstruction* offset, HInstruction* shift, uint32_t dex_pc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +0530152 : HExpression(kIntermediateAddressIndex,
153 DataType::Type::kInt32,
154 SideEffects::None(),
155 dex_pc) {
Artem Serove1811ed2017-04-27 16:50:47 +0100156 SetRawInputAt(0, index);
157 SetRawInputAt(1, offset);
158 SetRawInputAt(2, shift);
159 }
160
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100161 bool IsClonable() const override { return true; }
162 bool CanBeMoved() const override { return true; }
163 bool InstructionDataEquals(const HInstruction* other ATTRIBUTE_UNUSED) const override {
Artem Serove1811ed2017-04-27 16:50:47 +0100164 return true;
165 }
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100166 bool IsActualObject() const override { return false; }
Artem Serove1811ed2017-04-27 16:50:47 +0100167
168 HInstruction* GetIndex() const { return InputAt(0); }
169 HInstruction* GetOffset() const { return InputAt(1); }
170 HInstruction* GetShift() const { return InputAt(2); }
171
172 DECLARE_INSTRUCTION(IntermediateAddressIndex);
173
Artem Serovcced8ba2017-07-19 18:18:09 +0100174 protected:
175 DEFAULT_COPY_CONSTRUCTOR(IntermediateAddressIndex);
Artem Serove1811ed2017-04-27 16:50:47 +0100176};
177
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100178class HDataProcWithShifterOp final : public HExpression<2> {
Anton Kirilov74234da2017-01-13 14:42:47 +0000179 public:
180 enum OpKind {
181 kLSL, // Logical shift left.
182 kLSR, // Logical shift right.
183 kASR, // Arithmetic shift right.
184 kUXTB, // Unsigned extend byte.
185 kUXTH, // Unsigned extend half-word.
186 kUXTW, // Unsigned extend word.
187 kSXTB, // Signed extend byte.
188 kSXTH, // Signed extend half-word.
189 kSXTW, // Signed extend word.
190
191 // Aliases.
192 kFirstShiftOp = kLSL,
193 kLastShiftOp = kASR,
194 kFirstExtensionOp = kUXTB,
195 kLastExtensionOp = kSXTW
196 };
197 HDataProcWithShifterOp(HInstruction* instr,
198 HInstruction* left,
199 HInstruction* right,
200 OpKind op,
201 // The shift argument is unused if the operation
202 // is an extension.
203 int shift = 0,
204 uint32_t dex_pc = kNoDexPc)
Gupta Kumar, Sanjivd9e4d732018-02-05 13:35:03 +0530205 : HExpression(kDataProcWithShifterOp, instr->GetType(), SideEffects::None(), dex_pc),
Anton Kirilov74234da2017-01-13 14:42:47 +0000206 instr_kind_(instr->GetKind()), op_kind_(op),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100207 shift_amount_(shift & (instr->GetType() == DataType::Type::kInt32
Anton Kirilov74234da2017-01-13 14:42:47 +0000208 ? kMaxIntShiftDistance
209 : kMaxLongShiftDistance)) {
210 DCHECK(!instr->HasSideEffects());
211 SetRawInputAt(0, left);
212 SetRawInputAt(1, right);
213 }
214
Roland Levillainbbc6e7e2018-08-24 16:58:47 +0100215 bool IsClonable() const override { return true; }
216 bool CanBeMoved() const override { return true; }
217 bool InstructionDataEquals(const HInstruction* other_instr) const override {
Anton Kirilov74234da2017-01-13 14:42:47 +0000218 const HDataProcWithShifterOp* other = other_instr->AsDataProcWithShifterOp();
219 return instr_kind_ == other->instr_kind_ &&
220 op_kind_ == other->op_kind_ &&
221 shift_amount_ == other->shift_amount_;
222 }
223
224 static bool IsShiftOp(OpKind op_kind) {
225 return kFirstShiftOp <= op_kind && op_kind <= kLastShiftOp;
226 }
227
228 static bool IsExtensionOp(OpKind op_kind) {
229 return kFirstExtensionOp <= op_kind && op_kind <= kLastExtensionOp;
230 }
231
232 // Find the operation kind and shift amount from a bitfield move instruction.
233 static void GetOpInfoFromInstruction(HInstruction* bitfield_op,
234 /*out*/OpKind* op_kind,
235 /*out*/int* shift_amount);
236
237 InstructionKind GetInstrKind() const { return instr_kind_; }
238 OpKind GetOpKind() const { return op_kind_; }
239 int GetShiftAmount() const { return shift_amount_; }
240
241 DECLARE_INSTRUCTION(DataProcWithShifterOp);
242
Artem Serovcced8ba2017-07-19 18:18:09 +0100243 protected:
244 DEFAULT_COPY_CONSTRUCTOR(DataProcWithShifterOp);
245
Anton Kirilov74234da2017-01-13 14:42:47 +0000246 private:
247 InstructionKind instr_kind_;
248 OpKind op_kind_;
249 int shift_amount_;
250
251 friend std::ostream& operator<<(std::ostream& os, OpKind op);
Anton Kirilov74234da2017-01-13 14:42:47 +0000252};
253
254std::ostream& operator<<(std::ostream& os, const HDataProcWithShifterOp::OpKind op);
Artem Serov328429f2016-07-06 16:23:04 +0100255
Artem Udovichenko4a0dad62016-01-26 12:28:31 +0300256} // namespace art
257
258#endif // ART_COMPILER_OPTIMIZING_NODES_SHARED_H_