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  */
  24 
  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"
  30 
  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
  49 
  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 }
  70 
  71 //------------------------------find_unswitching_candidate-----------------------------
  72 // Find candidate "if" for unswitching
  73 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const {
  74 
  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 }
 100 
 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) {
 106 
 107   // Find first invariant test that doesn't exit the loop
 108   LoopNode *head = loop->_head->as_Loop();
 109 
 110   IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop);
 111   assert(unswitch_iff != NULL, "should be at least one");
 112 
 113   // Need to revert back to normal loop
 114   if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
 115     head->as_CountedLoop()->set_normal_loop();
 116   }
 117 
 118   ProjNode* proj_true = create_slow_version_of_loop(loop, old_new);
 119 
 120   assert(proj_true->is_IfTrue() && proj_true->unique_ctrl_out() == head, "by construction");
 121 
 122   // Increment unswitch count
 123   LoopNode* head_clone = old_new[head->_idx]->as_Loop();
 124   int nct = head->unswitch_count() + 1;
 125   head->set_unswitch_count(nct);
 126   head_clone->set_unswitch_count(nct);
 127 
 128   // Add test to new "if" outside of loop
 129   IfNode* invar_iff   = proj_true->in(0)->as_If();
 130   Node* invar_iff_c   = invar_iff->in(0);
 131   BoolNode* bol       = unswitch_iff->in(1)->as_Bool();
 132   invar_iff->set_req(1, bol);
 133   invar_iff->_prob    = unswitch_iff->_prob;
 134 
 135   ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj();
 136 
 137   // Hoist invariant casts out of each loop to the appropriate
 138   // control projection.
 139 
 140   Node_List worklist;
 141 
 142   for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
 143     ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj();
 144     // Copy to a worklist for easier manipulation
 145     for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
 146       Node* use = proj->fast_out(j);
 147       if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
 148         worklist.push(use);
 149       }
 150     }
 151     ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj();
 152     while (worklist.size() > 0) {
 153       Node* use = worklist.pop();
 154       Node* nuse = use->clone();
 155       nuse->set_req(0, invar_proj);
 156       _igvn.hash_delete(use);
 157       use->set_req(1, nuse);
 158       _igvn._worklist.push(use);
 159       register_new_node(nuse, invar_proj);
 160       // Same for the clone
 161       Node* use_clone = old_new[use->_idx];
 162       _igvn.hash_delete(use_clone);
 163       use_clone->set_req(1, nuse);
 164       _igvn._worklist.push(use_clone);
 165     }
 166   }
 167 
 168   // Hardwire the control paths in the loops into if(true) and if(false)
 169   _igvn.hash_delete(unswitch_iff);
 170   short_circuit_if(unswitch_iff, proj_true);
 171   _igvn._worklist.push(unswitch_iff);
 172 
 173   IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If();
 174   _igvn.hash_delete(unswitch_iff_clone);
 175   short_circuit_if(unswitch_iff_clone, proj_false);
 176   _igvn._worklist.push(unswitch_iff_clone);
 177 
 178   // Reoptimize loops
 179   loop->record_for_igvn();
 180   for(int i = loop->_body.size() - 1; i >= 0 ; i--) {
 181     Node *n = loop->_body[i];
 182     Node *n_clone = old_new[n->_idx];
 183     _igvn._worklist.push(n_clone);
 184   }
 185 
 186 #ifndef PRODUCT
 187   if (TraceLoopUnswitching) {
 188     tty->print_cr("Loop unswitching orig: %d @ %d  new: %d @ %d",
 189                   head->_idx,                unswitch_iff->_idx,
 190                   old_new[head->_idx]->_idx, unswitch_iff_clone->_idx);
 191   }
 192 #endif
 193 
 194   C->set_major_progress();
 195 }
 196 
 197 //-------------------------create_slow_version_of_loop------------------------
 198 // Create a slow version of the loop by cloning the loop
 199 // and inserting an if to select fast-slow versions.
 200 // Return control projection of the entry to the fast version.
 201 ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop,
 202                                                       Node_List &old_new) {
 203   LoopNode* head  = loop->_head->as_Loop();
 204   Node*     entry = head->in(LoopNode::EntryControl);
 205   _igvn.hash_delete(entry);
 206   _igvn._worklist.push(entry);
 207   IdealLoopTree* outer_loop = loop->_parent;
 208 
 209   Node *cont      = _igvn.intcon(1);
 210   set_ctrl(cont, C->root());
 211   Node* opq       = new (C, 2) Opaque1Node(C, cont);
 212   register_node(opq, outer_loop, entry, dom_depth(entry));
 213   Node *bol       = new (C, 2) Conv2BNode(opq);
 214   register_node(bol, outer_loop, entry, dom_depth(entry));
 215   IfNode* iff = new (C, 2) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN);
 216   register_node(iff, outer_loop, entry, dom_depth(entry));
 217   ProjNode* iffast = new (C, 1) IfTrueNode(iff);
 218   register_node(iffast, outer_loop, iff, dom_depth(iff));
 219   ProjNode* ifslow = new (C, 1) IfFalseNode(iff);
 220   register_node(ifslow, outer_loop, iff, dom_depth(iff));
 221 
 222   // Clone the loop body.  The clone becomes the fast loop.  The
 223   // original pre-header will (illegally) have 2 control users (old & new loops).
 224   clone_loop(loop, old_new, dom_depth(head), iff);
 225   assert(old_new[head->_idx]->is_Loop(), "" );
 226 
 227   // Fast (true) control
 228   _igvn.hash_delete(head);
 229   head->set_req(LoopNode::EntryControl, iffast);
 230   set_idom(head, iffast, dom_depth(head));
 231   _igvn._worklist.push(head);
 232 
 233   // Slow (false) control
 234   LoopNode* slow_head = old_new[head->_idx]->as_Loop();
 235   _igvn.hash_delete(slow_head);
 236   slow_head->set_req(LoopNode::EntryControl, ifslow);
 237   set_idom(slow_head, ifslow, dom_depth(slow_head));
 238   _igvn._worklist.push(slow_head);
 239 
 240   recompute_dom_depth();
 241 
 242   return iffast;
 243 }