< prev index next >

src/hotspot/share/opto/loopUnswitch.cpp

Print this page




   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/convertnode.hpp"
  29 #include "opto/loopnode.hpp"
  30 #include "opto/opaquenode.hpp"
  31 #include "opto/rootnode.hpp"
  32 
  33 //================= Loop Unswitching =====================
  34 //
  35 // orig:                       transformed:
  36 //                               if (invariant-test) then
  37 //  predicate                      predicate
  38 //  loop                           loop
  39 //    stmt1                          stmt1
  40 //    if (invariant-test) then       stmt2
  41 //      stmt2                        stmt4
  42 //    else                         endloop
  43 //      stmt3                    else
  44 //    endif                        predicate [clone]
  45 //    stmt4                        loop [clone]
  46 //  endloop                          stmt1 [clone]
  47 //                                   stmt3
  48 //                                   stmt4 [clone]
  49 //                                 endloop
  50 //                               endif
  51 //
  52 // Note: the "else" clause may be empty
  53 

  54 //------------------------------policy_unswitching-----------------------------
  55 // Return TRUE or FALSE if the loop should be unswitched
  56 // (ie. clone loop with an invariant test that does not exit the loop)
  57 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const {
  58   if (!LoopUnswitching) {
  59     return false;
  60   }
  61   if (!_head->is_Loop()) {
  62     return false;
  63   }
  64 
  65   // If nodes are depleted, some transform has miscalculated its needs.
  66   assert(!phase->exceeding_node_budget(), "sanity");
  67 
  68   // check for vectorized loops, any unswitching was already applied
  69   if (_head->is_CountedLoop() && _head->as_CountedLoop()->is_unroll_only()) {
  70     return false;
  71   }
  72 
  73   LoopNode* head = _head->as_Loop();
  74   if (head->unswitch_count() + 1 > head->unswitch_max()) {
  75     return false;
  76   }
  77   if (phase->find_unswitching_candidate(this) == NULL) {






  78     return false;
  79   }
  80 
  81   // Too speculative if running low on nodes.
  82   return phase->may_require_nodes(est_loop_clone_sz(2));
  83 }
  84 
  85 //------------------------------find_unswitching_candidate-----------------------------
  86 // Find candidate "if" for unswitching
  87 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const {
  88 
  89   // Find first invariant test that doesn't exit the loop
  90   LoopNode *head = loop->_head->as_Loop();
  91   IfNode* unswitch_iff = NULL;
  92   Node* n = head->in(LoopNode::LoopBackControl);
  93   while (n != head) {
  94     Node* n_dom = idom(n);
  95     if (n->is_Region()) {
  96       if (n_dom->is_If()) {
  97         IfNode* iff = n_dom->as_If();
  98         if (iff->in(1)->is_Bool()) {
  99           BoolNode* bol = iff->in(1)->as_Bool();
 100           if (bol->in(1)->is_Cmp()) {
 101             // If condition is invariant and not a loop exit,
 102             // then found reason to unswitch.
 103             if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) {
 104               unswitch_iff = iff;
 105             }
 106           }
 107         }
 108       }
 109     }
 110     n = n_dom;
 111   }















 112   return unswitch_iff;
 113 }
 114 
 115 //------------------------------do_unswitching-----------------------------
 116 // Clone loop with an invariant test (that does not exit) and
 117 // insert a clone of the test that selects which version to
 118 // execute.
 119 void PhaseIdealLoop::do_unswitching(IdealLoopTree *loop, Node_List &old_new) {
 120 
 121   // Find first invariant test that doesn't exit the loop
 122   LoopNode *head = loop->_head->as_Loop();
 123 
 124   IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop);
 125   assert(unswitch_iff != NULL, "should be at least one");




 126 
 127 #ifndef PRODUCT
 128   if (TraceLoopOpts) {
 129     tty->print("Unswitch   %d ", head->unswitch_count()+1);
 130     loop->dump_head();
 131   }
 132 #endif
 133 
 134   // Need to revert back to normal loop
 135   if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
 136     head->as_CountedLoop()->set_normal_loop();
 137   }
 138 
 139   ProjNode* proj_true = create_slow_version_of_loop(loop, old_new, unswitch_iff->Opcode(), CloneIncludesStripMined);
 140 
 141 #ifdef ASSERT
 142   Node* uniqc = proj_true->unique_ctrl_out();
 143   Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl);
 144   Node* predicate = find_predicate(entry);
 145   if (predicate != NULL) {


 151     if (n != NULL) {
 152       predicate = n;
 153       entry = skip_loop_predicates(entry);
 154     }
 155   }
 156   if (predicate != NULL && UseProfiledLoopPredicate) {
 157     entry = find_predicate(entry);
 158     if (entry != NULL) predicate = entry;
 159   }
 160   if (predicate != NULL) predicate = predicate->in(0);
 161   assert(proj_true->is_IfTrue() &&
 162          (predicate == NULL && uniqc == head && !head->is_strip_mined() ||
 163           predicate == NULL && uniqc == head->in(LoopNode::EntryControl) && head->is_strip_mined() ||
 164           predicate != NULL && uniqc == predicate), "by construction");
 165 #endif
 166   // Increment unswitch count
 167   LoopNode* head_clone = old_new[head->_idx]->as_Loop();
 168   int nct = head->unswitch_count() + 1;
 169   head->set_unswitch_count(nct);
 170   head_clone->set_unswitch_count(nct);

 171 
 172   // Add test to new "if" outside of loop
 173   IfNode* invar_iff   = proj_true->in(0)->as_If();
 174   Node* invar_iff_c   = invar_iff->in(0);
 175   BoolNode* bol       = unswitch_iff->in(1)->as_Bool();
 176   invar_iff->set_req(1, bol);
 177   invar_iff->_prob    = unswitch_iff->_prob;

































 178 
 179   ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj();
 180 
 181   // Hoist invariant casts out of each loop to the appropriate
 182   // control projection.
 183 
 184   Node_List worklist;
 185 
 186   for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
 187     ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj();
 188     // Copy to a worklist for easier manipulation
 189     for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
 190       Node* use = proj->fast_out(j);
 191       if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
 192         worklist.push(use);
 193       }
 194     }
 195     ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj();
 196     while (worklist.size() > 0) {
 197       Node* use = worklist.pop();
 198       Node* nuse = use->clone();
 199       nuse->set_req(0, invar_proj);
 200       _igvn.replace_input_of(use, 1, nuse);
 201       register_new_node(nuse, invar_proj);
 202       // Same for the clone
 203       Node* use_clone = old_new[use->_idx];
 204       _igvn.replace_input_of(use_clone, 1, nuse);
 205     }
 206   }
 207 
 208   // Hardwire the control paths in the loops into if(true) and if(false)
 209   _igvn.rehash_node_delayed(unswitch_iff);
 210   short_circuit_if(unswitch_iff, proj_true);
 211 
 212   IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If();
 213   _igvn.rehash_node_delayed(unswitch_iff_clone);
 214   short_circuit_if(unswitch_iff_clone, proj_false);












 215 
 216   // Reoptimize loops
 217   loop->record_for_igvn();
 218   for(int i = loop->_body.size() - 1; i >= 0 ; i--) {
 219     Node *n = loop->_body[i];
 220     Node *n_clone = old_new[n->_idx];
 221     _igvn._worklist.push(n_clone);
 222   }
 223 
 224 #ifndef PRODUCT
 225   if (TraceLoopUnswitching) {
 226     tty->print_cr("Loop unswitching orig: %d @ %d  new: %d @ %d",
 227                   head->_idx,                unswitch_iff->_idx,
 228                   old_new[head->_idx]->_idx, unswitch_iff_clone->_idx);
 229   }
 230 #endif
 231 
 232   C->set_major_progress();
 233 }
 234 




   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/mulnode.hpp"
  28 #include "opto/addnode.hpp"
  29 #include "opto/connode.hpp"
  30 #include "opto/convertnode.hpp"
  31 #include "opto/loopnode.hpp"
  32 #include "opto/opaquenode.hpp"
  33 #include "opto/rootnode.hpp"
  34 
  35 //================= Loop Unswitching =====================
  36 //
  37 // orig:                       transformed:
  38 //                               if (invariant-test) then
  39 //  predicate                      predicate
  40 //  loop                           loop
  41 //    stmt1                          stmt1
  42 //    if (invariant-test) then       stmt2
  43 //      stmt2                        stmt4
  44 //    else                         endloop
  45 //      stmt3                    else
  46 //    endif                        predicate [clone]
  47 //    stmt4                        loop [clone]
  48 //  endloop                          stmt1 [clone]
  49 //                                   stmt3
  50 //                                   stmt4 [clone]
  51 //                                 endloop
  52 //                               endif
  53 //
  54 // Note: the "else" clause may be empty
  55 
  56 
  57 //------------------------------policy_unswitching-----------------------------
  58 // Return TRUE or FALSE if the loop should be unswitched
  59 // (ie. clone loop with an invariant test that does not exit the loop)
  60 bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const {
  61   if (!LoopUnswitching) {
  62     return false;
  63   }
  64   if (!_head->is_Loop()) {
  65     return false;
  66   }
  67 
  68   // If nodes are depleted, some transform has miscalculated its needs.
  69   assert(!phase->exceeding_node_budget(), "sanity");
  70 
  71   // check for vectorized loops, any unswitching was already applied
  72   if (_head->is_CountedLoop() && _head->as_CountedLoop()->is_unroll_only()) {
  73     return false;
  74   }
  75 
  76   LoopNode* head = _head->as_Loop();
  77   if (head->unswitch_count() + 1 > head->unswitch_max()) {
  78     return false;
  79   }
  80 
  81   if (head->is_flattened_arrays()) {
  82     return false;
  83   }
  84 
  85   Node_List flattened_checks;
  86   if (phase->find_unswitching_candidate(this, flattened_checks) == NULL && flattened_checks.size() == 0) {
  87     return false;
  88   }
  89 
  90   // Too speculative if running low on nodes.
  91   return phase->may_require_nodes(est_loop_clone_sz(2));
  92 }
  93 
  94 //------------------------------find_unswitching_candidate-----------------------------
  95 // Find candidate "if" for unswitching
  96 IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop, Node_List& flattened_checks) const {
  97 
  98   // Find first invariant test that doesn't exit the loop
  99   LoopNode *head = loop->_head->as_Loop();
 100   IfNode* unswitch_iff = NULL;
 101   Node* n = head->in(LoopNode::LoopBackControl);
 102   while (n != head) {
 103     Node* n_dom = idom(n);
 104     if (n->is_Region()) {
 105       if (n_dom->is_If()) {
 106         IfNode* iff = n_dom->as_If();
 107         if (iff->in(1)->is_Bool()) {
 108           BoolNode* bol = iff->in(1)->as_Bool();
 109           if (bol->in(1)->is_Cmp()) {
 110             // If condition is invariant and not a loop exit,
 111             // then found reason to unswitch.
 112             if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) {
 113               unswitch_iff = iff;
 114             }
 115           }
 116         }
 117       }
 118     }
 119     n = n_dom;
 120   }
 121 
 122   Node* array;
 123   if (unswitch_iff == NULL || unswitch_iff->is_flattened_array_check(&_igvn, array)) {
 124     // collect all flattened array checks
 125     for (uint i = 0; i < loop->_body.size(); i++) {
 126       Node* n = loop->_body.at(i);
 127       if (n->is_If() && n->as_If()->is_flattened_array_check(&_igvn, array) &&
 128           loop->is_invariant(n->in(1)) &&
 129           !loop->is_loop_exit(n)) {
 130         flattened_checks.push(n);
 131       }
 132     }
 133     unswitch_iff = NULL;
 134   }
 135 
 136   return unswitch_iff;
 137 }
 138 
 139 //------------------------------do_unswitching-----------------------------
 140 // Clone loop with an invariant test (that does not exit) and
 141 // insert a clone of the test that selects which version to
 142 // execute.
 143 void PhaseIdealLoop::do_unswitching(IdealLoopTree *loop, Node_List &old_new) {
 144 
 145   // Find first invariant test that doesn't exit the loop
 146   LoopNode *head = loop->_head->as_Loop();
 147 
 148   Node_List flattened_checks;
 149   IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop, flattened_checks);
 150   assert(unswitch_iff != NULL || flattened_checks.size() > 0, "should be at least one");
 151   if (unswitch_iff == NULL) {
 152     unswitch_iff = flattened_checks.at(0)->as_If();
 153   }
 154 
 155 #ifndef PRODUCT
 156   if (TraceLoopOpts) {
 157     tty->print("Unswitch   %d ", head->unswitch_count()+1);
 158     loop->dump_head();
 159   }
 160 #endif
 161 
 162   // Need to revert back to normal loop
 163   if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
 164     head->as_CountedLoop()->set_normal_loop();
 165   }
 166 
 167   ProjNode* proj_true = create_slow_version_of_loop(loop, old_new, unswitch_iff->Opcode(), CloneIncludesStripMined);
 168 
 169 #ifdef ASSERT
 170   Node* uniqc = proj_true->unique_ctrl_out();
 171   Node* entry = head->skip_strip_mined()->in(LoopNode::EntryControl);
 172   Node* predicate = find_predicate(entry);
 173   if (predicate != NULL) {


 179     if (n != NULL) {
 180       predicate = n;
 181       entry = skip_loop_predicates(entry);
 182     }
 183   }
 184   if (predicate != NULL && UseProfiledLoopPredicate) {
 185     entry = find_predicate(entry);
 186     if (entry != NULL) predicate = entry;
 187   }
 188   if (predicate != NULL) predicate = predicate->in(0);
 189   assert(proj_true->is_IfTrue() &&
 190          (predicate == NULL && uniqc == head && !head->is_strip_mined() ||
 191           predicate == NULL && uniqc == head->in(LoopNode::EntryControl) && head->is_strip_mined() ||
 192           predicate != NULL && uniqc == predicate), "by construction");
 193 #endif
 194   // Increment unswitch count
 195   LoopNode* head_clone = old_new[head->_idx]->as_Loop();
 196   int nct = head->unswitch_count() + 1;
 197   head->set_unswitch_count(nct);
 198   head_clone->set_unswitch_count(nct);
 199   head_clone->mark_flattened_arrays();
 200 
 201   // Add test to new "if" outside of loop
 202   IfNode* invar_iff   = proj_true->in(0)->as_If();
 203   Node* invar_iff_c   = invar_iff->in(0);


 204   invar_iff->_prob    = unswitch_iff->_prob;
 205   if (flattened_checks.size() > 0) {
 206     // Flattened array checks are used in
 207     // Parse::array_store()/Parse::array_load() to switch between a
 208     // legacy object array access and a flattened value array
 209     // access. We want the performance impact on legacy accesses to be
 210     // as small as possible so we make 2 copies of the loops: a fast
 211     // one where all accesses are known to be legacy, a slow one where
 212     // some accesses are to flattened arrays. Flattened array checks
 213     // can be removed from the first one but not from the second one
 214     // as it can have a mix of flattened/legacy accesses.
 215     BoolNode* bol       = unswitch_iff->in(1)->clone()->as_Bool();
 216     register_new_node(bol, invar_iff->in(0));
 217     Node* cmp = bol->in(1)->clone();
 218     register_new_node(cmp, invar_iff->in(0));
 219     bol->set_req(1, cmp);
 220     Node* in1 = NULL;
 221     for (uint i = 0; i < flattened_checks.size(); i++) {
 222       Node* v = flattened_checks.at(i)->in(1)->in(1)->in(1);
 223       v = new AndINode(v, _igvn.intcon(Klass::_lh_array_tag_vt_value));
 224       register_new_node(v, invar_iff->in(0));
 225       if (in1 == NULL) {
 226         in1 = v;
 227       } else {
 228         in1 = new OrINode(in1, v);
 229         register_new_node(in1, invar_iff->in(0));
 230       }
 231     }
 232     cmp->set_req(1, in1);
 233     invar_iff->set_req(1, bol);
 234   } else {
 235     BoolNode* bol       = unswitch_iff->in(1)->as_Bool();
 236     invar_iff->set_req(1, bol);
 237   }
 238 
 239   ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj();
 240 
 241   // Hoist invariant casts out of each loop to the appropriate
 242   // control projection.
 243 
 244   Node_List worklist;
 245 
 246   for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
 247     ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj();
 248     // Copy to a worklist for easier manipulation
 249     for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
 250       Node* use = proj->fast_out(j);
 251       if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
 252         worklist.push(use);
 253       }
 254     }
 255     ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj();
 256     while (worklist.size() > 0) {
 257       Node* use = worklist.pop();
 258       Node* nuse = use->clone();
 259       nuse->set_req(0, invar_proj);
 260       _igvn.replace_input_of(use, 1, nuse);
 261       register_new_node(nuse, invar_proj);
 262       // Same for the clone
 263       Node* use_clone = old_new[use->_idx];
 264       _igvn.replace_input_of(use_clone, 1, nuse);
 265     }
 266   }
 267 




 268   IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If();
 269   if (flattened_checks.size() > 0) {
 270     for (uint i = 0; i < flattened_checks.size(); i++) {
 271       IfNode* iff = flattened_checks.at(i)->as_If();
 272       _igvn.rehash_node_delayed(iff);
 273       short_circuit_if(iff, proj_true);
 274     }
 275   } else {
 276     // Hardwire the control paths in the loops into if(true) and if(false)
 277     _igvn.rehash_node_delayed(unswitch_iff);
 278     short_circuit_if(unswitch_iff, proj_true);
 279 
 280     _igvn.rehash_node_delayed(unswitch_iff_clone);
 281     short_circuit_if(unswitch_iff_clone, proj_false);
 282   }
 283 
 284   // Reoptimize loops
 285   loop->record_for_igvn();
 286   for(int i = loop->_body.size() - 1; i >= 0 ; i--) {
 287     Node *n = loop->_body[i];
 288     Node *n_clone = old_new[n->_idx];
 289     _igvn._worklist.push(n_clone);
 290   }
 291 
 292 #ifndef PRODUCT
 293   if (TraceLoopUnswitching) {
 294     tty->print_cr("Loop unswitching orig: %d @ %d  new: %d @ %d",
 295                   head->_idx,                unswitch_iff->_idx,
 296                   old_new[head->_idx]->_idx, unswitch_iff_clone->_idx);
 297   }
 298 #endif
 299 
 300   C->set_major_progress();
 301 }
 302 


< prev index next >