-
Notifications
You must be signed in to change notification settings - Fork 5.8k
/
Copy pathdivnode.cpp
1782 lines (1485 loc) · 62.2 KB
/
divnode.cpp
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
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
/*
* Copyright (c) 1997, 2025, Oracle and/or its affiliates. All rights reserved.
* DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
*
* This code is free software; you can redistribute it and/or modify it
* under the terms of the GNU General Public License version 2 only, as
* published by the Free Software Foundation.
*
* This code is distributed in the hope that it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
* version 2 for more details (a copy is included in the LICENSE file that
* accompanied this code).
*
* You should have received a copy of the GNU General Public License version
* 2 along with this work; if not, write to the Free Software Foundation,
* Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
*
* Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
* or visit www.oracle.com if you need additional information or have any
* questions.
*
*/
#include "memory/allocation.inline.hpp"
#include "opto/addnode.hpp"
#include "opto/connode.hpp"
#include "opto/convertnode.hpp"
#include "opto/divnode.hpp"
#include "opto/machnode.hpp"
#include "opto/movenode.hpp"
#include "opto/matcher.hpp"
#include "opto/mulnode.hpp"
#include "opto/phaseX.hpp"
#include "opto/subnode.hpp"
#include "utilities/powerOfTwo.hpp"
#include "opto/runtime.hpp"
// Portions of code courtesy of Clifford Click
// Optimization - Graph Style
#include <math.h>
ModFloatingNode::ModFloatingNode(Compile* C, const TypeFunc* tf, const char* name) : CallLeafNode(tf, nullptr, name, TypeRawPtr::BOTTOM) {
add_flag(Flag_is_macro);
C->add_macro_node(this);
}
ModDNode::ModDNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::Math_DD_D_Type(), "drem") {
init_req(TypeFunc::Parms + 0, a);
init_req(TypeFunc::Parms + 1, C->top());
init_req(TypeFunc::Parms + 2, b);
init_req(TypeFunc::Parms + 3, C->top());
}
ModFNode::ModFNode(Compile* C, Node* a, Node* b) : ModFloatingNode(C, OptoRuntime::modf_Type(), "frem") {
init_req(TypeFunc::Parms + 0, a);
init_req(TypeFunc::Parms + 1, b);
}
//----------------------magic_int_divide_constants-----------------------------
// Compute magic multiplier and shift constant for converting a 32 bit divide
// by constant into a multiply/shift/add series. Return false if calculations
// fail.
//
// Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with
// minor type name and parameter changes.
static bool magic_int_divide_constants(jint d, jint &M, jint &s) {
int32_t p;
uint32_t ad, anc, delta, q1, r1, q2, r2, t;
const uint32_t two31 = 0x80000000L; // 2**31.
ad = ABS(d);
if (d == 0 || d == 1) return false;
t = two31 + ((uint32_t)d >> 31);
anc = t - 1 - t%ad; // Absolute value of nc.
p = 31; // Init. p.
q1 = two31/anc; // Init. q1 = 2**p/|nc|.
r1 = two31 - q1*anc; // Init. r1 = rem(2**p, |nc|).
q2 = two31/ad; // Init. q2 = 2**p/|d|.
r2 = two31 - q2*ad; // Init. r2 = rem(2**p, |d|).
do {
p = p + 1;
q1 = 2*q1; // Update q1 = 2**p/|nc|.
r1 = 2*r1; // Update r1 = rem(2**p, |nc|).
if (r1 >= anc) { // (Must be an unsigned
q1 = q1 + 1; // comparison here).
r1 = r1 - anc;
}
q2 = 2*q2; // Update q2 = 2**p/|d|.
r2 = 2*r2; // Update r2 = rem(2**p, |d|).
if (r2 >= ad) { // (Must be an unsigned
q2 = q2 + 1; // comparison here).
r2 = r2 - ad;
}
delta = ad - r2;
} while (q1 < delta || (q1 == delta && r1 == 0));
M = q2 + 1;
if (d < 0) M = -M; // Magic number and
s = p - 32; // shift amount to return.
return true;
}
//--------------------------transform_int_divide-------------------------------
// Convert a division by constant divisor into an alternate Ideal graph.
// Return null if no transformation occurs.
static Node *transform_int_divide( PhaseGVN *phase, Node *dividend, jint divisor ) {
// Check for invalid divisors
assert( divisor != 0 && divisor != min_jint,
"bad divisor for transforming to long multiply" );
bool d_pos = divisor >= 0;
jint d = d_pos ? divisor : -divisor;
const int N = 32;
// Result
Node *q = nullptr;
if (d == 1) {
// division by +/- 1
if (!d_pos) {
// Just negate the value
q = new SubINode(phase->intcon(0), dividend);
}
} else if ( is_power_of_2(d) ) {
// division by +/- a power of 2
// See if we can simply do a shift without rounding
bool needs_rounding = true;
const Type *dt = phase->type(dividend);
const TypeInt *dti = dt->isa_int();
if (dti && dti->_lo >= 0) {
// we don't need to round a positive dividend
needs_rounding = false;
} else if( dividend->Opcode() == Op_AndI ) {
// An AND mask of sufficient size clears the low bits and
// I can avoid rounding.
const TypeInt *andconi_t = phase->type( dividend->in(2) )->isa_int();
if( andconi_t && andconi_t->is_con() ) {
jint andconi = andconi_t->get_con();
if( andconi < 0 && is_power_of_2(-andconi) && (-andconi) >= d ) {
if( (-andconi) == d ) // Remove AND if it clears bits which will be shifted
dividend = dividend->in(1);
needs_rounding = false;
}
}
}
// Add rounding to the shift to handle the sign bit
int l = log2i_graceful(d - 1) + 1;
if (needs_rounding) {
// Divide-by-power-of-2 can be made into a shift, but you have to do
// more math for the rounding. You need to add 0 for positive
// numbers, and "i-1" for negative numbers. Example: i=4, so the
// shift is by 2. You need to add 3 to negative dividends and 0 to
// positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
// (-2+3)>>2 becomes 0, etc.
// Compute 0 or -1, based on sign bit
Node *sign = phase->transform(new RShiftINode(dividend, phase->intcon(N - 1)));
// Mask sign bit to the low sign bits
Node *round = phase->transform(new URShiftINode(sign, phase->intcon(N - l)));
// Round up before shifting
dividend = phase->transform(new AddINode(dividend, round));
}
// Shift for division
q = new RShiftINode(dividend, phase->intcon(l));
if (!d_pos) {
q = new SubINode(phase->intcon(0), phase->transform(q));
}
} else {
// Attempt the jint constant divide -> multiply transform found in
// "Division by Invariant Integers using Multiplication"
// by Granlund and Montgomery
// See also "Hacker's Delight", chapter 10 by Warren.
jint magic_const;
jint shift_const;
if (magic_int_divide_constants(d, magic_const, shift_const)) {
Node *magic = phase->longcon(magic_const);
Node *dividend_long = phase->transform(new ConvI2LNode(dividend));
// Compute the high half of the dividend x magic multiplication
Node *mul_hi = phase->transform(new MulLNode(dividend_long, magic));
if (magic_const < 0) {
mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N)));
mul_hi = phase->transform(new ConvL2INode(mul_hi));
// The magic multiplier is too large for a 32 bit constant. We've adjusted
// it down by 2^32, but have to add 1 dividend back in after the multiplication.
// This handles the "overflow" case described by Granlund and Montgomery.
mul_hi = phase->transform(new AddINode(dividend, mul_hi));
// Shift over the (adjusted) mulhi
if (shift_const != 0) {
mul_hi = phase->transform(new RShiftINode(mul_hi, phase->intcon(shift_const)));
}
} else {
// No add is required, we can merge the shifts together.
mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(N + shift_const)));
mul_hi = phase->transform(new ConvL2INode(mul_hi));
}
// Get a 0 or -1 from the sign of the dividend.
Node *addend0 = mul_hi;
Node *addend1 = phase->transform(new RShiftINode(dividend, phase->intcon(N-1)));
// If the divisor is negative, swap the order of the input addends;
// this has the effect of negating the quotient.
if (!d_pos) {
Node *temp = addend0; addend0 = addend1; addend1 = temp;
}
// Adjust the final quotient by subtracting -1 (adding 1)
// from the mul_hi.
q = new SubINode(addend0, addend1);
}
}
return q;
}
//---------------------magic_long_divide_constants-----------------------------
// Compute magic multiplier and shift constant for converting a 64 bit divide
// by constant into a multiply/shift/add series. Return false if calculations
// fail.
//
// Borrowed almost verbatim from Hacker's Delight by Henry S. Warren, Jr. with
// minor type name and parameter changes. Adjusted to 64 bit word width.
static bool magic_long_divide_constants(jlong d, jlong &M, jint &s) {
int64_t p;
uint64_t ad, anc, delta, q1, r1, q2, r2, t;
const uint64_t two63 = UCONST64(0x8000000000000000); // 2**63.
ad = ABS(d);
if (d == 0 || d == 1) return false;
t = two63 + ((uint64_t)d >> 63);
anc = t - 1 - t%ad; // Absolute value of nc.
p = 63; // Init. p.
q1 = two63/anc; // Init. q1 = 2**p/|nc|.
r1 = two63 - q1*anc; // Init. r1 = rem(2**p, |nc|).
q2 = two63/ad; // Init. q2 = 2**p/|d|.
r2 = two63 - q2*ad; // Init. r2 = rem(2**p, |d|).
do {
p = p + 1;
q1 = 2*q1; // Update q1 = 2**p/|nc|.
r1 = 2*r1; // Update r1 = rem(2**p, |nc|).
if (r1 >= anc) { // (Must be an unsigned
q1 = q1 + 1; // comparison here).
r1 = r1 - anc;
}
q2 = 2*q2; // Update q2 = 2**p/|d|.
r2 = 2*r2; // Update r2 = rem(2**p, |d|).
if (r2 >= ad) { // (Must be an unsigned
q2 = q2 + 1; // comparison here).
r2 = r2 - ad;
}
delta = ad - r2;
} while (q1 < delta || (q1 == delta && r1 == 0));
M = q2 + 1;
if (d < 0) M = -M; // Magic number and
s = p - 64; // shift amount to return.
return true;
}
//---------------------long_by_long_mulhi--------------------------------------
// Generate ideal node graph for upper half of a 64 bit x 64 bit multiplication
static Node* long_by_long_mulhi(PhaseGVN* phase, Node* dividend, jlong magic_const) {
// If the architecture supports a 64x64 mulhi, there is
// no need to synthesize it in ideal nodes.
if (Matcher::has_match_rule(Op_MulHiL)) {
Node* v = phase->longcon(magic_const);
return new MulHiLNode(dividend, v);
}
// Taken from Hacker's Delight, Fig. 8-2. Multiply high signed.
//
// int mulhs(int u, int v) {
// unsigned u0, v0, w0;
// int u1, v1, w1, w2, t;
//
// u0 = u & 0xFFFF; u1 = u >> 16;
// v0 = v & 0xFFFF; v1 = v >> 16;
// w0 = u0*v0;
// t = u1*v0 + (w0 >> 16);
// w1 = t & 0xFFFF;
// w2 = t >> 16;
// w1 = u0*v1 + w1;
// return u1*v1 + w2 + (w1 >> 16);
// }
//
// Note: The version above is for 32x32 multiplications, while the
// following inline comments are adapted to 64x64.
const int N = 64;
// Dummy node to keep intermediate nodes alive during construction
Node* hook = new Node(4);
// u0 = u & 0xFFFFFFFF; u1 = u >> 32;
Node* u0 = phase->transform(new AndLNode(dividend, phase->longcon(0xFFFFFFFF)));
Node* u1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N / 2)));
hook->init_req(0, u0);
hook->init_req(1, u1);
// v0 = v & 0xFFFFFFFF; v1 = v >> 32;
Node* v0 = phase->longcon(magic_const & 0xFFFFFFFF);
Node* v1 = phase->longcon(magic_const >> (N / 2));
// w0 = u0*v0;
Node* w0 = phase->transform(new MulLNode(u0, v0));
// t = u1*v0 + (w0 >> 32);
Node* u1v0 = phase->transform(new MulLNode(u1, v0));
Node* temp = phase->transform(new URShiftLNode(w0, phase->intcon(N / 2)));
Node* t = phase->transform(new AddLNode(u1v0, temp));
hook->init_req(2, t);
// w1 = t & 0xFFFFFFFF;
Node* w1 = phase->transform(new AndLNode(t, phase->longcon(0xFFFFFFFF)));
hook->init_req(3, w1);
// w2 = t >> 32;
Node* w2 = phase->transform(new RShiftLNode(t, phase->intcon(N / 2)));
// w1 = u0*v1 + w1;
Node* u0v1 = phase->transform(new MulLNode(u0, v1));
w1 = phase->transform(new AddLNode(u0v1, w1));
// return u1*v1 + w2 + (w1 >> 32);
Node* u1v1 = phase->transform(new MulLNode(u1, v1));
Node* temp1 = phase->transform(new AddLNode(u1v1, w2));
Node* temp2 = phase->transform(new RShiftLNode(w1, phase->intcon(N / 2)));
// Remove the bogus extra edges used to keep things alive
hook->destruct(phase);
return new AddLNode(temp1, temp2);
}
//--------------------------transform_long_divide------------------------------
// Convert a division by constant divisor into an alternate Ideal graph.
// Return null if no transformation occurs.
static Node *transform_long_divide( PhaseGVN *phase, Node *dividend, jlong divisor ) {
// Check for invalid divisors
assert( divisor != 0L && divisor != min_jlong,
"bad divisor for transforming to long multiply" );
bool d_pos = divisor >= 0;
jlong d = d_pos ? divisor : -divisor;
const int N = 64;
// Result
Node *q = nullptr;
if (d == 1) {
// division by +/- 1
if (!d_pos) {
// Just negate the value
q = new SubLNode(phase->longcon(0), dividend);
}
} else if ( is_power_of_2(d) ) {
// division by +/- a power of 2
// See if we can simply do a shift without rounding
bool needs_rounding = true;
const Type *dt = phase->type(dividend);
const TypeLong *dtl = dt->isa_long();
if (dtl && dtl->_lo > 0) {
// we don't need to round a positive dividend
needs_rounding = false;
} else if( dividend->Opcode() == Op_AndL ) {
// An AND mask of sufficient size clears the low bits and
// I can avoid rounding.
const TypeLong *andconl_t = phase->type( dividend->in(2) )->isa_long();
if( andconl_t && andconl_t->is_con() ) {
jlong andconl = andconl_t->get_con();
if( andconl < 0 && is_power_of_2(-andconl) && (-andconl) >= d ) {
if( (-andconl) == d ) // Remove AND if it clears bits which will be shifted
dividend = dividend->in(1);
needs_rounding = false;
}
}
}
// Add rounding to the shift to handle the sign bit
int l = log2i_graceful(d - 1) + 1;
if (needs_rounding) {
// Divide-by-power-of-2 can be made into a shift, but you have to do
// more math for the rounding. You need to add 0 for positive
// numbers, and "i-1" for negative numbers. Example: i=4, so the
// shift is by 2. You need to add 3 to negative dividends and 0 to
// positive ones. So (-7+3)>>2 becomes -1, (-4+3)>>2 becomes -1,
// (-2+3)>>2 becomes 0, etc.
// Compute 0 or -1, based on sign bit
Node *sign = phase->transform(new RShiftLNode(dividend, phase->intcon(N - 1)));
// Mask sign bit to the low sign bits
Node *round = phase->transform(new URShiftLNode(sign, phase->intcon(N - l)));
// Round up before shifting
dividend = phase->transform(new AddLNode(dividend, round));
}
// Shift for division
q = new RShiftLNode(dividend, phase->intcon(l));
if (!d_pos) {
q = new SubLNode(phase->longcon(0), phase->transform(q));
}
} else if ( !Matcher::use_asm_for_ldiv_by_con(d) ) { // Use hardware DIV instruction when
// it is faster than code generated below.
// Attempt the jlong constant divide -> multiply transform found in
// "Division by Invariant Integers using Multiplication"
// by Granlund and Montgomery
// See also "Hacker's Delight", chapter 10 by Warren.
jlong magic_const;
jint shift_const;
if (magic_long_divide_constants(d, magic_const, shift_const)) {
// Compute the high half of the dividend x magic multiplication
Node *mul_hi = phase->transform(long_by_long_mulhi(phase, dividend, magic_const));
// The high half of the 128-bit multiply is computed.
if (magic_const < 0) {
// The magic multiplier is too large for a 64 bit constant. We've adjusted
// it down by 2^64, but have to add 1 dividend back in after the multiplication.
// This handles the "overflow" case described by Granlund and Montgomery.
mul_hi = phase->transform(new AddLNode(dividend, mul_hi));
}
// Shift over the (adjusted) mulhi
if (shift_const != 0) {
mul_hi = phase->transform(new RShiftLNode(mul_hi, phase->intcon(shift_const)));
}
// Get a 0 or -1 from the sign of the dividend.
Node *addend0 = mul_hi;
Node *addend1 = phase->transform(new RShiftLNode(dividend, phase->intcon(N-1)));
// If the divisor is negative, swap the order of the input addends;
// this has the effect of negating the quotient.
if (!d_pos) {
Node *temp = addend0; addend0 = addend1; addend1 = temp;
}
// Adjust the final quotient by subtracting -1 (adding 1)
// from the mul_hi.
q = new SubLNode(addend0, addend1);
}
}
return q;
}
template <typename TypeClass, typename Unsigned>
Node* unsigned_div_ideal(PhaseGVN* phase, bool can_reshape, Node* div) {
// Check for dead control input
if (div->in(0) != nullptr && div->remove_dead_region(phase, can_reshape)) {
return div;
}
// Don't bother trying to transform a dead node
if (div->in(0) != nullptr && div->in(0)->is_top()) {
return nullptr;
}
const Type* t = phase->type(div->in(2));
if (t == Type::TOP) {
return nullptr;
}
const TypeClass* type_divisor = t->cast<TypeClass>();
// Check for useless control input
// Check for excluding div-zero case
if (div->in(0) != nullptr && (type_divisor->_hi < 0 || type_divisor->_lo > 0)) {
div->set_req(0, nullptr); // Yank control input
return div;
}
if (!type_divisor->is_con()) {
return nullptr;
}
Unsigned divisor = static_cast<Unsigned>(type_divisor->get_con()); // Get divisor
if (divisor == 0 || divisor == 1) {
return nullptr; // Dividing by zero constant does not idealize
}
if (is_power_of_2(divisor)) {
return make_urshift<TypeClass>(div->in(1), phase->intcon(log2i_graceful(divisor)));
}
return nullptr;
}
//=============================================================================
//------------------------------Identity---------------------------------------
// If the divisor is 1, we are an identity on the dividend.
Node* DivINode::Identity(PhaseGVN* phase) {
return (phase->type( in(2) )->higher_equal(TypeInt::ONE)) ? in(1) : this;
}
//------------------------------Idealize---------------------------------------
// Divides can be changed to multiplies and/or shifts
Node *DivINode::Ideal(PhaseGVN *phase, bool can_reshape) {
if (in(0) && remove_dead_region(phase, can_reshape)) return this;
// Don't bother trying to transform a dead node
if( in(0) && in(0)->is_top() ) return nullptr;
const Type *t = phase->type( in(2) );
if( t == TypeInt::ONE ) // Identity?
return nullptr; // Skip it
const TypeInt *ti = t->isa_int();
if( !ti ) return nullptr;
// Check for useless control input
// Check for excluding div-zero case
if (in(0) && (ti->_hi < 0 || ti->_lo > 0)) {
set_req(0, nullptr); // Yank control input
return this;
}
if( !ti->is_con() ) return nullptr;
jint i = ti->get_con(); // Get divisor
if (i == 0) return nullptr; // Dividing by zero constant does not idealize
// Dividing by MININT does not optimize as a power-of-2 shift.
if( i == min_jint ) return nullptr;
return transform_int_divide( phase, in(1), i );
}
//------------------------------Value------------------------------------------
// A DivINode divides its inputs. The third input is a Control input, used to
// prevent hoisting the divide above an unsafe test.
const Type* DivINode::Value(PhaseGVN* phase) const {
// Either input is TOP ==> the result is TOP
const Type *t1 = phase->type( in(1) );
const Type *t2 = phase->type( in(2) );
if( t1 == Type::TOP ) return Type::TOP;
if( t2 == Type::TOP ) return Type::TOP;
// x/x == 1 since we always generate the dynamic divisor check for 0.
if (in(1) == in(2)) {
return TypeInt::ONE;
}
// Either input is BOTTOM ==> the result is the local BOTTOM
const Type *bot = bottom_type();
if( (t1 == bot) || (t2 == bot) ||
(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
return bot;
// Divide the two numbers. We approximate.
// If divisor is a constant and not zero
const TypeInt *i1 = t1->is_int();
const TypeInt *i2 = t2->is_int();
int widen = MAX2(i1->_widen, i2->_widen);
if( i2->is_con() && i2->get_con() != 0 ) {
int32_t d = i2->get_con(); // Divisor
jint lo, hi;
if( d >= 0 ) {
lo = i1->_lo/d;
hi = i1->_hi/d;
} else {
if( d == -1 && i1->_lo == min_jint ) {
// 'min_jint/-1' throws arithmetic exception during compilation
lo = min_jint;
// do not support holes, 'hi' must go to either min_jint or max_jint:
// [min_jint, -10]/[-1,-1] ==> [min_jint] UNION [10,max_jint]
hi = i1->_hi == min_jint ? min_jint : max_jint;
} else {
lo = i1->_hi/d;
hi = i1->_lo/d;
}
}
return TypeInt::make(lo, hi, widen);
}
// If the dividend is a constant
if( i1->is_con() ) {
int32_t d = i1->get_con();
if( d < 0 ) {
if( d == min_jint ) {
// (-min_jint) == min_jint == (min_jint / -1)
return TypeInt::make(min_jint, max_jint/2 + 1, widen);
} else {
return TypeInt::make(d, -d, widen);
}
}
return TypeInt::make(-d, d, widen);
}
// Otherwise we give up all hope
return TypeInt::INT;
}
//=============================================================================
//------------------------------Identity---------------------------------------
// If the divisor is 1, we are an identity on the dividend.
Node* DivLNode::Identity(PhaseGVN* phase) {
return (phase->type( in(2) )->higher_equal(TypeLong::ONE)) ? in(1) : this;
}
//------------------------------Idealize---------------------------------------
// Dividing by a power of 2 is a shift.
Node *DivLNode::Ideal( PhaseGVN *phase, bool can_reshape) {
if (in(0) && remove_dead_region(phase, can_reshape)) return this;
// Don't bother trying to transform a dead node
if( in(0) && in(0)->is_top() ) return nullptr;
const Type *t = phase->type( in(2) );
if( t == TypeLong::ONE ) // Identity?
return nullptr; // Skip it
const TypeLong *tl = t->isa_long();
if( !tl ) return nullptr;
// Check for useless control input
// Check for excluding div-zero case
if (in(0) && (tl->_hi < 0 || tl->_lo > 0)) {
set_req(0, nullptr); // Yank control input
return this;
}
if( !tl->is_con() ) return nullptr;
jlong l = tl->get_con(); // Get divisor
if (l == 0) return nullptr; // Dividing by zero constant does not idealize
// Dividing by MINLONG does not optimize as a power-of-2 shift.
if( l == min_jlong ) return nullptr;
return transform_long_divide( phase, in(1), l );
}
//------------------------------Value------------------------------------------
// A DivLNode divides its inputs. The third input is a Control input, used to
// prevent hoisting the divide above an unsafe test.
const Type* DivLNode::Value(PhaseGVN* phase) const {
// Either input is TOP ==> the result is TOP
const Type *t1 = phase->type( in(1) );
const Type *t2 = phase->type( in(2) );
if( t1 == Type::TOP ) return Type::TOP;
if( t2 == Type::TOP ) return Type::TOP;
// x/x == 1 since we always generate the dynamic divisor check for 0.
if (in(1) == in(2)) {
return TypeLong::ONE;
}
// Either input is BOTTOM ==> the result is the local BOTTOM
const Type *bot = bottom_type();
if( (t1 == bot) || (t2 == bot) ||
(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
return bot;
// Divide the two numbers. We approximate.
// If divisor is a constant and not zero
const TypeLong *i1 = t1->is_long();
const TypeLong *i2 = t2->is_long();
int widen = MAX2(i1->_widen, i2->_widen);
if( i2->is_con() && i2->get_con() != 0 ) {
jlong d = i2->get_con(); // Divisor
jlong lo, hi;
if( d >= 0 ) {
lo = i1->_lo/d;
hi = i1->_hi/d;
} else {
if( d == CONST64(-1) && i1->_lo == min_jlong ) {
// 'min_jlong/-1' throws arithmetic exception during compilation
lo = min_jlong;
// do not support holes, 'hi' must go to either min_jlong or max_jlong:
// [min_jlong, -10]/[-1,-1] ==> [min_jlong] UNION [10,max_jlong]
hi = i1->_hi == min_jlong ? min_jlong : max_jlong;
} else {
lo = i1->_hi/d;
hi = i1->_lo/d;
}
}
return TypeLong::make(lo, hi, widen);
}
// If the dividend is a constant
if( i1->is_con() ) {
jlong d = i1->get_con();
if( d < 0 ) {
if( d == min_jlong ) {
// (-min_jlong) == min_jlong == (min_jlong / -1)
return TypeLong::make(min_jlong, max_jlong/2 + 1, widen);
} else {
return TypeLong::make(d, -d, widen);
}
}
return TypeLong::make(-d, d, widen);
}
// Otherwise we give up all hope
return TypeLong::LONG;
}
//=============================================================================
//------------------------------Value------------------------------------------
// An DivFNode divides its inputs. The third input is a Control input, used to
// prevent hoisting the divide above an unsafe test.
const Type* DivFNode::Value(PhaseGVN* phase) const {
// Either input is TOP ==> the result is TOP
const Type *t1 = phase->type( in(1) );
const Type *t2 = phase->type( in(2) );
if( t1 == Type::TOP ) return Type::TOP;
if( t2 == Type::TOP ) return Type::TOP;
// Either input is BOTTOM ==> the result is the local BOTTOM
const Type *bot = bottom_type();
if( (t1 == bot) || (t2 == bot) ||
(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
return bot;
// x/x == 1, we ignore 0/0.
// Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
// Does not work for variables because of NaN's
if (in(1) == in(2) && t1->base() == Type::FloatCon &&
!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) { // could be negative ZERO or NaN
return TypeF::ONE;
}
if( t2 == TypeF::ONE )
return t1;
// If divisor is a constant and not zero, divide them numbers
if( t1->base() == Type::FloatCon &&
t2->base() == Type::FloatCon &&
t2->getf() != 0.0 ) // could be negative zero
return TypeF::make( t1->getf()/t2->getf() );
// If the dividend is a constant zero
// Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
// Test TypeF::ZERO is not sufficient as it could be negative zero
if( t1 == TypeF::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0 )
return TypeF::ZERO;
// Otherwise we give up all hope
return Type::FLOAT;
}
//------------------------------isA_Copy---------------------------------------
// Dividing by self is 1.
// If the divisor is 1, we are an identity on the dividend.
Node* DivFNode::Identity(PhaseGVN* phase) {
return (phase->type( in(2) ) == TypeF::ONE) ? in(1) : this;
}
//------------------------------Idealize---------------------------------------
Node *DivFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
if (in(0) && remove_dead_region(phase, can_reshape)) return this;
// Don't bother trying to transform a dead node
if( in(0) && in(0)->is_top() ) return nullptr;
const Type *t2 = phase->type( in(2) );
if( t2 == TypeF::ONE ) // Identity?
return nullptr; // Skip it
const TypeF *tf = t2->isa_float_constant();
if( !tf ) return nullptr;
if( tf->base() != Type::FloatCon ) return nullptr;
// Check for out of range values
if( tf->is_nan() || !tf->is_finite() ) return nullptr;
// Get the value
float f = tf->getf();
int exp;
// Only for special case of dividing by a power of 2
if( frexp((double)f, &exp) != 0.5 ) return nullptr;
// Limit the range of acceptable exponents
if( exp < -126 || exp > 126 ) return nullptr;
// Compute the reciprocal
float reciprocal = ((float)1.0) / f;
assert( frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2" );
// return multiplication by the reciprocal
return (new MulFNode(in(1), phase->makecon(TypeF::make(reciprocal))));
}
//=============================================================================
//------------------------------Value------------------------------------------
// An DivHFNode divides its inputs. The third input is a Control input, used to
// prevent hoisting the divide above an unsafe test.
const Type* DivHFNode::Value(PhaseGVN* phase) const {
// Either input is TOP ==> the result is TOP
const Type* t1 = phase->type(in(1));
const Type* t2 = phase->type(in(2));
if(t1 == Type::TOP) { return Type::TOP; }
if(t2 == Type::TOP) { return Type::TOP; }
// Either input is BOTTOM ==> the result is the local BOTTOM
const Type* bot = bottom_type();
if((t1 == bot) || (t2 == bot) ||
(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM)) {
return bot;
}
// x/x == 1, we ignore 0/0.
// Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
// Does not work for variables because of NaN's
if (in(1) == in(2) && t1->base() == Type::HalfFloatCon &&
!g_isnan(t1->getf()) && g_isfinite(t1->getf()) && t1->getf() != 0.0) { // could be negative ZERO or NaN
return TypeH::ONE;
}
if (t2 == TypeH::ONE) {
return t1;
}
// If divisor is a constant and not zero, divide the numbers
if (t1->base() == Type::HalfFloatCon &&
t2->base() == Type::HalfFloatCon &&
t2->getf() != 0.0) {
// could be negative zero
return TypeH::make(t1->getf() / t2->getf());
}
// If the dividend is a constant zero
// Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
// Test TypeHF::ZERO is not sufficient as it could be negative zero
if (t1 == TypeH::ZERO && !g_isnan(t2->getf()) && t2->getf() != 0.0) {
return TypeH::ZERO;
}
// If divisor or dividend is nan then result is nan.
if (g_isnan(t1->getf()) || g_isnan(t2->getf())) {
return TypeH::make(NAN);
}
// Otherwise we give up all hope
return Type::HALF_FLOAT;
}
//-----------------------------------------------------------------------------
// Dividing by self is 1.
// IF the divisor is 1, we are an identity on the dividend.
Node* DivHFNode::Identity(PhaseGVN* phase) {
return (phase->type( in(2) ) == TypeH::ONE) ? in(1) : this;
}
//------------------------------Idealize---------------------------------------
Node* DivHFNode::Ideal(PhaseGVN* phase, bool can_reshape) {
if (in(0) != nullptr && remove_dead_region(phase, can_reshape)) return this;
// Don't bother trying to transform a dead node
if (in(0) != nullptr && in(0)->is_top()) { return nullptr; }
const Type* t2 = phase->type(in(2));
if (t2 == TypeH::ONE) { // Identity?
return nullptr; // Skip it
}
const TypeH* tf = t2->isa_half_float_constant();
if(tf == nullptr) { return nullptr; }
if(tf->base() != Type::HalfFloatCon) { return nullptr; }
// Check for out of range values
if(tf->is_nan() || !tf->is_finite()) { return nullptr; }
// Get the value
float f = tf->getf();
int exp;
// Consider the following geometric progression series of POT(power of two) numbers.
// 0.5 x 2^0 = 0.5, 0.5 x 2^1 = 1.0, 0.5 x 2^2 = 2.0, 0.5 x 2^3 = 4.0 ... 0.5 x 2^n,
// In all the above cases, normalized mantissa returned by frexp routine will
// be exactly equal to 0.5 while exponent will be 0,1,2,3...n
// Perform division to multiplication transform only if divisor is a POT value.
if(frexp((double)f, &exp) != 0.5) { return nullptr; }
// Limit the range of acceptable exponents
if(exp < -14 || exp > 15) { return nullptr; }
// Since divisor is a POT number, hence its reciprocal will never
// overflow 11 bits precision range of Float16
// value if exponent returned by frexp routine strictly lie
// within the exponent range of normal min(0x1.0P-14) and
// normal max(0x1.ffcP+15) values.
// Thus we can safely compute the reciprocal of divisor without
// any concerns about the precision loss and transform the division
// into a multiplication operation.
float reciprocal = ((float)1.0) / f;
assert(frexp((double)reciprocal, &exp) == 0.5, "reciprocal should be power of 2");
// return multiplication by the reciprocal
return (new MulHFNode(in(1), phase->makecon(TypeH::make(reciprocal))));
}
//=============================================================================
//------------------------------Value------------------------------------------
// An DivDNode divides its inputs. The third input is a Control input, used to
// prevent hoisting the divide above an unsafe test.
const Type* DivDNode::Value(PhaseGVN* phase) const {
// Either input is TOP ==> the result is TOP
const Type *t1 = phase->type( in(1) );
const Type *t2 = phase->type( in(2) );
if( t1 == Type::TOP ) return Type::TOP;
if( t2 == Type::TOP ) return Type::TOP;
// Either input is BOTTOM ==> the result is the local BOTTOM
const Type *bot = bottom_type();
if( (t1 == bot) || (t2 == bot) ||
(t1 == Type::BOTTOM) || (t2 == Type::BOTTOM) )
return bot;
// x/x == 1, we ignore 0/0.
// Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
// Does not work for variables because of NaN's
if (in(1) == in(2) && t1->base() == Type::DoubleCon &&
!g_isnan(t1->getd()) && g_isfinite(t1->getd()) && t1->getd() != 0.0) { // could be negative ZERO or NaN
return TypeD::ONE;
}
if( t2 == TypeD::ONE )
return t1;
// IA32 would only execute this for non-strict FP, which is never the
// case now.
#if ! defined(IA32)
// If divisor is a constant and not zero, divide them numbers
if( t1->base() == Type::DoubleCon &&
t2->base() == Type::DoubleCon &&
t2->getd() != 0.0 ) // could be negative zero
return TypeD::make( t1->getd()/t2->getd() );
#endif
// If the dividend is a constant zero
// Note: if t1 and t2 are zero then result is NaN (JVMS page 213)
// Test TypeF::ZERO is not sufficient as it could be negative zero
if( t1 == TypeD::ZERO && !g_isnan(t2->getd()) && t2->getd() != 0.0 )
return TypeD::ZERO;
// Otherwise we give up all hope
return Type::DOUBLE;
}
//------------------------------isA_Copy---------------------------------------
// Dividing by self is 1.
// If the divisor is 1, we are an identity on the dividend.
Node* DivDNode::Identity(PhaseGVN* phase) {
return (phase->type( in(2) ) == TypeD::ONE) ? in(1) : this;
}
//------------------------------Idealize---------------------------------------
Node *DivDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
if (in(0) && remove_dead_region(phase, can_reshape)) return this;
// Don't bother trying to transform a dead node
if( in(0) && in(0)->is_top() ) return nullptr;
const Type *t2 = phase->type( in(2) );
if( t2 == TypeD::ONE ) // Identity?
return nullptr; // Skip it
const TypeD *td = t2->isa_double_constant();
if( !td ) return nullptr;
if( td->base() != Type::DoubleCon ) return nullptr;
// Check for out of range values
if( td->is_nan() || !td->is_finite() ) return nullptr;
// Get the value
double d = td->getd();
int exp;
// Only for special case of dividing by a power of 2
if( frexp(d, &exp) != 0.5 ) return nullptr;
// Limit the range of acceptable exponents
if( exp < -1021 || exp > 1022 ) return nullptr;