src/share/vm/opto/loopUnswitch.cpp

Mon, 21 Mar 2011 11:28:14 -0700

author
kvn
date
Mon, 21 Mar 2011 11:28:14 -0700
changeset 2665
9dc311b8473e
parent 2314
f95d63e2154a
child 2708
1d1603768966
child 2727
08eb13460b3a
permissions
-rw-r--r--

7008866: Missing loop predicate for loop with multiple entries
Summary: Add predicates when loop head bytecode is parsed instead of when back branch bytecode is parsed.
Reviewed-by: never

     1 /*
     2  * Copyright (c) 2006, 2010, Oracle and/or its affiliates. All rights reserved.
     3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
     4  *
     5  * This code is free software; you can redistribute it and/or modify it
     6  * under the terms of the GNU General Public License version 2 only, as
     7  * published by the Free Software Foundation.
     8  *
     9  * This code is distributed in the hope that it will be useful, but WITHOUT
    10  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
    11  * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
    12  * version 2 for more details (a copy is included in the LICENSE file that
    13  * accompanied this code).
    14  *
    15  * You should have received a copy of the GNU General Public License version
    16  * 2 along with this work; if not, write to the Free Software Foundation,
    17  * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
    18  *
    19  * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
    20  * or visit www.oracle.com if you need additional information or have any
    21  * questions.
    22  *
    23  */
    25 #include "precompiled.hpp"
    26 #include "memory/allocation.inline.hpp"
    27 #include "opto/connode.hpp"
    28 #include "opto/loopnode.hpp"
    29 #include "opto/rootnode.hpp"
    31 //================= Loop Unswitching =====================
    32 //
    33 // orig:                       transformed:
    34 //                               if (invariant-test) then
    35 //  loop                           loop
    36 //    stmt1                          stmt1
    37 //    if (invariant-test) then       stmt2
    38 //      stmt2                        stmt4
    39 //    else                         endloop
    40 //      stmt3                    else
    41 //    endif                        loop [clone]
    42 //    stmt4                          stmt1 [clone]
    43 //  endloop                          stmt3
    44 //                                   stmt4 [clone]
    45 //                                 endloop
    46 //                               endif
    47 //
    48 // Note: the "else" clause may be empty
    50 //------------------------------policy_unswitching-----------------------------
    51 // Return TRUE or FALSE if the loop should be unswitched
    52 // (ie. clone loop with an invariant test that does not exit the loop)
    53 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const {
    54   if( !LoopUnswitching ) {
    55     return false;
    56   }
    57   if (!_head->is_Loop()) {
    58     return false;
    59   }
    60   uint nodes_left = MaxNodeLimit - phase->C->unique();
    61   if (2 * _body.size() > nodes_left) {
    62     return false; // Too speculative if running low on nodes.
    63   }
    64   LoopNode* head = _head->as_Loop();
    65   if (head->unswitch_count() + 1 > head->unswitch_max()) {
    66     return false;
    67   }
    68   return phase->find_unswitching_candidate(this) != NULL;
    69 }
    71 //------------------------------find_unswitching_candidate-----------------------------
    72 // Find candidate "if" for unswitching
    73 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const {
    75   // Find first invariant test that doesn't exit the loop
    76   LoopNode *head = loop->_head->as_Loop();
    77   IfNode* unswitch_iff = NULL;
    78   Node* n = head->in(LoopNode::LoopBackControl);
    79   while (n != head) {
    80     Node* n_dom = idom(n);
    81     if (n->is_Region()) {
    82       if (n_dom->is_If()) {
    83         IfNode* iff = n_dom->as_If();
    84         if (iff->in(1)->is_Bool()) {
    85           BoolNode* bol = iff->in(1)->as_Bool();
    86           if (bol->in(1)->is_Cmp()) {
    87             // If condition is invariant and not a loop exit,
    88             // then found reason to unswitch.
    89             if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) {
    90               unswitch_iff = iff;
    91             }
    92           }
    93         }
    94       }
    95     }
    96     n = n_dom;
    97   }
    98   return unswitch_iff;
    99 }
   101 //------------------------------do_unswitching-----------------------------
   102 // Clone loop with an invariant test (that does not exit) and
   103 // insert a clone of the test that selects which version to
   104 // execute.
   105 void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) {
   107   // Find first invariant test that doesn't exit the loop
   108   LoopNode *head = loop->_head->as_Loop();
   110   IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop);
   111   assert(unswitch_iff != NULL, "should be at least one");
   113 #ifndef PRODUCT
   114   if (TraceLoopOpts) {
   115     tty->print("Unswitch   %d ", head->unswitch_count()+1);
   116     loop->dump_head();
   117   }
   118 #endif
   120   // Need to revert back to normal loop
   121   if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
   122     head->as_CountedLoop()->set_normal_loop();
   123   }
   125   ProjNode* proj_true = create_slow_version_of_loop(loop, old_new);
   127   assert(proj_true->is_IfTrue() && proj_true->unique_ctrl_out() == head, "by construction");
   129   // Increment unswitch count
   130   LoopNode* head_clone = old_new[head->_idx]->as_Loop();
   131   int nct = head->unswitch_count() + 1;
   132   head->set_unswitch_count(nct);
   133   head_clone->set_unswitch_count(nct);
   135   // Add test to new "if" outside of loop
   136   IfNode* invar_iff   = proj_true->in(0)->as_If();
   137   Node* invar_iff_c   = invar_iff->in(0);
   138   BoolNode* bol       = unswitch_iff->in(1)->as_Bool();
   139   invar_iff->set_req(1, bol);
   140   invar_iff->_prob    = unswitch_iff->_prob;
   142   ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj();
   144   // Hoist invariant casts out of each loop to the appropriate
   145   // control projection.
   147   Node_List worklist;
   149   for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
   150     ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj();
   151     // Copy to a worklist for easier manipulation
   152     for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
   153       Node* use = proj->fast_out(j);
   154       if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
   155         worklist.push(use);
   156       }
   157     }
   158     ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj();
   159     while (worklist.size() > 0) {
   160       Node* use = worklist.pop();
   161       Node* nuse = use->clone();
   162       nuse->set_req(0, invar_proj);
   163       _igvn.hash_delete(use);
   164       use->set_req(1, nuse);
   165       _igvn._worklist.push(use);
   166       register_new_node(nuse, invar_proj);
   167       // Same for the clone
   168       Node* use_clone = old_new[use->_idx];
   169       _igvn.hash_delete(use_clone);
   170       use_clone->set_req(1, nuse);
   171       _igvn._worklist.push(use_clone);
   172     }
   173   }
   175   // Hardwire the control paths in the loops into if(true) and if(false)
   176   _igvn.hash_delete(unswitch_iff);
   177   short_circuit_if(unswitch_iff, proj_true);
   178   _igvn._worklist.push(unswitch_iff);
   180   IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If();
   181   _igvn.hash_delete(unswitch_iff_clone);
   182   short_circuit_if(unswitch_iff_clone, proj_false);
   183   _igvn._worklist.push(unswitch_iff_clone);
   185   // Reoptimize loops
   186   loop->record_for_igvn();
   187   for(int i = loop->_body.size() - 1; i >= 0 ; i--) {
   188     Node *n = loop->_body[i];
   189     Node *n_clone = old_new[n->_idx];
   190     _igvn._worklist.push(n_clone);
   191   }
   193 #ifndef PRODUCT
   194   if (TraceLoopUnswitching) {
   195     tty->print_cr("Loop unswitching orig: %d @ %d  new: %d @ %d",
   196                   head->_idx,                unswitch_iff->_idx,
   197                   old_new[head->_idx]->_idx, unswitch_iff_clone->_idx);
   198   }
   199 #endif
   201   C->set_major_progress();
   202 }
   204 //-------------------------create_slow_version_of_loop------------------------
   205 // Create a slow version of the loop by cloning the loop
   206 // and inserting an if to select fast-slow versions.
   207 // Return control projection of the entry to the fast version.
   208 ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop,
   209                                                       Node_List &old_new) {
   210   LoopNode* head  = loop->_head->as_Loop();
   211   Node*     entry = head->in(LoopNode::EntryControl);
   212   _igvn.hash_delete(entry);
   213   _igvn._worklist.push(entry);
   214   IdealLoopTree* outer_loop = loop->_parent;
   216   Node *cont      = _igvn.intcon(1);
   217   set_ctrl(cont, C->root());
   218   Node* opq       = new (C, 2) Opaque1Node(C, cont);
   219   register_node(opq, outer_loop, entry, dom_depth(entry));
   220   Node *bol       = new (C, 2) Conv2BNode(opq);
   221   register_node(bol, outer_loop, entry, dom_depth(entry));
   222   IfNode* iff = new (C, 2) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN);
   223   register_node(iff, outer_loop, entry, dom_depth(entry));
   224   ProjNode* iffast = new (C, 1) IfTrueNode(iff);
   225   register_node(iffast, outer_loop, iff, dom_depth(iff));
   226   ProjNode* ifslow = new (C, 1) IfFalseNode(iff);
   227   register_node(ifslow, outer_loop, iff, dom_depth(iff));
   229   // Clone the loop body.  The clone becomes the fast loop.  The
   230   // original pre-header will (illegally) have 2 control users (old & new loops).
   231   clone_loop(loop, old_new, dom_depth(head), iff);
   232   assert(old_new[head->_idx]->is_Loop(), "" );
   234   // Fast (true) control
   235   _igvn.hash_delete(head);
   236   head->set_req(LoopNode::EntryControl, iffast);
   237   set_idom(head, iffast, dom_depth(head));
   238   _igvn._worklist.push(head);
   240   // Slow (false) control
   241   LoopNode* slow_head = old_new[head->_idx]->as_Loop();
   242   _igvn.hash_delete(slow_head);
   243   slow_head->set_req(LoopNode::EntryControl, ifslow);
   244   set_idom(slow_head, ifslow, dom_depth(slow_head));
   245   _igvn._worklist.push(slow_head);
   247   recompute_dom_depth();
   249   return iffast;
   250 }

mercurial