1 /*
2 * Copyright (c) 1998, 2016, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
368 xphi = stride;
369 stride = tmp;
370 }
371 if (xphi->Opcode() == Op_CastII) {
372 xphi = xphi->in(1);
373 }
374 // Stride must be constant
375 int stride_con = stride->get_int();
376 if (stride_con == 0)
377 return false; // missed some peephole opt
378
379 if (!xphi->is_Phi())
380 return false; // Too much math on the trip counter
381 if (phi_incr != NULL && phi_incr != xphi)
382 return false;
383 PhiNode *phi = xphi->as_Phi();
384
385 // Phi must be of loop header; backedge must wrap to increment
386 if (phi->region() != x)
387 return false;
388 if (trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr ||
389 trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1) {
390 return false;
391 }
392 Node *init_trip = phi->in(LoopNode::EntryControl);
393
394 // If iv trunc type is smaller than int, check for possible wrap.
395 if (!TypeInt::INT->higher_equal(iv_trunc_t)) {
396 assert(trunc1 != NULL, "must have found some truncation");
397
398 // Get a better type for the phi (filtered thru if's)
399 const TypeInt* phi_ft = filtered_type(phi);
400
401 // Can iv take on a value that will wrap?
402 //
403 // Ensure iv's limit is not within "stride" of the wrap value.
404 //
405 // Example for "short" type
406 // Truncation ensures value is in the range -32768..32767 (iv_trunc_t)
407 // If the stride is +10, then the last value of the induction
408 // variable before the increment (phi_ft->_hi) must be
409 // <= 32767 - 10 and (phi_ft->_lo) must be >= -32768 to
413 if (iv_trunc_t->_hi - phi_ft->_hi < stride_con ||
414 iv_trunc_t->_lo > phi_ft->_lo) {
415 return false; // truncation may occur
416 }
417 } else if (stride_con < 0) {
418 if (iv_trunc_t->_lo - phi_ft->_lo > stride_con ||
419 iv_trunc_t->_hi < phi_ft->_hi) {
420 return false; // truncation may occur
421 }
422 }
423 // No possibility of wrap so truncation can be discarded
424 // Promote iv type to Int
425 } else {
426 assert(trunc1 == NULL && trunc2 == NULL, "no truncation for int");
427 }
428
429 // If the condition is inverted and we will be rolling
430 // through MININT to MAXINT, then bail out.
431 if (bt == BoolTest::eq || // Bail out, but this loop trips at most twice!
432 // Odd stride
433 bt == BoolTest::ne && stride_con != 1 && stride_con != -1 ||
434 // Count down loop rolls through MAXINT
435 (bt == BoolTest::le || bt == BoolTest::lt) && stride_con < 0 ||
436 // Count up loop rolls through MININT
437 (bt == BoolTest::ge || bt == BoolTest::gt) && stride_con > 0) {
438 return false; // Bail out
439 }
440
441 const TypeInt* init_t = gvn->type(init_trip)->is_int();
442 const TypeInt* limit_t = gvn->type(limit)->is_int();
443
444 if (stride_con > 0) {
445 jlong init_p = (jlong)init_t->_lo + stride_con;
446 if (init_p > (jlong)max_jint || init_p > (jlong)limit_t->_hi)
447 return false; // cyclic loop or this loop trips only once
448 } else {
449 jlong init_p = (jlong)init_t->_hi + stride_con;
450 if (init_p < (jlong)min_jint || init_p < (jlong)limit_t->_lo)
451 return false; // cyclic loop or this loop trips only once
452 }
453
454 if (phi_incr != NULL) {
455 // check if there is a possiblity of IV overflowing after the first increment
456 if (stride_con > 0) {
457 if (init_t->_hi > max_jint - stride_con) {
|
1 /*
2 * Copyright (c) 1998, 2017, Oracle and/or its affiliates. All rights reserved.
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4 *
5 * This code is free software; you can redistribute it and/or modify it
6 * under the terms of the GNU General Public License version 2 only, as
7 * published by the Free Software Foundation.
8 *
9 * This code is distributed in the hope that it will be useful, but WITHOUT
10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
12 * version 2 for more details (a copy is included in the LICENSE file that
13 * accompanied this code).
14 *
15 * You should have received a copy of the GNU General Public License version
16 * 2 along with this work; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
18 *
19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
20 * or visit www.oracle.com if you need additional information or have any
21 * questions.
22 *
368 xphi = stride;
369 stride = tmp;
370 }
371 if (xphi->Opcode() == Op_CastII) {
372 xphi = xphi->in(1);
373 }
374 // Stride must be constant
375 int stride_con = stride->get_int();
376 if (stride_con == 0)
377 return false; // missed some peephole opt
378
379 if (!xphi->is_Phi())
380 return false; // Too much math on the trip counter
381 if (phi_incr != NULL && phi_incr != xphi)
382 return false;
383 PhiNode *phi = xphi->as_Phi();
384
385 // Phi must be of loop header; backedge must wrap to increment
386 if (phi->region() != x)
387 return false;
388 if ((trunc1 == NULL && phi->in(LoopNode::LoopBackControl) != incr) ||
389 (trunc1 != NULL && phi->in(LoopNode::LoopBackControl) != trunc1)) {
390 return false;
391 }
392 Node *init_trip = phi->in(LoopNode::EntryControl);
393
394 // If iv trunc type is smaller than int, check for possible wrap.
395 if (!TypeInt::INT->higher_equal(iv_trunc_t)) {
396 assert(trunc1 != NULL, "must have found some truncation");
397
398 // Get a better type for the phi (filtered thru if's)
399 const TypeInt* phi_ft = filtered_type(phi);
400
401 // Can iv take on a value that will wrap?
402 //
403 // Ensure iv's limit is not within "stride" of the wrap value.
404 //
405 // Example for "short" type
406 // Truncation ensures value is in the range -32768..32767 (iv_trunc_t)
407 // If the stride is +10, then the last value of the induction
408 // variable before the increment (phi_ft->_hi) must be
409 // <= 32767 - 10 and (phi_ft->_lo) must be >= -32768 to
413 if (iv_trunc_t->_hi - phi_ft->_hi < stride_con ||
414 iv_trunc_t->_lo > phi_ft->_lo) {
415 return false; // truncation may occur
416 }
417 } else if (stride_con < 0) {
418 if (iv_trunc_t->_lo - phi_ft->_lo > stride_con ||
419 iv_trunc_t->_hi < phi_ft->_hi) {
420 return false; // truncation may occur
421 }
422 }
423 // No possibility of wrap so truncation can be discarded
424 // Promote iv type to Int
425 } else {
426 assert(trunc1 == NULL && trunc2 == NULL, "no truncation for int");
427 }
428
429 // If the condition is inverted and we will be rolling
430 // through MININT to MAXINT, then bail out.
431 if (bt == BoolTest::eq || // Bail out, but this loop trips at most twice!
432 // Odd stride
433 (bt == BoolTest::ne && stride_con != 1 && stride_con != -1) ||
434 // Count down loop rolls through MAXINT
435 ((bt == BoolTest::le || bt == BoolTest::lt) && stride_con < 0) ||
436 // Count up loop rolls through MININT
437 ((bt == BoolTest::ge || bt == BoolTest::gt) && stride_con > 0)) {
438 return false; // Bail out
439 }
440
441 const TypeInt* init_t = gvn->type(init_trip)->is_int();
442 const TypeInt* limit_t = gvn->type(limit)->is_int();
443
444 if (stride_con > 0) {
445 jlong init_p = (jlong)init_t->_lo + stride_con;
446 if (init_p > (jlong)max_jint || init_p > (jlong)limit_t->_hi)
447 return false; // cyclic loop or this loop trips only once
448 } else {
449 jlong init_p = (jlong)init_t->_hi + stride_con;
450 if (init_p < (jlong)min_jint || init_p < (jlong)limit_t->_lo)
451 return false; // cyclic loop or this loop trips only once
452 }
453
454 if (phi_incr != NULL) {
455 // check if there is a possiblity of IV overflowing after the first increment
456 if (stride_con > 0) {
457 if (init_t->_hi > max_jint - stride_con) {
|