1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
|
/*
* Copyright (C) 2017 The Android Open Source Project
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef ART_COMPILER_OPTIMIZING_NODES_VECTOR_H_
#define ART_COMPILER_OPTIMIZING_NODES_VECTOR_H_
// This #include should never be used by compilation, because this header file (nodes_vector.h)
// is included in the header file nodes.h itself. However it gives editing tools better context.
#include "nodes.h"
namespace art {
// Memory alignment, represented as an offset relative to a base, where 0 <= offset < base,
// and base is a power of two. For example, the value Alignment(16, 0) means memory is
// perfectly aligned at a 16-byte boundary, whereas the value Alignment(16, 4) means
// memory is always exactly 4 bytes above such a boundary.
class Alignment {
public:
Alignment(size_t base, size_t offset) : base_(base), offset_(offset) {
DCHECK_LT(offset, base);
DCHECK(IsPowerOfTwo(base));
}
// Returns true if memory is "at least" aligned at the given boundary.
// Assumes requested base is power of two.
bool IsAlignedAt(size_t base) const {
DCHECK_NE(0u, base);
DCHECK(IsPowerOfTwo(base));
return ((offset_ | base_) & (base - 1u)) == 0;
}
std::string ToString() const {
return "ALIGN(" + std::to_string(base_) + "," + std::to_string(offset_) + ")";
}
bool operator==(const Alignment& other) const {
return base_ == other.base_ && offset_ == other.offset_;
}
private:
size_t base_;
size_t offset_;
};
//
// Definitions of abstract vector operations in HIR.
//
// Abstraction of a vector operation, i.e., an operation that performs
// GetVectorLength() x GetPackedType() operations simultaneously.
class HVecOperation : public HVariableInputSizeInstruction {
public:
// A SIMD operation looks like a FPU location.
// TODO: we could introduce SIMD types in HIR.
static constexpr Primitive::Type kSIMDType = Primitive::kPrimDouble;
HVecOperation(ArenaAllocator* arena,
Primitive::Type packed_type,
SideEffects side_effects,
size_t number_of_inputs,
size_t vector_length,
uint32_t dex_pc)
: HVariableInputSizeInstruction(side_effects,
dex_pc,
arena,
number_of_inputs,
kArenaAllocVectorNode),
vector_length_(vector_length) {
SetPackedField<TypeField>(packed_type);
DCHECK_LT(1u, vector_length);
}
// Returns the number of elements packed in a vector.
size_t GetVectorLength() const {
return vector_length_;
}
// Returns the number of bytes in a full vector.
size_t GetVectorNumberOfBytes() const {
return vector_length_ * Primitive::ComponentSize(GetPackedType());
}
// Returns the type of the vector operation.
Primitive::Type GetType() const OVERRIDE {
return kSIMDType;
}
// Returns the true component type packed in a vector.
Primitive::Type GetPackedType() const {
return GetPackedField<TypeField>();
}
// Assumes vector nodes cannot be moved by default. Each concrete implementation
// that can be moved should override this method and return true.
bool CanBeMoved() const OVERRIDE { return false; }
// Tests if all data of a vector node (vector length and packed type) is equal.
// Each concrete implementation that adds more fields should test equality of
// those fields in its own method *and* call all super methods.
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecOperation());
const HVecOperation* o = other->AsVecOperation();
return GetVectorLength() == o->GetVectorLength() && GetPackedType() == o->GetPackedType();
}
DECLARE_ABSTRACT_INSTRUCTION(VecOperation);
protected:
// Additional packed bits.
static constexpr size_t kFieldType = HInstruction::kNumberOfGenericPackedBits;
static constexpr size_t kFieldTypeSize =
MinimumBitsToStore(static_cast<size_t>(Primitive::kPrimLast));
static constexpr size_t kNumberOfVectorOpPackedBits = kFieldType + kFieldTypeSize;
static_assert(kNumberOfVectorOpPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
using TypeField = BitField<Primitive::Type, kFieldType, kFieldTypeSize>;
private:
const size_t vector_length_;
DISALLOW_COPY_AND_ASSIGN(HVecOperation);
};
// Abstraction of a unary vector operation.
class HVecUnaryOperation : public HVecOperation {
public:
HVecUnaryOperation(ArenaAllocator* arena,
HInstruction* input,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc)
: HVecOperation(arena,
packed_type,
SideEffects::None(),
/* number_of_inputs */ 1,
vector_length,
dex_pc) {
SetRawInputAt(0, input);
}
HInstruction* GetInput() const { return InputAt(0); }
DECLARE_ABSTRACT_INSTRUCTION(VecUnaryOperation);
private:
DISALLOW_COPY_AND_ASSIGN(HVecUnaryOperation);
};
// Abstraction of a binary vector operation.
class HVecBinaryOperation : public HVecOperation {
public:
HVecBinaryOperation(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc)
: HVecOperation(arena,
packed_type,
SideEffects::None(),
/* number_of_inputs */ 2,
vector_length,
dex_pc) {
SetRawInputAt(0, left);
SetRawInputAt(1, right);
}
HInstruction* GetLeft() const { return InputAt(0); }
HInstruction* GetRight() const { return InputAt(1); }
DECLARE_ABSTRACT_INSTRUCTION(VecBinaryOperation);
private:
DISALLOW_COPY_AND_ASSIGN(HVecBinaryOperation);
};
// Abstraction of a vector operation that references memory, with an alignment.
// The Android runtime guarantees at least "component size" alignment for array
// elements and, thus, vectors.
class HVecMemoryOperation : public HVecOperation {
public:
HVecMemoryOperation(ArenaAllocator* arena,
Primitive::Type packed_type,
SideEffects side_effects,
size_t number_of_inputs,
size_t vector_length,
uint32_t dex_pc)
: HVecOperation(arena, packed_type, side_effects, number_of_inputs, vector_length, dex_pc),
alignment_(Primitive::ComponentSize(packed_type), 0) {
DCHECK_GE(number_of_inputs, 2u);
}
void SetAlignment(Alignment alignment) { alignment_ = alignment; }
Alignment GetAlignment() const { return alignment_; }
HInstruction* GetArray() const { return InputAt(0); }
HInstruction* GetIndex() const { return InputAt(1); }
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecMemoryOperation());
const HVecMemoryOperation* o = other->AsVecMemoryOperation();
return HVecOperation::InstructionDataEquals(o) && GetAlignment() == o->GetAlignment();
}
DECLARE_ABSTRACT_INSTRUCTION(VecMemoryOperation);
private:
Alignment alignment_;
DISALLOW_COPY_AND_ASSIGN(HVecMemoryOperation);
};
// Packed type consistency checker ("same vector length" integral types may mix freely).
inline static bool HasConsistentPackedTypes(HInstruction* input, Primitive::Type type) {
if (input->IsPhi()) {
return input->GetType() == HVecOperation::kSIMDType; // carries SIMD
}
DCHECK(input->IsVecOperation());
Primitive::Type input_type = input->AsVecOperation()->GetPackedType();
switch (input_type) {
case Primitive::kPrimBoolean:
case Primitive::kPrimByte:
return type == Primitive::kPrimBoolean ||
type == Primitive::kPrimByte;
case Primitive::kPrimChar:
case Primitive::kPrimShort:
return type == Primitive::kPrimChar ||
type == Primitive::kPrimShort;
default:
return type == input_type;
}
}
//
// Definitions of concrete unary vector operations in HIR.
//
// Replicates the given scalar into a vector,
// viz. replicate(x) = [ x, .. , x ].
class HVecReplicateScalar FINAL : public HVecUnaryOperation {
public:
HVecReplicateScalar(ArenaAllocator* arena,
HInstruction* scalar,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecUnaryOperation(arena, scalar, packed_type, vector_length, dex_pc) {
DCHECK(!scalar->IsVecOperation());
}
// A replicate needs to stay in place, since SIMD registers are not
// kept alive across vector loop boundaries (yet).
bool CanBeMoved() const OVERRIDE { return false; }
DECLARE_INSTRUCTION(VecReplicateScalar);
private:
DISALLOW_COPY_AND_ASSIGN(HVecReplicateScalar);
};
// Extracts a particular scalar from the given vector,
// viz. extract[ x1, .. , xn ] = x_i.
//
// TODO: for now only i == 1 case supported.
class HVecExtractScalar FINAL : public HVecUnaryOperation {
public:
HVecExtractScalar(ArenaAllocator* arena,
HInstruction* input,
Primitive::Type packed_type,
size_t vector_length,
size_t index,
uint32_t dex_pc = kNoDexPc)
: HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(input, packed_type));
DCHECK_LT(index, vector_length);
DCHECK_EQ(index, 0u);
}
// Yields a single component in the vector.
Primitive::Type GetType() const OVERRIDE {
return GetPackedType();
}
// An extract needs to stay in place, since SIMD registers are not
// kept alive across vector loop boundaries (yet).
bool CanBeMoved() const OVERRIDE { return false; }
DECLARE_INSTRUCTION(VecExtractScalar);
private:
DISALLOW_COPY_AND_ASSIGN(HVecExtractScalar);
};
// Reduces the given vector into the first element as sum/min/max,
// viz. sum-reduce[ x1, .. , xn ] = [ y, ---- ], where y = sum xi
// and the "-" denotes "don't care" (implementation dependent).
class HVecReduce FINAL : public HVecUnaryOperation {
public:
enum ReductionKind {
kSum = 1,
kMin = 2,
kMax = 3
};
HVecReduce(ArenaAllocator* arena,
HInstruction* input,
Primitive::Type packed_type,
size_t vector_length,
ReductionKind kind,
uint32_t dex_pc = kNoDexPc)
: HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc),
kind_(kind) {
DCHECK(HasConsistentPackedTypes(input, packed_type));
}
ReductionKind GetKind() const { return kind_; }
bool CanBeMoved() const OVERRIDE { return true; }
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecReduce());
const HVecReduce* o = other->AsVecReduce();
return HVecOperation::InstructionDataEquals(o) && GetKind() == o->GetKind();
}
DECLARE_INSTRUCTION(VecReduce);
private:
const ReductionKind kind_;
DISALLOW_COPY_AND_ASSIGN(HVecReduce);
};
// Converts every component in the vector,
// viz. cnv[ x1, .. , xn ] = [ cnv(x1), .. , cnv(xn) ].
class HVecCnv FINAL : public HVecUnaryOperation {
public:
HVecCnv(ArenaAllocator* arena,
HInstruction* input,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
DCHECK(input->IsVecOperation());
DCHECK_NE(GetInputType(), GetResultType()); // actual convert
}
Primitive::Type GetInputType() const { return InputAt(0)->AsVecOperation()->GetPackedType(); }
Primitive::Type GetResultType() const { return GetPackedType(); }
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecCnv);
private:
DISALLOW_COPY_AND_ASSIGN(HVecCnv);
};
// Negates every component in the vector,
// viz. neg[ x1, .. , xn ] = [ -x1, .. , -xn ].
class HVecNeg FINAL : public HVecUnaryOperation {
public:
HVecNeg(ArenaAllocator* arena,
HInstruction* input,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(input, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecNeg);
private:
DISALLOW_COPY_AND_ASSIGN(HVecNeg);
};
// Takes absolute value of every component in the vector,
// viz. abs[ x1, .. , xn ] = [ |x1|, .. , |xn| ].
class HVecAbs FINAL : public HVecUnaryOperation {
public:
HVecAbs(ArenaAllocator* arena,
HInstruction* input,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(input, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecAbs);
private:
DISALLOW_COPY_AND_ASSIGN(HVecAbs);
};
// Bitwise- or boolean-nots every component in the vector,
// viz. not[ x1, .. , xn ] = [ ~x1, .. , ~xn ], or
// not[ x1, .. , xn ] = [ !x1, .. , !xn ] for boolean.
class HVecNot FINAL : public HVecUnaryOperation {
public:
HVecNot(ArenaAllocator* arena,
HInstruction* input,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecUnaryOperation(arena, input, packed_type, vector_length, dex_pc) {
DCHECK(input->IsVecOperation());
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecNot);
private:
DISALLOW_COPY_AND_ASSIGN(HVecNot);
};
//
// Definitions of concrete binary vector operations in HIR.
//
// Adds every component in the two vectors,
// viz. [ x1, .. , xn ] + [ y1, .. , yn ] = [ x1 + y1, .. , xn + yn ].
class HVecAdd FINAL : public HVecBinaryOperation {
public:
HVecAdd(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
DCHECK(HasConsistentPackedTypes(right, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecAdd);
private:
DISALLOW_COPY_AND_ASSIGN(HVecAdd);
};
// Performs halving add on every component in the two vectors, viz.
// rounded [ x1, .. , xn ] hradd [ y1, .. , yn ] = [ (x1 + y1 + 1) >> 1, .. , (xn + yn + 1) >> 1 ]
// or [ x1, .. , xn ] hadd [ y1, .. , yn ] = [ (x1 + y1) >> 1, .. , (xn + yn ) >> 1 ]
// for signed operands x, y (sign extension) or unsigned operands x, y (zero extension).
class HVecHalvingAdd FINAL : public HVecBinaryOperation {
public:
HVecHalvingAdd(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
bool is_unsigned,
bool is_rounded,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
DCHECK(HasConsistentPackedTypes(right, packed_type));
SetPackedFlag<kFieldHAddIsUnsigned>(is_unsigned);
SetPackedFlag<kFieldHAddIsRounded>(is_rounded);
}
bool IsUnsigned() const { return GetPackedFlag<kFieldHAddIsUnsigned>(); }
bool IsRounded() const { return GetPackedFlag<kFieldHAddIsRounded>(); }
bool CanBeMoved() const OVERRIDE { return true; }
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecHalvingAdd());
const HVecHalvingAdd* o = other->AsVecHalvingAdd();
return HVecOperation::InstructionDataEquals(o) &&
IsUnsigned() == o->IsUnsigned() &&
IsRounded() == o->IsRounded();
}
DECLARE_INSTRUCTION(VecHalvingAdd);
private:
// Additional packed bits.
static constexpr size_t kFieldHAddIsUnsigned = HVecOperation::kNumberOfVectorOpPackedBits;
static constexpr size_t kFieldHAddIsRounded = kFieldHAddIsUnsigned + 1;
static constexpr size_t kNumberOfHAddPackedBits = kFieldHAddIsRounded + 1;
static_assert(kNumberOfHAddPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
DISALLOW_COPY_AND_ASSIGN(HVecHalvingAdd);
};
// Subtracts every component in the two vectors,
// viz. [ x1, .. , xn ] - [ y1, .. , yn ] = [ x1 - y1, .. , xn - yn ].
class HVecSub FINAL : public HVecBinaryOperation {
public:
HVecSub(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
DCHECK(HasConsistentPackedTypes(right, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecSub);
private:
DISALLOW_COPY_AND_ASSIGN(HVecSub);
};
// Multiplies every component in the two vectors,
// viz. [ x1, .. , xn ] * [ y1, .. , yn ] = [ x1 * y1, .. , xn * yn ].
class HVecMul FINAL : public HVecBinaryOperation {
public:
HVecMul(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
DCHECK(HasConsistentPackedTypes(right, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecMul);
private:
DISALLOW_COPY_AND_ASSIGN(HVecMul);
};
// Divides every component in the two vectors,
// viz. [ x1, .. , xn ] / [ y1, .. , yn ] = [ x1 / y1, .. , xn / yn ].
class HVecDiv FINAL : public HVecBinaryOperation {
public:
HVecDiv(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
DCHECK(HasConsistentPackedTypes(right, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecDiv);
private:
DISALLOW_COPY_AND_ASSIGN(HVecDiv);
};
// Takes minimum of every component in the two vectors,
// viz. MIN( [ x1, .. , xn ] , [ y1, .. , yn ]) = [ min(x1, y1), .. , min(xn, yn) ].
class HVecMin FINAL : public HVecBinaryOperation {
public:
HVecMin(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
bool is_unsigned,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
DCHECK(HasConsistentPackedTypes(right, packed_type));
SetPackedFlag<kFieldMinOpIsUnsigned>(is_unsigned);
}
bool IsUnsigned() const { return GetPackedFlag<kFieldMinOpIsUnsigned>(); }
bool CanBeMoved() const OVERRIDE { return true; }
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecMin());
const HVecMin* o = other->AsVecMin();
return HVecOperation::InstructionDataEquals(o) && IsUnsigned() == o->IsUnsigned();
}
DECLARE_INSTRUCTION(VecMin);
private:
// Additional packed bits.
static constexpr size_t kFieldMinOpIsUnsigned = HVecOperation::kNumberOfVectorOpPackedBits;
static constexpr size_t kNumberOfMinOpPackedBits = kFieldMinOpIsUnsigned + 1;
static_assert(kNumberOfMinOpPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
DISALLOW_COPY_AND_ASSIGN(HVecMin);
};
// Takes maximum of every component in the two vectors,
// viz. MAX( [ x1, .. , xn ] , [ y1, .. , yn ]) = [ max(x1, y1), .. , max(xn, yn) ].
class HVecMax FINAL : public HVecBinaryOperation {
public:
HVecMax(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
bool is_unsigned,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
DCHECK(HasConsistentPackedTypes(right, packed_type));
SetPackedFlag<kFieldMaxOpIsUnsigned>(is_unsigned);
}
bool IsUnsigned() const { return GetPackedFlag<kFieldMaxOpIsUnsigned>(); }
bool CanBeMoved() const OVERRIDE { return true; }
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecMax());
const HVecMax* o = other->AsVecMax();
return HVecOperation::InstructionDataEquals(o) && IsUnsigned() == o->IsUnsigned();
}
DECLARE_INSTRUCTION(VecMax);
private:
// Additional packed bits.
static constexpr size_t kFieldMaxOpIsUnsigned = HVecOperation::kNumberOfVectorOpPackedBits;
static constexpr size_t kNumberOfMaxOpPackedBits = kFieldMaxOpIsUnsigned + 1;
static_assert(kNumberOfMaxOpPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
DISALLOW_COPY_AND_ASSIGN(HVecMax);
};
// Bitwise-ands every component in the two vectors,
// viz. [ x1, .. , xn ] & [ y1, .. , yn ] = [ x1 & y1, .. , xn & yn ].
class HVecAnd FINAL : public HVecBinaryOperation {
public:
HVecAnd(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(left->IsVecOperation() && right->IsVecOperation());
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecAnd);
private:
DISALLOW_COPY_AND_ASSIGN(HVecAnd);
};
// Bitwise-and-nots every component in the two vectors,
// viz. [ x1, .. , xn ] and-not [ y1, .. , yn ] = [ ~x1 & y1, .. , ~xn & yn ].
class HVecAndNot FINAL : public HVecBinaryOperation {
public:
HVecAndNot(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(left->IsVecOperation() && right->IsVecOperation());
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecAndNot);
private:
DISALLOW_COPY_AND_ASSIGN(HVecAndNot);
};
// Bitwise-ors every component in the two vectors,
// viz. [ x1, .. , xn ] | [ y1, .. , yn ] = [ x1 | y1, .. , xn | yn ].
class HVecOr FINAL : public HVecBinaryOperation {
public:
HVecOr(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(left->IsVecOperation() && right->IsVecOperation());
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecOr);
private:
DISALLOW_COPY_AND_ASSIGN(HVecOr);
};
// Bitwise-xors every component in the two vectors,
// viz. [ x1, .. , xn ] ^ [ y1, .. , yn ] = [ x1 ^ y1, .. , xn ^ yn ].
class HVecXor FINAL : public HVecBinaryOperation {
public:
HVecXor(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(left->IsVecOperation() && right->IsVecOperation());
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecXor);
private:
DISALLOW_COPY_AND_ASSIGN(HVecXor);
};
// Logically shifts every component in the vector left by the given distance,
// viz. [ x1, .. , xn ] << d = [ x1 << d, .. , xn << d ].
class HVecShl FINAL : public HVecBinaryOperation {
public:
HVecShl(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecShl);
private:
DISALLOW_COPY_AND_ASSIGN(HVecShl);
};
// Arithmetically shifts every component in the vector right by the given distance,
// viz. [ x1, .. , xn ] >> d = [ x1 >> d, .. , xn >> d ].
class HVecShr FINAL : public HVecBinaryOperation {
public:
HVecShr(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecShr);
private:
DISALLOW_COPY_AND_ASSIGN(HVecShr);
};
// Logically shifts every component in the vector right by the given distance,
// viz. [ x1, .. , xn ] >>> d = [ x1 >>> d, .. , xn >>> d ].
class HVecUShr FINAL : public HVecBinaryOperation {
public:
HVecUShr(ArenaAllocator* arena,
HInstruction* left,
HInstruction* right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecBinaryOperation(arena, left, right, packed_type, vector_length, dex_pc) {
DCHECK(HasConsistentPackedTypes(left, packed_type));
}
bool CanBeMoved() const OVERRIDE { return true; }
DECLARE_INSTRUCTION(VecUShr);
private:
DISALLOW_COPY_AND_ASSIGN(HVecUShr);
};
//
// Definitions of concrete miscellaneous vector operations in HIR.
//
// Assigns the given scalar elements to a vector,
// viz. set( array(x1, .., xn) ) = [ x1, .. , xn ] if n == m,
// set( array(x1, .., xm) ) = [ x1, .. , xm, 0, .., 0 ] if m < n.
class HVecSetScalars FINAL : public HVecOperation {
public:
HVecSetScalars(ArenaAllocator* arena,
HInstruction** scalars, // array
Primitive::Type packed_type,
size_t vector_length,
size_t number_of_scalars,
uint32_t dex_pc = kNoDexPc)
: HVecOperation(arena,
packed_type,
SideEffects::None(),
number_of_scalars,
vector_length,
dex_pc) {
for (size_t i = 0; i < number_of_scalars; i++) {
DCHECK(!scalars[i]->IsVecOperation());
SetRawInputAt(0, scalars[i]);
}
}
// Setting scalars needs to stay in place, since SIMD registers are not
// kept alive across vector loop boundaries (yet).
bool CanBeMoved() const OVERRIDE { return false; }
DECLARE_INSTRUCTION(VecSetScalars);
private:
DISALLOW_COPY_AND_ASSIGN(HVecSetScalars);
};
// Multiplies every component in the two vectors, adds the result vector to the accumulator vector.
// viz. [ acc1, .., accn ] + [ x1, .. , xn ] * [ y1, .. , yn ] =
// [ acc1 + x1 * y1, .. , accn + xn * yn ].
class HVecMultiplyAccumulate FINAL : public HVecOperation {
public:
HVecMultiplyAccumulate(ArenaAllocator* arena,
InstructionKind op,
HInstruction* accumulator,
HInstruction* mul_left,
HInstruction* mul_right,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecOperation(arena,
packed_type,
SideEffects::None(),
/* number_of_inputs */ 3,
vector_length,
dex_pc),
op_kind_(op) {
DCHECK(op == InstructionKind::kAdd || op == InstructionKind::kSub);
DCHECK(HasConsistentPackedTypes(accumulator, packed_type));
DCHECK(HasConsistentPackedTypes(mul_left, packed_type));
DCHECK(HasConsistentPackedTypes(mul_right, packed_type));
SetRawInputAt(kInputAccumulatorIndex, accumulator);
SetRawInputAt(kInputMulLeftIndex, mul_left);
SetRawInputAt(kInputMulRightIndex, mul_right);
}
static constexpr int kInputAccumulatorIndex = 0;
static constexpr int kInputMulLeftIndex = 1;
static constexpr int kInputMulRightIndex = 2;
bool CanBeMoved() const OVERRIDE { return true; }
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecMultiplyAccumulate());
const HVecMultiplyAccumulate* o = other->AsVecMultiplyAccumulate();
return HVecOperation::InstructionDataEquals(o) && GetOpKind() == o->GetOpKind();
}
InstructionKind GetOpKind() const { return op_kind_; }
DECLARE_INSTRUCTION(VecMultiplyAccumulate);
private:
// Indicates if this is a MADD or MSUB.
const InstructionKind op_kind_;
DISALLOW_COPY_AND_ASSIGN(HVecMultiplyAccumulate);
};
// Loads a vector from memory, viz. load(mem, 1)
// yield the vector [ mem(1), .. , mem(n) ].
class HVecLoad FINAL : public HVecMemoryOperation {
public:
HVecLoad(ArenaAllocator* arena,
HInstruction* base,
HInstruction* index,
Primitive::Type packed_type,
size_t vector_length,
bool is_string_char_at,
uint32_t dex_pc = kNoDexPc)
: HVecMemoryOperation(arena,
packed_type,
SideEffects::ArrayReadOfType(packed_type),
/* number_of_inputs */ 2,
vector_length,
dex_pc) {
SetRawInputAt(0, base);
SetRawInputAt(1, index);
SetPackedFlag<kFieldIsStringCharAt>(is_string_char_at);
}
bool IsStringCharAt() const { return GetPackedFlag<kFieldIsStringCharAt>(); }
bool CanBeMoved() const OVERRIDE { return true; }
bool InstructionDataEquals(const HInstruction* other) const OVERRIDE {
DCHECK(other->IsVecLoad());
const HVecLoad* o = other->AsVecLoad();
return HVecMemoryOperation::InstructionDataEquals(o) && IsStringCharAt() == o->IsStringCharAt();
}
DECLARE_INSTRUCTION(VecLoad);
private:
// Additional packed bits.
static constexpr size_t kFieldIsStringCharAt = HVecOperation::kNumberOfVectorOpPackedBits;
static constexpr size_t kNumberOfVecLoadPackedBits = kFieldIsStringCharAt + 1;
static_assert(kNumberOfVecLoadPackedBits <= kMaxNumberOfPackedBits, "Too many packed fields.");
DISALLOW_COPY_AND_ASSIGN(HVecLoad);
};
// Stores a vector to memory, viz. store(m, 1, [x1, .. , xn] )
// sets mem(1) = x1, .. , mem(n) = xn.
class HVecStore FINAL : public HVecMemoryOperation {
public:
HVecStore(ArenaAllocator* arena,
HInstruction* base,
HInstruction* index,
HInstruction* value,
Primitive::Type packed_type,
size_t vector_length,
uint32_t dex_pc = kNoDexPc)
: HVecMemoryOperation(arena,
packed_type,
SideEffects::ArrayWriteOfType(packed_type),
/* number_of_inputs */ 3,
vector_length,
dex_pc) {
DCHECK(HasConsistentPackedTypes(value, packed_type));
SetRawInputAt(0, base);
SetRawInputAt(1, index);
SetRawInputAt(2, value);
}
// A store needs to stay in place.
bool CanBeMoved() const OVERRIDE { return false; }
DECLARE_INSTRUCTION(VecStore);
private:
DISALLOW_COPY_AND_ASSIGN(HVecStore);
};
} // namespace art
#endif // ART_COMPILER_OPTIMIZING_NODES_VECTOR_H_
|