25
#include "precompiled.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "opto/addnode.hpp"
28
#include "opto/castnode.hpp"
29
#include "opto/cfgnode.hpp"
30
#include "opto/connode.hpp"
31
#include "opto/machnode.hpp"
32
#include "opto/movenode.hpp"
33
#include "opto/mulnode.hpp"
34
#include "opto/phaseX.hpp"
35
#include "opto/subnode.hpp"
50
uint AddNode::hash() const {
51
return (uintptr_t)in(1) + (uintptr_t)in(2) + Opcode();
56
Node* AddNode::Identity(PhaseGVN* phase) {
57
const Type *zero = add_id();
58
if( phase->type( in(1) )->higher_equal( zero ) ) return in(2);
59
if( phase->type( in(2) )->higher_equal( zero ) ) return in(1);
65
static bool commute(PhaseGVN* phase, Node* add) {
66
Node *in1 = add->in(1);
67
Node *in2 = add->in(2);
70
if ((in1->Opcode() == add->as_Add()->max_opcode() && in2->Opcode() == add->as_Add()->min_opcode())
71
|| (in1->Opcode() == add->as_Add()->min_opcode() && in2->Opcode() == add->as_Add()->max_opcode())) {
72
Node *in11 = in1->in(1);
73
Node *in12 = in1->in(2);
75
Node *in21 = in2->in(1);
76
Node *in22 = in2->in(2);
78
if ((in11 == in21 && in12 == in22) ||
79
(in11 == in22 && in12 == in21)) {
80
add->set_req_X(1, in11, phase);
81
add->set_req_X(2, in12, phase);
86
bool con_left = phase->type(in1)->singleton();
87
bool con_right = phase->type(in2)->singleton();
91
if( con_right ) return false;
94
add->swap_edges(1, 2);
100
if (in2->is_Load()) {
101
if (!in1->is_Load()) {
106
} else if( in1->is_Load() ) {
108
add->swap_edges(1, 2);
114
if (in1->is_Phi() && (phi = in1->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add)
116
if (in2->is_Phi() && (phi = in2->as_Phi()) && phi->region()->is_Loop() && phi->in(2) == add) {
117
add->swap_edges(1, 2);
122
if( in1->_idx > in2->_idx ) {
123
add->swap_edges(1, 2);
131
Node *AddNode::Ideal(PhaseGVN *phase, bool can_reshape) {
132
const Type *t1 = phase->type(in(1));
133
const Type *t2 = phase->type(in(2));
134
bool con_left = t1->singleton();
135
bool con_right = t2->singleton();
138
if (commute(phase, this)) return this;
140
AddNode *progress = nullptr;
147
int add1_op = add1->Opcode();
148
int this_op = Opcode();
149
if (con_right && t2 != Type::TOP &&
150
add1_op == this_op) {
153
const Type *t12 = phase->type(add1->in(2));
154
if (t12->singleton() && t12 != Type::TOP) {
158
Node *add11 = add1->in(1);
159
int add11_op = add11->Opcode();
160
if ((add1 == add1->in(1))
161
|| (add11_op == this_op && add11->in(1) == add1)) {
162
assert(false, "dead loop in AddNode::Ideal");
166
Node *x1 = add1->in(1);
167
Node *x2 = phase->makecon(add1->as_Add()->add_ring(t2, t12));
168
set_req_X(2, x2, phase);
169
set_req_X(1, x1, phase);
172
add1_op = add1->Opcode();
177
if (add1_op == this_op && !con_right) {
178
Node *a12 = add1->in(2);
179
const Type *t12 = phase->type( a12 );
180
if (t12->singleton() && t12 != Type::TOP && (add1 != add1->in(1)) &&
181
!(add1->in(1)->is_Phi() && (add1->in(1)->as_Phi()->is_tripcount(T_INT) || add1->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
182
assert(add1->in(1) != this, "dead loop in AddNode::Ideal");
183
add2 = add1->clone();
184
add2->set_req(2, in(2));
185
add2 = phase->transform(add2);
186
set_req_X(1, add2, phase);
187
set_req_X(2, a12, phase);
194
int add2_op = add2->Opcode();
195
if (add2_op == this_op && !con_left) {
196
Node *a22 = add2->in(2);
197
const Type *t22 = phase->type( a22 );
198
if (t22->singleton() && t22 != Type::TOP && (add2 != add2->in(1)) &&
199
!(add2->in(1)->is_Phi() && (add2->in(1)->as_Phi()->is_tripcount(T_INT) || add2->in(1)->as_Phi()->is_tripcount(T_LONG)))) {
200
assert(add2->in(1) != this, "dead loop in AddNode::Ideal");
201
Node *addx = add2->clone();
202
addx->set_req(1, in(1));
203
addx->set_req(2, add2->in(1));
204
addx = phase->transform(addx);
205
set_req_X(1, addx, phase);
206
set_req_X(2, a22, phase);
217
const Type* AddNode::Value(PhaseGVN* phase) const {
219
const Type* t1 = phase->type(in(1));
220
const Type* t2 = phase->type(in(2));
221
if (t1 == Type::TOP || t2 == Type::TOP) {
226
const Type* tadd = add_of_identity(t1, t2);
227
if (tadd != nullptr) {
231
return add_ring(t1, t2);
236
const Type *AddNode::add_of_identity( const Type *t1, const Type *t2 ) const {
237
const Type *zero = add_id();
238
if( t1->higher_equal( zero ) ) return t2;
239
if( t2->higher_equal( zero ) ) return t1;
244
AddNode* AddNode::make(Node* in1, Node* in2, BasicType bt) {
247
return new AddINode(in1, in2);
249
return new AddLNode(in1, in2);
251
fatal("Not implemented for %s", type2name(bt));
256
bool AddNode::is_not(PhaseGVN* phase, Node* n, BasicType bt) {
257
return n->Opcode() == Op_Xor(bt) && phase->type(n->in(2)) == TypeInteger::minus_1(bt);
260
AddNode* AddNode::make_not(PhaseGVN* phase, Node* n, BasicType bt) {
263
return new XorINode(n, phase->intcon(-1));
265
return new XorLNode(n, phase->longcon(-1L));
267
fatal("Not implemented for %s", type2name(bt));
274
Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
277
int op1 = in1->Opcode();
278
int op2 = in2->Opcode();
280
if (op1 == Op_Add(bt) && op2 == Op_Sub(bt)) {
287
if (op1 == Op_Sub(bt)) {
288
const Type* t_sub1 = phase->type(in1->in(1));
289
const Type* t_2 = phase->type(in2 );
290
if (t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP) {
291
return SubNode::make(phase->makecon(add_ring(t_sub1, t_2)), in1->in(2), bt);
294
if (op2 == Op_Sub(bt)) {
296
assert( in1->in(2) != this && in2->in(2) != this,
297
"dead loop in AddINode::Ideal" );
298
Node* sub = SubNode::make(nullptr, nullptr, bt);
300
PhaseIterGVN* igvn = phase->is_IterGVN();
306
if (igvn != nullptr) {
307
sub_in1 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(1), in2->in(1), bt));
309
sub_in1 = phase->transform(AddNode::make(in1->in(1), in2->in(1), bt));
312
if (igvn != nullptr) {
313
sub_in2 = igvn->register_new_node_with_optimizer(AddNode::make(in1->in(2), in2->in(2), bt));
315
sub_in2 = phase->transform(AddNode::make(in1->in(2), in2->in(2), bt));
317
sub->init_req(1, sub_in1);
318
sub->init_req(2, sub_in2);
322
if (op2 == Op_Add(bt) && in1->in(2) == in2->in(1)) {
323
assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
324
return AddNode::make(in1->in(1), in2->in(2), bt);
327
if (op2 == Op_Add(bt) && in1->in(2) == in2->in(2)) {
328
assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal/AddLNode::Ideal");
329
return AddNode::make(in1->in(1), in2->in(1), bt);
334
if (op1 == Op_Sub(bt) && in1->in(1)->Opcode() == Op_ConIL(bt)
335
&& in1 != in1->in(2) && !(in1->in(2)->is_Phi() && in1->in(2)->as_Phi()->is_tripcount(bt))) {
336
return AddNode::make(phase->transform(SubNode::make(in2, in1->in(2), bt)), in1->in(1), bt);
340
if (op2 == Op_Sub(bt) && in2->in(1)->Opcode() == Op_ConIL(bt)
341
&& in2 != in2->in(2) && !(in2->in(2)->is_Phi() && in2->in(2)->as_Phi()->is_tripcount(bt))) {
342
return AddNode::make(phase->transform(SubNode::make(in1, in2->in(2), bt)), in2->in(1), bt);
346
if (op1 == Op_Mul(bt) && op2 == Op_Mul(bt)) {
347
Node* add_in1 = nullptr;
348
Node* add_in2 = nullptr;
349
Node* mul_in = nullptr;
351
if (in1->in(1) == in2->in(1)) {
353
add_in1 = in1->in(2);
354
add_in2 = in2->in(2);
356
} else if (in1->in(2) == in2->in(1)) {
358
add_in1 = in1->in(1);
359
add_in2 = in2->in(2);
361
} else if (in1->in(2) == in2->in(2)) {
363
add_in1 = in1->in(1);
364
add_in2 = in2->in(1);
366
} else if (in1->in(1) == in2->in(2)) {
368
add_in1 = in1->in(2);
369
add_in2 = in2->in(1);
373
if (mul_in != nullptr) {
374
Node* add = phase->transform(AddNode::make(add_in1, add_in2, bt));
375
return MulNode::make(mul_in, add, bt);
380
if (Matcher::match_rule_supported(Op_RotateRight) &&
381
((op1 == Op_URShift(bt) && op2 == Op_LShift(bt)) || (op1 == Op_LShift(bt) && op2 == Op_URShift(bt))) &&
382
in1->in(1) != nullptr && in1->in(1) == in2->in(1)) {
383
Node* rshift = op1 == Op_URShift(bt) ? in1->in(2) : in2->in(2);
384
Node* lshift = op1 == Op_URShift(bt) ? in2->in(2) : in1->in(2);
385
if (rshift != nullptr && lshift != nullptr) {
386
const TypeInt* rshift_t = phase->type(rshift)->isa_int();
387
const TypeInt* lshift_t = phase->type(lshift)->isa_int();
388
int bits = bt == T_INT ? 32 : 64;
389
int mask = bt == T_INT ? 0x1F : 0x3F;
390
if (lshift_t != nullptr && lshift_t->is_con() &&
391
rshift_t != nullptr && rshift_t->is_con() &&
392
((lshift_t->get_con() & mask) == (bits - (rshift_t->get_con() & mask)))) {
393
return new RotateRightNode(in1->in(1), phase->intcon(rshift_t->get_con() & mask), TypeInteger::bottom(bt));
398
return AddNode::Ideal(phase, can_reshape);
402
Node* AddINode::Ideal(PhaseGVN* phase, bool can_reshape) {
405
int op1 = in1->Opcode();
406
int op2 = in2->Opcode();
419
if (op1 == Op_URShiftI && op2 == Op_ConI &&
420
in1->in(2)->Opcode() == Op_ConI) {
421
jint z = phase->type(in1->in(2))->is_int()->get_con() & 0x1f;
422
jint y = phase->type(in2)->is_int()->get_con();
424
if (z < 5 && -5 < y && y < 0) {
425
const Type* t_in11 = phase->type(in1->in(1));
426
if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z))) {
427
Node* a = phase->transform(new AddINode( in1->in(1), phase->intcon(y<<z)));
428
return new URShiftINode(a, in1->in(2));
433
return AddNode::IdealIL(phase, can_reshape, T_INT);
439
Node* AddINode::Identity(PhaseGVN* phase) {
440
if (in(1)->Opcode() == Op_SubI && in(1)->in(2) == in(2)) {
442
} else if (in(2)->Opcode() == Op_SubI && in(2)->in(2) == in(1)) {
445
return AddNode::Identity(phase);
453
const Type *AddINode::add_ring( const Type *t0, const Type *t1 ) const {
454
const TypeInt *r0 = t0->is_int();
455
const TypeInt *r1 = t1->is_int();
456
int lo = java_add(r0->_lo, r1->_lo);
457
int hi = java_add(r0->_hi, r1->_hi);
458
if( !(r0->is_con() && r1->is_con()) ) {
460
if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
461
lo = min_jint; hi = max_jint;
463
if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
464
lo = min_jint; hi = max_jint;
467
lo = min_jint; hi = max_jint;
474
return TypeInt::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
480
Node* AddLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
481
return AddNode::IdealIL(phase, can_reshape, T_LONG);
487
Node* AddLNode::Identity(PhaseGVN* phase) {
488
if (in(1)->Opcode() == Op_SubL && in(1)->in(2) == in(2)) {
490
} else if (in(2)->Opcode() == Op_SubL && in(2)->in(2) == in(1)) {
493
return AddNode::Identity(phase);
501
const Type *AddLNode::add_ring( const Type *t0, const Type *t1 ) const {
502
const TypeLong *r0 = t0->is_long();
503
const TypeLong *r1 = t1->is_long();
504
jlong lo = java_add(r0->_lo, r1->_lo);
505
jlong hi = java_add(r0->_hi, r1->_hi);
506
if( !(r0->is_con() && r1->is_con()) ) {
508
if( (r0->_lo & r1->_lo) < 0 && lo >= 0 ) {
509
lo =min_jlong; hi = max_jlong;
511
if( (~(r0->_hi | r1->_hi)) < 0 && hi < 0 ) {
512
lo = min_jlong; hi = max_jlong;
515
lo = min_jlong; hi = max_jlong;
522
return TypeLong::make( lo, hi, MAX2(r0->_widen,r1->_widen) );
529
const Type *AddFNode::add_of_identity( const Type *t1, const Type *t2 ) const {
546
const Type *AddFNode::add_ring( const Type *t0, const Type *t1 ) const {
547
if (!t0->isa_float_constant() || !t1->isa_float_constant()) {
548
return bottom_type();
550
return TypeF::make( t0->getf() + t1->getf() );
554
Node *AddFNode::Ideal(PhaseGVN *phase, bool can_reshape) {
556
return commute(phase, this) ? this : nullptr;
563
const Type *AddDNode::add_of_identity( const Type *t1, const Type *t2 ) const {
579
const Type *AddDNode::add_ring( const Type *t0, const Type *t1 ) const {
580
if (!t0->isa_double_constant() || !t1->isa_double_constant()) {
581
return bottom_type();
583
return TypeD::make( t0->getd() + t1->getd() );
587
Node *AddDNode::Ideal(PhaseGVN *phase, bool can_reshape) {
589
return commute(phase, this) ? this : nullptr;
596
Node* AddPNode::Identity(PhaseGVN* phase) {
597
return ( phase->type( in(Offset) )->higher_equal( TypeX_ZERO ) ) ? in(Address) : this;
601
Node *AddPNode::Ideal(PhaseGVN *phase, bool can_reshape) {
603
if( phase->type( in(Address) ) == Type::TOP ) return nullptr;
606
const Node *n = in(Address);
607
if (n->is_AddP() && n->in(Base) == in(Base)) {
608
const AddPNode *addp = n->as_AddP();
609
assert( !addp->in(Address)->is_AddP() ||
610
addp->in(Address)->as_AddP() != addp,
611
"dead loop in AddPNode::Ideal" );
613
const Type *t = phase->type( addp->in(Offset) );
614
if( t == Type::TOP ) return nullptr;
615
const TypeX *t12 = t->is_intptr_t();
616
if( t12->is_con() ) {
618
const Type *temp_t2 = phase->type( in(Offset) );
619
if( temp_t2 == Type::TOP ) return nullptr;
620
const TypeX *t2 = temp_t2->is_intptr_t();
625
address = addp->in(Address);
626
offset = phase->MakeConX(t2->get_con() + t12->get_con());
629
address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
630
offset = addp->in(Offset);
632
set_req_X(Address, address, phase);
633
set_req_X(Offset, offset, phase);
639
if( in(Base)->bottom_type() == Type::TOP ) {
641
if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
642
Node* offset = in(Offset);
643
return new CastX2PNode(offset);
651
const Node *add = in(Offset);
652
if( add->Opcode() == Op_AddX && add->in(1) != add ) {
653
const Type *t22 = phase->type( add->in(2) );
654
if( t22->singleton() && (t22 != Type::TOP) ) {
655
set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
656
set_req_X(Offset, add->in(2), phase);
666
const Type *AddPNode::bottom_type() const {
667
if (in(Address) == nullptr) return TypePtr::BOTTOM;
668
const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
669
if( !tp ) return Type::TOP;
670
assert( in(Offset)->Opcode() != Op_ConP, "" );
671
const Type *t = in(Offset)->bottom_type();
673
return tp->add_offset(Type::OffsetTop);
674
const TypeX *tx = t->is_intptr_t();
675
intptr_t txoffset = Type::OffsetBot;
677
txoffset = tx->get_con();
679
return tp->add_offset(txoffset);
683
const Type* AddPNode::Value(PhaseGVN* phase) const {
685
const Type *t1 = phase->type( in(Address) );
686
const Type *t2 = phase->type( in(Offset) );
687
if( t1 == Type::TOP ) return Type::TOP;
688
if( t2 == Type::TOP ) return Type::TOP;
691
const TypePtr *p1 = t1->isa_ptr();
693
const TypeX *p2 = t2->is_intptr_t();
695
intptr_t p2offset = Type::OffsetBot;
697
p2offset = p2->get_con();
699
return p1->add_offset(p2offset);
706
Node* AddPNode::Ideal_base_and_offset(Node* ptr, PhaseValues* phase,
709
if (ptr->is_AddP()) {
710
Node* base = ptr->in(AddPNode::Base);
711
Node* addr = ptr->in(AddPNode::Address);
712
Node* offs = ptr->in(AddPNode::Offset);
713
if (base == addr || base->is_top()) {
714
offset = phase->find_intptr_t_con(offs, Type::OffsetBot);
715
if (offset != Type::OffsetBot) {
720
offset = Type::OffsetBot;
727
int AddPNode::unpack_offsets(Node* elements[], int length) const {
729
Node const* addr = this;
730
Node* base = addr->in(AddPNode::Base);
731
while (addr->is_AddP()) {
732
if (addr->in(AddPNode::Base) != base) {
736
elements[count++] = addr->in(AddPNode::Offset);
737
if (count == length) {
741
addr = addr->in(AddPNode::Address);
751
uint AddPNode::match_edge(uint idx) const {
757
Node* OrINode::Identity(PhaseGVN* phase) {
759
if (in(1) == in(2)) {
763
return AddNode::Identity(phase);
767
static Node* rotate_shift(PhaseGVN* phase, Node* lshift, Node* rshift, int mask) {
769
const TypeInt* lshift_t = phase->type(lshift)->isa_int();
770
const TypeInt* rshift_t = phase->type(rshift)->isa_int();
771
if (lshift_t != nullptr && lshift_t->is_con() &&
772
rshift_t != nullptr && rshift_t->is_con() &&
773
((lshift_t->get_con() & mask) == ((mask + 1) - (rshift_t->get_con() & mask)))) {
774
return phase->intcon(lshift_t->get_con() & mask);
777
if (rshift->Opcode() == Op_SubI && rshift->in(2) == lshift && rshift->in(1)->is_Con()){
778
const TypeInt* shift_t = phase->type(rshift->in(1))->isa_int();
779
if (shift_t != nullptr && shift_t->is_con() &&
780
(shift_t->get_con() == 0 || shift_t->get_con() == (mask + 1))) {
787
Node* OrINode::Ideal(PhaseGVN* phase, bool can_reshape) {
788
int lopcode = in(1)->Opcode();
789
int ropcode = in(2)->Opcode();
790
if (Matcher::match_rule_supported(Op_RotateLeft) &&
791
lopcode == Op_LShiftI && ropcode == Op_URShiftI && in(1)->in(1) == in(2)->in(1)) {
792
Node* lshift = in(1)->in(2);
793
Node* rshift = in(2)->in(2);
794
Node* shift = rotate_shift(phase, lshift, rshift, 0x1F);
795
if (shift != nullptr) {
796
return new RotateLeftNode(in(1)->in(1), shift, TypeInt::INT);
800
if (Matcher::match_rule_supported(Op_RotateRight) &&
801
lopcode == Op_URShiftI && ropcode == Op_LShiftI && in(1)->in(1) == in(2)->in(1)) {
802
Node* rshift = in(1)->in(2);
803
Node* lshift = in(2)->in(2);
804
Node* shift = rotate_shift(phase, rshift, lshift, 0x1F);
805
if (shift != nullptr) {
806
return new RotateRightNode(in(1)->in(1), shift, TypeInt::INT);
811
if (AddNode::is_not(phase, in(1), T_INT) && AddNode::is_not(phase, in(2), T_INT)) {
812
Node* and_a_b = new AndINode(in(1)->in(1), in(2)->in(1));
813
Node* tn = phase->transform(and_a_b);
814
return AddNode::make_not(phase, tn, T_INT);
824
const Type *OrINode::add_ring( const Type *t0, const Type *t1 ) const {
825
const TypeInt *r0 = t0->is_int();
826
const TypeInt *r1 = t1->is_int();
829
if ( r0 == TypeInt::BOOL ) {
830
if ( r1 == TypeInt::ONE) {
832
} else if ( r1 == TypeInt::BOOL ) {
833
return TypeInt::BOOL;
835
} else if ( r0 == TypeInt::ONE ) {
836
if ( r1 == TypeInt::BOOL ) {
842
if( !r0->is_con() || !r1->is_con() )
846
return TypeInt::make( r0->get_con() | r1->get_con() );
851
Node* OrLNode::Identity(PhaseGVN* phase) {
853
if (in(1) == in(2)) {
857
return AddNode::Identity(phase);
860
Node* OrLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
861
int lopcode = in(1)->Opcode();
862
int ropcode = in(2)->Opcode();
863
if (Matcher::match_rule_supported(Op_RotateLeft) &&
864
lopcode == Op_LShiftL && ropcode == Op_URShiftL && in(1)->in(1) == in(2)->in(1)) {
865
Node* lshift = in(1)->in(2);
866
Node* rshift = in(2)->in(2);
867
Node* shift = rotate_shift(phase, lshift, rshift, 0x3F);
868
if (shift != nullptr) {
869
return new RotateLeftNode(in(1)->in(1), shift, TypeLong::LONG);
873
if (Matcher::match_rule_supported(Op_RotateRight) &&
874
lopcode == Op_URShiftL && ropcode == Op_LShiftL && in(1)->in(1) == in(2)->in(1)) {
875
Node* rshift = in(1)->in(2);
876
Node* lshift = in(2)->in(2);
877
Node* shift = rotate_shift(phase, rshift, lshift, 0x3F);
878
if (shift != nullptr) {
879
return new RotateRightNode(in(1)->in(1), shift, TypeLong::LONG);
884
if (AddNode::is_not(phase, in(1), T_LONG) && AddNode::is_not(phase, in(2), T_LONG)) {
885
Node* and_a_b = new AndLNode(in(1)->in(1), in(2)->in(1));
886
Node* tn = phase->transform(and_a_b);
887
return AddNode::make_not(phase, tn, T_LONG);
894
const Type *OrLNode::add_ring( const Type *t0, const Type *t1 ) const {
895
const TypeLong *r0 = t0->is_long();
896
const TypeLong *r1 = t1->is_long();
899
if( !r0->is_con() || !r1->is_con() )
900
return TypeLong::LONG;
903
return TypeLong::make( r0->get_con() | r1->get_con() );
909
static bool is_used_in_only_arithmetic(Node* n, BasicType bt) {
910
for (DUIterator_Fast imax, i = n->fast_outs(imax); i < imax; i++) {
911
Node* u = n->fast_out(i);
912
if (u->Opcode() != Op_Add(bt) && u->Opcode() != Op_Sub(bt)) {
921
Node* XorINode::Ideal(PhaseGVN* phase, bool can_reshape) {
927
if (phase->type(in2) == TypeInt::MINUS_1) {
928
if (phase->is_IterGVN()) {
929
if (is_used_in_only_arithmetic(this, T_INT)
931
|| (in1->Opcode() == Op_AddI || in1->Opcode() == Op_SubI)) {
932
return new SubINode(in2, in1);
936
phase->record_for_igvn(this);
941
const TypeInt* in2_type = phase->type(in2)->isa_int();
942
if (in1->Opcode() == Op_CMoveI && in2_type != nullptr && in2_type->is_con()) {
943
int in2_val = in2_type->get_con();
946
const TypeInt* left = phase->type(in1->in(CMoveNode::IfFalse))->isa_int();
947
const TypeInt* right = phase->type(in1->in(CMoveNode::IfTrue))->isa_int();
950
if (left != nullptr && right != nullptr && left->is_con() && right->is_con()) {
951
Node* cond = in1->in(CMoveNode::Condition);
954
if (cond->is_Bool()) {
955
int cmp_op = cond->in(1)->Opcode();
957
if (cmp_op == Op_CmpI || cmp_op == Op_CmpP) {
958
int l_val = left->get_con();
959
int r_val = right->get_con();
961
return new CMoveINode(cond, phase->intcon(l_val ^ in2_val), phase->intcon(r_val ^ in2_val), TypeInt::INT);
967
return AddNode::Ideal(phase, can_reshape);
970
const Type* XorINode::Value(PhaseGVN* phase) const {
973
const Type* t1 = phase->type(in1);
974
const Type* t2 = phase->type(in2);
975
if (t1 == Type::TOP || t2 == Type::TOP) {
979
if (in1->eqv_uncast(in2)) {
984
const TypeInt* t1i = t1->is_int();
985
const TypeInt* t2i = t2->is_int();
986
if ((t1i->_lo >= 0) &&
991
const TypeInt* t1x = TypeInt::make(0, round_down_power_of_2(t1i->_hi) + (round_down_power_of_2(t1i->_hi) - 1), t1i->_widen);
992
const TypeInt* t2x = TypeInt::make(0, round_down_power_of_2(t2i->_hi) + (round_down_power_of_2(t2i->_hi) - 1), t2i->_widen);
993
return t1x->meet(t2x);
995
return AddNode::Value(phase);
1004
const Type *XorINode::add_ring( const Type *t0, const Type *t1 ) const {
1005
const TypeInt *r0 = t0->is_int();
1006
const TypeInt *r1 = t1->is_int();
1009
if( r0 == TypeInt::BOOL && ( r1 == TypeInt::ONE
1010
|| r1 == TypeInt::BOOL))
1011
return TypeInt::BOOL;
1013
if( !r0->is_con() || !r1->is_con() )
1014
return TypeInt::INT;
1017
return TypeInt::make( r0->get_con() ^ r1->get_con() );
1022
const Type *XorLNode::add_ring( const Type *t0, const Type *t1 ) const {
1023
const TypeLong *r0 = t0->is_long();
1024
const TypeLong *r1 = t1->is_long();
1027
if( !r0->is_con() || !r1->is_con() )
1028
return TypeLong::LONG;
1031
return TypeLong::make( r0->get_con() ^ r1->get_con() );
1034
Node* XorLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1040
if (phase->type(in2) == TypeLong::MINUS_1) {
1041
if (phase->is_IterGVN()) {
1042
if (is_used_in_only_arithmetic(this, T_LONG)
1044
|| (in1->Opcode() == Op_AddL || in1->Opcode() == Op_SubL)) {
1045
return new SubLNode(in2, in1);
1049
phase->record_for_igvn(this);
1052
return AddNode::Ideal(phase, can_reshape);
1055
const Type* XorLNode::Value(PhaseGVN* phase) const {
1058
const Type* t1 = phase->type(in1);
1059
const Type* t2 = phase->type(in2);
1060
if (t1 == Type::TOP || t2 == Type::TOP) {
1064
if (in1->eqv_uncast(in2)) {
1069
const TypeLong* t1l = t1->is_long();
1070
const TypeLong* t2l = t2->is_long();
1071
if ((t1l->_lo >= 0) &&
1076
const TypeLong* t1x = TypeLong::make(0, round_down_power_of_2(t1l->_hi) + (round_down_power_of_2(t1l->_hi) - 1), t1l->_widen);
1077
const TypeLong* t2x = TypeLong::make(0, round_down_power_of_2(t2l->_hi) + (round_down_power_of_2(t2l->_hi) - 1), t2l->_widen);
1078
return t1x->meet(t2x);
1080
return AddNode::Value(phase);
1083
Node* MaxNode::build_min_max_int(Node* a, Node* b, bool is_max) {
1085
return new MaxINode(a, b);
1087
return new MinINode(a, b);
1091
Node* MaxNode::build_min_max_long(PhaseGVN* phase, Node* a, Node* b, bool is_max) {
1093
return new MaxLNode(phase->C, a, b);
1095
return new MinLNode(phase->C, a, b);
1099
Node* MaxNode::build_min_max(Node* a, Node* b, bool is_max, bool is_unsigned, const Type* t, PhaseGVN& gvn) {
1100
bool is_int = gvn.type(a)->isa_int();
1101
assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1102
assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1103
BasicType bt = is_int ? T_INT: T_LONG;
1104
Node* hook = nullptr;
1105
if (gvn.is_IterGVN()) {
1108
hook->init_req(0, a);
1109
hook->init_req(1, b);
1111
Node* res = nullptr;
1112
if (is_int && !is_unsigned) {
1113
res = gvn.transform(build_min_max_int(a, b, is_max));
1114
assert(gvn.type(res)->is_int()->_lo >= t->is_int()->_lo && gvn.type(res)->is_int()->_hi <= t->is_int()->_hi, "type doesn't match");
1116
Node* cmp = nullptr;
1118
cmp = gvn.transform(CmpNode::make(a, b, bt, is_unsigned));
1120
cmp = gvn.transform(CmpNode::make(b, a, bt, is_unsigned));
1122
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1123
res = gvn.transform(CMoveNode::make(nullptr, bol, a, b, t));
1125
if (hook != nullptr) {
1126
hook->destruct(&gvn);
1131
Node* MaxNode::build_min_max_diff_with_zero(Node* a, Node* b, bool is_max, const Type* t, PhaseGVN& gvn) {
1132
bool is_int = gvn.type(a)->isa_int();
1133
assert(is_int || gvn.type(a)->isa_long(), "int or long inputs");
1134
assert(is_int == (gvn.type(b)->isa_int() != nullptr), "inconsistent inputs");
1135
BasicType bt = is_int ? T_INT: T_LONG;
1136
Node* zero = gvn.integercon(0, bt);
1137
Node* hook = nullptr;
1138
if (gvn.is_IterGVN()) {
1141
hook->init_req(0, a);
1142
hook->init_req(1, b);
1144
Node* cmp = nullptr;
1146
cmp = gvn.transform(CmpNode::make(a, b, bt, false));
1148
cmp = gvn.transform(CmpNode::make(b, a, bt, false));
1150
Node* sub = gvn.transform(SubNode::make(a, b, bt));
1151
Node* bol = gvn.transform(new BoolNode(cmp, BoolTest::lt));
1152
Node* res = gvn.transform(CMoveNode::make(nullptr, bol, sub, zero, t));
1153
if (hook != nullptr) {
1154
hook->destruct(&gvn);
1160
static bool can_overflow(const TypeInt* t, jint c) {
1163
return ((c < 0 && (java_add(t_lo, c) > t_lo)) ||
1164
(c > 0 && (java_add(t_hi, c) < t_hi)));
1170
Node* MaxNode::extract_add(PhaseGVN* phase, ConstAddOperands x_operands, ConstAddOperands y_operands) {
1171
Node* x = x_operands.first;
1172
Node* y = y_operands.first;
1173
int opcode = Opcode();
1174
assert(opcode == Op_MaxI || opcode == Op_MinI, "Unexpected opcode");
1175
const TypeInt* tx = phase->type(x)->isa_int();
1176
jint x_off = x_operands.second;
1177
jint y_off = y_operands.second;
1178
if (x == y && tx != nullptr &&
1179
!can_overflow(tx, x_off) &&
1180
!can_overflow(tx, y_off)) {
1181
jint c = opcode == Op_MinI ? MIN2(x_off, y_off) : MAX2(x_off, y_off);
1182
return new AddINode(x, phase->intcon(c));
1191
static ConstAddOperands as_add_with_constant(Node* n) {
1192
if (n->Opcode() != Op_AddI) {
1193
return ConstAddOperands(n, 0);
1198
return ConstAddOperands(n, 0);
1200
const Type* c_type = c->bottom_type();
1201
if (c_type == Type::TOP) {
1202
return ConstAddOperands(nullptr, 0);
1204
return ConstAddOperands(x, c_type->is_int()->get_con());
1207
Node* MaxNode::IdealI(PhaseGVN* phase, bool can_reshape) {
1208
int opcode = Opcode();
1209
assert(opcode == Op_MinI || opcode == Op_MaxI, "Unexpected opcode");
1219
for (uint inner_op_index = 1; inner_op_index <= 2; inner_op_index++) {
1220
if (in(inner_op_index)->Opcode() != opcode) {
1223
Node* outer_add = in(inner_op_index == 1 ? 2 : 1);
1224
ConstAddOperands outer_add_operands = as_add_with_constant(outer_add);
1225
if (outer_add_operands.first == nullptr) {
1230
for (uint inner_add_index = 1; inner_add_index <= 2; inner_add_index++) {
1231
Node* inner_op = in(inner_op_index);
1232
Node* inner_add = inner_op->in(inner_add_index);
1233
ConstAddOperands inner_add_operands = as_add_with_constant(inner_add);
1234
if (inner_add_operands.first == nullptr) {
1238
Node* add_extracted = extract_add(phase, inner_add_operands, outer_add_operands);
1239
if (add_extracted == nullptr) {
1242
Node* add_transformed = phase->transform(add_extracted);
1243
Node* inner_other = inner_op->in(inner_add_index == 1 ? 2 : 1);
1244
return build_min_max_int(add_transformed, inner_other, opcode == Op_MaxI);
1255
ConstAddOperands xC = as_add_with_constant(in(1));
1256
ConstAddOperands yC = as_add_with_constant(in(2));
1257
if (xC.first == nullptr || yC.first == nullptr) return nullptr;
1258
return extract_add(phase, xC, yC);
1262
Node* MaxINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1263
return IdealI(phase, can_reshape);
1269
const Type *MaxINode::add_ring( const Type *t0, const Type *t1 ) const {
1270
const TypeInt *r0 = t0->is_int();
1271
const TypeInt *r1 = t1->is_int();
1274
return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1281
Node* MinINode::Ideal(PhaseGVN* phase, bool can_reshape) {
1282
return IdealI(phase, can_reshape);
1287
const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
1288
const TypeInt *r0 = t0->is_int();
1289
const TypeInt *r1 = t1->is_int();
1292
return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
1323
static Node* fold_subI_no_underflow_pattern(Node* n, PhaseGVN* phase) {
1324
assert(n->Opcode() == Op_MaxL || n->Opcode() == Op_MinL, "sanity");
1326
jlong clamp = (n->Opcode() == Op_MaxL) ? min_jint : max_jint;
1327
auto is_clamp = [&](Node* c) {
1328
const TypeLong* t = phase->type(c)->isa_long();
1329
return t != nullptr && t->is_con() &&
1330
t->get_con() == clamp;
1333
auto is_sub_con = [&](Node* c) {
1334
const TypeLong* t = phase->type(c)->isa_long();
1335
return t != nullptr && t->is_con() &&
1336
t->get_con() < max_jint && t->get_con() > min_jint &&
1337
(t->get_con() < 0) == (n->Opcode() == Op_MaxL);
1340
Node* add1 = n->in(1);
1341
Node* clamp1 = n->in(2);
1342
if (add1->Opcode() == Op_AddL && is_clamp(clamp1)) {
1343
Node* max2 = add1->in(1);
1344
Node* con1 = add1->in(2);
1345
if (max2->Opcode() == n->Opcode() && is_sub_con(con1)) {
1346
Node* add2 = max2->in(1);
1347
Node* clamp2 = max2->in(2);
1348
if (add2->Opcode() == Op_AddL && is_clamp(clamp2)) {
1349
Node* x = add2->in(1);
1350
Node* con2 = add2->in(2);
1351
if (is_sub_con(con2)) {
1352
Node* new_con = phase->transform(new AddLNode(con1, con2));
1353
Node* new_sub = phase->transform(new AddLNode(x, new_con));
1354
n->set_req_X(1, new_sub, phase);
1363
const Type* MaxLNode::add_ring(const Type* t0, const Type* t1) const {
1364
const TypeLong* r0 = t0->is_long();
1365
const TypeLong* r1 = t1->is_long();
1367
return TypeLong::make(MAX2(r0->_lo, r1->_lo), MAX2(r0->_hi, r1->_hi), MAX2(r0->_widen, r1->_widen));
1370
Node* MaxLNode::Identity(PhaseGVN* phase) {
1371
const TypeLong* t1 = phase->type(in(1))->is_long();
1372
const TypeLong* t2 = phase->type(in(2))->is_long();
1375
if (t1->_lo >= t2->_hi) {
1377
} else if (t2->_lo >= t1->_hi) {
1381
return MaxNode::Identity(phase);
1384
Node* MaxLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1385
Node* n = AddNode::Ideal(phase, can_reshape);
1390
return fold_subI_no_underflow_pattern(this, phase);
1395
const Type* MinLNode::add_ring(const Type* t0, const Type* t1) const {
1396
const TypeLong* r0 = t0->is_long();
1397
const TypeLong* r1 = t1->is_long();
1399
return TypeLong::make(MIN2(r0->_lo, r1->_lo), MIN2(r0->_hi, r1->_hi), MIN2(r0->_widen, r1->_widen));
1402
Node* MinLNode::Identity(PhaseGVN* phase) {
1403
const TypeLong* t1 = phase->type(in(1))->is_long();
1404
const TypeLong* t2 = phase->type(in(2))->is_long();
1407
if (t1->_lo >= t2->_hi) {
1409
} else if (t2->_lo >= t1->_hi) {
1413
return MaxNode::Identity(phase);
1416
Node* MinLNode::Ideal(PhaseGVN* phase, bool can_reshape) {
1417
Node* n = AddNode::Ideal(phase, can_reshape);
1422
return fold_subI_no_underflow_pattern(this, phase);
1427
Node* MaxNode::Identity(PhaseGVN* phase) {
1428
if (in(1) == in(2)) {
1432
return AddNode::Identity(phase);
1436
const Type* MinFNode::add_ring(const Type* t0, const Type* t1 ) const {
1437
const TypeF* r0 = t0->isa_float_constant();
1438
const TypeF* r1 = t1->isa_float_constant();
1439
if (r0 == nullptr || r1 == nullptr) {
1440
return bottom_type();
1450
float f0 = r0->getf();
1451
float f1 = r1->getf();
1452
if (f0 != 0.0f || f1 != 0.0f) {
1453
return f0 < f1 ? r0 : r1;
1457
return (jint_cast(f0) < jint_cast(f1)) ? r0 : r1;
1461
const Type* MinDNode::add_ring(const Type* t0, const Type* t1) const {
1462
const TypeD* r0 = t0->isa_double_constant();
1463
const TypeD* r1 = t1->isa_double_constant();
1464
if (r0 == nullptr || r1 == nullptr) {
1465
return bottom_type();
1475
double d0 = r0->getd();
1476
double d1 = r1->getd();
1477
if (d0 != 0.0 || d1 != 0.0) {
1478
return d0 < d1 ? r0 : r1;
1482
return (jlong_cast(d0) < jlong_cast(d1)) ? r0 : r1;
1486
const Type* MaxFNode::add_ring(const Type* t0, const Type* t1) const {
1487
const TypeF* r0 = t0->isa_float_constant();
1488
const TypeF* r1 = t1->isa_float_constant();
1489
if (r0 == nullptr || r1 == nullptr) {
1490
return bottom_type();
1500
float f0 = r0->getf();
1501
float f1 = r1->getf();
1502
if (f0 != 0.0f || f1 != 0.0f) {
1503
return f0 > f1 ? r0 : r1;
1507
return (jint_cast(f0) > jint_cast(f1)) ? r0 : r1;
1511
const Type* MaxDNode::add_ring(const Type* t0, const Type* t1) const {
1512
const TypeD* r0 = t0->isa_double_constant();
1513
const TypeD* r1 = t1->isa_double_constant();
1514
if (r0 == nullptr || r1 == nullptr) {
1515
return bottom_type();
1525
double d0 = r0->getd();
1526
double d1 = r1->getd();
1527
if (d0 != 0.0 || d1 != 0.0) {
1528
return d0 > d1 ? r0 : r1;
1532
return (jlong_cast(d0) > jlong_cast(d1)) ? r0 : r1;