520 peeled_dom_test_elim(loop,old_new); |
520 peeled_dom_test_elim(loop,old_new); |
521 |
521 |
522 loop->record_for_igvn(); |
522 loop->record_for_igvn(); |
523 } |
523 } |
524 |
524 |
|
525 #define EMPTY_LOOP_SIZE 7 // number of nodes in an empty loop |
|
526 |
525 //------------------------------policy_maximally_unroll------------------------ |
527 //------------------------------policy_maximally_unroll------------------------ |
526 // Return exact loop trip count, or 0 if not maximally unrolling |
528 // Calculate exact loop trip count and return true if loop can be maximally |
|
529 // unrolled. |
527 bool IdealLoopTree::policy_maximally_unroll( PhaseIdealLoop *phase ) const { |
530 bool IdealLoopTree::policy_maximally_unroll( PhaseIdealLoop *phase ) const { |
528 CountedLoopNode *cl = _head->as_CountedLoop(); |
531 CountedLoopNode *cl = _head->as_CountedLoop(); |
529 assert(cl->is_normal_loop(), ""); |
532 assert(cl->is_normal_loop(), ""); |
|
533 if (!cl->is_valid_counted_loop()) |
|
534 return false; // Malformed counted loop |
530 |
535 |
531 Node *init_n = cl->init_trip(); |
536 Node *init_n = cl->init_trip(); |
532 Node *limit_n = cl->limit(); |
537 Node *limit_n = cl->limit(); |
533 |
538 |
534 // Non-constant bounds |
539 // Non-constant bounds |
535 if (init_n == NULL || !init_n->is_Con() || |
540 if (init_n == NULL || !init_n->is_Con() || |
536 limit_n == NULL || !limit_n->is_Con() || |
541 limit_n == NULL || !limit_n->is_Con()) { |
537 // protect against stride not being a constant |
|
538 !cl->stride_is_con()) { |
|
539 return false; |
542 return false; |
540 } |
543 } |
541 int init = init_n->get_int(); |
544 // Use longs to avoid integer overflow. |
542 int limit = limit_n->get_int(); |
545 int stride_con = cl->stride_con(); |
543 int span = limit - init; |
546 long init_con = cl->init_trip()->get_int(); |
544 int stride = cl->stride_con(); |
547 long limit_con = cl->limit()->get_int(); |
545 |
548 int stride_m = stride_con - (stride_con > 0 ? 1 : -1); |
546 if (init >= limit || stride > span) { |
549 long trip_cnt = (limit_con - init_con + stride_m)/stride_con; |
|
550 |
|
551 // Note, max_jint is used to indicate unknown trip count. |
|
552 if (trip_cnt <= 0 || trip_cnt >= (long)max_jint) { |
547 // return a false (no maximally unroll) and the regular unroll/peel |
553 // return a false (no maximally unroll) and the regular unroll/peel |
548 // route will make a small mess which CCP will fold away. |
554 // route will make a small mess which CCP will fold away. |
549 return false; |
555 return false; |
550 } |
556 } |
551 uint trip_count = span/stride; // trip_count can be greater than 2 Gig. |
557 uint trip_count = (uint)trip_cnt; |
552 assert( (int)trip_count*stride == span, "must divide evenly" ); |
558 cl->set_trip_count(trip_count); |
553 |
559 |
554 // Real policy: if we maximally unroll, does it get too big? |
560 // Real policy: if we maximally unroll, does it get too big? |
555 // Allow the unrolled mess to get larger than standard loop |
561 // Allow the unrolled mess to get larger than standard loop |
556 // size. After all, it will no longer be a loop. |
562 // size. After all, it will no longer be a loop. |
557 uint body_size = _body.size(); |
563 uint body_size = _body.size(); |
558 uint unroll_limit = (uint)LoopUnrollLimit * 4; |
564 uint unroll_limit = (uint)LoopUnrollLimit * 4; |
559 assert( (intx)unroll_limit == LoopUnrollLimit * 4, "LoopUnrollLimit must fit in 32bits"); |
565 assert( (intx)unroll_limit == LoopUnrollLimit * 4, "LoopUnrollLimit must fit in 32bits"); |
560 cl->set_trip_count(trip_count); |
|
561 if (trip_count > unroll_limit || body_size > unroll_limit) { |
566 if (trip_count > unroll_limit || body_size > unroll_limit) { |
|
567 return false; |
|
568 } |
|
569 |
|
570 // Take into account that after unroll conjoined heads and tails will fold, |
|
571 // otherwise policy_unroll() may allow more unrolling than max unrolling. |
|
572 uint new_body_size = EMPTY_LOOP_SIZE + (body_size - EMPTY_LOOP_SIZE) * trip_count; |
|
573 uint tst_body_size = (new_body_size - EMPTY_LOOP_SIZE) / trip_count + EMPTY_LOOP_SIZE; |
|
574 if (body_size != tst_body_size) // Check for int overflow |
|
575 return false; |
|
576 if (new_body_size > unroll_limit || |
|
577 // Unrolling can result in a large amount of node construction |
|
578 new_body_size >= MaxNodeLimit - phase->C->unique()) { |
562 return false; |
579 return false; |
563 } |
580 } |
564 |
581 |
565 // Currently we don't have policy to optimize one iteration loops. |
582 // Currently we don't have policy to optimize one iteration loops. |
566 // Maximally unrolling transformation is used for that: |
583 // Maximally unrolling transformation is used for that: |
567 // it is peeled and the original loop become non reachable (dead). |
584 // it is peeled and the original loop become non reachable (dead). |
568 if (trip_count == 1) |
585 // Also fully unroll a loop with few iterations regardless next |
|
586 // conditions since following loop optimizations will split |
|
587 // such loop anyway (pre-main-post). |
|
588 if (trip_count <= 3) |
569 return true; |
589 return true; |
570 |
590 |
571 // Do not unroll a loop with String intrinsics code. |
591 // Do not unroll a loop with String intrinsics code. |
572 // String intrinsics are large and have loops. |
592 // String intrinsics are large and have loops. |
573 for (uint k = 0; k < _body.size(); k++) { |
593 for (uint k = 0; k < _body.size(); k++) { |
602 bool IdealLoopTree::policy_unroll( PhaseIdealLoop *phase ) const { |
612 bool IdealLoopTree::policy_unroll( PhaseIdealLoop *phase ) const { |
603 |
613 |
604 CountedLoopNode *cl = _head->as_CountedLoop(); |
614 CountedLoopNode *cl = _head->as_CountedLoop(); |
605 assert(cl->is_normal_loop() || cl->is_main_loop(), ""); |
615 assert(cl->is_normal_loop() || cl->is_main_loop(), ""); |
606 |
616 |
607 // protect against stride not being a constant |
617 if (!cl->is_valid_counted_loop()) |
608 if (!cl->stride_is_con()) return false; |
618 return false; // Malformed counted loop |
609 |
619 |
610 // protect against over-unrolling |
620 // protect against over-unrolling |
611 if (cl->trip_count() <= 1) return false; |
621 if (cl->trip_count() <= 1) return false; |
|
622 |
|
623 // Check for stride being a small enough constant |
|
624 if (abs(cl->stride_con()) > (1<<3)) return false; |
612 |
625 |
613 int future_unroll_ct = cl->unrolled_count() * 2; |
626 int future_unroll_ct = cl->unrolled_count() * 2; |
614 |
627 |
615 // Don't unroll if the next round of unrolling would push us |
628 // Don't unroll if the next round of unrolling would push us |
616 // over the expected trip count of the loop. One is subtracted |
629 // over the expected trip count of the loop. One is subtracted |