src/hotspot/share/opto/loopTransform.cpp
author phedlin
Wed, 17 Apr 2019 14:52:25 +0200
changeset 54698 3ebf58dbf5d8
parent 54022 ff399127078a
child 54699 1a5305f51bfd
permissions -rw-r--r--
8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'. Reviewed-by: vlivanov
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     1
/*
54022
ff399127078a 8217561: X86: Add floating-point Math.min/max intrinsics
bsrbnd
parents: 53306
diff changeset
     2
 * Copyright (c) 2000, 2019, Oracle and/or its affiliates. All rights reserved.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
     3
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     4
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     5
 * This code is free software; you can redistribute it and/or modify it
489c9b5090e2 Initial load
duke
parents:
diff changeset
     6
 * under the terms of the GNU General Public License version 2 only, as
489c9b5090e2 Initial load
duke
parents:
diff changeset
     7
 * published by the Free Software Foundation.
489c9b5090e2 Initial load
duke
parents:
diff changeset
     8
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
     9
 * This code is distributed in the hope that it will be useful, but WITHOUT
489c9b5090e2 Initial load
duke
parents:
diff changeset
    10
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
489c9b5090e2 Initial load
duke
parents:
diff changeset
    11
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
489c9b5090e2 Initial load
duke
parents:
diff changeset
    12
 * version 2 for more details (a copy is included in the LICENSE file that
489c9b5090e2 Initial load
duke
parents:
diff changeset
    13
 * accompanied this code).
489c9b5090e2 Initial load
duke
parents:
diff changeset
    14
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    15
 * You should have received a copy of the GNU General Public License version
489c9b5090e2 Initial load
duke
parents:
diff changeset
    16
 * 2 along with this work; if not, write to the Free Software Foundation,
489c9b5090e2 Initial load
duke
parents:
diff changeset
    17
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    18
 *
5547
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5054
diff changeset
    19
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5054
diff changeset
    20
 * or visit www.oracle.com if you need additional information or have any
f4b087cbb361 6941466: Oracle rebranding changes for Hotspot repositories
trims
parents: 5054
diff changeset
    21
 * questions.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    22
 *
489c9b5090e2 Initial load
duke
parents:
diff changeset
    23
 */
489c9b5090e2 Initial load
duke
parents:
diff changeset
    24
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    25
#include "precompiled.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    26
#include "compiler/compileLog.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    27
#include "memory/allocation.inline.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    28
#include "opto/addnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    29
#include "opto/callnode.hpp"
27708
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
    30
#include "opto/castnode.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    31
#include "opto/connode.hpp"
23528
8f1a7f5e8066 8001532: C2 node files refactoring
morris
parents: 23491
diff changeset
    32
#include "opto/convertnode.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    33
#include "opto/divnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    34
#include "opto/loopnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    35
#include "opto/mulnode.hpp"
23528
8f1a7f5e8066 8001532: C2 node files refactoring
morris
parents: 23491
diff changeset
    36
#include "opto/movenode.hpp"
8f1a7f5e8066 8001532: C2 node files refactoring
morris
parents: 23491
diff changeset
    37
#include "opto/opaquenode.hpp"
7397
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    38
#include "opto/rootnode.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    39
#include "opto/runtime.hpp"
5b173b4ca846 6989984: Use standard include model for Hospot
stefank
parents: 6770
diff changeset
    40
#include "opto/subnode.hpp"
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
    41
#include "opto/superword.hpp"
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
    42
#include "opto/vectornode.hpp"
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    43
489c9b5090e2 Initial load
duke
parents:
diff changeset
    44
//------------------------------is_loop_exit-----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    45
// Given an IfNode, return the loop-exiting projection or NULL if both
489c9b5090e2 Initial load
duke
parents:
diff changeset
    46
// arms remain in the loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    47
Node *IdealLoopTree::is_loop_exit(Node *iff) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    48
  if( iff->outcnt() != 2 ) return NULL; // Ignore partially dead tests
489c9b5090e2 Initial load
duke
parents:
diff changeset
    49
  PhaseIdealLoop *phase = _phase;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    50
  // Test is an IfNode, has 2 projections.  If BOTH are in the loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
    51
  // we need loop unswitching instead of peeling.
489c9b5090e2 Initial load
duke
parents:
diff changeset
    52
  if( !is_member(phase->get_loop( iff->raw_out(0) )) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
    53
    return iff->raw_out(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    54
  if( !is_member(phase->get_loop( iff->raw_out(1) )) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
    55
    return iff->raw_out(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    56
  return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
    57
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    58
489c9b5090e2 Initial load
duke
parents:
diff changeset
    59
489c9b5090e2 Initial load
duke
parents:
diff changeset
    60
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
    61
489c9b5090e2 Initial load
duke
parents:
diff changeset
    62
489c9b5090e2 Initial load
duke
parents:
diff changeset
    63
//------------------------------record_for_igvn----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
    64
// Put loop body on igvn work list
489c9b5090e2 Initial load
duke
parents:
diff changeset
    65
void IdealLoopTree::record_for_igvn() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    66
  for( uint i = 0; i < _body.size(); i++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
    67
    Node *n = _body.at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    68
    _phase->_igvn._worklist.push(n);
489c9b5090e2 Initial load
duke
parents:
diff changeset
    69
  }
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
    70
  // put body of outer strip mined loop on igvn work list as well
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
    71
  if (_head->is_CountedLoop() && _head->as_Loop()->is_strip_mined()) {
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
    72
    CountedLoopNode* l = _head->as_CountedLoop();
49600
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    73
    Node* outer_loop = l->outer_loop();
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    74
    assert(outer_loop != NULL, "missing piece of strip mined loop");
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    75
    _phase->_igvn._worklist.push(outer_loop);
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    76
    Node* outer_loop_tail = l->outer_loop_tail();
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    77
    assert(outer_loop_tail != NULL, "missing piece of strip mined loop");
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    78
    _phase->_igvn._worklist.push(outer_loop_tail);
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    79
    Node* outer_loop_end = l->outer_loop_end();
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    80
    assert(outer_loop_end != NULL, "missing piece of strip mined loop");
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    81
    _phase->_igvn._worklist.push(outer_loop_end);
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    82
    Node* outer_safepoint = l->outer_safepoint();
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    83
    assert(outer_safepoint != NULL, "missing piece of strip mined loop");
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    84
    _phase->_igvn._worklist.push(outer_safepoint);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
    85
    Node* cle_out = _head->as_CountedLoop()->loopexit()->proj_out(false);
49600
d7c83c8e4e65 8197931: Null pointer dereference in Unique_Node_List::push of node.hpp:1510
roland
parents: 49487
diff changeset
    86
    assert(cle_out != NULL, "missing piece of strip mined loop");
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
    87
    _phase->_igvn._worklist.push(cle_out);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
    88
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
    89
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
    90
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
    91
//------------------------------compute_exact_trip_count-----------------------
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
    92
// Compute loop trip count if possible. Do not recalculate trip count for
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
    93
// split loops (pre-main-post) which have their limits and inits behind Opaque node.
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
    94
void IdealLoopTree::compute_trip_count(PhaseIdealLoop* phase) {
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
    95
  if (!_head->as_Loop()->is_valid_counted_loop()) {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
    96
    return;
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
    97
  }
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
    98
  CountedLoopNode* cl = _head->as_CountedLoop();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
    99
  // Trip count may become nonexact for iteration split loops since
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   100
  // RCE modifies limits. Note, _trip_count value is not reset since
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   101
  // it is used to limit unrolling of main loop.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   102
  cl->set_nonexact_trip_count();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   103
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   104
  // Loop's test should be part of loop.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   105
  if (!phase->is_member(this, phase->get_ctrl(cl->loopexit()->in(CountedLoopEndNode::TestValue))))
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   106
    return; // Infinite loop
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   107
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   108
#ifdef ASSERT
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   109
  BoolTest::mask bt = cl->loopexit()->test_trip();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   110
  assert(bt == BoolTest::lt || bt == BoolTest::gt ||
10005
ce4ea81a1329 7052494: Eclipse test fails on JDK 7 b142
kvn
parents: 9955
diff changeset
   111
         bt == BoolTest::ne, "canonical test is expected");
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   112
#endif
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   113
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   114
  Node* init_n = cl->init_trip();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   115
  Node* limit_n = cl->limit();
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   116
  if (init_n != NULL && limit_n != NULL) {
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   117
    // Use longs to avoid integer overflow.
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   118
    int stride_con = cl->stride_con();
50262
131a631b271a 8202949: C2: assert(false) failed: Bad graph detected in build_loop_late
thartmann
parents: 49600
diff changeset
   119
    const TypeInt* init_type = phase->_igvn.type(init_n)->is_int();
131a631b271a 8202949: C2: assert(false) failed: Bad graph detected in build_loop_late
thartmann
parents: 49600
diff changeset
   120
    const TypeInt* limit_type = phase->_igvn.type(limit_n)->is_int();
131a631b271a 8202949: C2: assert(false) failed: Bad graph detected in build_loop_late
thartmann
parents: 49600
diff changeset
   121
    jlong init_con = (stride_con > 0) ? init_type->_lo : init_type->_hi;
131a631b271a 8202949: C2: assert(false) failed: Bad graph detected in build_loop_late
thartmann
parents: 49600
diff changeset
   122
    jlong limit_con = (stride_con > 0) ? limit_type->_hi : limit_type->_lo;
131a631b271a 8202949: C2: assert(false) failed: Bad graph detected in build_loop_late
thartmann
parents: 49600
diff changeset
   123
    int stride_m = stride_con - (stride_con > 0 ? 1 : -1);
13967
bf7c8dfb5121 8000313: C2 should use jlong for 64bit values
vlivanov
parents: 13895
diff changeset
   124
    jlong trip_count = (limit_con - init_con + stride_m)/stride_con;
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   125
    if (trip_count > 0 && (julong)trip_count < (julong)max_juint) {
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   126
      if (init_n->is_Con() && limit_n->is_Con()) {
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   127
        // Set exact trip count.
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   128
        cl->set_exact_trip_count((uint)trip_count);
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   129
      } else if (cl->unrolled_count() == 1) {
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   130
        // Set maximum trip count before unrolling.
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   131
        cl->set_trip_count((uint)trip_count);
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
   132
      }
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   133
    }
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   134
  }
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   135
}
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   136
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   137
//------------------------------compute_profile_trip_cnt----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   138
// Compute loop trip count from profile data as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   139
//    (backedge_count + loop_exit_count) / loop_exit_count
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   140
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   141
float IdealLoopTree::compute_profile_trip_cnt_helper(Node* n) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   142
  if (n->is_If()) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   143
    IfNode *iff = n->as_If();
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   144
    if (iff->_fcnt != COUNT_UNKNOWN && iff->_prob != PROB_UNKNOWN) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   145
      Node *exit = is_loop_exit(iff);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   146
      if (exit) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   147
        float exit_prob = iff->_prob;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   148
        if (exit->Opcode() == Op_IfFalse) exit_prob = 1.0 - exit_prob;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   149
        if (exit_prob > PROB_MIN) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   150
          float exit_cnt = iff->_fcnt * exit_prob;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   151
          return exit_cnt;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   152
        }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   153
      }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   154
    }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   155
  }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   156
  if (n->is_Jump()) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   157
    JumpNode *jmp = n->as_Jump();
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   158
    if (jmp->_fcnt != COUNT_UNKNOWN) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   159
      float* probs = jmp->_probs;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   160
      float exit_prob = 0;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   161
      PhaseIdealLoop *phase = _phase;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   162
      for (DUIterator_Fast imax, i = jmp->fast_outs(imax); i < imax; i++) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   163
        JumpProjNode* u = jmp->fast_out(i)->as_JumpProj();
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   164
        if (!is_member(_phase->get_loop(u))) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   165
          exit_prob += probs[u->_con];
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   166
        }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   167
      }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   168
      return exit_prob * jmp->_fcnt;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   169
    }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   170
  }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   171
  return 0;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   172
}
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   173
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   174
void IdealLoopTree::compute_profile_trip_cnt(PhaseIdealLoop *phase) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   175
  if (!_head->is_Loop()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   176
    return;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   177
  }
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   178
  LoopNode* head = _head->as_Loop();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   179
  if (head->profile_trip_cnt() != COUNT_UNKNOWN) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   180
    return; // Already computed
489c9b5090e2 Initial load
duke
parents:
diff changeset
   181
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   182
  float trip_cnt = (float)max_jint; // default is big
489c9b5090e2 Initial load
duke
parents:
diff changeset
   183
489c9b5090e2 Initial load
duke
parents:
diff changeset
   184
  Node* back = head->in(LoopNode::LoopBackControl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   185
  while (back != head) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   186
    if ((back->Opcode() == Op_IfTrue || back->Opcode() == Op_IfFalse) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   187
        back->in(0) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   188
        back->in(0)->is_If() &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   189
        back->in(0)->as_If()->_fcnt != COUNT_UNKNOWN &&
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   190
        back->in(0)->as_If()->_prob != PROB_UNKNOWN &&
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   191
        (back->Opcode() == Op_IfTrue ? 1-back->in(0)->as_If()->_prob : back->in(0)->as_If()->_prob) > PROB_MIN) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   192
      break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   193
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   194
    back = phase->idom(back);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   195
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   196
  if (back != head) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   197
    assert((back->Opcode() == Op_IfTrue || back->Opcode() == Op_IfFalse) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   198
           back->in(0), "if-projection exists");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   199
    IfNode* back_if = back->in(0)->as_If();
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   200
    float loop_back_cnt = back_if->_fcnt * (back->Opcode() == Op_IfTrue ? back_if->_prob : (1 - back_if->_prob));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   201
489c9b5090e2 Initial load
duke
parents:
diff changeset
   202
    // Now compute a loop exit count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   203
    float loop_exit_cnt = 0.0f;
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   204
    if (_child == NULL) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   205
      for( uint i = 0; i < _body.size(); i++ ) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   206
        Node *n = _body[i];
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   207
        loop_exit_cnt += compute_profile_trip_cnt_helper(n);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   208
      }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   209
    } else {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   210
      ResourceMark rm;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   211
      Unique_Node_List wq;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   212
      wq.push(back);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   213
      for (uint i = 0; i < wq.size(); i++) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   214
        Node *n = wq.at(i);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   215
        assert(n->is_CFG(), "only control nodes");
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   216
        if (n != head) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   217
          if (n->is_Region()) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   218
            for (uint j = 1; j < n->req(); j++) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   219
              wq.push(n->in(j));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   220
            }
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   221
          } else {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   222
            loop_exit_cnt += compute_profile_trip_cnt_helper(n);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   223
            wq.push(n->in(0));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   224
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   225
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   226
      }
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   227
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   228
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   229
    if (loop_exit_cnt > 0.0f) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   230
      trip_cnt = (loop_back_cnt + loop_exit_cnt) / loop_exit_cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   231
    } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   232
      // No exit count so use
489c9b5090e2 Initial load
duke
parents:
diff changeset
   233
      trip_cnt = loop_back_cnt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   234
    }
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   235
  } else {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
   236
    head->mark_profile_trip_failed();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   237
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   238
#ifndef PRODUCT
489c9b5090e2 Initial load
duke
parents:
diff changeset
   239
  if (TraceProfileTripCount) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   240
    tty->print_cr("compute_profile_trip_cnt  lp: %d cnt: %f\n", head->_idx, trip_cnt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   241
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   242
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   243
  head->set_profile_trip_cnt(trip_cnt);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   244
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   245
489c9b5090e2 Initial load
duke
parents:
diff changeset
   246
//---------------------is_invariant_addition-----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   247
// Return nonzero index of invariant operand for an Add or Sub
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1500
diff changeset
   248
// of (nonconstant) invariant and variant values. Helper for reassociate_invariants.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   249
int IdealLoopTree::is_invariant_addition(Node* n, PhaseIdealLoop *phase) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   250
  int op = n->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   251
  if (op == Op_AddI || op == Op_SubI) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   252
    bool in1_invar = this->is_invariant(n->in(1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   253
    bool in2_invar = this->is_invariant(n->in(2));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   254
    if (in1_invar && !in2_invar) return 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   255
    if (!in1_invar && in2_invar) return 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   256
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   257
  return 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   258
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   259
489c9b5090e2 Initial load
duke
parents:
diff changeset
   260
//---------------------reassociate_add_sub-----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   261
// Reassociate invariant add and subtract expressions:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   262
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   263
// inv1 + (x + inv2)  =>  ( inv1 + inv2) + x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   264
// (x + inv2) + inv1  =>  ( inv1 + inv2) + x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   265
// inv1 + (x - inv2)  =>  ( inv1 - inv2) + x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   266
// inv1 - (inv2 - x)  =>  ( inv1 - inv2) + x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   267
// (x + inv2) - inv1  =>  (-inv1 + inv2) + x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   268
// (x - inv2) + inv1  =>  ( inv1 - inv2) + x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   269
// (x - inv2) - inv1  =>  (-inv1 - inv2) + x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   270
// inv1 + (inv2 - x)  =>  ( inv1 + inv2) - x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   271
// inv1 - (x - inv2)  =>  ( inv1 + inv2) - x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   272
// (inv2 - x) + inv1  =>  ( inv1 + inv2) - x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   273
// (inv2 - x) - inv1  =>  (-inv1 + inv2) - x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   274
// inv1 - (x + inv2)  =>  ( inv1 - inv2) - x
489c9b5090e2 Initial load
duke
parents:
diff changeset
   275
//
489c9b5090e2 Initial load
duke
parents:
diff changeset
   276
Node* IdealLoopTree::reassociate_add_sub(Node* n1, PhaseIdealLoop *phase) {
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 44314
diff changeset
   277
  if ((!n1->is_Add() && !n1->is_Sub()) || n1->outcnt() == 0) return NULL;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   278
  if (is_invariant(n1)) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   279
  int inv1_idx = is_invariant_addition(n1, phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   280
  if (!inv1_idx) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   281
  // Don't mess with add of constant (igvn moves them to expression tree root.)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   282
  if (n1->is_Add() && n1->in(2)->is_Con()) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   283
  Node* inv1 = n1->in(inv1_idx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   284
  Node* n2 = n1->in(3 - inv1_idx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   285
  int inv2_idx = is_invariant_addition(n2, phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   286
  if (!inv2_idx) return NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   287
  Node* x    = n2->in(3 - inv2_idx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   288
  Node* inv2 = n2->in(inv2_idx);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   289
489c9b5090e2 Initial load
duke
parents:
diff changeset
   290
  bool neg_x    = n2->is_Sub() && inv2_idx == 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   291
  bool neg_inv2 = n2->is_Sub() && inv2_idx == 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   292
  bool neg_inv1 = n1->is_Sub() && inv1_idx == 2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   293
  if (n1->is_Sub() && inv1_idx == 1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   294
    neg_x    = !neg_x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   295
    neg_inv2 = !neg_inv2;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   296
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   297
  Node* inv1_c = phase->get_ctrl(inv1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   298
  Node* inv2_c = phase->get_ctrl(inv2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   299
  Node* n_inv1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   300
  if (neg_inv1) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   301
    Node *zero = phase->_igvn.intcon(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   302
    phase->set_ctrl(zero, phase->C->root());
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
   303
    n_inv1 = new SubINode(zero, inv1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   304
    phase->register_new_node(n_inv1, inv1_c);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   305
  } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   306
    n_inv1 = inv1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   307
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   308
  Node* inv;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   309
  if (neg_inv2) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
   310
    inv = new SubINode(n_inv1, inv2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   311
  } else {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
   312
    inv = new AddINode(n_inv1, inv2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   313
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   314
  phase->register_new_node(inv, phase->get_early_ctrl(inv));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   315
489c9b5090e2 Initial load
duke
parents:
diff changeset
   316
  Node* addx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   317
  if (neg_x) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
   318
    addx = new SubINode(inv, x);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   319
  } else {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
   320
    addx = new AddINode(x, inv);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   321
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   322
  phase->register_new_node(addx, phase->get_ctrl(x));
5901
c046f8e9c52b 6677629: PhaseIterGVN::subsume_node() should call hash_delete() and add_users_to_worklist()
kvn
parents: 5547
diff changeset
   323
  phase->_igvn.replace_node(n1, addx);
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   324
  assert(phase->get_loop(phase->get_ctrl(n1)) == this, "");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   325
  _body.yank(n1);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   326
  return addx;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   327
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   328
489c9b5090e2 Initial load
duke
parents:
diff changeset
   329
//---------------------reassociate_invariants-----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   330
// Reassociate invariant expressions:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   331
void IdealLoopTree::reassociate_invariants(PhaseIdealLoop *phase) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   332
  for (int i = _body.size() - 1; i >= 0; i--) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   333
    Node *n = _body.at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   334
    for (int j = 0; j < 5; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   335
      Node* nn = reassociate_add_sub(n, phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   336
      if (nn == NULL) break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   337
      n = nn; // again
489c9b5090e2 Initial load
duke
parents:
diff changeset
   338
    };
489c9b5090e2 Initial load
duke
parents:
diff changeset
   339
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   340
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   341
489c9b5090e2 Initial load
duke
parents:
diff changeset
   342
//------------------------------policy_peeling---------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   343
// Return TRUE or FALSE if the loop should be peeled or not.  Peel if we can
489c9b5090e2 Initial load
duke
parents:
diff changeset
   344
// make some loop-invariant test (usually a null-check) happen before the loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   345
bool IdealLoopTree::policy_peeling( PhaseIdealLoop *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   346
  Node *test = ((IdealLoopTree*)this)->tail();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   347
  int  body_size = ((IdealLoopTree*)this)->_body.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   348
  // Peeling does loop cloning which can result in O(N^2) node construction
489c9b5090e2 Initial load
duke
parents:
diff changeset
   349
  if( body_size > 255 /* Prevent overflow for large body_size */
27707
f7d26e5b8b5d 8058148: MaxNodeLimit and LiveNodeCountInliningCutoff
vlivanov
parents: 25927
diff changeset
   350
      || (body_size * body_size + phase->C->live_nodes()) > phase->C->max_node_limit() ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   351
    return false;           // too large to safely clone
489c9b5090e2 Initial load
duke
parents:
diff changeset
   352
  }
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   353
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   354
  // check for vectorized loops, any peeling done was already applied
54698
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   355
  if (_head->is_CountedLoop()) {
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   356
    CountedLoopNode* cl = _head->as_CountedLoop();
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   357
    if (cl->is_unroll_only() || cl->trip_count() == 1) {
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   358
      return false;
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   359
    }
53167
030429d6baac 8215044: C2 crash in loopTransform.cpp with assert(cl->trip_count() > 0) failed: peeling a fully unrolled loop
roland
parents: 53063
diff changeset
   360
  }
030429d6baac 8215044: C2 crash in loopTransform.cpp with assert(cl->trip_count() > 0) failed: peeling a fully unrolled loop
roland
parents: 53063
diff changeset
   361
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   362
  while( test != _head ) {      // Scan till run off top of loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   363
    if( test->is_If() ) {       // Test?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   364
      Node *ctrl = phase->get_ctrl(test->in(1));
489c9b5090e2 Initial load
duke
parents:
diff changeset
   365
      if (ctrl->is_top())
489c9b5090e2 Initial load
duke
parents:
diff changeset
   366
        return false;           // Found dead test on live IF?  No peeling!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   367
      // Standard IF only has one input value to check for loop invariance
34164
a9e6034d7707 8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents: 33628
diff changeset
   368
      assert(test->Opcode() == Op_If || test->Opcode() == Op_CountedLoopEnd || test->Opcode() == Op_RangeCheck, "Check this code when new subtype is added");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   369
      // Condition is not a member of this loop?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   370
      if( !is_member(phase->get_loop(ctrl)) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   371
          is_loop_exit(test) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   372
        return true;            // Found reason to peel!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   373
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   374
    // Walk up dominators to loop _head looking for test which is
489c9b5090e2 Initial load
duke
parents:
diff changeset
   375
    // executed on every path thru loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   376
    test = phase->idom(test);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   377
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   378
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   379
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   380
489c9b5090e2 Initial load
duke
parents:
diff changeset
   381
//------------------------------peeled_dom_test_elim---------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   382
// If we got the effect of peeling, either by actually peeling or by making
489c9b5090e2 Initial load
duke
parents:
diff changeset
   383
// a pre-loop which must execute at least once, we can remove all
489c9b5090e2 Initial load
duke
parents:
diff changeset
   384
// loop-invariant dominated tests in the main body.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   385
void PhaseIdealLoop::peeled_dom_test_elim( IdealLoopTree *loop, Node_List &old_new ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   386
  bool progress = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   387
  while( progress ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   388
    progress = false;           // Reset for next iteration
489c9b5090e2 Initial load
duke
parents:
diff changeset
   389
    Node *prev = loop->_head->in(LoopNode::LoopBackControl);//loop->tail();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   390
    Node *test = prev->in(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   391
    while( test != loop->_head ) { // Scan till run off top of loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   392
489c9b5090e2 Initial load
duke
parents:
diff changeset
   393
      int p_op = prev->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   394
      if( (p_op == Op_IfFalse || p_op == Op_IfTrue) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   395
          test->is_If() &&      // Test?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   396
          !test->in(1)->is_Con() && // And not already obvious?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   397
          // Condition is not a member of this loop?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   398
          !loop->is_member(get_loop(get_ctrl(test->in(1))))){
489c9b5090e2 Initial load
duke
parents:
diff changeset
   399
        // Walk loop body looking for instances of this test
489c9b5090e2 Initial load
duke
parents:
diff changeset
   400
        for( uint i = 0; i < loop->_body.size(); i++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   401
          Node *n = loop->_body.at(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   402
          if( n->is_If() && n->in(1) == test->in(1) /*&& n != loop->tail()->in(0)*/ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   403
            // IfNode was dominated by version in peeled loop body
489c9b5090e2 Initial load
duke
parents:
diff changeset
   404
            progress = true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   405
            dominated_by( old_new[prev->_idx], n );
489c9b5090e2 Initial load
duke
parents:
diff changeset
   406
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   407
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   408
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   409
      prev = test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   410
      test = idom(test);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   411
    } // End of scan tests in loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   412
489c9b5090e2 Initial load
duke
parents:
diff changeset
   413
  } // End of while( progress )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   414
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   415
489c9b5090e2 Initial load
duke
parents:
diff changeset
   416
//------------------------------do_peeling-------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   417
// Peel the first iteration of the given loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   418
// Step 1: Clone the loop body.  The clone becomes the peeled iteration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   419
//         The pre-loop illegally has 2 control users (old & new loops).
489c9b5090e2 Initial load
duke
parents:
diff changeset
   420
// Step 2: Make the old-loop fall-in edges point to the peeled iteration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   421
//         Do this by making the old-loop fall-in edges act as if they came
489c9b5090e2 Initial load
duke
parents:
diff changeset
   422
//         around the loopback from the prior iteration (follow the old-loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   423
//         backedges) and then map to the new peeled iteration.  This leaves
489c9b5090e2 Initial load
duke
parents:
diff changeset
   424
//         the pre-loop with only 1 user (the new peeled iteration), but the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   425
//         peeled-loop backedge has 2 users.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   426
// Step 3: Cut the backedge on the clone (so its not a loop) and remove the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   427
//         extra backedge user.
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   428
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   429
//                   orig
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   430
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   431
//                  stmt1
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   432
//                    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   433
//                    v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   434
//              loop predicate
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   435
//                    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   436
//                    v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   437
//                   loop<----+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   438
//                     |      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   439
//                   stmt2    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   440
//                     |      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   441
//                     v      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   442
//                    if      ^
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   443
//                   / \      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   444
//                  /   \     |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   445
//                 v     v    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   446
//               false true   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   447
//               /       \    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   448
//              /         ----+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   449
//             |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   450
//             v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   451
//           exit
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   452
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   453
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   454
//            after clone loop
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   455
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   456
//                   stmt1
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   457
//                     |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   458
//                     v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   459
//               loop predicate
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   460
//                 /       \
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   461
//        clone   /         \   orig
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   462
//               /           \
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   463
//              /             \
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   464
//             v               v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   465
//   +---->loop clone          loop<----+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   466
//   |      |                    |      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   467
//   |    stmt2 clone          stmt2    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   468
//   |      |                    |      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   469
//   |      v                    v      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   470
//   ^      if clone            If      ^
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   471
//   |      / \                / \      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   472
//   |     /   \              /   \     |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   473
//   |    v     v            v     v    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   474
//   |    true  false      false true   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   475
//   |    /         \      /       \    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   476
//   +----           \    /         ----+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   477
//                    \  /
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   478
//                    1v v2
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   479
//                  region
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   480
//                     |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   481
//                     v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   482
//                   exit
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   483
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   484
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   485
//         after peel and predicate move
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   486
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   487
//                   stmt1
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   488
//                    /
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   489
//                   /
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   490
//        clone     /            orig
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   491
//                 /
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   492
//                /              +----------+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   493
//               /               |          |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   494
//              /          loop predicate   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   495
//             /                 |          |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   496
//            v                  v          |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   497
//   TOP-->loop clone          loop<----+   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   498
//          |                    |      |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   499
//        stmt2 clone          stmt2    |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   500
//          |                    |      |   ^
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   501
//          v                    v      |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   502
//          if clone            If      ^   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   503
//          / \                / \      |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   504
//         /   \              /   \     |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   505
//        v     v            v     v    |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   506
//      true   false      false  true   |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   507
//        |         \      /       \    |   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   508
//        |          \    /         ----+   ^
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   509
//        |           \  /                  |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   510
//        |           1v v2                 |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   511
//        v         region                  |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   512
//        |            |                    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   513
//        |            v                    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   514
//        |          exit                   |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   515
//        |                                 |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   516
//        +--------------->-----------------+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   517
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   518
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   519
//              final graph
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   520
//
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   521
//                  stmt1
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   522
//                    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   523
//                    v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   524
//                  stmt2 clone
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   525
//                    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   526
//                    v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   527
//                   if clone
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   528
//                  / |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   529
//                 /  |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   530
//                v   v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   531
//            false  true
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   532
//             |      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   533
//             |      v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   534
//             | loop predicate
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   535
//             |      |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   536
//             |      v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   537
//             |     loop<----+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   538
//             |      |       |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   539
//             |    stmt2     |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   540
//             |      |       |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   541
//             |      v       |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   542
//             v      if      ^
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   543
//             |     /  \     |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   544
//             |    /    \    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   545
//             |   v     v    |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   546
//             | false  true  |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   547
//             |  |        \  |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   548
//             v  v         --+
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   549
//            region
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   550
//              |
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   551
//              v
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   552
//             exit
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   553
//
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   554
void PhaseIdealLoop::do_peeling( IdealLoopTree *loop, Node_List &old_new ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   555
489c9b5090e2 Initial load
duke
parents:
diff changeset
   556
  C->set_major_progress();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   557
  // Peeling a 'main' loop in a pre/main/post situation obfuscates the
31133
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
   558
  // 'pre' loop from the main and the 'pre' can no longer have its
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   559
  // iterations adjusted.  Therefore, we need to declare this loop as
489c9b5090e2 Initial load
duke
parents:
diff changeset
   560
  // no longer a 'main' loop; it will need new pre and post loops before
489c9b5090e2 Initial load
duke
parents:
diff changeset
   561
  // we can do further RCE.
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   562
#ifndef PRODUCT
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   563
  if (TraceLoopOpts) {
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   564
    tty->print("Peel         ");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   565
    loop->dump_head();
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   566
  }
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   567
#endif
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
   568
  LoopNode* head = loop->_head->as_Loop();
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   569
  bool counted_loop = head->is_CountedLoop();
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   570
  if (counted_loop) {
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   571
    CountedLoopNode *cl = head->as_CountedLoop();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   572
    assert(cl->trip_count() > 0, "peeling a fully unrolled loop");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   573
    cl->set_trip_count(cl->trip_count() - 1);
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   574
    if (cl->is_main_loop()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   575
      cl->set_normal_loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   576
#ifndef PRODUCT
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
   577
      if (PrintOpto && VerifyLoopOptimizations) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   578
        tty->print("Peeling a 'main' loop; resetting to 'normal' ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   579
        loop->dump_head();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   580
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   581
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
   582
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   583
  }
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   584
  Node* entry = head->in(LoopNode::EntryControl);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   585
489c9b5090e2 Initial load
duke
parents:
diff changeset
   586
  // Step 1: Clone the loop body.  The clone becomes the peeled iteration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   587
  //         The pre-loop illegally has 2 control users (old & new loops).
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
   588
  clone_loop(loop, old_new, dom_depth(head->skip_strip_mined()), ControlAroundStripMined);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   589
489c9b5090e2 Initial load
duke
parents:
diff changeset
   590
  // Step 2: Make the old-loop fall-in edges point to the peeled iteration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   591
  //         Do this by making the old-loop fall-in edges act as if they came
489c9b5090e2 Initial load
duke
parents:
diff changeset
   592
  //         around the loopback from the prior iteration (follow the old-loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   593
  //         backedges) and then map to the new peeled iteration.  This leaves
489c9b5090e2 Initial load
duke
parents:
diff changeset
   594
  //         the pre-loop with only 1 user (the new peeled iteration), but the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   595
  //         peeled-loop backedge has 2 users.
10258
10c77b8c8d3e 7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents: 10011
diff changeset
   596
  Node* new_entry = old_new[head->in(LoopNode::LoopBackControl)->_idx];
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
   597
  _igvn.hash_delete(head->skip_strip_mined());
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
   598
  head->skip_strip_mined()->set_req(LoopNode::EntryControl, new_entry);
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   599
  for (DUIterator_Fast jmax, j = head->fast_outs(jmax); j < jmax; j++) {
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   600
    Node* old = head->fast_out(j);
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   601
    if (old->in(0) == loop->_head && old->req() == 3 && old->is_Phi()) {
10258
10c77b8c8d3e 7068051: SIGSEGV in PhaseIdealLoop::build_loop_late_post
kvn
parents: 10011
diff changeset
   602
      Node* new_exit_value = old_new[old->in(LoopNode::LoopBackControl)->_idx];
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   603
      if (!new_exit_value )     // Backedge value is ALSO loop invariant?
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   604
        // Then loop body backedge value remains the same.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   605
        new_exit_value = old->in(LoopNode::LoopBackControl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   606
      _igvn.hash_delete(old);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   607
      old->set_req(LoopNode::EntryControl, new_exit_value);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   608
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   609
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   610
489c9b5090e2 Initial load
duke
parents:
diff changeset
   611
489c9b5090e2 Initial load
duke
parents:
diff changeset
   612
  // Step 3: Cut the backedge on the clone (so its not a loop) and remove the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   613
  //         extra backedge user.
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   614
  Node* new_head = old_new[head->_idx];
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   615
  _igvn.hash_delete(new_head);
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   616
  new_head->set_req(LoopNode::LoopBackControl, C->top());
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   617
  for (DUIterator_Fast j2max, j2 = new_head->fast_outs(j2max); j2 < j2max; j2++) {
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   618
    Node* use = new_head->fast_out(j2);
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   619
    if (use->in(0) == new_head && use->req() == 3 && use->is_Phi()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   620
      _igvn.hash_delete(use);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   621
      use->set_req(LoopNode::LoopBackControl, C->top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
   622
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   623
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   624
489c9b5090e2 Initial load
duke
parents:
diff changeset
   625
489c9b5090e2 Initial load
duke
parents:
diff changeset
   626
  // Step 4: Correct dom-depth info.  Set to loop-head depth.
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   627
  int dd = dom_depth(head);
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
   628
  set_idom(head, head->in(1), dd);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   629
  for (uint j3 = 0; j3 < loop->_body.size(); j3++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   630
    Node *old = loop->_body.at(j3);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   631
    Node *nnn = old_new[old->_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
   632
    if (!has_ctrl(nnn))
489c9b5090e2 Initial load
duke
parents:
diff changeset
   633
      set_idom(nnn, idom(nnn), dd-1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   634
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   635
489c9b5090e2 Initial load
duke
parents:
diff changeset
   636
  // Now force out all loop-invariant dominating tests.  The optimizer
489c9b5090e2 Initial load
duke
parents:
diff changeset
   637
  // finds some, but we _know_ they are all useless.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   638
  peeled_dom_test_elim(loop,old_new);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   639
489c9b5090e2 Initial load
duke
parents:
diff changeset
   640
  loop->record_for_igvn();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   641
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   642
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   643
#define EMPTY_LOOP_SIZE 7 // number of nodes in an empty loop
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   644
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   645
//------------------------------policy_maximally_unroll------------------------
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   646
// Calculate exact loop trip count and return true if loop can be maximally
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   647
// unrolled.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   648
bool IdealLoopTree::policy_maximally_unroll( PhaseIdealLoop *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   649
  CountedLoopNode *cl = _head->as_CountedLoop();
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   650
  assert(cl->is_normal_loop(), "");
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   651
  if (!cl->is_valid_counted_loop())
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   652
    return false; // Malformed counted loop
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   653
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   654
  if (!cl->has_exact_trip_count()) {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   655
    // Trip count is not exact.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   656
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   657
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   658
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   659
  uint trip_count = cl->trip_count();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   660
  // Note, max_juint is used to indicate unknown trip count.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   661
  assert(trip_count > 1, "one iteration loop should be optimized out already");
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
   662
  assert(trip_count < max_juint, "exact trip_count should be less than max_uint.");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   663
489c9b5090e2 Initial load
duke
parents:
diff changeset
   664
  // Real policy: if we maximally unroll, does it get too big?
489c9b5090e2 Initial load
duke
parents:
diff changeset
   665
  // Allow the unrolled mess to get larger than standard loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   666
  // size.  After all, it will no longer be a loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   667
  uint body_size    = _body.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   668
  uint unroll_limit = (uint)LoopUnrollLimit * 4;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   669
  assert( (intx)unroll_limit == LoopUnrollLimit * 4, "LoopUnrollLimit must fit in 32bits");
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   670
  if (trip_count > unroll_limit || body_size > unroll_limit) {
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   671
    return false;
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   672
  }
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   673
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   674
  // Fully unroll a loop with few iterations regardless next
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   675
  // conditions since following loop optimizations will split
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   676
  // such loop anyway (pre-main-post).
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   677
  if (trip_count <= 3)
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   678
    return true;
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   679
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   680
  // Take into account that after unroll conjoined heads and tails will fold,
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   681
  // otherwise policy_unroll() may allow more unrolling than max unrolling.
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   682
  uint new_body_size = EMPTY_LOOP_SIZE + (body_size - EMPTY_LOOP_SIZE) * trip_count;
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   683
  uint tst_body_size = (new_body_size - EMPTY_LOOP_SIZE) / trip_count + EMPTY_LOOP_SIZE;
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   684
  if (body_size != tst_body_size) // Check for int overflow
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   685
    return false;
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   686
  if (new_body_size > unroll_limit ||
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   687
      // Unrolling can result in a large amount of node construction
27707
f7d26e5b8b5d 8058148: MaxNodeLimit and LiveNodeCountInliningCutoff
vlivanov
parents: 25927
diff changeset
   688
      new_body_size >= phase->C->max_node_limit() - phase->C->live_nodes()) {
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   689
    return false;
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   690
  }
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   691
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   692
  // Do not unroll a loop with String intrinsics code.
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   693
  // String intrinsics are large and have loops.
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   694
  for (uint k = 0; k < _body.size(); k++) {
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   695
    Node* n = _body.at(k);
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   696
    switch (n->Opcode()) {
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   697
      case Op_StrComp:
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   698
      case Op_StrEquals:
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   699
      case Op_StrIndexOf:
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 32202
diff changeset
   700
      case Op_StrIndexOfChar:
15242
695bb216be99 6896617: Optimize sun.nio.cs.ISO_8859_1$Encode.encodeArrayLoop() on x86
kvn
parents: 14623
diff changeset
   701
      case Op_EncodeISOArray:
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 32202
diff changeset
   702
      case Op_AryEq:
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 32202
diff changeset
   703
      case Op_HasNegatives: {
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   704
        return false;
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   705
      }
23491
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   706
#if INCLUDE_RTM_OPT
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   707
      case Op_FastLock:
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   708
      case Op_FastUnlock: {
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   709
        // Don't unroll RTM locking code because it is large.
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   710
        if (UseRTMLocking) {
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   711
          return false;
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   712
        }
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   713
      }
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   714
#endif
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   715
    } // switch
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   716
  }
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   717
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   718
  return true; // Do maximally unroll
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   719
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   720
489c9b5090e2 Initial load
duke
parents:
diff changeset
   721
489c9b5090e2 Initial load
duke
parents:
diff changeset
   722
//------------------------------policy_unroll----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   723
// Return TRUE or FALSE if the loop should be unrolled or not.  Unroll if
489c9b5090e2 Initial load
duke
parents:
diff changeset
   724
// the loop is a CountedLoop and the body is small enough.
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   725
bool IdealLoopTree::policy_unroll(PhaseIdealLoop *phase) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   726
489c9b5090e2 Initial load
duke
parents:
diff changeset
   727
  CountedLoopNode *cl = _head->as_CountedLoop();
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   728
  assert(cl->is_normal_loop() || cl->is_main_loop(), "");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   729
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   730
  if (!cl->is_valid_counted_loop())
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   731
    return false; // Malformed counted loop
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   732
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   733
  // Protect against over-unrolling.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   734
  // After split at least one iteration will be executed in pre-loop.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   735
  if (cl->trip_count() <= (uint)(cl->is_normal_loop() ? 2 : 1)) return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   736
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   737
  _local_loop_unroll_limit = LoopUnrollLimit;
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   738
  _local_loop_unroll_factor = 4;
9434
56d7158be6dc 7039652: Performance regression after 7004547 changes
kvn
parents: 9124
diff changeset
   739
  int future_unroll_ct = cl->unrolled_count() * 2;
47591
d78db2ebce5e 8187601: Unrolling more when SLP auto-vectorization failed
zyao
parents: 47216
diff changeset
   740
  if (!cl->is_vectorized_loop()) {
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   741
    if (future_unroll_ct > LoopMaxUnroll) return false;
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   742
  } else {
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   743
    // obey user constraints on vector mapped loops with additional unrolling applied
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
   744
    int unroll_constraint = (cl->slp_max_unroll()) ? cl->slp_max_unroll() : 1;
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
   745
    if ((future_unroll_ct / unroll_constraint) > LoopMaxUnroll) return false;
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   746
  }
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
   747
9434
56d7158be6dc 7039652: Performance regression after 7004547 changes
kvn
parents: 9124
diff changeset
   748
  // Check for initial stride being a small enough constant
56d7158be6dc 7039652: Performance regression after 7004547 changes
kvn
parents: 9124
diff changeset
   749
  if (abs(cl->stride_con()) > (1<<2)*future_unroll_ct) return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   750
489c9b5090e2 Initial load
duke
parents:
diff changeset
   751
  // Don't unroll if the next round of unrolling would push us
489c9b5090e2 Initial load
duke
parents:
diff changeset
   752
  // over the expected trip count of the loop.  One is subtracted
489c9b5090e2 Initial load
duke
parents:
diff changeset
   753
  // from the expected trip count because the pre-loop normally
489c9b5090e2 Initial load
duke
parents:
diff changeset
   754
  // executes 1 iteration.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   755
  if (UnrollLimitForProfileCheck > 0 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   756
      cl->profile_trip_cnt() != COUNT_UNKNOWN &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   757
      future_unroll_ct        > UnrollLimitForProfileCheck &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   758
      (float)future_unroll_ct > cl->profile_trip_cnt() - 1.0) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   759
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   760
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   761
489c9b5090e2 Initial load
duke
parents:
diff changeset
   762
  // When unroll count is greater than LoopUnrollMin, don't unroll if:
489c9b5090e2 Initial load
duke
parents:
diff changeset
   763
  //   the residual iterations are more than 10% of the trip count
489c9b5090e2 Initial load
duke
parents:
diff changeset
   764
  //   and rounds of "unroll,optimize" are not making significant progress
489c9b5090e2 Initial load
duke
parents:
diff changeset
   765
  //   Progress defined as current size less than 20% larger than previous size.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   766
  if (UseSuperWord && cl->node_count_before_unroll() > 0 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
   767
      future_unroll_ct > LoopUnrollMin &&
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
   768
      (future_unroll_ct - 1) * (100 / LoopPercentProfileLimit) > cl->profile_trip_cnt() &&
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   769
      1.2 * cl->node_count_before_unroll() < (double)_body.size()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   770
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   771
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   772
489c9b5090e2 Initial load
duke
parents:
diff changeset
   773
  Node *init_n = cl->init_trip();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   774
  Node *limit_n = cl->limit();
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   775
  int stride_con = cl->stride_con();
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 51017
diff changeset
   776
  if (limit_n == NULL) return false; // We will dereference it below.
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 51017
diff changeset
   777
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   778
  // Non-constant bounds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   779
  // Protect against over-unrolling when init or/and limit are not constant
489c9b5090e2 Initial load
duke
parents:
diff changeset
   780
  // (so that trip_count's init value is maxint) but iv range is known.
51078
fc6cfe40e32a 8207049: Minor improvements of compiler code.
goetz
parents: 51017
diff changeset
   781
  if (init_n == NULL || !init_n->is_Con() || !limit_n->is_Con()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   782
    Node* phi = cl->phi();
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   783
    if (phi != NULL) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   784
      assert(phi->is_Phi() && phi->in(0) == _head, "Counted loop should have iv phi.");
489c9b5090e2 Initial load
duke
parents:
diff changeset
   785
      const TypeInt* iv_type = phase->_igvn.type(phi)->is_int();
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   786
      int next_stride = stride_con * 2; // stride after this unroll
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   787
      if (next_stride > 0) {
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   788
        if (iv_type->_lo + next_stride <= iv_type->_lo || // overflow
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   789
            iv_type->_lo + next_stride >  iv_type->_hi) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   790
          return false;  // over-unrolling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   791
        }
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   792
      } else if (next_stride < 0) {
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   793
        if (iv_type->_hi + next_stride >= iv_type->_hi || // overflow
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   794
            iv_type->_hi + next_stride <  iv_type->_lo) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   795
          return false;  // over-unrolling
489c9b5090e2 Initial load
duke
parents:
diff changeset
   796
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   797
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   798
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   799
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   800
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   801
  // After unroll limit will be adjusted: new_limit = limit-stride.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   802
  // Bailout if adjustment overflow.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   803
  const TypeInt* limit_type = phase->_igvn.type(limit_n)->is_int();
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 44314
diff changeset
   804
  if ((stride_con > 0 && ((limit_type->_hi - stride_con) >= limit_type->_hi)) ||
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 44314
diff changeset
   805
      (stride_con < 0 && ((limit_type->_lo - stride_con) <= limit_type->_lo)))
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   806
    return false;  // overflow
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   807
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   808
  // Adjust body_size to determine if we unroll or not
489c9b5090e2 Initial load
duke
parents:
diff changeset
   809
  uint body_size = _body.size();
10538
d6bf448fa3ad 7054211: No loop unrolling done in jdk7b144 for a test update() while loop
kvn
parents: 10263
diff changeset
   810
  // Key test to unroll loop in CRC32 java code
d6bf448fa3ad 7054211: No loop unrolling done in jdk7b144 for a test update() while loop
kvn
parents: 10263
diff changeset
   811
  int xors_in_loop = 0;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   812
  // Also count ModL, DivL and MulL which expand mightly
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   813
  for (uint k = 0; k < _body.size(); k++) {
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   814
    Node* n = _body.at(k);
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   815
    switch (n->Opcode()) {
10538
d6bf448fa3ad 7054211: No loop unrolling done in jdk7b144 for a test update() while loop
kvn
parents: 10263
diff changeset
   816
      case Op_XorI: xors_in_loop++; break; // CRC32 java code
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   817
      case Op_ModL: body_size += 30; break;
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   818
      case Op_DivL: body_size += 30; break;
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   819
      case Op_MulL: body_size += 10; break;
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   820
      case Op_StrComp:
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   821
      case Op_StrEquals:
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   822
      case Op_StrIndexOf:
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 32202
diff changeset
   823
      case Op_StrIndexOfChar:
15242
695bb216be99 6896617: Optimize sun.nio.cs.ISO_8859_1$Encode.encodeArrayLoop() on x86
kvn
parents: 14623
diff changeset
   824
      case Op_EncodeISOArray:
33628
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 32202
diff changeset
   825
      case Op_AryEq:
09241459a8b8 8141132: JEP 254: Compact Strings
thartmann
parents: 32202
diff changeset
   826
      case Op_HasNegatives: {
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   827
        // Do not unroll a loop with String intrinsics code.
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   828
        // String intrinsics are large and have loops.
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   829
        return false;
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   830
      }
23491
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   831
#if INCLUDE_RTM_OPT
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   832
      case Op_FastLock:
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   833
      case Op_FastUnlock: {
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   834
        // Don't unroll RTM locking code because it is large.
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   835
        if (UseRTMLocking) {
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   836
          return false;
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   837
        }
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   838
      }
f690330b10b9 8031320: Use Intel RTM instructions for locks
kvn
parents: 22911
diff changeset
   839
#endif
8879
e6ae9a7aa72a 7029152: Ideal nodes for String intrinsics miss memory edge optimization
kvn
parents: 8870
diff changeset
   840
    } // switch
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   841
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   842
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   843
  if (UseSuperWord) {
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   844
    if (!cl->is_reduction_loop()) {
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   845
      phase->mark_reductions(this);
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   846
    }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   847
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   848
    // Only attempt slp analysis when user controls do not prohibit it
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   849
    if (LoopMaxUnroll > _local_loop_unroll_factor) {
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   850
      // Once policy_slp_analysis succeeds, mark the loop with the
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   851
      // maximal unroll factor so that we minimize analysis passes
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   852
      if (future_unroll_ct >= _local_loop_unroll_factor) {
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   853
        policy_unroll_slp_analysis(cl, phase, future_unroll_ct);
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   854
      }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   855
    }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   856
  }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   857
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   858
  int slp_max_unroll_factor = cl->slp_max_unroll();
46692
117b089cb1c3 8175096: Analyse subword in the loop to set maximum vector size
vdeshpande
parents: 46630
diff changeset
   859
  if ((LoopMaxUnroll < slp_max_unroll_factor) && FLAG_IS_DEFAULT(LoopMaxUnroll) && UseSubwordForMaxVector) {
117b089cb1c3 8175096: Analyse subword in the loop to set maximum vector size
vdeshpande
parents: 46630
diff changeset
   860
    LoopMaxUnroll = slp_max_unroll_factor;
117b089cb1c3 8175096: Analyse subword in the loop to set maximum vector size
vdeshpande
parents: 46630
diff changeset
   861
  }
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   862
  if (cl->has_passed_slp()) {
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   863
    if (slp_max_unroll_factor >= future_unroll_ct) return true;
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   864
    // Normal case: loop too big
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   865
    return false;
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   866
  }
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   867
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   868
  // Check for being too big
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   869
  if (body_size > (uint)_local_loop_unroll_limit) {
51017
dd7ce84016a5 8194740: UseSubwordForMaxVector causes performance regression
vdeshpande
parents: 50632
diff changeset
   870
    if ((cl->is_subword_loop() || xors_in_loop >= 4) && body_size < (uint)LoopUnrollLimit * 4) {
dd7ce84016a5 8194740: UseSubwordForMaxVector causes performance regression
vdeshpande
parents: 50632
diff changeset
   871
      return true;
dd7ce84016a5 8194740: UseSubwordForMaxVector causes performance regression
vdeshpande
parents: 50632
diff changeset
   872
    }
10538
d6bf448fa3ad 7054211: No loop unrolling done in jdk7b144 for a test update() while loop
kvn
parents: 10263
diff changeset
   873
    // Normal case: loop too big
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   874
    return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   875
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   876
54698
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   877
  if (cl->is_unroll_only()) {
34174
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
   878
    if (TraceSuperWordLoopUnrollAnalysis) {
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
   879
      tty->print_cr("policy_unroll passed vector loop(vlen=%d,factor = %d)\n", slp_max_unroll_factor, future_unroll_ct);
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
   880
    }
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   881
  }
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   882
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   883
  // Unroll once!  (Each trip will soon do double iterations)
489c9b5090e2 Initial load
duke
parents:
diff changeset
   884
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   885
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   886
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   887
void IdealLoopTree::policy_unroll_slp_analysis(CountedLoopNode *cl, PhaseIdealLoop *phase, int future_unroll_ct) {
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   888
  // Enable this functionality target by target as needed
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   889
  if (SuperWordLoopUnrollAnalysis) {
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   890
    if (!cl->was_slp_analyzed()) {
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   891
      SuperWord sw(phase);
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   892
      sw.transform_loop(this, false);
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   893
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   894
      // If the loop is slp canonical analyze it
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   895
      if (sw.early_return() == false) {
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   896
        sw.unrolling_analysis(_local_loop_unroll_factor);
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   897
      }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   898
    }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   899
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   900
    if (cl->has_passed_slp()) {
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   901
      int slp_max_unroll_factor = cl->slp_max_unroll();
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   902
      if (slp_max_unroll_factor >= future_unroll_ct) {
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   903
        int new_limit = cl->node_count_before_unroll() * slp_max_unroll_factor;
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   904
        if (new_limit > LoopUnrollLimit) {
34174
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
   905
          if (TraceSuperWordLoopUnrollAnalysis) {
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
   906
            tty->print_cr("slp analysis unroll=%d, default limit=%d\n", new_limit, _local_loop_unroll_limit);
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
   907
          }
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   908
          _local_loop_unroll_limit = new_limit;
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   909
        }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   910
      }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   911
    }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   912
  }
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   913
}
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
   914
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   915
//------------------------------policy_align-----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   916
// Return TRUE or FALSE if the loop should be cache-line aligned.  Gather the
489c9b5090e2 Initial load
duke
parents:
diff changeset
   917
// expression that does the alignment.  Note that only one array base can be
2131
98f9cef66a34 6810672: Comment typos
twisti
parents: 1500
diff changeset
   918
// aligned in a loop (unless the VM guarantees mutual alignment).  Note that
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   919
// if we vectorize short memory ops into longer memory ops, we may want to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   920
// increase alignment.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   921
bool IdealLoopTree::policy_align( PhaseIdealLoop *phase ) const {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   922
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   923
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   924
489c9b5090e2 Initial load
duke
parents:
diff changeset
   925
//------------------------------policy_range_check-----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   926
// Return TRUE or FALSE if the loop should be range-check-eliminated.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   927
// Actually we do iteration-splitting, a more powerful form of RCE.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   928
bool IdealLoopTree::policy_range_check( PhaseIdealLoop *phase ) const {
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   929
  if (!RangeCheckElimination) return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   930
489c9b5090e2 Initial load
duke
parents:
diff changeset
   931
  CountedLoopNode *cl = _head->as_CountedLoop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   932
  // If we unrolled with no intention of doing RCE and we later
489c9b5090e2 Initial load
duke
parents:
diff changeset
   933
  // changed our minds, we got no pre-loop.  Either we need to
489c9b5090e2 Initial load
duke
parents:
diff changeset
   934
  // make a new pre-loop, or we gotta disallow RCE.
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   935
  if (cl->is_main_no_pre_loop()) return false; // Disallowed for now.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   936
  Node *trip_counter = cl->phi();
489c9b5090e2 Initial load
duke
parents:
diff changeset
   937
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   938
  // check for vectorized loops, some opts are no longer needed
54698
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   939
  if (cl->is_unroll_only()) return false;
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   940
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   941
  // Check loop body for tests of trip-counter plus loop-invariant vs
489c9b5090e2 Initial load
duke
parents:
diff changeset
   942
  // loop-invariant.
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   943
  for (uint i = 0; i < _body.size(); i++) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   944
    Node *iff = _body[i];
34164
a9e6034d7707 8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents: 33628
diff changeset
   945
    if (iff->Opcode() == Op_If ||
a9e6034d7707 8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents: 33628
diff changeset
   946
        iff->Opcode() == Op_RangeCheck) { // Test?
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   947
489c9b5090e2 Initial load
duke
parents:
diff changeset
   948
      // Comparing trip+off vs limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
   949
      Node *bol = iff->in(1);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   950
      if (bol->req() != 2) continue; // dead constant test
4643
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
   951
      if (!bol->is_Bool()) {
39429
2c9cde29cd3f 8159715: Fix for 8072422 is incorrect
thartmann
parents: 39419
diff changeset
   952
        assert(bol->Opcode() == Op_Conv2B, "predicate check only");
4643
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
   953
        continue;
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
   954
      }
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   955
      if (bol->as_Bool()->_test._test == BoolTest::ne)
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   956
        continue; // not RC
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
   957
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   958
      Node *cmp = bol->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   959
      Node *rc_exp = cmp->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   960
      Node *limit = cmp->in(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   961
489c9b5090e2 Initial load
duke
parents:
diff changeset
   962
      Node *limit_c = phase->get_ctrl(limit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   963
      if( limit_c == phase->C->top() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   964
        return false;           // Found dead test on live IF?  No RCE!
489c9b5090e2 Initial load
duke
parents:
diff changeset
   965
      if( is_member(phase->get_loop(limit_c) ) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   966
        // Compare might have operands swapped; commute them
489c9b5090e2 Initial load
duke
parents:
diff changeset
   967
        rc_exp = cmp->in(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   968
        limit  = cmp->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   969
        limit_c = phase->get_ctrl(limit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
   970
        if( is_member(phase->get_loop(limit_c) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   971
          continue;             // Both inputs are loop varying; cannot RCE
489c9b5090e2 Initial load
duke
parents:
diff changeset
   972
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   973
489c9b5090e2 Initial load
duke
parents:
diff changeset
   974
      if (!phase->is_scaled_iv_plus_offset(rc_exp, trip_counter, NULL, NULL)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
   975
        continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   976
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   977
      // Yeah!  Found a test like 'trip+off vs limit'
489c9b5090e2 Initial load
duke
parents:
diff changeset
   978
      // Test is an IfNode, has 2 projections.  If BOTH are in the loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
   979
      // we need loop unswitching instead of iteration splitting.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   980
      if( is_loop_exit(iff) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   981
        return true;            // Found reason to split iterations
489c9b5090e2 Initial load
duke
parents:
diff changeset
   982
    } // End of is IF
489c9b5090e2 Initial load
duke
parents:
diff changeset
   983
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
   984
489c9b5090e2 Initial load
duke
parents:
diff changeset
   985
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
   986
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
   987
489c9b5090e2 Initial load
duke
parents:
diff changeset
   988
//------------------------------policy_peel_only-------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
   989
// Return TRUE or FALSE if the loop should NEVER be RCE'd or aligned.  Useful
489c9b5090e2 Initial load
duke
parents:
diff changeset
   990
// for unrolling loops with NO array accesses.
489c9b5090e2 Initial load
duke
parents:
diff changeset
   991
bool IdealLoopTree::policy_peel_only( PhaseIdealLoop *phase ) const {
31772
718fc367468d 8129920: Vectorized loop unrolling
kvn
parents: 31590
diff changeset
   992
  // check for vectorized loops, any peeling done was already applied
54698
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   993
  if (_head->is_CountedLoop() && _head->as_CountedLoop()->is_unroll_only()) {
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   994
    return false;
3ebf58dbf5d8 8223137: Rename predicate 'do_unroll_only()' to 'is_unroll_only()'.
phedlin
parents: 54022
diff changeset
   995
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
   996
489c9b5090e2 Initial load
duke
parents:
diff changeset
   997
  for( uint i = 0; i < _body.size(); i++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   998
    if( _body[i]->is_Mem() )
489c9b5090e2 Initial load
duke
parents:
diff changeset
   999
      return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1000
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1001
  // No memory accesses at all!
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1002
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1003
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1004
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1005
//------------------------------clone_up_backedge_goo--------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1006
// If Node n lives in the back_ctrl block and cannot float, we clone a private
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1007
// version of n in preheader_ctrl block and return that, otherwise return n.
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1008
Node *PhaseIdealLoop::clone_up_backedge_goo( Node *back_ctrl, Node *preheader_ctrl, Node *n, VectorSet &visited, Node_Stack &clones ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1009
  if( get_ctrl(n) != back_ctrl ) return n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1010
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1011
  // Only visit once
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1012
  if (visited.test_set(n->_idx)) {
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1013
    Node *x = clones.find(n->_idx);
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1014
    if (x != NULL)
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1015
      return x;
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1016
    return n;
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1017
  }
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1018
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1019
  Node *x = NULL;               // If required, a clone of 'n'
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1020
  // Check for 'n' being pinned in the backedge.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1021
  if( n->in(0) && n->in(0) == back_ctrl ) {
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1022
    assert(clones.find(n->_idx) == NULL, "dead loop");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1023
    x = n->clone();             // Clone a copy of 'n' to preheader
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1024
    clones.push(x, n->_idx);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1025
    x->set_req( 0, preheader_ctrl ); // Fix x's control input to preheader
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1026
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1027
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1028
  // Recursive fixup any other input edges into x.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1029
  // If there are no changes we can just return 'n', otherwise
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1030
  // we need to clone a private copy and change it.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1031
  for( uint i = 1; i < n->req(); i++ ) {
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1032
    Node *g = clone_up_backedge_goo( back_ctrl, preheader_ctrl, n->in(i), visited, clones );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1033
    if( g != n->in(i) ) {
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1034
      if( !x ) {
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1035
        assert(clones.find(n->_idx) == NULL, "dead loop");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1036
        x = n->clone();
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1037
        clones.push(x, n->_idx);
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1038
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1039
      x->set_req(i, g);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1040
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1041
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1042
  if( x ) {                     // x can legally float to pre-header location
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1043
    register_new_node( x, preheader_ctrl );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1044
    return x;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1045
  } else {                      // raise n to cover LCA of uses
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1046
    set_ctrl( n, find_non_split_ctrl(back_ctrl->in(0)) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1047
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1048
  return n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1049
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1050
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1051
Node* PhaseIdealLoop::cast_incr_before_loop(Node* incr, Node* ctrl, Node* loop) {
27708
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1052
  Node* castii = new CastIINode(incr, TypeInt::INT, true);
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1053
  castii->set_req(0, ctrl);
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1054
  register_new_node(castii, ctrl);
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1055
  for (DUIterator_Fast imax, i = incr->fast_outs(imax); i < imax; i++) {
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1056
    Node* n = incr->fast_out(i);
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1057
    if (n->is_Phi() && n->in(0) == loop) {
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1058
      int nrep = n->replace_edge(incr, castii);
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1059
      return castii;
27708
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1060
    }
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1061
  }
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1062
  return NULL;
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1063
}
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1064
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1065
// Make a copy of the skeleton range check predicates before the main
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1066
// loop and set the initial value of loop as input. After unrolling,
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1067
// the range of values for the induction variable in the main loop can
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1068
// fall outside the allowed range of values by the array access (main
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1069
// loop is never executed). When that happens, range check
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1070
// CastII/ConvI2L nodes cause some data paths to die. For consistency,
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1071
// the control paths must die too but the range checks were removed by
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1072
// predication. The range checks that we add here guarantee that they do.
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1073
void PhaseIdealLoop::duplicate_predicates_helper(Node* predicate, Node* start, Node* end,
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1074
                                                 IdealLoopTree* outer_loop, LoopNode* outer_main_head,
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1075
                                                 uint dd_main_head) {
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1076
  if (predicate != NULL) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1077
    IfNode* iff = predicate->in(0)->as_If();
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1078
    ProjNode* uncommon_proj = iff->proj_out(1 - predicate->as_Proj()->_con);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1079
    Node* rgn = uncommon_proj->unique_ctrl_out();
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1080
    assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1081
    assert(iff->in(1)->in(1)->Opcode() == Op_Opaque1, "unexpected predicate shape");
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1082
    predicate = iff->in(0);
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1083
    Node* current_proj = outer_main_head->in(LoopNode::EntryControl);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1084
    Node* prev_proj = current_proj;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1085
    while (predicate != NULL && predicate->is_Proj() && predicate->in(0)->is_If()) {
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1086
      iff = predicate->in(0)->as_If();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1087
      uncommon_proj = iff->proj_out(1 - predicate->as_Proj()->_con);
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1088
      if (uncommon_proj->unique_ctrl_out() != rgn)
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1089
        break;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1090
      if (iff->in(1)->Opcode() == Op_Opaque4) {
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1091
        assert(skeleton_predicate_has_opaque(iff), "unexpected");
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1092
        // Clone the predicate twice and initialize one with the initial
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1093
        // value of the loop induction variable. Leave the other predicate
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1094
        // to be initialized when increasing the stride during loop unrolling.
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1095
        prev_proj = clone_skeleton_predicate(iff, start, predicate, uncommon_proj, current_proj, outer_loop, prev_proj);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1096
        assert(skeleton_predicate_has_opaque(prev_proj->in(0)->as_If()) == (start->Opcode() == Op_Opaque1), "");
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1097
        prev_proj = clone_skeleton_predicate(iff, end, predicate, uncommon_proj, current_proj, outer_loop, prev_proj);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1098
        assert(skeleton_predicate_has_opaque(prev_proj->in(0)->as_If()) == (end->Opcode() == Op_Opaque1), "");
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1099
        // Remove the skeleton predicate from the pre-loop
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1100
        _igvn.replace_input_of(iff, 1, _igvn.intcon(1));
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1101
      }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1102
      predicate = predicate->in(0)->in(0);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1103
    }
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1104
    _igvn.replace_input_of(outer_main_head, LoopNode::EntryControl, prev_proj);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1105
    set_idom(outer_main_head, prev_proj, dd_main_head);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1106
  }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1107
}
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1108
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1109
static bool skeleton_follow_inputs(Node* n, int op) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1110
  return (n->is_Bool() ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1111
          n->is_Cmp() ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1112
          op == Op_AndL ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1113
          op == Op_OrL ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1114
          op == Op_RShiftL ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1115
          op == Op_LShiftL ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1116
          op == Op_AddL ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1117
          op == Op_AddI ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1118
          op == Op_MulL ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1119
          op == Op_MulI ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1120
          op == Op_SubL ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1121
          op == Op_SubI ||
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1122
          op == Op_ConvI2L);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1123
}
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1124
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1125
bool PhaseIdealLoop::skeleton_predicate_has_opaque(IfNode* iff) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1126
  ResourceMark rm;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1127
  Unique_Node_List wq;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1128
  wq.push(iff->in(1)->in(1));
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1129
  for (uint i = 0; i < wq.size(); i++) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1130
    Node* n = wq.at(i);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1131
    int op = n->Opcode();
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1132
    if (skeleton_follow_inputs(n, op)) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1133
      for (uint j = 1; j < n->req(); j++) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1134
        Node* m = n->in(j);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1135
        if (m != NULL) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1136
          wq.push(m);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1137
        }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1138
      }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1139
      continue;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1140
    }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1141
    if (op == Op_Opaque1) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1142
      return true;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1143
    }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1144
  }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1145
  return false;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1146
}
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1147
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1148
Node* PhaseIdealLoop::clone_skeleton_predicate(Node* iff, Node* value, Node* predicate, Node* uncommon_proj,
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1149
                                               Node* current_proj, IdealLoopTree* outer_loop, Node* prev_proj) {
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1150
  Node_Stack to_clone(2);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1151
  to_clone.push(iff->in(1), 1);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1152
  uint current = C->unique();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1153
  Node* result = NULL;
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1154
  // Look for the opaque node to replace with the new value
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1155
  // and clone everything in between. We keep the Opaque4 node
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1156
  // so the duplicated predicates are eliminated once loop
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1157
  // opts are over: they are here only to keep the IR graph
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1158
  // consistent.
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1159
  do {
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1160
    Node* n = to_clone.node();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1161
    uint i = to_clone.index();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1162
    Node* m = n->in(i);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1163
    int op = m->Opcode();
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1164
    if (skeleton_follow_inputs(m, op)) {
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1165
        to_clone.push(m, 1);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1166
        continue;
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1167
    }
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1168
    if (op == Op_Opaque1) {
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1169
      if (n->_idx < current) {
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1170
        n = n->clone();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1171
      }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1172
      n->set_req(i, value);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1173
      register_new_node(n, current_proj);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1174
      to_clone.set_node(n);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1175
    }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1176
    for (;;) {
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1177
      Node* cur = to_clone.node();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1178
      uint j = to_clone.index();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1179
      if (j+1 < cur->req()) {
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1180
        to_clone.set_index(j+1);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1181
        break;
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1182
      }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1183
      to_clone.pop();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1184
      if (to_clone.size() == 0) {
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1185
        result = cur;
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1186
        break;
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1187
      }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1188
      Node* next = to_clone.node();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1189
      j = to_clone.index();
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1190
      if (next->in(j) != cur) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1191
        assert(cur->_idx >= current || next->in(j)->Opcode() == Op_Opaque1, "new node or Opaque1 being replaced");
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1192
        if (next->_idx < current) {
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1193
          next = next->clone();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1194
          register_new_node(next, current_proj);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1195
          to_clone.set_node(next);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1196
        }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1197
        next->set_req(j, cur);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1198
      }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1199
    }
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1200
  } while (result == NULL);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1201
  assert(result->_idx >= current, "new node expected");
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1202
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1203
  Node* proj = predicate->clone();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1204
  Node* other_proj = uncommon_proj->clone();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1205
  Node* new_iff = iff->clone();
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1206
  new_iff->set_req(1, result);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1207
  proj->set_req(0, new_iff);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1208
  other_proj->set_req(0, new_iff);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1209
  Node *frame = new ParmNode(C->start(), TypeFunc::FramePtr);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1210
  register_new_node(frame, C->start());
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1211
  // It's impossible for the predicate to fail at runtime. Use an Halt node.
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1212
  Node* halt = new HaltNode(other_proj, frame);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1213
  C->root()->add_req(halt);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1214
  new_iff->set_req(0, prev_proj);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1215
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1216
  register_control(new_iff, outer_loop->_parent, prev_proj);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1217
  register_control(proj, outer_loop->_parent, new_iff);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1218
  register_control(other_proj, _ltree_root, new_iff);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1219
  register_control(halt, _ltree_root, other_proj);
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1220
  return proj;
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1221
}
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1222
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1223
void PhaseIdealLoop::duplicate_predicates(CountedLoopNode* pre_head, Node* start, Node* end,
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1224
                                          IdealLoopTree* outer_loop, LoopNode* outer_main_head,
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1225
                                          uint dd_main_head) {
50561
5756e8eecb17 8205034: [BACKOUT] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50558
diff changeset
  1226
  if (UseLoopPredicate) {
5756e8eecb17 8205034: [BACKOUT] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50558
diff changeset
  1227
    Node* entry = pre_head->in(LoopNode::EntryControl);
5756e8eecb17 8205034: [BACKOUT] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50558
diff changeset
  1228
    Node* predicate = NULL;
5756e8eecb17 8205034: [BACKOUT] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50558
diff changeset
  1229
    predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_loop_limit_check);
5756e8eecb17 8205034: [BACKOUT] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50558
diff changeset
  1230
    if (predicate != NULL) {
52608
61241fc2217a 8211451: ~2.5% regression on compression benchmark starting with 12-b11
roland
parents: 51078
diff changeset
  1231
      entry = skip_loop_predicates(entry);
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1232
    }
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1233
    Node* profile_predicate = NULL;
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1234
    if (UseProfiledLoopPredicate) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1235
      profile_predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_profile_predicate);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1236
      if (profile_predicate != NULL) {
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1237
        entry = skip_loop_predicates(entry);
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1238
      }
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  1239
    }
50561
5756e8eecb17 8205034: [BACKOUT] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50558
diff changeset
  1240
    predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1241
    duplicate_predicates_helper(predicate, start, end, outer_loop, outer_main_head, dd_main_head);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1242
    duplicate_predicates_helper(profile_predicate, start, end, outer_loop, outer_main_head, dd_main_head);
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1243
  }
27708
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1244
}
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1245
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1246
//------------------------------insert_pre_post_loops--------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1247
// Insert pre and post loops.  If peel_only is set, the pre-loop can not have
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1248
// more iterations added.  It acts as a 'peel' only, no lower-bound RCE, no
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1249
// alignment.  Useful to unroll loops that do no array accesses.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1250
void PhaseIdealLoop::insert_pre_post_loops( IdealLoopTree *loop, Node_List &old_new, bool peel_only ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1251
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1252
#ifndef PRODUCT
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1253
  if (TraceLoopOpts) {
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1254
    if (peel_only)
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1255
      tty->print("PeelMainPost ");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1256
    else
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1257
      tty->print("PreMainPost  ");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1258
    loop->dump_head();
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1259
  }
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1260
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1261
  C->set_major_progress();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1262
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1263
  // Find common pieces of the loop being guarded with pre & post loops
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1264
  CountedLoopNode *main_head = loop->_head->as_CountedLoop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1265
  assert( main_head->is_normal_loop(), "" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1266
  CountedLoopEndNode *main_end = main_head->loopexit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1267
  assert( main_end->outcnt() == 2, "1 true, 1 false path only" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1268
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1269
  Node *pre_header= main_head->in(LoopNode::EntryControl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1270
  Node *init      = main_head->init_trip();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1271
  Node *incr      = main_end ->incr();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1272
  Node *limit     = main_end ->limit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1273
  Node *stride    = main_end ->stride();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1274
  Node *cmp       = main_end ->cmp_node();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1275
  BoolTest::mask b_test = main_end->test_trip();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1276
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1277
  // Need only 1 user of 'bol' because I will be hacking the loop bounds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1278
  Node *bol = main_end->in(CountedLoopEndNode::TestValue);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1279
  if( bol->outcnt() != 1 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1280
    bol = bol->clone();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1281
    register_new_node(bol,main_end->in(CountedLoopEndNode::TestControl));
25913
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
  1282
    _igvn.replace_input_of(main_end, CountedLoopEndNode::TestValue, bol);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1283
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1284
  // Need only 1 user of 'cmp' because I will be hacking the loop bounds.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1285
  if( cmp->outcnt() != 1 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1286
    cmp = cmp->clone();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1287
    register_new_node(cmp,main_end->in(CountedLoopEndNode::TestControl));
25913
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
  1288
    _igvn.replace_input_of(bol, 1, cmp);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1289
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1290
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1291
  // Add the post loop
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1292
  CountedLoopNode *post_head = NULL;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1293
  Node *main_exit = insert_post_loop(loop, old_new, main_head, main_end, incr, limit, post_head);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1294
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1295
  //------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1296
  // Step B: Create Pre-Loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1297
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1298
  // Step B1: Clone the loop body.  The clone becomes the pre-loop.  The main
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1299
  // loop pre-header illegally has 2 control users (old & new loops).
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1300
  LoopNode* outer_main_head = main_head;
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1301
  IdealLoopTree* outer_loop = loop;
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1302
  if (main_head->is_strip_mined()) {
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1303
    main_head->verify_strip_mined(1);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1304
    outer_main_head = main_head->outer_loop();
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1305
    outer_loop = loop->_parent;
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1306
    assert(outer_loop->_head == outer_main_head, "broken loop tree");
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1307
  }
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1308
  uint dd_main_head = dom_depth(outer_main_head);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1309
  clone_loop(loop, old_new, dd_main_head, ControlAroundStripMined);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1310
  CountedLoopNode*    pre_head = old_new[main_head->_idx]->as_CountedLoop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1311
  CountedLoopEndNode* pre_end  = old_new[main_end ->_idx]->as_CountedLoopEnd();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1312
  pre_head->set_pre_loop(main_head);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1313
  Node *pre_incr = old_new[incr->_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1314
1432
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1399
diff changeset
  1315
  // Reduce the pre-loop trip count.
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1399
diff changeset
  1316
  pre_end->_prob = PROB_FAIR;
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1399
diff changeset
  1317
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1318
  // Find the pre-loop normal exit.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1319
  Node* pre_exit = pre_end->proj_out(false);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1320
  assert( pre_exit->Opcode() == Op_IfFalse, "" );
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1321
  IfFalseNode *new_pre_exit = new IfFalseNode(pre_end);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1322
  _igvn.register_new_node_with_optimizer( new_pre_exit );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1323
  set_idom(new_pre_exit, pre_end, dd_main_head);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1324
  set_loop(new_pre_exit, outer_loop->_parent);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1325
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1326
  // Step B2: Build a zero-trip guard for the main-loop.  After leaving the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1327
  // pre-loop, the main-loop may not execute at all.  Later in life this
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1328
  // zero-trip guard will become the minimum-trip guard when we unroll
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1329
  // the main-loop.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1330
  Node *min_opaq = new Opaque1Node(C, limit);
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1331
  Node *min_cmp  = new CmpINode( pre_incr, min_opaq );
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1332
  Node *min_bol  = new BoolNode( min_cmp, b_test );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1333
  register_new_node( min_opaq, new_pre_exit );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1334
  register_new_node( min_cmp , new_pre_exit );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1335
  register_new_node( min_bol , new_pre_exit );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1336
1432
44f076e3d2a4 6667595: Set probability FAIR for pre-, post- loops and ALWAYS for main loop
kvn
parents: 1399
diff changeset
  1337
  // Build the IfNode (assume the main-loop is executed always).
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1338
  IfNode *min_iff = new IfNode( new_pre_exit, min_bol, PROB_ALWAYS, COUNT_UNKNOWN );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1339
  _igvn.register_new_node_with_optimizer( min_iff );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1340
  set_idom(min_iff, new_pre_exit, dd_main_head);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1341
  set_loop(min_iff, outer_loop->_parent);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1342
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1343
  // Plug in the false-path, taken if we need to skip main-loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1344
  _igvn.hash_delete( pre_exit );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1345
  pre_exit->set_req(0, min_iff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1346
  set_idom(pre_exit, min_iff, dd_main_head);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1347
  set_idom(pre_exit->unique_ctrl_out(), min_iff, dd_main_head);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1348
  // Make the true-path, must enter the main loop
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1349
  Node *min_taken = new IfTrueNode( min_iff );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1350
  _igvn.register_new_node_with_optimizer( min_taken );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1351
  set_idom(min_taken, min_iff, dd_main_head);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1352
  set_loop(min_taken, outer_loop->_parent);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1353
  // Plug in the true path
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1354
  _igvn.hash_delete(outer_main_head);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1355
  outer_main_head->set_req(LoopNode::EntryControl, min_taken);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1356
  set_idom(outer_main_head, min_taken, dd_main_head);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1357
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1358
  Arena *a = Thread::current()->resource_area();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1359
  VectorSet visited(a);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1360
  Node_Stack clones(a, main_head->back_control()->outcnt());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1361
  // Step B3: Make the fall-in values to the main-loop come from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1362
  // fall-out values of the pre-loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1363
  for (DUIterator_Fast i2max, i2 = main_head->fast_outs(i2max); i2 < i2max; i2++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1364
    Node* main_phi = main_head->fast_out(i2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1365
    if( main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() > 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1366
      Node *pre_phi = old_new[main_phi->_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1367
      Node *fallpre  = clone_up_backedge_goo(pre_head->back_control(),
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1368
                                             main_head->skip_strip_mined()->in(LoopNode::EntryControl),
10011
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1369
                                             pre_phi->in(LoopNode::LoopBackControl),
e8b38f7b9959 7044738: Loop unroll optimization causes incorrect result
kvn
parents: 10005
diff changeset
  1370
                                             visited, clones);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1371
      _igvn.hash_delete(main_phi);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1372
      main_phi->set_req( LoopNode::EntryControl, fallpre );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1373
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1374
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1375
27708
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1376
  // Nodes inside the loop may be control dependent on a predicate
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1377
  // that was moved before the preloop. If the back branch of the main
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1378
  // or post loops becomes dead, those nodes won't be dependent on the
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1379
  // test that guards that loop nest anymore which could lead to an
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1380
  // incorrect array access because it executes independently of the
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1381
  // test that was guarding the loop nest. We add a special CastII on
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1382
  // the if branch that enters the loop, between the input induction
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1383
  // variable value and the induction variable Phi to preserve correct
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1384
  // dependencies.
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1385
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1386
  // CastII for the main loop:
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1387
  Node* castii = cast_incr_before_loop( pre_incr, min_taken, main_head );
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1388
  assert(castii != NULL, "no castII inserted");
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1389
  Node* opaque_castii = new Opaque1Node(C, castii);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1390
  register_new_node(opaque_castii, outer_main_head->in(LoopNode::EntryControl));
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1391
  duplicate_predicates(pre_head, castii, opaque_castii, outer_loop, outer_main_head, dd_main_head);
27708
8a8710cb8fc4 8054478: C2: Incorrectly compiled char[] array access crashes JVM
roland
parents: 27707
diff changeset
  1392
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1393
  // Step B4: Shorten the pre-loop to run only 1 iteration (for now).
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1394
  // RCE and alignment may change this later.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1395
  Node *cmp_end = pre_end->cmp_node();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1396
  assert( cmp_end->in(2) == limit, "" );
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1397
  Node *pre_limit = new AddINode( init, stride );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1398
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1399
  // Save the original loop limit in this Opaque1 node for
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1400
  // use by range check elimination.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1401
  Node *pre_opaq  = new Opaque1Node(C, pre_limit, limit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1402
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1403
  register_new_node( pre_limit, pre_head->in(0) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1404
  register_new_node( pre_opaq , pre_head->in(0) );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1405
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1406
  // Since no other users of pre-loop compare, I can hack limit directly
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1407
  assert( cmp_end->outcnt() == 1, "no other users" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1408
  _igvn.hash_delete(cmp_end);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1409
  cmp_end->set_req(2, peel_only ? pre_limit : pre_opaq);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1410
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1411
  // Special case for not-equal loop bounds:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1412
  // Change pre loop test, main loop test, and the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1413
  // main loop guard test to use lt or gt depending on stride
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1414
  // direction:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1415
  // positive stride use <
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1416
  // negative stride use >
10005
ce4ea81a1329 7052494: Eclipse test fails on JDK 7 b142
kvn
parents: 9955
diff changeset
  1417
  //
ce4ea81a1329 7052494: Eclipse test fails on JDK 7 b142
kvn
parents: 9955
diff changeset
  1418
  // not-equal test is kept for post loop to handle case
ce4ea81a1329 7052494: Eclipse test fails on JDK 7 b142
kvn
parents: 9955
diff changeset
  1419
  // when init > limit when stride > 0 (and reverse).
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1420
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1421
  if (pre_end->in(CountedLoopEndNode::TestValue)->as_Bool()->_test._test == BoolTest::ne) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1422
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1423
    BoolTest::mask new_test = (main_end->stride_con() > 0) ? BoolTest::lt : BoolTest::gt;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1424
    // Modify pre loop end condition
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1425
    Node* pre_bol = pre_end->in(CountedLoopEndNode::TestValue)->as_Bool();
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1426
    BoolNode* new_bol0 = new BoolNode(pre_bol->in(1), new_test);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1427
    register_new_node( new_bol0, pre_head->in(0) );
25913
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
  1428
    _igvn.replace_input_of(pre_end, CountedLoopEndNode::TestValue, new_bol0);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1429
    // Modify main loop guard condition
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1430
    assert(min_iff->in(CountedLoopEndNode::TestValue) == min_bol, "guard okay");
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1431
    BoolNode* new_bol1 = new BoolNode(min_bol->in(1), new_test);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1432
    register_new_node( new_bol1, new_pre_exit );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1433
    _igvn.hash_delete(min_iff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1434
    min_iff->set_req(CountedLoopEndNode::TestValue, new_bol1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1435
    // Modify main loop end condition
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1436
    BoolNode* main_bol = main_end->in(CountedLoopEndNode::TestValue)->as_Bool();
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  1437
    BoolNode* new_bol2 = new BoolNode(main_bol->in(1), new_test);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1438
    register_new_node( new_bol2, main_end->in(CountedLoopEndNode::TestControl) );
25913
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
  1439
    _igvn.replace_input_of(main_end, CountedLoopEndNode::TestValue, new_bol2);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1440
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1441
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1442
  // Flag main loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1443
  main_head->set_main_loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1444
  if( peel_only ) main_head->set_main_no_pre_loop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1445
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1446
  // Subtract a trip count for the pre-loop.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1447
  main_head->set_trip_count(main_head->trip_count() - 1);
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1448
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1449
  // It's difficult to be precise about the trip-counts
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1450
  // for the pre/post loops.  They are usually very short,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1451
  // so guess that 4 trips is a reasonable value.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1452
  post_head->set_profile_trip_cnt(4.0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1453
  pre_head->set_profile_trip_cnt(4.0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1454
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1455
  // Now force out all loop-invariant dominating tests.  The optimizer
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1456
  // finds some, but we _know_ they are all useless.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1457
  peeled_dom_test_elim(loop,old_new);
24020
d73865d27efc 8039050: Crash in C2 compiler at Node::rematerialize
kvn
parents: 23528
diff changeset
  1458
  loop->record_for_igvn();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1459
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1460
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1461
//------------------------------insert_vector_post_loop------------------------
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1462
// Insert a copy of the atomic unrolled vectorized main loop as a post loop,
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1463
// unroll_policy has already informed us that more unrolling is about to happen to
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1464
// the main loop.  The resultant post loop will serve as a vectorized drain loop.
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1465
void PhaseIdealLoop::insert_vector_post_loop(IdealLoopTree *loop, Node_List &old_new) {
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1466
  if (!loop->_head->is_CountedLoop()) return;
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1467
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1468
  CountedLoopNode *cl = loop->_head->as_CountedLoop();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1469
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1470
  // only process vectorized main loops
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1471
  if (!cl->is_vectorized_loop() || !cl->is_main_loop()) return;
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1472
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1473
  int slp_max_unroll_factor = cl->slp_max_unroll();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1474
  int cur_unroll = cl->unrolled_count();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1475
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1476
  if (slp_max_unroll_factor == 0) return;
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1477
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1478
  // only process atomic unroll vector loops (not super unrolled after vectorization)
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1479
  if (cur_unroll != slp_max_unroll_factor) return;
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1480
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1481
  // we only ever process this one time
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1482
  if (cl->has_atomic_post_loop()) return;
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1483
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1484
#ifndef PRODUCT
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1485
  if (TraceLoopOpts) {
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1486
    tty->print("PostVector  ");
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1487
    loop->dump_head();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1488
  }
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1489
#endif
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1490
  C->set_major_progress();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1491
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1492
  // Find common pieces of the loop being guarded with pre & post loops
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1493
  CountedLoopNode *main_head = loop->_head->as_CountedLoop();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1494
  CountedLoopEndNode *main_end = main_head->loopexit();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1495
  // diagnostic to show loop end is not properly formed
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1496
  assert(main_end->outcnt() == 2, "1 true, 1 false path only");
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1497
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1498
  // mark this loop as processed
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1499
  main_head->mark_has_atomic_post_loop();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1500
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1501
  Node *incr = main_end->incr();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1502
  Node *limit = main_end->limit();
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1503
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1504
  // In this case we throw away the result as we are not using it to connect anything else.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1505
  CountedLoopNode *post_head = NULL;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1506
  insert_post_loop(loop, old_new, main_head, main_end, incr, limit, post_head);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1507
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1508
  // It's difficult to be precise about the trip-counts
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1509
  // for post loops.  They are usually very short,
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1510
  // so guess that unit vector trips is a reasonable value.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1511
  post_head->set_profile_trip_cnt(cur_unroll);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1512
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1513
  // Now force out all loop-invariant dominating tests.  The optimizer
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1514
  // finds some, but we _know_ they are all useless.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1515
  peeled_dom_test_elim(loop, old_new);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1516
  loop->record_for_igvn();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1517
}
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1518
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1519
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1520
//-------------------------insert_scalar_rced_post_loop------------------------
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1521
// Insert a copy of the rce'd main loop as a post loop,
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1522
// We have not unrolled the main loop, so this is the right time to inject this.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1523
// Later we will examine the partner of this post loop pair which still has range checks
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1524
// to see inject code which tests at runtime if the range checks are applicable.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1525
void PhaseIdealLoop::insert_scalar_rced_post_loop(IdealLoopTree *loop, Node_List &old_new) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1526
  if (!loop->_head->is_CountedLoop()) return;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1527
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1528
  CountedLoopNode *cl = loop->_head->as_CountedLoop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1529
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1530
  // only process RCE'd main loops
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1531
  if (!cl->is_main_loop() || cl->range_checks_present()) return;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1532
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1533
#ifndef PRODUCT
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1534
  if (TraceLoopOpts) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1535
    tty->print("PostScalarRce  ");
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1536
    loop->dump_head();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1537
  }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1538
#endif
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1539
  C->set_major_progress();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1540
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1541
  // Find common pieces of the loop being guarded with pre & post loops
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1542
  CountedLoopNode *main_head = loop->_head->as_CountedLoop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1543
  CountedLoopEndNode *main_end = main_head->loopexit();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1544
  // diagnostic to show loop end is not properly formed
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1545
  assert(main_end->outcnt() == 2, "1 true, 1 false path only");
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1546
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1547
  Node *incr = main_end->incr();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1548
  Node *limit = main_end->limit();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1549
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1550
  // In this case we throw away the result as we are not using it to connect anything else.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1551
  CountedLoopNode *post_head = NULL;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1552
  insert_post_loop(loop, old_new, main_head, main_end, incr, limit, post_head);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1553
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1554
  // It's difficult to be precise about the trip-counts
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1555
  // for post loops.  They are usually very short,
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1556
  // so guess that unit vector trips is a reasonable value.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1557
  post_head->set_profile_trip_cnt(4.0);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1558
  post_head->set_is_rce_post_loop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1559
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1560
  // Now force out all loop-invariant dominating tests.  The optimizer
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1561
  // finds some, but we _know_ they are all useless.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1562
  peeled_dom_test_elim(loop, old_new);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1563
  loop->record_for_igvn();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1564
}
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1565
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1566
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1567
//------------------------------insert_post_loop-------------------------------
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1568
// Insert post loops.  Add a post loop to the given loop passed.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1569
Node *PhaseIdealLoop::insert_post_loop(IdealLoopTree *loop, Node_List &old_new,
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1570
                                       CountedLoopNode *main_head, CountedLoopEndNode *main_end,
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1571
                                       Node *incr, Node *limit, CountedLoopNode *&post_head) {
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1572
  IfNode* outer_main_end = main_end;
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1573
  IdealLoopTree* outer_loop = loop;
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1574
  if (main_head->is_strip_mined()) {
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1575
    main_head->verify_strip_mined(1);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1576
    outer_main_end = main_head->outer_loop_end();
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1577
    outer_loop = loop->_parent;
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1578
    assert(outer_loop->_head == main_head->in(LoopNode::EntryControl), "broken loop tree");
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1579
  }
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1580
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1581
  //------------------------------
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1582
  // Step A: Create a new post-Loop.
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1583
  Node* main_exit = outer_main_end->proj_out(false);
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1584
  assert(main_exit->Opcode() == Op_IfFalse, "");
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1585
  int dd_main_exit = dom_depth(main_exit);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1586
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
  1587
  // Step A1: Clone the loop body of main. The clone becomes the post-loop.
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1588
  // The main loop pre-header illegally has 2 control users (old & new loops).
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1589
  clone_loop(loop, old_new, dd_main_exit, ControlAroundStripMined);
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1590
  assert(old_new[main_end->_idx]->Opcode() == Op_CountedLoopEnd, "");
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1591
  post_head = old_new[main_head->_idx]->as_CountedLoop();
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1592
  post_head->set_normal_loop();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1593
  post_head->set_post_loop(main_head);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1594
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1595
  // Reduce the post-loop trip count.
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1596
  CountedLoopEndNode* post_end = old_new[main_end->_idx]->as_CountedLoopEnd();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1597
  post_end->_prob = PROB_FAIR;
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1598
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1599
  // Build the main-loop normal exit.
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1600
  IfFalseNode *new_main_exit = new IfFalseNode(outer_main_end);
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1601
  _igvn.register_new_node_with_optimizer(new_main_exit);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1602
  set_idom(new_main_exit, outer_main_end, dd_main_exit);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1603
  set_loop(new_main_exit, outer_loop->_parent);
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1604
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1605
  // Step A2: Build a zero-trip guard for the post-loop.  After leaving the
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1606
  // main-loop, the post-loop may not execute at all.  We 'opaque' the incr
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1607
  // (the previous loop trip-counter exit value) because we will be changing
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1608
  // the exit value (via additional unrolling) so we cannot constant-fold away the zero
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1609
  // trip guard until all unrolling is done.
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1610
  Node *zer_opaq = new Opaque1Node(C, incr);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1611
  Node *zer_cmp = new CmpINode(zer_opaq, limit);
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1612
  Node *zer_bol = new BoolNode(zer_cmp, main_end->test_trip());
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1613
  register_new_node(zer_opaq, new_main_exit);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1614
  register_new_node(zer_cmp, new_main_exit);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1615
  register_new_node(zer_bol, new_main_exit);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1616
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1617
  // Build the IfNode
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1618
  IfNode *zer_iff = new IfNode(new_main_exit, zer_bol, PROB_FAIR, COUNT_UNKNOWN);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1619
  _igvn.register_new_node_with_optimizer(zer_iff);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1620
  set_idom(zer_iff, new_main_exit, dd_main_exit);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1621
  set_loop(zer_iff, outer_loop->_parent);
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1622
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1623
  // Plug in the false-path, taken if we need to skip this post-loop
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1624
  _igvn.replace_input_of(main_exit, 0, zer_iff);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1625
  set_idom(main_exit, zer_iff, dd_main_exit);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1626
  set_idom(main_exit->unique_out(), zer_iff, dd_main_exit);
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1627
  // Make the true-path, must enter this post loop
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1628
  Node *zer_taken = new IfTrueNode(zer_iff);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1629
  _igvn.register_new_node_with_optimizer(zer_taken);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1630
  set_idom(zer_taken, zer_iff, dd_main_exit);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1631
  set_loop(zer_taken, outer_loop->_parent);
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1632
  // Plug in the true path
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1633
  _igvn.hash_delete(post_head);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1634
  post_head->set_req(LoopNode::EntryControl, zer_taken);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1635
  set_idom(post_head, zer_taken, dd_main_exit);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1636
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1637
  Arena *a = Thread::current()->resource_area();
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1638
  VectorSet visited(a);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1639
  Node_Stack clones(a, main_head->back_control()->outcnt());
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1640
  // Step A3: Make the fall-in values to the post-loop come from the
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1641
  // fall-out values of the main-loop.
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1642
  for (DUIterator_Fast imax, i = main_head->fast_outs(imax); i < imax; i++) {
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1643
    Node* main_phi = main_head->fast_out(i);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1644
    if (main_phi->is_Phi() && main_phi->in(0) == main_head && main_phi->outcnt() >0) {
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1645
      Node *cur_phi = old_new[main_phi->_idx];
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1646
      Node *fallnew = clone_up_backedge_goo(main_head->back_control(),
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1647
                                            post_head->init_control(),
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1648
                                            main_phi->in(LoopNode::LoopBackControl),
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1649
                                            visited, clones);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1650
      _igvn.hash_delete(cur_phi);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1651
      cur_phi->set_req(LoopNode::EntryControl, fallnew);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1652
    }
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1653
  }
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1654
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1655
  // CastII for the new post loop:
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1656
  Node* castii = cast_incr_before_loop(zer_opaq->in(1), zer_taken, post_head);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1657
  assert(castii != NULL, "no castII inserted");
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1658
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1659
  return new_main_exit;
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1660
}
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  1661
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1662
//------------------------------is_invariant-----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1663
// Return true if n is invariant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1664
bool IdealLoopTree::is_invariant(Node* n) const {
4643
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
  1665
  Node *n_c = _phase->has_ctrl(n) ? _phase->get_ctrl(n) : n;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1666
  if (n_c->is_top()) return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1667
  return !is_member(_phase->get_loop(n_c));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1668
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1669
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1670
void PhaseIdealLoop::update_skeleton_predicates(Node* ctrl, CountedLoopNode* loop_head, Node* init, int stride_con) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1671
  // Search for skeleton predicates and update them according to the new stride
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1672
  Node* entry = ctrl;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1673
  Node* prev_proj = ctrl;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1674
  LoopNode* outer_loop_head = loop_head->skip_strip_mined();
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1675
  IdealLoopTree* outer_loop = get_loop(outer_loop_head);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1676
  while (entry != NULL && entry->is_Proj() && entry->in(0)->is_If()) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1677
    IfNode* iff = entry->in(0)->as_If();
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1678
    ProjNode* proj = iff->proj_out(1 - entry->as_Proj()->_con);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1679
    if (proj->unique_ctrl_out()->Opcode() != Op_Halt) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1680
      break;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1681
    }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1682
    if (iff->in(1)->Opcode() == Op_Opaque4) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1683
      // Look for predicate with an Opaque1 node that can be used as a template
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1684
      if (!skeleton_predicate_has_opaque(iff)) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1685
        // No Opaque1 node? It's either the check for the first value
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1686
        // of the first iteration or the check for the last value of
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1687
        // the first iteration of an unrolled loop. We can't
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1688
        // tell. Kill it in any case.
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1689
        _igvn.replace_input_of(iff, 1, iff->in(1)->in(2));
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1690
      } else {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1691
        // Add back the predicate for the value at the beginning of the first entry
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1692
        prev_proj = clone_skeleton_predicate(iff, init, entry, proj, ctrl, outer_loop, prev_proj);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1693
        assert(!skeleton_predicate_has_opaque(prev_proj->in(0)->as_If()), "unexpected");
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1694
        // Compute the value of the loop induction variable at the end of the
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1695
        // first iteration of the unrolled loop: init + new_stride_con - init_inc
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1696
        int init_inc = stride_con/loop_head->unrolled_count();
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1697
        assert(init_inc != 0, "invalid loop increment");
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1698
        int new_stride_con = stride_con * 2;
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1699
        Node* max_value = _igvn.intcon(new_stride_con - init_inc);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1700
        max_value = new AddINode(init, max_value);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1701
        register_new_node(max_value, get_ctrl(iff->in(1)));
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1702
        prev_proj = clone_skeleton_predicate(iff, max_value, entry, proj, ctrl, outer_loop, prev_proj);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1703
        assert(!skeleton_predicate_has_opaque(prev_proj->in(0)->as_If()), "unexpected");
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1704
      }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1705
    }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1706
    entry = entry->in(0)->in(0);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1707
  }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1708
  if (prev_proj != ctrl) {
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1709
    _igvn.replace_input_of(outer_loop_head, LoopNode::EntryControl, prev_proj);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1710
    set_idom(outer_loop_head, prev_proj, dom_depth(outer_loop_head));
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1711
  }
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1712
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1713
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1714
//------------------------------do_unroll--------------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1715
// Unroll the loop body one step - make each trip do 2 iterations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1716
void PhaseIdealLoop::do_unroll( IdealLoopTree *loop, Node_List &old_new, bool adjust_min_trip ) {
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1717
  assert(LoopUnrollLimit, "");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1718
  CountedLoopNode *loop_head = loop->_head->as_CountedLoop();
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1719
  CountedLoopEndNode *loop_end = loop_head->loopexit();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1720
#ifndef PRODUCT
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1721
  if (PrintOpto && VerifyLoopOptimizations) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1722
    tty->print("Unrolling ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1723
    loop->dump_head();
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1724
  } else if (TraceLoopOpts) {
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  1725
    if (loop_head->trip_count() < (uint)LoopUnrollLimit) {
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1726
      tty->print("Unroll %d(%2d) ", loop_head->unrolled_count()*2, loop_head->trip_count());
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
  1727
    } else {
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1728
      tty->print("Unroll %d     ", loop_head->unrolled_count()*2);
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
  1729
    }
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1730
    loop->dump_head();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1731
  }
30593
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1732
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 44314
diff changeset
  1733
  if (C->do_vector_loop() && (PrintOpto && (VerifyLoopOptimizations || TraceLoopOpts))) {
30593
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1734
    Arena* arena = Thread::current()->resource_area();
32202
7e7ad8b06f5b 8011858: Use Compile::live_nodes() instead of Compile::unique() in appropriate places
kvn
parents: 31772
diff changeset
  1735
    Node_Stack stack(arena, C->live_nodes() >> 2);
30593
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1736
    Node_List rpo_list;
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1737
    VectorSet visited(arena);
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1738
    visited.set(loop_head->_idx);
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1739
    rpo( loop_head, stack, visited, rpo_list );
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1740
    dump(loop, rpo_list.size(), rpo_list );
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1741
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1742
#endif
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1743
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1744
  // Remember loop node count before unrolling to detect
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1745
  // if rounds of unroll,optimize are making progress
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1746
  loop_head->set_node_count_before_unroll(loop->_body.size());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1747
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1748
  Node *ctrl  = loop_head->skip_strip_mined()->in(LoopNode::EntryControl);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1749
  Node *limit = loop_head->limit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1750
  Node *init  = loop_head->init_trip();
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  1751
  Node *stride = loop_head->stride();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1752
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1753
  Node *opaq = NULL;
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1754
  if (adjust_min_trip) {       // If not maximally unrolling, need adjustment
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1755
    // Search for zero-trip guard.
36809
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  1756
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  1757
    // Check the shape of the graph at the loop entry. If an inappropriate
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  1758
    // graph shape is encountered, the compiler bails out loop unrolling;
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  1759
    // compilation of the method will still succeed.
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  1760
    if (!is_canonical_loop_entry(loop_head)) {
36809
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  1761
      return;
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  1762
    }
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  1763
    opaq = loop_head->skip_predicates()->in(0)->in(1)->in(1)->in(2);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1764
    // Zero-trip test uses an 'opaque' node which is not shared.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1765
    assert(opaq->outcnt() == 1 && opaq->in(1) == limit, "");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1766
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1767
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1768
  C->set_major_progress();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1769
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1770
  Node* new_limit = NULL;
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1771
  int stride_con = stride->get_int();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1772
  int stride_p = (stride_con > 0) ? stride_con : -stride_con;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1773
  uint old_trip_count = loop_head->trip_count();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1774
  // Verify that unroll policy result is still valid.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1775
  assert(old_trip_count > 1 &&
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1776
      (!adjust_min_trip || stride_p <= (1<<3)*loop_head->unrolled_count()), "sanity");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1777
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  1778
  update_skeleton_predicates(ctrl, loop_head, init, stride_con);
50632
fd430e352427 8205033: [REDO] Induction variable of over-unrolled loop conflicts with range checks
thartmann
parents: 50623
diff changeset
  1779
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1780
  // Adjust loop limit to keep valid iterations number after unroll.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1781
  // Use (limit - stride) instead of (((limit - init)/stride) & (-2))*stride
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1782
  // which may overflow.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1783
  if (!adjust_min_trip) {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1784
    assert(old_trip_count > 1 && (old_trip_count & 1) == 0,
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1785
        "odd trip count for maximally unroll");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1786
    // Don't need to adjust limit for maximally unroll since trip count is even.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1787
  } else if (loop_head->has_exact_trip_count() && init->is_Con()) {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1788
    // Loop's limit is constant. Loop's init could be constant when pre-loop
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1789
    // become peeled iteration.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1790
    jlong init_con = init->get_int();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1791
    // We can keep old loop limit if iterations count stays the same:
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1792
    //   old_trip_count == new_trip_count * 2
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1793
    // Note: since old_trip_count >= 2 then new_trip_count >= 1
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1794
    // so we also don't need to adjust zero trip test.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1795
    jlong limit_con  = limit->get_int();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1796
    // (stride_con*2) not overflow since stride_con <= 8.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1797
    int new_stride_con = stride_con * 2;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1798
    int stride_m    = new_stride_con - (stride_con > 0 ? 1 : -1);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1799
    jlong trip_count = (limit_con - init_con + stride_m)/new_stride_con;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1800
    // New trip count should satisfy next conditions.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1801
    assert(trip_count > 0 && (julong)trip_count < (julong)max_juint/2, "sanity");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1802
    uint new_trip_count = (uint)trip_count;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1803
    adjust_min_trip = (old_trip_count != new_trip_count*2);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1804
  }
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1805
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1806
  if (adjust_min_trip) {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1807
    // Step 2: Adjust the trip limit if it is called for.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1808
    // The adjustment amount is -stride. Need to make sure if the
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1809
    // adjustment underflows or overflows, then the main loop is skipped.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1810
    Node* cmp = loop_end->cmp_node();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1811
    assert(cmp->in(2) == limit, "sanity");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1812
    assert(opaq != NULL && opaq->in(1) == limit, "sanity");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1813
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1814
    // Verify that policy_unroll result is still valid.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1815
    const TypeInt* limit_type = _igvn.type(limit)->is_int();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1816
    assert(stride_con > 0 && ((limit_type->_hi - stride_con) < limit_type->_hi) ||
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1817
        stride_con < 0 && ((limit_type->_lo - stride_con) > limit_type->_lo), "sanity");
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1818
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1819
    if (limit->is_Con()) {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1820
      // The check in policy_unroll and the assert above guarantee
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1821
      // no underflow if limit is constant.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1822
      new_limit = _igvn.intcon(limit->get_int() - stride_con);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1823
      set_ctrl(new_limit, C->root());
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1824
    } else {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1825
      // Limit is not constant.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1826
      if (loop_head->unrolled_count() == 1) { // only for first unroll
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1827
        // Separate limit by Opaque node in case it is an incremented
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1828
        // variable from previous loop to avoid using pre-incremented
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1829
        // value which could increase register pressure.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1830
        // Otherwise reorg_offsets() optimization will create a separate
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1831
        // Opaque node for each use of trip-counter and as result
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1832
        // zero trip guard limit will be different from loop limit.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1833
        assert(has_ctrl(opaq), "should have it");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1834
        Node* opaq_ctrl = get_ctrl(opaq);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1835
        limit = new Opaque2Node( C, limit );
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1836
        register_new_node( limit, opaq_ctrl );
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1837
      }
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 44314
diff changeset
  1838
      if ((stride_con > 0 && (java_subtract(limit_type->_lo, stride_con) < limit_type->_lo)) ||
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 44314
diff changeset
  1839
          (stride_con < 0 && (java_subtract(limit_type->_hi, stride_con) > limit_type->_hi))) {
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1840
        // No underflow.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1841
        new_limit = new SubINode(limit, stride);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1842
      } else {
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1843
        // (limit - stride) may underflow.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1844
        // Clamp the adjustment value with MININT or MAXINT:
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1845
        //
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1846
        //   new_limit = limit-stride
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1847
        //   if (stride > 0)
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1848
        //     new_limit = (limit < new_limit) ? MININT : new_limit;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1849
        //   else
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1850
        //     new_limit = (limit > new_limit) ? MAXINT : new_limit;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1851
        //
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1852
        BoolTest::mask bt = loop_end->test_trip();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1853
        assert(bt == BoolTest::lt || bt == BoolTest::gt, "canonical test is expected");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1854
        Node* adj_max = _igvn.intcon((stride_con > 0) ? min_jint : max_jint);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1855
        set_ctrl(adj_max, C->root());
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1856
        Node* old_limit = NULL;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1857
        Node* adj_limit = NULL;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1858
        Node* bol = limit->is_CMove() ? limit->in(CMoveNode::Condition) : NULL;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1859
        if (loop_head->unrolled_count() > 1 &&
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1860
            limit->is_CMove() && limit->Opcode() == Op_CMoveI &&
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1861
            limit->in(CMoveNode::IfTrue) == adj_max &&
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1862
            bol->as_Bool()->_test._test == bt &&
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1863
            bol->in(1)->Opcode() == Op_CmpI &&
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1864
            bol->in(1)->in(2) == limit->in(CMoveNode::IfFalse)) {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1865
          // Loop was unrolled before.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1866
          // Optimize the limit to avoid nested CMove:
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1867
          // use original limit as old limit.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1868
          old_limit = bol->in(1)->in(1);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1869
          // Adjust previous adjusted limit.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1870
          adj_limit = limit->in(CMoveNode::IfFalse);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1871
          adj_limit = new SubINode(adj_limit, stride);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1872
        } else {
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1873
          old_limit = limit;
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1874
          adj_limit = new SubINode(limit, stride);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1875
        }
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1876
        assert(old_limit != NULL && adj_limit != NULL, "");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1877
        register_new_node( adj_limit, ctrl ); // adjust amount
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1878
        Node* adj_cmp = new CmpINode(old_limit, adj_limit);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1879
        register_new_node( adj_cmp, ctrl );
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1880
        Node* adj_bool = new BoolNode(adj_cmp, bt);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1881
        register_new_node( adj_bool, ctrl );
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1882
        new_limit = new CMoveINode(adj_bool, adj_limit, adj_max, TypeInt::INT);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1883
      }
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1884
      register_new_node(new_limit, ctrl);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1885
    }
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1886
    assert(new_limit != NULL, "");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1887
    // Replace in loop test.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1888
    assert(loop_end->in(1)->in(1) == cmp, "sanity");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1889
    if (cmp->outcnt() == 1 && loop_end->in(1)->outcnt() == 1) {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1890
      // Don't need to create new test since only one user.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1891
      _igvn.hash_delete(cmp);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1892
      cmp->set_req(2, new_limit);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1893
    } else {
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1894
      // Create new test since it is shared.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1895
      Node* ctrl2 = loop_end->in(0);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1896
      Node* cmp2  = cmp->clone();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1897
      cmp2->set_req(2, new_limit);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1898
      register_new_node(cmp2, ctrl2);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1899
      Node* bol2 = loop_end->in(1)->clone();
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1900
      bol2->set_req(1, cmp2);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1901
      register_new_node(bol2, ctrl2);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1902
      _igvn.replace_input_of(loop_end, 1, bol2);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1903
    }
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1904
    // Step 3: Find the min-trip test guaranteed before a 'main' loop.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  1905
    // Make it a 1-trip test (means at least 2 trips).
37287
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1906
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1907
    // Guard test uses an 'opaque' node which is not shared.  Hence I
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1908
    // can edit it's inputs directly.  Hammer in the new limit for the
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1909
    // minimum-trip guard.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1910
    assert(opaq->outcnt() == 1, "");
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1911
    _igvn.replace_input_of(opaq, 1, new_limit);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1912
  }
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1913
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1914
  // Adjust max trip count. The trip count is intentionally rounded
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1915
  // down here (e.g. 15-> 7-> 3-> 1) because if we unwittingly over-unroll,
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1916
  // the main, unrolled, part of the loop will never execute as it is protected
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1917
  // by the min-trip test.  See bug 4834191 for a case where we over-unrolled
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1918
  // and later determined that part of the unrolled loop was dead.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1919
  loop_head->set_trip_count(old_trip_count / 2);
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1920
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1921
  // Double the count of original iterations in the unrolled loop body.
c2660335bf81 8072422: Cleanup: Remove some unused flags/code in loop optimizations
zmajo
parents: 36809
diff changeset
  1922
  loop_head->double_unrolled_count();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1923
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1924
  // ---------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1925
  // Step 4: Clone the loop body.  Move it inside the loop.  This loop body
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1926
  // represents the odd iterations; since the loop trips an even number of
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1927
  // times its backedge is never taken.  Kill the backedge.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1928
  uint dd = dom_depth(loop_head);
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1929
  clone_loop(loop, old_new, dd, IgnoreStripMined);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1930
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1931
  // Make backedges of the clone equal to backedges of the original.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1932
  // Make the fall-in from the original come from the fall-out of the clone.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1933
  for (DUIterator_Fast jmax, j = loop_head->fast_outs(jmax); j < jmax; j++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1934
    Node* phi = loop_head->fast_out(j);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1935
    if( phi->is_Phi() && phi->in(0) == loop_head && phi->outcnt() > 0 ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1936
      Node *newphi = old_new[phi->_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1937
      _igvn.hash_delete( phi );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1938
      _igvn.hash_delete( newphi );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1939
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1940
      phi   ->set_req(LoopNode::   EntryControl, newphi->in(LoopNode::LoopBackControl));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1941
      newphi->set_req(LoopNode::LoopBackControl, phi   ->in(LoopNode::LoopBackControl));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1942
      phi   ->set_req(LoopNode::LoopBackControl, C->top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1943
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1944
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1945
  Node *clone_head = old_new[loop_head->_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1946
  _igvn.hash_delete( clone_head );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1947
  loop_head ->set_req(LoopNode::   EntryControl, clone_head->in(LoopNode::LoopBackControl));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1948
  clone_head->set_req(LoopNode::LoopBackControl, loop_head ->in(LoopNode::LoopBackControl));
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1949
  loop_head ->set_req(LoopNode::LoopBackControl, C->top());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1950
  loop->_head = clone_head;     // New loop header
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1951
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1952
  set_idom(loop_head,  loop_head ->in(LoopNode::EntryControl), dd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1953
  set_idom(clone_head, clone_head->in(LoopNode::EntryControl), dd);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1954
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1955
  // Kill the clone's backedge
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1956
  Node *newcle = old_new[loop_end->_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1957
  _igvn.hash_delete( newcle );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1958
  Node *one = _igvn.intcon(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1959
  set_ctrl(one, C->root());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1960
  newcle->set_req(1, one);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1961
  // Force clone into same loop body
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1962
  uint max = loop->_body.size();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1963
  for( uint k = 0; k < max; k++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1964
    Node *old = loop->_body.at(k);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1965
    Node *nnn = old_new[old->_idx];
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1966
    loop->_body.push(nnn);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1967
    if (!has_ctrl(old))
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1968
      set_loop(nnn, loop);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1969
  }
1399
9648dfd4ce09 6384206: Phis which are later unneeded are impairing our ability to inline based on static types
never
parents: 781
diff changeset
  1970
9648dfd4ce09 6384206: Phis which are later unneeded are impairing our ability to inline based on static types
never
parents: 781
diff changeset
  1971
  loop->record_for_igvn();
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  1972
  loop_head->clear_strip_mined();
30593
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1973
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1974
#ifndef PRODUCT
46630
75aa3e39d02c 8182299: Enable disabled clang warnings, build on OSX 10 + Xcode 8
jwilhelm
parents: 44314
diff changeset
  1975
  if (C->do_vector_loop() && (PrintOpto && (VerifyLoopOptimizations || TraceLoopOpts))) {
30593
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1976
    tty->print("\nnew loop after unroll\n");       loop->dump_head();
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1977
    for (uint i = 0; i < loop->_body.size(); i++) {
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1978
      loop->_body.at(i)->dump();
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1979
    }
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1980
    if(C->clone_map().is_debug()) {
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1981
      tty->print("\nCloneMap\n");
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1982
      Dict* dict = C->clone_map().dict();
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1983
      DictI i(dict);
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1984
      tty->print_cr("Dict@%p[%d] = ", dict, dict->Size());
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1985
      for (int ii = 0; i.test(); ++i, ++ii) {
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1986
        NodeCloneInfo cl((uint64_t)dict->operator[]((void*)i._key));
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1987
        tty->print("%d->%d:%d,", (int)(intptr_t)i._key, cl.idx(), cl.gen());
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1988
        if (ii % 10 == 9) {
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1989
          tty->print_cr(" ");
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1990
        }
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1991
      }
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1992
      tty->print_cr(" ");
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1993
    }
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1994
  }
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1995
#endif
69f942690128 8076284: Improve vectorization of parallel streams
kvn
parents: 30211
diff changeset
  1996
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1997
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1998
489c9b5090e2 Initial load
duke
parents:
diff changeset
  1999
//------------------------------do_maximally_unroll----------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2000
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2001
void PhaseIdealLoop::do_maximally_unroll( IdealLoopTree *loop, Node_List &old_new ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2002
  CountedLoopNode *cl = loop->_head->as_CountedLoop();
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2003
  assert(cl->has_exact_trip_count(), "trip count is not exact");
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2004
  assert(cl->trip_count() > 0, "");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2005
#ifndef PRODUCT
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2006
  if (TraceLoopOpts) {
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2007
    tty->print("MaxUnroll  %d ", cl->trip_count());
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2008
    loop->dump_head();
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2009
  }
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2010
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2011
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2012
  // If loop is tripping an odd number of times, peel odd iteration
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2013
  if ((cl->trip_count() & 1) == 1) {
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2014
    do_peeling(loop, old_new);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2015
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2016
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2017
  // Now its tripping an even number of times remaining.  Double loop body.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2018
  // Do not adjust pre-guards; they are not needed and do not exist.
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2019
  if (cl->trip_count() > 0) {
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2020
    assert((cl->trip_count() & 1) == 0, "missed peeling");
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2021
    do_unroll(loop, old_new, false);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2022
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2023
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2024
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2025
void PhaseIdealLoop::mark_reductions(IdealLoopTree *loop) {
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2026
  if (SuperWordReductions == false) return;
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2027
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2028
  CountedLoopNode* loop_head = loop->_head->as_CountedLoop();
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2029
  if (loop_head->unrolled_count() > 1) {
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2030
    return;
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2031
  }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2032
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2033
  Node* trip_phi = loop_head->phi();
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2034
  for (DUIterator_Fast imax, i = loop_head->fast_outs(imax); i < imax; i++) {
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2035
    Node* phi = loop_head->fast_out(i);
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2036
    if (phi->is_Phi() && phi->outcnt() > 0 && phi != trip_phi) {
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2037
      // For definitions which are loop inclusive and not tripcounts.
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2038
      Node* def_node = phi->in(LoopNode::LoopBackControl);
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2039
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2040
      if (def_node != NULL) {
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2041
        Node* n_ctrl = get_ctrl(def_node);
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2042
        if (n_ctrl != NULL && loop->is_member(get_loop(n_ctrl))) {
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2043
          // Now test it to see if it fits the standard pattern for a reduction operator.
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2044
          int opc = def_node->Opcode();
54022
ff399127078a 8217561: X86: Add floating-point Math.min/max intrinsics
bsrbnd
parents: 53306
diff changeset
  2045
          if (opc != ReductionNode::opcode(opc, def_node->bottom_type()->basic_type())
ff399127078a 8217561: X86: Add floating-point Math.min/max intrinsics
bsrbnd
parents: 53306
diff changeset
  2046
              || opc == Op_MinD || opc == Op_MinF || opc == Op_MaxD || opc == Op_MaxF) {
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2047
            if (!def_node->is_reduction()) { // Not marked yet
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2048
              // To be a reduction, the arithmetic node must have the phi as input and provide a def to it
31048
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2049
              bool ok = false;
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2050
              for (unsigned j = 1; j < def_node->req(); j++) {
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2051
                Node* in = def_node->in(j);
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2052
                if (in == phi) {
31048
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2053
                  ok = true;
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2054
                  break;
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2055
                }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2056
              }
31048
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2057
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2058
              // do nothing if we did not match the initial criteria
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2059
              if (ok == false) {
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2060
                continue;
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2061
              }
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2062
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2063
              // The result of the reduction must not be used in the loop
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2064
              for (DUIterator_Fast imax, i = def_node->fast_outs(imax); i < imax && ok; i++) {
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2065
                Node* u = def_node->fast_out(i);
42622
0d672faf5c79 8170470: superword may miss reductions
roland
parents: 40031
diff changeset
  2066
                if (!loop->is_member(get_loop(ctrl_or_self(u)))) {
31048
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2067
                  continue;
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2068
                }
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2069
                if (u == phi) {
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2070
                  continue;
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2071
                }
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2072
                ok = false;
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2073
              }
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2074
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2075
              // iff the uses conform
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2076
              if (ok) {
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2077
                def_node->add_flag(Node::Flag_is_reduction);
31403
7bf7e00a1aac 8080325: SuperWord loop unrolling analysis
mcberg
parents: 31133
diff changeset
  2078
                loop_head->mark_has_reductions();
31048
ad1dfe0256a7 8080976: Unexpected AIOOB thrown from 1.9.0-ea-b64 on (regression)
roland
parents: 30593
diff changeset
  2079
              }
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2080
            }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2081
          }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2082
        }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2083
      }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2084
    }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2085
  }
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2086
}
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  2087
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2088
//------------------------------adjust_limit-----------------------------------
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2089
// Helper function for add_constraint().
53169
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2090
Node* PhaseIdealLoop::adjust_limit(int stride_con, Node * scale, Node *offset, Node *rc_limit, Node *loop_limit, Node *pre_ctrl, bool round_up) {
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2091
  // Compute "I :: (limit-offset)/scale"
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2092
  Node *con = new SubINode(rc_limit, offset);
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2093
  register_new_node(con, pre_ctrl);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2094
  Node *X = new DivINode(0, con, scale);
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2095
  register_new_node(X, pre_ctrl);
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2096
53169
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2097
  // When the absolute value of scale is greater than one, the integer
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2098
  // division may round limit down so add one to the limit.
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2099
  if (round_up) {
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2100
    X = new AddINode(X, _igvn.intcon(1));
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2101
    register_new_node(X, pre_ctrl);
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2102
  }
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2103
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2104
  // Adjust loop limit
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2105
  loop_limit = (stride_con > 0)
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2106
               ? (Node*)(new MinINode(loop_limit, X))
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2107
               : (Node*)(new MaxINode(loop_limit, X));
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2108
  register_new_node(loop_limit, pre_ctrl);
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2109
  return loop_limit;
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2110
}
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2111
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2112
//------------------------------add_constraint---------------------------------
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2113
// Constrain the main loop iterations so the conditions:
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2114
//    low_limit <= scale_con * I + offset  <  upper_limit
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2115
// always holds true.  That is, either increase the number of iterations in
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2116
// the pre-loop or the post-loop until the condition holds true in the main
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2117
// loop.  Stride, scale, offset and limit are all loop invariant.  Further,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2118
// stride and scale are constants (offset and limit often are).
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2119
void PhaseIdealLoop::add_constraint( int stride_con, int scale_con, Node *offset, Node *low_limit, Node *upper_limit, Node *pre_ctrl, Node **pre_limit, Node **main_limit ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2120
  // For positive stride, the pre-loop limit always uses a MAX function
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2121
  // and the main loop a MIN function.  For negative stride these are
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2122
  // reversed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2123
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2124
  // Also for positive stride*scale the affine function is increasing, so the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2125
  // pre-loop must check for underflow and the post-loop for overflow.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2126
  // Negative stride*scale reverses this; pre-loop checks for overflow and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2127
  // post-loop for underflow.
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2128
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2129
  Node *scale = _igvn.intcon(scale_con);
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2130
  set_ctrl(scale, C->root());
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2131
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2132
  if ((stride_con^scale_con) >= 0) { // Use XOR to avoid overflow
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2133
    // The overflow limit: scale*I+offset < upper_limit
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2134
    // For main-loop compute
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2135
    //   ( if (scale > 0) /* and stride > 0 */
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2136
    //       I < (upper_limit-offset)/scale
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2137
    //     else /* scale < 0 and stride < 0 */
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2138
    //       I > (upper_limit-offset)/scale
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2139
    //   )
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2140
    //
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2141
    // (upper_limit-offset) may overflow or underflow.
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2142
    // But it is fine since main loop will either have
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2143
    // less iterations or will be skipped in such case.
53169
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2144
    *main_limit = adjust_limit(stride_con, scale, offset, upper_limit, *main_limit, pre_ctrl, false);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2145
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2146
    // The underflow limit: low_limit <= scale*I+offset.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2147
    // For pre-loop compute
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2148
    //   NOT(scale*I+offset >= low_limit)
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2149
    //   scale*I+offset < low_limit
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2150
    //   ( if (scale > 0) /* and stride > 0 */
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2151
    //       I < (low_limit-offset)/scale
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2152
    //     else /* scale < 0 and stride < 0 */
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2153
    //       I > (low_limit-offset)/scale
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2154
    //   )
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2155
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2156
    if (low_limit->get_int() == -max_jint) {
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2157
      // We need this guard when scale*pre_limit+offset >= limit
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2158
      // due to underflow. So we need execute pre-loop until
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2159
      // scale*I+offset >= min_int. But (min_int-offset) will
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2160
      // underflow when offset > 0 and X will be > original_limit
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2161
      // when stride > 0. To avoid it we replace positive offset with 0.
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2162
      //
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2163
      // Also (min_int+1 == -max_int) is used instead of min_int here
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2164
      // to avoid problem with scale == -1 (min_int/(-1) == min_int).
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2165
      Node* shift = _igvn.intcon(31);
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2166
      set_ctrl(shift, C->root());
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2167
      Node* sign = new RShiftINode(offset, shift);
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2168
      register_new_node(sign, pre_ctrl);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2169
      offset = new AndINode(offset, sign);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2170
      register_new_node(offset, pre_ctrl);
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2171
    } else {
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2172
      assert(low_limit->get_int() == 0, "wrong low limit for range check");
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2173
      // The only problem we have here when offset == min_int
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2174
      // since (0-min_int) == min_int. It may be fine for stride > 0
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2175
      // but for stride < 0 X will be < original_limit. To avoid it
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2176
      // max(pre_limit, original_limit) is used in do_range_check().
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2177
    }
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2178
    // Pass (-stride) to indicate pre_loop_cond = NOT(main_loop_cond);
53169
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2179
    *pre_limit = adjust_limit((-stride_con), scale, offset, low_limit, *pre_limit, pre_ctrl,
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2180
                              scale_con > 1 && stride_con > 0);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2181
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2182
  } else { // stride_con*scale_con < 0
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2183
    // For negative stride*scale pre-loop checks for overflow and
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2184
    // post-loop for underflow.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2185
    //
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2186
    // The overflow limit: scale*I+offset < upper_limit
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2187
    // For pre-loop compute
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2188
    //   NOT(scale*I+offset < upper_limit)
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2189
    //   scale*I+offset >= upper_limit
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2190
    //   scale*I+offset+1 > upper_limit
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2191
    //   ( if (scale < 0) /* and stride > 0 */
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2192
    //       I < (upper_limit-(offset+1))/scale
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2193
    //     else /* scale > 0 and stride < 0 */
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2194
    //       I > (upper_limit-(offset+1))/scale
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2195
    //   )
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2196
    //
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2197
    // (upper_limit-offset-1) may underflow or overflow.
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2198
    // To avoid it min(pre_limit, original_limit) is used
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2199
    // in do_range_check() for stride > 0 and max() for < 0.
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2200
    Node *one  = _igvn.intcon(1);
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2201
    set_ctrl(one, C->root());
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2202
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2203
    Node *plus_one = new AddINode(offset, one);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2204
    register_new_node( plus_one, pre_ctrl );
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2205
    // Pass (-stride) to indicate pre_loop_cond = NOT(main_loop_cond);
53169
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2206
    *pre_limit = adjust_limit((-stride_con), scale, plus_one, upper_limit, *pre_limit, pre_ctrl,
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2207
                              scale_con < -1 && stride_con > 0);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2208
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2209
    if (low_limit->get_int() == -max_jint) {
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2210
      // We need this guard when scale*main_limit+offset >= limit
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2211
      // due to underflow. So we need execute main-loop while
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2212
      // scale*I+offset+1 > min_int. But (min_int-offset-1) will
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2213
      // underflow when (offset+1) > 0 and X will be < main_limit
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2214
      // when scale < 0 (and stride > 0). To avoid it we replace
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2215
      // positive (offset+1) with 0.
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2216
      //
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2217
      // Also (min_int+1 == -max_int) is used instead of min_int here
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2218
      // to avoid problem with scale == -1 (min_int/(-1) == min_int).
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2219
      Node* shift = _igvn.intcon(31);
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2220
      set_ctrl(shift, C->root());
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2221
      Node* sign = new RShiftINode(plus_one, shift);
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2222
      register_new_node(sign, pre_ctrl);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2223
      plus_one = new AndINode(plus_one, sign);
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2224
      register_new_node(plus_one, pre_ctrl);
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2225
    } else {
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2226
      assert(low_limit->get_int() == 0, "wrong low limit for range check");
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2227
      // The only problem we have here when offset == max_int
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2228
      // since (max_int+1) == min_int and (0-min_int) == min_int.
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2229
      // But it is fine since main loop will either have
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2230
      // less iterations or will be skipped in such case.
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2231
    }
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2232
    // The underflow limit: low_limit <= scale*I+offset.
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2233
    // For main-loop compute
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2234
    //   scale*I+offset+1 > low_limit
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2235
    //   ( if (scale < 0) /* and stride > 0 */
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2236
    //       I < (low_limit-(offset+1))/scale
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2237
    //     else /* scale > 0 and stride < 0 */
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2238
    //       I > (low_limit-(offset+1))/scale
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2239
    //   )
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2240
53169
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2241
    *main_limit = adjust_limit(stride_con, scale, plus_one, low_limit, *main_limit, pre_ctrl,
98580226126d 8215265: C2: range check elimination may allow illegal out of bound access
roland
parents: 53167
diff changeset
  2242
                               false);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2243
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2244
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2245
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2246
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2247
//------------------------------is_scaled_iv---------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2248
// Return true if exp is a constant times an induction var
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2249
bool PhaseIdealLoop::is_scaled_iv(Node* exp, Node* iv, int* p_scale) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2250
  if (exp == iv) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2251
    if (p_scale != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2252
      *p_scale = 1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2253
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2254
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2255
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2256
  int opc = exp->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2257
  if (opc == Op_MulI) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2258
    if (exp->in(1) == iv && exp->in(2)->is_Con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2259
      if (p_scale != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2260
        *p_scale = exp->in(2)->get_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2261
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2262
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2263
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2264
    if (exp->in(2) == iv && exp->in(1)->is_Con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2265
      if (p_scale != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2266
        *p_scale = exp->in(1)->get_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2267
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2268
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2269
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2270
  } else if (opc == Op_LShiftI) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2271
    if (exp->in(1) == iv && exp->in(2)->is_Con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2272
      if (p_scale != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2273
        *p_scale = 1 << exp->in(2)->get_int();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2274
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2275
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2276
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2277
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2278
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2279
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2280
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2281
//-----------------------------is_scaled_iv_plus_offset------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2282
// Return true if exp is a simple induction variable expression: k1*iv + (invar + k2)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2283
bool PhaseIdealLoop::is_scaled_iv_plus_offset(Node* exp, Node* iv, int* p_scale, Node** p_offset, int depth) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2284
  if (is_scaled_iv(exp, iv, p_scale)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2285
    if (p_offset != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2286
      Node *zero = _igvn.intcon(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2287
      set_ctrl(zero, C->root());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2288
      *p_offset = zero;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2289
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2290
    return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2291
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2292
  int opc = exp->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2293
  if (opc == Op_AddI) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2294
    if (is_scaled_iv(exp->in(1), iv, p_scale)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2295
      if (p_offset != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2296
        *p_offset = exp->in(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2297
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2298
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2299
    }
35139
071b000357fc 8145754: PhaseIdealLoop::is_scaled_iv_plus_offset() does not match AddI
thartmann
parents: 34174
diff changeset
  2300
    if (is_scaled_iv(exp->in(2), iv, p_scale)) {
071b000357fc 8145754: PhaseIdealLoop::is_scaled_iv_plus_offset() does not match AddI
thartmann
parents: 34174
diff changeset
  2301
      if (p_offset != NULL) {
071b000357fc 8145754: PhaseIdealLoop::is_scaled_iv_plus_offset() does not match AddI
thartmann
parents: 34174
diff changeset
  2302
        *p_offset = exp->in(1);
071b000357fc 8145754: PhaseIdealLoop::is_scaled_iv_plus_offset() does not match AddI
thartmann
parents: 34174
diff changeset
  2303
      }
071b000357fc 8145754: PhaseIdealLoop::is_scaled_iv_plus_offset() does not match AddI
thartmann
parents: 34174
diff changeset
  2304
      return true;
071b000357fc 8145754: PhaseIdealLoop::is_scaled_iv_plus_offset() does not match AddI
thartmann
parents: 34174
diff changeset
  2305
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2306
    if (exp->in(2)->is_Con()) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2307
      Node* offset2 = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2308
      if (depth < 2 &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2309
          is_scaled_iv_plus_offset(exp->in(1), iv, p_scale,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2310
                                   p_offset != NULL ? &offset2 : NULL, depth+1)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2311
        if (p_offset != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2312
          Node *ctrl_off2 = get_ctrl(offset2);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2313
          Node* offset = new AddINode(offset2, exp->in(2));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2314
          register_new_node(offset, ctrl_off2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2315
          *p_offset = offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2316
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2317
        return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2318
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2319
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2320
  } else if (opc == Op_SubI) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2321
    if (is_scaled_iv(exp->in(1), iv, p_scale)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2322
      if (p_offset != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2323
        Node *zero = _igvn.intcon(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2324
        set_ctrl(zero, C->root());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2325
        Node *ctrl_off = get_ctrl(exp->in(2));
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2326
        Node* offset = new SubINode(zero, exp->in(2));
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2327
        register_new_node(offset, ctrl_off);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2328
        *p_offset = offset;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2329
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2330
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2331
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2332
    if (is_scaled_iv(exp->in(2), iv, p_scale)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2333
      if (p_offset != NULL) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2334
        *p_scale *= -1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2335
        *p_offset = exp->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2336
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2337
      return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2338
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2339
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2340
  return false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2341
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2342
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2343
// Same as PhaseIdealLoop::duplicate_predicates() but for range checks
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2344
// eliminated by iteration splitting.
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2345
Node* PhaseIdealLoop::add_range_check_predicate(IdealLoopTree* loop, CountedLoopNode* cl,
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2346
                                                Node* predicate_proj, int scale_con, Node* offset,
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2347
                                                Node* limit, jint stride_con, Node* value) {
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2348
  bool overflow = false;
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2349
  BoolNode* bol = rc_predicate(loop, predicate_proj, scale_con, offset, value, NULL, stride_con, limit, (stride_con > 0) != (scale_con > 0), overflow);
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2350
  Node* opaque_bol = new Opaque4Node(C, bol, _igvn.intcon(1));
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2351
  register_new_node(opaque_bol, predicate_proj);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2352
  IfNode* new_iff = NULL;
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2353
  if (overflow) {
53063
b04860fd2e2c 8211698: Crash in C2 compiled code during execution of double array heavy processing code
rraghavan
parents: 52925
diff changeset
  2354
    new_iff = new IfNode(predicate_proj, opaque_bol, PROB_MAX, COUNT_UNKNOWN);
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2355
  } else {
53063
b04860fd2e2c 8211698: Crash in C2 compiled code during execution of double array heavy processing code
rraghavan
parents: 52925
diff changeset
  2356
    new_iff = new RangeCheckNode(predicate_proj, opaque_bol, PROB_MAX, COUNT_UNKNOWN);
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2357
  }
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2358
  register_control(new_iff, loop->_parent, predicate_proj);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2359
  Node* iffalse = new IfFalseNode(new_iff);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2360
  register_control(iffalse, _ltree_root, new_iff);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2361
  ProjNode* iftrue = new IfTrueNode(new_iff);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2362
  register_control(iftrue, loop->_parent, new_iff);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2363
  Node *frame = new ParmNode(C->start(), TypeFunc::FramePtr);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2364
  register_new_node(frame, C->start());
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2365
  Node* halt = new HaltNode(iffalse, frame);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2366
  register_control(halt, _ltree_root, iffalse);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2367
  C->root()->add_req(halt);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2368
  return iftrue;
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2369
}
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2370
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2371
//------------------------------do_range_check---------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2372
// Eliminate range-checks and other trip-counter vs loop-invariant tests.
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2373
int PhaseIdealLoop::do_range_check( IdealLoopTree *loop, Node_List &old_new ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2374
#ifndef PRODUCT
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2375
  if (PrintOpto && VerifyLoopOptimizations) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2376
    tty->print("Range Check Elimination ");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2377
    loop->dump_head();
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2378
  } else if (TraceLoopOpts) {
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2379
    tty->print("RangeCheck   ");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2380
    loop->dump_head();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2381
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2382
#endif
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2383
  assert(RangeCheckElimination, "");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2384
  CountedLoopNode *cl = loop->_head->as_CountedLoop();
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2385
  // If we fail before trying to eliminate range checks, set multiversion state
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2386
  int closed_range_checks = 1;
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2387
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2388
  // protect against stride not being a constant
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2389
  if (!cl->stride_is_con())
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2390
    return closed_range_checks;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2391
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2392
  // Find the trip counter; we are iteration splitting based on it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2393
  Node *trip_counter = cl->phi();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2394
  // Find the main loop limit; we will trim it's iterations
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2395
  // to not ever trip end tests
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2396
  Node *main_limit = cl->limit();
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2397
36809
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  2398
  // Check graph shape. Cannot optimize a loop if zero-trip
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  2399
  // Opaque1 node is optimized away and then another round
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  2400
  // of loop opts attempted.
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2401
  if (!is_canonical_loop_entry(cl)) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2402
    return closed_range_checks;
36809
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  2403
  }
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  2404
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2405
  // Need to find the main-loop zero-trip guard
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2406
  Node *ctrl  = cl->skip_predicates();
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2407
  Node *iffm = ctrl->in(0);
36809
6f9362b27d4f 8148754: C2 loop unrolling fails due to unexpected graph shape
zmajo
parents: 36316
diff changeset
  2408
  Node *opqzm = iffm->in(1)->in(1)->in(2);
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2409
  assert(opqzm->in(1) == main_limit, "do not understand situation");
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2410
31133
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2411
  // Find the pre-loop limit; we will expand its iterations to
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2412
  // not ever trip low tests.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2413
  Node *p_f = iffm->in(0);
31131
d92082b3ef93 8078866: compiler/eliminateAutobox/6934604/TestIntBoxing.java assert(p_f->Opcode() == Op_IfFalse) failed
roland
parents: 31048
diff changeset
  2414
  // pre loop may have been optimized out
d92082b3ef93 8078866: compiler/eliminateAutobox/6934604/TestIntBoxing.java assert(p_f->Opcode() == Op_IfFalse) failed
roland
parents: 31048
diff changeset
  2415
  if (p_f->Opcode() != Op_IfFalse) {
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2416
    return closed_range_checks;
31131
d92082b3ef93 8078866: compiler/eliminateAutobox/6934604/TestIntBoxing.java assert(p_f->Opcode() == Op_IfFalse) failed
roland
parents: 31048
diff changeset
  2417
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2418
  CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2419
  assert(pre_end->loopnode()->is_pre_loop(), "");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2420
  Node *pre_opaq1 = pre_end->limit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2421
  // Occasionally it's possible for a pre-loop Opaque1 node to be
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2422
  // optimized away and then another round of loop opts attempted.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2423
  // We can not optimize this particular loop in that case.
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2424
  if (pre_opaq1->Opcode() != Op_Opaque1)
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2425
    return closed_range_checks;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2426
  Opaque1Node *pre_opaq = (Opaque1Node*)pre_opaq1;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2427
  Node *pre_limit = pre_opaq->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2428
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2429
  // Where do we put new limit calculations
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2430
  Node *pre_ctrl = pre_end->loopnode()->in(LoopNode::EntryControl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2431
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2432
  // Ensure the original loop limit is available from the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2433
  // pre-loop Opaque1 node.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2434
  Node *orig_limit = pre_opaq->original_loop_limit();
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2435
  if (orig_limit == NULL || _igvn.type(orig_limit) == Type::TOP)
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2436
    return closed_range_checks;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2437
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2438
  // Must know if its a count-up or count-down loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2439
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2440
  int stride_con = cl->stride_con();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2441
  Node *zero = _igvn.intcon(0);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2442
  Node *one  = _igvn.intcon(1);
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2443
  // Use symmetrical int range [-max_jint,max_jint]
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2444
  Node *mini = _igvn.intcon(-max_jint);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2445
  set_ctrl(zero, C->root());
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2446
  set_ctrl(one,  C->root());
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2447
  set_ctrl(mini, C->root());
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2448
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2449
  // Range checks that do not dominate the loop backedge (ie.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2450
  // conditionally executed) can lengthen the pre loop limit beyond
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2451
  // the original loop limit. To prevent this, the pre limit is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2452
  // (for stride > 0) MINed with the original loop limit (MAXed
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2453
  // stride < 0) when some range_check (rc) is conditionally
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2454
  // executed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2455
  bool conditional_rc = false;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2456
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2457
  // Count number of range checks and reduce by load range limits, if zero,
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2458
  // the loop is in canonical form to multiversion.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2459
  closed_range_checks = 0;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2460
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2461
  Node* predicate_proj = cl->skip_strip_mined()->in(LoopNode::EntryControl);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2462
  assert(predicate_proj->is_Proj() && predicate_proj->in(0)->is_If(), "if projection only");
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
  2463
  // Check loop body for tests of trip-counter plus loop-invariant vs loop-variant.
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2464
  for( uint i = 0; i < loop->_body.size(); i++ ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2465
    Node *iff = loop->_body[i];
34164
a9e6034d7707 8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents: 33628
diff changeset
  2466
    if (iff->Opcode() == Op_If ||
a9e6034d7707 8137168: Replace IfNode with a new RangeCheckNode for range checks
roland
parents: 33628
diff changeset
  2467
        iff->Opcode() == Op_RangeCheck) { // Test?
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2468
      // Test is an IfNode, has 2 projections.  If BOTH are in the loop
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2469
      // we need loop unswitching instead of iteration splitting.
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2470
      closed_range_checks++;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2471
      Node *exit = loop->is_loop_exit(iff);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2472
      if( !exit ) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2473
      int flip = (exit->Opcode() == Op_IfTrue) ? 1 : 0;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2474
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2475
      // Get boolean condition to test
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2476
      Node *i1 = iff->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2477
      if( !i1->is_Bool() ) continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2478
      BoolNode *bol = i1->as_Bool();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2479
      BoolTest b_test = bol->_test;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2480
      // Flip sense of test if exit condition is flipped
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2481
      if( flip )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2482
        b_test = b_test.negate();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2483
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2484
      // Get compare
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2485
      Node *cmp = bol->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2486
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2487
      // Look for trip_counter + offset vs limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2488
      Node *rc_exp = cmp->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2489
      Node *limit  = cmp->in(2);
47765
b7c7428eaab9 8189610: Reconcile jvm.h and all jvm_md.h between java.base and hotspot
coleenp
parents: 47591
diff changeset
  2490
      int scale_con= 1;        // Assume trip counter not scaled
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2491
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2492
      Node *limit_c = get_ctrl(limit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2493
      if( loop->is_member(get_loop(limit_c) ) ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2494
        // Compare might have operands swapped; commute them
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2495
        b_test = b_test.commute();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2496
        rc_exp = cmp->in(2);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2497
        limit  = cmp->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2498
        limit_c = get_ctrl(limit);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2499
        if( loop->is_member(get_loop(limit_c) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2500
          continue;             // Both inputs are loop varying; cannot RCE
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2501
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2502
      // Here we know 'limit' is loop invariant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2503
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2504
      // 'limit' maybe pinned below the zero trip test (probably from a
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2505
      // previous round of rce), in which case, it can't be used in the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2506
      // zero trip test expression which must occur before the zero test's if.
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2507
      if (is_dominator(ctrl, limit_c)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2508
        continue;  // Don't rce this check but continue looking for other candidates.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2509
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2510
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2511
      // Check for scaled induction variable plus an offset
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2512
      Node *offset = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2513
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2514
      if (!is_scaled_iv_plus_offset(rc_exp, trip_counter, &scale_con, &offset)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2515
        continue;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2516
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2517
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2518
      Node *offset_c = get_ctrl(offset);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2519
      if( loop->is_member( get_loop(offset_c) ) )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2520
        continue;               // Offset is not really loop invariant
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2521
      // Here we know 'offset' is loop invariant.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2522
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2523
      // As above for the 'limit', the 'offset' maybe pinned below the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2524
      // zero trip test.
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2525
      if (is_dominator(ctrl, offset_c)) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2526
        continue; // Don't rce this check but continue looking for other candidates.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2527
      }
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2528
#ifdef ASSERT
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2529
      if (TraceRangeLimitCheck) {
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2530
        tty->print_cr("RC bool node%s", flip ? " flipped:" : ":");
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2531
        bol->dump(2);
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2532
      }
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2533
#endif
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2534
      // At this point we have the expression as:
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2535
      //   scale_con * trip_counter + offset :: limit
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2536
      // where scale_con, offset and limit are loop invariant.  Trip_counter
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2537
      // monotonically increases by stride_con, a constant.  Both (or either)
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2538
      // stride_con and scale_con can be negative which will flip about the
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2539
      // sense of the test.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2540
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2541
      // Adjust pre and main loop limits to guard the correct iteration set
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2542
      if( cmp->Opcode() == Op_CmpU ) {// Unsigned compare is really 2 tests
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2543
        if( b_test._test == BoolTest::lt ) { // Range checks always use lt
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2544
          // The underflow and overflow limits: 0 <= scale*I+offset < limit
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2545
          add_constraint( stride_con, scale_con, offset, zero, limit, pre_ctrl, &pre_limit, &main_limit );
39429
2c9cde29cd3f 8159715: Fix for 8072422 is incorrect
thartmann
parents: 39419
diff changeset
  2546
          // (0-offset)/scale could be outside of loop iterations range.
2c9cde29cd3f 8159715: Fix for 8072422 is incorrect
thartmann
parents: 39419
diff changeset
  2547
          conditional_rc = true;
53306
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2548
          Node* init = cl->init_trip();
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2549
          Node* opaque_init = new Opaque1Node(C, init);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2550
          register_new_node(opaque_init, predicate_proj);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2551
          // template predicate so it can be updated on next unrolling
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2552
          predicate_proj = add_range_check_predicate(loop, cl, predicate_proj, scale_con, offset, limit, stride_con, opaque_init);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2553
          assert(skeleton_predicate_has_opaque(predicate_proj->in(0)->as_If()), "unexpected");
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2554
          // predicate on first value of first iteration
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2555
          predicate_proj = add_range_check_predicate(loop, cl, predicate_proj, scale_con, offset, limit, stride_con, init);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2556
          assert(!skeleton_predicate_has_opaque(predicate_proj->in(0)->as_If()), "unexpected");
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2557
          int init_inc = stride_con/cl->unrolled_count();
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2558
          assert(init_inc != 0, "invalid loop increment");
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2559
          Node* max_value = _igvn.intcon(stride_con - init_inc);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2560
          max_value = new AddINode(init, max_value);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2561
          register_new_node(max_value, predicate_proj);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2562
          // predicate on last value of first iteration (in case unrolling has already happened)
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2563
          predicate_proj = add_range_check_predicate(loop, cl, predicate_proj, scale_con, offset, limit, stride_con, max_value);
8e260023fc53 8216135: C2 assert(!had_error) failed: bad dominance
roland
parents: 53169
diff changeset
  2564
          assert(!skeleton_predicate_has_opaque(predicate_proj->in(0)->as_If()), "unexpected");
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2565
        } else {
34174
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
  2566
          if (PrintOpto) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2567
            tty->print_cr("missed RCE opportunity");
34174
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
  2568
          }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2569
          continue;             // In release mode, ignore it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2570
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2571
      } else {                  // Otherwise work on normal compares
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2572
        switch( b_test._test ) {
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2573
        case BoolTest::gt:
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2574
          // Fall into GE case
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2575
        case BoolTest::ge:
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2576
          // Convert (I*scale+offset) >= Limit to (I*(-scale)+(-offset)) <= -Limit
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2577
          scale_con = -scale_con;
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2578
          offset = new SubINode( zero, offset );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2579
          register_new_node( offset, pre_ctrl );
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2580
          limit  = new SubINode( zero, limit );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2581
          register_new_node( limit, pre_ctrl );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2582
          // Fall into LE case
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2583
        case BoolTest::le:
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2584
          if (b_test._test != BoolTest::gt) {
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2585
            // Convert X <= Y to X < Y+1
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2586
            limit = new AddINode( limit, one );
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2587
            register_new_node( limit, pre_ctrl );
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2588
          }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2589
          // Fall into LT case
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2590
        case BoolTest::lt:
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2591
          // The underflow and overflow limits: MIN_INT <= scale*I+offset < limit
9941
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2592
          // Note: (MIN_INT+1 == -MAX_INT) is used instead of MIN_INT here
f2365fbd62f4 7044725: -XX:-UnrollLimitCheck -Xcomp : Exception: String index out of range: 29488
kvn
parents: 9634
diff changeset
  2593
          // to avoid problem with scale == -1: MIN_INT/(-1) == MIN_INT.
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2594
          add_constraint( stride_con, scale_con, offset, mini, limit, pre_ctrl, &pre_limit, &main_limit );
39429
2c9cde29cd3f 8159715: Fix for 8072422 is incorrect
thartmann
parents: 39419
diff changeset
  2595
          // ((MIN_INT+1)-offset)/scale could be outside of loop iterations range.
2c9cde29cd3f 8159715: Fix for 8072422 is incorrect
thartmann
parents: 39419
diff changeset
  2596
          // Note: negative offset is replaced with 0 but (MIN_INT+1)/scale could
2c9cde29cd3f 8159715: Fix for 8072422 is incorrect
thartmann
parents: 39419
diff changeset
  2597
          // still be outside of loop range.
2c9cde29cd3f 8159715: Fix for 8072422 is incorrect
thartmann
parents: 39419
diff changeset
  2598
          conditional_rc = true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2599
          break;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2600
        default:
34174
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
  2601
          if (PrintOpto) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2602
            tty->print_cr("missed RCE opportunity");
34174
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
  2603
          }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2604
          continue;             // Unhandled case
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2605
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2606
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2607
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2608
      // Kill the eliminated test
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2609
      C->set_major_progress();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2610
      Node *kill_con = _igvn.intcon( 1-flip );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2611
      set_ctrl(kill_con, C->root());
12958
009b6c9586d8 7173340: C2: code cleanup: use PhaseIterGVN::replace_edge(Node*, int, Node*) where applicable
kvn
parents: 10538
diff changeset
  2612
      _igvn.replace_input_of(iff, 1, kill_con);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2613
      // Find surviving projection
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2614
      assert(iff->is_If(), "");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2615
      ProjNode* dp = ((IfNode*)iff)->proj_out(1-flip);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2616
      // Find loads off the surviving projection; remove their control edge
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2617
      for (DUIterator_Fast imax, i = dp->fast_outs(imax); i < imax; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2618
        Node* cd = dp->fast_out(i); // Control-dependent node
21526
03b4acedb351 8017065: C2 allows safepoint checks to leak into G1 pre-barriers
iveresov
parents: 21208
diff changeset
  2619
        if (cd->is_Load() && cd->depends_only_on_test()) {   // Loads can now float around in the loop
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2620
          // Allow the load to float around in the loop, or before it
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2621
          // but NOT before the pre-loop.
12958
009b6c9586d8 7173340: C2: code cleanup: use PhaseIterGVN::replace_edge(Node*, int, Node*) where applicable
kvn
parents: 10538
diff changeset
  2622
          _igvn.replace_input_of(cd, 0, ctrl); // ctrl, not NULL
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2623
          --i;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2624
          --imax;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2625
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2626
      }
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2627
      if (limit->Opcode() == Op_LoadRange) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2628
        closed_range_checks--;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2629
      }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2630
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2631
    } // End of is IF
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2632
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2633
  }
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2634
  if (predicate_proj != cl->skip_strip_mined()->in(LoopNode::EntryControl)) {
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2635
    _igvn.replace_input_of(cl->skip_strip_mined(), LoopNode::EntryControl, predicate_proj);
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2636
    set_idom(cl->skip_strip_mined(), predicate_proj, dom_depth(cl->skip_strip_mined()));
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2637
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2638
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2639
  // Update loop limits
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2640
  if (conditional_rc) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2641
    pre_limit = (stride_con > 0) ? (Node*)new MinINode(pre_limit, orig_limit)
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  2642
                                 : (Node*)new MaxINode(pre_limit, orig_limit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2643
    register_new_node(pre_limit, pre_ctrl);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2644
  }
25913
81dbc151e91c 8040213: C2 does not put all modified nodes on IGVN worklist
thartmann
parents: 24923
diff changeset
  2645
  _igvn.replace_input_of(pre_opaq, 1, pre_limit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2646
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2647
  // Note:: we are making the main loop limit no longer precise;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2648
  // need to round up based on stride.
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  2649
  cl->set_nonexact_trip_count();
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2650
  Node *main_cle = cl->loopexit();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2651
  Node *main_bol = main_cle->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2652
  // Hacking loop bounds; need private copies of exit test
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2653
  if( main_bol->outcnt() > 1 ) {// BoolNode shared?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2654
    main_bol = main_bol->clone();// Clone a private BoolNode
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2655
    register_new_node( main_bol, main_cle->in(0) );
25927
29dc55d9e8a0 8053915: bigapps assert failure in C2: modified node is not on IGVN._worklist
thartmann
parents: 25913
diff changeset
  2656
    _igvn.replace_input_of(main_cle, 1, main_bol);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2657
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2658
  Node *main_cmp = main_bol->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2659
  if( main_cmp->outcnt() > 1 ) { // CmpNode shared?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2660
    main_cmp = main_cmp->clone();// Clone a private CmpNode
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2661
    register_new_node( main_cmp, main_cle->in(0) );
28639
beee8cc25bbf 8068864: C2 failed: modified node is not on IGVN._worklist
kvn
parents: 27708
diff changeset
  2662
    _igvn.replace_input_of(main_bol, 1, main_cmp);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2663
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2664
  // Hack the now-private loop bounds
12958
009b6c9586d8 7173340: C2: code cleanup: use PhaseIterGVN::replace_edge(Node*, int, Node*) where applicable
kvn
parents: 10538
diff changeset
  2665
  _igvn.replace_input_of(main_cmp, 2, main_limit);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2666
  // The OpaqueNode is unshared by design
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2667
  assert( opqzm->outcnt() == 1, "cannot hack shared node" );
12958
009b6c9586d8 7173340: C2: code cleanup: use PhaseIterGVN::replace_edge(Node*, int, Node*) where applicable
kvn
parents: 10538
diff changeset
  2668
  _igvn.replace_input_of(opqzm, 1, main_limit);
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2669
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2670
  return closed_range_checks;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2671
}
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2672
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2673
//------------------------------has_range_checks-------------------------------
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2674
// Check to see if RCE cleaned the current loop of range-checks.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2675
void PhaseIdealLoop::has_range_checks(IdealLoopTree *loop) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2676
  assert(RangeCheckElimination, "");
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2677
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2678
  // skip if not a counted loop
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2679
  if (!loop->is_counted()) return;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2680
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2681
  CountedLoopNode *cl = loop->_head->as_CountedLoop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2682
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2683
  // skip this loop if it is already checked
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2684
  if (cl->has_been_range_checked()) return;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2685
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
  2686
  // Now check for existence of range checks
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2687
  for (uint i = 0; i < loop->_body.size(); i++) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2688
    Node *iff = loop->_body[i];
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2689
    int iff_opc = iff->Opcode();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2690
    if (iff_opc == Op_If || iff_opc == Op_RangeCheck) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2691
      cl->mark_has_range_checks();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2692
      break;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2693
    }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2694
  }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2695
  cl->set_has_been_range_checked();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2696
}
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2697
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2698
//-------------------------multi_version_post_loops----------------------------
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2699
// Check the range checks that remain, if simple, use the bounds to guard
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2700
// which version to a post loop we execute, one with range checks or one without
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2701
bool PhaseIdealLoop::multi_version_post_loops(IdealLoopTree *rce_loop, IdealLoopTree *legacy_loop) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2702
  bool multi_version_succeeded = false;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2703
  assert(RangeCheckElimination, "");
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2704
  CountedLoopNode *legacy_cl = legacy_loop->_head->as_CountedLoop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2705
  assert(legacy_cl->is_post_loop(), "");
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2706
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
  2707
  // Check for existence of range checks using the unique instance to make a guard with
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2708
  Unique_Node_List worklist;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2709
  for (uint i = 0; i < legacy_loop->_body.size(); i++) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2710
    Node *iff = legacy_loop->_body[i];
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2711
    int iff_opc = iff->Opcode();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2712
    if (iff_opc == Op_If || iff_opc == Op_RangeCheck) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2713
      worklist.push(iff);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2714
    }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2715
  }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2716
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2717
  // Find RCE'd post loop so that we can stage its guard.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2718
  if (!is_canonical_loop_entry(legacy_cl)) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2719
  Node* ctrl = legacy_cl->in(LoopNode::EntryControl);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2720
  Node* iffm = ctrl->in(0);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2721
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2722
  // Now we test that both the post loops are connected
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2723
  Node* post_loop_region = iffm->in(0);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2724
  if (post_loop_region == NULL) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2725
  if (!post_loop_region->is_Region()) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2726
  Node* covering_region = post_loop_region->in(RegionNode::Control+1);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2727
  if (covering_region == NULL) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2728
  if (!covering_region->is_Region()) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2729
  Node* p_f = covering_region->in(RegionNode::Control);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2730
  if (p_f == NULL) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2731
  if (!p_f->is_IfFalse()) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2732
  if (!p_f->in(0)->is_CountedLoopEnd()) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2733
  CountedLoopEndNode* rce_loop_end = p_f->in(0)->as_CountedLoopEnd();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2734
  if (rce_loop_end == NULL) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2735
  CountedLoopNode* rce_cl = rce_loop_end->loopnode();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2736
  if (rce_cl == NULL || !rce_cl->is_post_loop()) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2737
  CountedLoopNode *known_rce_cl = rce_loop->_head->as_CountedLoop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2738
  if (rce_cl != known_rce_cl) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2739
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2740
  // Then we fetch the cover entry test
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2741
  ctrl = rce_cl->in(LoopNode::EntryControl);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2742
  if (!ctrl->is_IfTrue() && !ctrl->is_IfFalse()) return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2743
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2744
#ifndef PRODUCT
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2745
  if (TraceLoopOpts) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2746
    tty->print("PostMultiVersion\n");
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2747
    rce_loop->dump_head();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2748
    legacy_loop->dump_head();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2749
  }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2750
#endif
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2751
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2752
  // Now fetch the limit we want to compare against
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2753
  Node *limit = rce_cl->limit();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2754
  bool first_time = true;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2755
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2756
  // If we got this far, we identified the post loop which has been RCE'd and
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2757
  // we have a work list.  Now we will try to transform the if guard to cause
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2758
  // the loop pair to be multi version executed with the determination left to runtime
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2759
  // or the optimizer if full information is known about the given arrays at compile time.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2760
  Node *last_min = NULL;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2761
  multi_version_succeeded = true;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2762
  while (worklist.size()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2763
    Node* rc_iffm = worklist.pop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2764
    if (rc_iffm->is_If()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2765
      Node *rc_bolzm = rc_iffm->in(1);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2766
      if (rc_bolzm->is_Bool()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2767
        Node *rc_cmpzm = rc_bolzm->in(1);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2768
        if (rc_cmpzm->is_Cmp()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2769
          Node *rc_left = rc_cmpzm->in(2);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2770
          if (rc_left->Opcode() != Op_LoadRange) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2771
            multi_version_succeeded = false;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2772
            break;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2773
          }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2774
          if (first_time) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2775
            last_min = rc_left;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2776
            first_time = false;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2777
          } else {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2778
            Node *cur_min = new MinINode(last_min, rc_left);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2779
            last_min = cur_min;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2780
            _igvn.register_new_node_with_optimizer(last_min);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2781
          }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2782
        }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2783
      }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2784
    }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2785
  }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2786
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2787
  // All we have to do is update the limit of the rce loop
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2788
  // with the min of our expression and the current limit.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2789
  // We will use this expression to replace the current limit.
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2790
  if (last_min && multi_version_succeeded) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2791
    Node *cur_min = new MinINode(last_min, limit);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2792
    _igvn.register_new_node_with_optimizer(cur_min);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2793
    Node *cmp_node = rce_loop_end->cmp_node();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2794
    _igvn.replace_input_of(cmp_node, 2, cur_min);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2795
    set_ctrl(cur_min, ctrl);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2796
    set_loop(cur_min, rce_loop->_parent);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2797
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2798
    legacy_cl->mark_is_multiversioned();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2799
    rce_cl->mark_is_multiversioned();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2800
    multi_version_succeeded = true;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2801
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2802
    C->set_major_progress();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2803
  }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2804
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2805
  return multi_version_succeeded;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2806
}
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2807
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2808
//-------------------------poison_rce_post_loop--------------------------------
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
  2809
// Causes the rce'd post loop to be optimized away if multiversioning fails
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2810
void PhaseIdealLoop::poison_rce_post_loop(IdealLoopTree *rce_loop) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2811
  CountedLoopNode *rce_cl = rce_loop->_head->as_CountedLoop();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2812
  Node* ctrl = rce_cl->in(LoopNode::EntryControl);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2813
  if (ctrl->is_IfTrue() || ctrl->is_IfFalse()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2814
    Node* iffm = ctrl->in(0);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2815
    if (iffm->is_If()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2816
      Node* cur_bool = iffm->in(1);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2817
      if (cur_bool->is_Bool()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2818
        Node* cur_cmp = cur_bool->in(1);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2819
        if (cur_cmp->is_Cmp()) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2820
          BoolTest::mask new_test = BoolTest::gt;
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2821
          BoolNode *new_bool = new BoolNode(cur_cmp, new_test);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2822
          _igvn.replace_node(cur_bool, new_bool);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2823
          _igvn._worklist.push(new_bool);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2824
          Node* left_op = cur_cmp->in(1);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2825
          _igvn.replace_input_of(cur_cmp, 2, left_op);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2826
          C->set_major_progress();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2827
        }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2828
      }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2829
    }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  2830
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2831
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2832
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2833
//------------------------------DCE_loop_body----------------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2834
// Remove simplistic dead code from loop body
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2835
void IdealLoopTree::DCE_loop_body() {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2836
  for( uint i = 0; i < _body.size(); i++ )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2837
    if( _body.at(i)->outcnt() == 0 )
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2838
      _body.map( i--, _body.pop() );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2839
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2840
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2841
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2842
//------------------------------adjust_loop_exit_prob--------------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2843
// Look for loop-exit tests with the 50/50 (or worse) guesses from the parsing stage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2844
// Replace with a 1-in-10 exit guess.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2845
void IdealLoopTree::adjust_loop_exit_prob( PhaseIdealLoop *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2846
  Node *test = tail();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2847
  while( test != _head ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2848
    uint top = test->Opcode();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2849
    if( top == Op_IfTrue || top == Op_IfFalse ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2850
      int test_con = ((ProjNode*)test)->_con;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2851
      assert(top == (uint)(test_con? Op_IfTrue: Op_IfFalse), "sanity");
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2852
      IfNode *iff = test->in(0)->as_If();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2853
      if( iff->outcnt() == 2 ) {        // Ignore dead tests
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2854
        Node *bol = iff->in(1);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2855
        if( bol && bol->req() > 1 && bol->in(1) &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2856
            ((bol->in(1)->Opcode() == Op_StorePConditional ) ||
1500
bea9a90f3e8f 6462850: generate biased locking code in C2 ideal graph
kvn
parents: 1433
diff changeset
  2857
             (bol->in(1)->Opcode() == Op_StoreIConditional ) ||
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2858
             (bol->in(1)->Opcode() == Op_StoreLConditional ) ||
39419
cc993a4ab581 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38129
diff changeset
  2859
             (bol->in(1)->Opcode() == Op_CompareAndExchangeB ) ||
cc993a4ab581 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38129
diff changeset
  2860
             (bol->in(1)->Opcode() == Op_CompareAndExchangeS ) ||
36316
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2861
             (bol->in(1)->Opcode() == Op_CompareAndExchangeI ) ||
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2862
             (bol->in(1)->Opcode() == Op_CompareAndExchangeL ) ||
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2863
             (bol->in(1)->Opcode() == Op_CompareAndExchangeP ) ||
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2864
             (bol->in(1)->Opcode() == Op_CompareAndExchangeN ) ||
39419
cc993a4ab581 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38129
diff changeset
  2865
             (bol->in(1)->Opcode() == Op_WeakCompareAndSwapB ) ||
cc993a4ab581 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38129
diff changeset
  2866
             (bol->in(1)->Opcode() == Op_WeakCompareAndSwapS ) ||
36316
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2867
             (bol->in(1)->Opcode() == Op_WeakCompareAndSwapI ) ||
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2868
             (bol->in(1)->Opcode() == Op_WeakCompareAndSwapL ) ||
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2869
             (bol->in(1)->Opcode() == Op_WeakCompareAndSwapP ) ||
7a83de7aabca 8148146: Integrate new internal Unsafe entry points, and basic intrinsic support for VarHandles
shade
parents: 36066
diff changeset
  2870
             (bol->in(1)->Opcode() == Op_WeakCompareAndSwapN ) ||
39419
cc993a4ab581 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38129
diff changeset
  2871
             (bol->in(1)->Opcode() == Op_CompareAndSwapB ) ||
cc993a4ab581 8157726: VarHandles/Unsafe should support sub-word atomic ops
shade
parents: 38129
diff changeset
  2872
             (bol->in(1)->Opcode() == Op_CompareAndSwapS ) ||
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2873
             (bol->in(1)->Opcode() == Op_CompareAndSwapI ) ||
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2874
             (bol->in(1)->Opcode() == Op_CompareAndSwapL ) ||
360
21d113ecbf6a 6420645: Create a vm that uses compressed oops for up to 32gb heapsizes
coleenp
parents: 212
diff changeset
  2875
             (bol->in(1)->Opcode() == Op_CompareAndSwapP ) ||
52925
9c18c9d839d3 8214259: Implementation: JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
rkennke
parents: 52608
diff changeset
  2876
             (bol->in(1)->Opcode() == Op_CompareAndSwapN ) ||
9c18c9d839d3 8214259: Implementation: JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
rkennke
parents: 52608
diff changeset
  2877
             (bol->in(1)->Opcode() == Op_ShenandoahCompareAndExchangeP ) ||
9c18c9d839d3 8214259: Implementation: JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
rkennke
parents: 52608
diff changeset
  2878
             (bol->in(1)->Opcode() == Op_ShenandoahCompareAndExchangeN ) ||
9c18c9d839d3 8214259: Implementation: JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
rkennke
parents: 52608
diff changeset
  2879
             (bol->in(1)->Opcode() == Op_ShenandoahWeakCompareAndSwapP ) ||
9c18c9d839d3 8214259: Implementation: JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
rkennke
parents: 52608
diff changeset
  2880
             (bol->in(1)->Opcode() == Op_ShenandoahWeakCompareAndSwapN ) ||
9c18c9d839d3 8214259: Implementation: JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
rkennke
parents: 52608
diff changeset
  2881
             (bol->in(1)->Opcode() == Op_ShenandoahCompareAndSwapP ) ||
9c18c9d839d3 8214259: Implementation: JEP 189: Shenandoah: A Low-Pause-Time Garbage Collector (Experimental)
rkennke
parents: 52608
diff changeset
  2882
             (bol->in(1)->Opcode() == Op_ShenandoahCompareAndSwapN )))
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2883
          return;               // Allocation loops RARELY take backedge
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2884
        // Find the OTHER exit path from the IF
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2885
        Node* ex = iff->proj_out(1-test_con);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2886
        float p = iff->_prob;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2887
        if( !phase->is_member( this, ex ) && iff->_fcnt == COUNT_UNKNOWN ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2888
          if( top == Op_IfTrue ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2889
            if( p < (PROB_FAIR + PROB_UNLIKELY_MAG(3))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2890
              iff->_prob = PROB_STATIC_FREQUENT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2891
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2892
          } else {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2893
            if( p > (PROB_FAIR - PROB_UNLIKELY_MAG(3))) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2894
              iff->_prob = PROB_STATIC_INFREQUENT;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2895
            }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2896
          }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2897
        }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2898
      }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2899
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2900
    test = phase->idom(test);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2901
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2902
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2903
31133
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2904
#ifdef ASSERT
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2905
static CountedLoopNode* locate_pre_from_main(CountedLoopNode *cl) {
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2906
  Node *ctrl  = cl->skip_predicates();
31133
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2907
  assert(ctrl->Opcode() == Op_IfTrue || ctrl->Opcode() == Op_IfFalse, "");
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2908
  Node *iffm = ctrl->in(0);
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2909
  assert(iffm->Opcode() == Op_If, "");
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2910
  Node *p_f = iffm->in(0);
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2911
  assert(p_f->Opcode() == Op_IfFalse, "");
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2912
  CountedLoopEndNode *pre_end = p_f->in(0)->as_CountedLoopEnd();
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2913
  assert(pre_end->loopnode()->is_pre_loop(), "");
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2914
  return pre_end->loopnode();
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2915
}
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2916
#endif
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2917
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2918
// Remove the main and post loops and make the pre loop execute all
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2919
// iterations. Useful when the pre loop is found empty.
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2920
void IdealLoopTree::remove_main_post_loops(CountedLoopNode *cl, PhaseIdealLoop *phase) {
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2921
  CountedLoopEndNode* pre_end = cl->loopexit();
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2922
  Node* pre_cmp = pre_end->cmp_node();
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2923
  if (pre_cmp->in(2)->Opcode() != Op_Opaque1) {
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2924
    // Only safe to remove the main loop if the compiler optimized it
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2925
    // out based on an unknown number of iterations
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2926
    return;
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2927
  }
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2928
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2929
  // Can we find the main loop?
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2930
  if (_next == NULL) {
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2931
    return;
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2932
  }
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2933
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2934
  Node* next_head = _next->_head;
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2935
  if (!next_head->is_CountedLoop()) {
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2936
    return;
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2937
  }
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2938
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2939
  CountedLoopNode* main_head = next_head->as_CountedLoop();
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2940
  if (!main_head->is_main_loop()) {
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2941
    return;
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2942
  }
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2943
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2944
  assert(locate_pre_from_main(main_head) == cl, "bad main loop");
49487
bde392011cd8 8193130: Bad graph when unrolled loop bounds conflicts with range checks
roland
parents: 48603
diff changeset
  2945
  Node* main_iff = main_head->skip_predicates()->in(0);
31133
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2946
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2947
  // Remove the Opaque1Node of the pre loop and make it execute all iterations
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2948
  phase->_igvn.replace_input_of(pre_cmp, 2, pre_cmp->in(2)->in(2));
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2949
  // Remove the Opaque1Node of the main loop so it can be optimized out
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2950
  Node* main_cmp = main_iff->in(1)->in(1);
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2951
  assert(main_cmp->in(2)->Opcode() == Op_Opaque1, "main loop has no opaque node?");
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2952
  phase->_igvn.replace_input_of(main_cmp, 2, main_cmp->in(2)->in(1));
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2953
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2954
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2955
//------------------------------policy_do_remove_empty_loop--------------------
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2956
// Micro-benchmark spamming.  Policy is to always remove empty loops.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2957
// The 'DO' part is to replace the trip counter with the value it will
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2958
// have on the last iteration.  This will break the loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2959
bool IdealLoopTree::policy_do_remove_empty_loop( PhaseIdealLoop *phase ) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2960
  // Minimum size must be empty loop
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
  2961
  if (_body.size() > EMPTY_LOOP_SIZE)
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2962
    return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2963
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2964
  if (!_head->is_CountedLoop())
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2965
    return false;     // Dead loop
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2966
  CountedLoopNode *cl = _head->as_CountedLoop();
10263
fa58671dde31 7077439: Possible reference through NULL in loopPredicate.cpp:726
kvn
parents: 10258
diff changeset
  2967
  if (!cl->is_valid_counted_loop())
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2968
    return false; // Malformed loop
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  2969
  if (!phase->is_member(this, phase->get_ctrl(cl->loopexit()->in(CountedLoopEndNode::TestValue))))
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2970
    return false;             // Infinite loop
8870
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  2971
31133
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2972
  if (cl->is_pre_loop()) {
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2973
    // If the loop we are removing is a pre-loop then the main and
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2974
    // post loop can be removed as well
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2975
    remove_main_post_loops(cl, phase);
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2976
  }
3e7542b42a61 8085832: Optimize main and post loop out when pre loop is found empty
roland
parents: 31131
diff changeset
  2977
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2978
#ifdef ASSERT
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2979
  // Ensure only one phi which is the iv.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2980
  Node* iv = NULL;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2981
  for (DUIterator_Fast imax, i = cl->fast_outs(imax); i < imax; i++) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2982
    Node* n = cl->fast_out(i);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2983
    if (n->Opcode() == Op_Phi) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2984
      assert(iv == NULL, "Too many phis" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2985
      iv = n;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2986
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2987
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2988
  assert(iv == cl->phi(), "Wrong phi" );
489c9b5090e2 Initial load
duke
parents:
diff changeset
  2989
#endif
8870
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  2990
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  2991
  // main and post loops have explicitly created zero trip guard
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  2992
  bool needs_guard = !cl->is_main_loop() && !cl->is_post_loop();
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  2993
  if (needs_guard) {
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  2994
    // Skip guard if values not overlap.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  2995
    const TypeInt* init_t = phase->_igvn.type(cl->init_trip())->is_int();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  2996
    const TypeInt* limit_t = phase->_igvn.type(cl->limit())->is_int();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  2997
    int  stride_con = cl->stride_con();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  2998
    if (stride_con > 0) {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  2999
      needs_guard = (init_t->_hi >= limit_t->_lo);
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3000
    } else {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3001
      needs_guard = (init_t->_lo <= limit_t->_hi);
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3002
    }
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3003
  }
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3004
  if (needs_guard) {
8870
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3005
    // Check for an obvious zero trip guard.
50623
5209d8a6303e 8203197: C2: consider all paths in loop body for loop predication
roland
parents: 50561
diff changeset
  3006
    Node* inctrl = PhaseIdealLoop::skip_all_loop_predicates(cl->skip_predicates());
35838
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3007
    if (inctrl->Opcode() == Op_IfTrue || inctrl->Opcode() == Op_IfFalse) {
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3008
      bool maybe_swapped = (inctrl->Opcode() == Op_IfFalse);
8870
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3009
      // The test should look like just the backedge of a CountedLoop
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3010
      Node* iff = inctrl->in(0);
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3011
      if (iff->is_If()) {
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3012
        Node* bol = iff->in(1);
35838
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3013
        if (bol->is_Bool()) {
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3014
          BoolTest test = bol->as_Bool()->_test;
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3015
          if (maybe_swapped) {
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3016
            test._test = test.commute();
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3017
            test._test = test.negate();
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3018
          }
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3019
          if (test._test == cl->loopexit()->test_trip()) {
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3020
            Node* cmp = bol->in(1);
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3021
            int init_idx = maybe_swapped ? 2 : 1;
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3022
            int limit_idx = maybe_swapped ? 1 : 2;
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3023
            if (cmp->is_Cmp() && cmp->in(init_idx) == cl->init_trip() && cmp->in(limit_idx) == cl->limit()) {
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3024
              needs_guard = false;
a6e58eeeaa2e 8137049: Code quality: reducing an trivial integer loop does not produce an optimal code
roland
parents: 35759
diff changeset
  3025
            }
8870
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3026
          }
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3027
        }
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3028
      }
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3029
    }
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3030
  }
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3031
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3032
#ifndef PRODUCT
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3033
  if (PrintOpto) {
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3034
    tty->print("Removing empty loop with%s zero trip guard", needs_guard ? "out" : "");
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3035
    this->dump_head();
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3036
  } else if (TraceLoopOpts) {
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3037
    tty->print("Empty with%s zero trip guard   ", needs_guard ? "out" : "");
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3038
    this->dump_head();
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3039
  }
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3040
#endif
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3041
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3042
  if (needs_guard) {
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3043
    // Peel the loop to ensure there's a zero trip guard
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3044
    Node_List old_new;
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3045
    phase->do_peeling(this, old_new);
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3046
  }
119881dc9d0b 7024475: loop doesn't terminate when compiled
never
parents: 8732
diff changeset
  3047
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3048
  // Replace the phi at loop head with the final value of the last
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3049
  // iteration.  Then the CountedLoopEnd will collapse (backedge never
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3050
  // taken) and all loop-invariant uses of the exit values will be correct.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3051
  Node *phi = cl->phi();
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3052
  Node *exact_limit = phase->exact_limit(this);
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3053
  if (exact_limit != cl->limit()) {
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3054
    // We also need to replace the original limit to collapse loop exit.
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3055
    Node* cmp = cl->loopexit()->cmp_node();
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3056
    assert(cl->limit() == cmp->in(2), "sanity");
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3057
    phase->_igvn._worklist.push(cmp->in(2)); // put limit on worklist
12958
009b6c9586d8 7173340: C2: code cleanup: use PhaseIterGVN::replace_edge(Node*, int, Node*) where applicable
kvn
parents: 10538
diff changeset
  3058
    phase->_igvn.replace_input_of(cmp, 2, exact_limit); // put cmp on worklist
9446
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3059
  }
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3060
  // Note: the final value after increment should not overflow since
748a37b25d10 5091921: Sign flip issues in loop optimizer
kvn
parents: 9434
diff changeset
  3061
  // counted loop has limit check predicate.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3062
  Node *final = new SubINode( exact_limit, cl->stride() );
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3063
  phase->register_new_node(final,cl->in(LoopNode::EntryControl));
5901
c046f8e9c52b 6677629: PhaseIterGVN::subsume_node() should call hash_delete() and add_users_to_worklist()
kvn
parents: 5547
diff changeset
  3064
  phase->_igvn.replace_node(phi,final);
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3065
  phase->C->set_major_progress();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3066
  return true;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3067
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3068
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3069
//------------------------------policy_do_one_iteration_loop-------------------
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3070
// Convert one iteration loop into normal code.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3071
bool IdealLoopTree::policy_do_one_iteration_loop( PhaseIdealLoop *phase ) {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3072
  if (!_head->as_Loop()->is_valid_counted_loop())
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3073
    return false; // Only for counted loop
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3074
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3075
  CountedLoopNode *cl = _head->as_CountedLoop();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3076
  if (!cl->has_exact_trip_count() || cl->trip_count() != 1) {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3077
    return false;
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3078
  }
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3079
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3080
#ifndef PRODUCT
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3081
  if(TraceLoopOpts) {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3082
    tty->print("OneIteration ");
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3083
    this->dump_head();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3084
  }
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3085
#endif
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3086
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3087
  Node *init_n = cl->init_trip();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3088
#ifdef ASSERT
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3089
  // Loop boundaries should be constant since trip count is exact.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3090
  assert(init_n->get_int() + cl->stride_con() >= cl->limit()->get_int(), "should be one iteration");
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3091
#endif
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3092
  // Replace the phi at loop head with the value of the init_trip.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3093
  // Then the CountedLoopEnd will collapse (backedge will not be taken)
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3094
  // and all loop-invariant uses of the exit values will be correct.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3095
  phase->_igvn.replace_node(cl->phi(), cl->init_trip());
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3096
  phase->C->set_major_progress();
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3097
  return true;
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3098
}
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3099
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3100
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3101
//------------------------------iteration_split_impl---------------------------
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3102
bool IdealLoopTree::iteration_split_impl( PhaseIdealLoop *phase, Node_List &old_new ) {
40031
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
  3103
  // Compute loop trip count if possible.
6cc034201dba 8159016: Over-unrolled loop is partially removed
thartmann
parents: 39429
diff changeset
  3104
  compute_trip_count(phase);
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3105
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3106
  // Convert one iteration loop into normal code.
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3107
  if (policy_do_one_iteration_loop(phase))
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3108
    return true;
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3109
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3110
  // Check and remove empty loops (spam micro-benchmarks)
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3111
  if (policy_do_remove_empty_loop(phase))
4643
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
  3112
    return true;  // Here we removed an empty loop
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3113
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3114
  bool should_peel = policy_peeling(phase); // Should we peel?
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3115
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3116
  bool should_unswitch = policy_unswitching(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3117
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3118
  // Non-counted loops may be peeled; exactly 1 iteration is peeled.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3119
  // This removes loop-invariant tests (usually null checks).
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3120
  if (!_head->is_CountedLoop()) { // Non-counted loop
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3121
    if (PartialPeelLoop && phase->partial_peel(this, old_new)) {
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3122
      // Partial peel succeeded so terminate this round of loop opts
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3123
      return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3124
    }
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3125
    if (should_peel) {            // Should we peel?
34174
4db2fb26dc49 8140424: don't prefix developer and notproduct flag variables with CONST_ in product builds
twisti
parents: 34164
diff changeset
  3126
      if (PrintOpto) { tty->print_cr("should_peel"); }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3127
      phase->do_peeling(this,old_new);
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3128
    } else if (should_unswitch) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3129
      phase->do_unswitching(this, old_new);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3130
    }
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3131
    return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3132
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3133
  CountedLoopNode *cl = _head->as_CountedLoop();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3134
10263
fa58671dde31 7077439: Possible reference through NULL in loopPredicate.cpp:726
kvn
parents: 10258
diff changeset
  3135
  if (!cl->is_valid_counted_loop()) return true; // Ignore various kinds of broken loops
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3136
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3137
  // Do nothing special to pre- and post- loops
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3138
  if (cl->is_pre_loop() || cl->is_post_loop()) return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3139
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3140
  // Compute loop trip count from profile data
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3141
  compute_profile_trip_cnt(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3142
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3143
  // Before attempting fancy unrolling, RCE or alignment, see if we want
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3144
  // to completely unroll this loop or do loop unswitching.
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3145
  if (cl->is_normal_loop()) {
2876
b8ebc85d0b47 6837146: Should perform unswitch before maximally unroll in loop transformation
cfang
parents: 2131
diff changeset
  3146
    if (should_unswitch) {
b8ebc85d0b47 6837146: Should perform unswitch before maximally unroll in loop transformation
cfang
parents: 2131
diff changeset
  3147
      phase->do_unswitching(this, old_new);
b8ebc85d0b47 6837146: Should perform unswitch before maximally unroll in loop transformation
cfang
parents: 2131
diff changeset
  3148
      return true;
b8ebc85d0b47 6837146: Should perform unswitch before maximally unroll in loop transformation
cfang
parents: 2131
diff changeset
  3149
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3150
    bool should_maximally_unroll =  policy_maximally_unroll(phase);
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3151
    if (should_maximally_unroll) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3152
      // Here we did some unrolling and peeling.  Eventually we will
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3153
      // completely unroll this loop and it will no longer be a loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3154
      phase->do_maximally_unroll(this,old_new);
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3155
      return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3156
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3157
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3158
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
  3159
  // Skip next optimizations if running low on nodes. Note that
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
  3160
  // policy_unswitching and policy_maximally_unroll have this check.
27707
f7d26e5b8b5d 8058148: MaxNodeLimit and LiveNodeCountInliningCutoff
vlivanov
parents: 25927
diff changeset
  3161
  int nodes_left = phase->C->max_node_limit() - phase->C->live_nodes();
f7d26e5b8b5d 8058148: MaxNodeLimit and LiveNodeCountInliningCutoff
vlivanov
parents: 25927
diff changeset
  3162
  if ((int)(2 * _body.size()) > nodes_left) {
9109
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
  3163
    return true;
ec7b2f3b386c 7004547: regular loop unroll should not unroll more than max unrolling
kvn
parents: 9104
diff changeset
  3164
  }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3165
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3166
  // Counted loops may be peeled, may need some iterations run up
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3167
  // front for RCE, and may want to align loop refs to a cache
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3168
  // line.  Thus we clone a full loop up front whose trip count is
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3169
  // at least 1 (if peeling), but may be several more.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3170
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3171
  // The main loop will start cache-line aligned with at least 1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3172
  // iteration of the unrolled body (zero-trip test required) and
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3173
  // will have some range checks removed.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3174
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3175
  // A post-loop will finish any odd iterations (leftover after
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3176
  // unrolling), plus any needed for RCE purposes.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3177
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3178
  bool should_unroll = policy_unroll(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3179
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3180
  bool should_rce = policy_range_check(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3181
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3182
  bool should_align = policy_align(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3183
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3184
  // If not RCE'ing (iteration splitting) or Aligning, then we do not
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3185
  // need a pre-loop.  We may still need to peel an initial iteration but
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3186
  // we will not be needing an unknown number of pre-iterations.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3187
  //
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3188
  // Basically, if may_rce_align reports FALSE first time through,
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3189
  // we will not be able to later do RCE or Aligning on this loop.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3190
  bool may_rce_align = !policy_peel_only(phase) || should_rce || should_align;
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3191
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3192
  // If we have any of these conditions (RCE, alignment, unrolling) met, then
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3193
  // we switch to the pre-/main-/post-loop model.  This model also covers
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3194
  // peeling.
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3195
  if (should_rce || should_align || should_unroll) {
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3196
    if (cl->is_normal_loop())  // Convert to 'pre/main/post' loops
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3197
      phase->insert_pre_post_loops(this,old_new, !may_rce_align);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3198
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3199
    // Adjust the pre- and main-loop limits to let the pre and post loops run
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3200
    // with full checks, but the main-loop with no checks.  Remove said
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3201
    // checks from the main body.
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3202
    if (should_rce) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3203
      if (phase->do_range_check(this, old_new) != 0) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3204
        cl->mark_has_range_checks();
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3205
      }
38129
7adeefaa6d1d 8154763: Crash with "assert(RangeCheckElimination)" if RangeCheckElimination is disabled
thartmann
parents: 37292
diff changeset
  3206
    } else if (PostLoopMultiversioning) {
37292
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3207
      phase->has_range_checks(this);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3208
    }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3209
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3210
    if (should_unroll && !should_peel && PostLoopMultiversioning) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3211
      // Try to setup multiversioning on main loops before they are unrolled
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3212
      if (cl->is_main_loop() && (cl->unrolled_count() == 1)) {
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3213
        phase->insert_scalar_rced_post_loop(this, old_new);
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3214
      }
64f6ae06310e 8151573: Multiversioning for range check elimination
mcberg
parents: 37287
diff changeset
  3215
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3216
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3217
    // Double loop body for unrolling.  Adjust the minimum-trip test (will do
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3218
    // twice as many iterations as before) and the main body limit (only do
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3219
    // an even number of trips).  If we are peeling, we might enable some RCE
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3220
    // and we'd rather unroll the post-RCE'd loop SO... do not unroll if
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3221
    // peeling.
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  3222
    if (should_unroll && !should_peel) {
36066
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  3223
      if (SuperWordLoopUnrollAnalysis) {
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  3224
        phase->insert_vector_post_loop(this, old_new);
60ce66ce3c76 8149421: Vectorized Post Loops
mcberg
parents: 35838
diff changeset
  3225
      }
30211
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  3226
      phase->do_unroll(this, old_new, true);
442fbbb31f75 8074981: Integer/FP scalar reduction optimization
kvn
parents: 29344
diff changeset
  3227
    }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3228
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3229
    // Adjust the pre-loop limits to align the main body
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3230
    // iterations.
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3231
    if (should_align)
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3232
      Unimplemented();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3233
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3234
  } else {                      // Else we have an unchanged counted loop
9121
704ece791737 7004555: Add new policy for one iteration loops
kvn
parents: 9109
diff changeset
  3235
    if (should_peel)           // Might want to peel but do nothing else
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3236
      phase->do_peeling(this,old_new);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3237
  }
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3238
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3239
}
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3240
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3241
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3242
//=============================================================================
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3243
//------------------------------iteration_split--------------------------------
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3244
bool IdealLoopTree::iteration_split( PhaseIdealLoop *phase, Node_List &old_new ) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3245
  // Recursively iteration split nested loops
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3246
  if (_child && !_child->iteration_split(phase, old_new))
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3247
    return false;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3248
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3249
  // Clean out prior deadwood
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3250
  DCE_loop_body();
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3251
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3252
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3253
  // Look for loop-exit tests with my 50/50 guesses from the Parsing stage.
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3254
  // Replace with a 1-in-10 exit guess.
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3255
  if (_parent /*not the root loop*/ &&
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3256
      !_irreducible &&
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3257
      // Also ignore the occasional dead backedge
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3258
      !tail()->is_top()) {
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3259
    adjust_loop_exit_prob(phase);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3260
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3261
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3262
  // Gate unrolling, RCE and peeling efforts.
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3263
  if (!_child &&                // If not an inner loop, do not split
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3264
      !_irreducible &&
212
cd4963e67949 6667612: (Escape Analysis) disable loop cloning if it has a scalar replaceable allocation
kvn
parents: 1
diff changeset
  3265
      _allow_optimizations &&
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3266
      !tail()->is_top()) {     // Also ignore the occasional dead backedge
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3267
    if (!_has_call) {
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3268
        if (!iteration_split_impl(phase, old_new)) {
4643
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
  3269
          return false;
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
  3270
        }
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3271
    } else if (policy_unswitching(phase)) {
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3272
      phase->do_unswitching(this, old_new);
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3273
    }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3274
  }
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3275
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3276
  // Minor offset re-organization to remove loop-fallout uses of
8732
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3277
  // trip counter when there was no major reshaping.
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3278
  phase->reorg_offsets(this);
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3279
16fc1c68714b 7008866: Missing loop predicate for loop with multiple entries
kvn
parents: 7397
diff changeset
  3280
  if (_next && !_next->iteration_split(phase, old_new))
1433
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3281
    return false;
ee60bd139c07 6743188: incomplete fix for 6700047 C2 failed in idom_no_update
never
parents: 1432
diff changeset
  3282
  return true;
1
489c9b5090e2 Initial load
duke
parents:
diff changeset
  3283
}
4643
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
  3284
61c659c91c57 6894779: Loop Predication for Loop Optimizer in C2
cfang
parents: 3261
diff changeset
  3285
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3286
//=============================================================================
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3287
// Process all the loops in the loop tree and replace any fill
35574
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3288
// patterns with an intrinsic version.
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3289
bool PhaseIdealLoop::do_intrinsify_fill() {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3290
  bool changed = false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3291
  for (LoopTreeIterator iter(_ltree_root); !iter.done(); iter.next()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3292
    IdealLoopTree* lpt = iter.current();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3293
    changed |= intrinsify_fill(lpt);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3294
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3295
  return changed;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3296
}
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3297
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3298
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3299
// Examine an inner loop looking for a a single store of an invariant
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3300
// value in a unit stride loop,
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3301
bool PhaseIdealLoop::match_fill_loop(IdealLoopTree* lpt, Node*& store, Node*& store_value,
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3302
                                     Node*& shift, Node*& con) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3303
  const char* msg = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3304
  Node* msg_node = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3305
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3306
  store_value = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3307
  con = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3308
  shift = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3309
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3310
  // Process the loop looking for stores.  If there are multiple
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3311
  // stores or extra control flow give at this point.
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3312
  CountedLoopNode* head = lpt->_head->as_CountedLoop();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3313
  for (uint i = 0; msg == NULL && i < lpt->_body.size(); i++) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3314
    Node* n = lpt->_body.at(i);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3315
    if (n->outcnt() == 0) continue; // Ignore dead
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3316
    if (n->is_Store()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3317
      if (store != NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3318
        msg = "multiple stores";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3319
        break;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3320
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3321
      int opc = n->Opcode();
13969
d2a189b83b87 7054512: Compress class pointers after perm gen removal
roland
parents: 13967
diff changeset
  3322
      if (opc == Op_StoreP || opc == Op_StoreN || opc == Op_StoreNKlass || opc == Op_StoreCM) {
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3323
        msg = "oop fills not handled";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3324
        break;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3325
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3326
      Node* value = n->in(MemNode::ValueIn);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3327
      if (!lpt->is_invariant(value)) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3328
        msg  = "variant store value";
6455
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3329
      } else if (!_igvn.type(n->in(MemNode::Address))->isa_aryptr()) {
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3330
        msg = "not array address";
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3331
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3332
      store = n;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3333
      store_value = value;
48603
e5da6c246176 8194992: Null pointer dereference in MultiNode::proj_out related to loopexit()
dlong
parents: 48595
diff changeset
  3334
    } else if (n->is_If() && n != head->loopexit_or_null()) {
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3335
      msg = "extra control flow";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3336
      msg_node = n;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3337
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3338
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3339
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3340
  if (store == NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3341
    // No store in loop
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3342
    return false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3343
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3344
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3345
  if (msg == NULL && head->stride_con() != 1) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3346
    // could handle negative strides too
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3347
    if (head->stride_con() < 0) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3348
      msg = "negative stride";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3349
    } else {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3350
      msg = "non-unit stride";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3351
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3352
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3353
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3354
  if (msg == NULL && !store->in(MemNode::Address)->is_AddP()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3355
    msg = "can't handle store address";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3356
    msg_node = store->in(MemNode::Address);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3357
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3358
6739
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3359
  if (msg == NULL &&
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3360
      (!store->in(MemNode::Memory)->is_Phi() ||
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3361
       store->in(MemNode::Memory)->in(LoopNode::LoopBackControl) != store)) {
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3362
    msg = "store memory isn't proper phi";
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3363
    msg_node = store->in(MemNode::Memory);
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3364
  }
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3365
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3366
  // Make sure there is an appropriate fill routine
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3367
  BasicType t = store->as_Mem()->memory_type();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3368
  const char* fill_name;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3369
  if (msg == NULL &&
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3370
      StubRoutines::select_fill_function(t, false, fill_name) == NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3371
    msg = "unsupported store";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3372
    msg_node = store;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3373
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3374
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3375
  if (msg != NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3376
#ifndef PRODUCT
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3377
    if (TraceOptimizeFill) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3378
      tty->print_cr("not fill intrinsic candidate: %s", msg);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3379
      if (msg_node != NULL) msg_node->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3380
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3381
#endif
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3382
    return false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3383
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3384
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3385
  // Make sure the address expression can be handled.  It should be
35574
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3386
  // head->phi * elsize + con.  head->phi might have a ConvI2L(CastII()).
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3387
  Node* elements[4];
35574
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3388
  Node* cast = NULL;
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3389
  Node* conv = NULL;
6455
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3390
  bool found_index = false;
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3391
  int count = store->in(MemNode::Address)->as_AddP()->unpack_offsets(elements, ARRAY_SIZE(elements));
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3392
  for (int e = 0; e < count; e++) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3393
    Node* n = elements[e];
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3394
    if (n->is_Con() && con == NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3395
      con = n;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3396
    } else if (n->Opcode() == Op_LShiftX && shift == NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3397
      Node* value = n->in(1);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3398
#ifdef _LP64
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3399
      if (value->Opcode() == Op_ConvI2L) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3400
        conv = value;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3401
        value = value->in(1);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3402
      }
35574
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3403
      if (value->Opcode() == Op_CastII &&
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3404
          value->as_CastII()->has_range_check()) {
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3405
        // Skip range check dependent CastII nodes
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3406
        cast = value;
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3407
        value = value->in(1);
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3408
      }
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3409
#endif
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3410
      if (value != head->phi()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3411
        msg = "unhandled shift in address";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3412
      } else {
9104
85edf8ec98b2 7026957: assert(type2aelembytes(store->as_Mem()->memory_type(), true) == 1 << shift->in(2)->get_int()) failed
never
parents: 9101
diff changeset
  3413
        if (type2aelembytes(store->as_Mem()->memory_type(), true) != (1 << n->in(2)->get_int())) {
85edf8ec98b2 7026957: assert(type2aelembytes(store->as_Mem()->memory_type(), true) == 1 << shift->in(2)->get_int()) failed
never
parents: 9101
diff changeset
  3414
          msg = "scale doesn't match";
85edf8ec98b2 7026957: assert(type2aelembytes(store->as_Mem()->memory_type(), true) == 1 << shift->in(2)->get_int()) failed
never
parents: 9101
diff changeset
  3415
        } else {
85edf8ec98b2 7026957: assert(type2aelembytes(store->as_Mem()->memory_type(), true) == 1 << shift->in(2)->get_int()) failed
never
parents: 9101
diff changeset
  3416
          found_index = true;
85edf8ec98b2 7026957: assert(type2aelembytes(store->as_Mem()->memory_type(), true) == 1 << shift->in(2)->get_int()) failed
never
parents: 9101
diff changeset
  3417
          shift = n;
85edf8ec98b2 7026957: assert(type2aelembytes(store->as_Mem()->memory_type(), true) == 1 << shift->in(2)->get_int()) failed
never
parents: 9101
diff changeset
  3418
        }
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3419
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3420
    } else if (n->Opcode() == Op_ConvI2L && conv == NULL) {
35574
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3421
      conv = n;
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3422
      n = n->in(1);
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3423
      if (n->Opcode() == Op_CastII &&
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3424
          n->as_CastII()->has_range_check()) {
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3425
        // Skip range check dependent CastII nodes
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3426
        cast = n;
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3427
        n = n->in(1);
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3428
      }
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3429
      if (n == head->phi()) {
6455
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3430
        found_index = true;
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3431
      } else {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3432
        msg = "unhandled input to ConvI2L";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3433
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3434
    } else if (n == head->phi()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3435
      // no shift, check below for allowed cases
6455
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3436
      found_index = true;
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3437
    } else {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3438
      msg = "unhandled node in address";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3439
      msg_node = n;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3440
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3441
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3442
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3443
  if (count == -1) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3444
    msg = "malformed address expression";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3445
    msg_node = store;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3446
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3447
6455
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3448
  if (!found_index) {
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3449
    msg = "missing use of index";
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3450
  }
4a8142e5f75f 6982533: Crash in ~StubRoutines::jbyte_fill with AggressiveOpts enabled
never
parents: 6433
diff changeset
  3451
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3452
  // byte sized items won't have a shift
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3453
  if (msg == NULL && shift == NULL && t != T_BYTE && t != T_BOOLEAN) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3454
    msg = "can't find shift";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3455
    msg_node = store;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3456
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3457
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3458
  if (msg != NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3459
#ifndef PRODUCT
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3460
    if (TraceOptimizeFill) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3461
      tty->print_cr("not fill intrinsic: %s", msg);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3462
      if (msg_node != NULL) msg_node->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3463
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3464
#endif
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3465
    return false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3466
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3467
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3468
  // No make sure all the other nodes in the loop can be handled
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3469
  VectorSet ok(Thread::current()->resource_area());
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3470
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3471
  // store related values are ok
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3472
  ok.set(store->_idx);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3473
  ok.set(store->in(MemNode::Memory)->_idx);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3474
16380
d1e1ca068275 8009181: [parfait] Null pointer deference in hotspot/src/share/vm/opto/loopTransform.cpp
morris
parents: 15919
diff changeset
  3475
  CountedLoopEndNode* loop_exit = head->loopexit();
d1e1ca068275 8009181: [parfait] Null pointer deference in hotspot/src/share/vm/opto/loopTransform.cpp
morris
parents: 15919
diff changeset
  3476
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3477
  // Loop structure is ok
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3478
  ok.set(head->_idx);
16380
d1e1ca068275 8009181: [parfait] Null pointer deference in hotspot/src/share/vm/opto/loopTransform.cpp
morris
parents: 15919
diff changeset
  3479
  ok.set(loop_exit->_idx);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3480
  ok.set(head->phi()->_idx);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3481
  ok.set(head->incr()->_idx);
16380
d1e1ca068275 8009181: [parfait] Null pointer deference in hotspot/src/share/vm/opto/loopTransform.cpp
morris
parents: 15919
diff changeset
  3482
  ok.set(loop_exit->cmp_node()->_idx);
d1e1ca068275 8009181: [parfait] Null pointer deference in hotspot/src/share/vm/opto/loopTransform.cpp
morris
parents: 15919
diff changeset
  3483
  ok.set(loop_exit->in(1)->_idx);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3484
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3485
  // Address elements are ok
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3486
  if (con)   ok.set(con->_idx);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3487
  if (shift) ok.set(shift->_idx);
35574
2b25eb88c8d6 6675699: need comprehensive fix for unconstrained ConvI2L with narrowed type
thartmann
parents: 35155
diff changeset
  3488
  if (cast)  ok.set(cast->_idx);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3489
  if (conv)  ok.set(conv->_idx);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3490
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3491
  for (uint i = 0; msg == NULL && i < lpt->_body.size(); i++) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3492
    Node* n = lpt->_body.at(i);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3493
    if (n->outcnt() == 0) continue; // Ignore dead
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3494
    if (ok.test(n->_idx)) continue;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3495
    // Backedge projection is ok
16380
d1e1ca068275 8009181: [parfait] Null pointer deference in hotspot/src/share/vm/opto/loopTransform.cpp
morris
parents: 15919
diff changeset
  3496
    if (n->is_IfTrue() && n->in(0) == loop_exit) continue;
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3497
    if (!n->is_AddP()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3498
      msg = "unhandled node";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3499
      msg_node = n;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3500
      break;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3501
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3502
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3503
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3504
  // Make sure no unexpected values are used outside the loop
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3505
  for (uint i = 0; msg == NULL && i < lpt->_body.size(); i++) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3506
    Node* n = lpt->_body.at(i);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3507
    // These values can be replaced with other nodes if they are used
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3508
    // outside the loop.
16380
d1e1ca068275 8009181: [parfait] Null pointer deference in hotspot/src/share/vm/opto/loopTransform.cpp
morris
parents: 15919
diff changeset
  3509
    if (n == store || n == loop_exit || n == head->incr() || n == store->in(MemNode::Memory)) continue;
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3510
    for (SimpleDUIterator iter(n); iter.has_next(); iter.next()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3511
      Node* use = iter.get();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3512
      if (!lpt->_body.contains(use)) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3513
        msg = "node is used outside loop";
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3514
        // lpt->_body.dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3515
        msg_node = n;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3516
        break;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3517
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3518
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3519
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3520
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3521
#ifdef ASSERT
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3522
  if (TraceOptimizeFill) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3523
    if (msg != NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3524
      tty->print_cr("no fill intrinsic: %s", msg);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3525
      if (msg_node != NULL) msg_node->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3526
    } else {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3527
      tty->print_cr("fill intrinsic for:");
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3528
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3529
    store->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3530
    if (Verbose) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3531
      lpt->_body.dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3532
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3533
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3534
#endif
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3535
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3536
  return msg == NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3537
}
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3538
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3539
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3540
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3541
bool PhaseIdealLoop::intrinsify_fill(IdealLoopTree* lpt) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3542
  // Only for counted inner loops
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3543
  if (!lpt->is_counted() || !lpt->is_inner()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3544
    return false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3545
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3546
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3547
  // Must have constant stride
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3548
  CountedLoopNode* head = lpt->_head->as_CountedLoop();
10263
fa58671dde31 7077439: Possible reference through NULL in loopPredicate.cpp:726
kvn
parents: 10258
diff changeset
  3549
  if (!head->is_valid_counted_loop() || !head->is_normal_loop()) {
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3550
    return false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3551
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3552
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3553
  head->verify_strip_mined(1);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3554
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3555
  // Check that the body only contains a store of a loop invariant
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3556
  // value that is indexed by the loop phi.
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3557
  Node* store = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3558
  Node* store_value = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3559
  Node* shift = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3560
  Node* offset = NULL;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3561
  if (!match_fill_loop(lpt, store, store_value, shift, offset)) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3562
    return false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3563
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3564
48595
5d699d81c10c 8194988: 8 Null pointer dereference defect groups related to MultiNode::proj_out()
dlong
parents: 48145
diff changeset
  3565
  Node* exit = head->loopexit()->proj_out_or_null(0);
44314
30ae899b9eca 8175345: Reported null pointer dereference defect groups
rraghavan
parents: 42622
diff changeset
  3566
  if (exit == NULL) {
30ae899b9eca 8175345: Reported null pointer dereference defect groups
rraghavan
parents: 42622
diff changeset
  3567
    return false;
30ae899b9eca 8175345: Reported null pointer dereference defect groups
rraghavan
parents: 42622
diff changeset
  3568
  }
30ae899b9eca 8175345: Reported null pointer dereference defect groups
rraghavan
parents: 42622
diff changeset
  3569
9101
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3570
#ifndef PRODUCT
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3571
  if (TraceLoopOpts) {
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3572
    tty->print("ArrayFill    ");
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3573
    lpt->dump_head();
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3574
  }
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3575
#endif
ff58f9a8e31c 7004535: Clone loop predicate during loop unswitch
kvn
parents: 8884
diff changeset
  3576
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3577
  // Now replace the whole loop body by a call to a fill routine that
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3578
  // covers the same region as the loop.
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3579
  Node* base = store->in(MemNode::Address)->as_AddP()->in(AddPNode::Base);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3580
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3581
  // Build an expression for the beginning of the copy region
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3582
  Node* index = head->init_trip();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3583
#ifdef _LP64
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3584
  index = new ConvI2LNode(index);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3585
  _igvn.register_new_node_with_optimizer(index);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3586
#endif
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3587
  if (shift != NULL) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3588
    // byte arrays don't require a shift but others do.
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3589
    index = new LShiftXNode(index, shift->in(2));
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3590
    _igvn.register_new_node_with_optimizer(index);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3591
  }
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3592
  index = new AddPNode(base, base, index);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3593
  _igvn.register_new_node_with_optimizer(index);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3594
  Node* from = new AddPNode(base, index, offset);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3595
  _igvn.register_new_node_with_optimizer(from);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3596
  // Compute the number of elements to copy
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3597
  Node* len = new SubINode(head->limit(), head->init_trip());
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3598
  _igvn.register_new_node_with_optimizer(len);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3599
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3600
  BasicType t = store->as_Mem()->memory_type();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3601
  bool aligned = false;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3602
  if (offset != NULL && head->init_trip()->is_Con()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3603
    int element_size = type2aelembytes(t);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3604
    aligned = (offset->find_intptr_t_type()->get_con() + head->init_trip()->get_int() * element_size) % HeapWordSize == 0;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3605
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3606
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3607
  // Build a call to the fill routine
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3608
  const char* fill_name;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3609
  address fill = StubRoutines::select_fill_function(t, aligned, fill_name);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3610
  assert(fill != NULL, "what?");
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3611
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3612
  // Convert float/double to int/long for fill routines
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3613
  if (t == T_FLOAT) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3614
    store_value = new MoveF2INode(store_value);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3615
    _igvn.register_new_node_with_optimizer(store_value);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3616
  } else if (t == T_DOUBLE) {
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3617
    store_value = new MoveD2LNode(store_value);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3618
    _igvn.register_new_node_with_optimizer(store_value);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3619
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3620
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3621
  Node* mem_phi = store->in(MemNode::Memory);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3622
  Node* result_ctrl;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3623
  Node* result_mem;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3624
  const TypeFunc* call_type = OptoRuntime::array_fill_Type();
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3625
  CallLeafNode *call = new CallLeafNoFPNode(call_type, fill,
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3626
                                            fill_name, TypeAryPtr::get_array_body_type(t));
22834
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3627
  uint cnt = 0;
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3628
  call->init_req(TypeFunc::Parms + cnt++, from);
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3629
  call->init_req(TypeFunc::Parms + cnt++, store_value);
6770
a67870aaedb3 6980792: Crash "exception happened outside interpreter, nmethods and vtable stubs (1)"
never
parents: 6739
diff changeset
  3630
#ifdef _LP64
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3631
  len = new ConvI2LNode(len);
6770
a67870aaedb3 6980792: Crash "exception happened outside interpreter, nmethods and vtable stubs (1)"
never
parents: 6739
diff changeset
  3632
  _igvn.register_new_node_with_optimizer(len);
a67870aaedb3 6980792: Crash "exception happened outside interpreter, nmethods and vtable stubs (1)"
never
parents: 6739
diff changeset
  3633
#endif
22834
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3634
  call->init_req(TypeFunc::Parms + cnt++, len);
6770
a67870aaedb3 6980792: Crash "exception happened outside interpreter, nmethods and vtable stubs (1)"
never
parents: 6739
diff changeset
  3635
#ifdef _LP64
22834
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3636
  call->init_req(TypeFunc::Parms + cnt++, C->top());
6770
a67870aaedb3 6980792: Crash "exception happened outside interpreter, nmethods and vtable stubs (1)"
never
parents: 6739
diff changeset
  3637
#endif
22834
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3638
  call->init_req(TypeFunc::Control,   head->init_control());
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3639
  call->init_req(TypeFunc::I_O,       C->top());       // Does no I/O.
3e2df6a4a28c 8024342: PPC64 (part 111): Support for C calling conventions that require 64-bit ints.
goetz
parents: 19283
diff changeset
  3640
  call->init_req(TypeFunc::Memory,    mem_phi->in(LoopNode::EntryControl));
48595
5d699d81c10c 8194988: 8 Null pointer dereference defect groups related to MultiNode::proj_out()
dlong
parents: 48145
diff changeset
  3641
  call->init_req(TypeFunc::ReturnAdr, C->start()->proj_out_or_null(TypeFunc::ReturnAdr));
5d699d81c10c 8194988: 8 Null pointer dereference defect groups related to MultiNode::proj_out()
dlong
parents: 48145
diff changeset
  3642
  call->init_req(TypeFunc::FramePtr,  C->start()->proj_out_or_null(TypeFunc::FramePtr));
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3643
  _igvn.register_new_node_with_optimizer(call);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3644
  result_ctrl = new ProjNode(call,TypeFunc::Control);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3645
  _igvn.register_new_node_with_optimizer(result_ctrl);
24923
9631f7d691dc 8034812: remove IDX_INIT macro hack in Node class
thartmann
parents: 24020
diff changeset
  3646
  result_mem = new ProjNode(call,TypeFunc::Memory);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3647
  _igvn.register_new_node_with_optimizer(result_mem);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3648
14144
effbc094884c 7198606: Improve VM optimization
kvn
parents: 13524
diff changeset
  3649
/* Disable following optimization until proper fix (add missing checks).
effbc094884c 7198606: Improve VM optimization
kvn
parents: 13524
diff changeset
  3650
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3651
  // If this fill is tightly coupled to an allocation and overwrites
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3652
  // the whole body, allow it to take over the zeroing.
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3653
  AllocateNode* alloc = AllocateNode::Ideal_allocation(base, this);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3654
  if (alloc != NULL && alloc->is_AllocateArray()) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3655
    Node* length = alloc->as_AllocateArray()->Ideal_length();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3656
    if (head->limit() == length &&
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3657
        head->init_trip() == _igvn.intcon(0)) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3658
      if (TraceOptimizeFill) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3659
        tty->print_cr("Eliminated zeroing in allocation");
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3660
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3661
      alloc->maybe_set_complete(&_igvn);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3662
    } else {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3663
#ifdef ASSERT
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3664
      if (TraceOptimizeFill) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3665
        tty->print_cr("filling array but bounds don't match");
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3666
        alloc->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3667
        head->init_trip()->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3668
        head->limit()->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3669
        length->dump();
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3670
      }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3671
#endif
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3672
    }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3673
  }
14144
effbc094884c 7198606: Improve VM optimization
kvn
parents: 13524
diff changeset
  3674
*/
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3675
48145
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3676
  if (head->is_strip_mined()) {
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3677
    // Inner strip mined loop goes away so get rid of outer strip
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3678
    // mined loop
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3679
    Node* outer_sfpt = head->outer_safepoint();
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3680
    Node* in = outer_sfpt->in(0);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3681
    Node* outer_out = head->outer_loop_exit();
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3682
    lazy_replace(outer_out, in);
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3683
    _igvn.replace_input_of(outer_sfpt, 0, C->top());
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3684
  }
f913f6dba2d3 8186027: C2: loop strip mining
roland
parents: 47765
diff changeset
  3685
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3686
  // Redirect the old control and memory edges that are outside the loop.
6739
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3687
  // Sometimes the memory phi of the head is used as the outgoing
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3688
  // state of the loop.  It's safe in this case to replace it with the
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3689
  // result_mem.
750998abc20f 6984979: OptimizeFill misses some cases with an odd memory graph
never
parents: 6455
diff changeset
  3690
  _igvn.replace_node(store->in(MemNode::Memory), result_mem);
35759
85d3873f87b4 8147645: get_ctrl_no_update() code is wrong
roland
parents: 35574
diff changeset
  3691
  lazy_replace(exit, result_ctrl);
6433
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3692
  _igvn.replace_node(store, result_mem);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3693
  // Any uses the increment outside of the loop become the loop limit.
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3694
  _igvn.replace_node(head->incr(), head->limit());
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3695
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3696
  // Disconnect the head from the loop.
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3697
  for (uint i = 0; i < lpt->_body.size(); i++) {
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3698
    Node* n = lpt->_body.at(i);
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3699
    _igvn.replace_node(n, C->top());
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3700
  }
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3701
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3702
  return true;
b0e4fafdc38b 4809552: Optimize Arrays.fill(...)
never
parents: 5901
diff changeset
  3703
}