< prev index next >

src/hotspot/share/opto/subnode.cpp

Print this page
rev 51863 : 8210152: Optimize integer divisible by power-of-2 check
Summary: Integer conditional negation operation before zero check is eliminated
Reviewed-by: kvn, thartmann
Contributed-by: pengfei.li@arm.com


1505         ( cmp2_type == TypeInt::ZERO ) ) {
1506     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),cmp1->in(2)));
1507     return new BoolNode( ncmp, _test._test );
1508   }
1509 
1510   // Same as above but with and AddI of a constant
1511   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1512       cop == Op_CmpI &&
1513       cmp1_op == Op_AddI &&
1514       cmp1->in(2) != NULL &&
1515       phase->type(cmp1->in(2))->isa_int() &&
1516       phase->type(cmp1->in(2))->is_int()->is_con() &&
1517       cmp2_type == TypeInt::ZERO &&
1518       !is_counted_loop_cmp(cmp) // modifying the exit test of a counted loop messes the counted loop shape
1519       ) {
1520     const TypeInt* cmp1_in2 = phase->type(cmp1->in(2))->is_int();
1521     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),phase->intcon(-cmp1_in2->_hi)));
1522     return new BoolNode( ncmp, _test._test );
1523   }
1524 































1525   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1526   // most general case because negating 0x80000000 does nothing.  Needed for
1527   // the CmpF3/SubI/CmpI idiom.
1528   if( cop == Op_CmpI &&
1529       cmp1_op == Op_SubI &&
1530       cmp2_type == TypeInt::ZERO &&
1531       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1532       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1533     Node *ncmp = phase->transform( new CmpINode(cmp1->in(2),cmp2));
1534     return new BoolNode( ncmp, _test.commute() );
1535   }
1536 
1537   // Try to optimize signed integer comparison
1538   return fold_cmpI(phase, cmp->as_Sub(), cmp1, cop, cmp1_op, cmp2_type);
1539 
1540   //  The transformation below is not valid for either signed or unsigned
1541   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1542   //  This transformation can be resurrected when we are able to
1543   //  make inferences about the range of values being subtracted from
1544   //  (or added to) relative to the wraparound point.




1505         ( cmp2_type == TypeInt::ZERO ) ) {
1506     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),cmp1->in(2)));
1507     return new BoolNode( ncmp, _test._test );
1508   }
1509 
1510   // Same as above but with and AddI of a constant
1511   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1512       cop == Op_CmpI &&
1513       cmp1_op == Op_AddI &&
1514       cmp1->in(2) != NULL &&
1515       phase->type(cmp1->in(2))->isa_int() &&
1516       phase->type(cmp1->in(2))->is_int()->is_con() &&
1517       cmp2_type == TypeInt::ZERO &&
1518       !is_counted_loop_cmp(cmp) // modifying the exit test of a counted loop messes the counted loop shape
1519       ) {
1520     const TypeInt* cmp1_in2 = phase->type(cmp1->in(2))->is_int();
1521     Node *ncmp = phase->transform( new CmpINode(cmp1->in(1),phase->intcon(-cmp1_in2->_hi)));
1522     return new BoolNode( ncmp, _test._test );
1523   }
1524 
1525   // Change "bool eq/ne (cmp (phi (X -X) 0))" into "bool eq/ne (cmp X 0)"
1526   // since zero check of conditional negation of an integer is equal to
1527   // zero check of the integer directly.
1528   if ((_test._test == BoolTest::eq || _test._test == BoolTest::ne) &&
1529       (cop == Op_CmpI) &&
1530       (cmp2_type == TypeInt::ZERO) &&
1531       (cmp1_op == Op_Phi)) {
1532     // There should be a diamond phi with true path at index 1 or 2
1533     PhiNode *phi = cmp1->as_Phi();
1534     int idx_true = phi->is_diamond_phi();
1535     if (idx_true != 0) {
1536       // True input is in(idx_true) while false input is in(3 - idx_true)
1537       Node *tin = phi->in(idx_true);
1538       Node *fin = phi->in(3 - idx_true);
1539       if ((tin->Opcode() == Op_SubI) &&
1540           (phase->type(tin->in(1)) == TypeInt::ZERO) &&
1541           (tin->in(2) == fin)) {
1542         // Found conditional negation at true path, create a new CmpINode without that
1543         Node *ncmp = phase->transform(new CmpINode(fin, cmp2));
1544         return new BoolNode(ncmp, _test._test);
1545       }
1546       if ((fin->Opcode() == Op_SubI) &&
1547           (phase->type(fin->in(1)) == TypeInt::ZERO) &&
1548           (fin->in(2) == tin)) {
1549         // Found conditional negation at false path, create a new CmpINode without that
1550         Node *ncmp = phase->transform(new CmpINode(tin, cmp2));
1551         return new BoolNode(ncmp, _test._test);
1552       }
1553     }
1554   }
1555 
1556   // Change (-A vs 0) into (A vs 0) by commuting the test.  Disallow in the
1557   // most general case because negating 0x80000000 does nothing.  Needed for
1558   // the CmpF3/SubI/CmpI idiom.
1559   if( cop == Op_CmpI &&
1560       cmp1_op == Op_SubI &&
1561       cmp2_type == TypeInt::ZERO &&
1562       phase->type( cmp1->in(1) ) == TypeInt::ZERO &&
1563       phase->type( cmp1->in(2) )->higher_equal(TypeInt::SYMINT) ) {
1564     Node *ncmp = phase->transform( new CmpINode(cmp1->in(2),cmp2));
1565     return new BoolNode( ncmp, _test.commute() );
1566   }
1567 
1568   // Try to optimize signed integer comparison
1569   return fold_cmpI(phase, cmp->as_Sub(), cmp1, cop, cmp1_op, cmp2_type);
1570 
1571   //  The transformation below is not valid for either signed or unsigned
1572   //  comparisons due to wraparound concerns at MAX_VALUE and MIN_VALUE.
1573   //  This transformation can be resurrected when we are able to
1574   //  make inferences about the range of values being subtracted from
1575   //  (or added to) relative to the wraparound point.


< prev index next >