loopUnswitch.cpp 9.4 KB
Newer Older
D
duke 已提交
1
/*
2
 * Copyright (c) 2006, 2012, Oracle and/or its affiliates. All rights reserved.
D
duke 已提交
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
 *
 * This code is free software; you can redistribute it and/or modify it
 * under the terms of the GNU General Public License version 2 only, as
 * published by the Free Software Foundation.
 *
 * This code is distributed in the hope that it will be useful, but WITHOUT
 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
 * FITNESS FOR A PARTICULAR PURPOSE.  See the GNU General Public License
 * version 2 for more details (a copy is included in the LICENSE file that
 * accompanied this code).
 *
 * You should have received a copy of the GNU General Public License version
 * 2 along with this work; if not, write to the Free Software Foundation,
 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
 *
19 20 21
 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
 * or visit www.oracle.com if you need additional information or have any
 * questions.
D
duke 已提交
22 23 24
 *
 */

25 26 27 28 29
#include "precompiled.hpp"
#include "memory/allocation.inline.hpp"
#include "opto/connode.hpp"
#include "opto/loopnode.hpp"
#include "opto/rootnode.hpp"
D
duke 已提交
30 31 32 33 34

//================= Loop Unswitching =====================
//
// orig:                       transformed:
//                               if (invariant-test) then
35
//  predicate                      predicate
D
duke 已提交
36 37 38 39 40 41
//  loop                           loop
//    stmt1                          stmt1
//    if (invariant-test) then       stmt2
//      stmt2                        stmt4
//    else                         endloop
//      stmt3                    else
42 43 44 45
//    endif                        predicate [clone]
//    stmt4                        loop [clone]
//  endloop                          stmt1 [clone]
//                                   stmt3
D
duke 已提交
46 47 48 49 50 51 52 53 54 55 56 57 58
//                                   stmt4 [clone]
//                                 endloop
//                               endif
//
// Note: the "else" clause may be empty

//------------------------------policy_unswitching-----------------------------
// Return TRUE or FALSE if the loop should be unswitched
// (ie. clone loop with an invariant test that does not exit the loop)
bool IdealLoopTree::policy_unswitching( PhaseIdealLoop *phase ) const {
  if( !LoopUnswitching ) {
    return false;
  }
59 60 61
  if (!_head->is_Loop()) {
    return false;
  }
D
duke 已提交
62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114
  uint nodes_left = MaxNodeLimit - phase->C->unique();
  if (2 * _body.size() > nodes_left) {
    return false; // Too speculative if running low on nodes.
  }
  LoopNode* head = _head->as_Loop();
  if (head->unswitch_count() + 1 > head->unswitch_max()) {
    return false;
  }
  return phase->find_unswitching_candidate(this) != NULL;
}

//------------------------------find_unswitching_candidate-----------------------------
// Find candidate "if" for unswitching
IfNode* PhaseIdealLoop::find_unswitching_candidate(const IdealLoopTree *loop) const {

  // Find first invariant test that doesn't exit the loop
  LoopNode *head = loop->_head->as_Loop();
  IfNode* unswitch_iff = NULL;
  Node* n = head->in(LoopNode::LoopBackControl);
  while (n != head) {
    Node* n_dom = idom(n);
    if (n->is_Region()) {
      if (n_dom->is_If()) {
        IfNode* iff = n_dom->as_If();
        if (iff->in(1)->is_Bool()) {
          BoolNode* bol = iff->in(1)->as_Bool();
          if (bol->in(1)->is_Cmp()) {
            // If condition is invariant and not a loop exit,
            // then found reason to unswitch.
            if (loop->is_invariant(bol) && !loop->is_loop_exit(iff)) {
              unswitch_iff = iff;
            }
          }
        }
      }
    }
    n = n_dom;
  }
  return unswitch_iff;
}

//------------------------------do_unswitching-----------------------------
// Clone loop with an invariant test (that does not exit) and
// insert a clone of the test that selects which version to
// execute.
void PhaseIdealLoop::do_unswitching (IdealLoopTree *loop, Node_List &old_new) {

  // Find first invariant test that doesn't exit the loop
  LoopNode *head = loop->_head->as_Loop();

  IfNode* unswitch_iff = find_unswitching_candidate((const IdealLoopTree *)loop);
  assert(unswitch_iff != NULL, "should be at least one");

115 116 117 118 119 120 121
#ifndef PRODUCT
  if (TraceLoopOpts) {
    tty->print("Unswitch   %d ", head->unswitch_count()+1);
    loop->dump_head();
  }
#endif

D
duke 已提交
122 123 124 125 126 127 128
  // Need to revert back to normal loop
  if (head->is_CountedLoop() && !head->as_CountedLoop()->is_normal_loop()) {
    head->as_CountedLoop()->set_normal_loop();
  }

  ProjNode* proj_true = create_slow_version_of_loop(loop, old_new);

129 130 131 132
#ifdef ASSERT
  Node* uniqc = proj_true->unique_ctrl_out();
  Node* entry = head->in(LoopNode::EntryControl);
  Node* predicate = find_predicate(entry);
133 134 135 136 137
  if (predicate != NULL && LoopLimitCheck && UseLoopPredicate) {
    // We may have two predicates, find first.
    entry = find_predicate(entry->in(0)->in(0));
    if (entry != NULL) predicate = entry;
  }
138 139 140 141 142
  if (predicate != NULL) predicate = predicate->in(0);
  assert(proj_true->is_IfTrue() &&
         (predicate == NULL && uniqc == head ||
          predicate != NULL && uniqc == predicate), "by construction");
#endif
D
duke 已提交
143 144 145 146 147 148 149 150 151 152 153 154 155 156 157
  // Increment unswitch count
  LoopNode* head_clone = old_new[head->_idx]->as_Loop();
  int nct = head->unswitch_count() + 1;
  head->set_unswitch_count(nct);
  head_clone->set_unswitch_count(nct);

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

  ProjNode* proj_false = invar_iff->proj_out(0)->as_Proj();

T
twisti 已提交
158
  // Hoist invariant casts out of each loop to the appropriate
D
duke 已提交
159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176
  // control projection.

  Node_List worklist;

  for (DUIterator_Fast imax, i = unswitch_iff->fast_outs(imax); i < imax; i++) {
    ProjNode* proj= unswitch_iff->fast_out(i)->as_Proj();
    // Copy to a worklist for easier manipulation
    for (DUIterator_Fast jmax, j = proj->fast_outs(jmax); j < jmax; j++) {
      Node* use = proj->fast_out(j);
      if (use->Opcode() == Op_CheckCastPP && loop->is_invariant(use->in(1))) {
        worklist.push(use);
      }
    }
    ProjNode* invar_proj = invar_iff->proj_out(proj->_con)->as_Proj();
    while (worklist.size() > 0) {
      Node* use = worklist.pop();
      Node* nuse = use->clone();
      nuse->set_req(0, invar_proj);
177
      _igvn.replace_input_of(use, 1, nuse);
D
duke 已提交
178 179 180
      register_new_node(nuse, invar_proj);
      // Same for the clone
      Node* use_clone = old_new[use->_idx];
181
      _igvn.replace_input_of(use_clone, 1, nuse);
D
duke 已提交
182 183 184 185
    }
  }

  // Hardwire the control paths in the loops into if(true) and if(false)
186
  _igvn.rehash_node_delayed(unswitch_iff);
D
duke 已提交
187 188 189
  short_circuit_if(unswitch_iff, proj_true);

  IfNode* unswitch_iff_clone = old_new[unswitch_iff->_idx]->as_If();
190
  _igvn.rehash_node_delayed(unswitch_iff_clone);
D
duke 已提交
191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218
  short_circuit_if(unswitch_iff_clone, proj_false);

  // Reoptimize loops
  loop->record_for_igvn();
  for(int i = loop->_body.size() - 1; i >= 0 ; i--) {
    Node *n = loop->_body[i];
    Node *n_clone = old_new[n->_idx];
    _igvn._worklist.push(n_clone);
  }

#ifndef PRODUCT
  if (TraceLoopUnswitching) {
    tty->print_cr("Loop unswitching orig: %d @ %d  new: %d @ %d",
                  head->_idx,                unswitch_iff->_idx,
                  old_new[head->_idx]->_idx, unswitch_iff_clone->_idx);
  }
#endif

  C->set_major_progress();
}

//-------------------------create_slow_version_of_loop------------------------
// Create a slow version of the loop by cloning the loop
// and inserting an if to select fast-slow versions.
// Return control projection of the entry to the fast version.
ProjNode* PhaseIdealLoop::create_slow_version_of_loop(IdealLoopTree *loop,
                                                      Node_List &old_new) {
  LoopNode* head  = loop->_head->as_Loop();
219
  bool counted_loop = head->is_CountedLoop();
D
duke 已提交
220
  Node*     entry = head->in(LoopNode::EntryControl);
221
  _igvn.rehash_node_delayed(entry);
D
duke 已提交
222 223 224 225
  IdealLoopTree* outer_loop = loop->_parent;

  Node *cont      = _igvn.intcon(1);
  set_ctrl(cont, C->root());
226
  Node* opq       = new (C) Opaque1Node(C, cont);
D
duke 已提交
227
  register_node(opq, outer_loop, entry, dom_depth(entry));
228
  Node *bol       = new (C) Conv2BNode(opq);
D
duke 已提交
229
  register_node(bol, outer_loop, entry, dom_depth(entry));
230
  IfNode* iff = new (C) IfNode(entry, bol, PROB_MAX, COUNT_UNKNOWN);
D
duke 已提交
231
  register_node(iff, outer_loop, entry, dom_depth(entry));
232
  ProjNode* iffast = new (C) IfTrueNode(iff);
D
duke 已提交
233
  register_node(iffast, outer_loop, iff, dom_depth(iff));
234
  ProjNode* ifslow = new (C) IfFalseNode(iff);
D
duke 已提交
235 236 237
  register_node(ifslow, outer_loop, iff, dom_depth(iff));

  // Clone the loop body.  The clone becomes the fast loop.  The
238 239
  // original pre-header will (illegally) have 3 control users
  // (old & new loops & new if).
D
duke 已提交
240 241 242 243
  clone_loop(loop, old_new, dom_depth(head), iff);
  assert(old_new[head->_idx]->is_Loop(), "" );

  // Fast (true) control
244
  Node* iffast_pred = clone_loop_predicates(entry, iffast, !counted_loop);
245
  _igvn.replace_input_of(head, LoopNode::EntryControl, iffast_pred);
246
  set_idom(head, iffast_pred, dom_depth(head));
D
duke 已提交
247 248

  // Slow (false) control
249
  Node* ifslow_pred = clone_loop_predicates(entry, ifslow, !counted_loop);
D
duke 已提交
250
  LoopNode* slow_head = old_new[head->_idx]->as_Loop();
251
  _igvn.replace_input_of(slow_head, LoopNode::EntryControl, ifslow_pred);
252
  set_idom(slow_head, ifslow_pred, dom_depth(slow_head));
D
duke 已提交
253 254 255 256 257

  recompute_dom_depth();

  return iffast;
}