@@ -395,159 +395,9 @@ Node* AddNode::IdealIL(PhaseGVN* phase, bool can_reshape, BasicType bt) {
395395 }
396396 }
397397
398- // Convert a + a + ... + a into a*n
399- Node* serial_additions = convert_serial_additions (phase, bt);
400- if (serial_additions != nullptr ) {
401- return serial_additions;
402- }
403-
404398 return AddNode::Ideal (phase, can_reshape);
405399}
406400
407- // Try to convert a serial of additions into a single multiplication. Also convert `(a * CON) + a` to `(CON + 1) * a` as
408- // a side effect. On success, a new MulNode is returned.
409- Node* AddNode::convert_serial_additions (PhaseGVN* phase, BasicType bt) {
410- // We need to make sure that the current AddNode is not part of a MulNode that has already been optimized to a
411- // power-of-2 addition (e.g., 3 * a => (a << 2) + a). Without this check, GVN would keep trying to optimize the same
412- // node and can't progress. For example, 3 * a => (a << 2) + a => 3 * a => (a << 2) + a => ...
413- if (find_power_of_two_addition_pattern (this , bt, nullptr ) != nullptr ) {
414- return nullptr ;
415- }
416-
417- Node* in1 = in (1 );
418- Node* in2 = in (2 );
419- jlong multiplier;
420-
421- // While multiplications can be potentially optimized to power-of-2 subtractions (e.g., a * 7 => (a << 3) - a),
422- // (x - y) + y => x is already handled by the Identity() methods. So, we don't need to check for that pattern here.
423- if (find_simple_addition_pattern (in1, bt, &multiplier) == in2
424- || find_simple_lshift_pattern (in1, bt, &multiplier) == in2
425- || find_simple_multiplication_pattern (in1, bt, &multiplier) == in2
426- || find_power_of_two_addition_pattern (in1, bt, &multiplier) == in2) {
427- multiplier++; // +1 for the in2 term
428-
429- Node* con = (bt == T_INT)
430- ? (Node*) phase->intcon ((jint) multiplier) // intentional type narrowing to allow overflow at max_jint
431- : (Node*) phase->longcon (multiplier);
432- return MulNode::make (con, in2, bt);
433- }
434-
435- return nullptr ;
436- }
437-
438- // Try to match `a + a`. On success, return `a` and set `2` as `multiplier`.
439- // The method matches `n` for pattern: AddNode(a, a).
440- Node* AddNode::find_simple_addition_pattern (Node* n, BasicType bt, jlong* multiplier) {
441- if (n->Opcode () == Op_Add (bt) && n->in (1 ) == n->in (2 )) {
442- *multiplier = 2 ;
443- return n->in (1 );
444- }
445-
446- return nullptr ;
447- }
448-
449- // Try to match `a << CON`. On success, return `a` and set `1 << CON` as `multiplier`.
450- // Match `n` for pattern: LShiftNode(a, CON).
451- // Note that the power-of-2 multiplication optimization could potentially convert a MulNode to this pattern.
452- Node* AddNode::find_simple_lshift_pattern (Node* n, BasicType bt, jlong* multiplier) {
453- // Note that power-of-2 multiplication optimization could potentially convert a MulNode to this pattern
454- if (n->Opcode () == Op_LShift (bt) && n->in (2 )->is_Con ()) {
455- Node* con = n->in (2 );
456- if (con->is_top ()) {
457- return nullptr ;
458- }
459-
460- *multiplier = ((jlong) 1 << con->get_int ());
461- return n->in (1 );
462- }
463-
464- return nullptr ;
465- }
466-
467- // Try to match `CON * a`. On success, return `a` and set `CON` as `multiplier`.
468- // Match `n` for patterns:
469- // - MulNode(CON, a)
470- // - MulNode(a, CON)
471- Node* AddNode::find_simple_multiplication_pattern (Node* n, BasicType bt, jlong* multiplier) {
472- // This optimization technically only produces MulNode(CON, a), but we might as match MulNode(a, CON), too.
473- if (n->Opcode () == Op_Mul (bt) && (n->in (1 )->is_Con () || n->in (2 )->is_Con ())) {
474- Node* con = n->in (1 );
475- Node* base = n->in (2 );
476-
477- // swap ConNode to lhs for easier matching
478- if (!con->is_Con ()) {
479- swap (con, base);
480- }
481-
482- if (con->is_top ()) {
483- return nullptr ;
484- }
485-
486- *multiplier = con->get_integer_as_long (bt);
487- return base;
488- }
489-
490- return nullptr ;
491- }
492-
493- // Try to match `(a << CON1) + (a << CON2)`. On success, return `a` and set `(1 << CON1) + (1 << CON2)` as `multiplier`.
494- // Match `n` for patterns:
495- // - AddNode(LShiftNode(a, CON), LShiftNode(a, CON)/a)
496- // - AddNode(LShiftNode(a, CON)/a, LShiftNode(a, CON))
497- // given that lhs is different from rhs.
498- // Note that one of the term of the addition could simply be `a` (i.e., a << 0). Calling this function with `multiplier`
499- // being null is safe.
500- Node* AddNode::find_power_of_two_addition_pattern (Node* n, BasicType bt, jlong* multiplier) {
501- if (n->Opcode () == Op_Add (bt) && n->in (1 ) != n->in (2 )) {
502- Node* lhs = n->in (1 );
503- Node* rhs = n->in (2 );
504-
505- // swap LShiftNode to lhs for easier matching
506- if (lhs->Opcode () != Op_LShift (bt)) {
507- swap (lhs, rhs);
508- }
509-
510- // AddNode(LShiftNode(a, CON), *)?
511- if (lhs->Opcode () != Op_LShift (bt) || !lhs->in (2 )->is_Con ()) {
512- return nullptr ;
513- }
514-
515- jlong lhs_multiplier = 0 ;
516- if (multiplier != nullptr ) {
517- Node* con = lhs->in (2 );
518- if (con->is_top ()) {
519- return nullptr ;
520- }
521-
522- lhs_multiplier = (jlong) 1 << con->get_int ();
523- }
524-
525- // AddNode(LShiftNode(a, CON), a)?
526- if (lhs->in (1 ) == rhs) {
527- if (multiplier != nullptr ) {
528- *multiplier = lhs_multiplier + 1 ;
529- }
530-
531- return rhs;
532- }
533-
534- // AddNode(LShiftNode(a, CON), LShiftNode(a, CON2))?
535- if (rhs->Opcode () == Op_LShift (bt) && lhs->in (1 ) == rhs->in (1 ) && rhs->in (2 )->is_Con ()) {
536- if (multiplier != nullptr ) {
537- Node* con = rhs->in (2 );
538- if (con->is_top ()) {
539- return nullptr ;
540- }
541-
542- *multiplier = lhs_multiplier + ((jlong) 1 << con->get_int ());
543- }
544-
545- return lhs->in (1 );
546- }
547- return nullptr ;
548- }
549- return nullptr ;
550- }
551401
552402Node* AddINode::Ideal (PhaseGVN* phase, bool can_reshape) {
553403 Node* in1 = in (1 );
0 commit comments