@@ -96,11 +96,11 @@ void IdealLoopTree::record_for_igvn() {
96
96
// ------------------------------compute_exact_trip_count-----------------------
97
97
// Compute loop trip count if possible. Do not recalculate trip count for
98
98
// split loops (pre-main-post) which have their limits and inits behind Opaque node.
99
- void IdealLoopTree::compute_trip_count (PhaseIdealLoop* phase) {
100
- if (!_head->as_Loop ()->is_valid_counted_loop (T_INT )) {
99
+ void IdealLoopTree::compute_trip_count (PhaseIdealLoop* phase, BasicType loop_bt ) {
100
+ if (!_head->as_Loop ()->is_valid_counted_loop (loop_bt )) {
101
101
return ;
102
102
}
103
- CountedLoopNode * cl = _head->as_CountedLoop ();
103
+ BaseCountedLoopNode * cl = _head->as_BaseCountedLoop ();
104
104
// Trip count may become nonexact for iteration split loops since
105
105
// RCE modifies limits. Note, _trip_count value is not reset since
106
106
// it is used to limit unrolling of main loop.
@@ -119,24 +119,62 @@ void IdealLoopTree::compute_trip_count(PhaseIdealLoop* phase) {
119
119
Node* init_n = cl->init_trip ();
120
120
Node* limit_n = cl->limit ();
121
121
if (init_n != nullptr && limit_n != nullptr ) {
122
- // Use longs to avoid integer overflow.
123
- int stride_con = cl->stride_con ();
124
- const TypeInt* init_type = phase->_igvn .type (init_n)->is_int ();
125
- const TypeInt* limit_type = phase->_igvn .type (limit_n)->is_int ();
126
- jlong init_con = (stride_con > 0 ) ? init_type->_lo : init_type->_hi ;
127
- jlong limit_con = (stride_con > 0 ) ? limit_type->_hi : limit_type->_lo ;
128
- int stride_m = stride_con - (stride_con > 0 ? 1 : -1 );
129
- jlong trip_count = (limit_con - init_con + stride_m)/stride_con;
122
+ jlong stride_con = cl->stride_con ();
123
+ const TypeInteger* init_type = phase->_igvn .type (init_n)->is_integer (loop_bt);
124
+ const TypeInteger* limit_type = phase->_igvn .type (limit_n)->is_integer (loop_bt);
125
+
126
+ // compute trip count
127
+ // It used to be computed as:
128
+ // max(1, limit_con - init_con + stride_m) / stride_con
129
+ // with stride_m = stride_con - (stride_con > 0 ? 1 : -1)
130
+ // for int counted loops only and by promoting all values to long to avoid overflow
131
+ // This implements the computation for int and long counted loops in a way that promotion to the next larger integer
132
+ // type is not needed to protect against overflow.
133
+ //
134
+ // Use unsigned longs to avoid overflow: number of iteration is a positive number but can be really large for
135
+ // instance if init_con = min_jint, limit_con = max_jint
136
+ jlong init_con = (stride_con > 0 ) ? init_type->lo_as_long () : init_type->hi_as_long ();
137
+ julong uinit_con = init_con;
138
+ jlong limit_con = (stride_con > 0 ) ? limit_type->hi_as_long () : limit_type->lo_as_long ();
139
+ julong ulimit_con = limit_con;
130
140
// The loop body is always executed at least once even if init >= limit (for stride_con > 0) or
131
141
// init <= limit (for stride_con < 0).
132
- trip_count = MAX2 (trip_count, (jlong)1 );
133
- if (trip_count < (jlong)max_juint) {
142
+ julong udiff = 1 ;
143
+ if (stride_con > 0 && limit_con > init_con) {
144
+ udiff = ulimit_con - uinit_con;
145
+ } else if (stride_con < 0 && limit_con < init_con) {
146
+ udiff = uinit_con - ulimit_con;
147
+ }
148
+ // The loop runs for one more iteration if the limit is (stride > 0 in this example):
149
+ // init + k * stride + small_value, 0 < small_value < stride
150
+ julong utrip_count = udiff / ABS (stride_con);
151
+ if (utrip_count * ABS (stride_con) != udiff) {
152
+ // Guaranteed to not overflow because it can only happen for ABS(stride) > 1 in which case, utrip_count can't be
153
+ // max_juint/max_julong
154
+ utrip_count++;
155
+ }
156
+
157
+ #ifdef ASSERT
158
+ if (loop_bt == T_INT) {
159
+ // Use longs to avoid integer overflow.
160
+ jlong init_con = (stride_con > 0 ) ? init_type->is_int ()->_lo : init_type->is_int ()->_hi ;
161
+ jlong limit_con = (stride_con > 0 ) ? limit_type->is_int ()->_hi : limit_type->is_int ()->_lo ;
162
+ int stride_m = stride_con - (stride_con > 0 ? 1 : -1 );
163
+ jlong trip_count = (limit_con - init_con + stride_m) / stride_con;
164
+ // The loop body is always executed at least once even if init >= limit (for stride_con > 0) or
165
+ // init <= limit (for stride_con < 0).
166
+ trip_count = MAX2 (trip_count, (jlong)1 );
167
+ assert (checked_cast<juint>(trip_count) == checked_cast<juint>(utrip_count), " incorrect trip count computation" );
168
+ }
169
+ #endif
170
+
171
+ if (utrip_count < max_unsigned_integer (loop_bt)) {
134
172
if (init_n->is_Con () && limit_n->is_Con ()) {
135
173
// Set exact trip count.
136
- cl->set_exact_trip_count ((uint)trip_count );
137
- } else if (cl ->unrolled_count () == 1 ) {
174
+ cl->set_exact_trip_count (utrip_count );
175
+ } else if (loop_bt == T_LONG || cl-> as_CountedLoop () ->unrolled_count () == 1 ) {
138
176
// Set maximum trip count before unrolling.
139
- cl->set_trip_count ((uint)trip_count );
177
+ cl->set_trip_count (utrip_count );
140
178
}
141
179
}
142
180
}
@@ -1851,7 +1889,7 @@ void PhaseIdealLoop::do_unroll(IdealLoopTree *loop, Node_List &old_new, bool adj
1851
1889
#ifndef PRODUCT
1852
1890
if (TraceLoopOpts) {
1853
1891
if (loop_head->trip_count () < (uint)LoopUnrollLimit) {
1854
- tty->print (" Unroll %d(%2d ) " , loop_head->unrolled_count ()*2 , loop_head->trip_count ());
1892
+ tty->print (" Unroll %d(" JULONG_FORMAT_W ( 2 ) " ) " , loop_head->unrolled_count ()*2 , loop_head->trip_count ());
1855
1893
} else {
1856
1894
tty->print (" Unroll %d " , loop_head->unrolled_count ()*2 );
1857
1895
}
@@ -2104,7 +2142,7 @@ void PhaseIdealLoop::do_maximally_unroll(IdealLoopTree *loop, Node_List &old_new
2104
2142
assert (cl->trip_count () > 0 , " " );
2105
2143
#ifndef PRODUCT
2106
2144
if (TraceLoopOpts) {
2107
- tty->print (" MaxUnroll %d " , cl->trip_count ());
2145
+ tty->print (" MaxUnroll " JULONG_FORMAT " " , cl->trip_count ());
2108
2146
loop->dump_head ();
2109
2147
}
2110
2148
#endif
@@ -3359,7 +3397,7 @@ bool IdealLoopTree::iteration_split_impl(PhaseIdealLoop *phase, Node_List &old_n
3359
3397
return false ;
3360
3398
}
3361
3399
// Compute loop trip count if possible.
3362
- compute_trip_count (phase);
3400
+ compute_trip_count (phase, T_INT );
3363
3401
3364
3402
// Convert one-iteration loop into normal code.
3365
3403
if (do_one_iteration_loop (phase)) {
0 commit comments