237
238 //=============================================================================
239 //------------------------------Idealize---------------------------------------
240 Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
241 Node* in1 = in(1);
242 Node* in2 = in(2);
243 int op1 = in1->Opcode();
244 int op2 = in2->Opcode();
245 // Fold (con1-x)+con2 into (con1+con2)-x
246 if ( op1 == Op_AddI && op2 == Op_SubI ) {
247 // Swap edges to try optimizations below
248 in1 = in2;
249 in2 = in(1);
250 op1 = op2;
251 op2 = in2->Opcode();
252 }
253 if( op1 == Op_SubI ) {
254 const Type *t_sub1 = phase->type( in1->in(1) );
255 const Type *t_2 = phase->type( in2 );
256 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
257 return new (phase->C) SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ),
258 in1->in(2) );
259 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
260 if( op2 == Op_SubI ) {
261 // Check for dead cycle: d = (a-b)+(c-d)
262 assert( in1->in(2) != this && in2->in(2) != this,
263 "dead loop in AddINode::Ideal" );
264 Node *sub = new (phase->C) SubINode(NULL, NULL);
265 sub->init_req(1, phase->transform(new (phase->C) AddINode(in1->in(1), in2->in(1) ) ));
266 sub->init_req(2, phase->transform(new (phase->C) AddINode(in1->in(2), in2->in(2) ) ));
267 return sub;
268 }
269 // Convert "(a-b)+(b+c)" into "(a+c)"
270 if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {
271 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
272 return new (phase->C) AddINode(in1->in(1), in2->in(2));
273 }
274 // Convert "(a-b)+(c+b)" into "(a+c)"
275 if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {
276 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
277 return new (phase->C) AddINode(in1->in(1), in2->in(1));
278 }
279 // Convert "(a-b)+(b-c)" into "(a-c)"
280 if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {
281 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
282 return new (phase->C) SubINode(in1->in(1), in2->in(2));
283 }
284 // Convert "(a-b)+(c-a)" into "(c-b)"
285 if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {
286 assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
287 return new (phase->C) SubINode(in2->in(1), in1->in(2));
288 }
289 }
290
291 // Convert "x+(0-y)" into "(x-y)"
292 if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )
293 return new (phase->C) SubINode(in1, in2->in(2) );
294
295 // Convert "(0-y)+x" into "(x-y)"
296 if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )
297 return new (phase->C) SubINode( in2, in1->in(2) );
298
299 // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
300 // Helps with array allocation math constant folding
301 // See 4790063:
302 // Unrestricted transformation is unsafe for some runtime values of 'x'
303 // ( x == 0, z == 1, y == -1 ) fails
304 // ( x == -5, z == 1, y == 1 ) fails
305 // Transform works for small z and small negative y when the addition
306 // (x + (y << z)) does not cross zero.
307 // Implement support for negative y and (x >= -(y << z))
308 // Have not observed cases where type information exists to support
309 // positive y and (x <= -(y << z))
310 if( op1 == Op_URShiftI && op2 == Op_ConI &&
311 in1->in(2)->Opcode() == Op_ConI ) {
312 jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
313 jint y = phase->type( in2 )->is_int()->get_con();
314
315 if( z < 5 && -5 < y && y < 0 ) {
316 const Type *t_in11 = phase->type(in1->in(1));
317 if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
318 Node *a = phase->transform( new (phase->C) AddINode( in1->in(1), phase->intcon(y<<z) ) );
319 return new (phase->C) URShiftINode( a, in1->in(2) );
320 }
321 }
322 }
323
324 return AddNode::Ideal(phase, can_reshape);
325 }
326
327
328 //------------------------------Identity---------------------------------------
329 // Fold (x-y)+y OR y+(x-y) into x
330 Node *AddINode::Identity( PhaseTransform *phase ) {
331 if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) {
332 return in(1)->in(1);
333 }
334 else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) {
335 return in(2)->in(1);
336 }
337 return AddNode::Identity(phase);
338 }
339
370 //=============================================================================
371 //------------------------------Idealize---------------------------------------
372 Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
373 Node* in1 = in(1);
374 Node* in2 = in(2);
375 int op1 = in1->Opcode();
376 int op2 = in2->Opcode();
377 // Fold (con1-x)+con2 into (con1+con2)-x
378 if ( op1 == Op_AddL && op2 == Op_SubL ) {
379 // Swap edges to try optimizations below
380 in1 = in2;
381 in2 = in(1);
382 op1 = op2;
383 op2 = in2->Opcode();
384 }
385 // Fold (con1-x)+con2 into (con1+con2)-x
386 if( op1 == Op_SubL ) {
387 const Type *t_sub1 = phase->type( in1->in(1) );
388 const Type *t_2 = phase->type( in2 );
389 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
390 return new (phase->C) SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ),
391 in1->in(2) );
392 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
393 if( op2 == Op_SubL ) {
394 // Check for dead cycle: d = (a-b)+(c-d)
395 assert( in1->in(2) != this && in2->in(2) != this,
396 "dead loop in AddLNode::Ideal" );
397 Node *sub = new (phase->C) SubLNode(NULL, NULL);
398 sub->init_req(1, phase->transform(new (phase->C) AddLNode(in1->in(1), in2->in(1) ) ));
399 sub->init_req(2, phase->transform(new (phase->C) AddLNode(in1->in(2), in2->in(2) ) ));
400 return sub;
401 }
402 // Convert "(a-b)+(b+c)" into "(a+c)"
403 if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {
404 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
405 return new (phase->C) AddLNode(in1->in(1), in2->in(2));
406 }
407 // Convert "(a-b)+(c+b)" into "(a+c)"
408 if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {
409 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
410 return new (phase->C) AddLNode(in1->in(1), in2->in(1));
411 }
412 // Convert "(a-b)+(b-c)" into "(a-c)"
413 if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {
414 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
415 return new (phase->C) SubLNode(in1->in(1), in2->in(2));
416 }
417 // Convert "(a-b)+(c-a)" into "(c-b)"
418 if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) {
419 assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
420 return new (phase->C) SubLNode(in2->in(1), in1->in(2));
421 }
422 }
423
424 // Convert "x+(0-y)" into "(x-y)"
425 if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )
426 return new (phase->C) SubLNode( in1, in2->in(2) );
427
428 // Convert "(0-y)+x" into "(x-y)"
429 if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO )
430 return new (phase->C) SubLNode( in2, in1->in(2) );
431
432 // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"
433 // into "(X<<1)+Y" and let shift-folding happen.
434 if( op2 == Op_AddL &&
435 in2->in(1) == in1 &&
436 op1 != Op_ConL &&
437 0 ) {
438 Node *shift = phase->transform(new (phase->C) LShiftLNode(in1,phase->intcon(1)));
439 return new (phase->C) AddLNode(shift,in2->in(2));
440 }
441
442 return AddNode::Ideal(phase, can_reshape);
443 }
444
445
446 //------------------------------Identity---------------------------------------
447 // Fold (x-y)+y OR y+(x-y) into x
448 Node *AddLNode::Identity( PhaseTransform *phase ) {
449 if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) {
450 return in(1)->in(1);
451 }
452 else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) {
453 return in(2)->in(1);
454 }
455 return AddNode::Identity(phase);
456 }
457
458
459 //------------------------------add_ring---------------------------------------
579 assert( !addp->in(Address)->is_AddP() ||
580 addp->in(Address)->as_AddP() != addp,
581 "dead loop in AddPNode::Ideal" );
582 // Type of left input's right input
583 const Type *t = phase->type( addp->in(Offset) );
584 if( t == Type::TOP ) return NULL;
585 const TypeX *t12 = t->is_intptr_t();
586 if( t12->is_con() ) { // Left input is an add of a constant?
587 // If the right input is a constant, combine constants
588 const Type *temp_t2 = phase->type( in(Offset) );
589 if( temp_t2 == Type::TOP ) return NULL;
590 const TypeX *t2 = temp_t2->is_intptr_t();
591 Node* address;
592 Node* offset;
593 if( t2->is_con() ) {
594 // The Add of the flattened expression
595 address = addp->in(Address);
596 offset = phase->MakeConX(t2->get_con() + t12->get_con());
597 } else {
598 // Else move the constant to the right. ((A+con)+B) into ((A+B)+con)
599 address = phase->transform(new (phase->C) AddPNode(in(Base),addp->in(Address),in(Offset)));
600 offset = addp->in(Offset);
601 }
602 PhaseIterGVN *igvn = phase->is_IterGVN();
603 if( igvn ) {
604 set_req_X(Address,address,igvn);
605 set_req_X(Offset,offset,igvn);
606 } else {
607 set_req(Address,address);
608 set_req(Offset,offset);
609 }
610 return this;
611 }
612 }
613
614 // Raw pointers?
615 if( in(Base)->bottom_type() == Type::TOP ) {
616 // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
617 if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
618 Node* offset = in(Offset);
619 return new (phase->C) CastX2PNode(offset);
620 }
621 }
622
623 // If the right is an add of a constant, push the offset down.
624 // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
625 // The idea is to merge array_base+scaled_index groups together,
626 // and only have different constant offsets from the same base.
627 const Node *add = in(Offset);
628 if( add->Opcode() == Op_AddX && add->in(1) != add ) {
629 const Type *t22 = phase->type( add->in(2) );
630 if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?
631 set_req(Address, phase->transform(new (phase->C) AddPNode(in(Base),in(Address),add->in(1))));
632 set_req(Offset, add->in(2));
633 PhaseIterGVN *igvn = phase->is_IterGVN();
634 if (add->outcnt() == 0 && igvn) {
635 // add disconnected.
636 igvn->_worklist.push((Node*)add);
637 }
638 return this; // Made progress
639 }
640 }
641
642 return NULL; // No progress
643 }
644
645 //------------------------------bottom_type------------------------------------
646 // Bottom-type is the pointer-type with unknown offset.
647 const Type *AddPNode::bottom_type() const {
648 if (in(Address) == NULL) return TypePtr::BOTTOM;
649 const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
650 if( !tp ) return Type::TOP; // TOP input means TOP output
651 assert( in(Offset)->Opcode() != Op_ConP, "" );
841 const TypeInt *r0 = t0->is_int(); // Handy access
842 const TypeInt *r1 = t1->is_int();
843
844 // Otherwise just MAX them bits.
845 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
846 }
847
848 //=============================================================================
849 //------------------------------Idealize---------------------------------------
850 // MINs show up in range-check loop limit calculations. Look for
851 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"
852 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
853 Node *progress = NULL;
854 // Force a right-spline graph
855 Node *l = in(1);
856 Node *r = in(2);
857 // Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) )
858 // to force a right-spline graph for the rest of MinINode::Ideal().
859 if( l->Opcode() == Op_MinI ) {
860 assert( l != l->in(1), "dead loop in MinINode::Ideal" );
861 r = phase->transform(new (phase->C) MinINode(l->in(2),r));
862 l = l->in(1);
863 set_req(1, l);
864 set_req(2, r);
865 return this;
866 }
867
868 // Get left input & constant
869 Node *x = l;
870 int x_off = 0;
871 if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
872 x->in(2)->is_Con() ) {
873 const Type *t = x->in(2)->bottom_type();
874 if( t == Type::TOP ) return NULL; // No progress
875 x_off = t->is_int()->get_con();
876 x = x->in(1);
877 }
878
879 // Scan a right-spline-tree for MINs
880 Node *y = r;
881 int y_off = 0;
889 }
890 if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
891 swap_edges(1, 2);
892 return this;
893 }
894
895
896 if( r->Opcode() == Op_MinI ) {
897 assert( r != r->in(2), "dead loop in MinINode::Ideal" );
898 y = r->in(1);
899 // Check final part of MIN tree
900 if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
901 y->in(2)->is_Con() ) {
902 const Type *t = y->in(2)->bottom_type();
903 if( t == Type::TOP ) return NULL; // No progress
904 y_off = t->is_int()->get_con();
905 y = y->in(1);
906 }
907
908 if( x->_idx > y->_idx )
909 return new (phase->C) MinINode(r->in(1),phase->transform(new (phase->C) MinINode(l,r->in(2))));
910
911 // See if covers: MIN2(x+c0,MIN2(y+c1,z))
912 if( !phase->eqv(x,y) ) return NULL;
913 // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into
914 // MIN2(x+c0 or x+c1 which less, z).
915 return new (phase->C) MinINode(phase->transform(new (phase->C) AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2));
916 } else {
917 // See if covers: MIN2(x+c0,y+c1)
918 if( !phase->eqv(x,y) ) return NULL;
919 // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less.
920 return new (phase->C) AddINode(x,phase->intcon(MIN2(x_off,y_off)));
921 }
922
923 }
924
925 //------------------------------add_ring---------------------------------------
926 // Supplied function returns the sum of the inputs.
927 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
928 const TypeInt *r0 = t0->is_int(); // Handy access
929 const TypeInt *r1 = t1->is_int();
930
931 // Otherwise just MIN them bits.
932 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
933 }
|
237
238 //=============================================================================
239 //------------------------------Idealize---------------------------------------
240 Node *AddINode::Ideal(PhaseGVN *phase, bool can_reshape) {
241 Node* in1 = in(1);
242 Node* in2 = in(2);
243 int op1 = in1->Opcode();
244 int op2 = in2->Opcode();
245 // Fold (con1-x)+con2 into (con1+con2)-x
246 if ( op1 == Op_AddI && op2 == Op_SubI ) {
247 // Swap edges to try optimizations below
248 in1 = in2;
249 in2 = in(1);
250 op1 = op2;
251 op2 = in2->Opcode();
252 }
253 if( op1 == Op_SubI ) {
254 const Type *t_sub1 = phase->type( in1->in(1) );
255 const Type *t_2 = phase->type( in2 );
256 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
257 return new SubINode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
258 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
259 if( op2 == Op_SubI ) {
260 // Check for dead cycle: d = (a-b)+(c-d)
261 assert( in1->in(2) != this && in2->in(2) != this,
262 "dead loop in AddINode::Ideal" );
263 Node *sub = new SubINode(NULL, NULL);
264 sub->init_req(1, phase->transform(new AddINode(in1->in(1), in2->in(1) ) ));
265 sub->init_req(2, phase->transform(new AddINode(in1->in(2), in2->in(2) ) ));
266 return sub;
267 }
268 // Convert "(a-b)+(b+c)" into "(a+c)"
269 if( op2 == Op_AddI && in1->in(2) == in2->in(1) ) {
270 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
271 return new AddINode(in1->in(1), in2->in(2));
272 }
273 // Convert "(a-b)+(c+b)" into "(a+c)"
274 if( op2 == Op_AddI && in1->in(2) == in2->in(2) ) {
275 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
276 return new AddINode(in1->in(1), in2->in(1));
277 }
278 // Convert "(a-b)+(b-c)" into "(a-c)"
279 if( op2 == Op_SubI && in1->in(2) == in2->in(1) ) {
280 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddINode::Ideal");
281 return new SubINode(in1->in(1), in2->in(2));
282 }
283 // Convert "(a-b)+(c-a)" into "(c-b)"
284 if( op2 == Op_SubI && in1->in(1) == in2->in(2) ) {
285 assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddINode::Ideal");
286 return new SubINode(in2->in(1), in1->in(2));
287 }
288 }
289
290 // Convert "x+(0-y)" into "(x-y)"
291 if( op2 == Op_SubI && phase->type(in2->in(1)) == TypeInt::ZERO )
292 return new SubINode(in1, in2->in(2) );
293
294 // Convert "(0-y)+x" into "(x-y)"
295 if( op1 == Op_SubI && phase->type(in1->in(1)) == TypeInt::ZERO )
296 return new SubINode( in2, in1->in(2) );
297
298 // Convert (x>>>z)+y into (x+(y<<z))>>>z for small constant z and y.
299 // Helps with array allocation math constant folding
300 // See 4790063:
301 // Unrestricted transformation is unsafe for some runtime values of 'x'
302 // ( x == 0, z == 1, y == -1 ) fails
303 // ( x == -5, z == 1, y == 1 ) fails
304 // Transform works for small z and small negative y when the addition
305 // (x + (y << z)) does not cross zero.
306 // Implement support for negative y and (x >= -(y << z))
307 // Have not observed cases where type information exists to support
308 // positive y and (x <= -(y << z))
309 if( op1 == Op_URShiftI && op2 == Op_ConI &&
310 in1->in(2)->Opcode() == Op_ConI ) {
311 jint z = phase->type( in1->in(2) )->is_int()->get_con() & 0x1f; // only least significant 5 bits matter
312 jint y = phase->type( in2 )->is_int()->get_con();
313
314 if( z < 5 && -5 < y && y < 0 ) {
315 const Type *t_in11 = phase->type(in1->in(1));
316 if( t_in11 != Type::TOP && (t_in11->is_int()->_lo >= -(y << z)) ) {
317 Node *a = phase->transform( new AddINode( in1->in(1), phase->intcon(y<<z) ) );
318 return new URShiftINode( a, in1->in(2) );
319 }
320 }
321 }
322
323 return AddNode::Ideal(phase, can_reshape);
324 }
325
326
327 //------------------------------Identity---------------------------------------
328 // Fold (x-y)+y OR y+(x-y) into x
329 Node *AddINode::Identity( PhaseTransform *phase ) {
330 if( in(1)->Opcode() == Op_SubI && phase->eqv(in(1)->in(2),in(2)) ) {
331 return in(1)->in(1);
332 }
333 else if( in(2)->Opcode() == Op_SubI && phase->eqv(in(2)->in(2),in(1)) ) {
334 return in(2)->in(1);
335 }
336 return AddNode::Identity(phase);
337 }
338
369 //=============================================================================
370 //------------------------------Idealize---------------------------------------
371 Node *AddLNode::Ideal(PhaseGVN *phase, bool can_reshape) {
372 Node* in1 = in(1);
373 Node* in2 = in(2);
374 int op1 = in1->Opcode();
375 int op2 = in2->Opcode();
376 // Fold (con1-x)+con2 into (con1+con2)-x
377 if ( op1 == Op_AddL && op2 == Op_SubL ) {
378 // Swap edges to try optimizations below
379 in1 = in2;
380 in2 = in(1);
381 op1 = op2;
382 op2 = in2->Opcode();
383 }
384 // Fold (con1-x)+con2 into (con1+con2)-x
385 if( op1 == Op_SubL ) {
386 const Type *t_sub1 = phase->type( in1->in(1) );
387 const Type *t_2 = phase->type( in2 );
388 if( t_sub1->singleton() && t_2->singleton() && t_sub1 != Type::TOP && t_2 != Type::TOP )
389 return new SubLNode(phase->makecon( add_ring( t_sub1, t_2 ) ), in1->in(2) );
390 // Convert "(a-b)+(c-d)" into "(a+c)-(b+d)"
391 if( op2 == Op_SubL ) {
392 // Check for dead cycle: d = (a-b)+(c-d)
393 assert( in1->in(2) != this && in2->in(2) != this,
394 "dead loop in AddLNode::Ideal" );
395 Node *sub = new SubLNode(NULL, NULL);
396 sub->init_req(1, phase->transform(new AddLNode(in1->in(1), in2->in(1) ) ));
397 sub->init_req(2, phase->transform(new AddLNode(in1->in(2), in2->in(2) ) ));
398 return sub;
399 }
400 // Convert "(a-b)+(b+c)" into "(a+c)"
401 if( op2 == Op_AddL && in1->in(2) == in2->in(1) ) {
402 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
403 return new AddLNode(in1->in(1), in2->in(2));
404 }
405 // Convert "(a-b)+(c+b)" into "(a+c)"
406 if( op2 == Op_AddL && in1->in(2) == in2->in(2) ) {
407 assert(in1->in(1) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
408 return new AddLNode(in1->in(1), in2->in(1));
409 }
410 // Convert "(a-b)+(b-c)" into "(a-c)"
411 if( op2 == Op_SubL && in1->in(2) == in2->in(1) ) {
412 assert(in1->in(1) != this && in2->in(2) != this,"dead loop in AddLNode::Ideal");
413 return new SubLNode(in1->in(1), in2->in(2));
414 }
415 // Convert "(a-b)+(c-a)" into "(c-b)"
416 if( op2 == Op_SubL && in1->in(1) == in1->in(2) ) {
417 assert(in1->in(2) != this && in2->in(1) != this,"dead loop in AddLNode::Ideal");
418 return new SubLNode(in2->in(1), in1->in(2));
419 }
420 }
421
422 // Convert "x+(0-y)" into "(x-y)"
423 if( op2 == Op_SubL && phase->type(in2->in(1)) == TypeLong::ZERO )
424 return new SubLNode( in1, in2->in(2) );
425
426 // Convert "(0-y)+x" into "(x-y)"
427 if( op1 == Op_SubL && phase->type(in1->in(1)) == TypeInt::ZERO )
428 return new SubLNode( in2, in1->in(2) );
429
430 // Convert "X+X+X+X+X...+X+Y" into "k*X+Y" or really convert "X+(X+Y)"
431 // into "(X<<1)+Y" and let shift-folding happen.
432 if( op2 == Op_AddL &&
433 in2->in(1) == in1 &&
434 op1 != Op_ConL &&
435 0 ) {
436 Node *shift = phase->transform(new LShiftLNode(in1,phase->intcon(1)));
437 return new AddLNode(shift,in2->in(2));
438 }
439
440 return AddNode::Ideal(phase, can_reshape);
441 }
442
443
444 //------------------------------Identity---------------------------------------
445 // Fold (x-y)+y OR y+(x-y) into x
446 Node *AddLNode::Identity( PhaseTransform *phase ) {
447 if( in(1)->Opcode() == Op_SubL && phase->eqv(in(1)->in(2),in(2)) ) {
448 return in(1)->in(1);
449 }
450 else if( in(2)->Opcode() == Op_SubL && phase->eqv(in(2)->in(2),in(1)) ) {
451 return in(2)->in(1);
452 }
453 return AddNode::Identity(phase);
454 }
455
456
457 //------------------------------add_ring---------------------------------------
577 assert( !addp->in(Address)->is_AddP() ||
578 addp->in(Address)->as_AddP() != addp,
579 "dead loop in AddPNode::Ideal" );
580 // Type of left input's right input
581 const Type *t = phase->type( addp->in(Offset) );
582 if( t == Type::TOP ) return NULL;
583 const TypeX *t12 = t->is_intptr_t();
584 if( t12->is_con() ) { // Left input is an add of a constant?
585 // If the right input is a constant, combine constants
586 const Type *temp_t2 = phase->type( in(Offset) );
587 if( temp_t2 == Type::TOP ) return NULL;
588 const TypeX *t2 = temp_t2->is_intptr_t();
589 Node* address;
590 Node* offset;
591 if( t2->is_con() ) {
592 // The Add of the flattened expression
593 address = addp->in(Address);
594 offset = phase->MakeConX(t2->get_con() + t12->get_con());
595 } else {
596 // Else move the constant to the right. ((A+con)+B) into ((A+B)+con)
597 address = phase->transform(new AddPNode(in(Base),addp->in(Address),in(Offset)));
598 offset = addp->in(Offset);
599 }
600 PhaseIterGVN *igvn = phase->is_IterGVN();
601 if( igvn ) {
602 set_req_X(Address,address,igvn);
603 set_req_X(Offset,offset,igvn);
604 } else {
605 set_req(Address,address);
606 set_req(Offset,offset);
607 }
608 return this;
609 }
610 }
611
612 // Raw pointers?
613 if( in(Base)->bottom_type() == Type::TOP ) {
614 // If this is a NULL+long form (from unsafe accesses), switch to a rawptr.
615 if (phase->type(in(Address)) == TypePtr::NULL_PTR) {
616 Node* offset = in(Offset);
617 return new CastX2PNode(offset);
618 }
619 }
620
621 // If the right is an add of a constant, push the offset down.
622 // Convert: (ptr + (offset+con)) into (ptr+offset)+con.
623 // The idea is to merge array_base+scaled_index groups together,
624 // and only have different constant offsets from the same base.
625 const Node *add = in(Offset);
626 if( add->Opcode() == Op_AddX && add->in(1) != add ) {
627 const Type *t22 = phase->type( add->in(2) );
628 if( t22->singleton() && (t22 != Type::TOP) ) { // Right input is an add of a constant?
629 set_req(Address, phase->transform(new AddPNode(in(Base),in(Address),add->in(1))));
630 set_req(Offset, add->in(2));
631 PhaseIterGVN *igvn = phase->is_IterGVN();
632 if (add->outcnt() == 0 && igvn) {
633 // add disconnected.
634 igvn->_worklist.push((Node*)add);
635 }
636 return this; // Made progress
637 }
638 }
639
640 return NULL; // No progress
641 }
642
643 //------------------------------bottom_type------------------------------------
644 // Bottom-type is the pointer-type with unknown offset.
645 const Type *AddPNode::bottom_type() const {
646 if (in(Address) == NULL) return TypePtr::BOTTOM;
647 const TypePtr *tp = in(Address)->bottom_type()->isa_ptr();
648 if( !tp ) return Type::TOP; // TOP input means TOP output
649 assert( in(Offset)->Opcode() != Op_ConP, "" );
839 const TypeInt *r0 = t0->is_int(); // Handy access
840 const TypeInt *r1 = t1->is_int();
841
842 // Otherwise just MAX them bits.
843 return TypeInt::make( MAX2(r0->_lo,r1->_lo), MAX2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
844 }
845
846 //=============================================================================
847 //------------------------------Idealize---------------------------------------
848 // MINs show up in range-check loop limit calculations. Look for
849 // "MIN2(x+c0,MIN2(y,x+c1))". Pick the smaller constant: "MIN2(x+c0,y)"
850 Node *MinINode::Ideal(PhaseGVN *phase, bool can_reshape) {
851 Node *progress = NULL;
852 // Force a right-spline graph
853 Node *l = in(1);
854 Node *r = in(2);
855 // Transform MinI1( MinI2(a,b), c) into MinI1( a, MinI2(b,c) )
856 // to force a right-spline graph for the rest of MinINode::Ideal().
857 if( l->Opcode() == Op_MinI ) {
858 assert( l != l->in(1), "dead loop in MinINode::Ideal" );
859 r = phase->transform(new MinINode(l->in(2),r));
860 l = l->in(1);
861 set_req(1, l);
862 set_req(2, r);
863 return this;
864 }
865
866 // Get left input & constant
867 Node *x = l;
868 int x_off = 0;
869 if( x->Opcode() == Op_AddI && // Check for "x+c0" and collect constant
870 x->in(2)->is_Con() ) {
871 const Type *t = x->in(2)->bottom_type();
872 if( t == Type::TOP ) return NULL; // No progress
873 x_off = t->is_int()->get_con();
874 x = x->in(1);
875 }
876
877 // Scan a right-spline-tree for MINs
878 Node *y = r;
879 int y_off = 0;
887 }
888 if( x->_idx > y->_idx && r->Opcode() != Op_MinI ) {
889 swap_edges(1, 2);
890 return this;
891 }
892
893
894 if( r->Opcode() == Op_MinI ) {
895 assert( r != r->in(2), "dead loop in MinINode::Ideal" );
896 y = r->in(1);
897 // Check final part of MIN tree
898 if( y->Opcode() == Op_AddI &&// Check for "y+c1" and collect constant
899 y->in(2)->is_Con() ) {
900 const Type *t = y->in(2)->bottom_type();
901 if( t == Type::TOP ) return NULL; // No progress
902 y_off = t->is_int()->get_con();
903 y = y->in(1);
904 }
905
906 if( x->_idx > y->_idx )
907 return new MinINode(r->in(1),phase->transform(new MinINode(l,r->in(2))));
908
909 // See if covers: MIN2(x+c0,MIN2(y+c1,z))
910 if( !phase->eqv(x,y) ) return NULL;
911 // If (y == x) transform MIN2(x+c0, MIN2(x+c1,z)) into
912 // MIN2(x+c0 or x+c1 which less, z).
913 return new MinINode(phase->transform(new AddINode(x,phase->intcon(MIN2(x_off,y_off)))),r->in(2));
914 } else {
915 // See if covers: MIN2(x+c0,y+c1)
916 if( !phase->eqv(x,y) ) return NULL;
917 // If (y == x) transform MIN2(x+c0,x+c1) into x+c0 or x+c1 which less.
918 return new AddINode(x,phase->intcon(MIN2(x_off,y_off)));
919 }
920
921 }
922
923 //------------------------------add_ring---------------------------------------
924 // Supplied function returns the sum of the inputs.
925 const Type *MinINode::add_ring( const Type *t0, const Type *t1 ) const {
926 const TypeInt *r0 = t0->is_int(); // Handy access
927 const TypeInt *r1 = t1->is_int();
928
929 // Otherwise just MIN them bits.
930 return TypeInt::make( MIN2(r0->_lo,r1->_lo), MIN2(r0->_hi,r1->_hi), MAX2(r0->_widen,r1->_widen) );
931 }
|