annotate src/share/vm/opto/loopPredicate.cpp @ 2433:2e038ad0c1d0

7009361: JSR 292 Invalid value on stack on solaris-sparc with -Xcomp Reviewed-by: kvn, twisti
author never
date Mon, 02 May 2011 18:53:37 -0700
parents 08eb13460b3a
children bad7ecd0b6ed
rev   line source
kvn@2292 1 /*
kvn@2292 2 * Copyright (c) 2011, Oracle and/or its affiliates. All rights reserved.
kvn@2292 3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
kvn@2292 4 *
kvn@2292 5 * This code is free software; you can redistribute it and/or modify it
kvn@2292 6 * under the terms of the GNU General Public License version 2 only, as
kvn@2292 7 * published by the Free Software Foundation.
kvn@2292 8 *
kvn@2292 9 * This code is distributed in the hope that it will be useful, but WITHOUT
kvn@2292 10 * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
kvn@2292 11 * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
kvn@2292 12 * version 2 for more details (a copy is included in the LICENSE file that
kvn@2292 13 * accompanied this code).
kvn@2292 14 *
kvn@2292 15 * You should have received a copy of the GNU General Public License version
kvn@2292 16 * 2 along with this work; if not, write to the Free Software Foundation,
kvn@2292 17 * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA.
kvn@2292 18 *
kvn@2292 19 * Please contact Oracle, 500 Oracle Parkway, Redwood Shores, CA 94065 USA
kvn@2292 20 * or visit www.oracle.com if you need additional information or have any
kvn@2292 21 * questions.
kvn@2292 22 *
kvn@2292 23 */
kvn@2292 24
kvn@2292 25 #include "precompiled.hpp"
kvn@2292 26 #include "opto/loopnode.hpp"
kvn@2292 27 #include "opto/addnode.hpp"
kvn@2292 28 #include "opto/callnode.hpp"
kvn@2292 29 #include "opto/connode.hpp"
kvn@2292 30 #include "opto/loopnode.hpp"
kvn@2292 31 #include "opto/mulnode.hpp"
kvn@2292 32 #include "opto/rootnode.hpp"
kvn@2292 33 #include "opto/subnode.hpp"
kvn@2292 34
kvn@2292 35 /*
kvn@2292 36 * The general idea of Loop Predication is to insert a predicate on the entry
kvn@2292 37 * path to a loop, and raise a uncommon trap if the check of the condition fails.
kvn@2292 38 * The condition checks are promoted from inside the loop body, and thus
kvn@2292 39 * the checks inside the loop could be eliminated. Currently, loop predication
kvn@2292 40 * optimization has been applied to remove array range check and loop invariant
kvn@2292 41 * checks (such as null checks).
kvn@2292 42 */
kvn@2292 43
kvn@2292 44 //-------------------------------is_uncommon_trap_proj----------------------------
kvn@2292 45 // Return true if proj is the form of "proj->[region->..]call_uct"
kvn@2292 46 bool PhaseIdealLoop::is_uncommon_trap_proj(ProjNode* proj, Deoptimization::DeoptReason reason) {
kvn@2292 47 int path_limit = 10;
kvn@2292 48 assert(proj, "invalid argument");
kvn@2292 49 Node* out = proj;
kvn@2292 50 for (int ct = 0; ct < path_limit; ct++) {
kvn@2292 51 out = out->unique_ctrl_out();
kvn@2292 52 if (out == NULL)
kvn@2292 53 return false;
kvn@2292 54 if (out->is_CallStaticJava()) {
kvn@2292 55 int req = out->as_CallStaticJava()->uncommon_trap_request();
kvn@2292 56 if (req != 0) {
kvn@2292 57 Deoptimization::DeoptReason trap_reason = Deoptimization::trap_request_reason(req);
kvn@2292 58 if (trap_reason == reason || reason == Deoptimization::Reason_none) {
kvn@2292 59 return true;
kvn@2292 60 }
kvn@2292 61 }
kvn@2292 62 return false; // don't do further after call
kvn@2292 63 }
kvn@2292 64 if (out->Opcode() != Op_Region)
kvn@2292 65 return false;
kvn@2292 66 }
kvn@2292 67 return false;
kvn@2292 68 }
kvn@2292 69
kvn@2292 70 //-------------------------------is_uncommon_trap_if_pattern-------------------------
kvn@2292 71 // Return true for "if(test)-> proj -> ...
kvn@2292 72 // |
kvn@2292 73 // V
kvn@2292 74 // other_proj->[region->..]call_uct"
kvn@2292 75 //
kvn@2292 76 // "must_reason_predicate" means the uct reason must be Reason_predicate
kvn@2292 77 bool PhaseIdealLoop::is_uncommon_trap_if_pattern(ProjNode *proj, Deoptimization::DeoptReason reason) {
kvn@2292 78 Node *in0 = proj->in(0);
kvn@2292 79 if (!in0->is_If()) return false;
kvn@2292 80 // Variation of a dead If node.
kvn@2292 81 if (in0->outcnt() < 2) return false;
kvn@2292 82 IfNode* iff = in0->as_If();
kvn@2292 83
kvn@2292 84 // we need "If(Conv2B(Opaque1(...)))" pattern for reason_predicate
kvn@2292 85 if (reason != Deoptimization::Reason_none) {
kvn@2292 86 if (iff->in(1)->Opcode() != Op_Conv2B ||
kvn@2292 87 iff->in(1)->in(1)->Opcode() != Op_Opaque1) {
kvn@2292 88 return false;
kvn@2292 89 }
kvn@2292 90 }
kvn@2292 91
kvn@2292 92 ProjNode* other_proj = iff->proj_out(1-proj->_con)->as_Proj();
kvn@2292 93 if (is_uncommon_trap_proj(other_proj, reason)) {
kvn@2292 94 assert(reason == Deoptimization::Reason_none ||
kvn@2292 95 Compile::current()->is_predicate_opaq(iff->in(1)->in(1)), "should be on the list");
kvn@2292 96 return true;
kvn@2292 97 }
kvn@2292 98 return false;
kvn@2292 99 }
kvn@2292 100
kvn@2292 101 //-------------------------------register_control-------------------------
kvn@2292 102 void PhaseIdealLoop::register_control(Node* n, IdealLoopTree *loop, Node* pred) {
kvn@2292 103 assert(n->is_CFG(), "must be control node");
kvn@2292 104 _igvn.register_new_node_with_optimizer(n);
kvn@2292 105 loop->_body.push(n);
kvn@2292 106 set_loop(n, loop);
kvn@2292 107 // When called from beautify_loops() idom is not constructed yet.
kvn@2292 108 if (_idom != NULL) {
kvn@2292 109 set_idom(n, pred, dom_depth(pred));
kvn@2292 110 }
kvn@2292 111 }
kvn@2292 112
kvn@2292 113 //------------------------------create_new_if_for_predicate------------------------
kvn@2292 114 // create a new if above the uct_if_pattern for the predicate to be promoted.
kvn@2292 115 //
kvn@2292 116 // before after
kvn@2292 117 // ---------- ----------
kvn@2292 118 // ctrl ctrl
kvn@2292 119 // | |
kvn@2292 120 // | |
kvn@2292 121 // v v
kvn@2292 122 // iff new_iff
kvn@2292 123 // / \ / \
kvn@2292 124 // / \ / \
kvn@2292 125 // v v v v
kvn@2292 126 // uncommon_proj cont_proj if_uct if_cont
kvn@2292 127 // \ | | | |
kvn@2292 128 // \ | | | |
kvn@2292 129 // v v v | v
kvn@2292 130 // rgn loop | iff
kvn@2292 131 // | | / \
kvn@2292 132 // | | / \
kvn@2292 133 // v | v v
kvn@2292 134 // uncommon_trap | uncommon_proj cont_proj
kvn@2292 135 // \ \ | |
kvn@2292 136 // \ \ | |
kvn@2292 137 // v v v v
kvn@2292 138 // rgn loop
kvn@2292 139 // |
kvn@2292 140 // |
kvn@2292 141 // v
kvn@2292 142 // uncommon_trap
kvn@2292 143 //
kvn@2292 144 //
kvn@2292 145 // We will create a region to guard the uct call if there is no one there.
kvn@2292 146 // The true projecttion (if_cont) of the new_iff is returned.
kvn@2292 147 // This code is also used to clone predicates to clonned loops.
kvn@2292 148 ProjNode* PhaseIdealLoop::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
kvn@2292 149 Deoptimization::DeoptReason reason) {
kvn@2292 150 assert(is_uncommon_trap_if_pattern(cont_proj, reason), "must be a uct if pattern!");
kvn@2292 151 IfNode* iff = cont_proj->in(0)->as_If();
kvn@2292 152
kvn@2292 153 ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
kvn@2292 154 Node *rgn = uncommon_proj->unique_ctrl_out();
kvn@2292 155 assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
kvn@2292 156
kvn@2292 157 uint proj_index = 1; // region's edge corresponding to uncommon_proj
kvn@2292 158 if (!rgn->is_Region()) { // create a region to guard the call
kvn@2292 159 assert(rgn->is_Call(), "must be call uct");
kvn@2292 160 CallNode* call = rgn->as_Call();
kvn@2292 161 IdealLoopTree* loop = get_loop(call);
kvn@2292 162 rgn = new (C, 1) RegionNode(1);
kvn@2292 163 rgn->add_req(uncommon_proj);
kvn@2292 164 register_control(rgn, loop, uncommon_proj);
kvn@2292 165 _igvn.hash_delete(call);
kvn@2292 166 call->set_req(0, rgn);
kvn@2292 167 // When called from beautify_loops() idom is not constructed yet.
kvn@2292 168 if (_idom != NULL) {
kvn@2292 169 set_idom(call, rgn, dom_depth(rgn));
kvn@2292 170 }
kvn@2292 171 } else {
kvn@2292 172 // Find region's edge corresponding to uncommon_proj
kvn@2292 173 for (; proj_index < rgn->req(); proj_index++)
kvn@2292 174 if (rgn->in(proj_index) == uncommon_proj) break;
kvn@2292 175 assert(proj_index < rgn->req(), "sanity");
kvn@2292 176 }
kvn@2292 177
kvn@2292 178 Node* entry = iff->in(0);
kvn@2292 179 if (new_entry != NULL) {
kvn@2292 180 // Clonning the predicate to new location.
kvn@2292 181 entry = new_entry;
kvn@2292 182 }
kvn@2292 183 // Create new_iff
kvn@2292 184 IdealLoopTree* lp = get_loop(entry);
kvn@2292 185 IfNode *new_iff = iff->clone()->as_If();
kvn@2292 186 new_iff->set_req(0, entry);
kvn@2292 187 register_control(new_iff, lp, entry);
kvn@2292 188 Node *if_cont = new (C, 1) IfTrueNode(new_iff);
kvn@2292 189 Node *if_uct = new (C, 1) IfFalseNode(new_iff);
kvn@2292 190 if (cont_proj->is_IfFalse()) {
kvn@2292 191 // Swap
kvn@2292 192 Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
kvn@2292 193 }
kvn@2292 194 register_control(if_cont, lp, new_iff);
kvn@2292 195 register_control(if_uct, get_loop(rgn), new_iff);
kvn@2292 196
kvn@2292 197 // if_uct to rgn
kvn@2292 198 _igvn.hash_delete(rgn);
kvn@2292 199 rgn->add_req(if_uct);
kvn@2292 200 // When called from beautify_loops() idom is not constructed yet.
kvn@2292 201 if (_idom != NULL) {
kvn@2292 202 Node* ridom = idom(rgn);
kvn@2292 203 Node* nrdom = dom_lca(ridom, new_iff);
kvn@2292 204 set_idom(rgn, nrdom, dom_depth(rgn));
kvn@2292 205 }
kvn@2292 206
kvn@2292 207 // If rgn has phis add new edges which has the same
kvn@2292 208 // value as on original uncommon_proj pass.
kvn@2292 209 assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last");
kvn@2292 210 bool has_phi = false;
kvn@2292 211 for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) {
kvn@2292 212 Node* use = rgn->fast_out(i);
kvn@2292 213 if (use->is_Phi() && use->outcnt() > 0) {
kvn@2292 214 assert(use->in(0) == rgn, "");
kvn@2292 215 _igvn.hash_delete(use);
kvn@2292 216 use->add_req(use->in(proj_index));
kvn@2292 217 _igvn._worklist.push(use);
kvn@2292 218 has_phi = true;
kvn@2292 219 }
kvn@2292 220 }
kvn@2292 221 assert(!has_phi || rgn->req() > 3, "no phis when region is created");
kvn@2292 222
kvn@2292 223 if (new_entry == NULL) {
kvn@2292 224 // Attach if_cont to iff
kvn@2292 225 _igvn.hash_delete(iff);
kvn@2292 226 iff->set_req(0, if_cont);
kvn@2292 227 if (_idom != NULL) {
kvn@2292 228 set_idom(iff, if_cont, dom_depth(iff));
kvn@2292 229 }
kvn@2292 230 }
kvn@2292 231 return if_cont->as_Proj();
kvn@2292 232 }
kvn@2292 233
kvn@2292 234 //------------------------------create_new_if_for_predicate------------------------
kvn@2292 235 // Create a new if below new_entry for the predicate to be cloned (IGVN optimization)
kvn@2292 236 ProjNode* PhaseIterGVN::create_new_if_for_predicate(ProjNode* cont_proj, Node* new_entry,
kvn@2292 237 Deoptimization::DeoptReason reason) {
kvn@2292 238 assert(new_entry != 0, "only used for clone predicate");
kvn@2292 239 assert(PhaseIdealLoop::is_uncommon_trap_if_pattern(cont_proj, reason), "must be a uct if pattern!");
kvn@2292 240 IfNode* iff = cont_proj->in(0)->as_If();
kvn@2292 241
kvn@2292 242 ProjNode *uncommon_proj = iff->proj_out(1 - cont_proj->_con);
kvn@2292 243 Node *rgn = uncommon_proj->unique_ctrl_out();
kvn@2292 244 assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
kvn@2292 245
kvn@2292 246 uint proj_index = 1; // region's edge corresponding to uncommon_proj
kvn@2292 247 if (!rgn->is_Region()) { // create a region to guard the call
kvn@2292 248 assert(rgn->is_Call(), "must be call uct");
kvn@2292 249 CallNode* call = rgn->as_Call();
kvn@2292 250 rgn = new (C, 1) RegionNode(1);
kvn@2292 251 register_new_node_with_optimizer(rgn);
kvn@2292 252 rgn->add_req(uncommon_proj);
kvn@2292 253 hash_delete(call);
kvn@2292 254 call->set_req(0, rgn);
kvn@2292 255 } else {
kvn@2292 256 // Find region's edge corresponding to uncommon_proj
kvn@2292 257 for (; proj_index < rgn->req(); proj_index++)
kvn@2292 258 if (rgn->in(proj_index) == uncommon_proj) break;
kvn@2292 259 assert(proj_index < rgn->req(), "sanity");
kvn@2292 260 }
kvn@2292 261
kvn@2292 262 // Create new_iff in new location.
kvn@2292 263 IfNode *new_iff = iff->clone()->as_If();
kvn@2292 264 new_iff->set_req(0, new_entry);
kvn@2292 265
kvn@2292 266 register_new_node_with_optimizer(new_iff);
kvn@2292 267 Node *if_cont = new (C, 1) IfTrueNode(new_iff);
kvn@2292 268 Node *if_uct = new (C, 1) IfFalseNode(new_iff);
kvn@2292 269 if (cont_proj->is_IfFalse()) {
kvn@2292 270 // Swap
kvn@2292 271 Node* tmp = if_uct; if_uct = if_cont; if_cont = tmp;
kvn@2292 272 }
kvn@2292 273 register_new_node_with_optimizer(if_cont);
kvn@2292 274 register_new_node_with_optimizer(if_uct);
kvn@2292 275
kvn@2292 276 // if_uct to rgn
kvn@2292 277 hash_delete(rgn);
kvn@2292 278 rgn->add_req(if_uct);
kvn@2292 279
kvn@2292 280 // If rgn has phis add corresponding new edges which has the same
kvn@2292 281 // value as on original uncommon_proj pass.
kvn@2292 282 assert(rgn->in(rgn->req() -1) == if_uct, "new edge should be last");
kvn@2292 283 bool has_phi = false;
kvn@2292 284 for (DUIterator_Fast imax, i = rgn->fast_outs(imax); i < imax; i++) {
kvn@2292 285 Node* use = rgn->fast_out(i);
kvn@2292 286 if (use->is_Phi() && use->outcnt() > 0) {
kvn@2292 287 hash_delete(use);
kvn@2292 288 use->add_req(use->in(proj_index));
kvn@2292 289 _worklist.push(use);
kvn@2292 290 has_phi = true;
kvn@2292 291 }
kvn@2292 292 }
kvn@2292 293 assert(!has_phi || rgn->req() > 3, "no phis when region is created");
kvn@2292 294
kvn@2292 295 return if_cont->as_Proj();
kvn@2292 296 }
kvn@2292 297
kvn@2292 298 //--------------------------clone_predicate-----------------------
kvn@2292 299 ProjNode* PhaseIdealLoop::clone_predicate(ProjNode* predicate_proj, Node* new_entry,
kvn@2292 300 Deoptimization::DeoptReason reason,
kvn@2292 301 PhaseIdealLoop* loop_phase,
kvn@2292 302 PhaseIterGVN* igvn) {
kvn@2292 303 ProjNode* new_predicate_proj;
kvn@2292 304 if (loop_phase != NULL) {
kvn@2292 305 new_predicate_proj = loop_phase->create_new_if_for_predicate(predicate_proj, new_entry, reason);
kvn@2292 306 } else {
kvn@2292 307 new_predicate_proj = igvn->create_new_if_for_predicate(predicate_proj, new_entry, reason);
kvn@2292 308 }
kvn@2292 309 IfNode* iff = new_predicate_proj->in(0)->as_If();
kvn@2292 310 Node* ctrl = iff->in(0);
kvn@2292 311
kvn@2292 312 // Match original condition since predicate's projections could be swapped.
kvn@2292 313 assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be");
kvn@2292 314 Node* opq = new (igvn->C, 2) Opaque1Node(igvn->C, predicate_proj->in(0)->in(1)->in(1)->in(1));
kvn@2292 315 igvn->C->add_predicate_opaq(opq);
kvn@2292 316
kvn@2292 317 Node* bol = new (igvn->C, 2) Conv2BNode(opq);
kvn@2292 318 if (loop_phase != NULL) {
kvn@2292 319 loop_phase->register_new_node(opq, ctrl);
kvn@2292 320 loop_phase->register_new_node(bol, ctrl);
kvn@2292 321 } else {
kvn@2292 322 igvn->register_new_node_with_optimizer(opq);
kvn@2292 323 igvn->register_new_node_with_optimizer(bol);
kvn@2292 324 }
kvn@2292 325 igvn->hash_delete(iff);
kvn@2292 326 iff->set_req(1, bol);
kvn@2292 327 return new_predicate_proj;
kvn@2292 328 }
kvn@2292 329
kvn@2292 330 //--------------------------move_predicate-----------------------
kvn@2292 331 // Cut predicate from old place and move it to new.
kvn@2292 332 ProjNode* PhaseIdealLoop::move_predicate(ProjNode* predicate_proj, Node* new_entry,
kvn@2292 333 Deoptimization::DeoptReason reason,
kvn@2292 334 PhaseIdealLoop* loop_phase,
kvn@2292 335 PhaseIterGVN* igvn) {
kvn@2292 336 assert(new_entry != NULL, "must be");
kvn@2292 337 assert(predicate_proj->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be");
kvn@2292 338 IfNode* iff = predicate_proj->in(0)->as_If();
kvn@2292 339 Node* old_entry = iff->in(0);
kvn@2292 340
kvn@2292 341 // Cut predicate from old place.
kvn@2292 342 Node* old = predicate_proj;
kvn@2292 343 igvn->_worklist.push(old);
kvn@2292 344 for (DUIterator_Last imin, i = old->last_outs(imin); i >= imin; ) {
kvn@2292 345 Node* use = old->last_out(i); // for each use...
kvn@2292 346 igvn->hash_delete(use);
kvn@2292 347 igvn->_worklist.push(use);
kvn@2292 348 // Update use-def info
kvn@2292 349 uint uses_found = 0;
kvn@2292 350 for (uint j = 0; j < use->req(); j++) {
kvn@2292 351 if (use->in(j) == old) {
kvn@2292 352 use->set_req(j, old_entry);
kvn@2292 353 uses_found++;
kvn@2292 354 if (loop_phase != NULL) {
kvn@2292 355 if (use->is_CFG()) {
kvn@2292 356 // When called from beautify_loops() idom is not constructed yet.
kvn@2292 357 if (loop_phase->_idom != NULL)
kvn@2292 358 loop_phase->set_idom(use, old_entry, loop_phase->dom_depth(use));
kvn@2292 359 } else {
kvn@2292 360 loop_phase->set_ctrl(use, old_entry);
kvn@2292 361 }
kvn@2292 362 }
kvn@2292 363 }
kvn@2292 364 }
kvn@2292 365 i -= uses_found; // we deleted 1 or more copies of this edge
kvn@2292 366 }
kvn@2292 367
kvn@2292 368 // Move predicate.
kvn@2292 369 igvn->hash_delete(iff);
kvn@2292 370 iff->set_req(0, new_entry);
kvn@2292 371 igvn->_worklist.push(iff);
kvn@2292 372
kvn@2292 373 if (loop_phase != NULL) {
kvn@2292 374 // Fix up idom and ctrl.
kvn@2292 375 loop_phase->set_ctrl(iff->in(1), new_entry);
kvn@2292 376 loop_phase->set_ctrl(iff->in(1)->in(1), new_entry);
kvn@2292 377 // When called from beautify_loops() idom is not constructed yet.
kvn@2292 378 if (loop_phase->_idom != NULL)
kvn@2292 379 loop_phase->set_idom(iff, new_entry, loop_phase->dom_depth(iff));
kvn@2292 380 }
kvn@2292 381
kvn@2292 382 return predicate_proj;
kvn@2292 383 }
kvn@2292 384
kvn@2292 385 //--------------------------clone_loop_predicates-----------------------
kvn@2292 386 // Interface from IGVN
kvn@2292 387 Node* PhaseIterGVN::clone_loop_predicates(Node* old_entry, Node* new_entry) {
kvn@2292 388 return PhaseIdealLoop::clone_loop_predicates(old_entry, new_entry, false, NULL, this);
kvn@2292 389 }
kvn@2292 390 Node* PhaseIterGVN::move_loop_predicates(Node* old_entry, Node* new_entry) {
kvn@2292 391 return PhaseIdealLoop::clone_loop_predicates(old_entry, new_entry, true, NULL, this);
kvn@2292 392 }
kvn@2292 393
kvn@2292 394 // Interface from PhaseIdealLoop
kvn@2292 395 Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry) {
kvn@2292 396 return clone_loop_predicates(old_entry, new_entry, false, this, &this->_igvn);
kvn@2292 397 }
kvn@2292 398 Node* PhaseIdealLoop::move_loop_predicates(Node* old_entry, Node* new_entry) {
kvn@2292 399 return clone_loop_predicates(old_entry, new_entry, true, this, &this->_igvn);
kvn@2292 400 }
kvn@2292 401
kvn@2292 402 // Clone loop predicates to cloned loops (peeled, unswitched, split_if).
kvn@2292 403 Node* PhaseIdealLoop::clone_loop_predicates(Node* old_entry, Node* new_entry,
kvn@2292 404 bool move_predicates,
kvn@2292 405 PhaseIdealLoop* loop_phase,
kvn@2292 406 PhaseIterGVN* igvn) {
kvn@2292 407 #ifdef ASSERT
kvn@2292 408 if (new_entry == NULL || !(new_entry->is_Proj() || new_entry->is_Region() || new_entry->is_SafePoint())) {
kvn@2292 409 if (new_entry != NULL)
kvn@2292 410 new_entry->dump();
kvn@2292 411 assert(false, "not IfTrue, IfFalse, Region or SafePoint");
kvn@2292 412 }
kvn@2292 413 #endif
kvn@2292 414 // Search original predicates
kvn@2292 415 Node* entry = old_entry;
kvn@2292 416 if (UseLoopPredicate) {
kvn@2292 417 ProjNode* predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
kvn@2292 418 if (predicate_proj != NULL) { // right pattern that can be used by loop predication
kvn@2292 419 assert(entry->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be");
kvn@2292 420 if (move_predicates) {
kvn@2292 421 new_entry = move_predicate(predicate_proj, new_entry,
kvn@2292 422 Deoptimization::Reason_predicate,
kvn@2292 423 loop_phase, igvn);
kvn@2292 424 assert(new_entry == predicate_proj, "old predicate fall through projection");
kvn@2292 425 } else {
kvn@2292 426 // clone predicate
kvn@2292 427 new_entry = clone_predicate(predicate_proj, new_entry,
kvn@2292 428 Deoptimization::Reason_predicate,
kvn@2292 429 loop_phase, igvn);
kvn@2292 430 assert(new_entry != NULL && new_entry->is_Proj(), "IfTrue or IfFalse after clone predicate");
kvn@2292 431 }
kvn@2292 432 if (TraceLoopPredicate) {
kvn@2292 433 tty->print_cr("Loop Predicate %s: ", move_predicates ? "moved" : "cloned");
kvn@2292 434 debug_only( new_entry->in(0)->dump(); )
kvn@2292 435 }
kvn@2292 436 }
kvn@2292 437 }
kvn@2292 438 return new_entry;
kvn@2292 439 }
kvn@2292 440
kvn@2292 441 //--------------------------eliminate_loop_predicates-----------------------
kvn@2292 442 void PhaseIdealLoop::eliminate_loop_predicates(Node* entry) {
kvn@2292 443 if (UseLoopPredicate) {
kvn@2292 444 ProjNode* predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
kvn@2292 445 if (predicate_proj != NULL) { // right pattern that can be used by loop predication
kvn@2292 446 Node* n = entry->in(0)->in(1)->in(1);
kvn@2292 447 assert(n->Opcode()==Op_Opaque1, "must be");
kvn@2292 448 // Remove Opaque1 node from predicates list.
kvn@2292 449 // IGVN will remove this predicate check.
kvn@2292 450 _igvn.replace_node(n, n->in(1));
kvn@2292 451 }
kvn@2292 452 }
kvn@2292 453 }
kvn@2292 454
kvn@2292 455 //--------------------------skip_loop_predicates------------------------------
kvn@2292 456 // Skip related predicates.
kvn@2292 457 Node* PhaseIdealLoop::skip_loop_predicates(Node* entry) {
kvn@2292 458 Node* predicate = NULL;
kvn@2292 459 if (UseLoopPredicate) {
kvn@2292 460 predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
kvn@2292 461 if (predicate != NULL) { // right pattern that can be used by loop predication
kvn@2292 462 assert(entry->is_Proj() && entry->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be");
kvn@2292 463 IfNode* iff = entry->in(0)->as_If();
kvn@2292 464 ProjNode* uncommon_proj = iff->proj_out(1 - entry->as_Proj()->_con);
kvn@2292 465 Node* rgn = uncommon_proj->unique_ctrl_out();
kvn@2292 466 assert(rgn->is_Region() || rgn->is_Call(), "must be a region or call uct");
kvn@2292 467 entry = entry->in(0)->in(0);
kvn@2292 468 while (entry != NULL && entry->is_Proj() && entry->in(0)->is_If()) {
kvn@2292 469 uncommon_proj = entry->in(0)->as_If()->proj_out(1 - entry->as_Proj()->_con);
kvn@2292 470 if (uncommon_proj->unique_ctrl_out() != rgn)
kvn@2292 471 break;
kvn@2292 472 entry = entry->in(0)->in(0);
kvn@2292 473 }
kvn@2292 474 }
kvn@2292 475 }
kvn@2292 476 return entry;
kvn@2292 477 }
kvn@2292 478
kvn@2292 479 //--------------------------find_predicate_insertion_point-------------------
kvn@2292 480 // Find a good location to insert a predicate
kvn@2292 481 ProjNode* PhaseIdealLoop::find_predicate_insertion_point(Node* start_c, Deoptimization::DeoptReason reason) {
kvn@2292 482 if (start_c == NULL || !start_c->is_Proj())
kvn@2292 483 return NULL;
kvn@2292 484 if (is_uncommon_trap_if_pattern(start_c->as_Proj(), reason)) {
kvn@2292 485 return start_c->as_Proj();
kvn@2292 486 }
kvn@2292 487 return NULL;
kvn@2292 488 }
kvn@2292 489
kvn@2292 490 //--------------------------find_predicate------------------------------------
kvn@2292 491 // Find a predicate
kvn@2292 492 Node* PhaseIdealLoop::find_predicate(Node* entry) {
kvn@2292 493 Node* predicate = NULL;
kvn@2292 494 if (UseLoopPredicate) {
kvn@2292 495 predicate = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
kvn@2292 496 if (predicate != NULL) { // right pattern that can be used by loop predication
kvn@2292 497 assert(entry->in(0)->in(1)->in(1)->Opcode()==Op_Opaque1, "must be");
kvn@2292 498 return entry;
kvn@2292 499 }
kvn@2292 500 }
kvn@2292 501 return NULL;
kvn@2292 502 }
kvn@2292 503
kvn@2292 504 //------------------------------Invariance-----------------------------------
kvn@2292 505 // Helper class for loop_predication_impl to compute invariance on the fly and
kvn@2292 506 // clone invariants.
kvn@2292 507 class Invariance : public StackObj {
kvn@2292 508 VectorSet _visited, _invariant;
kvn@2292 509 Node_Stack _stack;
kvn@2292 510 VectorSet _clone_visited;
kvn@2292 511 Node_List _old_new; // map of old to new (clone)
kvn@2292 512 IdealLoopTree* _lpt;
kvn@2292 513 PhaseIdealLoop* _phase;
kvn@2292 514
kvn@2292 515 // Helper function to set up the invariance for invariance computation
kvn@2292 516 // If n is a known invariant, set up directly. Otherwise, look up the
kvn@2292 517 // the possibility to push n onto the stack for further processing.
kvn@2292 518 void visit(Node* use, Node* n) {
kvn@2292 519 if (_lpt->is_invariant(n)) { // known invariant
kvn@2292 520 _invariant.set(n->_idx);
kvn@2292 521 } else if (!n->is_CFG()) {
kvn@2292 522 Node *n_ctrl = _phase->ctrl_or_self(n);
kvn@2292 523 Node *u_ctrl = _phase->ctrl_or_self(use); // self if use is a CFG
kvn@2292 524 if (_phase->is_dominator(n_ctrl, u_ctrl)) {
kvn@2292 525 _stack.push(n, n->in(0) == NULL ? 1 : 0);
kvn@2292 526 }
kvn@2292 527 }
kvn@2292 528 }
kvn@2292 529
kvn@2292 530 // Compute invariance for "the_node" and (possibly) all its inputs recursively
kvn@2292 531 // on the fly
kvn@2292 532 void compute_invariance(Node* n) {
kvn@2292 533 assert(_visited.test(n->_idx), "must be");
kvn@2292 534 visit(n, n);
kvn@2292 535 while (_stack.is_nonempty()) {
kvn@2292 536 Node* n = _stack.node();
kvn@2292 537 uint idx = _stack.index();
kvn@2292 538 if (idx == n->req()) { // all inputs are processed
kvn@2292 539 _stack.pop();
kvn@2292 540 // n is invariant if it's inputs are all invariant
kvn@2292 541 bool all_inputs_invariant = true;
kvn@2292 542 for (uint i = 0; i < n->req(); i++) {
kvn@2292 543 Node* in = n->in(i);
kvn@2292 544 if (in == NULL) continue;
kvn@2292 545 assert(_visited.test(in->_idx), "must have visited input");
kvn@2292 546 if (!_invariant.test(in->_idx)) { // bad guy
kvn@2292 547 all_inputs_invariant = false;
kvn@2292 548 break;
kvn@2292 549 }
kvn@2292 550 }
kvn@2292 551 if (all_inputs_invariant) {
kvn@2292 552 _invariant.set(n->_idx); // I am a invariant too
kvn@2292 553 }
kvn@2292 554 } else { // process next input
kvn@2292 555 _stack.set_index(idx + 1);
kvn@2292 556 Node* m = n->in(idx);
kvn@2292 557 if (m != NULL && !_visited.test_set(m->_idx)) {
kvn@2292 558 visit(n, m);
kvn@2292 559 }
kvn@2292 560 }
kvn@2292 561 }
kvn@2292 562 }
kvn@2292 563
kvn@2292 564 // Helper function to set up _old_new map for clone_nodes.
kvn@2292 565 // If n is a known invariant, set up directly ("clone" of n == n).
kvn@2292 566 // Otherwise, push n onto the stack for real cloning.
kvn@2292 567 void clone_visit(Node* n) {
kvn@2292 568 assert(_invariant.test(n->_idx), "must be invariant");
kvn@2292 569 if (_lpt->is_invariant(n)) { // known invariant
kvn@2292 570 _old_new.map(n->_idx, n);
kvn@2292 571 } else { // to be cloned
kvn@2292 572 assert(!n->is_CFG(), "should not see CFG here");
kvn@2292 573 _stack.push(n, n->in(0) == NULL ? 1 : 0);
kvn@2292 574 }
kvn@2292 575 }
kvn@2292 576
kvn@2292 577 // Clone "n" and (possibly) all its inputs recursively
kvn@2292 578 void clone_nodes(Node* n, Node* ctrl) {
kvn@2292 579 clone_visit(n);
kvn@2292 580 while (_stack.is_nonempty()) {
kvn@2292 581 Node* n = _stack.node();
kvn@2292 582 uint idx = _stack.index();
kvn@2292 583 if (idx == n->req()) { // all inputs processed, clone n!
kvn@2292 584 _stack.pop();
kvn@2292 585 // clone invariant node
kvn@2292 586 Node* n_cl = n->clone();
kvn@2292 587 _old_new.map(n->_idx, n_cl);
kvn@2292 588 _phase->register_new_node(n_cl, ctrl);
kvn@2292 589 for (uint i = 0; i < n->req(); i++) {
kvn@2292 590 Node* in = n_cl->in(i);
kvn@2292 591 if (in == NULL) continue;
kvn@2292 592 n_cl->set_req(i, _old_new[in->_idx]);
kvn@2292 593 }
kvn@2292 594 } else { // process next input
kvn@2292 595 _stack.set_index(idx + 1);
kvn@2292 596 Node* m = n->in(idx);
kvn@2292 597 if (m != NULL && !_clone_visited.test_set(m->_idx)) {
kvn@2292 598 clone_visit(m); // visit the input
kvn@2292 599 }
kvn@2292 600 }
kvn@2292 601 }
kvn@2292 602 }
kvn@2292 603
kvn@2292 604 public:
kvn@2292 605 Invariance(Arena* area, IdealLoopTree* lpt) :
kvn@2292 606 _lpt(lpt), _phase(lpt->_phase),
kvn@2292 607 _visited(area), _invariant(area), _stack(area, 10 /* guess */),
kvn@2292 608 _clone_visited(area), _old_new(area)
kvn@2292 609 {}
kvn@2292 610
kvn@2292 611 // Map old to n for invariance computation and clone
kvn@2292 612 void map_ctrl(Node* old, Node* n) {
kvn@2292 613 assert(old->is_CFG() && n->is_CFG(), "must be");
kvn@2292 614 _old_new.map(old->_idx, n); // "clone" of old is n
kvn@2292 615 _invariant.set(old->_idx); // old is invariant
kvn@2292 616 _clone_visited.set(old->_idx);
kvn@2292 617 }
kvn@2292 618
kvn@2292 619 // Driver function to compute invariance
kvn@2292 620 bool is_invariant(Node* n) {
kvn@2292 621 if (!_visited.test_set(n->_idx))
kvn@2292 622 compute_invariance(n);
kvn@2292 623 return (_invariant.test(n->_idx) != 0);
kvn@2292 624 }
kvn@2292 625
kvn@2292 626 // Driver function to clone invariant
kvn@2292 627 Node* clone(Node* n, Node* ctrl) {
kvn@2292 628 assert(ctrl->is_CFG(), "must be");
kvn@2292 629 assert(_invariant.test(n->_idx), "must be an invariant");
kvn@2292 630 if (!_clone_visited.test(n->_idx))
kvn@2292 631 clone_nodes(n, ctrl);
kvn@2292 632 return _old_new[n->_idx];
kvn@2292 633 }
kvn@2292 634 };
kvn@2292 635
kvn@2292 636 //------------------------------is_range_check_if -----------------------------------
kvn@2292 637 // Returns true if the predicate of iff is in "scale*iv + offset u< load_range(ptr)" format
kvn@2292 638 // Note: this function is particularly designed for loop predication. We require load_range
kvn@2292 639 // and offset to be loop invariant computed on the fly by "invar"
kvn@2292 640 bool IdealLoopTree::is_range_check_if(IfNode *iff, PhaseIdealLoop *phase, Invariance& invar) const {
kvn@2292 641 if (!is_loop_exit(iff)) {
kvn@2292 642 return false;
kvn@2292 643 }
kvn@2292 644 if (!iff->in(1)->is_Bool()) {
kvn@2292 645 return false;
kvn@2292 646 }
kvn@2292 647 const BoolNode *bol = iff->in(1)->as_Bool();
kvn@2292 648 if (bol->_test._test != BoolTest::lt) {
kvn@2292 649 return false;
kvn@2292 650 }
kvn@2292 651 if (!bol->in(1)->is_Cmp()) {
kvn@2292 652 return false;
kvn@2292 653 }
kvn@2292 654 const CmpNode *cmp = bol->in(1)->as_Cmp();
kvn@2292 655 if (cmp->Opcode() != Op_CmpU) {
kvn@2292 656 return false;
kvn@2292 657 }
kvn@2292 658 Node* range = cmp->in(2);
kvn@2292 659 if (range->Opcode() != Op_LoadRange) {
kvn@2292 660 const TypeInt* tint = phase->_igvn.type(range)->isa_int();
kvn@2292 661 if (!OptimizeFill || tint == NULL || tint->empty() || tint->_lo < 0) {
kvn@2292 662 // Allow predication on positive values that aren't LoadRanges.
kvn@2292 663 // This allows optimization of loops where the length of the
kvn@2292 664 // array is a known value and doesn't need to be loaded back
kvn@2292 665 // from the array.
kvn@2292 666 return false;
kvn@2292 667 }
kvn@2292 668 }
kvn@2292 669 if (!invar.is_invariant(range)) {
kvn@2292 670 return false;
kvn@2292 671 }
kvn@2292 672 Node *iv = _head->as_CountedLoop()->phi();
kvn@2292 673 int scale = 0;
kvn@2292 674 Node *offset = NULL;
kvn@2292 675 if (!phase->is_scaled_iv_plus_offset(cmp->in(1), iv, &scale, &offset)) {
kvn@2292 676 return false;
kvn@2292 677 }
kvn@2292 678 if (offset && !invar.is_invariant(offset)) { // offset must be invariant
kvn@2292 679 return false;
kvn@2292 680 }
kvn@2292 681 return true;
kvn@2292 682 }
kvn@2292 683
kvn@2292 684 //------------------------------rc_predicate-----------------------------------
kvn@2292 685 // Create a range check predicate
kvn@2292 686 //
kvn@2292 687 // for (i = init; i < limit; i += stride) {
kvn@2292 688 // a[scale*i+offset]
kvn@2292 689 // }
kvn@2292 690 //
kvn@2292 691 // Compute max(scale*i + offset) for init <= i < limit and build the predicate
kvn@2292 692 // as "max(scale*i + offset) u< a.length".
kvn@2292 693 //
kvn@2292 694 // There are two cases for max(scale*i + offset):
kvn@2292 695 // (1) stride*scale > 0
kvn@2292 696 // max(scale*i + offset) = scale*(limit-stride) + offset
kvn@2292 697 // (2) stride*scale < 0
kvn@2292 698 // max(scale*i + offset) = scale*init + offset
kvn@2292 699 BoolNode* PhaseIdealLoop::rc_predicate(Node* ctrl,
kvn@2292 700 int scale, Node* offset,
kvn@2292 701 Node* init, Node* limit, Node* stride,
kvn@2292 702 Node* range, bool upper) {
never@2433 703 stringStream* predString = NULL;
never@2433 704 if (TraceLoopPredicate) {
never@2433 705 predString = new stringStream();
never@2433 706 predString->print("rc_predicate ");
never@2433 707 }
kvn@2292 708
kvn@2292 709 Node* max_idx_expr = init;
kvn@2292 710 int stride_con = stride->get_int();
kvn@2292 711 if ((stride_con > 0) == (scale > 0) == upper) {
kvn@2292 712 max_idx_expr = new (C, 3) SubINode(limit, stride);
kvn@2292 713 register_new_node(max_idx_expr, ctrl);
never@2433 714 if (TraceLoopPredicate) predString->print("(limit - stride) ");
kvn@2292 715 } else {
never@2433 716 if (TraceLoopPredicate) predString->print("init ");
kvn@2292 717 }
kvn@2292 718
kvn@2292 719 if (scale != 1) {
kvn@2292 720 ConNode* con_scale = _igvn.intcon(scale);
kvn@2292 721 max_idx_expr = new (C, 3) MulINode(max_idx_expr, con_scale);
kvn@2292 722 register_new_node(max_idx_expr, ctrl);
never@2433 723 if (TraceLoopPredicate) predString->print("* %d ", scale);
kvn@2292 724 }
kvn@2292 725
kvn@2292 726 if (offset && (!offset->is_Con() || offset->get_int() != 0)){
kvn@2292 727 max_idx_expr = new (C, 3) AddINode(max_idx_expr, offset);
kvn@2292 728 register_new_node(max_idx_expr, ctrl);
kvn@2292 729 if (TraceLoopPredicate)
never@2433 730 if (offset->is_Con()) predString->print("+ %d ", offset->get_int());
never@2433 731 else predString->print("+ offset ");
kvn@2292 732 }
kvn@2292 733
kvn@2292 734 CmpUNode* cmp = new (C, 3) CmpUNode(max_idx_expr, range);
kvn@2292 735 register_new_node(cmp, ctrl);
kvn@2292 736 BoolNode* bol = new (C, 2) BoolNode(cmp, BoolTest::lt);
kvn@2292 737 register_new_node(bol, ctrl);
kvn@2292 738
never@2433 739 if (TraceLoopPredicate) {
never@2433 740 predString->print_cr("<u range");
never@2433 741 tty->print(predString->as_string());
never@2433 742 }
kvn@2292 743 return bol;
kvn@2292 744 }
kvn@2292 745
kvn@2292 746 //------------------------------ loop_predication_impl--------------------------
kvn@2292 747 // Insert loop predicates for null checks and range checks
kvn@2292 748 bool PhaseIdealLoop::loop_predication_impl(IdealLoopTree *loop) {
kvn@2292 749 if (!UseLoopPredicate) return false;
kvn@2292 750
kvn@2292 751 if (!loop->_head->is_Loop()) {
kvn@2292 752 // Could be a simple region when irreducible loops are present.
kvn@2292 753 return false;
kvn@2292 754 }
kvn@2292 755
kvn@2292 756 if (loop->_head->unique_ctrl_out()->Opcode() == Op_NeverBranch) {
kvn@2292 757 // do nothing for infinite loops
kvn@2292 758 return false;
kvn@2292 759 }
kvn@2292 760
kvn@2292 761 CountedLoopNode *cl = NULL;
kvn@2292 762 if (loop->_head->is_CountedLoop()) {
kvn@2292 763 cl = loop->_head->as_CountedLoop();
kvn@2292 764 // do nothing for iteration-splitted loops
kvn@2292 765 if (!cl->is_normal_loop()) return false;
kvn@2292 766 }
kvn@2292 767
kvn@2292 768 LoopNode *lpn = loop->_head->as_Loop();
kvn@2292 769 Node* entry = lpn->in(LoopNode::EntryControl);
kvn@2292 770
kvn@2292 771 ProjNode *predicate_proj = find_predicate_insertion_point(entry, Deoptimization::Reason_predicate);
kvn@2292 772 if (!predicate_proj) {
kvn@2292 773 #ifndef PRODUCT
kvn@2292 774 if (TraceLoopPredicate) {
kvn@2292 775 tty->print("missing predicate:");
kvn@2292 776 loop->dump_head();
kvn@2292 777 lpn->dump(1);
kvn@2292 778 }
kvn@2292 779 #endif
kvn@2292 780 return false;
kvn@2292 781 }
kvn@2292 782 ConNode* zero = _igvn.intcon(0);
kvn@2292 783 set_ctrl(zero, C->root());
kvn@2292 784
kvn@2292 785 ResourceArea *area = Thread::current()->resource_area();
kvn@2292 786 Invariance invar(area, loop);
kvn@2292 787
kvn@2292 788 // Create list of if-projs such that a newer proj dominates all older
kvn@2292 789 // projs in the list, and they all dominate loop->tail()
kvn@2292 790 Node_List if_proj_list(area);
kvn@2292 791 LoopNode *head = loop->_head->as_Loop();
kvn@2292 792 Node *current_proj = loop->tail(); //start from tail
kvn@2292 793 while (current_proj != head) {
kvn@2292 794 if (loop == get_loop(current_proj) && // still in the loop ?
kvn@2292 795 current_proj->is_Proj() && // is a projection ?
kvn@2292 796 current_proj->in(0)->Opcode() == Op_If) { // is a if projection ?
kvn@2292 797 if_proj_list.push(current_proj);
kvn@2292 798 }
kvn@2292 799 current_proj = idom(current_proj);
kvn@2292 800 }
kvn@2292 801
kvn@2292 802 bool hoisted = false; // true if at least one proj is promoted
kvn@2292 803 while (if_proj_list.size() > 0) {
kvn@2292 804 // Following are changed to nonnull when a predicate can be hoisted
kvn@2292 805 ProjNode* new_predicate_proj = NULL;
kvn@2292 806
kvn@2292 807 ProjNode* proj = if_proj_list.pop()->as_Proj();
kvn@2292 808 IfNode* iff = proj->in(0)->as_If();
kvn@2292 809
kvn@2292 810 if (!is_uncommon_trap_if_pattern(proj, Deoptimization::Reason_none)) {
kvn@2292 811 if (loop->is_loop_exit(iff)) {
kvn@2292 812 // stop processing the remaining projs in the list because the execution of them
kvn@2292 813 // depends on the condition of "iff" (iff->in(1)).
kvn@2292 814 break;
kvn@2292 815 } else {
kvn@2292 816 // Both arms are inside the loop. There are two cases:
kvn@2292 817 // (1) there is one backward branch. In this case, any remaining proj
kvn@2292 818 // in the if_proj list post-dominates "iff". So, the condition of "iff"
kvn@2292 819 // does not determine the execution the remining projs directly, and we
kvn@2292 820 // can safely continue.
kvn@2292 821 // (2) both arms are forwarded, i.e. a diamond shape. In this case, "proj"
kvn@2292 822 // does not dominate loop->tail(), so it can not be in the if_proj list.
kvn@2292 823 continue;
kvn@2292 824 }
kvn@2292 825 }
kvn@2292 826
kvn@2292 827 Node* test = iff->in(1);
kvn@2292 828 if (!test->is_Bool()){ //Conv2B, ...
kvn@2292 829 continue;
kvn@2292 830 }
kvn@2292 831 BoolNode* bol = test->as_Bool();
kvn@2292 832 if (invar.is_invariant(bol)) {
kvn@2292 833 // Invariant test
kvn@2292 834 new_predicate_proj = create_new_if_for_predicate(predicate_proj, NULL,
kvn@2292 835 Deoptimization::Reason_predicate);
kvn@2292 836 Node* ctrl = new_predicate_proj->in(0)->as_If()->in(0);
kvn@2292 837 BoolNode* new_predicate_bol = invar.clone(bol, ctrl)->as_Bool();
kvn@2292 838
kvn@2292 839 // Negate test if necessary
kvn@2292 840 bool negated = false;
kvn@2292 841 if (proj->_con != predicate_proj->_con) {
kvn@2292 842 new_predicate_bol = new (C, 2) BoolNode(new_predicate_bol->in(1), new_predicate_bol->_test.negate());
kvn@2292 843 register_new_node(new_predicate_bol, ctrl);
kvn@2292 844 negated = true;
kvn@2292 845 }
kvn@2292 846 IfNode* new_predicate_iff = new_predicate_proj->in(0)->as_If();
kvn@2292 847 _igvn.hash_delete(new_predicate_iff);
kvn@2292 848 new_predicate_iff->set_req(1, new_predicate_bol);
kvn@2292 849 #ifndef PRODUCT
kvn@2292 850 if (TraceLoopPredicate) {
kvn@2292 851 tty->print("Predicate invariant if%s: %d ", negated ? " negated" : "", new_predicate_iff->_idx);
kvn@2292 852 loop->dump_head();
kvn@2292 853 } else if (TraceLoopOpts) {
kvn@2292 854 tty->print("Predicate IC ");
kvn@2292 855 loop->dump_head();
kvn@2292 856 }
kvn@2292 857 #endif
kvn@2292 858 } else if (cl != NULL && loop->is_range_check_if(iff, this, invar)) {
kvn@2292 859 assert(proj->_con == predicate_proj->_con, "must match");
kvn@2292 860
kvn@2292 861 // Range check for counted loops
kvn@2292 862 const Node* cmp = bol->in(1)->as_Cmp();
kvn@2292 863 Node* idx = cmp->in(1);
kvn@2292 864 assert(!invar.is_invariant(idx), "index is variant");
kvn@2292 865 assert(cmp->in(2)->Opcode() == Op_LoadRange || OptimizeFill, "must be");
kvn@2292 866 Node* rng = cmp->in(2);
kvn@2292 867 assert(invar.is_invariant(rng), "range must be invariant");
kvn@2292 868 int scale = 1;
kvn@2292 869 Node* offset = zero;
kvn@2292 870 bool ok = is_scaled_iv_plus_offset(idx, cl->phi(), &scale, &offset);
kvn@2292 871 assert(ok, "must be index expression");
kvn@2292 872
kvn@2292 873 Node* init = cl->init_trip();
kvn@2292 874 Node* limit = cl->limit();
kvn@2292 875 Node* stride = cl->stride();
kvn@2292 876
kvn@2292 877 // Build if's for the upper and lower bound tests. The
kvn@2292 878 // lower_bound test will dominate the upper bound test and all
kvn@2292 879 // cloned or created nodes will use the lower bound test as
kvn@2292 880 // their declared control.
kvn@2292 881 ProjNode* lower_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate);
kvn@2292 882 ProjNode* upper_bound_proj = create_new_if_for_predicate(predicate_proj, NULL, Deoptimization::Reason_predicate);
kvn@2292 883 assert(upper_bound_proj->in(0)->as_If()->in(0) == lower_bound_proj, "should dominate");
kvn@2292 884 Node *ctrl = lower_bound_proj->in(0)->as_If()->in(0);
kvn@2292 885
kvn@2292 886 // Perform cloning to keep Invariance state correct since the
kvn@2292 887 // late schedule will place invariant things in the loop.
kvn@2292 888 rng = invar.clone(rng, ctrl);
kvn@2292 889 if (offset && offset != zero) {
kvn@2292 890 assert(invar.is_invariant(offset), "offset must be loop invariant");
kvn@2292 891 offset = invar.clone(offset, ctrl);
kvn@2292 892 }
kvn@2292 893
kvn@2292 894 // Test the lower bound
kvn@2292 895 Node* lower_bound_bol = rc_predicate(ctrl, scale, offset, init, limit, stride, rng, false);
kvn@2292 896 IfNode* lower_bound_iff = lower_bound_proj->in(0)->as_If();
kvn@2292 897 _igvn.hash_delete(lower_bound_iff);
kvn@2292 898 lower_bound_iff->set_req(1, lower_bound_bol);
kvn@2292 899 if (TraceLoopPredicate) tty->print_cr("lower bound check if: %d", lower_bound_iff->_idx);
kvn@2292 900
kvn@2292 901 // Test the upper bound
kvn@2292 902 Node* upper_bound_bol = rc_predicate(ctrl, scale, offset, init, limit, stride, rng, true);
kvn@2292 903 IfNode* upper_bound_iff = upper_bound_proj->in(0)->as_If();
kvn@2292 904 _igvn.hash_delete(upper_bound_iff);
kvn@2292 905 upper_bound_iff->set_req(1, upper_bound_bol);
kvn@2292 906 if (TraceLoopPredicate) tty->print_cr("upper bound check if: %d", lower_bound_iff->_idx);
kvn@2292 907
kvn@2292 908 // Fall through into rest of the clean up code which will move
kvn@2292 909 // any dependent nodes onto the upper bound test.
kvn@2292 910 new_predicate_proj = upper_bound_proj;
kvn@2292 911
kvn@2292 912 #ifndef PRODUCT
kvn@2292 913 if (TraceLoopOpts && !TraceLoopPredicate) {
kvn@2292 914 tty->print("Predicate RC ");
kvn@2292 915 loop->dump_head();
kvn@2292 916 }
kvn@2292 917 #endif
kvn@2292 918 } else {
kvn@2292 919 // Loop variant check (for example, range check in non-counted loop)
kvn@2292 920 // with uncommon trap.
kvn@2292 921 continue;
kvn@2292 922 }
kvn@2292 923 assert(new_predicate_proj != NULL, "sanity");
kvn@2292 924 // Success - attach condition (new_predicate_bol) to predicate if
kvn@2292 925 invar.map_ctrl(proj, new_predicate_proj); // so that invariance test can be appropriate
kvn@2292 926
kvn@2292 927 // Eliminate the old If in the loop body
kvn@2292 928 dominated_by( new_predicate_proj, iff, proj->_con != new_predicate_proj->_con );
kvn@2292 929
kvn@2292 930 hoisted = true;
kvn@2292 931 C->set_major_progress();
kvn@2292 932 } // end while
kvn@2292 933
kvn@2292 934 #ifndef PRODUCT
kvn@2292 935 // report that the loop predication has been actually performed
kvn@2292 936 // for this loop
kvn@2292 937 if (TraceLoopPredicate && hoisted) {
kvn@2292 938 tty->print("Loop Predication Performed:");
kvn@2292 939 loop->dump_head();
kvn@2292 940 }
kvn@2292 941 #endif
kvn@2292 942
kvn@2292 943 return hoisted;
kvn@2292 944 }
kvn@2292 945
kvn@2292 946 //------------------------------loop_predication--------------------------------
kvn@2292 947 // driver routine for loop predication optimization
kvn@2292 948 bool IdealLoopTree::loop_predication( PhaseIdealLoop *phase) {
kvn@2292 949 bool hoisted = false;
kvn@2292 950 // Recursively promote predicates
kvn@2292 951 if (_child) {
kvn@2292 952 hoisted = _child->loop_predication( phase);
kvn@2292 953 }
kvn@2292 954
kvn@2292 955 // self
kvn@2292 956 if (!_irreducible && !tail()->is_top()) {
kvn@2292 957 hoisted |= phase->loop_predication_impl(this);
kvn@2292 958 }
kvn@2292 959
kvn@2292 960 if (_next) { //sibling
kvn@2292 961 hoisted |= _next->loop_predication( phase);
kvn@2292 962 }
kvn@2292 963
kvn@2292 964 return hoisted;
kvn@2292 965 }