src/share/vm/opto/loopUnswitch.cpp

Fri, 22 Feb 2008 17:55:13 -0800

author
kvn
date
Fri, 22 Feb 2008 17:55:13 -0800
changeset 463
67914967a4b5
parent 435
a61af66fc99e
child 543
a761c2d3b76a
permissions
-rw-r--r--

6650373: Assert in methodOopDesc::make_adapters()
Summary: AdapterHandlerLibrary::get_create_adapter_index() returns incorrect value (-2) when CodeCache is full.
Reviewed-by: sgoldman

     1 /*
     2  * Copyright 2006 Sun Microsystems, Inc.  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 Sun Microsystems, Inc., 4150 Network Circle, Santa Clara,
    20  * CA 95054 USA or visit www.sun.com if you need additional information or
    21  * have any questions.
    22  *
    23  */
    25 #include "incls/_precompiled.incl"
    26 #include "incls/_loopUnswitch.cpp.incl"
    28 //================= Loop Unswitching =====================
    29 //
    30 // orig:                       transformed:
    31 //                               if (invariant-test) then
    32 //  loop                           loop
    33 //    stmt1                          stmt1
    34 //    if (invariant-test) then       stmt2
    35 //      stmt2                        stmt4
    36 //    else                         endloop
    37 //      stmt3                    else
    38 //    endif                        loop [clone]
    39 //    stmt4                          stmt1 [clone]
    40 //  endloop                          stmt3
    41 //                                   stmt4 [clone]
    42 //                                 endloop
    43 //                               endif
    44 //
    45 // Note: the "else" clause may be empty
    47 //------------------------------policy_unswitching-----------------------------
    48 // Return TRUE or FALSE if the loop should be unswitched
    49 // (ie. clone loop with an invariant test that does not exit the loop)
    50 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const {
    51   if( !LoopUnswitching ) {
    52     return false;
    53   }
    54   uint nodes_left = MaxNodeLimit - phase->C->unique();
    55   if (2 * _body.size() > nodes_left) {
    56     return false; // Too speculative if running low on nodes.
    57   }
    58   LoopNode* head = _head->as_Loop();
    59   if (head->unswitch_count() + 1 > head->unswitch_max()) {
    60     return false;
    61   }
    62   return phase->find_unswitching_candidate(this) != NULL;
    63 }
    65 //------------------------------find_unswitching_candidate-----------------------------
    66 // Find candidate "if" for unswitching
    67 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const {
    69   // Find first invariant test that doesn't exit the loop
    70   LoopNode *head = loop->_head->as_Loop();
    71   IfNode* unswitch_iff = NULL;
    72   Node* n = head->in(LoopNode::LoopBackControl);
    73   while (n != head) {
    74     Node* n_dom = idom(n);
    75     if (n->is_Region()) {
    76       if (n_dom->is_If()) {
    77         IfNode* iff = n_dom->as_If();
    78         if (iff->in(1)->is_Bool()) {
    79           BoolNode* bol = iff->in(1)->as_Bool();
    80           if (bol->in(1)->is_Cmp()) {
    81             // If condition is invariant and not a loop exit,
    82             // then found reason to unswitch.
    83             if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) {
    84               unswitch_iff = iff;
    85             }
    86           }
    87         }
    88       }
    89     }
    90     n = n_dom;
    91   }
    92   return unswitch_iff;
    93 }
    95 //------------------------------do_unswitching-----------------------------
    96 // Clone loop with an invariant test (that does not exit) and
    97 // insert a clone of the test that selects which version to
    98 // execute.
    99 void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) {
   101   // Find first invariant test that doesn't exit the loop
   102   LoopNode *head = loop->_head->as_Loop();
   104   IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop);
   105   assert(unswitch_iff != NULL, "should be at least one");
   107   // Need to revert back to normal loop
   108   if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
   109     head->as_CountedLoop()->set_normal_loop();
   110   }
   112   ProjNode* proj_true = create_slow_version_of_loop(loop, old_new);
   114   assert(proj_true->is_IfTrue() && proj_true->unique_ctrl_out() == head, "by construction");
   116   // Increment unswitch count
   117   LoopNode* head_clone = old_new[head->_idx]->as_Loop();
   118   int nct = head->unswitch_count() + 1;
   119   head->set_unswitch_count(nct);
   120   head_clone->set_unswitch_count(nct);
   122   // Add test to new "if" outside of loop
   123   IfNode* invar_iff   = proj_true->in(0)->as_If();
   124   Node* invar_iff_c   = invar_iff->in(0);
   125   BoolNode* bol       = unswitch_iff->in(1)->as_Bool();
   126   invar_iff->set_req(1, bol);
   127   invar_iff->_prob    = unswitch_iff->_prob;
   129   ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj();
   131   // Hoist invariant casts out of each loop to the appropiate
   132   // control projection.
   134   Node_List worklist;
   136   for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
   137     ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj();
   138     // Copy to a worklist for easier manipulation
   139     for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
   140       Node* use = proj->fast_out(j);
   141       if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
   142         worklist.push(use);
   143       }
   144     }
   145     ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj();
   146     while (worklist.size() > 0) {
   147       Node* use = worklist.pop();
   148       Node* nuse = use->clone();
   149       nuse->set_req(0, invar_proj);
   150       _igvn.hash_delete(use);
   151       use->set_req(1, nuse);
   152       _igvn._worklist.push(use);
   153       register_new_node(nuse, invar_proj);
   154       // Same for the clone
   155       Node* use_clone = old_new[use->_idx];
   156       _igvn.hash_delete(use_clone);
   157       use_clone->set_req(1, nuse);
   158       _igvn._worklist.push(use_clone);
   159     }
   160   }
   162   // Hardwire the control paths in the loops into if(true) and if(false)
   163   _igvn.hash_delete(unswitch_iff);
   164   short_circuit_if(unswitch_iff, proj_true);
   165   _igvn._worklist.push(unswitch_iff);
   167   IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If();
   168   _igvn.hash_delete(unswitch_iff_clone);
   169   short_circuit_if(unswitch_iff_clone, proj_false);
   170   _igvn._worklist.push(unswitch_iff_clone);
   172   // Reoptimize loops
   173   loop->record_for_igvn();
   174   for(int i = loop->_body.size() - 1; i >= 0 ; i--) {
   175     Node *n = loop->_body[i];
   176     Node *n_clone = old_new[n->_idx];
   177     _igvn._worklist.push(n_clone);
   178   }
   180 #ifndef PRODUCT
   181   if (TraceLoopUnswitching) {
   182     tty->print_cr("Loop unswitching orig: %d @ %d  new: %d @ %d",
   183                   head->_idx,                unswitch_iff->_idx,
   184                   old_new[head->_idx]->_idx, unswitch_iff_clone->_idx);
   185   }
   186 #endif
   188   C->set_major_progress();
   189 }
   191 //-------------------------create_slow_version_of_loop------------------------
   192 // Create a slow version of the loop by cloning the loop
   193 // and inserting an if to select fast-slow versions.
   194 // Return control projection of the entry to the fast version.
   195 ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop,
   196                                                       Node_List &old_new) {
   197   LoopNode* head  = loop->_head->as_Loop();
   198   Node*     entry = head->in(LoopNode::EntryControl);
   199   _igvn.hash_delete(entry);
   200   _igvn._worklist.push(entry);
   201   IdealLoopTree* outer_loop = loop->_parent;
   203   Node *cont      = _igvn.intcon(1);
   204   set_ctrl(cont, C->root());
   205   Node* opq       = new (C, 2) Opaque1Node(cont);
   206   register_node(opq, outer_loop, entry, dom_depth(entry));
   207   Node *bol       = new (C, 2) Conv2BNode(opq);
   208   register_node(bol, outer_loop, entry, dom_depth(entry));
   209   IfNode* iff = new (C, 2) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN);
   210   register_node(iff, outer_loop, entry, dom_depth(entry));
   211   ProjNode* iffast = new (C, 1) IfTrueNode(iff);
   212   register_node(iffast, outer_loop, iff, dom_depth(iff));
   213   ProjNode* ifslow = new (C, 1) IfFalseNode(iff);
   214   register_node(ifslow, outer_loop, iff, dom_depth(iff));
   216   // Clone the loop body.  The clone becomes the fast loop.  The
   217   // original pre-header will (illegally) have 2 control users (old & new loops).
   218   clone_loop(loop, old_new, dom_depth(head), iff);
   219   assert(old_new[head->_idx]->is_Loop(), "" );
   221   // Fast (true) control
   222   _igvn.hash_delete(head);
   223   head->set_req(LoopNode::EntryControl, iffast);
   224   set_idom(head, iffast, dom_depth(head));
   225   _igvn._worklist.push(head);
   227   // Slow (false) control
   228   LoopNode* slow_head = old_new[head->_idx]->as_Loop();
   229   _igvn.hash_delete(slow_head);
   230   slow_head->set_req(LoopNode::EntryControl, ifslow);
   231   set_idom(slow_head, ifslow, dom_depth(slow_head));
   232   _igvn._worklist.push(slow_head);
   234   recompute_dom_depth();
   236   return iffast;
   237 }

mercurial