blob: 8f3ab11d00fb3bd39053fbdece2418072fc793c1 [file] [log] [blame]
Aart Bikf8f5a162017-02-06 15:35:29 -08001/*
2 * Copyright (C) 2017 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_VECTOR_H_
18#define ART_COMPILER_OPTIMIZING_NODES_VECTOR_H_
19
20// This #include should never be used by compilation, because this header file (nodes_vector.h)
21// is included in the header file nodes.h itself. However it gives editing tools better context.
22#include "nodes.h"
23
24namespace art {
25
26// Memory alignment, represented as an offset relative to a base, where 0 <= offset < base,
27// and base is a power of two. For example, the value Alignment(16, 0) means memory is
28// perfectly aligned at a 16-byte boundary, whereas the value Alignment(16, 4) means
29// memory is always exactly 4 bytes above such a boundary.
30class Alignment {
31 public:
32 Alignment(size_t base, size_t offset) : base_(base), offset_(offset) {
33 DCHECK_LT(offset, base);
34 DCHECK(IsPowerOfTwo(base));
35 }
36
Aart Bik46b6dbc2017-10-03 11:37:37 -070037 // Returns true if memory is at least aligned at the given boundary.
Aart Bikf8f5a162017-02-06 15:35:29 -080038 // Assumes requested base is power of two.
39 bool IsAlignedAt(size_t base) const {
40 DCHECK_NE(0u, base);
41 DCHECK(IsPowerOfTwo(base));
42 return ((offset_ | base_) & (base - 1u)) == 0;
43 }
44
Aart Bik46b6dbc2017-10-03 11:37:37 -070045 size_t Base() const { return base_; }
46
47 size_t Offset() const { return offset_; }
48
Aart Bikf8f5a162017-02-06 15:35:29 -080049 std::string ToString() const {
50 return "ALIGN(" + std::to_string(base_) + "," + std::to_string(offset_) + ")";
51 }
52
Aart Bikb79f4ac2017-07-10 10:10:37 -070053 bool operator==(const Alignment& other) const {
54 return base_ == other.base_ && offset_ == other.offset_;
55 }
56
Aart Bikf8f5a162017-02-06 15:35:29 -080057 private:
58 size_t base_;
59 size_t offset_;
60};
61
62//
63// Definitions of abstract vector operations in HIR.
64//
65
66// Abstraction of a vector operation, i.e., an operation that performs
67// GetVectorLength() x GetPackedType() operations simultaneously.
68class HVecOperation : public HVariableInputSizeInstruction {
69 public:
Aart Bik0148de42017-09-05 09:25:01 -070070 // A SIMD operation looks like a FPU location.
71 // TODO: we could introduce SIMD types in HIR.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010072 static constexpr DataType::Type kSIMDType = DataType::Type::kFloat64;
Aart Bik0148de42017-09-05 09:25:01 -070073
Aart Bikf8f5a162017-02-06 15:35:29 -080074 HVecOperation(ArenaAllocator* arena,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010075 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -080076 SideEffects side_effects,
77 size_t number_of_inputs,
78 size_t vector_length,
79 uint32_t dex_pc)
80 : HVariableInputSizeInstruction(side_effects,
81 dex_pc,
82 arena,
83 number_of_inputs,
84 kArenaAllocVectorNode),
85 vector_length_(vector_length) {
86 SetPackedField<TypeField>(packed_type);
87 DCHECK_LT(1u, vector_length);
88 }
89
90 // Returns the number of elements packed in a vector.
91 size_t GetVectorLength() const {
92 return vector_length_;
93 }
94
95 // Returns the number of bytes in a full vector.
96 size_t GetVectorNumberOfBytes() const {
Vladimir Marko0ebe0d82017-09-21 22:50:39 +010097 return vector_length_ * DataType::Size(GetPackedType());
Aart Bikf8f5a162017-02-06 15:35:29 -080098 }
99
Aart Bik0148de42017-09-05 09:25:01 -0700100 // Returns the type of the vector operation.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100101 DataType::Type GetType() const OVERRIDE {
Aart Bik0148de42017-09-05 09:25:01 -0700102 return kSIMDType;
Aart Bikf8f5a162017-02-06 15:35:29 -0800103 }
104
105 // Returns the true component type packed in a vector.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100106 DataType::Type GetPackedType() const {
Aart Bikf8f5a162017-02-06 15:35:29 -0800107 return GetPackedField<TypeField>();
108 }
109
Aart Bikb79f4ac2017-07-10 10:10:37 -0700110 // Assumes vector nodes cannot be moved by default. Each concrete implementation
111 // that can be moved should override this method and return true.
112 bool CanBeMoved() const OVERRIDE { return false; }
113
114 // Tests if all data of a vector node (vector length and packed type) is equal.
115 // Each concrete implementation that adds more fields should test equality of
116 // those fields in its own method *and* call all super methods.
117 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
118 DCHECK(other->IsVecOperation());
119 const HVecOperation* o = other->AsVecOperation();
120 return GetVectorLength() == o->GetVectorLength() && GetPackedType() == o->GetPackedType();
121 }
122
Aart Bik46b6dbc2017-10-03 11:37:37 -0700123 // Maps an integral type to the same-size signed type and leaves other types alone.
124 // Can be used to test relaxed type consistency in which packed same-size integral
125 // types can co-exist, but other type mixes are an error.
126 static DataType::Type ToSignedType(DataType::Type type) {
127 switch (type) {
128 case DataType::Type::kBool: // 1-byte storage unit
129 case DataType::Type::kUint8:
130 return DataType::Type::kInt8;
131 case DataType::Type::kUint16:
132 return DataType::Type::kInt16;
133 default:
134 DCHECK(type != DataType::Type::kVoid && type != DataType::Type::kReference) << type;
135 return type;
136 }
137 }
138
Aart Bikf8f5a162017-02-06 15:35:29 -0800139 DECLARE_ABSTRACT_INSTRUCTION(VecOperation);
140
Aart Bikdb14fcf2017-04-25 15:53:58 -0700141 protected:
Aart Bikf8f5a162017-02-06 15:35:29 -0800142 // Additional packed bits.
143 static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
144 static constexpr size_t kFieldTypeSize =
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100145 MinimumBitsToStore(static_cast<size_t>(DataType::Type::kLast));
Aart Bikf8f5a162017-02-06 15:35:29 -0800146 static constexpr size_t kNumberOfVectorOpPackedBits = kFieldType + kFieldTypeSize;
147 static_assert(kNumberOfVectorOpPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100148 using TypeField = BitField<DataType::Type, kFieldType, kFieldTypeSize>;
Aart Bikf8f5a162017-02-06 15:35:29 -0800149
Aart Bikdb14fcf2017-04-25 15:53:58 -0700150 private:
Aart Bikf8f5a162017-02-06 15:35:29 -0800151 const size_t vector_length_;
152
153 DISALLOW_COPY_AND_ASSIGN(HVecOperation);
154};
155
156// Abstraction of a unary vector operation.
157class HVecUnaryOperation : public HVecOperation {
158 public:
159 HVecUnaryOperation(ArenaAllocator* arena,
Aart Bik8de59162017-04-21 09:42:01 -0700160 HInstruction* input,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100161 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800162 size_t vector_length,
163 uint32_t dex_pc)
164 : HVecOperation(arena,
165 packed_type,
166 SideEffects::None(),
Aart Bik8de59162017-04-21 09:42:01 -0700167 /* number_of_inputs */ 1,
Aart Bikf8f5a162017-02-06 15:35:29 -0800168 vector_length,
Aart Bik8de59162017-04-21 09:42:01 -0700169 dex_pc) {
170 SetRawInputAt(0, input);
171 }
172
173 HInstruction* GetInput() const { return InputAt(0); }
174
Aart Bikf8f5a162017-02-06 15:35:29 -0800175 DECLARE_ABSTRACT_INSTRUCTION(VecUnaryOperation);
Aart Bik8de59162017-04-21 09:42:01 -0700176
Aart Bikf8f5a162017-02-06 15:35:29 -0800177 private:
178 DISALLOW_COPY_AND_ASSIGN(HVecUnaryOperation);
179};
180
181// Abstraction of a binary vector operation.
182class HVecBinaryOperation : public HVecOperation {
183 public:
184 HVecBinaryOperation(ArenaAllocator* arena,
Aart Bik8de59162017-04-21 09:42:01 -0700185 HInstruction* left,
186 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100187 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800188 size_t vector_length,
189 uint32_t dex_pc)
190 : HVecOperation(arena,
191 packed_type,
192 SideEffects::None(),
Aart Bik8de59162017-04-21 09:42:01 -0700193 /* number_of_inputs */ 2,
Aart Bikf8f5a162017-02-06 15:35:29 -0800194 vector_length,
Aart Bik8de59162017-04-21 09:42:01 -0700195 dex_pc) {
196 SetRawInputAt(0, left);
197 SetRawInputAt(1, right);
198 }
Artem Serovf34dd202017-04-10 17:41:46 +0100199
200 HInstruction* GetLeft() const { return InputAt(0); }
201 HInstruction* GetRight() const { return InputAt(1); }
202
Aart Bikf8f5a162017-02-06 15:35:29 -0800203 DECLARE_ABSTRACT_INSTRUCTION(VecBinaryOperation);
Aart Bik8de59162017-04-21 09:42:01 -0700204
Aart Bikf8f5a162017-02-06 15:35:29 -0800205 private:
206 DISALLOW_COPY_AND_ASSIGN(HVecBinaryOperation);
207};
208
209// Abstraction of a vector operation that references memory, with an alignment.
Aart Bik46b6dbc2017-10-03 11:37:37 -0700210// The Android runtime guarantees elements have at least natural alignment.
Aart Bikf8f5a162017-02-06 15:35:29 -0800211class HVecMemoryOperation : public HVecOperation {
212 public:
213 HVecMemoryOperation(ArenaAllocator* arena,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100214 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800215 SideEffects side_effects,
216 size_t number_of_inputs,
217 size_t vector_length,
218 uint32_t dex_pc)
219 : HVecOperation(arena, packed_type, side_effects, number_of_inputs, vector_length, dex_pc),
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100220 alignment_(DataType::Size(packed_type), 0) {
Artem Serove1811ed2017-04-27 16:50:47 +0100221 DCHECK_GE(number_of_inputs, 2u);
222 }
Aart Bikf8f5a162017-02-06 15:35:29 -0800223
224 void SetAlignment(Alignment alignment) { alignment_ = alignment; }
225
226 Alignment GetAlignment() const { return alignment_; }
227
Artem Serove1811ed2017-04-27 16:50:47 +0100228 HInstruction* GetArray() const { return InputAt(0); }
229 HInstruction* GetIndex() const { return InputAt(1); }
230
Aart Bikb79f4ac2017-07-10 10:10:37 -0700231 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
232 DCHECK(other->IsVecMemoryOperation());
233 const HVecMemoryOperation* o = other->AsVecMemoryOperation();
234 return HVecOperation::InstructionDataEquals(o) && GetAlignment() == o->GetAlignment();
235 }
236
Aart Bikf8f5a162017-02-06 15:35:29 -0800237 DECLARE_ABSTRACT_INSTRUCTION(VecMemoryOperation);
238
239 private:
240 Alignment alignment_;
241
242 DISALLOW_COPY_AND_ASSIGN(HVecMemoryOperation);
243};
244
Aart Bik0148de42017-09-05 09:25:01 -0700245// Packed type consistency checker ("same vector length" integral types may mix freely).
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100246inline static bool HasConsistentPackedTypes(HInstruction* input, DataType::Type type) {
Aart Bik0148de42017-09-05 09:25:01 -0700247 if (input->IsPhi()) {
248 return input->GetType() == HVecOperation::kSIMDType; // carries SIMD
249 }
Aart Bikd58bc322017-05-01 14:49:18 -0700250 DCHECK(input->IsVecOperation());
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100251 DataType::Type input_type = input->AsVecOperation()->GetPackedType();
Aart Bik46b6dbc2017-10-03 11:37:37 -0700252 return HVecOperation::ToSignedType(input_type) == HVecOperation::ToSignedType(type);
Aart Bikd58bc322017-05-01 14:49:18 -0700253}
254
Aart Bikf8f5a162017-02-06 15:35:29 -0800255//
Aart Bik8de59162017-04-21 09:42:01 -0700256// Definitions of concrete unary vector operations in HIR.
Aart Bikf8f5a162017-02-06 15:35:29 -0800257//
258
259// Replicates the given scalar into a vector,
260// viz. replicate(x) = [ x, .. , x ].
261class HVecReplicateScalar FINAL : public HVecUnaryOperation {
262 public:
263 HVecReplicateScalar(ArenaAllocator* arena,
264 HInstruction* scalar,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100265 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800266 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700267 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700268 : HVecUnaryOperation(arena, scalar, packed_type, vector_length, dex_pc) {
269 DCHECK(!scalar->IsVecOperation());
Aart Bikf8f5a162017-02-06 15:35:29 -0800270 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700271
272 // A replicate needs to stay in place, since SIMD registers are not
273 // kept alive across vector loop boundaries (yet).
274 bool CanBeMoved() const OVERRIDE { return false; }
275
Aart Bikf8f5a162017-02-06 15:35:29 -0800276 DECLARE_INSTRUCTION(VecReplicateScalar);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700277
Aart Bikf8f5a162017-02-06 15:35:29 -0800278 private:
279 DISALLOW_COPY_AND_ASSIGN(HVecReplicateScalar);
280};
281
Aart Bik0148de42017-09-05 09:25:01 -0700282// Extracts a particular scalar from the given vector,
283// viz. extract[ x1, .. , xn ] = x_i.
284//
285// TODO: for now only i == 1 case supported.
286class HVecExtractScalar FINAL : public HVecUnaryOperation {
287 public:
288 HVecExtractScalar(ArenaAllocator* arena,
289 HInstruction* input,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100290 DataType::Type packed_type,
Aart Bik0148de42017-09-05 09:25:01 -0700291 size_t vector_length,
292 size_t index,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700293 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700294 : HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700295 DCHECK(HasConsistentPackedTypes(input, packed_type));
Aart Bik0148de42017-09-05 09:25:01 -0700296 DCHECK_LT(index, vector_length);
297 DCHECK_EQ(index, 0u);
298 }
299
300 // Yields a single component in the vector.
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100301 DataType::Type GetType() const OVERRIDE {
Aart Bik0148de42017-09-05 09:25:01 -0700302 return GetPackedType();
303 }
304
305 // An extract needs to stay in place, since SIMD registers are not
306 // kept alive across vector loop boundaries (yet).
307 bool CanBeMoved() const OVERRIDE { return false; }
308
309 DECLARE_INSTRUCTION(VecExtractScalar);
310
311 private:
312 DISALLOW_COPY_AND_ASSIGN(HVecExtractScalar);
313};
314
315// Reduces the given vector into the first element as sum/min/max,
316// viz. sum-reduce[ x1, .. , xn ] = [ y, ---- ], where y = sum xi
317// and the "-" denotes "don't care" (implementation dependent).
318class HVecReduce FINAL : public HVecUnaryOperation {
319 public:
320 enum ReductionKind {
321 kSum = 1,
322 kMin = 2,
323 kMax = 3
324 };
325
326 HVecReduce(ArenaAllocator* arena,
327 HInstruction* input,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100328 DataType::Type packed_type,
Aart Bik0148de42017-09-05 09:25:01 -0700329 size_t vector_length,
330 ReductionKind kind,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700331 uint32_t dex_pc)
Aart Bik0148de42017-09-05 09:25:01 -0700332 : HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc),
333 kind_(kind) {
334 DCHECK(HasConsistentPackedTypes(input, packed_type));
Aart Bikcfa59b42017-08-31 09:08:13 -0700335 }
336
Aart Bik0148de42017-09-05 09:25:01 -0700337 ReductionKind GetKind() const { return kind_; }
Aart Bikf8f5a162017-02-06 15:35:29 -0800338
Aart Bikb79f4ac2017-07-10 10:10:37 -0700339 bool CanBeMoved() const OVERRIDE { return true; }
340
Aart Bik0148de42017-09-05 09:25:01 -0700341 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
342 DCHECK(other->IsVecReduce());
343 const HVecReduce* o = other->AsVecReduce();
344 return HVecOperation::InstructionDataEquals(o) && GetKind() == o->GetKind();
345 }
346
347 DECLARE_INSTRUCTION(VecReduce);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700348
Aart Bikf8f5a162017-02-06 15:35:29 -0800349 private:
Aart Bik0148de42017-09-05 09:25:01 -0700350 const ReductionKind kind_;
351
352 DISALLOW_COPY_AND_ASSIGN(HVecReduce);
Aart Bikf8f5a162017-02-06 15:35:29 -0800353};
354
355// Converts every component in the vector,
356// viz. cnv[ x1, .. , xn ] = [ cnv(x1), .. , cnv(xn) ].
357class HVecCnv FINAL : public HVecUnaryOperation {
358 public:
359 HVecCnv(ArenaAllocator* arena,
360 HInstruction* input,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100361 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800362 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700363 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700364 : HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
Aart Bikf8f5a162017-02-06 15:35:29 -0800365 DCHECK(input->IsVecOperation());
Aart Bikd58bc322017-05-01 14:49:18 -0700366 DCHECK_NE(GetInputType(), GetResultType()); // actual convert
Aart Bikf8f5a162017-02-06 15:35:29 -0800367 }
368
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100369 DataType::Type GetInputType() const { return InputAt(0)->AsVecOperation()->GetPackedType(); }
370 DataType::Type GetResultType() const { return GetPackedType(); }
Aart Bikf8f5a162017-02-06 15:35:29 -0800371
Aart Bikb79f4ac2017-07-10 10:10:37 -0700372 bool CanBeMoved() const OVERRIDE { return true; }
373
Aart Bikf8f5a162017-02-06 15:35:29 -0800374 DECLARE_INSTRUCTION(VecCnv);
375
376 private:
377 DISALLOW_COPY_AND_ASSIGN(HVecCnv);
378};
379
380// Negates every component in the vector,
381// viz. neg[ x1, .. , xn ] = [ -x1, .. , -xn ].
382class HVecNeg FINAL : public HVecUnaryOperation {
383 public:
384 HVecNeg(ArenaAllocator* arena,
385 HInstruction* input,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100386 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800387 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700388 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700389 : HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700390 DCHECK(HasConsistentPackedTypes(input, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800391 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700392
393 bool CanBeMoved() const OVERRIDE { return true; }
394
Aart Bikf8f5a162017-02-06 15:35:29 -0800395 DECLARE_INSTRUCTION(VecNeg);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700396
Aart Bikf8f5a162017-02-06 15:35:29 -0800397 private:
398 DISALLOW_COPY_AND_ASSIGN(HVecNeg);
399};
400
Aart Bik6daebeb2017-04-03 14:35:41 -0700401// Takes absolute value of every component in the vector,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700402// viz. abs[ x1, .. , xn ] = [ |x1|, .. , |xn| ]
403// for signed operand x.
Aart Bik6daebeb2017-04-03 14:35:41 -0700404class HVecAbs FINAL : public HVecUnaryOperation {
405 public:
406 HVecAbs(ArenaAllocator* arena,
407 HInstruction* input,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100408 DataType::Type packed_type,
Aart Bik6daebeb2017-04-03 14:35:41 -0700409 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700410 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700411 : HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700412 DCHECK(HasConsistentPackedTypes(input, packed_type));
Aart Bik6daebeb2017-04-03 14:35:41 -0700413 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700414
415 bool CanBeMoved() const OVERRIDE { return true; }
416
Aart Bik6daebeb2017-04-03 14:35:41 -0700417 DECLARE_INSTRUCTION(VecAbs);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700418
Aart Bik6daebeb2017-04-03 14:35:41 -0700419 private:
420 DISALLOW_COPY_AND_ASSIGN(HVecAbs);
421};
422
Aart Bikf8f5a162017-02-06 15:35:29 -0800423// Bitwise- or boolean-nots every component in the vector,
424// viz. not[ x1, .. , xn ] = [ ~x1, .. , ~xn ], or
425// not[ x1, .. , xn ] = [ !x1, .. , !xn ] for boolean.
426class HVecNot FINAL : public HVecUnaryOperation {
427 public:
428 HVecNot(ArenaAllocator* arena,
429 HInstruction* input,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100430 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800431 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700432 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700433 : HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
Aart Bikf8f5a162017-02-06 15:35:29 -0800434 DCHECK(input->IsVecOperation());
Aart Bikf8f5a162017-02-06 15:35:29 -0800435 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700436
437 bool CanBeMoved() const OVERRIDE { return true; }
438
Aart Bikf8f5a162017-02-06 15:35:29 -0800439 DECLARE_INSTRUCTION(VecNot);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700440
Aart Bikf8f5a162017-02-06 15:35:29 -0800441 private:
442 DISALLOW_COPY_AND_ASSIGN(HVecNot);
443};
444
Aart Bik8de59162017-04-21 09:42:01 -0700445//
446// Definitions of concrete binary vector operations in HIR.
447//
448
Aart Bikf8f5a162017-02-06 15:35:29 -0800449// Adds every component in the two vectors,
450// viz. [ x1, .. , xn ] + [ y1, .. , yn ] = [ x1 + y1, .. , xn + yn ].
451class HVecAdd FINAL : public HVecBinaryOperation {
452 public:
453 HVecAdd(ArenaAllocator* arena,
454 HInstruction* left,
455 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100456 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800457 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700458 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700459 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700460 DCHECK(HasConsistentPackedTypes(left, packed_type));
461 DCHECK(HasConsistentPackedTypes(right, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800462 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700463
464 bool CanBeMoved() const OVERRIDE { return true; }
465
Aart Bikf8f5a162017-02-06 15:35:29 -0800466 DECLARE_INSTRUCTION(VecAdd);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700467
Aart Bikf8f5a162017-02-06 15:35:29 -0800468 private:
469 DISALLOW_COPY_AND_ASSIGN(HVecAdd);
470};
471
Aart Bikf3e61ee2017-04-12 17:09:20 -0700472// Performs halving add on every component in the two vectors, viz.
Aart Bikdbbac8f2017-09-01 13:06:08 -0700473// rounded [ x1, .. , xn ] hradd [ y1, .. , yn ] = [ (x1 + y1 + 1) >> 1, .. , (xn + yn + 1) >> 1 ]
474// truncated [ x1, .. , xn ] hadd [ y1, .. , yn ] = [ (x1 + y1) >> 1, .. , (xn + yn ) >> 1 ]
Aart Bik46b6dbc2017-10-03 11:37:37 -0700475// for either both signed or both unsigned operands x, y.
Aart Bikf3e61ee2017-04-12 17:09:20 -0700476class HVecHalvingAdd FINAL : public HVecBinaryOperation {
477 public:
478 HVecHalvingAdd(ArenaAllocator* arena,
479 HInstruction* left,
480 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100481 DataType::Type packed_type,
Aart Bikf3e61ee2017-04-12 17:09:20 -0700482 size_t vector_length,
Aart Bikf3e61ee2017-04-12 17:09:20 -0700483 bool is_rounded,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700484 bool is_unsigned,
485 uint32_t dex_pc)
486 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100487 // The `is_unsigned` flag should be used exclusively with the Int32 or Int64.
488 // This flag is a temporary measure while we do not have the Uint32 and Uint64 data types.
489 DCHECK(!is_unsigned ||
490 packed_type == DataType::Type::kInt32 ||
491 packed_type == DataType::Type::kInt64) << packed_type;
Aart Bikd58bc322017-05-01 14:49:18 -0700492 DCHECK(HasConsistentPackedTypes(left, packed_type));
493 DCHECK(HasConsistentPackedTypes(right, packed_type));
Aart Bikdb14fcf2017-04-25 15:53:58 -0700494 SetPackedFlag<kFieldHAddIsUnsigned>(is_unsigned);
495 SetPackedFlag<kFieldHAddIsRounded>(is_rounded);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700496 }
497
Aart Bikdb14fcf2017-04-25 15:53:58 -0700498 bool IsUnsigned() const { return GetPackedFlag<kFieldHAddIsUnsigned>(); }
499 bool IsRounded() const { return GetPackedFlag<kFieldHAddIsRounded>(); }
Aart Bikf3e61ee2017-04-12 17:09:20 -0700500
Aart Bikb79f4ac2017-07-10 10:10:37 -0700501 bool CanBeMoved() const OVERRIDE { return true; }
502
503 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
504 DCHECK(other->IsVecHalvingAdd());
505 const HVecHalvingAdd* o = other->AsVecHalvingAdd();
506 return HVecOperation::InstructionDataEquals(o) &&
507 IsUnsigned() == o->IsUnsigned() &&
508 IsRounded() == o->IsRounded();
509 }
510
Aart Bikf3e61ee2017-04-12 17:09:20 -0700511 DECLARE_INSTRUCTION(VecHalvingAdd);
512
513 private:
Aart Bikdb14fcf2017-04-25 15:53:58 -0700514 // Additional packed bits.
515 static constexpr size_t kFieldHAddIsUnsigned = HVecOperation::kNumberOfVectorOpPackedBits;
516 static constexpr size_t kFieldHAddIsRounded = kFieldHAddIsUnsigned + 1;
517 static constexpr size_t kNumberOfHAddPackedBits = kFieldHAddIsRounded + 1;
518 static_assert(kNumberOfHAddPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
Aart Bikf3e61ee2017-04-12 17:09:20 -0700519
520 DISALLOW_COPY_AND_ASSIGN(HVecHalvingAdd);
521};
522
Aart Bikf8f5a162017-02-06 15:35:29 -0800523// Subtracts every component in the two vectors,
524// viz. [ x1, .. , xn ] - [ y1, .. , yn ] = [ x1 - y1, .. , xn - yn ].
525class HVecSub FINAL : public HVecBinaryOperation {
526 public:
527 HVecSub(ArenaAllocator* arena,
528 HInstruction* left,
529 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100530 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800531 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700532 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700533 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700534 DCHECK(HasConsistentPackedTypes(left, packed_type));
535 DCHECK(HasConsistentPackedTypes(right, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800536 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700537
538 bool CanBeMoved() const OVERRIDE { return true; }
539
Aart Bikf8f5a162017-02-06 15:35:29 -0800540 DECLARE_INSTRUCTION(VecSub);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700541
Aart Bikf8f5a162017-02-06 15:35:29 -0800542 private:
543 DISALLOW_COPY_AND_ASSIGN(HVecSub);
544};
545
546// Multiplies every component in the two vectors,
547// viz. [ x1, .. , xn ] * [ y1, .. , yn ] = [ x1 * y1, .. , xn * yn ].
548class HVecMul FINAL : public HVecBinaryOperation {
549 public:
550 HVecMul(ArenaAllocator* arena,
551 HInstruction* left,
552 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100553 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800554 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700555 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700556 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700557 DCHECK(HasConsistentPackedTypes(left, packed_type));
558 DCHECK(HasConsistentPackedTypes(right, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800559 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700560
561 bool CanBeMoved() const OVERRIDE { return true; }
562
Aart Bikf8f5a162017-02-06 15:35:29 -0800563 DECLARE_INSTRUCTION(VecMul);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700564
Aart Bikf8f5a162017-02-06 15:35:29 -0800565 private:
566 DISALLOW_COPY_AND_ASSIGN(HVecMul);
567};
568
569// Divides every component in the two vectors,
570// viz. [ x1, .. , xn ] / [ y1, .. , yn ] = [ x1 / y1, .. , xn / yn ].
571class HVecDiv FINAL : public HVecBinaryOperation {
572 public:
573 HVecDiv(ArenaAllocator* arena,
574 HInstruction* left,
575 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100576 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800577 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700578 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700579 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700580 DCHECK(HasConsistentPackedTypes(left, packed_type));
581 DCHECK(HasConsistentPackedTypes(right, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800582 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700583
584 bool CanBeMoved() const OVERRIDE { return true; }
585
Aart Bikf8f5a162017-02-06 15:35:29 -0800586 DECLARE_INSTRUCTION(VecDiv);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700587
Aart Bikf8f5a162017-02-06 15:35:29 -0800588 private:
589 DISALLOW_COPY_AND_ASSIGN(HVecDiv);
590};
591
Aart Bikf3e61ee2017-04-12 17:09:20 -0700592// Takes minimum of every component in the two vectors,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700593// viz. MIN( [ x1, .. , xn ] , [ y1, .. , yn ]) = [ min(x1, y1), .. , min(xn, yn) ]
594// for either both signed or both unsigned operands x, y.
Aart Bikf3e61ee2017-04-12 17:09:20 -0700595class HVecMin FINAL : public HVecBinaryOperation {
596 public:
597 HVecMin(ArenaAllocator* arena,
598 HInstruction* left,
599 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100600 DataType::Type packed_type,
Aart Bikf3e61ee2017-04-12 17:09:20 -0700601 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700602 bool is_unsigned,
603 uint32_t dex_pc)
604 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100605 // The `is_unsigned` flag should be used exclusively with the Int32 or Int64.
606 // This flag is a temporary measure while we do not have the Uint32 and Uint64 data types.
607 DCHECK(!is_unsigned ||
608 packed_type == DataType::Type::kInt32 ||
609 packed_type == DataType::Type::kInt64) << packed_type;
Aart Bikd58bc322017-05-01 14:49:18 -0700610 DCHECK(HasConsistentPackedTypes(left, packed_type));
611 DCHECK(HasConsistentPackedTypes(right, packed_type));
Aart Bikc8e93c72017-05-10 10:49:22 -0700612 SetPackedFlag<kFieldMinOpIsUnsigned>(is_unsigned);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700613 }
Aart Bikc8e93c72017-05-10 10:49:22 -0700614
615 bool IsUnsigned() const { return GetPackedFlag<kFieldMinOpIsUnsigned>(); }
616
Aart Bikb79f4ac2017-07-10 10:10:37 -0700617 bool CanBeMoved() const OVERRIDE { return true; }
618
619 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
620 DCHECK(other->IsVecMin());
621 const HVecMin* o = other->AsVecMin();
622 return HVecOperation::InstructionDataEquals(o) && IsUnsigned() == o->IsUnsigned();
623 }
624
Aart Bikf3e61ee2017-04-12 17:09:20 -0700625 DECLARE_INSTRUCTION(VecMin);
Aart Bikc8e93c72017-05-10 10:49:22 -0700626
Aart Bikf3e61ee2017-04-12 17:09:20 -0700627 private:
Aart Bikc8e93c72017-05-10 10:49:22 -0700628 // Additional packed bits.
629 static constexpr size_t kFieldMinOpIsUnsigned = HVecOperation::kNumberOfVectorOpPackedBits;
630 static constexpr size_t kNumberOfMinOpPackedBits = kFieldMinOpIsUnsigned + 1;
631 static_assert(kNumberOfMinOpPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
632
Aart Bikf3e61ee2017-04-12 17:09:20 -0700633 DISALLOW_COPY_AND_ASSIGN(HVecMin);
634};
635
636// Takes maximum of every component in the two vectors,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700637// viz. MAX( [ x1, .. , xn ] , [ y1, .. , yn ]) = [ max(x1, y1), .. , max(xn, yn) ]
638// for either both signed or both unsigned operands x, y.
Aart Bikf3e61ee2017-04-12 17:09:20 -0700639class HVecMax FINAL : public HVecBinaryOperation {
640 public:
641 HVecMax(ArenaAllocator* arena,
642 HInstruction* left,
643 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100644 DataType::Type packed_type,
Aart Bikf3e61ee2017-04-12 17:09:20 -0700645 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700646 bool is_unsigned,
647 uint32_t dex_pc)
648 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100649 // The `is_unsigned` flag should be used exclusively with the Int32 or Int64.
650 // This flag is a temporary measure while we do not have the Uint32 and Uint64 data types.
651 DCHECK(!is_unsigned ||
652 packed_type == DataType::Type::kInt32 ||
653 packed_type == DataType::Type::kInt64) << packed_type;
Aart Bikd58bc322017-05-01 14:49:18 -0700654 DCHECK(HasConsistentPackedTypes(left, packed_type));
655 DCHECK(HasConsistentPackedTypes(right, packed_type));
Aart Bikc8e93c72017-05-10 10:49:22 -0700656 SetPackedFlag<kFieldMaxOpIsUnsigned>(is_unsigned);
Aart Bikf3e61ee2017-04-12 17:09:20 -0700657 }
Aart Bikc8e93c72017-05-10 10:49:22 -0700658
659 bool IsUnsigned() const { return GetPackedFlag<kFieldMaxOpIsUnsigned>(); }
660
Aart Bikb79f4ac2017-07-10 10:10:37 -0700661 bool CanBeMoved() const OVERRIDE { return true; }
662
663 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
664 DCHECK(other->IsVecMax());
665 const HVecMax* o = other->AsVecMax();
666 return HVecOperation::InstructionDataEquals(o) && IsUnsigned() == o->IsUnsigned();
667 }
668
Aart Bikf3e61ee2017-04-12 17:09:20 -0700669 DECLARE_INSTRUCTION(VecMax);
Aart Bikc8e93c72017-05-10 10:49:22 -0700670
Aart Bikf3e61ee2017-04-12 17:09:20 -0700671 private:
Aart Bikc8e93c72017-05-10 10:49:22 -0700672 // Additional packed bits.
673 static constexpr size_t kFieldMaxOpIsUnsigned = HVecOperation::kNumberOfVectorOpPackedBits;
674 static constexpr size_t kNumberOfMaxOpPackedBits = kFieldMaxOpIsUnsigned + 1;
675 static_assert(kNumberOfMaxOpPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
676
Aart Bikf3e61ee2017-04-12 17:09:20 -0700677 DISALLOW_COPY_AND_ASSIGN(HVecMax);
678};
679
Aart Bikf8f5a162017-02-06 15:35:29 -0800680// Bitwise-ands every component in the two vectors,
681// viz. [ x1, .. , xn ] & [ y1, .. , yn ] = [ x1 & y1, .. , xn & yn ].
682class HVecAnd FINAL : public HVecBinaryOperation {
683 public:
684 HVecAnd(ArenaAllocator* arena,
685 HInstruction* left,
686 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100687 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800688 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700689 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700690 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikf8f5a162017-02-06 15:35:29 -0800691 DCHECK(left->IsVecOperation() && right->IsVecOperation());
Aart Bikf8f5a162017-02-06 15:35:29 -0800692 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700693
694 bool CanBeMoved() const OVERRIDE { return true; }
695
Aart Bikf8f5a162017-02-06 15:35:29 -0800696 DECLARE_INSTRUCTION(VecAnd);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700697
Aart Bikf8f5a162017-02-06 15:35:29 -0800698 private:
699 DISALLOW_COPY_AND_ASSIGN(HVecAnd);
700};
701
702// Bitwise-and-nots every component in the two vectors,
703// viz. [ x1, .. , xn ] and-not [ y1, .. , yn ] = [ ~x1 & y1, .. , ~xn & yn ].
704class HVecAndNot FINAL : public HVecBinaryOperation {
705 public:
706 HVecAndNot(ArenaAllocator* arena,
707 HInstruction* left,
708 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100709 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800710 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700711 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700712 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikf8f5a162017-02-06 15:35:29 -0800713 DCHECK(left->IsVecOperation() && right->IsVecOperation());
Aart Bikf8f5a162017-02-06 15:35:29 -0800714 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700715
716 bool CanBeMoved() const OVERRIDE { return true; }
717
Aart Bikf8f5a162017-02-06 15:35:29 -0800718 DECLARE_INSTRUCTION(VecAndNot);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700719
Aart Bikf8f5a162017-02-06 15:35:29 -0800720 private:
721 DISALLOW_COPY_AND_ASSIGN(HVecAndNot);
722};
723
724// Bitwise-ors every component in the two vectors,
725// viz. [ x1, .. , xn ] | [ y1, .. , yn ] = [ x1 | y1, .. , xn | yn ].
726class HVecOr FINAL : public HVecBinaryOperation {
727 public:
728 HVecOr(ArenaAllocator* arena,
729 HInstruction* left,
730 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100731 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800732 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700733 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700734 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikf8f5a162017-02-06 15:35:29 -0800735 DCHECK(left->IsVecOperation() && right->IsVecOperation());
Aart Bikf8f5a162017-02-06 15:35:29 -0800736 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700737
738 bool CanBeMoved() const OVERRIDE { return true; }
739
Aart Bikf8f5a162017-02-06 15:35:29 -0800740 DECLARE_INSTRUCTION(VecOr);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700741
Aart Bikf8f5a162017-02-06 15:35:29 -0800742 private:
743 DISALLOW_COPY_AND_ASSIGN(HVecOr);
744};
745
746// Bitwise-xors every component in the two vectors,
747// viz. [ x1, .. , xn ] ^ [ y1, .. , yn ] = [ x1 ^ y1, .. , xn ^ yn ].
748class HVecXor FINAL : public HVecBinaryOperation {
749 public:
750 HVecXor(ArenaAllocator* arena,
751 HInstruction* left,
752 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100753 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800754 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700755 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700756 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikf8f5a162017-02-06 15:35:29 -0800757 DCHECK(left->IsVecOperation() && right->IsVecOperation());
Aart Bikf8f5a162017-02-06 15:35:29 -0800758 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700759
760 bool CanBeMoved() const OVERRIDE { return true; }
761
Aart Bikf8f5a162017-02-06 15:35:29 -0800762 DECLARE_INSTRUCTION(VecXor);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700763
Aart Bikf8f5a162017-02-06 15:35:29 -0800764 private:
765 DISALLOW_COPY_AND_ASSIGN(HVecXor);
766};
767
768// Logically shifts every component in the vector left by the given distance,
769// viz. [ x1, .. , xn ] << d = [ x1 << d, .. , xn << d ].
770class HVecShl FINAL : public HVecBinaryOperation {
771 public:
772 HVecShl(ArenaAllocator* arena,
773 HInstruction* left,
774 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100775 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800776 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700777 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700778 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700779 DCHECK(HasConsistentPackedTypes(left, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800780 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700781
782 bool CanBeMoved() const OVERRIDE { return true; }
783
Aart Bikf8f5a162017-02-06 15:35:29 -0800784 DECLARE_INSTRUCTION(VecShl);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700785
Aart Bikf8f5a162017-02-06 15:35:29 -0800786 private:
787 DISALLOW_COPY_AND_ASSIGN(HVecShl);
788};
789
790// Arithmetically shifts every component in the vector right by the given distance,
791// viz. [ x1, .. , xn ] >> d = [ x1 >> d, .. , xn >> d ].
792class HVecShr FINAL : public HVecBinaryOperation {
793 public:
794 HVecShr(ArenaAllocator* arena,
795 HInstruction* left,
796 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100797 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800798 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700799 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700800 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700801 DCHECK(HasConsistentPackedTypes(left, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800802 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700803
804 bool CanBeMoved() const OVERRIDE { return true; }
805
Aart Bikf8f5a162017-02-06 15:35:29 -0800806 DECLARE_INSTRUCTION(VecShr);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700807
Aart Bikf8f5a162017-02-06 15:35:29 -0800808 private:
809 DISALLOW_COPY_AND_ASSIGN(HVecShr);
810};
811
812// Logically shifts every component in the vector right by the given distance,
813// viz. [ x1, .. , xn ] >>> d = [ x1 >>> d, .. , xn >>> d ].
814class HVecUShr FINAL : public HVecBinaryOperation {
815 public:
816 HVecUShr(ArenaAllocator* arena,
817 HInstruction* left,
818 HInstruction* right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100819 DataType::Type packed_type,
Aart Bikf8f5a162017-02-06 15:35:29 -0800820 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700821 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700822 : HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -0700823 DCHECK(HasConsistentPackedTypes(left, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -0800824 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700825
826 bool CanBeMoved() const OVERRIDE { return true; }
827
Aart Bikf8f5a162017-02-06 15:35:29 -0800828 DECLARE_INSTRUCTION(VecUShr);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700829
Aart Bikf8f5a162017-02-06 15:35:29 -0800830 private:
831 DISALLOW_COPY_AND_ASSIGN(HVecUShr);
832};
833
Aart Bik8de59162017-04-21 09:42:01 -0700834//
835// Definitions of concrete miscellaneous vector operations in HIR.
836//
837
838// Assigns the given scalar elements to a vector,
Aart Bikdbbac8f2017-09-01 13:06:08 -0700839// viz. set( array(x1, .. , xn) ) = [ x1, .. , xn ] if n == m,
840// set( array(x1, .. , xm) ) = [ x1, .. , xm, 0, .. , 0 ] if m < n.
Aart Bik8de59162017-04-21 09:42:01 -0700841class HVecSetScalars FINAL : public HVecOperation {
Aart Bik0148de42017-09-05 09:25:01 -0700842 public:
Aart Bik8de59162017-04-21 09:42:01 -0700843 HVecSetScalars(ArenaAllocator* arena,
Aart Bik5e3afa92017-09-20 14:11:11 -0700844 HInstruction* scalars[],
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100845 DataType::Type packed_type,
Aart Bik8de59162017-04-21 09:42:01 -0700846 size_t vector_length,
Aart Bik0148de42017-09-05 09:25:01 -0700847 size_t number_of_scalars,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700848 uint32_t dex_pc)
Aart Bik8de59162017-04-21 09:42:01 -0700849 : HVecOperation(arena,
850 packed_type,
851 SideEffects::None(),
Aart Bik0148de42017-09-05 09:25:01 -0700852 number_of_scalars,
Aart Bik8de59162017-04-21 09:42:01 -0700853 vector_length,
854 dex_pc) {
Aart Bik0148de42017-09-05 09:25:01 -0700855 for (size_t i = 0; i < number_of_scalars; i++) {
Aart Bik5e3afa92017-09-20 14:11:11 -0700856 DCHECK(!scalars[i]->IsVecOperation() || scalars[i]->IsVecExtractScalar());
Aart Bik8de59162017-04-21 09:42:01 -0700857 SetRawInputAt(0, scalars[i]);
858 }
859 }
Aart Bikb79f4ac2017-07-10 10:10:37 -0700860
861 // Setting scalars needs to stay in place, since SIMD registers are not
862 // kept alive across vector loop boundaries (yet).
863 bool CanBeMoved() const OVERRIDE { return false; }
864
Aart Bik8de59162017-04-21 09:42:01 -0700865 DECLARE_INSTRUCTION(VecSetScalars);
Aart Bikb79f4ac2017-07-10 10:10:37 -0700866
Aart Bik8de59162017-04-21 09:42:01 -0700867 private:
868 DISALLOW_COPY_AND_ASSIGN(HVecSetScalars);
869};
870
Aart Bikdbbac8f2017-09-01 13:06:08 -0700871// Multiplies every component in the two vectors, adds the result vector to the accumulator vector,
872// viz. [ a1, .. , an ] + [ x1, .. , xn ] * [ y1, .. , yn ] = [ a1 + x1 * y1, .. , an + xn * yn ].
Artem Serovf34dd202017-04-10 17:41:46 +0100873class HVecMultiplyAccumulate FINAL : public HVecOperation {
874 public:
875 HVecMultiplyAccumulate(ArenaAllocator* arena,
876 InstructionKind op,
877 HInstruction* accumulator,
878 HInstruction* mul_left,
879 HInstruction* mul_right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100880 DataType::Type packed_type,
Artem Serovf34dd202017-04-10 17:41:46 +0100881 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700882 uint32_t dex_pc)
Artem Serovf34dd202017-04-10 17:41:46 +0100883 : HVecOperation(arena,
884 packed_type,
885 SideEffects::None(),
Aart Bik8de59162017-04-21 09:42:01 -0700886 /* number_of_inputs */ 3,
Artem Serovf34dd202017-04-10 17:41:46 +0100887 vector_length,
888 dex_pc),
889 op_kind_(op) {
890 DCHECK(op == InstructionKind::kAdd || op == InstructionKind::kSub);
Aart Bikd58bc322017-05-01 14:49:18 -0700891 DCHECK(HasConsistentPackedTypes(accumulator, packed_type));
892 DCHECK(HasConsistentPackedTypes(mul_left, packed_type));
893 DCHECK(HasConsistentPackedTypes(mul_right, packed_type));
Aart Bikdbbac8f2017-09-01 13:06:08 -0700894 SetRawInputAt(0, accumulator);
895 SetRawInputAt(1, mul_left);
896 SetRawInputAt(2, mul_right);
Artem Serovf34dd202017-04-10 17:41:46 +0100897 }
898
Nicolas Geoffray9858bf72017-07-08 12:34:55 +0000899 bool CanBeMoved() const OVERRIDE { return true; }
900
Artem Serovf34dd202017-04-10 17:41:46 +0100901 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
Aart Bikb79f4ac2017-07-10 10:10:37 -0700902 DCHECK(other->IsVecMultiplyAccumulate());
903 const HVecMultiplyAccumulate* o = other->AsVecMultiplyAccumulate();
904 return HVecOperation::InstructionDataEquals(o) && GetOpKind() == o->GetOpKind();
Artem Serovf34dd202017-04-10 17:41:46 +0100905 }
906
907 InstructionKind GetOpKind() const { return op_kind_; }
908
909 DECLARE_INSTRUCTION(VecMultiplyAccumulate);
910
911 private:
912 // Indicates if this is a MADD or MSUB.
913 const InstructionKind op_kind_;
914
915 DISALLOW_COPY_AND_ASSIGN(HVecMultiplyAccumulate);
916};
917
Aart Bikdbbac8f2017-09-01 13:06:08 -0700918// Takes the absolute difference of two vectors, and adds the results to
919// same-precision or wider-precision components in the accumulator,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700920// viz. SAD([ a1, .. , am ], [ x1, .. , xn ], [ y1, .. , yn ]) =
Aart Bikdbbac8f2017-09-01 13:06:08 -0700921// [ a1 + sum abs(xi-yi), .. , am + sum abs(xj-yj) ],
Aart Bik46b6dbc2017-10-03 11:37:37 -0700922// for m <= n, non-overlapping sums, and signed operands x, y.
Aart Bikdbbac8f2017-09-01 13:06:08 -0700923class HVecSADAccumulate FINAL : public HVecOperation {
924 public:
925 HVecSADAccumulate(ArenaAllocator* arena,
926 HInstruction* accumulator,
927 HInstruction* sad_left,
928 HInstruction* sad_right,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100929 DataType::Type packed_type,
Aart Bikdbbac8f2017-09-01 13:06:08 -0700930 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700931 uint32_t dex_pc)
Aart Bikdbbac8f2017-09-01 13:06:08 -0700932 : HVecOperation(arena,
933 packed_type,
934 SideEffects::None(),
935 /* number_of_inputs */ 3,
936 vector_length,
937 dex_pc) {
938 DCHECK(HasConsistentPackedTypes(accumulator, packed_type));
939 DCHECK(sad_left->IsVecOperation());
940 DCHECK(sad_right->IsVecOperation());
941 DCHECK_EQ(sad_left->AsVecOperation()->GetPackedType(),
942 sad_right->AsVecOperation()->GetPackedType());
943 SetRawInputAt(0, accumulator);
944 SetRawInputAt(1, sad_left);
945 SetRawInputAt(2, sad_right);
946 }
947
948 DECLARE_INSTRUCTION(VecSADAccumulate);
949
950 private:
951 DISALLOW_COPY_AND_ASSIGN(HVecSADAccumulate);
952};
953
Aart Bikf8f5a162017-02-06 15:35:29 -0800954// Loads a vector from memory, viz. load(mem, 1)
955// yield the vector [ mem(1), .. , mem(n) ].
956class HVecLoad FINAL : public HVecMemoryOperation {
957 public:
958 HVecLoad(ArenaAllocator* arena,
959 HInstruction* base,
960 HInstruction* index,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +0100961 DataType::Type packed_type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100962 SideEffects side_effects,
Aart Bikf8f5a162017-02-06 15:35:29 -0800963 size_t vector_length,
Aart Bikdb14fcf2017-04-25 15:53:58 -0700964 bool is_string_char_at,
Aart Bik46b6dbc2017-10-03 11:37:37 -0700965 uint32_t dex_pc)
Aart Bikf8f5a162017-02-06 15:35:29 -0800966 : HVecMemoryOperation(arena,
967 packed_type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +0100968 side_effects,
Aart Bik8de59162017-04-21 09:42:01 -0700969 /* number_of_inputs */ 2,
Aart Bikf8f5a162017-02-06 15:35:29 -0800970 vector_length,
971 dex_pc) {
972 SetRawInputAt(0, base);
973 SetRawInputAt(1, index);
Aart Bikdb14fcf2017-04-25 15:53:58 -0700974 SetPackedFlag<kFieldIsStringCharAt>(is_string_char_at);
Aart Bikf8f5a162017-02-06 15:35:29 -0800975 }
Aart Bikdb14fcf2017-04-25 15:53:58 -0700976
977 bool IsStringCharAt() const { return GetPackedFlag<kFieldIsStringCharAt>(); }
978
Aart Bikb79f4ac2017-07-10 10:10:37 -0700979 bool CanBeMoved() const OVERRIDE { return true; }
980
981 bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
982 DCHECK(other->IsVecLoad());
983 const HVecLoad* o = other->AsVecLoad();
984 return HVecMemoryOperation::InstructionDataEquals(o) && IsStringCharAt() == o->IsStringCharAt();
985 }
986
987 DECLARE_INSTRUCTION(VecLoad);
988
Aart Bikf8f5a162017-02-06 15:35:29 -0800989 private:
Aart Bikdb14fcf2017-04-25 15:53:58 -0700990 // Additional packed bits.
991 static constexpr size_t kFieldIsStringCharAt = HVecOperation::kNumberOfVectorOpPackedBits;
992 static constexpr size_t kNumberOfVecLoadPackedBits = kFieldIsStringCharAt + 1;
993 static_assert(kNumberOfVecLoadPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
994
Aart Bikf8f5a162017-02-06 15:35:29 -0800995 DISALLOW_COPY_AND_ASSIGN(HVecLoad);
996};
997
998// Stores a vector to memory, viz. store(m, 1, [x1, .. , xn] )
999// sets mem(1) = x1, .. , mem(n) = xn.
1000class HVecStore FINAL : public HVecMemoryOperation {
1001 public:
1002 HVecStore(ArenaAllocator* arena,
1003 HInstruction* base,
1004 HInstruction* index,
1005 HInstruction* value,
Vladimir Marko0ebe0d82017-09-21 22:50:39 +01001006 DataType::Type packed_type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001007 SideEffects side_effects,
Aart Bikf8f5a162017-02-06 15:35:29 -08001008 size_t vector_length,
Aart Bik46b6dbc2017-10-03 11:37:37 -07001009 uint32_t dex_pc)
Aart Bikf8f5a162017-02-06 15:35:29 -08001010 : HVecMemoryOperation(arena,
1011 packed_type,
Vladimir Markod5d2f2c2017-09-26 12:37:26 +01001012 side_effects,
Aart Bik8de59162017-04-21 09:42:01 -07001013 /* number_of_inputs */ 3,
Aart Bikf8f5a162017-02-06 15:35:29 -08001014 vector_length,
1015 dex_pc) {
Aart Bikd58bc322017-05-01 14:49:18 -07001016 DCHECK(HasConsistentPackedTypes(value, packed_type));
Aart Bikf8f5a162017-02-06 15:35:29 -08001017 SetRawInputAt(0, base);
1018 SetRawInputAt(1, index);
1019 SetRawInputAt(2, value);
1020 }
Aart Bikb79f4ac2017-07-10 10:10:37 -07001021
1022 // A store needs to stay in place.
1023 bool CanBeMoved() const OVERRIDE { return false; }
1024
Aart Bikf8f5a162017-02-06 15:35:29 -08001025 DECLARE_INSTRUCTION(VecStore);
Aart Bikb79f4ac2017-07-10 10:10:37 -07001026
Aart Bikf8f5a162017-02-06 15:35:29 -08001027 private:
1028 DISALLOW_COPY_AND_ASSIGN(HVecStore);
1029};
1030
1031} // namespace art
1032
1033#endif // ART_COMPILER_OPTIMIZING_NODES_VECTOR_H_