@@ -1121,18 +1121,18 @@ bool IdealLoopTree::policy_range_check(PhaseIdealLoop* phase, bool provisional,
1121
1121
continue ; // not RC
1122
1122
}
1123
1123
Node *cmp = bol->in (1 );
1124
- Node *rc_exp = cmp->in (1 );
1125
- Node *limit = cmp->in (2 );
1126
1124
1127
1125
if (provisional) {
1128
1126
// Try to pattern match with either cmp inputs, do not check
1129
1127
// whether one of the inputs is loop independent as it may not
1130
1128
// have had a chance to be hoisted yet.
1131
- if (!phase->is_scaled_iv_plus_offset (cmp->in (1 ), trip_counter, NULL , NULL , bt ) &&
1132
- !phase->is_scaled_iv_plus_offset (cmp->in (2 ), trip_counter, NULL , NULL , bt )) {
1129
+ if (!phase->is_scaled_iv_plus_offset (cmp->in (1 ), trip_counter, bt , NULL , NULL ) &&
1130
+ !phase->is_scaled_iv_plus_offset (cmp->in (2 ), trip_counter, bt , NULL , NULL )) {
1133
1131
continue ;
1134
1132
}
1135
1133
} else {
1134
+ Node *rc_exp = cmp->in (1 );
1135
+ Node *limit = cmp->in (2 );
1136
1136
Node *limit_c = phase->get_ctrl (limit);
1137
1137
if (limit_c == phase->C ->top ()) {
1138
1138
return false ; // Found dead test on live IF? No RCE!
@@ -1147,7 +1147,7 @@ bool IdealLoopTree::policy_range_check(PhaseIdealLoop* phase, bool provisional,
1147
1147
}
1148
1148
}
1149
1149
1150
- if (!phase->is_scaled_iv_plus_offset (rc_exp, trip_counter, NULL , NULL , bt )) {
1150
+ if (!phase->is_scaled_iv_plus_offset (rc_exp, trip_counter, bt , NULL , NULL )) {
1151
1151
continue ;
1152
1152
}
1153
1153
}
@@ -2522,134 +2522,236 @@ void PhaseIdealLoop::add_constraint(jlong stride_con, jlong scale_con, Node* off
2522
2522
}
2523
2523
}
2524
2524
2525
+ // ----------------------------------is_iv------------------------------------
2526
+ // Return true if exp is the value (of type bt) of the given induction var.
2527
+ // This grammar of cases is recognized, where X is I|L according to bt:
2528
+ // VIV[iv] = iv | (CastXX VIV[iv]) | (ConvI2X VIV[iv])
2525
2529
bool PhaseIdealLoop::is_iv (Node* exp, Node* iv, BasicType bt) {
2526
- if (exp == iv) {
2530
+ exp = exp ->uncast ();
2531
+ if (exp == iv && iv->bottom_type ()->isa_integer (bt)) {
2527
2532
return true ;
2528
2533
}
2529
2534
2530
- if (bt == T_LONG && iv->bottom_type ()->isa_int () && exp ->Opcode () == Op_ConvI2L && exp ->in (1 ) == iv) {
2535
+ if (bt == T_LONG && iv->bottom_type ()->isa_int () && exp ->Opcode () == Op_ConvI2L && exp ->in (1 )-> uncast () == iv) {
2531
2536
return true ;
2532
2537
}
2533
2538
return false ;
2534
2539
}
2535
2540
2536
2541
// ------------------------------is_scaled_iv---------------------------------
2537
- // Return true if exp is a constant times an induction var
2538
- bool PhaseIdealLoop::is_scaled_iv (Node* exp, Node* iv, jlong* p_scale, BasicType bt, bool * converted) {
2539
- exp = exp ->uncast ();
2540
- assert (bt == T_INT || bt == T_LONG, " unexpected int type" );
2541
- if (is_iv (exp , iv, bt)) {
2542
+ // Return true if exp is a constant times the given induction var (of type bt).
2543
+ // The multiplication is either done in full precision (exactly of type bt),
2544
+ // or else bt is T_LONG but iv is scaled using 32-bit arithmetic followed by a ConvI2L.
2545
+ // This grammar of cases is recognized, where X is I|L according to bt:
2546
+ // SIV[iv] = VIV[iv] | (CastXX SIV[iv])
2547
+ // | (MulX VIV[iv] ConX) | (MulX ConX VIV[iv])
2548
+ // | (LShiftX VIV[iv] ConI)
2549
+ // | (ConvI2L SIV[iv]) -- a "short-scale" can occur here; note recursion
2550
+ // | (SubX 0 SIV[iv]) -- same as MulX(iv, -scale); note recursion
2551
+ // VIV[iv] = [either iv or its value converted; see is_iv() above]
2552
+ // On success, the constant scale value is stored back to *p_scale.
2553
+ // The value (*p_short_scale) reports if such a ConvI2L conversion was present.
2554
+ bool PhaseIdealLoop::is_scaled_iv (Node* exp, Node* iv, BasicType bt, jlong* p_scale, bool * p_short_scale, int depth) {
2555
+ BasicType exp_bt = bt;
2556
+ exp = exp ->uncast (); // strip casts
2557
+ assert (exp_bt == T_INT || exp_bt == T_LONG, " unexpected int type" );
2558
+ if (is_iv (exp , iv, exp_bt)) {
2542
2559
if (p_scale != NULL ) {
2543
2560
*p_scale = 1 ;
2544
2561
}
2562
+ if (p_short_scale != NULL ) {
2563
+ *p_short_scale = false ;
2564
+ }
2545
2565
return true ;
2546
2566
}
2547
- if (bt == T_LONG && iv->bottom_type ()->isa_int () && exp ->Opcode () == Op_ConvI2L) {
2567
+ if (exp_bt == T_LONG && iv->bottom_type ()->isa_int () && exp ->Opcode () == Op_ConvI2L) {
2548
2568
exp = exp ->in (1 );
2549
- bt = T_INT;
2550
- if (converted != NULL ) {
2551
- *converted = true ;
2552
- }
2569
+ exp_bt = T_INT;
2553
2570
}
2554
2571
int opc = exp ->Opcode ();
2572
+ int which = 0 ; // this is which subexpression we find the iv in
2555
2573
// Can't use is_Mul() here as it's true for AndI and AndL
2556
- if (opc == Op_Mul (bt)) {
2557
- if (is_iv (exp ->in (1 )->uncast (), iv, bt) && exp ->in (2 )->is_Con ()) {
2574
+ if (opc == Op_Mul (exp_bt)) {
2575
+ if ((is_iv (exp ->in (which = 1 ), iv, exp_bt) && exp ->in (2 )->is_Con ()) ||
2576
+ (is_iv (exp ->in (which = 2 ), iv, exp_bt) && exp ->in (1 )->is_Con ())) {
2577
+ Node* factor = exp ->in (which == 1 ? 2 : 1 ); // the other argument
2578
+ jlong scale = factor->find_integer_as_long (exp_bt, 0 );
2579
+ if (scale == 0 ) {
2580
+ return false ; // might be top
2581
+ }
2558
2582
if (p_scale != NULL ) {
2559
- *p_scale = exp ->in (2 )->get_integer_as_long (bt);
2583
+ *p_scale = scale;
2584
+ }
2585
+ if (p_short_scale != NULL ) {
2586
+ // (ConvI2L (MulI iv K)) can be 64-bit linear if iv is kept small enough...
2587
+ *p_short_scale = (exp_bt != bt && scale != 1 );
2560
2588
}
2561
2589
return true ;
2562
2590
}
2563
- if (is_iv (exp ->in (2 )->uncast (), iv, bt) && exp ->in (1 )->is_Con ()) {
2591
+ } else if (opc == Op_LShift (exp_bt)) {
2592
+ if (is_iv (exp ->in (1 ), iv, exp_bt) && exp ->in (2 )->is_Con ()) {
2593
+ jint shift_amount = exp ->in (2 )->find_int_con (min_jint);
2594
+ if (shift_amount == min_jint) {
2595
+ return false ; // might be top
2596
+ }
2597
+ jlong scale;
2598
+ if (exp_bt == T_INT) {
2599
+ scale = java_shift_left ((jint)1 , (juint)shift_amount);
2600
+ } else if (exp_bt == T_LONG) {
2601
+ scale = java_shift_left ((jlong)1 , (julong)shift_amount);
2602
+ }
2564
2603
if (p_scale != NULL ) {
2565
- *p_scale = exp ->in (1 )->get_integer_as_long (bt);
2604
+ *p_scale = scale;
2605
+ }
2606
+ if (p_short_scale != NULL ) {
2607
+ // (ConvI2L (MulI iv K)) can be 64-bit linear if iv is kept small enough...
2608
+ *p_short_scale = (exp_bt != bt && scale != 1 );
2566
2609
}
2567
2610
return true ;
2568
2611
}
2569
- } else if (opc == Op_LShift (bt)) {
2570
- if (is_iv (exp ->in (1 )->uncast (), iv, bt) && exp ->in (2 )->is_Con ()) {
2612
+ } else if (opc == Op_Sub (exp_bt) &&
2613
+ exp ->in (1 )->find_integer_as_long (exp_bt, -1 ) == 0 ) {
2614
+ jlong scale = 0 ;
2615
+ if (depth == 0 && is_scaled_iv (exp ->in (2 ), iv, exp_bt, &scale, p_short_scale, depth + 1 )) {
2616
+ // SubX(0, iv*K) => iv*(-K)
2617
+ if (scale == min_signed_integer (exp_bt)) {
2618
+ // This should work even if -K overflows, but let's not.
2619
+ return false ;
2620
+ }
2621
+ scale = java_multiply (scale, (jlong)-1 );
2571
2622
if (p_scale != NULL ) {
2572
- jint shift_amount = exp ->in (2 )->get_int ();
2573
- if (bt == T_INT) {
2574
- *p_scale = java_shift_left ((jint)1 , (juint)shift_amount);
2575
- } else if (bt == T_LONG) {
2576
- *p_scale = java_shift_left ((jlong)1 , (julong)shift_amount);
2577
- }
2623
+ *p_scale = scale;
2624
+ }
2625
+ if (p_short_scale != NULL ) {
2626
+ // (ConvI2L (MulI iv K)) can be 64-bit linear if iv is kept small enough...
2627
+ *p_short_scale = *p_short_scale || (exp_bt != bt && scale != 1 );
2578
2628
}
2579
2629
return true ;
2580
2630
}
2581
2631
}
2632
+ // We could also recognize (iv*K1)*K2, even with overflow, but let's not.
2582
2633
return false ;
2583
2634
}
2584
2635
2585
- // -----------------------------is_scaled_iv_plus_offset------------------------------
2586
- // Return true if exp is a simple induction variable expression: k1*iv + (invar + k2)
2587
- bool PhaseIdealLoop::is_scaled_iv_plus_offset (Node* exp, Node* iv, jlong* p_scale, Node** p_offset, BasicType bt, bool * converted, int depth) {
2636
+ // -------------------------is_scaled_iv_plus_offset--------------------------
2637
+ // Return true if exp is a simple linear transform of the given induction var.
2638
+ // The scale must be constant and the addition tree (if any) must be simple.
2639
+ // This grammar of cases is recognized, where X is I|L according to bt:
2640
+ //
2641
+ // OIV[iv] = SIV[iv] | (CastXX OIV[iv])
2642
+ // | (AddX SIV[iv] E) | (AddX E SIV[iv])
2643
+ // | (SubX SIV[iv] E) | (SubX E SIV[iv])
2644
+ // SSIV[iv] = (ConvI2X SIV[iv]) -- a "short scale" might occur here
2645
+ // SIV[iv] = [a possibly scaled value of iv; see is_scaled_iv() above]
2646
+ //
2647
+ // On success, the constant scale value is stored back to *p_scale unless null.
2648
+ // Likewise, the addend (perhaps a synthetic AddX node) is stored to *p_offset.
2649
+ // Also, (*p_short_scale) reports if a ConvI2L conversion was seen after a MulI,
2650
+ // meaning bt is T_LONG but iv was scaled using 32-bit arithmetic.
2651
+ // To avoid looping, the match is depth-limited, and so may fail to match the grammar to complex expressions.
2652
+ bool PhaseIdealLoop::is_scaled_iv_plus_offset (Node* exp, Node* iv, BasicType bt, jlong* p_scale, Node** p_offset, bool * p_short_scale, int depth) {
2588
2653
assert (bt == T_INT || bt == T_LONG, " unexpected int type" );
2589
- if (is_scaled_iv (exp , iv, p_scale, bt, converted)) {
2654
+ jlong scale = 0 ; // to catch result from is_scaled_iv()
2655
+ BasicType exp_bt = bt;
2656
+ exp = exp ->uncast ();
2657
+ if (is_scaled_iv (exp , iv, exp_bt, &scale, p_short_scale)) {
2658
+ if (p_scale != NULL ) {
2659
+ *p_scale = scale;
2660
+ }
2590
2661
if (p_offset != NULL ) {
2591
- Node *zero = _igvn.integercon ( 0 , bt);
2662
+ Node *zero = _igvn.zerocon ( bt);
2592
2663
set_ctrl (zero, C->root ());
2593
2664
*p_offset = zero;
2594
2665
}
2595
2666
return true ;
2596
2667
}
2597
- exp = exp ->uncast ();
2668
+ if (exp_bt != bt) {
2669
+ // We would now be matching inputs like (ConvI2L exp:(AddI (MulI iv S) E)).
2670
+ // It's hard to make 32-bit arithmetic linear if it overflows. Although we do
2671
+ // cope with overflowing multiplication by S, it would be even more work to
2672
+ // handle overflowing addition of E. So we bail out here on ConvI2L input.
2673
+ return false ;
2674
+ }
2598
2675
int opc = exp ->Opcode ();
2599
- if (opc == Op_Add (bt)) {
2600
- if (is_scaled_iv (exp ->in (1 ), iv, p_scale, bt, converted)) {
2676
+ int which = 0 ; // this is which subexpression we find the iv in
2677
+ Node* offset = NULL ;
2678
+ if (opc == Op_Add (exp_bt)) {
2679
+ // Check for a scaled IV in (AddX (MulX iv S) E) or (AddX E (MulX iv S)).
2680
+ if (is_scaled_iv (exp ->in (which = 1 ), iv, bt, &scale, p_short_scale) ||
2681
+ is_scaled_iv (exp ->in (which = 2 ), iv, bt, &scale, p_short_scale)) {
2682
+ offset = exp ->in (which == 1 ? 2 : 1 ); // the other argument
2683
+ if (p_scale != NULL ) {
2684
+ *p_scale = scale;
2685
+ }
2601
2686
if (p_offset != NULL ) {
2602
- *p_offset = exp -> in ( 2 ) ;
2687
+ *p_offset = offset ;
2603
2688
}
2604
2689
return true ;
2605
2690
}
2606
- if (is_scaled_iv (exp ->in (2 ), iv, p_scale, bt, converted)) {
2607
- if (p_offset != NULL ) {
2608
- *p_offset = exp ->in (1 );
2609
- }
2691
+ // Check for more addends, like (AddX (AddX (MulX iv S) E1) E2), etc.
2692
+ if (is_scaled_iv_plus_extra_offset (exp ->in (1 ), exp ->in (2 ), iv, bt, p_scale, p_offset, p_short_scale, depth) ||
2693
+ is_scaled_iv_plus_extra_offset (exp ->in (2 ), exp ->in (1 ), iv, bt, p_scale, p_offset, p_short_scale, depth)) {
2610
2694
return true ;
2611
2695
}
2612
- if (exp -> in ( 2 )-> is_Con ( )) {
2613
- Node* offset2 = NULL ;
2614
- if (depth < 2 &&
2615
- is_scaled_iv_plus_offset ( exp -> in ( 1 ), iv, p_scale,
2616
- p_offset != NULL ? &offset2 : NULL , bt, converted, depth+ 1 )) {
2617
- if (p_offset != NULL ) {
2618
- Node *ctrl_off2 = get_ctrl (offset2);
2619
- Node* offset = AddNode::make (offset2, exp -> in ( 2 ), bt);
2620
- register_new_node (offset, ctrl_off2);
2621
- *p_offset = offset;
2696
+ } else if (opc == Op_Sub (exp_bt )) {
2697
+ if ( is_scaled_iv ( exp -> in (which = 1 ), iv, bt, &scale, p_short_scale) ||
2698
+ is_scaled_iv ( exp -> in (which = 2 ), iv, bt, &scale, p_short_scale)) {
2699
+ // Match (SubX SIV[iv] E) as if (AddX SIV[iv] (SubX 0 E)), and
2700
+ // match (SubX E SIV[iv]) as if (AddX E (SubX 0 SIV[iv])).
2701
+ offset = exp -> in (which == 1 ? 2 : 1 ); // the other argument
2702
+ if (which == 2 ) {
2703
+ // We can't handle a scale of min_jint (or min_jlong) here as -1 * min_jint = min_jint
2704
+ if (scale == min_signed_integer (bt)) {
2705
+ return false ; // cannot negate the scale of the iv
2622
2706
}
2623
- return true ;
2707
+ scale = java_multiply (scale, (jlong)- 1 ) ;
2624
2708
}
2625
- }
2626
- } else if (opc == Op_Sub (bt)) {
2627
- if (is_scaled_iv (exp ->in (1 ), iv, p_scale, bt, converted)) {
2628
- if (p_offset != NULL ) {
2629
- Node *zero = _igvn.integercon (0 , bt);
2630
- set_ctrl (zero, C->root ());
2631
- Node *ctrl_off = get_ctrl (exp ->in (2 ));
2632
- Node* offset = SubNode::make (zero, exp ->in (2 ), bt);
2633
- register_new_node (offset, ctrl_off);
2634
- *p_offset = offset;
2709
+ if (p_scale != NULL ) {
2710
+ *p_scale = scale;
2635
2711
}
2636
- return true ;
2637
- }
2638
- if (is_scaled_iv (exp ->in (2 ), iv, p_scale, bt, converted)) {
2639
2712
if (p_offset != NULL ) {
2640
- // We can't handle a scale of min_jint (or min_jlong) here as -1 * min_jint = min_jint
2641
- if (*p_scale == min_signed_integer (bt)) {
2642
- return false ;
2713
+ if (which == 1 ) { // must negate the extracted offset
2714
+ Node *zero = _igvn.integercon (0 , exp_bt);
2715
+ set_ctrl (zero, C->root ());
2716
+ Node *ctrl_off = get_ctrl (offset);
2717
+ offset = SubNode::make (zero, offset, exp_bt);
2718
+ register_new_node (offset, ctrl_off);
2643
2719
}
2644
- *p_scale *= -1 ;
2645
- *p_offset = exp ->in (1 );
2720
+ *p_offset = offset;
2646
2721
}
2647
2722
return true ;
2648
2723
}
2649
2724
}
2650
2725
return false ;
2651
2726
}
2652
2727
2728
+ // Helper for is_scaled_iv_plus_offset(), not called separately.
2729
+ // The caller encountered (AddX exp1 offset3) or (AddX offset3 exp1).
2730
+ // Here, exp1 is inspected to see if it is a simple linear transform of iv.
2731
+ // If so, the offset3 is combined with any other offset2 from inside exp1.
2732
+ bool PhaseIdealLoop::is_scaled_iv_plus_extra_offset (Node* exp1, Node* offset3, Node* iv,
2733
+ BasicType bt,
2734
+ jlong* p_scale, Node** p_offset,
2735
+ bool * p_short_scale, int depth) {
2736
+ // By the time we reach here, it is unlikely that exp1 is a simple iv*K.
2737
+ // If is a linear iv transform, it is probably an add or subtract.
2738
+ // Let's collect the internal offset2 from it.
2739
+ Node* offset2 = NULL ;
2740
+ if (offset3->is_Con () &&
2741
+ depth < 2 &&
2742
+ is_scaled_iv_plus_offset (exp1, iv, bt, p_scale,
2743
+ &offset2, p_short_scale, depth+1 )) {
2744
+ if (p_offset != NULL ) {
2745
+ Node* ctrl_off2 = get_ctrl (offset2);
2746
+ Node* offset = AddNode::make (offset2, offset3, bt);
2747
+ register_new_node (offset, ctrl_off2);
2748
+ *p_offset = offset;
2749
+ }
2750
+ return true ;
2751
+ }
2752
+ return false ;
2753
+ }
2754
+
2653
2755
// Same as PhaseIdealLoop::duplicate_predicates() but for range checks
2654
2756
// eliminated by iteration splitting.
2655
2757
Node* PhaseIdealLoop::add_range_check_predicate (IdealLoopTree* loop, CountedLoopNode* cl,
0 commit comments